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

taosdata / TDengine / #4986

15 Mar 2026 08:32AM UTC coverage: 37.305% (-31.3%) from 68.601%
#4986

push

travis-ci

tomchon
test: keep docs and unit test

125478 of 336361 relevant lines covered (37.3%)

1134847.06 hits per line

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

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

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

73
int32_t addTableExtSchema(SMetaEntry *pEntry, const SSchema *pColumn, int32_t newColNum, SExtSchema *pExtSchema) {
×
74
  // no need to add ext schema when no column needs ext schemas
75
  if (!HAS_TYPE_MOD(pColumn) && !pEntry->pExtSchemas) return 0;
×
76
  if (!pEntry->pExtSchemas) {
×
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);
×
83
  }
84
  if (!pEntry->pExtSchemas) return terrno;
×
85
  pEntry->pExtSchemas[newColNum - 1] = *pExtSchema;
×
86
  return 0;
×
87
}
88

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

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

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

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

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

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

170
  memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
×
171

172
  return 0;
×
173
}
174

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

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

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

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

211
  for (int32_t i = 0; i < pRef->nCols; i++) {
×
212
    SColRef* pColRef = &pRef->pColRef[i];
×
213
    if (pColRef->hasRef) {
×
214
      code = taosHashPut(pColRefHash, pColRef->refTableName, strlen(pColRef->refTableName), NULL, 0);
×
215
      if (code) {
×
216
        goto _return;
×
217
      }
218
    }
219
  }
220

221
  memcpy(pMetaRsp->pColRefs, pRef->pColRef, pRef->nCols * sizeof(SColRef));
×
222
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
×
223
  if (tableType == TSDB_VIRTUAL_NORMAL_TABLE) {
×
224
    pMetaRsp->tuid = pEntry->uid;
×
225
  } else if (tableType == TSDB_VIRTUAL_CHILD_TABLE) {
×
226
    pMetaRsp->tuid = pEntry->uid;
×
227
    pMetaRsp->suid = pEntry->ctbEntry.suid;
×
228
  }
229

230
  pMetaRsp->tableType = tableType;
×
231
  pMetaRsp->virtualStb = false; // super table will never be processed here
×
232
  pMetaRsp->numOfColRefs = pRef->nCols;
×
233
  pMetaRsp->rversion = pRef->version;
×
234
  if (ownerId != 0) pMetaRsp->ownerId = ownerId;
×
235

236
  taosHashCleanup(pColRefHash);
×
237
  return code;
×
238
_return:
×
239
  taosHashCleanup(pColRefHash);
×
240
  taosMemoryFreeClear(pMetaRsp->pSchemaExt);
×
241
  taosMemoryFreeClear(pMetaRsp->pSchemas);
×
242
  taosMemoryFreeClear(pMetaRsp->pColRefs);
×
243
  return code;
×
244
}
245

246
int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
×
247
  int32_t code = 0;
×
248

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

256
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
×
257
  tb_uid_t    tuid = pCtbEntry->uid;
×
258
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
×
259
  int32_t     nTagData = 0;
×
260

261
  SArray *pTagVals = NULL;
×
262
  code = tTagToValArray((const STag *)data, &pTagVals);
×
263
  if (code) {
×
264
    return code;
×
265
  }
266

267
  SIndexMultiTerm *terms = indexMultiTermCreate();
×
268
  if (terms == NULL) {
×
269
    return terrno;
×
270
  }
271

272
  int16_t nCols = taosArrayGetSize(pTagVals);
×
273
  for (int i = 0; i < nCols; i++) {
×
274
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
×
275
    char     type = pTagVal->type;
×
276

277
    char   *key = pTagVal->pKey;
×
278
    int32_t nKey = strlen(key);
×
279

280
    SIndexTerm *term = NULL;
×
281
    if (type == TSDB_DATA_TYPE_NULL) {
×
282
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
×
283
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
×
284
      if (pTagVal->nData > 0) {
×
285
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
×
286
        if (val == NULL) {
×
287
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
288
        }
289
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
×
290
        if (len < 0) {
×
291
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
292
        }
293
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
×
294
        type = TSDB_DATA_TYPE_VARCHAR;
×
295
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
×
296
        taosMemoryFree(val);
×
297
      } else if (pTagVal->nData == 0) {
×
298
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
×
299
      }
300
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
×
301
      double val = *(double *)(&pTagVal->i64);
×
302
      int    len = sizeof(val);
×
303
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
×
304
    } else if (type == TSDB_DATA_TYPE_BOOL) {
×
305
      int val = *(int *)(&pTagVal->i64);
×
306
      int len = sizeof(val);
×
307
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
×
308
    }
309

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

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

341
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
×
342
  tb_uid_t    tuid = pCtbEntry->uid;
×
343
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
×
344
  int32_t     nTagData = 0;
×
345

346
  SArray *pTagVals = NULL;
×
347
  code = tTagToValArray((const STag *)data, &pTagVals);
×
348
  if (code) {
×
349
    return code;
×
350
  }
351

352
  SIndexMultiTerm *terms = indexMultiTermCreate();
×
353
  if (terms == NULL) {
×
354
    return terrno;
×
355
  }
356

357
  int16_t nCols = taosArrayGetSize(pTagVals);
×
358
  for (int i = 0; i < nCols; i++) {
×
359
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
×
360
    char     type = pTagVal->type;
×
361

362
    char   *key = pTagVal->pKey;
×
363
    int32_t nKey = strlen(key);
×
364

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

416
static int32_t metaDropTables(SMeta *pMeta, SArray *tbUids) {
×
417
  int32_t code = 0;
×
418
  if (taosArrayGetSize(tbUids) == 0) return TSDB_CODE_SUCCESS;
×
419

420
  int64_t    nCtbDropped = 0;
×
421
  SSHashObj *suidHash = tSimpleHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
×
422
  if (suidHash == NULL) {
×
423
    return terrno;
×
424
  }
425

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

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

469
  pMeta->changed = true;
×
470
  return 0;
×
471
}
472

473
static int32_t metaFilterTableByHash(SMeta *pMeta, SArray *uidList) {
×
474
  int32_t code = 0;
×
475
  // 1, tranverse table's
476
  // 2, validate table name using vnodeValidateTableHash
477
  // 3, push invalidated table's uid into uidList
478

479
  TBC *pCur;
480
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
×
481
  if (code < 0) {
×
482
    return code;
×
483
  }
484

485
  code = tdbTbcMoveToFirst(pCur);
×
486
  if (code) {
×
487
    tdbTbcClose(pCur);
×
488
    return code;
×
489
  }
490

491
  void *pData = NULL, *pKey = NULL;
×
492
  int   nData = 0, nKey = 0;
×
493

494
  while (1) {
×
495
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
×
496
    if (ret < 0) {
×
497
      break;
×
498
    }
499

500
    SMetaEntry me = {0};
×
501
    SDecoder   dc = {0};
×
502
    tDecoderInit(&dc, pData, nData);
×
503
    code = metaDecodeEntry(&dc, &me);
×
504
    if (code < 0) {
×
505
      tDecoderClear(&dc);
×
506
      return code;
×
507
    }
508

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

528
  return 0;
×
529
}
530

531
int32_t metaTrimTables(SMeta *pMeta, int64_t version) {
×
532
  int32_t code = 0;
×
533

534
  SArray *tbUids = taosArrayInit(8, sizeof(int64_t));
×
535
  if (tbUids == NULL) {
×
536
    return terrno;
×
537
  }
538

539
  code = metaFilterTableByHash(pMeta, tbUids);
×
540
  if (code != 0) {
×
541
    goto end;
×
542
  }
543
  if (TARRAY_SIZE(tbUids) == 0) {
×
544
    goto end;
×
545
  }
546

547
  metaInfo("vgId:%d, trim %ld tables", TD_VID(pMeta->pVnode), taosArrayGetSize(tbUids));
×
548
  code = metaDropTables(pMeta, tbUids);
×
549
  if (code) goto end;
×
550

551
end:
×
552
  taosArrayDestroy(tbUids);
×
553

554
  return code;
×
555
}
556

557
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
1✔
558
  metaRLock(pMeta);
1✔
559

560
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
1✔
561

562
  metaULock(pMeta);
1✔
563

564
  if (ret != 0) {
1✔
565
    metaError("ttl failed to find expired table, ret:%d", ret);
×
566
  }
567

568
  return ret;
1✔
569
}
570

571
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
×
572
  int64_t btime;
573
  if (pME->type == TSDB_CHILD_TABLE) {
×
574
    btime = pME->ctbEntry.btime;
×
575
  } else if (pME->type == TSDB_NORMAL_TABLE) {
×
576
    btime = pME->ntbEntry.btime;
×
577
  } else {
578
    return TSDB_CODE_FAILED;
×
579
  }
580

581
  btimeKey->btime = btime;
×
582
  btimeKey->uid = pME->uid;
×
583
  return 0;
×
584
}
585

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

596
static void metaDeleteTtl(SMeta *pMeta, const SMetaEntry *pME) {
×
597
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
×
598

599
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
×
600
  if (pME->type == TSDB_CHILD_TABLE) {
×
601
    ctx.ttlDays = pME->ctbEntry.ttlDays;
×
602
  } else {
603
    ctx.ttlDays = pME->ntbEntry.ttlDays;
×
604
  }
605

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

614
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl) {
×
615
  void      *pData = NULL;
×
616
  int        nData = 0;
×
617
  int        rc = 0;
×
618
  SMetaEntry e = {0};
×
619
  SDecoder   dc = {0};
×
620
  int32_t    ret = 0;
×
621

622
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
×
623
  if (rc < 0) {
×
624
    return rc;
×
625
  }
626
  int64_t version = ((SUidIdxVal *)pData)[0].version;
×
627

628
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
×
629
  if (rc < 0) {
×
630
    tdbFree(pData);
×
631
    return rc;
×
632
  }
633

634
  tDecoderInit(&dc, pData, nData);
×
635
  rc = metaDecodeEntry(&dc, &e);
×
636
  if (rc < 0) {
×
637
    tDecoderClear(&dc);
×
638
    return rc;
×
639
  }
640

641
  if (type) *type = e.type;
×
642

643
  if (e.type == TSDB_CHILD_TABLE) {
×
644
    if (pSuid) *pSuid = e.ctbEntry.suid;
×
645
    void *tData = NULL;
×
646
    int   tLen = 0;
×
647

648
    if (tdbTbGet(pMeta->pUidIdx, &e.ctbEntry.suid, sizeof(tb_uid_t), &tData, &tLen) == 0) {
×
649
      STbDbKey tbDbKey = {.uid = e.ctbEntry.suid, .version = ((SUidIdxVal *)tData)[0].version};
×
650
      if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &tData, &tLen) == 0) {
×
651
        SDecoder   tdc = {0};
×
652
        SMetaEntry stbEntry = {0};
×
653

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

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

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

689
            const void *pTagData = NULL;
×
690
            int32_t     nTagData = 0;
×
691

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

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

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

741
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
×
742
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
×
743

744
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
×
745

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

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

774
    --pMeta->pVnode->config.vndStats.numOfNTables;
×
775
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
×
776

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

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

813
  ret = metaCacheDrop(pMeta, uid);
×
814
  if (ret < 0) {
×
815
    metaError("vgId:%d, failed to drop cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
816
              tstrerror(ret));
817
  }
818

819
  tDecoderClear(&dc);
×
820
  tdbFree(pData);
×
821

822
  return 0;
×
823
}
824

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

833
int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
×
834
  SNcolIdxKey ncolKey = {0};
×
835
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
×
836
    return 0;
×
837
  }
838
  return tdbTbDelete(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), pMeta->txn);
×
839
}
840

841
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
×
842
  pMeta->changed = true;
×
843
  switch (pReq->action) {
×
844
    case TSDB_ALTER_TABLE_ADD_COLUMN:
×
845
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
846
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF:
847
      return metaAddTableColumn(pMeta, version, pReq, pMetaRsp);
×
848
    case TSDB_ALTER_TABLE_DROP_COLUMN:
×
849
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
×
850
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
×
851
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
×
852
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
×
853
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
×
854
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
×
855
      return metaUpdateTableTagValue(pMeta, version, pReq);
×
856
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
×
857
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
×
858
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
×
859
      return metaUpdateTableOptions2(pMeta, version, pReq);
×
860
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
×
861
      return metaUpdateTableColCompress2(pMeta, version, pReq);
×
862
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
×
863
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
×
864
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
×
865
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
×
866
    default:
×
867
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
868
      break;
869
  }
870
}
871

872
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
×
873
  if (!tsTtlChangeOnWrite) return 0;
×
874

875
  if (changeTimeMs <= 0) {
×
876
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
×
877
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
×
878
  }
879

880
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
×
881

882
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
×
883
}
884

885
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
×
886
  if (!tsTtlChangeOnWrite) return 0;
×
887

888
  metaWLock(pMeta);
×
889
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
×
890
  metaULock(pMeta);
×
891
  return ret;
×
892
}
893

894
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
×
895
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
896
  if (IS_VAR_DATA_TYPE(type)) {
×
897
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
×
898
  } else {
899
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
×
900
  }
901

902
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
×
903
  if (*ppTagIdxKey == NULL) {
×
904
    return terrno;
×
905
  }
906

907
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
×
908
  (*ppTagIdxKey)->cid = cid;
×
909
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
×
910
  (*ppTagIdxKey)->type = type;
×
911

912
  // refactor
913
  if (IS_VAR_DATA_TYPE(type)) {
×
914
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
×
915
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
×
916
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
×
917
  } else {
918
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
×
919
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
×
920
  }
921

922
  return 0;
×
923
}
924

925
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
×
926
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
×
927
}
×
928

929
static void colCompressDebug(SHashObj *pColCmprObj) {
×
930
  void *p = taosHashIterate(pColCmprObj, NULL);
×
931
  while (p) {
×
932
    uint32_t cmprAlg = *(uint32_t *)p;
×
933
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
×
934
    p = taosHashIterate(pColCmprObj, p);
×
935

936
    uint8_t l1, l2, lvl;
937
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
×
938

939
    const char *l1str = columnEncodeStr(l1);
×
940
    const char *l2str = columnCompressStr(l2);
×
941
    const char *lvlstr = columnLevelStr(lvl);
×
942
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
×
943
  }
944
  return;
×
945
}
946

947
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
×
948
  int rc = 0;
×
949

950
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
×
951
  if (pColCmprObj == NULL) {
×
952
    pColCmprObj = NULL;
×
953
    return TSDB_CODE_OUT_OF_MEMORY;
×
954
  }
955

956
  void      *pData = NULL;
×
957
  int        nData = 0;
×
958
  SMetaEntry e = {0};
×
959
  SDecoder   dc = {0};
×
960

961
  *ppColCmprObj = NULL;
×
962

963
  metaRLock(pMeta);
×
964
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
×
965
  if (rc < 0) {
×
966
    taosHashCleanup(pColCmprObj);
×
967
    metaULock(pMeta);
×
968
    return TSDB_CODE_FAILED;
×
969
  }
970
  int64_t version = ((SUidIdxVal *)pData)[0].version;
×
971
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
×
972
  if (rc < 0) {
×
973
    metaULock(pMeta);
×
974
    taosHashCleanup(pColCmprObj);
×
975
    metaError("failed to get table entry");
×
976
    return rc;
×
977
  }
978

979
  tDecoderInit(&dc, pData, nData);
×
980
  rc = metaDecodeEntry(&dc, &e);
×
981
  if (rc < 0) {
×
982
    tDecoderClear(&dc);
×
983
    tdbFree(pData);
×
984
    metaULock(pMeta);
×
985
    taosHashCleanup(pColCmprObj);
×
986
    return rc;
×
987
  }
988
  if (withExtSchema(e.type)) {
×
989
    SColCmprWrapper *p = &e.colCmpr;
×
990
    for (int32_t i = 0; i < p->nCols; i++) {
×
991
      SColCmpr *pCmpr = &p->pColCmpr[i];
×
992
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
×
993
      if (rc < 0) {
×
994
        tDecoderClear(&dc);
×
995
        tdbFree(pData);
×
996
        metaULock(pMeta);
×
997
        taosHashCleanup(pColCmprObj);
×
998
        return rc;
×
999
      }
1000
    }
1001
  } else {
1002
    tDecoderClear(&dc);
×
1003
    tdbFree(pData);
×
1004
    metaULock(pMeta);
×
1005
    taosHashCleanup(pColCmprObj);
×
1006
    return 0;
×
1007
  }
1008
  tDecoderClear(&dc);
×
1009
  tdbFree(pData);
×
1010
  metaULock(pMeta);
×
1011

1012
  *ppColCmprObj = pColCmprObj;
×
1013
  colCompressDebug(pColCmprObj);
×
1014

1015
  return 0;
×
1016
}
1017
// refactor later
1018
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
×
1019
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
×
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