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

taosdata / TDengine / #4864

26 Nov 2025 05:46AM UTC coverage: 64.548% (+0.009%) from 64.539%
#4864

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

769 of 945 new or added lines in 33 files covered. (81.38%)

3006 existing lines in 116 files now uncovered.

158227 of 245129 relevant lines covered (64.55%)

111826500.07 hits per line

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

80.5
/source/dnode/vnode/src/meta/metaEntry.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "meta.h"
17
#include "osMemPool.h"
18
#include "osMemory.h"
19
#include "tencode.h"
20
#include "tmsg.h"
21

22
static bool schemasHasTypeMod(const SSchema *pSchema, int32_t nCols) {
725,933,665✔
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;
38,553,826✔
26
    }
27
  }
28
  return false;
688,182,807✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
184,575,042✔
32
  if (pME->pExtSchemas) {
184,575,042✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
14,869,043✔
34
    bool                  hasTypeMods = false;
14,869,043✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
14,869,043✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
14,732,523✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
132,994✔
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
132,994✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
14,869,823✔
43

44
    for (int32_t i = 0; i < pSchWrapper->nCols && hasTypeMods; ++i) {
1,723,242,411✔
45
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->pExtSchemas[i].typeMod));
2,147,483,647✔
46
    }
47
  }
48
  return 0;
184,560,999✔
49
}
50

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

57
  for (int32_t i = 0; i < pw->nCols; i++) {
12,677,252✔
58
    SColRef *p = &pw->pColRef[i];
11,387,424✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
22,774,848✔
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
22,774,848✔
61
    if (p->hasRef) {
11,387,424✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
12,712,888✔
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
12,712,888✔
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
12,712,888✔
65
    }
66
  }
67
  return 0;
1,289,828✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
695,657,506✔
71
  bool hasExtSchema = false;
695,657,506✔
72
  SSchemaWrapper* pSchWrapper = NULL;
695,657,506✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
695,657,506✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
690,776,309✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
4,894,061✔
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
4,894,061✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
695,720,647✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
695,728,435✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
53,305,156✔
84
    if (pME->pExtSchemas == NULL) {
26,646,920✔
85
      return terrno;
×
86
    }
87

88
    for (int32_t i = 0; i < pSchWrapper->nCols && hasExtSchema; i++) {
2,128,552,751✔
89
      TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pME->pExtSchemas[i].typeMod));
2,147,483,647✔
90
    }
91
  } else {
92
    pME->pExtSchemas = NULL;
669,067,773✔
93
  }
94

95
  return 0;
695,846,814✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
15,454,446✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
15,454,446✔
100
  bool                  hasTypeMods = false;
15,454,446✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
15,454,446✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
13,039,257✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
2,418,723✔
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
2,419,398✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
15,459,313✔
109

110
  if (hasTypeMods) {
15,452,627✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
51,373✔
112
    if (ret != NULL) {
51,373✔
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
51,373✔
114
    }
115
    return ret;
51,373✔
116
  }
117
  return NULL;
15,401,254✔
118
}
119

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

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

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

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
56,088✔
151
  int32_t    i = 0, j = 0;
56,088✔
152
  for (i = 0; i < nCols; ++i) {
431,566✔
153
    while (j < pParam->nFuncs) {
682,404✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
655,918✔
155
        pFuncIds[i] = pParam->funcIds[j];
306,926✔
156
        break;
306,926✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
348,992✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
68,552✔
160
        break;
68,552✔
161
      }
162
      ++j;
280,440✔
163
    }
164
    if (j >= pParam->nFuncs) {
401,964✔
165
      for (; i < nCols; ++i) {
52,972✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
26,486✔
167
      }
168
      break;
26,486✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
56,088✔
172

173
  return 0;
56,088✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
17,864,012✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
17,864,012✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
35,728,864✔
179
  if (pWrapper->nCols == 0) {
17,864,230✔
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
35,728,243✔
184
  uDebug("decode cols:%d", pWrapper->nCols);
17,863,827✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
35,727,178✔
186
  if (pWrapper->pColRef == NULL) {
17,863,973✔
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
248,544,899✔
191
    SColRef *p = &pWrapper->pColRef[i];
230,674,676✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
461,363,846✔
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
461,368,161✔
194
    if (p->hasRef) {
230,684,503✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
137,441,596✔
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
137,443,572✔
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
137,446,079✔
198
    }
199
  }
200
  return 0;
17,865,038✔
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) {
698,747✔
220
  if (pSrc->nCols > 0) {
698,747✔
221
    pDst->nCols = pSrc->nCols;
698,747✔
222
    pDst->version = pSrc->version;
698,747✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
698,747✔
224
    if (NULL == pDst->pColRef) {
698,747✔
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
698,747✔
228
  }
229
  return 0;
698,747✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
45,108,744✔
233
  int32_t code = 0;
45,108,744✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
90,261,483✔
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
90,297,798✔
236
  uTrace("encode cols:%d", pw->nCols);
45,145,059✔
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;
45,235,011✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
44,972,319✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
44,972,319✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
44,987,020✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
852,734,041✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
852,734,041✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
1,705,513,768✔
252
  if (pWrapper->nCols == 0) {
852,622,647✔
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
1,705,401,885✔
257
  uDebug("dencode cols:%d", pWrapper->nCols);
852,793,502✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
1,705,382,906✔
259
  if (pWrapper->pColCmpr == NULL) {
852,755,115✔
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;
853,052,823✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
242,534✔
273

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

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

284
  for (int32_t i = 0; i < pCmpr->nCols; i++) {
1,214,922✔
285
    SColCmpr *pColCmpr = &pCmpr->pColCmpr[i];
972,388✔
286
    SSchema  *pColSchema = &pSchema->pSchema[i];
972,388✔
287
    pColCmpr->id = pColSchema->colId;
972,388✔
288
    pColCmpr->alg = createDefaultColCmprByType(pColSchema->type);
972,388✔
289
  }
290
  return 0;
242,534✔
291
}
292

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
176,133,653✔
294
  if (pSrc->nCols > 0) {
176,133,653✔
295
    pDst->nCols = pSrc->nCols;
159,079,864✔
296
    pDst->version = pSrc->version;
159,078,897✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
159,073,048✔
298
    if (NULL == pDst->pColCmpr) {
159,060,602✔
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
159,056,220✔
302
  }
303
  return 0;
176,124,026✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
176,800,944✔
307
  if (pColRef) {
176,800,944✔
308
    taosMemoryFreeClear(pColRef->pColRef);
176,804,562✔
309
  }
310
}
176,821,581✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
176,794,719✔
313
  if (pCmpr) {
176,794,719✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
176,801,808✔
315
  }
316
}
176,819,988✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
188,625,916✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
188,625,916✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
377,297,686✔
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
377,229,482✔
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
377,171,465✔
323

324
  if (pME->type > 0) {
188,591,101✔
325
    if (pME->name == NULL) {
184,529,216✔
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
369,166,355✔
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
184,611,561✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
65,701,017✔
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
65,772,011✔
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
65,809,374✔
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
32,876,848✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
162,032✔
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
151,696,855✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
277,429,665✔
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
277,433,637✔
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
277,432,975✔
342
      if (pME->ctbEntry.commentLen > 0) {
138,717,977✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
15,528✔
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
277,413,845✔
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
138,704,317✔
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
12,991,214✔
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
25,982,428✔
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
25,982,428✔
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
25,982,428✔
351
      if (pME->ntbEntry.commentLen > 0) {
12,991,214✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
14,728✔
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
25,982,428✔
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
25,982,428✔
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) {
184,576,321✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,370,493✔
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
183,262,385✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
32,849,713✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
150,428,684✔
368
        if (pME->colCmpr.nCols != 0) {
12,316,780✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
12,074,246✔
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
242,534✔
372
          SColCmprWrapper colCmprs = {0};
242,534✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
242,534✔
374
          if (code != 0) {
242,534✔
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
242,534✔
379
          taosMemoryFree(colCmprs.pColCmpr);
242,534✔
380
          TAOS_CHECK_RETURN(code);
242,534✔
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
184,588,369✔
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
188,624,792✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
65,684,557✔
388
  }
389

390
  tEndEncode(pCoder);
188,491,116✔
391
  return 0;
188,550,768✔
392
}
393

394
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
1,477,148,915✔
395
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
1,477,148,915✔
396
  TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->version));
2,147,483,647✔
397
  TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->type));
2,147,483,647✔
398
  TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->uid));
2,147,483,647✔
399

400
  if (headerOnly) {
1,477,647,648✔
401
    tEndDecode(pCoder);
111,174✔
402
    return 0;
111,174✔
403
  }
404

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

408
    if (pME->type == TSDB_SUPER_TABLE) {
1,477,405,787✔
409
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,381,762,316✔
410
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,381,955,328✔
411
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,381,782,401✔
412
      if (TABLE_IS_ROLLUP(pME->flags)) {
690,814,856✔
413
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
596,714✔
414
      }
415
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
786,216,787✔
416
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
1,240,090,648✔
417
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
1,240,267,424✔
418
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
1,240,063,525✔
419
      if (pME->ctbEntry.commentLen > 0) {
619,932,499✔
420
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
21,974✔
421
      }
422
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
1,240,024,028✔
423
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
620,121,821✔
424
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
166,360,233✔
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
332,771,078✔
426
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
332,790,206✔
427
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
332,763,743✔
428
      if (pME->ntbEntry.commentLen > 0) {
166,369,255✔
429
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
22,082✔
430
      }
431
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
332,728,941✔
432
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
332,760,347✔
433
    } else if (pME->type == TSDB_TSMA_TABLE) {
×
434
      pME->smaEntry.tsma = tDecoderMalloc(pCoder, sizeof(STSma));
×
435
      if (!pME->smaEntry.tsma) {
×
436
        return terrno;
×
437
      }
438
      TAOS_CHECK_RETURN(tDecodeTSma(pCoder, pME->smaEntry.tsma, true));
×
439
    } else {
440
      metaError("meta/entry: invalide table type: %" PRId8 " decode failed.", pME->type);
×
441
      return TSDB_CODE_INVALID_PARA;
×
442
    }
443
    if (pME->type == TSDB_SUPER_TABLE) {
1,477,226,284✔
444
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
690,949,853✔
445
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
690,887,141✔
446

447
        if (pME->colCmpr.nCols == 0) {
690,981,971✔
448
          TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->stbEntry.schemaRow));
×
449
        }
450
      } else {
451
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->stbEntry.schemaRow));
×
452
        TABLE_SET_COL_COMPRESSED(pME->flags);
×
453
      }
454
    } else if (pME->type == TSDB_NORMAL_TABLE) {
786,114,922✔
455
      if (!tDecodeIsEnd(pCoder)) {
162,003,077✔
456
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
162,008,116✔
457
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
162,008,116✔
458
        if (pME->colCmpr.nCols == 0) {
162,005,990✔
459
          TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
460
        }
461
      } else {
UNCOV
462
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
UNCOV
463
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
464
      }
465
      TABLE_SET_COL_COMPRESSED(pME->flags);
162,000,538✔
466
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
624,110,149✔
467
      if (!tDecodeIsEnd(pCoder)) {
17,861,728✔
468
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
17,863,256✔
469
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
17,866,312✔
470
      } else {
471
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
UNCOV
472
        if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
×
473
           TAOS_CHECK_RETURN(metatInitDefaultSColRefWrapper(pCoder, &pME->colRef, &pME->ntbEntry.schemaRow));
×
474
        }
475
      }
476
    }
477
    if (!tDecodeIsEnd(pCoder)) {
1,477,176,877✔
478
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
695,752,705✔
479
    } else {
480
      pME->pExtSchemas = NULL;
781,424,172✔
481
    }
482
  }
483
  if (pME->type == TSDB_SUPER_TABLE) {
1,477,050,351✔
484
    if (!tDecodeIsEnd(pCoder)) {
690,751,360✔
485
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,381,731,235✔
486
    }
487
  }
488

489

490
  tEndDecode(pCoder);
1,477,198,915✔
491
  return 0;
1,477,165,023✔
492
}
493

494
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
1,477,052,753✔
495

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

501
  pDst->nCols = pSrc->nCols;
310,342,521✔
502
  pDst->version = pSrc->version;
310,335,602✔
503
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
310,325,578✔
504
  if (pDst->pSchema == NULL) {
310,252,245✔
505
    return terrno;
×
506
  }
507
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
310,270,463✔
508
  return TSDB_CODE_SUCCESS;
310,349,542✔
509
}
510

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

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

539
  return TSDB_CODE_SUCCESS;
91,143✔
540
}
541

542
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
310,306,421✔
543
  if (pSchema) {
310,306,421✔
544
    taosMemoryFreeClear(pSchema->pSchema);
310,314,233✔
545
  }
546
}
310,340,876✔
547

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

561
void metaCloneEntryFree(SMetaEntry **ppEntry) {
176,877,587✔
562
  if (ppEntry == NULL || *ppEntry == NULL) {
176,877,587✔
563
    return;
78,812✔
564
  }
565

566
  taosMemoryFreeClear((*ppEntry)->name);
176,812,708✔
567

568
  if ((*ppEntry)->type < 0) {
176,818,948✔
569
    taosMemoryFreeClear(*ppEntry);
×
570
    return;
×
571
  }
572

573
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
176,788,219✔
574
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
150,783,872✔
575
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
150,786,261✔
576
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
150,781,236✔
577
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
137,883✔
578
    }
579
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
26,035,512✔
580
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
17,289,100✔
581
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
17,288,476✔
582
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
8,747,036✔
583
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
8,747,036✔
584
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
8,747,036✔
585
  } else {
586
    return;
×
587
  }
588
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
176,815,714✔
589
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
176,811,997✔
590
  metaCloneColRefFree(&(*ppEntry)->colRef);
176,817,584✔
591

592
  taosMemoryFreeClear(*ppEntry);
176,782,432✔
593
  return;
176,807,308✔
594
}
595

596
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
176,820,100✔
597
  int32_t code = TSDB_CODE_SUCCESS;
176,820,100✔
598

599
  if (NULL == pEntry || NULL == ppEntry) {
176,820,100✔
600
    return TSDB_CODE_INVALID_PARA;
×
601
  }
602

603
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
176,837,379✔
604
  if (NULL == *ppEntry) {
176,780,986✔
605
    return terrno;
×
606
  }
607

608
  (*ppEntry)->version = pEntry->version;
176,809,756✔
609
  (*ppEntry)->type = pEntry->type;
176,817,868✔
610
  (*ppEntry)->uid = pEntry->uid;
176,802,256✔
611

612
  if (pEntry->type < 0) {
176,821,654✔
613
    return TSDB_CODE_SUCCESS;
×
614
  }
615

616
  if (pEntry->name) {
176,798,783✔
617
    (*ppEntry)->name = tstrdup(pEntry->name);
176,813,702✔
618
    if (NULL == (*ppEntry)->name) {
176,821,182✔
619
      code = terrno;
×
620
      metaCloneEntryFree(ppEntry);
×
621
      return code;
×
622
    }
623
  }
624

625
  if (pEntry->type == TSDB_SUPER_TABLE) {
176,803,461✔
626
    (*ppEntry)->flags = pEntry->flags;
150,780,468✔
627

628
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
150,768,795✔
629
    if (code) {
150,800,523✔
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633

634
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
150,800,523✔
635
    if (code) {
150,809,137✔
636
      metaCloneEntryFree(ppEntry);
×
637
      return code;
×
638
    }
639
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
150,809,137✔
640
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
91,143✔
641
      if (code) {
91,143✔
642
        metaCloneEntryFree(ppEntry);
×
643
        return code;
×
644
      }
645
    }
646
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
150,806,287✔
647
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
26,035,466✔
648
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
17,289,100✔
649
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
17,288,430✔
650
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
17,289,100✔
651

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

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

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

685
    // comment
686
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
8,747,036✔
687
    if (pEntry->ntbEntry.commentLen > 0) {
8,747,036✔
688
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
4,621✔
689
      if (NULL == (*ppEntry)->ntbEntry.comment) {
4,621✔
690
        code = terrno;
×
691
        metaCloneEntryFree(ppEntry);
×
692
        return code;
×
693
      }
694
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
4,621✔
695
    }
696
  } else {
697
    return TSDB_CODE_INVALID_PARA;
×
698
  }
699

700
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
176,826,992✔
701
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
708,400✔
702
    if (code) {
698,747✔
703
      metaCloneEntryFree(ppEntry);
×
704
      return code;
×
705
    }
706
  } else {
707
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
176,122,100✔
708
    if (code) {
176,120,086✔
709
      metaCloneEntryFree(ppEntry);
×
710
      return code;
×
711
    }
712
  }
713
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
176,818,833✔
714
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
12,687,148✔
715
    if (!(*ppEntry)->pExtSchemas) {
12,684,341✔
716
      code = terrno;
×
717
      metaCloneEntryFree(ppEntry);
×
718
      return code;
×
719
    }
720
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
12,684,035✔
721
  }
722

723
  return code;
176,803,196✔
724
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc