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

taosdata / TDengine / #4804

16 Oct 2025 10:33AM UTC coverage: 61.259% (+0.1%) from 61.147%
#4804

push

travis-ci

happyguoxy
Merge branch 'cover/3.0' of github.com:taosdata/TDengine into cover/3.0

156021 of 324369 branches covered (48.1%)

Branch coverage included in aggregate %.

79 of 100 new or added lines in 19 files covered. (79.0%)

3318 existing lines in 125 files now uncovered.

207798 of 269534 relevant lines covered (77.1%)

168909799.07 hits per line

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

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

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

14
extern SDmNotifyHandle dmNotifyHdl;
15

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

23
static void    metaTimeSeriesNotifyCheck(SMeta *pMeta);
24
static int32_t 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) {
280,014,813✔
67
  int32_t code = TSDB_CODE_SUCCESS;
280,014,813✔
68
  void   *value = NULL;
280,014,813✔
69
  int32_t valueSize = 0;
280,033,949✔
70

71
  // search uid index
72
  code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &value, &valueSize);
280,050,476✔
73
  if (TSDB_CODE_SUCCESS != code) {
280,044,847!
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 = {
280,044,847✔
80
      .version = ((SUidIdxVal *)value)->version,
280,040,607✔
81
      .uid = uid,
82
  };
83
  tdbFreeClear(value);
280,056,422✔
84

85
  code = tdbTbGet(pMeta->pTbDb, &key, sizeof(key), &value, &valueSize);
280,040,684✔
86
  if (TSDB_CODE_SUCCESS != code) {
280,068,050!
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};
280,068,050✔
94
  SMetaEntry entry = {0};
280,054,927✔
95

96
  tDecoderInit(&decoder, value, valueSize);
280,076,160✔
97
  code = metaDecodeEntry(&decoder, &entry);
280,084,906✔
98
  if (code) {
280,034,951!
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);
280,034,951✔
107
  if (code) {
280,041,305!
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);
280,041,305✔
116
  tDecoderClear(&decoder);
280,046,307✔
117
  return code;
280,080,970✔
118
}
119

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

125
  code = tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &value, &valueSize);
117,655,046!
126
  if (TSDB_CODE_SUCCESS != code) {
117,624,848!
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;
117,624,848✔
131
  tdbFreeClear(value);
117,634,102✔
132

133
  code = metaFetchEntryByUid(pMeta, uid, ppEntry);
117,624,313✔
134
  if (TSDB_CODE_SUCCESS != code) {
117,628,614!
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;
117,628,614✔
139
}
140

141
void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); }
280,176,431✔
142

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

147
  int32_t  code = TSDB_CODE_SUCCESS;
152,457,639✔
148
  int32_t  vgId = TD_VID(pMeta->pVnode);
152,457,639✔
149
  void    *value = NULL;
152,460,522✔
150
  int32_t  valueSize = 0;
152,460,522✔
151
  SEncoder encoder = {0};
152,460,522✔
152
  STbDbKey key = {
152,480,040✔
153
      .version = pEntry->version,
152,458,101✔
154
      .uid = pEntry->uid,
152,476,131✔
155
  };
156

157
  // encode entry
158
  tEncodeSize(metaEncodeEntry, pEntry, valueSize, code);
152,494,060!
159
  if (code != 0) {
152,359,117!
160
    metaErr(vgId, code);
×
161
    return code;
×
162
  }
163

164
  value = taosMemoryMalloc(valueSize);
152,359,117!
165
  if (NULL == value) {
152,316,902!
166
    metaErr(vgId, terrno);
×
167
    return terrno;
×
168
  }
169

170
  tEncoderInit(&encoder, value, valueSize);
152,316,902✔
171
  code = metaEncodeEntry(&encoder, pEntry);
152,453,618✔
172
  if (code) {
152,411,341!
173
    metaErr(vgId, code);
×
174
    tEncoderClear(&encoder);
×
175
    taosMemoryFree(value);
×
176
    return code;
×
177
  }
178
  tEncoderClear(&encoder);
152,411,341✔
179

180
  // put to tdb
181
  if (META_TABLE_OP_INSERT == op) {
152,436,389✔
182
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
116,983,011✔
183
  } else if (META_TABLE_OP_UPDATA == op) {
35,453,378✔
184
    code = tdbTbUpsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
20,854,701✔
185
  } else if (META_TABLE_OP_DELETE == op) {
14,598,677!
186
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
14,598,677✔
187
  } else {
188
    code = TSDB_CODE_INVALID_PARA;
×
189
  }
190
  if (TSDB_CODE_SUCCESS != code) {
152,426,850!
191
    metaErr(vgId, code);
×
192
  }
193
  taosMemoryFree(value);
152,443,907!
194
  return code;
152,445,308✔
195
}
196

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

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

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

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

217
  const SMetaEntry     *pEntry = pParam->pEntry;
29,888,517✔
218
  const SSchemaWrapper *pSchema = NULL;
29,837,877✔
219
  if (pEntry->type == TSDB_SUPER_TABLE) {
29,837,877✔
220
    pSchema = &pEntry->stbEntry.schemaRow;
18,630,914✔
221
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
11,265,050!
222
    pSchema = &pEntry->ntbEntry.schemaRow;
11,265,050✔
223
  } else {
224
    return TSDB_CODE_INVALID_PARA;
×
225
  }
226
  SSkmDbKey key = {
29,906,035✔
227
      .uid = pEntry->uid,
29,891,052✔
228
      .sver = pSchema->version,
29,885,202✔
229
  };
230

231
  // encode schema
232
  tEncodeSize(tEncodeSSchemaWrapper, pSchema, valueSize, code);
59,801,921!
233
  if (TSDB_CODE_SUCCESS != code) {
29,854,936!
234
    metaErr(vgId, code);
×
235
    return code;
×
236
  }
237

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

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

254
  // put to tdb
255
  if (META_TABLE_OP_INSERT == op) {
29,866,106✔
256
    code = tdbTbInsert(pMeta->pSkmDb, &key, sizeof(key), value, valueSize, pMeta->txn);
288✔
257
  } else if (META_TABLE_OP_UPDATA == op) {
29,867,627!
258
    code = tdbTbUpsert(pMeta->pSkmDb, &key, sizeof(key), value, valueSize, pMeta->txn);
29,867,627✔
259
  } else {
260
    code = TSDB_CODE_INVALID_PARA;
×
261
  }
262
  if (TSDB_CODE_SUCCESS != code) {
29,907,892!
263
    metaErr(vgId, code);
×
264
  }
265
  taosMemoryFree(value);
29,889,018!
266
  return code;
29,890,851✔
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,
42,957,043✔
274
                                                 const SSchema *pOldColumn, const SSchema *pNewColumn) {
275
  int32_t code = TSDB_CODE_SUCCESS;
42,957,043✔
276

277
  const SMetaEntry *pEntry = pParam->pEntry;
42,957,043✔
278
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
42,973,048✔
279
  enum { ADD_INDEX, DROP_INDEX } action;
280

281
  if (pOldColumn && pNewColumn) {
42,962,306✔
282
    if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
41,888,574✔
283
      return TSDB_CODE_SUCCESS;
1,891,460✔
284
    } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) {
40,009,288✔
285
      action = DROP_INDEX;
35,467✔
286
    } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
39,960,379!
287
      action = ADD_INDEX;
11,807✔
288
    } else {
289
      return TSDB_CODE_SUCCESS;
39,945,985✔
290
    }
291
  } else if (pOldColumn) {
1,073,732✔
292
    if (IS_IDX_ON(pOldColumn)) {
402,716✔
293
      action = DROP_INDEX;
12,882✔
294
    } else {
295
      return TSDB_CODE_SUCCESS;
385,436✔
296
    }
297
  } else {
298
    if (IS_IDX_ON(pNewColumn)) {
671,016!
299
      action = ADD_INDEX;
×
300
    } else {
301
      return TSDB_CODE_SUCCESS;
674,071✔
302
    }
303
  }
304

305
  // fetch all child tables
306
  SArray *childTables = 0;
60,156✔
307
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childTables);
60,641✔
308
  if (code) {
60,641!
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++) {
436,663✔
315
    int64_t uid = *(int64_t *)taosArrayGet(childTables, i);
376,645✔
316

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

326
    STagIdxKey *pTagIdxKey = NULL;
376,645✔
327
    int32_t     tagIdxKeySize = 0;
376,645✔
328

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

369
  taosArrayDestroy(childTables);
60,641✔
370
  return code;
60,641✔
371
}
372

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

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

381
  if (pOldColumn && pNewColumn) {
348,146✔
382
    return TSDB_CODE_SUCCESS;
334,108✔
383
  } else if (pOldColumn) {
14,038✔
384
    action = DROP_COLUMN;
6,030✔
385
  } else {
386
    action = ADD_COLUMN;
8,008✔
387
  }
388

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

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

410
    SMetaHandleParam param = {.pEntry = pChildEntry};
14,907✔
411

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

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

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

448
  taosArrayDestroy(childTables);
14,038✔
449
  return code;
14,038✔
450
}
451

452
static int32_t metaUpdateSuperTableTagSchema(SMeta *pMeta, const SMetaHandleParam *pParam) {
1,968,684✔
453
  int32_t               code = TSDB_CODE_SUCCESS;
1,968,684✔
454
  const SMetaEntry     *pEntry = pParam->pEntry;
1,968,684✔
455
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
1,978,099✔
456
  const SSchemaWrapper *pNewTagSchema = &pEntry->stbEntry.schemaTag;
1,977,829✔
457
  const SSchemaWrapper *pOldTagSchema = &pOldEntry->stbEntry.schemaTag;
1,976,266✔
458

459
  int32_t iOld = 0, iNew = 0;
1,976,417✔
460
  for (; iOld < pOldTagSchema->nCols && iNew < pNewTagSchema->nCols;) {
44,156,861✔
461
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
42,183,799✔
462
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
42,182,765✔
463

464
    if (pOldColumn->colId == pNewColumn->colId) {
42,168,676✔
465
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
41,877,396✔
466
      if (code) {
41,882,887✔
467
        metaErr(TD_VID(pMeta->pVnode), code);
346!
468
        return code;
×
469
      }
470

471
      iOld++;
41,882,541✔
472
      iNew++;
41,882,541✔
473
    } else if (pOldColumn->colId < pNewColumn->colId) {
298,916!
474
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
295,185✔
475
      if (code) {
296,890!
476
        metaErr(TD_VID(pMeta->pVnode), code);
×
477
        return code;
×
478
      }
479

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

488
      iNew++;
×
489
    }
490
  }
491

492
  for (; iOld < pOldTagSchema->nCols; iOld++) {
2,076,695✔
493
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
102,109✔
494
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
103,133✔
495
    if (code) {
101,107!
496
      metaErr(TD_VID(pMeta->pVnode), code);
×
497
      return code;
×
498
    }
499
  }
500

501
  for (; iNew < pNewTagSchema->nCols; iNew++) {
2,641,391✔
502
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
676,858✔
503
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, NULL, pNewColumn);
670,226✔
504
    if (code) {
671,642!
505
      metaErr(TD_VID(pMeta->pVnode), code);
×
506
      return code;
×
507
    }
508
  }
509

510
  return code;
1,965,546✔
511
}
512

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

520
  int32_t iOld = 0, iNew = 0;
18,090✔
521
  for (; iOld < pOldRowSchema->nCols && iNew < pNewRowSchema->nCols;) {
356,202✔
522
    SSchema *pOldColumn = pOldRowSchema->pSchema + iOld;
338,112✔
523
    SSchema *pNewColumn = pNewRowSchema->pSchema + iNew;
338,112✔
524

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

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

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

549
      iNew++;
×
550
    }
551
  }
552

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

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

571
  return code;
18,090✔
572
}
573

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

577
  const SMetaEntry *pEntry = pParam->pEntry;
31,831,165✔
578
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
31,929,390✔
579

580
  if (NULL == pOldEntry) {
31,907,043✔
581
    return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
17,370,801✔
582
  }
583

584
  if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
14,536,242✔
585
    // check row schema
586
    if (pOldEntry->ntbEntry.schemaRow.version != pEntry->ntbEntry.schemaRow.version) {
5,648,429✔
587
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
5,594,318✔
588
    }
589
  } else if (pEntry->type == TSDB_SUPER_TABLE) {
8,900,694!
590
    // check row schema
591
    if (pOldEntry->stbEntry.schemaRow.version != pEntry->stbEntry.schemaRow.version) {
8,918,603✔
592
      if (TABLE_IS_VIRTUAL(pEntry->flags)) {
6,936,189✔
593
        return metaUpdateSuperTableRowSchema(pMeta, pParam);
18,090✔
594
      } else {
595
        return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
6,917,985✔
596
      }
597
    }
598

599
    // check tag schema
600
    code = metaUpdateSuperTableTagSchema(pMeta, pParam);
1,973,480✔
601
    if (code) {
1,965,211!
UNCOV
602
      metaErr(TD_VID(pMeta->pVnode), code);
×
603
      return code;
×
604
    }
605

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

610
  return TSDB_CODE_SUCCESS;
1,997,287✔
611
}
612

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

618
// Uid Index
619
static void metaBuildEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) {
137,807,212✔
620
  pInfo->uid = pEntry->uid;
137,807,212✔
621
  pInfo->version = pEntry->version;
137,806,157✔
622
  if (pEntry->type == TSDB_SUPER_TABLE) {
137,817,299✔
623
    pInfo->suid = pEntry->uid;
20,602,803✔
624
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
20,589,769✔
625
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
117,191,262✔
626
    pInfo->suid = pEntry->ctbEntry.suid;
105,910,127✔
627
    pInfo->skmVer = 0;
105,932,334✔
628
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
11,293,397!
629
    pInfo->suid = 0;
11,293,397✔
630
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
11,293,397✔
631
  }
632
}
137,846,473✔
633

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

638
  const SMetaEntry *pEntry = pParam->pEntry;
137,825,972✔
639

640
  // update cache
641
  SMetaInfo info = {0};
137,827,096✔
642
  metaBuildEntryInfo(pEntry, &info);
137,830,812✔
643
  code = metaCacheUpsert(pMeta, &info);
137,788,872✔
644
  if (code) {
137,826,584!
645
    metaErr(vgId, code);
×
646
  }
647

648
  // put to tdb
649
  SUidIdxVal value = {
137,826,584✔
650
      .suid = info.suid,
137,806,520✔
651
      .skmVer = info.skmVer,
137,773,174✔
652
      .version = pEntry->version,
137,806,520✔
653
  };
654
  if (META_TABLE_OP_INSERT == op) {
137,773,174✔
655
    code = tdbTbInsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
116,969,402✔
656
  } else if (META_TABLE_OP_UPDATA == op) {
20,803,772!
657
    code = tdbTbUpsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
20,824,935✔
658
  }
659
  return code;
137,840,475✔
660
}
661

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

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

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

673
  const SMetaEntry *pEntry = pParam->pOldEntry;
15,773,448✔
674

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

681
  // delete cache
682
  (void)metaCacheDrop(pMeta, pEntry->uid);
15,774,133✔
683
  return code;
15,773,178✔
684
}
685

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

690
  const SMetaEntry *pEntry = pParam->pEntry;
116,992,672✔
691

692
  if (META_TABLE_OP_INSERT == op) {
117,019,670!
693
    code = tdbTbInsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
117,019,670!
694
                       pMeta->txn);
695
  } else if (META_TABLE_OP_UPDATA == op) {
×
696
    code = tdbTbUpsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
×
697
                       pMeta->txn);
698
  } else {
699
    code = TSDB_CODE_INVALID_PARA;
×
700
  }
701
  if (code) {
117,015,684!
702
    metaErr(TD_VID(pMeta->pVnode), code);
×
703
  }
704
  return code;
117,019,390✔
705
}
706

707
static int32_t metaNameIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
116,999,669✔
708
  int32_t code = TSDB_CODE_SUCCESS;
116,999,669✔
709
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
116,999,669✔
710
}
711

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

716
static int32_t metaNameIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
15,773,178✔
717
  int32_t code = TSDB_CODE_SUCCESS;
15,773,178✔
718

719
  const SMetaEntry *pEntry = pParam->pOldEntry;
15,773,178✔
720
  code = tdbTbDelete(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, pMeta->txn);
15,774,174!
721
  if (code) {
15,773,448!
722
    metaErr(TD_VID(pMeta->pVnode), code);
×
723
  }
724
  return code;
15,774,444✔
725
}
726

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

731
  int32_t code = tdbTbInsert(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), NULL, 0, pMeta->txn);
11,725,201✔
732
  if (code) {
11,731,148!
733
    metaErr(TD_VID(pMeta->pVnode), code);
×
734
  }
735
  return code;
11,718,328✔
736
}
737

738
static int32_t metaSUidIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
1,668,159✔
739
  const SMetaEntry *pEntry = pParam->pOldEntry;
1,668,159✔
740

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

748
// Child Index
749
static int32_t metaChildIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
105,327,595✔
750
  int32_t code = TSDB_CODE_SUCCESS;
105,327,595✔
751

752
  const SMetaEntry *pEntry = pParam->pEntry;
105,327,595✔
753

754
  SCtbIdxKey key = {
105,334,477✔
755
      .suid = pEntry->ctbEntry.suid,
105,338,966✔
756
      .uid = pEntry->uid,
105,340,996✔
757
  };
758

759
  if (META_TABLE_OP_INSERT == op) {
105,336,641✔
760
    code = tdbTbInsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
199,255,694✔
761
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
99,626,248✔
762
  } else if (META_TABLE_OP_UPDATA == op) {
5,703,881!
763
    code = tdbTbUpsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
11,407,762✔
764
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
5,703,881✔
765
  } else {
766
    code = TSDB_CODE_INVALID_PARA;
×
767
  }
768
  return code;
105,349,442✔
769
}
770

771
static int32_t metaChildIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
99,627,298✔
772
  return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
99,627,298✔
773
}
774

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

780
  const STag *pNewTags = (const STag *)pEntry->ctbEntry.pTags;
6,313,468✔
781
  const STag *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags;
6,313,468✔
782
  if (pNewTags->len != pOldTags->len || memcmp(pNewTags, pOldTags, pNewTags->len)) {
6,313,468!
783
    return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
5,703,881✔
784
  }
785
  return 0;
609,587✔
786
}
787

788
static int32_t metaChildIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
12,841,920✔
789
  const SMetaEntry *pEntry = pParam->pOldEntry;
12,841,920✔
790

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

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

803
  STagIdxKey *pTagIdxKey = NULL;
123,715,619✔
804
  int32_t     nTagIdxKey;
123,716,503✔
805
  const void *pTagData = NULL;
123,722,710✔
806
  int32_t     nTagData = 0;
123,722,710✔
807

808
  STagVal tagVal = {
123,722,710✔
809
      .cid = pTagColumn->colId,
123,728,096✔
810
  };
811

812
  if (tTagGet((const STag *)pEntry->ctbEntry.pTags, &tagVal)) {
123,726,650✔
813
    if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
113,697,928!
814
      pTagData = tagVal.pData;
20,435,345✔
815
      nTagData = (int32_t)tagVal.nData;
20,435,345✔
816
    } else {
817
      pTagData = &(tagVal.i64);
93,253,711✔
818
      nTagData = tDataTypes[pTagColumn->type].bytes;
93,253,711✔
819
    }
820
  } else {
821
    if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
10,026,897!
822
      nTagData = tDataTypes[pTagColumn->type].bytes;
9,677,024✔
823
    }
824
  }
825

826
  code = metaCreateTagIdxKey(pEntry->ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type,
123,722,695✔
827
                             pEntry->uid, &pTagIdxKey, &nTagIdxKey);
123,707,944✔
828
  if (code) {
123,686,844!
829
    metaErr(TD_VID(pMeta->pVnode), code);
×
830
    return code;
×
831
  }
832

833
  *ppTagIdxKey = pTagIdxKey;
123,686,844✔
834
  *pTagIdxKeySize = nTagIdxKey;
123,700,429✔
835
  return code;
123,706,492✔
836
}
837

838
static void metaFetchTagIdxKeyFree(STagIdxKey **ppTagIdxKey) {
123,725,082✔
839
  metaDestroyTagIdxKey(*ppTagIdxKey);
123,725,082✔
840
  *ppTagIdxKey = NULL;
123,729,833✔
841
}
123,728,548✔
842

843
static int32_t metaTagIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
99,621,888✔
844
  int32_t code = TSDB_CODE_SUCCESS;
99,621,888✔
845

846
  const SMetaEntry *pEntry = pParam->pEntry;
99,621,888✔
847
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
99,632,477✔
848

849
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
99,636,721✔
850
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
100,173,623✔
851
    const SSchema *pTagColumn = &pTagSchema->pSchema[0];
539,643✔
852

853
    STagVal tagVal = {
1,079,286✔
854
        .cid = pTagColumn->colId,
539,643✔
855
    };
856

857
    const void *pTagData = pEntry->ctbEntry.pTags;
539,643✔
858
    int32_t     nTagData = ((const STag *)pEntry->ctbEntry.pTags)->len;
539,643✔
859
    code = metaSaveJsonVarToIdx(pMeta, pEntry, pTagColumn);
539,643✔
860
    if (code) {
539,643!
861
      metaErr(TD_VID(pMeta->pVnode), code);
×
862
    }
863
  } else {
864
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
489,653,932✔
865
      STagIdxKey    *pTagIdxKey = NULL;
390,536,098✔
866
      int32_t        nTagIdxKey;
390,531,378✔
867
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
390,562,207✔
868

869
      if (!IS_IDX_ON(pTagColumn)) {
390,557,926✔
870
        continue;
291,417,268✔
871
      }
872

873
      code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pTagIdxKey, &nTagIdxKey);
99,157,439✔
874
      if (code) {
99,140,531!
875
        metaErr(TD_VID(pMeta->pVnode), code);
×
876
        return code;
×
877
      }
878

879
      code = tdbTbInsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn);
99,140,531✔
880
      if (code) {
99,164,566!
881
        metaErr(TD_VID(pMeta->pVnode), code);
×
882
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
883
        return code;
×
884
      }
885
      metaFetchTagIdxKeyFree(&pTagIdxKey);
99,164,566✔
886
    }
887
  }
888
  return code;
99,639,719✔
889
}
890

891
static int32_t metaTagIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
6,313,468✔
892
  int32_t code = TSDB_CODE_SUCCESS;
6,313,468✔
893

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

901
  if (pNewTags->len == pOldTags->len && !memcmp(pNewTags, pOldTags, pNewTags->len)) {
6,313,468!
902
    return code;
609,587✔
903
  }
904

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

912
    code = metaSaveJsonVarToIdx(pMeta, pEntry, &pTagSchema->pSchema[0]);
950✔
913
    if (code) {
950!
914
      metaErr(TD_VID(pMeta->pVnode), code);
×
915
      return code;
×
916
    }
917
  } else {
918
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
14,956,532✔
919
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
9,253,601✔
920

921
      if (!IS_IDX_ON(pTagColumn)) {
9,253,601✔
922
        continue;
3,551,480✔
923
      }
924

925
      STagIdxKey *pOldTagIdxKey = NULL;
5,702,121✔
926
      int32_t     oldTagIdxKeySize = 0;
5,702,121✔
927
      STagIdxKey *pNewTagIdxKey = NULL;
5,702,121✔
928
      int32_t     newTagIdxKeySize = 0;
5,702,121✔
929

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

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

943
      if (tagIdxKeyCmpr(pOldTagIdxKey, oldTagIdxKeySize, pNewTagIdxKey, newTagIdxKeySize)) {
5,702,121✔
944
        code = tdbTbDelete(pMeta->pTagIdx, pOldTagIdxKey, oldTagIdxKeySize, pMeta->txn);
4,368,806✔
945
        if (code) {
4,368,806!
946
          metaErr(TD_VID(pMeta->pVnode), code);
×
947
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
948
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
949
          return code;
×
950
        }
951

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

961
      metaFetchTagIdxKeyFree(&pOldTagIdxKey);
5,702,121✔
962
      metaFetchTagIdxKeyFree(&pNewTagIdxKey);
5,702,121✔
963
    }
964
  }
965
  return code;
5,703,881✔
966
}
967

968
static int32_t metaTagIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
12,842,916✔
969
  int32_t code = TSDB_CODE_SUCCESS;
12,842,916✔
970

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

978
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
12,842,916✔
979
    pTagColumn = &pTagSchema->pSchema[0];
168,339✔
980
    code = metaDelJsonVarFromIdx(pMeta, pChild, pTagColumn);
168,339✔
981
    if (code) {
168,339!
982
      metaErr(TD_VID(pMeta->pVnode), code);
×
983
    }
984
  } else {
985
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
41,227,648✔
986
      pTagColumn = &pTagSchema->pSchema[i];
28,554,080✔
987
      if (!IS_IDX_ON(pTagColumn)) {
28,552,300✔
988
        continue;
15,762,719✔
989
      }
990

991
      STagIdxKey *pTagIdxKey = NULL;
12,791,361✔
992
      int32_t     nTagIdxKey;
12,791,361✔
993

994
      code = metaFetchTagIdxKey(pMeta, pChild, pTagColumn, &pTagIdxKey, &nTagIdxKey);
12,791,337✔
995
      if (code) {
12,788,423!
996
        metaErr(TD_VID(pMeta->pVnode), code);
×
997
        return code;
×
998
      }
999

1000
      code = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
12,788,423✔
1001
      if (code) {
12,792,357!
1002
        metaErr(TD_VID(pMeta->pVnode), code);
×
1003
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
1004
        return code;
×
1005
      }
1006
      metaFetchTagIdxKeyFree(&pTagIdxKey);
12,792,357✔
1007
    }
1008
  }
1009
  return code;
12,841,907✔
1010
}
1011

1012
// Btime Index
1013
static int32_t metaBtimeIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
119,392,739✔
1014
  int32_t code = TSDB_CODE_SUCCESS;
119,392,739✔
1015

1016
  const SMetaEntry *pEntry;
1017
  if (META_TABLE_OP_DELETE == op) {
119,392,739✔
1018
    pEntry = pParam->pOldEntry;
14,104,280✔
1019
  } else {
1020
    pEntry = pParam->pEntry;
105,288,459✔
1021
  }
1022

1023
  SBtimeIdxKey key = {
119,392,672✔
1024
      .uid = pEntry->uid,
119,407,261✔
1025
  };
1026

1027
  if (TSDB_CHILD_TABLE == pEntry->type || TSDB_VIRTUAL_CHILD_TABLE == pEntry->type) {
119,403,887✔
1028
    key.btime = pEntry->ctbEntry.btime;
112,472,366✔
1029
  } else if (TSDB_NORMAL_TABLE == pEntry->type || TSDB_VIRTUAL_NORMAL_TABLE == pEntry->type) {
6,934,101!
1030
    key.btime = pEntry->ntbEntry.btime;
6,934,101✔
1031
  } else {
1032
    return TSDB_CODE_INVALID_PARA;
×
1033
  }
1034

1035
  if (META_TABLE_OP_INSERT == op) {
119,394,337✔
1036
    code = tdbTbInsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
105,290,057✔
1037
  } else if (META_TABLE_OP_UPDATA == op) {
14,104,280!
1038
    code = tdbTbUpsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
×
1039
  } else if (META_TABLE_OP_DELETE == op) {
14,104,280!
1040
    code = tdbTbDelete(pMeta->pBtimeIdx, &key, sizeof(key), pMeta->txn);
14,104,280✔
1041
  } else {
UNCOV
1042
    code = TSDB_CODE_INVALID_PARA;
×
1043
  }
1044
  if (code) {
119,402,462!
1045
    metaErr(TD_VID(pMeta->pVnode), code);
×
1046
  }
1047
  return code;
119,401,562✔
1048
}
1049

1050
static int32_t metaBtimeIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
105,290,276✔
1051
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
105,290,276✔
1052
}
1053

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

1058
static int32_t metaBtimeIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
14,104,280✔
1059
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_DELETE);
14,104,280✔
1060
}
1061

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

1066
  STtlUpdTtlCtx ctx = {
104,814,409✔
1067
      .uid = pEntry->uid,
104,820,294✔
1068
      .pTxn = pMeta->txn,
104,811,481✔
1069
  };
1070
  if (TSDB_CHILD_TABLE == pEntry->type) {
104,823,446✔
1071
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
99,315,824✔
1072
    ctx.changeTimeMs = pEntry->ctbEntry.btime;
99,317,090✔
1073
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
5,504,652!
1074
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
5,504,652✔
1075
    ctx.changeTimeMs = pEntry->ntbEntry.btime;
5,504,652✔
1076
  } else {
1077
    return TSDB_CODE_INVALID_PARA;
×
1078
  }
1079

1080
  int32_t ret = ttlMgrInsertTtl(pMeta->pTtlMgr, &ctx);
104,826,571✔
1081
  if (ret < 0) {
104,807,583!
1082
    metaError("vgId:%d, failed to insert ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid, tstrerror(ret));
×
1083
  }
1084
  return TSDB_CODE_SUCCESS;
104,804,181✔
1085
}
1086

1087
static int32_t metaTtlIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
104,808,883✔
1088
  return metaTtlIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
104,808,883✔
1089
}
1090

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

1093
static int32_t metaTtlIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
11,464,932✔
1094
  int32_t code = TSDB_CODE_SUCCESS;
11,464,932✔
1095

1096
  const SMetaEntry *pEntry = pParam->pEntry;
11,464,932✔
1097
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
11,464,932✔
1098

1099
  if ((pEntry->type == TSDB_CHILD_TABLE && pOldEntry->ctbEntry.ttlDays != pEntry->ctbEntry.ttlDays) ||
11,464,932✔
1100
      (pEntry->type == TSDB_NORMAL_TABLE && pOldEntry->ntbEntry.ttlDays != pEntry->ntbEntry.ttlDays)) {
11,458,893✔
1101
    code = metaTtlIdxDelete(pMeta, pParam);
12,786✔
1102
    if (code) {
12,786!
1103
      metaErr(TD_VID(pMeta->pVnode), code);
×
1104
    }
1105

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

1112
  return TSDB_CODE_SUCCESS;
11,464,932✔
1113
}
1114

1115
static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
14,030,218✔
1116
  int32_t code = TSDB_CODE_SUCCESS;
14,030,218✔
1117

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

1124
  if (TSDB_CHILD_TABLE == pEntry->type) {
14,030,218✔
1125
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
12,771,128✔
1126
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
1,259,090✔
1127
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
1,225,500✔
1128
  } else {
1129
    code = TSDB_CODE_INVALID_PARA;
33,590✔
1130
  }
1131

1132
  if (TSDB_CODE_SUCCESS == code) {
14,029,222✔
1133
    int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
13,994,612✔
1134
    if (ret < 0) {
13,994,612!
1135
      metaError("vgId:%d, failed to delete ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid,
×
1136
                tstrerror(ret));
1137
    }
1138
  }
1139
  return code;
14,029,222✔
1140
}
1141

1142
static void metaTimeSeriesNotifyCheck(SMeta *pMeta) {
114,630,518✔
1143
#if defined(TD_ENTERPRISE)
1144
  int64_t nTimeSeries = metaGetTimeSeriesNum(pMeta, 0);
114,630,518✔
1145
  int64_t deltaTS = nTimeSeries - pMeta->pVnode->config.vndStats.numOfReportedTimeSeries;
114,630,202✔
1146
  if (deltaTS > tsTimeSeriesThreshold) {
114,648,596✔
1147
    if (0 == atomic_val_compare_exchange_8(&dmNotifyHdl.state, 1, 2)) {
63,637,282✔
1148
      if (tsem_post(&dmNotifyHdl.sem) != 0) {
63,647,746!
1149
        metaError("vgId:%d, failed to post semaphore, errno:%d", TD_VID(pMeta->pVnode), ERRNO);
×
1150
      }
1151
    }
1152
  }
1153
#endif
1154
}
114,685,753✔
1155

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

1214
static int32_t metaHandleSuperTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
11,720,061✔
1215
  int32_t code = TSDB_CODE_SUCCESS;
11,720,061✔
1216

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

1225
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
70,346,272✔
1226
    SMetaTableOp          *op = &ops[i];
58,601,908✔
1227
    const SMetaHandleParam param = {
58,615,797✔
1228
        .pEntry = pEntry,
1229
    };
1230
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
58,603,113✔
1231
    if (TSDB_CODE_SUCCESS != code) {
58,594,718!
1232
      metaErr(TD_VID(pMeta->pVnode), code);
×
1233
      return code;
×
1234
    }
1235
  }
1236

1237
  return code;
11,744,364✔
1238
}
1239
static int32_t metaHandleSuperTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
11,650,607✔
1240
  int32_t code = TSDB_CODE_SUCCESS;
11,650,607✔
1241

1242
  metaWLock(pMeta);
11,650,607✔
1243
  code = metaHandleSuperTableCreateImpl(pMeta, pEntry);
11,733,197✔
1244
  metaULock(pMeta);
11,724,947✔
1245

1246
  if (TSDB_CODE_SUCCESS == code) {
11,729,078!
1247
    pMeta->pVnode->config.vndStats.numOfSTables++;
11,729,078✔
1248

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

1257
static int32_t metaHandleNormalTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
5,497,905✔
1258
  int32_t code = TSDB_CODE_SUCCESS;
5,497,905✔
1259

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

1269
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
38,485,335✔
1270
    SMetaTableOp *op = &ops[i];
32,987,430✔
1271

1272
    SMetaHandleParam param = {
32,987,430✔
1273
        .pEntry = pEntry,
1274
    };
1275

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

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

1288
  // update TDB
1289
  metaWLock(pMeta);
5,497,905✔
1290
  code = metaHandleNormalTableCreateImpl(pMeta, pEntry);
5,497,905✔
1291
  metaULock(pMeta);
5,497,905✔
1292

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

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

1311
static int32_t metaHandleChildTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry, const SMetaEntry *pSuperEntry) {
99,299,878✔
1312
  int32_t code = TSDB_CODE_SUCCESS;
99,299,878✔
1313

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

1324
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
794,458,117✔
1325
    SMetaTableOp *op = &ops[i];
695,143,962✔
1326

1327
    SMetaHandleParam param = {
695,176,430✔
1328
        .pEntry = pEntry,
1329
        .pSuperEntry = pSuperEntry,
1330
    };
1331

1332
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
695,161,883✔
1333
    if (TSDB_CODE_SUCCESS != code) {
695,099,138!
1334
      metaErr(TD_VID(pMeta->pVnode), code);
×
1335
      return code;
×
1336
    }
1337
  }
1338

1339
  if (TSDB_CODE_SUCCESS == code) {
99,314,155✔
1340
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
99,306,726✔
1341
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
99,288,837✔
1342
    if (ret < 0) {
99,297,299!
1343
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1344
    }
1345

1346
    ret = metaTbGroupCacheClear(pMeta, pSuperEntry->uid);
99,297,299✔
1347
    if (ret < 0) {
99,309,779!
1348
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1349
    }
1350
  }
1351
  return code;
99,299,787✔
1352
}
1353

1354
static int32_t metaHandleChildTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
99,281,052✔
1355
  int32_t     code = TSDB_CODE_SUCCESS;
99,281,052✔
1356
  SMetaEntry *pSuperEntry = NULL;
99,281,052✔
1357

1358
  // get the super table entry
1359
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
99,298,146✔
1360
  if (code) {
99,309,591!
1361
    metaErr(TD_VID(pMeta->pVnode), code);
×
1362
    return code;
×
1363
  }
1364

1365
  // update TDB
1366
  metaWLock(pMeta);
99,309,591✔
1367
  code = metaHandleChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
99,305,156✔
1368
  metaULock(pMeta);
99,295,778✔
1369

1370
  // update other stuff
1371
  if (TSDB_CODE_SUCCESS == code) {
99,296,366!
1372
    pMeta->pVnode->config.vndStats.numOfCTables++;
99,296,366✔
1373

1374
    if (!metaTbInFilterCache(pMeta, pSuperEntry->name, 1)) {
99,295,358!
1375
      int32_t nCols = 0;
99,315,712✔
1376
      int32_t ret = metaGetStbStats(pMeta->pVnode, pSuperEntry->uid, 0, &nCols, 0);
99,315,712✔
1377
      if (ret < 0) {
99,307,516!
1378
        metaErr(TD_VID(pMeta->pVnode), ret);
×
1379
      }
1380
      pMeta->pVnode->config.vndStats.numOfTimeSeries += (nCols > 0 ? nCols - 1 : 0);
99,307,516!
1381
    }
1382

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

1391
  } else {
1392
    metaErr(TD_VID(pMeta->pVnode), code);
×
1393
  }
1394
  metaTimeSeriesNotifyCheck(pMeta);
99,303,238✔
1395
  metaFetchEntryFree(&pSuperEntry);
99,316,093✔
1396
  return code;
99,317,332✔
1397
}
1398

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

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

1410
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1,036,962✔
1411
    SMetaTableOp *op = &ops[i];
864,135✔
1412

1413
    SMetaHandleParam param = {
864,135✔
1414
        .pEntry = pEntry,
1415
    };
1416

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

1424
  return code;
172,827✔
1425
}
1426

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

1430
  // update TDB
1431
  metaWLock(pMeta);
172,827✔
1432
  code = metaHandleVirtualNormalTableCreateImpl(pMeta, pEntry);
172,827✔
1433
  metaULock(pMeta);
172,827✔
1434

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

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

1448
  SMetaTableOp ops[] = {
320,000✔
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++) {
2,240,000✔
1458
    SMetaTableOp *op = &ops[i];
1,920,000✔
1459

1460
    SMetaHandleParam param = {
1,920,000✔
1461
        .pEntry = pEntry,
1462
        .pSuperEntry = pSuperEntry,
1463
    };
1464

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

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

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

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

1490
  return code;
320,000✔
1491
}
1492

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

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

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

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

1516
  metaFetchEntryFree(&pSuperEntry);
320,000✔
1517
  return code;
320,000✔
1518
}
1519

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

1523
  SMetaTableOp ops[] = {
1,218,753✔
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++) {
7,312,518✔
1534
    SMetaTableOp *op = &ops[i];
6,093,765✔
1535
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
6,093,765✔
1536
    if (code) {
6,093,765!
1537
      const SMetaEntry *pEntry = pParam->pEntry;
×
1538
      metaErr(TD_VID(pMeta->pVnode), code);
×
1539
    }
1540
  }
1541

1542
  return code;
1,218,753✔
1543
}
1544

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

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

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

1561
  // do the drop
1562
  metaWLock(pMeta);
1,218,753✔
1563
  code = metaHandleNormalTableDropImpl(pMeta, &param);
1,218,753✔
1564
  metaULock(pMeta);
1,218,753✔
1565
  if (code) {
1,218,753!
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--;
1,218,753✔
1573
  pMeta->pVnode->config.vndStats.numOfNTimeSeries -= (pOldEntry->ntbEntry.schemaRow.nCols - 1);
1,218,753✔
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) && pMeta->pVnode->pTsdb) {
1,218,753!
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);
1,218,753✔
1592
  return code;
1,218,753✔
1593
}
1594

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

1598
  const SMetaEntry *pEntry = pParam->pEntry;
12,797,683✔
1599
  const SMetaEntry *pChild = pParam->pOldEntry;
12,797,683✔
1600
  const SMetaEntry *pSuper = pParam->pSuperEntry;
12,796,923✔
1601

1602
  SMetaTableOp ops[] = {
12,797,683✔
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++) {
102,349,247✔
1613
    SMetaTableOp *op = &ops[i];
89,586,174✔
1614

1615
    if (op->table == META_ENTRY_TABLE && superDropped) {
89,587,170✔
1616
      continue;
1,174,771✔
1617
    }
1618

1619
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
88,411,403✔
1620
    if (code) {
88,410,383✔
1621
      metaErr(TD_VID(pMeta->pVnode), code);
33,590!
1622
      return code;
33,590✔
1623
    }
1624
  }
1625

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

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

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

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

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

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

1666
  // do the drop
1667
  metaWLock(pMeta);
12,797,683✔
1668
  code = metaHandleChildTableDropImpl(pMeta, &param, superDropped);
12,798,679✔
1669
  metaULock(pMeta);
12,797,683✔
1670
  if (code) {
12,795,667✔
1671
    metaErr(TD_VID(pMeta->pVnode), code);
33,590!
1672
    metaFetchEntryFree(&pChild);
33,590✔
1673
    metaFetchEntryFree(&pSuper);
33,590✔
1674
    return code;
33,590✔
1675
  }
1676

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

1686
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
12,765,089!
1687
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pChild->uid, pSuper->uid, NULL);
14,976✔
1688
    if (ret < 0) {
14,976!
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);
12,763,333✔
1706
  metaFetchEntryFree(&pSuper);
12,764,093✔
1707
  return code;
12,764,093✔
1708
}
1709

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

1713
  SMetaTableOp ops[] = {
44,616✔
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++) {
223,080✔
1723
    SMetaTableOp *op = &ops[i];
178,464✔
1724
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
178,464✔
1725
    if (code) {
178,464!
1726
      const SMetaEntry *pEntry = pParam->pEntry;
×
1727
      metaErr(TD_VID(pMeta->pVnode), code);
×
1728
    }
1729
  }
1730

1731
  return code;
44,616✔
1732
}
1733

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

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

1745
  SMetaHandleParam param = {
44,616✔
1746
      .pEntry = pEntry,
1747
      .pOldEntry = pOldEntry,
1748
  };
1749

1750
  // do the drop
1751
  metaWLock(pMeta);
44,616✔
1752
  code = metaHandleVirtualNormalTableDropImpl(pMeta, &param);
44,616✔
1753
  metaULock(pMeta);
44,616✔
1754
  if (code) {
44,616!
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--;
44,616✔
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) && pMeta->pVnode->pTsdb) {
44,616!
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);
44,616✔
1780
  return code;
44,616✔
1781
}
1782

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

1786
  const SMetaEntry *pEntry = pParam->pEntry;
44,237✔
1787
  const SMetaEntry *pChild = pParam->pOldEntry;
44,237✔
1788
  const SMetaEntry *pSuper = pParam->pSuperEntry;
44,237✔
1789

1790
  SMetaTableOp ops[] = {
44,237✔
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++) {
309,659✔
1800
    SMetaTableOp *op = &ops[i];
265,422✔
1801

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

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

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

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

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

1830
  return code;
44,237✔
1831
}
1832

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

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

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

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

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

1870
  metaFetchEntryFree(&pChild);
44,237✔
1871
  metaFetchEntryFree(&pSuper);
44,237✔
1872
  return code;
44,237✔
1873
}
1874

1875
int32_t metaGetChildUidsOfSuperTable(SMeta *pMeta, tb_uid_t suid, SArray **childList) {
1,773,899✔
1876
  int32_t code = TSDB_CODE_SUCCESS;
1,773,899✔
1877
  void   *key = NULL;
1,773,899✔
1878
  int32_t keySize = 0;
1,781,274✔
1879
  int32_t c;
1,778,443✔
1880

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

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

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

1905
  for (;;) {
1906
    if (tdbTbcNext(cursor, &key, &keySize, NULL, NULL) < 0) {
3,810,001✔
1907
      break;
1,346,510✔
1908
    }
1909

1910
    if (((SCtbIdxKey *)key)->suid < suid) {
2,463,575✔
1911
      continue;
342,954✔
1912
    } else if (((SCtbIdxKey *)key)->suid > suid) {
2,122,058✔
1913
      break;
436,486✔
1914
    }
1915

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

1925
  tdbTbcClose(cursor);
1,782,996✔
1926
  tdbFreeClear(key);
1,783,266✔
1927
  return code;
1,781,019✔
1928
}
1929

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

1934
  SMetaTableOp ops[] = {
1,667,273✔
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++) {
8,340,795✔
1944
    SMetaTableOp *op = &ops[i];
6,672,636✔
1945

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

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

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

1963
  const SMetaEntry *pEntry = pParam->pEntry;
5,285,532✔
1964

1965
  SMetaTableOp ops[] = {
5,285,532✔
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++) {
26,427,660✔
1972
    SMetaTableOp *op = &ops[i];
21,142,128✔
1973
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
21,142,128✔
1974
    if (code) {
21,142,128!
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;
5,285,532✔
1985
}
1986

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

1990
  const SMetaEntry *pEntry = pParam->pEntry;
337,133✔
1991

1992
  SMetaTableOp ops[] = {
337,133✔
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++) {
1,348,532✔
1998
    SMetaTableOp *op = &ops[i];
1,011,399✔
1999
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
1,011,399✔
2000
    if (code) {
1,011,399!
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;
337,133✔
2011
}
2012

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

2016
  const SMetaEntry *pEntry = pParam->pEntry;
134,068✔
2017
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
134,068✔
2018
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
134,068✔
2019

2020
  SMetaTableOp ops[] = {
134,068✔
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++) {
670,340✔
2028
    SMetaTableOp *op = &ops[i];
536,272✔
2029
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
536,272✔
2030
    if (code) {
536,272!
2031
      metaErr(TD_VID(pMeta->pVnode), code);
×
2032
      return code;
×
2033
    }
2034
  }
2035

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

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

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

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

2053
  const SMetaEntry *pEntry = pParam->pEntry;
6,179,400✔
2054
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
6,179,400✔
2055
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
6,179,400✔
2056

2057
  SMetaTableOp ops[] = {
6,179,400✔
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++) {
37,076,400✔
2066
    SMetaTableOp *op = &ops[i];
30,897,000✔
2067
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
30,897,000✔
2068
    if (code) {
30,897,000!
2069
      metaErr(TD_VID(pMeta->pVnode), code);
×
2070
      return code;
×
2071
    }
2072
  }
2073

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

2078
  if (metaTbGroupCacheClear(pMeta, pSuperEntry->uid) < 0) {
6,179,400!
2079
    metaErr(TD_VID(pMeta->pVnode), code);
×
2080
  }
2081
  return code;
6,179,400✔
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) {
8,911,358✔
2090
  int32_t code = TSDB_CODE_SUCCESS;
8,911,358✔
2091

2092
  const SMetaEntry *pEntry = pParam->pEntry;
8,911,358✔
2093
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
8,920,229✔
2094

2095
  SMetaTableOp ops[] = {
8,920,864✔
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++) {
35,629,849✔
2102
    SMetaTableOp *op = &ops[i];
26,732,830✔
2103
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
26,750,604✔
2104
    if (code) {
26,715,132!
2105
      metaErr(TD_VID(pMeta->pVnode), code);
×
2106
      return code;
×
2107
    }
2108
  }
2109

2110
  if (TSDB_CODE_SUCCESS == code) {
8,897,019!
2111
    metaUpdateStbStats(pMeta, pEntry->uid, 0, pEntry->stbEntry.schemaRow.nCols - pOldEntry->stbEntry.schemaRow.nCols,
8,898,238✔
2112
                       pEntry->stbEntry.keep);
8,900,546✔
2113
  }
2114

2115
  return code;
8,892,740✔
2116
}
2117

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

2121
  SMetaEntry *pOldEntry = NULL;
8,896,806✔
2122

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

2129
  SMetaHandleParam param = {
8,917,055✔
2130
      .pEntry = pEntry,
2131
      .pOldEntry = pOldEntry,
2132
  };
2133
  metaWLock(pMeta);
8,915,696✔
2134
  code = metaHandleSuperTableUpdateImpl(pMeta, &param);
8,915,934✔
2135
  metaULock(pMeta);
8,894,372✔
2136
  if (code) {
8,874,928!
2137
    metaErr(TD_VID(pMeta->pVnode), code);
×
2138
    metaFetchEntryFree(&pOldEntry);
×
2139
    return code;
×
2140
  }
2141

2142
  int     nCols = pEntry->stbEntry.schemaRow.nCols;
8,874,928✔
2143
  int     onCols = pOldEntry->stbEntry.schemaRow.nCols;
8,901,039✔
2144
  int32_t deltaCol = nCols - onCols;
8,909,091✔
2145
  bool    updStat = deltaCol != 0 && !TABLE_IS_VIRTUAL(pEntry->flags) && !metaTbInFilterCache(pMeta, pEntry->name, 1);
8,909,091!
2146

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

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

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

2195
    if (pTsdb) {
52,012!
2196
      tsdbCacheInvalidateSchema(pTsdb, pEntry->uid, -1, pEntry->stbEntry.schemaRow.version);
52,406✔
2197
    }
2198
  }
2199
  if (updStat) {
8,904,004✔
2200
    int64_t ctbNum = 0;
5,582,443✔
2201
    int32_t ret = metaGetStbStats(pMeta->pVnode, pEntry->uid, &ctbNum, 0, 0);
5,584,102✔
2202
    if (ret < 0) {
5,581,968!
2203
      metaError("vgId:%d, failed to get stb stats:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
×
2204
                pEntry->uid, tstrerror(ret));
2205
    }
2206
    pMeta->pVnode->config.vndStats.numOfTimeSeries += (ctbNum * deltaCol);
5,581,968✔
2207
    if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
5,579,866✔
2208
  }
2209
  metaFetchEntryFree(&pOldEntry);
8,904,228✔
2210
  return code;
8,877,230✔
2211
}
2212

2213
static int32_t metaHandleChildTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
6,179,400✔
2214
  int32_t code = TSDB_CODE_SUCCESS;
6,179,400✔
2215

2216
  SMetaEntry *pOldEntry = NULL;
6,179,400✔
2217
  SMetaEntry *pSuperEntry = NULL;
6,179,400✔
2218

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

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

2232
  SMetaHandleParam param = {
6,179,400✔
2233
      .pEntry = pEntry,
2234
      .pOldEntry = pOldEntry,
2235
      .pSuperEntry = pSuperEntry,
2236
  };
2237

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

2248
  metaFetchEntryFree(&pOldEntry);
6,179,400✔
2249
  metaFetchEntryFree(&pSuperEntry);
6,179,400✔
2250
  return code;
6,179,400✔
2251
}
2252

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

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

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

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

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

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

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

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

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

2341
  metaTimeSeriesNotifyCheck(pMeta);
337,133✔
2342
  metaFetchEntryFree(&pOldEntry);
337,133✔
2343
  return code;
337,133✔
2344
}
2345

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

2349
  SMetaEntry *pOldEntry = NULL;
134,068✔
2350
  SMetaEntry *pSuperEntry = NULL;
134,068✔
2351

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

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

2365
  SMetaHandleParam param = {
134,068✔
2366
      .pEntry = pEntry,
2367
      .pOldEntry = pOldEntry,
2368
      .pSuperEntry = pSuperEntry,
2369
  };
2370

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

2381
  metaFetchEntryFree(&pOldEntry);
134,068✔
2382
  metaFetchEntryFree(&pSuperEntry);
134,068✔
2383
  return code;
134,068✔
2384
}
2385

2386
static int32_t metaHandleSuperTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
1,664,546✔
2387
  int32_t     code = TSDB_CODE_SUCCESS;
1,664,546✔
2388
  SArray     *childList = NULL;
1,664,546✔
2389
  SMetaEntry *pOldEntry = NULL;
1,664,546✔
2390

2391
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
1,666,182✔
2392
  if (code) {
1,662,268!
2393
    metaErr(TD_VID(pMeta->pVnode), code);
×
2394
    return code;
×
2395
  }
2396

2397
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childList);
1,662,268✔
2398
  if (code) {
1,666,167!
2399
    metaErr(TD_VID(pMeta->pVnode), code);
×
2400
    metaFetchEntryFree(&pOldEntry);
×
2401
    return code;
×
2402
  }
2403

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

2409
  // loop to drop all child tables
2410
  for (int32_t i = 0; i < taosArrayGetSize(childList); i++) {
2,842,930✔
2411
    SMetaEntry childEntry = {
1,175,767✔
2412
        .version = pEntry->version,
1,175,767✔
2413
        .uid = *(tb_uid_t *)taosArrayGet(childList, i),
1,175,767✔
2414
        .type = -TSDB_CHILD_TABLE,
2415
    };
2416

2417
    code = metaHandleChildTableDrop(pMeta, &childEntry, true);
1,175,767✔
2418
    if (code) {
1,175,767✔
2419
      metaErr(TD_VID(pMeta->pVnode), code);
33,590!
2420
    }
2421
  }
2422

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

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

2441
  // free resource and return
2442
  taosArrayDestroy(childList);
1,668,159✔
2443
  metaFetchEntryFree(&pOldEntry);
1,668,159✔
2444
  return code;
1,668,159✔
2445
}
2446

2447
int32_t metaHandleEntry2(SMeta *pMeta, const SMetaEntry *pEntry) {
152,378,918✔
2448
  int32_t   code = TSDB_CODE_SUCCESS;
152,378,918✔
2449
  int32_t   vgId = TD_VID(pMeta->pVnode);
152,378,918✔
2450
  SMetaInfo info = {0};
152,471,497✔
2451
  int8_t    type = pEntry->type > 0 ? pEntry->type : -pEntry->type;
152,444,092✔
2452

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

2458
  if (pEntry->type > 0) {
152,406,119✔
2459
    bool isExist = false;
137,810,957✔
2460
    if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) {
137,810,957✔
2461
      isExist = true;
20,848,113✔
2462
    }
2463

2464
    switch (type) {
137,833,171!
2465
      case TSDB_SUPER_TABLE: {
20,617,557✔
2466
        if (isExist) {
20,617,557✔
2467
          code = metaHandleSuperTableUpdate(pMeta, pEntry);
8,904,559✔
2468
        } else {
2469
          code = metaHandleSuperTableCreate(pMeta, pEntry);
11,712,998✔
2470
        }
2471
        break;
20,626,474✔
2472
      }
2473
      case TSDB_CHILD_TABLE: {
105,468,149✔
2474
        if (isExist) {
105,468,149✔
2475
          code = metaHandleChildTableUpdate(pMeta, pEntry);
6,179,400✔
2476
        } else {
2477
          code = metaHandleChildTableCreate(pMeta, pEntry);
99,288,749✔
2478
        }
2479
        break;
105,489,789✔
2480
      }
2481
      case TSDB_NORMAL_TABLE: {
10,783,437✔
2482
        if (isExist) {
10,783,437✔
2483
          code = metaHandleNormalTableUpdate(pMeta, pEntry);
5,285,532✔
2484
        } else {
2485
          code = metaHandleNormalTableCreate(pMeta, pEntry);
5,497,905✔
2486
        }
2487
        break;
10,783,437✔
2488
      }
2489
      case TSDB_VIRTUAL_NORMAL_TABLE: {
509,960✔
2490
        if (isExist) {
509,960✔
2491
          code = metaHandleVirtualNormalTableUpdate(pMeta, pEntry);
337,133✔
2492
        } else {
2493
          code = metaHandleVirtualNormalTableCreate(pMeta, pEntry);
172,827✔
2494
        }
2495
        break;
509,960✔
2496
      }
2497
      case TSDB_VIRTUAL_CHILD_TABLE: {
454,068✔
2498
        if (isExist) {
454,068✔
2499
          code = metaHandleVirtualChildTableUpdate(pMeta, pEntry);
134,068✔
2500
        } else {
2501
          code = metaHandleVirtualChildTableCreate(pMeta, pEntry);
320,000✔
2502
        }
2503
        break;
454,068✔
2504
      }
2505
      default: {
×
2506
        code = TSDB_CODE_INVALID_PARA;
×
2507
        break;
×
2508
      }
2509
    }
2510
  } else {
2511
    switch (type) {
14,594,083!
2512
      case TSDB_SUPER_TABLE: {
1,663,565✔
2513
        code = metaHandleSuperTableDrop(pMeta, pEntry);
1,663,565✔
2514
        break;
1,668,159✔
2515
      }
2516
      case TSDB_CHILD_TABLE: {
11,622,912✔
2517
        code = metaHandleChildTableDrop(pMeta, pEntry, false);
11,622,912✔
2518
        break;
11,622,912✔
2519
      }
2520
      case TSDB_NORMAL_TABLE: {
1,218,753✔
2521
        code = metaHandleNormalTableDrop(pMeta, pEntry);
1,218,753✔
2522
        break;
1,218,753✔
2523
      }
2524
      case TSDB_VIRTUAL_NORMAL_TABLE: {
44,616✔
2525
        code = metaHandleVirtualNormalTableDrop(pMeta, pEntry);
44,616✔
2526
        break;
44,616✔
2527
      }
2528
      case TSDB_VIRTUAL_CHILD_TABLE: {
44,237✔
2529
        code = metaHandleVirtualChildTableDrop(pMeta, pEntry, false);
44,237✔
2530
        break;
44,237✔
2531
      }
2532
      default: {
×
2533
        code = TSDB_CODE_INVALID_PARA;
×
2534
        break;
×
2535
      }
2536
    }
2537
  }
2538

2539
  if (TSDB_CODE_SUCCESS == code) {
152,462,405!
2540
    pMeta->changed = true;
152,469,876✔
2541
    metaDebug("vgId:%d, index:%" PRId64 ", handle meta entry success, type:%d tb:%s uid:%" PRId64, vgId,
152,488,213✔
2542
              pEntry->version, pEntry->type, pEntry->type > 0 ? pEntry->name : "", pEntry->uid);
2543
  } else {
UNCOV
2544
    metaErr(vgId, code);
×
2545
  }
2546
  TAOS_RETURN(code);
152,485,567✔
2547
}
2548

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