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

taosdata / TDengine / #4832

31 Oct 2025 03:37AM UTC coverage: 58.59% (-0.2%) from 58.771%
#4832

push

travis-ci

SallyHuo-TAOS
Merge remote-tracking branch 'origin/cover/3.0' into cover/3.0

# Conflicts:
#	test/ci/run.sh

149363 of 324176 branches covered (46.07%)

Branch coverage included in aggregate %.

198471 of 269498 relevant lines covered (73.64%)

239014265.14 hits per line

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

65.44
/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;
110,949,780✔
26
    }
27
  }
28
  return false;
2,147,483,647✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
448,151,701✔
32
  if (pME->pExtSchemas) {
448,151,701✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
28,717,889✔
34
    bool                  hasTypeMods = false;
28,717,889✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
28,717,889✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
28,409,478✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
305,460!
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
305,460✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
28,708,496✔
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;
448,284,638✔
49
}
50

51
int meteEncodeColRefEntry(SEncoder *pCoder, const SMetaEntry *pME) {
3,119,916✔
52
  const SColRefWrapper *pw = &pME->colRef;
3,119,916✔
53
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
6,239,832!
54
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
6,239,832!
55
  uTrace("encode cols:%d", pw->nCols);
3,119,916!
56

57
  for (int32_t i = 0; i < pw->nCols; i++) {
30,644,066✔
58
    SColRef *p = &pw->pColRef[i];
27,524,150✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
55,048,300!
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
55,048,300!
61
    if (p->hasRef) {
27,524,150!
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
31,504,096!
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
31,504,096!
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
31,504,096!
65
    }
66
  }
67
  return 0;
3,119,916✔
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) {
3,488,562!
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
3,488,562✔
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);
172,279,426!
84
    if (pME->pExtSchemas == NULL) {
86,132,442!
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) {
120,732,416✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
120,732,416✔
100
  bool                  hasTypeMods = false;
120,732,416✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
120,732,416✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
87,378,310✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
33,454,056!
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
33,500,984✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
120,876,751✔
109

110
  if (hasTypeMods) {
120,683,247✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
148,436!
112
    if (ret != NULL) {
148,436!
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
148,436!
114
    }
115
    return ret;
148,436✔
116
  }
117
  return NULL;
120,534,811✔
118
}
119

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

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

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

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
54,638✔
151
  int32_t    i = 0, j = 0;
54,638✔
152
  for (i = 0; i < nCols; ++i) {
422,641✔
153
    while (j < pParam->nFuncs) {
671,726✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
644,407✔
155
        pFuncIds[i] = pParam->funcIds[j];
300,509✔
156
        break;
300,509✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
343,898✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
67,494✔
160
        break;
67,494✔
161
      }
162
      ++j;
276,404✔
163
    }
164
    if (j >= pParam->nFuncs) {
395,322✔
165
      for (; i < nCols; ++i) {
54,638✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
27,319✔
167
      }
168
      break;
27,319✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
54,638✔
172

173
  return 0;
54,638✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
54,978,201✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
54,978,201✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
109,956,584!
179
  if (pWrapper->nCols == 0) {
54,976,923!
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
109,956,402!
184
  uDebug("decode cols:%d", pWrapper->nCols);
54,978,201✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
109,958,051!
186
  if (pWrapper->pColRef == NULL) {
54,979,850!
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
672,001,560✔
191
    SColRef *p = &pWrapper->pColRef[i];
617,012,756✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
1,233,998,820!
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
1,234,058,633!
194
    if (p->hasRef) {
617,036,590!
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
379,019,207!
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
379,011,432!
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
379,024,463!
198
    }
199
  }
200
  return 0;
54,979,850✔
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,626,503✔
220
  if (pSrc->nCols > 0) {
1,626,503!
221
    pDst->nCols = pSrc->nCols;
1,626,503✔
222
    pDst->version = pSrc->version;
1,626,503✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
1,626,503!
224
    if (NULL == pDst->pColRef) {
1,626,503!
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
1,626,503!
228
  }
229
  return 0;
1,626,503✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
121,123,319✔
233
  int32_t code = 0;
121,123,319✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
242,271,232!
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
242,315,706!
236
  uTrace("encode cols:%d", pw->nCols);
121,167,793✔
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;
121,296,792✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
120,532,636✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
120,532,636✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
120,679,369✔
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;
674,514✔
273

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

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

284
  for (int32_t i = 0; i < pCmpr->nCols; i++) {
3,299,008!
285
    SColCmpr *pColCmpr = &pCmpr->pColCmpr[i];
2,624,494✔
286
    SSchema  *pColSchema = &pSchema->pSchema[i];
2,624,494✔
287
    pColCmpr->id = pColSchema->colId;
2,624,494✔
288
    pColCmpr->alg = createDefaultColCmprByType(pColSchema->type);
2,624,494✔
289
  }
290
  return 0;
674,514✔
291
}
292

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
403,250,383✔
294
  if (pSrc->nCols > 0) {
403,250,383✔
295
    pDst->nCols = pSrc->nCols;
378,710,905✔
296
    pDst->version = pSrc->version;
378,730,602✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
378,703,760!
298
    if (NULL == pDst->pColCmpr) {
378,646,823!
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
378,653,634!
302
  }
303
  return 0;
403,286,056✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
404,857,904✔
307
  if (pColRef) {
404,857,904!
308
    taosMemoryFreeClear(pColRef->pColRef);
404,872,312!
309
  }
310
}
404,878,732✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
404,854,761✔
313
  if (pCmpr) {
404,854,761!
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
404,873,960!
315
  }
316
}
404,796,315✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
462,333,977✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
462,333,977!
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
924,748,266!
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
924,622,763!
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
924,492,650!
323

324
  if (pME->type > 0) {
462,262,237✔
325
    if (pME->name == NULL) {
448,249,239!
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
896,483,376!
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
448,260,049✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
162,186,729!
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
162,305,981!
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
162,391,293!
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
81,150,350✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
154,272!
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
367,077,160✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
650,819,699!
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
650,852,809!
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
650,887,087!
342
      if (pME->ctbEntry.commentLen > 0) {
325,448,864✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
99,588!
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
650,851,859!
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
325,419,652!
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
41,704,115!
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
83,408,325!
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
83,408,325!
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
83,408,420!
351
      if (pME->ntbEntry.commentLen > 0) {
41,704,210✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
122,500!
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
83,408,230!
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
83,408,230!
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) {
448,251,910✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
3,321,580!
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
445,040,735✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
81,086,654✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
364,091,145✔
368
        if (pME->colCmpr.nCols != 0) {
40,127,951✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
39,453,532!
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
674,324!
372
          SColCmprWrapper colCmprs = {0};
674,419✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
674,514!
374
          if (code != 0) {
674,514!
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
674,514✔
379
          taosMemoryFree(colCmprs.pColCmpr);
674,419!
380
          TAOS_CHECK_RETURN(code);
674,419!
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
448,282,700!
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
462,252,093✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
162,194,099!
388
  }
389

390
  tEndEncode(pCoder);
462,159,540✔
391
  return 0;
462,128,204✔
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);
1,045,504✔
402
    return 0;
1,045,504✔
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));
586,555!
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) {
2,147,483,647✔
420
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
183,136!
421
      }
422
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
2,147,483,647!
423
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
2,147,483,647!
424
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
651,725,444!
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
1,303,730,478!
426
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
1,304,017,258!
427
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
1,303,785,463!
428
      if (pME->ntbEntry.commentLen > 0) {
651,753,405✔
429
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
243,478!
430
      }
431
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
1,303,327,642!
432
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
1,303,795,525!
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)) {
640,920,758!
456
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
641,051,672✔
457
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
641,052,931!
458
        if (pME->colCmpr.nCols == 0) {
640,952,593!
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);
641,148,698✔
466
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
2,147,483,647✔
467
      if (!tDecodeIsEnd(pCoder)) {
54,977,143!
468
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
54,978,402✔
469
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
54,980,920!
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) {
738,318,982✔
497
  if (pSrc == NULL || pDst == NULL) {
738,318,982!
498
    return TSDB_CODE_INVALID_PARA;
×
499
  }
500

501
  pDst->nCols = pSrc->nCols;
738,370,187✔
502
  pDst->version = pSrc->version;
738,345,972✔
503
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
738,356,213!
504
  if (pDst->pSchema == NULL) {
738,161,294!
505
    return terrno;
×
506
  }
507
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
738,210,775!
508
  return TSDB_CODE_SUCCESS;
738,366,981✔
509
}
510

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

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

539
  return TSDB_CODE_SUCCESS;
88,385✔
540
}
541

542
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
738,255,347✔
543
  if (pSchema) {
738,255,347!
544
    taosMemoryFreeClear(pSchema->pSchema);
738,276,020!
545
  }
546
}
738,270,623✔
547

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

561
void metaCloneEntryFree(SMetaEntry **ppEntry) {
405,033,328✔
562
  if (ppEntry == NULL || *ppEntry == NULL) {
405,033,328!
563
    return;
182,399✔
564
  }
565

566
  taosMemoryFreeClear((*ppEntry)->name);
404,865,769!
567

568
  if ((*ppEntry)->type < 0) {
404,849,751!
569
    taosMemoryFreeClear(*ppEntry);
×
570
    return;
×
571
  }
572

573
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
404,841,172✔
574
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
358,573,638✔
575
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
358,540,323✔
576
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
358,531,010✔
577
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
133,381✔
578
    }
579
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
46,293,145✔
580
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
25,099,063!
581
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
25,097,930!
582
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
21,194,265!
583
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
21,195,215✔
584
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
21,195,215!
585
  } else {
586
    return;
×
587
  }
588
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
404,863,822✔
589
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
404,817,560!
590
  metaCloneColRefFree(&(*ppEntry)->colRef);
404,813,751✔
591

592
  taosMemoryFreeClear(*ppEntry);
404,779,531!
593
  return;
404,821,231✔
594
}
595

596
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
404,806,555✔
597
  int32_t code = TSDB_CODE_SUCCESS;
404,806,555✔
598

599
  if (NULL == pEntry || NULL == ppEntry) {
404,806,555!
600
    return TSDB_CODE_INVALID_PARA;
×
601
  }
602

603
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
404,896,916!
604
  if (NULL == *ppEntry) {
404,733,257!
605
    return terrno;
×
606
  }
607

608
  (*ppEntry)->version = pEntry->version;
404,796,750✔
609
  (*ppEntry)->type = pEntry->type;
404,867,140✔
610
  (*ppEntry)->uid = pEntry->uid;
404,849,831✔
611

612
  if (pEntry->type < 0) {
404,842,367!
613
    return TSDB_CODE_SUCCESS;
×
614
  }
615

616
  if (pEntry->name) {
404,792,769!
617
    (*ppEntry)->name = tstrdup(pEntry->name);
404,880,203✔
618
    if (NULL == (*ppEntry)->name) {
404,899,250!
619
      code = terrno;
×
620
      metaCloneEntryFree(ppEntry);
×
621
      return code;
×
622
    }
623
  }
624

625
  if (pEntry->type == TSDB_SUPER_TABLE) {
404,870,966✔
626
    (*ppEntry)->flags = pEntry->flags;
358,606,432✔
627

628
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
358,555,178✔
629
    if (code) {
358,569,056!
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633

634
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
358,569,056✔
635
    if (code) {
358,601,720!
636
      metaCloneEntryFree(ppEntry);
×
637
      return code;
×
638
    }
639
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
358,601,720✔
640
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
88,385✔
641
      if (code) {
88,385!
642
        metaCloneEntryFree(ppEntry);
×
643
        return code;
×
644
      }
645
    }
646
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
358,612,010✔
647
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
46,294,392✔
648
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
25,101,671✔
649
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
25,101,671✔
650
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
25,100,424✔
651

652
    // comment
653
    (*ppEntry)->ctbEntry.commentLen = pEntry->ctbEntry.commentLen;
25,101,557✔
654
    if (pEntry->ctbEntry.commentLen > 0) {
25,102,918✔
655
      (*ppEntry)->ctbEntry.comment = taosMemoryMalloc(pEntry->ctbEntry.commentLen + 1);
29,109!
656
      if (NULL == (*ppEntry)->ctbEntry.comment) {
29,109!
657
        code = terrno;
×
658
        metaCloneEntryFree(ppEntry);
×
659
        return code;
×
660
      }
661
      memcpy((*ppEntry)->ctbEntry.comment, pEntry->ctbEntry.comment, pEntry->ctbEntry.commentLen + 1);
29,109!
662
    }
663

664
    // tags
665
    STag *pTags = (STag *)pEntry->ctbEntry.pTags;
25,101,557✔
666
    (*ppEntry)->ctbEntry.pTags = taosMemoryCalloc(1, pTags->len);
25,099,177!
667
    if (NULL == (*ppEntry)->ctbEntry.pTags) {
25,102,918!
668
      code = terrno;
×
669
      metaCloneEntryFree(ppEntry);
×
670
      return code;
×
671
    }
672
    memcpy((*ppEntry)->ctbEntry.pTags, pEntry->ctbEntry.pTags, pTags->len);
25,102,918!
673
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
21,195,215!
674
    (*ppEntry)->ntbEntry.btime = pEntry->ntbEntry.btime;
21,195,215✔
675
    (*ppEntry)->ntbEntry.ttlDays = pEntry->ntbEntry.ttlDays;
21,195,215✔
676
    (*ppEntry)->ntbEntry.ncid = pEntry->ntbEntry.ncid;
21,195,215✔
677

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

685
    // comment
686
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
21,195,215✔
687
    if (pEntry->ntbEntry.commentLen > 0) {
21,195,215✔
688
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
38,129!
689
      if (NULL == (*ppEntry)->ntbEntry.comment) {
38,129!
690
        code = terrno;
×
691
        metaCloneEntryFree(ppEntry);
×
692
        return code;
×
693
      }
694
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
38,129!
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) {
404,898,586✔
701
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
1,700,278✔
702
    if (code) {
1,626,503!
703
      metaCloneEntryFree(ppEntry);
×
704
      return code;
×
705
    }
706
  } else {
707
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
403,208,802✔
708
    if (code) {
403,279,319!
709
      metaCloneEntryFree(ppEntry);
×
710
      return code;
×
711
    }
712
  }
713
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
404,905,822✔
714
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
27,277,734!
715
    if (!(*ppEntry)->pExtSchemas) {
27,268,858!
716
      code = terrno;
×
717
      metaCloneEntryFree(ppEntry);
×
718
      return code;
×
719
    }
720
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
27,272,786!
721
  }
722

723
  return code;
404,870,547✔
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