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

taosdata / TDengine / #4839

09 Nov 2025 09:08AM UTC coverage: 70.408% (-0.8%) from 71.256%
#4839

push

travis-ci

web-flow
Merge a8d8f75bf into b63644c82

261 of 436 new or added lines in 5 files covered. (59.86%)

641 existing lines in 4 files now uncovered.

211965 of 301053 relevant lines covered (70.41%)

285833932.56 hits per line

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

76.76
/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) {
1,795,669,036✔
67
  int32_t code = TSDB_CODE_SUCCESS;
1,795,669,036✔
68
  void   *value = NULL;
1,795,669,036✔
69
  int32_t valueSize = 0;
1,795,835,938✔
70

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

78
  // search entry table
79
  STbDbKey key = {
1,795,840,693✔
80
      .version = ((SUidIdxVal *)value)->version,
1,795,747,867✔
81
      .uid = uid,
82
  };
83
  tdbFreeClear(value);
1,795,934,272✔
84

85
  code = tdbTbGet(pMeta->pTbDb, &key, sizeof(key), &value, &valueSize);
1,795,885,034✔
86
  if (TSDB_CODE_SUCCESS != code) {
1,795,990,204✔
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};
1,795,990,204✔
94
  SMetaEntry entry = {0};
1,795,964,250✔
95

96
  tDecoderInit(&decoder, value, valueSize);
1,796,032,740✔
97
  code = metaDecodeEntry(&decoder, &entry);
1,796,159,383✔
98
  if (code) {
1,795,854,301✔
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);
1,795,854,301✔
107
  if (code) {
1,795,888,792✔
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);
1,795,888,792✔
116
  tDecoderClear(&decoder);
1,795,845,124✔
117
  return code;
1,796,053,828✔
118
}
119

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

125
  code = tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &value, &valueSize);
808,014,842✔
126
  if (TSDB_CODE_SUCCESS != code) {
807,786,794✔
127
    metaError("vgId:%d, failed to get entry by name:%s since %s", TD_VID(pMeta->pVnode), name, tstrerror(code));
×
128
    return code;
×
129
  }
130
  int64_t uid = *(int64_t *)value;
807,786,794✔
131
  tdbFreeClear(value);
807,890,550✔
132

133
  code = metaFetchEntryByUid(pMeta, uid, ppEntry);
807,906,425✔
134
  if (TSDB_CODE_SUCCESS != code) {
807,857,427✔
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;
807,857,427✔
139
}
140

141
void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); }
1,796,439,785✔
142

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

147
  int32_t  code = TSDB_CODE_SUCCESS;
987,144,444✔
148
  int32_t  vgId = TD_VID(pMeta->pVnode);
987,144,444✔
149
  void    *value = NULL;
987,107,497✔
150
  int32_t  valueSize = 0;
987,107,497✔
151
  SEncoder encoder = {0};
987,107,497✔
152
  STbDbKey key = {
987,149,030✔
153
      .version = pEntry->version,
987,079,471✔
154
      .uid = pEntry->uid,
987,151,686✔
155
  };
156

157
  // encode entry
158
  tEncodeSize(metaEncodeEntry, pEntry, valueSize, code);
987,239,546✔
159
  if (code != 0) {
986,757,718✔
160
    metaErr(vgId, code);
×
161
    return code;
×
162
  }
163

164
  value = taosMemoryMalloc(valueSize);
986,757,718✔
165
  if (NULL == value) {
986,436,051✔
166
    metaErr(vgId, terrno);
×
167
    return terrno;
×
168
  }
169

170
  tEncoderInit(&encoder, value, valueSize);
986,436,051✔
171
  code = metaEncodeEntry(&encoder, pEntry);
987,005,864✔
172
  if (code) {
986,845,550✔
173
    metaErr(vgId, code);
×
174
    tEncoderClear(&encoder);
×
175
    taosMemoryFree(value);
×
176
    return code;
×
177
  }
178
  tEncoderClear(&encoder);
986,845,550✔
179

180
  // put to tdb
181
  if (META_TABLE_OP_INSERT == op) {
986,916,068✔
182
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
833,066,652✔
183
  } else if (META_TABLE_OP_UPDATA == op) {
153,849,416✔
184
    code = tdbTbUpsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
132,871,068✔
185
  } else if (META_TABLE_OP_DELETE == op) {
20,978,348✔
186
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
20,975,816✔
187
  } else {
188
    code = TSDB_CODE_INVALID_PARA;
2,532✔
189
  }
190
  if (TSDB_CODE_SUCCESS != code) {
986,842,136✔
191
    metaErr(vgId, code);
×
192
  }
193
  taosMemoryFree(value);
986,900,911✔
194
  return code;
986,903,500✔
195
}
196

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

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

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

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

217
  const SMetaEntry     *pEntry = pParam->pEntry;
185,767,542✔
218
  const SSchemaWrapper *pSchema = NULL;
185,722,381✔
219
  if (pEntry->type == TSDB_SUPER_TABLE) {
185,722,381✔
220
    pSchema = &pEntry->stbEntry.schemaRow;
125,643,786✔
221
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
60,222,919✔
222
    pSchema = &pEntry->ntbEntry.schemaRow;
60,222,919✔
223
  } else {
224
    return TSDB_CODE_INVALID_PARA;
×
225
  }
226
  SSkmDbKey key = {
186,007,681✔
227
      .uid = pEntry->uid,
185,867,278✔
228
      .sver = pSchema->version,
185,930,595✔
229
  };
230

231
  // encode schema
232
  tEncodeSize(tEncodeSSchemaWrapper, pSchema, valueSize, code);
371,858,959✔
233
  if (TSDB_CODE_SUCCESS != code) {
185,825,245✔
234
    metaErr(vgId, code);
×
235
    return code;
×
236
  }
237

238
  value = taosMemoryMalloc(valueSize);
185,825,245✔
239
  if (NULL == value) {
185,528,207✔
240
    metaErr(vgId, terrno);
×
241
    return terrno;
×
242
  }
243

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

254
  // put to tdb
255
  if (META_TABLE_OP_INSERT == op) {
185,844,507✔
256
    code = tdbTbInsert(pMeta->pSkmDb, &key, sizeof(key), value, valueSize, pMeta->txn);
16,973✔
257
  } else if (META_TABLE_OP_UPDATA == op) {
185,828,301✔
258
    code = tdbTbUpsert(pMeta->pSkmDb, &key, sizeof(key), value, valueSize, pMeta->txn);
185,828,301✔
259
  } else {
260
    code = TSDB_CODE_INVALID_PARA;
×
261
  }
262
  if (TSDB_CODE_SUCCESS != code) {
186,024,854✔
263
    metaErr(vgId, code);
×
264
  }
265
  taosMemoryFree(value);
185,936,457✔
266
  return code;
185,943,473✔
267
}
268

269
static int32_t metaSchemaTableInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
270
  return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
×
271
}
272

273
static int32_t metaAddOrDropTagIndexOfSuperTable(SMeta *pMeta, const SMetaHandleParam *pParam,
347,988,047✔
274
                                                 const SSchema *pOldColumn, const SSchema *pNewColumn) {
275
  int32_t code = TSDB_CODE_SUCCESS;
347,988,047✔
276

277
  const SMetaEntry *pEntry = pParam->pEntry;
347,988,047✔
278
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
348,078,460✔
279
  enum { ADD_INDEX, DROP_INDEX } action;
280

281
  if (pOldColumn && pNewColumn) {
348,067,716✔
282
    if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
341,178,058✔
283
      return TSDB_CODE_SUCCESS;
12,707,051✔
284
    } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) {
328,481,694✔
285
      action = DROP_INDEX;
123,055✔
286
    } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
328,286,419✔
287
      action = ADD_INDEX;
82,070✔
288
    } else {
289
      return TSDB_CODE_SUCCESS;
328,164,588✔
290
    }
291
  } else if (pOldColumn) {
6,889,658✔
292
    if (IS_IDX_ON(pOldColumn)) {
2,739,080✔
293
      action = DROP_INDEX;
77,006✔
294
    } else {
295
      return TSDB_CODE_SUCCESS;
2,643,967✔
296
    }
297
  } else {
298
    if (IS_IDX_ON(pNewColumn)) {
4,150,578✔
299
      action = ADD_INDEX;
×
300
    } else {
301
      return TSDB_CODE_SUCCESS;
4,177,520✔
302
    }
303
  }
304

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

313
  // do drop or add index
314
  for (int32_t i = 0; i < taosArrayGetSize(childTables); i++) {
2,376,679✔
315
    int64_t uid = *(int64_t *)taosArrayGet(childTables, i);
2,094,493✔
316

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

326
    STagIdxKey *pTagIdxKey = NULL;
2,094,493✔
327
    int32_t     tagIdxKeySize = 0;
2,094,493✔
328

329
    if (action == ADD_INDEX) {
2,094,493✔
330
      code = metaFetchTagIdxKey(pMeta, pChildEntry, pNewColumn, &pTagIdxKey, &tagIdxKeySize);
1,033,886✔
331
      if (code) {
1,033,886✔
332
        metaErr(TD_VID(pMeta->pVnode), code);
×
333
        taosArrayDestroy(childTables);
×
334
        metaFetchEntryFree(&pChildEntry);
×
335
        return code;
×
336
      }
337

338
      code = tdbTbInsert(pMeta->pTagIdx, pTagIdxKey, tagIdxKeySize, NULL, 0, pMeta->txn);
1,033,886✔
339
      if (code) {
1,033,886✔
340
        metaErr(TD_VID(pMeta->pVnode), code);
×
341
        taosArrayDestroy(childTables);
×
342
        metaFetchEntryFree(&pChildEntry);
×
343
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
344
        return code;
×
345
      }
346
    } else {
347
      code = metaFetchTagIdxKey(pMeta, pChildEntry, pOldColumn, &pTagIdxKey, &tagIdxKeySize);
1,060,607✔
348
      if (code) {
1,060,607✔
349
        metaErr(TD_VID(pMeta->pVnode), code);
×
350
        taosArrayDestroy(childTables);
×
351
        metaFetchEntryFree(&pChildEntry);
×
352
        return code;
×
353
      }
354

355
      code = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, tagIdxKeySize, pMeta->txn);
1,060,607✔
356
      if (code) {
1,060,607✔
357
        metaErr(TD_VID(pMeta->pVnode), code);
×
358
        taosArrayDestroy(childTables);
×
359
        metaFetchEntryFree(&pChildEntry);
×
360
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
361
        return code;
×
362
      }
363
    }
364

365
    metaFetchTagIdxKeyFree(&pTagIdxKey);
2,094,493✔
366
    metaFetchEntryFree(&pChildEntry);
2,094,493✔
367
  }
368

369
  taosArrayDestroy(childTables);
282,186✔
370
  return code;
282,186✔
371
}
372

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

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

381
  if (pOldColumn && pNewColumn) {
1,424,760✔
382
    return TSDB_CODE_SUCCESS;
1,367,078✔
383
  } else if (pOldColumn) {
57,682✔
384
    action = DROP_COLUMN;
24,750✔
385
  } else {
386
    action = ADD_COLUMN;
32,932✔
387
  }
388

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

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

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

410
    SMetaHandleParam param = {.pEntry = pChildEntry};
61,518✔
411

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

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

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

448
  taosArrayDestroy(childTables);
57,682✔
449
  return code;
57,682✔
450
}
451

452
static int32_t metaUpdateSuperTableTagSchema(SMeta *pMeta, const SMetaHandleParam *pParam) {
13,113,893✔
453
  int32_t               code = TSDB_CODE_SUCCESS;
13,113,893✔
454
  const SMetaEntry     *pEntry = pParam->pEntry;
13,113,893✔
455
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
13,134,236✔
456
  const SSchemaWrapper *pNewTagSchema = &pEntry->stbEntry.schemaTag;
13,118,311✔
457
  const SSchemaWrapper *pOldTagSchema = &pOldEntry->stbEntry.schemaTag;
13,133,492✔
458

459
  int32_t iOld = 0, iNew = 0;
13,123,500✔
460
  for (; iOld < pOldTagSchema->nCols && iNew < pNewTagSchema->nCols;) {
356,379,429✔
461
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
343,242,684✔
462
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
343,224,890✔
463

464
    if (pOldColumn->colId == pNewColumn->colId) {
343,224,450✔
465
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
341,053,956✔
466
      if (code) {
341,030,930✔
467
        metaErr(TD_VID(pMeta->pVnode), code);
×
468
        return code;
×
469
      }
470

471
      iOld++;
341,064,528✔
472
      iNew++;
341,064,528✔
473
    } else if (pOldColumn->colId < pNewColumn->colId) {
2,191,314✔
474
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
2,186,471✔
475
      if (code) {
2,180,442✔
476
        metaErr(TD_VID(pMeta->pVnode), code);
×
477
        return code;
×
478
      }
479

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

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

545,441✔
495
      iNew++;
544,520✔
UNCOV
496
    }
×
UNCOV
497
  }
×
498

499
  for (; iOld < pOldTagSchema->nCols; iOld++) {
500
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
501
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
17,249,000✔
502
    if (code) {
4,166,978✔
503
      metaErr(TD_VID(pMeta->pVnode), code);
4,148,467✔
504
      return code;
4,138,991✔
UNCOV
505
    }
×
NEW
506
    // drop old tag from meta stable tag filter cache
×
507
    code = metaStableTagFilterCacheDropTag(pMeta, pEntry->uid, pOldColumn->colId);
508
    if (code) {
509
      metaErr(TD_VID(pMeta->pVnode), code);
510
      return code;
13,090,820✔
511
    }
512
  }
513

72,563✔
514
  for (; iNew < pNewTagSchema->nCols; iNew++) {
72,563✔
515
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
72,563✔
516
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, NULL, pNewColumn);
72,563✔
517
    if (code) {
72,563✔
518
      metaErr(TD_VID(pMeta->pVnode), code);
72,563✔
519
      return code;
520
    }
72,563✔
521
  }
1,449,359✔
522

1,373,444✔
523
  return code;
1,380,192✔
524
}
525

1,381,879✔
526
static int32_t metaUpdateSuperTableRowSchema(SMeta *pMeta, const SMetaHandleParam *pParam) {
1,367,100✔
527
  int32_t               code = TSDB_CODE_SUCCESS;
1,360,330✔
UNCOV
528
  const SMetaEntry     *pEntry = pParam->pEntry;
×
UNCOV
529
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
×
530
  const SSchemaWrapper *pNewRowSchema = &pEntry->stbEntry.schemaRow;
531
  const SSchemaWrapper *pOldRowSchema = &pOldEntry->stbEntry.schemaRow;
532

1,360,330✔
533
  int32_t iOld = 0, iNew = 0;
1,360,330✔
534
  for (; iOld < pOldRowSchema->nCols && iNew < pNewRowSchema->nCols;) {
16,466✔
535
    SSchema *pOldColumn = pOldRowSchema->pSchema + iOld;
16,466✔
536
    SSchema *pNewColumn = pNewRowSchema->pSchema + iNew;
16,466✔
UNCOV
537

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

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

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

562
      iNew++;
107,182✔
563
    }
32,932✔
564
  }
32,932✔
565

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

198,942,420✔
575
  for (; iNew < pNewRowSchema->nCols; iNew++) {
198,942,420✔
576
    SSchema *pNewColumn = pNewRowSchema->pSchema + iNew;
577
    code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, NULL, pNewColumn);
198,942,420✔
578
    if (code) {
199,337,874✔
579
      metaErr(TD_VID(pMeta->pVnode), code);
580
      return code;
199,289,333✔
581
    }
113,432,347✔
582
  }
583

584
  return code;
85,856,986✔
585
}
586

23,441,728✔
587
static int32_t metaSchemaTableUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
23,145,904✔
588
  int32_t code = TSDB_CODE_SUCCESS;
589

62,471,810✔
590
  const SMetaEntry *pEntry = pParam->pEntry;
591
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
62,558,490✔
592

49,393,313✔
593
  if (NULL == pOldEntry) {
74,250✔
594
    return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
595
  }
49,309,643✔
596

597
  if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
598
    // check row schema
599
    if (pOldEntry->ntbEntry.schemaRow.version != pEntry->ntbEntry.schemaRow.version) {
600
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
13,122,774✔
601
    }
13,091,585✔
UNCOV
602
  } else if (pEntry->type == TSDB_SUPER_TABLE) {
×
UNCOV
603
    // check row schema
×
604
    if (pOldEntry->stbEntry.schemaRow.version != pEntry->stbEntry.schemaRow.version) {
605
      if (TABLE_IS_VIRTUAL(pEntry->flags)) {
606
        return metaUpdateSuperTableRowSchema(pMeta, pParam);
UNCOV
607
      } else {
×
608
        return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
609
      }
610
    }
13,252,708✔
611

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

619
  } else {
965,839,165✔
620
    return TSDB_CODE_INVALID_PARA;
965,839,165✔
621
  }
965,946,870✔
622

965,576,871✔
623
  return TSDB_CODE_SUCCESS;
138,837,040✔
624
}
138,813,281✔
625

826,933,093✔
626
static int32_t metaSchemaTableDelete(SMeta *pMeta, const SMetaHandleParam *pEntry) {
766,665,950✔
627
  // TODO
766,612,398✔
628
  return TSDB_CODE_SUCCESS;
60,373,193✔
629
}
60,373,193✔
630

60,373,193✔
631
// Uid Index
632
static void metaBuildEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) {
965,945,087✔
633
  pInfo->uid = pEntry->uid;
634
  pInfo->version = pEntry->version;
965,731,505✔
635
  if (pEntry->type == TSDB_SUPER_TABLE) {
965,731,505✔
636
    pInfo->suid = pEntry->uid;
965,731,505✔
637
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
638
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
965,728,950✔
639
    pInfo->suid = pEntry->ctbEntry.suid;
640
    pInfo->skmVer = 0;
641
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
965,899,748✔
642
    pInfo->suid = 0;
965,937,933✔
643
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
965,646,854✔
644
  }
965,790,985✔
UNCOV
645
}
×
646

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

965,604,805✔
651
  const SMetaEntry *pEntry = pParam->pEntry;
965,695,173✔
652

965,604,805✔
653
  // update cache
654
  SMetaInfo info = {0};
965,695,173✔
655
  metaBuildEntryInfo(pEntry, &info);
832,952,359✔
656
  code = metaCacheUpsert(pMeta, &info);
132,742,814✔
657
  if (code) {
132,720,335✔
658
    metaErr(vgId, code);
659
  }
966,061,204✔
660

661
  // put to tdb
662
  SUidIdxVal value = {
832,810,697✔
663
      .suid = info.suid,
832,810,697✔
664
      .skmVer = info.skmVer,
665
      .version = pEntry->version,
666
  };
132,775,797✔
667
  if (META_TABLE_OP_INSERT == op) {
132,775,797✔
668
    code = tdbTbInsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
669
  } else if (META_TABLE_OP_UPDATA == op) {
670
    code = tdbTbUpsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
26,604,015✔
671
  }
26,604,015✔
672
  return code;
673
}
26,604,015✔
674

675
static int32_t metaUidIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
676
  return metaUidIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
26,605,836✔
677
}
26,604,190✔
UNCOV
678

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

26,604,190✔
683
static int32_t metaUidIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
26,607,701✔
684
  int32_t code = 0;
685

686
  const SMetaEntry *pEntry = pParam->pOldEntry;
687

833,067,428✔
688
  // delete tdb
833,067,428✔
689
  code = tdbTbDelete(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), pMeta->txn);
690
  if (code) {
833,067,428✔
691
    metaErr(TD_VID(pMeta->pVnode), code);
692
  }
833,173,678✔
693

833,173,678✔
694
  // delete cache
UNCOV
695
  (void)metaCacheDrop(pMeta, pEntry->uid);
×
UNCOV
696
  return code;
×
697
}
698

UNCOV
699
// Name Index
×
700
static int32_t metaNameIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
701
  int32_t code = TSDB_CODE_SUCCESS;
833,256,148✔
UNCOV
702

×
703
  const SMetaEntry *pEntry = pParam->pEntry;
704

833,237,178✔
705
  if (META_TABLE_OP_INSERT == op) {
706
    code = tdbTbInsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
707
                       pMeta->txn);
833,116,095✔
708
  } else if (META_TABLE_OP_UPDATA == op) {
833,116,095✔
709
    code = tdbTbUpsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
833,116,095✔
710
                       pMeta->txn);
711
  } else {
712
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
713
  }
×
714
  if (code) {
715
    metaErr(TD_VID(pMeta->pVnode), code);
716
  }
26,604,195✔
717
  return code;
26,604,195✔
718
}
719

26,604,195✔
720
static int32_t metaNameIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
26,607,881✔
721
  int32_t code = TSDB_CODE_SUCCESS;
26,607,591✔
UNCOV
722
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
×
723
}
724

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

76,363,431✔
729
static int32_t metaNameIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
76,363,431✔
730
  int32_t code = TSDB_CODE_SUCCESS;
731

76,417,789✔
732
  const SMetaEntry *pEntry = pParam->pOldEntry;
76,468,057✔
UNCOV
733
  code = tdbTbDelete(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, pMeta->txn);
×
734
  if (code) {
735
    metaErr(TD_VID(pMeta->pVnode), code);
76,391,497✔
736
  }
737
  return code;
738
}
7,515,493✔
739

7,515,493✔
740
// Suid Index
741
static int32_t metaSUidIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
7,517,314✔
742
  const SMetaEntry *pEntry = pParam->pEntry;
7,517,314✔
UNCOV
743

×
744
  int32_t code = tdbTbInsert(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), NULL, 0, pMeta->txn);
745
  if (code) {
7,515,493✔
746
    metaErr(TD_VID(pMeta->pVnode), code);
747
  }
748
  return code;
749
}
764,619,400✔
750

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

754
  int32_t code = tdbTbDelete(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), pMeta->txn);
764,658,320✔
755
  if (code) {
764,691,497✔
756
    metaErr(TD_VID(pMeta->pVnode), code);
764,676,626✔
757
  }
758
  return code;
759
}
764,650,032✔
760

1,439,532,822✔
761
// Child Index
719,727,695✔
762
static int32_t metaChildIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
44,893,507✔
763
  int32_t code = TSDB_CODE_SUCCESS;
89,787,014✔
764

44,893,507✔
765
  const SMetaEntry *pEntry = pParam->pEntry;
UNCOV
766

×
767
  SCtbIdxKey key = {
768
      .suid = pEntry->ctbEntry.suid,
764,729,135✔
769
      .uid = pEntry->uid,
770
  };
771

719,743,623✔
772
  if (META_TABLE_OP_INSERT == op) {
719,743,623✔
773
    code = tdbTbInsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
774
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
775
  } else if (META_TABLE_OP_UPDATA == op) {
47,024,021✔
776
    code = tdbTbUpsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
47,024,021✔
777
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
47,024,021✔
778
  } else {
47,024,021✔
779
    code = TSDB_CODE_INVALID_PARA;
780
  }
47,024,021✔
781
  return code;
47,024,021✔
782
}
47,024,021✔
783

44,893,507✔
784
static int32_t metaChildIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
785
  return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
2,130,514✔
786
}
787

788
static int32_t metaChildIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
11,023,082✔
789
  const SMetaEntry *pEntry = pParam->pEntry;
11,023,082✔
790
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
791
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
11,024,947✔
792

11,023,082✔
793
  const STag *pNewTags = (const STag *)pEntry->ctbEntry.pTags;
11,024,947✔
794
  const STag *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags;
795
  if (pNewTags->len != pOldTags->len || memcmp(pNewTags, pOldTags, pNewTags->len)) {
11,024,281✔
796
    return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
797
  }
798
  return 0;
799
}
820,474,067✔
800

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

820,474,067✔
804
  SCtbIdxKey key = {
820,522,417✔
805
      .suid = pEntry->ctbEntry.suid,
820,580,120✔
806
      .uid = pEntry->uid,
820,580,120✔
807
  };
808
  return tdbTbDelete(pMeta->pCtbIdx, &key, sizeof(key), pMeta->txn);
820,580,120✔
809
}
820,564,895✔
810

811
// Tag Index
812
static int32_t metaFetchTagIdxKey(SMeta *pMeta, const SMetaEntry *pEntry, const SSchema *pTagColumn,
820,572,242✔
813
                                  STagIdxKey **ppTagIdxKey, int32_t *pTagIdxKeySize) {
790,958,931✔
814
  int32_t code = TSDB_CODE_SUCCESS;
78,327,748✔
815

78,327,748✔
816
  STagIdxKey *pTagIdxKey = NULL;
817
  int32_t     nTagIdxKey;
712,562,660✔
818
  const void *pTagData = NULL;
712,562,660✔
819
  int32_t     nTagData = 0;
820

821
  STagVal tagVal = {
29,581,361✔
822
      .cid = pTagColumn->colId,
29,033,608✔
823
  };
824

825
  if (tTagGet((const STag *)pEntry->ctbEntry.pTags, &tagVal)) {
826
    if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
820,550,263✔
827
      pTagData = tagVal.pData;
820,454,383✔
828
      nTagData = (int32_t)tagVal.nData;
820,285,178✔
UNCOV
829
    } else {
×
UNCOV
830
      pTagData = &(tagVal.i64);
×
831
      nTagData = tDataTypes[pTagColumn->type].bytes;
832
    }
833
  } else {
820,285,178✔
834
    if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
820,403,156✔
835
      nTagData = tDataTypes[pTagColumn->type].bytes;
820,465,329✔
836
    }
837
  }
838

820,560,043✔
839
  code = metaCreateTagIdxKey(pEntry->ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type,
820,560,043✔
840
                             pEntry->uid, &pTagIdxKey, &nTagIdxKey);
820,575,931✔
841
  if (code) {
820,589,044✔
842
    metaErr(TD_VID(pMeta->pVnode), code);
843
    return code;
719,695,572✔
844
  }
719,695,572✔
845

846
  *ppTagIdxKey = pTagIdxKey;
719,695,572✔
847
  *pTagIdxKeySize = nTagIdxKey;
719,773,140✔
848
  return code;
849
}
719,774,784✔
850

722,428,554✔
851
static void metaFetchTagIdxKeyFree(STagIdxKey **ppTagIdxKey) {
2,644,384✔
852
  metaDestroyTagIdxKey(*ppTagIdxKey);
853
  *ppTagIdxKey = NULL;
5,288,768✔
854
}
2,644,384✔
855

856
static int32_t metaTagIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
857
  int32_t code = TSDB_CODE_SUCCESS;
2,644,384✔
858

2,644,384✔
859
  const SMetaEntry *pEntry = pParam->pEntry;
2,644,384✔
860
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
2,644,384✔
UNCOV
861

×
862
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
863
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
864
    const SSchema *pTagColumn = &pTagSchema->pSchema[0];
2,147,483,647✔
865

2,147,483,647✔
866
    STagVal tagVal = {
2,147,483,647✔
867
        .cid = pTagColumn->colId,
2,147,483,647✔
868
    };
869

2,147,483,647✔
870
    const void *pTagData = pEntry->ctbEntry.pTags;
1,597,012,914✔
871
    int32_t     nTagData = ((const STag *)pEntry->ctbEntry.pTags)->len;
872
    code = metaSaveJsonVarToIdx(pMeta, pEntry, pTagColumn);
873
    if (code) {
717,781,537✔
874
      metaErr(TD_VID(pMeta->pVnode), code);
717,659,335✔
UNCOV
875
    }
×
UNCOV
876
  } else {
×
877
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
878
      STagIdxKey    *pTagIdxKey = NULL;
879
      int32_t        nTagIdxKey;
717,659,335✔
880
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
717,816,836✔
UNCOV
881

×
UNCOV
882
      if (!IS_IDX_ON(pTagColumn)) {
×
UNCOV
883
        continue;
×
884
      }
885

717,816,836✔
886
      code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pTagIdxKey, &nTagIdxKey);
887
      if (code) {
888
        metaErr(TD_VID(pMeta->pVnode), code);
719,830,171✔
889
        return code;
890
      }
891

47,024,021✔
892
      code = tdbTbInsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn);
47,024,021✔
893
      if (code) {
894
        metaErr(TD_VID(pMeta->pVnode), code);
47,024,021✔
895
        metaFetchTagIdxKeyFree(&pTagIdxKey);
47,024,021✔
896
        return code;
47,024,021✔
897
      }
47,024,021✔
898
      metaFetchTagIdxKeyFree(&pTagIdxKey);
47,024,021✔
899
    }
47,024,021✔
900
  }
901
  return code;
47,024,021✔
902
}
2,130,514✔
903

904
static int32_t metaTagIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
905
  int32_t code = TSDB_CODE_SUCCESS;
44,893,507✔
906

10,636✔
907
  const SMetaEntry     *pEntry = pParam->pEntry;
10,636✔
UNCOV
908
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
×
UNCOV
909
  const SMetaEntry     *pSuperEntry = pParam->pSuperEntry;
×
910
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
911
  const STag           *pNewTags = (const STag *)pEntry->ctbEntry.pTags;
912
  const STag           *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags;
10,636✔
913

10,636✔
UNCOV
914
  if (pNewTags->len == pOldTags->len && !memcmp(pNewTags, pOldTags, pNewTags->len)) {
×
UNCOV
915
    return code;
×
916
  }
917

918
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
96,867,278✔
919
    code = metaDelJsonVarFromIdx(pMeta, pOldEntry, &pTagSchema->pSchema[0]);
51,984,407✔
920
    if (code) {
921
      metaErr(TD_VID(pMeta->pVnode), code);
51,984,407✔
922
      return code;
7,110,170✔
923
    }
924

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

×
934
      if (!IS_IDX_ON(pTagColumn)) {
935
        continue;
936
      }
44,874,237✔
937

44,874,237✔
UNCOV
938
      STagIdxKey *pOldTagIdxKey = NULL;
×
UNCOV
939
      int32_t     oldTagIdxKeySize = 0;
×
UNCOV
940
      STagIdxKey *pNewTagIdxKey = NULL;
×
941
      int32_t     newTagIdxKeySize = 0;
942

943
      code = metaFetchTagIdxKey(pMeta, pOldEntry, pTagColumn, &pOldTagIdxKey, &oldTagIdxKeySize);
44,874,237✔
944
      if (code) {
43,236,879✔
945
        metaErr(TD_VID(pMeta->pVnode), code);
43,236,879✔
946
        return code;
×
UNCOV
947
      }
×
UNCOV
948

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

×
UNCOV
956
      if (tagIdxKeyCmpr(pOldTagIdxKey, oldTagIdxKeySize, pNewTagIdxKey, newTagIdxKeySize)) {
×
UNCOV
957
        code = tdbTbDelete(pMeta->pTagIdx, pOldTagIdxKey, oldTagIdxKeySize, pMeta->txn);
×
958
        if (code) {
959
          metaErr(TD_VID(pMeta->pVnode), code);
960
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
961
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
44,874,237✔
962
          return code;
44,874,237✔
963
        }
964

965
        code = tdbTbInsert(pMeta->pTagIdx, pNewTagIdxKey, newTagIdxKeySize, NULL, 0, pMeta->txn);
44,893,507✔
966
        if (code) {
967
          metaErr(TD_VID(pMeta->pVnode), code);
968
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
11,025,057✔
969
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
11,025,057✔
970
          return code;
971
        }
11,025,057✔
972
      }
11,025,057✔
973

11,025,057✔
974
      metaFetchTagIdxKeyFree(&pOldTagIdxKey);
11,024,391✔
975
      metaFetchTagIdxKeyFree(&pNewTagIdxKey);
11,024,391✔
976
    }
11,024,391✔
977
  }
978
  return code;
11,026,922✔
979
}
782,316✔
980

782,316✔
981
static int32_t metaTagIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
782,316✔
UNCOV
982
  int32_t code = TSDB_CODE_SUCCESS;
×
983

984
  const SMetaEntry     *pEntry = pParam->pEntry;
985
  const SMetaEntry     *pChild = pParam->pOldEntry;
45,197,276✔
986
  const SMetaEntry     *pSuper = pParam->pSuperEntry;
34,956,258✔
987
  const SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
34,951,640✔
988
  const SSchema        *pTagColumn = NULL;
24,021,830✔
989
  const STag           *pTags = (const STag *)pChild->ctbEntry.pTags;
990

991
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
10,933,762✔
992
    pTagColumn = &pTagSchema->pSchema[0];
10,935,627✔
993
    code = metaDelJsonVarFromIdx(pMeta, pChild, pTagColumn);
994
    if (code) {
10,934,570✔
995
      metaErr(TD_VID(pMeta->pVnode), code);
10,923,754✔
UNCOV
996
    }
×
UNCOV
997
  } else {
×
998
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
999
      pTagColumn = &pTagSchema->pSchema[i];
1000
      if (!IS_IDX_ON(pTagColumn)) {
10,923,754✔
1001
        continue;
10,933,762✔
UNCOV
1002
      }
×
UNCOV
1003

×
UNCOV
1004
      STagIdxKey *pTagIdxKey = NULL;
×
1005
      int32_t     nTagIdxKey;
1006

10,933,762✔
1007
      code = metaFetchTagIdxKey(pMeta, pChild, pTagColumn, &pTagIdxKey, &nTagIdxKey);
1008
      if (code) {
1009
        metaErr(TD_VID(pMeta->pVnode), code);
11,023,334✔
1010
        return code;
1011
      }
1012

1013
      code = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
775,844,694✔
1014
      if (code) {
775,844,694✔
1015
        metaErr(TD_VID(pMeta->pVnode), code);
1016
        metaFetchTagIdxKeyFree(&pTagIdxKey);
1017
        return code;
775,844,694✔
1018
      }
19,086,759✔
1019
      metaFetchTagIdxKeyFree(&pTagIdxKey);
1020
    }
756,757,935✔
1021
  }
1022
  return code;
1023
}
775,846,000✔
1024

775,960,109✔
1025
// Btime Index
1026
static int32_t metaBtimeIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
1027
  int32_t code = TSDB_CODE_SUCCESS;
775,966,711✔
1028

730,801,893✔
1029
  const SMetaEntry *pEntry;
45,142,345✔
1030
  if (META_TABLE_OP_DELETE == op) {
45,142,345✔
1031
    pEntry = pParam->pOldEntry;
UNCOV
1032
  } else {
×
1033
    pEntry = pParam->pEntry;
1034
  }
1035

775,839,223✔
1036
  SBtimeIdxKey key = {
756,757,075✔
1037
      .uid = pEntry->uid,
19,082,148✔
UNCOV
1038
  };
×
1039

19,082,148✔
1040
  if (TSDB_CHILD_TABLE == pEntry->type || TSDB_VIRTUAL_CHILD_TABLE == pEntry->type) {
19,086,759✔
1041
    key.btime = pEntry->ctbEntry.btime;
UNCOV
1042
  } else if (TSDB_NORMAL_TABLE == pEntry->type || TSDB_VIRTUAL_NORMAL_TABLE == pEntry->type) {
×
1043
    key.btime = pEntry->ntbEntry.btime;
1044
  } else {
775,924,201✔
1045
    return TSDB_CODE_INVALID_PARA;
×
1046
  }
1047

775,889,842✔
1048
  if (META_TABLE_OP_INSERT == op) {
1049
    code = tdbTbInsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
1050
  } else if (META_TABLE_OP_UPDATA == op) {
756,790,596✔
1051
    code = tdbTbUpsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
756,790,596✔
1052
  } else if (META_TABLE_OP_DELETE == op) {
1053
    code = tdbTbDelete(pMeta->pBtimeIdx, &key, sizeof(key), pMeta->txn);
UNCOV
1054
  } else {
×
1055
    code = TSDB_CODE_INVALID_PARA;
×
1056
  }
1057
  if (code) {
1058
    metaErr(TD_VID(pMeta->pVnode), code);
19,088,624✔
1059
  }
19,088,624✔
1060
  return code;
1061
}
1062

1063
static int32_t metaBtimeIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
754,623,053✔
1064
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
754,623,053✔
1065
}
1066

754,670,768✔
1067
static int32_t metaBtimeIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
754,711,264✔
1068
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
754,634,965✔
1069
}
1070

754,729,540✔
1071
static int32_t metaBtimeIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
718,326,815✔
1072
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_DELETE);
718,321,013✔
1073
}
36,347,082✔
1074

36,347,082✔
1075
// TTL Index
36,347,082✔
1076
static int32_t metaTtlIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
UNCOV
1077
  const SMetaEntry *pEntry = pParam->pEntry;
×
1078

1079
  STtlUpdTtlCtx ctx = {
1080
      .uid = pEntry->uid,
754,706,267✔
1081
      .pTxn = pMeta->txn,
754,599,661✔
UNCOV
1082
  };
×
1083
  if (TSDB_CHILD_TABLE == pEntry->type) {
1084
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
754,572,531✔
1085
    ctx.changeTimeMs = pEntry->ctbEntry.btime;
1086
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
1087
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
754,646,450✔
1088
    ctx.changeTimeMs = pEntry->ntbEntry.btime;
754,646,450✔
1089
  } else {
1090
    return TSDB_CODE_INVALID_PARA;
1091
  }
1092

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

68,379,480✔
1100
static int32_t metaTtlIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
68,348,841✔
1101
  return metaTtlIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
64,668✔
1102
}
64,668✔
UNCOV
1103

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

1106
static int32_t metaTtlIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
64,668✔
1107
  int32_t code = TSDB_CODE_SUCCESS;
64,668✔
UNCOV
1108

×
1109
  const SMetaEntry *pEntry = pParam->pEntry;
1110
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
1111

1112
  if ((pEntry->type == TSDB_CHILD_TABLE && pOldEntry->ctbEntry.ttlDays != pEntry->ctbEntry.ttlDays) ||
68,379,480✔
1113
      (pEntry->type == TSDB_NORMAL_TABLE && pOldEntry->ntbEntry.ttlDays != pEntry->ntbEntry.ttlDays)) {
1114
    code = metaTtlIdxDelete(pMeta, pParam);
1115
    if (code) {
18,790,180✔
1116
      metaErr(TD_VID(pMeta->pVnode), code);
18,790,180✔
1117
    }
1118

18,790,180✔
1119
    code = metaTtlIdxInsert(pMeta, pParam);
18,790,180✔
1120
    if (code) {
18,790,180✔
1121
      metaErr(TD_VID(pMeta->pVnode), code);
18,791,833✔
1122
    }
1123
  }
1124

18,790,180✔
1125
  return TSDB_CODE_SUCCESS;
10,735,912✔
1126
}
8,054,268✔
1127

7,917,523✔
1128
static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
1129
  int32_t code = TSDB_CODE_SUCCESS;
136,745✔
1130

1131
  const SMetaEntry *pEntry = pParam->pOldEntry;
1132
  STtlDelTtlCtx     ctx = {
18,788,315✔
1133
          .uid = pEntry->uid,
18,651,570✔
1134
          .pTxn = pMeta->txn,
18,655,088✔
UNCOV
1135
  };
×
1136

1137
  if (TSDB_CHILD_TABLE == pEntry->type) {
1138
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
1139
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
18,791,833✔
1140
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
1141
  } else {
1142
    code = TSDB_CODE_INVALID_PARA;
807,377,321✔
1143
  }
1144

807,377,321✔
1145
  if (TSDB_CODE_SUCCESS == code) {
807,389,395✔
1146
    int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
807,500,373✔
1147
    if (ret < 0) {
526,293,265✔
1148
      metaError("vgId:%d, failed to delete ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid,
526,292,027✔
UNCOV
1149
                tstrerror(ret));
×
1150
    }
1151
  }
1152
  return code;
1153
}
1154

807,727,832✔
1155
static void metaTimeSeriesNotifyCheck(SMeta *pMeta) {
1156
#if defined(TD_ENTERPRISE)
1157
  int64_t nTimeSeries = metaGetTimeSeriesNum(pMeta, 0);
1158
  int64_t deltaTS = nTimeSeries - pMeta->pVnode->config.vndStats.numOfReportedTimeSeries;
1159
  if (deltaTS > tsTimeSeriesThreshold) {
1160
    if (0 == atomic_val_compare_exchange_8(&dmNotifyHdl.state, 1, 2)) {
1161
      if (tsem_post(&dmNotifyHdl.sem) != 0) {
1162
        metaError("vgId:%d, failed to post semaphore, errno:%d", TD_VID(pMeta->pVnode), ERRNO);
1163
      }
1164
    }
1165
  }
1166
#endif
1167
}
1168

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

382,109,439✔
1227
static int32_t metaHandleSuperTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
382,178,740✔
1228
  int32_t code = TSDB_CODE_SUCCESS;
1229

1230
  SMetaTableOp ops[] = {
382,096,110✔
1231
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
381,971,970✔
UNCOV
1232
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: here should be insert
×
UNCOV
1233
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
×
1234
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1235
      {META_SUID_IDX, META_TABLE_OP_INSERT},      //
1236
  };
1237

76,459,851✔
1238
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1239
    SMetaTableOp          *op = &ops[i];
76,163,562✔
1240
    const SMetaHandleParam param = {
76,163,562✔
1241
        .pEntry = pEntry,
1242
    };
76,163,562✔
1243
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
76,463,866✔
1244
    if (TSDB_CODE_SUCCESS != code) {
76,427,909✔
1245
      metaErr(TD_VID(pMeta->pVnode), code);
1246
      return code;
76,418,739✔
1247
    }
76,418,739✔
1248
  }
1249

76,481,850✔
1250
  return code;
1251
}
UNCOV
1252
static int32_t metaHandleSuperTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
×
1253
  int32_t code = TSDB_CODE_SUCCESS;
1254

76,510,742✔
1255
  metaWLock(pMeta);
1256
  code = metaHandleSuperTableCreateImpl(pMeta, pEntry);
1257
  metaULock(pMeta);
36,313,053✔
1258

36,313,053✔
1259
  if (TSDB_CODE_SUCCESS == code) {
1260
    pMeta->pVnode->config.vndStats.numOfSTables++;
36,313,053✔
1261

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

254,191,371✔
1270
static int32_t metaHandleNormalTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
217,878,318✔
1271
  int32_t code = TSDB_CODE_SUCCESS;
1272

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

1282
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1283
    SMetaTableOp *op = &ops[i];
36,313,053✔
1284

1285
    SMetaHandleParam param = {
36,313,053✔
1286
        .pEntry = pEntry,
36,313,053✔
1287
    };
1288

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

36,313,053✔
1296
  return code;
36,313,053✔
1297
}
1298
static int32_t metaHandleNormalTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
36,313,053✔
1299
  int32_t code = TSDB_CODE_SUCCESS;
145,070✔
1300

145,070✔
UNCOV
1301
  // update TDB
×
1302
  metaWLock(pMeta);
1303
  code = metaHandleNormalTableCreateImpl(pMeta, pEntry);
1304
  metaULock(pMeta);
36,313,053✔
1305

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

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

1324
static int32_t metaHandleChildTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry, const SMetaEntry *pSuperEntry) {
2,147,483,647✔
1325
  int32_t code = TSDB_CODE_SUCCESS;
2,147,483,647✔
1326

1327
  SMetaTableOp ops[] = {
2,147,483,647✔
1328
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},  //
1329
      {META_UID_IDX, META_TABLE_OP_INSERT},      //
1330
      {META_NAME_IDX, META_TABLE_OP_INSERT},     //
1331
      {META_CHILD_IDX, META_TABLE_OP_INSERT},    //
1332
      {META_TAG_IDX, META_TABLE_OP_INSERT},      //
2,147,483,647✔
1333
      {META_BTIME_IDX, META_TABLE_OP_INSERT},    //
2,147,483,647✔
UNCOV
1334
      {META_TTL_IDX, META_TABLE_OP_INSERT},      //
×
UNCOV
1335
  };
×
1336

1337
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1338
    SMetaTableOp *op = &ops[i];
1339

718,320,870✔
1340
    SMetaHandleParam param = {
718,295,968✔
1341
        .pEntry = pEntry,
718,226,080✔
1342
        .pSuperEntry = pSuperEntry,
718,286,952✔
UNCOV
1343
    };
×
1344

1345
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
1346
    if (TSDB_CODE_SUCCESS != code) {
718,286,952✔
1347
      metaErr(TD_VID(pMeta->pVnode), code);
718,332,841✔
1348
      return code;
×
1349
    }
1350
  }
1351

718,274,021✔
1352
  if (TSDB_CODE_SUCCESS == code) {
1353
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
1354
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
718,148,666✔
1355
    if (ret < 0) {
718,148,666✔
1356
      metaErr(TD_VID(pMeta->pVnode), ret);
718,148,666✔
1357
    }
1358

1359
    ret = metaStableTagFilterCacheUpdateUid(
718,241,341✔
1360
      pMeta, pEntry, STABLE_TAG_FILTER_CACHE_ADD_TABLE);
718,330,538✔
NEW
1361
    if (ret < 0) {
×
NEW
1362
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1363
    }
1364

1365
    ret = metaTbGroupCacheClear(pMeta, pSuperEntry->uid);
1366
    if (ret < 0) {
718,330,538✔
1367
      metaErr(TD_VID(pMeta->pVnode), ret);
718,297,732✔
1368
    }
718,206,756✔
1369
  }
1370
  return code;
1371
}
718,159,574✔
1372

718,159,574✔
1373
static int32_t metaHandleChildTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
1374
  int32_t     code = TSDB_CODE_SUCCESS;
718,229,755✔
1375
  SMetaEntry *pSuperEntry = NULL;
718,343,362✔
1376

718,345,203✔
1377
  // get the super table entry
718,249,321✔
UNCOV
1378
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
×
1379
  if (code) {
1380
    metaErr(TD_VID(pMeta->pVnode), code);
718,249,321✔
1381
    return code;
1382
  }
1383

718,026,756✔
1384
  // update TDB
42,275,127✔
1385
  metaWLock(pMeta);
42,286,727✔
UNCOV
1386
  code = metaHandleChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
×
1387
  metaULock(pMeta);
1388

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

×
1393
    if (!metaTbInFilterCache(pMeta, pSuperEntry->name, 1)) {
1394
      int32_t nCols = 0;
718,286,918✔
1395
      int32_t ret = metaGetStbStats(pMeta->pVnode, pSuperEntry->uid, 0, &nCols, 0);
718,354,846✔
1396
      if (ret < 0) {
718,319,965✔
1397
        metaErr(TD_VID(pMeta->pVnode), ret);
1398
      }
1399
      pMeta->pVnode->config.vndStats.numOfTimeSeries += (nCols > 0 ? nCols - 1 : 0);
763,962✔
1400
    }
763,962✔
1401

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

1410
  } else {
4,583,772✔
1411
    metaErr(TD_VID(pMeta->pVnode), code);
3,819,810✔
1412
  }
1413
  metaTimeSeriesNotifyCheck(pMeta);
3,819,810✔
1414
  metaFetchEntryFree(&pSuperEntry);
1415
  return code;
1416
}
1417

3,819,810✔
1418
static int32_t metaHandleVirtualNormalTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
3,819,810✔
UNCOV
1419
  int32_t code = TSDB_CODE_SUCCESS;
×
UNCOV
1420

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

763,962✔
1429
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1430
    SMetaTableOp *op = &ops[i];
1431

763,962✔
1432
    SMetaHandleParam param = {
763,962✔
1433
        .pEntry = pEntry,
763,962✔
1434
    };
1435

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

1443
  return code;
1444
}
1,459,734✔
1445

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

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

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

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

8,758,404✔
UNCOV
1467
  SMetaTableOp ops[] = {
×
UNCOV
1468
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},  //
×
1469
      {META_UID_IDX, META_TABLE_OP_INSERT},      //
1470
      {META_NAME_IDX, META_TABLE_OP_INSERT},     //
1471
      {META_CHILD_IDX, META_TABLE_OP_INSERT},    //
1472
      {META_TAG_IDX, META_TABLE_OP_INSERT},      //
1,459,734✔
1473
      {META_BTIME_IDX, META_TABLE_OP_INSERT},    //
1,459,734✔
1474
  };
1,459,734✔
1475

1,459,734✔
UNCOV
1476
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
×
1477
    SMetaTableOp *op = &ops[i];
1478

1479
    SMetaHandleParam param = {
1,459,734✔
1480
        .pEntry = pEntry,
1,459,734✔
UNCOV
1481
        .pSuperEntry = pSuperEntry,
×
1482
    };
1483

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

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

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

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

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

1515
  return code;
1516
}
1,459,734✔
1517

1,459,734✔
1518
static int32_t metaHandleVirtualChildTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
1519
  int32_t     code = TSDB_CODE_SUCCESS;
1520
  SMetaEntry *pSuperEntry = NULL;
7,883,494✔
1521

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

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

47,300,964✔
1534
  // update other stuff
39,417,470✔
1535
  if (TSDB_CODE_SUCCESS == code) {
39,417,470✔
1536
    pMeta->pVnode->config.vndStats.numOfVCTables++;
39,417,470✔
UNCOV
1537
  } else {
×
1538
    metaErr(TD_VID(pMeta->pVnode), code);
×
1539
  }
1540

1541
  metaFetchEntryFree(&pSuperEntry);
1542
  return code;
7,883,494✔
1543
}
1544

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

7,883,494✔
1548
  SMetaTableOp ops[] = {
1549
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1550
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
7,883,494✔
1551
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
7,883,494✔
UNCOV
1552
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
×
UNCOV
1553
      {META_TTL_IDX, META_TABLE_OP_DELETE},      //
×
1554

1555
      // {META_SCHEMA_TABLE, META_TABLE_OP_DELETE},  //
1556
  };
7,883,494✔
1557

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

×
UNCOV
1567
  return code;
×
UNCOV
1568
}
×
1569

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

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

1581
  SMetaHandleParam param = {
1582
      .pEntry = pEntry,
1583
      .pOldEntry = pOldEntry,
1584
  };
7,883,494✔
UNCOV
1585

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

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

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

1609
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
1610
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pOldEntry->uid, 0, NULL);
1611
    if (ret < 0) {
1612
      metaErr(TD_VID(pMeta->pVnode), ret);
86,606,658✔
1613
    }
75,903,250✔
1614
  }
1615

75,903,250✔
1616
  metaFetchEntryFree(&pOldEntry);
5,631,218✔
1617
  return code;
1618
}
1619

70,272,800✔
1620
static int32_t metaHandleChildTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam, bool superDropped) {
70,271,333✔
1621
  int32_t code = TSDB_CODE_SUCCESS;
136,745✔
1622

136,745✔
1623
  const SMetaEntry *pEntry = pParam->pEntry;
1624
  const SMetaEntry *pChild = pParam->pOldEntry;
1625
  const SMetaEntry *pSuper = pParam->pSuperEntry;
1626

10,703,408✔
1627
  SMetaTableOp ops[] = {
10,708,901✔
1628
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
10,702,359✔
1629
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
10,708,056✔
UNCOV
1630
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
×
1631
      {META_CHILD_IDX, META_TABLE_OP_DELETE},    //
1632
      {META_TAG_IDX, META_TABLE_OP_DELETE},      //
1633
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
10,708,056✔
1634
      {META_TTL_IDX, META_TABLE_OP_DELETE},      //
10,704,851✔
UNCOV
1635
  };
×
1636

1637
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
10,705,273✔
1638
    SMetaTableOp *op = &ops[i];
1639

1640
    if (op->table == META_ENTRY_TABLE && superDropped) {
10,845,646✔
1641
      continue;
10,845,646✔
1642
    }
10,845,646✔
1643

10,845,646✔
1644
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
1645
    if (code) {
1646
      metaErr(TD_VID(pMeta->pVnode), code);
10,845,646✔
1647
      return code;
10,842,372✔
UNCOV
1648
    }
×
UNCOV
1649
  }
×
1650

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

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

1664
  ret = metaTbGroupCacheClear(pMeta, pSuper->uid);
1665
  if (ret < 0) {
1666
    metaErr(TD_VID(pMeta->pVnode), ret);
1667
  }
10,843,781✔
1668
  return code;
10,843,781✔
1669
}
10,839,839✔
1670

10,838,430✔
1671
static int32_t metaHandleChildTableDrop(SMeta *pMeta, const SMetaEntry *pEntry, bool superDropped) {
136,745✔
1672
  int32_t     code = TSDB_CODE_SUCCESS;
136,745✔
1673
  SMetaEntry *pChild = NULL;
136,745✔
1674
  SMetaEntry *pSuper = NULL;
136,745✔
1675

1676
  // fetch old entry
1677
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pChild);
1678
  if (code) {
10,701,685✔
1679
    metaErr(TD_VID(pMeta->pVnode), code);
10,707,844✔
1680
    return code;
10,707,844✔
1681
  }
10,706,151✔
1682

10,703,872✔
1683
  // fetch super entry
1684
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
1685
  if (code) {
1686
    metaErr(TD_VID(pMeta->pVnode), code);
10,705,273✔
1687
    metaFetchEntryFree(&pChild);
106,068✔
1688
    return code;
106,068✔
UNCOV
1689
  }
×
1690

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

1697
  // do the drop
1698
  metaWLock(pMeta);
1699
  code = metaHandleChildTableDropImpl(pMeta, &param, superDropped);
1700
  metaULock(pMeta);
1701
  if (code) {
1702
    metaErr(TD_VID(pMeta->pVnode), code);
1703
    metaFetchEntryFree(&pChild);
1704
    metaFetchEntryFree(&pSuper);
1705
    return code;
10,706,722✔
1706
  }
10,699,820✔
1707

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

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

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

181,836✔
1732
  if ((type == TSDB_CHILD_TABLE) && tbUid) {
1733
    *tbUid = uid;
1734
  }
181,836✔
1735
#endif
181,836✔
1736
  metaFetchEntryFree(&pChild);
181,836✔
1737
  metaFetchEntryFree(&pSuper);
1738
  return code;
1739
}
181,836✔
1740

181,836✔
UNCOV
1741
static int32_t metaHandleVirtualNormalTableDropImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
×
UNCOV
1742
  int32_t code = TSDB_CODE_SUCCESS;
×
1743

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

1750
      // {META_SCHEMA_TABLE, META_TABLE_OP_DELETE},  //
1751
  };
181,836✔
1752

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

181,836✔
1762
  return code;
1763
}
1764

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

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

×
1776
  SMetaHandleParam param = {
1777
      .pEntry = pEntry,
1778
      .pOldEntry = pOldEntry,
1779
  };
181,836✔
1780

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

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

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

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

×
1810
  metaFetchEntryFree(&pOldEntry);
1811
  return code;
1812
}
1813

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

181,276✔
UNCOV
1817
  const SMetaEntry *pEntry = pParam->pEntry;
×
1818
  const SMetaEntry *pChild = pParam->pOldEntry;
1819
  const SMetaEntry *pSuper = pParam->pSuperEntry;
1820

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

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

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

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

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

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

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

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

×
UNCOV
1867
  return code;
×
1868
}
1869

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

1875
  // fetch old entry
8,112,264✔
1876
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pChild);
8,112,264✔
1877
  if (code) {
8,112,264✔
1878
    metaErr(TD_VID(pMeta->pVnode), code);
8,129,089✔
1879
    return code;
8,126,893✔
1880
  }
1881

8,123,013✔
1882
  // fetch super entry
8,124,763✔
UNCOV
1883
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
×
1884
  if (code) {
1885
    metaErr(TD_VID(pMeta->pVnode), code);
1886
    metaFetchEntryFree(&pChild);
8,122,243✔
1887
    return code;
8,117,362✔
1888
  }
8,112,722✔
UNCOV
1889

×
UNCOV
1890
  SMetaHandleParam param = {
×
UNCOV
1891
      .pEntry = pEntry,
×
1892
      .pOldEntry = pChild,
1893
      .pSuperEntry = pSuper,
1894
  };
8,112,722✔
1895

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

18,546,376✔
1907
  metaFetchEntryFree(&pChild);
6,300,236✔
1908
  metaFetchEntryFree(&pSuper);
1909
  return code;
1910
}
12,234,034✔
1911

1,457,396✔
1912
int32_t metaGetChildUidsOfSuperTable(SMeta *pMeta, tb_uid_t suid, SArray **childList) {
10,779,171✔
1913
  int32_t code = TSDB_CODE_SUCCESS;
1,840,701✔
1914
  void   *key = NULL;
1915
  int32_t keySize = 0;
1916
  int32_t c;
17,896,192✔
UNCOV
1917

×
UNCOV
1918
  *childList = taosArrayInit(64, sizeof(tb_uid_t));
×
UNCOV
1919
  if (*childList == NULL) {
×
1920
    return terrno;
×
UNCOV
1921
  }
×
1922

1923
  TBC *cursor = NULL;
1924
  code = tdbTbcOpen(pMeta->pCtbIdx, &cursor, NULL);
1925
  if (code) {
8,140,937✔
1926
    taosArrayDestroy(*childList);
8,133,446✔
1927
    *childList = NULL;
8,133,582✔
1928
    return code;
1929
  }
1930

7,514,934✔
1931
  int32_t rc = tdbTbcMoveTo(cursor,
7,514,934✔
1932
                            &(SCtbIdxKey){
7,514,934✔
1933
                                .suid = suid,
1934
                                .uid = INT64_MIN,
7,517,314✔
1935
                            },
1936
                            sizeof(SCtbIdxKey), &c);
1937
  if (rc < 0) {
1938
    tdbTbcClose(cursor);
1939
    return 0;
1940
  }
1941

1942
  for (;;) {
1943
    if (tdbTbcNext(cursor, &key, &keySize, NULL, NULL) < 0) {
37,581,519✔
1944
      break;
30,066,026✔
1945
    }
1946

30,066,026✔
1947
    if (((SCtbIdxKey *)key)->suid < suid) {
30,065,614✔
UNCOV
1948
      continue;
×
UNCOV
1949
    } else if (((SCtbIdxKey *)key)->suid > suid) {
×
1950
      break;
1951
    }
1952

1953
    if (taosArrayPush(*childList, &(((SCtbIdxKey *)key)->uid)) == NULL) {
7,515,493✔
1954
      tdbFreeClear(key);
7,515,493✔
1955
      tdbTbcClose(cursor);
2,396,566✔
1956
      taosArrayDestroy(*childList);
1957
      *childList = NULL;
7,517,314✔
1958
      return terrno;
1959
    }
1960
  }
21,910,807✔
1961

21,910,807✔
1962
  tdbTbcClose(cursor);
1963
  tdbFreeClear(key);
21,910,807✔
1964
  return code;
1965
}
21,910,807✔
1966

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

1971
  SMetaTableOp ops[] = {
109,554,035✔
1972
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
87,643,228✔
1973
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
87,643,228✔
1974
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
87,643,228✔
UNCOV
1975
      {META_SUID_IDX, META_TABLE_OP_DELETE},     //
×
UNCOV
1976

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

1980
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1981
    SMetaTableOp *op = &ops[i];
1982

1983
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
1984
    if (TSDB_CODE_SUCCESS != code) {
21,910,807✔
1985
      metaErr(TD_VID(pMeta->pVnode), code);
1986
      return code;
1987
    }
1,385,371✔
1988
  }
1,385,371✔
1989

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

1997
static int32_t metaHandleNormalTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
5,541,484✔
1998
  int32_t code = TSDB_CODE_SUCCESS;
4,156,113✔
1999

4,156,113✔
2000
  const SMetaEntry *pEntry = pParam->pEntry;
4,156,113✔
UNCOV
2001

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

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

2027
  const SMetaEntry *pEntry = pParam->pEntry;
2,776,740✔
2028

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

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

2053
  const SMetaEntry *pEntry = pParam->pEntry;
46,468,673✔
2054
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
46,468,673✔
2055
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
46,468,673✔
2056

2057
  SMetaTableOp ops[] = {
46,468,673✔
2058
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},  //
2059
      {META_UID_IDX, META_TABLE_OP_UPDATA},      //
2060
      {META_TAG_IDX, META_TABLE_OP_UPDATA},      //
2061
      {META_CHILD_IDX, META_TABLE_OP_UPDATA},    //
2062
  };
2063

2064
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
2065
    SMetaTableOp *op = &ops[i];
278,812,038✔
2066
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
232,343,365✔
2067
    if (code) {
232,343,365✔
2068
      metaErr(TD_VID(pMeta->pVnode), code);
232,343,365✔
2069
      return code;
×
UNCOV
2070
    }
×
2071
  }
2072

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

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

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

62,544,256✔
2093
  if (metaRefDbsCacheClear(pMeta, pSuperEntry->uid) < 0) {
62,576,667✔
2094
    metaErr(TD_VID(pMeta->pVnode), code);
2095
  }
62,585,820✔
2096
  return code;
2097
}
2098

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

250,061,551✔
2102
  const SMetaEntry *pEntry = pParam->pEntry;
187,560,817✔
2103
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
187,657,470✔
2104
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
187,501,795✔
UNCOV
2105

×
UNCOV
2106
  SMetaTableOp ops[] = {
×
2107
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},  //
2108
      {META_UID_IDX, META_TABLE_OP_UPDATA},      //
2109
      {META_TAG_IDX, META_TABLE_OP_UPDATA},      //
2110
      {META_CHILD_IDX, META_TABLE_OP_UPDATA},    //
62,500,734✔
2111
      {META_TTL_IDX, META_TABLE_OP_UPDATA},      //
62,520,623✔
2112
  };
62,505,076✔
2113

2114
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
2115
    SMetaTableOp *op = &ops[i];
62,497,065✔
2116
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
2117
    if (code) {
2118
      metaErr(TD_VID(pMeta->pVnode), code);
62,489,016✔
2119
      return code;
62,489,016✔
2120
    }
2121
  }
62,489,016✔
2122

2123
  if (metaUidCacheClear(pMeta, pSuperEntry->uid) < 0) {
62,520,735✔
2124
    metaErr(TD_VID(pMeta->pVnode), code);
62,568,529✔
UNCOV
2125
  }
×
UNCOV
2126

×
2127
  // update stable tag filter cache: drop old then add new
2128
  code = metaStableTagFilterCacheUpdateUid(
2129
    pMeta, pOldEntry, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
62,568,529✔
2130
  if (TSDB_CODE_SUCCESS != code) {
2131
    metaErr(TD_VID(pMeta->pVnode), code);
2132
  }
2133
  code = metaStableTagFilterCacheUpdateUid(
62,560,390✔
2134
    pMeta, pEntry, STABLE_TAG_FILTER_CACHE_ADD_TABLE);
62,556,031✔
2135
  if (TSDB_CODE_SUCCESS != code) {
62,497,405✔
2136
    metaErr(TD_VID(pMeta->pVnode), code);
62,376,560✔
NEW
2137
  }
×
NEW
2138

×
2139
  if (metaTbGroupCacheClear(pMeta, pSuperEntry->uid) < 0) {
×
2140
    metaErr(TD_VID(pMeta->pVnode), code);
2141
  }
2142
  return code;
62,376,560✔
2143
#if 0
62,516,590✔
2144
  if (metaUpdateChangeTime(pMeta, ctbEntry.uid, pReq->ctimeMs) < 0) {
62,530,686✔
2145
    metaError("meta/table: failed to update change time:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
62,530,686✔
2146
  }
2147
#endif
62,496,410✔
2148
}
353,554✔
2149

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

2153
  const SMetaEntry *pEntry = pParam->pEntry;
2154
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
2155

353,554✔
2156
  SMetaTableOp ops[] = {
192,056✔
2157
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
192,056✔
2158
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
2159
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
190,811✔
2160
  };
192,056✔
UNCOV
2161

×
UNCOV
2162
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
×
UNCOV
2163
    SMetaTableOp *op = &ops[i];
×
2164
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
2165
    if (code) {
192,056✔
2166
      metaErr(TD_VID(pMeta->pVnode), code);
192,056✔
2167
      return code;
2168
    }
161,498✔
2169
  }
161,498✔
2170

161,498✔
2171
  if (TSDB_CODE_SUCCESS == code) {
161,498✔
2172
    metaUpdateStbStats(pMeta, pEntry->uid, 0, pEntry->stbEntry.schemaRow.nCols - pOldEntry->stbEntry.schemaRow.nCols,
161,498✔
2173
                       pEntry->stbEntry.keep);
2174
  }
1,340,038✔
2175

1,274,568✔
2176
  return code;
96,028✔
2177
}
96,028✔
2178

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

161,498✔
2182
  SMetaEntry *pOldEntry = NULL;
96,028✔
2183

96,028✔
UNCOV
2184
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
×
UNCOV
2185
  if (code) {
×
2186
    metaErr(TD_VID(pMeta->pVnode), code);
×
2187
    return code;
2188
  }
96,028✔
2189

96,028✔
2190
  SMetaHandleParam param = {
2191
      .pEntry = pEntry,
2192
      .pOldEntry = pOldEntry,
2193
  };
353,554✔
2194
  metaWLock(pMeta);
2195
  code = metaHandleSuperTableUpdateImpl(pMeta, &param);
353,554✔
2196
  metaULock(pMeta);
353,554✔
2197
  if (code) {
2198
    metaErr(TD_VID(pMeta->pVnode), code);
2199
    metaFetchEntryFree(&pOldEntry);
62,511,529✔
2200
    return code;
43,118,790✔
2201
  }
43,134,415✔
2202

43,122,540✔
UNCOV
2203
  int     nCols = pEntry->stbEntry.schemaRow.nCols;
×
2204
  int     onCols = pOldEntry->stbEntry.schemaRow.nCols;
2205
  int32_t deltaCol = nCols - onCols;
2206
  bool    updStat = deltaCol != 0 && !TABLE_IS_VIRTUAL(pEntry->flags) && !metaTbInFilterCache(pMeta, pEntry->name, 1);
43,122,540✔
2207

43,124,670✔
2208
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
2209
    STsdb  *pTsdb = pMeta->pVnode->pTsdb;
62,485,327✔
2210
    SArray *uids = NULL; /*taosArrayInit(8, sizeof(int64_t));
62,387,495✔
2211
     if (uids == NULL) {
2212
       metaErr(TD_VID(pMeta->pVnode), code);
2213
       metaFetchEntryFree(&pOldEntry);
46,468,673✔
2214
       return terrno;
46,468,673✔
2215
       }*/
2216
    if (deltaCol == 1) {
46,468,673✔
2217
      int16_t cid = pEntry->stbEntry.schemaRow.pSchema[nCols - 1].colId;
46,468,673✔
2218
      int8_t  col_type = pEntry->stbEntry.schemaRow.pSchema[nCols - 1].type;
2219

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

46,468,673✔
UNCOV
2242
      if (cid != -1) {
×
UNCOV
2243
        code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &uids);
×
UNCOV
2244
        if (code) {
×
2245
          metaErr(TD_VID(pMeta->pVnode), code);
×
2246
          metaFetchEntryFree(&pOldEntry);
2247
          return code;
2248
        }
46,468,673✔
2249
        if (pTsdb) {
46,468,673✔
2250
          TAOS_CHECK_RETURN(tsdbCacheDropSTableColumn(pTsdb, uids, cid, hasPrimaryKey));
46,468,673✔
2251
        }
2252
      }
2253
    }
21,910,807✔
2254
    if (uids) taosArrayDestroy(uids);
21,910,807✔
2255

21,910,807✔
2256
    if (pTsdb) {
2257
      tsdbCacheInvalidateSchema(pTsdb, pEntry->uid, -1, pEntry->stbEntry.schemaRow.version);
2258
    }
21,910,807✔
2259
  }
21,910,807✔
UNCOV
2260
  if (updStat) {
×
UNCOV
2261
    int64_t ctbNum = 0;
×
2262
    int32_t ret = metaGetStbStats(pMeta->pVnode, pEntry->uid, &ctbNum, 0, 0);
2263
    if (ret < 0) {
2264
      metaError("vgId:%d, failed to get stb stats:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
2265
                pEntry->uid, tstrerror(ret));
21,910,807✔
2266
    }
2267
    pMeta->pVnode->config.vndStats.numOfTimeSeries += (ctbNum * deltaCol);
2268
    if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
2269
  }
21,910,807✔
2270
  metaFetchEntryFree(&pOldEntry);
21,910,807✔
2271
  return code;
21,910,807✔
2272
}
21,910,807✔
UNCOV
2273

×
UNCOV
2274
static int32_t metaHandleChildTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
×
UNCOV
2275
  int32_t code = TSDB_CODE_SUCCESS;
×
2276

2277
  SMetaEntry *pOldEntry = NULL;
2278
  SMetaEntry *pSuperEntry = NULL;
2279

21,910,807✔
2280
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
188,257✔
2281
  if (code) {
2282
    metaErr(TD_VID(pMeta->pVnode), code);
2283
    return code;
2284
  }
2285

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

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

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

2309
  metaFetchEntryFree(&pOldEntry);
21,910,807✔
2310
  metaFetchEntryFree(&pSuperEntry);
21,910,807✔
2311
  return code;
21,910,807✔
2312
}
21,910,807✔
2313

21,910,807✔
2314
static int32_t metaHandleNormalTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
2315
  int32_t     code = TSDB_CODE_SUCCESS;
2316
  SMetaEntry *pOldEntry = NULL;
1,385,371✔
2317

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

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

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

555,348✔
UNCOV
2354
      if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
×
UNCOV
2355
        int16_t cid = pColumn->colId;
×
2356

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

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

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

7,499,982✔
2388
  // handle update
7,499,982✔
2389
  SMetaHandleParam param = {
7,504,248✔
2390
      .pEntry = pEntry,
2391
      .pOldEntry = pOldEntry,
7,502,899✔
2392
  };
7,501,295✔
UNCOV
2393
  metaWLock(pMeta);
×
UNCOV
2394
  code = metaHandleVirtualNormalTableUpdateImpl(pMeta, &param);
×
2395
  metaULock(pMeta);
2396
  if (code) {
2397
    metaErr(TD_VID(pMeta->pVnode), code);
7,501,295✔
2398
    metaFetchEntryFree(&pOldEntry);
7,492,016✔
2399
    return code;
×
UNCOV
2400
  }
×
UNCOV
2401

×
2402
  metaTimeSeriesNotifyCheck(pMeta);
2403
  metaFetchEntryFree(&pOldEntry);
2404
  return code;
7,492,016✔
2405
}
16,568✔
2406

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

2410
  SMetaEntry *pOldEntry = NULL;
13,139,309✔
2411
  SMetaEntry *pSuperEntry = NULL;
5,631,218✔
2412

5,631,218✔
2413
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
5,631,218✔
2414
  if (code) {
2415
    metaErr(TD_VID(pMeta->pVnode), code);
2416
    return code;
2417
  }
5,630,552✔
2418

5,624,997✔
2419
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
136,745✔
2420
  if (code) {
2421
    metaErr(TD_VID(pMeta->pVnode), code);
2422
    metaFetchEntryFree(&pOldEntry);
2423
    return code;
2424
  }
7,517,314✔
2425

2426
  SMetaHandleParam param = {
2427
      .pEntry = pEntry,
2428
      .pOldEntry = pOldEntry,
7,517,314✔
2429
      .pSuperEntry = pSuperEntry,
7,516,648✔
2430
  };
7,517,314✔
2431

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

2442
  metaFetchEntryFree(&pOldEntry);
7,517,314✔
2443
  metaFetchEntryFree(&pSuperEntry);
7,517,314✔
2444
  return code;
7,515,629✔
2445
}
2446

2447
static int32_t metaHandleSuperTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
986,691,523✔
2448
  int32_t     code = TSDB_CODE_SUCCESS;
986,691,523✔
2449
  SArray     *childList = NULL;
986,691,523✔
2450
  SMetaEntry *pOldEntry = NULL;
987,090,141✔
2451

987,067,431✔
2452
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
2453
  if (code) {
986,853,169✔
2454
    metaErr(TD_VID(pMeta->pVnode), code);
15,903✔
2455
    return code;
×
2456
  }
2457

2458
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childList);
986,841,392✔
2459
  if (code) {
965,652,028✔
2460
    metaErr(TD_VID(pMeta->pVnode), code);
965,652,028✔
2461
    metaFetchEntryFree(&pOldEntry);
132,821,723✔
2462
    return code;
2463
  }
2464

965,849,229✔
2465
  if (pMeta->pVnode->pTsdb && tsdbCacheDropSubTables(pMeta->pVnode->pTsdb, childList, pEntry->uid) < 0) {
138,824,586✔
2466
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
138,824,586✔
2467
              pEntry->uid, tstrerror(terrno));
62,435,452✔
2468
  }
2469

76,389,134✔
2470
  // loop to drop all child tables
2471
  for (int32_t i = 0; i < taosArrayGetSize(childList); i++) {
138,959,597✔
2472
    SMetaEntry childEntry = {
2473
        .version = pEntry->version,
764,636,368✔
2474
        .uid = *(tb_uid_t *)taosArrayGet(childList, i),
764,636,368✔
2475
        .type = -TSDB_CHILD_TABLE,
46,468,673✔
2476
    };
2477

718,167,695✔
2478
    code = metaHandleChildTableDrop(pMeta, &childEntry, true);
2479
    if (code) {
764,781,436✔
2480
      metaErr(TD_VID(pMeta->pVnode), code);
2481
    }
58,223,860✔
2482
  }
58,223,860✔
2483

21,910,807✔
2484
  // do drop super table
2485
  SMetaHandleParam param = {
36,313,053✔
2486
      .pEntry = pEntry,
2487
      .pOldEntry = pOldEntry,
58,223,860✔
2488
  };
2489
  metaWLock(pMeta);
2,149,333✔
2490
  code = metaHandleSuperTableDropImpl(pMeta, &param);
2,149,333✔
2491
  metaULock(pMeta);
1,385,371✔
2492
  if (code) {
2493
    metaErr(TD_VID(pMeta->pVnode), code);
763,962✔
2494
    taosArrayDestroy(childList);
2495
    metaFetchEntryFree(&pOldEntry);
2,149,333✔
2496
    return code;
2497
  }
2,015,082✔
2498

2,015,082✔
2499
  // do other stuff
555,348✔
2500
  // metaUpdTimeSeriesNum(pMeta);
2501

1,459,734✔
2502
  // free resource and return
2503
  taosArrayDestroy(childList);
2,015,082✔
2504
  metaFetchEntryFree(&pOldEntry);
UNCOV
2505
  return code;
×
UNCOV
2506
}
×
UNCOV
2507

×
2508
int32_t metaHandleEntry2(SMeta *pMeta, const SMetaEntry *pEntry) {
2509
  int32_t   code = TSDB_CODE_SUCCESS;
2510
  int32_t   vgId = TD_VID(pMeta->pVnode);
2511
  SMetaInfo info = {0};
20,962,674✔
2512
  int8_t    type = pEntry->type > 0 ? pEntry->type : -pEntry->type;
7,501,640✔
2513

7,501,640✔
2514
  if (NULL == pMeta || NULL == pEntry) {
7,517,314✔
2515
    metaError("%s failed at %s:%d since invalid parameter", __func__, __FILE__, __LINE__);
2516
    return TSDB_CODE_INVALID_PARA;
5,214,428✔
2517
  }
5,214,428✔
2518

5,210,840✔
2519
  if (pEntry->type > 0) {
2520
    bool isExist = false;
7,883,494✔
2521
    if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) {
7,883,494✔
2522
      isExist = true;
7,883,494✔
2523
    }
2524

181,836✔
2525
    switch (type) {
181,836✔
2526
      case TSDB_SUPER_TABLE: {
181,836✔
2527
        if (isExist) {
2528
          code = metaHandleSuperTableUpdate(pMeta, pEntry);
181,276✔
2529
        } else {
181,276✔
2530
          code = metaHandleSuperTableCreate(pMeta, pEntry);
181,276✔
2531
        }
UNCOV
2532
        break;
×
UNCOV
2533
      }
×
UNCOV
2534
      case TSDB_CHILD_TABLE: {
×
2535
        if (isExist) {
2536
          code = metaHandleChildTableUpdate(pMeta, pEntry);
2537
        } else {
2538
          code = metaHandleChildTableCreate(pMeta, pEntry);
2539
        }
987,104,068✔
2540
        break;
987,065,930✔
2541
      }
987,260,090✔
2542
      case TSDB_NORMAL_TABLE: {
2543
        if (isExist) {
2544
          code = metaHandleNormalTableUpdate(pMeta, pEntry);
38,138✔
2545
        } else {
2546
          code = metaHandleNormalTableCreate(pMeta, pEntry);
987,328,371✔
2547
        }
2548
        break;
2549
      }
1,765,387✔
2550
      case TSDB_VIRTUAL_NORMAL_TABLE: {
1,765,387✔
2551
        if (isExist) {
1,765,387✔
2552
          code = metaHandleVirtualNormalTableUpdate(pMeta, pEntry);
1,765,387✔
UNCOV
2553
        } else {
×
2554
          code = metaHandleVirtualNormalTableCreate(pMeta, pEntry);
2555
        }
1,765,387✔
2556
        break;
2557
      }
2558
      case TSDB_VIRTUAL_CHILD_TABLE: {
2559
        if (isExist) {
2560
          code = metaHandleVirtualChildTableUpdate(pMeta, pEntry);
2561
        } else {
2562
          code = metaHandleVirtualChildTableCreate(pMeta, pEntry);
2563
        }
2564
        break;
2565
      }
2566
      default: {
2567
        code = TSDB_CODE_INVALID_PARA;
2568
        break;
2569
      }
2570
    }
2571
  } else {
2572
    switch (type) {
2573
      case TSDB_SUPER_TABLE: {
2574
        code = metaHandleSuperTableDrop(pMeta, pEntry);
2575
        break;
2576
      }
2577
      case TSDB_CHILD_TABLE: {
2578
        code = metaHandleChildTableDrop(pMeta, pEntry, false);
2579
        break;
2580
      }
2581
      case TSDB_NORMAL_TABLE: {
2582
        code = metaHandleNormalTableDrop(pMeta, pEntry);
2583
        break;
2584
      }
2585
      case TSDB_VIRTUAL_NORMAL_TABLE: {
2586
        code = metaHandleVirtualNormalTableDrop(pMeta, pEntry);
2587
        break;
2588
      }
2589
      case TSDB_VIRTUAL_CHILD_TABLE: {
2590
        code = metaHandleVirtualChildTableDrop(pMeta, pEntry, false);
2591
        break;
2592
      }
2593
      default: {
2594
        code = TSDB_CODE_INVALID_PARA;
2595
        break;
2596
      }
2597
    }
2598
  }
2599

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

2610
void metaHandleSyncEntry(SMeta *pMeta, const SMetaEntry *pEntry) {
2611
  int32_t code = TSDB_CODE_SUCCESS;
2612
  code = metaHandleEntry2(pMeta, pEntry);
2613
  if (code) {
2614
    metaErr(TD_VID(pMeta->pVnode), code);
2615
  }
2616
  return;
2617
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc