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

taosdata / TDengine / #4550

21 Jul 2025 05:44AM UTC coverage: 54.508% (+0.2%) from 54.273%
#4550

push

travis-ci

web-flow
Merge pull request #32061 from taosdata/new_testcases

skip tsim cases

133505 of 315239 branches covered (42.35%)

Branch coverage included in aggregate %.

202054 of 300373 relevant lines covered (67.27%)

4175333.65 hits per line

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

58.26
/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) {
355,891✔
68
  int32_t code = TSDB_CODE_SUCCESS;
355,891✔
69
  void   *value = NULL;
355,891✔
70
  int32_t valueSize = 0;
355,891✔
71

72
  // search uid index
73
  code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &value, &valueSize);
355,891✔
74
  if (TSDB_CODE_SUCCESS != code) {
355,959!
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 = {
355,959✔
81
      .version = ((SUidIdxVal *)value)->version,
355,959✔
82
      .uid = uid,
83
  };
84
  tdbFreeClear(value);
355,959✔
85

86
  code = tdbTbGet(pMeta->pTbDb, &key, sizeof(key), &value, &valueSize);
355,947✔
87
  if (TSDB_CODE_SUCCESS != code) {
355,955!
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};
355,955✔
95
  SMetaEntry entry = {0};
355,955✔
96

97
  tDecoderInit(&decoder, value, valueSize);
355,955✔
98
  code = metaDecodeEntry(&decoder, &entry);
355,945✔
99
  if (code) {
355,840!
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);
355,840✔
108
  if (code) {
355,912!
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);
355,912✔
117
  tDecoderClear(&decoder);
355,932✔
118
  return code;
355,955✔
119
}
120

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

126
  code = tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &value, &valueSize);
157,678✔
127
  if (TSDB_CODE_SUCCESS != code) {
157,676!
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;
157,676✔
132
  tdbFreeClear(value);
157,676✔
133

134
  code = metaFetchEntryByUid(pMeta, uid, ppEntry);
157,674✔
135
  if (TSDB_CODE_SUCCESS != code) {
157,665!
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;
157,665✔
140
}
141

142
void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); }
356,006✔
143

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

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

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

165
  value = taosMemoryMalloc(valueSize);
206,626!
166
  if (NULL == value) {
206,681!
167
    metaErr(vgId, terrno);
×
168
    return terrno;
×
169
  }
170

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

181
  // put to tdb
182
  if (META_TABLE_OP_INSERT == op) {
206,841✔
183
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
172,740✔
184
  } else if (META_TABLE_OP_UPDATA == op) {
34,101✔
185
    code = tdbTbUpsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
17,274✔
186
  } else if (META_TABLE_OP_DELETE == op) {
16,827!
187
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
16,827✔
188
  } else {
189
    code = TSDB_CODE_INVALID_PARA;
×
190
  }
191
  if (TSDB_CODE_SUCCESS != code) {
206,970!
192
    metaErr(vgId, code);
×
193
  }
194
  taosMemoryFree(value);
206,933!
195
  return code;
206,946✔
196
}
197

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

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

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

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

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

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

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

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

255
  // put to tdb
256
  if (META_TABLE_OP_INSERT == op) {
44,516!
257
    code = tdbTbInsert(pMeta->pSkmDb, &key, sizeof(key), value, valueSize, pMeta->txn);
×
258
  } else if (META_TABLE_OP_UPDATA == op) {
44,547!
259
    code = tdbTbUpsert(pMeta->pSkmDb, &key, sizeof(key), value, valueSize, pMeta->txn);
44,547✔
260
  } else {
261
    code = TSDB_CODE_INVALID_PARA;
×
262
  }
263
  if (TSDB_CODE_SUCCESS != code) {
44,676!
264
    metaErr(vgId, code);
×
265
  }
266
  taosMemoryFree(value);
44,689!
267
  return code;
44,696✔
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,
18,940✔
275
                                                 const SSchema *pOldColumn, const SSchema *pNewColumn) {
276
  int32_t code = TSDB_CODE_SUCCESS;
18,940✔
277

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

282
  if (pOldColumn && pNewColumn) {
18,940✔
283
    if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
17,542✔
284
      return TSDB_CODE_SUCCESS;
1,885✔
285
    } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) {
15,657!
286
      action = DROP_INDEX;
77✔
287
    } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
15,580!
288
      action = ADD_INDEX;
22✔
289
    } else {
290
      return TSDB_CODE_SUCCESS;
15,558✔
291
    }
292
  } else if (pOldColumn) {
1,398✔
293
    if (IS_IDX_ON(pOldColumn)) {
465✔
294
      action = DROP_INDEX;
32✔
295
    } else {
296
      return TSDB_CODE_SUCCESS;
433✔
297
    }
298
  } else {
299
    if (IS_IDX_ON(pNewColumn)) {
933!
300
      action = ADD_INDEX;
×
301
    } else {
302
      return TSDB_CODE_SUCCESS;
933✔
303
    }
304
  }
305

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

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

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

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

330
    if (action == ADD_INDEX) {
689✔
331
      code = metaFetchTagIdxKey(pMeta, pChildEntry, pNewColumn, &pTagIdxKey, &tagIdxKeySize);
316✔
332
      if (code) {
316!
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);
316✔
340
      if (code) {
316!
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);
373✔
349
      if (code) {
373!
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);
373✔
357
      if (code) {
373!
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);
689✔
367
    metaFetchEntryFree(&pChildEntry);
689✔
368
  }
369

370
  taosArrayDestroy(childTables);
130✔
371
  return code;
132✔
372
}
373

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

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

382
  if (pOldColumn && pNewColumn) {
346✔
383
    return TSDB_CODE_SUCCESS;
332✔
384
  } else if (pOldColumn) {
14✔
385
    action = DROP_COLUMN;
6✔
386
  } else {
387
    action = ADD_COLUMN;
8✔
388
  }
389

390
  // fetch all child tables
391
  SArray *childTables = 0;
14✔
392
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childTables);
14✔
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) {
2,040✔
454
  int32_t               code = TSDB_CODE_SUCCESS;
2,040✔
455
  const SMetaEntry     *pEntry = pParam->pEntry;
2,040✔
456
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
2,040✔
457
  const SSchemaWrapper *pNewTagSchema = &pEntry->stbEntry.schemaTag;
2,040✔
458
  const SSchemaWrapper *pOldTagSchema = &pOldEntry->stbEntry.schemaTag;
2,040✔
459

460
  int32_t iOld = 0, iNew = 0;
2,040✔
461
  for (; iOld < pOldTagSchema->nCols && iNew < pNewTagSchema->nCols;) {
19,830✔
462
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
17,793✔
463
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
17,793✔
464

465
    if (pOldColumn->colId == pNewColumn->colId) {
17,793✔
466
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
17,536✔
467
      if (code) {
17,546✔
468
        metaErr(TD_VID(pMeta->pVnode), code);
9!
469
        return code;
×
470
      }
471

472
      iOld++;
17,537✔
473
      iNew++;
17,537✔
474
    } else if (pOldColumn->colId < pNewColumn->colId) {
257!
475
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
257✔
476
      if (code) {
255✔
477
        metaErr(TD_VID(pMeta->pVnode), code);
2!
478
        return code;
×
479
      }
480

481
      iOld++;
253✔
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++) {
2,245✔
494
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
207✔
495
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
207✔
496
    if (code) {
208!
497
      metaErr(TD_VID(pMeta->pVnode), code);
×
498
      return code;
×
499
    }
500
  }
501

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

511
  return code;
2,042✔
512
}
513

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

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

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

533
      iOld++;
332✔
534
      iNew++;
332✔
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++) {
20✔
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++) {
26✔
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;
18✔
573
}
574

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

578
  const SMetaEntry *pEntry = pParam->pEntry;
46,694✔
579
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
46,694✔
580

581
  if (NULL == pOldEntry) {
46,694✔
582
    return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
33,292✔
583
  }
584

585
  if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
13,402✔
586
    // check row schema
587
    if (pOldEntry->ntbEntry.schemaRow.version != pEntry->ntbEntry.schemaRow.version) {
5,492!
588
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
5,522✔
589
    }
590
  } else if (pEntry->type == TSDB_SUPER_TABLE) {
7,910!
591
    // check row schema
592
    if (pOldEntry->stbEntry.schemaRow.version != pEntry->stbEntry.schemaRow.version) {
7,919✔
593
      if (TABLE_IS_VIRTUAL(pEntry->flags)) {
5,863✔
594
        return metaUpdateSuperTableRowSchema(pMeta, pParam);
18✔
595
      } else {
596
        return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
5,845✔
597
      }
598
    }
599

600
    // check tag schema
601
    code = metaUpdateSuperTableTagSchema(pMeta, pParam);
2,056✔
602
    if (code) {
2,043!
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;
2,096✔
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) {
190,000✔
621
  pInfo->uid = pEntry->uid;
190,000✔
622
  pInfo->version = pEntry->version;
190,000✔
623
  if (pEntry->type == TSDB_SUPER_TABLE) {
190,000✔
624
    pInfo->suid = pEntry->uid;
32,765✔
625
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
32,765✔
626
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
157,235✔
627
    pInfo->suid = pEntry->ctbEntry.suid;
143,212✔
628
    pInfo->skmVer = 0;
143,212✔
629
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
14,023!
630
    pInfo->suid = 0;
14,023✔
631
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
14,023✔
632
  }
633
}
190,000✔
634

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

639
  const SMetaEntry *pEntry = pParam->pEntry;
189,974✔
640

641
  // update cache
642
  SMetaInfo info = {0};
189,974✔
643
  metaBuildEntryInfo(pEntry, &info);
189,974✔
644
  code = metaCacheUpsert(pMeta, &info);
190,046✔
645
  if (code) {
190,034!
646
    metaErr(vgId, code);
×
647
  }
648

649
  // put to tdb
650
  SUidIdxVal value = {
189,982✔
651
      .suid = info.suid,
189,982✔
652
      .skmVer = info.skmVer,
189,982✔
653
      .version = pEntry->version,
189,982✔
654
  };
655
  if (META_TABLE_OP_INSERT == op) {
189,982✔
656
    code = tdbTbInsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
172,746✔
657
  } else if (META_TABLE_OP_UPDATA == op) {
17,236!
658
    code = tdbTbUpsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
17,245✔
659
  }
660
  return code;
190,194✔
661
}
662

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

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

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

674
  const SMetaEntry *pEntry = pParam->pOldEntry;
18,225✔
675

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

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

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

691
  const SMetaEntry *pEntry = pParam->pEntry;
172,775✔
692

693
  if (META_TABLE_OP_INSERT == op) {
172,775!
694
    code = tdbTbInsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
172,820✔
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) {
172,881!
703
    metaErr(TD_VID(pMeta->pVnode), code);
×
704
  }
705
  return code;
172,858✔
706
}
707

708
static int32_t metaNameIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
172,803✔
709
  int32_t code = TSDB_CODE_SUCCESS;
172,803✔
710
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
172,803✔
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) {
18,226✔
718
  int32_t code = TSDB_CODE_SUCCESS;
18,226✔
719

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

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

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

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

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

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

753
  const SMetaEntry *pEntry = pParam->pEntry;
142,586✔
754

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

760
  if (META_TABLE_OP_INSERT == op) {
142,586✔
761
    code = tdbTbInsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
139,547✔
762
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
139,547✔
763
  } else if (META_TABLE_OP_UPDATA == op) {
3,039✔
764
    code = tdbTbUpsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
3,037✔
765
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
3,037✔
766
  } else {
767
    code = TSDB_CODE_INVALID_PARA;
2✔
768
  }
769
  return code;
142,586✔
770
}
771

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

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

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

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

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

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

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

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

813
  if (tTagGet((const STag *)pEntry->ctbEntry.pTags, &tagVal)) {
160,736✔
814
    if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
148,512!
815
      pTagData = tagVal.pData;
32,168✔
816
      nTagData = (int32_t)tagVal.nData;
32,168✔
817
    } else {
818
      pTagData = &(tagVal.i64);
116,344✔
819
      nTagData = tDataTypes[pTagColumn->type].bytes;
116,344✔
820
    }
821
  } else {
822
    if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
12,240!
823
      nTagData = tDataTypes[pTagColumn->type].bytes;
11,759✔
824
    }
825
  }
826

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

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

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

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

847
  const SMetaEntry *pEntry = pParam->pEntry;
139,544✔
848
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
139,544✔
849

850
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
139,544✔
851
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
140,179✔
852
    const SSchema *pTagColumn = &pTagSchema->pSchema[0];
635✔
853

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

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

870
      if (!IS_IDX_ON(pTagColumn)) {
505,256✔
871
        continue;
366,252✔
872
      }
873

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

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

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

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

902
  if (pNewTags->len == pOldTags->len && !memcmp(pNewTags, pOldTags, pNewTags->len)) {
3,764✔
903
    return code;
727✔
904
  }
905

906
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
3,037✔
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++) {
11,024✔
920
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
7,995✔
921

922
      if (!IS_IDX_ON(pTagColumn)) {
7,995✔
923
        continue;
4,966✔
924
      }
925

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

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

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

944
      if (tagIdxKeyCmpr(pOldTagIdxKey, oldTagIdxKeySize, pNewTagIdxKey, newTagIdxKeySize)) {
3,029✔
945
        code = tdbTbDelete(pMeta->pTagIdx, pOldTagIdxKey, oldTagIdxKeySize, pMeta->txn);
1,251✔
946
        if (code) {
1,251!
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);
1,251✔
954
        if (code) {
1,251!
955
          metaErr(TD_VID(pMeta->pVnode), code);
×
956
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
957
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
958
          return code;
×
959
        }
960
      }
961

962
      metaFetchTagIdxKeyFree(&pOldTagIdxKey);
3,029✔
963
      metaFetchTagIdxKeyFree(&pNewTagIdxKey);
3,029✔
964
    }
965
  }
966
  return code;
3,037✔
967
}
968

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

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

979
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
14,994✔
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++) {
46,858✔
987
      pTagColumn = &pTagSchema->pSchema[i];
32,061✔
988
      if (!IS_IDX_ON(pTagColumn)) {
32,061✔
989
        continue;
17,069✔
990
      }
991

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

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

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

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

1017
  const SMetaEntry *pEntry;
1018
  if (META_TABLE_OP_DELETE == op) {
164,127✔
1019
    pEntry = pParam->pOldEntry;
16,139✔
1020
  } else {
1021
    pEntry = pParam->pEntry;
147,988✔
1022
  }
1023

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

1028
  if (TSDB_CHILD_TABLE == pEntry->type || TSDB_VIRTUAL_CHILD_TABLE == pEntry->type) {
164,127✔
1029
    key.btime = pEntry->ctbEntry.btime;
154,535✔
1030
  } else if (TSDB_NORMAL_TABLE == pEntry->type || TSDB_VIRTUAL_NORMAL_TABLE == pEntry->type) {
9,592!
1031
    key.btime = pEntry->ntbEntry.btime;
9,592✔
1032
  } else {
1033
    return TSDB_CODE_INVALID_PARA;
×
1034
  }
1035

1036
  if (META_TABLE_OP_INSERT == op) {
164,127✔
1037
    code = tdbTbInsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
147,994✔
1038
  } else if (META_TABLE_OP_UPDATA == op) {
16,133!
1039
    code = tdbTbUpsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
×
1040
  } else if (META_TABLE_OP_DELETE == op) {
16,133!
1041
    code = tdbTbDelete(pMeta->pBtimeIdx, &key, sizeof(key), pMeta->txn);
16,139✔
1042
  } else {
1043
    code = TSDB_CODE_INVALID_PARA;
×
1044
  }
1045
  if (code) {
164,138!
1046
    metaErr(TD_VID(pMeta->pVnode), code);
×
1047
  }
1048
  return code;
164,138✔
1049
}
1050

1051
static int32_t metaBtimeIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
147,990✔
1052
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
147,990✔
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) {
16,139✔
1060
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_DELETE);
16,139✔
1061
}
1062

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

1067
  STtlUpdTtlCtx ctx = {
147,739✔
1068
      .uid = pEntry->uid,
147,739✔
1069
      .pTxn = pMeta->txn,
147,739✔
1070
  };
1071
  if (TSDB_CHILD_TABLE == pEntry->type) {
147,739✔
1072
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
139,436✔
1073
    ctx.changeTimeMs = pEntry->ctbEntry.btime;
139,436✔
1074
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
8,303!
1075
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
8,305✔
1076
    ctx.changeTimeMs = pEntry->ntbEntry.btime;
8,305✔
1077
  } else {
1078
    return TSDB_CODE_INVALID_PARA;
×
1079
  }
1080

1081
  int32_t ret = ttlMgrInsertTtl(pMeta->pTtlMgr, &ctx);
147,741✔
1082
  if (ret < 0) {
147,738!
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;
147,737✔
1086
}
1087

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

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

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

1097
  const SMetaEntry *pEntry = pParam->pEntry;
9,010✔
1098
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
9,010✔
1099

1100
  if ((pEntry->type == TSDB_CHILD_TABLE && pOldEntry->ctbEntry.ttlDays != pEntry->ctbEntry.ttlDays) ||
9,010✔
1101
      (pEntry->type == TSDB_NORMAL_TABLE && pOldEntry->ntbEntry.ttlDays != pEntry->ntbEntry.ttlDays)) {
8,995✔
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;
9,010✔
1114
}
1115

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

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

1125
  if (TSDB_CHILD_TABLE == pEntry->type) {
16,119✔
1126
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
14,968✔
1127
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
1,151✔
1128
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
1,115✔
1129
  } else {
1130
    code = TSDB_CODE_INVALID_PARA;
36✔
1131
  }
1132

1133
  if (TSDB_CODE_SUCCESS == code) {
16,119✔
1134
    int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
16,083✔
1135
    if (ret < 0) {
16,082!
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;
16,116✔
1141
}
1142

1143
static void metaTimeSeriesNotifyCheck(SMeta *pMeta) {
156,580✔
1144
#if defined(TD_ENTERPRISE)
1145
  int64_t nTimeSeries = metaGetTimeSeriesNum(pMeta, 0);
156,580✔
1146
  int64_t deltaTS = nTimeSeries - pMeta->pVnode->config.vndStats.numOfReportedTimeSeries;
156,591✔
1147
  if (deltaTS > tsTimeSeriesThreshold) {
156,591✔
1148
    if (0 == atomic_val_compare_exchange_8(&dmNotifyHdl.state, 1, 2)) {
87,592✔
1149
      if (tsem_post(&dmNotifyHdl.sem) != 0) {
87,571!
1150
        metaError("vgId:%d, failed to post semaphore, errno:%d", TD_VID(pMeta->pVnode), ERRNO);
×
1151
      }
1152
    }
1153
  }
1154
#endif
1155
}
156,591✔
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) {
24,918✔
1216
  int32_t code = TSDB_CODE_SUCCESS;
24,918✔
1217

1218
  SMetaTableOp ops[] = {
24,918✔
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++) {
149,122✔
1227
    SMetaTableOp          *op = &ops[i];
124,283✔
1228
    const SMetaHandleParam param = {
124,283✔
1229
        .pEntry = pEntry,
1230
    };
1231
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
124,283✔
1232
    if (TSDB_CODE_SUCCESS != code) {
124,194!
1233
      metaErr(TD_VID(pMeta->pVnode), code);
×
1234
      return code;
×
1235
    }
1236
  }
1237

1238
  return code;
24,839✔
1239
}
1240
static int32_t metaHandleSuperTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
24,359✔
1241
  int32_t code = TSDB_CODE_SUCCESS;
24,359✔
1242

1243
  metaWLock(pMeta);
24,359✔
1244
  code = metaHandleSuperTableCreateImpl(pMeta, pEntry);
24,945✔
1245
  metaULock(pMeta);
24,845✔
1246

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

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

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

1261
  SMetaTableOp ops[] = {
8,289✔
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++) {
58,022✔
1271
    SMetaTableOp *op = &ops[i];
49,733✔
1272

1273
    SMetaHandleParam param = {
49,733✔
1274
        .pEntry = pEntry,
1275
    };
1276

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

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

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

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

1299
    if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
8,289!
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);
8,289✔
1306
  } else {
1307
    metaErr(TD_VID(pMeta->pVnode), code);
×
1308
  }
1309
  return code;
8,289✔
1310
}
1311

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

1315
  SMetaTableOp ops[] = {
139,430✔
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,115,157✔
1326
    SMetaTableOp *op = &ops[i];
975,792✔
1327

1328
    SMetaHandleParam param = {
975,792✔
1329
        .pEntry = pEntry,
1330
        .pSuperEntry = pSuperEntry,
1331
    };
1332

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

1340
  if (TSDB_CODE_SUCCESS == code) {
139,365!
1341
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
139,421✔
1342
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
139,423✔
1343
    if (ret < 0) {
139,429!
1344
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1345
    }
1346

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

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

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

1366
  // update TDB
1367
  metaWLock(pMeta);
139,439✔
1368
  code = metaHandleChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
139,438✔
1369
  metaULock(pMeta);
139,430✔
1370

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

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

1384
    if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
139,434!
1385
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, pEntry->ctbEntry.suid, NULL);
3,032✔
1386
      if (rc < 0) {
3,032!
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);
139,434✔
1396
  metaFetchEntryFree(&pSuperEntry);
139,427✔
1397
  return code;
139,435✔
1398
}
1399

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

1403
  SMetaTableOp ops[] = {
158✔
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++) {
948✔
1412
    SMetaTableOp *op = &ops[i];
790✔
1413

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

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

1425
  return code;
158✔
1426
}
1427

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

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

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

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

1449
  SMetaTableOp ops[] = {
121✔
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++) {
847✔
1459
    SMetaTableOp *op = &ops[i];
726✔
1460

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

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

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

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

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

1491
  return code;
121✔
1492
}
1493

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

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

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

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

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

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

1524
  SMetaTableOp ops[] = {
1,099✔
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++) {
6,594✔
1535
    SMetaTableOp *op = &ops[i];
5,495✔
1536
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
5,495✔
1537
    if (code) {
5,495!
1538
      const SMetaEntry *pEntry = pParam->pEntry;
×
1539
      metaErr(TD_VID(pMeta->pVnode), code);
×
1540
    }
1541
  }
1542

1543
  return code;
1,099✔
1544
}
1545

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

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

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

1562
  // do the drop
1563
  metaWLock(pMeta);
1,099✔
1564
  code = metaHandleNormalTableDropImpl(pMeta, &param);
1,099✔
1565
  metaULock(pMeta);
1,099✔
1566
  if (code) {
1,099!
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--;
1,099✔
1574
  pMeta->pVnode->config.vndStats.numOfNTimeSeries -= (pOldEntry->ntbEntry.schemaRow.nCols - 1);
1,099✔
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) {
1,099!
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);
1,099✔
1593
  return code;
1,099✔
1594
}
1595

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

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

1603
  SMetaTableOp ops[] = {
14,988✔
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++) {
119,865✔
1614
    SMetaTableOp *op = &ops[i];
104,917✔
1615

1616
    if (op->table == META_ENTRY_TABLE && superDropped) {
104,917✔
1617
      continue;
1,399✔
1618
    }
1619

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

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

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

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

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

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

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

1667
  // do the drop
1668
  metaWLock(pMeta);
14,989✔
1669
  code = metaHandleChildTableDropImpl(pMeta, &param, superDropped);
14,989✔
1670
  metaULock(pMeta);
14,989✔
1671
  if (code) {
14,989✔
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)) {
14,953!
1680
    int32_t      nCols = 0;
14,953✔
1681
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
14,953✔
1682
    if (metaGetStbStats(pMeta->pVnode, pSuper->uid, NULL, &nCols, 0) == 0) {
14,953!
1683
      pStats->numOfTimeSeries -= nCols - 1;
14,953✔
1684
    }
1685
  }
1686

1687
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
14,953!
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);
14,953✔
1707
  metaFetchEntryFree(&pSuper);
14,953✔
1708
  return code;
14,953✔
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,222✔
1877
  int32_t code = TSDB_CODE_SUCCESS;
2,222✔
1878
  void   *key = NULL;
2,222✔
1879
  int32_t keySize = 0;
2,222✔
1880
  int32_t c;
1881

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

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

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

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

1911
    if (((SCtbIdxKey *)key)->suid < suid) {
2,926✔
1912
      continue;
388✔
1913
    } else if (((SCtbIdxKey *)key)->suid > suid) {
2,538✔
1914
      break;
435✔
1915
    }
1916

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

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

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

1935
  SMetaTableOp ops[] = {
2,087✔
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++) {
10,434✔
1945
    SMetaTableOp *op = &ops[i];
8,347✔
1946

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

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

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

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

1966
  SMetaTableOp ops[] = {
5,256✔
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,280✔
1973
    SMetaTableOp *op = &ops[i];
21,024✔
1974
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
21,024✔
1975
    if (code) {
21,024!
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,256✔
1986
}
1987

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

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

1993
  SMetaTableOp ops[] = {
320✔
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,280✔
1999
    SMetaTableOp *op = &ops[i];
960✔
2000
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
960✔
2001
    if (code) {
960!
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;
320✔
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) {
3,754✔
2052
  int32_t code = TSDB_CODE_SUCCESS;
3,754✔
2053

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

2058
  SMetaTableOp ops[] = {
3,754✔
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++) {
22,524✔
2067
    SMetaTableOp *op = &ops[i];
18,770✔
2068
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
18,770✔
2069
    if (code) {
18,770!
2070
      metaErr(TD_VID(pMeta->pVnode), code);
×
2071
      return code;
×
2072
    }
2073
  }
2074

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

2079
  if (metaTbGroupCacheClear(pMeta, pSuperEntry->uid) < 0) {
3,754!
2080
    metaErr(TD_VID(pMeta->pVnode), code);
×
2081
  }
2082
  return code;
3,754✔
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) {
7,902✔
2091
  int32_t code = TSDB_CODE_SUCCESS;
7,902✔
2092

2093
  const SMetaEntry *pEntry = pParam->pEntry;
7,902✔
2094
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
7,902✔
2095

2096
  SMetaTableOp ops[] = {
7,902✔
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++) {
31,637✔
2103
    SMetaTableOp *op = &ops[i];
23,751✔
2104
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
23,751✔
2105
    if (code) {
23,718!
2106
      metaErr(TD_VID(pMeta->pVnode), code);
×
2107
      return code;
×
2108
    }
2109
  }
2110

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

2116
  return code;
7,921✔
2117
}
2118

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

2122
  SMetaEntry *pOldEntry = NULL;
7,892✔
2123

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

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

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

2148
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
7,911!
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) {
7,911✔
2201
    int64_t ctbNum = 0;
4,295✔
2202
    int32_t ret = metaGetStbStats(pMeta->pVnode, pEntry->uid, &ctbNum, 0, 0);
4,295✔
2203
    if (ret < 0) {
4,304!
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);
4,297✔
2208
    if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
4,297✔
2209
  }
2210
  metaFetchEntryFree(&pOldEntry);
7,910✔
2211
  return code;
7,907✔
2212
}
2213

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

2217
  SMetaEntry *pOldEntry = NULL;
3,754✔
2218
  SMetaEntry *pSuperEntry = NULL;
3,754✔
2219

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

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

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

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

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

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

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

2265
  // handle update
2266
  SMetaHandleParam param = {
5,256✔
2267
      .pEntry = pEntry,
2268
      .pOldEntry = pOldEntry,
2269
  };
2270
  metaWLock(pMeta);
5,256✔
2271
  code = metaHandleNormalTableUpdateImpl(pMeta, &param);
5,256✔
2272
  metaULock(pMeta);
5,256✔
2273
  if (code) {
5,256!
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,256✔
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,256✔
2311
  pMeta->pVnode->config.vndStats.numOfNTimeSeries += deltaCol;
5,256✔
2312
  if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
5,256✔
2313
  metaFetchEntryFree(&pOldEntry);
5,256✔
2314
  return code;
5,256✔
2315
}
2316

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

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

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

2342
  metaTimeSeriesNotifyCheck(pMeta);
320✔
2343
  metaFetchEntryFree(&pOldEntry);
320✔
2344
  return code;
320✔
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,086✔
2388
  int32_t     code = TSDB_CODE_SUCCESS;
2,086✔
2389
  SArray     *childList = NULL;
2,086✔
2390
  SMetaEntry *pOldEntry = NULL;
2,086✔
2391

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

2398
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childList);
2,084✔
2399
  if (code) {
2,085!
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,085✔
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++) {
3,485✔
2412
    SMetaEntry childEntry = {
2,798✔
2413
        .version = pEntry->version,
1,399✔
2414
        .uid = *(tb_uid_t *)taosArrayGet(childList, i),
1,399✔
2415
        .type = -TSDB_CHILD_TABLE,
2416
    };
2417

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

2424
  // do drop super table
2425
  SMetaHandleParam param = {
2,085✔
2426
      .pEntry = pEntry,
2427
      .pOldEntry = pOldEntry,
2428
  };
2429
  metaWLock(pMeta);
2,085✔
2430
  code = metaHandleSuperTableDropImpl(pMeta, &param);
2,087✔
2431
  metaULock(pMeta);
2,087✔
2432
  if (code) {
2,087!
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,087✔
2444
  metaFetchEntryFree(&pOldEntry);
2,087✔
2445
  return code;
2,087✔
2446
}
2447

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

2454
  if (NULL == pMeta || NULL == pEntry) {
206,942!
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) {
207,018✔
2460
    bool isExist = false;
190,184✔
2461
    if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) {
190,184✔
2462
      isExist = true;
17,255✔
2463
    }
2464

2465
    switch (type) {
190,199✔
2466
      case TSDB_SUPER_TABLE: {
32,812✔
2467
        if (isExist) {
32,812✔
2468
          code = metaHandleSuperTableUpdate(pMeta, pEntry);
7,908✔
2469
        } else {
2470
          code = metaHandleSuperTableCreate(pMeta, pEntry);
24,904✔
2471
        }
2472
        break;
32,854✔
2473
      }
2474
      case TSDB_CHILD_TABLE: {
143,184✔
2475
        if (isExist) {
143,184✔
2476
          code = metaHandleChildTableUpdate(pMeta, pEntry);
3,754✔
2477
        } else {
2478
          code = metaHandleChildTableCreate(pMeta, pEntry);
139,430✔
2479
        }
2480
        break;
143,188✔
2481
      }
2482
      case TSDB_NORMAL_TABLE: {
13,545✔
2483
        if (isExist) {
13,545✔
2484
          code = metaHandleNormalTableUpdate(pMeta, pEntry);
5,256✔
2485
        } else {
2486
          code = metaHandleNormalTableCreate(pMeta, pEntry);
8,289✔
2487
        }
2488
        break;
13,545✔
2489
      }
2490
      case TSDB_VIRTUAL_NORMAL_TABLE: {
478✔
2491
        if (isExist) {
478✔
2492
          code = metaHandleVirtualNormalTableUpdate(pMeta, pEntry);
320✔
2493
        } else {
2494
          code = metaHandleVirtualNormalTableCreate(pMeta, pEntry);
158✔
2495
        }
2496
        break;
478✔
2497
      }
2498
      case TSDB_VIRTUAL_CHILD_TABLE: {
131✔
2499
        if (isExist) {
131✔
2500
          code = metaHandleVirtualChildTableUpdate(pMeta, pEntry);
10✔
2501
        } else {
2502
          code = metaHandleVirtualChildTableCreate(pMeta, pEntry);
121✔
2503
        }
2504
        break;
131✔
2505
      }
2506
      default: {
49✔
2507
        code = TSDB_CODE_INVALID_PARA;
49✔
2508
        break;
49✔
2509
      }
2510
    }
2511
  } else {
2512
    switch (type) {
16,834✔
2513
      case TSDB_SUPER_TABLE: {
2,087✔
2514
        code = metaHandleSuperTableDrop(pMeta, pEntry);
2,087✔
2515
        break;
2,087✔
2516
      }
2517
      case TSDB_CHILD_TABLE: {
13,590✔
2518
        code = metaHandleChildTableDrop(pMeta, pEntry, false);
13,590✔
2519
        break;
13,590✔
2520
      }
2521
      case TSDB_NORMAL_TABLE: {
1,099✔
2522
        code = metaHandleNormalTableDrop(pMeta, pEntry);
1,099✔
2523
        break;
1,099✔
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: {
7✔
2534
        code = TSDB_CODE_INVALID_PARA;
7✔
2535
        break;
7✔
2536
      }
2537
    }
2538
  }
2539

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

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

© 2026 Coveralls, Inc