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

taosdata / TDengine / #4851

14 Nov 2025 08:06AM UTC coverage: 63.754% (+0.03%) from 63.728%
#4851

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

354 of 675 new or added lines in 18 files covered. (52.44%)

3145 existing lines in 113 files now uncovered.

149128 of 233910 relevant lines covered (63.75%)

117183401.67 hits per line

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

80.5
/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) {
702,641,661✔
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;
40,910,709✔
26
    }
27
  }
28
  return false;
661,540,704✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
181,333,680✔
32
  if (pME->pExtSchemas) {
181,333,680✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
14,517,985✔
34
    bool                  hasTypeMods = false;
14,517,985✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
14,517,985✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
14,361,291✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
153,332✔
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
153,332✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
14,519,239✔
43

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

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

57
  for (int32_t i = 0; i < pw->nCols; i++) {
12,501,090✔
58
    SColRef *p = &pw->pColRef[i];
11,247,438✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
22,494,876✔
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
22,494,876✔
61
    if (p->hasRef) {
11,247,438✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
12,506,776✔
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
12,506,776✔
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
12,506,776✔
65
    }
66
  }
67
  return 0;
1,253,652✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
672,655,671✔
71
  bool hasExtSchema = false;
672,655,671✔
72
  SSchemaWrapper* pSchWrapper = NULL;
672,655,671✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
672,655,671✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
665,417,622✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
7,275,542✔
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
7,275,542✔
77
  } else {
UNCOV
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
672,696,793✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
672,716,082✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
58,617,401✔
84
    if (pME->pExtSchemas == NULL) {
29,302,127✔
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;
643,388,817✔
93
  }
94

95
  return 0;
672,699,610✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
15,494,393✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
15,494,393✔
100
  bool                  hasTypeMods = false;
15,494,393✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
15,494,393✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
13,251,045✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
2,246,658✔
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
2,248,171✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
15,499,256✔
109

110
  if (hasTypeMods) {
15,494,009✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
50,853✔
112
    if (ret != NULL) {
50,853✔
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
50,853✔
114
    }
115
    return ret;
50,853✔
116
  }
117
  return NULL;
15,443,156✔
118
}
119

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

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

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

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
55,584✔
151
  int32_t    i = 0, j = 0;
55,584✔
152
  for (i = 0; i < nCols; ++i) {
427,688✔
153
    while (j < pParam->nFuncs) {
676,272✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
650,024✔
155
        pFuncIds[i] = pParam->funcIds[j];
304,168✔
156
        break;
304,168✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
345,856✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
67,936✔
160
        break;
67,936✔
161
      }
162
      ++j;
277,920✔
163
    }
164
    if (j >= pParam->nFuncs) {
398,352✔
165
      for (; i < nCols; ++i) {
52,496✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
26,248✔
167
      }
168
      break;
26,248✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
55,584✔
172

173
  return 0;
55,584✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
14,321,977✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
14,321,977✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
28,644,466✔
179
  if (pWrapper->nCols == 0) {
14,321,873✔
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
28,644,570✔
184
  uDebug("decode cols:%d", pWrapper->nCols);
14,322,593✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
28,645,186✔
186
  if (pWrapper->pColRef == NULL) {
14,321,939✔
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
229,491,205✔
191
    SColRef *p = &pWrapper->pColRef[i];
215,158,444✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
430,345,337✔
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
430,353,747✔
194
    if (p->hasRef) {
215,182,466✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
125,944,346✔
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
125,939,988✔
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
125,940,386✔
198
    }
199
  }
200
  return 0;
14,322,593✔
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) {
688,481✔
220
  if (pSrc->nCols > 0) {
688,481✔
221
    pDst->nCols = pSrc->nCols;
688,481✔
222
    pDst->version = pSrc->version;
688,481✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
688,481✔
224
    if (NULL == pDst->pColRef) {
688,481✔
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
688,481✔
228
  }
229
  return 0;
688,481✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
43,665,753✔
233
  int32_t code = 0;
43,665,753✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
87,352,163✔
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
87,377,361✔
236
  uTrace("encode cols:%d", pw->nCols);
43,690,951✔
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;
43,784,293✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
43,598,812✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
43,598,812✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
43,622,676✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
826,778,098✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
826,778,098✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
1,653,609,978✔
252
  if (pWrapper->nCols == 0) {
826,698,955✔
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
1,653,399,933✔
257
  uDebug("dencode cols:%d", pWrapper->nCols);
826,774,344✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
1,653,366,778✔
259
  if (pWrapper->pColCmpr == NULL) {
826,753,527✔
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;
827,062,836✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
163,342✔
273

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

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

284
  for (int32_t i = 0; i < pCmpr->nCols; i++) {
745,762✔
285
    SColCmpr *pColCmpr = &pCmpr->pColCmpr[i];
582,420✔
286
    SSchema  *pColSchema = &pSchema->pSchema[i];
582,420✔
287
    pColCmpr->id = pColSchema->colId;
582,420✔
288
    pColCmpr->alg = createDefaultColCmprByType(pColSchema->type);
582,420✔
289
  }
290
  return 0;
163,342✔
291
}
292

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
173,945,271✔
294
  if (pSrc->nCols > 0) {
173,945,271✔
295
    pDst->nCols = pSrc->nCols;
157,081,579✔
296
    pDst->version = pSrc->version;
157,085,674✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
157,070,403✔
298
    if (NULL == pDst->pColCmpr) {
157,065,834✔
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
157,064,595✔
302
  }
303
  return 0;
173,952,006✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
174,612,132✔
307
  if (pColRef) {
174,612,132✔
308
    taosMemoryFreeClear(pColRef->pColRef);
174,632,705✔
309
  }
310
}
174,622,455✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
174,609,992✔
313
  if (pCmpr) {
174,609,992✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
174,622,114✔
315
  }
316
}
174,641,657✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
185,700,655✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
185,700,655✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
371,496,398✔
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
371,472,197✔
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
371,443,470✔
323

324
  if (pME->type > 0) {
185,732,979✔
325
    if (pME->name == NULL) {
181,360,278✔
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
362,730,159✔
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
181,368,679✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
62,936,166✔
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
63,013,577✔
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
63,075,928✔
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
31,496,187✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
156,716✔
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
149,840,682✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
273,870,846✔
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
273,880,960✔
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
273,879,032✔
342
      if (pME->ctbEntry.commentLen > 0) {
136,933,887✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
15,324✔
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
273,868,020✔
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
136,935,439✔
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
12,915,358✔
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
25,830,716✔
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
25,830,716✔
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
25,830,000✔
351
      if (pME->ntbEntry.commentLen > 0) {
12,914,642✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
14,540✔
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
25,830,000✔
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
25,830,716✔
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,353,322✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,328,012✔
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
180,081,077✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
31,512,091✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
148,554,668✔
368
        if (pME->colCmpr.nCols != 0) {
12,247,186✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
12,083,844✔
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
163,342✔
372
          SColCmprWrapper colCmprs = {0};
163,342✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
163,342✔
374
          if (code != 0) {
163,342✔
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
163,342✔
379
          taosMemoryFree(colCmprs.pColCmpr);
163,342✔
380
          TAOS_CHECK_RETURN(code);
163,342✔
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
181,368,657✔
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
185,716,620✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
62,903,002✔
388
  }
389

390
  tEndEncode(pCoder);
185,638,630✔
391
  return 0;
185,617,639✔
392
}
393

394
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
1,434,815,557✔
395
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
1,434,815,557✔
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,435,210,403✔
401
    tEndDecode(pCoder);
159,440✔
402
    return 0;
159,440✔
403
  }
404

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

408
    if (pME->type == TSDB_SUPER_TABLE) {
1,434,811,162✔
409
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,330,931,760✔
410
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,331,104,929✔
411
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,330,966,543✔
412
      if (TABLE_IS_ROLLUP(pME->flags)) {
665,389,140✔
413
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
590,580✔
414
      }
415
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
769,232,224✔
416
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
1,207,274,766✔
417
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
1,207,378,184✔
418
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
1,207,294,514✔
419
      if (pME->ctbEntry.commentLen > 0) {
603,609,994✔
420
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
21,718✔
421
      }
422
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
1,207,225,200✔
423
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
603,677,225✔
424
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
165,730,970✔
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
331,511,758✔
426
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
331,538,033✔
427
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
331,512,347✔
428
      if (pME->ntbEntry.commentLen > 0) {
165,740,267✔
429
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
21,844✔
430
      }
431
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
331,470,140✔
432
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
331,511,936✔
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,434,785,321✔
444
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
665,569,270✔
445
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
665,537,948✔
446

447
        if (pME->colCmpr.nCols == 0) {
665,539,170✔
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) {
769,183,189✔
455
      if (!tDecodeIsEnd(pCoder)) {
161,410,077✔
456
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
161,416,820✔
457
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
161,417,215✔
458
        if (pME->colCmpr.nCols == 0) {
161,420,658✔
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);
161,408,918✔
466
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
607,791,438✔
467
      if (!tDecodeIsEnd(pCoder)) {
14,322,593✔
468
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
14,322,593✔
469
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
14,322,593✔
470
      } else {
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,434,821,751✔
478
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
672,721,287✔
479
    } else {
480
      pME->pExtSchemas = NULL;
762,100,464✔
481
    }
482
  }
483
  if (pME->type == TSDB_SUPER_TABLE) {
1,434,499,623✔
484
    if (!tDecodeIsEnd(pCoder)) {
665,366,088✔
485
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,330,862,010✔
486
    }
487
  }
488

489

490
  tEndDecode(pCoder);
1,434,815,005✔
491
  return 0;
1,434,761,608✔
492
}
493

494
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
1,434,749,458✔
495

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

501
  pDst->nCols = pSrc->nCols;
306,446,841✔
502
  pDst->version = pSrc->version;
306,442,097✔
503
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
306,440,751✔
504
  if (pDst->pSchema == NULL) {
306,370,431✔
505
    return terrno;
×
506
  }
507
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
306,393,564✔
508
  return TSDB_CODE_SUCCESS;
306,468,671✔
509
}
510

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

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

539
  return TSDB_CODE_SUCCESS;
88,008✔
540
}
541

542
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
306,416,745✔
543
  if (pSchema) {
306,416,745✔
544
    taosMemoryFreeClear(pSchema->pSchema);
306,429,505✔
545
  }
546
}
306,432,231✔
547

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

561
void metaCloneEntryFree(SMetaEntry **ppEntry) {
174,691,347✔
562
  if (ppEntry == NULL || *ppEntry == NULL) {
174,691,347✔
563
    return;
77,617✔
564
  }
565

566
  taosMemoryFreeClear((*ppEntry)->name);
174,618,594✔
567

568
  if ((*ppEntry)->type < 0) {
174,656,850✔
569
    taosMemoryFreeClear(*ppEntry);
×
570
    return;
×
571
  }
572

573
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
174,594,649✔
574
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
148,877,369✔
575
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
148,885,958✔
576
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
148,884,474✔
577
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
137,416✔
578
    }
579
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
25,743,742✔
580
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
17,096,595✔
581
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
17,095,508✔
582
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
8,648,389✔
583
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
8,648,389✔
584
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
8,648,389✔
585
  } else {
586
    return;
×
587
  }
588
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
174,632,777✔
589
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
174,627,060✔
590
  metaCloneColRefFree(&(*ppEntry)->colRef);
174,633,949✔
591

592
  taosMemoryFreeClear(*ppEntry);
174,604,982✔
593
  return;
174,627,465✔
594
}
595

596
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
174,627,354✔
597
  int32_t code = TSDB_CODE_SUCCESS;
174,627,354✔
598

599
  if (NULL == pEntry || NULL == ppEntry) {
174,627,354✔
600
    return TSDB_CODE_INVALID_PARA;
×
601
  }
602

603
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
174,654,130✔
604
  if (NULL == *ppEntry) {
174,580,731✔
605
    return terrno;
×
606
  }
607

608
  (*ppEntry)->version = pEntry->version;
174,609,953✔
609
  (*ppEntry)->type = pEntry->type;
174,605,544✔
610
  (*ppEntry)->uid = pEntry->uid;
174,613,200✔
611

612
  if (pEntry->type < 0) {
174,634,079✔
613
    return TSDB_CODE_SUCCESS;
×
614
  }
615

616
  if (pEntry->name) {
174,629,469✔
617
    (*ppEntry)->name = tstrdup(pEntry->name);
174,622,616✔
618
    if (NULL == (*ppEntry)->name) {
174,641,035✔
619
      code = terrno;
×
620
      metaCloneEntryFree(ppEntry);
×
621
      return code;
×
622
    }
623
  }
624

625
  if (pEntry->type == TSDB_SUPER_TABLE) {
174,647,592✔
626
    (*ppEntry)->flags = pEntry->flags;
148,888,695✔
627

628
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
148,871,006✔
629
    if (code) {
148,902,373✔
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633

634
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
148,902,373✔
635
    if (code) {
148,915,729✔
636
      metaCloneEntryFree(ppEntry);
×
637
      return code;
×
638
    }
639
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
148,915,729✔
640
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
91,096✔
641
      if (code) {
88,008✔
642
        metaCloneEntryFree(ppEntry);
×
643
        return code;
×
644
      }
645
    }
646
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
148,906,196✔
647
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
25,744,984✔
648
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
17,096,595✔
649
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
17,097,253✔
650
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
17,096,595✔
651

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

664
    // tags
665
    STag *pTags = (STag *)pEntry->ctbEntry.pTags;
17,097,253✔
666
    (*ppEntry)->ctbEntry.pTags = taosMemoryCalloc(1, pTags->len);
17,096,595✔
667
    if (NULL == (*ppEntry)->ctbEntry.pTags) {
17,097,253✔
668
      code = terrno;
×
669
      metaCloneEntryFree(ppEntry);
×
670
      return code;
×
671
    }
672
    memcpy((*ppEntry)->ctbEntry.pTags, pEntry->ctbEntry.pTags, pTags->len);
17,095,937✔
673
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
8,648,389✔
674
    (*ppEntry)->ntbEntry.btime = pEntry->ntbEntry.btime;
8,648,389✔
675
    (*ppEntry)->ntbEntry.ttlDays = pEntry->ntbEntry.ttlDays;
8,648,389✔
676
    (*ppEntry)->ntbEntry.ncid = pEntry->ntbEntry.ncid;
8,648,389✔
677

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

685
    // comment
686
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
8,648,389✔
687
    if (pEntry->ntbEntry.commentLen > 0) {
8,648,389✔
688
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
4,562✔
689
      if (NULL == (*ppEntry)->ntbEntry.comment) {
4,562✔
690
        code = terrno;
×
691
        metaCloneEntryFree(ppEntry);
×
692
        return code;
×
693
      }
694
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
4,562✔
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) {
174,631,431✔
701
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
717,408✔
702
    if (code) {
688,481✔
703
      metaCloneEntryFree(ppEntry);
×
704
      return code;
×
705
    }
706
  } else {
707
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
173,940,320✔
708
    if (code) {
173,943,981✔
709
      metaCloneEntryFree(ppEntry);
×
710
      return code;
×
711
    }
712
  }
713
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
174,632,462✔
714
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
12,402,000✔
715
    if (!(*ppEntry)->pExtSchemas) {
12,395,887✔
716
      code = terrno;
×
717
      metaCloneEntryFree(ppEntry);
×
718
      return code;
×
719
    }
720
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
12,393,829✔
721
  }
722

723
  return code;
174,625,495✔
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