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

taosdata / TDengine / #4849

13 Nov 2025 07:17AM UTC coverage: 63.829% (+0.6%) from 63.27%
#4849

push

travis-ci

guanshengliang
test: enable auto upload

148928 of 233322 relevant lines covered (63.83%)

115593255.19 hits per line

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

80.71
/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) {
669,175,065✔
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;
36,077,281✔
26
    }
27
  }
28
  return false;
632,682,752✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
176,184,138✔
32
  if (pME->pExtSchemas) {
176,184,138✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
13,604,243✔
34
    bool                  hasTypeMods = false;
13,604,243✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
13,604,243✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
13,470,832✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
130,380✔
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
130,380✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
13,602,296✔
43

44
    for (int32_t i = 0; i < pSchWrapper->nCols && hasTypeMods; ++i) {
1,947,152,087✔
45
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->pExtSchemas[i].typeMod));
2,147,483,647✔
46
    }
47
  }
48
  return 0;
176,178,226✔
49
}
50

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

57
  for (int32_t i = 0; i < pw->nCols; i++) {
12,172,270✔
58
    SColRef *p = &pw->pColRef[i];
10,944,340✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
21,888,680✔
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
21,888,680✔
61
    if (p->hasRef) {
10,944,340✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
12,162,896✔
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
12,162,896✔
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
12,162,896✔
65
    }
66
  }
67
  return 0;
1,227,930✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
641,507,326✔
71
  bool hasExtSchema = false;
641,507,326✔
72
  SSchemaWrapper* pSchWrapper = NULL;
641,507,326✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
641,507,326✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
636,762,582✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
4,714,644✔
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
4,715,065✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
641,511,835✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
641,564,230✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
50,703,906✔
84
    if (pME->pExtSchemas == NULL) {
25,356,421✔
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;
616,207,212✔
93
  }
94

95
  return 0;
641,678,891✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
14,091,870✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
14,091,870✔
100
  bool                  hasTypeMods = false;
14,091,870✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
14,091,870✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
12,437,822✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
1,657,326✔
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
1,659,972✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
14,097,408✔
109

110
  if (hasTypeMods) {
14,091,362✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
49,917✔
112
    if (ret != NULL) {
49,917✔
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
49,917✔
114
    }
115
    return ret;
49,490✔
116
  }
117
  return NULL;
14,041,445✔
118
}
119

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

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

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

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
54,144✔
151
  int32_t    i = 0, j = 0;
54,144✔
152
  for (i = 0; i < nCols; ++i) {
416,608✔
153
    while (j < pParam->nFuncs) {
658,752✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
633,184✔
155
        pFuncIds[i] = pParam->funcIds[j];
296,288✔
156
        break;
296,288✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
336,896✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
66,176✔
160
        break;
66,176✔
161
      }
162
      ++j;
270,720✔
163
    }
164
    if (j >= pParam->nFuncs) {
388,032✔
165
      for (; i < nCols; ++i) {
51,136✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
25,568✔
167
      }
168
      break;
25,568✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
54,144✔
172

173
  return 0;
54,144✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
13,646,196✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
13,646,196✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
27,295,184✔
179
  if (pWrapper->nCols == 0) {
13,647,744✔
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
27,292,358✔
184
  uDebug("decode cols:%d", pWrapper->nCols);
13,646,805✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
27,295,650✔
186
  if (pWrapper->pColRef == NULL) {
13,649,398✔
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
219,167,277✔
191
    SColRef *p = &pWrapper->pColRef[i];
205,513,727✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
411,063,498✔
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
411,079,944✔
194
    if (p->hasRef) {
205,545,575✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
118,897,273✔
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
118,893,827✔
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
118,895,394✔
198
    }
199
  }
200
  return 0;
13,648,787✔
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) {
680,248✔
220
  if (pSrc->nCols > 0) {
680,248✔
221
    pDst->nCols = pSrc->nCols;
680,248✔
222
    pDst->version = pSrc->version;
680,248✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
680,248✔
224
    if (NULL == pDst->pColRef) {
680,248✔
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
680,248✔
228
  }
229
  return 0;
680,248✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
42,204,145✔
233
  int32_t code = 0;
42,204,145✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
84,393,063✔
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
84,418,709✔
236
  uTrace("encode cols:%d", pw->nCols);
42,229,791✔
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;
42,261,902✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
42,099,560✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
42,099,560✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
42,104,648✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
785,821,361✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
785,821,361✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
1,571,615,900✔
252
  if (pWrapper->nCols == 0) {
785,693,584✔
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
1,571,603,077✔
257
  uDebug("dencode cols:%d", pWrapper->nCols);
785,849,413✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
1,571,526,009✔
259
  if (pWrapper->pColCmpr == NULL) {
785,897,822✔
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;
786,069,719✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
167,198✔
273

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

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

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

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
168,714,299✔
294
  if (pSrc->nCols > 0) {
168,714,299✔
295
    pDst->nCols = pSrc->nCols;
152,218,730✔
296
    pDst->version = pSrc->version;
152,211,472✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
152,220,836✔
298
    if (NULL == pDst->pColCmpr) {
152,206,547✔
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
152,210,079✔
302
  }
303
  return 0;
168,728,478✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
169,392,776✔
307
  if (pColRef) {
169,392,776✔
308
    taosMemoryFreeClear(pColRef->pColRef);
169,402,857✔
309
  }
310
}
169,399,877✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
169,387,128✔
313
  if (pCmpr) {
169,387,128✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
169,398,423✔
315
  }
316
}
169,383,607✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
180,069,236✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
180,069,236✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
360,193,889✔
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
360,156,789✔
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
360,126,787✔
323

324
  if (pME->type > 0) {
180,070,897✔
325
    if (pME->name == NULL) {
176,235,262✔
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
352,462,646✔
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
176,246,058✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
61,229,479✔
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
61,267,110✔
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
61,268,926✔
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
30,600,171✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
152,656✔
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
145,604,818✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
266,643,790✔
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
266,646,215✔
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
266,649,549✔
342
      if (pME->ctbEntry.commentLen > 0) {
133,327,133✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
14,972✔
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
266,643,859✔
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
133,318,539✔
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
12,291,174✔
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
24,582,348✔
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
24,582,348✔
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
24,582,348✔
351
      if (pME->ntbEntry.commentLen > 0) {
12,291,174✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
13,996✔
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
24,582,348✔
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
24,582,348✔
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) {
176,208,664✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,288,485✔
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
174,964,275✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
30,593,788✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
144,410,520✔
368
        if (pME->colCmpr.nCols != 0) {
11,634,898✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
11,467,700✔
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
167,198✔
372
          SColCmprWrapper colCmprs = {0};
167,198✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
167,198✔
374
          if (code != 0) {
167,198✔
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
167,198✔
379
          taosMemoryFree(colCmprs.pColCmpr);
167,198✔
380
          TAOS_CHECK_RETURN(code);
167,198✔
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
176,218,365✔
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
180,043,530✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
61,209,915✔
388
  }
389

390
  tEndEncode(pCoder);
180,043,636✔
391
  return 0;
179,998,744✔
392
}
393

394
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
1,369,941,568✔
395
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
1,369,941,568✔
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,370,348,175✔
401
    tEndDecode(pCoder);
100,600✔
402
    return 0;
100,600✔
403
  }
404

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

408
    if (pME->type == TSDB_SUPER_TABLE) {
1,370,144,972✔
409
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,273,896,500✔
410
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,273,915,854✔
411
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,273,792,776✔
412
      if (TABLE_IS_ROLLUP(pME->flags)) {
636,885,499✔
413
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
574,528✔
414
      }
415
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
732,959,358✔
416
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
1,159,747,752✔
417
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
1,159,874,015✔
418
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
1,159,694,065✔
419
      if (pME->ctbEntry.commentLen > 0) {
579,757,391✔
420
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
21,110✔
421
      }
422
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
1,159,737,349✔
423
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
579,935,269✔
424
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
153,281,559✔
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
306,596,097✔
426
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
306,620,123✔
427
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
306,598,976✔
428
      if (pME->ntbEntry.commentLen > 0) {
153,286,264✔
429
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
20,852✔
430
      }
431
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
306,580,838✔
432
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
306,602,343✔
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,370,033,415✔
444
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
636,940,015✔
445
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
636,913,235✔
446

447
        if (pME->colCmpr.nCols == 0) {
636,889,870✔
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) {
732,907,434✔
455
      if (!tDecodeIsEnd(pCoder)) {
149,038,762✔
456
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
149,045,800✔
457
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
149,045,800✔
458
        if (pME->colCmpr.nCols == 0) {
149,035,138✔
459
          TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
460
        }
461
      } else {
462
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
463
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
464
      }
465
      TABLE_SET_COL_COMPRESSED(pME->flags);
149,038,965✔
466
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
583,894,518✔
467
      if (!tDecodeIsEnd(pCoder)) {
13,647,246✔
468
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
13,647,850✔
469
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
13,647,850✔
470
      } else {
471
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
472
        if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
170✔
473
           TAOS_CHECK_RETURN(metatInitDefaultSColRefWrapper(pCoder, &pME->colRef, &pME->ntbEntry.schemaRow));
×
474
        }
475
      }
476
    }
477
    if (!tDecodeIsEnd(pCoder)) {
1,370,023,932✔
478
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
641,620,925✔
479
    } else {
480
      pME->pExtSchemas = NULL;
728,403,007✔
481
    }
482
  }
483
  if (pME->type == TSDB_SUPER_TABLE) {
1,369,938,744✔
484
    if (!tDecodeIsEnd(pCoder)) {
636,839,207✔
485
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,273,675,896✔
486
    }
487
  }
488

489

490
  tEndDecode(pCoder);
1,370,014,992✔
491
  return 0;
1,369,768,435✔
492
}
493

494
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
1,369,876,578✔
495

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

501
  pDst->nCols = pSrc->nCols;
296,910,965✔
502
  pDst->version = pSrc->version;
296,909,991✔
503
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
296,865,265✔
504
  if (pDst->pSchema == NULL) {
296,812,832✔
505
    return terrno;
×
506
  }
507
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
296,830,138✔
508
  return TSDB_CODE_SUCCESS;
296,900,247✔
509
}
510

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

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

539
  return TSDB_CODE_SUCCESS;
87,232✔
540
}
541

542
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
296,873,899✔
543
  if (pSchema) {
296,873,899✔
544
    taosMemoryFreeClear(pSchema->pSchema);
296,884,880✔
545
  }
546
}
296,868,929✔
547

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

561
void metaCloneEntryFree(SMetaEntry **ppEntry) {
169,468,476✔
562
  if (ppEntry == NULL || *ppEntry == NULL) {
169,468,476✔
563
    return;
76,718✔
564
  }
565

566
  taosMemoryFreeClear((*ppEntry)->name);
169,395,566✔
567

568
  if ((*ppEntry)->type < 0) {
169,405,440✔
569
    taosMemoryFreeClear(*ppEntry);
×
570
    return;
×
571
  }
572

573
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
169,392,892✔
574
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
144,230,175✔
575
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
144,207,632✔
576
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
144,207,965✔
577
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
133,104✔
578
    }
579
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
25,171,287✔
580
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
16,723,644✔
581
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
16,723,275✔
582
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
8,450,031✔
583
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
8,450,031✔
584
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
8,450,031✔
585
  } else {
586
    return;
×
587
  }
588
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
169,389,492✔
589
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
169,382,298✔
590
  metaCloneColRefFree(&(*ppEntry)->colRef);
169,382,910✔
591

592
  taosMemoryFreeClear(*ppEntry);
169,372,009✔
593
  return;
169,384,875✔
594
}
595

596
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
169,388,584✔
597
  int32_t code = TSDB_CODE_SUCCESS;
169,388,584✔
598

599
  if (NULL == pEntry || NULL == ppEntry) {
169,388,584✔
600
    return TSDB_CODE_INVALID_PARA;
×
601
  }
602

603
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
169,404,876✔
604
  if (NULL == *ppEntry) {
169,354,847✔
605
    return terrno;
×
606
  }
607

608
  (*ppEntry)->version = pEntry->version;
169,378,262✔
609
  (*ppEntry)->type = pEntry->type;
169,398,409✔
610
  (*ppEntry)->uid = pEntry->uid;
169,383,365✔
611

612
  if (pEntry->type < 0) {
169,402,701✔
613
    return TSDB_CODE_SUCCESS;
×
614
  }
615

616
  if (pEntry->name) {
169,378,513✔
617
    (*ppEntry)->name = tstrdup(pEntry->name);
169,399,567✔
618
    if (NULL == (*ppEntry)->name) {
169,398,606✔
619
      code = terrno;
×
620
      metaCloneEntryFree(ppEntry);
×
621
      return code;
×
622
    }
623
  }
624

625
  if (pEntry->type == TSDB_SUPER_TABLE) {
169,389,009✔
626
    (*ppEntry)->flags = pEntry->flags;
144,224,173✔
627

628
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
144,213,519✔
629
    if (code) {
144,223,977✔
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633

634
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
144,223,977✔
635
    if (code) {
144,234,112✔
636
      metaCloneEntryFree(ppEntry);
×
637
      return code;
×
638
    }
639
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
144,234,112✔
640
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
88,736✔
641
      if (code) {
87,984✔
642
        metaCloneEntryFree(ppEntry);
×
643
        return code;
×
644
      }
645
    }
646
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
144,233,362✔
647
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
25,174,327✔
648
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
16,724,235✔
649
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
16,724,296✔
650
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
16,723,533✔
651

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

664
    // tags
665
    STag *pTags = (STag *)pEntry->ctbEntry.pTags;
16,724,235✔
666
    (*ppEntry)->ctbEntry.pTags = taosMemoryCalloc(1, pTags->len);
16,723,533✔
667
    if (NULL == (*ppEntry)->ctbEntry.pTags) {
16,724,296✔
668
      code = terrno;
×
669
      metaCloneEntryFree(ppEntry);
×
670
      return code;
×
671
    }
672
    memcpy((*ppEntry)->ctbEntry.pTags, pEntry->ctbEntry.pTags, pTags->len);
16,723,655✔
673
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
8,450,031✔
674
    (*ppEntry)->ntbEntry.btime = pEntry->ntbEntry.btime;
8,450,031✔
675
    (*ppEntry)->ntbEntry.ttlDays = pEntry->ntbEntry.ttlDays;
8,450,031✔
676
    (*ppEntry)->ntbEntry.ncid = pEntry->ntbEntry.ncid;
8,450,031✔
677

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

685
    // comment
686
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
8,450,031✔
687
    if (pEntry->ntbEntry.commentLen > 0) {
8,450,031✔
688
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
4,394✔
689
      if (NULL == (*ppEntry)->ntbEntry.comment) {
4,394✔
690
        code = terrno;
×
691
        metaCloneEntryFree(ppEntry);
×
692
        return code;
×
693
      }
694
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
4,394✔
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) {
169,397,830✔
701
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
702,045✔
702
    if (code) {
680,248✔
703
      metaCloneEntryFree(ppEntry);
×
704
      return code;
×
705
    }
706
  } else {
707
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
168,702,856✔
708
    if (code) {
168,727,386✔
709
      metaCloneEntryFree(ppEntry);
×
710
      return code;
×
711
    }
712
  }
713
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
169,407,634✔
714
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
11,495,696✔
715
    if (!(*ppEntry)->pExtSchemas) {
11,492,221✔
716
      code = terrno;
×
717
      metaCloneEntryFree(ppEntry);
×
718
      return code;
×
719
    }
720
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
11,491,413✔
721
  }
722

723
  return code;
169,391,751✔
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