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

taosdata / TDengine / #4835

31 Oct 2025 03:37AM UTC coverage: 58.506% (-0.3%) from 58.764%
#4835

push

travis-ci

SallyHuo-TAOS
Merge remote-tracking branch 'origin/cover/3.0' into cover/3.0

# Conflicts:
#	test/ci/run.sh

149104 of 324176 branches covered (45.99%)

Branch coverage included in aggregate %.

198232 of 269498 relevant lines covered (73.56%)

236558065.91 hits per line

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

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

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

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

89
int32_t dropTableExtSchema(SMetaEntry *pEntry, int32_t dropColId, int32_t newColNum) {
180,836✔
90
  // no ext schema, no need to drop
91
  if (!pEntry->pExtSchemas) return 0;
180,836✔
92
  if (dropColId == newColNum) {
4,830✔
93
    // drop the last column
94
    pEntry->pExtSchemas[dropColId - 1] = (SExtSchema){0};
1,610✔
95
  } else {
96
    // drop a column in the middle
97
    memmove(pEntry->pExtSchemas + dropColId, pEntry->pExtSchemas + dropColId + 1,
3,220!
98
            (newColNum - dropColId) * sizeof(SExtSchema));
3,220✔
99
  }
100
  for (int32_t i = 0; i < newColNum; i++) {
9,660!
101
    if (hasExtSchema(pEntry->pExtSchemas + i)) return 0;
9,660✔
102
  }
103
  taosMemoryFreeClear(pEntry->pExtSchemas);
×
104
  return 0;
×
105
}
106

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

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

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

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

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

169
  memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
19,856,964!
170

171
  return 0;
19,856,964✔
172
}
173

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

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

193
    pMetaRsp->numOfColumns = pSchema->nCols;
929,510✔
194
    pMetaRsp->sversion = pSchema->version;
929,510✔
195
    memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
929,510!
196
  }
197
  pMetaRsp->pColRefs = taosMemoryMalloc(pRef->nCols * sizeof(SColRef));
1,473,459!
198
  if (NULL == pMetaRsp->pColRefs) {
1,473,459!
199
    code = terrno;
×
200
    goto _return;
×
201
  }
202
  memcpy(pMetaRsp->pColRefs, pRef->pColRef, pRef->nCols * sizeof(SColRef));
1,473,459!
203
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
1,473,459!
204
  if (tableType == TSDB_VIRTUAL_NORMAL_TABLE) {
1,473,459✔
205
    pMetaRsp->tuid = pEntry->uid;
788,466✔
206
  } else if (tableType == TSDB_VIRTUAL_CHILD_TABLE) {
684,993!
207
    pMetaRsp->tuid = pEntry->uid;
684,993✔
208
    pMetaRsp->suid = pEntry->ctbEntry.suid;
684,993✔
209
  }
210

211
  pMetaRsp->tableType = tableType;
1,473,459✔
212
  pMetaRsp->virtualStb = false; // super table will never be processed here
1,473,459✔
213
  pMetaRsp->numOfColRefs = pRef->nCols;
1,473,459✔
214
  pMetaRsp->rversion = pRef->version;
1,473,459✔
215

216
  return code;
1,473,459✔
217
_return:
×
218
  taosMemoryFreeClear(pMetaRsp->pSchemaExt);
×
219
  taosMemoryFreeClear(pMetaRsp->pSchemas);
×
220
  taosMemoryFreeClear(pMetaRsp->pColRefs);
×
221
  return code;
×
222
}
223

224
int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
901,682✔
225
  int32_t code = 0;
901,682✔
226

227
#ifdef USE_INVERTED_INDEX
228
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
901,682!
229
    return TSDB_CODE_INVALID_PARA;
×
230
  }
231
  void       *data = pCtbEntry->ctbEntry.pTags;
901,682✔
232
  const char *tagName = pSchema->name;
901,682✔
233

234
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
901,682✔
235
  tb_uid_t    tuid = pCtbEntry->uid;
901,682✔
236
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
901,682✔
237
  int32_t     nTagData = 0;
901,682✔
238

239
  SArray *pTagVals = NULL;
901,682✔
240
  code = tTagToValArray((const STag *)data, &pTagVals);
901,682✔
241
  if (code) {
901,682!
242
    return code;
×
243
  }
244

245
  SIndexMultiTerm *terms = indexMultiTermCreate();
901,682✔
246
  if (terms == NULL) {
901,682!
247
    return terrno;
×
248
  }
249

250
  int16_t nCols = taosArrayGetSize(pTagVals);
901,682✔
251
  for (int i = 0; i < nCols; i++) {
2,152,237✔
252
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
1,250,555✔
253
    char     type = pTagVal->type;
1,250,555✔
254

255
    char   *key = pTagVal->pKey;
1,250,555✔
256
    int32_t nKey = strlen(key);
1,250,555!
257

258
    SIndexTerm *term = NULL;
1,250,555✔
259
    if (type == TSDB_DATA_TYPE_NULL) {
1,250,555✔
260
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
8,460✔
261
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
1,242,095✔
262
      if (pTagVal->nData > 0) {
1,190,715✔
263
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
1,185,075!
264
        if (val == NULL) {
1,185,075!
265
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
266
        }
267
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
1,185,075✔
268
        if (len < 0) {
1,185,075!
269
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
270
        }
271
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
1,185,075✔
272
        type = TSDB_DATA_TYPE_VARCHAR;
1,185,075✔
273
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
1,185,075✔
274
        taosMemoryFree(val);
1,185,075!
275
      } else if (pTagVal->nData == 0) {
5,640!
276
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
5,640✔
277
      }
278
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
51,380✔
279
      double val = *(double *)(&pTagVal->i64);
42,920✔
280
      int    len = sizeof(val);
42,920✔
281
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
42,920✔
282
    } else if (type == TSDB_DATA_TYPE_BOOL) {
8,460!
283
      int val = *(int *)(&pTagVal->i64);
8,460✔
284
      int len = sizeof(val);
8,460✔
285
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
8,460✔
286
    }
287

288
    if (term != NULL) {
1,250,555!
289
      int32_t ret = indexMultiTermAdd(terms, term);
1,250,555✔
290
      if (ret < 0) {
1,250,555!
291
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
292
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
293
      }
294
    } else {
295
      code = terrno;
×
296
      goto _exception;
×
297
    }
298
  }
299
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
901,682✔
300
  indexMultiTermDestroy(terms);
901,682✔
301

302
  taosArrayDestroy(pTagVals);
901,682✔
303
  return code;
901,682✔
304
_exception:
×
305
  indexMultiTermDestroy(terms);
×
306
  taosArrayDestroy(pTagVals);
×
307
#endif
308
  return code;
×
309
}
310
int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
288,388✔
311
int32_t code = 0;
288,388✔
312
#ifdef USE_INVERTED_INDEX
313
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
288,388!
314
    return TSDB_CODE_INVALID_PARA;
×
315
  }
316
  void       *data = pCtbEntry->ctbEntry.pTags;
288,388✔
317
  const char *tagName = pSchema->name;
288,388✔
318

319
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
288,388✔
320
  tb_uid_t    tuid = pCtbEntry->uid;
288,388✔
321
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
288,388✔
322
  int32_t     nTagData = 0;
288,388✔
323

324
  SArray *pTagVals = NULL;
288,388✔
325
  code = tTagToValArray((const STag *)data, &pTagVals);
288,388✔
326
  if (code) {
288,388!
327
    return code;
×
328
  }
329

330
  SIndexMultiTerm *terms = indexMultiTermCreate();
288,388✔
331
  if (terms == NULL) {
288,388!
332
    return terrno;
×
333
  }
334

335
  int16_t nCols = taosArrayGetSize(pTagVals);
288,388✔
336
  for (int i = 0; i < nCols; i++) {
826,427✔
337
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
538,039✔
338
    char     type = pTagVal->type;
538,039✔
339

340
    char   *key = pTagVal->pKey;
538,039✔
341
    int32_t nKey = strlen(key);
538,039!
342

343
    SIndexTerm *term = NULL;
538,039✔
344
    if (type == TSDB_DATA_TYPE_NULL) {
538,039!
345
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
×
346
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
538,039✔
347
      if (pTagVal->nData > 0) {
530,989!
348
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
530,989!
349
        if (val == NULL) {
530,989!
350
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
351
        }
352
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
530,989✔
353
        if (len < 0) {
530,989!
354
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
355
        }
356
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
530,989✔
357
        type = TSDB_DATA_TYPE_VARCHAR;
530,989✔
358
        term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, val, len);
530,989✔
359
        taosMemoryFree(val);
530,989!
360
      } else if (pTagVal->nData == 0) {
×
361
        term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
×
362
      }
363
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
7,050✔
364
      double val = *(double *)(&pTagVal->i64);
4,230✔
365
      int    len = sizeof(val);
4,230✔
366
      term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, (const char *)&val, len);
4,230✔
367
    } else if (type == TSDB_DATA_TYPE_BOOL) {
2,820!
368
      int val = *(int *)(&pTagVal->i64);
2,820✔
369
      int len = sizeof(val);
2,820✔
370
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
2,820✔
371
    }
372
    if (term != NULL) {
538,039!
373
      int32_t ret = indexMultiTermAdd(terms, term);
538,039✔
374
      if (ret < 0) {
538,039!
375
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
376
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
377
      }
378
    } else {
379
      code = terrno;
×
380
      goto _exception;
×
381
    }
382
  }
383
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
288,388✔
384
  indexMultiTermDestroy(terms);
288,388✔
385
  taosArrayDestroy(pTagVals);
288,388✔
386
  return code;
288,388✔
387
_exception:
×
388
  indexMultiTermDestroy(terms);
×
389
  taosArrayDestroy(pTagVals);
×
390
#endif
391
  return code;
×
392
}
393

394
static int32_t metaDropTables(SMeta *pMeta, SArray *tbUids) {
44,387✔
395
  int32_t code = 0;
44,387✔
396
  if (taosArrayGetSize(tbUids) == 0) return TSDB_CODE_SUCCESS;
44,387!
397

398
  int64_t    nCtbDropped = 0;
44,387✔
399
  SSHashObj *suidHash = tSimpleHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
44,387✔
400
  if (suidHash == NULL) {
44,387!
401
    return terrno;
×
402
  }
403

404
  metaWLock(pMeta);
44,387✔
405
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
270,094✔
406
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
225,707✔
407
    tb_uid_t suid = 0;
225,707✔
408
    int8_t   sysTbl = 0;
225,707✔
409
    int      type;
225,707✔
410
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
225,707✔
411
    if (code) return code;
225,707!
412
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
225,707!
413
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
217,526✔
414
      if (pVal) {
217,526✔
415
        nCtbDropped = *pVal + 1;
174,874✔
416
      } else {
417
        nCtbDropped = 1;
42,652✔
418
      }
419
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
217,526✔
420
      if (code) return code;
217,526!
421
    }
422
    /*
423
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
424
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
425
    }
426
    */
427
    metaDebug("batch drop table:%" PRId64, uid);
225,707!
428
  }
429
  metaULock(pMeta);
44,387✔
430

431
  // update timeseries
432
  void   *pCtbDropped = NULL;
44,387✔
433
  int32_t iter = 0;
44,387✔
434
  while ((pCtbDropped = tSimpleHashIterate(suidHash, pCtbDropped, &iter))) {
87,039✔
435
    tb_uid_t    *pSuid = tSimpleHashGetKey(pCtbDropped, NULL);
42,652✔
436
    int32_t      nCols = 0;
42,652✔
437
    int8_t       flags = 0;
42,652✔
438
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
42,652✔
439
    if (metaGetStbStats(pMeta->pVnode, *pSuid, NULL, &nCols, &flags) == 0) {
42,652!
440
      if (!TABLE_IS_VIRTUAL(flags)) {
42,652!
441
        pStats->numOfTimeSeries -= *(int64_t *)pCtbDropped * (nCols - 1);
42,652✔
442
      }
443
    }
444
  }
445
  tSimpleHashCleanup(suidHash);
44,387✔
446

447
  pMeta->changed = true;
44,387✔
448
  return 0;
44,387✔
449
}
450

451
static int32_t metaFilterTableByHash(SMeta *pMeta, SArray *uidList) {
56,317✔
452
  int32_t code = 0;
56,317✔
453
  // 1, tranverse table's
454
  // 2, validate table name using vnodeValidateTableHash
455
  // 3, push invalidated table's uid into uidList
456

457
  TBC *pCur;
56,317✔
458
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
56,317✔
459
  if (code < 0) {
56,317!
460
    return code;
×
461
  }
462

463
  code = tdbTbcMoveToFirst(pCur);
56,317✔
464
  if (code) {
56,317!
465
    tdbTbcClose(pCur);
×
466
    return code;
×
467
  }
468

469
  void *pData = NULL, *pKey = NULL;
56,317✔
470
  int   nData = 0, nKey = 0;
56,317✔
471

472
  while (1) {
499,158✔
473
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
555,475✔
474
    if (ret < 0) {
555,475✔
475
      break;
56,317✔
476
    }
477

478
    SMetaEntry me = {0};
499,158✔
479
    SDecoder   dc = {0};
499,158✔
480
    tDecoderInit(&dc, pData, nData);
499,158✔
481
    code = metaDecodeEntry(&dc, &me);
499,158✔
482
    if (code < 0) {
499,158!
483
      tDecoderClear(&dc);
×
484
      return code;
×
485
    }
486

487
    if (me.type != TSDB_SUPER_TABLE) {
499,158✔
488
      char tbFName[TSDB_TABLE_FNAME_LEN + 1];
451,404✔
489
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pMeta->pVnode->config.dbname, me.name);
451,404✔
490
      tbFName[TSDB_TABLE_FNAME_LEN] = '\0';
451,404✔
491
      if (pMeta->pVnode->mounted) tTrimMountPrefix(tbFName);
451,404!
492
      ret = vnodeValidateTableHash(pMeta->pVnode, tbFName);
451,404✔
493
      if (ret < 0 && terrno == TSDB_CODE_VND_HASH_MISMATCH) {
451,404!
494
        if (taosArrayPush(uidList, &me.uid) == NULL) {
225,707!
495
          code = terrno;
×
496
          break;
×
497
        }
498
      }
499
    }
500
    tDecoderClear(&dc);
499,158✔
501
  }
502
  tdbFree(pData);
56,317✔
503
  tdbFree(pKey);
56,317✔
504
  tdbTbcClose(pCur);
56,317✔
505

506
  return 0;
56,317✔
507
}
508

509
int32_t metaTrimTables(SMeta *pMeta, int64_t version) {
56,317✔
510
  int32_t code = 0;
56,317✔
511

512
  SArray *tbUids = taosArrayInit(8, sizeof(int64_t));
56,317✔
513
  if (tbUids == NULL) {
56,317!
514
    return terrno;
×
515
  }
516

517
  code = metaFilterTableByHash(pMeta, tbUids);
56,317✔
518
  if (code != 0) {
56,317!
519
    goto end;
×
520
  }
521
  if (TARRAY_SIZE(tbUids) == 0) {
56,317✔
522
    goto end;
11,930✔
523
  }
524

525
  metaInfo("vgId:%d, trim %ld tables", TD_VID(pMeta->pVnode), taosArrayGetSize(tbUids));
44,387!
526
  code = metaDropTables(pMeta, tbUids);
44,387✔
527
  if (code) goto end;
44,387!
528

529
end:
44,387✔
530
  taosArrayDestroy(tbUids);
56,317✔
531

532
  return code;
56,317✔
533
}
534

535
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
53,850,481✔
536
  metaRLock(pMeta);
53,850,481✔
537

538
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
53,897,530✔
539

540
  metaULock(pMeta);
53,797,070✔
541

542
  if (ret != 0) {
53,820,238!
543
    metaError("ttl failed to find expired table, ret:%d", ret);
×
544
  }
545

546
  return ret;
53,811,397✔
547
}
548

549
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
225,707✔
550
  int64_t btime;
551
  if (pME->type == TSDB_CHILD_TABLE) {
225,707✔
552
    btime = pME->ctbEntry.btime;
217,526✔
553
  } else if (pME->type == TSDB_NORMAL_TABLE) {
8,181!
554
    btime = pME->ntbEntry.btime;
8,181✔
555
  } else {
556
    return TSDB_CODE_FAILED;
×
557
  }
558

559
  btimeKey->btime = btime;
225,707✔
560
  btimeKey->uid = pME->uid;
225,707✔
561
  return 0;
225,707✔
562
}
563

564
static int metaBuildNColIdxKey(SNcolIdxKey *ncolKey, const SMetaEntry *pME) {
8,181✔
565
  if (pME->type == TSDB_NORMAL_TABLE) {
8,181!
566
    ncolKey->ncol = pME->ntbEntry.schemaRow.nCols;
8,181✔
567
    ncolKey->uid = pME->uid;
8,181✔
568
  } else {
569
    return TSDB_CODE_FAILED;
×
570
  }
571
  return 0;
8,181✔
572
}
573

574
static void metaDeleteTtl(SMeta *pMeta, const SMetaEntry *pME) {
225,707✔
575
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
225,707!
576

577
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
225,707✔
578
  if (pME->type == TSDB_CHILD_TABLE) {
225,707✔
579
    ctx.ttlDays = pME->ctbEntry.ttlDays;
217,526✔
580
  } else {
581
    ctx.ttlDays = pME->ntbEntry.ttlDays;
8,181✔
582
  }
583

584
  int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
225,707✔
585
  if (ret < 0) {
225,707!
586
    metaError("vgId:%d, failed to delete ttl for table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pME->name,
×
587
              pME->uid, tstrerror(ret));
588
  }
589
  return;
225,707✔
590
}
591

592
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl) {
225,707✔
593
  void      *pData = NULL;
225,707✔
594
  int        nData = 0;
225,707✔
595
  int        rc = 0;
225,707✔
596
  SMetaEntry e = {0};
225,707✔
597
  SDecoder   dc = {0};
225,707✔
598
  int32_t    ret = 0;
225,707✔
599

600
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
225,707✔
601
  if (rc < 0) {
225,707!
602
    return rc;
×
603
  }
604
  int64_t version = ((SUidIdxVal *)pData)[0].version;
225,707✔
605

606
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
225,707✔
607
  if (rc < 0) {
225,707!
608
    tdbFree(pData);
×
609
    return rc;
×
610
  }
611

612
  tDecoderInit(&dc, pData, nData);
225,707✔
613
  rc = metaDecodeEntry(&dc, &e);
225,707✔
614
  if (rc < 0) {
225,707!
615
    tDecoderClear(&dc);
×
616
    return rc;
×
617
  }
618

619
  if (type) *type = e.type;
225,707!
620

621
  if (e.type == TSDB_CHILD_TABLE) {
225,707✔
622
    if (pSuid) *pSuid = e.ctbEntry.suid;
217,526!
623
    void *tData = NULL;
217,526✔
624
    int   tLen = 0;
217,526✔
625

626
    if (tdbTbGet(pMeta->pUidIdx, &e.ctbEntry.suid, sizeof(tb_uid_t), &tData, &tLen) == 0) {
217,526!
627
      STbDbKey tbDbKey = {.uid = e.ctbEntry.suid, .version = ((SUidIdxVal *)tData)[0].version};
217,526✔
628
      if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &tData, &tLen) == 0) {
217,526!
629
        SDecoder   tdc = {0};
217,526✔
630
        SMetaEntry stbEntry = {0};
217,526✔
631

632
        tDecoderInit(&tdc, tData, tLen);
217,526✔
633
        ret = metaDecodeEntry(&tdc, &stbEntry);
217,526✔
634
        if (ret < 0) {
217,526!
635
          tDecoderClear(&tdc);
×
636
          metaError("vgId:%d, failed to decode child table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
637
                    e.ctbEntry.suid, tstrerror(ret));
638
          return ret;
×
639
        }
640

641
        if (pSysTbl) *pSysTbl = metaTbInFilterCache(pMeta, stbEntry.name, 1) ? 1 : 0;
217,526!
642

643
        SSchema        *pTagColumn = NULL;
217,526✔
644
        SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
217,526✔
645
        if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
217,526!
646
          pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
×
647
          ret = metaDelJsonVarFromIdx(pMeta, &e, pTagColumn);
×
648
          if (ret < 0) {
×
649
            metaError("vgId:%d, failed to delete json var from idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
650
                      e.name, e.uid, tstrerror(ret));
651
          }
652
        } else {
653
          for (int i = 0; i < pTagSchema->nCols; i++) {
1,152,760✔
654
            pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[i];
935,234✔
655
            if (!IS_IDX_ON(pTagColumn)) continue;
935,234✔
656
            STagIdxKey *pTagIdxKey = NULL;
217,526✔
657
            int32_t     nTagIdxKey;
217,526✔
658

659
            const void *pTagData = NULL;
217,526✔
660
            int32_t     nTagData = 0;
217,526✔
661

662
            STagVal tagVal = {.cid = pTagColumn->colId};
217,526✔
663
            if (tTagGet((const STag *)e.ctbEntry.pTags, &tagVal)) {
217,526!
664
              if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
217,526!
665
                pTagData = tagVal.pData;
×
666
                nTagData = (int32_t)tagVal.nData;
×
667
              } else {
668
                pTagData = &(tagVal.i64);
217,526✔
669
                nTagData = tDataTypes[pTagColumn->type].bytes;
217,526✔
670
              }
671
            } else {
672
              if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
×
673
                nTagData = tDataTypes[pTagColumn->type].bytes;
×
674
              }
675
            }
676

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

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

711
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
225,707!
712
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
225,707✔
713

714
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
225,707!
715

716
  if (e.type == TSDB_CHILD_TABLE) {
225,707✔
717
    ret =
718
        tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
217,526✔
719
    if (ret < 0) {
217,526!
720
      metaError("vgId:%d, failed to delete ctb idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
721
                tstrerror(ret));
722
    }
723

724
    --pMeta->pVnode->config.vndStats.numOfCTables;
217,526✔
725
    metaUpdateStbStats(pMeta, e.ctbEntry.suid, -1, 0, -1);
217,526✔
726
    ret = metaUidCacheClear(pMeta, e.ctbEntry.suid);
217,526✔
727
    if (ret < 0) {
217,526!
728
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
729
                e.ctbEntry.suid, tstrerror(ret));
730
    }
731
    ret = metaTbGroupCacheClear(pMeta, e.ctbEntry.suid);
217,526✔
732
    if (ret < 0) {
217,526!
733
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
734
                e.ctbEntry.suid, tstrerror(ret));
735
    }
736
    /*
737
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
738
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, e.ctbEntry.suid, NULL);
739
    }
740
    */
741
  } else if (e.type == TSDB_NORMAL_TABLE) {
8,181!
742
    // drop schema.db (todo)
743

744
    --pMeta->pVnode->config.vndStats.numOfNTables;
8,181✔
745
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
8,181✔
746

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

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

778
  ret = metaCacheDrop(pMeta, uid);
225,707✔
779
  if (ret < 0) {
225,707!
780
    metaError("vgId:%d, failed to drop cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
225,707!
781
              tstrerror(ret));
782
  }
783

784
  tDecoderClear(&dc);
225,707✔
785
  tdbFree(pData);
225,707✔
786

787
  return 0;
225,707✔
788
}
789

790
static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
225,707✔
791
  SBtimeIdxKey btimeKey = {0};
225,707✔
792
  if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) {
225,707!
793
    return 0;
×
794
  }
795
  return tdbTbDelete(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), pMeta->txn);
225,707✔
796
}
797

798
int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
8,181✔
799
  SNcolIdxKey ncolKey = {0};
8,181✔
800
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
8,181!
801
    return 0;
×
802
  }
803
  return tdbTbDelete(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), pMeta->txn);
8,181✔
804
}
805

806
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
20,312,088✔
807
  pMeta->changed = true;
20,312,088✔
808
  switch (pReq->action) {
20,312,088!
809
    case TSDB_ALTER_TABLE_ADD_COLUMN:
8,168,192✔
810
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
811
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF:
812
      return metaAddTableColumn(pMeta, version, pReq, pMetaRsp);
8,168,192✔
813
    case TSDB_ALTER_TABLE_DROP_COLUMN:
190,670✔
814
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
190,670✔
815
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
1,190,634✔
816
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
1,190,634✔
817
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
134,520✔
818
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
134,520✔
819
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
10,202,911✔
820
      return metaUpdateTableTagValue(pMeta, version, pReq);
10,202,911✔
821
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
7,695✔
822
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
7,695✔
823
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
76,566✔
824
      return metaUpdateTableOptions2(pMeta, version, pReq);
76,566✔
825
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
16,177✔
826
      return metaUpdateTableColCompress2(pMeta, version, pReq);
16,177✔
827
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
189,141✔
828
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
189,141✔
829
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
135,582✔
830
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
135,582✔
831
    default:
×
832
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
833
      break;
834
  }
835
}
836

837
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
3,021,053✔
838
  if (!tsTtlChangeOnWrite) return 0;
3,021,053!
839

840
  if (changeTimeMs <= 0) {
3,021,053!
841
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
×
842
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
×
843
  }
844

845
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
3,021,053✔
846

847
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
3,021,053✔
848
}
849

850
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
2,060,348,140✔
851
  if (!tsTtlChangeOnWrite) return 0;
2,060,348,140✔
852

853
  metaWLock(pMeta);
3,019,945✔
854
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
3,021,053✔
855
  metaULock(pMeta);
3,021,053✔
856
  return ret;
3,021,053✔
857
}
858

859
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
186,078,162✔
860
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
861
  if (IS_VAR_DATA_TYPE(type)) {
186,078,162✔
862
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
27,550,955✔
863
  } else {
864
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
158,527,207✔
865
  }
866

867
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
186,132,446✔
868
  if (*ppTagIdxKey == NULL) {
186,112,444!
869
    return terrno;
×
870
  }
871

872
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
186,097,522✔
873
  (*ppTagIdxKey)->cid = cid;
186,119,303✔
874
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
186,117,594✔
875
  (*ppTagIdxKey)->type = type;
186,061,088✔
876

877
  // refactor
878
  if (IS_VAR_DATA_TYPE(type)) {
186,094,956!
879
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
27,587,578✔
880
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
27,581,757!
881
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
27,583,794✔
882
  } else {
883
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
158,507,378!
884
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
158,527,232✔
885
  }
886

887
  return 0;
186,100,522✔
888
}
889

890
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
176,557,839✔
891
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
176,557,839!
892
}
176,561,987✔
893

894
static void colCompressDebug(SHashObj *pColCmprObj) {
236,588,423✔
895
  void *p = taosHashIterate(pColCmprObj, NULL);
236,588,423✔
896
  while (p) {
2,147,483,647✔
897
    uint32_t cmprAlg = *(uint32_t *)p;
2,147,483,647✔
898
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
2,147,483,647✔
899
    p = taosHashIterate(pColCmprObj, p);
2,147,483,647✔
900

901
    uint8_t l1, l2, lvl;
2,147,483,647✔
902
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
2,147,483,647✔
903

904
    const char *l1str = columnEncodeStr(l1);
2,147,483,647✔
905
    const char *l2str = columnCompressStr(l2);
2,147,483,647✔
906
    const char *lvlstr = columnLevelStr(lvl);
2,147,483,647✔
907
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
2,147,483,647✔
908
  }
909
  return;
236,708,807✔
910
}
911

912
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
236,624,432✔
913
  int rc = 0;
236,624,432✔
914

915
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
236,624,432✔
916
  if (pColCmprObj == NULL) {
236,650,477!
917
    pColCmprObj = NULL;
×
918
    return TSDB_CODE_OUT_OF_MEMORY;
×
919
  }
920

921
  void      *pData = NULL;
236,650,477✔
922
  int        nData = 0;
236,633,284✔
923
  SMetaEntry e = {0};
236,679,808✔
924
  SDecoder   dc = {0};
236,682,235✔
925

926
  *ppColCmprObj = NULL;
236,678,927✔
927

928
  metaRLock(pMeta);
236,686,671✔
929
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
236,696,337✔
930
  if (rc < 0) {
236,659,587!
931
    taosHashCleanup(pColCmprObj);
×
932
    metaULock(pMeta);
×
933
    return TSDB_CODE_FAILED;
×
934
  }
935
  int64_t version = ((SUidIdxVal *)pData)[0].version;
236,659,587✔
936
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
236,676,020✔
937
  if (rc < 0) {
236,647,392!
938
    metaULock(pMeta);
×
939
    taosHashCleanup(pColCmprObj);
×
940
    metaError("failed to get table entry");
×
941
    return rc;
×
942
  }
943

944
  tDecoderInit(&dc, pData, nData);
236,647,392✔
945
  rc = metaDecodeEntry(&dc, &e);
236,677,378✔
946
  if (rc < 0) {
236,560,842!
947
    tDecoderClear(&dc);
×
948
    tdbFree(pData);
×
949
    metaULock(pMeta);
×
950
    taosHashCleanup(pColCmprObj);
×
951
    return rc;
×
952
  }
953
  if (withExtSchema(e.type)) {
236,560,842!
954
    SColCmprWrapper *p = &e.colCmpr;
236,640,160✔
955
    for (int32_t i = 0; i < p->nCols; i++) {
2,147,483,647✔
956
      SColCmpr *pCmpr = &p->pColCmpr[i];
2,147,483,647✔
957
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
2,147,483,647✔
958
      if (rc < 0) {
2,147,483,647!
959
        tDecoderClear(&dc);
×
960
        tdbFree(pData);
×
961
        metaULock(pMeta);
×
962
        taosHashCleanup(pColCmprObj);
×
963
        return rc;
×
964
      }
965
    }
966
  } else {
967
    tDecoderClear(&dc);
×
968
    tdbFree(pData);
×
969
    metaULock(pMeta);
×
970
    taosHashCleanup(pColCmprObj);
×
971
    return 0;
×
972
  }
973
  tDecoderClear(&dc);
236,566,416✔
974
  tdbFree(pData);
236,653,860✔
975
  metaULock(pMeta);
236,622,047✔
976

977
  *ppColCmprObj = pColCmprObj;
236,628,778✔
978
  colCompressDebug(pColCmprObj);
236,627,267✔
979

980
  return 0;
236,710,467✔
981
}
982
// refactor later
983
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
12,503,889✔
984
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
12,529,615✔
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