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
};

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  TDB_FREE(pVal);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
622 623
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid) {
  // TODO
C
Cary Xu 已提交
624 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
  // 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 已提交
685 686 687 688 689
}

int metaRemoveSmaFromDb(SMeta *pMeta, int64_t indexUid) {
  // TODO
  ASSERT(0);
C
Cary Xu 已提交
690 691 692 693 694 695 696 697 698 699 700 701
#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 已提交
702
  return 0;
H
Hongze Cheng 已提交
703 704 705 706
}

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

#ifdef META_TDB_SMA_TEST
H
Hongze Cheng 已提交
710
  int32_t  ret = 0;
C
Cary Xu 已提交
711
  SMetaDB *pMetaDb = pMeta->pDB;
H
Hongze Cheng 已提交
712 713
  void    *pBuf = NULL, *qBuf = NULL;
  void    *key = {0}, *val = {0};
C
Cary Xu 已提交
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

  // 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 已提交
760 761 762
  return 0;
}

C
Cary Xu 已提交
763
void *metaGetSmaInfoByIndex(SMeta *pMeta, int64_t indexUid, bool isDecode) {
H
Hongze Cheng 已提交
764
  // TODO
C
Cary Xu 已提交
765 766 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
  // 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 已提交
808 809
}

C
Cary Xu 已提交
810 811 812 813 814 815 816 817
/**
 * @brief
 *
 * @param pMeta
 * @param uid 0 means iterate all uids.
 * @return SMSmaCursor*
 */
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
H
Hongze Cheng 已提交
818
  // TODO
C
Cary Xu 已提交
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
  // 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 已提交
844 845
}

C
Cary Xu 已提交
846 847 848 849 850 851 852
/**
 * @brief
 *
 * @param pCur
 * @return int64_t smaIndexUid
 */
int64_t metaSmaCursorNext(SMSmaCursor *pCur) {
H
Hongze Cheng 已提交
853
  // TODO
C
Cary Xu 已提交
854 855 856 857 858 859 860 861 862 863 864 865 866 867
  // 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 已提交
868 869
}

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

    taosMemoryFree(pCur);
  }
#endif
H
Hongze Cheng 已提交
882 883
}

C
Cary Xu 已提交
884
SArray *metaGetSmaTbUids(SMeta *pMeta, bool isDup) {
H
Hongze Cheng 已提交
885
  // TODO
C
Cary Xu 已提交
886 887 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
  // 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 已提交
932
}
H
Hongze Cheng 已提交
933 934 935 936 937 938 939 940 941

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 已提交
942
    tlen += taosEncodeFixedI8(buf, pSchema->index);
H
Hongze Cheng 已提交
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
    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 已提交
959
    buf = taosSkipFixedLen(buf, sizeof(int8_t));
H
Hongze Cheng 已提交
960 961 962 963 964 965 966 967
    buf = taosDecodeFixedI16(buf, &pSchema->colId);
    buf = taosDecodeFixedI32(buf, &pSchema->bytes);
    buf = taosDecodeStringTo(buf, pSchema->name);
  }

  return buf;
}

C
Cary Xu 已提交
968 969 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
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 已提交
1012 1013 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
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 已提交
1056 1057

int metaCommit(SMeta *pMeta) {
H
Hongze Cheng 已提交
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
  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 已提交
1068
  return 0;
H
Hongze Cheng 已提交
1069 1070 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
}

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 已提交
1104
  void     *ptr = NULL;
H
Hongze Cheng 已提交
1105 1106 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
  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);
}