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

taosdata / TDengine / #4911

04 Jan 2026 09:05AM UTC coverage: 65.028% (-0.8%) from 65.864%
#4911

push

travis-ci

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

1206 of 4524 new or added lines in 22 files covered. (26.66%)

1517 existing lines in 134 files now uncovered.

195276 of 300296 relevant lines covered (65.03%)

116931714.52 hits per line

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

80.9
/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) {
984,449,616✔
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;
26,081,228✔
26
    }
27
  }
28
  return false;
956,755,365✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
187,705,103✔
32
  if (pME->pExtSchemas) {
187,705,103✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
11,380,283✔
34
    bool                  hasTypeMods = false;
11,380,283✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
11,380,283✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
11,304,866✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
85,808✔
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
85,808✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
11,394,594✔
43

44
    for (int32_t i = 0; i < pSchWrapper->nCols && hasTypeMods; ++i) {
1,056,366,640✔
45
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->pExtSchemas[i].typeMod));
2,089,958,634✔
46
    }
47
  }
48
  return 0;
187,733,150✔
49
}
50

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

57
  for (int32_t i = 0; i < pw->nCols; i++) {
229,507,146✔
58
    SColRef *p = &pw->pColRef[i];
228,448,338✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
456,896,676✔
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
456,896,676✔
61
    if (p->hasRef) {
228,448,338✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
273,067,948✔
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
273,067,948✔
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
273,067,948✔
65
    }
66
  }
67
  return 0;
1,058,808✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
933,132,732✔
71
  bool hasExtSchema = false;
933,132,732✔
72
  SSchemaWrapper* pSchWrapper = NULL;
933,132,732✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
933,132,732✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
735,125,495✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
198,189,547✔
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
198,235,398✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
933,278,792✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
933,284,983✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
35,156,266✔
84
    if (pME->pExtSchemas == NULL) {
17,582,478✔
85
      return terrno;
×
86
    }
87

88
    for (int32_t i = 0; i < pSchWrapper->nCols && hasExtSchema; i++) {
1,160,755,354✔
89
      TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pME->pExtSchemas[i].typeMod));
2,147,483,647✔
90
    }
91
  } else {
92
    pME->pExtSchemas = NULL;
915,700,547✔
93
  }
94

95
  return 0;
933,283,956✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
39,952,087✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
39,952,087✔
100
  bool                  hasTypeMods = false;
39,952,087✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
39,952,087✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
28,920,966✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
11,043,029✔
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
11,048,549✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
39,969,429✔
109

110
  if (hasTypeMods) {
39,940,937✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
37,176✔
112
    if (ret != NULL) {
37,176✔
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
37,176✔
114
    }
115
    return ret;
37,176✔
116
  }
117
  return NULL;
39,903,761✔
118
}
119

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

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

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

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
25,452✔
151
  int32_t    i = 0, j = 0;
25,452✔
152
  for (i = 0; i < nCols; ++i) {
195,839✔
153
    while (j < pParam->nFuncs) {
309,666✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
297,647✔
155
        pFuncIds[i] = pParam->funcIds[j];
139,279✔
156
        break;
139,279✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
158,368✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
31,108✔
160
        break;
31,108✔
161
      }
162
      ++j;
127,260✔
163
    }
164
    if (j >= pParam->nFuncs) {
182,406✔
165
      for (; i < nCols; ++i) {
24,038✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
12,019✔
167
      }
168
      break;
12,019✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
25,452✔
172

173
  return 0;
25,452✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
7,388,247✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
7,388,247✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
14,776,494✔
179
  if (pWrapper->nCols == 0) {
7,388,247✔
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
14,776,494✔
184
  uDebug("decode cols:%d", pWrapper->nCols);
7,388,247✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
14,776,494✔
186
  if (pWrapper->pColRef == NULL) {
7,388,247✔
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
244,223,795✔
191
    SColRef *p = &pWrapper->pColRef[i];
236,832,728✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
473,679,415✔
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
473,691,100✔
194
    if (p->hasRef) {
236,849,642✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
135,727,488✔
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
135,727,443✔
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
135,727,488✔
198
    }
199
  }
200
  return 0;
7,388,247✔
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) {
622,470✔
220
  if (pSrc->nCols > 0) {
622,470✔
221
    pDst->nCols = pSrc->nCols;
622,470✔
222
    pDst->version = pSrc->version;
622,470✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
622,470✔
224
    if (NULL == pDst->pColRef) {
622,470✔
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
622,470✔
228
  }
229
  return 0;
622,470✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
58,675,418✔
233
  int32_t code = 0;
58,675,418✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
117,295,599✔
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
117,301,010✔
236
  uTrace("encode cols:%d", pw->nCols);
58,680,829✔
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;
58,719,245✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
58,530,510✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
58,530,510✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
58,563,423✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
933,224,348✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
933,224,348✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
1,866,776,383✔
252
  if (pWrapper->nCols == 0) {
933,232,968✔
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
1,866,559,790✔
257
  uDebug("dencode cols:%d", pWrapper->nCols);
933,446,080✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
1,866,779,967✔
259
  if (pWrapper->pColCmpr == NULL) {
933,405,135✔
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;
933,708,324✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
167,482✔
273

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

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

284
  for (int32_t i = 0; i < pCmpr->nCols; i++) {
870,640✔
285
    SColCmpr *pColCmpr = &pCmpr->pColCmpr[i];
703,158✔
286
    SSchema  *pColSchema = &pSchema->pSchema[i];
703,158✔
287
    pColCmpr->id = pColSchema->colId;
703,158✔
288
    pColCmpr->alg = createDefaultColCmprByType(pColSchema->type);
703,158✔
289
  }
290
  return 0;
167,482✔
291
}
292

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
160,652,817✔
294
  if (pSrc->nCols > 0) {
160,652,817✔
295
    pDst->nCols = pSrc->nCols;
144,721,585✔
296
    pDst->version = pSrc->version;
144,722,957✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
144,702,468✔
298
    if (NULL == pDst->pColCmpr) {
144,705,414✔
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
144,684,217✔
302
  }
303
  return 0;
160,656,781✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
161,267,559✔
307
  if (pColRef) {
161,267,559✔
308
    taosMemoryFreeClear(pColRef->pColRef);
161,276,038✔
309
  }
310
}
161,274,184✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
161,269,421✔
313
  if (pCmpr) {
161,269,421✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
161,275,578✔
315
  }
316
}
161,262,560✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
192,017,646✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
192,017,646✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
384,060,659✔
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
384,034,012✔
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
384,004,240✔
323

324
  if (pME->type > 0) {
192,004,566✔
325
    if (pME->name == NULL) {
187,737,217✔
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
375,403,394✔
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
187,678,209✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
46,546,536✔
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
46,631,235✔
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
46,601,427✔
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
23,259,985✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
72,821✔
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
164,406,755✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
256,846,493✔
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
256,841,149✔
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
256,839,493✔
342
      if (pME->ctbEntry.commentLen > 0) {
128,424,350✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
27,168✔
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
256,842,203✔
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
128,419,375✔
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
35,993,764✔
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
71,987,528✔
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
71,987,528✔
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
71,987,528✔
351
      if (pME->ntbEntry.commentLen > 0) {
35,993,764✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
37,488✔
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
71,987,528✔
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
71,987,528✔
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) {
187,704,008✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,142,186✔
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
186,594,861✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
23,294,109✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
163,388,744✔
368
        if (pME->colCmpr.nCols != 0) {
35,407,126✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
35,239,644✔
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
167,482✔
372
          SColCmprWrapper colCmprs = {0};
167,482✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
167,482✔
374
          if (code != 0) {
167,482✔
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
167,482✔
379
          taosMemoryFree(colCmprs.pColCmpr);
167,482✔
380
          TAOS_CHECK_RETURN(code);
167,482✔
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
187,730,117✔
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
192,001,961✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
46,602,605✔
388
    TAOS_CHECK_RETURN(tEncodeI64v(pCoder, pME->stbEntry.ownerId));
46,533,400✔
389
  } else if (pME->type == TSDB_NORMAL_TABLE) {
168,699,389✔
390
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.ownerId));
70,814,252✔
391
  }
392

393
  tEndEncode(pCoder);
191,970,299✔
394
  return 0;
192,016,515✔
395
}
396

397
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
1,506,406,415✔
398
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
1,506,406,415✔
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,506,906,182✔
404
    tEndDecode(pCoder);
318,266✔
405
    return 0;
318,266✔
406
  }
407

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

411
    if (pME->type == TSDB_SUPER_TABLE) {
1,506,448,997✔
412
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,470,507,730✔
413
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,470,548,262✔
414
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,470,388,928✔
415
      if (TABLE_IS_ROLLUP(pME->flags)) {
735,163,339✔
416
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
270,781✔
417
      }
418
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
770,823,558✔
419
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
1,142,281,989✔
420
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
1,142,299,130✔
421
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
1,142,130,445✔
422
      if (pME->ctbEntry.commentLen > 0) {
570,981,064✔
423
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
46,994✔
424
      }
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
1,142,175,764✔
426
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
571,153,905✔
427
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
199,935,580✔
428
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
400,040,993✔
429
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
400,154,719✔
430
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
399,991,119✔
431
      if (pME->ntbEntry.commentLen > 0) {
199,914,841✔
432
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
69,230✔
433
      }
434
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
399,919,309✔
435
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
400,024,329✔
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,506,339,299✔
447
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
735,242,048✔
448
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
735,242,291✔
449

450
        if (pME->colCmpr.nCols == 0) {
735,149,548✔
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) {
770,809,014✔
458
      if (!tDecodeIsEnd(pCoder)) {
198,194,955✔
459
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
198,272,258✔
460
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
198,272,258✔
461
        if (pME->colCmpr.nCols == 0) {
198,271,619✔
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);
198,303,872✔
469
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
572,611,183✔
470
      if (!tDecodeIsEnd(pCoder)) {
7,388,247✔
471
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
7,388,247✔
472
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
7,388,952✔
473
      } else {
UNCOV
474
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
UNCOV
475
        if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
×
476
           TAOS_CHECK_RETURN(metatInitDefaultSColRefWrapper(pCoder, &pME->colRef, &pME->ntbEntry.schemaRow));
×
477
        }
478
      }
479
    }
480
    if (!tDecodeIsEnd(pCoder)) {
1,506,297,512✔
481
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
933,547,186✔
482
    } else {
483
      pME->pExtSchemas = NULL;
572,750,326✔
484
    }
485
  }
486
  if (pME->type == TSDB_SUPER_TABLE) {
1,506,273,529✔
487
    if (!tDecodeIsEnd(pCoder)) {
735,123,221✔
488
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,470,266,899✔
489
    }
490
    if (!tDecodeIsEnd(pCoder)) {
735,077,965✔
491
      TAOS_CHECK_RETURN(tDecodeI64v(pCoder, &pME->stbEntry.ownerId));
1,470,137,226✔
492
    }
493
  } else if (pME->type == TSDB_NORMAL_TABLE) {
770,699,038✔
494
    if (!tDecodeIsEnd(pCoder)) {
198,295,967✔
495
      TAOS_CHECK_RETURN(tDecodeI64v(pCoder, &pME->ntbEntry.ownerId));
396,618,080✔
496
    }
497
  }
498

499

500
  tEndDecode(pCoder);
1,505,855,891✔
501
  return 0;
1,506,118,414✔
502
}
503

504
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
1,506,166,292✔
505

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

511
  pDst->nCols = pSrc->nCols;
282,389,742✔
512
  pDst->version = pSrc->version;
282,388,265✔
513
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
282,388,806✔
514
  if (pDst->pSchema == NULL) {
282,311,286✔
515
    return terrno;
×
516
  }
517
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
282,331,506✔
518
  return TSDB_CODE_SUCCESS;
282,404,715✔
519
}
520

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

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

549
  return TSDB_CODE_SUCCESS;
41,713✔
550
}
551

552
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
282,370,923✔
553
  if (pSchema) {
282,370,923✔
554
    taosMemoryFreeClear(pSchema->pSchema);
282,379,322✔
555
  }
556
}
282,371,897✔
557

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

571
void metaCloneEntryFree(SMetaEntry **ppEntry) {
161,336,977✔
572
  if (ppEntry == NULL || *ppEntry == NULL) {
161,336,977✔
573
    return;
69,975✔
574
  }
575

576
  taosMemoryFreeClear((*ppEntry)->name);
161,268,992✔
577

578
  if ((*ppEntry)->type < 0) {
161,275,768✔
579
    taosMemoryFreeClear(*ppEntry);
×
580
    return;
×
581
  }
582

583
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
161,270,572✔
584
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
137,259,259✔
585
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
137,241,499✔
586
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
137,244,745✔
587
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
62,216✔
588
    }
589
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
24,016,578✔
590
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
16,136,906✔
591
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
16,136,906✔
592
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
7,880,744✔
593
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
7,880,744✔
594
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
7,880,744✔
595
  } else {
596
    return;
×
597
  }
598
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
161,265,623✔
599
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
161,252,838✔
600
  metaCloneColRefFree(&(*ppEntry)->colRef);
161,257,147✔
601

602
  taosMemoryFreeClear(*ppEntry);
161,233,472✔
603
  return;
161,252,718✔
604
}
605

606
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
161,253,488✔
607
  int32_t code = TSDB_CODE_SUCCESS;
161,253,488✔
608

609
  if (NULL == pEntry || NULL == ppEntry) {
161,253,488✔
610
    return TSDB_CODE_INVALID_PARA;
×
611
  }
612

613
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
161,276,711✔
614
  if (NULL == *ppEntry) {
161,225,630✔
615
    return terrno;
×
616
  }
617

618
  (*ppEntry)->version = pEntry->version;
161,241,679✔
619
  (*ppEntry)->type = pEntry->type;
161,271,903✔
620
  (*ppEntry)->uid = pEntry->uid;
161,271,268✔
621

622
  if (pEntry->type < 0) {
161,259,553✔
623
    return TSDB_CODE_SUCCESS;
×
624
  }
625

626
  if (pEntry->name) {
161,261,084✔
627
    (*ppEntry)->name = tstrdup(pEntry->name);
161,270,729✔
628
    if (NULL == (*ppEntry)->name) {
161,281,421✔
629
      code = terrno;
×
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633
  }
634

635
  if (pEntry->type == TSDB_SUPER_TABLE) {
161,277,988✔
636
    (*ppEntry)->flags = pEntry->flags;
137,264,281✔
637

638
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
137,249,653✔
639
    if (code) {
137,260,312✔
640
      metaCloneEntryFree(ppEntry);
×
641
      return code;
×
642
    }
643

644
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
137,260,312✔
645
    if (code) {
137,272,214✔
646
      metaCloneEntryFree(ppEntry);
×
647
      return code;
×
648
    }
649
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
137,272,214✔
650
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
41,713✔
651
      if (code) {
41,713✔
652
        metaCloneEntryFree(ppEntry);
×
653
        return code;
×
654
      }
655
    }
656
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
137,268,091✔
657
    (*ppEntry)->stbEntry.ownerId = pEntry->stbEntry.ownerId;
137,260,451✔
658
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
24,017,650✔
659
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
16,136,906✔
660
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
16,136,653✔
661
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
16,136,906✔
662

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

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

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

697
    // comment
698
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
7,880,744✔
699
    if (pEntry->ntbEntry.commentLen > 0) {
7,880,744✔
700
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
11,617✔
701
      if (NULL == (*ppEntry)->ntbEntry.comment) {
11,617✔
702
        code = terrno;
×
703
        metaCloneEntryFree(ppEntry);
×
704
        return code;
×
705
      }
706
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
11,617✔
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) {
161,281,979✔
713
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
630,881✔
714
    if (code) {
622,470✔
715
      metaCloneEntryFree(ppEntry);
×
716
      return code;
×
717
    }
718
  } else {
719
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
160,654,031✔
720
    if (code) {
160,654,891✔
721
      metaCloneEntryFree(ppEntry);
×
722
      return code;
×
723
    }
724
  }
725
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
161,277,361✔
726
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
8,872,954✔
727
    if (!(*ppEntry)->pExtSchemas) {
8,872,582✔
728
      code = terrno;
×
729
      metaCloneEntryFree(ppEntry);
×
730
      return code;
×
731
    }
732
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
8,866,603✔
733
  }
734

735
  return code;
161,273,942✔
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