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

taosdata / TDengine / #5006

29 Mar 2026 04:32AM UTC coverage: 72.274% (+0.1%) from 72.152%
#5006

push

travis-ci

web-flow
refactor: do some internal refactor for TDgpt. (#34955)

253711 of 351039 relevant lines covered (72.27%)

131490495.89 hits per line

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

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

16
#include "meta.h"
17

18
extern SDmNotifyHandle dmNotifyHdl;
19

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

50
    SColCmpr *pCol = p + nCols;
3,269,739✔
51
    pCol->id = pSchema->colId;
3,269,739✔
52
    pCol->alg = compress;
3,269,739✔
53
    pWp->nCols = nCols + 1;
3,269,739✔
54
    pWp->version = ver;
3,269,739✔
55
  } else {
56
    for (int32_t i = 0; i < nCols; i++) {
25,183,549✔
57
      SColCmpr *pOCmpr = &pWp->pColCmpr[i];
25,183,549✔
58
      if (pOCmpr->id == pSchema->colId) {
25,183,549✔
59
        int32_t left = (nCols - i - 1) * sizeof(SColCmpr);
59,796✔
60
        if (left) {
59,796✔
61
          memmove(pWp->pColCmpr + i, pWp->pColCmpr + i + 1, left);
38,802✔
62
        }
63
        nCols--;
59,796✔
64
        break;
59,796✔
65
      }
66
    }
67
    pWp->nCols = nCols;
59,796✔
68
    pWp->version = ver;
59,796✔
69
  }
70
  return 0;
3,329,535✔
71
}
72

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

89
int32_t dropTableExtSchema(SMetaEntry *pEntry, int32_t dropColId, int32_t newColNum) {
90,268✔
90
  // no ext schema, no need to drop
91
  if (!pEntry->pExtSchemas) return 0;
90,268✔
92
  if (dropColId == newColNum) {
9,707✔
93
    // drop the last column
94
    pEntry->pExtSchemas[dropColId - 1] = (SExtSchema){0};
3,731✔
95
  } else {
96
    // drop a column in the middle
97
    memmove(pEntry->pExtSchemas + dropColId, pEntry->pExtSchemas + dropColId + 1,
5,976✔
98
            (newColNum - dropColId) * sizeof(SExtSchema));
5,976✔
99
  }
100
  for (int32_t i = 0; i < newColNum; i++) {
37,246✔
101
    if (hasExtSchema(pEntry->pExtSchemas + i)) return 0;
35,760✔
102
  }
103
  taosMemoryFreeClear(pEntry->pExtSchemas);
1,486✔
104
  return 0;
1,486✔
105
}
106

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

116
    SColRef *pCol = p + nCols;
60,741✔
117
    if (NULL == pColRef) {
60,741✔
118
      pCol->hasRef = false;
5,727✔
119
      pCol->id = pSchema->colId;
5,727✔
120
    } else {
121
      pCol->hasRef = pColRef->hasRef;
55,014✔
122
      pCol->id = pSchema->colId;
55,014✔
123
      if (pCol->hasRef) {
55,014✔
124
        tstrncpy(pCol->refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
23,461✔
125
        tstrncpy(pCol->refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
23,461✔
126
        tstrncpy(pCol->refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
23,461✔
127
      }
128
    }
129
    pWp->nCols = nCols + 1;
60,741✔
130
    pWp->version++;
60,741✔
131
  } else {
132
    for (int32_t i = 0; i < nCols; i++) {
37,674,615✔
133
      SColRef *pOColRef = &pWp->pColRef[i];
37,674,615✔
134
      if (pOColRef->id == pSchema->colId) {
37,674,615✔
135
        int32_t left = (nCols - i - 1) * sizeof(SColRef);
34,348✔
136
        if (left) {
34,348✔
137
          memmove(pWp->pColRef + i, pWp->pColRef + i + 1, left);
31,780✔
138
        }
139
        nCols--;
34,348✔
140
        break;
34,348✔
141
      }
142
    }
143
    pWp->nCols = nCols;
34,348✔
144
    pWp->version++;
34,348✔
145
  }
146
  return 0;
95,089✔
147
}
148

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

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

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

170
  memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
10,933,148✔
171

172
  return 0;
10,933,148✔
173
}
174

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

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

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

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

211
  for (int32_t i = 0; i < pRef->nCols; i++) {
232,493,967✔
212
    SColRef* pColRef = &pRef->pColRef[i];
231,717,687✔
213
    if (pColRef->hasRef) {
231,717,687✔
214
      code = taosHashPut(pColRefHash, pColRef->refTableName, strlen(pColRef->refTableName), NULL, 0);
153,710,740✔
215
      if (code) {
153,710,740✔
216
        goto _return;
×
217
      }
218
    }
219
  }
220

221
  memcpy(pMetaRsp->pColRefs, pRef->pColRef, pRef->nCols * sizeof(SColRef));
776,280✔
222
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
776,280✔
223
  if (tableType == TSDB_VIRTUAL_NORMAL_TABLE) {
776,280✔
224
    pMetaRsp->tuid = pEntry->uid;
399,043✔
225
  } else if (tableType == TSDB_VIRTUAL_CHILD_TABLE) {
377,237✔
226
    pMetaRsp->tuid = pEntry->uid;
377,237✔
227
    pMetaRsp->suid = pEntry->ctbEntry.suid;
377,237✔
228
  }
229

230
  pMetaRsp->tableType = tableType;
776,280✔
231
  pMetaRsp->virtualStb = false; // super table will never be processed here
776,280✔
232
  pMetaRsp->numOfColRefs = pRef->nCols;
776,280✔
233
  pMetaRsp->rversion = pRef->version;
776,280✔
234
  if (ownerId != 0) pMetaRsp->ownerId = ownerId;
776,280✔
235

236
  // Populate tag references
237
  if (pRef->nTagRefs > 0 && pRef->pTagRef) {
776,280✔
238
    pMetaRsp->pTagRefs = taosMemoryMalloc(pRef->nTagRefs * sizeof(SColRef));
×
239
    if (NULL == pMetaRsp->pTagRefs) {
×
240
      code = terrno;
×
241
      goto _return;
×
242
    }
243
    memcpy(pMetaRsp->pTagRefs, pRef->pTagRef, pRef->nTagRefs * sizeof(SColRef));
×
244
    pMetaRsp->numOfTagRefs = pRef->nTagRefs;
×
245
  } else {
246
    pMetaRsp->pTagRefs = NULL;
776,280✔
247
    pMetaRsp->numOfTagRefs = 0;
776,280✔
248
  }
249

250
  taosHashCleanup(pColRefHash);
776,280✔
251
  return code;
776,280✔
252
_return:
×
253
  taosHashCleanup(pColRefHash);
×
254
  taosMemoryFreeClear(pMetaRsp->pSchemaExt);
×
255
  taosMemoryFreeClear(pMetaRsp->pSchemas);
×
256
  taosMemoryFreeClear(pMetaRsp->pColRefs);
×
257
  taosMemoryFreeClear(pMetaRsp->pTagRefs);
×
258
  return code;
×
259
}
260

261
int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
261,164✔
262
  int32_t code = 0;
261,164✔
263

264
#ifdef USE_INVERTED_INDEX
265
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
261,164✔
266
    return TSDB_CODE_INVALID_PARA;
×
267
  }
268
  void       *data = pCtbEntry->ctbEntry.pTags;
261,164✔
269
  const char *tagName = pSchema->name;
261,164✔
270

271
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
261,164✔
272
  tb_uid_t    tuid = pCtbEntry->uid;
261,164✔
273
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
261,164✔
274
  int32_t     nTagData = 0;
261,164✔
275

276
  SArray *pTagVals = NULL;
261,164✔
277
  code = tTagToValArray((const STag *)data, &pTagVals);
261,164✔
278
  if (code) {
261,164✔
279
    return code;
×
280
  }
281

282
  SIndexMultiTerm *terms = indexMultiTermCreate();
261,164✔
283
  if (terms == NULL) {
261,164✔
284
    return terrno;
×
285
  }
286

287
  int16_t nCols = taosArrayGetSize(pTagVals);
261,164✔
288
  for (int i = 0; i < nCols; i++) {
636,943✔
289
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
375,779✔
290
    char     type = pTagVal->type;
375,779✔
291

292
    char   *key = pTagVal->pKey;
375,779✔
293
    int32_t nKey = strlen(key);
375,779✔
294

295
    SIndexTerm *term = NULL;
375,779✔
296
    if (type == TSDB_DATA_TYPE_NULL) {
375,779✔
297
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
2,826✔
298
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
372,953✔
299
      if (pTagVal->nData > 0) {
356,059✔
300
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
354,175✔
301
        if (val == NULL) {
354,175✔
302
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
303
        }
304
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
354,175✔
305
        if (len < 0) {
354,175✔
306
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
307
        }
308
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
354,175✔
309
        type = TSDB_DATA_TYPE_VARCHAR;
354,175✔
310
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
354,175✔
311
        taosMemoryFree(val);
354,175✔
312
      } else if (pTagVal->nData == 0) {
1,884✔
313
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
1,884✔
314
      }
315
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
16,894✔
316
      double val = *(double *)(&pTagVal->i64);
13,126✔
317
      int    len = sizeof(val);
13,126✔
318
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
13,126✔
319
    } else if (type == TSDB_DATA_TYPE_BOOL) {
3,768✔
320
      int val = *(int *)(&pTagVal->i64);
3,768✔
321
      int len = sizeof(val);
3,768✔
322
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
3,768✔
323
    }
324

325
    if (term != NULL) {
375,779✔
326
      int32_t ret = indexMultiTermAdd(terms, term);
375,779✔
327
      if (ret < 0) {
375,779✔
328
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
329
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
330
      }
331
    } else {
332
      code = terrno;
×
333
      goto _exception;
×
334
    }
335
  }
336
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
261,164✔
337
  indexMultiTermDestroy(terms);
261,164✔
338

339
  taosArrayDestroy(pTagVals);
261,164✔
340
  return code;
261,164✔
341
_exception:
×
342
  indexMultiTermDestroy(terms);
×
343
  taosArrayDestroy(pTagVals);
×
344
#endif
345
  return code;
×
346
}
347
int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
117,212✔
348
int32_t code = 0;
117,212✔
349
#ifdef USE_INVERTED_INDEX
350
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
117,212✔
351
    return TSDB_CODE_INVALID_PARA;
×
352
  }
353
  void       *data = pCtbEntry->ctbEntry.pTags;
117,212✔
354
  const char *tagName = pSchema->name;
117,212✔
355

356
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
117,212✔
357
  tb_uid_t    tuid = pCtbEntry->uid;
117,212✔
358
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
117,212✔
359
  int32_t     nTagData = 0;
117,212✔
360

361
  SArray *pTagVals = NULL;
117,212✔
362
  code = tTagToValArray((const STag *)data, &pTagVals);
117,212✔
363
  if (code) {
117,212✔
364
    return code;
×
365
  }
366

367
  SIndexMultiTerm *terms = indexMultiTermCreate();
117,212✔
368
  if (terms == NULL) {
117,212✔
369
    return terrno;
×
370
  }
371

372
  int16_t nCols = taosArrayGetSize(pTagVals);
117,212✔
373
  for (int i = 0; i < nCols; i++) {
336,064✔
374
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
218,852✔
375
    char     type = pTagVal->type;
218,852✔
376

377
    char   *key = pTagVal->pKey;
218,852✔
378
    int32_t nKey = strlen(key);
218,852✔
379

380
    SIndexTerm *term = NULL;
218,852✔
381
    if (type == TSDB_DATA_TYPE_NULL) {
218,852✔
382
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
×
383
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
218,852✔
384
      if (pTagVal->nData > 0) {
216,497✔
385
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
216,497✔
386
        if (val == NULL) {
216,497✔
387
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
388
        }
389
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
216,497✔
390
        if (len < 0) {
216,497✔
391
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
392
        }
393
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
216,497✔
394
        type = TSDB_DATA_TYPE_VARCHAR;
216,497✔
395
        term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, val, len);
216,497✔
396
        taosMemoryFree(val);
216,497✔
397
      } else if (pTagVal->nData == 0) {
×
398
        term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
×
399
      }
400
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
2,355✔
401
      double val = *(double *)(&pTagVal->i64);
1,413✔
402
      int    len = sizeof(val);
1,413✔
403
      term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, (const char *)&val, len);
1,413✔
404
    } else if (type == TSDB_DATA_TYPE_BOOL) {
942✔
405
      int val = *(int *)(&pTagVal->i64);
942✔
406
      int len = sizeof(val);
942✔
407
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
942✔
408
    }
409
    if (term != NULL) {
218,852✔
410
      int32_t ret = indexMultiTermAdd(terms, term);
218,852✔
411
      if (ret < 0) {
218,852✔
412
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
413
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
414
      }
415
    } else {
416
      code = terrno;
×
417
      goto _exception;
×
418
    }
419
  }
420
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
117,212✔
421
  indexMultiTermDestroy(terms);
117,212✔
422
  taosArrayDestroy(pTagVals);
117,212✔
423
  return code;
117,212✔
424
_exception:
×
425
  indexMultiTermDestroy(terms);
×
426
  taosArrayDestroy(pTagVals);
×
427
#endif
428
  return code;
×
429
}
430

431
static int32_t metaDropTables(SMeta *pMeta, SArray *tbUids) {
19,590✔
432
  int32_t code = 0;
19,590✔
433
  if (taosArrayGetSize(tbUids) == 0) return TSDB_CODE_SUCCESS;
19,590✔
434

435
  int64_t    nCtbDropped = 0;
19,590✔
436
  SSHashObj *suidHash = tSimpleHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
19,590✔
437
  if (suidHash == NULL) {
19,590✔
438
    return terrno;
×
439
  }
440

441
  metaWLock(pMeta);
19,590✔
442
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
99,733✔
443
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
80,143✔
444
    tb_uid_t suid = 0;
80,143✔
445
    int8_t   sysTbl = 0;
80,143✔
446
    int      type;
80,143✔
447
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
80,143✔
448
    if (code) return code;
80,143✔
449
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
80,143✔
450
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
73,603✔
451
      if (pVal) {
73,603✔
452
        nCtbDropped = *pVal + 1;
57,775✔
453
      } else {
454
        nCtbDropped = 1;
15,828✔
455
      }
456
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
73,603✔
457
      if (code) return code;
73,603✔
458
    }
459
    /*
460
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
461
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
462
    }
463
    */
464
    metaDebug("batch drop table:%" PRId64, uid);
80,143✔
465
  }
466
  metaULock(pMeta);
19,590✔
467

468
  // update timeseries
469
  void   *pCtbDropped = NULL;
19,590✔
470
  int32_t iter = 0;
19,590✔
471
  while ((pCtbDropped = tSimpleHashIterate(suidHash, pCtbDropped, &iter))) {
35,418✔
472
    tb_uid_t    *pSuid = tSimpleHashGetKey(pCtbDropped, NULL);
15,828✔
473
    int32_t      nCols = 0;
15,828✔
474
    int8_t       flags = 0;
15,828✔
475
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
15,828✔
476
    if (metaGetStbStats(pMeta->pVnode, *pSuid, NULL, &nCols, &flags) == 0) {
15,828✔
477
      if (!TABLE_IS_VIRTUAL(flags)) {
15,828✔
478
        pStats->numOfTimeSeries -= *(int64_t *)pCtbDropped * (nCols - 1);
15,828✔
479
      }
480
    }
481
  }
482
  tSimpleHashCleanup(suidHash);
19,590✔
483

484
  pMeta->changed = true;
19,590✔
485
  return 0;
19,590✔
486
}
487

488
static int32_t metaFilterTableByHash(SMeta *pMeta, SArray *uidList) {
24,741✔
489
  int32_t code = 0;
24,741✔
490
  // 1, tranverse table's
491
  // 2, validate table name using vnodeValidateTableHash
492
  // 3, push invalidated table's uid into uidList
493

494
  TBC *pCur;
24,741✔
495
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
24,741✔
496
  if (code < 0) {
24,741✔
497
    return code;
×
498
  }
499

500
  code = tdbTbcMoveToFirst(pCur);
24,741✔
501
  if (code) {
24,741✔
502
    tdbTbcClose(pCur);
×
503
    return code;
×
504
  }
505

506
  void *pData = NULL, *pKey = NULL;
24,741✔
507
  int   nData = 0, nKey = 0;
24,741✔
508

509
  while (1) {
179,015✔
510
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
203,756✔
511
    if (ret < 0) {
203,756✔
512
      break;
24,741✔
513
    }
514

515
    SMetaEntry me = {0};
179,015✔
516
    SDecoder   dc = {0};
179,015✔
517
    tDecoderInit(&dc, pData, nData);
179,015✔
518
    code = metaDecodeEntry(&dc, &me);
179,015✔
519
    if (code < 0) {
179,015✔
520
      tDecoderClear(&dc);
×
521
      return code;
×
522
    }
523

524
    if (me.type != TSDB_SUPER_TABLE) {
179,015✔
525
      char tbFName[TSDB_TABLE_FNAME_LEN + 1];
160,262✔
526
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pMeta->pVnode->config.dbname, me.name);
160,262✔
527
      tbFName[TSDB_TABLE_FNAME_LEN] = '\0';
160,262✔
528
      if (pMeta->pVnode->mounted) tTrimMountPrefix(tbFName);
160,262✔
529
      ret = vnodeValidateTableHash(pMeta->pVnode, tbFName);
160,262✔
530
      if (ret < 0 && terrno == TSDB_CODE_VND_HASH_MISMATCH) {
160,262✔
531
        if (taosArrayPush(uidList, &me.uid) == NULL) {
80,143✔
532
          code = terrno;
×
533
          break;
×
534
        }
535
      }
536
    }
537
    tDecoderClear(&dc);
179,015✔
538
  }
539
  tdbFree(pData);
24,741✔
540
  tdbFree(pKey);
24,741✔
541
  tdbTbcClose(pCur);
24,741✔
542

543
  return 0;
24,741✔
544
}
545

546
int32_t metaTrimTables(SMeta *pMeta, int64_t version) {
24,741✔
547
  int32_t code = 0;
24,741✔
548

549
  SArray *tbUids = taosArrayInit(8, sizeof(int64_t));
24,741✔
550
  if (tbUids == NULL) {
24,741✔
551
    return terrno;
×
552
  }
553

554
  code = metaFilterTableByHash(pMeta, tbUids);
24,741✔
555
  if (code != 0) {
24,741✔
556
    goto end;
×
557
  }
558
  if (TARRAY_SIZE(tbUids) == 0) {
24,741✔
559
    goto end;
5,151✔
560
  }
561

562
  metaInfo("vgId:%d, trim %ld tables", TD_VID(pMeta->pVnode), taosArrayGetSize(tbUids));
19,590✔
563
  code = metaDropTables(pMeta, tbUids);
19,590✔
564
  if (code) goto end;
19,590✔
565

566
end:
19,590✔
567
  taosArrayDestroy(tbUids);
24,741✔
568

569
  return code;
24,741✔
570
}
571

572
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
11,379,483✔
573
  metaRLock(pMeta);
11,379,483✔
574

575
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
11,388,587✔
576

577
  metaULock(pMeta);
11,371,312✔
578

579
  if (ret != 0) {
11,377,152✔
580
    metaError("ttl failed to find expired table, ret:%d", ret);
×
581
  }
582

583
  return ret;
11,378,938✔
584
}
585

586
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
80,143✔
587
  int64_t btime;
588
  if (pME->type == TSDB_CHILD_TABLE) {
80,143✔
589
    btime = pME->ctbEntry.btime;
73,603✔
590
  } else if (pME->type == TSDB_NORMAL_TABLE) {
6,540✔
591
    btime = pME->ntbEntry.btime;
6,540✔
592
  } else {
593
    return TSDB_CODE_FAILED;
×
594
  }
595

596
  btimeKey->btime = btime;
80,143✔
597
  btimeKey->uid = pME->uid;
80,143✔
598
  return 0;
80,143✔
599
}
600

601
static int metaBuildNColIdxKey(SNcolIdxKey *ncolKey, const SMetaEntry *pME) {
6,540✔
602
  if (pME->type == TSDB_NORMAL_TABLE) {
6,540✔
603
    ncolKey->ncol = pME->ntbEntry.schemaRow.nCols;
6,540✔
604
    ncolKey->uid = pME->uid;
6,540✔
605
  } else {
606
    return TSDB_CODE_FAILED;
×
607
  }
608
  return 0;
6,540✔
609
}
610

611
static void metaDeleteTtl(SMeta *pMeta, const SMetaEntry *pME) {
80,143✔
612
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
80,143✔
613

614
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
80,143✔
615
  if (pME->type == TSDB_CHILD_TABLE) {
80,143✔
616
    ctx.ttlDays = pME->ctbEntry.ttlDays;
73,603✔
617
  } else {
618
    ctx.ttlDays = pME->ntbEntry.ttlDays;
6,540✔
619
  }
620

621
  int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
80,143✔
622
  if (ret < 0) {
80,143✔
623
    metaError("vgId:%d, failed to delete ttl for table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pME->name,
×
624
              pME->uid, tstrerror(ret));
625
  }
626
  return;
80,143✔
627
}
628

629
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl) {
80,143✔
630
  void      *pData = NULL;
80,143✔
631
  int        nData = 0;
80,143✔
632
  int        rc = 0;
80,143✔
633
  SMetaEntry e = {0};
80,143✔
634
  SDecoder   dc = {0};
80,143✔
635
  int32_t    ret = 0;
80,143✔
636

637
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
80,143✔
638
  if (rc < 0) {
80,143✔
639
    return rc;
×
640
  }
641
  int64_t version = ((SUidIdxVal *)pData)[0].version;
80,143✔
642

643
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
80,143✔
644
  if (rc < 0) {
80,143✔
645
    tdbFree(pData);
×
646
    return rc;
×
647
  }
648

649
  tDecoderInit(&dc, pData, nData);
80,143✔
650
  rc = metaDecodeEntry(&dc, &e);
80,143✔
651
  if (rc < 0) {
80,143✔
652
    tDecoderClear(&dc);
×
653
    return rc;
×
654
  }
655

656
  if (type) *type = e.type;
80,143✔
657

658
  if (e.type == TSDB_CHILD_TABLE) {
80,143✔
659
    if (pSuid) *pSuid = e.ctbEntry.suid;
73,603✔
660
    void *tData = NULL;
73,603✔
661
    int   tLen = 0;
73,603✔
662

663
    if (tdbTbGet(pMeta->pUidIdx, &e.ctbEntry.suid, sizeof(tb_uid_t), &tData, &tLen) == 0) {
73,603✔
664
      STbDbKey tbDbKey = {.uid = e.ctbEntry.suid, .version = ((SUidIdxVal *)tData)[0].version};
73,603✔
665
      if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &tData, &tLen) == 0) {
73,603✔
666
        SDecoder   tdc = {0};
73,603✔
667
        SMetaEntry stbEntry = {0};
73,603✔
668

669
        tDecoderInit(&tdc, tData, tLen);
73,603✔
670
        ret = metaDecodeEntry(&tdc, &stbEntry);
73,603✔
671
        if (ret < 0) {
73,603✔
672
          tDecoderClear(&tdc);
×
673
          metaError("vgId:%d, failed to decode child table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
674
                    e.ctbEntry.suid, tstrerror(ret));
675
          return ret;
×
676
        }
677

678
        if (pSysTbl) *pSysTbl = metaTbInFilterCache(pMeta, stbEntry.name, 1) ? 1 : 0;
73,603✔
679
        
680
        ret = metaStableTagFilterCacheUpdateUid(
73,603✔
681
          pMeta, &e, &stbEntry, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
682
        if (ret < 0) {
73,603✔
683
          metaError("vgId:%d, failed to update stable tag filter cache:%s "
×
684
            "uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
685
            e.ctbEntry.suid, tstrerror(ret));
686
        }
687

688
        SSchema        *pTagColumn = NULL;
73,603✔
689
        SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
73,603✔
690
        if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
73,603✔
691
          pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
×
692
          ret = metaDelJsonVarFromIdx(pMeta, &e, pTagColumn);
×
693
          if (ret < 0) {
×
694
            metaError("vgId:%d, failed to delete json var from idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
695
                      e.name, e.uid, tstrerror(ret));
696
          }
697
        } else {
698
          for (int i = 0; i < pTagSchema->nCols; i++) {
381,558✔
699
            pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[i];
307,955✔
700
            if (!IS_IDX_ON(pTagColumn)) continue;
307,955✔
701
            STagIdxKey *pTagIdxKey = NULL;
73,603✔
702
            int32_t     nTagIdxKey;
73,603✔
703

704
            const void *pTagData = NULL;
73,603✔
705
            int32_t     nTagData = 0;
73,603✔
706

707
            STagVal tagVal = {.cid = pTagColumn->colId};
73,603✔
708
            if (tTagGet((const STag *)e.ctbEntry.pTags, &tagVal)) {
73,603✔
709
              if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
73,603✔
710
                pTagData = tagVal.pData;
×
711
                nTagData = (int32_t)tagVal.nData;
×
712
              } else {
713
                pTagData = &(tagVal.i64);
73,603✔
714
                nTagData = tDataTypes[pTagColumn->type].bytes;
73,603✔
715
              }
716
            } else {
717
              if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
×
718
                nTagData = tDataTypes[pTagColumn->type].bytes;
×
719
              }
720
            }
721

722
            if (metaCreateTagIdxKey(e.ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type, uid,
73,603✔
723
                                    &pTagIdxKey, &nTagIdxKey) == 0) {
724
              ret = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
73,603✔
725
              if (ret < 0) {
73,603✔
726
                metaError("vgId:%d, failed to delete tag idx key:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
727
                          e.name, e.uid, tstrerror(ret));
728
              }
729
            }
730
            metaDestroyTagIdxKey(pTagIdxKey);
73,603✔
731
            pTagIdxKey = NULL;
73,603✔
732
          }
733
        }
734
        tDecoderClear(&tdc);
73,603✔
735
      }
736
      tdbFree(tData);
73,603✔
737
    }
738
  }
739

740
  ret = tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), pMeta->txn);
80,143✔
741
  if (ret < 0) {
80,143✔
742
    metaError("vgId:%d, failed to delete table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
743
              tstrerror(ret));
744
  }
745
  ret = tdbTbDelete(pMeta->pNameIdx, e.name, strlen(e.name) + 1, pMeta->txn);
80,143✔
746
  if (ret < 0) {
80,143✔
747
    metaError("vgId:%d, failed to delete name idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
748
              tstrerror(ret));
749
  }
750
  ret = tdbTbDelete(pMeta->pUidIdx, &uid, sizeof(uid), pMeta->txn);
80,143✔
751
  if (ret < 0) {
80,143✔
752
    metaError("vgId:%d, failed to delete uid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
753
              tstrerror(ret));
754
  }
755

756
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
80,143✔
757
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
80,143✔
758

759
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
80,143✔
760

761
  if (e.type == TSDB_CHILD_TABLE) {
80,143✔
762
    ret =
763
        tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
73,603✔
764
    if (ret < 0) {
73,603✔
765
      metaError("vgId:%d, failed to delete ctb idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
766
                tstrerror(ret));
767
    }
768

769
    --pMeta->pVnode->config.vndStats.numOfCTables;
73,603✔
770
    metaUpdateStbStats(pMeta, e.ctbEntry.suid, -1, 0, -1);
73,603✔
771
    ret = metaUidCacheClear(pMeta, e.ctbEntry.suid);
73,603✔
772
    if (ret < 0) {
73,603✔
773
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
774
                e.ctbEntry.suid, tstrerror(ret));
775
    }
776
    ret = metaTbGroupCacheClear(pMeta, e.ctbEntry.suid);
73,603✔
777
    if (ret < 0) {
73,603✔
778
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
779
                e.ctbEntry.suid, tstrerror(ret));
780
    }
781
    /*
782
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
783
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, e.ctbEntry.suid, NULL);
784
    }
785
    */
786
  } else if (e.type == TSDB_NORMAL_TABLE) {
6,540✔
787
    // drop schema.db (todo)
788

789
    --pMeta->pVnode->config.vndStats.numOfNTables;
6,540✔
790
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
6,540✔
791

792
    /*
793
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
794
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, -1, &e.ntbEntry.schemaRow);
795
    }
796
    */
797
  } else if (e.type == TSDB_SUPER_TABLE) {
×
798
    ret = tdbTbDelete(pMeta->pSuidIdx, &e.uid, sizeof(tb_uid_t), pMeta->txn);
×
799
    if (ret < 0) {
×
800
      metaError("vgId:%d, failed to delete suid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
801
                tstrerror(ret));
802
    }
803
    // drop schema.db (todo)
804

805
    ret = metaStatsCacheDrop(pMeta, uid);
×
806
    if (ret < 0) {
×
807
      metaError("vgId:%d, failed to drop stats cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
808
                tstrerror(ret));
809
    }
810
    ret = metaUidCacheClear(pMeta, uid);
×
811
    if (ret < 0) {
×
812
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
813
                tstrerror(ret));
814
    }
815
    ret = metaStableTagFilterCacheDropSTable(pMeta, uid);
×
816
    if (ret < 0) {
×
817
      metaError("vgId:%d, failed to clear stable tag filter cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
818
                e.uid, tstrerror(ret));
819
    }
820
    ret = metaTbGroupCacheClear(pMeta, uid);
×
821
    if (ret < 0) {
×
822
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
823
                e.uid, tstrerror(ret));
824
    }
825
    --pMeta->pVnode->config.vndStats.numOfSTables;
×
826
  }
827

828
  ret = metaCacheDrop(pMeta, uid);
80,143✔
829
  if (ret < 0) {
80,143✔
830
    metaError("vgId:%d, failed to drop cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
80,143✔
831
              tstrerror(ret));
832
  }
833

834
  tDecoderClear(&dc);
80,143✔
835
  tdbFree(pData);
80,143✔
836

837
  return 0;
80,143✔
838
}
839

840
static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
80,143✔
841
  SBtimeIdxKey btimeKey = {0};
80,143✔
842
  if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) {
80,143✔
843
    return 0;
×
844
  }
845
  return tdbTbDelete(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), pMeta->txn);
80,143✔
846
}
847

848
int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
6,540✔
849
  SNcolIdxKey ncolKey = {0};
6,540✔
850
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
6,540✔
851
    return 0;
×
852
  }
853
  return tdbTbDelete(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), pMeta->txn);
6,540✔
854
}
855

856
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
12,544,508✔
857
  pMeta->changed = true;
12,544,508✔
858
  switch (pReq->action) {
12,544,508✔
859
    case TSDB_ALTER_TABLE_ADD_COLUMN:
3,657,326✔
860
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
861
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF:
862
      return metaAddTableColumn(pMeta, version, pReq, pMetaRsp);
3,657,326✔
863
    case TSDB_ALTER_TABLE_DROP_COLUMN:
90,268✔
864
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
90,268✔
865
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
500,508✔
866
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
500,508✔
867
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
40,989✔
868
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
40,989✔
869
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TABLE_TAG_VAL:
8,028,891✔
870
      return metaUpdateTableMultiTableTagValue(pMeta, version, pReq);
8,028,891✔
871
    case TSDB_ALTER_TABLE_UPDATE_CHILD_TABLE_TAG_VAL:
59,196✔
872
      return metaUpdateTableChildTableTagValue(pMeta, version, pReq);
59,196✔
873
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
22,956✔
874
      return metaUpdateTableOptions2(pMeta, version, pReq);
22,956✔
875
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
6,252✔
876
      return metaUpdateTableColCompress2(pMeta, version, pReq);
6,252✔
877
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
78,949✔
878
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
78,949✔
879
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
59,173✔
880
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
59,173✔
881
    default:
×
882
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
883
      break;
884
  }
885
}
886

887
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
21,296,699✔
888
  if (!tsTtlChangeOnWrite) return 0;
21,296,699✔
889

890
  if (changeTimeMs <= 0) {
21,296,699✔
891
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
×
892
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
×
893
  }
894

895
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
21,296,699✔
896

897
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
21,296,699✔
898
}
899

900
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
608,901,503✔
901
  if (!tsTtlChangeOnWrite) return 0;
608,901,503✔
902

903
  metaWLock(pMeta);
21,302,329✔
904
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
21,296,699✔
905
  metaULock(pMeta);
21,296,699✔
906
  return ret;
21,296,699✔
907
}
908

909
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
101,771,508✔
910
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
911
  if (IS_VAR_DATA_TYPE(type)) {
101,771,508✔
912
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
11,682,196✔
913
  } else {
914
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
90,089,312✔
915
  }
916

917
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
101,778,103✔
918
  if (*ppTagIdxKey == NULL) {
101,773,141✔
919
    return terrno;
×
920
  }
921

922
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
101,767,994✔
923
  (*ppTagIdxKey)->cid = cid;
101,775,248✔
924
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
101,775,881✔
925
  (*ppTagIdxKey)->type = type;
101,784,479✔
926

927
  // refactor
928
  if (IS_VAR_DATA_TYPE(type)) {
101,768,751✔
929
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
11,692,307✔
930
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
11,693,239✔
931
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
11,693,861✔
932
  } else {
933
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
90,076,444✔
934
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
90,080,502✔
935
  }
936

937
  return 0;
101,772,370✔
938
}
939

940
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
98,569,956✔
941
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
98,569,956✔
942
}
98,571,563✔
943

944
static void colCompressDebug(SHashObj *pColCmprObj) {
107,487,261✔
945
  void *p = taosHashIterate(pColCmprObj, NULL);
107,487,261✔
946
  while (p) {
923,949,923✔
947
    uint32_t cmprAlg = *(uint32_t *)p;
816,438,683✔
948
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
816,437,207✔
949
    p = taosHashIterate(pColCmprObj, p);
816,457,820✔
950

951
    uint8_t l1, l2, lvl;
816,599,046✔
952
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
816,582,182✔
953

954
    const char *l1str = columnEncodeStr(l1);
816,590,754✔
955
    const char *l2str = columnCompressStr(l2);
816,502,558✔
956
    const char *lvlstr = columnLevelStr(lvl);
816,458,009✔
957
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
816,447,033✔
958
  }
959
  return;
107,511,240✔
960
}
961

962
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
107,543,525✔
963
  int rc = 0;
107,543,525✔
964

965
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
107,543,525✔
966
  if (pColCmprObj == NULL) {
107,544,932✔
967
    pColCmprObj = NULL;
×
968
    return TSDB_CODE_OUT_OF_MEMORY;
×
969
  }
970

971
  void      *pData = NULL;
107,544,932✔
972
  int        nData = 0;
107,540,105✔
973
  SMetaEntry e = {0};
107,548,436✔
974
  SDecoder   dc = {0};
107,547,804✔
975

976
  *ppColCmprObj = NULL;
107,550,569✔
977

978
  metaRLock(pMeta);
107,547,680✔
979
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
107,549,689✔
980
  if (rc < 0) {
107,535,553✔
981
    taosHashCleanup(pColCmprObj);
44,523✔
982
    metaULock(pMeta);
44,523✔
983
    return TSDB_CODE_FAILED;
44,523✔
984
  }
985
  int64_t version = ((SUidIdxVal *)pData)[0].version;
107,491,030✔
986
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
107,505,355✔
987
  if (rc < 0) {
107,495,915✔
988
    metaULock(pMeta);
×
989
    taosHashCleanup(pColCmprObj);
×
990
    metaError("failed to get table entry");
×
991
    return rc;
×
992
  }
993

994
  tDecoderInit(&dc, pData, nData);
107,495,915✔
995
  rc = metaDecodeEntry(&dc, &e);
107,506,502✔
996
  if (rc < 0) {
107,470,975✔
997
    tDecoderClear(&dc);
×
998
    tdbFree(pData);
×
999
    metaULock(pMeta);
×
1000
    taosHashCleanup(pColCmprObj);
×
1001
    return rc;
×
1002
  }
1003
  if (withExtSchema(e.type)) {
107,470,975✔
1004
    SColCmprWrapper *p = &e.colCmpr;
107,497,116✔
1005
    for (int32_t i = 0; i < p->nCols; i++) {
924,037,325✔
1006
      SColCmpr *pCmpr = &p->pColCmpr[i];
816,521,205✔
1007
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
816,526,220✔
1008
      if (rc < 0) {
816,540,209✔
1009
        tDecoderClear(&dc);
×
1010
        tdbFree(pData);
×
1011
        metaULock(pMeta);
×
1012
        taosHashCleanup(pColCmprObj);
×
1013
        return rc;
×
1014
      }
1015
    }
1016
  } else {
1017
    tDecoderClear(&dc);
×
1018
    tdbFree(pData);
×
1019
    metaULock(pMeta);
×
1020
    taosHashCleanup(pColCmprObj);
×
1021
    return 0;
×
1022
  }
1023
  tDecoderClear(&dc);
107,485,555✔
1024
  tdbFree(pData);
107,496,758✔
1025
  metaULock(pMeta);
107,498,131✔
1026

1027
  *ppColCmprObj = pColCmprObj;
107,490,376✔
1028
  colCompressDebug(pColCmprObj);
107,481,822✔
1029

1030
  return 0;
107,510,766✔
1031
}
1032
// refactor later
1033
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
4,330,926✔
1034
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
4,330,926✔
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