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

taosdata / TDengine / #5073

17 May 2026 01:15AM UTC coverage: 73.317% (+0.02%) from 73.298%
#5073

push

travis-ci

web-flow
feat (TDgpt): Dynamic Model Synchronization Enhancements (#35344)

* refactor: do some internal refactor.

* fix: fix multiprocess sync issue.

* feat: add dynamic anomaly detection and forecasting services

* fix: log error message for undeploying model in exception handling

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fix: handle undeploy when model exists only on disk

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/286aafa0-c3ce-4c27-b803-2707571e9dc1

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: guard dynamic registry concurrent access

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: tighten service list locking scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: restore prophet support and update tests per review feedback

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: improve test name and move copy inside lock scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* Potential fix for pull request finding

Co-au... (continued)

281387 of 383795 relevant lines covered (73.32%)

138791003.92 hits per line

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

79.18
/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 metaUpdateTableMultiTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq);
25
int32_t metaUpdateTableChildTableTagValue(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,669,593✔
41
  int32_t nCols = pWp->nCols;
3,669,593✔
42
  int32_t ver = pWp->version;
3,669,593✔
43
  if (add) {
3,669,593✔
44
    SColCmpr *p = taosMemoryRealloc(pWp->pColCmpr, sizeof(SColCmpr) * (nCols + 1));
3,592,124✔
45
    if (p == NULL) {
3,592,124✔
46
      return terrno;
×
47
    }
48
    pWp->pColCmpr = p;
3,592,124✔
49

50
    SColCmpr *pCol = p + nCols;
3,592,124✔
51
    pCol->id = pSchema->colId;
3,592,124✔
52
    pCol->alg = compress;
3,592,124✔
53
    pWp->nCols = nCols + 1;
3,592,124✔
54
    pWp->version = ver;
3,592,124✔
55
  } else {
56
    for (int32_t i = 0; i < nCols; i++) {
35,899,715✔
57
      SColCmpr *pOCmpr = &pWp->pColCmpr[i];
35,899,715✔
58
      if (pOCmpr->id == pSchema->colId) {
35,899,715✔
59
        int32_t left = (nCols - i - 1) * sizeof(SColCmpr);
77,469✔
60
        if (left) {
77,469✔
61
          memmove(pWp->pColCmpr + i, pWp->pColCmpr + i + 1, left);
54,136✔
62
        }
63
        nCols--;
77,469✔
64
        break;
77,469✔
65
      }
66
    }
67
    pWp->nCols = nCols;
77,469✔
68
    pWp->version = ver;
77,469✔
69
  }
70
  return 0;
3,669,593✔
71
}
72

73
int32_t addTableExtSchema(SMetaEntry *pEntry, const SSchema *pColumn, int32_t newColNum, SExtSchema *pExtSchema) {
3,654,651✔
74
  // no need to add ext schema when no column needs ext schemas
75
  if (!HAS_TYPE_MOD(pColumn) && !pEntry->pExtSchemas) return 0;
3,654,651✔
76
  if (!pEntry->pExtSchemas) {
16,458✔
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));
2,541✔
80
  } else {
81
    // already has columns with ext schema
82
    pEntry->pExtSchemas = (SExtSchema *)taosMemoryRealloc(pEntry->pExtSchemas, sizeof(SExtSchema) * newColNum);
13,917✔
83
  }
84
  if (!pEntry->pExtSchemas) return terrno;
16,458✔
85
  pEntry->pExtSchemas[newColNum - 1] = *pExtSchema;
16,458✔
86
  return 0;
16,458✔
87
}
88

89
int32_t dropTableExtSchema(SMetaEntry *pEntry, int32_t dropColId, int32_t newColNum) {
111,696✔
90
  // no ext schema, no need to drop
91
  if (!pEntry->pExtSchemas) return 0;
111,696✔
92
  if (dropColId == newColNum) {
11,706✔
93
    // drop the last column
94
    pEntry->pExtSchemas[dropColId - 1] = (SExtSchema){0};
4,240✔
95
  } else {
96
    // drop a column in the middle
97
    memmove(pEntry->pExtSchemas + dropColId, pEntry->pExtSchemas + dropColId + 1,
7,466✔
98
            (newColNum - dropColId) * sizeof(SExtSchema));
7,466✔
99
  }
100
  for (int32_t i = 0; i < newColNum; i++) {
43,740✔
101
    if (hasExtSchema(pEntry->pExtSchemas + i)) return 0;
42,046✔
102
  }
103
  taosMemoryFreeClear(pEntry->pExtSchemas);
1,694✔
104
  return 0;
1,694✔
105
}
106

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

116
    SColRef *pCol = p + nCols;
69,568✔
117
    if (NULL == pColRef) {
69,568✔
118
      pCol->hasRef = false;
7,041✔
119
      pCol->id = pSchema->colId;
7,041✔
120
    } else {
121
      pCol->hasRef = pColRef->hasRef;
62,527✔
122
      pCol->id = pSchema->colId;
62,527✔
123
      if (pCol->hasRef) {
62,527✔
124
        tstrncpy(pCol->refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
26,465✔
125
        tstrncpy(pCol->refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
26,465✔
126
        tstrncpy(pCol->refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
26,465✔
127
      }
128
    }
129
    pWp->nCols = nCols + 1;
69,568✔
130
    pWp->version++;
69,568✔
131
  } else {
132
    for (int32_t i = 0; i < nCols; i++) {
43,722,347✔
133
      SColRef *pOColRef = &pWp->pColRef[i];
43,722,347✔
134
      if (pOColRef->id == pSchema->colId) {
43,722,347✔
135
        int32_t left = (nCols - i - 1) * sizeof(SColRef);
39,196✔
136
        if (left) {
39,196✔
137
          memmove(pWp->pColRef + i, pWp->pColRef + i + 1, left);
35,632✔
138
        }
139
        nCols--;
39,196✔
140
        break;
39,196✔
141
      }
142
    }
143
    pWp->nCols = nCols;
39,196✔
144
    pWp->version++;
39,196✔
145
  }
146
  return 0;
108,764✔
147
}
148

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

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

161
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
12,817,803✔
162
  pMetaRsp->numOfColumns = pSchema->nCols;
12,817,803✔
163
  pMetaRsp->tableType = TSDB_NORMAL_TABLE;
12,817,803✔
164
  pMetaRsp->sversion = pSchema->version;
12,817,803✔
165
  pMetaRsp->rversion = 1;
12,817,803✔
166
  pMetaRsp->tuid = uid;
12,817,803✔
167
  pMetaRsp->virtualStb = false; // super table will never be processed here
12,817,803✔
168
  if (ownerId != 0) pMetaRsp->ownerId = ownerId;
12,817,803✔
169

170
  memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
12,817,803✔
171

172
  return 0;
12,817,803✔
173
}
174

175
static int32_t metaFillRspSchemaExt(const SSchemaWrapper *pSchema, const SExtSchema *pExtSchemas, SSchemaExt *pSchemaExt) {
925,377✔
176
  if (pSchema == NULL || pSchemaExt == NULL) {
925,377✔
177
    return TSDB_CODE_SUCCESS;
×
178
  }
179

180
  for (int32_t i = 0; i < pSchema->nCols; ++i) {
270,389,489✔
181
    pSchemaExt[i].colId = pSchema->pSchema[i].colId;
269,464,112✔
182
  }
183

184
  if (pExtSchemas != NULL) {
925,377✔
185
    for (int32_t i = 0; i < pSchema->nCols; ++i) {
168,844✔
186
      pSchemaExt[i].typeMod = pExtSchemas[i].typeMod;
150,977✔
187
    }
188
  }
189

190
  return TSDB_CODE_SUCCESS;
925,377✔
191
}
192

193
int32_t metaUpdateVtbMetaRsp(SMetaEntry *pEntry, char *tbName, const SSchemaWrapper *pSchema, const SColRefWrapper *pRef,
925,377✔
194
                             const SExtSchema *pExtSchemas, int64_t ownerId, STableMetaRsp *pMetaRsp, int8_t tableType) {
195
  int32_t code = TSDB_CODE_SUCCESS;
925,377✔
196
  if (!pRef) {
925,377✔
197
    return TSDB_CODE_INVALID_PARA;
×
198
  }
199
  if (pSchema) {
925,377✔
200
    pMetaRsp->pSchemas = taosMemoryMalloc(pSchema->nCols * sizeof(SSchema));
925,377✔
201
    if (NULL == pMetaRsp->pSchemas) {
925,377✔
202
      code = terrno;
×
203
      goto _return;
×
204
    }
205

206
    pMetaRsp->pSchemaExt = taosMemoryCalloc(pSchema->nCols, sizeof(SSchemaExt));
925,377✔
207
    if (pMetaRsp->pSchemaExt == NULL) {
925,377✔
208
      code = terrno;
×
209
      goto _return;
×
210
    }
211

212
    pMetaRsp->numOfColumns = pSchema->nCols;
925,377✔
213
    pMetaRsp->sversion = pSchema->version;
925,377✔
214
    memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
925,377✔
215

216
    code = metaFillRspSchemaExt(pSchema, pExtSchemas, pMetaRsp->pSchemaExt);
925,377✔
217
    if (code != TSDB_CODE_SUCCESS) {
925,377✔
218
      goto _return;
×
219
    }
220
  }
221

222
  if (pRef->nCols > 0) {
925,377✔
223
    pMetaRsp->pColRefs = taosMemoryMalloc(pRef->nCols * sizeof(SColRef));
925,377✔
224
    if (NULL == pMetaRsp->pColRefs) {
925,377✔
225
      code = terrno;
×
226
      goto _return;
×
227
    }
228

229
    memcpy(pMetaRsp->pColRefs, pRef->pColRef, pRef->nCols * sizeof(SColRef));
925,377✔
230
  } else {
231
    pMetaRsp->pColRefs = NULL;
×
232
  }
233

234
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
925,377✔
235
  if (tableType == TSDB_VIRTUAL_NORMAL_TABLE) {
925,377✔
236
    pMetaRsp->tuid = pEntry->uid;
465,439✔
237
  } else if (tableType == TSDB_VIRTUAL_CHILD_TABLE) {
459,938✔
238
    pMetaRsp->tuid = pEntry->uid;
459,938✔
239
    pMetaRsp->suid = pEntry->ctbEntry.suid;
459,938✔
240
  }
241

242
  pMetaRsp->tableType = tableType;
925,377✔
243
  pMetaRsp->virtualStb = false; // super table will never be processed here
925,377✔
244
  pMetaRsp->numOfColRefs = pRef->nCols;
925,377✔
245
  pMetaRsp->rversion = pRef->version;
925,377✔
246
  if (ownerId != 0) pMetaRsp->ownerId = ownerId;
925,377✔
247

248
  // Populate tag references
249
  if (pRef->nTagRefs > 0 && pRef->pTagRef) {
925,377✔
250
    pMetaRsp->pTagRefs = taosMemoryMalloc(pRef->nTagRefs * sizeof(SColRef));
×
251
    if (NULL == pMetaRsp->pTagRefs) {
×
252
      code = terrno;
×
253
      goto _return;
×
254
    }
255
    memcpy(pMetaRsp->pTagRefs, pRef->pTagRef, pRef->nTagRefs * sizeof(SColRef));
×
256
    pMetaRsp->numOfTagRefs = pRef->nTagRefs;
×
257
  } else {
258
    pMetaRsp->pTagRefs = NULL;
925,377✔
259
    pMetaRsp->numOfTagRefs = 0;
925,377✔
260
  }
261

262
  return code;
925,377✔
263
_return:
×
264
  taosMemoryFreeClear(pMetaRsp->pSchemaExt);
×
265
  taosMemoryFreeClear(pMetaRsp->pSchemas);
×
266
  taosMemoryFreeClear(pMetaRsp->pColRefs);
×
267
  taosMemoryFreeClear(pMetaRsp->pTagRefs);
×
268
  return code;
×
269
}
270

271
int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
308,282✔
272
  int32_t code = 0;
308,282✔
273

274
#ifdef USE_INVERTED_INDEX
275
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
308,282✔
276
    return TSDB_CODE_INVALID_PARA;
×
277
  }
278
  void       *data = pCtbEntry->ctbEntry.pTags;
308,282✔
279
  const char *tagName = pSchema->name;
308,282✔
280

281
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
308,282✔
282
  tb_uid_t    tuid = pCtbEntry->uid;
308,282✔
283
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
308,282✔
284
  int32_t     nTagData = 0;
308,282✔
285

286
  SArray *pTagVals = NULL;
308,282✔
287
  code = tTagToValArray((const STag *)data, &pTagVals);
308,282✔
288
  if (code) {
308,282✔
289
    return code;
×
290
  }
291

292
  SIndexMultiTerm *terms = indexMultiTermCreate();
308,282✔
293
  if (terms == NULL) {
308,282✔
294
    return terrno;
×
295
  }
296

297
  int16_t nCols = taosArrayGetSize(pTagVals);
308,282✔
298
  for (int i = 0; i < nCols; i++) {
743,829✔
299
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
435,547✔
300
    char     type = pTagVal->type;
435,547✔
301

302
    char   *key = pTagVal->pKey;
435,547✔
303
    int32_t nKey = strlen(key);
435,547✔
304

305
    SIndexTerm *term = NULL;
435,547✔
306
    if (type == TSDB_DATA_TYPE_NULL) {
435,547✔
307
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
3,204✔
308
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
432,343✔
309
      if (pTagVal->nData > 0) {
405,897✔
310
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
403,761✔
311
        if (val == NULL) {
403,761✔
312
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
313
        }
314
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
403,761✔
315
        if (len < 0) {
403,761✔
316
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
317
        }
318
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
403,761✔
319
        type = TSDB_DATA_TYPE_VARCHAR;
403,761✔
320
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
403,761✔
321
        taosMemoryFree(val);
403,761✔
322
      } else if (pTagVal->nData == 0) {
2,136✔
323
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
2,136✔
324
      }
325
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
26,446✔
326
      double val = *(double *)(&pTagVal->i64);
22,174✔
327
      int    len = sizeof(val);
22,174✔
328
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
22,174✔
329
    } else if (type == TSDB_DATA_TYPE_BOOL) {
4,272✔
330
      int val = *(int *)(&pTagVal->i64);
4,272✔
331
      int len = sizeof(val);
4,272✔
332
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
4,272✔
333
    }
334

335
    if (term != NULL) {
435,547✔
336
      int32_t ret = indexMultiTermAdd(terms, term);
435,547✔
337
      if (ret < 0) {
435,547✔
338
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
339
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
340
      }
341
    } else {
342
      code = terrno;
×
343
      goto _exception;
×
344
    }
345
  }
346
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
308,282✔
347
  indexMultiTermDestroy(terms);
308,282✔
348

349
  taosArrayDestroy(pTagVals);
308,282✔
350
  return code;
308,282✔
351
_exception:
×
352
  indexMultiTermDestroy(terms);
×
353
  taosArrayDestroy(pTagVals);
×
354
#endif
355
  return code;
×
356
}
357
int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
128,039✔
358
int32_t code = 0;
128,039✔
359
#ifdef USE_INVERTED_INDEX
360
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
128,039✔
361
    return TSDB_CODE_INVALID_PARA;
×
362
  }
363
  void       *data = pCtbEntry->ctbEntry.pTags;
128,039✔
364
  const char *tagName = pSchema->name;
128,039✔
365

366
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
128,039✔
367
  tb_uid_t    tuid = pCtbEntry->uid;
128,039✔
368
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
128,039✔
369
  int32_t     nTagData = 0;
128,039✔
370

371
  SArray *pTagVals = NULL;
128,039✔
372
  code = tTagToValArray((const STag *)data, &pTagVals);
128,039✔
373
  if (code) {
128,039✔
374
    return code;
×
375
  }
376

377
  SIndexMultiTerm *terms = indexMultiTermCreate();
128,039✔
378
  if (terms == NULL) {
128,039✔
379
    return terrno;
×
380
  }
381

382
  int16_t nCols = taosArrayGetSize(pTagVals);
128,039✔
383
  for (int i = 0; i < nCols; i++) {
367,098✔
384
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
239,059✔
385
    char     type = pTagVal->type;
239,059✔
386

387
    char   *key = pTagVal->pKey;
239,059✔
388
    int32_t nKey = strlen(key);
239,059✔
389

390
    SIndexTerm *term = NULL;
239,059✔
391
    if (type == TSDB_DATA_TYPE_NULL) {
239,059✔
392
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
×
393
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
239,059✔
394
      if (pTagVal->nData > 0) {
236,389✔
395
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
236,389✔
396
        if (val == NULL) {
236,389✔
397
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
398
        }
399
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
236,389✔
400
        if (len < 0) {
236,389✔
401
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
402
        }
403
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
236,389✔
404
        type = TSDB_DATA_TYPE_VARCHAR;
236,389✔
405
        term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, val, len);
236,389✔
406
        taosMemoryFree(val);
236,389✔
407
      } else if (pTagVal->nData == 0) {
×
408
        term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
×
409
      }
410
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
2,670✔
411
      double val = *(double *)(&pTagVal->i64);
1,602✔
412
      int    len = sizeof(val);
1,602✔
413
      term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, (const char *)&val, len);
1,602✔
414
    } else if (type == TSDB_DATA_TYPE_BOOL) {
1,068✔
415
      int val = *(int *)(&pTagVal->i64);
1,068✔
416
      int len = sizeof(val);
1,068✔
417
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
1,068✔
418
    }
419
    if (term != NULL) {
239,059✔
420
      int32_t ret = indexMultiTermAdd(terms, term);
239,059✔
421
      if (ret < 0) {
239,059✔
422
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
423
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
424
      }
425
    } else {
426
      code = terrno;
×
427
      goto _exception;
×
428
    }
429
  }
430
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
128,039✔
431
  indexMultiTermDestroy(terms);
128,039✔
432
  taosArrayDestroy(pTagVals);
128,039✔
433
  return code;
128,039✔
434
_exception:
×
435
  indexMultiTermDestroy(terms);
×
436
  taosArrayDestroy(pTagVals);
×
437
#endif
438
  return code;
×
439
}
440

441
static int32_t metaDropTables(SMeta *pMeta, SArray *tbUids) {
22,560✔
442
  int32_t code = 0;
22,560✔
443
  if (taosArrayGetSize(tbUids) == 0) return TSDB_CODE_SUCCESS;
22,560✔
444

445
  int64_t    nCtbDropped = 0;
22,560✔
446
  SSHashObj *suidHash = tSimpleHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
22,560✔
447
  if (suidHash == NULL) {
22,560✔
448
    return terrno;
×
449
  }
450

451
  metaWLock(pMeta);
22,560✔
452
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
116,517✔
453
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
93,957✔
454
    tb_uid_t suid = 0;
93,957✔
455
    int8_t   sysTbl = 0;
93,957✔
456
    int      type;
93,957✔
457
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
93,957✔
458
    if (code) return code;
93,957✔
459
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
93,957✔
460
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
86,597✔
461
      if (pVal) {
86,597✔
462
        nCtbDropped = *pVal + 1;
68,283✔
463
      } else {
464
        nCtbDropped = 1;
18,314✔
465
      }
466
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
86,597✔
467
      if (code) return code;
86,597✔
468
    }
469
    /*
470
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
471
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
472
    }
473
    */
474
    metaDebug("batch drop table:%" PRId64, uid);
93,957✔
475
  }
476
  metaULock(pMeta);
22,560✔
477

478
  // update timeseries
479
  void   *pCtbDropped = NULL;
22,560✔
480
  int32_t iter = 0;
22,560✔
481
  while ((pCtbDropped = tSimpleHashIterate(suidHash, pCtbDropped, &iter))) {
40,874✔
482
    tb_uid_t    *pSuid = tSimpleHashGetKey(pCtbDropped, NULL);
18,314✔
483
    int32_t      nCols = 0;
18,314✔
484
    int8_t       flags = 0;
18,314✔
485
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
18,314✔
486
    if (metaGetStbStats(pMeta->pVnode, *pSuid, NULL, &nCols, &flags) == 0) {
18,314✔
487
      if (!TABLE_IS_VIRTUAL(flags)) {
18,314✔
488
        pStats->numOfTimeSeries -= *(int64_t *)pCtbDropped * (nCols - 1);
18,314✔
489
      }
490
    }
491
  }
492
  tSimpleHashCleanup(suidHash);
22,560✔
493

494
  pMeta->changed = true;
22,560✔
495
  return 0;
22,560✔
496
}
497

498
static int32_t metaFilterTableByHash(SMeta *pMeta, SArray *uidList) {
28,546✔
499
  int32_t code = 0;
28,546✔
500
  // 1, tranverse table's
501
  // 2, validate table name using vnodeValidateTableHash
502
  // 3, push invalidated table's uid into uidList
503

504
  TBC *pCur;
28,546✔
505
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
28,546✔
506
  if (code < 0) {
28,546✔
507
    return code;
×
508
  }
509

510
  code = tdbTbcMoveToFirst(pCur);
28,546✔
511
  if (code) {
28,546✔
512
    tdbTbcClose(pCur);
×
513
    return code;
×
514
  }
515

516
  void *pData = NULL, *pKey = NULL;
28,546✔
517
  int   nData = 0, nKey = 0;
28,546✔
518

519
  while (1) {
209,603✔
520
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
238,149✔
521
    if (ret < 0) {
238,149✔
522
      break;
28,546✔
523
    }
524

525
    SMetaEntry me = {0};
209,603✔
526
    SDecoder   dc = {0};
209,603✔
527
    tDecoderInit(&dc, pData, nData);
209,603✔
528
    code = metaDecodeEntry(&dc, &me);
209,603✔
529
    if (code < 0) {
209,603✔
530
      tDecoderClear(&dc);
×
531
      return code;
×
532
    }
533

534
    if (me.type != TSDB_SUPER_TABLE) {
209,603✔
535
      char tbFName[TSDB_TABLE_FNAME_LEN + 1];
187,912✔
536
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pMeta->pVnode->config.dbname, me.name);
187,912✔
537
      tbFName[TSDB_TABLE_FNAME_LEN] = '\0';
187,912✔
538
      if (pMeta->pVnode->mounted) tTrimMountPrefix(tbFName);
187,912✔
539
      ret = vnodeValidateTableHash(pMeta->pVnode, tbFName);
187,912✔
540
      if (ret < 0 && terrno == TSDB_CODE_VND_HASH_MISMATCH) {
187,912✔
541
        if (taosArrayPush(uidList, &me.uid) == NULL) {
93,957✔
542
          code = terrno;
×
543
          break;
×
544
        }
545
      }
546
    }
547
    tDecoderClear(&dc);
209,603✔
548
  }
549
  tdbFree(pData);
28,546✔
550
  tdbFree(pKey);
28,546✔
551
  tdbTbcClose(pCur);
28,546✔
552

553
  return 0;
28,546✔
554
}
555

556
int32_t metaTrimTables(SMeta *pMeta, int64_t version) {
28,546✔
557
  int32_t code = 0;
28,546✔
558

559
  SArray *tbUids = taosArrayInit(8, sizeof(int64_t));
28,546✔
560
  if (tbUids == NULL) {
28,546✔
561
    return terrno;
×
562
  }
563

564
  code = metaFilterTableByHash(pMeta, tbUids);
28,546✔
565
  if (code != 0) {
28,546✔
566
    goto end;
×
567
  }
568
  if (TARRAY_SIZE(tbUids) == 0) {
28,546✔
569
    goto end;
5,986✔
570
  }
571

572
  metaInfo("vgId:%d, trim %ld tables", TD_VID(pMeta->pVnode), taosArrayGetSize(tbUids));
22,560✔
573
  code = metaDropTables(pMeta, tbUids);
22,560✔
574
  if (code) goto end;
22,560✔
575

576
end:
22,560✔
577
  taosArrayDestroy(tbUids);
28,546✔
578

579
  return code;
28,546✔
580
}
581

582
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
11,118,495✔
583
  metaRLock(pMeta);
11,118,495✔
584

585
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
11,150,200✔
586

587
  metaULock(pMeta);
11,097,902✔
588

589
  if (ret != 0) {
11,109,065✔
590
    metaError("ttl failed to find expired table, ret:%d", ret);
×
591
  }
592

593
  return ret;
11,115,020✔
594
}
595

596
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
93,957✔
597
  int64_t btime;
598
  if (pME->type == TSDB_CHILD_TABLE) {
93,957✔
599
    btime = pME->ctbEntry.btime;
86,597✔
600
  } else if (pME->type == TSDB_NORMAL_TABLE) {
7,360✔
601
    btime = pME->ntbEntry.btime;
7,360✔
602
  } else {
603
    return TSDB_CODE_FAILED;
×
604
  }
605

606
  btimeKey->btime = btime;
93,957✔
607
  btimeKey->uid = pME->uid;
93,957✔
608
  return 0;
93,957✔
609
}
610

611
static int metaBuildNColIdxKey(SNcolIdxKey *ncolKey, const SMetaEntry *pME) {
7,360✔
612
  if (pME->type == TSDB_NORMAL_TABLE) {
7,360✔
613
    ncolKey->ncol = pME->ntbEntry.schemaRow.nCols;
7,360✔
614
    ncolKey->uid = pME->uid;
7,360✔
615
  } else {
616
    return TSDB_CODE_FAILED;
×
617
  }
618
  return 0;
7,360✔
619
}
620

621
static void metaDeleteTtl(SMeta *pMeta, const SMetaEntry *pME) {
93,957✔
622
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
93,957✔
623

624
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
93,957✔
625
  if (pME->type == TSDB_CHILD_TABLE) {
93,957✔
626
    ctx.ttlDays = pME->ctbEntry.ttlDays;
86,597✔
627
  } else {
628
    ctx.ttlDays = pME->ntbEntry.ttlDays;
7,360✔
629
  }
630

631
  int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
93,957✔
632
  if (ret < 0) {
93,957✔
633
    metaError("vgId:%d, failed to delete ttl for table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pME->name,
×
634
              pME->uid, tstrerror(ret));
635
  }
636
  return;
93,957✔
637
}
638

639
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl) {
93,957✔
640
  void      *pData = NULL;
93,957✔
641
  int        nData = 0;
93,957✔
642
  int        rc = 0;
93,957✔
643
  SMetaEntry e = {0};
93,957✔
644
  SDecoder   dc = {0};
93,957✔
645
  int32_t    ret = 0;
93,957✔
646

647
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
93,957✔
648
  if (rc < 0) {
93,957✔
649
    return rc;
×
650
  }
651
  int64_t version = ((SUidIdxVal *)pData)[0].version;
93,957✔
652

653
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
93,957✔
654
  if (rc < 0) {
93,957✔
655
    tdbFree(pData);
×
656
    return rc;
×
657
  }
658

659
  tDecoderInit(&dc, pData, nData);
93,957✔
660
  rc = metaDecodeEntry(&dc, &e);
93,957✔
661
  if (rc < 0) {
93,957✔
662
    tDecoderClear(&dc);
×
663
    return rc;
×
664
  }
665

666
  if (type) *type = e.type;
93,957✔
667

668
  if (e.type == TSDB_CHILD_TABLE) {
93,957✔
669
    if (pSuid) *pSuid = e.ctbEntry.suid;
86,597✔
670
    void *tData = NULL;
86,597✔
671
    int   tLen = 0;
86,597✔
672

673
    if (tdbTbGet(pMeta->pUidIdx, &e.ctbEntry.suid, sizeof(tb_uid_t), &tData, &tLen) == 0) {
86,597✔
674
      STbDbKey tbDbKey = {.uid = e.ctbEntry.suid, .version = ((SUidIdxVal *)tData)[0].version};
86,597✔
675
      if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &tData, &tLen) == 0) {
86,597✔
676
        SDecoder   tdc = {0};
86,597✔
677
        SMetaEntry stbEntry = {0};
86,597✔
678

679
        tDecoderInit(&tdc, tData, tLen);
86,597✔
680
        ret = metaDecodeEntry(&tdc, &stbEntry);
86,597✔
681
        if (ret < 0) {
86,597✔
682
          tDecoderClear(&tdc);
×
683
          metaError("vgId:%d, failed to decode child table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
684
                    e.ctbEntry.suid, tstrerror(ret));
685
          return ret;
×
686
        }
687

688
        if (pSysTbl) *pSysTbl = metaTbInFilterCache(pMeta, stbEntry.name, 1) ? 1 : 0;
86,597✔
689
        
690
        ret = metaStableTagFilterCacheUpdateUid(
86,597✔
691
          pMeta, &e, &stbEntry, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
692
        if (ret < 0) {
86,597✔
693
          metaError("vgId:%d, failed to update stable tag filter cache:%s "
×
694
            "uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
695
            e.ctbEntry.suid, tstrerror(ret));
696
        }
697

698
        SSchema        *pTagColumn = NULL;
86,597✔
699
        SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
86,597✔
700
        if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
86,597✔
701
          pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
×
702
          ret = metaDelJsonVarFromIdx(pMeta, &e, pTagColumn);
×
703
          if (ret < 0) {
×
704
            metaError("vgId:%d, failed to delete json var from idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
705
                      e.name, e.uid, tstrerror(ret));
706
          }
707
        } else {
708
          for (int i = 0; i < pTagSchema->nCols; i++) {
450,146✔
709
            pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[i];
363,549✔
710
            if (!IS_IDX_ON(pTagColumn)) continue;
363,549✔
711
            STagIdxKey *pTagIdxKey = NULL;
86,597✔
712
            int32_t     nTagIdxKey;
86,597✔
713

714
            const void *pTagData = NULL;
86,597✔
715
            int32_t     nTagData = 0;
86,597✔
716

717
            STagVal tagVal = {.cid = pTagColumn->colId};
86,597✔
718
            if (tTagGet((const STag *)e.ctbEntry.pTags, &tagVal)) {
86,597✔
719
              if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
86,597✔
720
                pTagData = tagVal.pData;
×
721
                nTagData = (int32_t)tagVal.nData;
×
722
              } else {
723
                pTagData = &(tagVal.i64);
86,597✔
724
                nTagData = tDataTypes[pTagColumn->type].bytes;
86,597✔
725
              }
726
            } else {
727
              if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
×
728
                nTagData = tDataTypes[pTagColumn->type].bytes;
×
729
              }
730
            }
731

732
            if (metaCreateTagIdxKey(e.ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type, uid,
86,597✔
733
                                    &pTagIdxKey, &nTagIdxKey) == 0) {
734
              ret = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
86,597✔
735
              if (ret < 0) {
86,597✔
736
                metaError("vgId:%d, failed to delete tag idx key:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
737
                          e.name, e.uid, tstrerror(ret));
738
              }
739
            }
740
            metaDestroyTagIdxKey(pTagIdxKey);
86,597✔
741
            pTagIdxKey = NULL;
86,597✔
742
          }
743
        }
744
        tDecoderClear(&tdc);
86,597✔
745
      }
746
      tdbFree(tData);
86,597✔
747
    }
748
  }
749

750
  ret = tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), pMeta->txn);
93,957✔
751
  if (ret < 0) {
93,957✔
752
    metaError("vgId:%d, failed to delete table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
753
              tstrerror(ret));
754
  }
755
  ret = tdbTbDelete(pMeta->pNameIdx, e.name, strlen(e.name) + 1, pMeta->txn);
93,957✔
756
  if (ret < 0) {
93,957✔
757
    metaError("vgId:%d, failed to delete name idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
758
              tstrerror(ret));
759
  }
760
  ret = tdbTbDelete(pMeta->pUidIdx, &uid, sizeof(uid), pMeta->txn);
93,957✔
761
  if (ret < 0) {
93,957✔
762
    metaError("vgId:%d, failed to delete uid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
763
              tstrerror(ret));
764
  }
765

766
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
93,957✔
767
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
93,957✔
768

769
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
93,957✔
770

771
  if (e.type == TSDB_CHILD_TABLE) {
93,957✔
772
    ret =
773
        tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
86,597✔
774
    if (ret < 0) {
86,597✔
775
      metaError("vgId:%d, failed to delete ctb idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
776
                tstrerror(ret));
777
    }
778

779
    --pMeta->pVnode->config.vndStats.numOfCTables;
86,597✔
780
    metaUpdateStbStats(pMeta, e.ctbEntry.suid, -1, 0, -1);
86,597✔
781
    ret = metaUidCacheClear(pMeta, e.ctbEntry.suid);
86,597✔
782
    if (ret < 0) {
86,597✔
783
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
784
                e.ctbEntry.suid, tstrerror(ret));
785
    }
786
    ret = metaTbGroupCacheClear(pMeta, e.ctbEntry.suid);
86,597✔
787
    if (ret < 0) {
86,597✔
788
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
789
                e.ctbEntry.suid, tstrerror(ret));
790
    }
791
    /*
792
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
793
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, e.ctbEntry.suid, NULL);
794
    }
795
    */
796
  } else if (e.type == TSDB_NORMAL_TABLE) {
7,360✔
797
    // drop schema.db (todo)
798

799
    --pMeta->pVnode->config.vndStats.numOfNTables;
7,360✔
800
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
7,360✔
801

802
    /*
803
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
804
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, -1, &e.ntbEntry.schemaRow);
805
    }
806
    */
807
  } else if (e.type == TSDB_SUPER_TABLE) {
×
808
    ret = tdbTbDelete(pMeta->pSuidIdx, &e.uid, sizeof(tb_uid_t), pMeta->txn);
×
809
    if (ret < 0) {
×
810
      metaError("vgId:%d, failed to delete suid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
811
                tstrerror(ret));
812
    }
813
    // drop schema.db (todo)
814

815
    ret = metaStatsCacheDrop(pMeta, uid);
×
816
    if (ret < 0) {
×
817
      metaError("vgId:%d, failed to drop stats cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
818
                tstrerror(ret));
819
    }
820
    ret = metaUidCacheClear(pMeta, uid);
×
821
    if (ret < 0) {
×
822
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
823
                tstrerror(ret));
824
    }
825
    ret = metaStableTagFilterCacheDropSTable(pMeta, uid);
×
826
    if (ret < 0) {
×
827
      metaError("vgId:%d, failed to clear stable tag filter cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
828
                e.uid, tstrerror(ret));
829
    }
830
    ret = metaTbGroupCacheClear(pMeta, uid);
×
831
    if (ret < 0) {
×
832
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
833
                e.uid, tstrerror(ret));
834
    }
835
    --pMeta->pVnode->config.vndStats.numOfSTables;
×
836
  }
837

838
  ret = metaCacheDrop(pMeta, uid);
93,957✔
839
  if (ret < 0) {
93,957✔
840
    metaError("vgId:%d, failed to drop cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
93,957✔
841
              tstrerror(ret));
842
  }
843

844
  tDecoderClear(&dc);
93,957✔
845
  tdbFree(pData);
93,957✔
846

847
  return 0;
93,957✔
848
}
849

850
static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
93,957✔
851
  SBtimeIdxKey btimeKey = {0};
93,957✔
852
  if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) {
93,957✔
853
    return 0;
×
854
  }
855
  return tdbTbDelete(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), pMeta->txn);
93,957✔
856
}
857

858
int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
7,360✔
859
  SNcolIdxKey ncolKey = {0};
7,360✔
860
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
7,360✔
861
    return 0;
×
862
  }
863
  return tdbTbDelete(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), pMeta->txn);
7,360✔
864
}
865

866
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
13,837,896✔
867
  pMeta->changed = true;
13,837,896✔
868
  switch (pReq->action) {
13,837,896✔
869
    case TSDB_ALTER_TABLE_ADD_COLUMN:
4,025,933✔
870
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
871
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF:
872
      return metaAddTableColumn(pMeta, version, pReq, pMetaRsp);
4,025,933✔
873
    case TSDB_ALTER_TABLE_DROP_COLUMN:
111,696✔
874
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
111,696✔
875
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
557,581✔
876
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
557,581✔
877
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
50,444✔
878
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
50,444✔
879
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TABLE_TAG_VAL:
8,836,199✔
880
      return metaUpdateTableMultiTableTagValue(pMeta, version, pReq);
8,836,199✔
881
    case TSDB_ALTER_TABLE_UPDATE_CHILD_TABLE_TAG_VAL:
67,506✔
882
      return metaUpdateTableChildTableTagValue(pMeta, version, pReq);
67,506✔
883
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
28,602✔
884
      return metaUpdateTableOptions2(pMeta, version, pReq);
28,602✔
885
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
6,952✔
886
      return metaUpdateTableColCompress2(pMeta, version, pReq);
6,952✔
887
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
87,843✔
888
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
87,843✔
889
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
65,140✔
890
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
65,140✔
891
    default:
×
892
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
893
      break;
894
  }
895
}
896

897
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
24,387,397✔
898
  if (!tsTtlChangeOnWrite) return 0;
24,387,397✔
899

900
  if (changeTimeMs <= 0) {
24,387,397✔
901
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
×
902
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
×
903
  }
904

905
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
24,387,397✔
906

907
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
24,387,397✔
908
}
909

910
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
1,638,088,920✔
911
  if (!tsTtlChangeOnWrite) return 0;
1,638,088,920✔
912

913
  metaWLock(pMeta);
24,405,917✔
914
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
24,387,397✔
915
  metaULock(pMeta);
24,387,397✔
916
  return ret;
24,387,397✔
917
}
918

919
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
131,216,772✔
920
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
921
  if (IS_VAR_DATA_TYPE(type)) {
131,216,772✔
922
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
13,078,305✔
923
  } else {
924
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
118,138,467✔
925
  }
926

927
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
131,275,851✔
928
  if (*ppTagIdxKey == NULL) {
131,235,592✔
929
    return terrno;
×
930
  }
931

932
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
131,208,554✔
933
  (*ppTagIdxKey)->cid = cid;
131,228,921✔
934
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
131,237,236✔
935
  (*ppTagIdxKey)->type = type;
131,240,257✔
936

937
  // refactor
938
  if (IS_VAR_DATA_TYPE(type)) {
131,216,959✔
939
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
13,145,443✔
940
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
13,132,307✔
941
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
13,135,162✔
942
  } else {
943
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
118,071,516✔
944
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
118,143,398✔
945
  }
946

947
  return 0;
131,209,115✔
948
}
949

950
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
127,658,770✔
951
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
127,658,770✔
952
}
127,665,238✔
953

954
static void colCompressDebug(SHashObj *pColCmprObj) {
125,375,537✔
955
  void *p = taosHashIterate(pColCmprObj, NULL);
125,375,537✔
956
  while (p) {
1,084,869,471✔
957
    uint32_t cmprAlg = *(uint32_t *)p;
959,435,219✔
958
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
959,473,834✔
959
    p = taosHashIterate(pColCmprObj, p);
959,558,234✔
960

961
    uint8_t l1, l2, lvl;
960,046,525✔
962
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
959,952,346✔
963

964
    const char *l1str = columnEncodeStr(l1);
959,928,407✔
965
    const char *l2str = columnCompressStr(l2);
959,718,465✔
966
    const char *lvlstr = columnLevelStr(lvl);
959,564,889✔
967
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
959,470,967✔
968
  }
969
  return;
125,434,252✔
970
}
971

972
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
125,448,046✔
973
  int rc = 0;
125,448,046✔
974

975
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
125,448,046✔
976
  if (pColCmprObj == NULL) {
125,444,719✔
977
    pColCmprObj = NULL;
×
978
    return TSDB_CODE_OUT_OF_MEMORY;
×
979
  }
980

981
  void      *pData = NULL;
125,444,719✔
982
  int        nData = 0;
125,435,312✔
983
  SMetaEntry e = {0};
125,465,276✔
984
  SDecoder   dc = {0};
125,466,986✔
985

986
  *ppColCmprObj = NULL;
125,470,522✔
987

988
  metaRLock(pMeta);
125,473,655✔
989
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
125,492,886✔
990
  if (rc < 0) {
125,455,174✔
991
    taosHashCleanup(pColCmprObj);
59,424✔
992
    metaULock(pMeta);
56,896✔
993
    return TSDB_CODE_FAILED;
55,632✔
994
  }
995
  int64_t version = ((SUidIdxVal *)pData)[0].version;
125,395,750✔
996
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
125,411,143✔
997
  if (rc < 0) {
125,403,298✔
998
    metaULock(pMeta);
×
999
    taosHashCleanup(pColCmprObj);
×
1000
    metaError("failed to get table entry");
×
1001
    return rc;
×
1002
  }
1003

1004
  tDecoderInit(&dc, pData, nData);
125,403,298✔
1005
  rc = metaDecodeEntry(&dc, &e);
125,418,353✔
1006
  if (rc < 0) {
125,356,411✔
1007
    tDecoderClear(&dc);
×
1008
    tdbFree(pData);
×
1009
    metaULock(pMeta);
×
1010
    taosHashCleanup(pColCmprObj);
×
1011
    return rc;
×
1012
  }
1013
  if (withColCompress(e.type)) {
125,356,411✔
1014
    SColCmprWrapper *p = &e.colCmpr;
125,384,110✔
1015
    for (int32_t i = 0; i < p->nCols; i++) {
1,085,477,685✔
1016
      SColCmpr *pCmpr = &p->pColCmpr[i];
960,066,847✔
1017
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
960,089,293✔
1018
      if (rc < 0) {
960,093,575✔
1019
        tDecoderClear(&dc);
×
1020
        tdbFree(pData);
×
1021
        metaULock(pMeta);
×
1022
        taosHashCleanup(pColCmprObj);
×
1023
        return rc;
×
1024
      }
1025
    }
1026
  } else {
1027
    tDecoderClear(&dc);
×
1028
    tdbFree(pData);
×
1029
    metaULock(pMeta);
×
1030
    taosHashCleanup(pColCmprObj);
×
1031
    return 0;
×
1032
  }
1033
  tDecoderClear(&dc);
125,347,235✔
1034
  tdbFree(pData);
125,428,553✔
1035
  metaULock(pMeta);
125,422,238✔
1036

1037
  *ppColCmprObj = pColCmprObj;
125,405,515✔
1038
  colCompressDebug(pColCmprObj);
125,405,949✔
1039

1040
  return 0;
125,434,781✔
1041
}
1042
// refactor later
1043
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
4,869,744✔
1044
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
4,870,180✔
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