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

taosdata / TDengine / #4800

16 Oct 2025 09:19AM UTC coverage: 53.935% (-7.1%) from 61.083%
#4800

push

travis-ci

web-flow
Merge b32e3a393 into a190048d5

134724 of 323629 branches covered (41.63%)

Branch coverage included in aggregate %.

184803 of 268802 relevant lines covered (68.75%)

69058627.2 hits per line

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

65.36
/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) {
210,373,499✔
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;
8,231,258✔
26
    }
27
  }
28
  return false;
201,351,306✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
47,638,627✔
32
  if (pME->pExtSchemas) {
47,638,627✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
1,999,867✔
34
    bool                  hasTypeMods = false;
1,999,867✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
1,999,867✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
1,967,215✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
29,884!
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
29,884✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
1,997,370✔
43

44
    for (int32_t i = 0; i < pSchWrapper->nCols && hasTypeMods; ++i) {
188,806,248✔
45
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->pExtSchemas[i].typeMod));
373,618,014!
46
    }
47
  }
48
  return 0;
47,641,328✔
49
}
50

51
int meteEncodeColRefEntry(SEncoder *pCoder, const SMetaEntry *pME) {
662,114✔
52
  const SColRefWrapper *pw = &pME->colRef;
662,114✔
53
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
1,324,228!
54
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
1,324,228!
55
  uTrace("encode cols:%d", pw->nCols);
662,114!
56

57
  for (int32_t i = 0; i < pw->nCols; i++) {
6,438,530✔
58
    SColRef *p = &pw->pColRef[i];
5,776,416✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
11,552,832!
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
11,552,832!
61
    if (p->hasRef) {
5,776,416!
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
6,248,784!
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
6,248,784!
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
6,248,784!
65
    }
66
  }
67
  return 0;
662,114✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
205,484,454✔
71
  bool hasExtSchema = false;
205,484,454✔
72
  SSchemaWrapper* pSchWrapper = NULL;
205,484,454✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
205,484,454✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
205,494,004✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
15,323!
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
15,323✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
205,500,840✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
205,513,052✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
12,574,685!
84
    if (pME->pExtSchemas == NULL) {
6,287,958!
85
      return terrno;
×
86
    }
87

88
    for (int32_t i = 0; i < pSchWrapper->nCols && hasExtSchema; i++) {
359,319,851✔
89
      TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pME->pExtSchemas[i].typeMod));
706,058,578!
90
    }
91
  } else {
92
    pME->pExtSchemas = NULL;
199,222,459✔
93
  }
94

95
  return 0;
205,474,450✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
2,894,053✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
2,894,053✔
100
  bool                  hasTypeMods = false;
2,894,053✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
2,894,053!
102
    pSchWrapper = &pME->stbEntry.schemaRow;
2,894,053✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
×
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
×
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
2,894,053✔
109

110
  if (hasTypeMods) {
2,893,849✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
14,036!
112
    if (ret != NULL) {
14,036!
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
14,036!
114
    }
115
    return ret;
14,036✔
116
  }
117
  return NULL;
2,879,813✔
118
}
119

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

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

131
  *rsmaSchema = (SSchemaRsma *)taosMemoryMalloc(sizeof(SSchemaRsma));
4,046!
132
  if (*rsmaSchema == NULL) {
4,046!
133
    return terrno;
×
134
  }
135

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
4,046✔
151
  int32_t    i = 0, j = 0;
4,046✔
152
  for (i = 0; i < nCols; ++i) {
31,297✔
153
    while (j < pParam->nFuncs) {
49,742✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
47,719✔
155
        pFuncIds[i] = pParam->funcIds[j];
22,253✔
156
        break;
22,253✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
25,466✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
4,998✔
160
        break;
4,998✔
161
      }
162
      ++j;
20,468✔
163
    }
164
    if (j >= pParam->nFuncs) {
29,274✔
165
      for (; i < nCols; ++i) {
4,046✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
2,023✔
167
      }
168
      break;
2,023✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
4,046✔
172

173
  return 0;
4,046✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
7,553,284✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
7,553,284✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
15,106,818!
179
  if (pWrapper->nCols == 0) {
7,553,160!
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
15,106,818!
184
  uDebug("decode cols:%d", pWrapper->nCols);
7,553,658✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
15,106,204!
186
  if (pWrapper->pColRef == NULL) {
7,552,546!
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
100,832,445✔
191
    SColRef *p = &pWrapper->pColRef[i];
93,276,664✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
186,551,621!
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
186,545,716!
194
    if (p->hasRef) {
93,273,145!
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
55,333,592!
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
55,332,438!
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
55,334,766!
198
    }
199
  }
200
  return 0;
7,553,494✔
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) {
391,488✔
220
  if (pSrc->nCols > 0) {
391,488!
221
    pDst->nCols = pSrc->nCols;
391,488✔
222
    pDst->version = pSrc->version;
391,488✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
391,488!
224
    if (NULL == pDst->pColRef) {
391,488!
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
391,488!
228
  }
229
  return 0;
391,488✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
12,785,251✔
233
  int32_t code = 0;
12,785,251✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
25,559,347!
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
25,561,486!
236
  uTrace("encode cols:%d", pw->nCols);
12,787,390!
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;
12,817,572✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
12,761,807✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
12,761,807✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
12,768,611✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
275,606,826✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
275,606,826✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
551,198,345!
252
  if (pWrapper->nCols == 0) {
275,556,636!
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
551,212,605!
257
  uDebug("dencode cols:%d", pWrapper->nCols);
275,620,025✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
551,215,804✔
259
  if (pWrapper->pColCmpr == NULL) {
275,639,234!
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;
275,700,739✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
50,492✔
273

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

280
  if (pCmpr->pColCmpr == NULL) {
50,492!
281
    return terrno;
×
282
  }
283

284
  for (int32_t i = 0; i < pCmpr->nCols; i++) {
276,278!
285
    SColCmpr *pColCmpr = &pCmpr->pColCmpr[i];
225,786✔
286
    SSchema  *pColSchema = &pSchema->pSchema[i];
225,786✔
287
    pColCmpr->id = pColSchema->colId;
225,786✔
288
    pColCmpr->alg = createDefaultColCmprByType(pColSchema->type);
225,786✔
289
  }
290
  return 0;
50,492✔
291
}
292

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
54,534,837✔
294
  if (pSrc->nCols > 0) {
54,534,837✔
295
    pDst->nCols = pSrc->nCols;
47,701,275✔
296
    pDst->version = pSrc->version;
47,698,375✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
47,700,041!
298
    if (NULL == pDst->pColCmpr) {
47,694,930!
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
47,690,881!
302
  }
303
  return 0;
54,537,730✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
54,927,263✔
307
  if (pColRef) {
54,927,263!
308
    taosMemoryFreeClear(pColRef->pColRef);
54,929,102!
309
  }
310
}
54,927,734✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
54,926,425✔
313
  if (pCmpr) {
54,926,425✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
54,926,497!
315
  }
316
}
54,927,697✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
57,450,678✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
57,450,678!
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
114,913,510!
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
114,899,459!
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
114,887,351!
323

324
  if (pME->type > 0) {
57,446,834✔
325
    if (pME->name == NULL) {
47,650,265!
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
95,297,107!
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
47,658,908✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
13,311,226!
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
13,325,620!
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
13,340,891!
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
6,662,199✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
11,305!
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
40,991,513✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
68,960,920!
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
68,961,981!
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
68,962,770!
342
      if (pME->ctbEntry.commentLen > 0) {
34,481,497✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
6,288!
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
68,961,817!
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
34,480,840!
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
6,513,030!
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
13,026,224!
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
13,026,224!
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
13,026,060!
351
      if (pME->ntbEntry.commentLen > 0) {
6,513,030✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
6,288!
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
13,026,388!
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
13,026,388!
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) {
47,650,220✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
693,163!
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
46,977,921✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
6,661,146✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
40,327,084✔
368
        if (pME->colCmpr.nCols != 0) {
6,144,334✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
6,093,842!
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
50,492✔
372
          SColCmprWrapper colCmprs = {0};
50,656✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
50,492!
374
          if (code != 0) {
50,492!
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
50,492✔
379
          taosMemoryFree(colCmprs.pColCmpr);
50,492!
380
          TAOS_CHECK_RETURN(code);
50,492!
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
47,661,162!
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
57,442,711✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
13,316,004!
388
  }
389

390
  tEndEncode(pCoder);
57,437,324✔
391
  return 0;
57,428,868✔
392
}
393

394
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
424,834,319✔
395
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
424,834,319!
396
  TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->version));
849,908,568!
397
  TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->type));
849,935,956!
398
  TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->uid));
850,026,006!
399

400
  if (headerOnly) {
425,016,255✔
401
    tEndDecode(pCoder);
5,188✔
402
    return 0;
5,188✔
403
  }
404

405
  if (pME->type > 0) {
425,011,067✔
406
    TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->name));
849,927,811!
407

408
    if (pME->type == TSDB_SUPER_TABLE) {
424,971,627✔
409
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
411,039,869!
410
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
411,084,307!
411
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
411,027,063!
412
      if (TABLE_IS_ROLLUP(pME->flags)) {
205,502,399✔
413
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
43,435!
414
      }
415
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
219,330,274✔
416
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
295,084,554!
417
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
295,148,148!
418
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
295,078,978!
419
      if (pME->ctbEntry.commentLen > 0) {
147,505,753✔
420
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
7,074!
421
      }
422
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
295,080,259!
423
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
147,574,133!
424
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
71,881,956!
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
143,772,325!
426
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
143,775,516!
427
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
143,769,808!
428
      if (pME->ntbEntry.commentLen > 0) {
71,881,456✔
429
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
7,074!
430
      }
431
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
143,767,342!
432
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
143,772,874!
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) {
424,899,280✔
444
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
205,530,263!
445
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
205,519,265!
446

447
        if (pME->colCmpr.nCols == 0) {
205,492,768!
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) {
219,296,142✔
455
      if (!tDecodeIsEnd(pCoder)) {
70,131,223!
456
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
70,131,962✔
457
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
70,131,962!
458
        if (pME->colCmpr.nCols == 0) {
70,128,047!
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);
70,129,370✔
466
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
149,174,599✔
467
      if (!tDecodeIsEnd(pCoder)) {
7,553,658!
468
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
7,553,658✔
469
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
7,553,534!
470
      } else {
471
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
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)) {
424,908,215✔
478
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
205,507,667!
479
    } else {
480
      pME->pExtSchemas = NULL;
219,400,548✔
481
    }
482
  }
483
  if (pME->type == TSDB_SUPER_TABLE) {
424,811,231✔
484
    if (!tDecodeIsEnd(pCoder)) {
205,455,272!
485
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
410,962,604!
486
    }
487
  }
488

489

490
  tEndDecode(pCoder);
424,835,438✔
491
  return 0;
424,925,980✔
492
}
493

494
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
424,852,809✔
495

496
static int32_t metaCloneSchema(const SSchemaWrapper *pSrc, SSchemaWrapper *pDst) {
90,956,408✔
497
  if (pSrc == NULL || pDst == NULL) {
90,956,408!
498
    return TSDB_CODE_INVALID_PARA;
×
499
  }
500

501
  pDst->nCols = pSrc->nCols;
90,962,258✔
502
  pDst->version = pSrc->version;
90,960,452✔
503
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
90,960,515!
504
  if (pDst->pSchema == NULL) {
90,929,715!
505
    return terrno;
×
506
  }
507
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
90,935,238!
508
  return TSDB_CODE_SUCCESS;
90,964,530✔
509
}
510

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

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

539
  return TSDB_CODE_SUCCESS;
6,545✔
540
}
541

542
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
90,957,316✔
543
  if (pSchema) {
90,957,316!
544
    taosMemoryFreeClear(pSchema->pSchema);
90,959,858!
545
  }
546
}
90,961,101✔
547

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

561
void metaCloneEntryFree(SMetaEntry **ppEntry) {
54,970,367✔
562
  if (ppEntry == NULL || *ppEntry == NULL) {
54,970,367!
563
    return;
44,243✔
564
  }
565

566
  taosMemoryFreeClear((*ppEntry)->name);
54,928,385!
567

568
  if ((*ppEntry)->type < 0) {
54,928,708!
569
    taosMemoryFreeClear(*ppEntry);
×
570
    return;
×
571
  }
572

573
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
54,926,153✔
574
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
42,996,262✔
575
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
42,997,380✔
576
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
42,995,562✔
577
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
9,877✔
578
    }
579
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
11,927,802✔
580
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
6,963,937!
581
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
6,963,937!
582
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
4,964,644!
583
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
4,964,644✔
584
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
4,964,644!
585
  } else {
586
    return;
×
587
  }
588
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
54,925,588✔
589
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
54,922,789!
590
  metaCloneColRefFree(&(*ppEntry)->colRef);
54,922,970✔
591

592
  taosMemoryFreeClear(*ppEntry);
54,921,126!
593
  return;
54,920,347✔
594
}
595

596
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
54,923,899✔
597
  int32_t code = TSDB_CODE_SUCCESS;
54,923,899✔
598

599
  if (NULL == pEntry || NULL == ppEntry) {
54,923,899!
600
    return TSDB_CODE_INVALID_PARA;
×
601
  }
602

603
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
54,930,080!
604
  if (NULL == *ppEntry) {
54,911,749!
605
    return terrno;
×
606
  }
607

608
  (*ppEntry)->version = pEntry->version;
54,913,538✔
609
  (*ppEntry)->type = pEntry->type;
54,922,830✔
610
  (*ppEntry)->uid = pEntry->uid;
54,923,731✔
611

612
  if (pEntry->type < 0) {
54,920,134!
613
    return TSDB_CODE_SUCCESS;
×
614
  }
615

616
  if (pEntry->name) {
54,917,374!
617
    (*ppEntry)->name = tstrdup(pEntry->name);
54,927,432✔
618
    if (NULL == (*ppEntry)->name) {
54,928,240!
619
      code = terrno;
×
620
      metaCloneEntryFree(ppEntry);
×
621
      return code;
×
622
    }
623
  }
624

625
  if (pEntry->type == TSDB_SUPER_TABLE) {
54,924,721✔
626
    (*ppEntry)->flags = pEntry->flags;
43,000,239✔
627

628
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
42,995,684✔
629
    if (code) {
42,997,539!
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633

634
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
42,997,539✔
635
    if (code) {
43,002,881!
636
      metaCloneEntryFree(ppEntry);
×
637
      return code;
×
638
    }
639
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
43,002,881✔
640
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
6,545✔
641
      if (code) {
6,545!
642
        metaCloneEntryFree(ppEntry);
×
643
        return code;
×
644
      }
645
    }
646
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
43,004,328✔
647
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
11,928,194✔
648
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
6,963,937✔
649
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
6,963,937✔
650
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
6,963,937✔
651

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

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

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

685
    // comment
686
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
4,964,644✔
687
    if (pEntry->ntbEntry.commentLen > 0) {
4,964,257✔
688
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
1,965!
689
      if (NULL == (*ppEntry)->ntbEntry.comment) {
1,965!
690
        code = terrno;
×
691
        metaCloneEntryFree(ppEntry);
×
692
        return code;
×
693
      }
694
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
1,965!
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) {
54,928,902✔
701
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
397,872✔
702
    if (code) {
391,488!
703
      metaCloneEntryFree(ppEntry);
×
704
      return code;
×
705
    }
706
  } else {
707
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
54,531,140✔
708
    if (code) {
54,537,414!
709
      metaCloneEntryFree(ppEntry);
×
710
      return code;
×
711
    }
712
  }
713
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
54,928,902!
714
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
2,244,975!
715
    if (!(*ppEntry)->pExtSchemas) {
2,242,698!
716
      code = terrno;
×
717
      metaCloneEntryFree(ppEntry);
×
718
      return code;
×
719
    }
720
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
2,243,472!
721
  }
722

723
  return code;
54,926,016✔
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