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

taosdata / TDengine / #4348

21 Jun 2025 07:48AM UTC coverage: 62.366% (+1.8%) from 60.571%
#4348

push

travis-ci

web-flow
docs: add OpenMetrics support and configuration details to taosAdapter documentation (#31427)

* docs: add OpenMetrics support and configuration details to taosAdapter documentation

* docs: enhance OpenMetrics section in taosAdapter documentation

156282 of 319947 branches covered (48.85%)

Branch coverage included in aggregate %.

242147 of 318911 relevant lines covered (75.93%)

6151642.33 hits per line

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

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

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

73
int32_t addTableExtSchema(SMetaEntry *pEntry, const SSchema *pColumn, int32_t newColNum, SExtSchema *pExtSchema) {
721✔
74
  // no need to add ext schema when no column needs ext schemas
75
  if (!HAS_TYPE_MOD(pColumn) && !pEntry->pExtSchemas) return 0;
721!
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) {
164✔
90
  // no ext schema, no need to drop
91
  if (!pEntry->pExtSchemas) return 0;
164✔
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) {
12✔
108
  int32_t nCols = pWp->nCols;
12✔
109
  if (add) {
12✔
110
    SColRef *p = taosMemoryRealloc(pWp->pColRef, sizeof(SColRef) * (nCols + 1));
6!
111
    if (p == NULL) {
6!
112
      return terrno;
×
113
    }
114
    pWp->pColRef = p;
6✔
115

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

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

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

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

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

171
  return 0;
15,797✔
172
}
173

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

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

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

210
  return code;
30✔
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) {
433✔
219
  int32_t code = 0;
433✔
220

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

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

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

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

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

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

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

282
    if (term != NULL) {
642!
283
      int32_t ret = indexMultiTermAdd(terms, term);
642✔
284
      if (ret < 0) {
642!
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);
433✔
294
  indexMultiTermDestroy(terms);
433✔
295

296
  taosArrayDestroy(pTagVals);
433✔
297
  return code;
433✔
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) {
204✔
305
int32_t code = 0;
204✔
306
#ifdef USE_INVERTED_INDEX
307
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
204!
308
    return TSDB_CODE_INVALID_PARA;
×
309
  }
310
  void       *data = pCtbEntry->ctbEntry.pTags;
204✔
311
  const char *tagName = pSchema->name;
204✔
312

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

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

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

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

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

337
    SIndexTerm *term = NULL;
383✔
338
    if (type == TSDB_DATA_TYPE_NULL) {
383!
339
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
×
340
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
383✔
341
      if (pTagVal->nData > 0) {
363!
342
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
363!
343
        if (val == NULL) {
363!
344
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
345
        }
346
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
363✔
347
        if (len < 0) {
363!
348
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
349
        }
350
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
363✔
351
        type = TSDB_DATA_TYPE_VARCHAR;
363✔
352
        term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, val, len);
363✔
353
        taosMemoryFree(val);
363!
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) {
20✔
358
      double val = *(double *)(&pTagVal->i64);
12✔
359
      int    len = sizeof(val);
12✔
360
      term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, (const char *)&val, len);
12✔
361
    } else if (type == TSDB_DATA_TYPE_BOOL) {
8!
362
      int val = *(int *)(&pTagVal->i64);
8✔
363
      int len = sizeof(val);
8✔
364
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
8✔
365
    }
366
    if (term != NULL) {
383!
367
      int32_t ret = indexMultiTermAdd(terms, term);
383✔
368
      if (ret < 0) {
383!
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);
204✔
378
  indexMultiTermDestroy(terms);
204✔
379
  taosArrayDestroy(pTagVals);
204✔
380
  return code;
204✔
381
_exception:
×
382
  indexMultiTermDestroy(terms);
×
383
  taosArrayDestroy(pTagVals);
×
384
#endif
385
  return code;
×
386
}
387

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

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

398
  metaWLock(pMeta);
27✔
399
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
257✔
400
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
230✔
401
    tb_uid_t suid = 0;
230✔
402
    int8_t   sysTbl = 0;
230✔
403
    int      type;
404
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
230✔
405
    if (code) return code;
230!
406
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
230!
407
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
229✔
408
      if (pVal) {
229✔
409
        nCtbDropped = *pVal + 1;
203✔
410
      } else {
411
        nCtbDropped = 1;
26✔
412
      }
413
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
229✔
414
      if (code) return code;
229!
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);
230✔
422
  }
423
  metaULock(pMeta);
27✔
424

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

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

445
static int32_t metaFilterTableByHash(SMeta *pMeta, SArray *uidList) {
30✔
446
  int32_t code = 0;
30✔
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);
30✔
453
  if (code < 0) {
30!
454
    return code;
×
455
  }
456

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

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

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

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

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

499
  return 0;
30✔
500
}
501

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

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

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

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

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

525
  return code;
30✔
526
}
527

528
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
18,942✔
529
  metaRLock(pMeta);
18,942✔
530

531
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
18,977✔
532

533
  metaULock(pMeta);
18,961✔
534

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

539
  return ret;
18,961✔
540
}
541

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

780
  return 0;
230✔
781
}
782

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

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

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

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

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

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

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

843
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
3,500,015✔
844
  if (!tsTtlChangeOnWrite) return 0;
3,500,015!
845

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

852
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
156,050✔
853
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
854
  if (IS_VAR_DATA_TYPE(type)) {
156,050✔
855
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
32,215✔
856
  } else {
857
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
123,835✔
858
  }
859

860
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
156,050!
861
  if (*ppTagIdxKey == NULL) {
156,084!
862
    return terrno;
×
863
  }
864

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

870
  // refactor
871
  if (IS_VAR_DATA_TYPE(type)) {
156,084!
872
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
32,250✔
873
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
32,250✔
874
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
32,250✔
875
  } else {
876
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
123,834✔
877
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
123,834✔
878
  }
879

880
  return 0;
156,084✔
881
}
882

883
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
155,221✔
884
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
155,221!
885
}
155,222✔
886

887
static void colCompressDebug(SHashObj *pColCmprObj) {
681,140✔
888
  void *p = taosHashIterate(pColCmprObj, NULL);
681,140✔
889
  while (p) {
7,255,156✔
890
    uint32_t cmprAlg = *(uint32_t *)p;
6,574,231✔
891
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
6,574,231✔
892
    p = taosHashIterate(pColCmprObj, p);
6,574,179✔
893

894
    uint8_t l1, l2, lvl;
895
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
6,575,526✔
896

897
    const char *l1str = columnEncodeStr(l1);
6,575,301✔
898
    const char *l2str = columnCompressStr(l2);
6,574,592✔
899
    const char *lvlstr = columnLevelStr(lvl);
6,574,196✔
900
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
6,573,961✔
901
  }
902
  return;
680,925✔
903
}
904

905
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
681,101✔
906
  int rc = 0;
681,101✔
907

908
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
681,101✔
909
  if (pColCmprObj == NULL) {
681,117!
910
    pColCmprObj = NULL;
×
911
    return TSDB_CODE_OUT_OF_MEMORY;
×
912
  }
913

914
  void      *pData = NULL;
681,117✔
915
  int        nData = 0;
681,117✔
916
  SMetaEntry e = {0};
681,117✔
917
  SDecoder   dc = {0};
681,117✔
918

919
  *ppColCmprObj = NULL;
681,117✔
920

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

937
  tDecoderInit(&dc, pData, nData);
681,133✔
938
  rc = metaDecodeEntry(&dc, &e);
681,115✔
939
  if (rc < 0) {
681,071!
940
    tDecoderClear(&dc);
×
941
    tdbFree(pData);
×
942
    metaULock(pMeta);
×
943
    taosHashClear(pColCmprObj);
×
944
    return rc;
×
945
  }
946
  if (withExtSchema(e.type)) {
681,071!
947
    SColCmprWrapper *p = &e.colCmpr;
681,051✔
948
    for (int32_t i = 0; i < p->nCols; i++) {
7,256,583✔
949
      SColCmpr *pCmpr = &p->pColCmpr[i];
6,575,162✔
950
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
6,575,162✔
951
      if (rc < 0) {
6,575,606✔
952
        tDecoderClear(&dc);
74✔
953
        tdbFree(pData);
×
954
        metaULock(pMeta);
×
955
        taosHashClear(pColCmprObj);
×
956
        return rc;
×
957
      }
958
    }
959
  } else {
960
    tDecoderClear(&dc);
×
961
    tdbFree(pData);
×
962
    metaULock(pMeta);
×
963
    taosHashClear(pColCmprObj);
×
964
    return 0;
×
965
  }
966
  tDecoderClear(&dc);
681,421✔
967
  tdbFree(pData);
681,083✔
968
  metaULock(pMeta);
681,112✔
969

970
  *ppColCmprObj = pColCmprObj;
681,144✔
971
  colCompressDebug(pColCmprObj);
681,144✔
972

973
  return 0;
681,058✔
974
}
975
// refactor later
976
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
8,794✔
977
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
8,793✔
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