metaTDBImpl.c 20.4 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
#ifndef META_REFACT

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

C
Cary Xu 已提交
26 27
#define META_TDB_SMA_TEST

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
333
  // update other index
H
Hongze Cheng 已提交
334
  if (pTbCfg->type == META_SUPER_TABLE) {
H
Hongze Cheng 已提交
335 336 337 338
    pKey = &uid;
    kLen = sizeof(uid);
    pVal = NULL;
    vLen = 0;
H
Hongze Cheng 已提交
339
    ret = tdbDbInsert(pMetaDb->pStbIdx, pKey, kLen, pVal, vLen, &pMetaDb->txn);
H
Hongze Cheng 已提交
340 341 342 343
    if (ret < 0) {
      return -1;
    }
  } else if (pTbCfg->type == META_CHILD_TABLE) {
H
Hongze Cheng 已提交
344 345 346 347 348 349
    ctbIdxKey.suid = pTbCfg->ctbCfg.suid;
    ctbIdxKey.uid = uid;
    pKey = &ctbIdxKey;
    kLen = sizeof(ctbIdxKey);
    pVal = NULL;
    vLen = 0;
H
Hongze Cheng 已提交
350
    ret = tdbDbInsert(pMetaDb->pCtbIdx, pKey, kLen, pVal, vLen, &pMetaDb->txn);
H
Hongze Cheng 已提交
351 352 353
    if (ret < 0) {
      return -1;
    }
C
Cary Xu 已提交
354 355 356 357 358 359
    // child table handle for rsma
    if (pHandle && pHandle->fp) {
      if (((*pHandle->fp)(pHandle->ahandle, &pHandle->result, &ctbIdxKey.suid, &uid)) < 0) {
        return -1;
      };
    }
H
Hongze Cheng 已提交
360
  } else if (pTbCfg->type == META_NORMAL_TABLE) {
H
Hongze Cheng 已提交
361 362 363 364
    pKey = &uid;
    kLen = sizeof(uid);
    pVal = NULL;
    vLen = 0;
H
Hongze Cheng 已提交
365
    ret = tdbDbInsert(pMetaDb->pNtbIdx, pKey, kLen, pVal, vLen, &pMetaDb->txn);
H
Hongze Cheng 已提交
366 367 368 369 370
    if (ret < 0) {
      return -1;
    }
  }

H
Hongze Cheng 已提交
371 372 373 374
  if (pMeta->pDB->pPool->size > 0) {
    metaCommit(pMeta);
  }

H
Hongze Cheng 已提交
375 376 377 378 379
  return 0;
}

int metaRemoveTableFromDb(SMeta *pMeta, tb_uid_t uid) {
  // TODO
H
Hongze Cheng 已提交
380
  ASSERT(0);
H
Hongze Cheng 已提交
381 382 383
  return 0;
}

C
Cary Xu 已提交
384
static SSchemaWrapper *metaGetTableSchemaImpl(SMeta *pMeta, tb_uid_t uid, int32_t sver, bool isinline, bool isGetEx) {
H
Hongze Cheng 已提交
385 386
  void           *pKey;
  void           *pVal;
H
Hongze Cheng 已提交
387 388 389 390 391
  int             kLen;
  int             vLen;
  int             ret;
  SSchemaDbKey    schemaDbKey;
  SSchemaWrapper *pSchemaWrapper;
H
Hongze Cheng 已提交
392
  void           *pBuf;
H
Hongze Cheng 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

  // 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 已提交
408
  metaDecodeSchemaEx(pBuf, pSchemaWrapper, isGetEx);
H
Hongze Cheng 已提交
409

H
Hongze Cheng 已提交
410
  tdbFree(pVal);
H
Hongze Cheng 已提交
411 412

  return pSchemaWrapper;
H
Hongze Cheng 已提交
413 414
}

C
Cary Xu 已提交
415 416 417 418 419 420 421 422 423
struct SMSmaCursor {
  TDBC    *pCur;
  tb_uid_t uid;
  void    *pKey;
  void    *pVal;
  int      kLen;
  int      vLen;
};

H
Hongze Cheng 已提交
424 425
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid) {
  // TODO
C
Cary Xu 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
  // 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?
H
Hongze Cheng 已提交
441
    if (tdbDbcNext(pCur->pCur, &pCur->pKey, &pCur->kLen, NULL, &pCur->vLen) == 0) {
C
Cary Xu 已提交
442 443 444 445 446 447 448 449 450 451
      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 已提交
452
      if ((pSW == NULL) && ((pSW = taosMemoryCalloc(1, sizeof(*pSW))) == NULL)) {
H
Hongze Cheng 已提交
453
        tdbFree(pSmaVal);
C
Cary Xu 已提交
454 455 456 457
        metaCloseSmaCursor(pCur);
        return NULL;
      }

C
Cary Xu 已提交
458 459 460
      ++pSW->number;
      STSma *tptr = (STSma *)taosMemoryRealloc(pSW->tSma, pSW->number * sizeof(STSma));
      if (tptr == NULL) {
H
Hongze Cheng 已提交
461
        tdbFree(pSmaVal);
C
Cary Xu 已提交
462 463 464 465 466 467 468 469
        metaCloseSmaCursor(pCur);
        tdDestroyTSmaWrapper(pSW);
        taosMemoryFreeClear(pSW);
        return NULL;
      }
      pSW->tSma = tptr;
      pBuf = pSmaVal;
      if (tDecodeTSma(pBuf, pSW->tSma + pSW->number - 1) == NULL) {
H
Hongze Cheng 已提交
470
        tdbFree(pSmaVal);
C
Cary Xu 已提交
471 472 473 474 475
        metaCloseSmaCursor(pCur);
        tdDestroyTSmaWrapper(pSW);
        taosMemoryFreeClear(pSW);
        return NULL;
      }
H
Hongze Cheng 已提交
476
      tdbFree(pSmaVal);
C
Cary Xu 已提交
477 478 479 480 481 482 483 484 485 486
      continue;
    }
    break;
  }

  metaCloseSmaCursor(pCur);

  return pSW;

#endif
H
Hongze Cheng 已提交
487 488 489 490 491
}

int metaRemoveSmaFromDb(SMeta *pMeta, int64_t indexUid) {
  // TODO
  ASSERT(0);
C
Cary Xu 已提交
492 493 494 495 496 497 498 499 500 501 502 503
#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 已提交
504
  return 0;
H
Hongze Cheng 已提交
505 506 507 508
}

int metaSaveSmaToDB(SMeta *pMeta, STSma *pSmaCfg) {
  // TODO
C
Cary Xu 已提交
509 510 511
  // ASSERT(0);

#ifdef META_TDB_SMA_TEST
H
Hongze Cheng 已提交
512
  int32_t  ret = 0;
C
Cary Xu 已提交
513
  SMetaDB *pMetaDb = pMeta->pDB;
H
Hongze Cheng 已提交
514 515
  void    *pBuf = NULL, *qBuf = NULL;
  void    *key = {0}, *val = {0};
C
Cary Xu 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532

  // 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);

H
Hongze Cheng 已提交
533
  ret = tdbDbInsert(pMeta->pDB->pSmaDB, key, kLen, val, vLen, &pMetaDb->txn);
C
Cary Xu 已提交
534 535 536 537 538 539 540 541 542 543 544 545 546 547
  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;

H
Hongze Cheng 已提交
548
  ret = tdbDbInsert(pMeta->pDB->pSmaIdx, key, kLen, val, vLen, &pMetaDb->txn);
C
Cary Xu 已提交
549 550 551 552 553 554 555 556 557 558 559 560 561
  if (ret < 0) {
    taosMemoryFreeClear(pBuf);
    return -1;
  }

  // release
  taosMemoryFreeClear(pBuf);

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

#endif
H
Hongze Cheng 已提交
562 563 564
  return 0;
}

C
Cary Xu 已提交
565 566 567 568 569 570 571 572
/**
 * @brief
 *
 * @param pMeta
 * @param uid 0 means iterate all uids.
 * @return SMSmaCursor*
 */
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
H
Hongze Cheng 已提交
573
  // TODO
C
Cary Xu 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
  // 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 已提交
599 600
}

C
Cary Xu 已提交
601 602 603 604 605 606 607
/**
 * @brief
 *
 * @param pCur
 * @return int64_t smaIndexUid
 */
int64_t metaSmaCursorNext(SMSmaCursor *pCur) {
H
Hongze Cheng 已提交
608
  // TODO
C
Cary Xu 已提交
609 610 611 612 613 614 615
  // ASSERT(0);
  // return NULL;
#ifdef META_TDB_SMA_TEST
  int         ret;
  void       *pBuf;
  SSmaIdxKey *smaIdxKey;

H
Hongze Cheng 已提交
616
  ret = tdbDbcNext(pCur->pCur, &pCur->pKey, &pCur->kLen, &pCur->pVal, &pCur->vLen);
C
Cary Xu 已提交
617 618 619 620 621 622
  if (ret < 0) {
    return 0;
  }
  smaIdxKey = pCur->pKey;
  return smaIdxKey->smaUid;
#endif
H
Hongze Cheng 已提交
623 624
}

C
Cary Xu 已提交
625
void metaCloseSmaCursor(SMSmaCursor *pCur) {
H
Hongze Cheng 已提交
626
  // TODO
C
Cary Xu 已提交
627 628 629 630 631 632 633 634 635 636
  // ASSERT(0);
#ifdef META_TDB_SMA_TEST
  if (pCur) {
    if (pCur->pCur) {
      tdbDbcClose(pCur->pCur);
    }

    taosMemoryFree(pCur);
  }
#endif
H
Hongze Cheng 已提交
637 638
}

H
Hongze Cheng 已提交
639 640 641 642 643 644 645 646
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 已提交
647
    tlen += taosEncodeFixedI8(buf, pSchema->flags);
H
Hongze Cheng 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
    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 已提交
664
    buf = taosSkipFixedLen(buf, sizeof(int8_t));
H
Hongze Cheng 已提交
665 666 667 668 669 670 671 672
    buf = taosDecodeFixedI16(buf, &pSchema->colId);
    buf = taosDecodeFixedI32(buf, &pSchema->bytes);
    buf = taosDecodeStringTo(buf, pSchema->name);
  }

  return buf;
}

C
Cary Xu 已提交
673
static int metaEncodeSchemaEx(void **buf, SSchemaWrapper *pSW) {
H
Hongze Cheng 已提交
674 675
  int      tlen = 0;
  SSchema *pSchema;
C
Cary Xu 已提交
676 677 678

  tlen += taosEncodeFixedU32(buf, pSW->nCols);
  for (int i = 0; i < pSW->nCols; ++i) {
H
Hongze Cheng 已提交
679
    pSchema = pSW->pSchema + i;
C
Cary Xu 已提交
680
    tlen += taosEncodeFixedI8(buf, pSchema->type);
H
Hongze Cheng 已提交
681
    tlen += taosEncodeFixedI8(buf, pSchema->flags);
C
Cary Xu 已提交
682 683 684 685 686 687 688 689 690 691 692
    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 已提交
693
    pSW->pSchema = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * pSW->nCols);
C
Cary Xu 已提交
694
    for (int i = 0; i < pSW->nCols; i++) {
H
Hongze Cheng 已提交
695
      SSchema *pSchema = pSW->pSchema + i;
C
Cary Xu 已提交
696
      buf = taosDecodeFixedI8(buf, &pSchema->type);
H
Hongze Cheng 已提交
697
      buf = taosDecodeFixedI8(buf, &pSchema->flags);
C
Cary Xu 已提交
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
      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 已提交
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
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 已提交
761 762

int metaCommit(SMeta *pMeta) {
H
Hongze Cheng 已提交
763 764 765 766 767 768 769 770 771 772
  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 已提交
773
  return 0;
H
Hongze Cheng 已提交
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
}

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 已提交
809
  void     *ptr = NULL;
H
Hongze Cheng 已提交
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
  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);
}
H
Hongze Cheng 已提交
842 843

#endif