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

taosdata / TDengine / #4513

17 Jul 2025 02:02AM UTC coverage: 31.359% (-31.1%) from 62.446%
#4513

push

travis-ci

web-flow
Merge pull request #31914 from taosdata/fix/3.0/compare-ans-failed

fix:Convert line endings from LF to CRLF for ans file

68541 of 301034 branches covered (22.77%)

Branch coverage included in aggregate %.

117356 of 291771 relevant lines covered (40.22%)

602262.98 hits per line

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

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

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

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

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

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

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

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

142
void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); }
230✔
143

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

282
  if (pOldColumn && pNewColumn) {
26✔
283
    if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
22!
284
      return TSDB_CODE_SUCCESS;
8✔
285
    } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) {
14!
286
      action = DROP_INDEX;
×
287
    } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
14!
288
      action = ADD_INDEX;
×
289
    } else {
290
      return TSDB_CODE_SUCCESS;
14✔
291
    }
292
  } else if (pOldColumn) {
4✔
293
    if (IS_IDX_ON(pOldColumn)) {
2!
294
      action = DROP_INDEX;
×
295
    } else {
296
      return TSDB_CODE_SUCCESS;
2✔
297
    }
298
  } else {
299
    if (IS_IDX_ON(pNewColumn)) {
2!
300
      action = ADD_INDEX;
×
301
    } else {
302
      return TSDB_CODE_SUCCESS;
2✔
303
    }
304
  }
305

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

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

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

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

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

370
  taosArrayDestroy(childTables);
×
371
  return code;
×
372
}
373

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

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

382
  if (pOldColumn && pNewColumn) {
×
383
    return TSDB_CODE_SUCCESS;
×
384
  } else if (pOldColumn) {
×
385
    action = DROP_COLUMN;
×
386
  } else {
387
    action = ADD_COLUMN;
×
388
  }
389

390
  // fetch all child tables
391
  SArray *childTables = 0;
×
392
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childTables);
×
393
  if (code) {
×
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++) {
×
400
    int64_t uid = *(int64_t *)taosArrayGet(childTables, i);
×
401

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

411
    SMetaHandleParam param = {.pEntry = pChildEntry};
×
412

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

422
      code = metaEntryTableUpdate(pMeta, &param);
×
423
      if (code) {
×
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);
×
431
      if (code) {
×
432
        metaErr(TD_VID(pMeta->pVnode), code);
×
433
        taosArrayDestroy(childTables);
×
434
        metaFetchEntryFree(&pChildEntry);
×
435
        return code;
×
436
      }
437

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

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

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

460
  int32_t iOld = 0, iNew = 0;
8✔
461
  for (; iOld < pOldTagSchema->nCols && iNew < pNewTagSchema->nCols;) {
30✔
462
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
22✔
463
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
22✔
464

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

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

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

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

511
  return code;
8✔
512
}
513

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

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

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

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

542
      iOld++;
×
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++) {
×
555
    SSchema *pOldColumn = pOldRowSchema->pSchema + iOld;
×
556
    code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, pOldColumn, NULL);
×
557
    if (code) {
×
558
      metaErr(TD_VID(pMeta->pVnode), code);
×
559
      return code;
×
560
    }
561
  }
562

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

572
  return code;
×
573
}
574

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

578
  const SMetaEntry *pEntry = pParam->pEntry;
91✔
579
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
91✔
580

581
  if (NULL == pOldEntry) {
91✔
582
    return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
75✔
583
  }
584

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

600
    // check tag schema
601
    code = metaUpdateSuperTableTagSchema(pMeta, pParam);
8✔
602
    if (code) {
8!
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;
8✔
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) {
188✔
621
  pInfo->uid = pEntry->uid;
188✔
622
  pInfo->version = pEntry->version;
188✔
623
  if (pEntry->type == TSDB_SUPER_TABLE) {
188✔
624
    pInfo->suid = pEntry->uid;
72✔
625
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
72✔
626
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
116!
627
    pInfo->suid = pEntry->ctbEntry.suid;
97✔
628
    pInfo->skmVer = 0;
97✔
629
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
19!
630
    pInfo->suid = 0;
19✔
631
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
19✔
632
  }
633
}
188✔
634

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

639
  const SMetaEntry *pEntry = pParam->pEntry;
188✔
640

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

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

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

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

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

674
  const SMetaEntry *pEntry = pParam->pOldEntry;
4✔
675

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

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

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

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

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

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

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

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

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

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

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

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

753
  const SMetaEntry *pEntry = pParam->pEntry;
97✔
754

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

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

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

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

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

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

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

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

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

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

813
  if (tTagGet((const STag *)pEntry->ctbEntry.pTags, &tagVal)) {
93!
814
    if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
93!
815
      pTagData = tagVal.pData;
4✔
816
      nTagData = (int32_t)tagVal.nData;
4✔
817
    } else {
818
      pTagData = &(tagVal.i64);
89✔
819
      nTagData = tDataTypes[pTagColumn->type].bytes;
89✔
820
    }
821
  } else {
822
    if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
×
823
      nTagData = tDataTypes[pTagColumn->type].bytes;
×
824
    }
825
  }
826

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

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

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

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

847
  const SMetaEntry *pEntry = pParam->pEntry;
97✔
848
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
97✔
849

850
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
97✔
851
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
101✔
852
    const SSchema *pTagColumn = &pTagSchema->pSchema[0];
4✔
853

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

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

870
      if (!IS_IDX_ON(pTagColumn)) {
158✔
871
        continue;
65✔
872
      }
873

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

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

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

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

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

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

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

922
      if (!IS_IDX_ON(pTagColumn)) {
×
923
        continue;
×
924
      }
925

926
      STagIdxKey *pOldTagIdxKey = NULL;
×
927
      int32_t     oldTagIdxKeySize = 0;
×
928
      STagIdxKey *pNewTagIdxKey = NULL;
×
929
      int32_t     newTagIdxKeySize = 0;
×
930

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

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

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

962
      metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
963
      metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
964
    }
965
  }
966
  return code;
×
967
}
968

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

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

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

992
      STagIdxKey *pTagIdxKey = NULL;
×
993
      int32_t     nTagIdxKey;
994

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

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

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

1017
  const SMetaEntry *pEntry;
1018
  if (META_TABLE_OP_DELETE == op) {
116!
1019
    pEntry = pParam->pOldEntry;
×
1020
  } else {
1021
    pEntry = pParam->pEntry;
116✔
1022
  }
1023

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

1028
  if (TSDB_CHILD_TABLE == pEntry->type || TSDB_VIRTUAL_CHILD_TABLE == pEntry->type) {
116!
1029
    key.btime = pEntry->ctbEntry.btime;
97✔
1030
  } else if (TSDB_NORMAL_TABLE == pEntry->type || TSDB_VIRTUAL_NORMAL_TABLE == pEntry->type) {
19!
1031
    key.btime = pEntry->ntbEntry.btime;
19✔
1032
  } else {
1033
    return TSDB_CODE_INVALID_PARA;
×
1034
  }
1035

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

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

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

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

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

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

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

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

1097
  const SMetaEntry *pEntry = pParam->pEntry;
×
1098
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
×
1099

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

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

1113
  return TSDB_CODE_SUCCESS;
×
1114
}
1115

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

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

1125
  if (TSDB_CHILD_TABLE == pEntry->type) {
×
1126
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
×
1127
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
×
1128
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
×
1129
  } else {
1130
    code = TSDB_CODE_INVALID_PARA;
×
1131
  }
1132

1133
  if (TSDB_CODE_SUCCESS == code) {
×
1134
    int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
×
1135
    if (ret < 0) {
×
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;
×
1141
}
1142

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

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

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

1243
  metaWLock(pMeta);
55✔
1244
  code = metaHandleSuperTableCreateImpl(pMeta, pEntry);
56✔
1245
  metaULock(pMeta);
56✔
1246

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

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

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

1261
  SMetaTableOp ops[] = {
19✔
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++) {
133✔
1271
    SMetaTableOp *op = &ops[i];
114✔
1272

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

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

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

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

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

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

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

1315
  SMetaTableOp ops[] = {
97✔
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++) {
776✔
1326
    SMetaTableOp *op = &ops[i];
679✔
1327

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1425
  return code;
×
1426
}
1427

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

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

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

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

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

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

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

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

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

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

1491
  return code;
×
1492
}
1493

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

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

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

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

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

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

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

1543
  return code;
×
1544
}
1545

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

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

1557
  SMetaHandleParam param = {
×
1558
      .pEntry = pEntry,
1559
      .pOldEntry = pOldEntry,
1560
  };
1561

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

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

1599
  const SMetaEntry *pEntry = pParam->pEntry;
×
1600
  const SMetaEntry *pChild = pParam->pOldEntry;
×
1601
  const SMetaEntry *pSuper = pParam->pSuperEntry;
×
1602

1603
  SMetaTableOp ops[] = {
×
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++) {
×
1614
    SMetaTableOp *op = &ops[i];
×
1615

1616
    if (op->table == META_ENTRY_TABLE && superDropped) {
×
1617
      continue;
×
1618
    }
1619

1620
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
×
1621
    if (code) {
×
1622
      metaErr(TD_VID(pMeta->pVnode), code);
×
1623
      return code;
×
1624
    }
1625
  }
1626

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

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

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

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

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

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

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

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

1687
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
×
1688
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pChild->uid, pSuper->uid, NULL);
×
1689
    if (ret < 0) {
×
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);
×
1707
  metaFetchEntryFree(&pSuper);
×
1708
  return code;
×
1709
}
1710

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

1714
  SMetaTableOp ops[] = {
×
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++) {
×
1724
    SMetaTableOp *op = &ops[i];
×
1725
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
×
1726
    if (code) {
×
1727
      const SMetaEntry *pEntry = pParam->pEntry;
×
1728
      metaErr(TD_VID(pMeta->pVnode), code);
×
1729
    }
1730
  }
1731

1732
  return code;
×
1733
}
1734

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

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

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

1751
  // do the drop
1752
  metaWLock(pMeta);
×
1753
  code = metaHandleVirtualNormalTableDropImpl(pMeta, &param);
×
1754
  metaULock(pMeta);
×
1755
  if (code) {
×
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--;
×
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) {
×
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);
×
1781
  return code;
×
1782
}
1783

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

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

1791
  SMetaTableOp ops[] = {
×
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++) {
×
1801
    SMetaTableOp *op = &ops[i];
×
1802

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

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

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

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

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

1831
  return code;
×
1832
}
1833

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

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

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

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

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

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

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

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

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

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

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

1911
    if (((SCtbIdxKey *)key)->suid < suid) {
×
1912
      continue;
×
1913
    } else if (((SCtbIdxKey *)key)->suid > suid) {
×
1914
      break;
×
1915
    }
1916

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

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

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

1935
  SMetaTableOp ops[] = {
4✔
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++) {
20✔
1945
    SMetaTableOp *op = &ops[i];
16✔
1946

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

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

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

1964
  const SMetaEntry *pEntry = pParam->pEntry;
×
1965

1966
  SMetaTableOp ops[] = {
×
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++) {
×
1973
    SMetaTableOp *op = &ops[i];
×
1974
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
×
1975
    if (code) {
×
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;
×
1986
}
1987

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

1991
  const SMetaEntry *pEntry = pParam->pEntry;
×
1992

1993
  SMetaTableOp ops[] = {
×
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++) {
×
1999
    SMetaTableOp *op = &ops[i];
×
2000
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
×
2001
    if (code) {
×
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;
×
2012
}
2013

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

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

2021
  SMetaTableOp ops[] = {
×
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++) {
×
2029
    SMetaTableOp *op = &ops[i];
×
2030
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
×
2031
    if (code) {
×
2032
      metaErr(TD_VID(pMeta->pVnode), code);
×
2033
      return code;
×
2034
    }
2035
  }
2036

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

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

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

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

2054
  const SMetaEntry *pEntry = pParam->pEntry;
×
2055
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
×
2056
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
×
2057

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

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

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

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

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

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

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

2116
  return code;
16✔
2117
}
2118

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

2122
  SMetaEntry *pOldEntry = NULL;
16✔
2123

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

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

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

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

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

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

2196
    if (pTsdb) {
×
2197
      tsdbCacheInvalidateSchema(pTsdb, pEntry->uid, -1, pEntry->stbEntry.schemaRow.version);
×
2198
    }
2199
  }
2200
  if (updStat) {
16✔
2201
    int64_t ctbNum = 0;
6✔
2202
    int32_t ret = metaGetStbStats(pMeta->pVnode, pEntry->uid, &ctbNum, 0, 0);
6✔
2203
    if (ret < 0) {
6!
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);
6✔
2208
    if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
6✔
2209
  }
2210
  metaFetchEntryFree(&pOldEntry);
16✔
2211
  return code;
16✔
2212
}
2213

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

2217
  SMetaEntry *pOldEntry = NULL;
×
2218
  SMetaEntry *pSuperEntry = NULL;
×
2219

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

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

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

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

2249
  metaFetchEntryFree(&pOldEntry);
×
2250
  metaFetchEntryFree(&pSuperEntry);
×
2251
  return code;
×
2252
}
2253

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

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

2265
  // handle update
2266
  SMetaHandleParam param = {
×
2267
      .pEntry = pEntry,
2268
      .pOldEntry = pOldEntry,
2269
  };
2270
  metaWLock(pMeta);
×
2271
  code = metaHandleNormalTableUpdateImpl(pMeta, &param);
×
2272
  metaULock(pMeta);
×
2273
  if (code) {
×
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) &&
×
2281
      pEntry->ntbEntry.schemaRow.version != pOldEntry->ntbEntry.schemaRow.version) {
×
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) {
×
2307
      tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, pEntry->uid, pEntry->ntbEntry.schemaRow.version);
×
2308
    }
2309
  }
2310
  int32_t deltaCol = pEntry->ntbEntry.schemaRow.nCols - pOldEntry->ntbEntry.schemaRow.nCols;
×
2311
  pMeta->pVnode->config.vndStats.numOfNTimeSeries += deltaCol;
×
2312
  if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
×
2313
  metaFetchEntryFree(&pOldEntry);
×
2314
  return code;
×
2315
}
2316

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

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

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

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

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

2350
  SMetaEntry *pOldEntry = NULL;
×
2351
  SMetaEntry *pSuperEntry = NULL;
×
2352

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

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

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

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

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

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

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

2398
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childList);
4✔
2399
  if (code) {
4!
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) {
4!
2406
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
×
2407
              pEntry->uid, tstrerror(terrno));
2408
  }
2409

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

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

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

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

2454
  if (NULL == pMeta || NULL == pEntry) {
192!
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) {
192✔
2460
    bool isExist = false;
188✔
2461
    if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) {
188✔
2462
      isExist = true;
16✔
2463
    }
2464

2465
    switch (type) {
188!
2466
      case TSDB_SUPER_TABLE: {
72✔
2467
        if (isExist) {
72✔
2468
          code = metaHandleSuperTableUpdate(pMeta, pEntry);
16✔
2469
        } else {
2470
          code = metaHandleSuperTableCreate(pMeta, pEntry);
56✔
2471
        }
2472
        break;
72✔
2473
      }
2474
      case TSDB_CHILD_TABLE: {
97✔
2475
        if (isExist) {
97!
2476
          code = metaHandleChildTableUpdate(pMeta, pEntry);
×
2477
        } else {
2478
          code = metaHandleChildTableCreate(pMeta, pEntry);
97✔
2479
        }
2480
        break;
97✔
2481
      }
2482
      case TSDB_NORMAL_TABLE: {
19✔
2483
        if (isExist) {
19!
2484
          code = metaHandleNormalTableUpdate(pMeta, pEntry);
×
2485
        } else {
2486
          code = metaHandleNormalTableCreate(pMeta, pEntry);
19✔
2487
        }
2488
        break;
19✔
2489
      }
2490
      case TSDB_VIRTUAL_NORMAL_TABLE: {
×
2491
        if (isExist) {
×
2492
          code = metaHandleVirtualNormalTableUpdate(pMeta, pEntry);
×
2493
        } else {
2494
          code = metaHandleVirtualNormalTableCreate(pMeta, pEntry);
×
2495
        }
2496
        break;
×
2497
      }
2498
      case TSDB_VIRTUAL_CHILD_TABLE: {
×
2499
        if (isExist) {
×
2500
          code = metaHandleVirtualChildTableUpdate(pMeta, pEntry);
×
2501
        } else {
2502
          code = metaHandleVirtualChildTableCreate(pMeta, pEntry);
×
2503
        }
2504
        break;
×
2505
      }
2506
      default: {
×
2507
        code = TSDB_CODE_INVALID_PARA;
×
2508
        break;
×
2509
      }
2510
    }
2511
  } else {
2512
    switch (type) {
4!
2513
      case TSDB_SUPER_TABLE: {
4✔
2514
        code = metaHandleSuperTableDrop(pMeta, pEntry);
4✔
2515
        break;
4✔
2516
      }
2517
      case TSDB_CHILD_TABLE: {
×
2518
        code = metaHandleChildTableDrop(pMeta, pEntry, false);
×
2519
        break;
×
2520
      }
2521
      case TSDB_NORMAL_TABLE: {
×
2522
        code = metaHandleNormalTableDrop(pMeta, pEntry);
×
2523
        break;
×
2524
      }
2525
      case TSDB_VIRTUAL_NORMAL_TABLE: {
×
2526
        code = metaHandleVirtualNormalTableDrop(pMeta, pEntry);
×
2527
        break;
×
2528
      }
2529
      case TSDB_VIRTUAL_CHILD_TABLE: {
×
2530
        code = metaHandleVirtualChildTableDrop(pMeta, pEntry, false);
×
2531
        break;
×
2532
      }
2533
      default: {
×
2534
        code = TSDB_CODE_INVALID_PARA;
×
2535
        break;
×
2536
      }
2537
    }
2538
  }
2539

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

2550
void metaHandleSyncEntry(SMeta *pMeta, const SMetaEntry *pEntry) {
×
2551
  int32_t code = TSDB_CODE_SUCCESS;
×
2552
  code = metaHandleEntry2(pMeta, pEntry);
×
2553
  if (code) {
×
2554
    metaErr(TD_VID(pMeta->pVnode), code);
×
2555
  }
2556
  return;
×
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