metaBDBImpl.c 9.0 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

H
more  
Hongze Cheng 已提交
16 17
#include "db.h"

H
Hongze Cheng 已提交
18 19
#include "metaDef.h"

H
Hongze Cheng 已提交
20
#include "tcoding.h"
H
more  
Hongze Cheng 已提交
21
#include "thash.h"
H
Hongze Cheng 已提交
22 23

struct SMetaDB {
H
more  
Hongze Cheng 已提交
24
  // DB
H
more  
Hongze Cheng 已提交
25 26
  DB *pTbDB;
  DB *pSchemaDB;
H
more  
Hongze Cheng 已提交
27
  // IDX
H
more  
Hongze Cheng 已提交
28 29 30 31
  DB *pNameIdx;
  DB *pStbIdx;
  DB *pNtbIdx;
  DB *pCtbIdx;
H
more  
Hongze Cheng 已提交
32
  // ENV
H
Hongze Cheng 已提交
33 34 35
  DB_ENV *pEvn;
};

H
more  
Hongze Cheng 已提交
36 37
static SMetaDB *metaNewDB();
static void     metaFreeDB(SMetaDB *pDB);
H
more  
Hongze Cheng 已提交
38 39 40 41
static int      metaOpenBDBEnv(DB_ENV **ppEnv, const char *path);
static void     metaCloseBDBEnv(DB_ENV *pEnv);
static int      metaOpenBDBDb(DB **ppDB, DB_ENV *pEnv, const char *pFName);
static void     metaCloseBDBDb(DB *pDB);
H
more  
Hongze Cheng 已提交
42

H
more  
Hongze Cheng 已提交
43 44
#define BDB_PERR(info, code) fprintf(stderr, info " reason: %s", db_strerror(code))
#define metaOpenBDBIdx metaOpenBDBDb
H
Hongze Cheng 已提交
45

H
Hongze Cheng 已提交
46
int metaOpenDB(SMeta *pMeta) {
H
more  
Hongze Cheng 已提交
47
  SMetaDB *pDB;
H
Hongze Cheng 已提交
48

H
more  
Hongze Cheng 已提交
49 50 51
  // Create DB object
  pDB = metaNewDB();
  if (pDB == NULL) {
H
more  
Hongze Cheng 已提交
52 53 54
    return -1;
  }

H
more  
Hongze Cheng 已提交
55
  pMeta->pDB = pDB;
H
more  
Hongze Cheng 已提交
56

H
more  
Hongze Cheng 已提交
57 58
  // Open DB Env
  if (metaOpenBDBEnv(&(pDB->pEvn), pMeta->path) < 0) {
H
more  
Hongze Cheng 已提交
59
    metaCloseDB(pMeta);
H
more  
Hongze Cheng 已提交
60 61
    return -1;
  }
H
Hongze Cheng 已提交
62

H
more  
Hongze Cheng 已提交
63 64 65 66
  // Open DBs
  if (metaOpenBDBDb(&(pDB->pTbDB), pDB->pEvn, "meta.db") < 0) {
    metaCloseDB(pMeta);
    return -1;
H
more  
Hongze Cheng 已提交
67 68
  }

H
more  
Hongze Cheng 已提交
69 70 71 72
  if (metaOpenBDBDb(&(pDB->pSchemaDB), pDB->pEvn, "meta.db") < 0) {
    metaCloseDB(pMeta);
    return -1;
  }
H
Hongze Cheng 已提交
73

H
more  
Hongze Cheng 已提交
74 75 76 77 78
  // Open Indices
  if (metaOpenBDBIdx(&(pDB->pNameIdx), pDB->pEvn, "index.db") < 0) {
    metaCloseDB(pMeta);
    return -1;
  }
H
Hongze Cheng 已提交
79

H
more  
Hongze Cheng 已提交
80 81 82 83
  if (metaOpenBDBIdx(&(pDB->pStbIdx), pDB->pEvn, "index.db") < 0) {
    metaCloseDB(pMeta);
    return -1;
  }
H
Hongze Cheng 已提交
84

H
more  
Hongze Cheng 已提交
85 86 87
  if (metaOpenBDBIdx(&(pDB->pNtbIdx), pDB->pEvn, "index.db") < 0) {
    metaCloseDB(pMeta);
    return -1;
H
Hongze Cheng 已提交
88 89
  }

H
more  
Hongze Cheng 已提交
90 91 92
  if (metaOpenBDBIdx(&(pDB->pCtbIdx), pDB->pEvn, "index.db") < 0) {
    metaCloseDB(pMeta);
    return -1;
H
more  
Hongze Cheng 已提交
93
  }
H
more  
Hongze Cheng 已提交
94
  // Associate Indices
H
more  
Hongze Cheng 已提交
95

H
Hongze Cheng 已提交
96 97 98 99 100
  return 0;
}

void metaCloseDB(SMeta *pMeta) {
  if (pMeta->pDB) {
H
more  
Hongze Cheng 已提交
101
    metaCloseBDBEnv(pMeta->pDB->pEvn);
H
more  
Hongze Cheng 已提交
102 103
    metaFreeDB(pMeta->pDB);
    pMeta->pDB = NULL;
H
Hongze Cheng 已提交
104 105 106
  }
}

H
more  
Hongze Cheng 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) {
  // TODO
  return 0;
}

int metaRemoveTableFromDb(SMeta *pMeta, tb_uid_t uid) {
  // TODO
  return 0;
}

/* ------------------------ STATIC METHODS ------------------------ */
static SMetaDB *metaNewDB() {
  SMetaDB *pDB = NULL;
  pDB = (SMetaDB *)calloc(1, sizeof(*pDB));
  if (pDB == NULL) {
    return NULL;
  }

  return pDB;
}

static void metaFreeDB(SMetaDB *pDB) {
  if (pDB) {
    free(pDB);
  }
}

static int metaOpenBDBEnv(DB_ENV **ppEnv, const char *path) {
  int     ret;
  DB_ENV *pEnv;

  if (path == NULL) return 0;

  ret = db_env_create(&pEnv, 0);
  if (ret != 0) {
    BDB_PERR("Failed to create META env", ret);
    return -1;
  }

  ret = pEnv->open(pEnv, path, DB_CREATE | DB_INIT_MPOOL, 0);
  if (ret != 0) {
    BDB_PERR("Failed to open META env", ret);
    return -1;
  }

  *ppEnv = pEnv;

  return 0;
}

static void metaCloseBDBEnv(DB_ENV *pEnv) {
  if (pEnv) {
    pEnv->close(pEnv, 0);
  }
}

static int metaOpenBDBDb(DB **ppDB, DB_ENV *pEnv, const char *pFName) {
  int ret;
  DB *pDB;

  ret = db_create(&((pDB)), (pEnv), 0);
  if (ret != 0) {
    BDB_PERR("Failed to create META DB", ret);
    return -1;
  }

  ret = pDB->open(pDB, NULL, pFName, NULL, DB_BTREE, DB_CREATE, 0);
  if (ret) {
    BDB_PERR("Failed to open META DB", ret);
    return -1;
  }

  return 0;
}

static void metaCloseBDBDb(DB *pDB) {
  if (pDB) {
    pDB->close(pDB, 0);
  }
}

#if 0
typedef struct {
  tb_uid_t uid;
  int32_t  sver;
} SSchemaKey;


static SMetaDB *metaNewDB();
static void     metaFreeDB(SMetaDB *pDB);
static int      metaCreateDBEnv(SMetaDB *pDB, const char *path);
static void     metaDestroyDBEnv(SMetaDB *pDB);
static int      metaEncodeSchemaKey(void **buf, SSchemaKey *pSchemaKey);
static void *   metaDecodeSchemaKey(void *buf, SSchemaKey *pSchemaKey);
static int      metaNameIdxCb(DB *sdbp, const DBT *pKey, const DBT *pValue, DBT *pSKey);
static int      metaUidIdxCb(DB *sdbp, const DBT *pKey, const DBT *pValue, DBT *pSKey);
static void     metaPutSchema(SMeta *pMeta, tb_uid_t uid, STSchema *pSchema);
static int      metaEncodeTbInfo(void **buf, STbCfg *pTbCfg);
static void *   metaDecodeTbInfo(void *buf, STbCfg *pTbCfg);
static int      metaSaveTbInfo(DB *pDB, tb_uid_t uid, STbCfg *pTbCfg);

#define META_ASSOCIATE_IDX(pDB, pIdx, cbf)                     \
  do {                                                         \
    int ret = (pDB)->associate((pDB), NULL, (pIdx), (cbf), 0); \
    if (ret != 0) {                                            \
      P_ERROR("Failed to associate META DB", ret);             \
      metaCloseDB(pMeta);                                      \
    }                                                          \
  } while (0)


H
more  
Hongze Cheng 已提交
218
int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) {
H
Hongze Cheng 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
  char       buf[512];
  void *     pBuf;
  DBT        key = {0};
  DBT        value = {0};
  SSchemaKey schemaKey;
  tb_uid_t   uid;

  if (pTbCfg->type == META_SUPER_TABLE) {
    // Handle SUPER table
    uid = pTbCfg->stbCfg.suid;

    // Same table info
    metaSaveTbInfo(pMeta->pDB->pStbDB, uid, pTbCfg);

    // save schema
    metaPutSchema(pMeta, uid, pTbCfg->stbCfg.pSchema);

    {
      // Create a super table DB and corresponding index DB
      DB *pStbDB;
      DB *pStbIdxDB;

      META_OPEN_DB(pStbDB, pMeta->pDB->pEvn, "meta.db");

      META_OPEN_DB(pStbIdxDB, pMeta->pDB->pEvn, "index.db");

      // TODO META_ASSOCIATE_IDX();
    }
  } else if (pTbCfg->type == META_CHILD_TABLE) {
    // Handle CHILD table
    uid = metaGenerateUid(pMeta);

    DB *pCTbDB = taosHashGet(pMeta->pDB->pCtbMap, &(pTbCfg->ctbCfg.suid), sizeof(pTbCfg->ctbCfg.suid));
    if (pCTbDB == NULL) {
      ASSERT(0);
    }

    metaSaveTbInfo(pCTbDB, uid, pTbCfg);

  } else if (pTbCfg->type == META_NORMAL_TABLE) {
    // Handle NORMAL table
    uid = metaGenerateUid(pMeta);

    metaSaveTbInfo(pMeta->pDB->pNtbDB, uid, pTbCfg);

    metaPutSchema(pMeta, uid, pTbCfg->stbCfg.pSchema);
  } else {
    ASSERT(0);
  }

H
Hongze Cheng 已提交
269 270 271 272 273
  return 0;
}

int metaRemoveTableFromDb(SMeta *pMeta, tb_uid_t uid) {
  // TODO
H
more  
Hongze Cheng 已提交
274 275 276
}

/* ------------------------ STATIC METHODS ------------------------ */
H
Hongze Cheng 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
static int metaEncodeSchemaKey(void **buf, SSchemaKey *pSchemaKey) {
  int tsize = 0;

  tsize += taosEncodeFixedU64(buf, pSchemaKey->uid);
  tsize += taosEncodeFixedI32(buf, pSchemaKey->sver);

  return tsize;
}

static void *metaDecodeSchemaKey(void *buf, SSchemaKey *pSchemaKey) {
  buf = taosDecodeFixedU64(buf, &(pSchemaKey->uid));
  buf = taosDecodeFixedI32(buf, &(pSchemaKey->sver));

  return buf;
}

static int metaNameIdxCb(DB *sdbp, const DBT *pKey, const DBT *pValue, DBT *pSKey) {
  // TODO
  return 0;
}

static int metaUidIdxCb(DB *sdbp, const DBT *pKey, const DBT *pValue, DBT *pSKey) {
  // TODO
  return 0;
}

static void metaPutSchema(SMeta *pMeta, tb_uid_t uid, STSchema *pSchema) {
  SSchemaKey skey;
  char       buf[256];
  void *     pBuf = buf;
  DBT        key = {0};
  DBT        value = {0};

  skey.uid = uid;
  skey.sver = schemaVersion(pSchema);

  key.data = &skey;
  key.size = sizeof(skey);

  tdEncodeSchema(&pBuf, pSchema);
  value.data = buf;
  value.size = POINTER_DISTANCE(pBuf, buf);

  pMeta->pDB->pSchemaDB->put(pMeta->pDB->pSchemaDB, NULL, &key, &value, 0);
}

static int metaEncodeTbInfo(void **buf, STbCfg *pTbCfg) {
H
more  
Hongze Cheng 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
  int tsize = 0;

  tsize += taosEncodeString(buf, pTbCfg->name);
  tsize += taosEncodeFixedU32(buf, pTbCfg->ttl);
  tsize += taosEncodeFixedU32(buf, pTbCfg->keep);

  if (pTbCfg->type == META_SUPER_TABLE) {
    tsize += tdEncodeSchema(buf, pTbCfg->stbCfg.pTagSchema);
  } else if (pTbCfg->type == META_CHILD_TABLE) {
    tsize += taosEncodeFixedU64(buf, pTbCfg->ctbCfg.suid);
    tsize += tdEncodeKVRow(buf, pTbCfg->ctbCfg.pTag);
  } else if (pTbCfg->type == META_NORMAL_TABLE) {
  } else {
    ASSERT(0);
  }

  return tsize;
H
Hongze Cheng 已提交
341 342 343 344
}

static void *metaDecodeTbInfo(void *buf, STbCfg *pTbCfg) {
  // TODO
H
more  
Hongze Cheng 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358
  buf = taosDecodeString(buf, &(pTbCfg->name));
  buf = taosDecodeFixedU32(buf, &(pTbCfg->ttl));
  buf = taosDecodeFixedU32(buf, &(pTbCfg->keep));

  if (pTbCfg->type == META_SUPER_TABLE) {
    buf = tdDecodeSchema(buf, &(pTbCfg->stbCfg.pTagSchema));
  } else if (pTbCfg->type == META_CHILD_TABLE) {
    buf = taosDecodeFixedU64(buf, &(pTbCfg->ctbCfg.suid));
    buf = tdDecodeKVRow(buf, &(pTbCfg->ctbCfg.pTag));
  } else if (pTbCfg->type == META_NORMAL_TABLE) {
  } else {
    ASSERT(0);
  }
  return buf;
H
Hongze Cheng 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
}

static int metaSaveTbInfo(DB *pDB, tb_uid_t uid, STbCfg *pTbCfg) {
  DBT   key = {0};
  DBT   value = {0};
  char  buf[512];
  void *pBuf = buf;

  key.data = &uid;
  key.size = sizeof(uid);

  metaEncodeTbInfo(&pBuf, pTbCfg);

  value.data = buf;
  value.size = POINTER_DISTANCE(pBuf, buf);

  pDB->put(pDB, NULL, &key, &value, 0);

  return 0;
H
more  
Hongze Cheng 已提交
378 379
}
#endif