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

taosdata / TDengine / #4914

06 Jan 2026 01:30AM UTC coverage: 64.876% (-0.008%) from 64.884%
#4914

push

travis-ci

web-flow
merge: from main to 3.0 branch #34167

180 of 319 new or added lines in 14 files covered. (56.43%)

3475 existing lines in 124 files now uncovered.

194993 of 300563 relevant lines covered (64.88%)

116239151.85 hits per line

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

81.11
/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) {
1,059,465,066✔
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;
29,773,040✔
26
    }
27
  }
28
  return false;
1,029,478,393✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
202,821,051✔
32
  if (pME->pExtSchemas) {
202,821,051✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
11,730,219✔
34
    bool                  hasTypeMods = false;
11,730,219✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
11,730,219✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
11,616,018✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
110,604✔
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
110,604✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
11,722,585✔
43

44
    for (int32_t i = 0; i < pSchWrapper->nCols && hasTypeMods; ++i) {
1,210,174,711✔
45
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->pExtSchemas[i].typeMod));
2,147,483,647✔
46
    }
47
  }
48
  return 0;
202,813,471✔
49
}
50

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

57
  for (int32_t i = 0; i < pw->nCols; i++) {
314,055,524✔
58
    SColRef *p = &pw->pColRef[i];
312,976,932✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
625,953,864✔
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
625,953,864✔
61
    if (p->hasRef) {
312,976,932✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
374,536,000✔
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
374,536,000✔
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
374,536,000✔
65
    }
66
  }
67
  return 0;
1,078,592✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
1,005,714,812✔
71
  bool hasExtSchema = false;
1,005,714,812✔
72
  SSchemaWrapper* pSchWrapper = NULL;
1,005,714,812✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
1,005,714,812✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
792,269,996✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
213,509,452✔
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
213,579,163✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
1,005,956,261✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
1,005,949,039✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
41,934,043✔
84
    if (pME->pExtSchemas == NULL) {
20,966,219✔
85
      return terrno;
×
86
    }
87

88
    for (int32_t i = 0; i < pSchWrapper->nCols && hasExtSchema; i++) {
1,482,383,142✔
89
      TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pME->pExtSchemas[i].typeMod));
2,147,483,647✔
90
    }
91
  } else {
92
    pME->pExtSchemas = NULL;
984,982,242✔
93
  }
94

95
  return 0;
1,006,153,038✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
42,020,222✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
42,020,222✔
100
  bool                  hasTypeMods = false;
42,020,222✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
42,020,222✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
30,359,701✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
11,674,632✔
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
11,678,720✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
42,042,097✔
109

110
  if (hasTypeMods) {
42,021,951✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
38,529✔
112
    if (ret != NULL) {
38,529✔
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
38,529✔
114
    }
115
    return ret;
38,529✔
116
  }
117
  return NULL;
41,983,422✔
118
}
119

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

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

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

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
26,316✔
151
  int32_t    i = 0, j = 0;
26,316✔
152
  for (i = 0; i < nCols; ++i) {
202,487✔
153
    while (j < pParam->nFuncs) {
320,178✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
307,751✔
155
        pFuncIds[i] = pParam->funcIds[j];
144,007✔
156
        break;
144,007✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
163,744✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
32,164✔
160
        break;
32,164✔
161
      }
162
      ++j;
131,580✔
163
    }
164
    if (j >= pParam->nFuncs) {
188,598✔
165
      for (; i < nCols; ++i) {
24,854✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
12,427✔
167
      }
168
      break;
12,427✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
26,316✔
172

173
  return 0;
26,316✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
5,384,279✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
5,384,279✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
10,769,587✔
179
  if (pWrapper->nCols == 0) {
5,384,692✔
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
10,768,767✔
184
  uDebug("decode cols:%d", pWrapper->nCols);
5,384,691✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
10,768,766✔
186
  if (pWrapper->pColRef == NULL) {
5,384,075✔
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
265,928,612✔
191
    SColRef *p = &pWrapper->pColRef[i];
260,544,731✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
521,129,095✔
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
521,144,505✔
194
    if (p->hasRef) {
260,573,076✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
149,257,169✔
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
149,255,937✔
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
149,256,553✔
198
    }
199
  }
200
  return 0;
5,385,105✔
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) {
643,155✔
220
  if (pSrc->nCols > 0) {
643,155✔
221
    pDst->nCols = pSrc->nCols;
643,155✔
222
    pDst->version = pSrc->version;
643,155✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
643,155✔
224
    if (NULL == pDst->pColRef) {
643,155✔
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
643,155✔
228
  }
229
  return 0;
643,155✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
68,565,109✔
233
  int32_t code = 0;
68,565,109✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
137,147,532✔
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
137,209,697✔
236
  uTrace("encode cols:%d", pw->nCols);
68,627,274✔
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;
68,688,830✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
68,448,271✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
68,448,271✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
68,502,686✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
1,005,869,384✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
1,005,869,384✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
2,011,862,126✔
252
  if (pWrapper->nCols == 0) {
1,005,703,451✔
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
2,011,480,494✔
257
  uDebug("dencode cols:%d", pWrapper->nCols);
1,006,000,904✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
2,011,814,546✔
259
  if (pWrapper->pColCmpr == NULL) {
1,005,412,944✔
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;
1,006,983,458✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
178,230✔
273

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

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

284
  for (int32_t i = 0; i < pCmpr->nCols; i++) {
921,684✔
285
    SColCmpr *pColCmpr = &pCmpr->pColCmpr[i];
743,454✔
286
    SSchema  *pColSchema = &pSchema->pSchema[i];
743,454✔
287
    pColCmpr->id = pColSchema->colId;
743,454✔
288
    pColCmpr->alg = createDefaultColCmprByType(pColSchema->type);
743,454✔
289
  }
290
  return 0;
178,230✔
291
}
292

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
166,968,226✔
294
  if (pSrc->nCols > 0) {
166,968,226✔
295
    pDst->nCols = pSrc->nCols;
150,779,873✔
296
    pDst->version = pSrc->version;
150,781,505✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
150,781,531✔
298
    if (NULL == pDst->pColCmpr) {
150,760,407✔
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
150,762,679✔
302
  }
303
  return 0;
166,980,467✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
167,573,749✔
307
  if (pColRef) {
167,573,749✔
308
    taosMemoryFreeClear(pColRef->pColRef);
167,591,543✔
309
  }
310
}
167,587,357✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
167,576,379✔
313
  if (pCmpr) {
167,576,379✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
167,590,437✔
315
  }
316
}
167,611,472✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
207,317,149✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
207,317,149✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
414,652,479✔
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
414,636,146✔
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
414,580,810✔
323

324
  if (pME->type > 0) {
207,262,586✔
325
    if (pME->name == NULL) {
202,803,674✔
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
405,622,967✔
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
202,820,732✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
47,870,571✔
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
47,964,344✔
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
47,990,449✔
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
23,959,054✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
74,562✔
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
178,845,139✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
267,144,589✔
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
267,152,894✔
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
267,144,226✔
342
      if (pME->ctbEntry.commentLen > 0) {
133,568,483✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
28,116✔
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
267,137,521✔
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
133,567,714✔
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
45,288,430✔
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
90,576,860✔
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
90,576,860✔
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
90,576,860✔
351
      if (pME->ntbEntry.commentLen > 0) {
45,288,430✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
38,164✔
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
90,576,860✔
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
90,576,860✔
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) {
202,819,752✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,140,594✔
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
201,711,255✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
23,991,096✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
177,731,264✔
368
        if (pME->colCmpr.nCols != 0) {
44,687,442✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
44,509,212✔
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
178,230✔
372
          SColCmprWrapper colCmprs = {0};
178,230✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
178,230✔
374
          if (code != 0) {
178,230✔
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
178,230✔
379
          taosMemoryFree(colCmprs.pColCmpr);
178,230✔
380
          TAOS_CHECK_RETURN(code);
178,230✔
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
202,862,603✔
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
207,310,984✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
47,929,153✔
388
    TAOS_CHECK_RETURN(tEncodeI64v(pCoder, pME->stbEntry.ownerId));
47,827,548✔
389
  } else if (pME->type == TSDB_NORMAL_TABLE) {
183,302,174✔
390
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.ownerId));
89,374,884✔
391
  }
392

393
  tEndEncode(pCoder);
207,203,044✔
394
  return 0;
207,217,886✔
395
}
396

397
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
1,593,550,859✔
398
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
1,593,550,859✔
399
  TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->version));
2,147,483,647✔
400
  TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->type));
2,147,483,647✔
401
  TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->uid));
2,147,483,647✔
402

403
  if (headerOnly) {
1,594,417,195✔
404
    tEndDecode(pCoder);
340,008✔
405
    return 0;
340,008✔
406
  }
407

408
  if (pME->type > 0) {
1,594,077,187✔
409
    TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->name));
2,147,483,647✔
410

411
    if (pME->type == TSDB_SUPER_TABLE) {
1,593,471,645✔
412
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,585,010,049✔
413
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,585,642,242✔
414
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,585,167,396✔
415
      if (TABLE_IS_ROLLUP(pME->flags)) {
792,304,311✔
416
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
279,973✔
417
      }
418
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
800,571,363✔
419
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
1,172,386,828✔
420
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
1,172,378,018✔
421
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
1,172,215,329✔
422
      if (pME->ctbEntry.commentLen > 0) {
586,027,201✔
423
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
48,776✔
424
      }
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
1,172,161,295✔
426
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
586,170,048✔
427
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
214,710,196✔
428
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
429,694,240✔
429
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
429,894,150✔
430
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
429,777,006✔
431
      if (pME->ntbEntry.commentLen > 0) {
214,827,794✔
432
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
70,790✔
433
      }
434
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
429,571,406✔
435
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
429,700,050✔
436
    } else if (pME->type == TSDB_TSMA_TABLE) {
×
437
      pME->smaEntry.tsma = tDecoderMalloc(pCoder, sizeof(STSma));
×
438
      if (!pME->smaEntry.tsma) {
×
439
        return terrno;
×
440
      }
441
      TAOS_CHECK_RETURN(tDecodeTSma(pCoder, pME->smaEntry.tsma, true));
×
442
    } else {
443
      metaError("meta/entry: invalide table type: %" PRId8 " decode failed.", pME->type);
×
444
      return TSDB_CODE_INVALID_PARA;
×
445
    }
446
    if (pME->type == TSDB_SUPER_TABLE) {
1,592,888,668✔
447
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
792,762,782✔
448
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
792,336,271✔
449

450
        if (pME->colCmpr.nCols == 0) {
793,015,949✔
451
          TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->stbEntry.schemaRow));
×
452
        }
453
      } else {
454
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->stbEntry.schemaRow));
×
455
        TABLE_SET_COL_COMPRESSED(pME->flags);
×
456
      }
457
    } else if (pME->type == TSDB_NORMAL_TABLE) {
800,585,737✔
458
      if (!tDecodeIsEnd(pCoder)) {
213,778,482✔
459
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
213,803,813✔
460
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
213,804,019✔
461
        if (pME->colCmpr.nCols == 0) {
213,870,784✔
462
          TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
463
        }
464
      } else {
UNCOV
465
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
UNCOV
466
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
467
      }
468
      TABLE_SET_COL_COMPRESSED(pME->flags);
213,813,422✔
469
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
586,980,287✔
470
      if (!tDecodeIsEnd(pCoder)) {
5,384,895✔
471
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
5,384,895✔
472
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
5,384,895✔
473
      } else {
474
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
475
        if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
13✔
476
           TAOS_CHECK_RETURN(metatInitDefaultSColRefWrapper(pCoder, &pME->colRef, &pME->ntbEntry.schemaRow));
×
477
        }
478
      }
479
    }
480
    if (!tDecodeIsEnd(pCoder)) {
1,593,201,696✔
481
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
1,006,064,054✔
482
    } else {
483
      pME->pExtSchemas = NULL;
587,137,642✔
484
    }
485
  }
486
  if (pME->type == TSDB_SUPER_TABLE) {
1,592,620,819✔
487
    if (!tDecodeIsEnd(pCoder)) {
791,457,219✔
488
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,584,246,399✔
489
    }
490
    if (!tDecodeIsEnd(pCoder)) {
792,012,016✔
491
      TAOS_CHECK_RETURN(tDecodeI64v(pCoder, &pME->stbEntry.ownerId));
1,584,275,725✔
492
    }
493
  } else if (pME->type == TSDB_NORMAL_TABLE) {
800,451,416✔
494
    if (!tDecodeIsEnd(pCoder)) {
213,736,449✔
495
      TAOS_CHECK_RETURN(tDecodeI64v(pCoder, &pME->ntbEntry.ownerId));
427,356,979✔
496
    }
497
  }
498

499

500
  tEndDecode(pCoder);
1,592,632,008✔
501
  return 0;
1,592,962,723✔
502
}
503

504
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
1,593,344,939✔
505

506
static int32_t metaCloneSchema(const SSchemaWrapper *pSrc, SSchemaWrapper *pDst) {
293,930,100✔
507
  if (pSrc == NULL || pDst == NULL) {
293,930,100✔
508
    return TSDB_CODE_INVALID_PARA;
×
509
  }
510

511
  pDst->nCols = pSrc->nCols;
293,962,074✔
512
  pDst->version = pSrc->version;
293,940,989✔
513
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
293,940,274✔
514
  if (pDst->pSchema == NULL) {
293,843,711✔
515
    return terrno;
×
516
  }
517
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
293,858,624✔
518
  return TSDB_CODE_SUCCESS;
293,957,173✔
519
}
520

521
static int32_t metaCloneRsmaParam(const SRSmaParam *pSrc, SRSmaParam *pDst) {
42,398✔
522
  if (pSrc == NULL || pDst == NULL) {
42,398✔
523
    return TSDB_CODE_INVALID_PARA;
×
524
  }
525
  memcpy(pDst, pSrc, sizeof(SRSmaParam));
42,398✔
526
  pDst->name = tstrdup(pSrc->name);
42,398✔
527
  if (pDst->name == NULL) {
43,129✔
528
    return terrno;
×
529
  }
530
  if (pSrc->nFuncs > 0) {
43,129✔
531
    pDst->nFuncs = pSrc->nFuncs;
43,129✔
532
    pDst->funcColIds = (col_id_t *)taosMemoryMalloc(pSrc->nFuncs * sizeof(col_id_t));
43,129✔
533
    if (pDst->funcColIds == NULL) {
43,129✔
534
      return terrno;
×
535
    }
536
    memcpy(pDst->funcColIds, pSrc->funcColIds, pSrc->nFuncs * sizeof(col_id_t));
43,129✔
537

538
    pDst->funcIds = (func_id_t *)taosMemoryMalloc(pSrc->nFuncs * sizeof(func_id_t));
42,398✔
539
    if (pDst->funcIds == NULL) {
43,129✔
540
      return terrno;
×
541
    }
542
    memcpy(pDst->funcIds, pSrc->funcIds, pSrc->nFuncs * sizeof(func_id_t));
42,398✔
543
  } else {
544
    pDst->nFuncs = 0;
×
545
    pDst->funcColIds = NULL;
×
546
    pDst->funcIds = NULL;
×
547
  }
548

549
  return TSDB_CODE_SUCCESS;
42,398✔
550
}
551

552
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
293,893,780✔
553
  if (pSchema) {
293,893,780✔
554
    taosMemoryFreeClear(pSchema->pSchema);
293,921,070✔
555
  }
556
}
293,911,216✔
557

558
/**
559
 * @param type 0x01 free name
560
 */
561
void metaFreeRsmaParam(SRSmaParam *pParam, int8_t type) {
65,059✔
562
  if (pParam) {
65,059✔
563
    if ((type & 0x01)) {
65,059✔
564
      taosMemoryFreeClear(pParam->name);
65,059✔
565
    }
566
    taosMemoryFreeClear(pParam->funcColIds);
65,059✔
567
    taosMemoryFreeClear(pParam->funcIds);
65,059✔
568
  }
569
}
65,059✔
570

571
void metaCloneEntryFree(SMetaEntry **ppEntry) {
167,661,141✔
572
  if (ppEntry == NULL || *ppEntry == NULL) {
167,661,141✔
573
    return;
72,138✔
574
  }
575

576
  taosMemoryFreeClear((*ppEntry)->name);
167,600,241✔
577

578
  if ((*ppEntry)->type < 0) {
167,620,031✔
579
    taosMemoryFreeClear(*ppEntry);
×
580
    return;
×
581
  }
582

583
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
167,570,637✔
584
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
142,725,466✔
585
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
142,709,577✔
586
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
142,709,317✔
587
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
64,328✔
588
    }
589
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
24,883,317✔
590
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
16,402,735✔
591
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
16,401,775✔
592
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
8,481,848✔
593
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
8,481,848✔
594
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
8,481,848✔
595
  } else {
596
    return;
×
597
  }
598
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
167,597,874✔
599
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
167,597,998✔
600
  metaCloneColRefFree(&(*ppEntry)->colRef);
167,600,639✔
601

602
  taosMemoryFreeClear(*ppEntry);
167,574,740✔
603
  return;
167,582,802✔
604
}
605

606
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
167,595,888✔
607
  int32_t code = TSDB_CODE_SUCCESS;
167,595,888✔
608

609
  if (NULL == pEntry || NULL == ppEntry) {
167,595,888✔
610
    return TSDB_CODE_INVALID_PARA;
×
611
  }
612

613
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
167,631,415✔
614
  if (NULL == *ppEntry) {
167,551,736✔
615
    return terrno;
×
616
  }
617

618
  (*ppEntry)->version = pEntry->version;
167,583,851✔
619
  (*ppEntry)->type = pEntry->type;
167,599,961✔
620
  (*ppEntry)->uid = pEntry->uid;
167,595,676✔
621

622
  if (pEntry->type < 0) {
167,595,539✔
623
    return TSDB_CODE_SUCCESS;
×
624
  }
625

626
  if (pEntry->name) {
167,586,919✔
627
    (*ppEntry)->name = tstrdup(pEntry->name);
167,605,759✔
628
    if (NULL == (*ppEntry)->name) {
167,615,989✔
629
      code = terrno;
×
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633
  }
634

635
  if (pEntry->type == TSDB_SUPER_TABLE) {
167,608,372✔
636
    (*ppEntry)->flags = pEntry->flags;
142,708,771✔
637

638
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
142,724,453✔
639
    if (code) {
142,724,850✔
640
      metaCloneEntryFree(ppEntry);
×
641
      return code;
×
642
    }
643

644
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
142,724,850✔
645
    if (code) {
142,757,226✔
646
      metaCloneEntryFree(ppEntry);
×
647
      return code;
×
648
    }
649
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
142,757,226✔
650
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
43,129✔
651
      if (code) {
42,398✔
652
        metaCloneEntryFree(ppEntry);
×
653
        return code;
×
654
      }
655
    }
656
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
142,758,415✔
657
    (*ppEntry)->stbEntry.ownerId = pEntry->stbEntry.ownerId;
142,744,297✔
658
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
24,885,230✔
659
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
16,403,382✔
660
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
16,403,041✔
661
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
16,403,041✔
662

663
    // comment
664
    (*ppEntry)->ctbEntry.commentLen = pEntry->ctbEntry.commentLen;
16,403,041✔
665
    if (pEntry->ctbEntry.commentLen > 0) {
16,403,382✔
666
      (*ppEntry)->ctbEntry.comment = taosMemoryMalloc(pEntry->ctbEntry.commentLen + 1);
8,301✔
667
      if (NULL == (*ppEntry)->ctbEntry.comment) {
8,301✔
668
        code = terrno;
×
669
        metaCloneEntryFree(ppEntry);
×
670
        return code;
×
671
      }
672
      memcpy((*ppEntry)->ctbEntry.comment, pEntry->ctbEntry.comment, pEntry->ctbEntry.commentLen + 1);
8,301✔
673
    }
674

675
    // tags
676
    STag *pTags = (STag *)pEntry->ctbEntry.pTags;
16,403,041✔
677
    (*ppEntry)->ctbEntry.pTags = taosMemoryCalloc(1, pTags->len);
16,403,041✔
678
    if (NULL == (*ppEntry)->ctbEntry.pTags) {
16,403,082✔
679
      code = terrno;
×
680
      metaCloneEntryFree(ppEntry);
×
681
      return code;
×
682
    }
683
    memcpy((*ppEntry)->ctbEntry.pTags, pEntry->ctbEntry.pTags, pTags->len);
16,402,741✔
684
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
8,481,848✔
685
    (*ppEntry)->ntbEntry.btime = pEntry->ntbEntry.btime;
8,481,848✔
686
    (*ppEntry)->ntbEntry.ttlDays = pEntry->ntbEntry.ttlDays;
8,481,848✔
687
    (*ppEntry)->ntbEntry.ncid = pEntry->ntbEntry.ncid;
8,481,848✔
688
    (*ppEntry)->ntbEntry.ownerId = pEntry->ntbEntry.ownerId;
8,481,848✔
689

690
    // schema
691
    code = metaCloneSchema(&pEntry->ntbEntry.schemaRow, &(*ppEntry)->ntbEntry.schemaRow);
8,481,848✔
692
    if (code) {
8,481,848✔
693
      metaCloneEntryFree(ppEntry);
×
694
      return code;
×
695
    }
696

697
    // comment
698
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
8,481,848✔
699
    if (pEntry->ntbEntry.commentLen > 0) {
8,481,848✔
700
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
11,833✔
701
      if (NULL == (*ppEntry)->ntbEntry.comment) {
11,833✔
702
        code = terrno;
×
703
        metaCloneEntryFree(ppEntry);
×
704
        return code;
×
705
      }
706
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
11,833✔
707
    }
708
  } else {
709
    return TSDB_CODE_INVALID_PARA;
×
710
  }
711

712
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
167,618,195✔
713
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
647,300✔
714
    if (code) {
643,155✔
715
      metaCloneEntryFree(ppEntry);
×
716
      return code;
×
717
    }
718
  } else {
719
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
166,954,495✔
720
    if (code) {
166,968,102✔
721
      metaCloneEntryFree(ppEntry);
×
722
      return code;
×
723
    }
724
  }
725
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
167,611,257✔
726
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
9,216,130✔
727
    if (!(*ppEntry)->pExtSchemas) {
9,213,072✔
728
      code = terrno;
×
729
      metaCloneEntryFree(ppEntry);
×
730
      return code;
×
731
    }
732
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
9,210,885✔
733
  }
734

735
  return code;
167,569,096✔
736
}
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