metaTable.c 44.7 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
    ASSERT(0);
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
  ASSERT(ret == 0 && c == 0);
H
Hongze Cheng 已提交
346

H
Hongze Cheng 已提交
347
  ret = tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);
348
  ASSERT(ret == 0);
H
Hongze Cheng 已提交
349

H
Hongze Cheng 已提交
350 351 352
  oStbEntry.pBuf = taosMemoryMalloc(nData);
  memcpy(oStbEntry.pBuf, pData, nData);
  tDecoderInit(&dc, oStbEntry.pBuf, nData);
H
Hongze Cheng 已提交
353 354 355 356 357 358
  metaDecodeEntry(&dc, &oStbEntry);

  nStbEntry.version = version;
  nStbEntry.type = TSDB_SUPER_TABLE;
  nStbEntry.uid = pReq->suid;
  nStbEntry.name = pReq->name;
359
  nStbEntry.stbEntry.schemaRow = pReq->schemaRow;
H
Hongze Cheng 已提交
360 361 362 363
  nStbEntry.stbEntry.schemaTag = pReq->schemaTag;

  metaWLock(pMeta);
  // compare two entry
364 365
  if (oStbEntry.stbEntry.schemaRow.version != pReq->schemaRow.version) {
    metaSaveToSkmDb(pMeta, &nStbEntry);
H
Hongze Cheng 已提交
366 367 368 369 370 371
  }

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

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

374
  // metaStatsCacheDrop(pMeta, nStbEntry.uid);
375

H
Hongze Cheng 已提交
376
  metaULock(pMeta);
377 378

  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
H
Hongze Cheng 已提交
379
  tDecoderClear(&dc);
H
Hongze Cheng 已提交
380 381
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
H
Hongze Cheng 已提交
382 383 384
  return 0;
}

385
int metaCreateTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **pMetaRsp) {
H
Hongze Cheng 已提交
386 387
  SMetaEntry  me = {0};
  SMetaReader mr = {0};
H
Hongze Cheng 已提交
388 389 390 391 392

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

395
  if (pReq->type == TSDB_CHILD_TABLE) {
396
    tb_uid_t suid = metaGetTableEntryUidByName(pMeta, pReq->ctb.stbName);
397 398 399 400 401 402
    if (suid != pReq->ctb.suid) {
      terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
      return -1;
    }
  }

H
Hongze Cheng 已提交
403
  // validate req
H
Hongze Cheng 已提交
404
  metaReaderInit(&mr, pMeta, 0);
H
Hongze Cheng 已提交
405
  if (metaGetTableEntryByName(&mr, pReq->name) == 0) {
406 407 408 409 410
    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 已提交
411 412 413 414
    pReq->uid = mr.me.uid;
    if (pReq->type == TSDB_CHILD_TABLE) {
      pReq->ctb.suid = mr.me.ctbEntry.suid;
    }
H
Hongze Cheng 已提交
415 416 417
    terrno = TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
    metaReaderClear(&mr);
    return -1;
C
Cary Xu 已提交
418 419
  } else if (terrno == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
    terrno = TSDB_CODE_SUCCESS;
H
Hongze Cheng 已提交
420
  }
H
Hongze Cheng 已提交
421
  metaReaderClear(&mr);
H
Hongze Cheng 已提交
422 423

  // build SMetaEntry
H
Hongze Cheng 已提交
424
  me.version = version;
H
Hongze Cheng 已提交
425 426 427 428 429 430
  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 已提交
431
    me.ctbEntry.commentLen = pReq->commentLen;
wmmhello's avatar
wmmhello 已提交
432
    me.ctbEntry.comment = pReq->comment;
H
Hongze Cheng 已提交
433 434
    me.ctbEntry.suid = pReq->ctb.suid;
    me.ctbEntry.pTags = pReq->ctb.pTag;
435

wmmhello's avatar
wmmhello 已提交
436
#ifdef TAG_FILTER_DEBUG
dengyihao's avatar
dengyihao 已提交
437 438
    SArray *pTagVals = NULL;
    int32_t code = tTagToValArray((STag *)pReq->ctb.pTag, &pTagVals);
wmmhello's avatar
wmmhello 已提交
439
    for (int i = 0; i < taosArrayGetSize(pTagVals); i++) {
dengyihao's avatar
dengyihao 已提交
440
      STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
wmmhello's avatar
wmmhello 已提交
441 442

      if (IS_VAR_DATA_TYPE(pTagVal->type)) {
dengyihao's avatar
dengyihao 已提交
443
        char *buf = taosMemoryCalloc(pTagVal->nData + 1, 1);
wmmhello's avatar
wmmhello 已提交
444
        memcpy(buf, pTagVal->pData, pTagVal->nData);
dengyihao's avatar
dengyihao 已提交
445 446
        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 已提交
447 448 449 450
        taosMemoryFree(buf);
      } else {
        double val = 0;
        GET_TYPED_DATA(val, double, pTagVal->type, &pTagVal->i64);
dengyihao's avatar
dengyihao 已提交
451 452
        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 已提交
453 454
      }
    }
wmmhello's avatar
wmmhello 已提交
455
#endif
wmmhello's avatar
wmmhello 已提交
456

457
    ++pMeta->pVnode->config.vndStats.numOfCTables;
458 459 460

    metaWLock(pMeta);
    metaUpdateStbStats(pMeta, me.ctbEntry.suid, 1);
461
    metaUidCacheClear(pMeta, me.ctbEntry.suid);
462
    metaULock(pMeta);
H
Hongze Cheng 已提交
463 464 465
  } else {
    me.ntbEntry.ctime = pReq->ctime;
    me.ntbEntry.ttlDays = pReq->ttl;
wmmhello's avatar
wmmhello 已提交
466
    me.ntbEntry.commentLen = pReq->commentLen;
wmmhello's avatar
wmmhello 已提交
467
    me.ntbEntry.comment = pReq->comment;
468 469
    me.ntbEntry.schemaRow = pReq->ntb.schemaRow;
    me.ntbEntry.ncid = me.ntbEntry.schemaRow.pSchema[me.ntbEntry.schemaRow.nCols - 1].colId + 1;
470 471

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

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

477 478 479 480 481 482 483 484 485 486
  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 {
487
        metaUpdateMetaRsp(pReq->uid, pReq->name, &pReq->ntb.schemaRow, *pMetaRsp);
488 489 490 491
      }
    }
  }

S
Shengliang Guan 已提交
492
  metaDebug("vgId:%d, table:%s uid %" PRId64 " is created, type:%" PRId8, TD_VID(pMeta->pVnode), pReq->name, pReq->uid,
H
Hongze Cheng 已提交
493
            pReq->type);
H
refact  
Hongze Cheng 已提交
494
  return 0;
H
Hongze Cheng 已提交
495 496

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

502
int metaDropTable(SMeta *pMeta, int64_t version, SVDropTbReq *pReq, SArray *tbUids, tb_uid_t *tbUid) {
H
Hongze Cheng 已提交
503
  void    *pData = NULL;
H
Hongze Cheng 已提交
504 505 506 507
  int      nData = 0;
  int      rc = 0;
  tb_uid_t uid;
  int      type;
H
more  
Hongze Cheng 已提交
508

H
Hongze Cheng 已提交
509 510
  rc = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &pData, &nData);
  if (rc < 0) {
511
    terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
H
more  
Hongze Cheng 已提交
512 513
    return -1;
  }
H
Hongze Cheng 已提交
514 515
  uid = *(tb_uid_t *)pData;

H
Hongze Cheng 已提交
516 517 518
  metaWLock(pMeta);
  metaDropTableByUid(pMeta, uid, &type);
  metaULock(pMeta);
H
Hongze Cheng 已提交
519

520
  if ((type == TSDB_CHILD_TABLE || type == TSDB_NORMAL_TABLE) && tbUids) {
H
Hongze Cheng 已提交
521
    taosArrayPush(tbUids, &uid);
H
Hongze Cheng 已提交
522
  }
H
Hongze Cheng 已提交
523

524 525 526 527
  if ((type == TSDB_CHILD_TABLE) && tbUid) {
    *tbUid = uid;
  }

H
Hongze Cheng 已提交
528 529 530
  tdbFree(pData);
  return 0;
}
H
Hongze Cheng 已提交
531

532 533
int metaTtlDropTable(SMeta *pMeta, int64_t ttl, SArray *tbUids) {
  int ret = metaTtlSmaller(pMeta, ttl, tbUids);
H
Hongze Cheng 已提交
534
  if (ret != 0) {
535 536
    return ret;
  }
537
  if (taosArrayGetSize(tbUids) == 0) {
538 539 540 541
    return 0;
  }

  metaWLock(pMeta);
542 543 544
  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 已提交
545
    metaDebug("ttl drop table:%" PRId64, *uid);
546 547 548 549 550
  }
  metaULock(pMeta);
  return 0;
}

H
Hongze Cheng 已提交
551
static void metaBuildTtlIdxKey(STtlIdxKey *ttlKey, const SMetaEntry *pME) {
M
Minglei Jin 已提交
552 553
  int64_t ttlDays = 0;
  int64_t ctime = 0;
554 555 556 557 558 559 560
  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 {
561
    ASSERT(0);
562 563 564 565
  }

  if (ttlDays <= 0) return;

wmmhello's avatar
wmmhello 已提交
566
  ttlKey->dtime = ctime / 1000 + ttlDays * tsTtlUnit;
567 568
  ttlKey->uid = pME->uid;
}
dengyihao's avatar
dengyihao 已提交
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
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 已提交
585 586 587 588 589 590
  if (pME->type == TSDB_NORMAL_TABLE) {
    ncolKey->ncol = pME->ntbEntry.schemaRow.nCols;
    ncolKey->uid = pME->uid;
  } else {
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
591 592
  return 0;
}
593 594 595 596

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

H
Hongze Cheng 已提交
601
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type) {
H
Hongze Cheng 已提交
602
  void      *pData = NULL;
H
Hongze Cheng 已提交
603 604 605 606 607 608
  int        nData = 0;
  int        rc = 0;
  SMetaEntry e = {0};
  SDecoder   dc = {0};

  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
609 610 611
  if (rc < 0) {
    return -1;
  }
H
Hongze Cheng 已提交
612
  int64_t version = ((SUidIdxVal *)pData)[0].version;
H
Hongze Cheng 已提交
613 614 615 616

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

  tDecoderInit(&dc, pData, nData);
M
Minglei Jin 已提交
617 618 619 620 621
  rc = metaDecodeEntry(&dc, &e);
  if (rc < 0) {
    tDecoderClear(&dc);
    return -1;
  }
H
Hongze Cheng 已提交
622 623 624

  if (type) *type = e.type;

625 626 627 628 629
  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 已提交
630
      version = ((SUidIdxVal *)tData)[0].version;
631 632 633 634 635 636 637 638 639 640
      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);
dengyihao's avatar
dengyihao 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
        } else {
          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) {
660
            tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
dengyihao's avatar
dengyihao 已提交
661 662
          }
          metaDestroyTagIdxKey(pTagIdxKey);
663 664 665 666 667 668 669
        }
        tDecoderClear(&tdc);
      }
      tdbFree(tData);
    }
  }

670 671 672
  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);
673

dengyihao's avatar
dengyihao 已提交
674 675 676
  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 已提交
677
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtlIdx(pMeta, &e);
C
Cary Xu 已提交
678

H
Hongze Cheng 已提交
679
  if (e.type == TSDB_CHILD_TABLE) {
680
    tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
681 682

    --pMeta->pVnode->config.vndStats.numOfCTables;
683 684

    metaUpdateStbStats(pMeta, e.ctbEntry.suid, -1);
685
    metaUidCacheClear(pMeta, e.ctbEntry.suid);
H
Hongze Cheng 已提交
686 687
  } else if (e.type == TSDB_NORMAL_TABLE) {
    // drop schema.db (todo)
688 689

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

695
    metaStatsCacheDrop(pMeta, uid);
696
    metaUidCacheClear(pMeta, uid);
697
    --pMeta->pVnode->config.vndStats.numOfSTables;
H
Hongze Cheng 已提交
698 699
  }

H
Hongze Cheng 已提交
700 701
  metaCacheDrop(pMeta, uid);

H
Hongze Cheng 已提交
702 703
  tDecoderClear(&dc);
  tdbFree(pData);
H
Hongze Cheng 已提交
704

H
refact  
Hongze Cheng 已提交
705 706
  return 0;
}
dengyihao's avatar
dengyihao 已提交
707 708 709 710 711 712
// opt ins_tables
int metaUpdateCtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
  SCtimeIdxKey ctimeKey = {0};
  if (metaBuildCtimeIdxKey(&ctimeKey, pME) < 0) {
    return 0;
  }
713 714 715
  metaDebug("vgId:%d, start to save ctime:%" PRId64 " uid:%" PRId64 " ct:%" PRId64, TD_VID(pMeta->pVnode), pME->version,
            pME->uid, ctimeKey.ctime);

716
  return tdbTbInsert(pMeta->pCtimeIdx, &ctimeKey, sizeof(ctimeKey), NULL, 0, pMeta->txn);
dengyihao's avatar
dengyihao 已提交
717 718 719 720 721 722 723
}

int metaDeleteCtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
  SCtimeIdxKey ctimeKey = {0};
  if (metaBuildCtimeIdxKey(&ctimeKey, pME) < 0) {
    return 0;
  }
724
  return tdbTbDelete(pMeta->pCtimeIdx, &ctimeKey, sizeof(ctimeKey), pMeta->txn);
dengyihao's avatar
dengyihao 已提交
725 726 727 728 729 730
}
int metaUpdateNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
  SNcolIdxKey ncolKey = {0};
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
    return 0;
  }
731
  return tdbTbInsert(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), NULL, 0, pMeta->txn);
dengyihao's avatar
dengyihao 已提交
732 733 734 735 736 737 738
}

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

D
dapan1121 已提交
742
static int metaAlterTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq, STableMetaRsp *pMetaRsp) {
H
Hongze Cheng 已提交
743
  void           *pVal = NULL;
H
Hongze Cheng 已提交
744
  int             nVal = 0;
H
Hongze Cheng 已提交
745
  const void     *pData = NULL;
H
Hongze Cheng 已提交
746 747 748 749
  int             nData = 0;
  int             ret = 0;
  tb_uid_t        uid;
  int64_t         oversion;
H
Hongze Cheng 已提交
750
  SSchema        *pColumn = NULL;
H
Hongze Cheng 已提交
751 752 753 754
  SMetaEntry      entry = {0};
  SSchemaWrapper *pSchema;
  int             c;

H
Hongze Cheng 已提交
755
  if (pAlterTbReq->colName == NULL) {
H
Hongze Cheng 已提交
756 757 758 759
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }

H
Hongze Cheng 已提交
760
  // search name index
H
Hongze Cheng 已提交
761
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
H
Hongze Cheng 已提交
762
  if (ret < 0) {
763
    terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
H
Hongze Cheng 已提交
764 765 766 767 768 769 770 771
    return -1;
  }

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

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

774
  tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, NULL);
H
Hongze Cheng 已提交
775
  tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c);
776
  ASSERT(c == 0);
H
Hongze Cheng 已提交
777

H
Hongze Cheng 已提交
778
  tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
779
  oversion = ((SUidIdxVal *)pData)[0].version;
H
Hongze Cheng 已提交
780 781

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

784
  tdbTbcOpen(pMeta->pTbDb, &pTbDbc, NULL);
H
Hongze Cheng 已提交
785
  tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c);
786
  ASSERT(c == 0);
H
Hongze Cheng 已提交
787
  tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
788 789 790

  // get table entry
  SDecoder dc = {0};
H
Hongze Cheng 已提交
791 792 793
  entry.pBuf = taosMemoryMalloc(nData);
  memcpy(entry.pBuf, pData, nData);
  tDecoderInit(&dc, entry.pBuf, nData);
H
Hongze Cheng 已提交
794
  ret = metaDecodeEntry(&dc, &entry);
795
  ASSERT(ret == 0);
H
Hongze Cheng 已提交
796 797 798 799 800 801

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

dengyihao's avatar
dengyihao 已提交
804 805 806 807
  // save old entry
  SMetaEntry oldEntry = {.type = TSDB_NORMAL_TABLE, .uid = entry.uid};
  oldEntry.ntbEntry.schemaRow.nCols = pSchema->nCols;

H
Hongze Cheng 已提交
808 809 810 811 812 813 814
  int32_t iCol = 0;
  for (;;) {
    pColumn = NULL;

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

815
    ASSERT(pAlterTbReq->colName);
H
Hongze Cheng 已提交
816 817 818 819 820
    if (strcmp(pColumn->name, pAlterTbReq->colName) == 0) break;
    iCol++;
  }

  entry.version = version;
H
Hongze Cheng 已提交
821 822
  int      tlen;
  SSchema *pNewSchema = NULL;
H
Hongze Cheng 已提交
823 824 825 826 827 828
  switch (pAlterTbReq->action) {
    case TSDB_ALTER_TABLE_ADD_COLUMN:
      if (pColumn) {
        terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
        goto _err;
      }
829
      pSchema->version++;
H
Hongze Cheng 已提交
830
      pSchema->nCols++;
H
Hongze Cheng 已提交
831 832 833
      pNewSchema = taosMemoryMalloc(sizeof(SSchema) * pSchema->nCols);
      memcpy(pNewSchema, pSchema->pSchema, sizeof(SSchema) * (pSchema->nCols - 1));
      pSchema->pSchema = pNewSchema;
834 835 836 837 838
      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);
839 840

      ++pMeta->pVnode->config.vndStats.numOfNTimeSeries;
H
Hongze Cheng 已提交
841 842 843
      break;
    case TSDB_ALTER_TABLE_DROP_COLUMN:
      if (pColumn == NULL) {
844
        terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
H
Hongze Cheng 已提交
845 846 847 848 849 850
        goto _err;
      }
      if (pColumn->colId == 0) {
        terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
        goto _err;
      }
L
Liu Jicong 已提交
851 852 853 854
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
        goto _err;
      }
855
      pSchema->version++;
H
Hongze Cheng 已提交
856 857 858 859
      tlen = (pSchema->nCols - iCol - 1) * sizeof(SSchema);
      if (tlen) {
        memmove(pColumn, pColumn + 1, tlen);
      }
H
Hongze Cheng 已提交
860
      pSchema->nCols--;
861 862

      --pMeta->pVnode->config.vndStats.numOfNTimeSeries;
H
Hongze Cheng 已提交
863 864 865
      break;
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
      if (pColumn == NULL) {
866
        terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
H
Hongze Cheng 已提交
867 868
        goto _err;
      }
H
Hongze Cheng 已提交
869
      if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes > pAlterTbReq->colModBytes) {
H
Hongze Cheng 已提交
870 871 872
        terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
        goto _err;
      }
L
Liu Jicong 已提交
873 874 875 876
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
        goto _err;
      }
877
      pSchema->version++;
H
Hongze Cheng 已提交
878
      pColumn->bytes = pAlterTbReq->colModBytes;
H
Hongze Cheng 已提交
879 880
      break;
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
H
Hongze Cheng 已提交
881 882 883 884
      if (pAlterTbReq->colNewName == NULL) {
        terrno = TSDB_CODE_INVALID_MSG;
        goto _err;
      }
H
Hongze Cheng 已提交
885
      if (pColumn == NULL) {
886
        terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
H
Hongze Cheng 已提交
887 888
        goto _err;
      }
L
Liu Jicong 已提交
889 890 891 892
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
        goto _err;
      }
893
      pSchema->version++;
H
Hongze Cheng 已提交
894 895 896 897 898 899
      strcpy(pColumn->name, pAlterTbReq->colNewName);
      break;
  }

  entry.version = version;

dengyihao's avatar
dengyihao 已提交
900 901 902
  metaDeleteNcolIdx(pMeta, &oldEntry);
  metaUpdateNcolIdx(pMeta, &entry);

H
Hongze Cheng 已提交
903 904 905 906 907 908
  // do actual write
  metaWLock(pMeta);

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

H
Hongze Cheng 已提交
909
  metaUpdateUidIdx(pMeta, &entry);
H
Hongze Cheng 已提交
910 911 912 913 914

  metaSaveToSkmDb(pMeta, &entry);

  metaULock(pMeta);

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

wmmhello's avatar
wmmhello 已提交
917
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
H
Hongze Cheng 已提交
918
  if (pNewSchema) taosMemoryFree(pNewSchema);
H
Hongze Cheng 已提交
919 920
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
921 922
  tDecoderClear(&dc);

H
Hongze Cheng 已提交
923
  return 0;
H
Hongze Cheng 已提交
924 925

_err:
wmmhello's avatar
wmmhello 已提交
926
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
H
Hongze Cheng 已提交
927 928
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
929 930
  tDecoderClear(&dc);

H
Hongze Cheng 已提交
931
  return -1;
H
Hongze Cheng 已提交
932 933 934
}

static int metaUpdateTableTagVal(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
H
Hongze Cheng 已提交
935 936
  SMetaEntry  ctbEntry = {0};
  SMetaEntry  stbEntry = {0};
H
Hongze Cheng 已提交
937
  void       *pVal = NULL;
H
Hongze Cheng 已提交
938 939 940 941 942 943 944 945
  int         nVal = 0;
  int         ret;
  int         c;
  tb_uid_t    uid;
  int64_t     oversion;
  const void *pData = NULL;
  int         nData = 0;

H
Hongze Cheng 已提交
946 947 948 949 950
  if (pAlterTbReq->tagName == NULL) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }

H
Hongze Cheng 已提交
951
  // search name index
H
Hongze Cheng 已提交
952
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
H
Hongze Cheng 已提交
953
  if (ret < 0) {
954
    terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
H
Hongze Cheng 已提交
955 956 957 958 959 960 961 962
    return -1;
  }

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

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

965
  tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, NULL);
H
Hongze Cheng 已提交
966
  tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c);
967
  ASSERT(c == 0);
H
Hongze Cheng 已提交
968

H
Hongze Cheng 已提交
969
  tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
970
  oversion = ((SUidIdxVal *)pData)[0].version;
H
Hongze Cheng 已提交
971 972

  // search table.db
H
Hongze Cheng 已提交
973
  TBC     *pTbDbc = NULL;
H
Hongze Cheng 已提交
974 975
  SDecoder dc1 = {0};
  SDecoder dc2 = {0};
H
Hongze Cheng 已提交
976

H
Hongze Cheng 已提交
977
  /* get ctbEntry */
978
  tdbTbcOpen(pMeta->pTbDb, &pTbDbc, NULL);
H
Hongze Cheng 已提交
979
  tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c);
980
  ASSERT(c == 0);
H
Hongze Cheng 已提交
981
  tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
982

H
Hongze Cheng 已提交
983 984
  ctbEntry.pBuf = taosMemoryMalloc(nData);
  memcpy(ctbEntry.pBuf, pData, nData);
H
Hongze Cheng 已提交
985 986
  tDecoderInit(&dc1, ctbEntry.pBuf, nData);
  metaDecodeEntry(&dc1, &ctbEntry);
H
Hongze Cheng 已提交
987

H
Hongze Cheng 已提交
988
  /* get stbEntry*/
H
Hongze Cheng 已提交
989
  tdbTbGet(pMeta->pUidIdx, &ctbEntry.ctbEntry.suid, sizeof(tb_uid_t), &pVal, &nVal);
990 991 992 993 994
  if (!pVal) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto _err;
  }

H
Hongze Cheng 已提交
995 996
  tdbTbGet(pMeta->pTbDb, &((STbDbKey){.uid = ctbEntry.ctbEntry.suid, .version = ((SUidIdxVal *)pVal)[0].version}),
           sizeof(STbDbKey), (void **)&stbEntry.pBuf, &nVal);
H
Hongze Cheng 已提交
997
  tdbFree(pVal);
H
Hongze Cheng 已提交
998 999
  tDecoderInit(&dc2, stbEntry.pBuf, nVal);
  metaDecodeEntry(&dc2, &stbEntry);
H
Hongze Cheng 已提交
1000 1001

  SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
H
Hongze Cheng 已提交
1002
  SSchema        *pColumn = NULL;
H
Hongze Cheng 已提交
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
  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) {
1015
    terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
H
Hongze Cheng 已提交
1016 1017
    goto _err;
  }
H
Hongze Cheng 已提交
1018

H
Hongze Cheng 已提交
1019
  ctbEntry.version = version;
H
Hongze Cheng 已提交
1020
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
wmmhello's avatar
wmmhello 已提交
1021
    ctbEntry.ctbEntry.pTags = taosMemoryMalloc(pAlterTbReq->nTagVal);
H
Hongze Cheng 已提交
1022
    if (ctbEntry.ctbEntry.pTags == NULL) {
wmmhello's avatar
wmmhello 已提交
1023 1024 1025
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
1026 1027
    memcpy((void *)ctbEntry.ctbEntry.pTags, pAlterTbReq->pTagVal, pAlterTbReq->nTagVal);
  } else {
C
Cary Xu 已提交
1028
    const STag *pOldTag = (const STag *)ctbEntry.ctbEntry.pTags;
H
Hongze Cheng 已提交
1029 1030
    STag       *pNewTag = NULL;
    SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
C
Cary Xu 已提交
1031
    if (!pTagArray) {
C
Cary Xu 已提交
1032 1033 1034
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
wmmhello's avatar
wmmhello 已提交
1035 1036 1037
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
      SSchema *pCol = &pTagSchema->pSchema[i];
      if (iCol == i) {
1038 1039 1040
        if (pAlterTbReq->isNull) {
          continue;
        }
wmmhello's avatar
wmmhello 已提交
1041 1042 1043 1044 1045 1046
        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;
1047
        } else {
wmmhello's avatar
wmmhello 已提交
1048 1049 1050
          memcpy(&val.i64, pAlterTbReq->pTagVal, pAlterTbReq->nTagVal);
        }
        taosArrayPush(pTagArray, &val);
wmmhello's avatar
wmmhello 已提交
1051
      } else {
wmmhello's avatar
wmmhello 已提交
1052
        STagVal val = {.cid = pCol->colId};
wmmhello's avatar
wmmhello 已提交
1053 1054
        if (tTagGet(pOldTag, &val)) {
          taosArrayPush(pTagArray, &val);
H
Hongze Cheng 已提交
1055 1056 1057
        }
      }
    }
C
Cary Xu 已提交
1058 1059
    if ((terrno = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag)) < 0) {
      taosArrayDestroy(pTagArray);
C
Cary Xu 已提交
1060 1061 1062
      goto _err;
    }
    ctbEntry.ctbEntry.pTags = (uint8_t *)pNewTag;
C
Cary Xu 已提交
1063
    taosArrayDestroy(pTagArray);
wmmhello's avatar
wmmhello 已提交
1064
  }
H
Hongze Cheng 已提交
1065

1066 1067
  metaWLock(pMeta);

H
Hongze Cheng 已提交
1068 1069 1070 1071
  // save to table.db
  metaSaveToTbDb(pMeta, &ctbEntry);

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

dengyihao's avatar
dengyihao 已提交
1074 1075 1076 1077
  if (iCol == 0) {
    metaUpdateTagIdx(pMeta, &ctbEntry);
  }

1078
  ASSERT(ctbEntry.ctbEntry.pTags);
1079
  SCtbIdxKey ctbIdxKey = {.suid = ctbEntry.ctbEntry.suid, .uid = uid};
H
Hongze Cheng 已提交
1080
  tdbTbUpsert(pMeta->pCtbIdx, &ctbIdxKey, sizeof(ctbIdxKey), ctbEntry.ctbEntry.pTags,
1081
              ((STag *)(ctbEntry.ctbEntry.pTags))->len, pMeta->txn);
1082

1083 1084
  metaUidCacheClear(pMeta, ctbEntry.ctbEntry.suid);

1085 1086
  metaULock(pMeta);

H
Hongze Cheng 已提交
1087 1088
  tDecoderClear(&dc1);
  tDecoderClear(&dc2);
M
Minglei Jin 已提交
1089
  taosMemoryFree((void *)ctbEntry.ctbEntry.pTags);
H
Hongze Cheng 已提交
1090 1091
  if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
  if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
H
Hongze Cheng 已提交
1092 1093
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
H
Hongze Cheng 已提交
1094
  return 0;
H
Hongze Cheng 已提交
1095 1096

_err:
H
Hongze Cheng 已提交
1097 1098
  tDecoderClear(&dc1);
  tDecoderClear(&dc2);
H
Hongze Cheng 已提交
1099 1100
  if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
  if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
H
Hongze Cheng 已提交
1101 1102
  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
H
Hongze Cheng 已提交
1103
  return -1;
H
Hongze Cheng 已提交
1104 1105 1106
}

static int metaUpdateTableOptions(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
H
Hongze Cheng 已提交
1107 1108 1109 1110 1111 1112 1113 1114 1115
  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 已提交
1116 1117 1118 1119

  // search name index
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
  if (ret < 0) {
1120
    terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
wmmhello's avatar
wmmhello 已提交
1121
    return -1;
1122
  }
wmmhello's avatar
wmmhello 已提交
1123 1124 1125 1126 1127 1128 1129 1130

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

  // search uid index
  TBC *pUidIdxc = NULL;

1131
  tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, NULL);
wmmhello's avatar
wmmhello 已提交
1132
  tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c);
1133
  ASSERT(c == 0);
wmmhello's avatar
wmmhello 已提交
1134 1135

  tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
H
Hongze Cheng 已提交
1136
  oversion = ((SUidIdxVal *)pData)[0].version;
wmmhello's avatar
wmmhello 已提交
1137 1138 1139 1140

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

1141
  tdbTbcOpen(pMeta->pTbDb, &pTbDbc, NULL);
wmmhello's avatar
wmmhello 已提交
1142
  tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c);
1143
  ASSERT(c == 0);
wmmhello's avatar
wmmhello 已提交
1144 1145 1146 1147 1148 1149 1150 1151
  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);
1152
  ASSERT(ret == 0);
wmmhello's avatar
wmmhello 已提交
1153 1154 1155 1156 1157

  entry.version = version;
  metaWLock(pMeta);
  // build SMetaEntry
  if (entry.type == TSDB_CHILD_TABLE) {
H
Hongze Cheng 已提交
1158
    if (pAlterTbReq->updateTTL) {
wmmhello's avatar
wmmhello 已提交
1159 1160 1161 1162
      metaDeleteTtlIdx(pMeta, &entry);
      entry.ctbEntry.ttlDays = pAlterTbReq->newTTL;
      metaUpdateTtlIdx(pMeta, &entry);
    }
H
Hongze Cheng 已提交
1163
    if (pAlterTbReq->newCommentLen >= 0) {
wmmhello's avatar
wmmhello 已提交
1164 1165 1166
      entry.ctbEntry.commentLen = pAlterTbReq->newCommentLen;
      entry.ctbEntry.comment = pAlterTbReq->newComment;
    }
wmmhello's avatar
wmmhello 已提交
1167
  } else {
H
Hongze Cheng 已提交
1168
    if (pAlterTbReq->updateTTL) {
wmmhello's avatar
wmmhello 已提交
1169 1170 1171 1172
      metaDeleteTtlIdx(pMeta, &entry);
      entry.ntbEntry.ttlDays = pAlterTbReq->newTTL;
      metaUpdateTtlIdx(pMeta, &entry);
    }
H
Hongze Cheng 已提交
1173
    if (pAlterTbReq->newCommentLen >= 0) {
wmmhello's avatar
wmmhello 已提交
1174 1175 1176
      entry.ntbEntry.commentLen = pAlterTbReq->newCommentLen;
      entry.ntbEntry.comment = pAlterTbReq->newComment;
    }
1177
  }
wmmhello's avatar
wmmhello 已提交
1178 1179 1180

  // save to table db
  metaSaveToTbDb(pMeta, &entry);
H
Hongze Cheng 已提交
1181
  metaUpdateUidIdx(pMeta, &entry);
wmmhello's avatar
wmmhello 已提交
1182 1183 1184 1185
  metaULock(pMeta);

  tdbTbcClose(pTbDbc);
  tdbTbcClose(pUidIdxc);
1186
  tDecoderClear(&dc);
wmmhello's avatar
wmmhello 已提交
1187
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
H
Hongze Cheng 已提交
1188 1189 1190
  return 0;
}

D
dapan1121 已提交
1191
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
H
Hongze Cheng 已提交
1192 1193 1194 1195 1196
  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 已提交
1197
      return metaAlterTableColumn(pMeta, version, pReq, pMetaRsp);
H
Hongze Cheng 已提交
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
    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 已提交
1209
static int metaSaveToTbDb(SMeta *pMeta, const SMetaEntry *pME) {
H
Hongze Cheng 已提交
1210
  STbDbKey tbDbKey;
H
Hongze Cheng 已提交
1211 1212
  void    *pKey = NULL;
  void    *pVal = NULL;
H
Hongze Cheng 已提交
1213 1214
  int      kLen = 0;
  int      vLen = 0;
H
Hongze Cheng 已提交
1215
  SEncoder coder = {0};
H
Hongze Cheng 已提交
1216 1217

  // set key and value
H
Hongze Cheng 已提交
1218
  tbDbKey.version = pME->version;
H
Hongze Cheng 已提交
1219 1220
  tbDbKey.uid = pME->uid;

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

H
Hongze Cheng 已提交
1224 1225
  pKey = &tbDbKey;
  kLen = sizeof(tbDbKey);
H
Hongze Cheng 已提交
1226

wafwerar's avatar
wafwerar 已提交
1227 1228 1229
  int32_t ret = 0;
  tEncodeSize(metaEncodeEntry, pME, vLen, ret);
  if (ret < 0) {
H
Hongze Cheng 已提交
1230 1231 1232 1233 1234 1235 1236 1237 1238
    goto _err;
  }

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

H
Hongze Cheng 已提交
1239
  tEncoderInit(&coder, pVal, vLen);
H
Hongze Cheng 已提交
1240 1241 1242 1243 1244

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

H
Hongze Cheng 已提交
1245
  tEncoderClear(&coder);
H
Hongze Cheng 已提交
1246 1247

  // write to table.db
1248
  if (tdbTbInsert(pMeta->pTbDb, pKey, kLen, pVal, vLen, pMeta->txn) < 0) {
H
Hongze Cheng 已提交
1249 1250 1251 1252 1253 1254 1255
    goto _err;
  }

  taosMemoryFree(pVal);
  return 0;

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

H
Hongze Cheng 已提交
1259 1260 1261 1262
  taosMemoryFree(pVal);
  return -1;
}

H
Hongze Cheng 已提交
1263
static int metaUpdateUidIdx(SMeta *pMeta, const SMetaEntry *pME) {
H
Hongze Cheng 已提交
1264 1265 1266 1267 1268 1269 1270
  // upsert cache
  SMetaInfo info;
  metaGetEntryInfo(pME, &info);
  metaCacheUpsert(pMeta, &info);

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

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

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

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

H
Hongze Cheng 已提交
1282
static int metaUpdateTtlIdx(SMeta *pMeta, const SMetaEntry *pME) {
1283 1284
  STtlIdxKey ttlKey = {0};
  metaBuildTtlIdxKey(&ttlKey, pME);
H
Hongze Cheng 已提交
1285
  if (ttlKey.dtime == 0) return 0;
1286
  return tdbTbInsert(pMeta->pTtlIdx, &ttlKey, sizeof(ttlKey), NULL, 0, pMeta->txn);
H
Hongze Cheng 已提交
1287 1288
}

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

H
Hongze Cheng 已提交
1292
  return tdbTbInsert(pMeta->pCtbIdx, &ctbIdxKey, sizeof(ctbIdxKey), pME->ctbEntry.pTags,
1293
                     ((STag *)(pME->ctbEntry.pTags))->len, pMeta->txn);
H
Hongze Cheng 已提交
1294 1295
}

wmmhello's avatar
wmmhello 已提交
1296
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
1297
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
dengyihao's avatar
dengyihao 已提交
1298 1299 1300 1301 1302
  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 已提交
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313

  *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 已提交
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323

  // 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 已提交
1324 1325 1326 1327 1328 1329 1330 1331 1332

  return 0;
}

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

static int metaUpdateTagIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry) {
H
Hongze Cheng 已提交
1333
  void          *pData = NULL;
H
Hongze Cheng 已提交
1334 1335 1336
  int            nData = 0;
  STbDbKey       tbDbKey = {0};
  SMetaEntry     stbEntry = {0};
H
Hongze Cheng 已提交
1337
  STagIdxKey    *pTagIdxKey = NULL;
H
Hongze Cheng 已提交
1338
  int32_t        nTagIdxKey;
M
Minglei Jin 已提交
1339 1340
  const SSchema *pTagColumn;
  const void    *pTagData = NULL;
C
Cary Xu 已提交
1341
  int32_t        nTagData = 0;
H
Hongze Cheng 已提交
1342
  SDecoder       dc = {0};
1343
  int32_t        ret = 0;
H
Hongze Cheng 已提交
1344
  // get super table
H
Hongze Cheng 已提交
1345
  if (tdbTbGet(pMeta->pUidIdx, &pCtbEntry->ctbEntry.suid, sizeof(tb_uid_t), &pData, &nData) != 0) {
1346 1347
    ret = -1;
    goto end;
wmmhello's avatar
wmmhello 已提交
1348
  }
H
Hongze Cheng 已提交
1349
  tbDbKey.uid = pCtbEntry->ctbEntry.suid;
H
Hongze Cheng 已提交
1350
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
H
Hongze Cheng 已提交
1351
  tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData);
H
Hongze Cheng 已提交
1352 1353

  tDecoderInit(&dc, pData, nData);
M
Minglei Jin 已提交
1354 1355 1356 1357
  ret = metaDecodeEntry(&dc, &stbEntry);
  if (ret < 0) {
    goto end;
  }
H
Hongze Cheng 已提交
1358

M
Minglei Jin 已提交
1359 1360 1361 1362
  if (stbEntry.stbEntry.schemaTag.pSchema == NULL) {
    goto end;
  }

H
Hongze Cheng 已提交
1363
  pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
C
Cary Xu 已提交
1364 1365

  STagVal tagVal = {.cid = pTagColumn->colId};
1366
  if (pTagColumn->type != TSDB_DATA_TYPE_JSON) {
wmmhello's avatar
wmmhello 已提交
1367
    tTagGet((const STag *)pCtbEntry->ctbEntry.pTags, &tagVal);
1368
    if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
wmmhello's avatar
wmmhello 已提交
1369 1370
      pTagData = tagVal.pData;
      nTagData = (int32_t)tagVal.nData;
1371
    } else {
wmmhello's avatar
wmmhello 已提交
1372 1373 1374
      pTagData = &(tagVal.i64);
      nTagData = tDataTypes[pTagColumn->type].bytes;
    }
1375 1376 1377
  } else {
    // pTagData = pCtbEntry->ctbEntry.pTags;
    // nTagData = ((const STag *)pCtbEntry->ctbEntry.pTags)->len;
dengyihao's avatar
dengyihao 已提交
1378 1379
    pTagData = pCtbEntry->ctbEntry.pTags;
    nTagData = ((const STag *)pCtbEntry->ctbEntry.pTags)->len;
1380 1381
    ret = metaSaveJsonVarToIdx(pMeta, pCtbEntry, pTagColumn);
    goto end;
wmmhello's avatar
wmmhello 已提交
1382
  }
dengyihao's avatar
dengyihao 已提交
1383 1384 1385 1386 1387 1388
  if (pTagData != NULL) {
    if (metaCreateTagIdxKey(pCtbEntry->ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type,
                            pCtbEntry->uid, &pTagIdxKey, &nTagIdxKey) < 0) {
      ret = -1;
      goto end;
    }
1389
    tdbTbUpsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn);
H
Hongze Cheng 已提交
1390
  }
1391
end:
H
Hongze Cheng 已提交
1392 1393 1394
  metaDestroyTagIdxKey(pTagIdxKey);
  tDecoderClear(&dc);
  tdbFree(pData);
1395
  return ret;
H
Hongze Cheng 已提交
1396 1397
}

H
Hongze Cheng 已提交
1398
static int metaSaveToSkmDb(SMeta *pMeta, const SMetaEntry *pME) {
H
Hongze Cheng 已提交
1399
  SEncoder              coder = {0};
H
Hongze Cheng 已提交
1400
  void                 *pVal = NULL;
H
Hongze Cheng 已提交
1401 1402 1403 1404 1405 1406
  int                   vLen = 0;
  int                   rcode = 0;
  SSkmDbKey             skmDbKey = {0};
  const SSchemaWrapper *pSW;

  if (pME->type == TSDB_SUPER_TABLE) {
1407
    pSW = &pME->stbEntry.schemaRow;
H
Hongze Cheng 已提交
1408
  } else if (pME->type == TSDB_NORMAL_TABLE) {
1409
    pSW = &pME->ntbEntry.schemaRow;
H
Hongze Cheng 已提交
1410
  } else {
1411
    ASSERT(0);
H
Hongze Cheng 已提交
1412 1413
  }

H
Hongze Cheng 已提交
1414
  skmDbKey.uid = pME->uid;
1415
  skmDbKey.sver = pSW->version;
H
Hongze Cheng 已提交
1416

1417 1418 1419 1420 1421
  // 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 已提交
1422
  // encode schema
wafwerar's avatar
wafwerar 已提交
1423 1424 1425
  int32_t ret = 0;
  tEncodeSize(tEncodeSSchemaWrapper, pSW, vLen, ret);
  if (ret < 0) return -1;
H
Hongze Cheng 已提交
1426
  pVal = taosMemoryMalloc(vLen);
H
Hongze Cheng 已提交
1427 1428 1429 1430 1431 1432
  if (pVal == NULL) {
    rcode = -1;
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

H
Hongze Cheng 已提交
1433
  tEncoderInit(&coder, pVal, vLen);
H
Hongze Cheng 已提交
1434 1435
  tEncodeSSchemaWrapper(&coder, pSW);

1436
  if (tdbTbInsert(pMeta->pSkmDb, &skmDbKey, sizeof(skmDbKey), pVal, vLen, pMeta->txn) < 0) {
H
Hongze Cheng 已提交
1437 1438 1439 1440
    rcode = -1;
    goto _exit;
  }

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

H
Hongze Cheng 已提交
1444
_exit:
H
Hongze Cheng 已提交
1445
  taosMemoryFree(pVal);
H
Hongze Cheng 已提交
1446
  tEncoderClear(&coder);
H
Hongze Cheng 已提交
1447 1448 1449
  return rcode;
}

H
Hongze Cheng 已提交
1450
int metaHandleEntry(SMeta *pMeta, const SMetaEntry *pME) {
H
Hongze Cheng 已提交
1451 1452
  metaWLock(pMeta);

H
Hongze Cheng 已提交
1453
  // save to table.db
H
Hongze Cheng 已提交
1454
  if (metaSaveToTbDb(pMeta, pME) < 0) goto _err;
H
Hongze Cheng 已提交
1455 1456

  // update uid.idx
H
Hongze Cheng 已提交
1457
  if (metaUpdateUidIdx(pMeta, pME) < 0) goto _err;
H
Hongze Cheng 已提交
1458 1459

  // update name.idx
H
Hongze Cheng 已提交
1460
  if (metaUpdateNameIdx(pMeta, pME) < 0) goto _err;
H
Hongze Cheng 已提交
1461 1462 1463

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

    // update tag.idx
H
Hongze Cheng 已提交
1467
    if (metaUpdateTagIdx(pMeta, pME) < 0) goto _err;
H
Hongze Cheng 已提交
1468 1469
  } else {
    // update schema.db
H
Hongze Cheng 已提交
1470
    if (metaSaveToSkmDb(pMeta, pME) < 0) goto _err;
C
Cary Xu 已提交
1471 1472 1473

    if (pME->type == TSDB_SUPER_TABLE) {
      if (metaUpdateSuidIdx(pMeta, pME) < 0) goto _err;
H
Hongze Cheng 已提交
1474
    }
H
Hongze Cheng 已提交
1475 1476
  }

dengyihao's avatar
dengyihao 已提交
1477 1478 1479 1480 1481 1482
  if (metaUpdateCtimeIdx(pMeta, pME) < 0) goto _err;

  if (pME->type == TSDB_NORMAL_TABLE) {
    if (metaUpdateNcolIdx(pMeta, pME) < 0) goto _err;
  }

H
Hongze Cheng 已提交
1483
  if (pME->type != TSDB_SUPER_TABLE) {
H
Hongze Cheng 已提交
1484
    if (metaUpdateTtlIdx(pMeta, pME) < 0) goto _err;
H
Hongze Cheng 已提交
1485 1486
  }

H
Hongze Cheng 已提交
1487
  metaULock(pMeta);
H
Hongze Cheng 已提交
1488
  return 0;
H
Hongze Cheng 已提交
1489 1490 1491 1492

_err:
  metaULock(pMeta);
  return -1;
1493
}
dengyihao's avatar
dengyihao 已提交
1494
// refactor later
dengyihao's avatar
dengyihao 已提交
1495 1496
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }