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

taosdata / TDengine / #4069

12 May 2025 05:35AM UTC coverage: 63.048% (+0.5%) from 62.547%
#4069

push

travis-ci

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

merge: from main to 3.0 branch

157521 of 317858 branches covered (49.56%)

Branch coverage included in aggregate %.

374 of 573 new or added lines in 31 files covered. (65.27%)

4949 existing lines in 87 files now uncovered.

242707 of 316936 relevant lines covered (76.58%)

18229906.31 hits per line

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

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

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

73
int32_t addTableExtSchema(SMetaEntry *pEntry, const SSchema *pColumn, int32_t newColNum, SExtSchema *pExtSchema) {
720✔
74
  // no need to add ext schema when no column needs ext schemas
75
  if (!HAS_TYPE_MOD(pColumn) && !pEntry->pExtSchemas) return 0;
720!
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) {
165✔
90
  // no ext schema, no need to drop
91
  if (!pEntry->pExtSchemas) return 0;
165✔
92
  if (dropColId == newColNum) {
65✔
93
    // drop the last column
94
    pEntry->pExtSchemas[dropColId - 1] = (SExtSchema){0};
25✔
95
  } else {
96
    // drop a column in the middle
97
    memmove(pEntry->pExtSchemas + dropColId, pEntry->pExtSchemas + dropColId + 1,
40✔
98
            (newColNum - dropColId) * sizeof(SExtSchema));
40✔
99
  }
100
  for (int32_t i = 0; i < newColNum; i++) {
250✔
101
    if (hasExtSchema(pEntry->pExtSchemas + i)) return 0;
240✔
102
  }
103
  taosMemoryFreeClear(pEntry->pExtSchemas);
10!
104
  return 0;
10✔
105
}
106

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

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

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

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

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

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

171
  return 0;
16,849✔
172
}
173

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

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

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

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

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

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

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

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

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

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

252
    SIndexTerm *term = NULL;
646✔
253
    if (type == TSDB_DATA_TYPE_NULL) {
646✔
254
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
24✔
255
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
622✔
256
      if (pTagVal->nData > 0) {
515✔
257
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
499!
258
        if (val == NULL) {
499!
UNCOV
259
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
260
        }
261
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
499✔
262
        if (len < 0) {
499!
UNCOV
263
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
264
        }
265
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
499✔
266
        type = TSDB_DATA_TYPE_VARCHAR;
499✔
267
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
499✔
268
        taosMemoryFree(val);
499!
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) {
646!
283
      int32_t ret = indexMultiTermAdd(terms, term);
646✔
284
      if (ret < 0) {
646!
UNCOV
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 {
UNCOV
289
      code = terrno;
×
290
      goto _exception;
×
291
    }
292
  }
293
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
437✔
294
  indexMultiTermDestroy(terms);
437✔
295

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

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

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

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

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

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

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

457
  code = tdbTbcMoveToFirst(pCur);
32✔
458
  if (code) {
32!
UNCOV
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!
UNCOV
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!
UNCOV
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!
UNCOV
507
    return terrno;
×
508
  }
509

510
  code = metaFilterTableByHash(pMeta, tbUids);
32✔
511
  if (code != 0) {
32!
UNCOV
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) {
64,787✔
529
  metaRLock(pMeta);
64,787✔
530

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

533
  metaULock(pMeta);
64,564✔
534

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

539
  return ret;
64,806✔
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 {
UNCOV
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 {
UNCOV
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!
UNCOV
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!
UNCOV
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!
UNCOV
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!
UNCOV
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!
UNCOV
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));
UNCOV
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!
UNCOV
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!
UNCOV
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 {
UNCOV
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!
UNCOV
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!
UNCOV
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!
UNCOV
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!
UNCOV
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!
UNCOV
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!
UNCOV
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!
UNCOV
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
    */
UNCOV
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

UNCOV
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
    }
UNCOV
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
    }
UNCOV
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
    }
UNCOV
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!
UNCOV
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!
UNCOV
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) {
5,487✔
800
  pMeta->changed = true;
5,487✔
801
  switch (pReq->action) {
5,487!
802
    case TSDB_ALTER_TABLE_ADD_COLUMN:
1,229✔
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);
1,229✔
806
    case TSDB_ALTER_TABLE_DROP_COLUMN:
176✔
807
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
176✔
808
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
745✔
809
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
745✔
810
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
46✔
811
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
46✔
812
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
3,167✔
813
      return metaUpdateTableTagValue(pMeta, version, pReq);
3,167✔
814
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
5✔
815
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
5✔
816
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
105✔
817
      return metaUpdateTableOptions2(pMeta, version, pReq);
105✔
818
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
15✔
819
      return metaUpdateTableColCompress2(pMeta, version, pReq);
15✔
UNCOV
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

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

UNCOV
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

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

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

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

UNCOV
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,
210,202✔
853
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
854
  if (IS_VAR_DATA_TYPE(type)) {
210,202✔
855
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
40,169✔
856
  } else {
857
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
170,033✔
858
  }
859

860
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
210,202!
861
  if (*ppTagIdxKey == NULL) {
210,285!
UNCOV
862
    return terrno;
×
863
  }
864

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

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

880
  return 0;
210,285✔
881
}
882

883
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
209,404✔
884
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
209,404!
885
}
209,415✔
886

887
static void colCompressDebug(SHashObj *pColCmprObj) {
1,257,980✔
888
  void *p = taosHashIterate(pColCmprObj, NULL);
1,257,980✔
889
  while (p) {
10,298,331✔
890
    uint32_t cmprAlg = *(uint32_t *)p;
9,040,511✔
891
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
9,040,511✔
892
    p = taosHashIterate(pColCmprObj, p);
9,040,367✔
893

894
    uint8_t l1, l2, lvl;
895
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
9,042,125✔
896

897
    const char *l1str = columnEncodeStr(l1);
9,041,685✔
898
    const char *l2str = columnCompressStr(l2);
9,040,953✔
899
    const char *lvlstr = columnLevelStr(lvl);
9,040,524✔
900
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
9,040,326✔
901
  }
902
  return;
1,257,820✔
903
}
904

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

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

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

919
  *ppColCmprObj = NULL;
1,257,973✔
920

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

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

970
  *ppColCmprObj = pColCmprObj;
1,258,012✔
971
  colCompressDebug(pColCmprObj);
1,258,012✔
972

973
  return 0;
1,257,918✔
974
}
975
// refactor later
976
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
55,343✔
977
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
55,270✔
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