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

taosdata / TDengine / #4041

09 May 2025 07:58AM UTC coverage: 62.508% (-0.3%) from 62.788%
#4041

push

travis-ci

web-flow
enh: update database fetch functions to include status in JSON output (#31005)

155567 of 317611 branches covered (48.98%)

Branch coverage included in aggregate %.

15 of 18 new or added lines in 1 file covered. (83.33%)

3906 existing lines in 185 files now uncovered.

240901 of 316655 relevant lines covered (76.08%)

6304979.72 hits per line

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

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

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

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

117
    SColRef *pCol = p + nCols;
6✔
118
    if (NULL == pColRef) {
6✔
119
      pCol->hasRef = false;
3✔
120
      pCol->id = pSchema->colId;
3✔
121
    } else {
122
      pCol->hasRef = pColRef->hasRef;
3✔
123
      pCol->id = pSchema->colId;
3✔
124
      if (pCol->hasRef) {
3!
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;
6✔
131
    pWp->version = ver;
6✔
132
  } else {
133
    for (int32_t i = 0; i < nCols; i++) {
24!
134
      SColRef *pOColRef = &pWp->pColRef[i];
24✔
135
      if (pOColRef->id == pSchema->colId) {
24✔
136
        int32_t left = (nCols - i - 1) * sizeof(SColRef);
6✔
137
        if (left) {
6!
138
          memmove(pWp->pColRef + i, pWp->pColRef + i + 1, left);
×
139
        }
140
        nCols--;
6✔
141
        break;
6✔
142
      }
143
    }
144
    pWp->nCols = nCols;
6✔
145
    pWp->version = ver;
6✔
146
  }
147
  return 0;
12✔
148
}
149

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

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

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

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

171
  return 0;
13,916✔
172
}
173

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

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

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

209
  return code;
14✔
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) {
245✔
218
  int32_t code = 0;
245✔
219

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

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

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

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

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

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

251
    SIndexTerm *term = NULL;
287✔
252
    if (type == TSDB_DATA_TYPE_NULL) {
287✔
253
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
24✔
254
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
263✔
255
      if (pTagVal->nData > 0) {
160✔
256
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
144!
257
        if (val == NULL) {
144!
258
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
259
        }
260
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
144✔
261
        if (len < 0) {
144!
262
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
263
        }
264
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
144✔
265
        type = TSDB_DATA_TYPE_VARCHAR;
144✔
266
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
144✔
267
        taosMemoryFree(val);
144!
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) {
287!
282
      int32_t ret = indexMultiTermAdd(terms, term);
287✔
283
      if (ret < 0) {
287!
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);
245✔
293
  indexMultiTermDestroy(terms);
245✔
294

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

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

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

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

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

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

336
    SIndexTerm *term = NULL;
32✔
337
    if (type == TSDB_DATA_TYPE_NULL) {
32!
338
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
×
339
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
32✔
340
      if (pTagVal->nData > 0) {
12!
341
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
12!
342
        if (val == NULL) {
12!
343
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
344
        }
345
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
12✔
346
        if (len < 0) {
12!
347
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
348
        }
349
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
12✔
350
        type = TSDB_DATA_TYPE_VARCHAR;
12✔
351
        term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, val, len);
12✔
352
        taosMemoryFree(val);
12!
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) {
32!
366
      int32_t ret = indexMultiTermAdd(terms, term);
32✔
367
      if (ret < 0) {
32!
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);
16✔
377
  indexMultiTermDestroy(terms);
16✔
378
  taosArrayDestroy(pTagVals);
16✔
379
  return code;
16✔
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) {
16,326✔
528
  metaRLock(pMeta);
16,326✔
529

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

532
  metaULock(pMeta);
16,328✔
533

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

538
  return ret;
16,353✔
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) {
1,133✔
799
  pMeta->changed = true;
1,133✔
800
  switch (pReq->action) {
1,133!
801
    case TSDB_ALTER_TABLE_ADD_COLUMN:
178✔
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);
178✔
805
    case TSDB_ALTER_TABLE_DROP_COLUMN:
141✔
806
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
141✔
807
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
42✔
808
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
42✔
809
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
32✔
810
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
32✔
811
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
657✔
812
      return metaUpdateTableTagValue(pMeta, version, pReq);
657✔
813
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
5✔
814
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
5✔
815
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
73✔
816
      return metaUpdateTableOptions2(pMeta, version, pReq);
73✔
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

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

UNCOV
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

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

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

842
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
2,951,828✔
843
  if (!tsTtlChangeOnWrite) return 0;
2,951,828!
844

845
  metaWLock(pMeta);
×
UNCOV
846
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
×
UNCOV
847
  metaULock(pMeta);
×
UNCOV
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,
128,756✔
852
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
853
  if (IS_VAR_DATA_TYPE(type)) {
128,756✔
854
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
24,446✔
855
  } else {
856
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
104,310✔
857
  }
858

859
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
128,756!
860
  if (*ppTagIdxKey == NULL) {
128,783!
861
    return terrno;
×
862
  }
863

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

869
  // refactor
870
  if (IS_VAR_DATA_TYPE(type)) {
128,783!
871
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
24,471✔
872
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
24,471✔
873
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
24,471✔
874
  } else {
875
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
104,312✔
876
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
104,312✔
877
  }
878

879
  return 0;
128,783✔
880
}
881

882
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
127,924✔
883
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
127,924!
884
}
127,926✔
885

886
static void colCompressDebug(SHashObj *pColCmprObj) {
685,735✔
887
  void *p = taosHashIterate(pColCmprObj, NULL);
685,735✔
888
  while (p) {
6,873,952✔
889
    uint32_t cmprAlg = *(uint32_t *)p;
6,188,352✔
890
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
6,188,352✔
891
    p = taosHashIterate(pColCmprObj, p);
6,188,232✔
892

893
    uint8_t l1, l2, lvl;
894
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
6,189,370✔
895

896
    const char *l1str = columnEncodeStr(l1);
6,189,026✔
897
    const char *l2str = columnCompressStr(l2);
6,188,529✔
898
    const char *lvlstr = columnLevelStr(lvl);
6,188,277✔
899
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
6,188,124✔
900
  }
901
  return;
685,600✔
902
}
903

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

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

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

918
  *ppColCmprObj = NULL;
685,747✔
919

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

936
  tDecoderInit(&dc, pData, nData);
685,752✔
937
  rc = metaDecodeEntry(&dc, &e);
685,733✔
938
  if (rc < 0) {
685,692!
939
    tDecoderClear(&dc);
×
940
    tdbFree(pData);
×
941
    metaULock(pMeta);
×
942
    taosHashClear(pColCmprObj);
×
943
    return rc;
×
944
  }
945
  if (withExtSchema(e.type)) {
685,692!
946
    SColCmprWrapper *p = &e.colCmpr;
685,703✔
947
    for (int32_t i = 0; i < p->nCols; i++) {
6,875,508✔
948
      SColCmpr *pCmpr = &p->pColCmpr[i];
6,189,458✔
949
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
6,189,458✔
950
      if (rc < 0) {
6,189,800!
UNCOV
951
        tDecoderClear(&dc);
×
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);
686,050✔
966
  tdbFree(pData);
685,737✔
967
  metaULock(pMeta);
685,746✔
968

969
  *ppColCmprObj = pColCmprObj;
685,768✔
970
  colCompressDebug(pColCmprObj);
685,768✔
971

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