• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #3535

23 Nov 2024 02:07AM UTC coverage: 60.85% (+0.03%) from 60.825%
#3535

push

travis-ci

web-flow
Merge pull request #28893 from taosdata/doc/internal

refact: rename taos lib name

120252 of 252737 branches covered (47.58%)

Branch coverage included in aggregate %.

201187 of 275508 relevant lines covered (73.02%)

15886166.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

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

16
#include "meta.h"
17

18
extern SDmNotifyHandle dmNotifyHdl;
19

20
static int  metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema);
21
static int  metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema);
22
static int  metaSaveToTbDb(SMeta *pMeta, const SMetaEntry *pME);
23
static int  metaUpdateUidIdx(SMeta *pMeta, const SMetaEntry *pME);
24
static int  metaUpdateNameIdx(SMeta *pMeta, const SMetaEntry *pME);
25
static void metaUpdateTtl(SMeta *pMeta, const SMetaEntry *pME);
26
static int  metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs);
27
static int  metaSaveToSkmDb(SMeta *pMeta, const SMetaEntry *pME);
28
static int  metaUpdateCtbIdx(SMeta *pMeta, const SMetaEntry *pME);
29
static int  metaUpdateSuidIdx(SMeta *pMeta, const SMetaEntry *pME);
30
static int  metaUpdateTagIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry);
31
static int  metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl);
32
static void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey);
33
// opt ins_tables query
34
static int metaUpdateBtimeIdx(SMeta *pMeta, const SMetaEntry *pME);
35
static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME);
36
static int metaUpdateNcolIdx(SMeta *pMeta, const SMetaEntry *pME);
37
static int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME);
38

39
static int32_t updataTableColCmpr(SColCmprWrapper *pWp, SSchema *pSchema, int8_t add, uint32_t compress) {
650✔
40
  int32_t nCols = pWp->nCols;
650✔
41
  int32_t ver = pWp->version;
650✔
42
  if (add) {
650✔
43
    SColCmpr *p = taosMemoryCalloc(1, sizeof(SColCmpr) * (nCols + 1));
582✔
44
    if (p == NULL) {
582!
45
      return terrno;
×
46
    }
47

48
    memcpy(p, pWp->pColCmpr, sizeof(SColCmpr) * nCols);
582✔
49

50
    SColCmpr *pCol = p + nCols;
582✔
51
    pCol->id = pSchema->colId;
582✔
52
    pCol->alg = compress;
582✔
53
    pWp->nCols = nCols + 1;
582✔
54
    pWp->version = ver;
582✔
55
    pWp->pColCmpr = p;
582✔
56
  } else {
57
    for (int32_t i = 0; i < nCols; i++) {
20,815!
58
      SColCmpr *pOCmpr = &pWp->pColCmpr[i];
20,815✔
59
      if (pOCmpr->id == pSchema->colId) {
20,815✔
60
        int32_t left = (nCols - i - 1) * sizeof(SColCmpr);
68✔
61
        if (left) {
68✔
62
          memmove(pWp->pColCmpr + i, pWp->pColCmpr + i + 1, left);
35✔
63
        }
64
        nCols--;
68✔
65
        break;
68✔
66
      }
67
    }
68
    pWp->nCols = nCols;
68✔
69
    pWp->version = ver;
68✔
70
  }
71
  return 0;
650✔
72
}
73
static void metaGetEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) {
291,305✔
74
  pInfo->uid = pEntry->uid;
291,305✔
75
  pInfo->version = pEntry->version;
291,305✔
76
  if (pEntry->type == TSDB_SUPER_TABLE) {
291,305✔
77
    pInfo->suid = pEntry->uid;
38,491✔
78
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
38,491✔
79
  } else if (pEntry->type == TSDB_CHILD_TABLE) {
252,814✔
80
    pInfo->suid = pEntry->ctbEntry.suid;
236,668✔
81
    pInfo->skmVer = 0;
236,668✔
82
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
16,146!
83
    pInfo->suid = 0;
16,284✔
84
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
16,284✔
85
  } else {
86
    metaError("meta/table: invalide table type: %" PRId8 " get entry info failed.", pEntry->type);
×
87
  }
88
}
291,305✔
89

90
static int metaUpdateMetaRsp(tb_uid_t uid, char *tbName, SSchemaWrapper *pSchema, STableMetaRsp *pMetaRsp) {
16,215✔
91
  pMetaRsp->pSchemas = taosMemoryMalloc(pSchema->nCols * sizeof(SSchema));
16,215✔
92
  if (NULL == pMetaRsp->pSchemas) {
16,216!
93
    return terrno;
×
94
  }
95

96
  pMetaRsp->pSchemaExt = taosMemoryMalloc(pSchema->nCols * sizeof(SSchemaExt));
16,216✔
97
  if (pMetaRsp->pSchemaExt == NULL) {
16,216✔
98
    taosMemoryFree(pMetaRsp->pSchemas);
1✔
99
    return terrno;
×
100
  }
101

102
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
16,215✔
103
  pMetaRsp->numOfColumns = pSchema->nCols;
16,215✔
104
  pMetaRsp->tableType = TSDB_NORMAL_TABLE;
16,215✔
105
  pMetaRsp->sversion = pSchema->version;
16,215✔
106
  pMetaRsp->tuid = uid;
16,215✔
107

108
  memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
16,215✔
109

110
  return 0;
16,215✔
111
}
112

113
static int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
576✔
114
  int32_t code = 0;
576✔
115

116
#ifdef USE_INVERTED_INDEX
117
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
576!
118
    return TSDB_CODE_INVALID_PARA;
×
119
  }
120
  void       *data = pCtbEntry->ctbEntry.pTags;
576✔
121
  const char *tagName = pSchema->name;
576✔
122

123
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
576✔
124
  tb_uid_t    tuid = pCtbEntry->uid;
576✔
125
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
576✔
126
  int32_t     nTagData = 0;
576✔
127

128
  SArray *pTagVals = NULL;
576✔
129
  code = tTagToValArray((const STag *)data, &pTagVals);
576✔
130
  if (code) {
576!
131
    return code;
×
132
  }
133

134
  SIndexMultiTerm *terms = indexMultiTermCreate();
576✔
135
  if (terms == NULL) {
576!
136
    return terrno;
×
137
  }
138

139
  int16_t nCols = taosArrayGetSize(pTagVals);
576✔
140
  for (int i = 0; i < nCols; i++) {
1,513✔
141
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
937✔
142
    char     type = pTagVal->type;
937✔
143

144
    char   *key = pTagVal->pKey;
937✔
145
    int32_t nKey = strlen(key);
937✔
146

147
    SIndexTerm *term = NULL;
937✔
148
    if (type == TSDB_DATA_TYPE_NULL) {
937✔
149
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
24✔
150
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
913✔
151
      if (pTagVal->nData > 0) {
812✔
152
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
796✔
153
        if (val == NULL) {
796!
154
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
155
        }
156
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE);
796✔
157
        if (len < 0) {
796!
158
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
159
        }
160
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
796✔
161
        type = TSDB_DATA_TYPE_VARCHAR;
796✔
162
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
796✔
163
        taosMemoryFree(val);
796✔
164
      } else if (pTagVal->nData == 0) {
16!
165
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
16✔
166
      }
167
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
101✔
168
      double val = *(double *)(&pTagVal->i64);
77✔
169
      int    len = sizeof(val);
77✔
170
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
77✔
171
    } else if (type == TSDB_DATA_TYPE_BOOL) {
24!
172
      int val = *(int *)(&pTagVal->i64);
24✔
173
      int len = sizeof(val);
24✔
174
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
24✔
175
    }
176

177
    if (term != NULL) {
937!
178
      int32_t ret = indexMultiTermAdd(terms, term);
937✔
179
      if (ret < 0) {
937!
180
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
181
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
182
      }
183
    } else {
184
      code = terrno;
×
185
      goto _exception;
×
186
    }
187
  }
188
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
576✔
189
  indexMultiTermDestroy(terms);
576✔
190

191
  taosArrayDestroy(pTagVals);
576✔
192
#endif
193
  return code;
576✔
194
_exception:
×
195
  indexMultiTermDestroy(terms);
×
196
  taosArrayDestroy(pTagVals);
×
197
  return code;
×
198
}
199
int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
196✔
200
#ifdef USE_INVERTED_INDEX
201
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
196!
202
    return TSDB_CODE_INVALID_PARA;
×
203
  }
204
  void       *data = pCtbEntry->ctbEntry.pTags;
196✔
205
  const char *tagName = pSchema->name;
196✔
206

207
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
196✔
208
  tb_uid_t    tuid = pCtbEntry->uid;
196✔
209
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
196✔
210
  int32_t     nTagData = 0;
196✔
211

212
  SArray *pTagVals = NULL;
196✔
213
  int32_t code = tTagToValArray((const STag *)data, &pTagVals);
196✔
214
  if (code) {
196!
215
    return code;
×
216
  }
217

218
  SIndexMultiTerm *terms = indexMultiTermCreate();
196✔
219
  if (terms == NULL) {
196!
220
    return terrno;
×
221
  }
222

223
  int16_t nCols = taosArrayGetSize(pTagVals);
196✔
224
  for (int i = 0; i < nCols; i++) {
555✔
225
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
359✔
226
    char     type = pTagVal->type;
359✔
227

228
    char   *key = pTagVal->pKey;
359✔
229
    int32_t nKey = strlen(key);
359✔
230

231
    SIndexTerm *term = NULL;
359✔
232
    if (type == TSDB_DATA_TYPE_NULL) {
359!
233
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
×
234
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
359✔
235
      if (pTagVal->nData > 0) {
355!
236
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
355✔
237
        if (val == NULL) {
355!
238
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
239
        }
240
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE);
355✔
241
        if (len < 0) {
355!
242
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
243
        }
244
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
355✔
245
        type = TSDB_DATA_TYPE_VARCHAR;
355✔
246
        term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, val, len);
355✔
247
        taosMemoryFree(val);
355✔
248
      } else if (pTagVal->nData == 0) {
×
249
        term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
×
250
      }
251
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
4!
252
      double val = *(double *)(&pTagVal->i64);
4✔
253
      int    len = sizeof(val);
4✔
254
      term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, (const char *)&val, len);
4✔
255
    } else if (type == TSDB_DATA_TYPE_BOOL) {
×
256
      int val = *(int *)(&pTagVal->i64);
×
257
      int len = sizeof(val);
×
258
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
×
259
    }
260
    if (term != NULL) {
359!
261
      int32_t ret = indexMultiTermAdd(terms, term);
359✔
262
      if (ret < 0) {
359!
263
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
264
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
265
      }
266
    } else {
267
      code = terrno;
×
268
      goto _exception;
×
269
    }
270
  }
271
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
196✔
272
  indexMultiTermDestroy(terms);
196✔
273
  taosArrayDestroy(pTagVals);
196✔
274
#endif
275
  return code;
196✔
276
_exception:
×
277
  indexMultiTermDestroy(terms);
×
278
  taosArrayDestroy(pTagVals);
×
279
  return code;
×
280
}
281

282
static inline void metaTimeSeriesNotifyCheck(SMeta *pMeta) {
173,778✔
283
#if defined(TD_ENTERPRISE)
284
  int64_t nTimeSeries = metaGetTimeSeriesNum(pMeta, 0);
173,778✔
285
  int64_t deltaTS = nTimeSeries - pMeta->pVnode->config.vndStats.numOfReportedTimeSeries;
173,789✔
286
  if (deltaTS > tsTimeSeriesThreshold) {
173,789✔
287
    if (0 == atomic_val_compare_exchange_8(&dmNotifyHdl.state, 1, 2)) {
112,038✔
288
      if (tsem_post(&dmNotifyHdl.sem) != 0) {
112,031!
289
        metaError("vgId:%d, failed to post semaphore, errno:%d", TD_VID(pMeta->pVnode), errno);
×
290
      }
291
    }
292
  }
293
#endif
294
}
173,814✔
295

296
int metaCreateSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
30,086✔
297
  SMetaEntry  me = {0};
30,086✔
298
  int         kLen = 0;
30,086✔
299
  int         vLen = 0;
30,086✔
300
  const void *pKey = NULL;
30,086✔
301
  const void *pVal = NULL;
30,086✔
302
  void       *pBuf = NULL;
30,086✔
303
  int32_t     szBuf = 0;
30,086✔
304
  void       *p = NULL;
30,086✔
305
  int32_t     code = 0;
30,086✔
306

307
  // validate req
308
  void *pData = NULL;
30,086✔
309
  int   nData = 0;
30,086✔
310
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &pData, &nData) == 0) {
30,086✔
311
    tb_uid_t uid = *(tb_uid_t *)pData;
39✔
312
    tdbFree(pData);
39✔
313
    SMetaInfo info;
314
    if (metaGetInfo(pMeta, uid, &info, NULL) == TSDB_CODE_NOT_FOUND) {
39!
315
      return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
316
    }
317
    if (info.uid == info.suid) {
39✔
318
      return 0;
35✔
319
    } else {
320
      return terrno = TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
4✔
321
    }
322
  }
323

324
  // set structs
325
  me.version = version;
30,260✔
326
  me.type = TSDB_SUPER_TABLE;
30,260✔
327
  me.uid = pReq->suid;
30,260✔
328
  me.name = pReq->name;
30,260✔
329
  me.stbEntry.schemaRow = pReq->schemaRow;
30,260✔
330
  me.stbEntry.schemaTag = pReq->schemaTag;
30,260✔
331
  if (pReq->rollup) {
30,260✔
332
    TABLE_SET_ROLLUP(me.flags);
7✔
333
    me.stbEntry.rsmaParam = pReq->rsmaParam;
7✔
334
  }
335
  if (pReq->colCmpred) {
30,260✔
336
    TABLE_SET_COL_COMPRESSED(me.flags);
30,244✔
337
    me.colCmpr = pReq->colCmpr;
30,244✔
338
  }
339

340
  code = metaHandleEntry(pMeta, &me);
30,260✔
341
  if (code) goto _err;
30,260!
342

343
  ++pMeta->pVnode->config.vndStats.numOfSTables;
30,260✔
344

345
  pMeta->changed = true;
30,260✔
346
  metaDebug("vgId:%d, stb:%s is created, suid:%" PRId64, TD_VID(pMeta->pVnode), pReq->name, pReq->suid);
30,260✔
347

348
  return 0;
30,277✔
349

350
_err:
×
351
  metaError("vgId:%d, failed to create stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
352
            tstrerror(terrno));
353
  return code;
×
354
}
355

356
int metaDropSTable(SMeta *pMeta, int64_t verison, SVDropStbReq *pReq, SArray *tbUidList) {
2,342✔
357
  void   *pKey = NULL;
2,342✔
358
  int     nKey = 0;
2,342✔
359
  void   *pData = NULL;
2,342✔
360
  int     nData = 0;
2,342✔
361
  int     c = 0;
2,342✔
362
  int     rc = 0;
2,342✔
363
  int32_t lino;
364
  int32_t ret;
365

366
  // check if super table exists
367
  rc = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &pData, &nData);
2,342✔
368
  if (rc < 0 || *(tb_uid_t *)pData != pReq->suid) {
2,350✔
369
    tdbFree(pData);
30✔
370
    return terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
31✔
371
  }
372

373
  // drop all child tables
374
  TBC *pCtbIdxc = NULL;
2,320✔
375

376
  rc = tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL);
2,320✔
377
  if (rc) {
2,319!
378
    return (terrno = rc);
×
379
  }
380

381
  rc = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = pReq->suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c);
2,319✔
382
  if (rc < 0) {
2,321!
383
    tdbTbcClose(pCtbIdxc);
×
384
    metaWLock(pMeta);
×
385
    goto _drop_super_table;
×
386
  }
387

388
  for (;;) {
389
    rc = tdbTbcNext(pCtbIdxc, &pKey, &nKey, NULL, NULL);
5,748✔
390
    if (rc < 0) break;
5,741✔
391

392
    if (((SCtbIdxKey *)pKey)->suid < pReq->suid) {
4,802✔
393
      continue;
233✔
394
    } else if (((SCtbIdxKey *)pKey)->suid > pReq->suid) {
4,569✔
395
      break;
1,380✔
396
    }
397

398
    if (taosArrayPush(tbUidList, &(((SCtbIdxKey *)pKey)->uid)) == NULL) {
6,383!
399
      tdbFree(pKey);
×
400
      tdbTbcClose(pCtbIdxc);
×
401
      return terrno;
×
402
    }
403
  }
404

405
  tdbTbcClose(pCtbIdxc);
2,319✔
406

407
  ret = tsdbCacheDropSubTables(pMeta->pVnode->pTsdb, tbUidList, pReq->suid);
2,322✔
408
  if (ret < 0) {
2,322!
409
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
410
              tstrerror(terrno));
411
  }
412

413
  metaWLock(pMeta);
2,322✔
414

415
  for (int32_t iChild = 0; iChild < taosArrayGetSize(tbUidList); iChild++) {
5,520✔
416
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUidList, iChild);
3,199✔
417
    ret = metaDropTableByUid(pMeta, uid, NULL, NULL, NULL);
3,199✔
418
    if (ret < 0) {
3,199!
419
      metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
420
                pReq->suid, tstrerror(terrno));
421
    }
422
  }
423

424
  // drop super table
425
_drop_super_table:
2,320✔
426
  tdbTbGet(pMeta->pUidIdx, &pReq->suid, sizeof(tb_uid_t), &pData, &nData);
2,320✔
427
  ret = tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = ((SUidIdxVal *)pData)[0].version, .uid = pReq->suid},
2,322✔
428
                    sizeof(STbDbKey), pMeta->txn);
429
  if (ret < 0) {
2,322!
430
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
431
              tstrerror(terrno));
432
  }
433

434
  ret = tdbTbDelete(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, pMeta->txn);
2,322✔
435
  if (ret < 0) {
2,322!
436
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
437
              tstrerror(terrno));
438
  }
439

440
  ret = tdbTbDelete(pMeta->pUidIdx, &pReq->suid, sizeof(tb_uid_t), pMeta->txn);
2,322✔
441
  if (ret < 0) {
2,322!
442
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
443
              tstrerror(terrno));
444
  }
445

446
  ret = tdbTbDelete(pMeta->pSuidIdx, &pReq->suid, sizeof(tb_uid_t), pMeta->txn);
2,322✔
447
  if (ret < 0) {
2,322!
448
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
449
              tstrerror(terrno));
450
  }
451

452
  ret = metaStatsCacheDrop(pMeta, pReq->suid);
2,322✔
453
  if (ret < 0) {
2,322✔
454
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
335!
455
              tstrerror(terrno));
456
  }
457

458
  metaULock(pMeta);
2,322✔
459

460
  metaUpdTimeSeriesNum(pMeta);
2,322✔
461

462
  pMeta->changed = true;
2,322✔
463

464
_exit:
2,322✔
465
  tdbFree(pKey);
2,322✔
466
  tdbFree(pData);
2,322✔
467
  metaDebug("vgId:%d, super table %s uid:%" PRId64 " is dropped", TD_VID(pMeta->pVnode), pReq->name, pReq->suid);
2,322✔
468
  return 0;
2,322✔
469
}
470

471
static int32_t metaGetSubtables(SMeta *pMeta, int64_t suid, SArray *uids) {
95✔
472
  if (!uids) return TSDB_CODE_INVALID_PARA;
95!
473

474
  int   c = 0;
95✔
475
  void *pKey = NULL;
95✔
476
  int   nKey = 0;
95✔
477
  TBC  *pCtbIdxc = NULL;
95✔
478

479
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL));
95!
480
  int rc = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c);
96✔
481
  if (rc < 0) {
96!
482
    tdbTbcClose(pCtbIdxc);
×
483
    metaWLock(pMeta);
×
484
    return 0;
×
485
  }
486

487
  for (;;) {
488
    rc = tdbTbcNext(pCtbIdxc, &pKey, &nKey, NULL, NULL);
336✔
489
    if (rc < 0) break;
334✔
490

491
    if (((SCtbIdxKey *)pKey)->suid < suid) {
238!
492
      continue;
×
493
    } else if (((SCtbIdxKey *)pKey)->suid > suid) {
238!
494
      break;
×
495
    }
496

497
    if (taosArrayPush(uids, &(((SCtbIdxKey *)pKey)->uid)) == NULL) {
478!
498
      tdbFree(pKey);
×
499
      tdbTbcClose(pCtbIdxc);
×
500
      return terrno;
×
501
    }
502
  }
503

504
  tdbFree(pKey);
96✔
505

506
  tdbTbcClose(pCtbIdxc);
96✔
507
  return 0;
96✔
508
}
509

510
int metaAlterSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
4,854✔
511
  SMetaEntry  oStbEntry = {0};
4,854✔
512
  SMetaEntry  nStbEntry = {0};
4,854✔
513
  TBC        *pUidIdxc = NULL;
4,854✔
514
  TBC        *pTbDbc = NULL;
4,854✔
515
  const void *pData;
516
  int         nData;
517
  int64_t     oversion;
518
  SDecoder    dc = {0};
4,854✔
519
  int32_t     ret;
520
  int32_t     c = -2;
4,854✔
521

522
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, NULL));
4,854!
523
  ret = tdbTbcMoveTo(pUidIdxc, &pReq->suid, sizeof(tb_uid_t), &c);
4,863✔
524
  if (ret < 0 || c) {
4,873!
525
    tdbTbcClose(pUidIdxc);
×
526

527
    return terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
×
528
  }
529

530
  ret = tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
4,875✔
531
  if (ret < 0) {
4,874!
532
    tdbTbcClose(pUidIdxc);
×
533

534
    return terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
×
535
  }
536

537
  oversion = ((SUidIdxVal *)pData)[0].version;
4,874✔
538

539
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pTbDb, &pTbDbc, NULL));
4,874!
540
  ret = tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = pReq->suid, .version = oversion}), sizeof(STbDbKey), &c);
4,876✔
541
  if (!(ret == 0 && c == 0)) {
4,865!
542
    tdbTbcClose(pUidIdxc);
×
543
    tdbTbcClose(pTbDbc);
×
544

545
    metaError("meta/table: invalide ret: %" PRId32 " or c: %" PRId32 "alter stb failed.", ret, c);
×
546
    return terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
×
547
  }
548

549
  ret = tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);
4,865✔
550
  if (ret < 0) {
4,864!
551
    tdbTbcClose(pUidIdxc);
×
552
    tdbTbcClose(pTbDbc);
×
553

554
    return terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
×
555
  }
556

557
  if ((oStbEntry.pBuf = taosMemoryMalloc(nData)) == NULL) {
4,864!
558
    tdbTbcClose(pTbDbc);
×
559
    tdbTbcClose(pUidIdxc);
×
560
    return terrno;
×
561
  }
562
  memcpy(oStbEntry.pBuf, pData, nData);
4,875✔
563
  tDecoderInit(&dc, oStbEntry.pBuf, nData);
4,875✔
564
  ret = metaDecodeEntry(&dc, &oStbEntry);
4,863✔
565
  if (ret < 0) {
4,857!
566
    metaError("vgId:%d, failed to decode stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
567
              pReq->suid, tstrerror(ret));
568
    tDecoderClear(&dc);
×
569
    tdbTbcClose(pTbDbc);
×
570
    tdbTbcClose(pUidIdxc);
×
571
    return terrno;
×
572
  }
573

574
  nStbEntry.version = version;
4,857✔
575
  nStbEntry.type = TSDB_SUPER_TABLE;
4,857✔
576
  nStbEntry.uid = pReq->suid;
4,857✔
577
  nStbEntry.name = pReq->name;
4,857✔
578
  nStbEntry.stbEntry.schemaRow = pReq->schemaRow;
4,857✔
579
  nStbEntry.stbEntry.schemaTag = pReq->schemaTag;
4,857✔
580
  nStbEntry.colCmpr = pReq->colCmpr;
4,857✔
581
  TABLE_SET_COL_COMPRESSED(nStbEntry.flags);
4,857✔
582

583
  int     nCols = pReq->schemaRow.nCols;
4,857✔
584
  int     onCols = oStbEntry.stbEntry.schemaRow.nCols;
4,857✔
585
  int32_t deltaCol = nCols - onCols;
4,857✔
586
  bool    updStat = deltaCol != 0 && !metaTbInFilterCache(pMeta, pReq->name, 1);
4,857!
587

588
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
4,864✔
589
    STsdb  *pTsdb = pMeta->pVnode->pTsdb;
128✔
590
    SArray *uids = taosArrayInit(8, sizeof(int64_t));
128✔
591
    if (uids == NULL) {
127!
592
      if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
×
593
      tDecoderClear(&dc);
×
594
      tdbTbcClose(pTbDbc);
×
595
      tdbTbcClose(pUidIdxc);
×
596
      return terrno;
×
597
    }
598
    if (deltaCol == 1) {
127✔
599
      int16_t cid = pReq->schemaRow.pSchema[nCols - 1].colId;
63✔
600
      int8_t  col_type = pReq->schemaRow.pSchema[nCols - 1].type;
63✔
601

602
      TAOS_CHECK_RETURN(metaGetSubtables(pMeta, pReq->suid, uids));
63!
603
      TAOS_CHECK_RETURN(tsdbCacheNewSTableColumn(pTsdb, uids, cid, col_type));
64!
604
    } else if (deltaCol == -1) {
64!
605
      int16_t cid = -1;
64✔
606
      bool    hasPrimaryKey = false;
64✔
607
      if (onCols >= 2) {
64!
608
        hasPrimaryKey = (oStbEntry.stbEntry.schemaRow.pSchema[1].flags & COL_IS_KEY) ? true : false;
64✔
609
      }
610
      for (int i = 0, j = 0; i < nCols && j < onCols; ++i, ++j) {
416!
611
        if (pReq->schemaRow.pSchema[i].colId != oStbEntry.stbEntry.schemaRow.pSchema[j].colId) {
384✔
612
          cid = oStbEntry.stbEntry.schemaRow.pSchema[j].colId;
32✔
613
          break;
32✔
614
        }
615
      }
616

617
      if (cid != -1) {
64✔
618
        TAOS_CHECK_RETURN(metaGetSubtables(pMeta, pReq->suid, uids));
32!
619
        TAOS_CHECK_RETURN(tsdbCacheDropSTableColumn(pTsdb, uids, cid, hasPrimaryKey));
32!
620
      }
621
    }
622
    if (uids) taosArrayDestroy(uids);
128!
623

624
    tsdbCacheInvalidateSchema(pTsdb, pReq->suid, -1, pReq->schemaRow.version);
128✔
625
  }
626

627
  metaWLock(pMeta);
4,864✔
628
  // compare two entry
629
  if (oStbEntry.stbEntry.schemaRow.version != pReq->schemaRow.version) {
4,871✔
630
    ret = metaSaveToSkmDb(pMeta, &nStbEntry);
2,981✔
631
    if (ret < 0) {
2,979!
632
      metaError("vgId:%d, failed to save skm db:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
633
                pReq->suid, tstrerror(ret));
634
    }
635
  }
636

637
  // update table.db
638
  ret = metaSaveToTbDb(pMeta, &nStbEntry);
4,869✔
639
  if (ret < 0) {
4,872!
640
    metaError("vgId:%d, failed to save tb db:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
641
              pReq->suid, tstrerror(ret));
642
  }
643

644
  // update uid index
645
  ret = metaUpdateUidIdx(pMeta, &nStbEntry);
4,872✔
646
  if (ret < 0) {
4,874!
647
    metaError("vgId:%d, failed to update uid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
648
              pReq->suid, tstrerror(ret));
649
  }
650

651
  // metaStatsCacheDrop(pMeta, nStbEntry.uid);
652

653
  if (updStat) {
4,875✔
654
    metaUpdateStbStats(pMeta, pReq->suid, 0, deltaCol);
2,138✔
655
  }
656
  metaULock(pMeta);
4,874✔
657

658
  if (updStat) {
4,866✔
659
    int64_t ctbNum;
660
    ret = metaGetStbStats(pMeta->pVnode, pReq->suid, &ctbNum, NULL);
2,134✔
661
    if (ret < 0) {
2,138!
662
      metaError("vgId:%d, failed to get stb stats:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
663
                pReq->suid, tstrerror(ret));
664
    }
665
    pMeta->pVnode->config.vndStats.numOfTimeSeries += (ctbNum * deltaCol);
2,138✔
666
    metaTimeSeriesNotifyCheck(pMeta);
2,138✔
667
  }
668

669
  pMeta->changed = true;
4,868✔
670

671
_exit:
4,868✔
672
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
4,868!
673
  tDecoderClear(&dc);
4,871✔
674
  tdbTbcClose(pTbDbc);
4,876✔
675
  tdbTbcClose(pUidIdxc);
4,874✔
676
  return 0;
4,878✔
677
}
678
int metaAddIndexToSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
997✔
679
  SMetaEntry oStbEntry = {0};
997✔
680
  SMetaEntry nStbEntry = {0};
997✔
681
  STbDbKey   tbDbKey = {0};
997✔
682

683
  TBC     *pUidIdxc = NULL;
997✔
684
  TBC     *pTbDbc = NULL;
997✔
685
  void    *pData = NULL;
997✔
686
  int      nData = 0;
997✔
687
  int64_t  oversion;
688
  SDecoder dc = {0};
997✔
689
  int32_t  ret;
690
  int32_t  c = -2;
997✔
691
  tb_uid_t suid = pReq->suid;
997✔
692
  int32_t  code = 0;
997✔
693

694
  // get super table
695
  if ((code = tdbTbGet(pMeta->pUidIdx, &suid, sizeof(tb_uid_t), &pData, &nData)) != 0) {
997!
696
    goto _err;
×
697
  }
698

699
  tbDbKey.uid = suid;
997✔
700
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
997✔
701
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData)) != 0) {
997!
702
    goto _err;
×
703
  }
704

705
  tDecoderInit(&dc, pData, nData);
997✔
706
  code = metaDecodeEntry(&dc, &oStbEntry);
997✔
707
  if (code < 0) {
997!
708
    goto _err;
×
709
  }
710

711
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
997!
712
    code = TSDB_CODE_INVALID_PARA;
×
713
    goto _err;
×
714
  }
715

716
  if (oStbEntry.stbEntry.schemaTag.version == pReq->schemaTag.version) {
997!
717
    code = TSDB_CODE_INVALID_PARA;
×
718
    goto _err;
×
719
  }
720

721
  if (oStbEntry.stbEntry.schemaTag.nCols != pReq->schemaTag.nCols) {
997!
722
    code = TSDB_CODE_INVALID_PARA;
×
723
    goto _err;
×
724
  }
725

726
  int diffIdx = -1;
997✔
727
  for (int i = 0; i < pReq->schemaTag.nCols; i++) {
7,920!
728
    SSchema *pNew = pReq->schemaTag.pSchema + i;
7,920✔
729
    SSchema *pOld = oStbEntry.stbEntry.schemaTag.pSchema + i;
7,920✔
730
    if (pNew->type != pOld->type || pNew->colId != pOld->colId || pNew->bytes != pOld->bytes ||
7,920!
731
        strncmp(pOld->name, pNew->name, sizeof(pNew->name))) {
7,920!
732
      code = TSDB_CODE_INVALID_PARA;
×
733
      goto _err;
×
734
    }
735
    if (IS_IDX_ON(pNew) && !IS_IDX_ON(pOld)) {
7,920✔
736
      // if (diffIdx != -1) goto _err;
737
      diffIdx = i;
997✔
738
      break;
997✔
739
    }
740
  }
741

742
  if (diffIdx == -1) {
997!
743
    code = TSDB_CODE_INVALID_PARA;
×
744
    goto _err;
×
745
  }
746

747
  // Get target schema info
748
  SSchemaWrapper *pTagSchema = &pReq->schemaTag;
997✔
749
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
997!
750
    code = TSDB_CODE_INVALID_PARA;
×
751
    goto _err;
×
752
  }
753
  SSchema *pCol = pTagSchema->pSchema + diffIdx;
997✔
754

755
  /*
756
   * iterator all pTdDbc by uid and version
757
   */
758
  TBC *pCtbIdxc = NULL;
997✔
759
  code = tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL);
997✔
760
  if (code != 0) {
997!
761
    goto _err;
×
762
  }
763

764
  code = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c);
997✔
765
  if (code < 0) {
997!
766
    tdbTbcClose(pCtbIdxc);
×
767
    goto _err;
×
768
  }
769
  for (;;) {
19,315✔
770
    void *pKey = NULL, *pVal = NULL;
20,312✔
771
    int   nKey = 0, nVal = 0;
20,312✔
772
    code = tdbTbcNext(pCtbIdxc, &pKey, &nKey, &pVal, &nVal);
20,312✔
773
    if (code < 0) {
20,312✔
774
      tdbFree(pKey);
997✔
775
      tdbFree(pVal);
997✔
776
      tdbTbcClose(pCtbIdxc);
997✔
777
      pCtbIdxc = NULL;
997✔
778
      break;
997✔
779
    }
780
    if (((SCtbIdxKey *)pKey)->suid != suid) {
19,315✔
781
      tdbFree(pKey);
3,873✔
782
      tdbFree(pVal);
3,873✔
783
      continue;
3,873✔
784
    }
785
    STagIdxKey *pTagIdxKey = NULL;
15,442✔
786
    int32_t     nTagIdxKey;
787

788
    const void *pTagData = NULL;
15,442✔
789
    int32_t     nTagData = 0;
15,442✔
790

791
    SCtbIdxKey *table = (SCtbIdxKey *)pKey;
15,442✔
792
    STagVal     tagVal = {.cid = pCol->colId};
15,442✔
793
    if (tTagGet((const STag *)pVal, &tagVal)) {
15,442!
794
      if (IS_VAR_DATA_TYPE(pCol->type)) {
15,442!
795
        pTagData = tagVal.pData;
2,347✔
796
        nTagData = (int32_t)tagVal.nData;
2,347✔
797
      } else {
798
        pTagData = &(tagVal.i64);
13,095✔
799
        nTagData = tDataTypes[pCol->type].bytes;
13,095✔
800
      }
801
    } else {
802
      if (!IS_VAR_DATA_TYPE(pCol->type)) {
×
803
        nTagData = tDataTypes[pCol->type].bytes;
×
804
      }
805
    }
806
    code = metaCreateTagIdxKey(suid, pCol->colId, pTagData, nTagData, pCol->type, table->uid, &pTagIdxKey, &nTagIdxKey);
15,442✔
807
    tdbFree(pKey);
15,442✔
808
    tdbFree(pVal);
15,442✔
809
    if (code < 0) {
15,442!
810
      metaDestroyTagIdxKey(pTagIdxKey);
×
811
      tdbTbcClose(pCtbIdxc);
×
812
      goto _err;
×
813
    }
814

815
    metaWLock(pMeta);
15,442✔
816
    ret = tdbTbUpsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn);
15,442✔
817
    if (ret < 0) {
15,442!
818
      metaError("vgId:%d, failed to upsert tag idx key:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
819
                pReq->suid, tstrerror(ret));
820
    }
821
    metaULock(pMeta);
15,442✔
822

823
    metaDestroyTagIdxKey(pTagIdxKey);
15,442✔
824
    pTagIdxKey = NULL;
15,442✔
825
  }
826

827
  nStbEntry.version = version;
997✔
828
  nStbEntry.type = TSDB_SUPER_TABLE;
997✔
829
  nStbEntry.uid = pReq->suid;
997✔
830
  nStbEntry.name = pReq->name;
997✔
831
  nStbEntry.stbEntry.schemaRow = pReq->schemaRow;
997✔
832
  nStbEntry.stbEntry.schemaTag = pReq->schemaTag;
997✔
833
  nStbEntry.colCmpr = pReq->colCmpr;
997✔
834

835
  metaWLock(pMeta);
997✔
836
  // update table.db
837
  ret = metaSaveToTbDb(pMeta, &nStbEntry);
997✔
838
  if (ret < 0) {
997!
839
    metaError("vgId:%d, failed to save tb db:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
840
              pReq->suid, tstrerror(ret));
841
  }
842
  // update uid index
843
  ret = metaUpdateUidIdx(pMeta, &nStbEntry);
997✔
844
  if (ret < 0) {
997!
845
    metaError("vgId:%d, failed to update uid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
846
              pReq->suid, tstrerror(ret));
847
  }
848
  metaULock(pMeta);
997✔
849

850
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
997!
851
  tDecoderClear(&dc);
997✔
852
  tdbFree(pData);
997✔
853

854
  tdbTbcClose(pCtbIdxc);
997✔
855
  return TSDB_CODE_SUCCESS;
997✔
856
_err:
×
857
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
×
858
  tDecoderClear(&dc);
×
859
  tdbFree(pData);
×
860

861
  return code;
×
862
}
863
int metaDropIndexFromSTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
2,172✔
864
  int32_t    code = 0;
2,172✔
865
  SMetaEntry oStbEntry = {0};
2,172✔
866
  SMetaEntry nStbEntry = {0};
2,172✔
867

868
  STbDbKey tbDbKey = {0};
2,172✔
869
  TBC     *pUidIdxc = NULL;
2,172✔
870
  TBC     *pTbDbc = NULL;
2,172✔
871
  int      ret = 0;
2,172✔
872
  int      c = -2;
2,172✔
873
  void    *pData = NULL;
2,172✔
874
  int      nData = 0;
2,172✔
875
  int64_t  oversion;
876
  SDecoder dc = {0};
2,172✔
877

878
  tb_uid_t suid = pReq->stbUid;
2,172✔
879

880
  if ((code = tdbTbGet(pMeta->pUidIdx, &suid, sizeof(tb_uid_t), &pData, &nData)) != 0) {
2,172!
881
    goto _err;
×
882
  }
883

884
  tbDbKey.uid = suid;
2,172✔
885
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
2,172✔
886
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData)) != 0) {
2,172!
887
    goto _err;
×
888
  }
889

890
  tDecoderInit(&dc, pData, nData);
2,172✔
891
  code = metaDecodeEntry(&dc, &oStbEntry);
2,172✔
892
  if (code != 0) {
2,172!
893
    goto _err;
×
894
  }
895

896
  SSchema *pCol = NULL;
2,172✔
897
  int32_t  colId = -1;
2,172✔
898
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
6,960!
899
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
6,960✔
900
    if (0 == strncmp(schema->name, pReq->colName, sizeof(pReq->colName))) {
6,960✔
901
      if (IS_IDX_ON(schema)) {
2,172!
902
        pCol = schema;
2,172✔
903
      }
904
      break;
2,172✔
905
    }
906
  }
907

908
  if (pCol == NULL) {
2,172!
909
    metaError("vgId:%d, failed to drop index on %s.%s,since %s", TD_VID(pMeta->pVnode), pReq->stb, pReq->colName,
×
910
              tstrerror(TSDB_CODE_VND_COL_NOT_EXISTS));
911
    code = 0;
×
912

913
    goto _err;
×
914
  }
915

916
  /*
917
   * iterator all pTdDbc by uid and version
918
   */
919
  TBC *pCtbIdxc = NULL;
2,172✔
920
  code = tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL);
2,172✔
921
  if (code != 0) {
2,172!
922
    goto _err;
×
923
  }
924

925
  code = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c);
2,172✔
926
  if (code < 0) {
2,172!
927
    tdbTbcClose(pCtbIdxc);
×
928
    goto _err;
×
929
  }
930
  for (;;) {
22,400✔
931
    void *pKey = NULL, *pVal = NULL;
24,572✔
932
    int   nKey = 0, nVal = 0;
24,572✔
933

934
    code = tdbTbcNext(pCtbIdxc, &pKey, &nKey, &pVal, &nVal);
24,572✔
935
    if (code < 0) {
24,572✔
936
      tdbFree(pKey);
2,172✔
937
      tdbFree(pVal);
2,172✔
938
      tdbTbcClose(pCtbIdxc);
2,172✔
939
      pCtbIdxc = NULL;
2,172✔
940
      break;
2,172✔
941
    }
942
    if (((SCtbIdxKey *)pKey)->suid != suid) {
22,400✔
943
      tdbFree(pKey);
9,197✔
944
      tdbFree(pVal);
9,197✔
945
      continue;
9,197✔
946
    }
947
    STagIdxKey *pTagIdxKey = NULL;
13,203✔
948
    int32_t     nTagIdxKey;
949

950
    const void *pTagData = NULL;
13,203✔
951
    int32_t     nTagData = 0;
13,203✔
952

953
    SCtbIdxKey *table = (SCtbIdxKey *)pKey;
13,203✔
954
    STagVal     tagVal = {.cid = pCol->colId};
13,203✔
955
    if (tTagGet((const STag *)pVal, &tagVal)) {
13,203!
956
      if (IS_VAR_DATA_TYPE(pCol->type)) {
13,203!
957
        pTagData = tagVal.pData;
3,679✔
958
        nTagData = (int32_t)tagVal.nData;
3,679✔
959
      } else {
960
        pTagData = &(tagVal.i64);
9,524✔
961
        nTagData = tDataTypes[pCol->type].bytes;
9,524✔
962
      }
963
    } else {
964
      if (!IS_VAR_DATA_TYPE(pCol->type)) {
×
965
        nTagData = tDataTypes[pCol->type].bytes;
×
966
      }
967
    }
968

969
    code = metaCreateTagIdxKey(suid, pCol->colId, pTagData, nTagData, pCol->type, table->uid, &pTagIdxKey, &nTagIdxKey);
13,203✔
970
    tdbFree(pKey);
13,203✔
971
    tdbFree(pVal);
13,203✔
972
    if (code < 0) {
13,203!
973
      metaDestroyTagIdxKey(pTagIdxKey);
×
974
      tdbTbcClose(pCtbIdxc);
×
975
      goto _err;
×
976
    }
977

978
    metaWLock(pMeta);
13,203✔
979
    ret = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
13,203✔
980
    if (ret < 0) {
13,203!
981
      metaError("vgId:%d, failed to delete tag idx key:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->stb,
×
982
                pReq->stbUid, tstrerror(ret));
983
    }
984
    metaULock(pMeta);
13,203✔
985
    metaDestroyTagIdxKey(pTagIdxKey);
13,203✔
986
    pTagIdxKey = NULL;
13,203✔
987
  }
988

989
  // clear idx flag
990
  SSCHMEA_SET_IDX_OFF(pCol);
2,172✔
991

992
  nStbEntry.version = version;
2,172✔
993
  nStbEntry.type = TSDB_SUPER_TABLE;
2,172✔
994
  nStbEntry.uid = oStbEntry.uid;
2,172✔
995
  nStbEntry.name = oStbEntry.name;
2,172!
996

997
  SSchemaWrapper  *row = tCloneSSchemaWrapper(&oStbEntry.stbEntry.schemaRow);
2,172!
998
  SSchemaWrapper  *tag = tCloneSSchemaWrapper(&oStbEntry.stbEntry.schemaTag);
2,172!
999
  SColCmprWrapper *cmpr = tCloneSColCmprWrapper(&oStbEntry.colCmpr);
2,172✔
1000
  if (row == NULL || tag == NULL || cmpr == NULL) {
2,172!
1001
    tDeleteSchemaWrapper(row);
1002
    tDeleteSchemaWrapper(tag);
1003
    tDeleteSColCmprWrapper(cmpr);
1004
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1005

1006
    tdbTbcClose(pCtbIdxc);
×
1007
    goto _err;
×
1008
  }
1009

1010
  nStbEntry.stbEntry.schemaRow = *row;
2,172✔
1011
  nStbEntry.stbEntry.schemaTag = *tag;
2,172✔
1012
  nStbEntry.stbEntry.rsmaParam = oStbEntry.stbEntry.rsmaParam;
2,172✔
1013
  nStbEntry.colCmpr = *cmpr;
2,172✔
1014

1015
  nStbEntry.colCmpr = oStbEntry.colCmpr;
2,172✔
1016

1017
  metaWLock(pMeta);
2,172✔
1018
  // update table.db
1019
  ret = metaSaveToTbDb(pMeta, &nStbEntry);
2,172✔
1020
  if (ret < 0) {
2,172!
1021
    metaError("vgId:%d, failed to save tb db:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->stb,
×
1022
              pReq->stbUid, tstrerror(ret));
1023
  }
1024
  // update uid index
1025
  ret = metaUpdateUidIdx(pMeta, &nStbEntry);
2,172✔
1026
  if (ret < 0) {
2,172!
1027
    metaError("vgId:%d, failed to update uid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->stb,
×
1028
              pReq->stbUid, tstrerror(ret));
1029
  }
1030
  metaULock(pMeta);
2,172✔
1031

1032
  tDeleteSchemaWrapper(tag);
1033
  tDeleteSchemaWrapper(row);
1034
  tDeleteSColCmprWrapper(cmpr);
1035

1036
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
2,172!
1037
  tDecoderClear(&dc);
2,172✔
1038
  tdbFree(pData);
2,172✔
1039

1040
  tdbTbcClose(pCtbIdxc);
2,172✔
1041
  return TSDB_CODE_SUCCESS;
2,172✔
1042
_err:
×
1043
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
×
1044
  tDecoderClear(&dc);
×
1045
  tdbFree(pData);
×
1046

1047
  return code;
×
1048
}
1049

1050
int metaCreateTable(SMeta *pMeta, int64_t ver, SVCreateTbReq *pReq, STableMetaRsp **pMetaRsp) {
192,484✔
1051
  SMetaEntry  me = {0};
192,484✔
1052
  SMetaReader mr = {0};
192,484✔
1053
  int32_t     ret;
1054

1055
  // validate message
1056
  if (pReq->type != TSDB_CHILD_TABLE && pReq->type != TSDB_NORMAL_TABLE) {
192,484!
1057
    terrno = TSDB_CODE_INVALID_MSG;
×
1058
    goto _err;
×
1059
  }
1060

1061
  if (pReq->type == TSDB_CHILD_TABLE) {
192,484✔
1062
    tb_uid_t suid = metaGetTableEntryUidByName(pMeta, pReq->ctb.stbName);
177,321✔
1063
    if (suid != pReq->ctb.suid) {
177,309!
1064
      return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
1065
    }
1066
  }
1067

1068
  // validate req
1069
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
192,472✔
1070
  if (metaGetTableEntryByName(&mr, pReq->name) == 0) {
192,497✔
1071
    if (pReq->type == TSDB_CHILD_TABLE && pReq->ctb.suid != mr.me.ctbEntry.suid) {
21,399✔
1072
      metaReaderClear(&mr);
9✔
1073
      return terrno = TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
9✔
1074
    }
1075
    pReq->uid = mr.me.uid;
21,390✔
1076
    if (pReq->type == TSDB_CHILD_TABLE) {
21,390✔
1077
      pReq->ctb.suid = mr.me.ctbEntry.suid;
21,384✔
1078
    }
1079
    metaReaderClear(&mr);
21,390✔
1080
    return terrno = TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
21,391✔
1081
  } else if (terrno == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
171,092!
1082
    terrno = TSDB_CODE_SUCCESS;
171,090✔
1083
  }
1084
  metaReaderClear(&mr);
171,083✔
1085

1086
  bool sysTbl = (pReq->type == TSDB_CHILD_TABLE) && metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1);
171,091!
1087

1088
  if (!sysTbl && ((terrno = grantCheck(TSDB_GRANT_TIMESERIES)) < 0)) goto _err;
171,081!
1089

1090
  // build SMetaEntry
1091
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
171,055✔
1092
  me.version = ver;
171,055✔
1093
  me.type = pReq->type;
171,055✔
1094
  me.uid = pReq->uid;
171,055✔
1095
  me.name = pReq->name;
171,055✔
1096
  if (me.type == TSDB_CHILD_TABLE) {
171,055✔
1097
    me.ctbEntry.btime = pReq->btime;
155,881✔
1098
    me.ctbEntry.ttlDays = pReq->ttl;
155,881✔
1099
    me.ctbEntry.commentLen = pReq->commentLen;
155,881✔
1100
    me.ctbEntry.comment = pReq->comment;
155,881✔
1101
    me.ctbEntry.suid = pReq->ctb.suid;
155,881✔
1102
    me.ctbEntry.pTags = pReq->ctb.pTag;
155,881✔
1103

1104
#ifdef TAG_FILTER_DEBUG
1105
    SArray *pTagVals = NULL;
1106
    int32_t code = tTagToValArray((STag *)pReq->ctb.pTag, &pTagVals);
1107
    for (int i = 0; i < taosArrayGetSize(pTagVals); i++) {
1108
      STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
1109

1110
      if (IS_VAR_DATA_TYPE(pTagVal->type)) {
1111
        char *buf = taosMemoryCalloc(pTagVal->nData + 1, 1);
1112
        memcpy(buf, pTagVal->pData, pTagVal->nData);
1113
        metaDebug("metaTag table:%s varchar index:%d cid:%d type:%d value:%s", pReq->name, i, pTagVal->cid,
1114
                  pTagVal->type, buf);
1115
        taosMemoryFree(buf);
1116
      } else {
1117
        double val = 0;
1118
        GET_TYPED_DATA(val, double, pTagVal->type, &pTagVal->i64);
1119
        metaDebug("metaTag table:%s number index:%d cid:%d type:%d value:%f", pReq->name, i, pTagVal->cid,
1120
                  pTagVal->type, val);
1121
      }
1122
    }
1123
#endif
1124

1125
    ++pStats->numOfCTables;
155,881✔
1126

1127
    if (!sysTbl) {
155,881✔
1128
      int32_t nCols = 0;
155,880✔
1129
      ret = metaGetStbStats(pMeta->pVnode, me.ctbEntry.suid, 0, &nCols);
155,880✔
1130
      if (ret < 0) {
155,919!
1131
        metaError("vgId:%d, failed to get stb stats:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
1132
                  pReq->ctb.suid, tstrerror(ret));
1133
      }
1134
      pStats->numOfTimeSeries += nCols - 1;
155,920✔
1135
    }
1136

1137
    metaWLock(pMeta);
155,921✔
1138
    metaUpdateStbStats(pMeta, me.ctbEntry.suid, 1, 0);
155,924✔
1139
    ret = metaUidCacheClear(pMeta, me.ctbEntry.suid);
155,899✔
1140
    if (ret < 0) {
155,926!
1141
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
1142
                pReq->ctb.suid, tstrerror(ret));
1143
    }
1144
    ret = metaTbGroupCacheClear(pMeta, me.ctbEntry.suid);
155,926✔
1145
    if (ret < 0) {
155,922!
1146
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
1147
                pReq->ctb.suid, tstrerror(ret));
1148
    }
1149
    metaULock(pMeta);
155,922✔
1150

1151
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
155,918✔
1152
      ret = tsdbCacheNewTable(pMeta->pVnode->pTsdb, me.uid, me.ctbEntry.suid, NULL);
960✔
1153
      if (ret < 0) {
960!
1154
        metaError("vgId:%d, failed to create table:%s since %s", TD_VID(pMeta->pVnode), pReq->name, tstrerror(ret));
×
1155
        goto _err;
×
1156
      }
1157
    }
1158
  } else {
1159
    me.ntbEntry.btime = pReq->btime;
15,174✔
1160
    me.ntbEntry.ttlDays = pReq->ttl;
15,174✔
1161
    me.ntbEntry.commentLen = pReq->commentLen;
15,174✔
1162
    me.ntbEntry.comment = pReq->comment;
15,174✔
1163
    me.ntbEntry.schemaRow = pReq->ntb.schemaRow;
15,174✔
1164
    me.ntbEntry.ncid = me.ntbEntry.schemaRow.pSchema[me.ntbEntry.schemaRow.nCols - 1].colId + 1;
15,174✔
1165
    me.colCmpr = pReq->colCmpr;
15,174✔
1166
    TABLE_SET_COL_COMPRESSED(me.flags);
15,174✔
1167

1168
    ++pStats->numOfNTables;
15,174✔
1169
    pStats->numOfNTimeSeries += me.ntbEntry.schemaRow.nCols - 1;
15,174✔
1170

1171
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
15,174✔
1172
      ret = tsdbCacheNewTable(pMeta->pVnode->pTsdb, me.uid, -1, &me.ntbEntry.schemaRow);
42✔
1173
      if (ret < 0) {
42!
1174
        metaError("vgId:%d, failed to create table:%s since %s", TD_VID(pMeta->pVnode), pReq->name, tstrerror(ret));
×
1175
        goto _err;
×
1176
      }
1177
    }
1178
  }
1179

1180
  if (metaHandleEntry(pMeta, &me) < 0) goto _err;
171,092!
1181

1182
  metaTimeSeriesNotifyCheck(pMeta);
171,067✔
1183

1184
  if (pMetaRsp) {
171,091!
1185
    *pMetaRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
171,093✔
1186

1187
    if (*pMetaRsp) {
171,090!
1188
      if (me.type == TSDB_CHILD_TABLE) {
171,092✔
1189
        (*pMetaRsp)->tableType = TSDB_CHILD_TABLE;
155,919✔
1190
        (*pMetaRsp)->tuid = pReq->uid;
155,919✔
1191
        (*pMetaRsp)->suid = pReq->ctb.suid;
155,919✔
1192
        strcpy((*pMetaRsp)->tbName, pReq->name);
155,919✔
1193
      } else {
1194
        ret = metaUpdateMetaRsp(pReq->uid, pReq->name, &pReq->ntb.schemaRow, *pMetaRsp);
15,173✔
1195
        if (ret < 0) {
15,173!
1196
          metaError("vgId:%d, failed to update meta rsp:%s since %s", TD_VID(pMeta->pVnode), pReq->name,
×
1197
                    tstrerror(ret));
1198
        }
1199
        for (int32_t i = 0; i < pReq->colCmpr.nCols; i++) {
275,305✔
1200
          SColCmpr *p = &pReq->colCmpr.pColCmpr[i];
260,132✔
1201
          (*pMetaRsp)->pSchemaExt[i].colId = p->id;
260,132✔
1202
          (*pMetaRsp)->pSchemaExt[i].compress = p->alg;
260,132✔
1203
        }
1204
      }
1205
    }
1206
  }
1207

1208
  pMeta->changed = true;
171,088✔
1209
  metaDebug("vgId:%d, table:%s uid %" PRId64 " is created, type:%" PRId8, TD_VID(pMeta->pVnode), pReq->name, pReq->uid,
171,088✔
1210
            pReq->type);
1211
  return 0;
171,094✔
1212

1213
_err:
×
1214
  metaError("vgId:%d, failed to create table:%s type:%s since %s", TD_VID(pMeta->pVnode), pReq->name,
×
1215
            pReq->type == TSDB_CHILD_TABLE ? "child table" : "normal table", tstrerror(terrno));
1216
  return TSDB_CODE_FAILED;
×
1217
}
1218

1219
int metaDropTable(SMeta *pMeta, int64_t version, SVDropTbReq *pReq, SArray *tbUids, tb_uid_t *tbUid) {
16,032✔
1220
  void    *pData = NULL;
16,032✔
1221
  int      nData = 0;
16,032✔
1222
  int      rc = 0;
16,032✔
1223
  tb_uid_t uid = 0;
16,032✔
1224
  tb_uid_t suid = 0;
16,032✔
1225
  int8_t   sysTbl = 0;
16,032✔
1226
  int      type;
1227

1228
  rc = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &pData, &nData);
16,032✔
1229
  if (rc < 0) {
16,032!
1230
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1231
  }
1232
  uid = *(tb_uid_t *)pData;
16,032✔
1233

1234
  metaWLock(pMeta);
16,032✔
1235
  rc = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
16,032✔
1236
  metaULock(pMeta);
16,032✔
1237

1238
  if (rc < 0) goto _exit;
16,032!
1239

1240
  if (!sysTbl && type == TSDB_CHILD_TABLE) {
16,032!
1241
    int32_t      nCols = 0;
13,669✔
1242
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
13,669✔
1243
    if (metaGetStbStats(pMeta->pVnode, suid, NULL, &nCols) == 0) {
13,669!
1244
      pStats->numOfTimeSeries -= nCols - 1;
13,669✔
1245
    }
1246
  }
1247

1248
  if ((type == TSDB_CHILD_TABLE || type == TSDB_NORMAL_TABLE) && tbUids) {
16,032!
1249
    if (taosArrayPush(tbUids, &uid) == NULL) {
16,032!
1250
      rc = terrno;
×
1251
      goto _exit;
×
1252
    }
1253

1254
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
16,032✔
1255
      int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
75✔
1256
      if (ret < 0) {
75!
1257
        metaError("vgId:%d, failed to drop table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, uid,
×
1258
                  tstrerror(ret));
1259
      }
1260
    }
1261
  }
1262

1263
  if ((type == TSDB_CHILD_TABLE) && tbUid) {
16,032!
1264
    *tbUid = uid;
13,669✔
1265
  }
1266

1267
  pMeta->changed = true;
16,032✔
1268
_exit:
16,032✔
1269
  tdbFree(pData);
16,032✔
1270
  return rc;
16,032✔
1271
}
1272

1273
int32_t metaDropTables(SMeta *pMeta, SArray *tbUids) {
63✔
1274
  int32_t code = 0;
63✔
1275
  if (taosArrayGetSize(tbUids) == 0) return TSDB_CODE_SUCCESS;
63!
1276

1277
  int64_t    nCtbDropped = 0;
63✔
1278
  SSHashObj *suidHash = tSimpleHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
63✔
1279
  if (suidHash == NULL) {
63!
1280
    return terrno;
×
1281
  }
1282

1283
  metaWLock(pMeta);
63✔
1284
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
5,332✔
1285
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
5,269✔
1286
    tb_uid_t suid = 0;
5,269✔
1287
    int8_t   sysTbl = 0;
5,269✔
1288
    int      type;
1289
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
5,269✔
1290
    if (code) return code;
5,269!
1291
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
5,269!
1292
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
5,260✔
1293
      if (pVal) {
5,260✔
1294
        nCtbDropped = *pVal + 1;
5,199✔
1295
      } else {
1296
        nCtbDropped = 1;
61✔
1297
      }
1298
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
5,260✔
1299
      if (code) return code;
5,260!
1300
    }
1301
    /*
1302
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
1303
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
1304
    }
1305
    */
1306
    metaDebug("batch drop table:%" PRId64, uid);
5,269✔
1307
  }
1308
  metaULock(pMeta);
63✔
1309

1310
  // update timeseries
1311
  void   *pCtbDropped = NULL;
63✔
1312
  int32_t iter = 0;
63✔
1313
  while ((pCtbDropped = tSimpleHashIterate(suidHash, pCtbDropped, &iter))) {
124✔
1314
    tb_uid_t    *pSuid = tSimpleHashGetKey(pCtbDropped, NULL);
61✔
1315
    int32_t      nCols = 0;
61✔
1316
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
61✔
1317
    if (metaGetStbStats(pMeta->pVnode, *pSuid, NULL, &nCols) == 0) {
61!
1318
      pStats->numOfTimeSeries -= *(int64_t *)pCtbDropped * (nCols - 1);
61✔
1319
    }
1320
  }
1321
  tSimpleHashCleanup(suidHash);
63✔
1322

1323
  pMeta->changed = true;
63✔
1324
  return 0;
63✔
1325
}
1326

1327
static int32_t metaFilterTableByHash(SMeta *pMeta, SArray *uidList) {
86✔
1328
  int32_t code = 0;
86✔
1329
  // 1, tranverse table's
1330
  // 2, validate table name using vnodeValidateTableHash
1331
  // 3, push invalidated table's uid into uidList
1332

1333
  TBC *pCur;
1334
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
86✔
1335
  if (code < 0) {
86!
1336
    return code;
×
1337
  }
1338

1339
  code = tdbTbcMoveToFirst(pCur);
86✔
1340
  if (code) {
86!
1341
    tdbTbcClose(pCur);
×
1342
    return code;
×
1343
  }
1344

1345
  void *pData = NULL, *pKey = NULL;
86✔
1346
  int   nData = 0, nKey = 0;
86✔
1347

1348
  while (1) {
10,610✔
1349
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
10,696✔
1350
    if (ret < 0) {
10,696✔
1351
      break;
86✔
1352
    }
1353

1354
    SMetaEntry me = {0};
10,610✔
1355
    SDecoder   dc = {0};
10,610✔
1356
    tDecoderInit(&dc, pData, nData);
10,610✔
1357
    code = metaDecodeEntry(&dc, &me);
10,610✔
1358
    if (code < 0) {
10,610!
1359
      tDecoderClear(&dc);
×
1360
      return code;
×
1361
    }
1362

1363
    if (me.type != TSDB_SUPER_TABLE) {
10,610✔
1364
      char tbFName[TSDB_TABLE_FNAME_LEN + 1];
1365
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pMeta->pVnode->config.dbname, me.name);
10,538✔
1366
      tbFName[TSDB_TABLE_FNAME_LEN] = '\0';
10,538✔
1367
      int32_t ret = vnodeValidateTableHash(pMeta->pVnode, tbFName);
10,538✔
1368
      if (ret < 0 && terrno == TSDB_CODE_VND_HASH_MISMATCH) {
10,538!
1369
        if (taosArrayPush(uidList, &me.uid) == NULL) {
5,269!
1370
          code = terrno;
×
1371
          break;
×
1372
        }
1373
      }
1374
    }
1375
    tDecoderClear(&dc);
10,610✔
1376
  }
1377
  tdbFree(pData);
86✔
1378
  tdbFree(pKey);
86✔
1379
  tdbTbcClose(pCur);
86✔
1380

1381
  return 0;
86✔
1382
}
1383

1384
int32_t metaTrimTables(SMeta *pMeta) {
86✔
1385
  int32_t code = 0;
86✔
1386

1387
  SArray *tbUids = taosArrayInit(8, sizeof(int64_t));
86✔
1388
  if (tbUids == NULL) {
86!
1389
    return terrno;
×
1390
  }
1391

1392
  code = metaFilterTableByHash(pMeta, tbUids);
86✔
1393
  if (code != 0) {
86!
1394
    goto end;
×
1395
  }
1396
  if (TARRAY_SIZE(tbUids) == 0) {
86✔
1397
    goto end;
23✔
1398
  }
1399

1400
  metaInfo("vgId:%d, trim %ld tables", TD_VID(pMeta->pVnode), taosArrayGetSize(tbUids));
63!
1401
  code = metaDropTables(pMeta, tbUids);
63✔
1402
  if (code) goto end;
63!
1403

1404
end:
63✔
1405
  taosArrayDestroy(tbUids);
86✔
1406

1407
  return code;
86✔
1408
}
1409

1410
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
94,866✔
1411
  metaRLock(pMeta);
94,866✔
1412

1413
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
94,961✔
1414

1415
  metaULock(pMeta);
94,604✔
1416

1417
  if (ret != 0) {
94,836!
1418
    metaError("ttl failed to find expired table, ret:%d", ret);
×
1419
  }
1420

1421
  return ret;
94,876✔
1422
}
1423

1424
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
302,985✔
1425
  int64_t btime;
1426
  if (pME->type == TSDB_CHILD_TABLE) {
302,985✔
1427
    btime = pME->ctbEntry.btime;
255,623✔
1428
  } else if (pME->type == TSDB_NORMAL_TABLE) {
47,362✔
1429
    btime = pME->ntbEntry.btime;
17,556✔
1430
  } else {
1431
    return TSDB_CODE_FAILED;
29,806✔
1432
  }
1433

1434
  btimeKey->btime = btime;
273,179✔
1435
  btimeKey->uid = pME->uid;
273,179✔
1436
  return 0;
273,179✔
1437
}
1438

1439
static int metaBuildNColIdxKey(SNcolIdxKey *ncolKey, const SMetaEntry *pME) {
19,640✔
1440
  if (pME->type == TSDB_NORMAL_TABLE) {
19,640!
1441
    ncolKey->ncol = pME->ntbEntry.schemaRow.nCols;
19,640✔
1442
    ncolKey->uid = pME->uid;
19,640✔
1443
  } else {
1444
    return TSDB_CODE_FAILED;
×
1445
  }
1446
  return 0;
19,640✔
1447
}
1448

1449
static void metaDeleteTtl(SMeta *pMeta, const SMetaEntry *pME) {
24,547✔
1450
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
24,547!
1451

1452
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
24,547✔
1453
  if (pME->type == TSDB_CHILD_TABLE) {
24,547✔
1454
    ctx.ttlDays = pME->ctbEntry.ttlDays;
22,153✔
1455
  } else {
1456
    ctx.ttlDays = pME->ntbEntry.ttlDays;
2,394✔
1457
  }
1458

1459
  int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
24,547✔
1460
  if (ret < 0) {
24,547!
1461
    metaError("vgId:%d, failed to delete ttl for table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pME->name,
×
1462
              pME->uid, tstrerror(ret));
1463
  }
1464
  return;
24,547✔
1465
}
1466

1467
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl) {
24,500✔
1468
  void      *pData = NULL;
24,500✔
1469
  int        nData = 0;
24,500✔
1470
  int        rc = 0;
24,500✔
1471
  SMetaEntry e = {0};
24,500✔
1472
  SDecoder   dc = {0};
24,500✔
1473
  int32_t    ret = 0;
24,500✔
1474

1475
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
24,500✔
1476
  if (rc < 0) {
24,500!
1477
    return rc;
×
1478
  }
1479
  int64_t version = ((SUidIdxVal *)pData)[0].version;
24,500✔
1480

1481
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
24,500✔
1482
  if (rc < 0) {
24,500!
1483
    tdbFree(pData);
×
1484
    return rc;
×
1485
  }
1486

1487
  tDecoderInit(&dc, pData, nData);
24,500✔
1488
  rc = metaDecodeEntry(&dc, &e);
24,500✔
1489
  if (rc < 0) {
24,497!
1490
    tDecoderClear(&dc);
×
1491
    return rc;
×
1492
  }
1493

1494
  if (type) *type = e.type;
24,497✔
1495

1496
  if (e.type == TSDB_CHILD_TABLE) {
24,497✔
1497
    if (pSuid) *pSuid = e.ctbEntry.suid;
22,125✔
1498
    void *tData = NULL;
22,125✔
1499
    int   tLen = 0;
22,125✔
1500

1501
    if (tdbTbGet(pMeta->pUidIdx, &e.ctbEntry.suid, sizeof(tb_uid_t), &tData, &tLen) == 0) {
22,125!
1502
      STbDbKey tbDbKey = {.uid = e.ctbEntry.suid, .version = ((SUidIdxVal *)tData)[0].version};
22,128✔
1503
      if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &tData, &tLen) == 0) {
22,128!
1504
        SDecoder   tdc = {0};
22,128✔
1505
        SMetaEntry stbEntry = {0};
22,128✔
1506

1507
        tDecoderInit(&tdc, tData, tLen);
22,128✔
1508
        int32_t ret = metaDecodeEntry(&tdc, &stbEntry);
22,128✔
1509
        if (ret < 0) {
22,126!
1510
          tDecoderClear(&tdc);
×
1511
          metaError("vgId:%d, failed to decode child table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
1512
                    e.ctbEntry.suid, tstrerror(ret));
1513
          return ret;
×
1514
        }
1515

1516
        if (pSysTbl) *pSysTbl = metaTbInFilterCache(pMeta, stbEntry.name, 1) ? 1 : 0;
22,126✔
1517

1518
        SSchema        *pTagColumn = NULL;
22,125✔
1519
        SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
22,125✔
1520
        if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
22,125✔
1521
          pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
196✔
1522
          ret = metaDelJsonVarFromIdx(pMeta, &e, pTagColumn);
196✔
1523
          if (ret < 0) {
196!
1524
            metaError("vgId:%d, failed to delete json var from idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
1525
                      e.name, e.uid, tstrerror(ret));
1526
          }
1527
        } else {
1528
          for (int i = 0; i < pTagSchema->nCols; i++) {
77,320✔
1529
            pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[i];
55,388✔
1530
            if (!IS_IDX_ON(pTagColumn)) continue;
55,388✔
1531
            STagIdxKey *pTagIdxKey = NULL;
25,723✔
1532
            int32_t     nTagIdxKey;
1533

1534
            const void *pTagData = NULL;
25,723✔
1535
            int32_t     nTagData = 0;
25,723✔
1536

1537
            STagVal tagVal = {.cid = pTagColumn->colId};
25,723✔
1538
            if (tTagGet((const STag *)e.ctbEntry.pTags, &tagVal)) {
25,723✔
1539
              if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
25,022!
1540
                pTagData = tagVal.pData;
5,912✔
1541
                nTagData = (int32_t)tagVal.nData;
5,912✔
1542
              } else {
1543
                pTagData = &(tagVal.i64);
19,110✔
1544
                nTagData = tDataTypes[pTagColumn->type].bytes;
19,110✔
1545
              }
1546
            } else {
1547
              if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
704!
1548
                nTagData = tDataTypes[pTagColumn->type].bytes;
592✔
1549
              }
1550
            }
1551

1552
            if (metaCreateTagIdxKey(e.ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type, uid,
25,726!
1553
                                    &pTagIdxKey, &nTagIdxKey) == 0) {
1554
              ret = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
25,725✔
1555
              if (ret < 0) {
25,726!
1556
                metaError("vgId:%d, failed to delete tag idx key:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
1557
                          e.name, e.uid, tstrerror(ret));
1558
              }
1559
            }
1560
            metaDestroyTagIdxKey(pTagIdxKey);
25,726✔
1561
            pTagIdxKey = NULL;
25,726✔
1562
          }
1563
        }
1564
        tDecoderClear(&tdc);
22,128✔
1565
      }
1566
      tdbFree(tData);
22,128✔
1567
    }
1568
  }
1569

1570
  ret = tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), pMeta->txn);
24,500✔
1571
  if (ret < 0) {
24,500!
1572
    metaError("vgId:%d, failed to delete table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
1573
              tstrerror(ret));
1574
  }
1575
  ret = tdbTbDelete(pMeta->pNameIdx, e.name, strlen(e.name) + 1, pMeta->txn);
24,500✔
1576
  if (ret < 0) {
24,500!
1577
    metaError("vgId:%d, failed to delete name idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
1578
              tstrerror(ret));
1579
  }
1580
  ret = tdbTbDelete(pMeta->pUidIdx, &uid, sizeof(uid), pMeta->txn);
24,500✔
1581
  if (ret < 0) {
24,500!
1582
    metaError("vgId:%d, failed to delete uid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
1583
              tstrerror(ret));
1584
  }
1585

1586
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
24,500!
1587
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
24,500✔
1588

1589
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
24,500!
1590

1591
  if (e.type == TSDB_CHILD_TABLE) {
24,500✔
1592
    ret =
1593
        tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
22,128✔
1594
    if (ret < 0) {
22,126!
1595
      metaError("vgId:%d, failed to delete ctb idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
1596
                tstrerror(ret));
1597
    }
1598

1599
    --pMeta->pVnode->config.vndStats.numOfCTables;
22,126✔
1600
    metaUpdateStbStats(pMeta, e.ctbEntry.suid, -1, 0);
22,126✔
1601
    ret = metaUidCacheClear(pMeta, e.ctbEntry.suid);
22,126✔
1602
    if (ret < 0) {
22,128!
1603
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
1604
                e.ctbEntry.suid, tstrerror(ret));
1605
    }
1606
    ret = metaTbGroupCacheClear(pMeta, e.ctbEntry.suid);
22,128✔
1607
    if (ret < 0) {
22,128!
1608
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
1609
                e.ctbEntry.suid, tstrerror(ret));
1610
    }
1611
    /*
1612
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
1613
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, e.ctbEntry.suid, NULL);
1614
    }
1615
    */
1616
  } else if (e.type == TSDB_NORMAL_TABLE) {
2,372!
1617
    // drop schema.db (todo)
1618

1619
    --pMeta->pVnode->config.vndStats.numOfNTables;
2,372✔
1620
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
2,372✔
1621

1622
    /*
1623
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
1624
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, -1, &e.ntbEntry.schemaRow);
1625
    }
1626
    */
1627
  } else if (e.type == TSDB_SUPER_TABLE) {
×
1628
    ret = tdbTbDelete(pMeta->pSuidIdx, &e.uid, sizeof(tb_uid_t), pMeta->txn);
×
1629
    if (ret < 0) {
×
1630
      metaError("vgId:%d, failed to delete suid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
1631
                tstrerror(ret));
1632
    }
1633
    // drop schema.db (todo)
1634

1635
    ret = metaStatsCacheDrop(pMeta, uid);
×
1636
    if (ret < 0) {
×
1637
      metaError("vgId:%d, failed to drop stats cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
1638
                tstrerror(ret));
1639
    }
1640
    ret = metaUidCacheClear(pMeta, uid);
×
1641
    if (ret < 0) {
×
1642
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
1643
                tstrerror(ret));
1644
    }
1645
    ret = metaTbGroupCacheClear(pMeta, uid);
×
1646
    if (ret < 0) {
×
1647
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
1648
                e.uid, tstrerror(ret));
1649
    }
1650
    --pMeta->pVnode->config.vndStats.numOfSTables;
×
1651
  }
1652

1653
  ret = metaCacheDrop(pMeta, uid);
24,500✔
1654
  if (ret < 0) {
24,500✔
1655
    metaError("vgId:%d, failed to drop cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
5,273!
1656
              tstrerror(ret));
1657
  }
1658

1659
  tDecoderClear(&dc);
24,500✔
1660
  tdbFree(pData);
24,500✔
1661

1662
  return 0;
24,500✔
1663
}
1664
// opt ins_tables
1665
int metaUpdateBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
278,724✔
1666
  SBtimeIdxKey btimeKey = {0};
278,724✔
1667
  if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) {
278,724✔
1668
    return 0;
30,410✔
1669
  }
1670
  metaTrace("vgId:%d, start to save version:%" PRId64 " uid:%" PRId64 " btime:%" PRId64, TD_VID(pMeta->pVnode),
248,615✔
1671
            pME->version, pME->uid, btimeKey.btime);
1672

1673
  return tdbTbUpsert(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), NULL, 0, pMeta->txn);
248,615✔
1674
}
1675

1676
int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
24,500✔
1677
  SBtimeIdxKey btimeKey = {0};
24,500✔
1678
  if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) {
24,500!
1679
    return 0;
×
1680
  }
1681
  return tdbTbDelete(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), pMeta->txn);
24,499✔
1682
}
1683
int metaUpdateNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
16,226✔
1684
  SNcolIdxKey ncolKey = {0};
16,226✔
1685
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
16,226!
1686
    return 0;
×
1687
  }
1688
  return tdbTbUpsert(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), NULL, 0, pMeta->txn);
16,226✔
1689
}
1690

1691
int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
3,414✔
1692
  SNcolIdxKey ncolKey = {0};
3,414✔
1693
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
3,414!
1694
    return 0;
×
1695
  }
1696
  return tdbTbDelete(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), pMeta->txn);
3,414✔
1697
}
1698

1699
static int metaAlterTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq, STableMetaRsp *pMetaRsp) {
1,973✔
1700
  void           *pVal = NULL;
1,973✔
1701
  int             nVal = 0;
1,973✔
1702
  const void     *pData = NULL;
1,973✔
1703
  int             nData = 0;
1,973✔
1704
  int             ret = 0;
1,973✔
1705
  tb_uid_t        uid;
1706
  int64_t         oversion;
1707
  SSchema        *pColumn = NULL;
1,973✔
1708
  SMetaEntry      entry = {0};
1,973✔
1709
  SSchemaWrapper *pSchema;
1710
  int             c;
1711
  bool            freeColCmpr = false;
1,973✔
1712
  if (pAlterTbReq->colName == NULL) {
1,973!
1713
    metaError("meta/table: null pAlterTbReq->colName");
×
1714
    return terrno = TSDB_CODE_INVALID_MSG;
×
1715
  }
1716

1717
  // search name index
1718
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
1,973✔
1719
  if (ret < 0) {
1,973!
1720
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1721
  }
1722

1723
  uid = *(tb_uid_t *)pVal;
1,973✔
1724
  tdbFree(pVal);
1,973✔
1725
  pVal = NULL;
1,973✔
1726

1727
  // search uid index
1728
  TBC *pUidIdxc = NULL;
1,973✔
1729

1730
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, NULL));
1,973!
1731
  ret = tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c);
1,973✔
1732
  if (c != 0) {
1,973!
1733
    tdbTbcClose(pUidIdxc);
×
1734
    metaError("meta/table: invalide c: %" PRId32 " alt tb column failed.", c);
×
1735
    return TSDB_CODE_FAILED;
×
1736
  }
1737

1738
  ret = tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
1,973✔
1739
  oversion = ((SUidIdxVal *)pData)[0].version;
1,973✔
1740

1741
  // search table.db
1742
  TBC *pTbDbc = NULL;
1,973✔
1743

1744
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pTbDb, &pTbDbc, NULL));
1,973!
1745
  ret = tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c);
1,973✔
1746
  if (c != 0) {
1,973!
1747
    tdbTbcClose(pUidIdxc);
×
1748
    tdbTbcClose(pTbDbc);
×
1749
    metaError("meta/table: invalide c: %" PRId32 " alt tb column failed.", c);
×
1750
    return TSDB_CODE_FAILED;
×
1751
  }
1752

1753
  ret = tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);
1,973✔
1754

1755
  // get table entry
1756
  SDecoder dc = {0};
1,973✔
1757
  if ((entry.pBuf = taosMemoryMalloc(nData)) == NULL) {
1,973!
1758
    tdbTbcClose(pUidIdxc);
×
1759
    tdbTbcClose(pTbDbc);
×
1760
    return terrno;
×
1761
  }
1762
  memcpy(entry.pBuf, pData, nData);
1,973✔
1763
  tDecoderInit(&dc, entry.pBuf, nData);
1,973✔
1764
  ret = metaDecodeEntry(&dc, &entry);
1,973✔
1765
  if (ret != 0) {
1,973!
1766
    tdbTbcClose(pUidIdxc);
×
1767
    tdbTbcClose(pTbDbc);
×
1768
    tDecoderClear(&dc);
×
1769
    metaError("meta/table: invalide ret: %" PRId32 " alt tb column failed.", ret);
×
1770
    return ret;
×
1771
  }
1772

1773
  if (entry.type != TSDB_NORMAL_TABLE) {
1,973✔
1774
    terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
15✔
1775
    goto _err;
15✔
1776
  }
1777
  // search the column to add/drop/update
1778
  pSchema = &entry.ntbEntry.schemaRow;
1,958✔
1779

1780
  // save old entry
1781
  SMetaEntry oldEntry = {.type = TSDB_NORMAL_TABLE, .uid = entry.uid};
1,958✔
1782
  oldEntry.ntbEntry.schemaRow.nCols = pSchema->nCols;
1,958✔
1783

1784
  int32_t rowLen = -1;
1,958✔
1785
  if (pAlterTbReq->action == TSDB_ALTER_TABLE_ADD_COLUMN ||
1,958✔
1786
      pAlterTbReq->action == TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES) {
867✔
1787
    rowLen = 0;
1,834✔
1788
  }
1789

1790
  int32_t  iCol = 0, jCol = 0;
1,958✔
1791
  SSchema *qColumn = NULL;
1,958✔
1792
  for (;;) {
1793
    qColumn = NULL;
100,608✔
1794

1795
    if (jCol >= pSchema->nCols) break;
100,608✔
1796
    qColumn = &pSchema->pSchema[jCol];
98,651✔
1797

1798
    if (!pColumn && (strcmp(qColumn->name, pAlterTbReq->colName) == 0)) {
98,651✔
1799
      pColumn = qColumn;
1,366✔
1800
      iCol = jCol;
1,366✔
1801
      if (rowLen < 0) break;
1,366✔
1802
    }
1803
    rowLen += qColumn->bytes;
98,650✔
1804
    ++jCol;
98,650✔
1805
  }
1806

1807
  entry.version = version;
1,958✔
1808
  int      tlen;
1809
  SSchema *pNewSchema = NULL;
1,958✔
1810
  SSchema  tScheam;
1811
  switch (pAlterTbReq->action) {
1,958!
1812
    case TSDB_ALTER_TABLE_ADD_COLUMN:
1,092✔
1813
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
1814
      if (pColumn) {
1,092✔
1815
        terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
500✔
1816
        goto _err;
500✔
1817
      }
1818
      if ((terrno = grantCheck(TSDB_GRANT_TIMESERIES)) < 0) {
592!
1819
        goto _err;
×
1820
      }
1821
      if (rowLen + pAlterTbReq->bytes > TSDB_MAX_BYTES_PER_ROW) {
592✔
1822
        terrno = TSDB_CODE_PAR_INVALID_ROW_LENGTH;
10✔
1823
        goto _err;
10✔
1824
      }
1825
      pSchema->version++;
582✔
1826
      pSchema->nCols++;
582✔
1827
      pNewSchema = taosMemoryMalloc(sizeof(SSchema) * pSchema->nCols);
582✔
1828
      if (pNewSchema == NULL) {
582!
1829
        goto _err;
×
1830
      }
1831
      memcpy(pNewSchema, pSchema->pSchema, sizeof(SSchema) * (pSchema->nCols - 1));
582✔
1832
      pSchema->pSchema = pNewSchema;
582✔
1833
      pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].bytes = pAlterTbReq->bytes;
582✔
1834
      pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].type = pAlterTbReq->type;
582✔
1835
      pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].flags = pAlterTbReq->flags;
582✔
1836
      pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].colId = entry.ntbEntry.ncid++;
582✔
1837
      strcpy(pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].name, pAlterTbReq->colName);
582✔
1838

1839
      ++pMeta->pVnode->config.vndStats.numOfNTimeSeries;
582✔
1840
      metaTimeSeriesNotifyCheck(pMeta);
582✔
1841

1842
      if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
582✔
1843
        int16_t cid = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].colId;
3✔
1844
        int8_t  col_type = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].type;
3✔
1845
        int32_t ret = tsdbCacheNewNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, col_type);
3✔
1846
        if (ret < 0) {
3!
1847
          terrno = ret;
×
1848
          goto _err;
×
1849
        }
1850
      }
1851
      SSchema *pCol = &pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1];
582✔
1852
      uint32_t compress = pAlterTbReq->action == TSDB_ALTER_TABLE_ADD_COLUMN ? createDefaultColCmprByType(pCol->type)
581✔
1853
                                                                             : pAlterTbReq->compress;
582✔
1854
      if (updataTableColCmpr(&entry.colCmpr, pCol, 1, compress) != 0) {
582!
1855
        metaError("vgId:%d, failed to update table col cmpr:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name,
×
1856
                  entry.uid);
1857
      }
1858
      freeColCmpr = true;
582✔
1859
      if (entry.colCmpr.nCols != pSchema->nCols) {
582!
1860
        if (pNewSchema) taosMemoryFree(pNewSchema);
×
1861
        if (freeColCmpr) taosMemoryFree(entry.colCmpr.pColCmpr);
×
1862
        terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
1863
        goto _err;
×
1864
      }
1865
      break;
582✔
1866
    case TSDB_ALTER_TABLE_DROP_COLUMN:
79✔
1867
      if (pColumn == NULL) {
79!
1868
        terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
×
1869
        goto _err;
×
1870
      }
1871
      if (pColumn->colId == 0) {
79!
1872
        terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
1873
        goto _err;
×
1874
      }
1875
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
79✔
1876
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
11✔
1877
        goto _err;
11✔
1878
      }
1879
      bool hasPrimayKey = false;
68✔
1880
      if (pSchema->nCols >= 2) {
68!
1881
        hasPrimayKey = pSchema->pSchema[1].flags & COL_IS_KEY ? true : false;
68✔
1882
      }
1883

1884
      memcpy(&tScheam, pColumn, sizeof(SSchema));
68✔
1885
      pSchema->version++;
68✔
1886
      tlen = (pSchema->nCols - iCol - 1) * sizeof(SSchema);
68✔
1887
      if (tlen) {
68✔
1888
        memmove(pColumn, pColumn + 1, tlen);
35✔
1889
      }
1890
      pSchema->nCols--;
68✔
1891

1892
      --pMeta->pVnode->config.vndStats.numOfNTimeSeries;
68✔
1893

1894
      if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
68✔
1895
        int16_t cid = pColumn->colId;
3✔
1896

1897
        if (tsdbCacheDropNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, hasPrimayKey) != 0) {
3!
1898
          metaError("vgId:%d, failed to drop ntable column:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name,
×
1899
                    entry.uid);
1900
        }
1901
      }
1902

1903
      if (updataTableColCmpr(&entry.colCmpr, &tScheam, 0, 0) != 0) {
68!
1904
        metaError("vgId:%d, failed to update table col cmpr:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name,
×
1905
                  entry.uid);
1906
      }
1907
      if (entry.colCmpr.nCols != pSchema->nCols) {
68!
1908
        terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
1909
        goto _err;
×
1910
      }
1911
      break;
68✔
1912
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
743✔
1913
      if (pColumn == NULL) {
743!
1914
        terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
×
1915
        goto _err;
×
1916
      }
1917
      if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes >= pAlterTbReq->colModBytes) {
743!
1918
        terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
301✔
1919
        goto _err;
301✔
1920
      }
1921
      if (rowLen + pAlterTbReq->colModBytes - pColumn->bytes > TSDB_MAX_BYTES_PER_ROW) {
442✔
1922
        terrno = TSDB_CODE_PAR_INVALID_ROW_LENGTH;
70✔
1923
        goto _err;
70✔
1924
      }
1925
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
372✔
1926
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
13✔
1927
        goto _err;
13✔
1928
      }
1929
      pSchema->version++;
359✔
1930
      pColumn->bytes = pAlterTbReq->colModBytes;
359✔
1931
      break;
359✔
1932
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
44✔
1933
      if (pAlterTbReq->colNewName == NULL) {
44!
1934
        terrno = TSDB_CODE_INVALID_MSG;
×
1935
        goto _err;
×
1936
      }
1937
      if (pColumn == NULL) {
44!
1938
        terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
×
1939
        goto _err;
×
1940
      }
1941
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
44✔
1942
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
11✔
1943
        goto _err;
11✔
1944
      }
1945
      pSchema->version++;
33✔
1946
      strcpy(pColumn->name, pAlterTbReq->colNewName);
33✔
1947
      break;
33✔
1948
  }
1949

1950
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
1,042✔
1951
    tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, entry.uid, pSchema->version);
6✔
1952
  }
1953

1954
  entry.version = version;
1,042✔
1955

1956
  // do actual write
1957
  metaWLock(pMeta);
1,042✔
1958

1959
  if (metaDeleteNcolIdx(pMeta, &oldEntry) < 0) {
1,042!
1960
    metaError("vgId:%d, failed to delete ncol idx:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
×
1961
  }
1962

1963
  if (metaUpdateNcolIdx(pMeta, &entry) < 0) {
1,042!
1964
    metaError("vgId:%d, failed to update ncol idx:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
×
1965
  }
1966

1967
  // save to table db
1968
  if (metaSaveToTbDb(pMeta, &entry) < 0) {
1,042!
1969
    metaError("vgId:%d, failed to save to tb db:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
×
1970
  }
1971

1972
  if (metaUpdateUidIdx(pMeta, &entry) < 0) {
1,042!
1973
    metaError("vgId:%d, failed to update uid idx:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
×
1974
  }
1975

1976
  if (metaSaveToSkmDb(pMeta, &entry) < 0) {
1,042!
1977
    metaError("vgId:%d, failed to save to skm db:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
×
1978
  }
1979

1980
  if (metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs) < 0) {
1,042!
1981
    metaError("vgId:%d, failed to update change time:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
×
1982
  }
1983

1984
  metaULock(pMeta);
1,042✔
1985

1986
  if (metaUpdateMetaRsp(uid, pAlterTbReq->tbName, pSchema, pMetaRsp) < 0) {
1,042!
1987
    metaError("vgId:%d, failed to update meta rsp:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
×
1988
  }
1989
  for (int32_t i = 0; i < entry.colCmpr.nCols; i++) {
70,557✔
1990
    SColCmpr *p = &entry.colCmpr.pColCmpr[i];
69,515✔
1991
    pMetaRsp->pSchemaExt[i].colId = p->id;
69,515✔
1992
    pMetaRsp->pSchemaExt[i].compress = p->alg;
69,515✔
1993
  }
1994

1995
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
1,042!
1996
  if (pNewSchema) taosMemoryFree(pNewSchema);
1,042✔
1997
  if (freeColCmpr) taosMemoryFree(entry.colCmpr.pColCmpr);
1,042✔
1998

1999
  tdbTbcClose(pTbDbc);
1,042✔
2000
  tdbTbcClose(pUidIdxc);
1,042✔
2001
  tDecoderClear(&dc);
1,042✔
2002

2003
  return 0;
1,042✔
2004

2005
_err:
931✔
2006
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
931!
2007
  tdbTbcClose(pTbDbc);
931✔
2008
  tdbTbcClose(pUidIdxc);
931✔
2009
  tDecoderClear(&dc);
931✔
2010

2011
  return terrno != 0 ? terrno : TSDB_CODE_FAILED;
931!
2012
}
2013

2014
static int metaUpdateTableTagVal(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
3,103✔
2015
  SMetaEntry  ctbEntry = {0};
3,103✔
2016
  SMetaEntry  stbEntry = {0};
3,103✔
2017
  void       *pVal = NULL;
3,103✔
2018
  int         nVal = 0;
3,103✔
2019
  int         ret;
2020
  int         c;
2021
  tb_uid_t    uid;
2022
  int64_t     oversion;
2023
  const void *pData = NULL;
3,103✔
2024
  int         nData = 0;
3,103✔
2025

2026
  if (pAlterTbReq->tagName == NULL) {
3,103!
2027
    return terrno = TSDB_CODE_INVALID_MSG;
×
2028
  }
2029

2030
  // search name index
2031
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
3,103✔
2032
  if (ret < 0) {
3,103✔
2033
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
1✔
2034
  }
2035

2036
  uid = *(tb_uid_t *)pVal;
3,102✔
2037
  tdbFree(pVal);
3,102✔
2038
  pVal = NULL;
3,102✔
2039

2040
  // search uid index
2041
  TBC *pUidIdxc = NULL;
3,102✔
2042

2043
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, NULL));
3,102!
2044
  if (tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c) < 0) {
3,102!
2045
    metaTrace("meta/table: failed to move to uid index, uid:%" PRId64, uid);
×
2046
  }
2047
  if (c != 0) {
3,102!
2048
    tdbTbcClose(pUidIdxc);
×
2049
    metaError("meta/table: invalide c: %" PRId32 " update tb tag val failed.", c);
×
2050
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2051
  }
2052

2053
  if (tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData) != 0) {
3,102!
2054
    metaError("meta/table: failed to get uid index, uid:%" PRId64, uid);
×
2055
  }
2056
  oversion = ((SUidIdxVal *)pData)[0].version;
3,102✔
2057

2058
  // search table.db
2059
  TBC     *pTbDbc = NULL;
3,102✔
2060
  SDecoder dc1 = {0};
3,102✔
2061
  SDecoder dc2 = {0};
3,102✔
2062

2063
  /* get ctbEntry */
2064
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pTbDb, &pTbDbc, NULL));
3,102!
2065
  if (tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c) != 0) {
3,102!
2066
    metaError("meta/table: failed to move to tb db, uid:%" PRId64, uid);
×
2067
  }
2068
  if (c != 0) {
3,102!
2069
    tdbTbcClose(pUidIdxc);
×
2070
    tdbTbcClose(pTbDbc);
×
2071
    metaError("meta/table: invalide c: %" PRId32 " update tb tag val failed.", c);
×
2072
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2073
  }
2074

2075
  if (tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData) != 0) {
3,102!
2076
    metaError("meta/table: failed to get tb db, uid:%" PRId64, uid);
×
2077
  }
2078

2079
  if ((ctbEntry.pBuf = taosMemoryMalloc(nData)) == NULL) {
3,102!
2080
    tdbTbcClose(pUidIdxc);
×
2081
    tdbTbcClose(pTbDbc);
×
2082
    return terrno;
×
2083
  }
2084
  memcpy(ctbEntry.pBuf, pData, nData);
3,102✔
2085
  tDecoderInit(&dc1, ctbEntry.pBuf, nData);
3,102✔
2086
  ret = metaDecodeEntry(&dc1, &ctbEntry);
3,102✔
2087
  if (ret < 0) {
3,102!
2088
    terrno = ret;
×
2089
    goto _err;
×
2090
  }
2091

2092
  /* get stbEntry*/
2093
  if (tdbTbGet(pMeta->pUidIdx, &ctbEntry.ctbEntry.suid, sizeof(tb_uid_t), &pVal, &nVal) != 0) {
3,102!
2094
    metaError("meta/table: failed to get uid index, uid:%" PRId64, ctbEntry.ctbEntry.suid);
×
2095
  }
2096
  if (!pVal) {
3,102!
2097
    terrno = TSDB_CODE_INVALID_MSG;
×
2098
    goto _err;
×
2099
  }
2100

2101
  if (tdbTbGet(pMeta->pTbDb, &((STbDbKey){.uid = ctbEntry.ctbEntry.suid, .version = ((SUidIdxVal *)pVal)[0].version}),
3,102!
2102
               sizeof(STbDbKey), (void **)&stbEntry.pBuf, &nVal) != 0) {
2103
    metaError("meta/table: failed to get tb db, uid:%" PRId64, ctbEntry.ctbEntry.suid);
×
2104
  }
2105
  tdbFree(pVal);
3,102✔
2106
  tDecoderInit(&dc2, stbEntry.pBuf, nVal);
3,102✔
2107
  ret = metaDecodeEntry(&dc2, &stbEntry);
3,102✔
2108
  if (ret < 0) {
3,102!
2109
    terrno = ret;
×
2110
    goto _err;
×
2111
  }
2112

2113
  SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
3,102✔
2114
  SSchema        *pColumn = NULL;
3,102✔
2115
  int32_t         iCol = 0;
3,102✔
2116
  for (;;) {
2117
    pColumn = NULL;
5,253✔
2118

2119
    if (iCol >= pTagSchema->nCols) break;
5,253!
2120
    pColumn = &pTagSchema->pSchema[iCol];
5,253✔
2121

2122
    if (strcmp(pColumn->name, pAlterTbReq->tagName) == 0) break;
5,253✔
2123
    iCol++;
2,151✔
2124
  }
2125

2126
  if (pColumn == NULL) {
3,102!
2127
    terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
×
2128
    goto _err;
×
2129
  }
2130

2131
  ctbEntry.version = version;
3,102✔
2132
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
3,102✔
2133
    ctbEntry.ctbEntry.pTags = taosMemoryMalloc(pAlterTbReq->nTagVal);
205✔
2134
    if (ctbEntry.ctbEntry.pTags == NULL) {
205!
2135
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2136
      goto _err;
×
2137
    }
2138
    memcpy((void *)ctbEntry.ctbEntry.pTags, pAlterTbReq->pTagVal, pAlterTbReq->nTagVal);
205✔
2139
  } else {
2140
    const STag *pOldTag = (const STag *)ctbEntry.ctbEntry.pTags;
2,897✔
2141
    STag       *pNewTag = NULL;
2,897✔
2142
    SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
2,897✔
2143
    if (!pTagArray) {
2,897!
2144
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2145
      goto _err;
×
2146
    }
2147
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
9,722✔
2148
      SSchema *pCol = &pTagSchema->pSchema[i];
6,825✔
2149
      if (iCol == i) {
6,825✔
2150
        if (pAlterTbReq->isNull) {
2,897✔
2151
          continue;
256✔
2152
        }
2153
        STagVal val = {0};
2,641✔
2154
        val.type = pCol->type;
2,641✔
2155
        val.cid = pCol->colId;
2,641✔
2156
        if (IS_VAR_DATA_TYPE(pCol->type)) {
2,641!
2157
          val.pData = pAlterTbReq->pTagVal;
1,164✔
2158
          val.nData = pAlterTbReq->nTagVal;
1,164✔
2159
        } else {
2160
          memcpy(&val.i64, pAlterTbReq->pTagVal, pAlterTbReq->nTagVal);
1,477✔
2161
        }
2162
        if (taosArrayPush(pTagArray, &val) == NULL) {
2,641!
2163
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2164
          taosArrayDestroy(pTagArray);
×
2165
          goto _err;
×
2166
        }
2167
      } else {
2168
        STagVal val = {.cid = pCol->colId};
3,928✔
2169
        if (tTagGet(pOldTag, &val)) {
3,928✔
2170
          if (taosArrayPush(pTagArray, &val) == NULL) {
3,266!
2171
            terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2172
            taosArrayDestroy(pTagArray);
×
2173
            goto _err;
×
2174
          }
2175
        }
2176
      }
2177
    }
2178
    if ((terrno = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag)) < 0) {
2,897!
2179
      taosArrayDestroy(pTagArray);
×
2180
      goto _err;
×
2181
    }
2182
    ctbEntry.ctbEntry.pTags = (uint8_t *)pNewTag;
2,897✔
2183
    taosArrayDestroy(pTagArray);
2,897✔
2184
  }
2185

2186
  metaWLock(pMeta);
3,102✔
2187

2188
  // save to table.db
2189
  if (metaSaveToTbDb(pMeta, &ctbEntry) < 0) {
3,102!
2190
    metaError("meta/table: failed to save to tb db:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2191
  }
2192

2193
  // save to uid.idx
2194
  if (metaUpdateUidIdx(pMeta, &ctbEntry) < 0) {
3,102!
2195
    metaError("meta/table: failed to update uid idx:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2196
  }
2197

2198
  if (metaUpdateTagIdx(pMeta, &ctbEntry) < 0) {
3,102!
2199
    metaError("meta/table: failed to update tag idx:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2200
  }
2201

2202
  if (NULL == ctbEntry.ctbEntry.pTags) {
3,102!
2203
    metaError("meta/table: null tags, update tag val failed.");
×
2204
    goto _err;
×
2205
  }
2206

2207
  SCtbIdxKey ctbIdxKey = {.suid = ctbEntry.ctbEntry.suid, .uid = uid};
3,102✔
2208
  if (tdbTbUpsert(pMeta->pCtbIdx, &ctbIdxKey, sizeof(ctbIdxKey), ctbEntry.ctbEntry.pTags,
3,102!
2209
                  ((STag *)(ctbEntry.ctbEntry.pTags))->len, pMeta->txn) < 0) {
3,102✔
2210
    metaError("meta/table: failed to upsert ctb idx:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2211
  }
2212

2213
  if (metaUidCacheClear(pMeta, ctbEntry.ctbEntry.suid) < 0) {
3,102!
2214
    metaError("meta/table: failed to clear uid cache:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2215
  }
2216

2217
  if (metaTbGroupCacheClear(pMeta, ctbEntry.ctbEntry.suid) < 0) {
3,102!
2218
    metaError("meta/table: failed to clear group cache:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2219
  }
2220

2221
  if (metaUpdateChangeTime(pMeta, ctbEntry.uid, pAlterTbReq->ctimeMs) < 0) {
3,102!
2222
    metaError("meta/table: failed to update change time:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2223
  }
2224

2225
  metaULock(pMeta);
3,102✔
2226

2227
  tDecoderClear(&dc1);
3,102✔
2228
  tDecoderClear(&dc2);
3,102✔
2229
  taosMemoryFree((void *)ctbEntry.ctbEntry.pTags);
3,102✔
2230
  if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
3,102!
2231
  if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
3,102!
2232
  tdbTbcClose(pTbDbc);
3,102✔
2233
  tdbTbcClose(pUidIdxc);
3,102✔
2234
  return 0;
3,102✔
2235

2236
_err:
×
2237
  tDecoderClear(&dc1);
×
2238
  tDecoderClear(&dc2);
×
2239
  if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
×
2240
  if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
×
2241
  tdbTbcClose(pTbDbc);
×
2242
  tdbTbcClose(pUidIdxc);
×
2243
  return -1;
×
2244
}
2245

2246
static int metaUpdateTableOptions(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
109✔
2247
  void       *pVal = NULL;
109✔
2248
  int         nVal = 0;
109✔
2249
  const void *pData = NULL;
109✔
2250
  int         nData = 0;
109✔
2251
  int         ret = 0;
109✔
2252
  tb_uid_t    uid;
2253
  int64_t     oversion;
2254
  SMetaEntry  entry = {0};
109✔
2255
  int         c = 0;
109✔
2256

2257
  // search name index
2258
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
109✔
2259
  if (ret < 0) {
109!
2260
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2261
  }
2262

2263
  uid = *(tb_uid_t *)pVal;
109✔
2264
  tdbFree(pVal);
109✔
2265
  pVal = NULL;
109✔
2266

2267
  // search uid index
2268
  TBC *pUidIdxc = NULL;
109✔
2269

2270
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, NULL));
109!
2271
  if (tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c) < 0) {
109!
2272
    metaError("meta/table: failed to move to uid index, uid:%" PRId64, uid);
×
2273
  }
2274
  if (c != 0) {
109!
2275
    tdbTbcClose(pUidIdxc);
×
2276
    metaError("meta/table: invalide c: %" PRId32 " update tb options failed.", c);
×
2277
    return TSDB_CODE_FAILED;
×
2278
  }
2279

2280
  if (tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData) < 0) {
109!
2281
    metaError("meta/table: failed to get uid index, uid:%" PRId64, uid);
×
2282
  }
2283
  oversion = ((SUidIdxVal *)pData)[0].version;
109✔
2284

2285
  // search table.db
2286
  TBC *pTbDbc = NULL;
109✔
2287

2288
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pTbDb, &pTbDbc, NULL));
109!
2289
  if (tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c) < 0) {
109!
2290
    metaError("meta/table: failed to move to tb db, uid:%" PRId64, uid);
×
2291
  }
2292
  if (c != 0) {
109!
2293
    tdbTbcClose(pUidIdxc);
×
2294
    tdbTbcClose(pTbDbc);
×
2295
    metaError("meta/table: invalide c: %" PRId32 " update tb options failed.", c);
×
2296
    return TSDB_CODE_FAILED;
×
2297
  }
2298

2299
  if (tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData) < 0) {
109!
2300
    metaError("meta/table: failed to get tb db, uid:%" PRId64, uid);
×
2301
  }
2302

2303
  // get table entry
2304
  SDecoder dc = {0};
109✔
2305
  if ((entry.pBuf = taosMemoryMalloc(nData)) == NULL) {
109!
2306
    tdbTbcClose(pUidIdxc);
×
2307
    tdbTbcClose(pTbDbc);
×
2308
    return terrno;
×
2309
  }
2310
  memcpy(entry.pBuf, pData, nData);
109✔
2311
  tDecoderInit(&dc, entry.pBuf, nData);
109✔
2312
  ret = metaDecodeEntry(&dc, &entry);
109✔
2313
  if (ret != 0) {
109!
2314
    tDecoderClear(&dc);
×
2315
    tdbTbcClose(pUidIdxc);
×
2316
    tdbTbcClose(pTbDbc);
×
2317
    metaError("meta/table: invalide ret: %" PRId32 " alt tb options failed.", ret);
×
2318
    return TSDB_CODE_FAILED;
×
2319
  }
2320

2321
  entry.version = version;
109✔
2322
  metaWLock(pMeta);
109✔
2323
  // build SMetaEntry
2324
  if (entry.type == TSDB_CHILD_TABLE) {
109✔
2325
    if (pAlterTbReq->updateTTL) {
56✔
2326
      metaDeleteTtl(pMeta, &entry);
25✔
2327
      entry.ctbEntry.ttlDays = pAlterTbReq->newTTL;
25✔
2328
      metaUpdateTtl(pMeta, &entry);
25✔
2329
    }
2330
    if (pAlterTbReq->newCommentLen >= 0) {
56✔
2331
      entry.ctbEntry.commentLen = pAlterTbReq->newCommentLen;
31✔
2332
      entry.ctbEntry.comment = pAlterTbReq->newComment;
31✔
2333
    }
2334
  } else {
2335
    if (pAlterTbReq->updateTTL) {
53✔
2336
      metaDeleteTtl(pMeta, &entry);
22✔
2337
      entry.ntbEntry.ttlDays = pAlterTbReq->newTTL;
22✔
2338
      metaUpdateTtl(pMeta, &entry);
22✔
2339
    }
2340
    if (pAlterTbReq->newCommentLen >= 0) {
53✔
2341
      entry.ntbEntry.commentLen = pAlterTbReq->newCommentLen;
31✔
2342
      entry.ntbEntry.comment = pAlterTbReq->newComment;
31✔
2343
    }
2344
  }
2345

2346
  // save to table db
2347
  if (metaSaveToTbDb(pMeta, &entry) < 0) {
109!
2348
    metaError("meta/table: failed to save to tb db:%s uid:%" PRId64, entry.name, entry.uid);
×
2349
  }
2350

2351
  if (metaUpdateUidIdx(pMeta, &entry) < 0) {
109!
2352
    metaError("meta/table: failed to update uid idx:%s uid:%" PRId64, entry.name, entry.uid);
×
2353
  }
2354

2355
  if (metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs) < 0) {
109!
2356
    metaError("meta/table: failed to update change time:%s uid:%" PRId64, entry.name, entry.uid);
×
2357
  }
2358

2359
  metaULock(pMeta);
109✔
2360

2361
  tdbTbcClose(pTbDbc);
109✔
2362
  tdbTbcClose(pUidIdxc);
109✔
2363
  tDecoderClear(&dc);
109✔
2364
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
109!
2365
  return 0;
109✔
2366
}
2367

2368
static int metaAddTagIndex(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
×
2369
  SMetaEntry  stbEntry = {0};
×
2370
  void       *pVal = NULL;
×
2371
  int         nVal = 0;
×
2372
  int         ret;
2373
  int         c;
2374
  tb_uid_t    uid, suid;
2375
  int64_t     oversion;
2376
  const void *pData = NULL;
×
2377
  int         nData = 0;
×
2378
  SDecoder    dc = {0};
×
2379

2380
  if (pAlterTbReq->tagName == NULL) {
×
2381
    return terrno = TSDB_CODE_INVALID_MSG;
×
2382
  }
2383

2384
  // search name index
2385
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
×
2386
  if (ret < 0) {
×
2387
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2388
  } else {
2389
    uid = *(tb_uid_t *)pVal;
×
2390
    tdbFree(pVal);
×
2391
    pVal = NULL;
×
2392
  }
2393

2394
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(tb_uid_t), &pVal, &nVal) == -1) {
×
2395
    ret = -1;
×
2396
    goto _err;
×
2397
  }
2398
  suid = ((SUidIdxVal *)pVal)[0].suid;
×
2399

2400
  STbDbKey tbDbKey = {0};
×
2401
  tbDbKey.uid = suid;
×
2402
  tbDbKey.version = ((SUidIdxVal *)pVal)[0].version;
×
2403
  ret = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pVal, &nVal);
×
2404
  if (ret < 0) {
×
2405
    goto _err;
×
2406
  }
2407
  tDecoderInit(&dc, pVal, nVal);
×
2408
  ret = metaDecodeEntry(&dc, &stbEntry);
×
2409
  if (ret < 0) {
×
2410
    tDecoderClear(&dc);
×
2411
    goto _err;
×
2412
  }
2413

2414
  // Get target schema info
2415
  SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
×
2416
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
×
2417
    terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
×
2418
    goto _err;
×
2419
  }
2420
  SSchema *pCol = NULL;
×
2421
  int32_t  iCol = 0;
×
2422
  for (;;) {
2423
    pCol = NULL;
×
2424
    if (iCol >= pTagSchema->nCols) break;
×
2425
    pCol = &pTagSchema->pSchema[iCol];
×
2426
    if (strcmp(pCol->name, pAlterTbReq->tagName) == 0) break;
×
2427
    iCol++;
×
2428
  }
2429

2430
  if (iCol == 0) {
×
2431
    terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
×
2432
    goto _err;
×
2433
  }
2434
  if (pCol == NULL) {
×
2435
    terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
×
2436
    goto _err;
×
2437
  }
2438

2439
  /*
2440
   * iterator all pTdDbc by uid and version
2441
   */
2442
  TBC *pCtbIdxc = NULL;
×
2443
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL));
×
2444
  int rc = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c);
×
2445
  if (rc < 0) {
×
2446
    tdbTbcClose(pCtbIdxc);
×
2447
    goto _err;
×
2448
  }
2449
  for (;;) {
×
2450
    void *pKey, *pVal;
2451
    int   nKey, nVal;
2452
    rc = tdbTbcNext(pCtbIdxc, &pKey, &nKey, &pVal, &nVal);
×
2453
    if (rc < 0) break;
×
2454
    if (((SCtbIdxKey *)pKey)->suid != uid) {
×
2455
      tdbFree(pKey);
×
2456
      tdbFree(pVal);
×
2457
      continue;
×
2458
    }
2459
    STagIdxKey *pTagIdxKey = NULL;
×
2460
    int32_t     nTagIdxKey;
2461

2462
    const void *pTagData = NULL;
×
2463
    int32_t     nTagData = 0;
×
2464

2465
    STagVal tagVal = {.cid = pCol->colId};
×
2466
    if (tTagGet((const STag *)pVal, &tagVal)) {
×
2467
      if (IS_VAR_DATA_TYPE(pCol->type)) {
×
2468
        pTagData = tagVal.pData;
×
2469
        nTagData = (int32_t)tagVal.nData;
×
2470
      } else {
2471
        pTagData = &(tagVal.i64);
×
2472
        nTagData = tDataTypes[pCol->type].bytes;
×
2473
      }
2474
    } else {
2475
      if (!IS_VAR_DATA_TYPE(pCol->type)) {
×
2476
        nTagData = tDataTypes[pCol->type].bytes;
×
2477
      }
2478
    }
2479
    if (metaCreateTagIdxKey(suid, pCol->colId, pTagData, nTagData, pCol->type, uid, &pTagIdxKey, &nTagIdxKey) < 0) {
×
2480
      tdbFree(pKey);
×
2481
      tdbFree(pVal);
×
2482
      metaDestroyTagIdxKey(pTagIdxKey);
×
2483
      tdbTbcClose(pCtbIdxc);
×
2484
      goto _err;
×
2485
    }
2486
    ret = tdbTbUpsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn);
×
2487
    if (ret < 0) {
×
2488
      metaError("meta/table: failed to upsert tag idx:%s uid:%" PRId64, stbEntry.name, stbEntry.uid);
×
2489
    }
2490
    metaDestroyTagIdxKey(pTagIdxKey);
×
2491
    pTagIdxKey = NULL;
×
2492
  }
2493
  tdbTbcClose(pCtbIdxc);
×
2494
  return 0;
×
2495

2496
_err:
×
2497
  // tDecoderClear(&dc1);
2498
  // tDecoderClear(&dc2);
2499
  // if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
2500
  // if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
2501
  // tdbTbcClose(pTbDbc);
2502
  // tdbTbcClose(pUidIdxc);
2503
  return TSDB_CODE_FAILED;
×
2504
}
2505

2506
typedef struct SMetaPair {
2507
  void *key;
2508
  int   nkey;
2509
} SMetaPair;
2510

2511
static int metaDropTagIndex(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
×
2512
  SMetaEntry  stbEntry = {0};
×
2513
  void       *pVal = NULL;
×
2514
  int         nVal = 0;
×
2515
  int         ret;
2516
  int         c;
2517
  tb_uid_t    suid;
2518
  int64_t     oversion;
2519
  const void *pData = NULL;
×
2520
  int         nData = 0;
×
2521
  SDecoder    dc = {0};
×
2522

2523
  if (pAlterTbReq->tagName == NULL) {
×
2524
    return terrno = TSDB_CODE_INVALID_MSG;
×
2525
  }
2526

2527
  // search name index
2528
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
×
2529
  if (ret < 0) {
×
2530
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2531
  }
2532
  suid = *(tb_uid_t *)pVal;
×
2533
  tdbFree(pVal);
×
2534
  pVal = NULL;
×
2535

2536
  if (tdbTbGet(pMeta->pUidIdx, &suid, sizeof(tb_uid_t), &pVal, &nVal) == -1) {
×
2537
    ret = -1;
×
2538
    goto _err;
×
2539
  }
2540

2541
  STbDbKey tbDbKey = {0};
×
2542
  tbDbKey.uid = suid;
×
2543
  tbDbKey.version = ((SUidIdxVal *)pVal)[0].version;
×
2544
  ret = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pVal, &nVal);
×
2545
  if (ret < 0) {
×
2546
    goto _err;
×
2547
  }
2548

2549
  tDecoderInit(&dc, pVal, nVal);
×
2550
  ret = metaDecodeEntry(&dc, &stbEntry);
×
2551
  if (ret < 0) {
×
2552
    tDecoderClear(&dc);
×
2553
    goto _err;
×
2554
  }
2555

2556
  // Get targe schema info
2557
  SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
×
2558
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
×
2559
    terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
×
2560
    goto _err;
×
2561
  }
2562
  SSchema *pCol = NULL;
×
2563
  int32_t  iCol = 0;
×
2564
  for (;;) {
2565
    pCol = NULL;
×
2566
    if (iCol >= pTagSchema->nCols) break;
×
2567
    pCol = &pTagSchema->pSchema[iCol];
×
2568
    if (strcmp(pCol->name, pAlterTbReq->tagName) == 0) break;
×
2569
    iCol++;
×
2570
  }
2571
  if (iCol == 0) {
×
2572
    // cannot drop 1th tag index
2573
    terrno = -1;
×
2574
    goto _err;
×
2575
  }
2576
  if (pCol == NULL) {
×
2577
    terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
×
2578
    goto _err;
×
2579
  }
2580

2581
  if (IS_IDX_ON(pCol)) {
×
2582
    terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
×
2583
    goto _err;
×
2584
  }
2585

2586
  SArray *tagIdxList = taosArrayInit(512, sizeof(SMetaPair));
×
2587
  if (tagIdxList == NULL) {
×
2588
    goto _err;
×
2589
  }
2590

2591
  TBC *pTagIdxc = NULL;
×
2592
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pTagIdx, &pTagIdxc, NULL));
×
2593
  int rc =
2594
      tdbTbcMoveTo(pTagIdxc, &(STagIdxKey){.suid = suid, .cid = INT32_MIN, .type = pCol->type}, sizeof(STagIdxKey), &c);
×
2595
  for (;;) {
×
2596
    void *pKey, *pVal;
2597
    int   nKey, nVal;
2598
    rc = tdbTbcNext(pTagIdxc, &pKey, &nKey, &pVal, &nVal);
×
2599
    STagIdxKey *pIdxKey = (STagIdxKey *)pKey;
×
2600
    if (pIdxKey->suid != suid || pIdxKey->cid != pCol->colId) {
×
2601
      tdbFree(pKey);
×
2602
      tdbFree(pVal);
×
2603
      continue;
×
2604
    }
2605

2606
    SMetaPair pair = {.key = pKey, nKey = nKey};
×
2607
    if (taosArrayPush(tagIdxList, &pair) == NULL) {
×
2608
      goto _err;
×
2609
    }
2610
  }
2611
  tdbTbcClose(pTagIdxc);
2612

2613
  metaWLock(pMeta);
2614
  for (int i = 0; i < taosArrayGetSize(tagIdxList); i++) {
2615
    SMetaPair *pair = taosArrayGet(tagIdxList, i);
2616
    ret = tdbTbDelete(pMeta->pTagIdx, pair->key, pair->nkey, pMeta->txn);
2617
    if (ret < 0) {
2618
      metaError("meta/table: failed to delete tag idx:%s uid:%" PRId64, stbEntry.name, stbEntry.uid);
2619
    }
2620
  }
2621
  metaULock(pMeta);
2622

2623
  taosArrayDestroy(tagIdxList);
2624

2625
  // set pCol->flags; INDEX_ON
2626
  return 0;
2627
_err:
×
2628
  return TSDB_CODE_FAILED;
×
2629
}
2630
int32_t metaUpdateTableColCompress(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
5✔
2631
  // impl later
2632
  SMetaEntry  tbEntry = {0};
5✔
2633
  void       *pVal = NULL;
5✔
2634
  int         nVal = 0;
5✔
2635
  int         ret;
2636
  int         c;
2637
  tb_uid_t    suid;
2638
  int64_t     oversion;
2639
  const void *pData = NULL;
5✔
2640
  int         nData = 0;
5✔
2641
  SDecoder    dc = {0};
5✔
2642
  ret = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &pVal, &nVal);
5✔
2643
  if (ret < 0) {
5!
2644
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2645
  }
2646
  suid = *(tb_uid_t *)pVal;
5✔
2647
  tdbFree(pVal);
5✔
2648
  pVal = NULL;
5✔
2649

2650
  if (tdbTbGet(pMeta->pUidIdx, &suid, sizeof(tb_uid_t), &pVal, &nVal) == -1) {
5!
2651
    terrno = TSDB_CODE_INVALID_MSG;
×
2652
    ret = -1;
×
2653
    goto _err;
×
2654
  }
2655

2656
  STbDbKey tbDbKey = {0};
5✔
2657
  tbDbKey.uid = suid;
5✔
2658
  tbDbKey.version = ((SUidIdxVal *)pVal)[0].version;
5✔
2659
  if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pVal, &nVal) < 0) {
5!
2660
    terrno = TSDB_CODE_INVALID_MSG;
×
2661
    tdbFree(pVal);
×
2662
    goto _err;
×
2663
  }
2664

2665
  tDecoderInit(&dc, pVal, nVal);
5✔
2666
  ret = metaDecodeEntry(&dc, &tbEntry);
5✔
2667
  if (ret < 0) {
5!
2668
    terrno = TSDB_CODE_INVALID_MSG;
×
2669
    tdbFree(pVal);
×
2670
    tDecoderClear(&dc);
×
2671
    goto _err;
×
2672
  }
2673
  if (tbEntry.type != TSDB_NORMAL_TABLE && tbEntry.type != TSDB_SUPER_TABLE) {
5!
2674
    terrno = TSDB_CODE_INVALID_MSG;
×
2675
    tdbFree(pVal);
×
2676
    tDecoderClear(&dc);
×
2677
    goto _err;
×
2678
  }
2679
  int8_t           updated = 0;
5✔
2680
  SColCmprWrapper *wp = &tbEntry.colCmpr;
5✔
2681
  for (int32_t i = 0; i < wp->nCols; i++) {
40✔
2682
    SColCmpr *p = &wp->pColCmpr[i];
35✔
2683
    if (p->id == pReq->colId) {
35✔
2684
      uint32_t dst = 0;
5✔
2685
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
5✔
2686
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
2687
      if (updated > 0) {
5!
2688
        p->alg = dst;
5✔
2689
      }
2690
    }
2691
  }
2692
  if (updated == 0) {
5!
2693
    tdbFree(pVal);
×
2694
    tDecoderClear(&dc);
×
2695
    terrno = TSDB_CODE_VND_COLUMN_COMPRESS_ALREADY_EXIST;
×
2696
    goto _err;
×
2697
  } else if (updated < 0) {
5!
2698
    tdbFree(pVal);
×
2699
    tDecoderClear(&dc);
×
2700
    terrno = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
2701
    goto _err;
×
2702
  }
2703
  tbEntry.version = version;
5✔
2704

2705
  metaWLock(pMeta);
5✔
2706
  if (metaSaveToTbDb(pMeta, &tbEntry) < 0) {
5!
2707
    metaError("meta/table: failed to save to tb db:%s uid:%" PRId64, tbEntry.name, tbEntry.uid);
×
2708
  }
2709

2710
  if (metaUpdateUidIdx(pMeta, &tbEntry) < 0) {
5!
2711
    metaError("meta/table: failed to update uid idx:%s uid:%" PRId64, tbEntry.name, tbEntry.uid);
×
2712
  }
2713

2714
  if (metaUpdateChangeTime(pMeta, suid, pReq->ctimeMs) < 0) {
5!
2715
    metaError("meta/table: failed to update change time:%s uid:%" PRId64, tbEntry.name, tbEntry.uid);
×
2716
  }
2717

2718
  metaULock(pMeta);
5✔
2719

2720
  tdbFree(pVal);
5✔
2721
  tDecoderClear(&dc);
5✔
2722

2723
  return 0;
5✔
2724
_err:
×
2725
  return TSDB_CODE_FAILED;
×
2726
}
2727

2728
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
5,190✔
2729
  pMeta->changed = true;
5,190✔
2730
  switch (pReq->action) {
5,190!
2731
    case TSDB_ALTER_TABLE_ADD_COLUMN:
1,973✔
2732
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
2733
    case TSDB_ALTER_TABLE_DROP_COLUMN:
2734
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
2735
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
2736
      return metaAlterTableColumn(pMeta, version, pReq, pMetaRsp);
1,973✔
2737
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
3,103✔
2738
      return metaUpdateTableTagVal(pMeta, version, pReq);
3,103✔
2739
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
109✔
2740
      return metaUpdateTableOptions(pMeta, version, pReq);
109✔
2741
    case TSDB_ALTER_TABLE_ADD_TAG_INDEX:
×
2742
      return metaAddTagIndex(pMeta, version, pReq);
×
2743
    case TSDB_ALTER_TABLE_DROP_TAG_INDEX:
×
2744
      return metaDropTagIndex(pMeta, version, pReq);
×
2745
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
5✔
2746
      return metaUpdateTableColCompress(pMeta, version, pReq);
5✔
2747
    default:
×
2748
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
2749
      break;
2750
  }
2751
}
2752

2753
static int metaSaveToTbDb(SMeta *pMeta, const SMetaEntry *pME) {
291,384✔
2754
  STbDbKey tbDbKey;
2755
  void    *pKey = NULL;
291,384✔
2756
  void    *pVal = NULL;
291,384✔
2757
  int      kLen = 0;
291,384✔
2758
  int      vLen = 0;
291,384✔
2759
  SEncoder coder = {0};
291,384✔
2760

2761
  // set key and value
2762
  tbDbKey.version = pME->version;
291,384✔
2763
  tbDbKey.uid = pME->uid;
291,384✔
2764

2765
  metaDebug("vgId:%d, start to save table version:%" PRId64 " uid:%" PRId64, TD_VID(pMeta->pVnode), pME->version,
291,384✔
2766
            pME->uid);
2767

2768
  pKey = &tbDbKey;
291,397✔
2769
  kLen = sizeof(tbDbKey);
291,397✔
2770

2771
  int32_t ret = 0;
291,397✔
2772
  tEncodeSize(metaEncodeEntry, pME, vLen, ret);
291,397!
2773
  if (ret < 0) {
291,183!
2774
    goto _err;
×
2775
  }
2776

2777
  pVal = taosMemoryMalloc(vLen);
291,183✔
2778
  if (pVal == NULL) {
291,310!
2779
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2780
    goto _err;
×
2781
  }
2782

2783
  tEncoderInit(&coder, pVal, vLen);
291,310✔
2784

2785
  if (metaEncodeEntry(&coder, pME) < 0) {
291,289!
2786
    goto _err;
×
2787
  }
2788

2789
  tEncoderClear(&coder);
291,370✔
2790

2791
  // write to table.db
2792
  if (tdbTbInsert(pMeta->pTbDb, pKey, kLen, pVal, vLen, pMeta->txn) < 0) {
291,442!
2793
    goto _err;
×
2794
  }
2795

2796
  taosMemoryFree(pVal);
291,451✔
2797
  return 0;
291,409✔
2798

2799
_err:
×
2800
  metaError("vgId:%d, failed to save table version:%" PRId64 "uid:%" PRId64 " %s", TD_VID(pMeta->pVnode), pME->version,
×
2801
            pME->uid, tstrerror(terrno));
2802

2803
  taosMemoryFree(pVal);
×
2804
  return TSDB_CODE_FAILED;
×
2805
}
2806

2807
static int metaUpdateUidIdx(SMeta *pMeta, const SMetaEntry *pME) {
291,328✔
2808
  // upsert cache
2809
  SMetaInfo info;
2810
  metaGetEntryInfo(pME, &info);
291,328✔
2811
  int32_t ret = metaCacheUpsert(pMeta, &info);
291,395✔
2812
  if (ret < 0) {
291,398!
2813
    metaError("vgId:%d, failed to upsert cache, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pME->uid, tstrerror(ret));
×
2814
  }
2815

2816
  SUidIdxVal uidIdxVal = {.suid = info.suid, .version = info.version, .skmVer = info.skmVer};
291,398✔
2817

2818
  return tdbTbUpsert(pMeta->pUidIdx, &pME->uid, sizeof(tb_uid_t), &uidIdxVal, sizeof(uidIdxVal), pMeta->txn);
291,398✔
2819
}
2820

2821
static int metaUpdateSuidIdx(SMeta *pMeta, const SMetaEntry *pME) {
30,439✔
2822
  return tdbTbUpsert(pMeta->pSuidIdx, &pME->uid, sizeof(tb_uid_t), NULL, 0, pMeta->txn);
30,439✔
2823
}
2824

2825
static int metaUpdateNameIdx(SMeta *pMeta, const SMetaEntry *pME) {
279,112✔
2826
  return tdbTbUpsert(pMeta->pNameIdx, pME->name, strlen(pME->name) + 1, &pME->uid, sizeof(tb_uid_t), pMeta->txn);
279,112✔
2827
}
2828

2829
static void metaUpdateTtl(SMeta *pMeta, const SMetaEntry *pME) {
248,753✔
2830
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
248,753!
2831

2832
  STtlUpdTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
248,753✔
2833
  if (pME->type == TSDB_CHILD_TABLE) {
248,753✔
2834
    ctx.ttlDays = pME->ctbEntry.ttlDays;
233,549✔
2835
    ctx.changeTimeMs = pME->ctbEntry.btime;
233,549✔
2836
  } else {
2837
    ctx.ttlDays = pME->ntbEntry.ttlDays;
15,204✔
2838
    ctx.changeTimeMs = pME->ntbEntry.btime;
15,204✔
2839
  }
2840

2841
  int32_t ret = ttlMgrInsertTtl(pMeta->pTtlMgr, &ctx);
248,753✔
2842
  if (ret < 0) {
248,739!
2843
    metaError("vgId:%d, failed to insert ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pME->uid, tstrerror(ret));
×
2844
  }
2845

2846
  return;
248,741✔
2847
}
2848

2849
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
4,259✔
2850
  if (!tsTtlChangeOnWrite) return 0;
4,259✔
2851

2852
  if (changeTimeMs <= 0) {
1!
2853
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
×
2854
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
×
2855
  }
2856

2857
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
1✔
2858

2859
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
1✔
2860
}
2861

2862
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
13,990,141✔
2863
  if (!tsTtlChangeOnWrite) return 0;
13,990,141!
2864

2865
  metaWLock(pMeta);
×
2866
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
1✔
2867
  metaULock(pMeta);
1✔
2868
  return ret;
1✔
2869
}
2870

2871
static int metaUpdateCtbIdx(SMeta *pMeta, const SMetaEntry *pME) {
233,518✔
2872
  SCtbIdxKey ctbIdxKey = {.suid = pME->ctbEntry.suid, .uid = pME->uid};
233,518✔
2873

2874
  return tdbTbUpsert(pMeta->pCtbIdx, &ctbIdxKey, sizeof(ctbIdxKey), pME->ctbEntry.pTags,
467,051✔
2875
                     ((STag *)(pME->ctbEntry.pTags))->len, pMeta->txn);
233,518✔
2876
}
2877

2878
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
290,798✔
2879
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
2880
  if (IS_VAR_DATA_TYPE(type)) {
290,798✔
2881
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
39,109✔
2882
  } else {
2883
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
251,689✔
2884
  }
2885

2886
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
290,798✔
2887
  if (*ppTagIdxKey == NULL) {
290,823!
2888
    return terrno;
×
2889
  }
2890

2891
  (*ppTagIdxKey)->suid = suid;
290,830✔
2892
  (*ppTagIdxKey)->cid = cid;
290,830✔
2893
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
290,830✔
2894
  (*ppTagIdxKey)->type = type;
290,830✔
2895

2896
  // refactor
2897
  if (IS_VAR_DATA_TYPE(type)) {
290,830!
2898
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
39,139✔
2899
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
39,139✔
2900
    *(tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData) = uid;
39,139✔
2901
  } else {
2902
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
251,691✔
2903
    *(tb_uid_t *)((*ppTagIdxKey)->data + nTagData) = uid;
251,691✔
2904
  }
2905

2906
  return 0;
290,830✔
2907
}
2908

2909
static void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
290,402✔
2910
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
290,402!
2911
}
290,390✔
2912

2913
static int metaUpdateTagIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry) {
236,626✔
2914
  void          *pData = NULL;
236,626✔
2915
  int            nData = 0;
236,626✔
2916
  STbDbKey       tbDbKey = {0};
236,626✔
2917
  SMetaEntry     stbEntry = {0};
236,626✔
2918
  STagIdxKey    *pTagIdxKey = NULL;
236,626✔
2919
  int32_t        nTagIdxKey;
2920
  const SSchema *pTagColumn;
2921
  const void    *pTagData = NULL;
236,626✔
2922
  int32_t        nTagData = 0;
236,626✔
2923
  SDecoder       dc = {0};
236,626✔
2924
  int32_t        ret = 0;
236,626✔
2925
  // get super table
2926
  if (tdbTbGet(pMeta->pUidIdx, &pCtbEntry->ctbEntry.suid, sizeof(tb_uid_t), &pData, &nData) != 0) {
236,626!
2927
    metaError("vgId:%d, failed to get stable suid for update. version:%" PRId64, TD_VID(pMeta->pVnode),
×
2928
              pCtbEntry->version);
2929
    ret = TSDB_CODE_TDB_INVALID_TABLE_ID;
×
2930
    goto end;
×
2931
  }
2932
  tbDbKey.uid = pCtbEntry->ctbEntry.suid;
236,626✔
2933
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
236,626✔
2934
  ret = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData);
236,626✔
2935
  if (ret < 0) {
236,636!
2936
    metaError("vgId:%d, failed to get stable for update. version:%" PRId64, TD_VID(pMeta->pVnode), pCtbEntry->version);
×
2937
    goto end;
×
2938
  }
2939

2940
  tDecoderInit(&dc, pData, nData);
236,636✔
2941
  ret = metaDecodeEntry(&dc, &stbEntry);
236,616✔
2942
  if (ret < 0) {
236,561!
2943
    goto end;
×
2944
  }
2945

2946
  if (stbEntry.stbEntry.schemaTag.pSchema == NULL) {
236,561!
2947
    ret = TSDB_CODE_INVALID_PARA;
×
2948
    goto end;
×
2949
  }
2950

2951
  SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
236,561✔
2952
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
236,561✔
2953
    pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
576✔
2954
    STagVal tagVal = {.cid = pTagColumn->colId};
576✔
2955

2956
    pTagData = pCtbEntry->ctbEntry.pTags;
576✔
2957
    nTagData = ((const STag *)pCtbEntry->ctbEntry.pTags)->len;
576✔
2958
    ret = metaSaveJsonVarToIdx(pMeta, pCtbEntry, pTagColumn);
576✔
2959
    goto end;
576✔
2960
  } else {
2961
    for (int i = 0; i < pTagSchema->nCols; i++) {
846,434✔
2962
      pTagData = NULL;
610,395✔
2963
      nTagData = 0;
610,395✔
2964
      pTagColumn = &pTagSchema->pSchema[i];
610,395✔
2965
      if (!IS_IDX_ON(pTagColumn)) continue;
610,395✔
2966

2967
      STagVal tagVal = {.cid = pTagColumn->colId};
235,964✔
2968
      if (tTagGet((const STag *)pCtbEntry->ctbEntry.pTags, &tagVal)) {
235,964✔
2969
        if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
234,844!
2970
          pTagData = tagVal.pData;
26,820✔
2971
          nTagData = (int32_t)tagVal.nData;
26,820✔
2972
        } else {
2973
          pTagData = &(tagVal.i64);
208,024✔
2974
          nTagData = tDataTypes[pTagColumn->type].bytes;
208,024✔
2975
        }
2976
      } else {
2977
        if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
1,163!
2978
          nTagData = tDataTypes[pTagColumn->type].bytes;
899✔
2979
        }
2980
      }
2981
      if (metaCreateTagIdxKey(pCtbEntry->ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type,
236,002!
2982
                              pCtbEntry->uid, &pTagIdxKey, &nTagIdxKey) < 0) {
236,007✔
2983
        ret = -1;
×
2984
        goto end;
×
2985
      }
2986
      if (tdbTbUpsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn) < 0) {
236,002!
2987
        metaError("vgId:%d, failed to update tag index. version:%" PRId64, TD_VID(pMeta->pVnode), pCtbEntry->version);
×
2988
      }
2989
      metaDestroyTagIdxKey(pTagIdxKey);
236,031✔
2990
      pTagIdxKey = NULL;
236,018✔
2991
    }
2992
  }
2993
end:
236,039✔
2994
  tDecoderClear(&dc);
236,615✔
2995
  tdbFree(pData);
236,600✔
2996
  return ret;
236,622✔
2997
}
2998

2999
static int metaSaveToSkmDb(SMeta *pMeta, const SMetaEntry *pME) {
49,592✔
3000
  SEncoder              coder = {0};
49,592✔
3001
  void                 *pVal = NULL;
49,592✔
3002
  int                   vLen = 0;
49,592✔
3003
  int                   rcode = 0;
49,592✔
3004
  SSkmDbKey             skmDbKey = {0};
49,592✔
3005
  const SSchemaWrapper *pSW;
3006

3007
  if (pME->type == TSDB_SUPER_TABLE) {
49,592✔
3008
    pSW = &pME->stbEntry.schemaRow;
33,421✔
3009
  } else if (pME->type == TSDB_NORMAL_TABLE) {
16,171!
3010
    pSW = &pME->ntbEntry.schemaRow;
16,226✔
3011
  } else {
3012
    metaError("meta/table: invalide table type: %" PRId8 " save skm db failed.", pME->type);
×
3013
    return TSDB_CODE_FAILED;
×
3014
  }
3015

3016
  skmDbKey.uid = pME->uid;
49,647✔
3017
  skmDbKey.sver = pSW->version;
49,647✔
3018

3019
  // if receive tmq meta message is: create stable1 then delete stable1 then create stable1 with multi vgroups
3020
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(skmDbKey), NULL, NULL) == 0) {
49,647✔
3021
    return rcode;
10✔
3022
  }
3023

3024
  // encode schema
3025
  int32_t ret = 0;
49,654✔
3026
  tEncodeSize(tEncodeSSchemaWrapper, pSW, vLen, ret);
99,300✔
3027
  if (ret < 0) return -1;
49,570!
3028
  pVal = taosMemoryMalloc(vLen);
49,570✔
3029
  if (pVal == NULL) {
49,653!
3030
    rcode = -1;
×
3031
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3032
    goto _exit;
×
3033
  }
3034

3035
  tEncoderInit(&coder, pVal, vLen);
49,653✔
3036
  ret = tEncodeSSchemaWrapper(&coder, pSW);
49,663✔
3037
  if (ret < 0) {
49,663!
3038
    rcode = -1;
×
3039
    goto _exit;
×
3040
  }
3041

3042
  if (tdbTbInsert(pMeta->pSkmDb, &skmDbKey, sizeof(skmDbKey), pVal, vLen, pMeta->txn) < 0) {
49,663!
3043
    rcode = -1;
×
3044
    goto _exit;
×
3045
  }
3046

3047
  metaDebug("vgId:%d, set schema:(%" PRId64 ") sver:%d since %s", TD_VID(pMeta->pVnode), pME->uid, pSW->version,
49,674✔
3048
            tstrerror(terrno));
3049

3050
_exit:
38,402✔
3051
  taosMemoryFree(pVal);
49,679✔
3052
  tEncoderClear(&coder);
49,661✔
3053
  return rcode;
49,654✔
3054
}
3055

3056
int metaHandleEntry(SMeta *pMeta, const SMetaEntry *pME) {
278,748✔
3057
  int32_t code = 0;
278,748✔
3058
  int32_t line = 0;
278,748✔
3059
  metaWLock(pMeta);
278,748✔
3060

3061
  // save to table.db
3062
  code = metaSaveToTbDb(pMeta, pME);
279,190✔
3063
  VND_CHECK_CODE(code, line, _err);
279,080!
3064

3065
  // update uid.idx
3066
  code = metaUpdateUidIdx(pMeta, pME);
279,080✔
3067
  VND_CHECK_CODE(code, line, _err);
279,183!
3068

3069
  // update name.idx
3070
  code = metaUpdateNameIdx(pMeta, pME);
279,183✔
3071
  VND_CHECK_CODE(code, line, _err);
279,199!
3072

3073
  if (pME->type == TSDB_CHILD_TABLE) {
279,199✔
3074
    // update ctb.idx
3075
    code = metaUpdateCtbIdx(pMeta, pME);
233,525✔
3076
    VND_CHECK_CODE(code, line, _err);
233,532!
3077

3078
    // update tag.idx
3079
    code = metaUpdateTagIdx(pMeta, pME);
233,532✔
3080
    VND_CHECK_CODE(code, line, _err);
233,503!
3081
  } else {
3082
    // update schema.db
3083
    code = metaSaveToSkmDb(pMeta, pME);
45,674✔
3084
    VND_CHECK_CODE(code, line, _err);
45,624!
3085

3086
    if (pME->type == TSDB_SUPER_TABLE) {
45,624✔
3087
      code = metaUpdateSuidIdx(pMeta, pME);
30,466✔
3088
      VND_CHECK_CODE(code, line, _err);
30,459!
3089
    }
3090
  }
3091

3092
  code = metaUpdateBtimeIdx(pMeta, pME);
279,120✔
3093
  VND_CHECK_CODE(code, line, _err);
279,112!
3094

3095
  if (pME->type == TSDB_NORMAL_TABLE) {
279,112✔
3096
    code = metaUpdateNcolIdx(pMeta, pME);
15,184✔
3097
    VND_CHECK_CODE(code, line, _err);
15,184!
3098
  }
3099

3100
  if (pME->type != TSDB_SUPER_TABLE) {
279,112✔
3101
    metaUpdateTtl(pMeta, pME);
248,712✔
3102
  }
3103

3104
  if (pME->type == TSDB_SUPER_TABLE || pME->type == TSDB_NORMAL_TABLE) {
279,095✔
3105
  }
3106

3107
  metaULock(pMeta);
279,095✔
3108
  metaDebug("vgId:%d, handle meta entry, ver:%" PRId64 ", uid:%" PRId64 ", name:%s", TD_VID(pMeta->pVnode),
279,152✔
3109
            pME->version, pME->uid, pME->name);
3110
  return 0;
279,141✔
3111

3112
_err:
×
3113
  metaULock(pMeta);
×
3114
  metaError("vgId:%d, failed to handle meta entry since %s at line:%d, ver:%" PRId64 ", uid:%" PRId64 ", name:%s",
×
3115
            TD_VID(pMeta->pVnode), terrstr(), line, pME->version, pME->uid, pME->name);
3116
  return TSDB_CODE_FAILED;
×
3117
}
3118

3119
static void colCompressDebug(SHashObj *pColCmprObj) {
442,856✔
3120
  void *p = taosHashIterate(pColCmprObj, NULL);
442,856✔
3121
  while (p) {
3,846,683✔
3122
    uint32_t cmprAlg = *(uint32_t *)p;
3,404,002✔
3123
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
3,404,002✔
3124
    p = taosHashIterate(pColCmprObj, p);
3,403,961✔
3125

3126
    uint8_t l1, l2, lvl;
3127
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
3,404,501✔
3128

3129
    const char *l1str = columnEncodeStr(l1);
3,404,403✔
3130
    const char *l2str = columnCompressStr(l2);
3,404,114✔
3131
    const char *lvlstr = columnLevelStr(lvl);
3,403,962✔
3132
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
3,403,819✔
3133
  }
3134
  return;
442,681✔
3135
}
3136
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
442,858✔
3137
  int rc = 0;
442,858✔
3138

3139
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
442,858✔
3140
  if (pColCmprObj == NULL) {
442,866!
3141
    pColCmprObj = NULL;
×
3142
    return TSDB_CODE_OUT_OF_MEMORY;
×
3143
  }
3144

3145
  void      *pData = NULL;
442,866✔
3146
  int        nData = 0;
442,866✔
3147
  SMetaEntry e = {0};
442,866✔
3148
  SDecoder   dc = {0};
442,866✔
3149

3150
  *ppColCmprObj = NULL;
442,866✔
3151

3152
  metaRLock(pMeta);
442,866✔
3153
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
442,890✔
3154
  if (rc < 0) {
442,886✔
3155
    taosHashClear(pColCmprObj);
25✔
3156
    metaULock(pMeta);
25✔
3157
    return TSDB_CODE_FAILED;
25✔
3158
  }
3159
  int64_t version = ((SUidIdxVal *)pData)[0].version;
442,861✔
3160
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
442,861✔
3161
  if (rc < 0) {
442,849!
3162
    metaULock(pMeta);
×
3163
    taosHashClear(pColCmprObj);
×
3164
    metaError("failed to get table entry");
×
3165
    return rc;
×
3166
  }
3167

3168
  tDecoderInit(&dc, pData, nData);
442,849✔
3169
  rc = metaDecodeEntry(&dc, &e);
442,836✔
3170
  if (rc < 0) {
442,809!
3171
    tDecoderClear(&dc);
×
3172
    tdbFree(pData);
×
3173
    metaULock(pMeta);
×
3174
    taosHashClear(pColCmprObj);
×
3175
    return rc;
×
3176
  }
3177
  if (useCompress(e.type)) {
442,809!
3178
    SColCmprWrapper *p = &e.colCmpr;
442,809✔
3179
    for (int32_t i = 0; i < p->nCols; i++) {
3,846,880✔
3180
      SColCmpr *pCmpr = &p->pColCmpr[i];
3,403,884✔
3181
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
3,403,884✔
3182
      if (rc < 0) {
3,404,101✔
3183
        tDecoderClear(&dc);
30✔
3184
        tdbFree(pData);
×
3185
        metaULock(pMeta);
×
3186
        taosHashClear(pColCmprObj);
×
3187
        return rc;
×
3188
      }
3189
    }
3190
  } else {
3191
    tDecoderClear(&dc);
×
3192
    tdbFree(pData);
×
3193
    metaULock(pMeta);
×
3194
    taosHashClear(pColCmprObj);
×
3195
    return 0;
×
3196
  }
3197
  tDecoderClear(&dc);
442,996✔
3198
  tdbFree(pData);
442,826✔
3199
  metaULock(pMeta);
442,849✔
3200

3201
  *ppColCmprObj = pColCmprObj;
442,859✔
3202
  colCompressDebug(pColCmprObj);
442,859✔
3203

3204
  return 0;
442,725✔
3205
}
3206
// refactor later
3207
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
39,104✔
3208
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
39,079✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc