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

taosdata / TDengine / #4888

17 Dec 2025 07:43AM UTC coverage: 65.281% (-0.008%) from 65.289%
#4888

push

travis-ci

web-flow
test: update ci env (#33959)

178960 of 274136 relevant lines covered (65.28%)

101703773.12 hits per line

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

80.92
/source/dnode/vnode/src/meta/metaEntry.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
#include "osMemPool.h"
18
#include "osMemory.h"
19
#include "tencode.h"
20
#include "tmsg.h"
21

22
static bool schemasHasTypeMod(const SSchema *pSchema, int32_t nCols) {
678,505,323✔
23
  for (int32_t i = 0; i < nCols; i++) {
2,147,483,647✔
24
    if (HAS_TYPE_MOD(pSchema + i)) {
2,147,483,647✔
25
      return true;
36,556,615✔
26
    }
27
  }
28
  return false;
640,036,660✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
213,380,066✔
32
  if (pME->pExtSchemas) {
213,380,066✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
14,277,744✔
34
    bool                  hasTypeMods = false;
14,277,744✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
14,277,744✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
14,153,093✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
132,748✔
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
132,748✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
14,285,262✔
43

44
    for (int32_t i = 0; i < pSchWrapper->nCols && hasTypeMods; ++i) {
2,147,483,647✔
45
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->pExtSchemas[i].typeMod));
2,147,483,647✔
46
    }
47
  }
48
  return 0;
213,414,911✔
49
}
50

51
int meteEncodeColRefEntry(SEncoder *pCoder, const SMetaEntry *pME) {
1,254,670✔
52
  const SColRefWrapper *pw = &pME->colRef;
1,254,670✔
53
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
2,509,340✔
54
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
2,509,340✔
55
  uTrace("encode cols:%d", pw->nCols);
1,254,670✔
56

57
  for (int32_t i = 0; i < pw->nCols; i++) {
332,741,844✔
58
    SColRef *p = &pw->pColRef[i];
331,487,174✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
662,974,348✔
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
662,974,348✔
61
    if (p->hasRef) {
331,487,174✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
396,955,780✔
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
396,955,780✔
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
396,955,780✔
65
    }
66
  }
67
  return 0;
1,254,670✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
649,597,495✔
71
  bool hasExtSchema = false;
649,597,495✔
72
  SSchemaWrapper* pSchWrapper = NULL;
649,597,495✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
649,597,495✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
644,801,797✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
4,906,722✔
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
4,906,286✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
649,768,292✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
649,682,462✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
50,784,877✔
84
    if (pME->pExtSchemas == NULL) {
25,391,297✔
85
      return terrno;
×
86
    }
87

88
    for (int32_t i = 0; i < pSchWrapper->nCols && hasExtSchema; i++) {
2,147,483,647✔
89
      TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pME->pExtSchemas[i].typeMod));
2,147,483,647✔
90
    }
91
  } else {
92
    pME->pExtSchemas = NULL;
624,290,572✔
93
  }
94

95
  return 0;
649,653,456✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
14,633,459✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
14,633,459✔
100
  bool                  hasTypeMods = false;
14,633,459✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
14,633,459✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
12,881,857✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
1,788,510✔
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
1,788,910✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
14,670,652✔
109

110
  if (hasTypeMods) {
14,590,124✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
40,473✔
112
    if (ret != NULL) {
40,473✔
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
40,473✔
114
    }
115
    return ret;
40,473✔
116
  }
117
  return NULL;
14,549,651✔
118
}
119

120
int32_t metaGetRsmaSchema(const SMetaEntry *pME, SSchemaRsma **rsmaSchema) {
27,828✔
121
  if (!rsmaSchema) return 0;
27,828✔
122
  if ((pME->type != TSDB_SUPER_TABLE) || !TABLE_IS_ROLLUP(pME->flags)) {  // only support super table
27,828✔
123
    *rsmaSchema = NULL;
×
124
    return 0;
×
125
  }
126

127
  const SRSmaParam *pParam = &pME->stbEntry.rsmaParam;
27,828✔
128
  const SSchema    *pSchema = pME->stbEntry.schemaRow.pSchema;
27,828✔
129
  int32_t           nCols = pME->stbEntry.schemaRow.nCols;
27,828✔
130

131
  *rsmaSchema = (SSchemaRsma *)taosMemoryMalloc(sizeof(SSchemaRsma));
27,828✔
132
  if (*rsmaSchema == NULL) {
27,828✔
133
    return terrno;
×
134
  }
135

136
  (*rsmaSchema)->funcIds = taosMemoryMalloc(sizeof(func_id_t) * nCols);
27,828✔
137
  if ((*rsmaSchema)->funcIds == NULL) {
27,828✔
138
    taosMemoryFree(*rsmaSchema);
×
139
    *rsmaSchema = NULL;
×
140
    return terrno;
×
141
  }
142

143
  (void)snprintf((*rsmaSchema)->tbName, TSDB_TABLE_NAME_LEN, "%s", pME->name);
27,828✔
144
  (*rsmaSchema)->tbUid = pME->uid;
27,828✔
145
  (*rsmaSchema)->tbType = pME->type;
27,828✔
146
  (*rsmaSchema)->interval[0] = pParam->interval[0];
27,828✔
147
  (*rsmaSchema)->interval[1] = pParam->interval[1];
27,828✔
148
  (*rsmaSchema)->nFuncs = nCols;
27,828✔
149

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
27,828✔
151
  int32_t    i = 0, j = 0;
27,828✔
152
  for (i = 0; i < nCols; ++i) {
214,121✔
153
    while (j < pParam->nFuncs) {
338,574✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
325,433✔
155
        pFuncIds[i] = pParam->funcIds[j];
152,281✔
156
        break;
152,281✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
173,152✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
34,012✔
160
        break;
34,012✔
161
      }
162
      ++j;
139,140✔
163
    }
164
    if (j >= pParam->nFuncs) {
199,434✔
165
      for (; i < nCols; ++i) {
26,282✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
13,141✔
167
      }
168
      break;
13,141✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
27,828✔
172

173
  return 0;
27,828✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
7,233,797✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
7,233,797✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
14,467,594✔
179
  if (pWrapper->nCols == 0) {
7,233,797✔
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
14,466,511✔
184
  uDebug("decode cols:%d", pWrapper->nCols);
7,233,797✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
14,467,594✔
186
  if (pWrapper->pColRef == NULL) {
7,233,797✔
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
290,487,860✔
191
    SColRef *p = &pWrapper->pColRef[i];
283,247,291✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
566,511,401✔
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
566,528,984✔
194
    if (p->hasRef) {
283,274,648✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
163,461,540✔
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
163,458,145✔
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
163,460,861✔
198
    }
199
  }
200
  return 0;
7,233,797✔
201
}
202

203
static FORCE_INLINE int32_t metatInitDefaultSColRefWrapper(SDecoder *pDecoder, SColRefWrapper *pRef,
204
                                                            SSchemaWrapper *pSchema) {
205
  pRef->nCols = pSchema->nCols;
×
206
  if ((pRef->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pRef->nCols * sizeof(SColRef))) == NULL) {
×
207
    return terrno;
×
208
  }
209

210
  for (int32_t i = 0; i < pRef->nCols; i++) {
×
211
    SColRef  *pColRef = &pRef->pColRef[i];
×
212
    SSchema  *pColSchema = &pSchema->pSchema[i];
×
213
    pColRef->id = pColSchema->colId;
×
214
    pColRef->hasRef = false;
×
215
  }
216
  return 0;
×
217
}
218

219
static int32_t metaCloneColRef(const SColRefWrapper*pSrc, SColRefWrapper *pDst) {
719,629✔
220
  if (pSrc->nCols > 0) {
719,629✔
221
    pDst->nCols = pSrc->nCols;
719,629✔
222
    pDst->version = pSrc->version;
719,629✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
719,629✔
224
    if (NULL == pDst->pColRef) {
719,629✔
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
719,629✔
228
  }
229
  return 0;
719,629✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
74,971,476✔
233
  int32_t code = 0;
74,971,476✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
149,944,235✔
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
149,938,345✔
236
  uTrace("encode cols:%d", pw->nCols);
74,965,586✔
237

238
  for (int32_t i = 0; i < pw->nCols; i++) {
2,147,483,647✔
239
    SColCmpr *p = &pw->pColCmpr[i];
2,147,483,647✔
240
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
2,147,483,647✔
241
    TAOS_CHECK_RETURN(tEncodeU32(pCoder, p->alg));
2,147,483,647✔
242
  }
243
  return code;
75,045,050✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
74,758,804✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
74,758,804✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
74,797,712✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
751,927,985✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
751,927,985✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
1,504,486,721✔
252
  if (pWrapper->nCols == 0) {
752,117,439✔
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
1,503,898,156✔
257
  uDebug("dencode cols:%d", pWrapper->nCols);
752,015,980✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
1,504,422,514✔
259
  if (pWrapper->pColCmpr == NULL) {
752,452,877✔
260
    return terrno;
×
261
  }
262

263
  for (int i = 0; i < pWrapper->nCols; i++) {
2,147,483,647✔
264
    SColCmpr *p = &pWrapper->pColCmpr[i];
2,147,483,647✔
265
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
2,147,483,647✔
266
    TAOS_CHECK_RETURN(tDecodeU32(pDecoder, &p->alg));
2,147,483,647✔
267
  }
268
  return 0;
752,612,430✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
242,352✔
273

274
  if (pDecoder == NULL) {
242,352✔
275
    pCmpr->pColCmpr = taosMemoryCalloc(1, pCmpr->nCols * sizeof(SColCmpr));
242,352✔
276
  } else {
277
    pCmpr->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pCmpr->nCols * sizeof(SColCmpr));
×
278
  }
279

280
  if (pCmpr->pColCmpr == NULL) {
242,352✔
281
    return terrno;
×
282
  }
283

284
  for (int32_t i = 0; i < pCmpr->nCols; i++) {
1,228,720✔
285
    SColCmpr *pColCmpr = &pCmpr->pColCmpr[i];
986,368✔
286
    SSchema  *pColSchema = &pSchema->pSchema[i];
986,368✔
287
    pColCmpr->id = pColSchema->colId;
986,368✔
288
    pColCmpr->alg = createDefaultColCmprByType(pColSchema->type);
986,368✔
289
  }
290
  return 0;
242,352✔
291
}
292

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
174,737,730✔
294
  if (pSrc->nCols > 0) {
174,737,730✔
295
    pDst->nCols = pSrc->nCols;
157,258,102✔
296
    pDst->version = pSrc->version;
157,253,426✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
157,241,797✔
298
    if (NULL == pDst->pColCmpr) {
157,233,116✔
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
157,231,067✔
302
  }
303
  return 0;
174,737,365✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
175,458,431✔
307
  if (pColRef) {
175,458,431✔
308
    taosMemoryFreeClear(pColRef->pColRef);
175,463,400✔
309
  }
310
}
175,461,496✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
175,452,703✔
313
  if (pCmpr) {
175,452,703✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
175,452,895✔
315
  }
316
}
175,457,861✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
217,426,985✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
217,426,985✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
434,888,429✔
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
434,885,729✔
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
434,894,711✔
323

324
  if (pME->type > 0) {
217,449,077✔
325
    if (pME->name == NULL) {
213,414,857✔
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
426,782,086✔
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
213,379,651✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
61,898,420✔
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
61,968,565✔
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
61,970,927✔
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
30,962,068✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
80,392✔
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
182,425,509✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
275,457,238✔
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
275,450,938✔
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
275,446,209✔
342
      if (pME->ctbEntry.commentLen > 0) {
137,723,249✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
15,452✔
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
275,460,816✔
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
137,733,587✔
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
44,702,682✔
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
89,405,364✔
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
89,405,364✔
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
89,405,364✔
351
      if (pME->ntbEntry.commentLen > 0) {
44,702,682✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
14,636✔
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
89,405,364✔
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
89,405,364✔
356
    } else if (pME->type == TSDB_TSMA_TABLE) {
×
357
      TAOS_CHECK_RETURN(tEncodeTSma(pCoder, pME->smaEntry.tsma));
×
358
    } else {
359
      metaError("meta/entry: invalide table type: %" PRId8 " encode failed.", pME->type);
×
360
      return TSDB_CODE_INVALID_PARA;
×
361
    }
362
    if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
213,404,688✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,302,779✔
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
212,111,607✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
30,990,586✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
181,190,080✔
368
        if (pME->colCmpr.nCols != 0) {
44,034,524✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
43,792,172✔
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
242,352✔
372
          SColCmprWrapper colCmprs = {0};
242,352✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
242,352✔
374
          if (code != 0) {
242,352✔
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
242,352✔
379
          taosMemoryFree(colCmprs.pColCmpr);
242,352✔
380
          TAOS_CHECK_RETURN(code);
242,352✔
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
213,445,411✔
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
217,425,567✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
61,946,514✔
388
  }
389

390
  tEndEncode(pCoder);
217,423,066✔
391
  return 0;
217,431,417✔
392
}
393

394
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
1,303,892,537✔
395
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
1,303,892,537✔
396
  TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->version));
2,147,483,647✔
397
  TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->type));
2,147,483,647✔
398
  TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->uid));
2,147,483,647✔
399

400
  if (headerOnly) {
1,304,503,951✔
401
    tEndDecode(pCoder);
106,082✔
402
    return 0;
106,082✔
403
  }
404

405
  if (pME->type > 0) {
1,304,397,869✔
406
    TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->name));
2,147,483,647✔
407

408
    if (pME->type == TSDB_SUPER_TABLE) {
1,304,284,056✔
409
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,289,888,219✔
410
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,289,944,000✔
411
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,289,757,813✔
412
      if (TABLE_IS_ROLLUP(pME->flags)) {
644,777,718✔
413
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
295,286✔
414
      }
415
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
658,856,640✔
416
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
1,101,101,836✔
417
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
1,101,194,554✔
418
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
1,101,049,043✔
419
      if (pME->ctbEntry.commentLen > 0) {
550,454,084✔
420
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
21,650✔
421
      }
422
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
1,100,972,101✔
423
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
550,598,847✔
424
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
108,604,775✔
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
217,478,961✔
426
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
217,630,722✔
427
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
217,596,826✔
428
      if (pME->ntbEntry.commentLen > 0) {
108,780,476✔
429
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
21,656✔
430
      }
431
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
217,367,915✔
432
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
217,470,858✔
433
    } else if (pME->type == TSDB_TSMA_TABLE) {
×
434
      pME->smaEntry.tsma = tDecoderMalloc(pCoder, sizeof(STSma));
×
435
      if (!pME->smaEntry.tsma) {
×
436
        return terrno;
×
437
      }
438
      TAOS_CHECK_RETURN(tDecodeTSma(pCoder, pME->smaEntry.tsma, true));
×
439
    } else {
440
      metaError("meta/entry: invalide table type: %" PRId8 " decode failed.", pME->type);
×
441
      return TSDB_CODE_INVALID_PARA;
×
442
    }
443
    if (pME->type == TSDB_SUPER_TABLE) {
1,303,802,998✔
444
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
644,922,305✔
445
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
644,959,536✔
446

447
        if (pME->colCmpr.nCols == 0) {
645,011,478✔
448
          TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->stbEntry.schemaRow));
×
449
        }
450
      } else {
451
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->stbEntry.schemaRow));
×
452
        TABLE_SET_COL_COMPRESSED(pME->flags);
×
453
      }
454
    } else if (pME->type == TSDB_NORMAL_TABLE) {
658,651,238✔
455
      if (!tDecodeIsEnd(pCoder)) {
107,576,599✔
456
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
107,580,363✔
457
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
107,580,363✔
458
        if (pME->colCmpr.nCols == 0) {
107,534,420✔
459
          TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
460
        }
461
      } else {
462
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
463
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
464
      }
465
      TABLE_SET_COL_COMPRESSED(pME->flags);
107,507,488✔
466
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
551,372,598✔
467
      if (!tDecodeIsEnd(pCoder)) {
7,233,391✔
468
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
7,232,712✔
469
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
7,233,391✔
470
      } else {
471
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
679✔
472
        if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
679✔
473
           TAOS_CHECK_RETURN(metatInitDefaultSColRefWrapper(pCoder, &pME->colRef, &pME->ntbEntry.schemaRow));
×
474
        }
475
      }
476
    }
477
    if (!tDecodeIsEnd(pCoder)) {
1,304,158,477✔
478
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
649,849,067✔
479
    } else {
480
      pME->pExtSchemas = NULL;
654,309,410✔
481
    }
482
  }
483
  if (pME->type == TSDB_SUPER_TABLE) {
1,303,898,292✔
484
    if (!tDecodeIsEnd(pCoder)) {
644,721,740✔
485
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,289,774,773✔
486
    }
487
  }
488

489

490
  tEndDecode(pCoder);
1,304,071,101✔
491
  return 0;
1,303,807,459✔
492
}
493

494
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
1,303,887,382✔
495

496
static int32_t metaCloneSchema(const SSchemaWrapper *pSrc, SSchemaWrapper *pDst) {
306,665,777✔
497
  if (pSrc == NULL || pDst == NULL) {
306,665,777✔
498
    return TSDB_CODE_INVALID_PARA;
×
499
  }
500

501
  pDst->nCols = pSrc->nCols;
306,682,818✔
502
  pDst->version = pSrc->version;
306,675,815✔
503
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
306,650,025✔
504
  if (pDst->pSchema == NULL) {
306,593,218✔
505
    return terrno;
×
506
  }
507
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
306,631,308✔
508
  return TSDB_CODE_SUCCESS;
306,683,893✔
509
}
510

511
static int32_t metaCloneRsmaParam(const SRSmaParam *pSrc, SRSmaParam *pDst) {
44,834✔
512
  if (pSrc == NULL || pDst == NULL) {
44,834✔
513
    return TSDB_CODE_INVALID_PARA;
×
514
  }
515
  memcpy(pDst, pSrc, sizeof(SRSmaParam));
45,607✔
516
  pDst->name = tstrdup(pSrc->name);
45,607✔
517
  if (pDst->name == NULL) {
45,607✔
518
    return terrno;
×
519
  }
520
  if (pSrc->nFuncs > 0) {
44,834✔
521
    pDst->nFuncs = pSrc->nFuncs;
44,834✔
522
    pDst->funcColIds = (col_id_t *)taosMemoryMalloc(pSrc->nFuncs * sizeof(col_id_t));
44,834✔
523
    if (pDst->funcColIds == NULL) {
44,834✔
524
      return terrno;
×
525
    }
526
    memcpy(pDst->funcColIds, pSrc->funcColIds, pSrc->nFuncs * sizeof(col_id_t));
44,834✔
527

528
    pDst->funcIds = (func_id_t *)taosMemoryMalloc(pSrc->nFuncs * sizeof(func_id_t));
44,834✔
529
    if (pDst->funcIds == NULL) {
44,834✔
530
      return terrno;
×
531
    }
532
    memcpy(pDst->funcIds, pSrc->funcIds, pSrc->nFuncs * sizeof(func_id_t));
44,834✔
533
  } else {
534
    pDst->nFuncs = 0;
×
535
    pDst->funcColIds = NULL;
×
536
    pDst->funcIds = NULL;
×
537
  }
538

539
  return TSDB_CODE_SUCCESS;
44,834✔
540
}
541

542
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
306,654,066✔
543
  if (pSchema) {
306,654,066✔
544
    taosMemoryFreeClear(pSchema->pSchema);
306,666,030✔
545
  }
546
}
306,650,454✔
547

548
/**
549
 * @param type 0x01 free name
550
 */
551
void metaFreeRsmaParam(SRSmaParam *pParam, int8_t type) {
67,251✔
552
  if (pParam) {
67,251✔
553
    if ((type & 0x01)) {
67,251✔
554
      taosMemoryFreeClear(pParam->name);
68,024✔
555
    }
556
    taosMemoryFreeClear(pParam->funcColIds);
68,024✔
557
    taosMemoryFreeClear(pParam->funcIds);
69,570✔
558
  }
559
}
68,024✔
560

561
void metaCloneEntryFree(SMetaEntry **ppEntry) {
175,535,530✔
562
  if (ppEntry == NULL || *ppEntry == NULL) {
175,535,530✔
563
    return;
81,237✔
564
  }
565

566
  taosMemoryFreeClear((*ppEntry)->name);
175,459,437✔
567

568
  if ((*ppEntry)->type < 0) {
175,459,664✔
569
    taosMemoryFreeClear(*ppEntry);
×
570
    return;
×
571
  }
572

573
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
175,394,532✔
574
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
148,918,094✔
575
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
148,919,541✔
576
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
148,928,744✔
577
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
68,797✔
578
    }
579
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
26,520,559✔
580
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
17,719,859✔
581
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
17,719,859✔
582
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
8,801,553✔
583
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
8,801,553✔
584
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
8,801,553✔
585
  } else {
586
    return;
×
587
  }
588
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
175,449,567✔
589
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
175,430,308✔
590
  metaCloneColRefFree(&(*ppEntry)->colRef);
175,436,320✔
591

592
  taosMemoryFreeClear(*ppEntry);
175,429,369✔
593
  return;
175,443,379✔
594
}
595

596
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
175,444,435✔
597
  int32_t code = TSDB_CODE_SUCCESS;
175,444,435✔
598

599
  if (NULL == pEntry || NULL == ppEntry) {
175,444,435✔
600
    return TSDB_CODE_INVALID_PARA;
×
601
  }
602

603
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
175,460,777✔
604
  if (NULL == *ppEntry) {
175,421,347✔
605
    return terrno;
×
606
  }
607

608
  (*ppEntry)->version = pEntry->version;
175,432,087✔
609
  (*ppEntry)->type = pEntry->type;
175,430,881✔
610
  (*ppEntry)->uid = pEntry->uid;
175,454,555✔
611

612
  if (pEntry->type < 0) {
175,461,771✔
613
    return TSDB_CODE_SUCCESS;
×
614
  }
615

616
  if (pEntry->name) {
175,458,129✔
617
    (*ppEntry)->name = tstrdup(pEntry->name);
175,447,569✔
618
    if (NULL == (*ppEntry)->name) {
175,447,496✔
619
      code = terrno;
×
620
      metaCloneEntryFree(ppEntry);
×
621
      return code;
×
622
    }
623
  }
624

625
  if (pEntry->type == TSDB_SUPER_TABLE) {
175,443,976✔
626
    (*ppEntry)->flags = pEntry->flags;
148,929,707✔
627

628
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
148,916,639✔
629
    if (code) {
148,938,803✔
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633

634
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
148,938,803✔
635
    if (code) {
148,952,037✔
636
      metaCloneEntryFree(ppEntry);
×
637
      return code;
×
638
    }
639
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
148,952,037✔
640
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
45,607✔
641
      if (code) {
44,834✔
642
        metaCloneEntryFree(ppEntry);
×
643
        return code;
×
644
      }
645
    }
646
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
148,949,153✔
647
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
26,521,290✔
648
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
17,719,737✔
649
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
17,719,798✔
650
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
17,719,859✔
651

652
    // comment
653
    (*ppEntry)->ctbEntry.commentLen = pEntry->ctbEntry.commentLen;
17,719,798✔
654
    if (pEntry->ctbEntry.commentLen > 0) {
17,719,859✔
655
      (*ppEntry)->ctbEntry.comment = taosMemoryMalloc(pEntry->ctbEntry.commentLen + 1);
4,708✔
656
      if (NULL == (*ppEntry)->ctbEntry.comment) {
4,708✔
657
        code = terrno;
×
658
        metaCloneEntryFree(ppEntry);
×
659
        return code;
×
660
      }
661
      memcpy((*ppEntry)->ctbEntry.comment, pEntry->ctbEntry.comment, pEntry->ctbEntry.commentLen + 1);
4,708✔
662
    }
663

664
    // tags
665
    STag *pTags = (STag *)pEntry->ctbEntry.pTags;
17,719,798✔
666
    (*ppEntry)->ctbEntry.pTags = taosMemoryCalloc(1, pTags->len);
17,719,859✔
667
    if (NULL == (*ppEntry)->ctbEntry.pTags) {
17,719,776✔
668
      code = terrno;
×
669
      metaCloneEntryFree(ppEntry);
×
670
      return code;
×
671
    }
672
    memcpy((*ppEntry)->ctbEntry.pTags, pEntry->ctbEntry.pTags, pTags->len);
17,719,320✔
673
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
8,801,553✔
674
    (*ppEntry)->ntbEntry.btime = pEntry->ntbEntry.btime;
8,801,553✔
675
    (*ppEntry)->ntbEntry.ttlDays = pEntry->ntbEntry.ttlDays;
8,801,553✔
676
    (*ppEntry)->ntbEntry.ncid = pEntry->ntbEntry.ncid;
8,801,553✔
677

678
    // schema
679
    code = metaCloneSchema(&pEntry->ntbEntry.schemaRow, &(*ppEntry)->ntbEntry.schemaRow);
8,801,553✔
680
    if (code) {
8,801,553✔
681
      metaCloneEntryFree(ppEntry);
×
682
      return code;
×
683
    }
684

685
    // comment
686
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
8,801,553✔
687
    if (pEntry->ntbEntry.commentLen > 0) {
8,801,553✔
688
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
4,592✔
689
      if (NULL == (*ppEntry)->ntbEntry.comment) {
4,592✔
690
        code = terrno;
×
691
        metaCloneEntryFree(ppEntry);
×
692
        return code;
×
693
      }
694
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
4,592✔
695
    }
696
  } else {
697
    return TSDB_CODE_INVALID_PARA;
×
698
  }
699

700
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
175,460,684✔
701
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
740,346✔
702
    if (code) {
719,629✔
703
      metaCloneEntryFree(ppEntry);
×
704
      return code;
×
705
    }
706
  } else {
707
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
174,729,491✔
708
    if (code) {
174,736,773✔
709
      metaCloneEntryFree(ppEntry);
×
710
      return code;
×
711
    }
712
  }
713
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
175,456,402✔
714
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
11,985,067✔
715
    if (!(*ppEntry)->pExtSchemas) {
11,976,369✔
716
      code = terrno;
×
717
      metaCloneEntryFree(ppEntry);
×
718
      return code;
×
719
    }
720
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
11,974,754✔
721
  }
722

723
  return code;
175,451,472✔
724
}
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