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

taosdata / TDengine / #4927

14 Jan 2026 07:42AM UTC coverage: 66.12% (+0.07%) from 66.053%
#4927

push

travis-ci

web-flow
fix memleak decimal (#34283)

2 of 16 new or added lines in 1 file covered. (12.5%)

476 existing lines in 124 files now uncovered.

200574 of 303348 relevant lines covered (66.12%)

130242324.59 hits per line

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

80.71
/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,095,879✔
41
  int32_t nCols = pWp->nCols;
3,095,879✔
42
  int32_t ver = pWp->version;
3,095,879✔
43
  if (add) {
3,095,879✔
44
    SColCmpr *p = taosMemoryRealloc(pWp->pColCmpr, sizeof(SColCmpr) * (nCols + 1));
3,034,370✔
45
    if (p == NULL) {
3,034,370✔
46
      return terrno;
×
47
    }
48
    pWp->pColCmpr = p;
3,034,370✔
49

50
    SColCmpr *pCol = p + nCols;
3,034,370✔
51
    pCol->id = pSchema->colId;
3,034,370✔
52
    pCol->alg = compress;
3,034,370✔
53
    pWp->nCols = nCols + 1;
3,034,370✔
54
    pWp->version = ver;
3,034,370✔
55
  } else {
56
    for (int32_t i = 0; i < nCols; i++) {
37,689,859✔
57
      SColCmpr *pOCmpr = &pWp->pColCmpr[i];
37,689,859✔
58
      if (pOCmpr->id == pSchema->colId) {
37,689,859✔
59
        int32_t left = (nCols - i - 1) * sizeof(SColCmpr);
61,509✔
60
        if (left) {
61,509✔
61
          memmove(pWp->pColCmpr + i, pWp->pColCmpr + i + 1, left);
42,007✔
62
        }
63
        nCols--;
61,509✔
64
        break;
61,509✔
65
      }
66
    }
67
    pWp->nCols = nCols;
61,509✔
68
    pWp->version = ver;
61,509✔
69
  }
70
  return 0;
3,095,879✔
71
}
72

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

89
int32_t dropTableExtSchema(SMetaEntry *pEntry, int32_t dropColId, int32_t newColNum) {
89,905✔
90
  // no ext schema, no need to drop
91
  if (!pEntry->pExtSchemas) return 0;
89,905✔
92
  if (dropColId == newColNum) {
9,053✔
93
    // drop the last column
94
    pEntry->pExtSchemas[dropColId - 1] = (SExtSchema){0};
3,479✔
95
  } else {
96
    // drop a column in the middle
97
    memmove(pEntry->pExtSchemas + dropColId, pEntry->pExtSchemas + dropColId + 1,
5,574✔
98
            (newColNum - dropColId) * sizeof(SExtSchema));
5,574✔
99
  }
100
  for (int32_t i = 0; i < newColNum; i++) {
34,714✔
101
    if (hasExtSchema(pEntry->pExtSchemas + i)) return 0;
33,330✔
102
  }
103
  taosMemoryFreeClear(pEntry->pExtSchemas);
1,384✔
104
  return 0;
1,384✔
105
}
106

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

116
    SColRef *pCol = p + nCols;
55,028✔
117
    if (NULL == pColRef) {
55,028✔
118
      pCol->hasRef = false;
5,295✔
119
      pCol->id = pSchema->colId;
5,295✔
120
    } else {
121
      pCol->hasRef = pColRef->hasRef;
49,733✔
122
      pCol->id = pSchema->colId;
49,733✔
123
      if (pCol->hasRef) {
49,733✔
124
        tstrncpy(pCol->refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
20,295✔
125
        tstrncpy(pCol->refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
20,295✔
126
        tstrncpy(pCol->refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
20,295✔
127
      }
128
    }
129
    pWp->nCols = nCols + 1;
55,028✔
130
    pWp->version++;
55,028✔
131
  } else {
132
    for (int32_t i = 0; i < nCols; i++) {
33,275,790✔
133
      SColRef *pOColRef = &pWp->pColRef[i];
33,275,790✔
134
      if (pOColRef->id == pSchema->colId) {
33,275,790✔
135
        int32_t left = (nCols - i - 1) * sizeof(SColRef);
31,966✔
136
        if (left) {
31,966✔
137
          memmove(pWp->pColRef + i, pWp->pColRef + i + 1, left);
29,724✔
138
        }
139
        nCols--;
31,966✔
140
        break;
31,966✔
141
      }
142
    }
143
    pWp->nCols = nCols;
31,966✔
144
    pWp->version++;
31,966✔
145
  }
146
  return 0;
86,994✔
147
}
148

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

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

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

169
  memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
8,706,914✔
170

171
  return 0;
8,706,914✔
172
}
173

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

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

194
    pMetaRsp->numOfColumns = pSchema->nCols;
380,437✔
195
    pMetaRsp->sversion = pSchema->version;
380,437✔
196
    memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
380,437✔
197
  }
198
  pMetaRsp->pColRefs = taosMemoryMalloc(pRef->nCols * sizeof(SColRef));
575,405✔
199
  if (NULL == pMetaRsp->pColRefs) {
575,405✔
200
    code = terrno;
×
201
    goto _return;
×
202
  }
203

204
  pColRefHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
575,405✔
205
  if (!pColRefHash) {
575,405✔
206
    code = terrno;
×
207
    goto _return;
×
208
  }
209

210
  for (int32_t i = 0; i < pRef->nCols; i++) {
204,864,968✔
211
    SColRef* pColRef = &pRef->pColRef[i];
204,289,563✔
212
    if (pColRef->hasRef) {
204,289,563✔
213
      code = taosHashPut(pColRefHash, pColRef->refTableName, strlen(pColRef->refTableName), NULL, 0);
135,524,654✔
214
      if (code) {
135,524,654✔
215
        goto _return;
×
216
      }
217
    }
218
  }
219

220
  if (taosHashGetSize(pColRefHash) > 2000) {
575,405✔
221
    code = TSDB_CODE_VTABLE_TOO_MANY_REFERENCE;
1,012✔
222
    goto _return;
1,012✔
223
  }
224

225
  memcpy(pMetaRsp->pColRefs, pRef->pColRef, pRef->nCols * sizeof(SColRef));
574,393✔
226
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
574,393✔
227
  if (tableType == TSDB_VIRTUAL_NORMAL_TABLE) {
574,393✔
228
    pMetaRsp->tuid = pEntry->uid;
327,624✔
229
  } else if (tableType == TSDB_VIRTUAL_CHILD_TABLE) {
246,769✔
230
    pMetaRsp->tuid = pEntry->uid;
246,769✔
231
    pMetaRsp->suid = pEntry->ctbEntry.suid;
246,769✔
232
  }
233

234
  pMetaRsp->tableType = tableType;
574,393✔
235
  pMetaRsp->virtualStb = false; // super table will never be processed here
574,393✔
236
  pMetaRsp->numOfColRefs = pRef->nCols;
574,393✔
237
  pMetaRsp->rversion = pRef->version;
574,393✔
238

239
  taosHashCleanup(pColRefHash);
574,393✔
240
  return code;
574,393✔
241
_return:
1,012✔
242
  taosHashCleanup(pColRefHash);
1,012✔
243
  taosMemoryFreeClear(pMetaRsp->pSchemaExt);
1,012✔
244
  taosMemoryFreeClear(pMetaRsp->pSchemas);
1,012✔
245
  taosMemoryFreeClear(pMetaRsp->pColRefs);
1,012✔
246
  return code;
1,012✔
247
}
248

249
int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
245,577✔
250
  int32_t code = 0;
245,577✔
251

252
#ifdef USE_INVERTED_INDEX
253
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
245,577✔
UNCOV
254
    return TSDB_CODE_INVALID_PARA;
×
255
  }
256
  void       *data = pCtbEntry->ctbEntry.pTags;
245,577✔
257
  const char *tagName = pSchema->name;
245,577✔
258

259
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
245,577✔
260
  tb_uid_t    tuid = pCtbEntry->uid;
245,577✔
261
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
245,577✔
262
  int32_t     nTagData = 0;
245,577✔
263

264
  SArray *pTagVals = NULL;
245,577✔
265
  code = tTagToValArray((const STag *)data, &pTagVals);
245,577✔
266
  if (code) {
245,577✔
UNCOV
267
    return code;
×
268
  }
269

270
  SIndexMultiTerm *terms = indexMultiTermCreate();
245,577✔
271
  if (terms == NULL) {
245,577✔
UNCOV
272
    return terrno;
×
273
  }
274

275
  int16_t nCols = taosArrayGetSize(pTagVals);
245,577✔
276
  for (int i = 0; i < nCols; i++) {
600,834✔
277
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
355,257✔
278
    char     type = pTagVal->type;
355,257✔
279

280
    char   *key = pTagVal->pKey;
355,257✔
281
    int32_t nKey = strlen(key);
355,257✔
282

283
    SIndexTerm *term = NULL;
355,257✔
284
    if (type == TSDB_DATA_TYPE_NULL) {
355,257✔
285
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
2,616✔
286
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
352,641✔
287
      if (pTagVal->nData > 0) {
335,726✔
288
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
333,982✔
289
        if (val == NULL) {
333,982✔
UNCOV
290
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
291
        }
292
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
333,982✔
293
        if (len < 0) {
333,982✔
UNCOV
294
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
295
        }
296
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
333,982✔
297
        type = TSDB_DATA_TYPE_VARCHAR;
333,982✔
298
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
333,982✔
299
        taosMemoryFree(val);
333,982✔
300
      } else if (pTagVal->nData == 0) {
1,744✔
301
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
1,744✔
302
      }
303
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
16,915✔
304
      double val = *(double *)(&pTagVal->i64);
13,427✔
305
      int    len = sizeof(val);
13,427✔
306
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
13,427✔
307
    } else if (type == TSDB_DATA_TYPE_BOOL) {
3,488✔
308
      int val = *(int *)(&pTagVal->i64);
3,488✔
309
      int len = sizeof(val);
3,488✔
310
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
3,488✔
311
    }
312

313
    if (term != NULL) {
355,257✔
314
      int32_t ret = indexMultiTermAdd(terms, term);
355,257✔
315
      if (ret < 0) {
355,257✔
UNCOV
316
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
317
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
318
      }
319
    } else {
UNCOV
320
      code = terrno;
×
UNCOV
321
      goto _exception;
×
322
    }
323
  }
324
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
245,577✔
325
  indexMultiTermDestroy(terms);
245,577✔
326

327
  taosArrayDestroy(pTagVals);
245,577✔
328
  return code;
245,577✔
UNCOV
329
_exception:
×
UNCOV
330
  indexMultiTermDestroy(terms);
×
331
  taosArrayDestroy(pTagVals);
×
332
#endif
333
  return code;
×
334
}
335
int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
110,837✔
336
int32_t code = 0;
110,837✔
337
#ifdef USE_INVERTED_INDEX
338
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
110,837✔
UNCOV
339
    return TSDB_CODE_INVALID_PARA;
×
340
  }
341
  void       *data = pCtbEntry->ctbEntry.pTags;
110,837✔
342
  const char *tagName = pSchema->name;
110,837✔
343

344
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
110,837✔
345
  tb_uid_t    tuid = pCtbEntry->uid;
110,837✔
346
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
110,837✔
347
  int32_t     nTagData = 0;
110,837✔
348

349
  SArray *pTagVals = NULL;
110,837✔
350
  code = tTagToValArray((const STag *)data, &pTagVals);
110,837✔
351
  if (code) {
110,837✔
UNCOV
352
    return code;
×
353
  }
354

355
  SIndexMultiTerm *terms = indexMultiTermCreate();
110,837✔
356
  if (terms == NULL) {
110,837✔
UNCOV
357
    return terrno;
×
358
  }
359

360
  int16_t nCols = taosArrayGetSize(pTagVals);
110,837✔
361
  for (int i = 0; i < nCols; i++) {
317,795✔
362
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
206,958✔
363
    char     type = pTagVal->type;
206,958✔
364

365
    char   *key = pTagVal->pKey;
206,958✔
366
    int32_t nKey = strlen(key);
206,958✔
367

368
    SIndexTerm *term = NULL;
206,958✔
369
    if (type == TSDB_DATA_TYPE_NULL) {
206,958✔
UNCOV
370
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
×
371
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
206,958✔
372
      if (pTagVal->nData > 0) {
204,778✔
373
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
204,778✔
374
        if (val == NULL) {
204,778✔
UNCOV
375
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
376
        }
377
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
204,778✔
378
        if (len < 0) {
204,778✔
UNCOV
379
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
380
        }
381
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
204,778✔
382
        type = TSDB_DATA_TYPE_VARCHAR;
204,778✔
383
        term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, val, len);
204,778✔
384
        taosMemoryFree(val);
204,778✔
UNCOV
385
      } else if (pTagVal->nData == 0) {
×
UNCOV
386
        term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
×
387
      }
388
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
2,180✔
389
      double val = *(double *)(&pTagVal->i64);
1,308✔
390
      int    len = sizeof(val);
1,308✔
391
      term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, (const char *)&val, len);
1,308✔
392
    } else if (type == TSDB_DATA_TYPE_BOOL) {
872✔
393
      int val = *(int *)(&pTagVal->i64);
872✔
394
      int len = sizeof(val);
872✔
395
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
872✔
396
    }
397
    if (term != NULL) {
206,958✔
398
      int32_t ret = indexMultiTermAdd(terms, term);
206,958✔
399
      if (ret < 0) {
206,958✔
UNCOV
400
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
401
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
402
      }
403
    } else {
UNCOV
404
      code = terrno;
×
UNCOV
405
      goto _exception;
×
406
    }
407
  }
408
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
110,837✔
409
  indexMultiTermDestroy(terms);
110,837✔
410
  taosArrayDestroy(pTagVals);
110,837✔
411
  return code;
110,837✔
UNCOV
412
_exception:
×
UNCOV
413
  indexMultiTermDestroy(terms);
×
414
  taosArrayDestroy(pTagVals);
×
415
#endif
416
  return code;
×
417
}
418

419
static int32_t metaDropTables(SMeta *pMeta, SArray *tbUids) {
19,298✔
420
  int32_t code = 0;
19,298✔
421
  if (taosArrayGetSize(tbUids) == 0) return TSDB_CODE_SUCCESS;
19,298✔
422

423
  int64_t    nCtbDropped = 0;
19,298✔
424
  SSHashObj *suidHash = tSimpleHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
19,298✔
425
  if (suidHash == NULL) {
19,298✔
UNCOV
426
    return terrno;
×
427
  }
428

429
  metaWLock(pMeta);
19,298✔
430
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
97,687✔
431
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
78,389✔
432
    tb_uid_t suid = 0;
78,389✔
433
    int8_t   sysTbl = 0;
78,389✔
434
    int      type;
78,389✔
435
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
78,389✔
436
    if (code) return code;
78,389✔
437
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
78,389✔
438
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
72,976✔
439
      if (pVal) {
72,976✔
440
        nCtbDropped = *pVal + 1;
56,519✔
441
      } else {
442
        nCtbDropped = 1;
16,457✔
443
      }
444
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
72,976✔
445
      if (code) return code;
72,976✔
446
    }
447
    /*
448
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
449
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
450
    }
451
    */
452
    metaDebug("batch drop table:%" PRId64, uid);
78,389✔
453
  }
454
  metaULock(pMeta);
19,298✔
455

456
  // update timeseries
457
  void   *pCtbDropped = NULL;
19,298✔
458
  int32_t iter = 0;
19,298✔
459
  while ((pCtbDropped = tSimpleHashIterate(suidHash, pCtbDropped, &iter))) {
35,755✔
460
    tb_uid_t    *pSuid = tSimpleHashGetKey(pCtbDropped, NULL);
16,457✔
461
    int32_t      nCols = 0;
16,457✔
462
    int8_t       flags = 0;
16,457✔
463
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
16,457✔
464
    if (metaGetStbStats(pMeta->pVnode, *pSuid, NULL, &nCols, &flags) == 0) {
16,457✔
465
      if (!TABLE_IS_VIRTUAL(flags)) {
16,457✔
466
        pStats->numOfTimeSeries -= *(int64_t *)pCtbDropped * (nCols - 1);
16,457✔
467
      }
468
    }
469
  }
470
  tSimpleHashCleanup(suidHash);
19,298✔
471

472
  pMeta->changed = true;
19,298✔
473
  return 0;
19,298✔
474
}
475

476
static int32_t metaFilterTableByHash(SMeta *pMeta, SArray *uidList) {
23,546✔
477
  int32_t code = 0;
23,546✔
478
  // 1, tranverse table's
479
  // 2, validate table name using vnodeValidateTableHash
480
  // 3, push invalidated table's uid into uidList
481

482
  TBC *pCur;
23,546✔
483
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
23,546✔
484
  if (code < 0) {
23,546✔
UNCOV
485
    return code;
×
486
  }
487

488
  code = tdbTbcMoveToFirst(pCur);
23,546✔
489
  if (code) {
23,546✔
UNCOV
490
    tdbTbcClose(pCur);
×
UNCOV
491
    return code;
×
492
  }
493

494
  void *pData = NULL, *pKey = NULL;
23,546✔
495
  int   nData = 0, nKey = 0;
23,546✔
496

497
  while (1) {
174,662✔
498
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
198,208✔
499
    if (ret < 0) {
198,208✔
500
      break;
23,546✔
501
    }
502

503
    SMetaEntry me = {0};
174,662✔
504
    SDecoder   dc = {0};
174,662✔
505
    tDecoderInit(&dc, pData, nData);
174,662✔
506
    code = metaDecodeEntry(&dc, &me);
174,662✔
507
    if (code < 0) {
174,662✔
UNCOV
508
      tDecoderClear(&dc);
×
UNCOV
509
      return code;
×
510
    }
511

512
    if (me.type != TSDB_SUPER_TABLE) {
174,662✔
513
      char tbFName[TSDB_TABLE_FNAME_LEN + 1];
156,753✔
514
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pMeta->pVnode->config.dbname, me.name);
156,753✔
515
      tbFName[TSDB_TABLE_FNAME_LEN] = '\0';
156,753✔
516
      if (pMeta->pVnode->mounted) tTrimMountPrefix(tbFName);
156,753✔
517
      ret = vnodeValidateTableHash(pMeta->pVnode, tbFName);
156,753✔
518
      if (ret < 0 && terrno == TSDB_CODE_VND_HASH_MISMATCH) {
156,753✔
519
        if (taosArrayPush(uidList, &me.uid) == NULL) {
78,389✔
UNCOV
520
          code = terrno;
×
UNCOV
521
          break;
×
522
        }
523
      }
524
    }
525
    tDecoderClear(&dc);
174,662✔
526
  }
527
  tdbFree(pData);
23,546✔
528
  tdbFree(pKey);
23,546✔
529
  tdbTbcClose(pCur);
23,546✔
530

531
  return 0;
23,546✔
532
}
533

534
int32_t metaTrimTables(SMeta *pMeta, int64_t version) {
23,546✔
535
  int32_t code = 0;
23,546✔
536

537
  SArray *tbUids = taosArrayInit(8, sizeof(int64_t));
23,546✔
538
  if (tbUids == NULL) {
23,546✔
UNCOV
539
    return terrno;
×
540
  }
541

542
  code = metaFilterTableByHash(pMeta, tbUids);
23,546✔
543
  if (code != 0) {
23,546✔
UNCOV
544
    goto end;
×
545
  }
546
  if (TARRAY_SIZE(tbUids) == 0) {
23,546✔
547
    goto end;
4,248✔
548
  }
549

550
  metaInfo("vgId:%d, trim %ld tables", TD_VID(pMeta->pVnode), taosArrayGetSize(tbUids));
19,298✔
551
  code = metaDropTables(pMeta, tbUids);
19,298✔
552
  if (code) goto end;
19,298✔
553

554
end:
19,298✔
555
  taosArrayDestroy(tbUids);
23,546✔
556

557
  return code;
23,546✔
558
}
559

560
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
9,275,226✔
561
  metaRLock(pMeta);
9,275,226✔
562

563
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
9,283,876✔
564

565
  metaULock(pMeta);
9,262,569✔
566

567
  if (ret != 0) {
9,264,926✔
UNCOV
568
    metaError("ttl failed to find expired table, ret:%d", ret);
×
569
  }
570

571
  return ret;
9,267,912✔
572
}
573

574
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
78,389✔
575
  int64_t btime;
576
  if (pME->type == TSDB_CHILD_TABLE) {
78,389✔
577
    btime = pME->ctbEntry.btime;
72,976✔
578
  } else if (pME->type == TSDB_NORMAL_TABLE) {
5,413✔
579
    btime = pME->ntbEntry.btime;
5,413✔
580
  } else {
UNCOV
581
    return TSDB_CODE_FAILED;
×
582
  }
583

584
  btimeKey->btime = btime;
78,389✔
585
  btimeKey->uid = pME->uid;
78,389✔
586
  return 0;
78,389✔
587
}
588

589
static int metaBuildNColIdxKey(SNcolIdxKey *ncolKey, const SMetaEntry *pME) {
5,413✔
590
  if (pME->type == TSDB_NORMAL_TABLE) {
5,413✔
591
    ncolKey->ncol = pME->ntbEntry.schemaRow.nCols;
5,413✔
592
    ncolKey->uid = pME->uid;
5,413✔
593
  } else {
UNCOV
594
    return TSDB_CODE_FAILED;
×
595
  }
596
  return 0;
5,413✔
597
}
598

599
static void metaDeleteTtl(SMeta *pMeta, const SMetaEntry *pME) {
78,389✔
600
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
78,389✔
601

602
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
78,389✔
603
  if (pME->type == TSDB_CHILD_TABLE) {
78,389✔
604
    ctx.ttlDays = pME->ctbEntry.ttlDays;
72,976✔
605
  } else {
606
    ctx.ttlDays = pME->ntbEntry.ttlDays;
5,413✔
607
  }
608

609
  int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
78,389✔
610
  if (ret < 0) {
78,389✔
UNCOV
611
    metaError("vgId:%d, failed to delete ttl for table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pME->name,
×
612
              pME->uid, tstrerror(ret));
613
  }
614
  return;
78,389✔
615
}
616

617
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl) {
78,389✔
618
  void      *pData = NULL;
78,389✔
619
  int        nData = 0;
78,389✔
620
  int        rc = 0;
78,389✔
621
  SMetaEntry e = {0};
78,389✔
622
  SDecoder   dc = {0};
78,389✔
623
  int32_t    ret = 0;
78,389✔
624

625
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
78,389✔
626
  if (rc < 0) {
78,389✔
UNCOV
627
    return rc;
×
628
  }
629
  int64_t version = ((SUidIdxVal *)pData)[0].version;
78,389✔
630

631
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
78,389✔
632
  if (rc < 0) {
78,389✔
UNCOV
633
    tdbFree(pData);
×
UNCOV
634
    return rc;
×
635
  }
636

637
  tDecoderInit(&dc, pData, nData);
78,389✔
638
  rc = metaDecodeEntry(&dc, &e);
78,389✔
639
  if (rc < 0) {
78,389✔
UNCOV
640
    tDecoderClear(&dc);
×
UNCOV
641
    return rc;
×
642
  }
643

644
  if (type) *type = e.type;
78,389✔
645

646
  if (e.type == TSDB_CHILD_TABLE) {
78,389✔
647
    if (pSuid) *pSuid = e.ctbEntry.suid;
72,976✔
648
    void *tData = NULL;
72,976✔
649
    int   tLen = 0;
72,976✔
650

651
    if (tdbTbGet(pMeta->pUidIdx, &e.ctbEntry.suid, sizeof(tb_uid_t), &tData, &tLen) == 0) {
72,976✔
652
      STbDbKey tbDbKey = {.uid = e.ctbEntry.suid, .version = ((SUidIdxVal *)tData)[0].version};
72,976✔
653
      if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &tData, &tLen) == 0) {
72,976✔
654
        SDecoder   tdc = {0};
72,976✔
655
        SMetaEntry stbEntry = {0};
72,976✔
656

657
        tDecoderInit(&tdc, tData, tLen);
72,976✔
658
        ret = metaDecodeEntry(&tdc, &stbEntry);
72,976✔
659
        if (ret < 0) {
72,976✔
UNCOV
660
          tDecoderClear(&tdc);
×
UNCOV
661
          metaError("vgId:%d, failed to decode child table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
662
                    e.ctbEntry.suid, tstrerror(ret));
663
          return ret;
×
664
        }
665

666
        if (pSysTbl) *pSysTbl = metaTbInFilterCache(pMeta, stbEntry.name, 1) ? 1 : 0;
72,976✔
667
        
668
        ret = metaStableTagFilterCacheUpdateUid(
72,976✔
669
          pMeta, &e, &stbEntry, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
670
        if (ret < 0) {
72,976✔
UNCOV
671
          metaError("vgId:%d, failed to update stable tag filter cache:%s "
×
672
            "uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
673
            e.ctbEntry.suid, tstrerror(ret));
674
        }
675

676
        SSchema        *pTagColumn = NULL;
72,976✔
677
        SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
72,976✔
678
        if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
72,976✔
UNCOV
679
          pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
×
UNCOV
680
          ret = metaDelJsonVarFromIdx(pMeta, &e, pTagColumn);
×
681
          if (ret < 0) {
×
682
            metaError("vgId:%d, failed to delete json var from idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
683
                      e.name, e.uid, tstrerror(ret));
684
          }
685
        } else {
686
          for (int i = 0; i < pTagSchema->nCols; i++) {
375,740✔
687
            pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[i];
302,764✔
688
            if (!IS_IDX_ON(pTagColumn)) continue;
302,764✔
689
            STagIdxKey *pTagIdxKey = NULL;
72,976✔
690
            int32_t     nTagIdxKey;
72,976✔
691

692
            const void *pTagData = NULL;
72,976✔
693
            int32_t     nTagData = 0;
72,976✔
694

695
            STagVal tagVal = {.cid = pTagColumn->colId};
72,976✔
696
            if (tTagGet((const STag *)e.ctbEntry.pTags, &tagVal)) {
72,976✔
697
              if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
72,976✔
UNCOV
698
                pTagData = tagVal.pData;
×
UNCOV
699
                nTagData = (int32_t)tagVal.nData;
×
700
              } else {
701
                pTagData = &(tagVal.i64);
72,976✔
702
                nTagData = tDataTypes[pTagColumn->type].bytes;
72,976✔
703
              }
704
            } else {
UNCOV
705
              if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
×
UNCOV
706
                nTagData = tDataTypes[pTagColumn->type].bytes;
×
707
              }
708
            }
709

710
            if (metaCreateTagIdxKey(e.ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type, uid,
72,976✔
711
                                    &pTagIdxKey, &nTagIdxKey) == 0) {
712
              ret = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
72,976✔
713
              if (ret < 0) {
72,976✔
UNCOV
714
                metaError("vgId:%d, failed to delete tag idx key:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
715
                          e.name, e.uid, tstrerror(ret));
716
              }
717
            }
718
            metaDestroyTagIdxKey(pTagIdxKey);
72,976✔
719
            pTagIdxKey = NULL;
72,976✔
720
          }
721
        }
722
        tDecoderClear(&tdc);
72,976✔
723
      }
724
      tdbFree(tData);
72,976✔
725
    }
726
  }
727

728
  ret = tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), pMeta->txn);
78,389✔
729
  if (ret < 0) {
78,389✔
UNCOV
730
    metaError("vgId:%d, failed to delete table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
731
              tstrerror(ret));
732
  }
733
  ret = tdbTbDelete(pMeta->pNameIdx, e.name, strlen(e.name) + 1, pMeta->txn);
78,389✔
734
  if (ret < 0) {
78,389✔
UNCOV
735
    metaError("vgId:%d, failed to delete name idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
736
              tstrerror(ret));
737
  }
738
  ret = tdbTbDelete(pMeta->pUidIdx, &uid, sizeof(uid), pMeta->txn);
78,389✔
739
  if (ret < 0) {
78,389✔
UNCOV
740
    metaError("vgId:%d, failed to delete uid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
741
              tstrerror(ret));
742
  }
743

744
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
78,389✔
745
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
78,389✔
746

747
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
78,389✔
748

749
  if (e.type == TSDB_CHILD_TABLE) {
78,389✔
750
    ret =
751
        tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
72,976✔
752
    if (ret < 0) {
72,976✔
UNCOV
753
      metaError("vgId:%d, failed to delete ctb idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
754
                tstrerror(ret));
755
    }
756

757
    --pMeta->pVnode->config.vndStats.numOfCTables;
72,976✔
758
    metaUpdateStbStats(pMeta, e.ctbEntry.suid, -1, 0, -1);
72,976✔
759
    ret = metaUidCacheClear(pMeta, e.ctbEntry.suid);
72,976✔
760
    if (ret < 0) {
72,976✔
UNCOV
761
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
762
                e.ctbEntry.suid, tstrerror(ret));
763
    }
764
    ret = metaTbGroupCacheClear(pMeta, e.ctbEntry.suid);
72,976✔
765
    if (ret < 0) {
72,976✔
UNCOV
766
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
767
                e.ctbEntry.suid, tstrerror(ret));
768
    }
769
    /*
770
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
771
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, e.ctbEntry.suid, NULL);
772
    }
773
    */
774
  } else if (e.type == TSDB_NORMAL_TABLE) {
5,413✔
775
    // drop schema.db (todo)
776

777
    --pMeta->pVnode->config.vndStats.numOfNTables;
5,413✔
778
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
5,413✔
779

780
    /*
781
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
782
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, -1, &e.ntbEntry.schemaRow);
783
    }
784
    */
UNCOV
785
  } else if (e.type == TSDB_SUPER_TABLE) {
×
UNCOV
786
    ret = tdbTbDelete(pMeta->pSuidIdx, &e.uid, sizeof(tb_uid_t), pMeta->txn);
×
787
    if (ret < 0) {
×
788
      metaError("vgId:%d, failed to delete suid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
789
                tstrerror(ret));
790
    }
791
    // drop schema.db (todo)
792

UNCOV
793
    ret = metaStatsCacheDrop(pMeta, uid);
×
UNCOV
794
    if (ret < 0) {
×
795
      metaError("vgId:%d, failed to drop stats cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
796
                tstrerror(ret));
797
    }
UNCOV
798
    ret = metaUidCacheClear(pMeta, uid);
×
UNCOV
799
    if (ret < 0) {
×
800
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
801
                tstrerror(ret));
802
    }
UNCOV
803
    ret = metaStableTagFilterCacheDropSTable(pMeta, uid);
×
UNCOV
804
    if (ret < 0) {
×
805
      metaError("vgId:%d, failed to clear stable tag filter cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
806
                e.uid, tstrerror(ret));
807
    }
UNCOV
808
    ret = metaTbGroupCacheClear(pMeta, uid);
×
UNCOV
809
    if (ret < 0) {
×
810
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
811
                e.uid, tstrerror(ret));
812
    }
UNCOV
813
    --pMeta->pVnode->config.vndStats.numOfSTables;
×
814
  }
815

816
  ret = metaCacheDrop(pMeta, uid);
78,389✔
817
  if (ret < 0) {
78,389✔
818
    metaError("vgId:%d, failed to drop cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
78,389✔
819
              tstrerror(ret));
820
  }
821

822
  tDecoderClear(&dc);
78,389✔
823
  tdbFree(pData);
78,389✔
824

825
  return 0;
78,389✔
826
}
827

828
static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
78,389✔
829
  SBtimeIdxKey btimeKey = {0};
78,389✔
830
  if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) {
78,389✔
UNCOV
831
    return 0;
×
832
  }
833
  return tdbTbDelete(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), pMeta->txn);
78,389✔
834
}
835

836
int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
5,413✔
837
  SNcolIdxKey ncolKey = {0};
5,413✔
838
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
5,413✔
UNCOV
839
    return 0;
×
840
  }
841
  return tdbTbDelete(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), pMeta->txn);
5,413✔
842
}
843

844
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
11,555,486✔
845
  pMeta->changed = true;
11,555,486✔
846
  switch (pReq->action) {
11,555,486✔
847
    case TSDB_ALTER_TABLE_ADD_COLUMN:
3,386,697✔
848
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
849
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF:
850
      return metaAddTableColumn(pMeta, version, pReq, pMetaRsp);
3,386,697✔
851
    case TSDB_ALTER_TABLE_DROP_COLUMN:
89,905✔
852
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
89,905✔
853
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
463,494✔
854
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
463,494✔
855
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
42,975✔
856
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
42,975✔
857
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
7,415,413✔
858
      return metaUpdateTableTagValue(pMeta, version, pReq);
7,415,413✔
859
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
3,160✔
860
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
3,160✔
861
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
23,824✔
862
      return metaUpdateTableOptions2(pMeta, version, pReq);
23,824✔
863
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
5,792✔
864
      return metaUpdateTableColCompress2(pMeta, version, pReq);
5,792✔
865
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
71,430✔
866
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
71,430✔
867
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
52,796✔
868
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
52,796✔
UNCOV
869
    default:
×
UNCOV
870
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
871
      break;
872
  }
873
}
874

875
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
560,778✔
876
  if (!tsTtlChangeOnWrite) return 0;
560,778✔
877

878
  if (changeTimeMs <= 0) {
560,778✔
UNCOV
879
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
×
UNCOV
880
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
×
881
  }
882

883
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
560,778✔
884

885
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
560,778✔
886
}
887

888
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
657,789,103✔
889
  if (!tsTtlChangeOnWrite) return 0;
657,789,103✔
890

891
  metaWLock(pMeta);
585,344✔
892
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
560,778✔
893
  metaULock(pMeta);
560,778✔
894
  return ret;
560,778✔
895
}
896

897
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
76,958,079✔
898
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
899
  if (IS_VAR_DATA_TYPE(type)) {
76,958,079✔
900
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
9,342,801✔
901
  } else {
902
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
67,615,278✔
903
  }
904

905
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
76,964,839✔
906
  if (*ppTagIdxKey == NULL) {
76,969,452✔
UNCOV
907
    return terrno;
×
908
  }
909

910
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
76,963,483✔
911
  (*ppTagIdxKey)->cid = cid;
76,964,395✔
912
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
76,977,413✔
913
  (*ppTagIdxKey)->type = type;
76,962,560✔
914

915
  // refactor
916
  if (IS_VAR_DATA_TYPE(type)) {
76,969,360✔
917
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
9,363,823✔
918
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
9,354,865✔
919
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
9,355,267✔
920
  } else {
921
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
67,605,537✔
922
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
67,608,809✔
923
  }
924

925
  return 0;
76,966,673✔
926
}
927

928
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
74,016,648✔
929
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
74,016,648✔
930
}
74,014,153✔
931

932
static void colCompressDebug(SHashObj *pColCmprObj) {
85,747,736✔
933
  void *p = taosHashIterate(pColCmprObj, NULL);
85,747,736✔
934
  while (p) {
727,981,835✔
935
    uint32_t cmprAlg = *(uint32_t *)p;
642,191,569✔
936
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
642,197,435✔
937
    p = taosHashIterate(pColCmprObj, p);
642,200,503✔
938

939
    uint8_t l1, l2, lvl;
642,342,742✔
940
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
642,352,893✔
941

942
    const char *l1str = columnEncodeStr(l1);
642,323,217✔
943
    const char *l2str = columnCompressStr(l2);
642,214,381✔
944
    const char *lvlstr = columnLevelStr(lvl);
642,215,036✔
945
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
642,198,162✔
946
  }
947
  return;
85,790,266✔
948
}
949

950
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
85,801,160✔
951
  int rc = 0;
85,801,160✔
952

953
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
85,801,160✔
954
  if (pColCmprObj == NULL) {
85,802,169✔
UNCOV
955
    pColCmprObj = NULL;
×
UNCOV
956
    return TSDB_CODE_OUT_OF_MEMORY;
×
957
  }
958

959
  void      *pData = NULL;
85,802,169✔
960
  int        nData = 0;
85,815,297✔
961
  SMetaEntry e = {0};
85,814,977✔
962
  SDecoder   dc = {0};
85,815,396✔
963

964
  *ppColCmprObj = NULL;
85,812,988✔
965

966
  metaRLock(pMeta);
85,800,505✔
967
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
85,826,278✔
968
  if (rc < 0) {
85,815,829✔
969
    taosHashCleanup(pColCmprObj);
41,151✔
970
    metaULock(pMeta);
41,151✔
971
    return TSDB_CODE_FAILED;
41,151✔
972
  }
973
  int64_t version = ((SUidIdxVal *)pData)[0].version;
85,774,678✔
974
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
85,775,758✔
975
  if (rc < 0) {
85,765,329✔
UNCOV
976
    metaULock(pMeta);
×
UNCOV
977
    taosHashCleanup(pColCmprObj);
×
978
    metaError("failed to get table entry");
×
979
    return rc;
×
980
  }
981

982
  tDecoderInit(&dc, pData, nData);
85,765,329✔
983
  rc = metaDecodeEntry(&dc, &e);
85,765,001✔
984
  if (rc < 0) {
85,763,750✔
UNCOV
985
    tDecoderClear(&dc);
×
UNCOV
986
    tdbFree(pData);
×
987
    metaULock(pMeta);
×
988
    taosHashCleanup(pColCmprObj);
×
989
    return rc;
×
990
  }
991
  if (withExtSchema(e.type)) {
85,763,750✔
992
    SColCmprWrapper *p = &e.colCmpr;
85,764,563✔
993
    for (int32_t i = 0; i < p->nCols; i++) {
728,057,055✔
994
      SColCmpr *pCmpr = &p->pColCmpr[i];
642,273,380✔
995
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
642,262,132✔
996
      if (rc < 0) {
642,292,492✔
UNCOV
997
        tDecoderClear(&dc);
×
UNCOV
998
        tdbFree(pData);
×
999
        metaULock(pMeta);
×
1000
        taosHashCleanup(pColCmprObj);
×
1001
        return rc;
×
1002
      }
1003
    }
1004
  } else {
UNCOV
1005
    tDecoderClear(&dc);
×
UNCOV
1006
    tdbFree(pData);
×
1007
    metaULock(pMeta);
×
1008
    taosHashCleanup(pColCmprObj);
×
1009
    return 0;
×
1010
  }
1011
  tDecoderClear(&dc);
85,739,757✔
1012
  tdbFree(pData);
85,776,681✔
1013
  metaULock(pMeta);
85,764,060✔
1014

1015
  *ppColCmprObj = pColCmprObj;
85,751,923✔
1016
  colCompressDebug(pColCmprObj);
85,765,019✔
1017

1018
  return 0;
85,789,568✔
1019
}
1020
// refactor later
1021
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
3,824,477✔
1022
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
3,824,477✔
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