metaBDBImpl.c 9.7 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

H
Hongze Cheng 已提交
23 24 25 26 27
typedef struct {
  tb_uid_t uid;
  int32_t  sver;
} SSchemaKey;

H
Hongze Cheng 已提交
28
struct SMetaDB {
H
more  
Hongze Cheng 已提交
29
  // DB
H
more  
Hongze Cheng 已提交
30 31
  DB *pTbDB;
  DB *pSchemaDB;
H
more  
Hongze Cheng 已提交
32
  // IDX
H
more  
Hongze Cheng 已提交
33 34 35 36
  DB *pNameIdx;
  DB *pStbIdx;
  DB *pNtbIdx;
  DB *pCtbIdx;
H
more  
Hongze Cheng 已提交
37
  // ENV
H
Hongze Cheng 已提交
38 39 40
  DB_ENV *pEvn;
};

H
Hongze Cheng 已提交
41 42
typedef int (*bdbIdxCbPtr)(DB *, const DBT *, const DBT *, DBT *);

H
more  
Hongze Cheng 已提交
43 44
static SMetaDB *metaNewDB();
static void     metaFreeDB(SMetaDB *pDB);
H
more  
Hongze Cheng 已提交
45 46
static int      metaOpenBDBEnv(DB_ENV **ppEnv, const char *path);
static void     metaCloseBDBEnv(DB_ENV *pEnv);
H
Hongze Cheng 已提交
47
static int      metaOpenBDBDb(DB **ppDB, DB_ENV *pEnv, const char *pFName, bool isDup);
H
more  
Hongze Cheng 已提交
48
static void     metaCloseBDBDb(DB *pDB);
H
Hongze Cheng 已提交
49
static int      metaOpenBDBIdx(DB **ppIdx, DB_ENV *pEnv, const char *pFName, DB *pDB, bdbIdxCbPtr cbf, bool isDup);
H
Hongze Cheng 已提交
50
static void     metaCloseBDBIdx(DB *pIdx);
H
Hongze Cheng 已提交
51 52 53 54
static int      metaNameIdxCb(DB *pIdx, const DBT *pKey, const DBT *pValue, DBT *pSKey);
static int      metaStbIdxCb(DB *pIdx, const DBT *pKey, const DBT *pValue, DBT *pSKey);
static int      metaNtbIdxCb(DB *pIdx, const DBT *pKey, const DBT *pValue, DBT *pSKey);
static int      metaCtbIdxCb(DB *pIdx, const DBT *pKey, const DBT *pValue, DBT *pSKey);
H
Hongze Cheng 已提交
55 56 57
static int      metaEncodeTbInfo(void **buf, STbCfg *pTbCfg);
static void *   metaDecodeTbInfo(void *buf, STbCfg *pTbCfg);
static void     metaClearTbCfg(STbCfg *pTbCfg);
H
more  
Hongze Cheng 已提交
58

H
more  
Hongze Cheng 已提交
59
#define BDB_PERR(info, code) fprintf(stderr, info " reason: %s", db_strerror(code))
H
Hongze Cheng 已提交
60

H
Hongze Cheng 已提交
61
int metaOpenDB(SMeta *pMeta) {
H
more  
Hongze Cheng 已提交
62
  SMetaDB *pDB;
H
Hongze Cheng 已提交
63

H
more  
Hongze Cheng 已提交
64 65 66
  // Create DB object
  pDB = metaNewDB();
  if (pDB == NULL) {
H
more  
Hongze Cheng 已提交
67 68 69
    return -1;
  }

H
more  
Hongze Cheng 已提交
70
  pMeta->pDB = pDB;
H
more  
Hongze Cheng 已提交
71

H
more  
Hongze Cheng 已提交
72 73
  // Open DB Env
  if (metaOpenBDBEnv(&(pDB->pEvn), pMeta->path) < 0) {
H
more  
Hongze Cheng 已提交
74
    metaCloseDB(pMeta);
H
more  
Hongze Cheng 已提交
75 76
    return -1;
  }
H
Hongze Cheng 已提交
77

H
more  
Hongze Cheng 已提交
78
  // Open DBs
H
Hongze Cheng 已提交
79
  if (metaOpenBDBDb(&(pDB->pTbDB), pDB->pEvn, "meta.db", false) < 0) {
H
more  
Hongze Cheng 已提交
80 81
    metaCloseDB(pMeta);
    return -1;
H
more  
Hongze Cheng 已提交
82 83
  }

H
Hongze Cheng 已提交
84
  if (metaOpenBDBDb(&(pDB->pSchemaDB), pDB->pEvn, "meta.db", false) < 0) {
H
more  
Hongze Cheng 已提交
85 86 87
    metaCloseDB(pMeta);
    return -1;
  }
H
Hongze Cheng 已提交
88

H
more  
Hongze Cheng 已提交
89
  // Open Indices
H
Hongze Cheng 已提交
90
  if (metaOpenBDBIdx(&(pDB->pNameIdx), pDB->pEvn, "name.index", pDB->pTbDB, &metaNameIdxCb, false) < 0) {
H
more  
Hongze Cheng 已提交
91 92 93
    metaCloseDB(pMeta);
    return -1;
  }
H
Hongze Cheng 已提交
94

H
Hongze Cheng 已提交
95
  if (metaOpenBDBIdx(&(pDB->pStbIdx), pDB->pEvn, "stb.index", pDB->pTbDB, &metaStbIdxCb, false) < 0) {
H
more  
Hongze Cheng 已提交
96 97 98
    metaCloseDB(pMeta);
    return -1;
  }
H
Hongze Cheng 已提交
99

H
Hongze Cheng 已提交
100
  if (metaOpenBDBIdx(&(pDB->pNtbIdx), pDB->pEvn, "ntb.index", pDB->pTbDB, &metaNtbIdxCb, false) < 0) {
H
more  
Hongze Cheng 已提交
101 102
    metaCloseDB(pMeta);
    return -1;
H
Hongze Cheng 已提交
103 104
  }

H
Hongze Cheng 已提交
105
  if (metaOpenBDBIdx(&(pDB->pCtbIdx), pDB->pEvn, "ctb.index", pDB->pTbDB, &metaCtbIdxCb, true) < 0) {
H
more  
Hongze Cheng 已提交
106 107
    metaCloseDB(pMeta);
    return -1;
H
more  
Hongze Cheng 已提交
108 109
  }

H
Hongze Cheng 已提交
110 111 112 113 114
  return 0;
}

void metaCloseDB(SMeta *pMeta) {
  if (pMeta->pDB) {
H
Hongze Cheng 已提交
115 116 117 118 119 120
    metaCloseBDBIdx(pMeta->pDB->pCtbIdx);
    metaCloseBDBIdx(pMeta->pDB->pNtbIdx);
    metaCloseBDBIdx(pMeta->pDB->pStbIdx);
    metaCloseBDBIdx(pMeta->pDB->pNameIdx);
    metaCloseBDBDb(pMeta->pDB->pSchemaDB);
    metaCloseBDBDb(pMeta->pDB->pTbDB);
H
more  
Hongze Cheng 已提交
121
    metaCloseBDBEnv(pMeta->pDB->pEvn);
H
more  
Hongze Cheng 已提交
122 123
    metaFreeDB(pMeta->pDB);
    pMeta->pDB = NULL;
H
Hongze Cheng 已提交
124 125 126
  }
}

H
more  
Hongze Cheng 已提交
127
int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) {
H
Hongze Cheng 已提交
128
  tb_uid_t  uid;
H
Hongze Cheng 已提交
129 130 131
  char      buf[512];
  void *    pBuf;
  DBT       key, value;
H
Hongze Cheng 已提交
132 133 134 135 136 137 138 139 140 141
  STSchema *pSchema = NULL;

  if (pTbCfg->type == META_SUPER_TABLE) {
    uid = pTbCfg->stbCfg.suid;
  } else {
    uid = metaGenerateUid(pMeta);
  }

  {
    // save table info
H
Hongze Cheng 已提交
142 143 144
    pBuf = buf;
    memset(&key, 0, sizeof(key));
    memset(&value, 0, sizeof(key));
H
Hongze Cheng 已提交
145 146 147 148

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

H
Hongze Cheng 已提交
149
    metaEncodeTbInfo(&pBuf, pTbCfg);
H
Hongze Cheng 已提交
150 151 152

    value.data = buf;
    value.size = POINTER_DISTANCE(pBuf, buf);
H
Hongze Cheng 已提交
153
    value.app_data = pTbCfg;
H
Hongze Cheng 已提交
154 155 156 157 158 159 160 161 162 163 164 165

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

  // save schema
  if (pTbCfg->type == META_SUPER_TABLE) {
    pSchema = pTbCfg->stbCfg.pSchema;
  } else if (pTbCfg->type == META_NORMAL_TABLE) {
    pSchema = pTbCfg->ntbCfg.pSchema;
  }

  if (pSchema) {
H
Hongze Cheng 已提交
166 167 168 169
    pBuf = buf;
    memset(&key, 0, sizeof(key));
    memset(&value, 0, sizeof(key));
    SSchemaKey schemaKey = {uid, schemaVersion(pSchema)};
H
Hongze Cheng 已提交
170

H
Hongze Cheng 已提交
171 172
    key.data = &schemaKey;
    key.size = sizeof(schemaKey);
H
Hongze Cheng 已提交
173 174 175 176 177 178 179 180 181

    tdEncodeSchema(&pBuf, pSchema);

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

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

H
more  
Hongze Cheng 已提交
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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
  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);
  }
}

H
Hongze Cheng 已提交
236
static int metaOpenBDBDb(DB **ppDB, DB_ENV *pEnv, const char *pFName, bool isDup) {
H
more  
Hongze Cheng 已提交
237 238 239
  int ret;
  DB *pDB;

H
Hongze Cheng 已提交
240
  ret = db_create(&(pDB), pEnv, 0);
H
more  
Hongze Cheng 已提交
241 242 243 244 245
  if (ret != 0) {
    BDB_PERR("Failed to create META DB", ret);
    return -1;
  }

H
Hongze Cheng 已提交
246 247 248 249 250 251 252 253
  if (isDup) {
    ret = pDB->set_flags(pDB, DB_DUPSORT);
    if (ret != 0) {
      BDB_PERR("Failed to set DB flags", ret);
      return -1;
    }
  }

H
more  
Hongze Cheng 已提交
254 255 256 257 258 259
  ret = pDB->open(pDB, NULL, pFName, NULL, DB_BTREE, DB_CREATE, 0);
  if (ret) {
    BDB_PERR("Failed to open META DB", ret);
    return -1;
  }

H
Hongze Cheng 已提交
260 261
  *ppDB = pDB;

H
more  
Hongze Cheng 已提交
262 263 264 265 266 267 268 269 270
  return 0;
}

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

H
Hongze Cheng 已提交
271
static int metaOpenBDBIdx(DB **ppIdx, DB_ENV *pEnv, const char *pFName, DB *pDB, bdbIdxCbPtr cbf, bool isDup) {
H
Hongze Cheng 已提交
272 273 274
  DB *pIdx;
  int ret;

H
Hongze Cheng 已提交
275
  if (metaOpenBDBDb(ppIdx, pEnv, pFName, isDup) < 0) {
H
Hongze Cheng 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    return -1;
  }

  pIdx = *ppIdx;
  ret = pDB->associate(pDB, NULL, pIdx, cbf, 0);
  if (ret) {
    BDB_PERR("Failed to associate META DB and Index", ret);
  }

  return 0;
}

static void metaCloseBDBIdx(DB *pIdx) {
  if (pIdx) {
    pIdx->close(pIdx, 0);
  }
}

static int metaNameIdxCb(DB *pIdx, const DBT *pKey, const DBT *pValue, DBT *pSKey) {
H
Hongze Cheng 已提交
295
  STbCfg *pTbCfg = (STbCfg *)(pValue->app_data);
H
Hongze Cheng 已提交
296

H
Hongze Cheng 已提交
297
  memset(pSKey, 0, sizeof(*pSKey));
H
Hongze Cheng 已提交
298

H
Hongze Cheng 已提交
299 300
  pSKey->data = pTbCfg->name;
  pSKey->size = strlen(pTbCfg->name);
H
Hongze Cheng 已提交
301 302 303 304

  return 0;
}

H
Hongze Cheng 已提交
305
static int metaStbIdxCb(DB *pIdx, const DBT *pKey, const DBT *pValue, DBT *pSKey) {
H
Hongze Cheng 已提交
306
  STbCfg *pTbCfg = (STbCfg *)(pValue->app_data);
H
Hongze Cheng 已提交
307

H
Hongze Cheng 已提交
308
  if (pTbCfg->type == META_SUPER_TABLE) {
H
Hongze Cheng 已提交
309 310 311
    memset(pSKey, 0, sizeof(*pSKey));
    pSKey->data = pKey->data;
    pSKey->size = pKey->size;
H
Hongze Cheng 已提交
312

H
Hongze Cheng 已提交
313
    return 0;
H
Hongze Cheng 已提交
314
  } else {
H
Hongze Cheng 已提交
315
    return DB_DONOTINDEX;
H
Hongze Cheng 已提交
316
  }
H
Hongze Cheng 已提交
317 318
}

H
Hongze Cheng 已提交
319
static int metaNtbIdxCb(DB *pIdx, const DBT *pKey, const DBT *pValue, DBT *pSKey) {
H
Hongze Cheng 已提交
320
  STbCfg *pTbCfg = (STbCfg *)(pValue->app_data);
H
Hongze Cheng 已提交
321

H
Hongze Cheng 已提交
322
  if (pTbCfg->type == META_NORMAL_TABLE) {
H
Hongze Cheng 已提交
323 324 325
    memset(pSKey, 0, sizeof(*pSKey));
    pSKey->data = pKey->data;
    pSKey->size = pKey->size;
H
Hongze Cheng 已提交
326

H
Hongze Cheng 已提交
327 328 329 330
    return 0;
  } else {
    return DB_DONOTINDEX;
  }
H
Hongze Cheng 已提交
331 332
}

H
Hongze Cheng 已提交
333
static int metaCtbIdxCb(DB *pIdx, const DBT *pKey, const DBT *pValue, DBT *pSKey) {
H
Hongze Cheng 已提交
334
  STbCfg *pTbCfg = (STbCfg *)(pValue->app_data);
H
Hongze Cheng 已提交
335
  DBT *   pDbt;
H
Hongze Cheng 已提交
336

H
Hongze Cheng 已提交
337
  if (pTbCfg->type == META_CHILD_TABLE) {
H
Hongze Cheng 已提交
338 339 340 341 342 343 344 345 346 347
    pDbt = calloc(2, sizeof(DBT));

    // First key is suid
    pDbt[0].data = &(pTbCfg->ctbCfg.suid);
    pDbt[0].size = sizeof(pTbCfg->ctbCfg.suid);

    // Second key is the first tag
    pDbt[1].data = NULL;
    pDbt[1].size = 0;

H
Hongze Cheng 已提交
348
    // Set index key
H
Hongze Cheng 已提交
349
    memset(pSKey, 0, sizeof(*pSKey));
H
Hongze Cheng 已提交
350 351 352
    pSKey->flags = DB_DBT_MULTIPLE | DB_DBT_APPMALLOC;
    pSKey->data = pDbt;
    pSKey->size = 2;
H
Hongze Cheng 已提交
353

H
Hongze Cheng 已提交
354 355 356 357
    return 0;
  } else {
    return DB_DONOTINDEX;
  }
H
Hongze Cheng 已提交
358 359 360
}

static int metaEncodeTbInfo(void **buf, STbCfg *pTbCfg) {
H
more  
Hongze Cheng 已提交
361 362 363 364 365
  int tsize = 0;

  tsize += taosEncodeString(buf, pTbCfg->name);
  tsize += taosEncodeFixedU32(buf, pTbCfg->ttl);
  tsize += taosEncodeFixedU32(buf, pTbCfg->keep);
H
Hongze Cheng 已提交
366
  tsize += taosEncodeFixedU8(buf, pTbCfg->type);
H
more  
Hongze Cheng 已提交
367 368 369 370 371 372 373 374 375 376 377 378

  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 已提交
379 380 381
}

static void *metaDecodeTbInfo(void *buf, STbCfg *pTbCfg) {
H
more  
Hongze Cheng 已提交
382 383 384
  buf = taosDecodeString(buf, &(pTbCfg->name));
  buf = taosDecodeFixedU32(buf, &(pTbCfg->ttl));
  buf = taosDecodeFixedU32(buf, &(pTbCfg->keep));
H
Hongze Cheng 已提交
385
  buf = taosDecodeFixedU8(buf, &(pTbCfg->type));
H
more  
Hongze Cheng 已提交
386 387 388 389 390 391 392 393 394 395 396

  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 已提交
397 398
}

H
Hongze Cheng 已提交
399 400 401 402 403 404 405 406
static void metaClearTbCfg(STbCfg *pTbCfg) {
  tfree(pTbCfg->name);
  if (pTbCfg->type == META_SUPER_TABLE) {
    tdFreeSchema(pTbCfg->stbCfg.pTagSchema);
  } else if (pTbCfg->type == META_CHILD_TABLE) {
    tfree(pTbCfg->ctbCfg.pTag);
  }
}