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

taosdata / TDengine / #3655

14 Mar 2025 08:10AM UTC coverage: 59.532% (+22.6%) from 36.951%
#3655

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.

139898 of 302527 branches covered (46.24%)

Branch coverage included in aggregate %.

71 of 99 new or added lines in 12 files covered. (71.72%)

2746 existing lines in 57 files now uncovered.

220499 of 302857 relevant lines covered (72.81%)

5509252.7 hits per line

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

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

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

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

96
  tDecoderInit(&decoder, value, valueSize);
129,996✔
97
  code = metaDecodeEntry(&decoder, &entry);
129,983✔
98
  if (code) {
129,917!
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);
129,917✔
107
  if (code) {
129,973!
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);
129,973✔
116
  tDecoderClear(&decoder);
129,969✔
117
  return code;
129,990✔
118
}
119

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

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

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

141
void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); }
129,957✔
142

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

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

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

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

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

180
  // put to tdb
181
  if (META_TABLE_OP_INSERT == op) {
149,038✔
182
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
137,564✔
183
  } else if (META_TABLE_OP_UPDATA == op) {
11,474✔
184
    code = tdbTbUpsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
7,498✔
185
  } else if (META_TABLE_OP_DELETE == op) {
3,976!
186
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
3,976✔
187
  } else {
188
    code = TSDB_CODE_INVALID_PARA;
×
189
  }
190
  if (TSDB_CODE_SUCCESS != code) {
149,105!
191
    metaErr(vgId, code);
×
192
  }
193
  taosMemoryFree(value);
149,096!
194
  return code;
149,070✔
195
}
196

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

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

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

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

217
  const SMetaEntry     *pEntry = pParam->pEntry;
37,715✔
218
  const SSchemaWrapper *pSchema = NULL;
37,715✔
219
  if (pEntry->type == TSDB_SUPER_TABLE) {
37,715✔
220
    pSchema = &pEntry->stbEntry.schemaRow;
24,333✔
221
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
13,382✔
222
    pSchema = &pEntry->ntbEntry.schemaRow;
13,380✔
223
  } else {
224
    return TSDB_CODE_INVALID_PARA;
2✔
225
  }
226
  SSkmDbKey key = {
37,713✔
227
      .uid = pEntry->uid,
37,713✔
228
      .sver = pSchema->version,
37,713✔
229
  };
230

231
  // encode schema
232
  tEncodeSize(tEncodeSSchemaWrapper, pSchema, valueSize, code);
75,432✔
233
  if (TSDB_CODE_SUCCESS != code) {
37,589!
234
    metaErr(vgId, code);
×
235
    return code;
×
236
  }
237

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

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

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

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

281
  if (pOldColumn && pNewColumn) {
89,728✔
282
    if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
87,862✔
283
      return TSDB_CODE_SUCCESS;
3,330✔
284
    } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) {
84,532!
285
      action = DROP_INDEX;
46✔
286
    } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
84,486!
287
      action = ADD_INDEX;
27✔
288
    } else {
289
      return TSDB_CODE_SUCCESS;
84,459✔
290
    }
291
  } else if (pOldColumn) {
1,866✔
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,078!
299
      action = ADD_INDEX;
×
300
    } else {
301
      return TSDB_CODE_SUCCESS;
1,078✔
302
    }
303
  }
304

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

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

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

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

338
      code = tdbTbInsert(pMeta->pTagIdx, pTagIdxKey, tagIdxKeySize, NULL, 0, pMeta->txn);
111✔
339
      if (code) {
111!
340
        metaErr(TD_VID(pMeta->pVnode), code);
×
341
        taosArrayDestroy(childTables);
×
342
        metaFetchEntryFree(&pChildEntry);
×
343
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
344
        return code;
×
345
      }
346
    } else {
347
      code = metaFetchTagIdxKey(pMeta, pChildEntry, pOldColumn, &pTagIdxKey, &tagIdxKeySize);
121✔
348
      if (code) {
121!
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);
121✔
356
      if (code) {
121!
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);
232✔
366
    metaFetchEntryFree(&pChildEntry);
232✔
367
  }
368

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

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

380
  int32_t iOld = 0, iNew = 0;
3,422✔
381
  for (; iOld < pOldTagSchema->nCols && iNew < pNewTagSchema->nCols;) {
91,900✔
382
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
88,473✔
383
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
88,473✔
384

385
    if (pOldColumn->colId == pNewColumn->colId) {
88,473✔
386
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
87,861✔
387
      if (code) {
87,867✔
388
        metaErr(TD_VID(pMeta->pVnode), code);
1!
389
        return code;
×
390
      }
391

392
      iOld++;
87,866✔
393
      iNew++;
87,866✔
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++) {
3,603✔
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++) {
4,513✔
423
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
1,087✔
424
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, NULL, pNewColumn);
1,087✔
425
    if (code) {
1,087✔
426
      metaErr(TD_VID(pMeta->pVnode), code);
1!
427
      return code;
×
428
    }
429
  }
430

431
  return code;
3,426✔
432
}
433

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

437
  const SMetaEntry *pEntry = pParam->pEntry;
40,681✔
438
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
40,681✔
439

440
  if (NULL == pOldEntry) {
40,681✔
441
    return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
34,393✔
442
  }
443

444
  if (pEntry->type == TSDB_NORMAL_TABLE) {
6,288✔
445
    // check row schema
446
    if (pOldEntry->ntbEntry.schemaRow.version != pEntry->ntbEntry.schemaRow.version) {
232✔
447
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
191✔
448
    }
449
  } else if (pEntry->type == TSDB_SUPER_TABLE) {
6,056!
450
    // check row schema
451
    if (pOldEntry->stbEntry.schemaRow.version != pEntry->stbEntry.schemaRow.version) {
6,567✔
452
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
3,141✔
453
    }
454

455
    // check tag schema
456
    code = metaUpdateSuperTableTagSchema(pMeta, pParam);
3,426✔
457
    if (code) {
3,424✔
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;
3,464✔
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) {
145,045✔
476
  pInfo->uid = pEntry->uid;
145,045✔
477
  pInfo->version = pEntry->version;
145,045✔
478
  if (pEntry->type == TSDB_SUPER_TABLE) {
145,045✔
479
    pInfo->suid = pEntry->uid;
27,751✔
480
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
27,751✔
481
  } else if (pEntry->type == TSDB_CHILD_TABLE) {
117,294✔
482
    pInfo->suid = pEntry->ctbEntry.suid;
103,931✔
483
    pInfo->skmVer = 0;
103,931✔
484
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
13,363!
485
    pInfo->suid = 0;
13,421✔
486
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
13,421✔
487
  }
488
}
145,045✔
489

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

494
  const SMetaEntry *pEntry = pParam->pEntry;
145,037✔
495

496
  // update cache
497
  SMetaInfo info = {0};
145,037✔
498
  metaBuildEntryInfo(pEntry, &info);
145,037✔
499
  code = metaCacheUpsert(pMeta, &info);
145,095✔
500
  if (code) {
145,003!
501
    metaErr(vgId, code);
×
502
  }
503

504
  // put to tdb
505
  SUidIdxVal value = {
144,968✔
506
      .suid = info.suid,
144,968✔
507
      .skmVer = info.skmVer,
144,968✔
508
      .version = pEntry->version,
144,968✔
509
  };
510
  if (META_TABLE_OP_INSERT == op) {
144,968✔
511
    code = tdbTbInsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
137,487✔
512
  } else if (META_TABLE_OP_UPDATA == op) {
7,481!
513
    code = tdbTbUpsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
7,488✔
514
  }
515
  return code;
145,144✔
516
}
517

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

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

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

529
  const SMetaEntry *pEntry = pParam->pOldEntry;
6,614✔
530

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

537
  // delete cache
538
  (void)metaCacheDrop(pMeta, pEntry->uid);
6,617✔
539
  return code;
6,619✔
540
}
541

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

546
  const SMetaEntry *pEntry = pParam->pEntry;
137,577✔
547

548
  if (META_TABLE_OP_INSERT == op) {
137,577!
549
    code = tdbTbInsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
137,592✔
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;
137,650✔
558
}
559

560
static int32_t metaNameIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
137,582✔
561
  int32_t code = TSDB_CODE_SUCCESS;
137,582✔
562
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
137,582✔
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) {
6,618✔
570
  int32_t code = TSDB_CODE_SUCCESS;
6,618✔
571

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

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

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

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

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

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

605
  const SMetaEntry *pEntry = pParam->pEntry;
103,707✔
606

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

612
  if (META_TABLE_OP_INSERT == op) {
103,707✔
613
    code = tdbTbInsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
103,237✔
614
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
103,237✔
615
  } else if (META_TABLE_OP_UPDATA == op) {
470!
616
    code = tdbTbUpsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
476✔
617
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
476✔
618
  } else {
619
    code = TSDB_CODE_INVALID_PARA;
×
620
  }
621
  return code;
103,723✔
622
}
623

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

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

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

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

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

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

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

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

665
  if (tTagGet((const STag *)pEntry->ctbEntry.pTags, &tagVal)) {
107,727✔
666
    if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
107,347!
667
      pTagData = tagVal.pData;
17,109✔
668
      nTagData = (int32_t)tagVal.nData;
17,109✔
669
    } else {
670
      pTagData = &(tagVal.i64);
90,238✔
671
      nTagData = tDataTypes[pTagColumn->type].bytes;
90,238✔
672
    }
673
  } else {
674
    if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
377!
675
      nTagData = tDataTypes[pTagColumn->type].bytes;
231✔
676
    }
677
  }
678

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

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

691
static void metaFetchTagIdxKeyFree(STagIdxKey **ppTagIdxKey) {
107,740✔
692
  metaDestroyTagIdxKey(*ppTagIdxKey);
107,740✔
693
  *ppTagIdxKey = NULL;
107,736✔
694
}
107,736✔
695

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

699
  const SMetaEntry *pEntry = pParam->pEntry;
103,239✔
700
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
103,239✔
701

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

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

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

722
      if (!IS_IDX_ON(pTagColumn)) {
363,261✔
723
        continue;
260,202✔
724
      }
725

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

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

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

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

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

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

765
    code = metaSaveJsonVarToIdx(pMeta, pEntry, &pTagSchema->pSchema[0]);
8✔
766
    if (code) {
8!
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;
476✔
819
}
820

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

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

831
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
3,480✔
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++) {
19,897✔
839
      pTagColumn = &pTagSchema->pSchema[i];
16,425✔
840
      if (!IS_IDX_ON(pTagColumn)) {
16,425✔
841
        continue;
12,910✔
842
      }
843

844
      STagIdxKey *pTagIdxKey = NULL;
3,515✔
845
      int32_t     nTagIdxKey;
846

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

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

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

869
  const SMetaEntry *pEntry;
870
  if (META_TABLE_OP_DELETE == op) {
121,666✔
871
    pEntry = pParam->pOldEntry;
5,263✔
872
  } else {
873
    pEntry = pParam->pEntry;
116,403✔
874
  }
875

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

880
  if (TSDB_CHILD_TABLE == pEntry->type) {
121,666✔
881
    key.btime = pEntry->ctbEntry.btime;
106,698✔
882
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
14,968!
883
    key.btime = pEntry->ntbEntry.btime;
14,970✔
884
  } else {
885
    return TSDB_CODE_INVALID_PARA;
×
886
  }
887

888
  if (META_TABLE_OP_INSERT == op) {
121,668✔
889
    code = tdbTbInsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
116,407✔
890
  } else if (META_TABLE_OP_UPDATA == op) {
5,261!
891
    code = tdbTbUpsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
×
892
  } else if (META_TABLE_OP_DELETE == op) {
5,261!
893
    code = tdbTbDelete(pMeta->pBtimeIdx, &key, sizeof(key), pMeta->txn);
5,264✔
894
  } else {
895
    code = TSDB_CODE_INVALID_PARA;
×
896
  }
897
  if (code) {
121,687!
898
    metaErr(TD_VID(pMeta->pVnode), code);
×
899
  }
900
  return code;
121,689✔
901
}
902

903
static int32_t metaBtimeIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
116,412✔
904
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
116,412✔
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) {
5,263✔
912
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_DELETE);
5,263✔
913
}
914

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

919
  STtlUpdTtlCtx ctx = {
116,437✔
920
      .uid = pEntry->uid,
116,437✔
921
      .pTxn = pMeta->txn,
116,437✔
922
  };
923
  if (TSDB_CHILD_TABLE == pEntry->type) {
116,437✔
924
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
103,238✔
925
    ctx.changeTimeMs = pEntry->ctbEntry.btime;
103,238✔
926
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
13,199!
927
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
13,199✔
928
    ctx.changeTimeMs = pEntry->ntbEntry.btime;
13,199✔
929
  } else {
930
    return TSDB_CODE_INVALID_PARA;
×
931
  }
932

933
  int32_t ret = ttlMgrInsertTtl(pMeta->pTtlMgr, &ctx);
116,437✔
934
  if (ret < 0) {
116,435!
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;
116,436✔
938
}
939

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

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

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

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

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

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

965
  return TSDB_CODE_SUCCESS;
930✔
966
}
967

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

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

977
  if (TSDB_CHILD_TABLE == pEntry->type) {
5,287✔
978
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
3,494✔
979
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
1,793!
980
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
1,793✔
981
  } else {
982
    code = TSDB_CODE_INVALID_PARA;
×
983
  }
984

985
  if (TSDB_CODE_SUCCESS == code) {
5,287!
986
    int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
5,287✔
987
    if (ret < 0) {
5,287!
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;
5,286✔
993
}
994

995
static void metaTimeSeriesNotifyCheck(SMeta *pMeta) {
119,007✔
996
#if defined(TD_ENTERPRISE)
997
  int64_t nTimeSeries = metaGetTimeSeriesNum(pMeta, 0);
119,007✔
998
  int64_t deltaTS = nTimeSeries - pMeta->pVnode->config.vndStats.numOfReportedTimeSeries;
119,015✔
999
  if (deltaTS > tsTimeSeriesThreshold) {
119,015✔
1000
    if (0 == atomic_val_compare_exchange_8(&dmNotifyHdl.state, 1, 2)) {
74,596✔
1001
      if (tsem_post(&dmNotifyHdl.sem) != 0) {
74,594!
1002
        metaError("vgId:%d, failed to post semaphore, errno:%d", TD_VID(pMeta->pVnode), ERRNO);
×
1003
      }
1004
    }
1005
  }
1006
#endif
1007
}
119,027✔
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) {
21,190✔
1068
  int32_t code = TSDB_CODE_SUCCESS;
21,190✔
1069

1070
  SMetaTableOp ops[] = {
21,190✔
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++) {
127,012✔
1079
    SMetaTableOp          *op = &ops[i];
105,851✔
1080
    const SMetaHandleParam param = {
105,851✔
1081
        .pEntry = pEntry,
1082
    };
1083

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

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

1096
  metaWLock(pMeta);
21,204✔
1097
  code = metaHandleSuperTableCreateImpl(pMeta, pEntry);
21,221✔
1098
  metaULock(pMeta);
21,178✔
1099

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

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

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

1114
  SMetaTableOp ops[] = {
13,189✔
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++) {
92,303✔
1124
    SMetaTableOp *op = &ops[i];
79,120✔
1125

1126
    SMetaHandleParam param = {
79,120✔
1127
        .pEntry = pEntry,
1128
    };
1129

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

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

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

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

1152
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
13,189✔
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);
13,189✔
1159
  } else {
1160
    metaErr(TD_VID(pMeta->pVnode), code);
×
1161
  }
1162
  return code;
13,190✔
1163
}
1164

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

1168
  SMetaTableOp ops[] = {
103,236✔
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++) {
825,297✔
1179
    SMetaTableOp *op = &ops[i];
722,190✔
1180

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

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

1193
  if (TSDB_CODE_SUCCESS == code) {
103,107!
1194
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
103,223✔
1195
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
103,228✔
1196
    if (ret < 0) {
103,249!
1197
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1198
    }
1199

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

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

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

1219
  // update TDB
1220
  metaWLock(pMeta);
103,230✔
1221
  code = metaHandleChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
103,245✔
1222
  metaULock(pMeta);
103,227✔
1223

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

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

1237
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
103,238✔
1238
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, pEntry->ctbEntry.suid, NULL);
1,958✔
1239
      if (rc < 0) {
1,958!
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);
103,238✔
1249
  metaFetchEntryFree(&pSuperEntry);
103,236✔
1250
  return code;
103,252✔
1251
}
1252

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

1256
  SMetaTableOp ops[] = {
1,783✔
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,698✔
1267
    SMetaTableOp *op = &ops[i];
8,915✔
1268
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
8,915✔
1269
    if (code) {
8,915!
1270
      const SMetaEntry *pEntry = pParam->pEntry;
×
1271
      metaErr(TD_VID(pMeta->pVnode), code);
×
1272
    }
1273
  }
1274

1275
  return code;
1,783✔
1276
}
1277

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

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

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

1294
  // do the drop
1295
  metaWLock(pMeta);
1,783✔
1296
  code = metaHandleNormalTableDropImpl(pMeta, &param);
1,783✔
1297
  metaULock(pMeta);
1,783✔
1298
  if (code) {
1,783!
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,783✔
1306
  pMeta->pVnode->config.vndStats.numOfNTimeSeries -= (pOldEntry->ntbEntry.schemaRow.nCols - 1);
1,783✔
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,783!
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,783✔
1325
  return code;
1,783✔
1326
}
1327

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

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

1335
  SMetaTableOp ops[] = {
3,479✔
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++) {
27,802✔
1346
    SMetaTableOp *op = &ops[i];
24,332✔
1347

1348
    if (op->table == META_ENTRY_TABLE && superDropped) {
24,332✔
1349
      continue;
2,641✔
1350
    }
1351

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

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

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

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

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

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

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

1399
  // do the drop
1400
  metaWLock(pMeta);
3,480✔
1401
  code = metaHandleChildTableDropImpl(pMeta, &param, superDropped);
3,479✔
1402
  metaULock(pMeta);
3,480✔
1403
  if (code) {
3,479!
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)) {
3,479!
1412
    int32_t      nCols = 0;
3,478✔
1413
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
3,478✔
1414
    if (metaGetStbStats(pMeta->pVnode, pSuper->uid, NULL, &nCols) == 0) {
3,478!
1415
      pStats->numOfTimeSeries -= nCols - 1;
3,479✔
1416
    }
1417
  }
1418

1419
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
3,479✔
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);
3,479✔
1439
  metaFetchEntryFree(&pSuper);
3,480✔
1440
  return code;
3,480✔
1441
}
1442

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

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

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

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

1473
  for (;;) {
1474
    if (tdbTbcNext(cursor, &key, &keySize, NULL, NULL) < 0) {
5,072✔
1475
      break;
1,473✔
1476
    }
1477

1478
    if (((SCtbIdxKey *)key)->suid < suid) {
3,602✔
1479
      continue;
425✔
1480
    } else if (((SCtbIdxKey *)key)->suid > suid) {
3,177✔
1481
      break;
68✔
1482
    }
1483

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

1493
  tdbTbcClose(cursor);
1,541✔
1494
  tdbFreeClear(key);
1,541✔
1495
  return code;
1,541✔
1496
}
1497

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

1502
  SMetaTableOp ops[] = {
1,354✔
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++) {
6,772✔
1512
    SMetaTableOp *op = &ops[i];
5,419✔
1513

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

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

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

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

1533
  SMetaTableOp ops[] = {
232✔
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,160✔
1540
    SMetaTableOp *op = &ops[i];
928✔
1541
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
928✔
1542
    if (code) {
928!
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;
232✔
1553
}
1554

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

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

1562
  SMetaTableOp ops[] = {
698✔
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,188✔
1571
    SMetaTableOp *op = &ops[i];
3,490✔
1572
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
3,490✔
1573
    if (code) {
3,490!
1574
      metaErr(TD_VID(pMeta->pVnode), code);
×
1575
      return code;
×
1576
    }
1577
  }
1578

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

1583
  if (metaTbGroupCacheClear(pMeta, pSuperEntry->uid) < 0) {
698!
1584
    metaErr(TD_VID(pMeta->pVnode), code);
×
1585
  }
1586
  return code;
698✔
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) {
6,558✔
1595
  int32_t code = TSDB_CODE_SUCCESS;
6,558✔
1596

1597
  const SMetaEntry *pEntry = pParam->pEntry;
6,558✔
1598
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
6,558✔
1599

1600
  SMetaTableOp ops[] = {
6,558✔
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++) {
26,236✔
1607
    SMetaTableOp *op = &ops[i];
19,684✔
1608
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
19,684✔
1609
    if (code) {
19,676!
1610
      metaErr(TD_VID(pMeta->pVnode), code);
×
1611
      return code;
×
1612
    }
1613
  }
1614

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

1620
  return code;
6,568✔
1621
}
1622

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

1626
  SMetaEntry *pOldEntry = NULL;
6,550✔
1627

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

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

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

1652
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
6,567✔
1653
    STsdb  *pTsdb = pMeta->pVnode->pTsdb;
128✔
1654
    SArray *uids = NULL; /*taosArrayInit(8, sizeof(int64_t));
128✔
1655
     if (uids == NULL) {
1656
       metaErr(TD_VID(pMeta->pVnode), code);
1657
       metaFetchEntryFree(&pOldEntry);
1658
       return terrno;
1659
       }*/
1660
    if (deltaCol == 1) {
128✔
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!
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) {
64!
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!
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);
128✔
1695

1696
    tsdbCacheInvalidateSchema(pTsdb, pEntry->uid, -1, pEntry->stbEntry.schemaRow.version);
128✔
1697
  }
1698
  if (updStat) {
6,567✔
1699
    int64_t ctbNum = 0;
2,944✔
1700
    int32_t ret = metaGetStbStats(pMeta->pVnode, pEntry->uid, &ctbNum, NULL);
2,944✔
1701
    if (ret < 0) {
2,946!
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,946✔
1706
    if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
2,946✔
1707
  }
1708
  metaFetchEntryFree(&pOldEntry);
6,569✔
1709
  return code;
6,560✔
1710
}
1711

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

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

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

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

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

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

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

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

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

1763
  // handle update
1764
  SMetaHandleParam param = {
232✔
1765
      .pEntry = pEntry,
1766
      .pOldEntry = pOldEntry,
1767
  };
1768
  metaWLock(pMeta);
232✔
1769
  code = metaHandleNormalTableUpdateImpl(pMeta, &param);
232✔
1770
  metaULock(pMeta);
232✔
1771
  if (code) {
232!
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) &&
232✔
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;
232✔
1807
  pMeta->pVnode->config.vndStats.numOfNTimeSeries += deltaCol;  
232✔
1808
  if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
232✔
1809
  metaFetchEntryFree(&pOldEntry);
232✔
1810
  return code;
232✔
1811
}
1812

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

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

1824
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childList);
1,355✔
1825
  if (code) {
1,355!
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) {
1,355!
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++) {
3,997✔
1838
    SMetaEntry childEntry = {
5,284✔
1839
        .version = pEntry->version,
2,642✔
1840
        .uid = *(tb_uid_t *)taosArrayGet(childList, i),
2,642✔
1841
        .type = -TSDB_CHILD_TABLE,
1842
    };
1843

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

1850
  // do drop super table
1851
  SMetaHandleParam param = {
1,354✔
1852
      .pEntry = pEntry,
1853
      .pOldEntry = pOldEntry,
1854
  };
1855
  metaWLock(pMeta);
1,354✔
1856
  code = metaHandleSuperTableDropImpl(pMeta, &param);
1,354✔
1857
  metaULock(pMeta);
1,355✔
1858
  if (code) {
1,355!
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);
1,355✔
1867

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

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

1880
  if (NULL == pMeta || NULL == pEntry) {
148,781!
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) {
149,120✔
1886
    bool isExist = false;
145,146✔
1887
    if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) {
145,146✔
1888
      isExist = true;
7,496✔
1889
    }
1890

1891
    switch (type) {
145,152!
1892
      case TSDB_SUPER_TABLE: {
27,792✔
1893
        if (isExist) {
27,792✔
1894
          code = metaHandleSuperTableUpdate(pMeta, pEntry);
6,566✔
1895
        } else {
1896
          code = metaHandleSuperTableCreate(pMeta, pEntry);
21,226✔
1897
        }
1898
        break;
27,793✔
1899
      }
1900
      case TSDB_CHILD_TABLE: {
103,946✔
1901
        if (isExist) {
103,946✔
1902
          code = metaHandleChildTableUpdate(pMeta, pEntry);
698✔
1903
        } else {
1904
          code = metaHandleChildTableCreate(pMeta, pEntry);
103,248✔
1905
        }
1906
        break;
103,946✔
1907
      }
1908
      case TSDB_NORMAL_TABLE: {
13,421✔
1909
        if (isExist) {
13,421✔
1910
          code = metaHandleNormalTableUpdate(pMeta, pEntry);
232✔
1911
        } else {
1912
          code = metaHandleNormalTableCreate(pMeta, pEntry);
13,189✔
1913
        }
1914
        break;
13,422✔
1915
      }
UNCOV
1916
      default: {
×
1917
        code = TSDB_CODE_INVALID_PARA;
×
1918
        break;
×
1919
      }
1920
    }
1921
  } else {
1922
    switch (type) {
3,974!
1923
      case TSDB_SUPER_TABLE: {
1,354✔
1924
        code = metaHandleSuperTableDrop(pMeta, pEntry);
1,354✔
1925
        break;
1,355✔
1926
      }
1927
      case TSDB_CHILD_TABLE: {
838✔
1928
        code = metaHandleChildTableDrop(pMeta, pEntry, false);
838✔
1929
        break;
838✔
1930
      }
1931
      case TSDB_NORMAL_TABLE: {
1,783✔
1932
        code = metaHandleNormalTableDrop(pMeta, pEntry);
1,783✔
1933
        break;
1,783✔
1934
      }
UNCOV
1935
      default: {
×
1936
        code = TSDB_CODE_INVALID_PARA;
×
1937
        break;
×
1938
      }
1939
    }
1940
  }
1941

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

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