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

taosdata / TDengine / #4943

30 Jan 2026 06:19AM UTC coverage: 66.718% (-0.07%) from 66.788%
#4943

push

travis-ci

web-flow
merge: from main to 3.0 #34453

1122 of 2018 new or added lines in 72 files covered. (55.6%)

823 existing lines in 156 files now uncovered.

204811 of 306978 relevant lines covered (66.72%)

123993567.34 hits per line

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

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

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

73
int32_t addTableExtSchema(SMetaEntry *pEntry, const SSchema *pColumn, int32_t newColNum, SExtSchema *pExtSchema) {
3,083,455✔
74
  // no need to add ext schema when no column needs ext schemas
75
  if (!HAS_TYPE_MOD(pColumn) && !pEntry->pExtSchemas) return 0;
3,083,455✔
76
  if (!pEntry->pExtSchemas) {
12,241✔
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));
2,163✔
80
  } else {
81
    // already has columns with ext schema
82
    pEntry->pExtSchemas = (SExtSchema *)taosMemoryRealloc(pEntry->pExtSchemas, sizeof(SExtSchema) * newColNum);
10,078✔
83
  }
84
  if (!pEntry->pExtSchemas) return terrno;
12,241✔
85
  pEntry->pExtSchemas[newColNum - 1] = *pExtSchema;
12,241✔
86
  return 0;
12,241✔
87
}
88

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

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

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

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

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

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

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

171
  return 0;
7,335,862✔
172
}
173

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

188
    pMetaRsp->pSchemaExt = taosMemoryMalloc(pSchema->nCols * sizeof(SSchemaExt));
373,439✔
189
    if (pMetaRsp->pSchemaExt == NULL) {
373,439✔
190
      code = terrno;
×
191
      goto _return;
×
192
    }
193

194
    pMetaRsp->numOfColumns = pSchema->nCols;
373,439✔
195
    pMetaRsp->sversion = pSchema->version;
373,439✔
196
    memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
373,439✔
197
  }
198
  pMetaRsp->pColRefs = taosMemoryMalloc(pRef->nCols * sizeof(SColRef));
564,200✔
199
  if (NULL == pMetaRsp->pColRefs) {
564,200✔
200
    code = terrno;
×
201
    goto _return;
×
202
  }
203

204
  pColRefHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
564,200✔
205
  if (!pColRefHash) {
564,200✔
206
    code = terrno;
×
207
    goto _return;
×
208
  }
209

210
  for (int32_t i = 0; i < pRef->nCols; i++) {
5,871,942✔
211
    SColRef* pColRef = &pRef->pColRef[i];
5,307,742✔
212
    if (pColRef->hasRef) {
5,307,742✔
213
      code = taosHashPut(pColRefHash, pColRef->refTableName, strlen(pColRef->refTableName), NULL, 0);
2,893,569✔
214
      if (code) {
2,893,569✔
215
        goto _return;
×
216
      }
217
    }
218
  }
219

220
  if (taosHashGetSize(pColRefHash) > 2000) {
564,200✔
UNCOV
221
    code = TSDB_CODE_VTABLE_TOO_MANY_REFERENCE;
×
UNCOV
222
    goto _return;
×
223
  }
224

225
  memcpy(pMetaRsp->pColRefs, pRef->pColRef, pRef->nCols * sizeof(SColRef));
564,200✔
226
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
564,200✔
227
  if (tableType == TSDB_VIRTUAL_NORMAL_TABLE) {
564,200✔
228
    pMetaRsp->tuid = pEntry->uid;
321,809✔
229
  } else if (tableType == TSDB_VIRTUAL_CHILD_TABLE) {
242,391✔
230
    pMetaRsp->tuid = pEntry->uid;
242,391✔
231
    pMetaRsp->suid = pEntry->ctbEntry.suid;
242,391✔
232
  }
233

234
  pMetaRsp->tableType = tableType;
564,200✔
235
  pMetaRsp->virtualStb = false; // super table will never be processed here
564,200✔
236
  pMetaRsp->numOfColRefs = pRef->nCols;
564,200✔
237
  pMetaRsp->rversion = pRef->version;
564,200✔
238

239
  taosHashCleanup(pColRefHash);
564,200✔
240
  return code;
564,200✔
UNCOV
241
_return:
×
UNCOV
242
  taosHashCleanup(pColRefHash);
×
UNCOV
243
  taosMemoryFreeClear(pMetaRsp->pSchemaExt);
×
UNCOV
244
  taosMemoryFreeClear(pMetaRsp->pSchemas);
×
UNCOV
245
  taosMemoryFreeClear(pMetaRsp->pColRefs);
×
UNCOV
246
  return code;
×
247
}
248

249
int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
249,307✔
250
  int32_t code = 0;
249,307✔
251

252
#ifdef USE_INVERTED_INDEX
253
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
249,307✔
254
    return TSDB_CODE_INVALID_PARA;
×
255
  }
256
  void       *data = pCtbEntry->ctbEntry.pTags;
249,307✔
257
  const char *tagName = pSchema->name;
249,307✔
258

259
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
249,307✔
260
  tb_uid_t    tuid = pCtbEntry->uid;
249,307✔
261
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
249,307✔
262
  int32_t     nTagData = 0;
249,307✔
263

264
  SArray *pTagVals = NULL;
249,307✔
265
  code = tTagToValArray((const STag *)data, &pTagVals);
249,307✔
266
  if (code) {
249,307✔
267
    return code;
×
268
  }
269

270
  SIndexMultiTerm *terms = indexMultiTermCreate();
249,307✔
271
  if (terms == NULL) {
249,307✔
272
    return terrno;
×
273
  }
274

275
  int16_t nCols = taosArrayGetSize(pTagVals);
249,307✔
276
  for (int i = 0; i < nCols; i++) {
608,552✔
277
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
359,245✔
278
    char     type = pTagVal->type;
359,245✔
279

280
    char   *key = pTagVal->pKey;
359,245✔
281
    int32_t nKey = strlen(key);
359,245✔
282

283
    SIndexTerm *term = NULL;
359,245✔
284
    if (type == TSDB_DATA_TYPE_NULL) {
359,245✔
285
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
2,658✔
286
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
356,587✔
287
      if (pTagVal->nData > 0) {
339,086✔
288
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
337,314✔
289
        if (val == NULL) {
337,314✔
290
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
291
        }
292
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
337,314✔
293
        if (len < 0) {
337,314✔
294
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
295
        }
296
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
337,314✔
297
        type = TSDB_DATA_TYPE_VARCHAR;
337,314✔
298
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
337,314✔
299
        taosMemoryFree(val);
337,314✔
300
      } else if (pTagVal->nData == 0) {
1,772✔
301
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
1,772✔
302
      }
303
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
17,501✔
304
      double val = *(double *)(&pTagVal->i64);
13,957✔
305
      int    len = sizeof(val);
13,957✔
306
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
13,957✔
307
    } else if (type == TSDB_DATA_TYPE_BOOL) {
3,544✔
308
      int val = *(int *)(&pTagVal->i64);
3,544✔
309
      int len = sizeof(val);
3,544✔
310
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
3,544✔
311
    }
312

313
    if (term != NULL) {
359,245✔
314
      int32_t ret = indexMultiTermAdd(terms, term);
359,245✔
315
      if (ret < 0) {
359,245✔
316
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
317
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
318
      }
319
    } else {
320
      code = terrno;
×
321
      goto _exception;
×
322
    }
323
  }
324
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
249,307✔
325
  indexMultiTermDestroy(terms);
249,307✔
326

327
  taosArrayDestroy(pTagVals);
249,307✔
328
  return code;
249,307✔
329
_exception:
×
330
  indexMultiTermDestroy(terms);
×
331
  taosArrayDestroy(pTagVals);
×
332
#endif
333
  return code;
×
334
}
335
int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
110,684✔
336
int32_t code = 0;
110,684✔
337
#ifdef USE_INVERTED_INDEX
338
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
110,684✔
339
    return TSDB_CODE_INVALID_PARA;
×
340
  }
341
  void       *data = pCtbEntry->ctbEntry.pTags;
110,684✔
342
  const char *tagName = pSchema->name;
110,684✔
343

344
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
110,684✔
345
  tb_uid_t    tuid = pCtbEntry->uid;
110,684✔
346
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
110,684✔
347
  int32_t     nTagData = 0;
110,684✔
348

349
  SArray *pTagVals = NULL;
110,684✔
350
  code = tTagToValArray((const STag *)data, &pTagVals);
110,684✔
351
  if (code) {
110,684✔
352
    return code;
×
353
  }
354

355
  SIndexMultiTerm *terms = indexMultiTermCreate();
110,684✔
356
  if (terms == NULL) {
110,684✔
357
    return terrno;
×
358
  }
359

360
  int16_t nCols = taosArrayGetSize(pTagVals);
110,684✔
361
  for (int i = 0; i < nCols; i++) {
317,354✔
362
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
206,670✔
363
    char     type = pTagVal->type;
206,670✔
364

365
    char   *key = pTagVal->pKey;
206,670✔
366
    int32_t nKey = strlen(key);
206,670✔
367

368
    SIndexTerm *term = NULL;
206,670✔
369
    if (type == TSDB_DATA_TYPE_NULL) {
206,670✔
370
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
×
371
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
206,670✔
372
      if (pTagVal->nData > 0) {
204,455✔
373
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
204,455✔
374
        if (val == NULL) {
204,455✔
375
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
376
        }
377
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
204,455✔
378
        if (len < 0) {
204,455✔
379
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
380
        }
381
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
204,455✔
382
        type = TSDB_DATA_TYPE_VARCHAR;
204,455✔
383
        term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, val, len);
204,455✔
384
        taosMemoryFree(val);
204,455✔
385
      } else if (pTagVal->nData == 0) {
×
386
        term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
×
387
      }
388
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
2,215✔
389
      double val = *(double *)(&pTagVal->i64);
1,329✔
390
      int    len = sizeof(val);
1,329✔
391
      term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, (const char *)&val, len);
1,329✔
392
    } else if (type == TSDB_DATA_TYPE_BOOL) {
886✔
393
      int val = *(int *)(&pTagVal->i64);
886✔
394
      int len = sizeof(val);
886✔
395
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
886✔
396
    }
397
    if (term != NULL) {
206,670✔
398
      int32_t ret = indexMultiTermAdd(terms, term);
206,670✔
399
      if (ret < 0) {
206,670✔
400
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
401
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
402
      }
403
    } else {
404
      code = terrno;
×
405
      goto _exception;
×
406
    }
407
  }
408
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
110,684✔
409
  indexMultiTermDestroy(terms);
110,684✔
410
  taosArrayDestroy(pTagVals);
110,684✔
411
  return code;
110,684✔
412
_exception:
×
413
  indexMultiTermDestroy(terms);
×
414
  taosArrayDestroy(pTagVals);
×
415
#endif
416
  return code;
×
417
}
418

419
static int32_t metaDropTables(SMeta *pMeta, SArray *tbUids) {
19,791✔
420
  int32_t code = 0;
19,791✔
421
  if (taosArrayGetSize(tbUids) == 0) return TSDB_CODE_SUCCESS;
19,791✔
422

423
  int64_t    nCtbDropped = 0;
19,791✔
424
  SSHashObj *suidHash = tSimpleHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
19,791✔
425
  if (suidHash == NULL) {
19,791✔
426
    return terrno;
×
427
  }
428

429
  metaWLock(pMeta);
19,791✔
430
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
100,319✔
431
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
80,528✔
432
    tb_uid_t suid = 0;
80,528✔
433
    int8_t   sysTbl = 0;
80,528✔
434
    int      type;
80,528✔
435
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
80,528✔
436
    if (code) return code;
80,528✔
437
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
80,528✔
438
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
74,893✔
439
      if (pVal) {
74,893✔
440
        nCtbDropped = *pVal + 1;
58,057✔
441
      } else {
442
        nCtbDropped = 1;
16,836✔
443
      }
444
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
74,893✔
445
      if (code) return code;
74,893✔
446
    }
447
    /*
448
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
449
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
450
    }
451
    */
452
    metaDebug("batch drop table:%" PRId64, uid);
80,528✔
453
  }
454
  metaULock(pMeta);
19,791✔
455

456
  // update timeseries
457
  void   *pCtbDropped = NULL;
19,791✔
458
  int32_t iter = 0;
19,791✔
459
  while ((pCtbDropped = tSimpleHashIterate(suidHash, pCtbDropped, &iter))) {
36,627✔
460
    tb_uid_t    *pSuid = tSimpleHashGetKey(pCtbDropped, NULL);
16,836✔
461
    int32_t      nCols = 0;
16,836✔
462
    int8_t       flags = 0;
16,836✔
463
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
16,836✔
464
    if (metaGetStbStats(pMeta->pVnode, *pSuid, NULL, &nCols, &flags) == 0) {
16,836✔
465
      if (!TABLE_IS_VIRTUAL(flags)) {
16,836✔
466
        pStats->numOfTimeSeries -= *(int64_t *)pCtbDropped * (nCols - 1);
16,836✔
467
      }
468
    }
469
  }
470
  tSimpleHashCleanup(suidHash);
19,791✔
471

472
  pMeta->changed = true;
19,791✔
473
  return 0;
19,791✔
474
}
475

476
static int32_t metaFilterTableByHash(SMeta *pMeta, SArray *uidList) {
24,228✔
477
  int32_t code = 0;
24,228✔
478
  // 1, tranverse table's
479
  // 2, validate table name using vnodeValidateTableHash
480
  // 3, push invalidated table's uid into uidList
481

482
  TBC *pCur;
24,228✔
483
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
24,228✔
484
  if (code < 0) {
24,228✔
485
    return code;
×
486
  }
487

488
  code = tdbTbcMoveToFirst(pCur);
24,228✔
489
  if (code) {
24,228✔
490
    tdbTbcClose(pCur);
×
491
    return code;
×
492
  }
493

494
  void *pData = NULL, *pKey = NULL;
24,228✔
495
  int   nData = 0, nKey = 0;
24,228✔
496

497
  while (1) {
179,406✔
498
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
203,634✔
499
    if (ret < 0) {
203,634✔
500
      break;
24,228✔
501
    }
502

503
    SMetaEntry me = {0};
179,406✔
504
    SDecoder   dc = {0};
179,406✔
505
    tDecoderInit(&dc, pData, nData);
179,406✔
506
    code = metaDecodeEntry(&dc, &me);
179,406✔
507
    if (code < 0) {
179,406✔
508
      tDecoderClear(&dc);
×
509
      return code;
×
510
    }
511

512
    if (me.type != TSDB_SUPER_TABLE) {
179,406✔
513
      char tbFName[TSDB_TABLE_FNAME_LEN + 1];
161,056✔
514
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pMeta->pVnode->config.dbname, me.name);
161,056✔
515
      tbFName[TSDB_TABLE_FNAME_LEN] = '\0';
161,056✔
516
      if (pMeta->pVnode->mounted) tTrimMountPrefix(tbFName);
161,056✔
517
      ret = vnodeValidateTableHash(pMeta->pVnode, tbFName);
161,056✔
518
      if (ret < 0 && terrno == TSDB_CODE_VND_HASH_MISMATCH) {
161,056✔
519
        if (taosArrayPush(uidList, &me.uid) == NULL) {
80,528✔
520
          code = terrno;
×
521
          break;
×
522
        }
523
      }
524
    }
525
    tDecoderClear(&dc);
179,406✔
526
  }
527
  tdbFree(pData);
24,228✔
528
  tdbFree(pKey);
24,228✔
529
  tdbTbcClose(pCur);
24,228✔
530

531
  return 0;
24,228✔
532
}
533

534
int32_t metaTrimTables(SMeta *pMeta, int64_t version) {
24,228✔
535
  int32_t code = 0;
24,228✔
536

537
  SArray *tbUids = taosArrayInit(8, sizeof(int64_t));
24,228✔
538
  if (tbUids == NULL) {
24,228✔
539
    return terrno;
×
540
  }
541

542
  code = metaFilterTableByHash(pMeta, tbUids);
24,228✔
543
  if (code != 0) {
24,228✔
544
    goto end;
×
545
  }
546
  if (TARRAY_SIZE(tbUids) == 0) {
24,228✔
547
    goto end;
4,437✔
548
  }
549

550
  metaInfo("vgId:%d, trim %ld tables", TD_VID(pMeta->pVnode), taosArrayGetSize(tbUids));
19,791✔
551
  code = metaDropTables(pMeta, tbUids);
19,791✔
552
  if (code) goto end;
19,791✔
553

554
end:
19,791✔
555
  taosArrayDestroy(tbUids);
24,228✔
556

557
  return code;
24,228✔
558
}
559

560
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
12,835,697✔
561
  metaRLock(pMeta);
12,835,697✔
562

563
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
12,846,389✔
564

565
  metaULock(pMeta);
12,829,849✔
566

567
  if (ret != 0) {
12,834,610✔
568
    metaError("ttl failed to find expired table, ret:%d", ret);
×
569
  }
570

571
  return ret;
12,834,682✔
572
}
573

574
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
80,528✔
575
  int64_t btime;
576
  if (pME->type == TSDB_CHILD_TABLE) {
80,528✔
577
    btime = pME->ctbEntry.btime;
74,893✔
578
  } else if (pME->type == TSDB_NORMAL_TABLE) {
5,635✔
579
    btime = pME->ntbEntry.btime;
5,635✔
580
  } else {
581
    return TSDB_CODE_FAILED;
×
582
  }
583

584
  btimeKey->btime = btime;
80,528✔
585
  btimeKey->uid = pME->uid;
80,528✔
586
  return 0;
80,528✔
587
}
588

589
static int metaBuildNColIdxKey(SNcolIdxKey *ncolKey, const SMetaEntry *pME) {
5,635✔
590
  if (pME->type == TSDB_NORMAL_TABLE) {
5,635✔
591
    ncolKey->ncol = pME->ntbEntry.schemaRow.nCols;
5,635✔
592
    ncolKey->uid = pME->uid;
5,635✔
593
  } else {
594
    return TSDB_CODE_FAILED;
×
595
  }
596
  return 0;
5,635✔
597
}
598

599
static void metaDeleteTtl(SMeta *pMeta, const SMetaEntry *pME) {
80,528✔
600
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
80,528✔
601

602
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
80,528✔
603
  if (pME->type == TSDB_CHILD_TABLE) {
80,528✔
604
    ctx.ttlDays = pME->ctbEntry.ttlDays;
74,893✔
605
  } else {
606
    ctx.ttlDays = pME->ntbEntry.ttlDays;
5,635✔
607
  }
608

609
  int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
80,528✔
610
  if (ret < 0) {
80,528✔
611
    metaError("vgId:%d, failed to delete ttl for table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pME->name,
×
612
              pME->uid, tstrerror(ret));
613
  }
614
  return;
80,528✔
615
}
616

617
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl) {
80,528✔
618
  void      *pData = NULL;
80,528✔
619
  int        nData = 0;
80,528✔
620
  int        rc = 0;
80,528✔
621
  SMetaEntry e = {0};
80,528✔
622
  SDecoder   dc = {0};
80,528✔
623
  int32_t    ret = 0;
80,528✔
624

625
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
80,528✔
626
  if (rc < 0) {
80,528✔
627
    return rc;
×
628
  }
629
  int64_t version = ((SUidIdxVal *)pData)[0].version;
80,528✔
630

631
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
80,528✔
632
  if (rc < 0) {
80,528✔
633
    tdbFree(pData);
×
634
    return rc;
×
635
  }
636

637
  tDecoderInit(&dc, pData, nData);
80,528✔
638
  rc = metaDecodeEntry(&dc, &e);
80,528✔
639
  if (rc < 0) {
80,528✔
640
    tDecoderClear(&dc);
×
641
    return rc;
×
642
  }
643

644
  if (type) *type = e.type;
80,528✔
645

646
  if (e.type == TSDB_CHILD_TABLE) {
80,528✔
647
    if (pSuid) *pSuid = e.ctbEntry.suid;
74,893✔
648
    void *tData = NULL;
74,893✔
649
    int   tLen = 0;
74,893✔
650

651
    if (tdbTbGet(pMeta->pUidIdx, &e.ctbEntry.suid, sizeof(tb_uid_t), &tData, &tLen) == 0) {
74,893✔
652
      STbDbKey tbDbKey = {.uid = e.ctbEntry.suid, .version = ((SUidIdxVal *)tData)[0].version};
74,893✔
653
      if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &tData, &tLen) == 0) {
74,893✔
654
        SDecoder   tdc = {0};
74,893✔
655
        SMetaEntry stbEntry = {0};
74,893✔
656

657
        tDecoderInit(&tdc, tData, tLen);
74,893✔
658
        ret = metaDecodeEntry(&tdc, &stbEntry);
74,893✔
659
        if (ret < 0) {
74,893✔
660
          tDecoderClear(&tdc);
×
661
          metaError("vgId:%d, failed to decode child table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
662
                    e.ctbEntry.suid, tstrerror(ret));
663
          return ret;
×
664
        }
665

666
        if (pSysTbl) *pSysTbl = metaTbInFilterCache(pMeta, stbEntry.name, 1) ? 1 : 0;
74,893✔
667
        
668
        ret = metaStableTagFilterCacheUpdateUid(
74,893✔
669
          pMeta, &e, &stbEntry, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
670
        if (ret < 0) {
74,893✔
671
          metaError("vgId:%d, failed to update stable tag filter cache:%s "
×
672
            "uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
673
            e.ctbEntry.suid, tstrerror(ret));
674
        }
675

676
        SSchema        *pTagColumn = NULL;
74,893✔
677
        SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
74,893✔
678
        if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
74,893✔
679
          pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
×
680
          ret = metaDelJsonVarFromIdx(pMeta, &e, pTagColumn);
×
681
          if (ret < 0) {
×
682
            metaError("vgId:%d, failed to delete json var from idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
683
                      e.name, e.uid, tstrerror(ret));
684
          }
685
        } else {
686
          for (int i = 0; i < pTagSchema->nCols; i++) {
386,294✔
687
            pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[i];
311,401✔
688
            if (!IS_IDX_ON(pTagColumn)) continue;
311,401✔
689
            STagIdxKey *pTagIdxKey = NULL;
74,893✔
690
            int32_t     nTagIdxKey;
74,893✔
691

692
            const void *pTagData = NULL;
74,893✔
693
            int32_t     nTagData = 0;
74,893✔
694

695
            STagVal tagVal = {.cid = pTagColumn->colId};
74,893✔
696
            if (tTagGet((const STag *)e.ctbEntry.pTags, &tagVal)) {
74,893✔
697
              if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
74,893✔
698
                pTagData = tagVal.pData;
×
699
                nTagData = (int32_t)tagVal.nData;
×
700
              } else {
701
                pTagData = &(tagVal.i64);
74,893✔
702
                nTagData = tDataTypes[pTagColumn->type].bytes;
74,893✔
703
              }
704
            } else {
705
              if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
×
706
                nTagData = tDataTypes[pTagColumn->type].bytes;
×
707
              }
708
            }
709

710
            if (metaCreateTagIdxKey(e.ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type, uid,
74,893✔
711
                                    &pTagIdxKey, &nTagIdxKey) == 0) {
712
              ret = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
74,893✔
713
              if (ret < 0) {
74,893✔
714
                metaError("vgId:%d, failed to delete tag idx key:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
715
                          e.name, e.uid, tstrerror(ret));
716
              }
717
            }
718
            metaDestroyTagIdxKey(pTagIdxKey);
74,893✔
719
            pTagIdxKey = NULL;
74,893✔
720
          }
721
        }
722
        tDecoderClear(&tdc);
74,893✔
723
      }
724
      tdbFree(tData);
74,893✔
725
    }
726
  }
727

728
  ret = tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), pMeta->txn);
80,528✔
729
  if (ret < 0) {
80,528✔
730
    metaError("vgId:%d, failed to delete table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
731
              tstrerror(ret));
732
  }
733
  ret = tdbTbDelete(pMeta->pNameIdx, e.name, strlen(e.name) + 1, pMeta->txn);
80,528✔
734
  if (ret < 0) {
80,528✔
735
    metaError("vgId:%d, failed to delete name idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
736
              tstrerror(ret));
737
  }
738
  ret = tdbTbDelete(pMeta->pUidIdx, &uid, sizeof(uid), pMeta->txn);
80,528✔
739
  if (ret < 0) {
80,528✔
740
    metaError("vgId:%d, failed to delete uid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
741
              tstrerror(ret));
742
  }
743

744
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
80,528✔
745
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
80,528✔
746

747
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
80,528✔
748

749
  if (e.type == TSDB_CHILD_TABLE) {
80,528✔
750
    ret =
751
        tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
74,893✔
752
    if (ret < 0) {
74,893✔
753
      metaError("vgId:%d, failed to delete ctb idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
754
                tstrerror(ret));
755
    }
756

757
    --pMeta->pVnode->config.vndStats.numOfCTables;
74,893✔
758
    metaUpdateStbStats(pMeta, e.ctbEntry.suid, -1, 0, -1);
74,893✔
759
    ret = metaUidCacheClear(pMeta, e.ctbEntry.suid);
74,893✔
760
    if (ret < 0) {
74,893✔
761
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
762
                e.ctbEntry.suid, tstrerror(ret));
763
    }
764
    ret = metaTbGroupCacheClear(pMeta, e.ctbEntry.suid);
74,893✔
765
    if (ret < 0) {
74,893✔
766
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
767
                e.ctbEntry.suid, tstrerror(ret));
768
    }
769
    /*
770
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
771
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, e.ctbEntry.suid, NULL);
772
    }
773
    */
774
  } else if (e.type == TSDB_NORMAL_TABLE) {
5,635✔
775
    // drop schema.db (todo)
776

777
    --pMeta->pVnode->config.vndStats.numOfNTables;
5,635✔
778
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
5,635✔
779

780
    /*
781
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
782
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, -1, &e.ntbEntry.schemaRow);
783
    }
784
    */
785
  } else if (e.type == TSDB_SUPER_TABLE) {
×
786
    ret = tdbTbDelete(pMeta->pSuidIdx, &e.uid, sizeof(tb_uid_t), pMeta->txn);
×
787
    if (ret < 0) {
×
788
      metaError("vgId:%d, failed to delete suid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
789
                tstrerror(ret));
790
    }
791
    // drop schema.db (todo)
792

793
    ret = metaStatsCacheDrop(pMeta, uid);
×
794
    if (ret < 0) {
×
795
      metaError("vgId:%d, failed to drop stats cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
796
                tstrerror(ret));
797
    }
798
    ret = metaUidCacheClear(pMeta, uid);
×
799
    if (ret < 0) {
×
800
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
801
                tstrerror(ret));
802
    }
803
    ret = metaStableTagFilterCacheDropSTable(pMeta, uid);
×
804
    if (ret < 0) {
×
805
      metaError("vgId:%d, failed to clear stable tag filter cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
806
                e.uid, tstrerror(ret));
807
    }
808
    ret = metaTbGroupCacheClear(pMeta, uid);
×
809
    if (ret < 0) {
×
810
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
811
                e.uid, tstrerror(ret));
812
    }
813
    --pMeta->pVnode->config.vndStats.numOfSTables;
×
814
  }
815

816
  ret = metaCacheDrop(pMeta, uid);
80,528✔
817
  if (ret < 0) {
80,528✔
818
    metaError("vgId:%d, failed to drop cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
80,528✔
819
              tstrerror(ret));
820
  }
821

822
  tDecoderClear(&dc);
80,528✔
823
  tdbFree(pData);
80,528✔
824

825
  return 0;
80,528✔
826
}
827

828
static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
80,528✔
829
  SBtimeIdxKey btimeKey = {0};
80,528✔
830
  if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) {
80,528✔
831
    return 0;
×
832
  }
833
  return tdbTbDelete(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), pMeta->txn);
80,528✔
834
}
835

836
int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
5,635✔
837
  SNcolIdxKey ncolKey = {0};
5,635✔
838
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
5,635✔
839
    return 0;
×
840
  }
841
  return tdbTbDelete(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), pMeta->txn);
5,635✔
842
}
843

844
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
11,630,673✔
845
  pMeta->changed = true;
11,630,673✔
846
  switch (pReq->action) {
11,630,673✔
847
    case TSDB_ALTER_TABLE_ADD_COLUMN:
3,398,070✔
848
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
849
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF:
850
      return metaAddTableColumn(pMeta, version, pReq, pMetaRsp);
3,398,070✔
851
    case TSDB_ALTER_TABLE_DROP_COLUMN:
80,354✔
852
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
80,354✔
853
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
476,078✔
854
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
476,078✔
855
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
43,115✔
856
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
43,115✔
857
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
7,476,945✔
858
      return metaUpdateTableTagValue(pMeta, version, pReq);
7,476,945✔
859
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
3,180✔
860
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
3,180✔
861
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
24,174✔
862
      return metaUpdateTableOptions2(pMeta, version, pReq);
24,174✔
863
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
5,897✔
864
      return metaUpdateTableColCompress2(pMeta, version, pReq);
5,897✔
865
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
70,802✔
866
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
70,802✔
867
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
52,058✔
868
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
52,058✔
869
    default:
×
870
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
871
      break;
872
  }
873
}
874

875
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
20,679,737✔
876
  if (!tsTtlChangeOnWrite) return 0;
20,679,737✔
877

878
  if (changeTimeMs <= 0) {
20,679,737✔
879
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
×
880
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
×
881
  }
882

883
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
20,679,737✔
884

885
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
20,679,737✔
886
}
887

888
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
725,872,389✔
889
  if (!tsTtlChangeOnWrite) return 0;
725,872,389✔
890

891
  metaWLock(pMeta);
20,681,545✔
892
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
20,679,737✔
893
  metaULock(pMeta);
20,679,737✔
894
  return ret;
20,679,737✔
895
}
896

897
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
82,153,518✔
898
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
899
  if (IS_VAR_DATA_TYPE(type)) {
82,153,518✔
900
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
9,469,578✔
901
  } else {
902
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
72,683,940✔
903
  }
904

905
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
82,176,743✔
906
  if (*ppTagIdxKey == NULL) {
82,166,726✔
907
    return terrno;
×
908
  }
909

910
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
82,152,310✔
911
  (*ppTagIdxKey)->cid = cid;
82,160,363✔
912
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
82,175,194✔
913
  (*ppTagIdxKey)->type = type;
82,167,786✔
914

915
  // refactor
916
  if (IS_VAR_DATA_TYPE(type)) {
82,163,613✔
917
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
9,512,146✔
918
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
9,494,370✔
919
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
9,497,147✔
920
  } else {
921
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
72,651,467✔
922
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
72,677,506✔
923
  }
924

925
  return 0;
82,154,296✔
926
}
927

928
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
79,094,139✔
929
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
79,094,139✔
930
}
79,094,928✔
931

932
static void colCompressDebug(SHashObj *pColCmprObj) {
86,329,356✔
933
  void *p = taosHashIterate(pColCmprObj, NULL);
86,329,356✔
934
  while (p) {
710,381,055✔
935
    uint32_t cmprAlg = *(uint32_t *)p;
623,960,538✔
936
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
623,956,590✔
937
    p = taosHashIterate(pColCmprObj, p);
624,022,615✔
938

939
    uint8_t l1, l2, lvl;
624,368,322✔
940
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
624,326,295✔
941

942
    const char *l1str = columnEncodeStr(l1);
624,362,639✔
943
    const char *l2str = columnCompressStr(l2);
624,161,761✔
944
    const char *lvlstr = columnLevelStr(lvl);
624,070,328✔
945
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
623,995,734✔
946
  }
947
  return;
86,420,517✔
948
}
949

950
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
86,412,471✔
951
  int rc = 0;
86,412,471✔
952

953
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
86,412,471✔
954
  if (pColCmprObj == NULL) {
86,403,586✔
955
    pColCmprObj = NULL;
×
956
    return TSDB_CODE_OUT_OF_MEMORY;
×
957
  }
958

959
  void      *pData = NULL;
86,403,586✔
960
  int        nData = 0;
86,399,816✔
961
  SMetaEntry e = {0};
86,421,641✔
962
  SDecoder   dc = {0};
86,422,554✔
963

964
  *ppColCmprObj = NULL;
86,421,292✔
965

966
  metaRLock(pMeta);
86,423,743✔
967
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
86,432,364✔
968
  if (rc < 0) {
86,397,669✔
969
    taosHashCleanup(pColCmprObj);
14,725✔
970
    metaULock(pMeta);
14,725✔
971
    return TSDB_CODE_FAILED;
14,725✔
972
  }
973
  int64_t version = ((SUidIdxVal *)pData)[0].version;
86,382,944✔
974
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
86,392,398✔
975
  if (rc < 0) {
86,390,217✔
976
    metaULock(pMeta);
×
977
    taosHashCleanup(pColCmprObj);
×
978
    metaError("failed to get table entry");
×
979
    return rc;
×
980
  }
981

982
  tDecoderInit(&dc, pData, nData);
86,390,217✔
983
  rc = metaDecodeEntry(&dc, &e);
86,415,599✔
984
  if (rc < 0) {
86,309,291✔
985
    tDecoderClear(&dc);
×
986
    tdbFree(pData);
×
987
    metaULock(pMeta);
×
988
    taosHashCleanup(pColCmprObj);
×
989
    return rc;
×
990
  }
991
  if (withExtSchema(e.type)) {
86,309,291✔
992
    SColCmprWrapper *p = &e.colCmpr;
86,390,878✔
993
    for (int32_t i = 0; i < p->nCols; i++) {
710,757,886✔
994
      SColCmpr *pCmpr = &p->pColCmpr[i];
624,352,228✔
995
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
624,370,155✔
996
      if (rc < 0) {
624,367,008✔
997
        tDecoderClear(&dc);
×
998
        tdbFree(pData);
×
999
        metaULock(pMeta);
×
1000
        taosHashCleanup(pColCmprObj);
×
1001
        return rc;
×
1002
      }
1003
    }
1004
  } else {
1005
    tDecoderClear(&dc);
×
1006
    tdbFree(pData);
×
1007
    metaULock(pMeta);
×
1008
    taosHashCleanup(pColCmprObj);
×
1009
    return 0;
×
1010
  }
1011
  tDecoderClear(&dc);
86,349,041✔
1012
  tdbFree(pData);
86,360,559✔
1013
  metaULock(pMeta);
86,391,766✔
1014

1015
  *ppColCmprObj = pColCmprObj;
86,360,841✔
1016
  colCompressDebug(pColCmprObj);
86,366,872✔
1017

1018
  return 0;
86,419,233✔
1019
}
1020
// refactor later
1021
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
3,907,471✔
1022
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
3,907,471✔
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