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

taosdata / TDengine / #4909

30 Dec 2025 10:52AM UTC coverage: 65.542% (+0.2%) from 65.386%
#4909

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

857 existing lines in 113 files now uncovered.

193924 of 295877 relevant lines covered (65.54%)

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

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

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

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

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

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

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

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

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

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

171
  return 0;
6,917,910✔
172
}
173

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

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

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

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

210
  for (int32_t i = 0; i < pRef->nCols; i++) {
4,910,300✔
211
    SColRef* pColRef = &pRef->pColRef[i];
4,403,717✔
212
    if (pColRef->hasRef) {
4,403,717✔
213
      code = taosHashPut(pColRefHash, pColRef->refTableName, strlen(pColRef->refTableName), NULL, 0);
2,386,517✔
214
      if (code) {
2,386,517✔
215
        goto _return;
×
216
      }
217
    }
218
  }
219

220
  if (taosHashGetSize(pColRefHash) > 1000) {
506,583✔
UNCOV
221
    code = TSDB_CODE_VTABLE_TOO_MANY_REFERENCE;
×
UNCOV
222
    goto _return;
×
223
  }
224

225

226

227
  memcpy(pMetaRsp->pColRefs, pRef->pColRef, pRef->nCols * sizeof(SColRef));
506,583✔
228
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
506,583✔
229
  if (tableType == TSDB_VIRTUAL_NORMAL_TABLE) {
506,583✔
230
    pMetaRsp->tuid = pEntry->uid;
296,966✔
231
  } else if (tableType == TSDB_VIRTUAL_CHILD_TABLE) {
209,617✔
232
    pMetaRsp->tuid = pEntry->uid;
209,617✔
233
    pMetaRsp->suid = pEntry->ctbEntry.suid;
209,617✔
234
  }
235

236
  pMetaRsp->tableType = tableType;
506,583✔
237
  pMetaRsp->virtualStb = false; // super table will never be processed here
506,583✔
238
  pMetaRsp->numOfColRefs = pRef->nCols;
506,583✔
239
  pMetaRsp->rversion = pRef->version;
506,583✔
240

241
  taosHashCleanup(pColRefHash);
506,583✔
242
  return code;
506,583✔
UNCOV
243
_return:
×
UNCOV
244
  taosHashCleanup(pColRefHash);
×
UNCOV
245
  taosMemoryFreeClear(pMetaRsp->pSchemaExt);
×
UNCOV
246
  taosMemoryFreeClear(pMetaRsp->pSchemas);
×
UNCOV
247
  taosMemoryFreeClear(pMetaRsp->pColRefs);
×
UNCOV
248
  return code;
×
249
}
250

251
int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
241,501✔
252
  int32_t code = 0;
241,501✔
253

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

261
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
241,501✔
262
  tb_uid_t    tuid = pCtbEntry->uid;
241,501✔
263
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
241,501✔
264
  int32_t     nTagData = 0;
241,501✔
265

266
  SArray *pTagVals = NULL;
241,501✔
267
  code = tTagToValArray((const STag *)data, &pTagVals);
241,501✔
268
  if (code) {
241,501✔
269
    return code;
×
270
  }
271

272
  SIndexMultiTerm *terms = indexMultiTermCreate();
241,501✔
273
  if (terms == NULL) {
241,501✔
274
    return terrno;
×
275
  }
276

277
  int16_t nCols = taosArrayGetSize(pTagVals);
241,501✔
278
  for (int i = 0; i < nCols; i++) {
590,902✔
279
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
349,401✔
280
    char     type = pTagVal->type;
349,401✔
281

282
    char   *key = pTagVal->pKey;
349,401✔
283
    int32_t nKey = strlen(key);
349,401✔
284

285
    SIndexTerm *term = NULL;
349,401✔
286
    if (type == TSDB_DATA_TYPE_NULL) {
349,401✔
287
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
2,604✔
288
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
346,797✔
289
      if (pTagVal->nData > 0) {
329,918✔
290
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
328,182✔
291
        if (val == NULL) {
328,182✔
292
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
293
        }
294
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
328,182✔
295
        if (len < 0) {
328,182✔
296
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
297
        }
298
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
328,182✔
299
        type = TSDB_DATA_TYPE_VARCHAR;
328,182✔
300
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
328,182✔
301
        taosMemoryFree(val);
328,182✔
302
      } else if (pTagVal->nData == 0) {
1,736✔
303
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
1,736✔
304
      }
305
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
16,879✔
306
      double val = *(double *)(&pTagVal->i64);
13,407✔
307
      int    len = sizeof(val);
13,407✔
308
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
13,407✔
309
    } else if (type == TSDB_DATA_TYPE_BOOL) {
3,472✔
310
      int val = *(int *)(&pTagVal->i64);
3,472✔
311
      int len = sizeof(val);
3,472✔
312
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
3,472✔
313
    }
314

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

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

346
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
108,754✔
347
  tb_uid_t    tuid = pCtbEntry->uid;
108,754✔
348
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
108,754✔
349
  int32_t     nTagData = 0;
108,754✔
350

351
  SArray *pTagVals = NULL;
108,754✔
352
  code = tTagToValArray((const STag *)data, &pTagVals);
108,754✔
353
  if (code) {
108,754✔
354
    return code;
×
355
  }
356

357
  SIndexMultiTerm *terms = indexMultiTermCreate();
108,754✔
358
  if (terms == NULL) {
108,754✔
359
    return terrno;
×
360
  }
361

362
  int16_t nCols = taosArrayGetSize(pTagVals);
108,754✔
363
  for (int i = 0; i < nCols; i++) {
311,828✔
364
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
203,074✔
365
    char     type = pTagVal->type;
203,074✔
366

367
    char   *key = pTagVal->pKey;
203,074✔
368
    int32_t nKey = strlen(key);
203,074✔
369

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

421
static int32_t metaDropTables(SMeta *pMeta, SArray *tbUids) {
18,627✔
422
  int32_t code = 0;
18,627✔
423
  if (taosArrayGetSize(tbUids) == 0) return TSDB_CODE_SUCCESS;
18,627✔
424

425
  int64_t    nCtbDropped = 0;
18,627✔
426
  SSHashObj *suidHash = tSimpleHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
18,627✔
427
  if (suidHash == NULL) {
18,627✔
428
    return terrno;
×
429
  }
430

431
  metaWLock(pMeta);
18,627✔
432
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
96,269✔
433
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
77,642✔
434
    tb_uid_t suid = 0;
77,642✔
435
    int8_t   sysTbl = 0;
77,642✔
436
    int      type;
77,642✔
437
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
77,642✔
438
    if (code) return code;
77,642✔
439
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
77,642✔
440
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
72,793✔
441
      if (pVal) {
72,793✔
442
        nCtbDropped = *pVal + 1;
56,401✔
443
      } else {
444
        nCtbDropped = 1;
16,392✔
445
      }
446
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
72,793✔
447
      if (code) return code;
72,793✔
448
    }
449
    /*
450
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
451
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
452
    }
453
    */
454
    metaDebug("batch drop table:%" PRId64, uid);
77,642✔
455
  }
456
  metaULock(pMeta);
18,627✔
457

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

474
  pMeta->changed = true;
18,627✔
475
  return 0;
18,627✔
476
}
477

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

484
  TBC *pCur;
23,587✔
485
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
23,587✔
486
  if (code < 0) {
23,587✔
487
    return code;
×
488
  }
489

490
  code = tdbTbcMoveToFirst(pCur);
23,587✔
491
  if (code) {
23,587✔
492
    tdbTbcClose(pCur);
×
493
    return code;
×
494
  }
495

496
  void *pData = NULL, *pKey = NULL;
23,587✔
497
  int   nData = 0, nKey = 0;
23,587✔
498

499
  while (1) {
173,148✔
500
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
196,735✔
501
    if (ret < 0) {
196,735✔
502
      break;
23,587✔
503
    }
504

505
    SMetaEntry me = {0};
173,148✔
506
    SDecoder   dc = {0};
173,148✔
507
    tDecoderInit(&dc, pData, nData);
173,148✔
508
    code = metaDecodeEntry(&dc, &me);
173,148✔
509
    if (code < 0) {
173,148✔
510
      tDecoderClear(&dc);
×
511
      return code;
×
512
    }
513

514
    if (me.type != TSDB_SUPER_TABLE) {
173,148✔
515
      char tbFName[TSDB_TABLE_FNAME_LEN + 1];
155,284✔
516
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pMeta->pVnode->config.dbname, me.name);
155,284✔
517
      tbFName[TSDB_TABLE_FNAME_LEN] = '\0';
155,284✔
518
      if (pMeta->pVnode->mounted) tTrimMountPrefix(tbFName);
155,284✔
519
      ret = vnodeValidateTableHash(pMeta->pVnode, tbFName);
155,284✔
520
      if (ret < 0 && terrno == TSDB_CODE_VND_HASH_MISMATCH) {
155,284✔
521
        if (taosArrayPush(uidList, &me.uid) == NULL) {
77,642✔
522
          code = terrno;
×
523
          break;
×
524
        }
525
      }
526
    }
527
    tDecoderClear(&dc);
173,148✔
528
  }
529
  tdbFree(pData);
23,587✔
530
  tdbFree(pKey);
23,587✔
531
  tdbTbcClose(pCur);
23,587✔
532

533
  return 0;
23,587✔
534
}
535

536
int32_t metaTrimTables(SMeta *pMeta, int64_t version) {
23,587✔
537
  int32_t code = 0;
23,587✔
538

539
  SArray *tbUids = taosArrayInit(8, sizeof(int64_t));
23,587✔
540
  if (tbUids == NULL) {
23,587✔
541
    return terrno;
×
542
  }
543

544
  code = metaFilterTableByHash(pMeta, tbUids);
23,587✔
545
  if (code != 0) {
23,587✔
546
    goto end;
×
547
  }
548
  if (TARRAY_SIZE(tbUids) == 0) {
23,587✔
549
    goto end;
4,960✔
550
  }
551

552
  metaInfo("vgId:%d, trim %ld tables", TD_VID(pMeta->pVnode), taosArrayGetSize(tbUids));
18,627✔
553
  code = metaDropTables(pMeta, tbUids);
18,627✔
554
  if (code) goto end;
18,627✔
555

556
end:
18,627✔
557
  taosArrayDestroy(tbUids);
23,587✔
558

559
  return code;
23,587✔
560
}
561

562
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
12,873,058✔
563
  metaRLock(pMeta);
12,873,058✔
564

565
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
12,880,563✔
566

567
  metaULock(pMeta);
12,859,146✔
568

569
  if (ret != 0) {
12,865,337✔
570
    metaError("ttl failed to find expired table, ret:%d", ret);
×
571
  }
572

573
  return ret;
12,865,402✔
574
}
575

576
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
77,642✔
577
  int64_t btime;
578
  if (pME->type == TSDB_CHILD_TABLE) {
77,642✔
579
    btime = pME->ctbEntry.btime;
72,793✔
580
  } else if (pME->type == TSDB_NORMAL_TABLE) {
4,849✔
581
    btime = pME->ntbEntry.btime;
4,849✔
582
  } else {
583
    return TSDB_CODE_FAILED;
×
584
  }
585

586
  btimeKey->btime = btime;
77,642✔
587
  btimeKey->uid = pME->uid;
77,642✔
588
  return 0;
77,642✔
589
}
590

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

601
static void metaDeleteTtl(SMeta *pMeta, const SMetaEntry *pME) {
77,642✔
602
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
77,642✔
603

604
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
77,642✔
605
  if (pME->type == TSDB_CHILD_TABLE) {
77,642✔
606
    ctx.ttlDays = pME->ctbEntry.ttlDays;
72,793✔
607
  } else {
608
    ctx.ttlDays = pME->ntbEntry.ttlDays;
4,849✔
609
  }
610

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

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

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

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

639
  tDecoderInit(&dc, pData, nData);
77,642✔
640
  rc = metaDecodeEntry(&dc, &e);
77,642✔
641
  if (rc < 0) {
77,642✔
642
    tDecoderClear(&dc);
×
643
    return rc;
×
644
  }
645

646
  if (type) *type = e.type;
77,642✔
647

648
  if (e.type == TSDB_CHILD_TABLE) {
77,642✔
649
    if (pSuid) *pSuid = e.ctbEntry.suid;
72,793✔
650
    void *tData = NULL;
72,793✔
651
    int   tLen = 0;
72,793✔
652

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

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

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

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

694
            const void *pTagData = NULL;
72,793✔
695
            int32_t     nTagData = 0;
72,793✔
696

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

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

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

746
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
77,642✔
747
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
77,642✔
748

749
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
77,642✔
750

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

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

779
    --pMeta->pVnode->config.vndStats.numOfNTables;
4,849✔
780
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
4,849✔
781

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

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

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

824
  tDecoderClear(&dc);
77,642✔
825
  tdbFree(pData);
77,642✔
826

827
  return 0;
77,642✔
828
}
829

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

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

846
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
11,311,025✔
847
  pMeta->changed = true;
11,311,025✔
848
  switch (pReq->action) {
11,311,025✔
849
    case TSDB_ALTER_TABLE_ADD_COLUMN:
3,340,131✔
850
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
851
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF:
852
      return metaAddTableColumn(pMeta, version, pReq, pMetaRsp);
3,340,131✔
853
    case TSDB_ALTER_TABLE_DROP_COLUMN:
80,228✔
854
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
80,228✔
855
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
465,905✔
856
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
465,905✔
857
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
42,349✔
858
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
42,349✔
859
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
7,227,369✔
860
      return metaUpdateTableTagValue(pMeta, version, pReq);
7,227,369✔
861
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
3,120✔
862
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
3,120✔
863
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
23,638✔
864
      return metaUpdateTableOptions2(pMeta, version, pReq);
23,638✔
865
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
5,798✔
866
      return metaUpdateTableColCompress2(pMeta, version, pReq);
5,798✔
867
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
70,461✔
868
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
70,461✔
869
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
52,026✔
870
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
52,026✔
871
    default:
×
872
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
873
      break;
874
  }
875
}
876

877
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
1,012,017✔
878
  if (!tsTtlChangeOnWrite) return 0;
1,012,017✔
879

880
  if (changeTimeMs <= 0) {
1,012,017✔
881
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
×
882
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
×
883
  }
884

885
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
1,012,017✔
886

887
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
1,012,017✔
888
}
889

890
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
647,156,435✔
891
  if (!tsTtlChangeOnWrite) return 0;
647,156,435✔
892

893
  metaWLock(pMeta);
1,013,725✔
894
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
1,012,017✔
895
  metaULock(pMeta);
1,012,017✔
896
  return ret;
1,012,017✔
897
}
898

899
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
75,247,006✔
900
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
901
  if (IS_VAR_DATA_TYPE(type)) {
75,247,006✔
902
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
9,307,211✔
903
  } else {
904
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
65,939,795✔
905
  }
906

907
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
75,257,775✔
908
  if (*ppTagIdxKey == NULL) {
75,256,012✔
909
    return terrno;
×
910
  }
911

912
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
75,257,415✔
913
  (*ppTagIdxKey)->cid = cid;
75,255,724✔
914
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
75,247,914✔
915
  (*ppTagIdxKey)->type = type;
75,246,308✔
916

917
  // refactor
918
  if (IS_VAR_DATA_TYPE(type)) {
75,251,377✔
919
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
9,323,400✔
920
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
9,316,805✔
921
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
9,316,816✔
922
  } else {
923
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
65,927,977✔
924
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
65,933,231✔
925
  }
926

927
  return 0;
75,258,363✔
928
}
929

930
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
72,199,796✔
931
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
72,199,796✔
932
}
72,205,701✔
933

934
static void colCompressDebug(SHashObj *pColCmprObj) {
76,207,697✔
935
  void *p = taosHashIterate(pColCmprObj, NULL);
76,207,697✔
936
  while (p) {
648,838,194✔
937
    uint32_t cmprAlg = *(uint32_t *)p;
572,565,423✔
938
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
572,563,960✔
939
    p = taosHashIterate(pColCmprObj, p);
572,598,879✔
940

941
    uint8_t l1, l2, lvl;
572,752,309✔
942
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
572,765,845✔
943

944
    const char *l1str = columnEncodeStr(l1);
572,788,992✔
945
    const char *l2str = columnCompressStr(l2);
572,657,257✔
946
    const char *lvlstr = columnLevelStr(lvl);
572,630,172✔
947
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
572,615,753✔
948
  }
949
  return;
76,272,771✔
950
}
951

952
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
76,274,313✔
953
  int rc = 0;
76,274,313✔
954

955
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
76,274,313✔
956
  if (pColCmprObj == NULL) {
76,271,220✔
957
    pColCmprObj = NULL;
×
958
    return TSDB_CODE_OUT_OF_MEMORY;
×
959
  }
960

961
  void      *pData = NULL;
76,271,220✔
962
  int        nData = 0;
76,275,900✔
963
  SMetaEntry e = {0};
76,276,058✔
964
  SDecoder   dc = {0};
76,275,309✔
965

966
  *ppColCmprObj = NULL;
76,276,450✔
967

968
  metaRLock(pMeta);
76,269,923✔
969
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
76,283,755✔
970
  if (rc < 0) {
76,253,685✔
971
    taosHashCleanup(pColCmprObj);
14,375✔
972
    metaULock(pMeta);
14,375✔
973
    return TSDB_CODE_FAILED;
14,375✔
974
  }
975
  int64_t version = ((SUidIdxVal *)pData)[0].version;
76,239,310✔
976
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
76,231,244✔
977
  if (rc < 0) {
76,258,599✔
978
    metaULock(pMeta);
×
979
    taosHashCleanup(pColCmprObj);
×
980
    metaError("failed to get table entry");
×
981
    return rc;
×
982
  }
983

984
  tDecoderInit(&dc, pData, nData);
76,258,599✔
985
  rc = metaDecodeEntry(&dc, &e);
76,263,851✔
986
  if (rc < 0) {
76,203,782✔
987
    tDecoderClear(&dc);
×
988
    tdbFree(pData);
×
989
    metaULock(pMeta);
×
990
    taosHashCleanup(pColCmprObj);
×
991
    return rc;
×
992
  }
993
  if (withExtSchema(e.type)) {
76,203,782✔
994
    SColCmprWrapper *p = &e.colCmpr;
76,232,634✔
995
    for (int32_t i = 0; i < p->nCols; i++) {
648,970,546✔
996
      SColCmpr *pCmpr = &p->pColCmpr[i];
572,718,251✔
997
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
572,716,546✔
998
      if (rc < 0) {
572,737,912✔
999
        tDecoderClear(&dc);
×
1000
        tdbFree(pData);
×
1001
        metaULock(pMeta);
×
1002
        taosHashCleanup(pColCmprObj);
×
1003
        return rc;
×
1004
      }
1005
    }
1006
  } else {
1007
    tDecoderClear(&dc);
×
1008
    tdbFree(pData);
×
1009
    metaULock(pMeta);
×
1010
    taosHashCleanup(pColCmprObj);
×
1011
    return 0;
×
1012
  }
1013
  tDecoderClear(&dc);
76,215,379✔
1014
  tdbFree(pData);
76,265,608✔
1015
  metaULock(pMeta);
76,245,897✔
1016

1017
  *ppColCmprObj = pColCmprObj;
76,209,472✔
1018
  colCompressDebug(pColCmprObj);
76,220,196✔
1019

1020
  return 0;
76,272,409✔
1021
}
1022
// refactor later
1023
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
3,864,394✔
1024
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
3,864,394✔
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