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

taosdata / TDengine / #4824

27 Oct 2025 05:39AM UTC coverage: 61.409% (+0.2%) from 61.242%
#4824

push

travis-ci

web-flow
Merge pull request #33376 from taosdata/3.0

merge 3.0

156919 of 324854 branches covered (48.3%)

Branch coverage included in aggregate %.

371 of 493 new or added lines in 25 files covered. (75.25%)

2822 existing lines in 108 files now uncovered.

208404 of 270043 relevant lines covered (77.17%)

229356519.53 hits per line

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

65.89
/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) {
2,147,483,647✔
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;
103,499,832✔
26
    }
27
  }
28
  return false;
2,147,483,647✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
485,972,071✔
32
  if (pME->pExtSchemas) {
485,972,071✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
24,461,498✔
34
    bool                  hasTypeMods = false;
24,461,498✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
24,461,498✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
24,154,736✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
296,576!
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
296,576✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
24,437,029✔
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;
486,148,569✔
49
}
50

51
int meteEncodeColRefEntry(SEncoder *pCoder, const SMetaEntry *pME) {
2,847,356✔
52
  const SColRefWrapper *pw = &pME->colRef;
2,847,356✔
53
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
5,694,712!
54
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
5,694,712!
55
  uTrace("encode cols:%d", pw->nCols);
2,847,356!
56

57
  for (int32_t i = 0; i < pw->nCols; i++) {
27,836,278✔
58
    SColRef *p = &pw->pColRef[i];
24,988,922✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
49,977,844!
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
49,977,844!
61
    if (p->hasRef) {
24,988,922!
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
28,307,848!
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
28,307,848!
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
28,307,848!
65
    }
66
  }
67
  return 0;
2,847,356✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
2,147,483,647✔
71
  bool hasExtSchema = false;
2,147,483,647✔
72
  SSchemaWrapper* pSchWrapper = NULL;
2,147,483,647✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
2,147,483,647✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
2,147,483,647✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
9,650,277!
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
9,650,277✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
2,147,483,647✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
2,147,483,647!
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
164,655,494!
84
    if (pME->pExtSchemas == NULL) {
82,325,927!
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;
2,147,483,647✔
93
  }
94

95
  return 0;
2,147,483,647✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
100,767,228✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
100,767,228✔
100
  bool                  hasTypeMods = false;
100,767,228✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
100,767,228✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
73,183,918✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
27,667,114!
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
27,669,186✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
100,865,746✔
109

110
  if (hasTypeMods) {
100,743,939✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
126,321!
112
    if (ret != NULL) {
127,517!
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
127,517!
114
    }
115
    return ret;
127,517✔
116
  }
117
  return NULL;
100,617,618✔
118
}
119

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

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

131
  *rsmaSchema = (SSchemaRsma *)taosMemoryMalloc(sizeof(SSchemaRsma));
38,284!
132
  if (*rsmaSchema == NULL) {
38,284!
133
    return terrno;
×
134
  }
135

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
38,284✔
151
  int32_t    i = 0, j = 0;
38,284✔
152
  for (i = 0; i < nCols; ++i) {
296,138✔
153
    while (j < pParam->nFuncs) {
470,668✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
451,526✔
155
        pFuncIds[i] = pParam->funcIds[j];
210,562✔
156
        break;
210,562✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
240,964✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
47,292✔
160
        break;
47,292✔
161
      }
162
      ++j;
193,672✔
163
    }
164
    if (j >= pParam->nFuncs) {
276,996✔
165
      for (; i < nCols; ++i) {
38,284✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
19,142✔
167
      }
168
      break;
19,142✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
38,284✔
172

173
  return 0;
38,284✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
54,551,824✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
54,551,824✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
109,106,606!
179
  if (pWrapper->nCols == 0) {
54,552,055!
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
109,104,097!
184
  uDebug("decode cols:%d", pWrapper->nCols);
54,554,575✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
109,110,304!
186
  if (pWrapper->pColRef == NULL) {
54,554,789!
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
646,506,119✔
191
    SColRef *p = &pWrapper->pColRef[i];
591,931,618✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
1,183,917,617!
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
1,183,916,521!
194
    if (p->hasRef) {
591,958,834!
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
366,852,664!
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
366,846,471!
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
366,830,846!
198
    }
199
  }
200
  return 0;
54,557,285✔
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) {
1,497,452✔
220
  if (pSrc->nCols > 0) {
1,497,452!
221
    pDst->nCols = pSrc->nCols;
1,497,452✔
222
    pDst->version = pSrc->version;
1,497,452✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
1,497,452!
224
    if (NULL == pDst->pColRef) {
1,497,452!
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
1,497,452!
228
  }
229
  return 0;
1,497,452✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
109,294,412✔
233
  int32_t code = 0;
109,294,412✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
218,571,889!
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
218,646,212!
236
  uTrace("encode cols:%d", pw->nCols);
109,368,735✔
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;
109,509,960✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
108,776,187✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
108,776,187✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
108,948,493✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
2,147,483,647✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
2,147,483,647✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
2,147,483,647!
252
  if (pWrapper->nCols == 0) {
2,147,483,647!
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
2,147,483,647!
257
  uDebug("dencode cols:%d", pWrapper->nCols);
2,147,483,647✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
2,147,483,647✔
259
  if (pWrapper->pColCmpr == NULL) {
2,147,483,647!
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;
2,147,483,647✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
604,374✔
273

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

280
  if (pCmpr->pColCmpr == NULL) {
604,374!
281
    return terrno;
×
282
  }
283

284
  for (int32_t i = 0; i < pCmpr->nCols; i++) {
2,952,120!
285
    SColCmpr *pColCmpr = &pCmpr->pColCmpr[i];
2,347,746✔
286
    SSchema  *pColSchema = &pSchema->pSchema[i];
2,347,746✔
287
    pColCmpr->id = pColSchema->colId;
2,347,746✔
288
    pColCmpr->alg = createDefaultColCmprByType(pColSchema->type);
2,347,746✔
289
  }
290
  return 0;
604,374✔
291
}
292

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
482,415,334✔
294
  if (pSrc->nCols > 0) {
482,415,334✔
295
    pDst->nCols = pSrc->nCols;
438,833,512✔
296
    pDst->version = pSrc->version;
438,826,842✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
438,790,525!
298
    if (NULL == pDst->pColCmpr) {
438,778,635!
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
438,736,853!
302
  }
303
  return 0;
482,430,732✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
483,853,715✔
307
  if (pColRef) {
483,853,715!
308
    taosMemoryFreeClear(pColRef->pColRef);
483,889,370!
309
  }
310
}
483,884,595✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
483,852,114✔
313
  if (pCmpr) {
483,852,114!
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
483,880,314!
315
  }
316
}
483,859,883✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
531,176,125✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
531,176,125!
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
1,062,459,200!
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
1,062,392,774!
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
1,062,260,842!
323

324
  if (pME->type > 0) {
531,110,564✔
325
    if (pME->name == NULL) {
486,190,879!
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
972,269,419!
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
486,139,357✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
147,654,666!
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
147,882,064!
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
147,958,275!
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
73,882,559✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
106,970!
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
412,213,206✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
750,468,516!
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
750,462,072!
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
750,494,052!
342
      if (pME->ctbEntry.commentLen > 0) {
375,268,221✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
70,916!
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
750,480,396!
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
375,232,113!
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
37,010,002!
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
74,020,004!
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
74,020,004!
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
74,020,004!
351
      if (pME->ntbEntry.commentLen > 0) {
37,010,002✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
97,204!
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
74,020,004!
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
74,020,004!
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) {
486,132,185✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
3,057,767!
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
483,181,278✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
73,896,020✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
409,482,643✔
368
        if (pME->colCmpr.nCols != 0) {
35,558,124✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
34,953,750!
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
604,374!
372
          SColCmprWrapper colCmprs = {0};
604,374✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
604,374!
374
          if (code != 0) {
604,374!
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
604,374✔
379
          taosMemoryFree(colCmprs.pColCmpr);
604,374!
380
          TAOS_CHECK_RETURN(code);
604,374!
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
486,168,549!
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
531,083,108✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
147,761,997!
388
  }
389

390
  tEndEncode(pCoder);
531,064,613✔
391
  return 0;
531,001,830✔
392
}
393

394
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
2,147,483,647✔
395
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
2,147,483,647!
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) {
2,147,483,647✔
401
    tEndDecode(pCoder);
857,718✔
402
    return 0;
857,718✔
403
  }
404

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

408
    if (pME->type == TSDB_SUPER_TABLE) {
2,147,483,647✔
409
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
2,147,483,647!
410
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
2,147,483,647!
411
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
2,147,483,647!
412
      if (TABLE_IS_ROLLUP(pME->flags)) {
2,147,483,647✔
413
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
410,990!
414
      }
415
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
2,147,483,647✔
416
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
2,147,483,647!
417
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
2,147,483,647!
418
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
2,147,483,647!
419
      if (pME->ctbEntry.commentLen > 0) {
1,967,789,662✔
420
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
125,546!
421
      }
422
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
2,147,483,647!
423
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
1,969,106,618!
424
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
577,178,244!
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
1,154,912,653!
426
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
1,155,255,324!
427
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
1,154,881,861!
428
      if (pME->ntbEntry.commentLen > 0) {
577,241,784✔
429
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
183,428!
430
      }
431
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
1,154,506,438!
432
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
1,155,015,773!
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) {
2,147,483,647✔
444
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
2,147,483,647!
445
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
2,147,483,647!
446

447
        if (pME->colCmpr.nCols == 0) {
2,147,483,647!
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) {
2,147,483,647✔
455
      if (!tDecodeIsEnd(pCoder)) {
567,249,540!
456
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
567,405,665✔
457
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
567,405,665!
458
        if (pME->colCmpr.nCols == 0) {
567,267,172!
459
          TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
460
        }
461
      } else {
UNCOV
462
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
UNCOV
463
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
464
      }
465
      TABLE_SET_COL_COMPRESSED(pME->flags);
567,521,309✔
466
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
1,976,471,854✔
467
      if (!tDecodeIsEnd(pCoder)) {
54,552,249!
468
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
54,553,611✔
469
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
54,558,215!
470
      } else {
471
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
472
        if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
×
473
           TAOS_CHECK_RETURN(metatInitDefaultSColRefWrapper(pCoder, &pME->colRef, &pME->ntbEntry.schemaRow));
×
474
        }
475
      }
476
    }
477
    if (!tDecodeIsEnd(pCoder)) {
2,147,483,647✔
478
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
2,147,483,647!
479
    } else {
480
      pME->pExtSchemas = NULL;
2,147,483,647✔
481
    }
482
  }
483
  if (pME->type == TSDB_SUPER_TABLE) {
2,147,483,647✔
484
    if (!tDecodeIsEnd(pCoder)) {
2,147,483,647✔
485
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
2,147,483,647!
486
    }
487
  }
488

489

490
  tEndDecode(pCoder);
2,147,483,647✔
491
  return 0;
2,147,483,647✔
492
}
493

494
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
2,147,483,647✔
495

496
static int32_t metaCloneSchema(const SSchemaWrapper *pSrc, SSchemaWrapper *pDst) {
860,096,670✔
497
  if (pSrc == NULL || pDst == NULL) {
860,096,670!
498
    return TSDB_CODE_INVALID_PARA;
×
499
  }
500

501
  pDst->nCols = pSrc->nCols;
860,174,768✔
502
  pDst->version = pSrc->version;
860,138,400✔
503
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
860,154,761!
504
  if (pDst->pSchema == NULL) {
859,913,104!
505
    return terrno;
×
506
  }
507
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
859,960,501!
508
  return TSDB_CODE_SUCCESS;
860,181,191✔
509
}
510

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

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

539
  return TSDB_CODE_SUCCESS;
61,930✔
540
}
541

542
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
860,026,353✔
543
  if (pSchema) {
860,026,353!
544
    taosMemoryFreeClear(pSchema->pSchema);
860,069,383!
545
  }
546
}
859,995,698✔
547

548
/**
549
 * @param type 0x01 free name
550
 */
551
void metaFreeRsmaParam(SRSmaParam *pParam, int8_t type) {
93,458✔
552
  if (pParam) {
93,458!
553
    if ((type & 0x01)) {
93,458!
554
      taosMemoryFreeClear(pParam->name);
93,458!
555
    }
556
    taosMemoryFreeClear(pParam->funcColIds);
93,458!
557
    taosMemoryFreeClear(pParam->funcIds);
91,206!
558
  }
559
}
92,332✔
560

561
void metaCloneEntryFree(SMetaEntry **ppEntry) {
484,029,675✔
562
  if (ppEntry == NULL || *ppEntry == NULL) {
484,029,675!
563
    return;
168,100✔
564
  }
565

566
  taosMemoryFreeClear((*ppEntry)->name);
483,885,973!
567

568
  if ((*ppEntry)->type < 0) {
483,897,562!
569
    taosMemoryFreeClear(*ppEntry);
×
570
    return;
×
571
  }
572

573
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
483,855,205✔
574
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
420,337,184✔
575
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
420,231,716✔
576
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
420,269,018✔
577
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
93,458✔
578
    }
579
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
63,567,738✔
580
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
44,079,499!
581
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
44,082,391!
582
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
19,490,844!
583
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
19,490,844✔
584
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
19,490,844!
585
  } else {
586
    return;
×
587
  }
588
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
483,888,628✔
589
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
483,833,579!
590
  metaCloneColRefFree(&(*ppEntry)->colRef);
483,814,809✔
591

592
  taosMemoryFreeClear(*ppEntry);
483,792,836!
593
  return;
483,829,154✔
594
}
595

596
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
483,837,830✔
597
  int32_t code = TSDB_CODE_SUCCESS;
483,837,830✔
598

599
  if (NULL == pEntry || NULL == ppEntry) {
483,837,830!
600
    return TSDB_CODE_INVALID_PARA;
×
601
  }
602

603
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
483,924,305!
604
  if (NULL == *ppEntry) {
483,740,464!
605
    return terrno;
×
606
  }
607

608
  (*ppEntry)->version = pEntry->version;
483,808,724✔
609
  (*ppEntry)->type = pEntry->type;
483,865,212✔
610
  (*ppEntry)->uid = pEntry->uid;
483,866,941✔
611

612
  if (pEntry->type < 0) {
483,859,177!
613
    return TSDB_CODE_SUCCESS;
×
614
  }
615

616
  if (pEntry->name) {
483,874,406✔
617
    (*ppEntry)->name = tstrdup(pEntry->name);
483,910,416✔
618
    if (NULL == (*ppEntry)->name) {
483,913,761!
619
      code = terrno;
×
620
      metaCloneEntryFree(ppEntry);
×
621
      return code;
×
622
    }
623
  }
624

625
  if (pEntry->type == TSDB_SUPER_TABLE) {
483,904,671✔
626
    (*ppEntry)->flags = pEntry->flags;
420,344,445✔
627

628
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
420,296,972✔
629
    if (code) {
420,341,992!
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633

634
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
420,341,992✔
635
    if (code) {
420,361,418!
636
      metaCloneEntryFree(ppEntry);
×
637
      return code;
×
638
    }
639
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
420,361,418✔
640
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
61,930✔
641
      if (code) {
61,930!
642
        metaCloneEntryFree(ppEntry);
×
643
        return code;
×
644
      }
645
    }
646
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
420,360,975✔
647
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
63,572,538✔
648
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
44,081,694✔
649
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
44,082,391✔
650
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
44,082,391✔
651

652
    // comment
653
    (*ppEntry)->ctbEntry.commentLen = pEntry->ctbEntry.commentLen;
44,081,531✔
654
    if (pEntry->ctbEntry.commentLen > 0) {
44,082,391✔
655
      (*ppEntry)->ctbEntry.comment = taosMemoryMalloc(pEntry->ctbEntry.commentLen + 1);
20,866!
656
      if (NULL == (*ppEntry)->ctbEntry.comment) {
20,866!
657
        code = terrno;
×
658
        metaCloneEntryFree(ppEntry);
×
659
        return code;
×
660
      }
661
      memcpy((*ppEntry)->ctbEntry.comment, pEntry->ctbEntry.comment, pEntry->ctbEntry.commentLen + 1);
20,866!
662
    }
663

664
    // tags
665
    STag *pTags = (STag *)pEntry->ctbEntry.pTags;
44,082,391✔
666
    (*ppEntry)->ctbEntry.pTags = taosMemoryCalloc(1, pTags->len);
44,082,391!
667
    if (NULL == (*ppEntry)->ctbEntry.pTags) {
44,079,952!
668
      code = terrno;
×
669
      metaCloneEntryFree(ppEntry);
×
670
      return code;
×
671
    }
672
    memcpy((*ppEntry)->ctbEntry.pTags, pEntry->ctbEntry.pTags, pTags->len);
44,080,834!
673
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
19,490,844!
674
    (*ppEntry)->ntbEntry.btime = pEntry->ntbEntry.btime;
19,490,844✔
675
    (*ppEntry)->ntbEntry.ttlDays = pEntry->ntbEntry.ttlDays;
19,490,844✔
676
    (*ppEntry)->ntbEntry.ncid = pEntry->ntbEntry.ncid;
19,490,844✔
677

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

685
    // comment
686
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
19,490,844✔
687
    if (pEntry->ntbEntry.commentLen > 0) {
19,490,844✔
688
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
30,134!
689
      if (NULL == (*ppEntry)->ntbEntry.comment) {
30,134!
690
        code = terrno;
×
691
        metaCloneEntryFree(ppEntry);
×
692
        return code;
×
693
      }
694
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
30,134!
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) {
483,922,169✔
701
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
1,552,609✔
702
    if (code) {
1,497,452!
703
      metaCloneEntryFree(ppEntry);
×
704
      return code;
×
705
    }
706
  } else {
707
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
482,369,011✔
708
    if (code) {
482,427,615!
709
      metaCloneEntryFree(ppEntry);
×
710
      return code;
×
711
    }
712
  }
713
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
483,925,067✔
714
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
23,231,509!
715
    if (!(*ppEntry)->pExtSchemas) {
23,223,014!
716
      code = terrno;
×
717
      metaCloneEntryFree(ppEntry);
×
718
      return code;
×
719
    }
720
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
23,227,650!
721
  }
722

723
  return code;
483,880,119✔
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