metaTDBImpl.c 26.1 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * 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
Hongze Cheng 已提交
14 15
 */

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

H
Hongze Cheng 已提交
18 19 20 21 22 23
typedef struct SPoolMem {
  int64_t          size;
  struct SPoolMem *prev;
  struct SPoolMem *next;
} SPoolMem;

C
Cary Xu 已提交
24 25
#define META_TDB_SMA_TEST

H
Hongze Cheng 已提交
26 27 28
static SPoolMem *openPool();
static void      clearPool(SPoolMem *pPool);
static void      closePool(SPoolMem *pPool);
H
Hongze Cheng 已提交
29
static void     *poolMalloc(void *arg, size_t size);
H
Hongze Cheng 已提交
30
static void      poolFree(void *arg, void *ptr);
H
Hongze Cheng 已提交
31 32

struct SMetaDB {
H
Hongze Cheng 已提交
33
  TXN       txn;
H
Hongze Cheng 已提交
34 35 36 37 38 39 40
  TENV     *pEnv;
  TDB      *pTbDB;
  TDB      *pSchemaDB;
  TDB      *pNameIdx;
  TDB      *pStbIdx;
  TDB      *pNtbIdx;
  TDB      *pCtbIdx;
H
Hongze Cheng 已提交
41
  SPoolMem *pPool;
C
Cary Xu 已提交
42 43 44 45
#ifdef META_TDB_SMA_TEST
  TDB *pSmaDB;
  TDB *pSmaIdx;
#endif
H
Hongze Cheng 已提交
46 47
};

wafwerar's avatar
wafwerar 已提交
48 49
#pragma pack(push,1)
typedef struct {
H
Hongze Cheng 已提交
50 51 52
  tb_uid_t uid;
  int32_t  sver;
} SSchemaDbKey;
wafwerar's avatar
wafwerar 已提交
53
#pragma pack(pop)
H
Hongze Cheng 已提交
54 55

typedef struct {
H
Hongze Cheng 已提交
56
  char    *name;
H
Hongze Cheng 已提交
57 58 59 60 61 62 63 64
  tb_uid_t uid;
} SNameIdxKey;

typedef struct {
  tb_uid_t suid;
  tb_uid_t uid;
} SCtbIdxKey;

C
Cary Xu 已提交
65 66 67 68 69
typedef struct {
  tb_uid_t uid;
  int64_t  smaUid;
} SSmaIdxKey;

H
Hongze Cheng 已提交
70 71
static int   metaEncodeTbInfo(void **buf, STbCfg *pTbCfg);
static void *metaDecodeTbInfo(void *buf, STbCfg *pTbCfg);
H
Hongze Cheng 已提交
72 73
static int   metaEncodeSchema(void **buf, SSchemaWrapper *pSW);
static void *metaDecodeSchema(void *buf, SSchemaWrapper *pSW);
C
Cary Xu 已提交
74 75 76 77
static int   metaEncodeSchemaEx(void **buf, SSchemaWrapper *pSW);
static void *metaDecodeSchemaEx(void *buf, SSchemaWrapper *pSW, bool isGetEx);

static SSchemaWrapper *metaGetTableSchemaImpl(SMeta *pMeta, tb_uid_t uid, int32_t sver, bool isinline, bool isGetEx);
H
Hongze Cheng 已提交
78

H
Hongze Cheng 已提交
79 80 81 82 83 84 85
static inline int metaUidCmpr(const void *arg1, int len1, const void *arg2, int len2) {
  tb_uid_t uid1, uid2;

  ASSERT(len1 == sizeof(tb_uid_t));
  ASSERT(len2 == sizeof(tb_uid_t));

  uid1 = ((tb_uid_t *)arg1)[0];
H
Hongze Cheng 已提交
86
  uid2 = ((tb_uid_t *)arg2)[0];
H
Hongze Cheng 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

  if (uid1 < uid2) {
    return -1;
  }
  if (uid1 == uid2) {
    return 0;
  } else {
    return 1;
  }
}

static inline int metaSchemaKeyCmpr(const void *arg1, int len1, const void *arg2, int len2) {
  int           c;
  SSchemaDbKey *pKey1 = (SSchemaDbKey *)arg1;
  SSchemaDbKey *pKey2 = (SSchemaDbKey *)arg2;

  c = metaUidCmpr(arg1, sizeof(tb_uid_t), arg2, sizeof(tb_uid_t));
  if (c) return c;

  if (pKey1->sver > pKey2->sver) {
    return 1;
  } else if (pKey1->sver == pKey2->sver) {
    return 0;
  } else {
    return -1;
  }
}

static inline int metaNameIdxCmpr(const void *arg1, int len1, const void *arg2, int len2) {
  return strcmp((char *)arg1, (char *)arg2);
}

static inline int metaCtbIdxCmpr(const void *arg1, int len1, const void *arg2, int len2) {
  int         c;
  SCtbIdxKey *pKey1 = (SCtbIdxKey *)arg1;
  SCtbIdxKey *pKey2 = (SCtbIdxKey *)arg2;

  c = metaUidCmpr(arg1, sizeof(tb_uid_t), arg2, sizeof(tb_uid_t));
  if (c) return c;

  return metaUidCmpr(&pKey1->uid, sizeof(tb_uid_t), &pKey2->uid, sizeof(tb_uid_t));
}

C
Cary Xu 已提交
130 131 132 133 134 135 136 137 138 139 140
static inline int metaSmaIdxCmpr(const void *arg1, int len1, const void *arg2, int len2) {
  int         c;
  SSmaIdxKey *pKey1 = (SSmaIdxKey *)arg1;
  SSmaIdxKey *pKey2 = (SSmaIdxKey *)arg2;

  c = metaUidCmpr(arg1, sizeof(tb_uid_t), arg2, sizeof(tb_uid_t));
  if (c) return c;

  return metaUidCmpr(&pKey1->smaUid, sizeof(int64_t), &pKey2->smaUid, sizeof(int64_t));
}

H
Hongze Cheng 已提交
141
int metaOpenDB(SMeta *pMeta) {
H
Hongze Cheng 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  SMetaDB *pMetaDb;
  int      ret;

  // allocate DB handle
  pMetaDb = taosMemoryCalloc(1, sizeof(*pMetaDb));
  if (pMetaDb == NULL) {
    // TODO
    ASSERT(0);
    return -1;
  }

  // open the ENV
  ret = tdbEnvOpen(pMeta->path, 4096, 256, &(pMetaDb->pEnv));
  if (ret < 0) {
    // TODO
    ASSERT(0);
    return -1;
  }

  // open table DB
H
Hongze Cheng 已提交
162
  ret = tdbDbOpen("table.db", sizeof(tb_uid_t), TDB_VARIANT_LEN, metaUidCmpr, pMetaDb->pEnv, &(pMetaDb->pTbDB));
H
Hongze Cheng 已提交
163 164 165 166 167 168
  if (ret < 0) {
    // TODO
    ASSERT(0);
    return -1;
  }

C
Cary Xu 已提交
169 170 171 172 173 174 175 176 177
#ifdef META_TDB_SMA_TEST
  ret = tdbDbOpen("sma.db", sizeof(int64_t), TDB_VARIANT_LEN, metaUidCmpr, pMetaDb->pEnv, &(pMetaDb->pSmaDB));
  if (ret < 0) {
    // TODO
    ASSERT(0);
    return -1;
  }
#endif

H
Hongze Cheng 已提交
178
  // open schema DB
H
Hongze Cheng 已提交
179
  ret = tdbDbOpen("schema.db", sizeof(SSchemaDbKey), TDB_VARIANT_LEN, metaSchemaKeyCmpr, pMetaDb->pEnv,
H
Hongze Cheng 已提交
180 181 182 183 184 185 186
                  &(pMetaDb->pSchemaDB));
  if (ret < 0) {
    // TODO
    ASSERT(0);
    return -1;
  }

H
Hongze Cheng 已提交
187
  ret = tdbDbOpen("name.idx", TDB_VARIANT_LEN, 0, metaNameIdxCmpr, pMetaDb->pEnv, &(pMetaDb->pNameIdx));
H
Hongze Cheng 已提交
188 189 190 191 192 193
  if (ret < 0) {
    // TODO
    ASSERT(0);
    return -1;
  }

H
Hongze Cheng 已提交
194
  ret = tdbDbOpen("stb.idx", sizeof(tb_uid_t), 0, metaUidCmpr, pMetaDb->pEnv, &(pMetaDb->pStbIdx));
H
Hongze Cheng 已提交
195 196 197 198 199 200
  if (ret < 0) {
    // TODO
    ASSERT(0);
    return -1;
  }

H
Hongze Cheng 已提交
201
  ret = tdbDbOpen("ntb.idx", sizeof(tb_uid_t), 0, metaUidCmpr, pMetaDb->pEnv, &(pMetaDb->pNtbIdx));
H
Hongze Cheng 已提交
202 203 204 205 206 207
  if (ret < 0) {
    // TODO
    ASSERT(0);
    return -1;
  }

H
Hongze Cheng 已提交
208
  ret = tdbDbOpen("ctb.idx", sizeof(SCtbIdxKey), 0, metaCtbIdxCmpr, pMetaDb->pEnv, &(pMetaDb->pCtbIdx));
H
Hongze Cheng 已提交
209 210 211 212 213 214
  if (ret < 0) {
    // TODO
    ASSERT(0);
    return -1;
  }

C
Cary Xu 已提交
215 216 217 218 219 220 221 222 223
#ifdef META_TDB_SMA_TEST
  ret = tdbDbOpen("sma.idx", sizeof(SSmaIdxKey), 0, metaSmaIdxCmpr, pMetaDb->pEnv, &(pMetaDb->pSmaIdx));
  if (ret < 0) {
    // TODO
    ASSERT(0);
    return -1;
  }
#endif

H
Hongze Cheng 已提交
224 225
  pMetaDb->pPool = openPool();
  tdbTxnOpen(&pMetaDb->txn, 0, poolMalloc, poolFree, pMetaDb->pPool, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED);
H
Hongze Cheng 已提交
226 227
  tdbBegin(pMetaDb->pEnv, NULL);

H
Hongze Cheng 已提交
228
  pMeta->pDB = pMetaDb;
H
Hongze Cheng 已提交
229 230 231 232
  return 0;
}

void metaCloseDB(SMeta *pMeta) {
H
Hongze Cheng 已提交
233
  if (pMeta->pDB) {
H
Hongze Cheng 已提交
234 235 236
    tdbCommit(pMeta->pDB->pEnv, &pMeta->pDB->txn);
    tdbTxnClose(&pMeta->pDB->txn);
    clearPool(pMeta->pDB->pPool);
C
Cary Xu 已提交
237 238 239
#ifdef META_TDB_SMA_TEST
    tdbDbClose(pMeta->pDB->pSmaIdx);
#endif
H
Hongze Cheng 已提交
240 241 242 243
    tdbDbClose(pMeta->pDB->pCtbIdx);
    tdbDbClose(pMeta->pDB->pNtbIdx);
    tdbDbClose(pMeta->pDB->pStbIdx);
    tdbDbClose(pMeta->pDB->pNameIdx);
C
Cary Xu 已提交
244 245 246
#ifdef META_TDB_SMA_TEST
    tdbDbClose(pMeta->pDB->pSmaDB);
#endif
H
Hongze Cheng 已提交
247 248 249 250
    tdbDbClose(pMeta->pDB->pSchemaDB);
    tdbDbClose(pMeta->pDB->pTbDB);
    taosMemoryFree(pMeta->pDB);
  }
H
Hongze Cheng 已提交
251 252 253
}

int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) {
H
Hongze Cheng 已提交
254
  tb_uid_t       uid;
H
Hongze Cheng 已提交
255 256 257
  SMetaDB       *pMetaDb;
  void          *pKey;
  void          *pVal;
H
Hongze Cheng 已提交
258 259 260 261
  int            kLen;
  int            vLen;
  int            ret;
  char           buf[512];
H
Hongze Cheng 已提交
262
  void          *pBuf;
H
Hongze Cheng 已提交
263 264 265
  SCtbIdxKey     ctbIdxKey;
  SSchemaDbKey   schemaDbKey;
  SSchemaWrapper schemaWrapper;
H
Hongze Cheng 已提交
266 267 268 269 270 271 272 273 274 275

  pMetaDb = pMeta->pDB;

  // TODO: make this operation pre-process
  if (pTbCfg->type == META_SUPER_TABLE) {
    uid = pTbCfg->stbCfg.suid;
  } else {
    uid = metaGenerateUid(pMeta);
  }

H
Hongze Cheng 已提交
276 277 278 279 280 281 282 283
  // check name and uid unique
  if (tdbDbGet(pMetaDb->pTbDB, &uid, sizeof(uid), NULL, NULL) == 0) {
    return -1;
  }
  if (tdbDbGet(pMetaDb->pNameIdx, pTbCfg->name, strlen(pTbCfg->name) + 1, NULL, NULL) == 0) {
    return -1;
  }

H
Hongze Cheng 已提交
284
  // save to table.db
H
Hongze Cheng 已提交
285 286 287 288 289
  pKey = &uid;
  kLen = sizeof(uid);
  pVal = pBuf = buf;
  metaEncodeTbInfo(&pBuf, pTbCfg);
  vLen = POINTER_DISTANCE(pBuf, buf);
H
Hongze Cheng 已提交
290
  ret = tdbDbInsert(pMetaDb->pTbDB, pKey, kLen, pVal, vLen, &pMetaDb->txn);
H
Hongze Cheng 已提交
291 292 293 294
  if (ret < 0) {
    return -1;
  }

H
Hongze Cheng 已提交
295 296 297 298 299 300 301 302 303
  // save to schema.db for META_SUPER_TABLE and META_NORMAL_TABLE
  if (pTbCfg->type != META_CHILD_TABLE) {
    schemaDbKey.uid = uid;
    schemaDbKey.sver = 0;  // TODO
    pKey = &schemaDbKey;
    kLen = sizeof(schemaDbKey);

    if (pTbCfg->type == META_SUPER_TABLE) {
      schemaWrapper.nCols = pTbCfg->stbCfg.nCols;
H
Hongze Cheng 已提交
304
      schemaWrapper.pSchema = pTbCfg->stbCfg.pSchema;
H
Hongze Cheng 已提交
305 306
    } else {
      schemaWrapper.nCols = pTbCfg->ntbCfg.nCols;
H
Hongze Cheng 已提交
307
      schemaWrapper.pSchema = pTbCfg->ntbCfg.pSchema;
H
Hongze Cheng 已提交
308 309
    }
    pVal = pBuf = buf;
C
Cary Xu 已提交
310
    metaEncodeSchemaEx(&pBuf, &schemaWrapper);
H
Hongze Cheng 已提交
311
    vLen = POINTER_DISTANCE(pBuf, buf);
H
Hongze Cheng 已提交
312
    ret = tdbDbInsert(pMetaDb->pSchemaDB, pKey, kLen, pVal, vLen, &pMeta->pDB->txn);
H
Hongze Cheng 已提交
313 314 315
    if (ret < 0) {
      return -1;
    }
H
Hongze Cheng 已提交
316 317 318
  }

  // update name.idx
H
Hongze Cheng 已提交
319
  int nameLen = strlen(pTbCfg->name);
H
Hongze Cheng 已提交
320 321 322 323 324 325
  memcpy(buf, pTbCfg->name, nameLen + 1);
  ((tb_uid_t *)(buf + nameLen + 1))[0] = uid;
  pKey = buf;
  kLen = nameLen + 1 + sizeof(uid);
  pVal = NULL;
  vLen = 0;
H
Hongze Cheng 已提交
326
  ret = tdbDbInsert(pMetaDb->pNameIdx, pKey, kLen, pVal, vLen, &pMetaDb->txn);
H
Hongze Cheng 已提交
327 328 329 330
  if (ret < 0) {
    return -1;
  }

H
Hongze Cheng 已提交
331
  // update other index
H
Hongze Cheng 已提交
332
  if (pTbCfg->type == META_SUPER_TABLE) {
H
Hongze Cheng 已提交
333 334 335 336
    pKey = &uid;
    kLen = sizeof(uid);
    pVal = NULL;
    vLen = 0;
H
Hongze Cheng 已提交
337
    ret = tdbDbInsert(pMetaDb->pStbIdx, pKey, kLen, pVal, vLen, &pMetaDb->txn);
H
Hongze Cheng 已提交
338 339 340 341
    if (ret < 0) {
      return -1;
    }
  } else if (pTbCfg->type == META_CHILD_TABLE) {
H
Hongze Cheng 已提交
342 343 344 345 346 347
    ctbIdxKey.suid = pTbCfg->ctbCfg.suid;
    ctbIdxKey.uid = uid;
    pKey = &ctbIdxKey;
    kLen = sizeof(ctbIdxKey);
    pVal = NULL;
    vLen = 0;
H
Hongze Cheng 已提交
348
    ret = tdbDbInsert(pMetaDb->pCtbIdx, pKey, kLen, pVal, vLen, &pMetaDb->txn);
H
Hongze Cheng 已提交
349 350 351 352
    if (ret < 0) {
      return -1;
    }
  } else if (pTbCfg->type == META_NORMAL_TABLE) {
H
Hongze Cheng 已提交
353 354 355 356
    pKey = &uid;
    kLen = sizeof(uid);
    pVal = NULL;
    vLen = 0;
H
Hongze Cheng 已提交
357
    ret = tdbDbInsert(pMetaDb->pNtbIdx, pKey, kLen, pVal, vLen, &pMetaDb->txn);
H
Hongze Cheng 已提交
358 359 360 361 362
    if (ret < 0) {
      return -1;
    }
  }

H
Hongze Cheng 已提交
363 364 365 366
  if (pMeta->pDB->pPool->size > 0) {
    metaCommit(pMeta);
  }

H
Hongze Cheng 已提交
367 368 369 370 371
  return 0;
}

int metaRemoveTableFromDb(SMeta *pMeta, tb_uid_t uid) {
  // TODO
H
Hongze Cheng 已提交
372
  ASSERT(0);
H
Hongze Cheng 已提交
373 374 375 376
  return 0;
}

STbCfg *metaGetTbInfoByUid(SMeta *pMeta, tb_uid_t uid) {
H
Hongze Cheng 已提交
377 378
  int      ret;
  SMetaDB *pMetaDb = pMeta->pDB;
H
Hongze Cheng 已提交
379 380
  void    *pKey;
  void    *pVal;
H
Hongze Cheng 已提交
381 382
  int      kLen;
  int      vLen;
H
Hongze Cheng 已提交
383
  STbCfg  *pTbCfg;
H
Hongze Cheng 已提交
384 385 386 387

  // Fetch
  pKey = &uid;
  kLen = sizeof(uid);
H
Hongze Cheng 已提交
388
  pVal = NULL;
H
Hongze Cheng 已提交
389 390 391 392 393 394 395 396 397 398 399 400
  ret = tdbDbGet(pMetaDb->pTbDB, pKey, kLen, &pVal, &vLen);
  if (ret < 0) {
    return NULL;
  }

  // Decode
  pTbCfg = taosMemoryMalloc(sizeof(*pTbCfg));
  metaDecodeTbInfo(pVal, pTbCfg);

  TDB_FREE(pVal);

  return pTbCfg;
H
Hongze Cheng 已提交
401 402 403
}

STbCfg *metaGetTbInfoByName(SMeta *pMeta, char *tbname, tb_uid_t *uid) {
H
Hongze Cheng 已提交
404 405
  void *pKey;
  void *pVal;
H
Hongze Cheng 已提交
406 407
  void *ppKey;
  int   pkLen;
H
Hongze Cheng 已提交
408 409 410 411 412 413
  int   kLen;
  int   vLen;
  int   ret;

  pKey = tbname;
  kLen = strlen(tbname) + 1;
H
Hongze Cheng 已提交
414
  pVal = NULL;
H
Hongze Cheng 已提交
415 416
  ppKey = NULL;
  ret = tdbDbPGet(pMeta->pDB->pNameIdx, pKey, kLen, &ppKey, &pkLen, &pVal, &vLen);
H
Hongze Cheng 已提交
417 418 419 420
  if (ret < 0) {
    return NULL;
  }

H
Hongze Cheng 已提交
421 422 423 424
  ASSERT(pkLen == kLen + sizeof(uid));

  *uid = *(tb_uid_t *)POINTER_SHIFT(ppKey, kLen);
  TDB_FREE(ppKey);
H
Hongze Cheng 已提交
425 426 427
  TDB_FREE(pVal);

  return metaGetTbInfoByUid(pMeta, *uid);
H
Hongze Cheng 已提交
428 429 430
}

SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, bool isinline) {
H
Hongze Cheng 已提交
431
  return metaGetTableSchemaImpl(pMeta, uid, sver, isinline, false);
C
Cary Xu 已提交
432 433 434
}

static SSchemaWrapper *metaGetTableSchemaImpl(SMeta *pMeta, tb_uid_t uid, int32_t sver, bool isinline, bool isGetEx) {
H
Hongze Cheng 已提交
435 436
  void           *pKey;
  void           *pVal;
H
Hongze Cheng 已提交
437 438 439 440 441
  int             kLen;
  int             vLen;
  int             ret;
  SSchemaDbKey    schemaDbKey;
  SSchemaWrapper *pSchemaWrapper;
H
Hongze Cheng 已提交
442
  void           *pBuf;
H
Hongze Cheng 已提交
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457

  // fetch
  schemaDbKey.uid = uid;
  schemaDbKey.sver = sver;
  pKey = &schemaDbKey;
  kLen = sizeof(schemaDbKey);
  pVal = NULL;
  ret = tdbDbGet(pMeta->pDB->pSchemaDB, pKey, kLen, &pVal, &vLen);
  if (ret < 0) {
    return NULL;
  }

  // decode
  pBuf = pVal;
  pSchemaWrapper = taosMemoryMalloc(sizeof(*pSchemaWrapper));
C
Cary Xu 已提交
458
  metaDecodeSchemaEx(pBuf, pSchemaWrapper, isGetEx);
H
Hongze Cheng 已提交
459 460 461 462

  TDB_FREE(pVal);

  return pSchemaWrapper;
H
Hongze Cheng 已提交
463 464 465
}

STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver) {
H
Hongze Cheng 已提交
466 467 468
  tb_uid_t        quid;
  SSchemaWrapper *pSW;
  STSchemaBuilder sb;
H
Hongze Cheng 已提交
469
  SSchema        *pSchema;
H
Hongze Cheng 已提交
470 471
  STSchema       *pTSchema;
  STbCfg         *pTbCfg;
H
Hongze Cheng 已提交
472 473 474 475 476 477 478 479

  pTbCfg = metaGetTbInfoByUid(pMeta, uid);
  if (pTbCfg->type == META_CHILD_TABLE) {
    quid = pTbCfg->ctbCfg.suid;
  } else {
    quid = uid;
  }

C
Cary Xu 已提交
480
  pSW = metaGetTableSchemaImpl(pMeta, quid, sver, true, true);
H
Hongze Cheng 已提交
481 482 483 484 485 486
  if (pSW == NULL) {
    return NULL;
  }

  tdInitTSchemaBuilder(&sb, 0);
  for (int i = 0; i < pSW->nCols; i++) {
H
Hongze Cheng 已提交
487 488
    pSchema = pSW->pSchema + i;
    tdAddColToSchema(&sb, pSchema->type, pSchema->flags, pSchema->colId, pSchema->bytes);
H
Hongze Cheng 已提交
489 490 491 492 493
  }
  pTSchema = tdGetSchemaFromBuilder(&sb);
  tdDestroyTSchemaBuilder(&sb);

  return pTSchema;
H
Hongze Cheng 已提交
494 495
}

H
Hongze Cheng 已提交
496 497 498 499
struct SMTbCursor {
  TDBC *pDbc;
};

H
Hongze Cheng 已提交
500
SMTbCursor *metaOpenTbCursor(SMeta *pMeta) {
H
Hongze Cheng 已提交
501
  SMTbCursor *pTbCur = NULL;
H
Hongze Cheng 已提交
502
  SMetaDB    *pDB = pMeta->pDB;
H
Hongze Cheng 已提交
503 504 505 506 507 508 509 510 511

  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
  if (pTbCur == NULL) {
    return NULL;
  }

  tdbDbcOpen(pDB->pTbDB, &pTbCur->pDbc);

  return pTbCur;
H
Hongze Cheng 已提交
512 513 514
}

void metaCloseTbCursor(SMTbCursor *pTbCur) {
H
Hongze Cheng 已提交
515 516 517 518 519 520
  if (pTbCur) {
    if (pTbCur->pDbc) {
      tdbDbcClose(pTbCur->pDbc);
    }
    taosMemoryFree(pTbCur);
  }
H
Hongze Cheng 已提交
521 522 523
}

char *metaTbCursorNext(SMTbCursor *pTbCur) {
H
Hongze Cheng 已提交
524 525
  void  *pKey = NULL;
  void  *pVal = NULL;
H
Hongze Cheng 已提交
526 527 528
  int    kLen;
  int    vLen;
  int    ret;
H
Hongze Cheng 已提交
529
  void  *pBuf;
H
Hongze Cheng 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
  STbCfg tbCfg;

  for (;;) {
    ret = tdbDbNext(pTbCur->pDbc, &pKey, &kLen, &pVal, &vLen);
    if (ret < 0) break;
    pBuf = pVal;
    metaDecodeTbInfo(pBuf, &tbCfg);
    if (tbCfg.type == META_SUPER_TABLE) {
      taosMemoryFree(tbCfg.name);
      taosMemoryFree(tbCfg.stbCfg.pTagSchema);
      continue;
    } else if (tbCfg.type == META_CHILD_TABLE) {
      kvRowFree(tbCfg.ctbCfg.pTag);
    }

    return tbCfg.name;
  }

H
Hongze Cheng 已提交
548 549 550
  return NULL;
}

H
Hongze Cheng 已提交
551
struct SMCtbCursor {
H
Hongze Cheng 已提交
552
  TDBC    *pCur;
H
Hongze Cheng 已提交
553
  tb_uid_t suid;
H
Hongze Cheng 已提交
554 555
  void    *pKey;
  void    *pVal;
H
Hongze Cheng 已提交
556 557
  int      kLen;
  int      vLen;
H
Hongze Cheng 已提交
558 559
};

H
Hongze Cheng 已提交
560
SMCtbCursor *metaOpenCtbCursor(SMeta *pMeta, tb_uid_t uid) {
H
Hongze Cheng 已提交
561
  SMCtbCursor *pCtbCur = NULL;
H
Hongze Cheng 已提交
562
  SMetaDB     *pDB = pMeta->pDB;
H
Hongze Cheng 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
  int          ret;

  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
  if (pCtbCur == NULL) {
    return NULL;
  }

  pCtbCur->suid = uid;
  ret = tdbDbcOpen(pDB->pCtbIdx, &pCtbCur->pCur);
  if (ret < 0) {
    taosMemoryFree(pCtbCur);
    return NULL;
  }

  // TODO: move the cursor to the suid there

  return pCtbCur;
H
Hongze Cheng 已提交
580 581 582
}

void metaCloseCtbCurosr(SMCtbCursor *pCtbCur) {
H
Hongze Cheng 已提交
583 584 585 586 587 588 589 590 591 592
  if (pCtbCur) {
    if (pCtbCur->pCur) {
      tdbDbcClose(pCtbCur->pCur);

      TDB_FREE(pCtbCur->pKey);
      TDB_FREE(pCtbCur->pVal);
    }

    taosMemoryFree(pCtbCur);
  }
H
Hongze Cheng 已提交
593 594 595
}

tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
H
Hongze Cheng 已提交
596 597 598 599 600 601 602 603
  int         ret;
  SCtbIdxKey *pCtbIdxKey;

  ret = tdbDbNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
  if (ret < 0) {
    return 0;
  }

H
Hongze Cheng 已提交
604
  pCtbIdxKey = pCtbCur->pKey;
H
Hongze Cheng 已提交
605 606

  return pCtbIdxKey->uid;
H
Hongze Cheng 已提交
607 608 609 610
}

int metaGetTbNum(SMeta *pMeta) {
  // TODO
H
Hongze Cheng 已提交
611
  // ASSERT(0);
H
Hongze Cheng 已提交
612 613 614
  return 0;
}

C
Cary Xu 已提交
615 616 617 618 619 620 621 622 623
struct SMSmaCursor {
  TDBC    *pCur;
  tb_uid_t uid;
  void    *pKey;
  void    *pVal;
  int      kLen;
  int      vLen;
};

H
Hongze Cheng 已提交
624 625
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid) {
  // TODO
C
Cary Xu 已提交
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
  // ASSERT(0);
  // return NULL;
#ifdef META_TDB_SMA_TEST
  STSmaWrapper *pSW = NULL;

  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
  if (pCur == NULL) {
    return NULL;
  }

  void       *pBuf = NULL;
  SSmaIdxKey *pSmaIdxKey = NULL;

  while (true) {
    // TODO: lock during iterate?
    if (tdbDbNext(pCur->pCur, &pCur->pKey, &pCur->kLen, NULL, &pCur->vLen) == 0) {
      pSmaIdxKey = pCur->pKey;
      ASSERT(pSmaIdxKey != NULL);

      void *pSmaVal = metaGetSmaInfoByIndex(pMeta, pSmaIdxKey->smaUid, false);

      if (pSmaVal == NULL) {
        tsdbWarn("no tsma exists for indexUid: %" PRIi64, pSmaIdxKey->smaUid);
        continue;
      }

C
Cary Xu 已提交
652 653 654 655 656 657
      if ((pSW == NULL) && ((pSW = taosMemoryCalloc(1, sizeof(*pSW))) == NULL)) {
        TDB_FREE(pSmaVal);
        metaCloseSmaCursor(pCur);
        return NULL;
      }

C
Cary Xu 已提交
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
      ++pSW->number;
      STSma *tptr = (STSma *)taosMemoryRealloc(pSW->tSma, pSW->number * sizeof(STSma));
      if (tptr == NULL) {
        TDB_FREE(pSmaVal);
        metaCloseSmaCursor(pCur);
        tdDestroyTSmaWrapper(pSW);
        taosMemoryFreeClear(pSW);
        return NULL;
      }
      pSW->tSma = tptr;
      pBuf = pSmaVal;
      if (tDecodeTSma(pBuf, pSW->tSma + pSW->number - 1) == NULL) {
        TDB_FREE(pSmaVal);
        metaCloseSmaCursor(pCur);
        tdDestroyTSmaWrapper(pSW);
        taosMemoryFreeClear(pSW);
        return NULL;
      }
      TDB_FREE(pSmaVal);
      continue;
    }
    break;
  }

  metaCloseSmaCursor(pCur);

  return pSW;

#endif
H
Hongze Cheng 已提交
687 688 689 690 691
}

int metaRemoveSmaFromDb(SMeta *pMeta, int64_t indexUid) {
  // TODO
  ASSERT(0);
C
Cary Xu 已提交
692 693 694 695 696 697 698 699 700 701 702 703
#ifndef META_TDB_SMA_TEST
  DBT key = {0};

  key.data = (void *)indexName;
  key.size = strlen(indexName);

  metaDBWLock(pMeta->pDB);
  // TODO: No guarantee of consistence.
  // Use transaction or DB->sync() for some guarantee.
  pMeta->pDB->pSmaDB->del(pMeta->pDB->pSmaDB, NULL, &key, 0);
  metaDBULock(pMeta->pDB);
#endif
H
Hongze Cheng 已提交
704
  return 0;
H
Hongze Cheng 已提交
705 706 707 708
}

int metaSaveSmaToDB(SMeta *pMeta, STSma *pSmaCfg) {
  // TODO
C
Cary Xu 已提交
709 710 711
  // ASSERT(0);

#ifdef META_TDB_SMA_TEST
H
Hongze Cheng 已提交
712
  int32_t  ret = 0;
C
Cary Xu 已提交
713
  SMetaDB *pMetaDb = pMeta->pDB;
H
Hongze Cheng 已提交
714 715
  void    *pBuf = NULL, *qBuf = NULL;
  void    *key = {0}, *val = {0};
C
Cary Xu 已提交
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761

  // save sma info
  int32_t len = tEncodeTSma(NULL, pSmaCfg);
  pBuf = taosMemoryCalloc(1, len);
  if (pBuf == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  key = (void *)&pSmaCfg->indexUid;
  qBuf = pBuf;
  tEncodeTSma(&qBuf, pSmaCfg);
  val = pBuf;

  int32_t kLen = sizeof(pSmaCfg->indexUid);
  int32_t vLen = POINTER_DISTANCE(qBuf, pBuf);

  ret = tdbDbInsert(pMeta->pDB->pSmaDB, key, kLen, val, vLen, &pMetaDb->txn);
  if (ret < 0) {
    taosMemoryFreeClear(pBuf);
    return -1;
  }

  // add sma idx
  SSmaIdxKey smaIdxKey;
  smaIdxKey.uid = pSmaCfg->tableUid;
  smaIdxKey.smaUid = pSmaCfg->indexUid;
  key = &smaIdxKey;
  kLen = sizeof(smaIdxKey);
  val = NULL;
  vLen = 0;

  ret = tdbDbInsert(pMeta->pDB->pSmaIdx, key, kLen, val, vLen, &pMetaDb->txn);
  if (ret < 0) {
    taosMemoryFreeClear(pBuf);
    return -1;
  }

  // release
  taosMemoryFreeClear(pBuf);

  if (pMeta->pDB->pPool->size > 0) {
    metaCommit(pMeta);
  }

#endif
H
Hongze Cheng 已提交
762 763 764
  return 0;
}

C
Cary Xu 已提交
765
void *metaGetSmaInfoByIndex(SMeta *pMeta, int64_t indexUid, bool isDecode) {
H
Hongze Cheng 已提交
766
  // TODO
C
Cary Xu 已提交
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
  // ASSERT(0);
  // return NULL;
#ifdef META_TDB_SMA_TEST
  SMetaDB *pDB = pMeta->pDB;
  void    *pKey = NULL;
  void    *pVal = NULL;
  int      kLen = 0;
  int      vLen = 0;
  int      ret = -1;

  // Set key
  pKey = (void *)&indexUid;
  kLen = sizeof(indexUid);

  // Query
  ret = tdbDbGet(pDB->pSmaDB, pKey, kLen, &pVal, &vLen);
  if (ret != 0 || !pVal) {
    return NULL;
  }

  if (!isDecode) {
    // return raw value
    return pVal;
  }

  // Decode
  STSma *pCfg = (STSma *)taosMemoryCalloc(1, sizeof(STSma));
  if (pCfg == NULL) {
    taosMemoryFree(pVal);
    return NULL;
  }

  void *pBuf = pVal;
  if (tDecodeTSma(pBuf, pCfg) == NULL) {
    tdDestroyTSma(pCfg);
    taosMemoryFree(pCfg);
    TDB_FREE(pVal);
    return NULL;
  }

  TDB_FREE(pVal);
  return pCfg;
#endif
H
Hongze Cheng 已提交
810 811
}

C
Cary Xu 已提交
812 813 814 815 816 817 818 819
/**
 * @brief
 *
 * @param pMeta
 * @param uid 0 means iterate all uids.
 * @return SMSmaCursor*
 */
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
H
Hongze Cheng 已提交
820
  // TODO
C
Cary Xu 已提交
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
  // ASSERT(0);
  // return NULL;
#ifdef META_TDB_SMA_TEST
  SMSmaCursor *pCur = NULL;
  SMetaDB     *pDB = pMeta->pDB;
  int          ret;

  pCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pCur));
  if (pCur == NULL) {
    return NULL;
  }

  pCur->uid = uid;
  ret = tdbDbcOpen(pDB->pSmaIdx, &(pCur->pCur));
  if ((ret != 0) || (pCur->pCur == NULL)) {
    taosMemoryFree(pCur);
    return NULL;
  }

  if (uid != 0) {
    // TODO: move to the specific uid
  }

  return pCur;
#endif
H
Hongze Cheng 已提交
846 847
}

C
Cary Xu 已提交
848 849 850 851 852 853 854
/**
 * @brief
 *
 * @param pCur
 * @return int64_t smaIndexUid
 */
int64_t metaSmaCursorNext(SMSmaCursor *pCur) {
H
Hongze Cheng 已提交
855
  // TODO
C
Cary Xu 已提交
856 857 858 859 860 861 862 863 864 865 866 867 868 869
  // ASSERT(0);
  // return NULL;
#ifdef META_TDB_SMA_TEST
  int         ret;
  void       *pBuf;
  SSmaIdxKey *smaIdxKey;

  ret = tdbDbNext(pCur->pCur, &pCur->pKey, &pCur->kLen, &pCur->pVal, &pCur->vLen);
  if (ret < 0) {
    return 0;
  }
  smaIdxKey = pCur->pKey;
  return smaIdxKey->smaUid;
#endif
H
Hongze Cheng 已提交
870 871
}

C
Cary Xu 已提交
872
void metaCloseSmaCursor(SMSmaCursor *pCur) {
H
Hongze Cheng 已提交
873
  // TODO
C
Cary Xu 已提交
874 875 876 877 878 879 880 881 882 883
  // ASSERT(0);
#ifdef META_TDB_SMA_TEST
  if (pCur) {
    if (pCur->pCur) {
      tdbDbcClose(pCur->pCur);
    }

    taosMemoryFree(pCur);
  }
#endif
H
Hongze Cheng 已提交
884 885
}

C
Cary Xu 已提交
886
SArray *metaGetSmaTbUids(SMeta *pMeta, bool isDup) {
H
Hongze Cheng 已提交
887
  // TODO
C
Cary Xu 已提交
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
  // ASSERT(0); // comment this line to pass CI
  // return NULL:
#ifdef META_TDB_SMA_TEST
  SArray  *pUids = NULL;
  SMetaDB *pDB = pMeta->pDB;
  void    *pKey;

  // TODO: lock?
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
  if (pCur == NULL) {
    return NULL;
  }
  // TODO: lock?

  SSmaIdxKey *pSmaIdxKey = NULL;
  tb_uid_t    uid = 0;
  while (true) {
    // TODO: lock during iterate?
    if (tdbDbNext(pCur->pCur, &pCur->pKey, &pCur->kLen, NULL, &pCur->vLen) == 0) {
      ASSERT(pSmaIdxKey != NULL);
      pSmaIdxKey = pCur->pKey;

      if (pSmaIdxKey->uid == 0 || pSmaIdxKey->uid == uid) {
        continue;
      }
      uid = pSmaIdxKey->uid;

      if (!pUids) {
        pUids = taosArrayInit(16, sizeof(tb_uid_t));
        if (!pUids) {
          metaCloseSmaCursor(pCur);
          return NULL;
        }
      }

      taosArrayPush(pUids, &uid);

      continue;
    }
    break;
  }

  metaCloseSmaCursor(pCur);

  return pUids;
#endif
H
Hongze Cheng 已提交
934
}
H
Hongze Cheng 已提交
935 936 937 938 939 940 941 942 943

static int metaEncodeSchema(void **buf, SSchemaWrapper *pSW) {
  int      tlen = 0;
  SSchema *pSchema;

  tlen += taosEncodeFixedU32(buf, pSW->nCols);
  for (int i = 0; i < pSW->nCols; i++) {
    pSchema = pSW->pSchema + i;
    tlen += taosEncodeFixedI8(buf, pSchema->type);
H
Hongze Cheng 已提交
944
    tlen += taosEncodeFixedI8(buf, pSchema->flags);
H
Hongze Cheng 已提交
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
    tlen += taosEncodeFixedI16(buf, pSchema->colId);
    tlen += taosEncodeFixedI32(buf, pSchema->bytes);
    tlen += taosEncodeString(buf, pSchema->name);
  }

  return tlen;
}

static void *metaDecodeSchema(void *buf, SSchemaWrapper *pSW) {
  SSchema *pSchema;

  buf = taosDecodeFixedU32(buf, &pSW->nCols);
  pSW->pSchema = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * pSW->nCols);
  for (int i = 0; i < pSW->nCols; i++) {
    pSchema = pSW->pSchema + i;
    buf = taosDecodeFixedI8(buf, &pSchema->type);
dengyihao's avatar
dengyihao 已提交
961
    buf = taosSkipFixedLen(buf, sizeof(int8_t));
H
Hongze Cheng 已提交
962 963 964 965 966 967 968 969
    buf = taosDecodeFixedI16(buf, &pSchema->colId);
    buf = taosDecodeFixedI32(buf, &pSchema->bytes);
    buf = taosDecodeStringTo(buf, pSchema->name);
  }

  return buf;
}

C
Cary Xu 已提交
970
static int metaEncodeSchemaEx(void **buf, SSchemaWrapper *pSW) {
H
Hongze Cheng 已提交
971 972
  int      tlen = 0;
  SSchema *pSchema;
C
Cary Xu 已提交
973 974 975

  tlen += taosEncodeFixedU32(buf, pSW->nCols);
  for (int i = 0; i < pSW->nCols; ++i) {
H
Hongze Cheng 已提交
976
    pSchema = pSW->pSchema + i;
C
Cary Xu 已提交
977
    tlen += taosEncodeFixedI8(buf, pSchema->type);
H
Hongze Cheng 已提交
978
    tlen += taosEncodeFixedI8(buf, pSchema->flags);
C
Cary Xu 已提交
979 980 981 982 983 984 985 986 987 988 989
    tlen += taosEncodeFixedI16(buf, pSchema->colId);
    tlen += taosEncodeFixedI32(buf, pSchema->bytes);
    tlen += taosEncodeString(buf, pSchema->name);
  }

  return tlen;
}

static void *metaDecodeSchemaEx(void *buf, SSchemaWrapper *pSW, bool isGetEx) {
  buf = taosDecodeFixedU32(buf, &pSW->nCols);
  if (isGetEx) {
H
Hongze Cheng 已提交
990
    pSW->pSchema = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * pSW->nCols);
C
Cary Xu 已提交
991
    for (int i = 0; i < pSW->nCols; i++) {
H
Hongze Cheng 已提交
992
      SSchema *pSchema = pSW->pSchema + i;
C
Cary Xu 已提交
993
      buf = taosDecodeFixedI8(buf, &pSchema->type);
H
Hongze Cheng 已提交
994
      buf = taosDecodeFixedI8(buf, &pSchema->flags);
C
Cary Xu 已提交
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
      buf = taosDecodeFixedI16(buf, &pSchema->colId);
      buf = taosDecodeFixedI32(buf, &pSchema->bytes);
      buf = taosDecodeStringTo(buf, pSchema->name);
    }
  } else {
    pSW->pSchema = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * pSW->nCols);
    for (int i = 0; i < pSW->nCols; i++) {
      SSchema *pSchema = pSW->pSchema + i;
      buf = taosDecodeFixedI8(buf, &pSchema->type);
      buf = taosSkipFixedLen(buf, sizeof(int8_t));
      buf = taosDecodeFixedI16(buf, &pSchema->colId);
      buf = taosDecodeFixedI32(buf, &pSchema->bytes);
      buf = taosDecodeStringTo(buf, pSchema->name);
    }
  }

  return buf;
}

H
Hongze Cheng 已提交
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
static int metaEncodeTbInfo(void **buf, STbCfg *pTbCfg) {
  int tsize = 0;

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

  if (pTbCfg->type == META_SUPER_TABLE) {
    SSchemaWrapper sw = {.nCols = pTbCfg->stbCfg.nTagCols, .pSchema = pTbCfg->stbCfg.pTagSchema};
    tsize += metaEncodeSchema(buf, &sw);
  } 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) {
    // TODO
  } else {
    ASSERT(0);
  }

  return tsize;
}

static void *metaDecodeTbInfo(void *buf, STbCfg *pTbCfg) {
  buf = taosDecodeString(buf, &(pTbCfg->name));
  buf = taosDecodeFixedU32(buf, &(pTbCfg->ttl));
  buf = taosDecodeFixedU32(buf, &(pTbCfg->keep));
  buf = taosDecodeFixedU8(buf, &(pTbCfg->info));

  if (pTbCfg->type == META_SUPER_TABLE) {
    SSchemaWrapper sw;
    buf = metaDecodeSchema(buf, &sw);
    pTbCfg->stbCfg.nTagCols = sw.nCols;
    pTbCfg->stbCfg.pTagSchema = sw.pSchema;
  } 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) {
    // TODO
  } else {
    ASSERT(0);
  }
  return buf;
}
H
Hongze Cheng 已提交
1058 1059

int metaCommit(SMeta *pMeta) {
H
Hongze Cheng 已提交
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
  TXN *pTxn = &pMeta->pDB->txn;

  // Commit current txn
  tdbCommit(pMeta->pDB->pEnv, pTxn);
  tdbTxnClose(pTxn);
  clearPool(pMeta->pDB->pPool);

  // start a new txn
  tdbTxnOpen(&pMeta->pDB->txn, 0, poolMalloc, poolFree, pMeta->pDB->pPool, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED);
  tdbBegin(pMeta->pDB->pEnv, pTxn);
H
Hongze Cheng 已提交
1070
  return 0;
H
Hongze Cheng 已提交
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
}

static SPoolMem *openPool() {
  SPoolMem *pPool = (SPoolMem *)tdbOsMalloc(sizeof(*pPool));

  pPool->prev = pPool->next = pPool;
  pPool->size = 0;

  return pPool;
}

static void clearPool(SPoolMem *pPool) {
  SPoolMem *pMem;

  do {
    pMem = pPool->next;

    if (pMem == pPool) break;

    pMem->next->prev = pMem->prev;
    pMem->prev->next = pMem->next;
    pPool->size -= pMem->size;

    tdbOsFree(pMem);
  } while (1);

  assert(pPool->size == 0);
}

static void closePool(SPoolMem *pPool) {
  clearPool(pPool);
  tdbOsFree(pPool);
}

static void *poolMalloc(void *arg, size_t size) {
H
Hongze Cheng 已提交
1106
  void     *ptr = NULL;
H
Hongze Cheng 已提交
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
  SPoolMem *pPool = (SPoolMem *)arg;
  SPoolMem *pMem;

  pMem = (SPoolMem *)tdbOsMalloc(sizeof(*pMem) + size);
  if (pMem == NULL) {
    assert(0);
  }

  pMem->size = sizeof(*pMem) + size;
  pMem->next = pPool->next;
  pMem->prev = pPool;

  pPool->next->prev = pMem;
  pPool->next = pMem;
  pPool->size += pMem->size;

  ptr = (void *)(&pMem[1]);
  return ptr;
}

static void poolFree(void *arg, void *ptr) {
  SPoolMem *pPool = (SPoolMem *)arg;
  SPoolMem *pMem;

  pMem = &(((SPoolMem *)ptr)[-1]);

  pMem->next->prev = pMem->prev;
  pMem->prev->next = pMem->next;
  pPool->size -= pMem->size;

  tdbOsFree(pMem);
}