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

taosdata / TDengine / #4854

17 Nov 2025 09:53AM UTC coverage: 64.06% (+0.1%) from 63.951%
#4854

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

218 of 311 new or added lines in 32 files covered. (70.1%)

571 existing lines in 112 files now uncovered.

151123 of 235910 relevant lines covered (64.06%)

114377315.5 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) {
676,048,075✔
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;
35,937,392✔
26
    }
27
  }
28
  return false;
639,478,052✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
176,118,201✔
32
  if (pME->pExtSchemas) {
176,118,201✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
13,517,321✔
34
    bool                  hasTypeMods = false;
13,517,321✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
13,517,321✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
13,392,227✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
128,298✔
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
128,298✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
13,524,825✔
43

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

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

57
  for (int32_t i = 0; i < pw->nCols; i++) {
12,245,934✔
58
    SColRef *p = &pw->pColRef[i];
11,010,794✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
22,021,588✔
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
22,021,588✔
61
    if (p->hasRef) {
11,010,794✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
12,235,664✔
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
12,235,664✔
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
12,235,664✔
65
    }
66
  }
67
  return 0;
1,235,140✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
648,989,134✔
71
  bool hasExtSchema = false;
648,989,134✔
72
  SSchemaWrapper* pSchWrapper = NULL;
648,989,134✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
648,989,134✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
644,284,646✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
4,769,418✔
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
4,769,418✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
649,137,933✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
649,049,499✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
50,714,251✔
84
    if (pME->pExtSchemas == NULL) {
25,357,499✔
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;
623,695,979✔
93
  }
94

95
  return 0;
649,061,835✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
13,537,336✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
13,537,336✔
100
  bool                  hasTypeMods = false;
13,537,336✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
13,537,336✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
11,922,175✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
1,618,101✔
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
1,619,368✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
13,541,315✔
109

110
  if (hasTypeMods) {
13,534,858✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
49,762✔
112
    if (ret != NULL) {
49,762✔
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
49,762✔
114
    }
115
    return ret;
49,762✔
116
  }
117
  return NULL;
13,485,096✔
118
}
119

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

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

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

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
54,504✔
151
  int32_t    i = 0, j = 0;
54,504✔
152
  for (i = 0; i < nCols; ++i) {
419,378✔
153
    while (j < pParam->nFuncs) {
663,132✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
637,394✔
155
        pFuncIds[i] = pParam->funcIds[j];
298,258✔
156
        break;
298,258✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
339,136✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
66,616✔
160
        break;
66,616✔
161
      }
162
      ++j;
272,520✔
163
    }
164
    if (j >= pParam->nFuncs) {
390,612✔
165
      for (; i < nCols; ++i) {
51,476✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
25,738✔
167
      }
168
      break;
25,738✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
54,504✔
172

173
  return 0;
54,504✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
13,636,862✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
13,636,862✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
27,277,638✔
179
  if (pWrapper->nCols == 0) {
13,638,835✔
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
27,275,122✔
184
  uDebug("decode cols:%d", pWrapper->nCols);
13,638,228✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
27,277,360✔
186
  if (pWrapper->pColRef == NULL) {
13,639,707✔
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
219,309,609✔
191
    SColRef *p = &pWrapper->pColRef[i];
205,666,701✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
411,351,648✔
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
411,358,468✔
194
    if (p->hasRef) {
205,680,931✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
118,890,748✔
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
118,889,899✔
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
118,892,562✔
198
    }
199
  }
200
  return 0;
13,640,097✔
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) {
684,479✔
220
  if (pSrc->nCols > 0) {
684,479✔
221
    pDst->nCols = pSrc->nCols;
684,479✔
222
    pDst->version = pSrc->version;
684,479✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
684,479✔
224
    if (NULL == pDst->pColRef) {
684,479✔
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
684,479✔
228
  }
229
  return 0;
684,479✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
41,889,533✔
233
  int32_t code = 0;
41,889,533✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
83,798,365✔
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
83,848,932✔
236
  uTrace("encode cols:%d", pw->nCols);
41,940,100✔
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;
41,982,751✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
41,821,782✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
41,821,782✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
41,841,980✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
789,669,182✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
789,669,182✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
1,579,556,793✔
252
  if (pWrapper->nCols == 0) {
789,753,317✔
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
1,579,482,932✔
257
  uDebug("dencode cols:%d", pWrapper->nCols);
789,788,561✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
1,579,498,196✔
259
  if (pWrapper->pColCmpr == NULL) {
789,860,578✔
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;
790,007,796✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
154,088✔
273

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

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

284
  for (int32_t i = 0; i < pCmpr->nCols; i++) {
712,932✔
285
    SColCmpr *pColCmpr = &pCmpr->pColCmpr[i];
558,676✔
286
    SSchema  *pColSchema = &pSchema->pSchema[i];
558,676✔
287
    pColCmpr->id = pColSchema->colId;
558,676✔
288
    pColCmpr->alg = createDefaultColCmprByType(pColSchema->type);
558,676✔
289
  }
290
  return 0;
154,256✔
291
}
292

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
169,059,367✔
294
  if (pSrc->nCols > 0) {
169,059,367✔
295
    pDst->nCols = pSrc->nCols;
152,314,160✔
296
    pDst->version = pSrc->version;
152,317,701✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
152,295,758✔
298
    if (NULL == pDst->pColCmpr) {
152,290,226✔
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
152,292,873✔
302
  }
303
  return 0;
169,043,561✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
169,719,363✔
307
  if (pColRef) {
169,719,363✔
308
    taosMemoryFreeClear(pColRef->pColRef);
169,734,413✔
309
  }
310
}
169,731,437✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
169,726,566✔
313
  if (pCmpr) {
169,726,566✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
169,739,735✔
315
  }
316
}
169,716,405✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
180,009,025✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
180,009,025✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
360,042,927✔
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
360,033,158✔
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
360,016,775✔
323

324
  if (pME->type > 0) {
179,999,360✔
325
    if (pME->name == NULL) {
176,130,099✔
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
352,230,637✔
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
176,114,367✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
60,391,588✔
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
60,483,446✔
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
60,455,931✔
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
30,190,718✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
155,185✔
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
145,931,752✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
267,046,957✔
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
267,044,397✔
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
267,042,229✔
342
      if (pME->ctbEntry.commentLen > 0) {
133,522,423✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
14,880✔
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
267,048,144✔
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
133,524,197✔
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
12,412,110✔
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
24,824,220✔
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
24,824,220✔
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
24,824,220✔
351
      if (pME->ntbEntry.commentLen > 0) {
12,412,110✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
13,744✔
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
24,824,220✔
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
24,824,220✔
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) {
176,148,634✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,304,624✔
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
174,849,364✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
30,231,933✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
144,696,050✔
368
        if (pME->colCmpr.nCols != 0) {
11,751,780✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
11,597,524✔
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
154,256✔
372
          SColCmprWrapper colCmprs = {0};
154,256✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
154,256✔
374
          if (code != 0) {
154,256✔
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
154,256✔
379
          taosMemoryFree(colCmprs.pColCmpr);
154,256✔
380
          TAOS_CHECK_RETURN(code);
154,256✔
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
176,157,995✔
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
180,026,646✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
60,399,703✔
388
  }
389

390
  tEndEncode(pCoder);
179,989,601✔
391
  return 0;
180,007,336✔
392
}
393

394
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
1,370,468,499✔
395
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
1,370,468,499✔
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,370,895,876✔
401
    tEndDecode(pCoder);
100,988✔
402
    return 0;
100,988✔
403
  }
404

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

408
    if (pME->type == TSDB_SUPER_TABLE) {
1,370,668,508✔
409
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,288,911,044✔
410
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,288,976,056✔
411
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,288,838,092✔
412
      if (TABLE_IS_ROLLUP(pME->flags)) {
644,345,116✔
413
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
577,591✔
414
      }
415
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
726,038,394✔
416
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
1,153,033,631✔
417
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
1,153,156,246✔
418
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
1,153,084,714✔
419
      if (pME->ctbEntry.commentLen > 0) {
576,509,927✔
420
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
20,874✔
421
      }
422
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
1,153,002,802✔
423
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
576,578,208✔
424
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
149,692,067✔
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
299,423,072✔
426
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
299,443,085✔
427
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
299,429,605✔
428
      if (pME->ntbEntry.commentLen > 0) {
149,706,629✔
429
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
20,310✔
430
      }
431
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
299,417,072✔
432
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
299,427,355✔
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,370,529,852✔
444
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
644,396,896✔
445
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
644,442,372✔
446

447
        if (pME->colCmpr.nCols == 0) {
644,506,653✔
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) {
725,915,404✔
455
      if (!tDecodeIsEnd(pCoder)) {
145,471,331✔
456
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
145,469,053✔
457
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
145,469,440✔
458
        if (pME->colCmpr.nCols == 0) {
145,460,214✔
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);
2,278✔
463
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
2,278✔
464
      }
465
      TABLE_SET_COL_COMPRESSED(pME->flags);
145,464,766✔
466
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
580,532,097✔
467
      if (!tDecodeIsEnd(pCoder)) {
13,638,803✔
468
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
13,639,450✔
469
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
13,639,450✔
470
      } else {
UNCOV
471
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
UNCOV
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,370,614,772✔
478
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
649,183,677✔
479
    } else {
480
      pME->pExtSchemas = NULL;
721,431,095✔
481
    }
482
  }
483
  if (pME->type == TSDB_SUPER_TABLE) {
1,370,437,165✔
484
    if (!tDecodeIsEnd(pCoder)) {
644,286,113✔
485
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,288,823,704✔
486
    }
487
  }
488

489

490
  tEndDecode(pCoder);
1,370,582,397✔
491
  return 0;
1,370,305,195✔
492
}
493

494
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
1,370,470,872✔
495

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

501
  pDst->nCols = pSrc->nCols;
297,001,034✔
502
  pDst->version = pSrc->version;
296,999,429✔
503
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
296,991,163✔
504
  if (pDst->pSchema == NULL) {
296,918,022✔
505
    return terrno;
×
506
  }
507
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
296,950,309✔
508
  return TSDB_CODE_SUCCESS;
297,016,098✔
509
}
510

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

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

539
  return TSDB_CODE_SUCCESS;
89,326✔
540
}
541

542
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
296,978,982✔
543
  if (pSchema) {
296,978,982✔
544
    taosMemoryFreeClear(pSchema->pSchema);
296,990,463✔
545
  }
546
}
296,974,901✔
547

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

561
void metaCloneEntryFree(SMetaEntry **ppEntry) {
169,809,033✔
562
  if (ppEntry == NULL || *ppEntry == NULL) {
169,809,033✔
563
    return;
77,202✔
564
  }
565

566
  taosMemoryFreeClear((*ppEntry)->name);
169,735,333✔
567

568
  if ((*ppEntry)->type < 0) {
169,753,261✔
569
    taosMemoryFreeClear(*ppEntry);
×
570
    return;
×
571
  }
572

573
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
169,705,403✔
574
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
144,233,411✔
575
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
144,218,838✔
576
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
144,223,618✔
577
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
134,746✔
578
    }
579
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
25,498,622✔
580
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
16,976,182✔
581
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
16,975,374✔
582
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
8,523,964✔
583
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
8,523,964✔
584
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
8,523,964✔
585
  } else {
586
    return;
×
587
  }
588
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
169,721,448✔
589
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
169,701,926✔
590
  metaCloneColRefFree(&(*ppEntry)->colRef);
169,716,002✔
591

592
  taosMemoryFreeClear(*ppEntry);
169,686,919✔
593
  return;
169,725,495✔
594
}
595

596
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
169,726,190✔
597
  int32_t code = TSDB_CODE_SUCCESS;
169,726,190✔
598

599
  if (NULL == pEntry || NULL == ppEntry) {
169,726,190✔
600
    return TSDB_CODE_INVALID_PARA;
×
601
  }
602

603
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
169,742,268✔
604
  if (NULL == *ppEntry) {
169,688,032✔
605
    return terrno;
×
606
  }
607

608
  (*ppEntry)->version = pEntry->version;
169,700,953✔
609
  (*ppEntry)->type = pEntry->type;
169,709,368✔
610
  (*ppEntry)->uid = pEntry->uid;
169,729,576✔
611

612
  if (pEntry->type < 0) {
169,740,170✔
613
    return TSDB_CODE_SUCCESS;
×
614
  }
615

616
  if (pEntry->name) {
169,739,774✔
617
    (*ppEntry)->name = tstrdup(pEntry->name);
169,729,796✔
618
    if (NULL == (*ppEntry)->name) {
169,733,868✔
619
      code = terrno;
×
620
      metaCloneEntryFree(ppEntry);
×
621
      return code;
×
622
    }
623
  }
624

625
  if (pEntry->type == TSDB_SUPER_TABLE) {
169,725,512✔
626
    (*ppEntry)->flags = pEntry->flags;
144,218,396✔
627

628
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
144,227,723✔
629
    if (code) {
144,247,335✔
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633

634
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
144,247,335✔
635
    if (code) {
144,249,310✔
636
      metaCloneEntryFree(ppEntry);
×
637
      return code;
×
638
    }
639
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
144,249,310✔
640
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
89,326✔
641
      if (code) {
89,326✔
642
        metaCloneEntryFree(ppEntry);
×
643
        return code;
×
644
      }
645
    }
646
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
144,249,257✔
647
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
25,500,146✔
648
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
16,976,182✔
649
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
16,976,182✔
650
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
16,976,182✔
651

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

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

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

685
    // comment
686
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
8,523,964✔
687
    if (pEntry->ntbEntry.commentLen > 0) {
8,523,964✔
688
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
4,317✔
689
      if (NULL == (*ppEntry)->ntbEntry.comment) {
4,317✔
690
        code = terrno;
×
691
        metaCloneEntryFree(ppEntry);
×
692
        return code;
×
693
      }
694
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
4,317✔
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) {
169,743,687✔
701
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
712,717✔
702
    if (code) {
684,479✔
703
      metaCloneEntryFree(ppEntry);
×
704
      return code;
×
705
    }
706
  } else {
707
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
169,040,975✔
708
    if (code) {
169,039,893✔
709
      metaCloneEntryFree(ppEntry);
×
710
      return code;
×
711
    }
712
  }
713
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
169,724,372✔
714
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
11,358,235✔
715
    if (!(*ppEntry)->pExtSchemas) {
11,362,815✔
716
      code = terrno;
×
717
      metaCloneEntryFree(ppEntry);
×
718
      return code;
×
719
    }
720
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
11,356,129✔
721
  }
722

723
  return code;
169,737,052✔
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