metaTable.c 64.6 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 19 20 21 22
static int  metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema);
static int  metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema);
static int  metaSaveToTbDb(SMeta *pMeta, const SMetaEntry *pME);
static int  metaUpdateUidIdx(SMeta *pMeta, const SMetaEntry *pME);
static int  metaUpdateNameIdx(SMeta *pMeta, const SMetaEntry *pME);
23
static int  metaUpdateTtl(SMeta *pMeta, const SMetaEntry *pME);
dengyihao's avatar
dengyihao 已提交
24 25 26 27 28 29
static int  metaSaveToSkmDb(SMeta *pMeta, const SMetaEntry *pME);
static int  metaUpdateCtbIdx(SMeta *pMeta, const SMetaEntry *pME);
static int  metaUpdateSuidIdx(SMeta *pMeta, const SMetaEntry *pME);
static int  metaUpdateTagIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry);
static int  metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type);
static void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey);
dengyihao's avatar
dengyihao 已提交
30
// opt ins_tables query
31 32
static int metaUpdateBtimeIdx(SMeta *pMeta, const SMetaEntry *pME);
static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME);
dengyihao's avatar
dengyihao 已提交
33 34
static int metaUpdateNcolIdx(SMeta *pMeta, const SMetaEntry *pME);
static int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME);
H
Hongze Cheng 已提交
35

H
Hongze Cheng 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48
static void metaGetEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) {
  pInfo->uid = pEntry->uid;
  pInfo->version = pEntry->version;
  if (pEntry->type == TSDB_SUPER_TABLE) {
    pInfo->suid = pEntry->uid;
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
  } else if (pEntry->type == TSDB_CHILD_TABLE) {
    pInfo->suid = pEntry->ctbEntry.suid;
    pInfo->skmVer = 0;
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
    pInfo->suid = 0;
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
  } else {
49
    metaError("meta/table: invalide table type: %" PRId8 " get entry info failed.", pEntry->type);
H
Hongze Cheng 已提交
50 51 52
  }
}

dengyihao's avatar
dengyihao 已提交
53
static int metaUpdateMetaRsp(tb_uid_t uid, char *tbName, SSchemaWrapper *pSchema, STableMetaRsp *pMetaRsp) {
D
dapan1121 已提交
54 55
  pMetaRsp->pSchemas = taosMemoryMalloc(pSchema->nCols * sizeof(SSchema));
  if (NULL == pMetaRsp->pSchemas) {
56
    terrno = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
57 58 59
    return -1;
  }

M
Minglei Jin 已提交
60
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
D
dapan1121 已提交
61 62 63 64 65 66 67 68 69 70
  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 已提交
71 72 73 74 75
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 已提交
76
  void       *data = pCtbEntry->ctbEntry.pTags;
dengyihao's avatar
dengyihao 已提交
77 78 79 80 81 82 83 84 85 86 87
  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 已提交
88

dengyihao's avatar
dengyihao 已提交
89 90 91 92 93
  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 已提交
94

H
Hongze Cheng 已提交
95
    char   *key = pTagVal->pKey;
dengyihao's avatar
dengyihao 已提交
96
    int32_t nKey = strlen(key);
dengyihao's avatar
dengyihao 已提交
97 98 99

    SIndexTerm *term = NULL;
    if (type == TSDB_DATA_TYPE_NULL) {
dengyihao's avatar
dengyihao 已提交
100
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
dengyihao's avatar
dengyihao 已提交
101 102
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
      if (pTagVal->nData > 0) {
H
Hongze Cheng 已提交
103
        char   *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
dengyihao's avatar
dengyihao 已提交
104 105
        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 已提交
106
        type = TSDB_DATA_TYPE_VARCHAR;
dengyihao's avatar
dengyihao 已提交
107
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
wmmhello's avatar
wmmhello 已提交
108
        taosMemoryFree(val);
dengyihao's avatar
dengyihao 已提交
109
      } else if (pTagVal->nData == 0) {
dengyihao's avatar
dengyihao 已提交
110
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
dengyihao's avatar
dengyihao 已提交
111 112 113
      }
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
      double val = *(double *)(&pTagVal->i64);
dengyihao's avatar
dengyihao 已提交
114
      int    len = sizeof(val);
dengyihao's avatar
dengyihao 已提交
115 116 117
      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 已提交
118
      int len = sizeof(val);
dengyihao's avatar
dengyihao 已提交
119
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
dengyihao's avatar
dengyihao 已提交
120
    }
dengyihao's avatar
dengyihao 已提交
121
    if (term != NULL) {
dengyihao's avatar
dengyihao 已提交
122 123 124
      indexMultiTermAdd(terms, term);
    }
  }
dengyihao's avatar
dengyihao 已提交
125
  indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
dengyihao's avatar
dengyihao 已提交
126
  indexMultiTermDestroy(terms);
dengyihao's avatar
dengyihao 已提交
127 128

  taosArrayDestroy(pTagVals);
dengyihao's avatar
dengyihao 已提交
129
#endif
dengyihao's avatar
dengyihao 已提交
130
  return 0;
dengyihao's avatar
dengyihao 已提交
131
}
132 133 134 135 136
int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
#ifdef USE_INVERTED_INDEX
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
    return -1;
  }
137
  void       *data = pCtbEntry->ctbEntry.pTags;
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
  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;

156
    char   *key = pTagVal->pKey;
157 158 159 160 161 162 163
    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) {
164
        char   *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
165 166 167 168
        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);
dengyihao's avatar
dengyihao 已提交
169
        taosMemoryFree(val);
170 171 172 173 174 175 176 177 178 179 180
      } 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 已提交
181
    }
dengyihao's avatar
dengyihao 已提交
182
    if (term != NULL) {
dengyihao's avatar
dengyihao 已提交
183 184 185
      indexMultiTermAdd(terms, term);
    }
  }
dengyihao's avatar
dengyihao 已提交
186
  indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
dengyihao's avatar
dengyihao 已提交
187
  indexMultiTermDestroy(terms);
dengyihao's avatar
dengyihao 已提交
188
  taosArrayDestroy(pTagVals);
dengyihao's avatar
dengyihao 已提交
189
#endif
dengyihao's avatar
dengyihao 已提交
190
  return 0;
dengyihao's avatar
dengyihao 已提交
191 192
}

H
Hongze Cheng 已提交
193
int metaCreateSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
H
Hongze Cheng 已提交
194
  SMetaEntry  me = {0};
H
Hongze Cheng 已提交
195 196 197 198
  int         kLen = 0;
  int         vLen = 0;
  const void *pKey = NULL;
  const void *pVal = NULL;
H
Hongze Cheng 已提交
199
  void       *pBuf = NULL;
H
Hongze Cheng 已提交
200
  int32_t     szBuf = 0;
H
Hongze Cheng 已提交
201
  void       *p = NULL;
H
Hongze Cheng 已提交
202 203

  // validate req
204 205
  void *pData = NULL;
  int   nData = 0;
M
Minglei Jin 已提交
206
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &pData, &nData) == 0) {
207 208 209
    tb_uid_t uid = *(tb_uid_t *)pData;
    tdbFree(pData);
    SMetaInfo info;
210 211 212 213
    if (metaGetInfo(pMeta, uid, &info, NULL) == TSDB_CODE_NOT_FOUND) {
      terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
      return -1;
    }
214
    if (info.uid == info.suid) {
215 216
      return 0;
    } else {
217
      terrno = TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
218 219
      return -1;
    }
H
Hongze Cheng 已提交
220
  }
H
Hongze Cheng 已提交
221 222

  // set structs
H
Hongze Cheng 已提交
223
  me.version = version;
H
Hongze Cheng 已提交
224 225 226
  me.type = TSDB_SUPER_TABLE;
  me.uid = pReq->suid;
  me.name = pReq->name;
227
  me.stbEntry.schemaRow = pReq->schemaRow;
H
Hongze Cheng 已提交
228
  me.stbEntry.schemaTag = pReq->schemaTag;
C
Cary Xu 已提交
229 230 231 232
  if (pReq->rollup) {
    TABLE_SET_ROLLUP(me.flags);
    me.stbEntry.rsmaParam = pReq->rsmaParam;
  }
H
Hongze Cheng 已提交
233

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

236 237
  ++pMeta->pVnode->config.vndStats.numOfSTables;

S
Shengliang Guan 已提交
238
  metaDebug("vgId:%d, stb:%s is created, suid:%" PRId64, TD_VID(pMeta->pVnode), pReq->name, pReq->suid);
H
Hongze Cheng 已提交
239 240 241 242

  return 0;

_err:
M
Minglei Jin 已提交
243 244
  metaError("vgId:%d, failed to create stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
            tstrerror(terrno));
H
Hongze Cheng 已提交
245 246 247
  return -1;
}

248
int metaDropSTable(SMeta *pMeta, int64_t verison, SVDropStbReq *pReq, SArray *tbUidList) {
H
Hongze Cheng 已提交
249 250 251 252 253 254 255 256 257 258
  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) {
H
Hongze Cheng 已提交
259
    tdbFree(pData);
260
    terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
H
Hongze Cheng 已提交
261
    return -1;
H
Hongze Cheng 已提交
262 263
  }

H
Hongze Cheng 已提交
264
  // drop all child tables
265
  TBC *pCtbIdxc = NULL;
H
Hongze Cheng 已提交
266

267
  tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL);
H
Hongze Cheng 已提交
268 269
  rc = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = pReq->suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c);
  if (rc < 0) {
H
Hongze Cheng 已提交
270
    tdbTbcClose(pCtbIdxc);
H
Hongze Cheng 已提交
271 272
    metaWLock(pMeta);
    goto _drop_super_table;
H
Hongze Cheng 已提交
273 274 275
  }

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

H
Hongze Cheng 已提交
279 280 281 282 283
    if (((SCtbIdxKey *)pKey)->suid < pReq->suid) {
      continue;
    } else if (((SCtbIdxKey *)pKey)->suid > pReq->suid) {
      break;
    }
H
Hongze Cheng 已提交
284

285
    taosArrayPush(tbUidList, &(((SCtbIdxKey *)pKey)->uid));
H
Hongze Cheng 已提交
286 287 288 289 290
  }

  tdbTbcClose(pCtbIdxc);

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

292 293
  for (int32_t iChild = 0; iChild < taosArrayGetSize(tbUidList); iChild++) {
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUidList, iChild);
H
Hongze Cheng 已提交
294
    metaDropTableByUid(pMeta, uid, NULL);
H
Hongze Cheng 已提交
295 296
  }

H
Hongze Cheng 已提交
297 298 299
  // drop super table
_drop_super_table:
  tdbTbGet(pMeta->pUidIdx, &pReq->suid, sizeof(tb_uid_t), &pData, &nData);
H
Hongze Cheng 已提交
300
  tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = ((SUidIdxVal *)pData)[0].version, .uid = pReq->suid},
301 302 303 304
              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);
  tdbTbDelete(pMeta->pSuidIdx, &pReq->suid, sizeof(tb_uid_t), pMeta->txn);
H
Hongze Cheng 已提交
305 306 307

  metaULock(pMeta);

H
Hongze Cheng 已提交
308
_exit:
H
Hongze Cheng 已提交
309 310
  tdbFree(pKey);
  tdbFree(pData);
C
Cary Xu 已提交
311
  metaDebug("vgId:%d, super table %s uid:%" PRId64 " is dropped", TD_VID(pMeta->pVnode), pReq->name, pReq->suid);
H
Hongze Cheng 已提交
312 313 314
  return 0;
}

H
Hongze Cheng 已提交
315 316 317
int metaAlterSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
  SMetaEntry  oStbEntry = {0};
  SMetaEntry  nStbEntry = {0};
H
Hongze Cheng 已提交
318 319
  TBC        *pUidIdxc = NULL;
  TBC        *pTbDbc = NULL;
H
Hongze Cheng 已提交
320 321 322 323 324
  const void *pData;
  int         nData;
  int64_t     oversion;
  SDecoder    dc = {0};
  int32_t     ret;
325
  int32_t     c = -2;
H
Hongze Cheng 已提交
326

327
  tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, NULL);
H
Hongze Cheng 已提交
328
  ret = tdbTbcMoveTo(pUidIdxc, &pReq->suid, sizeof(tb_uid_t), &c);
H
Hongze Cheng 已提交
329
  if (ret < 0 || c) {
330 331 332
    tdbTbcClose(pUidIdxc);

    terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
H
Hongze Cheng 已提交
333 334 335
    return -1;
  }

H
Hongze Cheng 已提交
336
  ret = tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
337
  if (ret < 0) {
M
Minglei Jin 已提交
338 339
    tdbTbcClose(pUidIdxc);

M
Minglei Jin 已提交
340
    terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
H
Hongze Cheng 已提交
341 342 343
    return -1;
  }

H
Hongze Cheng 已提交
344
  oversion = ((SUidIdxVal *)pData)[0].version;
H
Hongze Cheng 已提交
345

346
  tdbTbcOpen(pMeta->pTbDb, &pTbDbc, NULL);
H
Hongze Cheng 已提交
347
  ret = tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = pReq->suid, .version = oversion}), sizeof(STbDbKey), &c);
348
  if (!(ret == 0 && c == 0)) {
349 350 351 352
    tdbTbcClose(pUidIdxc);
    tdbTbcClose(pTbDbc);

    terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
353 354 355
    metaError("meta/table: invalide ret: %" PRId32 " or c: %" PRId32 "alter stb failed.", ret, c);
    return -1;
  }
H
Hongze Cheng 已提交
356

H
Hongze Cheng 已提交
357
  ret = tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);
358
  if (ret < 0) {
359
    tdbTbcClose(pUidIdxc);
360 361 362 363 364
    tdbTbcClose(pTbDbc);

    terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
    return -1;
  }
H
Hongze Cheng 已提交
365

H
Hongze Cheng 已提交
366 367 368
  oStbEntry.pBuf = taosMemoryMalloc(nData);
  memcpy(oStbEntry.pBuf, pData, nData);
  tDecoderInit(&dc, oStbEntry.pBuf, nData);
H
Hongze Cheng 已提交
369 370 371 372 373 374
  metaDecodeEntry(&dc, &oStbEntry);

  nStbEntry.version = version;
  nStbEntry.type = TSDB_SUPER_TABLE;
  nStbEntry.uid = pReq->suid;
  nStbEntry.name = pReq->name;
375
  nStbEntry.stbEntry.schemaRow = pReq->schemaRow;
H
Hongze Cheng 已提交
376 377 378 379
  nStbEntry.stbEntry.schemaTag = pReq->schemaTag;

  metaWLock(pMeta);
  // compare two entry
380 381
  if (oStbEntry.stbEntry.schemaRow.version != pReq->schemaRow.version) {
    metaSaveToSkmDb(pMeta, &nStbEntry);
H
Hongze Cheng 已提交
382 383 384 385 386 387
  }

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

  // update uid index
H
Hongze Cheng 已提交
388
  metaUpdateUidIdx(pMeta, &nStbEntry);
H
Hongze Cheng 已提交
389

390
  // metaStatsCacheDrop(pMeta, nStbEntry.uid);
391

H
Hongze Cheng 已提交
392
  metaULock(pMeta);
393 394

  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
H
Hongze Cheng 已提交
395
  tDecoderClear(&dc);
H
Hongze Cheng 已提交
396 397
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
H
Hongze Cheng 已提交
398 399
  return 0;
}
dengyihao's avatar
dengyihao 已提交
400
int metaAddIndexToSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
dengyihao's avatar
dengyihao 已提交
401 402 403
  SMetaEntry oStbEntry = {0};
  SMetaEntry nStbEntry = {0};

dengyihao's avatar
dengyihao 已提交
404 405
  STbDbKey tbDbKey = {0};

dengyihao's avatar
dengyihao 已提交
406 407
  TBC     *pUidIdxc = NULL;
  TBC     *pTbDbc = NULL;
dengyihao's avatar
dengyihao 已提交
408 409
  void    *pData = NULL;
  int      nData = 0;
dengyihao's avatar
dengyihao 已提交
410 411 412 413 414 415 416 417 418 419 420 421
  int64_t  oversion;
  SDecoder dc = {0};
  int32_t  ret;
  int32_t  c = -2;
  tb_uid_t suid = pReq->suid;

  // get super table
  if (tdbTbGet(pMeta->pUidIdx, &suid, sizeof(tb_uid_t), &pData, &nData) != 0) {
    ret = -1;
    goto _err;
  }

dengyihao's avatar
dengyihao 已提交
422 423 424
  tbDbKey.uid = suid;
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
  tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData);
dengyihao's avatar
dengyihao 已提交
425

dengyihao's avatar
dengyihao 已提交
426 427 428 429 430
  tDecoderInit(&dc, pData, nData);
  ret = metaDecodeEntry(&dc, &oStbEntry);
  if (ret < 0) {
    goto _err;
  }
dengyihao's avatar
dengyihao 已提交
431

dengyihao's avatar
dengyihao 已提交
432 433 434
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
    goto _err;
  }
dengyihao's avatar
dengyihao 已提交
435

dengyihao's avatar
dengyihao 已提交
436
  if (oStbEntry.stbEntry.schemaTag.version == pReq->schemaTag.version) {
dengyihao's avatar
dengyihao 已提交
437 438
    goto _err;
  }
dengyihao's avatar
dengyihao 已提交
439

dengyihao's avatar
dengyihao 已提交
440 441 442
  if (oStbEntry.stbEntry.schemaTag.nCols != pReq->schemaTag.nCols) {
    goto _err;
  }
dengyihao's avatar
dengyihao 已提交
443

dengyihao's avatar
dengyihao 已提交
444 445 446 447 448 449 450 451 452 453 454 455
  int diffIdx = -1;
  for (int i = 0; i < pReq->schemaTag.nCols; i++) {
    SSchema *pNew = pReq->schemaTag.pSchema + i;
    SSchema *pOld = oStbEntry.stbEntry.schemaTag.pSchema + i;
    if (pNew->type != pOld->type || pNew->colId != pOld->colId || pNew->bytes != pOld->bytes ||
        strncmp(pOld->name, pNew->name, sizeof(pNew->name))) {
      goto _err;
    }
    if (IS_IDX_ON(pNew) && !IS_IDX_ON(pOld)) {
      if (diffIdx != -1) goto _err;
      diffIdx = i;
    }
dengyihao's avatar
dengyihao 已提交
456 457
  }

Y
yihaoDeng 已提交
458
  if (diffIdx == -1 || diffIdx == 0) {
dengyihao's avatar
dengyihao 已提交
459 460
    goto _err;
  }
dengyihao's avatar
dengyihao 已提交
461 462

  // Get target schema info
dengyihao's avatar
dengyihao 已提交
463
  SSchemaWrapper *pTagSchema = &pReq->schemaTag;
dengyihao's avatar
dengyihao 已提交
464 465 466 467
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
    terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
    goto _err;
  }
dengyihao's avatar
dengyihao 已提交
468
  SSchema *pCol = pTagSchema->pSchema + diffIdx;
dengyihao's avatar
dengyihao 已提交
469 470 471 472 473 474 475 476 477 478 479 480

  /*
   * iterator all pTdDbc by uid and version
   */
  TBC *pCtbIdxc = NULL;
  tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL);
  int rc = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c);
  if (rc < 0) {
    tdbTbcClose(pCtbIdxc);
    goto _err;
  }
  for (;;) {
dengyihao's avatar
dengyihao 已提交
481 482
    void *pKey = NULL, *pVal = NULL;
    int   nKey = 0, nVal = 0;
dengyihao's avatar
dengyihao 已提交
483
    rc = tdbTbcNext(pCtbIdxc, &pKey, &nKey, &pVal, &nVal);
dengyihao's avatar
dengyihao 已提交
484 485 486 487 488 489 490
    if (rc < 0) {
      tdbFree(pKey);
      tdbFree(pVal);
      tdbTbcClose(pCtbIdxc);
      pCtbIdxc = NULL;
      break;
    }
dengyihao's avatar
dengyihao 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
    if (((SCtbIdxKey *)pKey)->suid != suid) {
      tdbFree(pKey);
      tdbFree(pVal);
      continue;
    }
    STagIdxKey *pTagIdxKey = NULL;
    int32_t     nTagIdxKey;

    const void *pTagData = NULL;
    int32_t     nTagData = 0;

    SCtbIdxKey *table = (SCtbIdxKey *)pKey;
    STagVal     tagVal = {.cid = pCol->colId};
    tTagGet((const STag *)pVal, &tagVal);
    if (IS_VAR_DATA_TYPE(pCol->type)) {
      pTagData = tagVal.pData;
      nTagData = (int32_t)tagVal.nData;
    } else {
      pTagData = &(tagVal.i64);
      nTagData = tDataTypes[pCol->type].bytes;
    }
dengyihao's avatar
dengyihao 已提交
512 513 514 515
    rc = metaCreateTagIdxKey(suid, pCol->colId, pTagData, nTagData, pCol->type, table->uid, &pTagIdxKey, &nTagIdxKey);
    tdbFree(pKey);
    tdbFree(pVal);
    if (rc < 0) {
dengyihao's avatar
dengyihao 已提交
516
      metaDestroyTagIdxKey(pTagIdxKey);
dengyihao's avatar
dengyihao 已提交
517
      tdbTbcClose(pCtbIdxc);
dengyihao's avatar
dengyihao 已提交
518 519
      goto _err;
    }
dengyihao's avatar
dengyihao 已提交
520 521

    metaWLock(pMeta);
dengyihao's avatar
dengyihao 已提交
522
    tdbTbUpsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn);
dengyihao's avatar
dengyihao 已提交
523
    metaULock(pMeta);
dengyihao's avatar
dengyihao 已提交
524 525
    metaDestroyTagIdxKey(pTagIdxKey);
  }
dengyihao's avatar
dengyihao 已提交
526 527 528 529 530 531 532 533

  nStbEntry.version = version;
  nStbEntry.type = TSDB_SUPER_TABLE;
  nStbEntry.uid = pReq->suid;
  nStbEntry.name = pReq->name;
  nStbEntry.stbEntry.schemaRow = pReq->schemaRow;
  nStbEntry.stbEntry.schemaTag = pReq->schemaTag;

dengyihao's avatar
dengyihao 已提交
534
  metaWLock(pMeta);
dengyihao's avatar
dengyihao 已提交
535 536 537 538 539
  // update table.db
  metaSaveToTbDb(pMeta, &nStbEntry);
  // update uid index
  metaUpdateUidIdx(pMeta, &nStbEntry);
  metaULock(pMeta);
dengyihao's avatar
dengyihao 已提交
540 541 542

  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
  tDecoderClear(&dc);
dengyihao's avatar
dengyihao 已提交
543 544 545
  tdbFree(pData);

  tdbTbcClose(pCtbIdxc);
dengyihao's avatar
dengyihao 已提交
546
  return TSDB_CODE_SUCCESS;
dengyihao's avatar
dengyihao 已提交
547
_err:
dengyihao's avatar
dengyihao 已提交
548 549 550 551
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
  tDecoderClear(&dc);
  tdbFree(pData);

dengyihao's avatar
dengyihao 已提交
552
  return TSDB_CODE_VND_COL_ALREADY_EXISTS;
dengyihao's avatar
dengyihao 已提交
553
}
dengyihao's avatar
dengyihao 已提交
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
int metaDropIndexFromSTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
  SMetaEntry oStbEntry = {0};
  SMetaEntry nStbEntry = {0};

  STbDbKey tbDbKey = {0};
  TBC     *pUidIdxc = NULL;
  TBC     *pTbDbc = NULL;
  int      ret = 0;
  int      c = -2;
  void    *pData = NULL;
  int      nData = 0;
  int64_t  oversion;
  SDecoder dc = {0};

  tb_uid_t suid = pReq->stbUid;

  if (tdbTbGet(pMeta->pUidIdx, &suid, sizeof(tb_uid_t), &pData, &nData) != 0) {
    ret = -1;
    goto _err;
  }

  tbDbKey.uid = suid;
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
  tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData);
  tDecoderInit(&dc, pData, nData);
  ret = metaDecodeEntry(&dc, &oStbEntry);
  if (ret < 0) {
    goto _err;
  }

  SSchema *pCol = NULL;
  int32_t  colId = -1;
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
dengyihao's avatar
dengyihao 已提交
588
    if (0 == strncmp(schema->name, pReq->colName, sizeof(pReq->colName))) {
dengyihao's avatar
dengyihao 已提交
589
      if (i != 0 || IS_IDX_ON(schema)) {
dengyihao's avatar
dengyihao 已提交
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
        pCol = schema;
      }
      break;
    }
  }

  if (pCol == NULL) {
    goto _err;
  }

  /*
   * iterator all pTdDbc by uid and version
   */
  TBC *pCtbIdxc = NULL;
  tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL);
  int rc = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c);
  if (rc < 0) {
    tdbTbcClose(pCtbIdxc);
    goto _err;
  }
  for (;;) {
    void *pKey = NULL, *pVal = NULL;
    int   nKey = 0, nVal = 0;
    rc = tdbTbcNext(pCtbIdxc, &pKey, &nKey, &pVal, &nVal);
dengyihao's avatar
dengyihao 已提交
614 615 616 617 618 619 620
    if (rc < 0) {
      tdbFree(pKey);
      tdbFree(pVal);
      tdbTbcClose(pCtbIdxc);
      pCtbIdxc = NULL;
      break;
    }
dengyihao's avatar
dengyihao 已提交
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
    if (((SCtbIdxKey *)pKey)->suid != suid) {
      tdbFree(pKey);
      tdbFree(pVal);
      continue;
    }
    STagIdxKey *pTagIdxKey = NULL;
    int32_t     nTagIdxKey;

    const void *pTagData = NULL;
    int32_t     nTagData = 0;

    SCtbIdxKey *table = (SCtbIdxKey *)pKey;
    STagVal     tagVal = {.cid = pCol->colId};
    tTagGet((const STag *)pVal, &tagVal);
    if (IS_VAR_DATA_TYPE(pCol->type)) {
      pTagData = tagVal.pData;
      nTagData = (int32_t)tagVal.nData;
    } else {
      pTagData = &(tagVal.i64);
      nTagData = tDataTypes[pCol->type].bytes;
    }
dengyihao's avatar
dengyihao 已提交
642 643 644 645
    rc = metaCreateTagIdxKey(suid, pCol->colId, pTagData, nTagData, pCol->type, table->uid, &pTagIdxKey, &nTagIdxKey);
    tdbFree(pKey);
    tdbFree(pVal);
    if (rc < 0) {
dengyihao's avatar
dengyihao 已提交
646
      metaDestroyTagIdxKey(pTagIdxKey);
dengyihao's avatar
dengyihao 已提交
647
      tdbTbcClose(pCtbIdxc);
dengyihao's avatar
dengyihao 已提交
648 649
      goto _err;
    }
dengyihao's avatar
dengyihao 已提交
650 651

    metaWLock(pMeta);
dengyihao's avatar
dengyihao 已提交
652
    tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
dengyihao's avatar
dengyihao 已提交
653
    metaULock(pMeta);
dengyihao's avatar
dengyihao 已提交
654 655 656 657
    metaDestroyTagIdxKey(pTagIdxKey);
  }

  // clear idx flag
dengyihao's avatar
dengyihao 已提交
658
  SSCHMEA_SET_IDX_OFF(pCol);
dengyihao's avatar
dengyihao 已提交
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678

  nStbEntry.version = version;
  nStbEntry.type = TSDB_SUPER_TABLE;
  nStbEntry.uid = oStbEntry.uid;
  nStbEntry.name = oStbEntry.name;

  SSchemaWrapper *row = tCloneSSchemaWrapper(&oStbEntry.stbEntry.schemaRow);
  SSchemaWrapper *tag = tCloneSSchemaWrapper(&oStbEntry.stbEntry.schemaTag);

  nStbEntry.stbEntry.schemaRow = *row;
  nStbEntry.stbEntry.schemaTag = *tag;
  nStbEntry.stbEntry.rsmaParam = oStbEntry.stbEntry.rsmaParam;

  metaWLock(pMeta);
  // update table.db
  metaSaveToTbDb(pMeta, &nStbEntry);
  // update uid index
  metaUpdateUidIdx(pMeta, &nStbEntry);
  metaULock(pMeta);

679 680
  tDeleteSchemaWrapper(tag);
  tDeleteSchemaWrapper(row);
dengyihao's avatar
dengyihao 已提交
681

dengyihao's avatar
dengyihao 已提交
682 683
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
  tDecoderClear(&dc);
dengyihao's avatar
dengyihao 已提交
684 685 686
  tdbFree(pData);

  tdbTbcClose(pCtbIdxc);
dengyihao's avatar
dengyihao 已提交
687
  return TSDB_CODE_SUCCESS;
dengyihao's avatar
dengyihao 已提交
688
_err:
dengyihao's avatar
dengyihao 已提交
689 690 691 692
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
  tDecoderClear(&dc);
  tdbFree(pData);

dengyihao's avatar
dengyihao 已提交
693
  return -1;
dengyihao's avatar
dengyihao 已提交
694
}
H
Hongze Cheng 已提交
695

696
int metaCreateTable(SMeta *pMeta, int64_t ver, SVCreateTbReq *pReq, STableMetaRsp **pMetaRsp) {
H
Hongze Cheng 已提交
697 698
  SMetaEntry  me = {0};
  SMetaReader mr = {0};
H
Hongze Cheng 已提交
699 700 701 702 703

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

706
  if (pReq->type == TSDB_CHILD_TABLE) {
707
    tb_uid_t suid = metaGetTableEntryUidByName(pMeta, pReq->ctb.stbName);
708 709 710 711 712 713
    if (suid != pReq->ctb.suid) {
      terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
      return -1;
    }
  }

H
Hongze Cheng 已提交
714
  // validate req
H
Hongze Cheng 已提交
715
  metaReaderInit(&mr, pMeta, 0);
H
Hongze Cheng 已提交
716
  if (metaGetTableEntryByName(&mr, pReq->name) == 0) {
717 718 719 720 721
    if (pReq->type == TSDB_CHILD_TABLE && pReq->ctb.suid != mr.me.ctbEntry.suid) {
      terrno = TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
      metaReaderClear(&mr);
      return -1;
    }
H
Hongze Cheng 已提交
722 723 724 725
    pReq->uid = mr.me.uid;
    if (pReq->type == TSDB_CHILD_TABLE) {
      pReq->ctb.suid = mr.me.ctbEntry.suid;
    }
H
Hongze Cheng 已提交
726 727 728
    terrno = TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
    metaReaderClear(&mr);
    return -1;
C
Cary Xu 已提交
729 730
  } else if (terrno == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
    terrno = TSDB_CODE_SUCCESS;
H
Hongze Cheng 已提交
731
  }
H
Hongze Cheng 已提交
732
  metaReaderClear(&mr);
H
Hongze Cheng 已提交
733 734

  // build SMetaEntry
735
  me.version = ver;
H
Hongze Cheng 已提交
736 737 738 739
  me.type = pReq->type;
  me.uid = pReq->uid;
  me.name = pReq->name;
  if (me.type == TSDB_CHILD_TABLE) {
740
    me.ctbEntry.btime = pReq->btime;
H
Hongze Cheng 已提交
741
    me.ctbEntry.ttlDays = pReq->ttl;
wmmhello's avatar
wmmhello 已提交
742
    me.ctbEntry.commentLen = pReq->commentLen;
wmmhello's avatar
wmmhello 已提交
743
    me.ctbEntry.comment = pReq->comment;
H
Hongze Cheng 已提交
744 745
    me.ctbEntry.suid = pReq->ctb.suid;
    me.ctbEntry.pTags = pReq->ctb.pTag;
746

wmmhello's avatar
wmmhello 已提交
747
#ifdef TAG_FILTER_DEBUG
dengyihao's avatar
dengyihao 已提交
748 749
    SArray *pTagVals = NULL;
    int32_t code = tTagToValArray((STag *)pReq->ctb.pTag, &pTagVals);
wmmhello's avatar
wmmhello 已提交
750
    for (int i = 0; i < taosArrayGetSize(pTagVals); i++) {
dengyihao's avatar
dengyihao 已提交
751
      STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
wmmhello's avatar
wmmhello 已提交
752 753

      if (IS_VAR_DATA_TYPE(pTagVal->type)) {
dengyihao's avatar
dengyihao 已提交
754
        char *buf = taosMemoryCalloc(pTagVal->nData + 1, 1);
wmmhello's avatar
wmmhello 已提交
755
        memcpy(buf, pTagVal->pData, pTagVal->nData);
dengyihao's avatar
dengyihao 已提交
756 757
        metaDebug("metaTag table:%s varchar index:%d cid:%d type:%d value:%s", pReq->name, i, pTagVal->cid,
                  pTagVal->type, buf);
wmmhello's avatar
wmmhello 已提交
758 759 760 761
        taosMemoryFree(buf);
      } else {
        double val = 0;
        GET_TYPED_DATA(val, double, pTagVal->type, &pTagVal->i64);
dengyihao's avatar
dengyihao 已提交
762 763
        metaDebug("metaTag table:%s number index:%d cid:%d type:%d value:%f", pReq->name, i, pTagVal->cid,
                  pTagVal->type, val);
wmmhello's avatar
wmmhello 已提交
764 765
      }
    }
wmmhello's avatar
wmmhello 已提交
766
#endif
wmmhello's avatar
wmmhello 已提交
767

768
    ++pMeta->pVnode->config.vndStats.numOfCTables;
769 770 771

    metaWLock(pMeta);
    metaUpdateStbStats(pMeta, me.ctbEntry.suid, 1);
772
    metaUidCacheClear(pMeta, me.ctbEntry.suid);
D
dapan1121 已提交
773
    metaTbGroupCacheClear(pMeta, me.ctbEntry.suid);
774
    metaULock(pMeta);
H
Hongze Cheng 已提交
775
  } else {
776
    me.ntbEntry.btime = pReq->btime;
H
Hongze Cheng 已提交
777
    me.ntbEntry.ttlDays = pReq->ttl;
wmmhello's avatar
wmmhello 已提交
778
    me.ntbEntry.commentLen = pReq->commentLen;
wmmhello's avatar
wmmhello 已提交
779
    me.ntbEntry.comment = pReq->comment;
780 781
    me.ntbEntry.schemaRow = pReq->ntb.schemaRow;
    me.ntbEntry.ncid = me.ntbEntry.schemaRow.pSchema[me.ntbEntry.schemaRow.nCols - 1].colId + 1;
782 783

    ++pMeta->pVnode->config.vndStats.numOfNTables;
784
    pMeta->pVnode->config.vndStats.numOfNTimeSeries += me.ntbEntry.schemaRow.nCols - 1;
H
more  
Hongze Cheng 已提交
785 786
  }

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

789 790 791 792 793 794 795 796 797 798
  if (pMetaRsp) {
    *pMetaRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));

    if (*pMetaRsp) {
      if (me.type == TSDB_CHILD_TABLE) {
        (*pMetaRsp)->tableType = TSDB_CHILD_TABLE;
        (*pMetaRsp)->tuid = pReq->uid;
        (*pMetaRsp)->suid = pReq->ctb.suid;
        strcpy((*pMetaRsp)->tbName, pReq->name);
      } else {
799
        metaUpdateMetaRsp(pReq->uid, pReq->name, &pReq->ntb.schemaRow, *pMetaRsp);
800 801 802 803
      }
    }
  }

S
Shengliang Guan 已提交
804
  metaDebug("vgId:%d, table:%s uid %" PRId64 " is created, type:%" PRId8, TD_VID(pMeta->pVnode), pReq->name, pReq->uid,
H
Hongze Cheng 已提交
805
            pReq->type);
H
refact  
Hongze Cheng 已提交
806
  return 0;
H
Hongze Cheng 已提交
807 808

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

814
int metaDropTable(SMeta *pMeta, int64_t version, SVDropTbReq *pReq, SArray *tbUids, tb_uid_t *tbUid) {
H
Hongze Cheng 已提交
815
  void    *pData = NULL;
H
Hongze Cheng 已提交
816 817 818 819
  int      nData = 0;
  int      rc = 0;
  tb_uid_t uid;
  int      type;
H
more  
Hongze Cheng 已提交
820

H
Hongze Cheng 已提交
821 822
  rc = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &pData, &nData);
  if (rc < 0) {
823
    terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
H
more  
Hongze Cheng 已提交
824 825
    return -1;
  }
H
Hongze Cheng 已提交
826 827
  uid = *(tb_uid_t *)pData;

H
Hongze Cheng 已提交
828 829 830
  metaWLock(pMeta);
  metaDropTableByUid(pMeta, uid, &type);
  metaULock(pMeta);
H
Hongze Cheng 已提交
831

832
  if ((type == TSDB_CHILD_TABLE || type == TSDB_NORMAL_TABLE) && tbUids) {
H
Hongze Cheng 已提交
833
    taosArrayPush(tbUids, &uid);
H
Hongze Cheng 已提交
834
  }
H
Hongze Cheng 已提交
835

836 837 838 839
  if ((type == TSDB_CHILD_TABLE) && tbUid) {
    *tbUid = uid;
  }

H
Hongze Cheng 已提交
840 841 842
  tdbFree(pData);
  return 0;
}
H
Hongze Cheng 已提交
843

844 845 846 847 848 849 850 851 852 853
static void metaDropTables(SMeta *pMeta, SArray *tbUids) {
  metaWLock(pMeta);
  for (int i = 0; i < TARRAY_SIZE(tbUids); ++i) {
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
    metaDropTableByUid(pMeta, uid, NULL);
    metaDebug("batch drop table:%" PRId64, uid);
  }
  metaULock(pMeta);
}

M
Minglei Jin 已提交
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
static int32_t metaFilterTableByHash(SMeta *pMeta, SArray *uidList) {
  int32_t code = 0;
  // 1, tranverse table's
  // 2, validate table name using vnodeValidateTableHash
  // 3, push invalidated table's uid into uidList

  TBC *pCur;
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
  if (code < 0) {
    return code;
  }

  code = tdbTbcMoveToFirst(pCur);
  if (code) {
    tdbTbcClose(pCur);
    return code;
  }

M
Minglei Jin 已提交
872 873
  void *pData = NULL, *pKey = NULL;
  int   nData = 0, nKey = 0;
M
Minglei Jin 已提交
874 875

  while (1) {
M
Minglei Jin 已提交
876
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
M
Minglei Jin 已提交
877 878 879 880 881 882 883 884
    if (ret < 0) {
      break;
    }

    SMetaEntry me = {0};
    SDecoder   dc = {0};
    tDecoderInit(&dc, pData, nData);
    metaDecodeEntry(&dc, &me);
885

M
Minglei Jin 已提交
886
    if (me.type != TSDB_SUPER_TABLE) {
887 888 889 890 891
      char tbFName[TSDB_TABLE_FNAME_LEN + 1];
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pMeta->pVnode->config.dbname, me.name);
      tbFName[TSDB_TABLE_FNAME_LEN] = '\0';
      int32_t ret = vnodeValidateTableHash(pMeta->pVnode, tbFName);
      if (ret < 0 && terrno == TSDB_CODE_VND_HASH_MISMATCH) {
M
Minglei Jin 已提交
892 893 894 895 896 897
        taosArrayPush(uidList, &me.uid);
      }
    }
    tDecoderClear(&dc);
  }
  tdbFree(pData);
M
Minglei Jin 已提交
898
  tdbFree(pKey);
M
Minglei Jin 已提交
899 900 901 902 903 904
  tdbTbcClose(pCur);

  return 0;
}

int32_t metaTrimTables(SMeta *pMeta) {
905 906 907 908 909 910 911
  int32_t code = 0;

  SArray *tbUids = taosArrayInit(8, sizeof(int64_t));
  if (tbUids == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

M
Minglei Jin 已提交
912
  code = metaFilterTableByHash(pMeta, tbUids);
913 914 915 916 917 918 919
  if (code != 0) {
    goto end;
  }
  if (TARRAY_SIZE(tbUids) == 0) {
    goto end;
  }

920
  metaInfo("vgId:%d, trim %ld tables", TD_VID(pMeta->pVnode), taosArrayGetSize(tbUids));
921 922 923 924 925 926 927 928
  metaDropTables(pMeta, tbUids);

end:
  taosArrayDestroy(tbUids);

  return code;
}

929 930
int metaTtlDropTable(SMeta *pMeta, int64_t timePointMs, SArray *tbUids) {
  int ret = ttlMgrFlush(pMeta->pTtlMgr, pMeta->txn);
H
Hongze Cheng 已提交
931
  if (ret != 0) {
932 933 934 935 936 937 938
    metaError("ttl failed to flush, ret:%d", ret);
    return ret;
  }

  ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids);
  if (ret != 0) {
    metaError("ttl failed to find expired table, ret:%d", ret);
939 940
    return ret;
  }
941
  if (TARRAY_SIZE(tbUids) == 0) {
942 943 944
    return 0;
  }

945
  metaInfo("ttl find expired table count: %zu", TARRAY_SIZE(tbUids));
946

947
  metaDropTables(pMeta, tbUids);
948 949 950
  return 0;
}

951 952
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
  int64_t btime;
953
  if (pME->type == TSDB_CHILD_TABLE) {
954
    btime = pME->ctbEntry.btime;
955
  } else if (pME->type == TSDB_NORMAL_TABLE) {
956
    btime = pME->ntbEntry.btime;
dengyihao's avatar
dengyihao 已提交
957 958 959 960
  } else {
    return -1;
  }

961 962
  btimeKey->btime = btime;
  btimeKey->uid = pME->uid;
dengyihao's avatar
dengyihao 已提交
963 964 965 966
  return 0;
}

static int metaBuildNColIdxKey(SNcolIdxKey *ncolKey, const SMetaEntry *pME) {
dengyihao's avatar
dengyihao 已提交
967 968 969 970 971 972
  if (pME->type == TSDB_NORMAL_TABLE) {
    ncolKey->ncol = pME->ntbEntry.schemaRow.nCols;
    ncolKey->uid = pME->uid;
  } else {
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
973 974
  return 0;
}
975

976 977 978
static int metaDeleteTtl(SMeta *pMeta, const SMetaEntry *pME) {
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
  return ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
979 980
}

H
Hongze Cheng 已提交
981
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type) {
H
Hongze Cheng 已提交
982
  void      *pData = NULL;
H
Hongze Cheng 已提交
983 984 985 986 987 988
  int        nData = 0;
  int        rc = 0;
  SMetaEntry e = {0};
  SDecoder   dc = {0};

  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
989 990 991
  if (rc < 0) {
    return -1;
  }
H
Hongze Cheng 已提交
992
  int64_t version = ((SUidIdxVal *)pData)[0].version;
H
Hongze Cheng 已提交
993 994 995 996

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

  tDecoderInit(&dc, pData, nData);
M
Minglei Jin 已提交
997 998 999 1000 1001
  rc = metaDecodeEntry(&dc, &e);
  if (rc < 0) {
    tDecoderClear(&dc);
    return -1;
  }
H
Hongze Cheng 已提交
1002 1003 1004

  if (type) *type = e.type;

1005 1006 1007 1008 1009
  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) {
H
Hongze Cheng 已提交
1010
      STbDbKey tbDbKey = {.uid = e.ctbEntry.suid, .version = ((SUidIdxVal *)tData)[0].version};
1011 1012 1013 1014 1015 1016
      if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &tData, &tLen) == 0) {
        SDecoder   tdc = {0};
        SMetaEntry stbEntry = {0};

        tDecoderInit(&tdc, tData, tLen);
        metaDecodeEntry(&tdc, &stbEntry);
dengyihao's avatar
dengyihao 已提交
1017 1018 1019 1020 1021

        SSchema        *pTagColumn = NULL;
        SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
        if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
          pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
1022
          metaDelJsonVarFromIdx(pMeta, &e, pTagColumn);
dengyihao's avatar
dengyihao 已提交
1023
        } else {
dengyihao's avatar
dengyihao 已提交
1024 1025
          for (int i = 0; i < pTagSchema->nCols; i++) {
            pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[i];
dengyihao's avatar
dengyihao 已提交
1026
            if (!IS_IDX_ON(pTagColumn)) continue;
dengyihao's avatar
dengyihao 已提交
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
            STagIdxKey *pTagIdxKey = NULL;
            int32_t     nTagIdxKey;

            const void *pTagData = NULL;
            int32_t     nTagData = 0;

            STagVal tagVal = {.cid = pTagColumn->colId};
            tTagGet((const STag *)e.ctbEntry.pTags, &tagVal);
            if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
              pTagData = tagVal.pData;
              nTagData = (int32_t)tagVal.nData;
            } else {
              pTagData = &(tagVal.i64);
              nTagData = tDataTypes[pTagColumn->type].bytes;
            }

            if (metaCreateTagIdxKey(e.ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type, uid,
                                    &pTagIdxKey, &nTagIdxKey) == 0) {
              tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
            }
            metaDestroyTagIdxKey(pTagIdxKey);
dengyihao's avatar
dengyihao 已提交
1048
          }
1049 1050 1051 1052 1053 1054 1055
        }
        tDecoderClear(&tdc);
      }
      tdbFree(tData);
    }
  }

1056 1057 1058
  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);
1059

1060
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
dengyihao's avatar
dengyihao 已提交
1061 1062
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);

1063
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
C
Cary Xu 已提交
1064

H
Hongze Cheng 已提交
1065
  if (e.type == TSDB_CHILD_TABLE) {
1066
    tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
1067 1068

    --pMeta->pVnode->config.vndStats.numOfCTables;
1069 1070

    metaUpdateStbStats(pMeta, e.ctbEntry.suid, -1);
1071
    metaUidCacheClear(pMeta, e.ctbEntry.suid);
M
Minglei Jin 已提交
1072
    metaTbGroupCacheClear(pMeta, e.ctbEntry.suid);
H
Hongze Cheng 已提交
1073 1074
  } else if (e.type == TSDB_NORMAL_TABLE) {
    // drop schema.db (todo)
1075 1076

    --pMeta->pVnode->config.vndStats.numOfNTables;
1077
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
H
Hongze Cheng 已提交
1078
  } else if (e.type == TSDB_SUPER_TABLE) {
1079
    tdbTbDelete(pMeta->pSuidIdx, &e.uid, sizeof(tb_uid_t), pMeta->txn);
H
Hongze Cheng 已提交
1080
    // drop schema.db (todo)
1081

1082
    metaStatsCacheDrop(pMeta, uid);
1083
    metaUidCacheClear(pMeta, uid);
M
Minglei Jin 已提交
1084
    metaTbGroupCacheClear(pMeta, uid);
1085
    --pMeta->pVnode->config.vndStats.numOfSTables;
H
Hongze Cheng 已提交
1086 1087
  }

H
Hongze Cheng 已提交
1088 1089
  metaCacheDrop(pMeta, uid);

H
Hongze Cheng 已提交
1090 1091
  tDecoderClear(&dc);
  tdbFree(pData);
H
Hongze Cheng 已提交
1092

H
refact  
Hongze Cheng 已提交
1093 1094
  return 0;
}
dengyihao's avatar
dengyihao 已提交
1095
// opt ins_tables
1096 1097 1098
int metaUpdateBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
  SBtimeIdxKey btimeKey = {0};
  if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) {
dengyihao's avatar
dengyihao 已提交
1099 1100
    return 0;
  }
1101 1102
  metaTrace("vgId:%d, start to save version:%" PRId64 " uid:%" PRId64 " btime:%" PRId64, TD_VID(pMeta->pVnode),
            pME->version, pME->uid, btimeKey.btime);
1103

1104
  return tdbTbUpsert(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), NULL, 0, pMeta->txn);
dengyihao's avatar
dengyihao 已提交
1105 1106
}

1107 1108 1109
int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
  SBtimeIdxKey btimeKey = {0};
  if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) {
dengyihao's avatar
dengyihao 已提交
1110 1111
    return 0;
  }
1112
  return tdbTbDelete(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), pMeta->txn);
dengyihao's avatar
dengyihao 已提交
1113 1114 1115 1116 1117 1118
}
int metaUpdateNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
  SNcolIdxKey ncolKey = {0};
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
    return 0;
  }
1119
  return tdbTbUpsert(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), NULL, 0, pMeta->txn);
dengyihao's avatar
dengyihao 已提交
1120 1121 1122 1123 1124 1125 1126
}

int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
  SNcolIdxKey ncolKey = {0};
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
    return 0;
  }
1127
  return tdbTbDelete(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), pMeta->txn);
dengyihao's avatar
dengyihao 已提交
1128
}
H
Hongze Cheng 已提交
1129

D
dapan1121 已提交
1130
static int metaAlterTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq, STableMetaRsp *pMetaRsp) {
H
Hongze Cheng 已提交
1131
  void           *pVal = NULL;
H
Hongze Cheng 已提交
1132
  int             nVal = 0;
H
Hongze Cheng 已提交
1133
  const void     *pData = NULL;
H
Hongze Cheng 已提交
1134 1135 1136 1137
  int             nData = 0;
  int             ret = 0;
  tb_uid_t        uid;
  int64_t         oversion;
H
Hongze Cheng 已提交
1138
  SSchema        *pColumn = NULL;
H
Hongze Cheng 已提交
1139 1140 1141 1142
  SMetaEntry      entry = {0};
  SSchemaWrapper *pSchema;
  int             c;

H
Hongze Cheng 已提交
1143
  if (pAlterTbReq->colName == NULL) {
H
Hongze Cheng 已提交
1144 1145 1146 1147
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }

H
Hongze Cheng 已提交
1148
  // search name index
H
Hongze Cheng 已提交
1149
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
H
Hongze Cheng 已提交
1150
  if (ret < 0) {
1151
    terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
H
Hongze Cheng 已提交
1152 1153 1154 1155 1156 1157 1158 1159
    return -1;
  }

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

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

1162
  tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, NULL);
H
Hongze Cheng 已提交
1163
  tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c);
1164
  if (c != 0) {
1165
    tdbTbcClose(pUidIdxc);
1166 1167 1168
    metaError("meta/table: invalide c: %" PRId32 " alt tb column failed.", c);
    return -1;
  }
H
Hongze Cheng 已提交
1169

H
Hongze Cheng 已提交
1170
  tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
1171
  oversion = ((SUidIdxVal *)pData)[0].version;
H
Hongze Cheng 已提交
1172 1173

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

1176
  tdbTbcOpen(pMeta->pTbDb, &pTbDbc, NULL);
H
Hongze Cheng 已提交
1177
  tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c);
1178
  if (c != 0) {
1179 1180
    tdbTbcClose(pUidIdxc);
    tdbTbcClose(pTbDbc);
1181 1182 1183 1184
    metaError("meta/table: invalide c: %" PRId32 " alt tb column failed.", c);
    return -1;
  }

H
Hongze Cheng 已提交
1185
  tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
1186 1187 1188

  // get table entry
  SDecoder dc = {0};
H
Hongze Cheng 已提交
1189 1190 1191
  entry.pBuf = taosMemoryMalloc(nData);
  memcpy(entry.pBuf, pData, nData);
  tDecoderInit(&dc, entry.pBuf, nData);
H
Hongze Cheng 已提交
1192
  ret = metaDecodeEntry(&dc, &entry);
1193
  if (ret != 0) {
1194 1195
    tdbTbcClose(pUidIdxc);
    tdbTbcClose(pTbDbc);
1196 1197 1198 1199
    tDecoderClear(&dc);
    metaError("meta/table: invalide ret: %" PRId32 " alt tb column failed.", ret);
    return -1;
  }
H
Hongze Cheng 已提交
1200 1201 1202 1203 1204 1205

  if (entry.type != TSDB_NORMAL_TABLE) {
    terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
    goto _err;
  }
  // search the column to add/drop/update
1206
  pSchema = &entry.ntbEntry.schemaRow;
H
Hongze Cheng 已提交
1207

dengyihao's avatar
dengyihao 已提交
1208 1209 1210 1211
  // save old entry
  SMetaEntry oldEntry = {.type = TSDB_NORMAL_TABLE, .uid = entry.uid};
  oldEntry.ntbEntry.schemaRow.nCols = pSchema->nCols;

H
Hongze Cheng 已提交
1212 1213 1214 1215 1216 1217 1218
  int32_t iCol = 0;
  for (;;) {
    pColumn = NULL;

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

1219 1220 1221 1222 1223
    if (NULL == pAlterTbReq->colName) {
      metaError("meta/table: null pAlterTbReq->colName");
      return -1;
    }

H
Hongze Cheng 已提交
1224 1225 1226 1227 1228
    if (strcmp(pColumn->name, pAlterTbReq->colName) == 0) break;
    iCol++;
  }

  entry.version = version;
H
Hongze Cheng 已提交
1229 1230
  int      tlen;
  SSchema *pNewSchema = NULL;
H
Hongze Cheng 已提交
1231 1232 1233 1234 1235 1236
  switch (pAlterTbReq->action) {
    case TSDB_ALTER_TABLE_ADD_COLUMN:
      if (pColumn) {
        terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
        goto _err;
      }
1237
      pSchema->version++;
H
Hongze Cheng 已提交
1238
      pSchema->nCols++;
H
Hongze Cheng 已提交
1239 1240 1241
      pNewSchema = taosMemoryMalloc(sizeof(SSchema) * pSchema->nCols);
      memcpy(pNewSchema, pSchema->pSchema, sizeof(SSchema) * (pSchema->nCols - 1));
      pSchema->pSchema = pNewSchema;
1242 1243 1244 1245 1246
      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);
1247 1248

      ++pMeta->pVnode->config.vndStats.numOfNTimeSeries;
H
Hongze Cheng 已提交
1249 1250 1251
      break;
    case TSDB_ALTER_TABLE_DROP_COLUMN:
      if (pColumn == NULL) {
1252
        terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
H
Hongze Cheng 已提交
1253 1254 1255 1256 1257 1258
        goto _err;
      }
      if (pColumn->colId == 0) {
        terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
        goto _err;
      }
L
Liu Jicong 已提交
1259 1260 1261 1262
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
        goto _err;
      }
1263
      pSchema->version++;
H
Hongze Cheng 已提交
1264 1265 1266 1267
      tlen = (pSchema->nCols - iCol - 1) * sizeof(SSchema);
      if (tlen) {
        memmove(pColumn, pColumn + 1, tlen);
      }
H
Hongze Cheng 已提交
1268
      pSchema->nCols--;
1269 1270

      --pMeta->pVnode->config.vndStats.numOfNTimeSeries;
H
Hongze Cheng 已提交
1271 1272 1273
      break;
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
      if (pColumn == NULL) {
1274
        terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
H
Hongze Cheng 已提交
1275 1276
        goto _err;
      }
H
Hongze Cheng 已提交
1277
      if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes > pAlterTbReq->colModBytes) {
H
Hongze Cheng 已提交
1278 1279 1280
        terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
        goto _err;
      }
L
Liu Jicong 已提交
1281 1282 1283 1284
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
        goto _err;
      }
1285
      pSchema->version++;
H
Hongze Cheng 已提交
1286
      pColumn->bytes = pAlterTbReq->colModBytes;
H
Hongze Cheng 已提交
1287 1288
      break;
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
H
Hongze Cheng 已提交
1289 1290 1291 1292
      if (pAlterTbReq->colNewName == NULL) {
        terrno = TSDB_CODE_INVALID_MSG;
        goto _err;
      }
H
Hongze Cheng 已提交
1293
      if (pColumn == NULL) {
1294
        terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
H
Hongze Cheng 已提交
1295 1296
        goto _err;
      }
L
Liu Jicong 已提交
1297 1298 1299 1300
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
        goto _err;
      }
1301
      pSchema->version++;
H
Hongze Cheng 已提交
1302 1303 1304 1305 1306 1307
      strcpy(pColumn->name, pAlterTbReq->colNewName);
      break;
  }

  entry.version = version;

H
Hongze Cheng 已提交
1308 1309 1310
  // do actual write
  metaWLock(pMeta);

dengyihao's avatar
fix RC  
dengyihao 已提交
1311 1312
  metaDeleteNcolIdx(pMeta, &oldEntry);
  metaUpdateNcolIdx(pMeta, &entry);
H
Hongze Cheng 已提交
1313 1314 1315
  // save to table db
  metaSaveToTbDb(pMeta, &entry);

H
Hongze Cheng 已提交
1316
  metaUpdateUidIdx(pMeta, &entry);
H
Hongze Cheng 已提交
1317 1318 1319 1320 1321

  metaSaveToSkmDb(pMeta, &entry);

  metaULock(pMeta);

1322 1323
  metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs);

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

wmmhello's avatar
wmmhello 已提交
1326
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
H
Hongze Cheng 已提交
1327
  if (pNewSchema) taosMemoryFree(pNewSchema);
H
Hongze Cheng 已提交
1328 1329
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
1330 1331
  tDecoderClear(&dc);

H
Hongze Cheng 已提交
1332
  return 0;
H
Hongze Cheng 已提交
1333 1334

_err:
wmmhello's avatar
wmmhello 已提交
1335
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
H
Hongze Cheng 已提交
1336 1337
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
1338 1339
  tDecoderClear(&dc);

H
Hongze Cheng 已提交
1340
  return -1;
H
Hongze Cheng 已提交
1341 1342 1343
}

static int metaUpdateTableTagVal(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
H
Hongze Cheng 已提交
1344 1345
  SMetaEntry  ctbEntry = {0};
  SMetaEntry  stbEntry = {0};
H
Hongze Cheng 已提交
1346
  void       *pVal = NULL;
H
Hongze Cheng 已提交
1347 1348 1349 1350 1351 1352 1353 1354
  int         nVal = 0;
  int         ret;
  int         c;
  tb_uid_t    uid;
  int64_t     oversion;
  const void *pData = NULL;
  int         nData = 0;

H
Hongze Cheng 已提交
1355 1356 1357 1358 1359
  if (pAlterTbReq->tagName == NULL) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }

H
Hongze Cheng 已提交
1360
  // search name index
H
Hongze Cheng 已提交
1361
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
H
Hongze Cheng 已提交
1362
  if (ret < 0) {
1363
    terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
H
Hongze Cheng 已提交
1364 1365 1366 1367 1368 1369 1370 1371
    return -1;
  }

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

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

1374
  tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, NULL);
H
Hongze Cheng 已提交
1375
  tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c);
1376
  if (c != 0) {
1377 1378
    tdbTbcClose(pUidIdxc);
    terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
1379 1380 1381
    metaError("meta/table: invalide c: %" PRId32 " update tb tag val failed.", c);
    return -1;
  }
H
Hongze Cheng 已提交
1382

H
Hongze Cheng 已提交
1383
  tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
1384
  oversion = ((SUidIdxVal *)pData)[0].version;
H
Hongze Cheng 已提交
1385 1386

  // search table.db
H
Hongze Cheng 已提交
1387
  TBC     *pTbDbc = NULL;
H
Hongze Cheng 已提交
1388 1389
  SDecoder dc1 = {0};
  SDecoder dc2 = {0};
H
Hongze Cheng 已提交
1390

H
Hongze Cheng 已提交
1391
  /* get ctbEntry */
1392
  tdbTbcOpen(pMeta->pTbDb, &pTbDbc, NULL);
H
Hongze Cheng 已提交
1393
  tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c);
1394
  if (c != 0) {
1395 1396 1397
    tdbTbcClose(pUidIdxc);
    tdbTbcClose(pTbDbc);
    terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
1398 1399 1400 1401
    metaError("meta/table: invalide c: %" PRId32 " update tb tag val failed.", c);
    return -1;
  }

H
Hongze Cheng 已提交
1402
  tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
1403

H
Hongze Cheng 已提交
1404 1405
  ctbEntry.pBuf = taosMemoryMalloc(nData);
  memcpy(ctbEntry.pBuf, pData, nData);
H
Hongze Cheng 已提交
1406 1407
  tDecoderInit(&dc1, ctbEntry.pBuf, nData);
  metaDecodeEntry(&dc1, &ctbEntry);
H
Hongze Cheng 已提交
1408

H
Hongze Cheng 已提交
1409
  /* get stbEntry*/
H
Hongze Cheng 已提交
1410
  tdbTbGet(pMeta->pUidIdx, &ctbEntry.ctbEntry.suid, sizeof(tb_uid_t), &pVal, &nVal);
1411 1412 1413 1414 1415
  if (!pVal) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto _err;
  }

H
Hongze Cheng 已提交
1416 1417
  tdbTbGet(pMeta->pTbDb, &((STbDbKey){.uid = ctbEntry.ctbEntry.suid, .version = ((SUidIdxVal *)pVal)[0].version}),
           sizeof(STbDbKey), (void **)&stbEntry.pBuf, &nVal);
H
Hongze Cheng 已提交
1418
  tdbFree(pVal);
H
Hongze Cheng 已提交
1419 1420
  tDecoderInit(&dc2, stbEntry.pBuf, nVal);
  metaDecodeEntry(&dc2, &stbEntry);
H
Hongze Cheng 已提交
1421 1422

  SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
H
Hongze Cheng 已提交
1423
  SSchema        *pColumn = NULL;
H
Hongze Cheng 已提交
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
  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) {
1436
    terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
H
Hongze Cheng 已提交
1437 1438
    goto _err;
  }
H
Hongze Cheng 已提交
1439

H
Hongze Cheng 已提交
1440
  ctbEntry.version = version;
H
Hongze Cheng 已提交
1441
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
wmmhello's avatar
wmmhello 已提交
1442
    ctbEntry.ctbEntry.pTags = taosMemoryMalloc(pAlterTbReq->nTagVal);
H
Hongze Cheng 已提交
1443
    if (ctbEntry.ctbEntry.pTags == NULL) {
wmmhello's avatar
wmmhello 已提交
1444 1445 1446
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
1447 1448
    memcpy((void *)ctbEntry.ctbEntry.pTags, pAlterTbReq->pTagVal, pAlterTbReq->nTagVal);
  } else {
C
Cary Xu 已提交
1449
    const STag *pOldTag = (const STag *)ctbEntry.ctbEntry.pTags;
H
Hongze Cheng 已提交
1450 1451
    STag       *pNewTag = NULL;
    SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
C
Cary Xu 已提交
1452
    if (!pTagArray) {
C
Cary Xu 已提交
1453 1454 1455
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
wmmhello's avatar
wmmhello 已提交
1456 1457 1458
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
      SSchema *pCol = &pTagSchema->pSchema[i];
      if (iCol == i) {
1459 1460 1461
        if (pAlterTbReq->isNull) {
          continue;
        }
wmmhello's avatar
wmmhello 已提交
1462 1463 1464 1465 1466 1467
        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;
1468
        } else {
wmmhello's avatar
wmmhello 已提交
1469 1470 1471
          memcpy(&val.i64, pAlterTbReq->pTagVal, pAlterTbReq->nTagVal);
        }
        taosArrayPush(pTagArray, &val);
wmmhello's avatar
wmmhello 已提交
1472
      } else {
wmmhello's avatar
wmmhello 已提交
1473
        STagVal val = {.cid = pCol->colId};
wmmhello's avatar
wmmhello 已提交
1474 1475
        if (tTagGet(pOldTag, &val)) {
          taosArrayPush(pTagArray, &val);
H
Hongze Cheng 已提交
1476 1477 1478
        }
      }
    }
C
Cary Xu 已提交
1479 1480
    if ((terrno = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag)) < 0) {
      taosArrayDestroy(pTagArray);
C
Cary Xu 已提交
1481 1482 1483
      goto _err;
    }
    ctbEntry.ctbEntry.pTags = (uint8_t *)pNewTag;
C
Cary Xu 已提交
1484
    taosArrayDestroy(pTagArray);
wmmhello's avatar
wmmhello 已提交
1485
  }
H
Hongze Cheng 已提交
1486

1487 1488
  metaWLock(pMeta);

H
Hongze Cheng 已提交
1489 1490 1491 1492
  // save to table.db
  metaSaveToTbDb(pMeta, &ctbEntry);

  // save to uid.idx
H
Hongze Cheng 已提交
1493
  metaUpdateUidIdx(pMeta, &ctbEntry);
H
Hongze Cheng 已提交
1494

dengyihao's avatar
dengyihao 已提交
1495
  metaUpdateTagIdx(pMeta, &ctbEntry);
dengyihao's avatar
dengyihao 已提交
1496

1497 1498 1499 1500 1501
  if (NULL == ctbEntry.ctbEntry.pTags) {
    metaError("meta/table: null tags, update tag val failed.");
    goto _err;
  }

1502
  SCtbIdxKey ctbIdxKey = {.suid = ctbEntry.ctbEntry.suid, .uid = uid};
H
Hongze Cheng 已提交
1503
  tdbTbUpsert(pMeta->pCtbIdx, &ctbIdxKey, sizeof(ctbIdxKey), ctbEntry.ctbEntry.pTags,
1504
              ((STag *)(ctbEntry.ctbEntry.pTags))->len, pMeta->txn);
1505

1506
  metaUidCacheClear(pMeta, ctbEntry.ctbEntry.suid);
M
Minglei Jin 已提交
1507
  metaTbGroupCacheClear(pMeta, ctbEntry.ctbEntry.suid);
1508

1509 1510
  metaULock(pMeta);

1511 1512
  metaUpdateChangeTime(pMeta, ctbEntry.uid, pAlterTbReq->ctimeMs);

H
Hongze Cheng 已提交
1513 1514
  tDecoderClear(&dc1);
  tDecoderClear(&dc2);
M
Minglei Jin 已提交
1515
  taosMemoryFree((void *)ctbEntry.ctbEntry.pTags);
H
Hongze Cheng 已提交
1516 1517
  if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
  if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
H
Hongze Cheng 已提交
1518 1519
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
H
Hongze Cheng 已提交
1520
  return 0;
H
Hongze Cheng 已提交
1521 1522

_err:
H
Hongze Cheng 已提交
1523 1524
  tDecoderClear(&dc1);
  tDecoderClear(&dc2);
H
Hongze Cheng 已提交
1525 1526
  if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
  if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
H
Hongze Cheng 已提交
1527 1528
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
H
Hongze Cheng 已提交
1529
  return -1;
H
Hongze Cheng 已提交
1530 1531 1532
}

static int metaUpdateTableOptions(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
H
Hongze Cheng 已提交
1533 1534 1535 1536 1537 1538 1539 1540 1541
  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 已提交
1542 1543 1544 1545

  // search name index
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
  if (ret < 0) {
1546
    terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
wmmhello's avatar
wmmhello 已提交
1547
    return -1;
1548
  }
wmmhello's avatar
wmmhello 已提交
1549 1550 1551 1552 1553 1554 1555 1556

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

  // search uid index
  TBC *pUidIdxc = NULL;

1557
  tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, NULL);
wmmhello's avatar
wmmhello 已提交
1558
  tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c);
1559
  if (c != 0) {
1560
    tdbTbcClose(pUidIdxc);
1561 1562 1563
    metaError("meta/table: invalide c: %" PRId32 " update tb options failed.", c);
    return -1;
  }
wmmhello's avatar
wmmhello 已提交
1564 1565

  tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
1566
  oversion = ((SUidIdxVal *)pData)[0].version;
wmmhello's avatar
wmmhello 已提交
1567 1568 1569 1570

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

1571
  tdbTbcOpen(pMeta->pTbDb, &pTbDbc, NULL);
wmmhello's avatar
wmmhello 已提交
1572
  tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c);
1573
  if (c != 0) {
1574 1575
    tdbTbcClose(pUidIdxc);
    tdbTbcClose(pTbDbc);
1576 1577 1578 1579
    metaError("meta/table: invalide c: %" PRId32 " update tb options failed.", c);
    return -1;
  }

wmmhello's avatar
wmmhello 已提交
1580 1581 1582 1583 1584 1585 1586 1587
  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);
1588 1589
  if (ret != 0) {
    tDecoderClear(&dc);
1590 1591
    tdbTbcClose(pUidIdxc);
    tdbTbcClose(pTbDbc);
1592 1593 1594
    metaError("meta/table: invalide ret: %" PRId32 " alt tb options failed.", ret);
    return -1;
  }
wmmhello's avatar
wmmhello 已提交
1595 1596 1597 1598 1599

  entry.version = version;
  metaWLock(pMeta);
  // build SMetaEntry
  if (entry.type == TSDB_CHILD_TABLE) {
H
Hongze Cheng 已提交
1600
    if (pAlterTbReq->updateTTL) {
1601
      metaDeleteTtl(pMeta, &entry);
wmmhello's avatar
wmmhello 已提交
1602
      entry.ctbEntry.ttlDays = pAlterTbReq->newTTL;
1603
      metaUpdateTtl(pMeta, &entry);
wmmhello's avatar
wmmhello 已提交
1604
    }
H
Hongze Cheng 已提交
1605
    if (pAlterTbReq->newCommentLen >= 0) {
wmmhello's avatar
wmmhello 已提交
1606 1607 1608
      entry.ctbEntry.commentLen = pAlterTbReq->newCommentLen;
      entry.ctbEntry.comment = pAlterTbReq->newComment;
    }
wmmhello's avatar
wmmhello 已提交
1609
  } else {
H
Hongze Cheng 已提交
1610
    if (pAlterTbReq->updateTTL) {
1611
      metaDeleteTtl(pMeta, &entry);
wmmhello's avatar
wmmhello 已提交
1612
      entry.ntbEntry.ttlDays = pAlterTbReq->newTTL;
1613
      metaUpdateTtl(pMeta, &entry);
wmmhello's avatar
wmmhello 已提交
1614
    }
H
Hongze Cheng 已提交
1615
    if (pAlterTbReq->newCommentLen >= 0) {
wmmhello's avatar
wmmhello 已提交
1616 1617 1618
      entry.ntbEntry.commentLen = pAlterTbReq->newCommentLen;
      entry.ntbEntry.comment = pAlterTbReq->newComment;
    }
1619
  }
wmmhello's avatar
wmmhello 已提交
1620 1621 1622

  // save to table db
  metaSaveToTbDb(pMeta, &entry);
H
Hongze Cheng 已提交
1623
  metaUpdateUidIdx(pMeta, &entry);
wmmhello's avatar
wmmhello 已提交
1624 1625
  metaULock(pMeta);

1626 1627
  metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs);

wmmhello's avatar
wmmhello 已提交
1628 1629
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
1630
  tDecoderClear(&dc);
wmmhello's avatar
wmmhello 已提交
1631
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
H
Hongze Cheng 已提交
1632 1633 1634
  return 0;
}

dengyihao's avatar
dengyihao 已提交
1635 1636 1637 1638 1639 1640
static int metaAddTagIndex(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
  SMetaEntry  stbEntry = {0};
  void       *pVal = NULL;
  int         nVal = 0;
  int         ret;
  int         c;
dengyihao's avatar
dengyihao 已提交
1641
  tb_uid_t    uid, suid;
dengyihao's avatar
dengyihao 已提交
1642 1643 1644
  int64_t     oversion;
  const void *pData = NULL;
  int         nData = 0;
dengyihao's avatar
dengyihao 已提交
1645
  SDecoder    dc = {0};
dengyihao's avatar
dengyihao 已提交
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656

  if (pAlterTbReq->tagName == NULL) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }

  // search name index
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
  if (ret < 0) {
    terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
    return -1;
Y
yihaoDeng 已提交
1657 1658 1659 1660
  } else {
    uid = *(tb_uid_t *)pVal;
    tdbFree(pVal);
    pVal = NULL;
dengyihao's avatar
dengyihao 已提交
1661 1662
  }

dengyihao's avatar
dengyihao 已提交
1663 1664 1665 1666
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(tb_uid_t), &pVal, &nVal) == -1) {
    ret = -1;
    goto _err;
  }
dengyihao's avatar
dengyihao 已提交
1667
  suid = ((SUidIdxVal *)pVal)[0].suid;
dengyihao's avatar
dengyihao 已提交
1668

dengyihao's avatar
dengyihao 已提交
1669
  STbDbKey tbDbKey = {0};
dengyihao's avatar
dengyihao 已提交
1670
  tbDbKey.uid = suid;
dengyihao's avatar
dengyihao 已提交
1671 1672 1673 1674 1675 1676 1677
  tbDbKey.version = ((SUidIdxVal *)pVal)[0].version;
  tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pVal, &nVal);
  tDecoderInit(&dc, pVal, nVal);
  ret = metaDecodeEntry(&dc, &stbEntry);
  if (ret < 0) {
    goto _err;
  }
dengyihao's avatar
dengyihao 已提交
1678

dengyihao's avatar
dengyihao 已提交
1679
  // Get target schema info
dengyihao's avatar
dengyihao 已提交
1680 1681 1682
  SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
    terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
dengyihao's avatar
dengyihao 已提交
1683 1684
    goto _err;
  }
dengyihao's avatar
dengyihao 已提交
1685 1686
  SSchema *pCol = NULL;
  int32_t  iCol = 0;
dengyihao's avatar
dengyihao 已提交
1687
  for (;;) {
dengyihao's avatar
dengyihao 已提交
1688
    pCol = NULL;
dengyihao's avatar
dengyihao 已提交
1689
    if (iCol >= pTagSchema->nCols) break;
dengyihao's avatar
dengyihao 已提交
1690 1691
    pCol = &pTagSchema->pSchema[iCol];
    if (strcmp(pCol->name, pAlterTbReq->tagName) == 0) break;
dengyihao's avatar
dengyihao 已提交
1692 1693 1694
    iCol++;
  }

dengyihao's avatar
dengyihao 已提交
1695 1696
  if (iCol == 0) {
    terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
dengyihao's avatar
dengyihao 已提交
1697 1698
    goto _err;
  }
dengyihao's avatar
dengyihao 已提交
1699 1700
  if (pCol == NULL) {
    terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
dengyihao's avatar
dengyihao 已提交
1701 1702
    goto _err;
  }
dengyihao's avatar
dengyihao 已提交
1703

dengyihao's avatar
dengyihao 已提交
1704 1705 1706 1707 1708
  /*
   * iterator all pTdDbc by uid and version
   */
  TBC *pCtbIdxc = NULL;
  tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL);
dengyihao's avatar
dengyihao 已提交
1709
  int rc = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c);
dengyihao's avatar
dengyihao 已提交
1710 1711
  if (rc < 0) {
    tdbTbcClose(pCtbIdxc);
dengyihao's avatar
dengyihao 已提交
1712 1713
    goto _err;
  }
dengyihao's avatar
dengyihao 已提交
1714 1715 1716 1717 1718 1719
  for (;;) {
    void *pKey, *pVal;
    int   nKey, nVal;
    rc = tdbTbcNext(pCtbIdxc, &pKey, &nKey, &pVal, &nVal);
    if (rc < 0) break;
    if (((SCtbIdxKey *)pKey)->suid != uid) {
dengyihao's avatar
dengyihao 已提交
1720
      tdbFree(pKey);
dengyihao's avatar
dengyihao 已提交
1721 1722 1723
      tdbFree(pVal);
      continue;
    }
dengyihao's avatar
dengyihao 已提交
1724 1725
    STagIdxKey *pTagIdxKey = NULL;
    int32_t     nTagIdxKey;
dengyihao's avatar
dengyihao 已提交
1726

dengyihao's avatar
dengyihao 已提交
1727 1728
    const void *pTagData = NULL;
    int32_t     nTagData = 0;
dengyihao's avatar
dengyihao 已提交
1729

dengyihao's avatar
dengyihao 已提交
1730 1731 1732 1733 1734 1735 1736 1737 1738
    STagVal tagVal = {.cid = pCol->colId};
    tTagGet((const STag *)pVal, &tagVal);
    if (IS_VAR_DATA_TYPE(pCol->type)) {
      pTagData = tagVal.pData;
      nTagData = (int32_t)tagVal.nData;
    } else {
      pTagData = &(tagVal.i64);
      nTagData = tDataTypes[pCol->type].bytes;
    }
dengyihao's avatar
dengyihao 已提交
1739
    if (metaCreateTagIdxKey(suid, pCol->colId, pTagData, nTagData, pCol->type, uid, &pTagIdxKey, &nTagIdxKey) < 0) {
Y
yihaoDeng 已提交
1740 1741
      tdbFree(pKey);
      tdbFree(pVal);
dengyihao's avatar
dengyihao 已提交
1742
      metaDestroyTagIdxKey(pTagIdxKey);
Y
yihaoDeng 已提交
1743
      tdbTbcClose(pCtbIdxc);
dengyihao's avatar
dengyihao 已提交
1744 1745 1746 1747 1748
      goto _err;
    }
    tdbTbUpsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn);
    metaDestroyTagIdxKey(pTagIdxKey);
  }
Y
yihaoDeng 已提交
1749
  tdbTbcClose(pCtbIdxc);
dengyihao's avatar
dengyihao 已提交
1750
  return 0;
dengyihao's avatar
dengyihao 已提交
1751 1752

_err:
dengyihao's avatar
dengyihao 已提交
1753 1754 1755 1756 1757 1758
  // tDecoderClear(&dc1);
  // tDecoderClear(&dc2);
  // if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
  // if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
  // tdbTbcClose(pTbDbc);
  // tdbTbcClose(pUidIdxc);
dengyihao's avatar
dengyihao 已提交
1759
  return -1;
dengyihao's avatar
dengyihao 已提交
1760
}
dengyihao's avatar
dengyihao 已提交
1761

dengyihao's avatar
dengyihao 已提交
1762 1763 1764 1765 1766
typedef struct SMetaPair {
  void *key;
  int   nkey;
} SMetaPair;

dengyihao's avatar
dengyihao 已提交
1767 1768 1769 1770 1771 1772
static int metaDropTagIndex(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
  SMetaEntry  stbEntry = {0};
  void       *pVal = NULL;
  int         nVal = 0;
  int         ret;
  int         c;
dengyihao's avatar
dengyihao 已提交
1773
  tb_uid_t    suid;
dengyihao's avatar
dengyihao 已提交
1774 1775 1776
  int64_t     oversion;
  const void *pData = NULL;
  int         nData = 0;
dengyihao's avatar
dengyihao 已提交
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
  SDecoder    dc = {0};

  if (pAlterTbReq->tagName == NULL) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }

  // search name index
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
  if (ret < 0) {
    terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
    return -1;
  }
  suid = *(tb_uid_t *)pVal;
  tdbFree(pVal);
  pVal = NULL;

  if (tdbTbGet(pMeta->pUidIdx, &suid, sizeof(tb_uid_t), &pVal, &nVal) == -1) {
    ret = -1;
    goto _err;
  }

  STbDbKey tbDbKey = {0};
  tbDbKey.uid = suid;
  tbDbKey.version = ((SUidIdxVal *)pVal)[0].version;
  tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pVal, &nVal);

  tDecoderInit(&dc, pVal, nVal);
  ret = metaDecodeEntry(&dc, &stbEntry);
  if (ret < 0) {
    goto _err;
  }

  // Get targe schema info
  SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
    terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
    goto _err;
  }
  SSchema *pCol = NULL;
  int32_t  iCol = 0;
  for (;;) {
    pCol = NULL;
    if (iCol >= pTagSchema->nCols) break;
    pCol = &pTagSchema->pSchema[iCol];
    if (strcmp(pCol->name, pAlterTbReq->tagName) == 0) break;
    iCol++;
  }
  if (iCol == 0) {
    // cannot drop 1th tag index
    terrno = -1;
    goto _err;
  }
  if (pCol == NULL) {
    terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
    goto _err;
  }
dengyihao's avatar
dengyihao 已提交
1834

dengyihao's avatar
dengyihao 已提交
1835
  if (IS_IDX_ON(pCol)) {
dengyihao's avatar
dengyihao 已提交
1836 1837 1838 1839
    terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
    goto _err;
  }

dengyihao's avatar
dengyihao 已提交
1840
  SArray *tagIdxList = taosArrayInit(512, sizeof(SMetaPair));
dengyihao's avatar
dengyihao 已提交
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850

  TBC *pTagIdxc = NULL;
  tdbTbcOpen(pMeta->pTagIdx, &pTagIdxc, NULL);
  int rc =
      tdbTbcMoveTo(pTagIdxc, &(STagIdxKey){.suid = suid, .cid = INT32_MIN, .type = pCol->type}, sizeof(STagIdxKey), &c);
  for (;;) {
    void *pKey, *pVal;
    int   nKey, nVal;
    rc = tdbTbcNext(pTagIdxc, &pKey, &nKey, &pVal, &nVal);
    STagIdxKey *pIdxKey = (STagIdxKey *)pKey;
dengyihao's avatar
dengyihao 已提交
1851
    if (pIdxKey->suid != suid || pIdxKey->cid != pCol->colId) {
dengyihao's avatar
dengyihao 已提交
1852 1853 1854 1855
      tdbFree(pKey);
      tdbFree(pVal);
      continue;
    }
dengyihao's avatar
dengyihao 已提交
1856 1857 1858

    SMetaPair pair = {.key = pKey, nKey = nKey};
    taosArrayPush(tagIdxList, &pair);
dengyihao's avatar
dengyihao 已提交
1859 1860
  }
  tdbTbcClose(pTagIdxc);
dengyihao's avatar
dengyihao 已提交
1861

dengyihao's avatar
dengyihao 已提交
1862
  metaWLock(pMeta);
dengyihao's avatar
dengyihao 已提交
1863 1864 1865 1866
  for (int i = 0; i < taosArrayGetSize(tagIdxList); i++) {
    SMetaPair *pair = taosArrayGet(tagIdxList, i);
    tdbTbDelete(pMeta->pTagIdx, pair->key, pair->nkey, pMeta->txn);
  }
dengyihao's avatar
dengyihao 已提交
1867
  metaULock(pMeta);
dengyihao's avatar
dengyihao 已提交
1868 1869 1870

  taosArrayDestroy(tagIdxList);

dengyihao's avatar
dengyihao 已提交
1871
  // set pCol->flags; INDEX_ON
dengyihao's avatar
dengyihao 已提交
1872
  return 0;
dengyihao's avatar
dengyihao 已提交
1873 1874
_err:
  return -1;
dengyihao's avatar
dengyihao 已提交
1875 1876
}

D
dapan1121 已提交
1877
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
H
Hongze Cheng 已提交
1878 1879 1880 1881 1882
  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 已提交
1883
      return metaAlterTableColumn(pMeta, version, pReq, pMetaRsp);
H
Hongze Cheng 已提交
1884 1885 1886 1887
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
      return metaUpdateTableTagVal(pMeta, version, pReq);
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
      return metaUpdateTableOptions(pMeta, version, pReq);
dengyihao's avatar
dengyihao 已提交
1888 1889 1890 1891
    case TSDB_ALTER_TABLE_ADD_TAG_INDEX:
      return metaAddTagIndex(pMeta, version, pReq);
    case TSDB_ALTER_TABLE_DROP_TAG_INDEX:
      return metaDropTagIndex(pMeta, version, pReq);
H
Hongze Cheng 已提交
1892 1893 1894 1895 1896 1897 1898
    default:
      terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
      return -1;
      break;
  }
}

H
Hongze Cheng 已提交
1899
static int metaSaveToTbDb(SMeta *pMeta, const SMetaEntry *pME) {
H
Hongze Cheng 已提交
1900
  STbDbKey tbDbKey;
H
Hongze Cheng 已提交
1901 1902
  void    *pKey = NULL;
  void    *pVal = NULL;
H
Hongze Cheng 已提交
1903 1904
  int      kLen = 0;
  int      vLen = 0;
H
Hongze Cheng 已提交
1905
  SEncoder coder = {0};
H
Hongze Cheng 已提交
1906 1907

  // set key and value
H
Hongze Cheng 已提交
1908
  tbDbKey.version = pME->version;
H
Hongze Cheng 已提交
1909 1910
  tbDbKey.uid = pME->uid;

S
Shengliang Guan 已提交
1911
  metaDebug("vgId:%d, start to save table version:%" PRId64 " uid:%" PRId64, TD_VID(pMeta->pVnode), pME->version,
1912 1913
            pME->uid);

H
Hongze Cheng 已提交
1914 1915
  pKey = &tbDbKey;
  kLen = sizeof(tbDbKey);
H
Hongze Cheng 已提交
1916

wafwerar's avatar
wafwerar 已提交
1917 1918 1919
  int32_t ret = 0;
  tEncodeSize(metaEncodeEntry, pME, vLen, ret);
  if (ret < 0) {
H
Hongze Cheng 已提交
1920 1921 1922 1923 1924 1925 1926 1927 1928
    goto _err;
  }

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

H
Hongze Cheng 已提交
1929
  tEncoderInit(&coder, pVal, vLen);
H
Hongze Cheng 已提交
1930 1931 1932 1933 1934

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

H
Hongze Cheng 已提交
1935
  tEncoderClear(&coder);
H
Hongze Cheng 已提交
1936 1937

  // write to table.db
1938
  if (tdbTbInsert(pMeta->pTbDb, pKey, kLen, pVal, vLen, pMeta->txn) < 0) {
H
Hongze Cheng 已提交
1939 1940 1941 1942 1943 1944 1945
    goto _err;
  }

  taosMemoryFree(pVal);
  return 0;

_err:
S
Shengliang Guan 已提交
1946
  metaError("vgId:%d, failed to save table version:%" PRId64 "uid:%" PRId64 " %s", TD_VID(pMeta->pVnode), pME->version,
1947 1948
            pME->uid, tstrerror(terrno));

H
Hongze Cheng 已提交
1949 1950 1951 1952
  taosMemoryFree(pVal);
  return -1;
}

H
Hongze Cheng 已提交
1953
static int metaUpdateUidIdx(SMeta *pMeta, const SMetaEntry *pME) {
H
Hongze Cheng 已提交
1954 1955 1956 1957 1958 1959 1960
  // upsert cache
  SMetaInfo info;
  metaGetEntryInfo(pME, &info);
  metaCacheUpsert(pMeta, &info);

  SUidIdxVal uidIdxVal = {.suid = info.suid, .version = info.version, .skmVer = info.skmVer};

1961
  return tdbTbUpsert(pMeta->pUidIdx, &pME->uid, sizeof(tb_uid_t), &uidIdxVal, sizeof(uidIdxVal), pMeta->txn);
H
Hongze Cheng 已提交
1962 1963
}

C
Cary Xu 已提交
1964
static int metaUpdateSuidIdx(SMeta *pMeta, const SMetaEntry *pME) {
1965
  return tdbTbUpsert(pMeta->pSuidIdx, &pME->uid, sizeof(tb_uid_t), NULL, 0, pMeta->txn);
C
Cary Xu 已提交
1966 1967
}

H
Hongze Cheng 已提交
1968
static int metaUpdateNameIdx(SMeta *pMeta, const SMetaEntry *pME) {
1969
  return tdbTbUpsert(pMeta->pNameIdx, pME->name, strlen(pME->name) + 1, &pME->uid, sizeof(tb_uid_t), pMeta->txn);
H
Hongze Cheng 已提交
1970 1971
}

1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990
static int metaUpdateTtl(SMeta *pMeta, const SMetaEntry *pME) {
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return 0;

  STtlUpdTtlCtx ctx = {.uid = pME->uid};

  if (pME->type == TSDB_CHILD_TABLE) {
    ctx.ttlDays = pME->ctbEntry.ttlDays;
    ctx.changeTimeMs = pME->ctbEntry.btime;
  } else {
    ctx.ttlDays = pME->ntbEntry.ttlDays;
    ctx.changeTimeMs = pME->ntbEntry.btime;
  }

  return ttlMgrInsertTtl(pMeta->pTtlMgr, &ctx);
}

int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
  if (!tsTtlChangeOnWrite) return 0;

S
Shungang Li 已提交
1991 1992 1993 1994 1995
  if (changeTimeMs <= 0) {
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
  }

1996 1997 1998
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs};

  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
H
Hongze Cheng 已提交
1999 2000
}

H
Hongze Cheng 已提交
2001 2002
static int metaUpdateCtbIdx(SMeta *pMeta, const SMetaEntry *pME) {
  SCtbIdxKey ctbIdxKey = {.suid = pME->ctbEntry.suid, .uid = pME->uid};
wmmhello's avatar
wmmhello 已提交
2003

2004
  return tdbTbUpsert(pMeta->pCtbIdx, &ctbIdxKey, sizeof(ctbIdxKey), pME->ctbEntry.pTags,
2005
                     ((STag *)(pME->ctbEntry.pTags))->len, pMeta->txn);
H
Hongze Cheng 已提交
2006 2007
}

wmmhello's avatar
wmmhello 已提交
2008
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
2009
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
dengyihao's avatar
dengyihao 已提交
2010 2011 2012 2013 2014
  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 已提交
2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025

  *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 已提交
2026 2027 2028 2029

  // refactor
  if (IS_VAR_DATA_TYPE(type)) {
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
dengyihao's avatar
dengyihao 已提交
2030
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
dengyihao's avatar
dengyihao 已提交
2031 2032
    *(tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData) = uid;
  } else {
dengyihao's avatar
dengyihao 已提交
2033
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
dengyihao's avatar
dengyihao 已提交
2034 2035
    *(tb_uid_t *)((*ppTagIdxKey)->data + nTagData) = uid;
  }
H
Hongze Cheng 已提交
2036 2037 2038 2039 2040 2041 2042 2043 2044

  return 0;
}

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

static int metaUpdateTagIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry) {
H
Hongze Cheng 已提交
2045
  void          *pData = NULL;
H
Hongze Cheng 已提交
2046 2047 2048
  int            nData = 0;
  STbDbKey       tbDbKey = {0};
  SMetaEntry     stbEntry = {0};
H
Hongze Cheng 已提交
2049
  STagIdxKey    *pTagIdxKey = NULL;
H
Hongze Cheng 已提交
2050
  int32_t        nTagIdxKey;
M
Minglei Jin 已提交
2051 2052
  const SSchema *pTagColumn;
  const void    *pTagData = NULL;
C
Cary Xu 已提交
2053
  int32_t        nTagData = 0;
H
Hongze Cheng 已提交
2054
  SDecoder       dc = {0};
2055
  int32_t        ret = 0;
H
Hongze Cheng 已提交
2056
  // get super table
H
Hongze Cheng 已提交
2057
  if (tdbTbGet(pMeta->pUidIdx, &pCtbEntry->ctbEntry.suid, sizeof(tb_uid_t), &pData, &nData) != 0) {
2058 2059 2060
    metaError("vgId:%d, failed to get stable suid for update. version:%" PRId64, TD_VID(pMeta->pVnode),
              pCtbEntry->version);
    terrno = TSDB_CODE_TDB_INVALID_TABLE_ID;
2061 2062
    ret = -1;
    goto end;
wmmhello's avatar
wmmhello 已提交
2063
  }
H
Hongze Cheng 已提交
2064
  tbDbKey.uid = pCtbEntry->ctbEntry.suid;
H
Hongze Cheng 已提交
2065
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
H
Hongze Cheng 已提交
2066
  tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData);
H
Hongze Cheng 已提交
2067 2068

  tDecoderInit(&dc, pData, nData);
M
Minglei Jin 已提交
2069 2070 2071 2072
  ret = metaDecodeEntry(&dc, &stbEntry);
  if (ret < 0) {
    goto end;
  }
H
Hongze Cheng 已提交
2073

M
Minglei Jin 已提交
2074 2075 2076 2077
  if (stbEntry.stbEntry.schemaTag.pSchema == NULL) {
    goto end;
  }

dengyihao's avatar
dengyihao 已提交
2078 2079 2080
  SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
    pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
dengyihao's avatar
dengyihao 已提交
2081
    STagVal tagVal = {.cid = pTagColumn->colId};
C
Cary Xu 已提交
2082

dengyihao's avatar
dengyihao 已提交
2083 2084
    pTagData = pCtbEntry->ctbEntry.pTags;
    nTagData = ((const STag *)pCtbEntry->ctbEntry.pTags)->len;
2085 2086
    ret = metaSaveJsonVarToIdx(pMeta, pCtbEntry, pTagColumn);
    goto end;
dengyihao's avatar
dengyihao 已提交
2087 2088 2089
  } else {
    for (int i = 0; i < pTagSchema->nCols; i++) {
      pTagColumn = &pTagSchema->pSchema[i];
dengyihao's avatar
dengyihao 已提交
2090
      if (i != 0 && !IS_IDX_ON(pTagColumn)) continue;
dengyihao's avatar
dengyihao 已提交
2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110

      STagVal tagVal = {.cid = pTagColumn->colId};
      tTagGet((const STag *)pCtbEntry->ctbEntry.pTags, &tagVal);
      if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
        pTagData = tagVal.pData;
        nTagData = (int32_t)tagVal.nData;
      } else {
        pTagData = &(tagVal.i64);
        nTagData = tDataTypes[pTagColumn->type].bytes;
      }

      if (pTagData != NULL) {
        if (metaCreateTagIdxKey(pCtbEntry->ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type,
                                pCtbEntry->uid, &pTagIdxKey, &nTagIdxKey) < 0) {
          ret = -1;
          goto end;
        }
        tdbTbUpsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn);
      }
      metaDestroyTagIdxKey(pTagIdxKey);
dengyihao's avatar
dengyihao 已提交
2111
    }
H
Hongze Cheng 已提交
2112
  }
2113
end:
dengyihao's avatar
dengyihao 已提交
2114
  // metaDestroyTagIdxKey(pTagIdxKey);
H
Hongze Cheng 已提交
2115 2116
  tDecoderClear(&dc);
  tdbFree(pData);
2117
  return ret;
H
Hongze Cheng 已提交
2118 2119
}

H
Hongze Cheng 已提交
2120
static int metaSaveToSkmDb(SMeta *pMeta, const SMetaEntry *pME) {
H
Hongze Cheng 已提交
2121
  SEncoder              coder = {0};
H
Hongze Cheng 已提交
2122
  void                 *pVal = NULL;
H
Hongze Cheng 已提交
2123 2124 2125 2126 2127 2128
  int                   vLen = 0;
  int                   rcode = 0;
  SSkmDbKey             skmDbKey = {0};
  const SSchemaWrapper *pSW;

  if (pME->type == TSDB_SUPER_TABLE) {
2129
    pSW = &pME->stbEntry.schemaRow;
H
Hongze Cheng 已提交
2130
  } else if (pME->type == TSDB_NORMAL_TABLE) {
2131
    pSW = &pME->ntbEntry.schemaRow;
H
Hongze Cheng 已提交
2132
  } else {
2133 2134
    metaError("meta/table: invalide table type: %" PRId8 " save skm db failed.", pME->type);
    return TSDB_CODE_FAILED;
H
Hongze Cheng 已提交
2135 2136
  }

H
Hongze Cheng 已提交
2137
  skmDbKey.uid = pME->uid;
2138
  skmDbKey.sver = pSW->version;
H
Hongze Cheng 已提交
2139

2140 2141 2142 2143 2144
  // 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 已提交
2145
  // encode schema
wafwerar's avatar
wafwerar 已提交
2146 2147 2148
  int32_t ret = 0;
  tEncodeSize(tEncodeSSchemaWrapper, pSW, vLen, ret);
  if (ret < 0) return -1;
H
Hongze Cheng 已提交
2149
  pVal = taosMemoryMalloc(vLen);
H
Hongze Cheng 已提交
2150 2151 2152 2153 2154 2155
  if (pVal == NULL) {
    rcode = -1;
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

H
Hongze Cheng 已提交
2156
  tEncoderInit(&coder, pVal, vLen);
H
Hongze Cheng 已提交
2157 2158
  tEncodeSSchemaWrapper(&coder, pSW);

2159
  if (tdbTbInsert(pMeta->pSkmDb, &skmDbKey, sizeof(skmDbKey), pVal, vLen, pMeta->txn) < 0) {
H
Hongze Cheng 已提交
2160 2161 2162 2163
    rcode = -1;
    goto _exit;
  }

M
Minglei Jin 已提交
2164 2165 2166
  metaDebug("vgId:%d, set schema:(%" PRId64 ") sver:%d since %s", TD_VID(pMeta->pVnode), pME->uid, pSW->version,
            tstrerror(terrno));

H
Hongze Cheng 已提交
2167
_exit:
H
Hongze Cheng 已提交
2168
  taosMemoryFree(pVal);
H
Hongze Cheng 已提交
2169
  tEncoderClear(&coder);
H
Hongze Cheng 已提交
2170 2171 2172
  return rcode;
}

H
Hongze Cheng 已提交
2173
int metaHandleEntry(SMeta *pMeta, const SMetaEntry *pME) {
2174 2175
  int32_t code = 0;
  int32_t line = 0;
H
Hongze Cheng 已提交
2176 2177
  metaWLock(pMeta);

H
Hongze Cheng 已提交
2178
  // save to table.db
2179 2180
  code = metaSaveToTbDb(pMeta, pME);
  VND_CHECK_CODE(code, line, _err);
H
Hongze Cheng 已提交
2181 2182

  // update uid.idx
2183 2184
  code = metaUpdateUidIdx(pMeta, pME);
  VND_CHECK_CODE(code, line, _err);
H
Hongze Cheng 已提交
2185 2186

  // update name.idx
2187 2188
  code = metaUpdateNameIdx(pMeta, pME);
  VND_CHECK_CODE(code, line, _err);
H
Hongze Cheng 已提交
2189 2190 2191

  if (pME->type == TSDB_CHILD_TABLE) {
    // update ctb.idx
2192 2193
    code = metaUpdateCtbIdx(pMeta, pME);
    VND_CHECK_CODE(code, line, _err);
H
Hongze Cheng 已提交
2194 2195

    // update tag.idx
2196 2197
    code = metaUpdateTagIdx(pMeta, pME);
    VND_CHECK_CODE(code, line, _err);
H
Hongze Cheng 已提交
2198 2199
  } else {
    // update schema.db
2200 2201
    code = metaSaveToSkmDb(pMeta, pME);
    VND_CHECK_CODE(code, line, _err);
C
Cary Xu 已提交
2202 2203

    if (pME->type == TSDB_SUPER_TABLE) {
2204 2205
      code = metaUpdateSuidIdx(pMeta, pME);
      VND_CHECK_CODE(code, line, _err);
H
Hongze Cheng 已提交
2206
    }
H
Hongze Cheng 已提交
2207 2208
  }

2209
  code = metaUpdateBtimeIdx(pMeta, pME);
2210
  VND_CHECK_CODE(code, line, _err);
dengyihao's avatar
dengyihao 已提交
2211 2212

  if (pME->type == TSDB_NORMAL_TABLE) {
2213 2214
    code = metaUpdateNcolIdx(pMeta, pME);
    VND_CHECK_CODE(code, line, _err);
dengyihao's avatar
dengyihao 已提交
2215 2216
  }

H
Hongze Cheng 已提交
2217
  if (pME->type != TSDB_SUPER_TABLE) {
2218
    code = metaUpdateTtl(pMeta, pME);
2219
    VND_CHECK_CODE(code, line, _err);
H
Hongze Cheng 已提交
2220 2221
  }

H
Hongze Cheng 已提交
2222
  metaULock(pMeta);
2223 2224
  metaDebug("vgId:%d, handle meta entry, ver:%" PRId64 ", uid:%" PRId64 ", name:%s", TD_VID(pMeta->pVnode),
            pME->version, pME->uid, pME->name);
H
Hongze Cheng 已提交
2225
  return 0;
H
Hongze Cheng 已提交
2226 2227 2228

_err:
  metaULock(pMeta);
2229 2230
  metaError("vgId:%d, failed to handle meta entry since %s at line:%d, ver:%" PRId64 ", uid:%" PRId64 ", name:%s",
            TD_VID(pMeta->pVnode), terrstr(), line, pME->version, pME->uid, pME->name);
H
Hongze Cheng 已提交
2231
  return -1;
2232
}
2233

dengyihao's avatar
dengyihao 已提交
2234
// refactor later
dengyihao's avatar
dengyihao 已提交
2235 2236
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }