metaTable.c 37.0 KB
Newer Older
H
more  
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
refact  
Hongze Cheng 已提交
14 15
 */

H
Hongze Cheng 已提交
16
#include "meta.h"
H
refact  
Hongze Cheng 已提交
17

dengyihao's avatar
dengyihao 已提交
18
static int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema);
19
static int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema);
H
Hongze Cheng 已提交
20 21 22 23 24 25
static int metaSaveToTbDb(SMeta *pMeta, const SMetaEntry *pME);
static int metaUpdateUidIdx(SMeta *pMeta, const SMetaEntry *pME);
static int metaUpdateNameIdx(SMeta *pMeta, const SMetaEntry *pME);
static int metaUpdateTtlIdx(SMeta *pMeta, const SMetaEntry *pME);
static int metaSaveToSkmDb(SMeta *pMeta, const SMetaEntry *pME);
static int metaUpdateCtbIdx(SMeta *pMeta, const SMetaEntry *pME);
C
Cary Xu 已提交
26
static int metaUpdateSuidIdx(SMeta *pMeta, const SMetaEntry *pME);
H
Hongze Cheng 已提交
27
static int metaUpdateTagIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry);
H
Hongze Cheng 已提交
28
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type);
H
Hongze Cheng 已提交
29

dengyihao's avatar
dengyihao 已提交
30
static int metaUpdateMetaRsp(tb_uid_t uid, char *tbName, SSchemaWrapper *pSchema, STableMetaRsp *pMetaRsp) {
D
dapan1121 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  pMetaRsp->pSchemas = taosMemoryMalloc(pSchema->nCols * sizeof(SSchema));
  if (NULL == pMetaRsp->pSchemas) {
    terrno = TSDB_CODE_VND_OUT_OF_MEMORY;
    return -1;
  }

  strcpy(pMetaRsp->tbName, tbName);
  pMetaRsp->numOfColumns = pSchema->nCols;
  pMetaRsp->tableType = TSDB_NORMAL_TABLE;
  pMetaRsp->sversion = pSchema->version;
  pMetaRsp->tuid = uid;

  memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));

  return 0;
}

dengyihao's avatar
dengyihao 已提交
48 49 50 51 52
static int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
#ifdef USE_INVERTED_INDEX
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
    return -1;
  }
H
Hongze Cheng 已提交
53
  void       *data = pCtbEntry->ctbEntry.pTags;
dengyihao's avatar
dengyihao 已提交
54 55 56 57 58 59 60 61 62 63 64
  const char *tagName = pSchema->name;

  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
  tb_uid_t    tuid = pCtbEntry->uid;
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
  int32_t     nTagData = 0;

  SArray *pTagVals = NULL;
  if (tTagToValArray((const STag *)data, &pTagVals) != 0) {
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
65

dengyihao's avatar
dengyihao 已提交
66 67 68 69 70
  SIndexMultiTerm *terms = indexMultiTermCreate();
  int16_t          nCols = taosArrayGetSize(pTagVals);
  for (int i = 0; i < nCols; i++) {
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
    char     type = pTagVal->type;
dengyihao's avatar
dengyihao 已提交
71

H
Hongze Cheng 已提交
72
    char   *key = pTagVal->pKey;
dengyihao's avatar
dengyihao 已提交
73
    int32_t nKey = strlen(key);
dengyihao's avatar
dengyihao 已提交
74 75 76

    SIndexTerm *term = NULL;
    if (type == TSDB_DATA_TYPE_NULL) {
dengyihao's avatar
dengyihao 已提交
77
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
dengyihao's avatar
dengyihao 已提交
78 79
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
      if (pTagVal->nData > 0) {
H
Hongze Cheng 已提交
80
        char   *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
dengyihao's avatar
dengyihao 已提交
81 82
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE);
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
dengyihao's avatar
dengyihao 已提交
83
        type = TSDB_DATA_TYPE_VARCHAR;
dengyihao's avatar
dengyihao 已提交
84
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
dengyihao's avatar
dengyihao 已提交
85
      } else if (pTagVal->nData == 0) {
dengyihao's avatar
dengyihao 已提交
86
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
dengyihao's avatar
dengyihao 已提交
87 88 89
      }
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
      double val = *(double *)(&pTagVal->i64);
dengyihao's avatar
dengyihao 已提交
90
      int    len = sizeof(val);
dengyihao's avatar
dengyihao 已提交
91 92 93
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
    } else if (type == TSDB_DATA_TYPE_BOOL) {
      int val = *(int *)(&pTagVal->i64);
dengyihao's avatar
dengyihao 已提交
94
      int len = sizeof(val);
dengyihao's avatar
dengyihao 已提交
95
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
dengyihao's avatar
dengyihao 已提交
96
    }
dengyihao's avatar
dengyihao 已提交
97
    if (term != NULL) {
dengyihao's avatar
dengyihao 已提交
98 99 100
      indexMultiTermAdd(terms, term);
    }
  }
dengyihao's avatar
dengyihao 已提交
101
  indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
dengyihao's avatar
dengyihao 已提交
102 103
  indexMultiTermDestroy(terms);
#endif
dengyihao's avatar
dengyihao 已提交
104
  return 0;
dengyihao's avatar
dengyihao 已提交
105
}
106 107 108 109 110
int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
#ifdef USE_INVERTED_INDEX
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
    return -1;
  }
111
  void       *data = pCtbEntry->ctbEntry.pTags;
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
  const char *tagName = pSchema->name;

  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
  tb_uid_t    tuid = pCtbEntry->uid;
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
  int32_t     nTagData = 0;

  SArray *pTagVals = NULL;
  if (tTagToValArray((const STag *)data, &pTagVals) != 0) {
    return -1;
  }

  SIndexMultiTerm *terms = indexMultiTermCreate();
  int16_t          nCols = taosArrayGetSize(pTagVals);
  for (int i = 0; i < nCols; i++) {
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
    char     type = pTagVal->type;

130
    char   *key = pTagVal->pKey;
131 132 133 134 135 136 137
    int32_t nKey = strlen(key);

    SIndexTerm *term = NULL;
    if (type == TSDB_DATA_TYPE_NULL) {
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
      if (pTagVal->nData > 0) {
138
        char   *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE);
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
        type = TSDB_DATA_TYPE_VARCHAR;
        term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, val, len);
      } else if (pTagVal->nData == 0) {
        term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
      }
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
      double val = *(double *)(&pTagVal->i64);
      int    len = sizeof(val);
      term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, (const char *)&val, len);
    } else if (type == TSDB_DATA_TYPE_BOOL) {
      int val = *(int *)(&pTagVal->i64);
      int len = sizeof(val);
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
dengyihao's avatar
dengyihao 已提交
154
    }
dengyihao's avatar
dengyihao 已提交
155
    if (term != NULL) {
dengyihao's avatar
dengyihao 已提交
156 157 158
      indexMultiTermAdd(terms, term);
    }
  }
dengyihao's avatar
dengyihao 已提交
159
  indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
dengyihao's avatar
dengyihao 已提交
160 161
  indexMultiTermDestroy(terms);
#endif
dengyihao's avatar
dengyihao 已提交
162
  return 0;
dengyihao's avatar
dengyihao 已提交
163 164
}

H
Hongze Cheng 已提交
165
int metaCreateSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
H
Hongze Cheng 已提交
166
  SMetaEntry  me = {0};
H
Hongze Cheng 已提交
167 168 169 170
  int         kLen = 0;
  int         vLen = 0;
  const void *pKey = NULL;
  const void *pVal = NULL;
H
Hongze Cheng 已提交
171
  void       *pBuf = NULL;
H
Hongze Cheng 已提交
172
  int32_t     szBuf = 0;
H
Hongze Cheng 已提交
173
  void       *p = NULL;
H
Hongze Cheng 已提交
174 175 176
  SMetaReader mr = {0};

  // validate req
H
Hongze Cheng 已提交
177
  metaReaderInit(&mr, pMeta, 0);
H
Hongze Cheng 已提交
178
  if (metaGetTableEntryByName(&mr, pReq->name) == 0) {
H
Hongze Cheng 已提交
179 180
// TODO: just for pass case
#if 0
181
    terrno = TSDB_CODE_TDB_STB_ALREADY_EXIST;
H
Hongze Cheng 已提交
182 183
    metaReaderClear(&mr);
    return -1;
H
Hongze Cheng 已提交
184 185 186 187
#else
    metaReaderClear(&mr);
    return 0;
#endif
H
Hongze Cheng 已提交
188
  }
H
Hongze Cheng 已提交
189
  metaReaderClear(&mr);
H
Hongze Cheng 已提交
190 191

  // set structs
H
Hongze Cheng 已提交
192
  me.version = version;
H
Hongze Cheng 已提交
193 194 195
  me.type = TSDB_SUPER_TABLE;
  me.uid = pReq->suid;
  me.name = pReq->name;
196
  me.stbEntry.schemaRow = pReq->schemaRow;
H
Hongze Cheng 已提交
197
  me.stbEntry.schemaTag = pReq->schemaTag;
C
Cary Xu 已提交
198 199 200 201
  if (pReq->rollup) {
    TABLE_SET_ROLLUP(me.flags);
    me.stbEntry.rsmaParam = pReq->rsmaParam;
  }
H
Hongze Cheng 已提交
202

H
Hongze Cheng 已提交
203
  if (metaHandleEntry(pMeta, &me) < 0) goto _err;
H
Hongze Cheng 已提交
204

205 206
  ++pMeta->pVnode->config.vndStats.numOfSTables;

S
Shengliang Guan 已提交
207
  metaDebug("vgId:%d, super table is created, name:%s uid: %" PRId64, TD_VID(pMeta->pVnode), pReq->name, pReq->suid);
H
Hongze Cheng 已提交
208 209 210 211

  return 0;

_err:
S
Shengliang Guan 已提交
212
  metaError("vgId:%d, failed to create super table: %s uid: %" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
H
Hongze Cheng 已提交
213 214 215 216
            pReq->suid, tstrerror(terrno));
  return -1;
}

217
int metaDropSTable(SMeta *pMeta, int64_t verison, SVDropStbReq *pReq, SArray *tbUidList) {
H
Hongze Cheng 已提交
218 219 220 221 222 223 224 225 226 227
  void *pKey = NULL;
  int   nKey = 0;
  void *pData = NULL;
  int   nData = 0;
  int   c = 0;
  int   rc = 0;

  // check if super table exists
  rc = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &pData, &nData);
  if (rc < 0 || *(tb_uid_t *)pData != pReq->suid) {
228
    terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
H
Hongze Cheng 已提交
229
    return -1;
H
Hongze Cheng 已提交
230 231
  }

H
Hongze Cheng 已提交
232
  // drop all child tables
233
  TBC *pCtbIdxc = NULL;
H
Hongze Cheng 已提交
234

H
Hongze Cheng 已提交
235
  tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, &pMeta->txn);
H
Hongze Cheng 已提交
236 237
  rc = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = pReq->suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c);
  if (rc < 0) {
H
Hongze Cheng 已提交
238
    tdbTbcClose(pCtbIdxc);
H
Hongze Cheng 已提交
239 240
    metaWLock(pMeta);
    goto _drop_super_table;
H
Hongze Cheng 已提交
241 242 243
  }

  for (;;) {
H
Hongze Cheng 已提交
244 245
    rc = tdbTbcNext(pCtbIdxc, &pKey, &nKey, NULL, NULL);
    if (rc < 0) break;
H
Hongze Cheng 已提交
246

H
Hongze Cheng 已提交
247 248 249 250 251
    if (((SCtbIdxKey *)pKey)->suid < pReq->suid) {
      continue;
    } else if (((SCtbIdxKey *)pKey)->suid > pReq->suid) {
      break;
    }
H
Hongze Cheng 已提交
252

253
    taosArrayPush(tbUidList, &(((SCtbIdxKey *)pKey)->uid));
H
Hongze Cheng 已提交
254 255 256 257 258
  }

  tdbTbcClose(pCtbIdxc);

  metaWLock(pMeta);
H
Hongze Cheng 已提交
259

260 261
  for (int32_t iChild = 0; iChild < taosArrayGetSize(tbUidList); iChild++) {
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUidList, iChild);
H
Hongze Cheng 已提交
262
    metaDropTableByUid(pMeta, uid, NULL);
H
Hongze Cheng 已提交
263 264
  }

H
Hongze Cheng 已提交
265 266 267 268 269 270 271
  // drop super table
_drop_super_table:
  tdbTbGet(pMeta->pUidIdx, &pReq->suid, sizeof(tb_uid_t), &pData, &nData);
  tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = *(int64_t *)pData, .uid = pReq->suid}, sizeof(STbDbKey),
              &pMeta->txn);
  tdbTbDelete(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &pMeta->txn);
  tdbTbDelete(pMeta->pUidIdx, &pReq->suid, sizeof(tb_uid_t), &pMeta->txn);
C
Cary Xu 已提交
272
  tdbTbDelete(pMeta->pSuidIdx, &pReq->suid, sizeof(tb_uid_t), &pMeta->txn);
H
Hongze Cheng 已提交
273 274 275

  metaULock(pMeta);

H
Hongze Cheng 已提交
276
_exit:
H
Hongze Cheng 已提交
277 278
  tdbFree(pKey);
  tdbFree(pData);
C
Cary Xu 已提交
279
  metaDebug("vgId:%d, super table %s uid:%" PRId64 " is dropped", TD_VID(pMeta->pVnode), pReq->name, pReq->suid);
H
Hongze Cheng 已提交
280 281 282
  return 0;
}

H
Hongze Cheng 已提交
283 284 285
int metaAlterSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
  SMetaEntry  oStbEntry = {0};
  SMetaEntry  nStbEntry = {0};
H
Hongze Cheng 已提交
286 287
  TBC        *pUidIdxc = NULL;
  TBC        *pTbDbc = NULL;
H
Hongze Cheng 已提交
288 289 290 291 292 293 294
  const void *pData;
  int         nData;
  int64_t     oversion;
  SDecoder    dc = {0};
  int32_t     ret;
  int32_t     c;

H
Hongze Cheng 已提交
295 296
  tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, &pMeta->txn);
  ret = tdbTbcMoveTo(pUidIdxc, &pReq->suid, sizeof(tb_uid_t), &c);
H
Hongze Cheng 已提交
297
  if (ret < 0 || c) {
298 299 300 301
    tdbTbcClose(pUidIdxc);

    terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
    // ASSERT(0);
H
Hongze Cheng 已提交
302 303 304
    return -1;
  }

H
Hongze Cheng 已提交
305
  ret = tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
306 307 308 309 310 311 312
  if (ret < 0) {
    ASSERT(0);
    return -1;
  }

  oversion = *(int64_t *)pData;

H
Hongze Cheng 已提交
313 314
  tdbTbcOpen(pMeta->pTbDb, &pTbDbc, &pMeta->txn);
  ret = tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = pReq->suid, .version = oversion}), sizeof(STbDbKey), &c);
H
Hongze Cheng 已提交
315 316
  ASSERT(ret == 0 && c == 0);

H
Hongze Cheng 已提交
317
  ret = tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
318 319
  ASSERT(ret == 0);

H
Hongze Cheng 已提交
320 321 322
  oStbEntry.pBuf = taosMemoryMalloc(nData);
  memcpy(oStbEntry.pBuf, pData, nData);
  tDecoderInit(&dc, oStbEntry.pBuf, nData);
H
Hongze Cheng 已提交
323 324 325 326 327 328
  metaDecodeEntry(&dc, &oStbEntry);

  nStbEntry.version = version;
  nStbEntry.type = TSDB_SUPER_TABLE;
  nStbEntry.uid = pReq->suid;
  nStbEntry.name = pReq->name;
329
  nStbEntry.stbEntry.schemaRow = pReq->schemaRow;
H
Hongze Cheng 已提交
330 331 332 333
  nStbEntry.stbEntry.schemaTag = pReq->schemaTag;

  metaWLock(pMeta);
  // compare two entry
334 335
  if (oStbEntry.stbEntry.schemaRow.version != pReq->schemaRow.version) {
    metaSaveToSkmDb(pMeta, &nStbEntry);
H
Hongze Cheng 已提交
336 337 338 339 340 341 342 343 344 345
  }

  // if (oStbEntry.stbEntry.schemaTag.sver != pReq->schemaTag.sver) {
  //   // change tag schema
  // }

  // update table.db
  metaSaveToTbDb(pMeta, &nStbEntry);

  // update uid index
H
Hongze Cheng 已提交
346
  tdbTbcUpsert(pUidIdxc, &pReq->suid, sizeof(tb_uid_t), &version, sizeof(version), 0);
H
Hongze Cheng 已提交
347

H
Hongze Cheng 已提交
348
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
H
Hongze Cheng 已提交
349 350
  metaULock(pMeta);
  tDecoderClear(&dc);
H
Hongze Cheng 已提交
351 352
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
H
Hongze Cheng 已提交
353 354 355
  return 0;
}

H
Hongze Cheng 已提交
356
int metaCreateTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
H
Hongze Cheng 已提交
357 358
  SMetaEntry  me = {0};
  SMetaReader mr = {0};
H
Hongze Cheng 已提交
359 360 361 362 363

  // validate message
  if (pReq->type != TSDB_CHILD_TABLE && pReq->type != TSDB_NORMAL_TABLE) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto _err;
H
more  
Hongze Cheng 已提交
364 365
  }

366 367 368 369 370 371 372 373
  if (pReq->type == TSDB_CHILD_TABLE) {
    tb_uid_t suid = metaGetTableEntryUidByName(pMeta, pReq->ctb.name);
    if (suid != pReq->ctb.suid) {
      terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
      return -1;
    }
  }

H
Hongze Cheng 已提交
374
  // validate req
H
Hongze Cheng 已提交
375
  metaReaderInit(&mr, pMeta, 0);
H
Hongze Cheng 已提交
376
  if (metaGetTableEntryByName(&mr, pReq->name) == 0) {
H
Hongze Cheng 已提交
377 378 379 380
    pReq->uid = mr.me.uid;
    if (pReq->type == TSDB_CHILD_TABLE) {
      pReq->ctb.suid = mr.me.ctbEntry.suid;
    }
H
Hongze Cheng 已提交
381 382 383
    terrno = TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
    metaReaderClear(&mr);
    return -1;
H
Hongze Cheng 已提交
384
  }
H
Hongze Cheng 已提交
385
  metaReaderClear(&mr);
H
Hongze Cheng 已提交
386 387

  // build SMetaEntry
H
Hongze Cheng 已提交
388
  me.version = version;
H
Hongze Cheng 已提交
389 390 391 392 393 394
  me.type = pReq->type;
  me.uid = pReq->uid;
  me.name = pReq->name;
  if (me.type == TSDB_CHILD_TABLE) {
    me.ctbEntry.ctime = pReq->ctime;
    me.ctbEntry.ttlDays = pReq->ttl;
wmmhello's avatar
wmmhello 已提交
395
    me.ctbEntry.commentLen = pReq->commentLen;
wmmhello's avatar
wmmhello 已提交
396
    me.ctbEntry.comment = pReq->comment;
H
Hongze Cheng 已提交
397 398
    me.ctbEntry.suid = pReq->ctb.suid;
    me.ctbEntry.pTags = pReq->ctb.pTag;
399 400

    ++pMeta->pVnode->config.vndStats.numOfCTables;
H
Hongze Cheng 已提交
401 402 403
  } else {
    me.ntbEntry.ctime = pReq->ctime;
    me.ntbEntry.ttlDays = pReq->ttl;
wmmhello's avatar
wmmhello 已提交
404
    me.ntbEntry.commentLen = pReq->commentLen;
wmmhello's avatar
wmmhello 已提交
405
    me.ntbEntry.comment = pReq->comment;
406 407
    me.ntbEntry.schemaRow = pReq->ntb.schemaRow;
    me.ntbEntry.ncid = me.ntbEntry.schemaRow.pSchema[me.ntbEntry.schemaRow.nCols - 1].colId + 1;
408 409

    ++pMeta->pVnode->config.vndStats.numOfNTables;
H
more  
Hongze Cheng 已提交
410 411
  }

H
Hongze Cheng 已提交
412
  if (metaHandleEntry(pMeta, &me) < 0) goto _err;
H
Hongze Cheng 已提交
413

S
Shengliang Guan 已提交
414
  metaDebug("vgId:%d, table %s uid %" PRId64 " is created, type:%" PRId8, TD_VID(pMeta->pVnode), pReq->name, pReq->uid,
H
Hongze Cheng 已提交
415
            pReq->type);
H
refact  
Hongze Cheng 已提交
416
  return 0;
H
Hongze Cheng 已提交
417 418

_err:
S
Shengliang Guan 已提交
419
  metaError("vgId:%d, failed to create table:%s type:%s since %s", TD_VID(pMeta->pVnode), pReq->name,
H
Hongze Cheng 已提交
420 421
            pReq->type == TSDB_CHILD_TABLE ? "child table" : "normal table", tstrerror(terrno));
  return -1;
H
refact  
Hongze Cheng 已提交
422 423
}

424
int metaDropTable(SMeta *pMeta, int64_t version, SVDropTbReq *pReq, SArray *tbUids) {
H
Hongze Cheng 已提交
425
  void    *pData = NULL;
H
Hongze Cheng 已提交
426 427 428 429
  int      nData = 0;
  int      rc = 0;
  tb_uid_t uid;
  int      type;
H
more  
Hongze Cheng 已提交
430

H
Hongze Cheng 已提交
431 432 433
  rc = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &pData, &nData);
  if (rc < 0) {
    terrno = TSDB_CODE_VND_TABLE_NOT_EXIST;
H
more  
Hongze Cheng 已提交
434 435
    return -1;
  }
H
Hongze Cheng 已提交
436 437
  uid = *(tb_uid_t *)pData;

H
Hongze Cheng 已提交
438 439 440
  metaWLock(pMeta);
  metaDropTableByUid(pMeta, uid, &type);
  metaULock(pMeta);
H
Hongze Cheng 已提交
441

442
  if ((type == TSDB_CHILD_TABLE || type == TSDB_NORMAL_TABLE) && tbUids) {
H
Hongze Cheng 已提交
443
    taosArrayPush(tbUids, &uid);
H
Hongze Cheng 已提交
444
  }
H
Hongze Cheng 已提交
445

H
Hongze Cheng 已提交
446 447 448
  tdbFree(pData);
  return 0;
}
H
Hongze Cheng 已提交
449

450 451
int metaTtlDropTable(SMeta *pMeta, int64_t ttl, SArray *tbUids) {
  int ret = metaTtlSmaller(pMeta, ttl, tbUids);
H
Hongze Cheng 已提交
452
  if (ret != 0) {
453 454
    return ret;
  }
455
  if (taosArrayGetSize(tbUids) == 0) {
456 457 458 459
    return 0;
  }

  metaWLock(pMeta);
460 461 462
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
    tb_uid_t *uid = (tb_uid_t *)taosArrayGet(tbUids, i);
    metaDropTableByUid(pMeta, *uid, NULL);
H
Hongze Cheng 已提交
463
    metaDebug("ttl drop table:%" PRId64, *uid);
464 465 466 467 468
  }
  metaULock(pMeta);
  return 0;
}

H
Hongze Cheng 已提交
469 470 471
static void metaBuildTtlIdxKey(STtlIdxKey *ttlKey, const SMetaEntry *pME) {
  int64_t ttlDays;
  int64_t ctime;
472 473 474 475 476 477 478 479 480 481 482 483
  if (pME->type == TSDB_CHILD_TABLE) {
    ctime = pME->ctbEntry.ctime;
    ttlDays = pME->ctbEntry.ttlDays;
  } else if (pME->type == TSDB_NORMAL_TABLE) {
    ctime = pME->ntbEntry.ctime;
    ttlDays = pME->ntbEntry.ttlDays;
  } else {
    ASSERT(0);
  }

  if (ttlDays <= 0) return;

wmmhello's avatar
wmmhello 已提交
484
  ttlKey->dtime = ctime / 1000 + ttlDays * tsTtlUnit;
485 486 487 488 489 490
  ttlKey->uid = pME->uid;
}

static int metaDeleteTtlIdx(SMeta *pMeta, const SMetaEntry *pME) {
  STtlIdxKey ttlKey = {0};
  metaBuildTtlIdxKey(&ttlKey, pME);
H
Hongze Cheng 已提交
491
  if (ttlKey.dtime == 0) return 0;
492 493 494
  return tdbTbDelete(pMeta->pTtlIdx, &ttlKey, sizeof(ttlKey), &pMeta->txn);
}

H
Hongze Cheng 已提交
495
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type) {
H
Hongze Cheng 已提交
496
  void      *pData = NULL;
H
Hongze Cheng 已提交
497 498 499 500 501 502
  int        nData = 0;
  int        rc = 0;
  SMetaEntry e = {0};
  SDecoder   dc = {0};

  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
wmmhello's avatar
wmmhello 已提交
503
  int64_t version = *(int64_t *)pData;
H
Hongze Cheng 已提交
504 505 506 507 508 509 510 511

  tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);

  tDecoderInit(&dc, pData, nData);
  metaDecodeEntry(&dc, &e);

  if (type) *type = e.type;

512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
  if (e.type == TSDB_CHILD_TABLE) {
    void *tData = NULL;
    int   tLen = 0;

    if (tdbTbGet(pMeta->pUidIdx, &e.ctbEntry.suid, sizeof(tb_uid_t), &tData, &tLen) == 0) {
      version = *(int64_t *)tData;
      STbDbKey tbDbKey = {.uid = e.ctbEntry.suid, .version = version};
      if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &tData, &tLen) == 0) {
        SDecoder   tdc = {0};
        SMetaEntry stbEntry = {0};

        tDecoderInit(&tdc, tData, tLen);
        metaDecodeEntry(&tdc, &stbEntry);
        const SSchema *pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
        if (pTagColumn->type == TSDB_DATA_TYPE_JSON) {
          metaDelJsonVarFromIdx(pMeta, &e, pTagColumn);
        }
        tDecoderClear(&tdc);
      }
      tdbFree(tData);
    }
  }

H
Hongze Cheng 已提交
535 536 537
  tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pMeta->txn);
  tdbTbDelete(pMeta->pNameIdx, e.name, strlen(e.name) + 1, &pMeta->txn);
  tdbTbDelete(pMeta->pUidIdx, &uid, sizeof(uid), &pMeta->txn);
538

H
Hongze Cheng 已提交
539
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtlIdx(pMeta, &e);
C
Cary Xu 已提交
540

H
Hongze Cheng 已提交
541 542
  if (e.type == TSDB_CHILD_TABLE) {
    tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), &pMeta->txn);
543 544

    --pMeta->pVnode->config.vndStats.numOfCTables;
H
Hongze Cheng 已提交
545 546
  } else if (e.type == TSDB_NORMAL_TABLE) {
    // drop schema.db (todo)
547 548

    --pMeta->pVnode->config.vndStats.numOfNTables;
H
Hongze Cheng 已提交
549
  } else if (e.type == TSDB_SUPER_TABLE) {
C
Cary Xu 已提交
550
    tdbTbDelete(pMeta->pSuidIdx, &e.uid, sizeof(tb_uid_t), &pMeta->txn);
H
Hongze Cheng 已提交
551
    // drop schema.db (todo)
552 553

    --pMeta->pVnode->config.vndStats.numOfSTables;
H
Hongze Cheng 已提交
554 555
  }

H
Hongze Cheng 已提交
556 557
  tDecoderClear(&dc);
  tdbFree(pData);
H
Hongze Cheng 已提交
558

H
refact  
Hongze Cheng 已提交
559 560
  return 0;
}
H
Hongze Cheng 已提交
561

D
dapan1121 已提交
562
static int metaAlterTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq, STableMetaRsp *pMetaRsp) {
H
Hongze Cheng 已提交
563
  void           *pVal = NULL;
H
Hongze Cheng 已提交
564
  int             nVal = 0;
H
Hongze Cheng 已提交
565
  const void     *pData = NULL;
H
Hongze Cheng 已提交
566 567 568 569
  int             nData = 0;
  int             ret = 0;
  tb_uid_t        uid;
  int64_t         oversion;
H
Hongze Cheng 已提交
570
  SSchema        *pColumn = NULL;
H
Hongze Cheng 已提交
571 572 573 574 575
  SMetaEntry      entry = {0};
  SSchemaWrapper *pSchema;
  int             c;

  // search name index
H
Hongze Cheng 已提交
576
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
H
Hongze Cheng 已提交
577 578 579 580 581 582 583 584 585 586
  if (ret < 0) {
    terrno = TSDB_CODE_VND_TABLE_NOT_EXIST;
    return -1;
  }

  uid = *(tb_uid_t *)pVal;
  tdbFree(pVal);
  pVal = NULL;

  // search uid index
H
Hongze Cheng 已提交
587
  TBC *pUidIdxc = NULL;
H
Hongze Cheng 已提交
588

H
Hongze Cheng 已提交
589 590
  tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, &pMeta->txn);
  tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c);
H
Hongze Cheng 已提交
591 592
  ASSERT(c == 0);

H
Hongze Cheng 已提交
593
  tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
594 595 596
  oversion = *(int64_t *)pData;

  // search table.db
H
Hongze Cheng 已提交
597
  TBC *pTbDbc = NULL;
H
Hongze Cheng 已提交
598

H
Hongze Cheng 已提交
599 600
  tdbTbcOpen(pMeta->pTbDb, &pTbDbc, &pMeta->txn);
  tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c);
H
Hongze Cheng 已提交
601
  ASSERT(c == 0);
H
Hongze Cheng 已提交
602
  tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
603 604 605

  // get table entry
  SDecoder dc = {0};
H
Hongze Cheng 已提交
606 607 608
  entry.pBuf = taosMemoryMalloc(nData);
  memcpy(entry.pBuf, pData, nData);
  tDecoderInit(&dc, entry.pBuf, nData);
H
Hongze Cheng 已提交
609 610
  ret = metaDecodeEntry(&dc, &entry);
  ASSERT(ret == 0);
H
Hongze Cheng 已提交
611 612 613 614 615 616 617

  if (entry.type != TSDB_NORMAL_TABLE) {
    terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
    goto _err;
  }

  // search the column to add/drop/update
618
  pSchema = &entry.ntbEntry.schemaRow;
H
Hongze Cheng 已提交
619 620 621 622 623 624 625 626 627 628 629 630
  int32_t iCol = 0;
  for (;;) {
    pColumn = NULL;

    if (iCol >= pSchema->nCols) break;
    pColumn = &pSchema->pSchema[iCol];

    if (strcmp(pColumn->name, pAlterTbReq->colName) == 0) break;
    iCol++;
  }

  entry.version = version;
H
Hongze Cheng 已提交
631 632
  int      tlen;
  SSchema *pNewSchema = NULL;
H
Hongze Cheng 已提交
633 634 635 636 637 638
  switch (pAlterTbReq->action) {
    case TSDB_ALTER_TABLE_ADD_COLUMN:
      if (pColumn) {
        terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
        goto _err;
      }
639
      pSchema->version++;
H
Hongze Cheng 已提交
640
      pSchema->nCols++;
H
Hongze Cheng 已提交
641 642 643
      pNewSchema = taosMemoryMalloc(sizeof(SSchema) * pSchema->nCols);
      memcpy(pNewSchema, pSchema->pSchema, sizeof(SSchema) * (pSchema->nCols - 1));
      pSchema->pSchema = pNewSchema;
644 645 646 647 648
      pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].bytes = pAlterTbReq->bytes;
      pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].type = pAlterTbReq->type;
      pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].flags = pAlterTbReq->flags;
      pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].colId = entry.ntbEntry.ncid++;
      strcpy(pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].name, pAlterTbReq->colName);
H
Hongze Cheng 已提交
649 650 651 652 653 654 655 656 657 658
      break;
    case TSDB_ALTER_TABLE_DROP_COLUMN:
      if (pColumn == NULL) {
        terrno = TSDB_CODE_VND_TABLE_COL_NOT_EXISTS;
        goto _err;
      }
      if (pColumn->colId == 0) {
        terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
        goto _err;
      }
L
Liu Jicong 已提交
659 660 661 662
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
        goto _err;
      }
663
      pSchema->version++;
H
Hongze Cheng 已提交
664 665 666 667
      tlen = (pSchema->nCols - iCol - 1) * sizeof(SSchema);
      if (tlen) {
        memmove(pColumn, pColumn + 1, tlen);
      }
H
Hongze Cheng 已提交
668
      pSchema->nCols--;
H
Hongze Cheng 已提交
669 670 671 672 673 674
      break;
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
      if (pColumn == NULL) {
        terrno = TSDB_CODE_VND_TABLE_COL_NOT_EXISTS;
        goto _err;
      }
H
Hongze Cheng 已提交
675
      if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes > pAlterTbReq->colModBytes) {
H
Hongze Cheng 已提交
676 677 678
        terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
        goto _err;
      }
L
Liu Jicong 已提交
679 680 681 682
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
        goto _err;
      }
683
      pSchema->version++;
H
Hongze Cheng 已提交
684
      pColumn->bytes = pAlterTbReq->colModBytes;
H
Hongze Cheng 已提交
685 686 687 688 689 690
      break;
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
      if (pColumn == NULL) {
        terrno = TSDB_CODE_VND_TABLE_COL_NOT_EXISTS;
        goto _err;
      }
L
Liu Jicong 已提交
691 692 693 694
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
        goto _err;
      }
695
      pSchema->version++;
H
Hongze Cheng 已提交
696 697 698 699 700 701
      strcpy(pColumn->name, pAlterTbReq->colNewName);
      break;
  }

  entry.version = version;

H
Hongze Cheng 已提交
702 703 704 705 706 707
  // do actual write
  metaWLock(pMeta);

  // save to table db
  metaSaveToTbDb(pMeta, &entry);

H
Hongze Cheng 已提交
708
  tdbTbcUpsert(pUidIdxc, &entry.uid, sizeof(tb_uid_t), &version, sizeof(version), 0);
H
Hongze Cheng 已提交
709 710 711 712 713

  metaSaveToSkmDb(pMeta, &entry);

  metaULock(pMeta);

D
dapan1121 已提交
714 715
  metaUpdateMetaRsp(uid, pAlterTbReq->tbName, pSchema, pMetaRsp);

wmmhello's avatar
wmmhello 已提交
716
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
H
Hongze Cheng 已提交
717
  if (pNewSchema) taosMemoryFree(pNewSchema);
H
Hongze Cheng 已提交
718 719
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
720 721
  tDecoderClear(&dc);

H
Hongze Cheng 已提交
722
  return 0;
H
Hongze Cheng 已提交
723 724

_err:
wmmhello's avatar
wmmhello 已提交
725
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
H
Hongze Cheng 已提交
726 727
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
728 729
  tDecoderClear(&dc);

H
Hongze Cheng 已提交
730
  return -1;
H
Hongze Cheng 已提交
731 732 733
}

static int metaUpdateTableTagVal(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
H
Hongze Cheng 已提交
734 735
  SMetaEntry  ctbEntry = {0};
  SMetaEntry  stbEntry = {0};
H
Hongze Cheng 已提交
736
  void       *pVal = NULL;
H
Hongze Cheng 已提交
737 738 739 740 741 742 743 744 745
  int         nVal = 0;
  int         ret;
  int         c;
  tb_uid_t    uid;
  int64_t     oversion;
  const void *pData = NULL;
  int         nData = 0;

  // search name index
H
Hongze Cheng 已提交
746
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
H
Hongze Cheng 已提交
747 748 749 750 751 752 753 754 755 756
  if (ret < 0) {
    terrno = TSDB_CODE_VND_TABLE_NOT_EXIST;
    return -1;
  }

  uid = *(tb_uid_t *)pVal;
  tdbFree(pVal);
  pVal = NULL;

  // search uid index
H
Hongze Cheng 已提交
757
  TBC *pUidIdxc = NULL;
H
Hongze Cheng 已提交
758

H
Hongze Cheng 已提交
759 760
  tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, &pMeta->txn);
  tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c);
H
Hongze Cheng 已提交
761 762
  ASSERT(c == 0);

H
Hongze Cheng 已提交
763
  tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
764 765 766
  oversion = *(int64_t *)pData;

  // search table.db
H
Hongze Cheng 已提交
767
  TBC     *pTbDbc = NULL;
H
Hongze Cheng 已提交
768 769
  SDecoder dc1 = {0};
  SDecoder dc2 = {0};
H
Hongze Cheng 已提交
770

H
Hongze Cheng 已提交
771
  /* get ctbEntry */
H
Hongze Cheng 已提交
772 773
  tdbTbcOpen(pMeta->pTbDb, &pTbDbc, &pMeta->txn);
  tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c);
H
Hongze Cheng 已提交
774
  ASSERT(c == 0);
H
Hongze Cheng 已提交
775
  tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
776

H
Hongze Cheng 已提交
777 778
  ctbEntry.pBuf = taosMemoryMalloc(nData);
  memcpy(ctbEntry.pBuf, pData, nData);
H
Hongze Cheng 已提交
779 780
  tDecoderInit(&dc1, ctbEntry.pBuf, nData);
  metaDecodeEntry(&dc1, &ctbEntry);
H
Hongze Cheng 已提交
781

H
Hongze Cheng 已提交
782
  /* get stbEntry*/
H
Hongze Cheng 已提交
783 784 785
  tdbTbGet(pMeta->pUidIdx, &ctbEntry.ctbEntry.suid, sizeof(tb_uid_t), &pVal, &nVal);
  tdbTbGet(pMeta->pTbDb, &((STbDbKey){.uid = ctbEntry.ctbEntry.suid, .version = *(int64_t *)pVal}), sizeof(STbDbKey),
           (void **)&stbEntry.pBuf, &nVal);
H
Hongze Cheng 已提交
786
  tdbFree(pVal);
H
Hongze Cheng 已提交
787 788
  tDecoderInit(&dc2, stbEntry.pBuf, nVal);
  metaDecodeEntry(&dc2, &stbEntry);
H
Hongze Cheng 已提交
789 790

  SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
H
Hongze Cheng 已提交
791
  SSchema        *pColumn = NULL;
H
Hongze Cheng 已提交
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
  int32_t         iCol = 0;
  for (;;) {
    pColumn = NULL;

    if (iCol >= pTagSchema->nCols) break;
    pColumn = &pTagSchema->pSchema[iCol];

    if (strcmp(pColumn->name, pAlterTbReq->tagName) == 0) break;
    iCol++;
  }

  if (pColumn == NULL) {
    terrno = TSDB_CODE_VND_TABLE_COL_NOT_EXISTS;
    goto _err;
  }
H
Hongze Cheng 已提交
807

H
Hongze Cheng 已提交
808
  ctbEntry.version = version;
H
Hongze Cheng 已提交
809
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
wmmhello's avatar
wmmhello 已提交
810
    ctbEntry.ctbEntry.pTags = taosMemoryMalloc(pAlterTbReq->nTagVal);
H
Hongze Cheng 已提交
811
    if (ctbEntry.ctbEntry.pTags == NULL) {
wmmhello's avatar
wmmhello 已提交
812 813 814
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
815 816
    memcpy((void *)ctbEntry.ctbEntry.pTags, pAlterTbReq->pTagVal, pAlterTbReq->nTagVal);
  } else {
C
Cary Xu 已提交
817
    const STag *pOldTag = (const STag *)ctbEntry.ctbEntry.pTags;
H
Hongze Cheng 已提交
818 819
    STag       *pNewTag = NULL;
    SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
C
Cary Xu 已提交
820
    if (!pTagArray) {
C
Cary Xu 已提交
821 822 823
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
wmmhello's avatar
wmmhello 已提交
824 825 826
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
      SSchema *pCol = &pTagSchema->pSchema[i];
      if (iCol == i) {
827 828 829
        if (pAlterTbReq->isNull) {
          continue;
        }
wmmhello's avatar
wmmhello 已提交
830 831 832 833 834 835
        STagVal val = {0};
        val.type = pCol->type;
        val.cid = pCol->colId;
        if (IS_VAR_DATA_TYPE(pCol->type)) {
          val.pData = pAlterTbReq->pTagVal;
          val.nData = pAlterTbReq->nTagVal;
836
        } else {
wmmhello's avatar
wmmhello 已提交
837 838 839
          memcpy(&val.i64, pAlterTbReq->pTagVal, pAlterTbReq->nTagVal);
        }
        taosArrayPush(pTagArray, &val);
wmmhello's avatar
wmmhello 已提交
840
      } else {
wmmhello's avatar
wmmhello 已提交
841
        STagVal val = {.cid = pCol->colId};
wmmhello's avatar
wmmhello 已提交
842 843
        if (tTagGet(pOldTag, &val)) {
          taosArrayPush(pTagArray, &val);
H
Hongze Cheng 已提交
844 845 846
        }
      }
    }
C
Cary Xu 已提交
847 848
    if ((terrno = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag)) < 0) {
      taosArrayDestroy(pTagArray);
C
Cary Xu 已提交
849 850 851
      goto _err;
    }
    ctbEntry.ctbEntry.pTags = (uint8_t *)pNewTag;
C
Cary Xu 已提交
852
    taosArrayDestroy(pTagArray);
wmmhello's avatar
wmmhello 已提交
853
  }
H
Hongze Cheng 已提交
854 855 856 857 858

  // save to table.db
  metaSaveToTbDb(pMeta, &ctbEntry);

  // save to uid.idx
H
Hongze Cheng 已提交
859
  tdbTbUpsert(pMeta->pUidIdx, &ctbEntry.uid, sizeof(tb_uid_t), &version, sizeof(version), &pMeta->txn);
H
Hongze Cheng 已提交
860

dengyihao's avatar
dengyihao 已提交
861 862 863 864
  if (iCol == 0) {
    metaUpdateTagIdx(pMeta, &ctbEntry);
  }

H
Hongze Cheng 已提交
865 866
  tDecoderClear(&dc1);
  tDecoderClear(&dc2);
H
Hongze Cheng 已提交
867
  if (ctbEntry.ctbEntry.pTags) taosMemoryFree((void *)ctbEntry.ctbEntry.pTags);
H
Hongze Cheng 已提交
868 869
  if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
  if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
H
Hongze Cheng 已提交
870 871
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
H
Hongze Cheng 已提交
872
  return 0;
H
Hongze Cheng 已提交
873 874

_err:
H
Hongze Cheng 已提交
875 876
  tDecoderClear(&dc1);
  tDecoderClear(&dc2);
H
Hongze Cheng 已提交
877 878
  if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
  if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
H
Hongze Cheng 已提交
879 880
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
H
Hongze Cheng 已提交
881
  return -1;
H
Hongze Cheng 已提交
882 883 884
}

static int metaUpdateTableOptions(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
H
Hongze Cheng 已提交
885 886 887 888 889 890 891 892 893
  void       *pVal = NULL;
  int         nVal = 0;
  const void *pData = NULL;
  int         nData = 0;
  int         ret = 0;
  tb_uid_t    uid;
  int64_t     oversion;
  SMetaEntry  entry = {0};
  int         c = 0;
wmmhello's avatar
wmmhello 已提交
894 895 896 897 898 899

  // search name index
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
  if (ret < 0) {
    terrno = TSDB_CODE_VND_TABLE_NOT_EXIST;
    return -1;
900
  }
wmmhello's avatar
wmmhello 已提交
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935

  uid = *(tb_uid_t *)pVal;
  tdbFree(pVal);
  pVal = NULL;

  // search uid index
  TBC *pUidIdxc = NULL;

  tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, &pMeta->txn);
  tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c);
  ASSERT(c == 0);

  tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
  oversion = *(int64_t *)pData;

  // search table.db
  TBC *pTbDbc = NULL;

  tdbTbcOpen(pMeta->pTbDb, &pTbDbc, &pMeta->txn);
  tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c);
  ASSERT(c == 0);
  tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);

  // get table entry
  SDecoder dc = {0};
  entry.pBuf = taosMemoryMalloc(nData);
  memcpy(entry.pBuf, pData, nData);
  tDecoderInit(&dc, entry.pBuf, nData);
  ret = metaDecodeEntry(&dc, &entry);
  ASSERT(ret == 0);

  entry.version = version;
  metaWLock(pMeta);
  // build SMetaEntry
  if (entry.type == TSDB_CHILD_TABLE) {
H
Hongze Cheng 已提交
936
    if (pAlterTbReq->updateTTL) {
wmmhello's avatar
wmmhello 已提交
937 938 939 940
      metaDeleteTtlIdx(pMeta, &entry);
      entry.ctbEntry.ttlDays = pAlterTbReq->newTTL;
      metaUpdateTtlIdx(pMeta, &entry);
    }
H
Hongze Cheng 已提交
941
    if (pAlterTbReq->newCommentLen >= 0) {
wmmhello's avatar
wmmhello 已提交
942 943 944
      entry.ctbEntry.commentLen = pAlterTbReq->newCommentLen;
      entry.ctbEntry.comment = pAlterTbReq->newComment;
    }
wmmhello's avatar
wmmhello 已提交
945
  } else {
H
Hongze Cheng 已提交
946
    if (pAlterTbReq->updateTTL) {
wmmhello's avatar
wmmhello 已提交
947 948 949 950
      metaDeleteTtlIdx(pMeta, &entry);
      entry.ntbEntry.ttlDays = pAlterTbReq->newTTL;
      metaUpdateTtlIdx(pMeta, &entry);
    }
H
Hongze Cheng 已提交
951
    if (pAlterTbReq->newCommentLen >= 0) {
wmmhello's avatar
wmmhello 已提交
952 953 954
      entry.ntbEntry.commentLen = pAlterTbReq->newCommentLen;
      entry.ntbEntry.comment = pAlterTbReq->newComment;
    }
955
  }
wmmhello's avatar
wmmhello 已提交
956 957 958 959 960 961 962 963

  // save to table db
  metaSaveToTbDb(pMeta, &entry);
  tdbTbcUpsert(pUidIdxc, &entry.uid, sizeof(tb_uid_t), &version, sizeof(version), 0);
  metaULock(pMeta);

  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
964
  tDecoderClear(&dc);
wmmhello's avatar
wmmhello 已提交
965
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
H
Hongze Cheng 已提交
966 967 968
  return 0;
}

D
dapan1121 已提交
969
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
H
Hongze Cheng 已提交
970 971 972 973 974
  switch (pReq->action) {
    case TSDB_ALTER_TABLE_ADD_COLUMN:
    case TSDB_ALTER_TABLE_DROP_COLUMN:
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
D
dapan1121 已提交
975
      return metaAlterTableColumn(pMeta, version, pReq, pMetaRsp);
H
Hongze Cheng 已提交
976 977 978 979 980 981 982 983 984 985 986
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
      return metaUpdateTableTagVal(pMeta, version, pReq);
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
      return metaUpdateTableOptions(pMeta, version, pReq);
    default:
      terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
      return -1;
      break;
  }
}

H
Hongze Cheng 已提交
987
static int metaSaveToTbDb(SMeta *pMeta, const SMetaEntry *pME) {
H
Hongze Cheng 已提交
988
  STbDbKey tbDbKey;
H
Hongze Cheng 已提交
989 990
  void    *pKey = NULL;
  void    *pVal = NULL;
H
Hongze Cheng 已提交
991 992
  int      kLen = 0;
  int      vLen = 0;
H
Hongze Cheng 已提交
993
  SEncoder coder = {0};
H
Hongze Cheng 已提交
994 995

  // set key and value
H
Hongze Cheng 已提交
996
  tbDbKey.version = pME->version;
H
Hongze Cheng 已提交
997 998
  tbDbKey.uid = pME->uid;

999 1000 1001
  metaDebug("vgId:%d, start to save table version:%" PRId64 "uid: %" PRId64, TD_VID(pMeta->pVnode), pME->version,
            pME->uid);

H
Hongze Cheng 已提交
1002 1003
  pKey = &tbDbKey;
  kLen = sizeof(tbDbKey);
H
Hongze Cheng 已提交
1004

wafwerar's avatar
wafwerar 已提交
1005 1006 1007
  int32_t ret = 0;
  tEncodeSize(metaEncodeEntry, pME, vLen, ret);
  if (ret < 0) {
H
Hongze Cheng 已提交
1008 1009 1010 1011 1012 1013 1014 1015 1016
    goto _err;
  }

  pVal = taosMemoryMalloc(vLen);
  if (pVal == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

H
Hongze Cheng 已提交
1017
  tEncoderInit(&coder, pVal, vLen);
H
Hongze Cheng 已提交
1018 1019 1020 1021 1022

  if (metaEncodeEntry(&coder, pME) < 0) {
    goto _err;
  }

H
Hongze Cheng 已提交
1023
  tEncoderClear(&coder);
H
Hongze Cheng 已提交
1024 1025

  // write to table.db
H
Hongze Cheng 已提交
1026
  if (tdbTbInsert(pMeta->pTbDb, pKey, kLen, pVal, vLen, &pMeta->txn) < 0) {
H
Hongze Cheng 已提交
1027 1028 1029 1030 1031 1032 1033
    goto _err;
  }

  taosMemoryFree(pVal);
  return 0;

_err:
1034 1035 1036
  metaError("vgId:%d, failed to save table version:%" PRId64 "uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pME->version,
            pME->uid, tstrerror(terrno));

H
Hongze Cheng 已提交
1037 1038 1039 1040
  taosMemoryFree(pVal);
  return -1;
}

H
Hongze Cheng 已提交
1041
static int metaUpdateUidIdx(SMeta *pMeta, const SMetaEntry *pME) {
H
Hongze Cheng 已提交
1042
  return tdbTbInsert(pMeta->pUidIdx, &pME->uid, sizeof(tb_uid_t), &pME->version, sizeof(int64_t), &pMeta->txn);
H
Hongze Cheng 已提交
1043 1044
}

C
Cary Xu 已提交
1045 1046 1047 1048
static int metaUpdateSuidIdx(SMeta *pMeta, const SMetaEntry *pME) {
  return tdbTbInsert(pMeta->pSuidIdx, &pME->uid, sizeof(tb_uid_t), NULL, 0, &pMeta->txn);
}

H
Hongze Cheng 已提交
1049
static int metaUpdateNameIdx(SMeta *pMeta, const SMetaEntry *pME) {
H
Hongze Cheng 已提交
1050
  return tdbTbInsert(pMeta->pNameIdx, pME->name, strlen(pME->name) + 1, &pME->uid, sizeof(tb_uid_t), &pMeta->txn);
H
Hongze Cheng 已提交
1051 1052
}

H
Hongze Cheng 已提交
1053
static int metaUpdateTtlIdx(SMeta *pMeta, const SMetaEntry *pME) {
1054 1055
  STtlIdxKey ttlKey = {0};
  metaBuildTtlIdxKey(&ttlKey, pME);
H
Hongze Cheng 已提交
1056
  if (ttlKey.dtime == 0) return 0;
H
Hongze Cheng 已提交
1057
  return tdbTbInsert(pMeta->pTtlIdx, &ttlKey, sizeof(ttlKey), NULL, 0, &pMeta->txn);
H
Hongze Cheng 已提交
1058 1059
}

H
Hongze Cheng 已提交
1060 1061
static int metaUpdateCtbIdx(SMeta *pMeta, const SMetaEntry *pME) {
  SCtbIdxKey ctbIdxKey = {.suid = pME->ctbEntry.suid, .uid = pME->uid};
H
Hongze Cheng 已提交
1062
  return tdbTbInsert(pMeta->pCtbIdx, &ctbIdxKey, sizeof(ctbIdxKey), NULL, 0, &pMeta->txn);
H
Hongze Cheng 已提交
1063 1064
}

wmmhello's avatar
wmmhello 已提交
1065
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
1066
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
dengyihao's avatar
dengyihao 已提交
1067 1068 1069 1070 1071
  if (IS_VAR_DATA_TYPE(type)) {
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
  } else {
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
  }
H
Hongze Cheng 已提交
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082

  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
  if (*ppTagIdxKey == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  (*ppTagIdxKey)->suid = suid;
  (*ppTagIdxKey)->cid = cid;
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
  (*ppTagIdxKey)->type = type;
dengyihao's avatar
dengyihao 已提交
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092

  // refactor
  if (IS_VAR_DATA_TYPE(type)) {
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
    memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
    *(tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData) = uid;
  } else {
    memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
    *(tb_uid_t *)((*ppTagIdxKey)->data + nTagData) = uid;
  }
H
Hongze Cheng 已提交
1093 1094 1095 1096 1097 1098 1099 1100 1101

  return 0;
}

static void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
}

static int metaUpdateTagIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry) {
H
Hongze Cheng 已提交
1102
  void          *pData = NULL;
H
Hongze Cheng 已提交
1103 1104 1105
  int            nData = 0;
  STbDbKey       tbDbKey = {0};
  SMetaEntry     stbEntry = {0};
H
Hongze Cheng 已提交
1106
  STagIdxKey    *pTagIdxKey = NULL;
H
Hongze Cheng 已提交
1107 1108
  int32_t        nTagIdxKey;
  const SSchema *pTagColumn;       // = &stbEntry.stbEntry.schema.pSchema[0];
H
Hongze Cheng 已提交
1109
  const void    *pTagData = NULL;  //
C
Cary Xu 已提交
1110
  int32_t        nTagData = 0;
H
Hongze Cheng 已提交
1111 1112 1113
  SDecoder       dc = {0};

  // get super table
H
Hongze Cheng 已提交
1114
  if (tdbTbGet(pMeta->pUidIdx, &pCtbEntry->ctbEntry.suid, sizeof(tb_uid_t), &pData, &nData) != 0) {
wmmhello's avatar
wmmhello 已提交
1115 1116
    return -1;
  }
H
Hongze Cheng 已提交
1117 1118
  tbDbKey.uid = pCtbEntry->ctbEntry.suid;
  tbDbKey.version = *(int64_t *)pData;
H
Hongze Cheng 已提交
1119
  tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData);
H
Hongze Cheng 已提交
1120 1121 1122 1123 1124

  tDecoderInit(&dc, pData, nData);
  metaDecodeEntry(&dc, &stbEntry);

  pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
C
Cary Xu 已提交
1125 1126

  STagVal tagVal = {.cid = pTagColumn->colId};
1127
  if (pTagColumn->type != TSDB_DATA_TYPE_JSON) {
wmmhello's avatar
wmmhello 已提交
1128
    tTagGet((const STag *)pCtbEntry->ctbEntry.pTags, &tagVal);
1129
    if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
wmmhello's avatar
wmmhello 已提交
1130 1131
      pTagData = tagVal.pData;
      nTagData = (int32_t)tagVal.nData;
1132
    } else {
wmmhello's avatar
wmmhello 已提交
1133 1134 1135
      pTagData = &(tagVal.i64);
      nTagData = tDataTypes[pTagColumn->type].bytes;
    }
1136 1137 1138
  } else {
    // pTagData = pCtbEntry->ctbEntry.pTags;
    // nTagData = ((const STag *)pCtbEntry->ctbEntry.pTags)->len;
dengyihao's avatar
dengyihao 已提交
1139 1140 1141
    pTagData = pCtbEntry->ctbEntry.pTags;
    nTagData = ((const STag *)pCtbEntry->ctbEntry.pTags)->len;
    return metaSaveJsonVarToIdx(pMeta, pCtbEntry, pTagColumn);
wmmhello's avatar
wmmhello 已提交
1142
  }
1143 1144
  if (metaCreateTagIdxKey(pCtbEntry->ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type,
                          pCtbEntry->uid, &pTagIdxKey, &nTagIdxKey) < 0) {
H
Hongze Cheng 已提交
1145 1146
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
1147
  tdbTbUpsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, &pMeta->txn);
H
Hongze Cheng 已提交
1148 1149 1150
  metaDestroyTagIdxKey(pTagIdxKey);
  tDecoderClear(&dc);
  tdbFree(pData);
H
Hongze Cheng 已提交
1151 1152 1153
  return 0;
}

H
Hongze Cheng 已提交
1154
static int metaSaveToSkmDb(SMeta *pMeta, const SMetaEntry *pME) {
H
Hongze Cheng 已提交
1155
  SEncoder              coder = {0};
H
Hongze Cheng 已提交
1156
  void                 *pVal = NULL;
H
Hongze Cheng 已提交
1157 1158 1159 1160 1161 1162
  int                   vLen = 0;
  int                   rcode = 0;
  SSkmDbKey             skmDbKey = {0};
  const SSchemaWrapper *pSW;

  if (pME->type == TSDB_SUPER_TABLE) {
1163
    pSW = &pME->stbEntry.schemaRow;
H
Hongze Cheng 已提交
1164
  } else if (pME->type == TSDB_NORMAL_TABLE) {
1165
    pSW = &pME->ntbEntry.schemaRow;
H
Hongze Cheng 已提交
1166 1167
  } else {
    ASSERT(0);
H
Hongze Cheng 已提交
1168 1169
  }

H
Hongze Cheng 已提交
1170
  skmDbKey.uid = pME->uid;
1171
  skmDbKey.sver = pSW->version;
H
Hongze Cheng 已提交
1172

1173 1174 1175 1176 1177
  // if receive tmq meta message is: create stable1 then delete stable1 then create stable1 with multi vgroups
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(skmDbKey), NULL, NULL) == 0) {
    return rcode;
  }

H
Hongze Cheng 已提交
1178
  // encode schema
wafwerar's avatar
wafwerar 已提交
1179 1180 1181
  int32_t ret = 0;
  tEncodeSize(tEncodeSSchemaWrapper, pSW, vLen, ret);
  if (ret < 0) return -1;
H
Hongze Cheng 已提交
1182
  pVal = taosMemoryMalloc(vLen);
H
Hongze Cheng 已提交
1183 1184 1185 1186 1187 1188
  if (pVal == NULL) {
    rcode = -1;
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

H
Hongze Cheng 已提交
1189
  tEncoderInit(&coder, pVal, vLen);
H
Hongze Cheng 已提交
1190 1191
  tEncodeSSchemaWrapper(&coder, pSW);

H
Hongze Cheng 已提交
1192
  if (tdbTbInsert(pMeta->pSkmDb, &skmDbKey, sizeof(skmDbKey), pVal, vLen, &pMeta->txn) < 0) {
H
Hongze Cheng 已提交
1193 1194 1195 1196 1197
    rcode = -1;
    goto _exit;
  }

_exit:
H
Hongze Cheng 已提交
1198
  taosMemoryFree(pVal);
H
Hongze Cheng 已提交
1199
  tEncoderClear(&coder);
H
Hongze Cheng 已提交
1200 1201 1202
  return rcode;
}

H
Hongze Cheng 已提交
1203
int metaHandleEntry(SMeta *pMeta, const SMetaEntry *pME) {
H
Hongze Cheng 已提交
1204 1205
  metaWLock(pMeta);

H
Hongze Cheng 已提交
1206
  // save to table.db
H
Hongze Cheng 已提交
1207
  if (metaSaveToTbDb(pMeta, pME) < 0) goto _err;
H
Hongze Cheng 已提交
1208 1209

  // update uid.idx
H
Hongze Cheng 已提交
1210
  if (metaUpdateUidIdx(pMeta, pME) < 0) goto _err;
H
Hongze Cheng 已提交
1211 1212

  // update name.idx
H
Hongze Cheng 已提交
1213
  if (metaUpdateNameIdx(pMeta, pME) < 0) goto _err;
H
Hongze Cheng 已提交
1214 1215 1216

  if (pME->type == TSDB_CHILD_TABLE) {
    // update ctb.idx
H
Hongze Cheng 已提交
1217
    if (metaUpdateCtbIdx(pMeta, pME) < 0) goto _err;
H
Hongze Cheng 已提交
1218 1219

    // update tag.idx
H
Hongze Cheng 已提交
1220
    if (metaUpdateTagIdx(pMeta, pME) < 0) goto _err;
H
Hongze Cheng 已提交
1221 1222
  } else {
    // update schema.db
H
Hongze Cheng 已提交
1223
    if (metaSaveToSkmDb(pMeta, pME) < 0) goto _err;
C
Cary Xu 已提交
1224 1225 1226

    if (pME->type == TSDB_SUPER_TABLE) {
      if (metaUpdateSuidIdx(pMeta, pME) < 0) goto _err;
H
Hongze Cheng 已提交
1227
    }
H
Hongze Cheng 已提交
1228 1229 1230
  }

  if (pME->type != TSDB_SUPER_TABLE) {
H
Hongze Cheng 已提交
1231
    if (metaUpdateTtlIdx(pMeta, pME) < 0) goto _err;
H
Hongze Cheng 已提交
1232 1233
  }

H
Hongze Cheng 已提交
1234
  metaULock(pMeta);
H
Hongze Cheng 已提交
1235
  return 0;
H
Hongze Cheng 已提交
1236 1237 1238 1239

_err:
  metaULock(pMeta);
  return -1;
1240
}
dengyihao's avatar
dengyihao 已提交
1241
// refactor later
dengyihao's avatar
dengyihao 已提交
1242 1243
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }