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

taosdata / TDengine / #3653

14 Mar 2025 08:10AM UTC coverage: 22.565% (-41.0%) from 63.596%
#3653

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.

49248 of 302527 branches covered (16.28%)

Branch coverage included in aggregate %.

53 of 99 new or added lines in 12 files covered. (53.54%)

155872 existing lines in 443 files now uncovered.

87359 of 302857 relevant lines covered (28.84%)

570004.22 hits per line

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

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

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

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

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

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

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

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

141
void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); }
36✔
142

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

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

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

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

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

180
  // put to tdb
181
  if (META_TABLE_OP_INSERT == op) {
38✔
182
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
18✔
183
  } else if (META_TABLE_OP_UPDATA == op) {
20✔
184
    code = tdbTbUpsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
16✔
185
  } else if (META_TABLE_OP_DELETE == op) {
4!
186
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
4✔
187
  } else {
188
    code = TSDB_CODE_INVALID_PARA;
×
189
  }
190
  if (TSDB_CODE_SUCCESS != code) {
38!
191
    metaErr(vgId, code);
×
192
  }
193
  taosMemoryFree(value);
38!
194
  return code;
38✔
195
}
196

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

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

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

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

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

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

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

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

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

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

281
  if (pOldColumn && pNewColumn) {
26✔
282
    if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
22!
283
      return TSDB_CODE_SUCCESS;
8✔
284
    } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) {
14!
UNCOV
285
      action = DROP_INDEX;
×
286
    } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
14!
UNCOV
287
      action = ADD_INDEX;
×
288
    } else {
289
      return TSDB_CODE_SUCCESS;
14✔
290
    }
291
  } else if (pOldColumn) {
4✔
292
    if (IS_IDX_ON(pOldColumn)) {
2!
UNCOV
293
      action = DROP_INDEX;
×
294
    } else {
295
      return TSDB_CODE_SUCCESS;
2✔
296
    }
297
  } else {
298
    if (IS_IDX_ON(pNewColumn)) {
2!
299
      action = ADD_INDEX;
×
300
    } else {
301
      return TSDB_CODE_SUCCESS;
2✔
302
    }
303
  }
304

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

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

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

UNCOV
326
    STagIdxKey *pTagIdxKey = NULL;
×
UNCOV
327
    int32_t     tagIdxKeySize = 0;
×
328

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

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

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

UNCOV
365
    metaFetchTagIdxKeyFree(&pTagIdxKey);
×
UNCOV
366
    metaFetchEntryFree(&pChildEntry);
×
367
  }
368

UNCOV
369
  taosArrayDestroy(childTables);
×
UNCOV
370
  return code;
×
371
}
372

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

380
  int32_t iOld = 0, iNew = 0;
8✔
381
  for (; iOld < pOldTagSchema->nCols && iNew < pNewTagSchema->nCols;) {
30✔
382
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
22✔
383
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
22✔
384

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

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

UNCOV
401
      iOld++;
×
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++) {
10✔
414
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
2✔
415
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
2✔
416
    if (code) {
2!
417
      metaErr(TD_VID(pMeta->pVnode), code);
×
418
      return code;
×
419
    }
420
  }
421

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

431
  return code;
8✔
432
}
433

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

437
  const SMetaEntry *pEntry = pParam->pEntry;
34✔
438
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
34✔
439

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

444
  if (pEntry->type == TSDB_NORMAL_TABLE) {
16!
445
    // check row schema
UNCOV
446
    if (pOldEntry->ntbEntry.schemaRow.version != pEntry->ntbEntry.schemaRow.version) {
×
UNCOV
447
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
×
448
    }
449
  } else if (pEntry->type == TSDB_SUPER_TABLE) {
16!
450
    // check row schema
451
    if (pOldEntry->stbEntry.schemaRow.version != pEntry->stbEntry.schemaRow.version) {
16✔
452
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
8✔
453
    }
454

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

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

466
  return TSDB_CODE_SUCCESS;
8✔
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) {
34✔
476
  pInfo->uid = pEntry->uid;
34✔
477
  pInfo->version = pEntry->version;
34✔
478
  if (pEntry->type == TSDB_SUPER_TABLE) {
34!
479
    pInfo->suid = pEntry->uid;
34✔
480
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
34✔
UNCOV
481
  } else if (pEntry->type == TSDB_CHILD_TABLE) {
×
UNCOV
482
    pInfo->suid = pEntry->ctbEntry.suid;
×
UNCOV
483
    pInfo->skmVer = 0;
×
UNCOV
484
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
×
UNCOV
485
    pInfo->suid = 0;
×
UNCOV
486
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
×
487
  }
488
}
34✔
489

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

494
  const SMetaEntry *pEntry = pParam->pEntry;
34✔
495

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

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

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

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

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

529
  const SMetaEntry *pEntry = pParam->pOldEntry;
4✔
530

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

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

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

546
  const SMetaEntry *pEntry = pParam->pEntry;
18✔
547

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

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

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

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

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

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

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

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

UNCOV
605
  const SMetaEntry *pEntry = pParam->pEntry;
×
606

UNCOV
607
  SCtbIdxKey key = {
×
UNCOV
608
      .suid = pEntry->ctbEntry.suid,
×
UNCOV
609
      .uid = pEntry->uid,
×
610
  };
611

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

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

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

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

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

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

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

UNCOV
656
  STagIdxKey *pTagIdxKey = NULL;
×
657
  int32_t     nTagIdxKey;
UNCOV
658
  const void *pTagData = NULL;
×
UNCOV
659
  int32_t     nTagData = 0;
×
660

UNCOV
661
  STagVal tagVal = {
×
UNCOV
662
      .cid = pTagColumn->colId,
×
663
  };
664

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

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

UNCOV
686
  *ppTagIdxKey = pTagIdxKey;
×
UNCOV
687
  *pTagIdxKeySize = nTagIdxKey;
×
UNCOV
688
  return code;
×
689
}
690

UNCOV
691
static void metaFetchTagIdxKeyFree(STagIdxKey **ppTagIdxKey) {
×
UNCOV
692
  metaDestroyTagIdxKey(*ppTagIdxKey);
×
UNCOV
693
  *ppTagIdxKey = NULL;
×
UNCOV
694
}
×
695

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

UNCOV
699
  const SMetaEntry *pEntry = pParam->pEntry;
×
UNCOV
700
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
×
701

UNCOV
702
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
×
UNCOV
703
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
×
UNCOV
704
    const SSchema *pTagColumn = &pTagSchema->pSchema[0];
×
705

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

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

UNCOV
722
      if (!IS_IDX_ON(pTagColumn)) {
×
UNCOV
723
        continue;
×
724
      }
725

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

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

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

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

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

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

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

UNCOV
774
      if (!IS_IDX_ON(pTagColumn)) {
×
UNCOV
775
        continue;
×
776
      }
777

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

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

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

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

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

UNCOV
814
      metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
UNCOV
815
      metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
816
    }
817
  }
UNCOV
818
  return code;
×
819
}
820

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

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

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

UNCOV
844
      STagIdxKey *pTagIdxKey = NULL;
×
845
      int32_t     nTagIdxKey;
846

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

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

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

869
  const SMetaEntry *pEntry;
UNCOV
870
  if (META_TABLE_OP_DELETE == op) {
×
UNCOV
871
    pEntry = pParam->pOldEntry;
×
872
  } else {
UNCOV
873
    pEntry = pParam->pEntry;
×
874
  }
875

UNCOV
876
  SBtimeIdxKey key = {
×
UNCOV
877
      .uid = pEntry->uid,
×
878
  };
879

UNCOV
880
  if (TSDB_CHILD_TABLE == pEntry->type) {
×
UNCOV
881
    key.btime = pEntry->ctbEntry.btime;
×
UNCOV
882
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
×
UNCOV
883
    key.btime = pEntry->ntbEntry.btime;
×
884
  } else {
885
    return TSDB_CODE_INVALID_PARA;
×
886
  }
887

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

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

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

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

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

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

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

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

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

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

UNCOV
949
  const SMetaEntry *pEntry = pParam->pEntry;
×
UNCOV
950
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
×
951

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

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

UNCOV
965
  return TSDB_CODE_SUCCESS;
×
966
}
967

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

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

UNCOV
977
  if (TSDB_CHILD_TABLE == pEntry->type) {
×
UNCOV
978
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
×
UNCOV
979
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
×
UNCOV
980
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
×
981
  } else {
982
    code = TSDB_CODE_INVALID_PARA;
×
983
  }
984

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

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

1070
  SMetaTableOp ops[] = {
18✔
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++) {
108✔
1079
    SMetaTableOp          *op = &ops[i];
90✔
1080
    const SMetaHandleParam param = {
90✔
1081
        .pEntry = pEntry,
1082
    };
1083

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

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

1096
  metaWLock(pMeta);
18✔
1097
  code = metaHandleSuperTableCreateImpl(pMeta, pEntry);
18✔
1098
  metaULock(pMeta);
18✔
1099

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

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

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

UNCOV
1114
  SMetaTableOp ops[] = {
×
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

UNCOV
1123
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
×
UNCOV
1124
    SMetaTableOp *op = &ops[i];
×
1125

UNCOV
1126
    SMetaHandleParam param = {
×
1127
        .pEntry = pEntry,
1128
    };
1129

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

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

1142
  // update TDB
UNCOV
1143
  metaWLock(pMeta);
×
UNCOV
1144
  code = metaHandleNormalTableCreateImpl(pMeta, pEntry);
×
UNCOV
1145
  metaULock(pMeta);
×
1146

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

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

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

UNCOV
1168
  SMetaTableOp ops[] = {
×
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

UNCOV
1178
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
×
UNCOV
1179
    SMetaTableOp *op = &ops[i];
×
1180

UNCOV
1181
    SMetaHandleParam param = {
×
1182
        .pEntry = pEntry,
1183
        .pSuperEntry = pSuperEntry,
1184
    };
1185

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

UNCOV
1193
  if (TSDB_CODE_SUCCESS == code) {
×
NEW
1194
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
×
UNCOV
1195
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
×
UNCOV
1196
    if (ret < 0) {
×
1197
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1198
    }
1199

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

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

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

1219
  // update TDB
UNCOV
1220
  metaWLock(pMeta);
×
UNCOV
1221
  code = metaHandleChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
×
UNCOV
1222
  metaULock(pMeta);
×
1223

1224
  // update other stuff
UNCOV
1225
  if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
1226
    pMeta->pVnode->config.vndStats.numOfCTables++;
×
1227

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

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

UNCOV
1253
static int32_t metaHandleNormalTableDropImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
×
UNCOV
1254
  int32_t code = TSDB_CODE_SUCCESS;
×
1255

UNCOV
1256
  SMetaTableOp ops[] = {
×
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

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

UNCOV
1275
  return code;
×
1276
}
1277

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

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

UNCOV
1289
  SMetaHandleParam param = {
×
1290
      .pEntry = pEntry,
1291
      .pOldEntry = pOldEntry,
1292
  };
1293

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

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

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

UNCOV
1317
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
×
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

UNCOV
1324
  metaFetchEntryFree(&pOldEntry);
×
UNCOV
1325
  return code;
×
1326
}
1327

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

UNCOV
1331
  const SMetaEntry *pEntry = pParam->pEntry;
×
UNCOV
1332
  const SMetaEntry *pChild = pParam->pOldEntry;
×
UNCOV
1333
  const SMetaEntry *pSuper = pParam->pSuperEntry;
×
1334

UNCOV
1335
  SMetaTableOp ops[] = {
×
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

UNCOV
1345
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
×
UNCOV
1346
    SMetaTableOp *op = &ops[i];
×
1347

UNCOV
1348
    if (op->table == META_ENTRY_TABLE && superDropped) {
×
UNCOV
1349
      continue;
×
1350
    }
1351

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

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

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

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

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

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

UNCOV
1393
  SMetaHandleParam param = {
×
1394
      .pEntry = pEntry,
1395
      .pOldEntry = pChild,
1396
      .pSuperEntry = pSuper,
1397
  };
1398

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

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

UNCOV
1419
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
×
UNCOV
1420
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pChild->uid, pSuper->uid, NULL);
×
UNCOV
1421
    if (ret < 0) {
×
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
UNCOV
1438
  metaFetchEntryFree(&pChild);
×
UNCOV
1439
  metaFetchEntryFree(&pSuper);
×
UNCOV
1440
  return code;
×
1441
}
1442

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

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

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

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

1473
  for (;;) {
1474
    if (tdbTbcNext(cursor, &key, &keySize, NULL, NULL) < 0) {
4!
1475
      break;
4✔
1476
    }
1477

UNCOV
1478
    if (((SCtbIdxKey *)key)->suid < suid) {
×
UNCOV
1479
      continue;
×
UNCOV
1480
    } else if (((SCtbIdxKey *)key)->suid > suid) {
×
UNCOV
1481
      break;
×
1482
    }
1483

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

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

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

1502
  SMetaTableOp ops[] = {
4✔
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++) {
20✔
1512
    SMetaTableOp *op = &ops[i];
16✔
1513

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

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

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

UNCOV
1531
  const SMetaEntry *pEntry = pParam->pEntry;
×
1532

UNCOV
1533
  SMetaTableOp ops[] = {
×
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
  };
UNCOV
1539
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
×
UNCOV
1540
    SMetaTableOp *op = &ops[i];
×
UNCOV
1541
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
×
UNCOV
1542
    if (code) {
×
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
UNCOV
1552
  return code;
×
1553
}
1554

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

UNCOV
1558
  const SMetaEntry *pEntry = pParam->pEntry;
×
UNCOV
1559
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
×
UNCOV
1560
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
×
1561

UNCOV
1562
  SMetaTableOp ops[] = {
×
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

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

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

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

1597
  const SMetaEntry *pEntry = pParam->pEntry;
16✔
1598
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
16✔
1599

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

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

1620
  return code;
16✔
1621
}
1622

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

1626
  SMetaEntry *pOldEntry = NULL;
16✔
1627

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

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

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

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

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

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

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

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

UNCOV
1715
  SMetaEntry *pOldEntry = NULL;
×
UNCOV
1716
  SMetaEntry *pSuperEntry = NULL;
×
1717

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

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

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

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

UNCOV
1747
  metaFetchEntryFree(&pOldEntry);
×
UNCOV
1748
  metaFetchEntryFree(&pSuperEntry);
×
UNCOV
1749
  return code;
×
1750
}
1751

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

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

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

1777
  // do other stuff
UNCOV
1778
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) &&
×
UNCOV
1779
      pEntry->ntbEntry.schemaRow.version != pOldEntry->ntbEntry.schemaRow.version) {
×
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
UNCOV
1804
    tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, pEntry->uid, pEntry->ntbEntry.schemaRow.version);
×
1805
  }
UNCOV
1806
  int32_t deltaCol = pEntry->ntbEntry.schemaRow.nCols - pOldEntry->ntbEntry.schemaRow.nCols;
×
UNCOV
1807
  pMeta->pVnode->config.vndStats.numOfNTimeSeries += deltaCol;  
×
UNCOV
1808
  if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
×
UNCOV
1809
  metaFetchEntryFree(&pOldEntry);
×
UNCOV
1810
  return code;
×
1811
}
1812

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

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

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

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

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

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

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

1880
  if (NULL == pMeta || NULL == pEntry) {
38!
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) {
38✔
1886
    bool isExist = false;
34✔
1887
    if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) {
34✔
1888
      isExist = true;
16✔
1889
    }
1890

1891
    switch (type) {
34!
1892
      case TSDB_SUPER_TABLE: {
34✔
1893
        if (isExist) {
34✔
1894
          code = metaHandleSuperTableUpdate(pMeta, pEntry);
16✔
1895
        } else {
1896
          code = metaHandleSuperTableCreate(pMeta, pEntry);
18✔
1897
        }
1898
        break;
34✔
1899
      }
UNCOV
1900
      case TSDB_CHILD_TABLE: {
×
UNCOV
1901
        if (isExist) {
×
UNCOV
1902
          code = metaHandleChildTableUpdate(pMeta, pEntry);
×
1903
        } else {
UNCOV
1904
          code = metaHandleChildTableCreate(pMeta, pEntry);
×
1905
        }
UNCOV
1906
        break;
×
1907
      }
UNCOV
1908
      case TSDB_NORMAL_TABLE: {
×
UNCOV
1909
        if (isExist) {
×
UNCOV
1910
          code = metaHandleNormalTableUpdate(pMeta, pEntry);
×
1911
        } else {
UNCOV
1912
          code = metaHandleNormalTableCreate(pMeta, pEntry);
×
1913
        }
UNCOV
1914
        break;
×
1915
      }
1916
      default: {
×
1917
        code = TSDB_CODE_INVALID_PARA;
×
1918
        break;
×
1919
      }
1920
    }
1921
  } else {
1922
    switch (type) {
4!
1923
      case TSDB_SUPER_TABLE: {
4✔
1924
        code = metaHandleSuperTableDrop(pMeta, pEntry);
4✔
1925
        break;
4✔
1926
      }
UNCOV
1927
      case TSDB_CHILD_TABLE: {
×
UNCOV
1928
        code = metaHandleChildTableDrop(pMeta, pEntry, false);
×
UNCOV
1929
        break;
×
1930
      }
UNCOV
1931
      case TSDB_NORMAL_TABLE: {
×
UNCOV
1932
        code = metaHandleNormalTableDrop(pMeta, pEntry);
×
UNCOV
1933
        break;
×
1934
      }
1935
      default: {
×
1936
        code = TSDB_CODE_INVALID_PARA;
×
1937
        break;
×
1938
      }
1939
    }
1940
  }
1941

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

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