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

taosdata / TDengine / #3625

26 Feb 2025 10:19AM UTC coverage: 63.633% (+0.1%) from 63.485%
#3625

push

travis-ci

web-flow
Merge pull request #29914 from taosdata/feat/TS-5613-3.0

feat:[TS-5613]support bool in cast

148738 of 299799 branches covered (49.61%)

Branch coverage included in aggregate %.

233124 of 300297 relevant lines covered (77.63%)

17654074.26 hits per line

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

61.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) {
180,624✔
67
  int32_t code = TSDB_CODE_SUCCESS;
180,624✔
68
  void   *value = NULL;
180,624✔
69
  int32_t valueSize = 0;
180,624✔
70

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

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

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

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

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

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

141
void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); }
180,728✔
142

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

281
  if (pOldColumn && pNewColumn) {
138,534✔
282
    if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
136,615✔
283
      return TSDB_CODE_SUCCESS;
3,313✔
284
    } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) {
133,302!
285
      action = DROP_INDEX;
2,188✔
286
    } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
131,114!
287
      action = ADD_INDEX;
967✔
288
    } else {
289
      return TSDB_CODE_SUCCESS;
130,147✔
290
    }
291
  } else if (pOldColumn) {
1,919✔
292
    if (IS_IDX_ON(pOldColumn)) {
832✔
293
      action = DROP_INDEX;
17✔
294
    } else {
295
      return TSDB_CODE_SUCCESS;
815✔
296
    }
297
  } else {
298
    if (IS_IDX_ON(pNewColumn)) {
1,087!
299
      action = ADD_INDEX;
×
300
    } else {
301
      return TSDB_CODE_SUCCESS;
1,087✔
302
    }
303
  }
304

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

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

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

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

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

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

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

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

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

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

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

385
    if (pOldColumn->colId == pNewColumn->colId) {
137,261✔
386
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
136,616✔
387
      if (code) {
136,615✔
388
        metaErr(TD_VID(pMeta->pVnode), code);
4!
389
        return code;
×
390
      }
391

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

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

409
      iNew++;
×
410
    }
411
  }
412

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

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

431
  return code;
6,489✔
432
}
433

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

437
  const SMetaEntry *pEntry = pParam->pEntry;
49,883✔
438
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
49,883✔
439

440
  if (NULL == pOldEntry) {
49,883✔
441
    return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
40,106✔
442
  }
443

444
  if (pEntry->type == TSDB_NORMAL_TABLE) {
9,777✔
445
    // check row schema
446
    if (pOldEntry->ntbEntry.schemaRow.version != pEntry->ntbEntry.schemaRow.version) {
233✔
447
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
190✔
448
    }
449
  } else if (pEntry->type == TSDB_SUPER_TABLE) {
9,544!
450
    // check row schema
451
    if (pOldEntry->stbEntry.schemaRow.version != pEntry->stbEntry.schemaRow.version) {
9,628✔
452
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
3,131✔
453
    }
454

455
    // check tag schema
456
    code = metaUpdateSuperTableTagSchema(pMeta, pParam);
6,497✔
457
    if (code) {
6,494!
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;
6,537✔
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) {
191,340✔
476
  pInfo->uid = pEntry->uid;
191,340✔
477
  pInfo->version = pEntry->version;
191,340✔
478
  if (pEntry->type == TSDB_SUPER_TABLE) {
191,340✔
479
    pInfo->suid = pEntry->uid;
35,719✔
480
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
35,719✔
481
  } else if (pEntry->type == TSDB_CHILD_TABLE) {
155,621✔
482
    pInfo->suid = pEntry->ctbEntry.suid;
141,475✔
483
    pInfo->skmVer = 0;
141,475✔
484
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
14,146!
485
    pInfo->suid = 0;
14,260✔
486
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
14,260✔
487
  }
488
}
191,340✔
489

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

494
  const SMetaEntry *pEntry = pParam->pEntry;
191,344✔
495

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

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

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

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

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

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

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

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

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

546
  const SMetaEntry *pEntry = pParam->pEntry;
180,869✔
547

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

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

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

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

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

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

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

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

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

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

605
  const SMetaEntry *pEntry = pParam->pEntry;
141,271✔
606

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

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

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

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

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

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

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

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

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

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

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

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

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

691
static void metaFetchTagIdxKeyFree(STagIdxKey **ppTagIdxKey) {
150,650✔
692
  metaDestroyTagIdxKey(*ppTagIdxKey);
150,650✔
693
  *ppTagIdxKey = NULL;
150,653✔
694
}
150,653✔
695

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

699
  const SMetaEntry *pEntry = pParam->pEntry;
140,803✔
700
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
140,803✔
701

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

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

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

722
      if (!IS_IDX_ON(pTagColumn)) {
447,853✔
723
        continue;
307,262✔
724
      }
725

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

869
  const SMetaEntry *pEntry;
870
  if (META_TABLE_OP_DELETE == op) {
160,743✔
871
    pEntry = pParam->pOldEntry;
5,947✔
872
  } else {
873
    pEntry = pParam->pEntry;
154,796✔
874
  }
875

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

880
  if (TSDB_CHILD_TABLE == pEntry->type) {
160,743✔
881
    key.btime = pEntry->ctbEntry.btime;
144,970✔
882
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
15,773!
883
    key.btime = pEntry->ntbEntry.btime;
15,793✔
884
  } else {
885
    return TSDB_CODE_INVALID_PARA;
×
886
  }
887

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

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

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

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

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

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

933
  int32_t ret = ttlMgrInsertTtl(pMeta->pTtlMgr, &ctx);
154,842✔
934
  if (ret < 0) {
154,832!
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;
154,834✔
938
}
939

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

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

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

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

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

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

965
  return TSDB_CODE_SUCCESS;
930✔
966
}
967

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

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

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

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

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

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

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

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

1096
  metaWLock(pMeta);
25,670✔
1097
  code = metaHandleSuperTableCreateImpl(pMeta, pEntry);
26,134✔
1098
  metaULock(pMeta);
26,085✔
1099

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

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

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

1114
  SMetaTableOp ops[] = {
14,026✔
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++) {
98,173✔
1124
    SMetaTableOp *op = &ops[i];
84,147✔
1125

1126
    SMetaHandleParam param = {
84,147✔
1127
        .pEntry = pEntry,
1128
    };
1129

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

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

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

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

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

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

1168
  SMetaTableOp ops[] = {
140,791✔
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,125,524✔
1179
    SMetaTableOp *op = &ops[i];
984,952✔
1180

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

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

1193
  if (TSDB_CODE_SUCCESS == code) {
140,572!
1194
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0);
140,788✔
1195
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
140,778✔
1196
    if (ret < 0) {
140,825!
1197
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1198
    }
1199

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

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

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

1219
  // update TDB
1220
  metaWLock(pMeta);
140,808✔
1221
  code = metaHandleChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
140,825✔
1222
  metaULock(pMeta);
140,791✔
1223

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

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

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

1245
  } else {
1246
    metaErr(TD_VID(pMeta->pVnode), code);
×
1247
  }
1248
  metaTimeSeriesNotifyCheck(pMeta);
140,793✔
1249
  metaFetchEntryFree(&pSuperEntry);
140,822✔
1250
  return code;
140,840✔
1251
}
1252

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

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

1275
  return code;
1,768✔
1276
}
1277

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

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

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

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

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

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

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

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

1335
  SMetaTableOp ops[] = {
4,178✔
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,399✔
1346
    SMetaTableOp *op = &ops[i];
29,229✔
1347

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1478
    if (((SCtbIdxKey *)key)->suid < suid) {
13,156✔
1479
      continue;
2,396✔
1480
    } else if (((SCtbIdxKey *)key)->suid > suid) {
10,760✔
1481
      break;
2,204✔
1482
    }
1483

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

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

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

1502
  SMetaTableOp ops[] = {
2,341✔
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,703✔
1512
    SMetaTableOp *op = &ops[i];
9,363✔
1513

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

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

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

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

1533
  SMetaTableOp ops[] = {
233✔
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,165✔
1540
    SMetaTableOp *op = &ops[i];
932✔
1541
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
932✔
1542
    if (code) {
932!
1543
      metaErr(TD_VID(pMeta->pVnode), code);
×
1544
      return code;
×
1545
    }
1546
  }
1547
#if 0
1548
  if (metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs) < 0) {
1549
    metaError("vgId:%d, failed to update change time:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
1550
  }
1551
#endif
1552
  return code;
233✔
1553
}
1554

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

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

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

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

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

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

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

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

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

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

1619
  return code;
9,630✔
1620
}
1621

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

1625
  SMetaEntry *pOldEntry = NULL;
9,618✔
1626

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

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

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

1651
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
9,628✔
1652
    STsdb  *pTsdb = pMeta->pVnode->pTsdb;
2,016✔
1653
    SArray *uids = NULL; /*taosArrayInit(8, sizeof(int64_t));
2,016✔
1654
     if (uids == NULL) {
1655
       metaErr(TD_VID(pMeta->pVnode), code);
1656
       metaFetchEntryFree(&pOldEntry);
1657
       return terrno;
1658
       }*/
1659
    if (deltaCol == 1) {
2,016✔
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,952✔
1671
      int16_t cid = -1;
63✔
1672
      bool    hasPrimaryKey = false;
63✔
1673
      if (onCols >= 2) {
63!
1674
        hasPrimaryKey = (pOldEntry->stbEntry.schemaRow.pSchema[1].flags & COL_IS_KEY) ? true : false;
63✔
1675
      }
1676
      for (int i = 0, j = 0; i < nCols && j < onCols; ++i, ++j) {
413!
1677
        if (pEntry->stbEntry.schemaRow.pSchema[i].colId != pOldEntry->stbEntry.schemaRow.pSchema[j].colId) {
382✔
1678
          cid = pOldEntry->stbEntry.schemaRow.pSchema[j].colId;
32✔
1679
          break;
32✔
1680
        }
1681
      }
1682

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

1695
    tsdbCacheInvalidateSchema(pTsdb, pEntry->uid, -1, pEntry->stbEntry.schemaRow.version);
2,016✔
1696
  }
1697
  if (updStat) {
9,627✔
1698
    int64_t ctbNum = 0;
2,932✔
1699
    int32_t ret = metaGetStbStats(pMeta->pVnode, pEntry->uid, &ctbNum, NULL);
2,932✔
1700
    if (ret < 0) {
2,935!
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);
2,933✔
1705
    if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
2,933✔
1706
  }
1707
  metaFetchEntryFree(&pOldEntry);
9,628✔
1708
  return code;
9,629✔
1709
}
1710

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

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

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

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

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

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

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

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

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

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

1776
  // do other stuff
1777
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) &&
233✔
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;
233✔
1806
  pMeta->pVnode->config.vndStats.numOfNTimeSeries += deltaCol;  
233✔
1807
  if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
233✔
1808
  metaFetchEntryFree(&pOldEntry);
233✔
1809
  return code;
233✔
1810
}
1811

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

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

1823
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childList);
2,323✔
1824
  if (code) {
2,339!
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,339!
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,732✔
1837
    SMetaEntry childEntry = {
6,780✔
1838
        .version = pEntry->version,
3,390✔
1839
        .uid = *(tb_uid_t *)taosArrayGet(childList, i),
3,390✔
1840
        .type = -TSDB_CHILD_TABLE,
1841
    };
1842

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

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

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

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

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

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

1884
  if (pEntry->type > 0) {
196,396✔
1885
    bool isExist = false;
191,526✔
1886
    if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) {
191,526✔
1887
      isExist = true;
10,558✔
1888
    }
1889

1890
    switch (type) {
191,558!
1891
      case TSDB_SUPER_TABLE: {
35,781✔
1892
        if (isExist) {
35,781✔
1893
          code = metaHandleSuperTableUpdate(pMeta, pEntry);
9,630✔
1894
        } else {
1895
          code = metaHandleSuperTableCreate(pMeta, pEntry);
26,151✔
1896
        }
1897
        break;
35,803✔
1898
      }
1899
      case TSDB_CHILD_TABLE: {
141,519✔
1900
        if (isExist) {
141,519✔
1901
          code = metaHandleChildTableUpdate(pMeta, pEntry);
697✔
1902
        } else {
1903
          code = metaHandleChildTableCreate(pMeta, pEntry);
140,822✔
1904
        }
1905
        break;
141,530✔
1906
      }
1907
      case TSDB_NORMAL_TABLE: {
14,258✔
1908
        if (isExist) {
14,258✔
1909
          code = metaHandleNormalTableUpdate(pMeta, pEntry);
233✔
1910
        } else {
1911
          code = metaHandleNormalTableCreate(pMeta, pEntry);
14,025✔
1912
        }
1913
        break;
14,259✔
1914
      }
1915
      default: {
×
1916
        code = TSDB_CODE_INVALID_PARA;
×
1917
        break;
×
1918
      }
1919
    }
1920
  } else {
1921
    switch (type) {
4,870✔
1922
      case TSDB_SUPER_TABLE: {
2,300✔
1923
        code = metaHandleSuperTableDrop(pMeta, pEntry);
2,300✔
1924
        break;
2,341✔
1925
      }
1926
      case TSDB_CHILD_TABLE: {
788✔
1927
        code = metaHandleChildTableDrop(pMeta, pEntry, false);
788✔
1928
        break;
788✔
1929
      }
1930
      case TSDB_NORMAL_TABLE: {
1,768✔
1931
        code = metaHandleNormalTableDrop(pMeta, pEntry);
1,768✔
1932
        break;
1,768✔
1933
      }
1934
      default: {
14✔
1935
        code = TSDB_CODE_INVALID_PARA;
14✔
1936
        break;
14✔
1937
      }
1938
    }
1939
  }
1940

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

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