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

taosdata / TDengine / #3659

14 Mar 2025 08:10AM UTC coverage: 63.314% (+0.06%) from 63.25%
#3659

push

travis-ci

web-flow
feat(keep): support keep on super table level. (#30097)

* Feat: support use keep while create super table.

* Test(keep): add test for create super table with keep option.

* Feat(keep): Add tmsg for create keep.

* Feat(keep): support alter table option keep.

* Fix(keep): Add baisc test for alter table option.

* Fix(keep): memory leek.

* Feat(keep): add keep to metaEntry&metaCache and fix earliestTs with stn keep.

* Test(keep): add some cases for select with stb keep.

* Fix: fix ci core while alter stb.

* Feat(keep): delete expired data in super table level.

* Feat: remove get stb keep while query.

* Fix : build error.

* Revert "Fix : build error."

This reverts commit 0ed66e4e8.

* Revert "Feat(keep): delete expired data in super table level."

This reverts commit 36330f6b4.

* Fix : build errors.

* Feat : support restart taosd.

* Fix : alter table comment problems.

* Test : add tests for super table keep.

* Fix: change sdb stb reserve size.

* Test: add more tests.

* Feat: Disable normal tables and sub tables from setting the keep parameter

* Fix: add more checks to avoid unknown address.

* Docs: Add docs for stable keep.

* Fix: some review changes.

* Fix: review errors.

149064 of 302527 branches covered (49.27%)

Branch coverage included in aggregate %.

88 of 99 new or added lines in 12 files covered. (88.89%)

3134 existing lines in 16 files now uncovered.

234231 of 302857 relevant lines covered (77.34%)

18177246.92 hits per line

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

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

11
#include "meta.h"
12

13
extern SDmNotifyHandle dmNotifyHdl;
14

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

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

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

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

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

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

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

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

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

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

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

96
  tDecoderInit(&decoder, value, valueSize);
183,874✔
97
  code = metaDecodeEntry(&decoder, &entry);
183,857✔
98
  if (code) {
183,781!
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);
183,781✔
107
  if (code) {
183,849!
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);
183,849✔
116
  tDecoderClear(&decoder);
183,840✔
117
  return code;
183,872✔
118
}
119

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

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

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

141
void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); }
183,832✔
142

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

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

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

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

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

180
  // put to tdb
181
  if (META_TABLE_OP_INSERT == op) {
200,161✔
182
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
184,318✔
183
  } else if (META_TABLE_OP_UPDATA == op) {
15,843✔
184
    code = tdbTbUpsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
10,631✔
185
  } else if (META_TABLE_OP_DELETE == op) {
5,212!
186
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
5,212✔
187
  } else {
188
    code = TSDB_CODE_INVALID_PARA;
×
189
  }
190
  if (TSDB_CODE_SUCCESS != code) {
200,266!
191
    metaErr(vgId, code);
×
192
  }
193
  taosMemoryFree(value);
200,251!
194
  return code;
200,223✔
195
}
196

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

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

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

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

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

231
  // encode schema
232
  tEncodeSize(tEncodeSSchemaWrapper, pSchema, valueSize, code);
90,071✔
233
  if (TSDB_CODE_SUCCESS != code) {
44,904!
234
    metaErr(vgId, code);
×
235
    return code;
×
236
  }
237

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

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

254
  // put to tdb
255
  if (META_TABLE_OP_INSERT == op) {
44,780!
256
    code = tdbTbInsert(pMeta->pSkmDb, &key, sizeof(key), value, valueSize, pMeta->txn);
×
257
  } else if (META_TABLE_OP_UPDATA == op) {
44,793!
258
    code = tdbTbUpsert(pMeta->pSkmDb, &key, sizeof(key), value, valueSize, pMeta->txn);
44,793✔
259
  } else {
260
    code = TSDB_CODE_INVALID_PARA;
×
261
  }
262
  if (TSDB_CODE_SUCCESS != code) {
45,059!
263
    metaErr(vgId, code);
×
264
  }
265
  taosMemoryFree(value);
45,049!
266
  return code;
45,045✔
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,
136,076✔
274
                                                 const SSchema *pOldColumn, const SSchema *pNewColumn) {
275
  int32_t code = TSDB_CODE_SUCCESS;
136,076✔
276

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

281
  if (pOldColumn && pNewColumn) {
136,076✔
282
    if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
134,178✔
283
      return TSDB_CODE_SUCCESS;
3,330✔
284
    } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) {
130,848!
285
      action = DROP_INDEX;
2,210✔
286
    } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
128,638!
287
      action = ADD_INDEX;
967✔
288
    } else {
289
      return TSDB_CODE_SUCCESS;
127,671✔
290
    }
291
  } else if (pOldColumn) {
1,898✔
292
    if (IS_IDX_ON(pOldColumn)) {
788✔
293
      action = DROP_INDEX;
17✔
294
    } else {
295
      return TSDB_CODE_SUCCESS;
771✔
296
    }
297
  } else {
298
    if (IS_IDX_ON(pNewColumn)) {
1,110!
299
      action = ADD_INDEX;
×
300
    } else {
301
      return TSDB_CODE_SUCCESS;
1,110✔
302
    }
303
  }
304

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

313
  // do drop or add index
314
  for (int32_t i = 0; i < taosArrayGetSize(childTables); i++) {
8,191✔
315
    int64_t uid = *(int64_t *)taosArrayGet(childTables, i);
4,997✔
316

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

326
    STagIdxKey *pTagIdxKey = NULL;
4,997✔
327
    int32_t     tagIdxKeySize = 0;
4,997✔
328

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

369
  taosArrayDestroy(childTables);
3,194✔
370
  return code;
3,194✔
371
}
372

373
static int32_t metaUpdateSuperTableTagSchema(SMeta *pMeta, const SMetaHandleParam *pParam) {
6,526✔
374
  int32_t               code = TSDB_CODE_SUCCESS;
6,526✔
375
  const SMetaEntry     *pEntry = pParam->pEntry;
6,526✔
376
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
6,526✔
377
  const SSchemaWrapper *pNewTagSchema = &pEntry->stbEntry.schemaTag;
6,526✔
378
  const SSchemaWrapper *pOldTagSchema = &pOldEntry->stbEntry.schemaTag;
6,526✔
379

380
  int32_t iOld = 0, iNew = 0;
6,526✔
381
  for (; iOld < pOldTagSchema->nCols && iNew < pNewTagSchema->nCols;) {
141,320✔
382
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
134,789✔
383
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
134,789✔
384

385
    if (pOldColumn->colId == pNewColumn->colId) {
134,789✔
386
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
134,177✔
387
      if (code) {
134,183✔
388
        metaErr(TD_VID(pMeta->pVnode), code);
1!
389
        return code;
×
390
      }
391

392
      iOld++;
134,182✔
393
      iNew++;
134,182✔
394
    } else if (pOldColumn->colId < pNewColumn->colId) {
612!
395
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
612✔
396
      if (code) {
612!
397
        metaErr(TD_VID(pMeta->pVnode), code);
×
398
        return code;
×
399
      }
400

401
      iOld++;
612✔
402
    } else {
403
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, NULL, pNewColumn);
×
404
      if (code) {
×
405
        metaErr(TD_VID(pMeta->pVnode), code);
×
406
        return code;
×
407
      }
408

409
      iNew++;
×
410
    }
411
  }
412

413
  for (; iOld < pOldTagSchema->nCols; iOld++) {
6,707✔
414
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
176✔
415
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
176✔
416
    if (code) {
176!
417
      metaErr(TD_VID(pMeta->pVnode), code);
×
418
      return code;
×
419
    }
420
  }
421

422
  for (; iNew < pNewTagSchema->nCols; iNew++) {
7,649✔
423
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
1,119✔
424
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, NULL, pNewColumn);
1,119✔
425
    if (code) {
1,119✔
426
      metaErr(TD_VID(pMeta->pVnode), code);
1!
427
      return code;
×
428
    }
429
  }
430

431
  return code;
6,530✔
432
}
433

434
static int32_t metaSchemaTableUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
51,065✔
435
  int32_t code = TSDB_CODE_SUCCESS;
51,065✔
436

437
  const SMetaEntry *pEntry = pParam->pEntry;
51,065✔
438
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
51,065✔
439

440
  if (NULL == pOldEntry) {
51,065✔
441
    return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
41,688✔
442
  }
443

444
  if (pEntry->type == TSDB_NORMAL_TABLE) {
9,377✔
445
    // check row schema
446
    if (pOldEntry->ntbEntry.schemaRow.version != pEntry->ntbEntry.schemaRow.version) {
234✔
447
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
191✔
448
    }
449
  } else if (pEntry->type == TSDB_SUPER_TABLE) {
9,143!
450
    // check row schema
451
    if (pOldEntry->stbEntry.schemaRow.version != pEntry->stbEntry.schemaRow.version) {
9,694✔
452
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
3,164✔
453
    }
454

455
    // check tag schema
456
    code = metaUpdateSuperTableTagSchema(pMeta, pParam);
6,530✔
457
    if (code) {
6,528✔
458
      metaErr(TD_VID(pMeta->pVnode), code);
1!
459
      return code;
×
460
    }
461

462
  } else {
463
    return TSDB_CODE_INVALID_PARA;
×
464
  }
465

466
  return TSDB_CODE_SUCCESS;
6,570✔
467
}
468

469
static int32_t metaSchemaTableDelete(SMeta *pMeta, const SMetaHandleParam *pEntry) {
×
470
  // TODO
471
  return TSDB_CODE_SUCCESS;
×
472
}
473

474
// Uid Index
475
static void metaBuildEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) {
194,931✔
476
  pInfo->uid = pEntry->uid;
194,931✔
477
  pInfo->version = pEntry->version;
194,931✔
478
  if (pEntry->type == TSDB_SUPER_TABLE) {
194,931✔
479
    pInfo->suid = pEntry->uid;
36,954✔
480
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
36,954✔
481
  } else if (pEntry->type == TSDB_CHILD_TABLE) {
157,977✔
482
    pInfo->suid = pEntry->ctbEntry.suid;
143,417✔
483
    pInfo->skmVer = 0;
143,417✔
484
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
14,560!
485
    pInfo->suid = 0;
14,651✔
486
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
14,651✔
487
  }
488
}
194,931✔
489

490
static int32_t metaUidIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
194,935✔
491
  int32_t code = TSDB_CODE_SUCCESS;
194,935✔
492
  int32_t vgId = TD_VID(pMeta->pVnode);
194,935✔
493

494
  const SMetaEntry *pEntry = pParam->pEntry;
194,935✔
495

496
  // update cache
497
  SMetaInfo info = {0};
194,935✔
498
  metaBuildEntryInfo(pEntry, &info);
194,935✔
499
  code = metaCacheUpsert(pMeta, &info);
195,021✔
500
  if (code) {
194,896!
501
    metaErr(vgId, code);
×
502
  }
503

504
  // put to tdb
505
  SUidIdxVal value = {
194,854✔
506
      .suid = info.suid,
194,854✔
507
      .skmVer = info.skmVer,
194,854✔
508
      .version = pEntry->version,
194,854✔
509
  };
510
  if (META_TABLE_OP_INSERT == op) {
194,854✔
511
    code = tdbTbInsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
184,240✔
512
  } else if (META_TABLE_OP_UPDATA == op) {
10,614!
513
    code = tdbTbUpsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
10,621✔
514
  }
515
  return code;
195,087✔
516
}
517

518
static int32_t metaUidIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
184,312✔
519
  return metaUidIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
184,312✔
520
}
521

522
static int32_t metaUidIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
10,610✔
523
  return metaUidIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
10,610✔
524
}
525

526
static int32_t metaUidIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
8,911✔
527
  int32_t code = 0;
8,911✔
528

529
  const SMetaEntry *pEntry = pParam->pOldEntry;
8,911✔
530

531
  // delete tdb
532
  code = tdbTbDelete(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), pMeta->txn);
8,911✔
533
  if (code) {
8,913!
534
    metaErr(TD_VID(pMeta->pVnode), code);
×
535
  }
536

537
  // delete cache
538
  (void)metaCacheDrop(pMeta, pEntry->uid);
8,913✔
539
  return code;
8,916✔
540
}
541

542
// Name Index
543
static int32_t metaNameIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
184,349✔
544
  int32_t code = TSDB_CODE_SUCCESS;
184,349✔
545

546
  const SMetaEntry *pEntry = pParam->pEntry;
184,349✔
547

548
  if (META_TABLE_OP_INSERT == op) {
184,349!
549
    code = tdbTbInsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
184,371✔
550
                       pMeta->txn);
551
  } else if (META_TABLE_OP_UPDATA == op) {
×
552
    code = tdbTbUpsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
×
553
                       pMeta->txn);
554
  } else {
555
    code = TSDB_CODE_INVALID_PARA;
×
556
  }
557
  return code;
184,457✔
558
}
559

560
static int32_t metaNameIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
184,361✔
561
  int32_t code = TSDB_CODE_SUCCESS;
184,361✔
562
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
184,361✔
563
}
564

565
static int32_t metaNameIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
566
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
×
567
}
568

569
static int32_t metaNameIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
8,915✔
570
  int32_t code = TSDB_CODE_SUCCESS;
8,915✔
571

572
  const SMetaEntry *pEntry = pParam->pOldEntry;
8,915✔
573
  code = tdbTbDelete(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, pMeta->txn);
8,915✔
574
  if (code) {
8,914!
575
    metaErr(TD_VID(pMeta->pVnode), code);
×
576
  }
577
  return code;
8,914✔
578
}
579

580
// Suid Index
581
static int32_t metaSUidIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
27,236✔
582
  const SMetaEntry *pEntry = pParam->pEntry;
27,236✔
583

584
  int32_t code = tdbTbInsert(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), NULL, 0, pMeta->txn);
27,236✔
585
  if (code) {
27,302!
586
    metaErr(TD_VID(pMeta->pVnode), code);
×
587
  }
588
  return code;
27,258✔
589
}
590

591
static int32_t metaSUidIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
2,552✔
592
  const SMetaEntry *pEntry = pParam->pOldEntry;
2,552✔
593

594
  int32_t code = tdbTbDelete(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), pMeta->txn);
2,552✔
595
  if (code) {
2,552!
596
    metaErr(TD_VID(pMeta->pVnode), code);
×
597
  }
598
  return code;
2,552✔
599
}
600

601
// Child Index
602
static int32_t metaChildIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
143,195✔
603
  int32_t code = TSDB_CODE_SUCCESS;
143,195✔
604

605
  const SMetaEntry *pEntry = pParam->pEntry;
143,195✔
606

607
  SCtbIdxKey key = {
143,195✔
608
      .suid = pEntry->ctbEntry.suid,
143,195✔
609
      .uid = pEntry->uid,
143,195✔
610
  };
611

612
  if (META_TABLE_OP_INSERT == op) {
143,195✔
613
    code = tdbTbInsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
142,727✔
614
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
142,727✔
615
  } else if (META_TABLE_OP_UPDATA == op) {
468!
616
    code = tdbTbUpsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
479✔
617
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
479✔
618
  } else {
619
    code = TSDB_CODE_INVALID_PARA;
×
620
  }
621
  return code;
143,212✔
622
}
623

624
static int32_t metaChildIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
142,710✔
625
  return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
142,710✔
626
}
627

628
static int32_t metaChildIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
701✔
629
  const SMetaEntry *pEntry = pParam->pEntry;
701✔
630
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
701✔
631
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
701✔
632

633
  const STag *pNewTags = (const STag *)pEntry->ctbEntry.pTags;
701✔
634
  const STag *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags;
701✔
635
  if (pNewTags->len != pOldTags->len || memcmp(pNewTags, pOldTags, pNewTags->len)) {
701✔
636
    return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
479✔
637
  }
638
  return 0;
222✔
639
}
640

641
static int32_t metaChildIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
4,540✔
642
  const SMetaEntry *pEntry = pParam->pOldEntry;
4,540✔
643

644
  SCtbIdxKey key = {
4,540✔
645
      .suid = pEntry->ctbEntry.suid,
4,540✔
646
      .uid = pEntry->uid,
4,540✔
647
  };
648
  return tdbTbDelete(pMeta->pCtbIdx, &key, sizeof(key), pMeta->txn);
4,540✔
649
}
650

651
// Tag Index
652
static int32_t metaFetchTagIdxKey(SMeta *pMeta, const SMetaEntry *pEntry, const SSchema *pTagColumn,
152,895✔
653
                                  STagIdxKey **ppTagIdxKey, int32_t *pTagIdxKeySize) {
654
  int32_t code = TSDB_CODE_SUCCESS;
152,895✔
655

656
  STagIdxKey *pTagIdxKey = NULL;
152,895✔
657
  int32_t     nTagIdxKey;
658
  const void *pTagData = NULL;
152,895✔
659
  int32_t     nTagData = 0;
152,895✔
660

661
  STagVal tagVal = {
152,895✔
662
      .cid = pTagColumn->colId,
152,895✔
663
  };
664

665
  if (tTagGet((const STag *)pEntry->ctbEntry.pTags, &tagVal)) {
152,895✔
666
    if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
152,450!
667
      pTagData = tagVal.pData;
23,058✔
668
      nTagData = (int32_t)tagVal.nData;
23,058✔
669
    } else {
670
      pTagData = &(tagVal.i64);
129,392✔
671
      nTagData = tDataTypes[pTagColumn->type].bytes;
129,392✔
672
    }
673
  } else {
674
    if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
444!
675
      nTagData = tDataTypes[pTagColumn->type].bytes;
254✔
676
    }
677
  }
678

679
  code = metaCreateTagIdxKey(pEntry->ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type,
152,894✔
680
                             pEntry->uid, &pTagIdxKey, &nTagIdxKey);
152,894✔
681
  if (code) {
152,895✔
682
    metaErr(TD_VID(pMeta->pVnode), code);
6!
683
    return code;
×
684
  }
685

686
  *ppTagIdxKey = pTagIdxKey;
152,889✔
687
  *pTagIdxKeySize = nTagIdxKey;
152,889✔
688
  return code;
152,889✔
689
}
690

691
static void metaFetchTagIdxKeyFree(STagIdxKey **ppTagIdxKey) {
152,920✔
692
  metaDestroyTagIdxKey(*ppTagIdxKey);
152,920✔
693
  *ppTagIdxKey = NULL;
152,915✔
694
}
152,915✔
695

696
static int32_t metaTagIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
142,715✔
697
  int32_t code = TSDB_CODE_SUCCESS;
142,715✔
698

699
  const SMetaEntry *pEntry = pParam->pEntry;
142,715✔
700
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
142,715✔
701

702
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
142,715✔
703
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
142,958✔
704
    const SSchema *pTagColumn = &pTagSchema->pSchema[0];
243✔
705

706
    STagVal tagVal = {
243✔
707
        .cid = pTagColumn->colId,
243✔
708
    };
709

710
    const void *pTagData = pEntry->ctbEntry.pTags;
243✔
711
    int32_t     nTagData = ((const STag *)pEntry->ctbEntry.pTags)->len;
243✔
712
    code = metaSaveJsonVarToIdx(pMeta, pEntry, pTagColumn);
243✔
713
    if (code) {
243!
714
      metaErr(TD_VID(pMeta->pVnode), code);
×
715
    }
716
  } else {
717
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
602,373✔
718
      STagIdxKey    *pTagIdxKey = NULL;
459,895✔
719
      int32_t        nTagIdxKey;
720
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
459,895✔
721

722
      if (!IS_IDX_ON(pTagColumn)) {
459,895✔
723
        continue;
317,459✔
724
      }
725

726
      code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pTagIdxKey, &nTagIdxKey);
142,436✔
727
      if (code) {
142,413!
728
        metaErr(TD_VID(pMeta->pVnode), code);
×
729
        return code;
×
730
      }
731

732
      code = tdbTbInsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn);
142,413✔
733
      if (code) {
142,450!
734
        metaErr(TD_VID(pMeta->pVnode), code);
×
735
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
736
        return code;
×
737
      }
738
      metaFetchTagIdxKeyFree(&pTagIdxKey);
142,450✔
739
    }
740
  }
741
  return code;
142,721✔
742
}
743

744
static int32_t metaTagIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
701✔
745
  int32_t code = TSDB_CODE_SUCCESS;
701✔
746

747
  const SMetaEntry     *pEntry = pParam->pEntry;
701✔
748
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
701✔
749
  const SMetaEntry     *pSuperEntry = pParam->pSuperEntry;
701✔
750
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
701✔
751
  const STag           *pNewTags = (const STag *)pEntry->ctbEntry.pTags;
701✔
752
  const STag           *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags;
701✔
753

754
  if (pNewTags->len == pOldTags->len && !memcmp(pNewTags, pOldTags, pNewTags->len)) {
701✔
755
    return code;
222✔
756
  }
757

758
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
479✔
759
    code = metaDelJsonVarFromIdx(pMeta, pOldEntry, &pTagSchema->pSchema[0]);
11✔
760
    if (code) {
11!
761
      metaErr(TD_VID(pMeta->pVnode), code);
×
762
      return code;
×
763
    }
764

765
    code = metaSaveJsonVarToIdx(pMeta, pEntry, &pTagSchema->pSchema[0]);
11✔
766
    if (code) {
11!
767
      metaErr(TD_VID(pMeta->pVnode), code);
×
768
      return code;
×
769
    }
770
  } else {
771
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
2,361✔
772
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
1,893✔
773

774
      if (!IS_IDX_ON(pTagColumn)) {
1,893✔
775
        continue;
1,429✔
776
      }
777

778
      STagIdxKey *pOldTagIdxKey = NULL;
464✔
779
      int32_t     oldTagIdxKeySize = 0;
464✔
780
      STagIdxKey *pNewTagIdxKey = NULL;
464✔
781
      int32_t     newTagIdxKeySize = 0;
464✔
782

783
      code = metaFetchTagIdxKey(pMeta, pOldEntry, pTagColumn, &pOldTagIdxKey, &oldTagIdxKeySize);
464✔
784
      if (code) {
464!
785
        metaErr(TD_VID(pMeta->pVnode), code);
×
786
        return code;
×
787
      }
788

789
      code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pNewTagIdxKey, &newTagIdxKeySize);
464✔
790
      if (code) {
464!
791
        metaErr(TD_VID(pMeta->pVnode), code);
×
792
        metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
793
        return code;
×
794
      }
795

796
      if (tagIdxKeyCmpr(pOldTagIdxKey, oldTagIdxKeySize, pNewTagIdxKey, newTagIdxKeySize)) {
464✔
797
        code = tdbTbDelete(pMeta->pTagIdx, pOldTagIdxKey, oldTagIdxKeySize, pMeta->txn);
97✔
798
        if (code) {
97!
799
          metaErr(TD_VID(pMeta->pVnode), code);
×
800
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
801
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
802
          return code;
×
803
        }
804

805
        code = tdbTbInsert(pMeta->pTagIdx, pNewTagIdxKey, newTagIdxKeySize, NULL, 0, pMeta->txn);
97✔
806
        if (code) {
97!
807
          metaErr(TD_VID(pMeta->pVnode), code);
×
808
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
809
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
810
          return code;
×
811
        }
812
      }
813

814
      metaFetchTagIdxKeyFree(&pOldTagIdxKey);
464✔
815
      metaFetchTagIdxKeyFree(&pNewTagIdxKey);
464✔
816
    }
817
  }
818
  return code;
479✔
819
}
820

821
static int32_t metaTagIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
4,541✔
822
  int32_t code = TSDB_CODE_SUCCESS;
4,541✔
823

824
  const SMetaEntry     *pEntry = pParam->pEntry;
4,541✔
825
  const SMetaEntry     *pChild = pParam->pOldEntry;
4,541✔
826
  const SMetaEntry     *pSuper = pParam->pSuperEntry;
4,541✔
827
  const SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
4,541✔
828
  const SSchema        *pTagColumn = NULL;
4,541✔
829
  const STag           *pTags = (const STag *)pChild->ctbEntry.pTags;
4,541✔
830

831
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
4,541✔
832
    pTagColumn = &pTagSchema->pSchema[0];
8✔
833
    code = metaDelJsonVarFromIdx(pMeta, pChild, pTagColumn);
8✔
834
    if (code) {
8!
835
      metaErr(TD_VID(pMeta->pVnode), code);
×
836
    }
837
  } else {
838
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
30,004✔
839
      pTagColumn = &pTagSchema->pSchema[i];
25,471✔
840
      if (!IS_IDX_ON(pTagColumn)) {
25,471✔
841
        continue;
20,923✔
842
      }
843

844
      STagIdxKey *pTagIdxKey = NULL;
4,548✔
845
      int32_t     nTagIdxKey;
846

847
      code = metaFetchTagIdxKey(pMeta, pChild, pTagColumn, &pTagIdxKey, &nTagIdxKey);
4,548✔
848
      if (code) {
4,548!
849
        metaErr(TD_VID(pMeta->pVnode), code);
×
850
        return code;
×
851
      }
852

853
      code = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
4,548✔
854
      if (code) {
4,548!
855
        metaErr(TD_VID(pMeta->pVnode), code);
×
856
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
857
        return code;
×
858
      }
859
      metaFetchTagIdxKeyFree(&pTagIdxKey);
4,548✔
860
    }
861
  }
862
  return code;
4,541✔
863
}
864

865
// Btime Index
866
static int32_t metaBtimeIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
163,469✔
867
  int32_t code = TSDB_CODE_SUCCESS;
163,469✔
868

869
  const SMetaEntry *pEntry;
870
  if (META_TABLE_OP_DELETE == op) {
163,469✔
871
    pEntry = pParam->pOldEntry;
6,363✔
872
  } else {
873
    pEntry = pParam->pEntry;
157,106✔
874
  }
875

876
  SBtimeIdxKey key = {
163,469✔
877
      .uid = pEntry->uid,
163,469✔
878
  };
879

880
  if (TSDB_CHILD_TABLE == pEntry->type) {
163,469✔
881
    key.btime = pEntry->ctbEntry.btime;
147,238✔
882
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
16,231!
883
    key.btime = pEntry->ntbEntry.btime;
16,237✔
884
  } else {
885
    return TSDB_CODE_INVALID_PARA;
×
886
  }
887

888
  if (META_TABLE_OP_INSERT == op) {
163,475✔
889
    code = tdbTbInsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
157,117✔
890
  } else if (META_TABLE_OP_UPDATA == op) {
6,358!
891
    code = tdbTbUpsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
×
892
  } else if (META_TABLE_OP_DELETE == op) {
6,358!
893
    code = tdbTbDelete(pMeta->pBtimeIdx, &key, sizeof(key), pMeta->txn);
6,364✔
894
  } else {
895
    code = TSDB_CODE_INVALID_PARA;
×
896
  }
897
  if (code) {
163,497!
898
    metaErr(TD_VID(pMeta->pVnode), code);
×
899
  }
900
  return code;
163,499✔
901
}
902

903
static int32_t metaBtimeIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
157,115✔
904
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
157,115✔
905
}
906

907
static int32_t metaBtimeIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
908
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
×
909
}
910

911
static int32_t metaBtimeIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
6,363✔
912
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_DELETE);
6,363✔
913
}
914

915
// TTL Index
916
static int32_t metaTtlIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
157,135✔
917
  const SMetaEntry *pEntry = pParam->pEntry;
157,135✔
918

919
  STtlUpdTtlCtx ctx = {
157,135✔
920
      .uid = pEntry->uid,
157,135✔
921
      .pTxn = pMeta->txn,
157,135✔
922
  };
923
  if (TSDB_CHILD_TABLE == pEntry->type) {
157,135✔
924
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
142,713✔
925
    ctx.changeTimeMs = pEntry->ctbEntry.btime;
142,713✔
926
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
14,422!
927
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
14,428✔
928
    ctx.changeTimeMs = pEntry->ntbEntry.btime;
14,428✔
929
  } else {
930
    return TSDB_CODE_INVALID_PARA;
×
931
  }
932

933
  int32_t ret = ttlMgrInsertTtl(pMeta->pTtlMgr, &ctx);
157,141✔
934
  if (ret < 0) {
157,139!
935
    metaError("vgId:%d, failed to insert ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid, tstrerror(ret));
×
936
  }
937
  return TSDB_CODE_SUCCESS;
157,141✔
938
}
939

940
static int32_t metaTtlIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
157,142✔
941
  return metaTtlIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
157,142✔
942
}
943

944
static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam);
945

946
static int32_t metaTtlIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
935✔
947
  int32_t code = TSDB_CODE_SUCCESS;
935✔
948

949
  const SMetaEntry *pEntry = pParam->pEntry;
935✔
950
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
935✔
951

952
  if ((pEntry->type == TSDB_CHILD_TABLE && pOldEntry->ctbEntry.ttlDays != pEntry->ctbEntry.ttlDays) ||
935✔
953
      (pEntry->type == TSDB_NORMAL_TABLE && pOldEntry->ntbEntry.ttlDays != pEntry->ntbEntry.ttlDays)) {
920✔
954
    code = metaTtlIdxDelete(pMeta, pParam);
26✔
955
    if (code) {
26!
956
      metaErr(TD_VID(pMeta->pVnode), code);
×
957
    }
958

959
    code = metaTtlIdxInsert(pMeta, pParam);
26✔
960
    if (code) {
26!
961
      metaErr(TD_VID(pMeta->pVnode), code);
×
962
    }
963
  }
964

965
  return TSDB_CODE_SUCCESS;
935✔
966
}
967

968
static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
6,388✔
969
  int32_t code = TSDB_CODE_SUCCESS;
6,388✔
970

971
  const SMetaEntry *pEntry = pParam->pOldEntry;
6,388✔
972
  STtlDelTtlCtx     ctx = {
6,388✔
973
          .uid = pEntry->uid,
6,388✔
974
          .pTxn = pMeta->txn,
6,388✔
975
  };
976

977
  if (TSDB_CHILD_TABLE == pEntry->type) {
6,388✔
978
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
4,555✔
979
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
1,833!
980
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
1,833✔
981
  } else {
982
    code = TSDB_CODE_INVALID_PARA;
×
983
  }
984

985
  if (TSDB_CODE_SUCCESS == code) {
6,388!
986
    int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
6,388✔
987
    if (ret < 0) {
6,388!
988
      metaError("vgId:%d, failed to delete ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid,
×
989
                tstrerror(ret));
990
    }
991
  }
992
  return code;
6,387✔
993
}
994

995
static void metaTimeSeriesNotifyCheck(SMeta *pMeta) {
159,719✔
996
#if defined(TD_ENTERPRISE)
997
  int64_t nTimeSeries = metaGetTimeSeriesNum(pMeta, 0);
159,719✔
998
  int64_t deltaTS = nTimeSeries - pMeta->pVnode->config.vndStats.numOfReportedTimeSeries;
159,743✔
999
  if (deltaTS > tsTimeSeriesThreshold) {
159,743✔
1000
    if (0 == atomic_val_compare_exchange_8(&dmNotifyHdl.state, 1, 2)) {
108,287✔
1001
      if (tsem_post(&dmNotifyHdl.sem) != 0) {
108,285!
1002
        metaError("vgId:%d, failed to post semaphore, errno:%d", TD_VID(pMeta->pVnode), ERRNO);
×
1003
      }
1004
    }
1005
  }
1006
#endif
1007
}
159,758✔
1008

1009
static int32_t (*metaTableOpFn[META_TABLE_MAX][META_TABLE_OP_MAX])(SMeta *pMeta, const SMetaHandleParam *pParam) =
1010
    {
1011
        [META_ENTRY_TABLE] =
1012
            {
1013
                [META_TABLE_OP_INSERT] = metaEntryTableInsert,
1014
                [META_TABLE_OP_UPDATA] = metaEntryTableUpdate,
1015
                [META_TABLE_OP_DELETE] = metaEntryTableDelete,
1016
            },
1017
        [META_SCHEMA_TABLE] =
1018
            {
1019
                [META_TABLE_OP_INSERT] = metaSchemaTableInsert,
1020
                [META_TABLE_OP_UPDATA] = metaSchemaTableUpdate,
1021
                [META_TABLE_OP_DELETE] = metaSchemaTableDelete,
1022
            },
1023
        [META_UID_IDX] =
1024
            {
1025
                [META_TABLE_OP_INSERT] = metaUidIdxInsert,
1026
                [META_TABLE_OP_UPDATA] = metaUidIdxUpdate,
1027
                [META_TABLE_OP_DELETE] = metaUidIdxDelete,
1028
            },
1029
        [META_NAME_IDX] =
1030
            {
1031
                [META_TABLE_OP_INSERT] = metaNameIdxInsert,
1032
                [META_TABLE_OP_UPDATA] = metaNameIdxUpdate,
1033
                [META_TABLE_OP_DELETE] = metaNameIdxDelete,
1034
            },
1035
        [META_SUID_IDX] =
1036
            {
1037
                [META_TABLE_OP_INSERT] = metaSUidIdxInsert,
1038
                [META_TABLE_OP_UPDATA] = NULL,
1039
                [META_TABLE_OP_DELETE] = metaSUidIdxDelete,
1040
            },
1041
        [META_CHILD_IDX] =
1042
            {
1043
                [META_TABLE_OP_INSERT] = metaChildIdxInsert,
1044
                [META_TABLE_OP_UPDATA] = metaChildIdxUpdate,
1045
                [META_TABLE_OP_DELETE] = metaChildIdxDelete,
1046
            },
1047
        [META_TAG_IDX] =
1048
            {
1049
                [META_TABLE_OP_INSERT] = metaTagIdxInsert,
1050
                [META_TABLE_OP_UPDATA] = metaTagIdxUpdate,
1051
                [META_TABLE_OP_DELETE] = metaTagIdxDelete,
1052
            },
1053
        [META_BTIME_IDX] =
1054
            {
1055
                [META_TABLE_OP_INSERT] = metaBtimeIdxInsert,
1056
                [META_TABLE_OP_UPDATA] = metaBtimeIdxUpdate,
1057
                [META_TABLE_OP_DELETE] = metaBtimeIdxDelete,
1058
            },
1059
        [META_TTL_IDX] =
1060
            {
1061
                [META_TABLE_OP_INSERT] = metaTtlIdxInsert,
1062
                [META_TABLE_OP_UPDATA] = metaTtlIdxUpdate,
1063
                [META_TABLE_OP_DELETE] = metaTtlIdxDelete,
1064
            },
1065
};
1066

1067
static int32_t metaHandleSuperTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
27,257✔
1068
  int32_t code = TSDB_CODE_SUCCESS;
27,257✔
1069

1070
  SMetaTableOp ops[] = {
27,257✔
1071
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1072
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: here should be insert
1073
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1074
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1075
      {META_SUID_IDX, META_TABLE_OP_INSERT},      //
1076
  };
1077

1078
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
163,357✔
1079
    SMetaTableOp          *op = &ops[i];
136,156✔
1080
    const SMetaHandleParam param = {
136,156✔
1081
        .pEntry = pEntry,
1082
    };
1083

1084
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
136,156✔
1085
    if (TSDB_CODE_SUCCESS != code) {
136,131✔
1086
      metaErr(TD_VID(pMeta->pVnode), code);
31!
1087
      return code;
×
1088
    }
1089
  }
1090

1091
  return code;
27,201✔
1092
}
1093
static int32_t metaHandleSuperTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
27,270✔
1094
  int32_t code = TSDB_CODE_SUCCESS;
27,270✔
1095

1096
  metaWLock(pMeta);
27,270✔
1097
  code = metaHandleSuperTableCreateImpl(pMeta, pEntry);
27,314✔
1098
  metaULock(pMeta);
27,241✔
1099

1100
  if (TSDB_CODE_SUCCESS == code) {
27,306!
1101
    pMeta->pVnode->config.vndStats.numOfSTables++;
27,306✔
1102

1103
    metaInfo("vgId:%d, %s success, version:%" PRId64 " type:%d uid:%" PRId64 " name:%s", TD_VID(pMeta->pVnode),
27,306!
1104
             __func__, pEntry->version, pEntry->type, pEntry->uid, pEntry->name);
1105
  } else {
1106
    metaErr(TD_VID(pMeta->pVnode), code);
×
1107
  }
1108
  return code;
27,336✔
1109
}
1110

1111
static int32_t metaHandleNormalTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
14,417✔
1112
  int32_t code = TSDB_CODE_SUCCESS;
14,417✔
1113

1114
  SMetaTableOp ops[] = {
14,417✔
1115
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1116
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: need to be insert
1117
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1118
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1119
      {META_BTIME_IDX, META_TABLE_OP_INSERT},     //
1120
      {META_TTL_IDX, META_TABLE_OP_INSERT},       //
1121
  };
1122

1123
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
100,899✔
1124
    SMetaTableOp *op = &ops[i];
86,488✔
1125

1126
    SMetaHandleParam param = {
86,488✔
1127
        .pEntry = pEntry,
1128
    };
1129

1130
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
86,488✔
1131
    if (TSDB_CODE_SUCCESS != code) {
86,482!
1132
      metaErr(TD_VID(pMeta->pVnode), code);
×
1133
      return code;
×
1134
    }
1135
  }
1136

1137
  return code;
14,411✔
1138
}
1139
static int32_t metaHandleNormalTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
14,417✔
1140
  int32_t code = TSDB_CODE_SUCCESS;
14,417✔
1141

1142
  // update TDB
1143
  metaWLock(pMeta);
14,417✔
1144
  code = metaHandleNormalTableCreateImpl(pMeta, pEntry);
14,417✔
1145
  metaULock(pMeta);
14,417✔
1146

1147
  // update other stuff
1148
  if (TSDB_CODE_SUCCESS == code) {
14,417!
1149
    pMeta->pVnode->config.vndStats.numOfNTables++;
14,417✔
1150
    pMeta->pVnode->config.vndStats.numOfNTimeSeries += pEntry->ntbEntry.schemaRow.nCols - 1;
14,417✔
1151

1152
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
14,417✔
1153
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, -1, &pEntry->ntbEntry.schemaRow);
30✔
1154
      if (rc < 0) {
30!
1155
        metaError("vgId:%d, failed to create table:%s since %s", TD_VID(pMeta->pVnode), pEntry->name, tstrerror(rc));
×
1156
      }
1157
    }
1158
    metaTimeSeriesNotifyCheck(pMeta);
14,417✔
1159
  } else {
1160
    metaErr(TD_VID(pMeta->pVnode), code);
×
1161
  }
1162
  return code;
14,418✔
1163
}
1164

1165
static int32_t metaHandleChildTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry, const SMetaEntry *pSuperEntry) {
142,722✔
1166
  int32_t code = TSDB_CODE_SUCCESS;
142,722✔
1167

1168
  SMetaTableOp ops[] = {
142,722✔
1169
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},  //
1170
      {META_UID_IDX, META_TABLE_OP_INSERT},      //
1171
      {META_NAME_IDX, META_TABLE_OP_INSERT},     //
1172
      {META_CHILD_IDX, META_TABLE_OP_INSERT},    //
1173
      {META_TAG_IDX, META_TABLE_OP_INSERT},      //
1174
      {META_BTIME_IDX, META_TABLE_OP_INSERT},    //
1175
      {META_TTL_IDX, META_TABLE_OP_INSERT},      //
1176
  };
1177

1178
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1,140,928✔
1179
    SMetaTableOp *op = &ops[i];
998,381✔
1180

1181
    SMetaHandleParam param = {
998,381✔
1182
        .pEntry = pEntry,
1183
        .pSuperEntry = pSuperEntry,
1184
    };
1185

1186
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
998,381✔
1187
    if (TSDB_CODE_SUCCESS != code) {
998,168!
1188
      metaErr(TD_VID(pMeta->pVnode), code);
×
1189
      return code;
×
1190
    }
1191
  }
1192

1193
  if (TSDB_CODE_SUCCESS == code) {
142,547!
1194
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
142,701✔
1195
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
142,707✔
1196
    if (ret < 0) {
142,730!
1197
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1198
    }
1199

1200
    ret = metaTbGroupCacheClear(pMeta, pSuperEntry->uid);
142,730✔
1201
    if (ret < 0) {
142,721!
1202
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1203
    }
1204
  }
1205
  return code;
142,718✔
1206
}
1207

1208
static int32_t metaHandleChildTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
142,722✔
1209
  int32_t     code = TSDB_CODE_SUCCESS;
142,722✔
1210
  SMetaEntry *pSuperEntry = NULL;
142,722✔
1211

1212
  // get the super table entry
1213
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
142,722✔
1214
  if (code) {
142,716!
1215
    metaErr(TD_VID(pMeta->pVnode), code);
×
1216
    return code;
×
1217
  }
1218

1219
  // update TDB
1220
  metaWLock(pMeta);
142,716✔
1221
  code = metaHandleChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
142,735✔
1222
  metaULock(pMeta);
142,706✔
1223

1224
  // update other stuff
1225
  if (TSDB_CODE_SUCCESS == code) {
142,715!
1226
    pMeta->pVnode->config.vndStats.numOfCTables++;
142,715✔
1227

1228
    if (!metaTbInFilterCache(pMeta, pSuperEntry->name, 1)) {
142,715!
1229
      int32_t nCols = 0;
142,727✔
1230
      int32_t ret = metaGetStbStats(pMeta->pVnode, pSuperEntry->uid, 0, &nCols);
142,727✔
1231
      if (ret < 0) {
142,721!
1232
        metaErr(TD_VID(pMeta->pVnode), ret);
×
1233
      }
1234
      pMeta->pVnode->config.vndStats.numOfTimeSeries += (nCols > 0 ? nCols - 1 : 0);
142,710!
1235
    }
1236

1237
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
142,710✔
1238
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, pEntry->ctbEntry.suid, NULL);
1,972✔
1239
      if (rc < 0) {
1,972!
1240
        metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
1241
                  tstrerror(rc));
1242
      }
1243
    }
1244

1245
  } else {
1246
    metaErr(TD_VID(pMeta->pVnode), code);
×
1247
  }
1248
  metaTimeSeriesNotifyCheck(pMeta);
142,710✔
1249
  metaFetchEntryFree(&pSuperEntry);
142,720✔
1250
  return code;
142,738✔
1251
}
1252

1253
static int32_t metaHandleNormalTableDropImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
1,822✔
1254
  int32_t code = TSDB_CODE_SUCCESS;
1,822✔
1255

1256
  SMetaTableOp ops[] = {
1,822✔
1257
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1258
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1259
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1260
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1261
      {META_TTL_IDX, META_TABLE_OP_DELETE},      //
1262

1263
      // {META_SCHEMA_TABLE, META_TABLE_OP_DELETE},  //
1264
  };
1265

1266
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
10,932✔
1267
    SMetaTableOp *op = &ops[i];
9,110✔
1268
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
9,110✔
1269
    if (code) {
9,110!
1270
      const SMetaEntry *pEntry = pParam->pEntry;
×
1271
      metaErr(TD_VID(pMeta->pVnode), code);
×
1272
    }
1273
  }
1274

1275
  return code;
1,822✔
1276
}
1277

1278
static int32_t metaHandleNormalTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
1,822✔
1279
  int32_t     code = TSDB_CODE_SUCCESS;
1,822✔
1280
  SMetaEntry *pOldEntry = NULL;
1,822✔
1281

1282
  // fetch the entry
1283
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
1,822✔
1284
  if (code) {
1,822!
1285
    metaErr(TD_VID(pMeta->pVnode), code);
×
1286
    return code;
×
1287
  }
1288

1289
  SMetaHandleParam param = {
1,822✔
1290
      .pEntry = pEntry,
1291
      .pOldEntry = pOldEntry,
1292
  };
1293

1294
  // do the drop
1295
  metaWLock(pMeta);
1,822✔
1296
  code = metaHandleNormalTableDropImpl(pMeta, &param);
1,822✔
1297
  metaULock(pMeta);
1,822✔
1298
  if (code) {
1,822!
1299
    metaErr(TD_VID(pMeta->pVnode), code);
×
1300
    metaFetchEntryFree(&pOldEntry);
×
1301
    return code;
×
1302
  }
1303

1304
  // update other stuff
1305
  pMeta->pVnode->config.vndStats.numOfNTables--;
1,822✔
1306
  pMeta->pVnode->config.vndStats.numOfNTimeSeries -= (pOldEntry->ntbEntry.schemaRow.nCols - 1);
1,822✔
1307

1308
#if 0
1309
  if (tbUids) {
1310
    if (taosArrayPush(tbUids, &uid) == NULL) {
1311
      rc = terrno;
1312
      goto _exit;
1313
    }
1314
  }
1315
#endif
1316

1317
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
1,822!
1318
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pOldEntry->uid, 0, NULL);
×
1319
    if (ret < 0) {
×
1320
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1321
    }
1322
  }
1323

1324
  metaFetchEntryFree(&pOldEntry);
1,822✔
1325
  return code;
1,822✔
1326
}
1327

1328
static int32_t metaHandleChildTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam, bool superDropped) {
4,540✔
1329
  int32_t code = TSDB_CODE_SUCCESS;
4,540✔
1330

1331
  const SMetaEntry *pEntry = pParam->pEntry;
4,540✔
1332
  const SMetaEntry *pChild = pParam->pOldEntry;
4,540✔
1333
  const SMetaEntry *pSuper = pParam->pSuperEntry;
4,540✔
1334

1335
  SMetaTableOp ops[] = {
4,540✔
1336
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1337
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1338
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1339
      {META_CHILD_IDX, META_TABLE_OP_DELETE},    //
1340
      {META_TAG_IDX, META_TABLE_OP_DELETE},      //
1341
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1342
      {META_TTL_IDX, META_TABLE_OP_DELETE},      //
1343
  };
1344

1345
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
36,273✔
1346
    SMetaTableOp *op = &ops[i];
31,749✔
1347

1348
    if (op->table == META_ENTRY_TABLE && superDropped) {
31,749✔
1349
      continue;
3,702✔
1350
    }
1351

1352
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
28,047✔
1353
    if (code) {
28,042✔
1354
      metaErr(TD_VID(pMeta->pVnode), code);
11!
1355
      return code;
×
1356
    }
1357
  }
1358

1359
  --pMeta->pVnode->config.vndStats.numOfCTables;
4,524✔
1360
  metaUpdateStbStats(pMeta, pParam->pSuperEntry->uid, -1, 0, -1);
4,524✔
1361
  int32_t ret = metaUidCacheClear(pMeta, pSuper->uid);
4,539✔
1362
  if (ret < 0) {
4,542!
1363
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1364
  }
1365

1366
  ret = metaTbGroupCacheClear(pMeta, pSuper->uid);
4,542✔
1367
  if (ret < 0) {
4,541!
1368
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1369
  }
1370
  return code;
4,541✔
1371
}
1372

1373
static int32_t metaHandleChildTableDrop(SMeta *pMeta, const SMetaEntry *pEntry, bool superDropped) {
4,541✔
1374
  int32_t     code = TSDB_CODE_SUCCESS;
4,541✔
1375
  SMetaEntry *pChild = NULL;
4,541✔
1376
  SMetaEntry *pSuper = NULL;
4,541✔
1377

1378
  // fetch old entry
1379
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pChild);
4,541✔
1380
  if (code) {
4,538!
1381
    metaErr(TD_VID(pMeta->pVnode), code);
×
1382
    return code;
×
1383
  }
1384

1385
  // fetch super entry
1386
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
4,538✔
1387
  if (code) {
4,540!
1388
    metaErr(TD_VID(pMeta->pVnode), code);
×
1389
    metaFetchEntryFree(&pChild);
×
1390
    return code;
×
1391
  }
1392

1393
  SMetaHandleParam param = {
4,540✔
1394
      .pEntry = pEntry,
1395
      .pOldEntry = pChild,
1396
      .pSuperEntry = pSuper,
1397
  };
1398

1399
  // do the drop
1400
  metaWLock(pMeta);
4,540✔
1401
  code = metaHandleChildTableDropImpl(pMeta, &param, superDropped);
4,540✔
1402
  metaULock(pMeta);
4,541✔
1403
  if (code) {
4,540!
1404
    metaErr(TD_VID(pMeta->pVnode), code);
×
1405
    metaFetchEntryFree(&pChild);
×
1406
    metaFetchEntryFree(&pSuper);
×
1407
    return code;
×
1408
  }
1409

1410
  // do other stuff
1411
  if (!metaTbInFilterCache(pMeta, pSuper->name, 1)) {
4,540!
1412
    int32_t      nCols = 0;
4,539✔
1413
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
4,539✔
1414
    if (metaGetStbStats(pMeta->pVnode, pSuper->uid, NULL, &nCols) == 0) {
4,539!
1415
      pStats->numOfTimeSeries -= nCols - 1;
4,540✔
1416
    }
1417
  }
1418

1419
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
4,540✔
1420
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pChild->uid, pSuper->uid, NULL);
10✔
1421
    if (ret < 0) {
10!
1422
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1423
    }
1424
  }
1425

1426
#if 0
1427
  if (tbUids) {
1428
    if (taosArrayPush(tbUids, &uid) == NULL) {
1429
      rc = terrno;
1430
      goto _exit;
1431
    }
1432
  }
1433

1434
  if ((type == TSDB_CHILD_TABLE) && tbUid) {
1435
    *tbUid = uid;
1436
  }
1437
#endif
1438
  metaFetchEntryFree(&pChild);
4,540✔
1439
  metaFetchEntryFree(&pSuper);
4,541✔
1440
  return code;
4,541✔
1441
}
1442

1443
static int32_t metaGetChildUidsOfSuperTable(SMeta *pMeta, tb_uid_t suid, SArray **childList) {
5,839✔
1444
  int32_t code = TSDB_CODE_SUCCESS;
5,839✔
1445
  void   *key = NULL;
5,839✔
1446
  int32_t keySize = 0;
5,839✔
1447
  int32_t c;
1448

1449
  *childList = taosArrayInit(64, sizeof(tb_uid_t));
5,839✔
1450
  if (*childList == NULL) {
5,840!
1451
    return terrno;
×
1452
  }
1453

1454
  TBC *cursor = NULL;
5,840✔
1455
  code = tdbTbcOpen(pMeta->pCtbIdx, &cursor, NULL);
5,840✔
1456
  if (code) {
5,840!
1457
    taosArrayDestroy(*childList);
×
1458
    *childList = NULL;
×
1459
    return code;
×
1460
  }
1461

1462
  int32_t rc = tdbTbcMoveTo(cursor,
5,840✔
1463
                            &(SCtbIdxKey){
5,840✔
1464
                                .suid = suid,
1465
                                .uid = INT64_MIN,
1466
                            },
1467
                            sizeof(SCtbIdxKey), &c);
1468
  if (rc < 0) {
5,839!
1469
    tdbTbcClose(cursor);
×
1470
    return 0;
×
1471
  }
1472

1473
  for (;;) {
1474
    if (tdbTbcNext(cursor, &key, &keySize, NULL, NULL) < 0) {
17,231✔
1475
      break;
3,619✔
1476
    }
1477

1478
    if (((SCtbIdxKey *)key)->suid < suid) {
13,613✔
1479
      continue;
2,458✔
1480
    } else if (((SCtbIdxKey *)key)->suid > suid) {
11,155✔
1481
      break;
2,222✔
1482
    }
1483

1484
    if (taosArrayPush(*childList, &(((SCtbIdxKey *)key)->uid)) == NULL) {
17,867!
1485
      tdbFreeClear(key);
×
1486
      tdbTbcClose(cursor);
×
1487
      taosArrayDestroy(*childList);
×
1488
      *childList = NULL;
×
1489
      return terrno;
×
1490
    }
1491
  }
1492

1493
  tdbTbcClose(cursor);
5,841✔
1494
  tdbFreeClear(key);
5,842✔
1495
  return code;
5,842✔
1496
}
1497

1498
static int32_t metaHandleSuperTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
2,551✔
1499
  int32_t           code = TSDB_CODE_SUCCESS;
2,551✔
1500
  const SMetaEntry *pEntry = pParam->pEntry;
2,551✔
1501

1502
  SMetaTableOp ops[] = {
2,551✔
1503
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1504
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1505
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1506
      {META_SUID_IDX, META_TABLE_OP_DELETE},     //
1507

1508
      // {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: here should be insert
1509
  };
1510

1511
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
12,754✔
1512
    SMetaTableOp *op = &ops[i];
10,202✔
1513

1514
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
10,202✔
1515
    if (TSDB_CODE_SUCCESS != code) {
10,203!
1516
      metaErr(TD_VID(pMeta->pVnode), code);
×
1517
      return code;
×
1518
    }
1519
  }
1520

1521
  int32_t ret = metaStatsCacheDrop(pMeta, pEntry->uid);
2,552✔
1522
  if (ret < 0) {
2,552✔
1523
    metaErr(TD_VID(pMeta->pVnode), ret);
382!
1524
  }
1525
  return code;
2,552✔
1526
}
1527

1528
static int32_t metaHandleNormalTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
234✔
1529
  int32_t code = TSDB_CODE_SUCCESS;
234✔
1530

1531
  const SMetaEntry *pEntry = pParam->pEntry;
234✔
1532

1533
  SMetaTableOp ops[] = {
234✔
1534
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
1535
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
1536
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
1537
      {META_TTL_IDX, META_TABLE_OP_UPDATA},       //
1538
  };
1539
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1,170✔
1540
    SMetaTableOp *op = &ops[i];
936✔
1541
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
936✔
1542
    if (code) {
936!
1543
      metaErr(TD_VID(pMeta->pVnode), code);
×
1544
      return code;
×
1545
    }
1546
  }
1547
#if 0
1548
  if (metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs) < 0) {
1549
    metaError("vgId:%d, failed to update change time:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
1550
  }
1551
#endif
1552
  return code;
234✔
1553
}
1554

1555
static int32_t metaHandleChildTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
701✔
1556
  int32_t code = TSDB_CODE_SUCCESS;
701✔
1557

1558
  const SMetaEntry *pEntry = pParam->pEntry;
701✔
1559
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
701✔
1560
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
701✔
1561

1562
  SMetaTableOp ops[] = {
701✔
1563
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},  //
1564
      {META_UID_IDX, META_TABLE_OP_UPDATA},      //
1565
      {META_TAG_IDX, META_TABLE_OP_UPDATA},      //
1566
      {META_CHILD_IDX, META_TABLE_OP_UPDATA},    //
1567
      {META_TTL_IDX, META_TABLE_OP_UPDATA},      //
1568
  };
1569

1570
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
4,206✔
1571
    SMetaTableOp *op = &ops[i];
3,505✔
1572
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
3,505✔
1573
    if (code) {
3,505!
1574
      metaErr(TD_VID(pMeta->pVnode), code);
×
1575
      return code;
×
1576
    }
1577
  }
1578

1579
  if (metaUidCacheClear(pMeta, pSuperEntry->uid) < 0) {
701!
1580
    metaErr(TD_VID(pMeta->pVnode), code);
×
1581
  }
1582

1583
  if (metaTbGroupCacheClear(pMeta, pSuperEntry->uid) < 0) {
701!
1584
    metaErr(TD_VID(pMeta->pVnode), code);
×
1585
  }
1586
  return code;
701✔
1587
#if 0
1588
  if (metaUpdateChangeTime(pMeta, ctbEntry.uid, pReq->ctimeMs) < 0) {
1589
    metaError("meta/table: failed to update change time:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
1590
  }
1591
#endif
1592
}
1593

1594
static int32_t metaHandleSuperTableUpdateImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
9,686✔
1595
  int32_t code = TSDB_CODE_SUCCESS;
9,686✔
1596

1597
  const SMetaEntry *pEntry = pParam->pEntry;
9,686✔
1598
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
9,686✔
1599

1600
  SMetaTableOp ops[] = {
9,686✔
1601
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
1602
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
1603
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
1604
  };
1605

1606
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
38,747✔
1607
    SMetaTableOp *op = &ops[i];
29,067✔
1608
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
29,067✔
1609
    if (code) {
29,059!
1610
      metaErr(TD_VID(pMeta->pVnode), code);
×
1611
      return code;
×
1612
    }
1613
  }
1614

1615
  if (TSDB_CODE_SUCCESS == code) {
9,680✔
1616
    metaUpdateStbStats(pMeta, pEntry->uid, 0, pEntry->stbEntry.schemaRow.nCols - pOldEntry->stbEntry.schemaRow.nCols,
9,679✔
1617
                       pEntry->stbEntry.keep);
9,679✔
1618
  }
1619

1620
  return code;
9,696✔
1621
}
1622

1623
static int32_t metaHandleSuperTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
9,678✔
1624
  int32_t code = TSDB_CODE_SUCCESS;
9,678✔
1625

1626
  SMetaEntry *pOldEntry = NULL;
9,678✔
1627

1628
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
9,678✔
1629
  if (code) {
9,698!
UNCOV
1630
    metaErr(TD_VID(pMeta->pVnode), code);
×
1631
    return code;
×
1632
  }
1633

1634
  SMetaHandleParam param = {
9,698✔
1635
      .pEntry = pEntry,
1636
      .pOldEntry = pOldEntry,
1637
  };
1638
  metaWLock(pMeta);
9,698✔
1639
  code = metaHandleSuperTableUpdateImpl(pMeta, &param);
9,696✔
1640
  metaULock(pMeta);
9,688✔
1641
  if (code) {
9,689!
UNCOV
1642
    metaErr(TD_VID(pMeta->pVnode), code);
×
1643
    metaFetchEntryFree(&pOldEntry);
×
1644
    return code;
×
1645
  }
1646

1647
  int     nCols = pEntry->stbEntry.schemaRow.nCols;
9,689✔
1648
  int     onCols = pOldEntry->stbEntry.schemaRow.nCols;
9,689✔
1649
  int32_t deltaCol = nCols - onCols;
9,689✔
1650
  bool    updStat = deltaCol != 0 && !metaTbInFilterCache(pMeta, pEntry->name, 1);
9,689!
1651

1652
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
9,694✔
1653
    STsdb  *pTsdb = pMeta->pVnode->pTsdb;
2,124✔
1654
    SArray *uids = NULL; /*taosArrayInit(8, sizeof(int64_t));
2,124✔
1655
     if (uids == NULL) {
1656
       metaErr(TD_VID(pMeta->pVnode), code);
1657
       metaFetchEntryFree(&pOldEntry);
1658
       return terrno;
1659
       }*/
1660
    if (deltaCol == 1) {
2,124✔
1661
      int16_t cid = pEntry->stbEntry.schemaRow.pSchema[nCols - 1].colId;
64✔
1662
      int8_t  col_type = pEntry->stbEntry.schemaRow.pSchema[nCols - 1].type;
64✔
1663

1664
      code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &uids);
64✔
1665
      if (code) {
64!
UNCOV
1666
        metaErr(TD_VID(pMeta->pVnode), code);
×
1667
        metaFetchEntryFree(&pOldEntry);
×
1668
        return code;
×
1669
      }
1670
      TAOS_CHECK_RETURN(tsdbCacheNewSTableColumn(pTsdb, uids, cid, col_type));
64!
1671
    } else if (deltaCol == -1) {
2,060✔
1672
      int16_t cid = -1;
64✔
1673
      bool    hasPrimaryKey = false;
64✔
1674
      if (onCols >= 2) {
64!
1675
        hasPrimaryKey = (pOldEntry->stbEntry.schemaRow.pSchema[1].flags & COL_IS_KEY) ? true : false;
64✔
1676
      }
1677
      for (int i = 0, j = 0; i < nCols && j < onCols; ++i, ++j) {
416!
1678
        if (pEntry->stbEntry.schemaRow.pSchema[i].colId != pOldEntry->stbEntry.schemaRow.pSchema[j].colId) {
384✔
1679
          cid = pOldEntry->stbEntry.schemaRow.pSchema[j].colId;
32✔
1680
          break;
32✔
1681
        }
1682
      }
1683

1684
      if (cid != -1) {
64✔
1685
        code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &uids);
32✔
1686
        if (code) {
32!
UNCOV
1687
          metaErr(TD_VID(pMeta->pVnode), code);
×
1688
          metaFetchEntryFree(&pOldEntry);
×
1689
          return code;
×
1690
        }
1691
        TAOS_CHECK_RETURN(tsdbCacheDropSTableColumn(pTsdb, uids, cid, hasPrimaryKey));
32!
1692
      }
1693
    }
1694
    if (uids) taosArrayDestroy(uids);
2,124✔
1695

1696
    tsdbCacheInvalidateSchema(pTsdb, pEntry->uid, -1, pEntry->stbEntry.schemaRow.version);
2,124✔
1697
  }
1698
  if (updStat) {
9,694✔
1699
    int64_t ctbNum = 0;
2,960✔
1700
    int32_t ret = metaGetStbStats(pMeta->pVnode, pEntry->uid, &ctbNum, NULL);
2,960✔
1701
    if (ret < 0) {
2,962!
UNCOV
1702
      metaError("vgId:%d, failed to get stb stats:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
×
1703
                pEntry->uid, tstrerror(ret));
1704
    }
1705
    pMeta->pVnode->config.vndStats.numOfTimeSeries += (ctbNum * deltaCol);
2,962✔
1706
    if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
2,962✔
1707
  }
1708
  metaFetchEntryFree(&pOldEntry);
9,696✔
1709
  return code;
9,688✔
1710
}
1711

1712
static int32_t metaHandleChildTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
701✔
1713
  int32_t code = TSDB_CODE_SUCCESS;
701✔
1714

1715
  SMetaEntry *pOldEntry = NULL;
701✔
1716
  SMetaEntry *pSuperEntry = NULL;
701✔
1717

1718
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
701✔
1719
  if (code) {
701!
UNCOV
1720
    metaErr(TD_VID(pMeta->pVnode), code);
×
1721
    return code;
×
1722
  }
1723

1724
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
701✔
1725
  if (code) {
701!
UNCOV
1726
    metaErr(TD_VID(pMeta->pVnode), code);
×
1727
    metaFetchEntryFree(&pOldEntry);
×
1728
    return code;
×
1729
  }
1730

1731
  SMetaHandleParam param = {
701✔
1732
      .pEntry = pEntry,
1733
      .pOldEntry = pOldEntry,
1734
      .pSuperEntry = pSuperEntry,
1735
  };
1736

1737
  metaWLock(pMeta);
701✔
1738
  code = metaHandleChildTableUpdateImpl(pMeta, &param);
701✔
1739
  metaULock(pMeta);
701✔
1740
  if (code) {
701!
UNCOV
1741
    metaErr(TD_VID(pMeta->pVnode), code);
×
1742
    metaFetchEntryFree(&pOldEntry);
×
1743
    metaFetchEntryFree(&pSuperEntry);
×
1744
    return code;
×
1745
  }
1746

1747
  metaFetchEntryFree(&pOldEntry);
701✔
1748
  metaFetchEntryFree(&pSuperEntry);
701✔
1749
  return code;
701✔
1750
}
1751

1752
static int32_t metaHandleNormalTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
234✔
1753
  int32_t     code = TSDB_CODE_SUCCESS;
234✔
1754
  SMetaEntry *pOldEntry = NULL;
234✔
1755

1756
  // fetch old entry
1757
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
234✔
1758
  if (code) {
234!
UNCOV
1759
    metaErr(TD_VID(pMeta->pVnode), code);
×
1760
    return code;
×
1761
  }
1762

1763
  // handle update
1764
  SMetaHandleParam param = {
234✔
1765
      .pEntry = pEntry,
1766
      .pOldEntry = pOldEntry,
1767
  };
1768
  metaWLock(pMeta);
234✔
1769
  code = metaHandleNormalTableUpdateImpl(pMeta, &param);
234✔
1770
  metaULock(pMeta);
234✔
1771
  if (code) {
234!
UNCOV
1772
    metaErr(TD_VID(pMeta->pVnode), code);
×
1773
    metaFetchEntryFree(&pOldEntry);
×
1774
    return code;
×
1775
  }
1776

1777
  // do other stuff
1778
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) &&
234✔
1779
      pEntry->ntbEntry.schemaRow.version != pOldEntry->ntbEntry.schemaRow.version) {
6!
1780
#if 0
1781
    {  // for add column
1782
      int16_t cid = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].colId;
1783
      int8_t  col_type = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].type;
1784
      int32_t ret = tsdbCacheNewNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, col_type);
1785
      if (ret < 0) {
1786
        terrno = ret;
1787
        goto _err;
1788
      }
1789
    }
1790
    {  // for drop column
1791

1792
      if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
1793
        int16_t cid = pColumn->colId;
1794

1795
        if (tsdbCacheDropNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, hasPrimayKey) != 0) {
1796
          metaError("vgId:%d, failed to drop ntable column:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name,
1797
                    entry.uid);
1798
        }
1799
        tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, entry.uid, pSchema->version);
1800
      }
1801
    }
1802
    }
1803
#endif
1804
    tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, pEntry->uid, pEntry->ntbEntry.schemaRow.version);
6✔
1805
  }
1806
  int32_t deltaCol = pEntry->ntbEntry.schemaRow.nCols - pOldEntry->ntbEntry.schemaRow.nCols;
234✔
1807
  pMeta->pVnode->config.vndStats.numOfNTimeSeries += deltaCol;  
234✔
1808
  if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
234✔
1809
  metaFetchEntryFree(&pOldEntry);
234✔
1810
  return code;
234✔
1811
}
1812

1813
static int32_t metaHandleSuperTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
2,551✔
1814
  int32_t     code = TSDB_CODE_SUCCESS;
2,551✔
1815
  SArray     *childList = NULL;
2,551✔
1816
  SMetaEntry *pOldEntry = NULL;
2,551✔
1817

1818
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
2,551✔
1819
  if (code) {
2,552!
UNCOV
1820
    metaErr(TD_VID(pMeta->pVnode), code);
×
1821
    return code;
×
1822
  }
1823

1824
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childList);
2,552✔
1825
  if (code) {
2,552!
UNCOV
1826
    metaErr(TD_VID(pMeta->pVnode), code);
×
1827
    metaFetchEntryFree(&pOldEntry);
×
1828
    return code;
×
1829
  }
1830

1831
  if (tsdbCacheDropSubTables(pMeta->pVnode->pTsdb, childList, pEntry->uid) < 0) {
2,552!
UNCOV
1832
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
×
1833
              pEntry->uid, tstrerror(terrno));
1834
  }
1835

1836
  // loop to drop all child tables
1837
  for (int32_t i = 0; i < taosArrayGetSize(childList); i++) {
6,255✔
1838
    SMetaEntry childEntry = {
7,406✔
1839
        .version = pEntry->version,
3,703✔
1840
        .uid = *(tb_uid_t *)taosArrayGet(childList, i),
3,703✔
1841
        .type = -TSDB_CHILD_TABLE,
1842
    };
1843

1844
    code = metaHandleChildTableDrop(pMeta, &childEntry, true);
3,703✔
1845
    if (code) {
3,703!
UNCOV
1846
      metaErr(TD_VID(pMeta->pVnode), code);
×
1847
    }
1848
  }
1849

1850
  // do drop super table
1851
  SMetaHandleParam param = {
2,551✔
1852
      .pEntry = pEntry,
1853
      .pOldEntry = pOldEntry,
1854
  };
1855
  metaWLock(pMeta);
2,551✔
1856
  code = metaHandleSuperTableDropImpl(pMeta, &param);
2,551✔
1857
  metaULock(pMeta);
2,552✔
1858
  if (code) {
2,552!
UNCOV
1859
    metaErr(TD_VID(pMeta->pVnode), code);
×
1860
    taosArrayDestroy(childList);
×
1861
    metaFetchEntryFree(&pOldEntry);
×
1862
    return code;
×
1863
  }
1864

1865
  // do other stuff
1866
  metaUpdTimeSeriesNum(pMeta);
2,552✔
1867

1868
  // free resource and return
1869
  taosArrayDestroy(childList);
2,552✔
1870
  metaFetchEntryFree(&pOldEntry);
2,552✔
1871
  return code;
2,552✔
1872
}
1873

1874
int32_t metaHandleEntry2(SMeta *pMeta, const SMetaEntry *pEntry) {
199,904✔
1875
  int32_t   code = TSDB_CODE_SUCCESS;
199,904✔
1876
  int32_t   vgId = TD_VID(pMeta->pVnode);
199,904✔
1877
  SMetaInfo info = {0};
199,904✔
1878
  int8_t    type = pEntry->type > 0 ? pEntry->type : -pEntry->type;
199,904✔
1879

1880
  if (NULL == pMeta || NULL == pEntry) {
199,904!
UNCOV
1881
    metaError("%s failed at %s:%d since invalid parameter", __func__, __FILE__, __LINE__);
×
1882
    return TSDB_CODE_INVALID_PARA;
×
1883
  }
1884

1885
  if (pEntry->type > 0) {
200,282✔
1886
    bool isExist = false;
195,076✔
1887
    if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) {
195,076✔
1888
      isExist = true;
10,629✔
1889
    }
1890

1891
    switch (type) {
195,098!
1892
      case TSDB_SUPER_TABLE: {
37,014✔
1893
        if (isExist) {
37,014✔
1894
          code = metaHandleSuperTableUpdate(pMeta, pEntry);
9,694✔
1895
        } else {
1896
          code = metaHandleSuperTableCreate(pMeta, pEntry);
27,320✔
1897
        }
1898
        break;
37,025✔
1899
      }
1900
      case TSDB_CHILD_TABLE: {
143,439✔
1901
        if (isExist) {
143,439✔
1902
          code = metaHandleChildTableUpdate(pMeta, pEntry);
701✔
1903
        } else {
1904
          code = metaHandleChildTableCreate(pMeta, pEntry);
142,738✔
1905
        }
1906
        break;
143,432✔
1907
      }
1908
      case TSDB_NORMAL_TABLE: {
14,651✔
1909
        if (isExist) {
14,651✔
1910
          code = metaHandleNormalTableUpdate(pMeta, pEntry);
234✔
1911
        } else {
1912
          code = metaHandleNormalTableCreate(pMeta, pEntry);
14,417✔
1913
        }
1914
        break;
14,652✔
1915
      }
UNCOV
1916
      default: {
×
1917
        code = TSDB_CODE_INVALID_PARA;
×
1918
        break;
×
1919
      }
1920
    }
1921
  } else {
1922
    switch (type) {
5,206!
1923
      case TSDB_SUPER_TABLE: {
2,551✔
1924
        code = metaHandleSuperTableDrop(pMeta, pEntry);
2,551✔
1925
        break;
2,552✔
1926
      }
1927
      case TSDB_CHILD_TABLE: {
838✔
1928
        code = metaHandleChildTableDrop(pMeta, pEntry, false);
838✔
1929
        break;
838✔
1930
      }
1931
      case TSDB_NORMAL_TABLE: {
1,822✔
1932
        code = metaHandleNormalTableDrop(pMeta, pEntry);
1,822✔
1933
        break;
1,822✔
1934
      }
UNCOV
1935
      default: {
×
1936
        code = TSDB_CODE_INVALID_PARA;
×
1937
        break;
×
1938
      }
1939
    }
1940
  }
1941

1942
  if (TSDB_CODE_SUCCESS == code) {
200,310!
1943
    pMeta->changed = true;
200,310✔
1944
    metaDebug("vgId:%d, %s success, version:%" PRId64 " type:%d uid:%" PRId64 " name:%s", vgId, __func__,
200,310✔
1945
              pEntry->version, pEntry->type, pEntry->uid, pEntry->type > 0 ? pEntry->name : "");
1946
  } else {
UNCOV
1947
    metaErr(vgId, code);
×
1948
  }
1949
  TAOS_RETURN(code);
200,320✔
1950
}
1951

1952
void metaHandleSyncEntry(SMeta *pMeta, const SMetaEntry *pEntry) {
5,200✔
1953
  int32_t code = TSDB_CODE_SUCCESS;
5,200✔
1954
  code = metaHandleEntry2(pMeta, pEntry);
5,200✔
1955
  if (code) {
5,200!
UNCOV
1956
    metaErr(TD_VID(pMeta->pVnode), code);
×
1957
  }
1958
  return;
5,200✔
1959
}
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