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

taosdata / TDengine / #3558

17 Dec 2024 06:05AM UTC coverage: 59.778% (+1.6%) from 58.204%
#3558

push

travis-ci

web-flow
Merge pull request #29179 from taosdata/merge/mainto3.0

merge: form main to 3.0 branch

132787 of 287595 branches covered (46.17%)

Branch coverage included in aggregate %.

104 of 191 new or added lines in 5 files covered. (54.45%)

6085 existing lines in 168 files now uncovered.

209348 of 284746 relevant lines covered (73.52%)

8164844.48 hits per line

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

15.84
/source/dnode/vnode/src/meta/metaTable.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "meta.h"
17

18
extern SDmNotifyHandle dmNotifyHdl;
19

20
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
21
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
22
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
23
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
24
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq);
25
int32_t metaUpdateTableMultiTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq);
26
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq);
27
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq);
28

29
int32_t metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema);
30

31
int32_t     metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema);
32
static int  metaSaveToTbDb(SMeta *pMeta, const SMetaEntry *pME);
33
static int  metaUpdateUidIdx(SMeta *pMeta, const SMetaEntry *pME);
34
static int  metaUpdateNameIdx(SMeta *pMeta, const SMetaEntry *pME);
35
static void metaUpdateTtl(SMeta *pMeta, const SMetaEntry *pME);
36
static int  metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs);
37
static int  metaSaveToSkmDb(SMeta *pMeta, const SMetaEntry *pME);
38
static int  metaUpdateCtbIdx(SMeta *pMeta, const SMetaEntry *pME);
39
static int  metaUpdateSuidIdx(SMeta *pMeta, const SMetaEntry *pME);
40
static int  metaUpdateTagIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry);
41
static int  metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl);
42
void        metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey);
43
// opt ins_tables query
44
static int metaUpdateBtimeIdx(SMeta *pMeta, const SMetaEntry *pME);
45
static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME);
46
static int metaUpdateNcolIdx(SMeta *pMeta, const SMetaEntry *pME);
47
static int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME);
48

49
int metaHandleEntry(SMeta *pMeta, const SMetaEntry *pME);
50

51
int32_t updataTableColCmpr(SColCmprWrapper *pWp, SSchema *pSchema, int8_t add, uint32_t compress) {
117✔
52
  int32_t nCols = pWp->nCols;
117✔
53
  int32_t ver = pWp->version;
117✔
54
  if (add) {
117✔
55
    SColCmpr *p = taosMemoryRealloc(pWp->pColCmpr, sizeof(SColCmpr) * (nCols + 1));
65!
56
    if (p == NULL) {
65!
UNCOV
57
      return terrno;
×
58
    }
59
    pWp->pColCmpr = p;
65✔
60

61
    SColCmpr *pCol = p + nCols;
65✔
62
    pCol->id = pSchema->colId;
65✔
63
    pCol->alg = compress;
65✔
64
    pWp->nCols = nCols + 1;
65✔
65
    pWp->version = ver;
65✔
66
  } else {
67
    for (int32_t i = 0; i < nCols; i++) {
170!
68
      SColCmpr *pOCmpr = &pWp->pColCmpr[i];
170✔
69
      if (pOCmpr->id == pSchema->colId) {
170✔
70
        int32_t left = (nCols - i - 1) * sizeof(SColCmpr);
52✔
71
        if (left) {
52✔
72
          memmove(pWp->pColCmpr + i, pWp->pColCmpr + i + 1, left);
35✔
73
        }
74
        nCols--;
52✔
75
        break;
52✔
76
      }
77
    }
78
    pWp->nCols = nCols;
52✔
79
    pWp->version = ver;
52✔
80
  }
81
  return 0;
117✔
82
}
UNCOV
83
static void metaGetEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) {
×
UNCOV
84
  pInfo->uid = pEntry->uid;
×
UNCOV
85
  pInfo->version = pEntry->version;
×
86
  if (pEntry->type == TSDB_SUPER_TABLE) {
×
UNCOV
87
    pInfo->suid = pEntry->uid;
×
UNCOV
88
    pInfo->skmVer = pEntry->stbEntry.schemaRow.version;
×
UNCOV
89
  } else if (pEntry->type == TSDB_CHILD_TABLE) {
×
UNCOV
90
    pInfo->suid = pEntry->ctbEntry.suid;
×
UNCOV
91
    pInfo->skmVer = 0;
×
UNCOV
92
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
×
93
    pInfo->suid = 0;
×
UNCOV
94
    pInfo->skmVer = pEntry->ntbEntry.schemaRow.version;
×
95
  } else {
UNCOV
96
    metaError("meta/table: invalide table type: %" PRId8 " get entry info failed.", pEntry->type);
×
97
  }
98
}
×
99

100
int metaUpdateMetaRsp(tb_uid_t uid, char *tbName, SSchemaWrapper *pSchema, STableMetaRsp *pMetaRsp) {
13,371✔
101
  pMetaRsp->pSchemas = taosMemoryMalloc(pSchema->nCols * sizeof(SSchema));
13,371!
102
  if (NULL == pMetaRsp->pSchemas) {
13,371!
UNCOV
103
    return terrno;
×
104
  }
105

106
  pMetaRsp->pSchemaExt = taosMemoryMalloc(pSchema->nCols * sizeof(SSchemaExt));
13,371!
107
  if (pMetaRsp->pSchemaExt == NULL) {
13,371!
UNCOV
108
    taosMemoryFree(pMetaRsp->pSchemas);
×
UNCOV
109
    return terrno;
×
110
  }
111

112
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
13,371✔
113
  pMetaRsp->numOfColumns = pSchema->nCols;
13,371✔
114
  pMetaRsp->tableType = TSDB_NORMAL_TABLE;
13,371✔
115
  pMetaRsp->sversion = pSchema->version;
13,371✔
116
  pMetaRsp->tuid = uid;
13,371✔
117

118
  memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
13,371✔
119

120
  return 0;
13,371✔
121
}
122

123
int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
158✔
124
  int32_t code = 0;
158✔
125

126
#ifdef USE_INVERTED_INDEX
127
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
158!
UNCOV
128
    return TSDB_CODE_INVALID_PARA;
×
129
  }
130
  void       *data = pCtbEntry->ctbEntry.pTags;
158✔
131
  const char *tagName = pSchema->name;
158✔
132

133
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
158✔
134
  tb_uid_t    tuid = pCtbEntry->uid;
158✔
135
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
158✔
136
  int32_t     nTagData = 0;
158✔
137

138
  SArray *pTagVals = NULL;
158✔
139
  code = tTagToValArray((const STag *)data, &pTagVals);
158✔
140
  if (code) {
158!
UNCOV
141
    return code;
×
142
  }
143

144
  SIndexMultiTerm *terms = indexMultiTermCreate();
158✔
145
  if (terms == NULL) {
158!
UNCOV
146
    return terrno;
×
147
  }
148

149
  int16_t nCols = taosArrayGetSize(pTagVals);
158✔
150
  for (int i = 0; i < nCols; i++) {
345✔
151
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
187✔
152
    char     type = pTagVal->type;
187✔
153

154
    char   *key = pTagVal->pKey;
187✔
155
    int32_t nKey = strlen(key);
187✔
156

157
    SIndexTerm *term = NULL;
187✔
158
    if (type == TSDB_DATA_TYPE_NULL) {
187✔
159
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
18✔
160
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
169✔
161
      if (pTagVal->nData > 0) {
87✔
162
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
75!
163
        if (val == NULL) {
75!
UNCOV
164
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
165
        }
166
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
75✔
167
        if (len < 0) {
75!
UNCOV
168
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
169
        }
170
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
75✔
171
        type = TSDB_DATA_TYPE_VARCHAR;
75✔
172
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
75✔
173
        taosMemoryFree(val);
75!
174
      } else if (pTagVal->nData == 0) {
12!
175
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
12✔
176
      }
177
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
82✔
178
      double val = *(double *)(&pTagVal->i64);
64✔
179
      int    len = sizeof(val);
64✔
180
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
64✔
181
    } else if (type == TSDB_DATA_TYPE_BOOL) {
18!
182
      int val = *(int *)(&pTagVal->i64);
18✔
183
      int len = sizeof(val);
18✔
184
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
18✔
185
    }
186

187
    if (term != NULL) {
187!
188
      int32_t ret = indexMultiTermAdd(terms, term);
187✔
189
      if (ret < 0) {
187!
UNCOV
190
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
191
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
192
      }
193
    } else {
194
      code = terrno;
×
195
      goto _exception;
×
196
    }
197
  }
198
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
158✔
199
  indexMultiTermDestroy(terms);
158✔
200

201
  taosArrayDestroy(pTagVals);
158✔
202
#endif
203
  return code;
158✔
UNCOV
204
_exception:
×
UNCOV
205
  indexMultiTermDestroy(terms);
×
UNCOV
206
  taosArrayDestroy(pTagVals);
×
UNCOV
207
  return code;
×
208
}
209
int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
12✔
210
#ifdef USE_INVERTED_INDEX
211
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
12!
UNCOV
212
    return TSDB_CODE_INVALID_PARA;
×
213
  }
214
  void       *data = pCtbEntry->ctbEntry.pTags;
12✔
215
  const char *tagName = pSchema->name;
12✔
216

217
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
12✔
218
  tb_uid_t    tuid = pCtbEntry->uid;
12✔
219
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
12✔
220
  int32_t     nTagData = 0;
12✔
221

222
  SArray *pTagVals = NULL;
12✔
223
  int32_t code = tTagToValArray((const STag *)data, &pTagVals);
12✔
224
  if (code) {
12!
UNCOV
225
    return code;
×
226
  }
227

228
  SIndexMultiTerm *terms = indexMultiTermCreate();
12✔
229
  if (terms == NULL) {
12!
UNCOV
230
    return terrno;
×
231
  }
232

233
  int16_t nCols = taosArrayGetSize(pTagVals);
12✔
234
  for (int i = 0; i < nCols; i++) {
36✔
235
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
24✔
236
    char     type = pTagVal->type;
24✔
237

238
    char   *key = pTagVal->pKey;
24✔
239
    int32_t nKey = strlen(key);
24✔
240

241
    SIndexTerm *term = NULL;
24✔
242
    if (type == TSDB_DATA_TYPE_NULL) {
24!
UNCOV
243
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
×
244
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
24✔
245
      if (pTagVal->nData > 0) {
9!
246
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
9!
247
        if (val == NULL) {
9!
248
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
249
        }
250
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
9✔
251
        if (len < 0) {
9!
UNCOV
252
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
253
        }
254
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
9✔
255
        type = TSDB_DATA_TYPE_VARCHAR;
9✔
256
        term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, val, len);
9✔
257
        taosMemoryFree(val);
9!
258
      } else if (pTagVal->nData == 0) {
×
UNCOV
259
        term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
×
260
      }
261
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
15✔
262
      double val = *(double *)(&pTagVal->i64);
9✔
263
      int    len = sizeof(val);
9✔
264
      term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, (const char *)&val, len);
9✔
265
    } else if (type == TSDB_DATA_TYPE_BOOL) {
6!
266
      int val = *(int *)(&pTagVal->i64);
6✔
267
      int len = sizeof(val);
6✔
268
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
6✔
269
    }
270
    if (term != NULL) {
24!
271
      int32_t ret = indexMultiTermAdd(terms, term);
24✔
272
      if (ret < 0) {
24!
UNCOV
273
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
274
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
275
      }
276
    } else {
277
      code = terrno;
×
278
      goto _exception;
×
279
    }
280
  }
281
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
12✔
282
  indexMultiTermDestroy(terms);
12✔
283
  taosArrayDestroy(pTagVals);
12✔
284
#endif
285
  return code;
12✔
UNCOV
286
_exception:
×
UNCOV
287
  indexMultiTermDestroy(terms);
×
UNCOV
288
  taosArrayDestroy(pTagVals);
×
289
  return code;
×
290
}
291

292
void metaTimeSeriesNotifyCheck(SMeta *pMeta) {
192✔
293
#if defined(TD_ENTERPRISE)
294
  int64_t nTimeSeries = metaGetTimeSeriesNum(pMeta, 0);
192✔
295
  int64_t deltaTS = nTimeSeries - pMeta->pVnode->config.vndStats.numOfReportedTimeSeries;
192✔
296
  if (deltaTS > tsTimeSeriesThreshold) {
192✔
297
    if (0 == atomic_val_compare_exchange_8(&dmNotifyHdl.state, 1, 2)) {
8!
298
      if (tsem_post(&dmNotifyHdl.sem) != 0) {
8!
UNCOV
299
        metaError("vgId:%d, failed to post semaphore, errno:%d", TD_VID(pMeta->pVnode), errno);
×
300
      }
301
    }
302
  }
303
#endif
304
}
192✔
305

UNCOV
306
static int metaCreateSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
×
UNCOV
307
  SMetaEntry  me = {0};
×
UNCOV
308
  int         kLen = 0;
×
UNCOV
309
  int         vLen = 0;
×
UNCOV
310
  const void *pKey = NULL;
×
UNCOV
311
  const void *pVal = NULL;
×
UNCOV
312
  void       *pBuf = NULL;
×
UNCOV
313
  int32_t     szBuf = 0;
×
UNCOV
314
  void       *p = NULL;
×
315
  int32_t     code = 0;
×
316

317
  // validate req
UNCOV
318
  void *pData = NULL;
×
UNCOV
319
  int   nData = 0;
×
UNCOV
320
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &pData, &nData) == 0) {
×
UNCOV
321
    tb_uid_t uid = *(tb_uid_t *)pData;
×
UNCOV
322
    tdbFree(pData);
×
323
    SMetaInfo info;
UNCOV
324
    if (metaGetInfo(pMeta, uid, &info, NULL) == TSDB_CODE_NOT_FOUND) {
×
UNCOV
325
      return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
326
    }
UNCOV
327
    if (info.uid == info.suid) {
×
UNCOV
328
      return 0;
×
329
    } else {
UNCOV
330
      return terrno = TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
×
331
    }
332
  }
333

334
  // set structs
UNCOV
335
  me.version = version;
×
UNCOV
336
  me.type = TSDB_SUPER_TABLE;
×
UNCOV
337
  me.uid = pReq->suid;
×
UNCOV
338
  me.name = pReq->name;
×
UNCOV
339
  me.stbEntry.schemaRow = pReq->schemaRow;
×
UNCOV
340
  me.stbEntry.schemaTag = pReq->schemaTag;
×
UNCOV
341
  if (pReq->rollup) {
×
UNCOV
342
    TABLE_SET_ROLLUP(me.flags);
×
UNCOV
343
    me.stbEntry.rsmaParam = pReq->rsmaParam;
×
344
  }
UNCOV
345
  if (pReq->colCmpred) {
×
UNCOV
346
    TABLE_SET_COL_COMPRESSED(me.flags);
×
UNCOV
347
    me.colCmpr = pReq->colCmpr;
×
348
  }
349

350
  code = metaHandleEntry(pMeta, &me);
×
351
  if (code) goto _err;
×
352

353
  ++pMeta->pVnode->config.vndStats.numOfSTables;
×
354

UNCOV
355
  pMeta->changed = true;
×
UNCOV
356
  metaDebug("vgId:%d, stb:%s is created, suid:%" PRId64, TD_VID(pMeta->pVnode), pReq->name, pReq->suid);
×
357

UNCOV
358
  return 0;
×
359

UNCOV
360
_err:
×
UNCOV
361
  metaError("vgId:%d, failed to create stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
362
            tstrerror(terrno));
UNCOV
363
  return code;
×
364
}
365

UNCOV
366
static int metaDropSTable(SMeta *pMeta, int64_t verison, SVDropStbReq *pReq, SArray *tbUidList) {
×
UNCOV
367
  void   *pKey = NULL;
×
UNCOV
368
  int     nKey = 0;
×
UNCOV
369
  void   *pData = NULL;
×
UNCOV
370
  int     nData = 0;
×
UNCOV
371
  int     c = 0;
×
UNCOV
372
  int     rc = 0;
×
373
  int32_t lino;
374
  int32_t ret;
375

376
  // check if super table exists
UNCOV
377
  rc = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &pData, &nData);
×
378
  if (rc < 0 || *(tb_uid_t *)pData != pReq->suid) {
×
UNCOV
379
    tdbFree(pData);
×
UNCOV
380
    return terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
×
381
  }
382

383
  // drop all child tables
384
  TBC *pCtbIdxc = NULL;
×
385

UNCOV
386
  rc = tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL);
×
UNCOV
387
  if (rc) {
×
UNCOV
388
    return (terrno = rc);
×
389
  }
390

UNCOV
391
  rc = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = pReq->suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c);
×
UNCOV
392
  if (rc < 0) {
×
UNCOV
393
    tdbTbcClose(pCtbIdxc);
×
UNCOV
394
    metaWLock(pMeta);
×
UNCOV
395
    goto _drop_super_table;
×
396
  }
397

398
  for (;;) {
399
    rc = tdbTbcNext(pCtbIdxc, &pKey, &nKey, NULL, NULL);
×
400
    if (rc < 0) break;
×
401

UNCOV
402
    if (((SCtbIdxKey *)pKey)->suid < pReq->suid) {
×
UNCOV
403
      continue;
×
UNCOV
404
    } else if (((SCtbIdxKey *)pKey)->suid > pReq->suid) {
×
UNCOV
405
      break;
×
406
    }
407

UNCOV
408
    if (taosArrayPush(tbUidList, &(((SCtbIdxKey *)pKey)->uid)) == NULL) {
×
409
      tdbFree(pKey);
×
UNCOV
410
      tdbTbcClose(pCtbIdxc);
×
UNCOV
411
      return terrno;
×
412
    }
413
  }
414

UNCOV
415
  tdbTbcClose(pCtbIdxc);
×
416

UNCOV
417
  ret = tsdbCacheDropSubTables(pMeta->pVnode->pTsdb, tbUidList, pReq->suid);
×
UNCOV
418
  if (ret < 0) {
×
419
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
420
              tstrerror(terrno));
421
  }
422

UNCOV
423
  metaWLock(pMeta);
×
424

UNCOV
425
  for (int32_t iChild = 0; iChild < taosArrayGetSize(tbUidList); iChild++) {
×
UNCOV
426
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUidList, iChild);
×
UNCOV
427
    ret = metaDropTableByUid(pMeta, uid, NULL, NULL, NULL);
×
UNCOV
428
    if (ret < 0) {
×
UNCOV
429
      metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
430
                pReq->suid, tstrerror(terrno));
431
    }
432
  }
433

434
  // drop super table
UNCOV
435
_drop_super_table:
×
436
  tdbTbGet(pMeta->pUidIdx, &pReq->suid, sizeof(tb_uid_t), &pData, &nData);
×
UNCOV
437
  ret = tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = ((SUidIdxVal *)pData)[0].version, .uid = pReq->suid},
×
438
                    sizeof(STbDbKey), pMeta->txn);
UNCOV
439
  if (ret < 0) {
×
UNCOV
440
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
441
              tstrerror(terrno));
442
  }
443

UNCOV
444
  ret = tdbTbDelete(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, pMeta->txn);
×
UNCOV
445
  if (ret < 0) {
×
UNCOV
446
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
447
              tstrerror(terrno));
448
  }
449

UNCOV
450
  ret = metaCacheDrop(pMeta, pReq->suid);
×
UNCOV
451
  if (ret < 0) {
×
UNCOV
452
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
453
              tstrerror(terrno));
454
  }
455

UNCOV
456
  ret = tdbTbDelete(pMeta->pUidIdx, &pReq->suid, sizeof(tb_uid_t), pMeta->txn);
×
UNCOV
457
  if (ret < 0) {
×
UNCOV
458
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
459
              tstrerror(terrno));
460
  }
461

UNCOV
462
  ret = tdbTbDelete(pMeta->pSuidIdx, &pReq->suid, sizeof(tb_uid_t), pMeta->txn);
×
UNCOV
463
  if (ret < 0) {
×
UNCOV
464
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
465
              tstrerror(terrno));
466
  }
467

UNCOV
468
  ret = metaStatsCacheDrop(pMeta, pReq->suid);
×
UNCOV
469
  if (ret < 0) {
×
UNCOV
470
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
471
              tstrerror(terrno));
472
  }
473

UNCOV
474
  metaULock(pMeta);
×
475

UNCOV
476
  metaUpdTimeSeriesNum(pMeta);
×
477

UNCOV
478
  pMeta->changed = true;
×
479

UNCOV
480
_exit:
×
UNCOV
481
  tdbFree(pKey);
×
482
  tdbFree(pData);
×
483
  metaDebug("vgId:%d, super table %s uid:%" PRId64 " is dropped", TD_VID(pMeta->pVnode), pReq->name, pReq->suid);
×
484
  return 0;
×
485
}
486

UNCOV
487
static int32_t metaGetSubtables(SMeta *pMeta, int64_t suid, SArray *uids) {
×
UNCOV
488
  if (!uids) return TSDB_CODE_INVALID_PARA;
×
489

UNCOV
490
  int   c = 0;
×
UNCOV
491
  void *pKey = NULL;
×
492
  int   nKey = 0;
×
UNCOV
493
  TBC  *pCtbIdxc = NULL;
×
494

UNCOV
495
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL));
×
UNCOV
496
  int rc = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c);
×
UNCOV
497
  if (rc < 0) {
×
498
    tdbTbcClose(pCtbIdxc);
×
499
    metaWLock(pMeta);
×
500
    return 0;
×
501
  }
502

503
  for (;;) {
UNCOV
504
    rc = tdbTbcNext(pCtbIdxc, &pKey, &nKey, NULL, NULL);
×
UNCOV
505
    if (rc < 0) break;
×
506

UNCOV
507
    if (((SCtbIdxKey *)pKey)->suid < suid) {
×
UNCOV
508
      continue;
×
UNCOV
509
    } else if (((SCtbIdxKey *)pKey)->suid > suid) {
×
UNCOV
510
      break;
×
511
    }
512

UNCOV
513
    if (taosArrayPush(uids, &(((SCtbIdxKey *)pKey)->uid)) == NULL) {
×
UNCOV
514
      tdbFree(pKey);
×
UNCOV
515
      tdbTbcClose(pCtbIdxc);
×
UNCOV
516
      return terrno;
×
517
    }
518
  }
519

UNCOV
520
  tdbFree(pKey);
×
521

UNCOV
522
  tdbTbcClose(pCtbIdxc);
×
UNCOV
523
  return 0;
×
524
}
525

UNCOV
526
static int metaAlterSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
×
527
  SMetaEntry  oStbEntry = {0};
×
UNCOV
528
  SMetaEntry  nStbEntry = {0};
×
UNCOV
529
  TBC        *pUidIdxc = NULL;
×
UNCOV
530
  TBC        *pTbDbc = NULL;
×
531
  const void *pData;
532
  int         nData;
533
  int64_t     oversion;
534
  SDecoder    dc = {0};
×
535
  int32_t     ret;
UNCOV
536
  int32_t     c = -2;
×
537

UNCOV
538
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, NULL));
×
UNCOV
539
  ret = tdbTbcMoveTo(pUidIdxc, &pReq->suid, sizeof(tb_uid_t), &c);
×
UNCOV
540
  if (ret < 0 || c) {
×
UNCOV
541
    tdbTbcClose(pUidIdxc);
×
542

543
    return terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
×
544
  }
545

546
  ret = tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
×
UNCOV
547
  if (ret < 0) {
×
UNCOV
548
    tdbTbcClose(pUidIdxc);
×
549

UNCOV
550
    return terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
×
551
  }
552

UNCOV
553
  oversion = ((SUidIdxVal *)pData)[0].version;
×
554

UNCOV
555
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pTbDb, &pTbDbc, NULL));
×
UNCOV
556
  ret = tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = pReq->suid, .version = oversion}), sizeof(STbDbKey), &c);
×
UNCOV
557
  if (!(ret == 0 && c == 0)) {
×
558
    tdbTbcClose(pUidIdxc);
×
559
    tdbTbcClose(pTbDbc);
×
560

UNCOV
561
    metaError("meta/table: invalide ret: %" PRId32 " or c: %" PRId32 "alter stb failed.", ret, c);
×
UNCOV
562
    return terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
×
563
  }
564

UNCOV
565
  ret = tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);
×
566
  if (ret < 0) {
×
UNCOV
567
    tdbTbcClose(pUidIdxc);
×
568
    tdbTbcClose(pTbDbc);
×
569

570
    return terrno = TSDB_CODE_TDB_STB_NOT_EXIST;
×
571
  }
572

UNCOV
573
  if ((oStbEntry.pBuf = taosMemoryMalloc(nData)) == NULL) {
×
UNCOV
574
    tdbTbcClose(pTbDbc);
×
UNCOV
575
    tdbTbcClose(pUidIdxc);
×
UNCOV
576
    return terrno;
×
577
  }
UNCOV
578
  memcpy(oStbEntry.pBuf, pData, nData);
×
UNCOV
579
  tDecoderInit(&dc, oStbEntry.pBuf, nData);
×
UNCOV
580
  ret = metaDecodeEntry(&dc, &oStbEntry);
×
UNCOV
581
  if (ret < 0) {
×
UNCOV
582
    metaError("vgId:%d, failed to decode stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
583
              pReq->suid, tstrerror(ret));
UNCOV
584
    tDecoderClear(&dc);
×
UNCOV
585
    tdbTbcClose(pTbDbc);
×
UNCOV
586
    tdbTbcClose(pUidIdxc);
×
UNCOV
587
    return terrno;
×
588
  }
589

UNCOV
590
  nStbEntry.version = version;
×
UNCOV
591
  nStbEntry.type = TSDB_SUPER_TABLE;
×
592
  nStbEntry.uid = pReq->suid;
×
593
  nStbEntry.name = pReq->name;
×
594
  nStbEntry.stbEntry.schemaRow = pReq->schemaRow;
×
595
  nStbEntry.stbEntry.schemaTag = pReq->schemaTag;
×
596
  nStbEntry.colCmpr = pReq->colCmpr;
×
UNCOV
597
  TABLE_SET_COL_COMPRESSED(nStbEntry.flags);
×
598

UNCOV
599
  int     nCols = pReq->schemaRow.nCols;
×
UNCOV
600
  int     onCols = oStbEntry.stbEntry.schemaRow.nCols;
×
UNCOV
601
  int32_t deltaCol = nCols - onCols;
×
UNCOV
602
  bool    updStat = deltaCol != 0 && !metaTbInFilterCache(pMeta, pReq->name, 1);
×
603

UNCOV
604
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
×
UNCOV
605
    STsdb  *pTsdb = pMeta->pVnode->pTsdb;
×
UNCOV
606
    SArray *uids = taosArrayInit(8, sizeof(int64_t));
×
UNCOV
607
    if (uids == NULL) {
×
UNCOV
608
      if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
×
UNCOV
609
      tDecoderClear(&dc);
×
UNCOV
610
      tdbTbcClose(pTbDbc);
×
UNCOV
611
      tdbTbcClose(pUidIdxc);
×
UNCOV
612
      return terrno;
×
613
    }
UNCOV
614
    if (deltaCol == 1) {
×
UNCOV
615
      int16_t cid = pReq->schemaRow.pSchema[nCols - 1].colId;
×
UNCOV
616
      int8_t  col_type = pReq->schemaRow.pSchema[nCols - 1].type;
×
617

UNCOV
618
      TAOS_CHECK_RETURN(metaGetSubtables(pMeta, pReq->suid, uids));
×
UNCOV
619
      TAOS_CHECK_RETURN(tsdbCacheNewSTableColumn(pTsdb, uids, cid, col_type));
×
UNCOV
620
    } else if (deltaCol == -1) {
×
UNCOV
621
      int16_t cid = -1;
×
UNCOV
622
      bool    hasPrimaryKey = false;
×
UNCOV
623
      if (onCols >= 2) {
×
UNCOV
624
        hasPrimaryKey = (oStbEntry.stbEntry.schemaRow.pSchema[1].flags & COL_IS_KEY) ? true : false;
×
625
      }
UNCOV
626
      for (int i = 0, j = 0; i < nCols && j < onCols; ++i, ++j) {
×
UNCOV
627
        if (pReq->schemaRow.pSchema[i].colId != oStbEntry.stbEntry.schemaRow.pSchema[j].colId) {
×
UNCOV
628
          cid = oStbEntry.stbEntry.schemaRow.pSchema[j].colId;
×
UNCOV
629
          break;
×
630
        }
631
      }
632

UNCOV
633
      if (cid != -1) {
×
UNCOV
634
        TAOS_CHECK_RETURN(metaGetSubtables(pMeta, pReq->suid, uids));
×
UNCOV
635
        TAOS_CHECK_RETURN(tsdbCacheDropSTableColumn(pTsdb, uids, cid, hasPrimaryKey));
×
636
      }
637
    }
UNCOV
638
    if (uids) taosArrayDestroy(uids);
×
639

640
    tsdbCacheInvalidateSchema(pTsdb, pReq->suid, -1, pReq->schemaRow.version);
×
641
  }
642

UNCOV
643
  metaWLock(pMeta);
×
644
  // compare two entry
UNCOV
645
  if (oStbEntry.stbEntry.schemaRow.version != pReq->schemaRow.version) {
×
UNCOV
646
    ret = metaSaveToSkmDb(pMeta, &nStbEntry);
×
647
    if (ret < 0) {
×
UNCOV
648
      metaError("vgId:%d, failed to save skm db:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
649
                pReq->suid, tstrerror(ret));
650
    }
651
  }
652

653
  // update table.db
UNCOV
654
  ret = metaSaveToTbDb(pMeta, &nStbEntry);
×
UNCOV
655
  if (ret < 0) {
×
UNCOV
656
    metaError("vgId:%d, failed to save tb db:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
657
              pReq->suid, tstrerror(ret));
658
  }
659

660
  // update uid index
UNCOV
661
  ret = metaUpdateUidIdx(pMeta, &nStbEntry);
×
662
  if (ret < 0) {
×
UNCOV
663
    metaError("vgId:%d, failed to update uid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
664
              pReq->suid, tstrerror(ret));
665
  }
666

667
  // metaStatsCacheDrop(pMeta, nStbEntry.uid);
668

UNCOV
669
  if (updStat) {
×
UNCOV
670
    metaUpdateStbStats(pMeta, pReq->suid, 0, deltaCol);
×
671
  }
UNCOV
672
  metaULock(pMeta);
×
673

UNCOV
674
  if (updStat) {
×
675
    int64_t ctbNum;
UNCOV
676
    ret = metaGetStbStats(pMeta->pVnode, pReq->suid, &ctbNum, NULL);
×
UNCOV
677
    if (ret < 0) {
×
UNCOV
678
      metaError("vgId:%d, failed to get stb stats:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
679
                pReq->suid, tstrerror(ret));
680
    }
UNCOV
681
    pMeta->pVnode->config.vndStats.numOfTimeSeries += (ctbNum * deltaCol);
×
UNCOV
682
    metaTimeSeriesNotifyCheck(pMeta);
×
683
  }
684

UNCOV
685
  pMeta->changed = true;
×
686

UNCOV
687
_exit:
×
UNCOV
688
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
×
UNCOV
689
  tDecoderClear(&dc);
×
UNCOV
690
  tdbTbcClose(pTbDbc);
×
UNCOV
691
  tdbTbcClose(pUidIdxc);
×
UNCOV
692
  return 0;
×
693
}
694

UNCOV
695
static int metaAddIndexToSTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
×
696
  SMetaEntry oStbEntry = {0};
×
UNCOV
697
  SMetaEntry nStbEntry = {0};
×
UNCOV
698
  STbDbKey   tbDbKey = {0};
×
699

UNCOV
700
  TBC     *pUidIdxc = NULL;
×
UNCOV
701
  TBC     *pTbDbc = NULL;
×
702
  void    *pData = NULL;
×
UNCOV
703
  int      nData = 0;
×
704
  int64_t  oversion;
UNCOV
705
  SDecoder dc = {0};
×
706
  int32_t  ret;
UNCOV
707
  int32_t  c = -2;
×
708
  tb_uid_t suid = pReq->suid;
×
UNCOV
709
  int32_t  code = 0;
×
710

711
  // get super table
712
  if ((code = tdbTbGet(pMeta->pUidIdx, &suid, sizeof(tb_uid_t), &pData, &nData)) != 0) {
×
713
    goto _err;
×
714
  }
715

UNCOV
716
  tbDbKey.uid = suid;
×
717
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
×
718
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData)) != 0) {
×
UNCOV
719
    goto _err;
×
720
  }
721

722
  tDecoderInit(&dc, pData, nData);
×
723
  code = metaDecodeEntry(&dc, &oStbEntry);
×
UNCOV
724
  if (code < 0) {
×
UNCOV
725
    goto _err;
×
726
  }
727

UNCOV
728
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
×
UNCOV
729
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
730
    goto _err;
×
731
  }
732

733
  if (oStbEntry.stbEntry.schemaTag.version == pReq->schemaTag.version) {
×
UNCOV
734
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
735
    goto _err;
×
736
  }
737

UNCOV
738
  if (oStbEntry.stbEntry.schemaTag.nCols != pReq->schemaTag.nCols) {
×
UNCOV
739
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
740
    goto _err;
×
741
  }
742

743
  int diffIdx = -1;
×
744
  for (int i = 0; i < pReq->schemaTag.nCols; i++) {
×
UNCOV
745
    SSchema *pNew = pReq->schemaTag.pSchema + i;
×
UNCOV
746
    SSchema *pOld = oStbEntry.stbEntry.schemaTag.pSchema + i;
×
UNCOV
747
    if (pNew->type != pOld->type || pNew->colId != pOld->colId || pNew->bytes != pOld->bytes ||
×
UNCOV
748
        strncmp(pOld->name, pNew->name, sizeof(pNew->name))) {
×
UNCOV
749
      code = TSDB_CODE_INVALID_PARA;
×
750
      goto _err;
×
751
    }
UNCOV
752
    if (IS_IDX_ON(pNew) && !IS_IDX_ON(pOld)) {
×
753
      // if (diffIdx != -1) goto _err;
UNCOV
754
      diffIdx = i;
×
UNCOV
755
      break;
×
756
    }
757
  }
758

UNCOV
759
  if (diffIdx == -1) {
×
UNCOV
760
    code = TSDB_CODE_INVALID_PARA;
×
761
    goto _err;
×
762
  }
763

764
  // Get target schema info
UNCOV
765
  SSchemaWrapper *pTagSchema = &pReq->schemaTag;
×
766
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
×
767
    code = TSDB_CODE_INVALID_PARA;
×
UNCOV
768
    goto _err;
×
769
  }
UNCOV
770
  SSchema *pCol = pTagSchema->pSchema + diffIdx;
×
771

772
  /*
773
   * iterator all pTdDbc by uid and version
774
   */
UNCOV
775
  TBC *pCtbIdxc = NULL;
×
UNCOV
776
  code = tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL);
×
UNCOV
777
  if (code != 0) {
×
UNCOV
778
    goto _err;
×
779
  }
780

UNCOV
781
  code = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c);
×
UNCOV
782
  if (code < 0) {
×
UNCOV
783
    tdbTbcClose(pCtbIdxc);
×
UNCOV
784
    goto _err;
×
785
  }
UNCOV
786
  for (;;) {
×
UNCOV
787
    void *pKey = NULL, *pVal = NULL;
×
UNCOV
788
    int   nKey = 0, nVal = 0;
×
UNCOV
789
    code = tdbTbcNext(pCtbIdxc, &pKey, &nKey, &pVal, &nVal);
×
UNCOV
790
    if (code < 0) {
×
UNCOV
791
      tdbFree(pKey);
×
UNCOV
792
      tdbFree(pVal);
×
UNCOV
793
      tdbTbcClose(pCtbIdxc);
×
UNCOV
794
      pCtbIdxc = NULL;
×
UNCOV
795
      break;
×
796
    }
UNCOV
797
    if (((SCtbIdxKey *)pKey)->suid != suid) {
×
UNCOV
798
      tdbFree(pKey);
×
UNCOV
799
      tdbFree(pVal);
×
UNCOV
800
      continue;
×
801
    }
802
    STagIdxKey *pTagIdxKey = NULL;
×
803
    int32_t     nTagIdxKey;
804

UNCOV
805
    const void *pTagData = NULL;
×
UNCOV
806
    int32_t     nTagData = 0;
×
807

UNCOV
808
    SCtbIdxKey *table = (SCtbIdxKey *)pKey;
×
UNCOV
809
    STagVal     tagVal = {.cid = pCol->colId};
×
810
    if (tTagGet((const STag *)pVal, &tagVal)) {
×
811
      if (IS_VAR_DATA_TYPE(pCol->type)) {
×
812
        pTagData = tagVal.pData;
×
UNCOV
813
        nTagData = (int32_t)tagVal.nData;
×
814
      } else {
UNCOV
815
        pTagData = &(tagVal.i64);
×
UNCOV
816
        nTagData = tDataTypes[pCol->type].bytes;
×
817
      }
818
    } else {
UNCOV
819
      if (!IS_VAR_DATA_TYPE(pCol->type)) {
×
UNCOV
820
        nTagData = tDataTypes[pCol->type].bytes;
×
821
      }
822
    }
UNCOV
823
    code = metaCreateTagIdxKey(suid, pCol->colId, pTagData, nTagData, pCol->type, table->uid, &pTagIdxKey, &nTagIdxKey);
×
UNCOV
824
    tdbFree(pKey);
×
UNCOV
825
    tdbFree(pVal);
×
UNCOV
826
    if (code < 0) {
×
UNCOV
827
      metaDestroyTagIdxKey(pTagIdxKey);
×
UNCOV
828
      tdbTbcClose(pCtbIdxc);
×
UNCOV
829
      goto _err;
×
830
    }
831

UNCOV
832
    metaWLock(pMeta);
×
UNCOV
833
    ret = tdbTbUpsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn);
×
UNCOV
834
    if (ret < 0) {
×
UNCOV
835
      metaError("vgId:%d, failed to upsert tag idx key:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
836
                pReq->suid, tstrerror(ret));
837
    }
UNCOV
838
    metaULock(pMeta);
×
839

UNCOV
840
    metaDestroyTagIdxKey(pTagIdxKey);
×
UNCOV
841
    pTagIdxKey = NULL;
×
842
  }
843

UNCOV
844
  nStbEntry.version = version;
×
845
  nStbEntry.type = TSDB_SUPER_TABLE;
×
UNCOV
846
  nStbEntry.uid = pReq->suid;
×
UNCOV
847
  nStbEntry.name = pReq->name;
×
UNCOV
848
  nStbEntry.stbEntry.schemaRow = pReq->schemaRow;
×
UNCOV
849
  nStbEntry.stbEntry.schemaTag = pReq->schemaTag;
×
UNCOV
850
  nStbEntry.colCmpr = pReq->colCmpr;
×
851

UNCOV
852
  metaWLock(pMeta);
×
853
  // update table.db
UNCOV
854
  ret = metaSaveToTbDb(pMeta, &nStbEntry);
×
UNCOV
855
  if (ret < 0) {
×
856
    metaError("vgId:%d, failed to save tb db:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
857
              pReq->suid, tstrerror(ret));
858
  }
859
  // update uid index
UNCOV
860
  ret = metaUpdateUidIdx(pMeta, &nStbEntry);
×
861
  if (ret < 0) {
×
UNCOV
862
    metaError("vgId:%d, failed to update uid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
863
              pReq->suid, tstrerror(ret));
864
  }
UNCOV
865
  metaULock(pMeta);
×
866

UNCOV
867
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
×
UNCOV
868
  tDecoderClear(&dc);
×
UNCOV
869
  tdbFree(pData);
×
870

UNCOV
871
  tdbTbcClose(pCtbIdxc);
×
UNCOV
872
  return TSDB_CODE_SUCCESS;
×
UNCOV
873
_err:
×
UNCOV
874
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
×
UNCOV
875
  tDecoderClear(&dc);
×
UNCOV
876
  tdbFree(pData);
×
877

UNCOV
878
  return code;
×
879
}
880

881
static int metaDropIndexFromSTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
×
UNCOV
882
  int32_t    code = 0;
×
UNCOV
883
  SMetaEntry oStbEntry = {0};
×
UNCOV
884
  SMetaEntry nStbEntry = {0};
×
885

UNCOV
886
  STbDbKey tbDbKey = {0};
×
887
  TBC     *pUidIdxc = NULL;
×
UNCOV
888
  TBC     *pTbDbc = NULL;
×
UNCOV
889
  int      ret = 0;
×
UNCOV
890
  int      c = -2;
×
UNCOV
891
  void    *pData = NULL;
×
UNCOV
892
  int      nData = 0;
×
893
  int64_t  oversion;
UNCOV
894
  SDecoder dc = {0};
×
895

UNCOV
896
  tb_uid_t suid = pReq->stbUid;
×
897

UNCOV
898
  if ((code = tdbTbGet(pMeta->pUidIdx, &suid, sizeof(tb_uid_t), &pData, &nData)) != 0) {
×
UNCOV
899
    goto _err;
×
900
  }
901

UNCOV
902
  tbDbKey.uid = suid;
×
UNCOV
903
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
×
UNCOV
904
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData)) != 0) {
×
UNCOV
905
    goto _err;
×
906
  }
907

UNCOV
908
  tDecoderInit(&dc, pData, nData);
×
909
  code = metaDecodeEntry(&dc, &oStbEntry);
×
UNCOV
910
  if (code != 0) {
×
911
    goto _err;
×
912
  }
913

UNCOV
914
  SSchema *pCol = NULL;
×
UNCOV
915
  int32_t  colId = -1;
×
UNCOV
916
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
×
UNCOV
917
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
×
UNCOV
918
    if (0 == strncmp(schema->name, pReq->colName, sizeof(pReq->colName))) {
×
UNCOV
919
      if (IS_IDX_ON(schema)) {
×
UNCOV
920
        pCol = schema;
×
921
      }
922
      break;
×
923
    }
924
  }
925

UNCOV
926
  if (pCol == NULL) {
×
927
    metaError("vgId:%d, failed to drop index on %s.%s,since %s", TD_VID(pMeta->pVnode), pReq->stb, pReq->colName,
×
928
              tstrerror(TSDB_CODE_VND_COL_NOT_EXISTS));
UNCOV
929
    code = 0;
×
930

UNCOV
931
    goto _err;
×
932
  }
933

934
  /*
935
   * iterator all pTdDbc by uid and version
936
   */
UNCOV
937
  TBC *pCtbIdxc = NULL;
×
UNCOV
938
  code = tdbTbcOpen(pMeta->pCtbIdx, &pCtbIdxc, NULL);
×
UNCOV
939
  if (code != 0) {
×
UNCOV
940
    goto _err;
×
941
  }
942

UNCOV
943
  code = tdbTbcMoveTo(pCtbIdxc, &(SCtbIdxKey){.suid = suid, .uid = INT64_MIN}, sizeof(SCtbIdxKey), &c);
×
UNCOV
944
  if (code < 0) {
×
UNCOV
945
    tdbTbcClose(pCtbIdxc);
×
UNCOV
946
    goto _err;
×
947
  }
UNCOV
948
  for (;;) {
×
UNCOV
949
    void *pKey = NULL, *pVal = NULL;
×
UNCOV
950
    int   nKey = 0, nVal = 0;
×
951

UNCOV
952
    code = tdbTbcNext(pCtbIdxc, &pKey, &nKey, &pVal, &nVal);
×
UNCOV
953
    if (code < 0) {
×
UNCOV
954
      tdbFree(pKey);
×
UNCOV
955
      tdbFree(pVal);
×
UNCOV
956
      tdbTbcClose(pCtbIdxc);
×
UNCOV
957
      pCtbIdxc = NULL;
×
UNCOV
958
      break;
×
959
    }
UNCOV
960
    if (((SCtbIdxKey *)pKey)->suid != suid) {
×
UNCOV
961
      tdbFree(pKey);
×
UNCOV
962
      tdbFree(pVal);
×
UNCOV
963
      continue;
×
964
    }
965
    STagIdxKey *pTagIdxKey = NULL;
×
966
    int32_t     nTagIdxKey;
967

UNCOV
968
    const void *pTagData = NULL;
×
UNCOV
969
    int32_t     nTagData = 0;
×
970

UNCOV
971
    SCtbIdxKey *table = (SCtbIdxKey *)pKey;
×
UNCOV
972
    STagVal     tagVal = {.cid = pCol->colId};
×
973
    if (tTagGet((const STag *)pVal, &tagVal)) {
×
974
      if (IS_VAR_DATA_TYPE(pCol->type)) {
×
975
        pTagData = tagVal.pData;
×
UNCOV
976
        nTagData = (int32_t)tagVal.nData;
×
977
      } else {
UNCOV
978
        pTagData = &(tagVal.i64);
×
UNCOV
979
        nTagData = tDataTypes[pCol->type].bytes;
×
980
      }
981
    } else {
UNCOV
982
      if (!IS_VAR_DATA_TYPE(pCol->type)) {
×
UNCOV
983
        nTagData = tDataTypes[pCol->type].bytes;
×
984
      }
985
    }
986

UNCOV
987
    code = metaCreateTagIdxKey(suid, pCol->colId, pTagData, nTagData, pCol->type, table->uid, &pTagIdxKey, &nTagIdxKey);
×
UNCOV
988
    tdbFree(pKey);
×
UNCOV
989
    tdbFree(pVal);
×
UNCOV
990
    if (code < 0) {
×
UNCOV
991
      metaDestroyTagIdxKey(pTagIdxKey);
×
UNCOV
992
      tdbTbcClose(pCtbIdxc);
×
UNCOV
993
      goto _err;
×
994
    }
995

UNCOV
996
    metaWLock(pMeta);
×
UNCOV
997
    ret = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
×
UNCOV
998
    if (ret < 0) {
×
UNCOV
999
      metaError("vgId:%d, failed to delete tag idx key:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->stb,
×
1000
                pReq->stbUid, tstrerror(ret));
1001
    }
UNCOV
1002
    metaULock(pMeta);
×
UNCOV
1003
    metaDestroyTagIdxKey(pTagIdxKey);
×
1004
    pTagIdxKey = NULL;
×
1005
  }
1006

1007
  // clear idx flag
UNCOV
1008
  SSCHMEA_SET_IDX_OFF(pCol);
×
1009

UNCOV
1010
  nStbEntry.version = version;
×
UNCOV
1011
  nStbEntry.type = TSDB_SUPER_TABLE;
×
UNCOV
1012
  nStbEntry.uid = oStbEntry.uid;
×
UNCOV
1013
  nStbEntry.name = oStbEntry.name;
×
1014

UNCOV
1015
  SSchemaWrapper  *row = tCloneSSchemaWrapper(&oStbEntry.stbEntry.schemaRow);
×
UNCOV
1016
  SSchemaWrapper  *tag = tCloneSSchemaWrapper(&oStbEntry.stbEntry.schemaTag);
×
UNCOV
1017
  SColCmprWrapper *cmpr = tCloneSColCmprWrapper(&oStbEntry.colCmpr);
×
UNCOV
1018
  if (row == NULL || tag == NULL || cmpr == NULL) {
×
1019
    tDeleteSchemaWrapper(row);
1020
    tDeleteSchemaWrapper(tag);
1021
    tDeleteSColCmprWrapper(cmpr);
UNCOV
1022
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1023

UNCOV
1024
    tdbTbcClose(pCtbIdxc);
×
UNCOV
1025
    goto _err;
×
1026
  }
1027

UNCOV
1028
  nStbEntry.stbEntry.schemaRow = *row;
×
UNCOV
1029
  nStbEntry.stbEntry.schemaTag = *tag;
×
UNCOV
1030
  nStbEntry.stbEntry.rsmaParam = oStbEntry.stbEntry.rsmaParam;
×
UNCOV
1031
  nStbEntry.colCmpr = *cmpr;
×
1032

UNCOV
1033
  nStbEntry.colCmpr = oStbEntry.colCmpr;
×
1034

UNCOV
1035
  metaWLock(pMeta);
×
1036
  // update table.db
UNCOV
1037
  ret = metaSaveToTbDb(pMeta, &nStbEntry);
×
UNCOV
1038
  if (ret < 0) {
×
UNCOV
1039
    metaError("vgId:%d, failed to save tb db:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->stb,
×
1040
              pReq->stbUid, tstrerror(ret));
1041
  }
1042
  // update uid index
1043
  ret = metaUpdateUidIdx(pMeta, &nStbEntry);
×
1044
  if (ret < 0) {
×
1045
    metaError("vgId:%d, failed to update uid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->stb,
×
1046
              pReq->stbUid, tstrerror(ret));
1047
  }
UNCOV
1048
  metaULock(pMeta);
×
1049

1050
  tDeleteSchemaWrapper(tag);
1051
  tDeleteSchemaWrapper(row);
1052
  tDeleteSColCmprWrapper(cmpr);
1053

UNCOV
1054
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
×
UNCOV
1055
  tDecoderClear(&dc);
×
UNCOV
1056
  tdbFree(pData);
×
1057

1058
  tdbTbcClose(pCtbIdxc);
×
UNCOV
1059
  return TSDB_CODE_SUCCESS;
×
UNCOV
1060
_err:
×
UNCOV
1061
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
×
UNCOV
1062
  tDecoderClear(&dc);
×
UNCOV
1063
  tdbFree(pData);
×
1064

UNCOV
1065
  return code;
×
1066
}
1067

UNCOV
1068
static int metaCreateTable(SMeta *pMeta, int64_t ver, SVCreateTbReq *pReq, STableMetaRsp **pMetaRsp) {
×
UNCOV
1069
  SMetaEntry  me = {0};
×
UNCOV
1070
  SMetaReader mr = {0};
×
1071
  int32_t     ret;
1072

1073
  // validate message
UNCOV
1074
  if (pReq->type != TSDB_CHILD_TABLE && pReq->type != TSDB_NORMAL_TABLE) {
×
UNCOV
1075
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
1076
    goto _err;
×
1077
  }
1078

UNCOV
1079
  if (pReq->type == TSDB_CHILD_TABLE) {
×
UNCOV
1080
    tb_uid_t suid = metaGetTableEntryUidByName(pMeta, pReq->ctb.stbName);
×
UNCOV
1081
    if (suid != pReq->ctb.suid) {
×
UNCOV
1082
      return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
1083
    }
1084
  }
1085

1086
  // validate req
UNCOV
1087
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK);
×
UNCOV
1088
  if (metaGetTableEntryByName(&mr, pReq->name) == 0) {
×
UNCOV
1089
    if (pReq->type == TSDB_CHILD_TABLE && pReq->ctb.suid != mr.me.ctbEntry.suid) {
×
UNCOV
1090
      metaReaderClear(&mr);
×
UNCOV
1091
      return terrno = TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
×
1092
    }
UNCOV
1093
    pReq->uid = mr.me.uid;
×
UNCOV
1094
    if (pReq->type == TSDB_CHILD_TABLE) {
×
UNCOV
1095
      pReq->ctb.suid = mr.me.ctbEntry.suid;
×
1096
    }
UNCOV
1097
    metaReaderClear(&mr);
×
UNCOV
1098
    return terrno = TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
×
UNCOV
1099
  } else if (terrno == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
×
UNCOV
1100
    terrno = TSDB_CODE_SUCCESS;
×
1101
  }
UNCOV
1102
  metaReaderClear(&mr);
×
1103

UNCOV
1104
  bool sysTbl = (pReq->type == TSDB_CHILD_TABLE) && metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1);
×
1105

UNCOV
1106
  if (!sysTbl && ((terrno = grantCheck(TSDB_GRANT_TIMESERIES)) < 0)) goto _err;
×
1107

1108
  // build SMetaEntry
UNCOV
1109
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
×
UNCOV
1110
  me.version = ver;
×
UNCOV
1111
  me.type = pReq->type;
×
UNCOV
1112
  me.uid = pReq->uid;
×
UNCOV
1113
  me.name = pReq->name;
×
UNCOV
1114
  if (me.type == TSDB_CHILD_TABLE) {
×
UNCOV
1115
    me.ctbEntry.btime = pReq->btime;
×
UNCOV
1116
    me.ctbEntry.ttlDays = pReq->ttl;
×
UNCOV
1117
    me.ctbEntry.commentLen = pReq->commentLen;
×
UNCOV
1118
    me.ctbEntry.comment = pReq->comment;
×
UNCOV
1119
    me.ctbEntry.suid = pReq->ctb.suid;
×
UNCOV
1120
    me.ctbEntry.pTags = pReq->ctb.pTag;
×
1121

1122
#ifdef TAG_FILTER_DEBUG
1123
    SArray *pTagVals = NULL;
1124
    int32_t code = tTagToValArray((STag *)pReq->ctb.pTag, &pTagVals);
1125
    for (int i = 0; i < taosArrayGetSize(pTagVals); i++) {
1126
      STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
1127

1128
      if (IS_VAR_DATA_TYPE(pTagVal->type)) {
1129
        char *buf = taosMemoryCalloc(pTagVal->nData + 1, 1);
1130
        memcpy(buf, pTagVal->pData, pTagVal->nData);
1131
        metaDebug("metaTag table:%s varchar index:%d cid:%d type:%d value:%s", pReq->name, i, pTagVal->cid,
1132
                  pTagVal->type, buf);
1133
        taosMemoryFree(buf);
1134
      } else {
1135
        double val = 0;
1136
        GET_TYPED_DATA(val, double, pTagVal->type, &pTagVal->i64);
1137
        metaDebug("metaTag table:%s number index:%d cid:%d type:%d value:%f", pReq->name, i, pTagVal->cid,
1138
                  pTagVal->type, val);
1139
      }
1140
    }
1141
#endif
1142

UNCOV
1143
    ++pStats->numOfCTables;
×
1144

UNCOV
1145
    if (!sysTbl) {
×
1146
      int32_t nCols = 0;
×
UNCOV
1147
      ret = metaGetStbStats(pMeta->pVnode, me.ctbEntry.suid, 0, &nCols);
×
UNCOV
1148
      if (ret < 0) {
×
UNCOV
1149
        metaError("vgId:%d, failed to get stb stats:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
1150
                  pReq->ctb.suid, tstrerror(ret));
1151
      }
UNCOV
1152
      pStats->numOfTimeSeries += nCols - 1;
×
1153
    }
1154

1155
    metaWLock(pMeta);
×
UNCOV
1156
    metaUpdateStbStats(pMeta, me.ctbEntry.suid, 1, 0);
×
UNCOV
1157
    ret = metaUidCacheClear(pMeta, me.ctbEntry.suid);
×
UNCOV
1158
    if (ret < 0) {
×
UNCOV
1159
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
1160
                pReq->ctb.suid, tstrerror(ret));
1161
    }
UNCOV
1162
    ret = metaTbGroupCacheClear(pMeta, me.ctbEntry.suid);
×
UNCOV
1163
    if (ret < 0) {
×
UNCOV
1164
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
1165
                pReq->ctb.suid, tstrerror(ret));
1166
    }
UNCOV
1167
    metaULock(pMeta);
×
1168

UNCOV
1169
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
×
UNCOV
1170
      ret = tsdbCacheNewTable(pMeta->pVnode->pTsdb, me.uid, me.ctbEntry.suid, NULL);
×
UNCOV
1171
      if (ret < 0) {
×
UNCOV
1172
        metaError("vgId:%d, failed to create table:%s since %s", TD_VID(pMeta->pVnode), pReq->name, tstrerror(ret));
×
UNCOV
1173
        goto _err;
×
1174
      }
1175
    }
1176
  } else {
UNCOV
1177
    me.ntbEntry.btime = pReq->btime;
×
UNCOV
1178
    me.ntbEntry.ttlDays = pReq->ttl;
×
UNCOV
1179
    me.ntbEntry.commentLen = pReq->commentLen;
×
UNCOV
1180
    me.ntbEntry.comment = pReq->comment;
×
UNCOV
1181
    me.ntbEntry.schemaRow = pReq->ntb.schemaRow;
×
UNCOV
1182
    me.ntbEntry.ncid = me.ntbEntry.schemaRow.pSchema[me.ntbEntry.schemaRow.nCols - 1].colId + 1;
×
UNCOV
1183
    me.colCmpr = pReq->colCmpr;
×
UNCOV
1184
    TABLE_SET_COL_COMPRESSED(me.flags);
×
1185

UNCOV
1186
    ++pStats->numOfNTables;
×
UNCOV
1187
    pStats->numOfNTimeSeries += me.ntbEntry.schemaRow.nCols - 1;
×
1188

UNCOV
1189
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
×
UNCOV
1190
      ret = tsdbCacheNewTable(pMeta->pVnode->pTsdb, me.uid, -1, &me.ntbEntry.schemaRow);
×
UNCOV
1191
      if (ret < 0) {
×
UNCOV
1192
        metaError("vgId:%d, failed to create table:%s since %s", TD_VID(pMeta->pVnode), pReq->name, tstrerror(ret));
×
UNCOV
1193
        goto _err;
×
1194
      }
1195
    }
1196
  }
1197

UNCOV
1198
  if (metaHandleEntry(pMeta, &me) < 0) goto _err;
×
1199

UNCOV
1200
  metaTimeSeriesNotifyCheck(pMeta);
×
1201

UNCOV
1202
  if (pMetaRsp) {
×
UNCOV
1203
    *pMetaRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
×
1204

UNCOV
1205
    if (*pMetaRsp) {
×
UNCOV
1206
      if (me.type == TSDB_CHILD_TABLE) {
×
UNCOV
1207
        (*pMetaRsp)->tableType = TSDB_CHILD_TABLE;
×
UNCOV
1208
        (*pMetaRsp)->tuid = pReq->uid;
×
UNCOV
1209
        (*pMetaRsp)->suid = pReq->ctb.suid;
×
UNCOV
1210
        tstrncpy((*pMetaRsp)->tbName, pReq->name, strlen(pReq->name) + 1);
×
1211
      } else {
UNCOV
1212
        ret = metaUpdateMetaRsp(pReq->uid, pReq->name, &pReq->ntb.schemaRow, *pMetaRsp);
×
1213
        if (ret < 0) {
×
1214
          metaError("vgId:%d, failed to update meta rsp:%s since %s", TD_VID(pMeta->pVnode), pReq->name,
×
1215
                    tstrerror(ret));
1216
        }
UNCOV
1217
        for (int32_t i = 0; i < pReq->colCmpr.nCols; i++) {
×
UNCOV
1218
          SColCmpr *p = &pReq->colCmpr.pColCmpr[i];
×
UNCOV
1219
          (*pMetaRsp)->pSchemaExt[i].colId = p->id;
×
UNCOV
1220
          (*pMetaRsp)->pSchemaExt[i].compress = p->alg;
×
1221
        }
1222
      }
1223
    }
1224
  }
1225

UNCOV
1226
  pMeta->changed = true;
×
UNCOV
1227
  metaDebug("vgId:%d, table:%s uid %" PRId64 " is created, type:%" PRId8, TD_VID(pMeta->pVnode), pReq->name, pReq->uid,
×
1228
            pReq->type);
UNCOV
1229
  return 0;
×
1230

UNCOV
1231
_err:
×
UNCOV
1232
  metaError("vgId:%d, failed to create table:%s type:%s since %s", TD_VID(pMeta->pVnode), pReq->name,
×
1233
            pReq->type == TSDB_CHILD_TABLE ? "child table" : "normal table", tstrerror(terrno));
UNCOV
1234
  return TSDB_CODE_FAILED;
×
1235
}
1236

UNCOV
1237
static int metaDropTable(SMeta *pMeta, int64_t version, SVDropTbReq *pReq, SArray *tbUids, tb_uid_t *tbUid) {
×
UNCOV
1238
  void    *pData = NULL;
×
UNCOV
1239
  int      nData = 0;
×
UNCOV
1240
  int      rc = 0;
×
UNCOV
1241
  tb_uid_t uid = 0;
×
UNCOV
1242
  tb_uid_t suid = 0;
×
UNCOV
1243
  int8_t   sysTbl = 0;
×
1244
  int      type;
1245

UNCOV
1246
  rc = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &pData, &nData);
×
UNCOV
1247
  if (rc < 0) {
×
UNCOV
1248
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1249
  }
1250
  uid = *(tb_uid_t *)pData;
×
1251

UNCOV
1252
  metaWLock(pMeta);
×
UNCOV
1253
  rc = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
×
UNCOV
1254
  metaULock(pMeta);
×
1255

UNCOV
1256
  if (rc < 0) goto _exit;
×
1257

UNCOV
1258
  if (!sysTbl && type == TSDB_CHILD_TABLE) {
×
UNCOV
1259
    int32_t      nCols = 0;
×
UNCOV
1260
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
×
UNCOV
1261
    if (metaGetStbStats(pMeta->pVnode, suid, NULL, &nCols) == 0) {
×
UNCOV
1262
      pStats->numOfTimeSeries -= nCols - 1;
×
1263
    }
1264
  }
1265

UNCOV
1266
  if ((type == TSDB_CHILD_TABLE || type == TSDB_NORMAL_TABLE) && tbUids) {
×
UNCOV
1267
    if (taosArrayPush(tbUids, &uid) == NULL) {
×
UNCOV
1268
      rc = terrno;
×
UNCOV
1269
      goto _exit;
×
1270
    }
1271

UNCOV
1272
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
×
UNCOV
1273
      int32_t ret = tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
×
UNCOV
1274
      if (ret < 0) {
×
UNCOV
1275
        metaError("vgId:%d, failed to drop table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, uid,
×
1276
                  tstrerror(ret));
1277
      }
1278
    }
1279
  }
1280

UNCOV
1281
  if ((type == TSDB_CHILD_TABLE) && tbUid) {
×
UNCOV
1282
    *tbUid = uid;
×
1283
  }
1284

UNCOV
1285
  pMeta->changed = true;
×
UNCOV
1286
_exit:
×
UNCOV
1287
  tdbFree(pData);
×
UNCOV
1288
  return rc;
×
1289
}
1290

1291
static int32_t metaDropTables(SMeta *pMeta, SArray *tbUids) {
38✔
1292
  int32_t code = 0;
38✔
1293
  if (taosArrayGetSize(tbUids) == 0) return TSDB_CODE_SUCCESS;
38!
1294

1295
  int64_t    nCtbDropped = 0;
38✔
1296
  SSHashObj *suidHash = tSimpleHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
38✔
1297
  if (suidHash == NULL) {
38!
UNCOV
1298
    return terrno;
×
1299
  }
1300

1301
  metaWLock(pMeta);
38✔
1302
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
5,267✔
1303
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
5,229✔
1304
    tb_uid_t suid = 0;
5,229✔
1305
    int8_t   sysTbl = 0;
5,229✔
1306
    int      type;
1307
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
5,229✔
1308
    if (code) return code;
5,229!
1309
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
5,229!
1310
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
5,229✔
1311
      if (pVal) {
5,229✔
1312
        nCtbDropped = *pVal + 1;
5,191✔
1313
      } else {
1314
        nCtbDropped = 1;
38✔
1315
      }
1316
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
5,229✔
1317
      if (code) return code;
5,229!
1318
    }
1319
    /*
1320
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
1321
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
1322
    }
1323
    */
1324
    metaDebug("batch drop table:%" PRId64, uid);
5,229✔
1325
  }
1326
  metaULock(pMeta);
38✔
1327

1328
  // update timeseries
1329
  void   *pCtbDropped = NULL;
38✔
1330
  int32_t iter = 0;
38✔
1331
  while ((pCtbDropped = tSimpleHashIterate(suidHash, pCtbDropped, &iter))) {
76✔
1332
    tb_uid_t    *pSuid = tSimpleHashGetKey(pCtbDropped, NULL);
38✔
1333
    int32_t      nCols = 0;
38✔
1334
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
38✔
1335
    if (metaGetStbStats(pMeta->pVnode, *pSuid, NULL, &nCols) == 0) {
38!
1336
      pStats->numOfTimeSeries -= *(int64_t *)pCtbDropped * (nCols - 1);
38✔
1337
    }
1338
  }
1339
  tSimpleHashCleanup(suidHash);
38✔
1340

1341
  pMeta->changed = true;
38✔
1342
  return 0;
38✔
1343
}
1344

1345
static int32_t metaFilterTableByHash(SMeta *pMeta, SArray *uidList) {
40✔
1346
  int32_t code = 0;
40✔
1347
  // 1, tranverse table's
1348
  // 2, validate table name using vnodeValidateTableHash
1349
  // 3, push invalidated table's uid into uidList
1350

1351
  TBC *pCur;
1352
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
40✔
1353
  if (code < 0) {
40!
UNCOV
1354
    return code;
×
1355
  }
1356

1357
  code = tdbTbcMoveToFirst(pCur);
40✔
1358
  if (code) {
40!
1359
    tdbTbcClose(pCur);
×
1360
    return code;
×
1361
  }
1362

1363
  void *pData = NULL, *pKey = NULL;
40✔
1364
  int   nData = 0, nKey = 0;
40✔
1365

1366
  while (1) {
10,498✔
1367
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
10,538✔
1368
    if (ret < 0) {
10,538✔
1369
      break;
40✔
1370
    }
1371

1372
    SMetaEntry me = {0};
10,498✔
1373
    SDecoder   dc = {0};
10,498✔
1374
    tDecoderInit(&dc, pData, nData);
10,498✔
1375
    code = metaDecodeEntry(&dc, &me);
10,498✔
1376
    if (code < 0) {
10,498!
UNCOV
1377
      tDecoderClear(&dc);
×
UNCOV
1378
      return code;
×
1379
    }
1380

1381
    if (me.type != TSDB_SUPER_TABLE) {
10,498✔
1382
      char tbFName[TSDB_TABLE_FNAME_LEN + 1];
1383
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pMeta->pVnode->config.dbname, me.name);
10,458✔
1384
      tbFName[TSDB_TABLE_FNAME_LEN] = '\0';
10,458✔
1385
      int32_t ret = vnodeValidateTableHash(pMeta->pVnode, tbFName);
10,458✔
1386
      if (ret < 0 && terrno == TSDB_CODE_VND_HASH_MISMATCH) {
10,458!
1387
        if (taosArrayPush(uidList, &me.uid) == NULL) {
5,229!
UNCOV
1388
          code = terrno;
×
1389
          break;
×
1390
        }
1391
      }
1392
    }
1393
    tDecoderClear(&dc);
10,498✔
1394
  }
1395
  tdbFree(pData);
40✔
1396
  tdbFree(pKey);
40✔
1397
  tdbTbcClose(pCur);
40✔
1398

1399
  return 0;
40✔
1400
}
1401

1402
int32_t metaTrimTables(SMeta *pMeta) {
40✔
1403
  int32_t code = 0;
40✔
1404

1405
  SArray *tbUids = taosArrayInit(8, sizeof(int64_t));
40✔
1406
  if (tbUids == NULL) {
40!
UNCOV
1407
    return terrno;
×
1408
  }
1409

1410
  code = metaFilterTableByHash(pMeta, tbUids);
40✔
1411
  if (code != 0) {
40!
UNCOV
1412
    goto end;
×
1413
  }
1414
  if (TARRAY_SIZE(tbUids) == 0) {
40✔
1415
    goto end;
2✔
1416
  }
1417

1418
  metaInfo("vgId:%d, trim %ld tables", TD_VID(pMeta->pVnode), taosArrayGetSize(tbUids));
38!
1419
  code = metaDropTables(pMeta, tbUids);
38✔
1420
  if (code) goto end;
38!
1421

1422
end:
38✔
1423
  taosArrayDestroy(tbUids);
40✔
1424

1425
  return code;
40✔
1426
}
1427

1428
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
18,940✔
1429
  metaRLock(pMeta);
18,940✔
1430

1431
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
19,040✔
1432

1433
  metaULock(pMeta);
19,000✔
1434

1435
  if (ret != 0) {
19,043!
UNCOV
1436
    metaError("ttl failed to find expired table, ret:%d", ret);
×
1437
  }
1438

1439
  return ret;
18,990✔
1440
}
1441

1442
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
5,229✔
1443
  int64_t btime;
1444
  if (pME->type == TSDB_CHILD_TABLE) {
5,229!
1445
    btime = pME->ctbEntry.btime;
5,229✔
UNCOV
1446
  } else if (pME->type == TSDB_NORMAL_TABLE) {
×
UNCOV
1447
    btime = pME->ntbEntry.btime;
×
1448
  } else {
UNCOV
1449
    return TSDB_CODE_FAILED;
×
1450
  }
1451

1452
  btimeKey->btime = btime;
5,229✔
1453
  btimeKey->uid = pME->uid;
5,229✔
1454
  return 0;
5,229✔
1455
}
1456

UNCOV
1457
static int metaBuildNColIdxKey(SNcolIdxKey *ncolKey, const SMetaEntry *pME) {
×
UNCOV
1458
  if (pME->type == TSDB_NORMAL_TABLE) {
×
UNCOV
1459
    ncolKey->ncol = pME->ntbEntry.schemaRow.nCols;
×
UNCOV
1460
    ncolKey->uid = pME->uid;
×
1461
  } else {
UNCOV
1462
    return TSDB_CODE_FAILED;
×
1463
  }
UNCOV
1464
  return 0;
×
1465
}
1466

1467
static void metaDeleteTtl(SMeta *pMeta, const SMetaEntry *pME) {
5,229✔
1468
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
5,229!
1469

1470
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
5,229✔
1471
  if (pME->type == TSDB_CHILD_TABLE) {
5,229!
1472
    ctx.ttlDays = pME->ctbEntry.ttlDays;
5,229✔
1473
  } else {
UNCOV
1474
    ctx.ttlDays = pME->ntbEntry.ttlDays;
×
1475
  }
1476

1477
  int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
5,229✔
1478
  if (ret < 0) {
5,229!
UNCOV
1479
    metaError("vgId:%d, failed to delete ttl for table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pME->name,
×
1480
              pME->uid, tstrerror(ret));
1481
  }
1482
  return;
5,229✔
1483
}
1484

1485
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl) {
5,229✔
1486
  void      *pData = NULL;
5,229✔
1487
  int        nData = 0;
5,229✔
1488
  int        rc = 0;
5,229✔
1489
  SMetaEntry e = {0};
5,229✔
1490
  SDecoder   dc = {0};
5,229✔
1491
  int32_t    ret = 0;
5,229✔
1492

1493
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
5,229✔
1494
  if (rc < 0) {
5,229!
UNCOV
1495
    return rc;
×
1496
  }
1497
  int64_t version = ((SUidIdxVal *)pData)[0].version;
5,229✔
1498

1499
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
5,229✔
1500
  if (rc < 0) {
5,229!
UNCOV
1501
    tdbFree(pData);
×
UNCOV
1502
    return rc;
×
1503
  }
1504

1505
  tDecoderInit(&dc, pData, nData);
5,229✔
1506
  rc = metaDecodeEntry(&dc, &e);
5,229✔
1507
  if (rc < 0) {
5,229!
UNCOV
1508
    tDecoderClear(&dc);
×
UNCOV
1509
    return rc;
×
1510
  }
1511

1512
  if (type) *type = e.type;
5,229!
1513

1514
  if (e.type == TSDB_CHILD_TABLE) {
5,229!
1515
    if (pSuid) *pSuid = e.ctbEntry.suid;
5,229!
1516
    void *tData = NULL;
5,229✔
1517
    int   tLen = 0;
5,229✔
1518

1519
    if (tdbTbGet(pMeta->pUidIdx, &e.ctbEntry.suid, sizeof(tb_uid_t), &tData, &tLen) == 0) {
5,229!
1520
      STbDbKey tbDbKey = {.uid = e.ctbEntry.suid, .version = ((SUidIdxVal *)tData)[0].version};
5,229✔
1521
      if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &tData, &tLen) == 0) {
5,229!
1522
        SDecoder   tdc = {0};
5,229✔
1523
        SMetaEntry stbEntry = {0};
5,229✔
1524

1525
        tDecoderInit(&tdc, tData, tLen);
5,229✔
1526
        int32_t ret = metaDecodeEntry(&tdc, &stbEntry);
5,229✔
1527
        if (ret < 0) {
5,229!
UNCOV
1528
          tDecoderClear(&tdc);
×
UNCOV
1529
          metaError("vgId:%d, failed to decode child table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
1530
                    e.ctbEntry.suid, tstrerror(ret));
UNCOV
1531
          return ret;
×
1532
        }
1533

1534
        if (pSysTbl) *pSysTbl = metaTbInFilterCache(pMeta, stbEntry.name, 1) ? 1 : 0;
5,229!
1535

1536
        SSchema        *pTagColumn = NULL;
5,229✔
1537
        SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
5,229✔
1538
        if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
5,229!
UNCOV
1539
          pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
×
UNCOV
1540
          ret = metaDelJsonVarFromIdx(pMeta, &e, pTagColumn);
×
UNCOV
1541
          if (ret < 0) {
×
UNCOV
1542
            metaError("vgId:%d, failed to delete json var from idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
1543
                      e.name, e.uid, tstrerror(ret));
1544
          }
1545
        } else {
1546
          for (int i = 0; i < pTagSchema->nCols; i++) {
11,342✔
1547
            pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[i];
6,113✔
1548
            if (!IS_IDX_ON(pTagColumn)) continue;
6,113✔
1549
            STagIdxKey *pTagIdxKey = NULL;
5,229✔
1550
            int32_t     nTagIdxKey;
1551

1552
            const void *pTagData = NULL;
5,229✔
1553
            int32_t     nTagData = 0;
5,229✔
1554

1555
            STagVal tagVal = {.cid = pTagColumn->colId};
5,229✔
1556
            if (tTagGet((const STag *)e.ctbEntry.pTags, &tagVal)) {
5,229!
1557
              if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
5,229!
UNCOV
1558
                pTagData = tagVal.pData;
×
UNCOV
1559
                nTagData = (int32_t)tagVal.nData;
×
1560
              } else {
1561
                pTagData = &(tagVal.i64);
5,229✔
1562
                nTagData = tDataTypes[pTagColumn->type].bytes;
5,229✔
1563
              }
1564
            } else {
UNCOV
1565
              if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
×
UNCOV
1566
                nTagData = tDataTypes[pTagColumn->type].bytes;
×
1567
              }
1568
            }
1569

1570
            if (metaCreateTagIdxKey(e.ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type, uid,
5,229!
1571
                                    &pTagIdxKey, &nTagIdxKey) == 0) {
1572
              ret = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
5,229✔
1573
              if (ret < 0) {
5,229!
UNCOV
1574
                metaError("vgId:%d, failed to delete tag idx key:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
1575
                          e.name, e.uid, tstrerror(ret));
1576
              }
1577
            }
1578
            metaDestroyTagIdxKey(pTagIdxKey);
5,229✔
1579
            pTagIdxKey = NULL;
5,229✔
1580
          }
1581
        }
1582
        tDecoderClear(&tdc);
5,229✔
1583
      }
1584
      tdbFree(tData);
5,229✔
1585
    }
1586
  }
1587

1588
  ret = tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), pMeta->txn);
5,229✔
1589
  if (ret < 0) {
5,229!
UNCOV
1590
    metaError("vgId:%d, failed to delete table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
1591
              tstrerror(ret));
1592
  }
1593
  ret = tdbTbDelete(pMeta->pNameIdx, e.name, strlen(e.name) + 1, pMeta->txn);
5,229✔
1594
  if (ret < 0) {
5,229!
1595
    metaError("vgId:%d, failed to delete name idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
1596
              tstrerror(ret));
1597
  }
1598
  ret = tdbTbDelete(pMeta->pUidIdx, &uid, sizeof(uid), pMeta->txn);
5,229✔
1599
  if (ret < 0) {
5,229!
UNCOV
1600
    metaError("vgId:%d, failed to delete uid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
1601
              tstrerror(ret));
1602
  }
1603

1604
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
5,229!
1605
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
5,229!
1606

1607
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
5,229!
1608

1609
  if (e.type == TSDB_CHILD_TABLE) {
5,229!
1610
    ret =
1611
        tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
5,229✔
1612
    if (ret < 0) {
5,229!
UNCOV
1613
      metaError("vgId:%d, failed to delete ctb idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
1614
                tstrerror(ret));
1615
    }
1616

1617
    --pMeta->pVnode->config.vndStats.numOfCTables;
5,229✔
1618
    metaUpdateStbStats(pMeta, e.ctbEntry.suid, -1, 0);
5,229✔
1619
    ret = metaUidCacheClear(pMeta, e.ctbEntry.suid);
5,229✔
1620
    if (ret < 0) {
5,229!
UNCOV
1621
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
1622
                e.ctbEntry.suid, tstrerror(ret));
1623
    }
1624
    ret = metaTbGroupCacheClear(pMeta, e.ctbEntry.suid);
5,229✔
1625
    if (ret < 0) {
5,229!
UNCOV
1626
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
1627
                e.ctbEntry.suid, tstrerror(ret));
1628
    }
1629
    /*
1630
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
1631
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, e.ctbEntry.suid, NULL);
1632
    }
1633
    */
UNCOV
1634
  } else if (e.type == TSDB_NORMAL_TABLE) {
×
1635
    // drop schema.db (todo)
1636

1637
    --pMeta->pVnode->config.vndStats.numOfNTables;
×
UNCOV
1638
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
×
1639

1640
    /*
1641
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
1642
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, -1, &e.ntbEntry.schemaRow);
1643
    }
1644
    */
1645
  } else if (e.type == TSDB_SUPER_TABLE) {
×
1646
    ret = tdbTbDelete(pMeta->pSuidIdx, &e.uid, sizeof(tb_uid_t), pMeta->txn);
×
1647
    if (ret < 0) {
×
UNCOV
1648
      metaError("vgId:%d, failed to delete suid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
1649
                tstrerror(ret));
1650
    }
1651
    // drop schema.db (todo)
1652

UNCOV
1653
    ret = metaStatsCacheDrop(pMeta, uid);
×
UNCOV
1654
    if (ret < 0) {
×
UNCOV
1655
      metaError("vgId:%d, failed to drop stats cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
1656
                tstrerror(ret));
1657
    }
UNCOV
1658
    ret = metaUidCacheClear(pMeta, uid);
×
UNCOV
1659
    if (ret < 0) {
×
UNCOV
1660
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
1661
                tstrerror(ret));
1662
    }
UNCOV
1663
    ret = metaTbGroupCacheClear(pMeta, uid);
×
UNCOV
1664
    if (ret < 0) {
×
UNCOV
1665
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
1666
                e.uid, tstrerror(ret));
1667
    }
UNCOV
1668
    --pMeta->pVnode->config.vndStats.numOfSTables;
×
1669
  }
1670

1671
  ret = metaCacheDrop(pMeta, uid);
5,229✔
1672
  if (ret < 0) {
5,229!
1673
    metaError("vgId:%d, failed to drop cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
5,229!
1674
              tstrerror(ret));
1675
  }
1676

1677
  tDecoderClear(&dc);
5,229✔
1678
  tdbFree(pData);
5,229✔
1679

1680
  return 0;
5,229✔
1681
}
1682
// opt ins_tables
UNCOV
1683
static int metaUpdateBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
×
UNCOV
1684
  SBtimeIdxKey btimeKey = {0};
×
UNCOV
1685
  if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) {
×
1686
    return 0;
×
1687
  }
UNCOV
1688
  metaTrace("vgId:%d, start to save version:%" PRId64 " uid:%" PRId64 " btime:%" PRId64, TD_VID(pMeta->pVnode),
×
1689
            pME->version, pME->uid, btimeKey.btime);
1690

UNCOV
1691
  return tdbTbUpsert(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), NULL, 0, pMeta->txn);
×
1692
}
1693

1694
static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
5,229✔
1695
  SBtimeIdxKey btimeKey = {0};
5,229✔
1696
  if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) {
5,229!
UNCOV
1697
    return 0;
×
1698
  }
1699
  return tdbTbDelete(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), pMeta->txn);
5,229✔
1700
}
1701

UNCOV
1702
int metaUpdateNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
×
UNCOV
1703
  SNcolIdxKey ncolKey = {0};
×
UNCOV
1704
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
×
UNCOV
1705
    return 0;
×
1706
  }
UNCOV
1707
  return tdbTbUpsert(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), NULL, 0, pMeta->txn);
×
1708
}
1709

UNCOV
1710
int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
×
UNCOV
1711
  SNcolIdxKey ncolKey = {0};
×
UNCOV
1712
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
×
1713
    return 0;
×
1714
  }
UNCOV
1715
  return tdbTbDelete(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), pMeta->txn);
×
1716
}
1717

UNCOV
1718
static int metaAlterTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq, STableMetaRsp *pMetaRsp) {
×
UNCOV
1719
  void           *pVal = NULL;
×
1720
  int             nVal = 0;
×
UNCOV
1721
  const void     *pData = NULL;
×
UNCOV
1722
  int             nData = 0;
×
UNCOV
1723
  int             ret = 0;
×
1724
  tb_uid_t        uid;
1725
  int64_t         oversion;
UNCOV
1726
  SSchema        *pColumn = NULL;
×
UNCOV
1727
  SMetaEntry      entry = {0};
×
1728
  SSchemaWrapper *pSchema;
1729
  int             c;
UNCOV
1730
  bool            freeColCmpr = false;
×
UNCOV
1731
  if (pAlterTbReq->colName == NULL) {
×
UNCOV
1732
    metaError("meta/table: null pAlterTbReq->colName");
×
1733
    return terrno = TSDB_CODE_INVALID_MSG;
×
1734
  }
1735

1736
  // search name index
UNCOV
1737
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
×
UNCOV
1738
  if (ret < 0) {
×
UNCOV
1739
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1740
  }
1741

UNCOV
1742
  uid = *(tb_uid_t *)pVal;
×
UNCOV
1743
  tdbFree(pVal);
×
UNCOV
1744
  pVal = NULL;
×
1745

1746
  // search uid index
1747
  TBC *pUidIdxc = NULL;
×
1748

1749
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, NULL));
×
1750
  ret = tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c);
×
UNCOV
1751
  if (c != 0) {
×
UNCOV
1752
    tdbTbcClose(pUidIdxc);
×
UNCOV
1753
    metaError("meta/table: invalide c: %" PRId32 " alt tb column failed.", c);
×
UNCOV
1754
    return TSDB_CODE_FAILED;
×
1755
  }
1756

UNCOV
1757
  ret = tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData);
×
1758
  oversion = ((SUidIdxVal *)pData)[0].version;
×
1759

1760
  // search table.db
UNCOV
1761
  TBC *pTbDbc = NULL;
×
1762

UNCOV
1763
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pTbDb, &pTbDbc, NULL));
×
UNCOV
1764
  ret = tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c);
×
UNCOV
1765
  if (c != 0) {
×
1766
    tdbTbcClose(pUidIdxc);
×
1767
    tdbTbcClose(pTbDbc);
×
1768
    metaError("meta/table: invalide c: %" PRId32 " alt tb column failed.", c);
×
1769
    return TSDB_CODE_FAILED;
×
1770
  }
1771

UNCOV
1772
  ret = tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData);
×
1773

1774
  // get table entry
UNCOV
1775
  SDecoder dc = {0};
×
UNCOV
1776
  if ((entry.pBuf = taosMemoryMalloc(nData)) == NULL) {
×
UNCOV
1777
    tdbTbcClose(pUidIdxc);
×
UNCOV
1778
    tdbTbcClose(pTbDbc);
×
UNCOV
1779
    return terrno;
×
1780
  }
UNCOV
1781
  memcpy(entry.pBuf, pData, nData);
×
UNCOV
1782
  tDecoderInit(&dc, entry.pBuf, nData);
×
UNCOV
1783
  ret = metaDecodeEntry(&dc, &entry);
×
UNCOV
1784
  if (ret != 0) {
×
UNCOV
1785
    tdbTbcClose(pUidIdxc);
×
UNCOV
1786
    tdbTbcClose(pTbDbc);
×
UNCOV
1787
    tDecoderClear(&dc);
×
UNCOV
1788
    metaError("meta/table: invalide ret: %" PRId32 " alt tb column failed.", ret);
×
UNCOV
1789
    return ret;
×
1790
  }
1791

UNCOV
1792
  if (entry.type != TSDB_NORMAL_TABLE) {
×
UNCOV
1793
    terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
UNCOV
1794
    goto _err;
×
1795
  }
1796
  // search the column to add/drop/update
UNCOV
1797
  pSchema = &entry.ntbEntry.schemaRow;
×
1798

1799
  // save old entry
UNCOV
1800
  SMetaEntry oldEntry = {.type = TSDB_NORMAL_TABLE, .uid = entry.uid};
×
UNCOV
1801
  oldEntry.ntbEntry.schemaRow.nCols = pSchema->nCols;
×
1802

UNCOV
1803
  int32_t rowLen = -1;
×
UNCOV
1804
  if (pAlterTbReq->action == TSDB_ALTER_TABLE_ADD_COLUMN ||
×
UNCOV
1805
      pAlterTbReq->action == TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES) {
×
UNCOV
1806
    rowLen = 0;
×
1807
  }
1808

UNCOV
1809
  int32_t  iCol = 0, jCol = 0;
×
UNCOV
1810
  SSchema *qColumn = NULL;
×
1811
  for (;;) {
UNCOV
1812
    qColumn = NULL;
×
1813

UNCOV
1814
    if (jCol >= pSchema->nCols) break;
×
1815
    qColumn = &pSchema->pSchema[jCol];
×
1816

UNCOV
1817
    if (!pColumn && (strcmp(qColumn->name, pAlterTbReq->colName) == 0)) {
×
UNCOV
1818
      pColumn = qColumn;
×
1819
      iCol = jCol;
×
UNCOV
1820
      if (rowLen < 0) break;
×
1821
    }
1822
    rowLen += qColumn->bytes;
×
1823
    ++jCol;
×
1824
  }
1825

UNCOV
1826
  entry.version = version;
×
1827
  int      tlen;
UNCOV
1828
  SSchema *pNewSchema = NULL;
×
1829
  SSchema  tScheam;
UNCOV
1830
  switch (pAlterTbReq->action) {
×
UNCOV
1831
    case TSDB_ALTER_TABLE_ADD_COLUMN:
×
1832
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
UNCOV
1833
      if (pColumn) {
×
UNCOV
1834
        terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
×
UNCOV
1835
        goto _err;
×
1836
      }
UNCOV
1837
      if ((terrno = grantCheck(TSDB_GRANT_TIMESERIES)) < 0) {
×
UNCOV
1838
        goto _err;
×
1839
      }
UNCOV
1840
      if (rowLen + pAlterTbReq->bytes > TSDB_MAX_BYTES_PER_ROW) {
×
UNCOV
1841
        terrno = TSDB_CODE_PAR_INVALID_ROW_LENGTH;
×
UNCOV
1842
        goto _err;
×
1843
      }
UNCOV
1844
      pSchema->version++;
×
UNCOV
1845
      pSchema->nCols++;
×
UNCOV
1846
      pNewSchema = taosMemoryMalloc(sizeof(SSchema) * pSchema->nCols);
×
UNCOV
1847
      if (pNewSchema == NULL) {
×
1848
        goto _err;
×
1849
      }
UNCOV
1850
      memcpy(pNewSchema, pSchema->pSchema, sizeof(SSchema) * (pSchema->nCols - 1));
×
UNCOV
1851
      pSchema->pSchema = pNewSchema;
×
UNCOV
1852
      pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].bytes = pAlterTbReq->bytes;
×
UNCOV
1853
      pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].type = pAlterTbReq->type;
×
UNCOV
1854
      pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].flags = pAlterTbReq->flags;
×
UNCOV
1855
      pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].colId = entry.ntbEntry.ncid++;
×
1856
      tstrncpy(pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].name, pAlterTbReq->colName,
×
1857
               strlen(pAlterTbReq->colName) + 1);
1858

UNCOV
1859
      ++pMeta->pVnode->config.vndStats.numOfNTimeSeries;
×
UNCOV
1860
      metaTimeSeriesNotifyCheck(pMeta);
×
1861

1862
      if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
×
1863
        int16_t cid = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].colId;
×
1864
        int8_t  col_type = pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].type;
×
UNCOV
1865
        int32_t ret = tsdbCacheNewNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, col_type);
×
UNCOV
1866
        if (ret < 0) {
×
UNCOV
1867
          terrno = ret;
×
UNCOV
1868
          goto _err;
×
1869
        }
1870
      }
UNCOV
1871
      SSchema *pCol = &pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1];
×
UNCOV
1872
      uint32_t compress = pAlterTbReq->action == TSDB_ALTER_TABLE_ADD_COLUMN ? createDefaultColCmprByType(pCol->type)
×
1873
                                                                             : pAlterTbReq->compress;
×
1874
      if (updataTableColCmpr(&entry.colCmpr, pCol, 1, compress) != 0) {
×
UNCOV
1875
        metaError("vgId:%d, failed to update table col cmpr:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name,
×
1876
                  entry.uid);
1877
      }
UNCOV
1878
      freeColCmpr = true;
×
UNCOV
1879
      if (entry.colCmpr.nCols != pSchema->nCols) {
×
UNCOV
1880
        if (pNewSchema) taosMemoryFree(pNewSchema);
×
UNCOV
1881
        if (freeColCmpr) taosMemoryFree(entry.colCmpr.pColCmpr);
×
UNCOV
1882
        terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
UNCOV
1883
        goto _err;
×
1884
      }
UNCOV
1885
      break;
×
UNCOV
1886
    case TSDB_ALTER_TABLE_DROP_COLUMN:
×
UNCOV
1887
      if (pColumn == NULL) {
×
UNCOV
1888
        terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
×
UNCOV
1889
        goto _err;
×
1890
      }
UNCOV
1891
      if (pColumn->colId == 0) {
×
UNCOV
1892
        terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
UNCOV
1893
        goto _err;
×
1894
      }
UNCOV
1895
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
×
UNCOV
1896
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
×
UNCOV
1897
        goto _err;
×
1898
      }
1899
      bool hasPrimayKey = false;
×
UNCOV
1900
      if (pSchema->nCols >= 2) {
×
UNCOV
1901
        hasPrimayKey = pSchema->pSchema[1].flags & COL_IS_KEY ? true : false;
×
1902
      }
1903

UNCOV
1904
      memcpy(&tScheam, pColumn, sizeof(SSchema));
×
1905
      pSchema->version++;
×
UNCOV
1906
      tlen = (pSchema->nCols - iCol - 1) * sizeof(SSchema);
×
UNCOV
1907
      if (tlen) {
×
UNCOV
1908
        memmove(pColumn, pColumn + 1, tlen);
×
1909
      }
1910
      pSchema->nCols--;
×
1911

UNCOV
1912
      --pMeta->pVnode->config.vndStats.numOfNTimeSeries;
×
1913

UNCOV
1914
      if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
×
1915
        int16_t cid = pColumn->colId;
×
1916

UNCOV
1917
        if (tsdbCacheDropNTableColumn(pMeta->pVnode->pTsdb, entry.uid, cid, hasPrimayKey) != 0) {
×
UNCOV
1918
          metaError("vgId:%d, failed to drop ntable column:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name,
×
1919
                    entry.uid);
1920
        }
1921
      }
1922

1923
      if (updataTableColCmpr(&entry.colCmpr, &tScheam, 0, 0) != 0) {
×
1924
        metaError("vgId:%d, failed to update table col cmpr:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name,
×
1925
                  entry.uid);
1926
      }
UNCOV
1927
      if (entry.colCmpr.nCols != pSchema->nCols) {
×
UNCOV
1928
        terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
UNCOV
1929
        goto _err;
×
1930
      }
UNCOV
1931
      break;
×
UNCOV
1932
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
×
UNCOV
1933
      if (pColumn == NULL) {
×
UNCOV
1934
        terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
×
1935
        goto _err;
×
1936
      }
UNCOV
1937
      if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes >= pAlterTbReq->colModBytes) {
×
UNCOV
1938
        terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
1939
        goto _err;
×
1940
      }
UNCOV
1941
      if (rowLen + pAlterTbReq->colModBytes - pColumn->bytes > TSDB_MAX_BYTES_PER_ROW) {
×
UNCOV
1942
        terrno = TSDB_CODE_PAR_INVALID_ROW_LENGTH;
×
UNCOV
1943
        goto _err;
×
1944
      }
UNCOV
1945
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
×
UNCOV
1946
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
×
UNCOV
1947
        goto _err;
×
1948
      }
UNCOV
1949
      pSchema->version++;
×
UNCOV
1950
      pColumn->bytes = pAlterTbReq->colModBytes;
×
UNCOV
1951
      break;
×
UNCOV
1952
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
×
UNCOV
1953
      if (pAlterTbReq->colNewName == NULL) {
×
UNCOV
1954
        terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
1955
        goto _err;
×
1956
      }
UNCOV
1957
      if (pColumn == NULL) {
×
UNCOV
1958
        terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
×
UNCOV
1959
        goto _err;
×
1960
      }
1961
      if (tqCheckColModifiable(pMeta->pVnode->pTq, uid, pColumn->colId) != 0) {
×
UNCOV
1962
        terrno = TSDB_CODE_VND_COL_SUBSCRIBED;
×
UNCOV
1963
        goto _err;
×
1964
      }
1965
      pSchema->version++;
×
UNCOV
1966
      tstrncpy(pColumn->name, pAlterTbReq->colNewName, strlen(pAlterTbReq->colNewName) + 1);
×
UNCOV
1967
      break;
×
1968
  }
1969

1970
  if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
×
UNCOV
1971
    tsdbCacheInvalidateSchema(pMeta->pVnode->pTsdb, 0, entry.uid, pSchema->version);
×
1972
  }
1973

1974
  entry.version = version;
×
1975

1976
  // do actual write
UNCOV
1977
  metaWLock(pMeta);
×
1978

UNCOV
1979
  if (metaDeleteNcolIdx(pMeta, &oldEntry) < 0) {
×
UNCOV
1980
    metaError("vgId:%d, failed to delete ncol idx:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
×
1981
  }
1982

UNCOV
1983
  if (metaUpdateNcolIdx(pMeta, &entry) < 0) {
×
UNCOV
1984
    metaError("vgId:%d, failed to update ncol idx:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
×
1985
  }
1986

1987
  // save to table db
1988
  if (metaSaveToTbDb(pMeta, &entry) < 0) {
×
UNCOV
1989
    metaError("vgId:%d, failed to save to tb db:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
×
1990
  }
1991

UNCOV
1992
  if (metaUpdateUidIdx(pMeta, &entry) < 0) {
×
UNCOV
1993
    metaError("vgId:%d, failed to update uid idx:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
×
1994
  }
1995

UNCOV
1996
  if (metaSaveToSkmDb(pMeta, &entry) < 0) {
×
UNCOV
1997
    metaError("vgId:%d, failed to save to skm db:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
×
1998
  }
1999

UNCOV
2000
  if (metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs) < 0) {
×
UNCOV
2001
    metaError("vgId:%d, failed to update change time:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
×
2002
  }
2003

UNCOV
2004
  metaULock(pMeta);
×
2005

UNCOV
2006
  if (metaUpdateMetaRsp(uid, pAlterTbReq->tbName, pSchema, pMetaRsp) < 0) {
×
UNCOV
2007
    metaError("vgId:%d, failed to update meta rsp:%s uid:%" PRId64, TD_VID(pMeta->pVnode), entry.name, entry.uid);
×
2008
  }
UNCOV
2009
  for (int32_t i = 0; i < entry.colCmpr.nCols; i++) {
×
UNCOV
2010
    SColCmpr *p = &entry.colCmpr.pColCmpr[i];
×
UNCOV
2011
    pMetaRsp->pSchemaExt[i].colId = p->id;
×
UNCOV
2012
    pMetaRsp->pSchemaExt[i].compress = p->alg;
×
2013
  }
2014

UNCOV
2015
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
×
UNCOV
2016
  if (pNewSchema) taosMemoryFree(pNewSchema);
×
UNCOV
2017
  if (freeColCmpr) taosMemoryFree(entry.colCmpr.pColCmpr);
×
2018

UNCOV
2019
  tdbTbcClose(pTbDbc);
×
UNCOV
2020
  tdbTbcClose(pUidIdxc);
×
UNCOV
2021
  tDecoderClear(&dc);
×
2022

UNCOV
2023
  return 0;
×
2024

UNCOV
2025
_err:
×
UNCOV
2026
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
×
UNCOV
2027
  tdbTbcClose(pTbDbc);
×
UNCOV
2028
  tdbTbcClose(pUidIdxc);
×
UNCOV
2029
  tDecoderClear(&dc);
×
2030

2031
  return terrno != 0 ? terrno : TSDB_CODE_FAILED;
×
2032
}
2033

UNCOV
2034
static int metaUpdateTableMultiTagVal(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
×
UNCOV
2035
  SMetaEntry  ctbEntry = {0};
×
UNCOV
2036
  SMetaEntry  stbEntry = {0};
×
UNCOV
2037
  void       *pVal = NULL;
×
UNCOV
2038
  int         nVal = 0;
×
2039
  int         ret;
2040
  int         c;
2041
  tb_uid_t    uid;
2042
  int64_t     oversion;
2043
  const void *pData = NULL;
×
UNCOV
2044
  int         nData = 0;
×
UNCOV
2045
  SHashObj   *pTagTable = NULL;
×
2046

2047
  // search name index
2048
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
×
UNCOV
2049
  if (ret < 0) {
×
UNCOV
2050
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2051
  }
2052

2053
  uid = *(tb_uid_t *)pVal;
×
2054
  tdbFree(pVal);
×
UNCOV
2055
  pVal = NULL;
×
2056

2057
  // search uid index
UNCOV
2058
  TBC *pUidIdxc = NULL;
×
2059

UNCOV
2060
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, NULL));
×
UNCOV
2061
  if (tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c) < 0) {
×
UNCOV
2062
    metaTrace("meta/table: failed to move to uid index, uid:%" PRId64, uid);
×
2063
  }
UNCOV
2064
  if (c != 0) {
×
UNCOV
2065
    tdbTbcClose(pUidIdxc);
×
2066
    metaError("meta/table: invalide c: %" PRId32 " update tb tag val failed.", c);
×
UNCOV
2067
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2068
  }
2069

2070
  if (tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData) != 0) {
×
2071
    tdbTbcClose(pUidIdxc);
×
2072
    metaError("meta/table: failed to get uid index, uid:%" PRId64, uid);
×
UNCOV
2073
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2074
  }
UNCOV
2075
  oversion = ((SUidIdxVal *)pData)[0].version;
×
2076

2077
  // search table.db
2078
  TBC     *pTbDbc = NULL;
×
2079
  SDecoder dc1 = {0};
×
UNCOV
2080
  SDecoder dc2 = {0};
×
2081

2082
  /* get ctbEntry */
2083
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pTbDb, &pTbDbc, NULL));
×
2084
  if (tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c) != 0) {
×
2085
    metaError("meta/table: failed to move to tb db, uid:%" PRId64, uid);
×
2086
  }
UNCOV
2087
  if (c != 0) {
×
UNCOV
2088
    tdbTbcClose(pUidIdxc);
×
UNCOV
2089
    tdbTbcClose(pTbDbc);
×
UNCOV
2090
    metaError("meta/table: invalide c: %" PRId32 " update tb tag val failed.", c);
×
2091
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2092
  }
2093

UNCOV
2094
  if (tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData) != 0) {
×
UNCOV
2095
    metaError("meta/table: failed to get tb db, uid:%" PRId64, uid);
×
UNCOV
2096
    tdbTbcClose(pUidIdxc);
×
2097
    tdbTbcClose(pTbDbc);
×
UNCOV
2098
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2099
  }
2100

2101
  if ((ctbEntry.pBuf = taosMemoryMalloc(nData)) == NULL) {
×
UNCOV
2102
    tdbTbcClose(pUidIdxc);
×
UNCOV
2103
    tdbTbcClose(pTbDbc);
×
UNCOV
2104
    return terrno;
×
2105
  }
2106
  memcpy(ctbEntry.pBuf, pData, nData);
×
UNCOV
2107
  tDecoderInit(&dc1, ctbEntry.pBuf, nData);
×
UNCOV
2108
  ret = metaDecodeEntry(&dc1, &ctbEntry);
×
UNCOV
2109
  if (ret < 0) {
×
UNCOV
2110
    terrno = ret;
×
UNCOV
2111
    goto _err;
×
2112
  }
2113

2114
  /* get stbEntry*/
UNCOV
2115
  if (tdbTbGet(pMeta->pUidIdx, &ctbEntry.ctbEntry.suid, sizeof(tb_uid_t), &pVal, &nVal) != 0) {
×
UNCOV
2116
    metaError("meta/table: failed to get uid index, uid:%" PRId64, ctbEntry.ctbEntry.suid);
×
2117
  }
UNCOV
2118
  if (!pVal) {
×
2119
    terrno = TSDB_CODE_INVALID_MSG;
×
2120
    goto _err;
×
2121
  }
2122

UNCOV
2123
  if (tdbTbGet(pMeta->pTbDb, &((STbDbKey){.uid = ctbEntry.ctbEntry.suid, .version = ((SUidIdxVal *)pVal)[0].version}),
×
2124
               sizeof(STbDbKey), (void **)&stbEntry.pBuf, &nVal) != 0) {
UNCOV
2125
    metaError("meta/table: failed to get tb db, uid:%" PRId64, ctbEntry.ctbEntry.suid);
×
2126
  }
UNCOV
2127
  tdbFree(pVal);
×
2128
  tDecoderInit(&dc2, stbEntry.pBuf, nVal);
×
UNCOV
2129
  ret = metaDecodeEntry(&dc2, &stbEntry);
×
UNCOV
2130
  if (ret < 0) {
×
UNCOV
2131
    terrno = ret;
×
UNCOV
2132
    goto _err;
×
2133
  }
2134

UNCOV
2135
  int32_t nTagVals = taosArrayGetSize(pAlterTbReq->pMultiTag);
×
UNCOV
2136
  pTagTable = taosHashInit(nTagVals, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
×
UNCOV
2137
  if (pTagTable == NULL) {
×
UNCOV
2138
    ret = terrno;
×
UNCOV
2139
    goto _err;
×
2140
  }
2141

2142
  // remove duplicate tag name
UNCOV
2143
  for (int i = 0; i < nTagVals; i++) {
×
UNCOV
2144
    SMultiTagUpateVal *pTagVal = taosArrayGet(pAlterTbReq->pMultiTag, i);
×
UNCOV
2145
    ret = taosHashPut(pTagTable, pTagVal->tagName, strlen(pTagVal->tagName), pTagVal, sizeof(*pTagVal));
×
UNCOV
2146
    if (ret != 0) {
×
UNCOV
2147
      goto _err;
×
2148
    }
2149
  }
2150

UNCOV
2151
  SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
×
UNCOV
2152
  SSchema        *pColumn = NULL;
×
UNCOV
2153
  int32_t         iCol = 0;
×
2154
  int32_t         count = 0;
×
2155

2156
  for (;;) {
UNCOV
2157
    pColumn = NULL;
×
2158

UNCOV
2159
    if (iCol >= pTagSchema->nCols) break;
×
UNCOV
2160
    pColumn = &pTagSchema->pSchema[iCol];
×
2161
    if (taosHashGet(pTagTable, pColumn->name, strlen(pColumn->name)) != NULL) {
×
2162
      count++;
×
2163
    }
UNCOV
2164
    iCol++;
×
2165
  }
UNCOV
2166
  if (count != taosHashGetSize(pTagTable)) {
×
UNCOV
2167
    terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
×
UNCOV
2168
    goto _err;
×
2169
  }
2170

2171
  ctbEntry.version = version;
×
2172
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
×
2173
    terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
×
UNCOV
2174
    goto _err;
×
2175
  } else {
UNCOV
2176
    const STag *pOldTag = (const STag *)ctbEntry.ctbEntry.pTags;
×
UNCOV
2177
    STag       *pNewTag = NULL;
×
UNCOV
2178
    SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
×
UNCOV
2179
    if (!pTagArray) {
×
UNCOV
2180
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
2181
      goto _err;
×
2182
    }
UNCOV
2183
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
×
UNCOV
2184
      SSchema           *pCol = &pTagSchema->pSchema[i];
×
UNCOV
2185
      SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
×
UNCOV
2186
      if (pTagVal == NULL) {
×
UNCOV
2187
        STagVal val = {.cid = pCol->colId};
×
UNCOV
2188
        if (tTagGet(pOldTag, &val)) {
×
2189
          if (taosArrayPush(pTagArray, &val) == NULL) {
×
2190
            terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2191
            taosArrayDestroy(pTagArray);
×
UNCOV
2192
            goto _err;
×
2193
          }
2194
        }
2195
      } else {
2196
        STagVal val = {0};
×
2197
        val.type = pCol->type;
×
UNCOV
2198
        val.cid = pCol->colId;
×
UNCOV
2199
        if (pTagVal->isNull) continue;
×
2200

UNCOV
2201
        if (IS_VAR_DATA_TYPE(pCol->type)) {
×
UNCOV
2202
          val.pData = pTagVal->pTagVal;
×
UNCOV
2203
          val.nData = pTagVal->nTagVal;
×
2204
        } else {
UNCOV
2205
          memcpy(&val.i64, pTagVal->pTagVal, pTagVal->nTagVal);
×
2206
        }
2207
        if (taosArrayPush(pTagArray, &val) == NULL) {
×
UNCOV
2208
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
2209
          taosArrayDestroy(pTagArray);
×
UNCOV
2210
          goto _err;
×
2211
        }
2212
      }
2213
    }
UNCOV
2214
    if ((terrno = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag)) < 0) {
×
UNCOV
2215
      taosArrayDestroy(pTagArray);
×
2216
      goto _err;
×
2217
    }
UNCOV
2218
    ctbEntry.ctbEntry.pTags = (uint8_t *)pNewTag;
×
UNCOV
2219
    taosArrayDestroy(pTagArray);
×
2220
  }
2221

2222
  metaWLock(pMeta);
×
2223

2224
  // save to table.db
UNCOV
2225
  if (metaSaveToTbDb(pMeta, &ctbEntry) < 0) {
×
2226
    metaError("meta/table: failed to save to tb db:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2227
  }
2228

2229
  // save to uid.idx
2230
  if (metaUpdateUidIdx(pMeta, &ctbEntry) < 0) {
×
UNCOV
2231
    metaError("meta/table: failed to update uid idx:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2232
  }
2233

2234
  if (metaUpdateTagIdx(pMeta, &ctbEntry) < 0) {
×
UNCOV
2235
    metaError("meta/table: failed to update tag idx:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2236
  }
2237

UNCOV
2238
  SCtbIdxKey ctbIdxKey = {.suid = ctbEntry.ctbEntry.suid, .uid = uid};
×
UNCOV
2239
  if (tdbTbUpsert(pMeta->pCtbIdx, &ctbIdxKey, sizeof(ctbIdxKey), ctbEntry.ctbEntry.pTags,
×
UNCOV
2240
                  ((STag *)(ctbEntry.ctbEntry.pTags))->len, pMeta->txn) < 0) {
×
UNCOV
2241
    metaError("meta/table: failed to upsert ctb idx:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2242
  }
2243

UNCOV
2244
  if (metaUidCacheClear(pMeta, ctbEntry.ctbEntry.suid) < 0) {
×
UNCOV
2245
    metaError("meta/table: failed to clear uid cache:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2246
  }
2247

UNCOV
2248
  if (metaTbGroupCacheClear(pMeta, ctbEntry.ctbEntry.suid) < 0) {
×
2249
    metaError("meta/table: failed to clear group cache:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2250
  }
2251

2252
  if (metaUpdateChangeTime(pMeta, ctbEntry.uid, pAlterTbReq->ctimeMs) < 0) {
×
2253
    metaError("meta/table: failed to update change time:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2254
  }
2255

2256
  metaULock(pMeta);
×
2257

UNCOV
2258
  tDecoderClear(&dc1);
×
UNCOV
2259
  tDecoderClear(&dc2);
×
UNCOV
2260
  taosMemoryFree((void *)ctbEntry.ctbEntry.pTags);
×
UNCOV
2261
  if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
×
UNCOV
2262
  if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
×
UNCOV
2263
  tdbTbcClose(pTbDbc);
×
UNCOV
2264
  tdbTbcClose(pUidIdxc);
×
UNCOV
2265
  taosHashCleanup(pTagTable);
×
UNCOV
2266
  return 0;
×
2267

UNCOV
2268
_err:
×
UNCOV
2269
  tDecoderClear(&dc1);
×
UNCOV
2270
  tDecoderClear(&dc2);
×
UNCOV
2271
  if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
×
2272
  if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
×
UNCOV
2273
  tdbTbcClose(pTbDbc);
×
UNCOV
2274
  tdbTbcClose(pUidIdxc);
×
UNCOV
2275
  taosHashCleanup(pTagTable);
×
UNCOV
2276
  return -1;
×
2277
}
UNCOV
2278
static int metaUpdateTableTagVal(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
×
UNCOV
2279
  SMetaEntry  ctbEntry = {0};
×
UNCOV
2280
  SMetaEntry  stbEntry = {0};
×
UNCOV
2281
  void       *pVal = NULL;
×
UNCOV
2282
  int         nVal = 0;
×
2283
  int         ret;
2284
  int         c;
2285
  tb_uid_t    uid;
2286
  int64_t     oversion;
UNCOV
2287
  const void *pData = NULL;
×
UNCOV
2288
  int         nData = 0;
×
2289

2290
  if (pAlterTbReq->tagName == NULL) {
×
UNCOV
2291
    return terrno = TSDB_CODE_INVALID_MSG;
×
2292
  }
2293

2294
  // search name index
2295
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
×
UNCOV
2296
  if (ret < 0) {
×
UNCOV
2297
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2298
  }
2299

2300
  uid = *(tb_uid_t *)pVal;
×
2301
  tdbFree(pVal);
×
UNCOV
2302
  pVal = NULL;
×
2303

2304
  // search uid index
UNCOV
2305
  TBC *pUidIdxc = NULL;
×
2306

UNCOV
2307
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, NULL));
×
UNCOV
2308
  if (tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c) < 0) {
×
UNCOV
2309
    metaTrace("meta/table: failed to move to uid index, uid:%" PRId64, uid);
×
2310
  }
UNCOV
2311
  if (c != 0) {
×
UNCOV
2312
    tdbTbcClose(pUidIdxc);
×
2313
    metaError("meta/table: invalide c: %" PRId32 " update tb tag val failed.", c);
×
UNCOV
2314
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2315
  }
2316

2317
  if (tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData) != 0) {
×
2318
    tdbTbcClose(pUidIdxc);
×
2319
    metaError("meta/table: failed to get uid index, uid:%" PRId64, uid);
×
UNCOV
2320
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2321
  }
UNCOV
2322
  oversion = ((SUidIdxVal *)pData)[0].version;
×
2323

2324
  // search table.db
2325
  TBC     *pTbDbc = NULL;
×
2326
  SDecoder dc1 = {0};
×
UNCOV
2327
  SDecoder dc2 = {0};
×
2328

2329
  /* get ctbEntry */
2330
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pTbDb, &pTbDbc, NULL));
×
2331
  if (tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c) != 0) {
×
2332
    metaError("meta/table: failed to move to tb db, uid:%" PRId64, uid);
×
2333
  }
UNCOV
2334
  if (c != 0) {
×
UNCOV
2335
    tdbTbcClose(pUidIdxc);
×
UNCOV
2336
    tdbTbcClose(pTbDbc);
×
UNCOV
2337
    metaError("meta/table: invalide c: %" PRId32 " update tb tag val failed.", c);
×
2338
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2339
  }
2340

UNCOV
2341
  if (tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData) != 0) {
×
UNCOV
2342
    metaError("meta/table: failed to get tb db, uid:%" PRId64, uid);
×
UNCOV
2343
    tdbTbcClose(pUidIdxc);
×
2344
    tdbTbcClose(pTbDbc);
×
UNCOV
2345
    return terrno = TSDB_CODE_INVALID_MSG;
×
2346
  }
2347

2348
  if ((ctbEntry.pBuf = taosMemoryMalloc(nData)) == NULL) {
×
UNCOV
2349
    tdbTbcClose(pUidIdxc);
×
UNCOV
2350
    tdbTbcClose(pTbDbc);
×
UNCOV
2351
    return terrno;
×
2352
  }
2353
  memcpy(ctbEntry.pBuf, pData, nData);
×
UNCOV
2354
  tDecoderInit(&dc1, ctbEntry.pBuf, nData);
×
UNCOV
2355
  ret = metaDecodeEntry(&dc1, &ctbEntry);
×
UNCOV
2356
  if (ret < 0) {
×
UNCOV
2357
    terrno = ret;
×
UNCOV
2358
    goto _err;
×
2359
  }
2360

2361
  /* get stbEntry*/
UNCOV
2362
  if (tdbTbGet(pMeta->pUidIdx, &ctbEntry.ctbEntry.suid, sizeof(tb_uid_t), &pVal, &nVal) != 0) {
×
UNCOV
2363
    metaError("meta/table: failed to get uid index, uid:%" PRId64, ctbEntry.ctbEntry.suid);
×
2364
  }
UNCOV
2365
  if (!pVal) {
×
UNCOV
2366
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
2367
    goto _err;
×
2368
  }
2369

UNCOV
2370
  if (tdbTbGet(pMeta->pTbDb, &((STbDbKey){.uid = ctbEntry.ctbEntry.suid, .version = ((SUidIdxVal *)pVal)[0].version}),
×
2371
               sizeof(STbDbKey), (void **)&stbEntry.pBuf, &nVal) != 0) {
UNCOV
2372
    metaError("meta/table: failed to get tb db, uid:%" PRId64, ctbEntry.ctbEntry.suid);
×
2373
  }
UNCOV
2374
  tdbFree(pVal);
×
UNCOV
2375
  tDecoderInit(&dc2, stbEntry.pBuf, nVal);
×
UNCOV
2376
  ret = metaDecodeEntry(&dc2, &stbEntry);
×
UNCOV
2377
  if (ret < 0) {
×
2378
    terrno = ret;
×
2379
    goto _err;
×
2380
  }
2381

UNCOV
2382
  SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
×
UNCOV
2383
  SSchema        *pColumn = NULL;
×
UNCOV
2384
  int32_t         iCol = 0;
×
2385

2386
  for (;;) {
2387
    pColumn = NULL;
×
2388

UNCOV
2389
    if (iCol >= pTagSchema->nCols) break;
×
UNCOV
2390
    pColumn = &pTagSchema->pSchema[iCol];
×
2391

UNCOV
2392
    if (strcmp(pColumn->name, pAlterTbReq->tagName) == 0) break;
×
UNCOV
2393
    iCol++;
×
2394
  }
2395

2396
  if (pColumn == NULL) {
×
UNCOV
2397
    terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
×
UNCOV
2398
    goto _err;
×
2399
  }
2400

UNCOV
2401
  ctbEntry.version = version;
×
UNCOV
2402
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
×
UNCOV
2403
    ctbEntry.ctbEntry.pTags = taosMemoryMalloc(pAlterTbReq->nTagVal);
×
UNCOV
2404
    if (ctbEntry.ctbEntry.pTags == NULL) {
×
UNCOV
2405
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
2406
      goto _err;
×
2407
    }
UNCOV
2408
    memcpy((void *)ctbEntry.ctbEntry.pTags, pAlterTbReq->pTagVal, pAlterTbReq->nTagVal);
×
2409
  } else {
UNCOV
2410
    const STag *pOldTag = (const STag *)ctbEntry.ctbEntry.pTags;
×
UNCOV
2411
    STag       *pNewTag = NULL;
×
UNCOV
2412
    SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
×
UNCOV
2413
    if (!pTagArray) {
×
2414
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2415
      goto _err;
×
2416
    }
UNCOV
2417
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
×
UNCOV
2418
      SSchema *pCol = &pTagSchema->pSchema[i];
×
UNCOV
2419
      if (iCol == i) {
×
UNCOV
2420
        if (pAlterTbReq->isNull) {
×
UNCOV
2421
          continue;
×
2422
        }
2423
        STagVal val = {0};
×
2424
        val.type = pCol->type;
×
UNCOV
2425
        val.cid = pCol->colId;
×
UNCOV
2426
        if (IS_VAR_DATA_TYPE(pCol->type)) {
×
UNCOV
2427
          val.pData = pAlterTbReq->pTagVal;
×
UNCOV
2428
          val.nData = pAlterTbReq->nTagVal;
×
2429
        } else {
2430
          memcpy(&val.i64, pAlterTbReq->pTagVal, pAlterTbReq->nTagVal);
×
2431
        }
UNCOV
2432
        if (taosArrayPush(pTagArray, &val) == NULL) {
×
UNCOV
2433
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
2434
          taosArrayDestroy(pTagArray);
×
UNCOV
2435
          goto _err;
×
2436
        }
2437
      } else {
UNCOV
2438
        STagVal val = {.cid = pCol->colId};
×
UNCOV
2439
        if (tTagGet(pOldTag, &val)) {
×
UNCOV
2440
          if (taosArrayPush(pTagArray, &val) == NULL) {
×
2441
            terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
2442
            taosArrayDestroy(pTagArray);
×
UNCOV
2443
            goto _err;
×
2444
          }
2445
        }
2446
      }
2447
    }
UNCOV
2448
    if ((terrno = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag)) < 0) {
×
UNCOV
2449
      taosArrayDestroy(pTagArray);
×
2450
      goto _err;
×
2451
    }
UNCOV
2452
    ctbEntry.ctbEntry.pTags = (uint8_t *)pNewTag;
×
UNCOV
2453
    taosArrayDestroy(pTagArray);
×
2454
  }
2455

2456
  metaWLock(pMeta);
×
2457

2458
  // save to table.db
UNCOV
2459
  if (metaSaveToTbDb(pMeta, &ctbEntry) < 0) {
×
2460
    metaError("meta/table: failed to save to tb db:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2461
  }
2462

2463
  // save to uid.idx
2464
  if (metaUpdateUidIdx(pMeta, &ctbEntry) < 0) {
×
UNCOV
2465
    metaError("meta/table: failed to update uid idx:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2466
  }
2467

2468
  if (metaUpdateTagIdx(pMeta, &ctbEntry) < 0) {
×
UNCOV
2469
    metaError("meta/table: failed to update tag idx:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2470
  }
2471

UNCOV
2472
  SCtbIdxKey ctbIdxKey = {.suid = ctbEntry.ctbEntry.suid, .uid = uid};
×
UNCOV
2473
  if (tdbTbUpsert(pMeta->pCtbIdx, &ctbIdxKey, sizeof(ctbIdxKey), ctbEntry.ctbEntry.pTags,
×
UNCOV
2474
                  ((STag *)(ctbEntry.ctbEntry.pTags))->len, pMeta->txn) < 0) {
×
UNCOV
2475
    metaError("meta/table: failed to upsert ctb idx:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2476
  }
2477

UNCOV
2478
  if (metaUidCacheClear(pMeta, ctbEntry.ctbEntry.suid) < 0) {
×
UNCOV
2479
    metaError("meta/table: failed to clear uid cache:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2480
  }
2481

2482
  if (metaTbGroupCacheClear(pMeta, ctbEntry.ctbEntry.suid) < 0) {
×
2483
    metaError("meta/table: failed to clear group cache:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2484
  }
2485

2486
  if (metaUpdateChangeTime(pMeta, ctbEntry.uid, pAlterTbReq->ctimeMs) < 0) {
×
2487
    metaError("meta/table: failed to update change time:%s uid:%" PRId64, ctbEntry.name, ctbEntry.uid);
×
2488
  }
2489

UNCOV
2490
  metaULock(pMeta);
×
2491

UNCOV
2492
  tDecoderClear(&dc1);
×
UNCOV
2493
  tDecoderClear(&dc2);
×
UNCOV
2494
  taosMemoryFree((void *)ctbEntry.ctbEntry.pTags);
×
UNCOV
2495
  if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
×
UNCOV
2496
  if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
×
UNCOV
2497
  tdbTbcClose(pTbDbc);
×
UNCOV
2498
  tdbTbcClose(pUidIdxc);
×
UNCOV
2499
  return 0;
×
2500

UNCOV
2501
_err:
×
UNCOV
2502
  tDecoderClear(&dc1);
×
UNCOV
2503
  tDecoderClear(&dc2);
×
UNCOV
2504
  if (ctbEntry.pBuf) taosMemoryFree(ctbEntry.pBuf);
×
UNCOV
2505
  if (stbEntry.pBuf) tdbFree(stbEntry.pBuf);
×
2506
  tdbTbcClose(pTbDbc);
×
UNCOV
2507
  tdbTbcClose(pUidIdxc);
×
UNCOV
2508
  return -1;
×
2509
}
2510

UNCOV
2511
static int metaUpdateTableOptions(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
×
UNCOV
2512
  void       *pVal = NULL;
×
UNCOV
2513
  int         nVal = 0;
×
UNCOV
2514
  const void *pData = NULL;
×
UNCOV
2515
  int         nData = 0;
×
UNCOV
2516
  int         ret = 0;
×
2517
  tb_uid_t    uid;
2518
  int64_t     oversion;
UNCOV
2519
  SMetaEntry  entry = {0};
×
UNCOV
2520
  int         c = 0;
×
2521

2522
  // search name index
2523
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
×
UNCOV
2524
  if (ret < 0) {
×
UNCOV
2525
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2526
  }
2527

UNCOV
2528
  uid = *(tb_uid_t *)pVal;
×
UNCOV
2529
  tdbFree(pVal);
×
UNCOV
2530
  pVal = NULL;
×
2531

2532
  // search uid index
UNCOV
2533
  TBC *pUidIdxc = NULL;
×
2534

UNCOV
2535
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pUidIdx, &pUidIdxc, NULL));
×
2536
  if (tdbTbcMoveTo(pUidIdxc, &uid, sizeof(uid), &c) < 0) {
×
UNCOV
2537
    metaError("meta/table: failed to move to uid index, uid:%" PRId64, uid);
×
2538
  }
2539
  if (c != 0) {
×
2540
    tdbTbcClose(pUidIdxc);
×
2541
    metaError("meta/table: invalide c: %" PRId32 " update tb options failed.", c);
×
2542
    return TSDB_CODE_FAILED;
×
2543
  }
2544

UNCOV
2545
  if (tdbTbcGet(pUidIdxc, NULL, NULL, &pData, &nData) < 0) {
×
2546
    metaError("meta/table: failed to get uid index, uid:%" PRId64, uid);
×
2547
  }
UNCOV
2548
  oversion = ((SUidIdxVal *)pData)[0].version;
×
2549

2550
  // search table.db
UNCOV
2551
  TBC *pTbDbc = NULL;
×
2552

2553
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pTbDb, &pTbDbc, NULL));
×
2554
  if (tdbTbcMoveTo(pTbDbc, &((STbDbKey){.uid = uid, .version = oversion}), sizeof(STbDbKey), &c) < 0) {
×
UNCOV
2555
    metaError("meta/table: failed to move to tb db, uid:%" PRId64, uid);
×
2556
  }
UNCOV
2557
  if (c != 0) {
×
UNCOV
2558
    tdbTbcClose(pUidIdxc);
×
UNCOV
2559
    tdbTbcClose(pTbDbc);
×
2560
    metaError("meta/table: invalide c: %" PRId32 " update tb options failed.", c);
×
2561
    return TSDB_CODE_FAILED;
×
2562
  }
2563

2564
  if (tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData) < 0) {
×
UNCOV
2565
    metaError("meta/table: failed to get tb db, uid:%" PRId64, uid);
×
2566
  }
2567

2568
  // get table entry
UNCOV
2569
  SDecoder dc = {0};
×
UNCOV
2570
  if ((entry.pBuf = taosMemoryMalloc(nData)) == NULL) {
×
UNCOV
2571
    tdbTbcClose(pUidIdxc);
×
UNCOV
2572
    tdbTbcClose(pTbDbc);
×
UNCOV
2573
    return terrno;
×
2574
  }
UNCOV
2575
  memcpy(entry.pBuf, pData, nData);
×
UNCOV
2576
  tDecoderInit(&dc, entry.pBuf, nData);
×
UNCOV
2577
  ret = metaDecodeEntry(&dc, &entry);
×
UNCOV
2578
  if (ret != 0) {
×
UNCOV
2579
    tDecoderClear(&dc);
×
UNCOV
2580
    tdbTbcClose(pUidIdxc);
×
UNCOV
2581
    tdbTbcClose(pTbDbc);
×
UNCOV
2582
    metaError("meta/table: invalide ret: %" PRId32 " alt tb options failed.", ret);
×
UNCOV
2583
    return TSDB_CODE_FAILED;
×
2584
  }
2585

UNCOV
2586
  entry.version = version;
×
UNCOV
2587
  metaWLock(pMeta);
×
2588
  // build SMetaEntry
UNCOV
2589
  if (entry.type == TSDB_CHILD_TABLE) {
×
UNCOV
2590
    if (pAlterTbReq->updateTTL) {
×
UNCOV
2591
      metaDeleteTtl(pMeta, &entry);
×
UNCOV
2592
      entry.ctbEntry.ttlDays = pAlterTbReq->newTTL;
×
UNCOV
2593
      metaUpdateTtl(pMeta, &entry);
×
2594
    }
UNCOV
2595
    if (pAlterTbReq->newCommentLen >= 0) {
×
UNCOV
2596
      entry.ctbEntry.commentLen = pAlterTbReq->newCommentLen;
×
UNCOV
2597
      entry.ctbEntry.comment = pAlterTbReq->newComment;
×
2598
    }
2599
  } else {
UNCOV
2600
    if (pAlterTbReq->updateTTL) {
×
UNCOV
2601
      metaDeleteTtl(pMeta, &entry);
×
2602
      entry.ntbEntry.ttlDays = pAlterTbReq->newTTL;
×
UNCOV
2603
      metaUpdateTtl(pMeta, &entry);
×
2604
    }
UNCOV
2605
    if (pAlterTbReq->newCommentLen >= 0) {
×
UNCOV
2606
      entry.ntbEntry.commentLen = pAlterTbReq->newCommentLen;
×
UNCOV
2607
      entry.ntbEntry.comment = pAlterTbReq->newComment;
×
2608
    }
2609
  }
2610

2611
  // save to table db
UNCOV
2612
  if (metaSaveToTbDb(pMeta, &entry) < 0) {
×
UNCOV
2613
    metaError("meta/table: failed to save to tb db:%s uid:%" PRId64, entry.name, entry.uid);
×
2614
  }
2615

2616
  if (metaUpdateUidIdx(pMeta, &entry) < 0) {
×
2617
    metaError("meta/table: failed to update uid idx:%s uid:%" PRId64, entry.name, entry.uid);
×
2618
  }
2619

UNCOV
2620
  if (metaUpdateChangeTime(pMeta, entry.uid, pAlterTbReq->ctimeMs) < 0) {
×
UNCOV
2621
    metaError("meta/table: failed to update change time:%s uid:%" PRId64, entry.name, entry.uid);
×
2622
  }
2623

2624
  metaULock(pMeta);
×
2625

2626
  tdbTbcClose(pTbDbc);
×
2627
  tdbTbcClose(pUidIdxc);
×
UNCOV
2628
  tDecoderClear(&dc);
×
UNCOV
2629
  if (entry.pBuf) taosMemoryFree(entry.pBuf);
×
UNCOV
2630
  return 0;
×
2631
}
2632

2633
typedef struct SMetaPair {
2634
  void *key;
2635
  int   nkey;
2636
} SMetaPair;
2637

UNCOV
2638
static int metaDropTagIndex(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) {
×
UNCOV
2639
  SMetaEntry  stbEntry = {0};
×
2640
  void       *pVal = NULL;
×
2641
  int         nVal = 0;
×
2642
  int         ret;
2643
  int         c;
2644
  tb_uid_t    suid;
2645
  int64_t     oversion;
2646
  const void *pData = NULL;
×
2647
  int         nData = 0;
×
2648
  SDecoder    dc = {0};
×
2649

2650
  if (pAlterTbReq->tagName == NULL) {
×
2651
    return terrno = TSDB_CODE_INVALID_MSG;
×
2652
  }
2653

2654
  // search name index
2655
  ret = tdbTbGet(pMeta->pNameIdx, pAlterTbReq->tbName, strlen(pAlterTbReq->tbName) + 1, &pVal, &nVal);
×
2656
  if (ret < 0) {
×
2657
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2658
  }
UNCOV
2659
  suid = *(tb_uid_t *)pVal;
×
UNCOV
2660
  tdbFree(pVal);
×
2661
  pVal = NULL;
×
2662

2663
  if (tdbTbGet(pMeta->pUidIdx, &suid, sizeof(tb_uid_t), &pVal, &nVal) == -1) {
×
2664
    ret = -1;
×
UNCOV
2665
    goto _err;
×
2666
  }
2667

UNCOV
2668
  STbDbKey tbDbKey = {0};
×
2669
  tbDbKey.uid = suid;
×
2670
  tbDbKey.version = ((SUidIdxVal *)pVal)[0].version;
×
2671
  ret = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pVal, &nVal);
×
2672
  if (ret < 0) {
×
2673
    goto _err;
×
2674
  }
2675

2676
  tDecoderInit(&dc, pVal, nVal);
×
2677
  ret = metaDecodeEntry(&dc, &stbEntry);
×
2678
  if (ret < 0) {
×
UNCOV
2679
    tDecoderClear(&dc);
×
2680
    goto _err;
×
2681
  }
2682

2683
  // Get targe schema info
UNCOV
2684
  SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
×
UNCOV
2685
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
×
UNCOV
2686
    terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
×
UNCOV
2687
    goto _err;
×
2688
  }
2689
  SSchema *pCol = NULL;
×
2690
  int32_t  iCol = 0;
×
2691
  for (;;) {
2692
    pCol = NULL;
×
2693
    if (iCol >= pTagSchema->nCols) break;
×
UNCOV
2694
    pCol = &pTagSchema->pSchema[iCol];
×
2695
    if (strcmp(pCol->name, pAlterTbReq->tagName) == 0) break;
×
UNCOV
2696
    iCol++;
×
2697
  }
2698
  if (iCol == 0) {
×
2699
    // cannot drop 1th tag index
2700
    terrno = -1;
×
2701
    goto _err;
×
2702
  }
2703
  if (pCol == NULL) {
×
UNCOV
2704
    terrno = TSDB_CODE_VND_COL_NOT_EXISTS;
×
2705
    goto _err;
×
2706
  }
2707

2708
  if (IS_IDX_ON(pCol)) {
×
2709
    terrno = TSDB_CODE_VND_COL_ALREADY_EXISTS;
×
UNCOV
2710
    goto _err;
×
2711
  }
2712

2713
  SArray *tagIdxList = taosArrayInit(512, sizeof(SMetaPair));
×
2714
  if (tagIdxList == NULL) {
×
2715
    goto _err;
×
2716
  }
2717

2718
  TBC *pTagIdxc = NULL;
×
UNCOV
2719
  TAOS_CHECK_RETURN(tdbTbcOpen(pMeta->pTagIdx, &pTagIdxc, NULL));
×
2720
  int rc =
2721
      tdbTbcMoveTo(pTagIdxc, &(STagIdxKey){.suid = suid, .cid = INT32_MIN, .type = pCol->type}, sizeof(STagIdxKey), &c);
×
2722
  for (;;) {
×
2723
    void *pKey, *pVal;
2724
    int   nKey, nVal;
2725
    rc = tdbTbcNext(pTagIdxc, &pKey, &nKey, &pVal, &nVal);
×
2726
    STagIdxKey *pIdxKey = (STagIdxKey *)pKey;
×
2727
    if (pIdxKey->suid != suid || pIdxKey->cid != pCol->colId) {
×
2728
      tdbFree(pKey);
×
2729
      tdbFree(pVal);
×
2730
      continue;
×
2731
    }
2732

2733
    SMetaPair pair = {.key = pKey, nKey = nKey};
×
2734
    if (taosArrayPush(tagIdxList, &pair) == NULL) {
×
UNCOV
2735
      goto _err;
×
2736
    }
2737
  }
2738
  tdbTbcClose(pTagIdxc);
2739

2740
  metaWLock(pMeta);
2741
  for (int i = 0; i < taosArrayGetSize(tagIdxList); i++) {
2742
    SMetaPair *pair = taosArrayGet(tagIdxList, i);
2743
    ret = tdbTbDelete(pMeta->pTagIdx, pair->key, pair->nkey, pMeta->txn);
2744
    if (ret < 0) {
2745
      metaError("meta/table: failed to delete tag idx:%s uid:%" PRId64, stbEntry.name, stbEntry.uid);
2746
    }
2747
  }
2748
  metaULock(pMeta);
2749

2750
  taosArrayDestroy(tagIdxList);
2751

2752
  // set pCol->flags; INDEX_ON
2753
  return 0;
UNCOV
2754
_err:
×
UNCOV
2755
  return TSDB_CODE_FAILED;
×
2756
}
2757
static int32_t metaUpdateTableColCompress(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
×
2758
  // impl later
2759
  SMetaEntry  tbEntry = {0};
×
2760
  void       *pVal = NULL;
×
UNCOV
2761
  int         nVal = 0;
×
2762
  int         ret;
2763
  int         c;
2764
  tb_uid_t    suid;
2765
  int64_t     oversion;
2766
  const void *pData = NULL;
×
2767
  int         nData = 0;
×
UNCOV
2768
  SDecoder    dc = {0};
×
2769
  ret = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &pVal, &nVal);
×
2770
  if (ret < 0) {
×
UNCOV
2771
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2772
  }
UNCOV
2773
  suid = *(tb_uid_t *)pVal;
×
2774
  tdbFree(pVal);
×
2775
  pVal = NULL;
×
2776

UNCOV
2777
  if (tdbTbGet(pMeta->pUidIdx, &suid, sizeof(tb_uid_t), &pVal, &nVal) == -1) {
×
2778
    terrno = TSDB_CODE_INVALID_MSG;
×
2779
    ret = -1;
×
2780
    goto _err;
×
2781
  }
2782

2783
  STbDbKey tbDbKey = {0};
×
2784
  tbDbKey.uid = suid;
×
UNCOV
2785
  tbDbKey.version = ((SUidIdxVal *)pVal)[0].version;
×
UNCOV
2786
  if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pVal, &nVal) < 0) {
×
2787
    terrno = TSDB_CODE_INVALID_MSG;
×
2788
    tdbFree(pVal);
×
2789
    goto _err;
×
2790
  }
2791

2792
  tDecoderInit(&dc, pVal, nVal);
×
UNCOV
2793
  ret = metaDecodeEntry(&dc, &tbEntry);
×
UNCOV
2794
  if (ret < 0) {
×
2795
    terrno = TSDB_CODE_INVALID_MSG;
×
2796
    tdbFree(pVal);
×
2797
    tDecoderClear(&dc);
×
2798
    goto _err;
×
2799
  }
UNCOV
2800
  if (tbEntry.type != TSDB_NORMAL_TABLE && tbEntry.type != TSDB_SUPER_TABLE) {
×
UNCOV
2801
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
2802
    tdbFree(pVal);
×
2803
    tDecoderClear(&dc);
×
2804
    goto _err;
×
2805
  }
2806
  int8_t           updated = 0;
×
UNCOV
2807
  SColCmprWrapper *wp = &tbEntry.colCmpr;
×
2808
  for (int32_t i = 0; i < wp->nCols; i++) {
×
2809
    SColCmpr *p = &wp->pColCmpr[i];
×
UNCOV
2810
    if (p->id == pReq->colId) {
×
2811
      uint32_t dst = 0;
×
2812
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
×
2813
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
2814
      if (updated > 0) {
×
2815
        p->alg = dst;
×
2816
      }
2817
    }
2818
  }
2819
  if (updated == 0) {
×
2820
    tdbFree(pVal);
×
UNCOV
2821
    tDecoderClear(&dc);
×
2822
    terrno = TSDB_CODE_VND_COLUMN_COMPRESS_ALREADY_EXIST;
×
2823
    goto _err;
×
2824
  } else if (updated < 0) {
×
UNCOV
2825
    tdbFree(pVal);
×
UNCOV
2826
    tDecoderClear(&dc);
×
2827
    terrno = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
2828
    goto _err;
×
2829
  }
UNCOV
2830
  tbEntry.version = version;
×
2831

2832
  metaWLock(pMeta);
×
2833
  if (metaSaveToTbDb(pMeta, &tbEntry) < 0) {
×
2834
    metaError("meta/table: failed to save to tb db:%s uid:%" PRId64, tbEntry.name, tbEntry.uid);
×
2835
  }
2836

2837
  if (metaUpdateUidIdx(pMeta, &tbEntry) < 0) {
×
2838
    metaError("meta/table: failed to update uid idx:%s uid:%" PRId64, tbEntry.name, tbEntry.uid);
×
2839
  }
2840

2841
  if (metaUpdateChangeTime(pMeta, suid, pReq->ctimeMs) < 0) {
×
UNCOV
2842
    metaError("meta/table: failed to update change time:%s uid:%" PRId64, tbEntry.name, tbEntry.uid);
×
2843
  }
2844

2845
  metaULock(pMeta);
×
2846

2847
  tdbFree(pVal);
×
2848
  tDecoderClear(&dc);
×
2849

UNCOV
2850
  return 0;
×
UNCOV
2851
_err:
×
2852
  return TSDB_CODE_FAILED;
×
2853
}
2854

2855
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
902✔
2856
  pMeta->changed = true;
902✔
2857
  switch (pReq->action) {
902!
2858
    case TSDB_ALTER_TABLE_ADD_COLUMN:
65✔
2859
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
2860
      return metaAddTableColumn(pMeta, version, pReq, pMetaRsp);
65✔
2861
    case TSDB_ALTER_TABLE_DROP_COLUMN:
65✔
2862
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
65✔
2863
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
38✔
2864
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
38✔
2865
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
29✔
2866
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
29✔
2867
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
642✔
2868
      return metaUpdateTableTagValue(pMeta, version, pReq);
642✔
2869
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
5✔
2870
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
5✔
2871
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
53✔
2872
      return metaUpdateTableOptions2(pMeta, version, pReq);
53✔
2873
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
5✔
2874
      return metaUpdateTableColCompress2(pMeta, version, pReq);
5✔
UNCOV
2875
    default:
×
UNCOV
2876
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
2877
      break;
2878
  }
2879
}
2880

UNCOV
2881
static int metaSaveToTbDb(SMeta *pMeta, const SMetaEntry *pME) {
×
2882
  STbDbKey tbDbKey;
UNCOV
2883
  void    *pKey = NULL;
×
UNCOV
2884
  void    *pVal = NULL;
×
UNCOV
2885
  int      kLen = 0;
×
UNCOV
2886
  int      vLen = 0;
×
UNCOV
2887
  SEncoder coder = {0};
×
2888

2889
  // set key and value
2890
  tbDbKey.version = pME->version;
×
UNCOV
2891
  tbDbKey.uid = pME->uid;
×
2892

UNCOV
2893
  metaDebug("vgId:%d, start to save table version:%" PRId64 " uid:%" PRId64, TD_VID(pMeta->pVnode), pME->version,
×
2894
            pME->uid);
2895

UNCOV
2896
  pKey = &tbDbKey;
×
2897
  kLen = sizeof(tbDbKey);
×
2898

2899
  int32_t ret = 0;
×
UNCOV
2900
  tEncodeSize(metaEncodeEntry, pME, vLen, ret);
×
UNCOV
2901
  if (ret < 0) {
×
UNCOV
2902
    goto _err;
×
2903
  }
2904

UNCOV
2905
  pVal = taosMemoryMalloc(vLen);
×
2906
  if (pVal == NULL) {
×
2907
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2908
    goto _err;
×
2909
  }
2910

UNCOV
2911
  tEncoderInit(&coder, pVal, vLen);
×
2912

UNCOV
2913
  if (metaEncodeEntry(&coder, pME) < 0) {
×
2914
    goto _err;
×
2915
  }
2916

2917
  tEncoderClear(&coder);
×
2918

2919
  // write to table.db
2920
  if (tdbTbInsert(pMeta->pTbDb, pKey, kLen, pVal, vLen, pMeta->txn) < 0) {
×
2921
    goto _err;
×
2922
  }
2923

UNCOV
2924
  taosMemoryFree(pVal);
×
UNCOV
2925
  return 0;
×
2926

UNCOV
2927
_err:
×
UNCOV
2928
  metaError("vgId:%d, failed to save table version:%" PRId64 "uid:%" PRId64 " %s", TD_VID(pMeta->pVnode), pME->version,
×
2929
            pME->uid, tstrerror(terrno));
2930

UNCOV
2931
  taosMemoryFree(pVal);
×
UNCOV
2932
  return TSDB_CODE_FAILED;
×
2933
}
2934

UNCOV
2935
static int metaUpdateUidIdx(SMeta *pMeta, const SMetaEntry *pME) {
×
2936
  // upsert cache
2937
  SMetaInfo info;
UNCOV
2938
  metaGetEntryInfo(pME, &info);
×
2939
  int32_t ret = metaCacheUpsert(pMeta, &info);
×
2940
  if (ret < 0) {
×
2941
    metaError("vgId:%d, failed to upsert cache, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pME->uid, tstrerror(ret));
×
2942
  }
2943

2944
  SUidIdxVal uidIdxVal = {.suid = info.suid, .version = info.version, .skmVer = info.skmVer};
×
2945

2946
  return tdbTbUpsert(pMeta->pUidIdx, &pME->uid, sizeof(tb_uid_t), &uidIdxVal, sizeof(uidIdxVal), pMeta->txn);
×
2947
}
2948

UNCOV
2949
static int metaUpdateSuidIdx(SMeta *pMeta, const SMetaEntry *pME) {
×
UNCOV
2950
  return tdbTbUpsert(pMeta->pSuidIdx, &pME->uid, sizeof(tb_uid_t), NULL, 0, pMeta->txn);
×
2951
}
2952

2953
static int metaUpdateNameIdx(SMeta *pMeta, const SMetaEntry *pME) {
×
UNCOV
2954
  return tdbTbUpsert(pMeta->pNameIdx, pME->name, strlen(pME->name) + 1, &pME->uid, sizeof(tb_uid_t), pMeta->txn);
×
2955
}
2956

2957
static void metaUpdateTtl(SMeta *pMeta, const SMetaEntry *pME) {
×
UNCOV
2958
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
×
2959

UNCOV
2960
  STtlUpdTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
×
2961
  if (pME->type == TSDB_CHILD_TABLE) {
×
UNCOV
2962
    ctx.ttlDays = pME->ctbEntry.ttlDays;
×
UNCOV
2963
    ctx.changeTimeMs = pME->ctbEntry.btime;
×
2964
  } else {
UNCOV
2965
    ctx.ttlDays = pME->ntbEntry.ttlDays;
×
UNCOV
2966
    ctx.changeTimeMs = pME->ntbEntry.btime;
×
2967
  }
2968

UNCOV
2969
  int32_t ret = ttlMgrInsertTtl(pMeta->pTtlMgr, &ctx);
×
2970
  if (ret < 0) {
×
2971
    metaError("vgId:%d, failed to insert ttl, uid: %" PRId64 " %s", TD_VID(pMeta->pVnode), pME->uid, tstrerror(ret));
×
2972
  }
2973

UNCOV
2974
  return;
×
2975
}
2976

UNCOV
2977
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
×
UNCOV
2978
  if (!tsTtlChangeOnWrite) return 0;
×
2979

UNCOV
2980
  if (changeTimeMs <= 0) {
×
UNCOV
2981
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
×
UNCOV
2982
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
×
2983
  }
2984

UNCOV
2985
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
×
2986

UNCOV
2987
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
×
2988
}
2989

2990
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
13,313,105✔
2991
  if (!tsTtlChangeOnWrite) return 0;
13,313,105!
2992

2993
  metaWLock(pMeta);
×
UNCOV
2994
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
×
UNCOV
2995
  metaULock(pMeta);
×
2996
  return ret;
×
2997
}
2998

UNCOV
2999
static int metaUpdateCtbIdx(SMeta *pMeta, const SMetaEntry *pME) {
×
UNCOV
3000
  SCtbIdxKey ctbIdxKey = {.suid = pME->ctbEntry.suid, .uid = pME->uid};
×
3001

UNCOV
3002
  return tdbTbUpsert(pMeta->pCtbIdx, &ctbIdxKey, sizeof(ctbIdxKey), pME->ctbEntry.pTags,
×
UNCOV
3003
                     ((STag *)(pME->ctbEntry.pTags))->len, pMeta->txn);
×
3004
}
3005

3006
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
219,334✔
3007
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
3008
  if (IS_VAR_DATA_TYPE(type)) {
219,334!
3009
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
8,765✔
3010
  } else {
3011
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
210,569✔
3012
  }
3013

3014
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
219,334!
3015
  if (*ppTagIdxKey == NULL) {
219,378!
UNCOV
3016
    return terrno;
×
3017
  }
3018

3019
  (*ppTagIdxKey)->suid = suid;
219,378✔
3020
  (*ppTagIdxKey)->cid = cid;
219,378✔
3021
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
219,378✔
3022
  (*ppTagIdxKey)->type = type;
219,378✔
3023

3024
  // refactor
3025
  if (IS_VAR_DATA_TYPE(type)) {
219,378!
3026
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
8,813✔
3027
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
8,813✔
3028
    *(tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData) = uid;
8,813✔
3029
  } else {
3030
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
210,565✔
3031
    *(tb_uid_t *)((*ppTagIdxKey)->data + nTagData) = uid;
210,565✔
3032
  }
3033

3034
  return 0;
219,378✔
3035
}
3036

3037
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
218,987✔
3038
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
218,987!
3039
}
218,982✔
3040

UNCOV
3041
static int metaUpdateTagIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry) {
×
3042
  void          *pData = NULL;
×
UNCOV
3043
  int            nData = 0;
×
UNCOV
3044
  STbDbKey       tbDbKey = {0};
×
UNCOV
3045
  SMetaEntry     stbEntry = {0};
×
UNCOV
3046
  STagIdxKey    *pTagIdxKey = NULL;
×
3047
  int32_t        nTagIdxKey;
3048
  const SSchema *pTagColumn;
3049
  const void    *pTagData = NULL;
×
UNCOV
3050
  int32_t        nTagData = 0;
×
UNCOV
3051
  SDecoder       dc = {0};
×
3052
  int32_t        ret = 0;
×
3053
  // get super table
UNCOV
3054
  if (tdbTbGet(pMeta->pUidIdx, &pCtbEntry->ctbEntry.suid, sizeof(tb_uid_t), &pData, &nData) != 0) {
×
UNCOV
3055
    metaError("vgId:%d, failed to get stable suid for update. version:%" PRId64, TD_VID(pMeta->pVnode),
×
3056
              pCtbEntry->version);
UNCOV
3057
    ret = TSDB_CODE_TDB_INVALID_TABLE_ID;
×
UNCOV
3058
    goto end;
×
3059
  }
UNCOV
3060
  tbDbKey.uid = pCtbEntry->ctbEntry.suid;
×
UNCOV
3061
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
×
3062
  ret = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData);
×
UNCOV
3063
  if (ret < 0) {
×
UNCOV
3064
    metaError("vgId:%d, failed to get stable for update. version:%" PRId64, TD_VID(pMeta->pVnode), pCtbEntry->version);
×
UNCOV
3065
    goto end;
×
3066
  }
3067

UNCOV
3068
  tDecoderInit(&dc, pData, nData);
×
UNCOV
3069
  ret = metaDecodeEntry(&dc, &stbEntry);
×
UNCOV
3070
  if (ret < 0) {
×
UNCOV
3071
    goto end;
×
3072
  }
3073

UNCOV
3074
  if (stbEntry.stbEntry.schemaTag.pSchema == NULL) {
×
UNCOV
3075
    ret = TSDB_CODE_INVALID_PARA;
×
UNCOV
3076
    goto end;
×
3077
  }
3078

UNCOV
3079
  SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
×
UNCOV
3080
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
×
UNCOV
3081
    pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
×
UNCOV
3082
    STagVal tagVal = {.cid = pTagColumn->colId};
×
3083

UNCOV
3084
    pTagData = pCtbEntry->ctbEntry.pTags;
×
UNCOV
3085
    nTagData = ((const STag *)pCtbEntry->ctbEntry.pTags)->len;
×
UNCOV
3086
    ret = metaSaveJsonVarToIdx(pMeta, pCtbEntry, pTagColumn);
×
UNCOV
3087
    goto end;
×
3088
  } else {
UNCOV
3089
    for (int i = 0; i < pTagSchema->nCols; i++) {
×
UNCOV
3090
      pTagData = NULL;
×
UNCOV
3091
      nTagData = 0;
×
3092
      pTagColumn = &pTagSchema->pSchema[i];
×
UNCOV
3093
      if (!IS_IDX_ON(pTagColumn)) continue;
×
3094

UNCOV
3095
      STagVal tagVal = {.cid = pTagColumn->colId};
×
UNCOV
3096
      if (tTagGet((const STag *)pCtbEntry->ctbEntry.pTags, &tagVal)) {
×
UNCOV
3097
        if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
×
UNCOV
3098
          pTagData = tagVal.pData;
×
UNCOV
3099
          nTagData = (int32_t)tagVal.nData;
×
3100
        } else {
3101
          pTagData = &(tagVal.i64);
×
3102
          nTagData = tDataTypes[pTagColumn->type].bytes;
×
3103
        }
3104
      } else {
UNCOV
3105
        if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
×
3106
          nTagData = tDataTypes[pTagColumn->type].bytes;
×
3107
        }
3108
      }
UNCOV
3109
      if (metaCreateTagIdxKey(pCtbEntry->ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type,
×
UNCOV
3110
                              pCtbEntry->uid, &pTagIdxKey, &nTagIdxKey) < 0) {
×
UNCOV
3111
        ret = -1;
×
UNCOV
3112
        goto end;
×
3113
      }
3114
      if (tdbTbUpsert(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, NULL, 0, pMeta->txn) < 0) {
×
3115
        metaError("vgId:%d, failed to update tag index. version:%" PRId64, TD_VID(pMeta->pVnode), pCtbEntry->version);
×
3116
      }
3117
      metaDestroyTagIdxKey(pTagIdxKey);
×
UNCOV
3118
      pTagIdxKey = NULL;
×
3119
    }
3120
  }
UNCOV
3121
end:
×
UNCOV
3122
  tDecoderClear(&dc);
×
UNCOV
3123
  tdbFree(pData);
×
UNCOV
3124
  return ret;
×
3125
}
3126

UNCOV
3127
static int metaSaveToSkmDb(SMeta *pMeta, const SMetaEntry *pME) {
×
UNCOV
3128
  SEncoder              coder = {0};
×
UNCOV
3129
  void                 *pVal = NULL;
×
UNCOV
3130
  int                   vLen = 0;
×
UNCOV
3131
  int                   rcode = 0;
×
UNCOV
3132
  SSkmDbKey             skmDbKey = {0};
×
3133
  const SSchemaWrapper *pSW;
3134

UNCOV
3135
  if (pME->type == TSDB_SUPER_TABLE) {
×
UNCOV
3136
    pSW = &pME->stbEntry.schemaRow;
×
3137
  } else if (pME->type == TSDB_NORMAL_TABLE) {
×
UNCOV
3138
    pSW = &pME->ntbEntry.schemaRow;
×
3139
  } else {
UNCOV
3140
    metaError("meta/table: invalide table type: %" PRId8 " save skm db failed.", pME->type);
×
UNCOV
3141
    return TSDB_CODE_FAILED;
×
3142
  }
3143

UNCOV
3144
  skmDbKey.uid = pME->uid;
×
UNCOV
3145
  skmDbKey.sver = pSW->version;
×
3146

3147
  // if receive tmq meta message is: create stable1 then delete stable1 then create stable1 with multi vgroups
UNCOV
3148
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(skmDbKey), NULL, NULL) == 0) {
×
UNCOV
3149
    return rcode;
×
3150
  }
3151

3152
  // encode schema
UNCOV
3153
  int32_t ret = 0;
×
UNCOV
3154
  tEncodeSize(tEncodeSSchemaWrapper, pSW, vLen, ret);
×
UNCOV
3155
  if (ret < 0) return -1;
×
UNCOV
3156
  pVal = taosMemoryMalloc(vLen);
×
UNCOV
3157
  if (pVal == NULL) {
×
UNCOV
3158
    rcode = -1;
×
UNCOV
3159
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
3160
    goto _exit;
×
3161
  }
3162

UNCOV
3163
  tEncoderInit(&coder, pVal, vLen);
×
UNCOV
3164
  ret = tEncodeSSchemaWrapper(&coder, pSW);
×
UNCOV
3165
  if (ret < 0) {
×
UNCOV
3166
    rcode = -1;
×
UNCOV
3167
    goto _exit;
×
3168
  }
3169

UNCOV
3170
  if (tdbTbInsert(pMeta->pSkmDb, &skmDbKey, sizeof(skmDbKey), pVal, vLen, pMeta->txn) < 0) {
×
UNCOV
3171
    rcode = -1;
×
UNCOV
3172
    goto _exit;
×
3173
  }
3174

UNCOV
3175
  metaDebug("vgId:%d, set schema:(%" PRId64 ") sver:%d since %s", TD_VID(pMeta->pVnode), pME->uid, pSW->version,
×
3176
            tstrerror(terrno));
3177

3178
_exit:
×
3179
  taosMemoryFree(pVal);
×
UNCOV
3180
  tEncoderClear(&coder);
×
UNCOV
3181
  return rcode;
×
3182
}
3183

UNCOV
3184
int metaHandleEntry(SMeta *pMeta, const SMetaEntry *pME) {
×
3185
  int32_t code = 0;
×
3186
  int32_t line = 0;
×
UNCOV
3187
  metaWLock(pMeta);
×
3188

3189
  // save to table.db
UNCOV
3190
  code = metaSaveToTbDb(pMeta, pME);
×
UNCOV
3191
  VND_CHECK_CODE(code, line, _err);
×
3192

3193
  // update uid.idx
UNCOV
3194
  code = metaUpdateUidIdx(pMeta, pME);
×
UNCOV
3195
  VND_CHECK_CODE(code, line, _err);
×
3196

3197
  // update name.idx
UNCOV
3198
  code = metaUpdateNameIdx(pMeta, pME);
×
UNCOV
3199
  VND_CHECK_CODE(code, line, _err);
×
3200

UNCOV
3201
  if (pME->type == TSDB_CHILD_TABLE) {
×
3202
    // update ctb.idx
UNCOV
3203
    code = metaUpdateCtbIdx(pMeta, pME);
×
UNCOV
3204
    VND_CHECK_CODE(code, line, _err);
×
3205

3206
    // update tag.idx
UNCOV
3207
    code = metaUpdateTagIdx(pMeta, pME);
×
UNCOV
3208
    VND_CHECK_CODE(code, line, _err);
×
3209
  } else {
3210
    // update schema.db
UNCOV
3211
    code = metaSaveToSkmDb(pMeta, pME);
×
UNCOV
3212
    VND_CHECK_CODE(code, line, _err);
×
3213

UNCOV
3214
    if (pME->type == TSDB_SUPER_TABLE) {
×
UNCOV
3215
      code = metaUpdateSuidIdx(pMeta, pME);
×
UNCOV
3216
      VND_CHECK_CODE(code, line, _err);
×
3217
    }
3218
  }
3219

UNCOV
3220
  code = metaUpdateBtimeIdx(pMeta, pME);
×
UNCOV
3221
  VND_CHECK_CODE(code, line, _err);
×
3222

UNCOV
3223
  if (pME->type == TSDB_NORMAL_TABLE) {
×
UNCOV
3224
    code = metaUpdateNcolIdx(pMeta, pME);
×
UNCOV
3225
    VND_CHECK_CODE(code, line, _err);
×
3226
  }
3227

UNCOV
3228
  if (pME->type != TSDB_SUPER_TABLE) {
×
UNCOV
3229
    metaUpdateTtl(pMeta, pME);
×
3230
  }
3231

3232
  if (pME->type == TSDB_SUPER_TABLE || pME->type == TSDB_NORMAL_TABLE) {
×
3233
  }
3234

UNCOV
3235
  metaULock(pMeta);
×
3236
  metaDebug("vgId:%d, handle meta entry, ver:%" PRId64 ", uid:%" PRId64 ", name:%s", TD_VID(pMeta->pVnode),
×
3237
            pME->version, pME->uid, pME->name);
UNCOV
3238
  return 0;
×
3239

UNCOV
3240
_err:
×
UNCOV
3241
  metaULock(pMeta);
×
UNCOV
3242
  metaError("vgId:%d, failed to handle meta entry since %s at line:%d, ver:%" PRId64 ", uid:%" PRId64 ", name:%s",
×
3243
            TD_VID(pMeta->pVnode), terrstr(), line, pME->version, pME->uid, pME->name);
UNCOV
3244
  return TSDB_CODE_FAILED;
×
3245
}
3246

3247
static void colCompressDebug(SHashObj *pColCmprObj) {
369,441✔
3248
  void *p = taosHashIterate(pColCmprObj, NULL);
369,441✔
3249
  while (p) {
2,824,536✔
3250
    uint32_t cmprAlg = *(uint32_t *)p;
2,455,166✔
3251
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
2,455,166✔
3252
    p = taosHashIterate(pColCmprObj, p);
2,455,143✔
3253

3254
    uint8_t l1, l2, lvl;
3255
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
2,455,743✔
3256

3257
    const char *l1str = columnEncodeStr(l1);
2,455,624✔
3258
    const char *l2str = columnCompressStr(l2);
2,455,339✔
3259
    const char *lvlstr = columnLevelStr(lvl);
2,455,190✔
3260
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
2,455,092✔
3261
  }
3262
  return;
369,370✔
3263
}
3264
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
369,412✔
3265
  int rc = 0;
369,412✔
3266

3267
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
369,412✔
3268
  if (pColCmprObj == NULL) {
369,421!
UNCOV
3269
    pColCmprObj = NULL;
×
UNCOV
3270
    return TSDB_CODE_OUT_OF_MEMORY;
×
3271
  }
3272

3273
  void      *pData = NULL;
369,421✔
3274
  int        nData = 0;
369,421✔
3275
  SMetaEntry e = {0};
369,421✔
3276
  SDecoder   dc = {0};
369,421✔
3277

3278
  *ppColCmprObj = NULL;
369,421✔
3279

3280
  metaRLock(pMeta);
369,421✔
3281
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
369,452✔
3282
  if (rc < 0) {
369,437!
UNCOV
3283
    taosHashClear(pColCmprObj);
×
UNCOV
3284
    metaULock(pMeta);
×
UNCOV
3285
    return TSDB_CODE_FAILED;
×
3286
  }
3287
  int64_t version = ((SUidIdxVal *)pData)[0].version;
369,437✔
3288
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
369,437✔
3289
  if (rc < 0) {
369,434!
UNCOV
3290
    metaULock(pMeta);
×
UNCOV
3291
    taosHashClear(pColCmprObj);
×
3292
    metaError("failed to get table entry");
×
3293
    return rc;
×
3294
  }
3295

3296
  tDecoderInit(&dc, pData, nData);
369,434✔
3297
  rc = metaDecodeEntry(&dc, &e);
369,419✔
3298
  if (rc < 0) {
369,361!
UNCOV
3299
    tDecoderClear(&dc);
×
UNCOV
3300
    tdbFree(pData);
×
UNCOV
3301
    metaULock(pMeta);
×
UNCOV
3302
    taosHashClear(pColCmprObj);
×
UNCOV
3303
    return rc;
×
3304
  }
3305
  if (useCompress(e.type)) {
369,361!
3306
    SColCmprWrapper *p = &e.colCmpr;
369,365✔
3307
    for (int32_t i = 0; i < p->nCols; i++) {
2,825,385✔
3308
      SColCmpr *pCmpr = &p->pColCmpr[i];
2,455,741✔
3309
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
2,455,741✔
3310
      if (rc < 0) {
2,456,057✔
3311
        tDecoderClear(&dc);
37✔
UNCOV
3312
        tdbFree(pData);
×
UNCOV
3313
        metaULock(pMeta);
×
UNCOV
3314
        taosHashClear(pColCmprObj);
×
UNCOV
3315
        return rc;
×
3316
      }
3317
    }
3318
  } else {
UNCOV
3319
    tDecoderClear(&dc);
×
UNCOV
3320
    tdbFree(pData);
×
UNCOV
3321
    metaULock(pMeta);
×
UNCOV
3322
    taosHashClear(pColCmprObj);
×
UNCOV
3323
    return 0;
×
3324
  }
3325
  tDecoderClear(&dc);
369,644✔
3326
  tdbFree(pData);
369,421✔
3327
  metaULock(pMeta);
369,437✔
3328

3329
  *ppColCmprObj = pColCmprObj;
369,446✔
3330
  colCompressDebug(pColCmprObj);
369,446✔
3331

3332
  return 0;
369,388✔
3333
}
3334
// refactor later
3335
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
7,739✔
3336
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
7,731✔
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