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

taosdata / TDengine / #4995

18 Mar 2026 06:26AM UTC coverage: 71.996% (-0.2%) from 72.244%
#4995

push

travis-ci

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

2 of 4 new or added lines in 2 files covered. (50.0%)

5312 existing lines in 167 files now uncovered.

244665 of 339830 relevant lines covered (72.0%)

135013613.72 hits per line

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

81.72
/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,173,478,053✔
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;
156,778,148✔
26
    }
27
  }
28
  return false;
1,014,038,071✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
199,850,675✔
32
  if (pME->pExtSchemas) {
199,850,675✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
12,244,323✔
34
    bool                  hasTypeMods = false;
12,244,323✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
12,244,323✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
12,131,176✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
114,396✔
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
114,396✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
12,248,600✔
43

44
    for (int32_t i = 0; i < pSchWrapper->nCols && hasTypeMods; ++i) {
1,300,873,577✔
45
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->pExtSchemas[i].typeMod));
2,147,483,647✔
46
    }
47
  }
48
  return 0;
199,860,301✔
49
}
50

51
int meteEncodeColRefEntry(SEncoder *pCoder, const SMetaEntry *pME) {
1,817,050✔
52
  const SColRefWrapper *pw = &pME->colRef;
1,817,050✔
53
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
3,634,100✔
54
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
3,634,100✔
55
  uTrace("encode cols:%d", pw->nCols);
1,817,050✔
56

57
  for (int32_t i = 0; i < pw->nCols; i++) {
457,358,820✔
58
    SColRef *p = &pw->pColRef[i];
455,541,770✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
911,083,540✔
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
911,083,540✔
61
    if (p->hasRef) {
455,541,770✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
603,973,500✔
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
603,973,500✔
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
603,973,500✔
65
    }
66
  }
67
  return 0;
1,817,050✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
1,116,022,250✔
71
  bool hasExtSchema = false;
1,116,022,250✔
72
  SSchemaWrapper* pSchWrapper = NULL;
1,116,022,250✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
1,116,022,250✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
872,820,950✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
243,282,498✔
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
243,331,336✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
1,116,056,754✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
1,116,122,796✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
295,251,389✔
84
    if (pME->pExtSchemas == NULL) {
147,630,516✔
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;
968,496,434✔
93
  }
94

95
  return 0;
1,116,114,121✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
45,276,695✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
45,276,695✔
100
  bool                  hasTypeMods = false;
45,276,695✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
45,276,695✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
33,077,182✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
12,212,739✔
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
12,225,892✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
45,304,154✔
109

110
  if (hasTypeMods) {
45,265,671✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
27,618✔
112
    if (ret != NULL) {
27,618✔
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
27,618✔
114
    }
115
    return ret;
27,618✔
116
  }
117
  return NULL;
45,238,053✔
118
}
119

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

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

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

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
27,639✔
151
  int32_t    i = 0, j = 0;
27,639✔
152
  for (i = 0; i < nCols; ++i) {
209,160✔
153
    while (j < pParam->nFuncs) {
330,174✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
316,728✔
155
        pFuncIds[i] = pParam->funcIds[j];
147,906✔
156
        break;
147,906✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
168,822✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
33,615✔
160
        break;
33,615✔
161
      }
162
      ++j;
135,207✔
163
    }
164
    if (j >= pParam->nFuncs) {
194,967✔
165
      for (; i < nCols; ++i) {
26,892✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
13,446✔
167
      }
168
      break;
13,446✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
27,639✔
172

173
  return 0;
27,639✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
48,451,576✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
48,451,576✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
96,912,358✔
179
  if (pWrapper->nCols == 0) {
48,454,656✔
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
96,905,733✔
184
  uDebug("decode cols:%d", pWrapper->nCols);
48,454,496✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
96,903,359✔
186
  if (pWrapper->pColRef == NULL) {
48,453,202✔
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
837,369,954✔
191
    SColRef *p = &pWrapper->pColRef[i];
788,892,427✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
1,577,878,615✔
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
1,577,925,880✔
194
    if (p->hasRef) {
788,980,360✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
437,529,919✔
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
437,526,349✔
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
437,534,605✔
198
    }
199
  }
200
  return 0;
48,459,643✔
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) {
1,226,674✔
220
  if (pSrc->nCols > 0) {
1,226,674✔
221
    pDst->nCols = pSrc->nCols;
1,226,674✔
222
    pDst->version = pSrc->version;
1,226,674✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
1,226,674✔
224
    if (NULL == pDst->pColRef) {
1,226,069✔
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
1,226,069✔
228
  }
229
  return 0;
1,226,069✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
46,921,214✔
233
  int32_t code = 0;
46,921,214✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
93,821,992✔
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
93,805,078✔
236
  uTrace("encode cols:%d", pw->nCols);
46,904,300✔
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;
46,959,079✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
46,677,634✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
46,677,634✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
46,729,492✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
1,116,050,870✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
1,116,050,870✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
2,147,483,647✔
252
  if (pWrapper->nCols == 0) {
1,116,028,950✔
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
2,147,483,647✔
257
  uDebug("dencode cols:%d", pWrapper->nCols);
1,116,105,915✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
2,147,483,647✔
259
  if (pWrapper->pColCmpr == NULL) {
1,116,228,854✔
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,116,536,183✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
236,918✔
273

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

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

284
  for (int32_t i = 0; i < pCmpr->nCols; i++) {
1,185,172✔
285
    SColCmpr *pColCmpr = &pCmpr->pColCmpr[i];
948,254✔
286
    SSchema  *pColSchema = &pSchema->pSchema[i];
948,254✔
287
    pColCmpr->id = pColSchema->colId;
948,254✔
288
    pColCmpr->alg = createDefaultColCmprByType(pColSchema->type);
948,254✔
289
  }
290
  return 0;
236,918✔
291
}
292

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
195,931,843✔
294
  if (pSrc->nCols > 0) {
195,931,843✔
295
    pDst->nCols = pSrc->nCols;
170,164,228✔
296
    pDst->version = pSrc->version;
170,169,368✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
170,173,108✔
298
    if (NULL == pDst->pColCmpr) {
170,147,834✔
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
170,155,479✔
302
  }
303
  return 0;
195,946,732✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
197,150,233✔
307
  if (pColRef) {
197,150,233✔
308
    taosMemoryFreeClear(pColRef->pColRef);
197,161,723✔
309
  }
310
}
197,163,521✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
197,151,284✔
313
  if (pCmpr) {
197,151,284✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
197,162,431✔
315
  }
316
}
197,139,628✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
204,820,058✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
204,820,058✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
409,678,154✔
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
409,621,642✔
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
409,601,510✔
323

324
  if (pME->type > 0) {
204,809,248✔
325
    if (pME->name == NULL) {
199,850,540✔
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
399,658,958✔
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
199,826,182✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
51,143,151✔
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
51,225,372✔
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
51,210,056✔
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
25,571,636✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
112,668✔
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
174,259,032✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
304,373,620✔
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
304,370,939✔
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
304,367,893✔
342
      if (pME->ctbEntry.commentLen > 0) {
152,186,362✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
29,132✔
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
304,375,502✔
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
152,187,708✔
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
22,083,884✔
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
44,167,768✔
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
44,167,768✔
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
44,167,768✔
351
      if (pME->ntbEntry.commentLen > 0) {
22,083,884✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
18,492✔
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
44,167,768✔
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
44,167,768✔
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) {
199,877,365✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,891,630✔
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
198,007,057✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
25,567,302✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
172,487,174✔
368
        if (pME->colCmpr.nCols != 0) {
21,352,466✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
21,115,548✔
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
236,918✔
372
          SColCmprWrapper colCmprs = {0};
236,918✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
236,918✔
374
          if (code != 0) {
236,918✔
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
236,918✔
379
          taosMemoryFree(colCmprs.pColCmpr);
236,918✔
380
          TAOS_CHECK_RETURN(code);
236,918✔
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
199,870,262✔
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
204,801,774✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
51,179,006✔
388
    TAOS_CHECK_RETURN(tEncodeI64v(pCoder, pME->stbEntry.ownerId));
51,096,611✔
389
  } else if (pME->type == TSDB_NORMAL_TABLE) {
179,203,115✔
390
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.ownerId));
42,704,932✔
391
  }
392

393
  tEndEncode(pCoder);
204,759,052✔
394
  return 0;
204,818,437✔
395
}
396

397
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
1,885,913,213✔
398
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
1,885,913,213✔
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,886,368,932✔
404
    tEndDecode(pCoder);
338,000✔
405
    return 0;
338,000✔
406
  }
407

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

411
    if (pME->type == TSDB_SUPER_TABLE) {
1,886,038,070✔
412
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,745,960,348✔
413
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,746,006,795✔
414
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,745,871,171✔
415
      if (TABLE_IS_ROLLUP(pME->flags)) {
872,866,909✔
416
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
342,963✔
417
      }
418
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
1,012,754,403✔
419
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
1,523,609,559✔
420
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
1,523,704,930✔
421
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
1,523,582,774✔
422
      if (pME->ctbEntry.commentLen > 0) {
761,728,621✔
423
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
50,396✔
424
      }
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
1,523,564,710✔
426
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
761,858,609✔
427
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
251,202,337✔
428
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
502,537,197✔
429
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
502,619,783✔
430
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
502,558,705✔
431
      if (pME->ntbEntry.commentLen > 0) {
251,251,386✔
432
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
38,426✔
433
      }
434
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
502,432,493✔
435
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
502,449,487✔
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,885,737,355✔
447
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
872,913,663✔
448
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
872,953,702✔
449

450
        if (pME->colCmpr.nCols == 0) {
873,049,691✔
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) {
1,012,695,833✔
458
      if (!tDecodeIsEnd(pCoder)) {
243,438,926✔
459
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
243,439,787✔
460
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
243,439,787✔
461
        if (pME->colCmpr.nCols == 0) {
243,359,751✔
462
          TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
463
        }
464
      } else {
465
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
2,666✔
466
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
2,666✔
467
      }
468
      TABLE_SET_COL_COMPRESSED(pME->flags);
243,386,805✔
469
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
769,397,774✔
470
      if (!tDecodeIsEnd(pCoder)) {
48,456,843✔
471
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
48,455,667✔
472
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
48,455,667✔
473
      } else {
474
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
1,176✔
475
        if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
1,176✔
476
           TAOS_CHECK_RETURN(metatInitDefaultSColRefWrapper(pCoder, &pME->colRef, &pME->ntbEntry.schemaRow));
×
477
        }
478
      }
479
    }
480
    if (!tDecodeIsEnd(pCoder)) {
1,885,969,568✔
481
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
1,116,348,512✔
482
    } else {
483
      pME->pExtSchemas = NULL;
769,621,056✔
484
    }
485
  }
486
  if (pME->type == TSDB_SUPER_TABLE) {
1,885,659,870✔
487
    if (!tDecodeIsEnd(pCoder)) {
872,776,701✔
488
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,745,816,108✔
489
    }
490
    if (!tDecodeIsEnd(pCoder)) {
872,805,126✔
491
      TAOS_CHECK_RETURN(tDecodeI64v(pCoder, &pME->stbEntry.ownerId));
1,745,659,617✔
492
    }
493
  } else if (pME->type == TSDB_NORMAL_TABLE) {
1,012,616,103✔
494
    if (!tDecodeIsEnd(pCoder)) {
243,370,775✔
495
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.ownerId));
486,820,174✔
496
    }
497
  }
498

499

500
  tEndDecode(pCoder);
1,885,788,255✔
501
  return 0;
1,885,826,194✔
502
}
503

504
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
1,885,707,417✔
505

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

511
  pDst->nCols = pSrc->nCols;
332,421,369✔
512
  pDst->version = pSrc->version;
332,404,761✔
513
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
332,432,533✔
514
  if (pDst->pSchema == NULL) {
332,327,348✔
515
    return terrno;
×
516
  }
517
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
332,349,543✔
518
  return TSDB_CODE_SUCCESS;
332,420,985✔
519
}
520

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

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

549
  return TSDB_CODE_SUCCESS;
75,537✔
550
}
551

552
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
332,400,146✔
553
  if (pSchema) {
332,400,146✔
554
    taosMemoryFreeClear(pSchema->pSchema);
332,420,236✔
555
  }
556
}
332,379,057✔
557

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

571
void metaCloneEntryFree(SMetaEntry **ppEntry) {
197,230,813✔
572
  if (ppEntry == NULL || *ppEntry == NULL) {
197,230,813✔
573
    return;
77,684✔
574
  }
575

576
  taosMemoryFreeClear((*ppEntry)->name);
197,160,486✔
577

578
  if ((*ppEntry)->type < 0) {
197,177,350✔
579
    taosMemoryFreeClear(*ppEntry);
×
580
    return;
×
581
  }
582

583
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
197,113,127✔
584
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
161,789,280✔
585
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
161,764,824✔
586
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
161,770,224✔
587
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
115,173✔
588
    }
589
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
35,372,160✔
590
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
26,532,600✔
591
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
26,533,876✔
592
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
8,841,079✔
593
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
8,841,079✔
594
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
8,841,079✔
595
  } else {
596
    return;
×
597
  }
598
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
197,140,672✔
599
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
197,132,638✔
600
  metaCloneColRefFree(&(*ppEntry)->colRef);
197,140,408✔
601

602
  taosMemoryFreeClear(*ppEntry);
197,113,272✔
603
  return;
197,142,678✔
604
}
605

606
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
197,135,042✔
607
  int32_t code = TSDB_CODE_SUCCESS;
197,135,042✔
608

609
  if (NULL == pEntry || NULL == ppEntry) {
197,135,042✔
610
    return TSDB_CODE_INVALID_PARA;
×
611
  }
612

613
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
197,164,926✔
614
  if (NULL == *ppEntry) {
197,104,580✔
615
    return terrno;
×
616
  }
617

618
  (*ppEntry)->version = pEntry->version;
197,121,746✔
619
  (*ppEntry)->type = pEntry->type;
197,131,810✔
620
  (*ppEntry)->uid = pEntry->uid;
197,149,542✔
621

622
  if (pEntry->type < 0) {
197,158,433✔
623
    return TSDB_CODE_SUCCESS;
×
624
  }
625

626
  if (pEntry->name) {
197,149,711✔
627
    (*ppEntry)->name = tstrdup(pEntry->name);
197,152,642✔
628
    if (NULL == (*ppEntry)->name) {
197,153,323✔
UNCOV
629
      code = terrno;
×
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633
  }
634

635
  if (pEntry->type == TSDB_SUPER_TABLE) {
197,137,642✔
636
    (*ppEntry)->flags = pEntry->flags;
161,778,646✔
637

638
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
161,789,685✔
639
    if (code) {
161,783,998✔
640
      metaCloneEntryFree(ppEntry);
×
641
      return code;
×
642
    }
643

644
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
161,783,998✔
645
    if (code) {
161,804,350✔
646
      metaCloneEntryFree(ppEntry);
×
647
      return code;
×
648
    }
649
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
161,804,350✔
650
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
75,537✔
651
      if (code) {
75,537✔
652
        metaCloneEntryFree(ppEntry);
×
653
        return code;
×
654
      }
655
    }
656
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
161,804,957✔
657
    (*ppEntry)->stbEntry.ownerId = pEntry->stbEntry.ownerId;
161,799,376✔
658
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
35,374,028✔
659
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
26,534,219✔
660
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
26,533,876✔
661
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
26,533,876✔
662

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

675
    // tags
676
    STag *pTags = (STag *)pEntry->ctbEntry.pTags;
26,533,876✔
677
    (*ppEntry)->ctbEntry.pTags = taosMemoryCalloc(1, pTags->len);
26,532,048✔
678
    if (NULL == (*ppEntry)->ctbEntry.pTags) {
26,532,640✔
679
      code = terrno;
×
680
      metaCloneEntryFree(ppEntry);
×
681
      return code;
×
682
    }
683
    memcpy((*ppEntry)->ctbEntry.pTags, pEntry->ctbEntry.pTags, pTags->len);
26,532,949✔
684
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
8,839,775✔
685
    (*ppEntry)->ntbEntry.btime = pEntry->ntbEntry.btime;
8,839,775✔
686
    (*ppEntry)->ntbEntry.ttlDays = pEntry->ntbEntry.ttlDays;
8,840,427✔
687
    (*ppEntry)->ntbEntry.ncid = pEntry->ntbEntry.ncid;
8,840,427✔
688
    (*ppEntry)->ntbEntry.ownerId = pEntry->ntbEntry.ownerId;
8,841,079✔
689

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

697
    // comment
698
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
8,841,079✔
699
    if (pEntry->ntbEntry.commentLen > 0) {
8,841,079✔
700
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
5,945✔
701
      if (NULL == (*ppEntry)->ntbEntry.comment) {
5,945✔
702
        code = terrno;
×
703
        metaCloneEntryFree(ppEntry);
×
704
        return code;
×
705
      }
706
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
5,945✔
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) {
197,180,196✔
713
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
1,225,590✔
714
    if (code) {
1,226,069✔
715
      metaCloneEntryFree(ppEntry);
×
716
      return code;
×
717
    }
718
  } else {
719
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
195,924,841✔
720
    if (code) {
195,944,662✔
721
      metaCloneEntryFree(ppEntry);
×
722
      return code;
×
723
    }
724
  }
725
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
197,170,731✔
726
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
9,612,046✔
727
    if (!(*ppEntry)->pExtSchemas) {
9,604,509✔
728
      code = terrno;
×
729
      metaCloneEntryFree(ppEntry);
×
730
      return code;
×
731
    }
732
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
9,611,386✔
733
  }
734

735
  return code;
197,151,555✔
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