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

taosdata / TDengine / #4506

15 Jul 2025 12:33AM UTC coverage: 62.026% (-0.7%) from 62.706%
#4506

push

travis-ci

web-flow
docs: update stream docs (#31874)

155391 of 320094 branches covered (48.55%)

Branch coverage included in aggregate %.

240721 of 318525 relevant lines covered (75.57%)

6529048.03 hits per line

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

58.27
/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) {
404,573✔
67
  int32_t code = TSDB_CODE_SUCCESS;
404,573✔
68
  void   *value = NULL;
404,573✔
69
  int32_t valueSize = 0;
404,573✔
70

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

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

96
  tDecoderInit(&decoder, value, valueSize);
404,749✔
97
  code = metaDecodeEntry(&decoder, &entry);
404,700✔
98
  if (code) {
404,517!
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);
404,517✔
107
  if (code) {
404,653!
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);
404,653✔
116
  tDecoderClear(&decoder);
404,664✔
117
  return code;
404,679✔
118
}
119

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

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

133
  code = metaFetchEntryByUid(pMeta, uid, ppEntry);
180,597✔
134
  if (TSDB_CODE_SUCCESS != code) {
180,527!
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;
180,527✔
139
}
140

141
void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); }
404,719✔
142

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

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

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

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

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

180
  // put to tdb
181
  if (META_TABLE_OP_INSERT == op) {
240,315✔
182
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
200,294✔
183
  } else if (META_TABLE_OP_UPDATA == op) {
40,021✔
184
    code = tdbTbUpsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
22,276✔
185
  } else if (META_TABLE_OP_DELETE == op) {
17,745!
186
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
17,746✔
187
  } else {
188
    code = TSDB_CODE_INVALID_PARA;
×
189
  }
190
  if (TSDB_CODE_SUCCESS != code) {
240,494!
191
    metaErr(vgId, code);
×
192
  }
193
  taosMemoryFree(value);
240,332!
194
  return code;
240,471✔
195
}
196

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

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

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

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

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

231
  // encode schema
232
  tEncodeSize(tEncodeSSchemaWrapper, pSchema, valueSize, code);
113,527✔
233
  if (TSDB_CODE_SUCCESS != code) {
56,608!
234
    metaErr(vgId, code);
×
235
    return code;
×
236
  }
237

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

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

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

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

281
  if (pOldColumn && pNewColumn) {
102,184✔
282
    if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
99,646✔
283
      return TSDB_CODE_SUCCESS;
4,109✔
284
    } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) {
95,537!
285
      action = DROP_INDEX;
55✔
286
    } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
95,482!
287
      action = ADD_INDEX;
26✔
288
    } else {
289
      return TSDB_CODE_SUCCESS;
95,456✔
290
    }
291
  } else if (pOldColumn) {
2,538✔
292
    if (IS_IDX_ON(pOldColumn)) {
995✔
293
      action = DROP_INDEX;
45✔
294
    } else {
295
      return TSDB_CODE_SUCCESS;
950✔
296
    }
297
  } else {
298
    if (IS_IDX_ON(pNewColumn)) {
1,543!
299
      action = ADD_INDEX;
×
300
    } else {
301
      return TSDB_CODE_SUCCESS;
1,543✔
302
    }
303
  }
304

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

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

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

326
    STagIdxKey *pTagIdxKey = NULL;
656✔
327
    int32_t     tagIdxKeySize = 0;
656✔
328

329
    if (action == ADD_INDEX) {
656✔
330
      code = metaFetchTagIdxKey(pMeta, pChildEntry, pNewColumn, &pTagIdxKey, &tagIdxKeySize);
319✔
331
      if (code) {
319!
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);
319✔
339
      if (code) {
319!
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);
337✔
348
      if (code) {
337!
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);
337✔
356
      if (code) {
337!
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);
656✔
366
    metaFetchEntryFree(&pChildEntry);
656✔
367
  }
368

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

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

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

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

389
  // fetch all child tables
390
  SArray *childTables = 0;
12✔
391
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childTables);
12✔
392
  if (code) {
12!
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++) {
18✔
399
    int64_t uid = *(int64_t *)taosArrayGet(childTables, i);
6✔
400

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

410
    SMetaHandleParam param = {.pEntry = pChildEntry};
6✔
411

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

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

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

448
  taosArrayDestroy(childTables);
12✔
449
  return code;
12✔
450
}
451

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

459
  int32_t iOld = 0, iNew = 0;
4,321✔
460
  for (; iOld < pOldTagSchema->nCols && iNew < pNewTagSchema->nCols;) {
104,719✔
461
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
100,395✔
462
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
100,395✔
463

464
    if (pOldColumn->colId == pNewColumn->colId) {
100,395✔
465
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
99,633✔
466
      if (code) {
99,643✔
467
        metaErr(TD_VID(pMeta->pVnode), code);
7!
468
        return code;
×
469
      }
470

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

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

488
      iNew++;
×
489
    }
490
  }
491

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

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

510
  return code;
4,329✔
511
}
512

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

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

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

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

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

549
      iNew++;
×
550
    }
551
  }
552

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

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

571
  return code;
12✔
572
}
573

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

577
  const SMetaEntry *pEntry = pParam->pEntry;
60,168✔
578
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
60,168✔
579

580
  if (NULL == pOldEntry) {
60,168✔
581
    return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
42,665✔
582
  }
583

584
  if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
17,503✔
585
    // check row schema
586
    if (pOldEntry->ntbEntry.schemaRow.version != pEntry->ntbEntry.schemaRow.version) {
4,752!
587
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
5,702✔
588
    }
589
  } else if (pEntry->type == TSDB_SUPER_TABLE) {
12,751!
590
    // check row schema
591
    if (pOldEntry->stbEntry.schemaRow.version != pEntry->stbEntry.schemaRow.version) {
12,769✔
592
      if (TABLE_IS_VIRTUAL(pEntry->flags)) {
8,434✔
593
        return metaUpdateSuperTableRowSchema(pMeta, pParam);
12✔
594
      } else {
595
        return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
8,422✔
596
      }
597
    }
598

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

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

610
  return TSDB_CODE_SUCCESS;
4,387✔
611
}
612

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

618
// Uid Index
619
static void metaBuildEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) {
222,503✔
620
  pInfo->uid = pEntry->uid;
222,503✔
621
  pInfo->version = pEntry->version;
222,503✔
622
  if (pEntry->type == TSDB_SUPER_TABLE) {
222,503✔
623
    pInfo->suid = pEntry->uid;
39,216✔
624
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
39,216✔
625
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
183,287✔
626
    pInfo->suid = pEntry->ctbEntry.suid;
161,355✔
627
    pInfo->skmVer = 0;
161,355✔
628
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
21,932✔
629
    pInfo->suid = 0;
21,931✔
630
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
21,931✔
631
  }
632
}
222,503✔
633

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

638
  const SMetaEntry *pEntry = pParam->pEntry;
222,492✔
639

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

648
  // put to tdb
649
  SUidIdxVal value = {
222,384✔
650
      .suid = info.suid,
222,384✔
651
      .skmVer = info.skmVer,
222,384✔
652
      .version = pEntry->version,
222,384✔
653
  };
654
  if (META_TABLE_OP_INSERT == op) {
222,384✔
655
    code = tdbTbInsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
200,151✔
656
  } else if (META_TABLE_OP_UPDATA == op) {
22,233!
657
    code = tdbTbUpsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
22,259✔
658
  }
659
  return code;
222,824✔
660
}
661

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

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

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

673
  const SMetaEntry *pEntry = pParam->pOldEntry;
20,068✔
674

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

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

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

690
  const SMetaEntry *pEntry = pParam->pEntry;
200,310✔
691

692
  if (META_TABLE_OP_INSERT == op) {
200,310!
693
    code = tdbTbInsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
200,357✔
694
                       pMeta->txn);
695
  } else if (META_TABLE_OP_UPDATA == op) {
×
696
    code = tdbTbUpsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
×
697
                       pMeta->txn);
698
  } else {
699
    code = TSDB_CODE_INVALID_PARA;
×
700
  }
701
  return code;
200,550✔
702
}
703

704
static int32_t metaNameIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
200,345✔
705
  int32_t code = TSDB_CODE_SUCCESS;
200,345✔
706
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
200,345✔
707
}
708

709
static int32_t metaNameIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
710
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
×
711
}
712

713
static int32_t metaNameIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
20,071✔
714
  int32_t code = TSDB_CODE_SUCCESS;
20,071✔
715

716
  const SMetaEntry *pEntry = pParam->pOldEntry;
20,071✔
717
  code = tdbTbDelete(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, pMeta->txn);
20,071✔
718
  if (code) {
20,075!
719
    metaErr(TD_VID(pMeta->pVnode), code);
×
720
  }
721
  return code;
20,074✔
722
}
723

724
// Suid Index
725
static int32_t metaSUidIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
26,354✔
726
  const SMetaEntry *pEntry = pParam->pEntry;
26,354✔
727

728
  int32_t code = tdbTbInsert(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), NULL, 0, pMeta->txn);
26,354✔
729
  if (code) {
26,509!
730
    metaErr(TD_VID(pMeta->pVnode), code);
×
731
  }
732
  return code;
26,418✔
733
}
734

735
static int32_t metaSUidIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
1,933✔
736
  const SMetaEntry *pEntry = pParam->pOldEntry;
1,933✔
737

738
  int32_t code = tdbTbDelete(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), pMeta->txn);
1,933✔
739
  if (code) {
1,936!
740
    metaErr(TD_VID(pMeta->pVnode), code);
×
741
  }
742
  return code;
1,935✔
743
}
744

745
// Child Index
746
static int32_t metaChildIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
160,897✔
747
  int32_t code = TSDB_CODE_SUCCESS;
160,897✔
748

749
  const SMetaEntry *pEntry = pParam->pEntry;
160,897✔
750

751
  SCtbIdxKey key = {
160,897✔
752
      .suid = pEntry->ctbEntry.suid,
160,897✔
753
      .uid = pEntry->uid,
160,897✔
754
  };
755

756
  if (META_TABLE_OP_INSERT == op) {
160,897✔
757
    code = tdbTbInsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
157,847✔
758
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
157,847✔
759
  } else if (META_TABLE_OP_UPDATA == op) {
3,050!
760
    code = tdbTbUpsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
3,059✔
761
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
3,059✔
762
  } else {
763
    code = TSDB_CODE_INVALID_PARA;
×
764
  }
765
  return code;
160,926✔
766
}
767

768
static int32_t metaChildIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
157,835✔
769
  return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
157,835✔
770
}
771

772
static int32_t metaChildIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
3,733✔
773
  const SMetaEntry *pEntry = pParam->pEntry;
3,733✔
774
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
3,733✔
775
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
3,733✔
776

777
  const STag *pNewTags = (const STag *)pEntry->ctbEntry.pTags;
3,733✔
778
  const STag *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags;
3,733✔
779
  if (pNewTags->len != pOldTags->len || memcmp(pNewTags, pOldTags, pNewTags->len)) {
3,733✔
780
    return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
3,059✔
781
  }
782
  return 0;
674✔
783
}
784

785
static int32_t metaChildIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
15,830✔
786
  const SMetaEntry *pEntry = pParam->pOldEntry;
15,830✔
787

788
  SCtbIdxKey key = {
15,830✔
789
      .suid = pEntry->ctbEntry.suid,
15,830✔
790
      .uid = pEntry->uid,
15,830✔
791
  };
792
  return tdbTbDelete(pMeta->pCtbIdx, &key, sizeof(key), pMeta->txn);
15,830✔
793
}
794

795
// Tag Index
796
static int32_t metaFetchTagIdxKey(SMeta *pMeta, const SMetaEntry *pEntry, const SSchema *pTagColumn,
179,866✔
797
                                  STagIdxKey **ppTagIdxKey, int32_t *pTagIdxKeySize) {
798
  int32_t code = TSDB_CODE_SUCCESS;
179,866✔
799

800
  STagIdxKey *pTagIdxKey = NULL;
179,866✔
801
  int32_t     nTagIdxKey;
802
  const void *pTagData = NULL;
179,866✔
803
  int32_t     nTagData = 0;
179,866✔
804

805
  STagVal tagVal = {
179,866✔
806
      .cid = pTagColumn->colId,
179,866✔
807
  };
808

809
  if (tTagGet((const STag *)pEntry->ctbEntry.pTags, &tagVal)) {
179,866✔
810
    if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
177,701!
811
      pTagData = tagVal.pData;
32,184✔
812
      nTagData = (int32_t)tagVal.nData;
32,184✔
813
    } else {
814
      pTagData = &(tagVal.i64);
145,517✔
815
      nTagData = tDataTypes[pTagColumn->type].bytes;
145,517✔
816
    }
817
  } else {
818
    if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
2,185!
819
      nTagData = tDataTypes[pTagColumn->type].bytes;
1,700✔
820
    }
821
  }
822

823
  code = metaCreateTagIdxKey(pEntry->ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type,
179,886✔
824
                             pEntry->uid, &pTagIdxKey, &nTagIdxKey);
179,886✔
825
  if (code) {
179,887✔
826
    metaErr(TD_VID(pMeta->pVnode), code);
11!
827
    return code;
×
828
  }
829

830
  *ppTagIdxKey = pTagIdxKey;
179,876✔
831
  *pTagIdxKeySize = nTagIdxKey;
179,876✔
832
  return code;
179,876✔
833
}
834

835
static void metaFetchTagIdxKeyFree(STagIdxKey **ppTagIdxKey) {
179,911✔
836
  metaDestroyTagIdxKey(*ppTagIdxKey);
179,911✔
837
  *ppTagIdxKey = NULL;
179,894✔
838
}
179,894✔
839

840
static int32_t metaTagIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
157,846✔
841
  int32_t code = TSDB_CODE_SUCCESS;
157,846✔
842

843
  const SMetaEntry *pEntry = pParam->pEntry;
157,846✔
844
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
157,846✔
845

846
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
157,846✔
847
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
158,532✔
848
    const SSchema *pTagColumn = &pTagSchema->pSchema[0];
686✔
849

850
    STagVal tagVal = {
686✔
851
        .cid = pTagColumn->colId,
686✔
852
    };
853

854
    const void *pTagData = pEntry->ctbEntry.pTags;
686✔
855
    int32_t     nTagData = ((const STag *)pEntry->ctbEntry.pTags)->len;
686✔
856
    code = metaSaveJsonVarToIdx(pMeta, pEntry, pTagColumn);
686✔
857
    if (code) {
686!
858
      metaErr(TD_VID(pMeta->pVnode), code);
×
859
    }
860
  } else {
861
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
628,280✔
862
      STagIdxKey    *pTagIdxKey = NULL;
471,103✔
863
      int32_t        nTagIdxKey;
864
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
471,103✔
865

866
      if (!IS_IDX_ON(pTagColumn)) {
471,103✔
867
        continue;
313,796✔
868
      }
869

870
      code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pTagIdxKey, &nTagIdxKey);
157,307✔
871
      if (code) {
157,303!
872
        metaErr(TD_VID(pMeta->pVnode), code);
×
873
        return code;
×
874
      }
875

876
      code = tdbTbInsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn);
157,303✔
877
      if (code) {
157,340!
878
        metaErr(TD_VID(pMeta->pVnode), code);
×
879
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
880
        return code;
×
881
      }
882
      metaFetchTagIdxKeyFree(&pTagIdxKey);
157,340✔
883
    }
884
  }
885
  return code;
157,863✔
886
}
887

888
static int32_t metaTagIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
3,733✔
889
  int32_t code = TSDB_CODE_SUCCESS;
3,733✔
890

891
  const SMetaEntry     *pEntry = pParam->pEntry;
3,733✔
892
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
3,733✔
893
  const SMetaEntry     *pSuperEntry = pParam->pSuperEntry;
3,733✔
894
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
3,733✔
895
  const STag           *pNewTags = (const STag *)pEntry->ctbEntry.pTags;
3,733✔
896
  const STag           *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags;
3,733✔
897

898
  if (pNewTags->len == pOldTags->len && !memcmp(pNewTags, pOldTags, pNewTags->len)) {
3,733✔
899
    return code;
674✔
900
  }
901

902
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
3,059✔
903
    code = metaDelJsonVarFromIdx(pMeta, pOldEntry, &pTagSchema->pSchema[0]);
8✔
904
    if (code) {
8!
905
      metaErr(TD_VID(pMeta->pVnode), code);
×
906
      return code;
×
907
    }
908

909
    code = metaSaveJsonVarToIdx(pMeta, pEntry, &pTagSchema->pSchema[0]);
8✔
910
    if (code) {
8!
911
      metaErr(TD_VID(pMeta->pVnode), code);
×
912
      return code;
×
913
    }
914
  } else {
915
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
11,133✔
916
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
8,082✔
917

918
      if (!IS_IDX_ON(pTagColumn)) {
8,082✔
919
        continue;
5,034✔
920
      }
921

922
      STagIdxKey *pOldTagIdxKey = NULL;
3,048✔
923
      int32_t     oldTagIdxKeySize = 0;
3,048✔
924
      STagIdxKey *pNewTagIdxKey = NULL;
3,048✔
925
      int32_t     newTagIdxKeySize = 0;
3,048✔
926

927
      code = metaFetchTagIdxKey(pMeta, pOldEntry, pTagColumn, &pOldTagIdxKey, &oldTagIdxKeySize);
3,048✔
928
      if (code) {
3,048!
929
        metaErr(TD_VID(pMeta->pVnode), code);
×
930
        return code;
×
931
      }
932

933
      code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pNewTagIdxKey, &newTagIdxKeySize);
3,048✔
934
      if (code) {
3,048!
935
        metaErr(TD_VID(pMeta->pVnode), code);
×
936
        metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
937
        return code;
×
938
      }
939

940
      if (tagIdxKeyCmpr(pOldTagIdxKey, oldTagIdxKeySize, pNewTagIdxKey, newTagIdxKeySize)) {
3,048✔
941
        code = tdbTbDelete(pMeta->pTagIdx, pOldTagIdxKey, oldTagIdxKeySize, pMeta->txn);
1,256✔
942
        if (code) {
1,256!
943
          metaErr(TD_VID(pMeta->pVnode), code);
×
944
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
945
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
946
          return code;
×
947
        }
948

949
        code = tdbTbInsert(pMeta->pTagIdx, pNewTagIdxKey, newTagIdxKeySize, NULL, 0, pMeta->txn);
1,256✔
950
        if (code) {
1,256!
951
          metaErr(TD_VID(pMeta->pVnode), code);
×
952
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
953
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
954
          return code;
×
955
        }
956
      }
957

958
      metaFetchTagIdxKeyFree(&pOldTagIdxKey);
3,048✔
959
      metaFetchTagIdxKeyFree(&pNewTagIdxKey);
3,048✔
960
    }
961
  }
962
  return code;
3,059✔
963
}
964

965
static int32_t metaTagIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
15,830✔
966
  int32_t code = TSDB_CODE_SUCCESS;
15,830✔
967

968
  const SMetaEntry     *pEntry = pParam->pEntry;
15,830✔
969
  const SMetaEntry     *pChild = pParam->pOldEntry;
15,830✔
970
  const SMetaEntry     *pSuper = pParam->pSuperEntry;
15,830✔
971
  const SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
15,830✔
972
  const SSchema        *pTagColumn = NULL;
15,830✔
973
  const STag           *pTags = (const STag *)pChild->ctbEntry.pTags;
15,830✔
974

975
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
15,830✔
976
    pTagColumn = &pTagSchema->pSchema[0];
197✔
977
    code = metaDelJsonVarFromIdx(pMeta, pChild, pTagColumn);
197✔
978
    if (code) {
197!
979
      metaErr(TD_VID(pMeta->pVnode), code);
×
980
    }
981
  } else {
982
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
54,382✔
983
      pTagColumn = &pTagSchema->pSchema[i];
38,749✔
984
      if (!IS_IDX_ON(pTagColumn)) {
38,749✔
985
        continue;
22,929✔
986
      }
987

988
      STagIdxKey *pTagIdxKey = NULL;
15,820✔
989
      int32_t     nTagIdxKey;
990

991
      code = metaFetchTagIdxKey(pMeta, pChild, pTagColumn, &pTagIdxKey, &nTagIdxKey);
15,820✔
992
      if (code) {
15,820!
993
        metaErr(TD_VID(pMeta->pVnode), code);
×
994
        return code;
×
995
      }
996

997
      code = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
15,820✔
998
      if (code) {
15,820!
999
        metaErr(TD_VID(pMeta->pVnode), code);
×
1000
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
1001
        return code;
×
1002
      }
1003
      metaFetchTagIdxKeyFree(&pTagIdxKey);
15,820✔
1004
    }
1005
  }
1006
  return code;
15,830✔
1007
}
1008

1009
// Btime Index
1010
static int32_t metaBtimeIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
192,126✔
1011
  int32_t code = TSDB_CODE_SUCCESS;
192,126✔
1012

1013
  const SMetaEntry *pEntry;
1014
  if (META_TABLE_OP_DELETE == op) {
192,126✔
1015
    pEntry = pParam->pOldEntry;
18,139✔
1016
  } else {
1017
    pEntry = pParam->pEntry;
173,987✔
1018
  }
1019

1020
  SBtimeIdxKey key = {
192,126✔
1021
      .uid = pEntry->uid,
192,126✔
1022
  };
1023

1024
  if (TSDB_CHILD_TABLE == pEntry->type || TSDB_VIRTUAL_CHILD_TABLE == pEntry->type) {
192,126✔
1025
    key.btime = pEntry->ctbEntry.btime;
173,656✔
1026
  } else if (TSDB_NORMAL_TABLE == pEntry->type || TSDB_VIRTUAL_NORMAL_TABLE == pEntry->type) {
18,470!
1027
    key.btime = pEntry->ntbEntry.btime;
18,470✔
1028
  } else {
1029
    return TSDB_CODE_INVALID_PARA;
×
1030
  }
1031

1032
  if (META_TABLE_OP_INSERT == op) {
192,126✔
1033
    code = tdbTbInsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
173,996✔
1034
  } else if (META_TABLE_OP_UPDATA == op) {
18,130!
1035
    code = tdbTbUpsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
×
1036
  } else if (META_TABLE_OP_DELETE == op) {
18,130!
1037
    code = tdbTbDelete(pMeta->pBtimeIdx, &key, sizeof(key), pMeta->txn);
18,139✔
1038
  } else {
1039
    code = TSDB_CODE_INVALID_PARA;
×
1040
  }
1041
  if (code) {
192,154!
1042
    metaErr(TD_VID(pMeta->pVnode), code);
×
1043
  }
1044
  return code;
192,156✔
1045
}
1046

1047
static int32_t metaBtimeIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
173,987✔
1048
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
173,987✔
1049
}
1050

1051
static int32_t metaBtimeIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
1052
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
×
1053
}
1054

1055
static int32_t metaBtimeIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
18,139✔
1056
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_DELETE);
18,139✔
1057
}
1058

1059
// TTL Index
1060
static int32_t metaTtlIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
173,863✔
1061
  const SMetaEntry *pEntry = pParam->pEntry;
173,863✔
1062

1063
  STtlUpdTtlCtx ctx = {
173,863✔
1064
      .uid = pEntry->uid,
173,863✔
1065
      .pTxn = pMeta->txn,
173,863✔
1066
  };
1067
  if (TSDB_CHILD_TABLE == pEntry->type) {
173,863✔
1068
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
157,807✔
1069
    ctx.changeTimeMs = pEntry->ctbEntry.btime;
157,807✔
1070
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
16,056!
1071
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
16,064✔
1072
    ctx.changeTimeMs = pEntry->ntbEntry.btime;
16,064✔
1073
  } else {
1074
    return TSDB_CODE_INVALID_PARA;
×
1075
  }
1076

1077
  int32_t ret = ttlMgrInsertTtl(pMeta->pTtlMgr, &ctx);
173,871✔
1078
  if (ret < 0) {
173,851!
1079
    metaError("vgId:%d, failed to insert ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid, tstrerror(ret));
×
1080
  }
1081
  return TSDB_CODE_SUCCESS;
173,849✔
1082
}
1083

1084
static int32_t metaTtlIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
173,859✔
1085
  return metaTtlIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
173,859✔
1086
}
1087

1088
static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam);
1089

1090
static int32_t metaTtlIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
9,202✔
1091
  int32_t code = TSDB_CODE_SUCCESS;
9,202✔
1092

1093
  const SMetaEntry *pEntry = pParam->pEntry;
9,202✔
1094
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
9,202✔
1095

1096
  if ((pEntry->type == TSDB_CHILD_TABLE && pOldEntry->ctbEntry.ttlDays != pEntry->ctbEntry.ttlDays) ||
9,202✔
1097
      (pEntry->type == TSDB_NORMAL_TABLE && pOldEntry->ntbEntry.ttlDays != pEntry->ntbEntry.ttlDays)) {
9,182✔
1098
    code = metaTtlIdxDelete(pMeta, pParam);
41✔
1099
    if (code) {
41!
1100
      metaErr(TD_VID(pMeta->pVnode), code);
×
1101
    }
1102

1103
    code = metaTtlIdxInsert(pMeta, pParam);
41✔
1104
    if (code) {
41!
1105
      metaErr(TD_VID(pMeta->pVnode), code);
×
1106
    }
1107
  }
1108

1109
  return TSDB_CODE_SUCCESS;
9,202✔
1110
}
1111

1112
static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
18,126✔
1113
  int32_t code = TSDB_CODE_SUCCESS;
18,126✔
1114

1115
  const SMetaEntry *pEntry = pParam->pOldEntry;
18,126✔
1116
  STtlDelTtlCtx     ctx = {
18,126✔
1117
          .uid = pEntry->uid,
18,126✔
1118
          .pTxn = pMeta->txn,
18,126✔
1119
  };
1120

1121
  if (TSDB_CHILD_TABLE == pEntry->type) {
18,126✔
1122
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
15,839✔
1123
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
2,287✔
1124
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
2,283✔
1125
  } else {
1126
    code = TSDB_CODE_INVALID_PARA;
4✔
1127
  }
1128

1129
  if (TSDB_CODE_SUCCESS == code) {
18,126✔
1130
    int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
18,122✔
1131
    if (ret < 0) {
18,122!
1132
      metaError("vgId:%d, failed to delete ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid,
×
1133
                tstrerror(ret));
1134
    }
1135
  }
1136
  return code;
18,126✔
1137
}
1138

1139
static void metaTimeSeriesNotifyCheck(SMeta *pMeta) {
183,885✔
1140
#if defined(TD_ENTERPRISE)
1141
  int64_t nTimeSeries = metaGetTimeSeriesNum(pMeta, 0);
183,885✔
1142
  int64_t deltaTS = nTimeSeries - pMeta->pVnode->config.vndStats.numOfReportedTimeSeries;
183,926✔
1143
  if (deltaTS > tsTimeSeriesThreshold) {
183,926✔
1144
    if (0 == atomic_val_compare_exchange_8(&dmNotifyHdl.state, 1, 2)) {
103,150✔
1145
      if (tsem_post(&dmNotifyHdl.sem) != 0) {
103,159!
1146
        metaError("vgId:%d, failed to post semaphore, errno:%d", TD_VID(pMeta->pVnode), ERRNO);
×
1147
      }
1148
    }
1149
  }
1150
#endif
1151
}
183,956✔
1152

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

1211
static int32_t metaHandleSuperTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
26,432✔
1212
  int32_t code = TSDB_CODE_SUCCESS;
26,432✔
1213

1214
  SMetaTableOp ops[] = {
26,432✔
1215
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1216
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: here should be insert
1217
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1218
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1219
      {META_SUID_IDX, META_TABLE_OP_INSERT},      //
1220
  };
1221

1222
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
158,501✔
1223
    SMetaTableOp          *op = &ops[i];
132,102✔
1224
    const SMetaHandleParam param = {
132,102✔
1225
        .pEntry = pEntry,
1226
    };
1227
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
132,102✔
1228
    if (TSDB_CODE_SUCCESS != code) {
131,984!
1229
      metaErr(TD_VID(pMeta->pVnode), code);
×
1230
      return code;
×
1231
    }
1232
  }
1233

1234
  return code;
26,399✔
1235
}
1236
static int32_t metaHandleSuperTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
25,835✔
1237
  int32_t code = TSDB_CODE_SUCCESS;
25,835✔
1238

1239
  metaWLock(pMeta);
25,835✔
1240
  code = metaHandleSuperTableCreateImpl(pMeta, pEntry);
26,541✔
1241
  metaULock(pMeta);
26,412✔
1242

1243
  if (TSDB_CODE_SUCCESS == code) {
26,542!
1244
    pMeta->pVnode->config.vndStats.numOfSTables++;
26,542✔
1245

1246
    metaInfo("vgId:%d, %s success, version:%" PRId64 " type:%d uid:%" PRId64 " name:%s", TD_VID(pMeta->pVnode),
26,542✔
1247
             __func__, pEntry->version, pEntry->type, pEntry->uid, pEntry->name);
1248
  } else {
1249
    metaErr(TD_VID(pMeta->pVnode), code);
×
1250
  }
1251
  return code;
26,562✔
1252
}
1253

1254
static int32_t metaHandleNormalTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
16,043✔
1255
  int32_t code = TSDB_CODE_SUCCESS;
16,043✔
1256

1257
  SMetaTableOp ops[] = {
16,043✔
1258
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1259
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: need to be insert
1260
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1261
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1262
      {META_BTIME_IDX, META_TABLE_OP_INSERT},     //
1263
      {META_TTL_IDX, META_TABLE_OP_INSERT},       //
1264
  };
1265

1266
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
112,290✔
1267
    SMetaTableOp *op = &ops[i];
96,247✔
1268

1269
    SMetaHandleParam param = {
96,247✔
1270
        .pEntry = pEntry,
1271
    };
1272

1273
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
96,247✔
1274
    if (TSDB_CODE_SUCCESS != code) {
96,245!
1275
      metaErr(TD_VID(pMeta->pVnode), code);
×
1276
      return code;
×
1277
    }
1278
  }
1279

1280
  return code;
16,043✔
1281
}
1282
static int32_t metaHandleNormalTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
16,044✔
1283
  int32_t code = TSDB_CODE_SUCCESS;
16,044✔
1284

1285
  // update TDB
1286
  metaWLock(pMeta);
16,044✔
1287
  code = metaHandleNormalTableCreateImpl(pMeta, pEntry);
16,043✔
1288
  metaULock(pMeta);
16,043✔
1289

1290
  // update other stuff
1291
  if (TSDB_CODE_SUCCESS == code) {
16,044!
1292
    pMeta->pVnode->config.vndStats.numOfNTables++;
16,044✔
1293
    pMeta->pVnode->config.vndStats.numOfNTimeSeries += pEntry->ntbEntry.schemaRow.nCols - 1;
16,044✔
1294

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

1308
static int32_t metaHandleChildTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry, const SMetaEntry *pSuperEntry) {
157,753✔
1309
  int32_t code = TSDB_CODE_SUCCESS;
157,753✔
1310

1311
  SMetaTableOp ops[] = {
157,753✔
1312
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},  //
1313
      {META_UID_IDX, META_TABLE_OP_INSERT},      //
1314
      {META_NAME_IDX, META_TABLE_OP_INSERT},     //
1315
      {META_CHILD_IDX, META_TABLE_OP_INSERT},    //
1316
      {META_TAG_IDX, META_TABLE_OP_INSERT},      //
1317
      {META_BTIME_IDX, META_TABLE_OP_INSERT},    //
1318
      {META_TTL_IDX, META_TABLE_OP_INSERT},      //
1319
  };
1320

1321
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1,261,561✔
1322
    SMetaTableOp *op = &ops[i];
1,103,927✔
1323

1324
    SMetaHandleParam param = {
1,103,927✔
1325
        .pEntry = pEntry,
1326
        .pSuperEntry = pSuperEntry,
1327
    };
1328

1329
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
1,103,927✔
1330
    if (TSDB_CODE_SUCCESS != code) {
1,103,786!
1331
      metaErr(TD_VID(pMeta->pVnode), code);
×
1332
      return code;
×
1333
    }
1334
  }
1335

1336
  if (TSDB_CODE_SUCCESS == code) {
157,634!
1337
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
157,768✔
1338
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
157,762✔
1339
    if (ret < 0) {
157,803!
1340
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1341
    }
1342

1343
    ret = metaTbGroupCacheClear(pMeta, pSuperEntry->uid);
157,803✔
1344
    if (ret < 0) {
157,778!
1345
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1346
    }
1347
  }
1348
  return code;
157,781✔
1349
}
1350

1351
static int32_t metaHandleChildTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
157,751✔
1352
  int32_t     code = TSDB_CODE_SUCCESS;
157,751✔
1353
  SMetaEntry *pSuperEntry = NULL;
157,751✔
1354

1355
  // get the super table entry
1356
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
157,751✔
1357
  if (code) {
157,778!
1358
    metaErr(TD_VID(pMeta->pVnode), code);
×
1359
    return code;
×
1360
  }
1361

1362
  // update TDB
1363
  metaWLock(pMeta);
157,778✔
1364
  code = metaHandleChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
157,780✔
1365
  metaULock(pMeta);
157,768✔
1366

1367
  // update other stuff
1368
  if (TSDB_CODE_SUCCESS == code) {
157,794!
1369
    pMeta->pVnode->config.vndStats.numOfCTables++;
157,794✔
1370

1371
    if (!metaTbInFilterCache(pMeta, pSuperEntry->name, 1)) {
157,794✔
1372
      int32_t nCols = 0;
157,772✔
1373
      int32_t ret = metaGetStbStats(pMeta->pVnode, pSuperEntry->uid, 0, &nCols, 0);
157,772✔
1374
      if (ret < 0) {
157,753!
1375
        metaErr(TD_VID(pMeta->pVnode), ret);
×
1376
      }
1377
      pMeta->pVnode->config.vndStats.numOfTimeSeries += (nCols > 0 ? nCols - 1 : 0);
157,759!
1378
    }
1379

1380
    if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
157,766!
1381
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, pEntry->ctbEntry.suid, NULL);
12,464✔
1382
      if (rc < 0) {
12,464!
1383
        metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
1384
                  tstrerror(rc));
1385
      }
1386
    }
1387

1388
  } else {
1389
    metaErr(TD_VID(pMeta->pVnode), code);
×
1390
  }
1391
  metaTimeSeriesNotifyCheck(pMeta);
157,766✔
1392
  metaFetchEntryFree(&pSuperEntry);
157,782✔
1393
  return code;
157,798✔
1394
}
1395

1396
static int32_t metaHandleVirtualNormalTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
117✔
1397
  int32_t code = TSDB_CODE_SUCCESS;
117✔
1398

1399
  SMetaTableOp ops[] = {
117✔
1400
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1401
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: need to be insert
1402
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1403
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1404
      {META_BTIME_IDX, META_TABLE_OP_INSERT},     //
1405
  };
1406

1407
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
702✔
1408
    SMetaTableOp *op = &ops[i];
585✔
1409

1410
    SMetaHandleParam param = {
585✔
1411
        .pEntry = pEntry,
1412
    };
1413

1414
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
585✔
1415
    if (TSDB_CODE_SUCCESS != code) {
585!
1416
      metaErr(TD_VID(pMeta->pVnode), code);
×
1417
      return code;
×
1418
    }
1419
  }
1420

1421
  return code;
117✔
1422
}
1423

1424
static int32_t metaHandleVirtualNormalTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
117✔
1425
  int32_t code = TSDB_CODE_SUCCESS;
117✔
1426

1427
  // update TDB
1428
  metaWLock(pMeta);
117✔
1429
  code = metaHandleVirtualNormalTableCreateImpl(pMeta, pEntry);
117✔
1430
  metaULock(pMeta);
117✔
1431

1432
  // update other stuff
1433
  if (TSDB_CODE_SUCCESS == code) {
117!
1434
    pMeta->pVnode->config.vndStats.numOfVTables++;
117✔
1435
  } else {
1436
    metaErr(TD_VID(pMeta->pVnode), code);
×
1437
  }
1438
  return code;
117✔
1439
}
1440

1441
static int32_t metaHandleVirtualChildTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry,
70✔
1442
                                                     const SMetaEntry *pSuperEntry) {
1443
  int32_t code = TSDB_CODE_SUCCESS;
70✔
1444

1445
  SMetaTableOp ops[] = {
70✔
1446
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},  //
1447
      {META_UID_IDX, META_TABLE_OP_INSERT},      //
1448
      {META_NAME_IDX, META_TABLE_OP_INSERT},     //
1449
      {META_CHILD_IDX, META_TABLE_OP_INSERT},    //
1450
      {META_TAG_IDX, META_TABLE_OP_INSERT},      //
1451
      {META_BTIME_IDX, META_TABLE_OP_INSERT},    //
1452
  };
1453

1454
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
490✔
1455
    SMetaTableOp *op = &ops[i];
420✔
1456

1457
    SMetaHandleParam param = {
420✔
1458
        .pEntry = pEntry,
1459
        .pSuperEntry = pSuperEntry,
1460
    };
1461

1462
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
420✔
1463
    if (TSDB_CODE_SUCCESS != code) {
420!
1464
      metaErr(TD_VID(pMeta->pVnode), code);
×
1465
      return code;
×
1466
    }
1467
  }
1468

1469
  if (TSDB_CODE_SUCCESS == code) {
70!
1470
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
70✔
1471
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
70✔
1472
    if (ret < 0) {
70!
1473
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1474
    }
1475

1476
    ret = metaTbGroupCacheClear(pMeta, pSuperEntry->uid);
70✔
1477
    if (ret < 0) {
70!
1478
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1479
    }
1480

1481
    ret = metaRefDbsCacheClear(pMeta, pSuperEntry->uid);
70✔
1482
    if (ret < 0) {
70!
1483
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1484
    }
1485
  }
1486

1487
  return code;
70✔
1488
}
1489

1490
static int32_t metaHandleVirtualChildTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
70✔
1491
  int32_t     code = TSDB_CODE_SUCCESS;
70✔
1492
  SMetaEntry *pSuperEntry = NULL;
70✔
1493

1494
  // get the super table entry
1495
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
70✔
1496
  if (code) {
70!
1497
    metaErr(TD_VID(pMeta->pVnode), code);
×
1498
    return code;
×
1499
  }
1500

1501
  // update TDB
1502
  metaWLock(pMeta);
70✔
1503
  code = metaHandleVirtualChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
70✔
1504
  metaULock(pMeta);
70✔
1505

1506
  // update other stuff
1507
  if (TSDB_CODE_SUCCESS == code) {
70!
1508
    pMeta->pVnode->config.vndStats.numOfVCTables++;
70✔
1509
  } else {
1510
    metaErr(TD_VID(pMeta->pVnode), code);
×
1511
  }
1512

1513
  metaFetchEntryFree(&pSuperEntry);
70✔
1514
  return code;
70✔
1515
}
1516

1517
static int32_t metaHandleNormalTableDropImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
2,262✔
1518
  int32_t code = TSDB_CODE_SUCCESS;
2,262✔
1519

1520
  SMetaTableOp ops[] = {
2,262✔
1521
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1522
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1523
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1524
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1525
      {META_TTL_IDX, META_TABLE_OP_DELETE},      //
1526

1527
      // {META_SCHEMA_TABLE, META_TABLE_OP_DELETE},  //
1528
  };
1529

1530
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
13,572✔
1531
    SMetaTableOp *op = &ops[i];
11,310✔
1532
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
11,310✔
1533
    if (code) {
11,310!
1534
      const SMetaEntry *pEntry = pParam->pEntry;
×
1535
      metaErr(TD_VID(pMeta->pVnode), code);
×
1536
    }
1537
  }
1538

1539
  return code;
2,262✔
1540
}
1541

1542
static int32_t metaHandleNormalTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
2,262✔
1543
  int32_t     code = TSDB_CODE_SUCCESS;
2,262✔
1544
  SMetaEntry *pOldEntry = NULL;
2,262✔
1545

1546
  // fetch the entry
1547
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
2,262✔
1548
  if (code) {
2,262!
1549
    metaErr(TD_VID(pMeta->pVnode), code);
×
1550
    return code;
×
1551
  }
1552

1553
  SMetaHandleParam param = {
2,262✔
1554
      .pEntry = pEntry,
1555
      .pOldEntry = pOldEntry,
1556
  };
1557

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

1568
  // update other stuff
1569
  pMeta->pVnode->config.vndStats.numOfNTables--;
2,262✔
1570
  pMeta->pVnode->config.vndStats.numOfNTimeSeries -= (pOldEntry->ntbEntry.schemaRow.nCols - 1);
2,262✔
1571

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

1581
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
2,262!
1582
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pOldEntry->uid, 0, NULL);
×
1583
    if (ret < 0) {
×
1584
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1585
    }
1586
  }
1587

1588
  metaFetchEntryFree(&pOldEntry);
2,262✔
1589
  return code;
2,262✔
1590
}
1591

1592
static int32_t metaHandleChildTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam, bool superDropped) {
15,823✔
1593
  int32_t code = TSDB_CODE_SUCCESS;
15,823✔
1594

1595
  const SMetaEntry *pEntry = pParam->pEntry;
15,823✔
1596
  const SMetaEntry *pChild = pParam->pOldEntry;
15,823✔
1597
  const SMetaEntry *pSuper = pParam->pSuperEntry;
15,823✔
1598

1599
  SMetaTableOp ops[] = {
15,823✔
1600
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1601
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1602
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1603
      {META_CHILD_IDX, META_TABLE_OP_DELETE},    //
1604
      {META_TAG_IDX, META_TABLE_OP_DELETE},      //
1605
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1606
      {META_TTL_IDX, META_TABLE_OP_DELETE},      //
1607
  };
1608

1609
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
126,572✔
1610
    SMetaTableOp *op = &ops[i];
110,756✔
1611

1612
    if (op->table == META_ENTRY_TABLE && superDropped) {
110,756✔
1613
      continue;
2,328✔
1614
    }
1615

1616
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
108,428✔
1617
    if (code) {
108,430✔
1618
      metaErr(TD_VID(pMeta->pVnode), code);
9!
1619
      return code;
4✔
1620
    }
1621
  }
1622

1623
  --pMeta->pVnode->config.vndStats.numOfCTables;
15,816✔
1624
  metaUpdateStbStats(pMeta, pParam->pSuperEntry->uid, -1, 0, -1);
15,816✔
1625
  int32_t ret = metaUidCacheClear(pMeta, pSuper->uid);
15,819✔
1626
  if (ret < 0) {
15,819!
1627
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1628
  }
1629

1630
  ret = metaTbGroupCacheClear(pMeta, pSuper->uid);
15,819✔
1631
  if (ret < 0) {
15,819!
1632
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1633
  }
1634
  return code;
15,819✔
1635
}
1636

1637
static int32_t metaHandleChildTableDrop(SMeta *pMeta, const SMetaEntry *pEntry, bool superDropped) {
15,823✔
1638
  int32_t     code = TSDB_CODE_SUCCESS;
15,823✔
1639
  SMetaEntry *pChild = NULL;
15,823✔
1640
  SMetaEntry *pSuper = NULL;
15,823✔
1641

1642
  // fetch old entry
1643
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pChild);
15,823✔
1644
  if (code) {
15,823!
1645
    metaErr(TD_VID(pMeta->pVnode), code);
×
1646
    return code;
×
1647
  }
1648

1649
  // fetch super entry
1650
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
15,823✔
1651
  if (code) {
15,823!
1652
    metaErr(TD_VID(pMeta->pVnode), code);
×
1653
    metaFetchEntryFree(&pChild);
×
1654
    return code;
×
1655
  }
1656

1657
  SMetaHandleParam param = {
15,823✔
1658
      .pEntry = pEntry,
1659
      .pOldEntry = pChild,
1660
      .pSuperEntry = pSuper,
1661
  };
1662

1663
  // do the drop
1664
  metaWLock(pMeta);
15,823✔
1665
  code = metaHandleChildTableDropImpl(pMeta, &param, superDropped);
15,823✔
1666
  metaULock(pMeta);
15,823✔
1667
  if (code) {
15,822✔
1668
    metaErr(TD_VID(pMeta->pVnode), code);
4!
1669
    metaFetchEntryFree(&pChild);
4✔
1670
    metaFetchEntryFree(&pSuper);
4✔
1671
    return code;
4✔
1672
  }
1673

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

1683
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
15,819!
1684
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pChild->uid, pSuper->uid, NULL);
76✔
1685
    if (ret < 0) {
76!
1686
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1687
    }
1688
  }
1689

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

1698
  if ((type == TSDB_CHILD_TABLE) && tbUid) {
1699
    *tbUid = uid;
1700
  }
1701
#endif
1702
  metaFetchEntryFree(&pChild);
15,819✔
1703
  metaFetchEntryFree(&pSuper);
15,819✔
1704
  return code;
15,819✔
1705
}
1706

1707
static int32_t metaHandleVirtualNormalTableDropImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
47✔
1708
  int32_t code = TSDB_CODE_SUCCESS;
47✔
1709

1710
  SMetaTableOp ops[] = {
47✔
1711
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1712
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1713
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1714
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1715

1716
      // {META_SCHEMA_TABLE, META_TABLE_OP_DELETE},  //
1717
  };
1718

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

1728
  return code;
47✔
1729
}
1730

1731
static int32_t metaHandleVirtualNormalTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
47✔
1732
  int32_t     code = TSDB_CODE_SUCCESS;
47✔
1733
  SMetaEntry *pOldEntry = NULL;
47✔
1734

1735
  // fetch the entry
1736
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
47✔
1737
  if (code) {
47!
1738
    metaErr(TD_VID(pMeta->pVnode), code);
×
1739
    return code;
×
1740
  }
1741

1742
  SMetaHandleParam param = {
47✔
1743
      .pEntry = pEntry,
1744
      .pOldEntry = pOldEntry,
1745
  };
1746

1747
  // do the drop
1748
  metaWLock(pMeta);
47✔
1749
  code = metaHandleVirtualNormalTableDropImpl(pMeta, &param);
47✔
1750
  metaULock(pMeta);
47✔
1751
  if (code) {
47!
1752
    metaErr(TD_VID(pMeta->pVnode), code);
×
1753
    metaFetchEntryFree(&pOldEntry);
×
1754
    return code;
×
1755
  }
1756

1757
  // update other stuff
1758
  pMeta->pVnode->config.vndStats.numOfVTables--;
47✔
1759

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

1769
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
47!
1770
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pOldEntry->uid, 0, NULL);
×
1771
    if (ret < 0) {
×
1772
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1773
    }
1774
  }
1775

1776
  metaFetchEntryFree(&pOldEntry);
47✔
1777
  return code;
47✔
1778
}
1779

1780
static int32_t metaHandleVirtualChildTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam, bool superDropped) {
7✔
1781
  int32_t code = TSDB_CODE_SUCCESS;
7✔
1782

1783
  const SMetaEntry *pEntry = pParam->pEntry;
7✔
1784
  const SMetaEntry *pChild = pParam->pOldEntry;
7✔
1785
  const SMetaEntry *pSuper = pParam->pSuperEntry;
7✔
1786

1787
  SMetaTableOp ops[] = {
7✔
1788
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1789
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1790
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1791
      {META_CHILD_IDX, META_TABLE_OP_DELETE},    //
1792
      {META_TAG_IDX, META_TABLE_OP_DELETE},      //
1793
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1794
  };
1795

1796
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
49✔
1797
    SMetaTableOp *op = &ops[i];
42✔
1798

1799
    if (op->table == META_ENTRY_TABLE && superDropped) {
42!
1800
      continue;
×
1801
    }
1802

1803
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
42✔
1804
    if (code) {
42!
1805
      metaErr(TD_VID(pMeta->pVnode), code);
×
1806
      return code;
×
1807
    }
1808
  }
1809

1810
  --pMeta->pVnode->config.vndStats.numOfVCTables;
7✔
1811
  metaUpdateStbStats(pMeta, pParam->pSuperEntry->uid, -1, 0, -1);
7✔
1812
  int32_t ret = metaUidCacheClear(pMeta, pSuper->uid);
7✔
1813
  if (ret < 0) {
7!
1814
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1815
  }
1816

1817
  ret = metaTbGroupCacheClear(pMeta, pSuper->uid);
7✔
1818
  if (ret < 0) {
7!
1819
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1820
  }
1821

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

1827
  return code;
7✔
1828
}
1829

1830
static int32_t metaHandleVirtualChildTableDrop(SMeta *pMeta, const SMetaEntry *pEntry, bool superDropped) {
7✔
1831
  int32_t     code = TSDB_CODE_SUCCESS;
7✔
1832
  SMetaEntry *pChild = NULL;
7✔
1833
  SMetaEntry *pSuper = NULL;
7✔
1834

1835
  // fetch old entry
1836
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pChild);
7✔
1837
  if (code) {
7!
1838
    metaErr(TD_VID(pMeta->pVnode), code);
×
1839
    return code;
×
1840
  }
1841

1842
  // fetch super entry
1843
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
7✔
1844
  if (code) {
7!
1845
    metaErr(TD_VID(pMeta->pVnode), code);
×
1846
    metaFetchEntryFree(&pChild);
×
1847
    return code;
×
1848
  }
1849

1850
  SMetaHandleParam param = {
7✔
1851
      .pEntry = pEntry,
1852
      .pOldEntry = pChild,
1853
      .pSuperEntry = pSuper,
1854
  };
1855

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

1867
  metaFetchEntryFree(&pChild);
7✔
1868
  metaFetchEntryFree(&pSuper);
7✔
1869
  return code;
7✔
1870
}
1871

1872
static int32_t metaGetChildUidsOfSuperTable(SMeta *pMeta, tb_uid_t suid, SArray **childList) {
2,340✔
1873
  int32_t code = TSDB_CODE_SUCCESS;
2,340✔
1874
  void   *key = NULL;
2,340✔
1875
  int32_t keySize = 0;
2,340✔
1876
  int32_t c;
1877

1878
  *childList = taosArrayInit(64, sizeof(tb_uid_t));
2,340✔
1879
  if (*childList == NULL) {
2,350!
1880
    return terrno;
×
1881
  }
1882

1883
  TBC *cursor = NULL;
2,350✔
1884
  code = tdbTbcOpen(pMeta->pCtbIdx, &cursor, NULL);
2,350✔
1885
  if (code) {
2,348!
1886
    taosArrayDestroy(*childList);
×
1887
    *childList = NULL;
×
1888
    return code;
×
1889
  }
1890

1891
  int32_t rc = tdbTbcMoveTo(cursor,
2,348✔
1892
                            &(SCtbIdxKey){
2,348✔
1893
                                .suid = suid,
1894
                                .uid = INT64_MIN,
1895
                            },
1896
                            sizeof(SCtbIdxKey), &c);
1897
  if (rc < 0) {
2,350!
1898
    tdbTbcClose(cursor);
×
1899
    return 0;
×
1900
  }
1901

1902
  for (;;) {
1903
    if (tdbTbcNext(cursor, &key, &keySize, NULL, NULL) < 0) {
6,723✔
1904
      break;
1,454✔
1905
    }
1906

1907
    if (((SCtbIdxKey *)key)->suid < suid) {
5,266✔
1908
      continue;
252✔
1909
    } else if (((SCtbIdxKey *)key)->suid > suid) {
5,014✔
1910
      break;
896✔
1911
    }
1912

1913
    if (taosArrayPush(*childList, &(((SCtbIdxKey *)key)->uid)) == NULL) {
8,239!
1914
      tdbFreeClear(key);
×
1915
      tdbTbcClose(cursor);
×
1916
      taosArrayDestroy(*childList);
×
1917
      *childList = NULL;
×
1918
      return terrno;
×
1919
    }
1920
  }
1921

1922
  tdbTbcClose(cursor);
2,350✔
1923
  tdbFreeClear(key);
2,349✔
1924
  return code;
2,350✔
1925
}
1926

1927
static int32_t metaHandleSuperTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
1,933✔
1928
  int32_t           code = TSDB_CODE_SUCCESS;
1,933✔
1929
  const SMetaEntry *pEntry = pParam->pEntry;
1,933✔
1930

1931
  SMetaTableOp ops[] = {
1,933✔
1932
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1933
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1934
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1935
      {META_SUID_IDX, META_TABLE_OP_DELETE},     //
1936

1937
      // {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: here should be insert
1938
  };
1939

1940
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
9,670✔
1941
    SMetaTableOp *op = &ops[i];
7,735✔
1942

1943
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
7,735✔
1944
    if (TSDB_CODE_SUCCESS != code) {
7,737!
1945
      metaErr(TD_VID(pMeta->pVnode), code);
×
1946
      return code;
×
1947
    }
1948
  }
1949

1950
  int32_t ret = metaStatsCacheDrop(pMeta, pEntry->uid);
1,935✔
1951
  if (ret < 0) {
1,935✔
1952
    metaErr(TD_VID(pMeta->pVnode), ret);
605!
1953
  }
1954
  return code;
1,936✔
1955
}
1956

1957
static int32_t metaHandleNormalTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
5,469✔
1958
  int32_t code = TSDB_CODE_SUCCESS;
5,469✔
1959

1960
  const SMetaEntry *pEntry = pParam->pEntry;
5,469✔
1961

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

1984
static int32_t metaHandleVirtualNormalTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
302✔
1985
  int32_t code = TSDB_CODE_SUCCESS;
302✔
1986

1987
  const SMetaEntry *pEntry = pParam->pEntry;
302✔
1988

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

2010
static int32_t metaHandleVirtualChildTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
2011
  int32_t code = TSDB_CODE_SUCCESS;
×
2012

2013
  const SMetaEntry *pEntry = pParam->pEntry;
×
2014
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
×
2015
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
×
2016

2017
  SMetaTableOp ops[] = {
×
2018
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},  //
2019
      {META_UID_IDX, META_TABLE_OP_UPDATA},      //
2020
      {META_TAG_IDX, META_TABLE_OP_UPDATA},      //
2021
      {META_CHILD_IDX, META_TABLE_OP_UPDATA},    //
2022
  };
2023

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

2033
  if (metaUidCacheClear(pMeta, pSuperEntry->uid) < 0) {
×
2034
    metaErr(TD_VID(pMeta->pVnode), code);
×
2035
  }
2036

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

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

2047
static int32_t metaHandleChildTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
3,733✔
2048
  int32_t code = TSDB_CODE_SUCCESS;
3,733✔
2049

2050
  const SMetaEntry *pEntry = pParam->pEntry;
3,733✔
2051
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
3,733✔
2052
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
3,733✔
2053

2054
  SMetaTableOp ops[] = {
3,733✔
2055
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},  //
2056
      {META_UID_IDX, META_TABLE_OP_UPDATA},      //
2057
      {META_TAG_IDX, META_TABLE_OP_UPDATA},      //
2058
      {META_CHILD_IDX, META_TABLE_OP_UPDATA},    //
2059
      {META_TTL_IDX, META_TABLE_OP_UPDATA},      //
2060
  };
2061

2062
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
22,398✔
2063
    SMetaTableOp *op = &ops[i];
18,665✔
2064
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
18,665✔
2065
    if (code) {
18,665!
2066
      metaErr(TD_VID(pMeta->pVnode), code);
×
2067
      return code;
×
2068
    }
2069
  }
2070

2071
  if (metaUidCacheClear(pMeta, pSuperEntry->uid) < 0) {
3,733!
2072
    metaErr(TD_VID(pMeta->pVnode), code);
×
2073
  }
2074

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

2086
static int32_t metaHandleSuperTableUpdateImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
12,737✔
2087
  int32_t code = TSDB_CODE_SUCCESS;
12,737✔
2088

2089
  const SMetaEntry *pEntry = pParam->pEntry;
12,737✔
2090
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
12,737✔
2091

2092
  SMetaTableOp ops[] = {
12,737✔
2093
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
2094
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
2095
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
2096
  };
2097

2098
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
50,932✔
2099
    SMetaTableOp *op = &ops[i];
38,227✔
2100
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
38,227✔
2101
    if (code) {
38,205✔
2102
      metaErr(TD_VID(pMeta->pVnode), code);
10!
2103
      return code;
×
2104
    }
2105
  }
2106

2107
  if (TSDB_CODE_SUCCESS == code) {
12,705!
2108
    metaUpdateStbStats(pMeta, pEntry->uid, 0, pEntry->stbEntry.schemaRow.nCols - pOldEntry->stbEntry.schemaRow.nCols,
12,709✔
2109
                       pEntry->stbEntry.keep);
12,709✔
2110
  }
2111

2112
  return code;
12,760✔
2113
}
2114

2115
static int32_t metaHandleSuperTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
12,725✔
2116
  int32_t code = TSDB_CODE_SUCCESS;
12,725✔
2117

2118
  SMetaEntry *pOldEntry = NULL;
12,725✔
2119

2120
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
12,725✔
2121
  if (code) {
12,760!
2122
    metaErr(TD_VID(pMeta->pVnode), code);
×
2123
    return code;
×
2124
  }
2125

2126
  SMetaHandleParam param = {
12,760✔
2127
      .pEntry = pEntry,
2128
      .pOldEntry = pOldEntry,
2129
  };
2130
  metaWLock(pMeta);
12,760✔
2131
  code = metaHandleSuperTableUpdateImpl(pMeta, &param);
12,761✔
2132
  metaULock(pMeta);
12,724✔
2133
  if (code) {
12,732!
2134
    metaErr(TD_VID(pMeta->pVnode), code);
×
2135
    metaFetchEntryFree(&pOldEntry);
×
2136
    return code;
×
2137
  }
2138

2139
  int     nCols = pEntry->stbEntry.schemaRow.nCols;
12,732✔
2140
  int     onCols = pOldEntry->stbEntry.schemaRow.nCols;
12,732✔
2141
  int32_t deltaCol = nCols - onCols;
12,732✔
2142
  bool    updStat = deltaCol != 0 && !TABLE_IS_VIRTUAL(pEntry->flags) && !metaTbInFilterCache(pMeta, pEntry->name, 1);
12,732!
2143

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

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

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

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

2210
static int32_t metaHandleChildTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
3,733✔
2211
  int32_t code = TSDB_CODE_SUCCESS;
3,733✔
2212

2213
  SMetaEntry *pOldEntry = NULL;
3,733✔
2214
  SMetaEntry *pSuperEntry = NULL;
3,733✔
2215

2216
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
3,733✔
2217
  if (code) {
3,733!
2218
    metaErr(TD_VID(pMeta->pVnode), code);
×
2219
    return code;
×
2220
  }
2221

2222
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
3,733✔
2223
  if (code) {
3,733!
2224
    metaErr(TD_VID(pMeta->pVnode), code);
×
2225
    metaFetchEntryFree(&pOldEntry);
×
2226
    return code;
×
2227
  }
2228

2229
  SMetaHandleParam param = {
3,733✔
2230
      .pEntry = pEntry,
2231
      .pOldEntry = pOldEntry,
2232
      .pSuperEntry = pSuperEntry,
2233
  };
2234

2235
  metaWLock(pMeta);
3,733✔
2236
  code = metaHandleChildTableUpdateImpl(pMeta, &param);
3,733✔
2237
  metaULock(pMeta);
3,733✔
2238
  if (code) {
3,733!
2239
    metaErr(TD_VID(pMeta->pVnode), code);
×
2240
    metaFetchEntryFree(&pOldEntry);
×
2241
    metaFetchEntryFree(&pSuperEntry);
×
2242
    return code;
×
2243
  }
2244

2245
  metaFetchEntryFree(&pOldEntry);
3,733✔
2246
  metaFetchEntryFree(&pSuperEntry);
3,733✔
2247
  return code;
3,733✔
2248
}
2249

2250
static int32_t metaHandleNormalTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
5,469✔
2251
  int32_t     code = TSDB_CODE_SUCCESS;
5,469✔
2252
  SMetaEntry *pOldEntry = NULL;
5,469✔
2253

2254
  // fetch old entry
2255
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
5,469✔
2256
  if (code) {
5,469!
2257
    metaErr(TD_VID(pMeta->pVnode), code);
×
2258
    return code;
×
2259
  }
2260

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

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

2290
      if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
2291
        int16_t cid = pColumn->colId;
2292

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

2313
static int32_t metaHandleVirtualNormalTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
302✔
2314
  int32_t     code = TSDB_CODE_SUCCESS;
302✔
2315
  SMetaEntry *pOldEntry = NULL;
302✔
2316

2317
  // fetch old entry
2318
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
302✔
2319
  if (code) {
302!
2320
    metaErr(TD_VID(pMeta->pVnode), code);
×
2321
    return code;
×
2322
  }
2323

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

2338
  metaTimeSeriesNotifyCheck(pMeta);
302✔
2339
  metaFetchEntryFree(&pOldEntry);
302✔
2340
  return code;
302✔
2341
}
2342

2343
static int32_t metaHandleVirtualChildTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
×
2344
  int32_t code = TSDB_CODE_SUCCESS;
×
2345

2346
  SMetaEntry *pOldEntry = NULL;
×
2347
  SMetaEntry *pSuperEntry = NULL;
×
2348

2349
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
×
2350
  if (code) {
×
2351
    metaErr(TD_VID(pMeta->pVnode), code);
×
2352
    return code;
×
2353
  }
2354

2355
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
×
2356
  if (code) {
×
2357
    metaErr(TD_VID(pMeta->pVnode), code);
×
2358
    metaFetchEntryFree(&pOldEntry);
×
2359
    return code;
×
2360
  }
2361

2362
  SMetaHandleParam param = {
×
2363
      .pEntry = pEntry,
2364
      .pOldEntry = pOldEntry,
2365
      .pSuperEntry = pSuperEntry,
2366
  };
2367

2368
  metaWLock(pMeta);
×
2369
  code = metaHandleVirtualChildTableUpdateImpl(pMeta, &param);
×
2370
  metaULock(pMeta);
×
2371
  if (code) {
×
2372
    metaErr(TD_VID(pMeta->pVnode), code);
×
2373
    metaFetchEntryFree(&pOldEntry);
×
2374
    metaFetchEntryFree(&pSuperEntry);
×
2375
    return code;
×
2376
  }
2377

2378
  metaFetchEntryFree(&pOldEntry);
×
2379
  metaFetchEntryFree(&pSuperEntry);
×
2380
  return code;
×
2381
}
2382

2383
static int32_t metaHandleSuperTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
1,929✔
2384
  int32_t     code = TSDB_CODE_SUCCESS;
1,929✔
2385
  SArray     *childList = NULL;
1,929✔
2386
  SMetaEntry *pOldEntry = NULL;
1,929✔
2387

2388
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
1,929✔
2389
  if (code) {
1,936!
2390
    metaErr(TD_VID(pMeta->pVnode), code);
×
2391
    return code;
×
2392
  }
2393

2394
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childList);
1,936✔
2395
  if (code) {
1,936!
2396
    metaErr(TD_VID(pMeta->pVnode), code);
×
2397
    metaFetchEntryFree(&pOldEntry);
×
2398
    return code;
×
2399
  }
2400

2401
  if (pMeta->pVnode->pTsdb && tsdbCacheDropSubTables(pMeta->pVnode->pTsdb, childList, pEntry->uid) < 0) {
1,936!
2402
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
6!
2403
              pEntry->uid, tstrerror(terrno));
2404
  }
2405

2406
  // loop to drop all child tables
2407
  for (int32_t i = 0; i < taosArrayGetSize(childList); i++) {
4,260✔
2408
    SMetaEntry childEntry = {
4,656✔
2409
        .version = pEntry->version,
2,328✔
2410
        .uid = *(tb_uid_t *)taosArrayGet(childList, i),
2,328✔
2411
        .type = -TSDB_CHILD_TABLE,
2412
    };
2413

2414
    code = metaHandleChildTableDrop(pMeta, &childEntry, true);
2,328✔
2415
    if (code) {
2,328✔
2416
      metaErr(TD_VID(pMeta->pVnode), code);
4!
2417
    }
2418
  }
2419

2420
  // do drop super table
2421
  SMetaHandleParam param = {
1,930✔
2422
      .pEntry = pEntry,
2423
      .pOldEntry = pOldEntry,
2424
  };
2425
  metaWLock(pMeta);
1,930✔
2426
  code = metaHandleSuperTableDropImpl(pMeta, &param);
1,936✔
2427
  metaULock(pMeta);
1,936✔
2428
  if (code) {
1,935!
2429
    metaErr(TD_VID(pMeta->pVnode), code);
×
2430
    taosArrayDestroy(childList);
×
2431
    metaFetchEntryFree(&pOldEntry);
×
2432
    return code;
×
2433
  }
2434

2435
  // do other stuff
2436
  // metaUpdTimeSeriesNum(pMeta);
2437

2438
  // free resource and return
2439
  taosArrayDestroy(childList);
1,935✔
2440
  metaFetchEntryFree(&pOldEntry);
1,935✔
2441
  return code;
1,936✔
2442
}
2443

2444
int32_t metaHandleEntry2(SMeta *pMeta, const SMetaEntry *pEntry) {
240,322✔
2445
  int32_t   code = TSDB_CODE_SUCCESS;
240,322✔
2446
  int32_t   vgId = TD_VID(pMeta->pVnode);
240,322✔
2447
  SMetaInfo info = {0};
240,322✔
2448
  int8_t    type = pEntry->type > 0 ? pEntry->type : -pEntry->type;
240,322✔
2449

2450
  if (NULL == pMeta || NULL == pEntry) {
240,322!
2451
    metaError("%s failed at %s:%d since invalid parameter", __func__, __FILE__, __LINE__);
×
2452
    return TSDB_CODE_INVALID_PARA;
×
2453
  }
2454

2455
  if (pEntry->type > 0) {
240,514✔
2456
    bool isExist = false;
222,771✔
2457
    if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) {
222,771✔
2458
      isExist = true;
22,270✔
2459
    }
2460

2461
    switch (type) {
222,788!
2462
      case TSDB_SUPER_TABLE: {
39,294✔
2463
        if (isExist) {
39,294✔
2464
          code = metaHandleSuperTableUpdate(pMeta, pEntry);
12,767✔
2465
        } else {
2466
          code = metaHandleSuperTableCreate(pMeta, pEntry);
26,527✔
2467
        }
2468
        break;
39,283✔
2469
      }
2470
      case TSDB_CHILD_TABLE: {
161,508✔
2471
        if (isExist) {
161,508✔
2472
          code = metaHandleChildTableUpdate(pMeta, pEntry);
3,733✔
2473
        } else {
2474
          code = metaHandleChildTableCreate(pMeta, pEntry);
157,775✔
2475
        }
2476
        break;
161,519✔
2477
      }
2478
      case TSDB_NORMAL_TABLE: {
21,513✔
2479
        if (isExist) {
21,513✔
2480
          code = metaHandleNormalTableUpdate(pMeta, pEntry);
5,469✔
2481
        } else {
2482
          code = metaHandleNormalTableCreate(pMeta, pEntry);
16,044✔
2483
        }
2484
        break;
21,513✔
2485
      }
2486
      case TSDB_VIRTUAL_NORMAL_TABLE: {
419✔
2487
        if (isExist) {
419✔
2488
          code = metaHandleVirtualNormalTableUpdate(pMeta, pEntry);
302✔
2489
        } else {
2490
          code = metaHandleVirtualNormalTableCreate(pMeta, pEntry);
117✔
2491
        }
2492
        break;
419✔
2493
      }
2494
      case TSDB_VIRTUAL_CHILD_TABLE: {
70✔
2495
        if (isExist) {
70!
2496
          code = metaHandleVirtualChildTableUpdate(pMeta, pEntry);
×
2497
        } else {
2498
          code = metaHandleVirtualChildTableCreate(pMeta, pEntry);
70✔
2499
        }
2500
        break;
70✔
2501
      }
2502
      default: {
×
2503
        code = TSDB_CODE_INVALID_PARA;
×
2504
        break;
×
2505
      }
2506
    }
2507
  } else {
2508
    switch (type) {
17,743✔
2509
      case TSDB_SUPER_TABLE: {
1,931✔
2510
        code = metaHandleSuperTableDrop(pMeta, pEntry);
1,931✔
2511
        break;
1,935✔
2512
      }
2513
      case TSDB_CHILD_TABLE: {
13,495✔
2514
        code = metaHandleChildTableDrop(pMeta, pEntry, false);
13,495✔
2515
        break;
13,495✔
2516
      }
2517
      case TSDB_NORMAL_TABLE: {
2,262✔
2518
        code = metaHandleNormalTableDrop(pMeta, pEntry);
2,262✔
2519
        break;
2,262✔
2520
      }
2521
      case TSDB_VIRTUAL_NORMAL_TABLE: {
47✔
2522
        code = metaHandleVirtualNormalTableDrop(pMeta, pEntry);
47✔
2523
        break;
47✔
2524
      }
2525
      case TSDB_VIRTUAL_CHILD_TABLE: {
7✔
2526
        code = metaHandleVirtualChildTableDrop(pMeta, pEntry, false);
7✔
2527
        break;
7✔
2528
      }
2529
      default: {
1✔
2530
        code = TSDB_CODE_INVALID_PARA;
1✔
2531
        break;
1✔
2532
      }
2533
    }
2534
  }
2535

2536
  if (TSDB_CODE_SUCCESS == code) {
240,535✔
2537
    pMeta->changed = true;
240,533✔
2538
    metaDebug("vgId:%d, index:%" PRId64 ", handle meta entry success, type:%d tb:%s uid:%" PRId64, vgId,
240,533✔
2539
              pEntry->version, pEntry->type, pEntry->type > 0 ? pEntry->name : "", pEntry->uid);
2540
  } else {
2541
    metaErr(vgId, code);
2!
2542
  }
2543
  TAOS_RETURN(code);
240,538✔
2544
}
2545

2546
void metaHandleSyncEntry(SMeta *pMeta, const SMetaEntry *pEntry) {
497✔
2547
  int32_t code = TSDB_CODE_SUCCESS;
497✔
2548
  code = metaHandleEntry2(pMeta, pEntry);
497✔
2549
  if (code) {
497!
2550
    metaErr(TD_VID(pMeta->pVnode), code);
×
2551
  }
2552
  return;
497✔
2553
}
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