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

taosdata / TDengine / #4143

24 May 2025 03:30AM UTC coverage: 32.868% (-29.4%) from 62.238%
#4143

push

travis-ci

web-flow
test: migrate stream cases (#31164)

76401 of 312956 branches covered (24.41%)

Branch coverage included in aggregate %.

128686 of 311012 relevant lines covered (41.38%)

579734.08 hits per line

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

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

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

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

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

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

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

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

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

171
  return 0;
19✔
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
  pMetaRsp->rversion = pRef->version;
×
209

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

499
  return 0;
×
500
}
501

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

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

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

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

522
end:
×
523
  taosArrayDestroy(tbUids);
×
524

525
  return code;
×
526
}
527

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

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

533
  metaULock(pMeta);
58✔
534

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

539
  return ret;
58✔
540
}
541

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

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

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

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

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

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

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

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

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

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

612
  if (type) *type = e.type;
×
613

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

777
  tDecoderClear(&dc);
×
778
  tdbFree(pData);
×
779

780
  return 0;
×
781
}
782

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

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

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

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

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

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

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

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

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

852
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
93✔
853
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
854
  if (IS_VAR_DATA_TYPE(type)) {
93!
855
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
4✔
856
  } else {
857
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
89✔
858
  }
859

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

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

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

880
  return 0;
93✔
881
}
882

883
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
93✔
884
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
93!
885
}
93✔
886

887
static void colCompressDebug(SHashObj *pColCmprObj) {
56✔
888
  void *p = taosHashIterate(pColCmprObj, NULL);
56✔
889
  while (p) {
343✔
890
    uint32_t cmprAlg = *(uint32_t *)p;
287✔
891
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
287✔
892
    p = taosHashIterate(pColCmprObj, p);
287✔
893

894
    uint8_t l1, l2, lvl;
895
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
287✔
896

897
    const char *l1str = columnEncodeStr(l1);
287✔
898
    const char *l2str = columnCompressStr(l2);
287✔
899
    const char *lvlstr = columnLevelStr(lvl);
287✔
900
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
287!
901
  }
902
  return;
56✔
903
}
904

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

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

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

919
  *ppColCmprObj = NULL;
56✔
920

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

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

970
  *ppColCmprObj = pColCmprObj;
56✔
971
  colCompressDebug(pColCmprObj);
56✔
972

973
  return 0;
56✔
974
}
975
// refactor later
976
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
98✔
977
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
97✔
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