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

taosdata / TDengine / #5058

17 May 2026 01:15AM UTC coverage: 73.387% (-0.02%) from 73.406%
#5058

push

travis-ci

web-flow
feat (TDgpt): Dynamic Model Synchronization Enhancements (#35344)

* refactor: do some internal refactor.

* fix: fix multiprocess sync issue.

* feat: add dynamic anomaly detection and forecasting services

* fix: log error message for undeploying model in exception handling

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fix: handle undeploy when model exists only on disk

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/286aafa0-c3ce-4c27-b803-2707571e9dc1

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: guard dynamic registry concurrent access

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: tighten service list locking scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: restore prophet support and update tests per review feedback

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: improve test name and move copy inside lock scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* Potential fix for pull request finding

Co-au... (continued)

281656 of 383795 relevant lines covered (73.39%)

135114337.11 hits per line

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

76.79
/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) {
233,314,604✔
67
  int32_t code = TSDB_CODE_SUCCESS;
233,314,604✔
68
  void   *value = NULL;
233,314,604✔
69
  int32_t valueSize = 0;
233,337,978✔
70

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

78
  // search entry table
79
  STbDbKey key = {
233,375,704✔
80
      .version = ((SUidIdxVal *)value)->version,
233,380,395✔
81
      .uid = uid,
82
  };
83
  tdbFreeClear(value);
233,372,124✔
84

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

96
  tDecoderInit(&decoder, value, valueSize);
233,386,046✔
97
  code = metaDecodeEntry(&decoder, &entry);
233,394,284✔
98
  if (code) {
233,343,645✔
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);
233,343,645✔
107
  if (code) {
233,364,444✔
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);
233,364,444✔
116
  tDecoderClear(&decoder);
233,365,820✔
117
  return code;
233,395,869✔
118
}
119

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

125
  code = tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &value, &valueSize);
94,691,856✔
126
  if (TSDB_CODE_SUCCESS != code) {
94,669,006✔
127
    metaError("vgId:%d, failed to get entry by name:%s since %s", TD_VID(pMeta->pVnode), name, tstrerror(code));
1,073✔
128
    return code;
1,073✔
129
  }
130
  int64_t uid = *(int64_t *)value;
94,667,933✔
131
  tdbFreeClear(value);
94,666,642✔
132

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

141
void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); }
233,448,610✔
142

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

147
  int32_t  code = TSDB_CODE_SUCCESS;
121,693,448✔
148
  int32_t  vgId = TD_VID(pMeta->pVnode);
121,693,448✔
149
  void    *value = NULL;
121,698,998✔
150
  int32_t  valueSize = 0;
121,698,998✔
151
  SEncoder encoder = {0};
121,698,998✔
152
  STbDbKey key = {
121,688,876✔
153
      .version = pEntry->version,
121,677,588✔
154
      .uid = pEntry->uid,
121,678,068✔
155
  };
156

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

164
  value = taosMemoryMalloc(valueSize);
121,581,446✔
165
  if (NULL == value) {
121,586,929✔
166
    metaErr(vgId, terrno);
×
167
    return terrno;
×
168
  }
169

170
  tEncoderInit(&encoder, value, valueSize);
121,586,929✔
171
  code = metaEncodeEntry(&encoder, pEntry);
121,665,355✔
172
  if (code) {
121,655,525✔
173
    metaErr(vgId, code);
×
174
    tEncoderClear(&encoder);
×
175
    taosMemoryFree(value);
×
176
    return code;
×
177
  }
178
  tEncoderClear(&encoder);
121,655,525✔
179

180
  // put to tdb
181
  if (META_TABLE_OP_INSERT == op) {
121,671,063✔
182
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
96,126,009✔
183
  } else if (META_TABLE_OP_UPDATA == op) {
25,545,054✔
184
    code = tdbTbUpsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
22,574,743✔
185
  } else if (META_TABLE_OP_DELETE == op) {
2,970,311✔
186
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
2,970,311✔
187
  } else {
188
    code = TSDB_CODE_INVALID_PARA;
×
189
  }
190
  if (TSDB_CODE_SUCCESS != code) {
121,659,422✔
191
    metaErr(vgId, code);
×
192
  }
193
  taosMemoryFree(value);
121,657,199✔
194
  return code;
121,688,332✔
195
}
196

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

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

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

209
// Schema Table
210
static int32_t metaSchemaTableUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
27,436,306✔
211
  int32_t  code = TSDB_CODE_SUCCESS;
27,436,306✔
212
  int32_t  vgId = TD_VID(pMeta->pVnode);
27,436,306✔
213
  SEncoder encoder = {0};
27,453,223✔
214
  void    *value = NULL;
27,454,913✔
215
  int32_t  valueSize = 0;
27,454,913✔
216
  int32_t  valueSizeExt = 0;
27,454,913✔
217
  bool     hasTypeMods = false;
27,454,913✔
218

219
  const SMetaEntry     *pEntry = pParam->pEntry;
27,454,913✔
220
  const SSchemaWrapper *pSchema = NULL;
27,416,783✔
221
  const SExtSchema     *pSchemaExt = NULL;
27,416,783✔
222
  if (pEntry->type == TSDB_SUPER_TABLE) {
27,416,783✔
223
    pSchema = &pEntry->stbEntry.schemaRow;
14,301,750✔
224
    pSchemaExt = pEntry->pExtSchemas;
14,339,755✔
225
    valueSizeExt = sizeof(SExtSchema) * pEntry->stbEntry.schemaRow.nCols;
14,326,460✔
226
    hasTypeMods = schemasHasTypeMod(pSchema->pSchema, pEntry->stbEntry.schemaRow.nCols);
14,327,498✔
227
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
13,104,371✔
228
    pSchema = &pEntry->ntbEntry.schemaRow;
13,104,371✔
229
    pSchemaExt = pEntry->pExtSchemas;
13,104,371✔
230
    valueSizeExt = sizeof(SExtSchema) * pEntry->ntbEntry.schemaRow.nCols;
13,104,371✔
231
    hasTypeMods = schemasHasTypeMod(pSchema->pSchema, pEntry->ntbEntry.schemaRow.nCols);
13,104,371✔
232
  } else {
233
    return TSDB_CODE_INVALID_PARA;
×
234
  }
235
  
236

237
  SSkmDbKey key = {
27,418,987✔
238
      .uid = pEntry->uid,
27,403,124✔
239
      .sver = pSchema->version,
27,437,975✔
240
  };
241

242
  // encode schema
243
  tEncodeSize(tEncodeSSchemaWrapper, pSchema, valueSize, code);
54,906,873✔
244
  if (TSDB_CODE_SUCCESS != code) {
27,425,441✔
245
    metaErr(vgId, code);
×
246
    return code;
×
247
  }
248

249
  value = taosMemoryMalloc(valueSize);
27,425,441✔
250
  if (NULL == value) {
27,429,976✔
251
    metaErr(vgId, terrno);
×
252
    return terrno;
×
253
  }
254

255
  tEncoderInit(&encoder, value, valueSize);
27,429,976✔
256
  code = tEncodeSSchemaWrapper(&encoder, pSchema);
27,459,091✔
257
  if (TSDB_CODE_SUCCESS != code) {
27,459,091✔
258
    metaErr(vgId, code);
×
259
    tEncoderClear(&encoder);
×
260
    taosMemoryFree(value);
×
261
    return code;
×
262
  }
263
  tEncoderClear(&encoder);
27,459,091✔
264

265
  // put to tdb
266
  if (META_TABLE_OP_INSERT == op) {
27,457,971✔
267
    code = tdbTbInsert(pMeta->pSkmDb, &key, sizeof(key), value, valueSize, pMeta->txn);
26,111✔
268
    if (code == 0 && hasTypeMods){
×
269
      code = tdbTbInsert(pMeta->pSkmExtDb, &key, sizeof(key), pSchemaExt, valueSizeExt, pMeta->txn);
×
270
    }
271
  } else if (META_TABLE_OP_UPDATA == op) {
27,431,916✔
272
    code = tdbTbUpsert(pMeta->pSkmDb, &key, sizeof(key), value, valueSize, pMeta->txn);
27,431,916✔
273
    if (code == 0 && hasTypeMods){
27,481,184✔
274
      code = tdbTbUpsert(pMeta->pSkmExtDb, &key, sizeof(key), pSchemaExt, valueSizeExt, pMeta->txn);
5,171,333✔
275
    }
276
  } else {
277
    code = TSDB_CODE_INVALID_PARA;
×
278
  }
279
  if (TSDB_CODE_SUCCESS != code) {
27,478,259✔
280
    metaErr(vgId, code);
×
281
  }
282
  taosMemoryFree(value);
27,475,259✔
283
  return code;
27,475,267✔
284
}
285

286
static int32_t metaSchemaTableInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
287
  return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
×
288
}
289

290
static int32_t metaAddOrDropTagIndexOfSuperTable(SMeta *pMeta, const SMetaHandleParam *pParam,
28,498,768✔
291
                                                 const SSchema *pOldColumn, const SSchema *pNewColumn) {
292
  int32_t code = TSDB_CODE_SUCCESS;
28,498,768✔
293

294
  const SMetaEntry *pEntry = pParam->pEntry;
28,498,768✔
295
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
28,522,207✔
296
  enum { ADD_INDEX, DROP_INDEX } action;
297

298
  if (pOldColumn && pNewColumn) {
28,516,939✔
299
    if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
27,783,664✔
300
      return TSDB_CODE_SUCCESS;
1,470,167✔
301
    } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) {
26,311,855✔
302
      action = DROP_INDEX;
30,518✔
303
    } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
26,268,036✔
304
      action = ADD_INDEX;
21,150✔
305
    } else {
306
      return TSDB_CODE_SUCCESS;
26,239,183✔
307
    }
308
  } else if (pOldColumn) {
733,275✔
309
    if (IS_IDX_ON(pOldColumn)) {
268,629✔
310
      action = DROP_INDEX;
11,997✔
311
    } else {
312
      return TSDB_CODE_SUCCESS;
251,421✔
313
    }
314
  } else {
315
    if (IS_IDX_ON(pNewColumn)) {
464,646✔
316
      action = ADD_INDEX;
×
317
    } else {
318
      return TSDB_CODE_SUCCESS;
466,791✔
319
    }
320
  }
321

322
  // fetch all child tables
323
  SArray *childTables = 0;
63,665✔
324
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childTables);
63,258✔
325
  if (code) {
63,665✔
326
    metaErr(TD_VID(pMeta->pVnode), code);
×
327
    return code;
×
328
  }
329

330
  // do drop or add index
331
  for (int32_t i = 0; i < taosArrayGetSize(childTables); i++) {
10,254,787✔
332
    int64_t uid = *(int64_t *)taosArrayGet(childTables, i);
10,191,122✔
333

334
    // fetch child entry
335
    SMetaEntry *pChildEntry = NULL;
10,191,122✔
336
    code = metaFetchEntryByUid(pMeta, uid, &pChildEntry);
10,191,122✔
337
    if (code) {
10,191,122✔
338
      metaErr(TD_VID(pMeta->pVnode), code);
×
339
      taosArrayDestroy(childTables);
×
340
      return code;
×
341
    }
342

343
    STagIdxKey *pTagIdxKey = NULL;
10,191,122✔
344
    int32_t     tagIdxKeySize = 0;
10,191,122✔
345

346
    if (action == ADD_INDEX) {
10,191,122✔
347
      code = metaFetchTagIdxKey(pMeta, pChildEntry, pNewColumn, &pTagIdxKey, &tagIdxKeySize);
6,146,101✔
348
      if (code) {
6,146,101✔
349
        metaErr(TD_VID(pMeta->pVnode), code);
×
350
        taosArrayDestroy(childTables);
×
351
        metaFetchEntryFree(&pChildEntry);
×
352
        return code;
×
353
      }
354

355
      code = tdbTbInsert(pMeta->pTagIdx, pTagIdxKey, tagIdxKeySize, NULL, 0, pMeta->txn);
6,146,101✔
356
      if (code) {
6,146,101✔
357
        metaErr(TD_VID(pMeta->pVnode), code);
×
358
        taosArrayDestroy(childTables);
×
359
        metaFetchEntryFree(&pChildEntry);
×
360
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
361
        return code;
×
362
      }
363
    } else {
364
      code = metaFetchTagIdxKey(pMeta, pChildEntry, pOldColumn, &pTagIdxKey, &tagIdxKeySize);
4,045,021✔
365
      if (code) {
4,045,021✔
366
        metaErr(TD_VID(pMeta->pVnode), code);
×
367
        taosArrayDestroy(childTables);
×
368
        metaFetchEntryFree(&pChildEntry);
×
369
        return code;
×
370
      }
371

372
      code = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, tagIdxKeySize, pMeta->txn);
4,045,021✔
373
      if (code) {
4,045,021✔
374
        metaErr(TD_VID(pMeta->pVnode), code);
×
375
        taosArrayDestroy(childTables);
×
376
        metaFetchEntryFree(&pChildEntry);
×
377
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
378
        return code;
×
379
      }
380
    }
381

382
    metaFetchTagIdxKeyFree(&pTagIdxKey);
10,191,122✔
383
    metaFetchEntryFree(&pChildEntry);
10,191,122✔
384
  }
385

386
  taosArrayDestroy(childTables);
63,665✔
387
  return code;
63,665✔
388
}
389

390
static int32_t metaAddOrDropColumnIndexOfVirtualSuperTable(SMeta *pMeta, const SMetaHandleParam *pParam,
163,791,950✔
391
                                                           const SSchema *pOldColumn, const SSchema *pNewColumn) {
392
  int32_t code = TSDB_CODE_SUCCESS;
163,791,950✔
393

394
  const SMetaEntry *pEntry = pParam->pEntry;
163,791,950✔
395
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
163,792,646✔
396
  enum { ADD_COLUMN, DROP_COLUMN } action;
397

398
  if (pOldColumn && pNewColumn) {
163,793,342✔
399
    return TSDB_CODE_SUCCESS;
163,776,458✔
400
  } else if (pOldColumn) {
16,884✔
401
    action = DROP_COLUMN;
8,362✔
402
  } else {
403
    action = ADD_COLUMN;
8,522✔
404
  }
405

406
  // fetch all child tables
407
  SArray *childTables = 0;
16,884✔
408
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childTables);
16,884✔
409
  if (code) {
16,884✔
410
    metaErr(TD_VID(pMeta->pVnode), code);
×
411
    return code;
×
412
  }
413

414
  // do drop or add index
415
  for (int32_t i = 0; i < taosArrayGetSize(childTables); i++) {
29,052✔
416
    int64_t uid = *(int64_t *)taosArrayGet(childTables, i);
12,168✔
417

418
    // fetch child entry
419
    SMetaEntry *pChildEntry = NULL;
12,168✔
420
    code = metaFetchEntryByUid(pMeta, uid, &pChildEntry);
12,168✔
421
    if (code) {
12,168✔
422
      metaErr(TD_VID(pMeta->pVnode), code);
×
423
      taosArrayDestroy(childTables);
×
424
      return code;
×
425
    }
426

427
    SMetaHandleParam param = {.pEntry = pChildEntry};
12,168✔
428

429
    if (action == ADD_COLUMN) {
12,168✔
430
      code = updataTableColRef(&pChildEntry->colRef, pNewColumn, 1, NULL);
7,123✔
431
      if (code) {
7,123✔
432
        metaErr(TD_VID(pMeta->pVnode), code);
×
433
        taosArrayDestroy(childTables);
×
434
        metaFetchEntryFree(&pChildEntry);
×
435
        return code;
×
436
      }
437

438
      code = metaEntryTableUpdate(pMeta, &param);
7,123✔
439
      if (code) {
7,123✔
440
        metaErr(TD_VID(pMeta->pVnode), code);
×
441
        taosArrayDestroy(childTables);
×
442
        metaFetchEntryFree(&pChildEntry);
×
443
        return code;
×
444
      }
445
    } else {
446
      code = updataTableColRef(&pChildEntry->colRef, pOldColumn, 0, NULL);
5,045✔
447
      if (code) {
5,045✔
448
        metaErr(TD_VID(pMeta->pVnode), code);
×
449
        taosArrayDestroy(childTables);
×
450
        metaFetchEntryFree(&pChildEntry);
×
451
        return code;
×
452
      }
453

454
      code = metaEntryTableUpdate(pMeta, &param);
5,045✔
455
      if (code) {
5,045✔
456
        metaErr(TD_VID(pMeta->pVnode), code);
×
457
        taosArrayDestroy(childTables);
×
458
        metaFetchEntryFree(&pChildEntry);
×
459
        return code;
×
460
      }
461
    }
462
    metaFetchEntryFree(&pChildEntry);
12,168✔
463
  }
464

465
  taosArrayDestroy(childTables);
16,884✔
466
  return code;
16,884✔
467
}
468

469
static int32_t metaUpdateSuperTableTagSchema(SMeta *pMeta, const SMetaHandleParam *pParam) {
1,431,766✔
470
  int32_t               code = TSDB_CODE_SUCCESS;
1,431,766✔
471
  const SMetaEntry     *pEntry = pParam->pEntry;
1,431,766✔
472
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
1,433,240✔
473
  const SSchemaWrapper *pNewTagSchema = &pEntry->stbEntry.schemaTag;
1,432,554✔
474
  const SSchemaWrapper *pOldTagSchema = &pOldEntry->stbEntry.schemaTag;
1,432,962✔
475

476
  int32_t iOld = 0, iNew = 0;
1,421,852✔
477
  for (; iOld < pOldTagSchema->nCols && iNew < pNewTagSchema->nCols;) {
29,359,749✔
478
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
27,945,304✔
479
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
27,951,737✔
480

481
    if (pOldColumn->colId == pNewColumn->colId) {
27,967,099✔
482
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
27,770,605✔
483
      if (code) {
27,758,950✔
484
        metaErr(TD_VID(pMeta->pVnode), code);
17,267✔
485
        return code;
×
486
      }
487

488
      iOld++;
27,741,683✔
489
      iNew++;
27,741,683✔
490
    } else if (pOldColumn->colId < pNewColumn->colId) {
197,688✔
491
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
196,951✔
492
      if (code) {
195,477✔
493
        metaErr(TD_VID(pMeta->pVnode), code);
×
494
        return code;
×
495
      }
496

497
      // drop old tag from meta stable tag filter cache
498
      code = metaStableTagFilterCacheDropTag(pMeta, pEntry->uid, pOldColumn->colId);
195,477✔
499
      if (code) {
196,214✔
500
        metaErr(TD_VID(pMeta->pVnode), code);
×
501
        return code;
×
502
      }
503

504
      iOld++;
196,214✔
505
    } else {
506
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, NULL, pNewColumn);
×
507
      if (code) {
×
508
        metaErr(TD_VID(pMeta->pVnode), code);
×
509
        return code;
×
510
      }
511

512
      iNew++;
×
513
    }
514
  }
515

516
  for (; iOld < pOldTagSchema->nCols; iOld++) {
1,497,037✔
517
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
66,797✔
518
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
65,782✔
519
    if (code) {
67,256✔
520
      metaErr(TD_VID(pMeta->pVnode), code);
×
521
      return code;
×
522
    }
523
    // drop old tag from meta stable tag filter cache
524
    code = metaStableTagFilterCacheDropTag(pMeta, pEntry->uid, pOldColumn->colId);
67,256✔
525
    if (code) {
68,730✔
526
      metaErr(TD_VID(pMeta->pVnode), code);
×
527
      return code;
×
528
    }
529
  }
530

531
  for (; iNew < pNewTagSchema->nCols; iNew++) {
1,891,587✔
532
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
469,092✔
533
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, NULL, pNewColumn);
467,941✔
534
    if (code) {
464,594✔
535
      metaErr(TD_VID(pMeta->pVnode), code);
×
536
      return code;
×
537
    }
538
  }
539

540
  return code;
1,415,883✔
541
}
542

543
static int32_t metaUpdateSuperTableRowSchema(SMeta *pMeta, const SMetaHandleParam *pParam) {
21,084✔
544
  int32_t               code = TSDB_CODE_SUCCESS;
21,084✔
545
  const SMetaEntry     *pEntry = pParam->pEntry;
21,084✔
546
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
21,084✔
547
  const SSchemaWrapper *pNewRowSchema = &pEntry->stbEntry.schemaRow;
21,084✔
548
  const SSchemaWrapper *pOldRowSchema = &pOldEntry->stbEntry.schemaRow;
21,084✔
549

550
  int32_t iOld = 0, iNew = 0;
21,084✔
551
  for (; iOld < pOldRowSchema->nCols && iNew < pNewRowSchema->nCols;) {
163,798,254✔
552
    SSchema *pOldColumn = pOldRowSchema->pSchema + iOld;
163,777,170✔
553
    SSchema *pNewColumn = pNewRowSchema->pSchema + iNew;
163,777,136✔
554

555
    if (pOldColumn->colId == pNewColumn->colId) {
163,778,612✔
556
      code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
163,777,162✔
557
      if (code) {
163,775,686✔
558
        metaErr(TD_VID(pMeta->pVnode), code);
1,400✔
559
        return code;
×
560
      }
561

562
      iOld++;
163,774,286✔
563
      iNew++;
163,774,286✔
564
    } else if (pOldColumn->colId < pNewColumn->colId) {
2,884✔
565
      code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, pOldColumn, NULL);
2,180✔
566
      if (code) {
2,884✔
567
        metaErr(TD_VID(pMeta->pVnode), code);
×
568
        return code;
×
569
      }
570

571
      iOld++;
2,884✔
572
    } else {
573
      code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, NULL, pNewColumn);
×
574
      if (code) {
×
575
        metaErr(TD_VID(pMeta->pVnode), code);
×
576
        return code;
×
577
      }
578

579
      iNew++;
×
580
    }
581
  }
582

583
  for (; iOld < pOldRowSchema->nCols; iOld++) {
26,562✔
584
    SSchema *pOldColumn = pOldRowSchema->pSchema + iOld;
5,478✔
585
    code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, pOldColumn, NULL);
5,478✔
586
    if (code) {
5,478✔
587
      metaErr(TD_VID(pMeta->pVnode), code);
×
588
      return code;
×
589
    }
590
  }
591

592
  for (; iNew < pNewRowSchema->nCols; iNew++) {
29,606✔
593
    SSchema *pNewColumn = pNewRowSchema->pSchema + iNew;
7,784✔
594
    code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, NULL, pNewColumn);
8,522✔
595
    if (code) {
8,522✔
596
      metaErr(TD_VID(pMeta->pVnode), code);
×
597
      return code;
×
598
    }
599
  }
600

601
  return code;
21,084✔
602
}
603

604
static int32_t metaSchemaTableUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
28,918,932✔
605
  int32_t code = TSDB_CODE_SUCCESS;
28,918,932✔
606

607
  const SMetaEntry *pEntry = pParam->pEntry;
28,918,932✔
608
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
28,950,979✔
609

610
  if (NULL == pOldEntry) {
28,936,100✔
611
    return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
15,457,820✔
612
  }
613

614
  if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
13,478,280✔
615
    // check row schema
616
    if (pOldEntry->ntbEntry.schemaRow.version != pEntry->ntbEntry.schemaRow.version) {
4,252,149✔
617
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
4,218,372✔
618
    }
619
  } else if (pEntry->type == TSDB_SUPER_TABLE) {
9,224,197✔
620
    // check row schema
621
    if (pOldEntry->stbEntry.schemaRow.version != pEntry->stbEntry.schemaRow.version) {
9,233,130✔
622
      if (TABLE_IS_VIRTUAL(pEntry->flags)) {
7,805,148✔
623
        return metaUpdateSuperTableRowSchema(pMeta, pParam);
21,084✔
624
      } else {
625
        return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
7,775,774✔
626
      }
627
    }
628

629
    // check tag schema
630
    code = metaUpdateSuperTableTagSchema(pMeta, pParam);
1,427,057✔
631
    if (code) {
1,409,522✔
632
      metaErr(TD_VID(pMeta->pVnode), code);
213✔
633
      return code;
×
634
    }
635

636
  } else {
637
    return TSDB_CODE_INVALID_PARA;
×
638
  }
639

640
  return TSDB_CODE_SUCCESS;
1,432,397✔
641
}
642

643
static int32_t metaSchemaTableDelete(SMeta *pMeta, const SMetaHandleParam *pEntry) {
×
644
  // TODO
645
  return TSDB_CODE_SUCCESS;
×
646
}
647

648
// Uid Index
649
static void metaBuildEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) {
118,665,596✔
650
  pInfo->uid = pEntry->uid;
118,665,596✔
651
  pInfo->version = pEntry->version;
118,621,814✔
652
  if (pEntry->type == TSDB_SUPER_TABLE) {
118,643,881✔
653
    pInfo->suid = pEntry->uid;
15,777,448✔
654
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
15,742,755✔
655
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
102,822,044✔
656
    pInfo->suid = pEntry->ctbEntry.suid;
89,724,307✔
657
    pInfo->skmVer = 0;
89,723,948✔
658
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
13,126,901✔
659
    pInfo->suid = 0;
13,126,901✔
660
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
13,126,901✔
661
  }
662
}
118,644,087✔
663

664
static int32_t metaUidIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
118,672,355✔
665
  int32_t code = TSDB_CODE_SUCCESS;
118,672,355✔
666
  int32_t vgId = TD_VID(pMeta->pVnode);
118,672,355✔
667

668
  const SMetaEntry *pEntry = pParam->pEntry;
118,677,848✔
669

670
  // update cache
671
  SMetaInfo info = {0};
118,683,602✔
672
  metaBuildEntryInfo(pEntry, &info);
118,679,335✔
673
  code = metaCacheUpsert(pMeta, &info);
118,588,174✔
674
  if (code) {
118,619,497✔
675
    metaErr(vgId, code);
×
676
  }
677

678
  // put to tdb
679
  SUidIdxVal value = {
118,619,595✔
680
      .suid = info.suid,
118,655,910✔
681
      .skmVer = info.skmVer,
118,551,050✔
682
      .version = pEntry->version,
118,655,910✔
683
  };
684
  if (META_TABLE_OP_INSERT == op) {
118,551,050✔
685
    code = tdbTbInsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
96,060,380✔
686
  } else if (META_TABLE_OP_UPDATA == op) {
22,490,670✔
687
    code = tdbTbUpsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
22,519,458✔
688
  }
689
  return code;
118,712,467✔
690
}
691

692
static int32_t metaUidIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
96,105,824✔
693
  return metaUidIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
96,105,824✔
694
}
695

696
static int32_t metaUidIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
22,574,841✔
697
  return metaUidIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
22,574,841✔
698
}
699

700
static int32_t metaUidIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
3,902,013✔
701
  int32_t code = 0;
3,902,013✔
702

703
  const SMetaEntry *pEntry = pParam->pOldEntry;
3,902,013✔
704

705
  // delete tdb
706
  code = tdbTbDelete(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), pMeta->txn);
3,902,013✔
707
  if (code) {
3,902,013✔
708
    metaErr(TD_VID(pMeta->pVnode), code);
×
709
  }
710

711
  // delete cache
712
  (void)metaCacheDrop(pMeta, pEntry->uid);
3,902,013✔
713
  return code;
3,902,013✔
714
}
715

716
// Name Index
717
static int32_t metaNameIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
96,111,898✔
718
  int32_t code = TSDB_CODE_SUCCESS;
96,111,898✔
719

720
  const SMetaEntry *pEntry = pParam->pEntry;
96,111,898✔
721

722
  if (META_TABLE_OP_INSERT == op) {
96,130,059✔
723
    code = tdbTbInsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
96,130,059✔
724
                       pMeta->txn);
725
  } else if (META_TABLE_OP_UPDATA == op) {
×
726
    code = tdbTbUpsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
×
727
                       pMeta->txn);
728
  } else {
729
    code = TSDB_CODE_INVALID_PARA;
×
730
  }
731
  if (code) {
96,134,948✔
732
    metaErr(TD_VID(pMeta->pVnode), code);
×
733
  }
734
  return code;
96,132,935✔
735
}
736

737
static int32_t metaNameIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
96,118,625✔
738
  int32_t code = TSDB_CODE_SUCCESS;
96,118,625✔
739
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
96,118,625✔
740
}
741

742
static int32_t metaNameIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
743
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
×
744
}
745

746
static int32_t metaNameIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
3,902,013✔
747
  int32_t code = TSDB_CODE_SUCCESS;
3,902,013✔
748

749
  const SMetaEntry *pEntry = pParam->pOldEntry;
3,902,013✔
750
  code = tdbTbDelete(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, pMeta->txn);
3,902,013✔
751
  if (code) {
3,902,013✔
752
    metaErr(TD_VID(pMeta->pVnode), code);
×
753
  }
754
  return code;
3,901,994✔
755
}
756

757
// Suid Index
758
static int32_t metaSUidIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
6,567,926✔
759
  const SMetaEntry *pEntry = pParam->pEntry;
6,567,926✔
760

761
  int32_t code = tdbTbInsert(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), NULL, 0, pMeta->txn);
6,584,718✔
762
  if (code) {
6,591,395✔
763
    metaErr(TD_VID(pMeta->pVnode), code);
×
764
  }
765
  return code;
6,584,508✔
766
}
767

768
static int32_t metaSUidIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
1,073,863✔
769
  const SMetaEntry *pEntry = pParam->pOldEntry;
1,073,863✔
770

771
  int32_t code = tdbTbDelete(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), pMeta->txn);
1,073,863✔
772
  if (code) {
1,073,882✔
773
    metaErr(TD_VID(pMeta->pVnode), code);
×
774
  }
775
  return code;
1,073,863✔
776
}
777

778
// Child Index
779
static int32_t metaChildIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
89,546,187✔
780
  int32_t code = TSDB_CODE_SUCCESS;
89,546,187✔
781

782
  const SMetaEntry *pEntry = pParam->pEntry;
89,546,187✔
783

784
  SCtbIdxKey key = {
89,547,989✔
785
      .suid = pEntry->ctbEntry.suid,
89,553,376✔
786
      .uid = pEntry->uid,
89,550,973✔
787
  };
788

789
  if (META_TABLE_OP_INSERT == op) {
89,551,923✔
790
    code = tdbTbInsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
161,330,585✔
791
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
80,664,839✔
792
  } else if (META_TABLE_OP_UPDATA == op) {
8,883,516✔
793
    code = tdbTbUpsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
17,767,664✔
794
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
8,883,516✔
795
  } else {
796
    code = TSDB_CODE_INVALID_PARA;
×
797
  }
798
  return code;
89,562,720✔
799
}
800

801
static int32_t metaChildIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
80,665,722✔
802
  return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
80,665,722✔
803
}
804

805
static int32_t metaChildIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
9,101,175✔
806
  const SMetaEntry *pEntry = pParam->pEntry;
9,101,175✔
807
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
9,101,175✔
808
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
9,100,404✔
809

810
  const STag *pNewTags = (const STag *)pEntry->ctbEntry.pTags;
9,101,175✔
811
  const STag *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags;
9,101,175✔
812
  if (pNewTags->len != pOldTags->len || memcmp(pNewTags, pOldTags, pNewTags->len)) {
9,101,175✔
813
    return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
8,883,516✔
814
  }
815
  return 0;
216,199✔
816
}
817

818
static int32_t metaChildIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
1,802,715✔
819
  const SMetaEntry *pEntry = pParam->pOldEntry;
1,802,715✔
820

821
  SCtbIdxKey key = {
1,802,715✔
822
      .suid = pEntry->ctbEntry.suid,
1,802,715✔
823
      .uid = pEntry->uid,
1,802,715✔
824
  };
825
  return tdbTbDelete(pMeta->pCtbIdx, &key, sizeof(key), pMeta->txn);
1,802,715✔
826
}
827

828
// Tag Index
829
static int32_t metaFetchTagIdxKey(SMeta *pMeta, const SMetaEntry *pEntry, const SSchema *pTagColumn,
112,183,315✔
830
                                  STagIdxKey **ppTagIdxKey, int32_t *pTagIdxKeySize) {
831
  int32_t code = TSDB_CODE_SUCCESS;
112,183,315✔
832

833
  STagIdxKey *pTagIdxKey = NULL;
112,183,315✔
834
  int32_t     nTagIdxKey;
112,189,377✔
835
  const void *pTagData = NULL;
112,197,692✔
836
  int32_t     nTagData = 0;
112,197,692✔
837

838
  STagVal tagVal = {
112,197,692✔
839
      .cid = pTagColumn->colId,
112,195,128✔
840
  };
841

842
  if (tTagGet((const STag *)pEntry->ctbEntry.pTags, &tagVal)) {
112,194,754✔
843
    if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
108,899,232✔
844
      pTagData = tagVal.pData;
12,794,440✔
845
      nTagData = (int32_t)tagVal.nData;
12,794,440✔
846
    } else {
847
      pTagData = &(tagVal.i64);
96,108,599✔
848
      nTagData = tDataTypes[pTagColumn->type].bytes;
96,108,599✔
849
    }
850
  } else {
851
    if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
3,299,164✔
852
      nTagData = tDataTypes[pTagColumn->type].bytes;
3,188,897✔
853
    }
854
  }
855

856
  code = metaCreateTagIdxKey(pEntry->ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type,
112,195,627✔
857
                             pEntry->uid, &pTagIdxKey, &nTagIdxKey);
112,180,458✔
858
  if (code) {
112,160,132✔
859
    metaErr(TD_VID(pMeta->pVnode), code);
×
860
    return code;
×
861
  }
862

863
  *ppTagIdxKey = pTagIdxKey;
112,160,132✔
864
  *pTagIdxKeySize = nTagIdxKey;
112,165,990✔
865
  return code;
112,179,034✔
866
}
867

868
static void metaFetchTagIdxKeyFree(STagIdxKey **ppTagIdxKey) {
112,198,693✔
869
  metaDestroyTagIdxKey(*ppTagIdxKey);
112,198,693✔
870
  *ppTagIdxKey = NULL;
112,203,710✔
871
}
112,203,590✔
872

873
static int32_t metaTagIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
80,664,787✔
874
  int32_t code = TSDB_CODE_SUCCESS;
80,664,787✔
875

876
  const SMetaEntry *pEntry = pParam->pEntry;
80,664,787✔
877
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
80,668,022✔
878

879
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
80,664,784✔
880
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
80,980,401✔
881
    const SSchema *pTagColumn = &pTagSchema->pSchema[0];
314,984✔
882

883
    STagVal tagVal = {
629,968✔
884
        .cid = pTagColumn->colId,
314,984✔
885
    };
886

887
    const void *pTagData = pEntry->ctbEntry.pTags;
314,984✔
888
    int32_t     nTagData = ((const STag *)pEntry->ctbEntry.pTags)->len;
314,984✔
889
    code = metaSaveJsonVarToIdx(pMeta, pEntry, pTagColumn);
314,984✔
890
    if (code) {
314,984✔
891
      metaErr(TD_VID(pMeta->pVnode), code);
×
892
    }
893
  } else {
894
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
332,969,120✔
895
      STagIdxKey    *pTagIdxKey = NULL;
252,598,516✔
896
      int32_t        nTagIdxKey;
252,592,193✔
897
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
252,610,892✔
898

899
      if (!IS_IDX_ON(pTagColumn)) {
252,612,243✔
900
        continue;
172,217,450✔
901
      }
902

903
      code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pTagIdxKey, &nTagIdxKey);
80,400,585✔
904
      if (code) {
80,365,862✔
905
        metaErr(TD_VID(pMeta->pVnode), code);
×
906
        return code;
×
907
      }
908

909
      code = tdbTbInsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn);
80,365,862✔
910
      if (code) {
80,401,977✔
911
        metaErr(TD_VID(pMeta->pVnode), code);
×
912
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
913
        return code;
×
914
      }
915
      metaFetchTagIdxKeyFree(&pTagIdxKey);
80,401,977✔
916
    }
917
  }
918
  return code;
80,675,420✔
919
}
920

921
static int32_t metaTagIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
9,101,175✔
922
  int32_t code = TSDB_CODE_SUCCESS;
9,101,175✔
923

924
  const SMetaEntry     *pEntry = pParam->pEntry;
9,101,175✔
925
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
9,101,175✔
926
  const SMetaEntry     *pSuperEntry = pParam->pSuperEntry;
9,101,175✔
927
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
9,101,175✔
928
  const STag           *pNewTags = (const STag *)pEntry->ctbEntry.pTags;
9,101,175✔
929
  const STag           *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags;
9,101,175✔
930

931
  if (pNewTags->len == pOldTags->len && !memcmp(pNewTags, pOldTags, pNewTags->len)) {
9,101,175✔
932
    return code;
216,199✔
933
  }
934

935
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
8,884,976✔
936
    code = metaDelJsonVarFromIdx(pMeta, pOldEntry, &pTagSchema->pSchema[0]);
1,108✔
937
    if (code) {
1,108✔
938
      metaErr(TD_VID(pMeta->pVnode), code);
×
939
      return code;
×
940
    }
941

942
    code = metaSaveJsonVarToIdx(pMeta, pEntry, &pTagSchema->pSchema[0]);
1,108✔
943
    if (code) {
1,108✔
944
      metaErr(TD_VID(pMeta->pVnode), code);
×
945
      return code;
×
946
    }
947
  } else {
948
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
19,420,285✔
949
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
10,536,417✔
950

951
      if (!IS_IDX_ON(pTagColumn)) {
10,537,188✔
952
        continue;
1,654,319✔
953
      }
954

955
      STagIdxKey *pOldTagIdxKey = NULL;
8,882,869✔
956
      int32_t     oldTagIdxKeySize = 0;
8,882,869✔
957
      STagIdxKey *pNewTagIdxKey = NULL;
8,882,869✔
958
      int32_t     newTagIdxKeySize = 0;
8,882,098✔
959

960
      code = metaFetchTagIdxKey(pMeta, pOldEntry, pTagColumn, &pOldTagIdxKey, &oldTagIdxKeySize);
8,882,098✔
961
      if (code) {
8,881,409✔
962
        metaErr(TD_VID(pMeta->pVnode), code);
×
963
        return code;
×
964
      }
965

966
      code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pNewTagIdxKey, &newTagIdxKeySize);
8,881,409✔
967
      if (code) {
8,882,869✔
968
        metaErr(TD_VID(pMeta->pVnode), code);
×
969
        metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
970
        return code;
×
971
      }
972

973
      if (tagIdxKeyCmpr(pOldTagIdxKey, oldTagIdxKeySize, pNewTagIdxKey, newTagIdxKeySize)) {
8,882,869✔
974
        code = tdbTbDelete(pMeta->pTagIdx, pOldTagIdxKey, oldTagIdxKeySize, pMeta->txn);
8,433,772✔
975
        if (code) {
8,434,461✔
976
          metaErr(TD_VID(pMeta->pVnode), code);
×
977
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
978
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
979
          return code;
×
980
        }
981

982
        code = tdbTbInsert(pMeta->pTagIdx, pNewTagIdxKey, newTagIdxKeySize, NULL, 0, pMeta->txn);
8,434,461✔
983
        if (code) {
8,434,461✔
984
          metaErr(TD_VID(pMeta->pVnode), code);
×
985
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
986
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
987
          return code;
×
988
        }
989
      }
990

991
      metaFetchTagIdxKeyFree(&pOldTagIdxKey);
8,882,098✔
992
      metaFetchTagIdxKeyFree(&pNewTagIdxKey);
8,882,098✔
993
    }
994
  }
995
  return code;
8,884,976✔
996
}
997

998
static int32_t metaTagIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
1,802,715✔
999
  int32_t code = TSDB_CODE_SUCCESS;
1,802,715✔
1000

1001
  const SMetaEntry     *pEntry = pParam->pEntry;
1,802,715✔
1002
  const SMetaEntry     *pChild = pParam->pOldEntry;
1,802,715✔
1003
  const SMetaEntry     *pSuper = pParam->pSuperEntry;
1,802,715✔
1004
  const SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
1,802,715✔
1005
  const SSchema        *pTagColumn = NULL;
1,802,715✔
1006
  const STag           *pTags = (const STag *)pChild->ctbEntry.pTags;
1,802,715✔
1007

1008
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
1,802,715✔
1009
    pTagColumn = &pTagSchema->pSchema[0];
130,029✔
1010
    code = metaDelJsonVarFromIdx(pMeta, pChild, pTagColumn);
130,029✔
1011
    if (code) {
130,029✔
1012
      metaErr(TD_VID(pMeta->pVnode), code);
×
1013
    }
1014
  } else {
1015
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
9,213,853✔
1016
      pTagColumn = &pTagSchema->pSchema[i];
7,541,763✔
1017
      if (!IS_IDX_ON(pTagColumn)) {
7,541,763✔
1018
        continue;
3,697,889✔
1019
      }
1020

1021
      STagIdxKey *pTagIdxKey = NULL;
3,843,874✔
1022
      int32_t     nTagIdxKey;
3,843,646✔
1023

1024
      code = metaFetchTagIdxKey(pMeta, pChild, pTagColumn, &pTagIdxKey, &nTagIdxKey);
3,843,874✔
1025
      if (code) {
3,843,874✔
1026
        metaErr(TD_VID(pMeta->pVnode), code);
×
1027
        return code;
×
1028
      }
1029

1030
      code = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
3,843,874✔
1031
      if (code) {
3,843,874✔
1032
        metaErr(TD_VID(pMeta->pVnode), code);
×
1033
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
1034
        return code;
×
1035
      }
1036
      metaFetchTagIdxKeyFree(&pTagIdxKey);
3,843,874✔
1037
    }
1038
  }
1039
  return code;
1,802,715✔
1040
}
1041

1042
// Btime Index
1043
static int32_t metaBtimeIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
92,373,910✔
1044
  int32_t code = TSDB_CODE_SUCCESS;
92,373,910✔
1045

1046
  const SMetaEntry *pEntry;
1047
  if (META_TABLE_OP_DELETE == op) {
92,373,910✔
1048
    pEntry = pParam->pOldEntry;
2,828,131✔
1049
  } else {
1050
    pEntry = pParam->pEntry;
89,545,779✔
1051
  }
1052

1053
  SBtimeIdxKey key = {
92,373,531✔
1054
      .uid = pEntry->uid,
92,371,083✔
1055
  };
1056

1057
  if (TSDB_CHILD_TABLE == pEntry->type || TSDB_VIRTUAL_CHILD_TABLE == pEntry->type) {
92,373,490✔
1058
    key.btime = pEntry->ctbEntry.btime;
82,465,212✔
1059
  } else if (TSDB_NORMAL_TABLE == pEntry->type || TSDB_VIRTUAL_NORMAL_TABLE == pEntry->type) {
9,911,401✔
1060
    key.btime = pEntry->ntbEntry.btime;
9,911,401✔
1061
  } else {
1062
    return TSDB_CODE_INVALID_PARA;
×
1063
  }
1064

1065
  if (META_TABLE_OP_INSERT == op) {
92,365,034✔
1066
    code = tdbTbInsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
89,537,499✔
1067
  } else if (META_TABLE_OP_UPDATA == op) {
2,827,535✔
1068
    code = tdbTbUpsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
×
1069
  } else if (META_TABLE_OP_DELETE == op) {
2,827,535✔
1070
    code = tdbTbDelete(pMeta->pBtimeIdx, &key, sizeof(key), pMeta->txn);
2,827,535✔
1071
  } else {
1072
    code = TSDB_CODE_INVALID_PARA;
×
1073
  }
1074
  if (code) {
92,379,569✔
1075
    metaErr(TD_VID(pMeta->pVnode), code);
×
1076
  }
1077
  return code;
92,377,691✔
1078
}
1079

1080
static int32_t metaBtimeIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
89,548,703✔
1081
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
89,548,703✔
1082
}
1083

1084
static int32_t metaBtimeIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
1085
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
×
1086
}
1087

1088
static int32_t metaBtimeIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
2,827,535✔
1089
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_DELETE);
2,827,535✔
1090
}
1091

1092
// TTL Index
1093
static int32_t metaTtlIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
88,944,450✔
1094
  const SMetaEntry *pEntry = pParam->pEntry;
88,944,450✔
1095

1096
  STtlUpdTtlCtx ctx = {
88,949,585✔
1097
      .uid = pEntry->uid,
88,955,035✔
1098
      .pTxn = pMeta->txn,
88,952,224✔
1099
  };
1100
  if (TSDB_CHILD_TABLE == pEntry->type) {
88,949,242✔
1101
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
80,274,676✔
1102
    ctx.changeTimeMs = pEntry->ctbEntry.btime;
80,282,021✔
1103
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
8,671,843✔
1104
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
8,671,843✔
1105
    ctx.changeTimeMs = pEntry->ntbEntry.btime;
8,671,843✔
1106
  } else {
1107
    return TSDB_CODE_INVALID_PARA;
×
1108
  }
1109

1110
  int32_t ret = ttlMgrInsertTtl(pMeta->pTtlMgr, &ctx);
88,944,816✔
1111
  if (ret < 0) {
88,942,357✔
1112
    metaError("vgId:%d, failed to insert ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid, tstrerror(ret));
×
1113
  }
1114
  return TSDB_CODE_SUCCESS;
88,944,265✔
1115
}
1116

1117
static int32_t metaTtlIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
88,945,454✔
1118
  return metaTtlIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
88,945,454✔
1119
}
1120

1121
static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam);
1122

1123
static int32_t metaTtlIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
12,833,538✔
1124
  int32_t code = TSDB_CODE_SUCCESS;
12,833,538✔
1125

1126
  const SMetaEntry *pEntry = pParam->pEntry;
12,833,538✔
1127
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
12,833,538✔
1128

1129
  if ((pEntry->type == TSDB_CHILD_TABLE && pOldEntry->ctbEntry.ttlDays != pEntry->ctbEntry.ttlDays) ||
12,833,538✔
1130
      (pEntry->type == TSDB_NORMAL_TABLE && pOldEntry->ntbEntry.ttlDays != pEntry->ntbEntry.ttlDays)) {
12,828,309✔
1131
    code = metaTtlIdxDelete(pMeta, pParam);
10,559✔
1132
    if (code) {
10,559✔
1133
      metaErr(TD_VID(pMeta->pVnode), code);
×
1134
    }
1135

1136
    code = metaTtlIdxInsert(pMeta, pParam);
10,559✔
1137
    if (code) {
10,559✔
1138
      metaErr(TD_VID(pMeta->pVnode), code);
×
1139
    }
1140
  }
1141

1142
  return TSDB_CODE_SUCCESS;
12,833,538✔
1143
}
1144

1145
static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
2,759,023✔
1146
  int32_t code = TSDB_CODE_SUCCESS;
2,759,023✔
1147

1148
  const SMetaEntry *pEntry = pParam->pOldEntry;
2,759,023✔
1149
  STtlDelTtlCtx     ctx = {
2,759,619✔
1150
          .uid = pEntry->uid,
2,759,619✔
1151
          .pTxn = pMeta->txn,
2,759,023✔
1152
  };
1153

1154
  if (TSDB_CHILD_TABLE == pEntry->type) {
2,759,023✔
1155
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
1,748,512✔
1156
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
1,010,511✔
1157
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
986,444✔
1158
  } else {
1159
    code = TSDB_CODE_INVALID_PARA;
24,067✔
1160
  }
1161

1162
  if (TSDB_CODE_SUCCESS == code) {
2,759,023✔
1163
    int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
2,734,956✔
1164
    if (ret < 0) {
2,734,956✔
1165
      metaError("vgId:%d, failed to delete ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid,
×
1166
                tstrerror(ret));
1167
    }
1168
  }
1169
  return code;
2,759,023✔
1170
}
1171

1172
static void metaTimeSeriesNotifyCheck(SMeta *pMeta) {
97,447,523✔
1173
#if defined(TD_ENTERPRISE)
1174
  int64_t nTimeSeries = metaGetTimeSeriesNum(pMeta, 0);
97,447,523✔
1175
  int64_t deltaTS = nTimeSeries - pMeta->pVnode->config.vndStats.numOfReportedTimeSeries;
97,411,287✔
1176
  if (deltaTS > tsTimeSeriesThreshold) {
97,420,763✔
1177
    if (0 == atomic_val_compare_exchange_8(&dmNotifyHdl.state, 1, 2)) {
57,966,649✔
1178
      if (tsem_post(&dmNotifyHdl.sem) != 0) {
57,969,175✔
1179
        metaError("vgId:%d, failed to post semaphore, errno:%d", TD_VID(pMeta->pVnode), ERRNO);
×
1180
      }
1181
    }
1182
  }
1183
#endif
1184
}
97,466,709✔
1185

1186
static int32_t (*metaTableOpFn[META_TABLE_MAX][META_TABLE_OP_MAX])(SMeta *pMeta, const SMetaHandleParam *pParam) =
1187
    {
1188
        [META_ENTRY_TABLE] =
1189
            {
1190
                [META_TABLE_OP_INSERT] = metaEntryTableInsert,
1191
                [META_TABLE_OP_UPDATA] = metaEntryTableUpdate,
1192
                [META_TABLE_OP_DELETE] = metaEntryTableDelete,
1193
            },
1194
        [META_SCHEMA_TABLE] =
1195
            {
1196
                [META_TABLE_OP_INSERT] = metaSchemaTableInsert,
1197
                [META_TABLE_OP_UPDATA] = metaSchemaTableUpdate,
1198
                [META_TABLE_OP_DELETE] = metaSchemaTableDelete,
1199
            },
1200
        [META_UID_IDX] =
1201
            {
1202
                [META_TABLE_OP_INSERT] = metaUidIdxInsert,
1203
                [META_TABLE_OP_UPDATA] = metaUidIdxUpdate,
1204
                [META_TABLE_OP_DELETE] = metaUidIdxDelete,
1205
            },
1206
        [META_NAME_IDX] =
1207
            {
1208
                [META_TABLE_OP_INSERT] = metaNameIdxInsert,
1209
                [META_TABLE_OP_UPDATA] = metaNameIdxUpdate,
1210
                [META_TABLE_OP_DELETE] = metaNameIdxDelete,
1211
            },
1212
        [META_SUID_IDX] =
1213
            {
1214
                [META_TABLE_OP_INSERT] = metaSUidIdxInsert,
1215
                [META_TABLE_OP_UPDATA] = NULL,
1216
                [META_TABLE_OP_DELETE] = metaSUidIdxDelete,
1217
            },
1218
        [META_CHILD_IDX] =
1219
            {
1220
                [META_TABLE_OP_INSERT] = metaChildIdxInsert,
1221
                [META_TABLE_OP_UPDATA] = metaChildIdxUpdate,
1222
                [META_TABLE_OP_DELETE] = metaChildIdxDelete,
1223
            },
1224
        [META_TAG_IDX] =
1225
            {
1226
                [META_TABLE_OP_INSERT] = metaTagIdxInsert,
1227
                [META_TABLE_OP_UPDATA] = metaTagIdxUpdate,
1228
                [META_TABLE_OP_DELETE] = metaTagIdxDelete,
1229
            },
1230
        [META_BTIME_IDX] =
1231
            {
1232
                [META_TABLE_OP_INSERT] = metaBtimeIdxInsert,
1233
                [META_TABLE_OP_UPDATA] = metaBtimeIdxUpdate,
1234
                [META_TABLE_OP_DELETE] = metaBtimeIdxDelete,
1235
            },
1236
        [META_TTL_IDX] =
1237
            {
1238
                [META_TABLE_OP_INSERT] = metaTtlIdxInsert,
1239
                [META_TABLE_OP_UPDATA] = metaTtlIdxUpdate,
1240
                [META_TABLE_OP_DELETE] = metaTtlIdxDelete,
1241
            },
1242
};
1243

1244
static int32_t metaHandleSuperTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
6,587,857✔
1245
  int32_t code = TSDB_CODE_SUCCESS;
6,587,857✔
1246

1247
  SMetaTableOp ops[] = {
6,587,857✔
1248
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1249
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: here should be insert
1250
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1251
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1252
      {META_SUID_IDX, META_TABLE_OP_INSERT},      //
1253
  };
1254

1255
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
39,514,778✔
1256
    SMetaTableOp          *op = &ops[i];
32,921,411✔
1257
    const SMetaHandleParam param = {
32,931,406✔
1258
        .pEntry = pEntry,
1259
    };
1260
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
32,936,418✔
1261
    if (TSDB_CODE_SUCCESS != code) {
32,911,206✔
1262
      metaErr(TD_VID(pMeta->pVnode), code);
×
1263
      return code;
×
1264
    }
1265
  }
1266

1267
  return code;
6,593,367✔
1268
}
1269
static int32_t metaHandleSuperTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
6,566,598✔
1270
  int32_t code = TSDB_CODE_SUCCESS;
6,566,598✔
1271

1272
  metaWLock(pMeta);
6,566,598✔
1273
  code = metaHandleSuperTableCreateImpl(pMeta, pEntry);
6,592,162✔
1274
  metaULock(pMeta);
6,583,383✔
1275

1276
  if (TSDB_CODE_SUCCESS == code) {
6,588,797✔
1277
    pMeta->pVnode->config.vndStats.numOfSTables++;
6,588,797✔
1278

1279
    metaInfo("vgId:%d, %s success, version:%" PRId64 " type:%d uid:%" PRId64 " name:%s", TD_VID(pMeta->pVnode),
6,593,749✔
1280
             __func__, pEntry->version, pEntry->type, pEntry->uid, pEntry->name);
1281
  } else {
1282
    metaErr(TD_VID(pMeta->pVnode), code);
×
1283
  }
1284
  return code;
6,595,918✔
1285
}
1286

1287
static int32_t metaHandleNormalTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
8,666,513✔
1288
  int32_t code = TSDB_CODE_SUCCESS;
8,666,513✔
1289

1290
  SMetaTableOp ops[] = {
8,666,513✔
1291
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1292
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: need to be insert
1293
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1294
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1295
      {META_BTIME_IDX, META_TABLE_OP_INSERT},     //
1296
      {META_TTL_IDX, META_TABLE_OP_INSERT},       //
1297
  };
1298

1299
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
60,665,591✔
1300
    SMetaTableOp *op = &ops[i];
51,999,078✔
1301

1302
    SMetaHandleParam param = {
51,999,078✔
1303
        .pEntry = pEntry,
1304
    };
1305

1306
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
51,999,078✔
1307
    if (TSDB_CODE_SUCCESS != code) {
51,999,078✔
1308
      metaErr(TD_VID(pMeta->pVnode), code);
×
1309
      return code;
×
1310
    }
1311
  }
1312

1313
  return code;
8,666,513✔
1314
}
1315
static int32_t metaHandleNormalTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
8,666,513✔
1316
  int32_t code = TSDB_CODE_SUCCESS;
8,666,513✔
1317

1318
  // update TDB
1319
  metaWLock(pMeta);
8,666,513✔
1320
  code = metaHandleNormalTableCreateImpl(pMeta, pEntry);
8,666,513✔
1321
  metaULock(pMeta);
8,666,513✔
1322

1323
  // update other stuff
1324
  if (TSDB_CODE_SUCCESS == code) {
8,666,513✔
1325
    pMeta->pVnode->config.vndStats.numOfNTables++;
8,666,513✔
1326
    pMeta->pVnode->config.vndStats.numOfNTimeSeries += pEntry->ntbEntry.schemaRow.nCols - 1;
8,666,513✔
1327

1328
    if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
8,666,513✔
1329
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, -1, &pEntry->ntbEntry.schemaRow);
17,971✔
1330
      if (rc < 0) {
17,971✔
1331
        metaError("vgId:%d, failed to create table:%s since %s", TD_VID(pMeta->pVnode), pEntry->name, tstrerror(rc));
×
1332
      }
1333
    }
1334
    metaTimeSeriesNotifyCheck(pMeta);
8,666,513✔
1335
  } else {
1336
    metaErr(TD_VID(pMeta->pVnode), code);
×
1337
  }
1338
  return code;
8,666,513✔
1339
}
1340

1341
static int32_t metaHandleChildTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry, const SMetaEntry *pSuperEntry) {
80,276,960✔
1342
  int32_t code = TSDB_CODE_SUCCESS;
80,276,960✔
1343

1344
  SMetaTableOp ops[] = {
80,276,960✔
1345
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},  //
1346
      {META_UID_IDX, META_TABLE_OP_INSERT},      //
1347
      {META_NAME_IDX, META_TABLE_OP_INSERT},     //
1348
      {META_CHILD_IDX, META_TABLE_OP_INSERT},    //
1349
      {META_TAG_IDX, META_TABLE_OP_INSERT},      //
1350
      {META_BTIME_IDX, META_TABLE_OP_INSERT},    //
1351
      {META_TTL_IDX, META_TABLE_OP_INSERT},      //
1352
  };
1353

1354
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
642,212,157✔
1355
    SMetaTableOp *op = &ops[i];
561,923,183✔
1356

1357
    SMetaHandleParam param = {
561,946,772✔
1358
        .pEntry = pEntry,
1359
        .pSuperEntry = pSuperEntry,
1360
    };
1361

1362
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
561,947,999✔
1363
    if (TSDB_CODE_SUCCESS != code) {
561,907,591✔
1364
      metaErr(TD_VID(pMeta->pVnode), code);
×
1365
      return code;
×
1366
    }
1367
  }
1368

1369
  if (TSDB_CODE_SUCCESS == code) {
80,288,974✔
1370
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
80,275,809✔
1371
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
80,252,493✔
1372
    if (ret < 0) {
80,269,860✔
1373
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1374
    }
1375

1376
    ret = metaStableTagFilterCacheUpdateUid(
80,269,860✔
1377
      pMeta, pEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_ADD_TABLE);
1378
    if (ret < 0) {
80,249,552✔
1379
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1380
    }
1381

1382
    ret = metaTbGroupCacheClear(pMeta, pSuperEntry->uid);
80,249,552✔
1383
    if (ret < 0) {
80,275,201✔
1384
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1385
    }
1386
  }
1387
  return code;
80,264,276✔
1388
}
1389

1390
static int32_t metaHandleChildTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
80,262,158✔
1391
  int32_t     code = TSDB_CODE_SUCCESS;
80,262,158✔
1392
  SMetaEntry *pSuperEntry = NULL;
80,262,158✔
1393

1394
  // get the super table entry
1395
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
80,272,506✔
1396
  if (code) {
80,276,401✔
1397
    metaErr(TD_VID(pMeta->pVnode), code);
×
1398
    return code;
×
1399
  }
1400

1401
  // update TDB
1402
  metaWLock(pMeta);
80,276,401✔
1403
  code = metaHandleChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
80,278,175✔
1404
  metaULock(pMeta);
80,264,893✔
1405

1406
  // update other stuff
1407
  if (TSDB_CODE_SUCCESS == code) {
80,277,693✔
1408
    pMeta->pVnode->config.vndStats.numOfCTables++;
80,277,693✔
1409

1410
    if (!metaTbInFilterCache(pMeta, pSuperEntry->name, 1)) {
80,277,200✔
1411
      int32_t nCols = 0;
80,281,800✔
1412
      int32_t ret = metaGetStbStats(pMeta->pVnode, pSuperEntry->uid, 0, &nCols, 0);
80,282,010✔
1413
      if (ret < 0) {
80,265,434✔
1414
        metaErr(TD_VID(pMeta->pVnode), ret);
×
1415
      }
1416
      pMeta->pVnode->config.vndStats.numOfTimeSeries += (nCols > 0 ? nCols - 1 : 0);
80,265,434✔
1417
    }
1418

1419
    if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
80,260,840✔
1420
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, pEntry->ctbEntry.suid, NULL);
9,063,382✔
1421
      if (rc < 0) {
9,063,009✔
1422
        metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
1423
                  tstrerror(rc));
1424
      }
1425
    }
1426

1427
  } else {
1428
    metaErr(TD_VID(pMeta->pVnode), code);
×
1429
  }
1430
  metaTimeSeriesNotifyCheck(pMeta);
80,253,023✔
1431
  metaFetchEntryFree(&pSuperEntry);
80,285,513✔
1432
  return code;
80,278,983✔
1433
}
1434

1435
static int32_t metaHandleVirtualNormalTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
219,472✔
1436
  int32_t code = TSDB_CODE_SUCCESS;
219,472✔
1437

1438
  SMetaTableOp ops[] = {
219,472✔
1439
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1440
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: need to be insert
1441
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1442
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1443
      {META_BTIME_IDX, META_TABLE_OP_INSERT},     //
1444
  };
1445

1446
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1,316,832✔
1447
    SMetaTableOp *op = &ops[i];
1,097,360✔
1448

1449
    SMetaHandleParam param = {
1,097,360✔
1450
        .pEntry = pEntry,
1451
    };
1452

1453
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
1,097,360✔
1454
    if (TSDB_CODE_SUCCESS != code) {
1,097,360✔
1455
      metaErr(TD_VID(pMeta->pVnode), code);
×
1456
      return code;
×
1457
    }
1458
  }
1459

1460
  return code;
219,472✔
1461
}
1462

1463
static int32_t metaHandleVirtualNormalTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
219,472✔
1464
  int32_t code = TSDB_CODE_SUCCESS;
219,472✔
1465

1466
  // update TDB
1467
  metaWLock(pMeta);
219,472✔
1468
  code = metaHandleVirtualNormalTableCreateImpl(pMeta, pEntry);
219,472✔
1469
  metaULock(pMeta);
219,472✔
1470

1471
  // update other stuff
1472
  if (TSDB_CODE_SUCCESS == code) {
219,472✔
1473
    pMeta->pVnode->config.vndStats.numOfVTables++;
219,472✔
1474
  } else {
1475
    metaErr(TD_VID(pMeta->pVnode), code);
×
1476
  }
1477
  return code;
219,472✔
1478
}
1479

1480
static int32_t metaHandleVirtualChildTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry,
390,856✔
1481
                                                     const SMetaEntry *pSuperEntry) {
1482
  int32_t code = TSDB_CODE_SUCCESS;
390,856✔
1483

1484
  SMetaTableOp ops[] = {
390,856✔
1485
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},  //
1486
      {META_UID_IDX, META_TABLE_OP_INSERT},      //
1487
      {META_NAME_IDX, META_TABLE_OP_INSERT},     //
1488
      {META_CHILD_IDX, META_TABLE_OP_INSERT},    //
1489
      {META_TAG_IDX, META_TABLE_OP_INSERT},      //
1490
      {META_BTIME_IDX, META_TABLE_OP_INSERT},    //
1491
  };
1492

1493
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
2,735,992✔
1494
    SMetaTableOp *op = &ops[i];
2,345,136✔
1495

1496
    SMetaHandleParam param = {
2,345,136✔
1497
        .pEntry = pEntry,
1498
        .pSuperEntry = pSuperEntry,
1499
    };
1500

1501
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
2,345,136✔
1502
    if (TSDB_CODE_SUCCESS != code) {
2,345,136✔
1503
      metaErr(TD_VID(pMeta->pVnode), code);
×
1504
      return code;
×
1505
    }
1506
  }
1507

1508
  if (TSDB_CODE_SUCCESS == code) {
390,856✔
1509
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
390,856✔
1510
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
390,856✔
1511
    if (ret < 0) {
390,856✔
1512
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1513
    }
1514

1515
    ret = metaStableTagFilterCacheUpdateUid(
390,856✔
1516
      pMeta, pEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_ADD_TABLE);
1517
    if (ret < 0) {
390,856✔
1518
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1519
    }
1520

1521
    ret = metaTbGroupCacheClear(pMeta, pSuperEntry->uid);
390,856✔
1522
    if (ret < 0) {
390,856✔
1523
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1524
    }
1525

1526
    ret = metaRefDbsCacheClear(pMeta, pSuperEntry->uid);
390,856✔
1527
    if (ret < 0) {
390,856✔
1528
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1529
    }
1530
  }
1531

1532
  return code;
390,856✔
1533
}
1534

1535
static int32_t metaHandleVirtualChildTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
390,856✔
1536
  int32_t     code = TSDB_CODE_SUCCESS;
390,856✔
1537
  SMetaEntry *pSuperEntry = NULL;
390,856✔
1538

1539
  // get the super table entry
1540
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
390,856✔
1541
  if (code) {
390,856✔
1542
    metaErr(TD_VID(pMeta->pVnode), code);
×
1543
    return code;
×
1544
  }
1545

1546
  // update TDB
1547
  metaWLock(pMeta);
390,856✔
1548
  code = metaHandleVirtualChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
390,856✔
1549
  metaULock(pMeta);
390,856✔
1550

1551
  // update other stuff
1552
  if (TSDB_CODE_SUCCESS == code) {
390,856✔
1553
    pMeta->pVnode->config.vndStats.numOfVCTables++;
390,856✔
1554
  } else {
1555
    metaErr(TD_VID(pMeta->pVnode), code);
×
1556
  }
1557

1558
  metaFetchEntryFree(&pSuperEntry);
390,856✔
1559
  return code;
390,856✔
1560
}
1561

1562
static int32_t metaHandleNormalTableDropImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
981,114✔
1563
  int32_t code = TSDB_CODE_SUCCESS;
981,114✔
1564

1565
  SMetaTableOp ops[] = {
981,114✔
1566
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1567
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1568
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1569
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1570
      {META_TTL_IDX, META_TABLE_OP_DELETE},      //
1571

1572
      // {META_SCHEMA_TABLE, META_TABLE_OP_DELETE},  //
1573
  };
1574

1575
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
5,886,684✔
1576
    SMetaTableOp *op = &ops[i];
4,905,570✔
1577
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
4,905,570✔
1578
    if (code) {
4,905,570✔
1579
      const SMetaEntry *pEntry = pParam->pEntry;
×
1580
      metaErr(TD_VID(pMeta->pVnode), code);
×
1581
    }
1582
  }
1583

1584
  return code;
981,114✔
1585
}
1586

1587
static int32_t metaHandleNormalTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
981,114✔
1588
  int32_t     code = TSDB_CODE_SUCCESS;
981,114✔
1589
  SMetaEntry *pOldEntry = NULL;
981,114✔
1590

1591
  // fetch the entry
1592
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
981,114✔
1593
  if (code) {
981,114✔
1594
    metaErr(TD_VID(pMeta->pVnode), code);
×
1595
    return code;
×
1596
  }
1597

1598
  SMetaHandleParam param = {
981,114✔
1599
      .pEntry = pEntry,
1600
      .pOldEntry = pOldEntry,
1601
  };
1602

1603
  // do the drop
1604
  metaWLock(pMeta);
981,114✔
1605
  code = metaHandleNormalTableDropImpl(pMeta, &param);
981,114✔
1606
  metaULock(pMeta);
981,114✔
1607
  if (code) {
981,114✔
1608
    metaErr(TD_VID(pMeta->pVnode), code);
×
1609
    metaFetchEntryFree(&pOldEntry);
×
1610
    return code;
×
1611
  }
1612

1613
  // update other stuff
1614
  pMeta->pVnode->config.vndStats.numOfNTables--;
981,114✔
1615
  pMeta->pVnode->config.vndStats.numOfNTimeSeries -= (pOldEntry->ntbEntry.schemaRow.nCols - 1);
981,114✔
1616

1617
#if 0
1618
  if (tbUids) {
1619
    if (taosArrayPush(tbUids, &uid) == NULL) {
1620
      rc = terrno;
1621
      goto _exit;
1622
    }
1623
  }
1624
#endif
1625

1626
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
981,114✔
1627
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pOldEntry->uid, 0, NULL);
×
1628
    if (ret < 0) {
×
1629
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1630
    }
1631
  }
1632

1633
  metaFetchEntryFree(&pOldEntry);
981,114✔
1634
  return code;
981,114✔
1635
}
1636

1637
static int32_t metaHandleChildTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam, bool superDropped) {
1,767,345✔
1638
  int32_t code = TSDB_CODE_SUCCESS;
1,767,345✔
1639

1640
  const SMetaEntry *pEntry = pParam->pEntry;
1,767,345✔
1641
  const SMetaEntry *pChild = pParam->pOldEntry;
1,767,345✔
1642
  const SMetaEntry *pSuper = pParam->pSuperEntry;
1,767,345✔
1643

1644
  SMetaTableOp ops[] = {
1,767,345✔
1645
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1646
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1647
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1648
      {META_CHILD_IDX, META_TABLE_OP_DELETE},    //
1649
      {META_TAG_IDX, META_TABLE_OP_DELETE},      //
1650
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1651
      {META_TTL_IDX, META_TABLE_OP_DELETE},      //
1652
  };
1653

1654
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
14,117,525✔
1655
    SMetaTableOp *op = &ops[i];
12,374,242✔
1656

1657
    if (op->table == META_ENTRY_TABLE && superDropped) {
12,374,614✔
1658
      continue;
931,702✔
1659
    }
1660

1661
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
11,442,912✔
1662
    if (code) {
11,441,353✔
1663
      metaErr(TD_VID(pMeta->pVnode), code);
24,067✔
1664
      return code;
24,067✔
1665
    }
1666
  }
1667

1668
  --pMeta->pVnode->config.vndStats.numOfCTables;
1,743,283✔
1669
  metaUpdateStbStats(pMeta, pParam->pSuperEntry->uid, -1, 0, -1);
1,743,879✔
1670
  int32_t ret = metaUidCacheClear(pMeta, pSuper->uid);
1,743,283✔
1671
  if (ret < 0) {
1,743,283✔
1672
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1673
  }
1674

1675
  ret = metaStableTagFilterCacheUpdateUid(
1,743,283✔
1676
    pMeta, pChild, pSuper, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
1677
  if (ret < 0) {
1,742,687✔
1678
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1679
  }
1680

1681
  ret = metaTbGroupCacheClear(pMeta, pSuper->uid);
1,742,687✔
1682
  if (ret < 0) {
1,743,283✔
1683
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1684
  }
1685
  return code;
1,742,687✔
1686
}
1687

1688
static int32_t metaHandleChildTableDrop(SMeta *pMeta, const SMetaEntry *pEntry, bool superDropped) {
1,767,946✔
1689
  int32_t     code = TSDB_CODE_SUCCESS;
1,767,946✔
1690
  SMetaEntry *pChild = NULL;
1,767,946✔
1691
  SMetaEntry *pSuper = NULL;
1,767,946✔
1692

1693
  // fetch old entry
1694
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pChild);
1,767,946✔
1695
  if (code) {
1,766,754✔
1696
    metaErr(TD_VID(pMeta->pVnode), code);
×
1697
    return code;
×
1698
  }
1699

1700
  // fetch super entry
1701
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
1,766,754✔
1702
  if (code) {
1,767,345✔
1703
    metaErr(TD_VID(pMeta->pVnode), code);
×
1704
    metaFetchEntryFree(&pChild);
×
1705
    return code;
×
1706
  }
1707

1708
  SMetaHandleParam param = {
1,767,345✔
1709
      .pEntry = pEntry,
1710
      .pOldEntry = pChild,
1711
      .pSuperEntry = pSuper,
1712
  };
1713

1714
  // do the drop
1715
  metaWLock(pMeta);
1,767,345✔
1716
  code = metaHandleChildTableDropImpl(pMeta, &param, superDropped);
1,767,345✔
1717
  metaULock(pMeta);
1,766,754✔
1718
  if (code) {
1,767,350✔
1719
    metaErr(TD_VID(pMeta->pVnode), code);
24,067✔
1720
    metaFetchEntryFree(&pChild);
24,067✔
1721
    metaFetchEntryFree(&pSuper);
24,067✔
1722
    return code;
24,067✔
1723
  }
1724

1725
  // do other stuff
1726
  if (!metaTbInFilterCache(pMeta, pSuper->name, 1)) {
1,743,283✔
1727
    int32_t      nCols = 0;
1,743,879✔
1728
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
1,743,879✔
1729
    if (metaGetStbStats(pMeta->pVnode, pSuper->uid, NULL, &nCols, 0) == 0) {
1,743,283✔
1730
      pStats->numOfTimeSeries -= nCols - 1;
1,743,879✔
1731
    }
1732
  }
1733

1734
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
1,743,879✔
1735
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pChild->uid, pSuper->uid, NULL);
10,017✔
1736
    if (ret < 0) {
10,017✔
1737
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1738
    }
1739
  }
1740

1741
#if 0
1742
  if (tbUids) {
1743
    if (taosArrayPush(tbUids, &uid) == NULL) {
1744
      rc = terrno;
1745
      goto _exit;
1746
    }
1747
  }
1748

1749
  if ((type == TSDB_CHILD_TABLE) && tbUid) {
1750
    *tbUid = uid;
1751
  }
1752
#endif
1753
  metaFetchEntryFree(&pChild);
1,743,283✔
1754
  metaFetchEntryFree(&pSuper);
1,742,315✔
1755
  return code;
1,743,283✔
1756
}
1757

1758
static int32_t metaHandleVirtualNormalTableDropImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
44,302✔
1759
  int32_t code = TSDB_CODE_SUCCESS;
44,302✔
1760

1761
  SMetaTableOp ops[] = {
44,302✔
1762
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1763
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1764
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1765
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1766

1767
      // {META_SCHEMA_TABLE, META_TABLE_OP_DELETE},  //
1768
  };
1769

1770
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
221,510✔
1771
    SMetaTableOp *op = &ops[i];
177,208✔
1772
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
177,208✔
1773
    if (code) {
177,208✔
1774
      const SMetaEntry *pEntry = pParam->pEntry;
×
1775
      metaErr(TD_VID(pMeta->pVnode), code);
×
1776
    }
1777
  }
1778

1779
  return code;
44,302✔
1780
}
1781

1782
static int32_t metaHandleVirtualNormalTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
44,302✔
1783
  int32_t     code = TSDB_CODE_SUCCESS;
44,302✔
1784
  SMetaEntry *pOldEntry = NULL;
44,302✔
1785

1786
  // fetch the entry
1787
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
44,302✔
1788
  if (code) {
44,302✔
1789
    metaErr(TD_VID(pMeta->pVnode), code);
×
1790
    return code;
×
1791
  }
1792

1793
  SMetaHandleParam param = {
44,302✔
1794
      .pEntry = pEntry,
1795
      .pOldEntry = pOldEntry,
1796
  };
1797

1798
  // do the drop
1799
  metaWLock(pMeta);
44,302✔
1800
  code = metaHandleVirtualNormalTableDropImpl(pMeta, &param);
44,302✔
1801
  metaULock(pMeta);
44,302✔
1802
  if (code) {
44,302✔
1803
    metaErr(TD_VID(pMeta->pVnode), code);
×
1804
    metaFetchEntryFree(&pOldEntry);
×
1805
    return code;
×
1806
  }
1807

1808
  // update other stuff
1809
  pMeta->pVnode->config.vndStats.numOfVTables--;
44,302✔
1810

1811
#if 0
1812
  if (tbUids) {
1813
    if (taosArrayPush(tbUids, &uid) == NULL) {
1814
      rc = terrno;
1815
      goto _exit;
1816
    }
1817
  }
1818
#endif
1819

1820
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) && pMeta->pVnode->pTsdb) {
44,302✔
1821
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pOldEntry->uid, 0, NULL);
×
1822
    if (ret < 0) {
×
1823
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1824
    }
1825
  }
1826

1827
  metaFetchEntryFree(&pOldEntry);
44,302✔
1828
  return code;
44,302✔
1829
}
1830

1831
static int32_t metaHandleVirtualChildTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam, bool superDropped) {
34,769✔
1832
  int32_t code = TSDB_CODE_SUCCESS;
34,769✔
1833

1834
  const SMetaEntry *pEntry = pParam->pEntry;
34,769✔
1835
  const SMetaEntry *pChild = pParam->pOldEntry;
34,769✔
1836
  const SMetaEntry *pSuper = pParam->pSuperEntry;
34,769✔
1837

1838
  SMetaTableOp ops[] = {
34,769✔
1839
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1840
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1841
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1842
      {META_CHILD_IDX, META_TABLE_OP_DELETE},    //
1843
      {META_TAG_IDX, META_TABLE_OP_DELETE},      //
1844
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1845
  };
1846

1847
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
243,383✔
1848
    SMetaTableOp *op = &ops[i];
208,614✔
1849

1850
    if (op->table == META_ENTRY_TABLE && superDropped) {
208,614✔
1851
      continue;
×
1852
    }
1853

1854
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
208,614✔
1855
    if (code) {
208,614✔
1856
      metaErr(TD_VID(pMeta->pVnode), code);
×
1857
      return code;
×
1858
    }
1859
  }
1860

1861
  --pMeta->pVnode->config.vndStats.numOfVCTables;
34,769✔
1862
  metaUpdateStbStats(pMeta, pParam->pSuperEntry->uid, -1, 0, -1);
34,769✔
1863
  int32_t ret = metaUidCacheClear(pMeta, pSuper->uid);
34,769✔
1864
  if (ret < 0) {
34,769✔
1865
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1866
  }
1867

1868
  ret = metaStableTagFilterCacheUpdateUid(
34,769✔
1869
    pMeta, pChild, pSuper, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
1870
  if (ret < 0) {
34,769✔
1871
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1872
  }
1873

1874
  ret = metaTbGroupCacheClear(pMeta, pSuper->uid);
34,769✔
1875
  if (ret < 0) {
34,769✔
1876
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1877
  }
1878

1879
  ret = metaRefDbsCacheClear(pMeta, pSuper->uid);
34,769✔
1880
  if (ret < 0) {
34,769✔
1881
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1882
  }
1883

1884
  return code;
34,769✔
1885
}
1886

1887
static int32_t metaHandleVirtualChildTableDrop(SMeta *pMeta, const SMetaEntry *pEntry, bool superDropped) {
34,769✔
1888
  int32_t     code = TSDB_CODE_SUCCESS;
34,769✔
1889
  SMetaEntry *pChild = NULL;
34,769✔
1890
  SMetaEntry *pSuper = NULL;
34,769✔
1891

1892
  // fetch old entry
1893
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pChild);
34,769✔
1894
  if (code) {
34,769✔
1895
    metaErr(TD_VID(pMeta->pVnode), code);
×
1896
    return code;
×
1897
  }
1898

1899
  // fetch super entry
1900
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
34,769✔
1901
  if (code) {
34,769✔
1902
    metaErr(TD_VID(pMeta->pVnode), code);
×
1903
    metaFetchEntryFree(&pChild);
×
1904
    return code;
×
1905
  }
1906

1907
  SMetaHandleParam param = {
34,769✔
1908
      .pEntry = pEntry,
1909
      .pOldEntry = pChild,
1910
      .pSuperEntry = pSuper,
1911
  };
1912

1913
  // do the drop
1914
  metaWLock(pMeta);
34,769✔
1915
  code = metaHandleVirtualChildTableDropImpl(pMeta, &param, superDropped);
34,769✔
1916
  metaULock(pMeta);
34,769✔
1917
  if (code) {
34,769✔
1918
    metaErr(TD_VID(pMeta->pVnode), code);
×
1919
    metaFetchEntryFree(&pChild);
×
1920
    metaFetchEntryFree(&pSuper);
×
1921
    return code;
×
1922
  }
1923

1924
  metaFetchEntryFree(&pChild);
34,769✔
1925
  metaFetchEntryFree(&pSuper);
34,769✔
1926
  return code;
34,769✔
1927
}
1928

1929
int32_t metaGetChildUidsOfSuperTable(SMeta *pMeta, tb_uid_t suid, SArray **childList) {
1,178,804✔
1930
  int32_t code = TSDB_CODE_SUCCESS;
1,178,804✔
1931
  void   *key = NULL;
1,178,804✔
1932
  int32_t keySize = 0;
1,183,157✔
1933
  int32_t c;
1,183,243✔
1934

1935
  *childList = taosArrayInit(64, sizeof(tb_uid_t));
1,183,823✔
1936
  if (*childList == NULL) {
1,182,016✔
1937
    return terrno;
×
1938
  }
1939

1940
  TBC *cursor = NULL;
1,182,349✔
1941
  code = tdbTbcOpen(pMeta->pCtbIdx, &cursor, NULL);
1,182,682✔
1942
  if (code) {
1,182,383✔
1943
    taosArrayDestroy(*childList);
×
1944
    *childList = NULL;
×
1945
    return code;
×
1946
  }
1947

1948
  int32_t rc = tdbTbcMoveTo(cursor,
1,183,087✔
1949
                            &(SCtbIdxKey){
1,182,383✔
1950
                                .suid = suid,
1951
                                .uid = INT64_MIN,
1952
                            },
1953
                            sizeof(SCtbIdxKey), &c);
1954
  if (rc < 0) {
1,184,491✔
1955
    tdbTbcClose(cursor);
×
1956
    return 0;
×
1957
  }
1958

1959
  for (;;) {
1960
    if (tdbTbcNext(cursor, &key, &keySize, NULL, NULL) < 0) {
12,728,408✔
1961
      break;
890,488✔
1962
    }
1963

1964
    if (((SCtbIdxKey *)key)->suid < suid) {
11,837,247✔
1965
      continue;
258,625✔
1966
    } else if (((SCtbIdxKey *)key)->suid > suid) {
11,579,295✔
1967
      break;
294,003✔
1968
    }
1969

1970
    if (taosArrayPush(*childList, &(((SCtbIdxKey *)key)->uid)) == NULL) {
22,570,584✔
1971
      tdbFreeClear(key);
×
1972
      tdbTbcClose(cursor);
×
1973
      taosArrayDestroy(*childList);
×
1974
      *childList = NULL;
×
1975
      return terrno;
×
1976
    }
1977
  }
1978

1979
  tdbTbcClose(cursor);
1,184,491✔
1980
  tdbFreeClear(key);
1,184,491✔
1981
  return code;
1,183,757✔
1982
}
1983

1984
static int32_t metaHandleSuperTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
1,073,882✔
1985
  int32_t           code = TSDB_CODE_SUCCESS;
1,073,882✔
1986
  const SMetaEntry *pEntry = pParam->pEntry;
1,073,882✔
1987

1988
  SMetaTableOp ops[] = {
1,073,882✔
1989
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1990
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1991
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1992
      {META_SUID_IDX, META_TABLE_OP_DELETE},     //
1993

1994
      // {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: here should be insert
1995
  };
1996

1997
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
5,367,951✔
1998
    SMetaTableOp *op = &ops[i];
4,294,069✔
1999

2000
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
4,294,107✔
2001
    if (TSDB_CODE_SUCCESS != code) {
4,294,841✔
2002
      metaErr(TD_VID(pMeta->pVnode), code);
×
2003
      return code;
×
2004
    }
2005
  }
2006

2007
  int32_t ret = metaStatsCacheDrop(pMeta, pEntry->uid);
1,073,882✔
2008
  if (ret < 0) {
1,073,863✔
2009
    metaErr(TD_VID(pMeta->pVnode), ret);
368,668✔
2010
  }
2011
  return code;
1,073,863✔
2012
}
2013

2014
static int32_t metaHandleNormalTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
3,989,446✔
2015
  int32_t code = TSDB_CODE_SUCCESS;
3,989,446✔
2016

2017
  const SMetaEntry *pEntry = pParam->pEntry;
3,989,446✔
2018

2019
  SMetaTableOp ops[] = {
3,989,446✔
2020
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
2021
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
2022
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
2023
      {META_TTL_IDX, META_TABLE_OP_UPDATA},       //
2024
  };
2025
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
19,947,230✔
2026
    SMetaTableOp *op = &ops[i];
15,957,784✔
2027
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
15,957,784✔
2028
    if (code) {
15,957,784✔
2029
      metaErr(TD_VID(pMeta->pVnode), code);
×
2030
      return code;
×
2031
    }
2032
  }
2033
#if 0
2034
  if (metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs) < 0) {
2035
    metaError("vgId:%d, failed to update change time:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
2036
  }
2037
#endif
2038
  return code;
3,989,446✔
2039
}
2040

2041
static int32_t metaHandleVirtualNormalTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
251,470✔
2042
  int32_t code = TSDB_CODE_SUCCESS;
251,470✔
2043

2044
  const SMetaEntry *pEntry = pParam->pEntry;
251,470✔
2045

2046
  SMetaTableOp ops[] = {
251,470✔
2047
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
2048
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
2049
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
2050
  };
2051
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1,005,880✔
2052
    SMetaTableOp *op = &ops[i];
754,410✔
2053
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
754,410✔
2054
    if (code) {
754,410✔
2055
      metaErr(TD_VID(pMeta->pVnode), code);
×
2056
      return code;
×
2057
    }
2058
  }
2059
#if 0
2060
  if (metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs) < 0) {
2061
    metaError("vgId:%d, failed to update change time:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
2062
  }
2063
#endif
2064
  return code;
251,470✔
2065
}
2066

2067
static int32_t metaHandleVirtualChildTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
256,394✔
2068
  int32_t code = TSDB_CODE_SUCCESS;
256,394✔
2069

2070
  const SMetaEntry *pEntry = pParam->pEntry;
256,394✔
2071
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
257,083✔
2072
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
257,083✔
2073

2074
  SMetaTableOp ops[] = {
257,083✔
2075
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},  //
2076
      {META_UID_IDX, META_TABLE_OP_UPDATA},      //
2077
      {META_TAG_IDX, META_TABLE_OP_UPDATA},      //
2078
      {META_CHILD_IDX, META_TABLE_OP_UPDATA},    //
2079
  };
2080

2081
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1,284,726✔
2082
    SMetaTableOp *op = &ops[i];
1,027,643✔
2083
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
1,026,954✔
2084
    if (code) {
1,027,643✔
2085
      metaErr(TD_VID(pMeta->pVnode), code);
×
2086
      return code;
×
2087
    }
2088
  }
2089

2090
  if (metaUidCacheClear(pMeta, pSuperEntry->uid) < 0) {
257,083✔
2091
    metaErr(TD_VID(pMeta->pVnode), code);
×
2092
  }
2093

2094
  // update stable tag filter cache: drop old then add new
2095
  code = metaStableTagFilterCacheUpdateUid(
257,083✔
2096
    pMeta, pOldEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
2097
  if (TSDB_CODE_SUCCESS != code) {
257,083✔
2098
    metaErr(TD_VID(pMeta->pVnode), code);
×
2099
  }
2100
  code = metaStableTagFilterCacheUpdateUid(
257,083✔
2101
    pMeta, pEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_ADD_TABLE);
2102
  if (TSDB_CODE_SUCCESS != code) {
257,083✔
2103
    metaErr(TD_VID(pMeta->pVnode), code);
×
2104
  }
2105

2106
  if (metaTbGroupCacheClear(pMeta, pSuperEntry->uid) < 0) {
257,083✔
2107
    metaErr(TD_VID(pMeta->pVnode), code);
×
2108
  }
2109

2110
  if (metaRefDbsCacheClear(pMeta, pSuperEntry->uid) < 0) {
257,083✔
2111
    metaErr(TD_VID(pMeta->pVnode), code);
×
2112
  }
2113
  return code;
257,083✔
2114
}
2115

2116
static int32_t metaHandleChildTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
8,844,092✔
2117
  int32_t code = TSDB_CODE_SUCCESS;
8,844,092✔
2118

2119
  const SMetaEntry *pEntry = pParam->pEntry;
8,844,092✔
2120
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
8,844,092✔
2121
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
8,844,092✔
2122

2123
  SMetaTableOp ops[] = {
8,843,321✔
2124
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},  //
2125
      {META_UID_IDX, META_TABLE_OP_UPDATA},      //
2126
      {META_TAG_IDX, META_TABLE_OP_UPDATA},      //
2127
      {META_CHILD_IDX, META_TABLE_OP_UPDATA},    //
2128
      {META_TTL_IDX, META_TABLE_OP_UPDATA},      //
2129
  };
2130

2131
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
53,063,010✔
2132
    SMetaTableOp *op = &ops[i];
44,219,689✔
2133
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
44,219,689✔
2134
    if (code) {
44,218,918✔
2135
      metaErr(TD_VID(pMeta->pVnode), code);
×
2136
      return code;
×
2137
    }
2138
  }
2139

2140
  if (metaUidCacheClear(pMeta, pSuperEntry->uid) < 0) {
8,843,321✔
2141
    metaErr(TD_VID(pMeta->pVnode), code);
×
2142
  }
2143

2144
  // update stable tag filter cache: drop old then add new
2145
  code = metaStableTagFilterCacheUpdateUid(
8,844,092✔
2146
    pMeta, pOldEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
2147
  if (TSDB_CODE_SUCCESS != code) {
8,844,092✔
2148
    metaErr(TD_VID(pMeta->pVnode), code);
×
2149
  }
2150
  code = metaStableTagFilterCacheUpdateUid(
8,844,092✔
2151
    pMeta, pEntry, pSuperEntry, STABLE_TAG_FILTER_CACHE_ADD_TABLE);
2152
  if (TSDB_CODE_SUCCESS != code) {
8,844,092✔
2153
    metaErr(TD_VID(pMeta->pVnode), code);
×
2154
  }
2155

2156
  if (metaTbGroupCacheClear(pMeta, pSuperEntry->uid) < 0) {
8,844,092✔
2157
    metaErr(TD_VID(pMeta->pVnode), code);
×
2158
  }
2159
  return code;
8,843,321✔
2160
#if 0
2161
  if (metaUpdateChangeTime(pMeta, ctbEntry.uid, pReq->ctimeMs) < 0) {
2162
    metaError("meta/table: failed to update change time:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
2163
  }
2164
#endif
2165
}
2166

2167
static int32_t metaHandleSuperTableUpdateImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
9,238,112✔
2168
  int32_t code = TSDB_CODE_SUCCESS;
9,238,112✔
2169

2170
  const SMetaEntry *pEntry = pParam->pEntry;
9,238,112✔
2171
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
9,240,530✔
2172

2173
  SMetaTableOp ops[] = {
9,239,179✔
2174
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
2175
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
2176
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
2177
  };
2178

2179
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
36,930,506✔
2180
    SMetaTableOp *op = &ops[i];
27,714,628✔
2181
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
27,722,967✔
2182
    if (code) {
27,689,890✔
2183
      metaErr(TD_VID(pMeta->pVnode), code);
×
2184
      return code;
×
2185
    }
2186
  }
2187

2188
  if (TSDB_CODE_SUCCESS == code) {
9,215,878✔
2189
    metaUpdateStbStats(pMeta, pEntry->uid, 0, pEntry->stbEntry.schemaRow.nCols - pOldEntry->stbEntry.schemaRow.nCols,
9,229,083✔
2190
                       pEntry->stbEntry.keep);
9,216,408✔
2191
  }
2192

2193
  return code;
9,219,026✔
2194
}
2195

2196
static int32_t metaHandleSuperTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
9,235,696✔
2197
  int32_t code = TSDB_CODE_SUCCESS;
9,235,696✔
2198

2199
  SMetaEntry *pOldEntry = NULL;
9,235,696✔
2200

2201
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
9,238,770✔
2202
  if (code) {
9,236,504✔
2203
    metaErr(TD_VID(pMeta->pVnode), code);
×
2204
    return code;
×
2205
  }
2206

2207
  SMetaHandleParam param = {
9,236,504✔
2208
      .pEntry = pEntry,
2209
      .pOldEntry = pOldEntry,
2210
  };
2211
  metaWLock(pMeta);
9,239,793✔
2212
  code = metaHandleSuperTableUpdateImpl(pMeta, &param);
9,241,060✔
2213
  metaULock(pMeta);
9,219,884✔
2214
  if (code) {
9,217,387✔
2215
    metaErr(TD_VID(pMeta->pVnode), code);
×
2216
    metaFetchEntryFree(&pOldEntry);
×
2217
    return code;
×
2218
  }
2219

2220
  int     nCols = pEntry->stbEntry.schemaRow.nCols;
9,217,387✔
2221
  int     onCols = pOldEntry->stbEntry.schemaRow.nCols;
9,232,408✔
2222
  int32_t deltaCol = nCols - onCols;
9,226,794✔
2223
  bool    updStat = deltaCol != 0 && !TABLE_IS_VIRTUAL(pEntry->flags) && !metaTbInFilterCache(pMeta, pEntry->name, 1);
9,226,794✔
2224

2225
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
9,214,286✔
2226
    STsdb  *pTsdb = pMeta->pVnode->pTsdb;
35,070✔
2227
    SArray *uids = NULL; /*taosArrayInit(8, sizeof(int64_t));
35,070✔
2228
     if (uids == NULL) {
2229
       metaErr(TD_VID(pMeta->pVnode), code);
2230
       metaFetchEntryFree(&pOldEntry);
2231
       return terrno;
2232
       }*/
2233
    if (deltaCol == 1) {
35,070✔
2234
      int16_t cid = pEntry->stbEntry.schemaRow.pSchema[nCols - 1].colId;
20,040✔
2235
      int8_t  col_type = pEntry->stbEntry.schemaRow.pSchema[nCols - 1].type;
20,040✔
2236

2237
      code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &uids);
20,040✔
2238
      if (code) {
20,040✔
2239
        metaErr(TD_VID(pMeta->pVnode), code);
×
2240
        metaFetchEntryFree(&pOldEntry);
×
2241
        return code;
×
2242
      }
2243
      if (pTsdb) {
20,040✔
2244
        TAOS_CHECK_RETURN(tsdbCacheNewSTableColumn(pTsdb, uids, cid, col_type));
20,040✔
2245
      }
2246
    } else if (deltaCol == -1) {
15,030✔
2247
      int16_t cid = -1;
15,030✔
2248
      bool    hasPrimaryKey = false;
15,030✔
2249
      if (onCols >= 2) {
15,030✔
2250
        hasPrimaryKey = (pOldEntry->stbEntry.schemaRow.pSchema[1].flags & COL_IS_KEY) ? true : false;
15,030✔
2251
      }
2252
      for (int i = 0, j = 0; i < nCols && j < onCols; ++i, ++j) {
145,290✔
2253
        if (pEntry->stbEntry.schemaRow.pSchema[i].colId != pOldEntry->stbEntry.schemaRow.pSchema[j].colId) {
140,280✔
2254
          cid = pOldEntry->stbEntry.schemaRow.pSchema[j].colId;
10,020✔
2255
          break;
10,020✔
2256
        }
2257
      }
2258

2259
      if (cid != -1) {
15,030✔
2260
        code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &uids);
10,020✔
2261
        if (code) {
10,020✔
2262
          metaErr(TD_VID(pMeta->pVnode), code);
×
2263
          metaFetchEntryFree(&pOldEntry);
×
2264
          return code;
×
2265
        }
2266
        if (pTsdb) {
10,020✔
2267
          TAOS_CHECK_RETURN(tsdbCacheDropSTableColumn(pTsdb, uids, cid, hasPrimaryKey));
10,020✔
2268
        }
2269
      }
2270
    }
2271
    if (uids) taosArrayDestroy(uids);
35,070✔
2272

2273
    if (pTsdb) {
35,070✔
2274
      tsdbCacheInvalidateSchema(pTsdb, pEntry->uid, -1, pEntry->stbEntry.schemaRow.version);
35,070✔
2275
    }
2276
  }
2277
  if (updStat) {
9,224,373✔
2278
    int64_t ctbNum = 0;
6,775,129✔
2279
    int32_t ret = metaGetStbStats(pMeta->pVnode, pEntry->uid, &ctbNum, 0, 0);
6,774,389✔
2280
    if (ret < 0) {
6,775,332✔
2281
      metaError("vgId:%d, failed to get stb stats:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
×
2282
                pEntry->uid, tstrerror(ret));
2283
    }
2284
    pMeta->pVnode->config.vndStats.numOfTimeSeries += (ctbNum * deltaCol);
6,775,332✔
2285
    if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
6,773,098✔
2286
  }
2287
  metaFetchEntryFree(&pOldEntry);
9,226,105✔
2288
  return code;
9,194,924✔
2289
}
2290

2291
static int32_t metaHandleChildTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
8,844,092✔
2292
  int32_t code = TSDB_CODE_SUCCESS;
8,844,092✔
2293

2294
  SMetaEntry *pOldEntry = NULL;
8,844,092✔
2295
  SMetaEntry *pSuperEntry = NULL;
8,844,092✔
2296

2297
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
8,844,092✔
2298
  if (code) {
8,844,092✔
2299
    metaErr(TD_VID(pMeta->pVnode), code);
×
2300
    return code;
×
2301
  }
2302

2303
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
8,844,092✔
2304
  if (code) {
8,844,092✔
2305
    metaErr(TD_VID(pMeta->pVnode), code);
×
2306
    metaFetchEntryFree(&pOldEntry);
×
2307
    return code;
×
2308
  }
2309

2310
  SMetaHandleParam param = {
8,844,092✔
2311
      .pEntry = pEntry,
2312
      .pOldEntry = pOldEntry,
2313
      .pSuperEntry = pSuperEntry,
2314
  };
2315

2316
  metaWLock(pMeta);
8,844,092✔
2317
  code = metaHandleChildTableUpdateImpl(pMeta, &param);
8,843,321✔
2318
  metaULock(pMeta);
8,844,092✔
2319
  if (code) {
8,843,321✔
2320
    metaErr(TD_VID(pMeta->pVnode), code);
×
2321
    metaFetchEntryFree(&pOldEntry);
×
2322
    metaFetchEntryFree(&pSuperEntry);
×
2323
    return code;
×
2324
  }
2325

2326
  metaFetchEntryFree(&pOldEntry);
8,843,321✔
2327
  metaFetchEntryFree(&pSuperEntry);
8,844,092✔
2328
  return code;
8,842,914✔
2329
}
2330

2331
static int32_t metaHandleNormalTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
3,989,446✔
2332
  int32_t     code = TSDB_CODE_SUCCESS;
3,989,446✔
2333
  SMetaEntry *pOldEntry = NULL;
3,989,446✔
2334

2335
  // fetch old entry
2336
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
3,989,446✔
2337
  if (code) {
3,989,446✔
2338
    metaErr(TD_VID(pMeta->pVnode), code);
×
2339
    return code;
×
2340
  }
2341

2342
  // handle update
2343
  SMetaHandleParam param = {
3,989,446✔
2344
      .pEntry = pEntry,
2345
      .pOldEntry = pOldEntry,
2346
  };
2347
  metaWLock(pMeta);
3,989,446✔
2348
  code = metaHandleNormalTableUpdateImpl(pMeta, &param);
3,989,446✔
2349
  metaULock(pMeta);
3,989,446✔
2350
  if (code) {
3,989,446✔
2351
    metaErr(TD_VID(pMeta->pVnode), code);
×
2352
    metaFetchEntryFree(&pOldEntry);
×
2353
    return code;
×
2354
  }
2355

2356
  // do other stuff
2357
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) &&
3,989,446✔
2358
      pEntry->ntbEntry.schemaRow.version != pOldEntry->ntbEntry.schemaRow.version) {
28,781✔
2359
#if 0
2360
    {  // for add column
2361
      int16_t cid = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].colId;
2362
      int8_t  col_type = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].type;
2363
      int32_t ret = tsdbCacheNewNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, col_type);
2364
      if (ret < 0) {
2365
        terrno = ret;
2366
        goto _err;
2367
      }
2368
    }
2369
    {  // for drop column
2370

2371
      if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
2372
        int16_t cid = pColumn->colId;
2373

2374
        if (tsdbCacheDropNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, hasPrimayKey) != 0) {
2375
          metaError("vgId:%d, failed to drop ntable column:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name,
2376
                    entry.uid);
2377
        }
2378
        tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, entry.uid, pSchema->version);
2379
      }
2380
    }
2381
    }
2382
#endif
2383
    if (pMeta->pVnode->pTsdb) {
28,781✔
2384
      tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, pEntry->uid, pEntry->ntbEntry.schemaRow.version);
28,781✔
2385
    }
2386
  }
2387
  int32_t deltaCol = pEntry->ntbEntry.schemaRow.nCols - pOldEntry->ntbEntry.schemaRow.nCols;
3,989,446✔
2388
  pMeta->pVnode->config.vndStats.numOfNTimeSeries += deltaCol;
3,989,446✔
2389
  if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
3,989,446✔
2390
  metaFetchEntryFree(&pOldEntry);
3,989,446✔
2391
  return code;
3,989,446✔
2392
}
2393

2394
static int32_t metaHandleVirtualNormalTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
251,470✔
2395
  int32_t     code = TSDB_CODE_SUCCESS;
251,470✔
2396
  SMetaEntry *pOldEntry = NULL;
251,470✔
2397

2398
  // fetch old entry
2399
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
251,470✔
2400
  if (code) {
251,470✔
2401
    metaErr(TD_VID(pMeta->pVnode), code);
×
2402
    return code;
×
2403
  }
2404

2405
  // handle update
2406
  SMetaHandleParam param = {
251,470✔
2407
      .pEntry = pEntry,
2408
      .pOldEntry = pOldEntry,
2409
  };
2410
  metaWLock(pMeta);
251,470✔
2411
  code = metaHandleVirtualNormalTableUpdateImpl(pMeta, &param);
251,470✔
2412
  metaULock(pMeta);
251,470✔
2413
  if (code) {
251,470✔
2414
    metaErr(TD_VID(pMeta->pVnode), code);
×
2415
    metaFetchEntryFree(&pOldEntry);
×
2416
    return code;
×
2417
  }
2418

2419
  metaTimeSeriesNotifyCheck(pMeta);
251,470✔
2420
  metaFetchEntryFree(&pOldEntry);
251,470✔
2421
  return code;
251,470✔
2422
}
2423

2424
static int32_t metaHandleVirtualChildTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
257,083✔
2425
  int32_t code = TSDB_CODE_SUCCESS;
257,083✔
2426

2427
  SMetaEntry *pOldEntry = NULL;
257,083✔
2428
  SMetaEntry *pSuperEntry = NULL;
257,083✔
2429

2430
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
257,083✔
2431
  if (code) {
257,083✔
2432
    metaErr(TD_VID(pMeta->pVnode), code);
×
2433
    return code;
×
2434
  }
2435

2436
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
257,083✔
2437
  if (code) {
257,083✔
2438
    metaErr(TD_VID(pMeta->pVnode), code);
×
2439
    metaFetchEntryFree(&pOldEntry);
×
2440
    return code;
×
2441
  }
2442

2443
  SMetaHandleParam param = {
257,083✔
2444
      .pEntry = pEntry,
2445
      .pOldEntry = pOldEntry,
2446
      .pSuperEntry = pSuperEntry,
2447
  };
2448

2449
  metaWLock(pMeta);
257,083✔
2450
  code = metaHandleVirtualChildTableUpdateImpl(pMeta, &param);
256,394✔
2451
  metaULock(pMeta);
257,083✔
2452
  if (code) {
257,083✔
2453
    metaErr(TD_VID(pMeta->pVnode), code);
×
2454
    metaFetchEntryFree(&pOldEntry);
×
2455
    metaFetchEntryFree(&pSuperEntry);
×
2456
    return code;
×
2457
  }
2458

2459
  metaFetchEntryFree(&pOldEntry);
257,083✔
2460
  metaFetchEntryFree(&pSuperEntry);
257,083✔
2461
  return code;
257,083✔
2462
}
2463

2464
static int32_t metaHandleSuperTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
1,071,876✔
2465
  int32_t     code = TSDB_CODE_SUCCESS;
1,071,876✔
2466
  SArray     *childList = NULL;
1,071,876✔
2467
  SMetaEntry *pOldEntry = NULL;
1,071,876✔
2468

2469
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
1,071,876✔
2470
  if (code) {
1,071,880✔
2471
    metaErr(TD_VID(pMeta->pVnode), code);
×
2472
    return code;
×
2473
  }
2474

2475
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childList);
1,071,880✔
2476
  if (code) {
1,073,882✔
2477
    metaErr(TD_VID(pMeta->pVnode), code);
×
2478
    metaFetchEntryFree(&pOldEntry);
×
2479
    return code;
×
2480
  }
2481

2482
  if (pMeta->pVnode->pTsdb && tsdbCacheDropSubTables(pMeta->pVnode->pTsdb, childList, pEntry->uid) < 0) {
1,073,882✔
2483
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
3,066✔
2484
              pEntry->uid, tstrerror(terrno));
2485
  }
2486

2487
  // loop to drop all child tables
2488
  for (int32_t i = 0; i < taosArrayGetSize(childList); i++) {
2,003,722✔
2489
    SMetaEntry childEntry = {
931,816✔
2490
        .version = pEntry->version,
931,702✔
2491
        .uid = *(tb_uid_t *)taosArrayGet(childList, i),
931,702✔
2492
        .type = -TSDB_CHILD_TABLE,
2493
    };
2494

2495
    code = metaHandleChildTableDrop(pMeta, &childEntry, true);
931,702✔
2496
    if (code) {
930,510✔
2497
      metaErr(TD_VID(pMeta->pVnode), code);
24,067✔
2498
    }
2499
  }
2500

2501
  // do drop super table
2502
  SMetaHandleParam param = {
1,073,882✔
2503
      .pEntry = pEntry,
2504
      .pOldEntry = pOldEntry,
2505
  };
2506
  metaWLock(pMeta);
1,073,882✔
2507
  code = metaHandleSuperTableDropImpl(pMeta, &param);
1,073,882✔
2508
  metaULock(pMeta);
1,073,882✔
2509
  if (code) {
1,073,863✔
2510
    metaErr(TD_VID(pMeta->pVnode), code);
×
2511
    taosArrayDestroy(childList);
×
2512
    metaFetchEntryFree(&pOldEntry);
×
2513
    return code;
×
2514
  }
2515

2516
  // do other stuff
2517
  // metaUpdTimeSeriesNum(pMeta);
2518

2519
  // free resource and return
2520
  taosArrayDestroy(childList);
1,073,863✔
2521
  metaFetchEntryFree(&pOldEntry);
1,073,882✔
2522
  return code;
1,073,882✔
2523
}
2524

2525
int32_t metaHandleEntry2(SMeta *pMeta, const SMetaEntry *pEntry) {
121,660,747✔
2526
  int32_t   code = TSDB_CODE_SUCCESS;
121,660,747✔
2527
  int32_t   vgId = TD_VID(pMeta->pVnode);
121,660,747✔
2528
  SMetaInfo info = {0};
121,659,125✔
2529
  int8_t    type = pEntry->type > 0 ? pEntry->type : -pEntry->type;
121,669,439✔
2530

2531
  if (NULL == pMeta || NULL == pEntry) {
121,672,302✔
2532
    metaError("%s failed at %s:%d since invalid parameter", __func__, __FILE__, __LINE__);
22,631✔
2533
    return TSDB_CODE_INVALID_PARA;
×
2534
  }
2535

2536
  if (pEntry->type > 0) {
121,649,671✔
2537
    bool isExist = false;
118,694,029✔
2538
    if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) {
118,694,029✔
2539
      isExist = true;
22,583,288✔
2540
    }
2541

2542
    switch (type) {
118,697,835✔
2543
      case TSDB_SUPER_TABLE: {
15,813,032✔
2544
        if (isExist) {
15,813,032✔
2545
          code = metaHandleSuperTableUpdate(pMeta, pEntry);
9,234,197✔
2546
        } else {
2547
          code = metaHandleSuperTableCreate(pMeta, pEntry);
6,578,835✔
2548
        }
2549
        break;
15,812,620✔
2550
      }
2551
      case TSDB_CHILD_TABLE: {
89,109,963✔
2552
        if (isExist) {
89,109,963✔
2553
          code = metaHandleChildTableUpdate(pMeta, pEntry);
8,843,321✔
2554
        } else {
2555
          code = metaHandleChildTableCreate(pMeta, pEntry);
80,266,642✔
2556
        }
2557
        break;
89,126,017✔
2558
      }
2559
      case TSDB_NORMAL_TABLE: {
12,655,959✔
2560
        if (isExist) {
12,655,959✔
2561
          code = metaHandleNormalTableUpdate(pMeta, pEntry);
3,989,446✔
2562
        } else {
2563
          code = metaHandleNormalTableCreate(pMeta, pEntry);
8,666,513✔
2564
        }
2565
        break;
12,655,959✔
2566
      }
2567
      case TSDB_VIRTUAL_NORMAL_TABLE: {
470,942✔
2568
        if (isExist) {
470,942✔
2569
          code = metaHandleVirtualNormalTableUpdate(pMeta, pEntry);
251,470✔
2570
        } else {
2571
          code = metaHandleVirtualNormalTableCreate(pMeta, pEntry);
219,472✔
2572
        }
2573
        break;
470,942✔
2574
      }
2575
      case TSDB_VIRTUAL_CHILD_TABLE: {
647,939✔
2576
        if (isExist) {
647,939✔
2577
          code = metaHandleVirtualChildTableUpdate(pMeta, pEntry);
257,083✔
2578
        } else {
2579
          code = metaHandleVirtualChildTableCreate(pMeta, pEntry);
390,856✔
2580
        }
2581
        break;
647,939✔
2582
      }
2583
      default: {
×
2584
        code = TSDB_CODE_INVALID_PARA;
×
2585
        break;
×
2586
      }
2587
    }
2588
  } else {
2589
    switch (type) {
2,967,632✔
2590
      case TSDB_SUPER_TABLE: {
1,071,203✔
2591
        code = metaHandleSuperTableDrop(pMeta, pEntry);
1,071,203✔
2592
        break;
1,073,882✔
2593
      }
2594
      case TSDB_CHILD_TABLE: {
836,244✔
2595
        code = metaHandleChildTableDrop(pMeta, pEntry, false);
836,244✔
2596
        break;
836,244✔
2597
      }
2598
      case TSDB_NORMAL_TABLE: {
981,114✔
2599
        code = metaHandleNormalTableDrop(pMeta, pEntry);
981,114✔
2600
        break;
981,114✔
2601
      }
2602
      case TSDB_VIRTUAL_NORMAL_TABLE: {
44,302✔
2603
        code = metaHandleVirtualNormalTableDrop(pMeta, pEntry);
44,302✔
2604
        break;
44,302✔
2605
      }
2606
      case TSDB_VIRTUAL_CHILD_TABLE: {
34,769✔
2607
        code = metaHandleVirtualChildTableDrop(pMeta, pEntry, false);
34,769✔
2608
        break;
34,769✔
2609
      }
2610
      default: {
×
2611
        code = TSDB_CODE_INVALID_PARA;
×
2612
        break;
×
2613
      }
2614
    }
2615
  }
2616

2617
  if (TSDB_CODE_SUCCESS == code) {
121,683,788✔
2618
    pMeta->changed = true;
121,697,193✔
2619
    metaDebug("vgId:%d, index:%" PRId64 ", handle meta entry success, type:%d tb:%s uid:%" PRId64, vgId,
121,708,118✔
2620
              pEntry->version, pEntry->type, pEntry->type > 0 ? pEntry->name : "", pEntry->uid);
2621
  } else {
2622
    metaErr(vgId, code);
19✔
2623
  }
2624
  TAOS_RETURN(code);
121,696,612✔
2625
}
2626

2627
void metaHandleSyncEntry(SMeta *pMeta, const SMetaEntry *pEntry) {
220,356✔
2628
  int32_t code = TSDB_CODE_SUCCESS;
220,356✔
2629
  code = metaHandleEntry2(pMeta, pEntry);
220,356✔
2630
  if (code) {
220,356✔
2631
    metaErr(TD_VID(pMeta->pVnode), code);
×
2632
  }
2633
  return;
220,356✔
2634
}
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