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

taosdata / TDengine / #5025

17 Apr 2026 01:15AM UTC coverage: 73.009% (+0.06%) from 72.954%
#5025

push

travis-ci

web-flow
fix: replace \s with [[:space:]] in shell regex for macOS BSD compat (#35162)

6 of 7 new or added lines in 2 files covered. (85.71%)

2432 existing lines in 145 files now uncovered.

273326 of 374375 relevant lines covered (73.01%)

130781443.02 hits per line

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

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

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

14
extern SDmNotifyHandle dmNotifyHdl;
15

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

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

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

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

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

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

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

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

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

78
  // search entry table
79
  STbDbKey key = {
208,789,122✔
80
      .version = ((SUidIdxVal *)value)->version,
208,782,589✔
81
      .uid = uid,
82
  };
83
  tdbFreeClear(value);
208,794,433✔
84

85
  code = tdbTbGet(pMeta->pTbDb, &key, sizeof(key), &value, &valueSize);
208,783,391✔
86
  if (TSDB_CODE_SUCCESS != code) {
208,790,467✔
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};
208,790,467✔
94
  SMetaEntry entry = {0};
208,788,809✔
95

96
  tDecoderInit(&decoder, value, valueSize);
208,795,230✔
97
  code = metaDecodeEntry(&decoder, &entry);
208,804,245✔
98
  if (code) {
208,785,949✔
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);
208,785,949✔
107
  if (code) {
208,783,392✔
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);
208,783,392✔
116
  tDecoderClear(&decoder);
208,779,598✔
117
  return code;
208,800,003✔
118
}
119

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

125
  code = tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &value, &valueSize);
84,978,092✔
126
  if (TSDB_CODE_SUCCESS != code) {
84,948,873✔
127
    metaError("vgId:%d, failed to get entry by name:%s since %s", TD_VID(pMeta->pVnode), name, tstrerror(code));
957✔
128
    return code;
957✔
129
  }
130
  int64_t uid = *(int64_t *)value;
84,947,916✔
131
  tdbFreeClear(value);
84,962,369✔
132

133
  code = metaFetchEntryByUid(pMeta, uid, ppEntry);
84,963,649✔
134
  if (TSDB_CODE_SUCCESS != code) {
84,958,985✔
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;
84,958,985✔
139
}
140

141
void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); }
208,876,161✔
142

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

147
  int32_t  code = TSDB_CODE_SUCCESS;
109,186,030✔
148
  int32_t  vgId = TD_VID(pMeta->pVnode);
109,186,030✔
149
  void    *value = NULL;
109,181,436✔
150
  int32_t  valueSize = 0;
109,181,436✔
151
  SEncoder encoder = {0};
109,181,436✔
152
  STbDbKey key = {
109,179,293✔
153
      .version = pEntry->version,
109,167,482✔
154
      .uid = pEntry->uid,
109,184,367✔
155
  };
156

157
  // encode entry
158
  tEncodeSize(metaEncodeEntry, pEntry, valueSize, code);
109,188,792✔
159
  if (code != 0) {
109,141,237✔
160
    metaErr(vgId, code);
×
161
    return code;
×
162
  }
163

164
  value = taosMemoryMalloc(valueSize);
109,141,237✔
165
  if (NULL == value) {
109,117,181✔
166
    metaErr(vgId, terrno);
×
167
    return terrno;
×
168
  }
169

170
  tEncoderInit(&encoder, value, valueSize);
109,117,181✔
171
  code = metaEncodeEntry(&encoder, pEntry);
109,164,505✔
172
  if (code) {
109,159,430✔
173
    metaErr(vgId, code);
×
174
    tEncoderClear(&encoder);
×
175
    taosMemoryFree(value);
×
176
    return code;
×
177
  }
178
  tEncoderClear(&encoder);
109,159,430✔
179

180
  // put to tdb
181
  if (META_TABLE_OP_INSERT == op) {
109,158,127✔
182
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
85,890,912✔
183
  } else if (META_TABLE_OP_UPDATA == op) {
23,267,215✔
184
    code = tdbTbUpsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
20,549,073✔
185
  } else if (META_TABLE_OP_DELETE == op) {
2,718,142✔
186
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
2,718,142✔
187
  } else {
188
    code = TSDB_CODE_INVALID_PARA;
×
189
  }
190
  if (TSDB_CODE_SUCCESS != code) {
109,157,449✔
191
    metaErr(vgId, code);
×
192
  }
193
  taosMemoryFree(value);
109,156,894✔
194
  return code;
109,161,461✔
195
}
196

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

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

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

209
// Schema Table
210
static int32_t metaSchemaTableUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
24,321,118✔
211
  int32_t  code = TSDB_CODE_SUCCESS;
24,321,118✔
212
  int32_t  vgId = TD_VID(pMeta->pVnode);
24,321,118✔
213
  SEncoder encoder = {0};
24,331,778✔
214
  void    *value = NULL;
24,343,617✔
215
  int32_t  valueSize = 0;
24,343,617✔
216
  int32_t  valueSizeExt = 0;
24,343,617✔
217
  bool     hasTypeMods = false;
24,343,617✔
218

219
  const SMetaEntry     *pEntry = pParam->pEntry;
24,343,617✔
220
  const SSchemaWrapper *pSchema = NULL;
24,311,919✔
221
  const SExtSchema     *pSchemaExt = NULL;
24,311,919✔
222
  if (pEntry->type == TSDB_SUPER_TABLE) {
24,311,919✔
223
    pSchema = &pEntry->stbEntry.schemaRow;
12,461,781✔
224
    pSchemaExt = pEntry->pExtSchemas;
12,466,382✔
225
    valueSizeExt = sizeof(SExtSchema) * pEntry->stbEntry.schemaRow.nCols;
12,436,412✔
226
    hasTypeMods = schemasHasTypeMod(pSchema->pSchema, pEntry->stbEntry.schemaRow.nCols);
12,429,796✔
227
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
11,884,064✔
228
    pSchema = &pEntry->ntbEntry.schemaRow;
11,884,064✔
229
    pSchemaExt = pEntry->pExtSchemas;
11,884,064✔
230
    valueSizeExt = sizeof(SExtSchema) * pEntry->ntbEntry.schemaRow.nCols;
11,884,064✔
231
    hasTypeMods = schemasHasTypeMod(pSchema->pSchema, pEntry->ntbEntry.schemaRow.nCols);
11,884,064✔
232
  } else {
UNCOV
233
    return TSDB_CODE_INVALID_PARA;
×
234
  }
235
  
236

237
  SSkmDbKey key = {
24,313,564✔
238
      .uid = pEntry->uid,
24,307,747✔
239
      .sver = pSchema->version,
24,306,536✔
240
  };
241

242
  // encode schema
243
  tEncodeSize(tEncodeSSchemaWrapper, pSchema, valueSize, code);
48,676,797✔
244
  if (TSDB_CODE_SUCCESS != code) {
24,343,177✔
UNCOV
245
    metaErr(vgId, code);
×
UNCOV
246
    return code;
×
247
  }
248

249
  value = taosMemoryMalloc(valueSize);
24,343,177✔
250
  if (NULL == value) {
24,303,008✔
UNCOV
251
    metaErr(vgId, terrno);
×
UNCOV
252
    return terrno;
×
253
  }
254

255
  tEncoderInit(&encoder, value, valueSize);
24,303,008✔
256
  code = tEncodeSSchemaWrapper(&encoder, pSchema);
24,349,152✔
257
  if (TSDB_CODE_SUCCESS != code) {
24,349,152✔
UNCOV
258
    metaErr(vgId, code);
×
UNCOV
259
    tEncoderClear(&encoder);
×
260
    taosMemoryFree(value);
×
UNCOV
261
    return code;
×
262
  }
263
  tEncoderClear(&encoder);
24,349,152✔
264

265
  // put to tdb
266
  if (META_TABLE_OP_INSERT == op) {
24,346,735✔
267
    code = tdbTbInsert(pMeta->pSkmDb, &key, sizeof(key), value, valueSize, pMeta->txn);
1,566✔
UNCOV
268
    if (code == 0 && hasTypeMods){
×
269
      code = tdbTbInsert(pMeta->pSkmExtDb, &key, sizeof(key), pSchemaExt, valueSizeExt, pMeta->txn);
×
270
    }
271
  } else if (META_TABLE_OP_UPDATA == op) {
24,345,358✔
272
    code = tdbTbUpsert(pMeta->pSkmDb, &key, sizeof(key), value, valueSize, pMeta->txn);
24,345,358✔
273
    if (code == 0 && hasTypeMods){
24,365,593✔
274
      code = tdbTbUpsert(pMeta->pSkmExtDb, &key, sizeof(key), pSchemaExt, valueSizeExt, pMeta->txn);
4,504,878✔
275
    }
276
  } else {
UNCOV
277
    code = TSDB_CODE_INVALID_PARA;
×
278
  }
279
  if (TSDB_CODE_SUCCESS != code) {
24,360,743✔
UNCOV
280
    metaErr(vgId, code);
×
281
  }
282
  taosMemoryFree(value);
24,353,740✔
283
  return code;
24,354,843✔
284
}
285

UNCOV
286
static int32_t metaSchemaTableInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
UNCOV
287
  return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
×
288
}
289

290
static int32_t metaAddOrDropTagIndexOfSuperTable(SMeta *pMeta, const SMetaHandleParam *pParam,
23,140,437✔
291
                                                 const SSchema *pOldColumn, const SSchema *pNewColumn) {
292
  int32_t code = TSDB_CODE_SUCCESS;
23,140,437✔
293

294
  const SMetaEntry *pEntry = pParam->pEntry;
23,140,437✔
295
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
23,150,581✔
296
  enum { ADD_INDEX, DROP_INDEX } action;
297

298
  if (pOldColumn && pNewColumn) {
23,145,480✔
299
    if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
22,511,894✔
300
      return TSDB_CODE_SUCCESS;
1,264,327✔
301
    } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) {
21,250,030✔
302
      action = DROP_INDEX;
25,318✔
303
    } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
21,215,505✔
304
      action = ADD_INDEX;
18,490✔
305
    } else {
306
      return TSDB_CODE_SUCCESS;
21,192,359✔
307
    }
308
  } else if (pOldColumn) {
633,586✔
309
    if (IS_IDX_ON(pOldColumn)) {
225,740✔
310
      action = DROP_INDEX;
10,740✔
311
    } else {
312
      return TSDB_CODE_SUCCESS;
214,323✔
313
    }
314
  } else {
315
    if (IS_IDX_ON(pNewColumn)) {
407,846✔
UNCOV
316
      action = ADD_INDEX;
×
317
    } else {
318
      return TSDB_CODE_SUCCESS;
411,228✔
319
    }
320
  }
321

322
  // fetch all child tables
323
  SArray *childTables = 0;
54,548✔
324
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childTables);
55,144✔
325
  if (code) {
55,144✔
UNCOV
326
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
327
    return code;
×
328
  }
329

330
  // do drop or add index
331
  for (int32_t i = 0; i < taosArrayGetSize(childTables); i++) {
7,546,744✔
332
    int64_t uid = *(int64_t *)taosArrayGet(childTables, i);
7,491,600✔
333

334
    // fetch child entry
335
    SMetaEntry *pChildEntry = NULL;
7,491,600✔
336
    code = metaFetchEntryByUid(pMeta, uid, &pChildEntry);
7,491,600✔
337
    if (code) {
7,491,600✔
UNCOV
338
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
339
      taosArrayDestroy(childTables);
×
340
      return code;
×
341
    }
342

343
    STagIdxKey *pTagIdxKey = NULL;
7,491,600✔
344
    int32_t     tagIdxKeySize = 0;
7,491,600✔
345

346
    if (action == ADD_INDEX) {
7,491,600✔
347
      code = metaFetchTagIdxKey(pMeta, pChildEntry, pNewColumn, &pTagIdxKey, &tagIdxKeySize);
5,290,161✔
348
      if (code) {
5,290,161✔
349
        metaErr(TD_VID(pMeta->pVnode), code);
×
350
        taosArrayDestroy(childTables);
×
351
        metaFetchEntryFree(&pChildEntry);
×
352
        return code;
×
353
      }
354

355
      code = tdbTbInsert(pMeta->pTagIdx, pTagIdxKey, tagIdxKeySize, NULL, 0, pMeta->txn);
5,290,161✔
356
      if (code) {
5,290,161✔
357
        metaErr(TD_VID(pMeta->pVnode), code);
×
358
        taosArrayDestroy(childTables);
×
359
        metaFetchEntryFree(&pChildEntry);
×
360
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
361
        return code;
×
362
      }
363
    } else {
364
      code = metaFetchTagIdxKey(pMeta, pChildEntry, pOldColumn, &pTagIdxKey, &tagIdxKeySize);
2,201,439✔
365
      if (code) {
2,201,439✔
UNCOV
366
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
367
        taosArrayDestroy(childTables);
×
UNCOV
368
        metaFetchEntryFree(&pChildEntry);
×
UNCOV
369
        return code;
×
370
      }
371

372
      code = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, tagIdxKeySize, pMeta->txn);
2,201,439✔
373
      if (code) {
2,201,439✔
UNCOV
374
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
375
        taosArrayDestroy(childTables);
×
UNCOV
376
        metaFetchEntryFree(&pChildEntry);
×
UNCOV
377
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
UNCOV
378
        return code;
×
379
      }
380
    }
381

382
    metaFetchTagIdxKeyFree(&pTagIdxKey);
7,491,600✔
383
    metaFetchEntryFree(&pChildEntry);
7,491,600✔
384
  }
385

386
  taosArrayDestroy(childTables);
55,144✔
387
  return code;
55,144✔
388
}
389

390
static int32_t metaAddOrDropColumnIndexOfVirtualSuperTable(SMeta *pMeta, const SMetaHandleParam *pParam,
150,433,427✔
391
                                                           const SSchema *pOldColumn, const SSchema *pNewColumn) {
392
  int32_t code = TSDB_CODE_SUCCESS;
150,433,427✔
393

394
  const SMetaEntry *pEntry = pParam->pEntry;
150,433,427✔
395
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
151,744,895✔
396
  enum { ADD_COLUMN, DROP_COLUMN } action;
397

398
  if (pOldColumn && pNewColumn) {
151,953,511✔
399
    return TSDB_CODE_SUCCESS;
151,938,005✔
400
  } else if (pOldColumn) {
15,506✔
401
    action = DROP_COLUMN;
7,695✔
402
  } else {
403
    action = ADD_COLUMN;
7,811✔
404
  }
405

406
  // fetch all child tables
407
  SArray *childTables = 0;
15,506✔
408
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childTables);
15,506✔
409
  if (code) {
15,544✔
UNCOV
410
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
411
    return code;
×
412
  }
413

414
  // do drop or add index
415
  for (int32_t i = 0; i < taosArrayGetSize(childTables); i++) {
26,679✔
416
    int64_t uid = *(int64_t *)taosArrayGet(childTables, i);
11,135✔
417

418
    // fetch child entry
419
    SMetaEntry *pChildEntry = NULL;
11,135✔
420
    code = metaFetchEntryByUid(pMeta, uid, &pChildEntry);
11,135✔
421
    if (code) {
11,135✔
UNCOV
422
      metaErr(TD_VID(pMeta->pVnode), code);
×
423
      taosArrayDestroy(childTables);
×
424
      return code;
×
425
    }
426

427
    SMetaHandleParam param = {.pEntry = pChildEntry};
11,135✔
428

429
    if (action == ADD_COLUMN) {
11,135✔
430
      code = updataTableColRef(&pChildEntry->colRef, pNewColumn, 1, NULL);
6,518✔
431
      if (code) {
6,518✔
432
        metaErr(TD_VID(pMeta->pVnode), code);
×
433
        taosArrayDestroy(childTables);
×
434
        metaFetchEntryFree(&pChildEntry);
×
UNCOV
435
        return code;
×
436
      }
437

438
      code = metaEntryTableUpdate(pMeta, &param);
6,518✔
439
      if (code) {
6,518✔
440
        metaErr(TD_VID(pMeta->pVnode), code);
×
441
        taosArrayDestroy(childTables);
×
442
        metaFetchEntryFree(&pChildEntry);
×
UNCOV
443
        return code;
×
444
      }
445
    } else {
446
      code = updataTableColRef(&pChildEntry->colRef, pOldColumn, 0, NULL);
4,617✔
447
      if (code) {
4,617✔
UNCOV
448
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
449
        taosArrayDestroy(childTables);
×
UNCOV
450
        metaFetchEntryFree(&pChildEntry);
×
UNCOV
451
        return code;
×
452
      }
453

454
      code = metaEntryTableUpdate(pMeta, &param);
4,617✔
455
      if (code) {
4,617✔
UNCOV
456
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
457
        taosArrayDestroy(childTables);
×
UNCOV
458
        metaFetchEntryFree(&pChildEntry);
×
UNCOV
459
        return code;
×
460
      }
461
    }
462
    metaFetchEntryFree(&pChildEntry);
11,135✔
463
  }
464

465
  taosArrayDestroy(childTables);
15,544✔
466
  return code;
15,544✔
467
}
468

469
static int32_t metaUpdateSuperTableTagSchema(SMeta *pMeta, const SMetaHandleParam *pParam) {
1,220,846✔
470
  int32_t               code = TSDB_CODE_SUCCESS;
1,220,846✔
471
  const SMetaEntry     *pEntry = pParam->pEntry;
1,220,846✔
472
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
1,226,586✔
473
  const SSchemaWrapper *pNewTagSchema = &pEntry->stbEntry.schemaTag;
1,225,338✔
474
  const SSchemaWrapper *pOldTagSchema = &pOldEntry->stbEntry.schemaTag;
1,225,232✔
475

476
  int32_t iOld = 0, iNew = 0;
1,220,327✔
477
  for (; iOld < pOldTagSchema->nCols && iNew < pNewTagSchema->nCols;) {
23,889,064✔
478
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
22,674,205✔
479
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
22,672,905✔
480

481
    if (pOldColumn->colId == pNewColumn->colId) {
22,672,531✔
482
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
22,506,855✔
483
      if (code) {
22,504,041✔
484
        metaErr(TD_VID(pMeta->pVnode), code);
731✔
UNCOV
485
        return code;
×
486
      }
487

488
      iOld++;
22,503,310✔
489
      iNew++;
22,503,310✔
490
    } else if (pOldColumn->colId < pNewColumn->colId) {
166,781✔
491
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
165,427✔
492
      if (code) {
165,075✔
UNCOV
493
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
494
        return code;
×
495
      }
496

497
      // drop old tag from meta stable tag filter cache
498
      code = metaStableTagFilterCacheDropTag(pMeta, pEntry->uid, pOldColumn->colId);
165,075✔
499
      if (code) {
165,427✔
UNCOV
500
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
501
        return code;
×
502
      }
503

504
      iOld++;
165,427✔
505
    } else {
UNCOV
506
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, NULL, pNewColumn);
×
UNCOV
507
      if (code) {
×
UNCOV
508
        metaErr(TD_VID(pMeta->pVnode), code);
×
509
        return code;
×
510
      }
511

UNCOV
512
      iNew++;
×
513
    }
514
  }
515

516
  for (; iOld < pOldTagSchema->nCols; iOld++) {
1,279,742✔
517
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
60,374✔
518
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
58,301✔
519
    if (code) {
60,313✔
UNCOV
520
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
521
      return code;
×
522
    }
523
    // drop old tag from meta stable tag filter cache
524
    code = metaStableTagFilterCacheDropTag(pMeta, pEntry->uid, pOldColumn->colId);
60,313✔
525
    if (code) {
56,947✔
UNCOV
526
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
527
      return code;
×
528
    }
529
  }
530

531
  for (; iNew < pNewTagSchema->nCols; iNew++) {
1,626,943✔
532
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
410,829✔
533
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, NULL, pNewColumn);
410,552✔
534
    if (code) {
407,846✔
UNCOV
535
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
536
      return code;
×
537
    }
538
  }
539

540
  return code;
1,218,468✔
541
}
542

543
static int32_t metaUpdateSuperTableRowSchema(SMeta *pMeta, const SMetaHandleParam *pParam) {
19,397✔
544
  int32_t               code = TSDB_CODE_SUCCESS;
19,397✔
545
  const SMetaEntry     *pEntry = pParam->pEntry;
19,397✔
546
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
19,397✔
547
  const SSchemaWrapper *pNewRowSchema = &pEntry->stbEntry.schemaRow;
19,397✔
548
  const SSchemaWrapper *pOldRowSchema = &pOldEntry->stbEntry.schemaRow;
19,397✔
549

550
  int32_t iOld = 0, iNew = 0;
19,397✔
551
  for (; iOld < pOldRowSchema->nCols && iNew < pNewRowSchema->nCols;) {
150,753,466✔
552
    SSchema *pOldColumn = pOldRowSchema->pSchema + iOld;
150,783,879✔
553
    SSchema *pNewColumn = pNewRowSchema->pSchema + iNew;
150,787,981✔
554

555
    if (pOldColumn->colId == pNewColumn->colId) {
150,555,339✔
556
      code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
151,570,583✔
557
      if (code) {
150,818,159✔
558
        metaErr(TD_VID(pMeta->pVnode), code);
86,728✔
559
        return code;
×
560
      }
561

562
      iOld++;
150,731,431✔
563
      iNew++;
150,731,431✔
564
    } else if (pOldColumn->colId < pNewColumn->colId) {
2,638✔
565
      code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, pOldColumn, NULL);
2,638✔
566
      if (code) {
2,638✔
UNCOV
567
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
568
        return code;
×
569
      }
570

571
      iOld++;
2,638✔
572
    } else {
UNCOV
573
      code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, NULL, pNewColumn);
×
UNCOV
574
      if (code) {
×
UNCOV
575
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
576
        return code;
×
577
      }
578

579
      iNew++;
×
580
    }
581
  }
582

583
  for (; iOld < pOldRowSchema->nCols; iOld++) {
24,473✔
584
    SSchema *pOldColumn = pOldRowSchema->pSchema + iOld;
5,057✔
585
    code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, pOldColumn, NULL);
5,057✔
586
    if (code) {
5,076✔
UNCOV
587
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
588
      return code;
×
589
    }
590
  }
591

592
  for (; iNew < pNewRowSchema->nCols; iNew++) {
27,246✔
593
    SSchema *pNewColumn = pNewRowSchema->pSchema + iNew;
7,830✔
594
    code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, NULL, pNewColumn);
7,830✔
595
    if (code) {
7,830✔
UNCOV
596
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
597
      return code;
×
598
    }
599
  }
600

601
  return code;
19,416✔
602
}
603

604
static int32_t metaSchemaTableUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
25,591,863✔
605
  int32_t code = TSDB_CODE_SUCCESS;
25,591,863✔
606

607
  const SMetaEntry *pEntry = pParam->pEntry;
25,591,863✔
608
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
25,627,131✔
609

610
  if (NULL == pOldEntry) {
25,612,231✔
611
    return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
13,526,595✔
612
  }
613

614
  if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
12,085,636✔
615
    // check row schema
616
    if (pOldEntry->ntbEntry.schemaRow.version != pEntry->ntbEntry.schemaRow.version) {
3,952,907✔
617
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
3,920,823✔
618
    }
619
  } else if (pEntry->type == TSDB_SUPER_TABLE) {
8,137,881✔
620
    // check row schema
621
    if (pOldEntry->stbEntry.schemaRow.version != pEntry->stbEntry.schemaRow.version) {
8,155,853✔
622
      if (TABLE_IS_VIRTUAL(pEntry->flags)) {
6,925,270✔
623
        return metaUpdateSuperTableRowSchema(pMeta, pParam);
19,397✔
624
      } else {
625
        return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
6,891,411✔
626
      }
627
    }
628

629
    // check tag schema
630
    code = metaUpdateSuperTableTagSchema(pMeta, pParam);
1,222,171✔
631
    if (code) {
1,223,306✔
632
      metaErr(TD_VID(pMeta->pVnode), code);
1,501✔
UNCOV
633
      return code;
×
634
    }
635

636
  } else {
UNCOV
637
    return TSDB_CODE_INVALID_PARA;
×
638
  }
639

640
  return TSDB_CODE_SUCCESS;
1,242,290✔
641
}
642

UNCOV
643
static int32_t metaSchemaTableDelete(SMeta *pMeta, const SMetaHandleParam *pEntry) {
×
644
  // TODO
UNCOV
645
  return TSDB_CODE_SUCCESS;
×
646
}
647

648
// Uid Index
649
static void metaBuildEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) {
106,429,698✔
650
  pInfo->uid = pEntry->uid;
106,429,698✔
651
  pInfo->version = pEntry->version;
106,433,494✔
652
  if (pEntry->type == TSDB_SUPER_TABLE) {
106,463,140✔
653
    pInfo->suid = pEntry->uid;
13,689,393✔
654
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
13,706,599✔
655
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
92,699,777✔
656
    pInfo->suid = pEntry->ctbEntry.suid;
80,819,284✔
657
    pInfo->skmVer = 0;
80,817,921✔
658
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
11,904,452✔
659
    pInfo->suid = 0;
11,904,452✔
660
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
11,904,452✔
661
  }
662
}
106,402,937✔
663

664
static int32_t metaUidIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
106,424,513✔
665
  int32_t code = TSDB_CODE_SUCCESS;
106,424,513✔
666
  int32_t vgId = TD_VID(pMeta->pVnode);
106,424,513✔
667

668
  const SMetaEntry *pEntry = pParam->pEntry;
106,431,894✔
669

670
  // update cache
671
  SMetaInfo info = {0};
106,455,873✔
672
  metaBuildEntryInfo(pEntry, &info);
106,451,128✔
673
  code = metaCacheUpsert(pMeta, &info);
106,370,968✔
674
  if (code) {
106,407,225✔
UNCOV
675
    metaErr(vgId, code);
×
676
  }
677

678
  // put to tdb
679
  SUidIdxVal value = {
106,407,225✔
680
      .suid = info.suid,
106,418,804✔
681
      .skmVer = info.skmVer,
106,352,999✔
682
      .version = pEntry->version,
106,418,804✔
683
  };
684
  if (META_TABLE_OP_INSERT == op) {
106,352,999✔
685
    code = tdbTbInsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
85,862,342✔
686
  } else if (META_TABLE_OP_UPDATA == op) {
20,490,657✔
687
    code = tdbTbUpsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
20,531,515✔
688
  }
689
  return code;
106,435,053✔
690
}
691

692
static int32_t metaUidIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
85,884,444✔
693
  return metaUidIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
85,884,444✔
694
}
695

696
static int32_t metaUidIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
20,540,438✔
697
  return metaUidIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
20,540,438✔
698
}
699

700
static int32_t metaUidIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
3,562,846✔
701
  int32_t code = 0;
3,562,846✔
702

703
  const SMetaEntry *pEntry = pParam->pOldEntry;
3,562,846✔
704

705
  // delete tdb
706
  code = tdbTbDelete(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), pMeta->txn);
3,563,533✔
707
  if (code) {
3,562,529✔
708
    metaErr(TD_VID(pMeta->pVnode), code);
×
709
  }
710

711
  // delete cache
712
  (void)metaCacheDrop(pMeta, pEntry->uid);
3,562,529✔
713
  return code;
3,563,533✔
714
}
715

716
// Name Index
717
static int32_t metaNameIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
85,893,184✔
718
  int32_t code = TSDB_CODE_SUCCESS;
85,893,184✔
719

720
  const SMetaEntry *pEntry = pParam->pEntry;
85,893,184✔
721

722
  if (META_TABLE_OP_INSERT == op) {
85,903,788✔
723
    code = tdbTbInsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
85,903,807✔
724
                       pMeta->txn);
725
  } else if (META_TABLE_OP_UPDATA == op) {
×
726
    code = tdbTbUpsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
×
727
                       pMeta->txn);
728
  } else {
UNCOV
729
    code = TSDB_CODE_INVALID_PARA;
×
730
  }
731
  if (code) {
85,905,267✔
UNCOV
732
    metaErr(TD_VID(pMeta->pVnode), code);
×
733
  }
734
  return code;
85,902,923✔
735
}
736

737
static int32_t metaNameIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
85,895,750✔
738
  int32_t code = TSDB_CODE_SUCCESS;
85,895,750✔
739
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
85,895,750✔
740
}
741

UNCOV
742
static int32_t metaNameIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
UNCOV
743
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
×
744
}
745

746
static int32_t metaNameIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
3,561,492✔
747
  int32_t code = TSDB_CODE_SUCCESS;
3,561,492✔
748

749
  const SMetaEntry *pEntry = pParam->pOldEntry;
3,561,492✔
750
  code = tdbTbDelete(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, pMeta->txn);
3,563,533✔
751
  if (code) {
3,563,533✔
UNCOV
752
    metaErr(TD_VID(pMeta->pVnode), code);
×
753
  }
754
  return code;
3,562,846✔
755
}
756

757
// Suid Index
758
static int32_t metaSUidIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
5,563,251✔
759
  const SMetaEntry *pEntry = pParam->pEntry;
5,563,251✔
760

761
  int32_t code = tdbTbInsert(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), NULL, 0, pMeta->txn);
5,569,512✔
762
  if (code) {
5,569,536✔
UNCOV
763
    metaErr(TD_VID(pMeta->pVnode), code);
×
764
  }
765
  return code;
5,567,638✔
766
}
767

768
static int32_t metaSUidIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
961,507✔
769
  const SMetaEntry *pEntry = pParam->pOldEntry;
961,507✔
770

771
  int32_t code = tdbTbDelete(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), pMeta->txn);
962,194✔
772
  if (code) {
962,194✔
UNCOV
773
    metaErr(TD_VID(pMeta->pVnode), code);
×
774
  }
775
  return code;
961,507✔
776
}
777

778
// Child Index
779
static int32_t metaChildIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
80,628,816✔
780
  int32_t code = TSDB_CODE_SUCCESS;
80,628,816✔
781

782
  const SMetaEntry *pEntry = pParam->pEntry;
80,628,816✔
783

784
  SCtbIdxKey key = {
80,633,845✔
785
      .suid = pEntry->ctbEntry.suid,
80,635,765✔
786
      .uid = pEntry->uid,
80,628,608✔
787
  };
788

789
  if (META_TABLE_OP_INSERT == op) {
80,636,121✔
790
    code = tdbTbInsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
144,748,277✔
791
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
72,374,440✔
792
  } else if (META_TABLE_OP_UPDATA == op) {
8,258,049✔
793
    code = tdbTbUpsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
16,516,041✔
794
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
8,258,049✔
795
  } else {
UNCOV
796
    code = TSDB_CODE_INVALID_PARA;
×
797
  }
798
  return code;
80,639,209✔
799
}
800

801
static int32_t metaChildIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
72,373,496✔
802
  return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
72,373,496✔
803
}
804

805
static int32_t metaChildIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
8,456,728✔
806
  const SMetaEntry *pEntry = pParam->pEntry;
8,456,728✔
807
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
8,456,728✔
808
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
8,456,728✔
809

810
  const STag *pNewTags = (const STag *)pEntry->ctbEntry.pTags;
8,456,728✔
811
  const STag *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags;
8,456,728✔
812
  if (pNewTags->len != pOldTags->len || memcmp(pNewTags, pOldTags, pNewTags->len)) {
8,456,728✔
813
    return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
8,258,049✔
814
  }
815
  return 0;
198,679✔
816
}
817

818
static int32_t metaChildIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
1,714,562✔
819
  const SMetaEntry *pEntry = pParam->pOldEntry;
1,714,562✔
820

821
  SCtbIdxKey key = {
1,714,562✔
822
      .suid = pEntry->ctbEntry.suid,
1,714,562✔
823
      .uid = pEntry->uid,
1,714,562✔
824
  };
825
  return tdbTbDelete(pMeta->pCtbIdx, &key, sizeof(key), pMeta->txn);
1,714,562✔
826
}
827

828
// Tag Index
829
static int32_t metaFetchTagIdxKey(SMeta *pMeta, const SMetaEntry *pEntry, const SSchema *pTagColumn,
100,873,594✔
830
                                  STagIdxKey **ppTagIdxKey, int32_t *pTagIdxKeySize) {
831
  int32_t code = TSDB_CODE_SUCCESS;
100,873,594✔
832

833
  STagIdxKey *pTagIdxKey = NULL;
100,873,594✔
834
  int32_t     nTagIdxKey;
100,877,009✔
835
  const void *pTagData = NULL;
100,878,313✔
836
  int32_t     nTagData = 0;
100,878,313✔
837

838
  STagVal tagVal = {
100,878,313✔
839
      .cid = pTagColumn->colId,
100,877,520✔
840
  };
841

842
  if (tTagGet((const STag *)pEntry->ctbEntry.pTags, &tagVal)) {
100,873,388✔
843
    if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
97,854,925✔
844
      pTagData = tagVal.pData;
11,442,602✔
845
      nTagData = (int32_t)tagVal.nData;
11,442,602✔
846
    } else {
847
      pTagData = &(tagVal.i64);
86,411,265✔
848
      nTagData = tDataTypes[pTagColumn->type].bytes;
86,411,265✔
849
    }
850
  } else {
851
    if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
3,016,917✔
852
      nTagData = tDataTypes[pTagColumn->type].bytes;
2,917,980✔
853
    }
854
  }
855

856
  code = metaCreateTagIdxKey(pEntry->ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type,
100,876,569✔
857
                             pEntry->uid, &pTagIdxKey, &nTagIdxKey);
100,871,787✔
858
  if (code) {
100,867,220✔
UNCOV
859
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
860
    return code;
×
861
  }
862

863
  *ppTagIdxKey = pTagIdxKey;
100,867,220✔
864
  *pTagIdxKeySize = nTagIdxKey;
100,866,484✔
865
  return code;
100,869,887✔
866
}
867

868
static void metaFetchTagIdxKeyFree(STagIdxKey **ppTagIdxKey) {
100,876,917✔
869
  metaDestroyTagIdxKey(*ppTagIdxKey);
100,876,917✔
870
  *ppTagIdxKey = NULL;
100,874,148✔
871
}
100,875,086✔
872

873
static int32_t metaTagIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
72,371,328✔
874
  int32_t code = TSDB_CODE_SUCCESS;
72,371,328✔
875

876
  const SMetaEntry *pEntry = pParam->pEntry;
72,371,328✔
877
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
72,378,977✔
878

879
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
72,379,475✔
880
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
72,663,064✔
881
    const SSchema *pTagColumn = &pTagSchema->pSchema[0];
285,778✔
882

883
    STagVal tagVal = {
571,556✔
884
        .cid = pTagColumn->colId,
285,778✔
885
    };
886

887
    const void *pTagData = pEntry->ctbEntry.pTags;
285,778✔
888
    int32_t     nTagData = ((const STag *)pEntry->ctbEntry.pTags)->len;
285,778✔
889
    code = metaSaveJsonVarToIdx(pMeta, pEntry, pTagColumn);
285,778✔
890
    if (code) {
285,778✔
UNCOV
891
      metaErr(TD_VID(pMeta->pVnode), code);
×
892
    }
893
  } else {
894
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
297,396,964✔
895
      STagIdxKey    *pTagIdxKey = NULL;
225,295,615✔
896
      int32_t        nTagIdxKey;
225,290,215✔
897
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
225,305,861✔
898

899
      if (!IS_IDX_ON(pTagColumn)) {
225,313,239✔
900
        continue;
153,181,363✔
901
      }
902

903
      code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pTagIdxKey, &nTagIdxKey);
72,132,355✔
904
      if (code) {
72,125,100✔
UNCOV
905
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
906
        return code;
×
907
      }
908

909
      code = tdbTbInsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn);
72,125,100✔
910
      if (code) {
72,130,923✔
UNCOV
911
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
912
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
UNCOV
913
        return code;
×
914
      }
915
      metaFetchTagIdxKeyFree(&pTagIdxKey);
72,130,923✔
916
    }
917
  }
918
  return code;
72,379,777✔
919
}
920

921
static int32_t metaTagIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
8,456,728✔
922
  int32_t code = TSDB_CODE_SUCCESS;
8,456,728✔
923

924
  const SMetaEntry     *pEntry = pParam->pEntry;
8,456,728✔
925
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
8,456,728✔
926
  const SMetaEntry     *pSuperEntry = pParam->pSuperEntry;
8,456,728✔
927
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
8,456,728✔
928
  const STag           *pNewTags = (const STag *)pEntry->ctbEntry.pTags;
8,456,728✔
929
  const STag           *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags;
8,456,728✔
930

931
  if (pNewTags->len == pOldTags->len && !memcmp(pNewTags, pOldTags, pNewTags->len)) {
8,456,728✔
932
    return code;
198,679✔
933
  }
934

935
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
8,258,049✔
936
    code = metaDelJsonVarFromIdx(pMeta, pOldEntry, &pTagSchema->pSchema[0]);
972✔
937
    if (code) {
972✔
UNCOV
938
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
939
      return code;
×
940
    }
941

942
    code = metaSaveJsonVarToIdx(pMeta, pEntry, &pTagSchema->pSchema[0]);
972✔
943
    if (code) {
972✔
UNCOV
944
      metaErr(TD_VID(pMeta->pVnode), code);
×
945
      return code;
×
946
    }
947
  } else {
948
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
18,022,436✔
949
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
9,765,359✔
950

951
      if (!IS_IDX_ON(pTagColumn)) {
9,765,359✔
952
        continue;
1,509,158✔
953
      }
954

955
      STagIdxKey *pOldTagIdxKey = NULL;
8,256,201✔
956
      int32_t     oldTagIdxKeySize = 0;
8,256,201✔
957
      STagIdxKey *pNewTagIdxKey = NULL;
8,256,201✔
958
      int32_t     newTagIdxKeySize = 0;
8,256,201✔
959

960
      code = metaFetchTagIdxKey(pMeta, pOldEntry, pTagColumn, &pOldTagIdxKey, &oldTagIdxKeySize);
8,256,201✔
961
      if (code) {
8,255,828✔
962
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
963
        return code;
×
964
      }
965

966
      code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pNewTagIdxKey, &newTagIdxKeySize);
8,255,828✔
967
      if (code) {
8,256,201✔
968
        metaErr(TD_VID(pMeta->pVnode), code);
×
969
        metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
970
        return code;
×
971
      }
972

973
      if (tagIdxKeyCmpr(pOldTagIdxKey, oldTagIdxKeySize, pNewTagIdxKey, newTagIdxKeySize)) {
8,256,201✔
974
        code = tdbTbDelete(pMeta->pTagIdx, pOldTagIdxKey, oldTagIdxKeySize, pMeta->txn);
7,847,346✔
975
        if (code) {
7,847,346✔
UNCOV
976
          metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
977
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
UNCOV
978
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
UNCOV
979
          return code;
×
980
        }
981

982
        code = tdbTbInsert(pMeta->pTagIdx, pNewTagIdxKey, newTagIdxKeySize, NULL, 0, pMeta->txn);
7,847,346✔
983
        if (code) {
7,847,346✔
UNCOV
984
          metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
985
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
UNCOV
986
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
UNCOV
987
          return code;
×
988
        }
989
      }
990

991
      metaFetchTagIdxKeyFree(&pOldTagIdxKey);
8,256,201✔
992
      metaFetchTagIdxKeyFree(&pNewTagIdxKey);
8,256,201✔
993
    }
994
  }
995
  return code;
8,258,049✔
996
}
997

998
static int32_t metaTagIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
1,714,562✔
999
  int32_t code = TSDB_CODE_SUCCESS;
1,714,562✔
1000

1001
  const SMetaEntry     *pEntry = pParam->pEntry;
1,714,562✔
1002
  const SMetaEntry     *pChild = pParam->pOldEntry;
1,714,562✔
1003
  const SMetaEntry     *pSuper = pParam->pSuperEntry;
1,714,562✔
1004
  const SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
1,714,562✔
1005
  const SSchema        *pTagColumn = NULL;
1,714,562✔
1006
  const STag           *pTags = (const STag *)pChild->ctbEntry.pTags;
1,714,562✔
1007

1008
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
1,714,562✔
1009
    pTagColumn = &pTagSchema->pSchema[0];
119,690✔
1010
    code = metaDelJsonVarFromIdx(pMeta, pChild, pTagColumn);
119,690✔
1011
    if (code) {
119,690✔
UNCOV
1012
      metaErr(TD_VID(pMeta->pVnode), code);
×
1013
    }
1014
  } else {
1015
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
9,653,355✔
1016
      pTagColumn = &pTagSchema->pSchema[i];
8,058,483✔
1017
      if (!IS_IDX_ON(pTagColumn)) {
8,058,810✔
1018
        continue;
3,316,935✔
1019
      }
1020

1021
      STagIdxKey *pTagIdxKey = NULL;
4,741,875✔
1022
      int32_t     nTagIdxKey;
4,741,647✔
1023

1024
      code = metaFetchTagIdxKey(pMeta, pChild, pTagColumn, &pTagIdxKey, &nTagIdxKey);
4,741,875✔
1025
      if (code) {
4,740,521✔
UNCOV
1026
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1027
        return code;
×
1028
      }
1029

1030
      code = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
4,740,521✔
1031
      if (code) {
4,741,875✔
UNCOV
1032
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1033
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
UNCOV
1034
        return code;
×
1035
      }
1036
      metaFetchTagIdxKeyFree(&pTagIdxKey);
4,741,875✔
1037
    }
1038
  }
1039
  return code;
1,714,562✔
1040
}
1041

1042
// Btime Index
1043
static int32_t metaBtimeIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
82,931,724✔
1044
  int32_t code = TSDB_CODE_SUCCESS;
82,931,724✔
1045

1046
  const SMetaEntry *pEntry;
1047
  if (META_TABLE_OP_DELETE == op) {
82,931,724✔
1048
    pEntry = pParam->pOldEntry;
2,601,339✔
1049
  } else {
1050
    pEntry = pParam->pEntry;
80,330,385✔
1051
  }
1052

1053
  SBtimeIdxKey key = {
82,930,422✔
1054
      .uid = pEntry->uid,
82,932,488✔
1055
  };
1056

1057
  if (TSDB_CHILD_TABLE == pEntry->type || TSDB_VIRTUAL_CHILD_TABLE == pEntry->type) {
82,934,387✔
1058
    key.btime = pEntry->ctbEntry.btime;
74,088,598✔
1059
  } else if (TSDB_NORMAL_TABLE == pEntry->type || TSDB_VIRTUAL_NORMAL_TABLE == pEntry->type) {
8,849,517✔
1060
    key.btime = pEntry->ntbEntry.btime;
8,849,517✔
1061
  } else {
UNCOV
1062
    return TSDB_CODE_INVALID_PARA;
×
1063
  }
1064

1065
  if (META_TABLE_OP_INSERT == op) {
82,931,611✔
1066
    code = tdbTbInsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
80,330,272✔
1067
  } else if (META_TABLE_OP_UPDATA == op) {
2,601,339✔
1068
    code = tdbTbUpsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
×
1069
  } else if (META_TABLE_OP_DELETE == op) {
2,601,339✔
1070
    code = tdbTbDelete(pMeta->pBtimeIdx, &key, sizeof(key), pMeta->txn);
2,601,339✔
1071
  } else {
UNCOV
1072
    code = TSDB_CODE_INVALID_PARA;
×
1073
  }
1074
  if (code) {
82,936,264✔
UNCOV
1075
    metaErr(TD_VID(pMeta->pVnode), code);
×
1076
  }
1077
  return code;
82,933,803✔
1078
}
1079

1080
static int32_t metaBtimeIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
80,332,596✔
1081
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
80,332,596✔
1082
}
1083

UNCOV
1084
static int32_t metaBtimeIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
UNCOV
1085
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
×
1086
}
1087

1088
static int32_t metaBtimeIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
2,601,339✔
1089
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_DELETE);
2,601,339✔
1090
}
1091

1092
// TTL Index
1093
static int32_t metaTtlIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
79,788,782✔
1094
  const SMetaEntry *pEntry = pParam->pEntry;
79,788,782✔
1095

1096
  STtlUpdTtlCtx ctx = {
79,794,704✔
1097
      .uid = pEntry->uid,
79,800,483✔
1098
      .pTxn = pMeta->txn,
79,796,884✔
1099
  };
1100
  if (TSDB_CHILD_TABLE == pEntry->type) {
79,793,230✔
1101
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
72,018,040✔
1102
    ctx.changeTimeMs = pEntry->ctbEntry.btime;
72,029,616✔
1103
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
7,768,284✔
1104
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
7,768,284✔
1105
    ctx.changeTimeMs = pEntry->ntbEntry.btime;
7,768,284✔
1106
  } else {
UNCOV
1107
    return TSDB_CODE_INVALID_PARA;
×
1108
  }
1109

1110
  int32_t ret = ttlMgrInsertTtl(pMeta->pTtlMgr, &ctx);
79,797,165✔
1111
  if (ret < 0) {
79,786,461✔
UNCOV
1112
    metaError("vgId:%d, failed to insert ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid, tstrerror(ret));
×
1113
  }
1114
  return TSDB_CODE_SUCCESS;
79,786,001✔
1115
}
1116

1117
static int32_t metaTtlIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
79,790,908✔
1118
  return metaTtlIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
79,790,908✔
1119
}
1120

1121
static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam);
1122

1123
static int32_t metaTtlIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
11,933,084✔
1124
  int32_t code = TSDB_CODE_SUCCESS;
11,933,084✔
1125

1126
  const SMetaEntry *pEntry = pParam->pEntry;
11,933,084✔
1127
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
11,933,084✔
1128

1129
  if ((pEntry->type == TSDB_CHILD_TABLE && pOldEntry->ctbEntry.ttlDays != pEntry->ctbEntry.ttlDays) ||
11,933,084✔
1130
      (pEntry->type == TSDB_NORMAL_TABLE && pOldEntry->ntbEntry.ttlDays != pEntry->ntbEntry.ttlDays)) {
11,928,289✔
1131
    code = metaTtlIdxDelete(pMeta, pParam);
9,652✔
1132
    if (code) {
9,652✔
UNCOV
1133
      metaErr(TD_VID(pMeta->pVnode), code);
×
1134
    }
1135

1136
    code = metaTtlIdxInsert(pMeta, pParam);
9,652✔
1137
    if (code) {
9,652✔
UNCOV
1138
      metaErr(TD_VID(pMeta->pVnode), code);
×
1139
    }
1140
  }
1141

1142
  return TSDB_CODE_SUCCESS;
11,933,084✔
1143
}
1144

1145
static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
2,538,644✔
1146
  int32_t code = TSDB_CODE_SUCCESS;
2,538,644✔
1147

1148
  const SMetaEntry *pEntry = pParam->pOldEntry;
2,538,644✔
1149
  STtlDelTtlCtx     ctx = {
2,538,644✔
1150
          .uid = pEntry->uid,
2,538,644✔
1151
          .pTxn = pMeta->txn,
2,538,644✔
1152
  };
1153

1154
  if (TSDB_CHILD_TABLE == pEntry->type) {
2,538,644✔
1155
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
1,665,409✔
1156
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
873,235✔
1157
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
851,093✔
1158
  } else {
1159
    code = TSDB_CODE_INVALID_PARA;
22,142✔
1160
  }
1161

1162
  if (TSDB_CODE_SUCCESS == code) {
2,538,644✔
1163
    int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
2,516,502✔
1164
    if (ret < 0) {
2,515,825✔
UNCOV
1165
      metaError("vgId:%d, failed to delete ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid,
×
1166
                tstrerror(ret));
1167
    }
1168
  }
1169
  return code;
2,537,967✔
1170
}
1171

1172
static void metaTimeSeriesNotifyCheck(SMeta *pMeta) {
87,610,553✔
1173
#if defined(TD_ENTERPRISE)
1174
  int64_t nTimeSeries = metaGetTimeSeriesNum(pMeta, 0);
87,610,553✔
1175
  int64_t deltaTS = nTimeSeries - pMeta->pVnode->config.vndStats.numOfReportedTimeSeries;
87,613,358✔
1176
  if (deltaTS > tsTimeSeriesThreshold) {
87,624,848✔
1177
    if (0 == atomic_val_compare_exchange_8(&dmNotifyHdl.state, 1, 2)) {
50,911,435✔
1178
      if (tsem_post(&dmNotifyHdl.sem) != 0) {
50,912,707✔
UNCOV
1179
        metaError("vgId:%d, failed to post semaphore, errno:%d", TD_VID(pMeta->pVnode), ERRNO);
×
1180
      }
1181
    }
1182
  }
1183
#endif
1184
}
87,646,220✔
1185

1186
static int32_t (*metaTableOpFn[META_TABLE_MAX][META_TABLE_OP_MAX])(SMeta *pMeta, const SMetaHandleParam *pParam) =
1187
    {
1188
        [META_ENTRY_TABLE] =
1189
            {
1190
                [META_TABLE_OP_INSERT] = metaEntryTableInsert,
1191
                [META_TABLE_OP_UPDATA] = metaEntryTableUpdate,
1192
                [META_TABLE_OP_DELETE] = metaEntryTableDelete,
1193
            },
1194
        [META_SCHEMA_TABLE] =
1195
            {
1196
                [META_TABLE_OP_INSERT] = metaSchemaTableInsert,
1197
                [META_TABLE_OP_UPDATA] = metaSchemaTableUpdate,
1198
                [META_TABLE_OP_DELETE] = metaSchemaTableDelete,
1199
            },
1200
        [META_UID_IDX] =
1201
            {
1202
                [META_TABLE_OP_INSERT] = metaUidIdxInsert,
1203
                [META_TABLE_OP_UPDATA] = metaUidIdxUpdate,
1204
                [META_TABLE_OP_DELETE] = metaUidIdxDelete,
1205
            },
1206
        [META_NAME_IDX] =
1207
            {
1208
                [META_TABLE_OP_INSERT] = metaNameIdxInsert,
1209
                [META_TABLE_OP_UPDATA] = metaNameIdxUpdate,
1210
                [META_TABLE_OP_DELETE] = metaNameIdxDelete,
1211
            },
1212
        [META_SUID_IDX] =
1213
            {
1214
                [META_TABLE_OP_INSERT] = metaSUidIdxInsert,
1215
                [META_TABLE_OP_UPDATA] = NULL,
1216
                [META_TABLE_OP_DELETE] = metaSUidIdxDelete,
1217
            },
1218
        [META_CHILD_IDX] =
1219
            {
1220
                [META_TABLE_OP_INSERT] = metaChildIdxInsert,
1221
                [META_TABLE_OP_UPDATA] = metaChildIdxUpdate,
1222
                [META_TABLE_OP_DELETE] = metaChildIdxDelete,
1223
            },
1224
        [META_TAG_IDX] =
1225
            {
1226
                [META_TABLE_OP_INSERT] = metaTagIdxInsert,
1227
                [META_TABLE_OP_UPDATA] = metaTagIdxUpdate,
1228
                [META_TABLE_OP_DELETE] = metaTagIdxDelete,
1229
            },
1230
        [META_BTIME_IDX] =
1231
            {
1232
                [META_TABLE_OP_INSERT] = metaBtimeIdxInsert,
1233
                [META_TABLE_OP_UPDATA] = metaBtimeIdxUpdate,
1234
                [META_TABLE_OP_DELETE] = metaBtimeIdxDelete,
1235
            },
1236
        [META_TTL_IDX] =
1237
            {
1238
                [META_TABLE_OP_INSERT] = metaTtlIdxInsert,
1239
                [META_TABLE_OP_UPDATA] = metaTtlIdxUpdate,
1240
                [META_TABLE_OP_DELETE] = metaTtlIdxDelete,
1241
            },
1242
};
1243

1244
static int32_t metaHandleSuperTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
5,569,899✔
1245
  int32_t code = TSDB_CODE_SUCCESS;
5,569,899✔
1246

1247
  SMetaTableOp ops[] = {
5,569,899✔
1248
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1249
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: here should be insert
1250
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1251
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1252
      {META_SUID_IDX, META_TABLE_OP_INSERT},      //
1253
  };
1254

1255
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
33,423,141✔
1256
    SMetaTableOp          *op = &ops[i];
27,840,916✔
1257
    const SMetaHandleParam param = {
27,857,477✔
1258
        .pEntry = pEntry,
1259
    };
1260
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
27,860,715✔
1261
    if (TSDB_CODE_SUCCESS != code) {
27,834,062✔
UNCOV
1262
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1263
      return code;
×
1264
    }
1265
  }
1266

1267
  return code;
5,582,225✔
1268
}
1269
static int32_t metaHandleSuperTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
5,552,706✔
1270
  int32_t code = TSDB_CODE_SUCCESS;
5,552,706✔
1271

1272
  metaWLock(pMeta);
5,552,706✔
1273
  code = metaHandleSuperTableCreateImpl(pMeta, pEntry);
5,576,987✔
1274
  metaULock(pMeta);
5,570,302✔
1275

1276
  if (TSDB_CODE_SUCCESS == code) {
5,569,086✔
1277
    pMeta->pVnode->config.vndStats.numOfSTables++;
5,569,086✔
1278

1279
    metaInfo("vgId:%d, %s success, version:%" PRId64 " type:%d uid:%" PRId64 " name:%s", TD_VID(pMeta->pVnode),
5,577,288✔
1280
             __func__, pEntry->version, pEntry->type, pEntry->uid, pEntry->name);
1281
  } else {
UNCOV
1282
    metaErr(TD_VID(pMeta->pVnode), code);
×
1283
  }
1284
  return code;
5,580,180✔
1285
}
1286

1287
static int32_t metaHandleNormalTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
7,763,427✔
1288
  int32_t code = TSDB_CODE_SUCCESS;
7,763,427✔
1289

1290
  SMetaTableOp ops[] = {
7,763,427✔
1291
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1292
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: need to be insert
1293
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1294
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1295
      {META_BTIME_IDX, META_TABLE_OP_INSERT},     //
1296
      {META_TTL_IDX, META_TABLE_OP_INSERT},       //
1297
  };
1298

1299
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
54,343,989✔
1300
    SMetaTableOp *op = &ops[i];
46,580,562✔
1301

1302
    SMetaHandleParam param = {
46,580,562✔
1303
        .pEntry = pEntry,
1304
    };
1305

1306
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
46,580,562✔
1307
    if (TSDB_CODE_SUCCESS != code) {
46,580,158✔
UNCOV
1308
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1309
      return code;
×
1310
    }
1311
  }
1312

1313
  return code;
7,763,427✔
1314
}
1315
static int32_t metaHandleNormalTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
7,763,427✔
1316
  int32_t code = TSDB_CODE_SUCCESS;
7,763,427✔
1317

1318
  // update TDB
1319
  metaWLock(pMeta);
7,763,427✔
1320
  code = metaHandleNormalTableCreateImpl(pMeta, pEntry);
7,763,427✔
1321
  metaULock(pMeta);
7,763,427✔
1322

1323
  // update other stuff
1324
  if (TSDB_CODE_SUCCESS == code) {
7,763,427✔
1325
    pMeta->pVnode->config.vndStats.numOfNTables++;
7,763,427✔
1326
    pMeta->pVnode->config.vndStats.numOfNTimeSeries += pEntry->ntbEntry.schemaRow.nCols - 1;
7,763,427✔
1327

1328
    if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
7,763,427✔
1329
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, -1, &pEntry->ntbEntry.schemaRow);
17,495✔
1330
      if (rc < 0) {
17,495✔
UNCOV
1331
        metaError("vgId:%d, failed to create table:%s since %s", TD_VID(pMeta->pVnode), pEntry->name, tstrerror(rc));
×
1332
      }
1333
    }
1334
    metaTimeSeriesNotifyCheck(pMeta);
7,763,427✔
1335
  } else {
UNCOV
1336
    metaErr(TD_VID(pMeta->pVnode), code);
×
1337
  }
1338
  return code;
7,763,427✔
1339
}
1340

1341
static int32_t metaHandleChildTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry, const SMetaEntry *pSuperEntry) {
72,024,661✔
1342
  int32_t code = TSDB_CODE_SUCCESS;
72,024,661✔
1343

1344
  SMetaTableOp ops[] = {
72,024,661✔
1345
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},  //
1346
      {META_UID_IDX, META_TABLE_OP_INSERT},      //
1347
      {META_NAME_IDX, META_TABLE_OP_INSERT},     //
1348
      {META_CHILD_IDX, META_TABLE_OP_INSERT},    //
1349
      {META_TAG_IDX, META_TABLE_OP_INSERT},      //
1350
      {META_BTIME_IDX, META_TABLE_OP_INSERT},    //
1351
      {META_TTL_IDX, META_TABLE_OP_INSERT},      //
1352
  };
1353

1354
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
576,157,965✔
1355
    SMetaTableOp *op = &ops[i];
504,133,673✔
1356

1357
    SMetaHandleParam param = {
504,138,200✔
1358
        .pEntry = pEntry,
1359
        .pSuperEntry = pSuperEntry,
1360
    };
1361

1362
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
504,141,781✔
1363
    if (TSDB_CODE_SUCCESS != code) {
504,112,295✔
UNCOV
1364
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1365
      return code;
×
1366
    }
1367
  }
1368

1369
  if (TSDB_CODE_SUCCESS == code) {
72,024,292✔
1370
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
72,021,406✔
1371
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
72,015,253✔
1372
    if (ret < 0) {
72,016,033✔
UNCOV
1373
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1374
    }
1375

1376
    ret = metaStableTagFilterCacheUpdateUid(
72,016,033✔
1377
      pMeta, pEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_ADD_TABLE);
1378
    if (ret < 0) {
72,011,049✔
UNCOV
1379
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1380
    }
1381

1382
    ret = metaTbGroupCacheClear(pMeta, pSuperEntry->uid);
72,011,049✔
1383
    if (ret < 0) {
72,016,404✔
UNCOV
1384
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1385
    }
1386
  }
1387
  return code;
72,014,031✔
1388
}
1389

1390
static int32_t metaHandleChildTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
72,017,862✔
1391
  int32_t     code = TSDB_CODE_SUCCESS;
72,017,862✔
1392
  SMetaEntry *pSuperEntry = NULL;
72,017,862✔
1393

1394
  // get the super table entry
1395
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
72,024,966✔
1396
  if (code) {
72,025,655✔
1397
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1398
    return code;
×
1399
  }
1400

1401
  // update TDB
1402
  metaWLock(pMeta);
72,025,655✔
1403
  code = metaHandleChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
72,024,991✔
1404
  metaULock(pMeta);
72,013,057✔
1405

1406
  // update other stuff
1407
  if (TSDB_CODE_SUCCESS == code) {
72,022,891✔
1408
    pMeta->pVnode->config.vndStats.numOfCTables++;
72,022,891✔
1409

1410
    if (!metaTbInFilterCache(pMeta, pSuperEntry->name, 1)) {
72,025,859✔
1411
      int32_t nCols = 0;
72,019,119✔
1412
      int32_t ret = metaGetStbStats(pMeta->pVnode, pSuperEntry->uid, 0, &nCols, 0);
72,021,995✔
1413
      if (ret < 0) {
72,017,876✔
UNCOV
1414
        metaErr(TD_VID(pMeta->pVnode), ret);
×
1415
      }
1416
      pMeta->pVnode->config.vndStats.numOfTimeSeries += (nCols > 0 ? nCols - 1 : 0);
72,017,876✔
1417
    }
1418

1419
    if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
72,027,436✔
1420
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, pEntry->ctbEntry.suid, NULL);
8,425,200✔
1421
      if (rc < 0) {
8,425,200✔
UNCOV
1422
        metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
1423
                  tstrerror(rc));
1424
      }
1425
    }
1426

1427
  } else {
UNCOV
1428
    metaErr(TD_VID(pMeta->pVnode), code);
×
1429
  }
1430
  metaTimeSeriesNotifyCheck(pMeta);
72,014,336✔
1431
  metaFetchEntryFree(&pSuperEntry);
72,027,963✔
1432
  return code;
72,026,351✔
1433
}
1434

1435
static int32_t metaHandleVirtualNormalTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
199,717✔
1436
  int32_t code = TSDB_CODE_SUCCESS;
199,717✔
1437

1438
  SMetaTableOp ops[] = {
199,717✔
1439
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1440
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: need to be insert
1441
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1442
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1443
      {META_BTIME_IDX, META_TABLE_OP_INSERT},     //
1444
  };
1445

1446
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1,198,302✔
1447
    SMetaTableOp *op = &ops[i];
998,585✔
1448

1449
    SMetaHandleParam param = {
998,585✔
1450
        .pEntry = pEntry,
1451
    };
1452

1453
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
998,585✔
1454
    if (TSDB_CODE_SUCCESS != code) {
998,585✔
UNCOV
1455
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1456
      return code;
×
1457
    }
1458
  }
1459

1460
  return code;
199,717✔
1461
}
1462

1463
static int32_t metaHandleVirtualNormalTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
199,717✔
1464
  int32_t code = TSDB_CODE_SUCCESS;
199,717✔
1465

1466
  // update TDB
1467
  metaWLock(pMeta);
199,717✔
1468
  code = metaHandleVirtualNormalTableCreateImpl(pMeta, pEntry);
199,717✔
1469
  metaULock(pMeta);
199,717✔
1470

1471
  // update other stuff
1472
  if (TSDB_CODE_SUCCESS == code) {
199,717✔
1473
    pMeta->pVnode->config.vndStats.numOfVTables++;
199,717✔
1474
  } else {
UNCOV
1475
    metaErr(TD_VID(pMeta->pVnode), code);
×
1476
  }
1477
  return code;
199,717✔
1478
}
1479

1480
static int32_t metaHandleVirtualChildTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry,
352,841✔
1481
                                                     const SMetaEntry *pSuperEntry) {
1482
  int32_t code = TSDB_CODE_SUCCESS;
352,841✔
1483

1484
  SMetaTableOp ops[] = {
352,841✔
1485
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},  //
1486
      {META_UID_IDX, META_TABLE_OP_INSERT},      //
1487
      {META_NAME_IDX, META_TABLE_OP_INSERT},     //
1488
      {META_CHILD_IDX, META_TABLE_OP_INSERT},    //
1489
      {META_TAG_IDX, META_TABLE_OP_INSERT},      //
1490
      {META_BTIME_IDX, META_TABLE_OP_INSERT},    //
1491
  };
1492

1493
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
2,469,887✔
1494
    SMetaTableOp *op = &ops[i];
2,117,046✔
1495

1496
    SMetaHandleParam param = {
2,117,046✔
1497
        .pEntry = pEntry,
1498
        .pSuperEntry = pSuperEntry,
1499
    };
1500

1501
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
2,117,046✔
1502
    if (TSDB_CODE_SUCCESS != code) {
2,117,046✔
UNCOV
1503
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1504
      return code;
×
1505
    }
1506
  }
1507

1508
  if (TSDB_CODE_SUCCESS == code) {
352,841✔
1509
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
352,841✔
1510
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
352,841✔
1511
    if (ret < 0) {
352,841✔
UNCOV
1512
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1513
    }
1514

1515
    ret = metaStableTagFilterCacheUpdateUid(
352,841✔
1516
      pMeta, pEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_ADD_TABLE);
1517
    if (ret < 0) {
352,841✔
UNCOV
1518
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1519
    }
1520

1521
    ret = metaTbGroupCacheClear(pMeta, pSuperEntry->uid);
352,841✔
1522
    if (ret < 0) {
352,841✔
UNCOV
1523
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1524
    }
1525

1526
    ret = metaRefDbsCacheClear(pMeta, pSuperEntry->uid);
352,841✔
1527
    if (ret < 0) {
352,841✔
UNCOV
1528
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1529
    }
1530
  }
1531

1532
  return code;
352,841✔
1533
}
1534

1535
static int32_t metaHandleVirtualChildTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
352,841✔
1536
  int32_t     code = TSDB_CODE_SUCCESS;
352,841✔
1537
  SMetaEntry *pSuperEntry = NULL;
352,841✔
1538

1539
  // get the super table entry
1540
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
352,841✔
1541
  if (code) {
352,841✔
UNCOV
1542
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1543
    return code;
×
1544
  }
1545

1546
  // update TDB
1547
  metaWLock(pMeta);
352,841✔
1548
  code = metaHandleVirtualChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
352,841✔
1549
  metaULock(pMeta);
352,841✔
1550

1551
  // update other stuff
1552
  if (TSDB_CODE_SUCCESS == code) {
352,841✔
1553
    pMeta->pVnode->config.vndStats.numOfVCTables++;
352,841✔
1554
  } else {
UNCOV
1555
    metaErr(TD_VID(pMeta->pVnode), code);
×
1556
  }
1557

1558
  metaFetchEntryFree(&pSuperEntry);
352,841✔
1559
  return code;
352,841✔
1560
}
1561

1562
static int32_t metaHandleNormalTableDropImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
846,236✔
1563
  int32_t code = TSDB_CODE_SUCCESS;
846,236✔
1564

1565
  SMetaTableOp ops[] = {
846,236✔
1566
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1567
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1568
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1569
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1570
      {META_TTL_IDX, META_TABLE_OP_DELETE},      //
1571

1572
      // {META_SCHEMA_TABLE, META_TABLE_OP_DELETE},  //
1573
  };
1574

1575
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
5,077,416✔
1576
    SMetaTableOp *op = &ops[i];
4,231,180✔
1577
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
4,231,180✔
1578
    if (code) {
4,231,180✔
UNCOV
1579
      const SMetaEntry *pEntry = pParam->pEntry;
×
UNCOV
1580
      metaErr(TD_VID(pMeta->pVnode), code);
×
1581
    }
1582
  }
1583

1584
  return code;
846,236✔
1585
}
1586

1587
static int32_t metaHandleNormalTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
846,236✔
1588
  int32_t     code = TSDB_CODE_SUCCESS;
846,236✔
1589
  SMetaEntry *pOldEntry = NULL;
846,236✔
1590

1591
  // fetch the entry
1592
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
846,236✔
1593
  if (code) {
846,236✔
UNCOV
1594
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1595
    return code;
×
1596
  }
1597

1598
  SMetaHandleParam param = {
846,236✔
1599
      .pEntry = pEntry,
1600
      .pOldEntry = pOldEntry,
1601
  };
1602

1603
  // do the drop
1604
  metaWLock(pMeta);
846,236✔
1605
  code = metaHandleNormalTableDropImpl(pMeta, &param);
846,236✔
1606
  metaULock(pMeta);
846,236✔
1607
  if (code) {
846,236✔
UNCOV
1608
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1609
    metaFetchEntryFree(&pOldEntry);
×
1610
    return code;
×
1611
  }
1612

1613
  // update other stuff
1614
  pMeta->pVnode->config.vndStats.numOfNTables--;
846,236✔
1615
  pMeta->pVnode->config.vndStats.numOfNTimeSeries -= (pOldEntry->ntbEntry.schemaRow.nCols - 1);
846,236✔
1616

1617
#if 0
1618
  if (tbUids) {
1619
    if (taosArrayPush(tbUids, &uid) == NULL) {
1620
      rc = terrno;
1621
      goto _exit;
1622
    }
1623
  }
1624
#endif
1625

1626
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
846,236✔
UNCOV
1627
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pOldEntry->uid, 0, NULL);
×
UNCOV
1628
    if (ret < 0) {
×
UNCOV
1629
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1630
    }
1631
  }
1632

1633
  metaFetchEntryFree(&pOldEntry);
846,236✔
1634
  return code;
846,236✔
1635
}
1636

1637
static int32_t metaHandleChildTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam, bool superDropped) {
1,682,756✔
1638
  int32_t code = TSDB_CODE_SUCCESS;
1,682,756✔
1639

1640
  const SMetaEntry *pEntry = pParam->pEntry;
1,682,756✔
1641
  const SMetaEntry *pChild = pParam->pOldEntry;
1,682,756✔
1642
  const SMetaEntry *pSuper = pParam->pSuperEntry;
1,682,756✔
1643

1644
  SMetaTableOp ops[] = {
1,682,756✔
1645
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1646
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1647
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1648
      {META_CHILD_IDX, META_TABLE_OP_DELETE},    //
1649
      {META_TAG_IDX, META_TABLE_OP_DELETE},      //
1650
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1651
      {META_TTL_IDX, META_TABLE_OP_DELETE},      //
1652
  };
1653

1654
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
13,438,882✔
1655
    SMetaTableOp *op = &ops[i];
11,778,268✔
1656

1657
    if (op->table == META_ENTRY_TABLE && superDropped) {
11,777,582✔
1658
      continue;
844,704✔
1659
    }
1660

1661
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
10,932,878✔
1662
    if (code) {
10,933,352✔
1663
      metaErr(TD_VID(pMeta->pVnode), code);
22,142✔
1664
      return code;
22,142✔
1665
    }
1666
  }
1667

1668
  --pMeta->pVnode->config.vndStats.numOfCTables;
1,660,614✔
1669
  metaUpdateStbStats(pMeta, pParam->pSuperEntry->uid, -1, 0, -1);
1,659,937✔
1670
  int32_t ret = metaUidCacheClear(pMeta, pSuper->uid);
1,660,614✔
1671
  if (ret < 0) {
1,660,614✔
UNCOV
1672
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1673
  }
1674

1675
  ret = metaStableTagFilterCacheUpdateUid(
1,660,614✔
1676
    pMeta, pChild, pSuper, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
1677
  if (ret < 0) {
1,659,590✔
UNCOV
1678
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1679
  }
1680

1681
  ret = metaTbGroupCacheClear(pMeta, pSuper->uid);
1,659,590✔
1682
  if (ret < 0) {
1,660,614✔
UNCOV
1683
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1684
  }
1685
  return code;
1,660,614✔
1686
}
1687

1688
static int32_t metaHandleChildTableDrop(SMeta *pMeta, const SMetaEntry *pEntry, bool superDropped) {
1,682,756✔
1689
  int32_t     code = TSDB_CODE_SUCCESS;
1,682,756✔
1690
  SMetaEntry *pChild = NULL;
1,682,756✔
1691
  SMetaEntry *pSuper = NULL;
1,682,756✔
1692

1693
  // fetch old entry
1694
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pChild);
1,682,756✔
1695
  if (code) {
1,682,756✔
UNCOV
1696
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1697
    return code;
×
1698
  }
1699

1700
  // fetch super entry
1701
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
1,682,756✔
1702
  if (code) {
1,682,756✔
UNCOV
1703
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1704
    metaFetchEntryFree(&pChild);
×
UNCOV
1705
    return code;
×
1706
  }
1707

1708
  SMetaHandleParam param = {
1,682,756✔
1709
      .pEntry = pEntry,
1710
      .pOldEntry = pChild,
1711
      .pSuperEntry = pSuper,
1712
  };
1713

1714
  // do the drop
1715
  metaWLock(pMeta);
1,682,756✔
1716
  code = metaHandleChildTableDropImpl(pMeta, &param, superDropped);
1,682,756✔
1717
  metaULock(pMeta);
1,682,756✔
1718
  if (code) {
1,682,079✔
1719
    metaErr(TD_VID(pMeta->pVnode), code);
22,142✔
1720
    metaFetchEntryFree(&pChild);
22,142✔
1721
    metaFetchEntryFree(&pSuper);
22,142✔
1722
    return code;
22,142✔
1723
  }
1724

1725
  // do other stuff
1726
  if (!metaTbInFilterCache(pMeta, pSuper->name, 1)) {
1,659,937✔
1727
    int32_t      nCols = 0;
1,659,937✔
1728
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
1,659,937✔
1729
    if (metaGetStbStats(pMeta->pVnode, pSuper->uid, NULL, &nCols, 0) == 0) {
1,659,937✔
1730
      pStats->numOfTimeSeries -= nCols - 1;
1,660,614✔
1731
    }
1732
  }
1733

1734
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
1,660,614✔
1735
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pChild->uid, pSuper->uid, NULL);
8,808✔
1736
    if (ret < 0) {
8,808✔
UNCOV
1737
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1738
    }
1739
  }
1740

1741
#if 0
1742
  if (tbUids) {
1743
    if (taosArrayPush(tbUids, &uid) == NULL) {
1744
      rc = terrno;
1745
      goto _exit;
1746
    }
1747
  }
1748

1749
  if ((type == TSDB_CHILD_TABLE) && tbUid) {
1750
    *tbUid = uid;
1751
  }
1752
#endif
1753
  metaFetchEntryFree(&pChild);
1,660,614✔
1754
  metaFetchEntryFree(&pSuper);
1,660,614✔
1755
  return code;
1,659,937✔
1756
}
1757

1758
static int32_t metaHandleVirtualNormalTableDropImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
40,541✔
1759
  int32_t code = TSDB_CODE_SUCCESS;
40,541✔
1760

1761
  SMetaTableOp ops[] = {
40,541✔
1762
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1763
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1764
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1765
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1766

1767
      // {META_SCHEMA_TABLE, META_TABLE_OP_DELETE},  //
1768
  };
1769

1770
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
202,705✔
1771
    SMetaTableOp *op = &ops[i];
162,164✔
1772
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
162,164✔
1773
    if (code) {
162,164✔
UNCOV
1774
      const SMetaEntry *pEntry = pParam->pEntry;
×
UNCOV
1775
      metaErr(TD_VID(pMeta->pVnode), code);
×
1776
    }
1777
  }
1778

1779
  return code;
40,541✔
1780
}
1781

1782
static int32_t metaHandleVirtualNormalTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
40,541✔
1783
  int32_t     code = TSDB_CODE_SUCCESS;
40,541✔
1784
  SMetaEntry *pOldEntry = NULL;
40,541✔
1785

1786
  // fetch the entry
1787
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
40,541✔
1788
  if (code) {
40,541✔
UNCOV
1789
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1790
    return code;
×
1791
  }
1792

1793
  SMetaHandleParam param = {
40,541✔
1794
      .pEntry = pEntry,
1795
      .pOldEntry = pOldEntry,
1796
  };
1797

1798
  // do the drop
1799
  metaWLock(pMeta);
40,541✔
1800
  code = metaHandleVirtualNormalTableDropImpl(pMeta, &param);
40,541✔
1801
  metaULock(pMeta);
40,541✔
1802
  if (code) {
40,541✔
UNCOV
1803
    metaErr(TD_VID(pMeta->pVnode), code);
×
1804
    metaFetchEntryFree(&pOldEntry);
×
1805
    return code;
×
1806
  }
1807

1808
  // update other stuff
1809
  pMeta->pVnode->config.vndStats.numOfVTables--;
40,541✔
1810

1811
#if 0
1812
  if (tbUids) {
1813
    if (taosArrayPush(tbUids, &uid) == NULL) {
1814
      rc = terrno;
1815
      goto _exit;
1816
    }
1817
  }
1818
#endif
1819

1820
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
40,541✔
UNCOV
1821
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pOldEntry->uid, 0, NULL);
×
UNCOV
1822
    if (ret < 0) {
×
UNCOV
1823
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1824
    }
1825
  }
1826

1827
  metaFetchEntryFree(&pOldEntry);
40,541✔
1828
  return code;
40,541✔
1829
}
1830

1831
static int32_t metaHandleVirtualChildTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam, bool superDropped) {
31,806✔
1832
  int32_t code = TSDB_CODE_SUCCESS;
31,806✔
1833

1834
  const SMetaEntry *pEntry = pParam->pEntry;
31,806✔
1835
  const SMetaEntry *pChild = pParam->pOldEntry;
31,806✔
1836
  const SMetaEntry *pSuper = pParam->pSuperEntry;
31,806✔
1837

1838
  SMetaTableOp ops[] = {
31,806✔
1839
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1840
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1841
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1842
      {META_CHILD_IDX, META_TABLE_OP_DELETE},    //
1843
      {META_TAG_IDX, META_TABLE_OP_DELETE},      //
1844
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1845
  };
1846

1847
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
222,642✔
1848
    SMetaTableOp *op = &ops[i];
190,836✔
1849

1850
    if (op->table == META_ENTRY_TABLE && superDropped) {
190,836✔
UNCOV
1851
      continue;
×
1852
    }
1853

1854
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
190,836✔
1855
    if (code) {
190,836✔
UNCOV
1856
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1857
      return code;
×
1858
    }
1859
  }
1860

1861
  --pMeta->pVnode->config.vndStats.numOfVCTables;
31,806✔
1862
  metaUpdateStbStats(pMeta, pParam->pSuperEntry->uid, -1, 0, -1);
31,806✔
1863
  int32_t ret = metaUidCacheClear(pMeta, pSuper->uid);
31,806✔
1864
  if (ret < 0) {
31,806✔
UNCOV
1865
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1866
  }
1867

1868
  ret = metaStableTagFilterCacheUpdateUid(
31,806✔
1869
    pMeta, pChild, pSuper, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
1870
  if (ret < 0) {
31,806✔
UNCOV
1871
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1872
  }
1873

1874
  ret = metaTbGroupCacheClear(pMeta, pSuper->uid);
31,806✔
1875
  if (ret < 0) {
31,806✔
UNCOV
1876
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1877
  }
1878

1879
  ret = metaRefDbsCacheClear(pMeta, pSuper->uid);
31,806✔
1880
  if (ret < 0) {
31,806✔
UNCOV
1881
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1882
  }
1883

1884
  return code;
31,806✔
1885
}
1886

1887
static int32_t metaHandleVirtualChildTableDrop(SMeta *pMeta, const SMetaEntry *pEntry, bool superDropped) {
31,806✔
1888
  int32_t     code = TSDB_CODE_SUCCESS;
31,806✔
1889
  SMetaEntry *pChild = NULL;
31,806✔
1890
  SMetaEntry *pSuper = NULL;
31,806✔
1891

1892
  // fetch old entry
1893
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pChild);
31,806✔
1894
  if (code) {
31,806✔
UNCOV
1895
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1896
    return code;
×
1897
  }
1898

1899
  // fetch super entry
1900
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
31,806✔
1901
  if (code) {
31,806✔
1902
    metaErr(TD_VID(pMeta->pVnode), code);
×
1903
    metaFetchEntryFree(&pChild);
×
1904
    return code;
×
1905
  }
1906

1907
  SMetaHandleParam param = {
31,806✔
1908
      .pEntry = pEntry,
1909
      .pOldEntry = pChild,
1910
      .pSuperEntry = pSuper,
1911
  };
1912

1913
  // do the drop
1914
  metaWLock(pMeta);
31,806✔
1915
  code = metaHandleVirtualChildTableDropImpl(pMeta, &param, superDropped);
31,806✔
1916
  metaULock(pMeta);
31,806✔
1917
  if (code) {
31,806✔
UNCOV
1918
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1919
    metaFetchEntryFree(&pChild);
×
1920
    metaFetchEntryFree(&pSuper);
×
UNCOV
1921
    return code;
×
1922
  }
1923

1924
  metaFetchEntryFree(&pChild);
31,806✔
1925
  metaFetchEntryFree(&pSuper);
31,806✔
1926
  return code;
31,806✔
1927
}
1928

1929
int32_t metaGetChildUidsOfSuperTable(SMeta *pMeta, tb_uid_t suid, SArray **childList) {
1,066,367✔
1930
  int32_t code = TSDB_CODE_SUCCESS;
1,066,367✔
1931
  void   *key = NULL;
1,066,367✔
1932
  int32_t keySize = 0;
1,065,781✔
1933
  int32_t c;
1,065,754✔
1934

1935
  *childList = taosArrayInit(64, sizeof(tb_uid_t));
1,067,742✔
1936
  if (*childList == NULL) {
1,065,792✔
UNCOV
1937
    return terrno;
×
1938
  }
1939

1940
  TBC *cursor = NULL;
1,066,469✔
1941
  code = tdbTbcOpen(pMeta->pCtbIdx, &cursor, NULL);
1,067,823✔
1942
  if (code) {
1,066,459✔
UNCOV
1943
    taosArrayDestroy(*childList);
×
UNCOV
1944
    *childList = NULL;
×
UNCOV
1945
    return code;
×
1946
  }
1947

1948
  int32_t rc = tdbTbcMoveTo(cursor,
1,067,671✔
1949
                            &(SCtbIdxKey){
1,066,459✔
1950
                                .suid = suid,
1951
                                .uid = INT64_MIN,
1952
                            },
1953
                            sizeof(SCtbIdxKey), &c);
1954
  if (rc < 0) {
1,068,954✔
1955
    tdbTbcClose(cursor);
×
1956
    return 0;
×
1957
  }
1958

1959
  for (;;) {
1960
    if (tdbTbcNext(cursor, &key, &keySize, NULL, NULL) < 0) {
9,711,657✔
1961
      break;
646,487✔
1962
    }
1963

1964
    if (((SCtbIdxKey *)key)->suid < suid) {
9,065,517✔
1965
      continue;
115,251✔
1966
    } else if (((SCtbIdxKey *)key)->suid > suid) {
8,949,660✔
1967
      break;
422,467✔
1968
    }
1969

1970
    if (taosArrayPush(*childList, &(((SCtbIdxKey *)key)->uid)) == NULL) {
17,055,251✔
UNCOV
1971
      tdbFreeClear(key);
×
UNCOV
1972
      tdbTbcClose(cursor);
×
UNCOV
1973
      taosArrayDestroy(*childList);
×
UNCOV
1974
      *childList = NULL;
×
UNCOV
1975
      return terrno;
×
1976
    }
1977
  }
1978

1979
  tdbTbcClose(cursor);
1,068,954✔
1980
  tdbFreeClear(key);
1,068,348✔
1981
  return code;
1,068,348✔
1982
}
1983

1984
static int32_t metaHandleSuperTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
961,507✔
1985
  int32_t           code = TSDB_CODE_SUCCESS;
961,507✔
1986
  const SMetaEntry *pEntry = pParam->pEntry;
961,507✔
1987

1988
  SMetaTableOp ops[] = {
961,507✔
1989
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1990
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1991
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1992
      {META_SUID_IDX, META_TABLE_OP_DELETE},     //
1993

1994
      // {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: here should be insert
1995
  };
1996

1997
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
4,808,919✔
1998
    SMetaTableOp *op = &ops[i];
3,847,412✔
1999

2000
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
3,848,099✔
2001
    if (TSDB_CODE_SUCCESS != code) {
3,847,412✔
UNCOV
2002
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2003
      return code;
×
2004
    }
2005
  }
2006

2007
  int32_t ret = metaStatsCacheDrop(pMeta, pEntry->uid);
961,507✔
2008
  if (ret < 0) {
962,194✔
2009
    metaErr(TD_VID(pMeta->pVnode), ret);
334,167✔
2010
  }
2011
  return code;
962,194✔
2012
}
2013

2014
static int32_t metaHandleNormalTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
3,711,414✔
2015
  int32_t code = TSDB_CODE_SUCCESS;
3,711,414✔
2016

2017
  const SMetaEntry *pEntry = pParam->pEntry;
3,711,414✔
2018

2019
  SMetaTableOp ops[] = {
3,711,414✔
2020
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
2021
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
2022
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
2023
      {META_TTL_IDX, META_TABLE_OP_UPDATA},       //
2024
  };
2025
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
18,557,070✔
2026
    SMetaTableOp *op = &ops[i];
14,845,656✔
2027
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
14,845,656✔
2028
    if (code) {
14,845,656✔
UNCOV
2029
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2030
      return code;
×
2031
    }
2032
  }
2033
#if 0
2034
  if (metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs) < 0) {
2035
    metaError("vgId:%d, failed to update change time:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
2036
  }
2037
#endif
2038
  return code;
3,711,414✔
2039
}
2040

2041
static int32_t metaHandleVirtualNormalTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
229,894✔
2042
  int32_t code = TSDB_CODE_SUCCESS;
229,894✔
2043

2044
  const SMetaEntry *pEntry = pParam->pEntry;
229,894✔
2045

2046
  SMetaTableOp ops[] = {
229,894✔
2047
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
2048
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
2049
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
2050
  };
2051
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
919,576✔
2052
    SMetaTableOp *op = &ops[i];
689,682✔
2053
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
689,682✔
2054
    if (code) {
689,682✔
UNCOV
2055
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2056
      return code;
×
2057
    }
2058
  }
2059
#if 0
2060
  if (metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs) < 0) {
2061
    metaError("vgId:%d, failed to update change time:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
2062
  }
2063
#endif
2064
  return code;
229,894✔
2065
}
2066

2067
static int32_t metaHandleVirtualChildTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
235,058✔
2068
  int32_t code = TSDB_CODE_SUCCESS;
235,058✔
2069

2070
  const SMetaEntry *pEntry = pParam->pEntry;
235,058✔
2071
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
235,058✔
2072
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
235,058✔
2073

2074
  SMetaTableOp ops[] = {
235,058✔
2075
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},  //
2076
      {META_UID_IDX, META_TABLE_OP_UPDATA},      //
2077
      {META_TAG_IDX, META_TABLE_OP_UPDATA},      //
2078
      {META_CHILD_IDX, META_TABLE_OP_UPDATA},    //
2079
  };
2080

2081
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1,175,290✔
2082
    SMetaTableOp *op = &ops[i];
940,232✔
2083
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
940,232✔
2084
    if (code) {
940,232✔
UNCOV
2085
      metaErr(TD_VID(pMeta->pVnode), code);
×
2086
      return code;
×
2087
    }
2088
  }
2089

2090
  if (metaUidCacheClear(pMeta, pSuperEntry->uid) < 0) {
235,058✔
UNCOV
2091
    metaErr(TD_VID(pMeta->pVnode), code);
×
2092
  }
2093

2094
  // update stable tag filter cache: drop old then add new
2095
  code = metaStableTagFilterCacheUpdateUid(
235,058✔
2096
    pMeta, pOldEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
2097
  if (TSDB_CODE_SUCCESS != code) {
235,058✔
UNCOV
2098
    metaErr(TD_VID(pMeta->pVnode), code);
×
2099
  }
2100
  code = metaStableTagFilterCacheUpdateUid(
235,058✔
2101
    pMeta, pEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_ADD_TABLE);
2102
  if (TSDB_CODE_SUCCESS != code) {
235,058✔
UNCOV
2103
    metaErr(TD_VID(pMeta->pVnode), code);
×
2104
  }
2105

2106
  if (metaTbGroupCacheClear(pMeta, pSuperEntry->uid) < 0) {
235,058✔
UNCOV
2107
    metaErr(TD_VID(pMeta->pVnode), code);
×
2108
  }
2109

2110
  if (metaRefDbsCacheClear(pMeta, pSuperEntry->uid) < 0) {
235,058✔
UNCOV
2111
    metaErr(TD_VID(pMeta->pVnode), code);
×
2112
  }
2113
  return code;
235,058✔
2114
}
2115

2116
static int32_t metaHandleChildTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
8,221,670✔
2117
  int32_t code = TSDB_CODE_SUCCESS;
8,221,670✔
2118

2119
  const SMetaEntry *pEntry = pParam->pEntry;
8,221,670✔
2120
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
8,221,670✔
2121
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
8,221,670✔
2122

2123
  SMetaTableOp ops[] = {
8,221,670✔
2124
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},  //
2125
      {META_UID_IDX, META_TABLE_OP_UPDATA},      //
2126
      {META_TAG_IDX, META_TABLE_OP_UPDATA},      //
2127
      {META_CHILD_IDX, META_TABLE_OP_UPDATA},    //
2128
      {META_TTL_IDX, META_TABLE_OP_UPDATA},      //
2129
  };
2130

2131
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
49,330,020✔
2132
    SMetaTableOp *op = &ops[i];
41,108,350✔
2133
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
41,108,350✔
2134
    if (code) {
41,108,350✔
UNCOV
2135
      metaErr(TD_VID(pMeta->pVnode), code);
×
2136
      return code;
×
2137
    }
2138
  }
2139

2140
  if (metaUidCacheClear(pMeta, pSuperEntry->uid) < 0) {
8,221,670✔
UNCOV
2141
    metaErr(TD_VID(pMeta->pVnode), code);
×
2142
  }
2143

2144
  // update stable tag filter cache: drop old then add new
2145
  code = metaStableTagFilterCacheUpdateUid(
8,221,670✔
2146
    pMeta, pOldEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
2147
  if (TSDB_CODE_SUCCESS != code) {
8,221,670✔
UNCOV
2148
    metaErr(TD_VID(pMeta->pVnode), code);
×
2149
  }
2150
  code = metaStableTagFilterCacheUpdateUid(
8,221,670✔
2151
    pMeta, pEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_ADD_TABLE);
2152
  if (TSDB_CODE_SUCCESS != code) {
8,221,670✔
UNCOV
2153
    metaErr(TD_VID(pMeta->pVnode), code);
×
2154
  }
2155

2156
  if (metaTbGroupCacheClear(pMeta, pSuperEntry->uid) < 0) {
8,221,670✔
UNCOV
2157
    metaErr(TD_VID(pMeta->pVnode), code);
×
2158
  }
2159
  return code;
8,221,670✔
2160
#if 0
2161
  if (metaUpdateChangeTime(pMeta, ctbEntry.uid, pReq->ctimeMs) < 0) {
2162
    metaError("meta/table: failed to update change time:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
2163
  }
2164
#endif
2165
}
2166

2167
static int32_t metaHandleSuperTableUpdateImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
8,150,292✔
2168
  int32_t code = TSDB_CODE_SUCCESS;
8,150,292✔
2169

2170
  const SMetaEntry *pEntry = pParam->pEntry;
8,150,292✔
2171
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
8,151,904✔
2172

2173
  SMetaTableOp ops[] = {
8,152,263✔
2174
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
2175
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
2176
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
2177
  };
2178

2179
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
32,599,843✔
2180
    SMetaTableOp *op = &ops[i];
24,450,036✔
2181
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
24,456,289✔
2182
    if (code) {
24,448,869✔
UNCOV
2183
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2184
      return code;
×
2185
    }
2186
  }
2187

2188
  if (TSDB_CODE_SUCCESS == code) {
8,149,807✔
2189
    metaUpdateStbStats(pMeta, pEntry->uid, 0, pEntry->stbEntry.schemaRow.nCols - pOldEntry->stbEntry.schemaRow.nCols,
8,155,194✔
2190
                       pEntry->stbEntry.keep);
8,150,416✔
2191
  }
2192

2193
  return code;
8,147,893✔
2194
}
2195

2196
static int32_t metaHandleSuperTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
8,143,680✔
2197
  int32_t code = TSDB_CODE_SUCCESS;
8,143,680✔
2198

2199
  SMetaEntry *pOldEntry = NULL;
8,143,680✔
2200

2201
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
8,149,327✔
2202
  if (code) {
8,153,000✔
UNCOV
2203
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2204
    return code;
×
2205
  }
2206

2207
  SMetaHandleParam param = {
8,153,000✔
2208
      .pEntry = pEntry,
2209
      .pOldEntry = pOldEntry,
2210
  };
2211
  metaWLock(pMeta);
8,153,000✔
2212
  code = metaHandleSuperTableUpdateImpl(pMeta, &param);
8,152,932✔
2213
  metaULock(pMeta);
8,142,516✔
2214
  if (code) {
8,132,444✔
UNCOV
2215
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2216
    metaFetchEntryFree(&pOldEntry);
×
UNCOV
2217
    return code;
×
2218
  }
2219

2220
  int     nCols = pEntry->stbEntry.schemaRow.nCols;
8,132,444✔
2221
  int     onCols = pOldEntry->stbEntry.schemaRow.nCols;
8,144,036✔
2222
  int32_t deltaCol = nCols - onCols;
8,142,419✔
2223
  bool    updStat = deltaCol != 0 && !TABLE_IS_VIRTUAL(pEntry->flags) && !metaTbInFilterCache(pMeta, pEntry->name, 1);
8,142,419✔
2224

2225
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
8,148,101✔
2226
    STsdb  *pTsdb = pMeta->pVnode->pTsdb;
42,084✔
2227
    SArray *uids = NULL; /*taosArrayInit(8, sizeof(int64_t));
42,084✔
2228
     if (uids == NULL) {
2229
       metaErr(TD_VID(pMeta->pVnode), code);
2230
       metaFetchEntryFree(&pOldEntry);
2231
       return terrno;
2232
       }*/
2233
    if (deltaCol == 1) {
42,084✔
2234
      int16_t cid = pEntry->stbEntry.schemaRow.pSchema[nCols - 1].colId;
24,048✔
2235
      int8_t  col_type = pEntry->stbEntry.schemaRow.pSchema[nCols - 1].type;
24,048✔
2236

2237
      code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &uids);
24,048✔
2238
      if (code) {
24,048✔
UNCOV
2239
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2240
        metaFetchEntryFree(&pOldEntry);
×
UNCOV
2241
        return code;
×
2242
      }
2243
      if (pTsdb) {
24,048✔
2244
        TAOS_CHECK_RETURN(tsdbCacheNewSTableColumn(pTsdb, uids, cid, col_type));
24,048✔
2245
      }
2246
    } else if (deltaCol == -1) {
18,036✔
2247
      int16_t cid = -1;
18,036✔
2248
      bool    hasPrimaryKey = false;
18,036✔
2249
      if (onCols >= 2) {
18,036✔
2250
        hasPrimaryKey = (pOldEntry->stbEntry.schemaRow.pSchema[1].flags & COL_IS_KEY) ? true : false;
18,036✔
2251
      }
2252
      for (int i = 0, j = 0; i < nCols && j < onCols; ++i, ++j) {
174,348✔
2253
        if (pEntry->stbEntry.schemaRow.pSchema[i].colId != pOldEntry->stbEntry.schemaRow.pSchema[j].colId) {
168,336✔
2254
          cid = pOldEntry->stbEntry.schemaRow.pSchema[j].colId;
12,024✔
2255
          break;
12,024✔
2256
        }
2257
      }
2258

2259
      if (cid != -1) {
18,036✔
2260
        code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &uids);
12,024✔
2261
        if (code) {
12,024✔
UNCOV
2262
          metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2263
          metaFetchEntryFree(&pOldEntry);
×
2264
          return code;
×
2265
        }
2266
        if (pTsdb) {
12,024✔
2267
          TAOS_CHECK_RETURN(tsdbCacheDropSTableColumn(pTsdb, uids, cid, hasPrimaryKey));
12,024✔
2268
        }
2269
      }
2270
    }
2271
    if (uids) taosArrayDestroy(uids);
42,084✔
2272

2273
    if (pTsdb) {
42,084✔
2274
      tsdbCacheInvalidateSchema(pTsdb, pEntry->uid, -1, pEntry->stbEntry.schemaRow.version);
42,084✔
2275
    }
2276
  }
2277
  if (updStat) {
8,148,778✔
2278
    int64_t ctbNum = 0;
5,989,418✔
2279
    int32_t ret = metaGetStbStats(pMeta->pVnode, pEntry->uid, &ctbNum, 0, 0);
5,992,061✔
2280
    if (ret < 0) {
5,991,561✔
UNCOV
2281
      metaError("vgId:%d, failed to get stb stats:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
×
2282
                pEntry->uid, tstrerror(ret));
2283
    }
2284
    pMeta->pVnode->config.vndStats.numOfTimeSeries += (ctbNum * deltaCol);
5,991,561✔
2285
    if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
5,986,422✔
2286
  }
2287
  metaFetchEntryFree(&pOldEntry);
8,152,481✔
2288
  return code;
8,126,813✔
2289
}
2290

2291
static int32_t metaHandleChildTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
8,221,670✔
2292
  int32_t code = TSDB_CODE_SUCCESS;
8,221,670✔
2293

2294
  SMetaEntry *pOldEntry = NULL;
8,221,670✔
2295
  SMetaEntry *pSuperEntry = NULL;
8,221,670✔
2296

2297
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
8,221,670✔
2298
  if (code) {
8,221,670✔
UNCOV
2299
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2300
    return code;
×
2301
  }
2302

2303
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
8,221,670✔
2304
  if (code) {
8,221,670✔
2305
    metaErr(TD_VID(pMeta->pVnode), code);
×
2306
    metaFetchEntryFree(&pOldEntry);
×
UNCOV
2307
    return code;
×
2308
  }
2309

2310
  SMetaHandleParam param = {
8,221,670✔
2311
      .pEntry = pEntry,
2312
      .pOldEntry = pOldEntry,
2313
      .pSuperEntry = pSuperEntry,
2314
  };
2315

2316
  metaWLock(pMeta);
8,221,670✔
2317
  code = metaHandleChildTableUpdateImpl(pMeta, &param);
8,221,670✔
2318
  metaULock(pMeta);
8,221,670✔
2319
  if (code) {
8,221,670✔
UNCOV
2320
    metaErr(TD_VID(pMeta->pVnode), code);
×
2321
    metaFetchEntryFree(&pOldEntry);
×
2322
    metaFetchEntryFree(&pSuperEntry);
×
UNCOV
2323
    return code;
×
2324
  }
2325

2326
  metaFetchEntryFree(&pOldEntry);
8,221,670✔
2327
  metaFetchEntryFree(&pSuperEntry);
8,221,297✔
2328
  return code;
8,221,670✔
2329
}
2330

2331
static int32_t metaHandleNormalTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
3,711,414✔
2332
  int32_t     code = TSDB_CODE_SUCCESS;
3,711,414✔
2333
  SMetaEntry *pOldEntry = NULL;
3,711,414✔
2334

2335
  // fetch old entry
2336
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
3,711,414✔
2337
  if (code) {
3,711,414✔
UNCOV
2338
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2339
    return code;
×
2340
  }
2341

2342
  // handle update
2343
  SMetaHandleParam param = {
3,711,414✔
2344
      .pEntry = pEntry,
2345
      .pOldEntry = pOldEntry,
2346
  };
2347
  metaWLock(pMeta);
3,711,414✔
2348
  code = metaHandleNormalTableUpdateImpl(pMeta, &param);
3,711,414✔
2349
  metaULock(pMeta);
3,711,414✔
2350
  if (code) {
3,711,414✔
UNCOV
2351
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2352
    metaFetchEntryFree(&pOldEntry);
×
UNCOV
2353
    return code;
×
2354
  }
2355

2356
  // do other stuff
2357
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) &&
3,711,414✔
2358
      pEntry->ntbEntry.schemaRow.version != pOldEntry->ntbEntry.schemaRow.version) {
38,396✔
2359
#if 0
2360
    {  // for add column
2361
      int16_t cid = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].colId;
2362
      int8_t  col_type = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].type;
2363
      int32_t ret = tsdbCacheNewNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, col_type);
2364
      if (ret < 0) {
2365
        terrno = ret;
2366
        goto _err;
2367
      }
2368
    }
2369
    {  // for drop column
2370

2371
      if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
2372
        int16_t cid = pColumn->colId;
2373

2374
        if (tsdbCacheDropNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, hasPrimayKey) != 0) {
2375
          metaError("vgId:%d, failed to drop ntable column:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name,
2376
                    entry.uid);
2377
        }
2378
        tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, entry.uid, pSchema->version);
2379
      }
2380
    }
2381
    }
2382
#endif
2383
    if (pMeta->pVnode->pTsdb) {
38,396✔
2384
      tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, pEntry->uid, pEntry->ntbEntry.schemaRow.version);
38,396✔
2385
    }
2386
  }
2387
  int32_t deltaCol = pEntry->ntbEntry.schemaRow.nCols - pOldEntry->ntbEntry.schemaRow.nCols;
3,711,414✔
2388
  pMeta->pVnode->config.vndStats.numOfNTimeSeries += deltaCol;
3,711,414✔
2389
  if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
3,711,414✔
2390
  metaFetchEntryFree(&pOldEntry);
3,711,414✔
2391
  return code;
3,711,414✔
2392
}
2393

2394
static int32_t metaHandleVirtualNormalTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
229,894✔
2395
  int32_t     code = TSDB_CODE_SUCCESS;
229,894✔
2396
  SMetaEntry *pOldEntry = NULL;
229,894✔
2397

2398
  // fetch old entry
2399
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
229,894✔
2400
  if (code) {
229,894✔
UNCOV
2401
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2402
    return code;
×
2403
  }
2404

2405
  // handle update
2406
  SMetaHandleParam param = {
229,894✔
2407
      .pEntry = pEntry,
2408
      .pOldEntry = pOldEntry,
2409
  };
2410
  metaWLock(pMeta);
229,894✔
2411
  code = metaHandleVirtualNormalTableUpdateImpl(pMeta, &param);
229,894✔
2412
  metaULock(pMeta);
229,894✔
2413
  if (code) {
229,894✔
UNCOV
2414
    metaErr(TD_VID(pMeta->pVnode), code);
×
2415
    metaFetchEntryFree(&pOldEntry);
×
2416
    return code;
×
2417
  }
2418

2419
  metaTimeSeriesNotifyCheck(pMeta);
229,894✔
2420
  metaFetchEntryFree(&pOldEntry);
229,894✔
2421
  return code;
229,894✔
2422
}
2423

2424
static int32_t metaHandleVirtualChildTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
235,058✔
2425
  int32_t code = TSDB_CODE_SUCCESS;
235,058✔
2426

2427
  SMetaEntry *pOldEntry = NULL;
235,058✔
2428
  SMetaEntry *pSuperEntry = NULL;
235,058✔
2429

2430
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
235,058✔
2431
  if (code) {
235,058✔
UNCOV
2432
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2433
    return code;
×
2434
  }
2435

2436
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
235,058✔
2437
  if (code) {
235,058✔
2438
    metaErr(TD_VID(pMeta->pVnode), code);
×
2439
    metaFetchEntryFree(&pOldEntry);
×
UNCOV
2440
    return code;
×
2441
  }
2442

2443
  SMetaHandleParam param = {
235,058✔
2444
      .pEntry = pEntry,
2445
      .pOldEntry = pOldEntry,
2446
      .pSuperEntry = pSuperEntry,
2447
  };
2448

2449
  metaWLock(pMeta);
235,058✔
2450
  code = metaHandleVirtualChildTableUpdateImpl(pMeta, &param);
235,058✔
2451
  metaULock(pMeta);
235,058✔
2452
  if (code) {
235,058✔
UNCOV
2453
    metaErr(TD_VID(pMeta->pVnode), code);
×
2454
    metaFetchEntryFree(&pOldEntry);
×
2455
    metaFetchEntryFree(&pSuperEntry);
×
UNCOV
2456
    return code;
×
2457
  }
2458

2459
  metaFetchEntryFree(&pOldEntry);
235,058✔
2460
  metaFetchEntryFree(&pSuperEntry);
235,058✔
2461
  return code;
235,058✔
2462
}
2463

2464
static int32_t metaHandleSuperTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
961,587✔
2465
  int32_t     code = TSDB_CODE_SUCCESS;
961,587✔
2466
  SArray     *childList = NULL;
961,587✔
2467
  SMetaEntry *pOldEntry = NULL;
962,194✔
2468

2469
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
961,335✔
2470
  if (code) {
960,911✔
UNCOV
2471
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2472
    return code;
×
2473
  }
2474

2475
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childList);
960,911✔
2476
  if (code) {
961,588✔
UNCOV
2477
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2478
    metaFetchEntryFree(&pOldEntry);
×
UNCOV
2479
    return code;
×
2480
  }
2481

2482
  if (pMeta->pVnode->pTsdb && tsdbCacheDropSubTables(pMeta->pVnode->pTsdb, childList, pEntry->uid) < 0) {
961,588✔
2483
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
2,814✔
2484
              pEntry->uid, tstrerror(terrno));
2485
  }
2486

2487
  // loop to drop all child tables
2488
  for (int32_t i = 0; i < taosArrayGetSize(childList); i++) {
1,805,534✔
2489
    SMetaEntry childEntry = {
844,818✔
2490
        .version = pEntry->version,
844,704✔
2491
        .uid = *(tb_uid_t *)taosArrayGet(childList, i),
844,704✔
2492
        .type = -TSDB_CHILD_TABLE,
2493
    };
2494

2495
    code = metaHandleChildTableDrop(pMeta, &childEntry, true);
844,704✔
2496
    if (code) {
844,027✔
2497
      metaErr(TD_VID(pMeta->pVnode), code);
22,142✔
2498
    }
2499
  }
2500

2501
  // do drop super table
2502
  SMetaHandleParam param = {
960,830✔
2503
      .pEntry = pEntry,
2504
      .pOldEntry = pOldEntry,
2505
  };
2506
  metaWLock(pMeta);
961,507✔
2507
  code = metaHandleSuperTableDropImpl(pMeta, &param);
962,194✔
2508
  metaULock(pMeta);
962,194✔
2509
  if (code) {
962,194✔
UNCOV
2510
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2511
    taosArrayDestroy(childList);
×
UNCOV
2512
    metaFetchEntryFree(&pOldEntry);
×
UNCOV
2513
    return code;
×
2514
  }
2515

2516
  // do other stuff
2517
  // metaUpdTimeSeriesNum(pMeta);
2518

2519
  // free resource and return
2520
  taosArrayDestroy(childList);
962,194✔
2521
  metaFetchEntryFree(&pOldEntry);
962,194✔
2522
  return code;
961,517✔
2523
}
2524

2525
int32_t metaHandleEntry2(SMeta *pMeta, const SMetaEntry *pEntry) {
109,150,039✔
2526
  int32_t   code = TSDB_CODE_SUCCESS;
109,150,039✔
2527
  int32_t   vgId = TD_VID(pMeta->pVnode);
109,150,039✔
2528
  SMetaInfo info = {0};
109,173,266✔
2529
  int8_t    type = pEntry->type > 0 ? pEntry->type : -pEntry->type;
109,157,300✔
2530

2531
  if (NULL == pMeta || NULL == pEntry) {
109,157,234✔
2532
    metaError("%s failed at %s:%d since invalid parameter", __func__, __FILE__, __LINE__);
80,257✔
UNCOV
2533
    return TSDB_CODE_INVALID_PARA;
×
2534
  }
2535

2536
  if (pEntry->type > 0) {
109,076,977✔
2537
    bool isExist = false;
106,456,916✔
2538
    if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) {
106,456,916✔
2539
      isExist = true;
20,544,814✔
2540
    }
2541

2542
    switch (type) {
106,440,259✔
2543
      case TSDB_SUPER_TABLE: {
13,704,705✔
2544
        if (isExist) {
13,704,705✔
2545
          code = metaHandleSuperTableUpdate(pMeta, pEntry);
8,140,742✔
2546
        } else {
2547
          code = metaHandleSuperTableCreate(pMeta, pEntry);
5,563,963✔
2548
        }
2549
        break;
13,708,595✔
2550
      }
2551
      case TSDB_CHILD_TABLE: {
80,243,165✔
2552
        if (isExist) {
80,243,165✔
2553
          code = metaHandleChildTableUpdate(pMeta, pEntry);
8,221,670✔
2554
        } else {
2555
          code = metaHandleChildTableCreate(pMeta, pEntry);
72,021,495✔
2556
        }
2557
        break;
80,246,933✔
2558
      }
2559
      case TSDB_NORMAL_TABLE: {
11,474,841✔
2560
        if (isExist) {
11,474,841✔
2561
          code = metaHandleNormalTableUpdate(pMeta, pEntry);
3,711,414✔
2562
        } else {
2563
          code = metaHandleNormalTableCreate(pMeta, pEntry);
7,763,427✔
2564
        }
2565
        break;
11,474,841✔
2566
      }
2567
      case TSDB_VIRTUAL_NORMAL_TABLE: {
429,611✔
2568
        if (isExist) {
429,611✔
2569
          code = metaHandleVirtualNormalTableUpdate(pMeta, pEntry);
229,894✔
2570
        } else {
2571
          code = metaHandleVirtualNormalTableCreate(pMeta, pEntry);
199,717✔
2572
        }
2573
        break;
429,611✔
2574
      }
2575
      case TSDB_VIRTUAL_CHILD_TABLE: {
587,899✔
2576
        if (isExist) {
587,899✔
2577
          code = metaHandleVirtualChildTableUpdate(pMeta, pEntry);
235,058✔
2578
        } else {
2579
          code = metaHandleVirtualChildTableCreate(pMeta, pEntry);
352,841✔
2580
        }
2581
        break;
587,899✔
2582
      }
2583
      default: {
38✔
2584
        code = TSDB_CODE_INVALID_PARA;
38✔
2585
        break;
38✔
2586
      }
2587
    }
2588
  } else {
2589
    switch (type) {
2,718,133✔
2590
      case TSDB_SUPER_TABLE: {
961,517✔
2591
        code = metaHandleSuperTableDrop(pMeta, pEntry);
961,517✔
2592
        break;
962,194✔
2593
      }
2594
      case TSDB_CHILD_TABLE: {
838,052✔
2595
        code = metaHandleChildTableDrop(pMeta, pEntry, false);
838,052✔
2596
        break;
838,052✔
2597
      }
2598
      case TSDB_NORMAL_TABLE: {
846,236✔
2599
        code = metaHandleNormalTableDrop(pMeta, pEntry);
846,236✔
2600
        break;
846,236✔
2601
      }
2602
      case TSDB_VIRTUAL_NORMAL_TABLE: {
40,541✔
2603
        code = metaHandleVirtualNormalTableDrop(pMeta, pEntry);
40,541✔
2604
        break;
40,541✔
2605
      }
2606
      case TSDB_VIRTUAL_CHILD_TABLE: {
31,806✔
2607
        code = metaHandleVirtualChildTableDrop(pMeta, pEntry, false);
31,806✔
2608
        break;
31,806✔
2609
      }
UNCOV
2610
      default: {
×
UNCOV
2611
        code = TSDB_CODE_INVALID_PARA;
×
UNCOV
2612
        break;
×
2613
      }
2614
    }
2615
  }
2616

2617
  if (TSDB_CODE_SUCCESS == code) {
109,166,727✔
2618
    pMeta->changed = true;
109,178,088✔
2619
    metaDebug("vgId:%d, index:%" PRId64 ", handle meta entry success, type:%d tb:%s uid:%" PRId64, vgId,
109,186,431✔
2620
              pEntry->version, pEntry->type, pEntry->type > 0 ? pEntry->name : "", pEntry->uid);
2621
  } else {
UNCOV
2622
    metaErr(vgId, code);
×
2623
  }
2624
  TAOS_RETURN(code);
109,175,362✔
2625
}
2626

2627
void metaHandleSyncEntry(SMeta *pMeta, const SMetaEntry *pEntry) {
171,687✔
2628
  int32_t code = TSDB_CODE_SUCCESS;
171,687✔
2629
  code = metaHandleEntry2(pMeta, pEntry);
171,687✔
2630
  if (code) {
171,687✔
UNCOV
2631
    metaErr(TD_VID(pMeta->pVnode), code);
×
2632
  }
2633
  return;
171,687✔
2634
}
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