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

taosdata / TDengine / #4882

14 Dec 2025 03:48AM UTC coverage: 64.674% (+4.1%) from 60.617%
#4882

push

travis-ci

web-flow
test: update coverage workflow time (#33918)

167512 of 259010 relevant lines covered (64.67%)

106102375.15 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) {
696,711,614✔
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;
39,771,551✔
26
    }
27
  }
28
  return false;
658,056,240✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
181,650,505✔
32
  if (pME->pExtSchemas) {
181,650,505✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
14,655,367✔
34
    bool                  hasTypeMods = false;
14,655,367✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
14,655,367✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
14,500,856✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
151,554✔
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
151,554✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
14,656,448✔
43

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

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

57
  for (int32_t i = 0; i < pw->nCols; i++) {
11,898,470✔
58
    SColRef *p = &pw->pColRef[i];
10,649,026✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
21,298,052✔
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
21,298,052✔
61
    if (p->hasRef) {
10,649,026✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
11,918,160✔
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
11,918,160✔
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
11,918,160✔
65
    }
66
  }
67
  return 0;
1,249,444✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
668,332,259✔
71
  bool hasExtSchema = false;
668,332,259✔
72
  SSchemaWrapper* pSchWrapper = NULL;
668,332,259✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
668,332,259✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
661,426,894✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
6,926,302✔
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
6,926,302✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
668,406,256✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
668,309,112✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
56,469,836✔
84
    if (pME->pExtSchemas == NULL) {
28,235,862✔
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;
640,076,713✔
93
  }
94

95
  return 0;
668,361,062✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
13,733,364✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
13,733,364✔
100
  bool                  hasTypeMods = false;
13,733,364✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
13,733,364✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
12,099,196✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
1,638,370✔
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
1,640,215✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
13,737,500✔
109

110
  if (hasTypeMods) {
13,731,977✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
40,239✔
112
    if (ret != NULL) {
40,239✔
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
40,239✔
114
    }
115
    return ret;
40,239✔
116
  }
117
  return NULL;
13,691,738✔
118
}
119

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

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

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

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
27,792✔
151
  int32_t    i = 0, j = 0;
27,792✔
152
  for (i = 0; i < nCols; ++i) {
213,844✔
153
    while (j < pParam->nFuncs) {
338,136✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
325,012✔
155
        pFuncIds[i] = pParam->funcIds[j];
152,084✔
156
        break;
152,084✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
172,928✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
33,968✔
160
        break;
33,968✔
161
      }
162
      ++j;
138,960✔
163
    }
164
    if (j >= pParam->nFuncs) {
199,176✔
165
      for (; i < nCols; ++i) {
26,248✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
13,124✔
167
      }
168
      break;
13,124✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
27,792✔
172

173
  return 0;
27,792✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
10,805,798✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
10,805,798✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
21,611,448✔
179
  if (pWrapper->nCols == 0) {
10,805,650✔
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
21,612,916✔
184
  uDebug("decode cols:%d", pWrapper->nCols);
10,806,458✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
21,612,515✔
186
  if (pWrapper->pColRef == NULL) {
10,806,458✔
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
131,189,981✔
191
    SColRef *p = &pWrapper->pColRef[i];
120,377,188✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
240,780,196✔
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
240,797,362✔
194
    if (p->hasRef) {
120,403,366✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
70,079,415✔
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
70,077,261✔
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
70,080,593✔
198
    }
199
  }
200
  return 0;
10,806,458✔
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) {
699,727✔
220
  if (pSrc->nCols > 0) {
699,727✔
221
    pDst->nCols = pSrc->nCols;
699,727✔
222
    pDst->version = pSrc->version;
699,727✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
699,727✔
224
    if (NULL == pDst->pColRef) {
699,727✔
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
699,727✔
228
  }
229
  return 0;
699,727✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
42,503,191✔
233
  int32_t code = 0;
42,503,191✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
84,994,774✔
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
84,992,269✔
236
  uTrace("encode cols:%d", pw->nCols);
42,500,686✔
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;
42,549,562✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
42,260,304✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
42,260,304✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
42,303,959✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
755,268,072✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
755,268,072✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
1,510,600,961✔
252
  if (pWrapper->nCols == 0) {
755,219,306✔
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
1,510,505,160✔
257
  uDebug("dencode cols:%d", pWrapper->nCols);
755,253,459✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
1,510,566,371✔
259
  if (pWrapper->pColCmpr == NULL) {
755,287,048✔
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;
755,466,666✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
250,444✔
273

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

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

284
  for (int32_t i = 0; i < pCmpr->nCols; i++) {
1,277,232✔
285
    SColCmpr *pColCmpr = &pCmpr->pColCmpr[i];
1,026,788✔
286
    SSchema  *pColSchema = &pSchema->pSchema[i];
1,026,788✔
287
    pColCmpr->id = pColSchema->colId;
1,026,788✔
288
    pColCmpr->alg = createDefaultColCmprByType(pColSchema->type);
1,026,788✔
289
  }
290
  return 0;
250,444✔
291
}
292

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
175,843,216✔
294
  if (pSrc->nCols > 0) {
175,843,216✔
295
    pDst->nCols = pSrc->nCols;
158,359,036✔
296
    pDst->version = pSrc->version;
158,360,634✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
158,353,590✔
298
    if (NULL == pDst->pColCmpr) {
158,346,341✔
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
158,339,039✔
302
  }
303
  return 0;
175,829,323✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
176,532,786✔
307
  if (pColRef) {
176,532,786✔
308
    taosMemoryFreeClear(pColRef->pColRef);
176,539,062✔
309
  }
310
}
176,537,036✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
176,529,482✔
313
  if (pCmpr) {
176,529,482✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
176,530,300✔
315
  }
316
}
176,549,337✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
185,582,592✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
185,582,592✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
371,176,319✔
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
371,185,093✔
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
371,184,599✔
323

324
  if (pME->type > 0) {
185,587,443✔
325
    if (pME->name == NULL) {
181,652,651✔
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
363,281,215✔
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
181,638,354✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
61,307,234✔
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
61,394,400✔
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
61,375,172✔
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
30,657,717✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
79,516✔
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
150,985,497✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
276,953,434✔
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
276,953,043✔
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
276,949,775✔
342
      if (pME->ctbEntry.commentLen > 0) {
138,476,142✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
15,276✔
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
276,953,909✔
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
138,475,388✔
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
12,512,682✔
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
25,025,364✔
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
25,025,364✔
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
25,025,364✔
351
      if (pME->ntbEntry.commentLen > 0) {
12,512,682✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
14,684✔
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
25,025,364✔
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
25,025,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) {
181,665,574✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,297,921✔
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
180,376,068✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
30,691,534✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
149,740,771✔
368
        if (pME->colCmpr.nCols != 0) {
11,854,856✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
11,604,412✔
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
250,444✔
372
          SColCmprWrapper colCmprs = {0};
250,444✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
250,444✔
374
          if (code != 0) {
250,444✔
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
250,444✔
379
          taosMemoryFree(colCmprs.pColCmpr);
250,444✔
380
          TAOS_CHECK_RETURN(code);
250,444✔
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
181,671,693✔
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
185,626,088✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
61,352,233✔
388
  }
389

390
  tEndEncode(pCoder);
185,584,488✔
391
  return 0;
185,581,167✔
392
}
393

394
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
1,325,739,761✔
395
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
1,325,739,761✔
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,326,028,327✔
401
    tEndDecode(pCoder);
101,938✔
402
    return 0;
101,938✔
403
  }
404

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

408
    if (pME->type == TSDB_SUPER_TABLE) {
1,325,856,001✔
409
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,323,075,666✔
410
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,323,115,774✔
411
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,323,061,852✔
412
      if (TABLE_IS_ROLLUP(pME->flags)) {
661,498,353✔
413
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
295,676✔
414
      }
415
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
664,134,139✔
416
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
1,135,804,912✔
417
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
1,135,919,830✔
418
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
1,135,831,167✔
419
      if (pME->ctbEntry.commentLen > 0) {
567,870,702✔
420
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
21,346✔
421
      }
422
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
1,135,785,554✔
423
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
567,955,742✔
424
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
96,376,366✔
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
192,765,379✔
426
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
192,778,781✔
427
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
192,771,489✔
428
      if (pME->ntbEntry.commentLen > 0) {
96,381,599✔
429
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
21,688✔
430
      }
431
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
192,758,559✔
432
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
192,760,846✔
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,325,712,280✔
444
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
661,550,177✔
445
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
661,542,706✔
446

447
        if (pME->colCmpr.nCols == 0) {
661,587,911✔
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) {
664,097,277✔
455
      if (!tDecodeIsEnd(pCoder)) {
93,865,196✔
456
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
93,864,074✔
457
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
93,864,074✔
458
        if (pME->colCmpr.nCols == 0) {
93,853,509✔
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);
1,562✔
463
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
1,562✔
464
      }
465
      TABLE_SET_COL_COMPRESSED(pME->flags);
93,856,347✔
466
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
570,246,658✔
467
      if (!tDecodeIsEnd(pCoder)) {
10,805,798✔
468
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
10,805,798✔
469
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
10,806,569✔
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)) {
1,325,812,848✔
478
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
668,461,091✔
479
    } else {
480
      pME->pExtSchemas = NULL;
657,351,757✔
481
    }
482
  }
483
  if (pME->type == TSDB_SUPER_TABLE) {
1,325,600,928✔
484
    if (!tDecodeIsEnd(pCoder)) {
661,442,082✔
485
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,323,012,469✔
486
    }
487
  }
488

489

490
  tEndDecode(pCoder);
1,325,679,294✔
491
  return 0;
1,325,521,957✔
492
}
493

494
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
1,325,686,617✔
495

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

501
  pDst->nCols = pSrc->nCols;
308,805,223✔
502
  pDst->version = pSrc->version;
308,806,636✔
503
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
308,805,700✔
504
  if (pDst->pSchema == NULL) {
308,750,823✔
505
    return terrno;
×
506
  }
507
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
308,766,949✔
508
  return TSDB_CODE_SUCCESS;
308,807,346✔
509
}
510

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

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

539
  return TSDB_CODE_SUCCESS;
45,548✔
540
}
541

542
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
308,780,100✔
543
  if (pSchema) {
308,780,100✔
544
    taosMemoryFreeClear(pSchema->pSchema);
308,785,720✔
545
  }
546
}
308,775,578✔
547

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

561
void metaCloneEntryFree(SMetaEntry **ppEntry) {
176,593,323✔
562
  if (ppEntry == NULL || *ppEntry == NULL) {
176,593,323✔
563
    return;
79,563✔
564
  }
565

566
  taosMemoryFreeClear((*ppEntry)->name);
176,518,492✔
567

568
  if ((*ppEntry)->type < 0) {
176,545,284✔
569
    taosMemoryFreeClear(*ppEntry);
×
570
    return;
×
571
  }
572

573
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
176,487,565✔
574
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
149,975,725✔
575
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
149,962,675✔
576
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
149,964,431✔
577
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
68,708✔
578
    }
579
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
26,558,783✔
580
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
17,714,881✔
581
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
17,716,000✔
582
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
8,843,902✔
583
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
8,843,902✔
584
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
8,843,902✔
585
  } else {
586
    return;
×
587
  }
588
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
176,524,581✔
589
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
176,519,396✔
590
  metaCloneColRefFree(&(*ppEntry)->colRef);
176,526,462✔
591

592
  taosMemoryFreeClear(*ppEntry);
176,503,719✔
593
  return;
176,524,026✔
594
}
595

596
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
176,528,050✔
597
  int32_t code = TSDB_CODE_SUCCESS;
176,528,050✔
598

599
  if (NULL == pEntry || NULL == ppEntry) {
176,528,050✔
600
    return TSDB_CODE_INVALID_PARA;
×
601
  }
602

603
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
176,538,418✔
604
  if (NULL == *ppEntry) {
176,509,655✔
605
    return terrno;
×
606
  }
607

608
  (*ppEntry)->version = pEntry->version;
176,515,598✔
609
  (*ppEntry)->type = pEntry->type;
176,527,310✔
610
  (*ppEntry)->uid = pEntry->uid;
176,524,657✔
611

612
  if (pEntry->type < 0) {
176,537,625✔
613
    return TSDB_CODE_SUCCESS;
×
614
  }
615

616
  if (pEntry->name) {
176,534,769✔
617
    (*ppEntry)->name = tstrdup(pEntry->name);
176,531,226✔
618
    if (NULL == (*ppEntry)->name) {
176,535,302✔
619
      code = terrno;
×
620
      metaCloneEntryFree(ppEntry);
×
621
      return code;
×
622
    }
623
  }
624

625
  if (pEntry->type == TSDB_SUPER_TABLE) {
176,532,226✔
626
    (*ppEntry)->flags = pEntry->flags;
149,959,287✔
627

628
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
149,974,768✔
629
    if (code) {
149,981,261✔
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633

634
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
149,981,261✔
635
    if (code) {
149,990,072✔
636
      metaCloneEntryFree(ppEntry);
×
637
      return code;
×
638
    }
639
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
149,990,072✔
640
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
45,548✔
641
      if (code) {
45,548✔
642
        metaCloneEntryFree(ppEntry);
×
643
        return code;
×
644
      }
645
    }
646
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
149,989,430✔
647
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
26,559,902✔
648
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
17,716,000✔
649
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
17,716,000✔
650
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
17,716,000✔
651

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

664
    // tags
665
    STag *pTags = (STag *)pEntry->ctbEntry.pTags;
17,716,000✔
666
    (*ppEntry)->ctbEntry.pTags = taosMemoryCalloc(1, pTags->len);
17,716,000✔
667
    if (NULL == (*ppEntry)->ctbEntry.pTags) {
17,715,951✔
668
      code = terrno;
×
669
      metaCloneEntryFree(ppEntry);
×
670
      return code;
×
671
    }
672
    memcpy((*ppEntry)->ctbEntry.pTags, pEntry->ctbEntry.pTags, pTags->len);
17,715,480✔
673
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
8,843,902✔
674
    (*ppEntry)->ntbEntry.btime = pEntry->ntbEntry.btime;
8,843,902✔
675
    (*ppEntry)->ntbEntry.ttlDays = pEntry->ntbEntry.ttlDays;
8,843,902✔
676
    (*ppEntry)->ntbEntry.ncid = pEntry->ntbEntry.ncid;
8,843,902✔
677

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

685
    // comment
686
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
8,843,902✔
687
    if (pEntry->ntbEntry.commentLen > 0) {
8,843,902✔
688
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
4,604✔
689
      if (NULL == (*ppEntry)->ntbEntry.comment) {
4,604✔
690
        code = terrno;
×
691
        metaCloneEntryFree(ppEntry);
×
692
        return code;
×
693
      }
694
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
4,604✔
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) {
176,541,779✔
701
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
713,486✔
702
    if (code) {
699,727✔
703
      metaCloneEntryFree(ppEntry);
×
704
      return code;
×
705
    }
706
  } else {
707
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
175,832,427✔
708
    if (code) {
175,839,409✔
709
      metaCloneEntryFree(ppEntry);
×
710
      return code;
×
711
    }
712
  }
713
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
176,539,136✔
714
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
12,357,715✔
715
    if (!(*ppEntry)->pExtSchemas) {
12,356,338✔
716
      code = terrno;
×
717
      metaCloneEntryFree(ppEntry);
×
718
      return code;
×
719
    }
720
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
12,355,982✔
721
  }
722

723
  return code;
176,537,293✔
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