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

taosdata / TDengine / #3620

21 Feb 2025 09:00AM UTC coverage: 63.573% (+0.2%) from 63.423%
#3620

push

travis-ci

web-flow
ci: taosBenchmark add coverage cases branch 3.0 (#29788)

* fix: add unit test for taos-tools

* fix: only .cpp include

* fix: remove no use function

* fix: restore toolsSys.c

* fix: add toolsSys case

* fix: rebuild error fixed

* fix: fix build error

* fix: support get vgroups with core and memory limit

* fix: build error for strcasecmp

* fix: add insertBasic.py case

* fix: add command line set vgroups=3

* fix: change with ns database

* toolscJson read with int replace float and add insertPrecison.py

* fix: add insertBindVGroup.json case

* fix: remove public fun removeQuotation

* fix: vgroups change method

* fix: memory leak for runInsertLimitThread slot

* insertPrecision.py word write wrong

* fix: check isFloat number

* fix: vgroups change logic error

* fix: insertBasic.py real and expect error

* fix: adjust default vgroups

* fix: adjust default vgroups modify comment

148962 of 300203 branches covered (49.62%)

Branch coverage included in aggregate %.

15 of 16 new or added lines in 1 file covered. (93.75%)

2018 existing lines in 133 files now uncovered.

233201 of 300933 relevant lines covered (77.49%)

18174406.98 hits per line

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

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

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

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

85
  code = tdbTbGet(pMeta->pTbDb, &key, sizeof(key), &value, &valueSize);
185,075✔
86
  if (TSDB_CODE_SUCCESS != code) {
185,081!
87
    metaError("vgId:%d, failed to get entry by uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), uid, tstrerror(code));
×
UNCOV
88
    code = TSDB_CODE_INTERNAL_ERROR;
×
UNCOV
89
    return code;
×
90
  }
91

92
  // decode entry
93
  SDecoder   decoder = {0};
185,081✔
94
  SMetaEntry entry = {0};
185,081✔
95

96
  tDecoderInit(&decoder, value, valueSize);
185,081✔
97
  code = metaDecodeEntry(&decoder, &entry);
185,059✔
98
  if (code) {
184,975!
99
    metaError("vgId:%d, failed to decode entry by uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), uid,
×
100
              tstrerror(code));
101
    tDecoderClear(&decoder);
×
UNCOV
102
    tdbFreeClear(value);
×
UNCOV
103
    return code;
×
104
  }
105

106
  code = metaCloneEntry(&entry, ppEntry);
184,975✔
107
  if (code) {
185,040!
108
    metaError("vgId:%d, failed to clone entry by uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), uid,
×
109
              tstrerror(code));
110
    tDecoderClear(&decoder);
×
UNCOV
111
    tdbFreeClear(value);
×
UNCOV
112
    return code;
×
113
  }
114

115
  tdbFreeClear(value);
185,040✔
116
  tDecoderClear(&decoder);
185,034✔
117
  return code;
185,083✔
118
}
119

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

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

133
  code = metaFetchEntryByUid(pMeta, uid, ppEntry);
9,399✔
134
  if (TSDB_CODE_SUCCESS != code) {
9,397!
UNCOV
135
    metaError("vgId:%d, failed to get entry by uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), uid, tstrerror(code));
×
UNCOV
136
    code = TSDB_CODE_INTERNAL_ERROR;
×
137
  }
138
  return code;
9,397✔
139
}
140

141
void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); }
185,054✔
142

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

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

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

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

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

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

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

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

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

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

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

231
  // encode schema
232
  tEncodeSize(tEncodeSSchemaWrapper, pSchema, valueSize, code);
94,745✔
233
  if (TSDB_CODE_SUCCESS != code) {
47,259!
UNCOV
234
    metaErr(vgId, code);
×
UNCOV
235
    return code;
×
236
  }
237

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

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

254
  // put to tdb
255
  if (META_TABLE_OP_INSERT == op) {
47,335!
UNCOV
256
    code = tdbTbInsert(pMeta->pSkmDb, &key, sizeof(key), value, valueSize, pMeta->txn);
×
257
  } else if (META_TABLE_OP_UPDATA == op) {
47,337!
258
    code = tdbTbUpsert(pMeta->pSkmDb, &key, sizeof(key), value, valueSize, pMeta->txn);
47,337✔
259
  } else {
UNCOV
260
    code = TSDB_CODE_INVALID_PARA;
×
261
  }
262
  if (TSDB_CODE_SUCCESS != code) {
47,426!
UNCOV
263
    metaErr(vgId, code);
×
264
  }
265
  taosMemoryFree(value);
47,413!
266
  return code;
47,402✔
267
}
268

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

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

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

281
  if (pOldColumn && pNewColumn) {
137,874✔
282
    if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
135,974✔
283
      return TSDB_CODE_SUCCESS;
3,254✔
284
    } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) {
132,720!
285
      action = DROP_INDEX;
2,216✔
286
    } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
130,504!
287
      action = ADD_INDEX;
965✔
288
    } else {
289
      return TSDB_CODE_SUCCESS;
129,539✔
290
    }
291
  } else if (pOldColumn) {
1,900✔
292
    if (IS_IDX_ON(pOldColumn)) {
779✔
293
      action = DROP_INDEX;
17✔
294
    } else {
295
      return TSDB_CODE_SUCCESS;
762✔
296
    }
297
  } else {
298
    if (IS_IDX_ON(pNewColumn)) {
1,121!
UNCOV
299
      action = ADD_INDEX;
×
300
    } else {
301
      return TSDB_CODE_SUCCESS;
1,121✔
302
    }
303
  }
304

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

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

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

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

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

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

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

365
    metaFetchTagIdxKeyFree(&pTagIdxKey);
4,979✔
366
    metaFetchEntryFree(&pChildEntry);
4,979✔
367
  }
368

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

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

380
  int32_t iOld = 0, iNew = 0;
6,461✔
381
  for (; iOld < pOldTagSchema->nCols && iNew < pNewTagSchema->nCols;) {
143,035✔
382
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
136,573✔
383
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
136,573✔
384

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

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

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

UNCOV
409
      iNew++;
×
410
    }
411
  }
412

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

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

431
  return code;
6,462✔
432
}
433

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

437
  const SMetaEntry *pEntry = pParam->pEntry;
53,815✔
438
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
53,815✔
439

440
  if (NULL == pOldEntry) {
53,815✔
441
    return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
43,093✔
442
  }
443

444
  if (pEntry->type == TSDB_NORMAL_TABLE) {
10,722✔
445
    // check row schema
446
    if (pOldEntry->ntbEntry.schemaRow.version != pEntry->ntbEntry.schemaRow.version) {
236✔
447
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
191✔
448
    }
449
  } else if (pEntry->type == TSDB_SUPER_TABLE) {
10,486!
450
    // check row schema
451
    if (pOldEntry->stbEntry.schemaRow.version != pEntry->stbEntry.schemaRow.version) {
10,565✔
452
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
4,112✔
453
    }
454

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

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

466
  return TSDB_CODE_SUCCESS;
6,502✔
467
}
468

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

474
// Uid Index
475
static void metaBuildEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) {
197,543✔
476
  pInfo->uid = pEntry->uid;
197,543✔
477
  pInfo->version = pEntry->version;
197,543✔
478
  if (pEntry->type == TSDB_SUPER_TABLE) {
197,543✔
479
    pInfo->suid = pEntry->uid;
39,167✔
480
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
39,167✔
481
  } else if (pEntry->type == TSDB_CHILD_TABLE) {
158,376✔
482
    pInfo->suid = pEntry->ctbEntry.suid;
143,785✔
483
    pInfo->skmVer = 0;
143,785✔
484
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
14,591!
485
    pInfo->suid = 0;
14,697✔
486
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
14,697✔
487
  }
488
}
197,543✔
489

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

494
  const SMetaEntry *pEntry = pParam->pEntry;
197,564✔
495

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

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

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

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

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

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

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

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

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

546
  const SMetaEntry *pEntry = pParam->pEntry;
186,085✔
547

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

691
static void metaFetchTagIdxKeyFree(STagIdxKey **ppTagIdxKey) {
153,011✔
692
  metaDestroyTagIdxKey(*ppTagIdxKey);
153,011✔
693
  *ppTagIdxKey = NULL;
153,014✔
694
}
153,014✔
695

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

699
  const SMetaEntry *pEntry = pParam->pEntry;
143,078✔
700
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
143,078✔
701

702
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
143,078✔
703
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
143,265✔
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!
UNCOV
714
      metaErr(TD_VID(pMeta->pVnode), code);
×
715
    }
716
  } else {
717
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
626,171✔
718
      STagIdxKey    *pTagIdxKey = NULL;
483,253✔
719
      int32_t        nTagIdxKey;
720
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
483,253✔
721

722
      if (!IS_IDX_ON(pTagColumn)) {
483,253✔
723
        continue;
340,403✔
724
      }
725

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

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

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

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

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

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

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

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

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

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

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

796
      if (tagIdxKeyCmpr(pOldTagIdxKey, oldTagIdxKeySize, pNewTagIdxKey, newTagIdxKeySize)) {
466✔
797
        code = tdbTbDelete(pMeta->pTagIdx, pOldTagIdxKey, oldTagIdxKeySize, pMeta->txn);
97✔
798
        if (code) {
97!
799
          metaErr(TD_VID(pMeta->pVnode), code);
×
800
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
UNCOV
801
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
UNCOV
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);
×
UNCOV
809
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
UNCOV
810
          return code;
×
811
        }
812
      }
813

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

965
  return TSDB_CODE_SUCCESS;
936✔
966
}
967

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

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

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

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

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

1070
  SMetaTableOp ops[] = {
28,634✔
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++) {
171,547✔
1079
    SMetaTableOp          *op = &ops[i];
142,994✔
1080
    const SMetaHandleParam param = {
142,994✔
1081
        .pEntry = pEntry,
1082
    };
1083

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

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

1096
  metaWLock(pMeta);
28,236✔
1097
  code = metaHandleSuperTableCreateImpl(pMeta, pEntry);
28,700✔
1098
  metaULock(pMeta);
28,612✔
1099

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

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

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

1114
  SMetaTableOp ops[] = {
14,462✔
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++) {
101,199✔
1124
    SMetaTableOp *op = &ops[i];
86,743✔
1125

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

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

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

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

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

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

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

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

1178
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1,143,801✔
1179
    SMetaTableOp *op = &ops[i];
1,000,936✔
1180

1181
    SMetaHandleParam param = {
1,000,936✔
1182
        .pEntry = pEntry,
1183
        .pSuperEntry = pSuperEntry,
1184
    };
1185

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

1193
  if (TSDB_CODE_SUCCESS == code) {
142,865!
1194
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0);
143,072✔
1195
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
143,079✔
1196
    if (ret < 0) {
143,111!
UNCOV
1197
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1198
    }
1199

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

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

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

1219
  // update TDB
1220
  metaWLock(pMeta);
143,102✔
1221
  code = metaHandleChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
143,123✔
1222
  metaULock(pMeta);
143,084✔
1223

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

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

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

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

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

1275
  return code;
1,779✔
1276
}
1277

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

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

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

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

1304
  // update other stuff
1305
  pMeta->pVnode->config.vndStats.numOfNTables--;
1,779✔
1306
  pMeta->pVnode->config.vndStats.numOfNTimeSeries -= (pOldEntry->ntbEntry.schemaRow.nCols - 1);
1,779✔
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,779!
UNCOV
1318
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pOldEntry->uid, 0, NULL);
×
UNCOV
1319
    if (ret < 0) {
×
UNCOV
1320
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1321
    }
1322
  }
1323

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

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

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

1335
  SMetaTableOp ops[] = {
4,223✔
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++) {
33,746✔
1346
    SMetaTableOp *op = &ops[i];
29,532✔
1347

1348
    if (op->table == META_ENTRY_TABLE && superDropped) {
29,532✔
1349
      continue;
3,436✔
1350
    }
1351

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1478
    if (((SCtbIdxKey *)key)->suid < suid) {
12,939✔
1479
      continue;
622✔
1480
    } else if (((SCtbIdxKey *)key)->suid > suid) {
12,317✔
1481
      break;
3,682✔
1482
    }
1483

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

1493
  tdbTbcClose(cursor);
5,639✔
1494
  tdbFreeClear(key);
5,640✔
1495
  return code;
5,638✔
1496
}
1497

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

1502
  SMetaTableOp ops[] = {
2,344✔
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++) {
11,725✔
1512
    SMetaTableOp *op = &ops[i];
9,379✔
1513

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

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

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

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

1533
  SMetaTableOp ops[] = {
236✔
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,180✔
1540
    SMetaTableOp *op = &ops[i];
944✔
1541
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
944✔
1542
    if (code) {
944!
UNCOV
1543
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
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;
236✔
1553
}
1554

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

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

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

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

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

1597
  const SMetaEntry *pEntry = pParam->pEntry;
10,564✔
1598
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
10,564✔
1599

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

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

1619
  return code;
10,570✔
1620
}
1621

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

1625
  SMetaEntry *pOldEntry = NULL;
10,563✔
1626

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

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

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

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

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

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

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

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

1714
  SMetaEntry *pOldEntry = NULL;
700✔
1715
  SMetaEntry *pSuperEntry = NULL;
700✔
1716

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

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

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

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

1746
  metaFetchEntryFree(&pOldEntry);
700✔
1747
  metaFetchEntryFree(&pSuperEntry);
700✔
1748
  return code;
700✔
1749
}
1750

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

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

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

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

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

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

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

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

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

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

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

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

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

1864
  // do other stuff
1865
  metaUpdTimeSeriesNum(pMeta);
2,344✔
1866

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

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

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

1884
  if (pEntry->type > 0) {
202,640✔
1885
    bool isExist = false;
197,738✔
1886
    if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) {
197,738✔
1887
      isExist = true;
11,509✔
1888
    }
1889

1890
    switch (type) {
197,789✔
1891
      case TSDB_SUPER_TABLE: {
39,272✔
1892
        if (isExist) {
39,272✔
1893
          code = metaHandleSuperTableUpdate(pMeta, pEntry);
10,574✔
1894
        } else {
1895
          code = metaHandleSuperTableCreate(pMeta, pEntry);
28,698✔
1896
        }
1897
        break;
39,295✔
1898
      }
1899
      case TSDB_CHILD_TABLE: {
143,814✔
1900
        if (isExist) {
143,814✔
1901
          code = metaHandleChildTableUpdate(pMeta, pEntry);
700✔
1902
        } else {
1903
          code = metaHandleChildTableCreate(pMeta, pEntry);
143,114✔
1904
        }
1905
        break;
143,821✔
1906
      }
1907
      case TSDB_NORMAL_TABLE: {
14,698✔
1908
        if (isExist) {
14,698✔
1909
          code = metaHandleNormalTableUpdate(pMeta, pEntry);
236✔
1910
        } else {
1911
          code = metaHandleNormalTableCreate(pMeta, pEntry);
14,462✔
1912
        }
1913
        break;
14,697✔
1914
      }
1915
      default: {
5✔
1916
        code = TSDB_CODE_INVALID_PARA;
5✔
1917
        break;
5✔
1918
      }
1919
    }
1920
  } else {
1921
    switch (type) {
4,902!
1922
      case TSDB_SUPER_TABLE: {
2,343✔
1923
        code = metaHandleSuperTableDrop(pMeta, pEntry);
2,343✔
1924
        break;
2,345✔
1925
      }
1926
      case TSDB_CHILD_TABLE: {
788✔
1927
        code = metaHandleChildTableDrop(pMeta, pEntry, false);
788✔
1928
        break;
788✔
1929
      }
1930
      case TSDB_NORMAL_TABLE: {
1,779✔
1931
        code = metaHandleNormalTableDrop(pMeta, pEntry);
1,779✔
1932
        break;
1,779✔
1933
      }
UNCOV
1934
      default: {
×
UNCOV
1935
        code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1936
        break;
×
1937
      }
1938
    }
1939
  }
1940

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

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