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

taosdata / TDengine / #4113

17 May 2025 06:43AM UTC coverage: 62.054% (-0.8%) from 62.857%
#4113

push

travis-ci

web-flow
Merge pull request #31115 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

154737 of 318088 branches covered (48.65%)

Branch coverage included in aggregate %.

175 of 225 new or added lines in 20 files covered. (77.78%)

5853 existing lines in 216 files now uncovered.

239453 of 317147 relevant lines covered (75.5%)

15121865.73 hits per line

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

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

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

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

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

UNCOV
107
int32_t updataTableColRef(SColRefWrapper *pWp, const SSchema *pSchema, int8_t add, SColRef *pColRef) {
×
UNCOV
108
  int32_t nCols = pWp->nCols;
×
UNCOV
109
  if (add) {
×
UNCOV
110
    SColRef *p = taosMemoryRealloc(pWp->pColRef, sizeof(SColRef) * (nCols + 1));
×
UNCOV
111
    if (p == NULL) {
×
112
      return terrno;
×
113
    }
UNCOV
114
    pWp->pColRef = p;
×
115

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

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

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

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

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

171
  return 0;
15,222✔
172
}
173

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

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

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

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

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

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

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

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

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

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

252
    SIndexTerm *term = NULL;
295✔
253
    if (type == TSDB_DATA_TYPE_NULL) {
295✔
254
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
24✔
255
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
271✔
256
      if (pTagVal->nData > 0) {
164✔
257
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
148!
258
        if (val == NULL) {
148!
259
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
260
        }
261
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
148✔
262
        if (len < 0) {
148!
263
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
264
        }
265
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
148✔
266
        type = TSDB_DATA_TYPE_VARCHAR;
148✔
267
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
148✔
268
        taosMemoryFree(val);
148!
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) {
107✔
273
      double val = *(double *)(&pTagVal->i64);
83✔
274
      int    len = sizeof(val);
83✔
275
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
83✔
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) {
295!
283
      int32_t ret = indexMultiTermAdd(terms, term);
295✔
284
      if (ret < 0) {
295!
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);
249✔
294
  indexMultiTermDestroy(terms);
249✔
295

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

499
  return 0;
32✔
500
}
501

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

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

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

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

522
end:
29✔
523
  taosArrayDestroy(tbUids);
32✔
524

525
  return code;
32✔
526
}
527

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

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

533
  metaULock(pMeta);
90,506✔
534

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

539
  return ret;
90,674✔
540
}
541

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

552
  btimeKey->btime = btime;
1,483✔
553
  btimeKey->uid = pME->uid;
1,483✔
554
  return 0;
1,483✔
555
}
556

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

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

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

577
  int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
1,483✔
578
  if (ret < 0) {
1,483!
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;
1,483✔
583
}
584

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

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

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

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

612
  if (type) *type = e.type;
1,483!
613

614
  if (e.type == TSDB_CHILD_TABLE) {
1,483✔
615
    if (pSuid) *pSuid = e.ctbEntry.suid;
1,482!
616
    void *tData = NULL;
1,482✔
617
    int   tLen = 0;
1,482✔
618

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

625
        tDecoderInit(&tdc, tData, tLen);
1,482✔
626
        ret = metaDecodeEntry(&tdc, &stbEntry);
1,482✔
627
        if (ret < 0) {
1,482!
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;
1,482!
635

636
        SSchema        *pTagColumn = NULL;
1,482✔
637
        SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
1,482✔
638
        if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
1,482!
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++) {
3,848✔
647
            pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[i];
2,366✔
648
            if (!IS_IDX_ON(pTagColumn)) continue;
2,366✔
649
            STagIdxKey *pTagIdxKey = NULL;
1,482✔
650
            int32_t     nTagIdxKey;
651

652
            const void *pTagData = NULL;
1,482✔
653
            int32_t     nTagData = 0;
1,482✔
654

655
            STagVal tagVal = {.cid = pTagColumn->colId};
1,482✔
656
            if (tTagGet((const STag *)e.ctbEntry.pTags, &tagVal)) {
1,482!
657
              if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
1,482!
658
                pTagData = tagVal.pData;
×
659
                nTagData = (int32_t)tagVal.nData;
×
660
              } else {
661
                pTagData = &(tagVal.i64);
1,482✔
662
                nTagData = tDataTypes[pTagColumn->type].bytes;
1,482✔
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,
1,482!
671
                                    &pTagIdxKey, &nTagIdxKey) == 0) {
672
              ret = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
1,482✔
673
              if (ret < 0) {
1,482!
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);
1,482✔
679
            pTagIdxKey = NULL;
1,482✔
680
          }
681
        }
682
        tDecoderClear(&tdc);
1,482✔
683
      }
684
      tdbFree(tData);
1,482✔
685
    }
686
  }
687

688
  ret = tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), pMeta->txn);
1,483✔
689
  if (ret < 0) {
1,483!
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);
1,483✔
694
  if (ret < 0) {
1,483!
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);
1,483✔
699
  if (ret < 0) {
1,483!
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);
1,483!
705
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
1,483✔
706

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

709
  if (e.type == TSDB_CHILD_TABLE) {
1,483✔
710
    ret =
711
        tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
1,482✔
712
    if (ret < 0) {
1,482!
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;
1,482✔
718
    metaUpdateStbStats(pMeta, e.ctbEntry.suid, -1, 0, -1);
1,482✔
719
    ret = metaUidCacheClear(pMeta, e.ctbEntry.suid);
1,482✔
720
    if (ret < 0) {
1,482!
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);
1,482✔
725
    if (ret < 0) {
1,482!
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) {
1!
735
    // drop schema.db (todo)
736

737
    --pMeta->pVnode->config.vndStats.numOfNTables;
1✔
738
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
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);
1,483✔
772
  if (ret < 0) {
1,483!
773
    metaError("vgId:%d, failed to drop cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
1,483!
774
              tstrerror(ret));
775
  }
776

777
  tDecoderClear(&dc);
1,483✔
778
  tdbFree(pData);
1,483✔
779

780
  return 0;
1,483✔
781
}
782

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

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

799
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
1,219✔
800
  pMeta->changed = true;
1,219✔
801
  switch (pReq->action) {
1,219!
802
    case TSDB_ALTER_TABLE_ADD_COLUMN:
198✔
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);
198✔
806
    case TSDB_ALTER_TABLE_DROP_COLUMN:
154✔
807
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
154✔
808
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
42✔
809
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
42✔
810
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
32✔
811
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
32✔
812
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
700✔
813
      return metaUpdateTableTagValue(pMeta, version, pReq);
700✔
814
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
5✔
815
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
5✔
816
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
73✔
817
      return metaUpdateTableOptions2(pMeta, version, pReq);
73✔
818
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
15✔
819
      return metaUpdateTableColCompress2(pMeta, version, pReq);
15✔
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) {
12,697,580✔
844
  if (!tsTtlChangeOnWrite) return 0;
12,697,580!
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,
164,310✔
853
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
854
  if (IS_VAR_DATA_TYPE(type)) {
164,310✔
855
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
25,531✔
856
  } else {
857
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
138,779✔
858
  }
859

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

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

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

880
  return 0;
164,344✔
881
}
882

883
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
163,485✔
884
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
163,485!
885
}
163,476✔
886

887
static void colCompressDebug(SHashObj *pColCmprObj) {
1,030,772✔
888
  void *p = taosHashIterate(pColCmprObj, NULL);
1,030,772✔
889
  while (p) {
8,775,938✔
890
    uint32_t cmprAlg = *(uint32_t *)p;
7,745,374✔
891
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
7,745,374✔
892
    p = taosHashIterate(pColCmprObj, p);
7,745,225✔
893

894
    uint8_t l1, l2, lvl;
895
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
7,746,593✔
896

897
    const char *l1str = columnEncodeStr(l1);
7,746,368✔
898
    const char *l2str = columnCompressStr(l2);
7,745,716✔
899
    const char *lvlstr = columnLevelStr(lvl);
7,745,329✔
900
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
7,745,126✔
901
  }
902
  return;
1,030,564✔
903
}
904

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

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

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

919
  *ppColCmprObj = NULL;
1,030,748✔
920

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

937
  tDecoderInit(&dc, pData, nData);
1,030,757✔
938
  rc = metaDecodeEntry(&dc, &e);
1,030,735✔
939
  if (rc < 0) {
1,030,687!
940
    tDecoderClear(&dc);
×
941
    tdbFree(pData);
×
942
    metaULock(pMeta);
×
943
    taosHashClear(pColCmprObj);
×
944
    return rc;
×
945
  }
946
  if (withExtSchema(e.type)) {
1,030,687!
947
    SColCmprWrapper *p = &e.colCmpr;
1,030,683✔
948
    for (int32_t i = 0; i < p->nCols; i++) {
8,777,467✔
949
      SColCmpr *pCmpr = &p->pColCmpr[i];
7,746,457✔
950
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
7,746,457✔
951
      if (rc < 0) {
7,746,818✔
952
        tDecoderClear(&dc);
34✔
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);
1,031,010✔
967
  tdbFree(pData);
1,030,755✔
968
  metaULock(pMeta);
1,030,783✔
969

970
  *ppColCmprObj = pColCmprObj;
1,030,775✔
971
  colCompressDebug(pColCmprObj);
1,030,775✔
972

973
  return 0;
1,030,617✔
974
}
975
// refactor later
976
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
52,492✔
977
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
52,455✔
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