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

taosdata / TDengine / #5015

03 Apr 2026 03:59PM UTC coverage: 72.289% (+0.03%) from 72.256%
#5015

push

travis-ci

web-flow
merge: from main to 3.0 branch #35067

4055 of 5985 new or added lines in 68 files covered. (67.75%)

13044 existing lines in 149 files now uncovered.

257390 of 356056 relevant lines covered (72.29%)

130247228.09 hits per line

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

76.72
/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) {
205,212,808✔
67
  int32_t code = TSDB_CODE_SUCCESS;
205,212,808✔
68
  void   *value = NULL;
205,212,808✔
69
  int32_t valueSize = 0;
205,233,608✔
70

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

78
  // search entry table
79
  STbDbKey key = {
205,242,121✔
80
      .version = ((SUidIdxVal *)value)->version,
205,232,200✔
81
      .uid = uid,
82
  };
83
  tdbFreeClear(value);
205,259,583✔
84

85
  code = tdbTbGet(pMeta->pTbDb, &key, sizeof(key), &value, &valueSize);
205,226,032✔
86
  if (TSDB_CODE_SUCCESS != code) {
205,233,324✔
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};
205,233,324✔
94
  SMetaEntry entry = {0};
205,235,655✔
95

96
  tDecoderInit(&decoder, value, valueSize);
205,240,043✔
97
  code = metaDecodeEntry(&decoder, &entry);
205,252,780✔
98
  if (code) {
205,210,229✔
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);
205,210,229✔
107
  if (code) {
205,227,490✔
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);
205,227,490✔
116
  tDecoderClear(&decoder);
205,219,945✔
117
  return code;
205,251,664✔
118
}
119

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

125
  code = tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &value, &valueSize);
83,316,725✔
126
  if (TSDB_CODE_SUCCESS != code) {
83,294,049✔
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;
83,294,049✔
131
  tdbFreeClear(value);
83,301,648✔
132

133
  code = metaFetchEntryByUid(pMeta, uid, ppEntry);
83,294,969✔
134
  if (TSDB_CODE_SUCCESS != code) {
83,293,363✔
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;
83,293,363✔
139
}
140

141
void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); }
205,319,213✔
142

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

147
  int32_t  code = TSDB_CODE_SUCCESS;
107,017,851✔
148
  int32_t  vgId = TD_VID(pMeta->pVnode);
107,017,851✔
149
  void    *value = NULL;
107,032,923✔
150
  int32_t  valueSize = 0;
107,032,923✔
151
  SEncoder encoder = {0};
107,032,923✔
152
  STbDbKey key = {
107,035,233✔
153
      .version = pEntry->version,
107,008,847✔
154
      .uid = pEntry->uid,
107,020,273✔
155
  };
156

157
  // encode entry
158
  tEncodeSize(metaEncodeEntry, pEntry, valueSize, code);
107,014,421✔
159
  if (code != 0) {
106,916,977✔
160
    metaErr(vgId, code);
×
161
    return code;
×
162
  }
163

164
  value = taosMemoryMalloc(valueSize);
106,916,977✔
165
  if (NULL == value) {
106,985,522✔
166
    metaErr(vgId, terrno);
×
167
    return terrno;
×
168
  }
169

170
  tEncoderInit(&encoder, value, valueSize);
106,985,522✔
171
  code = metaEncodeEntry(&encoder, pEntry);
106,994,603✔
172
  if (code) {
106,953,869✔
173
    metaErr(vgId, code);
×
174
    tEncoderClear(&encoder);
×
175
    taosMemoryFree(value);
×
176
    return code;
×
177
  }
178
  tEncoderClear(&encoder);
106,953,869✔
179

180
  // put to tdb
181
  if (META_TABLE_OP_INSERT == op) {
107,002,399✔
182
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
84,297,761✔
183
  } else if (META_TABLE_OP_UPDATA == op) {
22,704,638✔
184
    code = tdbTbUpsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
20,041,424✔
185
  } else if (META_TABLE_OP_DELETE == op) {
2,663,214✔
186
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
2,663,895✔
187
  } else {
188
    code = TSDB_CODE_INVALID_PARA;
×
189
  }
190
  if (TSDB_CODE_SUCCESS != code) {
107,000,065✔
191
    metaErr(vgId, code);
×
192
  }
193
  taosMemoryFree(value);
107,006,121✔
194
  return code;
106,988,823✔
195
}
196

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

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

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

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

217
  const SMetaEntry     *pEntry = pParam->pEntry;
23,720,816✔
218
  const SSchemaWrapper *pSchema = NULL;
23,677,606✔
219
  if (pEntry->type == TSDB_SUPER_TABLE) {
23,677,606✔
220
    pSchema = &pEntry->stbEntry.schemaRow;
12,075,868✔
221
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
11,617,296✔
222
    pSchema = &pEntry->ntbEntry.schemaRow;
11,617,296✔
223
  } else {
224
    return TSDB_CODE_INVALID_PARA;
×
225
  }
226
  SSkmDbKey key = {
23,687,085✔
227
      .uid = pEntry->uid,
23,715,876✔
228
      .sver = pSchema->version,
23,714,879✔
229
  };
230

231
  // encode schema
232
  tEncodeSize(tEncodeSSchemaWrapper, pSchema, valueSize, code);
47,416,400✔
233
  if (TSDB_CODE_SUCCESS != code) {
23,702,236✔
234
    metaErr(vgId, code);
×
235
    return code;
×
236
  }
237

238
  value = taosMemoryMalloc(valueSize);
23,702,236✔
239
  if (NULL == value) {
23,671,058✔
240
    metaErr(vgId, terrno);
×
241
    return terrno;
×
242
  }
243

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

254
  // put to tdb
255
  if (META_TABLE_OP_INSERT == op) {
23,710,789✔
256
    code = tdbTbInsert(pMeta->pSkmDb, &key, sizeof(key), value, valueSize, pMeta->txn);
322✔
257
  } else if (META_TABLE_OP_UPDATA == op) {
23,711,877✔
258
    code = tdbTbUpsert(pMeta->pSkmDb, &key, sizeof(key), value, valueSize, pMeta->txn);
23,711,877✔
259
  } else {
260
    code = TSDB_CODE_INVALID_PARA;
×
261
  }
262
  if (TSDB_CODE_SUCCESS != code) {
23,727,110✔
263
    metaErr(vgId, code);
×
264
  }
265
  taosMemoryFree(value);
23,717,326✔
266
  return code;
23,719,313✔
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,
23,456,346✔
274
                                                 const SSchema *pOldColumn, const SSchema *pNewColumn) {
275
  int32_t code = TSDB_CODE_SUCCESS;
23,456,346✔
276

277
  const SMetaEntry *pEntry = pParam->pEntry;
23,456,346✔
278
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
23,468,606✔
279
  enum { ADD_INDEX, DROP_INDEX } action;
280

281
  if (pOldColumn && pNewColumn) {
23,465,489✔
282
    if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
22,836,238✔
283
      return TSDB_CODE_SUCCESS;
1,267,216✔
284
    } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) {
21,580,348✔
285
      action = DROP_INDEX;
26,617✔
286
    } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
21,548,154✔
287
      action = ADD_INDEX;
18,637✔
288
    } else {
289
      return TSDB_CODE_SUCCESS;
21,519,667✔
290
    }
291
  } else if (pOldColumn) {
629,251✔
292
    if (IS_IDX_ON(pOldColumn)) {
229,944✔
293
      action = DROP_INDEX;
9,917✔
294
    } else {
295
      return TSDB_CODE_SUCCESS;
219,015✔
296
    }
297
  } else {
298
    if (IS_IDX_ON(pNewColumn)) {
399,307✔
299
      action = ADD_INDEX;
×
300
    } else {
301
      return TSDB_CODE_SUCCESS;
399,440✔
302
    }
303
  }
304

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

313
  // do drop or add index
314
  for (int32_t i = 0; i < taosArrayGetSize(childTables); i++) {
8,033,384✔
315
    int64_t uid = *(int64_t *)taosArrayGet(childTables, i);
7,977,608✔
316

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

326
    STagIdxKey *pTagIdxKey = NULL;
7,977,608✔
327
    int32_t     tagIdxKeySize = 0;
7,977,608✔
328

329
    if (action == ADD_INDEX) {
7,977,608✔
330
      code = metaFetchTagIdxKey(pMeta, pChildEntry, pNewColumn, &pTagIdxKey, &tagIdxKeySize);
5,395,014✔
331
      if (code) {
5,395,014✔
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);
5,395,014✔
339
      if (code) {
5,395,014✔
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);
2,582,594✔
348
      if (code) {
2,582,594✔
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);
2,582,594✔
356
      if (code) {
2,582,594✔
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);
7,977,608✔
366
    metaFetchEntryFree(&pChildEntry);
7,977,608✔
367
  }
368

369
  taosArrayDestroy(childTables);
55,776✔
370
  return code;
55,776✔
371
}
372

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

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

381
  if (pOldColumn && pNewColumn) {
146,724,364✔
382
    return TSDB_CODE_SUCCESS;
146,711,562✔
383
  } else if (pOldColumn) {
12,802✔
384
    action = DROP_COLUMN;
6,322✔
385
  } else {
386
    action = ADD_COLUMN;
6,480✔
387
  }
388

389
  // fetch all child tables
390
  SArray *childTables = 0;
12,802✔
391
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childTables);
12,802✔
392
  if (code) {
12,802✔
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++) {
22,594✔
399
    int64_t uid = *(int64_t *)taosArrayGet(childTables, i);
9,792✔
400

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

410
    SMetaHandleParam param = {.pEntry = pChildEntry};
9,792✔
411

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

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

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

448
  taosArrayDestroy(childTables);
12,802✔
449
  return code;
12,802✔
450
}
451

452
static int32_t metaUpdateSuperTableTagSchema(SMeta *pMeta, const SMetaHandleParam *pParam) {
1,216,932✔
453
  int32_t               code = TSDB_CODE_SUCCESS;
1,216,932✔
454
  const SMetaEntry     *pEntry = pParam->pEntry;
1,216,932✔
455
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
1,216,577✔
456
  const SSchemaWrapper *pNewTagSchema = &pEntry->stbEntry.schemaTag;
1,214,808✔
457
  const SSchemaWrapper *pOldTagSchema = &pOldEntry->stbEntry.schemaTag;
1,219,635✔
458

459
  int32_t iOld = 0, iNew = 0;
1,219,209✔
460
  for (; iOld < pOldTagSchema->nCols && iNew < pNewTagSchema->nCols;) {
24,213,225✔
461
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
22,992,394✔
462
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
22,987,910✔
463

464
    if (pOldColumn->colId == pNewColumn->colId) {
22,975,458✔
465
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
22,825,330✔
466
      if (code) {
22,820,219✔
UNCOV
467
        metaErr(TD_VID(pMeta->pVnode), code);
×
468
        return code;
×
469
      }
470

471
      iOld++;
22,825,637✔
472
      iNew++;
22,825,637✔
473
    } else if (pOldColumn->colId < pNewColumn->colId) {
168,379✔
474
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
167,019✔
475
      if (code) {
168,036✔
476
        metaErr(TD_VID(pMeta->pVnode), code);
×
477
        return code;
×
478
      }
479

480
      // drop old tag from meta stable tag filter cache
481
      code = metaStableTagFilterCacheDropTag(pMeta, pEntry->uid, pOldColumn->colId);
168,036✔
482
      if (code) {
168,379✔
483
        metaErr(TD_VID(pMeta->pVnode), code);
×
484
        return code;
×
485
      }
486

487
      iOld++;
168,379✔
488
    } else {
489
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, NULL, pNewColumn);
×
490
      if (code) {
×
491
        metaErr(TD_VID(pMeta->pVnode), code);
×
492
        return code;
×
493
      }
494

495
      iNew++;
×
496
    }
497
  }
498

499
  for (; iOld < pOldTagSchema->nCols; iOld++) {
1,279,126✔
500
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
62,263✔
501
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
60,241✔
502
    if (code) {
59,548✔
503
      metaErr(TD_VID(pMeta->pVnode), code);
×
504
      return code;
×
505
    }
506
    // drop old tag from meta stable tag filter cache
507
    code = metaStableTagFilterCacheDropTag(pMeta, pEntry->uid, pOldColumn->colId);
59,548✔
508
    if (code) {
60,241✔
509
      metaErr(TD_VID(pMeta->pVnode), code);
×
510
      return code;
×
511
    }
512
  }
513

514
  for (; iNew < pNewTagSchema->nCols; iNew++) {
1,611,781✔
515
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
397,597✔
516
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, NULL, pNewColumn);
399,300✔
517
    if (code) {
395,594✔
518
      metaErr(TD_VID(pMeta->pVnode), code);
×
519
      return code;
×
520
    }
521
  }
522

523
  return code;
1,215,179✔
524
}
525

526
static int32_t metaUpdateSuperTableRowSchema(SMeta *pMeta, const SMetaHandleParam *pParam) {
16,616✔
527
  int32_t               code = TSDB_CODE_SUCCESS;
16,616✔
528
  const SMetaEntry     *pEntry = pParam->pEntry;
16,616✔
529
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
16,616✔
530
  const SSchemaWrapper *pNewRowSchema = &pEntry->stbEntry.schemaRow;
16,616✔
531
  const SSchemaWrapper *pOldRowSchema = &pOldEntry->stbEntry.schemaRow;
16,616✔
532

533
  int32_t iOld = 0, iNew = 0;
16,616✔
534
  for (; iOld < pOldRowSchema->nCols && iNew < pNewRowSchema->nCols;) {
146,730,802✔
535
    SSchema *pOldColumn = pOldRowSchema->pSchema + iOld;
146,714,186✔
536
    SSchema *pNewColumn = pNewRowSchema->pSchema + iNew;
146,714,186✔
537

538
    if (pOldColumn->colId == pNewColumn->colId) {
146,714,186✔
539
      code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
146,711,562✔
540
      if (code) {
146,711,562✔
UNCOV
541
        metaErr(TD_VID(pMeta->pVnode), code);
×
542
        return code;
×
543
      }
544

545
      iOld++;
146,711,562✔
546
      iNew++;
146,711,562✔
547
    } else if (pOldColumn->colId < pNewColumn->colId) {
2,624✔
548
      code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, pOldColumn, NULL);
2,624✔
549
      if (code) {
2,624✔
550
        metaErr(TD_VID(pMeta->pVnode), code);
×
551
        return code;
×
552
      }
553

554
      iOld++;
2,624✔
555
    } else {
556
      code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, NULL, pNewColumn);
×
557
      if (code) {
×
558
        metaErr(TD_VID(pMeta->pVnode), code);
×
559
        return code;
×
560
      }
561

562
      iNew++;
×
563
    }
564
  }
565

566
  for (; iOld < pOldRowSchema->nCols; iOld++) {
20,314✔
567
    SSchema *pOldColumn = pOldRowSchema->pSchema + iOld;
3,698✔
568
    code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, pOldColumn, NULL);
3,698✔
569
    if (code) {
3,698✔
570
      metaErr(TD_VID(pMeta->pVnode), code);
×
571
      return code;
×
572
    }
573
  }
574

575
  for (; iNew < pNewRowSchema->nCols; iNew++) {
23,096✔
576
    SSchema *pNewColumn = pNewRowSchema->pSchema + iNew;
6,480✔
577
    code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, NULL, pNewColumn);
6,480✔
578
    if (code) {
6,480✔
579
      metaErr(TD_VID(pMeta->pVnode), code);
×
580
      return code;
×
581
    }
582
  }
583

584
  return code;
16,616✔
585
}
586

587
static int32_t metaSchemaTableUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
24,944,452✔
588
  int32_t code = TSDB_CODE_SUCCESS;
24,944,452✔
589

590
  const SMetaEntry *pEntry = pParam->pEntry;
24,944,452✔
591
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
24,976,366✔
592

593
  if (NULL == pOldEntry) {
24,963,616✔
594
    return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
13,228,051✔
595
  }
596

597
  if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
11,735,565✔
598
    // check row schema
599
    if (pOldEntry->ntbEntry.schemaRow.version != pEntry->ntbEntry.schemaRow.version) {
3,879,025✔
600
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
3,852,405✔
601
    }
602
  } else if (pEntry->type == TSDB_SUPER_TABLE) {
7,861,034✔
603
    // check row schema
604
    if (pOldEntry->stbEntry.schemaRow.version != pEntry->stbEntry.schemaRow.version) {
7,867,507✔
605
      if (TABLE_IS_VIRTUAL(pEntry->flags)) {
6,644,552✔
606
        return metaUpdateSuperTableRowSchema(pMeta, pParam);
16,616✔
607
      } else {
608
        return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
6,631,549✔
609
      }
610
    }
611

612
    // check tag schema
613
    code = metaUpdateSuperTableTagSchema(pMeta, pParam);
1,216,277✔
614
    if (code) {
1,207,727✔
UNCOV
615
      metaErr(TD_VID(pMeta->pVnode), code);
×
616
      return code;
×
617
    }
618

619
  } else {
620
    return TSDB_CODE_INVALID_PARA;
×
621
  }
622

623
  return TSDB_CODE_SUCCESS;
1,232,559✔
624
}
625

626
static int32_t metaSchemaTableDelete(SMeta *pMeta, const SMetaHandleParam *pEntry) {
×
627
  // TODO
628
  return TSDB_CODE_SUCCESS;
×
629
}
630

631
// Uid Index
632
static void metaBuildEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) {
104,321,296✔
633
  pInfo->uid = pEntry->uid;
104,321,296✔
634
  pInfo->version = pEntry->version;
104,286,974✔
635
  if (pEntry->type == TSDB_SUPER_TABLE) {
104,301,458✔
636
    pInfo->suid = pEntry->uid;
13,329,703✔
637
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
13,297,141✔
638
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
90,984,153✔
639
    pInfo->suid = pEntry->ctbEntry.suid;
79,363,684✔
640
    pInfo->skmVer = 0;
79,368,077✔
641
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
11,634,716✔
642
    pInfo->suid = 0;
11,634,716✔
643
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
11,634,716✔
644
  }
645
}
104,349,885✔
646

647
static int32_t metaUidIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
104,316,363✔
648
  int32_t code = TSDB_CODE_SUCCESS;
104,316,363✔
649
  int32_t vgId = TD_VID(pMeta->pVnode);
104,316,363✔
650

651
  const SMetaEntry *pEntry = pParam->pEntry;
104,333,608✔
652

653
  // update cache
654
  SMetaInfo info = {0};
104,328,701✔
655
  metaBuildEntryInfo(pEntry, &info);
104,319,365✔
656
  code = metaCacheUpsert(pMeta, &info);
104,324,840✔
657
  if (code) {
104,314,571✔
658
    metaErr(vgId, code);
×
659
  }
660

661
  // put to tdb
662
  SUidIdxVal value = {
104,314,590✔
663
      .suid = info.suid,
104,313,609✔
664
      .skmVer = info.skmVer,
104,307,586✔
665
      .version = pEntry->version,
104,313,609✔
666
  };
667
  if (META_TABLE_OP_INSERT == op) {
104,307,586✔
668
    code = tdbTbInsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
84,269,947✔
669
  } else if (META_TABLE_OP_UPDATA == op) {
20,037,639✔
670
    code = tdbTbUpsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
20,031,670✔
671
  }
672
  return code;
104,349,944✔
673
}
674

675
static int32_t metaUidIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
84,277,110✔
676
  return metaUidIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
84,277,110✔
677
}
678

679
static int32_t metaUidIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
20,044,559✔
680
  return metaUidIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
20,044,559✔
681
}
682

683
static int32_t metaUidIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
3,495,258✔
684
  int32_t code = 0;
3,495,258✔
685

686
  const SMetaEntry *pEntry = pParam->pOldEntry;
3,495,258✔
687

688
  // delete tdb
689
  code = tdbTbDelete(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), pMeta->txn);
3,495,258✔
690
  if (code) {
3,495,258✔
691
    metaErr(TD_VID(pMeta->pVnode), code);
×
692
  }
693

694
  // delete cache
695
  (void)metaCacheDrop(pMeta, pEntry->uid);
3,495,258✔
696
  return code;
3,494,074✔
697
}
698

699
// Name Index
700
static int32_t metaNameIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
84,279,380✔
701
  int32_t code = TSDB_CODE_SUCCESS;
84,279,380✔
702

703
  const SMetaEntry *pEntry = pParam->pEntry;
84,279,380✔
704

705
  if (META_TABLE_OP_INSERT == op) {
84,300,677✔
706
    code = tdbTbInsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
84,300,677✔
707
                       pMeta->txn);
708
  } else if (META_TABLE_OP_UPDATA == op) {
×
709
    code = tdbTbUpsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
×
710
                       pMeta->txn);
711
  } else {
712
    code = TSDB_CODE_INVALID_PARA;
×
713
  }
714
  if (code) {
84,303,780✔
715
    metaErr(TD_VID(pMeta->pVnode), code);
×
716
  }
717
  return code;
84,303,313✔
718
}
719

720
static int32_t metaNameIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
84,284,623✔
721
  int32_t code = TSDB_CODE_SUCCESS;
84,284,623✔
722
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
84,284,623✔
723
}
724

725
static int32_t metaNameIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
726
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
×
727
}
728

729
static int32_t metaNameIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
3,494,601✔
730
  int32_t code = TSDB_CODE_SUCCESS;
3,494,601✔
731

732
  const SMetaEntry *pEntry = pParam->pOldEntry;
3,494,601✔
733
  code = tdbTbDelete(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, pMeta->txn);
3,494,601✔
734
  if (code) {
3,495,258✔
735
    metaErr(TD_VID(pMeta->pVnode), code);
×
736
  }
737
  return code;
3,495,258✔
738
}
739

740
// Suid Index
741
static int32_t metaSUidIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
5,456,514✔
742
  const SMetaEntry *pEntry = pParam->pEntry;
5,456,514✔
743

744
  int32_t code = tdbTbInsert(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), NULL, 0, pMeta->txn);
5,476,677✔
745
  if (code) {
5,479,282✔
746
    metaErr(TD_VID(pMeta->pVnode), code);
×
747
  }
748
  return code;
5,470,893✔
749
}
750

751
static int32_t metaSUidIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
940,848✔
752
  const SMetaEntry *pEntry = pParam->pOldEntry;
940,848✔
753

754
  int32_t code = tdbTbDelete(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), pMeta->txn);
940,848✔
755
  if (code) {
940,848✔
756
    metaErr(TD_VID(pMeta->pVnode), code);
×
757
  }
758
  return code;
940,848✔
759
}
760

761
// Child Index
762
static int32_t metaChildIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
79,172,053✔
763
  int32_t code = TSDB_CODE_SUCCESS;
79,172,053✔
764

765
  const SMetaEntry *pEntry = pParam->pEntry;
79,172,053✔
766

767
  SCtbIdxKey key = {
79,182,217✔
768
      .suid = pEntry->ctbEntry.suid,
79,184,962✔
769
      .uid = pEntry->uid,
79,182,794✔
770
  };
771

772
  if (META_TABLE_OP_INSERT == op) {
79,186,581✔
773
    code = tdbTbInsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
142,143,415✔
774
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
71,060,680✔
775
  } else if (META_TABLE_OP_UPDATA == op) {
8,113,641✔
776
    code = tdbTbUpsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
16,226,602✔
777
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
8,113,641✔
778
  } else {
779
    code = TSDB_CODE_INVALID_PARA;
×
780
  }
781
  return code;
79,192,545✔
782
}
783

784
static int32_t metaChildIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
71,062,784✔
785
  return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
71,062,784✔
786
}
787

788
static int32_t metaChildIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
8,310,068✔
789
  const SMetaEntry *pEntry = pParam->pEntry;
8,310,068✔
790
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
8,310,068✔
791
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
8,310,068✔
792

793
  const STag *pNewTags = (const STag *)pEntry->ctbEntry.pTags;
8,310,068✔
794
  const STag *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags;
8,309,366✔
795
  if (pNewTags->len != pOldTags->len || memcmp(pNewTags, pOldTags, pNewTags->len)) {
8,308,743✔
796
    return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
8,113,641✔
797
  }
798
  return 0;
196,427✔
799
}
800

801
static int32_t metaChildIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
1,671,964✔
802
  const SMetaEntry *pEntry = pParam->pOldEntry;
1,671,964✔
803

804
  SCtbIdxKey key = {
1,671,964✔
805
      .suid = pEntry->ctbEntry.suid,
1,671,964✔
806
      .uid = pEntry->uid,
1,671,964✔
807
  };
808
  return tdbTbDelete(pMeta->pCtbIdx, &key, sizeof(key), pMeta->txn);
1,671,964✔
809
}
810

811
// Tag Index
812
static int32_t metaFetchTagIdxKey(SMeta *pMeta, const SMetaEntry *pEntry, const SSchema *pTagColumn,
99,469,127✔
813
                                  STagIdxKey **ppTagIdxKey, int32_t *pTagIdxKeySize) {
814
  int32_t code = TSDB_CODE_SUCCESS;
99,469,127✔
815

816
  STagIdxKey *pTagIdxKey = NULL;
99,469,127✔
817
  int32_t     nTagIdxKey;
99,468,175✔
818
  const void *pTagData = NULL;
99,475,094✔
819
  int32_t     nTagData = 0;
99,475,094✔
820

821
  STagVal tagVal = {
99,475,094✔
822
      .cid = pTagColumn->colId,
99,476,681✔
823
  };
824

825
  if (tTagGet((const STag *)pEntry->ctbEntry.pTags, &tagVal)) {
99,470,246✔
826
    if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
96,494,027✔
827
      pTagData = tagVal.pData;
11,429,160✔
828
      nTagData = (int32_t)tagVal.nData;
11,429,160✔
829
    } else {
830
      pTagData = &(tagVal.i64);
85,055,281✔
831
      nTagData = tDataTypes[pTagColumn->type].bytes;
85,055,281✔
832
    }
833
  } else {
834
    if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
2,976,765✔
835
      nTagData = tDataTypes[pTagColumn->type].bytes;
2,879,382✔
836
    }
837
  }
838

839
  code = metaCreateTagIdxKey(pEntry->ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type,
99,470,132✔
840
                             pEntry->uid, &pTagIdxKey, &nTagIdxKey);
99,456,244✔
841
  if (code) {
99,450,152✔
842
    metaErr(TD_VID(pMeta->pVnode), code);
×
843
    return code;
×
844
  }
845

846
  *ppTagIdxKey = pTagIdxKey;
99,450,152✔
847
  *pTagIdxKeySize = nTagIdxKey;
99,453,930✔
848
  return code;
99,462,148✔
849
}
850

851
static void metaFetchTagIdxKeyFree(STagIdxKey **ppTagIdxKey) {
99,473,898✔
852
  metaDestroyTagIdxKey(*ppTagIdxKey);
99,473,898✔
853
  *ppTagIdxKey = NULL;
99,477,204✔
854
}
99,479,389✔
855

856
static int32_t metaTagIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
71,060,743✔
857
  int32_t code = TSDB_CODE_SUCCESS;
71,060,743✔
858

859
  const SMetaEntry *pEntry = pParam->pEntry;
71,060,743✔
860
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
71,066,632✔
861

862
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
71,070,045✔
863
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
71,334,929✔
864
    const SSchema *pTagColumn = &pTagSchema->pSchema[0];
264,221✔
865

866
    STagVal tagVal = {
528,442✔
867
        .cid = pTagColumn->colId,
264,221✔
868
    };
869

870
    const void *pTagData = pEntry->ctbEntry.pTags;
264,221✔
871
    int32_t     nTagData = ((const STag *)pEntry->ctbEntry.pTags)->len;
264,221✔
872
    code = metaSaveJsonVarToIdx(pMeta, pEntry, pTagColumn);
264,221✔
873
    if (code) {
264,221✔
874
      metaErr(TD_VID(pMeta->pVnode), code);
×
875
    }
876
  } else {
877
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
294,710,285✔
878
      STagIdxKey    *pTagIdxKey = NULL;
223,886,966✔
879
      int32_t        nTagIdxKey;
223,891,674✔
880
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
223,907,937✔
881

882
      if (!IS_IDX_ON(pTagColumn)) {
223,899,982✔
883
        continue;
153,068,417✔
884
      }
885

886
      code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pTagIdxKey, &nTagIdxKey);
70,848,551✔
887
      if (code) {
70,834,489✔
888
        metaErr(TD_VID(pMeta->pVnode), code);
×
889
        return code;
×
890
      }
891

892
      code = tdbTbInsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn);
70,834,489✔
893
      if (code) {
70,850,771✔
894
        metaErr(TD_VID(pMeta->pVnode), code);
×
895
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
896
        return code;
×
897
      }
898
      metaFetchTagIdxKeyFree(&pTagIdxKey);
70,850,771✔
899
    }
900
  }
901
  return code;
71,077,574✔
902
}
903

904
static int32_t metaTagIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
8,310,068✔
905
  int32_t code = TSDB_CODE_SUCCESS;
8,310,068✔
906

907
  const SMetaEntry     *pEntry = pParam->pEntry;
8,310,068✔
908
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
8,310,068✔
909
  const SMetaEntry     *pSuperEntry = pParam->pSuperEntry;
8,309,366✔
910
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
8,310,068✔
911
  const STag           *pNewTags = (const STag *)pEntry->ctbEntry.pTags;
8,310,068✔
912
  const STag           *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags;
8,310,068✔
913

914
  if (pNewTags->len == pOldTags->len && !memcmp(pNewTags, pOldTags, pNewTags->len)) {
8,309,366✔
915
    return code;
196,427✔
916
  }
917

918
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
8,113,641✔
919
    code = metaDelJsonVarFromIdx(pMeta, pOldEntry, &pTagSchema->pSchema[0]);
970✔
920
    if (code) {
970✔
921
      metaErr(TD_VID(pMeta->pVnode), code);
×
922
      return code;
×
923
    }
924

925
    code = metaSaveJsonVarToIdx(pMeta, pEntry, &pTagSchema->pSchema[0]);
970✔
926
    if (code) {
970✔
927
      metaErr(TD_VID(pMeta->pVnode), code);
×
928
      return code;
×
929
    }
930
  } else {
931
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
17,694,147✔
932
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
9,581,476✔
933

934
      if (!IS_IDX_ON(pTagColumn)) {
9,581,476✔
935
        continue;
1,468,805✔
936
      }
937

938
      STagIdxKey *pOldTagIdxKey = NULL;
8,112,671✔
939
      int32_t     oldTagIdxKeySize = 0;
8,112,671✔
940
      STagIdxKey *pNewTagIdxKey = NULL;
8,112,671✔
941
      int32_t     newTagIdxKeySize = 0;
8,111,969✔
942

943
      code = metaFetchTagIdxKey(pMeta, pOldEntry, pTagColumn, &pOldTagIdxKey, &oldTagIdxKeySize);
8,111,969✔
944
      if (code) {
8,112,671✔
945
        metaErr(TD_VID(pMeta->pVnode), code);
×
946
        return code;
×
947
      }
948

949
      code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pNewTagIdxKey, &newTagIdxKeySize);
8,112,671✔
950
      if (code) {
8,112,671✔
951
        metaErr(TD_VID(pMeta->pVnode), code);
×
952
        metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
953
        return code;
×
954
      }
955

956
      if (tagIdxKeyCmpr(pOldTagIdxKey, oldTagIdxKeySize, pNewTagIdxKey, newTagIdxKeySize)) {
8,112,671✔
957
        code = tdbTbDelete(pMeta->pTagIdx, pOldTagIdxKey, oldTagIdxKeySize, pMeta->txn);
7,707,146✔
958
        if (code) {
7,707,146✔
959
          metaErr(TD_VID(pMeta->pVnode), code);
×
960
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
961
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
962
          return code;
×
963
        }
964

965
        code = tdbTbInsert(pMeta->pTagIdx, pNewTagIdxKey, newTagIdxKeySize, NULL, 0, pMeta->txn);
7,707,146✔
966
        if (code) {
7,707,146✔
967
          metaErr(TD_VID(pMeta->pVnode), code);
×
968
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
969
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
970
          return code;
×
971
        }
972
      }
973

974
      metaFetchTagIdxKeyFree(&pOldTagIdxKey);
8,112,671✔
975
      metaFetchTagIdxKeyFree(&pNewTagIdxKey);
8,112,671✔
976
    }
977
  }
978
  return code;
8,113,641✔
979
}
980

981
static int32_t metaTagIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
1,671,437✔
982
  int32_t code = TSDB_CODE_SUCCESS;
1,671,437✔
983

984
  const SMetaEntry     *pEntry = pParam->pEntry;
1,671,437✔
985
  const SMetaEntry     *pChild = pParam->pOldEntry;
1,671,964✔
986
  const SMetaEntry     *pSuper = pParam->pSuperEntry;
1,671,964✔
987
  const SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
1,671,437✔
988
  const SSchema        *pTagColumn = NULL;
1,671,964✔
989
  const STag           *pTags = (const STag *)pChild->ctbEntry.pTags;
1,671,964✔
990

991
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
1,671,964✔
992
    pTagColumn = &pTagSchema->pSchema[0];
117,993✔
993
    code = metaDelJsonVarFromIdx(pMeta, pChild, pTagColumn);
117,993✔
994
    if (code) {
117,993✔
995
      metaErr(TD_VID(pMeta->pVnode), code);
×
996
    }
997
  } else {
998
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
9,238,768✔
999
      pTagColumn = &pTagSchema->pSchema[i];
7,684,797✔
1000
      if (!IS_IDX_ON(pTagColumn)) {
7,684,797✔
1001
        continue;
3,257,486✔
1002
      }
1003

1004
      STagIdxKey *pTagIdxKey = NULL;
4,427,311✔
1005
      int32_t     nTagIdxKey;
4,427,083✔
1006

1007
      code = metaFetchTagIdxKey(pMeta, pChild, pTagColumn, &pTagIdxKey, &nTagIdxKey);
4,426,784✔
1008
      if (code) {
4,427,311✔
1009
        metaErr(TD_VID(pMeta->pVnode), code);
×
1010
        return code;
×
1011
      }
1012

1013
      code = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
4,427,311✔
1014
      if (code) {
4,427,311✔
1015
        metaErr(TD_VID(pMeta->pVnode), code);
×
1016
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
1017
        return code;
×
1018
      }
1019
      metaFetchTagIdxKeyFree(&pTagIdxKey);
4,427,311✔
1020
    }
1021
  }
1022
  return code;
1,671,964✔
1023
}
1024

1025
// Btime Index
1026
static int32_t metaBtimeIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
81,371,164✔
1027
  int32_t code = TSDB_CODE_SUCCESS;
81,371,164✔
1028

1029
  const SMetaEntry *pEntry;
1030
  if (META_TABLE_OP_DELETE == op) {
81,371,164✔
1031
    pEntry = pParam->pOldEntry;
2,554,410✔
1032
  } else {
1033
    pEntry = pParam->pEntry;
78,816,754✔
1034
  }
1035

1036
  SBtimeIdxKey key = {
81,370,890✔
1037
      .uid = pEntry->uid,
81,390,351✔
1038
  };
1039

1040
  if (TSDB_CHILD_TABLE == pEntry->type || TSDB_VIRTUAL_CHILD_TABLE == pEntry->type) {
81,386,009✔
1041
    key.btime = pEntry->ctbEntry.btime;
72,744,358✔
1042
  } else if (TSDB_NORMAL_TABLE == pEntry->type || TSDB_VIRTUAL_NORMAL_TABLE == pEntry->type) {
8,647,356✔
1043
    key.btime = pEntry->ntbEntry.btime;
8,647,356✔
1044
  } else {
1045
    return TSDB_CODE_INVALID_PARA;
×
1046
  }
1047

1048
  if (META_TABLE_OP_INSERT == op) {
81,369,758✔
1049
    code = tdbTbInsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
78,815,348✔
1050
  } else if (META_TABLE_OP_UPDATA == op) {
2,554,410✔
1051
    code = tdbTbUpsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
×
1052
  } else if (META_TABLE_OP_DELETE == op) {
2,554,410✔
1053
    code = tdbTbDelete(pMeta->pBtimeIdx, &key, sizeof(key), pMeta->txn);
2,554,410✔
1054
  } else {
1055
    code = TSDB_CODE_INVALID_PARA;
×
1056
  }
1057
  if (code) {
81,377,188✔
1058
    metaErr(TD_VID(pMeta->pVnode), code);
×
1059
  }
1060
  return code;
81,374,780✔
1061
}
1062

1063
static int32_t metaBtimeIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
78,821,783✔
1064
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
78,821,783✔
1065
}
1066

1067
static int32_t metaBtimeIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
1068
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
×
1069
}
1070

1071
static int32_t metaBtimeIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
2,554,410✔
1072
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_DELETE);
2,554,410✔
1073
}
1074

1075
// TTL Index
1076
static int32_t metaTtlIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
78,290,200✔
1077
  const SMetaEntry *pEntry = pParam->pEntry;
78,290,200✔
1078

1079
  STtlUpdTtlCtx ctx = {
78,297,760✔
1080
      .uid = pEntry->uid,
78,304,924✔
1081
      .pTxn = pMeta->txn,
78,292,865✔
1082
  };
1083
  if (TSDB_CHILD_TABLE == pEntry->type) {
78,307,235✔
1084
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
70,727,173✔
1085
    ctx.changeTimeMs = pEntry->ctbEntry.btime;
70,727,718✔
1086
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
7,576,880✔
1087
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
7,576,880✔
1088
    ctx.changeTimeMs = pEntry->ntbEntry.btime;
7,576,880✔
1089
  } else {
1090
    return TSDB_CODE_INVALID_PARA;
×
1091
  }
1092

1093
  int32_t ret = ttlMgrInsertTtl(pMeta->pTtlMgr, &ctx);
78,309,203✔
1094
  if (ret < 0) {
78,286,265✔
1095
    metaError("vgId:%d, failed to insert ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid, tstrerror(ret));
×
1096
  }
1097
  return TSDB_CODE_SUCCESS;
78,283,087✔
1098
}
1099

1100
static int32_t metaTtlIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
78,293,398✔
1101
  return metaTtlIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
78,293,398✔
1102
}
1103

1104
static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam);
1105

1106
static int32_t metaTtlIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
11,720,828✔
1107
  int32_t code = TSDB_CODE_SUCCESS;
11,720,828✔
1108

1109
  const SMetaEntry *pEntry = pParam->pEntry;
11,720,828✔
1110
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
11,720,828✔
1111

1112
  if ((pEntry->type == TSDB_CHILD_TABLE && pOldEntry->ctbEntry.ttlDays != pEntry->ctbEntry.ttlDays) ||
11,720,828✔
1113
      (pEntry->type == TSDB_NORMAL_TABLE && pOldEntry->ntbEntry.ttlDays != pEntry->ntbEntry.ttlDays)) {
11,716,073✔
1114
    code = metaTtlIdxDelete(pMeta, pParam);
9,577✔
1115
    if (code) {
9,577✔
1116
      metaErr(TD_VID(pMeta->pVnode), code);
×
1117
    }
1118

1119
    code = metaTtlIdxInsert(pMeta, pParam);
9,577✔
1120
    if (code) {
9,577✔
1121
      metaErr(TD_VID(pMeta->pVnode), code);
×
1122
    }
1123
  }
1124

1125
  return TSDB_CODE_SUCCESS;
11,720,828✔
1126
}
1127

1128
static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
2,491,960✔
1129
  int32_t code = TSDB_CODE_SUCCESS;
2,491,960✔
1130

1131
  const SMetaEntry *pEntry = pParam->pOldEntry;
2,491,960✔
1132
  STtlDelTtlCtx     ctx = {
2,491,960✔
1133
          .uid = pEntry->uid,
2,491,960✔
1134
          .pTxn = pMeta->txn,
2,491,960✔
1135
  };
1136

1137
  if (TSDB_CHILD_TABLE == pEntry->type) {
2,491,582✔
1138
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
1,623,078✔
1139
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
868,504✔
1140
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
846,862✔
1141
  } else {
1142
    code = TSDB_CODE_INVALID_PARA;
21,642✔
1143
  }
1144

1145
  if (TSDB_CODE_SUCCESS == code) {
2,491,960✔
1146
    int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
2,469,940✔
1147
    if (ret < 0) {
2,470,318✔
1148
      metaError("vgId:%d, failed to delete ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid,
×
1149
                tstrerror(ret));
1150
    }
1151
  }
1152
  return code;
2,492,338✔
1153
}
1154

1155
static void metaTimeSeriesNotifyCheck(SMeta *pMeta) {
85,920,970✔
1156
#if defined(TD_ENTERPRISE)
1157
  int64_t nTimeSeries = metaGetTimeSeriesNum(pMeta, 0);
85,920,970✔
1158
  int64_t deltaTS = nTimeSeries - pMeta->pVnode->config.vndStats.numOfReportedTimeSeries;
85,922,968✔
1159
  if (deltaTS > tsTimeSeriesThreshold) {
85,936,617✔
1160
    if (0 == atomic_val_compare_exchange_8(&dmNotifyHdl.state, 1, 2)) {
53,239,652✔
1161
      if (tsem_post(&dmNotifyHdl.sem) != 0) {
53,240,653✔
1162
        metaError("vgId:%d, failed to post semaphore, errno:%d", TD_VID(pMeta->pVnode), ERRNO);
×
1163
      }
1164
    }
1165
  }
1166
#endif
1167
}
85,960,737✔
1168

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

1227
static int32_t metaHandleSuperTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
5,476,983✔
1228
  int32_t code = TSDB_CODE_SUCCESS;
5,476,983✔
1229

1230
  SMetaTableOp ops[] = {
5,476,983✔
1231
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1232
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: here should be insert
1233
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1234
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1235
      {META_SUID_IDX, META_TABLE_OP_INSERT},      //
1236
  };
1237

1238
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
32,855,446✔
1239
    SMetaTableOp          *op = &ops[i];
27,359,942✔
1240
    const SMetaHandleParam param = {
27,372,114✔
1241
        .pEntry = pEntry,
1242
    };
1243
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
27,364,311✔
1244
    if (TSDB_CODE_SUCCESS != code) {
27,353,890✔
1245
      metaErr(TD_VID(pMeta->pVnode), code);
×
1246
      return code;
×
1247
    }
1248
  }
1249

1250
  return code;
5,495,504✔
1251
}
1252
static int32_t metaHandleSuperTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
5,452,958✔
1253
  int32_t code = TSDB_CODE_SUCCESS;
5,452,958✔
1254

1255
  metaWLock(pMeta);
5,452,958✔
1256
  code = metaHandleSuperTableCreateImpl(pMeta, pEntry);
5,479,591✔
1257
  metaULock(pMeta);
5,473,758✔
1258

1259
  if (TSDB_CODE_SUCCESS == code) {
5,474,753✔
1260
    pMeta->pVnode->config.vndStats.numOfSTables++;
5,474,753✔
1261

1262
    metaInfo("vgId:%d, %s success, version:%" PRId64 " type:%d uid:%" PRId64 " name:%s", TD_VID(pMeta->pVnode),
5,482,646✔
1263
             __func__, pEntry->version, pEntry->type, pEntry->uid, pEntry->name);
1264
  } else {
1265
    metaErr(TD_VID(pMeta->pVnode), code);
×
1266
  }
1267
  return code;
5,485,079✔
1268
}
1269

1270
static int32_t metaHandleNormalTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
7,572,058✔
1271
  int32_t code = TSDB_CODE_SUCCESS;
7,572,058✔
1272

1273
  SMetaTableOp ops[] = {
7,572,058✔
1274
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1275
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: need to be insert
1276
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1277
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1278
      {META_BTIME_IDX, META_TABLE_OP_INSERT},     //
1279
      {META_TTL_IDX, META_TABLE_OP_INSERT},       //
1280
  };
1281

1282
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
53,004,406✔
1283
    SMetaTableOp *op = &ops[i];
45,432,348✔
1284

1285
    SMetaHandleParam param = {
45,432,348✔
1286
        .pEntry = pEntry,
1287
    };
1288

1289
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
45,432,348✔
1290
    if (TSDB_CODE_SUCCESS != code) {
45,432,348✔
1291
      metaErr(TD_VID(pMeta->pVnode), code);
×
1292
      return code;
×
1293
    }
1294
  }
1295

1296
  return code;
7,572,058✔
1297
}
1298
static int32_t metaHandleNormalTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
7,572,058✔
1299
  int32_t code = TSDB_CODE_SUCCESS;
7,572,058✔
1300

1301
  // update TDB
1302
  metaWLock(pMeta);
7,572,058✔
1303
  code = metaHandleNormalTableCreateImpl(pMeta, pEntry);
7,572,058✔
1304
  metaULock(pMeta);
7,572,058✔
1305

1306
  // update other stuff
1307
  if (TSDB_CODE_SUCCESS == code) {
7,572,058✔
1308
    pMeta->pVnode->config.vndStats.numOfNTables++;
7,572,058✔
1309
    pMeta->pVnode->config.vndStats.numOfNTimeSeries += pEntry->ntbEntry.schemaRow.nCols - 1;
7,572,058✔
1310

1311
    if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
7,572,058✔
1312
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, -1, &pEntry->ntbEntry.schemaRow);
15,947✔
1313
      if (rc < 0) {
15,947✔
1314
        metaError("vgId:%d, failed to create table:%s since %s", TD_VID(pMeta->pVnode), pEntry->name, tstrerror(rc));
×
1315
      }
1316
    }
1317
    metaTimeSeriesNotifyCheck(pMeta);
7,572,058✔
1318
  } else {
1319
    metaErr(TD_VID(pMeta->pVnode), code);
×
1320
  }
1321
  return code;
7,572,058✔
1322
}
1323

1324
static int32_t metaHandleChildTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry, const SMetaEntry *pSuperEntry) {
70,717,645✔
1325
  int32_t code = TSDB_CODE_SUCCESS;
70,717,645✔
1326

1327
  SMetaTableOp ops[] = {
70,717,645✔
1328
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},  //
1329
      {META_UID_IDX, META_TABLE_OP_INSERT},      //
1330
      {META_NAME_IDX, META_TABLE_OP_INSERT},     //
1331
      {META_CHILD_IDX, META_TABLE_OP_INSERT},    //
1332
      {META_TAG_IDX, META_TABLE_OP_INSERT},      //
1333
      {META_BTIME_IDX, META_TABLE_OP_INSERT},    //
1334
      {META_TTL_IDX, META_TABLE_OP_INSERT},      //
1335
  };
1336

1337
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
565,772,732✔
1338
    SMetaTableOp *op = &ops[i];
495,045,982✔
1339

1340
    SMetaHandleParam param = {
495,055,981✔
1341
        .pEntry = pEntry,
1342
        .pSuperEntry = pSuperEntry,
1343
    };
1344

1345
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
495,048,814✔
1346
    if (TSDB_CODE_SUCCESS != code) {
495,001,279✔
1347
      metaErr(TD_VID(pMeta->pVnode), code);
×
1348
      return code;
×
1349
    }
1350
  }
1351

1352
  if (TSDB_CODE_SUCCESS == code) {
70,726,750✔
1353
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
70,718,211✔
1354
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
70,708,194✔
1355
    if (ret < 0) {
70,716,408✔
1356
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1357
    }
1358

1359
    ret = metaStableTagFilterCacheUpdateUid(
70,716,408✔
1360
      pMeta, pEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_ADD_TABLE);
1361
    if (ret < 0) {
70,709,671✔
1362
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1363
    }
1364

1365
    ret = metaTbGroupCacheClear(pMeta, pSuperEntry->uid);
70,709,671✔
1366
    if (ret < 0) {
70,721,457✔
1367
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1368
    }
1369
  }
1370
  return code;
70,706,129✔
1371
}
1372

1373
static int32_t metaHandleChildTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
70,700,695✔
1374
  int32_t     code = TSDB_CODE_SUCCESS;
70,700,695✔
1375
  SMetaEntry *pSuperEntry = NULL;
70,700,695✔
1376

1377
  // get the super table entry
1378
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
70,714,824✔
1379
  if (code) {
70,725,452✔
1380
    metaErr(TD_VID(pMeta->pVnode), code);
×
1381
    return code;
×
1382
  }
1383

1384
  // update TDB
1385
  metaWLock(pMeta);
70,725,452✔
1386
  code = metaHandleChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
70,719,964✔
1387
  metaULock(pMeta);
70,709,843✔
1388

1389
  // update other stuff
1390
  if (TSDB_CODE_SUCCESS == code) {
70,718,339✔
1391
    pMeta->pVnode->config.vndStats.numOfCTables++;
70,718,339✔
1392

1393
    if (!metaTbInFilterCache(pMeta, pSuperEntry->name, 1)) {
70,714,743✔
1394
      int32_t nCols = 0;
70,729,287✔
1395
      int32_t ret = metaGetStbStats(pMeta->pVnode, pSuperEntry->uid, 0, &nCols, 0);
70,729,616✔
1396
      if (ret < 0) {
70,723,396✔
1397
        metaErr(TD_VID(pMeta->pVnode), ret);
×
1398
      }
1399
      pMeta->pVnode->config.vndStats.numOfTimeSeries += (nCols > 0 ? nCols - 1 : 0);
70,723,396✔
1400
    }
1401

1402
    if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
70,722,498✔
1403
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, pEntry->ctbEntry.suid, NULL);
8,002,787✔
1404
      if (rc < 0) {
8,002,787✔
1405
        metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
1406
                  tstrerror(rc));
1407
      }
1408
    }
1409

1410
  } else {
1411
    metaErr(TD_VID(pMeta->pVnode), code);
×
1412
  }
1413
  metaTimeSeriesNotifyCheck(pMeta);
70,726,417✔
1414
  metaFetchEntryFree(&pSuperEntry);
70,730,883✔
1415
  return code;
70,729,996✔
1416
}
1417

1418
static int32_t metaHandleVirtualNormalTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
192,852✔
1419
  int32_t code = TSDB_CODE_SUCCESS;
192,852✔
1420

1421
  SMetaTableOp ops[] = {
192,852✔
1422
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1423
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: need to be insert
1424
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1425
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1426
      {META_BTIME_IDX, META_TABLE_OP_INSERT},     //
1427
  };
1428

1429
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1,157,112✔
1430
    SMetaTableOp *op = &ops[i];
964,260✔
1431

1432
    SMetaHandleParam param = {
964,260✔
1433
        .pEntry = pEntry,
1434
    };
1435

1436
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
964,260✔
1437
    if (TSDB_CODE_SUCCESS != code) {
964,260✔
1438
      metaErr(TD_VID(pMeta->pVnode), code);
×
1439
      return code;
×
1440
    }
1441
  }
1442

1443
  return code;
192,852✔
1444
}
1445

1446
static int32_t metaHandleVirtualNormalTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
192,852✔
1447
  int32_t code = TSDB_CODE_SUCCESS;
192,852✔
1448

1449
  // update TDB
1450
  metaWLock(pMeta);
192,852✔
1451
  code = metaHandleVirtualNormalTableCreateImpl(pMeta, pEntry);
192,852✔
1452
  metaULock(pMeta);
192,852✔
1453

1454
  // update other stuff
1455
  if (TSDB_CODE_SUCCESS == code) {
192,852✔
1456
    pMeta->pVnode->config.vndStats.numOfVTables++;
192,852✔
1457
  } else {
1458
    metaErr(TD_VID(pMeta->pVnode), code);
×
1459
  }
1460
  return code;
192,852✔
1461
}
1462

1463
static int32_t metaHandleVirtualChildTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry,
345,538✔
1464
                                                     const SMetaEntry *pSuperEntry) {
1465
  int32_t code = TSDB_CODE_SUCCESS;
345,538✔
1466

1467
  SMetaTableOp ops[] = {
345,538✔
1468
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},  //
1469
      {META_UID_IDX, META_TABLE_OP_INSERT},      //
1470
      {META_NAME_IDX, META_TABLE_OP_INSERT},     //
1471
      {META_CHILD_IDX, META_TABLE_OP_INSERT},    //
1472
      {META_TAG_IDX, META_TABLE_OP_INSERT},      //
1473
      {META_BTIME_IDX, META_TABLE_OP_INSERT},    //
1474
  };
1475

1476
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
2,418,766✔
1477
    SMetaTableOp *op = &ops[i];
2,073,228✔
1478

1479
    SMetaHandleParam param = {
2,073,228✔
1480
        .pEntry = pEntry,
1481
        .pSuperEntry = pSuperEntry,
1482
    };
1483

1484
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
2,073,228✔
1485
    if (TSDB_CODE_SUCCESS != code) {
2,073,228✔
1486
      metaErr(TD_VID(pMeta->pVnode), code);
×
1487
      return code;
×
1488
    }
1489
  }
1490

1491
  if (TSDB_CODE_SUCCESS == code) {
345,538✔
1492
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
345,538✔
1493
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
345,538✔
1494
    if (ret < 0) {
345,538✔
1495
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1496
    }
1497

1498
    ret = metaStableTagFilterCacheUpdateUid(
345,538✔
1499
      pMeta, pEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_ADD_TABLE);
1500
    if (ret < 0) {
345,538✔
1501
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1502
    }
1503

1504
    ret = metaTbGroupCacheClear(pMeta, pSuperEntry->uid);
345,538✔
1505
    if (ret < 0) {
345,538✔
1506
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1507
    }
1508

1509
    ret = metaRefDbsCacheClear(pMeta, pSuperEntry->uid);
345,538✔
1510
    if (ret < 0) {
345,538✔
1511
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1512
    }
1513
  }
1514

1515
  return code;
345,538✔
1516
}
1517

1518
static int32_t metaHandleVirtualChildTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
345,538✔
1519
  int32_t     code = TSDB_CODE_SUCCESS;
345,538✔
1520
  SMetaEntry *pSuperEntry = NULL;
345,538✔
1521

1522
  // get the super table entry
1523
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
345,538✔
1524
  if (code) {
345,538✔
1525
    metaErr(TD_VID(pMeta->pVnode), code);
×
1526
    return code;
×
1527
  }
1528

1529
  // update TDB
1530
  metaWLock(pMeta);
345,538✔
1531
  code = metaHandleVirtualChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
345,538✔
1532
  metaULock(pMeta);
345,538✔
1533

1534
  // update other stuff
1535
  if (TSDB_CODE_SUCCESS == code) {
345,538✔
1536
    pMeta->pVnode->config.vndStats.numOfVCTables++;
345,538✔
1537
  } else {
1538
    metaErr(TD_VID(pMeta->pVnode), code);
×
1539
  }
1540

1541
  metaFetchEntryFree(&pSuperEntry);
345,538✔
1542
  return code;
345,538✔
1543
}
1544

1545
static int32_t metaHandleNormalTableDropImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
842,040✔
1546
  int32_t code = TSDB_CODE_SUCCESS;
842,040✔
1547

1548
  SMetaTableOp ops[] = {
842,040✔
1549
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1550
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1551
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1552
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1553
      {META_TTL_IDX, META_TABLE_OP_DELETE},      //
1554

1555
      // {META_SCHEMA_TABLE, META_TABLE_OP_DELETE},  //
1556
  };
1557

1558
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
5,052,240✔
1559
    SMetaTableOp *op = &ops[i];
4,210,200✔
1560
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
4,210,200✔
1561
    if (code) {
4,210,200✔
1562
      const SMetaEntry *pEntry = pParam->pEntry;
×
1563
      metaErr(TD_VID(pMeta->pVnode), code);
×
1564
    }
1565
  }
1566

1567
  return code;
842,040✔
1568
}
1569

1570
static int32_t metaHandleNormalTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
842,040✔
1571
  int32_t     code = TSDB_CODE_SUCCESS;
842,040✔
1572
  SMetaEntry *pOldEntry = NULL;
842,040✔
1573

1574
  // fetch the entry
1575
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
842,040✔
1576
  if (code) {
842,040✔
1577
    metaErr(TD_VID(pMeta->pVnode), code);
×
1578
    return code;
×
1579
  }
1580

1581
  SMetaHandleParam param = {
842,040✔
1582
      .pEntry = pEntry,
1583
      .pOldEntry = pOldEntry,
1584
  };
1585

1586
  // do the drop
1587
  metaWLock(pMeta);
842,040✔
1588
  code = metaHandleNormalTableDropImpl(pMeta, &param);
842,040✔
1589
  metaULock(pMeta);
842,040✔
1590
  if (code) {
842,040✔
1591
    metaErr(TD_VID(pMeta->pVnode), code);
×
1592
    metaFetchEntryFree(&pOldEntry);
×
1593
    return code;
×
1594
  }
1595

1596
  // update other stuff
1597
  pMeta->pVnode->config.vndStats.numOfNTables--;
842,040✔
1598
  pMeta->pVnode->config.vndStats.numOfNTimeSeries -= (pOldEntry->ntbEntry.schemaRow.nCols - 1);
842,040✔
1599

1600
#if 0
1601
  if (tbUids) {
1602
    if (taosArrayPush(tbUids, &uid) == NULL) {
1603
      rc = terrno;
1604
      goto _exit;
1605
    }
1606
  }
1607
#endif
1608

1609
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
842,040✔
1610
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pOldEntry->uid, 0, NULL);
×
1611
    if (ret < 0) {
×
1612
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1613
    }
1614
  }
1615

1616
  metaFetchEntryFree(&pOldEntry);
842,040✔
1617
  return code;
842,040✔
1618
}
1619

1620
static int32_t metaHandleChildTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam, bool superDropped) {
1,640,343✔
1621
  int32_t code = TSDB_CODE_SUCCESS;
1,640,343✔
1622

1623
  const SMetaEntry *pEntry = pParam->pEntry;
1,640,343✔
1624
  const SMetaEntry *pChild = pParam->pOldEntry;
1,640,343✔
1625
  const SMetaEntry *pSuper = pParam->pSuperEntry;
1,640,343✔
1626

1627
  SMetaTableOp ops[] = {
1,640,343✔
1628
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1629
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1630
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1631
      {META_CHILD_IDX, META_TABLE_OP_DELETE},    //
1632
      {META_TAG_IDX, META_TABLE_OP_DELETE},      //
1633
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1634
      {META_TTL_IDX, META_TABLE_OP_DELETE},      //
1635
  };
1636

1637
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
13,099,551✔
1638
    SMetaTableOp *op = &ops[i];
11,480,850✔
1639

1640
    if (op->table == META_ENTRY_TABLE && superDropped) {
11,480,699✔
1641
      continue;
831,363✔
1642
    }
1643

1644
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
10,649,336✔
1645
    if (code) {
10,649,577✔
1646
      metaErr(TD_VID(pMeta->pVnode), code);
21,642✔
1647
      return code;
21,642✔
1648
    }
1649
  }
1650

1651
  --pMeta->pVnode->config.vndStats.numOfCTables;
1,618,701✔
1652
  metaUpdateStbStats(pMeta, pParam->pSuperEntry->uid, -1, 0, -1);
1,618,701✔
1653
  int32_t ret = metaUidCacheClear(pMeta, pSuper->uid);
1,618,701✔
1654
  if (ret < 0) {
1,618,701✔
1655
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1656
  }
1657

1658
  ret = metaStableTagFilterCacheUpdateUid(
1,618,701✔
1659
    pMeta, pChild, pSuper, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
1660
  if (ret < 0) {
1,618,701✔
1661
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1662
  }
1663

1664
  ret = metaTbGroupCacheClear(pMeta, pSuper->uid);
1,618,701✔
1665
  if (ret < 0) {
1,618,701✔
1666
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1667
  }
1668
  return code;
1,618,701✔
1669
}
1670

1671
static int32_t metaHandleChildTableDrop(SMeta *pMeta, const SMetaEntry *pEntry, bool superDropped) {
1,639,965✔
1672
  int32_t     code = TSDB_CODE_SUCCESS;
1,639,965✔
1673
  SMetaEntry *pChild = NULL;
1,639,965✔
1674
  SMetaEntry *pSuper = NULL;
1,640,343✔
1675

1676
  // fetch old entry
1677
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pChild);
1,640,343✔
1678
  if (code) {
1,640,343✔
1679
    metaErr(TD_VID(pMeta->pVnode), code);
×
1680
    return code;
×
1681
  }
1682

1683
  // fetch super entry
1684
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
1,640,343✔
1685
  if (code) {
1,640,343✔
1686
    metaErr(TD_VID(pMeta->pVnode), code);
×
1687
    metaFetchEntryFree(&pChild);
×
1688
    return code;
×
1689
  }
1690

1691
  SMetaHandleParam param = {
1,640,343✔
1692
      .pEntry = pEntry,
1693
      .pOldEntry = pChild,
1694
      .pSuperEntry = pSuper,
1695
  };
1696

1697
  // do the drop
1698
  metaWLock(pMeta);
1,640,343✔
1699
  code = metaHandleChildTableDropImpl(pMeta, &param, superDropped);
1,640,343✔
1700
  metaULock(pMeta);
1,640,343✔
1701
  if (code) {
1,639,816✔
1702
    metaErr(TD_VID(pMeta->pVnode), code);
21,642✔
1703
    metaFetchEntryFree(&pChild);
21,642✔
1704
    metaFetchEntryFree(&pSuper);
21,642✔
1705
    return code;
21,642✔
1706
  }
1707

1708
  // do other stuff
1709
  if (!metaTbInFilterCache(pMeta, pSuper->name, 1)) {
1,618,174✔
1710
    int32_t      nCols = 0;
1,617,835✔
1711
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
1,617,835✔
1712
    if (metaGetStbStats(pMeta->pVnode, pSuper->uid, NULL, &nCols, 0) == 0) {
1,618,362✔
1713
      pStats->numOfTimeSeries -= nCols - 1;
1,618,701✔
1714
    }
1715
  }
1716

1717
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
1,618,174✔
1718
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pChild->uid, pSuper->uid, NULL);
8,679✔
1719
    if (ret < 0) {
8,679✔
1720
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1721
    }
1722
  }
1723

1724
#if 0
1725
  if (tbUids) {
1726
    if (taosArrayPush(tbUids, &uid) == NULL) {
1727
      rc = terrno;
1728
      goto _exit;
1729
    }
1730
  }
1731

1732
  if ((type == TSDB_CHILD_TABLE) && tbUid) {
1733
    *tbUid = uid;
1734
  }
1735
#endif
1736
  metaFetchEntryFree(&pChild);
1,618,701✔
1737
  metaFetchEntryFree(&pSuper);
1,618,174✔
1738
  return code;
1,617,835✔
1739
}
1740

1741
static int32_t metaHandleVirtualNormalTableDropImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
40,406✔
1742
  int32_t code = TSDB_CODE_SUCCESS;
40,406✔
1743

1744
  SMetaTableOp ops[] = {
40,406✔
1745
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1746
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1747
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1748
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1749

1750
      // {META_SCHEMA_TABLE, META_TABLE_OP_DELETE},  //
1751
  };
1752

1753
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
202,030✔
1754
    SMetaTableOp *op = &ops[i];
161,624✔
1755
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
161,624✔
1756
    if (code) {
161,624✔
1757
      const SMetaEntry *pEntry = pParam->pEntry;
×
1758
      metaErr(TD_VID(pMeta->pVnode), code);
×
1759
    }
1760
  }
1761

1762
  return code;
40,406✔
1763
}
1764

1765
static int32_t metaHandleVirtualNormalTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
40,406✔
1766
  int32_t     code = TSDB_CODE_SUCCESS;
40,406✔
1767
  SMetaEntry *pOldEntry = NULL;
40,406✔
1768

1769
  // fetch the entry
1770
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
40,406✔
1771
  if (code) {
40,406✔
1772
    metaErr(TD_VID(pMeta->pVnode), code);
×
1773
    return code;
×
1774
  }
1775

1776
  SMetaHandleParam param = {
40,406✔
1777
      .pEntry = pEntry,
1778
      .pOldEntry = pOldEntry,
1779
  };
1780

1781
  // do the drop
1782
  metaWLock(pMeta);
40,406✔
1783
  code = metaHandleVirtualNormalTableDropImpl(pMeta, &param);
40,406✔
1784
  metaULock(pMeta);
40,406✔
1785
  if (code) {
40,406✔
1786
    metaErr(TD_VID(pMeta->pVnode), code);
×
1787
    metaFetchEntryFree(&pOldEntry);
×
1788
    return code;
×
1789
  }
1790

1791
  // update other stuff
1792
  pMeta->pVnode->config.vndStats.numOfVTables--;
40,406✔
1793

1794
#if 0
1795
  if (tbUids) {
1796
    if (taosArrayPush(tbUids, &uid) == NULL) {
1797
      rc = terrno;
1798
      goto _exit;
1799
    }
1800
  }
1801
#endif
1802

1803
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
40,406✔
1804
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pOldEntry->uid, 0, NULL);
×
1805
    if (ret < 0) {
×
1806
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1807
    }
1808
  }
1809

1810
  metaFetchEntryFree(&pOldEntry);
40,406✔
1811
  return code;
40,406✔
1812
}
1813

1814
static int32_t metaHandleVirtualChildTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam, bool superDropped) {
31,621✔
1815
  int32_t code = TSDB_CODE_SUCCESS;
31,621✔
1816

1817
  const SMetaEntry *pEntry = pParam->pEntry;
31,621✔
1818
  const SMetaEntry *pChild = pParam->pOldEntry;
31,621✔
1819
  const SMetaEntry *pSuper = pParam->pSuperEntry;
31,621✔
1820

1821
  SMetaTableOp ops[] = {
31,621✔
1822
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1823
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1824
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1825
      {META_CHILD_IDX, META_TABLE_OP_DELETE},    //
1826
      {META_TAG_IDX, META_TABLE_OP_DELETE},      //
1827
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1828
  };
1829

1830
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
221,347✔
1831
    SMetaTableOp *op = &ops[i];
189,726✔
1832

1833
    if (op->table == META_ENTRY_TABLE && superDropped) {
189,726✔
1834
      continue;
×
1835
    }
1836

1837
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
189,726✔
1838
    if (code) {
189,726✔
1839
      metaErr(TD_VID(pMeta->pVnode), code);
×
1840
      return code;
×
1841
    }
1842
  }
1843

1844
  --pMeta->pVnode->config.vndStats.numOfVCTables;
31,621✔
1845
  metaUpdateStbStats(pMeta, pParam->pSuperEntry->uid, -1, 0, -1);
31,621✔
1846
  int32_t ret = metaUidCacheClear(pMeta, pSuper->uid);
31,621✔
1847
  if (ret < 0) {
31,621✔
1848
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1849
  }
1850

1851
  ret = metaStableTagFilterCacheUpdateUid(
31,621✔
1852
    pMeta, pChild, pSuper, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
1853
  if (ret < 0) {
31,621✔
1854
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1855
  }
1856

1857
  ret = metaTbGroupCacheClear(pMeta, pSuper->uid);
31,621✔
1858
  if (ret < 0) {
31,621✔
1859
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1860
  }
1861

1862
  ret = metaRefDbsCacheClear(pMeta, pSuper->uid);
31,621✔
1863
  if (ret < 0) {
31,621✔
1864
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1865
  }
1866

1867
  return code;
31,621✔
1868
}
1869

1870
static int32_t metaHandleVirtualChildTableDrop(SMeta *pMeta, const SMetaEntry *pEntry, bool superDropped) {
31,621✔
1871
  int32_t     code = TSDB_CODE_SUCCESS;
31,621✔
1872
  SMetaEntry *pChild = NULL;
31,621✔
1873
  SMetaEntry *pSuper = NULL;
31,621✔
1874

1875
  // fetch old entry
1876
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pChild);
31,621✔
1877
  if (code) {
31,621✔
1878
    metaErr(TD_VID(pMeta->pVnode), code);
×
1879
    return code;
×
1880
  }
1881

1882
  // fetch super entry
1883
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
31,621✔
1884
  if (code) {
31,621✔
1885
    metaErr(TD_VID(pMeta->pVnode), code);
×
1886
    metaFetchEntryFree(&pChild);
×
1887
    return code;
×
1888
  }
1889

1890
  SMetaHandleParam param = {
31,621✔
1891
      .pEntry = pEntry,
1892
      .pOldEntry = pChild,
1893
      .pSuperEntry = pSuper,
1894
  };
1895

1896
  // do the drop
1897
  metaWLock(pMeta);
31,621✔
1898
  code = metaHandleVirtualChildTableDropImpl(pMeta, &param, superDropped);
31,621✔
1899
  metaULock(pMeta);
31,621✔
1900
  if (code) {
31,621✔
1901
    metaErr(TD_VID(pMeta->pVnode), code);
×
1902
    metaFetchEntryFree(&pChild);
×
1903
    metaFetchEntryFree(&pSuper);
×
1904
    return code;
×
1905
  }
1906

1907
  metaFetchEntryFree(&pChild);
31,621✔
1908
  metaFetchEntryFree(&pSuper);
31,621✔
1909
  return code;
31,621✔
1910
}
1911

1912
int32_t metaGetChildUidsOfSuperTable(SMeta *pMeta, tb_uid_t suid, SArray **childList) {
1,028,543✔
1913
  int32_t code = TSDB_CODE_SUCCESS;
1,028,543✔
1914
  void   *key = NULL;
1,028,543✔
1915
  int32_t keySize = 0;
1,034,639✔
1916
  int32_t c;
1,033,443✔
1917

1918
  *childList = taosArrayInit(64, sizeof(tb_uid_t));
1,030,156✔
1919
  if (*childList == NULL) {
1,033,319✔
1920
    return terrno;
×
1921
  }
1922

1923
  TBC *cursor = NULL;
1,032,115✔
1924
  code = tdbTbcOpen(pMeta->pCtbIdx, &cursor, NULL);
1,032,714✔
1925
  if (code) {
1,033,377✔
1926
    taosArrayDestroy(*childList);
×
1927
    *childList = NULL;
×
1928
    return code;
×
1929
  }
1930

1931
  int32_t rc = tdbTbcMoveTo(cursor,
1,033,377✔
1932
                            &(SCtbIdxKey){
1,033,377✔
1933
                                .suid = suid,
1934
                                .uid = INT64_MIN,
1935
                            },
1936
                            sizeof(SCtbIdxKey), &c);
1937
  if (rc < 0) {
1,035,877✔
1938
    tdbTbcClose(cursor);
×
1939
    return 0;
×
1940
  }
1941

1942
  for (;;) {
1943
    if (tdbTbcNext(cursor, &key, &keySize, NULL, NULL) < 0) {
10,099,016✔
1944
      break;
620,336✔
1945
    }
1946

1947
    if (((SCtbIdxKey *)key)->suid < suid) {
9,478,393✔
1948
      continue;
109,175✔
1949
    } else if (((SCtbIdxKey *)key)->suid > suid) {
9,369,218✔
1950
      break;
416,198✔
1951
    }
1952

1953
    if (taosArrayPush(*childList, &(((SCtbIdxKey *)key)->uid)) == NULL) {
17,906,327✔
1954
      tdbFreeClear(key);
×
1955
      tdbTbcClose(cursor);
×
1956
      taosArrayDestroy(*childList);
×
1957
      *childList = NULL;
×
1958
      return terrno;
×
1959
    }
1960
  }
1961

1962
  tdbTbcClose(cursor);
1,036,534✔
1963
  tdbFreeClear(key);
1,035,278✔
1964
  return code;
1,035,877✔
1965
}
1966

1967
static int32_t metaHandleSuperTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
940,848✔
1968
  int32_t           code = TSDB_CODE_SUCCESS;
940,848✔
1969
  const SMetaEntry *pEntry = pParam->pEntry;
940,848✔
1970

1971
  SMetaTableOp ops[] = {
940,848✔
1972
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1973
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1974
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1975
      {META_SUID_IDX, META_TABLE_OP_DELETE},     //
1976

1977
      // {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: here should be insert
1978
  };
1979

1980
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
4,702,380✔
1981
    SMetaTableOp *op = &ops[i];
3,761,532✔
1982

1983
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
3,761,532✔
1984
    if (TSDB_CODE_SUCCESS != code) {
3,762,131✔
1985
      metaErr(TD_VID(pMeta->pVnode), code);
×
1986
      return code;
×
1987
    }
1988
  }
1989

1990
  int32_t ret = metaStatsCacheDrop(pMeta, pEntry->uid);
940,848✔
1991
  if (ret < 0) {
940,848✔
1992
    metaErr(TD_VID(pMeta->pVnode), ret);
322,227✔
1993
  }
1994
  return code;
940,848✔
1995
}
1996

1997
static int32_t metaHandleNormalTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
3,643,617✔
1998
  int32_t code = TSDB_CODE_SUCCESS;
3,643,617✔
1999

2000
  const SMetaEntry *pEntry = pParam->pEntry;
3,643,617✔
2001

2002
  SMetaTableOp ops[] = {
3,643,617✔
2003
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
2004
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
2005
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
2006
      {META_TTL_IDX, META_TABLE_OP_UPDATA},       //
2007
  };
2008
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
18,218,085✔
2009
    SMetaTableOp *op = &ops[i];
14,574,468✔
2010
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
14,574,468✔
2011
    if (code) {
14,574,468✔
2012
      metaErr(TD_VID(pMeta->pVnode), code);
×
2013
      return code;
×
2014
    }
2015
  }
2016
#if 0
2017
  if (metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs) < 0) {
2018
    metaError("vgId:%d, failed to update change time:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
2019
  }
2020
#endif
2021
  return code;
3,643,617✔
2022
}
2023

2024
static int32_t metaHandleVirtualNormalTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
226,189✔
2025
  int32_t code = TSDB_CODE_SUCCESS;
226,189✔
2026

2027
  const SMetaEntry *pEntry = pParam->pEntry;
226,189✔
2028

2029
  SMetaTableOp ops[] = {
226,189✔
2030
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
2031
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
2032
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
2033
  };
2034
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
904,756✔
2035
    SMetaTableOp *op = &ops[i];
678,567✔
2036
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
678,567✔
2037
    if (code) {
678,567✔
2038
      metaErr(TD_VID(pMeta->pVnode), code);
×
2039
      return code;
×
2040
    }
2041
  }
2042
#if 0
2043
  if (metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs) < 0) {
2044
    metaError("vgId:%d, failed to update change time:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
2045
  }
2046
#endif
2047
  return code;
226,189✔
2048
}
2049

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

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

2057
  SMetaTableOp ops[] = {
232,857✔
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
  };
2063

2064
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1,163,662✔
2065
    SMetaTableOp *op = &ops[i];
930,805✔
2066
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
930,805✔
2067
    if (code) {
931,428✔
2068
      metaErr(TD_VID(pMeta->pVnode), code);
×
2069
      return code;
×
2070
    }
2071
  }
2072

2073
  if (metaUidCacheClear(pMeta, pSuperEntry->uid) < 0) {
232,857✔
2074
    metaErr(TD_VID(pMeta->pVnode), code);
×
2075
  }
2076

2077
  // update stable tag filter cache: drop old then add new
2078
  code = metaStableTagFilterCacheUpdateUid(
232,857✔
2079
    pMeta, pOldEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
2080
  if (TSDB_CODE_SUCCESS != code) {
232,857✔
2081
    metaErr(TD_VID(pMeta->pVnode), code);
×
2082
  }
2083
  code = metaStableTagFilterCacheUpdateUid(
232,857✔
2084
    pMeta, pEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_ADD_TABLE);
2085
  if (TSDB_CODE_SUCCESS != code) {
232,857✔
2086
    metaErr(TD_VID(pMeta->pVnode), code);
×
2087
  }
2088

2089
  if (metaTbGroupCacheClear(pMeta, pSuperEntry->uid) < 0) {
232,857✔
2090
    metaErr(TD_VID(pMeta->pVnode), code);
×
2091
  }
2092

2093
  if (metaRefDbsCacheClear(pMeta, pSuperEntry->uid) < 0) {
232,234✔
2094
    metaErr(TD_VID(pMeta->pVnode), code);
×
2095
  }
2096
  return code;
232,857✔
2097
}
2098

2099
static int32_t metaHandleChildTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
8,077,211✔
2100
  int32_t code = TSDB_CODE_SUCCESS;
8,077,211✔
2101

2102
  const SMetaEntry *pEntry = pParam->pEntry;
8,077,211✔
2103
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
8,077,211✔
2104
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
8,077,211✔
2105

2106
  SMetaTableOp ops[] = {
8,077,211✔
2107
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},  //
2108
      {META_UID_IDX, META_TABLE_OP_UPDATA},      //
2109
      {META_TAG_IDX, META_TABLE_OP_UPDATA},      //
2110
      {META_CHILD_IDX, META_TABLE_OP_UPDATA},    //
2111
      {META_TTL_IDX, META_TABLE_OP_UPDATA},      //
2112
  };
2113

2114
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
48,462,564✔
2115
    SMetaTableOp *op = &ops[i];
40,385,353✔
2116
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
40,385,353✔
2117
    if (code) {
40,385,686✔
2118
      metaErr(TD_VID(pMeta->pVnode), code);
×
2119
      return code;
×
2120
    }
2121
  }
2122

2123
  if (metaUidCacheClear(pMeta, pSuperEntry->uid) < 0) {
8,077,211✔
2124
    metaErr(TD_VID(pMeta->pVnode), code);
×
2125
  }
2126

2127
  // update stable tag filter cache: drop old then add new
2128
  code = metaStableTagFilterCacheUpdateUid(
8,077,211✔
2129
    pMeta, pOldEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
2130
  if (TSDB_CODE_SUCCESS != code) {
8,077,211✔
2131
    metaErr(TD_VID(pMeta->pVnode), code);
×
2132
  }
2133
  code = metaStableTagFilterCacheUpdateUid(
8,077,211✔
2134
    pMeta, pEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_ADD_TABLE);
2135
  if (TSDB_CODE_SUCCESS != code) {
8,077,211✔
2136
    metaErr(TD_VID(pMeta->pVnode), code);
×
2137
  }
2138

2139
  if (metaTbGroupCacheClear(pMeta, pSuperEntry->uid) < 0) {
8,077,211✔
2140
    metaErr(TD_VID(pMeta->pVnode), code);
×
2141
  }
2142
  return code;
8,077,211✔
2143
#if 0
2144
  if (metaUpdateChangeTime(pMeta, ctbEntry.uid, pReq->ctimeMs) < 0) {
2145
    metaError("meta/table: failed to update change time:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
2146
  }
2147
#endif
2148
}
2149

2150
static int32_t metaHandleSuperTableUpdateImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
7,866,865✔
2151
  int32_t code = TSDB_CODE_SUCCESS;
7,866,865✔
2152

2153
  const SMetaEntry *pEntry = pParam->pEntry;
7,866,865✔
2154
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
7,868,772✔
2155

2156
  SMetaTableOp ops[] = {
7,868,388✔
2157
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
2158
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
2159
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
2160
  };
2161

2162
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
31,454,739✔
2163
    SMetaTableOp *op = &ops[i];
23,594,124✔
2164
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
23,602,244✔
2165
    if (code) {
23,590,413✔
2166
      metaErr(TD_VID(pMeta->pVnode), code);
×
2167
      return code;
×
2168
    }
2169
  }
2170

2171
  if (TSDB_CODE_SUCCESS == code) {
7,860,615✔
2172
    metaUpdateStbStats(pMeta, pEntry->uid, 0, pEntry->stbEntry.schemaRow.nCols - pOldEntry->stbEntry.schemaRow.nCols,
7,855,897✔
2173
                       pEntry->stbEntry.keep);
7,861,289✔
2174
  }
2175

2176
  return code;
7,860,560✔
2177
}
2178

2179
static int32_t metaHandleSuperTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
7,859,005✔
2180
  int32_t code = TSDB_CODE_SUCCESS;
7,859,005✔
2181

2182
  SMetaEntry *pOldEntry = NULL;
7,859,005✔
2183

2184
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
7,866,936✔
2185
  if (code) {
7,867,522✔
2186
    metaErr(TD_VID(pMeta->pVnode), code);
×
2187
    return code;
×
2188
  }
2189

2190
  SMetaHandleParam param = {
7,867,522✔
2191
      .pEntry = pEntry,
2192
      .pOldEntry = pOldEntry,
2193
  };
2194
  metaWLock(pMeta);
7,867,558✔
2195
  code = metaHandleSuperTableUpdateImpl(pMeta, &param);
7,865,536✔
2196
  metaULock(pMeta);
7,861,234✔
2197
  if (code) {
7,847,303✔
2198
    metaErr(TD_VID(pMeta->pVnode), code);
×
2199
    metaFetchEntryFree(&pOldEntry);
×
2200
    return code;
×
2201
  }
2202

2203
  int     nCols = pEntry->stbEntry.schemaRow.nCols;
7,847,303✔
2204
  int     onCols = pOldEntry->stbEntry.schemaRow.nCols;
7,865,435✔
2205
  int32_t deltaCol = nCols - onCols;
7,866,712✔
2206
  bool    updStat = deltaCol != 0 && !TABLE_IS_VIRTUAL(pEntry->flags) && !metaTbInFilterCache(pMeta, pEntry->name, 1);
7,866,712✔
2207

2208
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
7,864,023✔
2209
    STsdb  *pTsdb = pMeta->pVnode->pTsdb;
31,626✔
2210
    SArray *uids = NULL; /*taosArrayInit(8, sizeof(int64_t));
31,626✔
2211
     if (uids == NULL) {
2212
       metaErr(TD_VID(pMeta->pVnode), code);
2213
       metaFetchEntryFree(&pOldEntry);
2214
       return terrno;
2215
       }*/
2216
    if (deltaCol == 1) {
31,626✔
2217
      int16_t cid = pEntry->stbEntry.schemaRow.pSchema[nCols - 1].colId;
18,072✔
2218
      int8_t  col_type = pEntry->stbEntry.schemaRow.pSchema[nCols - 1].type;
18,072✔
2219

2220
      code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &uids);
18,072✔
2221
      if (code) {
18,072✔
2222
        metaErr(TD_VID(pMeta->pVnode), code);
×
2223
        metaFetchEntryFree(&pOldEntry);
×
2224
        return code;
×
2225
      }
2226
      if (pTsdb) {
18,072✔
2227
        TAOS_CHECK_RETURN(tsdbCacheNewSTableColumn(pTsdb, uids, cid, col_type));
18,072✔
2228
      }
2229
    } else if (deltaCol == -1) {
13,554✔
2230
      int16_t cid = -1;
13,554✔
2231
      bool    hasPrimaryKey = false;
13,554✔
2232
      if (onCols >= 2) {
13,554✔
2233
        hasPrimaryKey = (pOldEntry->stbEntry.schemaRow.pSchema[1].flags & COL_IS_KEY) ? true : false;
13,554✔
2234
      }
2235
      for (int i = 0, j = 0; i < nCols && j < onCols; ++i, ++j) {
131,022✔
2236
        if (pEntry->stbEntry.schemaRow.pSchema[i].colId != pOldEntry->stbEntry.schemaRow.pSchema[j].colId) {
126,504✔
2237
          cid = pOldEntry->stbEntry.schemaRow.pSchema[j].colId;
9,036✔
2238
          break;
9,036✔
2239
        }
2240
      }
2241

2242
      if (cid != -1) {
13,554✔
2243
        code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &uids);
9,036✔
2244
        if (code) {
9,036✔
2245
          metaErr(TD_VID(pMeta->pVnode), code);
×
2246
          metaFetchEntryFree(&pOldEntry);
×
2247
          return code;
×
2248
        }
2249
        if (pTsdb) {
9,036✔
2250
          TAOS_CHECK_RETURN(tsdbCacheDropSTableColumn(pTsdb, uids, cid, hasPrimaryKey));
9,036✔
2251
        }
2252
      }
2253
    }
2254
    if (uids) taosArrayDestroy(uids);
31,626✔
2255

2256
    if (pTsdb) {
31,626✔
2257
      tsdbCacheInvalidateSchema(pTsdb, pEntry->uid, -1, pEntry->stbEntry.schemaRow.version);
31,626✔
2258
    }
2259
  }
2260
  if (updStat) {
7,866,151✔
2261
    int64_t ctbNum = 0;
5,728,315✔
2262
    int32_t ret = metaGetStbStats(pMeta->pVnode, pEntry->uid, &ctbNum, 0, 0);
5,727,638✔
2263
    if (ret < 0) {
5,725,483✔
2264
      metaError("vgId:%d, failed to get stb stats:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
×
2265
                pEntry->uid, tstrerror(ret));
2266
    }
2267
    pMeta->pVnode->config.vndStats.numOfTimeSeries += (ctbNum * deltaCol);
5,725,502✔
2268
    if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
5,717,065✔
2269
  }
2270
  metaFetchEntryFree(&pOldEntry);
7,864,800✔
2271
  return code;
7,853,529✔
2272
}
2273

2274
static int32_t metaHandleChildTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
8,077,211✔
2275
  int32_t code = TSDB_CODE_SUCCESS;
8,077,211✔
2276

2277
  SMetaEntry *pOldEntry = NULL;
8,077,211✔
2278
  SMetaEntry *pSuperEntry = NULL;
8,077,211✔
2279

2280
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
8,077,211✔
2281
  if (code) {
8,077,211✔
2282
    metaErr(TD_VID(pMeta->pVnode), code);
×
2283
    return code;
×
2284
  }
2285

2286
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
8,077,211✔
2287
  if (code) {
8,077,211✔
2288
    metaErr(TD_VID(pMeta->pVnode), code);
×
2289
    metaFetchEntryFree(&pOldEntry);
×
2290
    return code;
×
2291
  }
2292

2293
  SMetaHandleParam param = {
8,077,211✔
2294
      .pEntry = pEntry,
2295
      .pOldEntry = pOldEntry,
2296
      .pSuperEntry = pSuperEntry,
2297
  };
2298

2299
  metaWLock(pMeta);
8,077,211✔
2300
  code = metaHandleChildTableUpdateImpl(pMeta, &param);
8,077,211✔
2301
  metaULock(pMeta);
8,077,211✔
2302
  if (code) {
8,076,509✔
2303
    metaErr(TD_VID(pMeta->pVnode), code);
×
2304
    metaFetchEntryFree(&pOldEntry);
×
2305
    metaFetchEntryFree(&pSuperEntry);
×
2306
    return code;
×
2307
  }
2308

2309
  metaFetchEntryFree(&pOldEntry);
8,076,509✔
2310
  metaFetchEntryFree(&pSuperEntry);
8,076,509✔
2311
  return code;
8,077,211✔
2312
}
2313

2314
static int32_t metaHandleNormalTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
3,643,617✔
2315
  int32_t     code = TSDB_CODE_SUCCESS;
3,643,617✔
2316
  SMetaEntry *pOldEntry = NULL;
3,643,617✔
2317

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

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

2339
  // do other stuff
2340
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) &&
3,643,617✔
2341
      pEntry->ntbEntry.schemaRow.version != pOldEntry->ntbEntry.schemaRow.version) {
25,927✔
2342
#if 0
2343
    {  // for add column
2344
      int16_t cid = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].colId;
2345
      int8_t  col_type = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].type;
2346
      int32_t ret = tsdbCacheNewNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, col_type);
2347
      if (ret < 0) {
2348
        terrno = ret;
2349
        goto _err;
2350
      }
2351
    }
2352
    {  // for drop column
2353

2354
      if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
2355
        int16_t cid = pColumn->colId;
2356

2357
        if (tsdbCacheDropNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, hasPrimayKey) != 0) {
2358
          metaError("vgId:%d, failed to drop ntable column:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name,
2359
                    entry.uid);
2360
        }
2361
        tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, entry.uid, pSchema->version);
2362
      }
2363
    }
2364
    }
2365
#endif
2366
    if (pMeta->pVnode->pTsdb) {
25,927✔
2367
      tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, pEntry->uid, pEntry->ntbEntry.schemaRow.version);
25,927✔
2368
    }
2369
  }
2370
  int32_t deltaCol = pEntry->ntbEntry.schemaRow.nCols - pOldEntry->ntbEntry.schemaRow.nCols;
3,643,617✔
2371
  pMeta->pVnode->config.vndStats.numOfNTimeSeries += deltaCol;
3,643,617✔
2372
  if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
3,643,617✔
2373
  metaFetchEntryFree(&pOldEntry);
3,643,617✔
2374
  return code;
3,643,617✔
2375
}
2376

2377
static int32_t metaHandleVirtualNormalTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
226,189✔
2378
  int32_t     code = TSDB_CODE_SUCCESS;
226,189✔
2379
  SMetaEntry *pOldEntry = NULL;
226,189✔
2380

2381
  // fetch old entry
2382
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
226,189✔
2383
  if (code) {
226,189✔
2384
    metaErr(TD_VID(pMeta->pVnode), code);
×
2385
    return code;
×
2386
  }
2387

2388
  // handle update
2389
  SMetaHandleParam param = {
226,189✔
2390
      .pEntry = pEntry,
2391
      .pOldEntry = pOldEntry,
2392
  };
2393
  metaWLock(pMeta);
226,189✔
2394
  code = metaHandleVirtualNormalTableUpdateImpl(pMeta, &param);
226,189✔
2395
  metaULock(pMeta);
226,189✔
2396
  if (code) {
226,189✔
2397
    metaErr(TD_VID(pMeta->pVnode), code);
×
2398
    metaFetchEntryFree(&pOldEntry);
×
2399
    return code;
×
2400
  }
2401

2402
  metaTimeSeriesNotifyCheck(pMeta);
226,189✔
2403
  metaFetchEntryFree(&pOldEntry);
226,189✔
2404
  return code;
226,189✔
2405
}
2406

2407
static int32_t metaHandleVirtualChildTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
232,857✔
2408
  int32_t code = TSDB_CODE_SUCCESS;
232,857✔
2409

2410
  SMetaEntry *pOldEntry = NULL;
232,857✔
2411
  SMetaEntry *pSuperEntry = NULL;
232,857✔
2412

2413
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
232,857✔
2414
  if (code) {
232,857✔
2415
    metaErr(TD_VID(pMeta->pVnode), code);
×
2416
    return code;
×
2417
  }
2418

2419
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
232,857✔
2420
  if (code) {
232,857✔
2421
    metaErr(TD_VID(pMeta->pVnode), code);
×
2422
    metaFetchEntryFree(&pOldEntry);
×
2423
    return code;
×
2424
  }
2425

2426
  SMetaHandleParam param = {
232,857✔
2427
      .pEntry = pEntry,
2428
      .pOldEntry = pOldEntry,
2429
      .pSuperEntry = pSuperEntry,
2430
  };
2431

2432
  metaWLock(pMeta);
232,857✔
2433
  code = metaHandleVirtualChildTableUpdateImpl(pMeta, &param);
232,857✔
2434
  metaULock(pMeta);
232,857✔
2435
  if (code) {
232,857✔
2436
    metaErr(TD_VID(pMeta->pVnode), code);
×
2437
    metaFetchEntryFree(&pOldEntry);
×
2438
    metaFetchEntryFree(&pSuperEntry);
×
2439
    return code;
×
2440
  }
2441

2442
  metaFetchEntryFree(&pOldEntry);
232,857✔
2443
  metaFetchEntryFree(&pSuperEntry);
232,857✔
2444
  return code;
232,857✔
2445
}
2446

2447
static int32_t metaHandleSuperTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
940,191✔
2448
  int32_t     code = TSDB_CODE_SUCCESS;
940,191✔
2449
  SArray     *childList = NULL;
940,191✔
2450
  SMetaEntry *pOldEntry = NULL;
939,592✔
2451

2452
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
940,191✔
2453
  if (code) {
938,336✔
2454
    metaErr(TD_VID(pMeta->pVnode), code);
×
2455
    return code;
×
2456
  }
2457

2458
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childList);
938,336✔
2459
  if (code) {
939,586✔
2460
    metaErr(TD_VID(pMeta->pVnode), code);
×
2461
    metaFetchEntryFree(&pOldEntry);
×
2462
    return code;
×
2463
  }
2464

2465
  if (pMeta->pVnode->pTsdb && tsdbCacheDropSubTables(pMeta->pVnode->pTsdb, childList, pEntry->uid) < 0) {
939,586✔
2466
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
2,810✔
2467
              pEntry->uid, tstrerror(terrno));
2468
  }
2469

2470
  // loop to drop all child tables
2471
  for (int32_t i = 0; i < taosArrayGetSize(childList); i++) {
1,771,152✔
2472
    SMetaEntry childEntry = {
831,477✔
2473
        .version = pEntry->version,
831,363✔
2474
        .uid = *(tb_uid_t *)taosArrayGet(childList, i),
830,985✔
2475
        .type = -TSDB_CHILD_TABLE,
2476
    };
2477

2478
    code = metaHandleChildTableDrop(pMeta, &childEntry, true);
831,363✔
2479
    if (code) {
831,363✔
2480
      metaErr(TD_VID(pMeta->pVnode), code);
21,642✔
2481
    }
2482
  }
2483

2484
  // do drop super table
2485
  SMetaHandleParam param = {
940,848✔
2486
      .pEntry = pEntry,
2487
      .pOldEntry = pOldEntry,
2488
  };
2489
  metaWLock(pMeta);
940,848✔
2490
  code = metaHandleSuperTableDropImpl(pMeta, &param);
940,848✔
2491
  metaULock(pMeta);
940,848✔
2492
  if (code) {
940,848✔
2493
    metaErr(TD_VID(pMeta->pVnode), code);
×
2494
    taosArrayDestroy(childList);
×
2495
    metaFetchEntryFree(&pOldEntry);
×
2496
    return code;
×
2497
  }
2498

2499
  // do other stuff
2500
  // metaUpdTimeSeriesNum(pMeta);
2501

2502
  // free resource and return
2503
  taosArrayDestroy(childList);
940,848✔
2504
  metaFetchEntryFree(&pOldEntry);
940,848✔
2505
  return code;
940,848✔
2506
}
2507

2508
int32_t metaHandleEntry2(SMeta *pMeta, const SMetaEntry *pEntry) {
106,984,967✔
2509
  int32_t   code = TSDB_CODE_SUCCESS;
106,984,967✔
2510
  int32_t   vgId = TD_VID(pMeta->pVnode);
106,984,967✔
2511
  SMetaInfo info = {0};
107,015,788✔
2512
  int8_t    type = pEntry->type > 0 ? pEntry->type : -pEntry->type;
106,994,576✔
2513

2514
  if (NULL == pMeta || NULL == pEntry) {
106,978,979✔
UNCOV
2515
    metaError("%s failed at %s:%d since invalid parameter", __func__, __FILE__, __LINE__);
×
2516
    return TSDB_CODE_INVALID_PARA;
×
2517
  }
2518

2519
  if (pEntry->type > 0) {
106,996,257✔
2520
    bool isExist = false;
104,308,801✔
2521
    if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) {
104,308,801✔
2522
      isExist = true;
20,045,031✔
2523
    }
2524

2525
    switch (type) {
104,321,311✔
2526
      case TSDB_SUPER_TABLE: {
13,327,162✔
2527
        if (isExist) {
13,327,162✔
2528
          code = metaHandleSuperTableUpdate(pMeta, pEntry);
7,851,350✔
2529
        } else {
2530
          code = metaHandleSuperTableCreate(pMeta, pEntry);
5,475,812✔
2531
        }
2532
        break;
13,349,754✔
2533
      }
2534
      case TSDB_CHILD_TABLE: {
78,781,019✔
2535
        if (isExist) {
78,781,019✔
2536
          code = metaHandleChildTableUpdate(pMeta, pEntry);
8,077,211✔
2537
        } else {
2538
          code = metaHandleChildTableCreate(pMeta, pEntry);
70,703,808✔
2539
        }
2540
        break;
78,802,216✔
2541
      }
2542
      case TSDB_NORMAL_TABLE: {
11,215,675✔
2543
        if (isExist) {
11,215,675✔
2544
          code = metaHandleNormalTableUpdate(pMeta, pEntry);
3,643,617✔
2545
        } else {
2546
          code = metaHandleNormalTableCreate(pMeta, pEntry);
7,572,058✔
2547
        }
2548
        break;
11,215,675✔
2549
      }
2550
      case TSDB_VIRTUAL_NORMAL_TABLE: {
419,041✔
2551
        if (isExist) {
419,041✔
2552
          code = metaHandleVirtualNormalTableUpdate(pMeta, pEntry);
226,189✔
2553
        } else {
2554
          code = metaHandleVirtualNormalTableCreate(pMeta, pEntry);
192,852✔
2555
        }
2556
        break;
419,041✔
2557
      }
2558
      case TSDB_VIRTUAL_CHILD_TABLE: {
578,395✔
2559
        if (isExist) {
578,395✔
2560
          code = metaHandleVirtualChildTableUpdate(pMeta, pEntry);
232,857✔
2561
        } else {
2562
          code = metaHandleVirtualChildTableCreate(pMeta, pEntry);
345,538✔
2563
        }
2564
        break;
578,395✔
2565
      }
2566
      default: {
19✔
2567
        code = TSDB_CODE_INVALID_PARA;
19✔
2568
        break;
19✔
2569
      }
2570
    }
2571
  } else {
2572
    switch (type) {
2,662,676✔
2573
      case TSDB_SUPER_TABLE: {
939,610✔
2574
        code = metaHandleSuperTableDrop(pMeta, pEntry);
939,610✔
2575
        break;
940,848✔
2576
      }
2577
      case TSDB_CHILD_TABLE: {
808,980✔
2578
        code = metaHandleChildTableDrop(pMeta, pEntry, false);
808,980✔
2579
        break;
808,980✔
2580
      }
2581
      case TSDB_NORMAL_TABLE: {
842,040✔
2582
        code = metaHandleNormalTableDrop(pMeta, pEntry);
842,040✔
2583
        break;
842,040✔
2584
      }
2585
      case TSDB_VIRTUAL_NORMAL_TABLE: {
40,406✔
2586
        code = metaHandleVirtualNormalTableDrop(pMeta, pEntry);
40,406✔
2587
        break;
40,406✔
2588
      }
2589
      case TSDB_VIRTUAL_CHILD_TABLE: {
31,621✔
2590
        code = metaHandleVirtualChildTableDrop(pMeta, pEntry, false);
31,621✔
2591
        break;
31,621✔
2592
      }
2593
      default: {
19✔
2594
        code = TSDB_CODE_INVALID_PARA;
19✔
2595
        break;
19✔
2596
      }
2597
    }
2598
  }
2599

2600
  if (TSDB_CODE_SUCCESS == code) {
107,029,014✔
2601
    pMeta->changed = true;
107,027,020✔
2602
    metaDebug("vgId:%d, index:%" PRId64 ", handle meta entry success, type:%d tb:%s uid:%" PRId64, vgId,
107,035,801✔
2603
              pEntry->version, pEntry->type, pEntry->type > 0 ? pEntry->name : "", pEntry->uid);
2604
  } else {
2605
    metaErr(vgId, code);
2,003✔
2606
  }
2607
  TAOS_RETURN(code);
107,038,758✔
2608
}
2609

2610
void metaHandleSyncEntry(SMeta *pMeta, const SMetaEntry *pEntry) {
161,677✔
2611
  int32_t code = TSDB_CODE_SUCCESS;
161,677✔
2612
  code = metaHandleEntry2(pMeta, pEntry);
161,677✔
2613
  if (code) {
161,677✔
2614
    metaErr(TD_VID(pMeta->pVnode), code);
×
2615
  }
2616
  return;
161,677✔
2617
}
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