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

taosdata / TDengine / #3991

29 Apr 2025 09:40AM UTC coverage: 62.455% (-0.01%) from 62.469%
#3991

push

travis-ci

web-flow
Merge pull request #30938 from taosdata/fix/taosdCmdArgs

fix: taosd cmd args

155487 of 317399 branches covered (48.99%)

Branch coverage included in aggregate %.

1 of 4 new or added lines in 1 file covered. (25.0%)

345 existing lines in 99 files now uncovered.

240414 of 316504 relevant lines covered (75.96%)

6321441.34 hits per line

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

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

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

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

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

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

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

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

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

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

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

171
  return 0;
15,297✔
172
}
173

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

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

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

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

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

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

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

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

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

243
  int16_t nCols = taosArrayGetSize(pTagVals);
433✔
244
  for (int i = 0; i < nCols; i++) {
1,071✔
245
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
638✔
246
    char     type = pTagVal->type;
638✔
247

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

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

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

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

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

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

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

328
  int16_t nCols = taosArrayGetSize(pTagVals);
204✔
329
  for (int i = 0; i < nCols; i++) {
587✔
330
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
383✔
331
    char     type = pTagVal->type;
383✔
332

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

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

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

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

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

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

440
  pMeta->changed = true;
27✔
441
  return 0;
27✔
442
}
443

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

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

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

462
  void *pData = NULL, *pKey = NULL;
30✔
463
  int   nData = 0, nKey = 0;
30✔
464

465
  while (1) {
488✔
466
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
518✔
467
    if (ret < 0) {
518✔
468
      break;
30✔
469
    }
470

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

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

498
  return 0;
30✔
499
}
500

501
int32_t metaTrimTables(SMeta *pMeta, int64_t version) {
30✔
502
  int32_t code = 0;
30✔
503

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

509
  code = metaFilterTableByHash(pMeta, tbUids);
30✔
510
  if (code != 0) {
30!
511
    goto end;
×
512
  }
513
  if (TARRAY_SIZE(tbUids) == 0) {
30✔
514
    goto end;
3✔
515
  }
516

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

521
end:
27✔
522
  taosArrayDestroy(tbUids);
30✔
523

524
  return code;
30✔
525
}
526

527
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
17,033✔
528
  metaRLock(pMeta);
17,033✔
529

530
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
17,047✔
531

532
  metaULock(pMeta);
17,015✔
533

534
  if (ret != 0) {
17,031!
535
    metaError("ttl failed to find expired table, ret:%d", ret);
×
536
  }
537

538
  return ret;
17,018✔
539
}
540

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

551
  btimeKey->btime = btime;
230✔
552
  btimeKey->uid = pME->uid;
230✔
553
  return 0;
230✔
554
}
555

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

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

569
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
230✔
570
  if (pME->type == TSDB_CHILD_TABLE) {
230✔
571
    ctx.ttlDays = pME->ctbEntry.ttlDays;
229✔
572
  } else {
573
    ctx.ttlDays = pME->ntbEntry.ttlDays;
1✔
574
  }
575

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

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

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

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

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

611
  if (type) *type = e.type;
230!
612

613
  if (e.type == TSDB_CHILD_TABLE) {
230✔
614
    if (pSuid) *pSuid = e.ctbEntry.suid;
229!
615
    void *tData = NULL;
229✔
616
    int   tLen = 0;
229✔
617

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

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

633
        if (pSysTbl) *pSysTbl = metaTbInFilterCache(pMeta, stbEntry.name, 1) ? 1 : 0;
229!
634

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

651
            const void *pTagData = NULL;
229✔
652
            int32_t     nTagData = 0;
229✔
653

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

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

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

703
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
230!
704
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
230✔
705

706
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
230!
707

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

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

736
    --pMeta->pVnode->config.vndStats.numOfNTables;
1✔
737
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
1✔
738

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

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

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

776
  tDecoderClear(&dc);
230✔
777
  tdbFree(pData);
230✔
778

779
  return 0;
230✔
780
}
781

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

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

798
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
5,375✔
799
  pMeta->changed = true;
5,375✔
800
  switch (pReq->action) {
5,375!
801
    case TSDB_ALTER_TABLE_ADD_COLUMN:
1,197✔
802
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
803
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF:
804
      return metaAddTableColumn(pMeta, version, pReq, pMetaRsp);
1,197✔
805
    case TSDB_ALTER_TABLE_DROP_COLUMN:
151✔
806
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
151✔
807
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
748✔
808
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
748✔
809
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
46✔
810
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
46✔
811
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
3,118✔
812
      return metaUpdateTableTagValue(pMeta, version, pReq);
3,118✔
813
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
5✔
814
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
5✔
815
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
105✔
816
      return metaUpdateTableOptions2(pMeta, version, pReq);
105✔
817
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
5✔
818
      return metaUpdateTableColCompress2(pMeta, version, pReq);
5✔
819
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
×
820
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
×
821
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
×
822
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
×
823
    default:
×
824
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
825
      break;
826
  }
827
}
828

829
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
×
830
  if (!tsTtlChangeOnWrite) return 0;
×
831

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

837
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
×
838

839
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
×
840
}
841

842
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
3,004,714✔
843
  if (!tsTtlChangeOnWrite) return 0;
3,004,714!
844

UNCOV
845
  metaWLock(pMeta);
×
846
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
×
847
  metaULock(pMeta);
×
848
  return ret;
×
849
}
850

851
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
155,866✔
852
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
853
  if (IS_VAR_DATA_TYPE(type)) {
155,866✔
854
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
34,356✔
855
  } else {
856
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
121,510✔
857
  }
858

859
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
155,866!
860
  if (*ppTagIdxKey == NULL) {
155,887!
861
    return terrno;
×
862
  }
863

864
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
155,887✔
865
  (*ppTagIdxKey)->cid = cid;
155,887✔
866
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
155,887✔
867
  (*ppTagIdxKey)->type = type;
155,887✔
868

869
  // refactor
870
  if (IS_VAR_DATA_TYPE(type)) {
155,887!
871
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
34,377✔
872
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
34,377✔
873
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
34,377✔
874
  } else {
875
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
121,510✔
876
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
121,510✔
877
  }
878

879
  return 0;
155,887✔
880
}
881

882
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
155,034✔
883
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
155,034!
884
}
155,028✔
885

886
static void colCompressDebug(SHashObj *pColCmprObj) {
594,279✔
887
  void *p = taosHashIterate(pColCmprObj, NULL);
594,279✔
888
  while (p) {
6,338,452✔
889
    uint32_t cmprAlg = *(uint32_t *)p;
5,744,382✔
890
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
5,744,382✔
891
    p = taosHashIterate(pColCmprObj, p);
5,744,314✔
892

893
    uint8_t l1, l2, lvl;
894
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
5,745,220✔
895

896
    const char *l1str = columnEncodeStr(l1);
5,745,076✔
897
    const char *l2str = columnCompressStr(l2);
5,744,762✔
898
    const char *lvlstr = columnLevelStr(lvl);
5,744,236✔
899
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
5,744,108✔
900
  }
901
  return;
594,070✔
902
}
903

904
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
594,239✔
905
  int rc = 0;
594,239✔
906

907
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
594,239✔
908
  if (pColCmprObj == NULL) {
594,261!
909
    pColCmprObj = NULL;
×
910
    return TSDB_CODE_OUT_OF_MEMORY;
×
911
  }
912

913
  void      *pData = NULL;
594,261✔
914
  int        nData = 0;
594,261✔
915
  SMetaEntry e = {0};
594,261✔
916
  SDecoder   dc = {0};
594,261✔
917

918
  *ppColCmprObj = NULL;
594,261✔
919

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

936
  tDecoderInit(&dc, pData, nData);
594,290✔
937
  rc = metaDecodeEntry(&dc, &e);
594,274✔
938
  if (rc < 0) {
594,197!
939
    tDecoderClear(&dc);
×
940
    tdbFree(pData);
×
941
    metaULock(pMeta);
×
942
    taosHashClear(pColCmprObj);
×
943
    return rc;
×
944
  }
945
  if (withExtSchema(e.type)) {
594,197!
946
    SColCmprWrapper *p = &e.colCmpr;
594,187✔
947
    for (int32_t i = 0; i < p->nCols; i++) {
6,339,420✔
948
      SColCmpr *pCmpr = &p->pColCmpr[i];
5,744,898✔
949
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
5,744,898✔
950
      if (rc < 0) {
5,745,253✔
951
        tDecoderClear(&dc);
20✔
952
        tdbFree(pData);
×
953
        metaULock(pMeta);
×
954
        taosHashClear(pColCmprObj);
×
955
        return rc;
×
956
      }
957
    }
958
  } else {
959
    tDecoderClear(&dc);
×
960
    tdbFree(pData);
×
961
    metaULock(pMeta);
×
962
    taosHashClear(pColCmprObj);
×
963
    return 0;
×
964
  }
965
  tDecoderClear(&dc);
594,522✔
966
  tdbFree(pData);
594,264✔
967
  metaULock(pMeta);
594,285✔
968

969
  *ppColCmprObj = pColCmprObj;
594,287✔
970
  colCompressDebug(pColCmprObj);
594,287✔
971

972
  return 0;
594,142✔
973
}
974
// refactor later
975
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
8,398✔
976
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
8,398✔
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