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

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

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

H
Hongze Cheng 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
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 {
    ASSERT(0);
  }
}

dengyihao's avatar
dengyihao 已提交
47
static int metaUpdateMetaRsp(tb_uid_t uid, char *tbName, SSchemaWrapper *pSchema, STableMetaRsp *pMetaRsp) {
D
dapan1121 已提交
48 49 50 51 52 53
  pMetaRsp->pSchemas = taosMemoryMalloc(pSchema->nCols * sizeof(SSchema));
  if (NULL == pMetaRsp->pSchemas) {
    terrno = TSDB_CODE_VND_OUT_OF_MEMORY;
    return -1;
  }

H
Hongze Cheng 已提交
54
  strncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
D
dapan1121 已提交
55 56 57 58 59 60 61 62 63 64
  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 已提交
65 66 67 68 69
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 已提交
70
  void       *data = pCtbEntry->ctbEntry.pTags;
dengyihao's avatar
dengyihao 已提交
71 72 73 74 75 76 77 78 79 80 81
  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 已提交
82

dengyihao's avatar
dengyihao 已提交
83 84 85 86 87
  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 已提交
88

H
Hongze Cheng 已提交
89
    char   *key = pTagVal->pKey;
dengyihao's avatar
dengyihao 已提交
90
    int32_t nKey = strlen(key);
dengyihao's avatar
dengyihao 已提交
91 92 93

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

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

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

H
Hongze Cheng 已提交
187
int metaCreateSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
H
Hongze Cheng 已提交
188
  SMetaEntry  me = {0};
H
Hongze Cheng 已提交
189 190 191 192
  int         kLen = 0;
  int         vLen = 0;
  const void *pKey = NULL;
  const void *pVal = NULL;
H
Hongze Cheng 已提交
193
  void       *pBuf = NULL;
H
Hongze Cheng 已提交
194
  int32_t     szBuf = 0;
H
Hongze Cheng 已提交
195
  void       *p = NULL;
H
Hongze Cheng 已提交
196 197

  // validate req
198 199
  void *pData = NULL;
  int   nData = 0;
M
Minglei Jin 已提交
200
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &pData, &nData) == 0) {
201 202 203 204 205
    tb_uid_t uid = *(tb_uid_t *)pData;
    tdbFree(pData);
    SMetaInfo info;
    metaGetInfo(pMeta, uid, &info);
    if (info.uid == info.suid) {
206 207
      return 0;
    } else {
208
      terrno = TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
209 210
      return -1;
    }
H
Hongze Cheng 已提交
211
  }
H
Hongze Cheng 已提交
212 213

  // set structs
H
Hongze Cheng 已提交
214
  me.version = version;
H
Hongze Cheng 已提交
215 216 217
  me.type = TSDB_SUPER_TABLE;
  me.uid = pReq->suid;
  me.name = pReq->name;
218
  me.stbEntry.schemaRow = pReq->schemaRow;
H
Hongze Cheng 已提交
219
  me.stbEntry.schemaTag = pReq->schemaTag;
C
Cary Xu 已提交
220 221 222 223
  if (pReq->rollup) {
    TABLE_SET_ROLLUP(me.flags);
    me.stbEntry.rsmaParam = pReq->rsmaParam;
  }
H
Hongze Cheng 已提交
224

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

227 228
  ++pMeta->pVnode->config.vndStats.numOfSTables;

S
Shengliang Guan 已提交
229
  metaDebug("vgId:%d, stb:%s is created, suid:%" PRId64, TD_VID(pMeta->pVnode), pReq->name, pReq->suid);
H
Hongze Cheng 已提交
230 231 232 233

  return 0;

_err:
M
Minglei Jin 已提交
234 235
  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 已提交
236 237 238
  return -1;
}

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

H
Hongze Cheng 已提交
255
  // drop all child tables
256
  TBC *pCtbIdxc = NULL;
H
Hongze Cheng 已提交
257

H
Hongze Cheng 已提交
258
  tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, &pMeta->txn);
H
Hongze Cheng 已提交
259 260
  rc = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = pReq->suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c);
  if (rc < 0) {
H
Hongze Cheng 已提交
261
    tdbTbcClose(pCtbIdxc);
H
Hongze Cheng 已提交
262 263
    metaWLock(pMeta);
    goto _drop_super_table;
H
Hongze Cheng 已提交
264 265 266
  }

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

H
Hongze Cheng 已提交
270 271 272 273 274
    if (((SCtbIdxKey *)pKey)->suid < pReq->suid) {
      continue;
    } else if (((SCtbIdxKey *)pKey)->suid > pReq->suid) {
      break;
    }
H
Hongze Cheng 已提交
275

276
    taosArrayPush(tbUidList, &(((SCtbIdxKey *)pKey)->uid));
H
Hongze Cheng 已提交
277 278 279 280 281
  }

  tdbTbcClose(pCtbIdxc);

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

283 284
  for (int32_t iChild = 0; iChild < taosArrayGetSize(tbUidList); iChild++) {
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUidList, iChild);
H
Hongze Cheng 已提交
285
    metaDropTableByUid(pMeta, uid, NULL);
H
Hongze Cheng 已提交
286 287
  }

H
Hongze Cheng 已提交
288 289 290
  // drop super table
_drop_super_table:
  tdbTbGet(pMeta->pUidIdx, &pReq->suid, sizeof(tb_uid_t), &pData, &nData);
H
Hongze Cheng 已提交
291 292
  tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = ((SUidIdxVal *)pData)[0].version, .uid = pReq->suid},
              sizeof(STbDbKey), &pMeta->txn);
H
Hongze Cheng 已提交
293 294
  tdbTbDelete(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &pMeta->txn);
  tdbTbDelete(pMeta->pUidIdx, &pReq->suid, sizeof(tb_uid_t), &pMeta->txn);
C
Cary Xu 已提交
295
  tdbTbDelete(pMeta->pSuidIdx, &pReq->suid, sizeof(tb_uid_t), &pMeta->txn);
H
Hongze Cheng 已提交
296 297 298

  metaULock(pMeta);

H
Hongze Cheng 已提交
299
_exit:
H
Hongze Cheng 已提交
300 301
  tdbFree(pKey);
  tdbFree(pData);
C
Cary Xu 已提交
302
  metaDebug("vgId:%d, super table %s uid:%" PRId64 " is dropped", TD_VID(pMeta->pVnode), pReq->name, pReq->suid);
H
Hongze Cheng 已提交
303 304 305
  return 0;
}

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

H
Hongze Cheng 已提交
318 319
  tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, &pMeta->txn);
  ret = tdbTbcMoveTo(pUidIdxc, &pReq->suid, sizeof(tb_uid_t), &c);
H
Hongze Cheng 已提交
320
  if (ret < 0 || c) {
321 322 323
    tdbTbcClose(pUidIdxc);

    terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
H
Hongze Cheng 已提交
324 325 326
    return -1;
  }

H
Hongze Cheng 已提交
327
  ret = tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
328
  if (ret < 0) {
M
Minglei Jin 已提交
329 330
    tdbTbcClose(pUidIdxc);

M
Minglei Jin 已提交
331
    terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
H
Hongze Cheng 已提交
332 333 334
    return -1;
  }

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

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

H
Hongze Cheng 已提交
341
  ret = tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
342 343
  ASSERT(ret == 0);

H
Hongze Cheng 已提交
344 345 346
  oStbEntry.pBuf = taosMemoryMalloc(nData);
  memcpy(oStbEntry.pBuf, pData, nData);
  tDecoderInit(&dc, oStbEntry.pBuf, nData);
H
Hongze Cheng 已提交
347 348 349 350 351 352
  metaDecodeEntry(&dc, &oStbEntry);

  nStbEntry.version = version;
  nStbEntry.type = TSDB_SUPER_TABLE;
  nStbEntry.uid = pReq->suid;
  nStbEntry.name = pReq->name;
353
  nStbEntry.stbEntry.schemaRow = pReq->schemaRow;
H
Hongze Cheng 已提交
354 355 356 357
  nStbEntry.stbEntry.schemaTag = pReq->schemaTag;

  metaWLock(pMeta);
  // compare two entry
358 359
  if (oStbEntry.stbEntry.schemaRow.version != pReq->schemaRow.version) {
    metaSaveToSkmDb(pMeta, &nStbEntry);
H
Hongze Cheng 已提交
360 361 362 363 364 365
  }

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

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

368 369
  metaStatsCacheDrop(pMeta, nStbEntry.uid);

H
Hongze Cheng 已提交
370
  metaULock(pMeta);
371 372

  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
H
Hongze Cheng 已提交
373
  tDecoderClear(&dc);
H
Hongze Cheng 已提交
374 375
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
H
Hongze Cheng 已提交
376 377 378
  return 0;
}

379
int metaCreateTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **pMetaRsp) {
H
Hongze Cheng 已提交
380 381
  SMetaEntry  me = {0};
  SMetaReader mr = {0};
H
Hongze Cheng 已提交
382 383 384 385 386

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

389
  if (pReq->type == TSDB_CHILD_TABLE) {
390
    tb_uid_t suid = metaGetTableEntryUidByName(pMeta, pReq->ctb.stbName);
391 392 393 394 395 396
    if (suid != pReq->ctb.suid) {
      terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
      return -1;
    }
  }

H
Hongze Cheng 已提交
397
  // validate req
H
Hongze Cheng 已提交
398
  metaReaderInit(&mr, pMeta, 0);
H
Hongze Cheng 已提交
399
  if (metaGetTableEntryByName(&mr, pReq->name) == 0) {
H
Hongze Cheng 已提交
400 401 402 403
    pReq->uid = mr.me.uid;
    if (pReq->type == TSDB_CHILD_TABLE) {
      pReq->ctb.suid = mr.me.ctbEntry.suid;
    }
H
Hongze Cheng 已提交
404 405 406
    terrno = TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
    metaReaderClear(&mr);
    return -1;
C
Cary Xu 已提交
407 408
  } else if (terrno == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
    terrno = TSDB_CODE_SUCCESS;
H
Hongze Cheng 已提交
409
  }
H
Hongze Cheng 已提交
410
  metaReaderClear(&mr);
H
Hongze Cheng 已提交
411 412

  // build SMetaEntry
H
Hongze Cheng 已提交
413
  me.version = version;
H
Hongze Cheng 已提交
414 415 416 417 418 419
  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 已提交
420
    me.ctbEntry.commentLen = pReq->commentLen;
wmmhello's avatar
wmmhello 已提交
421
    me.ctbEntry.comment = pReq->comment;
H
Hongze Cheng 已提交
422 423
    me.ctbEntry.suid = pReq->ctb.suid;
    me.ctbEntry.pTags = pReq->ctb.pTag;
424

wmmhello's avatar
wmmhello 已提交
425
#ifdef TAG_FILTER_DEBUG
dengyihao's avatar
dengyihao 已提交
426 427
    SArray *pTagVals = NULL;
    int32_t code = tTagToValArray((STag *)pReq->ctb.pTag, &pTagVals);
wmmhello's avatar
wmmhello 已提交
428
    for (int i = 0; i < taosArrayGetSize(pTagVals); i++) {
dengyihao's avatar
dengyihao 已提交
429
      STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
wmmhello's avatar
wmmhello 已提交
430 431

      if (IS_VAR_DATA_TYPE(pTagVal->type)) {
dengyihao's avatar
dengyihao 已提交
432
        char *buf = taosMemoryCalloc(pTagVal->nData + 1, 1);
wmmhello's avatar
wmmhello 已提交
433
        memcpy(buf, pTagVal->pData, pTagVal->nData);
dengyihao's avatar
dengyihao 已提交
434 435
        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 已提交
436 437 438 439
        taosMemoryFree(buf);
      } else {
        double val = 0;
        GET_TYPED_DATA(val, double, pTagVal->type, &pTagVal->i64);
dengyihao's avatar
dengyihao 已提交
440 441
        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 已提交
442 443
      }
    }
wmmhello's avatar
wmmhello 已提交
444
#endif
wmmhello's avatar
wmmhello 已提交
445

446
    ++pMeta->pVnode->config.vndStats.numOfCTables;
H
Hongze Cheng 已提交
447 448 449
  } else {
    me.ntbEntry.ctime = pReq->ctime;
    me.ntbEntry.ttlDays = pReq->ttl;
wmmhello's avatar
wmmhello 已提交
450
    me.ntbEntry.commentLen = pReq->commentLen;
wmmhello's avatar
wmmhello 已提交
451
    me.ntbEntry.comment = pReq->comment;
452 453
    me.ntbEntry.schemaRow = pReq->ntb.schemaRow;
    me.ntbEntry.ncid = me.ntbEntry.schemaRow.pSchema[me.ntbEntry.schemaRow.nCols - 1].colId + 1;
454 455

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

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

461 462 463 464 465 466 467 468 469 470
  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 {
471
        metaUpdateMetaRsp(pReq->uid, pReq->name, &pReq->ntb.schemaRow, *pMetaRsp);
472 473 474 475
      }
    }
  }

S
Shengliang Guan 已提交
476
  metaDebug("vgId:%d, table:%s uid %" PRId64 " is created, type:%" PRId8, TD_VID(pMeta->pVnode), pReq->name, pReq->uid,
H
Hongze Cheng 已提交
477
            pReq->type);
H
refact  
Hongze Cheng 已提交
478
  return 0;
H
Hongze Cheng 已提交
479 480

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

486
int metaDropTable(SMeta *pMeta, int64_t version, SVDropTbReq *pReq, SArray *tbUids, tb_uid_t *tbUid) {
H
Hongze Cheng 已提交
487
  void    *pData = NULL;
H
Hongze Cheng 已提交
488 489 490 491
  int      nData = 0;
  int      rc = 0;
  tb_uid_t uid;
  int      type;
H
more  
Hongze Cheng 已提交
492

H
Hongze Cheng 已提交
493 494 495
  rc = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &pData, &nData);
  if (rc < 0) {
    terrno = TSDB_CODE_VND_TABLE_NOT_EXIST;
H
more  
Hongze Cheng 已提交
496 497
    return -1;
  }
H
Hongze Cheng 已提交
498 499
  uid = *(tb_uid_t *)pData;

H
Hongze Cheng 已提交
500 501 502
  metaWLock(pMeta);
  metaDropTableByUid(pMeta, uid, &type);
  metaULock(pMeta);
H
Hongze Cheng 已提交
503

504
  if ((type == TSDB_CHILD_TABLE || type == TSDB_NORMAL_TABLE) && tbUids) {
H
Hongze Cheng 已提交
505
    taosArrayPush(tbUids, &uid);
H
Hongze Cheng 已提交
506
  }
H
Hongze Cheng 已提交
507

508 509 510 511
  if ((type == TSDB_CHILD_TABLE) && tbUid) {
    *tbUid = uid;
  }

H
Hongze Cheng 已提交
512 513 514
  tdbFree(pData);
  return 0;
}
H
Hongze Cheng 已提交
515

516 517
int metaTtlDropTable(SMeta *pMeta, int64_t ttl, SArray *tbUids) {
  int ret = metaTtlSmaller(pMeta, ttl, tbUids);
H
Hongze Cheng 已提交
518
  if (ret != 0) {
519 520
    return ret;
  }
521
  if (taosArrayGetSize(tbUids) == 0) {
522 523 524 525
    return 0;
  }

  metaWLock(pMeta);
526 527 528
  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 已提交
529
    metaDebug("ttl drop table:%" PRId64, *uid);
530 531 532 533 534
  }
  metaULock(pMeta);
  return 0;
}

H
Hongze Cheng 已提交
535 536 537
static void metaBuildTtlIdxKey(STtlIdxKey *ttlKey, const SMetaEntry *pME) {
  int64_t ttlDays;
  int64_t ctime;
538 539 540 541 542 543 544 545 546 547 548 549
  if (pME->type == TSDB_CHILD_TABLE) {
    ctime = pME->ctbEntry.ctime;
    ttlDays = pME->ctbEntry.ttlDays;
  } else if (pME->type == TSDB_NORMAL_TABLE) {
    ctime = pME->ntbEntry.ctime;
    ttlDays = pME->ntbEntry.ttlDays;
  } else {
    ASSERT(0);
  }

  if (ttlDays <= 0) return;

wmmhello's avatar
wmmhello 已提交
550
  ttlKey->dtime = ctime / 1000 + ttlDays * tsTtlUnit;
551 552 553 554 555 556
  ttlKey->uid = pME->uid;
}

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

H
Hongze Cheng 已提交
561
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type) {
H
Hongze Cheng 已提交
562
  void      *pData = NULL;
H
Hongze Cheng 已提交
563 564 565 566 567 568
  int        nData = 0;
  int        rc = 0;
  SMetaEntry e = {0};
  SDecoder   dc = {0};

  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
569 570 571
  if (rc < 0) {
    return -1;
  }
H
Hongze Cheng 已提交
572
  int64_t version = ((SUidIdxVal *)pData)[0].version;
H
Hongze Cheng 已提交
573 574 575 576 577 578 579 580

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

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

  if (type) *type = e.type;

581 582 583 584 585
  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 已提交
586
      version = ((SUidIdxVal *)tData)[0].version;
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
      STbDbKey tbDbKey = {.uid = e.ctbEntry.suid, .version = version};
      if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &tData, &tLen) == 0) {
        SDecoder   tdc = {0};
        SMetaEntry stbEntry = {0};

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

H
Hongze Cheng 已提交
604 605 606
  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);
607

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

H
Hongze Cheng 已提交
610 611
  if (e.type == TSDB_CHILD_TABLE) {
    tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), &pMeta->txn);
612 613

    --pMeta->pVnode->config.vndStats.numOfCTables;
H
Hongze Cheng 已提交
614 615
  } else if (e.type == TSDB_NORMAL_TABLE) {
    // drop schema.db (todo)
616 617

    --pMeta->pVnode->config.vndStats.numOfNTables;
618
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
H
Hongze Cheng 已提交
619
  } else if (e.type == TSDB_SUPER_TABLE) {
C
Cary Xu 已提交
620
    tdbTbDelete(pMeta->pSuidIdx, &e.uid, sizeof(tb_uid_t), &pMeta->txn);
H
Hongze Cheng 已提交
621
    // drop schema.db (todo)
622

623
    metaStatsCacheDrop(pMeta, uid);
624
    --pMeta->pVnode->config.vndStats.numOfSTables;
H
Hongze Cheng 已提交
625 626
  }

H
Hongze Cheng 已提交
627 628
  metaCacheDrop(pMeta, uid);

H
Hongze Cheng 已提交
629 630
  tDecoderClear(&dc);
  tdbFree(pData);
H
Hongze Cheng 已提交
631

H
refact  
Hongze Cheng 已提交
632 633
  return 0;
}
H
Hongze Cheng 已提交
634

D
dapan1121 已提交
635
static int metaAlterTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq, STableMetaRsp *pMetaRsp) {
H
Hongze Cheng 已提交
636
  void           *pVal = NULL;
H
Hongze Cheng 已提交
637
  int             nVal = 0;
H
Hongze Cheng 已提交
638
  const void     *pData = NULL;
H
Hongze Cheng 已提交
639 640 641 642
  int             nData = 0;
  int             ret = 0;
  tb_uid_t        uid;
  int64_t         oversion;
H
Hongze Cheng 已提交
643
  SSchema        *pColumn = NULL;
H
Hongze Cheng 已提交
644 645 646 647 648
  SMetaEntry      entry = {0};
  SSchemaWrapper *pSchema;
  int             c;

  // search name index
H
Hongze Cheng 已提交
649
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
H
Hongze Cheng 已提交
650 651 652 653 654 655 656 657 658 659
  if (ret < 0) {
    terrno = TSDB_CODE_VND_TABLE_NOT_EXIST;
    return -1;
  }

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

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

H
Hongze Cheng 已提交
662 663
  tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, &pMeta->txn);
  tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c);
H
Hongze Cheng 已提交
664 665
  ASSERT(c == 0);

H
Hongze Cheng 已提交
666
  tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
667
  oversion = ((SUidIdxVal *)pData)[0].version;
H
Hongze Cheng 已提交
668 669

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

H
Hongze Cheng 已提交
672 673
  tdbTbcOpen(pMeta->pTbDb, &pTbDbc, &pMeta->txn);
  tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c);
H
Hongze Cheng 已提交
674
  ASSERT(c == 0);
H
Hongze Cheng 已提交
675
  tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
676 677 678

  // get table entry
  SDecoder dc = {0};
H
Hongze Cheng 已提交
679 680 681
  entry.pBuf = taosMemoryMalloc(nData);
  memcpy(entry.pBuf, pData, nData);
  tDecoderInit(&dc, entry.pBuf, nData);
H
Hongze Cheng 已提交
682 683
  ret = metaDecodeEntry(&dc, &entry);
  ASSERT(ret == 0);
H
Hongze Cheng 已提交
684 685 686 687 688 689 690

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

  // search the column to add/drop/update
691
  pSchema = &entry.ntbEntry.schemaRow;
H
Hongze Cheng 已提交
692 693 694 695 696 697 698
  int32_t iCol = 0;
  for (;;) {
    pColumn = NULL;

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

H
Hongze Cheng 已提交
699
    ASSERT(pAlterTbReq->colName);
H
Hongze Cheng 已提交
700 701 702 703 704
    if (strcmp(pColumn->name, pAlterTbReq->colName) == 0) break;
    iCol++;
  }

  entry.version = version;
H
Hongze Cheng 已提交
705 706
  int      tlen;
  SSchema *pNewSchema = NULL;
H
Hongze Cheng 已提交
707 708 709 710 711 712
  switch (pAlterTbReq->action) {
    case TSDB_ALTER_TABLE_ADD_COLUMN:
      if (pColumn) {
        terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
        goto _err;
      }
713
      pSchema->version++;
H
Hongze Cheng 已提交
714
      pSchema->nCols++;
H
Hongze Cheng 已提交
715 716 717
      pNewSchema = taosMemoryMalloc(sizeof(SSchema) * pSchema->nCols);
      memcpy(pNewSchema, pSchema->pSchema, sizeof(SSchema) * (pSchema->nCols - 1));
      pSchema->pSchema = pNewSchema;
718 719 720 721 722
      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);
723 724

      ++pMeta->pVnode->config.vndStats.numOfNTimeSeries;
H
Hongze Cheng 已提交
725 726 727 728 729 730 731 732 733 734
      break;
    case TSDB_ALTER_TABLE_DROP_COLUMN:
      if (pColumn == NULL) {
        terrno = TSDB_CODE_VND_TABLE_COL_NOT_EXISTS;
        goto _err;
      }
      if (pColumn->colId == 0) {
        terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
        goto _err;
      }
L
Liu Jicong 已提交
735 736 737 738
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
        goto _err;
      }
739
      pSchema->version++;
H
Hongze Cheng 已提交
740 741 742 743
      tlen = (pSchema->nCols - iCol - 1) * sizeof(SSchema);
      if (tlen) {
        memmove(pColumn, pColumn + 1, tlen);
      }
H
Hongze Cheng 已提交
744
      pSchema->nCols--;
745 746

      --pMeta->pVnode->config.vndStats.numOfNTimeSeries;
H
Hongze Cheng 已提交
747 748 749 750 751 752
      break;
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
      if (pColumn == NULL) {
        terrno = TSDB_CODE_VND_TABLE_COL_NOT_EXISTS;
        goto _err;
      }
H
Hongze Cheng 已提交
753
      if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes > pAlterTbReq->colModBytes) {
H
Hongze Cheng 已提交
754 755 756
        terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
        goto _err;
      }
L
Liu Jicong 已提交
757 758 759 760
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
        goto _err;
      }
761
      pSchema->version++;
H
Hongze Cheng 已提交
762
      pColumn->bytes = pAlterTbReq->colModBytes;
H
Hongze Cheng 已提交
763 764 765 766 767 768
      break;
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
      if (pColumn == NULL) {
        terrno = TSDB_CODE_VND_TABLE_COL_NOT_EXISTS;
        goto _err;
      }
L
Liu Jicong 已提交
769 770 771 772
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
        goto _err;
      }
773
      pSchema->version++;
H
Hongze Cheng 已提交
774 775 776 777 778 779
      strcpy(pColumn->name, pAlterTbReq->colNewName);
      break;
  }

  entry.version = version;

H
Hongze Cheng 已提交
780 781 782 783 784 785
  // do actual write
  metaWLock(pMeta);

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

H
Hongze Cheng 已提交
786
  metaUpdateUidIdx(pMeta, &entry);
H
Hongze Cheng 已提交
787 788 789 790 791

  metaSaveToSkmDb(pMeta, &entry);

  metaULock(pMeta);

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

wmmhello's avatar
wmmhello 已提交
794
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
H
Hongze Cheng 已提交
795
  if (pNewSchema) taosMemoryFree(pNewSchema);
H
Hongze Cheng 已提交
796 797
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
798 799
  tDecoderClear(&dc);

H
Hongze Cheng 已提交
800
  return 0;
H
Hongze Cheng 已提交
801 802

_err:
wmmhello's avatar
wmmhello 已提交
803
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
H
Hongze Cheng 已提交
804 805
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
806 807
  tDecoderClear(&dc);

H
Hongze Cheng 已提交
808
  return -1;
H
Hongze Cheng 已提交
809 810 811
}

static int metaUpdateTableTagVal(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
H
Hongze Cheng 已提交
812 813
  SMetaEntry  ctbEntry = {0};
  SMetaEntry  stbEntry = {0};
H
Hongze Cheng 已提交
814
  void       *pVal = NULL;
H
Hongze Cheng 已提交
815 816 817 818 819 820 821 822
  int         nVal = 0;
  int         ret;
  int         c;
  tb_uid_t    uid;
  int64_t     oversion;
  const void *pData = NULL;
  int         nData = 0;

H
Hongze Cheng 已提交
823 824 825 826 827
  if (pAlterTbReq->tagName == NULL) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }

H
Hongze Cheng 已提交
828
  // search name index
H
Hongze Cheng 已提交
829
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
H
Hongze Cheng 已提交
830 831 832 833 834 835 836 837 838 839
  if (ret < 0) {
    terrno = TSDB_CODE_VND_TABLE_NOT_EXIST;
    return -1;
  }

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

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

H
Hongze Cheng 已提交
842 843
  tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, &pMeta->txn);
  tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c);
H
Hongze Cheng 已提交
844 845
  ASSERT(c == 0);

H
Hongze Cheng 已提交
846
  tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
847
  oversion = ((SUidIdxVal *)pData)[0].version;
H
Hongze Cheng 已提交
848 849

  // search table.db
H
Hongze Cheng 已提交
850
  TBC     *pTbDbc = NULL;
H
Hongze Cheng 已提交
851 852
  SDecoder dc1 = {0};
  SDecoder dc2 = {0};
H
Hongze Cheng 已提交
853

H
Hongze Cheng 已提交
854
  /* get ctbEntry */
H
Hongze Cheng 已提交
855 856
  tdbTbcOpen(pMeta->pTbDb, &pTbDbc, &pMeta->txn);
  tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c);
H
Hongze Cheng 已提交
857
  ASSERT(c == 0);
H
Hongze Cheng 已提交
858
  tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
859

H
Hongze Cheng 已提交
860 861
  ctbEntry.pBuf = taosMemoryMalloc(nData);
  memcpy(ctbEntry.pBuf, pData, nData);
H
Hongze Cheng 已提交
862 863
  tDecoderInit(&dc1, ctbEntry.pBuf, nData);
  metaDecodeEntry(&dc1, &ctbEntry);
H
Hongze Cheng 已提交
864

H
Hongze Cheng 已提交
865
  /* get stbEntry*/
H
Hongze Cheng 已提交
866
  tdbTbGet(pMeta->pUidIdx, &ctbEntry.ctbEntry.suid, sizeof(tb_uid_t), &pVal, &nVal);
H
Hongze Cheng 已提交
867 868
  tdbTbGet(pMeta->pTbDb, &((STbDbKey){.uid = ctbEntry.ctbEntry.suid, .version = ((SUidIdxVal *)pVal)[0].version}),
           sizeof(STbDbKey), (void **)&stbEntry.pBuf, &nVal);
H
Hongze Cheng 已提交
869
  tdbFree(pVal);
H
Hongze Cheng 已提交
870 871
  tDecoderInit(&dc2, stbEntry.pBuf, nVal);
  metaDecodeEntry(&dc2, &stbEntry);
H
Hongze Cheng 已提交
872 873

  SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
H
Hongze Cheng 已提交
874
  SSchema        *pColumn = NULL;
H
Hongze Cheng 已提交
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
  int32_t         iCol = 0;
  for (;;) {
    pColumn = NULL;

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

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

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

H
Hongze Cheng 已提交
891
  ctbEntry.version = version;
H
Hongze Cheng 已提交
892
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
wmmhello's avatar
wmmhello 已提交
893
    ctbEntry.ctbEntry.pTags = taosMemoryMalloc(pAlterTbReq->nTagVal);
H
Hongze Cheng 已提交
894
    if (ctbEntry.ctbEntry.pTags == NULL) {
wmmhello's avatar
wmmhello 已提交
895 896 897
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
898 899
    memcpy((void *)ctbEntry.ctbEntry.pTags, pAlterTbReq->pTagVal, pAlterTbReq->nTagVal);
  } else {
C
Cary Xu 已提交
900
    const STag *pOldTag = (const STag *)ctbEntry.ctbEntry.pTags;
H
Hongze Cheng 已提交
901 902
    STag       *pNewTag = NULL;
    SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
C
Cary Xu 已提交
903
    if (!pTagArray) {
C
Cary Xu 已提交
904 905 906
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
wmmhello's avatar
wmmhello 已提交
907 908 909
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
      SSchema *pCol = &pTagSchema->pSchema[i];
      if (iCol == i) {
910 911 912
        if (pAlterTbReq->isNull) {
          continue;
        }
wmmhello's avatar
wmmhello 已提交
913 914 915 916 917 918
        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;
919
        } else {
wmmhello's avatar
wmmhello 已提交
920 921 922
          memcpy(&val.i64, pAlterTbReq->pTagVal, pAlterTbReq->nTagVal);
        }
        taosArrayPush(pTagArray, &val);
wmmhello's avatar
wmmhello 已提交
923
      } else {
wmmhello's avatar
wmmhello 已提交
924
        STagVal val = {.cid = pCol->colId};
wmmhello's avatar
wmmhello 已提交
925 926
        if (tTagGet(pOldTag, &val)) {
          taosArrayPush(pTagArray, &val);
H
Hongze Cheng 已提交
927 928 929
        }
      }
    }
C
Cary Xu 已提交
930 931
    if ((terrno = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag)) < 0) {
      taosArrayDestroy(pTagArray);
C
Cary Xu 已提交
932 933 934
      goto _err;
    }
    ctbEntry.ctbEntry.pTags = (uint8_t *)pNewTag;
C
Cary Xu 已提交
935
    taosArrayDestroy(pTagArray);
wmmhello's avatar
wmmhello 已提交
936
  }
H
Hongze Cheng 已提交
937

938 939
  metaWLock(pMeta);

H
Hongze Cheng 已提交
940 941 942 943
  // save to table.db
  metaSaveToTbDb(pMeta, &ctbEntry);

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

dengyihao's avatar
dengyihao 已提交
946 947 948 949
  if (iCol == 0) {
    metaUpdateTagIdx(pMeta, &ctbEntry);
  }

M
Minglei Jin 已提交
950
  ASSERT(ctbEntry.ctbEntry.pTags);
951
  SCtbIdxKey ctbIdxKey = {.suid = ctbEntry.ctbEntry.suid, .uid = uid};
H
Hongze Cheng 已提交
952 953
  tdbTbUpsert(pMeta->pCtbIdx, &ctbIdxKey, sizeof(ctbIdxKey), ctbEntry.ctbEntry.pTags,
              ((STag *)(ctbEntry.ctbEntry.pTags))->len, &pMeta->txn);
954

955 956
  metaULock(pMeta);

H
Hongze Cheng 已提交
957 958
  tDecoderClear(&dc1);
  tDecoderClear(&dc2);
M
Minglei Jin 已提交
959
  taosMemoryFree((void *)ctbEntry.ctbEntry.pTags);
H
Hongze Cheng 已提交
960 961
  if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
  if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
H
Hongze Cheng 已提交
962 963
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
H
Hongze Cheng 已提交
964
  return 0;
H
Hongze Cheng 已提交
965 966

_err:
H
Hongze Cheng 已提交
967 968
  tDecoderClear(&dc1);
  tDecoderClear(&dc2);
H
Hongze Cheng 已提交
969 970
  if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
  if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
H
Hongze Cheng 已提交
971 972
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
H
Hongze Cheng 已提交
973
  return -1;
H
Hongze Cheng 已提交
974 975 976
}

static int metaUpdateTableOptions(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
H
Hongze Cheng 已提交
977 978 979 980 981 982 983 984 985
  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 已提交
986 987 988 989 990 991

  // search name index
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
  if (ret < 0) {
    terrno = TSDB_CODE_VND_TABLE_NOT_EXIST;
    return -1;
992
  }
wmmhello's avatar
wmmhello 已提交
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005

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

  // search uid index
  TBC *pUidIdxc = NULL;

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

  tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
1006
  oversion = ((SUidIdxVal *)pData)[0].version;
wmmhello's avatar
wmmhello 已提交
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027

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

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

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

  entry.version = version;
  metaWLock(pMeta);
  // build SMetaEntry
  if (entry.type == TSDB_CHILD_TABLE) {
H
Hongze Cheng 已提交
1028
    if (pAlterTbReq->updateTTL) {
wmmhello's avatar
wmmhello 已提交
1029 1030 1031 1032
      metaDeleteTtlIdx(pMeta, &entry);
      entry.ctbEntry.ttlDays = pAlterTbReq->newTTL;
      metaUpdateTtlIdx(pMeta, &entry);
    }
H
Hongze Cheng 已提交
1033
    if (pAlterTbReq->newCommentLen >= 0) {
wmmhello's avatar
wmmhello 已提交
1034 1035 1036
      entry.ctbEntry.commentLen = pAlterTbReq->newCommentLen;
      entry.ctbEntry.comment = pAlterTbReq->newComment;
    }
wmmhello's avatar
wmmhello 已提交
1037
  } else {
H
Hongze Cheng 已提交
1038
    if (pAlterTbReq->updateTTL) {
wmmhello's avatar
wmmhello 已提交
1039 1040 1041 1042
      metaDeleteTtlIdx(pMeta, &entry);
      entry.ntbEntry.ttlDays = pAlterTbReq->newTTL;
      metaUpdateTtlIdx(pMeta, &entry);
    }
H
Hongze Cheng 已提交
1043
    if (pAlterTbReq->newCommentLen >= 0) {
wmmhello's avatar
wmmhello 已提交
1044 1045 1046
      entry.ntbEntry.commentLen = pAlterTbReq->newCommentLen;
      entry.ntbEntry.comment = pAlterTbReq->newComment;
    }
1047
  }
wmmhello's avatar
wmmhello 已提交
1048 1049 1050

  // save to table db
  metaSaveToTbDb(pMeta, &entry);
H
Hongze Cheng 已提交
1051
  metaUpdateUidIdx(pMeta, &entry);
wmmhello's avatar
wmmhello 已提交
1052 1053 1054 1055
  metaULock(pMeta);

  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
1056
  tDecoderClear(&dc);
wmmhello's avatar
wmmhello 已提交
1057
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
H
Hongze Cheng 已提交
1058 1059 1060
  return 0;
}

D
dapan1121 已提交
1061
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
H
Hongze Cheng 已提交
1062 1063 1064 1065 1066
  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 已提交
1067
      return metaAlterTableColumn(pMeta, version, pReq, pMetaRsp);
H
Hongze Cheng 已提交
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
      return metaUpdateTableTagVal(pMeta, version, pReq);
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
      return metaUpdateTableOptions(pMeta, version, pReq);
    default:
      terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
      return -1;
      break;
  }
}

H
Hongze Cheng 已提交
1079
static int metaSaveToTbDb(SMeta *pMeta, const SMetaEntry *pME) {
H
Hongze Cheng 已提交
1080
  STbDbKey tbDbKey;
H
Hongze Cheng 已提交
1081 1082
  void    *pKey = NULL;
  void    *pVal = NULL;
H
Hongze Cheng 已提交
1083 1084
  int      kLen = 0;
  int      vLen = 0;
H
Hongze Cheng 已提交
1085
  SEncoder coder = {0};
H
Hongze Cheng 已提交
1086 1087

  // set key and value
H
Hongze Cheng 已提交
1088
  tbDbKey.version = pME->version;
H
Hongze Cheng 已提交
1089 1090
  tbDbKey.uid = pME->uid;

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

H
Hongze Cheng 已提交
1094 1095
  pKey = &tbDbKey;
  kLen = sizeof(tbDbKey);
H
Hongze Cheng 已提交
1096

wafwerar's avatar
wafwerar 已提交
1097 1098 1099
  int32_t ret = 0;
  tEncodeSize(metaEncodeEntry, pME, vLen, ret);
  if (ret < 0) {
H
Hongze Cheng 已提交
1100 1101 1102 1103 1104 1105 1106 1107 1108
    goto _err;
  }

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

H
Hongze Cheng 已提交
1109
  tEncoderInit(&coder, pVal, vLen);
H
Hongze Cheng 已提交
1110 1111 1112 1113 1114

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

H
Hongze Cheng 已提交
1115
  tEncoderClear(&coder);
H
Hongze Cheng 已提交
1116 1117

  // write to table.db
H
Hongze Cheng 已提交
1118
  if (tdbTbInsert(pMeta->pTbDb, pKey, kLen, pVal, vLen, &pMeta->txn) < 0) {
H
Hongze Cheng 已提交
1119 1120 1121 1122 1123 1124 1125
    goto _err;
  }

  taosMemoryFree(pVal);
  return 0;

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

H
Hongze Cheng 已提交
1129 1130 1131 1132
  taosMemoryFree(pVal);
  return -1;
}

H
Hongze Cheng 已提交
1133
static int metaUpdateUidIdx(SMeta *pMeta, const SMetaEntry *pME) {
H
Hongze Cheng 已提交
1134 1135 1136 1137 1138 1139 1140 1141
  // upsert cache
  SMetaInfo info;
  metaGetEntryInfo(pME, &info);
  metaCacheUpsert(pMeta, &info);

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

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

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

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

H
Hongze Cheng 已提交
1152
static int metaUpdateTtlIdx(SMeta *pMeta, const SMetaEntry *pME) {
1153 1154
  STtlIdxKey ttlKey = {0};
  metaBuildTtlIdxKey(&ttlKey, pME);
H
Hongze Cheng 已提交
1155
  if (ttlKey.dtime == 0) return 0;
H
Hongze Cheng 已提交
1156
  return tdbTbInsert(pMeta->pTtlIdx, &ttlKey, sizeof(ttlKey), NULL, 0, &pMeta->txn);
H
Hongze Cheng 已提交
1157 1158
}

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

H
Hongze Cheng 已提交
1162 1163
  return tdbTbInsert(pMeta->pCtbIdx, &ctbIdxKey, sizeof(ctbIdxKey), pME->ctbEntry.pTags,
                     ((STag *)(pME->ctbEntry.pTags))->len, &pMeta->txn);
H
Hongze Cheng 已提交
1164 1165
}

wmmhello's avatar
wmmhello 已提交
1166
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
1167
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
dengyihao's avatar
dengyihao 已提交
1168 1169 1170 1171 1172
  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 已提交
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183

  *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 已提交
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193

  // refactor
  if (IS_VAR_DATA_TYPE(type)) {
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
    memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
    *(tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData) = uid;
  } else {
    memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
    *(tb_uid_t *)((*ppTagIdxKey)->data + nTagData) = uid;
  }
H
Hongze Cheng 已提交
1194 1195 1196 1197 1198 1199 1200 1201 1202

  return 0;
}

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

static int metaUpdateTagIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry) {
H
Hongze Cheng 已提交
1203
  void          *pData = NULL;
H
Hongze Cheng 已提交
1204 1205 1206
  int            nData = 0;
  STbDbKey       tbDbKey = {0};
  SMetaEntry     stbEntry = {0};
H
Hongze Cheng 已提交
1207
  STagIdxKey    *pTagIdxKey = NULL;
H
Hongze Cheng 已提交
1208
  int32_t        nTagIdxKey;
M
Minglei Jin 已提交
1209 1210
  const SSchema *pTagColumn;
  const void    *pTagData = NULL;
C
Cary Xu 已提交
1211
  int32_t        nTagData = 0;
H
Hongze Cheng 已提交
1212
  SDecoder       dc = {0};
1213
  int32_t        ret = 0;
H
Hongze Cheng 已提交
1214
  // get super table
H
Hongze Cheng 已提交
1215
  if (tdbTbGet(pMeta->pUidIdx, &pCtbEntry->ctbEntry.suid, sizeof(tb_uid_t), &pData, &nData) != 0) {
1216 1217
    ret = -1;
    goto end;
wmmhello's avatar
wmmhello 已提交
1218
  }
H
Hongze Cheng 已提交
1219
  tbDbKey.uid = pCtbEntry->ctbEntry.suid;
H
Hongze Cheng 已提交
1220
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
H
Hongze Cheng 已提交
1221
  tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData);
H
Hongze Cheng 已提交
1222 1223 1224 1225 1226

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

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

  STagVal tagVal = {.cid = pTagColumn->colId};
1229
  if (pTagColumn->type != TSDB_DATA_TYPE_JSON) {
wmmhello's avatar
wmmhello 已提交
1230
    tTagGet((const STag *)pCtbEntry->ctbEntry.pTags, &tagVal);
1231
    if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
wmmhello's avatar
wmmhello 已提交
1232 1233
      pTagData = tagVal.pData;
      nTagData = (int32_t)tagVal.nData;
1234
    } else {
wmmhello's avatar
wmmhello 已提交
1235 1236 1237
      pTagData = &(tagVal.i64);
      nTagData = tDataTypes[pTagColumn->type].bytes;
    }
1238 1239 1240
  } else {
    // pTagData = pCtbEntry->ctbEntry.pTags;
    // nTagData = ((const STag *)pCtbEntry->ctbEntry.pTags)->len;
dengyihao's avatar
dengyihao 已提交
1241 1242
    pTagData = pCtbEntry->ctbEntry.pTags;
    nTagData = ((const STag *)pCtbEntry->ctbEntry.pTags)->len;
1243 1244
    ret = metaSaveJsonVarToIdx(pMeta, pCtbEntry, pTagColumn);
    goto end;
wmmhello's avatar
wmmhello 已提交
1245
  }
1246 1247
  if (metaCreateTagIdxKey(pCtbEntry->ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type,
                          pCtbEntry->uid, &pTagIdxKey, &nTagIdxKey) < 0) {
1248 1249
    ret = -1;
    goto end;
H
Hongze Cheng 已提交
1250
  }
dengyihao's avatar
dengyihao 已提交
1251
  tdbTbUpsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, &pMeta->txn);
1252
end:
H
Hongze Cheng 已提交
1253 1254 1255
  metaDestroyTagIdxKey(pTagIdxKey);
  tDecoderClear(&dc);
  tdbFree(pData);
1256
  return ret;
H
Hongze Cheng 已提交
1257 1258
}

H
Hongze Cheng 已提交
1259
static int metaSaveToSkmDb(SMeta *pMeta, const SMetaEntry *pME) {
H
Hongze Cheng 已提交
1260
  SEncoder              coder = {0};
H
Hongze Cheng 已提交
1261
  void                 *pVal = NULL;
H
Hongze Cheng 已提交
1262 1263 1264 1265 1266 1267
  int                   vLen = 0;
  int                   rcode = 0;
  SSkmDbKey             skmDbKey = {0};
  const SSchemaWrapper *pSW;

  if (pME->type == TSDB_SUPER_TABLE) {
1268
    pSW = &pME->stbEntry.schemaRow;
H
Hongze Cheng 已提交
1269
  } else if (pME->type == TSDB_NORMAL_TABLE) {
1270
    pSW = &pME->ntbEntry.schemaRow;
H
Hongze Cheng 已提交
1271 1272
  } else {
    ASSERT(0);
H
Hongze Cheng 已提交
1273 1274
  }

H
Hongze Cheng 已提交
1275
  skmDbKey.uid = pME->uid;
1276
  skmDbKey.sver = pSW->version;
H
Hongze Cheng 已提交
1277

1278 1279 1280 1281 1282
  // 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 已提交
1283
  // encode schema
wafwerar's avatar
wafwerar 已提交
1284 1285 1286
  int32_t ret = 0;
  tEncodeSize(tEncodeSSchemaWrapper, pSW, vLen, ret);
  if (ret < 0) return -1;
H
Hongze Cheng 已提交
1287
  pVal = taosMemoryMalloc(vLen);
H
Hongze Cheng 已提交
1288 1289 1290 1291 1292 1293
  if (pVal == NULL) {
    rcode = -1;
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

H
Hongze Cheng 已提交
1294
  tEncoderInit(&coder, pVal, vLen);
H
Hongze Cheng 已提交
1295 1296
  tEncodeSSchemaWrapper(&coder, pSW);

H
Hongze Cheng 已提交
1297
  if (tdbTbInsert(pMeta->pSkmDb, &skmDbKey, sizeof(skmDbKey), pVal, vLen, &pMeta->txn) < 0) {
H
Hongze Cheng 已提交
1298 1299 1300 1301
    rcode = -1;
    goto _exit;
  }

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

H
Hongze Cheng 已提交
1305
_exit:
H
Hongze Cheng 已提交
1306
  taosMemoryFree(pVal);
H
Hongze Cheng 已提交
1307
  tEncoderClear(&coder);
H
Hongze Cheng 已提交
1308 1309 1310
  return rcode;
}

H
Hongze Cheng 已提交
1311
int metaHandleEntry(SMeta *pMeta, const SMetaEntry *pME) {
H
Hongze Cheng 已提交
1312 1313
  metaWLock(pMeta);

H
Hongze Cheng 已提交
1314
  // save to table.db
H
Hongze Cheng 已提交
1315
  if (metaSaveToTbDb(pMeta, pME) < 0) goto _err;
H
Hongze Cheng 已提交
1316 1317

  // update uid.idx
H
Hongze Cheng 已提交
1318
  if (metaUpdateUidIdx(pMeta, pME) < 0) goto _err;
H
Hongze Cheng 已提交
1319 1320

  // update name.idx
H
Hongze Cheng 已提交
1321
  if (metaUpdateNameIdx(pMeta, pME) < 0) goto _err;
H
Hongze Cheng 已提交
1322 1323 1324

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

    // update tag.idx
H
Hongze Cheng 已提交
1328
    if (metaUpdateTagIdx(pMeta, pME) < 0) goto _err;
H
Hongze Cheng 已提交
1329 1330
  } else {
    // update schema.db
H
Hongze Cheng 已提交
1331
    if (metaSaveToSkmDb(pMeta, pME) < 0) goto _err;
C
Cary Xu 已提交
1332 1333 1334

    if (pME->type == TSDB_SUPER_TABLE) {
      if (metaUpdateSuidIdx(pMeta, pME) < 0) goto _err;
H
Hongze Cheng 已提交
1335
    }
H
Hongze Cheng 已提交
1336 1337 1338
  }

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

H
Hongze Cheng 已提交
1342
  metaULock(pMeta);
H
Hongze Cheng 已提交
1343
  return 0;
H
Hongze Cheng 已提交
1344 1345 1346 1347

_err:
  metaULock(pMeta);
  return -1;
1348
}
dengyihao's avatar
dengyihao 已提交
1349
// refactor later
dengyihao's avatar
dengyihao 已提交
1350 1351
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }