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

taosdata / TDengine / #4979

09 Mar 2026 09:42AM UTC coverage: 68.512% (+0.07%) from 68.439%
#4979

push

travis-ci

web-flow
doc: update user manual. (#34693)

211961 of 309380 relevant lines covered (68.51%)

134839524.93 hits per line

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

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

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

22
static bool schemasHasTypeMod(const SSchema *pSchema, int32_t nCols) {
1,251,928,221✔
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;
149,996,041✔
26
    }
27
  }
28
  return false;
1,099,314,096✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
216,149,108✔
32
  if (pME->pExtSchemas) {
216,149,108✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
12,351,051✔
34
    bool                  hasTypeMods = false;
12,351,051✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
12,351,051✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
12,241,240✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
96,928✔
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
96,928✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
12,341,537✔
43

44
    for (int32_t i = 0; i < pSchWrapper->nCols && hasTypeMods; ++i) {
1,346,124,705✔
45
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->pExtSchemas[i].typeMod));
2,147,483,647✔
46
    }
47
  }
48
  return 0;
216,183,785✔
49
}
50

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

57
  for (int32_t i = 0; i < pw->nCols; i++) {
439,736,028✔
58
    SColRef *p = &pw->pColRef[i];
438,353,098✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
876,706,196✔
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
876,706,196✔
61
    if (p->hasRef) {
438,353,098✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
581,284,488✔
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
581,284,488✔
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
581,284,488✔
65
    }
66
  }
67
  return 0;
1,382,930✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
1,194,043,548✔
71
  bool hasExtSchema = false;
1,194,043,548✔
72
  SSchemaWrapper* pSchWrapper = NULL;
1,194,043,548✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
1,194,043,548✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
921,435,818✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
272,739,368✔
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
272,817,532✔
77
  } else {
78
    return 0;
46✔
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
1,194,277,573✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
1,194,445,299✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
281,465,582✔
84
    if (pME->pExtSchemas == NULL) {
140,732,075✔
85
      return terrno;
×
86
    }
87

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

95
  return 0;
1,194,390,887✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
45,550,611✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
45,550,611✔
100
  bool                  hasTypeMods = false;
45,550,611✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
45,550,611✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
33,097,229✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
12,482,758✔
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
12,489,948✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
45,591,005✔
109

110
  if (hasTypeMods) {
45,546,367✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
40,588✔
112
    if (ret != NULL) {
40,323✔
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
40,323✔
114
    }
115
    return ret;
40,588✔
116
  }
117
  return NULL;
45,505,779✔
118
}
119

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

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

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

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
27,269✔
151
  int32_t    i = 0, j = 0;
27,269✔
152
  for (i = 0; i < nCols; ++i) {
206,360✔
153
    while (j < pParam->nFuncs) {
325,754✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
312,488✔
155
        pFuncIds[i] = pParam->funcIds[j];
145,926✔
156
        break;
145,926✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
166,562✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
33,165✔
160
        break;
33,165✔
161
      }
162
      ++j;
133,397✔
163
    }
164
    if (j >= pParam->nFuncs) {
192,357✔
165
      for (; i < nCols; ++i) {
26,532✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
13,266✔
167
      }
168
      break;
13,266✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
27,269✔
172

173
  return 0;
27,269✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
39,654,345✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
39,654,345✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
79,317,694✔
179
  if (pWrapper->nCols == 0) {
39,659,522✔
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
79,309,487✔
184
  uDebug("decode cols:%d", pWrapper->nCols);
39,657,690✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
79,317,655✔
186
  if (pWrapper->pColRef == NULL) {
39,659,965✔
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
885,435,392✔
191
    SColRef *p = &pWrapper->pColRef[i];
845,741,094✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
1,691,582,772✔
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
1,691,608,829✔
194
    if (p->hasRef) {
845,805,789✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
464,024,284✔
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
464,029,860✔
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
464,029,002✔
198
    }
199
  }
200
  return 0;
39,664,496✔
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) {
688,418✔
220
  if (pSrc->nCols > 0) {
688,418✔
221
    pDst->nCols = pSrc->nCols;
688,418✔
222
    pDst->version = pSrc->version;
688,418✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
688,418✔
224
    if (NULL == pDst->pColRef) {
688,418✔
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
688,418✔
228
  }
229
  return 0;
688,418✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
47,222,003✔
233
  int32_t code = 0;
47,222,003✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
94,466,735✔
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
94,547,406✔
236
  uTrace("encode cols:%d", pw->nCols);
47,302,674✔
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;
47,373,515✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
47,074,308✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
47,074,308✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
47,148,275✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
1,194,159,373✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
1,194,159,373✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
2,147,483,647✔
252
  if (pWrapper->nCols == 0) {
1,194,254,758✔
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
2,147,483,647✔
257
  uDebug("dencode cols:%d", pWrapper->nCols);
1,194,231,054✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
2,147,483,647✔
259
  if (pWrapper->pColCmpr == NULL) {
1,194,365,473✔
260
    return terrno;
×
261
  }
262

263
  for (int i = 0; i < pWrapper->nCols; i++) {
2,147,483,647✔
264
    SColCmpr *p = &pWrapper->pColCmpr[i];
2,147,483,647✔
265
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
2,147,483,647✔
266
    TAOS_CHECK_RETURN(tDecodeU32(pDecoder, &p->alg));
2,147,483,647✔
267
  }
268
  return 0;
1,194,815,468✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
230,730✔
273

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

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

284
  for (int32_t i = 0; i < pCmpr->nCols; i++) {
1,149,658✔
285
    SColCmpr *pColCmpr = &pCmpr->pColCmpr[i];
918,928✔
286
    SSchema  *pColSchema = &pSchema->pSchema[i];
918,928✔
287
    pColCmpr->id = pColSchema->colId;
918,928✔
288
    pColCmpr->alg = createDefaultColCmprByType(pColSchema->type);
918,928✔
289
  }
290
  return 0;
230,730✔
291
}
292

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
211,423,870✔
294
  if (pSrc->nCols > 0) {
211,423,870✔
295
    pDst->nCols = pSrc->nCols;
186,484,097✔
296
    pDst->version = pSrc->version;
186,479,596✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
186,440,578✔
298
    if (NULL == pDst->pColCmpr) {
186,452,062✔
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
186,427,146✔
302
  }
303
  return 0;
211,440,890✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
212,038,813✔
307
  if (pColRef) {
212,038,813✔
308
    taosMemoryFreeClear(pColRef->pColRef);
212,069,803✔
309
  }
310
}
212,063,101✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
212,073,481✔
313
  if (pCmpr) {
212,073,481✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
212,071,481✔
315
  }
316
}
212,130,426✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
221,319,658✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
221,319,658✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
442,656,261✔
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
442,612,208✔
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
442,582,822✔
323

324
  if (pME->type > 0) {
221,294,544✔
325
    if (pME->name == NULL) {
216,119,562✔
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
432,323,700✔
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
216,174,849✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
52,094,872✔
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
52,197,985✔
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
52,179,687✔
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
26,047,770✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
103,917✔
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
190,055,141✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
336,172,963✔
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
336,192,882✔
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
336,208,350✔
342
      if (pME->ctbEntry.commentLen > 0) {
168,106,724✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
29,468✔
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
336,149,967✔
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
168,066,105✔
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
21,996,704✔
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
43,993,408✔
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
43,993,408✔
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
43,993,408✔
351
      if (pME->ntbEntry.commentLen > 0) {
21,996,704✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
40,396✔
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
43,993,408✔
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
43,993,408✔
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) {
216,134,184✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,491,900✔
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
214,692,194✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
26,081,924✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
188,705,332✔
368
        if (pME->colCmpr.nCols != 0) {
21,267,678✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
21,036,948✔
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
230,730✔
372
          SColCmprWrapper colCmprs = {0};
230,730✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
230,730✔
374
          if (code != 0) {
230,730✔
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
230,730✔
379
          taosMemoryFree(colCmprs.pColCmpr);
230,730✔
380
          TAOS_CHECK_RETURN(code);
230,730✔
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
216,176,841✔
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
221,320,433✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
52,088,800✔
388
    TAOS_CHECK_RETURN(tEncodeI64v(pCoder, pME->stbEntry.ownerId));
51,996,544✔
389
  } else if (pME->type == TSDB_NORMAL_TABLE) {
195,215,127✔
390
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.ownerId));
42,535,356✔
391
  }
392

393
  tEndEncode(pCoder);
221,226,296✔
394
  return 0;
221,176,165✔
395
}
396

397
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
2,024,834,922✔
398
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
2,024,834,922✔
399
  TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->version));
2,147,483,647✔
400
  TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->type));
2,147,483,647✔
401
  TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->uid));
2,147,483,647✔
402

403
  if (headerOnly) {
2,025,667,257✔
404
    tEndDecode(pCoder);
345,404✔
405
    return 0;
345,404✔
406
  }
407

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

411
    if (pME->type == TSDB_SUPER_TABLE) {
2,025,115,825✔
412
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,843,074,059✔
413
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,843,404,628✔
414
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,843,295,502✔
415
      if (TABLE_IS_ROLLUP(pME->flags)) {
921,578,607✔
416
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
336,072✔
417
      }
418
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
1,102,949,500✔
419
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
1,643,445,257✔
420
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
1,643,630,664✔
421
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
1,643,313,547✔
422
      if (pME->ctbEntry.commentLen > 0) {
821,506,853✔
423
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
51,092✔
424
      }
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
1,643,341,322✔
426
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
821,784,883✔
427
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
281,566,526✔
428
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
563,361,538✔
429
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
563,459,288✔
430
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
563,321,601✔
431
      if (pME->ntbEntry.commentLen > 0) {
281,599,948✔
432
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
74,810✔
433
      }
434
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
563,088,667✔
435
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
563,304,135✔
436
    } else if (pME->type == TSDB_TSMA_TABLE) {
×
437
      pME->smaEntry.tsma = tDecoderMalloc(pCoder, sizeof(STSma));
×
438
      if (!pME->smaEntry.tsma) {
×
439
        return terrno;
×
440
      }
441
      TAOS_CHECK_RETURN(tDecodeTSma(pCoder, pME->smaEntry.tsma, true));
×
442
    } else {
443
      metaError("meta/entry: invalide table type: %" PRId8 " decode failed.", pME->type);
×
444
      return TSDB_CODE_INVALID_PARA;
×
445
    }
446
    if (pME->type == TSDB_SUPER_TABLE) {
2,024,734,583✔
447
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
921,689,181✔
448
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
921,611,677✔
449

450
        if (pME->colCmpr.nCols == 0) {
921,731,011✔
451
          TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->stbEntry.schemaRow));
×
452
        }
453
      } else {
454
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->stbEntry.schemaRow));
×
455
        TABLE_SET_COL_COMPRESSED(pME->flags);
×
456
      }
457
    } else if (pME->type == TSDB_NORMAL_TABLE) {
1,102,906,489✔
458
      if (!tDecodeIsEnd(pCoder)) {
272,897,821✔
459
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
272,935,872✔
460
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
272,935,872✔
461
        if (pME->colCmpr.nCols == 0) {
272,939,884✔
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);
272,934,450✔
469
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
830,068,407✔
470
      if (!tDecodeIsEnd(pCoder)) {
39,659,964✔
471
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
39,659,669✔
472
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
39,660,335✔
473
      } else {
474
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
295✔
475
        if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
295✔
476
           TAOS_CHECK_RETURN(metatInitDefaultSColRefWrapper(pCoder, &pME->colRef, &pME->ntbEntry.schemaRow));
×
477
        }
478
      }
479
    }
480
    if (!tDecodeIsEnd(pCoder)) {
2,024,843,770✔
481
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
1,194,532,053✔
482
    } else {
483
      pME->pExtSchemas = NULL;
830,311,717✔
484
    }
485
  }
486
  if (pME->type == TSDB_SUPER_TABLE) {
2,024,462,698✔
487
    if (!tDecodeIsEnd(pCoder)) {
921,478,189✔
488
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,843,045,393✔
489
    }
490
    if (!tDecodeIsEnd(pCoder)) {
921,214,864✔
491
      TAOS_CHECK_RETURN(tDecodeI64v(pCoder, &pME->stbEntry.ownerId));
1,842,689,924✔
492
    }
493
  } else if (pME->type == TSDB_NORMAL_TABLE) {
1,102,690,626✔
494
    if (!tDecodeIsEnd(pCoder)) {
272,799,312✔
495
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.ownerId));
545,742,842✔
496
    }
497
  }
498

499

500
  tEndDecode(pCoder);
2,024,130,393✔
501
  return 0;
2,024,376,132✔
502
}
503

504
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
2,024,652,983✔
505

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

511
  pDst->nCols = pSrc->nCols;
365,052,835✔
512
  pDst->version = pSrc->version;
365,050,052✔
513
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
365,041,580✔
514
  if (pDst->pSchema == NULL) {
364,901,340✔
515
    return terrno;
×
516
  }
517
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
364,933,586✔
518
  return TSDB_CODE_SUCCESS;
365,067,133✔
519
}
520

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

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

549
  return TSDB_CODE_SUCCESS;
72,226✔
550
}
551

552
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
364,980,993✔
553
  if (pSchema) {
364,980,993✔
554
    taosMemoryFreeClear(pSchema->pSchema);
364,994,243✔
555
  }
556
}
365,036,859✔
557

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

571
void metaCloneEntryFree(SMetaEntry **ppEntry) {
212,153,427✔
572
  if (ppEntry == NULL || *ppEntry == NULL) {
212,153,427✔
573
    return;
77,993✔
574
  }
575

576
  taosMemoryFreeClear((*ppEntry)->name);
212,087,726✔
577

578
  if ((*ppEntry)->type < 0) {
212,109,373✔
579
    taosMemoryFreeClear(*ppEntry);
×
580
    return;
×
581
  }
582

583
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
212,042,936✔
584
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
178,093,627✔
585
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
178,081,068✔
586
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
178,084,112✔
587
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
110,550✔
588
    }
589
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
34,013,184✔
590
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
25,180,759✔
591
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
25,180,318✔
592
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
8,831,780✔
593
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
8,831,780✔
594
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
8,831,780✔
595
  } else {
596
    return;
×
597
  }
598
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
212,096,762✔
599
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
212,083,444✔
600
  metaCloneColRefFree(&(*ppEntry)->colRef);
212,080,388✔
601

602
  taosMemoryFreeClear(*ppEntry);
212,033,066✔
603
  return;
212,072,724✔
604
}
605

606
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
212,090,280✔
607
  int32_t code = TSDB_CODE_SUCCESS;
212,090,280✔
608

609
  if (NULL == pEntry || NULL == ppEntry) {
212,090,280✔
610
    return TSDB_CODE_INVALID_PARA;
×
611
  }
612

613
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
212,135,252✔
614
  if (NULL == *ppEntry) {
212,051,426✔
615
    return terrno;
×
616
  }
617

618
  (*ppEntry)->version = pEntry->version;
212,077,870✔
619
  (*ppEntry)->type = pEntry->type;
212,110,369✔
620
  (*ppEntry)->uid = pEntry->uid;
212,092,496✔
621

622
  if (pEntry->type < 0) {
212,085,244✔
623
    return TSDB_CODE_SUCCESS;
×
624
  }
625

626
  if (pEntry->name) {
212,113,894✔
627
    (*ppEntry)->name = tstrdup(pEntry->name);
212,136,893✔
628
    if (NULL == (*ppEntry)->name) {
212,122,119✔
629
      code = terrno;
×
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633
  }
634

635
  if (pEntry->type == TSDB_SUPER_TABLE) {
212,121,925✔
636
    (*ppEntry)->flags = pEntry->flags;
178,084,780✔
637

638
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
178,084,033✔
639
    if (code) {
178,108,379✔
640
      metaCloneEntryFree(ppEntry);
×
641
      return code;
×
642
    }
643

644
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
178,108,379✔
645
    if (code) {
178,128,532✔
646
      metaCloneEntryFree(ppEntry);
×
647
      return code;
×
648
    }
649
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
178,128,532✔
650
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
72,963✔
651
      if (code) {
72,226✔
652
        metaCloneEntryFree(ppEntry);
×
653
        return code;
×
654
      }
655
    }
656
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
178,126,509✔
657
    (*ppEntry)->stbEntry.ownerId = pEntry->stbEntry.ownerId;
178,098,874✔
658
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
34,011,328✔
659
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
25,180,397✔
660
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
25,180,036✔
661
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
25,181,078✔
662

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

675
    // tags
676
    STag *pTags = (STag *)pEntry->ctbEntry.pTags;
25,179,618✔
677
    (*ppEntry)->ctbEntry.pTags = taosMemoryCalloc(1, pTags->len);
25,179,222✔
678
    if (NULL == (*ppEntry)->ctbEntry.pTags) {
25,181,084✔
679
      code = terrno;
×
680
      metaCloneEntryFree(ppEntry);
×
681
      return code;
×
682
    }
683
    memcpy((*ppEntry)->ctbEntry.pTags, pEntry->ctbEntry.pTags, pTags->len);
25,180,235✔
684
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
8,831,780✔
685
    (*ppEntry)->ntbEntry.btime = pEntry->ntbEntry.btime;
8,831,780✔
686
    (*ppEntry)->ntbEntry.ttlDays = pEntry->ntbEntry.ttlDays;
8,831,780✔
687
    (*ppEntry)->ntbEntry.ncid = pEntry->ntbEntry.ncid;
8,831,780✔
688
    (*ppEntry)->ntbEntry.ownerId = pEntry->ntbEntry.ownerId;
8,831,780✔
689

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

697
    // comment
698
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
8,831,780✔
699
    if (pEntry->ntbEntry.commentLen > 0) {
8,831,780✔
700
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
12,521✔
701
      if (NULL == (*ppEntry)->ntbEntry.comment) {
12,521✔
702
        code = terrno;
×
703
        metaCloneEntryFree(ppEntry);
×
704
        return code;
×
705
      }
706
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
12,521✔
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) {
212,116,627✔
713
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
728,385✔
714
    if (code) {
688,418✔
715
      metaCloneEntryFree(ppEntry);
×
716
      return code;
×
717
    }
718
  } else {
719
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
211,388,679✔
720
    if (code) {
211,431,992✔
721
      metaCloneEntryFree(ppEntry);
×
722
      return code;
×
723
    }
724
  }
725
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
212,120,410✔
726
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
9,698,343✔
727
    if (!(*ppEntry)->pExtSchemas) {
9,698,744✔
728
      code = terrno;
×
729
      metaCloneEntryFree(ppEntry);
×
730
      return code;
×
731
    }
732
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
9,697,295✔
733
  }
734

735
  return code;
212,069,635✔
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