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

taosdata / TDengine / #4556

21 Jul 2025 05:44AM UTC coverage: 56.496% (+2.0%) from 54.508%
#4556

push

travis-ci

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

skip tsim cases

138681 of 315239 branches covered (43.99%)

Branch coverage included in aggregate %.

209114 of 300373 relevant lines covered (69.62%)

17248409.04 hits per line

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

58.79
/source/dnode/vnode/src/meta/metaEntry2.c
1
/*
2
 * Copyright (c) 2023 Hongze Cheng <hzcheng@umich.edu>.
3
 * All rights reserved.
4
 *
5
 * This code is the intellectual property of Hongze Cheng.
6
 * Any reproduction or distribution, in whole or in part,
7
 * without the express written permission of Hongze Cheng is
8
 * strictly prohibited.
9
 */
10

11
#include "meta.h"
12
#include "vnodeInt.h"
13

14
extern SDmNotifyHandle dmNotifyHdl;
15

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

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

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

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

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

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

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

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

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

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

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

93
  // decode entry
94
  SDecoder   decoder = {0};
635,222✔
95
  SMetaEntry entry = {0};
635,222✔
96

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

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

116
  tdbFreeClear(value);
635,138✔
117
  tDecoderClear(&decoder);
635,137✔
118
  return code;
635,244✔
119
}
120

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

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

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

142
void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); }
635,248✔
143

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

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

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

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

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

181
  // put to tdb
182
  if (META_TABLE_OP_INSERT == op) {
374,341✔
183
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
315,096✔
184
  } else if (META_TABLE_OP_UPDATA == op) {
59,245✔
185
    code = tdbTbUpsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
39,575✔
186
  } else if (META_TABLE_OP_DELETE == op) {
19,670!
187
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
19,670✔
188
  } else {
189
    code = TSDB_CODE_INVALID_PARA;
×
190
  }
191
  if (TSDB_CODE_SUCCESS != code) {
374,487!
192
    metaErr(vgId, code);
×
193
  }
194
  taosMemoryFree(value);
374,431!
195
  return code;
374,451✔
196
}
197

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

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

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

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

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

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

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

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

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

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

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

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

282
  if (pOldColumn && pNewColumn) {
145,427✔
283
    if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
142,858✔
284
      return TSDB_CODE_SUCCESS;
4,206✔
285
    } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) {
138,652!
286
      action = DROP_INDEX;
2,237✔
287
    } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
136,415!
288
      action = ADD_INDEX;
964✔
289
    } else {
290
      return TSDB_CODE_SUCCESS;
135,451✔
291
    }
292
  } else if (pOldColumn) {
2,569✔
293
    if (IS_IDX_ON(pOldColumn)) {
1,027✔
294
      action = DROP_INDEX;
44✔
295
    } else {
296
      return TSDB_CODE_SUCCESS;
983✔
297
    }
298
  } else {
299
    if (IS_IDX_ON(pNewColumn)) {
1,542!
300
      action = ADD_INDEX;
×
301
    } else {
302
      return TSDB_CODE_SUCCESS;
1,542✔
303
    }
304
  }
305

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

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

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

327
    STagIdxKey *pTagIdxKey = NULL;
5,477✔
328
    int32_t     tagIdxKeySize = 0;
5,477✔
329

330
    if (action == ADD_INDEX) {
5,477✔
331
      code = metaFetchTagIdxKey(pMeta, pChildEntry, pNewColumn, &pTagIdxKey, &tagIdxKeySize);
1,936✔
332
      if (code) {
1,936!
333
        metaErr(TD_VID(pMeta->pVnode), code);
×
334
        taosArrayDestroy(childTables);
×
335
        metaFetchEntryFree(&pChildEntry);
×
336
        return code;
×
337
      }
338

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

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

366
    metaFetchTagIdxKeyFree(&pTagIdxKey);
5,477✔
367
    metaFetchEntryFree(&pChildEntry);
5,477✔
368
  }
369

370
  taosArrayDestroy(childTables);
3,244✔
371
  return code;
3,246✔
372
}
373

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

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

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

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

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

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

411
    SMetaHandleParam param = {.pEntry = pChildEntry};
15✔
412

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

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

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

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

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

460
  int32_t iOld = 0, iNew = 0;
7,512✔
461
  for (; iOld < pOldTagSchema->nCols && iNew < pNewTagSchema->nCols;) {
151,139✔
462
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
143,629✔
463
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
143,629✔
464

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

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

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

489
      iNew++;
×
490
    }
491
  }
492

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

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

511
  return code;
7,516✔
512
}
513

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

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

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

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

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

550
      iNew++;
×
551
    }
552
  }
553

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

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

572
  return code;
18✔
573
}
574

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

578
  const SMetaEntry *pEntry = pParam->pEntry;
71,710✔
579
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
71,710✔
580

581
  if (NULL == pOldEntry) {
71,710✔
582
    return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
46,074✔
583
  }
584

585
  if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
25,636✔
586
    // check row schema
587
    if (pOldEntry->ntbEntry.schemaRow.version != pEntry->ntbEntry.schemaRow.version) {
5,473!
588
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
5,540✔
589
    }
590
  } else if (pEntry->type == TSDB_SUPER_TABLE) {
20,163!
591
    // check row schema
592
    if (pOldEntry->stbEntry.schemaRow.version != pEntry->stbEntry.schemaRow.version) {
20,172✔
593
      if (TABLE_IS_VIRTUAL(pEntry->flags)) {
12,643✔
594
        return metaUpdateSuperTableRowSchema(pMeta, pParam);
18✔
595
      } else {
596
        return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
12,625✔
597
      }
598
    }
599

600
    // check tag schema
601
    code = metaUpdateSuperTableTagSchema(pMeta, pParam);
7,529✔
602
    if (code) {
7,517!
603
      metaErr(TD_VID(pMeta->pVnode), code);
×
604
      return code;
×
605
    }
606

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

611
  return TSDB_CODE_SUCCESS;
7,569✔
612
}
613

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

619
// Uid Index
620
static void metaBuildEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) {
354,606✔
621
  pInfo->uid = pEntry->uid;
354,606✔
622
  pInfo->version = pEntry->version;
354,606✔
623
  if (pEntry->type == TSDB_SUPER_TABLE) {
354,606✔
624
    pInfo->suid = pEntry->uid;
50,205✔
625
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
50,205✔
626
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
304,401✔
627
    pInfo->suid = pEntry->ctbEntry.suid;
282,790✔
628
    pInfo->skmVer = 0;
282,790✔
629
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
21,611!
630
    pInfo->suid = 0;
21,611✔
631
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
21,611✔
632
  }
633
}
354,606✔
634

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

639
  const SMetaEntry *pEntry = pParam->pEntry;
354,582✔
640

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

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

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

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

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

674
  const SMetaEntry *pEntry = pParam->pOldEntry;
22,388✔
675

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

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

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

691
  const SMetaEntry *pEntry = pParam->pEntry;
315,096✔
692

693
  if (META_TABLE_OP_INSERT == op) {
315,096!
694
    code = tdbTbInsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
315,165✔
695
                       pMeta->txn);
696
  } else if (META_TABLE_OP_UPDATA == op) {
×
697
    code = tdbTbUpsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
×
698
                       pMeta->txn);
699
  } else {
700
    code = TSDB_CODE_INVALID_PARA;
×
701
  }
702
  if (code) {
315,244!
703
    metaErr(TD_VID(pMeta->pVnode), code);
×
704
  }
705
  return code;
315,232✔
706
}
707

708
static int32_t metaNameIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
315,130✔
709
  int32_t code = TSDB_CODE_SUCCESS;
315,130✔
710
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
315,130✔
711
}
712

713
static int32_t metaNameIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
714
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
×
715
}
716

717
static int32_t metaNameIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
22,390✔
718
  int32_t code = TSDB_CODE_SUCCESS;
22,390✔
719

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

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

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

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

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

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

753
  const SMetaEntry *pEntry = pParam->pEntry;
282,206✔
754

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

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

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

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

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

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

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

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

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

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

813
  if (tTagGet((const STag *)pEntry->ctbEntry.pTags, &tagVal)) {
316,427✔
814
    if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
304,170!
815
      pTagData = tagVal.pData;
38,632✔
816
      nTagData = (int32_t)tagVal.nData;
38,632✔
817
    } else {
818
      pTagData = &(tagVal.i64);
265,538✔
819
      nTagData = tDataTypes[pTagColumn->type].bytes;
265,538✔
820
    }
821
  } else {
822
    if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
12,297!
823
      nTagData = tDataTypes[pTagColumn->type].bytes;
11,760✔
824
    }
825
  }
826

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

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

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

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

847
  const SMetaEntry *pEntry = pParam->pEntry;
269,121✔
848
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
269,121✔
849

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

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

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

870
      if (!IS_IDX_ON(pTagColumn)) {
804,999✔
871
        continue;
536,461✔
872
      }
873

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

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

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

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

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

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

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

922
      if (!IS_IDX_ON(pTagColumn)) {
18,126✔
923
        continue;
5,070✔
924
      }
925

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

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

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

944
      if (tagIdxKeyCmpr(pOldTagIdxKey, oldTagIdxKeySize, pNewTagIdxKey, newTagIdxKeySize)) {
13,056✔
945
        code = tdbTbDelete(pMeta->pTagIdx, pOldTagIdxKey, oldTagIdxKeySize, pMeta->txn);
11,256✔
946
        if (code) {
11,256!
947
          metaErr(TD_VID(pMeta->pVnode), code);
×
948
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
949
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
950
          return code;
×
951
        }
952

953
        code = tdbTbInsert(pMeta->pTagIdx, pNewTagIdxKey, newTagIdxKeySize, NULL, 0, pMeta->txn);
11,256✔
954
        if (code) {
11,256!
955
          metaErr(TD_VID(pMeta->pVnode), code);
×
956
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
957
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
958
          return code;
×
959
        }
960
      }
961

962
      metaFetchTagIdxKeyFree(&pOldTagIdxKey);
13,056✔
963
      metaFetchTagIdxKeyFree(&pNewTagIdxKey);
13,056✔
964
    }
965
  }
966
  return code;
13,064✔
967
}
968

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

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

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

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

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

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

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

1017
  const SMetaEntry *pEntry;
1018
  if (META_TABLE_OP_DELETE == op) {
304,142✔
1019
    pEntry = pParam->pOldEntry;
19,023✔
1020
  } else {
1021
    pEntry = pParam->pEntry;
285,119✔
1022
  }
1023

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

1028
  if (TSDB_CHILD_TABLE == pEntry->type || TSDB_VIRTUAL_CHILD_TABLE == pEntry->type) {
304,142✔
1029
    key.btime = pEntry->ctbEntry.btime;
285,471✔
1030
  } else if (TSDB_NORMAL_TABLE == pEntry->type || TSDB_VIRTUAL_NORMAL_TABLE == pEntry->type) {
18,671!
1031
    key.btime = pEntry->ntbEntry.btime;
18,671✔
1032
  } else {
1033
    return TSDB_CODE_INVALID_PARA;
×
1034
  }
1035

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

1051
static int32_t metaBtimeIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
285,123✔
1052
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
285,123✔
1053
}
1054

1055
static int32_t metaBtimeIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
1056
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
×
1057
}
1058

1059
static int32_t metaBtimeIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
19,023✔
1060
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_DELETE);
19,023✔
1061
}
1062

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

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

1081
  int32_t ret = ttlMgrInsertTtl(pMeta->pTtlMgr, &ctx);
284,888✔
1082
  if (ret < 0) {
284,880!
1083
    metaError("vgId:%d, failed to insert ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid, tstrerror(ret));
×
1084
  }
1085
  return TSDB_CODE_SUCCESS;
284,875✔
1086
}
1087

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

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

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

1097
  const SMetaEntry *pEntry = pParam->pEntry;
19,056✔
1098
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
19,056✔
1099

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

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

1113
  return TSDB_CODE_SUCCESS;
19,056✔
1114
}
1115

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

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

1125
  if (TSDB_CHILD_TABLE == pEntry->type) {
19,003✔
1126
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
16,343✔
1127
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
2,660✔
1128
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
2,624✔
1129
  } else {
1130
    code = TSDB_CODE_INVALID_PARA;
36✔
1131
  }
1132

1133
  if (TSDB_CODE_SUCCESS == code) {
19,003✔
1134
    int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
18,967✔
1135
    if (ret < 0) {
18,966!
1136
      metaError("vgId:%d, failed to delete ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid,
×
1137
                tstrerror(ret));
1138
    }
1139
  }
1140
  return code;
19,000✔
1141
}
1142

1143
static void metaTimeSeriesNotifyCheck(SMeta *pMeta) {
297,967✔
1144
#if defined(TD_ENTERPRISE)
1145
  int64_t nTimeSeries = metaGetTimeSeriesNum(pMeta, 0);
297,967✔
1146
  int64_t deltaTS = nTimeSeries - pMeta->pVnode->config.vndStats.numOfReportedTimeSeries;
298,002✔
1147
  if (deltaTS > tsTimeSeriesThreshold) {
298,002✔
1148
    if (0 == atomic_val_compare_exchange_8(&dmNotifyHdl.state, 1, 2)) {
210,075✔
1149
      if (tsem_post(&dmNotifyHdl.sem) != 0) {
210,062!
1150
        metaError("vgId:%d, failed to post semaphore, errno:%d", TD_VID(pMeta->pVnode), ERRNO);
×
1151
      }
1152
    }
1153
  }
1154
#endif
1155
}
298,032✔
1156

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

1215
static int32_t metaHandleSuperTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
30,116✔
1216
  int32_t code = TSDB_CODE_SUCCESS;
30,116✔
1217

1218
  SMetaTableOp ops[] = {
30,116✔
1219
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1220
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: here should be insert
1221
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1222
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1223
      {META_SUID_IDX, META_TABLE_OP_INSERT},      //
1224
  };
1225

1226
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
180,276✔
1227
    SMetaTableOp          *op = &ops[i];
150,251✔
1228
    const SMetaHandleParam param = {
150,251✔
1229
        .pEntry = pEntry,
1230
    };
1231
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
150,251✔
1232
    if (TSDB_CODE_SUCCESS != code) {
150,154!
1233
      metaErr(TD_VID(pMeta->pVnode), code);
×
1234
      return code;
×
1235
    }
1236
  }
1237

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

1243
  metaWLock(pMeta);
29,547✔
1244
  code = metaHandleSuperTableCreateImpl(pMeta, pEntry);
30,164✔
1245
  metaULock(pMeta);
30,052✔
1246

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

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

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

1261
  SMetaTableOp ops[] = {
15,858✔
1262
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1263
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: need to be insert
1264
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1265
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1266
      {META_BTIME_IDX, META_TABLE_OP_INSERT},     //
1267
      {META_TTL_IDX, META_TABLE_OP_INSERT},       //
1268
  };
1269

1270
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
111,005✔
1271
    SMetaTableOp *op = &ops[i];
95,147✔
1272

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

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

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

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

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

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

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

1315
  SMetaTableOp ops[] = {
269,038✔
1316
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},  //
1317
      {META_UID_IDX, META_TABLE_OP_INSERT},      //
1318
      {META_NAME_IDX, META_TABLE_OP_INSERT},     //
1319
      {META_CHILD_IDX, META_TABLE_OP_INSERT},    //
1320
      {META_TAG_IDX, META_TABLE_OP_INSERT},      //
1321
      {META_BTIME_IDX, META_TABLE_OP_INSERT},    //
1322
      {META_TTL_IDX, META_TABLE_OP_INSERT},      //
1323
  };
1324

1325
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
2,150,786✔
1326
    SMetaTableOp *op = &ops[i];
1,882,088✔
1327

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

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

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

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

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

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

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

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

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

1384
    if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
269,019!
1385
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, pEntry->ctbEntry.suid, NULL);
3,062✔
1386
      if (rc < 0) {
3,062!
1387
        metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
1388
                  tstrerror(rc));
1389
      }
1390
    }
1391

1392
  } else {
1393
    metaErr(TD_VID(pMeta->pVnode), code);
×
1394
  }
1395
  metaTimeSeriesNotifyCheck(pMeta);
269,019✔
1396
  metaFetchEntryFree(&pSuperEntry);
269,054✔
1397
  return code;
269,064✔
1398
}
1399

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

1403
  SMetaTableOp ops[] = {
159✔
1404
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1405
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: need to be insert
1406
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1407
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1408
      {META_BTIME_IDX, META_TABLE_OP_INSERT},     //
1409
  };
1410

1411
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
954✔
1412
    SMetaTableOp *op = &ops[i];
795✔
1413

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

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

1425
  return code;
159✔
1426
}
1427

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

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

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

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

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

1458
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
861✔
1459
    SMetaTableOp *op = &ops[i];
738✔
1460

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

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

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

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

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

1491
  return code;
123✔
1492
}
1493

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

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

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

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

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

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

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

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

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

1543
  return code;
2,608✔
1544
}
1545

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

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

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

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

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

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

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

1592
  metaFetchEntryFree(&pOldEntry);
2,608✔
1593
  return code;
2,608✔
1594
}
1595

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

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

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

1613
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
130,839✔
1614
    SMetaTableOp *op = &ops[i];
114,522✔
1615

1616
    if (op->table == META_ENTRY_TABLE && superDropped) {
114,522✔
1617
      continue;
2,719✔
1618
    }
1619

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

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

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

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

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

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

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

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

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

1687
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
16,328!
1688
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pChild->uid, pSuper->uid, NULL);
75✔
1689
    if (ret < 0) {
75!
1690
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1691
    }
1692
  }
1693

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

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

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

1714
  SMetaTableOp ops[] = {
46✔
1715
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1716
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1717
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1718
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1719

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

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

1732
  return code;
46✔
1733
}
1734

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

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

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

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

1761
  // update other stuff
1762
  pMeta->pVnode->config.vndStats.numOfVTables--;
46✔
1763

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

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

1780
  metaFetchEntryFree(&pOldEntry);
46✔
1781
  return code;
46✔
1782
}
1783

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

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

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

1800
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
35✔
1801
    SMetaTableOp *op = &ops[i];
30✔
1802

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

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

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

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

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

1831
  return code;
5✔
1832
}
1833

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

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

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

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

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

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

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

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

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

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

1906
  for (;;) {
1907
    if (tdbTbcNext(cursor, &key, &keySize, NULL, NULL) < 0) {
15,828✔
1908
      break;
3,202✔
1909
    }
1910

1911
    if (((SCtbIdxKey *)key)->suid < suid) {
12,623✔
1912
      continue;
1,008✔
1913
    } else if (((SCtbIdxKey *)key)->suid > suid) {
11,615✔
1914
      break;
3,423✔
1915
    }
1916

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

1926
  tdbTbcClose(cursor);
6,625✔
1927
  tdbFreeClear(key);
6,623✔
1928
  return code;
6,625✔
1929
}
1930

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

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

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

1944
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
16,833✔
1945
    SMetaTableOp *op = &ops[i];
13,467✔
1946

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2093
  const SMetaEntry *pEntry = pParam->pEntry;
20,156✔
2094
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
20,156✔
2095

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

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

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

2116
  return code;
20,174✔
2117
}
2118

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

2122
  SMetaEntry *pOldEntry = NULL;
20,147✔
2123

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

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

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

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

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

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

2196
    if (pTsdb) {
1,964!
2197
      tsdbCacheInvalidateSchema(pTsdb, pEntry->uid, -1, pEntry->stbEntry.schemaRow.version);
1,964✔
2198
    }
2199
  }
2200
  if (updStat) {
20,163✔
2201
    int64_t ctbNum = 0;
10,996✔
2202
    int32_t ret = metaGetStbStats(pMeta->pVnode, pEntry->uid, &ctbNum, 0, 0);
10,996✔
2203
    if (ret < 0) {
11,004!
2204
      metaError("vgId:%d, failed to get stb stats:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
×
2205
                pEntry->uid, tstrerror(ret));
2206
    }
2207
    pMeta->pVnode->config.vndStats.numOfTimeSeries += (ctbNum * deltaCol);
10,999✔
2208
    if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
10,999✔
2209
  }
2210
  metaFetchEntryFree(&pOldEntry);
20,162✔
2211
  return code;
20,160✔
2212
}
2213

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

2217
  SMetaEntry *pOldEntry = NULL;
13,782✔
2218
  SMetaEntry *pSuperEntry = NULL;
13,782✔
2219

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

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

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

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

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

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

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

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

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

2294
      if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
2295
        int16_t cid = pColumn->colId;
2296

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

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

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

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

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

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

2350
  SMetaEntry *pOldEntry = NULL;
10✔
2351
  SMetaEntry *pSuperEntry = NULL;
10✔
2352

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

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

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

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

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

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

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

2398
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childList);
3,353✔
2399
  if (code) {
3,364!
2400
    metaErr(TD_VID(pMeta->pVnode), code);
×
2401
    metaFetchEntryFree(&pOldEntry);
×
2402
    return code;
×
2403
  }
2404

2405
  if (pMeta->pVnode->pTsdb && tsdbCacheDropSubTables(pMeta->pVnode->pTsdb, childList, pEntry->uid) < 0) {
3,364✔
2406
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
4!
2407
              pEntry->uid, tstrerror(terrno));
2408
  }
2409

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

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

2424
  // do drop super table
2425
  SMetaHandleParam param = {
3,365✔
2426
      .pEntry = pEntry,
2427
      .pOldEntry = pOldEntry,
2428
  };
2429
  metaWLock(pMeta);
3,365✔
2430
  code = metaHandleSuperTableDropImpl(pMeta, &param);
3,367✔
2431
  metaULock(pMeta);
3,367✔
2432
  if (code) {
3,367!
2433
    metaErr(TD_VID(pMeta->pVnode), code);
×
2434
    taosArrayDestroy(childList);
×
2435
    metaFetchEntryFree(&pOldEntry);
×
2436
    return code;
×
2437
  }
2438

2439
  // do other stuff
2440
  // metaUpdTimeSeriesNum(pMeta);
2441

2442
  // free resource and return
2443
  taosArrayDestroy(childList);
3,367✔
2444
  metaFetchEntryFree(&pOldEntry);
3,367✔
2445
  return code;
3,367✔
2446
}
2447

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

2454
  if (NULL == pMeta || NULL == pEntry) {
374,412!
2455
    metaError("%s failed at %s:%d since invalid parameter", __func__, __FILE__, __LINE__);
×
2456
    return TSDB_CODE_INVALID_PARA;
×
2457
  }
2458

2459
  if (pEntry->type > 0) {
374,568✔
2460
    bool isExist = false;
354,891✔
2461
    if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) {
354,891✔
2462
      isExist = true;
39,557✔
2463
    }
2464

2465
    switch (type) {
354,909✔
2466
      case TSDB_SUPER_TABLE: {
50,282✔
2467
        if (isExist) {
50,282✔
2468
          code = metaHandleSuperTableUpdate(pMeta, pEntry);
20,163✔
2469
        } else {
2470
          code = metaHandleSuperTableCreate(pMeta, pEntry);
30,119✔
2471
        }
2472
        break;
50,328✔
2473
      }
2474
      case TSDB_CHILD_TABLE: {
282,823✔
2475
        if (isExist) {
282,823✔
2476
          code = metaHandleChildTableUpdate(pMeta, pEntry);
13,782✔
2477
        } else {
2478
          code = metaHandleChildTableCreate(pMeta, pEntry);
269,041✔
2479
        }
2480
        break;
282,834✔
2481
      }
2482
      case TSDB_NORMAL_TABLE: {
21,132✔
2483
        if (isExist) {
21,132✔
2484
          code = metaHandleNormalTableUpdate(pMeta, pEntry);
5,274✔
2485
        } else {
2486
          code = metaHandleNormalTableCreate(pMeta, pEntry);
15,858✔
2487
        }
2488
        break;
21,132✔
2489
      }
2490
      case TSDB_VIRTUAL_NORMAL_TABLE: {
479✔
2491
        if (isExist) {
479✔
2492
          code = metaHandleVirtualNormalTableUpdate(pMeta, pEntry);
320✔
2493
        } else {
2494
          code = metaHandleVirtualNormalTableCreate(pMeta, pEntry);
159✔
2495
        }
2496
        break;
479✔
2497
      }
2498
      case TSDB_VIRTUAL_CHILD_TABLE: {
133✔
2499
        if (isExist) {
133✔
2500
          code = metaHandleVirtualChildTableUpdate(pMeta, pEntry);
10✔
2501
        } else {
2502
          code = metaHandleVirtualChildTableCreate(pMeta, pEntry);
123✔
2503
        }
2504
        break;
133✔
2505
      }
2506
      default: {
60✔
2507
        code = TSDB_CODE_INVALID_PARA;
60✔
2508
        break;
60✔
2509
      }
2510
    }
2511
  } else {
2512
    switch (type) {
19,677✔
2513
      case TSDB_SUPER_TABLE: {
3,342✔
2514
        code = metaHandleSuperTableDrop(pMeta, pEntry);
3,342✔
2515
        break;
3,367✔
2516
      }
2517
      case TSDB_CHILD_TABLE: {
13,644✔
2518
        code = metaHandleChildTableDrop(pMeta, pEntry, false);
13,644✔
2519
        break;
13,644✔
2520
      }
2521
      case TSDB_NORMAL_TABLE: {
2,608✔
2522
        code = metaHandleNormalTableDrop(pMeta, pEntry);
2,608✔
2523
        break;
2,608✔
2524
      }
2525
      case TSDB_VIRTUAL_NORMAL_TABLE: {
46✔
2526
        code = metaHandleVirtualNormalTableDrop(pMeta, pEntry);
46✔
2527
        break;
46✔
2528
      }
2529
      case TSDB_VIRTUAL_CHILD_TABLE: {
5✔
2530
        code = metaHandleVirtualChildTableDrop(pMeta, pEntry, false);
5✔
2531
        break;
5✔
2532
      }
2533
      default: {
32✔
2534
        code = TSDB_CODE_INVALID_PARA;
32✔
2535
        break;
32✔
2536
      }
2537
    }
2538
  }
2539

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

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

© 2026 Coveralls, Inc