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
#include "tdbInt.h"
H
Hongze Cheng 已提交
19 20 21 22 23 24
typedef struct SPoolMem {
  int64_t          size;
  struct SPoolMem *prev;
  struct SPoolMem *next;
} SPoolMem;

C
Cary Xu 已提交
25 26
#define META_TDB_SMA_TEST

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

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

H
Hongze Cheng 已提交
49 50 51 52 53 54
typedef struct __attribute__((__packed__)) {
  tb_uid_t uid;
  int32_t  sver;
} SSchemaDbKey;

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

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

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

H
Hongze Cheng 已提交
69 70
static int   metaEncodeTbInfo(void **buf, STbCfg *pTbCfg);
static void *metaDecodeTbInfo(void *buf, STbCfg *pTbCfg);
H
Hongze Cheng 已提交
71 72
static int   metaEncodeSchema(void **buf, SSchemaWrapper *pSW);
static void *metaDecodeSchema(void *buf, SSchemaWrapper *pSW);
C
Cary Xu 已提交
73 74 75 76
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 已提交
77

H
Hongze Cheng 已提交
78 79 80 81 82 83 84
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 已提交
85
  uid2 = ((tb_uid_t *)arg2)[0];
H
Hongze Cheng 已提交
86 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

  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 已提交
129 130 131 132 133 134 135 136 137 138 139
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 已提交
140
int metaOpenDB(SMeta *pMeta) {
H
Hongze Cheng 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  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 已提交
161
  ret = tdbDbOpen("table.db", sizeof(tb_uid_t), TDB_VARIANT_LEN, metaUidCmpr, pMetaDb->pEnv, &(pMetaDb->pTbDB));
H
Hongze Cheng 已提交
162 163 164 165 166 167
  if (ret < 0) {
    // TODO
    ASSERT(0);
    return -1;
  }

C
Cary Xu 已提交
168 169 170 171 172 173 174 175 176
#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 已提交
177
  // open schema DB
H
Hongze Cheng 已提交
178
  ret = tdbDbOpen("schema.db", sizeof(SSchemaDbKey), TDB_VARIANT_LEN, metaSchemaKeyCmpr, pMetaDb->pEnv,
H
Hongze Cheng 已提交
179 180 181 182 183 184 185
                  &(pMetaDb->pSchemaDB));
  if (ret < 0) {
    // TODO
    ASSERT(0);
    return -1;
  }

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

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

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

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

C
Cary Xu 已提交
214 215 216 217 218 219 220 221 222
#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 已提交
223 224
  pMetaDb->pPool = openPool();
  tdbTxnOpen(&pMetaDb->txn, 0, poolMalloc, poolFree, pMetaDb->pPool, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED);
H
Hongze Cheng 已提交
225 226
  tdbBegin(pMetaDb->pEnv, NULL);

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

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

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

  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 已提交
275 276 277 278 279 280 281 282
  // 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 已提交
283
  // save to table.db
H
Hongze Cheng 已提交
284 285 286 287 288
  pKey = &uid;
  kLen = sizeof(uid);
  pVal = pBuf = buf;
  metaEncodeTbInfo(&pBuf, pTbCfg);
  vLen = POINTER_DISTANCE(pBuf, buf);
H
Hongze Cheng 已提交
289
  ret = tdbDbInsert(pMetaDb->pTbDB, pKey, kLen, pVal, vLen, &pMetaDb->txn);
H
Hongze Cheng 已提交
290 291 292 293
  if (ret < 0) {
    return -1;
  }

H
Hongze Cheng 已提交
294 295 296 297 298 299 300 301 302
  // 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 已提交
303
      schemaWrapper.pSchemaEx = pTbCfg->stbCfg.pSchema;
H
Hongze Cheng 已提交
304 305
    } else {
      schemaWrapper.nCols = pTbCfg->ntbCfg.nCols;
H
Hongze Cheng 已提交
306
      schemaWrapper.pSchemaEx = pTbCfg->ntbCfg.pSchema;
H
Hongze Cheng 已提交
307 308
    }
    pVal = pBuf = buf;
C
Cary Xu 已提交
309
    metaEncodeSchemaEx(&pBuf, &schemaWrapper);
H
Hongze Cheng 已提交
310
    vLen = POINTER_DISTANCE(pBuf, buf);
H
Hongze Cheng 已提交
311
    ret = tdbDbInsert(pMetaDb->pSchemaDB, pKey, kLen, pVal, vLen, &pMeta->pDB->txn);
H
Hongze Cheng 已提交
312 313 314
    if (ret < 0) {
      return -1;
    }
H
Hongze Cheng 已提交
315 316 317
  }

  // update name.idx
H
Hongze Cheng 已提交
318
  int nameLen = strlen(pTbCfg->name);
H
Hongze Cheng 已提交
319 320 321 322 323 324
  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 已提交
325
  ret = tdbDbInsert(pMetaDb->pNameIdx, pKey, kLen, pVal, vLen, &pMetaDb->txn);
H
Hongze Cheng 已提交
326 327 328 329
  if (ret < 0) {
    return -1;
  }

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

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

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

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

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

  // Fetch
  pKey = &uid;
  kLen = sizeof(uid);
H
Hongze Cheng 已提交
387
  pVal = NULL;
H
Hongze Cheng 已提交
388 389 390 391 392 393 394 395 396 397 398 399
  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 已提交
400 401 402
}

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

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

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

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

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

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

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

  // 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 已提交
457
  metaDecodeSchemaEx(pBuf, pSchemaWrapper, isGetEx);
H
Hongze Cheng 已提交
458 459 460 461

  TDB_FREE(pVal);

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

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

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

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

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

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

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

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

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

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

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

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

char *metaTbCursorNext(SMTbCursor *pTbCur) {
dengyihao's avatar
dengyihao 已提交
523 524
  void * pKey = NULL;
  void * pVal = NULL;
H
Hongze Cheng 已提交
525 526 527
  int    kLen;
  int    vLen;
  int    ret;
dengyihao's avatar
dengyihao 已提交
528
  void * pBuf;
H
Hongze Cheng 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
  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 已提交
547 548 549
  return NULL;
}

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

H
Hongze Cheng 已提交
559
SMCtbCursor *metaOpenCtbCursor(SMeta *pMeta, tb_uid_t uid) {
H
Hongze Cheng 已提交
560
  SMCtbCursor *pCtbCur = NULL;
dengyihao's avatar
dengyihao 已提交
561
  SMetaDB *    pDB = pMeta->pDB;
H
Hongze Cheng 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
  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 已提交
579 580 581
}

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
623 624
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid) {
  // TODO
C
Cary Xu 已提交
625 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 652 653 654 655 656 657 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
  // ASSERT(0);
  // return NULL;
#ifdef META_TDB_SMA_TEST
  STSmaWrapper *pSW = NULL;

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

  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
  if (pCur == NULL) {
    taosMemoryFree(pSW);
    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;
      }


      ++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 712 713 714 715 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
  // ASSERT(0);

#ifdef META_TDB_SMA_TEST
  int32_t ret = 0;
  SMetaDB *pMetaDb = pMeta->pDB;
  void   *pBuf = NULL, *qBuf = NULL;
  void   *key = {0}, *val = {0};

  // 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);
dengyihao's avatar
dengyihao 已提交
944
    tlen += taosEncodeFixedI8(buf, pSchema->index);
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 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
static int metaEncodeSchemaEx(void **buf, SSchemaWrapper *pSW) {
  int        tlen = 0;
  SSchemaEx *pSchema;

  tlen += taosEncodeFixedU32(buf, pSW->nCols);
  for (int i = 0; i < pSW->nCols; ++i) {
    pSchema = pSW->pSchemaEx + i;
    tlen += taosEncodeFixedI8(buf, pSchema->type);
    tlen += taosEncodeFixedI8(buf, pSchema->sma);
    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) {
    pSW->pSchemaEx = (SSchemaEx *)taosMemoryMalloc(sizeof(SSchemaEx) * pSW->nCols);
    for (int i = 0; i < pSW->nCols; i++) {
      SSchemaEx *pSchema = pSW->pSchemaEx + i;
      buf = taosDecodeFixedI8(buf, &pSchema->type);
      buf = taosDecodeFixedI8(buf, &pSchema->sma);
      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) {
dengyihao's avatar
dengyihao 已提交
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);
}