metaTable.c 62.2 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 23 24 25 26 27 28 29
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);
static int  metaUpdateTtlIdx(SMeta *pMeta, const SMetaEntry *pME);
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 31 32 33 34
// opt ins_tables query
static int metaUpdateCtimeIdx(SMeta *pMeta, const SMetaEntry *pME);
static int metaDeleteCtimeIdx(SMeta *pMeta, const SMetaEntry *pME);
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
    metaGetInfo(pMeta, uid, &info, NULL);
211
    if (info.uid == info.suid) {
212 213
      return 0;
    } else {
214
      terrno = TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
215 216
      return -1;
    }
H
Hongze Cheng 已提交
217
  }
H
Hongze Cheng 已提交
218 219

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

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

233 234
  ++pMeta->pVnode->config.vndStats.numOfSTables;

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

  return 0;

_err:
M
Minglei Jin 已提交
240 241
  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 已提交
242 243 244
  return -1;
}

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

H
Hongze Cheng 已提交
261
  // drop all child tables
262
  TBC *pCtbIdxc = NULL;
H
Hongze Cheng 已提交
263

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

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

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

282
    taosArrayPush(tbUidList, &(((SCtbIdxKey *)pKey)->uid));
H
Hongze Cheng 已提交
283 284 285 286 287
  }

  tdbTbcClose(pCtbIdxc);

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

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

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

  metaULock(pMeta);

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

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

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

    terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
H
Hongze Cheng 已提交
330 331 332
    return -1;
  }

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

M
Minglei Jin 已提交
337
    terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
H
Hongze Cheng 已提交
338 339 340
    return -1;
  }

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

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

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

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

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

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

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

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

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

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

387
  // metaStatsCacheDrop(pMeta, nStbEntry.uid);
388

H
Hongze Cheng 已提交
389
  metaULock(pMeta);
390 391

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

dengyihao's avatar
dengyihao 已提交
401 402
  STbDbKey tbDbKey = {0};

dengyihao's avatar
dengyihao 已提交
403 404
  TBC     *pUidIdxc = NULL;
  TBC     *pTbDbc = NULL;
dengyihao's avatar
dengyihao 已提交
405 406
  void    *pData = NULL;
  int      nData = 0;
dengyihao's avatar
dengyihao 已提交
407 408 409 410 411 412 413 414 415 416 417 418
  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 已提交
419 420 421
  tbDbKey.uid = suid;
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
  tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData);
dengyihao's avatar
dengyihao 已提交
422

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

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

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

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

dengyihao's avatar
dengyihao 已提交
441 442 443 444 445 446 447 448 449 450 451 452
  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 已提交
453 454
  }

dengyihao's avatar
dengyihao 已提交
455
  if (diffIdx == -1 && diffIdx == 0) {
dengyihao's avatar
dengyihao 已提交
456 457
    goto _err;
  }
dengyihao's avatar
dengyihao 已提交
458 459

  // Get target schema info
dengyihao's avatar
dengyihao 已提交
460
  SSchemaWrapper *pTagSchema = &pReq->schemaTag;
dengyihao's avatar
dengyihao 已提交
461 462 463 464
  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 已提交
465
  SSchema *pCol = pTagSchema->pSchema + diffIdx;
dengyihao's avatar
dengyihao 已提交
466 467 468 469 470 471 472 473 474 475 476 477

  /*
   * 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 已提交
478 479
    void *pKey = NULL, *pVal = NULL;
    int   nKey = 0, nVal = 0;
dengyihao's avatar
dengyihao 已提交
480
    rc = tdbTbcNext(pCtbIdxc, &pKey, &nKey, &pVal, &nVal);
dengyihao's avatar
dengyihao 已提交
481 482 483 484 485 486 487
    if (rc < 0) {
      tdbFree(pKey);
      tdbFree(pVal);
      tdbTbcClose(pCtbIdxc);
      pCtbIdxc = NULL;
      break;
    }
dengyihao's avatar
dengyihao 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
    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 已提交
509 510 511 512
    rc = metaCreateTagIdxKey(suid, pCol->colId, pTagData, nTagData, pCol->type, table->uid, &pTagIdxKey, &nTagIdxKey);
    tdbFree(pKey);
    tdbFree(pVal);
    if (rc < 0) {
dengyihao's avatar
dengyihao 已提交
513
      metaDestroyTagIdxKey(pTagIdxKey);
dengyihao's avatar
dengyihao 已提交
514
      tdbTbcClose(pCtbIdxc);
dengyihao's avatar
dengyihao 已提交
515 516
      goto _err;
    }
dengyihao's avatar
dengyihao 已提交
517 518

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

  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 已提交
531
  metaWLock(pMeta);
dengyihao's avatar
dengyihao 已提交
532 533 534 535 536
  // update table.db
  metaSaveToTbDb(pMeta, &nStbEntry);
  // update uid index
  metaUpdateUidIdx(pMeta, &nStbEntry);
  metaULock(pMeta);
dengyihao's avatar
dengyihao 已提交
537 538 539

  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
  tDecoderClear(&dc);
dengyihao's avatar
dengyihao 已提交
540 541 542
  tdbFree(pData);

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

dengyihao's avatar
dengyihao 已提交
549
  return TSDB_CODE_VND_COL_ALREADY_EXISTS;
dengyihao's avatar
dengyihao 已提交
550
}
dengyihao's avatar
dengyihao 已提交
551 552 553 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
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 已提交
585
    if (0 == strncmp(schema->name, pReq->colName, sizeof(pReq->colName))) {
dengyihao's avatar
dengyihao 已提交
586
      if (i != 0 || IS_IDX_ON(schema)) {
dengyihao's avatar
dengyihao 已提交
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
        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 已提交
611 612 613 614 615 616 617
    if (rc < 0) {
      tdbFree(pKey);
      tdbFree(pVal);
      tdbTbcClose(pCtbIdxc);
      pCtbIdxc = NULL;
      break;
    }
dengyihao's avatar
dengyihao 已提交
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
    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 已提交
639 640 641 642
    rc = metaCreateTagIdxKey(suid, pCol->colId, pTagData, nTagData, pCol->type, table->uid, &pTagIdxKey, &nTagIdxKey);
    tdbFree(pKey);
    tdbFree(pVal);
    if (rc < 0) {
dengyihao's avatar
dengyihao 已提交
643
      metaDestroyTagIdxKey(pTagIdxKey);
dengyihao's avatar
dengyihao 已提交
644
      tdbTbcClose(pCtbIdxc);
dengyihao's avatar
dengyihao 已提交
645 646
      goto _err;
    }
dengyihao's avatar
dengyihao 已提交
647 648

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

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

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

676 677
  tDeleteSchemaWrapper(tag);
  tDeleteSchemaWrapper(row);
dengyihao's avatar
dengyihao 已提交
678

dengyihao's avatar
dengyihao 已提交
679 680
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
  tDecoderClear(&dc);
dengyihao's avatar
dengyihao 已提交
681 682 683
  tdbFree(pData);

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

dengyihao's avatar
dengyihao 已提交
690
  return -1;
dengyihao's avatar
dengyihao 已提交
691
}
H
Hongze Cheng 已提交
692

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

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

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

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

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

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

      if (IS_VAR_DATA_TYPE(pTagVal->type)) {
dengyihao's avatar
dengyihao 已提交
751
        char *buf = taosMemoryCalloc(pTagVal->nData + 1, 1);
wmmhello's avatar
wmmhello 已提交
752
        memcpy(buf, pTagVal->pData, pTagVal->nData);
dengyihao's avatar
dengyihao 已提交
753 754
        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 已提交
755 756 757 758
        taosMemoryFree(buf);
      } else {
        double val = 0;
        GET_TYPED_DATA(val, double, pTagVal->type, &pTagVal->i64);
dengyihao's avatar
dengyihao 已提交
759 760
        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 已提交
761 762
      }
    }
wmmhello's avatar
wmmhello 已提交
763
#endif
wmmhello's avatar
wmmhello 已提交
764

765
    ++pMeta->pVnode->config.vndStats.numOfCTables;
766 767 768

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

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

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

786 787 788 789 790 791 792 793 794 795
  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 {
796
        metaUpdateMetaRsp(pReq->uid, pReq->name, &pReq->ntb.schemaRow, *pMetaRsp);
797 798 799 800
      }
    }
  }

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

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

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

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

H
Hongze Cheng 已提交
825 826 827
  metaWLock(pMeta);
  metaDropTableByUid(pMeta, uid, &type);
  metaULock(pMeta);
H
Hongze Cheng 已提交
828

829
  if ((type == TSDB_CHILD_TABLE || type == TSDB_NORMAL_TABLE) && tbUids) {
H
Hongze Cheng 已提交
830
    taosArrayPush(tbUids, &uid);
H
Hongze Cheng 已提交
831
  }
H
Hongze Cheng 已提交
832

833 834 835 836
  if ((type == TSDB_CHILD_TABLE) && tbUid) {
    *tbUid = uid;
  }

H
Hongze Cheng 已提交
837 838 839
  tdbFree(pData);
  return 0;
}
H
Hongze Cheng 已提交
840

841 842
int metaTtlDropTable(SMeta *pMeta, int64_t ttl, SArray *tbUids) {
  int ret = metaTtlSmaller(pMeta, ttl, tbUids);
H
Hongze Cheng 已提交
843
  if (ret != 0) {
844 845
    return ret;
  }
846
  if (taosArrayGetSize(tbUids) == 0) {
847 848 849 850
    return 0;
  }

  metaWLock(pMeta);
851 852 853
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
    tb_uid_t *uid = (tb_uid_t *)taosArrayGet(tbUids, i);
    metaDropTableByUid(pMeta, *uid, NULL);
H
Hongze Cheng 已提交
854
    metaDebug("ttl drop table:%" PRId64, *uid);
855 856 857 858 859
  }
  metaULock(pMeta);
  return 0;
}

H
Hongze Cheng 已提交
860
static void metaBuildTtlIdxKey(STtlIdxKey *ttlKey, const SMetaEntry *pME) {
M
Minglei Jin 已提交
861 862
  int64_t ttlDays = 0;
  int64_t ctime = 0;
863 864 865 866 867 868 869
  if (pME->type == TSDB_CHILD_TABLE) {
    ctime = pME->ctbEntry.ctime;
    ttlDays = pME->ctbEntry.ttlDays;
  } else if (pME->type == TSDB_NORMAL_TABLE) {
    ctime = pME->ntbEntry.ctime;
    ttlDays = pME->ntbEntry.ttlDays;
  } else {
870 871
    metaError("meta/table: invalide table type: %" PRId8 " build ttl idx key failed.", pME->type);
    return;
872 873 874 875
  }

  if (ttlDays <= 0) return;

wmmhello's avatar
wmmhello 已提交
876
  ttlKey->dtime = ctime / 1000 + ttlDays * tsTtlUnit;
877 878
  ttlKey->uid = pME->uid;
}
dengyihao's avatar
dengyihao 已提交
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
static int metaBuildCtimeIdxKey(SCtimeIdxKey *ctimeKey, const SMetaEntry *pME) {
  int64_t ctime;
  if (pME->type == TSDB_CHILD_TABLE) {
    ctime = pME->ctbEntry.ctime;
  } else if (pME->type == TSDB_NORMAL_TABLE) {
    ctime = pME->ntbEntry.ctime;
  } else {
    return -1;
  }

  ctimeKey->ctime = ctime;
  ctimeKey->uid = pME->uid;
  return 0;
}

static int metaBuildNColIdxKey(SNcolIdxKey *ncolKey, const SMetaEntry *pME) {
dengyihao's avatar
dengyihao 已提交
895 896 897 898 899 900
  if (pME->type == TSDB_NORMAL_TABLE) {
    ncolKey->ncol = pME->ntbEntry.schemaRow.nCols;
    ncolKey->uid = pME->uid;
  } else {
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
901 902
  return 0;
}
903 904 905 906

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

H
Hongze Cheng 已提交
911
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type) {
H
Hongze Cheng 已提交
912
  void      *pData = NULL;
H
Hongze Cheng 已提交
913 914 915 916 917 918
  int        nData = 0;
  int        rc = 0;
  SMetaEntry e = {0};
  SDecoder   dc = {0};

  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
919 920 921
  if (rc < 0) {
    return -1;
  }
H
Hongze Cheng 已提交
922
  int64_t version = ((SUidIdxVal *)pData)[0].version;
H
Hongze Cheng 已提交
923 924 925 926

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

  tDecoderInit(&dc, pData, nData);
M
Minglei Jin 已提交
927 928 929 930 931
  rc = metaDecodeEntry(&dc, &e);
  if (rc < 0) {
    tDecoderClear(&dc);
    return -1;
  }
H
Hongze Cheng 已提交
932 933 934

  if (type) *type = e.type;

935 936 937 938 939
  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 已提交
940
      STbDbKey tbDbKey = {.uid = e.ctbEntry.suid, .version = ((SUidIdxVal *)tData)[0].version};
941 942 943 944 945 946
      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 已提交
947 948 949 950 951

        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];
952
          metaDelJsonVarFromIdx(pMeta, &e, pTagColumn);
dengyihao's avatar
dengyihao 已提交
953
        } else {
dengyihao's avatar
dengyihao 已提交
954 955
          for (int i = 0; i < pTagSchema->nCols; i++) {
            pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[i];
dengyihao's avatar
dengyihao 已提交
956
            if (!IS_IDX_ON(pTagColumn)) continue;
dengyihao's avatar
dengyihao 已提交
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
            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 已提交
978
          }
979 980 981 982 983 984 985
        }
        tDecoderClear(&tdc);
      }
      tdbFree(tData);
    }
  }

986 987 988
  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);
989

dengyihao's avatar
dengyihao 已提交
990 991 992
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteCtimeIdx(pMeta, &e);
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);

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

H
Hongze Cheng 已提交
995
  if (e.type == TSDB_CHILD_TABLE) {
996
    tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
997 998

    --pMeta->pVnode->config.vndStats.numOfCTables;
999 1000

    metaUpdateStbStats(pMeta, e.ctbEntry.suid, -1);
1001
    metaUidCacheClear(pMeta, e.ctbEntry.suid);
D
dapan1121 已提交
1002
    metaTbGroupCacheClear(pMeta, e.ctbEntry.suid);    
H
Hongze Cheng 已提交
1003 1004
  } else if (e.type == TSDB_NORMAL_TABLE) {
    // drop schema.db (todo)
1005 1006

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

1012
    metaStatsCacheDrop(pMeta, uid);
1013
    metaUidCacheClear(pMeta, uid);
D
dapan1121 已提交
1014
    metaTbGroupCacheClear(pMeta, uid);        
1015
    --pMeta->pVnode->config.vndStats.numOfSTables;
H
Hongze Cheng 已提交
1016 1017
  }

H
Hongze Cheng 已提交
1018 1019
  metaCacheDrop(pMeta, uid);

H
Hongze Cheng 已提交
1020 1021
  tDecoderClear(&dc);
  tdbFree(pData);
H
Hongze Cheng 已提交
1022

H
refact  
Hongze Cheng 已提交
1023 1024
  return 0;
}
dengyihao's avatar
dengyihao 已提交
1025 1026 1027 1028 1029 1030
// opt ins_tables
int metaUpdateCtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
  SCtimeIdxKey ctimeKey = {0};
  if (metaBuildCtimeIdxKey(&ctimeKey, pME) < 0) {
    return 0;
  }
1031 1032
  metaTrace("vgId:%d, start to save version:%" PRId64 " uid:%" PRId64 " ctime:%" PRId64, TD_VID(pMeta->pVnode),
            pME->version, pME->uid, ctimeKey.ctime);
1033

1034
  return tdbTbUpsert(pMeta->pCtimeIdx, &ctimeKey, sizeof(ctimeKey), NULL, 0, pMeta->txn);
dengyihao's avatar
dengyihao 已提交
1035 1036 1037 1038 1039 1040 1041
}

int metaDeleteCtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
  SCtimeIdxKey ctimeKey = {0};
  if (metaBuildCtimeIdxKey(&ctimeKey, pME) < 0) {
    return 0;
  }
1042
  return tdbTbDelete(pMeta->pCtimeIdx, &ctimeKey, sizeof(ctimeKey), pMeta->txn);
dengyihao's avatar
dengyihao 已提交
1043 1044 1045 1046 1047 1048
}
int metaUpdateNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
  SNcolIdxKey ncolKey = {0};
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
    return 0;
  }
1049
  return tdbTbUpsert(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), NULL, 0, pMeta->txn);
dengyihao's avatar
dengyihao 已提交
1050 1051 1052 1053 1054 1055 1056
}

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

D
dapan1121 已提交
1060
static int metaAlterTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq, STableMetaRsp *pMetaRsp) {
H
Hongze Cheng 已提交
1061
  void           *pVal = NULL;
H
Hongze Cheng 已提交
1062
  int             nVal = 0;
H
Hongze Cheng 已提交
1063
  const void     *pData = NULL;
H
Hongze Cheng 已提交
1064 1065 1066 1067
  int             nData = 0;
  int             ret = 0;
  tb_uid_t        uid;
  int64_t         oversion;
H
Hongze Cheng 已提交
1068
  SSchema        *pColumn = NULL;
H
Hongze Cheng 已提交
1069 1070 1071 1072
  SMetaEntry      entry = {0};
  SSchemaWrapper *pSchema;
  int             c;

H
Hongze Cheng 已提交
1073
  if (pAlterTbReq->colName == NULL) {
H
Hongze Cheng 已提交
1074 1075 1076 1077
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }

H
Hongze Cheng 已提交
1078
  // search name index
H
Hongze Cheng 已提交
1079
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
H
Hongze Cheng 已提交
1080
  if (ret < 0) {
1081
    terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
H
Hongze Cheng 已提交
1082 1083 1084 1085 1086 1087 1088 1089
    return -1;
  }

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

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

1092
  tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, NULL);
H
Hongze Cheng 已提交
1093
  tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c);
1094
  if (c != 0) {
1095
    tdbTbcClose(pUidIdxc);
1096 1097 1098
    metaError("meta/table: invalide c: %" PRId32 " alt tb column failed.", c);
    return -1;
  }
H
Hongze Cheng 已提交
1099

H
Hongze Cheng 已提交
1100
  tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
1101
  oversion = ((SUidIdxVal *)pData)[0].version;
H
Hongze Cheng 已提交
1102 1103

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

1106
  tdbTbcOpen(pMeta->pTbDb, &pTbDbc, NULL);
H
Hongze Cheng 已提交
1107
  tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c);
1108
  if (c != 0) {
1109 1110
    tdbTbcClose(pUidIdxc);
    tdbTbcClose(pTbDbc);
1111 1112 1113 1114
    metaError("meta/table: invalide c: %" PRId32 " alt tb column failed.", c);
    return -1;
  }

H
Hongze Cheng 已提交
1115
  tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
1116 1117 1118

  // get table entry
  SDecoder dc = {0};
H
Hongze Cheng 已提交
1119 1120 1121
  entry.pBuf = taosMemoryMalloc(nData);
  memcpy(entry.pBuf, pData, nData);
  tDecoderInit(&dc, entry.pBuf, nData);
H
Hongze Cheng 已提交
1122
  ret = metaDecodeEntry(&dc, &entry);
1123
  if (ret != 0) {
1124 1125
    tdbTbcClose(pUidIdxc);
    tdbTbcClose(pTbDbc);
1126 1127 1128 1129
    tDecoderClear(&dc);
    metaError("meta/table: invalide ret: %" PRId32 " alt tb column failed.", ret);
    return -1;
  }
H
Hongze Cheng 已提交
1130 1131 1132 1133 1134 1135

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

dengyihao's avatar
dengyihao 已提交
1138 1139 1140 1141
  // save old entry
  SMetaEntry oldEntry = {.type = TSDB_NORMAL_TABLE, .uid = entry.uid};
  oldEntry.ntbEntry.schemaRow.nCols = pSchema->nCols;

H
Hongze Cheng 已提交
1142 1143 1144 1145 1146 1147 1148
  int32_t iCol = 0;
  for (;;) {
    pColumn = NULL;

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

1149 1150 1151 1152 1153
    if (NULL == pAlterTbReq->colName) {
      metaError("meta/table: null pAlterTbReq->colName");
      return -1;
    }

H
Hongze Cheng 已提交
1154 1155 1156 1157 1158
    if (strcmp(pColumn->name, pAlterTbReq->colName) == 0) break;
    iCol++;
  }

  entry.version = version;
H
Hongze Cheng 已提交
1159 1160
  int      tlen;
  SSchema *pNewSchema = NULL;
H
Hongze Cheng 已提交
1161 1162 1163 1164 1165 1166
  switch (pAlterTbReq->action) {
    case TSDB_ALTER_TABLE_ADD_COLUMN:
      if (pColumn) {
        terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
        goto _err;
      }
1167
      pSchema->version++;
H
Hongze Cheng 已提交
1168
      pSchema->nCols++;
H
Hongze Cheng 已提交
1169 1170 1171
      pNewSchema = taosMemoryMalloc(sizeof(SSchema) * pSchema->nCols);
      memcpy(pNewSchema, pSchema->pSchema, sizeof(SSchema) * (pSchema->nCols - 1));
      pSchema->pSchema = pNewSchema;
1172 1173 1174 1175 1176
      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);
1177 1178

      ++pMeta->pVnode->config.vndStats.numOfNTimeSeries;
H
Hongze Cheng 已提交
1179 1180 1181
      break;
    case TSDB_ALTER_TABLE_DROP_COLUMN:
      if (pColumn == NULL) {
1182
        terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
H
Hongze Cheng 已提交
1183 1184 1185 1186 1187 1188
        goto _err;
      }
      if (pColumn->colId == 0) {
        terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
        goto _err;
      }
L
Liu Jicong 已提交
1189 1190 1191 1192
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
        goto _err;
      }
1193
      pSchema->version++;
H
Hongze Cheng 已提交
1194 1195 1196 1197
      tlen = (pSchema->nCols - iCol - 1) * sizeof(SSchema);
      if (tlen) {
        memmove(pColumn, pColumn + 1, tlen);
      }
H
Hongze Cheng 已提交
1198
      pSchema->nCols--;
1199 1200

      --pMeta->pVnode->config.vndStats.numOfNTimeSeries;
H
Hongze Cheng 已提交
1201 1202 1203
      break;
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
      if (pColumn == NULL) {
1204
        terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
H
Hongze Cheng 已提交
1205 1206
        goto _err;
      }
H
Hongze Cheng 已提交
1207
      if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes > pAlterTbReq->colModBytes) {
H
Hongze Cheng 已提交
1208 1209 1210
        terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
        goto _err;
      }
L
Liu Jicong 已提交
1211 1212 1213 1214
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
        goto _err;
      }
1215
      pSchema->version++;
H
Hongze Cheng 已提交
1216
      pColumn->bytes = pAlterTbReq->colModBytes;
H
Hongze Cheng 已提交
1217 1218
      break;
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
H
Hongze Cheng 已提交
1219 1220 1221 1222
      if (pAlterTbReq->colNewName == NULL) {
        terrno = TSDB_CODE_INVALID_MSG;
        goto _err;
      }
H
Hongze Cheng 已提交
1223
      if (pColumn == NULL) {
1224
        terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
H
Hongze Cheng 已提交
1225 1226
        goto _err;
      }
L
Liu Jicong 已提交
1227 1228 1229 1230
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
        goto _err;
      }
1231
      pSchema->version++;
H
Hongze Cheng 已提交
1232 1233 1234 1235 1236 1237
      strcpy(pColumn->name, pAlterTbReq->colNewName);
      break;
  }

  entry.version = version;

H
Hongze Cheng 已提交
1238 1239 1240
  // do actual write
  metaWLock(pMeta);

dengyihao's avatar
fix RC  
dengyihao 已提交
1241 1242
  metaDeleteNcolIdx(pMeta, &oldEntry);
  metaUpdateNcolIdx(pMeta, &entry);
H
Hongze Cheng 已提交
1243 1244 1245
  // save to table db
  metaSaveToTbDb(pMeta, &entry);

H
Hongze Cheng 已提交
1246
  metaUpdateUidIdx(pMeta, &entry);
H
Hongze Cheng 已提交
1247 1248 1249 1250 1251

  metaSaveToSkmDb(pMeta, &entry);

  metaULock(pMeta);

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

wmmhello's avatar
wmmhello 已提交
1254
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
H
Hongze Cheng 已提交
1255
  if (pNewSchema) taosMemoryFree(pNewSchema);
H
Hongze Cheng 已提交
1256 1257
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
1258 1259
  tDecoderClear(&dc);

H
Hongze Cheng 已提交
1260
  return 0;
H
Hongze Cheng 已提交
1261 1262

_err:
wmmhello's avatar
wmmhello 已提交
1263
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
H
Hongze Cheng 已提交
1264 1265
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
1266 1267
  tDecoderClear(&dc);

H
Hongze Cheng 已提交
1268
  return -1;
H
Hongze Cheng 已提交
1269 1270 1271
}

static int metaUpdateTableTagVal(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
H
Hongze Cheng 已提交
1272 1273
  SMetaEntry  ctbEntry = {0};
  SMetaEntry  stbEntry = {0};
H
Hongze Cheng 已提交
1274
  void       *pVal = NULL;
H
Hongze Cheng 已提交
1275 1276 1277 1278 1279 1280 1281 1282
  int         nVal = 0;
  int         ret;
  int         c;
  tb_uid_t    uid;
  int64_t     oversion;
  const void *pData = NULL;
  int         nData = 0;

H
Hongze Cheng 已提交
1283 1284 1285 1286 1287
  if (pAlterTbReq->tagName == NULL) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }

H
Hongze Cheng 已提交
1288
  // search name index
H
Hongze Cheng 已提交
1289
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
H
Hongze Cheng 已提交
1290
  if (ret < 0) {
1291
    terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
H
Hongze Cheng 已提交
1292 1293 1294 1295 1296 1297 1298 1299
    return -1;
  }

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

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

1302
  tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, NULL);
H
Hongze Cheng 已提交
1303
  tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c);
1304
  if (c != 0) {
1305 1306
    tdbTbcClose(pUidIdxc);
    terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
1307 1308 1309
    metaError("meta/table: invalide c: %" PRId32 " update tb tag val failed.", c);
    return -1;
  }
H
Hongze Cheng 已提交
1310

H
Hongze Cheng 已提交
1311
  tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
1312
  oversion = ((SUidIdxVal *)pData)[0].version;
H
Hongze Cheng 已提交
1313 1314

  // search table.db
H
Hongze Cheng 已提交
1315
  TBC     *pTbDbc = NULL;
H
Hongze Cheng 已提交
1316 1317
  SDecoder dc1 = {0};
  SDecoder dc2 = {0};
H
Hongze Cheng 已提交
1318

H
Hongze Cheng 已提交
1319
  /* get ctbEntry */
1320
  tdbTbcOpen(pMeta->pTbDb, &pTbDbc, NULL);
H
Hongze Cheng 已提交
1321
  tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c);
1322
  if (c != 0) {
1323 1324 1325
    tdbTbcClose(pUidIdxc);
    tdbTbcClose(pTbDbc);
    terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
1326 1327 1328 1329
    metaError("meta/table: invalide c: %" PRId32 " update tb tag val failed.", c);
    return -1;
  }

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

H
Hongze Cheng 已提交
1332 1333
  ctbEntry.pBuf = taosMemoryMalloc(nData);
  memcpy(ctbEntry.pBuf, pData, nData);
H
Hongze Cheng 已提交
1334 1335
  tDecoderInit(&dc1, ctbEntry.pBuf, nData);
  metaDecodeEntry(&dc1, &ctbEntry);
H
Hongze Cheng 已提交
1336

H
Hongze Cheng 已提交
1337
  /* get stbEntry*/
H
Hongze Cheng 已提交
1338
  tdbTbGet(pMeta->pUidIdx, &ctbEntry.ctbEntry.suid, sizeof(tb_uid_t), &pVal, &nVal);
1339 1340 1341 1342 1343
  if (!pVal) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto _err;
  }

H
Hongze Cheng 已提交
1344 1345
  tdbTbGet(pMeta->pTbDb, &((STbDbKey){.uid = ctbEntry.ctbEntry.suid, .version = ((SUidIdxVal *)pVal)[0].version}),
           sizeof(STbDbKey), (void **)&stbEntry.pBuf, &nVal);
H
Hongze Cheng 已提交
1346
  tdbFree(pVal);
H
Hongze Cheng 已提交
1347 1348
  tDecoderInit(&dc2, stbEntry.pBuf, nVal);
  metaDecodeEntry(&dc2, &stbEntry);
H
Hongze Cheng 已提交
1349 1350

  SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
H
Hongze Cheng 已提交
1351
  SSchema        *pColumn = NULL;
H
Hongze Cheng 已提交
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
  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) {
1364
    terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
H
Hongze Cheng 已提交
1365 1366
    goto _err;
  }
H
Hongze Cheng 已提交
1367

H
Hongze Cheng 已提交
1368
  ctbEntry.version = version;
H
Hongze Cheng 已提交
1369
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
wmmhello's avatar
wmmhello 已提交
1370
    ctbEntry.ctbEntry.pTags = taosMemoryMalloc(pAlterTbReq->nTagVal);
H
Hongze Cheng 已提交
1371
    if (ctbEntry.ctbEntry.pTags == NULL) {
wmmhello's avatar
wmmhello 已提交
1372 1373 1374
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
1375 1376
    memcpy((void *)ctbEntry.ctbEntry.pTags, pAlterTbReq->pTagVal, pAlterTbReq->nTagVal);
  } else {
C
Cary Xu 已提交
1377
    const STag *pOldTag = (const STag *)ctbEntry.ctbEntry.pTags;
H
Hongze Cheng 已提交
1378 1379
    STag       *pNewTag = NULL;
    SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
C
Cary Xu 已提交
1380
    if (!pTagArray) {
C
Cary Xu 已提交
1381 1382 1383
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
wmmhello's avatar
wmmhello 已提交
1384 1385 1386
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
      SSchema *pCol = &pTagSchema->pSchema[i];
      if (iCol == i) {
1387 1388 1389
        if (pAlterTbReq->isNull) {
          continue;
        }
wmmhello's avatar
wmmhello 已提交
1390 1391 1392 1393 1394 1395
        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;
1396
        } else {
wmmhello's avatar
wmmhello 已提交
1397 1398 1399
          memcpy(&val.i64, pAlterTbReq->pTagVal, pAlterTbReq->nTagVal);
        }
        taosArrayPush(pTagArray, &val);
wmmhello's avatar
wmmhello 已提交
1400
      } else {
wmmhello's avatar
wmmhello 已提交
1401
        STagVal val = {.cid = pCol->colId};
wmmhello's avatar
wmmhello 已提交
1402 1403
        if (tTagGet(pOldTag, &val)) {
          taosArrayPush(pTagArray, &val);
H
Hongze Cheng 已提交
1404 1405 1406
        }
      }
    }
C
Cary Xu 已提交
1407 1408
    if ((terrno = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag)) < 0) {
      taosArrayDestroy(pTagArray);
C
Cary Xu 已提交
1409 1410 1411
      goto _err;
    }
    ctbEntry.ctbEntry.pTags = (uint8_t *)pNewTag;
C
Cary Xu 已提交
1412
    taosArrayDestroy(pTagArray);
wmmhello's avatar
wmmhello 已提交
1413
  }
H
Hongze Cheng 已提交
1414

1415 1416
  metaWLock(pMeta);

H
Hongze Cheng 已提交
1417 1418 1419 1420
  // save to table.db
  metaSaveToTbDb(pMeta, &ctbEntry);

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

dengyihao's avatar
dengyihao 已提交
1423
  metaUpdateTagIdx(pMeta, &ctbEntry);
dengyihao's avatar
dengyihao 已提交
1424

1425 1426 1427 1428 1429
  if (NULL == ctbEntry.ctbEntry.pTags) {
    metaError("meta/table: null tags, update tag val failed.");
    goto _err;
  }

1430
  SCtbIdxKey ctbIdxKey = {.suid = ctbEntry.ctbEntry.suid, .uid = uid};
H
Hongze Cheng 已提交
1431
  tdbTbUpsert(pMeta->pCtbIdx, &ctbIdxKey, sizeof(ctbIdxKey), ctbEntry.ctbEntry.pTags,
1432
              ((STag *)(ctbEntry.ctbEntry.pTags))->len, pMeta->txn);
1433

1434
  metaUidCacheClear(pMeta, ctbEntry.ctbEntry.suid);
D
dapan1121 已提交
1435
  metaTbGroupCacheClear(pMeta, ctbEntry.ctbEntry.suid);        
1436

1437 1438
  metaULock(pMeta);

H
Hongze Cheng 已提交
1439 1440
  tDecoderClear(&dc1);
  tDecoderClear(&dc2);
M
Minglei Jin 已提交
1441
  taosMemoryFree((void *)ctbEntry.ctbEntry.pTags);
H
Hongze Cheng 已提交
1442 1443
  if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
  if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
H
Hongze Cheng 已提交
1444 1445
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
H
Hongze Cheng 已提交
1446
  return 0;
H
Hongze Cheng 已提交
1447 1448

_err:
H
Hongze Cheng 已提交
1449 1450
  tDecoderClear(&dc1);
  tDecoderClear(&dc2);
H
Hongze Cheng 已提交
1451 1452
  if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
  if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
H
Hongze Cheng 已提交
1453 1454
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
H
Hongze Cheng 已提交
1455
  return -1;
H
Hongze Cheng 已提交
1456 1457 1458
}

static int metaUpdateTableOptions(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
H
Hongze Cheng 已提交
1459 1460 1461 1462 1463 1464 1465 1466 1467
  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 已提交
1468 1469 1470 1471

  // search name index
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
  if (ret < 0) {
1472
    terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
wmmhello's avatar
wmmhello 已提交
1473
    return -1;
1474
  }
wmmhello's avatar
wmmhello 已提交
1475 1476 1477 1478 1479 1480 1481 1482

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

  // search uid index
  TBC *pUidIdxc = NULL;

1483
  tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, NULL);
wmmhello's avatar
wmmhello 已提交
1484
  tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c);
1485
  if (c != 0) {
1486
    tdbTbcClose(pUidIdxc);
1487 1488 1489
    metaError("meta/table: invalide c: %" PRId32 " update tb options failed.", c);
    return -1;
  }
wmmhello's avatar
wmmhello 已提交
1490 1491

  tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
1492
  oversion = ((SUidIdxVal *)pData)[0].version;
wmmhello's avatar
wmmhello 已提交
1493 1494 1495 1496

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

1497
  tdbTbcOpen(pMeta->pTbDb, &pTbDbc, NULL);
wmmhello's avatar
wmmhello 已提交
1498
  tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c);
1499
  if (c != 0) {
1500 1501
    tdbTbcClose(pUidIdxc);
    tdbTbcClose(pTbDbc);
1502 1503 1504 1505
    metaError("meta/table: invalide c: %" PRId32 " update tb options failed.", c);
    return -1;
  }

wmmhello's avatar
wmmhello 已提交
1506 1507 1508 1509 1510 1511 1512 1513
  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);
1514 1515
  if (ret != 0) {
    tDecoderClear(&dc);
1516 1517
    tdbTbcClose(pUidIdxc);
    tdbTbcClose(pTbDbc);
1518 1519 1520
    metaError("meta/table: invalide ret: %" PRId32 " alt tb options failed.", ret);
    return -1;
  }
wmmhello's avatar
wmmhello 已提交
1521 1522 1523 1524 1525

  entry.version = version;
  metaWLock(pMeta);
  // build SMetaEntry
  if (entry.type == TSDB_CHILD_TABLE) {
H
Hongze Cheng 已提交
1526
    if (pAlterTbReq->updateTTL) {
wmmhello's avatar
wmmhello 已提交
1527 1528 1529 1530
      metaDeleteTtlIdx(pMeta, &entry);
      entry.ctbEntry.ttlDays = pAlterTbReq->newTTL;
      metaUpdateTtlIdx(pMeta, &entry);
    }
H
Hongze Cheng 已提交
1531
    if (pAlterTbReq->newCommentLen >= 0) {
wmmhello's avatar
wmmhello 已提交
1532 1533 1534
      entry.ctbEntry.commentLen = pAlterTbReq->newCommentLen;
      entry.ctbEntry.comment = pAlterTbReq->newComment;
    }
wmmhello's avatar
wmmhello 已提交
1535
  } else {
H
Hongze Cheng 已提交
1536
    if (pAlterTbReq->updateTTL) {
wmmhello's avatar
wmmhello 已提交
1537 1538 1539 1540
      metaDeleteTtlIdx(pMeta, &entry);
      entry.ntbEntry.ttlDays = pAlterTbReq->newTTL;
      metaUpdateTtlIdx(pMeta, &entry);
    }
H
Hongze Cheng 已提交
1541
    if (pAlterTbReq->newCommentLen >= 0) {
wmmhello's avatar
wmmhello 已提交
1542 1543 1544
      entry.ntbEntry.commentLen = pAlterTbReq->newCommentLen;
      entry.ntbEntry.comment = pAlterTbReq->newComment;
    }
1545
  }
wmmhello's avatar
wmmhello 已提交
1546 1547 1548

  // save to table db
  metaSaveToTbDb(pMeta, &entry);
H
Hongze Cheng 已提交
1549
  metaUpdateUidIdx(pMeta, &entry);
wmmhello's avatar
wmmhello 已提交
1550 1551 1552 1553
  metaULock(pMeta);

  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
1554
  tDecoderClear(&dc);
wmmhello's avatar
wmmhello 已提交
1555
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
H
Hongze Cheng 已提交
1556 1557 1558
  return 0;
}

dengyihao's avatar
dengyihao 已提交
1559 1560 1561 1562 1563 1564
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 已提交
1565
  tb_uid_t    uid, suid;
dengyihao's avatar
dengyihao 已提交
1566 1567 1568
  int64_t     oversion;
  const void *pData = NULL;
  int         nData = 0;
dengyihao's avatar
dengyihao 已提交
1569
  SDecoder    dc = {0};
dengyihao's avatar
dengyihao 已提交
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585

  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;
  }
  uid = *(tb_uid_t *)pVal;
  tdbFree(pVal);
  pVal = NULL;

dengyihao's avatar
dengyihao 已提交
1586 1587 1588 1589
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(tb_uid_t), &pVal, &nVal) == -1) {
    ret = -1;
    goto _err;
  }
dengyihao's avatar
dengyihao 已提交
1590
  suid = ((SUidIdxVal *)pVal)[0].suid;
dengyihao's avatar
dengyihao 已提交
1591

dengyihao's avatar
dengyihao 已提交
1592
  STbDbKey tbDbKey = {0};
dengyihao's avatar
dengyihao 已提交
1593
  tbDbKey.uid = suid;
dengyihao's avatar
dengyihao 已提交
1594 1595 1596 1597 1598 1599 1600
  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 已提交
1601

dengyihao's avatar
dengyihao 已提交
1602
  // Get target schema info
dengyihao's avatar
dengyihao 已提交
1603 1604 1605
  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 已提交
1606 1607
    goto _err;
  }
dengyihao's avatar
dengyihao 已提交
1608 1609
  SSchema *pCol = NULL;
  int32_t  iCol = 0;
dengyihao's avatar
dengyihao 已提交
1610
  for (;;) {
dengyihao's avatar
dengyihao 已提交
1611
    pCol = NULL;
dengyihao's avatar
dengyihao 已提交
1612
    if (iCol >= pTagSchema->nCols) break;
dengyihao's avatar
dengyihao 已提交
1613 1614
    pCol = &pTagSchema->pSchema[iCol];
    if (strcmp(pCol->name, pAlterTbReq->tagName) == 0) break;
dengyihao's avatar
dengyihao 已提交
1615 1616 1617
    iCol++;
  }

dengyihao's avatar
dengyihao 已提交
1618 1619
  if (iCol == 0) {
    terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
dengyihao's avatar
dengyihao 已提交
1620 1621
    goto _err;
  }
dengyihao's avatar
dengyihao 已提交
1622 1623
  if (pCol == NULL) {
    terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
dengyihao's avatar
dengyihao 已提交
1624 1625
    goto _err;
  }
dengyihao's avatar
dengyihao 已提交
1626

dengyihao's avatar
dengyihao 已提交
1627 1628 1629 1630 1631
  /*
   * iterator all pTdDbc by uid and version
   */
  TBC *pCtbIdxc = NULL;
  tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL);
dengyihao's avatar
dengyihao 已提交
1632
  int rc = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c);
dengyihao's avatar
dengyihao 已提交
1633 1634
  if (rc < 0) {
    tdbTbcClose(pCtbIdxc);
dengyihao's avatar
dengyihao 已提交
1635 1636
    goto _err;
  }
dengyihao's avatar
dengyihao 已提交
1637 1638 1639 1640 1641 1642
  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 已提交
1643
      tdbFree(pKey);
dengyihao's avatar
dengyihao 已提交
1644 1645 1646
      tdbFree(pVal);
      continue;
    }
dengyihao's avatar
dengyihao 已提交
1647 1648
    STagIdxKey *pTagIdxKey = NULL;
    int32_t     nTagIdxKey;
dengyihao's avatar
dengyihao 已提交
1649

dengyihao's avatar
dengyihao 已提交
1650 1651
    const void *pTagData = NULL;
    int32_t     nTagData = 0;
dengyihao's avatar
dengyihao 已提交
1652

dengyihao's avatar
dengyihao 已提交
1653 1654 1655 1656 1657 1658 1659 1660 1661
    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 已提交
1662
    if (metaCreateTagIdxKey(suid, pCol->colId, pTagData, nTagData, pCol->type, uid, &pTagIdxKey, &nTagIdxKey) < 0) {
dengyihao's avatar
dengyihao 已提交
1663 1664 1665 1666 1667 1668
      metaDestroyTagIdxKey(pTagIdxKey);
      goto _err;
    }
    tdbTbUpsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn);
    metaDestroyTagIdxKey(pTagIdxKey);
  }
dengyihao's avatar
dengyihao 已提交
1669
  return 0;
dengyihao's avatar
dengyihao 已提交
1670 1671

_err:
dengyihao's avatar
dengyihao 已提交
1672 1673 1674 1675 1676 1677
  // tDecoderClear(&dc1);
  // tDecoderClear(&dc2);
  // if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
  // if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
  // tdbTbcClose(pTbDbc);
  // tdbTbcClose(pUidIdxc);
dengyihao's avatar
dengyihao 已提交
1678
  return -1;
dengyihao's avatar
dengyihao 已提交
1679
}
dengyihao's avatar
dengyihao 已提交
1680

dengyihao's avatar
dengyihao 已提交
1681 1682 1683 1684 1685
typedef struct SMetaPair {
  void *key;
  int   nkey;
} SMetaPair;

dengyihao's avatar
dengyihao 已提交
1686 1687 1688 1689 1690 1691
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 已提交
1692
  tb_uid_t    suid;
dengyihao's avatar
dengyihao 已提交
1693 1694 1695
  int64_t     oversion;
  const void *pData = NULL;
  int         nData = 0;
dengyihao's avatar
dengyihao 已提交
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
  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 已提交
1753

dengyihao's avatar
dengyihao 已提交
1754
  if (IS_IDX_ON(pCol)) {
dengyihao's avatar
dengyihao 已提交
1755 1756 1757 1758
    terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
    goto _err;
  }

dengyihao's avatar
dengyihao 已提交
1759
  SArray *tagIdxList = taosArrayInit(512, sizeof(SMetaPair));
dengyihao's avatar
dengyihao 已提交
1760 1761 1762 1763 1764 1765 1766 1767 1768 1769

  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 已提交
1770
    if (pIdxKey->suid != suid || pIdxKey->cid != pCol->colId) {
dengyihao's avatar
dengyihao 已提交
1771 1772 1773 1774
      tdbFree(pKey);
      tdbFree(pVal);
      continue;
    }
dengyihao's avatar
dengyihao 已提交
1775 1776 1777

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

dengyihao's avatar
dengyihao 已提交
1781
  metaWLock(pMeta);
dengyihao's avatar
dengyihao 已提交
1782 1783 1784 1785
  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 已提交
1786
  metaULock(pMeta);
dengyihao's avatar
dengyihao 已提交
1787 1788 1789

  taosArrayDestroy(tagIdxList);

dengyihao's avatar
dengyihao 已提交
1790
  // set pCol->flags; INDEX_ON
dengyihao's avatar
dengyihao 已提交
1791
  return 0;
dengyihao's avatar
dengyihao 已提交
1792 1793
_err:
  return -1;
dengyihao's avatar
dengyihao 已提交
1794 1795
}

D
dapan1121 已提交
1796
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
H
Hongze Cheng 已提交
1797 1798 1799 1800 1801
  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 已提交
1802
      return metaAlterTableColumn(pMeta, version, pReq, pMetaRsp);
H
Hongze Cheng 已提交
1803 1804 1805 1806
    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 已提交
1807 1808 1809 1810
    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 已提交
1811 1812 1813 1814 1815 1816 1817
    default:
      terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
      return -1;
      break;
  }
}

H
Hongze Cheng 已提交
1818
static int metaSaveToTbDb(SMeta *pMeta, const SMetaEntry *pME) {
H
Hongze Cheng 已提交
1819
  STbDbKey tbDbKey;
H
Hongze Cheng 已提交
1820 1821
  void    *pKey = NULL;
  void    *pVal = NULL;
H
Hongze Cheng 已提交
1822 1823
  int      kLen = 0;
  int      vLen = 0;
H
Hongze Cheng 已提交
1824
  SEncoder coder = {0};
H
Hongze Cheng 已提交
1825 1826

  // set key and value
H
Hongze Cheng 已提交
1827
  tbDbKey.version = pME->version;
H
Hongze Cheng 已提交
1828 1829
  tbDbKey.uid = pME->uid;

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

H
Hongze Cheng 已提交
1833 1834
  pKey = &tbDbKey;
  kLen = sizeof(tbDbKey);
H
Hongze Cheng 已提交
1835

wafwerar's avatar
wafwerar 已提交
1836 1837 1838
  int32_t ret = 0;
  tEncodeSize(metaEncodeEntry, pME, vLen, ret);
  if (ret < 0) {
H
Hongze Cheng 已提交
1839 1840 1841 1842 1843 1844 1845 1846 1847
    goto _err;
  }

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

H
Hongze Cheng 已提交
1848
  tEncoderInit(&coder, pVal, vLen);
H
Hongze Cheng 已提交
1849 1850 1851 1852 1853

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

H
Hongze Cheng 已提交
1854
  tEncoderClear(&coder);
H
Hongze Cheng 已提交
1855 1856

  // write to table.db
1857
  if (tdbTbInsert(pMeta->pTbDb, pKey, kLen, pVal, vLen, pMeta->txn) < 0) {
H
Hongze Cheng 已提交
1858 1859 1860 1861 1862 1863 1864
    goto _err;
  }

  taosMemoryFree(pVal);
  return 0;

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

H
Hongze Cheng 已提交
1868 1869 1870 1871
  taosMemoryFree(pVal);
  return -1;
}

H
Hongze Cheng 已提交
1872
static int metaUpdateUidIdx(SMeta *pMeta, const SMetaEntry *pME) {
H
Hongze Cheng 已提交
1873 1874 1875 1876 1877 1878 1879
  // upsert cache
  SMetaInfo info;
  metaGetEntryInfo(pME, &info);
  metaCacheUpsert(pMeta, &info);

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

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

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

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

H
Hongze Cheng 已提交
1891
static int metaUpdateTtlIdx(SMeta *pMeta, const SMetaEntry *pME) {
1892 1893
  STtlIdxKey ttlKey = {0};
  metaBuildTtlIdxKey(&ttlKey, pME);
H
Hongze Cheng 已提交
1894
  if (ttlKey.dtime == 0) return 0;
1895
  return tdbTbUpsert(pMeta->pTtlIdx, &ttlKey, sizeof(ttlKey), NULL, 0, pMeta->txn);
H
Hongze Cheng 已提交
1896 1897
}

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

1901
  return tdbTbUpsert(pMeta->pCtbIdx, &ctbIdxKey, sizeof(ctbIdxKey), pME->ctbEntry.pTags,
1902
                     ((STag *)(pME->ctbEntry.pTags))->len, pMeta->txn);
H
Hongze Cheng 已提交
1903 1904
}

wmmhello's avatar
wmmhello 已提交
1905
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
1906
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
dengyihao's avatar
dengyihao 已提交
1907 1908 1909 1910 1911
  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 已提交
1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922

  *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 已提交
1923 1924 1925 1926

  // refactor
  if (IS_VAR_DATA_TYPE(type)) {
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
dengyihao's avatar
dengyihao 已提交
1927
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
dengyihao's avatar
dengyihao 已提交
1928 1929
    *(tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData) = uid;
  } else {
dengyihao's avatar
dengyihao 已提交
1930
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
dengyihao's avatar
dengyihao 已提交
1931 1932
    *(tb_uid_t *)((*ppTagIdxKey)->data + nTagData) = uid;
  }
H
Hongze Cheng 已提交
1933 1934 1935 1936 1937 1938 1939 1940 1941

  return 0;
}

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

static int metaUpdateTagIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry) {
H
Hongze Cheng 已提交
1942
  void          *pData = NULL;
H
Hongze Cheng 已提交
1943 1944 1945
  int            nData = 0;
  STbDbKey       tbDbKey = {0};
  SMetaEntry     stbEntry = {0};
H
Hongze Cheng 已提交
1946
  STagIdxKey    *pTagIdxKey = NULL;
H
Hongze Cheng 已提交
1947
  int32_t        nTagIdxKey;
M
Minglei Jin 已提交
1948 1949
  const SSchema *pTagColumn;
  const void    *pTagData = NULL;
C
Cary Xu 已提交
1950
  int32_t        nTagData = 0;
H
Hongze Cheng 已提交
1951
  SDecoder       dc = {0};
1952
  int32_t        ret = 0;
H
Hongze Cheng 已提交
1953
  // get super table
H
Hongze Cheng 已提交
1954
  if (tdbTbGet(pMeta->pUidIdx, &pCtbEntry->ctbEntry.suid, sizeof(tb_uid_t), &pData, &nData) != 0) {
1955 1956 1957
    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;
1958 1959
    ret = -1;
    goto end;
wmmhello's avatar
wmmhello 已提交
1960
  }
H
Hongze Cheng 已提交
1961
  tbDbKey.uid = pCtbEntry->ctbEntry.suid;
H
Hongze Cheng 已提交
1962
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
H
Hongze Cheng 已提交
1963
  tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData);
H
Hongze Cheng 已提交
1964 1965

  tDecoderInit(&dc, pData, nData);
M
Minglei Jin 已提交
1966 1967 1968 1969
  ret = metaDecodeEntry(&dc, &stbEntry);
  if (ret < 0) {
    goto end;
  }
H
Hongze Cheng 已提交
1970

M
Minglei Jin 已提交
1971 1972 1973 1974
  if (stbEntry.stbEntry.schemaTag.pSchema == NULL) {
    goto end;
  }

dengyihao's avatar
dengyihao 已提交
1975 1976 1977
  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 已提交
1978
    STagVal tagVal = {.cid = pTagColumn->colId};
C
Cary Xu 已提交
1979

dengyihao's avatar
dengyihao 已提交
1980 1981
    pTagData = pCtbEntry->ctbEntry.pTags;
    nTagData = ((const STag *)pCtbEntry->ctbEntry.pTags)->len;
1982 1983
    ret = metaSaveJsonVarToIdx(pMeta, pCtbEntry, pTagColumn);
    goto end;
dengyihao's avatar
dengyihao 已提交
1984 1985 1986
  } else {
    for (int i = 0; i < pTagSchema->nCols; i++) {
      pTagColumn = &pTagSchema->pSchema[i];
dengyihao's avatar
dengyihao 已提交
1987
      if (i != 0 && !IS_IDX_ON(pTagColumn)) continue;
dengyihao's avatar
dengyihao 已提交
1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007

      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 已提交
2008
    }
H
Hongze Cheng 已提交
2009
  }
2010
end:
dengyihao's avatar
dengyihao 已提交
2011
  // metaDestroyTagIdxKey(pTagIdxKey);
H
Hongze Cheng 已提交
2012 2013
  tDecoderClear(&dc);
  tdbFree(pData);
2014
  return ret;
H
Hongze Cheng 已提交
2015 2016
}

H
Hongze Cheng 已提交
2017
static int metaSaveToSkmDb(SMeta *pMeta, const SMetaEntry *pME) {
H
Hongze Cheng 已提交
2018
  SEncoder              coder = {0};
H
Hongze Cheng 已提交
2019
  void                 *pVal = NULL;
H
Hongze Cheng 已提交
2020 2021 2022 2023 2024 2025
  int                   vLen = 0;
  int                   rcode = 0;
  SSkmDbKey             skmDbKey = {0};
  const SSchemaWrapper *pSW;

  if (pME->type == TSDB_SUPER_TABLE) {
2026
    pSW = &pME->stbEntry.schemaRow;
H
Hongze Cheng 已提交
2027
  } else if (pME->type == TSDB_NORMAL_TABLE) {
2028
    pSW = &pME->ntbEntry.schemaRow;
H
Hongze Cheng 已提交
2029
  } else {
2030 2031
    metaError("meta/table: invalide table type: %" PRId8 " save skm db failed.", pME->type);
    return TSDB_CODE_FAILED;
H
Hongze Cheng 已提交
2032 2033
  }

H
Hongze Cheng 已提交
2034
  skmDbKey.uid = pME->uid;
2035
  skmDbKey.sver = pSW->version;
H
Hongze Cheng 已提交
2036

2037 2038 2039 2040 2041
  // 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 已提交
2042
  // encode schema
wafwerar's avatar
wafwerar 已提交
2043 2044 2045
  int32_t ret = 0;
  tEncodeSize(tEncodeSSchemaWrapper, pSW, vLen, ret);
  if (ret < 0) return -1;
H
Hongze Cheng 已提交
2046
  pVal = taosMemoryMalloc(vLen);
H
Hongze Cheng 已提交
2047 2048 2049 2050 2051 2052
  if (pVal == NULL) {
    rcode = -1;
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

H
Hongze Cheng 已提交
2053
  tEncoderInit(&coder, pVal, vLen);
H
Hongze Cheng 已提交
2054 2055
  tEncodeSSchemaWrapper(&coder, pSW);

2056
  if (tdbTbInsert(pMeta->pSkmDb, &skmDbKey, sizeof(skmDbKey), pVal, vLen, pMeta->txn) < 0) {
H
Hongze Cheng 已提交
2057 2058 2059 2060
    rcode = -1;
    goto _exit;
  }

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

H
Hongze Cheng 已提交
2064
_exit:
H
Hongze Cheng 已提交
2065
  taosMemoryFree(pVal);
H
Hongze Cheng 已提交
2066
  tEncoderClear(&coder);
H
Hongze Cheng 已提交
2067 2068 2069
  return rcode;
}

H
Hongze Cheng 已提交
2070
int metaHandleEntry(SMeta *pMeta, const SMetaEntry *pME) {
2071 2072
  int32_t code = 0;
  int32_t line = 0;
H
Hongze Cheng 已提交
2073 2074
  metaWLock(pMeta);

H
Hongze Cheng 已提交
2075
  // save to table.db
2076 2077
  code = metaSaveToTbDb(pMeta, pME);
  VND_CHECK_CODE(code, line, _err);
H
Hongze Cheng 已提交
2078 2079

  // update uid.idx
2080 2081
  code = metaUpdateUidIdx(pMeta, pME);
  VND_CHECK_CODE(code, line, _err);
H
Hongze Cheng 已提交
2082 2083

  // update name.idx
2084 2085
  code = metaUpdateNameIdx(pMeta, pME);
  VND_CHECK_CODE(code, line, _err);
H
Hongze Cheng 已提交
2086 2087 2088

  if (pME->type == TSDB_CHILD_TABLE) {
    // update ctb.idx
2089 2090
    code = metaUpdateCtbIdx(pMeta, pME);
    VND_CHECK_CODE(code, line, _err);
H
Hongze Cheng 已提交
2091 2092

    // update tag.idx
2093 2094
    code = metaUpdateTagIdx(pMeta, pME);
    VND_CHECK_CODE(code, line, _err);
H
Hongze Cheng 已提交
2095 2096
  } else {
    // update schema.db
2097 2098
    code = metaSaveToSkmDb(pMeta, pME);
    VND_CHECK_CODE(code, line, _err);
C
Cary Xu 已提交
2099 2100

    if (pME->type == TSDB_SUPER_TABLE) {
2101 2102
      code = metaUpdateSuidIdx(pMeta, pME);
      VND_CHECK_CODE(code, line, _err);
H
Hongze Cheng 已提交
2103
    }
H
Hongze Cheng 已提交
2104 2105
  }

2106 2107
  code = metaUpdateCtimeIdx(pMeta, pME);
  VND_CHECK_CODE(code, line, _err);
dengyihao's avatar
dengyihao 已提交
2108 2109

  if (pME->type == TSDB_NORMAL_TABLE) {
2110 2111
    code = metaUpdateNcolIdx(pMeta, pME);
    VND_CHECK_CODE(code, line, _err);
dengyihao's avatar
dengyihao 已提交
2112 2113
  }

H
Hongze Cheng 已提交
2114
  if (pME->type != TSDB_SUPER_TABLE) {
2115 2116
    code = metaUpdateTtlIdx(pMeta, pME);
    VND_CHECK_CODE(code, line, _err);
H
Hongze Cheng 已提交
2117 2118
  }

H
Hongze Cheng 已提交
2119
  metaULock(pMeta);
2120 2121
  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 已提交
2122
  return 0;
H
Hongze Cheng 已提交
2123 2124 2125

_err:
  metaULock(pMeta);
2126 2127
  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 已提交
2128
  return -1;
2129
}
2130

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