metaBDBImpl.c 13.1 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/>.
 */

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

H
Hongze Cheng 已提交
18 19
#include "metaDef.h"

H
Hongze Cheng 已提交
20
#include "tcoding.h"
H
more  
Hongze Cheng 已提交
21
#include "thash.h"
H
Hongze Cheng 已提交
22

H
Hongze Cheng 已提交
23 24 25 26 27
typedef struct {
  tb_uid_t uid;
  int32_t  sver;
} SSchemaKey;

H
Hongze Cheng 已提交
28
struct SMetaDB {
H
more  
Hongze Cheng 已提交
29
  // DB
H
more  
Hongze Cheng 已提交
30 31
  DB *pTbDB;
  DB *pSchemaDB;
H
more  
Hongze Cheng 已提交
32
  // IDX
H
more  
Hongze Cheng 已提交
33 34 35 36
  DB *pNameIdx;
  DB *pStbIdx;
  DB *pNtbIdx;
  DB *pCtbIdx;
H
more  
Hongze Cheng 已提交
37
  // ENV
H
Hongze Cheng 已提交
38 39 40
  DB_ENV *pEvn;
};

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

H
more  
Hongze Cheng 已提交
43 44
static SMetaDB *metaNewDB();
static void     metaFreeDB(SMetaDB *pDB);
H
more  
Hongze Cheng 已提交
45 46
static int      metaOpenBDBEnv(DB_ENV **ppEnv, const char *path);
static void     metaCloseBDBEnv(DB_ENV *pEnv);
H
Hongze Cheng 已提交
47
static int      metaOpenBDBDb(DB **ppDB, DB_ENV *pEnv, const char *pFName, bool isDup);
H
more  
Hongze Cheng 已提交
48
static void     metaCloseBDBDb(DB *pDB);
H
Hongze Cheng 已提交
49
static int      metaOpenBDBIdx(DB **ppIdx, DB_ENV *pEnv, const char *pFName, DB *pDB, bdbIdxCbPtr cbf, bool isDup);
H
Hongze Cheng 已提交
50
static void     metaCloseBDBIdx(DB *pIdx);
H
Hongze Cheng 已提交
51 52 53 54
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);
H
Hongze Cheng 已提交
55 56 57
static int      metaEncodeTbInfo(void **buf, STbCfg *pTbCfg);
static void *   metaDecodeTbInfo(void *buf, STbCfg *pTbCfg);
static void     metaClearTbCfg(STbCfg *pTbCfg);
H
more  
Hongze Cheng 已提交
58

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

H
Hongze Cheng 已提交
61
int metaOpenDB(SMeta *pMeta) {
H
more  
Hongze Cheng 已提交
62
  SMetaDB *pDB;
H
Hongze Cheng 已提交
63

H
more  
Hongze Cheng 已提交
64 65 66
  // Create DB object
  pDB = metaNewDB();
  if (pDB == NULL) {
H
more  
Hongze Cheng 已提交
67 68 69
    return -1;
  }

H
more  
Hongze Cheng 已提交
70
  pMeta->pDB = pDB;
H
more  
Hongze Cheng 已提交
71

H
more  
Hongze Cheng 已提交
72 73
  // Open DB Env
  if (metaOpenBDBEnv(&(pDB->pEvn), pMeta->path) < 0) {
H
more  
Hongze Cheng 已提交
74
    metaCloseDB(pMeta);
H
more  
Hongze Cheng 已提交
75 76
    return -1;
  }
H
Hongze Cheng 已提交
77

H
more  
Hongze Cheng 已提交
78
  // Open DBs
H
Hongze Cheng 已提交
79
  if (metaOpenBDBDb(&(pDB->pTbDB), pDB->pEvn, "meta.db", false) < 0) {
H
more  
Hongze Cheng 已提交
80 81
    metaCloseDB(pMeta);
    return -1;
H
more  
Hongze Cheng 已提交
82 83
  }

H
Hongze Cheng 已提交
84
  if (metaOpenBDBDb(&(pDB->pSchemaDB), pDB->pEvn, "meta.db", false) < 0) {
H
more  
Hongze Cheng 已提交
85 86 87
    metaCloseDB(pMeta);
    return -1;
  }
H
Hongze Cheng 已提交
88

H
more  
Hongze Cheng 已提交
89
  // Open Indices
H
Hongze Cheng 已提交
90
  if (metaOpenBDBIdx(&(pDB->pNameIdx), pDB->pEvn, "name.index", pDB->pTbDB, &metaNameIdxCb, false) < 0) {
H
more  
Hongze Cheng 已提交
91 92 93
    metaCloseDB(pMeta);
    return -1;
  }
H
Hongze Cheng 已提交
94

H
Hongze Cheng 已提交
95
  if (metaOpenBDBIdx(&(pDB->pStbIdx), pDB->pEvn, "stb.index", pDB->pTbDB, &metaStbIdxCb, false) < 0) {
H
more  
Hongze Cheng 已提交
96 97 98
    metaCloseDB(pMeta);
    return -1;
  }
H
Hongze Cheng 已提交
99

H
Hongze Cheng 已提交
100
  if (metaOpenBDBIdx(&(pDB->pNtbIdx), pDB->pEvn, "ntb.index", pDB->pTbDB, &metaNtbIdxCb, false) < 0) {
H
more  
Hongze Cheng 已提交
101 102
    metaCloseDB(pMeta);
    return -1;
H
Hongze Cheng 已提交
103 104
  }

H
Hongze Cheng 已提交
105
  if (metaOpenBDBIdx(&(pDB->pCtbIdx), pDB->pEvn, "ctb.index", pDB->pTbDB, &metaCtbIdxCb, true) < 0) {
H
more  
Hongze Cheng 已提交
106 107
    metaCloseDB(pMeta);
    return -1;
H
more  
Hongze Cheng 已提交
108 109
  }

H
Hongze Cheng 已提交
110 111 112 113 114
  return 0;
}

void metaCloseDB(SMeta *pMeta) {
  if (pMeta->pDB) {
H
Hongze Cheng 已提交
115 116 117 118 119 120
    metaCloseBDBIdx(pMeta->pDB->pCtbIdx);
    metaCloseBDBIdx(pMeta->pDB->pNtbIdx);
    metaCloseBDBIdx(pMeta->pDB->pStbIdx);
    metaCloseBDBIdx(pMeta->pDB->pNameIdx);
    metaCloseBDBDb(pMeta->pDB->pSchemaDB);
    metaCloseBDBDb(pMeta->pDB->pTbDB);
H
more  
Hongze Cheng 已提交
121
    metaCloseBDBEnv(pMeta->pDB->pEvn);
H
more  
Hongze Cheng 已提交
122 123
    metaFreeDB(pMeta->pDB);
    pMeta->pDB = NULL;
H
Hongze Cheng 已提交
124 125 126
  }
}

H
more  
Hongze Cheng 已提交
127
int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) {
H
more  
Hongze Cheng 已提交
128 129 130 131 132
  tb_uid_t uid;
  char     buf[512];
  void *   pBuf;
  DBT      key, value;
  SSchema *pSchema = NULL;
H
Hongze Cheng 已提交
133 134 135 136 137 138 139 140 141

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

  {
    // save table info
H
Hongze Cheng 已提交
142 143 144
    pBuf = buf;
    memset(&key, 0, sizeof(key));
    memset(&value, 0, sizeof(key));
H
Hongze Cheng 已提交
145 146 147 148

    key.data = &uid;
    key.size = sizeof(uid);

H
Hongze Cheng 已提交
149
    metaEncodeTbInfo(&pBuf, pTbCfg);
H
Hongze Cheng 已提交
150 151 152

    value.data = buf;
    value.size = POINTER_DISTANCE(pBuf, buf);
H
Hongze Cheng 已提交
153
    value.app_data = pTbCfg;
H
Hongze Cheng 已提交
154 155 156 157 158

    pMeta->pDB->pTbDB->put(pMeta->pDB->pTbDB, NULL, &key, &value, 0);
  }

  // save schema
H
more  
Hongze Cheng 已提交
159
  uint32_t ncols;
H
Hongze Cheng 已提交
160
  if (pTbCfg->type == META_SUPER_TABLE) {
H
more  
Hongze Cheng 已提交
161
    ncols = pTbCfg->stbCfg.nCols;
H
Hongze Cheng 已提交
162 163
    pSchema = pTbCfg->stbCfg.pSchema;
  } else if (pTbCfg->type == META_NORMAL_TABLE) {
H
more  
Hongze Cheng 已提交
164
    ncols = pTbCfg->ntbCfg.nCols;
H
Hongze Cheng 已提交
165 166 167 168
    pSchema = pTbCfg->ntbCfg.pSchema;
  }

  if (pSchema) {
H
Hongze Cheng 已提交
169 170 171
    pBuf = buf;
    memset(&key, 0, sizeof(key));
    memset(&value, 0, sizeof(key));
H
more  
Hongze Cheng 已提交
172
    SSchemaKey schemaKey = {uid, 0 /*TODO*/};
H
Hongze Cheng 已提交
173

H
Hongze Cheng 已提交
174 175
    key.data = &schemaKey;
    key.size = sizeof(schemaKey);
H
Hongze Cheng 已提交
176

H
more  
Hongze Cheng 已提交
177 178 179 180 181 182 183
    taosEncodeFixedU32(&pBuf, ncols);
    for (size_t i = 0; i < ncols; i++) {
      taosEncodeFixedI8(&pBuf, pSchema[i].type);
      taosEncodeFixedI32(&pBuf, pSchema[i].colId);
      taosEncodeFixedI32(&pBuf, pSchema[i].bytes);
      taosEncodeString(&pBuf, pSchema[i].name);
    }
H
Hongze Cheng 已提交
184 185 186 187 188 189 190

    value.data = buf;
    value.size = POINTER_DISTANCE(pBuf, buf);

    pMeta->pDB->pSchemaDB->put(pMeta->pDB->pSchemaDB, NULL, &key, &value, 0);
  }

H
more  
Hongze Cheng 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
  return 0;
}

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

/* ------------------------ STATIC METHODS ------------------------ */
static SMetaDB *metaNewDB() {
  SMetaDB *pDB = NULL;
  pDB = (SMetaDB *)calloc(1, sizeof(*pDB));
  if (pDB == NULL) {
    return NULL;
  }

  return pDB;
}

static void metaFreeDB(SMetaDB *pDB) {
  if (pDB) {
    free(pDB);
  }
}

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

  ret = pEnv->open(pEnv, path, DB_CREATE | DB_INIT_MPOOL, 0);
  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 已提交
245
static int metaOpenBDBDb(DB **ppDB, DB_ENV *pEnv, const char *pFName, bool isDup) {
H
more  
Hongze Cheng 已提交
246 247 248
  int ret;
  DB *pDB;

H
Hongze Cheng 已提交
249
  ret = db_create(&(pDB), pEnv, 0);
H
more  
Hongze Cheng 已提交
250 251 252 253 254
  if (ret != 0) {
    BDB_PERR("Failed to create META DB", ret);
    return -1;
  }

H
Hongze Cheng 已提交
255 256 257 258 259 260 261 262
  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 已提交
263 264 265 266 267 268
  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 已提交
269 270
  *ppDB = pDB;

H
more  
Hongze Cheng 已提交
271 272 273 274 275 276 277 278 279
  return 0;
}

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

H
Hongze Cheng 已提交
280
static int metaOpenBDBIdx(DB **ppIdx, DB_ENV *pEnv, const char *pFName, DB *pDB, bdbIdxCbPtr cbf, bool isDup) {
H
Hongze Cheng 已提交
281 282 283
  DB *pIdx;
  int ret;

H
Hongze Cheng 已提交
284
  if (metaOpenBDBDb(ppIdx, pEnv, pFName, isDup) < 0) {
H
Hongze Cheng 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    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 已提交
304
  STbCfg *pTbCfg = (STbCfg *)(pValue->app_data);
H
Hongze Cheng 已提交
305

H
Hongze Cheng 已提交
306
  memset(pSKey, 0, sizeof(*pSKey));
H
Hongze Cheng 已提交
307

H
Hongze Cheng 已提交
308 309
  pSKey->data = pTbCfg->name;
  pSKey->size = strlen(pTbCfg->name);
H
Hongze Cheng 已提交
310 311 312 313

  return 0;
}

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

H
Hongze Cheng 已提交
317
  if (pTbCfg->type == META_SUPER_TABLE) {
H
Hongze Cheng 已提交
318 319 320
    memset(pSKey, 0, sizeof(*pSKey));
    pSKey->data = pKey->data;
    pSKey->size = pKey->size;
H
Hongze Cheng 已提交
321

H
Hongze Cheng 已提交
322
    return 0;
H
Hongze Cheng 已提交
323
  } else {
H
Hongze Cheng 已提交
324
    return DB_DONOTINDEX;
H
Hongze Cheng 已提交
325
  }
H
Hongze Cheng 已提交
326 327
}

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

H
Hongze Cheng 已提交
331
  if (pTbCfg->type == META_NORMAL_TABLE) {
H
Hongze Cheng 已提交
332 333 334
    memset(pSKey, 0, sizeof(*pSKey));
    pSKey->data = pKey->data;
    pSKey->size = pKey->size;
H
Hongze Cheng 已提交
335

H
Hongze Cheng 已提交
336 337 338 339
    return 0;
  } else {
    return DB_DONOTINDEX;
  }
H
Hongze Cheng 已提交
340 341
}

H
Hongze Cheng 已提交
342
static int metaCtbIdxCb(DB *pIdx, const DBT *pKey, const DBT *pValue, DBT *pSKey) {
H
Hongze Cheng 已提交
343
  STbCfg *pTbCfg = (STbCfg *)(pValue->app_data);
H
Hongze Cheng 已提交
344
  DBT *   pDbt;
H
Hongze Cheng 已提交
345

H
Hongze Cheng 已提交
346
  if (pTbCfg->type == META_CHILD_TABLE) {
H
Hongze Cheng 已提交
347 348 349 350 351 352 353
    pDbt = calloc(2, sizeof(DBT));

    // First key is suid
    pDbt[0].data = &(pTbCfg->ctbCfg.suid);
    pDbt[0].size = sizeof(pTbCfg->ctbCfg.suid);

    // Second key is the first tag
H
Hongze Cheng 已提交
354
    void *pTagVal = tdGetKVRowValOfCol(pTbCfg->ctbCfg.pTag, (kvRowColIdx(pTbCfg->ctbCfg.pTag))[0].colId);
H
more  
Hongze Cheng 已提交
355 356
    pDbt[1].data = varDataVal(pTagVal);
    pDbt[1].size = varDataLen(pTagVal);
H
Hongze Cheng 已提交
357

H
Hongze Cheng 已提交
358
    // Set index key
H
Hongze Cheng 已提交
359
    memset(pSKey, 0, sizeof(*pSKey));
H
Hongze Cheng 已提交
360 361 362
    pSKey->flags = DB_DBT_MULTIPLE | DB_DBT_APPMALLOC;
    pSKey->data = pDbt;
    pSKey->size = 2;
H
Hongze Cheng 已提交
363

H
Hongze Cheng 已提交
364 365 366 367
    return 0;
  } else {
    return DB_DONOTINDEX;
  }
H
Hongze Cheng 已提交
368 369 370
}

static int metaEncodeTbInfo(void **buf, STbCfg *pTbCfg) {
H
more  
Hongze Cheng 已提交
371 372 373 374 375
  int tsize = 0;

  tsize += taosEncodeString(buf, pTbCfg->name);
  tsize += taosEncodeFixedU32(buf, pTbCfg->ttl);
  tsize += taosEncodeFixedU32(buf, pTbCfg->keep);
H
Hongze Cheng 已提交
376
  tsize += taosEncodeFixedU8(buf, pTbCfg->type);
H
more  
Hongze Cheng 已提交
377 378

  if (pTbCfg->type == META_SUPER_TABLE) {
H
more  
Hongze Cheng 已提交
379 380 381 382 383 384 385 386 387
    tsize += taosEncodeVariantU32(buf, pTbCfg->stbCfg.nTagCols);
    for (uint32_t i = 0; i < pTbCfg->stbCfg.nTagCols; i++) {
      tsize += taosEncodeFixedI8(buf, pTbCfg->stbCfg.pSchema[i].type);
      tsize += taosEncodeFixedI32(buf, pTbCfg->stbCfg.pSchema[i].colId);
      tsize += taosEncodeFixedI32(buf, pTbCfg->stbCfg.pSchema[i].bytes);
      tsize += taosEncodeString(buf, pTbCfg->stbCfg.pSchema[i].name);
    }

    // tsize += tdEncodeSchema(buf, pTbCfg->stbCfg.pTagSchema);
H
more  
Hongze Cheng 已提交
388 389 390 391 392 393 394 395 396
  } 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) {
  } else {
    ASSERT(0);
  }

  return tsize;
H
Hongze Cheng 已提交
397 398 399
}

static void *metaDecodeTbInfo(void *buf, STbCfg *pTbCfg) {
H
more  
Hongze Cheng 已提交
400 401 402
  buf = taosDecodeString(buf, &(pTbCfg->name));
  buf = taosDecodeFixedU32(buf, &(pTbCfg->ttl));
  buf = taosDecodeFixedU32(buf, &(pTbCfg->keep));
H
Hongze Cheng 已提交
403
  buf = taosDecodeFixedU8(buf, &(pTbCfg->type));
H
more  
Hongze Cheng 已提交
404 405

  if (pTbCfg->type == META_SUPER_TABLE) {
H
more  
Hongze Cheng 已提交
406
    buf = taosDecodeVariantU32(buf, &(pTbCfg->stbCfg.nTagCols));
H
more  
Hongze Cheng 已提交
407 408
    pTbCfg->stbCfg.pTagSchema = (SSchema *)malloc(sizeof(SSchema) * pTbCfg->stbCfg.nTagCols);
    for (uint32_t i = 0; i < pTbCfg->stbCfg.nTagCols; i++) {
H
more  
Hongze Cheng 已提交
409
      buf = taosDecodeFixedI8(buf, &(pTbCfg->stbCfg.pSchema[i].type));
H
more  
Hongze Cheng 已提交
410 411 412 413
      buf = taosDecodeFixedI32(buf, &pTbCfg->stbCfg.pSchema[i].colId);
      buf = taosDecodeFixedI32(buf, &pTbCfg->stbCfg.pSchema[i].bytes);
      buf = taosDecodeStringTo(buf, pTbCfg->stbCfg.pSchema[i].name);
    }
H
more  
Hongze Cheng 已提交
414 415 416 417 418 419 420 421
  } 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) {
  } else {
    ASSERT(0);
  }
  return buf;
H
Hongze Cheng 已提交
422 423
}

H
Hongze Cheng 已提交
424 425 426 427 428 429 430
static void metaClearTbCfg(STbCfg *pTbCfg) {
  tfree(pTbCfg->name);
  if (pTbCfg->type == META_SUPER_TABLE) {
    tdFreeSchema(pTbCfg->stbCfg.pTagSchema);
  } else if (pTbCfg->type == META_CHILD_TABLE) {
    tfree(pTbCfg->ctbCfg.pTag);
  }
H
more  
Hongze Cheng 已提交
431 432 433
}

/* ------------------------ FOR QUERY ------------------------ */
H
Hongze Cheng 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446
int metaGetTableInfo(SMeta *pMeta, char *tbname, STableMetaMsg **ppMsg) {
  DBT            key = {0};
  DBT            value = {0};
  SMetaDB *      pMetaDB = pMeta->pDB;
  int            ret;
  STbCfg         tbCfg;
  SSchemaKey     schemaKey;
  DBT            key1 = {0};
  DBT            value1 = {0};
  uint32_t       ncols;
  void *         pBuf;
  int            tlen;
  STableMetaMsg *pMsg;
H
more  
Hongze Cheng 已提交
447
  SSchema *      pSchema;
H
more  
Hongze Cheng 已提交
448 449 450 451

  key.data = tbname;
  key.size = strlen(tbname) + 1;

H
Hongze Cheng 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
  ret = pMetaDB->pNameIdx->get(pMetaDB->pNameIdx, NULL, &key, &value, 0);
  if (ret != 0) {
    // TODO
    return -1;
  }

  metaDecodeTbInfo(value.data, &tbCfg);

  switch (tbCfg.type) {
    case META_SUPER_TABLE:
      schemaKey.uid = tbCfg.stbCfg.suid;
      schemaKey.sver = 0;

      key1.data = &schemaKey;
      key1.size = sizeof(schemaKey);

      ret = pMetaDB->pSchemaDB->get(pMetaDB->pSchemaDB, &key1, &value1, NULL, 0);
      if (ret != 0) {
        // TODO
        return -1;
      }
      pBuf = value1.data;
      pBuf = taosDecodeFixedU32(pBuf, &ncols);

      tlen = sizeof(STableMetaMsg) + (tbCfg.stbCfg.nTagCols + ncols) * sizeof(SSchema);
      pMsg = calloc(1, tlen);
      if (pMsg == NULL) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        return -1;
      }

      strcpy(pMsg->tbFname, tbCfg.name);
      pMsg->numOfTags = tbCfg.stbCfg.nTagCols;
      pMsg->numOfColumns = ncols;
      pMsg->tableType = tbCfg.type;
      pMsg->sversion = 0;
      pMsg->tversion = 0;
      pMsg->suid = tbCfg.stbCfg.suid;
      pMsg->tuid = tbCfg.stbCfg.suid;
H
more  
Hongze Cheng 已提交
491 492 493
      memcpy(pMsg->pSchema, tbCfg.stbCfg.pSchema, sizeof(SSchema) * tbCfg.stbCfg.nCols);
      memcpy(POINTER_SHIFT(pMsg->pSchema, sizeof(SSchema) * tbCfg.stbCfg.nCols), tbCfg.stbCfg.pTagSchema,
              sizeof(SSchema) * tbCfg.stbCfg.nTagCols);
H
Hongze Cheng 已提交
494 495 496 497 498 499 500 501 502 503 504
      break;
    case META_CHILD_TABLE:
      ASSERT(0);
      break;
    case META_NORMAL_TABLE:
      ASSERT(0);
      break;
    default:
      ASSERT(0);
      break;
  }
H
more  
Hongze Cheng 已提交
505

H
Hongze Cheng 已提交
506
  *ppMsg = pMsg;
H
more  
Hongze Cheng 已提交
507 508

  return 0;
H
Hongze Cheng 已提交
509
}