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

taosdata / TDengine / #4980

10 Mar 2026 08:57AM UTC coverage: 68.492% (-0.02%) from 68.512%
#4980

push

travis-ci

web-flow
fix: add retry while exec ci case test_stable_keep_compact.py. (#34729)

211901 of 309380 relevant lines covered (68.49%)

135171938.8 hits per line

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

81.31
/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,249,150,409✔
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;
151,272,502✔
26
    }
27
  }
28
  return false;
1,096,695,435✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
215,839,088✔
32
  if (pME->pExtSchemas) {
215,839,088✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
12,845,058✔
34
    bool                  hasTypeMods = false;
12,845,058✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
12,845,058✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
12,741,502✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
96,612✔
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
96,612✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
12,836,770✔
43

44
    for (int32_t i = 0; i < pSchWrapper->nCols && hasTypeMods; ++i) {
1,107,902,215✔
45
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->pExtSchemas[i].typeMod));
2,147,483,647✔
46
    }
47
  }
48
  return 0;
215,874,739✔
49
}
50

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

57
  for (int32_t i = 0; i < pw->nCols; i++) {
442,136,860✔
58
    SColRef *p = &pw->pColRef[i];
440,748,798✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
881,497,596✔
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
881,497,596✔
61
    if (p->hasRef) {
440,748,798✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
584,476,532✔
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
584,476,532✔
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
584,476,532✔
65
    }
66
  }
67
  return 0;
1,388,062✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
1,190,619,270✔
71
  bool hasExtSchema = false;
1,190,619,270✔
72
  SSchemaWrapper* pSchWrapper = NULL;
1,190,619,270✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
1,190,619,270✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
917,485,050✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
273,267,326✔
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
273,365,550✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
1,190,855,651✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
1,191,033,103✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
282,960,649✔
84
    if (pME->pExtSchemas == NULL) {
141,482,924✔
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;
1,049,552,739✔
93
  }
94

95
  return 0;
1,191,009,372✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
45,681,367✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
45,681,367✔
100
  bool                  hasTypeMods = false;
45,681,367✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
45,681,367✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
33,153,727✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
12,549,782✔
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
12,555,120✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
45,709,647✔
109

110
  if (hasTypeMods) {
45,663,664✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
40,303✔
112
    if (ret != NULL) {
40,303✔
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
40,303✔
114
    }
115
    return ret;
40,303✔
116
  }
117
  return NULL;
45,623,361✔
118
}
119

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

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

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

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
27,158✔
151
  int32_t    i = 0, j = 0;
27,158✔
152
  for (i = 0; i < nCols; ++i) {
205,520✔
153
    while (j < pParam->nFuncs) {
324,428✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
311,216✔
155
        pFuncIds[i] = pParam->funcIds[j];
145,332✔
156
        break;
145,332✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
165,884✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
33,030✔
160
        break;
33,030✔
161
      }
162
      ++j;
132,854✔
163
    }
164
    if (j >= pParam->nFuncs) {
191,574✔
165
      for (; i < nCols; ++i) {
26,424✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
13,212✔
167
      }
168
      break;
13,212✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
27,158✔
172

173
  return 0;
27,158✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
39,883,826✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
39,883,826✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
79,768,833✔
179
  if (pWrapper->nCols == 0) {
39,882,759✔
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
79,766,040✔
184
  uDebug("decode cols:%d", pWrapper->nCols);
39,884,158✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
79,765,481✔
186
  if (pWrapper->pColRef == NULL) {
39,880,997✔
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
888,615,339✔
191
    SColRef *p = &pWrapper->pColRef[i];
848,716,980✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
1,697,517,671✔
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
1,697,552,788✔
194
    if (p->hasRef) {
848,783,823✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
465,855,230✔
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
465,850,332✔
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
465,862,124✔
198
    }
199
  }
200
  return 0;
39,887,149✔
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) {
689,593✔
220
  if (pSrc->nCols > 0) {
689,593✔
221
    pDst->nCols = pSrc->nCols;
689,593✔
222
    pDst->version = pSrc->version;
689,593✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
689,593✔
224
    if (NULL == pDst->pColRef) {
689,593✔
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
689,593✔
228
  }
229
  return 0;
689,593✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
47,983,449✔
233
  int32_t code = 0;
47,983,449✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
95,977,476✔
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
96,027,512✔
236
  uTrace("encode cols:%d", pw->nCols);
48,033,485✔
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;
48,098,117✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
47,805,454✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
47,805,454✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
47,855,649✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
1,190,618,354✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
1,190,618,354✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
2,147,483,647✔
252
  if (pWrapper->nCols == 0) {
1,190,823,847✔
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,190,848,089✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
2,147,483,647✔
259
  if (pWrapper->pColCmpr == NULL) {
1,190,893,920✔
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,191,348,853✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
238,419✔
273

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

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

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

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
209,759,522✔
294
  if (pSrc->nCols > 0) {
209,759,522✔
295
    pDst->nCols = pSrc->nCols;
186,158,054✔
296
    pDst->version = pSrc->version;
186,161,264✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
186,119,035✔
298
    if (NULL == pDst->pColCmpr) {
186,129,688✔
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
186,109,499✔
302
  }
303
  return 0;
209,772,437✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
210,398,553✔
307
  if (pColRef) {
210,398,553✔
308
    taosMemoryFreeClear(pColRef->pColRef);
210,420,204✔
309
  }
310
}
210,420,635✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
210,412,958✔
313
  if (pCmpr) {
210,412,958✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
210,410,994✔
315
  }
316
}
210,474,111✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
221,164,947✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
221,164,947✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
442,369,587✔
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
442,314,308✔
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
442,284,109✔
323

324
  if (pME->type > 0) {
221,155,184✔
325
    if (pME->name == NULL) {
215,807,078✔
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
431,696,776✔
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
215,871,858✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
53,321,656✔
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
53,405,960✔
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
53,387,570✔
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
26,657,730✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
106,430✔
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
189,139,365✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
334,087,102✔
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
334,109,602✔
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
334,116,847✔
342
      if (pME->ctbEntry.commentLen > 0) {
167,059,292✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
29,412✔
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
334,068,136✔
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
167,023,310✔
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
22,122,736✔
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
44,245,472✔
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
44,245,472✔
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
44,245,472✔
351
      if (pME->ntbEntry.commentLen > 0) {
22,122,736✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
40,292✔
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
44,245,472✔
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
44,245,472✔
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) {
215,824,846✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,489,107✔
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
214,387,724✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
26,695,079✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
187,775,846✔
368
        if (pME->colCmpr.nCols != 0) {
21,392,576✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
21,154,144✔
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
238,432✔
372
          SColCmprWrapper colCmprs = {0};
238,432✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
238,432✔
374
          if (code != 0) {
238,432✔
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
238,432✔
379
          taosMemoryFree(colCmprs.pColCmpr);
238,432✔
380
          TAOS_CHECK_RETURN(code);
238,432✔
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
215,859,981✔
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
221,187,332✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
53,350,447✔
388
    TAOS_CHECK_RETURN(tEncodeI64v(pCoder, pME->stbEntry.ownerId));
53,257,549✔
389
  } else if (pME->type == TSDB_NORMAL_TABLE) {
194,464,378✔
390
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.ownerId));
42,785,152✔
391
  }
392

393
  tEndEncode(pCoder);
221,103,678✔
394
  return 0;
221,052,509✔
395
}
396

397
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
2,040,583,224✔
398
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
2,040,583,224✔
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) {
2,041,534,991✔
404
    tEndDecode(pCoder);
341,032✔
405
    return 0;
341,032✔
406
  }
407

408
  if (pME->type > 0) {
2,041,193,959✔
409
    TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->name));
2,147,483,647✔
410

411
    if (pME->type == TSDB_SUPER_TABLE) {
2,041,008,033✔
412
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,835,089,042✔
413
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,835,380,589✔
414
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,835,288,322✔
415
      if (TABLE_IS_ROLLUP(pME->flags)) {
917,578,743✔
416
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
335,438✔
417
      }
418
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
1,122,817,411✔
419
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
1,682,053,973✔
420
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
1,682,278,821✔
421
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
1,681,941,918✔
422
      if (pME->ctbEntry.commentLen > 0) {
840,798,421✔
423
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
50,976✔
424
      }
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
1,681,957,428✔
426
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
841,111,278✔
427
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
282,127,016✔
428
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
564,468,295✔
429
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
564,573,259✔
430
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
564,434,449✔
431
      if (pME->ntbEntry.commentLen > 0) {
282,149,257✔
432
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
74,598✔
433
      }
434
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
564,227,966✔
435
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
564,416,976✔
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) {
2,040,634,910✔
447
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
917,697,704✔
448
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
917,648,203✔
449

450
        if (pME->colCmpr.nCols == 0) {
917,734,670✔
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,122,781,459✔
458
      if (!tDecodeIsEnd(pCoder)) {
273,441,624✔
459
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
273,472,207✔
460
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
273,472,431✔
461
        if (pME->colCmpr.nCols == 0) {
273,468,961✔
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);
×
466
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
467
      }
468
      TABLE_SET_COL_COMPRESSED(pME->flags);
273,449,200✔
469
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
849,403,213✔
470
      if (!tDecodeIsEnd(pCoder)) {
39,886,337✔
471
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
39,886,164✔
472
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
39,886,388✔
473
      } else {
474
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
173✔
475
        if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
173✔
476
           TAOS_CHECK_RETURN(metatInitDefaultSColRefWrapper(pCoder, &pME->colRef, &pME->ntbEntry.schemaRow));
×
477
        }
478
      }
479
    }
480
    if (!tDecodeIsEnd(pCoder)) {
2,040,754,738✔
481
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
1,191,074,318✔
482
    } else {
483
      pME->pExtSchemas = NULL;
849,680,420✔
484
    }
485
  }
486
  if (pME->type == TSDB_SUPER_TABLE) {
2,040,425,145✔
487
    if (!tDecodeIsEnd(pCoder)) {
917,464,959✔
488
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,835,064,524✔
489
    }
490
    if (!tDecodeIsEnd(pCoder)) {
917,252,713✔
491
      TAOS_CHECK_RETURN(tDecodeI64v(pCoder, &pME->stbEntry.ownerId));
1,834,764,363✔
492
    }
493
  } else if (pME->type == TSDB_NORMAL_TABLE) {
1,122,580,744✔
494
    if (!tDecodeIsEnd(pCoder)) {
273,429,921✔
495
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.ownerId));
546,922,698✔
496
    }
497
  }
498

499

500
  tEndDecode(pCoder);
2,040,112,972✔
501
  return 0;
2,040,342,381✔
502
}
503

504
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
2,040,479,765✔
505

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

511
  pDst->nCols = pSrc->nCols;
364,350,999✔
512
  pDst->version = pSrc->version;
364,335,758✔
513
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
364,340,673✔
514
  if (pDst->pSchema == NULL) {
364,208,992✔
515
    return terrno;
×
516
  }
517
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
364,245,024✔
518
  return TSDB_CODE_SUCCESS;
364,358,750✔
519
}
520

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

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

549
  return TSDB_CODE_SUCCESS;
72,666✔
550
}
551

552
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
364,291,567✔
553
  if (pSchema) {
364,291,567✔
554
    taosMemoryFreeClear(pSchema->pSchema);
364,305,570✔
555
  }
556
}
364,353,132✔
557

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

571
void metaCloneEntryFree(SMetaEntry **ppEntry) {
210,493,987✔
572
  if (ppEntry == NULL || *ppEntry == NULL) {
210,493,987✔
573
    return;
78,128✔
574
  }
575

576
  taosMemoryFreeClear((*ppEntry)->name);
210,432,017✔
577

578
  if ((*ppEntry)->type < 0) {
210,444,348✔
579
    taosMemoryFreeClear(*ppEntry);
×
580
    return;
×
581
  }
582

583
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
210,406,944✔
584
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
177,702,457✔
585
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
177,706,751✔
586
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
177,707,148✔
587
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
110,834✔
588
    }
589
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
32,731,963✔
590
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
23,835,367✔
591
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
23,834,738✔
592
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
8,897,438✔
593
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
8,897,634✔
594
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
8,897,634✔
595
  } else {
596
    return;
×
597
  }
598
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
210,438,256✔
599
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
210,423,748✔
600
  metaCloneColRefFree(&(*ppEntry)->colRef);
210,418,943✔
601

602
  taosMemoryFreeClear(*ppEntry);
210,383,313✔
603
  return;
210,426,537✔
604
}
605

606
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
210,429,829✔
607
  int32_t code = TSDB_CODE_SUCCESS;
210,429,829✔
608

609
  if (NULL == pEntry || NULL == ppEntry) {
210,429,829✔
610
    return TSDB_CODE_INVALID_PARA;
×
611
  }
612

613
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
210,469,354✔
614
  if (NULL == *ppEntry) {
210,382,568✔
615
    return terrno;
×
616
  }
617

618
  (*ppEntry)->version = pEntry->version;
210,405,193✔
619
  (*ppEntry)->type = pEntry->type;
210,437,054✔
620
  (*ppEntry)->uid = pEntry->uid;
210,405,504✔
621

622
  if (pEntry->type < 0) {
210,414,299✔
623
    return TSDB_CODE_SUCCESS;
×
624
  }
625

626
  if (pEntry->name) {
210,434,607✔
627
    (*ppEntry)->name = tstrdup(pEntry->name);
210,459,968✔
628
    if (NULL == (*ppEntry)->name) {
210,455,471✔
629
      code = terrno;
×
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633
  }
634

635
  if (pEntry->type == TSDB_SUPER_TABLE) {
210,449,243✔
636
    (*ppEntry)->flags = pEntry->flags;
177,701,273✔
637

638
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
177,685,621✔
639
    if (code) {
177,718,522✔
640
      metaCloneEntryFree(ppEntry);
×
641
      return code;
×
642
    }
643

644
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
177,718,522✔
645
    if (code) {
177,740,893✔
646
      metaCloneEntryFree(ppEntry);
×
647
      return code;
×
648
    }
649
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
177,740,893✔
650
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
72,666✔
651
      if (code) {
72,666✔
652
        metaCloneEntryFree(ppEntry);
×
653
        return code;
×
654
      }
655
    }
656
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
177,740,637✔
657
    (*ppEntry)->stbEntry.ownerId = pEntry->stbEntry.ownerId;
177,712,786✔
658
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
32,732,609✔
659
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
23,835,171✔
660
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
23,835,171✔
661
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
23,835,171✔
662

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

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

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

697
    // comment
698
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
8,897,438✔
699
    if (pEntry->ntbEntry.commentLen > 0) {
8,897,438✔
700
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
12,489✔
701
      if (NULL == (*ppEntry)->ntbEntry.comment) {
12,489✔
702
        code = terrno;
×
703
        metaCloneEntryFree(ppEntry);
×
704
        return code;
×
705
      }
706
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
12,489✔
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) {
210,454,809✔
713
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
733,683✔
714
    if (code) {
689,593✔
715
      metaCloneEntryFree(ppEntry);
×
716
      return code;
×
717
    }
718
  } else {
719
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
209,729,472✔
720
    if (code) {
209,763,310✔
721
      metaCloneEntryFree(ppEntry);
×
722
      return code;
×
723
    }
724
  }
725
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
210,452,903✔
726
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
10,214,143✔
727
    if (!(*ppEntry)->pExtSchemas) {
10,217,081✔
728
      code = terrno;
×
729
      metaCloneEntryFree(ppEntry);
×
730
      return code;
×
731
    }
732
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
10,218,688✔
733
  }
734

735
  return code;
210,410,860✔
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