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

taosdata / TDengine / #4666

12 Aug 2025 07:34AM UTC coverage: 60.112% (+0.2%) from 59.901%
#4666

push

travis-ci

web-flow
Merge pull request #32547 from taosdata/refactor/wangxu/get-started-installer

refactor: get started for installer and docker

138109 of 291999 branches covered (47.3%)

Branch coverage included in aggregate %.

208348 of 284354 relevant lines covered (73.27%)

18798400.93 hits per line

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

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

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

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

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

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

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

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

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

171
  return 0;
18,047✔
172
}
173

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

500
  return 0;
74✔
501
}
502

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

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

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

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

523
end:
62✔
524
  taosArrayDestroy(tbUids);
74✔
525

526
  return code;
74✔
527
}
528

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

532
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
93,847✔
533

534
  metaULock(pMeta);
93,511✔
535

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

540
  return ret;
93,598✔
541
}
542

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

553
  btimeKey->btime = btime;
5,270✔
554
  btimeKey->uid = pME->uid;
5,270✔
555
  return 0;
5,270✔
556
}
557

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

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

571
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
5,270✔
572
  if (pME->type == TSDB_CHILD_TABLE) {
5,270✔
573
    ctx.ttlDays = pME->ctbEntry.ttlDays;
5,260✔
574
  } else {
575
    ctx.ttlDays = pME->ntbEntry.ttlDays;
10✔
576
  }
577

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

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

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

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

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

613
  if (type) *type = e.type;
5,270!
614

615
  if (e.type == TSDB_CHILD_TABLE) {
5,270✔
616
    if (pSuid) *pSuid = e.ctbEntry.suid;
5,260!
617
    void *tData = NULL;
5,260✔
618
    int   tLen = 0;
5,260✔
619

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

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

635
        if (pSysTbl) *pSysTbl = metaTbInFilterCache(pMeta, stbEntry.name, 1) ? 1 : 0;
5,260!
636

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

653
            const void *pTagData = NULL;
5,260✔
654
            int32_t     nTagData = 0;
5,260✔
655

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

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

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

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

708
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
5,270!
709

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

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

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

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

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

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

778
  tDecoderClear(&dc);
5,270✔
779
  tdbFree(pData);
5,270✔
780

781
  return 0;
5,270✔
782
}
783

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

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

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

831
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
2,060✔
832
  if (!tsTtlChangeOnWrite) return 0;
2,060!
833

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

839
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
2,060✔
840

841
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
2,060✔
842
}
843

844
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
14,122,315✔
845
  if (!tsTtlChangeOnWrite) return 0;
14,122,315✔
846

847
  metaWLock(pMeta);
2,043✔
848
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
2,060✔
849
  metaULock(pMeta);
2,060✔
850
  return ret;
2,060✔
851
}
852

853
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
293,276✔
854
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
855
  if (IS_VAR_DATA_TYPE(type)) {
293,276!
856
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
38,978✔
857
  } else {
858
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
254,298✔
859
  }
860

861
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
293,276!
862
  if (*ppTagIdxKey == NULL) {
293,346!
863
    return terrno;
×
864
  }
865

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

871
  // refactor
872
  if (IS_VAR_DATA_TYPE(type)) {
293,346✔
873
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
39,041✔
874
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
39,041✔
875
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
39,041✔
876
  } else {
877
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
254,305✔
878
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
254,305✔
879
  }
880

881
  return 0;
293,346✔
882
}
883

884
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
275,725✔
885
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
275,725!
886
}
275,722✔
887

888
static void colCompressDebug(SHashObj *pColCmprObj) {
521,086✔
889
  void *p = taosHashIterate(pColCmprObj, NULL);
521,086✔
890
  while (p) {
3,600,901✔
891
    uint32_t cmprAlg = *(uint32_t *)p;
3,079,938✔
892
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
3,079,938✔
893
    p = taosHashIterate(pColCmprObj, p);
3,079,907✔
894

895
    uint8_t l1, l2, lvl;
896
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
3,080,247✔
897

898
    const char *l1str = columnEncodeStr(l1);
3,080,192✔
899
    const char *l2str = columnCompressStr(l2);
3,080,045✔
900
    const char *lvlstr = columnLevelStr(lvl);
3,079,887✔
901
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
3,079,812✔
902
  }
903
  return;
520,963✔
904
}
905

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

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

915
  void      *pData = NULL;
521,070✔
916
  int        nData = 0;
521,070✔
917
  SMetaEntry e = {0};
521,070✔
918
  SDecoder   dc = {0};
521,070✔
919

920
  *ppColCmprObj = NULL;
521,070✔
921

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

938
  tDecoderInit(&dc, pData, nData);
521,084✔
939
  rc = metaDecodeEntry(&dc, &e);
521,081✔
940
  if (rc < 0) {
521,035!
941
    tDecoderClear(&dc);
×
942
    tdbFree(pData);
×
943
    metaULock(pMeta);
×
944
    taosHashCleanup(pColCmprObj);
×
945
    return rc;
×
946
  }
947
  if (withExtSchema(e.type)) {
521,035!
948
    SColCmprWrapper *p = &e.colCmpr;
521,042✔
949
    for (int32_t i = 0; i < p->nCols; i++) {
3,601,619✔
950
      SColCmpr *pCmpr = &p->pColCmpr[i];
3,080,393✔
951
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
3,080,393✔
952
      if (rc < 0) {
3,080,589✔
953
        tDecoderClear(&dc);
12✔
954
        tdbFree(pData);
×
955
        metaULock(pMeta);
×
956
        taosHashCleanup(pColCmprObj);
×
957
        return rc;
×
958
      }
959
    }
960
  } else {
961
    tDecoderClear(&dc);
×
962
    tdbFree(pData);
×
963
    metaULock(pMeta);
×
964
    taosHashCleanup(pColCmprObj);
×
965
    return 0;
×
966
  }
967
  tDecoderClear(&dc);
521,226✔
968
  tdbFree(pData);
521,078✔
969
  metaULock(pMeta);
521,085✔
970

971
  *ppColCmprObj = pColCmprObj;
521,087✔
972
  colCompressDebug(pColCmprObj);
521,087✔
973

974
  return 0;
520,987✔
975
}
976
// refactor later
977
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
50,887✔
978
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
50,830✔
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