metaBDBImpl.c 25.0 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

16
#define ALLOW_FORBID_FUNC
H
more  
Hongze Cheng 已提交
17 18
#include "db.h"

H
Hongze Cheng 已提交
19
#include "vnodeInt.h"
H
Hongze Cheng 已提交
20

H
Hongze Cheng 已提交
21 22 23 24
#define IMPL_WITH_LOCK 1
// #if IMPL_WITH_LOCK
// #endif

H
Hongze Cheng 已提交
25 26 27
typedef struct {
  tb_uid_t uid;
  int32_t  sver;
H
Hongze Cheng 已提交
28
  int32_t  padding;
H
Hongze Cheng 已提交
29 30
} SSchemaKey;

H
Hongze Cheng 已提交
31
struct SMetaDB {
H
Hongze Cheng 已提交
32
#if IMPL_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
33
  TdThreadRwlock rwlock;
H
Hongze Cheng 已提交
34
#endif
H
more  
Hongze Cheng 已提交
35
  // DB
H
more  
Hongze Cheng 已提交
36 37
  DB *pTbDB;
  DB *pSchemaDB;
C
Cary Xu 已提交
38
  DB *pSmaDB;
C
Cary Xu 已提交
39

H
more  
Hongze Cheng 已提交
40
  // IDX
H
more  
Hongze Cheng 已提交
41 42 43 44
  DB *pNameIdx;
  DB *pStbIdx;
  DB *pNtbIdx;
  DB *pCtbIdx;
C
Cary Xu 已提交
45
  DB *pSmaIdx;
H
more  
Hongze Cheng 已提交
46
  // ENV
H
Hongze Cheng 已提交
47 48 49
  DB_ENV *pEvn;
};

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

H
more  
Hongze Cheng 已提交
52 53
static SMetaDB *metaNewDB();
static void     metaFreeDB(SMetaDB *pDB);
H
more  
Hongze Cheng 已提交
54 55
static int      metaOpenBDBEnv(DB_ENV **ppEnv, const char *path);
static void     metaCloseBDBEnv(DB_ENV *pEnv);
H
Hongze Cheng 已提交
56
static int      metaOpenBDBDb(DB **ppDB, DB_ENV *pEnv, const char *pFName, bool isDup);
H
more  
Hongze Cheng 已提交
57
static void     metaCloseBDBDb(DB *pDB);
H
Hongze Cheng 已提交
58
static int      metaOpenBDBIdx(DB **ppIdx, DB_ENV *pEnv, const char *pFName, DB *pDB, bdbIdxCbPtr cbf, bool isDup);
H
Hongze Cheng 已提交
59
static void     metaCloseBDBIdx(DB *pIdx);
H
Hongze Cheng 已提交
60 61 62 63
static int      metaNameIdxCb(DB *pIdx, const DBT *pKey, const DBT *pValue, DBT *pSKey);
static int      metaStbIdxCb(DB *pIdx, const DBT *pKey, const DBT *pValue, DBT *pSKey);
static int      metaNtbIdxCb(DB *pIdx, const DBT *pKey, const DBT *pValue, DBT *pSKey);
static int      metaCtbIdxCb(DB *pIdx, const DBT *pKey, const DBT *pValue, DBT *pSKey);
C
Cary Xu 已提交
64
static int      metaSmaIdxCb(DB *pIdx, const DBT *pKey, const DBT *pValue, DBT *pSKey);
H
Hongze Cheng 已提交
65
static int      metaEncodeTbInfo(void **buf, STbCfg *pTbCfg);
L
fix  
Liu Jicong 已提交
66
static void    *metaDecodeTbInfo(void *buf, STbCfg *pTbCfg);
H
Hongze Cheng 已提交
67
static void     metaClearTbCfg(STbCfg *pTbCfg);
H
Hongze Cheng 已提交
68
static int      metaEncodeSchema(void **buf, SSchemaWrapper *pSW);
L
fix  
Liu Jicong 已提交
69
static void    *metaDecodeSchema(void *buf, SSchemaWrapper *pSW);
C
Cary Xu 已提交
70 71
static int      metaEncodeSchemaEx(void **buf, SSchemaWrapper *pSW);
static void    *metaDecodeSchemaEx(void *buf, SSchemaWrapper *pSW, bool isGetEx);
H
Hongze Cheng 已提交
72 73 74
static void     metaDBWLock(SMetaDB *pDB);
static void     metaDBRLock(SMetaDB *pDB);
static void     metaDBULock(SMetaDB *pDB);
C
Cary Xu 已提交
75
static SSchemaWrapper *metaGetTableSchemaImpl(SMeta *pMeta, tb_uid_t uid, int32_t sver, bool isinline, bool isGetEx);
H
more  
Hongze Cheng 已提交
76

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

H
Hongze Cheng 已提交
79
int metaOpenDB(SMeta *pMeta) {
H
more  
Hongze Cheng 已提交
80
  SMetaDB *pDB;
H
Hongze Cheng 已提交
81

H
more  
Hongze Cheng 已提交
82 83 84
  // Create DB object
  pDB = metaNewDB();
  if (pDB == NULL) {
H
more  
Hongze Cheng 已提交
85 86 87
    return -1;
  }

H
more  
Hongze Cheng 已提交
88
  pMeta->pDB = pDB;
H
more  
Hongze Cheng 已提交
89

H
more  
Hongze Cheng 已提交
90 91
  // Open DB Env
  if (metaOpenBDBEnv(&(pDB->pEvn), pMeta->path) < 0) {
H
more  
Hongze Cheng 已提交
92
    metaCloseDB(pMeta);
H
more  
Hongze Cheng 已提交
93 94
    return -1;
  }
H
Hongze Cheng 已提交
95

H
more  
Hongze Cheng 已提交
96
  // Open DBs
H
Hongze Cheng 已提交
97
  if (metaOpenBDBDb(&(pDB->pTbDB), pDB->pEvn, "meta.db", false) < 0) {
H
more  
Hongze Cheng 已提交
98 99
    metaCloseDB(pMeta);
    return -1;
H
more  
Hongze Cheng 已提交
100 101
  }

H
more  
Hongze Cheng 已提交
102
  if (metaOpenBDBDb(&(pDB->pSchemaDB), pDB->pEvn, "schema.db", false) < 0) {
H
more  
Hongze Cheng 已提交
103 104 105
    metaCloseDB(pMeta);
    return -1;
  }
H
Hongze Cheng 已提交
106

C
Cary Xu 已提交
107 108 109 110 111
  if (metaOpenBDBDb(&(pDB->pSmaDB), pDB->pEvn, "sma.db", false) < 0) {
    metaCloseDB(pMeta);
    return -1;
  }

H
more  
Hongze Cheng 已提交
112
  // Open Indices
H
Hongze Cheng 已提交
113
  if (metaOpenBDBIdx(&(pDB->pNameIdx), pDB->pEvn, "name.index", pDB->pTbDB, &metaNameIdxCb, false) < 0) {
H
more  
Hongze Cheng 已提交
114 115 116
    metaCloseDB(pMeta);
    return -1;
  }
H
Hongze Cheng 已提交
117

H
Hongze Cheng 已提交
118
  if (metaOpenBDBIdx(&(pDB->pStbIdx), pDB->pEvn, "stb.index", pDB->pTbDB, &metaStbIdxCb, false) < 0) {
H
more  
Hongze Cheng 已提交
119 120 121
    metaCloseDB(pMeta);
    return -1;
  }
H
Hongze Cheng 已提交
122

H
Hongze Cheng 已提交
123
  if (metaOpenBDBIdx(&(pDB->pNtbIdx), pDB->pEvn, "ntb.index", pDB->pTbDB, &metaNtbIdxCb, false) < 0) {
H
more  
Hongze Cheng 已提交
124 125
    metaCloseDB(pMeta);
    return -1;
H
Hongze Cheng 已提交
126 127
  }

H
Hongze Cheng 已提交
128
  if (metaOpenBDBIdx(&(pDB->pCtbIdx), pDB->pEvn, "ctb.index", pDB->pTbDB, &metaCtbIdxCb, true) < 0) {
H
more  
Hongze Cheng 已提交
129 130
    metaCloseDB(pMeta);
    return -1;
H
more  
Hongze Cheng 已提交
131 132
  }

C
Cary Xu 已提交
133 134 135 136 137
  if (metaOpenBDBIdx(&(pDB->pSmaIdx), pDB->pEvn, "sma.index", pDB->pSmaDB, &metaSmaIdxCb, true) < 0) {
    metaCloseDB(pMeta);
    return -1;
  }

H
Hongze Cheng 已提交
138 139 140 141 142
  return 0;
}

void metaCloseDB(SMeta *pMeta) {
  if (pMeta->pDB) {
C
Cary Xu 已提交
143
    metaCloseBDBIdx(pMeta->pDB->pSmaIdx);
H
Hongze Cheng 已提交
144 145 146 147
    metaCloseBDBIdx(pMeta->pDB->pCtbIdx);
    metaCloseBDBIdx(pMeta->pDB->pNtbIdx);
    metaCloseBDBIdx(pMeta->pDB->pStbIdx);
    metaCloseBDBIdx(pMeta->pDB->pNameIdx);
C
Cary Xu 已提交
148
    metaCloseBDBDb(pMeta->pDB->pSmaDB);
H
Hongze Cheng 已提交
149 150
    metaCloseBDBDb(pMeta->pDB->pSchemaDB);
    metaCloseBDBDb(pMeta->pDB->pTbDB);
H
more  
Hongze Cheng 已提交
151
    metaCloseBDBEnv(pMeta->pDB->pEvn);
H
more  
Hongze Cheng 已提交
152 153
    metaFreeDB(pMeta->pDB);
    pMeta->pDB = NULL;
H
Hongze Cheng 已提交
154 155 156
  }
}

H
more  
Hongze Cheng 已提交
157
int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) {
C
Cary Xu 已提交
158 159 160 161 162 163 164
  tb_uid_t   uid;
  char       buf[512];
  char       buf1[512];
  void      *pBuf;
  DBT        key1, value1;
  DBT        key2, value2;
  SSchemaEx *pSchema = NULL;
H
Hongze Cheng 已提交
165 166 167 168 169 170 171 172 173

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

  {
    // save table info
H
Hongze Cheng 已提交
174
    pBuf = buf;
H
Hongze Cheng 已提交
175 176
    memset(&key1, 0, sizeof(key1));
    memset(&value1, 0, sizeof(key1));
H
Hongze Cheng 已提交
177

H
Hongze Cheng 已提交
178 179
    key1.data = &uid;
    key1.size = sizeof(uid);
H
Hongze Cheng 已提交
180

H
Hongze Cheng 已提交
181
    metaEncodeTbInfo(&pBuf, pTbCfg);
H
Hongze Cheng 已提交
182

H
Hongze Cheng 已提交
183 184 185
    value1.data = buf;
    value1.size = POINTER_DISTANCE(pBuf, buf);
    value1.app_data = pTbCfg;
H
Hongze Cheng 已提交
186 187 188
  }

  // save schema
H
more  
Hongze Cheng 已提交
189
  uint32_t ncols;
H
Hongze Cheng 已提交
190
  if (pTbCfg->type == META_SUPER_TABLE) {
H
more  
Hongze Cheng 已提交
191
    ncols = pTbCfg->stbCfg.nCols;
H
Hongze Cheng 已提交
192 193
    pSchema = pTbCfg->stbCfg.pSchema;
  } else if (pTbCfg->type == META_NORMAL_TABLE) {
H
more  
Hongze Cheng 已提交
194
    ncols = pTbCfg->ntbCfg.nCols;
H
Hongze Cheng 已提交
195 196 197 198
    pSchema = pTbCfg->ntbCfg.pSchema;
  }

  if (pSchema) {
H
fix  
Hongze Cheng 已提交
199
    pBuf = buf1;
H
Hongze Cheng 已提交
200 201
    memset(&key2, 0, sizeof(key2));
    memset(&value2, 0, sizeof(key2));
H
Hongze Cheng 已提交
202
    SSchemaKey schemaKey = {uid, 0 /*TODO*/, 0};
H
Hongze Cheng 已提交
203

H
Hongze Cheng 已提交
204 205
    key2.data = &schemaKey;
    key2.size = sizeof(schemaKey);
H
Hongze Cheng 已提交
206

C
Cary Xu 已提交
207 208
    SSchemaWrapper sw = {.nCols = ncols, .pSchemaEx = pSchema};
    metaEncodeSchemaEx(&pBuf, &sw);
H
Hongze Cheng 已提交
209

H
fix  
Hongze Cheng 已提交
210 211
    value2.data = buf1;
    value2.size = POINTER_DISTANCE(pBuf, buf1);
H
Hongze Cheng 已提交
212 213
  }

H
Hongze Cheng 已提交
214 215
  metaDBWLock(pMeta->pDB);
  pMeta->pDB->pTbDB->put(pMeta->pDB->pTbDB, NULL, &key1, &value1, 0);
H
fix  
Hongze Cheng 已提交
216 217 218
  if (pSchema) {
    pMeta->pDB->pSchemaDB->put(pMeta->pDB->pSchemaDB, NULL, &key2, &value2, 0);
  }
H
Hongze Cheng 已提交
219 220
  metaDBULock(pMeta->pDB);

H
more  
Hongze Cheng 已提交
221 222 223 224 225 226 227 228
  return 0;
}

int metaRemoveTableFromDb(SMeta *pMeta, tb_uid_t uid) {
  // TODO
  return 0;
}

C
Cary Xu 已提交
229
int metaSaveSmaToDB(SMeta *pMeta, STSma *pSmaCfg) {
C
Cary Xu 已提交
230 231
  // char  buf[512] = {0};  // TODO: may overflow
  void *pBuf = NULL, *qBuf = NULL;
C
Cary Xu 已提交
232 233
  DBT   key1 = {0}, value1 = {0};

234 235
  // save sma info
  int32_t len = tEncodeTSma(NULL, pSmaCfg);
236
  pBuf = taosMemoryCalloc(1, len);
237 238 239
  if (pBuf == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
C
Cary Xu 已提交
240 241
  }

242 243 244 245 246 247 248 249 250 251
  key1.data = (void *)&pSmaCfg->indexUid;
  key1.size = sizeof(pSmaCfg->indexUid);

  qBuf = pBuf;
  tEncodeTSma(&qBuf, pSmaCfg);

  value1.data = pBuf;
  value1.size = POINTER_DISTANCE(qBuf, pBuf);
  value1.app_data = pSmaCfg;

C
Cary Xu 已提交
252 253 254 255
  metaDBWLock(pMeta->pDB);
  pMeta->pDB->pSmaDB->put(pMeta->pDB->pSmaDB, NULL, &key1, &value1, 0);
  metaDBULock(pMeta->pDB);

256
  // release
wafwerar's avatar
wafwerar 已提交
257
  taosMemoryFreeClear(pBuf);
258

C
Cary Xu 已提交
259 260 261
  return 0;
}

H
Hongze Cheng 已提交
262
int metaRemoveSmaFromDb(SMeta *pMeta, int64_t indexUid) {
C
Cary Xu 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
  // TODO
#if 0
  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
  return 0;
}

H
more  
Hongze Cheng 已提交
279
/* ------------------------ STATIC METHODS ------------------------ */
H
Hongze Cheng 已提交
280 281 282 283 284 285 286 287
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);
288
    tlen += taosEncodeFixedI16(buf, pSchema->colId);
H
Hongze Cheng 已提交
289 290 291 292 293 294 295 296 297 298 299
    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);
wafwerar's avatar
wafwerar 已提交
300
  pSW->pSchema = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * pSW->nCols);
C
Cary Xu 已提交
301 302

  int8_t dummy;
H
Hongze Cheng 已提交
303 304 305
  for (int i = 0; i < pSW->nCols; i++) {
    pSchema = pSW->pSchema + i;
    buf = taosDecodeFixedI8(buf, &pSchema->type);
306
    buf = taosDecodeFixedI16(buf, &pSchema->colId);
H
Hongze Cheng 已提交
307 308 309 310 311 312 313
    buf = taosDecodeFixedI32(buf, &pSchema->bytes);
    buf = taosDecodeStringTo(buf, pSchema->name);
  }

  return buf;
}

C
Cary Xu 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
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
more  
Hongze Cheng 已提交
358 359
static SMetaDB *metaNewDB() {
  SMetaDB *pDB = NULL;
wafwerar's avatar
wafwerar 已提交
360
  pDB = (SMetaDB *)taosMemoryCalloc(1, sizeof(*pDB));
H
more  
Hongze Cheng 已提交
361 362 363 364
  if (pDB == NULL) {
    return NULL;
  }

H
Hongze Cheng 已提交
365
#if IMPL_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
366
  taosThreadRwlockInit(&pDB->rwlock, NULL);
H
Hongze Cheng 已提交
367 368
#endif

H
more  
Hongze Cheng 已提交
369 370 371 372 373
  return pDB;
}

static void metaFreeDB(SMetaDB *pDB) {
  if (pDB) {
H
Hongze Cheng 已提交
374
#if IMPL_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
375
    taosThreadRwlockDestroy(&pDB->rwlock);
H
Hongze Cheng 已提交
376
#endif
wafwerar's avatar
wafwerar 已提交
377
    taosMemoryFree(pDB);
H
more  
Hongze Cheng 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
  }
}

static int metaOpenBDBEnv(DB_ENV **ppEnv, const char *path) {
  int     ret;
  DB_ENV *pEnv;

  if (path == NULL) return 0;

  ret = db_env_create(&pEnv, 0);
  if (ret != 0) {
    BDB_PERR("Failed to create META env", ret);
    return -1;
  }

H
Hongze Cheng 已提交
393
  ret = pEnv->open(pEnv, path, DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL, 0);
H
more  
Hongze Cheng 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
  if (ret != 0) {
    BDB_PERR("Failed to open META env", ret);
    return -1;
  }

  *ppEnv = pEnv;

  return 0;
}

static void metaCloseBDBEnv(DB_ENV *pEnv) {
  if (pEnv) {
    pEnv->close(pEnv, 0);
  }
}

H
Hongze Cheng 已提交
410
static int metaOpenBDBDb(DB **ppDB, DB_ENV *pEnv, const char *pFName, bool isDup) {
H
more  
Hongze Cheng 已提交
411 412 413
  int ret;
  DB *pDB;

H
Hongze Cheng 已提交
414
  ret = db_create(&(pDB), pEnv, 0);
H
more  
Hongze Cheng 已提交
415 416 417 418 419
  if (ret != 0) {
    BDB_PERR("Failed to create META DB", ret);
    return -1;
  }

H
Hongze Cheng 已提交
420 421 422 423 424 425 426 427
  if (isDup) {
    ret = pDB->set_flags(pDB, DB_DUPSORT);
    if (ret != 0) {
      BDB_PERR("Failed to set DB flags", ret);
      return -1;
    }
  }

H
more  
Hongze Cheng 已提交
428 429 430 431 432 433
  ret = pDB->open(pDB, NULL, pFName, NULL, DB_BTREE, DB_CREATE, 0);
  if (ret) {
    BDB_PERR("Failed to open META DB", ret);
    return -1;
  }

H
Hongze Cheng 已提交
434 435
  *ppDB = pDB;

H
more  
Hongze Cheng 已提交
436 437 438 439 440 441 442 443 444
  return 0;
}

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

H
Hongze Cheng 已提交
445
static int metaOpenBDBIdx(DB **ppIdx, DB_ENV *pEnv, const char *pFName, DB *pDB, bdbIdxCbPtr cbf, bool isDup) {
H
Hongze Cheng 已提交
446 447 448
  DB *pIdx;
  int ret;

H
Hongze Cheng 已提交
449
  if (metaOpenBDBDb(ppIdx, pEnv, pFName, isDup) < 0) {
H
Hongze Cheng 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
    return -1;
  }

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

  return 0;
}

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

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

H
Hongze Cheng 已提交
471
  memset(pSKey, 0, sizeof(*pSKey));
H
Hongze Cheng 已提交
472

H
Hongze Cheng 已提交
473 474
  pSKey->data = pTbCfg->name;
  pSKey->size = strlen(pTbCfg->name);
H
Hongze Cheng 已提交
475 476 477 478

  return 0;
}

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

H
Hongze Cheng 已提交
482
  if (pTbCfg->type == META_SUPER_TABLE) {
H
Hongze Cheng 已提交
483 484 485
    memset(pSKey, 0, sizeof(*pSKey));
    pSKey->data = pKey->data;
    pSKey->size = pKey->size;
H
Hongze Cheng 已提交
486

H
Hongze Cheng 已提交
487
    return 0;
H
Hongze Cheng 已提交
488
  } else {
H
Hongze Cheng 已提交
489
    return DB_DONOTINDEX;
H
Hongze Cheng 已提交
490
  }
H
Hongze Cheng 已提交
491 492
}

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

H
Hongze Cheng 已提交
496
  if (pTbCfg->type == META_NORMAL_TABLE) {
H
Hongze Cheng 已提交
497 498 499
    memset(pSKey, 0, sizeof(*pSKey));
    pSKey->data = pKey->data;
    pSKey->size = pKey->size;
H
Hongze Cheng 已提交
500

H
Hongze Cheng 已提交
501 502 503 504
    return 0;
  } else {
    return DB_DONOTINDEX;
  }
H
Hongze Cheng 已提交
505 506
}

H
Hongze Cheng 已提交
507
static int metaCtbIdxCb(DB *pIdx, const DBT *pKey, const DBT *pValue, DBT *pSKey) {
H
Hongze Cheng 已提交
508
  STbCfg *pTbCfg = (STbCfg *)(pValue->app_data);
L
fix  
Liu Jicong 已提交
509
  DBT    *pDbt;
H
Hongze Cheng 已提交
510

H
Hongze Cheng 已提交
511
  if (pTbCfg->type == META_CHILD_TABLE) {
wafwerar's avatar
wafwerar 已提交
512
    // pDbt = taosMemoryCalloc(2, sizeof(DBT));
H
Hongze Cheng 已提交
513

H
Hongze Cheng 已提交
514 515 516
    // // First key is suid
    // pDbt[0].data = &(pTbCfg->ctbCfg.suid);
    // pDbt[0].size = sizeof(pTbCfg->ctbCfg.suid);
H
Hongze Cheng 已提交
517

H
Hongze Cheng 已提交
518 519 520 521
    // // Second key is the first tag
    // void *pTagVal = tdGetKVRowValOfCol(pTbCfg->ctbCfg.pTag, (kvRowColIdx(pTbCfg->ctbCfg.pTag))[0].colId);
    // pDbt[1].data = pTagVal;
    // pDbt[1].size = sizeof(int32_t);
H
Hongze Cheng 已提交
522

H
Hongze Cheng 已提交
523
    // Set index key
H
Hongze Cheng 已提交
524
    memset(pSKey, 0, sizeof(*pSKey));
H
Hongze Cheng 已提交
525
#if 0
H
Hongze Cheng 已提交
526 527 528
    pSKey->flags = DB_DBT_MULTIPLE | DB_DBT_APPMALLOC;
    pSKey->data = pDbt;
    pSKey->size = 2;
H
Hongze Cheng 已提交
529 530 531 532
#else
    pSKey->data = &(pTbCfg->ctbCfg.suid);
    pSKey->size = sizeof(pTbCfg->ctbCfg.suid);
#endif
H
Hongze Cheng 已提交
533

H
Hongze Cheng 已提交
534 535 536 537
    return 0;
  } else {
    return DB_DONOTINDEX;
  }
H
Hongze Cheng 已提交
538 539
}

C
Cary Xu 已提交
540
static int metaSmaIdxCb(DB *pIdx, const DBT *pKey, const DBT *pValue, DBT *pSKey) {
C
Cary Xu 已提交
541
  STSma *pSmaCfg = (STSma *)(pValue->app_data);
C
Cary Xu 已提交
542 543 544 545 546 547 548 549

  memset(pSKey, 0, sizeof(*pSKey));
  pSKey->data = &(pSmaCfg->tableUid);
  pSKey->size = sizeof(pSmaCfg->tableUid);

  return 0;
}

H
Hongze Cheng 已提交
550
static int metaEncodeTbInfo(void **buf, STbCfg *pTbCfg) {
H
more  
Hongze Cheng 已提交
551 552 553 554 555
  int tsize = 0;

  tsize += taosEncodeString(buf, pTbCfg->name);
  tsize += taosEncodeFixedU32(buf, pTbCfg->ttl);
  tsize += taosEncodeFixedU32(buf, pTbCfg->keep);
C
Cary Xu 已提交
556
  tsize += taosEncodeFixedU8(buf, pTbCfg->info);
H
more  
Hongze Cheng 已提交
557 558

  if (pTbCfg->type == META_SUPER_TABLE) {
H
Hongze Cheng 已提交
559 560
    SSchemaWrapper sw = {.nCols = pTbCfg->stbCfg.nTagCols, .pSchema = pTbCfg->stbCfg.pTagSchema};
    tsize += metaEncodeSchema(buf, &sw);
H
more  
Hongze Cheng 已提交
561 562 563 564
  } 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) {
565
    // TODO
H
more  
Hongze Cheng 已提交
566 567 568 569 570
  } else {
    ASSERT(0);
  }

  return tsize;
H
Hongze Cheng 已提交
571 572 573
}

static void *metaDecodeTbInfo(void *buf, STbCfg *pTbCfg) {
H
more  
Hongze Cheng 已提交
574 575 576
  buf = taosDecodeString(buf, &(pTbCfg->name));
  buf = taosDecodeFixedU32(buf, &(pTbCfg->ttl));
  buf = taosDecodeFixedU32(buf, &(pTbCfg->keep));
C
Cary Xu 已提交
577
  buf = taosDecodeFixedU8(buf, &(pTbCfg->info));
H
more  
Hongze Cheng 已提交
578 579

  if (pTbCfg->type == META_SUPER_TABLE) {
H
Hongze Cheng 已提交
580 581 582 583
    SSchemaWrapper sw;
    buf = metaDecodeSchema(buf, &sw);
    pTbCfg->stbCfg.nTagCols = sw.nCols;
    pTbCfg->stbCfg.pTagSchema = sw.pSchema;
H
more  
Hongze Cheng 已提交
584 585 586 587
  } 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) {
588
    // TODO
H
more  
Hongze Cheng 已提交
589 590 591 592
  } else {
    ASSERT(0);
  }
  return buf;
H
Hongze Cheng 已提交
593 594
}

H
Hongze Cheng 已提交
595
static void metaClearTbCfg(STbCfg *pTbCfg) {
wafwerar's avatar
wafwerar 已提交
596
  taosMemoryFreeClear(pTbCfg->name);
H
Hongze Cheng 已提交
597 598 599
  if (pTbCfg->type == META_SUPER_TABLE) {
    tdFreeSchema(pTbCfg->stbCfg.pTagSchema);
  } else if (pTbCfg->type == META_CHILD_TABLE) {
wafwerar's avatar
wafwerar 已提交
600
    taosMemoryFreeClear(pTbCfg->ctbCfg.pTag);
H
Hongze Cheng 已提交
601
  }
H
more  
Hongze Cheng 已提交
602 603 604
}

/* ------------------------ FOR QUERY ------------------------ */
H
Hongze Cheng 已提交
605
STbCfg *metaGetTbInfoByUid(SMeta *pMeta, tb_uid_t uid) {
L
fix  
Liu Jicong 已提交
606
  STbCfg  *pTbCfg = NULL;
H
Hongze Cheng 已提交
607 608 609 610 611 612 613 614 615 616
  SMetaDB *pDB = pMeta->pDB;
  DBT      key = {0};
  DBT      value = {0};
  int      ret;

  // Set key/value
  key.data = &uid;
  key.size = sizeof(uid);

  // Query
H
Hongze Cheng 已提交
617
  metaDBRLock(pDB);
H
Hongze Cheng 已提交
618
  ret = pDB->pTbDB->get(pDB->pTbDB, NULL, &key, &value, 0);
H
Hongze Cheng 已提交
619
  metaDBULock(pDB);
H
Hongze Cheng 已提交
620 621 622 623 624
  if (ret != 0) {
    return NULL;
  }

  // Decode
wafwerar's avatar
wafwerar 已提交
625
  pTbCfg = (STbCfg *)taosMemoryMalloc(sizeof(*pTbCfg));
H
Hongze Cheng 已提交
626 627 628
  if (pTbCfg == NULL) {
    return NULL;
  }
H
more  
Hongze Cheng 已提交
629

H
Hongze Cheng 已提交
630 631 632 633 634
  metaDecodeTbInfo(value.data, pTbCfg);

  return pTbCfg;
}

H
Hongze Cheng 已提交
635
STbCfg *metaGetTbInfoByName(SMeta *pMeta, char *tbname, tb_uid_t *uid) {
L
fix  
Liu Jicong 已提交
636
  STbCfg  *pTbCfg = NULL;
H
Hongze Cheng 已提交
637 638 639 640 641 642 643
  SMetaDB *pDB = pMeta->pDB;
  DBT      key = {0};
  DBT      pkey = {0};
  DBT      pvalue = {0};
  int      ret;

  // Set key/value
H
more  
Hongze Cheng 已提交
644
  key.data = tbname;
H
Hongze Cheng 已提交
645
  key.size = strlen(tbname);
H
more  
Hongze Cheng 已提交
646

H
Hongze Cheng 已提交
647
  // Query
H
Hongze Cheng 已提交
648
  metaDBRLock(pDB);
H
Hongze Cheng 已提交
649
  ret = pDB->pNameIdx->pget(pDB->pNameIdx, NULL, &key, &pkey, &pvalue, 0);
H
Hongze Cheng 已提交
650
  metaDBULock(pDB);
H
Hongze Cheng 已提交
651
  if (ret != 0) {
H
Hongze Cheng 已提交
652
    return NULL;
H
Hongze Cheng 已提交
653 654
  }

H
Hongze Cheng 已提交
655 656
  // Decode
  *uid = *(tb_uid_t *)(pkey.data);
wafwerar's avatar
wafwerar 已提交
657
  pTbCfg = (STbCfg *)taosMemoryMalloc(sizeof(*pTbCfg));
H
Hongze Cheng 已提交
658 659 660 661 662 663 664
  if (pTbCfg == NULL) {
    return NULL;
  }

  metaDecodeTbInfo(pvalue.data, pTbCfg);

  return pTbCfg;
H
more  
Hongze Cheng 已提交
665 666
}

C
Cary Xu 已提交
667
void *metaGetSmaInfoByIndex(SMeta *pMeta, int64_t indexUid, bool isDecode) {
H
Hongze Cheng 已提交
668
  STSma   *pCfg = NULL;
C
Cary Xu 已提交
669 670 671 672 673 674
  SMetaDB *pDB = pMeta->pDB;
  DBT      key = {0};
  DBT      value = {0};
  int      ret;

  // Set key/value
C
Cary Xu 已提交
675 676
  key.data = (void *)&indexUid;
  key.size = sizeof(indexUid);
C
Cary Xu 已提交
677 678 679 680 681 682 683 684 685 686

  // Query
  metaDBRLock(pDB);
  ret = pDB->pTbDB->get(pDB->pSmaDB, NULL, &key, &value, 0);
  metaDBULock(pDB);
  if (ret != 0) {
    return NULL;
  }

  // Decode
wafwerar's avatar
wafwerar 已提交
687
  pCfg = (STSma *)taosMemoryCalloc(1, sizeof(STSma));
C
Cary Xu 已提交
688 689 690 691
  if (pCfg == NULL) {
    return NULL;
  }

C
Cary Xu 已提交
692
  if (tDecodeTSma(value.data, pCfg) == NULL) {
wafwerar's avatar
wafwerar 已提交
693
    taosMemoryFreeClear(pCfg);
C
Cary Xu 已提交
694 695
    return NULL;
  }
C
Cary Xu 已提交
696 697 698 699

  return pCfg;
}

H
Hongze Cheng 已提交
700
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, bool isinline) {
C
Cary Xu 已提交
701 702 703 704
  return metaGetTableSchemaImpl(pMeta, uid, sver, isinline, false);
}

static SSchemaWrapper *metaGetTableSchemaImpl(SMeta *pMeta, tb_uid_t uid, int32_t sver, bool isinline, bool isGetEx) {
H
more  
Hongze Cheng 已提交
705 706
  uint32_t        nCols;
  SSchemaWrapper *pSW = NULL;
L
fix  
Liu Jicong 已提交
707
  SMetaDB        *pDB = pMeta->pDB;
H
more  
Hongze Cheng 已提交
708
  int             ret;
L
fix  
Liu Jicong 已提交
709
  void           *pBuf;
C
Cary Xu 已提交
710
  // SSchema        *pSchema;
H
Hongze Cheng 已提交
711 712 713
  SSchemaKey schemaKey = {uid, sver, 0};
  DBT        key = {0};
  DBT        value = {0};
H
more  
Hongze Cheng 已提交
714 715 716 717 718 719

  // Set key/value properties
  key.data = &schemaKey;
  key.size = sizeof(schemaKey);

  // Query
H
Hongze Cheng 已提交
720
  metaDBRLock(pDB);
H
more  
Hongze Cheng 已提交
721
  ret = pDB->pSchemaDB->get(pDB->pSchemaDB, NULL, &key, &value, 0);
H
Hongze Cheng 已提交
722
  metaDBULock(pDB);
H
more  
Hongze Cheng 已提交
723
  if (ret != 0) {
H
Hongze Cheng 已提交
724
    printf("failed to query schema DB since %s================\n", db_strerror(ret));
H
more  
Hongze Cheng 已提交
725 726 727 728 729
    return NULL;
  }

  // Decode the schema
  pBuf = value.data;
wafwerar's avatar
wafwerar 已提交
730
  pSW = taosMemoryMalloc(sizeof(*pSW));
C
Cary Xu 已提交
731
  metaDecodeSchemaEx(pBuf, pSW, isGetEx);
H
more  
Hongze Cheng 已提交
732 733

  return pSW;
H
more  
Hongze Cheng 已提交
734 735 736 737 738 739 740 741
}

struct SMTbCursor {
  DBC *pCur;
};

SMTbCursor *metaOpenTbCursor(SMeta *pMeta) {
  SMTbCursor *pTbCur = NULL;
L
fix  
Liu Jicong 已提交
742
  SMetaDB    *pDB = pMeta->pDB;
H
more  
Hongze Cheng 已提交
743

wafwerar's avatar
wafwerar 已提交
744
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
H
more  
Hongze Cheng 已提交
745 746 747 748 749 750
  if (pTbCur == NULL) {
    return NULL;
  }

  pDB->pTbDB->cursor(pDB->pTbDB, NULL, &(pTbCur->pCur), 0);

H
more  
Hongze Cheng 已提交
751 752 753 754 755 756
#if 0
    DB_BTREE_STAT *sp;
    pDB->pTbDB->stat(pDB->pTbDB, NULL, &sp, 0);
    printf("**************** %ld\n", sp->bt_nkeys);
#endif

H
more  
Hongze Cheng 已提交
757 758 759
  return pTbCur;
}

D
dapan1121 已提交
760
int metaGetTbNum(SMeta *pMeta) {
H
Hongze Cheng 已提交
761
  SMetaDB *pDB = pMeta->pDB;
D
dapan1121 已提交
762 763 764

  DB_BTREE_STAT *sp1;
  pDB->pTbDB->stat(pDB->pNtbIdx, NULL, &sp1, 0);
H
Hongze Cheng 已提交
765

D
dapan1121 已提交
766 767
  DB_BTREE_STAT *sp2;
  pDB->pTbDB->stat(pDB->pCtbIdx, NULL, &sp2, 0);
H
Hongze Cheng 已提交
768

D
dapan1121 已提交
769 770 771
  return sp1->bt_nkeys + sp2->bt_nkeys;
}

H
more  
Hongze Cheng 已提交
772 773 774 775 776
void metaCloseTbCursor(SMTbCursor *pTbCur) {
  if (pTbCur) {
    if (pTbCur->pCur) {
      pTbCur->pCur->close(pTbCur->pCur);
    }
wafwerar's avatar
wafwerar 已提交
777
    taosMemoryFree(pTbCur);
H
more  
Hongze Cheng 已提交
778 779 780 781 782 783 784
  }
}

char *metaTbCursorNext(SMTbCursor *pTbCur) {
  DBT    key = {0};
  DBT    value = {0};
  STbCfg tbCfg;
L
fix  
Liu Jicong 已提交
785
  void  *pBuf;
H
more  
Hongze Cheng 已提交
786

H
Hongze Cheng 已提交
787 788 789 790 791
  for (;;) {
    if (pTbCur->pCur->get(pTbCur->pCur, &key, &value, DB_NEXT) == 0) {
      pBuf = value.data;
      metaDecodeTbInfo(pBuf, &tbCfg);
      if (tbCfg.type == META_SUPER_TABLE) {
wafwerar's avatar
wafwerar 已提交
792 793
        taosMemoryFree(tbCfg.name);
        taosMemoryFree(tbCfg.stbCfg.pTagSchema);
H
Hongze Cheng 已提交
794
        continue;
H
Hongze Cheng 已提交
795
      } else if (tbCfg.type == META_CHILD_TABLE) {
H
Hongze Cheng 已提交
796
        kvRowFree(tbCfg.ctbCfg.pTag);
H
Hongze Cheng 已提交
797 798 799 800 801
      }
      return tbCfg.name;
    } else {
      return NULL;
    }
H
more  
Hongze Cheng 已提交
802
  }
H
more  
Hongze Cheng 已提交
803 804 805 806
}

STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver) {
  STSchemaBuilder sb;
L
fix  
Liu Jicong 已提交
807
  STSchema       *pTSchema = NULL;
C
Cary Xu 已提交
808
  SSchemaEx      *pSchema;
H
more  
Hongze Cheng 已提交
809
  SSchemaWrapper *pSW;
L
fix  
Liu Jicong 已提交
810
  STbCfg         *pTbCfg;
H
Hongze Cheng 已提交
811
  tb_uid_t        quid;
H
more  
Hongze Cheng 已提交
812

H
Hongze Cheng 已提交
813 814 815 816 817 818 819
  pTbCfg = metaGetTbInfoByUid(pMeta, uid);
  if (pTbCfg->type == META_CHILD_TABLE) {
    quid = pTbCfg->ctbCfg.suid;
  } else {
    quid = uid;
  }

C
Cary Xu 已提交
820
  pSW = metaGetTableSchemaImpl(pMeta, quid, sver, true, true);
H
more  
Hongze Cheng 已提交
821 822 823 824 825 826
  if (pSW == NULL) {
    return NULL;
  }

  // Rebuild a schema
  tdInitTSchemaBuilder(&sb, 0);
C
Cary Xu 已提交
827 828 829
  for (int32_t i = 0; i < pSW->nCols; ++i) {
    pSchema = pSW->pSchemaEx + i;
    tdAddColToSchema(&sb, pSchema->type, pSchema->sma, pSchema->colId, pSchema->bytes);
H
more  
Hongze Cheng 已提交
830 831 832 833 834
  }
  pTSchema = tdGetSchemaFromBuilder(&sb);
  tdDestroyTSchemaBuilder(&sb);

  return pTSchema;
H
more  
Hongze Cheng 已提交
835 836 837
}

struct SMCtbCursor {
L
fix  
Liu Jicong 已提交
838
  DBC     *pCur;
H
more  
Hongze Cheng 已提交
839 840 841 842 843
  tb_uid_t suid;
};

SMCtbCursor *metaOpenCtbCursor(SMeta *pMeta, tb_uid_t uid) {
  SMCtbCursor *pCtbCur = NULL;
L
fix  
Liu Jicong 已提交
844
  SMetaDB     *pDB = pMeta->pDB;
H
more  
Hongze Cheng 已提交
845 846
  int          ret;

wafwerar's avatar
wafwerar 已提交
847
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
H
more  
Hongze Cheng 已提交
848 849 850 851 852 853 854
  if (pCtbCur == NULL) {
    return NULL;
  }

  pCtbCur->suid = uid;
  ret = pDB->pCtbIdx->cursor(pDB->pCtbIdx, NULL, &(pCtbCur->pCur), 0);
  if (ret != 0) {
wafwerar's avatar
wafwerar 已提交
855
    taosMemoryFree(pCtbCur);
H
more  
Hongze Cheng 已提交
856 857 858 859 860 861 862 863 864 865 866 867
    return NULL;
  }

  return pCtbCur;
}

void metaCloseCtbCurosr(SMCtbCursor *pCtbCur) {
  if (pCtbCur) {
    if (pCtbCur->pCur) {
      pCtbCur->pCur->close(pCtbCur->pCur);
    }

wafwerar's avatar
wafwerar 已提交
868
    taosMemoryFree(pCtbCur);
H
more  
Hongze Cheng 已提交
869 870 871
  }
}

872
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
H
more  
Hongze Cheng 已提交
873 874 875
  DBT    skey = {0};
  DBT    pkey = {0};
  DBT    pval = {0};
L
fix  
Liu Jicong 已提交
876
  void  *pBuf;
H
more  
Hongze Cheng 已提交
877 878 879 880 881 882
  STbCfg tbCfg;

  // Set key
  skey.data = &(pCtbCur->suid);
  skey.size = sizeof(pCtbCur->suid);

883 884 885 886
  if (pCtbCur->pCur->pget(pCtbCur->pCur, &skey, &pkey, &pval, DB_NEXT) == 0) {
    tb_uid_t id = *(tb_uid_t *)pkey.data;
    assert(id != 0);
    return id;
H
Hongze Cheng 已提交
887 888
    //    metaDecodeTbInfo(pBuf, &tbCfg);
    //    return tbCfg.;
H
more  
Hongze Cheng 已提交
889
  } else {
890
    return 0;
H
more  
Hongze Cheng 已提交
891
  }
H
Hongze Cheng 已提交
892 893
}

C
Cary Xu 已提交
894 895 896 897 898 899 900 901 902 903
struct SMSmaCursor {
  DBC     *pCur;
  tb_uid_t uid;
};

SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
  SMSmaCursor *pCur = NULL;
  SMetaDB     *pDB = pMeta->pDB;
  int          ret;

wafwerar's avatar
wafwerar 已提交
904
  pCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pCur));
C
Cary Xu 已提交
905 906 907 908 909
  if (pCur == NULL) {
    return NULL;
  }

  pCur->uid = uid;
C
Cary Xu 已提交
910
  // TODO: lock?
C
Cary Xu 已提交
911
  ret = pDB->pCtbIdx->cursor(pDB->pSmaIdx, NULL, &(pCur->pCur), 0);
C
Cary Xu 已提交
912
  if ((ret != 0) || (pCur->pCur == NULL)) {
wafwerar's avatar
wafwerar 已提交
913
    taosMemoryFree(pCur);
C
Cary Xu 已提交
914 915 916 917 918 919
    return NULL;
  }

  return pCur;
}

C
Cary Xu 已提交
920
void metaCloseSmaCursor(SMSmaCursor *pCur) {
C
Cary Xu 已提交
921 922 923 924 925
  if (pCur) {
    if (pCur->pCur) {
      pCur->pCur->close(pCur->pCur);
    }

wafwerar's avatar
wafwerar 已提交
926
    taosMemoryFree(pCur);
C
Cary Xu 已提交
927 928 929
  }
}

C
Cary Xu 已提交
930 931
int64_t metaSmaCursorNext(SMSmaCursor *pCur) {
#if 0
C
Cary Xu 已提交
932 933 934
  DBT skey = {0};
  DBT pkey = {0};
  DBT pval = {0};
C
Cary Xu 已提交
935 936 937 938

  // Set key
  skey.data = &(pCur->uid);
  skey.size = sizeof(pCur->uid);
C
Cary Xu 已提交
939
  // TODO: lock?
C
Cary Xu 已提交
940
  if (pCur->pCur->pget(pCur->pCur, &skey, &pkey, &pval, DB_NEXT) == 0) {
C
Cary Xu 已提交
941
    const char *indexName = (const char *)pkey.data;
C
Cary Xu 已提交
942 943 944
    assert(indexName != NULL);
    return indexName;
  } else {
C
Cary Xu 已提交
945
    return NULL;
C
Cary Xu 已提交
946
  }
C
Cary Xu 已提交
947 948
#endif
  return 0;
C
Cary Xu 已提交
949 950
}

C
Cary Xu 已提交
951
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid) {
C
Cary Xu 已提交
952 953
  STSmaWrapper *pSW = NULL;

wafwerar's avatar
wafwerar 已提交
954
  pSW = taosMemoryCalloc(1, sizeof(*pSW));
C
Cary Xu 已提交
955 956 957 958 959 960
  if (pSW == NULL) {
    return NULL;
  }

  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
  if (pCur == NULL) {
wafwerar's avatar
wafwerar 已提交
961
    taosMemoryFree(pSW);
C
Cary Xu 已提交
962 963 964
    return NULL;
  }

C
Cary Xu 已提交
965 966
  DBT   skey = {.data = &(pCur->uid), .size = sizeof(pCur->uid)};
  DBT   pval = {0};
C
Cary Xu 已提交
967 968 969 970 971 972
  void *pBuf = NULL;

  while (true) {
    // TODO: lock?
    if (pCur->pCur->pget(pCur->pCur, &skey, NULL, &pval, DB_NEXT) == 0) {
      ++pSW->number;
wafwerar's avatar
wafwerar 已提交
973
      STSma *tptr = (STSma *)taosMemoryRealloc(pSW->tSma, pSW->number * sizeof(STSma));
C
Cary Xu 已提交
974
      if (tptr == NULL) {
C
Cary Xu 已提交
975
        metaCloseSmaCursor(pCur);
C
Cary Xu 已提交
976
        tdDestroyTSmaWrapper(pSW);
wafwerar's avatar
wafwerar 已提交
977
        taosMemoryFreeClear(pSW);
C
Cary Xu 已提交
978 979 980 981 982
        return NULL;
      }
      pSW->tSma = tptr;
      pBuf = pval.data;
      if (tDecodeTSma(pBuf, pSW->tSma + pSW->number - 1) == NULL) {
C
Cary Xu 已提交
983
        metaCloseSmaCursor(pCur);
C
Cary Xu 已提交
984
        tdDestroyTSmaWrapper(pSW);
wafwerar's avatar
wafwerar 已提交
985
        taosMemoryFreeClear(pSW);
C
Cary Xu 已提交
986 987 988 989 990 991 992
        return NULL;
      }
      continue;
    }
    break;
  }

C
Cary Xu 已提交
993 994
  metaCloseSmaCursor(pCur);

C
Cary Xu 已提交
995 996
  return pSW;
}
C
Cary Xu 已提交
997

C
Cary Xu 已提交
998
SArray *metaGetSmaTbUids(SMeta *pMeta, bool isDup) {
C
Cary Xu 已提交
999
  SArray  *pUids = NULL;
C
Cary Xu 已提交
1000
  SMetaDB *pDB = pMeta->pDB;
C
Cary Xu 已提交
1001
  DBC     *pCur = NULL;
C
Cary Xu 已提交
1002
  DBT      pkey = {0}, pval = {0};
1003
  uint32_t mode = isDup ? DB_NEXT_DUP : DB_NEXT_NODUP;
C
Cary Xu 已提交
1004 1005 1006
  int      ret;

  // TODO: lock?
C
Cary Xu 已提交
1007
  ret = pDB->pSmaIdx->cursor(pDB->pSmaIdx, NULL, &pCur, 0);
C
Cary Xu 已提交
1008 1009 1010 1011
  if (ret != 0) {
    return NULL;
  }
  // TODO: lock?
C
Cary Xu 已提交
1012

1013
  while ((ret = pCur->get(pCur, &pkey, &pval, mode)) == 0) {
C
Cary Xu 已提交
1014 1015 1016 1017 1018 1019 1020 1021
    if (!pUids) {
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
      if (!pUids) {
        return NULL;
      }
    }

    taosArrayPush(pUids, pkey.data);
C
Cary Xu 已提交
1022
  }
C
Cary Xu 已提交
1023
  // TODO: lock?
C
Cary Xu 已提交
1024 1025 1026 1027 1028 1029 1030 1031

  if (pCur) {
    pCur->close(pCur);
  }

  return pUids;
}

H
Hongze Cheng 已提交
1032 1033
static void metaDBWLock(SMetaDB *pDB) {
#if IMPL_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
1034
  taosThreadRwlockWrlock(&(pDB->rwlock));
H
Hongze Cheng 已提交
1035 1036 1037 1038 1039
#endif
}

static void metaDBRLock(SMetaDB *pDB) {
#if IMPL_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
1040
  taosThreadRwlockRdlock(&(pDB->rwlock));
H
Hongze Cheng 已提交
1041 1042 1043 1044 1045
#endif
}

static void metaDBULock(SMetaDB *pDB) {
#if IMPL_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
1046
  taosThreadRwlockUnlock(&(pDB->rwlock));
H
Hongze Cheng 已提交
1047 1048
#endif
}