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

taosdata / TDengine / #3542

27 Nov 2024 02:52AM UTC coverage: 60.819% (+0.04%) from 60.776%
#3542

push

travis-ci

web-flow
Merge pull request #28931 from taosdata/enh/jdbc-demo-3.0

update jdbc demo, and version history

120305 of 252779 branches covered (47.59%)

Branch coverage included in aggregate %.

201010 of 275538 relevant lines covered (72.95%)

19989893.51 hits per line

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

62.91
/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
static int  metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema);
21
static int  metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema);
22
static int  metaSaveToTbDb(SMeta *pMeta, const SMetaEntry *pME);
23
static int  metaUpdateUidIdx(SMeta *pMeta, const SMetaEntry *pME);
24
static int  metaUpdateNameIdx(SMeta *pMeta, const SMetaEntry *pME);
25
static void metaUpdateTtl(SMeta *pMeta, const SMetaEntry *pME);
26
static int  metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs);
27
static int  metaSaveToSkmDb(SMeta *pMeta, const SMetaEntry *pME);
28
static int  metaUpdateCtbIdx(SMeta *pMeta, const SMetaEntry *pME);
29
static int  metaUpdateSuidIdx(SMeta *pMeta, const SMetaEntry *pME);
30
static int  metaUpdateTagIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry);
31
static int  metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl);
32
static void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey);
33
// opt ins_tables query
34
static int metaUpdateBtimeIdx(SMeta *pMeta, const SMetaEntry *pME);
35
static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME);
36
static int metaUpdateNcolIdx(SMeta *pMeta, const SMetaEntry *pME);
37
static int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME);
38

39
static int32_t updataTableColCmpr(SColCmprWrapper *pWp, SSchema *pSchema, int8_t add, uint32_t compress) {
650✔
40
  int32_t nCols = pWp->nCols;
650✔
41
  int32_t ver = pWp->version;
650✔
42
  if (add) {
650✔
43
    SColCmpr *p = taosMemoryCalloc(1, sizeof(SColCmpr) * (nCols + 1));
582✔
44
    if (p == NULL) {
582!
45
      return terrno;
×
46
    }
47

48
    memcpy(p, pWp->pColCmpr, sizeof(SColCmpr) * nCols);
582✔
49

50
    SColCmpr *pCol = p + nCols;
582✔
51
    pCol->id = pSchema->colId;
582✔
52
    pCol->alg = compress;
582✔
53
    pWp->nCols = nCols + 1;
582✔
54
    pWp->version = ver;
582✔
55
    pWp->pColCmpr = p;
582✔
56
  } else {
57
    for (int32_t i = 0; i < nCols; i++) {
20,815!
58
      SColCmpr *pOCmpr = &pWp->pColCmpr[i];
20,815✔
59
      if (pOCmpr->id == pSchema->colId) {
20,815✔
60
        int32_t left = (nCols - i - 1) * sizeof(SColCmpr);
68✔
61
        if (left) {
68✔
62
          memmove(pWp->pColCmpr + i, pWp->pColCmpr + i + 1, left);
35✔
63
        }
64
        nCols--;
68✔
65
        break;
68✔
66
      }
67
    }
68
    pWp->nCols = nCols;
68✔
69
    pWp->version = ver;
68✔
70
  }
71
  return 0;
650✔
72
}
73
static void metaGetEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) {
274,585✔
74
  pInfo->uid = pEntry->uid;
274,585✔
75
  pInfo->version = pEntry->version;
274,585✔
76
  if (pEntry->type == TSDB_SUPER_TABLE) {
274,585✔
77
    pInfo->suid = pEntry->uid;
39,040✔
78
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
39,040✔
79
  } else if (pEntry->type == TSDB_CHILD_TABLE) {
235,545✔
80
    pInfo->suid = pEntry->ctbEntry.suid;
219,301✔
81
    pInfo->skmVer = 0;
219,301✔
82
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
16,244!
83
    pInfo->suid = 0;
16,314✔
84
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
16,314✔
85
  } else {
86
    metaError("meta/table: invalide table type: %" PRId8 " get entry info failed.", pEntry->type);
×
87
  }
88
}
274,585✔
89

90
static int metaUpdateMetaRsp(tb_uid_t uid, char *tbName, SSchemaWrapper *pSchema, STableMetaRsp *pMetaRsp) {
16,246✔
91
  pMetaRsp->pSchemas = taosMemoryMalloc(pSchema->nCols * sizeof(SSchema));
16,246✔
92
  if (NULL == pMetaRsp->pSchemas) {
16,246!
93
    return terrno;
×
94
  }
95

96
  pMetaRsp->pSchemaExt = taosMemoryMalloc(pSchema->nCols * sizeof(SSchemaExt));
16,246✔
97
  if (pMetaRsp->pSchemaExt == NULL) {
16,246!
98
    taosMemoryFree(pMetaRsp->pSchemas);
×
99
    return terrno;
×
100
  }
101

102
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
16,246✔
103
  pMetaRsp->numOfColumns = pSchema->nCols;
16,246✔
104
  pMetaRsp->tableType = TSDB_NORMAL_TABLE;
16,246✔
105
  pMetaRsp->sversion = pSchema->version;
16,246✔
106
  pMetaRsp->tuid = uid;
16,246✔
107

108
  memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
16,246✔
109

110
  return 0;
16,246✔
111
}
112

113
static int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
576✔
114
  int32_t code = 0;
576✔
115

116
#ifdef USE_INVERTED_INDEX
117
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
576!
118
    return TSDB_CODE_INVALID_PARA;
×
119
  }
120
  void       *data = pCtbEntry->ctbEntry.pTags;
576✔
121
  const char *tagName = pSchema->name;
576✔
122

123
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
576✔
124
  tb_uid_t    tuid = pCtbEntry->uid;
576✔
125
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
576✔
126
  int32_t     nTagData = 0;
576✔
127

128
  SArray *pTagVals = NULL;
576✔
129
  code = tTagToValArray((const STag *)data, &pTagVals);
576✔
130
  if (code) {
576!
131
    return code;
×
132
  }
133

134
  SIndexMultiTerm *terms = indexMultiTermCreate();
576✔
135
  if (terms == NULL) {
576!
136
    return terrno;
×
137
  }
138

139
  int16_t nCols = taosArrayGetSize(pTagVals);
576✔
140
  for (int i = 0; i < nCols; i++) {
1,513✔
141
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
937✔
142
    char     type = pTagVal->type;
937✔
143

144
    char   *key = pTagVal->pKey;
937✔
145
    int32_t nKey = strlen(key);
937✔
146

147
    SIndexTerm *term = NULL;
937✔
148
    if (type == TSDB_DATA_TYPE_NULL) {
937✔
149
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
24✔
150
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
913✔
151
      if (pTagVal->nData > 0) {
812✔
152
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
796✔
153
        if (val == NULL) {
796!
154
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
155
        }
156
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE);
796✔
157
        if (len < 0) {
796!
158
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
159
        }
160
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
796✔
161
        type = TSDB_DATA_TYPE_VARCHAR;
796✔
162
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
796✔
163
        taosMemoryFree(val);
796✔
164
      } else if (pTagVal->nData == 0) {
16!
165
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
16✔
166
      }
167
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
101✔
168
      double val = *(double *)(&pTagVal->i64);
77✔
169
      int    len = sizeof(val);
77✔
170
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
77✔
171
    } else if (type == TSDB_DATA_TYPE_BOOL) {
24!
172
      int val = *(int *)(&pTagVal->i64);
24✔
173
      int len = sizeof(val);
24✔
174
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
24✔
175
    }
176

177
    if (term != NULL) {
937!
178
      int32_t ret = indexMultiTermAdd(terms, term);
937✔
179
      if (ret < 0) {
937!
180
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
181
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
182
      }
183
    } else {
184
      code = terrno;
×
185
      goto _exception;
×
186
    }
187
  }
188
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
576✔
189
  indexMultiTermDestroy(terms);
576✔
190

191
  taosArrayDestroy(pTagVals);
576✔
192
#endif
193
  return code;
576✔
194
_exception:
×
195
  indexMultiTermDestroy(terms);
×
196
  taosArrayDestroy(pTagVals);
×
197
  return code;
×
198
}
199
int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
196✔
200
#ifdef USE_INVERTED_INDEX
201
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
196!
202
    return TSDB_CODE_INVALID_PARA;
×
203
  }
204
  void       *data = pCtbEntry->ctbEntry.pTags;
196✔
205
  const char *tagName = pSchema->name;
196✔
206

207
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
196✔
208
  tb_uid_t    tuid = pCtbEntry->uid;
196✔
209
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
196✔
210
  int32_t     nTagData = 0;
196✔
211

212
  SArray *pTagVals = NULL;
196✔
213
  int32_t code = tTagToValArray((const STag *)data, &pTagVals);
196✔
214
  if (code) {
196!
215
    return code;
×
216
  }
217

218
  SIndexMultiTerm *terms = indexMultiTermCreate();
196✔
219
  if (terms == NULL) {
196!
220
    return terrno;
×
221
  }
222

223
  int16_t nCols = taosArrayGetSize(pTagVals);
196✔
224
  for (int i = 0; i < nCols; i++) {
555✔
225
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
359✔
226
    char     type = pTagVal->type;
359✔
227

228
    char   *key = pTagVal->pKey;
359✔
229
    int32_t nKey = strlen(key);
359✔
230

231
    SIndexTerm *term = NULL;
359✔
232
    if (type == TSDB_DATA_TYPE_NULL) {
359!
233
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
×
234
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
359✔
235
      if (pTagVal->nData > 0) {
355!
236
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
355✔
237
        if (val == NULL) {
355!
238
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
239
        }
240
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE);
355✔
241
        if (len < 0) {
355!
242
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
243
        }
244
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
355✔
245
        type = TSDB_DATA_TYPE_VARCHAR;
355✔
246
        term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, val, len);
355✔
247
        taosMemoryFree(val);
355✔
248
      } else if (pTagVal->nData == 0) {
×
249
        term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
×
250
      }
251
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
4!
252
      double val = *(double *)(&pTagVal->i64);
4✔
253
      int    len = sizeof(val);
4✔
254
      term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, (const char *)&val, len);
4✔
255
    } else if (type == TSDB_DATA_TYPE_BOOL) {
×
256
      int val = *(int *)(&pTagVal->i64);
×
257
      int len = sizeof(val);
×
258
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
×
259
    }
260
    if (term != NULL) {
359!
261
      int32_t ret = indexMultiTermAdd(terms, term);
359✔
262
      if (ret < 0) {
359!
263
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
264
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
265
      }
266
    } else {
267
      code = terrno;
×
268
      goto _exception;
×
269
    }
270
  }
271
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
196✔
272
  indexMultiTermDestroy(terms);
196✔
273
  taosArrayDestroy(pTagVals);
196✔
274
#endif
275
  return code;
196✔
276
_exception:
×
277
  indexMultiTermDestroy(terms);
×
278
  taosArrayDestroy(pTagVals);
×
279
  return code;
×
280
}
281

282
static inline void metaTimeSeriesNotifyCheck(SMeta *pMeta) {
196,519✔
283
#if defined(TD_ENTERPRISE)
284
  int64_t nTimeSeries = metaGetTimeSeriesNum(pMeta, 0);
196,519✔
285
  int64_t deltaTS = nTimeSeries - pMeta->pVnode->config.vndStats.numOfReportedTimeSeries;
196,528✔
286
  if (deltaTS > tsTimeSeriesThreshold) {
196,528✔
287
    if (0 == atomic_val_compare_exchange_8(&dmNotifyHdl.state, 1, 2)) {
133,580✔
288
      if (tsem_post(&dmNotifyHdl.sem) != 0) {
133,576!
289
        metaError("vgId:%d, failed to post semaphore, errno:%d", TD_VID(pMeta->pVnode), errno);
×
290
      }
291
    }
292
  }
293
#endif
294
}
196,559✔
295

296
int metaCreateSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
30,760✔
297
  SMetaEntry  me = {0};
30,760✔
298
  int         kLen = 0;
30,760✔
299
  int         vLen = 0;
30,760✔
300
  const void *pKey = NULL;
30,760✔
301
  const void *pVal = NULL;
30,760✔
302
  void       *pBuf = NULL;
30,760✔
303
  int32_t     szBuf = 0;
30,760✔
304
  void       *p = NULL;
30,760✔
305
  int32_t     code = 0;
30,760✔
306

307
  // validate req
308
  void *pData = NULL;
30,760✔
309
  int   nData = 0;
30,760✔
310
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &pData, &nData) == 0) {
30,760✔
311
    tb_uid_t uid = *(tb_uid_t *)pData;
42✔
312
    tdbFree(pData);
42✔
313
    SMetaInfo info;
314
    if (metaGetInfo(pMeta, uid, &info, NULL) == TSDB_CODE_NOT_FOUND) {
42!
315
      return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
316
    }
317
    if (info.uid == info.suid) {
42✔
318
      return 0;
38✔
319
    } else {
320
      return terrno = TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
4✔
321
    }
322
  }
323

324
  // set structs
325
  me.version = version;
30,832✔
326
  me.type = TSDB_SUPER_TABLE;
30,832✔
327
  me.uid = pReq->suid;
30,832✔
328
  me.name = pReq->name;
30,832✔
329
  me.stbEntry.schemaRow = pReq->schemaRow;
30,832✔
330
  me.stbEntry.schemaTag = pReq->schemaTag;
30,832✔
331
  if (pReq->rollup) {
30,832✔
332
    TABLE_SET_ROLLUP(me.flags);
7✔
333
    me.stbEntry.rsmaParam = pReq->rsmaParam;
7✔
334
  }
335
  if (pReq->colCmpred) {
30,832✔
336
    TABLE_SET_COL_COMPRESSED(me.flags);
30,796✔
337
    me.colCmpr = pReq->colCmpr;
30,796✔
338
  }
339

340
  code = metaHandleEntry(pMeta, &me);
30,832✔
341
  if (code) goto _err;
30,857!
342

343
  ++pMeta->pVnode->config.vndStats.numOfSTables;
30,857✔
344

345
  pMeta->changed = true;
30,857✔
346
  metaDebug("vgId:%d, stb:%s is created, suid:%" PRId64, TD_VID(pMeta->pVnode), pReq->name, pReq->suid);
30,857✔
347

348
  return 0;
30,879✔
349

350
_err:
×
351
  metaError("vgId:%d, failed to create stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
352
            tstrerror(terrno));
353
  return code;
×
354
}
355

356
int metaDropSTable(SMeta *pMeta, int64_t verison, SVDropStbReq *pReq, SArray *tbUidList) {
3,366✔
357
  void   *pKey = NULL;
3,366✔
358
  int     nKey = 0;
3,366✔
359
  void   *pData = NULL;
3,366✔
360
  int     nData = 0;
3,366✔
361
  int     c = 0;
3,366✔
362
  int     rc = 0;
3,366✔
363
  int32_t lino;
364
  int32_t ret;
365

366
  // check if super table exists
367
  rc = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &pData, &nData);
3,366✔
368
  if (rc < 0 || *(tb_uid_t *)pData != pReq->suid) {
3,369!
369
    tdbFree(pData);
1✔
370
    return terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
2✔
371
  }
372

373
  // drop all child tables
374
  TBC *pCtbIdxc = NULL;
3,368✔
375

376
  rc = tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL);
3,368✔
377
  if (rc) {
3,370!
378
    return (terrno = rc);
×
379
  }
380

381
  rc = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = pReq->suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c);
3,370✔
382
  if (rc < 0) {
3,366!
383
    tdbTbcClose(pCtbIdxc);
×
384
    metaWLock(pMeta);
×
385
    goto _drop_super_table;
×
386
  }
387

388
  for (;;) {
389
    rc = tdbTbcNext(pCtbIdxc, &pKey, &nKey, NULL, NULL);
7,934✔
390
    if (rc < 0) break;
7,937✔
391

392
    if (((SCtbIdxKey *)pKey)->suid < pReq->suid) {
6,400✔
393
      continue;
410✔
394
    } else if (((SCtbIdxKey *)pKey)->suid > pReq->suid) {
5,990✔
395
      break;
1,833✔
396
    }
397

398
    if (taosArrayPush(tbUidList, &(((SCtbIdxKey *)pKey)->uid)) == NULL) {
8,315!
399
      tdbFree(pKey);
×
400
      tdbTbcClose(pCtbIdxc);
×
401
      return terrno;
×
402
    }
403
  }
404

405
  tdbTbcClose(pCtbIdxc);
3,370✔
406

407
  ret = tsdbCacheDropSubTables(pMeta->pVnode->pTsdb, tbUidList, pReq->suid);
3,371✔
408
  if (ret < 0) {
3,371!
409
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
410
              tstrerror(terrno));
411
  }
412

413
  metaWLock(pMeta);
3,371✔
414

415
  for (int32_t iChild = 0; iChild < taosArrayGetSize(tbUidList); iChild++) {
7,538✔
416
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUidList, iChild);
4,169✔
417
    ret = metaDropTableByUid(pMeta, uid, NULL, NULL, NULL);
4,169✔
418
    if (ret < 0) {
4,168!
419
      metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
420
                pReq->suid, tstrerror(terrno));
421
    }
422
  }
423

424
  // drop super table
425
_drop_super_table:
3,369✔
426
  tdbTbGet(pMeta->pUidIdx, &pReq->suid, sizeof(tb_uid_t), &pData, &nData);
3,369✔
427
  ret = tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = ((SUidIdxVal *)pData)[0].version, .uid = pReq->suid},
3,371✔
428
                    sizeof(STbDbKey), pMeta->txn);
429
  if (ret < 0) {
3,371!
430
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
431
              tstrerror(terrno));
432
  }
433

434
  ret = tdbTbDelete(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, pMeta->txn);
3,371✔
435
  if (ret < 0) {
3,371!
436
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
437
              tstrerror(terrno));
438
  }
439

440
  ret = tdbTbDelete(pMeta->pUidIdx, &pReq->suid, sizeof(tb_uid_t), pMeta->txn);
3,371✔
441
  if (ret < 0) {
3,370!
442
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
443
              tstrerror(terrno));
444
  }
445

446
  ret = tdbTbDelete(pMeta->pSuidIdx, &pReq->suid, sizeof(tb_uid_t), pMeta->txn);
3,370✔
447
  if (ret < 0) {
3,371!
448
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
449
              tstrerror(terrno));
450
  }
451

452
  ret = metaStatsCacheDrop(pMeta, pReq->suid);
3,371✔
453
  if (ret < 0) {
3,371✔
454
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
492!
455
              tstrerror(terrno));
456
  }
457

458
  metaULock(pMeta);
3,371✔
459

460
  metaUpdTimeSeriesNum(pMeta);
3,371✔
461

462
  pMeta->changed = true;
3,371✔
463

464
_exit:
3,371✔
465
  tdbFree(pKey);
3,371✔
466
  tdbFree(pData);
3,371✔
467
  metaDebug("vgId:%d, super table %s uid:%" PRId64 " is dropped", TD_VID(pMeta->pVnode), pReq->name, pReq->suid);
3,371✔
468
  return 0;
3,371✔
469
}
470

471
static int32_t metaGetSubtables(SMeta *pMeta, int64_t suid, SArray *uids) {
96✔
472
  if (!uids) return TSDB_CODE_INVALID_PARA;
96!
473

474
  int   c = 0;
96✔
475
  void *pKey = NULL;
96✔
476
  int   nKey = 0;
96✔
477
  TBC  *pCtbIdxc = NULL;
96✔
478

479
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL));
96!
480
  int rc = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c);
96✔
481
  if (rc < 0) {
96!
482
    tdbTbcClose(pCtbIdxc);
×
483
    metaWLock(pMeta);
×
484
    return 0;
×
485
  }
486

487
  for (;;) {
488
    rc = tdbTbcNext(pCtbIdxc, &pKey, &nKey, NULL, NULL);
336✔
489
    if (rc < 0) break;
336✔
490

491
    if (((SCtbIdxKey *)pKey)->suid < suid) {
240!
492
      continue;
×
493
    } else if (((SCtbIdxKey *)pKey)->suid > suid) {
240!
494
      break;
×
495
    }
496

497
    if (taosArrayPush(uids, &(((SCtbIdxKey *)pKey)->uid)) == NULL) {
480!
498
      tdbFree(pKey);
×
499
      tdbTbcClose(pCtbIdxc);
×
500
      return terrno;
×
501
    }
502
  }
503

504
  tdbFree(pKey);
96✔
505

506
  tdbTbcClose(pCtbIdxc);
96✔
507
  return 0;
96✔
508
}
509

510
int metaAlterSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
4,906✔
511
  SMetaEntry  oStbEntry = {0};
4,906✔
512
  SMetaEntry  nStbEntry = {0};
4,906✔
513
  TBC        *pUidIdxc = NULL;
4,906✔
514
  TBC        *pTbDbc = NULL;
4,906✔
515
  const void *pData;
516
  int         nData;
517
  int64_t     oversion;
518
  SDecoder    dc = {0};
4,906✔
519
  int32_t     ret;
520
  int32_t     c = -2;
4,906✔
521

522
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, NULL));
4,906!
523
  ret = tdbTbcMoveTo(pUidIdxc, &pReq->suid, sizeof(tb_uid_t), &c);
4,914✔
524
  if (ret < 0 || c) {
4,909!
525
    tdbTbcClose(pUidIdxc);
×
526

527
    return terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
×
528
  }
529

530
  ret = tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
4,914✔
531
  if (ret < 0) {
4,916!
532
    tdbTbcClose(pUidIdxc);
×
533

534
    return terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
×
535
  }
536

537
  oversion = ((SUidIdxVal *)pData)[0].version;
4,916✔
538

539
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pTbDb, &pTbDbc, NULL));
4,916!
540
  ret = tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = pReq->suid, .version = oversion}), sizeof(STbDbKey), &c);
4,918✔
541
  if (!(ret == 0 && c == 0)) {
4,917!
542
    tdbTbcClose(pUidIdxc);
7✔
543
    tdbTbcClose(pTbDbc);
×
544

545
    metaError("meta/table: invalide ret: %" PRId32 " or c: %" PRId32 "alter stb failed.", ret, c);
×
546
    return terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
×
547
  }
548

549
  ret = tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);
4,910✔
550
  if (ret < 0) {
4,916!
551
    tdbTbcClose(pUidIdxc);
×
552
    tdbTbcClose(pTbDbc);
×
553

554
    return terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
×
555
  }
556

557
  if ((oStbEntry.pBuf = taosMemoryMalloc(nData)) == NULL) {
4,916!
558
    tdbTbcClose(pTbDbc);
×
559
    tdbTbcClose(pUidIdxc);
×
560
    return terrno;
×
561
  }
562
  memcpy(oStbEntry.pBuf, pData, nData);
4,916✔
563
  tDecoderInit(&dc, oStbEntry.pBuf, nData);
4,916✔
564
  ret = metaDecodeEntry(&dc, &oStbEntry);
4,910✔
565
  if (ret < 0) {
4,905!
566
    metaError("vgId:%d, failed to decode stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
567
              pReq->suid, tstrerror(ret));
568
    tDecoderClear(&dc);
×
569
    tdbTbcClose(pTbDbc);
×
570
    tdbTbcClose(pUidIdxc);
×
571
    return terrno;
×
572
  }
573

574
  nStbEntry.version = version;
4,905✔
575
  nStbEntry.type = TSDB_SUPER_TABLE;
4,905✔
576
  nStbEntry.uid = pReq->suid;
4,905✔
577
  nStbEntry.name = pReq->name;
4,905✔
578
  nStbEntry.stbEntry.schemaRow = pReq->schemaRow;
4,905✔
579
  nStbEntry.stbEntry.schemaTag = pReq->schemaTag;
4,905✔
580
  nStbEntry.colCmpr = pReq->colCmpr;
4,905✔
581
  TABLE_SET_COL_COMPRESSED(nStbEntry.flags);
4,905✔
582

583
  int     nCols = pReq->schemaRow.nCols;
4,905✔
584
  int     onCols = oStbEntry.stbEntry.schemaRow.nCols;
4,905✔
585
  int32_t deltaCol = nCols - onCols;
4,905✔
586
  bool    updStat = deltaCol != 0 && !metaTbInFilterCache(pMeta, pReq->name, 1);
4,905!
587

588
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
4,914✔
589
    STsdb  *pTsdb = pMeta->pVnode->pTsdb;
128✔
590
    SArray *uids = taosArrayInit(8, sizeof(int64_t));
128✔
591
    if (uids == NULL) {
128!
592
      if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
×
593
      tDecoderClear(&dc);
×
594
      tdbTbcClose(pTbDbc);
×
595
      tdbTbcClose(pUidIdxc);
×
596
      return terrno;
×
597
    }
598
    if (deltaCol == 1) {
128✔
599
      int16_t cid = pReq->schemaRow.pSchema[nCols - 1].colId;
64✔
600
      int8_t  col_type = pReq->schemaRow.pSchema[nCols - 1].type;
64✔
601

602
      TAOS_CHECK_RETURN(metaGetSubtables(pMeta, pReq->suid, uids));
64!
603
      TAOS_CHECK_RETURN(tsdbCacheNewSTableColumn(pTsdb, uids, cid, col_type));
64!
604
    } else if (deltaCol == -1) {
64!
605
      int16_t cid = -1;
64✔
606
      bool    hasPrimaryKey = false;
64✔
607
      if (onCols >= 2) {
64!
608
        hasPrimaryKey = (oStbEntry.stbEntry.schemaRow.pSchema[1].flags & COL_IS_KEY) ? true : false;
64✔
609
      }
610
      for (int i = 0, j = 0; i < nCols && j < onCols; ++i, ++j) {
407!
611
        if (pReq->schemaRow.pSchema[i].colId != oStbEntry.stbEntry.schemaRow.pSchema[j].colId) {
375✔
612
          cid = oStbEntry.stbEntry.schemaRow.pSchema[j].colId;
32✔
613
          break;
32✔
614
        }
615
      }
616

617
      if (cid != -1) {
64✔
618
        TAOS_CHECK_RETURN(metaGetSubtables(pMeta, pReq->suid, uids));
32!
619
        TAOS_CHECK_RETURN(tsdbCacheDropSTableColumn(pTsdb, uids, cid, hasPrimaryKey));
32!
620
      }
621
    }
622
    if (uids) taosArrayDestroy(uids);
128✔
623

624
    tsdbCacheInvalidateSchema(pTsdb, pReq->suid, -1, pReq->schemaRow.version);
129✔
625
  }
626

627
  metaWLock(pMeta);
4,914✔
628
  // compare two entry
629
  if (oStbEntry.stbEntry.schemaRow.version != pReq->schemaRow.version) {
4,915✔
630
    ret = metaSaveToSkmDb(pMeta, &nStbEntry);
2,996✔
631
    if (ret < 0) {
2,992!
632
      metaError("vgId:%d, failed to save skm db:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
633
                pReq->suid, tstrerror(ret));
634
    }
635
  }
636

637
  // update table.db
638
  ret = metaSaveToTbDb(pMeta, &nStbEntry);
4,911✔
639
  if (ret < 0) {
4,910!
640
    metaError("vgId:%d, failed to save tb db:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
641
              pReq->suid, tstrerror(ret));
642
  }
643

644
  // update uid index
645
  ret = metaUpdateUidIdx(pMeta, &nStbEntry);
4,910✔
646
  if (ret < 0) {
4,918!
647
    metaError("vgId:%d, failed to update uid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
648
              pReq->suid, tstrerror(ret));
649
  }
650

651
  // metaStatsCacheDrop(pMeta, nStbEntry.uid);
652

653
  if (updStat) {
4,914✔
654
    metaUpdateStbStats(pMeta, pReq->suid, 0, deltaCol);
2,146✔
655
  }
656
  metaULock(pMeta);
4,915✔
657

658
  if (updStat) {
4,914✔
659
    int64_t ctbNum;
660
    ret = metaGetStbStats(pMeta->pVnode, pReq->suid, &ctbNum, NULL);
2,147✔
661
    if (ret < 0) {
2,146!
662
      metaError("vgId:%d, failed to get stb stats:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
663
                pReq->suid, tstrerror(ret));
664
    }
665
    pMeta->pVnode->config.vndStats.numOfTimeSeries += (ctbNum * deltaCol);
2,146✔
666
    metaTimeSeriesNotifyCheck(pMeta);
2,146✔
667
  }
668

669
  pMeta->changed = true;
4,911✔
670

671
_exit:
4,911✔
672
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
4,911!
673
  tDecoderClear(&dc);
4,915✔
674
  tdbTbcClose(pTbDbc);
4,919✔
675
  tdbTbcClose(pUidIdxc);
4,917✔
676
  return 0;
4,918✔
677
}
678
int metaAddIndexToSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
979✔
679
  SMetaEntry oStbEntry = {0};
979✔
680
  SMetaEntry nStbEntry = {0};
979✔
681
  STbDbKey   tbDbKey = {0};
979✔
682

683
  TBC     *pUidIdxc = NULL;
979✔
684
  TBC     *pTbDbc = NULL;
979✔
685
  void    *pData = NULL;
979✔
686
  int      nData = 0;
979✔
687
  int64_t  oversion;
688
  SDecoder dc = {0};
979✔
689
  int32_t  ret;
690
  int32_t  c = -2;
979✔
691
  tb_uid_t suid = pReq->suid;
979✔
692
  int32_t  code = 0;
979✔
693

694
  // get super table
695
  if ((code = tdbTbGet(pMeta->pUidIdx, &suid, sizeof(tb_uid_t), &pData, &nData)) != 0) {
979!
696
    goto _err;
×
697
  }
698

699
  tbDbKey.uid = suid;
979✔
700
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
979✔
701
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData)) != 0) {
979!
702
    goto _err;
×
703
  }
704

705
  tDecoderInit(&dc, pData, nData);
979✔
706
  code = metaDecodeEntry(&dc, &oStbEntry);
979✔
707
  if (code < 0) {
979!
708
    goto _err;
×
709
  }
710

711
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
979!
712
    code = TSDB_CODE_INVALID_PARA;
×
713
    goto _err;
×
714
  }
715

716
  if (oStbEntry.stbEntry.schemaTag.version == pReq->schemaTag.version) {
979!
717
    code = TSDB_CODE_INVALID_PARA;
×
718
    goto _err;
×
719
  }
720

721
  if (oStbEntry.stbEntry.schemaTag.nCols != pReq->schemaTag.nCols) {
979!
722
    code = TSDB_CODE_INVALID_PARA;
×
723
    goto _err;
×
724
  }
725

726
  int diffIdx = -1;
979✔
727
  for (int i = 0; i < pReq->schemaTag.nCols; i++) {
7,744!
728
    SSchema *pNew = pReq->schemaTag.pSchema + i;
7,744✔
729
    SSchema *pOld = oStbEntry.stbEntry.schemaTag.pSchema + i;
7,744✔
730
    if (pNew->type != pOld->type || pNew->colId != pOld->colId || pNew->bytes != pOld->bytes ||
7,744!
731
        strncmp(pOld->name, pNew->name, sizeof(pNew->name))) {
7,744!
732
      code = TSDB_CODE_INVALID_PARA;
×
733
      goto _err;
×
734
    }
735
    if (IS_IDX_ON(pNew) && !IS_IDX_ON(pOld)) {
7,744✔
736
      // if (diffIdx != -1) goto _err;
737
      diffIdx = i;
979✔
738
      break;
979✔
739
    }
740
  }
741

742
  if (diffIdx == -1) {
979!
743
    code = TSDB_CODE_INVALID_PARA;
×
744
    goto _err;
×
745
  }
746

747
  // Get target schema info
748
  SSchemaWrapper *pTagSchema = &pReq->schemaTag;
979✔
749
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
979!
750
    code = TSDB_CODE_INVALID_PARA;
×
751
    goto _err;
×
752
  }
753
  SSchema *pCol = pTagSchema->pSchema + diffIdx;
979✔
754

755
  /*
756
   * iterator all pTdDbc by uid and version
757
   */
758
  TBC *pCtbIdxc = NULL;
979✔
759
  code = tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL);
979✔
760
  if (code != 0) {
979!
761
    goto _err;
×
762
  }
763

764
  code = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c);
979✔
765
  if (code < 0) {
979!
766
    tdbTbcClose(pCtbIdxc);
×
767
    goto _err;
×
768
  }
769
  for (;;) {
19,097✔
770
    void *pKey = NULL, *pVal = NULL;
20,076✔
771
    int   nKey = 0, nVal = 0;
20,076✔
772
    code = tdbTbcNext(pCtbIdxc, &pKey, &nKey, &pVal, &nVal);
20,076✔
773
    if (code < 0) {
20,076✔
774
      tdbFree(pKey);
979✔
775
      tdbFree(pVal);
979✔
776
      tdbTbcClose(pCtbIdxc);
979✔
777
      pCtbIdxc = NULL;
979✔
778
      break;
979✔
779
    }
780
    if (((SCtbIdxKey *)pKey)->suid != suid) {
19,097✔
781
      tdbFree(pKey);
3,781✔
782
      tdbFree(pVal);
3,781✔
783
      continue;
3,781✔
784
    }
785
    STagIdxKey *pTagIdxKey = NULL;
15,316✔
786
    int32_t     nTagIdxKey;
787

788
    const void *pTagData = NULL;
15,316✔
789
    int32_t     nTagData = 0;
15,316✔
790

791
    SCtbIdxKey *table = (SCtbIdxKey *)pKey;
15,316✔
792
    STagVal     tagVal = {.cid = pCol->colId};
15,316✔
793
    if (tTagGet((const STag *)pVal, &tagVal)) {
15,316!
794
      if (IS_VAR_DATA_TYPE(pCol->type)) {
15,316!
795
        pTagData = tagVal.pData;
2,371✔
796
        nTagData = (int32_t)tagVal.nData;
2,371✔
797
      } else {
798
        pTagData = &(tagVal.i64);
12,945✔
799
        nTagData = tDataTypes[pCol->type].bytes;
12,945✔
800
      }
801
    } else {
802
      if (!IS_VAR_DATA_TYPE(pCol->type)) {
×
803
        nTagData = tDataTypes[pCol->type].bytes;
×
804
      }
805
    }
806
    code = metaCreateTagIdxKey(suid, pCol->colId, pTagData, nTagData, pCol->type, table->uid, &pTagIdxKey, &nTagIdxKey);
15,316✔
807
    tdbFree(pKey);
15,316✔
808
    tdbFree(pVal);
15,316✔
809
    if (code < 0) {
15,316!
810
      metaDestroyTagIdxKey(pTagIdxKey);
×
811
      tdbTbcClose(pCtbIdxc);
×
812
      goto _err;
×
813
    }
814

815
    metaWLock(pMeta);
15,316✔
816
    ret = tdbTbUpsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn);
15,316✔
817
    if (ret < 0) {
15,316!
818
      metaError("vgId:%d, failed to upsert tag idx key:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
819
                pReq->suid, tstrerror(ret));
820
    }
821
    metaULock(pMeta);
15,316✔
822

823
    metaDestroyTagIdxKey(pTagIdxKey);
15,316✔
824
    pTagIdxKey = NULL;
15,316✔
825
  }
826

827
  nStbEntry.version = version;
979✔
828
  nStbEntry.type = TSDB_SUPER_TABLE;
979✔
829
  nStbEntry.uid = pReq->suid;
979✔
830
  nStbEntry.name = pReq->name;
979✔
831
  nStbEntry.stbEntry.schemaRow = pReq->schemaRow;
979✔
832
  nStbEntry.stbEntry.schemaTag = pReq->schemaTag;
979✔
833
  nStbEntry.colCmpr = pReq->colCmpr;
979✔
834

835
  metaWLock(pMeta);
979✔
836
  // update table.db
837
  ret = metaSaveToTbDb(pMeta, &nStbEntry);
979✔
838
  if (ret < 0) {
979!
839
    metaError("vgId:%d, failed to save tb db:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
840
              pReq->suid, tstrerror(ret));
841
  }
842
  // update uid index
843
  ret = metaUpdateUidIdx(pMeta, &nStbEntry);
979✔
844
  if (ret < 0) {
979!
845
    metaError("vgId:%d, failed to update uid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
846
              pReq->suid, tstrerror(ret));
847
  }
848
  metaULock(pMeta);
979✔
849

850
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
979!
851
  tDecoderClear(&dc);
979✔
852
  tdbFree(pData);
979✔
853

854
  tdbTbcClose(pCtbIdxc);
979✔
855
  return TSDB_CODE_SUCCESS;
979✔
856
_err:
×
857
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
×
858
  tDecoderClear(&dc);
×
859
  tdbFree(pData);
×
860

861
  return code;
×
862
}
863
int metaDropIndexFromSTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
2,154✔
864
  int32_t    code = 0;
2,154✔
865
  SMetaEntry oStbEntry = {0};
2,154✔
866
  SMetaEntry nStbEntry = {0};
2,154✔
867

868
  STbDbKey tbDbKey = {0};
2,154✔
869
  TBC     *pUidIdxc = NULL;
2,154✔
870
  TBC     *pTbDbc = NULL;
2,154✔
871
  int      ret = 0;
2,154✔
872
  int      c = -2;
2,154✔
873
  void    *pData = NULL;
2,154✔
874
  int      nData = 0;
2,154✔
875
  int64_t  oversion;
876
  SDecoder dc = {0};
2,154✔
877

878
  tb_uid_t suid = pReq->stbUid;
2,154✔
879

880
  if ((code = tdbTbGet(pMeta->pUidIdx, &suid, sizeof(tb_uid_t), &pData, &nData)) != 0) {
2,154!
881
    goto _err;
×
882
  }
883

884
  tbDbKey.uid = suid;
2,154✔
885
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
2,154✔
886
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData)) != 0) {
2,154!
887
    goto _err;
×
888
  }
889

890
  tDecoderInit(&dc, pData, nData);
2,154✔
891
  code = metaDecodeEntry(&dc, &oStbEntry);
2,154✔
892
  if (code != 0) {
2,154!
893
    goto _err;
×
894
  }
895

896
  SSchema *pCol = NULL;
2,154✔
897
  int32_t  colId = -1;
2,154✔
898
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
6,790!
899
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
6,790✔
900
    if (0 == strncmp(schema->name, pReq->colName, sizeof(pReq->colName))) {
6,790✔
901
      if (IS_IDX_ON(schema)) {
2,154!
902
        pCol = schema;
2,154✔
903
      }
904
      break;
2,154✔
905
    }
906
  }
907

908
  if (pCol == NULL) {
2,154!
909
    metaError("vgId:%d, failed to drop index on %s.%s,since %s", TD_VID(pMeta->pVnode), pReq->stb, pReq->colName,
×
910
              tstrerror(TSDB_CODE_VND_COL_NOT_EXISTS));
911
    code = 0;
×
912

913
    goto _err;
×
914
  }
915

916
  /*
917
   * iterator all pTdDbc by uid and version
918
   */
919
  TBC *pCtbIdxc = NULL;
2,154✔
920
  code = tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL);
2,154✔
921
  if (code != 0) {
2,154!
922
    goto _err;
×
923
  }
924

925
  code = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c);
2,154✔
926
  if (code < 0) {
2,154!
927
    tdbTbcClose(pCtbIdxc);
×
928
    goto _err;
×
929
  }
930
  for (;;) {
21,009✔
931
    void *pKey = NULL, *pVal = NULL;
23,163✔
932
    int   nKey = 0, nVal = 0;
23,163✔
933

934
    code = tdbTbcNext(pCtbIdxc, &pKey, &nKey, &pVal, &nVal);
23,163✔
935
    if (code < 0) {
23,163✔
936
      tdbFree(pKey);
2,154✔
937
      tdbFree(pVal);
2,154✔
938
      tdbTbcClose(pCtbIdxc);
2,154✔
939
      pCtbIdxc = NULL;
2,154✔
940
      break;
2,154✔
941
    }
942
    if (((SCtbIdxKey *)pKey)->suid != suid) {
21,009✔
943
      tdbFree(pKey);
9,143✔
944
      tdbFree(pVal);
9,143✔
945
      continue;
9,143✔
946
    }
947
    STagIdxKey *pTagIdxKey = NULL;
11,866✔
948
    int32_t     nTagIdxKey;
949

950
    const void *pTagData = NULL;
11,866✔
951
    int32_t     nTagData = 0;
11,866✔
952

953
    SCtbIdxKey *table = (SCtbIdxKey *)pKey;
11,866✔
954
    STagVal     tagVal = {.cid = pCol->colId};
11,866✔
955
    if (tTagGet((const STag *)pVal, &tagVal)) {
11,866!
956
      if (IS_VAR_DATA_TYPE(pCol->type)) {
11,866!
957
        pTagData = tagVal.pData;
3,508✔
958
        nTagData = (int32_t)tagVal.nData;
3,508✔
959
      } else {
960
        pTagData = &(tagVal.i64);
8,358✔
961
        nTagData = tDataTypes[pCol->type].bytes;
8,358✔
962
      }
963
    } else {
964
      if (!IS_VAR_DATA_TYPE(pCol->type)) {
×
965
        nTagData = tDataTypes[pCol->type].bytes;
×
966
      }
967
    }
968

969
    code = metaCreateTagIdxKey(suid, pCol->colId, pTagData, nTagData, pCol->type, table->uid, &pTagIdxKey, &nTagIdxKey);
11,866✔
970
    tdbFree(pKey);
11,866✔
971
    tdbFree(pVal);
11,866✔
972
    if (code < 0) {
11,866!
973
      metaDestroyTagIdxKey(pTagIdxKey);
×
974
      tdbTbcClose(pCtbIdxc);
×
975
      goto _err;
×
976
    }
977

978
    metaWLock(pMeta);
11,866✔
979
    ret = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
11,866✔
980
    if (ret < 0) {
11,866!
981
      metaError("vgId:%d, failed to delete tag idx key:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->stb,
×
982
                pReq->stbUid, tstrerror(ret));
983
    }
984
    metaULock(pMeta);
11,866✔
985
    metaDestroyTagIdxKey(pTagIdxKey);
11,866✔
986
    pTagIdxKey = NULL;
11,866✔
987
  }
988

989
  // clear idx flag
990
  SSCHMEA_SET_IDX_OFF(pCol);
2,154✔
991

992
  nStbEntry.version = version;
2,154✔
993
  nStbEntry.type = TSDB_SUPER_TABLE;
2,154✔
994
  nStbEntry.uid = oStbEntry.uid;
2,154✔
995
  nStbEntry.name = oStbEntry.name;
2,154!
996

997
  SSchemaWrapper  *row = tCloneSSchemaWrapper(&oStbEntry.stbEntry.schemaRow);
2,154!
998
  SSchemaWrapper  *tag = tCloneSSchemaWrapper(&oStbEntry.stbEntry.schemaTag);
2,154!
999
  SColCmprWrapper *cmpr = tCloneSColCmprWrapper(&oStbEntry.colCmpr);
2,154✔
1000
  if (row == NULL || tag == NULL || cmpr == NULL) {
2,154!
1001
    tDeleteSchemaWrapper(row);
1002
    tDeleteSchemaWrapper(tag);
1003
    tDeleteSColCmprWrapper(cmpr);
1004
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1005

1006
    tdbTbcClose(pCtbIdxc);
×
1007
    goto _err;
×
1008
  }
1009

1010
  nStbEntry.stbEntry.schemaRow = *row;
2,154✔
1011
  nStbEntry.stbEntry.schemaTag = *tag;
2,154✔
1012
  nStbEntry.stbEntry.rsmaParam = oStbEntry.stbEntry.rsmaParam;
2,154✔
1013
  nStbEntry.colCmpr = *cmpr;
2,154✔
1014

1015
  nStbEntry.colCmpr = oStbEntry.colCmpr;
2,154✔
1016

1017
  metaWLock(pMeta);
2,154✔
1018
  // update table.db
1019
  ret = metaSaveToTbDb(pMeta, &nStbEntry);
2,154✔
1020
  if (ret < 0) {
2,154!
1021
    metaError("vgId:%d, failed to save tb db:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->stb,
×
1022
              pReq->stbUid, tstrerror(ret));
1023
  }
1024
  // update uid index
1025
  ret = metaUpdateUidIdx(pMeta, &nStbEntry);
2,154✔
1026
  if (ret < 0) {
2,154!
1027
    metaError("vgId:%d, failed to update uid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->stb,
×
1028
              pReq->stbUid, tstrerror(ret));
1029
  }
1030
  metaULock(pMeta);
2,154✔
1031

1032
  tDeleteSchemaWrapper(tag);
1033
  tDeleteSchemaWrapper(row);
1034
  tDeleteSColCmprWrapper(cmpr);
1035

1036
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
2,154!
1037
  tDecoderClear(&dc);
2,154✔
1038
  tdbFree(pData);
2,154✔
1039

1040
  tdbTbcClose(pCtbIdxc);
2,154✔
1041
  return TSDB_CODE_SUCCESS;
2,154✔
1042
_err:
×
1043
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
×
1044
  tDecoderClear(&dc);
×
1045
  tdbFree(pData);
×
1046

1047
  return code;
×
1048
}
1049

1050
int metaCreateTable(SMeta *pMeta, int64_t ver, SVCreateTbReq *pReq, STableMetaRsp **pMetaRsp) {
221,222✔
1051
  SMetaEntry  me = {0};
221,222✔
1052
  SMetaReader mr = {0};
221,222✔
1053
  int32_t     ret;
1054

1055
  // validate message
1056
  if (pReq->type != TSDB_CHILD_TABLE && pReq->type != TSDB_NORMAL_TABLE) {
221,222!
1057
    terrno = TSDB_CODE_INVALID_MSG;
×
1058
    goto _err;
×
1059
  }
1060

1061
  if (pReq->type == TSDB_CHILD_TABLE) {
221,222✔
1062
    tb_uid_t suid = metaGetTableEntryUidByName(pMeta, pReq->ctb.stbName);
206,016✔
1063
    if (suid != pReq->ctb.suid) {
206,013!
1064
      return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
1065
    }
1066
  }
1067

1068
  // validate req
1069
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
221,219✔
1070
  if (metaGetTableEntryByName(&mr, pReq->name) == 0) {
221,213✔
1071
    if (pReq->type == TSDB_CHILD_TABLE && pReq->ctb.suid != mr.me.ctbEntry.suid) {
27,396✔
1072
      metaReaderClear(&mr);
9✔
1073
      return terrno = TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
9✔
1074
    }
1075
    pReq->uid = mr.me.uid;
27,387✔
1076
    if (pReq->type == TSDB_CHILD_TABLE) {
27,387✔
1077
      pReq->ctb.suid = mr.me.ctbEntry.suid;
27,381✔
1078
    }
1079
    metaReaderClear(&mr);
27,387✔
1080
    return terrno = TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
27,387✔
1081
  } else if (terrno == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
193,817!
1082
    terrno = TSDB_CODE_SUCCESS;
193,815✔
1083
  }
1084
  metaReaderClear(&mr);
193,808✔
1085

1086
  bool sysTbl = (pReq->type == TSDB_CHILD_TABLE) && metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1);
193,814!
1087

1088
  if (!sysTbl && ((terrno = grantCheck(TSDB_GRANT_TIMESERIES)) < 0)) goto _err;
193,811!
1089

1090
  // build SMetaEntry
1091
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
193,797✔
1092
  me.version = ver;
193,797✔
1093
  me.type = pReq->type;
193,797✔
1094
  me.uid = pReq->uid;
193,797✔
1095
  me.name = pReq->name;
193,797✔
1096
  if (me.type == TSDB_CHILD_TABLE) {
193,797✔
1097
    me.ctbEntry.btime = pReq->btime;
178,594✔
1098
    me.ctbEntry.ttlDays = pReq->ttl;
178,594✔
1099
    me.ctbEntry.commentLen = pReq->commentLen;
178,594✔
1100
    me.ctbEntry.comment = pReq->comment;
178,594✔
1101
    me.ctbEntry.suid = pReq->ctb.suid;
178,594✔
1102
    me.ctbEntry.pTags = pReq->ctb.pTag;
178,594✔
1103

1104
#ifdef TAG_FILTER_DEBUG
1105
    SArray *pTagVals = NULL;
1106
    int32_t code = tTagToValArray((STag *)pReq->ctb.pTag, &pTagVals);
1107
    for (int i = 0; i < taosArrayGetSize(pTagVals); i++) {
1108
      STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
1109

1110
      if (IS_VAR_DATA_TYPE(pTagVal->type)) {
1111
        char *buf = taosMemoryCalloc(pTagVal->nData + 1, 1);
1112
        memcpy(buf, pTagVal->pData, pTagVal->nData);
1113
        metaDebug("metaTag table:%s varchar index:%d cid:%d type:%d value:%s", pReq->name, i, pTagVal->cid,
1114
                  pTagVal->type, buf);
1115
        taosMemoryFree(buf);
1116
      } else {
1117
        double val = 0;
1118
        GET_TYPED_DATA(val, double, pTagVal->type, &pTagVal->i64);
1119
        metaDebug("metaTag table:%s number index:%d cid:%d type:%d value:%f", pReq->name, i, pTagVal->cid,
1120
                  pTagVal->type, val);
1121
      }
1122
    }
1123
#endif
1124

1125
    ++pStats->numOfCTables;
178,594✔
1126

1127
    if (!sysTbl) {
178,594!
1128
      int32_t nCols = 0;
178,594✔
1129
      ret = metaGetStbStats(pMeta->pVnode, me.ctbEntry.suid, 0, &nCols);
178,594✔
1130
      if (ret < 0) {
178,621!
1131
        metaError("vgId:%d, failed to get stb stats:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
1132
                  pReq->ctb.suid, tstrerror(ret));
1133
      }
1134
      pStats->numOfTimeSeries += nCols - 1;
178,614✔
1135
    }
1136

1137
    metaWLock(pMeta);
178,614✔
1138
    metaUpdateStbStats(pMeta, me.ctbEntry.suid, 1, 0);
178,616✔
1139
    ret = metaUidCacheClear(pMeta, me.ctbEntry.suid);
178,589✔
1140
    if (ret < 0) {
178,622!
1141
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
1142
                pReq->ctb.suid, tstrerror(ret));
1143
    }
1144
    ret = metaTbGroupCacheClear(pMeta, me.ctbEntry.suid);
178,622✔
1145
    if (ret < 0) {
178,624!
1146
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
1147
                pReq->ctb.suid, tstrerror(ret));
1148
    }
1149
    metaULock(pMeta);
178,624✔
1150

1151
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
178,626✔
1152
      ret = tsdbCacheNewTable(pMeta->pVnode->pTsdb, me.uid, me.ctbEntry.suid, NULL);
960✔
1153
      if (ret < 0) {
960!
1154
        metaError("vgId:%d, failed to create table:%s since %s", TD_VID(pMeta->pVnode), pReq->name, tstrerror(ret));
×
1155
        goto _err;
×
1156
      }
1157
    }
1158
  } else {
1159
    me.ntbEntry.btime = pReq->btime;
15,203✔
1160
    me.ntbEntry.ttlDays = pReq->ttl;
15,203✔
1161
    me.ntbEntry.commentLen = pReq->commentLen;
15,203✔
1162
    me.ntbEntry.comment = pReq->comment;
15,203✔
1163
    me.ntbEntry.schemaRow = pReq->ntb.schemaRow;
15,203✔
1164
    me.ntbEntry.ncid = me.ntbEntry.schemaRow.pSchema[me.ntbEntry.schemaRow.nCols - 1].colId + 1;
15,203✔
1165
    me.colCmpr = pReq->colCmpr;
15,203✔
1166
    TABLE_SET_COL_COMPRESSED(me.flags);
15,203✔
1167

1168
    ++pStats->numOfNTables;
15,203✔
1169
    pStats->numOfNTimeSeries += me.ntbEntry.schemaRow.nCols - 1;
15,203✔
1170

1171
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
15,203✔
1172
      ret = tsdbCacheNewTable(pMeta->pVnode->pTsdb, me.uid, -1, &me.ntbEntry.schemaRow);
42✔
1173
      if (ret < 0) {
42!
1174
        metaError("vgId:%d, failed to create table:%s since %s", TD_VID(pMeta->pVnode), pReq->name, tstrerror(ret));
×
1175
        goto _err;
×
1176
      }
1177
    }
1178
  }
1179

1180
  if (metaHandleEntry(pMeta, &me) < 0) goto _err;
193,829!
1181

1182
  metaTimeSeriesNotifyCheck(pMeta);
193,802✔
1183

1184
  if (pMetaRsp) {
193,828!
1185
    *pMetaRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
193,830✔
1186

1187
    if (*pMetaRsp) {
193,820!
1188
      if (me.type == TSDB_CHILD_TABLE) {
193,824✔
1189
        (*pMetaRsp)->tableType = TSDB_CHILD_TABLE;
178,620✔
1190
        (*pMetaRsp)->tuid = pReq->uid;
178,620✔
1191
        (*pMetaRsp)->suid = pReq->ctb.suid;
178,620✔
1192
        strcpy((*pMetaRsp)->tbName, pReq->name);
178,620✔
1193
      } else {
1194
        ret = metaUpdateMetaRsp(pReq->uid, pReq->name, &pReq->ntb.schemaRow, *pMetaRsp);
15,204✔
1195
        if (ret < 0) {
15,204!
1196
          metaError("vgId:%d, failed to update meta rsp:%s since %s", TD_VID(pMeta->pVnode), pReq->name,
×
1197
                    tstrerror(ret));
1198
        }
1199
        for (int32_t i = 0; i < pReq->colCmpr.nCols; i++) {
275,419✔
1200
          SColCmpr *p = &pReq->colCmpr.pColCmpr[i];
260,215✔
1201
          (*pMetaRsp)->pSchemaExt[i].colId = p->id;
260,215✔
1202
          (*pMetaRsp)->pSchemaExt[i].compress = p->alg;
260,215✔
1203
        }
1204
      }
1205
    }
1206
  }
1207

1208
  pMeta->changed = true;
193,818✔
1209
  metaDebug("vgId:%d, table:%s uid %" PRId64 " is created, type:%" PRId8, TD_VID(pMeta->pVnode), pReq->name, pReq->uid,
193,818✔
1210
            pReq->type);
1211
  return 0;
193,825✔
1212

1213
_err:
×
1214
  metaError("vgId:%d, failed to create table:%s type:%s since %s", TD_VID(pMeta->pVnode), pReq->name,
×
1215
            pReq->type == TSDB_CHILD_TABLE ? "child table" : "normal table", tstrerror(terrno));
1216
  return TSDB_CODE_FAILED;
×
1217
}
1218

1219
int metaDropTable(SMeta *pMeta, int64_t version, SVDropTbReq *pReq, SArray *tbUids, tb_uid_t *tbUid) {
16,124✔
1220
  void    *pData = NULL;
16,124✔
1221
  int      nData = 0;
16,124✔
1222
  int      rc = 0;
16,124✔
1223
  tb_uid_t uid = 0;
16,124✔
1224
  tb_uid_t suid = 0;
16,124✔
1225
  int8_t   sysTbl = 0;
16,124✔
1226
  int      type;
1227

1228
  rc = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &pData, &nData);
16,124✔
1229
  if (rc < 0) {
16,124!
1230
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1231
  }
1232
  uid = *(tb_uid_t *)pData;
16,124✔
1233

1234
  metaWLock(pMeta);
16,124✔
1235
  rc = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
16,124✔
1236
  metaULock(pMeta);
16,124✔
1237

1238
  if (rc < 0) goto _exit;
16,124!
1239

1240
  if (!sysTbl && type == TSDB_CHILD_TABLE) {
16,124!
1241
    int32_t      nCols = 0;
13,761✔
1242
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
13,761✔
1243
    if (metaGetStbStats(pMeta->pVnode, suid, NULL, &nCols) == 0) {
13,761!
1244
      pStats->numOfTimeSeries -= nCols - 1;
13,761✔
1245
    }
1246
  }
1247

1248
  if ((type == TSDB_CHILD_TABLE || type == TSDB_NORMAL_TABLE) && tbUids) {
16,124!
1249
    if (taosArrayPush(tbUids, &uid) == NULL) {
16,124!
1250
      rc = terrno;
×
1251
      goto _exit;
×
1252
    }
1253

1254
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
16,124✔
1255
      int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
75✔
1256
      if (ret < 0) {
75!
1257
        metaError("vgId:%d, failed to drop table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, uid,
×
1258
                  tstrerror(ret));
1259
      }
1260
    }
1261
  }
1262

1263
  if ((type == TSDB_CHILD_TABLE) && tbUid) {
16,124!
1264
    *tbUid = uid;
13,761✔
1265
  }
1266

1267
  pMeta->changed = true;
16,124✔
1268
_exit:
16,124✔
1269
  tdbFree(pData);
16,124✔
1270
  return rc;
16,124✔
1271
}
1272

1273
int32_t metaDropTables(SMeta *pMeta, SArray *tbUids) {
64✔
1274
  int32_t code = 0;
64✔
1275
  if (taosArrayGetSize(tbUids) == 0) return TSDB_CODE_SUCCESS;
64!
1276

1277
  int64_t    nCtbDropped = 0;
64✔
1278
  SSHashObj *suidHash = tSimpleHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
64✔
1279
  if (suidHash == NULL) {
64!
1280
    return terrno;
×
1281
  }
1282

1283
  metaWLock(pMeta);
64✔
1284
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
5,334✔
1285
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
5,270✔
1286
    tb_uid_t suid = 0;
5,270✔
1287
    int8_t   sysTbl = 0;
5,270✔
1288
    int      type;
1289
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
5,270✔
1290
    if (code) return code;
5,270!
1291
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
5,270!
1292
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
5,258✔
1293
      if (pVal) {
5,258✔
1294
        nCtbDropped = *pVal + 1;
5,199✔
1295
      } else {
1296
        nCtbDropped = 1;
59✔
1297
      }
1298
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
5,258✔
1299
      if (code) return code;
5,258!
1300
    }
1301
    /*
1302
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
1303
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
1304
    }
1305
    */
1306
    metaDebug("batch drop table:%" PRId64, uid);
5,270✔
1307
  }
1308
  metaULock(pMeta);
64✔
1309

1310
  // update timeseries
1311
  void   *pCtbDropped = NULL;
64✔
1312
  int32_t iter = 0;
64✔
1313
  while ((pCtbDropped = tSimpleHashIterate(suidHash, pCtbDropped, &iter))) {
123✔
1314
    tb_uid_t    *pSuid = tSimpleHashGetKey(pCtbDropped, NULL);
59✔
1315
    int32_t      nCols = 0;
59✔
1316
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
59✔
1317
    if (metaGetStbStats(pMeta->pVnode, *pSuid, NULL, &nCols) == 0) {
59!
1318
      pStats->numOfTimeSeries -= *(int64_t *)pCtbDropped * (nCols - 1);
59✔
1319
    }
1320
  }
1321
  tSimpleHashCleanup(suidHash);
64✔
1322

1323
  pMeta->changed = true;
64✔
1324
  return 0;
64✔
1325
}
1326

1327
static int32_t metaFilterTableByHash(SMeta *pMeta, SArray *uidList) {
86✔
1328
  int32_t code = 0;
86✔
1329
  // 1, tranverse table's
1330
  // 2, validate table name using vnodeValidateTableHash
1331
  // 3, push invalidated table's uid into uidList
1332

1333
  TBC *pCur;
1334
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
86✔
1335
  if (code < 0) {
86!
1336
    return code;
×
1337
  }
1338

1339
  code = tdbTbcMoveToFirst(pCur);
86✔
1340
  if (code) {
86!
1341
    tdbTbcClose(pCur);
×
1342
    return code;
×
1343
  }
1344

1345
  void *pData = NULL, *pKey = NULL;
86✔
1346
  int   nData = 0, nKey = 0;
86✔
1347

1348
  while (1) {
10,612✔
1349
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
10,698✔
1350
    if (ret < 0) {
10,698✔
1351
      break;
86✔
1352
    }
1353

1354
    SMetaEntry me = {0};
10,612✔
1355
    SDecoder   dc = {0};
10,612✔
1356
    tDecoderInit(&dc, pData, nData);
10,612✔
1357
    code = metaDecodeEntry(&dc, &me);
10,612✔
1358
    if (code < 0) {
10,612!
1359
      tDecoderClear(&dc);
×
1360
      return code;
×
1361
    }
1362

1363
    if (me.type != TSDB_SUPER_TABLE) {
10,612✔
1364
      char tbFName[TSDB_TABLE_FNAME_LEN + 1];
1365
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pMeta->pVnode->config.dbname, me.name);
10,540✔
1366
      tbFName[TSDB_TABLE_FNAME_LEN] = '\0';
10,540✔
1367
      int32_t ret = vnodeValidateTableHash(pMeta->pVnode, tbFName);
10,540✔
1368
      if (ret < 0 && terrno == TSDB_CODE_VND_HASH_MISMATCH) {
10,540!
1369
        if (taosArrayPush(uidList, &me.uid) == NULL) {
5,270!
1370
          code = terrno;
×
1371
          break;
×
1372
        }
1373
      }
1374
    }
1375
    tDecoderClear(&dc);
10,612✔
1376
  }
1377
  tdbFree(pData);
86✔
1378
  tdbFree(pKey);
86✔
1379
  tdbTbcClose(pCur);
86✔
1380

1381
  return 0;
86✔
1382
}
1383

1384
int32_t metaTrimTables(SMeta *pMeta) {
86✔
1385
  int32_t code = 0;
86✔
1386

1387
  SArray *tbUids = taosArrayInit(8, sizeof(int64_t));
86✔
1388
  if (tbUids == NULL) {
86!
1389
    return terrno;
×
1390
  }
1391

1392
  code = metaFilterTableByHash(pMeta, tbUids);
86✔
1393
  if (code != 0) {
86!
1394
    goto end;
×
1395
  }
1396
  if (TARRAY_SIZE(tbUids) == 0) {
86✔
1397
    goto end;
22✔
1398
  }
1399

1400
  metaInfo("vgId:%d, trim %ld tables", TD_VID(pMeta->pVnode), taosArrayGetSize(tbUids));
64!
1401
  code = metaDropTables(pMeta, tbUids);
64✔
1402
  if (code) goto end;
64!
1403

1404
end:
64✔
1405
  taosArrayDestroy(tbUids);
86✔
1406

1407
  return code;
86✔
1408
}
1409

1410
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
98,614✔
1411
  metaRLock(pMeta);
98,614✔
1412

1413
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
98,675✔
1414

1415
  metaULock(pMeta);
98,463✔
1416

1417
  if (ret != 0) {
98,581!
1418
    metaError("ttl failed to find expired table, ret:%d", ret);
×
1419
  }
1420

1421
  return ret;
98,537✔
1422
}
1423

1424
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
287,601✔
1425
  int64_t btime;
1426
  if (pME->type == TSDB_CHILD_TABLE) {
287,601✔
1427
    btime = pME->ctbEntry.btime;
239,333✔
1428
  } else if (pME->type == TSDB_NORMAL_TABLE) {
48,268✔
1429
    btime = pME->ntbEntry.btime;
17,589✔
1430
  } else {
1431
    return TSDB_CODE_FAILED;
30,679✔
1432
  }
1433

1434
  btimeKey->btime = btime;
256,922✔
1435
  btimeKey->uid = pME->uid;
256,922✔
1436
  return 0;
256,922✔
1437
}
1438

1439
static int metaBuildNColIdxKey(SNcolIdxKey *ncolKey, const SMetaEntry *pME) {
19,673✔
1440
  if (pME->type == TSDB_NORMAL_TABLE) {
19,673!
1441
    ncolKey->ncol = pME->ntbEntry.schemaRow.nCols;
19,673✔
1442
    ncolKey->uid = pME->uid;
19,673✔
1443
  } else {
1444
    return TSDB_CODE_FAILED;
×
1445
  }
1446
  return 0;
19,673✔
1447
}
1448

1449
static void metaDeleteTtl(SMeta *pMeta, const SMetaEntry *pME) {
25,611✔
1450
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
25,611!
1451

1452
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
25,611✔
1453
  if (pME->type == TSDB_CHILD_TABLE) {
25,611✔
1454
    ctx.ttlDays = pME->ctbEntry.ttlDays;
23,214✔
1455
  } else {
1456
    ctx.ttlDays = pME->ntbEntry.ttlDays;
2,397✔
1457
  }
1458

1459
  int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
25,611✔
1460
  if (ret < 0) {
25,610!
1461
    metaError("vgId:%d, failed to delete ttl for table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pME->name,
×
1462
              pME->uid, tstrerror(ret));
1463
  }
1464
  return;
25,611✔
1465
}
1466

1467
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl) {
25,562✔
1468
  void      *pData = NULL;
25,562✔
1469
  int        nData = 0;
25,562✔
1470
  int        rc = 0;
25,562✔
1471
  SMetaEntry e = {0};
25,562✔
1472
  SDecoder   dc = {0};
25,562✔
1473
  int32_t    ret = 0;
25,562✔
1474

1475
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
25,562✔
1476
  if (rc < 0) {
25,567!
1477
    return rc;
×
1478
  }
1479
  int64_t version = ((SUidIdxVal *)pData)[0].version;
25,567✔
1480

1481
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
25,567✔
1482
  if (rc < 0) {
25,564!
1483
    tdbFree(pData);
×
1484
    return rc;
×
1485
  }
1486

1487
  tDecoderInit(&dc, pData, nData);
25,564✔
1488
  rc = metaDecodeEntry(&dc, &e);
25,564✔
1489
  if (rc < 0) {
25,567!
1490
    tDecoderClear(&dc);
×
1491
    return rc;
×
1492
  }
1493

1494
  if (type) *type = e.type;
25,567✔
1495

1496
  if (e.type == TSDB_CHILD_TABLE) {
25,567✔
1497
    if (pSuid) *pSuid = e.ctbEntry.suid;
23,192✔
1498
    void *tData = NULL;
23,192✔
1499
    int   tLen = 0;
23,192✔
1500

1501
    if (tdbTbGet(pMeta->pUidIdx, &e.ctbEntry.suid, sizeof(tb_uid_t), &tData, &tLen) == 0) {
23,192!
1502
      STbDbKey tbDbKey = {.uid = e.ctbEntry.suid, .version = ((SUidIdxVal *)tData)[0].version};
23,192✔
1503
      if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &tData, &tLen) == 0) {
23,192!
1504
        SDecoder   tdc = {0};
23,192✔
1505
        SMetaEntry stbEntry = {0};
23,192✔
1506

1507
        tDecoderInit(&tdc, tData, tLen);
23,192✔
1508
        int32_t ret = metaDecodeEntry(&tdc, &stbEntry);
23,192✔
1509
        if (ret < 0) {
23,191!
1510
          tDecoderClear(&tdc);
×
1511
          metaError("vgId:%d, failed to decode child table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
1512
                    e.ctbEntry.suid, tstrerror(ret));
1513
          return ret;
×
1514
        }
1515

1516
        if (pSysTbl) *pSysTbl = metaTbInFilterCache(pMeta, stbEntry.name, 1) ? 1 : 0;
23,191✔
1517

1518
        SSchema        *pTagColumn = NULL;
23,191✔
1519
        SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
23,191✔
1520
        if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
23,191✔
1521
          pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
196✔
1522
          ret = metaDelJsonVarFromIdx(pMeta, &e, pTagColumn);
196✔
1523
          if (ret < 0) {
196!
1524
            metaError("vgId:%d, failed to delete json var from idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
1525
                      e.name, e.uid, tstrerror(ret));
1526
          }
1527
        } else {
1528
          for (int i = 0; i < pTagSchema->nCols; i++) {
88,632✔
1529
            pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[i];
65,637✔
1530
            if (!IS_IDX_ON(pTagColumn)) continue;
65,637✔
1531
            STagIdxKey *pTagIdxKey = NULL;
27,953✔
1532
            int32_t     nTagIdxKey;
1533

1534
            const void *pTagData = NULL;
27,953✔
1535
            int32_t     nTagData = 0;
27,953✔
1536

1537
            STagVal tagVal = {.cid = pTagColumn->colId};
27,953✔
1538
            if (tTagGet((const STag *)e.ctbEntry.pTags, &tagVal)) {
27,953✔
1539
              if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
27,226!
1540
                pTagData = tagVal.pData;
6,609✔
1541
                nTagData = (int32_t)tagVal.nData;
6,609✔
1542
              } else {
1543
                pTagData = &(tagVal.i64);
20,617✔
1544
                nTagData = tDataTypes[pTagColumn->type].bytes;
20,617✔
1545
              }
1546
            } else {
1547
              if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
728!
1548
                nTagData = tDataTypes[pTagColumn->type].bytes;
592✔
1549
              }
1550
            }
1551

1552
            if (metaCreateTagIdxKey(e.ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type, uid,
27,954!
1553
                                    &pTagIdxKey, &nTagIdxKey) == 0) {
1554
              ret = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
27,953✔
1555
              if (ret < 0) {
27,954!
1556
                metaError("vgId:%d, failed to delete tag idx key:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
1557
                          e.name, e.uid, tstrerror(ret));
1558
              }
1559
            }
1560
            metaDestroyTagIdxKey(pTagIdxKey);
27,954✔
1561
            pTagIdxKey = NULL;
27,953✔
1562
          }
1563
        }
1564
        tDecoderClear(&tdc);
23,191✔
1565
      }
1566
      tdbFree(tData);
23,192✔
1567
    }
1568
  }
1569

1570
  ret = tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), pMeta->txn);
25,568✔
1571
  if (ret < 0) {
25,565!
1572
    metaError("vgId:%d, failed to delete table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
1573
              tstrerror(ret));
1574
  }
1575
  ret = tdbTbDelete(pMeta->pNameIdx, e.name, strlen(e.name) + 1, pMeta->txn);
25,565✔
1576
  if (ret < 0) {
25,563!
1577
    metaError("vgId:%d, failed to delete name idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
1578
              tstrerror(ret));
1579
  }
1580
  ret = tdbTbDelete(pMeta->pUidIdx, &uid, sizeof(uid), pMeta->txn);
25,563✔
1581
  if (ret < 0) {
25,565!
1582
    metaError("vgId:%d, failed to delete uid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
1583
              tstrerror(ret));
1584
  }
1585

1586
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
25,565!
1587
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
25,563✔
1588

1589
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
25,563!
1590

1591
  if (e.type == TSDB_CHILD_TABLE) {
25,564✔
1592
    ret =
1593
        tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
23,189✔
1594
    if (ret < 0) {
23,188!
1595
      metaError("vgId:%d, failed to delete ctb idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
1596
                tstrerror(ret));
1597
    }
1598

1599
    --pMeta->pVnode->config.vndStats.numOfCTables;
23,188✔
1600
    metaUpdateStbStats(pMeta, e.ctbEntry.suid, -1, 0);
23,188✔
1601
    ret = metaUidCacheClear(pMeta, e.ctbEntry.suid);
23,188✔
1602
    if (ret < 0) {
23,187!
1603
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
1604
                e.ctbEntry.suid, tstrerror(ret));
1605
    }
1606
    ret = metaTbGroupCacheClear(pMeta, e.ctbEntry.suid);
23,187✔
1607
    if (ret < 0) {
23,190!
1608
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
1609
                e.ctbEntry.suid, tstrerror(ret));
1610
    }
1611
    /*
1612
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
1613
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, e.ctbEntry.suid, NULL);
1614
    }
1615
    */
1616
  } else if (e.type == TSDB_NORMAL_TABLE) {
2,375!
1617
    // drop schema.db (todo)
1618

1619
    --pMeta->pVnode->config.vndStats.numOfNTables;
2,375✔
1620
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
2,375✔
1621

1622
    /*
1623
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
1624
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, -1, &e.ntbEntry.schemaRow);
1625
    }
1626
    */
1627
  } else if (e.type == TSDB_SUPER_TABLE) {
×
1628
    ret = tdbTbDelete(pMeta->pSuidIdx, &e.uid, sizeof(tb_uid_t), pMeta->txn);
×
1629
    if (ret < 0) {
×
1630
      metaError("vgId:%d, failed to delete suid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
1631
                tstrerror(ret));
1632
    }
1633
    // drop schema.db (todo)
1634

1635
    ret = metaStatsCacheDrop(pMeta, uid);
×
1636
    if (ret < 0) {
×
1637
      metaError("vgId:%d, failed to drop stats cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
1638
                tstrerror(ret));
1639
    }
1640
    ret = metaUidCacheClear(pMeta, uid);
×
1641
    if (ret < 0) {
×
1642
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
1643
                tstrerror(ret));
1644
    }
1645
    ret = metaTbGroupCacheClear(pMeta, uid);
×
1646
    if (ret < 0) {
×
1647
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
1648
                e.uid, tstrerror(ret));
1649
    }
1650
    --pMeta->pVnode->config.vndStats.numOfSTables;
×
1651
  }
1652

1653
  ret = metaCacheDrop(pMeta, uid);
25,566✔
1654
  if (ret < 0) {
25,566✔
1655
    metaError("vgId:%d, failed to drop cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
5,275!
1656
              tstrerror(ret));
1657
  }
1658

1659
  tDecoderClear(&dc);
25,566✔
1660
  tdbFree(pData);
25,563✔
1661

1662
  return 0;
25,565✔
1663
}
1664
// opt ins_tables
1665
int metaUpdateBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
262,192✔
1666
  SBtimeIdxKey btimeKey = {0};
262,192✔
1667
  if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) {
262,192✔
1668
    return 0;
30,989✔
1669
  }
1670
  metaTrace("vgId:%d, start to save version:%" PRId64 " uid:%" PRId64 " btime:%" PRId64, TD_VID(pMeta->pVnode),
231,331✔
1671
            pME->version, pME->uid, btimeKey.btime);
1672

1673
  return tdbTbUpsert(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), NULL, 0, pMeta->txn);
231,332✔
1674
}
1675

1676
int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
25,565✔
1677
  SBtimeIdxKey btimeKey = {0};
25,565✔
1678
  if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) {
25,565!
1679
    return 0;
×
1680
  }
1681
  return tdbTbDelete(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), pMeta->txn);
25,564✔
1682
}
1683
int metaUpdateNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
16,256✔
1684
  SNcolIdxKey ncolKey = {0};
16,256✔
1685
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
16,256!
1686
    return 0;
×
1687
  }
1688
  return tdbTbUpsert(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), NULL, 0, pMeta->txn);
16,256✔
1689
}
1690

1691
int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
3,417✔
1692
  SNcolIdxKey ncolKey = {0};
3,417✔
1693
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
3,417!
1694
    return 0;
×
1695
  }
1696
  return tdbTbDelete(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), pMeta->txn);
3,417✔
1697
}
1698

1699
static int metaAlterTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq, STableMetaRsp *pMetaRsp) {
1,971✔
1700
  void           *pVal = NULL;
1,971✔
1701
  int             nVal = 0;
1,971✔
1702
  const void     *pData = NULL;
1,971✔
1703
  int             nData = 0;
1,971✔
1704
  int             ret = 0;
1,971✔
1705
  tb_uid_t        uid;
1706
  int64_t         oversion;
1707
  SSchema        *pColumn = NULL;
1,971✔
1708
  SMetaEntry      entry = {0};
1,971✔
1709
  SSchemaWrapper *pSchema;
1710
  int             c;
1711
  bool            freeColCmpr = false;
1,971✔
1712
  if (pAlterTbReq->colName == NULL) {
1,971!
1713
    metaError("meta/table: null pAlterTbReq->colName");
×
1714
    return terrno = TSDB_CODE_INVALID_MSG;
×
1715
  }
1716

1717
  // search name index
1718
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
1,971✔
1719
  if (ret < 0) {
1,971!
1720
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1721
  }
1722

1723
  uid = *(tb_uid_t *)pVal;
1,971✔
1724
  tdbFree(pVal);
1,971✔
1725
  pVal = NULL;
1,971✔
1726

1727
  // search uid index
1728
  TBC *pUidIdxc = NULL;
1,971✔
1729

1730
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, NULL));
1,971!
1731
  ret = tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c);
1,971✔
1732
  if (c != 0) {
1,971!
1733
    tdbTbcClose(pUidIdxc);
×
1734
    metaError("meta/table: invalide c: %" PRId32 " alt tb column failed.", c);
×
1735
    return TSDB_CODE_FAILED;
×
1736
  }
1737

1738
  ret = tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
1,971✔
1739
  oversion = ((SUidIdxVal *)pData)[0].version;
1,971✔
1740

1741
  // search table.db
1742
  TBC *pTbDbc = NULL;
1,971✔
1743

1744
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pTbDb, &pTbDbc, NULL));
1,971!
1745
  ret = tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c);
1,971✔
1746
  if (c != 0) {
1,971!
1747
    tdbTbcClose(pUidIdxc);
×
1748
    tdbTbcClose(pTbDbc);
×
1749
    metaError("meta/table: invalide c: %" PRId32 " alt tb column failed.", c);
×
1750
    return TSDB_CODE_FAILED;
×
1751
  }
1752

1753
  ret = tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);
1,971✔
1754

1755
  // get table entry
1756
  SDecoder dc = {0};
1,971✔
1757
  if ((entry.pBuf = taosMemoryMalloc(nData)) == NULL) {
1,971!
1758
    tdbTbcClose(pUidIdxc);
×
1759
    tdbTbcClose(pTbDbc);
×
1760
    return terrno;
×
1761
  }
1762
  memcpy(entry.pBuf, pData, nData);
1,971✔
1763
  tDecoderInit(&dc, entry.pBuf, nData);
1,971✔
1764
  ret = metaDecodeEntry(&dc, &entry);
1,971✔
1765
  if (ret != 0) {
1,971!
1766
    tdbTbcClose(pUidIdxc);
×
1767
    tdbTbcClose(pTbDbc);
×
1768
    tDecoderClear(&dc);
×
1769
    metaError("meta/table: invalide ret: %" PRId32 " alt tb column failed.", ret);
×
1770
    return ret;
×
1771
  }
1772

1773
  if (entry.type != TSDB_NORMAL_TABLE) {
1,971✔
1774
    terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
15✔
1775
    goto _err;
15✔
1776
  }
1777
  // search the column to add/drop/update
1778
  pSchema = &entry.ntbEntry.schemaRow;
1,956✔
1779

1780
  // save old entry
1781
  SMetaEntry oldEntry = {.type = TSDB_NORMAL_TABLE, .uid = entry.uid};
1,956✔
1782
  oldEntry.ntbEntry.schemaRow.nCols = pSchema->nCols;
1,956✔
1783

1784
  int32_t rowLen = -1;
1,956✔
1785
  if (pAlterTbReq->action == TSDB_ALTER_TABLE_ADD_COLUMN ||
1,956✔
1786
      pAlterTbReq->action == TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES) {
870✔
1787
    rowLen = 0;
1,832✔
1788
  }
1789

1790
  int32_t  iCol = 0, jCol = 0;
1,956✔
1791
  SSchema *qColumn = NULL;
1,956✔
1792
  for (;;) {
1793
    qColumn = NULL;
100,717✔
1794

1795
    if (jCol >= pSchema->nCols) break;
100,717✔
1796
    qColumn = &pSchema->pSchema[jCol];
98,762✔
1797

1798
    if (!pColumn && (strcmp(qColumn->name, pAlterTbReq->colName) == 0)) {
98,762✔
1799
      pColumn = qColumn;
1,364✔
1800
      iCol = jCol;
1,364✔
1801
      if (rowLen < 0) break;
1,364✔
1802
    }
1803
    rowLen += qColumn->bytes;
98,761✔
1804
    ++jCol;
98,761✔
1805
  }
1806

1807
  entry.version = version;
1,956✔
1808
  int      tlen;
1809
  SSchema *pNewSchema = NULL;
1,956✔
1810
  SSchema  tScheam;
1811
  switch (pAlterTbReq->action) {
1,956!
1812
    case TSDB_ALTER_TABLE_ADD_COLUMN:
1,087✔
1813
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
1814
      if (pColumn) {
1,087✔
1815
        terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
495✔
1816
        goto _err;
495✔
1817
      }
1818
      if ((terrno = grantCheck(TSDB_GRANT_TIMESERIES)) < 0) {
592!
1819
        goto _err;
×
1820
      }
1821
      if (rowLen + pAlterTbReq->bytes > TSDB_MAX_BYTES_PER_ROW) {
592✔
1822
        terrno = TSDB_CODE_PAR_INVALID_ROW_LENGTH;
10✔
1823
        goto _err;
10✔
1824
      }
1825
      pSchema->version++;
582✔
1826
      pSchema->nCols++;
582✔
1827
      pNewSchema = taosMemoryMalloc(sizeof(SSchema) * pSchema->nCols);
582✔
1828
      if (pNewSchema == NULL) {
582!
1829
        goto _err;
×
1830
      }
1831
      memcpy(pNewSchema, pSchema->pSchema, sizeof(SSchema) * (pSchema->nCols - 1));
582✔
1832
      pSchema->pSchema = pNewSchema;
582✔
1833
      pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].bytes = pAlterTbReq->bytes;
582✔
1834
      pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].type = pAlterTbReq->type;
582✔
1835
      pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].flags = pAlterTbReq->flags;
582✔
1836
      pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].colId = entry.ntbEntry.ncid++;
582✔
1837
      strcpy(pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].name, pAlterTbReq->colName);
582✔
1838

1839
      ++pMeta->pVnode->config.vndStats.numOfNTimeSeries;
582✔
1840
      metaTimeSeriesNotifyCheck(pMeta);
582✔
1841

1842
      if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
582✔
1843
        int16_t cid = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].colId;
3✔
1844
        int8_t  col_type = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].type;
3✔
1845
        int32_t ret = tsdbCacheNewNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, col_type);
3✔
1846
        if (ret < 0) {
3!
1847
          terrno = ret;
×
1848
          goto _err;
×
1849
        }
1850
      }
1851
      SSchema *pCol = &pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1];
582✔
1852
      uint32_t compress = pAlterTbReq->action == TSDB_ALTER_TABLE_ADD_COLUMN ? createDefaultColCmprByType(pCol->type)
581✔
1853
                                                                             : pAlterTbReq->compress;
582✔
1854
      if (updataTableColCmpr(&entry.colCmpr, pCol, 1, compress) != 0) {
582!
1855
        metaError("vgId:%d, failed to update table col cmpr:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name,
×
1856
                  entry.uid);
1857
      }
1858
      freeColCmpr = true;
582✔
1859
      if (entry.colCmpr.nCols != pSchema->nCols) {
582!
1860
        if (pNewSchema) taosMemoryFree(pNewSchema);
×
1861
        if (freeColCmpr) taosMemoryFree(entry.colCmpr.pColCmpr);
×
1862
        terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
1863
        goto _err;
×
1864
      }
1865
      break;
582✔
1866
    case TSDB_ALTER_TABLE_DROP_COLUMN:
79✔
1867
      if (pColumn == NULL) {
79!
1868
        terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
×
1869
        goto _err;
×
1870
      }
1871
      if (pColumn->colId == 0) {
79!
1872
        terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
1873
        goto _err;
×
1874
      }
1875
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
79✔
1876
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
11✔
1877
        goto _err;
11✔
1878
      }
1879
      bool hasPrimayKey = false;
68✔
1880
      if (pSchema->nCols >= 2) {
68!
1881
        hasPrimayKey = pSchema->pSchema[1].flags & COL_IS_KEY ? true : false;
68✔
1882
      }
1883

1884
      memcpy(&tScheam, pColumn, sizeof(SSchema));
68✔
1885
      pSchema->version++;
68✔
1886
      tlen = (pSchema->nCols - iCol - 1) * sizeof(SSchema);
68✔
1887
      if (tlen) {
68✔
1888
        memmove(pColumn, pColumn + 1, tlen);
35✔
1889
      }
1890
      pSchema->nCols--;
68✔
1891

1892
      --pMeta->pVnode->config.vndStats.numOfNTimeSeries;
68✔
1893

1894
      if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
68✔
1895
        int16_t cid = pColumn->colId;
3✔
1896

1897
        if (tsdbCacheDropNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, hasPrimayKey) != 0) {
3!
1898
          metaError("vgId:%d, failed to drop ntable column:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name,
×
1899
                    entry.uid);
1900
        }
1901
      }
1902

1903
      if (updataTableColCmpr(&entry.colCmpr, &tScheam, 0, 0) != 0) {
68!
1904
        metaError("vgId:%d, failed to update table col cmpr:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name,
×
1905
                  entry.uid);
1906
      }
1907
      if (entry.colCmpr.nCols != pSchema->nCols) {
68!
1908
        terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
1909
        goto _err;
×
1910
      }
1911
      break;
68✔
1912
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
746✔
1913
      if (pColumn == NULL) {
746!
1914
        terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
×
1915
        goto _err;
×
1916
      }
1917
      if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes >= pAlterTbReq->colModBytes) {
746!
1918
        terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
304✔
1919
        goto _err;
304✔
1920
      }
1921
      if (rowLen + pAlterTbReq->colModBytes - pColumn->bytes > TSDB_MAX_BYTES_PER_ROW) {
442✔
1922
        terrno = TSDB_CODE_PAR_INVALID_ROW_LENGTH;
70✔
1923
        goto _err;
70✔
1924
      }
1925
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
372✔
1926
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
13✔
1927
        goto _err;
13✔
1928
      }
1929
      pSchema->version++;
359✔
1930
      pColumn->bytes = pAlterTbReq->colModBytes;
359✔
1931
      break;
359✔
1932
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
44✔
1933
      if (pAlterTbReq->colNewName == NULL) {
44!
1934
        terrno = TSDB_CODE_INVALID_MSG;
×
1935
        goto _err;
×
1936
      }
1937
      if (pColumn == NULL) {
44!
1938
        terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
×
1939
        goto _err;
×
1940
      }
1941
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
44✔
1942
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
11✔
1943
        goto _err;
11✔
1944
      }
1945
      pSchema->version++;
33✔
1946
      strcpy(pColumn->name, pAlterTbReq->colNewName);
33✔
1947
      break;
33✔
1948
  }
1949

1950
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
1,042✔
1951
    tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, entry.uid, pSchema->version);
6✔
1952
  }
1953

1954
  entry.version = version;
1,042✔
1955

1956
  // do actual write
1957
  metaWLock(pMeta);
1,042✔
1958

1959
  if (metaDeleteNcolIdx(pMeta, &oldEntry) < 0) {
1,042!
1960
    metaError("vgId:%d, failed to delete ncol idx:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
×
1961
  }
1962

1963
  if (metaUpdateNcolIdx(pMeta, &entry) < 0) {
1,042!
1964
    metaError("vgId:%d, failed to update ncol idx:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
×
1965
  }
1966

1967
  // save to table db
1968
  if (metaSaveToTbDb(pMeta, &entry) < 0) {
1,042!
1969
    metaError("vgId:%d, failed to save to tb db:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
×
1970
  }
1971

1972
  if (metaUpdateUidIdx(pMeta, &entry) < 0) {
1,042!
1973
    metaError("vgId:%d, failed to update uid idx:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
×
1974
  }
1975

1976
  if (metaSaveToSkmDb(pMeta, &entry) < 0) {
1,042!
1977
    metaError("vgId:%d, failed to save to skm db:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
×
1978
  }
1979

1980
  if (metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs) < 0) {
1,042!
1981
    metaError("vgId:%d, failed to update change time:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
×
1982
  }
1983

1984
  metaULock(pMeta);
1,042✔
1985

1986
  if (metaUpdateMetaRsp(uid, pAlterTbReq->tbName, pSchema, pMetaRsp) < 0) {
1,042!
1987
    metaError("vgId:%d, failed to update meta rsp:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
×
1988
  }
1989
  for (int32_t i = 0; i < entry.colCmpr.nCols; i++) {
70,557✔
1990
    SColCmpr *p = &entry.colCmpr.pColCmpr[i];
69,515✔
1991
    pMetaRsp->pSchemaExt[i].colId = p->id;
69,515✔
1992
    pMetaRsp->pSchemaExt[i].compress = p->alg;
69,515✔
1993
  }
1994

1995
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
1,042!
1996
  if (pNewSchema) taosMemoryFree(pNewSchema);
1,042✔
1997
  if (freeColCmpr) taosMemoryFree(entry.colCmpr.pColCmpr);
1,042✔
1998

1999
  tdbTbcClose(pTbDbc);
1,042✔
2000
  tdbTbcClose(pUidIdxc);
1,042✔
2001
  tDecoderClear(&dc);
1,042✔
2002

2003
  return 0;
1,042✔
2004

2005
_err:
929✔
2006
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
929!
2007
  tdbTbcClose(pTbDbc);
929✔
2008
  tdbTbcClose(pUidIdxc);
929✔
2009
  tDecoderClear(&dc);
929✔
2010

2011
  return terrno != 0 ? terrno : TSDB_CODE_FAILED;
929!
2012
}
2013

2014
static int metaUpdateTableTagVal(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
3,104✔
2015
  SMetaEntry  ctbEntry = {0};
3,104✔
2016
  SMetaEntry  stbEntry = {0};
3,104✔
2017
  void       *pVal = NULL;
3,104✔
2018
  int         nVal = 0;
3,104✔
2019
  int         ret;
2020
  int         c;
2021
  tb_uid_t    uid;
2022
  int64_t     oversion;
2023
  const void *pData = NULL;
3,104✔
2024
  int         nData = 0;
3,104✔
2025

2026
  if (pAlterTbReq->tagName == NULL) {
3,104!
2027
    return terrno = TSDB_CODE_INVALID_MSG;
×
2028
  }
2029

2030
  // search name index
2031
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
3,104✔
2032
  if (ret < 0) {
3,104✔
2033
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
1✔
2034
  }
2035

2036
  uid = *(tb_uid_t *)pVal;
3,103✔
2037
  tdbFree(pVal);
3,103✔
2038
  pVal = NULL;
3,103✔
2039

2040
  // search uid index
2041
  TBC *pUidIdxc = NULL;
3,103✔
2042

2043
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, NULL));
3,103!
2044
  if (tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c) < 0) {
3,103!
2045
    metaTrace("meta/table: failed to move to uid index, uid:%" PRId64, uid);
×
2046
  }
2047
  if (c != 0) {
3,103!
2048
    tdbTbcClose(pUidIdxc);
×
2049
    metaError("meta/table: invalide c: %" PRId32 " update tb tag val failed.", c);
×
2050
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2051
  }
2052

2053
  if (tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData) != 0) {
3,103!
2054
    metaError("meta/table: failed to get uid index, uid:%" PRId64, uid);
×
2055
  }
2056
  oversion = ((SUidIdxVal *)pData)[0].version;
3,103✔
2057

2058
  // search table.db
2059
  TBC     *pTbDbc = NULL;
3,103✔
2060
  SDecoder dc1 = {0};
3,103✔
2061
  SDecoder dc2 = {0};
3,103✔
2062

2063
  /* get ctbEntry */
2064
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pTbDb, &pTbDbc, NULL));
3,103!
2065
  if (tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c) != 0) {
3,103!
2066
    metaError("meta/table: failed to move to tb db, uid:%" PRId64, uid);
×
2067
  }
2068
  if (c != 0) {
3,103!
2069
    tdbTbcClose(pUidIdxc);
×
2070
    tdbTbcClose(pTbDbc);
×
2071
    metaError("meta/table: invalide c: %" PRId32 " update tb tag val failed.", c);
×
2072
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2073
  }
2074

2075
  if (tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData) != 0) {
3,103!
2076
    metaError("meta/table: failed to get tb db, uid:%" PRId64, uid);
×
2077
  }
2078

2079
  if ((ctbEntry.pBuf = taosMemoryMalloc(nData)) == NULL) {
3,103!
2080
    tdbTbcClose(pUidIdxc);
×
2081
    tdbTbcClose(pTbDbc);
×
2082
    return terrno;
×
2083
  }
2084
  memcpy(ctbEntry.pBuf, pData, nData);
3,103✔
2085
  tDecoderInit(&dc1, ctbEntry.pBuf, nData);
3,103✔
2086
  ret = metaDecodeEntry(&dc1, &ctbEntry);
3,103✔
2087
  if (ret < 0) {
3,103!
2088
    terrno = ret;
×
2089
    goto _err;
×
2090
  }
2091

2092
  /* get stbEntry*/
2093
  if (tdbTbGet(pMeta->pUidIdx, &ctbEntry.ctbEntry.suid, sizeof(tb_uid_t), &pVal, &nVal) != 0) {
3,103!
2094
    metaError("meta/table: failed to get uid index, uid:%" PRId64, ctbEntry.ctbEntry.suid);
×
2095
  }
2096
  if (!pVal) {
3,103!
2097
    terrno = TSDB_CODE_INVALID_MSG;
×
2098
    goto _err;
×
2099
  }
2100

2101
  if (tdbTbGet(pMeta->pTbDb, &((STbDbKey){.uid = ctbEntry.ctbEntry.suid, .version = ((SUidIdxVal *)pVal)[0].version}),
3,103!
2102
               sizeof(STbDbKey), (void **)&stbEntry.pBuf, &nVal) != 0) {
2103
    metaError("meta/table: failed to get tb db, uid:%" PRId64, ctbEntry.ctbEntry.suid);
×
2104
  }
2105
  tdbFree(pVal);
3,103✔
2106
  tDecoderInit(&dc2, stbEntry.pBuf, nVal);
3,103✔
2107
  ret = metaDecodeEntry(&dc2, &stbEntry);
3,103✔
2108
  if (ret < 0) {
3,103!
2109
    terrno = ret;
×
2110
    goto _err;
×
2111
  }
2112

2113
  SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
3,103✔
2114
  SSchema        *pColumn = NULL;
3,103✔
2115
  int32_t         iCol = 0;
3,103✔
2116
  for (;;) {
2117
    pColumn = NULL;
5,254✔
2118

2119
    if (iCol >= pTagSchema->nCols) break;
5,254!
2120
    pColumn = &pTagSchema->pSchema[iCol];
5,254✔
2121

2122
    if (strcmp(pColumn->name, pAlterTbReq->tagName) == 0) break;
5,254✔
2123
    iCol++;
2,151✔
2124
  }
2125

2126
  if (pColumn == NULL) {
3,103!
2127
    terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
×
2128
    goto _err;
×
2129
  }
2130

2131
  ctbEntry.version = version;
3,103✔
2132
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
3,103✔
2133
    ctbEntry.ctbEntry.pTags = taosMemoryMalloc(pAlterTbReq->nTagVal);
205✔
2134
    if (ctbEntry.ctbEntry.pTags == NULL) {
205!
2135
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2136
      goto _err;
×
2137
    }
2138
    memcpy((void *)ctbEntry.ctbEntry.pTags, pAlterTbReq->pTagVal, pAlterTbReq->nTagVal);
205✔
2139
  } else {
2140
    const STag *pOldTag = (const STag *)ctbEntry.ctbEntry.pTags;
2,898✔
2141
    STag       *pNewTag = NULL;
2,898✔
2142
    SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
2,898✔
2143
    if (!pTagArray) {
2,898!
2144
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2145
      goto _err;
×
2146
    }
2147
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
9,726✔
2148
      SSchema *pCol = &pTagSchema->pSchema[i];
6,828✔
2149
      if (iCol == i) {
6,828✔
2150
        if (pAlterTbReq->isNull) {
2,898✔
2151
          continue;
256✔
2152
        }
2153
        STagVal val = {0};
2,642✔
2154
        val.type = pCol->type;
2,642✔
2155
        val.cid = pCol->colId;
2,642✔
2156
        if (IS_VAR_DATA_TYPE(pCol->type)) {
2,642!
2157
          val.pData = pAlterTbReq->pTagVal;
1,164✔
2158
          val.nData = pAlterTbReq->nTagVal;
1,164✔
2159
        } else {
2160
          memcpy(&val.i64, pAlterTbReq->pTagVal, pAlterTbReq->nTagVal);
1,478✔
2161
        }
2162
        if (taosArrayPush(pTagArray, &val) == NULL) {
2,642!
2163
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2164
          taosArrayDestroy(pTagArray);
×
2165
          goto _err;
×
2166
        }
2167
      } else {
2168
        STagVal val = {.cid = pCol->colId};
3,930✔
2169
        if (tTagGet(pOldTag, &val)) {
3,930✔
2170
          if (taosArrayPush(pTagArray, &val) == NULL) {
3,266!
2171
            terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2172
            taosArrayDestroy(pTagArray);
×
2173
            goto _err;
×
2174
          }
2175
        }
2176
      }
2177
    }
2178
    if ((terrno = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag)) < 0) {
2,898!
2179
      taosArrayDestroy(pTagArray);
×
2180
      goto _err;
×
2181
    }
2182
    ctbEntry.ctbEntry.pTags = (uint8_t *)pNewTag;
2,898✔
2183
    taosArrayDestroy(pTagArray);
2,898✔
2184
  }
2185

2186
  metaWLock(pMeta);
3,103✔
2187

2188
  // save to table.db
2189
  if (metaSaveToTbDb(pMeta, &ctbEntry) < 0) {
3,103!
2190
    metaError("meta/table: failed to save to tb db:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2191
  }
2192

2193
  // save to uid.idx
2194
  if (metaUpdateUidIdx(pMeta, &ctbEntry) < 0) {
3,103!
2195
    metaError("meta/table: failed to update uid idx:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2196
  }
2197

2198
  if (metaUpdateTagIdx(pMeta, &ctbEntry) < 0) {
3,103!
2199
    metaError("meta/table: failed to update tag idx:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2200
  }
2201

2202
  if (NULL == ctbEntry.ctbEntry.pTags) {
3,103!
2203
    metaError("meta/table: null tags, update tag val failed.");
×
2204
    goto _err;
×
2205
  }
2206

2207
  SCtbIdxKey ctbIdxKey = {.suid = ctbEntry.ctbEntry.suid, .uid = uid};
3,103✔
2208
  if (tdbTbUpsert(pMeta->pCtbIdx, &ctbIdxKey, sizeof(ctbIdxKey), ctbEntry.ctbEntry.pTags,
3,103!
2209
                  ((STag *)(ctbEntry.ctbEntry.pTags))->len, pMeta->txn) < 0) {
3,103✔
2210
    metaError("meta/table: failed to upsert ctb idx:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2211
  }
2212

2213
  if (metaUidCacheClear(pMeta, ctbEntry.ctbEntry.suid) < 0) {
3,103!
2214
    metaError("meta/table: failed to clear uid cache:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2215
  }
2216

2217
  if (metaTbGroupCacheClear(pMeta, ctbEntry.ctbEntry.suid) < 0) {
3,103!
2218
    metaError("meta/table: failed to clear group cache:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2219
  }
2220

2221
  if (metaUpdateChangeTime(pMeta, ctbEntry.uid, pAlterTbReq->ctimeMs) < 0) {
3,103!
2222
    metaError("meta/table: failed to update change time:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2223
  }
2224

2225
  metaULock(pMeta);
3,103✔
2226

2227
  tDecoderClear(&dc1);
3,103✔
2228
  tDecoderClear(&dc2);
3,103✔
2229
  taosMemoryFree((void *)ctbEntry.ctbEntry.pTags);
3,103✔
2230
  if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
3,103!
2231
  if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
3,103!
2232
  tdbTbcClose(pTbDbc);
3,103✔
2233
  tdbTbcClose(pUidIdxc);
3,103✔
2234
  return 0;
3,103✔
2235

2236
_err:
×
2237
  tDecoderClear(&dc1);
×
2238
  tDecoderClear(&dc2);
×
2239
  if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
×
2240
  if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
×
2241
  tdbTbcClose(pTbDbc);
×
2242
  tdbTbcClose(pUidIdxc);
×
2243
  return -1;
×
2244
}
2245

2246
static int metaUpdateTableOptions(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
109✔
2247
  void       *pVal = NULL;
109✔
2248
  int         nVal = 0;
109✔
2249
  const void *pData = NULL;
109✔
2250
  int         nData = 0;
109✔
2251
  int         ret = 0;
109✔
2252
  tb_uid_t    uid;
2253
  int64_t     oversion;
2254
  SMetaEntry  entry = {0};
109✔
2255
  int         c = 0;
109✔
2256

2257
  // search name index
2258
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
109✔
2259
  if (ret < 0) {
109!
2260
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2261
  }
2262

2263
  uid = *(tb_uid_t *)pVal;
109✔
2264
  tdbFree(pVal);
109✔
2265
  pVal = NULL;
109✔
2266

2267
  // search uid index
2268
  TBC *pUidIdxc = NULL;
109✔
2269

2270
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, NULL));
109!
2271
  if (tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c) < 0) {
109!
2272
    metaError("meta/table: failed to move to uid index, uid:%" PRId64, uid);
×
2273
  }
2274
  if (c != 0) {
109!
2275
    tdbTbcClose(pUidIdxc);
×
2276
    metaError("meta/table: invalide c: %" PRId32 " update tb options failed.", c);
×
2277
    return TSDB_CODE_FAILED;
×
2278
  }
2279

2280
  if (tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData) < 0) {
109!
2281
    metaError("meta/table: failed to get uid index, uid:%" PRId64, uid);
×
2282
  }
2283
  oversion = ((SUidIdxVal *)pData)[0].version;
109✔
2284

2285
  // search table.db
2286
  TBC *pTbDbc = NULL;
109✔
2287

2288
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pTbDb, &pTbDbc, NULL));
109!
2289
  if (tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c) < 0) {
109!
2290
    metaError("meta/table: failed to move to tb db, uid:%" PRId64, uid);
×
2291
  }
2292
  if (c != 0) {
109!
2293
    tdbTbcClose(pUidIdxc);
×
2294
    tdbTbcClose(pTbDbc);
×
2295
    metaError("meta/table: invalide c: %" PRId32 " update tb options failed.", c);
×
2296
    return TSDB_CODE_FAILED;
×
2297
  }
2298

2299
  if (tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData) < 0) {
109!
2300
    metaError("meta/table: failed to get tb db, uid:%" PRId64, uid);
×
2301
  }
2302

2303
  // get table entry
2304
  SDecoder dc = {0};
109✔
2305
  if ((entry.pBuf = taosMemoryMalloc(nData)) == NULL) {
109!
2306
    tdbTbcClose(pUidIdxc);
×
2307
    tdbTbcClose(pTbDbc);
×
2308
    return terrno;
×
2309
  }
2310
  memcpy(entry.pBuf, pData, nData);
109✔
2311
  tDecoderInit(&dc, entry.pBuf, nData);
109✔
2312
  ret = metaDecodeEntry(&dc, &entry);
109✔
2313
  if (ret != 0) {
109!
2314
    tDecoderClear(&dc);
×
2315
    tdbTbcClose(pUidIdxc);
×
2316
    tdbTbcClose(pTbDbc);
×
2317
    metaError("meta/table: invalide ret: %" PRId32 " alt tb options failed.", ret);
×
2318
    return TSDB_CODE_FAILED;
×
2319
  }
2320

2321
  entry.version = version;
109✔
2322
  metaWLock(pMeta);
109✔
2323
  // build SMetaEntry
2324
  if (entry.type == TSDB_CHILD_TABLE) {
109✔
2325
    if (pAlterTbReq->updateTTL) {
56✔
2326
      metaDeleteTtl(pMeta, &entry);
25✔
2327
      entry.ctbEntry.ttlDays = pAlterTbReq->newTTL;
25✔
2328
      metaUpdateTtl(pMeta, &entry);
25✔
2329
    }
2330
    if (pAlterTbReq->newCommentLen >= 0) {
56✔
2331
      entry.ctbEntry.commentLen = pAlterTbReq->newCommentLen;
31✔
2332
      entry.ctbEntry.comment = pAlterTbReq->newComment;
31✔
2333
    }
2334
  } else {
2335
    if (pAlterTbReq->updateTTL) {
53✔
2336
      metaDeleteTtl(pMeta, &entry);
22✔
2337
      entry.ntbEntry.ttlDays = pAlterTbReq->newTTL;
22✔
2338
      metaUpdateTtl(pMeta, &entry);
22✔
2339
    }
2340
    if (pAlterTbReq->newCommentLen >= 0) {
53✔
2341
      entry.ntbEntry.commentLen = pAlterTbReq->newCommentLen;
31✔
2342
      entry.ntbEntry.comment = pAlterTbReq->newComment;
31✔
2343
    }
2344
  }
2345

2346
  // save to table db
2347
  if (metaSaveToTbDb(pMeta, &entry) < 0) {
109!
2348
    metaError("meta/table: failed to save to tb db:%s uid:%" PRId64, entry.name, entry.uid);
×
2349
  }
2350

2351
  if (metaUpdateUidIdx(pMeta, &entry) < 0) {
109!
2352
    metaError("meta/table: failed to update uid idx:%s uid:%" PRId64, entry.name, entry.uid);
×
2353
  }
2354

2355
  if (metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs) < 0) {
109!
2356
    metaError("meta/table: failed to update change time:%s uid:%" PRId64, entry.name, entry.uid);
×
2357
  }
2358

2359
  metaULock(pMeta);
109✔
2360

2361
  tdbTbcClose(pTbDbc);
109✔
2362
  tdbTbcClose(pUidIdxc);
109✔
2363
  tDecoderClear(&dc);
109✔
2364
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
109!
2365
  return 0;
109✔
2366
}
2367

2368
static int metaAddTagIndex(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
×
2369
  SMetaEntry  stbEntry = {0};
×
2370
  void       *pVal = NULL;
×
2371
  int         nVal = 0;
×
2372
  int         ret;
2373
  int         c;
2374
  tb_uid_t    uid, suid;
2375
  int64_t     oversion;
2376
  const void *pData = NULL;
×
2377
  int         nData = 0;
×
2378
  SDecoder    dc = {0};
×
2379

2380
  if (pAlterTbReq->tagName == NULL) {
×
2381
    return terrno = TSDB_CODE_INVALID_MSG;
×
2382
  }
2383

2384
  // search name index
2385
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
×
2386
  if (ret < 0) {
×
2387
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2388
  } else {
2389
    uid = *(tb_uid_t *)pVal;
×
2390
    tdbFree(pVal);
×
2391
    pVal = NULL;
×
2392
  }
2393

2394
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(tb_uid_t), &pVal, &nVal) == -1) {
×
2395
    ret = -1;
×
2396
    goto _err;
×
2397
  }
2398
  suid = ((SUidIdxVal *)pVal)[0].suid;
×
2399

2400
  STbDbKey tbDbKey = {0};
×
2401
  tbDbKey.uid = suid;
×
2402
  tbDbKey.version = ((SUidIdxVal *)pVal)[0].version;
×
2403
  ret = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pVal, &nVal);
×
2404
  if (ret < 0) {
×
2405
    goto _err;
×
2406
  }
2407
  tDecoderInit(&dc, pVal, nVal);
×
2408
  ret = metaDecodeEntry(&dc, &stbEntry);
×
2409
  if (ret < 0) {
×
2410
    tDecoderClear(&dc);
×
2411
    goto _err;
×
2412
  }
2413

2414
  // Get target schema info
2415
  SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
×
2416
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
×
2417
    terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
×
2418
    goto _err;
×
2419
  }
2420
  SSchema *pCol = NULL;
×
2421
  int32_t  iCol = 0;
×
2422
  for (;;) {
2423
    pCol = NULL;
×
2424
    if (iCol >= pTagSchema->nCols) break;
×
2425
    pCol = &pTagSchema->pSchema[iCol];
×
2426
    if (strcmp(pCol->name, pAlterTbReq->tagName) == 0) break;
×
2427
    iCol++;
×
2428
  }
2429

2430
  if (iCol == 0) {
×
2431
    terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
×
2432
    goto _err;
×
2433
  }
2434
  if (pCol == NULL) {
×
2435
    terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
×
2436
    goto _err;
×
2437
  }
2438

2439
  /*
2440
   * iterator all pTdDbc by uid and version
2441
   */
2442
  TBC *pCtbIdxc = NULL;
×
2443
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL));
×
2444
  int rc = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c);
×
2445
  if (rc < 0) {
×
2446
    tdbTbcClose(pCtbIdxc);
×
2447
    goto _err;
×
2448
  }
2449
  for (;;) {
×
2450
    void *pKey, *pVal;
2451
    int   nKey, nVal;
2452
    rc = tdbTbcNext(pCtbIdxc, &pKey, &nKey, &pVal, &nVal);
×
2453
    if (rc < 0) break;
×
2454
    if (((SCtbIdxKey *)pKey)->suid != uid) {
×
2455
      tdbFree(pKey);
×
2456
      tdbFree(pVal);
×
2457
      continue;
×
2458
    }
2459
    STagIdxKey *pTagIdxKey = NULL;
×
2460
    int32_t     nTagIdxKey;
2461

2462
    const void *pTagData = NULL;
×
2463
    int32_t     nTagData = 0;
×
2464

2465
    STagVal tagVal = {.cid = pCol->colId};
×
2466
    if (tTagGet((const STag *)pVal, &tagVal)) {
×
2467
      if (IS_VAR_DATA_TYPE(pCol->type)) {
×
2468
        pTagData = tagVal.pData;
×
2469
        nTagData = (int32_t)tagVal.nData;
×
2470
      } else {
2471
        pTagData = &(tagVal.i64);
×
2472
        nTagData = tDataTypes[pCol->type].bytes;
×
2473
      }
2474
    } else {
2475
      if (!IS_VAR_DATA_TYPE(pCol->type)) {
×
2476
        nTagData = tDataTypes[pCol->type].bytes;
×
2477
      }
2478
    }
2479
    if (metaCreateTagIdxKey(suid, pCol->colId, pTagData, nTagData, pCol->type, uid, &pTagIdxKey, &nTagIdxKey) < 0) {
×
2480
      tdbFree(pKey);
×
2481
      tdbFree(pVal);
×
2482
      metaDestroyTagIdxKey(pTagIdxKey);
×
2483
      tdbTbcClose(pCtbIdxc);
×
2484
      goto _err;
×
2485
    }
2486
    ret = tdbTbUpsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn);
×
2487
    if (ret < 0) {
×
2488
      metaError("meta/table: failed to upsert tag idx:%s uid:%" PRId64, stbEntry.name, stbEntry.uid);
×
2489
    }
2490
    metaDestroyTagIdxKey(pTagIdxKey);
×
2491
    pTagIdxKey = NULL;
×
2492
  }
2493
  tdbTbcClose(pCtbIdxc);
×
2494
  return 0;
×
2495

2496
_err:
×
2497
  // tDecoderClear(&dc1);
2498
  // tDecoderClear(&dc2);
2499
  // if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
2500
  // if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
2501
  // tdbTbcClose(pTbDbc);
2502
  // tdbTbcClose(pUidIdxc);
2503
  return TSDB_CODE_FAILED;
×
2504
}
2505

2506
typedef struct SMetaPair {
2507
  void *key;
2508
  int   nkey;
2509
} SMetaPair;
2510

2511
static int metaDropTagIndex(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
×
2512
  SMetaEntry  stbEntry = {0};
×
2513
  void       *pVal = NULL;
×
2514
  int         nVal = 0;
×
2515
  int         ret;
2516
  int         c;
2517
  tb_uid_t    suid;
2518
  int64_t     oversion;
2519
  const void *pData = NULL;
×
2520
  int         nData = 0;
×
2521
  SDecoder    dc = {0};
×
2522

2523
  if (pAlterTbReq->tagName == NULL) {
×
2524
    return terrno = TSDB_CODE_INVALID_MSG;
×
2525
  }
2526

2527
  // search name index
2528
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
×
2529
  if (ret < 0) {
×
2530
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2531
  }
2532
  suid = *(tb_uid_t *)pVal;
×
2533
  tdbFree(pVal);
×
2534
  pVal = NULL;
×
2535

2536
  if (tdbTbGet(pMeta->pUidIdx, &suid, sizeof(tb_uid_t), &pVal, &nVal) == -1) {
×
2537
    ret = -1;
×
2538
    goto _err;
×
2539
  }
2540

2541
  STbDbKey tbDbKey = {0};
×
2542
  tbDbKey.uid = suid;
×
2543
  tbDbKey.version = ((SUidIdxVal *)pVal)[0].version;
×
2544
  ret = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pVal, &nVal);
×
2545
  if (ret < 0) {
×
2546
    goto _err;
×
2547
  }
2548

2549
  tDecoderInit(&dc, pVal, nVal);
×
2550
  ret = metaDecodeEntry(&dc, &stbEntry);
×
2551
  if (ret < 0) {
×
2552
    tDecoderClear(&dc);
×
2553
    goto _err;
×
2554
  }
2555

2556
  // Get targe schema info
2557
  SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
×
2558
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
×
2559
    terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
×
2560
    goto _err;
×
2561
  }
2562
  SSchema *pCol = NULL;
×
2563
  int32_t  iCol = 0;
×
2564
  for (;;) {
2565
    pCol = NULL;
×
2566
    if (iCol >= pTagSchema->nCols) break;
×
2567
    pCol = &pTagSchema->pSchema[iCol];
×
2568
    if (strcmp(pCol->name, pAlterTbReq->tagName) == 0) break;
×
2569
    iCol++;
×
2570
  }
2571
  if (iCol == 0) {
×
2572
    // cannot drop 1th tag index
2573
    terrno = -1;
×
2574
    goto _err;
×
2575
  }
2576
  if (pCol == NULL) {
×
2577
    terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
×
2578
    goto _err;
×
2579
  }
2580

2581
  if (IS_IDX_ON(pCol)) {
×
2582
    terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
×
2583
    goto _err;
×
2584
  }
2585

2586
  SArray *tagIdxList = taosArrayInit(512, sizeof(SMetaPair));
×
2587
  if (tagIdxList == NULL) {
×
2588
    goto _err;
×
2589
  }
2590

2591
  TBC *pTagIdxc = NULL;
×
2592
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pTagIdx, &pTagIdxc, NULL));
×
2593
  int rc =
2594
      tdbTbcMoveTo(pTagIdxc, &(STagIdxKey){.suid = suid, .cid = INT32_MIN, .type = pCol->type}, sizeof(STagIdxKey), &c);
×
2595
  for (;;) {
×
2596
    void *pKey, *pVal;
2597
    int   nKey, nVal;
2598
    rc = tdbTbcNext(pTagIdxc, &pKey, &nKey, &pVal, &nVal);
×
2599
    STagIdxKey *pIdxKey = (STagIdxKey *)pKey;
×
2600
    if (pIdxKey->suid != suid || pIdxKey->cid != pCol->colId) {
×
2601
      tdbFree(pKey);
×
2602
      tdbFree(pVal);
×
2603
      continue;
×
2604
    }
2605

2606
    SMetaPair pair = {.key = pKey, nKey = nKey};
×
2607
    if (taosArrayPush(tagIdxList, &pair) == NULL) {
×
2608
      goto _err;
×
2609
    }
2610
  }
2611
  tdbTbcClose(pTagIdxc);
2612

2613
  metaWLock(pMeta);
2614
  for (int i = 0; i < taosArrayGetSize(tagIdxList); i++) {
2615
    SMetaPair *pair = taosArrayGet(tagIdxList, i);
2616
    ret = tdbTbDelete(pMeta->pTagIdx, pair->key, pair->nkey, pMeta->txn);
2617
    if (ret < 0) {
2618
      metaError("meta/table: failed to delete tag idx:%s uid:%" PRId64, stbEntry.name, stbEntry.uid);
2619
    }
2620
  }
2621
  metaULock(pMeta);
2622

2623
  taosArrayDestroy(tagIdxList);
2624

2625
  // set pCol->flags; INDEX_ON
2626
  return 0;
2627
_err:
×
2628
  return TSDB_CODE_FAILED;
×
2629
}
2630
int32_t metaUpdateTableColCompress(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
5✔
2631
  // impl later
2632
  SMetaEntry  tbEntry = {0};
5✔
2633
  void       *pVal = NULL;
5✔
2634
  int         nVal = 0;
5✔
2635
  int         ret;
2636
  int         c;
2637
  tb_uid_t    suid;
2638
  int64_t     oversion;
2639
  const void *pData = NULL;
5✔
2640
  int         nData = 0;
5✔
2641
  SDecoder    dc = {0};
5✔
2642
  ret = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &pVal, &nVal);
5✔
2643
  if (ret < 0) {
5!
2644
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2645
  }
2646
  suid = *(tb_uid_t *)pVal;
5✔
2647
  tdbFree(pVal);
5✔
2648
  pVal = NULL;
5✔
2649

2650
  if (tdbTbGet(pMeta->pUidIdx, &suid, sizeof(tb_uid_t), &pVal, &nVal) == -1) {
5!
2651
    terrno = TSDB_CODE_INVALID_MSG;
×
2652
    ret = -1;
×
2653
    goto _err;
×
2654
  }
2655

2656
  STbDbKey tbDbKey = {0};
5✔
2657
  tbDbKey.uid = suid;
5✔
2658
  tbDbKey.version = ((SUidIdxVal *)pVal)[0].version;
5✔
2659
  if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pVal, &nVal) < 0) {
5!
2660
    terrno = TSDB_CODE_INVALID_MSG;
×
2661
    tdbFree(pVal);
×
2662
    goto _err;
×
2663
  }
2664

2665
  tDecoderInit(&dc, pVal, nVal);
5✔
2666
  ret = metaDecodeEntry(&dc, &tbEntry);
5✔
2667
  if (ret < 0) {
5!
2668
    terrno = TSDB_CODE_INVALID_MSG;
×
2669
    tdbFree(pVal);
×
2670
    tDecoderClear(&dc);
×
2671
    goto _err;
×
2672
  }
2673
  if (tbEntry.type != TSDB_NORMAL_TABLE && tbEntry.type != TSDB_SUPER_TABLE) {
5!
2674
    terrno = TSDB_CODE_INVALID_MSG;
×
2675
    tdbFree(pVal);
×
2676
    tDecoderClear(&dc);
×
2677
    goto _err;
×
2678
  }
2679
  int8_t           updated = 0;
5✔
2680
  SColCmprWrapper *wp = &tbEntry.colCmpr;
5✔
2681
  for (int32_t i = 0; i < wp->nCols; i++) {
40✔
2682
    SColCmpr *p = &wp->pColCmpr[i];
35✔
2683
    if (p->id == pReq->colId) {
35✔
2684
      uint32_t dst = 0;
5✔
2685
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
5✔
2686
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
2687
      if (updated > 0) {
5!
2688
        p->alg = dst;
5✔
2689
      }
2690
    }
2691
  }
2692
  if (updated == 0) {
5!
2693
    tdbFree(pVal);
×
2694
    tDecoderClear(&dc);
×
2695
    terrno = TSDB_CODE_VND_COLUMN_COMPRESS_ALREADY_EXIST;
×
2696
    goto _err;
×
2697
  } else if (updated < 0) {
5!
2698
    tdbFree(pVal);
×
2699
    tDecoderClear(&dc);
×
2700
    terrno = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
2701
    goto _err;
×
2702
  }
2703
  tbEntry.version = version;
5✔
2704

2705
  metaWLock(pMeta);
5✔
2706
  if (metaSaveToTbDb(pMeta, &tbEntry) < 0) {
5!
2707
    metaError("meta/table: failed to save to tb db:%s uid:%" PRId64, tbEntry.name, tbEntry.uid);
×
2708
  }
2709

2710
  if (metaUpdateUidIdx(pMeta, &tbEntry) < 0) {
5!
2711
    metaError("meta/table: failed to update uid idx:%s uid:%" PRId64, tbEntry.name, tbEntry.uid);
×
2712
  }
2713

2714
  if (metaUpdateChangeTime(pMeta, suid, pReq->ctimeMs) < 0) {
5!
2715
    metaError("meta/table: failed to update change time:%s uid:%" PRId64, tbEntry.name, tbEntry.uid);
×
2716
  }
2717

2718
  metaULock(pMeta);
5✔
2719

2720
  tdbFree(pVal);
5✔
2721
  tDecoderClear(&dc);
5✔
2722

2723
  return 0;
5✔
2724
_err:
×
2725
  return TSDB_CODE_FAILED;
×
2726
}
2727

2728
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
5,189✔
2729
  pMeta->changed = true;
5,189✔
2730
  switch (pReq->action) {
5,189!
2731
    case TSDB_ALTER_TABLE_ADD_COLUMN:
1,971✔
2732
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
2733
    case TSDB_ALTER_TABLE_DROP_COLUMN:
2734
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
2735
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
2736
      return metaAlterTableColumn(pMeta, version, pReq, pMetaRsp);
1,971✔
2737
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
3,104✔
2738
      return metaUpdateTableTagVal(pMeta, version, pReq);
3,104✔
2739
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
109✔
2740
      return metaUpdateTableOptions(pMeta, version, pReq);
109✔
2741
    case TSDB_ALTER_TABLE_ADD_TAG_INDEX:
×
2742
      return metaAddTagIndex(pMeta, version, pReq);
×
2743
    case TSDB_ALTER_TABLE_DROP_TAG_INDEX:
×
2744
      return metaDropTagIndex(pMeta, version, pReq);
×
2745
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
5✔
2746
      return metaUpdateTableColCompress(pMeta, version, pReq);
5✔
2747
    default:
×
2748
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
2749
      break;
2750
  }
2751
}
2752

2753
static int metaSaveToTbDb(SMeta *pMeta, const SMetaEntry *pME) {
274,608✔
2754
  STbDbKey tbDbKey;
2755
  void    *pKey = NULL;
274,608✔
2756
  void    *pVal = NULL;
274,608✔
2757
  int      kLen = 0;
274,608✔
2758
  int      vLen = 0;
274,608✔
2759
  SEncoder coder = {0};
274,608✔
2760

2761
  // set key and value
2762
  tbDbKey.version = pME->version;
274,608✔
2763
  tbDbKey.uid = pME->uid;
274,608✔
2764

2765
  metaDebug("vgId:%d, start to save table version:%" PRId64 " uid:%" PRId64, TD_VID(pMeta->pVnode), pME->version,
274,608✔
2766
            pME->uid);
2767

2768
  pKey = &tbDbKey;
274,619✔
2769
  kLen = sizeof(tbDbKey);
274,619✔
2770

2771
  int32_t ret = 0;
274,619✔
2772
  tEncodeSize(metaEncodeEntry, pME, vLen, ret);
274,619!
2773
  if (ret < 0) {
274,500!
2774
    goto _err;
×
2775
  }
2776

2777
  pVal = taosMemoryMalloc(vLen);
274,500✔
2778
  if (pVal == NULL) {
274,580!
2779
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2780
    goto _err;
×
2781
  }
2782

2783
  tEncoderInit(&coder, pVal, vLen);
274,580✔
2784

2785
  if (metaEncodeEntry(&coder, pME) < 0) {
274,555!
2786
    goto _err;
×
2787
  }
2788

2789
  tEncoderClear(&coder);
274,591✔
2790

2791
  // write to table.db
2792
  if (tdbTbInsert(pMeta->pTbDb, pKey, kLen, pVal, vLen, pMeta->txn) < 0) {
274,680!
2793
    goto _err;
×
2794
  }
2795

2796
  taosMemoryFree(pVal);
274,653✔
2797
  return 0;
274,671✔
2798

2799
_err:
×
2800
  metaError("vgId:%d, failed to save table version:%" PRId64 "uid:%" PRId64 " %s", TD_VID(pMeta->pVnode), pME->version,
×
2801
            pME->uid, tstrerror(terrno));
2802

2803
  taosMemoryFree(pVal);
×
2804
  return TSDB_CODE_FAILED;
×
2805
}
2806

2807
static int metaUpdateUidIdx(SMeta *pMeta, const SMetaEntry *pME) {
274,594✔
2808
  // upsert cache
2809
  SMetaInfo info;
2810
  metaGetEntryInfo(pME, &info);
274,594✔
2811
  int32_t ret = metaCacheUpsert(pMeta, &info);
274,510✔
2812
  if (ret < 0) {
274,563!
2813
    metaError("vgId:%d, failed to upsert cache, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pME->uid, tstrerror(ret));
×
2814
  }
2815

2816
  SUidIdxVal uidIdxVal = {.suid = info.suid, .version = info.version, .skmVer = info.skmVer};
274,563✔
2817

2818
  return tdbTbUpsert(pMeta->pUidIdx, &pME->uid, sizeof(tb_uid_t), &uidIdxVal, sizeof(uidIdxVal), pMeta->txn);
274,563✔
2819
}
2820

2821
static int metaUpdateSuidIdx(SMeta *pMeta, const SMetaEntry *pME) {
30,998✔
2822
  return tdbTbUpsert(pMeta->pSuidIdx, &pME->uid, sizeof(tb_uid_t), NULL, 0, pMeta->txn);
30,998✔
2823
}
2824

2825
static int metaUpdateNameIdx(SMeta *pMeta, const SMetaEntry *pME) {
262,309✔
2826
  return tdbTbUpsert(pMeta->pNameIdx, pME->name, strlen(pME->name) + 1, &pME->uid, sizeof(tb_uid_t), pMeta->txn);
262,309✔
2827
}
2828

2829
static void metaUpdateTtl(SMeta *pMeta, const SMetaEntry *pME) {
231,401✔
2830
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
231,401!
2831

2832
  STtlUpdTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
231,401✔
2833
  if (pME->type == TSDB_CHILD_TABLE) {
231,401✔
2834
    ctx.ttlDays = pME->ctbEntry.ttlDays;
216,171✔
2835
    ctx.changeTimeMs = pME->ctbEntry.btime;
216,171✔
2836
  } else {
2837
    ctx.ttlDays = pME->ntbEntry.ttlDays;
15,230✔
2838
    ctx.changeTimeMs = pME->ntbEntry.btime;
15,230✔
2839
  }
2840

2841
  int32_t ret = ttlMgrInsertTtl(pMeta->pTtlMgr, &ctx);
231,401✔
2842
  if (ret < 0) {
231,410!
2843
    metaError("vgId:%d, failed to insert ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pME->uid, tstrerror(ret));
×
2844
  }
2845

2846
  return;
231,410✔
2847
}
2848

2849
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
4,260✔
2850
  if (!tsTtlChangeOnWrite) return 0;
4,260✔
2851

2852
  if (changeTimeMs <= 0) {
1!
2853
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
×
2854
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
×
2855
  }
2856

2857
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
1✔
2858

2859
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
1✔
2860
}
2861

2862
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
14,551,504✔
2863
  if (!tsTtlChangeOnWrite) return 0;
14,551,504!
2864

2865
  metaWLock(pMeta);
×
2866
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
1✔
2867
  metaULock(pMeta);
1✔
2868
  return ret;
1✔
2869
}
2870

2871
static int metaUpdateCtbIdx(SMeta *pMeta, const SMetaEntry *pME) {
216,137✔
2872
  SCtbIdxKey ctbIdxKey = {.suid = pME->ctbEntry.suid, .uid = pME->uid};
216,137✔
2873

2874
  return tdbTbUpsert(pMeta->pCtbIdx, &ctbIdxKey, sizeof(ctbIdxKey), pME->ctbEntry.pTags,
432,284✔
2875
                     ((STag *)(pME->ctbEntry.pTags))->len, pMeta->txn);
216,137✔
2876
}
2877

2878
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
274,198✔
2879
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
2880
  if (IS_VAR_DATA_TYPE(type)) {
274,198✔
2881
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
40,385✔
2882
  } else {
2883
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
233,813✔
2884
  }
2885

2886
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
274,198✔
2887
  if (*ppTagIdxKey == NULL) {
274,202!
2888
    return terrno;
×
2889
  }
2890

2891
  (*ppTagIdxKey)->suid = suid;
274,206✔
2892
  (*ppTagIdxKey)->cid = cid;
274,206✔
2893
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
274,206✔
2894
  (*ppTagIdxKey)->type = type;
274,206✔
2895

2896
  // refactor
2897
  if (IS_VAR_DATA_TYPE(type)) {
274,206✔
2898
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
40,408✔
2899
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
40,408✔
2900
    *(tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData) = uid;
40,408✔
2901
  } else {
2902
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
233,798✔
2903
    *(tb_uid_t *)((*ppTagIdxKey)->data + nTagData) = uid;
233,798✔
2904
  }
2905

2906
  return 0;
274,206✔
2907
}
2908

2909
static void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
273,766✔
2910
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
273,766!
2911
}
273,768✔
2912

2913
static int metaUpdateTagIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry) {
219,246✔
2914
  void          *pData = NULL;
219,246✔
2915
  int            nData = 0;
219,246✔
2916
  STbDbKey       tbDbKey = {0};
219,246✔
2917
  SMetaEntry     stbEntry = {0};
219,246✔
2918
  STagIdxKey    *pTagIdxKey = NULL;
219,246✔
2919
  int32_t        nTagIdxKey;
2920
  const SSchema *pTagColumn;
2921
  const void    *pTagData = NULL;
219,246✔
2922
  int32_t        nTagData = 0;
219,246✔
2923
  SDecoder       dc = {0};
219,246✔
2924
  int32_t        ret = 0;
219,246✔
2925
  // get super table
2926
  if (tdbTbGet(pMeta->pUidIdx, &pCtbEntry->ctbEntry.suid, sizeof(tb_uid_t), &pData, &nData) != 0) {
219,246!
2927
    metaError("vgId:%d, failed to get stable suid for update. version:%" PRId64, TD_VID(pMeta->pVnode),
×
2928
              pCtbEntry->version);
2929
    ret = TSDB_CODE_TDB_INVALID_TABLE_ID;
×
2930
    goto end;
×
2931
  }
2932
  tbDbKey.uid = pCtbEntry->ctbEntry.suid;
219,268✔
2933
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
219,268✔
2934
  ret = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData);
219,268✔
2935
  if (ret < 0) {
219,252!
2936
    metaError("vgId:%d, failed to get stable for update. version:%" PRId64, TD_VID(pMeta->pVnode), pCtbEntry->version);
×
2937
    goto end;
×
2938
  }
2939

2940
  tDecoderInit(&dc, pData, nData);
219,252✔
2941
  ret = metaDecodeEntry(&dc, &stbEntry);
219,243✔
2942
  if (ret < 0) {
219,221!
2943
    goto end;
×
2944
  }
2945

2946
  if (stbEntry.stbEntry.schemaTag.pSchema == NULL) {
219,221!
2947
    ret = TSDB_CODE_INVALID_PARA;
×
2948
    goto end;
×
2949
  }
2950

2951
  SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
219,221✔
2952
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
219,221✔
2953
    pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
576✔
2954
    STagVal tagVal = {.cid = pTagColumn->colId};
576✔
2955

2956
    pTagData = pCtbEntry->ctbEntry.pTags;
576✔
2957
    nTagData = ((const STag *)pCtbEntry->ctbEntry.pTags)->len;
576✔
2958
    ret = metaSaveJsonVarToIdx(pMeta, pCtbEntry, pTagColumn);
576✔
2959
    goto end;
576✔
2960
  } else {
2961
    for (int i = 0; i < pTagSchema->nCols; i++) {
820,235✔
2962
      pTagData = NULL;
601,554✔
2963
      nTagData = 0;
601,554✔
2964
      pTagColumn = &pTagSchema->pSchema[i];
601,554✔
2965
      if (!IS_IDX_ON(pTagColumn)) continue;
601,554✔
2966

2967
      STagVal tagVal = {.cid = pTagColumn->colId};
218,605✔
2968
      if (tTagGet((const STag *)pCtbEntry->ctbEntry.pTags, &tagVal)) {
218,605✔
2969
        if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
217,433!
2970
          pTagData = tagVal.pData;
27,485✔
2971
          nTagData = (int32_t)tagVal.nData;
27,485✔
2972
        } else {
2973
          pTagData = &(tagVal.i64);
189,948✔
2974
          nTagData = tDataTypes[pTagColumn->type].bytes;
189,948✔
2975
        }
2976
      } else {
2977
        if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
1,201!
2978
          nTagData = tDataTypes[pTagColumn->type].bytes;
903✔
2979
        }
2980
      }
2981
      if (metaCreateTagIdxKey(pCtbEntry->ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type,
218,618!
2982
                              pCtbEntry->uid, &pTagIdxKey, &nTagIdxKey) < 0) {
218,634✔
2983
        ret = -1;
×
2984
        goto end;
×
2985
      }
2986
      if (tdbTbUpsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn) < 0) {
218,618!
2987
        metaError("vgId:%d, failed to update tag index. version:%" PRId64, TD_VID(pMeta->pVnode), pCtbEntry->version);
×
2988
      }
2989
      metaDestroyTagIdxKey(pTagIdxKey);
218,640✔
2990
      pTagIdxKey = NULL;
218,641✔
2991
    }
2992
  }
2993
end:
218,681✔
2994
  tDecoderClear(&dc);
219,257✔
2995
  tdbFree(pData);
219,258✔
2996
  return ret;
219,263✔
2997
}
2998

2999
static int metaSaveToSkmDb(SMeta *pMeta, const SMetaEntry *pME) {
50,211✔
3000
  SEncoder              coder = {0};
50,211✔
3001
  void                 *pVal = NULL;
50,211✔
3002
  int                   vLen = 0;
50,211✔
3003
  int                   rcode = 0;
50,211✔
3004
  SSkmDbKey             skmDbKey = {0};
50,211✔
3005
  const SSchemaWrapper *pSW;
3006

3007
  if (pME->type == TSDB_SUPER_TABLE) {
50,211✔
3008
    pSW = &pME->stbEntry.schemaRow;
33,977✔
3009
  } else if (pME->type == TSDB_NORMAL_TABLE) {
16,234!
3010
    pSW = &pME->ntbEntry.schemaRow;
16,256✔
3011
  } else {
3012
    metaError("meta/table: invalide table type: %" PRId8 " save skm db failed.", pME->type);
×
3013
    return TSDB_CODE_FAILED;
×
3014
  }
3015

3016
  skmDbKey.uid = pME->uid;
50,233✔
3017
  skmDbKey.sver = pSW->version;
50,233✔
3018

3019
  // if receive tmq meta message is: create stable1 then delete stable1 then create stable1 with multi vgroups
3020
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(skmDbKey), NULL, NULL) == 0) {
50,233✔
3021
    return rcode;
10✔
3022
  }
3023

3024
  // encode schema
3025
  int32_t ret = 0;
50,255✔
3026
  tEncodeSize(tEncodeSSchemaWrapper, pSW, vLen, ret);
100,498✔
3027
  if (ret < 0) return -1;
50,166!
3028
  pVal = taosMemoryMalloc(vLen);
50,166✔
3029
  if (pVal == NULL) {
50,216!
3030
    rcode = -1;
×
3031
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3032
    goto _exit;
×
3033
  }
3034

3035
  tEncoderInit(&coder, pVal, vLen);
50,216✔
3036
  ret = tEncodeSSchemaWrapper(&coder, pSW);
50,208✔
3037
  if (ret < 0) {
50,208!
3038
    rcode = -1;
×
3039
    goto _exit;
×
3040
  }
3041

3042
  if (tdbTbInsert(pMeta->pSkmDb, &skmDbKey, sizeof(skmDbKey), pVal, vLen, pMeta->txn) < 0) {
50,208!
3043
    rcode = -1;
×
3044
    goto _exit;
×
3045
  }
3046

3047
  metaDebug("vgId:%d, set schema:(%" PRId64 ") sver:%d since %s", TD_VID(pMeta->pVnode), pME->uid, pSW->version,
50,237✔
3048
            tstrerror(terrno));
3049

3050
_exit:
38,961✔
3051
  taosMemoryFree(pVal);
50,244✔
3052
  tEncoderClear(&coder);
50,227✔
3053
  return rcode;
50,240✔
3054
}
3055

3056
int metaHandleEntry(SMeta *pMeta, const SMetaEntry *pME) {
262,078✔
3057
  int32_t code = 0;
262,078✔
3058
  int32_t line = 0;
262,078✔
3059
  metaWLock(pMeta);
262,078✔
3060

3061
  // save to table.db
3062
  code = metaSaveToTbDb(pMeta, pME);
262,346✔
3063
  VND_CHECK_CODE(code, line, _err);
262,307!
3064

3065
  // update uid.idx
3066
  code = metaUpdateUidIdx(pMeta, pME);
262,307✔
3067
  VND_CHECK_CODE(code, line, _err);
262,326!
3068

3069
  // update name.idx
3070
  code = metaUpdateNameIdx(pMeta, pME);
262,326✔
3071
  VND_CHECK_CODE(code, line, _err);
262,360!
3072

3073
  if (pME->type == TSDB_CHILD_TABLE) {
262,360✔
3074
    // update ctb.idx
3075
    code = metaUpdateCtbIdx(pMeta, pME);
216,145✔
3076
    VND_CHECK_CODE(code, line, _err);
216,145!
3077

3078
    // update tag.idx
3079
    code = metaUpdateTagIdx(pMeta, pME);
216,145✔
3080
    VND_CHECK_CODE(code, line, _err);
216,153!
3081
  } else {
3082
    // update schema.db
3083
    code = metaSaveToSkmDb(pMeta, pME);
46,215✔
3084
    VND_CHECK_CODE(code, line, _err);
46,187!
3085

3086
    if (pME->type == TSDB_SUPER_TABLE) {
46,187✔
3087
      code = metaUpdateSuidIdx(pMeta, pME);
31,003✔
3088
      VND_CHECK_CODE(code, line, _err);
31,009!
3089
    }
3090
  }
3091

3092
  code = metaUpdateBtimeIdx(pMeta, pME);
262,346✔
3093
  VND_CHECK_CODE(code, line, _err);
262,340!
3094

3095
  if (pME->type == TSDB_NORMAL_TABLE) {
262,340✔
3096
    code = metaUpdateNcolIdx(pMeta, pME);
15,214✔
3097
    VND_CHECK_CODE(code, line, _err);
15,214!
3098
  }
3099

3100
  if (pME->type != TSDB_SUPER_TABLE) {
262,340✔
3101
    metaUpdateTtl(pMeta, pME);
231,364✔
3102
  }
3103

3104
  if (pME->type == TSDB_SUPER_TABLE || pME->type == TSDB_NORMAL_TABLE) {
262,330✔
3105
  }
3106

3107
  metaULock(pMeta);
262,330✔
3108
  metaDebug("vgId:%d, handle meta entry, ver:%" PRId64 ", uid:%" PRId64 ", name:%s", TD_VID(pMeta->pVnode),
262,338✔
3109
            pME->version, pME->uid, pME->name);
3110
  return 0;
262,322✔
3111

3112
_err:
×
3113
  metaULock(pMeta);
×
3114
  metaError("vgId:%d, failed to handle meta entry since %s at line:%d, ver:%" PRId64 ", uid:%" PRId64 ", name:%s",
×
3115
            TD_VID(pMeta->pVnode), terrstr(), line, pME->version, pME->uid, pME->name);
3116
  return TSDB_CODE_FAILED;
×
3117
}
3118

3119
static void colCompressDebug(SHashObj *pColCmprObj) {
803,237✔
3120
  void *p = taosHashIterate(pColCmprObj, NULL);
803,237✔
3121
  while (p) {
5,660,149✔
3122
    uint32_t cmprAlg = *(uint32_t *)p;
4,857,121✔
3123
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
4,857,121✔
3124
    p = taosHashIterate(pColCmprObj, p);
4,857,001✔
3125

3126
    uint8_t l1, l2, lvl;
3127
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
4,857,898✔
3128

3129
    const char *l1str = columnEncodeStr(l1);
4,857,480✔
3130
    const char *l2str = columnCompressStr(l2);
4,857,263✔
3131
    const char *lvlstr = columnLevelStr(lvl);
4,857,095✔
3132
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
4,856,933✔
3133
  }
3134
  return;
803,028✔
3135
}
3136
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
803,239✔
3137
  int rc = 0;
803,239✔
3138

3139
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
803,239✔
3140
  if (pColCmprObj == NULL) {
803,268!
3141
    pColCmprObj = NULL;
×
3142
    return TSDB_CODE_OUT_OF_MEMORY;
×
3143
  }
3144

3145
  void      *pData = NULL;
803,268✔
3146
  int        nData = 0;
803,268✔
3147
  SMetaEntry e = {0};
803,268✔
3148
  SDecoder   dc = {0};
803,268✔
3149

3150
  *ppColCmprObj = NULL;
803,268✔
3151

3152
  metaRLock(pMeta);
803,268✔
3153
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
803,294✔
3154
  if (rc < 0) {
803,277✔
3155
    taosHashClear(pColCmprObj);
25✔
3156
    metaULock(pMeta);
25✔
3157
    return TSDB_CODE_FAILED;
25✔
3158
  }
3159
  int64_t version = ((SUidIdxVal *)pData)[0].version;
803,252✔
3160
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
803,252✔
3161
  if (rc < 0) {
803,246!
3162
    metaULock(pMeta);
×
3163
    taosHashClear(pColCmprObj);
×
3164
    metaError("failed to get table entry");
×
3165
    return rc;
×
3166
  }
3167

3168
  tDecoderInit(&dc, pData, nData);
803,246✔
3169
  rc = metaDecodeEntry(&dc, &e);
803,232✔
3170
  if (rc < 0) {
803,147!
3171
    tDecoderClear(&dc);
×
3172
    tdbFree(pData);
×
3173
    metaULock(pMeta);
×
3174
    taosHashClear(pColCmprObj);
×
3175
    return rc;
×
3176
  }
3177
  if (useCompress(e.type)) {
803,147!
3178
    SColCmprWrapper *p = &e.colCmpr;
803,169✔
3179
    for (int32_t i = 0; i < p->nCols; i++) {
5,660,075✔
3180
      SColCmpr *pCmpr = &p->pColCmpr[i];
4,856,663✔
3181
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
4,856,663✔
3182
      if (rc < 0) {
4,856,795!
3183
        tDecoderClear(&dc);
×
3184
        tdbFree(pData);
×
3185
        metaULock(pMeta);
×
3186
        taosHashClear(pColCmprObj);
×
3187
        return rc;
×
3188
      }
3189
    }
3190
  } else {
3191
    tDecoderClear(&dc);
×
3192
    tdbFree(pData);
×
3193
    metaULock(pMeta);
×
3194
    taosHashClear(pColCmprObj);
×
3195
    return 0;
×
3196
  }
3197
  tDecoderClear(&dc);
803,412✔
3198
  tdbFree(pData);
803,225✔
3199
  metaULock(pMeta);
803,260✔
3200

3201
  *ppColCmprObj = pColCmprObj;
803,262✔
3202
  colCompressDebug(pColCmprObj);
803,262✔
3203

3204
  return 0;
803,128✔
3205
}
3206
// refactor later
3207
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
96,169✔
3208
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
96,167✔
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

© 2025 Coveralls, Inc