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

taosdata / TDengine / #4951

06 Feb 2026 07:29AM UTC coverage: 66.887% (+0.04%) from 66.849%
#4951

push

travis-ci

web-flow
merge: from main to 3.0 #34521

765 of 1081 new or added lines in 28 files covered. (70.77%)

6419 existing lines in 131 files now uncovered.

205810 of 307696 relevant lines covered (66.89%)

127232277.37 hits per line

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

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

50
    SColCmpr *pCol = p + nCols;
3,098,285✔
51
    pCol->id = pSchema->colId;
3,098,285✔
52
    pCol->alg = compress;
3,098,285✔
53
    pWp->nCols = nCols + 1;
3,098,285✔
54
    pWp->version = ver;
3,098,285✔
55
  } else {
56
    for (int32_t i = 0; i < nCols; i++) {
42,080,366✔
57
      SColCmpr *pOCmpr = &pWp->pColCmpr[i];
42,080,366✔
58
      if (pOCmpr->id == pSchema->colId) {
42,080,366✔
59
        int32_t left = (nCols - i - 1) * sizeof(SColCmpr);
68,305✔
60
        if (left) {
68,305✔
61
          memmove(pWp->pColCmpr + i, pWp->pColCmpr + i + 1, left);
46,404✔
62
        }
63
        nCols--;
68,305✔
64
        break;
68,305✔
65
      }
66
    }
67
    pWp->nCols = nCols;
68,305✔
68
    pWp->version = ver;
68,305✔
69
  }
70
  return 0;
3,166,590✔
71
}
72

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

89
int32_t dropTableExtSchema(SMetaEntry *pEntry, int32_t dropColId, int32_t newColNum) {
97,250✔
90
  // no ext schema, no need to drop
91
  if (!pEntry->pExtSchemas) return 0;
97,250✔
92
  if (dropColId == newColNum) {
13,759✔
93
    // drop the last column
94
    pEntry->pExtSchemas[dropColId - 1] = (SExtSchema){0};
5,489✔
95
  } else {
96
    // drop a column in the middle
97
    memmove(pEntry->pExtSchemas + dropColId, pEntry->pExtSchemas + dropColId + 1,
8,270✔
98
            (newColNum - dropColId) * sizeof(SExtSchema));
8,270✔
99
  }
100
  for (int32_t i = 0; i < newColNum; i++) {
60,014✔
101
    if (hasExtSchema(pEntry->pExtSchemas + i)) return 0;
57,306✔
102
  }
103
  taosMemoryFreeClear(pEntry->pExtSchemas);
2,708✔
104
  return 0;
2,708✔
105
}
106

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

116
    SColRef *pCol = p + nCols;
56,052✔
117
    if (NULL == pColRef) {
56,052✔
118
      pCol->hasRef = false;
5,402✔
119
      pCol->id = pSchema->colId;
5,402✔
120
    } else {
121
      pCol->hasRef = pColRef->hasRef;
50,650✔
122
      pCol->id = pSchema->colId;
50,650✔
123
      if (pCol->hasRef) {
50,650✔
124
        tstrncpy(pCol->refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
20,658✔
125
        tstrncpy(pCol->refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
20,658✔
126
        tstrncpy(pCol->refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
20,658✔
127
      }
128
    }
129
    pWp->nCols = nCols + 1;
56,052✔
130
    pWp->version++;
56,052✔
131
  } else {
132
    for (int32_t i = 0; i < nCols; i++) {
34,522,976✔
133
      SColRef *pOColRef = &pWp->pColRef[i];
34,522,976✔
134
      if (pOColRef->id == pSchema->colId) {
34,522,976✔
135
        int32_t left = (nCols - i - 1) * sizeof(SColRef);
32,585✔
136
        if (left) {
32,585✔
137
          memmove(pWp->pColRef + i, pWp->pColRef + i + 1, left);
30,283✔
138
        }
139
        nCols--;
32,585✔
140
        break;
32,585✔
141
      }
142
    }
143
    pWp->nCols = nCols;
32,585✔
144
    pWp->version++;
32,585✔
145
  }
146
  return 0;
88,637✔
147
}
148

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

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

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

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

172
  return 0;
10,167,295✔
173
}
174

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

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

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

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

211
  for (int32_t i = 0; i < pRef->nCols; i++) {
212,670,366✔
212
    SColRef* pColRef = &pRef->pColRef[i];
212,070,088✔
213
    if (pColRef->hasRef) {
212,070,088✔
214
      code = taosHashPut(pColRefHash, pColRef->refTableName, strlen(pColRef->refTableName), NULL, 0);
140,694,650✔
215
      if (code) {
140,694,650✔
UNCOV
216
        goto _return;
×
217
      }
218
    }
219
  }
220

221
  memcpy(pMetaRsp->pColRefs, pRef->pColRef, pRef->nCols * sizeof(SColRef));
600,278✔
222
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
600,278✔
223
  if (tableType == TSDB_VIRTUAL_NORMAL_TABLE) {
600,278✔
224
    pMetaRsp->tuid = pEntry->uid;
339,781✔
225
  } else if (tableType == TSDB_VIRTUAL_CHILD_TABLE) {
260,497✔
226
    pMetaRsp->tuid = pEntry->uid;
260,497✔
227
    pMetaRsp->suid = pEntry->ctbEntry.suid;
260,497✔
228
  }
229

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

236
  taosHashCleanup(pColRefHash);
600,278✔
237
  return code;
600,278✔
UNCOV
238
_return:
×
UNCOV
239
  taosHashCleanup(pColRefHash);
×
240
  taosMemoryFreeClear(pMetaRsp->pSchemaExt);
×
241
  taosMemoryFreeClear(pMetaRsp->pSchemas);
×
242
  taosMemoryFreeClear(pMetaRsp->pColRefs);
×
243
  return code;
×
244
}
245

246
int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
259,830✔
247
  int32_t code = 0;
259,830✔
248

249
#ifdef USE_INVERTED_INDEX
250
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
259,830✔
UNCOV
251
    return TSDB_CODE_INVALID_PARA;
×
252
  }
253
  void       *data = pCtbEntry->ctbEntry.pTags;
259,830✔
254
  const char *tagName = pSchema->name;
259,830✔
255

256
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
259,830✔
257
  tb_uid_t    tuid = pCtbEntry->uid;
259,830✔
258
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
259,830✔
259
  int32_t     nTagData = 0;
259,830✔
260

261
  SArray *pTagVals = NULL;
259,830✔
262
  code = tTagToValArray((const STag *)data, &pTagVals);
259,830✔
263
  if (code) {
259,830✔
UNCOV
264
    return code;
×
265
  }
266

267
  SIndexMultiTerm *terms = indexMultiTermCreate();
259,830✔
268
  if (terms == NULL) {
259,830✔
UNCOV
269
    return terrno;
×
270
  }
271

272
  int16_t nCols = taosArrayGetSize(pTagVals);
259,830✔
273
  for (int i = 0; i < nCols; i++) {
631,639✔
274
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
371,809✔
275
    char     type = pTagVal->type;
371,809✔
276

277
    char   *key = pTagVal->pKey;
371,809✔
278
    int32_t nKey = strlen(key);
371,809✔
279

280
    SIndexTerm *term = NULL;
371,809✔
281
    if (type == TSDB_DATA_TYPE_NULL) {
371,809✔
282
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
2,748✔
283
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
369,061✔
284
      if (pTagVal->nData > 0) {
351,247✔
285
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
349,415✔
286
        if (val == NULL) {
349,415✔
UNCOV
287
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
288
        }
289
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
349,415✔
290
        if (len < 0) {
349,415✔
UNCOV
291
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
292
        }
293
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
349,415✔
294
        type = TSDB_DATA_TYPE_VARCHAR;
349,415✔
295
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
349,415✔
296
        taosMemoryFree(val);
349,415✔
297
      } else if (pTagVal->nData == 0) {
1,832✔
298
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
1,832✔
299
      }
300
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
17,814✔
301
      double val = *(double *)(&pTagVal->i64);
14,150✔
302
      int    len = sizeof(val);
14,150✔
303
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
14,150✔
304
    } else if (type == TSDB_DATA_TYPE_BOOL) {
3,664✔
305
      int val = *(int *)(&pTagVal->i64);
3,664✔
306
      int len = sizeof(val);
3,664✔
307
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
3,664✔
308
    }
309

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

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

341
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
112,066✔
342
  tb_uid_t    tuid = pCtbEntry->uid;
112,066✔
343
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
112,066✔
344
  int32_t     nTagData = 0;
112,066✔
345

346
  SArray *pTagVals = NULL;
112,066✔
347
  code = tTagToValArray((const STag *)data, &pTagVals);
112,066✔
348
  if (code) {
112,066✔
UNCOV
349
    return code;
×
350
  }
351

352
  SIndexMultiTerm *terms = indexMultiTermCreate();
112,066✔
353
  if (terms == NULL) {
112,066✔
UNCOV
354
    return terrno;
×
355
  }
356

357
  int16_t nCols = taosArrayGetSize(pTagVals);
112,066✔
358
  for (int i = 0; i < nCols; i++) {
321,319✔
359
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
209,253✔
360
    char     type = pTagVal->type;
209,253✔
361

362
    char   *key = pTagVal->pKey;
209,253✔
363
    int32_t nKey = strlen(key);
209,253✔
364

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

416
static int32_t metaDropTables(SMeta *pMeta, SArray *tbUids) {
19,303✔
417
  int32_t code = 0;
19,303✔
418
  if (taosArrayGetSize(tbUids) == 0) return TSDB_CODE_SUCCESS;
19,303✔
419

420
  int64_t    nCtbDropped = 0;
19,303✔
421
  SSHashObj *suidHash = tSimpleHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
19,303✔
422
  if (suidHash == NULL) {
19,303✔
UNCOV
423
    return terrno;
×
424
  }
425

426
  metaWLock(pMeta);
19,303✔
427
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
101,140✔
428
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
81,837✔
429
    tb_uid_t suid = 0;
81,837✔
430
    int8_t   sysTbl = 0;
81,837✔
431
    int      type;
81,837✔
432
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
81,837✔
433
    if (code) return code;
81,837✔
434
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
81,837✔
435
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
76,957✔
436
      if (pVal) {
76,957✔
437
        nCtbDropped = *pVal + 1;
59,896✔
438
      } else {
439
        nCtbDropped = 1;
17,061✔
440
      }
441
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
76,957✔
442
      if (code) return code;
76,957✔
443
    }
444
    /*
445
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
446
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
447
    }
448
    */
449
    metaDebug("batch drop table:%" PRId64, uid);
81,837✔
450
  }
451
  metaULock(pMeta);
19,303✔
452

453
  // update timeseries
454
  void   *pCtbDropped = NULL;
19,303✔
455
  int32_t iter = 0;
19,303✔
456
  while ((pCtbDropped = tSimpleHashIterate(suidHash, pCtbDropped, &iter))) {
36,364✔
457
    tb_uid_t    *pSuid = tSimpleHashGetKey(pCtbDropped, NULL);
17,061✔
458
    int32_t      nCols = 0;
17,061✔
459
    int8_t       flags = 0;
17,061✔
460
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
17,061✔
461
    if (metaGetStbStats(pMeta->pVnode, *pSuid, NULL, &nCols, &flags) == 0) {
17,061✔
462
      if (!TABLE_IS_VIRTUAL(flags)) {
17,061✔
463
        pStats->numOfTimeSeries -= *(int64_t *)pCtbDropped * (nCols - 1);
17,061✔
464
      }
465
    }
466
  }
467
  tSimpleHashCleanup(suidHash);
19,303✔
468

469
  pMeta->changed = true;
19,303✔
470
  return 0;
19,303✔
471
}
472

473
static int32_t metaFilterTableByHash(SMeta *pMeta, SArray *uidList) {
24,222✔
474
  int32_t code = 0;
24,222✔
475
  // 1, tranverse table's
476
  // 2, validate table name using vnodeValidateTableHash
477
  // 3, push invalidated table's uid into uidList
478

479
  TBC *pCur;
24,222✔
480
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
24,222✔
481
  if (code < 0) {
24,222✔
UNCOV
482
    return code;
×
483
  }
484

485
  code = tdbTbcMoveToFirst(pCur);
24,222✔
486
  if (code) {
24,222✔
UNCOV
487
    tdbTbcClose(pCur);
×
UNCOV
488
    return code;
×
489
  }
490

491
  void *pData = NULL, *pKey = NULL;
24,222✔
492
  int   nData = 0, nKey = 0;
24,222✔
493

494
  while (1) {
182,211✔
495
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
206,433✔
496
    if (ret < 0) {
206,433✔
497
      break;
24,222✔
498
    }
499

500
    SMetaEntry me = {0};
182,211✔
501
    SDecoder   dc = {0};
182,211✔
502
    tDecoderInit(&dc, pData, nData);
182,211✔
503
    code = metaDecodeEntry(&dc, &me);
182,211✔
504
    if (code < 0) {
182,211✔
UNCOV
505
      tDecoderClear(&dc);
×
UNCOV
506
      return code;
×
507
    }
508

509
    if (me.type != TSDB_SUPER_TABLE) {
182,211✔
510
      char tbFName[TSDB_TABLE_FNAME_LEN + 1];
163,666✔
511
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pMeta->pVnode->config.dbname, me.name);
163,666✔
512
      tbFName[TSDB_TABLE_FNAME_LEN] = '\0';
163,666✔
513
      if (pMeta->pVnode->mounted) tTrimMountPrefix(tbFName);
163,666✔
514
      ret = vnodeValidateTableHash(pMeta->pVnode, tbFName);
163,666✔
515
      if (ret < 0 && terrno == TSDB_CODE_VND_HASH_MISMATCH) {
163,666✔
516
        if (taosArrayPush(uidList, &me.uid) == NULL) {
81,837✔
UNCOV
517
          code = terrno;
×
UNCOV
518
          break;
×
519
        }
520
      }
521
    }
522
    tDecoderClear(&dc);
182,211✔
523
  }
524
  tdbFree(pData);
24,222✔
525
  tdbFree(pKey);
24,222✔
526
  tdbTbcClose(pCur);
24,222✔
527

528
  return 0;
24,222✔
529
}
530

531
int32_t metaTrimTables(SMeta *pMeta, int64_t version) {
24,222✔
532
  int32_t code = 0;
24,222✔
533

534
  SArray *tbUids = taosArrayInit(8, sizeof(int64_t));
24,222✔
535
  if (tbUids == NULL) {
24,222✔
UNCOV
536
    return terrno;
×
537
  }
538

539
  code = metaFilterTableByHash(pMeta, tbUids);
24,222✔
540
  if (code != 0) {
24,222✔
UNCOV
541
    goto end;
×
542
  }
543
  if (TARRAY_SIZE(tbUids) == 0) {
24,222✔
544
    goto end;
4,919✔
545
  }
546

547
  metaInfo("vgId:%d, trim %ld tables", TD_VID(pMeta->pVnode), taosArrayGetSize(tbUids));
19,303✔
548
  code = metaDropTables(pMeta, tbUids);
19,303✔
549
  if (code) goto end;
19,303✔
550

551
end:
19,303✔
552
  taosArrayDestroy(tbUids);
24,222✔
553

554
  return code;
24,222✔
555
}
556

557
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
10,506,612✔
558
  metaRLock(pMeta);
10,506,612✔
559

560
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
10,511,084✔
561

562
  metaULock(pMeta);
10,494,385✔
563

564
  if (ret != 0) {
10,503,615✔
UNCOV
565
    metaError("ttl failed to find expired table, ret:%d", ret);
×
566
  }
567

568
  return ret;
10,505,404✔
569
}
570

571
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
81,837✔
572
  int64_t btime;
573
  if (pME->type == TSDB_CHILD_TABLE) {
81,837✔
574
    btime = pME->ctbEntry.btime;
76,957✔
575
  } else if (pME->type == TSDB_NORMAL_TABLE) {
4,880✔
576
    btime = pME->ntbEntry.btime;
4,880✔
577
  } else {
UNCOV
578
    return TSDB_CODE_FAILED;
×
579
  }
580

581
  btimeKey->btime = btime;
81,837✔
582
  btimeKey->uid = pME->uid;
81,837✔
583
  return 0;
81,837✔
584
}
585

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

596
static void metaDeleteTtl(SMeta *pMeta, const SMetaEntry *pME) {
81,837✔
597
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
81,837✔
598

599
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
81,837✔
600
  if (pME->type == TSDB_CHILD_TABLE) {
81,837✔
601
    ctx.ttlDays = pME->ctbEntry.ttlDays;
76,957✔
602
  } else {
603
    ctx.ttlDays = pME->ntbEntry.ttlDays;
4,880✔
604
  }
605

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

614
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl) {
81,837✔
615
  void      *pData = NULL;
81,837✔
616
  int        nData = 0;
81,837✔
617
  int        rc = 0;
81,837✔
618
  SMetaEntry e = {0};
81,837✔
619
  SDecoder   dc = {0};
81,837✔
620
  int32_t    ret = 0;
81,837✔
621

622
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
81,837✔
623
  if (rc < 0) {
81,837✔
UNCOV
624
    return rc;
×
625
  }
626
  int64_t version = ((SUidIdxVal *)pData)[0].version;
81,837✔
627

628
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
81,837✔
629
  if (rc < 0) {
81,837✔
UNCOV
630
    tdbFree(pData);
×
UNCOV
631
    return rc;
×
632
  }
633

634
  tDecoderInit(&dc, pData, nData);
81,837✔
635
  rc = metaDecodeEntry(&dc, &e);
81,837✔
636
  if (rc < 0) {
81,837✔
UNCOV
637
    tDecoderClear(&dc);
×
UNCOV
638
    return rc;
×
639
  }
640

641
  if (type) *type = e.type;
81,837✔
642

643
  if (e.type == TSDB_CHILD_TABLE) {
81,837✔
644
    if (pSuid) *pSuid = e.ctbEntry.suid;
76,957✔
645
    void *tData = NULL;
76,957✔
646
    int   tLen = 0;
76,957✔
647

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

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

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

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

689
            const void *pTagData = NULL;
76,957✔
690
            int32_t     nTagData = 0;
76,957✔
691

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

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

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

741
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
81,837✔
742
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
81,837✔
743

744
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
81,837✔
745

746
  if (e.type == TSDB_CHILD_TABLE) {
81,837✔
747
    ret =
748
        tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
76,957✔
749
    if (ret < 0) {
76,957✔
UNCOV
750
      metaError("vgId:%d, failed to delete ctb idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
751
                tstrerror(ret));
752
    }
753

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

774
    --pMeta->pVnode->config.vndStats.numOfNTables;
4,880✔
775
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
4,880✔
776

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

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

813
  ret = metaCacheDrop(pMeta, uid);
81,837✔
814
  if (ret < 0) {
81,837✔
815
    metaError("vgId:%d, failed to drop cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
81,837✔
816
              tstrerror(ret));
817
  }
818

819
  tDecoderClear(&dc);
81,837✔
820
  tdbFree(pData);
81,837✔
821

822
  return 0;
81,837✔
823
}
824

825
static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
81,837✔
826
  SBtimeIdxKey btimeKey = {0};
81,837✔
827
  if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) {
81,837✔
UNCOV
828
    return 0;
×
829
  }
830
  return tdbTbDelete(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), pMeta->txn);
81,837✔
831
}
832

833
int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
4,880✔
834
  SNcolIdxKey ncolKey = {0};
4,880✔
835
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
4,880✔
UNCOV
836
    return 0;
×
837
  }
838
  return tdbTbDelete(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), pMeta->txn);
4,880✔
839
}
840

841
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
11,809,529✔
842
  pMeta->changed = true;
11,809,529✔
843
  switch (pReq->action) {
11,809,529✔
844
    case TSDB_ALTER_TABLE_ADD_COLUMN:
3,458,704✔
845
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
846
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF:
847
      return metaAddTableColumn(pMeta, version, pReq, pMetaRsp);
3,458,704✔
848
    case TSDB_ALTER_TABLE_DROP_COLUMN:
97,250✔
849
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
97,250✔
850
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
472,197✔
851
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
472,197✔
852
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
44,242✔
853
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
44,242✔
854
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
7,576,432✔
855
      return metaUpdateTableTagValue(pMeta, version, pReq);
7,576,432✔
856
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
3,564✔
857
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
3,564✔
858
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
24,422✔
859
      return metaUpdateTableOptions2(pMeta, version, pReq);
24,422✔
860
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
5,955✔
861
      return metaUpdateTableColCompress2(pMeta, version, pReq);
5,955✔
862
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
72,947✔
863
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
72,947✔
864
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
53,816✔
865
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
53,816✔
UNCOV
866
    default:
×
UNCOV
867
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
868
      break;
869
  }
870
}
871

872
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
19,991,666✔
873
  if (!tsTtlChangeOnWrite) return 0;
19,991,666✔
874

875
  if (changeTimeMs <= 0) {
19,991,666✔
UNCOV
876
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
×
UNCOV
877
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
×
878
  }
879

880
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
19,991,666✔
881

882
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
19,991,666✔
883
}
884

885
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
710,016,289✔
886
  if (!tsTtlChangeOnWrite) return 0;
710,016,289✔
887

888
  metaWLock(pMeta);
20,012,741✔
889
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
19,991,666✔
890
  metaULock(pMeta);
19,991,666✔
891
  return ret;
19,991,666✔
892
}
893

894
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
82,423,561✔
895
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
896
  if (IS_VAR_DATA_TYPE(type)) {
82,423,561✔
897
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
9,663,302✔
898
  } else {
899
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
72,760,259✔
900
  }
901

902
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
82,431,376✔
903
  if (*ppTagIdxKey == NULL) {
82,429,072✔
UNCOV
904
    return terrno;
×
905
  }
906

907
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
82,424,667✔
908
  (*ppTagIdxKey)->cid = cid;
82,438,688✔
909
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
82,435,023✔
910
  (*ppTagIdxKey)->type = type;
82,444,866✔
911

912
  // refactor
913
  if (IS_VAR_DATA_TYPE(type)) {
82,430,766✔
914
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
9,684,374✔
915
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
9,672,474✔
916
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
9,672,177✔
917
  } else {
918
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
72,746,392✔
919
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
72,747,410✔
920
  }
921

922
  return 0;
82,428,677✔
923
}
924

925
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
79,310,562✔
926
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
79,310,562✔
927
}
79,313,807✔
928

929
static void colCompressDebug(SHashObj *pColCmprObj) {
96,043,535✔
930
  void *p = taosHashIterate(pColCmprObj, NULL);
96,043,535✔
931
  while (p) {
779,667,715✔
932
    uint32_t cmprAlg = *(uint32_t *)p;
683,593,149✔
933
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
683,591,568✔
934
    p = taosHashIterate(pColCmprObj, p);
683,617,348✔
935

936
    uint8_t l1, l2, lvl;
683,788,343✔
937
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
683,773,584✔
938

939
    const char *l1str = columnEncodeStr(l1);
683,777,743✔
940
    const char *l2str = columnCompressStr(l2);
683,681,582✔
941
    const char *lvlstr = columnLevelStr(lvl);
683,639,997✔
942
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
683,616,009✔
943
  }
944
  return;
96,074,566✔
945
}
946

947
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
96,099,666✔
948
  int rc = 0;
96,099,666✔
949

950
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
96,099,666✔
951
  if (pColCmprObj == NULL) {
96,096,038✔
UNCOV
952
    pColCmprObj = NULL;
×
UNCOV
953
    return TSDB_CODE_OUT_OF_MEMORY;
×
954
  }
955

956
  void      *pData = NULL;
96,096,038✔
957
  int        nData = 0;
96,091,801✔
958
  SMetaEntry e = {0};
96,107,000✔
959
  SDecoder   dc = {0};
96,104,803✔
960

961
  *ppColCmprObj = NULL;
96,106,329✔
962

963
  metaRLock(pMeta);
96,098,636✔
964
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
96,113,131✔
965
  if (rc < 0) {
96,090,820✔
966
    taosHashCleanup(pColCmprObj);
43,636✔
967
    metaULock(pMeta);
43,636✔
968
    return TSDB_CODE_FAILED;
43,636✔
969
  }
970
  int64_t version = ((SUidIdxVal *)pData)[0].version;
96,047,184✔
971
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
96,071,376✔
972
  if (rc < 0) {
96,051,836✔
UNCOV
973
    metaULock(pMeta);
×
UNCOV
974
    taosHashCleanup(pColCmprObj);
×
975
    metaError("failed to get table entry");
×
976
    return rc;
×
977
  }
978

979
  tDecoderInit(&dc, pData, nData);
96,051,836✔
980
  rc = metaDecodeEntry(&dc, &e);
96,066,294✔
981
  if (rc < 0) {
96,031,832✔
UNCOV
982
    tDecoderClear(&dc);
×
UNCOV
983
    tdbFree(pData);
×
984
    metaULock(pMeta);
×
985
    taosHashCleanup(pColCmprObj);
×
986
    return rc;
×
987
  }
988
  if (withExtSchema(e.type)) {
96,031,832✔
989
    SColCmprWrapper *p = &e.colCmpr;
96,052,774✔
990
    for (int32_t i = 0; i < p->nCols; i++) {
779,811,684✔
991
      SColCmpr *pCmpr = &p->pColCmpr[i];
683,740,095✔
992
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
683,751,698✔
993
      if (rc < 0) {
683,758,910✔
UNCOV
994
        tDecoderClear(&dc);
×
UNCOV
995
        tdbFree(pData);
×
996
        metaULock(pMeta);
×
997
        taosHashCleanup(pColCmprObj);
×
998
        return rc;
×
999
      }
1000
    }
1001
  } else {
UNCOV
1002
    tDecoderClear(&dc);
×
UNCOV
1003
    tdbFree(pData);
×
1004
    metaULock(pMeta);
×
1005
    taosHashCleanup(pColCmprObj);
×
1006
    return 0;
×
1007
  }
1008
  tDecoderClear(&dc);
96,054,772✔
1009
  tdbFree(pData);
96,054,297✔
1010
  metaULock(pMeta);
96,057,650✔
1011

1012
  *ppColCmprObj = pColCmprObj;
96,050,003✔
1013
  colCompressDebug(pColCmprObj);
96,049,776✔
1014

1015
  return 0;
96,074,564✔
1016
}
1017
// refactor later
1018
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
4,016,097✔
1019
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
4,016,097✔
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