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

taosdata / TDengine / #4065

13 May 2025 05:36AM UTC coverage: 59.223% (+4.8%) from 54.389%
#4065

push

travis-ci

happyguoxy
Sync branches at 2025-05-13 13:35

144240 of 312886 branches covered (46.1%)

Branch coverage included in aggregate %.

225142 of 310823 relevant lines covered (72.43%)

5484141.37 hits per line

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

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

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

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

96
  tDecoderInit(&decoder, value, valueSize);
265,721✔
97
  code = metaDecodeEntry(&decoder, &entry);
265,660✔
98
  if (code) {
265,494!
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);
265,494✔
107
  if (code) {
265,677!
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);
265,677✔
116
  tDecoderClear(&decoder);
265,674✔
117
  return code;
265,720✔
118
}
119

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

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

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

141
void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); }
265,682✔
142

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

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

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

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

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

180
  // put to tdb
181
  if (META_TABLE_OP_INSERT == op) {
165,594✔
182
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
157,611✔
183
  } else if (META_TABLE_OP_UPDATA == op) {
7,983✔
184
    code = tdbTbUpsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
4,514✔
185
  } else if (META_TABLE_OP_DELETE == op) {
3,469!
186
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
3,469✔
187
  } else {
188
    code = TSDB_CODE_INVALID_PARA;
×
189
  }
190
  if (TSDB_CODE_SUCCESS != code) {
165,676!
191
    metaErr(vgId, code);
×
192
  }
193
  taosMemoryFree(value);
165,616!
194
  return code;
165,654✔
195
}
196

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

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

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

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

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

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

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

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

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

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

281
  if (pOldColumn && pNewColumn) {
8,107✔
282
    if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
7,111✔
283
      return TSDB_CODE_SUCCESS;
1,288✔
284
    } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) {
5,823!
285
      action = DROP_INDEX;
38✔
286
    } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
5,785!
287
      action = ADD_INDEX;
17✔
288
    } else {
289
      return TSDB_CODE_SUCCESS;
5,768✔
290
    }
291
  } else if (pOldColumn) {
996✔
292
    if (IS_IDX_ON(pOldColumn)) {
358✔
293
      action = DROP_INDEX;
14✔
294
    } else {
295
      return TSDB_CODE_SUCCESS;
344✔
296
    }
297
  } else {
298
    if (IS_IDX_ON(pNewColumn)) {
638!
299
      action = ADD_INDEX;
×
300
    } else {
301
      return TSDB_CODE_SUCCESS;
638✔
302
    }
303
  }
304

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

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

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

329
    if (action == ADD_INDEX) {
229✔
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);
118✔
348
      if (code) {
118!
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);
118✔
356
      if (code) {
118!
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);
229✔
366
    metaFetchEntryFree(&pChildEntry);
229✔
367
  }
368

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

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

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

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

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

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

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

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

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

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

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

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

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

459
  int32_t iOld = 0, iNew = 0;
1,371✔
460
  for (; iOld < pOldTagSchema->nCols && iNew < pNewTagSchema->nCols;) {
8,677✔
461
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
7,307✔
462
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
7,307✔
463

464
    if (pOldColumn->colId == pNewColumn->colId) {
7,307✔
465
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
7,107✔
466
      if (code) {
7,110✔
467
        metaErr(TD_VID(pMeta->pVnode), code);
4!
468
        return code;
×
469
      }
470

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

480
      iOld++;
200✔
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++) {
1,527✔
493
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
157✔
494
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
157✔
495
    if (code) {
157!
496
      metaErr(TD_VID(pMeta->pVnode), code);
×
497
      return code;
×
498
    }
499
  }
500

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

510
  return code;
1,372✔
511
}
512

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

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

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

532
      iOld++;
×
533
      iNew++;
×
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++) {
×
554
    SSchema *pOldColumn = pOldRowSchema->pSchema + iOld;
×
555
    code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, pOldColumn, NULL);
×
556
    if (code) {
×
557
      metaErr(TD_VID(pMeta->pVnode), code);
×
558
      return code;
×
559
    }
560
  }
561

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

571
  return code;
×
572
}
573

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

577
  const SMetaEntry *pEntry = pParam->pEntry;
40,599✔
578
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
40,599✔
579

580
  if (NULL == pOldEntry) {
40,599✔
581
    return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
36,902✔
582
  }
583

584
  if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,697✔
585
    // check row schema
586
    if (pOldEntry->ntbEntry.schemaRow.version != pEntry->ntbEntry.schemaRow.version) {
346!
587
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
391✔
588
    }
589
  } else if (pEntry->type == TSDB_SUPER_TABLE) {
3,351!
590
    // check row schema
591
    if (pOldEntry->stbEntry.schemaRow.version != pEntry->stbEntry.schemaRow.version) {
3,353✔
592
      if (TABLE_IS_VIRTUAL(pEntry->flags)) {
1,976!
593
        return metaUpdateSuperTableRowSchema(pMeta, pParam);
×
594
      } else {
595
        return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
1,976✔
596
      }
597
    }
598

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

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

610
  return TSDB_CODE_SUCCESS;
1,419✔
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) {
162,069✔
620
  pInfo->uid = pEntry->uid;
162,069✔
621
  pInfo->version = pEntry->version;
162,069✔
622
  if (pEntry->type == TSDB_SUPER_TABLE) {
162,069✔
623
    pInfo->suid = pEntry->uid;
26,518✔
624
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
26,518✔
625
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
135,551✔
626
    pInfo->suid = pEntry->ctbEntry.suid;
121,374✔
627
    pInfo->skmVer = 0;
121,374✔
628
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
14,177!
629
    pInfo->suid = 0;
14,177✔
630
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
14,177✔
631
  }
632
}
162,069✔
633

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

638
  const SMetaEntry *pEntry = pParam->pEntry;
162,052✔
639

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

648
  // put to tdb
649
  SUidIdxVal value = {
162,027✔
650
      .suid = info.suid,
162,027✔
651
      .skmVer = info.skmVer,
162,027✔
652
      .version = pEntry->version,
162,027✔
653
  };
654
  if (META_TABLE_OP_INSERT == op) {
162,027✔
655
    code = tdbTbInsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
157,513✔
656
  } else if (META_TABLE_OP_UPDATA == op) {
4,514!
657
    code = tdbTbUpsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
4,520✔
658
  }
659
  return code;
162,302✔
660
}
661

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

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

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

673
  const SMetaEntry *pEntry = pParam->pOldEntry;
8,343✔
674

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

681
  // delete cache
682
  (void)metaCacheDrop(pMeta, pEntry->uid);
8,347✔
683
  return code;
8,345✔
684
}
685

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

690
  const SMetaEntry *pEntry = pParam->pEntry;
157,592✔
691

692
  if (META_TABLE_OP_INSERT == op) {
157,592!
693
    code = tdbTbInsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
157,612✔
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;
157,723✔
702
}
703

704
static int32_t metaNameIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
157,596✔
705
  int32_t code = TSDB_CODE_SUCCESS;
157,596✔
706
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
157,596✔
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) {
8,345✔
714
  int32_t code = TSDB_CODE_SUCCESS;
8,345✔
715

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

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

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

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

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

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

749
  const SMetaEntry *pEntry = pParam->pEntry;
121,267✔
750

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

756
  if (META_TABLE_OP_INSERT == op) {
121,267✔
757
    code = tdbTbInsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
120,756✔
758
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
120,756✔
759
  } else if (META_TABLE_OP_UPDATA == op) {
511!
760
    code = tdbTbUpsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
520✔
761
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
520✔
762
  } else {
763
    code = TSDB_CODE_INVALID_PARA;
×
764
  }
765
  return code;
121,295✔
766
}
767

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

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

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

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

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

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

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

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

809
  if (tTagGet((const STag *)pEntry->ctbEntry.pTags, &tagVal)) {
127,489✔
810
    if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
127,139!
811
      pTagData = tagVal.pData;
25,670✔
812
      nTagData = (int32_t)tagVal.nData;
25,670✔
813
    } else {
814
      pTagData = &(tagVal.i64);
101,469✔
815
      nTagData = tDataTypes[pTagColumn->type].bytes;
101,469✔
816
    }
817
  } else {
818
    if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
376!
819
      nTagData = tDataTypes[pTagColumn->type].bytes;
230✔
820
    }
821
  }
822

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

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

835
static void metaFetchTagIdxKeyFree(STagIdxKey **ppTagIdxKey) {
127,509✔
836
  metaDestroyTagIdxKey(*ppTagIdxKey);
127,509✔
837
  *ppTagIdxKey = NULL;
127,501✔
838
}
127,501✔
839

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

843
  const SMetaEntry *pEntry = pParam->pEntry;
120,750✔
844
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
120,750✔
845

846
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
120,750✔
847
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
120,962✔
848
    const SSchema *pTagColumn = &pTagSchema->pSchema[0];
212✔
849

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

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

866
      if (!IS_IDX_ON(pTagColumn)) {
405,274✔
867
        continue;
284,714✔
868
      }
869

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

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

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

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

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

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

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

918
      if (!IS_IDX_ON(pTagColumn)) {
2,078✔
919
        continue;
1,568✔
920
      }
921

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

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

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

940
      if (tagIdxKeyCmpr(pOldTagIdxKey, oldTagIdxKeySize, pNewTagIdxKey, newTagIdxKeySize)) {
510✔
941
        code = tdbTbDelete(pMeta->pTagIdx, pOldTagIdxKey, oldTagIdxKeySize, pMeta->txn);
103✔
942
        if (code) {
103!
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);
103✔
950
        if (code) {
103!
951
          metaErr(TD_VID(pMeta->pVnode), code);
×
952
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
953
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
954
          return code;
×
955
        }
956
      }
957

958
      metaFetchTagIdxKeyFree(&pOldTagIdxKey);
510✔
959
      metaFetchTagIdxKeyFree(&pNewTagIdxKey);
510✔
960
    }
961
  }
962
  return code;
520✔
963
}
964

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

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

975
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
5,656✔
976
    pTagColumn = &pTagSchema->pSchema[0];
6✔
977
    code = metaDelJsonVarFromIdx(pMeta, pChild, pTagColumn);
6✔
978
    if (code) {
6!
979
      metaErr(TD_VID(pMeta->pVnode), code);
×
980
    }
981
  } else {
982
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
16,966✔
983
      pTagColumn = &pTagSchema->pSchema[i];
11,315✔
984
      if (!IS_IDX_ON(pTagColumn)) {
11,315✔
985
        continue;
5,624✔
986
      }
987

988
      STagIdxKey *pTagIdxKey = NULL;
5,691✔
989
      int32_t     nTagIdxKey;
990

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

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

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

1013
  const SMetaEntry *pEntry;
1014
  if (META_TABLE_OP_DELETE == op) {
141,880✔
1015
    pEntry = pParam->pOldEntry;
7,420✔
1016
  } else {
1017
    pEntry = pParam->pEntry;
134,460✔
1018
  }
1019

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

1024
  if (TSDB_CHILD_TABLE == pEntry->type || TSDB_VIRTUAL_CHILD_TABLE == pEntry->type) {
141,880✔
1025
    key.btime = pEntry->ctbEntry.btime;
126,376✔
1026
  } else if (TSDB_NORMAL_TABLE == pEntry->type || TSDB_VIRTUAL_NORMAL_TABLE == pEntry->type) {
15,504!
1027
    key.btime = pEntry->ntbEntry.btime;
15,504✔
1028
  } else {
1029
    return TSDB_CODE_INVALID_PARA;
×
1030
  }
1031

1032
  if (META_TABLE_OP_INSERT == op) {
141,880✔
1033
    code = tdbTbInsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
134,473✔
1034
  } else if (META_TABLE_OP_UPDATA == op) {
7,407!
1035
    code = tdbTbUpsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
×
1036
  } else if (META_TABLE_OP_DELETE == op) {
7,407!
1037
    code = tdbTbDelete(pMeta->pBtimeIdx, &key, sizeof(key), pMeta->txn);
7,420✔
1038
  } else {
1039
    code = TSDB_CODE_INVALID_PARA;
×
1040
  }
1041
  if (code) {
141,910!
1042
    metaErr(TD_VID(pMeta->pVnode), code);
×
1043
  }
1044
  return code;
141,917✔
1045
}
1046

1047
static int32_t metaBtimeIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
134,461✔
1048
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
134,461✔
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) {
7,420✔
1056
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_DELETE);
7,420✔
1057
}
1058

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

1063
  STtlUpdTtlCtx ctx = {
134,490✔
1064
      .uid = pEntry->uid,
134,490✔
1065
      .pTxn = pMeta->txn,
134,490✔
1066
  };
1067
  if (TSDB_CHILD_TABLE == pEntry->type) {
134,490✔
1068
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
120,761✔
1069
    ctx.changeTimeMs = pEntry->ctbEntry.btime;
120,761✔
1070
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
13,729!
1071
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
13,746✔
1072
    ctx.changeTimeMs = pEntry->ntbEntry.btime;
13,746✔
1073
  } else {
1074
    return TSDB_CODE_INVALID_PARA;
×
1075
  }
1076

1077
  int32_t ret = ttlMgrInsertTtl(pMeta->pTtlMgr, &ctx);
134,507✔
1078
  if (ret < 0) {
134,490!
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;
134,490✔
1082
}
1083

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

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

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

1093
  const SMetaEntry *pEntry = pParam->pEntry;
1,167✔
1094
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
1,167✔
1095

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

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

1109
  return TSDB_CODE_SUCCESS;
1,167✔
1110
}
1111

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

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

1121
  if (TSDB_CHILD_TABLE == pEntry->type) {
7,435✔
1122
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
5,663✔
1123
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
1,772!
1124
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
1,772✔
1125
  } else {
1126
    code = TSDB_CODE_INVALID_PARA;
×
1127
  }
1128

1129
  if (TSDB_CODE_SUCCESS == code) {
7,435!
1130
    int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
7,435✔
1131
    if (ret < 0) {
7,435!
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;
7,435✔
1137
}
1138

1139
static void metaTimeSeriesNotifyCheck(SMeta *pMeta) {
135,735✔
1140
#if defined(TD_ENTERPRISE)
1141
  int64_t nTimeSeries = metaGetTimeSeriesNum(pMeta, 0);
135,735✔
1142
  int64_t deltaTS = nTimeSeries - pMeta->pVnode->config.vndStats.numOfReportedTimeSeries;
135,760✔
1143
  if (deltaTS > tsTimeSeriesThreshold) {
135,760✔
1144
    if (0 == atomic_val_compare_exchange_8(&dmNotifyHdl.state, 1, 2)) {
91,272✔
1145
      if (tsem_post(&dmNotifyHdl.sem) != 0) {
91,264!
1146
        metaError("vgId:%d, failed to post semaphore, errno:%d", TD_VID(pMeta->pVnode), ERRNO);
×
1147
      }
1148
    }
1149
  }
1150
#endif
1151
}
135,764✔
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) {
23,208✔
1212
  int32_t code = TSDB_CODE_SUCCESS;
23,208✔
1213

1214
  SMetaTableOp ops[] = {
23,208✔
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++) {
138,952✔
1223
    SMetaTableOp          *op = &ops[i];
115,819✔
1224
    const SMetaHandleParam param = {
115,819✔
1225
        .pEntry = pEntry,
1226
    };
1227
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
115,819✔
1228
    if (TSDB_CODE_SUCCESS != code) {
115,734!
1229
      metaErr(TD_VID(pMeta->pVnode), code);
×
1230
      return code;
×
1231
    }
1232
  }
1233

1234
  return code;
23,133✔
1235
}
1236
static int32_t metaHandleSuperTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
22,744✔
1237
  int32_t code = TSDB_CODE_SUCCESS;
22,744✔
1238

1239
  metaWLock(pMeta);
22,744✔
1240
  code = metaHandleSuperTableCreateImpl(pMeta, pEntry);
23,269✔
1241
  metaULock(pMeta);
23,152✔
1242

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

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

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

1257
  SMetaTableOp ops[] = {
13,740✔
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++) {
96,145✔
1267
    SMetaTableOp *op = &ops[i];
82,413✔
1268

1269
    SMetaHandleParam param = {
82,413✔
1270
        .pEntry = pEntry,
1271
    };
1272

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

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

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

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

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

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

1311
  SMetaTableOp ops[] = {
120,734✔
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++) {
965,297✔
1322
    SMetaTableOp *op = &ops[i];
844,729✔
1323

1324
    SMetaHandleParam param = {
844,729✔
1325
        .pEntry = pEntry,
1326
        .pSuperEntry = pSuperEntry,
1327
    };
1328

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

1336
  if (TSDB_CODE_SUCCESS == code) {
120,568!
1337
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
120,748✔
1338
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
120,745✔
1339
    if (ret < 0) {
120,774!
1340
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1341
    }
1342

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

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

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

1362
  // update TDB
1363
  metaWLock(pMeta);
120,764✔
1364
  code = metaHandleChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
120,763✔
1365
  metaULock(pMeta);
120,751✔
1366

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

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

1380
    if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
120,763!
1381
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, pEntry->ctbEntry.suid, NULL);
15,699✔
1382
      if (rc < 0) {
15,700!
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);
120,764✔
1392
  metaFetchEntryFree(&pSuperEntry);
120,760✔
1393
  return code;
120,780✔
1394
}
1395

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

1399
  SMetaTableOp ops[] = {
1✔
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++) {
6✔
1408
    SMetaTableOp *op = &ops[i];
5✔
1409

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

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

1421
  return code;
1✔
1422
}
1423

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

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

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

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

1445
  SMetaTableOp ops[] = {
1✔
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++) {
7✔
1455
    SMetaTableOp *op = &ops[i];
6✔
1456

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

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

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

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

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

1487
  return code;
1✔
1488
}
1489

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

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

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

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

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

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

1520
  SMetaTableOp ops[] = {
1,764✔
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++) {
10,584✔
1531
    SMetaTableOp *op = &ops[i];
8,820✔
1532
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
8,820✔
1533
    if (code) {
8,820!
1534
      const SMetaEntry *pEntry = pParam->pEntry;
×
1535
      metaErr(TD_VID(pMeta->pVnode), code);
×
1536
    }
1537
  }
1538

1539
  return code;
1,764✔
1540
}
1541

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

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

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

1558
  // do the drop
1559
  metaWLock(pMeta);
1,764✔
1560
  code = metaHandleNormalTableDropImpl(pMeta, &param);
1,764✔
1561
  metaULock(pMeta);
1,764✔
1562
  if (code) {
1,764!
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--;
1,764✔
1570
  pMeta->pVnode->config.vndStats.numOfNTimeSeries -= (pOldEntry->ntbEntry.schemaRow.nCols - 1);
1,764✔
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) {
1,764!
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);
1,764✔
1589
  return code;
1,764✔
1590
}
1591

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

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

1599
  SMetaTableOp ops[] = {
5,656✔
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++) {
45,232✔
1610
    SMetaTableOp *op = &ops[i];
39,579✔
1611

1612
    if (op->table == META_ENTRY_TABLE && superDropped) {
39,579✔
1613
      continue;
4,878✔
1614
    }
1615

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

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

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

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

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

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

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

1663
  // do the drop
1664
  metaWLock(pMeta);
5,656✔
1665
  code = metaHandleChildTableDropImpl(pMeta, &param, superDropped);
5,656✔
1666
  metaULock(pMeta);
5,656✔
1667
  if (code) {
5,656!
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)) {
5,656!
1676
    int32_t      nCols = 0;
5,656✔
1677
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
5,656✔
1678
    if (metaGetStbStats(pMeta->pVnode, pSuper->uid, NULL, &nCols, 0) == 0) {
5,656!
1679
      pStats->numOfTimeSeries -= nCols - 1;
5,656✔
1680
    }
1681
  }
1682

1683
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
5,656!
1684
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pChild->uid, pSuper->uid, NULL);
3,470✔
1685
    if (ret < 0) {
3,470!
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);
5,656✔
1703
  metaFetchEntryFree(&pSuper);
5,654✔
1704
  return code;
5,656✔
1705
}
1706

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

1710
  SMetaTableOp ops[] = {
×
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++) {
×
1720
    SMetaTableOp *op = &ops[i];
×
1721
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
×
1722
    if (code) {
×
1723
      const SMetaEntry *pEntry = pParam->pEntry;
×
1724
      metaErr(TD_VID(pMeta->pVnode), code);
×
1725
    }
1726
  }
1727

1728
  return code;
×
1729
}
1730

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

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

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

1747
  // do the drop
1748
  metaWLock(pMeta);
×
1749
  code = metaHandleVirtualNormalTableDropImpl(pMeta, &param);
×
1750
  metaULock(pMeta);
×
1751
  if (code) {
×
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--;
×
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) {
×
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);
×
1777
  return code;
×
1778
}
1779

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

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

1787
  SMetaTableOp ops[] = {
×
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++) {
×
1797
    SMetaTableOp *op = &ops[i];
×
1798

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

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

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

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

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

1827
  return code;
×
1828
}
1829

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

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

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

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

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

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

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

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

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

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

1902
  for (;;) {
1903
    if (tdbTbcNext(cursor, &key, &keySize, NULL, NULL) < 0) {
7,769✔
1904
      break;
1,261✔
1905
    }
1906

1907
    if (((SCtbIdxKey *)key)->suid < suid) {
6,511✔
1908
      continue;
254✔
1909
    } else if (((SCtbIdxKey *)key)->suid > suid) {
6,257✔
1910
      break;
12✔
1911
    }
1912

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

1922
  tdbTbcClose(cursor);
1,273✔
1923
  tdbFreeClear(key);
1,272✔
1924
  return code;
1,273✔
1925
}
1926

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

1931
  SMetaTableOp ops[] = {
925✔
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++) {
4,631✔
1941
    SMetaTableOp *op = &ops[i];
3,705✔
1942

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

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

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

1960
  const SMetaEntry *pEntry = pParam->pEntry;
437✔
1961

1962
  SMetaTableOp ops[] = {
437✔
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++) {
2,185✔
1969
    SMetaTableOp *op = &ops[i];
1,748✔
1970
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
1,748✔
1971
    if (code) {
1,748!
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;
437✔
1982
}
1983

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

1987
  const SMetaEntry *pEntry = pParam->pEntry;
×
1988

1989
  SMetaTableOp ops[] = {
×
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++) {
×
1995
    SMetaTableOp *op = &ops[i];
×
1996
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
×
1997
    if (code) {
×
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;
×
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) {
730✔
2048
  int32_t code = TSDB_CODE_SUCCESS;
730✔
2049

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

2054
  SMetaTableOp ops[] = {
730✔
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++) {
4,380✔
2063
    SMetaTableOp *op = &ops[i];
3,650✔
2064
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
3,650✔
2065
    if (code) {
3,650!
2066
      metaErr(TD_VID(pMeta->pVnode), code);
×
2067
      return code;
×
2068
    }
2069
  }
2070

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

2075
  if (metaTbGroupCacheClear(pMeta, pSuperEntry->uid) < 0) {
730!
2076
    metaErr(TD_VID(pMeta->pVnode), code);
×
2077
  }
2078
  return code;
730✔
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) {
3,356✔
2087
  int32_t code = TSDB_CODE_SUCCESS;
3,356✔
2088

2089
  const SMetaEntry *pEntry = pParam->pEntry;
3,356✔
2090
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
3,356✔
2091

2092
  SMetaTableOp ops[] = {
3,356✔
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++) {
13,411✔
2099
    SMetaTableOp *op = &ops[i];
10,056✔
2100
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
10,056✔
2101
    if (code) {
10,052!
2102
      metaErr(TD_VID(pMeta->pVnode), code);
×
2103
      return code;
×
2104
    }
2105
  }
2106

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

2112
  return code;
3,359✔
2113
}
2114

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

2118
  SMetaEntry *pOldEntry = NULL;
3,349✔
2119

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

2126
  SMetaHandleParam param = {
3,361✔
2127
      .pEntry = pEntry,
2128
      .pOldEntry = pOldEntry,
2129
  };
2130
  metaWLock(pMeta);
3,361✔
2131
  code = metaHandleSuperTableUpdateImpl(pMeta, &param);
3,358✔
2132
  metaULock(pMeta);
3,347✔
2133
  if (code) {
3,346!
2134
    metaErr(TD_VID(pMeta->pVnode), code);
×
2135
    metaFetchEntryFree(&pOldEntry);
×
2136
    return code;
×
2137
  }
2138

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

2144
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
3,350✔
2145
    STsdb  *pTsdb = pMeta->pVnode->pTsdb;
338✔
2146
    SArray *uids = NULL; /*taosArrayInit(8, sizeof(int64_t));
338✔
2147
     if (uids == NULL) {
2148
       metaErr(TD_VID(pMeta->pVnode), code);
2149
       metaFetchEntryFree(&pOldEntry);
2150
       return terrno;
2151
       }*/
2152
    if (deltaCol == 1) {
338✔
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) {
154✔
2166
      int16_t cid = -1;
152✔
2167
      bool    hasPrimaryKey = false;
152✔
2168
      if (onCols >= 2) {
152!
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,278✔
2172
        if (pEntry->stbEntry.schemaRow.pSchema[i].colId != pOldEntry->stbEntry.schemaRow.pSchema[j].colId) {
1,218✔
2173
          cid = pOldEntry->stbEntry.schemaRow.pSchema[j].colId;
92✔
2174
          break;
92✔
2175
        }
2176
      }
2177

2178
      if (cid != -1) {
152✔
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);
338✔
2191

2192
    if (pTsdb) {
338!
2193
      tsdbCacheInvalidateSchema(pTsdb, pEntry->uid, -1, pEntry->stbEntry.schemaRow.version);
338✔
2194
    }
2195
  }
2196
  if (updStat) {
3,350✔
2197
    int64_t ctbNum = 0;
1,666✔
2198
    int32_t ret = metaGetStbStats(pMeta->pVnode, pEntry->uid, &ctbNum, 0, 0);
1,666✔
2199
    if (ret < 0) {
1,668!
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);
1,668✔
2204
    if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
1,668✔
2205
  }
2206
  metaFetchEntryFree(&pOldEntry);
3,352✔
2207
  return code;
3,352✔
2208
}
2209

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

2213
  SMetaEntry *pOldEntry = NULL;
730✔
2214
  SMetaEntry *pSuperEntry = NULL;
730✔
2215

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

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

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

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

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

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

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

2261
  // handle update
2262
  SMetaHandleParam param = {
437✔
2263
      .pEntry = pEntry,
2264
      .pOldEntry = pOldEntry,
2265
  };
2266
  metaWLock(pMeta);
437✔
2267
  code = metaHandleNormalTableUpdateImpl(pMeta, &param);
437✔
2268
  metaULock(pMeta);
437✔
2269
  if (code) {
437!
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) &&
437✔
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;
437✔
2307
  pMeta->pVnode->config.vndStats.numOfNTimeSeries += deltaCol;
437✔
2308
  if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
437✔
2309
  metaFetchEntryFree(&pOldEntry);
437✔
2310
  return code;
437✔
2311
}
2312

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

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

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

2338
  metaTimeSeriesNotifyCheck(pMeta);
×
2339
  metaFetchEntryFree(&pOldEntry);
×
2340
  return code;
×
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) {
927✔
2384
  int32_t     code = TSDB_CODE_SUCCESS;
927✔
2385
  SArray     *childList = NULL;
927✔
2386
  SMetaEntry *pOldEntry = NULL;
927✔
2387

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

2394
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childList);
926✔
2395
  if (code) {
927!
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) {
927!
2402
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
×
2403
              pEntry->uid, tstrerror(terrno));
2404
  }
2405

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

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

2420
  // do drop super table
2421
  SMetaHandleParam param = {
924✔
2422
      .pEntry = pEntry,
2423
      .pOldEntry = pOldEntry,
2424
  };
2425
  metaWLock(pMeta);
924✔
2426
  code = metaHandleSuperTableDropImpl(pMeta, &param);
927✔
2427
  metaULock(pMeta);
927✔
2428
  if (code) {
927!
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);
927✔
2440
  metaFetchEntryFree(&pOldEntry);
927✔
2441
  return code;
927✔
2442
}
2443

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

2450
  if (NULL == pMeta || NULL == pEntry) {
165,610!
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) {
165,727✔
2456
    bool isExist = false;
162,272✔
2457
    if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) {
162,272✔
2458
      isExist = true;
4,518✔
2459
    }
2460

2461
    switch (type) {
162,272✔
2462
      case TSDB_SUPER_TABLE: {
26,567✔
2463
        if (isExist) {
26,567✔
2464
          code = metaHandleSuperTableUpdate(pMeta, pEntry);
3,355✔
2465
        } else {
2466
          code = metaHandleSuperTableCreate(pMeta, pEntry);
23,212✔
2467
        }
2468
        break;
26,627✔
2469
      }
2470
      case TSDB_CHILD_TABLE: {
121,479✔
2471
        if (isExist) {
121,479✔
2472
          code = metaHandleChildTableUpdate(pMeta, pEntry);
730✔
2473
        } else {
2474
          code = metaHandleChildTableCreate(pMeta, pEntry);
120,749✔
2475
        }
2476
        break;
121,508✔
2477
      }
2478
      case TSDB_NORMAL_TABLE: {
14,177✔
2479
        if (isExist) {
14,177✔
2480
          code = metaHandleNormalTableUpdate(pMeta, pEntry);
437✔
2481
        } else {
2482
          code = metaHandleNormalTableCreate(pMeta, pEntry);
13,740✔
2483
        }
2484
        break;
14,176✔
2485
      }
2486
      case TSDB_VIRTUAL_NORMAL_TABLE: {
1✔
2487
        if (isExist) {
1!
2488
          code = metaHandleVirtualNormalTableUpdate(pMeta, pEntry);
×
2489
        } else {
2490
          code = metaHandleVirtualNormalTableCreate(pMeta, pEntry);
1✔
2491
        }
2492
        break;
1✔
2493
      }
2494
      case TSDB_VIRTUAL_CHILD_TABLE: {
1✔
2495
        if (isExist) {
1!
2496
          code = metaHandleVirtualChildTableUpdate(pMeta, pEntry);
×
2497
        } else {
2498
          code = metaHandleVirtualChildTableCreate(pMeta, pEntry);
1✔
2499
        }
2500
        break;
1✔
2501
      }
2502
      default: {
47✔
2503
        code = TSDB_CODE_INVALID_PARA;
47✔
2504
        break;
47✔
2505
      }
2506
    }
2507
  } else {
2508
    switch (type) {
3,455!
2509
      case TSDB_SUPER_TABLE: {
927✔
2510
        code = metaHandleSuperTableDrop(pMeta, pEntry);
927✔
2511
        break;
927✔
2512
      }
2513
      case TSDB_CHILD_TABLE: {
778✔
2514
        code = metaHandleChildTableDrop(pMeta, pEntry, false);
778✔
2515
        break;
778✔
2516
      }
2517
      case TSDB_NORMAL_TABLE: {
1,764✔
2518
        code = metaHandleNormalTableDrop(pMeta, pEntry);
1,764✔
2519
        break;
1,764✔
2520
      }
2521
      case TSDB_VIRTUAL_NORMAL_TABLE: {
×
2522
        code = metaHandleVirtualNormalTableDrop(pMeta, pEntry);
×
2523
        break;
×
2524
      }
2525
      case TSDB_VIRTUAL_CHILD_TABLE: {
×
2526
        code = metaHandleVirtualChildTableDrop(pMeta, pEntry, false);
×
2527
        break;
×
2528
      }
2529
      default: {
×
2530
        code = TSDB_CODE_INVALID_PARA;
×
2531
        break;
×
2532
      }
2533
    }
2534
  }
2535

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

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