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

taosdata / TDengine / #4847

11 Nov 2025 05:50AM UTC coverage: 62.651% (+0.3%) from 62.306%
#4847

push

travis-ci

web-flow
Merge e78cd6509 into 47a2ea7a0

542 of 650 new or added lines in 16 files covered. (83.38%)

1515 existing lines in 91 files now uncovered.

113826 of 181682 relevant lines covered (62.65%)

113230552.12 hits per line

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

80.35
/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
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
21
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
22
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
23
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
24
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq);
25
int32_t metaUpdateTableMultiTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq);
26
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq);
27
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq);
28
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
29
int32_t metaRemoveTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
30
int32_t metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema);
31

32
int32_t    metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema);
33
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs);
34
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl);
35
void       metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey);
36
// opt ins_tables query
37
static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME);
38
static int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME);
39

40
int32_t updataTableColCmpr(SColCmprWrapper *pWp, SSchema *pSchema, int8_t add, uint32_t compress) {
3,197,979✔
41
  int32_t nCols = pWp->nCols;
3,197,979✔
42
  int32_t ver = pWp->version;
3,197,979✔
43
  if (add) {
3,197,979✔
44
    SColCmpr *p = taosMemoryRealloc(pWp->pColCmpr, sizeof(SColCmpr) * (nCols + 1));
3,141,143✔
45
    if (p == NULL) {
3,141,143✔
46
      return terrno;
×
47
    }
48
    pWp->pColCmpr = p;
3,141,143✔
49

50
    SColCmpr *pCol = p + nCols;
3,141,143✔
51
    pCol->id = pSchema->colId;
3,141,143✔
52
    pCol->alg = compress;
3,141,143✔
53
    pWp->nCols = nCols + 1;
3,141,143✔
54
    pWp->version = ver;
3,141,143✔
55
  } else {
56
    for (int32_t i = 0; i < nCols; i++) {
19,767,975✔
57
      SColCmpr *pOCmpr = &pWp->pColCmpr[i];
19,767,975✔
58
      if (pOCmpr->id == pSchema->colId) {
19,767,975✔
59
        int32_t left = (nCols - i - 1) * sizeof(SColCmpr);
56,836✔
60
        if (left) {
56,836✔
61
          memmove(pWp->pColCmpr + i, pWp->pColCmpr + i + 1, left);
34,661✔
62
        }
63
        nCols--;
56,836✔
64
        break;
56,836✔
65
      }
66
    }
67
    pWp->nCols = nCols;
56,836✔
68
    pWp->version = ver;
56,836✔
69
  }
70
  return 0;
3,197,979✔
71
}
72

73
int32_t addTableExtSchema(SMetaEntry *pEntry, const SSchema *pColumn, int32_t newColNum, SExtSchema *pExtSchema) {
3,192,900✔
74
  // no need to add ext schema when no column needs ext schemas
75
  if (!HAS_TYPE_MOD(pColumn) && !pEntry->pExtSchemas) return 0;
3,192,900✔
76
  if (!pEntry->pExtSchemas) {
18,847✔
77
    // add a column which needs ext schema
78
    // set all extschemas to zero for all columns alrady existed
79
    pEntry->pExtSchemas = (SExtSchema *)taosMemoryCalloc(newColNum, sizeof(SExtSchema));
4,317✔
80
  } else {
81
    // already has columns with ext schema
82
    pEntry->pExtSchemas = (SExtSchema *)taosMemoryRealloc(pEntry->pExtSchemas, sizeof(SExtSchema) * newColNum);
14,530✔
83
  }
84
  if (!pEntry->pExtSchemas) return terrno;
18,847✔
85
  pEntry->pExtSchemas[newColNum - 1] = *pExtSchema;
18,847✔
86
  return 0;
18,847✔
87
}
88

89
int32_t dropTableExtSchema(SMetaEntry *pEntry, int32_t dropColId, int32_t newColNum) {
85,871✔
90
  // no ext schema, no need to drop
91
  if (!pEntry->pExtSchemas) return 0;
85,871✔
92
  if (dropColId == newColNum) {
14,495✔
93
    // drop the last column
94
    pEntry->pExtSchemas[dropColId - 1] = (SExtSchema){0};
5,791✔
95
  } else {
96
    // drop a column in the middle
97
    memmove(pEntry->pExtSchemas + dropColId, pEntry->pExtSchemas + dropColId + 1,
8,704✔
98
            (newColNum - dropColId) * sizeof(SExtSchema));
8,704✔
99
  }
100
  for (int32_t i = 0; i < newColNum; i++) {
63,526✔
101
    if (hasExtSchema(pEntry->pExtSchemas + i)) return 0;
60,648✔
102
  }
103
  taosMemoryFreeClear(pEntry->pExtSchemas);
2,878✔
104
  return 0;
2,878✔
105
}
106

107
int32_t updataTableColRef(SColRefWrapper *pWp, const SSchema *pSchema, int8_t add, SColRef *pColRef) {
90,152✔
108
  int32_t nCols = pWp->nCols;
90,152✔
109
  if (add) {
90,152✔
110
    SColRef *p = taosMemoryRealloc(pWp->pColRef, sizeof(SColRef) * (nCols + 1));
57,353✔
111
    if (p == NULL) {
57,353✔
112
      return terrno;
×
113
    }
114
    pWp->pColRef = p;
57,353✔
115

116
    SColRef *pCol = p + nCols;
57,353✔
117
    if (NULL == pColRef) {
57,353✔
118
      pCol->hasRef = false;
5,596✔
119
      pCol->id = pSchema->colId;
5,596✔
120
    } else {
121
      pCol->hasRef = pColRef->hasRef;
51,757✔
122
      pCol->id = pSchema->colId;
51,757✔
123
      if (pCol->hasRef) {
51,757✔
124
        tstrncpy(pCol->refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
21,252✔
125
        tstrncpy(pCol->refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
21,252✔
126
        tstrncpy(pCol->refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
21,252✔
127
      }
128
    }
129
    pWp->nCols = nCols + 1;
57,353✔
130
    pWp->version++;
57,353✔
131
  } else {
132
    for (int32_t i = 0; i < nCols; i++) {
128,021✔
133
      SColRef *pOColRef = &pWp->pColRef[i];
128,021✔
134
      if (pOColRef->id == pSchema->colId) {
128,021✔
135
        int32_t left = (nCols - i - 1) * sizeof(SColRef);
32,799✔
136
        if (left) {
32,799✔
137
          memmove(pWp->pColRef + i, pWp->pColRef + i + 1, left);
31,511✔
138
        }
139
        nCols--;
32,799✔
140
        break;
32,799✔
141
      }
142
    }
143
    pWp->nCols = nCols;
32,799✔
144
    pWp->version++;
32,799✔
145
  }
146
  return 0;
90,152✔
147
}
148

149
int metaUpdateMetaRsp(tb_uid_t uid, char *tbName, SSchemaWrapper *pSchema, STableMetaRsp *pMetaRsp) {
5,762,727✔
150
  pMetaRsp->pSchemas = taosMemoryMalloc(pSchema->nCols * sizeof(SSchema));
5,762,727✔
151
  if (NULL == pMetaRsp->pSchemas) {
5,762,727✔
152
    return terrno;
×
153
  }
154

155
  pMetaRsp->pSchemaExt = taosMemoryCalloc(1, pSchema->nCols * sizeof(SSchemaExt));
5,762,727✔
156
  if (pMetaRsp->pSchemaExt == NULL) {
5,762,727✔
157
    taosMemoryFree(pMetaRsp->pSchemas);
×
158
    return terrno;
×
159
  }
160

161
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
5,762,727✔
162
  pMetaRsp->numOfColumns = pSchema->nCols;
5,762,727✔
163
  pMetaRsp->tableType = TSDB_NORMAL_TABLE;
5,762,727✔
164
  pMetaRsp->sversion = pSchema->version;
5,762,727✔
165
  pMetaRsp->rversion = 1;
5,762,727✔
166
  pMetaRsp->tuid = uid;
5,762,727✔
167
  pMetaRsp->virtualStb = false; // super table will never be processed here
5,762,727✔
168

169
  memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
5,762,727✔
170

171
  return 0;
5,762,727✔
172
}
173

174
int32_t metaUpdateVtbMetaRsp(SMetaEntry *pEntry, char *tbName, SSchemaWrapper *pSchema, SColRefWrapper *pRef,
573,852✔
175
                             STableMetaRsp *pMetaRsp, int8_t tableType) {
176
  int32_t code = TSDB_CODE_SUCCESS;
573,852✔
177
  if (!pRef) {
573,852✔
178
    return TSDB_CODE_INVALID_PARA;
×
179
  }
180
  if (pSchema) {
573,852✔
181
    pMetaRsp->pSchemas = taosMemoryMalloc(pSchema->nCols * sizeof(SSchema));
383,609✔
182
    if (NULL == pMetaRsp->pSchemas) {
383,609✔
183
      code = terrno;
×
184
      goto _return;
×
185
    }
186

187
    pMetaRsp->pSchemaExt = taosMemoryMalloc(pSchema->nCols * sizeof(SSchemaExt));
383,609✔
188
    if (pMetaRsp->pSchemaExt == NULL) {
383,609✔
189
      code = terrno;
×
190
      goto _return;
×
191
    }
192

193
    pMetaRsp->numOfColumns = pSchema->nCols;
383,609✔
194
    pMetaRsp->sversion = pSchema->version;
383,609✔
195
    memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
383,609✔
196
  }
197
  pMetaRsp->pColRefs = taosMemoryMalloc(pRef->nCols * sizeof(SColRef));
573,852✔
198
  if (NULL == pMetaRsp->pColRefs) {
573,852✔
199
    code = terrno;
×
200
    goto _return;
×
201
  }
202
  memcpy(pMetaRsp->pColRefs, pRef->pColRef, pRef->nCols * sizeof(SColRef));
573,852✔
203
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
573,852✔
204
  if (tableType == TSDB_VIRTUAL_NORMAL_TABLE) {
573,852✔
205
    pMetaRsp->tuid = pEntry->uid;
326,407✔
206
  } else if (tableType == TSDB_VIRTUAL_CHILD_TABLE) {
247,445✔
207
    pMetaRsp->tuid = pEntry->uid;
247,445✔
208
    pMetaRsp->suid = pEntry->ctbEntry.suid;
247,445✔
209
  }
210

211
  pMetaRsp->tableType = tableType;
573,852✔
212
  pMetaRsp->virtualStb = false; // super table will never be processed here
573,852✔
213
  pMetaRsp->numOfColRefs = pRef->nCols;
573,852✔
214
  pMetaRsp->rversion = pRef->version;
573,852✔
215

216
  return code;
573,852✔
217
_return:
×
218
  taosMemoryFreeClear(pMetaRsp->pSchemaExt);
×
219
  taosMemoryFreeClear(pMetaRsp->pSchemas);
×
220
  taosMemoryFreeClear(pMetaRsp->pColRefs);
×
221
  return code;
×
222
}
223

224
int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
322,937✔
225
  int32_t code = 0;
322,937✔
226

227
#ifdef USE_INVERTED_INDEX
228
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
322,937✔
229
    return TSDB_CODE_INVALID_PARA;
×
230
  }
231
  void       *data = pCtbEntry->ctbEntry.pTags;
322,937✔
232
  const char *tagName = pSchema->name;
322,937✔
233

234
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
322,937✔
235
  tb_uid_t    tuid = pCtbEntry->uid;
322,937✔
236
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
322,937✔
237
  int32_t     nTagData = 0;
322,937✔
238

239
  SArray *pTagVals = NULL;
322,937✔
240
  code = tTagToValArray((const STag *)data, &pTagVals);
322,937✔
241
  if (code) {
322,937✔
242
    return code;
×
243
  }
244

245
  SIndexMultiTerm *terms = indexMultiTermCreate();
322,937✔
246
  if (terms == NULL) {
322,937✔
247
    return terrno;
×
248
  }
249

250
  int16_t nCols = taosArrayGetSize(pTagVals);
322,937✔
251
  for (int i = 0; i < nCols; i++) {
775,095✔
252
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
452,158✔
253
    char     type = pTagVal->type;
452,158✔
254

255
    char   *key = pTagVal->pKey;
452,158✔
256
    int32_t nKey = strlen(key);
452,158✔
257

258
    SIndexTerm *term = NULL;
452,158✔
259
    if (type == TSDB_DATA_TYPE_NULL) {
452,158✔
260
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
3,138✔
261
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
449,020✔
262
      if (pTagVal->nData > 0) {
436,038✔
263
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
433,946✔
264
        if (val == NULL) {
433,946✔
265
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
266
        }
267
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
433,946✔
268
        if (len < 0) {
433,946✔
269
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
270
        }
271
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
433,946✔
272
        type = TSDB_DATA_TYPE_VARCHAR;
433,946✔
273
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
433,946✔
274
        taosMemoryFree(val);
433,946✔
275
      } else if (pTagVal->nData == 0) {
2,092✔
276
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
2,092✔
277
      }
278
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
12,982✔
279
      double val = *(double *)(&pTagVal->i64);
9,844✔
280
      int    len = sizeof(val);
9,844✔
281
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
9,844✔
282
    } else if (type == TSDB_DATA_TYPE_BOOL) {
3,138✔
283
      int val = *(int *)(&pTagVal->i64);
3,138✔
284
      int len = sizeof(val);
3,138✔
285
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
3,138✔
286
    }
287

288
    if (term != NULL) {
452,158✔
289
      int32_t ret = indexMultiTermAdd(terms, term);
452,158✔
290
      if (ret < 0) {
452,158✔
291
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
292
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
293
      }
294
    } else {
295
      code = terrno;
×
296
      goto _exception;
×
297
    }
298
  }
299
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
322,937✔
300
  indexMultiTermDestroy(terms);
322,937✔
301

302
  taosArrayDestroy(pTagVals);
322,937✔
303
  return code;
322,937✔
304
_exception:
×
305
  indexMultiTermDestroy(terms);
×
306
  taosArrayDestroy(pTagVals);
×
307
#endif
308
  return code;
×
309
}
310
int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
118,135✔
311
int32_t code = 0;
118,135✔
312
#ifdef USE_INVERTED_INDEX
313
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
118,135✔
314
    return TSDB_CODE_INVALID_PARA;
×
315
  }
316
  void       *data = pCtbEntry->ctbEntry.pTags;
118,135✔
317
  const char *tagName = pSchema->name;
118,135✔
318

319
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
118,135✔
320
  tb_uid_t    tuid = pCtbEntry->uid;
118,135✔
321
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
118,135✔
322
  int32_t     nTagData = 0;
118,135✔
323

324
  SArray *pTagVals = NULL;
118,135✔
325
  code = tTagToValArray((const STag *)data, &pTagVals);
118,135✔
326
  if (code) {
118,135✔
327
    return code;
×
328
  }
329

330
  SIndexMultiTerm *terms = indexMultiTermCreate();
118,135✔
331
  if (terms == NULL) {
118,135✔
332
    return terrno;
×
333
  }
334

335
  int16_t nCols = taosArrayGetSize(pTagVals);
118,135✔
336
  for (int i = 0; i < nCols; i++) {
338,607✔
337
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
220,472✔
338
    char     type = pTagVal->type;
220,472✔
339

340
    char   *key = pTagVal->pKey;
220,472✔
341
    int32_t nKey = strlen(key);
220,472✔
342

343
    SIndexTerm *term = NULL;
220,472✔
344
    if (type == TSDB_DATA_TYPE_NULL) {
220,472✔
345
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
×
346
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
220,472✔
347
      if (pTagVal->nData > 0) {
217,857✔
348
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
217,857✔
349
        if (val == NULL) {
217,857✔
350
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
351
        }
352
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
217,857✔
353
        if (len < 0) {
217,857✔
354
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
355
        }
356
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
217,857✔
357
        type = TSDB_DATA_TYPE_VARCHAR;
217,857✔
358
        term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, val, len);
217,857✔
359
        taosMemoryFree(val);
217,857✔
360
      } else if (pTagVal->nData == 0) {
×
361
        term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
×
362
      }
363
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
2,615✔
364
      double val = *(double *)(&pTagVal->i64);
1,569✔
365
      int    len = sizeof(val);
1,569✔
366
      term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, (const char *)&val, len);
1,569✔
367
    } else if (type == TSDB_DATA_TYPE_BOOL) {
1,046✔
368
      int val = *(int *)(&pTagVal->i64);
1,046✔
369
      int len = sizeof(val);
1,046✔
370
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
1,046✔
371
    }
372
    if (term != NULL) {
220,472✔
373
      int32_t ret = indexMultiTermAdd(terms, term);
220,472✔
374
      if (ret < 0) {
220,472✔
375
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
376
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
377
      }
378
    } else {
379
      code = terrno;
×
380
      goto _exception;
×
381
    }
382
  }
383
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
118,135✔
384
  indexMultiTermDestroy(terms);
118,135✔
385
  taosArrayDestroy(pTagVals);
118,135✔
386
  return code;
118,135✔
387
_exception:
×
388
  indexMultiTermDestroy(terms);
×
389
  taosArrayDestroy(pTagVals);
×
390
#endif
391
  return code;
×
392
}
393

394
static int32_t metaDropTables(SMeta *pMeta, SArray *tbUids) {
12,996✔
395
  int32_t code = 0;
12,996✔
396
  if (taosArrayGetSize(tbUids) == 0) return TSDB_CODE_SUCCESS;
12,996✔
397

398
  int64_t    nCtbDropped = 0;
12,996✔
399
  SSHashObj *suidHash = tSimpleHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
12,996✔
400
  if (suidHash == NULL) {
12,996✔
401
    return terrno;
×
402
  }
403

404
  metaWLock(pMeta);
12,996✔
405
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
37,699✔
406
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
24,703✔
407
    tb_uid_t suid = 0;
24,703✔
408
    int8_t   sysTbl = 0;
24,703✔
409
    int      type;
24,703✔
410
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
24,703✔
411
    if (code) return code;
24,703✔
412
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
24,703✔
413
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
19,985✔
414
      if (pVal) {
19,985✔
415
        nCtbDropped = *pVal + 1;
9,009✔
416
      } else {
417
        nCtbDropped = 1;
10,976✔
418
      }
419
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
19,985✔
420
      if (code) return code;
19,985✔
421
    }
422
    /*
423
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
424
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
425
    }
426
    */
427
    metaDebug("batch drop table:%" PRId64, uid);
24,703✔
428
  }
429
  metaULock(pMeta);
12,996✔
430

431
  // update timeseries
432
  void   *pCtbDropped = NULL;
12,996✔
433
  int32_t iter = 0;
12,996✔
434
  while ((pCtbDropped = tSimpleHashIterate(suidHash, pCtbDropped, &iter))) {
23,972✔
435
    tb_uid_t    *pSuid = tSimpleHashGetKey(pCtbDropped, NULL);
10,976✔
436
    int32_t      nCols = 0;
10,976✔
437
    int8_t       flags = 0;
10,976✔
438
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
10,976✔
439
    if (metaGetStbStats(pMeta->pVnode, *pSuid, NULL, &nCols, &flags) == 0) {
10,976✔
440
      if (!TABLE_IS_VIRTUAL(flags)) {
10,976✔
441
        pStats->numOfTimeSeries -= *(int64_t *)pCtbDropped * (nCols - 1);
10,976✔
442
      }
443
    }
444
  }
445
  tSimpleHashCleanup(suidHash);
12,996✔
446

447
  pMeta->changed = true;
12,996✔
448
  return 0;
12,996✔
449
}
450

451
static int32_t metaFilterTableByHash(SMeta *pMeta, SArray *uidList) {
19,061✔
452
  int32_t code = 0;
19,061✔
453
  // 1, tranverse table's
454
  // 2, validate table name using vnodeValidateTableHash
455
  // 3, push invalidated table's uid into uidList
456

457
  TBC *pCur;
19,061✔
458
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
19,061✔
459
  if (code < 0) {
19,061✔
460
    return code;
×
461
  }
462

463
  code = tdbTbcMoveToFirst(pCur);
19,061✔
464
  if (code) {
19,061✔
465
    tdbTbcClose(pCur);
×
466
    return code;
×
467
  }
468

469
  void *pData = NULL, *pKey = NULL;
19,061✔
470
  int   nData = 0, nKey = 0;
19,061✔
471

472
  while (1) {
63,543✔
473
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
82,604✔
474
    if (ret < 0) {
82,604✔
475
      break;
19,061✔
476
    }
477

478
    SMetaEntry me = {0};
63,543✔
479
    SDecoder   dc = {0};
63,543✔
480
    tDecoderInit(&dc, pData, nData);
63,543✔
481
    code = metaDecodeEntry(&dc, &me);
63,543✔
482
    if (code < 0) {
63,543✔
483
      tDecoderClear(&dc);
×
484
      return code;
×
485
    }
486

487
    if (me.type != TSDB_SUPER_TABLE) {
63,543✔
488
      char tbFName[TSDB_TABLE_FNAME_LEN + 1];
49,382✔
489
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pMeta->pVnode->config.dbname, me.name);
49,382✔
490
      tbFName[TSDB_TABLE_FNAME_LEN] = '\0';
49,382✔
491
      if (pMeta->pVnode->mounted) tTrimMountPrefix(tbFName);
49,382✔
492
      ret = vnodeValidateTableHash(pMeta->pVnode, tbFName);
49,382✔
493
      if (ret < 0 && terrno == TSDB_CODE_VND_HASH_MISMATCH) {
49,382✔
494
        if (taosArrayPush(uidList, &me.uid) == NULL) {
24,703✔
495
          code = terrno;
×
496
          break;
×
497
        }
498
      }
499
    }
500
    tDecoderClear(&dc);
63,543✔
501
  }
502
  tdbFree(pData);
19,061✔
503
  tdbFree(pKey);
19,061✔
504
  tdbTbcClose(pCur);
19,061✔
505

506
  return 0;
19,061✔
507
}
508

509
int32_t metaTrimTables(SMeta *pMeta, int64_t version) {
19,061✔
510
  int32_t code = 0;
19,061✔
511

512
  SArray *tbUids = taosArrayInit(8, sizeof(int64_t));
19,061✔
513
  if (tbUids == NULL) {
19,061✔
514
    return terrno;
×
515
  }
516

517
  code = metaFilterTableByHash(pMeta, tbUids);
19,061✔
518
  if (code != 0) {
19,061✔
519
    goto end;
×
520
  }
521
  if (TARRAY_SIZE(tbUids) == 0) {
19,061✔
522
    goto end;
6,065✔
523
  }
524

525
  metaInfo("vgId:%d, trim %ld tables", TD_VID(pMeta->pVnode), taosArrayGetSize(tbUids));
12,996✔
526
  code = metaDropTables(pMeta, tbUids);
12,996✔
527
  if (code) goto end;
12,996✔
528

529
end:
12,996✔
530
  taosArrayDestroy(tbUids);
19,061✔
531

532
  return code;
19,061✔
533
}
534

535
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
8,580,523✔
536
  metaRLock(pMeta);
8,580,523✔
537

538
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
8,598,901✔
539

540
  metaULock(pMeta);
8,581,505✔
541

542
  if (ret != 0) {
8,581,324✔
543
    metaError("ttl failed to find expired table, ret:%d", ret);
×
544
  }
545

546
  return ret;
8,584,428✔
547
}
548

549
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
24,703✔
550
  int64_t btime;
551
  if (pME->type == TSDB_CHILD_TABLE) {
24,703✔
552
    btime = pME->ctbEntry.btime;
19,985✔
553
  } else if (pME->type == TSDB_NORMAL_TABLE) {
4,718✔
554
    btime = pME->ntbEntry.btime;
4,718✔
555
  } else {
556
    return TSDB_CODE_FAILED;
×
557
  }
558

559
  btimeKey->btime = btime;
24,703✔
560
  btimeKey->uid = pME->uid;
24,703✔
561
  return 0;
24,703✔
562
}
563

564
static int metaBuildNColIdxKey(SNcolIdxKey *ncolKey, const SMetaEntry *pME) {
4,718✔
565
  if (pME->type == TSDB_NORMAL_TABLE) {
4,718✔
566
    ncolKey->ncol = pME->ntbEntry.schemaRow.nCols;
4,718✔
567
    ncolKey->uid = pME->uid;
4,718✔
568
  } else {
569
    return TSDB_CODE_FAILED;
×
570
  }
571
  return 0;
4,718✔
572
}
573

574
static void metaDeleteTtl(SMeta *pMeta, const SMetaEntry *pME) {
24,703✔
575
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
24,703✔
576

577
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
24,703✔
578
  if (pME->type == TSDB_CHILD_TABLE) {
24,703✔
579
    ctx.ttlDays = pME->ctbEntry.ttlDays;
19,985✔
580
  } else {
581
    ctx.ttlDays = pME->ntbEntry.ttlDays;
4,718✔
582
  }
583

584
  int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
24,703✔
585
  if (ret < 0) {
24,703✔
586
    metaError("vgId:%d, failed to delete ttl for table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pME->name,
×
587
              pME->uid, tstrerror(ret));
588
  }
589
  return;
24,703✔
590
}
591

592
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl) {
24,703✔
593
  void      *pData = NULL;
24,703✔
594
  int        nData = 0;
24,703✔
595
  int        rc = 0;
24,703✔
596
  SMetaEntry e = {0};
24,703✔
597
  SDecoder   dc = {0};
24,703✔
598
  int32_t    ret = 0;
24,703✔
599

600
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
24,703✔
601
  if (rc < 0) {
24,703✔
602
    return rc;
×
603
  }
604
  int64_t version = ((SUidIdxVal *)pData)[0].version;
24,703✔
605

606
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
24,703✔
607
  if (rc < 0) {
24,703✔
608
    tdbFree(pData);
×
609
    return rc;
×
610
  }
611

612
  tDecoderInit(&dc, pData, nData);
24,703✔
613
  rc = metaDecodeEntry(&dc, &e);
24,703✔
614
  if (rc < 0) {
24,703✔
615
    tDecoderClear(&dc);
×
616
    return rc;
×
617
  }
618

619
  if (type) *type = e.type;
24,703✔
620

621
  if (e.type == TSDB_CHILD_TABLE) {
24,703✔
622
    if (pSuid) *pSuid = e.ctbEntry.suid;
19,985✔
623
    void *tData = NULL;
19,985✔
624
    int   tLen = 0;
19,985✔
625

626
    if (tdbTbGet(pMeta->pUidIdx, &e.ctbEntry.suid, sizeof(tb_uid_t), &tData, &tLen) == 0) {
19,985✔
627
      STbDbKey tbDbKey = {.uid = e.ctbEntry.suid, .version = ((SUidIdxVal *)tData)[0].version};
19,985✔
628
      if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &tData, &tLen) == 0) {
19,985✔
629
        SDecoder   tdc = {0};
19,985✔
630
        SMetaEntry stbEntry = {0};
19,985✔
631

632
        tDecoderInit(&tdc, tData, tLen);
19,985✔
633
        ret = metaDecodeEntry(&tdc, &stbEntry);
19,985✔
634
        if (ret < 0) {
19,985✔
635
          tDecoderClear(&tdc);
×
636
          metaError("vgId:%d, failed to decode child table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
637
                    e.ctbEntry.suid, tstrerror(ret));
638
          return ret;
×
639
        }
640

641
        if (pSysTbl) *pSysTbl = metaTbInFilterCache(pMeta, stbEntry.name, 1) ? 1 : 0;
19,985✔
642

643
        SSchema        *pTagColumn = NULL;
19,985✔
644
        SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
19,985✔
645
        if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
19,985✔
646
          pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
×
647
          ret = metaDelJsonVarFromIdx(pMeta, &e, pTagColumn);
×
648
          if (ret < 0) {
×
649
            metaError("vgId:%d, failed to delete json var from idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
650
                      e.name, e.uid, tstrerror(ret));
651
          }
652
        } else {
653
          for (int i = 0; i < pTagSchema->nCols; i++) {
53,905✔
654
            pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[i];
33,920✔
655
            if (!IS_IDX_ON(pTagColumn)) continue;
33,920✔
656
            STagIdxKey *pTagIdxKey = NULL;
19,985✔
657
            int32_t     nTagIdxKey;
19,985✔
658

659
            const void *pTagData = NULL;
19,985✔
660
            int32_t     nTagData = 0;
19,985✔
661

662
            STagVal tagVal = {.cid = pTagColumn->colId};
19,985✔
663
            if (tTagGet((const STag *)e.ctbEntry.pTags, &tagVal)) {
19,985✔
664
              if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
19,985✔
665
                pTagData = tagVal.pData;
×
666
                nTagData = (int32_t)tagVal.nData;
×
667
              } else {
668
                pTagData = &(tagVal.i64);
19,985✔
669
                nTagData = tDataTypes[pTagColumn->type].bytes;
19,985✔
670
              }
671
            } else {
672
              if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
×
673
                nTagData = tDataTypes[pTagColumn->type].bytes;
×
674
              }
675
            }
676

677
            if (metaCreateTagIdxKey(e.ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type, uid,
19,985✔
678
                                    &pTagIdxKey, &nTagIdxKey) == 0) {
679
              ret = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
19,985✔
680
              if (ret < 0) {
19,985✔
681
                metaError("vgId:%d, failed to delete tag idx key:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
682
                          e.name, e.uid, tstrerror(ret));
683
              }
684
            }
685
            metaDestroyTagIdxKey(pTagIdxKey);
19,985✔
686
            pTagIdxKey = NULL;
19,985✔
687
          }
688
        }
689
        tDecoderClear(&tdc);
19,985✔
690
      }
691
      tdbFree(tData);
19,985✔
692
    }
693
  }
694

695
  ret = tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), pMeta->txn);
24,703✔
696
  if (ret < 0) {
24,703✔
697
    metaError("vgId:%d, failed to delete table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
698
              tstrerror(ret));
699
  }
700
  ret = tdbTbDelete(pMeta->pNameIdx, e.name, strlen(e.name) + 1, pMeta->txn);
24,703✔
701
  if (ret < 0) {
24,703✔
702
    metaError("vgId:%d, failed to delete name idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
703
              tstrerror(ret));
704
  }
705
  ret = tdbTbDelete(pMeta->pUidIdx, &uid, sizeof(uid), pMeta->txn);
24,703✔
706
  if (ret < 0) {
24,703✔
707
    metaError("vgId:%d, failed to delete uid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
708
              tstrerror(ret));
709
  }
710

711
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
24,703✔
712
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
24,703✔
713

714
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
24,703✔
715

716
  if (e.type == TSDB_CHILD_TABLE) {
24,703✔
717
    ret =
718
        tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
19,985✔
719
    if (ret < 0) {
19,985✔
720
      metaError("vgId:%d, failed to delete ctb idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
721
                tstrerror(ret));
722
    }
723

724
    --pMeta->pVnode->config.vndStats.numOfCTables;
19,985✔
725
    metaUpdateStbStats(pMeta, e.ctbEntry.suid, -1, 0, -1);
19,985✔
726
    ret = metaUidCacheClear(pMeta, e.ctbEntry.suid);
19,985✔
727
    if (ret < 0) {
19,985✔
728
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
729
                e.ctbEntry.suid, tstrerror(ret));
730
    }
731
    ret = metaStableTagFilterCacheUpdateUid(
19,985✔
732
      pMeta, &e, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
19,985✔
NEW
733
    if (ret < 0) {
×
734
      metaError("vgId:%d, failed to update stable tag filter cache:%s "
735
        "uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
736
        e.ctbEntry.suid, tstrerror(ret));
737
    }
738
    ret = metaTbGroupCacheClear(pMeta, e.ctbEntry.suid);
739
    if (ret < 0) {
740
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
741
                e.ctbEntry.suid, tstrerror(ret));
4,718✔
742
    }
743
    /*
744
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
4,718✔
745
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, e.ctbEntry.suid, NULL);
4,718✔
746
    }
747
    */
748
  } else if (e.type == TSDB_NORMAL_TABLE) {
749
    // drop schema.db (todo)
750

751
    --pMeta->pVnode->config.vndStats.numOfNTables;
UNCOV
752
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
×
UNCOV
753

×
UNCOV
754
    /*
×
UNCOV
755
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
×
756
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, -1, &e.ntbEntry.schemaRow);
757
    }
758
    */
759
  } else if (e.type == TSDB_SUPER_TABLE) {
760
    ret = tdbTbDelete(pMeta->pSuidIdx, &e.uid, sizeof(tb_uid_t), pMeta->txn);
×
761
    if (ret < 0) {
×
762
      metaError("vgId:%d, failed to delete suid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
763
                tstrerror(ret));
764
    }
UNCOV
765
    // drop schema.db (todo)
×
UNCOV
766

×
767
    ret = metaStatsCacheDrop(pMeta, uid);
×
768
    if (ret < 0) {
769
      metaError("vgId:%d, failed to drop stats cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
UNCOV
770
                tstrerror(ret));
×
UNCOV
771
    }
×
772
    ret = metaUidCacheClear(pMeta, uid);
×
773
    if (ret < 0) {
774
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
UNCOV
775
                tstrerror(ret));
×
776
    }
777
    ret = metaStableTagFilterCacheDropSTable(pMeta, uid);
778
    if (ret < 0) {
24,703✔
779
      metaError("vgId:%d, failed to clear stable tag filter cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
24,703✔
780
                e.uid, tstrerror(ret));
24,703✔
781
    }
782
    ret = metaTbGroupCacheClear(pMeta, uid);
783
    if (ret < 0) {
784
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
24,703✔
785
                e.uid, tstrerror(ret));
24,703✔
786
    }
787
    --pMeta->pVnode->config.vndStats.numOfSTables;
24,703✔
788
  }
789

790
  ret = metaCacheDrop(pMeta, uid);
24,703✔
791
  if (ret < 0) {
24,703✔
792
    metaError("vgId:%d, failed to drop cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
24,703✔
UNCOV
793
              tstrerror(ret));
×
794
  }
795

24,703✔
796
  tDecoderClear(&dc);
797
  tdbFree(pData);
798

4,718✔
799
  return 0;
4,718✔
800
}
4,718✔
UNCOV
801

×
802
static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
803
  SBtimeIdxKey btimeKey = {0};
4,718✔
804
  if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) {
805
    return 0;
806
  }
11,924,922✔
807
  return tdbTbDelete(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), pMeta->txn);
11,924,922✔
808
}
11,924,922✔
809

3,507,140✔
810
int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
811
  SNcolIdxKey ncolKey = {0};
812
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
3,507,140✔
813
    return 0;
86,850✔
814
  }
86,850✔
815
  return tdbTbDelete(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), pMeta->txn);
480,291✔
816
}
480,291✔
817

44,476✔
818
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
44,476✔
819
  pMeta->changed = true;
7,645,981✔
820
  switch (pReq->action) {
7,645,981✔
821
    case TSDB_ALTER_TABLE_ADD_COLUMN:
3,285✔
822
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
3,285✔
823
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF:
16,854✔
824
      return metaAddTableColumn(pMeta, version, pReq, pMetaRsp);
16,854✔
825
    case TSDB_ALTER_TABLE_DROP_COLUMN:
6,478✔
826
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
6,478✔
827
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
77,275✔
828
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
77,275✔
829
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
56,292✔
830
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
56,292✔
UNCOV
831
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
×
UNCOV
832
      return metaUpdateTableTagValue(pMeta, version, pReq);
×
833
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
834
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
835
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
836
      return metaUpdateTableOptions2(pMeta, version, pReq);
837
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
1,130,020✔
838
      return metaUpdateTableColCompress2(pMeta, version, pReq);
1,130,020✔
839
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
840
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
1,130,020✔
UNCOV
841
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
×
UNCOV
842
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
×
843
    default:
844
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
845
      break;
1,130,020✔
846
  }
847
}
1,130,020✔
848

849
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
850
  if (!tsTtlChangeOnWrite) return 0;
534,418,246✔
851

534,418,246✔
852
  if (changeTimeMs <= 0) {
853
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
1,142,219✔
854
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
1,130,020✔
855
  }
1,130,020✔
856

1,130,020✔
857
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
858

859
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
77,164,065✔
860
}
861

77,164,065✔
862
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
5,517,709✔
863
  if (!tsTtlChangeOnWrite) return 0;
864

71,646,356✔
865
  metaWLock(pMeta);
866
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
867
  metaULock(pMeta);
77,173,298✔
868
  return ret;
77,170,800✔
UNCOV
869
}
×
870

871
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
872
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
77,164,820✔
873
  if (IS_VAR_DATA_TYPE(type)) {
77,170,735✔
874
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
77,173,170✔
875
  } else {
77,181,524✔
876
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
877
  }
878

77,167,669✔
879
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
5,538,282✔
880
  if (*ppTagIdxKey == NULL) {
5,527,629✔
881
    return terrno;
5,526,527✔
882
  }
883

71,629,387✔
884
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
71,632,548✔
885
  (*ppTagIdxKey)->cid = cid;
886
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
887
  (*ppTagIdxKey)->type = type;
77,157,950✔
888

889
  // refactor
890
  if (IS_VAR_DATA_TYPE(type)) {
73,738,168✔
891
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
73,738,168✔
892
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
73,738,629✔
893
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
894
  } else {
65,668,556✔
895
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
65,668,556✔
896
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
588,138,846✔
897
  }
522,462,381✔
898

522,459,854✔
899
  return 0;
522,467,603✔
900
}
901

522,416,272✔
902
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
522,512,401✔
903
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
904
}
522,513,886✔
905

522,476,419✔
906
static void colCompressDebug(SHashObj *pColCmprObj) {
522,466,356✔
907
  void *p = taosHashIterate(pColCmprObj, NULL);
522,463,557✔
908
  while (p) {
909
    uint32_t cmprAlg = *(uint32_t *)p;
65,676,465✔
910
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
911
    p = taosHashIterate(pColCmprObj, p);
912

65,717,391✔
913
    uint8_t l1, l2, lvl;
65,717,391✔
914
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
915

65,717,391✔
916
    const char *l1str = columnEncodeStr(l1);
65,714,814✔
UNCOV
917
    const char *l2str = columnCompressStr(l2);
×
UNCOV
918
    const char *lvlstr = columnLevelStr(lvl);
×
919
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
920
  }
921
  return;
65,714,814✔
922
}
65,713,811✔
923

65,717,416✔
924
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
65,715,326✔
925
  int rc = 0;
926

65,716,451✔
927
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
928
  if (pColCmprObj == NULL) {
65,715,890✔
929
    pColCmprObj = NULL;
65,719,334✔
930
    return TSDB_CODE_OUT_OF_MEMORY;
65,716,058✔
931
  }
42,429✔
932

42,429✔
933
  void      *pData = NULL;
42,429✔
934
  int        nData = 0;
935
  SMetaEntry e = {0};
65,673,629✔
936
  SDecoder   dc = {0};
65,677,041✔
937

65,675,401✔
UNCOV
938
  *ppColCmprObj = NULL;
×
UNCOV
939

×
UNCOV
940
  metaRLock(pMeta);
×
UNCOV
941
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
×
942
  if (rc < 0) {
943
    taosHashCleanup(pColCmprObj);
944
    metaULock(pMeta);
65,675,401✔
945
    return TSDB_CODE_FAILED;
65,674,225✔
946
  }
65,667,520✔
UNCOV
947
  int64_t version = ((SUidIdxVal *)pData)[0].version;
×
UNCOV
948
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
×
UNCOV
949
  if (rc < 0) {
×
950
    metaULock(pMeta);
×
951
    taosHashCleanup(pColCmprObj);
×
952
    metaError("failed to get table entry");
953
    return rc;
65,667,520✔
954
  }
65,673,308✔
955

588,183,539✔
956
  tDecoderInit(&dc, pData, nData);
522,499,492✔
957
  rc = metaDecodeEntry(&dc, &e);
522,504,976✔
958
  if (rc < 0) {
522,510,231✔
959
    tDecoderClear(&dc);
×
960
    tdbFree(pData);
×
961
    metaULock(pMeta);
×
962
    taosHashCleanup(pColCmprObj);
×
963
    return rc;
×
964
  }
965
  if (withExtSchema(e.type)) {
966
    SColCmprWrapper *p = &e.colCmpr;
UNCOV
967
    for (int32_t i = 0; i < p->nCols; i++) {
×
UNCOV
968
      SColCmpr *pCmpr = &p->pColCmpr[i];
×
UNCOV
969
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
×
UNCOV
970
      if (rc < 0) {
×
971
        tDecoderClear(&dc);
×
972
        tdbFree(pData);
973
        metaULock(pMeta);
65,672,368✔
974
        taosHashCleanup(pColCmprObj);
65,672,719✔
975
        return rc;
65,673,808✔
976
      }
977
    }
65,671,159✔
978
  } else {
65,671,178✔
979
    tDecoderClear(&dc);
980
    tdbFree(pData);
65,676,260✔
981
    metaULock(pMeta);
982
    taosHashCleanup(pColCmprObj);
983
    return 0;
4,191,290✔
984
  }
4,191,290✔
985
  tDecoderClear(&dc);
986
  tdbFree(pData);
987
  metaULock(pMeta);
988

989
  *ppColCmprObj = pColCmprObj;
990
  colCompressDebug(pColCmprObj);
991

992
  return 0;
993
}
994
// refactor later
995
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
996
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
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

© 2026 Coveralls, Inc