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

taosdata / TDengine / #3663

19 Mar 2025 09:21AM UTC coverage: 61.664% (-0.6%) from 62.28%
#3663

push

travis-ci

web-flow
docs: add defination of tmq_config_res_t & fix spell error (#30271)

153169 of 318241 branches covered (48.13%)

Branch coverage included in aggregate %.

239405 of 318390 relevant lines covered (75.19%)

5762846.6 hits per line

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

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

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

73
int32_t addTableExtSchema(SMetaEntry *pEntry, const SSchema *pColumn, int32_t newColNum, SExtSchema *pExtSchema) {
176✔
74
  // no need to add ext schema when no column needs ext schemas
75
  if (!HAS_TYPE_MOD(pColumn) && !pEntry->pExtSchemas) return 0;
176!
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) {
127✔
90
  // no ext schema, no need to drop
91
  if (!pEntry->pExtSchemas) return 0;
127✔
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) {
13,950✔
151
  pMetaRsp->pSchemas = taosMemoryMalloc(pSchema->nCols * sizeof(SSchema));
13,950!
152
  if (NULL == pMetaRsp->pSchemas) {
13,950!
153
    return terrno;
×
154
  }
155

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

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

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

171
  return 0;
13,949✔
172
}
173

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

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

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

209
  return code;
×
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
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
26✔
431
    if (metaGetStbStats(pMeta->pVnode, *pSuid, NULL, &nCols) == 0) {
26!
432
      pStats->numOfTimeSeries -= *(int64_t *)pCtbDropped * (nCols - 1);
26✔
433
    }
434
  }
435
  tSimpleHashCleanup(suidHash);
27✔
436

437
  pMeta->changed = true;
27✔
438
  return 0;
27✔
439
}
440

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

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

453
  code = tdbTbcMoveToFirst(pCur);
30✔
454
  if (code) {
30!
455
    tdbTbcClose(pCur);
×
456
    return code;
×
457
  }
458

459
  void *pData = NULL, *pKey = NULL;
30✔
460
  int   nData = 0, nKey = 0;
30✔
461

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

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

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

495
  return 0;
30✔
496
}
497

498
int32_t metaTrimTables(SMeta *pMeta, int64_t version) {
30✔
499
  int32_t code = 0;
30✔
500

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

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

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

518
end:
27✔
519
  taosArrayDestroy(tbUids);
30✔
520

521
  return code;
30✔
522
}
523

524
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
17,132✔
525
  metaRLock(pMeta);
17,132✔
526

527
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
17,161✔
528

529
  metaULock(pMeta);
17,113✔
530

531
  if (ret != 0) {
17,147!
532
    metaError("ttl failed to find expired table, ret:%d", ret);
×
533
  }
534

535
  return ret;
17,153✔
536
}
537

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

548
  btimeKey->btime = btime;
230✔
549
  btimeKey->uid = pME->uid;
230✔
550
  return 0;
230✔
551
}
552

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

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

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

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

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

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

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

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

608
  if (type) *type = e.type;
230!
609

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

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

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

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

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

648
            const void *pTagData = NULL;
229✔
649
            int32_t     nTagData = 0;
229✔
650

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

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

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

700
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
230!
701
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
230✔
702

703
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
230!
704

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

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

733
    --pMeta->pVnode->config.vndStats.numOfNTables;
1✔
734
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
1✔
735

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

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

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

773
  tDecoderClear(&dc);
230✔
774
  tdbFree(pData);
230✔
775

776
  return 0;
230✔
777
}
778

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

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

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

826
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
×
827
  if (!tsTtlChangeOnWrite) return 0;
×
828

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

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

836
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
×
837
}
838

839
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
2,954,164✔
840
  if (!tsTtlChangeOnWrite) return 0;
2,954,164!
841

842
  metaWLock(pMeta);
×
843
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
×
844
  metaULock(pMeta);
×
845
  return ret;
×
846
}
847

848
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
124,182✔
849
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
850
  if (IS_VAR_DATA_TYPE(type)) {
124,182✔
851
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
16,922✔
852
  } else {
853
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
107,260✔
854
  }
855

856
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
124,182!
857
  if (*ppTagIdxKey == NULL) {
124,230!
858
    return terrno;
×
859
  }
860

861
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
124,230✔
862
  (*ppTagIdxKey)->cid = cid;
124,230✔
863
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
124,230✔
864
  (*ppTagIdxKey)->type = type;
124,230✔
865

866
  // refactor
867
  if (IS_VAR_DATA_TYPE(type)) {
124,230!
868
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
16,976✔
869
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
16,976✔
870
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
16,976✔
871
  } else {
872
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
107,254✔
873
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
107,254✔
874
  }
875

876
  return 0;
124,230✔
877
}
878

879
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
123,398✔
880
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
123,398!
881
}
123,409✔
882

883
static void colCompressDebug(SHashObj *pColCmprObj) {
664,151✔
884
  void *p = taosHashIterate(pColCmprObj, NULL);
664,151✔
885
  while (p) {
6,650,948✔
886
    uint32_t cmprAlg = *(uint32_t *)p;
5,986,932✔
887
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
5,986,932✔
888
    p = taosHashIterate(pColCmprObj, p);
5,986,805✔
889

890
    uint8_t l1, l2, lvl;
891
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
5,987,509✔
892

893
    const char *l1str = columnEncodeStr(l1);
5,987,240✔
894
    const char *l2str = columnCompressStr(l2);
5,987,043✔
895
    const char *lvlstr = columnLevelStr(lvl);
5,986,860✔
896
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
5,986,722✔
897
  }
898
  return;
664,016✔
899
}
900

901
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
664,135✔
902
  int rc = 0;
664,135✔
903

904
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
664,135✔
905
  if (pColCmprObj == NULL) {
664,139!
906
    pColCmprObj = NULL;
×
907
    return TSDB_CODE_OUT_OF_MEMORY;
×
908
  }
909

910
  void      *pData = NULL;
664,139✔
911
  int        nData = 0;
664,139✔
912
  SMetaEntry e = {0};
664,139✔
913
  SDecoder   dc = {0};
664,139✔
914

915
  *ppColCmprObj = NULL;
664,139✔
916

917
  metaRLock(pMeta);
664,139✔
918
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
664,196✔
919
  if (rc < 0) {
664,180!
920
    taosHashClear(pColCmprObj);
×
921
    metaULock(pMeta);
×
922
    return TSDB_CODE_FAILED;
×
923
  }
924
  int64_t version = ((SUidIdxVal *)pData)[0].version;
664,180✔
925
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
664,180✔
926
  if (rc < 0) {
664,163!
927
    metaULock(pMeta);
×
928
    taosHashClear(pColCmprObj);
×
929
    metaError("failed to get table entry");
×
930
    return rc;
×
931
  }
932

933
  tDecoderInit(&dc, pData, nData);
664,163✔
934
  rc = metaDecodeEntry(&dc, &e);
664,156✔
935
  if (rc < 0) {
664,111!
936
    tDecoderClear(&dc);
×
937
    tdbFree(pData);
×
938
    metaULock(pMeta);
×
939
    taosHashClear(pColCmprObj);
×
940
    return rc;
×
941
  }
942
  if (withExtSchema(e.type)) {
664,111!
943
    SColCmprWrapper *p = &e.colCmpr;
664,131✔
944
    for (int32_t i = 0; i < p->nCols; i++) {
6,651,436✔
945
      SColCmpr *pCmpr = &p->pColCmpr[i];
5,987,035✔
946
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
5,987,035✔
947
      if (rc < 0) {
5,987,299!
948
        tDecoderClear(&dc);
×
949
        tdbFree(pData);
×
950
        metaULock(pMeta);
×
951
        taosHashClear(pColCmprObj);
×
952
        return rc;
×
953
      }
954
    }
955
  } else {
956
    tDecoderClear(&dc);
×
957
    tdbFree(pData);
×
958
    metaULock(pMeta);
×
959
    taosHashClear(pColCmprObj);
×
960
    return 0;
×
961
  }
962
  tDecoderClear(&dc);
664,401✔
963
  tdbFree(pData);
664,146✔
964
  metaULock(pMeta);
664,180✔
965

966
  *ppColCmprObj = pColCmprObj;
664,178✔
967
  colCompressDebug(pColCmprObj);
664,178✔
968

969
  return 0;
664,091✔
970
}
971
// refactor later
972
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
8,078✔
973
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
8,070✔
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