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

taosdata / TDengine / #4924

09 Jan 2026 08:13AM UTC coverage: 65.354% (-0.02%) from 65.373%
#4924

push

travis-ci

web-flow
merge: from main to 3.0 branch #34232

33 of 56 new or added lines in 8 files covered. (58.93%)

3192 existing lines in 116 files now uncovered.

198218 of 303297 relevant lines covered (65.35%)

118995160.34 hits per line

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

76.37
/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
#include "vnodeInt.h"
13

14
extern SDmNotifyHandle dmNotifyHdl;
15

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

23
static void    metaTimeSeriesNotifyCheck(SMeta *pMeta);
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) {
165,153,141✔
67
  int32_t code = TSDB_CODE_SUCCESS;
165,153,141✔
68
  void   *value = NULL;
165,153,141✔
69
  int32_t valueSize = 0;
165,169,834✔
70

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

78
  // search entry table
79
  STbDbKey key = {
165,182,484✔
80
      .version = ((SUidIdxVal *)value)->version,
165,175,386✔
81
      .uid = uid,
82
  };
83
  tdbFreeClear(value);
165,181,134✔
84

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

96
  tDecoderInit(&decoder, value, valueSize);
165,180,457✔
97
  code = metaDecodeEntry(&decoder, &entry);
165,204,741✔
98
  if (code) {
165,159,767✔
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);
165,159,767✔
107
  if (code) {
165,181,523✔
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);
165,181,523✔
116
  tDecoderClear(&decoder);
165,178,667✔
117
  return code;
165,199,623✔
118
}
119

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

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

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

141
void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); }
165,247,778✔
142

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

147
  int32_t  code = TSDB_CODE_SUCCESS;
96,646,333✔
148
  int32_t  vgId = TD_VID(pMeta->pVnode);
96,646,333✔
149
  void    *value = NULL;
96,643,410✔
150
  int32_t  valueSize = 0;
96,643,410✔
151
  SEncoder encoder = {0};
96,643,410✔
152
  STbDbKey key = {
96,658,093✔
153
      .version = pEntry->version,
96,642,110✔
154
      .uid = pEntry->uid,
96,642,334✔
155
  };
156

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

164
  value = taosMemoryMalloc(valueSize);
96,558,707✔
165
  if (NULL == value) {
96,553,494✔
166
    metaErr(vgId, terrno);
×
167
    return terrno;
×
168
  }
169

170
  tEncoderInit(&encoder, value, valueSize);
96,553,494✔
171
  code = metaEncodeEntry(&encoder, pEntry);
96,626,551✔
172
  if (code) {
96,614,136✔
173
    metaErr(vgId, code);
×
174
    tEncoderClear(&encoder);
×
175
    taosMemoryFree(value);
×
176
    return code;
×
177
  }
178
  tEncoderClear(&encoder);
96,614,136✔
179

180
  // put to tdb
181
  if (META_TABLE_OP_INSERT == op) {
96,617,180✔
182
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
76,470,821✔
183
  } else if (META_TABLE_OP_UPDATA == op) {
20,146,359✔
184
    code = tdbTbUpsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
17,968,821✔
185
  } else if (META_TABLE_OP_DELETE == op) {
2,177,538✔
186
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
2,177,538✔
187
  } else {
188
    code = TSDB_CODE_INVALID_PARA;
×
189
  }
190
  if (TSDB_CODE_SUCCESS != code) {
96,620,196✔
191
    metaErr(vgId, code);
×
192
  }
193
  taosMemoryFree(value);
96,613,931✔
194
  return code;
96,614,717✔
195
}
196

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

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

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

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

217
  const SMetaEntry     *pEntry = pParam->pEntry;
27,560,345✔
218
  const SSchemaWrapper *pSchema = NULL;
27,533,638✔
219
  if (pEntry->type == TSDB_SUPER_TABLE) {
27,533,638✔
220
    pSchema = &pEntry->stbEntry.schemaRow;
10,656,195✔
221
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
16,906,647✔
222
    pSchema = &pEntry->ntbEntry.schemaRow;
16,906,647✔
223
  } else {
224
    return TSDB_CODE_INVALID_PARA;
×
225
  }
226
  SSkmDbKey key = {
27,582,207✔
227
      .uid = pEntry->uid,
27,562,827✔
228
      .sver = pSchema->version,
27,578,139✔
229
  };
230

231
  // encode schema
232
  tEncodeSize(tEncodeSSchemaWrapper, pSchema, valueSize, code);
55,156,842✔
233
  if (TSDB_CODE_SUCCESS != code) {
27,538,529✔
234
    metaErr(vgId, code);
×
235
    return code;
×
236
  }
237

238
  value = taosMemoryMalloc(valueSize);
27,538,529✔
239
  if (NULL == value) {
27,550,256✔
240
    metaErr(vgId, terrno);
×
241
    return terrno;
×
242
  }
243

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

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

277
  const SMetaEntry *pEntry = pParam->pEntry;
19,287,033✔
278
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
19,297,392✔
279
  enum { ADD_INDEX, DROP_INDEX } action;
280

281
  if (pOldColumn && pNewColumn) {
19,296,172✔
282
    if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
18,745,009✔
283
      return TSDB_CODE_SUCCESS;
940,164✔
284
    } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) {
17,800,600✔
285
      action = DROP_INDEX;
13,802✔
286
    } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
17,783,895✔
287
      action = ADD_INDEX;
4,249✔
288
    } else {
289
      return TSDB_CODE_SUCCESS;
17,778,709✔
290
    }
291
  } else if (pOldColumn) {
551,163✔
292
    if (IS_IDX_ON(pOldColumn)) {
199,245✔
293
      action = DROP_INDEX;
9,375✔
294
    } else {
295
      return TSDB_CODE_SUCCESS;
188,971✔
296
    }
297
  } else {
298
    if (IS_IDX_ON(pNewColumn)) {
351,918✔
299
      action = ADD_INDEX;
×
300
    } else {
301
      return TSDB_CODE_SUCCESS;
359,253✔
302
    }
303
  }
304

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

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

326
    STagIdxKey *pTagIdxKey = NULL;
155,680✔
327
    int32_t     tagIdxKeySize = 0;
155,680✔
328

329
    if (action == ADD_INDEX) {
155,680✔
330
      code = metaFetchTagIdxKey(pMeta, pChildEntry, pNewColumn, &pTagIdxKey, &tagIdxKeySize);
74,437✔
331
      if (code) {
74,437✔
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);
74,437✔
339
      if (code) {
74,437✔
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);
81,243✔
348
      if (code) {
81,243✔
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);
81,243✔
356
      if (code) {
81,243✔
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);
155,680✔
366
    metaFetchEntryFree(&pChildEntry);
155,680✔
367
  }
368

369
  taosArrayDestroy(childTables);
26,876✔
370
  return code;
27,426✔
371
}
372

373
static int32_t metaAddOrDropColumnIndexOfVirtualSuperTable(SMeta *pMeta, const SMetaHandleParam *pParam,
77,254,090✔
374
                                                           const SSchema *pOldColumn, const SSchema *pNewColumn) {
375
  int32_t code = TSDB_CODE_SUCCESS;
77,254,090✔
376

377
  const SMetaEntry *pEntry = pParam->pEntry;
77,254,090✔
378
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
77,254,701✔
379
  enum { ADD_COLUMN, DROP_COLUMN } action;
380

381
  if (pOldColumn && pNewColumn) {
77,254,701✔
382
    return TSDB_CODE_SUCCESS;
77,244,563✔
383
  } else if (pOldColumn) {
10,138✔
384
    action = DROP_COLUMN;
4,782✔
385
  } else {
386
    action = ADD_COLUMN;
5,356✔
387
  }
388

389
  // fetch all child tables
390
  SArray *childTables = 0;
10,138✔
391
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childTables);
10,138✔
392
  if (code) {
10,138✔
393
    metaErr(TD_VID(pMeta->pVnode), code);
×
394
    return code;
×
395
  }
396

397
  // do drop or add index
398
  for (int32_t i = 0; i < taosArrayGetSize(childTables); i++) {
18,943✔
399
    int64_t uid = *(int64_t *)taosArrayGet(childTables, i);
8,805✔
400

401
    // fetch child entry
402
    SMetaEntry *pChildEntry = NULL;
8,805✔
403
    code = metaFetchEntryByUid(pMeta, uid, &pChildEntry);
8,805✔
404
    if (code) {
8,805✔
405
      metaErr(TD_VID(pMeta->pVnode), code);
×
406
      taosArrayDestroy(childTables);
×
407
      return code;
×
408
    }
409

410
    SMetaHandleParam param = {.pEntry = pChildEntry};
8,805✔
411

412
    if (action == ADD_COLUMN) {
8,805✔
413
      code = updataTableColRef(&pChildEntry->colRef, pNewColumn, 1, NULL);
5,259✔
414
      if (code) {
5,259✔
415
        metaErr(TD_VID(pMeta->pVnode), code);
×
416
        taosArrayDestroy(childTables);
×
417
        metaFetchEntryFree(&pChildEntry);
×
418
        return code;
×
419
      }
420

421
      code = metaEntryTableUpdate(pMeta, &param);
5,259✔
422
      if (code) {
5,259✔
423
        metaErr(TD_VID(pMeta->pVnode), code);
×
424
        taosArrayDestroy(childTables);
×
425
        metaFetchEntryFree(&pChildEntry);
×
426
        return code;
×
427
      }
428
    } else {
429
      code = updataTableColRef(&pChildEntry->colRef, pOldColumn, 0, NULL);
3,546✔
430
      if (code) {
3,546✔
431
        metaErr(TD_VID(pMeta->pVnode), code);
×
432
        taosArrayDestroy(childTables);
×
433
        metaFetchEntryFree(&pChildEntry);
×
434
        return code;
×
435
      }
436

437
      code = metaEntryTableUpdate(pMeta, &param);
3,546✔
438
      if (code) {
3,546✔
439
        metaErr(TD_VID(pMeta->pVnode), code);
×
440
        taosArrayDestroy(childTables);
×
441
        metaFetchEntryFree(&pChildEntry);
×
442
        return code;
×
443
      }
444
    }
445
    metaFetchEntryFree(&pChildEntry);
8,805✔
446
  }
447

448
  taosArrayDestroy(childTables);
10,138✔
449
  return code;
10,138✔
450
}
451

452
static int32_t metaUpdateSuperTableTagSchema(SMeta *pMeta, const SMetaHandleParam *pParam) {
1,014,228✔
453
  int32_t               code = TSDB_CODE_SUCCESS;
1,014,228✔
454
  const SMetaEntry     *pEntry = pParam->pEntry;
1,014,228✔
455
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
1,016,973✔
456
  const SSchemaWrapper *pNewTagSchema = &pEntry->stbEntry.schemaTag;
1,014,834✔
457
  const SSchemaWrapper *pOldTagSchema = &pOldEntry->stbEntry.schemaTag;
1,013,877✔
458

459
  int32_t iOld = 0, iNew = 0;
1,012,054✔
460
  for (; iOld < pOldTagSchema->nCols && iNew < pNewTagSchema->nCols;) {
19,894,745✔
461
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
18,881,757✔
462
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
18,880,196✔
463

464
    if (pOldColumn->colId == pNewColumn->colId) {
18,882,578✔
465
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
18,737,697✔
466
      if (code) {
18,737,022✔
UNCOV
467
        metaErr(TD_VID(pMeta->pVnode), code);
×
468
        return code;
×
469
      }
470

471
      iOld++;
18,738,058✔
472
      iNew++;
18,738,058✔
473
    } else if (pOldColumn->colId < pNewColumn->colId) {
144,633✔
474
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
144,633✔
475
      if (code) {
144,021✔
476
        metaErr(TD_VID(pMeta->pVnode), code);
×
477
        return code;
×
478
      }
479

480
      // drop old tag from meta stable tag filter cache
481
      code = metaStableTagFilterCacheDropTag(pMeta, pEntry->uid, pOldColumn->colId);
144,021✔
482
      if (code) {
144,633✔
483
        metaErr(TD_VID(pMeta->pVnode), code);
×
484
        return code;
×
485
      }
486

487
      iOld++;
144,633✔
488
    } else {
489
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, NULL, pNewColumn);
×
490
      if (code) {
×
491
        metaErr(TD_VID(pMeta->pVnode), code);
×
492
        return code;
×
493
      }
494

495
      iNew++;
×
496
    }
497
  }
498

499
  for (; iOld < pOldTagSchema->nCols; iOld++) {
1,069,183✔
500
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
54,327✔
501
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
53,713✔
502
    if (code) {
54,937✔
503
      metaErr(TD_VID(pMeta->pVnode), code);
×
504
      return code;
×
505
    }
506
    // drop old tag from meta stable tag filter cache
507
    code = metaStableTagFilterCacheDropTag(pMeta, pEntry->uid, pOldColumn->colId);
54,937✔
508
    if (code) {
52,489✔
509
      metaErr(TD_VID(pMeta->pVnode), code);
×
510
      return code;
×
511
    }
512
  }
513

514
  for (; iNew < pNewTagSchema->nCols; iNew++) {
1,367,977✔
515
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
359,805✔
516
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, NULL, pNewColumn);
357,419✔
517
    if (code) {
354,666✔
518
      metaErr(TD_VID(pMeta->pVnode), code);
×
519
      return code;
×
520
    }
521
  }
522

523
  return code;
1,010,619✔
524
}
525

526
static int32_t metaUpdateSuperTableRowSchema(SMeta *pMeta, const SMetaHandleParam *pParam) {
13,170✔
527
  int32_t               code = TSDB_CODE_SUCCESS;
13,170✔
528
  const SMetaEntry     *pEntry = pParam->pEntry;
13,170✔
529
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
13,170✔
530
  const SSchemaWrapper *pNewRowSchema = &pEntry->stbEntry.schemaRow;
13,170✔
531
  const SSchemaWrapper *pOldRowSchema = &pOldEntry->stbEntry.schemaRow;
13,170✔
532

533
  int32_t iOld = 0, iNew = 0;
13,170✔
534
  for (; iOld < pOldRowSchema->nCols && iNew < pNewRowSchema->nCols;) {
77,260,117✔
535
    SSchema *pOldColumn = pOldRowSchema->pSchema + iOld;
77,246,947✔
536
    SSchema *pNewColumn = pNewRowSchema->pSchema + iNew;
77,246,366✔
537

538
    if (pOldColumn->colId == pNewColumn->colId) {
77,246,366✔
539
      code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
77,244,563✔
540
      if (code) {
77,244,563✔
541
        metaErr(TD_VID(pMeta->pVnode), code);
×
542
        return code;
×
543
      }
544

545
      iOld++;
77,244,563✔
546
      iNew++;
77,244,563✔
547
    } else if (pOldColumn->colId < pNewColumn->colId) {
1,803✔
548
      code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, pOldColumn, NULL);
2,384✔
549
      if (code) {
2,384✔
550
        metaErr(TD_VID(pMeta->pVnode), code);
×
551
        return code;
×
552
      }
553

554
      iOld++;
2,384✔
555
    } else {
556
      code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, NULL, pNewColumn);
×
557
      if (code) {
×
558
        metaErr(TD_VID(pMeta->pVnode), code);
×
559
        return code;
×
560
      }
561

562
      iNew++;
×
563
    }
564
  }
565

566
  for (; iOld < pOldRowSchema->nCols; iOld++) {
15,568✔
567
    SSchema *pOldColumn = pOldRowSchema->pSchema + iOld;
2,398✔
568
    code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, pOldColumn, NULL);
2,398✔
569
    if (code) {
2,398✔
570
      metaErr(TD_VID(pMeta->pVnode), code);
×
571
      return code;
×
572
    }
573
  }
574

575
  for (; iNew < pNewRowSchema->nCols; iNew++) {
18,526✔
576
    SSchema *pNewColumn = pNewRowSchema->pSchema + iNew;
5,356✔
577
    code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, NULL, pNewColumn);
4,745✔
578
    if (code) {
5,356✔
579
      metaErr(TD_VID(pMeta->pVnode), code);
×
580
      return code;
×
581
    }
582
  }
583

584
  return code;
13,170✔
585
}
586

587
static int32_t metaSchemaTableUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
28,591,567✔
588
  int32_t code = TSDB_CODE_SUCCESS;
28,591,567✔
589

590
  const SMetaEntry *pEntry = pParam->pEntry;
28,591,567✔
591
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
28,629,068✔
592

593
  if (NULL == pOldEntry) {
28,611,010✔
594
    return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
18,002,350✔
595
  }
596

597
  if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
10,608,660✔
598
    // check row schema
599
    if (pOldEntry->ntbEntry.schemaRow.version != pEntry->ntbEntry.schemaRow.version) {
3,528,152✔
600
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
3,492,295✔
601
    }
602
  } else if (pEntry->type == TSDB_SUPER_TABLE) {
7,088,092✔
603
    // check row schema
604
    if (pOldEntry->stbEntry.schemaRow.version != pEntry->stbEntry.schemaRow.version) {
7,115,607✔
605
      if (TABLE_IS_VIRTUAL(pEntry->flags)) {
6,094,037✔
606
        return metaUpdateSuperTableRowSchema(pMeta, pParam);
13,170✔
607
      } else {
608
        return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
6,061,790✔
609
      }
610
    }
611

612
    // check tag schema
613
    code = metaUpdateSuperTableTagSchema(pMeta, pParam);
1,012,389✔
614
    if (code) {
1,014,250✔
615
      metaErr(TD_VID(pMeta->pVnode), code);
2,610✔
616
      return code;
×
617
    }
618

619
  } else {
620
    return TSDB_CODE_INVALID_PARA;
×
621
  }
622

623
  return TSDB_CODE_SUCCESS;
1,030,051✔
624
}
625

626
static int32_t metaSchemaTableDelete(SMeta *pMeta, const SMetaHandleParam *pEntry) {
×
627
  // TODO
628
  return TSDB_CODE_SUCCESS;
×
629
}
630

631
// Uid Index
632
static void metaBuildEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) {
94,426,591✔
633
  pInfo->uid = pEntry->uid;
94,426,591✔
634
  pInfo->version = pEntry->version;
94,440,219✔
635
  if (pEntry->type == TSDB_SUPER_TABLE) {
94,472,398✔
636
    pInfo->suid = pEntry->uid;
11,672,218✔
637
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
11,689,352✔
638
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
82,682,897✔
639
    pInfo->suid = pEntry->ctbEntry.suid;
65,817,939✔
640
    pInfo->skmVer = 0;
65,804,865✔
641
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
16,925,058✔
642
    pInfo->suid = 0;
16,925,058✔
643
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
16,925,058✔
644
  }
645
}
94,398,832✔
646

647
static int32_t metaUidIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
94,423,721✔
648
  int32_t code = TSDB_CODE_SUCCESS;
94,423,721✔
649
  int32_t vgId = TD_VID(pMeta->pVnode);
94,423,721✔
650

651
  const SMetaEntry *pEntry = pParam->pEntry;
94,430,377✔
652

653
  // update cache
654
  SMetaInfo info = {0};
94,449,993✔
655
  metaBuildEntryInfo(pEntry, &info);
94,441,650✔
656
  code = metaCacheUpsert(pMeta, &info);
94,355,329✔
657
  if (code) {
94,392,577✔
658
    metaErr(vgId, code);
×
659
  }
660

661
  // put to tdb
662
  SUidIdxVal value = {
94,392,577✔
663
      .suid = info.suid,
94,411,672✔
664
      .skmVer = info.skmVer,
94,324,513✔
665
      .version = pEntry->version,
94,411,672✔
666
  };
667
  if (META_TABLE_OP_INSERT == op) {
94,324,513✔
668
    code = tdbTbInsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
76,428,370✔
669
  } else if (META_TABLE_OP_UPDATA == op) {
17,896,143✔
670
    code = tdbTbUpsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
17,954,394✔
671
  }
672
  return code;
94,420,862✔
673
}
674

675
static int32_t metaUidIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
76,446,403✔
676
  return metaUidIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
76,446,403✔
677
}
678

679
static int32_t metaUidIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
17,961,274✔
680
  return metaUidIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
17,961,274✔
681
}
682

683
static int32_t metaUidIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
2,885,592✔
684
  int32_t code = 0;
2,885,592✔
685

686
  const SMetaEntry *pEntry = pParam->pOldEntry;
2,885,592✔
687

688
  // delete tdb
689
  code = tdbTbDelete(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), pMeta->txn);
2,886,195✔
690
  if (code) {
2,885,592✔
691
    metaErr(TD_VID(pMeta->pVnode), code);
×
692
  }
693

694
  // delete cache
695
  (void)metaCacheDrop(pMeta, pEntry->uid);
2,885,592✔
696
  return code;
2,886,445✔
697
}
698

699
// Name Index
700
static int32_t metaNameIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
76,466,749✔
701
  int32_t code = TSDB_CODE_SUCCESS;
76,466,749✔
702

703
  const SMetaEntry *pEntry = pParam->pEntry;
76,466,749✔
704

705
  if (META_TABLE_OP_INSERT == op) {
76,478,804✔
706
    code = tdbTbInsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
76,478,804✔
707
                       pMeta->txn);
708
  } else if (META_TABLE_OP_UPDATA == op) {
×
709
    code = tdbTbUpsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
×
710
                       pMeta->txn);
711
  } else {
712
    code = TSDB_CODE_INVALID_PARA;
×
713
  }
714
  if (code) {
76,480,269✔
715
    metaErr(TD_VID(pMeta->pVnode), code);
×
716
  }
717
  return code;
76,481,493✔
718
}
719

720
static int32_t metaNameIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
76,468,562✔
721
  int32_t code = TSDB_CODE_SUCCESS;
76,468,562✔
722
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
76,468,562✔
723
}
724

725
static int32_t metaNameIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
726
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
×
727
}
728

729
static int32_t metaNameIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
2,885,842✔
730
  int32_t code = TSDB_CODE_SUCCESS;
2,885,842✔
731

732
  const SMetaEntry *pEntry = pParam->pOldEntry;
2,885,842✔
733
  code = tdbTbDelete(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, pMeta->txn);
2,885,842✔
734
  if (code) {
2,886,445✔
735
    metaErr(TD_VID(pMeta->pVnode), code);
×
736
  }
737
  return code;
2,885,309✔
738
}
739

740
// Suid Index
741
static int32_t metaSUidIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
4,585,375✔
742
  const SMetaEntry *pEntry = pParam->pEntry;
4,585,375✔
743

744
  int32_t code = tdbTbInsert(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), NULL, 0, pMeta->txn);
4,590,146✔
745
  if (code) {
4,589,288✔
746
    metaErr(TD_VID(pMeta->pVnode), code);
×
747
  }
748
  return code;
4,587,930✔
749
}
750

751
static int32_t metaSUidIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
851,816✔
752
  const SMetaEntry *pEntry = pParam->pOldEntry;
851,816✔
753

754
  int32_t code = tdbTbDelete(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), pMeta->txn);
852,419✔
755
  if (code) {
852,419✔
756
    metaErr(TD_VID(pMeta->pVnode), code);
×
757
  }
758
  return code;
851,566✔
759
}
760

761
// Child Index
762
static int32_t metaChildIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
65,649,254✔
763
  int32_t code = TSDB_CODE_SUCCESS;
65,649,254✔
764

765
  const SMetaEntry *pEntry = pParam->pEntry;
65,649,254✔
766

767
  SCtbIdxKey key = {
65,652,872✔
768
      .suid = pEntry->ctbEntry.suid,
65,663,409✔
769
      .uid = pEntry->uid,
65,641,569✔
770
  };
771

772
  if (META_TABLE_OP_INSERT == op) {
65,656,777✔
773
    code = tdbTbInsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
116,955,704✔
774
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
58,482,092✔
775
  } else if (META_TABLE_OP_UPDATA == op) {
7,176,180✔
776
    code = tdbTbUpsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
14,352,360✔
777
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
7,176,180✔
778
  } else {
779
    code = TSDB_CODE_INVALID_PARA;
×
780
  }
781
  return code;
65,666,255✔
782
}
783

784
static int32_t metaChildIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
58,475,531✔
785
  return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
58,475,531✔
786
}
787

788
static int32_t metaChildIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
7,353,555✔
789
  const SMetaEntry *pEntry = pParam->pEntry;
7,353,555✔
790
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
7,353,555✔
791
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
7,353,555✔
792

793
  const STag *pNewTags = (const STag *)pEntry->ctbEntry.pTags;
7,353,555✔
794
  const STag *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags;
7,353,555✔
795
  if (pNewTags->len != pOldTags->len || memcmp(pNewTags, pOldTags, pNewTags->len)) {
7,353,555✔
796
    return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
7,176,180✔
797
  }
798
  return 0;
177,375✔
799
}
800

801
static int32_t metaChildIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
1,273,173✔
802
  const SMetaEntry *pEntry = pParam->pOldEntry;
1,273,173✔
803

804
  SCtbIdxKey key = {
1,272,998✔
805
      .suid = pEntry->ctbEntry.suid,
1,273,173✔
806
      .uid = pEntry->uid,
1,273,173✔
807
  };
808
  return tdbTbDelete(pMeta->pCtbIdx, &key, sizeof(key), pMeta->txn);
1,273,173✔
809
}
810

811
// Tag Index
812
static int32_t metaFetchTagIdxKey(SMeta *pMeta, const SMetaEntry *pEntry, const SSchema *pTagColumn,
73,983,346✔
813
                                  STagIdxKey **ppTagIdxKey, int32_t *pTagIdxKeySize) {
814
  int32_t code = TSDB_CODE_SUCCESS;
73,983,346✔
815

816
  STagIdxKey *pTagIdxKey = NULL;
73,983,346✔
817
  int32_t     nTagIdxKey;
73,987,462✔
818
  const void *pTagData = NULL;
73,992,371✔
819
  int32_t     nTagData = 0;
73,992,371✔
820

821
  STagVal tagVal = {
73,992,371✔
822
      .cid = pTagColumn->colId,
73,995,468✔
823
  };
824

825
  if (tTagGet((const STag *)pEntry->ctbEntry.pTags, &tagVal)) {
73,987,490✔
826
    if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
71,234,713✔
827
      pTagData = tagVal.pData;
8,876,396✔
828
      nTagData = (int32_t)tagVal.nData;
8,876,396✔
829
    } else {
830
      pTagData = &(tagVal.i64);
62,354,246✔
831
      nTagData = tDataTypes[pTagColumn->type].bytes;
62,354,246✔
832
    }
833
  } else {
834
    if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
2,746,432✔
835
      nTagData = tDataTypes[pTagColumn->type].bytes;
2,658,663✔
836
    }
837
  }
838

839
  code = metaCreateTagIdxKey(pEntry->ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type,
73,985,388✔
840
                             pEntry->uid, &pTagIdxKey, &nTagIdxKey);
73,972,320✔
841
  if (code) {
73,978,345✔
842
    metaErr(TD_VID(pMeta->pVnode), code);
×
843
    return code;
×
844
  }
845

846
  *ppTagIdxKey = pTagIdxKey;
73,978,345✔
847
  *pTagIdxKeySize = nTagIdxKey;
73,971,100✔
848
  return code;
73,973,183✔
849
}
850

851
static void metaFetchTagIdxKeyFree(STagIdxKey **ppTagIdxKey) {
73,984,605✔
852
  metaDestroyTagIdxKey(*ppTagIdxKey);
73,984,605✔
853
  *ppTagIdxKey = NULL;
73,986,849✔
854
}
73,989,138✔
855

856
static int32_t metaTagIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
58,474,171✔
857
  int32_t code = TSDB_CODE_SUCCESS;
58,474,171✔
858

859
  const SMetaEntry *pEntry = pParam->pEntry;
58,474,171✔
860
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
58,481,744✔
861

862
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
58,483,168✔
863
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
58,724,605✔
864
    const SSchema *pTagColumn = &pTagSchema->pSchema[0];
244,457✔
865

866
    STagVal tagVal = {
488,914✔
867
        .cid = pTagColumn->colId,
244,457✔
868
    };
869

870
    const void *pTagData = pEntry->ctbEntry.pTags;
244,457✔
871
    int32_t     nTagData = ((const STag *)pEntry->ctbEntry.pTags)->len;
244,457✔
872
    code = metaSaveJsonVarToIdx(pMeta, pEntry, pTagColumn);
244,457✔
873
    if (code) {
244,457✔
874
      metaErr(TD_VID(pMeta->pVnode), code);
×
875
    }
876
  } else {
877
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
248,802,821✔
878
      STagIdxKey    *pTagIdxKey = NULL;
190,548,891✔
879
      int32_t        nTagIdxKey;
190,553,023✔
880
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
190,573,838✔
881

882
      if (!IS_IDX_ON(pTagColumn)) {
190,575,592✔
883
        continue;
132,301,722✔
884
      }
885

886
      code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pTagIdxKey, &nTagIdxKey);
58,275,925✔
887
      if (code) {
58,257,454✔
888
        metaErr(TD_VID(pMeta->pVnode), code);
×
889
        return code;
×
890
      }
891

892
      code = tdbTbInsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn);
58,257,454✔
893
      if (code) {
58,269,163✔
894
        metaErr(TD_VID(pMeta->pVnode), code);
×
895
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
896
        return code;
×
897
      }
898
      metaFetchTagIdxKeyFree(&pTagIdxKey);
58,269,163✔
899
    }
900
  }
901
  return code;
58,485,924✔
902
}
903

904
static int32_t metaTagIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
7,353,555✔
905
  int32_t code = TSDB_CODE_SUCCESS;
7,353,555✔
906

907
  const SMetaEntry     *pEntry = pParam->pEntry;
7,353,555✔
908
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
7,353,555✔
909
  const SMetaEntry     *pSuperEntry = pParam->pSuperEntry;
7,353,555✔
910
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
7,353,555✔
911
  const STag           *pNewTags = (const STag *)pEntry->ctbEntry.pTags;
7,353,555✔
912
  const STag           *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags;
7,353,555✔
913

914
  if (pNewTags->len == pOldTags->len && !memcmp(pNewTags, pOldTags, pNewTags->len)) {
7,353,555✔
915
    return code;
177,375✔
916
  }
917

918
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
7,176,180✔
919
    code = metaDelJsonVarFromIdx(pMeta, pOldEntry, &pTagSchema->pSchema[0]);
886✔
920
    if (code) {
886✔
921
      metaErr(TD_VID(pMeta->pVnode), code);
×
922
      return code;
×
923
    }
924

925
    code = metaSaveJsonVarToIdx(pMeta, pEntry, &pTagSchema->pSchema[0]);
886✔
926
    if (code) {
886✔
927
      metaErr(TD_VID(pMeta->pVnode), code);
×
928
      return code;
×
929
    }
930
  } else {
931
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
15,385,405✔
932
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
8,210,111✔
933

934
      if (!IS_IDX_ON(pTagColumn)) {
8,210,111✔
935
        continue;
1,035,567✔
936
      }
937

938
      STagIdxKey *pOldTagIdxKey = NULL;
7,174,544✔
939
      int32_t     oldTagIdxKeySize = 0;
7,174,544✔
940
      STagIdxKey *pNewTagIdxKey = NULL;
7,174,544✔
941
      int32_t     newTagIdxKeySize = 0;
7,174,544✔
942

943
      code = metaFetchTagIdxKey(pMeta, pOldEntry, pTagColumn, &pOldTagIdxKey, &oldTagIdxKeySize);
7,174,544✔
944
      if (code) {
7,174,544✔
945
        metaErr(TD_VID(pMeta->pVnode), code);
×
946
        return code;
×
947
      }
948

949
      code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pNewTagIdxKey, &newTagIdxKeySize);
7,174,544✔
950
      if (code) {
7,174,544✔
951
        metaErr(TD_VID(pMeta->pVnode), code);
×
952
        metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
953
        return code;
×
954
      }
955

956
      if (tagIdxKeyCmpr(pOldTagIdxKey, oldTagIdxKeySize, pNewTagIdxKey, newTagIdxKeySize)) {
7,174,544✔
957
        code = tdbTbDelete(pMeta->pTagIdx, pOldTagIdxKey, oldTagIdxKeySize, pMeta->txn);
6,934,584✔
958
        if (code) {
6,934,584✔
959
          metaErr(TD_VID(pMeta->pVnode), code);
×
960
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
961
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
962
          return code;
×
963
        }
964

965
        code = tdbTbInsert(pMeta->pTagIdx, pNewTagIdxKey, newTagIdxKeySize, NULL, 0, pMeta->txn);
6,934,584✔
966
        if (code) {
6,934,584✔
967
          metaErr(TD_VID(pMeta->pVnode), code);
×
968
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
969
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
970
          return code;
×
971
        }
972
      }
973

974
      metaFetchTagIdxKeyFree(&pOldTagIdxKey);
7,174,544✔
975
      metaFetchTagIdxKeyFree(&pNewTagIdxKey);
7,174,544✔
976
    }
977
  }
978
  return code;
7,176,180✔
979
}
980

981
static int32_t metaTagIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
1,273,173✔
982
  int32_t code = TSDB_CODE_SUCCESS;
1,273,173✔
983

984
  const SMetaEntry     *pEntry = pParam->pEntry;
1,273,173✔
985
  const SMetaEntry     *pChild = pParam->pOldEntry;
1,273,173✔
986
  const SMetaEntry     *pSuper = pParam->pSuperEntry;
1,272,998✔
987
  const SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
1,273,173✔
988
  const SSchema        *pTagColumn = NULL;
1,273,173✔
989
  const STag           *pTags = (const STag *)pChild->ctbEntry.pTags;
1,273,173✔
990

991
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
1,272,588✔
992
    pTagColumn = &pTagSchema->pSchema[0];
108,658✔
993
    code = metaDelJsonVarFromIdx(pMeta, pChild, pTagColumn);
108,658✔
994
    if (code) {
108,658✔
995
      metaErr(TD_VID(pMeta->pVnode), code);
×
996
    }
997
  } else {
998
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
5,250,094✔
999
      pTagColumn = &pTagSchema->pSchema[i];
4,085,579✔
1000
      if (!IS_IDX_ON(pTagColumn)) {
4,084,994✔
1001
        continue;
2,873,664✔
1002
      }
1003

1004
      STagIdxKey *pTagIdxKey = NULL;
1,211,740✔
1005
      int32_t     nTagIdxKey;
1,211,915✔
1006

1007
      code = metaFetchTagIdxKey(pMeta, pChild, pTagColumn, &pTagIdxKey, &nTagIdxKey);
1,211,915✔
1008
      if (code) {
1,211,740✔
1009
        metaErr(TD_VID(pMeta->pVnode), code);
×
1010
        return code;
×
1011
      }
1012

1013
      code = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
1,211,740✔
1014
      if (code) {
1,211,915✔
1015
        metaErr(TD_VID(pMeta->pVnode), code);
×
1016
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
1017
        return code;
×
1018
      }
1019
      metaFetchTagIdxKeyFree(&pTagIdxKey);
1,211,915✔
1020
    }
1021
  }
1022
  return code;
1,272,763✔
1023
}
1024

1025
// Btime Index
1026
static int32_t metaBtimeIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
73,922,699✔
1027
  int32_t code = TSDB_CODE_SUCCESS;
73,922,699✔
1028

1029
  const SMetaEntry *pEntry;
1030
  if (META_TABLE_OP_DELETE == op) {
73,922,699✔
1031
    pEntry = pParam->pOldEntry;
2,034,026✔
1032
  } else {
1033
    pEntry = pParam->pEntry;
71,888,673✔
1034
  }
1035

1036
  SBtimeIdxKey key = {
73,923,016✔
1037
      .uid = pEntry->uid,
73,921,868✔
1038
  };
1039

1040
  if (TSDB_CHILD_TABLE == pEntry->type || TSDB_VIRTUAL_CHILD_TABLE == pEntry->type) {
73,928,749✔
1041
    key.btime = pEntry->ctbEntry.btime;
59,754,417✔
1042
  } else if (TSDB_NORMAL_TABLE == pEntry->type || TSDB_VIRTUAL_NORMAL_TABLE == pEntry->type) {
14,175,205✔
1043
    key.btime = pEntry->ntbEntry.btime;
14,175,205✔
1044
  } else {
1045
    return TSDB_CODE_INVALID_PARA;
×
1046
  }
1047

1048
  if (META_TABLE_OP_INSERT == op) {
73,918,639✔
1049
    code = tdbTbInsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
71,884,613✔
1050
  } else if (META_TABLE_OP_UPDATA == op) {
2,034,026✔
1051
    code = tdbTbUpsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
×
1052
  } else if (META_TABLE_OP_DELETE == op) {
2,034,026✔
1053
    code = tdbTbDelete(pMeta->pBtimeIdx, &key, sizeof(key), pMeta->txn);
2,034,026✔
1054
  } else {
1055
    code = TSDB_CODE_INVALID_PARA;
×
1056
  }
1057
  if (code) {
73,925,775✔
1058
    metaErr(TD_VID(pMeta->pVnode), code);
×
1059
  }
1060
  return code;
73,924,362✔
1061
}
1062

1063
static int32_t metaBtimeIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
71,890,382✔
1064
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
71,890,382✔
1065
}
1066

1067
static int32_t metaBtimeIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
1068
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
×
1069
}
1070

1071
static int32_t metaBtimeIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
2,034,026✔
1072
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_DELETE);
2,034,026✔
1073
}
1074

1075
// TTL Index
1076
static int32_t metaTtlIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
71,645,680✔
1077
  const SMetaEntry *pEntry = pParam->pEntry;
71,645,680✔
1078

1079
  STtlUpdTtlCtx ctx = {
71,652,431✔
1080
      .uid = pEntry->uid,
71,655,369✔
1081
      .pTxn = pMeta->txn,
71,653,403✔
1082
  };
1083
  if (TSDB_CHILD_TABLE == pEntry->type) {
71,653,074✔
1084
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
58,327,098✔
1085
    ctx.changeTimeMs = pEntry->ctbEntry.btime;
58,335,261✔
1086
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
13,321,295✔
1087
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
13,321,295✔
1088
    ctx.changeTimeMs = pEntry->ntbEntry.btime;
13,321,295✔
1089
  } else {
1090
    return TSDB_CODE_INVALID_PARA;
×
1091
  }
1092

1093
  int32_t ret = ttlMgrInsertTtl(pMeta->pTtlMgr, &ctx);
71,652,494✔
1094
  if (ret < 0) {
71,641,865✔
1095
    metaError("vgId:%d, failed to insert ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid, tstrerror(ret));
×
1096
  }
1097
  return TSDB_CODE_SUCCESS;
71,642,856✔
1098
}
1099

1100
static int32_t metaTtlIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
71,645,971✔
1101
  return metaTtlIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
71,645,971✔
1102
}
1103

1104
static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam);
1105

1106
static int32_t metaTtlIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
10,587,046✔
1107
  int32_t code = TSDB_CODE_SUCCESS;
10,587,046✔
1108

1109
  const SMetaEntry *pEntry = pParam->pEntry;
10,587,046✔
1110
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
10,587,046✔
1111

1112
  if ((pEntry->type == TSDB_CHILD_TABLE && pOldEntry->ctbEntry.ttlDays != pEntry->ctbEntry.ttlDays) ||
10,587,046✔
1113
      (pEntry->type == TSDB_NORMAL_TABLE && pOldEntry->ntbEntry.ttlDays != pEntry->ntbEntry.ttlDays)) {
10,582,730✔
1114
    code = metaTtlIdxDelete(pMeta, pParam);
8,696✔
1115
    if (code) {
8,696✔
1116
      metaErr(TD_VID(pMeta->pVnode), code);
×
1117
    }
1118

1119
    code = metaTtlIdxInsert(pMeta, pParam);
8,696✔
1120
    if (code) {
8,696✔
1121
      metaErr(TD_VID(pMeta->pVnode), code);
×
1122
    }
1123
  }
1124

1125
  return TSDB_CODE_SUCCESS;
10,587,046✔
1126
}
1127

1128
static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
1,990,431✔
1129
  int32_t code = TSDB_CODE_SUCCESS;
1,990,431✔
1130

1131
  const SMetaEntry *pEntry = pParam->pOldEntry;
1,990,431✔
1132
  STtlDelTtlCtx     ctx = {
1,990,431✔
1133
          .uid = pEntry->uid,
1,990,431✔
1134
          .pTxn = pMeta->txn,
1,990,021✔
1135
  };
1136

1137
  if (TSDB_CHILD_TABLE == pEntry->type) {
1,990,256✔
1138
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
1,231,479✔
1139
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
758,777✔
1140
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
738,872✔
1141
  } else {
1142
    code = TSDB_CODE_INVALID_PARA;
19,905✔
1143
  }
1144

1145
  if (TSDB_CODE_SUCCESS == code) {
1,990,021✔
1146
    int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
1,970,526✔
1147
    if (ret < 0) {
1,970,351✔
1148
      metaError("vgId:%d, failed to delete ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid,
×
1149
                tstrerror(ret));
1150
    }
1151
  }
1152
  return code;
1,989,846✔
1153
}
1154

1155
static void metaTimeSeriesNotifyCheck(SMeta *pMeta) {
78,591,962✔
1156
#if defined(TD_ENTERPRISE)
1157
  int64_t nTimeSeries = metaGetTimeSeriesNum(pMeta, 0);
78,591,962✔
1158
  int64_t deltaTS = nTimeSeries - pMeta->pVnode->config.vndStats.numOfReportedTimeSeries;
78,594,586✔
1159
  if (deltaTS > tsTimeSeriesThreshold) {
78,599,626✔
1160
    if (0 == atomic_val_compare_exchange_8(&dmNotifyHdl.state, 1, 2)) {
44,749,517✔
1161
      if (tsem_post(&dmNotifyHdl.sem) != 0) {
44,762,408✔
1162
        metaError("vgId:%d, failed to post semaphore, errno:%d", TD_VID(pMeta->pVnode), ERRNO);
×
1163
      }
1164
    }
1165
  }
1166
#endif
1167
}
78,637,287✔
1168

1169
static int32_t (*metaTableOpFn[META_TABLE_MAX][META_TABLE_OP_MAX])(SMeta *pMeta, const SMetaHandleParam *pParam) =
1170
    {
1171
        [META_ENTRY_TABLE] =
1172
            {
1173
                [META_TABLE_OP_INSERT] = metaEntryTableInsert,
1174
                [META_TABLE_OP_UPDATA] = metaEntryTableUpdate,
1175
                [META_TABLE_OP_DELETE] = metaEntryTableDelete,
1176
            },
1177
        [META_SCHEMA_TABLE] =
1178
            {
1179
                [META_TABLE_OP_INSERT] = metaSchemaTableInsert,
1180
                [META_TABLE_OP_UPDATA] = metaSchemaTableUpdate,
1181
                [META_TABLE_OP_DELETE] = metaSchemaTableDelete,
1182
            },
1183
        [META_UID_IDX] =
1184
            {
1185
                [META_TABLE_OP_INSERT] = metaUidIdxInsert,
1186
                [META_TABLE_OP_UPDATA] = metaUidIdxUpdate,
1187
                [META_TABLE_OP_DELETE] = metaUidIdxDelete,
1188
            },
1189
        [META_NAME_IDX] =
1190
            {
1191
                [META_TABLE_OP_INSERT] = metaNameIdxInsert,
1192
                [META_TABLE_OP_UPDATA] = metaNameIdxUpdate,
1193
                [META_TABLE_OP_DELETE] = metaNameIdxDelete,
1194
            },
1195
        [META_SUID_IDX] =
1196
            {
1197
                [META_TABLE_OP_INSERT] = metaSUidIdxInsert,
1198
                [META_TABLE_OP_UPDATA] = NULL,
1199
                [META_TABLE_OP_DELETE] = metaSUidIdxDelete,
1200
            },
1201
        [META_CHILD_IDX] =
1202
            {
1203
                [META_TABLE_OP_INSERT] = metaChildIdxInsert,
1204
                [META_TABLE_OP_UPDATA] = metaChildIdxUpdate,
1205
                [META_TABLE_OP_DELETE] = metaChildIdxDelete,
1206
            },
1207
        [META_TAG_IDX] =
1208
            {
1209
                [META_TABLE_OP_INSERT] = metaTagIdxInsert,
1210
                [META_TABLE_OP_UPDATA] = metaTagIdxUpdate,
1211
                [META_TABLE_OP_DELETE] = metaTagIdxDelete,
1212
            },
1213
        [META_BTIME_IDX] =
1214
            {
1215
                [META_TABLE_OP_INSERT] = metaBtimeIdxInsert,
1216
                [META_TABLE_OP_UPDATA] = metaBtimeIdxUpdate,
1217
                [META_TABLE_OP_DELETE] = metaBtimeIdxDelete,
1218
            },
1219
        [META_TTL_IDX] =
1220
            {
1221
                [META_TABLE_OP_INSERT] = metaTtlIdxInsert,
1222
                [META_TABLE_OP_UPDATA] = metaTtlIdxUpdate,
1223
                [META_TABLE_OP_DELETE] = metaTtlIdxDelete,
1224
            },
1225
};
1226

1227
static int32_t metaHandleSuperTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
4,593,953✔
1228
  int32_t code = TSDB_CODE_SUCCESS;
4,593,953✔
1229

1230
  SMetaTableOp ops[] = {
4,593,953✔
1231
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1232
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: here should be insert
1233
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1234
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1235
      {META_SUID_IDX, META_TABLE_OP_INSERT},      //
1236
  };
1237

1238
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
27,554,071✔
1239
    SMetaTableOp          *op = &ops[i];
22,956,328✔
1240
    const SMetaHandleParam param = {
22,970,518✔
1241
        .pEntry = pEntry,
1242
    };
1243
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
22,972,630✔
1244
    if (TSDB_CODE_SUCCESS != code) {
22,943,252✔
1245
      metaErr(TD_VID(pMeta->pVnode), code);
×
1246
      return code;
×
1247
    }
1248
  }
1249

1250
  return code;
4,597,743✔
1251
}
1252
static int32_t metaHandleSuperTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
4,573,342✔
1253
  int32_t code = TSDB_CODE_SUCCESS;
4,573,342✔
1254

1255
  metaWLock(pMeta);
4,573,342✔
1256
  code = metaHandleSuperTableCreateImpl(pMeta, pEntry);
4,598,558✔
1257
  metaULock(pMeta);
4,588,847✔
1258

1259
  if (TSDB_CODE_SUCCESS == code) {
4,595,425✔
1260
    pMeta->pVnode->config.vndStats.numOfSTables++;
4,595,425✔
1261

1262
    metaInfo("vgId:%d, %s success, version:%" PRId64 " type:%d uid:%" PRId64 " name:%s", TD_VID(pMeta->pVnode),
4,600,035✔
1263
             __func__, pEntry->version, pEntry->type, pEntry->uid, pEntry->name);
1264
  } else {
1265
    metaErr(TD_VID(pMeta->pVnode), code);
×
1266
  }
1267
  return code;
4,601,585✔
1268
}
1269

1270
static int32_t metaHandleNormalTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
13,316,915✔
1271
  int32_t code = TSDB_CODE_SUCCESS;
13,316,915✔
1272

1273
  SMetaTableOp ops[] = {
13,316,915✔
1274
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1275
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: need to be insert
1276
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1277
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1278
      {META_BTIME_IDX, META_TABLE_OP_INSERT},     //
1279
      {META_TTL_IDX, META_TABLE_OP_INSERT},       //
1280
  };
1281

1282
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
93,218,405✔
1283
    SMetaTableOp *op = &ops[i];
79,901,490✔
1284

1285
    SMetaHandleParam param = {
79,901,490✔
1286
        .pEntry = pEntry,
1287
    };
1288

1289
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
79,901,490✔
1290
    if (TSDB_CODE_SUCCESS != code) {
79,901,490✔
1291
      metaErr(TD_VID(pMeta->pVnode), code);
×
1292
      return code;
×
1293
    }
1294
  }
1295

1296
  return code;
13,316,915✔
1297
}
1298
static int32_t metaHandleNormalTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
13,316,915✔
1299
  int32_t code = TSDB_CODE_SUCCESS;
13,316,915✔
1300

1301
  // update TDB
1302
  metaWLock(pMeta);
13,316,915✔
1303
  code = metaHandleNormalTableCreateImpl(pMeta, pEntry);
13,316,915✔
1304
  metaULock(pMeta);
13,316,915✔
1305

1306
  // update other stuff
1307
  if (TSDB_CODE_SUCCESS == code) {
13,316,915✔
1308
    pMeta->pVnode->config.vndStats.numOfNTables++;
13,316,915✔
1309
    pMeta->pVnode->config.vndStats.numOfNTimeSeries += pEntry->ntbEntry.schemaRow.nCols - 1;
13,316,915✔
1310

1311
    if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
13,316,915✔
1312
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, -1, &pEntry->ntbEntry.schemaRow);
14,535✔
1313
      if (rc < 0) {
14,535✔
1314
        metaError("vgId:%d, failed to create table:%s since %s", TD_VID(pMeta->pVnode), pEntry->name, tstrerror(rc));
×
1315
      }
1316
    }
1317
    metaTimeSeriesNotifyCheck(pMeta);
13,316,915✔
1318
  } else {
1319
    metaErr(TD_VID(pMeta->pVnode), code);
×
1320
  }
1321
  return code;
13,316,915✔
1322
}
1323

1324
static int32_t metaHandleChildTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry, const SMetaEntry *pSuperEntry) {
58,329,478✔
1325
  int32_t code = TSDB_CODE_SUCCESS;
58,329,478✔
1326

1327
  SMetaTableOp ops[] = {
58,329,478✔
1328
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},  //
1329
      {META_UID_IDX, META_TABLE_OP_INSERT},      //
1330
      {META_NAME_IDX, META_TABLE_OP_INSERT},     //
1331
      {META_CHILD_IDX, META_TABLE_OP_INSERT},    //
1332
      {META_TAG_IDX, META_TABLE_OP_INSERT},      //
1333
      {META_BTIME_IDX, META_TABLE_OP_INSERT},    //
1334
      {META_TTL_IDX, META_TABLE_OP_INSERT},      //
1335
  };
1336

1337
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
466,604,795✔
1338
    SMetaTableOp *op = &ops[i];
408,269,740✔
1339

1340
    SMetaHandleParam param = {
408,283,921✔
1341
        .pEntry = pEntry,
1342
        .pSuperEntry = pSuperEntry,
1343
    };
1344

1345
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
408,288,208✔
1346
    if (TSDB_CODE_SUCCESS != code) {
408,251,099✔
1347
      metaErr(TD_VID(pMeta->pVnode), code);
×
1348
      return code;
×
1349
    }
1350
  }
1351

1352
  if (TSDB_CODE_SUCCESS == code) {
58,335,055✔
1353
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
58,324,691✔
1354
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
58,320,315✔
1355
    if (ret < 0) {
58,324,117✔
1356
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1357
    }
1358

1359
    ret = metaStableTagFilterCacheUpdateUid(
58,324,117✔
1360
      pMeta, pEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_ADD_TABLE);
1361
    if (ret < 0) {
58,313,849✔
1362
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1363
    }
1364

1365
    ret = metaTbGroupCacheClear(pMeta, pSuperEntry->uid);
58,313,849✔
1366
    if (ret < 0) {
58,324,916✔
1367
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1368
    }
1369
  }
1370
  return code;
58,318,936✔
1371
}
1372

1373
static int32_t metaHandleChildTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
58,315,682✔
1374
  int32_t     code = TSDB_CODE_SUCCESS;
58,315,682✔
1375
  SMetaEntry *pSuperEntry = NULL;
58,315,682✔
1376

1377
  // get the super table entry
1378
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
58,327,507✔
1379
  if (code) {
58,331,765✔
1380
    metaErr(TD_VID(pMeta->pVnode), code);
×
1381
    return code;
×
1382
  }
1383

1384
  // update TDB
1385
  metaWLock(pMeta);
58,331,765✔
1386
  code = metaHandleChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
58,328,713✔
1387
  metaULock(pMeta);
58,316,011✔
1388

1389
  // update other stuff
1390
  if (TSDB_CODE_SUCCESS == code) {
58,331,946✔
1391
    pMeta->pVnode->config.vndStats.numOfCTables++;
58,331,946✔
1392

1393
    if (!metaTbInFilterCache(pMeta, pSuperEntry->name, 1)) {
58,326,002✔
1394
      int32_t nCols = 0;
58,322,173✔
1395
      int32_t ret = metaGetStbStats(pMeta->pVnode, pSuperEntry->uid, 0, &nCols, 0);
58,324,518✔
1396
      if (ret < 0) {
58,319,524✔
1397
        metaErr(TD_VID(pMeta->pVnode), ret);
×
1398
      }
1399
      pMeta->pVnode->config.vndStats.numOfTimeSeries += (nCols > 0 ? nCols - 1 : 0);
58,319,524✔
1400
    }
1401

1402
    if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
58,328,672✔
1403
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, pEntry->ctbEntry.suid, NULL);
4,299,986✔
1404
      if (rc < 0) {
4,299,986✔
1405
        metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
1406
                  tstrerror(rc));
1407
      }
1408
    }
1409

1410
  } else {
1411
    metaErr(TD_VID(pMeta->pVnode), code);
×
1412
  }
1413
  metaTimeSeriesNotifyCheck(pMeta);
58,314,059✔
1414
  metaFetchEntryFree(&pSuperEntry);
58,335,118✔
1415
  return code;
58,331,523✔
1416
}
1417

1418
static int32_t metaHandleVirtualNormalTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
97,437✔
1419
  int32_t code = TSDB_CODE_SUCCESS;
97,437✔
1420

1421
  SMetaTableOp ops[] = {
97,437✔
1422
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1423
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: need to be insert
1424
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1425
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1426
      {META_BTIME_IDX, META_TABLE_OP_INSERT},     //
1427
  };
1428

1429
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
584,622✔
1430
    SMetaTableOp *op = &ops[i];
487,185✔
1431

1432
    SMetaHandleParam param = {
487,185✔
1433
        .pEntry = pEntry,
1434
    };
1435

1436
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
487,185✔
1437
    if (TSDB_CODE_SUCCESS != code) {
487,185✔
1438
      metaErr(TD_VID(pMeta->pVnode), code);
×
1439
      return code;
×
1440
    }
1441
  }
1442

1443
  return code;
97,437✔
1444
}
1445

1446
static int32_t metaHandleVirtualNormalTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
97,437✔
1447
  int32_t code = TSDB_CODE_SUCCESS;
97,437✔
1448

1449
  // update TDB
1450
  metaWLock(pMeta);
97,437✔
1451
  code = metaHandleVirtualNormalTableCreateImpl(pMeta, pEntry);
97,437✔
1452
  metaULock(pMeta);
97,437✔
1453

1454
  // update other stuff
1455
  if (TSDB_CODE_SUCCESS == code) {
97,437✔
1456
    pMeta->pVnode->config.vndStats.numOfVTables++;
97,437✔
1457
  } else {
1458
    metaErr(TD_VID(pMeta->pVnode), code);
×
1459
  }
1460
  return code;
97,437✔
1461
}
1462

1463
static int32_t metaHandleVirtualChildTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry,
152,553✔
1464
                                                     const SMetaEntry *pSuperEntry) {
1465
  int32_t code = TSDB_CODE_SUCCESS;
152,553✔
1466

1467
  SMetaTableOp ops[] = {
152,553✔
1468
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},  //
1469
      {META_UID_IDX, META_TABLE_OP_INSERT},      //
1470
      {META_NAME_IDX, META_TABLE_OP_INSERT},     //
1471
      {META_CHILD_IDX, META_TABLE_OP_INSERT},    //
1472
      {META_TAG_IDX, META_TABLE_OP_INSERT},      //
1473
      {META_BTIME_IDX, META_TABLE_OP_INSERT},    //
1474
  };
1475

1476
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1,067,871✔
1477
    SMetaTableOp *op = &ops[i];
915,318✔
1478

1479
    SMetaHandleParam param = {
915,318✔
1480
        .pEntry = pEntry,
1481
        .pSuperEntry = pSuperEntry,
1482
    };
1483

1484
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
915,318✔
1485
    if (TSDB_CODE_SUCCESS != code) {
915,318✔
1486
      metaErr(TD_VID(pMeta->pVnode), code);
×
1487
      return code;
×
1488
    }
1489
  }
1490

1491
  if (TSDB_CODE_SUCCESS == code) {
152,553✔
1492
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
152,553✔
1493
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
152,553✔
1494
    if (ret < 0) {
152,553✔
1495
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1496
    }
1497

1498
    ret = metaStableTagFilterCacheUpdateUid(
152,553✔
1499
      pMeta, pEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_ADD_TABLE);
1500
    if (ret < 0) {
152,553✔
1501
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1502
    }
1503

1504
    ret = metaTbGroupCacheClear(pMeta, pSuperEntry->uid);
152,553✔
1505
    if (ret < 0) {
152,553✔
1506
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1507
    }
1508

1509
    ret = metaRefDbsCacheClear(pMeta, pSuperEntry->uid);
152,553✔
1510
    if (ret < 0) {
152,553✔
1511
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1512
    }
1513
  }
1514

1515
  return code;
152,553✔
1516
}
1517

1518
static int32_t metaHandleVirtualChildTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
152,553✔
1519
  int32_t     code = TSDB_CODE_SUCCESS;
152,553✔
1520
  SMetaEntry *pSuperEntry = NULL;
152,553✔
1521

1522
  // get the super table entry
1523
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
152,553✔
1524
  if (code) {
152,553✔
1525
    metaErr(TD_VID(pMeta->pVnode), code);
×
1526
    return code;
×
1527
  }
1528

1529
  // update TDB
1530
  metaWLock(pMeta);
152,553✔
1531
  code = metaHandleVirtualChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
152,553✔
1532
  metaULock(pMeta);
152,553✔
1533

1534
  // update other stuff
1535
  if (TSDB_CODE_SUCCESS == code) {
152,553✔
1536
    pMeta->pVnode->config.vndStats.numOfVCTables++;
152,553✔
1537
  } else {
1538
    metaErr(TD_VID(pMeta->pVnode), code);
×
1539
  }
1540

1541
  metaFetchEntryFree(&pSuperEntry);
152,553✔
1542
  return code;
152,553✔
1543
}
1544

1545
static int32_t metaHandleNormalTableDropImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
734,492✔
1546
  int32_t code = TSDB_CODE_SUCCESS;
734,492✔
1547

1548
  SMetaTableOp ops[] = {
734,492✔
1549
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1550
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1551
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1552
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1553
      {META_TTL_IDX, META_TABLE_OP_DELETE},      //
1554

1555
      // {META_SCHEMA_TABLE, META_TABLE_OP_DELETE},  //
1556
  };
1557

1558
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
4,406,952✔
1559
    SMetaTableOp *op = &ops[i];
3,672,460✔
1560
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
3,672,460✔
1561
    if (code) {
3,672,460✔
1562
      const SMetaEntry *pEntry = pParam->pEntry;
×
1563
      metaErr(TD_VID(pMeta->pVnode), code);
×
1564
    }
1565
  }
1566

1567
  return code;
734,492✔
1568
}
1569

1570
static int32_t metaHandleNormalTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
734,492✔
1571
  int32_t     code = TSDB_CODE_SUCCESS;
734,492✔
1572
  SMetaEntry *pOldEntry = NULL;
734,492✔
1573

1574
  // fetch the entry
1575
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
734,492✔
1576
  if (code) {
734,492✔
1577
    metaErr(TD_VID(pMeta->pVnode), code);
×
1578
    return code;
×
1579
  }
1580

1581
  SMetaHandleParam param = {
734,492✔
1582
      .pEntry = pEntry,
1583
      .pOldEntry = pOldEntry,
1584
  };
1585

1586
  // do the drop
1587
  metaWLock(pMeta);
734,492✔
1588
  code = metaHandleNormalTableDropImpl(pMeta, &param);
734,492✔
1589
  metaULock(pMeta);
734,492✔
1590
  if (code) {
734,492✔
1591
    metaErr(TD_VID(pMeta->pVnode), code);
×
1592
    metaFetchEntryFree(&pOldEntry);
×
1593
    return code;
×
1594
  }
1595

1596
  // update other stuff
1597
  pMeta->pVnode->config.vndStats.numOfNTables--;
734,492✔
1598
  pMeta->pVnode->config.vndStats.numOfNTimeSeries -= (pOldEntry->ntbEntry.schemaRow.nCols - 1);
734,492✔
1599

1600
#if 0
1601
  if (tbUids) {
1602
    if (taosArrayPush(tbUids, &uid) == NULL) {
1603
      rc = terrno;
1604
      goto _exit;
1605
    }
1606
  }
1607
#endif
1608

1609
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
734,492✔
1610
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pOldEntry->uid, 0, NULL);
×
1611
    if (ret < 0) {
×
1612
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1613
    }
1614
  }
1615

1616
  metaFetchEntryFree(&pOldEntry);
734,492✔
1617
  return code;
734,492✔
1618
}
1619

1620
static int32_t metaHandleChildTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam, bool superDropped) {
1,247,243✔
1621
  int32_t code = TSDB_CODE_SUCCESS;
1,247,243✔
1622

1623
  const SMetaEntry *pEntry = pParam->pEntry;
1,247,243✔
1624
  const SMetaEntry *pChild = pParam->pOldEntry;
1,247,068✔
1625
  const SMetaEntry *pSuper = pParam->pSuperEntry;
1,246,993✔
1626

1627
  SMetaTableOp ops[] = {
1,246,993✔
1628
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1629
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1630
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1631
      {META_CHILD_IDX, META_TABLE_OP_DELETE},    //
1632
      {META_TAG_IDX, META_TABLE_OP_DELETE},      //
1633
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1634
      {META_TTL_IDX, META_TABLE_OP_DELETE},      //
1635
  };
1636

1637
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
9,957,689✔
1638
    SMetaTableOp *op = &ops[i];
8,730,351✔
1639

1640
    if (op->table == META_ENTRY_TABLE && superDropped) {
8,730,351✔
1641
      continue;
708,054✔
1642
    }
1643

1644
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
8,022,297✔
1645
    if (code) {
8,022,297✔
1646
      metaErr(TD_VID(pMeta->pVnode), code);
19,905✔
1647
      return code;
19,905✔
1648
    }
1649
  }
1650

1651
  --pMeta->pVnode->config.vndStats.numOfCTables;
1,227,338✔
1652
  metaUpdateStbStats(pMeta, pParam->pSuperEntry->uid, -1, 0, -1);
1,227,338✔
1653
  int32_t ret = metaUidCacheClear(pMeta, pSuper->uid);
1,227,088✔
1654
  if (ret < 0) {
1,227,338✔
1655
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1656
  }
1657

1658
  ret = metaStableTagFilterCacheUpdateUid(
1,227,338✔
1659
    pMeta, pChild, pSuper, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
1660
  if (ret < 0) {
1,227,338✔
1661
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1662
  }
1663

1664
  ret = metaTbGroupCacheClear(pMeta, pSuper->uid);
1,227,338✔
1665
  if (ret < 0) {
1,227,338✔
1666
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1667
  }
1668
  return code;
1,227,338✔
1669
}
1670

1671
static int32_t metaHandleChildTableDrop(SMeta *pMeta, const SMetaEntry *pEntry, bool superDropped) {
1,247,243✔
1672
  int32_t     code = TSDB_CODE_SUCCESS;
1,247,243✔
1673
  SMetaEntry *pChild = NULL;
1,247,243✔
1674
  SMetaEntry *pSuper = NULL;
1,247,243✔
1675

1676
  // fetch old entry
1677
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pChild);
1,247,243✔
1678
  if (code) {
1,247,243✔
1679
    metaErr(TD_VID(pMeta->pVnode), code);
×
1680
    return code;
×
1681
  }
1682

1683
  // fetch super entry
1684
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
1,247,243✔
1685
  if (code) {
1,247,243✔
1686
    metaErr(TD_VID(pMeta->pVnode), code);
×
1687
    metaFetchEntryFree(&pChild);
×
1688
    return code;
×
1689
  }
1690

1691
  SMetaHandleParam param = {
1,247,243✔
1692
      .pEntry = pEntry,
1693
      .pOldEntry = pChild,
1694
      .pSuperEntry = pSuper,
1695
  };
1696

1697
  // do the drop
1698
  metaWLock(pMeta);
1,247,243✔
1699
  code = metaHandleChildTableDropImpl(pMeta, &param, superDropped);
1,247,243✔
1700
  metaULock(pMeta);
1,246,833✔
1701
  if (code) {
1,246,344✔
1702
    metaErr(TD_VID(pMeta->pVnode), code);
19,905✔
1703
    metaFetchEntryFree(&pChild);
19,905✔
1704
    metaFetchEntryFree(&pSuper);
19,905✔
1705
    return code;
19,905✔
1706
  }
1707

1708
  // do other stuff
1709
  if (!metaTbInFilterCache(pMeta, pSuper->name, 1)) {
1,226,439✔
1710
    int32_t      nCols = 0;
1,227,338✔
1711
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
1,227,338✔
1712
    if (metaGetStbStats(pMeta->pVnode, pSuper->uid, NULL, &nCols, 0) == 0) {
1,227,338✔
1713
      pStats->numOfTimeSeries -= nCols - 1;
1,227,338✔
1714
    }
1715
  }
1716

1717
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
1,227,338✔
1718
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pChild->uid, pSuper->uid, NULL);
7,761✔
1719
    if (ret < 0) {
7,761✔
1720
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1721
    }
1722
  }
1723

1724
#if 0
1725
  if (tbUids) {
1726
    if (taosArrayPush(tbUids, &uid) == NULL) {
1727
      rc = terrno;
1728
      goto _exit;
1729
    }
1730
  }
1731

1732
  if ((type == TSDB_CHILD_TABLE) && tbUid) {
1733
    *tbUid = uid;
1734
  }
1735
#endif
1736
  metaFetchEntryFree(&pChild);
1,227,338✔
1737
  metaFetchEntryFree(&pSuper);
1,227,338✔
1738
  return code;
1,227,338✔
1739
}
1740

1741
static int32_t metaHandleVirtualNormalTableDropImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
26,361✔
1742
  int32_t code = TSDB_CODE_SUCCESS;
26,361✔
1743

1744
  SMetaTableOp ops[] = {
26,361✔
1745
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1746
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1747
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1748
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1749

1750
      // {META_SCHEMA_TABLE, META_TABLE_OP_DELETE},  //
1751
  };
1752

1753
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
131,805✔
1754
    SMetaTableOp *op = &ops[i];
105,444✔
1755
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
105,444✔
1756
    if (code) {
105,444✔
1757
      const SMetaEntry *pEntry = pParam->pEntry;
×
1758
      metaErr(TD_VID(pMeta->pVnode), code);
×
1759
    }
1760
  }
1761

1762
  return code;
26,361✔
1763
}
1764

1765
static int32_t metaHandleVirtualNormalTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
26,361✔
1766
  int32_t     code = TSDB_CODE_SUCCESS;
26,361✔
1767
  SMetaEntry *pOldEntry = NULL;
26,361✔
1768

1769
  // fetch the entry
1770
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
26,361✔
1771
  if (code) {
26,361✔
1772
    metaErr(TD_VID(pMeta->pVnode), code);
×
1773
    return code;
×
1774
  }
1775

1776
  SMetaHandleParam param = {
26,361✔
1777
      .pEntry = pEntry,
1778
      .pOldEntry = pOldEntry,
1779
  };
1780

1781
  // do the drop
1782
  metaWLock(pMeta);
26,361✔
1783
  code = metaHandleVirtualNormalTableDropImpl(pMeta, &param);
26,361✔
1784
  metaULock(pMeta);
26,361✔
1785
  if (code) {
26,361✔
1786
    metaErr(TD_VID(pMeta->pVnode), code);
×
1787
    metaFetchEntryFree(&pOldEntry);
×
1788
    return code;
×
1789
  }
1790

1791
  // update other stuff
1792
  pMeta->pVnode->config.vndStats.numOfVTables--;
26,361✔
1793

1794
#if 0
1795
  if (tbUids) {
1796
    if (taosArrayPush(tbUids, &uid) == NULL) {
1797
      rc = terrno;
1798
      goto _exit;
1799
    }
1800
  }
1801
#endif
1802

1803
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
26,361✔
1804
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pOldEntry->uid, 0, NULL);
×
1805
    if (ret < 0) {
×
1806
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1807
    }
1808
  }
1809

1810
  metaFetchEntryFree(&pOldEntry);
26,361✔
1811
  return code;
26,361✔
1812
}
1813

1814
static int32_t metaHandleVirtualChildTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam, bool superDropped) {
25,930✔
1815
  int32_t code = TSDB_CODE_SUCCESS;
25,930✔
1816

1817
  const SMetaEntry *pEntry = pParam->pEntry;
25,930✔
1818
  const SMetaEntry *pChild = pParam->pOldEntry;
25,930✔
1819
  const SMetaEntry *pSuper = pParam->pSuperEntry;
25,930✔
1820

1821
  SMetaTableOp ops[] = {
25,930✔
1822
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1823
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1824
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1825
      {META_CHILD_IDX, META_TABLE_OP_DELETE},    //
1826
      {META_TAG_IDX, META_TABLE_OP_DELETE},      //
1827
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1828
  };
1829

1830
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
181,510✔
1831
    SMetaTableOp *op = &ops[i];
155,580✔
1832

1833
    if (op->table == META_ENTRY_TABLE && superDropped) {
155,580✔
1834
      continue;
×
1835
    }
1836

1837
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
155,580✔
1838
    if (code) {
155,580✔
1839
      metaErr(TD_VID(pMeta->pVnode), code);
×
1840
      return code;
×
1841
    }
1842
  }
1843

1844
  --pMeta->pVnode->config.vndStats.numOfVCTables;
25,930✔
1845
  metaUpdateStbStats(pMeta, pParam->pSuperEntry->uid, -1, 0, -1);
25,930✔
1846
  int32_t ret = metaUidCacheClear(pMeta, pSuper->uid);
25,930✔
1847
  if (ret < 0) {
25,930✔
1848
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1849
  }
1850

1851
  ret = metaStableTagFilterCacheUpdateUid(
25,930✔
1852
    pMeta, pChild, pSuper, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
1853
  if (ret < 0) {
25,930✔
1854
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1855
  }
1856

1857
  ret = metaTbGroupCacheClear(pMeta, pSuper->uid);
25,930✔
1858
  if (ret < 0) {
25,930✔
1859
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1860
  }
1861

1862
  ret = metaRefDbsCacheClear(pMeta, pSuper->uid);
25,930✔
1863
  if (ret < 0) {
25,930✔
1864
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1865
  }
1866

1867
  return code;
25,930✔
1868
}
1869

1870
static int32_t metaHandleVirtualChildTableDrop(SMeta *pMeta, const SMetaEntry *pEntry, bool superDropped) {
25,930✔
1871
  int32_t     code = TSDB_CODE_SUCCESS;
25,930✔
1872
  SMetaEntry *pChild = NULL;
25,930✔
1873
  SMetaEntry *pSuper = NULL;
25,930✔
1874

1875
  // fetch old entry
1876
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pChild);
25,930✔
1877
  if (code) {
25,930✔
1878
    metaErr(TD_VID(pMeta->pVnode), code);
×
1879
    return code;
×
1880
  }
1881

1882
  // fetch super entry
1883
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
25,930✔
1884
  if (code) {
25,930✔
1885
    metaErr(TD_VID(pMeta->pVnode), code);
×
1886
    metaFetchEntryFree(&pChild);
×
1887
    return code;
×
1888
  }
1889

1890
  SMetaHandleParam param = {
25,930✔
1891
      .pEntry = pEntry,
1892
      .pOldEntry = pChild,
1893
      .pSuperEntry = pSuper,
1894
  };
1895

1896
  // do the drop
1897
  metaWLock(pMeta);
25,930✔
1898
  code = metaHandleVirtualChildTableDropImpl(pMeta, &param, superDropped);
25,930✔
1899
  metaULock(pMeta);
25,930✔
1900
  if (code) {
25,930✔
1901
    metaErr(TD_VID(pMeta->pVnode), code);
×
1902
    metaFetchEntryFree(&pChild);
×
1903
    metaFetchEntryFree(&pSuper);
×
1904
    return code;
×
1905
  }
1906

1907
  metaFetchEntryFree(&pChild);
25,930✔
1908
  metaFetchEntryFree(&pSuper);
25,930✔
1909
  return code;
25,930✔
1910
}
1911

1912
int32_t metaGetChildUidsOfSuperTable(SMeta *pMeta, tb_uid_t suid, SArray **childList) {
910,691✔
1913
  int32_t code = TSDB_CODE_SUCCESS;
910,691✔
1914
  void   *key = NULL;
910,691✔
1915
  int32_t keySize = 0;
913,939✔
1916
  int32_t c;
913,328✔
1917

1918
  *childList = taosArrayInit(64, sizeof(tb_uid_t));
913,553✔
1919
  if (*childList == NULL) {
911,280✔
1920
    return terrno;
×
1921
  }
1922

1923
  TBC *cursor = NULL;
911,083✔
1924
  code = tdbTbcOpen(pMeta->pCtbIdx, &cursor, NULL);
911,686✔
1925
  if (code) {
913,389✔
1926
    taosArrayDestroy(*childList);
×
1927
    *childList = NULL;
×
1928
    return code;
×
1929
  }
1930

1931
  int32_t rc = tdbTbcMoveTo(cursor,
913,939✔
1932
                            &(SCtbIdxKey){
913,389✔
1933
                                .suid = suid,
1934
                                .uid = INT64_MIN,
1935
                            },
1936
                            sizeof(SCtbIdxKey), &c);
1937
  if (rc < 0) {
915,039✔
1938
    tdbTbcClose(cursor);
×
1939
    return 0;
×
1940
  }
1941

1942
  for (;;) {
1943
    if (tdbTbcNext(cursor, &key, &keySize, NULL, NULL) < 0) {
2,103,058✔
1944
      break;
679,706✔
1945
    }
1946

1947
    if (((SCtbIdxKey *)key)->suid < suid) {
1,421,596✔
1948
      continue;
190,750✔
1949
    } else if (((SCtbIdxKey *)key)->suid > suid) {
1,230,846✔
1950
      break;
235,333✔
1951
    }
1952

1953
    if (taosArrayPush(*childList, &(((SCtbIdxKey *)key)->uid)) == NULL) {
1,993,935✔
1954
      tdbFreeClear(key);
×
1955
      tdbTbcClose(cursor);
×
1956
      taosArrayDestroy(*childList);
×
1957
      *childList = NULL;
×
1958
      return terrno;
×
1959
    }
1960
  }
1961

1962
  tdbTbcClose(cursor);
915,039✔
1963
  tdbFreeClear(key);
914,136✔
1964
  return code;
914,136✔
1965
}
1966

1967
static int32_t metaHandleSuperTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
851,566✔
1968
  int32_t           code = TSDB_CODE_SUCCESS;
851,566✔
1969
  const SMetaEntry *pEntry = pParam->pEntry;
851,566✔
1970

1971
  SMetaTableOp ops[] = {
851,816✔
1972
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1973
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1974
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1975
      {META_SUID_IDX, META_TABLE_OP_DELETE},     //
1976

1977
      // {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: here should be insert
1978
  };
1979

1980
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
4,259,786✔
1981
    SMetaTableOp *op = &ops[i];
3,408,220✔
1982

1983
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
3,409,426✔
1984
    if (TSDB_CODE_SUCCESS != code) {
3,407,970✔
1985
      metaErr(TD_VID(pMeta->pVnode), code);
×
1986
      return code;
×
1987
    }
1988
  }
1989

1990
  int32_t ret = metaStatsCacheDrop(pMeta, pEntry->uid);
851,566✔
1991
  if (ret < 0) {
852,419✔
1992
    metaErr(TD_VID(pMeta->pVnode), ret);
304,275✔
1993
  }
1994
  return code;
852,419✔
1995
}
1996

1997
static int32_t metaHandleNormalTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
3,310,611✔
1998
  int32_t code = TSDB_CODE_SUCCESS;
3,310,611✔
1999

2000
  const SMetaEntry *pEntry = pParam->pEntry;
3,310,611✔
2001

2002
  SMetaTableOp ops[] = {
3,310,611✔
2003
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
2004
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
2005
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
2006
      {META_TTL_IDX, META_TABLE_OP_UPDATA},       //
2007
  };
2008
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
16,553,055✔
2009
    SMetaTableOp *op = &ops[i];
13,242,444✔
2010
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
13,242,444✔
2011
    if (code) {
13,242,444✔
2012
      metaErr(TD_VID(pMeta->pVnode), code);
×
2013
      return code;
×
2014
    }
2015
  }
2016
#if 0
2017
  if (metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs) < 0) {
2018
    metaError("vgId:%d, failed to update change time:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
2019
  }
2020
#endif
2021
  return code;
3,310,611✔
2022
}
2023

2024
static int32_t metaHandleVirtualNormalTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
200,095✔
2025
  int32_t code = TSDB_CODE_SUCCESS;
200,095✔
2026

2027
  const SMetaEntry *pEntry = pParam->pEntry;
200,095✔
2028

2029
  SMetaTableOp ops[] = {
200,095✔
2030
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
2031
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
2032
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
2033
  };
2034
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
800,380✔
2035
    SMetaTableOp *op = &ops[i];
600,285✔
2036
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
600,285✔
2037
    if (code) {
600,285✔
2038
      metaErr(TD_VID(pMeta->pVnode), code);
×
2039
      return code;
×
2040
    }
2041
  }
2042
#if 0
2043
  if (metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs) < 0) {
2044
    metaError("vgId:%d, failed to update change time:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
2045
  }
2046
#endif
2047
  return code;
200,095✔
2048
}
2049

2050
static int32_t metaHandleVirtualChildTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
77,120✔
2051
  int32_t code = TSDB_CODE_SUCCESS;
77,120✔
2052

2053
  const SMetaEntry *pEntry = pParam->pEntry;
77,120✔
2054
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
77,120✔
2055
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
77,120✔
2056

2057
  SMetaTableOp ops[] = {
77,120✔
2058
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},  //
2059
      {META_UID_IDX, META_TABLE_OP_UPDATA},      //
2060
      {META_TAG_IDX, META_TABLE_OP_UPDATA},      //
2061
      {META_CHILD_IDX, META_TABLE_OP_UPDATA},    //
2062
  };
2063

2064
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
385,600✔
2065
    SMetaTableOp *op = &ops[i];
308,480✔
2066
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
308,480✔
2067
    if (code) {
308,480✔
2068
      metaErr(TD_VID(pMeta->pVnode), code);
×
2069
      return code;
×
2070
    }
2071
  }
2072

2073
  if (metaUidCacheClear(pMeta, pSuperEntry->uid) < 0) {
77,120✔
2074
    metaErr(TD_VID(pMeta->pVnode), code);
×
2075
  }
2076

2077
  // update stable tag filter cache: drop old then add new
2078
  code = metaStableTagFilterCacheUpdateUid(
77,120✔
2079
    pMeta, pOldEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
2080
  if (TSDB_CODE_SUCCESS != code) {
77,120✔
2081
    metaErr(TD_VID(pMeta->pVnode), code);
×
2082
  }
2083
  code = metaStableTagFilterCacheUpdateUid(
77,120✔
2084
    pMeta, pEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_ADD_TABLE);
2085
  if (TSDB_CODE_SUCCESS != code) {
77,120✔
2086
    metaErr(TD_VID(pMeta->pVnode), code);
×
2087
  }
2088

2089
  if (metaTbGroupCacheClear(pMeta, pSuperEntry->uid) < 0) {
77,120✔
2090
    metaErr(TD_VID(pMeta->pVnode), code);
×
2091
  }
2092

2093
  if (metaRefDbsCacheClear(pMeta, pSuperEntry->uid) < 0) {
77,120✔
2094
    metaErr(TD_VID(pMeta->pVnode), code);
×
2095
  }
2096
  return code;
77,120✔
2097
}
2098

2099
static int32_t metaHandleChildTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
7,276,435✔
2100
  int32_t code = TSDB_CODE_SUCCESS;
7,276,435✔
2101

2102
  const SMetaEntry *pEntry = pParam->pEntry;
7,276,435✔
2103
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
7,276,435✔
2104
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
7,276,435✔
2105

2106
  SMetaTableOp ops[] = {
7,276,435✔
2107
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},  //
2108
      {META_UID_IDX, META_TABLE_OP_UPDATA},      //
2109
      {META_TAG_IDX, META_TABLE_OP_UPDATA},      //
2110
      {META_CHILD_IDX, META_TABLE_OP_UPDATA},    //
2111
      {META_TTL_IDX, META_TABLE_OP_UPDATA},      //
2112
  };
2113

2114
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
43,658,610✔
2115
    SMetaTableOp *op = &ops[i];
36,382,175✔
2116
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
36,382,175✔
2117
    if (code) {
36,382,175✔
2118
      metaErr(TD_VID(pMeta->pVnode), code);
×
2119
      return code;
×
2120
    }
2121
  }
2122

2123
  if (metaUidCacheClear(pMeta, pSuperEntry->uid) < 0) {
7,276,435✔
2124
    metaErr(TD_VID(pMeta->pVnode), code);
×
2125
  }
2126

2127
  // update stable tag filter cache: drop old then add new
2128
  code = metaStableTagFilterCacheUpdateUid(
7,276,435✔
2129
    pMeta, pOldEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
2130
  if (TSDB_CODE_SUCCESS != code) {
7,276,435✔
2131
    metaErr(TD_VID(pMeta->pVnode), code);
×
2132
  }
2133
  code = metaStableTagFilterCacheUpdateUid(
7,276,435✔
2134
    pMeta, pEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_ADD_TABLE);
2135
  if (TSDB_CODE_SUCCESS != code) {
7,276,435✔
2136
    metaErr(TD_VID(pMeta->pVnode), code);
×
2137
  }
2138

2139
  if (metaTbGroupCacheClear(pMeta, pSuperEntry->uid) < 0) {
7,276,435✔
2140
    metaErr(TD_VID(pMeta->pVnode), code);
×
2141
  }
2142
  return code;
7,276,435✔
2143
#if 0
2144
  if (metaUpdateChangeTime(pMeta, ctbEntry.uid, pReq->ctimeMs) < 0) {
2145
    metaError("meta/table: failed to update change time:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
2146
  }
2147
#endif
2148
}
2149

2150
static int32_t metaHandleSuperTableUpdateImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
7,108,570✔
2151
  int32_t code = TSDB_CODE_SUCCESS;
7,108,570✔
2152

2153
  const SMetaEntry *pEntry = pParam->pEntry;
7,108,570✔
2154
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
7,112,842✔
2155

2156
  SMetaTableOp ops[] = {
7,108,320✔
2157
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
2158
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
2159
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
2160
  };
2161

2162
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
28,430,180✔
2163
    SMetaTableOp *op = &ops[i];
21,329,318✔
2164
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
21,336,606✔
2165
    if (code) {
21,317,069✔
2166
      metaErr(TD_VID(pMeta->pVnode), code);
×
2167
      return code;
×
2168
    }
2169
  }
2170

2171
  if (TSDB_CODE_SUCCESS == code) {
7,100,862✔
2172
    metaUpdateStbStats(pMeta, pEntry->uid, 0, pEntry->stbEntry.schemaRow.nCols - pOldEntry->stbEntry.schemaRow.nCols,
7,108,735✔
2173
                       pEntry->stbEntry.keep);
7,102,871✔
2174
  }
2175

2176
  return code;
7,098,756✔
2177
}
2178

2179
static int32_t metaHandleSuperTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
7,101,697✔
2180
  int32_t code = TSDB_CODE_SUCCESS;
7,101,697✔
2181

2182
  SMetaEntry *pOldEntry = NULL;
7,101,697✔
2183

2184
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
7,096,660✔
2185
  if (code) {
7,109,796✔
2186
    metaErr(TD_VID(pMeta->pVnode), code);
×
2187
    return code;
×
2188
  }
2189

2190
  SMetaHandleParam param = {
7,109,796✔
2191
      .pEntry = pEntry,
2192
      .pOldEntry = pOldEntry,
2193
  };
2194
  metaWLock(pMeta);
7,111,379✔
2195
  code = metaHandleSuperTableUpdateImpl(pMeta, &param);
7,110,781✔
2196
  metaULock(pMeta);
7,096,913✔
2197
  if (code) {
7,065,212✔
2198
    metaErr(TD_VID(pMeta->pVnode), code);
×
2199
    metaFetchEntryFree(&pOldEntry);
×
2200
    return code;
×
2201
  }
2202

2203
  int     nCols = pEntry->stbEntry.schemaRow.nCols;
7,065,212✔
2204
  int     onCols = pOldEntry->stbEntry.schemaRow.nCols;
7,098,071✔
2205
  int32_t deltaCol = nCols - onCols;
7,088,935✔
2206
  bool    updStat = deltaCol != 0 && !TABLE_IS_VIRTUAL(pEntry->flags) && !metaTbInFilterCache(pMeta, pEntry->name, 1);
7,088,935✔
2207

2208
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
7,090,802✔
2209
    STsdb  *pTsdb = pMeta->pVnode->pTsdb;
29,232✔
2210
    SArray *uids = NULL; /*taosArrayInit(8, sizeof(int64_t));
29,232✔
2211
     if (uids == NULL) {
2212
       metaErr(TD_VID(pMeta->pVnode), code);
2213
       metaFetchEntryFree(&pOldEntry);
2214
       return terrno;
2215
       }*/
2216
    if (deltaCol == 1) {
29,232✔
2217
      int16_t cid = pEntry->stbEntry.schemaRow.pSchema[nCols - 1].colId;
16,704✔
2218
      int8_t  col_type = pEntry->stbEntry.schemaRow.pSchema[nCols - 1].type;
16,704✔
2219

2220
      code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &uids);
16,704✔
2221
      if (code) {
16,704✔
2222
        metaErr(TD_VID(pMeta->pVnode), code);
×
2223
        metaFetchEntryFree(&pOldEntry);
×
2224
        return code;
×
2225
      }
2226
      if (pTsdb) {
16,704✔
2227
        TAOS_CHECK_RETURN(tsdbCacheNewSTableColumn(pTsdb, uids, cid, col_type));
16,704✔
2228
      }
2229
    } else if (deltaCol == -1) {
12,528✔
2230
      int16_t cid = -1;
12,528✔
2231
      bool    hasPrimaryKey = false;
12,528✔
2232
      if (onCols >= 2) {
12,528✔
2233
        hasPrimaryKey = (pOldEntry->stbEntry.schemaRow.pSchema[1].flags & COL_IS_KEY) ? true : false;
12,528✔
2234
      }
2235
      for (int i = 0, j = 0; i < nCols && j < onCols; ++i, ++j) {
121,104✔
2236
        if (pEntry->stbEntry.schemaRow.pSchema[i].colId != pOldEntry->stbEntry.schemaRow.pSchema[j].colId) {
116,928✔
2237
          cid = pOldEntry->stbEntry.schemaRow.pSchema[j].colId;
8,352✔
2238
          break;
8,352✔
2239
        }
2240
      }
2241

2242
      if (cid != -1) {
12,528✔
2243
        code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &uids);
8,352✔
2244
        if (code) {
8,352✔
2245
          metaErr(TD_VID(pMeta->pVnode), code);
×
2246
          metaFetchEntryFree(&pOldEntry);
×
2247
          return code;
×
2248
        }
2249
        if (pTsdb) {
8,352✔
2250
          TAOS_CHECK_RETURN(tsdbCacheDropSTableColumn(pTsdb, uids, cid, hasPrimaryKey));
8,352✔
2251
        }
2252
      }
2253
    }
2254
    if (uids) taosArrayDestroy(uids);
29,232✔
2255

2256
    if (pTsdb) {
29,232✔
2257
      tsdbCacheInvalidateSchema(pTsdb, pEntry->uid, -1, pEntry->stbEntry.schemaRow.version);
29,232✔
2258
    }
2259
  }
2260
  if (updStat) {
7,099,250✔
2261
    int64_t ctbNum = 0;
5,249,238✔
2262
    int32_t ret = metaGetStbStats(pMeta->pVnode, pEntry->uid, &ctbNum, 0, 0);
5,251,585✔
2263
    if (ret < 0) {
5,245,673✔
2264
      metaError("vgId:%d, failed to get stb stats:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
×
2265
                pEntry->uid, tstrerror(ret));
2266
    }
2267
    pMeta->pVnode->config.vndStats.numOfTimeSeries += (ctbNum * deltaCol);
5,245,673✔
2268
    if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
5,249,586✔
2269
  }
2270
  metaFetchEntryFree(&pOldEntry);
7,101,334✔
2271
  return code;
7,086,864✔
2272
}
2273

2274
static int32_t metaHandleChildTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
7,276,435✔
2275
  int32_t code = TSDB_CODE_SUCCESS;
7,276,435✔
2276

2277
  SMetaEntry *pOldEntry = NULL;
7,276,435✔
2278
  SMetaEntry *pSuperEntry = NULL;
7,276,435✔
2279

2280
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
7,276,435✔
2281
  if (code) {
7,276,435✔
2282
    metaErr(TD_VID(pMeta->pVnode), code);
×
2283
    return code;
×
2284
  }
2285

2286
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
7,276,435✔
2287
  if (code) {
7,276,435✔
2288
    metaErr(TD_VID(pMeta->pVnode), code);
×
2289
    metaFetchEntryFree(&pOldEntry);
×
2290
    return code;
×
2291
  }
2292

2293
  SMetaHandleParam param = {
7,276,435✔
2294
      .pEntry = pEntry,
2295
      .pOldEntry = pOldEntry,
2296
      .pSuperEntry = pSuperEntry,
2297
  };
2298

2299
  metaWLock(pMeta);
7,276,435✔
2300
  code = metaHandleChildTableUpdateImpl(pMeta, &param);
7,276,435✔
2301
  metaULock(pMeta);
7,276,435✔
2302
  if (code) {
7,276,435✔
2303
    metaErr(TD_VID(pMeta->pVnode), code);
×
2304
    metaFetchEntryFree(&pOldEntry);
×
2305
    metaFetchEntryFree(&pSuperEntry);
×
2306
    return code;
×
2307
  }
2308

2309
  metaFetchEntryFree(&pOldEntry);
7,276,435✔
2310
  metaFetchEntryFree(&pSuperEntry);
7,276,435✔
2311
  return code;
7,276,435✔
2312
}
2313

2314
static int32_t metaHandleNormalTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
3,310,611✔
2315
  int32_t     code = TSDB_CODE_SUCCESS;
3,310,611✔
2316
  SMetaEntry *pOldEntry = NULL;
3,310,611✔
2317

2318
  // fetch old entry
2319
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
3,310,611✔
2320
  if (code) {
3,310,611✔
2321
    metaErr(TD_VID(pMeta->pVnode), code);
×
2322
    return code;
×
2323
  }
2324

2325
  // handle update
2326
  SMetaHandleParam param = {
3,310,611✔
2327
      .pEntry = pEntry,
2328
      .pOldEntry = pOldEntry,
2329
  };
2330
  metaWLock(pMeta);
3,310,611✔
2331
  code = metaHandleNormalTableUpdateImpl(pMeta, &param);
3,310,611✔
2332
  metaULock(pMeta);
3,310,611✔
2333
  if (code) {
3,310,611✔
2334
    metaErr(TD_VID(pMeta->pVnode), code);
×
2335
    metaFetchEntryFree(&pOldEntry);
×
2336
    return code;
×
2337
  }
2338

2339
  // do other stuff
2340
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) &&
3,310,611✔
2341
      pEntry->ntbEntry.schemaRow.version != pOldEntry->ntbEntry.schemaRow.version) {
23,900✔
2342
#if 0
2343
    {  // for add column
2344
      int16_t cid = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].colId;
2345
      int8_t  col_type = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].type;
2346
      int32_t ret = tsdbCacheNewNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, col_type);
2347
      if (ret < 0) {
2348
        terrno = ret;
2349
        goto _err;
2350
      }
2351
    }
2352
    {  // for drop column
2353

2354
      if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
2355
        int16_t cid = pColumn->colId;
2356

2357
        if (tsdbCacheDropNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, hasPrimayKey) != 0) {
2358
          metaError("vgId:%d, failed to drop ntable column:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name,
2359
                    entry.uid);
2360
        }
2361
        tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, entry.uid, pSchema->version);
2362
      }
2363
    }
2364
    }
2365
#endif
2366
    if (pMeta->pVnode->pTsdb) {
23,900✔
2367
      tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, pEntry->uid, pEntry->ntbEntry.schemaRow.version);
23,900✔
2368
    }
2369
  }
2370
  int32_t deltaCol = pEntry->ntbEntry.schemaRow.nCols - pOldEntry->ntbEntry.schemaRow.nCols;
3,310,611✔
2371
  pMeta->pVnode->config.vndStats.numOfNTimeSeries += deltaCol;
3,310,611✔
2372
  if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
3,310,611✔
2373
  metaFetchEntryFree(&pOldEntry);
3,310,611✔
2374
  return code;
3,310,611✔
2375
}
2376

2377
static int32_t metaHandleVirtualNormalTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
200,095✔
2378
  int32_t     code = TSDB_CODE_SUCCESS;
200,095✔
2379
  SMetaEntry *pOldEntry = NULL;
200,095✔
2380

2381
  // fetch old entry
2382
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
200,095✔
2383
  if (code) {
200,095✔
2384
    metaErr(TD_VID(pMeta->pVnode), code);
×
2385
    return code;
×
2386
  }
2387

2388
  // handle update
2389
  SMetaHandleParam param = {
200,095✔
2390
      .pEntry = pEntry,
2391
      .pOldEntry = pOldEntry,
2392
  };
2393
  metaWLock(pMeta);
200,095✔
2394
  code = metaHandleVirtualNormalTableUpdateImpl(pMeta, &param);
200,095✔
2395
  metaULock(pMeta);
200,095✔
2396
  if (code) {
200,095✔
2397
    metaErr(TD_VID(pMeta->pVnode), code);
×
2398
    metaFetchEntryFree(&pOldEntry);
×
2399
    return code;
×
2400
  }
2401

2402
  metaTimeSeriesNotifyCheck(pMeta);
200,095✔
2403
  metaFetchEntryFree(&pOldEntry);
200,095✔
2404
  return code;
200,095✔
2405
}
2406

2407
static int32_t metaHandleVirtualChildTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
77,120✔
2408
  int32_t code = TSDB_CODE_SUCCESS;
77,120✔
2409

2410
  SMetaEntry *pOldEntry = NULL;
77,120✔
2411
  SMetaEntry *pSuperEntry = NULL;
77,120✔
2412

2413
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
77,120✔
2414
  if (code) {
77,120✔
2415
    metaErr(TD_VID(pMeta->pVnode), code);
×
2416
    return code;
×
2417
  }
2418

2419
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
77,120✔
2420
  if (code) {
77,120✔
2421
    metaErr(TD_VID(pMeta->pVnode), code);
×
2422
    metaFetchEntryFree(&pOldEntry);
×
2423
    return code;
×
2424
  }
2425

2426
  SMetaHandleParam param = {
77,120✔
2427
      .pEntry = pEntry,
2428
      .pOldEntry = pOldEntry,
2429
      .pSuperEntry = pSuperEntry,
2430
  };
2431

2432
  metaWLock(pMeta);
77,120✔
2433
  code = metaHandleVirtualChildTableUpdateImpl(pMeta, &param);
77,120✔
2434
  metaULock(pMeta);
77,120✔
2435
  if (code) {
77,120✔
2436
    metaErr(TD_VID(pMeta->pVnode), code);
×
2437
    metaFetchEntryFree(&pOldEntry);
×
2438
    metaFetchEntryFree(&pSuperEntry);
×
2439
    return code;
×
2440
  }
2441

2442
  metaFetchEntryFree(&pOldEntry);
77,120✔
2443
  metaFetchEntryFree(&pSuperEntry);
77,120✔
2444
  return code;
77,120✔
2445
}
2446

2447
static int32_t metaHandleSuperTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
851,816✔
2448
  int32_t     code = TSDB_CODE_SUCCESS;
851,816✔
2449
  SArray     *childList = NULL;
851,816✔
2450
  SMetaEntry *pOldEntry = NULL;
851,816✔
2451

2452
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
851,785✔
2453
  if (code) {
849,835✔
2454
    metaErr(TD_VID(pMeta->pVnode), code);
×
2455
    return code;
×
2456
  }
2457

2458
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childList);
849,835✔
2459
  if (code) {
849,813✔
2460
    metaErr(TD_VID(pMeta->pVnode), code);
×
2461
    metaFetchEntryFree(&pOldEntry);
×
2462
    return code;
×
2463
  }
2464

2465
  if (pMeta->pVnode->pTsdb && tsdbCacheDropSubTables(pMeta->pVnode->pTsdb, childList, pEntry->uid) < 0) {
849,813✔
2466
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
2,444✔
2467
              pEntry->uid, tstrerror(terrno));
2468
  }
2469

2470
  // loop to drop all child tables
2471
  for (int32_t i = 0; i < taosArrayGetSize(childList); i++) {
1,559,870✔
2472
    SMetaEntry childEntry = {
708,054✔
2473
        .version = pEntry->version,
708,054✔
2474
        .uid = *(tb_uid_t *)taosArrayGet(childList, i),
708,054✔
2475
        .type = -TSDB_CHILD_TABLE,
2476
    };
2477

2478
    code = metaHandleChildTableDrop(pMeta, &childEntry, true);
708,054✔
2479
    if (code) {
708,054✔
2480
      metaErr(TD_VID(pMeta->pVnode), code);
19,905✔
2481
    }
2482
  }
2483

2484
  // do drop super table
2485
  SMetaHandleParam param = {
851,566✔
2486
      .pEntry = pEntry,
2487
      .pOldEntry = pOldEntry,
2488
  };
2489
  metaWLock(pMeta);
851,816✔
2490
  code = metaHandleSuperTableDropImpl(pMeta, &param);
851,566✔
2491
  metaULock(pMeta);
852,419✔
2492
  if (code) {
851,802✔
2493
    metaErr(TD_VID(pMeta->pVnode), code);
×
2494
    taosArrayDestroy(childList);
×
2495
    metaFetchEntryFree(&pOldEntry);
×
2496
    return code;
×
2497
  }
2498

2499
  // do other stuff
2500
  // metaUpdTimeSeriesNum(pMeta);
2501

2502
  // free resource and return
2503
  taosArrayDestroy(childList);
851,802✔
2504
  metaFetchEntryFree(&pOldEntry);
852,419✔
2505
  return code;
852,419✔
2506
}
2507

2508
int32_t metaHandleEntry2(SMeta *pMeta, const SMetaEntry *pEntry) {
96,608,386✔
2509
  int32_t   code = TSDB_CODE_SUCCESS;
96,608,386✔
2510
  int32_t   vgId = TD_VID(pMeta->pVnode);
96,608,386✔
2511
  SMetaInfo info = {0};
96,630,002✔
2512
  int8_t    type = pEntry->type > 0 ? pEntry->type : -pEntry->type;
96,642,297✔
2513

2514
  if (NULL == pMeta || NULL == pEntry) {
96,622,602✔
2515
    metaError("%s failed at %s:%d since invalid parameter", __func__, __FILE__, __LINE__);
33,843✔
2516
    return TSDB_CODE_INVALID_PARA;
×
2517
  }
2518

2519
  if (pEntry->type > 0) {
96,588,794✔
2520
    bool isExist = false;
94,464,925✔
2521
    if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) {
94,464,925✔
2522
      isExist = true;
17,965,101✔
2523
    }
2524

2525
    switch (type) {
94,438,081✔
2526
      case TSDB_SUPER_TABLE: {
11,684,683✔
2527
        if (isExist) {
11,684,683✔
2528
          code = metaHandleSuperTableUpdate(pMeta, pEntry);
7,099,960✔
2529
        } else {
2530
          code = metaHandleSuperTableCreate(pMeta, pEntry);
4,584,723✔
2531
        }
2532
        break;
11,686,477✔
2533
      }
2534
      case TSDB_CHILD_TABLE: {
65,598,667✔
2535
        if (isExist) {
65,598,667✔
2536
          code = metaHandleChildTableUpdate(pMeta, pEntry);
7,276,435✔
2537
        } else {
2538
          code = metaHandleChildTableCreate(pMeta, pEntry);
58,322,232✔
2539
        }
2540
        break;
65,607,692✔
2541
      }
2542
      case TSDB_NORMAL_TABLE: {
16,627,526✔
2543
        if (isExist) {
16,627,526✔
2544
          code = metaHandleNormalTableUpdate(pMeta, pEntry);
3,310,611✔
2545
        } else {
2546
          code = metaHandleNormalTableCreate(pMeta, pEntry);
13,316,915✔
2547
        }
2548
        break;
16,627,526✔
2549
      }
2550
      case TSDB_VIRTUAL_NORMAL_TABLE: {
297,532✔
2551
        if (isExist) {
297,532✔
2552
          code = metaHandleVirtualNormalTableUpdate(pMeta, pEntry);
200,095✔
2553
        } else {
2554
          code = metaHandleVirtualNormalTableCreate(pMeta, pEntry);
97,437✔
2555
        }
2556
        break;
297,532✔
2557
      }
2558
      case TSDB_VIRTUAL_CHILD_TABLE: {
229,673✔
2559
        if (isExist) {
229,673✔
2560
          code = metaHandleVirtualChildTableUpdate(pMeta, pEntry);
77,120✔
2561
        } else {
2562
          code = metaHandleVirtualChildTableCreate(pMeta, pEntry);
152,553✔
2563
        }
2564
        break;
229,673✔
2565
      }
2566
      default: {
×
2567
        code = TSDB_CODE_INVALID_PARA;
×
2568
        break;
×
2569
      }
2570
    }
2571
  } else {
2572
    switch (type) {
2,176,474✔
2573
      case TSDB_SUPER_TABLE: {
850,502✔
2574
        code = metaHandleSuperTableDrop(pMeta, pEntry);
850,502✔
2575
        break;
852,419✔
2576
      }
2577
      case TSDB_CHILD_TABLE: {
539,189✔
2578
        code = metaHandleChildTableDrop(pMeta, pEntry, false);
539,189✔
2579
        break;
539,189✔
2580
      }
2581
      case TSDB_NORMAL_TABLE: {
734,492✔
2582
        code = metaHandleNormalTableDrop(pMeta, pEntry);
734,492✔
2583
        break;
734,492✔
2584
      }
2585
      case TSDB_VIRTUAL_NORMAL_TABLE: {
26,361✔
2586
        code = metaHandleVirtualNormalTableDrop(pMeta, pEntry);
26,361✔
2587
        break;
26,361✔
2588
      }
2589
      case TSDB_VIRTUAL_CHILD_TABLE: {
25,930✔
2590
        code = metaHandleVirtualChildTableDrop(pMeta, pEntry, false);
25,930✔
2591
        break;
25,930✔
2592
      }
2593
      default: {
×
2594
        code = TSDB_CODE_INVALID_PARA;
×
2595
        break;
×
2596
      }
2597
    }
2598
  }
2599

2600
  if (TSDB_CODE_SUCCESS == code) {
96,627,291✔
2601
    pMeta->changed = true;
96,640,885✔
2602
    metaDebug("vgId:%d, index:%" PRId64 ", handle meta entry success, type:%d tb:%s uid:%" PRId64, vgId,
96,661,428✔
2603
              pEntry->version, pEntry->type, pEntry->type > 0 ? pEntry->name : "", pEntry->uid);
2604
  } else {
2605
    metaErr(vgId, code);
×
2606
  }
2607
  TAOS_RETURN(code);
96,648,643✔
2608
}
2609

2610
void metaHandleSyncEntry(SMeta *pMeta, const SMetaEntry *pEntry) {
157,171✔
2611
  int32_t code = TSDB_CODE_SUCCESS;
157,171✔
2612
  code = metaHandleEntry2(pMeta, pEntry);
157,171✔
2613
  if (code) {
157,171✔
2614
    metaErr(TD_VID(pMeta->pVnode), code);
×
2615
  }
2616
  return;
157,171✔
2617
}
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