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

taosdata / TDengine / #4548

22 Jul 2025 02:37AM UTC coverage: 54.273% (-3.0%) from 57.287%
#4548

push

travis-ci

GitHub
Merge pull request #32061 from taosdata/new_testcases

132738 of 315239 branches covered (42.11%)

Branch coverage included in aggregate %.

201371 of 300373 relevant lines covered (67.04%)

3475977.14 hits per line

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

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

50
    SColCmpr *pCol = p + nCols;
4,217✔
51
    pCol->id = pSchema->colId;
4,217✔
52
    pCol->alg = compress;
4,217✔
53
    pWp->nCols = nCols + 1;
4,217✔
54
    pWp->version = ver;
4,217✔
55
  } else {
56
    for (int32_t i = 0; i < nCols; i++) {
16,976!
57
      SColCmpr *pOCmpr = &pWp->pColCmpr[i];
16,976✔
58
      if (pOCmpr->id == pSchema->colId) {
16,976✔
59
        int32_t left = (nCols - i - 1) * sizeof(SColCmpr);
90✔
60
        if (left) {
90✔
61
          memmove(pWp->pColCmpr + i, pWp->pColCmpr + i + 1, left);
58✔
62
        }
63
        nCols--;
90✔
64
        break;
90✔
65
      }
66
    }
67
    pWp->nCols = nCols;
90✔
68
    pWp->version = ver;
90✔
69
  }
70
  return 0;
4,307✔
71
}
72

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

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

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

116
    SColRef *pCol = p + nCols;
89✔
117
    if (NULL == pColRef) {
89✔
118
      pCol->hasRef = false;
9✔
119
      pCol->id = pSchema->colId;
9✔
120
    } else {
121
      pCol->hasRef = pColRef->hasRef;
80✔
122
      pCol->id = pSchema->colId;
80✔
123
      if (pCol->hasRef) {
80✔
124
        tstrncpy(pCol->refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
33✔
125
        tstrncpy(pCol->refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
33✔
126
        tstrncpy(pCol->refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
33✔
127
      }
128
    }
129
    pWp->nCols = nCols + 1;
89✔
130
    pWp->version++;
89✔
131
  } else {
132
    for (int32_t i = 0; i < nCols; i++) {
196!
133
      SColRef *pOColRef = &pWp->pColRef[i];
196✔
134
      if (pOColRef->id == pSchema->colId) {
196✔
135
        int32_t left = (nCols - i - 1) * sizeof(SColRef);
50✔
136
        if (left) {
50✔
137
          memmove(pWp->pColRef + i, pWp->pColRef + i + 1, left);
48✔
138
        }
139
        nCols--;
50✔
140
        break;
50✔
141
      }
142
    }
143
    pWp->nCols = nCols;
50✔
144
    pWp->version++;
50✔
145
  }
146
  return 0;
139✔
147
}
148

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

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

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

169
  memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
11,033✔
170

171
  return 0;
11,033✔
172
}
173

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

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

193
    pMetaRsp->numOfColumns = pSchema->nCols;
486✔
194
    pMetaRsp->sversion = pSchema->version;
486✔
195
    memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
486✔
196
  }
197
  pMetaRsp->pColRefs = taosMemoryMalloc(pRef->nCols * sizeof(SColRef));
607!
198
  if (NULL == pMetaRsp->pColRefs) {
607!
199
    code = terrno;
×
200
    goto _return;
×
201
  }
202
  memcpy(pMetaRsp->pColRefs, pRef->pColRef, pRef->nCols * sizeof(SColRef));
607✔
203
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
607✔
204
  pMetaRsp->tuid = uid;
607✔
205
  pMetaRsp->tableType = tableType;
607✔
206
  pMetaRsp->virtualStb = false; // super table will never be processed here
607✔
207
  pMetaRsp->numOfColRefs = pRef->nCols;
607✔
208
  pMetaRsp->rversion = pRef->version;
607✔
209

210
  return code;
607✔
211
_return:
×
212
  taosMemoryFreeClear(pMetaRsp->pSchemaExt);
×
213
  taosMemoryFreeClear(pMetaRsp->pSchemas);
×
214
  taosMemoryFreeClear(pMetaRsp->pColRefs);
×
215
  return code;
×
216
}
217

218
int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
424✔
219
  int32_t code = 0;
424✔
220

221
#ifdef USE_INVERTED_INDEX
222
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
424!
223
    return TSDB_CODE_INVALID_PARA;
×
224
  }
225
  void       *data = pCtbEntry->ctbEntry.pTags;
424✔
226
  const char *tagName = pSchema->name;
424✔
227

228
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
424✔
229
  tb_uid_t    tuid = pCtbEntry->uid;
424✔
230
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
424✔
231
  int32_t     nTagData = 0;
424✔
232

233
  SArray *pTagVals = NULL;
424✔
234
  code = tTagToValArray((const STag *)data, &pTagVals);
424✔
235
  if (code) {
424!
236
    return code;
×
237
  }
238

239
  SIndexMultiTerm *terms = indexMultiTermCreate();
424✔
240
  if (terms == NULL) {
424!
241
    return terrno;
×
242
  }
243

244
  int16_t nCols = taosArrayGetSize(pTagVals);
424✔
245
  for (int i = 0; i < nCols; i++) {
870✔
246
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
446✔
247
    char     type = pTagVal->type;
446✔
248

249
    char   *key = pTagVal->pKey;
446✔
250
    int32_t nKey = strlen(key);
446✔
251

252
    SIndexTerm *term = NULL;
446✔
253
    if (type == TSDB_DATA_TYPE_NULL) {
446✔
254
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
18✔
255
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
428✔
256
      if (pTagVal->nData > 0) {
361✔
257
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
349!
258
        if (val == NULL) {
349!
259
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
260
        }
261
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
349✔
262
        if (len < 0) {
349!
263
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
264
        }
265
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
349✔
266
        type = TSDB_DATA_TYPE_VARCHAR;
349✔
267
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
349✔
268
        taosMemoryFree(val);
349!
269
      } else if (pTagVal->nData == 0) {
12!
270
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
12✔
271
      }
272
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
67✔
273
      double val = *(double *)(&pTagVal->i64);
49✔
274
      int    len = sizeof(val);
49✔
275
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
49✔
276
    } else if (type == TSDB_DATA_TYPE_BOOL) {
18!
277
      int val = *(int *)(&pTagVal->i64);
18✔
278
      int len = sizeof(val);
18✔
279
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
18✔
280
    }
281

282
    if (term != NULL) {
446!
283
      int32_t ret = indexMultiTermAdd(terms, term);
446✔
284
      if (ret < 0) {
446!
285
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
286
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
287
      }
288
    } else {
289
      code = terrno;
×
290
      goto _exception;
×
291
    }
292
  }
293
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
424✔
294
  indexMultiTermDestroy(terms);
424✔
295

296
  taosArrayDestroy(pTagVals);
424✔
297
  return code;
424✔
298
_exception:
×
299
  indexMultiTermDestroy(terms);
×
300
  taosArrayDestroy(pTagVals);
×
301
#endif
302
  return code;
×
303
}
304
int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
13✔
305
int32_t code = 0;
13✔
306
#ifdef USE_INVERTED_INDEX
307
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
13!
308
    return TSDB_CODE_INVALID_PARA;
×
309
  }
310
  void       *data = pCtbEntry->ctbEntry.pTags;
13✔
311
  const char *tagName = pSchema->name;
13✔
312

313
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
13✔
314
  tb_uid_t    tuid = pCtbEntry->uid;
13✔
315
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
13✔
316
  int32_t     nTagData = 0;
13✔
317

318
  SArray *pTagVals = NULL;
13✔
319
  code = tTagToValArray((const STag *)data, &pTagVals);
13✔
320
  if (code) {
13!
321
    return code;
×
322
  }
323

324
  SIndexMultiTerm *terms = indexMultiTermCreate();
13✔
325
  if (terms == NULL) {
13!
326
    return terrno;
×
327
  }
328

329
  int16_t nCols = taosArrayGetSize(pTagVals);
13✔
330
  for (int i = 0; i < nCols; i++) {
38✔
331
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
25✔
332
    char     type = pTagVal->type;
25✔
333

334
    char   *key = pTagVal->pKey;
25✔
335
    int32_t nKey = strlen(key);
25✔
336

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

388
static int32_t metaDropTables(SMeta *pMeta, SArray *tbUids) {
8✔
389
  int32_t code = 0;
8✔
390
  if (taosArrayGetSize(tbUids) == 0) return TSDB_CODE_SUCCESS;
8!
391

392
  int64_t    nCtbDropped = 0;
8✔
393
  SSHashObj *suidHash = tSimpleHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
8✔
394
  if (suidHash == NULL) {
8!
395
    return terrno;
×
396
  }
397

398
  metaWLock(pMeta);
8✔
399
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
26✔
400
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
18✔
401
    tb_uid_t suid = 0;
18✔
402
    int8_t   sysTbl = 0;
18✔
403
    int      type;
404
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
18✔
405
    if (code) return code;
18!
406
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
18!
407
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
18✔
408
      if (pVal) {
18✔
409
        nCtbDropped = *pVal + 1;
10✔
410
      } else {
411
        nCtbDropped = 1;
8✔
412
      }
413
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
18✔
414
      if (code) return code;
18!
415
    }
416
    /*
417
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
418
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
419
    }
420
    */
421
    metaDebug("batch drop table:%" PRId64, uid);
18✔
422
  }
423
  metaULock(pMeta);
8✔
424

425
  // update timeseries
426
  void   *pCtbDropped = NULL;
8✔
427
  int32_t iter = 0;
8✔
428
  while ((pCtbDropped = tSimpleHashIterate(suidHash, pCtbDropped, &iter))) {
16✔
429
    tb_uid_t    *pSuid = tSimpleHashGetKey(pCtbDropped, NULL);
8✔
430
    int32_t      nCols = 0;
8✔
431
    int8_t       flags = 0;
8✔
432
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
8✔
433
    if (metaGetStbStats(pMeta->pVnode, *pSuid, NULL, &nCols, &flags) == 0) {
8!
434
      if (!TABLE_IS_VIRTUAL(flags)) {
8!
435
        pStats->numOfTimeSeries -= *(int64_t *)pCtbDropped * (nCols - 1);
8✔
436
      }
437
    }
438
  }
439
  tSimpleHashCleanup(suidHash);
8✔
440

441
  pMeta->changed = true;
8✔
442
  return 0;
8✔
443
}
444

445
static int32_t metaFilterTableByHash(SMeta *pMeta, SArray *uidList) {
8✔
446
  int32_t code = 0;
8✔
447
  // 1, tranverse table's
448
  // 2, validate table name using vnodeValidateTableHash
449
  // 3, push invalidated table's uid into uidList
450

451
  TBC *pCur;
452
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
8✔
453
  if (code < 0) {
8!
454
    return code;
×
455
  }
456

457
  code = tdbTbcMoveToFirst(pCur);
8✔
458
  if (code) {
8!
459
    tdbTbcClose(pCur);
×
460
    return code;
×
461
  }
462

463
  void *pData = NULL, *pKey = NULL;
8✔
464
  int   nData = 0, nKey = 0;
8✔
465

466
  while (1) {
46✔
467
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
54✔
468
    if (ret < 0) {
54✔
469
      break;
8✔
470
    }
471

472
    SMetaEntry me = {0};
46✔
473
    SDecoder   dc = {0};
46✔
474
    tDecoderInit(&dc, pData, nData);
46✔
475
    code = metaDecodeEntry(&dc, &me);
46✔
476
    if (code < 0) {
46!
477
      tDecoderClear(&dc);
×
478
      return code;
×
479
    }
480

481
    if (me.type != TSDB_SUPER_TABLE) {
46✔
482
      char tbFName[TSDB_TABLE_FNAME_LEN + 1];
483
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pMeta->pVnode->config.dbname, me.name);
36✔
484
      tbFName[TSDB_TABLE_FNAME_LEN] = '\0';
36✔
485
      if (pMeta->pVnode->mounted) tTrimMountPrefix(tbFName);
36!
486
      ret = vnodeValidateTableHash(pMeta->pVnode, tbFName);
36✔
487
      if (ret < 0 && terrno == TSDB_CODE_VND_HASH_MISMATCH) {
36!
488
        if (taosArrayPush(uidList, &me.uid) == NULL) {
18!
489
          code = terrno;
×
490
          break;
×
491
        }
492
      }
493
    }
494
    tDecoderClear(&dc);
46✔
495
  }
496
  tdbFree(pData);
8✔
497
  tdbFree(pKey);
8✔
498
  tdbTbcClose(pCur);
8✔
499

500
  return 0;
8✔
501
}
502

503
int32_t metaTrimTables(SMeta *pMeta, int64_t version) {
8✔
504
  int32_t code = 0;
8✔
505

506
  SArray *tbUids = taosArrayInit(8, sizeof(int64_t));
8✔
507
  if (tbUids == NULL) {
8!
508
    return terrno;
×
509
  }
510

511
  code = metaFilterTableByHash(pMeta, tbUids);
8✔
512
  if (code != 0) {
8!
513
    goto end;
×
514
  }
515
  if (TARRAY_SIZE(tbUids) == 0) {
8!
516
    goto end;
×
517
  }
518

519
  metaInfo("vgId:%d, trim %ld tables", TD_VID(pMeta->pVnode), taosArrayGetSize(tbUids));
8!
520
  code = metaDropTables(pMeta, tbUids);
8✔
521
  if (code) goto end;
8!
522

523
end:
8✔
524
  taosArrayDestroy(tbUids);
8✔
525

526
  return code;
8✔
527
}
528

529
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
10,312✔
530
  metaRLock(pMeta);
10,312✔
531

532
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
10,316✔
533

534
  metaULock(pMeta);
10,310✔
535

536
  if (ret != 0) {
10,319!
537
    metaError("ttl failed to find expired table, ret:%d", ret);
×
538
  }
539

540
  return ret;
10,312✔
541
}
542

543
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
18✔
544
  int64_t btime;
545
  if (pME->type == TSDB_CHILD_TABLE) {
18!
546
    btime = pME->ctbEntry.btime;
18✔
547
  } else if (pME->type == TSDB_NORMAL_TABLE) {
×
548
    btime = pME->ntbEntry.btime;
×
549
  } else {
550
    return TSDB_CODE_FAILED;
×
551
  }
552

553
  btimeKey->btime = btime;
18✔
554
  btimeKey->uid = pME->uid;
18✔
555
  return 0;
18✔
556
}
557

558
static int metaBuildNColIdxKey(SNcolIdxKey *ncolKey, const SMetaEntry *pME) {
×
559
  if (pME->type == TSDB_NORMAL_TABLE) {
×
560
    ncolKey->ncol = pME->ntbEntry.schemaRow.nCols;
×
561
    ncolKey->uid = pME->uid;
×
562
  } else {
563
    return TSDB_CODE_FAILED;
×
564
  }
565
  return 0;
×
566
}
567

568
static void metaDeleteTtl(SMeta *pMeta, const SMetaEntry *pME) {
18✔
569
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
18!
570

571
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
18✔
572
  if (pME->type == TSDB_CHILD_TABLE) {
18!
573
    ctx.ttlDays = pME->ctbEntry.ttlDays;
18✔
574
  } else {
575
    ctx.ttlDays = pME->ntbEntry.ttlDays;
×
576
  }
577

578
  int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
18✔
579
  if (ret < 0) {
18!
580
    metaError("vgId:%d, failed to delete ttl for table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pME->name,
×
581
              pME->uid, tstrerror(ret));
582
  }
583
  return;
18✔
584
}
585

586
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl) {
18✔
587
  void      *pData = NULL;
18✔
588
  int        nData = 0;
18✔
589
  int        rc = 0;
18✔
590
  SMetaEntry e = {0};
18✔
591
  SDecoder   dc = {0};
18✔
592
  int32_t    ret = 0;
18✔
593

594
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
18✔
595
  if (rc < 0) {
18!
596
    return rc;
×
597
  }
598
  int64_t version = ((SUidIdxVal *)pData)[0].version;
18✔
599

600
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
18✔
601
  if (rc < 0) {
18!
602
    tdbFree(pData);
×
603
    return rc;
×
604
  }
605

606
  tDecoderInit(&dc, pData, nData);
18✔
607
  rc = metaDecodeEntry(&dc, &e);
18✔
608
  if (rc < 0) {
18!
609
    tDecoderClear(&dc);
×
610
    return rc;
×
611
  }
612

613
  if (type) *type = e.type;
18!
614

615
  if (e.type == TSDB_CHILD_TABLE) {
18!
616
    if (pSuid) *pSuid = e.ctbEntry.suid;
18!
617
    void *tData = NULL;
18✔
618
    int   tLen = 0;
18✔
619

620
    if (tdbTbGet(pMeta->pUidIdx, &e.ctbEntry.suid, sizeof(tb_uid_t), &tData, &tLen) == 0) {
18!
621
      STbDbKey tbDbKey = {.uid = e.ctbEntry.suid, .version = ((SUidIdxVal *)tData)[0].version};
18✔
622
      if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &tData, &tLen) == 0) {
18!
623
        SDecoder   tdc = {0};
18✔
624
        SMetaEntry stbEntry = {0};
18✔
625

626
        tDecoderInit(&tdc, tData, tLen);
18✔
627
        ret = metaDecodeEntry(&tdc, &stbEntry);
18✔
628
        if (ret < 0) {
18!
629
          tDecoderClear(&tdc);
×
630
          metaError("vgId:%d, failed to decode child table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
631
                    e.ctbEntry.suid, tstrerror(ret));
632
          return ret;
×
633
        }
634

635
        if (pSysTbl) *pSysTbl = metaTbInFilterCache(pMeta, stbEntry.name, 1) ? 1 : 0;
18!
636

637
        SSchema        *pTagColumn = NULL;
18✔
638
        SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
18✔
639
        if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
18!
640
          pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
×
641
          ret = metaDelJsonVarFromIdx(pMeta, &e, pTagColumn);
×
642
          if (ret < 0) {
×
643
            metaError("vgId:%d, failed to delete json var from idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
644
                      e.name, e.uid, tstrerror(ret));
645
          }
646
        } else {
647
          for (int i = 0; i < pTagSchema->nCols; i++) {
44✔
648
            pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[i];
26✔
649
            if (!IS_IDX_ON(pTagColumn)) continue;
26✔
650
            STagIdxKey *pTagIdxKey = NULL;
18✔
651
            int32_t     nTagIdxKey;
652

653
            const void *pTagData = NULL;
18✔
654
            int32_t     nTagData = 0;
18✔
655

656
            STagVal tagVal = {.cid = pTagColumn->colId};
18✔
657
            if (tTagGet((const STag *)e.ctbEntry.pTags, &tagVal)) {
18!
658
              if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
18!
659
                pTagData = tagVal.pData;
×
660
                nTagData = (int32_t)tagVal.nData;
×
661
              } else {
662
                pTagData = &(tagVal.i64);
18✔
663
                nTagData = tDataTypes[pTagColumn->type].bytes;
18✔
664
              }
665
            } else {
666
              if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
×
667
                nTagData = tDataTypes[pTagColumn->type].bytes;
×
668
              }
669
            }
670

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

689
  ret = tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), pMeta->txn);
18✔
690
  if (ret < 0) {
18!
691
    metaError("vgId:%d, failed to delete table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
692
              tstrerror(ret));
693
  }
694
  ret = tdbTbDelete(pMeta->pNameIdx, e.name, strlen(e.name) + 1, pMeta->txn);
18✔
695
  if (ret < 0) {
18!
696
    metaError("vgId:%d, failed to delete name idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
697
              tstrerror(ret));
698
  }
699
  ret = tdbTbDelete(pMeta->pUidIdx, &uid, sizeof(uid), pMeta->txn);
18✔
700
  if (ret < 0) {
18!
701
    metaError("vgId:%d, failed to delete uid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
702
              tstrerror(ret));
703
  }
704

705
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
18!
706
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
18!
707

708
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
18!
709

710
  if (e.type == TSDB_CHILD_TABLE) {
18!
711
    ret =
712
        tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
18✔
713
    if (ret < 0) {
18!
714
      metaError("vgId:%d, failed to delete ctb idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
715
                tstrerror(ret));
716
    }
717

718
    --pMeta->pVnode->config.vndStats.numOfCTables;
18✔
719
    metaUpdateStbStats(pMeta, e.ctbEntry.suid, -1, 0, -1);
18✔
720
    ret = metaUidCacheClear(pMeta, e.ctbEntry.suid);
18✔
721
    if (ret < 0) {
18!
722
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
723
                e.ctbEntry.suid, tstrerror(ret));
724
    }
725
    ret = metaTbGroupCacheClear(pMeta, e.ctbEntry.suid);
18✔
726
    if (ret < 0) {
18!
727
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
728
                e.ctbEntry.suid, tstrerror(ret));
729
    }
730
    /*
731
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
732
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, e.ctbEntry.suid, NULL);
733
    }
734
    */
735
  } else if (e.type == TSDB_NORMAL_TABLE) {
×
736
    // drop schema.db (todo)
737

738
    --pMeta->pVnode->config.vndStats.numOfNTables;
×
739
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
×
740

741
    /*
742
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
743
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, -1, &e.ntbEntry.schemaRow);
744
    }
745
    */
746
  } else if (e.type == TSDB_SUPER_TABLE) {
×
747
    ret = tdbTbDelete(pMeta->pSuidIdx, &e.uid, sizeof(tb_uid_t), pMeta->txn);
×
748
    if (ret < 0) {
×
749
      metaError("vgId:%d, failed to delete suid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
750
                tstrerror(ret));
751
    }
752
    // drop schema.db (todo)
753

754
    ret = metaStatsCacheDrop(pMeta, uid);
×
755
    if (ret < 0) {
×
756
      metaError("vgId:%d, failed to drop stats cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
757
                tstrerror(ret));
758
    }
759
    ret = metaUidCacheClear(pMeta, uid);
×
760
    if (ret < 0) {
×
761
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
762
                tstrerror(ret));
763
    }
764
    ret = metaTbGroupCacheClear(pMeta, uid);
×
765
    if (ret < 0) {
×
766
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
767
                e.uid, tstrerror(ret));
768
    }
769
    --pMeta->pVnode->config.vndStats.numOfSTables;
×
770
  }
771

772
  ret = metaCacheDrop(pMeta, uid);
18✔
773
  if (ret < 0) {
18!
774
    metaError("vgId:%d, failed to drop cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
18!
775
              tstrerror(ret));
776
  }
777

778
  tDecoderClear(&dc);
18✔
779
  tdbFree(pData);
18✔
780

781
  return 0;
18✔
782
}
783

784
static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
18✔
785
  SBtimeIdxKey btimeKey = {0};
18✔
786
  if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) {
18!
787
    return 0;
×
788
  }
789
  return tdbTbDelete(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), pMeta->txn);
18✔
790
}
791

792
int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
×
793
  SNcolIdxKey ncolKey = {0};
×
794
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
×
795
    return 0;
×
796
  }
797
  return tdbTbDelete(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), pMeta->txn);
×
798
}
799

800
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
5,944✔
801
  pMeta->changed = true;
5,944✔
802
  switch (pReq->action) {
5,944!
803
    case TSDB_ALTER_TABLE_ADD_COLUMN:
4,297✔
804
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
805
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF:
806
      return metaAddTableColumn(pMeta, version, pReq, pMetaRsp);
4,297✔
807
    case TSDB_ALTER_TABLE_DROP_COLUMN:
134✔
808
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
134✔
809
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
54✔
810
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
54✔
811
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
43✔
812
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
43✔
813
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
1,229✔
814
      return metaUpdateTableTagValue(pMeta, version, pReq);
1,229✔
815
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
10✔
816
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
10✔
817
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
38✔
818
      return metaUpdateTableOptions2(pMeta, version, pReq);
38✔
819
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
16✔
820
      return metaUpdateTableColCompress2(pMeta, version, pReq);
16✔
821
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
79✔
822
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
79✔
823
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
44✔
824
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
44✔
825
    default:
×
826
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
827
      break;
828
  }
829
}
830

831
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
2,062✔
832
  if (!tsTtlChangeOnWrite) return 0;
2,062!
833

834
  if (changeTimeMs <= 0) {
2,062!
835
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
×
836
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
×
837
  }
838

839
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
2,062✔
840

841
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
2,062✔
842
}
843

844
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
2,242,751✔
845
  if (!tsTtlChangeOnWrite) return 0;
2,242,751✔
846

847
  metaWLock(pMeta);
2,063✔
848
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
2,062✔
849
  metaULock(pMeta);
2,062✔
850
  return ret;
2,062✔
851
}
852

853
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
129,249✔
854
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
855
  if (IS_VAR_DATA_TYPE(type)) {
129,249!
856
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
18,517✔
857
  } else {
858
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
110,732✔
859
  }
860

861
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
129,249!
862
  if (*ppTagIdxKey == NULL) {
129,288!
863
    return terrno;
×
864
  }
865

866
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
129,288✔
867
  (*ppTagIdxKey)->cid = cid;
129,288✔
868
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
129,288✔
869
  (*ppTagIdxKey)->type = type;
129,288✔
870

871
  // refactor
872
  if (IS_VAR_DATA_TYPE(type)) {
129,288!
873
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
18,564✔
874
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
18,564✔
875
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
18,564✔
876
  } else {
877
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
110,724✔
878
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
110,724✔
879
  }
880

881
  return 0;
129,288✔
882
}
883

884
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
119,826✔
885
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
119,826!
886
}
119,830✔
887

888
static void colCompressDebug(SHashObj *pColCmprObj) {
52,365✔
889
  void *p = taosHashIterate(pColCmprObj, NULL);
52,365✔
890
  while (p) {
661,345✔
891
    uint32_t cmprAlg = *(uint32_t *)p;
609,009✔
892
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
609,009✔
893
    p = taosHashIterate(pColCmprObj, p);
608,973✔
894

895
    uint8_t l1, l2, lvl;
896
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
609,112✔
897

898
    const char *l1str = columnEncodeStr(l1);
609,094✔
899
    const char *l2str = columnCompressStr(l2);
609,050✔
900
    const char *lvlstr = columnLevelStr(lvl);
609,002✔
901
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
608,978✔
902
  }
903
  return;
52,336✔
904
}
905

906
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
52,363✔
907
  int rc = 0;
52,363✔
908

909
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
52,363✔
910
  if (pColCmprObj == NULL) {
52,355!
911
    pColCmprObj = NULL;
×
912
    return TSDB_CODE_OUT_OF_MEMORY;
×
913
  }
914

915
  void      *pData = NULL;
52,355✔
916
  int        nData = 0;
52,355✔
917
  SMetaEntry e = {0};
52,355✔
918
  SDecoder   dc = {0};
52,355✔
919

920
  *ppColCmprObj = NULL;
52,355✔
921

922
  metaRLock(pMeta);
52,355✔
923
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
52,367✔
924
  if (rc < 0) {
52,365!
925
    taosHashClear(pColCmprObj);
×
926
    metaULock(pMeta);
×
927
    return TSDB_CODE_FAILED;
×
928
  }
929
  int64_t version = ((SUidIdxVal *)pData)[0].version;
52,365✔
930
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
52,365✔
931
  if (rc < 0) {
52,366!
932
    metaULock(pMeta);
×
933
    taosHashClear(pColCmprObj);
×
934
    metaError("failed to get table entry");
×
935
    return rc;
×
936
  }
937

938
  tDecoderInit(&dc, pData, nData);
52,366✔
939
  rc = metaDecodeEntry(&dc, &e);
52,366✔
940
  if (rc < 0) {
52,356!
941
    tDecoderClear(&dc);
×
942
    tdbFree(pData);
×
943
    metaULock(pMeta);
×
944
    taosHashClear(pColCmprObj);
×
945
    return rc;
×
946
  }
947
  if (withExtSchema(e.type)) {
52,356✔
948
    SColCmprWrapper *p = &e.colCmpr;
52,365✔
949
    for (int32_t i = 0; i < p->nCols; i++) {
661,580✔
950
      SColCmpr *pCmpr = &p->pColCmpr[i];
609,173✔
951
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
609,173✔
952
      if (rc < 0) {
609,223✔
953
        tDecoderClear(&dc);
8✔
954
        tdbFree(pData);
×
955
        metaULock(pMeta);
×
956
        taosHashClear(pColCmprObj);
×
957
        return rc;
×
958
      }
959
    }
960
  } else {
961
    tDecoderClear(&dc);
1✔
962
    tdbFree(pData);
×
963
    metaULock(pMeta);
×
964
    taosHashClear(pColCmprObj);
×
965
    return 0;
×
966
  }
967
  tDecoderClear(&dc);
52,407✔
968
  tdbFree(pData);
52,363✔
969
  metaULock(pMeta);
52,362✔
970

971
  *ppColCmprObj = pColCmprObj;
52,365✔
972
  colCompressDebug(pColCmprObj);
52,365✔
973

974
  return 0;
52,356✔
975
}
976
// refactor later
977
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
14,070✔
978
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
14,061✔
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