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

taosdata / TDengine / #4087

17 May 2025 01:40AM UTC coverage: 62.757% (-0.3%) from 63.048%
#4087

push

travis-ci

GitHub
fix: double close wal meta file. (#31059)

156587 of 317922 branches covered (49.25%)

Branch coverage included in aggregate %.

241845 of 316955 relevant lines covered (76.3%)

6570724.62 hits per line

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

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

11
#include "meta.h"
12

13
extern SDmNotifyHandle dmNotifyHdl;
14

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

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

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

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

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

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

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

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

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

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

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

96
  tDecoderInit(&decoder, value, valueSize);
370,128✔
97
  code = metaDecodeEntry(&decoder, &entry);
370,058✔
98
  if (code) {
369,889!
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);
369,889✔
107
  if (code) {
369,996!
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);
369,996✔
116
  tDecoderClear(&decoder);
370,036✔
117
  return code;
370,039✔
118
}
119

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

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

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

141
void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); }
370,007✔
142

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

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

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

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

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

180
  // put to tdb
181
  if (META_TABLE_OP_INSERT == op) {
222,936✔
182
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
188,931✔
183
  } else if (META_TABLE_OP_UPDATA == op) {
34,005✔
184
    code = tdbTbUpsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
15,594✔
185
  } else if (META_TABLE_OP_DELETE == op) {
18,411!
186
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
18,411✔
187
  } else {
188
    code = TSDB_CODE_INVALID_PARA;
×
189
  }
190
  if (TSDB_CODE_SUCCESS != code) {
223,095!
191
    metaErr(vgId, code);
×
192
  }
193
  taosMemoryFree(value);
223,012!
194
  return code;
223,122✔
195
}
196

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

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

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

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

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

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

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

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

254
  // put to tdb
255
  if (META_TABLE_OP_INSERT == op) {
52,344!
256
    code = tdbTbInsert(pMeta->pSkmDb, &key, sizeof(key), value, valueSize, pMeta->txn);
×
257
  } else if (META_TABLE_OP_UPDATA == op) {
52,361!
258
    code = tdbTbUpsert(pMeta->pSkmDb, &key, sizeof(key), value, valueSize, pMeta->txn);
52,361✔
259
  } else {
260
    code = TSDB_CODE_INVALID_PARA;
×
261
  }
262
  if (TSDB_CODE_SUCCESS != code) {
52,554!
263
    metaErr(vgId, code);
×
264
  }
265
  taosMemoryFree(value);
52,531!
266
  return code;
52,562✔
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,
107,097✔
274
                                                 const SSchema *pOldColumn, const SSchema *pNewColumn) {
275
  int32_t code = TSDB_CODE_SUCCESS;
107,097✔
276

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

281
  if (pOldColumn && pNewColumn) {
107,097✔
282
    if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
104,679✔
283
      return TSDB_CODE_SUCCESS;
3,943✔
284
    } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) {
100,736!
285
      action = DROP_INDEX;
76✔
286
    } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
100,660!
287
      action = ADD_INDEX;
17✔
288
    } else {
289
      return TSDB_CODE_SUCCESS;
100,643✔
290
    }
291
  } else if (pOldColumn) {
2,418✔
292
    if (IS_IDX_ON(pOldColumn)) {
895✔
293
      action = DROP_INDEX;
45✔
294
    } else {
295
      return TSDB_CODE_SUCCESS;
850✔
296
    }
297
  } else {
298
    if (IS_IDX_ON(pNewColumn)) {
1,523!
299
      action = ADD_INDEX;
×
300
    } else {
301
      return TSDB_CODE_SUCCESS;
1,523✔
302
    }
303
  }
304

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

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

326
    STagIdxKey *pTagIdxKey = NULL;
266✔
327
    int32_t     tagIdxKeySize = 0;
266✔
328

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

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

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

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

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

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

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

410
    SMetaHandleParam param = {.pEntry = pChildEntry};
6✔
411

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

421
      code = metaEntryTableUpdate(pMeta, &param);
3✔
422
      if (code) {
3!
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✔
430
      if (code) {
3!
431
        metaErr(TD_VID(pMeta->pVnode), code);
×
432
        taosArrayDestroy(childTables);
×
433
        metaFetchEntryFree(&pChildEntry);
×
434
        return code;
×
435
      }
436

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

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

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

459
  int32_t iOld = 0, iNew = 0;
4,195✔
460
  for (; iOld < pOldTagSchema->nCols && iNew < pNewTagSchema->nCols;) {
109,588✔
461
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
105,391✔
462
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
105,391✔
463

464
    if (pOldColumn->colId == pNewColumn->colId) {
105,391✔
465
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
104,675✔
466
      if (code) {
104,680✔
467
        metaErr(TD_VID(pMeta->pVnode), code);
4!
468
        return code;
×
469
      }
470

471
      iOld++;
104,676✔
472
      iNew++;
104,676✔
473
    } else if (pOldColumn->colId < pNewColumn->colId) {
716!
474
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
716✔
475
      if (code) {
716!
476
        metaErr(TD_VID(pMeta->pVnode), code);
×
477
        return code;
×
478
      }
479

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

488
      iNew++;
×
489
    }
490
  }
491

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

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

510
  return code;
4,196✔
511
}
512

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

520
  int32_t iOld = 0, iNew = 0;
12✔
521
  for (; iOld < pOldRowSchema->nCols && iNew < pNewRowSchema->nCols;) {
48✔
522
    SSchema *pOldColumn = pOldRowSchema->pSchema + iOld;
36✔
523
    SSchema *pNewColumn = pNewRowSchema->pSchema + iNew;
36✔
524

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

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

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

549
      iNew++;
×
550
    }
551
  }
552

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

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

571
  return code;
12✔
572
}
573

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

577
  const SMetaEntry *pEntry = pParam->pEntry;
56,687✔
578
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
56,687✔
579

580
  if (NULL == pOldEntry) {
56,687✔
581
    return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
44,428✔
582
  }
583

584
  if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
12,259✔
585
    // check row schema
586
    if (pOldEntry->ntbEntry.schemaRow.version != pEntry->ntbEntry.schemaRow.version) {
1,251!
587
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
1,281✔
588
    }
589
  } else if (pEntry->type == TSDB_SUPER_TABLE) {
11,008!
590
    // check row schema
591
    if (pOldEntry->stbEntry.schemaRow.version != pEntry->stbEntry.schemaRow.version) {
11,014✔
592
      if (TABLE_IS_VIRTUAL(pEntry->flags)) {
6,813✔
593
        return metaUpdateSuperTableRowSchema(pMeta, pParam);
12✔
594
      } else {
595
        return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
6,801✔
596
      }
597
    }
598

599
    // check tag schema
600
    code = metaUpdateSuperTableTagSchema(pMeta, pParam);
4,201✔
601
    if (code) {
4,195!
602
      metaErr(TD_VID(pMeta->pVnode), code);
×
603
      return code;
×
604
    }
605

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

610
  return TSDB_CODE_SUCCESS;
4,262✔
611
}
612

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

618
// Uid Index
619
static void metaBuildEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) {
204,525✔
620
  pInfo->uid = pEntry->uid;
204,525✔
621
  pInfo->version = pEntry->version;
204,525✔
622
  if (pEntry->type == TSDB_SUPER_TABLE) {
204,525✔
623
    pInfo->suid = pEntry->uid;
40,433✔
624
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
40,433✔
625
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
164,092✔
626
    pInfo->suid = pEntry->ctbEntry.suid;
147,730✔
627
    pInfo->skmVer = 0;
147,730✔
628
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
16,362!
629
    pInfo->suid = 0;
16,362✔
630
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
16,362✔
631
  }
632
}
204,525✔
633

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

638
  const SMetaEntry *pEntry = pParam->pEntry;
204,519✔
639

640
  // update cache
641
  SMetaInfo info = {0};
204,519✔
642
  metaBuildEntryInfo(pEntry, &info);
204,519✔
643
  code = metaCacheUpsert(pMeta, &info);
204,658✔
644
  if (code) {
204,627!
645
    metaErr(vgId, code);
×
646
  }
647

648
  // put to tdb
649
  SUidIdxVal value = {
204,515✔
650
      .suid = info.suid,
204,515✔
651
      .skmVer = info.skmVer,
204,515✔
652
      .version = pEntry->version,
204,515✔
653
  };
654
  if (META_TABLE_OP_INSERT == op) {
204,515✔
655
    code = tdbTbInsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
188,952✔
656
  } else if (META_TABLE_OP_UPDATA == op) {
15,563!
657
    code = tdbTbUpsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
15,577✔
658
  }
659
  return code;
204,784✔
660
}
661

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

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

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

673
  const SMetaEntry *pEntry = pParam->pOldEntry;
24,283✔
674

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

681
  // delete cache
682
  (void)metaCacheDrop(pMeta, pEntry->uid);
24,285✔
683
  return code;
24,287✔
684
}
685

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

690
  const SMetaEntry *pEntry = pParam->pEntry;
188,981✔
691

692
  if (META_TABLE_OP_INSERT == op) {
188,981!
693
    code = tdbTbInsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
189,027✔
694
                       pMeta->txn);
695
  } else if (META_TABLE_OP_UPDATA == op) {
×
696
    code = tdbTbUpsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
×
697
                       pMeta->txn);
698
  } else {
699
    code = TSDB_CODE_INVALID_PARA;
×
700
  }
701
  return code;
189,178✔
702
}
703

704
static int32_t metaNameIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
189,023✔
705
  int32_t code = TSDB_CODE_SUCCESS;
189,023✔
706
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
189,023✔
707
}
708

709
static int32_t metaNameIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
710
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
×
711
}
712

713
static int32_t metaNameIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
24,285✔
714
  int32_t code = TSDB_CODE_SUCCESS;
24,285✔
715

716
  const SMetaEntry *pEntry = pParam->pOldEntry;
24,285✔
717
  code = tdbTbDelete(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, pMeta->txn);
24,285✔
718
  if (code) {
24,286!
719
    metaErr(TD_VID(pMeta->pVnode), code);
×
720
  }
721
  return code;
24,286✔
722
}
723

724
// Suid Index
725
static int32_t metaSUidIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
29,401✔
726
  const SMetaEntry *pEntry = pParam->pEntry;
29,401✔
727

728
  int32_t code = tdbTbInsert(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), NULL, 0, pMeta->txn);
29,401✔
729
  if (code) {
29,466!
730
    metaErr(TD_VID(pMeta->pVnode), code);
×
731
  }
732
  return code;
29,408✔
733
}
734

735
static int32_t metaSUidIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
2,881✔
736
  const SMetaEntry *pEntry = pParam->pOldEntry;
2,881✔
737

738
  int32_t code = tdbTbDelete(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), pMeta->txn);
2,881✔
739
  if (code) {
2,881!
740
    metaErr(TD_VID(pMeta->pVnode), code);
×
741
  }
742
  return code;
2,881✔
743
}
744

745
// Child Index
746
static int32_t metaChildIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
147,342✔
747
  int32_t code = TSDB_CODE_SUCCESS;
147,342✔
748

749
  const SMetaEntry *pEntry = pParam->pEntry;
147,342✔
750

751
  SCtbIdxKey key = {
147,342✔
752
      .suid = pEntry->ctbEntry.suid,
147,342✔
753
      .uid = pEntry->uid,
147,342✔
754
  };
755

756
  if (META_TABLE_OP_INSERT == op) {
147,342✔
757
    code = tdbTbInsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
144,670✔
758
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
144,670✔
759
  } else if (META_TABLE_OP_UPDATA == op) {
2,672!
760
    code = tdbTbUpsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
2,679✔
761
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
2,679✔
762
  } else {
763
    code = TSDB_CODE_INVALID_PARA;
×
764
  }
765
  return code;
147,350✔
766
}
767

768
static int32_t metaChildIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
144,659✔
769
  return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
144,659✔
770
}
771

772
static int32_t metaChildIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
3,222✔
773
  const SMetaEntry *pEntry = pParam->pEntry;
3,222✔
774
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
3,222✔
775
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
3,222✔
776

777
  const STag *pNewTags = (const STag *)pEntry->ctbEntry.pTags;
3,222✔
778
  const STag *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags;
3,222✔
779
  if (pNewTags->len != pOldTags->len || memcmp(pNewTags, pOldTags, pNewTags->len)) {
3,222✔
780
    return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
2,679✔
781
  }
782
  return 0;
543✔
783
}
784

785
static int32_t metaChildIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
19,177✔
786
  const SMetaEntry *pEntry = pParam->pOldEntry;
19,177✔
787

788
  SCtbIdxKey key = {
19,177✔
789
      .suid = pEntry->ctbEntry.suid,
19,177✔
790
      .uid = pEntry->uid,
19,177✔
791
  };
792
  return tdbTbDelete(pMeta->pCtbIdx, &key, sizeof(key), pMeta->txn);
19,177✔
793
}
794

795
// Tag Index
796
static int32_t metaFetchTagIdxKey(SMeta *pMeta, const SMetaEntry *pEntry, const SSchema *pTagColumn,
168,775✔
797
                                  STagIdxKey **ppTagIdxKey, int32_t *pTagIdxKeySize) {
798
  int32_t code = TSDB_CODE_SUCCESS;
168,775✔
799

800
  STagIdxKey *pTagIdxKey = NULL;
168,775✔
801
  int32_t     nTagIdxKey;
802
  const void *pTagData = NULL;
168,775✔
803
  int32_t     nTagData = 0;
168,775✔
804

805
  STagVal tagVal = {
168,775✔
806
      .cid = pTagColumn->colId,
168,775✔
807
  };
808

809
  if (tTagGet((const STag *)pEntry->ctbEntry.pTags, &tagVal)) {
168,775✔
810
    if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
166,727!
811
      pTagData = tagVal.pData;
38,107✔
812
      nTagData = (int32_t)tagVal.nData;
38,107✔
813
    } else {
814
      pTagData = &(tagVal.i64);
128,620✔
815
      nTagData = tDataTypes[pTagColumn->type].bytes;
128,620✔
816
    }
817
  } else {
818
    if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
2,082!
819
      nTagData = tDataTypes[pTagColumn->type].bytes;
1,615✔
820
    }
821
  }
822

823
  code = metaCreateTagIdxKey(pEntry->ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type,
168,809✔
824
                             pEntry->uid, &pTagIdxKey, &nTagIdxKey);
168,809✔
825
  if (code) {
168,795✔
826
    metaErr(TD_VID(pMeta->pVnode), code);
8!
827
    return code;
×
828
  }
829

830
  *ppTagIdxKey = pTagIdxKey;
168,787✔
831
  *pTagIdxKeySize = nTagIdxKey;
168,787✔
832
  return code;
168,787✔
833
}
834

835
static void metaFetchTagIdxKeyFree(STagIdxKey **ppTagIdxKey) {
168,805✔
836
  metaDestroyTagIdxKey(*ppTagIdxKey);
168,805✔
837
  *ppTagIdxKey = NULL;
168,802✔
838
}
168,802✔
839

840
static int32_t metaTagIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
144,649✔
841
  int32_t code = TSDB_CODE_SUCCESS;
144,649✔
842

843
  const SMetaEntry *pEntry = pParam->pEntry;
144,649✔
844
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
144,649✔
845

846
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
144,649✔
847
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
145,078✔
848
    const SSchema *pTagColumn = &pTagSchema->pSchema[0];
429✔
849

850
    STagVal tagVal = {
429✔
851
        .cid = pTagColumn->colId,
429✔
852
    };
853

854
    const void *pTagData = pEntry->ctbEntry.pTags;
429✔
855
    int32_t     nTagData = ((const STag *)pEntry->ctbEntry.pTags)->len;
429✔
856
    code = metaSaveJsonVarToIdx(pMeta, pEntry, pTagColumn);
429✔
857
    if (code) {
429!
858
      metaErr(TD_VID(pMeta->pVnode), code);
×
859
    }
860
  } else {
861
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
628,690✔
862
      STagIdxKey    *pTagIdxKey = NULL;
484,447✔
863
      int32_t        nTagIdxKey;
864
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
484,447✔
865

866
      if (!IS_IDX_ON(pTagColumn)) {
484,447✔
867
        continue;
340,249✔
868
      }
869

870
      code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pTagIdxKey, &nTagIdxKey);
144,198✔
871
      if (code) {
144,202!
872
        metaErr(TD_VID(pMeta->pVnode), code);
×
873
        return code;
×
874
      }
875

876
      code = tdbTbInsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn);
144,202✔
877
      if (code) {
144,220!
878
        metaErr(TD_VID(pMeta->pVnode), code);
×
879
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
880
        return code;
×
881
      }
882
      metaFetchTagIdxKeyFree(&pTagIdxKey);
144,220✔
883
    }
884
  }
885
  return code;
144,672✔
886
}
887

888
static int32_t metaTagIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
3,222✔
889
  int32_t code = TSDB_CODE_SUCCESS;
3,222✔
890

891
  const SMetaEntry     *pEntry = pParam->pEntry;
3,222✔
892
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
3,222✔
893
  const SMetaEntry     *pSuperEntry = pParam->pSuperEntry;
3,222✔
894
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
3,222✔
895
  const STag           *pNewTags = (const STag *)pEntry->ctbEntry.pTags;
3,222✔
896
  const STag           *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags;
3,222✔
897

898
  if (pNewTags->len == pOldTags->len && !memcmp(pNewTags, pOldTags, pNewTags->len)) {
3,222✔
899
    return code;
543✔
900
  }
901

902
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
2,679✔
903
    code = metaDelJsonVarFromIdx(pMeta, pOldEntry, &pTagSchema->pSchema[0]);
8✔
904
    if (code) {
8!
905
      metaErr(TD_VID(pMeta->pVnode), code);
×
906
      return code;
×
907
    }
908

909
    code = metaSaveJsonVarToIdx(pMeta, pEntry, &pTagSchema->pSchema[0]);
8✔
910
    if (code) {
8!
911
      metaErr(TD_VID(pMeta->pVnode), code);
×
912
      return code;
×
913
    }
914
  } else {
915
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
9,327✔
916
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
6,656✔
917

918
      if (!IS_IDX_ON(pTagColumn)) {
6,656✔
919
        continue;
3,989✔
920
      }
921

922
      STagIdxKey *pOldTagIdxKey = NULL;
2,667✔
923
      int32_t     oldTagIdxKeySize = 0;
2,667✔
924
      STagIdxKey *pNewTagIdxKey = NULL;
2,667✔
925
      int32_t     newTagIdxKeySize = 0;
2,667✔
926

927
      code = metaFetchTagIdxKey(pMeta, pOldEntry, pTagColumn, &pOldTagIdxKey, &oldTagIdxKeySize);
2,667✔
928
      if (code) {
2,667!
929
        metaErr(TD_VID(pMeta->pVnode), code);
×
930
        return code;
×
931
      }
932

933
      code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pNewTagIdxKey, &newTagIdxKeySize);
2,667✔
934
      if (code) {
2,667!
935
        metaErr(TD_VID(pMeta->pVnode), code);
×
936
        metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
937
        return code;
×
938
      }
939

940
      if (tagIdxKeyCmpr(pOldTagIdxKey, oldTagIdxKeySize, pNewTagIdxKey, newTagIdxKeySize)) {
2,667✔
941
        code = tdbTbDelete(pMeta->pTagIdx, pOldTagIdxKey, oldTagIdxKeySize, pMeta->txn);
1,171✔
942
        if (code) {
1,171!
943
          metaErr(TD_VID(pMeta->pVnode), code);
×
944
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
945
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
946
          return code;
×
947
        }
948

949
        code = tdbTbInsert(pMeta->pTagIdx, pNewTagIdxKey, newTagIdxKeySize, NULL, 0, pMeta->txn);
1,171✔
950
        if (code) {
1,171!
951
          metaErr(TD_VID(pMeta->pVnode), code);
×
952
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
953
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
954
          return code;
×
955
        }
956
      }
957

958
      metaFetchTagIdxKeyFree(&pOldTagIdxKey);
2,667✔
959
      metaFetchTagIdxKeyFree(&pNewTagIdxKey);
2,667✔
960
    }
961
  }
962
  return code;
2,679✔
963
}
964

965
static int32_t metaTagIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
19,178✔
966
  int32_t code = TSDB_CODE_SUCCESS;
19,178✔
967

968
  const SMetaEntry     *pEntry = pParam->pEntry;
19,178✔
969
  const SMetaEntry     *pChild = pParam->pOldEntry;
19,178✔
970
  const SMetaEntry     *pSuper = pParam->pSuperEntry;
19,178✔
971
  const SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
19,178✔
972
  const SSchema        *pTagColumn = NULL;
19,178✔
973
  const STag           *pTags = (const STag *)pChild->ctbEntry.pTags;
19,178✔
974

975
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
19,178✔
976
    pTagColumn = &pTagSchema->pSchema[0];
196✔
977
    code = metaDelJsonVarFromIdx(pMeta, pChild, pTagColumn);
196✔
978
    if (code) {
196!
979
      metaErr(TD_VID(pMeta->pVnode), code);
×
980
    }
981
  } else {
982
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
68,473✔
983
      pTagColumn = &pTagSchema->pSchema[i];
49,492✔
984
      if (!IS_IDX_ON(pTagColumn)) {
49,492✔
985
        continue;
30,505✔
986
      }
987

988
      STagIdxKey *pTagIdxKey = NULL;
18,987✔
989
      int32_t     nTagIdxKey;
990

991
      code = metaFetchTagIdxKey(pMeta, pChild, pTagColumn, &pTagIdxKey, &nTagIdxKey);
18,987✔
992
      if (code) {
18,985!
993
        metaErr(TD_VID(pMeta->pVnode), code);
×
994
        return code;
×
995
      }
996

997
      code = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
18,985✔
998
      if (code) {
18,986!
999
        metaErr(TD_VID(pMeta->pVnode), code);
×
1000
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
1001
        return code;
×
1002
      }
1003
      metaFetchTagIdxKeyFree(&pTagIdxKey);
18,986✔
1004
    }
1005
  }
1006
  return code;
19,177✔
1007
}
1008

1009
// Btime Index
1010
static int32_t metaBtimeIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
181,050✔
1011
  int32_t code = TSDB_CODE_SUCCESS;
181,050✔
1012

1013
  const SMetaEntry *pEntry;
1014
  if (META_TABLE_OP_DELETE == op) {
181,050✔
1015
    pEntry = pParam->pOldEntry;
21,403✔
1016
  } else {
1017
    pEntry = pParam->pEntry;
159,647✔
1018
  }
1019

1020
  SBtimeIdxKey key = {
181,050✔
1021
      .uid = pEntry->uid,
181,050✔
1022
  };
1023

1024
  if (TSDB_CHILD_TABLE == pEntry->type || TSDB_VIRTUAL_CHILD_TABLE == pEntry->type) {
181,050✔
1025
    key.btime = pEntry->ctbEntry.btime;
163,810✔
1026
  } else if (TSDB_NORMAL_TABLE == pEntry->type || TSDB_VIRTUAL_NORMAL_TABLE == pEntry->type) {
17,240!
1027
    key.btime = pEntry->ntbEntry.btime;
17,240✔
1028
  } else {
1029
    return TSDB_CODE_INVALID_PARA;
×
1030
  }
1031

1032
  if (META_TABLE_OP_INSERT == op) {
181,050✔
1033
    code = tdbTbInsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
159,650✔
1034
  } else if (META_TABLE_OP_UPDATA == op) {
21,400!
1035
    code = tdbTbUpsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
×
1036
  } else if (META_TABLE_OP_DELETE == op) {
21,400!
1037
    code = tdbTbDelete(pMeta->pBtimeIdx, &key, sizeof(key), pMeta->txn);
21,403✔
1038
  } else {
1039
    code = TSDB_CODE_INVALID_PARA;
×
1040
  }
1041
  if (code) {
181,091!
1042
    metaErr(TD_VID(pMeta->pVnode), code);
×
1043
  }
1044
  return code;
181,086✔
1045
}
1046

1047
static int32_t metaBtimeIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
159,649✔
1048
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
159,649✔
1049
}
1050

1051
static int32_t metaBtimeIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
1052
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
×
1053
}
1054

1055
static int32_t metaBtimeIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
21,403✔
1056
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_DELETE);
21,403✔
1057
}
1058

1059
// TTL Index
1060
static int32_t metaTtlIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
159,708✔
1061
  const SMetaEntry *pEntry = pParam->pEntry;
159,708✔
1062

1063
  STtlUpdTtlCtx ctx = {
159,708✔
1064
      .uid = pEntry->uid,
159,708✔
1065
      .pTxn = pMeta->txn,
159,708✔
1066
  };
1067
  if (TSDB_CHILD_TABLE == pEntry->type) {
159,708✔
1068
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
144,692✔
1069
    ctx.changeTimeMs = pEntry->ctbEntry.btime;
144,692✔
1070
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
15,016!
1071
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
15,029✔
1072
    ctx.changeTimeMs = pEntry->ntbEntry.btime;
15,029✔
1073
  } else {
1074
    return TSDB_CODE_INVALID_PARA;
×
1075
  }
1076

1077
  int32_t ret = ttlMgrInsertTtl(pMeta->pTtlMgr, &ctx);
159,721✔
1078
  if (ret < 0) {
159,679!
1079
    metaError("vgId:%d, failed to insert ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid, tstrerror(ret));
×
1080
  }
1081
  return TSDB_CODE_SUCCESS;
159,684✔
1082
}
1083

1084
static int32_t metaTtlIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
159,697✔
1085
  return metaTtlIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
159,697✔
1086
}
1087

1088
static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam);
1089

1090
static int32_t metaTtlIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
4,566✔
1091
  int32_t code = TSDB_CODE_SUCCESS;
4,566✔
1092

1093
  const SMetaEntry *pEntry = pParam->pEntry;
4,566✔
1094
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
4,566✔
1095

1096
  if ((pEntry->type == TSDB_CHILD_TABLE && pOldEntry->ctbEntry.ttlDays != pEntry->ctbEntry.ttlDays) ||
4,566✔
1097
      (pEntry->type == TSDB_NORMAL_TABLE && pOldEntry->ntbEntry.ttlDays != pEntry->ntbEntry.ttlDays)) {
4,546✔
1098
    code = metaTtlIdxDelete(pMeta, pParam);
41✔
1099
    if (code) {
41!
1100
      metaErr(TD_VID(pMeta->pVnode), code);
×
1101
    }
1102

1103
    code = metaTtlIdxInsert(pMeta, pParam);
41✔
1104
    if (code) {
41!
1105
      metaErr(TD_VID(pMeta->pVnode), code);
×
1106
    }
1107
  }
1108

1109
  return TSDB_CODE_SUCCESS;
4,566✔
1110
}
1111

1112
static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
21,439✔
1113
  int32_t code = TSDB_CODE_SUCCESS;
21,439✔
1114

1115
  const SMetaEntry *pEntry = pParam->pOldEntry;
21,439✔
1116
  STtlDelTtlCtx     ctx = {
21,439✔
1117
          .uid = pEntry->uid,
21,439✔
1118
          .pTxn = pMeta->txn,
21,439✔
1119
  };
1120

1121
  if (TSDB_CHILD_TABLE == pEntry->type) {
21,439✔
1122
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
19,194✔
1123
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
2,245!
1124
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
2,246✔
1125
  } else {
1126
    code = TSDB_CODE_INVALID_PARA;
×
1127
  }
1128

1129
  if (TSDB_CODE_SUCCESS == code) {
21,439!
1130
    int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
21,440✔
1131
    if (ret < 0) {
21,439!
1132
      metaError("vgId:%d, failed to delete ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid,
×
1133
                tstrerror(ret));
1134
    }
1135
  }
1136
  return code;
21,440✔
1137
}
1138

1139
static void metaTimeSeriesNotifyCheck(SMeta *pMeta) {
164,503✔
1140
#if defined(TD_ENTERPRISE)
1141
  int64_t nTimeSeries = metaGetTimeSeriesNum(pMeta, 0);
164,503✔
1142
  int64_t deltaTS = nTimeSeries - pMeta->pVnode->config.vndStats.numOfReportedTimeSeries;
164,572✔
1143
  if (deltaTS > tsTimeSeriesThreshold) {
164,572✔
1144
    if (0 == atomic_val_compare_exchange_8(&dmNotifyHdl.state, 1, 2)) {
95,206✔
1145
      if (tsem_post(&dmNotifyHdl.sem) != 0) {
95,197!
1146
        metaError("vgId:%d, failed to post semaphore, errno:%d", TD_VID(pMeta->pVnode), ERRNO);
×
1147
      }
1148
    }
1149
  }
1150
#endif
1151
}
164,590✔
1152

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

1211
static int32_t metaHandleSuperTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
29,439✔
1212
  int32_t code = TSDB_CODE_SUCCESS;
29,439✔
1213

1214
  SMetaTableOp ops[] = {
29,439✔
1215
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1216
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: here should be insert
1217
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1218
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1219
      {META_SUID_IDX, META_TABLE_OP_INSERT},      //
1220
  };
1221

1222
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
176,444✔
1223
    SMetaTableOp          *op = &ops[i];
147,110✔
1224
    const SMetaHandleParam param = {
147,110✔
1225
        .pEntry = pEntry,
1226
    };
1227
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
147,110✔
1228
    if (TSDB_CODE_SUCCESS != code) {
146,906!
1229
      metaErr(TD_VID(pMeta->pVnode), code);
×
1230
      return code;
×
1231
    }
1232
  }
1233

1234
  return code;
29,334✔
1235
}
1236
static int32_t metaHandleSuperTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
28,866✔
1237
  int32_t code = TSDB_CODE_SUCCESS;
28,866✔
1238

1239
  metaWLock(pMeta);
28,866✔
1240
  code = metaHandleSuperTableCreateImpl(pMeta, pEntry);
29,500✔
1241
  metaULock(pMeta);
29,402✔
1242

1243
  if (TSDB_CODE_SUCCESS == code) {
29,501!
1244
    pMeta->pVnode->config.vndStats.numOfSTables++;
29,501✔
1245

1246
    metaInfo("vgId:%d, %s success, version:%" PRId64 " type:%d uid:%" PRId64 " name:%s", TD_VID(pMeta->pVnode),
29,501✔
1247
             __func__, pEntry->version, pEntry->type, pEntry->uid, pEntry->name);
1248
  } else {
1249
    metaErr(TD_VID(pMeta->pVnode), code);
×
1250
  }
1251
  return code;
29,519✔
1252
}
1253

1254
static int32_t metaHandleNormalTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
15,008✔
1255
  int32_t code = TSDB_CODE_SUCCESS;
15,008✔
1256

1257
  SMetaTableOp ops[] = {
15,008✔
1258
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1259
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: need to be insert
1260
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1261
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1262
      {META_BTIME_IDX, META_TABLE_OP_INSERT},     //
1263
      {META_TTL_IDX, META_TABLE_OP_INSERT},       //
1264
  };
1265

1266
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
105,047✔
1267
    SMetaTableOp *op = &ops[i];
90,041✔
1268

1269
    SMetaHandleParam param = {
90,041✔
1270
        .pEntry = pEntry,
1271
    };
1272

1273
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
90,041✔
1274
    if (TSDB_CODE_SUCCESS != code) {
90,040✔
1275
      metaErr(TD_VID(pMeta->pVnode), code);
1!
1276
      return code;
×
1277
    }
1278
  }
1279

1280
  return code;
15,006✔
1281
}
1282
static int32_t metaHandleNormalTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
15,008✔
1283
  int32_t code = TSDB_CODE_SUCCESS;
15,008✔
1284

1285
  // update TDB
1286
  metaWLock(pMeta);
15,008✔
1287
  code = metaHandleNormalTableCreateImpl(pMeta, pEntry);
15,008✔
1288
  metaULock(pMeta);
15,008✔
1289

1290
  // update other stuff
1291
  if (TSDB_CODE_SUCCESS == code) {
15,008!
1292
    pMeta->pVnode->config.vndStats.numOfNTables++;
15,008✔
1293
    pMeta->pVnode->config.vndStats.numOfNTimeSeries += pEntry->ntbEntry.schemaRow.nCols - 1;
15,008✔
1294

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

1308
static int32_t metaHandleChildTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry, const SMetaEntry *pSuperEntry) {
144,634✔
1309
  int32_t code = TSDB_CODE_SUCCESS;
144,634✔
1310

1311
  SMetaTableOp ops[] = {
144,634✔
1312
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},  //
1313
      {META_UID_IDX, META_TABLE_OP_INSERT},      //
1314
      {META_NAME_IDX, META_TABLE_OP_INSERT},     //
1315
      {META_CHILD_IDX, META_TABLE_OP_INSERT},    //
1316
      {META_TAG_IDX, META_TABLE_OP_INSERT},      //
1317
      {META_BTIME_IDX, META_TABLE_OP_INSERT},    //
1318
      {META_TTL_IDX, META_TABLE_OP_INSERT},      //
1319
  };
1320

1321
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1,156,385✔
1322
    SMetaTableOp *op = &ops[i];
1,011,889✔
1323

1324
    SMetaHandleParam param = {
1,011,889✔
1325
        .pEntry = pEntry,
1326
        .pSuperEntry = pSuperEntry,
1327
    };
1328

1329
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
1,011,889✔
1330
    if (TSDB_CODE_SUCCESS != code) {
1,011,703!
1331
      metaErr(TD_VID(pMeta->pVnode), code);
×
1332
      return code;
×
1333
    }
1334
  }
1335

1336
  if (TSDB_CODE_SUCCESS == code) {
144,496!
1337
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
144,641✔
1338
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
144,643✔
1339
    if (ret < 0) {
144,682!
1340
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1341
    }
1342

1343
    ret = metaTbGroupCacheClear(pMeta, pSuperEntry->uid);
144,682✔
1344
    if (ret < 0) {
144,666!
1345
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1346
    }
1347
  }
1348
  return code;
144,667✔
1349
}
1350

1351
static int32_t metaHandleChildTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
144,623✔
1352
  int32_t     code = TSDB_CODE_SUCCESS;
144,623✔
1353
  SMetaEntry *pSuperEntry = NULL;
144,623✔
1354

1355
  // get the super table entry
1356
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
144,623✔
1357
  if (code) {
144,664!
1358
    metaErr(TD_VID(pMeta->pVnode), code);
×
1359
    return code;
×
1360
  }
1361

1362
  // update TDB
1363
  metaWLock(pMeta);
144,664✔
1364
  code = metaHandleChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
144,672✔
1365
  metaULock(pMeta);
144,648✔
1366

1367
  // update other stuff
1368
  if (TSDB_CODE_SUCCESS == code) {
144,686!
1369
    pMeta->pVnode->config.vndStats.numOfCTables++;
144,686✔
1370

1371
    if (!metaTbInFilterCache(pMeta, pSuperEntry->name, 1)) {
144,686✔
1372
      int32_t nCols = 0;
144,662✔
1373
      int32_t ret = metaGetStbStats(pMeta->pVnode, pSuperEntry->uid, 0, &nCols, 0);
144,662✔
1374
      if (ret < 0) {
144,658!
1375
        metaErr(TD_VID(pMeta->pVnode), ret);
×
1376
      }
1377
      pMeta->pVnode->config.vndStats.numOfTimeSeries += (nCols > 0 ? nCols - 1 : 0);
144,673!
1378
    }
1379

1380
    if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
144,684!
1381
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, pEntry->ctbEntry.suid, NULL);
15,163✔
1382
      if (rc < 0) {
15,162!
1383
        metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
1384
                  tstrerror(rc));
1385
      }
1386
    }
1387

1388
  } else {
1389
    metaErr(TD_VID(pMeta->pVnode), code);
×
1390
  }
1391
  metaTimeSeriesNotifyCheck(pMeta);
144,683✔
1392
  metaFetchEntryFree(&pSuperEntry);
144,669✔
1393
  return code;
144,704✔
1394
}
1395

1396
static int32_t metaHandleVirtualNormalTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
4✔
1397
  int32_t code = TSDB_CODE_SUCCESS;
4✔
1398

1399
  SMetaTableOp ops[] = {
4✔
1400
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1401
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: need to be insert
1402
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1403
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1404
      {META_BTIME_IDX, META_TABLE_OP_INSERT},     //
1405
  };
1406

1407
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
24✔
1408
    SMetaTableOp *op = &ops[i];
20✔
1409

1410
    SMetaHandleParam param = {
20✔
1411
        .pEntry = pEntry,
1412
    };
1413

1414
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
20✔
1415
    if (TSDB_CODE_SUCCESS != code) {
20!
1416
      metaErr(TD_VID(pMeta->pVnode), code);
×
1417
      return code;
×
1418
    }
1419
  }
1420

1421
  return code;
4✔
1422
}
1423

1424
static int32_t metaHandleVirtualNormalTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
4✔
1425
  int32_t code = TSDB_CODE_SUCCESS;
4✔
1426

1427
  // update TDB
1428
  metaWLock(pMeta);
4✔
1429
  code = metaHandleVirtualNormalTableCreateImpl(pMeta, pEntry);
4✔
1430
  metaULock(pMeta);
4✔
1431

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

1441
static int32_t metaHandleVirtualChildTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry,
4✔
1442
                                                     const SMetaEntry *pSuperEntry) {
1443
  int32_t code = TSDB_CODE_SUCCESS;
4✔
1444

1445
  SMetaTableOp ops[] = {
4✔
1446
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},  //
1447
      {META_UID_IDX, META_TABLE_OP_INSERT},      //
1448
      {META_NAME_IDX, META_TABLE_OP_INSERT},     //
1449
      {META_CHILD_IDX, META_TABLE_OP_INSERT},    //
1450
      {META_TAG_IDX, META_TABLE_OP_INSERT},      //
1451
      {META_BTIME_IDX, META_TABLE_OP_INSERT},    //
1452
  };
1453

1454
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
28✔
1455
    SMetaTableOp *op = &ops[i];
24✔
1456

1457
    SMetaHandleParam param = {
24✔
1458
        .pEntry = pEntry,
1459
        .pSuperEntry = pSuperEntry,
1460
    };
1461

1462
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
24✔
1463
    if (TSDB_CODE_SUCCESS != code) {
24!
1464
      metaErr(TD_VID(pMeta->pVnode), code);
×
1465
      return code;
×
1466
    }
1467
  }
1468

1469
  if (TSDB_CODE_SUCCESS == code) {
4!
1470
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
4✔
1471
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
4✔
1472
    if (ret < 0) {
4!
1473
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1474
    }
1475

1476
    ret = metaTbGroupCacheClear(pMeta, pSuperEntry->uid);
4✔
1477
    if (ret < 0) {
4!
1478
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1479
    }
1480

1481
    ret = metaRefDbsCacheClear(pMeta, pSuperEntry->uid);
4✔
1482
    if (ret < 0) {
4!
1483
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1484
    }
1485
  }
1486

1487
  return code;
4✔
1488
}
1489

1490
static int32_t metaHandleVirtualChildTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
4✔
1491
  int32_t     code = TSDB_CODE_SUCCESS;
4✔
1492
  SMetaEntry *pSuperEntry = NULL;
4✔
1493

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

1501
  // update TDB
1502
  metaWLock(pMeta);
4✔
1503
  code = metaHandleVirtualChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
4✔
1504
  metaULock(pMeta);
4✔
1505

1506
  // update other stuff
1507
  if (TSDB_CODE_SUCCESS == code) {
4!
1508
    pMeta->pVnode->config.vndStats.numOfVCTables++;
4✔
1509
  } else {
1510
    metaErr(TD_VID(pMeta->pVnode), code);
×
1511
  }
1512

1513
  metaFetchEntryFree(&pSuperEntry);
4✔
1514
  return code;
4✔
1515
}
1516

1517
static int32_t metaHandleNormalTableDropImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
2,225✔
1518
  int32_t code = TSDB_CODE_SUCCESS;
2,225✔
1519

1520
  SMetaTableOp ops[] = {
2,225✔
1521
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1522
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1523
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1524
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1525
      {META_TTL_IDX, META_TABLE_OP_DELETE},      //
1526

1527
      // {META_SCHEMA_TABLE, META_TABLE_OP_DELETE},  //
1528
  };
1529

1530
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
13,350✔
1531
    SMetaTableOp *op = &ops[i];
11,125✔
1532
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
11,125✔
1533
    if (code) {
11,125!
1534
      const SMetaEntry *pEntry = pParam->pEntry;
×
1535
      metaErr(TD_VID(pMeta->pVnode), code);
×
1536
    }
1537
  }
1538

1539
  return code;
2,225✔
1540
}
1541

1542
static int32_t metaHandleNormalTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
2,225✔
1543
  int32_t     code = TSDB_CODE_SUCCESS;
2,225✔
1544
  SMetaEntry *pOldEntry = NULL;
2,225✔
1545

1546
  // fetch the entry
1547
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
2,225✔
1548
  if (code) {
2,225!
1549
    metaErr(TD_VID(pMeta->pVnode), code);
×
1550
    return code;
×
1551
  }
1552

1553
  SMetaHandleParam param = {
2,225✔
1554
      .pEntry = pEntry,
1555
      .pOldEntry = pOldEntry,
1556
  };
1557

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

1568
  // update other stuff
1569
  pMeta->pVnode->config.vndStats.numOfNTables--;
2,225✔
1570
  pMeta->pVnode->config.vndStats.numOfNTimeSeries -= (pOldEntry->ntbEntry.schemaRow.nCols - 1);
2,225✔
1571

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

1581
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
2,225!
1582
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pOldEntry->uid, 0, NULL);
×
1583
    if (ret < 0) {
×
1584
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1585
    }
1586
  }
1587

1588
  metaFetchEntryFree(&pOldEntry);
2,225✔
1589
  return code;
2,225✔
1590
}
1591

1592
static int32_t metaHandleChildTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam, bool superDropped) {
19,173✔
1593
  int32_t code = TSDB_CODE_SUCCESS;
19,173✔
1594

1595
  const SMetaEntry *pEntry = pParam->pEntry;
19,173✔
1596
  const SMetaEntry *pChild = pParam->pOldEntry;
19,173✔
1597
  const SMetaEntry *pSuper = pParam->pSuperEntry;
19,173✔
1598

1599
  SMetaTableOp ops[] = {
19,173✔
1600
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1601
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1602
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1603
      {META_CHILD_IDX, META_TABLE_OP_DELETE},    //
1604
      {META_TAG_IDX, META_TABLE_OP_DELETE},      //
1605
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1606
      {META_TTL_IDX, META_TABLE_OP_DELETE},      //
1607
  };
1608

1609
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
153,348✔
1610
    SMetaTableOp *op = &ops[i];
134,180✔
1611

1612
    if (op->table == META_ENTRY_TABLE && superDropped) {
134,180✔
1613
      continue;
5,873✔
1614
    }
1615

1616
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
128,307✔
1617
    if (code) {
128,313✔
1618
      metaErr(TD_VID(pMeta->pVnode), code);
11!
1619
      return code;
×
1620
    }
1621
  }
1622

1623
  --pMeta->pVnode->config.vndStats.numOfCTables;
19,168✔
1624
  metaUpdateStbStats(pMeta, pParam->pSuperEntry->uid, -1, 0, -1);
19,168✔
1625
  int32_t ret = metaUidCacheClear(pMeta, pSuper->uid);
19,173✔
1626
  if (ret < 0) {
19,175!
1627
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1628
  }
1629

1630
  ret = metaTbGroupCacheClear(pMeta, pSuper->uid);
19,175✔
1631
  if (ret < 0) {
19,173!
1632
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1633
  }
1634
  return code;
19,173✔
1635
}
1636

1637
static int32_t metaHandleChildTableDrop(SMeta *pMeta, const SMetaEntry *pEntry, bool superDropped) {
19,171✔
1638
  int32_t     code = TSDB_CODE_SUCCESS;
19,171✔
1639
  SMetaEntry *pChild = NULL;
19,171✔
1640
  SMetaEntry *pSuper = NULL;
19,171✔
1641

1642
  // fetch old entry
1643
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pChild);
19,171✔
1644
  if (code) {
19,171!
1645
    metaErr(TD_VID(pMeta->pVnode), code);
×
1646
    return code;
×
1647
  }
1648

1649
  // fetch super entry
1650
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
19,171✔
1651
  if (code) {
19,174!
1652
    metaErr(TD_VID(pMeta->pVnode), code);
×
1653
    metaFetchEntryFree(&pChild);
×
1654
    return code;
×
1655
  }
1656

1657
  SMetaHandleParam param = {
19,174✔
1658
      .pEntry = pEntry,
1659
      .pOldEntry = pChild,
1660
      .pSuperEntry = pSuper,
1661
  };
1662

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

1674
  // do other stuff
1675
  if (!metaTbInFilterCache(pMeta, pSuper->name, 1)) {
19,173!
1676
    int32_t      nCols = 0;
19,174✔
1677
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
19,174✔
1678
    if (metaGetStbStats(pMeta->pVnode, pSuper->uid, NULL, &nCols, 0) == 0) {
19,174!
1679
      pStats->numOfTimeSeries -= nCols - 1;
19,175✔
1680
    }
1681
  }
1682

1683
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
19,175!
1684
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pChild->uid, pSuper->uid, NULL);
2,776✔
1685
    if (ret < 0) {
2,776!
1686
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1687
    }
1688
  }
1689

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

1698
  if ((type == TSDB_CHILD_TABLE) && tbUid) {
1699
    *tbUid = uid;
1700
  }
1701
#endif
1702
  metaFetchEntryFree(&pChild);
19,175✔
1703
  metaFetchEntryFree(&pSuper);
19,174✔
1704
  return code;
19,173✔
1705
}
1706

1707
static int32_t metaHandleVirtualNormalTableDropImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
3✔
1708
  int32_t code = TSDB_CODE_SUCCESS;
3✔
1709

1710
  SMetaTableOp ops[] = {
3✔
1711
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1712
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1713
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1714
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1715

1716
      // {META_SCHEMA_TABLE, META_TABLE_OP_DELETE},  //
1717
  };
1718

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

1728
  return code;
3✔
1729
}
1730

1731
static int32_t metaHandleVirtualNormalTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
3✔
1732
  int32_t     code = TSDB_CODE_SUCCESS;
3✔
1733
  SMetaEntry *pOldEntry = NULL;
3✔
1734

1735
  // fetch the entry
1736
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
3✔
1737
  if (code) {
3!
1738
    metaErr(TD_VID(pMeta->pVnode), code);
×
1739
    return code;
×
1740
  }
1741

1742
  SMetaHandleParam param = {
3✔
1743
      .pEntry = pEntry,
1744
      .pOldEntry = pOldEntry,
1745
  };
1746

1747
  // do the drop
1748
  metaWLock(pMeta);
3✔
1749
  code = metaHandleVirtualNormalTableDropImpl(pMeta, &param);
3✔
1750
  metaULock(pMeta);
3✔
1751
  if (code) {
3!
1752
    metaErr(TD_VID(pMeta->pVnode), code);
×
1753
    metaFetchEntryFree(&pOldEntry);
×
1754
    return code;
×
1755
  }
1756

1757
  // update other stuff
1758
  pMeta->pVnode->config.vndStats.numOfVTables--;
3✔
1759

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

1769
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
3!
1770
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pOldEntry->uid, 0, NULL);
×
1771
    if (ret < 0) {
×
1772
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1773
    }
1774
  }
1775

1776
  metaFetchEntryFree(&pOldEntry);
3✔
1777
  return code;
3✔
1778
}
1779

1780
static int32_t metaHandleVirtualChildTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam, bool superDropped) {
3✔
1781
  int32_t code = TSDB_CODE_SUCCESS;
3✔
1782

1783
  const SMetaEntry *pEntry = pParam->pEntry;
3✔
1784
  const SMetaEntry *pChild = pParam->pOldEntry;
3✔
1785
  const SMetaEntry *pSuper = pParam->pSuperEntry;
3✔
1786

1787
  SMetaTableOp ops[] = {
3✔
1788
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1789
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1790
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1791
      {META_CHILD_IDX, META_TABLE_OP_DELETE},    //
1792
      {META_TAG_IDX, META_TABLE_OP_DELETE},      //
1793
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1794
  };
1795

1796
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
21✔
1797
    SMetaTableOp *op = &ops[i];
18✔
1798

1799
    if (op->table == META_ENTRY_TABLE && superDropped) {
18!
1800
      continue;
×
1801
    }
1802

1803
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
18✔
1804
    if (code) {
18!
1805
      metaErr(TD_VID(pMeta->pVnode), code);
×
1806
      return code;
×
1807
    }
1808
  }
1809

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

1817
  ret = metaTbGroupCacheClear(pMeta, pSuper->uid);
3✔
1818
  if (ret < 0) {
3!
1819
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1820
  }
1821

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

1827
  return code;
3✔
1828
}
1829

1830
static int32_t metaHandleVirtualChildTableDrop(SMeta *pMeta, const SMetaEntry *pEntry, bool superDropped) {
3✔
1831
  int32_t     code = TSDB_CODE_SUCCESS;
3✔
1832
  SMetaEntry *pChild = NULL;
3✔
1833
  SMetaEntry *pSuper = NULL;
3✔
1834

1835
  // fetch old entry
1836
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pChild);
3✔
1837
  if (code) {
3!
1838
    metaErr(TD_VID(pMeta->pVnode), code);
×
1839
    return code;
×
1840
  }
1841

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

1850
  SMetaHandleParam param = {
3✔
1851
      .pEntry = pEntry,
1852
      .pOldEntry = pChild,
1853
      .pSuperEntry = pSuper,
1854
  };
1855

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

1867
  metaFetchEntryFree(&pChild);
3✔
1868
  metaFetchEntryFree(&pSuper);
3✔
1869
  return code;
3✔
1870
}
1871

1872
static int32_t metaGetChildUidsOfSuperTable(SMeta *pMeta, tb_uid_t suid, SArray **childList) {
3,287✔
1873
  int32_t code = TSDB_CODE_SUCCESS;
3,287✔
1874
  void   *key = NULL;
3,287✔
1875
  int32_t keySize = 0;
3,287✔
1876
  int32_t c;
1877

1878
  *childList = taosArrayInit(64, sizeof(tb_uid_t));
3,287✔
1879
  if (*childList == NULL) {
3,301!
1880
    return terrno;
×
1881
  }
1882

1883
  TBC *cursor = NULL;
3,301✔
1884
  code = tdbTbcOpen(pMeta->pCtbIdx, &cursor, NULL);
3,301✔
1885
  if (code) {
3,305!
1886
    taosArrayDestroy(*childList);
×
1887
    *childList = NULL;
×
1888
    return code;
×
1889
  }
1890

1891
  int32_t rc = tdbTbcMoveTo(cursor,
3,305✔
1892
                            &(SCtbIdxKey){
3,305✔
1893
                                .suid = suid,
1894
                                .uid = INT64_MIN,
1895
                            },
1896
                            sizeof(SCtbIdxKey), &c);
1897
  if (rc < 0) {
3,301!
1898
    tdbTbcClose(cursor);
×
1899
    return 0;
×
1900
  }
1901

1902
  for (;;) {
1903
    if (tdbTbcNext(cursor, &key, &keySize, NULL, NULL) < 0) {
11,269✔
1904
      break;
2,411✔
1905
    }
1906

1907
    if (((SCtbIdxKey *)key)->suid < suid) {
8,852✔
1908
      continue;
706✔
1909
    } else if (((SCtbIdxKey *)key)->suid > suid) {
8,146✔
1910
      break;
893✔
1911
    }
1912

1913
    if (taosArrayPush(*childList, &(((SCtbIdxKey *)key)->uid)) == NULL) {
14,515!
1914
      tdbFreeClear(key);
×
1915
      tdbTbcClose(cursor);
×
1916
      taosArrayDestroy(*childList);
×
1917
      *childList = NULL;
×
1918
      return terrno;
×
1919
    }
1920
  }
1921

1922
  tdbTbcClose(cursor);
3,304✔
1923
  tdbFreeClear(key);
3,303✔
1924
  return code;
3,304✔
1925
}
1926

1927
static int32_t metaHandleSuperTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
2,881✔
1928
  int32_t           code = TSDB_CODE_SUCCESS;
2,881✔
1929
  const SMetaEntry *pEntry = pParam->pEntry;
2,881✔
1930

1931
  SMetaTableOp ops[] = {
2,881✔
1932
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1933
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1934
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1935
      {META_SUID_IDX, META_TABLE_OP_DELETE},     //
1936

1937
      // {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: here should be insert
1938
  };
1939

1940
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
14,402✔
1941
    SMetaTableOp *op = &ops[i];
11,523✔
1942

1943
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
11,523✔
1944
    if (TSDB_CODE_SUCCESS != code) {
11,521!
1945
      metaErr(TD_VID(pMeta->pVnode), code);
×
1946
      return code;
×
1947
    }
1948
  }
1949

1950
  int32_t ret = metaStatsCacheDrop(pMeta, pEntry->uid);
2,879✔
1951
  if (ret < 0) {
2,881✔
1952
    metaErr(TD_VID(pMeta->pVnode), ret);
1,003!
1953
  }
1954
  return code;
2,881✔
1955
}
1956

1957
static int32_t metaHandleNormalTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
1,344✔
1958
  int32_t code = TSDB_CODE_SUCCESS;
1,344✔
1959

1960
  const SMetaEntry *pEntry = pParam->pEntry;
1,344✔
1961

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

1984
static int32_t metaHandleVirtualNormalTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
6✔
1985
  int32_t code = TSDB_CODE_SUCCESS;
6✔
1986

1987
  const SMetaEntry *pEntry = pParam->pEntry;
6✔
1988

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

2010
static int32_t metaHandleVirtualChildTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
2011
  int32_t code = TSDB_CODE_SUCCESS;
×
2012

2013
  const SMetaEntry *pEntry = pParam->pEntry;
×
2014
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
×
2015
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
×
2016

2017
  SMetaTableOp ops[] = {
×
2018
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},  //
2019
      {META_UID_IDX, META_TABLE_OP_UPDATA},      //
2020
      {META_TAG_IDX, META_TABLE_OP_UPDATA},      //
2021
      {META_CHILD_IDX, META_TABLE_OP_UPDATA},    //
2022
  };
2023

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

2033
  if (metaUidCacheClear(pMeta, pSuperEntry->uid) < 0) {
×
2034
    metaErr(TD_VID(pMeta->pVnode), code);
×
2035
  }
2036

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

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

2047
static int32_t metaHandleChildTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
3,222✔
2048
  int32_t code = TSDB_CODE_SUCCESS;
3,222✔
2049

2050
  const SMetaEntry *pEntry = pParam->pEntry;
3,222✔
2051
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
3,222✔
2052
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
3,222✔
2053

2054
  SMetaTableOp ops[] = {
3,222✔
2055
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},  //
2056
      {META_UID_IDX, META_TABLE_OP_UPDATA},      //
2057
      {META_TAG_IDX, META_TABLE_OP_UPDATA},      //
2058
      {META_CHILD_IDX, META_TABLE_OP_UPDATA},    //
2059
      {META_TTL_IDX, META_TABLE_OP_UPDATA},      //
2060
  };
2061

2062
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
19,332✔
2063
    SMetaTableOp *op = &ops[i];
16,110✔
2064
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
16,110✔
2065
    if (code) {
16,110!
2066
      metaErr(TD_VID(pMeta->pVnode), code);
×
2067
      return code;
×
2068
    }
2069
  }
2070

2071
  if (metaUidCacheClear(pMeta, pSuperEntry->uid) < 0) {
3,222!
2072
    metaErr(TD_VID(pMeta->pVnode), code);
×
2073
  }
2074

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

2086
static int32_t metaHandleSuperTableUpdateImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
10,998✔
2087
  int32_t code = TSDB_CODE_SUCCESS;
10,998✔
2088

2089
  const SMetaEntry *pEntry = pParam->pEntry;
10,998✔
2090
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
10,998✔
2091

2092
  SMetaTableOp ops[] = {
10,998✔
2093
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
2094
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
2095
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
2096
  };
2097

2098
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
44,002✔
2099
    SMetaTableOp *op = &ops[i];
33,012✔
2100
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
33,012✔
2101
    if (code) {
32,994!
2102
      metaErr(TD_VID(pMeta->pVnode), code);
×
2103
      return code;
×
2104
    }
2105
  }
2106

2107
  if (TSDB_CODE_SUCCESS == code) {
10,990✔
2108
    metaUpdateStbStats(pMeta, pEntry->uid, 0, pEntry->stbEntry.schemaRow.nCols - pOldEntry->stbEntry.schemaRow.nCols,
10,986✔
2109
                       pEntry->stbEntry.keep);
10,986✔
2110
  }
2111

2112
  return code;
11,019✔
2113
}
2114

2115
static int32_t metaHandleSuperTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
10,995✔
2116
  int32_t code = TSDB_CODE_SUCCESS;
10,995✔
2117

2118
  SMetaEntry *pOldEntry = NULL;
10,995✔
2119

2120
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
10,995✔
2121
  if (code) {
11,019!
2122
    metaErr(TD_VID(pMeta->pVnode), code);
×
2123
    return code;
×
2124
  }
2125

2126
  SMetaHandleParam param = {
11,019✔
2127
      .pEntry = pEntry,
2128
      .pOldEntry = pOldEntry,
2129
  };
2130
  metaWLock(pMeta);
11,019✔
2131
  code = metaHandleSuperTableUpdateImpl(pMeta, &param);
11,008✔
2132
  metaULock(pMeta);
10,992✔
2133
  if (code) {
10,994!
2134
    metaErr(TD_VID(pMeta->pVnode), code);
×
2135
    metaFetchEntryFree(&pOldEntry);
×
2136
    return code;
×
2137
  }
2138

2139
  int     nCols = pEntry->stbEntry.schemaRow.nCols;
10,994✔
2140
  int     onCols = pOldEntry->stbEntry.schemaRow.nCols;
10,994✔
2141
  int32_t deltaCol = nCols - onCols;
10,994✔
2142
  bool    updStat = deltaCol != 0 && !TABLE_IS_VIRTUAL(pEntry->flags) && !metaTbInFilterCache(pMeta, pEntry->name, 1);
10,994!
2143

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

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

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

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

2210
static int32_t metaHandleChildTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
3,222✔
2211
  int32_t code = TSDB_CODE_SUCCESS;
3,222✔
2212

2213
  SMetaEntry *pOldEntry = NULL;
3,222✔
2214
  SMetaEntry *pSuperEntry = NULL;
3,222✔
2215

2216
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
3,222✔
2217
  if (code) {
3,222!
2218
    metaErr(TD_VID(pMeta->pVnode), code);
×
2219
    return code;
×
2220
  }
2221

2222
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
3,222✔
2223
  if (code) {
3,222!
2224
    metaErr(TD_VID(pMeta->pVnode), code);
×
2225
    metaFetchEntryFree(&pOldEntry);
×
2226
    return code;
×
2227
  }
2228

2229
  SMetaHandleParam param = {
3,222✔
2230
      .pEntry = pEntry,
2231
      .pOldEntry = pOldEntry,
2232
      .pSuperEntry = pSuperEntry,
2233
  };
2234

2235
  metaWLock(pMeta);
3,222✔
2236
  code = metaHandleChildTableUpdateImpl(pMeta, &param);
3,222✔
2237
  metaULock(pMeta);
3,222✔
2238
  if (code) {
3,222!
2239
    metaErr(TD_VID(pMeta->pVnode), code);
×
2240
    metaFetchEntryFree(&pOldEntry);
×
2241
    metaFetchEntryFree(&pSuperEntry);
×
2242
    return code;
×
2243
  }
2244

2245
  metaFetchEntryFree(&pOldEntry);
3,222✔
2246
  metaFetchEntryFree(&pSuperEntry);
3,222✔
2247
  return code;
3,222✔
2248
}
2249

2250
static int32_t metaHandleNormalTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
1,344✔
2251
  int32_t     code = TSDB_CODE_SUCCESS;
1,344✔
2252
  SMetaEntry *pOldEntry = NULL;
1,344✔
2253

2254
  // fetch old entry
2255
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
1,344✔
2256
  if (code) {
1,344!
2257
    metaErr(TD_VID(pMeta->pVnode), code);
×
2258
    return code;
×
2259
  }
2260

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

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

2290
      if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
2291
        int16_t cid = pColumn->colId;
2292

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

2313
static int32_t metaHandleVirtualNormalTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
6✔
2314
  int32_t     code = TSDB_CODE_SUCCESS;
6✔
2315
  SMetaEntry *pOldEntry = NULL;
6✔
2316

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

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

2338
  metaTimeSeriesNotifyCheck(pMeta);
6✔
2339
  metaFetchEntryFree(&pOldEntry);
6✔
2340
  return code;
6✔
2341
}
2342

2343
static int32_t metaHandleVirtualChildTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
×
2344
  int32_t code = TSDB_CODE_SUCCESS;
×
2345

2346
  SMetaEntry *pOldEntry = NULL;
×
2347
  SMetaEntry *pSuperEntry = NULL;
×
2348

2349
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
×
2350
  if (code) {
×
2351
    metaErr(TD_VID(pMeta->pVnode), code);
×
2352
    return code;
×
2353
  }
2354

2355
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
×
2356
  if (code) {
×
2357
    metaErr(TD_VID(pMeta->pVnode), code);
×
2358
    metaFetchEntryFree(&pOldEntry);
×
2359
    return code;
×
2360
  }
2361

2362
  SMetaHandleParam param = {
×
2363
      .pEntry = pEntry,
2364
      .pOldEntry = pOldEntry,
2365
      .pSuperEntry = pSuperEntry,
2366
  };
2367

2368
  metaWLock(pMeta);
×
2369
  code = metaHandleVirtualChildTableUpdateImpl(pMeta, &param);
×
2370
  metaULock(pMeta);
×
2371
  if (code) {
×
2372
    metaErr(TD_VID(pMeta->pVnode), code);
×
2373
    metaFetchEntryFree(&pOldEntry);
×
2374
    metaFetchEntryFree(&pSuperEntry);
×
2375
    return code;
×
2376
  }
2377

2378
  metaFetchEntryFree(&pOldEntry);
×
2379
  metaFetchEntryFree(&pSuperEntry);
×
2380
  return code;
×
2381
}
2382

2383
static int32_t metaHandleSuperTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
2,863✔
2384
  int32_t     code = TSDB_CODE_SUCCESS;
2,863✔
2385
  SArray     *childList = NULL;
2,863✔
2386
  SMetaEntry *pOldEntry = NULL;
2,863✔
2387

2388
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
2,863✔
2389
  if (code) {
2,874!
2390
    metaErr(TD_VID(pMeta->pVnode), code);
×
2391
    return code;
×
2392
  }
2393

2394
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childList);
2,874✔
2395
  if (code) {
2,876!
2396
    metaErr(TD_VID(pMeta->pVnode), code);
×
2397
    metaFetchEntryFree(&pOldEntry);
×
2398
    return code;
×
2399
  }
2400

2401
  if (pMeta->pVnode->pTsdb && tsdbCacheDropSubTables(pMeta->pVnode->pTsdb, childList, pEntry->uid) < 0) {
2,876!
2402
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
6!
2403
              pEntry->uid, tstrerror(terrno));
2404
  }
2405

2406
  // loop to drop all child tables
2407
  for (int32_t i = 0; i < taosArrayGetSize(childList); i++) {
8,755✔
2408
    SMetaEntry childEntry = {
11,746✔
2409
        .version = pEntry->version,
5,874✔
2410
        .uid = *(tb_uid_t *)taosArrayGet(childList, i),
5,874✔
2411
        .type = -TSDB_CHILD_TABLE,
2412
    };
2413

2414
    code = metaHandleChildTableDrop(pMeta, &childEntry, true);
5,872✔
2415
    if (code) {
5,874!
2416
      metaErr(TD_VID(pMeta->pVnode), code);
×
2417
    }
2418
  }
2419

2420
  // do drop super table
2421
  SMetaHandleParam param = {
2,879✔
2422
      .pEntry = pEntry,
2423
      .pOldEntry = pOldEntry,
2424
  };
2425
  metaWLock(pMeta);
2,879✔
2426
  code = metaHandleSuperTableDropImpl(pMeta, &param);
2,881✔
2427
  metaULock(pMeta);
2,881✔
2428
  if (code) {
2,881!
2429
    metaErr(TD_VID(pMeta->pVnode), code);
×
2430
    taosArrayDestroy(childList);
×
2431
    metaFetchEntryFree(&pOldEntry);
×
2432
    return code;
×
2433
  }
2434

2435
  // do other stuff
2436
  // metaUpdTimeSeriesNum(pMeta);
2437

2438
  // free resource and return
2439
  taosArrayDestroy(childList);
2,881✔
2440
  metaFetchEntryFree(&pOldEntry);
2,881✔
2441
  return code;
2,881✔
2442
}
2443

2444
int32_t metaHandleEntry2(SMeta *pMeta, const SMetaEntry *pEntry) {
222,590✔
2445
  int32_t   code = TSDB_CODE_SUCCESS;
222,590✔
2446
  int32_t   vgId = TD_VID(pMeta->pVnode);
222,590✔
2447
  SMetaInfo info = {0};
222,590✔
2448
  int8_t    type = pEntry->type > 0 ? pEntry->type : -pEntry->type;
222,590✔
2449

2450
  if (NULL == pMeta || NULL == pEntry) {
222,590!
2451
    metaError("%s failed at %s:%d since invalid parameter", __func__, __FILE__, __LINE__);
×
2452
    return TSDB_CODE_INVALID_PARA;
×
2453
  }
2454

2455
  if (pEntry->type > 0) {
223,129✔
2456
    bool isExist = false;
204,730✔
2457
    if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) {
204,730✔
2458
      isExist = true;
15,591✔
2459
    }
2460

2461
    switch (type) {
204,760!
2462
      case TSDB_SUPER_TABLE: {
40,519✔
2463
        if (isExist) {
40,519✔
2464
          code = metaHandleSuperTableUpdate(pMeta, pEntry);
11,020✔
2465
        } else {
2466
          code = metaHandleSuperTableCreate(pMeta, pEntry);
29,499✔
2467
        }
2468
        break;
40,514✔
2469
      }
2470
      case TSDB_CHILD_TABLE: {
147,883✔
2471
        if (isExist) {
147,883✔
2472
          code = metaHandleChildTableUpdate(pMeta, pEntry);
3,222✔
2473
        } else {
2474
          code = metaHandleChildTableCreate(pMeta, pEntry);
144,661✔
2475
        }
2476
        break;
147,912✔
2477
      }
2478
      case TSDB_NORMAL_TABLE: {
16,352✔
2479
        if (isExist) {
16,352✔
2480
          code = metaHandleNormalTableUpdate(pMeta, pEntry);
1,344✔
2481
        } else {
2482
          code = metaHandleNormalTableCreate(pMeta, pEntry);
15,008✔
2483
        }
2484
        break;
16,352✔
2485
      }
2486
      case TSDB_VIRTUAL_NORMAL_TABLE: {
10✔
2487
        if (isExist) {
10✔
2488
          code = metaHandleVirtualNormalTableUpdate(pMeta, pEntry);
6✔
2489
        } else {
2490
          code = metaHandleVirtualNormalTableCreate(pMeta, pEntry);
4✔
2491
        }
2492
        break;
10✔
2493
      }
2494
      case TSDB_VIRTUAL_CHILD_TABLE: {
4✔
2495
        if (isExist) {
4!
2496
          code = metaHandleVirtualChildTableUpdate(pMeta, pEntry);
×
2497
        } else {
2498
          code = metaHandleVirtualChildTableCreate(pMeta, pEntry);
4✔
2499
        }
2500
        break;
4✔
2501
      }
2502
      default: {
×
2503
        code = TSDB_CODE_INVALID_PARA;
×
2504
        break;
×
2505
      }
2506
    }
2507
  } else {
2508
    switch (type) {
18,399✔
2509
      case TSDB_SUPER_TABLE: {
2,863✔
2510
        code = metaHandleSuperTableDrop(pMeta, pEntry);
2,863✔
2511
        break;
2,881✔
2512
      }
2513
      case TSDB_CHILD_TABLE: {
13,299✔
2514
        code = metaHandleChildTableDrop(pMeta, pEntry, false);
13,299✔
2515
        break;
13,299✔
2516
      }
2517
      case TSDB_NORMAL_TABLE: {
2,225✔
2518
        code = metaHandleNormalTableDrop(pMeta, pEntry);
2,225✔
2519
        break;
2,225✔
2520
      }
2521
      case TSDB_VIRTUAL_NORMAL_TABLE: {
3✔
2522
        code = metaHandleVirtualNormalTableDrop(pMeta, pEntry);
3✔
2523
        break;
3✔
2524
      }
2525
      case TSDB_VIRTUAL_CHILD_TABLE: {
3✔
2526
        code = metaHandleVirtualChildTableDrop(pMeta, pEntry, false);
3✔
2527
        break;
3✔
2528
      }
2529
      default: {
6✔
2530
        code = TSDB_CODE_INVALID_PARA;
6✔
2531
        break;
6✔
2532
      }
2533
    }
2534
  }
2535

2536
  if (TSDB_CODE_SUCCESS == code) {
223,201✔
2537
    pMeta->changed = true;
223,191✔
2538
    metaDebug("vgId:%d, index:%" PRId64 ", handle meta entry success, type:%d tb:%s uid:%" PRId64, vgId,
223,191✔
2539
              pEntry->version, pEntry->type, pEntry->type > 0 ? pEntry->name : "", pEntry->uid);
2540
  } else {
2541
    metaErr(vgId, code);
10!
2542
  }
2543
  TAOS_RETURN(code);
223,202✔
2544
}
2545

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