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

taosdata / TDengine / #3632

08 Mar 2025 06:17AM UTC coverage: 60.719% (+0.05%) from 60.671%
#3632

push

travis-ci

web-flow
Merge pull request #29999 from taosdata/enh/TS-5089

feat: taosBenchmark supports exporting to CSV files

141890 of 300701 branches covered (47.19%)

Branch coverage included in aggregate %.

599 of 766 new or added lines in 3 files covered. (78.2%)

1025 existing lines in 124 files now uncovered.

223757 of 301490 relevant lines covered (74.22%)

17284906.68 hits per line

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

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

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

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

96
  tDecoderInit(&decoder, value, valueSize);
169,965✔
97
  code = metaDecodeEntry(&decoder, &entry);
169,949✔
98
  if (code) {
169,867!
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);
169,867✔
107
  if (code) {
169,961!
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);
169,961✔
116
  tDecoderClear(&decoder);
169,956✔
117
  return code;
169,973✔
118
}
119

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

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

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

141
void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); }
169,939✔
142

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

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

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

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

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

180
  // put to tdb
181
  if (META_TABLE_OP_INSERT == op) {
186,578✔
182
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
172,143✔
183
  } else if (META_TABLE_OP_UPDATA == op) {
14,435✔
184
    code = tdbTbUpsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
10,326✔
185
  } else if (META_TABLE_OP_DELETE == op) {
4,109!
186
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
4,110✔
187
  } else {
188
    code = TSDB_CODE_INVALID_PARA;
×
189
  }
190
  if (TSDB_CODE_SUCCESS != code) {
186,669!
191
    metaErr(vgId, code);
×
192
  }
193
  taosMemoryFree(value);
186,630!
194
  return code;
186,627✔
195
}
196

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

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

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

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

217
  const SMetaEntry     *pEntry = pParam->pEntry;
43,634✔
218
  const SSchemaWrapper *pSchema = NULL;
43,634✔
219
  if (pEntry->type == TSDB_SUPER_TABLE) {
43,634✔
220
    pSchema = &pEntry->stbEntry.schemaRow;
29,538✔
221
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
14,096!
222
    pSchema = &pEntry->ntbEntry.schemaRow;
14,120✔
223
  } else {
224
    return TSDB_CODE_INVALID_PARA;
×
225
  }
226
  SSkmDbKey key = {
43,658✔
227
      .uid = pEntry->uid,
43,658✔
228
      .sver = pSchema->version,
43,658✔
229
  };
230

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

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

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

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

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

281
  if (pOldColumn && pNewColumn) {
134,062✔
282
    if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
132,233✔
283
      return TSDB_CODE_SUCCESS;
3,157✔
284
    } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) {
129,076!
285
      action = DROP_INDEX;
2,158✔
286
    } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
126,918!
287
      action = ADD_INDEX;
957✔
288
    } else {
289
      return TSDB_CODE_SUCCESS;
125,961✔
290
    }
291
  } else if (pOldColumn) {
1,829✔
292
    if (IS_IDX_ON(pOldColumn)) {
765✔
293
      action = DROP_INDEX;
15✔
294
    } else {
295
      return TSDB_CODE_SUCCESS;
750✔
296
    }
297
  } else {
298
    if (IS_IDX_ON(pNewColumn)) {
1,064!
299
      action = ADD_INDEX;
×
300
    } else {
301
      return TSDB_CODE_SUCCESS;
1,064✔
302
    }
303
  }
304

305
  // fetch all child tables
306
  SArray *childTables = 0;
3,130✔
307
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childTables);
3,130✔
308
  if (code) {
3,130!
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++) {
8,019✔
315
    int64_t uid = *(int64_t *)taosArrayGet(childTables, i);
4,889✔
316

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

326
    STagIdxKey *pTagIdxKey = NULL;
4,889✔
327
    int32_t     tagIdxKeySize = 0;
4,889✔
328

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

369
  taosArrayDestroy(childTables);
3,130✔
370
  return code;
3,130✔
371
}
372

373
static int32_t metaUpdateSuperTableTagSchema(SMeta *pMeta, const SMetaHandleParam *pParam) {
6,312✔
374
  int32_t               code = TSDB_CODE_SUCCESS;
6,312✔
375
  const SMetaEntry     *pEntry = pParam->pEntry;
6,312✔
376
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
6,312✔
377
  const SSchemaWrapper *pNewTagSchema = &pEntry->stbEntry.schemaTag;
6,312✔
378
  const SSchemaWrapper *pOldTagSchema = &pOldEntry->stbEntry.schemaTag;
6,312✔
379

380
  int32_t iOld = 0, iNew = 0;
6,312✔
381
  for (; iOld < pOldTagSchema->nCols && iNew < pNewTagSchema->nCols;) {
139,136✔
382
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
132,825✔
383
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
132,825✔
384

385
    if (pOldColumn->colId == pNewColumn->colId) {
132,825✔
386
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
132,235✔
387
      if (code) {
132,234!
388
        metaErr(TD_VID(pMeta->pVnode), code);
×
389
        return code;
×
390
      }
391

392
      iOld++;
132,234✔
393
      iNew++;
132,234✔
394
    } else if (pOldColumn->colId < pNewColumn->colId) {
590!
395
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
590✔
396
      if (code) {
590!
397
        metaErr(TD_VID(pMeta->pVnode), code);
×
398
        return code;
×
399
      }
400

401
      iOld++;
590✔
402
    } else {
403
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, NULL, pNewColumn);
×
404
      if (code) {
×
405
        metaErr(TD_VID(pMeta->pVnode), code);
×
406
        return code;
×
407
      }
408

409
      iNew++;
×
410
    }
411
  }
412

413
  for (; iOld < pOldTagSchema->nCols; iOld++) {
6,486✔
414
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
175✔
415
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
175✔
416
    if (code) {
175!
417
      metaErr(TD_VID(pMeta->pVnode), code);
×
418
      return code;
×
419
    }
420
  }
421

422
  for (; iNew < pNewTagSchema->nCols; iNew++) {
7,378✔
423
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
1,066✔
424
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, NULL, pNewColumn);
1,066✔
425
    if (code) {
1,066!
426
      metaErr(TD_VID(pMeta->pVnode), code);
×
427
      return code;
×
428
    }
429
  }
430

431
  return code;
6,312✔
432
}
433

434
static int32_t metaSchemaTableUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
49,980✔
435
  int32_t code = TSDB_CODE_SUCCESS;
49,980✔
436

437
  const SMetaEntry *pEntry = pParam->pEntry;
49,980✔
438
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
49,980✔
439

440
  if (NULL == pOldEntry) {
49,980✔
441
    return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
40,383✔
442
  }
443

444
  if (pEntry->type == TSDB_NORMAL_TABLE) {
9,597✔
445
    // check row schema
446
    if (pOldEntry->ntbEntry.schemaRow.version != pEntry->ntbEntry.schemaRow.version) {
220✔
447
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
187✔
448
    }
449
  } else if (pEntry->type == TSDB_SUPER_TABLE) {
9,377!
450
    // check row schema
451
    if (pOldEntry->stbEntry.schemaRow.version != pEntry->stbEntry.schemaRow.version) {
9,426✔
452
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
3,114✔
453
    }
454

455
    // check tag schema
456
    code = metaUpdateSuperTableTagSchema(pMeta, pParam);
6,312✔
457
    if (code) {
6,310✔
458
      metaErr(TD_VID(pMeta->pVnode), code);
1!
459
      return code;
×
460
    }
461

462
  } else {
463
    return TSDB_CODE_INVALID_PARA;
×
464
  }
465

466
  return TSDB_CODE_SUCCESS;
6,342✔
467
}
468

469
static int32_t metaSchemaTableDelete(SMeta *pMeta, const SMetaHandleParam *pEntry) {
×
470
  // TODO
471
  return TSDB_CODE_SUCCESS;
×
472
}
473

474
// Uid Index
475
static void metaBuildEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) {
182,417✔
476
  pInfo->uid = pEntry->uid;
182,417✔
477
  pInfo->version = pEntry->version;
182,417✔
478
  if (pEntry->type == TSDB_SUPER_TABLE) {
182,417✔
479
    pInfo->suid = pEntry->uid;
35,871✔
480
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
35,871✔
481
  } else if (pEntry->type == TSDB_CHILD_TABLE) {
146,546✔
482
    pInfo->suid = pEntry->ctbEntry.suid;
132,482✔
483
    pInfo->skmVer = 0;
132,482✔
484
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
14,064!
485
    pInfo->suid = 0;
14,155✔
486
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
14,155✔
487
  }
488
}
182,417✔
489

490
static int32_t metaUidIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
182,408✔
491
  int32_t code = TSDB_CODE_SUCCESS;
182,408✔
492
  int32_t vgId = TD_VID(pMeta->pVnode);
182,408✔
493

494
  const SMetaEntry *pEntry = pParam->pEntry;
182,408✔
495

496
  // update cache
497
  SMetaInfo info = {0};
182,408✔
498
  metaBuildEntryInfo(pEntry, &info);
182,408✔
499
  code = metaCacheUpsert(pMeta, &info);
182,487✔
500
  if (code) {
182,370!
501
    metaErr(vgId, code);
×
502
  }
503

504
  // put to tdb
505
  SUidIdxVal value = {
182,333✔
506
      .suid = info.suid,
182,333✔
507
      .skmVer = info.skmVer,
182,333✔
508
      .version = pEntry->version,
182,333✔
509
  };
510
  if (META_TABLE_OP_INSERT == op) {
182,333✔
511
    code = tdbTbInsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
172,011✔
512
  } else if (META_TABLE_OP_UPDATA == op) {
10,322!
513
    code = tdbTbUpsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
10,323✔
514
  }
515
  return code;
182,585✔
516
}
517

518
static int32_t metaUidIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
172,109✔
519
  return metaUidIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
172,109✔
520
}
521

522
static int32_t metaUidIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
10,323✔
523
  return metaUidIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
10,323✔
524
}
525

526
static int32_t metaUidIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
7,534✔
527
  int32_t code = 0;
7,534✔
528

529
  const SMetaEntry *pEntry = pParam->pOldEntry;
7,534✔
530

531
  // delete tdb
532
  code = tdbTbDelete(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), pMeta->txn);
7,534✔
533
  if (code) {
7,536!
534
    metaErr(TD_VID(pMeta->pVnode), code);
×
535
  }
536

537
  // delete cache
538
  (void)metaCacheDrop(pMeta, pEntry->uid);
7,536✔
539
  return code;
7,537✔
540
}
541

542
// Name Index
543
static int32_t metaNameIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
172,129✔
544
  int32_t code = TSDB_CODE_SUCCESS;
172,129✔
545

546
  const SMetaEntry *pEntry = pParam->pEntry;
172,129✔
547

548
  if (META_TABLE_OP_INSERT == op) {
172,129!
549
    code = tdbTbInsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
172,158✔
550
                       pMeta->txn);
551
  } else if (META_TABLE_OP_UPDATA == op) {
×
552
    code = tdbTbUpsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
×
553
                       pMeta->txn);
554
  } else {
555
    code = TSDB_CODE_INVALID_PARA;
×
556
  }
557
  return code;
172,263✔
558
}
559

560
static int32_t metaNameIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
172,156✔
561
  int32_t code = TSDB_CODE_SUCCESS;
172,156✔
562
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
172,156✔
563
}
564

565
static int32_t metaNameIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
566
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
×
567
}
568

569
static int32_t metaNameIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
7,531✔
570
  int32_t code = TSDB_CODE_SUCCESS;
7,531✔
571

572
  const SMetaEntry *pEntry = pParam->pOldEntry;
7,531✔
573
  code = tdbTbDelete(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, pMeta->txn);
7,531✔
574
  if (code) {
7,536!
575
    metaErr(TD_VID(pMeta->pVnode), code);
×
576
  }
577
  return code;
7,536✔
578
}
579

580
// Suid Index
581
static int32_t metaSUidIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
26,439✔
582
  const SMetaEntry *pEntry = pParam->pEntry;
26,439✔
583

584
  int32_t code = tdbTbInsert(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), NULL, 0, pMeta->txn);
26,439✔
585
  if (code) {
26,503!
586
    metaErr(TD_VID(pMeta->pVnode), code);
×
587
  }
588
  return code;
26,466✔
589
}
590

591
static int32_t metaSUidIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
2,142✔
592
  const SMetaEntry *pEntry = pParam->pOldEntry;
2,142✔
593

594
  int32_t code = tdbTbDelete(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), pMeta->txn);
2,142✔
595
  if (code) {
2,142!
596
    metaErr(TD_VID(pMeta->pVnode), code);
×
597
  }
598
  return code;
2,142✔
599
}
600

601
// Child Index
602
static int32_t metaChildIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
132,284✔
603
  int32_t code = TSDB_CODE_SUCCESS;
132,284✔
604

605
  const SMetaEntry *pEntry = pParam->pEntry;
132,284✔
606

607
  SCtbIdxKey key = {
132,284✔
608
      .suid = pEntry->ctbEntry.suid,
132,284✔
609
      .uid = pEntry->uid,
132,284✔
610
  };
611

612
  if (META_TABLE_OP_INSERT == op) {
132,284✔
613
    code = tdbTbInsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
131,814✔
614
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
131,814✔
615
  } else if (META_TABLE_OP_UPDATA == op) {
470!
616
    code = tdbTbUpsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
473✔
617
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
473✔
618
  } else {
619
    code = TSDB_CODE_INVALID_PARA;
×
620
  }
621
  return code;
132,287✔
622
}
623

624
static int32_t metaChildIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
131,799✔
625
  return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
131,799✔
626
}
627

628
static int32_t metaChildIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
681✔
629
  const SMetaEntry *pEntry = pParam->pEntry;
681✔
630
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
681✔
631
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
681✔
632

633
  const STag *pNewTags = (const STag *)pEntry->ctbEntry.pTags;
681✔
634
  const STag *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags;
681✔
635
  if (pNewTags->len != pOldTags->len || memcmp(pNewTags, pOldTags, pNewTags->len)) {
681✔
636
    return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
473✔
637
  }
638
  return 0;
208✔
639
}
640

641
static int32_t metaChildIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
3,702✔
642
  const SMetaEntry *pEntry = pParam->pOldEntry;
3,702✔
643

644
  SCtbIdxKey key = {
3,702✔
645
      .suid = pEntry->ctbEntry.suid,
3,702✔
646
      .uid = pEntry->uid,
3,702✔
647
  };
648
  return tdbTbDelete(pMeta->pCtbIdx, &key, sizeof(key), pMeta->txn);
3,702✔
649
}
650

651
// Tag Index
652
static int32_t metaFetchTagIdxKey(SMeta *pMeta, const SMetaEntry *pEntry, const SSchema *pTagColumn,
141,141✔
653
                                  STagIdxKey **ppTagIdxKey, int32_t *pTagIdxKeySize) {
654
  int32_t code = TSDB_CODE_SUCCESS;
141,141✔
655

656
  STagIdxKey *pTagIdxKey = NULL;
141,141✔
657
  int32_t     nTagIdxKey;
658
  const void *pTagData = NULL;
141,141✔
659
  int32_t     nTagData = 0;
141,141✔
660

661
  STagVal tagVal = {
141,141✔
662
      .cid = pTagColumn->colId,
141,141✔
663
  };
664

665
  if (tTagGet((const STag *)pEntry->ctbEntry.pTags, &tagVal)) {
141,141✔
666
    if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
140,698!
667
      pTagData = tagVal.pData;
13,283✔
668
      nTagData = (int32_t)tagVal.nData;
13,283✔
669
    } else {
670
      pTagData = &(tagVal.i64);
127,415✔
671
      nTagData = tDataTypes[pTagColumn->type].bytes;
127,415✔
672
    }
673
  } else {
674
    if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
423!
675
      nTagData = tDataTypes[pTagColumn->type].bytes;
254✔
676
    }
677
  }
678

679
  code = metaCreateTagIdxKey(pEntry->ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type,
141,121✔
680
                             pEntry->uid, &pTagIdxKey, &nTagIdxKey);
141,121✔
681
  if (code) {
141,123✔
682
    metaErr(TD_VID(pMeta->pVnode), code);
13!
683
    return code;
×
684
  }
685

686
  *ppTagIdxKey = pTagIdxKey;
141,110✔
687
  *pTagIdxKeySize = nTagIdxKey;
141,110✔
688
  return code;
141,110✔
689
}
690

691
static void metaFetchTagIdxKeyFree(STagIdxKey **ppTagIdxKey) {
141,155✔
692
  metaDestroyTagIdxKey(*ppTagIdxKey);
141,155✔
693
  *ppTagIdxKey = NULL;
141,150✔
694
}
141,150✔
695

696
static int32_t metaTagIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
131,799✔
697
  int32_t code = TSDB_CODE_SUCCESS;
131,799✔
698

699
  const SMetaEntry *pEntry = pParam->pEntry;
131,799✔
700
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
131,799✔
701

702
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
131,799✔
703
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
131,955✔
704
    const SSchema *pTagColumn = &pTagSchema->pSchema[0];
156✔
705

706
    STagVal tagVal = {
156✔
707
        .cid = pTagColumn->colId,
156✔
708
    };
709

710
    const void *pTagData = pEntry->ctbEntry.pTags;
156✔
711
    int32_t     nTagData = ((const STag *)pEntry->ctbEntry.pTags)->len;
156✔
712
    code = metaSaveJsonVarToIdx(pMeta, pEntry, pTagColumn);
156✔
713
    if (code) {
156!
714
      metaErr(TD_VID(pMeta->pVnode), code);
×
715
    }
716
  } else {
717
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
570,512✔
718
      STagIdxKey    *pTagIdxKey = NULL;
438,856✔
719
      int32_t        nTagIdxKey;
720
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
438,856✔
721

722
      if (!IS_IDX_ON(pTagColumn)) {
438,856✔
723
        continue;
307,240✔
724
      }
725

726
      code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pTagIdxKey, &nTagIdxKey);
131,616✔
727
      if (code) {
131,588!
728
        metaErr(TD_VID(pMeta->pVnode), code);
×
729
        return code;
×
730
      }
731

732
      code = tdbTbInsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn);
131,588✔
733
      if (code) {
131,637!
734
        metaErr(TD_VID(pMeta->pVnode), code);
×
735
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
736
        return code;
×
737
      }
738
      metaFetchTagIdxKeyFree(&pTagIdxKey);
131,637✔
739
    }
740
  }
741
  return code;
131,812✔
742
}
743

744
static int32_t metaTagIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
681✔
745
  int32_t code = TSDB_CODE_SUCCESS;
681✔
746

747
  const SMetaEntry     *pEntry = pParam->pEntry;
681✔
748
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
681✔
749
  const SMetaEntry     *pSuperEntry = pParam->pSuperEntry;
681✔
750
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
681✔
751
  const STag           *pNewTags = (const STag *)pEntry->ctbEntry.pTags;
681✔
752
  const STag           *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags;
681✔
753

754
  if (pNewTags->len == pOldTags->len && !memcmp(pNewTags, pOldTags, pNewTags->len)) {
681✔
755
    return code;
208✔
756
  }
757

758
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
473✔
759
    code = metaDelJsonVarFromIdx(pMeta, pOldEntry, &pTagSchema->pSchema[0]);
6✔
760
    if (code) {
6!
761
      metaErr(TD_VID(pMeta->pVnode), code);
×
762
      return code;
×
763
    }
764

765
    code = metaSaveJsonVarToIdx(pMeta, pEntry, &pTagSchema->pSchema[0]);
6✔
766
    if (code) {
6!
767
      metaErr(TD_VID(pMeta->pVnode), code);
×
768
      return code;
×
769
    }
770
  } else {
771
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
2,357✔
772
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
1,890✔
773

774
      if (!IS_IDX_ON(pTagColumn)) {
1,890✔
775
        continue;
1,427✔
776
      }
777

778
      STagIdxKey *pOldTagIdxKey = NULL;
463✔
779
      int32_t     oldTagIdxKeySize = 0;
463✔
780
      STagIdxKey *pNewTagIdxKey = NULL;
463✔
781
      int32_t     newTagIdxKeySize = 0;
463✔
782

783
      code = metaFetchTagIdxKey(pMeta, pOldEntry, pTagColumn, &pOldTagIdxKey, &oldTagIdxKeySize);
463✔
784
      if (code) {
463!
785
        metaErr(TD_VID(pMeta->pVnode), code);
×
786
        return code;
×
787
      }
788

789
      code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pNewTagIdxKey, &newTagIdxKeySize);
463✔
790
      if (code) {
463!
791
        metaErr(TD_VID(pMeta->pVnode), code);
×
792
        metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
793
        return code;
×
794
      }
795

796
      if (tagIdxKeyCmpr(pOldTagIdxKey, oldTagIdxKeySize, pNewTagIdxKey, newTagIdxKeySize)) {
463✔
797
        code = tdbTbDelete(pMeta->pTagIdx, pOldTagIdxKey, oldTagIdxKeySize, pMeta->txn);
97✔
798
        if (code) {
97!
799
          metaErr(TD_VID(pMeta->pVnode), code);
×
800
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
801
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
802
          return code;
×
803
        }
804

805
        code = tdbTbInsert(pMeta->pTagIdx, pNewTagIdxKey, newTagIdxKeySize, NULL, 0, pMeta->txn);
97✔
806
        if (code) {
97!
807
          metaErr(TD_VID(pMeta->pVnode), code);
×
808
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
809
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
810
          return code;
×
811
        }
812
      }
813

814
      metaFetchTagIdxKeyFree(&pOldTagIdxKey);
463✔
815
      metaFetchTagIdxKeyFree(&pNewTagIdxKey);
463✔
816
    }
817
  }
818
  return code;
473✔
819
}
820

821
static int32_t metaTagIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
3,706✔
822
  int32_t code = TSDB_CODE_SUCCESS;
3,706✔
823

824
  const SMetaEntry     *pEntry = pParam->pEntry;
3,706✔
825
  const SMetaEntry     *pChild = pParam->pOldEntry;
3,706✔
826
  const SMetaEntry     *pSuper = pParam->pSuperEntry;
3,706✔
827
  const SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
3,706✔
828
  const SSchema        *pTagColumn = NULL;
3,706✔
829
  const STag           *pTags = (const STag *)pChild->ctbEntry.pTags;
3,706✔
830

831
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
3,706✔
832
    pTagColumn = &pTagSchema->pSchema[0];
6✔
833
    code = metaDelJsonVarFromIdx(pMeta, pChild, pTagColumn);
6✔
834
    if (code) {
6!
835
      metaErr(TD_VID(pMeta->pVnode), code);
×
836
    }
837
  } else {
838
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
28,190✔
839
      pTagColumn = &pTagSchema->pSchema[i];
24,493✔
840
      if (!IS_IDX_ON(pTagColumn)) {
24,493✔
841
        continue;
20,782✔
842
      }
843

844
      STagIdxKey *pTagIdxKey = NULL;
3,711✔
845
      int32_t     nTagIdxKey;
846

847
      code = metaFetchTagIdxKey(pMeta, pChild, pTagColumn, &pTagIdxKey, &nTagIdxKey);
3,711✔
848
      if (code) {
3,699!
849
        metaErr(TD_VID(pMeta->pVnode), code);
×
850
        return code;
×
851
      }
852

853
      code = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
3,699✔
854
      if (code) {
3,709!
855
        metaErr(TD_VID(pMeta->pVnode), code);
×
856
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
857
        return code;
×
858
      }
859
      metaFetchTagIdxKeyFree(&pTagIdxKey);
3,709✔
860
    }
861
  }
862
  return code;
3,703✔
863
}
864

865
// Btime Index
866
static int32_t metaBtimeIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
151,107✔
867
  int32_t code = TSDB_CODE_SUCCESS;
151,107✔
868

869
  const SMetaEntry *pEntry;
870
  if (META_TABLE_OP_DELETE == op) {
151,107✔
871
    pEntry = pParam->pOldEntry;
5,390✔
872
  } else {
873
    pEntry = pParam->pEntry;
145,717✔
874
  }
875

876
  SBtimeIdxKey key = {
151,107✔
877
      .uid = pEntry->uid,
151,107✔
878
  };
879

880
  if (TSDB_CHILD_TABLE == pEntry->type) {
151,107✔
881
    key.btime = pEntry->ctbEntry.btime;
135,497✔
882
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
15,610!
883
    key.btime = pEntry->ntbEntry.btime;
15,627✔
884
  } else {
885
    return TSDB_CODE_INVALID_PARA;
×
886
  }
887

888
  if (META_TABLE_OP_INSERT == op) {
151,124✔
889
    code = tdbTbInsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
145,732✔
890
  } else if (META_TABLE_OP_UPDATA == op) {
5,392!
891
    code = tdbTbUpsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
×
892
  } else if (META_TABLE_OP_DELETE == op) {
5,392✔
893
    code = tdbTbDelete(pMeta->pBtimeIdx, &key, sizeof(key), pMeta->txn);
5,390✔
894
  } else {
895
    code = TSDB_CODE_INVALID_PARA;
2✔
896
  }
897
  if (code) {
151,151!
898
    metaErr(TD_VID(pMeta->pVnode), code);
×
899
  }
900
  return code;
151,146✔
901
}
902

903
static int32_t metaBtimeIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
145,725✔
904
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
145,725✔
905
}
906

907
static int32_t metaBtimeIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
908
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
×
909
}
910

911
static int32_t metaBtimeIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
5,390✔
912
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_DELETE);
5,390✔
913
}
914

915
// TTL Index
916
static int32_t metaTtlIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
145,741✔
917
  const SMetaEntry *pEntry = pParam->pEntry;
145,741✔
918

919
  STtlUpdTtlCtx ctx = {
145,741✔
920
      .uid = pEntry->uid,
145,741✔
921
      .pTxn = pMeta->txn,
145,741✔
922
  };
923
  if (TSDB_CHILD_TABLE == pEntry->type) {
145,741✔
924
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
131,787✔
925
    ctx.changeTimeMs = pEntry->ctbEntry.btime;
131,787✔
926
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
13,954✔
927
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
13,946✔
928
    ctx.changeTimeMs = pEntry->ntbEntry.btime;
13,946✔
929
  } else {
930
    return TSDB_CODE_INVALID_PARA;
8✔
931
  }
932

933
  int32_t ret = ttlMgrInsertTtl(pMeta->pTtlMgr, &ctx);
145,733✔
934
  if (ret < 0) {
145,726!
935
    metaError("vgId:%d, failed to insert ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid, tstrerror(ret));
×
936
  }
937
  return TSDB_CODE_SUCCESS;
145,732✔
938
}
939

940
static int32_t metaTtlIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
145,744✔
941
  return metaTtlIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
145,744✔
942
}
943

944
static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam);
945

946
static int32_t metaTtlIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
901✔
947
  int32_t code = TSDB_CODE_SUCCESS;
901✔
948

949
  const SMetaEntry *pEntry = pParam->pEntry;
901✔
950
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
901✔
951

952
  if ((pEntry->type == TSDB_CHILD_TABLE && pOldEntry->ctbEntry.ttlDays != pEntry->ctbEntry.ttlDays) ||
901✔
953
      (pEntry->type == TSDB_NORMAL_TABLE && pOldEntry->ntbEntry.ttlDays != pEntry->ntbEntry.ttlDays)) {
890✔
954
    code = metaTtlIdxDelete(pMeta, pParam);
18✔
955
    if (code) {
18!
956
      metaErr(TD_VID(pMeta->pVnode), code);
×
957
    }
958

959
    code = metaTtlIdxInsert(pMeta, pParam);
18✔
960
    if (code) {
18!
961
      metaErr(TD_VID(pMeta->pVnode), code);
×
962
    }
963
  }
964

965
  return TSDB_CODE_SUCCESS;
901✔
966
}
967

968
static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
5,408✔
969
  int32_t code = TSDB_CODE_SUCCESS;
5,408✔
970

971
  const SMetaEntry *pEntry = pParam->pOldEntry;
5,408✔
972
  STtlDelTtlCtx     ctx = {
5,408✔
973
          .uid = pEntry->uid,
5,408✔
974
          .pTxn = pMeta->txn,
5,408✔
975
  };
976

977
  if (TSDB_CHILD_TABLE == pEntry->type) {
5,408✔
978
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
3,711✔
979
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
1,697!
980
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
1,697✔
981
  } else {
982
    code = TSDB_CODE_INVALID_PARA;
×
983
  }
984

985
  if (TSDB_CODE_SUCCESS == code) {
5,408!
986
    int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
5,408✔
987
    if (ret < 0) {
5,408!
988
      metaError("vgId:%d, failed to delete ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid,
×
989
                tstrerror(ret));
990
    }
991
  }
992
  return code;
5,406✔
993
}
994

995
static void metaTimeSeriesNotifyCheck(SMeta *pMeta) {
148,297✔
996
#if defined(TD_ENTERPRISE)
997
  int64_t nTimeSeries = metaGetTimeSeriesNum(pMeta, 0);
148,297✔
998
  int64_t deltaTS = nTimeSeries - pMeta->pVnode->config.vndStats.numOfReportedTimeSeries;
148,315✔
999
  if (deltaTS > tsTimeSeriesThreshold) {
148,315✔
1000
    if (0 == atomic_val_compare_exchange_8(&dmNotifyHdl.state, 1, 2)) {
102,224✔
1001
      if (tsem_post(&dmNotifyHdl.sem) != 0) {
102,214!
1002
        metaError("vgId:%d, failed to post semaphore, errno:%d", TD_VID(pMeta->pVnode), errno);
×
1003
      }
1004
    }
1005
  }
1006
#endif
1007
}
148,342✔
1008

1009
static int32_t (*metaTableOpFn[META_TABLE_MAX][META_TABLE_OP_MAX])(SMeta *pMeta, const SMetaHandleParam *pParam) =
1010
    {
1011
        [META_ENTRY_TABLE] =
1012
            {
1013
                [META_TABLE_OP_INSERT] = metaEntryTableInsert,
1014
                [META_TABLE_OP_UPDATA] = metaEntryTableUpdate,
1015
                [META_TABLE_OP_DELETE] = metaEntryTableDelete,
1016
            },
1017
        [META_SCHEMA_TABLE] =
1018
            {
1019
                [META_TABLE_OP_INSERT] = metaSchemaTableInsert,
1020
                [META_TABLE_OP_UPDATA] = metaSchemaTableUpdate,
1021
                [META_TABLE_OP_DELETE] = metaSchemaTableDelete,
1022
            },
1023
        [META_UID_IDX] =
1024
            {
1025
                [META_TABLE_OP_INSERT] = metaUidIdxInsert,
1026
                [META_TABLE_OP_UPDATA] = metaUidIdxUpdate,
1027
                [META_TABLE_OP_DELETE] = metaUidIdxDelete,
1028
            },
1029
        [META_NAME_IDX] =
1030
            {
1031
                [META_TABLE_OP_INSERT] = metaNameIdxInsert,
1032
                [META_TABLE_OP_UPDATA] = metaNameIdxUpdate,
1033
                [META_TABLE_OP_DELETE] = metaNameIdxDelete,
1034
            },
1035
        [META_SUID_IDX] =
1036
            {
1037
                [META_TABLE_OP_INSERT] = metaSUidIdxInsert,
1038
                [META_TABLE_OP_UPDATA] = NULL,
1039
                [META_TABLE_OP_DELETE] = metaSUidIdxDelete,
1040
            },
1041
        [META_CHILD_IDX] =
1042
            {
1043
                [META_TABLE_OP_INSERT] = metaChildIdxInsert,
1044
                [META_TABLE_OP_UPDATA] = metaChildIdxUpdate,
1045
                [META_TABLE_OP_DELETE] = metaChildIdxDelete,
1046
            },
1047
        [META_TAG_IDX] =
1048
            {
1049
                [META_TABLE_OP_INSERT] = metaTagIdxInsert,
1050
                [META_TABLE_OP_UPDATA] = metaTagIdxUpdate,
1051
                [META_TABLE_OP_DELETE] = metaTagIdxDelete,
1052
            },
1053
        [META_BTIME_IDX] =
1054
            {
1055
                [META_TABLE_OP_INSERT] = metaBtimeIdxInsert,
1056
                [META_TABLE_OP_UPDATA] = metaBtimeIdxUpdate,
1057
                [META_TABLE_OP_DELETE] = metaBtimeIdxDelete,
1058
            },
1059
        [META_TTL_IDX] =
1060
            {
1061
                [META_TABLE_OP_INSERT] = metaTtlIdxInsert,
1062
                [META_TABLE_OP_UPDATA] = metaTtlIdxUpdate,
1063
                [META_TABLE_OP_DELETE] = metaTtlIdxDelete,
1064
            },
1065
};
1066

1067
static int32_t metaHandleSuperTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
26,448✔
1068
  int32_t code = TSDB_CODE_SUCCESS;
26,448✔
1069

1070
  SMetaTableOp ops[] = {
26,448✔
1071
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1072
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: here should be insert
1073
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1074
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1075
      {META_SUID_IDX, META_TABLE_OP_INSERT},      //
1076
  };
1077

1078
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
158,548✔
1079
    SMetaTableOp          *op = &ops[i];
132,147✔
1080
    const SMetaHandleParam param = {
132,147✔
1081
        .pEntry = pEntry,
1082
    };
1083

1084
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
132,147✔
1085
    if (TSDB_CODE_SUCCESS != code) {
132,126✔
1086
      metaErr(TD_VID(pMeta->pVnode), code);
26!
1087
      return code;
×
1088
    }
1089
  }
1090

1091
  return code;
26,401✔
1092
}
1093
static int32_t metaHandleSuperTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
26,059✔
1094
  int32_t code = TSDB_CODE_SUCCESS;
26,059✔
1095

1096
  metaWLock(pMeta);
26,059✔
1097
  code = metaHandleSuperTableCreateImpl(pMeta, pEntry);
26,491✔
1098
  metaULock(pMeta);
26,459✔
1099

1100
  if (TSDB_CODE_SUCCESS == code) {
26,491!
1101
    pMeta->pVnode->config.vndStats.numOfSTables++;
26,491✔
1102

1103
    metaInfo("vgId:%d, %s success, version:%" PRId64 " type:%d uid:%" PRId64 " name:%s", TD_VID(pMeta->pVnode),
26,491!
1104
             __func__, pEntry->version, pEntry->type, pEntry->uid, pEntry->name);
1105
  } else {
1106
    metaErr(TD_VID(pMeta->pVnode), code);
×
1107
  }
1108
  return code;
26,518✔
1109
}
1110

1111
static int32_t metaHandleNormalTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
13,939✔
1112
  int32_t code = TSDB_CODE_SUCCESS;
13,939✔
1113

1114
  SMetaTableOp ops[] = {
13,939✔
1115
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1116
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: need to be insert
1117
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1118
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1119
      {META_BTIME_IDX, META_TABLE_OP_INSERT},     //
1120
      {META_TTL_IDX, META_TABLE_OP_INSERT},       //
1121
  };
1122

1123
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
97,536✔
1124
    SMetaTableOp *op = &ops[i];
83,597✔
1125

1126
    SMetaHandleParam param = {
83,597✔
1127
        .pEntry = pEntry,
1128
    };
1129

1130
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
83,597✔
1131
    if (TSDB_CODE_SUCCESS != code) {
83,594!
1132
      metaErr(TD_VID(pMeta->pVnode), code);
×
1133
      return code;
×
1134
    }
1135
  }
1136

1137
  return code;
13,939✔
1138
}
1139
static int32_t metaHandleNormalTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
13,936✔
1140
  int32_t code = TSDB_CODE_SUCCESS;
13,936✔
1141

1142
  // update TDB
1143
  metaWLock(pMeta);
13,936✔
1144
  code = metaHandleNormalTableCreateImpl(pMeta, pEntry);
13,939✔
1145
  metaULock(pMeta);
13,938✔
1146

1147
  // update other stuff
1148
  if (TSDB_CODE_SUCCESS == code) {
13,938!
1149
    pMeta->pVnode->config.vndStats.numOfNTables++;
13,938✔
1150
    pMeta->pVnode->config.vndStats.numOfNTimeSeries += pEntry->ntbEntry.schemaRow.nCols - 1;
13,938✔
1151

1152
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
13,938✔
1153
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, -1, &pEntry->ntbEntry.schemaRow);
28✔
1154
      if (rc < 0) {
28!
1155
        metaError("vgId:%d, failed to create table:%s since %s", TD_VID(pMeta->pVnode), pEntry->name, tstrerror(rc));
×
1156
      }
1157
    }
1158
    metaTimeSeriesNotifyCheck(pMeta);
13,938✔
1159
  } else {
1160
    metaErr(TD_VID(pMeta->pVnode), code);
×
1161
  }
1162
  return code;
13,937✔
1163
}
1164

1165
static int32_t metaHandleChildTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry, const SMetaEntry *pSuperEntry) {
131,795✔
1166
  int32_t code = TSDB_CODE_SUCCESS;
131,795✔
1167

1168
  SMetaTableOp ops[] = {
131,795✔
1169
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},  //
1170
      {META_UID_IDX, META_TABLE_OP_INSERT},      //
1171
      {META_NAME_IDX, META_TABLE_OP_INSERT},     //
1172
      {META_CHILD_IDX, META_TABLE_OP_INSERT},    //
1173
      {META_TAG_IDX, META_TABLE_OP_INSERT},      //
1174
      {META_BTIME_IDX, META_TABLE_OP_INSERT},    //
1175
      {META_TTL_IDX, META_TABLE_OP_INSERT},      //
1176
  };
1177

1178
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1,053,475✔
1179
    SMetaTableOp *op = &ops[i];
921,964✔
1180

1181
    SMetaHandleParam param = {
921,964✔
1182
        .pEntry = pEntry,
1183
        .pSuperEntry = pSuperEntry,
1184
    };
1185

1186
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
921,964✔
1187
    if (TSDB_CODE_SUCCESS != code) {
921,650!
1188
      metaErr(TD_VID(pMeta->pVnode), code);
×
1189
      return code;
×
1190
    }
1191
  }
1192

1193
  if (TSDB_CODE_SUCCESS == code) {
131,511!
1194
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0);
131,776✔
1195
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
131,767✔
1196
    if (ret < 0) {
131,821!
1197
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1198
    }
1199

1200
    ret = metaTbGroupCacheClear(pMeta, pSuperEntry->uid);
131,821✔
1201
    if (ret < 0) {
131,806!
1202
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1203
    }
1204
  }
1205
  return code;
131,805✔
1206
}
1207

1208
static int32_t metaHandleChildTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
131,801✔
1209
  int32_t     code = TSDB_CODE_SUCCESS;
131,801✔
1210
  SMetaEntry *pSuperEntry = NULL;
131,801✔
1211

1212
  // get the super table entry
1213
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
131,801✔
1214
  if (code) {
131,809!
1215
    metaErr(TD_VID(pMeta->pVnode), code);
×
1216
    return code;
×
1217
  }
1218

1219
  // update TDB
1220
  metaWLock(pMeta);
131,809✔
1221
  code = metaHandleChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
131,826✔
1222
  metaULock(pMeta);
131,796✔
1223

1224
  // update other stuff
1225
  if (TSDB_CODE_SUCCESS == code) {
131,810!
1226
    pMeta->pVnode->config.vndStats.numOfCTables++;
131,810✔
1227

1228
    if (!metaTbInFilterCache(pMeta, pSuperEntry->name, 1)) {
131,810!
1229
      int32_t nCols = 0;
131,799✔
1230
      int32_t ret = metaGetStbStats(pMeta->pVnode, pSuperEntry->uid, 0, &nCols);
131,799✔
1231
      if (ret < 0) {
131,810!
1232
        metaErr(TD_VID(pMeta->pVnode), ret);
×
1233
      }
1234
      pMeta->pVnode->config.vndStats.numOfTimeSeries += (nCols > 0 ? nCols - 1 : 0);
131,801!
1235
    }
1236

1237
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
131,800✔
1238
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, pEntry->ctbEntry.suid, NULL);
567✔
1239
      if (rc < 0) {
567!
1240
        metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
1241
                  tstrerror(rc));
1242
      }
1243
    }
1244

1245
  } else {
1246
    metaErr(TD_VID(pMeta->pVnode), code);
×
1247
  }
1248
  metaTimeSeriesNotifyCheck(pMeta);
131,800✔
1249
  metaFetchEntryFree(&pSuperEntry);
131,813✔
1250
  return code;
131,817✔
1251
}
1252

1253
static int32_t metaHandleNormalTableDropImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
1,690✔
1254
  int32_t code = TSDB_CODE_SUCCESS;
1,690✔
1255

1256
  SMetaTableOp ops[] = {
1,690✔
1257
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1258
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1259
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1260
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1261
      {META_TTL_IDX, META_TABLE_OP_DELETE},      //
1262

1263
      // {META_SCHEMA_TABLE, META_TABLE_OP_DELETE},  //
1264
  };
1265

1266
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
10,140✔
1267
    SMetaTableOp *op = &ops[i];
8,450✔
1268
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
8,450✔
1269
    if (code) {
8,450!
1270
      const SMetaEntry *pEntry = pParam->pEntry;
×
1271
      metaErr(TD_VID(pMeta->pVnode), code);
×
1272
    }
1273
  }
1274

1275
  return code;
1,690✔
1276
}
1277

1278
static int32_t metaHandleNormalTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
1,690✔
1279
  int32_t     code = TSDB_CODE_SUCCESS;
1,690✔
1280
  SMetaEntry *pOldEntry = NULL;
1,690✔
1281

1282
  // fetch the entry
1283
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
1,690✔
1284
  if (code) {
1,690!
1285
    metaErr(TD_VID(pMeta->pVnode), code);
×
1286
    return code;
×
1287
  }
1288

1289
  SMetaHandleParam param = {
1,690✔
1290
      .pEntry = pEntry,
1291
      .pOldEntry = pOldEntry,
1292
  };
1293

1294
  // do the drop
1295
  metaWLock(pMeta);
1,690✔
1296
  code = metaHandleNormalTableDropImpl(pMeta, &param);
1,690✔
1297
  metaULock(pMeta);
1,690✔
1298
  if (code) {
1,690!
1299
    metaErr(TD_VID(pMeta->pVnode), code);
×
1300
    metaFetchEntryFree(&pOldEntry);
×
1301
    return code;
×
1302
  }
1303

1304
  // update other stuff
1305
  pMeta->pVnode->config.vndStats.numOfNTables--;
1,690✔
1306
  pMeta->pVnode->config.vndStats.numOfNTimeSeries -= (pOldEntry->ntbEntry.schemaRow.nCols - 1);
1,690✔
1307

1308
#if 0
1309
  if (tbUids) {
1310
    if (taosArrayPush(tbUids, &uid) == NULL) {
1311
      rc = terrno;
1312
      goto _exit;
1313
    }
1314
  }
1315
#endif
1316

1317
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
1,690!
1318
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pOldEntry->uid, 0, NULL);
×
1319
    if (ret < 0) {
×
1320
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1321
    }
1322
  }
1323

1324
  metaFetchEntryFree(&pOldEntry);
1,690✔
1325
  return code;
1,690✔
1326
}
1327

1328
static int32_t metaHandleChildTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam, bool superDropped) {
3,697✔
1329
  int32_t code = TSDB_CODE_SUCCESS;
3,697✔
1330

1331
  const SMetaEntry *pEntry = pParam->pEntry;
3,697✔
1332
  const SMetaEntry *pChild = pParam->pOldEntry;
3,697✔
1333
  const SMetaEntry *pSuper = pParam->pSuperEntry;
3,697✔
1334

1335
  SMetaTableOp ops[] = {
3,697✔
1336
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1337
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1338
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1339
      {META_CHILD_IDX, META_TABLE_OP_DELETE},    //
1340
      {META_TAG_IDX, META_TABLE_OP_DELETE},      //
1341
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1342
      {META_TTL_IDX, META_TABLE_OP_DELETE},      //
1343
  };
1344

1345
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
29,548✔
1346
    SMetaTableOp *op = &ops[i];
25,885✔
1347

1348
    if (op->table == META_ENTRY_TABLE && superDropped) {
25,885✔
1349
      continue;
3,417✔
1350
    }
1351

1352
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
22,468✔
1353
    if (code) {
22,439✔
1354
      metaErr(TD_VID(pMeta->pVnode), code);
5!
1355
      return code;
×
1356
    }
1357
  }
1358

1359
  --pMeta->pVnode->config.vndStats.numOfCTables;
3,663✔
1360
  metaUpdateStbStats(pMeta, pParam->pSuperEntry->uid, -1, 0);
3,663✔
1361
  int32_t ret = metaUidCacheClear(pMeta, pSuper->uid);
3,692✔
1362
  if (ret < 0) {
3,701!
1363
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1364
  }
1365

1366
  ret = metaTbGroupCacheClear(pMeta, pSuper->uid);
3,701✔
1367
  if (ret < 0) {
3,705!
1368
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1369
  }
1370
  return code;
3,703✔
1371
}
1372

1373
static int32_t metaHandleChildTableDrop(SMeta *pMeta, const SMetaEntry *pEntry, bool superDropped) {
3,698✔
1374
  int32_t     code = TSDB_CODE_SUCCESS;
3,698✔
1375
  SMetaEntry *pChild = NULL;
3,698✔
1376
  SMetaEntry *pSuper = NULL;
3,698✔
1377

1378
  // fetch old entry
1379
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pChild);
3,698✔
1380
  if (code) {
3,699!
1381
    metaErr(TD_VID(pMeta->pVnode), code);
×
1382
    return code;
×
1383
  }
1384

1385
  // fetch super entry
1386
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
3,699✔
1387
  if (code) {
3,700!
1388
    metaErr(TD_VID(pMeta->pVnode), code);
×
1389
    metaFetchEntryFree(&pChild);
×
1390
    return code;
×
1391
  }
1392

1393
  SMetaHandleParam param = {
3,700✔
1394
      .pEntry = pEntry,
1395
      .pOldEntry = pChild,
1396
      .pSuperEntry = pSuper,
1397
  };
1398

1399
  // do the drop
1400
  metaWLock(pMeta);
3,700✔
1401
  code = metaHandleChildTableDropImpl(pMeta, &param, superDropped);
3,697✔
1402
  metaULock(pMeta);
3,702✔
1403
  if (code) {
3,694!
1404
    metaErr(TD_VID(pMeta->pVnode), code);
×
1405
    metaFetchEntryFree(&pChild);
×
1406
    metaFetchEntryFree(&pSuper);
×
1407
    return code;
×
1408
  }
1409

1410
  // do other stuff
1411
  if (!metaTbInFilterCache(pMeta, pSuper->name, 1)) {
3,694!
1412
    int32_t      nCols = 0;
3,690✔
1413
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
3,690✔
1414
    if (metaGetStbStats(pMeta->pVnode, pSuper->uid, NULL, &nCols) == 0) {
3,690!
1415
      pStats->numOfTimeSeries -= nCols - 1;
3,695✔
1416
    }
1417
  }
1418

1419
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
3,695✔
1420
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pChild->uid, pSuper->uid, NULL);
9✔
1421
    if (ret < 0) {
9!
1422
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1423
    }
1424
  }
1425

1426
#if 0
1427
  if (tbUids) {
1428
    if (taosArrayPush(tbUids, &uid) == NULL) {
1429
      rc = terrno;
1430
      goto _exit;
1431
    }
1432
  }
1433

1434
  if ((type == TSDB_CHILD_TABLE) && tbUid) {
1435
    *tbUid = uid;
1436
  }
1437
#endif
1438
  metaFetchEntryFree(&pChild);
3,695✔
1439
  metaFetchEntryFree(&pSuper);
3,698✔
1440
  return code;
3,702✔
1441
}
1442

1443
static int32_t metaGetChildUidsOfSuperTable(SMeta *pMeta, tb_uid_t suid, SArray **childList) {
5,360✔
1444
  int32_t code = TSDB_CODE_SUCCESS;
5,360✔
1445
  void   *key = NULL;
5,360✔
1446
  int32_t keySize = 0;
5,360✔
1447
  int32_t c;
1448

1449
  *childList = taosArrayInit(64, sizeof(tb_uid_t));
5,360✔
1450
  if (*childList == NULL) {
5,364!
1451
    return terrno;
×
1452
  }
1453

1454
  TBC *cursor = NULL;
5,364✔
1455
  code = tdbTbcOpen(pMeta->pCtbIdx, &cursor, NULL);
5,364✔
1456
  if (code) {
5,366!
1457
    taosArrayDestroy(*childList);
×
1458
    *childList = NULL;
×
1459
    return code;
×
1460
  }
1461

1462
  int32_t rc = tdbTbcMoveTo(cursor,
5,366✔
1463
                            &(SCtbIdxKey){
5,366✔
1464
                                .suid = suid,
1465
                                .uid = INT64_MIN,
1466
                            },
1467
                            sizeof(SCtbIdxKey), &c);
1468
  if (rc < 0) {
5,364!
1469
    tdbTbcClose(cursor);
×
1470
    return 0;
×
1471
  }
1472

1473
  for (;;) {
1474
    if (tdbTbcNext(cursor, &key, &keySize, NULL, NULL) < 0) {
14,510✔
1475
      break;
1,709✔
1476
    }
1477

1478
    if (((SCtbIdxKey *)key)->suid < suid) {
12,807✔
1479
      continue;
619✔
1480
    } else if (((SCtbIdxKey *)key)->suid > suid) {
12,188✔
1481
      break;
3,657✔
1482
    }
1483

1484
    if (taosArrayPush(*childList, &(((SCtbIdxKey *)key)->uid)) == NULL) {
17,058!
1485
      tdbFreeClear(key);
×
1486
      tdbTbcClose(cursor);
×
1487
      taosArrayDestroy(*childList);
×
1488
      *childList = NULL;
×
1489
      return terrno;
×
1490
    }
1491
  }
1492

1493
  tdbTbcClose(cursor);
5,366✔
1494
  tdbFreeClear(key);
5,367✔
1495
  return code;
5,368✔
1496
}
1497

1498
static int32_t metaHandleSuperTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
2,141✔
1499
  int32_t           code = TSDB_CODE_SUCCESS;
2,141✔
1500
  const SMetaEntry *pEntry = pParam->pEntry;
2,141✔
1501

1502
  SMetaTableOp ops[] = {
2,141✔
1503
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1504
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1505
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1506
      {META_SUID_IDX, META_TABLE_OP_DELETE},     //
1507

1508
      // {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: here should be insert
1509
  };
1510

1511
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
10,704✔
1512
    SMetaTableOp *op = &ops[i];
8,563✔
1513

1514
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
8,563✔
1515
    if (TSDB_CODE_SUCCESS != code) {
8,563!
1516
      metaErr(TD_VID(pMeta->pVnode), code);
×
1517
      return code;
×
1518
    }
1519
  }
1520

1521
  int32_t ret = metaStatsCacheDrop(pMeta, pEntry->uid);
2,141✔
1522
  if (ret < 0) {
2,142✔
1523
    metaErr(TD_VID(pMeta->pVnode), ret);
224!
1524
  }
1525
  return code;
2,142✔
1526
}
1527

1528
static int32_t metaHandleNormalTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
220✔
1529
  int32_t code = TSDB_CODE_SUCCESS;
220✔
1530

1531
  const SMetaEntry *pEntry = pParam->pEntry;
220✔
1532

1533
  SMetaTableOp ops[] = {
220✔
1534
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
1535
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
1536
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
1537
      {META_TTL_IDX, META_TABLE_OP_UPDATA},       //
1538
  };
1539
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1,100✔
1540
    SMetaTableOp *op = &ops[i];
880✔
1541
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
880✔
1542
    if (code) {
880!
1543
      metaErr(TD_VID(pMeta->pVnode), code);
×
1544
      return code;
×
1545
    }
1546
  }
1547
#if 0
1548
  if (metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs) < 0) {
1549
    metaError("vgId:%d, failed to update change time:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
1550
  }
1551
#endif
1552
  return code;
220✔
1553
}
1554

1555
static int32_t metaHandleChildTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
681✔
1556
  int32_t code = TSDB_CODE_SUCCESS;
681✔
1557

1558
  const SMetaEntry *pEntry = pParam->pEntry;
681✔
1559
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
681✔
1560
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
681✔
1561

1562
  SMetaTableOp ops[] = {
681✔
1563
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},  //
1564
      {META_UID_IDX, META_TABLE_OP_UPDATA},      //
1565
      {META_TAG_IDX, META_TABLE_OP_UPDATA},      //
1566
      {META_CHILD_IDX, META_TABLE_OP_UPDATA},    //
1567
      {META_TTL_IDX, META_TABLE_OP_UPDATA},      //
1568
  };
1569

1570
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
4,086✔
1571
    SMetaTableOp *op = &ops[i];
3,405✔
1572
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
3,405✔
1573
    if (code) {
3,405!
1574
      metaErr(TD_VID(pMeta->pVnode), code);
×
1575
      return code;
×
1576
    }
1577
  }
1578

1579
  if (metaUidCacheClear(pMeta, pSuperEntry->uid) < 0) {
681!
1580
    metaErr(TD_VID(pMeta->pVnode), code);
×
1581
  }
1582

1583
  if (metaTbGroupCacheClear(pMeta, pSuperEntry->uid) < 0) {
681!
1584
    metaErr(TD_VID(pMeta->pVnode), code);
×
1585
  }
1586
  return code;
681✔
1587
#if 0
1588
  if (metaUpdateChangeTime(pMeta, ctbEntry.uid, pReq->ctimeMs) < 0) {
1589
    metaError("meta/table: failed to update change time:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
1590
  }
1591
#endif
1592
}
1593

1594
static int32_t metaHandleSuperTableUpdateImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
9,424✔
1595
  int32_t code = TSDB_CODE_SUCCESS;
9,424✔
1596

1597
  const SMetaEntry *pEntry = pParam->pEntry;
9,424✔
1598
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
9,424✔
1599

1600
  SMetaTableOp ops[] = {
9,424✔
1601
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
1602
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
1603
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
1604
  };
1605

1606
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
37,685✔
1607
    SMetaTableOp *op = &ops[i];
28,269✔
1608
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
28,269✔
1609
    if (code) {
28,261!
1610
      metaErr(TD_VID(pMeta->pVnode), code);
×
1611
      return code;
×
1612
    }
1613
  }
1614

1615
  if (TSDB_CODE_SUCCESS == code) {
9,416!
1616
    metaUpdateStbStats(pMeta, pEntry->uid, 0, pEntry->stbEntry.schemaRow.nCols - pOldEntry->stbEntry.schemaRow.nCols);
9,420✔
1617
  }
1618

1619
  return code;
9,423✔
1620
}
1621

1622
static int32_t metaHandleSuperTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
9,423✔
1623
  int32_t code = TSDB_CODE_SUCCESS;
9,423✔
1624

1625
  SMetaEntry *pOldEntry = NULL;
9,423✔
1626

1627
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
9,423✔
1628
  if (code) {
9,426!
1629
    metaErr(TD_VID(pMeta->pVnode), code);
×
1630
    return code;
×
1631
  }
1632

1633
  SMetaHandleParam param = {
9,426✔
1634
      .pEntry = pEntry,
1635
      .pOldEntry = pOldEntry,
1636
  };
1637
  metaWLock(pMeta);
9,426✔
1638
  code = metaHandleSuperTableUpdateImpl(pMeta, &param);
9,425✔
1639
  metaULock(pMeta);
9,418✔
1640
  if (code) {
9,425!
1641
    metaErr(TD_VID(pMeta->pVnode), code);
×
1642
    metaFetchEntryFree(&pOldEntry);
×
1643
    return code;
×
1644
  }
1645

1646
  int     nCols = pEntry->stbEntry.schemaRow.nCols;
9,425✔
1647
  int     onCols = pOldEntry->stbEntry.schemaRow.nCols;
9,425✔
1648
  int32_t deltaCol = nCols - onCols;
9,425✔
1649
  bool    updStat = deltaCol != 0 && !metaTbInFilterCache(pMeta, pEntry->name, 1);
9,425!
1650

1651
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
9,425✔
1652
    STsdb  *pTsdb = pMeta->pVnode->pTsdb;
1,916✔
1653
    SArray *uids = NULL; /*taosArrayInit(8, sizeof(int64_t));
1,916✔
1654
     if (uids == NULL) {
1655
       metaErr(TD_VID(pMeta->pVnode), code);
1656
       metaFetchEntryFree(&pOldEntry);
1657
       return terrno;
1658
       }*/
1659
    if (deltaCol == 1) {
1,916✔
1660
      int16_t cid = pEntry->stbEntry.schemaRow.pSchema[nCols - 1].colId;
64✔
1661
      int8_t  col_type = pEntry->stbEntry.schemaRow.pSchema[nCols - 1].type;
64✔
1662

1663
      code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &uids);
64✔
1664
      if (code) {
64!
1665
        metaErr(TD_VID(pMeta->pVnode), code);
×
1666
        metaFetchEntryFree(&pOldEntry);
×
1667
        return code;
×
1668
      }
1669
      TAOS_CHECK_RETURN(tsdbCacheNewSTableColumn(pTsdb, uids, cid, col_type));
64!
1670
    } else if (deltaCol == -1) {
1,852✔
1671
      int16_t cid = -1;
64✔
1672
      bool    hasPrimaryKey = false;
64✔
1673
      if (onCols >= 2) {
64!
1674
        hasPrimaryKey = (pOldEntry->stbEntry.schemaRow.pSchema[1].flags & COL_IS_KEY) ? true : false;
64✔
1675
      }
1676
      for (int i = 0, j = 0; i < nCols && j < onCols; ++i, ++j) {
416!
1677
        if (pEntry->stbEntry.schemaRow.pSchema[i].colId != pOldEntry->stbEntry.schemaRow.pSchema[j].colId) {
384✔
1678
          cid = pOldEntry->stbEntry.schemaRow.pSchema[j].colId;
32✔
1679
          break;
32✔
1680
        }
1681
      }
1682

1683
      if (cid != -1) {
64✔
1684
        code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &uids);
32✔
1685
        if (code) {
32!
1686
          metaErr(TD_VID(pMeta->pVnode), code);
×
1687
          metaFetchEntryFree(&pOldEntry);
×
1688
          return code;
×
1689
        }
1690
        TAOS_CHECK_RETURN(tsdbCacheDropSTableColumn(pTsdb, uids, cid, hasPrimaryKey));
32!
1691
      }
1692
    }
1693
    if (uids) taosArrayDestroy(uids);
1,916✔
1694

1695
    tsdbCacheInvalidateSchema(pTsdb, pEntry->uid, -1, pEntry->stbEntry.schemaRow.version);
1,916✔
1696
  }
1697
  if (updStat) {
9,425✔
1698
    int64_t ctbNum = 0;
2,923✔
1699
    int32_t ret = metaGetStbStats(pMeta->pVnode, pEntry->uid, &ctbNum, NULL);
2,923✔
1700
    if (ret < 0) {
2,924!
1701
      metaError("vgId:%d, failed to get stb stats:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
×
1702
                pEntry->uid, tstrerror(ret));
1703
    }
1704
    pMeta->pVnode->config.vndStats.numOfTimeSeries += (ctbNum * deltaCol);
2,924✔
1705
    if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
2,924✔
1706
  }
1707
  metaFetchEntryFree(&pOldEntry);
9,426✔
1708
  return code;
9,425✔
1709
}
1710

1711
static int32_t metaHandleChildTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
681✔
1712
  int32_t code = TSDB_CODE_SUCCESS;
681✔
1713

1714
  SMetaEntry *pOldEntry = NULL;
681✔
1715
  SMetaEntry *pSuperEntry = NULL;
681✔
1716

1717
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
681✔
1718
  if (code) {
681!
1719
    metaErr(TD_VID(pMeta->pVnode), code);
×
1720
    return code;
×
1721
  }
1722

1723
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
681✔
1724
  if (code) {
681!
1725
    metaErr(TD_VID(pMeta->pVnode), code);
×
1726
    metaFetchEntryFree(&pOldEntry);
×
1727
    return code;
×
1728
  }
1729

1730
  SMetaHandleParam param = {
681✔
1731
      .pEntry = pEntry,
1732
      .pOldEntry = pOldEntry,
1733
      .pSuperEntry = pSuperEntry,
1734
  };
1735

1736
  metaWLock(pMeta);
681✔
1737
  code = metaHandleChildTableUpdateImpl(pMeta, &param);
681✔
1738
  metaULock(pMeta);
681✔
1739
  if (code) {
681!
1740
    metaErr(TD_VID(pMeta->pVnode), code);
×
1741
    metaFetchEntryFree(&pOldEntry);
×
1742
    metaFetchEntryFree(&pSuperEntry);
×
1743
    return code;
×
1744
  }
1745

1746
  metaFetchEntryFree(&pOldEntry);
681✔
1747
  metaFetchEntryFree(&pSuperEntry);
681✔
1748
  return code;
681✔
1749
}
1750

1751
static int32_t metaHandleNormalTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
220✔
1752
  int32_t     code = TSDB_CODE_SUCCESS;
220✔
1753
  SMetaEntry *pOldEntry = NULL;
220✔
1754

1755
  // fetch old entry
1756
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
220✔
1757
  if (code) {
220!
1758
    metaErr(TD_VID(pMeta->pVnode), code);
×
1759
    return code;
×
1760
  }
1761

1762
  // handle update
1763
  SMetaHandleParam param = {
220✔
1764
      .pEntry = pEntry,
1765
      .pOldEntry = pOldEntry,
1766
  };
1767
  metaWLock(pMeta);
220✔
1768
  code = metaHandleNormalTableUpdateImpl(pMeta, &param);
220✔
1769
  metaULock(pMeta);
220✔
1770
  if (code) {
220!
1771
    metaErr(TD_VID(pMeta->pVnode), code);
×
1772
    metaFetchEntryFree(&pOldEntry);
×
1773
    return code;
×
1774
  }
1775

1776
  // do other stuff
1777
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) &&
220✔
1778
      pEntry->ntbEntry.schemaRow.version != pOldEntry->ntbEntry.schemaRow.version) {
6!
1779
#if 0
1780
    {  // for add column
1781
      int16_t cid = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].colId;
1782
      int8_t  col_type = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].type;
1783
      int32_t ret = tsdbCacheNewNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, col_type);
1784
      if (ret < 0) {
1785
        terrno = ret;
1786
        goto _err;
1787
      }
1788
    }
1789
    {  // for drop column
1790

1791
      if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
1792
        int16_t cid = pColumn->colId;
1793

1794
        if (tsdbCacheDropNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, hasPrimayKey) != 0) {
1795
          metaError("vgId:%d, failed to drop ntable column:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name,
1796
                    entry.uid);
1797
        }
1798
        tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, entry.uid, pSchema->version);
1799
      }
1800
    }
1801
    }
1802
#endif
1803
    tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, pEntry->uid, pEntry->ntbEntry.schemaRow.version);
6✔
1804
  }
1805
  int32_t deltaCol = pEntry->ntbEntry.schemaRow.nCols - pOldEntry->ntbEntry.schemaRow.nCols;
220✔
1806
  pMeta->pVnode->config.vndStats.numOfNTimeSeries += deltaCol;  
220✔
1807
  if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
220✔
1808
  metaFetchEntryFree(&pOldEntry);
220✔
1809
  return code;
220✔
1810
}
1811

1812
static int32_t metaHandleSuperTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
2,137✔
1813
  int32_t     code = TSDB_CODE_SUCCESS;
2,137✔
1814
  SArray     *childList = NULL;
2,137✔
1815
  SMetaEntry *pOldEntry = NULL;
2,137✔
1816

1817
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
2,137✔
1818
  if (code) {
2,141!
1819
    metaErr(TD_VID(pMeta->pVnode), code);
×
1820
    return code;
×
1821
  }
1822

1823
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childList);
2,141✔
1824
  if (code) {
2,140!
1825
    metaErr(TD_VID(pMeta->pVnode), code);
×
1826
    metaFetchEntryFree(&pOldEntry);
×
1827
    return code;
×
1828
  }
1829

1830
  if (tsdbCacheDropSubTables(pMeta->pVnode->pTsdb, childList, pEntry->uid) < 0) {
2,140!
1831
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
×
1832
              pEntry->uid, tstrerror(terrno));
1833
  }
1834

1835
  // loop to drop all child tables
1836
  for (int32_t i = 0; i < taosArrayGetSize(childList); i++) {
5,566✔
1837
    SMetaEntry childEntry = {
6,842✔
1838
        .version = pEntry->version,
3,421✔
1839
        .uid = *(tb_uid_t *)taosArrayGet(childList, i),
3,421✔
1840
        .type = -TSDB_CHILD_TABLE,
1841
    };
1842

1843
    code = metaHandleChildTableDrop(pMeta, &childEntry, true);
3,421✔
1844
    if (code) {
3,424!
1845
      metaErr(TD_VID(pMeta->pVnode), code);
×
1846
    }
1847
  }
1848

1849
  // do drop super table
1850
  SMetaHandleParam param = {
2,141✔
1851
      .pEntry = pEntry,
1852
      .pOldEntry = pOldEntry,
1853
  };
1854
  metaWLock(pMeta);
2,141✔
1855
  code = metaHandleSuperTableDropImpl(pMeta, &param);
2,142✔
1856
  metaULock(pMeta);
2,141✔
1857
  if (code) {
2,142!
1858
    metaErr(TD_VID(pMeta->pVnode), code);
×
1859
    taosArrayDestroy(childList);
×
1860
    metaFetchEntryFree(&pOldEntry);
×
1861
    return code;
×
1862
  }
1863

1864
  // do other stuff
1865
  metaUpdTimeSeriesNum(pMeta);
2,142✔
1866

1867
  // free resource and return
1868
  taosArrayDestroy(childList);
2,141✔
1869
  metaFetchEntryFree(&pOldEntry);
2,142✔
1870
  return code;
2,142✔
1871
}
1872

1873
int32_t metaHandleEntry2(SMeta *pMeta, const SMetaEntry *pEntry) {
186,606✔
1874
  int32_t   code = TSDB_CODE_SUCCESS;
186,606✔
1875
  int32_t   vgId = TD_VID(pMeta->pVnode);
186,606✔
1876
  SMetaInfo info = {0};
186,606✔
1877
  int8_t    type = pEntry->type > 0 ? pEntry->type : -pEntry->type;
186,606✔
1878

1879
  if (NULL == pMeta || NULL == pEntry) {
186,606!
1880
    metaError("%s failed at %s:%d since invalid parameter", __func__, __FILE__, __LINE__);
×
1881
    return TSDB_CODE_INVALID_PARA;
×
1882
  }
1883

1884
  if (pEntry->type > 0) {
186,650✔
1885
    bool isExist = false;
182,550✔
1886
    if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) {
182,550✔
1887
      isExist = true;
10,328✔
1888
    }
1889

1890
    switch (type) {
182,591✔
1891
      case TSDB_SUPER_TABLE: {
35,930✔
1892
        if (isExist) {
35,930✔
1893
          code = metaHandleSuperTableUpdate(pMeta, pEntry);
9,427✔
1894
        } else {
1895
          code = metaHandleSuperTableCreate(pMeta, pEntry);
26,503✔
1896
        }
1897
        break;
35,941✔
1898
      }
1899
      case TSDB_CHILD_TABLE: {
132,501✔
1900
        if (isExist) {
132,501✔
1901
          code = metaHandleChildTableUpdate(pMeta, pEntry);
681✔
1902
        } else {
1903
          code = metaHandleChildTableCreate(pMeta, pEntry);
131,820✔
1904
        }
1905
        break;
132,491✔
1906
      }
1907
      case TSDB_NORMAL_TABLE: {
14,157✔
1908
        if (isExist) {
14,157✔
1909
          code = metaHandleNormalTableUpdate(pMeta, pEntry);
220✔
1910
        } else {
1911
          code = metaHandleNormalTableCreate(pMeta, pEntry);
13,937✔
1912
        }
1913
        break;
14,157✔
1914
      }
1915
      default: {
3✔
1916
        code = TSDB_CODE_INVALID_PARA;
3✔
1917
        break;
3✔
1918
      }
1919
    }
1920
  } else {
1921
    switch (type) {
4,100!
1922
      case TSDB_SUPER_TABLE: {
2,137✔
1923
        code = metaHandleSuperTableDrop(pMeta, pEntry);
2,137✔
1924
        break;
2,142✔
1925
      }
1926
      case TSDB_CHILD_TABLE: {
278✔
1927
        code = metaHandleChildTableDrop(pMeta, pEntry, false);
278✔
1928
        break;
278✔
1929
      }
1930
      case TSDB_NORMAL_TABLE: {
1,690✔
1931
        code = metaHandleNormalTableDrop(pMeta, pEntry);
1,690✔
1932
        break;
1,690✔
1933
      }
UNCOV
1934
      default: {
×
UNCOV
1935
        code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1936
        break;
×
1937
      }
1938
    }
1939
  }
1940

1941
  if (TSDB_CODE_SUCCESS == code) {
186,697!
1942
    pMeta->changed = true;
186,698✔
1943
    metaDebug("vgId:%d, %s success, version:%" PRId64 " type:%d uid:%" PRId64 " name:%s", vgId, __func__,
186,698✔
1944
              pEntry->version, pEntry->type, pEntry->uid, pEntry->type > 0 ? pEntry->name : "");
1945
  } else {
UNCOV
1946
    metaErr(vgId, code);
×
1947
  }
1948
  TAOS_RETURN(code);
186,697✔
1949
}
1950

1951
void metaHandleSyncEntry(SMeta *pMeta, const SMetaEntry *pEntry) {
5,940✔
1952
  int32_t code = TSDB_CODE_SUCCESS;
5,940✔
1953
  code = metaHandleEntry2(pMeta, pEntry);
5,940✔
1954
  if (code) {
5,940!
1955
    metaErr(TD_VID(pMeta->pVnode), code);
×
1956
  }
1957
  return;
5,940✔
1958
}
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