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

taosdata / TDengine / #4579

25 Jul 2025 01:12AM UTC coverage: 57.921% (+1.7%) from 56.254%
#4579

push

travis-ci

web-flow
enh: refactor some tsdb log info (#32159)

142530 of 316723 branches covered (45.0%)

Branch coverage included in aggregate %.

0 of 13 new or added lines in 2 files covered. (0.0%)

151 existing lines in 26 files now uncovered.

215698 of 301752 relevant lines covered (71.48%)

4618632.12 hits per line

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

58.1
/source/dnode/vnode/src/meta/metaEntry2.c
1
/*
2
 * Copyright (c) 2023 Hongze Cheng <hzcheng@umich.edu>.
3
 * All rights reserved.
4
 *
5
 * This code is the intellectual property of Hongze Cheng.
6
 * Any reproduction or distribution, in whole or in part,
7
 * without the express written permission of Hongze Cheng is
8
 * strictly prohibited.
9
 */
10

11
#include "meta.h"
12
#include "vnodeInt.h"
13

14
extern SDmNotifyHandle dmNotifyHdl;
15

16
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry);
17
void    metaCloneEntryFree(SMetaEntry **ppEntry);
18
void    metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey);
19
int     metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema);
20
int     metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema);
21
int     tagIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
22

23
static void    metaTimeSeriesNotifyCheck(SMeta *pMeta);
24
static int32_t metaGetChildUidsOfSuperTable(SMeta *pMeta, tb_uid_t suid, SArray **childList);
25
static int32_t metaFetchTagIdxKey(SMeta *pMeta, const SMetaEntry *pEntry, const SSchema *pTagColumn,
26
                                  STagIdxKey **ppTagIdxKey, int32_t *pTagIdxKeySize);
27
static void    metaFetchTagIdxKeyFree(STagIdxKey **ppTagIdxKey);
28

29
#define metaErr(VGID, ERRNO)                                                                                     \
30
  do {                                                                                                           \
31
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " type:%d uid:%" PRId64 " name:%s", VGID, \
32
              __func__, __FILE__, __LINE__, tstrerror(ERRNO), pEntry->version, pEntry->type, pEntry->uid,        \
33
              pEntry->type > 0 ? pEntry->name : NULL);                                                           \
34
  } while (0)
35

36
typedef enum {
37
  META_ENTRY_TABLE = 0,
38
  META_SCHEMA_TABLE,
39
  META_UID_IDX,
40
  META_NAME_IDX,
41
  META_SUID_IDX,
42
  META_CHILD_IDX,
43
  META_TAG_IDX,
44
  META_BTIME_IDX,
45
  META_TTL_IDX,
46
  META_TABLE_MAX,
47
} EMetaTable;
48

49
typedef enum {
50
  META_TABLE_OP_INSERT = 0,
51
  META_TABLE_OP_UPDATA,
52
  META_TABLE_OP_DELETE,
53
  META_TABLE_OP_MAX,
54
} EMetaTableOp;
55

56
typedef struct {
57
  const SMetaEntry *pEntry;
58
  const SMetaEntry *pSuperEntry;
59
  const SMetaEntry *pOldEntry;
60
} SMetaHandleParam;
61

62
typedef struct {
63
  EMetaTable   table;
64
  EMetaTableOp op;
65
} SMetaTableOp;
66

67
int32_t metaFetchEntryByUid(SMeta *pMeta, int64_t uid, SMetaEntry **ppEntry) {
486,575✔
68
  int32_t code = TSDB_CODE_SUCCESS;
486,575✔
69
  void   *value = NULL;
486,575✔
70
  int32_t valueSize = 0;
486,575✔
71

72
  // search uid index
73
  code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &value, &valueSize);
486,575✔
74
  if (TSDB_CODE_SUCCESS != code) {
486,629!
75
    metaError("vgId:%d, failed to get entry by uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), uid, tstrerror(code));
×
76
    return code;
×
77
  }
78

79
  // search entry table
80
  STbDbKey key = {
486,629✔
81
      .version = ((SUidIdxVal *)value)->version,
486,629✔
82
      .uid = uid,
83
  };
84
  tdbFreeClear(value);
486,629✔
85

86
  code = tdbTbGet(pMeta->pTbDb, &key, sizeof(key), &value, &valueSize);
486,606✔
87
  if (TSDB_CODE_SUCCESS != code) {
486,628!
88
    metaError("vgId:%d, failed to get entry by uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), uid, tstrerror(code));
×
89
    code = TSDB_CODE_INTERNAL_ERROR;
×
90
    return code;
×
91
  }
92

93
  // decode entry
94
  SDecoder   decoder = {0};
486,628✔
95
  SMetaEntry entry = {0};
486,628✔
96

97
  tDecoderInit(&decoder, value, valueSize);
486,628✔
98
  code = metaDecodeEntry(&decoder, &entry);
486,594✔
99
  if (code) {
486,457!
100
    metaError("vgId:%d, failed to decode entry by uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), uid,
×
101
              tstrerror(code));
102
    tDecoderClear(&decoder);
×
103
    tdbFreeClear(value);
×
104
    return code;
×
105
  }
106

107
  code = metaCloneEntry(&entry, ppEntry);
486,457✔
108
  if (code) {
486,625!
109
    metaError("vgId:%d, failed to clone entry by uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), uid,
×
110
              tstrerror(code));
111
    tDecoderClear(&decoder);
×
112
    tdbFreeClear(value);
×
113
    return code;
×
114
  }
115

116
  tdbFreeClear(value);
486,625✔
117
  tDecoderClear(&decoder);
486,637✔
118
  return code;
486,626✔
119
}
120

121
int32_t metaFetchEntryByName(SMeta *pMeta, const char *name, SMetaEntry **ppEntry) {
205,936✔
122
  int32_t code = TSDB_CODE_SUCCESS;
205,936✔
123
  void   *value = NULL;
205,936✔
124
  int32_t valueSize = 0;
205,936✔
125

126
  code = tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &value, &valueSize);
205,936✔
127
  if (TSDB_CODE_SUCCESS != code) {
205,938!
128
    metaError("vgId:%d, failed to get entry by name:%s since %s", TD_VID(pMeta->pVnode), name, tstrerror(code));
×
129
    return code;
×
130
  }
131
  int64_t uid = *(int64_t *)value;
205,938✔
132
  tdbFreeClear(value);
205,938✔
133

134
  code = metaFetchEntryByUid(pMeta, uid, ppEntry);
205,944✔
135
  if (TSDB_CODE_SUCCESS != code) {
205,907!
136
    metaError("vgId:%d, failed to get entry by uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), uid, tstrerror(code));
×
137
    code = TSDB_CODE_INTERNAL_ERROR;
×
138
  }
139
  return code;
205,907✔
140
}
141

142
void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); }
486,688✔
143

144
// Entry Table
145
static int32_t metaEntryTableUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
278,404✔
146
  const SMetaEntry *pEntry = pParam->pEntry;
278,404✔
147

148
  int32_t  code = TSDB_CODE_SUCCESS;
278,404✔
149
  int32_t  vgId = TD_VID(pMeta->pVnode);
278,404✔
150
  void    *value = NULL;
278,404✔
151
  int32_t  valueSize = 0;
278,404✔
152
  SEncoder encoder = {0};
278,404✔
153
  STbDbKey key = {
278,404✔
154
      .version = pEntry->version,
278,404✔
155
      .uid = pEntry->uid,
278,404✔
156
  };
157

158
  // encode entry
159
  tEncodeSize(metaEncodeEntry, pEntry, valueSize, code);
278,404!
160
  if (code != 0) {
277,147!
161
    metaErr(vgId, code);
×
162
    return code;
×
163
  }
164

165
  value = taosMemoryMalloc(valueSize);
277,147!
166
  if (NULL == value) {
278,352!
167
    metaErr(vgId, terrno);
×
168
    return terrno;
×
169
  }
170

171
  tEncoderInit(&encoder, value, valueSize);
278,352✔
172
  code = metaEncodeEntry(&encoder, pEntry);
278,339✔
173
  if (code) {
278,198!
174
    metaErr(vgId, code);
×
175
    tEncoderClear(&encoder);
×
176
    taosMemoryFree(value);
×
177
    return code;
×
178
  }
179
  tEncoderClear(&encoder);
278,198✔
180

181
  // put to tdb
182
  if (META_TABLE_OP_INSERT == op) {
278,320✔
183
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
223,638✔
184
  } else if (META_TABLE_OP_UPDATA == op) {
54,682✔
185
    code = tdbTbUpsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
36,048✔
186
  } else if (META_TABLE_OP_DELETE == op) {
18,634!
187
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
18,634✔
188
  } else {
189
    code = TSDB_CODE_INVALID_PARA;
×
190
  }
191
  if (TSDB_CODE_SUCCESS != code) {
278,357!
192
    metaErr(vgId, code);
×
193
  }
194
  taosMemoryFree(value);
278,308!
195
  return code;
278,403✔
196
}
197

198
static int32_t metaEntryTableInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
222,778✔
199
  return metaEntryTableUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
222,778✔
200
}
201

202
static int32_t metaEntryTableUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
36,031✔
203
  return metaEntryTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
36,031✔
204
}
205

206
static int32_t metaEntryTableDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
18,634✔
207
  return metaEntryTableUpsert(pMeta, pParam, META_TABLE_OP_DELETE);
18,634✔
208
}
209

210
// Schema Table
211
static int32_t metaSchemaTableUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
60,938✔
212
  int32_t  code = TSDB_CODE_SUCCESS;
60,938✔
213
  int32_t  vgId = TD_VID(pMeta->pVnode);
60,938✔
214
  SEncoder encoder = {0};
60,938✔
215
  void    *value = NULL;
60,938✔
216
  int32_t  valueSize = 0;
60,938✔
217

218
  const SMetaEntry     *pEntry = pParam->pEntry;
60,938✔
219
  const SSchemaWrapper *pSchema = NULL;
60,938✔
220
  if (pEntry->type == TSDB_SUPER_TABLE) {
60,938✔
221
    pSchema = &pEntry->stbEntry.schemaRow;
39,818✔
222
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
21,120!
223
    pSchema = &pEntry->ntbEntry.schemaRow;
21,120✔
224
  } else {
225
    return TSDB_CODE_INVALID_PARA;
×
226
  }
227
  SSkmDbKey key = {
60,938✔
228
      .uid = pEntry->uid,
60,938✔
229
      .sver = pSchema->version,
60,938✔
230
  };
231

232
  // encode schema
233
  tEncodeSize(tEncodeSSchemaWrapper, pSchema, valueSize, code);
121,846✔
234
  if (TSDB_CODE_SUCCESS != code) {
60,725!
235
    metaErr(vgId, code);
×
236
    return code;
×
237
  }
238

239
  value = taosMemoryMalloc(valueSize);
60,725!
240
  if (NULL == value) {
60,659!
241
    metaErr(vgId, terrno);
×
242
    return terrno;
×
243
  }
244

245
  tEncoderInit(&encoder, value, valueSize);
60,659✔
246
  code = tEncodeSSchemaWrapper(&encoder, pSchema);
60,674✔
247
  if (TSDB_CODE_SUCCESS != code) {
60,674!
248
    metaErr(vgId, code);
×
249
    tEncoderClear(&encoder);
×
250
    taosMemoryFree(value);
×
251
    return code;
×
252
  }
253
  tEncoderClear(&encoder);
60,674✔
254

255
  // put to tdb
256
  if (META_TABLE_OP_INSERT == op) {
60,606!
257
    code = tdbTbInsert(pMeta->pSkmDb, &key, sizeof(key), value, valueSize, pMeta->txn);
×
258
  } else if (META_TABLE_OP_UPDATA == op) {
60,661!
259
    code = tdbTbUpsert(pMeta->pSkmDb, &key, sizeof(key), value, valueSize, pMeta->txn);
60,661✔
260
  } else {
261
    code = TSDB_CODE_INVALID_PARA;
×
262
  }
263
  if (TSDB_CODE_SUCCESS != code) {
60,997!
264
    metaErr(vgId, code);
×
265
  }
266
  taosMemoryFree(value);
60,980!
267
  return code;
61,004✔
268
}
269

270
static int32_t metaSchemaTableInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
271
  return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
×
272
}
273

274
static int32_t metaAddOrDropTagIndexOfSuperTable(SMeta *pMeta, const SMetaHandleParam *pParam,
101,012✔
275
                                                 const SSchema *pOldColumn, const SSchema *pNewColumn) {
276
  int32_t code = TSDB_CODE_SUCCESS;
101,012✔
277

278
  const SMetaEntry *pEntry = pParam->pEntry;
101,012✔
279
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
101,012✔
280
  enum { ADD_INDEX, DROP_INDEX } action;
281

282
  if (pOldColumn && pNewColumn) {
101,012✔
283
    if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
98,569✔
284
      return TSDB_CODE_SUCCESS;
4,177✔
285
    } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) {
94,392!
286
      action = DROP_INDEX;
74✔
287
    } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
94,318!
288
      action = ADD_INDEX;
26✔
289
    } else {
290
      return TSDB_CODE_SUCCESS;
94,292✔
291
    }
292
  } else if (pOldColumn) {
2,443✔
293
    if (IS_IDX_ON(pOldColumn)) {
1,001✔
294
      action = DROP_INDEX;
44✔
295
    } else {
296
      return TSDB_CODE_SUCCESS;
957✔
297
    }
298
  } else {
299
    if (IS_IDX_ON(pNewColumn)) {
1,442!
300
      action = ADD_INDEX;
×
301
    } else {
302
      return TSDB_CODE_SUCCESS;
1,442✔
303
    }
304
  }
305

306
  // fetch all child tables
307
  SArray *childTables = 0;
144✔
308
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childTables);
144✔
309
  if (code) {
144✔
310
    metaErr(TD_VID(pMeta->pVnode), code);
1!
311
    return code;
×
312
  }
313

314
  // do drop or add index
315
  for (int32_t i = 0; i < taosArrayGetSize(childTables); i++) {
831✔
316
    int64_t uid = *(int64_t *)taosArrayGet(childTables, i);
688✔
317

318
    // fetch child entry
319
    SMetaEntry *pChildEntry = NULL;
688✔
320
    code = metaFetchEntryByUid(pMeta, uid, &pChildEntry);
688✔
321
    if (code) {
688!
322
      metaErr(TD_VID(pMeta->pVnode), code);
×
323
      taosArrayDestroy(childTables);
×
324
      return code;
×
325
    }
326

327
    STagIdxKey *pTagIdxKey = NULL;
688✔
328
    int32_t     tagIdxKeySize = 0;
688✔
329

330
    if (action == ADD_INDEX) {
688✔
331
      code = metaFetchTagIdxKey(pMeta, pChildEntry, pNewColumn, &pTagIdxKey, &tagIdxKeySize);
319✔
332
      if (code) {
319!
333
        metaErr(TD_VID(pMeta->pVnode), code);
×
334
        taosArrayDestroy(childTables);
×
335
        metaFetchEntryFree(&pChildEntry);
×
336
        return code;
×
337
      }
338

339
      code = tdbTbInsert(pMeta->pTagIdx, pTagIdxKey, tagIdxKeySize, NULL, 0, pMeta->txn);
319✔
340
      if (code) {
319!
341
        metaErr(TD_VID(pMeta->pVnode), code);
×
342
        taosArrayDestroy(childTables);
×
343
        metaFetchEntryFree(&pChildEntry);
×
344
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
345
        return code;
×
346
      }
347
    } else {
348
      code = metaFetchTagIdxKey(pMeta, pChildEntry, pOldColumn, &pTagIdxKey, &tagIdxKeySize);
369✔
349
      if (code) {
369!
350
        metaErr(TD_VID(pMeta->pVnode), code);
×
351
        taosArrayDestroy(childTables);
×
352
        metaFetchEntryFree(&pChildEntry);
×
353
        return code;
×
354
      }
355

356
      code = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, tagIdxKeySize, pMeta->txn);
369✔
357
      if (code) {
369!
358
        metaErr(TD_VID(pMeta->pVnode), code);
×
359
        taosArrayDestroy(childTables);
×
360
        metaFetchEntryFree(&pChildEntry);
×
361
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
362
        return code;
×
363
      }
364
    }
365

366
    metaFetchTagIdxKeyFree(&pTagIdxKey);
688✔
367
    metaFetchEntryFree(&pChildEntry);
688✔
368
  }
369

370
  taosArrayDestroy(childTables);
143✔
371
  return code;
144✔
372
}
373

374
static int32_t metaAddOrDropColumnIndexOfVirtualSuperTable(SMeta *pMeta, const SMetaHandleParam *pParam,
344✔
375
                                                           const SSchema *pOldColumn, const SSchema *pNewColumn) {
376
  int32_t code = TSDB_CODE_SUCCESS;
344✔
377

378
  const SMetaEntry *pEntry = pParam->pEntry;
344✔
379
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
344✔
380
  enum { ADD_COLUMN, DROP_COLUMN } action;
381

382
  if (pOldColumn && pNewColumn) {
344✔
383
    return TSDB_CODE_SUCCESS;
331✔
384
  } else if (pOldColumn) {
13✔
385
    action = DROP_COLUMN;
6✔
386
  } else {
387
    action = ADD_COLUMN;
7✔
388
  }
389

390
  // fetch all child tables
391
  SArray *childTables = 0;
13✔
392
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childTables);
13✔
393
  if (code) {
14!
394
    metaErr(TD_VID(pMeta->pVnode), code);
×
395
    return code;
×
396
  }
397

398
  // do drop or add index
399
  for (int32_t i = 0; i < taosArrayGetSize(childTables); i++) {
29✔
400
    int64_t uid = *(int64_t *)taosArrayGet(childTables, i);
15✔
401

402
    // fetch child entry
403
    SMetaEntry *pChildEntry = NULL;
15✔
404
    code = metaFetchEntryByUid(pMeta, uid, &pChildEntry);
15✔
405
    if (code) {
15!
406
      metaErr(TD_VID(pMeta->pVnode), code);
×
407
      taosArrayDestroy(childTables);
×
408
      return code;
×
409
    }
410

411
    SMetaHandleParam param = {.pEntry = pChildEntry};
15✔
412

413
    if (action == ADD_COLUMN) {
15✔
414
      code = updataTableColRef(&pChildEntry->colRef, pNewColumn, 1, NULL);
9✔
415
      if (code) {
9!
416
        metaErr(TD_VID(pMeta->pVnode), code);
×
417
        taosArrayDestroy(childTables);
×
418
        metaFetchEntryFree(&pChildEntry);
×
419
        return code;
×
420
      }
421

422
      code = metaEntryTableUpdate(pMeta, &param);
9✔
423
      if (code) {
9!
424
        metaErr(TD_VID(pMeta->pVnode), code);
×
425
        taosArrayDestroy(childTables);
×
426
        metaFetchEntryFree(&pChildEntry);
×
427
        return code;
×
428
      }
429
    } else {
430
      code = updataTableColRef(&pChildEntry->colRef, pOldColumn, 0, NULL);
6✔
431
      if (code) {
6!
432
        metaErr(TD_VID(pMeta->pVnode), code);
×
433
        taosArrayDestroy(childTables);
×
434
        metaFetchEntryFree(&pChildEntry);
×
435
        return code;
×
436
      }
437

438
      code = metaEntryTableUpdate(pMeta, &param);
6✔
439
      if (code) {
6!
440
        metaErr(TD_VID(pMeta->pVnode), code);
×
441
        taosArrayDestroy(childTables);
×
442
        metaFetchEntryFree(&pChildEntry);
×
443
        return code;
×
444
      }
445
    }
446
    metaFetchEntryFree(&pChildEntry);
15✔
447
  }
448

449
  taosArrayDestroy(childTables);
14✔
450
  return code;
14✔
451
}
452

453
static int32_t metaUpdateSuperTableTagSchema(SMeta *pMeta, const SMetaHandleParam *pParam) {
4,382✔
454
  int32_t               code = TSDB_CODE_SUCCESS;
4,382✔
455
  const SMetaEntry     *pEntry = pParam->pEntry;
4,382✔
456
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
4,382✔
457
  const SSchemaWrapper *pNewTagSchema = &pEntry->stbEntry.schemaTag;
4,382✔
458
  const SSchemaWrapper *pOldTagSchema = &pOldEntry->stbEntry.schemaTag;
4,382✔
459

460
  int32_t iOld = 0, iNew = 0;
4,382✔
461
  for (; iOld < pOldTagSchema->nCols && iNew < pNewTagSchema->nCols;) {
103,709✔
462
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
99,326✔
463
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
99,326✔
464

465
    if (pOldColumn->colId == pNewColumn->colId) {
99,326✔
466
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
98,554✔
467
      if (code) {
98,578✔
468
        metaErr(TD_VID(pMeta->pVnode), code);
25!
469
        return code;
×
470
      }
471

472
      iOld++;
98,553✔
473
      iNew++;
98,553✔
474
    } else if (pOldColumn->colId < pNewColumn->colId) {
772!
475
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
773✔
476
      if (code) {
774!
477
        metaErr(TD_VID(pMeta->pVnode), code);
×
478
        return code;
×
479
      }
480

481
      iOld++;
774✔
482
    } else {
483
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, NULL, pNewColumn);
×
484
      if (code) {
×
485
        metaErr(TD_VID(pMeta->pVnode), code);
×
486
        return code;
×
487
      }
488

489
      iNew++;
×
490
    }
491
  }
492

493
  for (; iOld < pOldTagSchema->nCols; iOld++) {
4,611✔
494
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
227✔
495
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
227✔
496
    if (code) {
228!
497
      metaErr(TD_VID(pMeta->pVnode), code);
×
498
      return code;
×
499
    }
500
  }
501

502
  for (; iNew < pNewTagSchema->nCols; iNew++) {
5,871✔
503
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
1,480✔
504
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, NULL, pNewColumn);
1,480✔
505
    if (code) {
1,479!
506
      metaErr(TD_VID(pMeta->pVnode), code);
×
507
      return code;
×
508
    }
509
  }
510

511
  return code;
4,391✔
512
}
513

514
static int32_t metaUpdateSuperTableRowSchema(SMeta *pMeta, const SMetaHandleParam *pParam) {
17✔
515
  int32_t               code = TSDB_CODE_SUCCESS;
17✔
516
  const SMetaEntry     *pEntry = pParam->pEntry;
17✔
517
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
17✔
518
  const SSchemaWrapper *pNewRowSchema = &pEntry->stbEntry.schemaRow;
17✔
519
  const SSchemaWrapper *pOldRowSchema = &pOldEntry->stbEntry.schemaRow;
17✔
520

521
  int32_t iOld = 0, iNew = 0;
17✔
522
  for (; iOld < pOldRowSchema->nCols && iNew < pNewRowSchema->nCols;) {
351✔
523
    SSchema *pOldColumn = pOldRowSchema->pSchema + iOld;
334✔
524
    SSchema *pNewColumn = pNewRowSchema->pSchema + iNew;
334✔
525

526
    if (pOldColumn->colId == pNewColumn->colId) {
334✔
527
      code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
330✔
528
      if (code) {
330!
529
        metaErr(TD_VID(pMeta->pVnode), code);
×
530
        return code;
×
531
      }
532

533
      iOld++;
330✔
534
      iNew++;
330✔
535
    } else if (pOldColumn->colId < pNewColumn->colId) {
4!
536
      code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, pOldColumn, NULL);
4✔
537
      if (code) {
4!
538
        metaErr(TD_VID(pMeta->pVnode), code);
×
539
        return code;
×
540
      }
541

542
      iOld++;
4✔
543
    } else {
544
      code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, NULL, pNewColumn);
×
545
      if (code) {
×
546
        metaErr(TD_VID(pMeta->pVnode), code);
×
547
        return code;
×
548
      }
549

550
      iNew++;
×
551
    }
552
  }
553

554
  for (; iOld < pOldRowSchema->nCols; iOld++) {
19✔
555
    SSchema *pOldColumn = pOldRowSchema->pSchema + iOld;
2✔
556
    code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, pOldColumn, NULL);
2✔
557
    if (code) {
2!
558
      metaErr(TD_VID(pMeta->pVnode), code);
×
559
      return code;
×
560
    }
561
  }
562

563
  for (; iNew < pNewRowSchema->nCols; iNew++) {
25✔
564
    SSchema *pNewColumn = pNewRowSchema->pSchema + iNew;
8✔
565
    code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, NULL, pNewColumn);
8✔
566
    if (code) {
8!
567
      metaErr(TD_VID(pMeta->pVnode), code);
×
568
      return code;
×
569
    }
570
  }
571

572
  return code;
17✔
573
}
574

575
static int32_t metaSchemaTableUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
65,364✔
576
  int32_t code = TSDB_CODE_SUCCESS;
65,364✔
577

578
  const SMetaEntry *pEntry = pParam->pEntry;
65,364✔
579
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
65,364✔
580

581
  if (NULL == pOldEntry) {
65,364✔
582
    return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
43,218✔
583
  }
584

585
  if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
22,146✔
586
    // check row schema
587
    if (pOldEntry->ntbEntry.schemaRow.version != pEntry->ntbEntry.schemaRow.version) {
5,538!
588
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
5,565✔
589
    }
590
  } else if (pEntry->type == TSDB_SUPER_TABLE) {
16,608!
591
    // check row schema
592
    if (pOldEntry->stbEntry.schemaRow.version != pEntry->stbEntry.schemaRow.version) {
16,616✔
593
      if (TABLE_IS_VIRTUAL(pEntry->flags)) {
12,211✔
594
        return metaUpdateSuperTableRowSchema(pMeta, pParam);
17✔
595
      } else {
596
        return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
12,194✔
597
      }
598
    }
599

600
    // check tag schema
601
    code = metaUpdateSuperTableTagSchema(pMeta, pParam);
4,405✔
602
    if (code) {
4,392!
603
      metaErr(TD_VID(pMeta->pVnode), code);
×
604
      return code;
×
605
    }
606

607
  } else {
608
    return TSDB_CODE_INVALID_PARA;
×
609
  }
610

611
  return TSDB_CODE_SUCCESS;
4,432✔
612
}
613

614
static int32_t metaSchemaTableDelete(SMeta *pMeta, const SMetaHandleParam *pEntry) {
×
615
  // TODO
616
  return TSDB_CODE_SUCCESS;
×
617
}
618

619
// Uid Index
620
static void metaBuildEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) {
259,525✔
621
  pInfo->uid = pEntry->uid;
259,525✔
622
  pInfo->version = pEntry->version;
259,525✔
623
  if (pEntry->type == TSDB_SUPER_TABLE) {
259,525✔
624
    pInfo->suid = pEntry->uid;
44,184✔
625
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
44,184✔
626
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
215,341✔
627
    pInfo->suid = pEntry->ctbEntry.suid;
194,181✔
628
    pInfo->skmVer = 0;
194,181✔
629
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
21,160!
630
    pInfo->suid = 0;
21,160✔
631
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
21,160✔
632
  }
633
}
259,525✔
634

635
static int32_t metaUidIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
259,537✔
636
  int32_t code = TSDB_CODE_SUCCESS;
259,537✔
637
  int32_t vgId = TD_VID(pMeta->pVnode);
259,537✔
638

639
  const SMetaEntry *pEntry = pParam->pEntry;
259,537✔
640

641
  // update cache
642
  SMetaInfo info = {0};
259,537✔
643
  metaBuildEntryInfo(pEntry, &info);
259,537✔
644
  code = metaCacheUpsert(pMeta, &info);
259,595✔
645
  if (code) {
259,435!
646
    metaErr(vgId, code);
×
647
  }
648

649
  // put to tdb
650
  SUidIdxVal value = {
259,372✔
651
      .suid = info.suid,
259,372✔
652
      .skmVer = info.skmVer,
259,372✔
653
      .version = pEntry->version,
259,372✔
654
  };
655
  if (META_TABLE_OP_INSERT == op) {
259,372✔
656
    code = tdbTbInsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
223,389✔
657
  } else if (META_TABLE_OP_UPDATA == op) {
35,983!
658
    code = tdbTbUpsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
35,984✔
659
  }
660
  return code;
259,777✔
661
}
662

663
static int32_t metaUidIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
223,581✔
664
  return metaUidIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
223,581✔
665
}
666

667
static int32_t metaUidIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
35,981✔
668
  return metaUidIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
35,981✔
669
}
670

671
static int32_t metaUidIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
20,379✔
672
  int32_t code = 0;
20,379✔
673

674
  const SMetaEntry *pEntry = pParam->pOldEntry;
20,379✔
675

676
  // delete tdb
677
  code = tdbTbDelete(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), pMeta->txn);
20,379✔
678
  if (code) {
20,381!
679
    metaErr(TD_VID(pMeta->pVnode), code);
×
680
  }
681

682
  // delete cache
683
  (void)metaCacheDrop(pMeta, pEntry->uid);
20,381✔
684
  return code;
20,381✔
685
}
686

687
// Name Index
688
static int32_t metaNameIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
223,617✔
689
  int32_t code = TSDB_CODE_SUCCESS;
223,617✔
690

691
  const SMetaEntry *pEntry = pParam->pEntry;
223,617✔
692

693
  if (META_TABLE_OP_INSERT == op) {
223,617!
694
    code = tdbTbInsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
223,654✔
695
                       pMeta->txn);
696
  } else if (META_TABLE_OP_UPDATA == op) {
×
697
    code = tdbTbUpsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
×
698
                       pMeta->txn);
699
  } else {
700
    code = TSDB_CODE_INVALID_PARA;
×
701
  }
702
  if (code) {
223,701!
703
    metaErr(TD_VID(pMeta->pVnode), code);
×
704
  }
705
  return code;
223,680✔
706
}
707

708
static int32_t metaNameIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
223,617✔
709
  int32_t code = TSDB_CODE_SUCCESS;
223,617✔
710
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
223,617✔
711
}
712

713
static int32_t metaNameIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
714
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
×
715
}
716

717
static int32_t metaNameIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
20,380✔
718
  int32_t code = TSDB_CODE_SUCCESS;
20,380✔
719

720
  const SMetaEntry *pEntry = pParam->pOldEntry;
20,380✔
721
  code = tdbTbDelete(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, pMeta->txn);
20,380✔
722
  if (code) {
20,380!
723
    metaErr(TD_VID(pMeta->pVnode), code);
×
724
  }
725
  return code;
20,379✔
726
}
727

728
// Suid Index
729
static int32_t metaSUidIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
27,635✔
730
  const SMetaEntry *pEntry = pParam->pEntry;
27,635✔
731

732
  int32_t code = tdbTbInsert(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), NULL, 0, pMeta->txn);
27,635✔
733
  if (code) {
27,717!
734
    metaErr(TD_VID(pMeta->pVnode), code);
×
735
  }
736
  return code;
27,675✔
737
}
738

739
static int32_t metaSUidIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
2,339✔
740
  const SMetaEntry *pEntry = pParam->pOldEntry;
2,339✔
741

742
  int32_t code = tdbTbDelete(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), pMeta->txn);
2,339✔
743
  if (code) {
2,341!
744
    metaErr(TD_VID(pMeta->pVnode), code);
×
745
  }
746
  return code;
2,340✔
747
}
748

749
// Child Index
750
static int32_t metaChildIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
193,527✔
751
  int32_t code = TSDB_CODE_SUCCESS;
193,527✔
752

753
  const SMetaEntry *pEntry = pParam->pEntry;
193,527✔
754

755
  SCtbIdxKey key = {
193,527✔
756
      .suid = pEntry->ctbEntry.suid,
193,527✔
757
      .uid = pEntry->uid,
193,527✔
758
  };
759

760
  if (META_TABLE_OP_INSERT == op) {
193,527✔
761
    code = tdbTbInsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
180,470✔
762
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
180,470✔
763
  } else if (META_TABLE_OP_UPDATA == op) {
13,057!
764
    code = tdbTbUpsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
13,077✔
765
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
13,077✔
766
  } else {
767
    code = TSDB_CODE_INVALID_PARA;
×
768
  }
769
  return code;
193,562✔
770
}
771

772
static int32_t metaChildIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
180,454✔
773
  return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
180,454✔
774
}
775

776
static int32_t metaChildIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
13,806✔
777
  const SMetaEntry *pEntry = pParam->pEntry;
13,806✔
778
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
13,806✔
779
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
13,806✔
780

781
  const STag *pNewTags = (const STag *)pEntry->ctbEntry.pTags;
13,806✔
782
  const STag *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags;
13,806✔
783
  if (pNewTags->len != pOldTags->len || memcmp(pNewTags, pOldTags, pNewTags->len)) {
13,806✔
784
    return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
13,077✔
785
  }
786
  return 0;
729✔
787
}
788

789
static int32_t metaChildIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
15,396✔
790
  const SMetaEntry *pEntry = pParam->pOldEntry;
15,396✔
791

792
  SCtbIdxKey key = {
15,396✔
793
      .suid = pEntry->ctbEntry.suid,
15,396✔
794
      .uid = pEntry->uid,
15,396✔
795
  };
796
  return tdbTbDelete(pMeta->pCtbIdx, &key, sizeof(key), pMeta->txn);
15,396✔
797
}
798

799
// Tag Index
800
static int32_t metaFetchTagIdxKey(SMeta *pMeta, const SMetaEntry *pEntry, const SSchema *pTagColumn,
222,053✔
801
                                  STagIdxKey **ppTagIdxKey, int32_t *pTagIdxKeySize) {
802
  int32_t code = TSDB_CODE_SUCCESS;
222,053✔
803

804
  STagIdxKey *pTagIdxKey = NULL;
222,053✔
805
  int32_t     nTagIdxKey;
806
  const void *pTagData = NULL;
222,053✔
807
  int32_t     nTagData = 0;
222,053✔
808

809
  STagVal tagVal = {
222,053✔
810
      .cid = pTagColumn->colId,
222,053✔
811
  };
812

813
  if (tTagGet((const STag *)pEntry->ctbEntry.pTags, &tagVal)) {
222,053✔
814
    if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
209,826!
815
      pTagData = tagVal.pData;
32,967✔
816
      nTagData = (int32_t)tagVal.nData;
32,967✔
817
    } else {
818
      pTagData = &(tagVal.i64);
176,859✔
819
      nTagData = tDataTypes[pTagColumn->type].bytes;
176,859✔
820
    }
821
  } else {
822
    if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
12,245!
823
      nTagData = tDataTypes[pTagColumn->type].bytes;
11,758✔
824
    }
825
  }
826

827
  code = metaCreateTagIdxKey(pEntry->ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type,
222,071✔
828
                             pEntry->uid, &pTagIdxKey, &nTagIdxKey);
222,071✔
829
  if (code) {
222,070✔
830
    metaErr(TD_VID(pMeta->pVnode), code);
29!
831
    return code;
×
832
  }
833

834
  *ppTagIdxKey = pTagIdxKey;
222,041✔
835
  *pTagIdxKeySize = nTagIdxKey;
222,041✔
836
  return code;
222,041✔
837
}
838

839
static void metaFetchTagIdxKeyFree(STagIdxKey **ppTagIdxKey) {
222,078✔
840
  metaDestroyTagIdxKey(*ppTagIdxKey);
222,078✔
841
  *ppTagIdxKey = NULL;
222,081✔
842
}
222,081✔
843

844
static int32_t metaTagIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
180,462✔
845
  int32_t code = TSDB_CODE_SUCCESS;
180,462✔
846

847
  const SMetaEntry *pEntry = pParam->pEntry;
180,462✔
848
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
180,462✔
849

850
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
180,462✔
851
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
181,178✔
852
    const SSchema *pTagColumn = &pTagSchema->pSchema[0];
716✔
853

854
    STagVal tagVal = {
716✔
855
        .cid = pTagColumn->colId,
716✔
856
    };
857

858
    const void *pTagData = pEntry->ctbEntry.pTags;
716✔
859
    int32_t     nTagData = ((const STag *)pEntry->ctbEntry.pTags)->len;
716✔
860
    code = metaSaveJsonVarToIdx(pMeta, pEntry, pTagColumn);
716✔
861
    if (code) {
716!
862
      metaErr(TD_VID(pMeta->pVnode), code);
×
863
    }
864
  } else {
865
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
839,938✔
866
      STagIdxKey    *pTagIdxKey = NULL;
660,183✔
867
      int32_t        nTagIdxKey;
868
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
660,183✔
869

870
      if (!IS_IDX_ON(pTagColumn)) {
660,183✔
871
        continue;
480,327✔
872
      }
873

874
      code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pTagIdxKey, &nTagIdxKey);
179,856✔
875
      if (code) {
179,830!
876
        metaErr(TD_VID(pMeta->pVnode), code);
×
877
        return code;
×
878
      }
879

880
      code = tdbTbInsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn);
179,830✔
881
      if (code) {
179,865!
882
        metaErr(TD_VID(pMeta->pVnode), code);
×
883
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
884
        return code;
×
885
      }
886
      metaFetchTagIdxKeyFree(&pTagIdxKey);
179,865✔
887
    }
888
  }
889
  return code;
180,471✔
890
}
891

892
static int32_t metaTagIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
13,806✔
893
  int32_t code = TSDB_CODE_SUCCESS;
13,806✔
894

895
  const SMetaEntry     *pEntry = pParam->pEntry;
13,806✔
896
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
13,806✔
897
  const SMetaEntry     *pSuperEntry = pParam->pSuperEntry;
13,806✔
898
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
13,806✔
899
  const STag           *pNewTags = (const STag *)pEntry->ctbEntry.pTags;
13,806✔
900
  const STag           *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags;
13,806✔
901

902
  if (pNewTags->len == pOldTags->len && !memcmp(pNewTags, pOldTags, pNewTags->len)) {
13,806✔
903
    return code;
729✔
904
  }
905

906
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
13,077✔
907
    code = metaDelJsonVarFromIdx(pMeta, pOldEntry, &pTagSchema->pSchema[0]);
8✔
908
    if (code) {
8!
909
      metaErr(TD_VID(pMeta->pVnode), code);
×
910
      return code;
×
911
    }
912

913
    code = metaSaveJsonVarToIdx(pMeta, pEntry, &pTagSchema->pSchema[0]);
8✔
914
    if (code) {
8!
915
      metaErr(TD_VID(pMeta->pVnode), code);
×
916
      return code;
×
917
    }
918
  } else {
919
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
31,199✔
920
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
18,130✔
921

922
      if (!IS_IDX_ON(pTagColumn)) {
18,130✔
923
        continue;
5,061✔
924
      }
925

926
      STagIdxKey *pOldTagIdxKey = NULL;
13,069✔
927
      int32_t     oldTagIdxKeySize = 0;
13,069✔
928
      STagIdxKey *pNewTagIdxKey = NULL;
13,069✔
929
      int32_t     newTagIdxKeySize = 0;
13,069✔
930

931
      code = metaFetchTagIdxKey(pMeta, pOldEntry, pTagColumn, &pOldTagIdxKey, &oldTagIdxKeySize);
13,069✔
932
      if (code) {
13,069!
933
        metaErr(TD_VID(pMeta->pVnode), code);
×
934
        return code;
×
935
      }
936

937
      code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pNewTagIdxKey, &newTagIdxKeySize);
13,069✔
938
      if (code) {
13,069!
939
        metaErr(TD_VID(pMeta->pVnode), code);
×
940
        metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
941
        return code;
×
942
      }
943

944
      if (tagIdxKeyCmpr(pOldTagIdxKey, oldTagIdxKeySize, pNewTagIdxKey, newTagIdxKeySize)) {
13,069✔
945
        code = tdbTbDelete(pMeta->pTagIdx, pOldTagIdxKey, oldTagIdxKeySize, pMeta->txn);
11,266✔
946
        if (code) {
11,266!
947
          metaErr(TD_VID(pMeta->pVnode), code);
×
948
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
949
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
950
          return code;
×
951
        }
952

953
        code = tdbTbInsert(pMeta->pTagIdx, pNewTagIdxKey, newTagIdxKeySize, NULL, 0, pMeta->txn);
11,266✔
954
        if (code) {
11,266!
955
          metaErr(TD_VID(pMeta->pVnode), code);
×
956
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
957
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
958
          return code;
×
959
        }
960
      }
961

962
      metaFetchTagIdxKeyFree(&pOldTagIdxKey);
13,069✔
963
      metaFetchTagIdxKeyFree(&pNewTagIdxKey);
13,069✔
964
    }
965
  }
966
  return code;
13,077✔
967
}
968

969
static int32_t metaTagIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
15,396✔
970
  int32_t code = TSDB_CODE_SUCCESS;
15,396✔
971

972
  const SMetaEntry     *pEntry = pParam->pEntry;
15,396✔
973
  const SMetaEntry     *pChild = pParam->pOldEntry;
15,396✔
974
  const SMetaEntry     *pSuper = pParam->pSuperEntry;
15,396✔
975
  const SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
15,396✔
976
  const SSchema        *pTagColumn = NULL;
15,396✔
977
  const STag           *pTags = (const STag *)pChild->ctbEntry.pTags;
15,396✔
978

979
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
15,396✔
980
    pTagColumn = &pTagSchema->pSchema[0];
197✔
981
    code = metaDelJsonVarFromIdx(pMeta, pChild, pTagColumn);
197✔
982
    if (code) {
197!
983
      metaErr(TD_VID(pMeta->pVnode), code);
×
984
    }
985
  } else {
986
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
49,866✔
987
      pTagColumn = &pTagSchema->pSchema[i];
34,665✔
988
      if (!IS_IDX_ON(pTagColumn)) {
34,665✔
989
        continue;
19,277✔
990
      }
991

992
      STagIdxKey *pTagIdxKey = NULL;
15,388✔
993
      int32_t     nTagIdxKey;
994

995
      code = metaFetchTagIdxKey(pMeta, pChild, pTagColumn, &pTagIdxKey, &nTagIdxKey);
15,388✔
996
      if (code) {
15,389!
997
        metaErr(TD_VID(pMeta->pVnode), code);
×
998
        return code;
×
999
      }
1000

1001
      code = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
15,389✔
1002
      if (code) {
15,390!
1003
        metaErr(TD_VID(pMeta->pVnode), code);
×
1004
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
1005
        return code;
×
1006
      }
1007
      metaFetchTagIdxKeyFree(&pTagIdxKey);
15,390✔
1008
    }
1009
  }
1010
  return code;
15,398✔
1011
}
1012

1013
// Btime Index
1014
static int32_t metaBtimeIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
214,021✔
1015
  int32_t code = TSDB_CODE_SUCCESS;
214,021✔
1016

1017
  const SMetaEntry *pEntry;
1018
  if (META_TABLE_OP_DELETE == op) {
214,021✔
1019
    pEntry = pParam->pOldEntry;
18,040✔
1020
  } else {
1021
    pEntry = pParam->pEntry;
195,981✔
1022
  }
1023

1024
  SBtimeIdxKey key = {
214,021✔
1025
      .uid = pEntry->uid,
214,021✔
1026
  };
1027

1028
  if (TSDB_CHILD_TABLE == pEntry->type || TSDB_VIRTUAL_CHILD_TABLE == pEntry->type) {
214,021✔
1029
    key.btime = pEntry->ctbEntry.btime;
195,837✔
1030
  } else if (TSDB_NORMAL_TABLE == pEntry->type || TSDB_VIRTUAL_NORMAL_TABLE == pEntry->type) {
18,184!
1031
    key.btime = pEntry->ntbEntry.btime;
18,184✔
1032
  } else {
1033
    return TSDB_CODE_INVALID_PARA;
×
1034
  }
1035

1036
  if (META_TABLE_OP_INSERT == op) {
214,021✔
1037
    code = tdbTbInsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
195,985✔
1038
  } else if (META_TABLE_OP_UPDATA == op) {
18,036!
1039
    code = tdbTbUpsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
×
1040
  } else if (META_TABLE_OP_DELETE == op) {
18,036!
1041
    code = tdbTbDelete(pMeta->pBtimeIdx, &key, sizeof(key), pMeta->txn);
18,040✔
1042
  } else {
UNCOV
1043
    code = TSDB_CODE_INVALID_PARA;
×
1044
  }
1045
  if (code) {
214,062!
1046
    metaErr(TD_VID(pMeta->pVnode), code);
×
1047
  }
1048
  return code;
214,051✔
1049
}
1050

1051
static int32_t metaBtimeIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
195,980✔
1052
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
195,980✔
1053
}
1054

1055
static int32_t metaBtimeIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
1056
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
×
1057
}
1058

1059
static int32_t metaBtimeIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
18,040✔
1060
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_DELETE);
18,040✔
1061
}
1062

1063
// TTL Index
1064
static int32_t metaTtlIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
195,669✔
1065
  const SMetaEntry *pEntry = pParam->pEntry;
195,669✔
1066

1067
  STtlUpdTtlCtx ctx = {
195,669✔
1068
      .uid = pEntry->uid,
195,669✔
1069
      .pTxn = pMeta->txn,
195,669✔
1070
  };
1071
  if (TSDB_CHILD_TABLE == pEntry->type) {
195,669✔
1072
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
180,281✔
1073
    ctx.changeTimeMs = pEntry->ctbEntry.btime;
180,281✔
1074
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
15,388!
1075
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
15,391✔
1076
    ctx.changeTimeMs = pEntry->ntbEntry.btime;
15,391✔
1077
  } else {
UNCOV
1078
    return TSDB_CODE_INVALID_PARA;
×
1079
  }
1080

1081
  int32_t ret = ttlMgrInsertTtl(pMeta->pTtlMgr, &ctx);
195,672✔
1082
  if (ret < 0) {
195,632!
1083
    metaError("vgId:%d, failed to insert ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid, tstrerror(ret));
×
1084
  }
1085
  return TSDB_CODE_SUCCESS;
195,634✔
1086
}
1087

1088
static int32_t metaTtlIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
195,655✔
1089
  return metaTtlIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
195,655✔
1090
}
1091

1092
static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam);
1093

1094
static int32_t metaTtlIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
19,085✔
1095
  int32_t code = TSDB_CODE_SUCCESS;
19,085✔
1096

1097
  const SMetaEntry *pEntry = pParam->pEntry;
19,085✔
1098
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
19,085✔
1099

1100
  if ((pEntry->type == TSDB_CHILD_TABLE && pOldEntry->ctbEntry.ttlDays != pEntry->ctbEntry.ttlDays) ||
19,085✔
1101
      (pEntry->type == TSDB_NORMAL_TABLE && pOldEntry->ntbEntry.ttlDays != pEntry->ntbEntry.ttlDays)) {
19,070✔
1102
    code = metaTtlIdxDelete(pMeta, pParam);
31✔
1103
    if (code) {
31!
1104
      metaErr(TD_VID(pMeta->pVnode), code);
×
1105
    }
1106

1107
    code = metaTtlIdxInsert(pMeta, pParam);
31✔
1108
    if (code) {
31!
1109
      metaErr(TD_VID(pMeta->pVnode), code);
×
1110
    }
1111
  }
1112

1113
  return TSDB_CODE_SUCCESS;
19,085✔
1114
}
1115

1116
static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
18,020✔
1117
  int32_t code = TSDB_CODE_SUCCESS;
18,020✔
1118

1119
  const SMetaEntry *pEntry = pParam->pOldEntry;
18,020✔
1120
  STtlDelTtlCtx     ctx = {
18,020✔
1121
          .uid = pEntry->uid,
18,020✔
1122
          .pTxn = pMeta->txn,
18,020✔
1123
  };
1124

1125
  if (TSDB_CHILD_TABLE == pEntry->type) {
18,020✔
1126
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
15,371✔
1127
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
2,649✔
1128
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
2,613✔
1129
  } else {
1130
    code = TSDB_CODE_INVALID_PARA;
36✔
1131
  }
1132

1133
  if (TSDB_CODE_SUCCESS == code) {
18,020✔
1134
    int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
17,984✔
1135
    if (ret < 0) {
17,984!
1136
      metaError("vgId:%d, failed to delete ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid,
×
1137
                tstrerror(ret));
1138
    }
1139
  }
1140
  return code;
18,020✔
1141
}
1142

1143
static void metaTimeSeriesNotifyCheck(SMeta *pMeta) {
208,698✔
1144
#if defined(TD_ENTERPRISE)
1145
  int64_t nTimeSeries = metaGetTimeSeriesNum(pMeta, 0);
208,698✔
1146
  int64_t deltaTS = nTimeSeries - pMeta->pVnode->config.vndStats.numOfReportedTimeSeries;
208,760✔
1147
  if (deltaTS > tsTimeSeriesThreshold) {
208,760✔
1148
    if (0 == atomic_val_compare_exchange_8(&dmNotifyHdl.state, 1, 2)) {
125,113✔
1149
      if (tsem_post(&dmNotifyHdl.sem) != 0) {
125,096!
1150
        metaError("vgId:%d, failed to post semaphore, errno:%d", TD_VID(pMeta->pVnode), ERRNO);
×
1151
      }
1152
    }
1153
  }
1154
#endif
1155
}
208,782✔
1156

1157
static int32_t (*metaTableOpFn[META_TABLE_MAX][META_TABLE_OP_MAX])(SMeta *pMeta, const SMetaHandleParam *pParam) =
1158
    {
1159
        [META_ENTRY_TABLE] =
1160
            {
1161
                [META_TABLE_OP_INSERT] = metaEntryTableInsert,
1162
                [META_TABLE_OP_UPDATA] = metaEntryTableUpdate,
1163
                [META_TABLE_OP_DELETE] = metaEntryTableDelete,
1164
            },
1165
        [META_SCHEMA_TABLE] =
1166
            {
1167
                [META_TABLE_OP_INSERT] = metaSchemaTableInsert,
1168
                [META_TABLE_OP_UPDATA] = metaSchemaTableUpdate,
1169
                [META_TABLE_OP_DELETE] = metaSchemaTableDelete,
1170
            },
1171
        [META_UID_IDX] =
1172
            {
1173
                [META_TABLE_OP_INSERT] = metaUidIdxInsert,
1174
                [META_TABLE_OP_UPDATA] = metaUidIdxUpdate,
1175
                [META_TABLE_OP_DELETE] = metaUidIdxDelete,
1176
            },
1177
        [META_NAME_IDX] =
1178
            {
1179
                [META_TABLE_OP_INSERT] = metaNameIdxInsert,
1180
                [META_TABLE_OP_UPDATA] = metaNameIdxUpdate,
1181
                [META_TABLE_OP_DELETE] = metaNameIdxDelete,
1182
            },
1183
        [META_SUID_IDX] =
1184
            {
1185
                [META_TABLE_OP_INSERT] = metaSUidIdxInsert,
1186
                [META_TABLE_OP_UPDATA] = NULL,
1187
                [META_TABLE_OP_DELETE] = metaSUidIdxDelete,
1188
            },
1189
        [META_CHILD_IDX] =
1190
            {
1191
                [META_TABLE_OP_INSERT] = metaChildIdxInsert,
1192
                [META_TABLE_OP_UPDATA] = metaChildIdxUpdate,
1193
                [META_TABLE_OP_DELETE] = metaChildIdxDelete,
1194
            },
1195
        [META_TAG_IDX] =
1196
            {
1197
                [META_TABLE_OP_INSERT] = metaTagIdxInsert,
1198
                [META_TABLE_OP_UPDATA] = metaTagIdxUpdate,
1199
                [META_TABLE_OP_DELETE] = metaTagIdxDelete,
1200
            },
1201
        [META_BTIME_IDX] =
1202
            {
1203
                [META_TABLE_OP_INSERT] = metaBtimeIdxInsert,
1204
                [META_TABLE_OP_UPDATA] = metaBtimeIdxUpdate,
1205
                [META_TABLE_OP_DELETE] = metaBtimeIdxDelete,
1206
            },
1207
        [META_TTL_IDX] =
1208
            {
1209
                [META_TABLE_OP_INSERT] = metaTtlIdxInsert,
1210
                [META_TABLE_OP_UPDATA] = metaTtlIdxUpdate,
1211
                [META_TABLE_OP_DELETE] = metaTtlIdxDelete,
1212
            },
1213
};
1214

1215
static int32_t metaHandleSuperTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
27,732✔
1216
  int32_t code = TSDB_CODE_SUCCESS;
27,732✔
1217

1218
  SMetaTableOp ops[] = {
27,732✔
1219
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1220
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: here should be insert
1221
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1222
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1223
      {META_SUID_IDX, META_TABLE_OP_INSERT},      //
1224
  };
1225

1226
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
166,062✔
1227
    SMetaTableOp          *op = &ops[i];
138,401✔
1228
    const SMetaHandleParam param = {
138,401✔
1229
        .pEntry = pEntry,
1230
    };
1231
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
138,401✔
1232
    if (TSDB_CODE_SUCCESS != code) {
138,267!
1233
      metaErr(TD_VID(pMeta->pVnode), code);
×
1234
      return code;
×
1235
    }
1236
  }
1237

1238
  return code;
27,661✔
1239
}
1240
static int32_t metaHandleSuperTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
26,898✔
1241
  int32_t code = TSDB_CODE_SUCCESS;
26,898✔
1242

1243
  metaWLock(pMeta);
26,898✔
1244
  code = metaHandleSuperTableCreateImpl(pMeta, pEntry);
27,749✔
1245
  metaULock(pMeta);
27,663✔
1246

1247
  if (TSDB_CODE_SUCCESS == code) {
27,739!
1248
    pMeta->pVnode->config.vndStats.numOfSTables++;
27,739✔
1249

1250
    metaInfo("vgId:%d, %s success, version:%" PRId64 " type:%d uid:%" PRId64 " name:%s", TD_VID(pMeta->pVnode),
27,739✔
1251
             __func__, pEntry->version, pEntry->type, pEntry->uid, pEntry->name);
1252
  } else {
1253
    metaErr(TD_VID(pMeta->pVnode), code);
×
1254
  }
1255
  return code;
27,753✔
1256
}
1257

1258
static int32_t metaHandleNormalTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
15,375✔
1259
  int32_t code = TSDB_CODE_SUCCESS;
15,375✔
1260

1261
  SMetaTableOp ops[] = {
15,375✔
1262
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1263
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: need to be insert
1264
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1265
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1266
      {META_BTIME_IDX, META_TABLE_OP_INSERT},     //
1267
      {META_TTL_IDX, META_TABLE_OP_INSERT},       //
1268
  };
1269

1270
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
107,625✔
1271
    SMetaTableOp *op = &ops[i];
92,250✔
1272

1273
    SMetaHandleParam param = {
92,250✔
1274
        .pEntry = pEntry,
1275
    };
1276

1277
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
92,250✔
1278
    if (TSDB_CODE_SUCCESS != code) {
92,250!
1279
      metaErr(TD_VID(pMeta->pVnode), code);
×
1280
      return code;
×
1281
    }
1282
  }
1283

1284
  return code;
15,375✔
1285
}
1286
static int32_t metaHandleNormalTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
15,375✔
1287
  int32_t code = TSDB_CODE_SUCCESS;
15,375✔
1288

1289
  // update TDB
1290
  metaWLock(pMeta);
15,375✔
1291
  code = metaHandleNormalTableCreateImpl(pMeta, pEntry);
15,375✔
1292
  metaULock(pMeta);
15,375✔
1293

1294
  // update other stuff
1295
  if (TSDB_CODE_SUCCESS == code) {
15,375!
1296
    pMeta->pVnode->config.vndStats.numOfNTables++;
15,375✔
1297
    pMeta->pVnode->config.vndStats.numOfNTimeSeries += pEntry->ntbEntry.schemaRow.nCols - 1;
15,375✔
1298

1299
    if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
15,375!
1300
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, -1, &pEntry->ntbEntry.schemaRow);
56✔
1301
      if (rc < 0) {
56!
1302
        metaError("vgId:%d, failed to create table:%s since %s", TD_VID(pMeta->pVnode), pEntry->name, tstrerror(rc));
×
1303
      }
1304
    }
1305
    metaTimeSeriesNotifyCheck(pMeta);
15,375✔
1306
  } else {
1307
    metaErr(TD_VID(pMeta->pVnode), code);
×
1308
  }
1309
  return code;
15,375✔
1310
}
1311

1312
static int32_t metaHandleChildTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry, const SMetaEntry *pSuperEntry) {
180,262✔
1313
  int32_t code = TSDB_CODE_SUCCESS;
180,262✔
1314

1315
  SMetaTableOp ops[] = {
180,262✔
1316
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},  //
1317
      {META_UID_IDX, META_TABLE_OP_INSERT},      //
1318
      {META_NAME_IDX, META_TABLE_OP_INSERT},     //
1319
      {META_CHILD_IDX, META_TABLE_OP_INSERT},    //
1320
      {META_TAG_IDX, META_TABLE_OP_INSERT},      //
1321
      {META_BTIME_IDX, META_TABLE_OP_INSERT},    //
1322
      {META_TTL_IDX, META_TABLE_OP_INSERT},      //
1323
  };
1324

1325
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1,441,090✔
1326
    SMetaTableOp *op = &ops[i];
1,261,005✔
1327

1328
    SMetaHandleParam param = {
1,261,005✔
1329
        .pEntry = pEntry,
1330
        .pSuperEntry = pSuperEntry,
1331
    };
1332

1333
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
1,261,005✔
1334
    if (TSDB_CODE_SUCCESS != code) {
1,260,783!
1335
      metaErr(TD_VID(pMeta->pVnode), code);
×
1336
      return code;
×
1337
    }
1338
  }
1339

1340
  if (TSDB_CODE_SUCCESS == code) {
180,085!
1341
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
180,248✔
1342
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
180,243✔
1343
    if (ret < 0) {
180,281!
1344
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1345
    }
1346

1347
    ret = metaTbGroupCacheClear(pMeta, pSuperEntry->uid);
180,281✔
1348
    if (ret < 0) {
180,266!
1349
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1350
    }
1351
  }
1352
  return code;
180,273✔
1353
}
1354

1355
static int32_t metaHandleChildTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
180,243✔
1356
  int32_t     code = TSDB_CODE_SUCCESS;
180,243✔
1357
  SMetaEntry *pSuperEntry = NULL;
180,243✔
1358

1359
  // get the super table entry
1360
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
180,243✔
1361
  if (code) {
180,280!
1362
    metaErr(TD_VID(pMeta->pVnode), code);
×
1363
    return code;
×
1364
  }
1365

1366
  // update TDB
1367
  metaWLock(pMeta);
180,280✔
1368
  code = metaHandleChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
180,286✔
1369
  metaULock(pMeta);
180,245✔
1370

1371
  // update other stuff
1372
  if (TSDB_CODE_SUCCESS == code) {
180,266!
1373
    pMeta->pVnode->config.vndStats.numOfCTables++;
180,266✔
1374

1375
    if (!metaTbInFilterCache(pMeta, pSuperEntry->name, 1)) {
180,266✔
1376
      int32_t nCols = 0;
180,276✔
1377
      int32_t ret = metaGetStbStats(pMeta->pVnode, pSuperEntry->uid, 0, &nCols, 0);
180,276✔
1378
      if (ret < 0) {
180,269!
1379
        metaErr(TD_VID(pMeta->pVnode), ret);
×
1380
      }
1381
      pMeta->pVnode->config.vndStats.numOfTimeSeries += (nCols > 0 ? nCols - 1 : 0);
180,273!
1382
    }
1383

1384
    if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
180,291!
1385
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, pEntry->ctbEntry.suid, NULL);
3,045✔
1386
      if (rc < 0) {
3,045!
1387
        metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
1388
                  tstrerror(rc));
1389
      }
1390
    }
1391

1392
  } else {
1393
    metaErr(TD_VID(pMeta->pVnode), code);
×
1394
  }
1395
  metaTimeSeriesNotifyCheck(pMeta);
180,291✔
1396
  metaFetchEntryFree(&pSuperEntry);
180,293✔
1397
  return code;
180,299✔
1398
}
1399

1400
static int32_t metaHandleVirtualNormalTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
166✔
1401
  int32_t code = TSDB_CODE_SUCCESS;
166✔
1402

1403
  SMetaTableOp ops[] = {
166✔
1404
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1405
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: need to be insert
1406
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1407
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1408
      {META_BTIME_IDX, META_TABLE_OP_INSERT},     //
1409
  };
1410

1411
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
996✔
1412
    SMetaTableOp *op = &ops[i];
830✔
1413

1414
    SMetaHandleParam param = {
830✔
1415
        .pEntry = pEntry,
1416
    };
1417

1418
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
830✔
1419
    if (TSDB_CODE_SUCCESS != code) {
830!
1420
      metaErr(TD_VID(pMeta->pVnode), code);
×
1421
      return code;
×
1422
    }
1423
  }
1424

1425
  return code;
166✔
1426
}
1427

1428
static int32_t metaHandleVirtualNormalTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
166✔
1429
  int32_t code = TSDB_CODE_SUCCESS;
166✔
1430

1431
  // update TDB
1432
  metaWLock(pMeta);
166✔
1433
  code = metaHandleVirtualNormalTableCreateImpl(pMeta, pEntry);
166✔
1434
  metaULock(pMeta);
166✔
1435

1436
  // update other stuff
1437
  if (TSDB_CODE_SUCCESS == code) {
166!
1438
    pMeta->pVnode->config.vndStats.numOfVTables++;
166✔
1439
  } else {
1440
    metaErr(TD_VID(pMeta->pVnode), code);
×
1441
  }
1442
  return code;
166✔
1443
}
1444

1445
static int32_t metaHandleVirtualChildTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry,
207✔
1446
                                                     const SMetaEntry *pSuperEntry) {
1447
  int32_t code = TSDB_CODE_SUCCESS;
207✔
1448

1449
  SMetaTableOp ops[] = {
207✔
1450
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},  //
1451
      {META_UID_IDX, META_TABLE_OP_INSERT},      //
1452
      {META_NAME_IDX, META_TABLE_OP_INSERT},     //
1453
      {META_CHILD_IDX, META_TABLE_OP_INSERT},    //
1454
      {META_TAG_IDX, META_TABLE_OP_INSERT},      //
1455
      {META_BTIME_IDX, META_TABLE_OP_INSERT},    //
1456
  };
1457

1458
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1,449✔
1459
    SMetaTableOp *op = &ops[i];
1,242✔
1460

1461
    SMetaHandleParam param = {
1,242✔
1462
        .pEntry = pEntry,
1463
        .pSuperEntry = pSuperEntry,
1464
    };
1465

1466
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
1,242✔
1467
    if (TSDB_CODE_SUCCESS != code) {
1,242!
1468
      metaErr(TD_VID(pMeta->pVnode), code);
×
1469
      return code;
×
1470
    }
1471
  }
1472

1473
  if (TSDB_CODE_SUCCESS == code) {
207!
1474
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
207✔
1475
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
207✔
1476
    if (ret < 0) {
207!
1477
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1478
    }
1479

1480
    ret = metaTbGroupCacheClear(pMeta, pSuperEntry->uid);
207✔
1481
    if (ret < 0) {
207!
1482
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1483
    }
1484

1485
    ret = metaRefDbsCacheClear(pMeta, pSuperEntry->uid);
207✔
1486
    if (ret < 0) {
207!
1487
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1488
    }
1489
  }
1490

1491
  return code;
207✔
1492
}
1493

1494
static int32_t metaHandleVirtualChildTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
207✔
1495
  int32_t     code = TSDB_CODE_SUCCESS;
207✔
1496
  SMetaEntry *pSuperEntry = NULL;
207✔
1497

1498
  // get the super table entry
1499
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
207✔
1500
  if (code) {
207!
1501
    metaErr(TD_VID(pMeta->pVnode), code);
×
1502
    return code;
×
1503
  }
1504

1505
  // update TDB
1506
  metaWLock(pMeta);
207✔
1507
  code = metaHandleVirtualChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
207✔
1508
  metaULock(pMeta);
207✔
1509

1510
  // update other stuff
1511
  if (TSDB_CODE_SUCCESS == code) {
207!
1512
    pMeta->pVnode->config.vndStats.numOfVCTables++;
207✔
1513
  } else {
1514
    metaErr(TD_VID(pMeta->pVnode), code);
×
1515
  }
1516

1517
  metaFetchEntryFree(&pSuperEntry);
207✔
1518
  return code;
207✔
1519
}
1520

1521
static int32_t metaHandleNormalTableDropImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
2,597✔
1522
  int32_t code = TSDB_CODE_SUCCESS;
2,597✔
1523

1524
  SMetaTableOp ops[] = {
2,597✔
1525
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1526
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1527
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1528
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1529
      {META_TTL_IDX, META_TABLE_OP_DELETE},      //
1530

1531
      // {META_SCHEMA_TABLE, META_TABLE_OP_DELETE},  //
1532
  };
1533

1534
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
15,582✔
1535
    SMetaTableOp *op = &ops[i];
12,985✔
1536
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
12,985✔
1537
    if (code) {
12,985!
1538
      const SMetaEntry *pEntry = pParam->pEntry;
×
1539
      metaErr(TD_VID(pMeta->pVnode), code);
×
1540
    }
1541
  }
1542

1543
  return code;
2,597✔
1544
}
1545

1546
static int32_t metaHandleNormalTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
2,597✔
1547
  int32_t     code = TSDB_CODE_SUCCESS;
2,597✔
1548
  SMetaEntry *pOldEntry = NULL;
2,597✔
1549

1550
  // fetch the entry
1551
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
2,597✔
1552
  if (code) {
2,597!
1553
    metaErr(TD_VID(pMeta->pVnode), code);
×
1554
    return code;
×
1555
  }
1556

1557
  SMetaHandleParam param = {
2,597✔
1558
      .pEntry = pEntry,
1559
      .pOldEntry = pOldEntry,
1560
  };
1561

1562
  // do the drop
1563
  metaWLock(pMeta);
2,597✔
1564
  code = metaHandleNormalTableDropImpl(pMeta, &param);
2,597✔
1565
  metaULock(pMeta);
2,597✔
1566
  if (code) {
2,597!
1567
    metaErr(TD_VID(pMeta->pVnode), code);
×
1568
    metaFetchEntryFree(&pOldEntry);
×
1569
    return code;
×
1570
  }
1571

1572
  // update other stuff
1573
  pMeta->pVnode->config.vndStats.numOfNTables--;
2,597✔
1574
  pMeta->pVnode->config.vndStats.numOfNTimeSeries -= (pOldEntry->ntbEntry.schemaRow.nCols - 1);
2,597✔
1575

1576
#if 0
1577
  if (tbUids) {
1578
    if (taosArrayPush(tbUids, &uid) == NULL) {
1579
      rc = terrno;
1580
      goto _exit;
1581
    }
1582
  }
1583
#endif
1584

1585
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
2,597!
1586
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pOldEntry->uid, 0, NULL);
×
1587
    if (ret < 0) {
×
1588
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1589
    }
1590
  }
1591

1592
  metaFetchEntryFree(&pOldEntry);
2,597✔
1593
  return code;
2,597✔
1594
}
1595

1596
static int32_t metaHandleChildTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam, bool superDropped) {
15,392✔
1597
  int32_t code = TSDB_CODE_SUCCESS;
15,392✔
1598

1599
  const SMetaEntry *pEntry = pParam->pEntry;
15,392✔
1600
  const SMetaEntry *pChild = pParam->pOldEntry;
15,392✔
1601
  const SMetaEntry *pSuper = pParam->pSuperEntry;
15,392✔
1602

1603
  SMetaTableOp ops[] = {
15,392✔
1604
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1605
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1606
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1607
      {META_CHILD_IDX, META_TABLE_OP_DELETE},    //
1608
      {META_TAG_IDX, META_TABLE_OP_DELETE},      //
1609
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1610
      {META_TTL_IDX, META_TABLE_OP_DELETE},      //
1611
  };
1612

1613
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
123,071✔
1614
    SMetaTableOp *op = &ops[i];
107,722✔
1615

1616
    if (op->table == META_ENTRY_TABLE && superDropped) {
107,722✔
1617
      continue;
1,745✔
1618
    }
1619

1620
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
105,977✔
1621
    if (code) {
105,979✔
1622
      metaErr(TD_VID(pMeta->pVnode), code);
45!
1623
      return code;
36✔
1624
    }
1625
  }
1626

1627
  --pMeta->pVnode->config.vndStats.numOfCTables;
15,349✔
1628
  metaUpdateStbStats(pMeta, pParam->pSuperEntry->uid, -1, 0, -1);
15,349✔
1629
  int32_t ret = metaUidCacheClear(pMeta, pSuper->uid);
15,356✔
1630
  if (ret < 0) {
15,355!
1631
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1632
  }
1633

1634
  ret = metaTbGroupCacheClear(pMeta, pSuper->uid);
15,355✔
1635
  if (ret < 0) {
15,356!
1636
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1637
  }
1638
  return code;
15,356✔
1639
}
1640

1641
static int32_t metaHandleChildTableDrop(SMeta *pMeta, const SMetaEntry *pEntry, bool superDropped) {
15,392✔
1642
  int32_t     code = TSDB_CODE_SUCCESS;
15,392✔
1643
  SMetaEntry *pChild = NULL;
15,392✔
1644
  SMetaEntry *pSuper = NULL;
15,392✔
1645

1646
  // fetch old entry
1647
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pChild);
15,392✔
1648
  if (code) {
15,392!
1649
    metaErr(TD_VID(pMeta->pVnode), code);
×
1650
    return code;
×
1651
  }
1652

1653
  // fetch super entry
1654
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
15,392✔
1655
  if (code) {
15,391!
1656
    metaErr(TD_VID(pMeta->pVnode), code);
×
1657
    metaFetchEntryFree(&pChild);
×
1658
    return code;
×
1659
  }
1660

1661
  SMetaHandleParam param = {
15,391✔
1662
      .pEntry = pEntry,
1663
      .pOldEntry = pChild,
1664
      .pSuperEntry = pSuper,
1665
  };
1666

1667
  // do the drop
1668
  metaWLock(pMeta);
15,391✔
1669
  code = metaHandleChildTableDropImpl(pMeta, &param, superDropped);
15,392✔
1670
  metaULock(pMeta);
15,392✔
1671
  if (code) {
15,392✔
1672
    metaErr(TD_VID(pMeta->pVnode), code);
36!
1673
    metaFetchEntryFree(&pChild);
36✔
1674
    metaFetchEntryFree(&pSuper);
36✔
1675
    return code;
36✔
1676
  }
1677

1678
  // do other stuff
1679
  if (!metaTbInFilterCache(pMeta, pSuper->name, 1)) {
15,356!
1680
    int32_t      nCols = 0;
15,356✔
1681
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
15,356✔
1682
    if (metaGetStbStats(pMeta->pVnode, pSuper->uid, NULL, &nCols, 0) == 0) {
15,356!
1683
      pStats->numOfTimeSeries -= nCols - 1;
15,356✔
1684
    }
1685
  }
1686

1687
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
15,356!
1688
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pChild->uid, pSuper->uid, NULL);
75✔
1689
    if (ret < 0) {
75!
1690
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1691
    }
1692
  }
1693

1694
#if 0
1695
  if (tbUids) {
1696
    if (taosArrayPush(tbUids, &uid) == NULL) {
1697
      rc = terrno;
1698
      goto _exit;
1699
    }
1700
  }
1701

1702
  if ((type == TSDB_CHILD_TABLE) && tbUid) {
1703
    *tbUid = uid;
1704
  }
1705
#endif
1706
  metaFetchEntryFree(&pChild);
15,356✔
1707
  metaFetchEntryFree(&pSuper);
15,356✔
1708
  return code;
15,356✔
1709
}
1710

1711
static int32_t metaHandleVirtualNormalTableDropImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
46✔
1712
  int32_t code = TSDB_CODE_SUCCESS;
46✔
1713

1714
  SMetaTableOp ops[] = {
46✔
1715
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1716
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1717
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1718
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1719

1720
      // {META_SCHEMA_TABLE, META_TABLE_OP_DELETE},  //
1721
  };
1722

1723
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
230✔
1724
    SMetaTableOp *op = &ops[i];
184✔
1725
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
184✔
1726
    if (code) {
184!
1727
      const SMetaEntry *pEntry = pParam->pEntry;
×
1728
      metaErr(TD_VID(pMeta->pVnode), code);
×
1729
    }
1730
  }
1731

1732
  return code;
46✔
1733
}
1734

1735
static int32_t metaHandleVirtualNormalTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
46✔
1736
  int32_t     code = TSDB_CODE_SUCCESS;
46✔
1737
  SMetaEntry *pOldEntry = NULL;
46✔
1738

1739
  // fetch the entry
1740
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
46✔
1741
  if (code) {
46!
1742
    metaErr(TD_VID(pMeta->pVnode), code);
×
1743
    return code;
×
1744
  }
1745

1746
  SMetaHandleParam param = {
46✔
1747
      .pEntry = pEntry,
1748
      .pOldEntry = pOldEntry,
1749
  };
1750

1751
  // do the drop
1752
  metaWLock(pMeta);
46✔
1753
  code = metaHandleVirtualNormalTableDropImpl(pMeta, &param);
46✔
1754
  metaULock(pMeta);
46✔
1755
  if (code) {
46!
1756
    metaErr(TD_VID(pMeta->pVnode), code);
×
1757
    metaFetchEntryFree(&pOldEntry);
×
1758
    return code;
×
1759
  }
1760

1761
  // update other stuff
1762
  pMeta->pVnode->config.vndStats.numOfVTables--;
46✔
1763

1764
#if 0
1765
  if (tbUids) {
1766
    if (taosArrayPush(tbUids, &uid) == NULL) {
1767
      rc = terrno;
1768
      goto _exit;
1769
    }
1770
  }
1771
#endif
1772

1773
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
46!
1774
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pOldEntry->uid, 0, NULL);
×
1775
    if (ret < 0) {
×
1776
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1777
    }
1778
  }
1779

1780
  metaFetchEntryFree(&pOldEntry);
46✔
1781
  return code;
46✔
1782
}
1783

1784
static int32_t metaHandleVirtualChildTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam, bool superDropped) {
5✔
1785
  int32_t code = TSDB_CODE_SUCCESS;
5✔
1786

1787
  const SMetaEntry *pEntry = pParam->pEntry;
5✔
1788
  const SMetaEntry *pChild = pParam->pOldEntry;
5✔
1789
  const SMetaEntry *pSuper = pParam->pSuperEntry;
5✔
1790

1791
  SMetaTableOp ops[] = {
5✔
1792
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1793
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1794
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1795
      {META_CHILD_IDX, META_TABLE_OP_DELETE},    //
1796
      {META_TAG_IDX, META_TABLE_OP_DELETE},      //
1797
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1798
  };
1799

1800
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
35✔
1801
    SMetaTableOp *op = &ops[i];
30✔
1802

1803
    if (op->table == META_ENTRY_TABLE && superDropped) {
30!
1804
      continue;
×
1805
    }
1806

1807
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
30✔
1808
    if (code) {
30!
1809
      metaErr(TD_VID(pMeta->pVnode), code);
×
1810
      return code;
×
1811
    }
1812
  }
1813

1814
  --pMeta->pVnode->config.vndStats.numOfVCTables;
5✔
1815
  metaUpdateStbStats(pMeta, pParam->pSuperEntry->uid, -1, 0, -1);
5✔
1816
  int32_t ret = metaUidCacheClear(pMeta, pSuper->uid);
5✔
1817
  if (ret < 0) {
5!
1818
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1819
  }
1820

1821
  ret = metaTbGroupCacheClear(pMeta, pSuper->uid);
5✔
1822
  if (ret < 0) {
5!
1823
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1824
  }
1825

1826
  ret = metaRefDbsCacheClear(pMeta, pSuper->uid);
5✔
1827
  if (ret < 0) {
5!
1828
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1829
  }
1830

1831
  return code;
5✔
1832
}
1833

1834
static int32_t metaHandleVirtualChildTableDrop(SMeta *pMeta, const SMetaEntry *pEntry, bool superDropped) {
5✔
1835
  int32_t     code = TSDB_CODE_SUCCESS;
5✔
1836
  SMetaEntry *pChild = NULL;
5✔
1837
  SMetaEntry *pSuper = NULL;
5✔
1838

1839
  // fetch old entry
1840
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pChild);
5✔
1841
  if (code) {
5!
1842
    metaErr(TD_VID(pMeta->pVnode), code);
×
1843
    return code;
×
1844
  }
1845

1846
  // fetch super entry
1847
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
5✔
1848
  if (code) {
5!
1849
    metaErr(TD_VID(pMeta->pVnode), code);
×
1850
    metaFetchEntryFree(&pChild);
×
1851
    return code;
×
1852
  }
1853

1854
  SMetaHandleParam param = {
5✔
1855
      .pEntry = pEntry,
1856
      .pOldEntry = pChild,
1857
      .pSuperEntry = pSuper,
1858
  };
1859

1860
  // do the drop
1861
  metaWLock(pMeta);
5✔
1862
  code = metaHandleVirtualChildTableDropImpl(pMeta, &param, superDropped);
5✔
1863
  metaULock(pMeta);
5✔
1864
  if (code) {
5!
1865
    metaErr(TD_VID(pMeta->pVnode), code);
×
1866
    metaFetchEntryFree(&pChild);
×
1867
    metaFetchEntryFree(&pSuper);
×
1868
    return code;
×
1869
  }
1870

1871
  metaFetchEntryFree(&pChild);
5✔
1872
  metaFetchEntryFree(&pSuper);
5✔
1873
  return code;
5✔
1874
}
1875

1876
static int32_t metaGetChildUidsOfSuperTable(SMeta *pMeta, tb_uid_t suid, SArray **childList) {
2,492✔
1877
  int32_t code = TSDB_CODE_SUCCESS;
2,492✔
1878
  void   *key = NULL;
2,492✔
1879
  int32_t keySize = 0;
2,492✔
1880
  int32_t c;
1881

1882
  *childList = taosArrayInit(64, sizeof(tb_uid_t));
2,492✔
1883
  if (*childList == NULL) {
2,496!
1884
    return terrno;
×
1885
  }
1886

1887
  TBC *cursor = NULL;
2,496✔
1888
  code = tdbTbcOpen(pMeta->pCtbIdx, &cursor, NULL);
2,496✔
1889
  if (code) {
2,498!
1890
    taosArrayDestroy(*childList);
×
1891
    *childList = NULL;
×
1892
    return code;
×
1893
  }
1894

1895
  int32_t rc = tdbTbcMoveTo(cursor,
2,498✔
1896
                            &(SCtbIdxKey){
2,498✔
1897
                                .suid = suid,
1898
                                .uid = INT64_MIN,
1899
                            },
1900
                            sizeof(SCtbIdxKey), &c);
1901
  if (rc < 0) {
2,498!
1902
    tdbTbcClose(cursor);
×
1903
    return 0;
×
1904
  }
1905

1906
  for (;;) {
1907
    if (tdbTbcNext(cursor, &key, &keySize, NULL, NULL) < 0) {
5,175✔
1908
      break;
1,696✔
1909
    }
1910

1911
    if (((SCtbIdxKey *)key)->suid < suid) {
3,477✔
1912
      continue;
241✔
1913
    } else if (((SCtbIdxKey *)key)->suid > suid) {
3,236✔
1914
      break;
802✔
1915
    }
1916

1917
    if (taosArrayPush(*childList, &(((SCtbIdxKey *)key)->uid)) == NULL) {
4,870!
1918
      tdbFreeClear(key);
×
1919
      tdbTbcClose(cursor);
×
1920
      taosArrayDestroy(*childList);
×
1921
      *childList = NULL;
×
1922
      return terrno;
×
1923
    }
1924
  }
1925

1926
  tdbTbcClose(cursor);
2,498✔
1927
  tdbFreeClear(key);
2,498✔
1928
  return code;
2,498✔
1929
}
1930

1931
static int32_t metaHandleSuperTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
2,339✔
1932
  int32_t           code = TSDB_CODE_SUCCESS;
2,339✔
1933
  const SMetaEntry *pEntry = pParam->pEntry;
2,339✔
1934

1935
  SMetaTableOp ops[] = {
2,339✔
1936
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1937
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1938
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1939
      {META_SUID_IDX, META_TABLE_OP_DELETE},     //
1940

1941
      // {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: here should be insert
1942
  };
1943

1944
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
11,703✔
1945
    SMetaTableOp *op = &ops[i];
9,360✔
1946

1947
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
9,360✔
1948
    if (TSDB_CODE_SUCCESS != code) {
9,363!
1949
      metaErr(TD_VID(pMeta->pVnode), code);
×
1950
      return code;
×
1951
    }
1952
  }
1953

1954
  int32_t ret = metaStatsCacheDrop(pMeta, pEntry->uid);
2,343✔
1955
  if (ret < 0) {
2,340✔
1956
    metaErr(TD_VID(pMeta->pVnode), ret);
872!
1957
  }
1958
  return code;
2,340✔
1959
}
1960

1961
static int32_t metaHandleNormalTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
5,289✔
1962
  int32_t code = TSDB_CODE_SUCCESS;
5,289✔
1963

1964
  const SMetaEntry *pEntry = pParam->pEntry;
5,289✔
1965

1966
  SMetaTableOp ops[] = {
5,289✔
1967
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
1968
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
1969
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
1970
      {META_TTL_IDX, META_TABLE_OP_UPDATA},       //
1971
  };
1972
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
26,445✔
1973
    SMetaTableOp *op = &ops[i];
21,156✔
1974
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
21,156✔
1975
    if (code) {
21,156!
1976
      metaErr(TD_VID(pMeta->pVnode), code);
×
1977
      return code;
×
1978
    }
1979
  }
1980
#if 0
1981
  if (metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs) < 0) {
1982
    metaError("vgId:%d, failed to update change time:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
1983
  }
1984
#endif
1985
  return code;
5,289✔
1986
}
1987

1988
static int32_t metaHandleVirtualNormalTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
330✔
1989
  int32_t code = TSDB_CODE_SUCCESS;
330✔
1990

1991
  const SMetaEntry *pEntry = pParam->pEntry;
330✔
1992

1993
  SMetaTableOp ops[] = {
330✔
1994
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
1995
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
1996
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
1997
  };
1998
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1,320✔
1999
    SMetaTableOp *op = &ops[i];
990✔
2000
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
990✔
2001
    if (code) {
990!
2002
      metaErr(TD_VID(pMeta->pVnode), code);
×
2003
      return code;
×
2004
    }
2005
  }
2006
#if 0
2007
  if (metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs) < 0) {
2008
    metaError("vgId:%d, failed to update change time:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
2009
  }
2010
#endif
2011
  return code;
330✔
2012
}
2013

2014
static int32_t metaHandleVirtualChildTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
10✔
2015
  int32_t code = TSDB_CODE_SUCCESS;
10✔
2016

2017
  const SMetaEntry *pEntry = pParam->pEntry;
10✔
2018
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
10✔
2019
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
10✔
2020

2021
  SMetaTableOp ops[] = {
10✔
2022
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},  //
2023
      {META_UID_IDX, META_TABLE_OP_UPDATA},      //
2024
      {META_TAG_IDX, META_TABLE_OP_UPDATA},      //
2025
      {META_CHILD_IDX, META_TABLE_OP_UPDATA},    //
2026
  };
2027

2028
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
50✔
2029
    SMetaTableOp *op = &ops[i];
40✔
2030
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
40✔
2031
    if (code) {
40!
2032
      metaErr(TD_VID(pMeta->pVnode), code);
×
2033
      return code;
×
2034
    }
2035
  }
2036

2037
  if (metaUidCacheClear(pMeta, pSuperEntry->uid) < 0) {
10!
2038
    metaErr(TD_VID(pMeta->pVnode), code);
×
2039
  }
2040

2041
  if (metaTbGroupCacheClear(pMeta, pSuperEntry->uid) < 0) {
10!
2042
    metaErr(TD_VID(pMeta->pVnode), code);
×
2043
  }
2044

2045
  if (metaRefDbsCacheClear(pMeta, pSuperEntry->uid) < 0) {
10!
2046
    metaErr(TD_VID(pMeta->pVnode), code);
×
2047
  }
2048
  return code;
10✔
2049
}
2050

2051
static int32_t metaHandleChildTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
13,796✔
2052
  int32_t code = TSDB_CODE_SUCCESS;
13,796✔
2053

2054
  const SMetaEntry *pEntry = pParam->pEntry;
13,796✔
2055
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
13,796✔
2056
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
13,796✔
2057

2058
  SMetaTableOp ops[] = {
13,796✔
2059
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},  //
2060
      {META_UID_IDX, META_TABLE_OP_UPDATA},      //
2061
      {META_TAG_IDX, META_TABLE_OP_UPDATA},      //
2062
      {META_CHILD_IDX, META_TABLE_OP_UPDATA},    //
2063
      {META_TTL_IDX, META_TABLE_OP_UPDATA},      //
2064
  };
2065

2066
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
82,776✔
2067
    SMetaTableOp *op = &ops[i];
68,980✔
2068
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
68,980✔
2069
    if (code) {
68,980!
2070
      metaErr(TD_VID(pMeta->pVnode), code);
×
2071
      return code;
×
2072
    }
2073
  }
2074

2075
  if (metaUidCacheClear(pMeta, pSuperEntry->uid) < 0) {
13,796!
2076
    metaErr(TD_VID(pMeta->pVnode), code);
×
2077
  }
2078

2079
  if (metaTbGroupCacheClear(pMeta, pSuperEntry->uid) < 0) {
13,796!
2080
    metaErr(TD_VID(pMeta->pVnode), code);
×
2081
  }
2082
  return code;
13,796✔
2083
#if 0
2084
  if (metaUpdateChangeTime(pMeta, ctbEntry.uid, pReq->ctimeMs) < 0) {
2085
    metaError("meta/table: failed to update change time:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
2086
  }
2087
#endif
2088
}
2089

2090
static int32_t metaHandleSuperTableUpdateImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
16,589✔
2091
  int32_t code = TSDB_CODE_SUCCESS;
16,589✔
2092

2093
  const SMetaEntry *pEntry = pParam->pEntry;
16,589✔
2094
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
16,589✔
2095

2096
  SMetaTableOp ops[] = {
16,589✔
2097
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
2098
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
2099
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
2100
  };
2101

2102
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
66,362✔
2103
    SMetaTableOp *op = &ops[i];
49,786✔
2104
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
49,786✔
2105
    if (code) {
49,784✔
2106
      metaErr(TD_VID(pMeta->pVnode), code);
11!
2107
      return code;
×
2108
    }
2109
  }
2110

2111
  if (TSDB_CODE_SUCCESS == code) {
16,576✔
2112
    metaUpdateStbStats(pMeta, pEntry->uid, 0, pEntry->stbEntry.schemaRow.nCols - pOldEntry->stbEntry.schemaRow.nCols,
16,573✔
2113
                       pEntry->stbEntry.keep);
16,573✔
2114
  }
2115

2116
  return code;
16,607✔
2117
}
2118

2119
static int32_t metaHandleSuperTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
16,584✔
2120
  int32_t code = TSDB_CODE_SUCCESS;
16,584✔
2121

2122
  SMetaEntry *pOldEntry = NULL;
16,584✔
2123

2124
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
16,584✔
2125
  if (code) {
16,617!
2126
    metaErr(TD_VID(pMeta->pVnode), code);
×
2127
    return code;
×
2128
  }
2129

2130
  SMetaHandleParam param = {
16,617✔
2131
      .pEntry = pEntry,
2132
      .pOldEntry = pOldEntry,
2133
  };
2134
  metaWLock(pMeta);
16,617✔
2135
  code = metaHandleSuperTableUpdateImpl(pMeta, &param);
16,614✔
2136
  metaULock(pMeta);
16,576✔
2137
  if (code) {
16,567!
2138
    metaErr(TD_VID(pMeta->pVnode), code);
×
2139
    metaFetchEntryFree(&pOldEntry);
×
2140
    return code;
×
2141
  }
2142

2143
  int     nCols = pEntry->stbEntry.schemaRow.nCols;
16,567✔
2144
  int     onCols = pOldEntry->stbEntry.schemaRow.nCols;
16,567✔
2145
  int32_t deltaCol = nCols - onCols;
16,567✔
2146
  bool    updStat = deltaCol != 0 && !TABLE_IS_VIRTUAL(pEntry->flags) && !metaTbInFilterCache(pMeta, pEntry->name, 1);
16,567!
2147

2148
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
16,578!
2149
    STsdb  *pTsdb = pMeta->pVnode->pTsdb;
×
2150
    SArray *uids = NULL; /*taosArrayInit(8, sizeof(int64_t));
×
2151
     if (uids == NULL) {
2152
       metaErr(TD_VID(pMeta->pVnode), code);
2153
       metaFetchEntryFree(&pOldEntry);
2154
       return terrno;
2155
       }*/
2156
    if (deltaCol == 1) {
×
2157
      int16_t cid = pEntry->stbEntry.schemaRow.pSchema[nCols - 1].colId;
×
2158
      int8_t  col_type = pEntry->stbEntry.schemaRow.pSchema[nCols - 1].type;
×
2159

2160
      code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &uids);
×
2161
      if (code) {
×
2162
        metaErr(TD_VID(pMeta->pVnode), code);
×
2163
        metaFetchEntryFree(&pOldEntry);
×
2164
        return code;
×
2165
      }
2166
      if (pTsdb) {
×
2167
        TAOS_CHECK_RETURN(tsdbCacheNewSTableColumn(pTsdb, uids, cid, col_type));
×
2168
      }
2169
    } else if (deltaCol == -1) {
×
2170
      int16_t cid = -1;
×
2171
      bool    hasPrimaryKey = false;
×
2172
      if (onCols >= 2) {
×
2173
        hasPrimaryKey = (pOldEntry->stbEntry.schemaRow.pSchema[1].flags & COL_IS_KEY) ? true : false;
×
2174
      }
2175
      for (int i = 0, j = 0; i < nCols && j < onCols; ++i, ++j) {
×
2176
        if (pEntry->stbEntry.schemaRow.pSchema[i].colId != pOldEntry->stbEntry.schemaRow.pSchema[j].colId) {
×
2177
          cid = pOldEntry->stbEntry.schemaRow.pSchema[j].colId;
×
2178
          break;
×
2179
        }
2180
      }
2181

2182
      if (cid != -1) {
×
2183
        code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &uids);
×
2184
        if (code) {
×
2185
          metaErr(TD_VID(pMeta->pVnode), code);
×
2186
          metaFetchEntryFree(&pOldEntry);
×
2187
          return code;
×
2188
        }
2189
        if (pTsdb) {
×
2190
          TAOS_CHECK_RETURN(tsdbCacheDropSTableColumn(pTsdb, uids, cid, hasPrimaryKey));
×
2191
        }
2192
      }
2193
    }
2194
    if (uids) taosArrayDestroy(uids);
×
2195

2196
    if (pTsdb) {
×
2197
      tsdbCacheInvalidateSchema(pTsdb, pEntry->uid, -1, pEntry->stbEntry.schemaRow.version);
×
2198
    }
2199
  }
2200
  if (updStat) {
16,578✔
2201
    int64_t ctbNum = 0;
10,581✔
2202
    int32_t ret = metaGetStbStats(pMeta->pVnode, pEntry->uid, &ctbNum, 0, 0);
10,581✔
2203
    if (ret < 0) {
10,601!
2204
      metaError("vgId:%d, failed to get stb stats:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
×
2205
                pEntry->uid, tstrerror(ret));
2206
    }
2207
    pMeta->pVnode->config.vndStats.numOfTimeSeries += (ctbNum * deltaCol);
10,589✔
2208
    if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
10,589✔
2209
  }
2210
  metaFetchEntryFree(&pOldEntry);
16,580✔
2211
  return code;
16,612✔
2212
}
2213

2214
static int32_t metaHandleChildTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
13,796✔
2215
  int32_t code = TSDB_CODE_SUCCESS;
13,796✔
2216

2217
  SMetaEntry *pOldEntry = NULL;
13,796✔
2218
  SMetaEntry *pSuperEntry = NULL;
13,796✔
2219

2220
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
13,796✔
2221
  if (code) {
13,796!
2222
    metaErr(TD_VID(pMeta->pVnode), code);
×
2223
    return code;
×
2224
  }
2225

2226
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
13,796✔
2227
  if (code) {
13,796!
2228
    metaErr(TD_VID(pMeta->pVnode), code);
×
2229
    metaFetchEntryFree(&pOldEntry);
×
2230
    return code;
×
2231
  }
2232

2233
  SMetaHandleParam param = {
13,796✔
2234
      .pEntry = pEntry,
2235
      .pOldEntry = pOldEntry,
2236
      .pSuperEntry = pSuperEntry,
2237
  };
2238

2239
  metaWLock(pMeta);
13,796✔
2240
  code = metaHandleChildTableUpdateImpl(pMeta, &param);
13,796✔
2241
  metaULock(pMeta);
13,796✔
2242
  if (code) {
13,796!
2243
    metaErr(TD_VID(pMeta->pVnode), code);
×
2244
    metaFetchEntryFree(&pOldEntry);
×
2245
    metaFetchEntryFree(&pSuperEntry);
×
2246
    return code;
×
2247
  }
2248

2249
  metaFetchEntryFree(&pOldEntry);
13,796✔
2250
  metaFetchEntryFree(&pSuperEntry);
13,796✔
2251
  return code;
13,796✔
2252
}
2253

2254
static int32_t metaHandleNormalTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
5,289✔
2255
  int32_t     code = TSDB_CODE_SUCCESS;
5,289✔
2256
  SMetaEntry *pOldEntry = NULL;
5,289✔
2257

2258
  // fetch old entry
2259
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
5,289✔
2260
  if (code) {
5,289!
2261
    metaErr(TD_VID(pMeta->pVnode), code);
×
2262
    return code;
×
2263
  }
2264

2265
  // handle update
2266
  SMetaHandleParam param = {
5,289✔
2267
      .pEntry = pEntry,
2268
      .pOldEntry = pOldEntry,
2269
  };
2270
  metaWLock(pMeta);
5,289✔
2271
  code = metaHandleNormalTableUpdateImpl(pMeta, &param);
5,289✔
2272
  metaULock(pMeta);
5,289✔
2273
  if (code) {
5,289!
2274
    metaErr(TD_VID(pMeta->pVnode), code);
×
2275
    metaFetchEntryFree(&pOldEntry);
×
2276
    return code;
×
2277
  }
2278

2279
  // do other stuff
2280
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) &&
5,289✔
2281
      pEntry->ntbEntry.schemaRow.version != pOldEntry->ntbEntry.schemaRow.version) {
12!
2282
#if 0
2283
    {  // for add column
2284
      int16_t cid = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].colId;
2285
      int8_t  col_type = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].type;
2286
      int32_t ret = tsdbCacheNewNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, col_type);
2287
      if (ret < 0) {
2288
        terrno = ret;
2289
        goto _err;
2290
      }
2291
    }
2292
    {  // for drop column
2293

2294
      if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
2295
        int16_t cid = pColumn->colId;
2296

2297
        if (tsdbCacheDropNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, hasPrimayKey) != 0) {
2298
          metaError("vgId:%d, failed to drop ntable column:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name,
2299
                    entry.uid);
2300
        }
2301
        tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, entry.uid, pSchema->version);
2302
      }
2303
    }
2304
    }
2305
#endif
2306
    if (pMeta->pVnode->pTsdb) {
12!
2307
      tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, pEntry->uid, pEntry->ntbEntry.schemaRow.version);
12✔
2308
    }
2309
  }
2310
  int32_t deltaCol = pEntry->ntbEntry.schemaRow.nCols - pOldEntry->ntbEntry.schemaRow.nCols;
5,289✔
2311
  pMeta->pVnode->config.vndStats.numOfNTimeSeries += deltaCol;
5,289✔
2312
  if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
5,289✔
2313
  metaFetchEntryFree(&pOldEntry);
5,289✔
2314
  return code;
5,289✔
2315
}
2316

2317
static int32_t metaHandleVirtualNormalTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
330✔
2318
  int32_t     code = TSDB_CODE_SUCCESS;
330✔
2319
  SMetaEntry *pOldEntry = NULL;
330✔
2320

2321
  // fetch old entry
2322
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
330✔
2323
  if (code) {
330!
2324
    metaErr(TD_VID(pMeta->pVnode), code);
×
2325
    return code;
×
2326
  }
2327

2328
  // handle update
2329
  SMetaHandleParam param = {
330✔
2330
      .pEntry = pEntry,
2331
      .pOldEntry = pOldEntry,
2332
  };
2333
  metaWLock(pMeta);
330✔
2334
  code = metaHandleVirtualNormalTableUpdateImpl(pMeta, &param);
330✔
2335
  metaULock(pMeta);
330✔
2336
  if (code) {
330!
2337
    metaErr(TD_VID(pMeta->pVnode), code);
×
2338
    metaFetchEntryFree(&pOldEntry);
×
2339
    return code;
×
2340
  }
2341

2342
  metaTimeSeriesNotifyCheck(pMeta);
330✔
2343
  metaFetchEntryFree(&pOldEntry);
330✔
2344
  return code;
330✔
2345
}
2346

2347
static int32_t metaHandleVirtualChildTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
10✔
2348
  int32_t code = TSDB_CODE_SUCCESS;
10✔
2349

2350
  SMetaEntry *pOldEntry = NULL;
10✔
2351
  SMetaEntry *pSuperEntry = NULL;
10✔
2352

2353
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
10✔
2354
  if (code) {
10!
2355
    metaErr(TD_VID(pMeta->pVnode), code);
×
2356
    return code;
×
2357
  }
2358

2359
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
10✔
2360
  if (code) {
10!
2361
    metaErr(TD_VID(pMeta->pVnode), code);
×
2362
    metaFetchEntryFree(&pOldEntry);
×
2363
    return code;
×
2364
  }
2365

2366
  SMetaHandleParam param = {
10✔
2367
      .pEntry = pEntry,
2368
      .pOldEntry = pOldEntry,
2369
      .pSuperEntry = pSuperEntry,
2370
  };
2371

2372
  metaWLock(pMeta);
10✔
2373
  code = metaHandleVirtualChildTableUpdateImpl(pMeta, &param);
10✔
2374
  metaULock(pMeta);
10✔
2375
  if (code) {
10!
2376
    metaErr(TD_VID(pMeta->pVnode), code);
×
2377
    metaFetchEntryFree(&pOldEntry);
×
2378
    metaFetchEntryFree(&pSuperEntry);
×
2379
    return code;
×
2380
  }
2381

2382
  metaFetchEntryFree(&pOldEntry);
10✔
2383
  metaFetchEntryFree(&pSuperEntry);
10✔
2384
  return code;
10✔
2385
}
2386

2387
static int32_t metaHandleSuperTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
2,341✔
2388
  int32_t     code = TSDB_CODE_SUCCESS;
2,341✔
2389
  SArray     *childList = NULL;
2,341✔
2390
  SMetaEntry *pOldEntry = NULL;
2,341✔
2391

2392
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
2,341✔
2393
  if (code) {
2,339!
2394
    metaErr(TD_VID(pMeta->pVnode), code);
×
2395
    return code;
×
2396
  }
2397

2398
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childList);
2,339✔
2399
  if (code) {
2,341!
2400
    metaErr(TD_VID(pMeta->pVnode), code);
×
2401
    metaFetchEntryFree(&pOldEntry);
×
2402
    return code;
×
2403
  }
2404

2405
  if (pMeta->pVnode->pTsdb && tsdbCacheDropSubTables(pMeta->pVnode->pTsdb, childList, pEntry->uid) < 0) {
2,341✔
2406
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
4!
2407
              pEntry->uid, tstrerror(terrno));
2408
  }
2409

2410
  // loop to drop all child tables
2411
  for (int32_t i = 0; i < taosArrayGetSize(childList); i++) {
4,084✔
2412
    SMetaEntry childEntry = {
3,490✔
2413
        .version = pEntry->version,
1,745✔
2414
        .uid = *(tb_uid_t *)taosArrayGet(childList, i),
1,745✔
2415
        .type = -TSDB_CHILD_TABLE,
2416
    };
2417

2418
    code = metaHandleChildTableDrop(pMeta, &childEntry, true);
1,745✔
2419
    if (code) {
1,745✔
2420
      metaErr(TD_VID(pMeta->pVnode), code);
36!
2421
    }
2422
  }
2423

2424
  // do drop super table
2425
  SMetaHandleParam param = {
2,340✔
2426
      .pEntry = pEntry,
2427
      .pOldEntry = pOldEntry,
2428
  };
2429
  metaWLock(pMeta);
2,340✔
2430
  code = metaHandleSuperTableDropImpl(pMeta, &param);
2,340✔
2431
  metaULock(pMeta);
2,340✔
2432
  if (code) {
2,340!
2433
    metaErr(TD_VID(pMeta->pVnode), code);
×
2434
    taosArrayDestroy(childList);
×
2435
    metaFetchEntryFree(&pOldEntry);
×
2436
    return code;
×
2437
  }
2438

2439
  // do other stuff
2440
  // metaUpdTimeSeriesNum(pMeta);
2441

2442
  // free resource and return
2443
  taosArrayDestroy(childList);
2,340✔
2444
  metaFetchEntryFree(&pOldEntry);
2,340✔
2445
  return code;
2,341✔
2446
}
2447

2448
int32_t metaHandleEntry2(SMeta *pMeta, const SMetaEntry *pEntry) {
277,694✔
2449
  int32_t   code = TSDB_CODE_SUCCESS;
277,694✔
2450
  int32_t   vgId = TD_VID(pMeta->pVnode);
277,694✔
2451
  SMetaInfo info = {0};
277,694✔
2452
  int8_t    type = pEntry->type > 0 ? pEntry->type : -pEntry->type;
277,694✔
2453

2454
  if (NULL == pMeta || NULL == pEntry) {
277,694!
2455
    metaError("%s failed at %s:%d since invalid parameter", __func__, __FILE__, __LINE__);
×
2456
    return TSDB_CODE_INVALID_PARA;
×
2457
  }
2458

2459
  if (pEntry->type > 0) {
278,451✔
2460
    bool isExist = false;
259,820✔
2461
    if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) {
259,820✔
2462
      isExist = true;
36,036✔
2463
    }
2464

2465
    switch (type) {
259,796!
2466
      case TSDB_SUPER_TABLE: {
44,361✔
2467
        if (isExist) {
44,361✔
2468
          code = metaHandleSuperTableUpdate(pMeta, pEntry);
16,614✔
2469
        } else {
2470
          code = metaHandleSuperTableCreate(pMeta, pEntry);
27,747✔
2471
        }
2472
        break;
44,338✔
2473
      }
2474
      case TSDB_CHILD_TABLE: {
194,070✔
2475
        if (isExist) {
194,070✔
2476
          code = metaHandleChildTableUpdate(pMeta, pEntry);
13,796✔
2477
        } else {
2478
          code = metaHandleChildTableCreate(pMeta, pEntry);
180,274✔
2479
        }
2480
        break;
194,085✔
2481
      }
2482
      case TSDB_NORMAL_TABLE: {
20,664✔
2483
        if (isExist) {
20,664✔
2484
          code = metaHandleNormalTableUpdate(pMeta, pEntry);
5,289✔
2485
        } else {
2486
          code = metaHandleNormalTableCreate(pMeta, pEntry);
15,375✔
2487
        }
2488
        break;
20,664✔
2489
      }
2490
      case TSDB_VIRTUAL_NORMAL_TABLE: {
496✔
2491
        if (isExist) {
496✔
2492
          code = metaHandleVirtualNormalTableUpdate(pMeta, pEntry);
330✔
2493
        } else {
2494
          code = metaHandleVirtualNormalTableCreate(pMeta, pEntry);
166✔
2495
        }
2496
        break;
496✔
2497
      }
2498
      case TSDB_VIRTUAL_CHILD_TABLE: {
217✔
2499
        if (isExist) {
217✔
2500
          code = metaHandleVirtualChildTableUpdate(pMeta, pEntry);
10✔
2501
        } else {
2502
          code = metaHandleVirtualChildTableCreate(pMeta, pEntry);
207✔
2503
        }
2504
        break;
217✔
2505
      }
2506
      default: {
×
2507
        code = TSDB_CODE_INVALID_PARA;
×
2508
        break;
×
2509
      }
2510
    }
2511
  } else {
2512
    switch (type) {
18,631!
2513
      case TSDB_SUPER_TABLE: {
2,341✔
2514
        code = metaHandleSuperTableDrop(pMeta, pEntry);
2,341✔
2515
        break;
2,340✔
2516
      }
2517
      case TSDB_CHILD_TABLE: {
13,647✔
2518
        code = metaHandleChildTableDrop(pMeta, pEntry, false);
13,647✔
2519
        break;
13,647✔
2520
      }
2521
      case TSDB_NORMAL_TABLE: {
2,597✔
2522
        code = metaHandleNormalTableDrop(pMeta, pEntry);
2,597✔
2523
        break;
2,597✔
2524
      }
2525
      case TSDB_VIRTUAL_NORMAL_TABLE: {
46✔
2526
        code = metaHandleVirtualNormalTableDrop(pMeta, pEntry);
46✔
2527
        break;
46✔
2528
      }
2529
      case TSDB_VIRTUAL_CHILD_TABLE: {
5✔
2530
        code = metaHandleVirtualChildTableDrop(pMeta, pEntry, false);
5✔
2531
        break;
5✔
2532
      }
2533
      default: {
×
2534
        code = TSDB_CODE_INVALID_PARA;
×
2535
        break;
×
2536
      }
2537
    }
2538
  }
2539

2540
  if (TSDB_CODE_SUCCESS == code) {
278,418✔
2541
    pMeta->changed = true;
278,409✔
2542
    metaDebug("vgId:%d, index:%" PRId64 ", handle meta entry success, type:%d tb:%s uid:%" PRId64, vgId,
278,409✔
2543
              pEntry->version, pEntry->type, pEntry->type > 0 ? pEntry->name : "", pEntry->uid);
2544
  } else {
2545
    metaErr(vgId, code);
9!
2546
  }
2547
  TAOS_RETURN(code);
278,419✔
2548
}
2549

2550
void metaHandleSyncEntry(SMeta *pMeta, const SMetaEntry *pEntry) {
612✔
2551
  int32_t code = TSDB_CODE_SUCCESS;
612✔
2552
  code = metaHandleEntry2(pMeta, pEntry);
612✔
2553
  if (code) {
612!
2554
    metaErr(TD_VID(pMeta->pVnode), code);
×
2555
  }
2556
  return;
612✔
2557
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc