• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

taosdata / TDengine / #3831

02 Apr 2025 01:14AM UTC coverage: 34.081% (-0.02%) from 34.097%
#3831

push

travis-ci

happyguoxy
test:alter gcda dir

148596 of 599532 branches covered (24.79%)

Branch coverage included in aggregate %.

222550 of 489473 relevant lines covered (45.47%)

1589752.67 hits per line

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

42.04
/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

13
extern SDmNotifyHandle dmNotifyHdl;
14

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

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

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

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

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

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

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

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

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

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

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

92
  // decode entry
93
  SDecoder   decoder = {0};
48,016✔
94
  SMetaEntry entry = {0};
48,016✔
95

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

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

115
  tdbFreeClear(value);
48,016✔
116
  tDecoderClear(&decoder);
48,016✔
117
  return code;
48,018✔
118
}
119

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

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

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

141
void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); }
48,016✔
142

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

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

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

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

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

180
  // put to tdb
181
  if (META_TABLE_OP_INSERT == op) {
9,012✔
182
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
7,512✔
183
  } else if (META_TABLE_OP_UPDATA == op) {
1,500✔
184
    code = tdbTbUpsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
258✔
185
  } else if (META_TABLE_OP_DELETE == op) {
1,242!
186
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
1,242✔
187
  } else {
188
    code = TSDB_CODE_INVALID_PARA;
×
189
  }
190
  if (TSDB_CODE_SUCCESS != code) {
9,016!
191
    metaErr(vgId, code);
×
192
  }
193
  taosMemoryFree(value);
9,012!
194
  return code;
9,012✔
195
}
196

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

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

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

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

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

231
  // encode schema
232
  tEncodeSize(tEncodeSSchemaWrapper, pSchema, valueSize, code);
1,646!
233
  if (TSDB_CODE_SUCCESS != code) {
824!
234
    metaErr(vgId, code);
×
235
    return code;
×
236
  }
237

238
  value = taosMemoryMalloc(valueSize);
824!
239
  if (NULL == value) {
822!
240
    metaErr(vgId, terrno);
×
241
    return terrno;
×
242
  }
243

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

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

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

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

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

281
  if (pOldColumn && pNewColumn) {
1,788✔
282
    if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
1,756✔
283
      return TSDB_CODE_SUCCESS;
788✔
284
    } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) {
968!
285
      action = DROP_INDEX;
56✔
286
    } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
912!
287
      action = ADD_INDEX;
56✔
288
    } else {
289
      return TSDB_CODE_SUCCESS;
856✔
290
    }
291
  } else if (pOldColumn) {
32✔
292
    if (IS_IDX_ON(pOldColumn)) {
12!
293
      action = DROP_INDEX;
×
294
    } else {
295
      return TSDB_CODE_SUCCESS;
12✔
296
    }
297
  } else {
298
    if (IS_IDX_ON(pNewColumn)) {
20!
299
      action = ADD_INDEX;
×
300
    } else {
301
      return TSDB_CODE_SUCCESS;
20✔
302
    }
303
  }
304

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

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

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

326
    STagIdxKey *pTagIdxKey = NULL;
38,270✔
327
    int32_t     tagIdxKeySize = 0;
38,270✔
328

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

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

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

365
    metaFetchTagIdxKeyFree(&pTagIdxKey);
38,270✔
366
    metaFetchEntryFree(&pChildEntry);
38,270✔
367
  }
368

369
  taosArrayDestroy(childTables);
112✔
370
  return code;
112✔
371
}
372

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

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

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

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

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

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

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

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

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

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

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

453
}
454

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

462
  int32_t iOld = 0, iNew = 0;
164✔
463
  for (; iOld < pOldTagSchema->nCols && iNew < pNewTagSchema->nCols;) {
1,924✔
464
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
1,760✔
465
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
1,760✔
466

467
    if (pOldColumn->colId == pNewColumn->colId) {
1,760✔
468
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
1,756✔
469
      if (code) {
1,756!
470
        metaErr(TD_VID(pMeta->pVnode), code);
×
471
        return code;
×
472
      }
473

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

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

491
      iNew++;
×
492
    }
493
  }
494

495
  for (; iOld < pOldTagSchema->nCols; iOld++) {
172✔
496
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
8✔
497
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
8✔
498
    if (code) {
8!
499
      metaErr(TD_VID(pMeta->pVnode), code);
×
500
      return code;
×
501
    }
502
  }
503

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

513
  return code;
164✔
514
}
515

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

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

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

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

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

552
      iNew++;
×
553
    }
554
  }
555

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

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

574
  return code;
×
575
}
576

577
static int32_t metaSchemaTableUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
984✔
578
  int32_t code = TSDB_CODE_SUCCESS;
984✔
579

580
  const SMetaEntry *pEntry = pParam->pEntry;
984✔
581
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
984✔
582

583
  if (NULL == pOldEntry) {
984✔
584
    return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
732✔
585
  }
586

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

601
    }
602

603
    // check tag schema
604
    code = metaUpdateSuperTableTagSchema(pMeta, pParam);
164✔
605
    if (code) {
164!
606
      metaErr(TD_VID(pMeta->pVnode), code);
×
607
      return code;
×
608
    }
609

610
  } else {
611
    return TSDB_CODE_INVALID_PARA;
×
612
  }
613

614
  return TSDB_CODE_SUCCESS;
164✔
615
}
616

617
static int32_t metaSchemaTableDelete(SMeta *pMeta, const SMetaHandleParam *pEntry) {
×
618
  // TODO
619
  return TSDB_CODE_SUCCESS;
×
620
}
621

622
// Uid Index
623
static void metaBuildEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) {
7,768✔
624
  pInfo->uid = pEntry->uid;
7,768✔
625
  pInfo->version = pEntry->version;
7,768✔
626
  if (pEntry->type == TSDB_SUPER_TABLE) {
7,768✔
627
    pInfo->suid = pEntry->uid;
854✔
628
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
854✔
629
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
6,914!
630
    pInfo->suid = pEntry->ctbEntry.suid;
6,782✔
631
    pInfo->skmVer = 0;
6,782✔
632
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
132!
633
    pInfo->suid = 0;
132✔
634
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
132✔
635
  }
636
}
7,768✔
637

638
static int32_t metaUidIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
7,768✔
639
  int32_t code = TSDB_CODE_SUCCESS;
7,768✔
640
  int32_t vgId = TD_VID(pMeta->pVnode);
7,768✔
641

642
  const SMetaEntry *pEntry = pParam->pEntry;
7,768✔
643

644
  // update cache
645
  SMetaInfo info = {0};
7,768✔
646
  metaBuildEntryInfo(pEntry, &info);
7,768✔
647
  code = metaCacheUpsert(pMeta, &info);
7,762✔
648
  if (code) {
7,764!
649
    metaErr(vgId, code);
×
650
  }
651

652
  // put to tdb
653
  SUidIdxVal value = {
7,764✔
654
      .suid = info.suid,
7,764✔
655
      .skmVer = info.skmVer,
7,764✔
656
      .version = pEntry->version,
7,764✔
657
  };
658
  if (META_TABLE_OP_INSERT == op) {
7,764✔
659
    code = tdbTbInsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
7,506✔
660
  } else if (META_TABLE_OP_UPDATA == op) {
258!
661
    code = tdbTbUpsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
258✔
662
  }
663
  return code;
7,772✔
664
}
665

666
static int32_t metaUidIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
7,510✔
667
  return metaUidIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
7,510✔
668
}
669

670
static int32_t metaUidIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
258✔
671
  return metaUidIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
258✔
672
}
673

674
static int32_t metaUidIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
1,248✔
675
  int32_t code = 0;
1,248✔
676

677
  const SMetaEntry *pEntry = pParam->pOldEntry;
1,248✔
678

679
  // delete tdb
680
  code = tdbTbDelete(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), pMeta->txn);
1,248✔
681
  if (code) {
1,248!
682
    metaErr(TD_VID(pMeta->pVnode), code);
×
683
  }
684

685
  // delete cache
686
  (void)metaCacheDrop(pMeta, pEntry->uid);
1,248✔
687
  return code;
1,248✔
688
}
689

690
// Name Index
691
static int32_t metaNameIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
7,508✔
692
  int32_t code = TSDB_CODE_SUCCESS;
7,508✔
693

694
  const SMetaEntry *pEntry = pParam->pEntry;
7,508✔
695

696
  if (META_TABLE_OP_INSERT == op) {
7,508!
697
    code = tdbTbInsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
7,510✔
698
                       pMeta->txn);
699
  } else if (META_TABLE_OP_UPDATA == op) {
×
700
    code = tdbTbUpsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
×
701
                       pMeta->txn);
702
  } else {
703
    code = TSDB_CODE_INVALID_PARA;
704
  }
705
  return code;
7,516✔
706
}
707

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

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

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

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

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

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

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

753
  const SMetaEntry *pEntry = pParam->pEntry;
6,780✔
754

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

847
  const SMetaEntry *pEntry = pParam->pEntry;
6,782✔
848
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
6,782✔
849

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

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

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

870
      if (!IS_IDX_ON(pTagColumn)) {
42,528✔
871
        continue;
35,748✔
872
      }
873

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

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

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

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

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

906
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
4!
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++) {
20✔
920
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
16✔
921

922
      if (!IS_IDX_ON(pTagColumn)) {
16✔
923
        continue;
12✔
924
      }
925

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

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

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

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

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

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

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

979
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
1,192!
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++) {
17,756✔
987
      pTagColumn = &pTagSchema->pSchema[i];
16,564✔
988
      if (!IS_IDX_ON(pTagColumn)) {
16,564✔
989
        continue;
6✔
990
      }
991

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

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

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

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

1017
  const SMetaEntry *pEntry;
1018
  if (META_TABLE_OP_DELETE == op) {
8,098✔
1019
    pEntry = pParam->pOldEntry;
1,228✔
1020
  } else {
1021
    pEntry = pParam->pEntry;
6,870✔
1022
  }
1023

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

1028
  if (TSDB_CHILD_TABLE == pEntry->type || TSDB_VIRTUAL_CHILD_TABLE == pEntry->type) {
8,098!
1029
    key.btime = pEntry->ctbEntry.btime;
7,968✔
1030
  } else if (TSDB_NORMAL_TABLE == pEntry->type || TSDB_VIRTUAL_NORMAL_TABLE == pEntry->type) {
130!
1031
    key.btime = pEntry->ntbEntry.btime;
130✔
1032
  } else {
1033
    return TSDB_CODE_INVALID_PARA;
×
1034
  }
1035

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1315
  SMetaTableOp ops[] = {
6,784✔
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++) {
54,244✔
1326
    SMetaTableOp *op = &ops[i];
47,460✔
1327

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

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

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

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

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

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

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

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

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

1384
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
6,780✔
1385
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, pEntry->ctbEntry.suid, NULL);
6✔
1386
      if (rc < 0) {
6!
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);
6,780✔
1396
  metaFetchEntryFree(&pSuperEntry);
6,784✔
1397
  return code;
6,780✔
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, const SMetaEntry *pSuperEntry) {
×
1446
  int32_t code = TSDB_CODE_SUCCESS;
×
1447

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

1457
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
×
1458
    SMetaTableOp *op = &ops[i];
×
1459

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

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

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

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

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

1490
  return code;
×
1491
}
1492

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

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

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

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

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

1520
static int32_t metaHandleNormalTableDropImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
36✔
1521
  int32_t code = TSDB_CODE_SUCCESS;
36✔
1522

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

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

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

1542
  return code;
36✔
1543
}
1544

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

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

1556
  SMetaHandleParam param = {
36✔
1557
      .pEntry = pEntry,
1558
      .pOldEntry = pOldEntry,
1559
  };
1560

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

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

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

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

1591
  metaFetchEntryFree(&pOldEntry);
36✔
1592
  return code;
36✔
1593
}
1594

1595
static int32_t metaHandleChildTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam, bool superDropped) {
1,192✔
1596
  int32_t code = TSDB_CODE_SUCCESS;
1,192✔
1597

1598
  const SMetaEntry *pEntry = pParam->pEntry;
1,192✔
1599
  const SMetaEntry *pChild = pParam->pOldEntry;
1,192✔
1600
  const SMetaEntry *pSuper = pParam->pSuperEntry;
1,192✔
1601

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

1612
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
9,536✔
1613
    SMetaTableOp *op = &ops[i];
8,344✔
1614

1615
    if (op->table == META_ENTRY_TABLE && superDropped) {
8,344✔
1616
      continue;
6✔
1617
    }
1618

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

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

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

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

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

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

1660
  SMetaHandleParam param = {
1,192✔
1661
      .pEntry = pEntry,
1662
      .pOldEntry = pChild,
1663
      .pSuperEntry = pSuper,
1664
  };
1665

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

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

1686
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
1,192!
1687
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pChild->uid, pSuper->uid, NULL);
×
1688
    if (ret < 0) {
×
1689
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1690
    }
1691
  }
1692

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

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

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

1713
  SMetaTableOp ops[] = {
×
1714
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1715
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1716
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1717
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1718

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

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

1731
  return code;
×
1732
}
1733

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

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

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

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

1760
  // update other stuff
1761
  pMeta->pVnode->config.vndStats.numOfVTables--;
×
1762

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

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

1779
  metaFetchEntryFree(&pOldEntry);
×
1780
  return code;
×
1781
}
1782

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

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

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

1799
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
×
1800
    SMetaTableOp *op = &ops[i];
×
1801

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

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

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

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

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

1830
  return code;
×
1831
}
1832

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

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

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

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

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

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

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

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

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

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

1905
  for (;;) {
1906
    if (tdbTbcNext(cursor, &key, &keySize, NULL, NULL) < 0) {
38,408✔
1907
      break;
128✔
1908
    }
1909

1910
    if (((SCtbIdxKey *)key)->suid < suid) {
38,280!
1911
      continue;
×
1912
    } else if (((SCtbIdxKey *)key)->suid > suid) {
38,280✔
1913
      break;
4✔
1914
    }
1915

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

1925
  tdbTbcClose(cursor);
132✔
1926
  tdbFreeClear(key);
132✔
1927
  return code;
132✔
1928
}
1929

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

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

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

1943
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
100✔
1944
    SMetaTableOp *op = &ops[i];
80✔
1945

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

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

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

1963
  const SMetaEntry *pEntry = pParam->pEntry;
38✔
1964

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

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

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

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

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

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

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

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

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

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

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

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

2053
  const SMetaEntry *pEntry = pParam->pEntry;
4✔
2054
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
4✔
2055
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
4✔
2056

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

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

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

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

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

2092
  const SMetaEntry *pEntry = pParam->pEntry;
216✔
2093
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
216✔
2094

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

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

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

2115
  return code;
216✔
2116
}
2117

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

2121
  SMetaEntry *pOldEntry = NULL;
216✔
2122

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

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

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

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

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

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

2191
    tsdbCacheInvalidateSchema(pTsdb, pEntry->uid, -1, pEntry->stbEntry.schemaRow.version);
×
2192
  }
2193
  if (updStat) {
216✔
2194
    int64_t ctbNum = 0;
44✔
2195
    int32_t ret = metaGetStbStats(pMeta->pVnode, pEntry->uid, &ctbNum, NULL);
44✔
2196
    if (ret < 0) {
44!
2197
      metaError("vgId:%d, failed to get stb stats:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
×
2198
                pEntry->uid, tstrerror(ret));
2199
    }
2200
    pMeta->pVnode->config.vndStats.numOfTimeSeries += (ctbNum * deltaCol);
44✔
2201
    if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
44✔
2202
  }
2203
  metaFetchEntryFree(&pOldEntry);
216✔
2204
  return code;
216✔
2205
}
2206

2207
static int32_t metaHandleChildTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
4✔
2208
  int32_t code = TSDB_CODE_SUCCESS;
4✔
2209

2210
  SMetaEntry *pOldEntry = NULL;
4✔
2211
  SMetaEntry *pSuperEntry = NULL;
4✔
2212

2213
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
4✔
2214
  if (code) {
4!
2215
    metaErr(TD_VID(pMeta->pVnode), code);
×
2216
    return code;
×
2217
  }
2218

2219
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
4✔
2220
  if (code) {
4!
2221
    metaErr(TD_VID(pMeta->pVnode), code);
×
2222
    metaFetchEntryFree(&pOldEntry);
×
2223
    return code;
×
2224
  }
2225

2226
  SMetaHandleParam param = {
4✔
2227
      .pEntry = pEntry,
2228
      .pOldEntry = pOldEntry,
2229
      .pSuperEntry = pSuperEntry,
2230
  };
2231

2232
  metaWLock(pMeta);
4✔
2233
  code = metaHandleChildTableUpdateImpl(pMeta, &param);
4✔
2234
  metaULock(pMeta);
4✔
2235
  if (code) {
4!
2236
    metaErr(TD_VID(pMeta->pVnode), code);
×
2237
    metaFetchEntryFree(&pOldEntry);
×
2238
    metaFetchEntryFree(&pSuperEntry);
×
2239
    return code;
×
2240
  }
2241

2242
  metaFetchEntryFree(&pOldEntry);
4✔
2243
  metaFetchEntryFree(&pSuperEntry);
4✔
2244
  return code;
4✔
2245
}
2246

2247
static int32_t metaHandleNormalTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
38✔
2248
  int32_t     code = TSDB_CODE_SUCCESS;
38✔
2249
  SMetaEntry *pOldEntry = NULL;
38✔
2250

2251
  // fetch old entry
2252
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
38✔
2253
  if (code) {
38!
2254
    metaErr(TD_VID(pMeta->pVnode), code);
×
2255
    return code;
×
2256
  }
2257

2258
  // handle update
2259
  SMetaHandleParam param = {
38✔
2260
      .pEntry = pEntry,
2261
      .pOldEntry = pOldEntry,
2262
  };
2263
  metaWLock(pMeta);
38✔
2264
  code = metaHandleNormalTableUpdateImpl(pMeta, &param);
38✔
2265
  metaULock(pMeta);
38✔
2266
  if (code) {
38!
2267
    metaErr(TD_VID(pMeta->pVnode), code);
×
2268
    metaFetchEntryFree(&pOldEntry);
×
2269
    return code;
×
2270
  }
2271

2272
  // do other stuff
2273
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) &&
38!
2274
      pEntry->ntbEntry.schemaRow.version != pOldEntry->ntbEntry.schemaRow.version) {
×
2275
#if 0
2276
    {  // for add column
2277
      int16_t cid = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].colId;
2278
      int8_t  col_type = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].type;
2279
      int32_t ret = tsdbCacheNewNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, col_type);
2280
      if (ret < 0) {
2281
        terrno = ret;
2282
        goto _err;
2283
      }
2284
    }
2285
    {  // for drop column
2286

2287
      if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
2288
        int16_t cid = pColumn->colId;
2289

2290
        if (tsdbCacheDropNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, hasPrimayKey) != 0) {
2291
          metaError("vgId:%d, failed to drop ntable column:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name,
2292
                    entry.uid);
2293
        }
2294
        tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, entry.uid, pSchema->version);
2295
      }
2296
    }
2297
    }
2298
#endif
2299
    tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, pEntry->uid, pEntry->ntbEntry.schemaRow.version);
×
2300
  }
2301
  int32_t deltaCol = pEntry->ntbEntry.schemaRow.nCols - pOldEntry->ntbEntry.schemaRow.nCols;
38✔
2302
  pMeta->pVnode->config.vndStats.numOfNTimeSeries += deltaCol;  
38✔
2303
  if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
38✔
2304
  metaFetchEntryFree(&pOldEntry);
38✔
2305
  return code;
38✔
2306
}
2307

2308
static int32_t metaHandleVirtualNormalTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
×
2309
  int32_t     code = TSDB_CODE_SUCCESS;
×
2310
  SMetaEntry *pOldEntry = NULL;
×
2311

2312
  // fetch old entry
2313
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
×
2314
  if (code) {
×
2315
    metaErr(TD_VID(pMeta->pVnode), code);
×
2316
    return code;
×
2317
  }
2318

2319
  // handle update
2320
  SMetaHandleParam param = {
×
2321
      .pEntry = pEntry,
2322
      .pOldEntry = pOldEntry,
2323
  };
2324
  metaWLock(pMeta);
×
2325
  code = metaHandleVirtualNormalTableUpdateImpl(pMeta, &param);
×
2326
  metaULock(pMeta);
×
2327
  if (code) {
×
2328
    metaErr(TD_VID(pMeta->pVnode), code);
×
2329
    metaFetchEntryFree(&pOldEntry);
×
2330
    return code;
×
2331
  }
2332

2333
  metaTimeSeriesNotifyCheck(pMeta);
×
2334
  metaFetchEntryFree(&pOldEntry);
×
2335
  return code;
×
2336
}
2337

2338
static int32_t metaHandleVirtualChildTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
×
2339
  int32_t code = TSDB_CODE_SUCCESS;
×
2340

2341
  SMetaEntry *pOldEntry = NULL;
×
2342
  SMetaEntry *pSuperEntry = NULL;
×
2343

2344
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
×
2345
  if (code) {
×
2346
    metaErr(TD_VID(pMeta->pVnode), code);
×
2347
    return code;
×
2348
  }
2349

2350
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
×
2351
  if (code) {
×
2352
    metaErr(TD_VID(pMeta->pVnode), code);
×
2353
    metaFetchEntryFree(&pOldEntry);
×
2354
    return code;
×
2355
  }
2356

2357
  SMetaHandleParam param = {
×
2358
      .pEntry = pEntry,
2359
      .pOldEntry = pOldEntry,
2360
      .pSuperEntry = pSuperEntry,
2361
  };
2362

2363
  metaWLock(pMeta);
×
2364
  code = metaHandleVirtualChildTableUpdateImpl(pMeta, &param);
×
2365
  metaULock(pMeta);
×
2366
  if (code) {
×
2367
    metaErr(TD_VID(pMeta->pVnode), code);
×
2368
    metaFetchEntryFree(&pOldEntry);
×
2369
    metaFetchEntryFree(&pSuperEntry);
×
2370
    return code;
×
2371
  }
2372

2373
  metaFetchEntryFree(&pOldEntry);
×
2374
  metaFetchEntryFree(&pSuperEntry);
×
2375
  return code;
×
2376
}
2377

2378
static int32_t metaHandleSuperTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
20✔
2379
  int32_t     code = TSDB_CODE_SUCCESS;
20✔
2380
  SArray     *childList = NULL;
20✔
2381
  SMetaEntry *pOldEntry = NULL;
20✔
2382

2383
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
20✔
2384
  if (code) {
20!
2385
    metaErr(TD_VID(pMeta->pVnode), code);
×
2386
    return code;
×
2387
  }
2388

2389
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childList);
20✔
2390
  if (code) {
20!
2391
    metaErr(TD_VID(pMeta->pVnode), code);
×
2392
    metaFetchEntryFree(&pOldEntry);
×
2393
    return code;
×
2394
  }
2395

2396
  if (tsdbCacheDropSubTables(pMeta->pVnode->pTsdb, childList, pEntry->uid) < 0) {
20!
2397
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
×
2398
              pEntry->uid, tstrerror(terrno));
2399
  }
2400

2401
  // loop to drop all child tables
2402
  for (int32_t i = 0; i < taosArrayGetSize(childList); i++) {
26✔
2403
    SMetaEntry childEntry = {
12✔
2404
        .version = pEntry->version,
6✔
2405
        .uid = *(tb_uid_t *)taosArrayGet(childList, i),
6✔
2406
        .type = -TSDB_CHILD_TABLE,
2407
    };
2408

2409
    code = metaHandleChildTableDrop(pMeta, &childEntry, true);
6✔
2410
    if (code) {
6!
2411
      metaErr(TD_VID(pMeta->pVnode), code);
×
2412
    }
2413
  }
2414

2415
  // do drop super table
2416
  SMetaHandleParam param = {
20✔
2417
      .pEntry = pEntry,
2418
      .pOldEntry = pOldEntry,
2419
  };
2420
  metaWLock(pMeta);
20✔
2421
  code = metaHandleSuperTableDropImpl(pMeta, &param);
20✔
2422
  metaULock(pMeta);
20✔
2423
  if (code) {
20!
2424
    metaErr(TD_VID(pMeta->pVnode), code);
×
2425
    taosArrayDestroy(childList);
×
2426
    metaFetchEntryFree(&pOldEntry);
×
2427
    return code;
×
2428
  }
2429

2430
  // do other stuff
2431
  metaUpdTimeSeriesNum(pMeta);
20✔
2432

2433
  // free resource and return
2434
  taosArrayDestroy(childList);
20✔
2435
  metaFetchEntryFree(&pOldEntry);
20✔
2436
  return code;
20✔
2437
}
2438

2439
int32_t metaHandleEntry2(SMeta *pMeta, const SMetaEntry *pEntry) {
9,016✔
2440
  int32_t   code = TSDB_CODE_SUCCESS;
9,016✔
2441
  int32_t   vgId = TD_VID(pMeta->pVnode);
9,016✔
2442
  SMetaInfo info = {0};
9,016✔
2443
  int8_t    type = pEntry->type > 0 ? pEntry->type : -pEntry->type;
9,016✔
2444

2445
  if (NULL == pMeta || NULL == pEntry) {
9,016!
2446
    metaError("%s failed at %s:%d since invalid parameter", __func__, __FILE__, __LINE__);
2!
2447
    return TSDB_CODE_INVALID_PARA;
×
2448
  }
2449

2450
  if (pEntry->type > 0) {
9,014✔
2451
    bool isExist = false;
7,774✔
2452
    if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) {
7,774✔
2453
      isExist = true;
258✔
2454
    }
2455

2456
    switch (type) {
7,776!
2457
      case TSDB_SUPER_TABLE: {
856✔
2458
        if (isExist) {
856✔
2459
          code = metaHandleSuperTableUpdate(pMeta, pEntry);
216✔
2460
        } else {
2461
          code = metaHandleSuperTableCreate(pMeta, pEntry);
640✔
2462
        }
2463
        break;
856✔
2464
      }
2465
      case TSDB_CHILD_TABLE: {
6,788✔
2466
        if (isExist) {
6,788✔
2467
          code = metaHandleChildTableUpdate(pMeta, pEntry);
4✔
2468
        } else {
2469
          code = metaHandleChildTableCreate(pMeta, pEntry);
6,784✔
2470
        }
2471
        break;
6,780✔
2472
      }
2473
      case TSDB_NORMAL_TABLE: {
132✔
2474
        if (isExist) {
132✔
2475
          code = metaHandleNormalTableUpdate(pMeta, pEntry);
38✔
2476
        } else {
2477
          code = metaHandleNormalTableCreate(pMeta, pEntry);
94✔
2478
        }
2479
        break;
132✔
2480
      }
2481
      case TSDB_VIRTUAL_NORMAL_TABLE: {
×
2482
        if (isExist) {
×
2483
          code = metaHandleVirtualNormalTableUpdate(pMeta, pEntry);
×
2484
        } else {
2485
          code = metaHandleVirtualNormalTableCreate(pMeta, pEntry);
×
2486
        }
2487
        break;
×
2488
      }
2489
      case TSDB_VIRTUAL_CHILD_TABLE: {
×
2490
        if (isExist) {
×
2491
          code = metaHandleVirtualChildTableUpdate(pMeta, pEntry);
×
2492
        } else {
2493
          code = metaHandleVirtualChildTableCreate(pMeta, pEntry);
×
2494
        }
2495
        break;
×
2496
      }
2497
      default: {
×
2498
        code = TSDB_CODE_INVALID_PARA;
×
2499
        break;
×
2500
      }
2501
    }
2502
  } else {
2503
    switch (type) {
1,240!
2504
      case TSDB_SUPER_TABLE: {
20✔
2505
        code = metaHandleSuperTableDrop(pMeta, pEntry);
20✔
2506
        break;
20✔
2507
      }
2508
      case TSDB_CHILD_TABLE: {
1,186✔
2509
        code = metaHandleChildTableDrop(pMeta, pEntry, false);
1,186✔
2510
        break;
1,186✔
2511
      }
2512
      case TSDB_NORMAL_TABLE: {
36✔
2513
        code = metaHandleNormalTableDrop(pMeta, pEntry);
36✔
2514
        break;
36✔
2515
      }
2516
      case TSDB_VIRTUAL_NORMAL_TABLE: {
×
2517
        code = metaHandleVirtualNormalTableDrop(pMeta, pEntry);
×
2518
        break;
×
2519
      }
2520
      case TSDB_VIRTUAL_CHILD_TABLE: {
×
2521
        code = metaHandleVirtualChildTableDrop(pMeta, pEntry, false);
×
2522
        break;
×
2523
      }
2524
      default: {
2525
        code = TSDB_CODE_INVALID_PARA;
2526
        break;
2527
      }
2528
    }
2529
  }
2530

2531
  if (TSDB_CODE_SUCCESS == code) {
9,008!
2532
    pMeta->changed = true;
9,010✔
2533
    metaDebug("vgId:%d, index:%" PRId64 ", handle meta entry success, type:%d tb:%s uid:%" PRId64, vgId, pEntry->version,
9,010!
2534
              pEntry->type, pEntry->type > 0 ? pEntry->name : "", pEntry->uid);
2535
  } else {
2536
    metaErr(vgId, code);
×
2537
  }
2538
  TAOS_RETURN(code);
9,008✔
2539
}
2540

2541
void metaHandleSyncEntry(SMeta *pMeta, const SMetaEntry *pEntry) {
×
2542
  int32_t code = TSDB_CODE_SUCCESS;
×
2543
  code = metaHandleEntry2(pMeta, pEntry);
×
2544
  if (code) {
×
2545
    metaErr(TD_VID(pMeta->pVnode), code);
×
2546
  }
2547
  return;
×
2548
}
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