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

taosdata / TDengine / #3831

02 Apr 2025 01:14AM UTC coverage: 34.081% (-0.02%) from 34.097%
#3831

push

travis-ci

happyguoxy
test:alter gcda dir

148596 of 599532 branches covered (24.79%)

Branch coverage included in aggregate %.

222550 of 489473 relevant lines covered (45.47%)

1589752.67 hits per line

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

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

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

73
int32_t addTableExtSchema(SMetaEntry *pEntry, const SSchema *pColumn, int32_t newColNum, SExtSchema *pExtSchema) {
28✔
74
  // no need to add ext schema when no column needs ext schemas
75
  if (!HAS_TYPE_MOD(pColumn) && !pEntry->pExtSchemas) return 0;
28!
76
  if (!pEntry->pExtSchemas) {
×
77
    // add a column which needs ext schema
78
    // set all extschemas to zero for all columns alrady existed
79
    pEntry->pExtSchemas = (SExtSchema *)taosMemoryCalloc(newColNum, sizeof(SExtSchema));
×
80
  } else {
81
    // already has columns with ext schema
82
    pEntry->pExtSchemas = (SExtSchema *)taosMemoryRealloc(pEntry->pExtSchemas, sizeof(SExtSchema) * newColNum);
×
83
  }
84
  if (!pEntry->pExtSchemas) return terrno;
×
85
  pEntry->pExtSchemas[newColNum - 1] = *pExtSchema;
×
86
  return 0;
×
87
}
88

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

107
int32_t updataTableColRef(SColRefWrapper *pWp, const SSchema *pSchema, int8_t add, SColRef *pColRef) {
×
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) {
132✔
151
  pMetaRsp->pSchemas = taosMemoryMalloc(pSchema->nCols * sizeof(SSchema));
132!
152
  if (NULL == pMetaRsp->pSchemas) {
132!
153
    return terrno;
×
154
  }
155

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

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

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

171
  return 0;
132✔
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) {
×
218
  int32_t code = 0;
×
219

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

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

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

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

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

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

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

281
    if (term != NULL) {
×
282
      int32_t ret = indexMultiTermAdd(terms, term);
×
283
      if (ret < 0) {
×
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);
×
293
  indexMultiTermDestroy(terms);
×
294

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

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

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

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

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

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

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

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

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

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

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

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

441
static int32_t metaFilterTableByHash(SMeta *pMeta, SArray *uidList) {
×
442
  int32_t code = 0;
×
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);
×
449
  if (code < 0) {
×
450
    return code;
×
451
  }
452

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

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

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

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

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

495
  return 0;
×
496
}
497

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

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

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

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

518
end:
×
519
  taosArrayDestroy(tbUids);
×
520

521
  return code;
×
522
}
523

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

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

529
  metaULock(pMeta);
972✔
530

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

535
  return ret;
972✔
536
}
537

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

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

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

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

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

573
  int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
×
574
  if (ret < 0) {
×
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;
×
579
}
580

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

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

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

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

608
  if (type) *type = e.type;
×
609

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

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

621
        tDecoderInit(&tdc, tData, tLen);
×
622
        ret = metaDecodeEntry(&tdc, &stbEntry);
×
623
        if (ret < 0) {
×
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;
×
631

632
        SSchema        *pTagColumn = NULL;
×
633
        SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
×
634
        if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
×
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++) {
×
643
            pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[i];
×
644
            if (!IS_IDX_ON(pTagColumn)) continue;
×
645
            STagIdxKey *pTagIdxKey = NULL;
×
646
            int32_t     nTagIdxKey;
647

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

651
            STagVal tagVal = {.cid = pTagColumn->colId};
×
652
            if (tTagGet((const STag *)e.ctbEntry.pTags, &tagVal)) {
×
653
              if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
×
654
                pTagData = tagVal.pData;
×
655
                nTagData = (int32_t)tagVal.nData;
×
656
              } else {
657
                pTagData = &(tagVal.i64);
×
658
                nTagData = tDataTypes[pTagColumn->type].bytes;
×
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,
×
667
                                    &pTagIdxKey, &nTagIdxKey) == 0) {
668
              ret = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
×
669
              if (ret < 0) {
×
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);
×
675
            pTagIdxKey = NULL;
×
676
          }
677
        }
678
        tDecoderClear(&tdc);
×
679
      }
680
      tdbFree(tData);
×
681
    }
682
  }
683

684
  ret = tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), pMeta->txn);
×
685
  if (ret < 0) {
×
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);
×
690
  if (ret < 0) {
×
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);
×
695
  if (ret < 0) {
×
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);
×
701
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
×
702

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

705
  if (e.type == TSDB_CHILD_TABLE) {
×
706
    ret =
707
        tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
×
708
    if (ret < 0) {
×
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;
×
714
    metaUpdateStbStats(pMeta, e.ctbEntry.suid, -1, 0, -1);
×
715
    ret = metaUidCacheClear(pMeta, e.ctbEntry.suid);
×
716
    if (ret < 0) {
×
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);
×
721
    if (ret < 0) {
×
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) {
×
731
    // drop schema.db (todo)
732

733
    --pMeta->pVnode->config.vndStats.numOfNTables;
×
734
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 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);
×
768
  if (ret < 0) {
×
769
    metaError("vgId:%d, failed to drop cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
770
              tstrerror(ret));
771
  }
772

773
  tDecoderClear(&dc);
×
774
  tdbFree(pData);
×
775

776
  return 0;
×
777
}
778

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

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

795
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
42✔
796
  pMeta->changed = true;
42✔
797
  switch (pReq->action) {
42!
798
    case TSDB_ALTER_TABLE_ADD_COLUMN:
28✔
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);
28✔
802
    case TSDB_ALTER_TABLE_DROP_COLUMN:
10✔
803
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
10✔
804
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
×
805
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
×
806
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
×
807
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
×
808
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
4✔
809
      return metaUpdateTableTagValue(pMeta, version, pReq);
4✔
810
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
×
811
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
×
812
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
×
813
      return metaUpdateTableOptions2(pMeta, version, pReq);
×
814
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
×
815
      return metaUpdateTableColCompress2(pMeta, version, pReq);
×
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) {
1,440,830✔
840
  if (!tsTtlChangeOnWrite) return 0;
1,440,830!
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,
61,622✔
849
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
850
  if (IS_VAR_DATA_TYPE(type)) {
61,622!
851
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
8,060✔
852
  } else {
853
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
53,562✔
854
  }
855

856
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
61,622!
857
  if (*ppTagIdxKey == NULL) {
61,626!
858
    return terrno;
×
859
  }
860

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

866
  // refactor
867
  if (IS_VAR_DATA_TYPE(type)) {
61,626!
868
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
8,066✔
869
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
8,066✔
870
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
8,066✔
871
  } else {
872
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
53,560!
873
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
53,560✔
874
  }
875

876
  return 0;
61,626✔
877
}
878

879
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
61,616✔
880
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
61,616!
881
}
61,614✔
882

883
static void colCompressDebug(SHashObj *pColCmprObj) {
8,924✔
884
  void *p = taosHashIterate(pColCmprObj, NULL);
8,924✔
885
  while (p) {
266,986✔
886
    uint32_t cmprAlg = *(uint32_t *)p;
258,062✔
887
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
258,062✔
888
    p = taosHashIterate(pColCmprObj, p);
258,062✔
889

890
    uint8_t l1, l2, lvl;
891
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
258,070✔
892

893
    const char *l1str = columnEncodeStr(l1);
258,064✔
894
    const char *l2str = columnCompressStr(l2);
258,058✔
895
    const char *lvlstr = columnLevelStr(lvl);
258,060✔
896
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
258,062✔
897
  }
898
  return;
8,924✔
899
}
900

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

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

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

915
  *ppColCmprObj = NULL;
8,924✔
916

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

933
  tDecoderInit(&dc, pData, nData);
8,924✔
934
  rc = metaDecodeEntry(&dc, &e);
8,924✔
935
  if (rc < 0) {
8,920!
936
    tDecoderClear(&dc);
×
937
    tdbFree(pData);
×
938
    metaULock(pMeta);
×
939
    taosHashClear(pColCmprObj);
×
940
    return rc;
×
941
  }
942
  if (withExtSchema(e.type)) {
8,920!
943
    SColCmprWrapper *p = &e.colCmpr;
8,922✔
944
    for (int32_t i = 0; i < p->nCols; i++) {
266,992✔
945
      SColCmpr *pCmpr = &p->pColCmpr[i];
258,068✔
946
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
258,068✔
947
      if (rc < 0) {
258,070!
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);
8,924✔
963
  tdbFree(pData);
8,924✔
964
  metaULock(pMeta);
8,924✔
965

966
  *ppColCmprObj = pColCmprObj;
8,924✔
967
  colCompressDebug(pColCmprObj);
8,924✔
968

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