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

taosdata / TDengine / #4863

26 Nov 2025 05:46AM UTC coverage: 64.539% (+0.2%) from 64.294%
#4863

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

767 of 945 new or added lines in 33 files covered. (81.16%)

701 existing lines in 109 files now uncovered.

158204 of 245129 relevant lines covered (64.54%)

111903199.95 hits per line

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

81.13
/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) {
697,188,817✔
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;
36,210,977✔
26
    }
27
  }
28
  return false;
659,998,529✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
178,633,971✔
32
  if (pME->pExtSchemas) {
178,633,971✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
13,633,320✔
34
    bool                  hasTypeMods = false;
13,633,320✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
13,633,320✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
13,497,222✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
129,500✔
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
129,500✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
13,630,062✔
43

44
    for (int32_t i = 0; i < pSchWrapper->nCols && hasTypeMods; ++i) {
1,884,220,399✔
45
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->pExtSchemas[i].typeMod));
2,147,483,647✔
46
    }
47
  }
48
  return 0;
178,622,706✔
49
}
50

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

57
  for (int32_t i = 0; i < pw->nCols; i++) {
12,456,086✔
58
    SColRef *p = &pw->pColRef[i];
11,186,482✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
22,372,964✔
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
22,372,964✔
61
    if (p->hasRef) {
11,186,482✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
12,490,056✔
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
12,490,056✔
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
12,490,056✔
65
    }
66
  }
67
  return 0;
1,269,604✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
669,699,926✔
71
  bool hasExtSchema = false;
669,699,926✔
72
  SSchemaWrapper* pSchWrapper = NULL;
669,699,926✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
669,699,926✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
664,946,369✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
4,766,768✔
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
4,766,768✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
669,750,736✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
669,782,955✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
51,028,222✔
84
    if (pME->pExtSchemas == NULL) {
25,514,478✔
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;
644,265,597✔
93
  }
94

95
  return 0;
669,867,624✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
13,884,707✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
13,884,707✔
100
  bool                  hasTypeMods = false;
13,884,707✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
13,884,707✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
12,292,946✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
1,596,725✔
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
1,599,165✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
13,891,707✔
109

110
  if (hasTypeMods) {
13,882,273✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
50,085✔
112
    if (ret != NULL) {
50,085✔
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
50,085✔
114
    }
115
    return ret;
50,085✔
116
  }
117
  return NULL;
13,832,188✔
118
}
119

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

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

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

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
54,720✔
151
  int32_t    i = 0, j = 0;
54,720✔
152
  for (i = 0; i < nCols; ++i) {
421,040✔
153
    while (j < pParam->nFuncs) {
665,760✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
639,920✔
155
        pFuncIds[i] = pParam->funcIds[j];
299,440✔
156
        break;
299,440✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
340,480✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
66,880✔
160
        break;
66,880✔
161
      }
162
      ++j;
273,600✔
163
    }
164
    if (j >= pParam->nFuncs) {
392,160✔
165
      for (; i < nCols; ++i) {
51,680✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
25,840✔
167
      }
168
      break;
25,840✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
54,720✔
172

173
  return 0;
54,720✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
17,163,698✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
17,163,698✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
34,325,554✔
179
  if (pWrapper->nCols == 0) {
17,162,216✔
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
34,326,874✔
184
  uDebug("decode cols:%d", pWrapper->nCols);
17,163,176✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
34,327,680✔
186
  if (pWrapper->pColRef == NULL) {
17,164,504✔
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
241,026,600✔
191
    SColRef *p = &pWrapper->pColRef[i];
223,858,714✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
447,736,342✔
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
447,745,501✔
194
    if (p->hasRef) {
223,874,373✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
133,099,324✔
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
133,098,200✔
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
133,097,326✔
198
    }
199
  }
200
  return 0;
17,164,906✔
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,412✔
220
  if (pSrc->nCols > 0) {
688,412✔
221
    pDst->nCols = pSrc->nCols;
688,412✔
222
    pDst->version = pSrc->version;
688,412✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
688,412✔
224
    if (NULL == pDst->pColRef) {
688,412✔
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
688,412✔
228
  }
229
  return 0;
688,412✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
43,021,699✔
233
  int32_t code = 0;
43,021,699✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
86,044,860✔
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
86,067,517✔
236
  uTrace("encode cols:%d", pw->nCols);
43,044,356✔
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,072,052✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
42,847,776✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
42,847,776✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
42,858,262✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
817,792,626✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
817,792,626✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
1,635,714,925✔
252
  if (pWrapper->nCols == 0) {
817,786,356✔
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
1,635,715,020✔
257
  uDebug("dencode cols:%d", pWrapper->nCols);
817,936,693✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
1,635,637,062✔
259
  if (pWrapper->pColCmpr == NULL) {
817,887,616✔
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;
818,051,851✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
241,202✔
273

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

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

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

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
170,697,166✔
294
  if (pSrc->nCols > 0) {
170,697,166✔
295
    pDst->nCols = pSrc->nCols;
153,877,205✔
296
    pDst->version = pSrc->version;
153,879,587✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
153,887,210✔
298
    if (NULL == pDst->pColCmpr) {
153,862,422✔
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
153,861,514✔
302
  }
303
  return 0;
170,705,430✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
171,370,814✔
307
  if (pColRef) {
171,370,814✔
308
    taosMemoryFreeClear(pColRef->pColRef);
171,382,671✔
309
  }
310
}
171,385,956✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
171,364,132✔
313
  if (pCmpr) {
171,364,132✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
171,378,190✔
315
  }
316
}
171,371,765✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
182,533,156✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
182,533,156✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
365,101,782✔
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
365,053,556✔
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
365,027,022✔
323

324
  if (pME->type > 0) {
182,528,311✔
325
    if (pME->name == NULL) {
178,674,952✔
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
357,353,020✔
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
178,687,190✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
62,328,178✔
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
62,367,630✔
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
62,397,262✔
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
31,174,020✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
154,280✔
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
147,475,804✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
269,847,574✔
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
269,860,826✔
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
269,868,831✔
342
      if (pME->ctbEntry.commentLen > 0) {
134,933,940✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
14,944✔
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
269,859,274✔
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
134,930,145✔
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
12,555,342✔
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
25,110,684✔
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
25,110,684✔
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
25,110,684✔
351
      if (pME->ntbEntry.commentLen > 0) {
12,555,342✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
13,888✔
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
25,110,684✔
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
25,110,684✔
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) {
178,649,297✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,345,049✔
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
177,354,969✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
31,159,802✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
146,251,234✔
368
        if (pME->colCmpr.nCols != 0) {
11,891,416✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
11,650,214✔
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
241,202✔
372
          SColCmprWrapper colCmprs = {0};
241,202✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
241,202✔
374
          if (code != 0) {
241,202✔
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
241,202✔
379
          taosMemoryFree(colCmprs.pColCmpr);
241,202✔
380
          TAOS_CHECK_RETURN(code);
241,202✔
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
178,668,776✔
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
182,504,835✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
62,361,943✔
388
  }
389

390
  tEndEncode(pCoder);
182,521,692✔
391
  return 0;
182,475,146✔
392
}
393

394
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
1,420,767,602✔
395
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
1,420,767,602✔
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,421,174,931✔
401
    tEndDecode(pCoder);
105,764✔
402
    return 0;
105,764✔
403
  }
404

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

408
    if (pME->type == TSDB_SUPER_TABLE) {
1,421,016,615✔
409
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,330,173,159✔
410
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,330,173,209✔
411
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,330,042,503✔
412
      if (TABLE_IS_ROLLUP(pME->flags)) {
664,999,624✔
413
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
580,640✔
414
      }
415
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
755,695,741✔
416
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
1,197,402,844✔
417
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
1,197,527,298✔
418
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
1,197,393,040✔
419
      if (pME->ctbEntry.commentLen > 0) {
598,632,045✔
420
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
20,946✔
421
      }
422
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
1,197,371,421✔
423
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
598,767,106✔
424
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
157,157,773✔
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
314,340,497✔
426
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
314,362,858✔
427
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
314,348,145✔
428
      if (pME->ntbEntry.commentLen > 0) {
157,166,460✔
429
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
20,514✔
430
      }
431
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
314,319,770✔
432
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
314,352,028✔
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,420,857,548✔
444
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
665,069,690✔
445
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
665,017,560✔
446

447
        if (pME->colCmpr.nCols == 0) {
665,006,555✔
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) {
755,635,930✔
455
      if (!tDecodeIsEnd(pCoder)) {
152,885,702✔
456
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
152,892,984✔
457
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
152,893,758✔
458
        if (pME->colCmpr.nCols == 0) {
152,879,404✔
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);
64✔
463
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
64✔
464
      }
465
      TABLE_SET_COL_COMPRESSED(pME->flags);
152,889,099✔
466
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
602,768,991✔
467
      if (!tDecodeIsEnd(pCoder)) {
17,163,540✔
468
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
17,164,148✔
469
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
17,165,664✔
470
      } else {
471
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
472
        if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
164✔
473
           TAOS_CHECK_RETURN(metatInitDefaultSColRefWrapper(pCoder, &pME->colRef, &pME->ntbEntry.schemaRow));
×
474
        }
475
      }
476
    }
477
    if (!tDecodeIsEnd(pCoder)) {
1,420,814,286✔
478
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
669,785,880✔
479
    } else {
480
      pME->pExtSchemas = NULL;
751,028,406✔
481
    }
482
  }
483
  if (pME->type == TSDB_SUPER_TABLE) {
1,420,778,303✔
484
    if (!tDecodeIsEnd(pCoder)) {
664,975,071✔
485
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,329,969,066✔
486
    }
487
  }
488

489

490
  tEndDecode(pCoder);
1,420,754,120✔
491
  return 0;
1,420,790,969✔
492
}
493

494
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
1,420,697,707✔
495

496
static int32_t metaCloneSchema(const SSchemaWrapper *pSrc, SSchemaWrapper *pDst) {
300,080,914✔
497
  if (pSrc == NULL || pDst == NULL) {
300,080,914✔
UNCOV
498
    return TSDB_CODE_INVALID_PARA;
×
499
  }
500

501
  pDst->nCols = pSrc->nCols;
300,112,304✔
502
  pDst->version = pSrc->version;
300,107,012✔
503
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
300,093,820✔
504
  if (pDst->pSchema == NULL) {
300,032,119✔
505
    return terrno;
×
506
  }
507
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
300,047,768✔
508
  return TSDB_CODE_SUCCESS;
300,107,995✔
509
}
510

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

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

539
  return TSDB_CODE_SUCCESS;
89,680✔
540
}
541

542
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
300,062,431✔
543
  if (pSchema) {
300,062,431✔
544
    taosMemoryFreeClear(pSchema->pSchema);
300,079,385✔
545
  }
546
}
300,076,701✔
547

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

561
void metaCloneEntryFree(SMetaEntry **ppEntry) {
171,449,338✔
562
  if (ppEntry == NULL || *ppEntry == NULL) {
171,449,338✔
563
    return;
77,642✔
564
  }
565

566
  taosMemoryFreeClear((*ppEntry)->name);
171,381,835✔
567

568
  if ((*ppEntry)->type < 0) {
171,382,957✔
569
    taosMemoryFreeClear(*ppEntry);
×
570
    return;
×
571
  }
572

573
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
171,367,651✔
574
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
145,756,288✔
575
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
145,744,997✔
576
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
145,744,551✔
577
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
133,760✔
578
    }
579
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
25,624,712✔
580
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
17,047,446✔
581
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
17,048,117✔
582
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
8,577,901✔
583
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
8,577,901✔
584
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
8,577,901✔
585
  } else {
586
    return;
×
587
  }
588
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
171,359,506✔
589
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
171,364,092✔
590
  metaCloneColRefFree(&(*ppEntry)->colRef);
171,351,289✔
591

592
  taosMemoryFreeClear(*ppEntry);
171,362,903✔
593
  return;
171,368,862✔
594
}
595

596
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
171,359,655✔
597
  int32_t code = TSDB_CODE_SUCCESS;
171,359,655✔
598

599
  if (NULL == pEntry || NULL == ppEntry) {
171,359,655✔
600
    return TSDB_CODE_INVALID_PARA;
×
601
  }
602

603
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
171,390,743✔
604
  if (NULL == *ppEntry) {
171,341,482✔
605
    return terrno;
×
606
  }
607

608
  (*ppEntry)->version = pEntry->version;
171,359,599✔
609
  (*ppEntry)->type = pEntry->type;
171,370,357✔
610
  (*ppEntry)->uid = pEntry->uid;
171,361,211✔
611

612
  if (pEntry->type < 0) {
171,383,972✔
613
    return TSDB_CODE_SUCCESS;
×
614
  }
615

616
  if (pEntry->name) {
171,367,213✔
617
    (*ppEntry)->name = tstrdup(pEntry->name);
171,383,787✔
618
    if (NULL == (*ppEntry)->name) {
171,381,009✔
619
      code = terrno;
×
620
      metaCloneEntryFree(ppEntry);
×
621
      return code;
×
622
    }
623
  }
624

625
  if (pEntry->type == TSDB_SUPER_TABLE) {
171,373,218✔
626
    (*ppEntry)->flags = pEntry->flags;
145,761,754✔
627

628
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
145,743,719✔
629
    if (code) {
145,761,586✔
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633

634
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
145,761,586✔
635
    if (code) {
145,772,986✔
636
      metaCloneEntryFree(ppEntry);
×
637
      return code;
×
638
    }
639
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
145,772,986✔
640
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
89,680✔
641
      if (code) {
89,680✔
642
        metaCloneEntryFree(ppEntry);
×
643
        return code;
×
644
      }
645
    }
646
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
145,771,787✔
647
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
25,626,018✔
648
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
17,048,117✔
649
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
17,048,117✔
650
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
17,048,117✔
651

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

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

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

685
    // comment
686
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
8,577,901✔
687
    if (pEntry->ntbEntry.commentLen > 0) {
8,577,901✔
688
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
4,361✔
689
      if (NULL == (*ppEntry)->ntbEntry.comment) {
4,361✔
690
        code = terrno;
×
691
        metaCloneEntryFree(ppEntry);
×
692
        return code;
×
693
      }
694
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
4,361✔
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) {
171,382,941✔
701
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
692,450✔
702
    if (code) {
688,412✔
703
      metaCloneEntryFree(ppEntry);
×
704
      return code;
×
705
    }
706
  } else {
707
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
170,695,609✔
708
    if (code) {
170,702,984✔
709
      metaCloneEntryFree(ppEntry);
×
710
      return code;
×
711
    }
712
  }
713
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
171,391,396✔
714
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
11,467,768✔
715
    if (!(*ppEntry)->pExtSchemas) {
11,464,522✔
716
      code = terrno;
×
717
      metaCloneEntryFree(ppEntry);
×
718
      return code;
×
719
    }
720
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
11,463,204✔
721
  }
722

723
  return code;
171,375,329✔
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