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

taosdata / TDengine / #3660

15 Mar 2025 09:06AM UTC coverage: 62.039% (-1.3%) from 63.314%
#3660

push

travis-ci

web-flow
feat(stream): support stream processing for virtual tables (#30144)

* enh: add client processing

* enh: add mnode vtables processing

* enh: add mnode vtable processing

* enh: add normal child vtable support

* fix: compile issues

* fix: compile issues

* fix: create stream issues

* fix: multi stream scan issue

* fix: remove debug info

* fix: agg task and task level issues

* fix: correct task output type

* fix: split vtablescan from agg

* fix: memory leak issues

* fix: add limitations

* Update 09-error-code.md

* Update 09-error-code.md

* fix: remove usless case

* feat(stream): extract original table data in source scan task

Implemented functionality in the source task to extract data
corresponding to the virtual table from the original table using WAL.
The extracted data is then sent to the downstream merge task for further
processing.

* feat(stream): multi-way merge using loser tree in virtual merge task

Implemented multi-way merge in the merge task using a loser tree to
combine data from multiple original table into a single virtual table.
The merged virtual table data is then pushed downstream for further
processing.  Introduced memory limit handling during the merge process
with configurable behavior when the memory limit is reached.

* fix(test): remove useless cases

---------

Co-authored-by: dapan1121 <wpan@taosdata.com>
Co-authored-by: Pan Wei <72057773+dapan1121@users.noreply.github.com>

154078 of 317582 branches covered (48.52%)

Branch coverage included in aggregate %.

313 of 2391 new or added lines in 34 files covered. (13.09%)

26134 existing lines in 205 files now uncovered.

240261 of 318051 relevant lines covered (75.54%)

16655189.27 hits per line

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

47.46
/source/dnode/vnode/src/meta/metaEntry2.c
1
/*
2
 * Copyright (c) 2023 Hongze Cheng <hzcheng@umich.edu>.
3
 * All rights reserved.
4
 *
5
 * This code is the intellectual property of Hongze Cheng.
6
 * Any reproduction or distribution, in whole or in part,
7
 * without the express written permission of Hongze Cheng is
8
 * strictly prohibited.
9
 */
10

11
#include "meta.h"
12

13
extern SDmNotifyHandle dmNotifyHdl;
14

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

22
static void    metaTimeSeriesNotifyCheck(SMeta *pMeta);
23
static int32_t metaGetChildUidsOfSuperTable(SMeta *pMeta, tb_uid_t suid, SArray **childList);
24
static int32_t metaFetchTagIdxKey(SMeta *pMeta, const SMetaEntry *pEntry, const SSchema *pTagColumn,
25
                                  STagIdxKey **ppTagIdxKey, int32_t *pTagIdxKeySize);
26
static void    metaFetchTagIdxKeyFree(STagIdxKey **ppTagIdxKey);
27

28
#define metaErr(VGID, ERRNO)                                                                                     \
29
  do {                                                                                                           \
30
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " type:%d uid:%" PRId64 " name:%s", VGID, \
31
              __func__, __FILE__, __LINE__, tstrerror(ERRNO), pEntry->version, pEntry->type, pEntry->uid,        \
32
              pEntry->type > 0 ? pEntry->name : NULL);                                                           \
33
  } while (0)
34

35
typedef enum {
36
  META_ENTRY_TABLE = 0,
37
  META_SCHEMA_TABLE,
38
  META_UID_IDX,
39
  META_NAME_IDX,
40
  META_SUID_IDX,
41
  META_CHILD_IDX,
42
  META_TAG_IDX,
43
  META_BTIME_IDX,
44
  META_TTL_IDX,
45
  META_TABLE_MAX,
46
} EMetaTable;
47

48
typedef enum {
49
  META_TABLE_OP_INSERT = 0,
50
  META_TABLE_OP_UPDATA,
51
  META_TABLE_OP_DELETE,
52
  META_TABLE_OP_MAX,
53
} EMetaTableOp;
54

55
typedef struct {
56
  const SMetaEntry *pEntry;
57
  const SMetaEntry *pSuperEntry;
58
  const SMetaEntry *pOldEntry;
59
} SMetaHandleParam;
60

61
typedef struct {
62
  EMetaTable   table;
63
  EMetaTableOp op;
64
} SMetaTableOp;
65

66
int32_t metaFetchEntryByUid(SMeta *pMeta, int64_t uid, SMetaEntry **ppEntry) {
184,309✔
67
  int32_t code = TSDB_CODE_SUCCESS;
184,309✔
68
  void   *value = NULL;
184,309✔
69
  int32_t valueSize = 0;
184,309✔
70

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

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

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

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

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

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

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

141
void metaFetchEntryFree(SMetaEntry **ppEntry) { metaCloneEntryFree(ppEntry); }
184,340✔
142

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

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

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

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

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

180
  // put to tdb
181
  if (META_TABLE_OP_INSERT == op) {
201,201✔
182
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
185,474✔
183
  } else if (META_TABLE_OP_UPDATA == op) {
15,727✔
184
    code = tdbTbUpsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
10,562✔
185
  } else if (META_TABLE_OP_DELETE == op) {
5,165!
186
    code = tdbTbInsert(pMeta->pTbDb, &key, sizeof(key), value, valueSize, pMeta->txn);
5,165✔
187
  } else {
188
    code = TSDB_CODE_INVALID_PARA;
×
189
  }
190
  if (TSDB_CODE_SUCCESS != code) {
201,291!
191
    metaErr(vgId, code);
×
192
  }
193
  taosMemoryFree(value);
201,216!
194
  return code;
201,218✔
195
}
196

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

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

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

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

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

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

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

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

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

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

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

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

281
  if (pOldColumn && pNewColumn) {
132,098✔
282
    if (IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
130,303✔
283
      return TSDB_CODE_SUCCESS;
3,082✔
284
    } else if (IS_IDX_ON(pOldColumn) && !IS_IDX_ON(pNewColumn)) {
127,221!
285
      action = DROP_INDEX;
2,181✔
286
    } else if (!IS_IDX_ON(pOldColumn) && IS_IDX_ON(pNewColumn)) {
125,040!
287
      action = ADD_INDEX;
945✔
288
    } else {
289
      return TSDB_CODE_SUCCESS;
124,095✔
290
    }
291
  } else if (pOldColumn) {
1,795✔
292
    if (IS_IDX_ON(pOldColumn)) {
750✔
293
      action = DROP_INDEX;
15✔
294
    } else {
295
      return TSDB_CODE_SUCCESS;
735✔
296
    }
297
  } else {
298
    if (IS_IDX_ON(pNewColumn)) {
1,045!
299
      action = ADD_INDEX;
×
300
    } else {
301
      return TSDB_CODE_SUCCESS;
1,045✔
302
    }
303
  }
304

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

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

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

326
    STagIdxKey *pTagIdxKey = NULL;
5,005✔
327
    int32_t     tagIdxKeySize = 0;
5,005✔
328

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

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

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

365
    metaFetchTagIdxKeyFree(&pTagIdxKey);
5,005✔
366
    metaFetchEntryFree(&pChildEntry);
5,005✔
367
  }
368

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

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

UNCOV
377
  const SMetaEntry *pEntry = pParam->pEntry;
×
UNCOV
378
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
×
379
  enum { ADD_COLUMN, DROP_COLUMN } action;
380

UNCOV
381
  if (pOldColumn && pNewColumn) {
×
UNCOV
382
    return TSDB_CODE_SUCCESS;
×
UNCOV
383
  } else if (pOldColumn) {
×
UNCOV
384
    action = DROP_COLUMN;
×
385
  } else {
UNCOV
386
    action = ADD_COLUMN;
×
387
  }
388

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

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

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

UNCOV
410
    SMetaHandleParam param = {
×
411
        .pEntry = pChildEntry
412
    };
413

UNCOV
414
    if (action == ADD_COLUMN) {
×
UNCOV
415
      code = updataTableColRef(&pChildEntry->colRef, pNewColumn, 1, NULL);
×
UNCOV
416
      if (code) {
×
417
        metaErr(TD_VID(pMeta->pVnode), code);
×
418
        taosArrayDestroy(childTables);
×
UNCOV
419
        metaFetchEntryFree(&pChildEntry);
×
UNCOV
420
        return code;
×
421
      }
422

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

UNCOV
439
      code = metaEntryTableUpdate(pMeta, &param);
×
UNCOV
440
      if (code) {
×
UNCOV
441
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
442
        taosArrayDestroy(childTables);
×
UNCOV
443
        metaFetchEntryFree(&pChildEntry);
×
UNCOV
444
        return code;
×
445
      }
446
    }
UNCOV
447
    metaFetchEntryFree(&pChildEntry);
×
448
  }
449

UNCOV
450
  taosArrayDestroy(childTables);
×
UNCOV
451
  return code;
×
452

453
}
454

455
static int32_t metaUpdateSuperTableTagSchema(SMeta *pMeta, const SMetaHandleParam *pParam) {
6,253✔
456
  int32_t               code = TSDB_CODE_SUCCESS;
6,253✔
457
  const SMetaEntry     *pEntry = pParam->pEntry;
6,253✔
458
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
6,253✔
459
  const SSchemaWrapper *pNewTagSchema = &pEntry->stbEntry.schemaTag;
6,253✔
460
  const SSchemaWrapper *pOldTagSchema = &pOldEntry->stbEntry.schemaTag;
6,253✔
461

462
  int32_t iOld = 0, iNew = 0;
6,253✔
463
  for (; iOld < pOldTagSchema->nCols && iNew < pNewTagSchema->nCols;) {
137,128✔
464
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
130,874✔
465
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
130,874✔
466

467
    if (pOldColumn->colId == pNewColumn->colId) {
130,874✔
468
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
130,303✔
469
      if (code) {
130,305✔
470
        metaErr(TD_VID(pMeta->pVnode), code);
1!
471
        return code;
×
472
      }
473

474
      iOld++;
130,304✔
475
      iNew++;
130,304✔
476
    } else if (pOldColumn->colId < pNewColumn->colId) {
571!
477
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
571✔
478
      if (code) {
571!
UNCOV
479
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
480
        return code;
×
481
      }
482

483
      iOld++;
571✔
484
    } else {
UNCOV
485
      code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, NULL, pNewColumn);
×
UNCOV
486
      if (code) {
×
UNCOV
487
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
488
        return code;
×
489
      }
490

UNCOV
491
      iNew++;
×
492
    }
493
  }
494

495
  for (; iOld < pOldTagSchema->nCols; iOld++) {
6,433✔
496
    SSchema *pOldColumn = pOldTagSchema->pSchema + iOld;
179✔
497
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, pOldColumn, NULL);
179✔
498
    if (code) {
179!
UNCOV
499
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
500
      return code;
×
501
    }
502
  }
503

504
  for (; iNew < pNewTagSchema->nCols; iNew++) {
7,302✔
505
    SSchema *pNewColumn = pNewTagSchema->pSchema + iNew;
1,049✔
506
    code = metaAddOrDropTagIndexOfSuperTable(pMeta, pParam, NULL, pNewColumn);
1,049✔
507
    if (code) {
1,048!
UNCOV
508
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
509
      return code;
×
510
    }
511
  }
512

513
  return code;
6,253✔
514
}
515

UNCOV
516
static int32_t metaUpdateSuperTableRowSchema(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
UNCOV
517
  int32_t               code = TSDB_CODE_SUCCESS;
×
UNCOV
518
  const SMetaEntry     *pEntry = pParam->pEntry;
×
UNCOV
519
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
×
UNCOV
520
  const SSchemaWrapper *pNewRowSchema = &pEntry->stbEntry.schemaRow;
×
UNCOV
521
  const SSchemaWrapper *pOldRowSchema = &pOldEntry->stbEntry.schemaRow;
×
522

UNCOV
523
  int32_t iOld = 0, iNew = 0;
×
UNCOV
524
  for (; iOld < pOldRowSchema->nCols && iNew < pNewRowSchema->nCols;) {
×
UNCOV
525
    SSchema *pOldColumn = pOldRowSchema->pSchema + iOld;
×
UNCOV
526
    SSchema *pNewColumn = pNewRowSchema->pSchema + iNew;
×
527

UNCOV
528
    if (pOldColumn->colId == pNewColumn->colId) {
×
UNCOV
529
      code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, pOldColumn, pNewColumn);
×
UNCOV
530
      if (code) {
×
UNCOV
531
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
532
        return code;
×
533
      }
534

UNCOV
535
      iOld++;
×
UNCOV
536
      iNew++;
×
UNCOV
537
    } else if (pOldColumn->colId < pNewColumn->colId) {
×
UNCOV
538
      code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, pOldColumn, NULL);
×
UNCOV
539
      if (code) {
×
UNCOV
540
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
541
        return code;
×
542
      }
543

UNCOV
544
      iOld++;
×
545
    } else {
UNCOV
546
      code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, NULL, pNewColumn);
×
UNCOV
547
      if (code) {
×
UNCOV
548
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
549
        return code;
×
550
      }
551

552
      iNew++;
×
553
    }
554
  }
555

UNCOV
556
  for (; iOld < pOldRowSchema->nCols; iOld++) {
×
UNCOV
557
    SSchema *pOldColumn = pOldRowSchema->pSchema + iOld;
×
UNCOV
558
    code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, pOldColumn, NULL);
×
UNCOV
559
    if (code) {
×
UNCOV
560
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
561
      return code;
×
562
    }
563
  }
564

565
  for (; iNew < pNewRowSchema->nCols; iNew++) {
×
566
    SSchema *pNewColumn = pNewRowSchema->pSchema + iNew;
×
UNCOV
567
    code = metaAddOrDropColumnIndexOfVirtualSuperTable(pMeta, pParam, NULL, pNewColumn);
×
UNCOV
568
    if (code) {
×
UNCOV
569
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
570
      return code;
×
571
    }
572
  }
573

UNCOV
574
  return code;
×
575
}
576

577
static int32_t metaSchemaTableUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
51,503✔
578
  int32_t code = TSDB_CODE_SUCCESS;
51,503✔
579

580
  const SMetaEntry *pEntry = pParam->pEntry;
51,503✔
581
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
51,503✔
582

583
  if (NULL == pOldEntry) {
51,503✔
584
    return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
42,143✔
585
  }
586

587
  if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
9,360!
588
    // check row schema
UNCOV
589
    if (pOldEntry->ntbEntry.schemaRow.version != pEntry->ntbEntry.schemaRow.version) {
×
590
      return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
303✔
591
    }
592
  } else if (pEntry->type == TSDB_SUPER_TABLE) {
9,512!
593
    // check row schema
594
    if (pOldEntry->stbEntry.schemaRow.version != pEntry->stbEntry.schemaRow.version) {
9,514✔
595
      if (TABLE_IS_VIRTUAL(pEntry->flags)) {
3,261!
596
        return metaUpdateSuperTableRowSchema(pMeta, pParam);
×
597
      } else {
598
        return metaSchemaTableUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
3,261✔
599
      }
600

601
    }
602

603
    // check tag schema
604
    code = metaUpdateSuperTableTagSchema(pMeta, pParam);
6,253✔
605
    if (code) {
6,251!
UNCOV
606
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
607
      return code;
×
608
    }
609

610
  } else {
UNCOV
611
    return TSDB_CODE_INVALID_PARA;
×
612
  }
613

614
  return TSDB_CODE_SUCCESS;
6,293✔
615
}
616

UNCOV
617
static int32_t metaSchemaTableDelete(SMeta *pMeta, const SMetaHandleParam *pEntry) {
×
618
  // TODO
619
  return TSDB_CODE_SUCCESS;
×
620
}
621

622
// Uid Index
623
static void metaBuildEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) {
196,005✔
624
  pInfo->uid = pEntry->uid;
196,005✔
625
  pInfo->version = pEntry->version;
196,005✔
626
  if (pEntry->type == TSDB_SUPER_TABLE) {
196,005✔
627
    pInfo->suid = pEntry->uid;
37,300✔
628
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
37,300✔
629
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
158,705!
630
    pInfo->suid = pEntry->ctbEntry.suid;
144,024✔
631
    pInfo->skmVer = 0;
144,024✔
632
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
14,681!
633
    pInfo->suid = 0;
14,682✔
634
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
14,682✔
635
  }
636
}
196,005✔
637

638
static int32_t metaUidIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
196,016✔
639
  int32_t code = TSDB_CODE_SUCCESS;
196,016✔
640
  int32_t vgId = TD_VID(pMeta->pVnode);
196,016✔
641

642
  const SMetaEntry *pEntry = pParam->pEntry;
196,016✔
643

644
  // update cache
645
  SMetaInfo info = {0};
196,016✔
646
  metaBuildEntryInfo(pEntry, &info);
196,016✔
647
  code = metaCacheUpsert(pMeta, &info);
196,017✔
648
  if (code) {
195,915!
UNCOV
649
    metaErr(vgId, code);
×
650
  }
651

652
  // put to tdb
653
  SUidIdxVal value = {
195,910✔
654
      .suid = info.suid,
195,910✔
655
      .skmVer = info.skmVer,
195,910✔
656
      .version = pEntry->version,
195,910✔
657
  };
658
  if (META_TABLE_OP_INSERT == op) {
195,910✔
659
    code = tdbTbInsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
185,358✔
660
  } else if (META_TABLE_OP_UPDATA == op) {
10,552!
661
    code = tdbTbUpsert(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), &value, sizeof(value), pMeta->txn);
10,557✔
662
  }
663
  return code;
196,210✔
664
}
665

666
static int32_t metaUidIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
185,466✔
667
  return metaUidIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
185,466✔
668
}
669

670
static int32_t metaUidIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
10,555✔
671
  return metaUidIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
10,555✔
672
}
673

674
static int32_t metaUidIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
8,845✔
675
  int32_t code = 0;
8,845✔
676

677
  const SMetaEntry *pEntry = pParam->pOldEntry;
8,845✔
678

679
  // delete tdb
680
  code = tdbTbDelete(pMeta->pUidIdx, &pEntry->uid, sizeof(pEntry->uid), pMeta->txn);
8,845✔
681
  if (code) {
8,845!
UNCOV
682
    metaErr(TD_VID(pMeta->pVnode), code);
×
683
  }
684

685
  // delete cache
686
  (void)metaCacheDrop(pMeta, pEntry->uid);
8,845✔
687
  return code;
8,844✔
688
}
689

690
// Name Index
691
static int32_t metaNameIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
185,529✔
692
  int32_t code = TSDB_CODE_SUCCESS;
185,529✔
693

694
  const SMetaEntry *pEntry = pParam->pEntry;
185,529✔
695

696
  if (META_TABLE_OP_INSERT == op) {
185,529!
697
    code = tdbTbInsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
185,580✔
698
                       pMeta->txn);
UNCOV
699
  } else if (META_TABLE_OP_UPDATA == op) {
×
UNCOV
700
    code = tdbTbUpsert(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, &pEntry->uid, sizeof(pEntry->uid),
×
701
                       pMeta->txn);
702
  } else {
UNCOV
703
    code = TSDB_CODE_INVALID_PARA;
×
704
  }
705
  return code;
185,627✔
706
}
707

708
static int32_t metaNameIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
185,532✔
709
  int32_t code = TSDB_CODE_SUCCESS;
185,532✔
710
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
185,532✔
711
}
712

UNCOV
713
static int32_t metaNameIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
714
  return metaNameIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
×
715
}
716

717
static int32_t metaNameIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
8,846✔
718
  int32_t code = TSDB_CODE_SUCCESS;
8,846✔
719

720
  const SMetaEntry *pEntry = pParam->pOldEntry;
8,846✔
721
  code = tdbTbDelete(pMeta->pNameIdx, pEntry->name, strlen(pEntry->name) + 1, pMeta->txn);
8,846✔
722
  if (code) {
8,845!
UNCOV
723
    metaErr(TD_VID(pMeta->pVnode), code);
×
724
  }
725
  return code;
8,845✔
726
}
727

728
// Suid Index
729
static int32_t metaSUidIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
27,748✔
730
  const SMetaEntry *pEntry = pParam->pEntry;
27,748✔
731

732
  int32_t code = tdbTbInsert(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), NULL, 0, pMeta->txn);
27,748✔
733
  if (code) {
27,858!
734
    metaErr(TD_VID(pMeta->pVnode), code);
×
735
  }
736
  return code;
27,812✔
737
}
738

739
static int32_t metaSUidIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
2,518✔
740
  const SMetaEntry *pEntry = pParam->pOldEntry;
2,518✔
741

742
  int32_t code = tdbTbDelete(pMeta->pSuidIdx, &pEntry->uid, sizeof(pEntry->uid), pMeta->txn);
2,518✔
743
  if (code) {
2,518!
UNCOV
744
    metaErr(TD_VID(pMeta->pVnode), code);
×
745
  }
746
  return code;
2,518✔
747
}
748

749
// Child Index
750
static int32_t metaChildIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
143,905✔
751
  int32_t code = TSDB_CODE_SUCCESS;
143,905✔
752

753
  const SMetaEntry *pEntry = pParam->pEntry;
143,905✔
754

755
  SCtbIdxKey key = {
143,905✔
756
      .suid = pEntry->ctbEntry.suid,
143,905✔
757
      .uid = pEntry->uid,
143,905✔
758
  };
759

760
  if (META_TABLE_OP_INSERT == op) {
143,905✔
761
    code = tdbTbInsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
143,437✔
762
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
143,437✔
763
  } else if (META_TABLE_OP_UPDATA == op) {
468!
764
    code = tdbTbUpsert(pMeta->pCtbIdx, &key, sizeof(key), pEntry->ctbEntry.pTags,
478✔
765
                       ((STag *)(pEntry->ctbEntry.pTags))->len, pMeta->txn);
478✔
766
  } else {
767
    code = TSDB_CODE_INVALID_PARA;
×
768
  }
769
  return code;
143,929✔
770
}
771

772
static int32_t metaChildIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
143,429✔
773
  return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
143,429✔
774
}
775

776
static int32_t metaChildIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
700✔
777
  const SMetaEntry *pEntry = pParam->pEntry;
700✔
778
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
700✔
779
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
700✔
780

781
  const STag *pNewTags = (const STag *)pEntry->ctbEntry.pTags;
700✔
782
  const STag *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags;
700✔
783
  if (pNewTags->len != pOldTags->len || memcmp(pNewTags, pOldTags, pNewTags->len)) {
700✔
784
    return metaChildIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
478✔
785
  }
786
  return 0;
222✔
787
}
788

789
static int32_t metaChildIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
4,517✔
790
  const SMetaEntry *pEntry = pParam->pOldEntry;
4,517✔
791

792
  SCtbIdxKey key = {
4,517✔
793
      .suid = pEntry->ctbEntry.suid,
4,517✔
794
      .uid = pEntry->uid,
4,517✔
795
  };
796
  return tdbTbDelete(pMeta->pCtbIdx, &key, sizeof(key), pMeta->txn);
4,517✔
797
}
798

799
// Tag Index
800
static int32_t metaFetchTagIdxKey(SMeta *pMeta, const SMetaEntry *pEntry, const SSchema *pTagColumn,
153,551✔
801
                                  STagIdxKey **ppTagIdxKey, int32_t *pTagIdxKeySize) {
802
  int32_t code = TSDB_CODE_SUCCESS;
153,551✔
803

804
  STagIdxKey *pTagIdxKey = NULL;
153,551✔
805
  int32_t     nTagIdxKey;
806
  const void *pTagData = NULL;
153,551✔
807
  int32_t     nTagData = 0;
153,551✔
808

809
  STagVal tagVal = {
153,551✔
810
      .cid = pTagColumn->colId,
153,551✔
811
  };
812

813
  if (tTagGet((const STag *)pEntry->ctbEntry.pTags, &tagVal)) {
153,551✔
814
    if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
153,129!
815
      pTagData = tagVal.pData;
22,676✔
816
      nTagData = (int32_t)tagVal.nData;
22,676✔
817
    } else {
818
      pTagData = &(tagVal.i64);
130,453✔
819
      nTagData = tDataTypes[pTagColumn->type].bytes;
130,453✔
820
    }
821
  } else {
822
    if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
428!
823
      nTagData = tDataTypes[pTagColumn->type].bytes;
257✔
824
    }
825
  }
826

827
  code = metaCreateTagIdxKey(pEntry->ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type,
153,557✔
828
                             pEntry->uid, &pTagIdxKey, &nTagIdxKey);
153,557✔
829
  if (code) {
153,541✔
830
    metaErr(TD_VID(pMeta->pVnode), code);
12!
UNCOV
831
    return code;
×
832
  }
833

834
  *ppTagIdxKey = pTagIdxKey;
153,529✔
835
  *pTagIdxKeySize = nTagIdxKey;
153,529✔
836
  return code;
153,529✔
837
}
838

839
static void metaFetchTagIdxKeyFree(STagIdxKey **ppTagIdxKey) {
153,564✔
840
  metaDestroyTagIdxKey(*ppTagIdxKey);
153,564✔
841
  *ppTagIdxKey = NULL;
153,565✔
842
}
153,565✔
843

844
static int32_t metaTagIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
143,429✔
845
  int32_t code = TSDB_CODE_SUCCESS;
143,429✔
846

847
  const SMetaEntry *pEntry = pParam->pEntry;
143,429✔
848
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
143,429✔
849

850
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
143,429✔
851
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
143,722✔
852
    const SSchema *pTagColumn = &pTagSchema->pSchema[0];
293✔
853

854
    STagVal tagVal = {
293✔
855
        .cid = pTagColumn->colId,
293✔
856
    };
857

858
    const void *pTagData = pEntry->ctbEntry.pTags;
293✔
859
    int32_t     nTagData = ((const STag *)pEntry->ctbEntry.pTags)->len;
293✔
860
    code = metaSaveJsonVarToIdx(pMeta, pEntry, pTagColumn);
293✔
861
    if (code) {
293!
UNCOV
862
      metaErr(TD_VID(pMeta->pVnode), code);
×
863
    }
864
  } else {
865
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
598,893✔
866
      STagIdxKey    *pTagIdxKey = NULL;
455,744✔
867
      int32_t        nTagIdxKey;
868
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
455,744✔
869

870
      if (!IS_IDX_ON(pTagColumn)) {
455,744✔
871
        continue;
312,637✔
872
      }
873

874
      code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pTagIdxKey, &nTagIdxKey);
143,107✔
875
      if (code) {
143,084!
UNCOV
876
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
877
        return code;
×
878
      }
879

880
      code = tdbTbInsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn);
143,084✔
881
      if (code) {
143,124!
UNCOV
882
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
883
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
UNCOV
884
        return code;
×
885
      }
886
      metaFetchTagIdxKeyFree(&pTagIdxKey);
143,124✔
887
    }
888
  }
889
  return code;
143,442✔
890
}
891

892
static int32_t metaTagIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
700✔
893
  int32_t code = TSDB_CODE_SUCCESS;
700✔
894

895
  const SMetaEntry     *pEntry = pParam->pEntry;
700✔
896
  const SMetaEntry     *pOldEntry = pParam->pOldEntry;
700✔
897
  const SMetaEntry     *pSuperEntry = pParam->pSuperEntry;
700✔
898
  const SSchemaWrapper *pTagSchema = &pSuperEntry->stbEntry.schemaTag;
700✔
899
  const STag           *pNewTags = (const STag *)pEntry->ctbEntry.pTags;
700✔
900
  const STag           *pOldTags = (const STag *)pOldEntry->ctbEntry.pTags;
700✔
901

902
  if (pNewTags->len == pOldTags->len && !memcmp(pNewTags, pOldTags, pNewTags->len)) {
700✔
903
    return code;
222✔
904
  }
905

906
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
478✔
907
    code = metaDelJsonVarFromIdx(pMeta, pOldEntry, &pTagSchema->pSchema[0]);
11✔
908
    if (code) {
11!
UNCOV
909
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
910
      return code;
×
911
    }
912

913
    code = metaSaveJsonVarToIdx(pMeta, pEntry, &pTagSchema->pSchema[0]);
11✔
914
    if (code) {
11!
UNCOV
915
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
916
      return code;
×
917
    }
918
  } else {
919
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
2,357✔
920
      const SSchema *pTagColumn = &pTagSchema->pSchema[i];
1,890✔
921

922
      if (!IS_IDX_ON(pTagColumn)) {
1,890✔
923
        continue;
1,427✔
924
      }
925

926
      STagIdxKey *pOldTagIdxKey = NULL;
463✔
927
      int32_t     oldTagIdxKeySize = 0;
463✔
928
      STagIdxKey *pNewTagIdxKey = NULL;
463✔
929
      int32_t     newTagIdxKeySize = 0;
463✔
930

931
      code = metaFetchTagIdxKey(pMeta, pOldEntry, pTagColumn, &pOldTagIdxKey, &oldTagIdxKeySize);
463✔
932
      if (code) {
463!
UNCOV
933
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
934
        return code;
×
935
      }
936

937
      code = metaFetchTagIdxKey(pMeta, pEntry, pTagColumn, &pNewTagIdxKey, &newTagIdxKeySize);
463✔
938
      if (code) {
463!
UNCOV
939
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
940
        metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
UNCOV
941
        return code;
×
942
      }
943

944
      if (tagIdxKeyCmpr(pOldTagIdxKey, oldTagIdxKeySize, pNewTagIdxKey, newTagIdxKeySize)) {
463✔
945
        code = tdbTbDelete(pMeta->pTagIdx, pOldTagIdxKey, oldTagIdxKeySize, pMeta->txn);
97✔
946
        if (code) {
97!
UNCOV
947
          metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
948
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
UNCOV
949
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
UNCOV
950
          return code;
×
951
        }
952

953
        code = tdbTbInsert(pMeta->pTagIdx, pNewTagIdxKey, newTagIdxKeySize, NULL, 0, pMeta->txn);
97✔
954
        if (code) {
97!
UNCOV
955
          metaErr(TD_VID(pMeta->pVnode), code);
×
956
          metaFetchTagIdxKeyFree(&pOldTagIdxKey);
×
UNCOV
957
          metaFetchTagIdxKeyFree(&pNewTagIdxKey);
×
UNCOV
958
          return code;
×
959
        }
960
      }
961

962
      metaFetchTagIdxKeyFree(&pOldTagIdxKey);
463✔
963
      metaFetchTagIdxKeyFree(&pNewTagIdxKey);
463✔
964
    }
965
  }
966
  return code;
478✔
967
}
968

969
static int32_t metaTagIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
4,518✔
970
  int32_t code = TSDB_CODE_SUCCESS;
4,518✔
971

972
  const SMetaEntry     *pEntry = pParam->pEntry;
4,518✔
973
  const SMetaEntry     *pChild = pParam->pOldEntry;
4,518✔
974
  const SMetaEntry     *pSuper = pParam->pSuperEntry;
4,518✔
975
  const SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
4,518✔
976
  const SSchema        *pTagColumn = NULL;
4,518✔
977
  const STag           *pTags = (const STag *)pChild->ctbEntry.pTags;
4,518✔
978

979
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
4,518✔
980
    pTagColumn = &pTagSchema->pSchema[0];
8✔
981
    code = metaDelJsonVarFromIdx(pMeta, pChild, pTagColumn);
8✔
982
    if (code) {
8!
UNCOV
983
      metaErr(TD_VID(pMeta->pVnode), code);
×
984
    }
985
  } else {
986
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
29,989✔
987
      pTagColumn = &pTagSchema->pSchema[i];
25,480✔
988
      if (!IS_IDX_ON(pTagColumn)) {
25,480✔
989
        continue;
20,960✔
990
      }
991

992
      STagIdxKey *pTagIdxKey = NULL;
4,520✔
993
      int32_t     nTagIdxKey;
994

995
      code = metaFetchTagIdxKey(pMeta, pChild, pTagColumn, &pTagIdxKey, &nTagIdxKey);
4,520✔
996
      if (code) {
4,520!
UNCOV
997
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
998
        return code;
×
999
      }
1000

1001
      code = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
4,520✔
1002
      if (code) {
4,520!
UNCOV
1003
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1004
        metaFetchTagIdxKeyFree(&pTagIdxKey);
×
UNCOV
1005
        return code;
×
1006
      }
1007
      metaFetchTagIdxKeyFree(&pTagIdxKey);
4,520✔
1008
    }
1009
  }
1010
  return code;
4,517✔
1011
}
1012

1013
// Btime Index
1014
static int32_t metaBtimeIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
164,069✔
1015
  int32_t code = TSDB_CODE_SUCCESS;
164,069✔
1016

1017
  const SMetaEntry *pEntry;
1018
  if (META_TABLE_OP_DELETE == op) {
164,069✔
1019
    pEntry = pParam->pOldEntry;
6,327✔
1020
  } else {
1021
    pEntry = pParam->pEntry;
157,742✔
1022
  }
1023

1024
  SBtimeIdxKey key = {
164,069✔
1025
      .uid = pEntry->uid,
164,069✔
1026
  };
1027

1028
  if (TSDB_CHILD_TABLE == pEntry->type || TSDB_VIRTUAL_CHILD_TABLE == pEntry->type) {
164,069!
1029
    key.btime = pEntry->ctbEntry.btime;
147,921✔
1030
  } else if (TSDB_NORMAL_TABLE == pEntry->type || TSDB_VIRTUAL_NORMAL_TABLE == pEntry->type) {
16,148!
1031
    key.btime = pEntry->ntbEntry.btime;
16,148✔
1032
  } else {
UNCOV
1033
    return TSDB_CODE_INVALID_PARA;
×
1034
  }
1035

1036
  if (META_TABLE_OP_INSERT == op) {
164,069✔
1037
    code = tdbTbInsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
157,750✔
1038
  } else if (META_TABLE_OP_UPDATA == op) {
6,319!
UNCOV
1039
    code = tdbTbUpsert(pMeta->pBtimeIdx, &key, sizeof(key), NULL, 0, pMeta->txn);
×
1040
  } else if (META_TABLE_OP_DELETE == op) {
6,319!
1041
    code = tdbTbDelete(pMeta->pBtimeIdx, &key, sizeof(key), pMeta->txn);
6,327✔
1042
  } else {
UNCOV
1043
    code = TSDB_CODE_INVALID_PARA;
×
1044
  }
1045
  if (code) {
164,110!
UNCOV
1046
    metaErr(TD_VID(pMeta->pVnode), code);
×
1047
  }
1048
  return code;
164,107✔
1049
}
1050

1051
static int32_t metaBtimeIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
157,745✔
1052
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
157,745✔
1053
}
1054

UNCOV
1055
static int32_t metaBtimeIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
UNCOV
1056
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_UPDATA);
×
1057
}
1058

1059
static int32_t metaBtimeIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
6,327✔
1060
  return metaBtimeIdxUpsert(pMeta, pParam, META_TABLE_OP_DELETE);
6,327✔
1061
}
1062

1063
// TTL Index
1064
static int32_t metaTtlIdxUpsert(SMeta *pMeta, const SMetaHandleParam *pParam, EMetaTableOp op) {
157,796✔
1065
  const SMetaEntry *pEntry = pParam->pEntry;
157,796✔
1066

1067
  STtlUpdTtlCtx ctx = {
157,796✔
1068
      .uid = pEntry->uid,
157,796✔
1069
      .pTxn = pMeta->txn,
157,796✔
1070
  };
1071
  if (TSDB_CHILD_TABLE == pEntry->type) {
157,796✔
1072
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
143,441✔
1073
    ctx.changeTimeMs = pEntry->ctbEntry.btime;
143,441✔
1074
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
14,355✔
1075
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
14,354✔
1076
    ctx.changeTimeMs = pEntry->ntbEntry.btime;
14,354✔
1077
  } else {
1078
    return TSDB_CODE_INVALID_PARA;
1✔
1079
  }
1080

1081
  int32_t ret = ttlMgrInsertTtl(pMeta->pTtlMgr, &ctx);
157,795✔
1082
  if (ret < 0) {
157,788!
UNCOV
1083
    metaError("vgId:%d, failed to insert ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid, tstrerror(ret));
×
1084
  }
1085
  return TSDB_CODE_SUCCESS;
157,790✔
1086
}
1087

1088
static int32_t metaTtlIdxInsert(SMeta *pMeta, const SMetaHandleParam *pParam) {
157,797✔
1089
  return metaTtlIdxUpsert(pMeta, pParam, META_TABLE_OP_INSERT);
157,797✔
1090
}
1091

1092
static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam);
1093

1094
static int32_t metaTtlIdxUpdate(SMeta *pMeta, const SMetaHandleParam *pParam) {
1,046✔
1095
  int32_t code = TSDB_CODE_SUCCESS;
1,046✔
1096

1097
  const SMetaEntry *pEntry = pParam->pEntry;
1,046✔
1098
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
1,046✔
1099

1100
  if ((pEntry->type == TSDB_CHILD_TABLE && pOldEntry->ctbEntry.ttlDays != pEntry->ctbEntry.ttlDays) ||
1,046✔
1101
      (pEntry->type == TSDB_NORMAL_TABLE && pOldEntry->ntbEntry.ttlDays != pEntry->ntbEntry.ttlDays)) {
1,031✔
1102
    code = metaTtlIdxDelete(pMeta, pParam);
26✔
1103
    if (code) {
26!
UNCOV
1104
      metaErr(TD_VID(pMeta->pVnode), code);
×
1105
    }
1106

1107
    code = metaTtlIdxInsert(pMeta, pParam);
26✔
1108
    if (code) {
26!
UNCOV
1109
      metaErr(TD_VID(pMeta->pVnode), code);
×
1110
    }
1111
  }
1112

1113
  return TSDB_CODE_SUCCESS;
1,046✔
1114
}
1115

1116
static int32_t metaTtlIdxDelete(SMeta *pMeta, const SMetaHandleParam *pParam) {
6,353✔
1117
  int32_t code = TSDB_CODE_SUCCESS;
6,353✔
1118

1119
  const SMetaEntry *pEntry = pParam->pOldEntry;
6,353✔
1120
  STtlDelTtlCtx     ctx = {
6,353✔
1121
          .uid = pEntry->uid,
6,353✔
1122
          .pTxn = pMeta->txn,
6,353✔
1123
  };
1124

1125
  if (TSDB_CHILD_TABLE == pEntry->type) {
6,353✔
1126
    ctx.ttlDays = pEntry->ctbEntry.ttlDays;
4,532✔
1127
  } else if (TSDB_NORMAL_TABLE == pEntry->type) {
1,821!
1128
    ctx.ttlDays = pEntry->ntbEntry.ttlDays;
1,821✔
1129
  } else {
UNCOV
1130
    code = TSDB_CODE_INVALID_PARA;
×
1131
  }
1132

1133
  if (TSDB_CODE_SUCCESS == code) {
6,353!
1134
    int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
6,354✔
1135
    if (ret < 0) {
6,353!
UNCOV
1136
      metaError("vgId:%d, failed to delete ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pEntry->uid,
×
1137
                tstrerror(ret));
1138
    }
1139
  }
1140
  return code;
6,353✔
1141
}
1142

1143
static void metaTimeSeriesNotifyCheck(SMeta *pMeta) {
160,488✔
1144
#if defined(TD_ENTERPRISE)
1145
  int64_t nTimeSeries = metaGetTimeSeriesNum(pMeta, 0);
160,488✔
1146
  int64_t deltaTS = nTimeSeries - pMeta->pVnode->config.vndStats.numOfReportedTimeSeries;
160,513✔
1147
  if (deltaTS > tsTimeSeriesThreshold) {
160,513✔
1148
    if (0 == atomic_val_compare_exchange_8(&dmNotifyHdl.state, 1, 2)) {
109,862✔
1149
      if (tsem_post(&dmNotifyHdl.sem) != 0) {
109,830!
UNCOV
1150
        metaError("vgId:%d, failed to post semaphore, errno:%d", TD_VID(pMeta->pVnode), ERRNO);
×
1151
      }
1152
    }
1153
  }
1154
#endif
1155
}
160,514✔
1156

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

1215
static int32_t metaHandleSuperTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
27,789✔
1216
  int32_t code = TSDB_CODE_SUCCESS;
27,789✔
1217

1218
  SMetaTableOp ops[] = {
27,789✔
1219
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1220
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: here should be insert
1221
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1222
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1223
      {META_SUID_IDX, META_TABLE_OP_INSERT},      //
1224
  };
1225

1226
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
166,545✔
1227
    SMetaTableOp          *op = &ops[i];
138,784✔
1228
    const SMetaHandleParam param = {
138,784✔
1229
        .pEntry = pEntry,
1230
    };
1231
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
138,784✔
1232
    if (TSDB_CODE_SUCCESS != code) {
138,796✔
1233
      metaErr(TD_VID(pMeta->pVnode), code);
40!
UNCOV
1234
      return code;
×
1235
    }
1236
  }
1237

1238
  return code;
27,761✔
1239
}
1240
static int32_t metaHandleSuperTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
27,489✔
1241
  int32_t code = TSDB_CODE_SUCCESS;
27,489✔
1242

1243
  metaWLock(pMeta);
27,489✔
1244
  code = metaHandleSuperTableCreateImpl(pMeta, pEntry);
27,857✔
1245
  metaULock(pMeta);
27,784✔
1246

1247
  if (TSDB_CODE_SUCCESS == code) {
27,835!
1248
    pMeta->pVnode->config.vndStats.numOfSTables++;
27,835✔
1249

1250
    metaInfo("vgId:%d, %s success, version:%" PRId64 " type:%d uid:%" PRId64 " name:%s", TD_VID(pMeta->pVnode),
27,835!
1251
             __func__, pEntry->version, pEntry->type, pEntry->uid, pEntry->name);
1252
  } else {
UNCOV
1253
    metaErr(TD_VID(pMeta->pVnode), code);
×
1254
  }
1255
  return code;
27,888✔
1256
}
1257

1258
static int32_t metaHandleNormalTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
14,344✔
1259
  int32_t code = TSDB_CODE_SUCCESS;
14,344✔
1260

1261
  SMetaTableOp ops[] = {
14,344✔
1262
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1263
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: need to be insert
1264
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1265
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1266
      {META_BTIME_IDX, META_TABLE_OP_INSERT},     //
1267
      {META_TTL_IDX, META_TABLE_OP_INSERT},       //
1268
  };
1269

1270
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
100,380✔
1271
    SMetaTableOp *op = &ops[i];
86,037✔
1272

1273
    SMetaHandleParam param = {
86,037✔
1274
        .pEntry = pEntry,
1275
    };
1276

1277
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
86,037✔
1278
    if (TSDB_CODE_SUCCESS != code) {
86,037✔
1279
      metaErr(TD_VID(pMeta->pVnode), code);
1!
UNCOV
1280
      return code;
×
1281
    }
1282
  }
1283

1284
  return code;
14,343✔
1285
}
1286
static int32_t metaHandleNormalTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
14,341✔
1287
  int32_t code = TSDB_CODE_SUCCESS;
14,341✔
1288

1289
  // update TDB
1290
  metaWLock(pMeta);
14,341✔
1291
  code = metaHandleNormalTableCreateImpl(pMeta, pEntry);
14,344✔
1292
  metaULock(pMeta);
14,344✔
1293

1294
  // update other stuff
1295
  if (TSDB_CODE_SUCCESS == code) {
14,342!
1296
    pMeta->pVnode->config.vndStats.numOfNTables++;
14,342✔
1297
    pMeta->pVnode->config.vndStats.numOfNTimeSeries += pEntry->ntbEntry.schemaRow.nCols - 1;
14,342✔
1298

1299
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
14,342✔
1300
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, -1, &pEntry->ntbEntry.schemaRow);
43✔
1301
      if (rc < 0) {
43!
UNCOV
1302
        metaError("vgId:%d, failed to create table:%s since %s", TD_VID(pMeta->pVnode), pEntry->name, tstrerror(rc));
×
1303
      }
1304
    }
1305
    metaTimeSeriesNotifyCheck(pMeta);
14,342✔
1306
  } else {
UNCOV
1307
    metaErr(TD_VID(pMeta->pVnode), code);
×
1308
  }
1309
  return code;
14,344✔
1310
}
1311

1312
static int32_t metaHandleChildTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry, const SMetaEntry *pSuperEntry) {
143,419✔
1313
  int32_t code = TSDB_CODE_SUCCESS;
143,419✔
1314

1315
  SMetaTableOp ops[] = {
143,419✔
1316
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},  //
1317
      {META_UID_IDX, META_TABLE_OP_INSERT},      //
1318
      {META_NAME_IDX, META_TABLE_OP_INSERT},     //
1319
      {META_CHILD_IDX, META_TABLE_OP_INSERT},    //
1320
      {META_TAG_IDX, META_TABLE_OP_INSERT},      //
1321
      {META_BTIME_IDX, META_TABLE_OP_INSERT},    //
1322
      {META_TTL_IDX, META_TABLE_OP_INSERT},      //
1323
  };
1324

1325
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1,146,887✔
1326
    SMetaTableOp *op = &ops[i];
1,003,542✔
1327

1328
    SMetaHandleParam param = {
1,003,542✔
1329
        .pEntry = pEntry,
1330
        .pSuperEntry = pSuperEntry,
1331
    };
1332

1333
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
1,003,542✔
1334
    if (TSDB_CODE_SUCCESS != code) {
1,003,446!
UNCOV
1335
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1336
      return code;
×
1337
    }
1338
  }
1339

1340
  if (TSDB_CODE_SUCCESS == code) {
143,345!
1341
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
143,419✔
1342
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
143,424✔
1343
    if (ret < 0) {
143,443!
UNCOV
1344
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1345
    }
1346

1347
    ret = metaTbGroupCacheClear(pMeta, pSuperEntry->uid);
143,443✔
1348
    if (ret < 0) {
143,425!
UNCOV
1349
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1350
    }
1351
  }
1352
  return code;
143,425✔
1353
}
1354

1355
static int32_t metaHandleChildTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
143,430✔
1356
  int32_t     code = TSDB_CODE_SUCCESS;
143,430✔
1357
  SMetaEntry *pSuperEntry = NULL;
143,430✔
1358

1359
  // get the super table entry
1360
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
143,430✔
1361
  if (code) {
143,417!
UNCOV
1362
    metaErr(TD_VID(pMeta->pVnode), code);
×
1363
    return code;
×
1364
  }
1365

1366
  // update TDB
1367
  metaWLock(pMeta);
143,417✔
1368
  code = metaHandleChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
143,448✔
1369
  metaULock(pMeta);
143,421✔
1370

1371
  // update other stuff
1372
  if (TSDB_CODE_SUCCESS == code) {
143,438!
1373
    pMeta->pVnode->config.vndStats.numOfCTables++;
143,438✔
1374

1375
    if (!metaTbInFilterCache(pMeta, pSuperEntry->name, 1)) {
143,438!
1376
      int32_t nCols = 0;
143,453✔
1377
      int32_t ret = metaGetStbStats(pMeta->pVnode, pSuperEntry->uid, 0, &nCols);
143,453✔
1378
      if (ret < 0) {
143,456!
UNCOV
1379
        metaErr(TD_VID(pMeta->pVnode), ret);
×
1380
      }
1381
      pMeta->pVnode->config.vndStats.numOfTimeSeries += (nCols > 0 ? nCols - 1 : 0);
143,448!
1382
    }
1383

1384
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
143,444✔
1385
      int32_t rc = tsdbCacheNewTable(pMeta->pVnode->pTsdb, pEntry->uid, pEntry->ctbEntry.suid, NULL);
2,060✔
1386
      if (rc < 0) {
2,060!
UNCOV
1387
        metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
1388
                  tstrerror(rc));
1389
      }
1390
    }
1391

1392
  } else {
UNCOV
1393
    metaErr(TD_VID(pMeta->pVnode), code);
×
1394
  }
1395
  metaTimeSeriesNotifyCheck(pMeta);
143,444✔
1396
  metaFetchEntryFree(&pSuperEntry);
143,445✔
1397
  return code;
143,457✔
1398
}
1399

UNCOV
1400
static int32_t metaHandleVirtualNormalTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry) {
×
UNCOV
1401
  int32_t code = TSDB_CODE_SUCCESS;
×
1402

UNCOV
1403
  SMetaTableOp ops[] = {
×
1404
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},   //
1405
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: need to be insert
1406
      {META_UID_IDX, META_TABLE_OP_INSERT},       //
1407
      {META_NAME_IDX, META_TABLE_OP_INSERT},      //
1408
      {META_BTIME_IDX, META_TABLE_OP_INSERT},     //
1409
  };
1410

UNCOV
1411
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
×
UNCOV
1412
    SMetaTableOp *op = &ops[i];
×
1413

UNCOV
1414
    SMetaHandleParam param = {
×
1415
        .pEntry = pEntry,
1416
    };
1417

UNCOV
1418
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
×
UNCOV
1419
    if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
1420
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1421
      return code;
×
1422
    }
1423
  }
1424

UNCOV
1425
  return code;
×
1426
}
1427

UNCOV
1428
static int32_t metaHandleVirtualNormalTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
×
UNCOV
1429
  int32_t code = TSDB_CODE_SUCCESS;
×
1430

1431
  // update TDB
UNCOV
1432
  metaWLock(pMeta);
×
UNCOV
1433
  code = metaHandleVirtualNormalTableCreateImpl(pMeta, pEntry);
×
UNCOV
1434
  metaULock(pMeta);
×
1435

1436
  // update other stuff
UNCOV
1437
  if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
1438
    pMeta->pVnode->config.vndStats.numOfVTables++;
×
1439
  } else {
UNCOV
1440
    metaErr(TD_VID(pMeta->pVnode), code);
×
1441
  }
UNCOV
1442
  return code;
×
1443
}
1444

UNCOV
1445
static int32_t metaHandleVirtualChildTableCreateImpl(SMeta *pMeta, const SMetaEntry *pEntry, const SMetaEntry *pSuperEntry) {
×
UNCOV
1446
  int32_t code = TSDB_CODE_SUCCESS;
×
1447

UNCOV
1448
  SMetaTableOp ops[] = {
×
1449
      {META_ENTRY_TABLE, META_TABLE_OP_INSERT},  //
1450
      {META_UID_IDX, META_TABLE_OP_INSERT},      //
1451
      {META_NAME_IDX, META_TABLE_OP_INSERT},     //
1452
      {META_CHILD_IDX, META_TABLE_OP_INSERT},    //
1453
      {META_TAG_IDX, META_TABLE_OP_INSERT},      //
1454
      {META_BTIME_IDX, META_TABLE_OP_INSERT},    //
1455
  };
1456

1457
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
×
1458
    SMetaTableOp *op = &ops[i];
×
1459

UNCOV
1460
    SMetaHandleParam param = {
×
1461
        .pEntry = pEntry,
1462
        .pSuperEntry = pSuperEntry,
1463
    };
1464

UNCOV
1465
    code = metaTableOpFn[op->table][op->op](pMeta, &param);
×
UNCOV
1466
    if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
1467
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1468
      return code;
×
1469
    }
1470
  }
1471

UNCOV
1472
  if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
1473
    metaUpdateStbStats(pMeta, pSuperEntry->uid, 1, 0, -1);
×
UNCOV
1474
    int32_t ret = metaUidCacheClear(pMeta, pSuperEntry->uid);
×
UNCOV
1475
    if (ret < 0) {
×
UNCOV
1476
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1477
    }
1478

UNCOV
1479
    ret = metaTbGroupCacheClear(pMeta, pSuperEntry->uid);
×
UNCOV
1480
    if (ret < 0) {
×
UNCOV
1481
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1482
    }
1483
  }
1484

1485
  return code;
×
1486
}
1487

1488
static int32_t metaHandleVirtualChildTableCreate(SMeta *pMeta, const SMetaEntry *pEntry) {
×
1489
  int32_t     code = TSDB_CODE_SUCCESS;
×
UNCOV
1490
  SMetaEntry *pSuperEntry = NULL;
×
1491

1492
  // get the super table entry
UNCOV
1493
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
×
UNCOV
1494
  if (code) {
×
UNCOV
1495
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1496
    return code;
×
1497
  }
1498

1499
  // update TDB
UNCOV
1500
  metaWLock(pMeta);
×
UNCOV
1501
  code = metaHandleVirtualChildTableCreateImpl(pMeta, pEntry, pSuperEntry);
×
UNCOV
1502
  metaULock(pMeta);
×
1503

1504
  // update other stuff
UNCOV
1505
  if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
1506
    pMeta->pVnode->config.vndStats.numOfVCTables++;
×
1507
  } else {
UNCOV
1508
    metaErr(TD_VID(pMeta->pVnode), code);
×
1509
  }
1510

UNCOV
1511
  metaFetchEntryFree(&pSuperEntry);
×
UNCOV
1512
  return code;
×
1513
}
1514

1515
static int32_t metaHandleNormalTableDropImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
1,810✔
1516
  int32_t code = TSDB_CODE_SUCCESS;
1,810✔
1517

1518
  SMetaTableOp ops[] = {
1,810✔
1519
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1520
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1521
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1522
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1523
      {META_TTL_IDX, META_TABLE_OP_DELETE},      //
1524

1525
      // {META_SCHEMA_TABLE, META_TABLE_OP_DELETE},  //
1526
  };
1527

1528
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
10,860✔
1529
    SMetaTableOp *op = &ops[i];
9,050✔
1530
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
9,050✔
1531
    if (code) {
9,050!
UNCOV
1532
      const SMetaEntry *pEntry = pParam->pEntry;
×
UNCOV
1533
      metaErr(TD_VID(pMeta->pVnode), code);
×
1534
    }
1535
  }
1536

1537
  return code;
1,810✔
1538
}
1539

1540
static int32_t metaHandleNormalTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
1,810✔
1541
  int32_t     code = TSDB_CODE_SUCCESS;
1,810✔
1542
  SMetaEntry *pOldEntry = NULL;
1,810✔
1543

1544
  // fetch the entry
1545
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
1,810✔
1546
  if (code) {
1,810!
UNCOV
1547
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1548
    return code;
×
1549
  }
1550

1551
  SMetaHandleParam param = {
1,810✔
1552
      .pEntry = pEntry,
1553
      .pOldEntry = pOldEntry,
1554
  };
1555

1556
  // do the drop
1557
  metaWLock(pMeta);
1,810✔
1558
  code = metaHandleNormalTableDropImpl(pMeta, &param);
1,810✔
1559
  metaULock(pMeta);
1,810✔
1560
  if (code) {
1,810!
UNCOV
1561
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1562
    metaFetchEntryFree(&pOldEntry);
×
UNCOV
1563
    return code;
×
1564
  }
1565

1566
  // update other stuff
1567
  pMeta->pVnode->config.vndStats.numOfNTables--;
1,810✔
1568
  pMeta->pVnode->config.vndStats.numOfNTimeSeries -= (pOldEntry->ntbEntry.schemaRow.nCols - 1);
1,810✔
1569

1570
#if 0
1571
  if (tbUids) {
1572
    if (taosArrayPush(tbUids, &uid) == NULL) {
1573
      rc = terrno;
1574
      goto _exit;
1575
    }
1576
  }
1577
#endif
1578

1579
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
1,810!
1580
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pOldEntry->uid, 0, NULL);
×
UNCOV
1581
    if (ret < 0) {
×
UNCOV
1582
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1583
    }
1584
  }
1585

1586
  metaFetchEntryFree(&pOldEntry);
1,810✔
1587
  return code;
1,810✔
1588
}
1589

1590
static int32_t metaHandleChildTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam, bool superDropped) {
4,518✔
1591
  int32_t code = TSDB_CODE_SUCCESS;
4,518✔
1592

1593
  const SMetaEntry *pEntry = pParam->pEntry;
4,518✔
1594
  const SMetaEntry *pChild = pParam->pOldEntry;
4,518✔
1595
  const SMetaEntry *pSuper = pParam->pSuperEntry;
4,518✔
1596

1597
  SMetaTableOp ops[] = {
4,518✔
1598
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1599
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1600
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1601
      {META_CHILD_IDX, META_TABLE_OP_DELETE},    //
1602
      {META_TAG_IDX, META_TABLE_OP_DELETE},      //
1603
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1604
      {META_TTL_IDX, META_TABLE_OP_DELETE},      //
1605
  };
1606

1607
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
36,109✔
1608
    SMetaTableOp *op = &ops[i];
31,594✔
1609

1610
    if (op->table == META_ENTRY_TABLE && superDropped) {
31,594✔
1611
      continue;
3,680✔
1612
    }
1613

1614
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
27,914✔
1615
    if (code) {
27,921✔
1616
      metaErr(TD_VID(pMeta->pVnode), code);
10!
UNCOV
1617
      return code;
×
1618
    }
1619
  }
1620

1621
  --pMeta->pVnode->config.vndStats.numOfCTables;
4,515✔
1622
  metaUpdateStbStats(pMeta, pParam->pSuperEntry->uid, -1, 0, -1);
4,515✔
1623
  int32_t ret = metaUidCacheClear(pMeta, pSuper->uid);
4,518✔
1624
  if (ret < 0) {
4,518!
UNCOV
1625
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1626
  }
1627

1628
  ret = metaTbGroupCacheClear(pMeta, pSuper->uid);
4,518✔
1629
  if (ret < 0) {
4,518!
1630
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1631
  }
1632
  return code;
4,517✔
1633
}
1634

1635
static int32_t metaHandleChildTableDrop(SMeta *pMeta, const SMetaEntry *pEntry, bool superDropped) {
4,518✔
1636
  int32_t     code = TSDB_CODE_SUCCESS;
4,518✔
1637
  SMetaEntry *pChild = NULL;
4,518✔
1638
  SMetaEntry *pSuper = NULL;
4,518✔
1639

1640
  // fetch old entry
1641
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pChild);
4,518✔
1642
  if (code) {
4,518!
1643
    metaErr(TD_VID(pMeta->pVnode), code);
×
1644
    return code;
×
1645
  }
1646

1647
  // fetch super entry
1648
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
4,518✔
1649
  if (code) {
4,518!
UNCOV
1650
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1651
    metaFetchEntryFree(&pChild);
×
UNCOV
1652
    return code;
×
1653
  }
1654

1655
  SMetaHandleParam param = {
4,518✔
1656
      .pEntry = pEntry,
1657
      .pOldEntry = pChild,
1658
      .pSuperEntry = pSuper,
1659
  };
1660

1661
  // do the drop
1662
  metaWLock(pMeta);
4,518✔
1663
  code = metaHandleChildTableDropImpl(pMeta, &param, superDropped);
4,518✔
1664
  metaULock(pMeta);
4,518✔
1665
  if (code) {
4,517!
1666
    metaErr(TD_VID(pMeta->pVnode), code);
×
1667
    metaFetchEntryFree(&pChild);
×
1668
    metaFetchEntryFree(&pSuper);
×
UNCOV
1669
    return code;
×
1670
  }
1671

1672
  // do other stuff
1673
  if (!metaTbInFilterCache(pMeta, pSuper->name, 1)) {
4,517!
1674
    int32_t      nCols = 0;
4,515✔
1675
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
4,515✔
1676
    if (metaGetStbStats(pMeta->pVnode, pSuper->uid, NULL, &nCols) == 0) {
4,515!
1677
      pStats->numOfTimeSeries -= nCols - 1;
4,518✔
1678
    }
1679
  }
1680

1681
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
4,518✔
1682
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pChild->uid, pSuper->uid, NULL);
10✔
1683
    if (ret < 0) {
10!
UNCOV
1684
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1685
    }
1686
  }
1687

1688
#if 0
1689
  if (tbUids) {
1690
    if (taosArrayPush(tbUids, &uid) == NULL) {
1691
      rc = terrno;
1692
      goto _exit;
1693
    }
1694
  }
1695

1696
  if ((type == TSDB_CHILD_TABLE) && tbUid) {
1697
    *tbUid = uid;
1698
  }
1699
#endif
1700
  metaFetchEntryFree(&pChild);
4,518✔
1701
  metaFetchEntryFree(&pSuper);
4,517✔
1702
  return code;
4,518✔
1703
}
1704

UNCOV
1705
static int32_t metaHandleVirtualNormalTableDropImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
×
UNCOV
1706
  int32_t code = TSDB_CODE_SUCCESS;
×
1707

UNCOV
1708
  SMetaTableOp ops[] = {
×
1709
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1710
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1711
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1712
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1713

1714
      // {META_SCHEMA_TABLE, META_TABLE_OP_DELETE},  //
1715
  };
1716

UNCOV
1717
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
×
UNCOV
1718
    SMetaTableOp *op = &ops[i];
×
UNCOV
1719
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
×
1720
    if (code) {
×
1721
      const SMetaEntry *pEntry = pParam->pEntry;
×
UNCOV
1722
      metaErr(TD_VID(pMeta->pVnode), code);
×
1723
    }
1724
  }
1725

1726
  return code;
×
1727
}
1728

UNCOV
1729
static int32_t metaHandleVirtualNormalTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
×
UNCOV
1730
  int32_t     code = TSDB_CODE_SUCCESS;
×
UNCOV
1731
  SMetaEntry *pOldEntry = NULL;
×
1732

1733
  // fetch the entry
UNCOV
1734
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
×
UNCOV
1735
  if (code) {
×
UNCOV
1736
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1737
    return code;
×
1738
  }
1739

UNCOV
1740
  SMetaHandleParam param = {
×
1741
      .pEntry = pEntry,
1742
      .pOldEntry = pOldEntry,
1743
  };
1744

1745
  // do the drop
UNCOV
1746
  metaWLock(pMeta);
×
UNCOV
1747
  code = metaHandleVirtualNormalTableDropImpl(pMeta, &param);
×
UNCOV
1748
  metaULock(pMeta);
×
UNCOV
1749
  if (code) {
×
UNCOV
1750
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1751
    metaFetchEntryFree(&pOldEntry);
×
UNCOV
1752
    return code;
×
1753
  }
1754

1755
  // update other stuff
UNCOV
1756
  pMeta->pVnode->config.vndStats.numOfVTables--;
×
1757

1758
#if 0
1759
  if (tbUids) {
1760
    if (taosArrayPush(tbUids, &uid) == NULL) {
1761
      rc = terrno;
1762
      goto _exit;
1763
    }
1764
  }
1765
#endif
1766

UNCOV
1767
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
×
UNCOV
1768
    int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, pOldEntry->uid, 0, NULL);
×
UNCOV
1769
    if (ret < 0) {
×
UNCOV
1770
      metaErr(TD_VID(pMeta->pVnode), ret);
×
1771
    }
1772
  }
1773

1774
  metaFetchEntryFree(&pOldEntry);
×
UNCOV
1775
  return code;
×
1776
}
1777

UNCOV
1778
static int32_t metaHandleVirtualChildTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam, bool superDropped) {
×
UNCOV
1779
  int32_t code = TSDB_CODE_SUCCESS;
×
1780

UNCOV
1781
  const SMetaEntry *pEntry = pParam->pEntry;
×
UNCOV
1782
  const SMetaEntry *pChild = pParam->pOldEntry;
×
UNCOV
1783
  const SMetaEntry *pSuper = pParam->pSuperEntry;
×
1784

UNCOV
1785
  SMetaTableOp ops[] = {
×
1786
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1787
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1788
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1789
      {META_CHILD_IDX, META_TABLE_OP_DELETE},    //
1790
      {META_TAG_IDX, META_TABLE_OP_DELETE},      //
1791
      {META_BTIME_IDX, META_TABLE_OP_DELETE},    //
1792
  };
1793

UNCOV
1794
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
×
UNCOV
1795
    SMetaTableOp *op = &ops[i];
×
1796

UNCOV
1797
    if (op->table == META_ENTRY_TABLE && superDropped) {
×
UNCOV
1798
      continue;
×
1799
    }
1800

UNCOV
1801
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
×
UNCOV
1802
    if (code) {
×
UNCOV
1803
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1804
      return code;
×
1805
    }
1806
  }
1807

UNCOV
1808
  --pMeta->pVnode->config.vndStats.numOfVCTables;
×
UNCOV
1809
  metaUpdateStbStats(pMeta, pParam->pSuperEntry->uid, -1, 0, -1);
×
UNCOV
1810
  int32_t ret = metaUidCacheClear(pMeta, pSuper->uid);
×
UNCOV
1811
  if (ret < 0) {
×
UNCOV
1812
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1813
  }
1814

UNCOV
1815
  ret = metaTbGroupCacheClear(pMeta, pSuper->uid);
×
UNCOV
1816
  if (ret < 0) {
×
UNCOV
1817
    metaErr(TD_VID(pMeta->pVnode), ret);
×
1818
  }
UNCOV
1819
  return code;
×
1820
}
1821

UNCOV
1822
static int32_t metaHandleVirtualChildTableDrop(SMeta *pMeta, const SMetaEntry *pEntry, bool superDropped) {
×
UNCOV
1823
  int32_t     code = TSDB_CODE_SUCCESS;
×
UNCOV
1824
  SMetaEntry *pChild = NULL;
×
UNCOV
1825
  SMetaEntry *pSuper = NULL;
×
1826

1827
  // fetch old entry
1828
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pChild);
×
UNCOV
1829
  if (code) {
×
UNCOV
1830
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1831
    return code;
×
1832
  }
1833

1834
  // fetch super entry
UNCOV
1835
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
×
UNCOV
1836
  if (code) {
×
UNCOV
1837
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1838
    metaFetchEntryFree(&pChild);
×
UNCOV
1839
    return code;
×
1840
  }
1841

UNCOV
1842
  SMetaHandleParam param = {
×
1843
      .pEntry = pEntry,
1844
      .pOldEntry = pChild,
1845
      .pSuperEntry = pSuper,
1846
  };
1847

1848
  // do the drop
UNCOV
1849
  metaWLock(pMeta);
×
UNCOV
1850
  code = metaHandleVirtualChildTableDropImpl(pMeta, &param, superDropped);
×
UNCOV
1851
  metaULock(pMeta);
×
UNCOV
1852
  if (code) {
×
UNCOV
1853
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1854
    metaFetchEntryFree(&pChild);
×
UNCOV
1855
    metaFetchEntryFree(&pSuper);
×
UNCOV
1856
    return code;
×
1857
  }
1858

1859
  metaFetchEntryFree(&pChild);
×
1860
  metaFetchEntryFree(&pSuper);
×
1861
  return code;
×
1862
}
1863

1864
static int32_t metaGetChildUidsOfSuperTable(SMeta *pMeta, tb_uid_t suid, SArray **childList) {
5,853✔
1865
  int32_t code = TSDB_CODE_SUCCESS;
5,853✔
1866
  void   *key = NULL;
5,853✔
1867
  int32_t keySize = 0;
5,853✔
1868
  int32_t c;
1869

1870
  *childList = taosArrayInit(64, sizeof(tb_uid_t));
5,853✔
1871
  if (*childList == NULL) {
5,861!
UNCOV
1872
    return terrno;
×
1873
  }
1874

1875
  TBC *cursor = NULL;
5,861✔
1876
  code = tdbTbcOpen(pMeta->pCtbIdx, &cursor, NULL);
5,861✔
1877
  if (code) {
5,862!
UNCOV
1878
    taosArrayDestroy(*childList);
×
UNCOV
1879
    *childList = NULL;
×
UNCOV
1880
    return code;
×
1881
  }
1882

1883
  int32_t rc = tdbTbcMoveTo(cursor,
5,862✔
1884
                            &(SCtbIdxKey){
5,862✔
1885
                                .suid = suid,
1886
                                .uid = INT64_MIN,
1887
                            },
1888
                            sizeof(SCtbIdxKey), &c);
1889
  if (rc < 0) {
5,859!
UNCOV
1890
    tdbTbcClose(cursor);
×
UNCOV
1891
    return 0;
×
1892
  }
1893

1894
  for (;;) {
1895
    if (tdbTbcNext(cursor, &key, &keySize, NULL, NULL) < 0) {
17,750✔
1896
      break;
3,631✔
1897
    }
1898

1899
    if (((SCtbIdxKey *)key)->suid < suid) {
14,111✔
1900
      continue;
2,438✔
1901
    } else if (((SCtbIdxKey *)key)->suid > suid) {
11,673✔
1902
      break;
2,229✔
1903
    }
1904

1905
    if (taosArrayPush(*childList, &(((SCtbIdxKey *)key)->uid)) == NULL) {
18,897!
UNCOV
1906
      tdbFreeClear(key);
×
UNCOV
1907
      tdbTbcClose(cursor);
×
UNCOV
1908
      taosArrayDestroy(*childList);
×
UNCOV
1909
      *childList = NULL;
×
UNCOV
1910
      return terrno;
×
1911
    }
1912
  }
1913

1914
  tdbTbcClose(cursor);
5,860✔
1915
  tdbFreeClear(key);
5,864✔
1916
  return code;
5,864✔
1917
}
1918

1919
static int32_t metaHandleSuperTableDropImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
2,517✔
1920
  int32_t           code = TSDB_CODE_SUCCESS;
2,517✔
1921
  const SMetaEntry *pEntry = pParam->pEntry;
2,517✔
1922

1923
  SMetaTableOp ops[] = {
2,517✔
1924
      {META_ENTRY_TABLE, META_TABLE_OP_DELETE},  //
1925
      {META_UID_IDX, META_TABLE_OP_DELETE},      //
1926
      {META_NAME_IDX, META_TABLE_OP_DELETE},     //
1927
      {META_SUID_IDX, META_TABLE_OP_DELETE},     //
1928

1929
      // {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  // TODO: here should be insert
1930
  };
1931

1932
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
12,587✔
1933
    SMetaTableOp *op = &ops[i];
10,071✔
1934

1935
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
10,071✔
1936
    if (TSDB_CODE_SUCCESS != code) {
10,070!
1937
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1938
      return code;
×
1939
    }
1940
  }
1941

1942
  int32_t ret = metaStatsCacheDrop(pMeta, pEntry->uid);
2,516✔
1943
  if (ret < 0) {
2,518✔
1944
    metaErr(TD_VID(pMeta->pVnode), ret);
375!
1945
  }
1946
  return code;
2,517✔
1947
}
1948

1949
static int32_t metaHandleNormalTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
346✔
1950
  int32_t code = TSDB_CODE_SUCCESS;
346✔
1951

1952
  const SMetaEntry *pEntry = pParam->pEntry;
346✔
1953

1954
  SMetaTableOp ops[] = {
346✔
1955
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
1956
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
1957
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
1958
      {META_TTL_IDX, META_TABLE_OP_UPDATA},       //
1959
  };
1960
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
1,730✔
1961
    SMetaTableOp *op = &ops[i];
1,384✔
1962
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
1,384✔
1963
    if (code) {
1,384!
UNCOV
1964
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1965
      return code;
×
1966
    }
1967
  }
1968
#if 0
1969
  if (metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs) < 0) {
1970
    metaError("vgId:%d, failed to update change time:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
1971
  }
1972
#endif
1973
  return code;
346✔
1974
}
1975

UNCOV
1976
static int32_t metaHandleVirtualNormalTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
UNCOV
1977
  int32_t code = TSDB_CODE_SUCCESS;
×
1978

UNCOV
1979
  const SMetaEntry *pEntry = pParam->pEntry;
×
1980

UNCOV
1981
  SMetaTableOp ops[] = {
×
1982
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
1983
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
1984
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
1985
  };
UNCOV
1986
  for (int32_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
×
UNCOV
1987
    SMetaTableOp *op = &ops[i];
×
UNCOV
1988
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
×
UNCOV
1989
    if (code) {
×
UNCOV
1990
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
1991
      return code;
×
1992
    }
1993
  }
1994
#if 0
1995
  if (metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs) < 0) {
1996
    metaError("vgId:%d, failed to update change time:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
1997
  }
1998
#endif
UNCOV
1999
  return code;
×
2000
}
2001

UNCOV
2002
static int32_t metaHandleVirtualChildTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
×
UNCOV
2003
  int32_t code = TSDB_CODE_SUCCESS;
×
2004

UNCOV
2005
  const SMetaEntry *pEntry = pParam->pEntry;
×
UNCOV
2006
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
×
UNCOV
2007
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
×
2008

UNCOV
2009
  SMetaTableOp ops[] = {
×
2010
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},  //
2011
      {META_UID_IDX, META_TABLE_OP_UPDATA},      //
2012
      {META_TAG_IDX, META_TABLE_OP_UPDATA},      //
2013
      {META_CHILD_IDX, META_TABLE_OP_UPDATA},    //
2014
  };
2015

UNCOV
2016
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
×
UNCOV
2017
    SMetaTableOp *op = &ops[i];
×
UNCOV
2018
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
×
UNCOV
2019
    if (code) {
×
UNCOV
2020
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2021
      return code;
×
2022
    }
2023
  }
2024

UNCOV
2025
  if (metaUidCacheClear(pMeta, pSuperEntry->uid) < 0) {
×
UNCOV
2026
    metaErr(TD_VID(pMeta->pVnode), code);
×
2027
  }
2028

UNCOV
2029
  if (metaTbGroupCacheClear(pMeta, pSuperEntry->uid) < 0) {
×
UNCOV
2030
    metaErr(TD_VID(pMeta->pVnode), code);
×
2031
  }
UNCOV
2032
  return code;
×
2033
}
2034

2035
static int32_t metaHandleChildTableUpdateImpl(SMeta *pMeta, const SMetaHandleParam *pParam) {
700✔
2036
  int32_t code = TSDB_CODE_SUCCESS;
700✔
2037

2038
  const SMetaEntry *pEntry = pParam->pEntry;
700✔
2039
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
700✔
2040
  const SMetaEntry *pSuperEntry = pParam->pSuperEntry;
700✔
2041

2042
  SMetaTableOp ops[] = {
700✔
2043
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},  //
2044
      {META_UID_IDX, META_TABLE_OP_UPDATA},      //
2045
      {META_TAG_IDX, META_TABLE_OP_UPDATA},      //
2046
      {META_CHILD_IDX, META_TABLE_OP_UPDATA},    //
2047
      {META_TTL_IDX, META_TABLE_OP_UPDATA},      //
2048
  };
2049

2050
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
4,200✔
2051
    SMetaTableOp *op = &ops[i];
3,500✔
2052
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
3,500✔
2053
    if (code) {
3,500!
UNCOV
2054
      metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2055
      return code;
×
2056
    }
2057
  }
2058

2059
  if (metaUidCacheClear(pMeta, pSuperEntry->uid) < 0) {
700!
UNCOV
2060
    metaErr(TD_VID(pMeta->pVnode), code);
×
2061
  }
2062

2063
  if (metaTbGroupCacheClear(pMeta, pSuperEntry->uid) < 0) {
700!
UNCOV
2064
    metaErr(TD_VID(pMeta->pVnode), code);
×
2065
  }
2066
  return code;
700✔
2067
#if 0
2068
  if (metaUpdateChangeTime(pMeta, ctbEntry.uid, pReq->ctimeMs) < 0) {
2069
    metaError("meta/table: failed to update change time:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
2070
  }
2071
#endif
2072
}
2073

2074
static int32_t metaHandleSuperTableUpdateImpl(SMeta *pMeta, SMetaHandleParam *pParam) {
9,511✔
2075
  int32_t code = TSDB_CODE_SUCCESS;
9,511✔
2076

2077
  const SMetaEntry *pEntry = pParam->pEntry;
9,511✔
2078
  const SMetaEntry *pOldEntry = pParam->pOldEntry;
9,511✔
2079

2080
  SMetaTableOp ops[] = {
9,511✔
2081
      {META_ENTRY_TABLE, META_TABLE_OP_UPDATA},   //
2082
      {META_UID_IDX, META_TABLE_OP_UPDATA},       //
2083
      {META_SCHEMA_TABLE, META_TABLE_OP_UPDATA},  //
2084
  };
2085

2086
  for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
38,040✔
2087
    SMetaTableOp *op = &ops[i];
28,533✔
2088
    code = metaTableOpFn[op->table][op->op](pMeta, pParam);
28,533✔
2089
    if (code) {
28,533✔
2090
      metaErr(TD_VID(pMeta->pVnode), code);
4!
UNCOV
2091
      return code;
×
2092
    }
2093
  }
2094

2095
  if (TSDB_CODE_SUCCESS == code) {
9,507!
2096
    metaUpdateStbStats(pMeta, pEntry->uid, 0, pEntry->stbEntry.schemaRow.nCols - pOldEntry->stbEntry.schemaRow.nCols,
9,513✔
2097
                       pEntry->stbEntry.keep);
9,513✔
2098
  }
2099

2100
  return code;
9,517✔
2101
}
2102

2103
static int32_t metaHandleSuperTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
9,513✔
2104
  int32_t code = TSDB_CODE_SUCCESS;
9,513✔
2105

2106
  SMetaEntry *pOldEntry = NULL;
9,513✔
2107

2108
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
9,513✔
2109
  if (code) {
9,517!
UNCOV
2110
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2111
    return code;
×
2112
  }
2113

2114
  SMetaHandleParam param = {
9,517✔
2115
      .pEntry = pEntry,
2116
      .pOldEntry = pOldEntry,
2117
  };
2118
  metaWLock(pMeta);
9,517✔
2119
  code = metaHandleSuperTableUpdateImpl(pMeta, &param);
9,519✔
2120
  metaULock(pMeta);
9,507✔
2121
  if (code) {
9,513!
UNCOV
2122
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2123
    metaFetchEntryFree(&pOldEntry);
×
UNCOV
2124
    return code;
×
2125
  }
2126

2127
  int     nCols = pEntry->stbEntry.schemaRow.nCols;
9,513✔
2128
  int     onCols = pOldEntry->stbEntry.schemaRow.nCols;
9,513✔
2129
  int32_t deltaCol = nCols - onCols;
9,513✔
2130
  bool    updStat = deltaCol != 0 && !metaTbInFilterCache(pMeta, pEntry->name, 1);
9,513!
2131

2132
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
9,509✔
2133
    STsdb  *pTsdb = pMeta->pVnode->pTsdb;
2,018✔
2134
    SArray *uids = NULL; /*taosArrayInit(8, sizeof(int64_t));
2,018✔
2135
     if (uids == NULL) {
2136
       metaErr(TD_VID(pMeta->pVnode), code);
2137
       metaFetchEntryFree(&pOldEntry);
2138
       return terrno;
2139
       }*/
2140
    if (deltaCol == 1) {
2,018✔
2141
      int16_t cid = pEntry->stbEntry.schemaRow.pSchema[nCols - 1].colId;
136✔
2142
      int8_t  col_type = pEntry->stbEntry.schemaRow.pSchema[nCols - 1].type;
136✔
2143

2144
      code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &uids);
136✔
2145
      if (code) {
136!
UNCOV
2146
        metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2147
        metaFetchEntryFree(&pOldEntry);
×
UNCOV
2148
        return code;
×
2149
      }
2150
      TAOS_CHECK_RETURN(tsdbCacheNewSTableColumn(pTsdb, uids, cid, col_type));
136!
2151
    } else if (deltaCol == -1) {
1,882✔
2152
      int16_t cid = -1;
118✔
2153
      bool    hasPrimaryKey = false;
118✔
2154
      if (onCols >= 2) {
118!
2155
        hasPrimaryKey = (pOldEntry->stbEntry.schemaRow.pSchema[1].flags & COL_IS_KEY) ? true : false;
118✔
2156
      }
2157
      for (int i = 0, j = 0; i < nCols && j < onCols; ++i, ++j) {
934✔
2158
        if (pEntry->stbEntry.schemaRow.pSchema[i].colId != pOldEntry->stbEntry.schemaRow.pSchema[j].colId) {
884✔
2159
          cid = pOldEntry->stbEntry.schemaRow.pSchema[j].colId;
68✔
2160
          break;
68✔
2161
        }
2162
      }
2163

2164
      if (cid != -1) {
118✔
2165
        code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &uids);
68✔
2166
        if (code) {
68!
UNCOV
2167
          metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2168
          metaFetchEntryFree(&pOldEntry);
×
UNCOV
2169
          return code;
×
2170
        }
2171
        TAOS_CHECK_RETURN(tsdbCacheDropSTableColumn(pTsdb, uids, cid, hasPrimaryKey));
68!
2172
      }
2173
    }
2174
    if (uids) taosArrayDestroy(uids);
2,018✔
2175

2176
    tsdbCacheInvalidateSchema(pTsdb, pEntry->uid, -1, pEntry->stbEntry.schemaRow.version);
2,018✔
2177
  }
2178
  if (updStat) {
9,508✔
2179
    int64_t ctbNum = 0;
3,061✔
2180
    int32_t ret = metaGetStbStats(pMeta->pVnode, pEntry->uid, &ctbNum, NULL);
3,061✔
2181
    if (ret < 0) {
3,066!
UNCOV
2182
      metaError("vgId:%d, failed to get stb stats:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
×
2183
                pEntry->uid, tstrerror(ret));
2184
    }
2185
    pMeta->pVnode->config.vndStats.numOfTimeSeries += (ctbNum * deltaCol);
3,065✔
2186
    if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
3,065✔
2187
  }
2188
  metaFetchEntryFree(&pOldEntry);
9,512✔
2189
  return code;
9,516✔
2190
}
2191

2192
static int32_t metaHandleChildTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
700✔
2193
  int32_t code = TSDB_CODE_SUCCESS;
700✔
2194

2195
  SMetaEntry *pOldEntry = NULL;
700✔
2196
  SMetaEntry *pSuperEntry = NULL;
700✔
2197

2198
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
700✔
2199
  if (code) {
700!
UNCOV
2200
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2201
    return code;
×
2202
  }
2203

2204
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
700✔
2205
  if (code) {
700!
UNCOV
2206
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2207
    metaFetchEntryFree(&pOldEntry);
×
UNCOV
2208
    return code;
×
2209
  }
2210

2211
  SMetaHandleParam param = {
700✔
2212
      .pEntry = pEntry,
2213
      .pOldEntry = pOldEntry,
2214
      .pSuperEntry = pSuperEntry,
2215
  };
2216

2217
  metaWLock(pMeta);
700✔
2218
  code = metaHandleChildTableUpdateImpl(pMeta, &param);
700✔
2219
  metaULock(pMeta);
700✔
2220
  if (code) {
700!
UNCOV
2221
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2222
    metaFetchEntryFree(&pOldEntry);
×
UNCOV
2223
    metaFetchEntryFree(&pSuperEntry);
×
UNCOV
2224
    return code;
×
2225
  }
2226

2227
  metaFetchEntryFree(&pOldEntry);
700✔
2228
  metaFetchEntryFree(&pSuperEntry);
700✔
2229
  return code;
700✔
2230
}
2231

2232
static int32_t metaHandleNormalTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
346✔
2233
  int32_t     code = TSDB_CODE_SUCCESS;
346✔
2234
  SMetaEntry *pOldEntry = NULL;
346✔
2235

2236
  // fetch old entry
2237
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
346✔
2238
  if (code) {
346!
UNCOV
2239
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2240
    return code;
×
2241
  }
2242

2243
  // handle update
2244
  SMetaHandleParam param = {
346✔
2245
      .pEntry = pEntry,
2246
      .pOldEntry = pOldEntry,
2247
  };
2248
  metaWLock(pMeta);
346✔
2249
  code = metaHandleNormalTableUpdateImpl(pMeta, &param);
346✔
2250
  metaULock(pMeta);
346✔
2251
  if (code) {
346!
UNCOV
2252
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2253
    metaFetchEntryFree(&pOldEntry);
×
UNCOV
2254
    return code;
×
2255
  }
2256

2257
  // do other stuff
2258
  if (!TSDB_CACHE_NO(pMeta->pVnode->config) &&
346✔
2259
      pEntry->ntbEntry.schemaRow.version != pOldEntry->ntbEntry.schemaRow.version) {
119!
2260
#if 0
2261
    {  // for add column
2262
      int16_t cid = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].colId;
2263
      int8_t  col_type = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].type;
2264
      int32_t ret = tsdbCacheNewNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, col_type);
2265
      if (ret < 0) {
2266
        terrno = ret;
2267
        goto _err;
2268
      }
2269
    }
2270
    {  // for drop column
2271

2272
      if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
2273
        int16_t cid = pColumn->colId;
2274

2275
        if (tsdbCacheDropNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, hasPrimayKey) != 0) {
2276
          metaError("vgId:%d, failed to drop ntable column:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name,
2277
                    entry.uid);
2278
        }
2279
        tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, entry.uid, pSchema->version);
2280
      }
2281
    }
2282
    }
2283
#endif
2284
    tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, pEntry->uid, pEntry->ntbEntry.schemaRow.version);
119✔
2285
  }
2286
  int32_t deltaCol = pEntry->ntbEntry.schemaRow.nCols - pOldEntry->ntbEntry.schemaRow.nCols;
346✔
2287
  pMeta->pVnode->config.vndStats.numOfNTimeSeries += deltaCol;  
346✔
2288
  if (deltaCol > 0) metaTimeSeriesNotifyCheck(pMeta);
346✔
2289
  metaFetchEntryFree(&pOldEntry);
346✔
2290
  return code;
346✔
2291
}
2292

UNCOV
2293
static int32_t metaHandleVirtualNormalTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
×
UNCOV
2294
  int32_t     code = TSDB_CODE_SUCCESS;
×
UNCOV
2295
  SMetaEntry *pOldEntry = NULL;
×
2296

2297
  // fetch old entry
UNCOV
2298
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
×
UNCOV
2299
  if (code) {
×
UNCOV
2300
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2301
    return code;
×
2302
  }
2303

2304
  // handle update
UNCOV
2305
  SMetaHandleParam param = {
×
2306
      .pEntry = pEntry,
2307
      .pOldEntry = pOldEntry,
2308
  };
UNCOV
2309
  metaWLock(pMeta);
×
UNCOV
2310
  code = metaHandleVirtualNormalTableUpdateImpl(pMeta, &param);
×
UNCOV
2311
  metaULock(pMeta);
×
UNCOV
2312
  if (code) {
×
UNCOV
2313
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2314
    metaFetchEntryFree(&pOldEntry);
×
UNCOV
2315
    return code;
×
2316
  }
2317

UNCOV
2318
  metaTimeSeriesNotifyCheck(pMeta);
×
UNCOV
2319
  metaFetchEntryFree(&pOldEntry);
×
UNCOV
2320
  return code;
×
2321
}
2322

UNCOV
2323
static int32_t metaHandleVirtualChildTableUpdate(SMeta *pMeta, const SMetaEntry *pEntry) {
×
UNCOV
2324
  int32_t code = TSDB_CODE_SUCCESS;
×
2325

UNCOV
2326
  SMetaEntry *pOldEntry = NULL;
×
UNCOV
2327
  SMetaEntry *pSuperEntry = NULL;
×
2328

UNCOV
2329
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
×
UNCOV
2330
  if (code) {
×
UNCOV
2331
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2332
    return code;
×
2333
  }
2334

UNCOV
2335
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuperEntry);
×
UNCOV
2336
  if (code) {
×
UNCOV
2337
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2338
    metaFetchEntryFree(&pOldEntry);
×
UNCOV
2339
    return code;
×
2340
  }
2341

UNCOV
2342
  SMetaHandleParam param = {
×
2343
      .pEntry = pEntry,
2344
      .pOldEntry = pOldEntry,
2345
      .pSuperEntry = pSuperEntry,
2346
  };
2347

UNCOV
2348
  metaWLock(pMeta);
×
UNCOV
2349
  code = metaHandleVirtualChildTableUpdateImpl(pMeta, &param);
×
UNCOV
2350
  metaULock(pMeta);
×
UNCOV
2351
  if (code) {
×
UNCOV
2352
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2353
    metaFetchEntryFree(&pOldEntry);
×
UNCOV
2354
    metaFetchEntryFree(&pSuperEntry);
×
UNCOV
2355
    return code;
×
2356
  }
2357

UNCOV
2358
  metaFetchEntryFree(&pOldEntry);
×
UNCOV
2359
  metaFetchEntryFree(&pSuperEntry);
×
UNCOV
2360
  return code;
×
2361
}
2362

2363
static int32_t metaHandleSuperTableDrop(SMeta *pMeta, const SMetaEntry *pEntry) {
2,504✔
2364
  int32_t     code = TSDB_CODE_SUCCESS;
2,504✔
2365
  SArray     *childList = NULL;
2,504✔
2366
  SMetaEntry *pOldEntry = NULL;
2,504✔
2367

2368
  code = metaFetchEntryByUid(pMeta, pEntry->uid, &pOldEntry);
2,504✔
2369
  if (code) {
2,514!
UNCOV
2370
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2371
    return code;
×
2372
  }
2373

2374
  code = metaGetChildUidsOfSuperTable(pMeta, pEntry->uid, &childList);
2,514✔
2375
  if (code) {
2,518!
UNCOV
2376
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2377
    metaFetchEntryFree(&pOldEntry);
×
UNCOV
2378
    return code;
×
2379
  }
2380

2381
  if (tsdbCacheDropSubTables(pMeta->pVnode->pTsdb, childList, pEntry->uid) < 0) {
2,518!
UNCOV
2382
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pEntry->name,
×
2383
              pEntry->uid, tstrerror(terrno));
2384
  }
2385

2386
  // loop to drop all child tables
2387
  for (int32_t i = 0; i < taosArrayGetSize(childList); i++) {
6,198✔
2388
    SMetaEntry childEntry = {
7,360✔
2389
        .version = pEntry->version,
3,680✔
2390
        .uid = *(tb_uid_t *)taosArrayGet(childList, i),
3,680✔
2391
        .type = -TSDB_CHILD_TABLE,
2392
    };
2393

2394
    code = metaHandleChildTableDrop(pMeta, &childEntry, true);
3,680✔
2395
    if (code) {
3,680!
UNCOV
2396
      metaErr(TD_VID(pMeta->pVnode), code);
×
2397
    }
2398
  }
2399

2400
  // do drop super table
2401
  SMetaHandleParam param = {
2,518✔
2402
      .pEntry = pEntry,
2403
      .pOldEntry = pOldEntry,
2404
  };
2405
  metaWLock(pMeta);
2,518✔
2406
  code = metaHandleSuperTableDropImpl(pMeta, &param);
2,517✔
2407
  metaULock(pMeta);
2,518✔
2408
  if (code) {
2,518!
UNCOV
2409
    metaErr(TD_VID(pMeta->pVnode), code);
×
UNCOV
2410
    taosArrayDestroy(childList);
×
UNCOV
2411
    metaFetchEntryFree(&pOldEntry);
×
UNCOV
2412
    return code;
×
2413
  }
2414

2415
  // do other stuff
2416
  metaUpdTimeSeriesNum(pMeta);
2,518✔
2417

2418
  // free resource and return
2419
  taosArrayDestroy(childList);
2,518✔
2420
  metaFetchEntryFree(&pOldEntry);
2,518✔
2421
  return code;
2,518✔
2422
}
2423

2424
int32_t metaHandleEntry2(SMeta *pMeta, const SMetaEntry *pEntry) {
201,229✔
2425
  int32_t   code = TSDB_CODE_SUCCESS;
201,229✔
2426
  int32_t   vgId = TD_VID(pMeta->pVnode);
201,229✔
2427
  SMetaInfo info = {0};
201,229✔
2428
  int8_t    type = pEntry->type > 0 ? pEntry->type : -pEntry->type;
201,229✔
2429

2430
  if (NULL == pMeta || NULL == pEntry) {
201,229!
UNCOV
2431
    metaError("%s failed at %s:%d since invalid parameter", __func__, __FILE__, __LINE__);
×
UNCOV
2432
    return TSDB_CODE_INVALID_PARA;
×
2433
  }
2434

2435
  if (pEntry->type > 0) {
201,256✔
2436
    bool isExist = false;
196,118✔
2437
    if (TSDB_CODE_SUCCESS == metaGetInfo(pMeta, pEntry->uid, &info, NULL)) {
196,118✔
2438
      isExist = true;
10,561✔
2439
    }
2440

2441
    switch (type) {
196,233!
2442
      case TSDB_SUPER_TABLE: {
37,346✔
2443
        if (isExist) {
37,346✔
2444
          code = metaHandleSuperTableUpdate(pMeta, pEntry);
9,515✔
2445
        } else {
2446
          code = metaHandleSuperTableCreate(pMeta, pEntry);
27,831✔
2447
        }
2448
        break;
37,400✔
2449
      }
2450
      case TSDB_CHILD_TABLE: {
144,152✔
2451
        if (isExist) {
144,152✔
2452
          code = metaHandleChildTableUpdate(pMeta, pEntry);
700✔
2453
        } else {
2454
          code = metaHandleChildTableCreate(pMeta, pEntry);
143,452✔
2455
        }
2456
        break;
144,147✔
2457
      }
2458
      case TSDB_NORMAL_TABLE: {
14,687✔
2459
        if (isExist) {
14,687✔
2460
          code = metaHandleNormalTableUpdate(pMeta, pEntry);
346✔
2461
        } else {
2462
          code = metaHandleNormalTableCreate(pMeta, pEntry);
14,341✔
2463
        }
2464
        break;
14,688✔
2465
      }
UNCOV
2466
      case TSDB_VIRTUAL_NORMAL_TABLE: {
×
UNCOV
2467
        if (isExist) {
×
UNCOV
2468
          code = metaHandleVirtualNormalTableUpdate(pMeta, pEntry);
×
2469
        } else {
UNCOV
2470
          code = metaHandleVirtualNormalTableCreate(pMeta, pEntry);
×
2471
        }
UNCOV
2472
        break;
×
2473
      }
UNCOV
2474
      case TSDB_VIRTUAL_CHILD_TABLE: {
×
UNCOV
2475
        if (isExist) {
×
UNCOV
2476
          code = metaHandleVirtualChildTableUpdate(pMeta, pEntry);
×
2477
        } else {
UNCOV
2478
          code = metaHandleVirtualChildTableCreate(pMeta, pEntry);
×
2479
        }
UNCOV
2480
        break;
×
2481
      }
2482
      default: {
48✔
2483
        code = TSDB_CODE_INVALID_PARA;
48✔
2484
        break;
48✔
2485
      }
2486
    }
2487
  } else {
2488
    switch (type) {
5,138!
2489
      case TSDB_SUPER_TABLE: {
2,506✔
2490
        code = metaHandleSuperTableDrop(pMeta, pEntry);
2,506✔
2491
        break;
2,518✔
2492
      }
2493
      case TSDB_CHILD_TABLE: {
838✔
2494
        code = metaHandleChildTableDrop(pMeta, pEntry, false);
838✔
2495
        break;
838✔
2496
      }
2497
      case TSDB_NORMAL_TABLE: {
1,810✔
2498
        code = metaHandleNormalTableDrop(pMeta, pEntry);
1,810✔
2499
        break;
1,810✔
2500
      }
UNCOV
2501
      case TSDB_VIRTUAL_NORMAL_TABLE: {
×
UNCOV
2502
        code = metaHandleVirtualNormalTableDrop(pMeta, pEntry);
×
UNCOV
2503
        break;
×
2504
      }
UNCOV
2505
      case TSDB_VIRTUAL_CHILD_TABLE: {
×
UNCOV
2506
        code = metaHandleVirtualChildTableDrop(pMeta, pEntry, false);
×
UNCOV
2507
        break;
×
2508
      }
UNCOV
2509
      default: {
×
UNCOV
2510
        code = TSDB_CODE_INVALID_PARA;
×
UNCOV
2511
        break;
×
2512
      }
2513
    }
2514
  }
2515

2516
  if (TSDB_CODE_SUCCESS == code) {
201,433✔
2517
    pMeta->changed = true;
201,404✔
2518
    metaDebug("vgId:%d, %s success, version:%" PRId64 " type:%d uid:%" PRId64 " name:%s", vgId, __func__,
201,404✔
2519
              pEntry->version, pEntry->type, pEntry->uid, pEntry->type > 0 ? pEntry->name : "");
2520
  } else {
2521
    metaErr(vgId, code);
29!
2522
  }
2523
  TAOS_RETURN(code);
201,436✔
2524
}
2525

2526
void metaHandleSyncEntry(SMeta *pMeta, const SMetaEntry *pEntry) {
5,851✔
2527
  int32_t code = TSDB_CODE_SUCCESS;
5,851✔
2528
  code = metaHandleEntry2(pMeta, pEntry);
5,851✔
2529
  if (code) {
5,851!
UNCOV
2530
    metaErr(TD_VID(pMeta->pVnode), code);
×
2531
  }
2532
  return;
5,851✔
2533
}
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