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

taosdata / TDengine / #4103

17 May 2025 02:18AM UTC coverage: 63.264% (+0.4%) from 62.905%
#4103

push

travis-ci

web-flow
Merge pull request #31110 from taosdata/3.0

merge 3.0

158149 of 318142 branches covered (49.71%)

Branch coverage included in aggregate %.

3 of 5 new or added lines in 1 file covered. (60.0%)

1725 existing lines in 138 files now uncovered.

243642 of 316962 relevant lines covered (76.87%)

16346281.8 hits per line

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

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

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

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

96
  tDecoderInit(&decoder, value, valueSize);
469,229✔
97
  code = metaDecodeEntry(&decoder, &entry);
469,080✔
98
  if (code) {
468,796!
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);
468,796✔
107
  if (code) {
469,048!
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);
469,048✔
116
  tDecoderClear(&decoder);
469,019✔
117
  return code;
469,189✔
118
}
119

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

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

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

141
void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); }
469,164✔
142

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

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

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

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

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

180
  // put to tdb
181
  if (META_TABLE_OP_INSERT == op) {
264,059✔
182
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
226,162✔
183
  } else if (META_TABLE_OP_UPDATA == op) {
37,897✔
184
    code = tdbTbUpsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
18,552✔
185
  } else if (META_TABLE_OP_DELETE == op) {
19,345!
186
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
19,345✔
187
  } else {
188
    code = TSDB_CODE_INVALID_PARA;
×
189
  }
190
  if (TSDB_CODE_SUCCESS != code) {
264,217!
191
    metaErr(vgId, code);
×
192
  }
193
  taosMemoryFree(value);
264,116!
194
  return code;
264,186✔
195
}
196

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

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

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

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

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

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

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

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

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

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

281
  if (pOldColumn && pNewColumn) {
151,068✔
282
    if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
148,700✔
283
      return TSDB_CODE_SUCCESS;
4,281✔
284
    } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) {
144,419!
285
      action = DROP_INDEX;
2,200✔
286
    } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
142,219!
287
      action = ADD_INDEX;
975✔
288
    } else {
289
      return TSDB_CODE_SUCCESS;
141,244✔
290
    }
291
  } else if (pOldColumn) {
2,368✔
292
    if (IS_IDX_ON(pOldColumn)) {
884✔
293
      action = DROP_INDEX;
45✔
294
    } else {
295
      return TSDB_CODE_SUCCESS;
839✔
296
    }
297
  } else {
298
    if (IS_IDX_ON(pNewColumn)) {
1,484!
299
      action = ADD_INDEX;
×
300
    } else {
301
      return TSDB_CODE_SUCCESS;
1,484✔
302
    }
303
  }
304

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

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

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

326
    STagIdxKey *pTagIdxKey = NULL;
24,796✔
327
    int32_t     tagIdxKeySize = 0;
24,796✔
328

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

369
  taosArrayDestroy(childTables);
3,220✔
370
  return code;
3,220✔
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) {
7,242✔
453
  int32_t               code = TSDB_CODE_SUCCESS;
7,242✔
454
  const SMetaEntry     *pEntry = pParam->pEntry;
7,242✔
455
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
7,242✔
456
  const SSchemaWrapper *pNewTagSchema = &pEntry->stbEntry.schemaTag;
7,242✔
457
  const SSchemaWrapper *pOldTagSchema = &pOldEntry->stbEntry.schemaTag;
7,242✔
458

459
  int32_t iOld = 0, iNew = 0;
7,242✔
460
  for (; iOld < pOldTagSchema->nCols && iNew < pNewTagSchema->nCols;) {
156,642✔
461
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
149,400✔
462
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
149,400✔
463

464
    if (pOldColumn->colId == pNewColumn->colId) {
149,400✔
465
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
148,695✔
466
      if (code) {
148,703✔
467
        metaErr(TD_VID(pMeta->pVnode), code);
8!
468
        return code;
×
469
      }
470

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

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

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

510
  return code;
7,243✔
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) {
61,061✔
575
  int32_t code = TSDB_CODE_SUCCESS;
61,061✔
576

577
  const SMetaEntry *pEntry = pParam->pEntry;
61,061✔
578
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
61,061✔
579

580
  if (NULL == pOldEntry) {
61,061✔
581
    return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
45,818✔
582
  }
583

584
  if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
15,243✔
585
    // check row schema
586
    if (pOldEntry->ntbEntry.schemaRow.version != pEntry->ntbEntry.schemaRow.version) {
1,258!
587
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
1,281✔
588
    }
589
  } else if (pEntry->type == TSDB_SUPER_TABLE) {
13,985!
590
    // check row schema
591
    if (pOldEntry->stbEntry.schemaRow.version != pEntry->stbEntry.schemaRow.version) {
13,988✔
592
      if (TABLE_IS_VIRTUAL(pEntry->flags)) {
6,742✔
593
        return metaUpdateSuperTableRowSchema(pMeta, pParam);
12✔
594
      } else {
595
        return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
6,730✔
596
      }
597
    }
598

599
    // check tag schema
600
    code = metaUpdateSuperTableTagSchema(pMeta, pParam);
7,246✔
601
    if (code) {
7,245!
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;
7,309✔
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) {
244,617✔
620
  pInfo->uid = pEntry->uid;
244,617✔
621
  pInfo->version = pEntry->version;
244,617✔
622
  if (pEntry->type == TSDB_SUPER_TABLE) {
244,617✔
623
    pInfo->suid = pEntry->uid;
43,910✔
624
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
43,910✔
625
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
200,707✔
626
    pInfo->suid = pEntry->ctbEntry.suid;
183,509✔
627
    pInfo->skmVer = 0;
183,509✔
628
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
17,198!
629
    pInfo->suid = 0;
17,198✔
630
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
17,198✔
631
  }
632
}
244,617✔
633

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

638
  const SMetaEntry *pEntry = pParam->pEntry;
244,608✔
639

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

648
  // put to tdb
649
  SUidIdxVal value = {
244,638✔
650
      .suid = info.suid,
244,638✔
651
      .skmVer = info.skmVer,
244,638✔
652
      .version = pEntry->version,
244,638✔
653
  };
654
  if (META_TABLE_OP_INSERT == op) {
244,638✔
655
    code = tdbTbInsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
226,087✔
656
  } else if (META_TABLE_OP_UPDATA == op) {
18,551!
657
    code = tdbTbUpsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
18,557✔
658
  }
659
  return code;
244,925✔
660
}
661

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

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

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

673
  const SMetaEntry *pEntry = pParam->pOldEntry;
24,805✔
674

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

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

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

690
  const SMetaEntry *pEntry = pParam->pEntry;
226,157✔
691

692
  if (META_TABLE_OP_INSERT == op) {
226,157!
693
    code = tdbTbInsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
226,171✔
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;
226,329✔
702
}
703

704
static int32_t metaNameIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
226,156✔
705
  int32_t code = TSDB_CODE_SUCCESS;
226,156✔
706
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
226,156✔
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) {
24,805✔
714
  int32_t code = TSDB_CODE_SUCCESS;
24,805✔
715

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

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

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

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

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

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

749
  const SMetaEntry *pEntry = pParam->pEntry;
183,096✔
750

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

756
  if (META_TABLE_OP_INSERT == op) {
183,096✔
757
    code = tdbTbInsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
180,443✔
758
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
180,443✔
759
  } else if (META_TABLE_OP_UPDATA == op) {
2,653!
760
    code = tdbTbUpsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
2,678✔
761
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
2,678✔
762
  } else {
763
    code = TSDB_CODE_INVALID_PARA;
×
764
  }
765
  return code;
183,140✔
766
}
767

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

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

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

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

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

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

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

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

809
  if (tTagGet((const STag *)pEntry->ctbEntry.pTags, &tagVal)) {
236,261✔
810
    if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
234,266!
811
      pTagData = tagVal.pData;
45,506✔
812
      nTagData = (int32_t)tagVal.nData;
45,506✔
813
    } else {
814
      pTagData = &(tagVal.i64);
188,760✔
815
      nTagData = tDataTypes[pTagColumn->type].bytes;
188,760✔
816
    }
817
  } else {
818
    if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
2,061!
819
      nTagData = tDataTypes[pTagColumn->type].bytes;
1,596✔
820
    }
821
  }
822

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

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

835
static void metaFetchTagIdxKeyFree(STagIdxKey **ppTagIdxKey) {
236,341✔
836
  metaDestroyTagIdxKey(*ppTagIdxKey);
236,341✔
837
  *ppTagIdxKey = NULL;
236,321✔
838
}
236,321✔
839

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

843
  const SMetaEntry *pEntry = pParam->pEntry;
180,416✔
844
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
180,416✔
845

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

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

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

866
      if (!IS_IDX_ON(pTagColumn)) {
565,778✔
867
        continue;
385,818✔
868
      }
869

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

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

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

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

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

902
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
2,678✔
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++) {
9,312✔
916
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
6,642✔
917

918
      if (!IS_IDX_ON(pTagColumn)) {
6,642✔
919
        continue;
3,976✔
920
      }
921

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

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

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

940
      if (tagIdxKeyCmpr(pOldTagIdxKey, oldTagIdxKeySize, pNewTagIdxKey, newTagIdxKeySize)) {
2,666✔
941
        code = tdbTbDelete(pMeta->pTagIdx, pOldTagIdxKey, oldTagIdxKeySize, pMeta->txn);
1,171✔
942
        if (code) {
1,171!
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,171✔
950
        if (code) {
1,171!
951
          metaErr(TD_VID(pMeta->pVnode), code);
×
952
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
953
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
954
          return code;
×
955
        }
956
      }
957

958
      metaFetchTagIdxKeyFree(&pOldTagIdxKey);
2,666✔
959
      metaFetchTagIdxKeyFree(&pNewTagIdxKey);
2,666✔
960
    }
961
  }
962
  return code;
2,678✔
963
}
964

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

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

975
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
19,317✔
976
    pTagColumn = &pTagSchema->pSchema[0];
196✔
977
    code = metaDelJsonVarFromIdx(pMeta, pChild, pTagColumn);
196✔
978
    if (code) {
196!
979
      metaErr(TD_VID(pMeta->pVnode), code);
×
980
    }
981
  } else {
982
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
75,782✔
983
      pTagColumn = &pTagSchema->pSchema[i];
56,659✔
984
      if (!IS_IDX_ON(pTagColumn)) {
56,659✔
985
        continue;
30,446✔
986
      }
987

988
      STagIdxKey *pTagIdxKey = NULL;
26,213✔
989
      int32_t     nTagIdxKey;
990

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

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

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

1013
  const SMetaEntry *pEntry;
1014
  if (META_TABLE_OP_DELETE == op) {
217,921✔
1015
    pEntry = pParam->pOldEntry;
21,704✔
1016
  } else {
1017
    pEntry = pParam->pEntry;
196,217✔
1018
  }
1019

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

1024
  if (TSDB_CHILD_TABLE == pEntry->type || TSDB_VIRTUAL_CHILD_TABLE == pEntry->type) {
217,921✔
1025
    key.btime = pEntry->ctbEntry.btime;
199,685✔
1026
  } else if (TSDB_NORMAL_TABLE == pEntry->type || TSDB_VIRTUAL_NORMAL_TABLE == pEntry->type) {
18,236!
1027
    key.btime = pEntry->ntbEntry.btime;
18,236✔
1028
  } else {
1029
    return TSDB_CODE_INVALID_PARA;
×
1030
  }
1031

1032
  if (META_TABLE_OP_INSERT == op) {
217,921✔
1033
    code = tdbTbInsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
196,239✔
1034
  } else if (META_TABLE_OP_UPDATA == op) {
21,682!
1035
    code = tdbTbUpsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
×
1036
  } else if (META_TABLE_OP_DELETE == op) {
21,682!
1037
    code = tdbTbDelete(pMeta->pBtimeIdx, &key, sizeof(key), pMeta->txn);
21,702✔
1038
  } else {
1039
    code = TSDB_CODE_INVALID_PARA;
×
1040
  }
1041
  if (code) {
218,007!
1042
    metaErr(TD_VID(pMeta->pVnode), code);
×
1043
  }
1044
  return code;
218,001✔
1045
}
1046

1047
static int32_t metaBtimeIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
196,233✔
1048
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
196,233✔
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) {
21,703✔
1056
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_DELETE);
21,703✔
1057
}
1058

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

1063
  STtlUpdTtlCtx ctx = {
196,287✔
1064
      .uid = pEntry->uid,
196,287✔
1065
      .pTxn = pMeta->txn,
196,287✔
1066
  };
1067
  if (TSDB_CHILD_TABLE == pEntry->type) {
196,287✔
1068
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
180,448✔
1069
    ctx.changeTimeMs = pEntry->ctbEntry.btime;
180,448✔
1070
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
15,839!
1071
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
15,867✔
1072
    ctx.changeTimeMs = pEntry->ntbEntry.btime;
15,867✔
1073
  } else {
1074
    return TSDB_CODE_INVALID_PARA;
×
1075
  }
1076

1077
  int32_t ret = ttlMgrInsertTtl(pMeta->pTtlMgr, &ctx);
196,315✔
1078
  if (ret < 0) {
196,303!
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;
196,301✔
1082
}
1083

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

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

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

1093
  const SMetaEntry *pEntry = pParam->pEntry;
4,566✔
1094
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
4,566✔
1095

1096
  if ((pEntry->type == TSDB_CHILD_TABLE && pOldEntry->ctbEntry.ttlDays != pEntry->ctbEntry.ttlDays) ||
4,566✔
1097
      (pEntry->type == TSDB_NORMAL_TABLE && pOldEntry->ntbEntry.ttlDays != pEntry->ntbEntry.ttlDays)) {
4,546✔
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;
4,566✔
1110
}
1111

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

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

1121
  if (TSDB_CHILD_TABLE == pEntry->type) {
21,742✔
1122
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
19,338✔
1123
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
2,404!
1124
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
2,404✔
1125
  } else {
UNCOV
1126
    code = TSDB_CODE_INVALID_PARA;
×
1127
  }
1128

1129
  if (TSDB_CODE_SUCCESS == code) {
21,742!
1130
    int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
21,742✔
1131
    if (ret < 0) {
21,742!
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;
21,741✔
1137
}
1138

1139
static void metaTimeSeriesNotifyCheck(SMeta *pMeta) {
200,704✔
1140
#if defined(TD_ENTERPRISE)
1141
  int64_t nTimeSeries = metaGetTimeSeriesNum(pMeta, 0);
200,704✔
1142
  int64_t deltaTS = nTimeSeries - pMeta->pVnode->config.vndStats.numOfReportedTimeSeries;
200,746✔
1143
  if (deltaTS > tsTimeSeriesThreshold) {
200,746✔
1144
    if (0 == atomic_val_compare_exchange_8(&dmNotifyHdl.state, 1, 2)) {
129,060✔
1145
      if (tsem_post(&dmNotifyHdl.sem) != 0) {
129,070!
1146
        metaError("vgId:%d, failed to post semaphore, errno:%d", TD_VID(pMeta->pVnode), ERRNO);
×
1147
      }
1148
    }
1149
  }
1150
#endif
1151
}
200,804✔
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) {
29,984✔
1212
  int32_t code = TSDB_CODE_SUCCESS;
29,984✔
1213

1214
  SMetaTableOp ops[] = {
29,984✔
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++) {
179,577✔
1223
    SMetaTableOp          *op = &ops[i];
149,624✔
1224
    const SMetaHandleParam param = {
149,624✔
1225
        .pEntry = pEntry,
1226
    };
1227
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
149,624✔
1228
    if (TSDB_CODE_SUCCESS != code) {
149,568!
1229
      metaErr(TD_VID(pMeta->pVnode), code);
×
1230
      return code;
×
1231
    }
1232
  }
1233

1234
  return code;
29,953✔
1235
}
1236
static int32_t metaHandleSuperTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
29,438✔
1237
  int32_t code = TSDB_CODE_SUCCESS;
29,438✔
1238

1239
  metaWLock(pMeta);
29,438✔
1240
  code = metaHandleSuperTableCreateImpl(pMeta, pEntry);
30,050✔
1241
  metaULock(pMeta);
29,935✔
1242

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

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

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

1257
  SMetaTableOp ops[] = {
15,847✔
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++) {
110,921✔
1267
    SMetaTableOp *op = &ops[i];
95,075✔
1268

1269
    SMetaHandleParam param = {
95,075✔
1270
        .pEntry = pEntry,
1271
    };
1272

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

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

1285
  // update TDB
1286
  metaWLock(pMeta);
15,847✔
1287
  code = metaHandleNormalTableCreateImpl(pMeta, pEntry);
15,847✔
1288
  metaULock(pMeta);
15,846✔
1289

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

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

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

1311
  SMetaTableOp ops[] = {
180,416✔
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,441,782✔
1322
    SMetaTableOp *op = &ops[i];
1,261,734✔
1323

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

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

1336
  if (TSDB_CODE_SUCCESS == code) {
180,048!
1337
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
180,420✔
1338
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
180,422✔
1339
    if (ret < 0) {
180,488!
1340
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1341
    }
1342

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

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

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

1362
  // update TDB
1363
  metaWLock(pMeta);
180,441✔
1364
  code = metaHandleChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
180,485✔
1365
  metaULock(pMeta);
180,443✔
1366

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

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

1380
    if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
180,439!
1381
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, pEntry->ctbEntry.suid, NULL);
14,584✔
1382
      if (rc < 0) {
14,585!
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);
180,440✔
1392
  metaFetchEntryFree(&pSuperEntry);
180,475✔
1393
  return code;
180,502✔
1394
}
1395

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

1399
  SMetaTableOp ops[] = {
4✔
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++) {
24✔
1408
    SMetaTableOp *op = &ops[i];
20✔
1409

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

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

1421
  return code;
4✔
1422
}
1423

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

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

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

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

1445
  SMetaTableOp ops[] = {
4✔
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++) {
28✔
1455
    SMetaTableOp *op = &ops[i];
24✔
1456

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

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

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

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

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

1487
  return code;
4✔
1488
}
1489

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

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

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

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

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

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

1520
  SMetaTableOp ops[] = {
2,383✔
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++) {
14,298✔
1531
    SMetaTableOp *op = &ops[i];
11,915✔
1532
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
11,915✔
1533
    if (code) {
11,915!
1534
      const SMetaEntry *pEntry = pParam->pEntry;
×
1535
      metaErr(TD_VID(pMeta->pVnode), code);
×
1536
    }
1537
  }
1538

1539
  return code;
2,383✔
1540
}
1541

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

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

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

1558
  // do the drop
1559
  metaWLock(pMeta);
2,383✔
1560
  code = metaHandleNormalTableDropImpl(pMeta, &param);
2,383✔
1561
  metaULock(pMeta);
2,383✔
1562
  if (code) {
2,383!
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,383✔
1570
  pMeta->pVnode->config.vndStats.numOfNTimeSeries -= (pOldEntry->ntbEntry.schemaRow.nCols - 1);
2,383✔
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,383!
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,383✔
1589
  return code;
2,383✔
1590
}
1591

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

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

1599
  SMetaTableOp ops[] = {
19,317✔
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++) {
154,485✔
1610
    SMetaTableOp *op = &ops[i];
135,171✔
1611

1612
    if (op->table == META_ENTRY_TABLE && superDropped) {
135,171✔
1613
      continue;
5,464✔
1614
    }
1615

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

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

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

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

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

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

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

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

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

1683
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
19,318!
1684
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pChild->uid, pSuper->uid, NULL);
2,176✔
1685
    if (ret < 0) {
2,176!
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);
19,318✔
1703
  metaFetchEntryFree(&pSuper);
19,318✔
1704
  return code;
19,318✔
1705
}
1706

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

1710
  SMetaTableOp ops[] = {
3✔
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++) {
15✔
1720
    SMetaTableOp *op = &ops[i];
12✔
1721
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
12✔
1722
    if (code) {
12!
1723
      const SMetaEntry *pEntry = pParam->pEntry;
×
1724
      metaErr(TD_VID(pMeta->pVnode), code);
×
1725
    }
1726
  }
1727

1728
  return code;
3✔
1729
}
1730

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

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

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

1747
  // do the drop
1748
  metaWLock(pMeta);
3✔
1749
  code = metaHandleVirtualNormalTableDropImpl(pMeta, &param);
3✔
1750
  metaULock(pMeta);
3✔
1751
  if (code) {
3!
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--;
3✔
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) {
3!
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);
3✔
1777
  return code;
3✔
1778
}
1779

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

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

1787
  SMetaTableOp ops[] = {
3✔
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++) {
21✔
1797
    SMetaTableOp *op = &ops[i];
18✔
1798

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

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

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

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

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

1827
  return code;
3✔
1828
}
1829

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

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

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

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

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

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

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

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

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

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

1902
  for (;;) {
1903
    if (tdbTbcNext(cursor, &key, &keySize, NULL, NULL) < 0) {
40,519✔
1904
      break;
4,015✔
1905
    }
1906

1907
    if (((SCtbIdxKey *)key)->suid < suid) {
36,497✔
1908
      continue;
2,533✔
1909
    } else if (((SCtbIdxKey *)key)->suid > suid) {
33,964✔
1910
      break;
2,593✔
1911
    }
1912

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

1922
  tdbTbcClose(cursor);
6,608✔
1923
  tdbFreeClear(key);
6,609✔
1924
  return code;
6,608✔
1925
}
1926

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

1931
  SMetaTableOp ops[] = {
3,101✔
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++) {
15,502✔
1941
    SMetaTableOp *op = &ops[i];
12,403✔
1942

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

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

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

1960
  const SMetaEntry *pEntry = pParam->pEntry;
1,344✔
1961

1962
  SMetaTableOp ops[] = {
1,344✔
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++) {
6,720✔
1969
    SMetaTableOp *op = &ops[i];
5,376✔
1970
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
5,376✔
1971
    if (code) {
5,376!
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;
1,344✔
1982
}
1983

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

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

1989
  SMetaTableOp ops[] = {
6✔
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++) {
24✔
1995
    SMetaTableOp *op = &ops[i];
18✔
1996
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
18✔
1997
    if (code) {
18!
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;
6✔
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,222✔
2048
  int32_t code = TSDB_CODE_SUCCESS;
3,222✔
2049

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

2054
  SMetaTableOp ops[] = {
3,222✔
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++) {
19,332✔
2063
    SMetaTableOp *op = &ops[i];
16,110✔
2064
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
16,110✔
2065
    if (code) {
16,110!
2066
      metaErr(TD_VID(pMeta->pVnode), code);
×
2067
      return code;
×
2068
    }
2069
  }
2070

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

2075
  if (metaTbGroupCacheClear(pMeta, pSuperEntry->uid) < 0) {
3,222!
2076
    metaErr(TD_VID(pMeta->pVnode), code);
×
2077
  }
2078
  return code;
3,222✔
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) {
13,979✔
2087
  int32_t code = TSDB_CODE_SUCCESS;
13,979✔
2088

2089
  const SMetaEntry *pEntry = pParam->pEntry;
13,979✔
2090
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
13,979✔
2091

2092
  SMetaTableOp ops[] = {
13,979✔
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++) {
55,932✔
2099
    SMetaTableOp *op = &ops[i];
41,951✔
2100
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
41,951✔
2101
    if (code) {
41,946!
2102
      metaErr(TD_VID(pMeta->pVnode), code);
×
2103
      return code;
×
2104
    }
2105
  }
2106

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

2112
  return code;
13,987✔
2113
}
2114

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

2118
  SMetaEntry *pOldEntry = NULL;
13,955✔
2119

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

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

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

2144
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
13,979✔
2145
    STsdb  *pTsdb = pMeta->pVnode->pTsdb;
2,257✔
2146
    SArray *uids = NULL; /*taosArrayInit(8, sizeof(int64_t));
2,257✔
2147
     if (uids == NULL) {
2148
       metaErr(TD_VID(pMeta->pVnode), code);
2149
       metaFetchEntryFree(&pOldEntry);
2150
       return terrno;
2151
       }*/
2152
    if (deltaCol == 1) {
2,257✔
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) {
184!
2158
        metaErr(TD_VID(pMeta->pVnode), code);
×
2159
        metaFetchEntryFree(&pOldEntry);
×
2160
        return code;
×
2161
      }
2162
      if (pTsdb) {
184✔
2163
        TAOS_CHECK_RETURN(tsdbCacheNewSTableColumn(pTsdb, uids, cid, col_type));
183!
2164
      }
2165
    } else if (deltaCol == -1) {
2,075✔
2166
      int16_t cid = -1;
154✔
2167
      bool    hasPrimaryKey = false;
154✔
2168
      if (onCols >= 2) {
154✔
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,286!
2172
        if (pEntry->stbEntry.schemaRow.pSchema[i].colId != pOldEntry->stbEntry.schemaRow.pSchema[j].colId) {
1,223✔
2173
          cid = pOldEntry->stbEntry.schemaRow.pSchema[j].colId;
91✔
2174
          break;
91✔
2175
        }
2176
      }
2177

2178
      if (cid != -1) {
154✔
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);
2,260✔
2191

2192
    if (pTsdb) {
2,260✔
2193
      tsdbCacheInvalidateSchema(pTsdb, pEntry->uid, -1, pEntry->stbEntry.schemaRow.version);
2,258✔
2194
    }
2195
  }
2196
  if (updStat) {
13,980✔
2197
    int64_t ctbNum = 0;
5,849✔
2198
    int32_t ret = metaGetStbStats(pMeta->pVnode, pEntry->uid, &ctbNum, 0, 0);
5,849✔
2199
    if (ret < 0) {
5,856!
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);
5,856✔
2204
    if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
5,856✔
2205
  }
2206
  metaFetchEntryFree(&pOldEntry);
13,987✔
2207
  return code;
13,986✔
2208
}
2209

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

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

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

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

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

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

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

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

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

2261
  // handle update
2262
  SMetaHandleParam param = {
1,344✔
2263
      .pEntry = pEntry,
2264
      .pOldEntry = pOldEntry,
2265
  };
2266
  metaWLock(pMeta);
1,344✔
2267
  code = metaHandleNormalTableUpdateImpl(pMeta, &param);
1,344✔
2268
  metaULock(pMeta);
1,344✔
2269
  if (code) {
1,344!
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) &&
1,344✔
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;
1,344✔
2307
  pMeta->pVnode->config.vndStats.numOfNTimeSeries += deltaCol;
1,344✔
2308
  if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
1,344✔
2309
  metaFetchEntryFree(&pOldEntry);
1,344✔
2310
  return code;
1,344✔
2311
}
2312

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

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

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

2338
  metaTimeSeriesNotifyCheck(pMeta);
6✔
2339
  metaFetchEntryFree(&pOldEntry);
6✔
2340
  return code;
6✔
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) {
3,097✔
2384
  int32_t     code = TSDB_CODE_SUCCESS;
3,097✔
2385
  SArray     *childList = NULL;
3,097✔
2386
  SMetaEntry *pOldEntry = NULL;
3,097✔
2387

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

2394
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childList);
3,098✔
2395
  if (code) {
3,100!
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) {
3,100!
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++) {
8,564✔
2408
    SMetaEntry childEntry = {
10,926✔
2409
        .version = pEntry->version,
5,463✔
2410
        .uid = *(tb_uid_t *)taosArrayGet(childList, i),
5,463✔
2411
        .type = -TSDB_CHILD_TABLE,
2412
    };
2413

2414
    code = metaHandleChildTableDrop(pMeta, &childEntry, true);
5,463✔
2415
    if (code) {
5,462!
2416
      metaErr(TD_VID(pMeta->pVnode), code);
×
2417
    }
2418
  }
2419

2420
  // do drop super table
2421
  SMetaHandleParam param = {
3,102✔
2422
      .pEntry = pEntry,
2423
      .pOldEntry = pOldEntry,
2424
  };
2425
  metaWLock(pMeta);
3,102✔
2426
  code = metaHandleSuperTableDropImpl(pMeta, &param);
3,102✔
2427
  metaULock(pMeta);
3,102✔
2428
  if (code) {
3,102!
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);
3,102✔
2440
  metaFetchEntryFree(&pOldEntry);
3,102✔
2441
  return code;
3,102✔
2442
}
2443

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

2450
  if (NULL == pMeta || NULL == pEntry) {
264,033!
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) {
264,210✔
2456
    bool isExist = false;
244,906✔
2457
    if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) {
244,906✔
2458
      isExist = true;
18,545✔
2459
    }
2460

2461
    switch (type) {
244,866✔
2462
      case TSDB_SUPER_TABLE: {
43,980✔
2463
        if (isExist) {
43,980✔
2464
          code = metaHandleSuperTableUpdate(pMeta, pEntry);
13,970✔
2465
        } else {
2466
          code = metaHandleSuperTableCreate(pMeta, pEntry);
30,010✔
2467
        }
2468
        break;
44,050✔
2469
      }
2470
      case TSDB_CHILD_TABLE: {
183,638✔
2471
        if (isExist) {
183,638✔
2472
          code = metaHandleChildTableUpdate(pMeta, pEntry);
3,222✔
2473
        } else {
2474
          code = metaHandleChildTableCreate(pMeta, pEntry);
180,416✔
2475
        }
2476
        break;
183,708✔
2477
      }
2478
      case TSDB_NORMAL_TABLE: {
17,191✔
2479
        if (isExist) {
17,191✔
2480
          code = metaHandleNormalTableUpdate(pMeta, pEntry);
1,344✔
2481
        } else {
2482
          code = metaHandleNormalTableCreate(pMeta, pEntry);
15,847✔
2483
        }
2484
        break;
17,191✔
2485
      }
2486
      case TSDB_VIRTUAL_NORMAL_TABLE: {
10✔
2487
        if (isExist) {
10✔
2488
          code = metaHandleVirtualNormalTableUpdate(pMeta, pEntry);
6✔
2489
        } else {
2490
          code = metaHandleVirtualNormalTableCreate(pMeta, pEntry);
4✔
2491
        }
2492
        break;
10✔
2493
      }
2494
      case TSDB_VIRTUAL_CHILD_TABLE: {
4✔
2495
        if (isExist) {
4!
2496
          code = metaHandleVirtualChildTableUpdate(pMeta, pEntry);
×
2497
        } else {
2498
          code = metaHandleVirtualChildTableCreate(pMeta, pEntry);
4✔
2499
        }
2500
        break;
4✔
2501
      }
2502
      default: {
43✔
2503
        code = TSDB_CODE_INVALID_PARA;
43✔
2504
        break;
43✔
2505
      }
2506
    }
2507
  } else {
2508
    switch (type) {
19,304!
2509
      case TSDB_SUPER_TABLE: {
3,098✔
2510
        code = metaHandleSuperTableDrop(pMeta, pEntry);
3,098✔
2511
        break;
3,102✔
2512
      }
2513
      case TSDB_CHILD_TABLE: {
13,854✔
2514
        code = metaHandleChildTableDrop(pMeta, pEntry, false);
13,854✔
2515
        break;
13,854✔
2516
      }
2517
      case TSDB_NORMAL_TABLE: {
2,383✔
2518
        code = metaHandleNormalTableDrop(pMeta, pEntry);
2,383✔
2519
        break;
2,383✔
2520
      }
2521
      case TSDB_VIRTUAL_NORMAL_TABLE: {
3✔
2522
        code = metaHandleVirtualNormalTableDrop(pMeta, pEntry);
3✔
2523
        break;
3✔
2524
      }
2525
      case TSDB_VIRTUAL_CHILD_TABLE: {
3✔
2526
        code = metaHandleVirtualChildTableDrop(pMeta, pEntry, false);
3✔
2527
        break;
3✔
2528
      }
2529
      default: {
×
2530
        code = TSDB_CODE_INVALID_PARA;
×
2531
        break;
×
2532
      }
2533
    }
2534
  }
2535

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

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