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

taosdata / TDengine / #3819

01 Apr 2025 09:27AM UTC coverage: 34.076% (+0.01%) from 34.065%
#3819

push

travis-ci

happyguoxy
test:alter gcda dir

148544 of 599532 branches covered (24.78%)

Branch coverage included in aggregate %.

222541 of 489451 relevant lines covered (45.47%)

763329.1 hits per line

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

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

11
#include "meta.h"
12

13
extern SDmNotifyHandle dmNotifyHdl;
14

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

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

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

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

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

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

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

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

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

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

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

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

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

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

115
  tdbFreeClear(value);
25,007✔
116
  tDecoderClear(&decoder);
25,009✔
117
  return code;
25,010✔
118
}
119

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

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

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

141
void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); }
25,010✔
142

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

281
  if (pOldColumn && pNewColumn) {
894✔
282
    if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
878✔
283
      return TSDB_CODE_SUCCESS;
394✔
284
    } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) {
484!
285
      action = DROP_INDEX;
28✔
286
    } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
456!
287
      action = ADD_INDEX;
28✔
288
    } else {
289
      return TSDB_CODE_SUCCESS;
428✔
290
    }
291
  } else if (pOldColumn) {
16✔
292
    if (IS_IDX_ON(pOldColumn)) {
6!
293
      action = DROP_INDEX;
×
294
    } else {
295
      return TSDB_CODE_SUCCESS;
6✔
296
    }
297
  } else {
298
    if (IS_IDX_ON(pNewColumn)) {
10!
299
      action = ADD_INDEX;
×
300
    } else {
301
      return TSDB_CODE_SUCCESS;
10✔
302
    }
303
  }
304

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

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

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

326
    STagIdxKey *pTagIdxKey = NULL;
20,290✔
327
    int32_t     tagIdxKeySize = 0;
20,290✔
328

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

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

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

365
    metaFetchTagIdxKeyFree(&pTagIdxKey);
20,290✔
366
    metaFetchEntryFree(&pChildEntry);
20,290✔
367
  }
368

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

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

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

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

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

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

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

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

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

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

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

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

453
}
454

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

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

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

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

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

491
      iNew++;
×
492
    }
493
  }
494

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

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

513
  return code;
82✔
514
}
515

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

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

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

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

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

552
      iNew++;
×
553
    }
554
  }
555

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

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

574
  return code;
×
575
}
576

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

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

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

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

601
    }
602

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

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

614
  return TSDB_CODE_SUCCESS;
82✔
615
}
616

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

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

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

642
  const SMetaEntry *pEntry = pParam->pEntry;
3,885✔
643

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

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

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

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

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

677
  const SMetaEntry *pEntry = pParam->pOldEntry;
547✔
678

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

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

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

694
  const SMetaEntry *pEntry = pParam->pEntry;
3,758✔
695

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

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

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

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

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

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

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

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

753
  const SMetaEntry *pEntry = pParam->pEntry;
3,391✔
754

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

847
  const SMetaEntry *pEntry = pParam->pEntry;
3,391✔
848
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
3,391✔
849

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

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

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

870
      if (!IS_IDX_ON(pTagColumn)) {
21,271✔
871
        continue;
17,879✔
872
      }
873

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

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

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

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

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

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

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

922
      if (!IS_IDX_ON(pTagColumn)) {
8✔
923
        continue;
6✔
924
      }
925

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

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

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

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

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

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

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

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

992
      STagIdxKey *pTagIdxKey = NULL;
7,201✔
993
      int32_t     nTagIdxKey;
994

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

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

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

1017
  const SMetaEntry *pEntry;
1018
  if (META_TABLE_OP_DELETE == op) {
3,975✔
1019
    pEntry = pParam->pOldEntry;
537✔
1020
  } else {
1021
    pEntry = pParam->pEntry;
3,438✔
1022
  }
1023

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

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

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

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

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

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

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

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

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

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

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

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

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

1113
  return TSDB_CODE_SUCCESS;
21✔
1114
}
1115

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1315
  SMetaTableOp ops[] = {
3,388✔
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++) {
27,112✔
1326
    SMetaTableOp *op = &ops[i];
23,724✔
1327

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1425
  return code;
×
1426
}
1427

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

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

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

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

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

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

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

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

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

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

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

1490
  return code;
×
1491
}
1492

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

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

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

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

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

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

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

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

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

1542
  return code;
18✔
1543
}
1544

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

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

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

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

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

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

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

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

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

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

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

1612
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
4,152✔
1613
    SMetaTableOp *op = &ops[i];
3,633✔
1614

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1731
  return code;
×
1732
}
1733

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1830
  return code;
×
1831
}
1832

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

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

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

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

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

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

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

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

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

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

1905
  for (;;) {
1906
    if (tdbTbcNext(cursor, &key, &keySize, NULL, NULL) < 0) {
20,359✔
1907
      break;
64✔
1908
    }
1909

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2115
  return code;
108✔
2116
}
2117

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

2121
  SMetaEntry *pOldEntry = NULL;
108✔
2122

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2450
  if (pEntry->type > 0) {
4,432✔
2451
    bool isExist = false;
3,888✔
2452
    if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) {
3,888✔
2453
      isExist = true;
129✔
2454
    }
2455

2456
    switch (type) {
3,889!
2457
      case TSDB_SUPER_TABLE: {
428✔
2458
        if (isExist) {
428✔
2459
          code = metaHandleSuperTableUpdate(pMeta, pEntry);
108✔
2460
        } else {
2461
          code = metaHandleSuperTableCreate(pMeta, pEntry);
320✔
2462
        }
2463
        break;
428✔
2464
      }
2465
      case TSDB_CHILD_TABLE: {
3,394✔
2466
        if (isExist) {
3,394✔
2467
          code = metaHandleChildTableUpdate(pMeta, pEntry);
2✔
2468
        } else {
2469
          code = metaHandleChildTableCreate(pMeta, pEntry);
3,392✔
2470
        }
2471
        break;
3,394✔
2472
      }
2473
      case TSDB_NORMAL_TABLE: {
66✔
2474
        if (isExist) {
66✔
2475
          code = metaHandleNormalTableUpdate(pMeta, pEntry);
19✔
2476
        } else {
2477
          code = metaHandleNormalTableCreate(pMeta, pEntry);
47✔
2478
        }
2479
        break;
66✔
2480
      }
2481
      case TSDB_VIRTUAL_NORMAL_TABLE: {
×
2482
        if (isExist) {
×
2483
          code = metaHandleVirtualNormalTableUpdate(pMeta, pEntry);
×
2484
        } else {
2485
          code = metaHandleVirtualNormalTableCreate(pMeta, pEntry);
×
2486
        }
2487
        break;
×
2488
      }
2489
      case TSDB_VIRTUAL_CHILD_TABLE: {
×
2490
        if (isExist) {
×
2491
          code = metaHandleVirtualChildTableUpdate(pMeta, pEntry);
×
2492
        } else {
2493
          code = metaHandleVirtualChildTableCreate(pMeta, pEntry);
×
2494
        }
2495
        break;
×
2496
      }
2497
      default: {
1✔
2498
        code = TSDB_CODE_INVALID_PARA;
1✔
2499
        break;
1✔
2500
      }
2501
    }
2502
  } else {
2503
    switch (type) {
544!
2504
      case TSDB_SUPER_TABLE: {
10✔
2505
        code = metaHandleSuperTableDrop(pMeta, pEntry);
10✔
2506
        break;
10✔
2507
      }
2508
      case TSDB_CHILD_TABLE: {
516✔
2509
        code = metaHandleChildTableDrop(pMeta, pEntry, false);
516✔
2510
        break;
516✔
2511
      }
2512
      case TSDB_NORMAL_TABLE: {
18✔
2513
        code = metaHandleNormalTableDrop(pMeta, pEntry);
18✔
2514
        break;
18✔
2515
      }
2516
      case TSDB_VIRTUAL_NORMAL_TABLE: {
×
2517
        code = metaHandleVirtualNormalTableDrop(pMeta, pEntry);
×
2518
        break;
×
2519
      }
2520
      case TSDB_VIRTUAL_CHILD_TABLE: {
×
2521
        code = metaHandleVirtualChildTableDrop(pMeta, pEntry, false);
×
2522
        break;
×
2523
      }
2524
      default: {
×
2525
        code = TSDB_CODE_INVALID_PARA;
×
2526
        break;
×
2527
      }
2528
    }
2529
  }
2530

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

2541
void metaHandleSyncEntry(SMeta *pMeta, const SMetaEntry *pEntry) {
×
2542
  int32_t code = TSDB_CODE_SUCCESS;
×
2543
  code = metaHandleEntry2(pMeta, pEntry);
×
2544
  if (code) {
×
2545
    metaErr(TD_VID(pMeta->pVnode), code);
×
2546
  }
2547
  return;
×
2548
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc