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

taosdata / TDengine / #4870

26 Nov 2025 05:46AM UTC coverage: 64.545% (+0.006%) from 64.539%
#4870

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

768 of 945 new or added lines in 33 files covered. (81.27%)

2982 existing lines in 119 files now uncovered.

158219 of 245129 relevant lines covered (64.55%)

112474797.36 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) {
714,782,285✔
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;
41,126,318✔
26
    }
27
  }
28
  return false;
673,012,545✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
184,740,831✔
32
  if (pME->pExtSchemas) {
184,740,831✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
14,649,429✔
34
    bool                  hasTypeMods = false;
14,649,429✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
14,649,429✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
14,498,165✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
154,932✔
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
154,932✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
14,650,631✔
43

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

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

57
  for (int32_t i = 0; i < pw->nCols; i++) {
12,672,036✔
58
    SColRef *p = &pw->pColRef[i];
11,383,764✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
22,767,528✔
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
22,767,528✔
61
    if (p->hasRef) {
11,383,764✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
12,724,136✔
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
12,724,136✔
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
12,724,136✔
65
    }
66
  }
67
  return 0;
1,288,272✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
684,077,892✔
71
  bool hasExtSchema = false;
684,077,892✔
72
  SSchemaWrapper* pSchWrapper = NULL;
684,077,892✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
684,077,892✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
676,773,423✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
7,302,246✔
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
7,302,246✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
684,097,264✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
684,135,508✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
58,860,519✔
84
    if (pME->pExtSchemas == NULL) {
29,424,533✔
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;
654,694,567✔
93
  }
94

95
  return 0;
684,254,393✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
16,069,754✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
16,069,754✔
100
  bool                  hasTypeMods = false;
16,069,754✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
16,069,754✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
13,691,935✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
2,384,312✔
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
2,386,610✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
16,079,281✔
109

110
  if (hasTypeMods) {
16,069,079✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
51,313✔
112
    if (ret != NULL) {
51,313✔
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
51,313✔
114
    }
115
    return ret;
51,313✔
116
  }
117
  return NULL;
16,017,766✔
118
}
119

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

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

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

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

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

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

173
  return 0;
56,088✔
174
}
175

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

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
35,053,602✔
184
  uDebug("decode cols:%d", pWrapper->nCols);
17,526,716✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
35,052,831✔
186
  if (pWrapper->pColRef == NULL) {
17,526,703✔
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
246,579,484✔
191
    SColRef *p = &pWrapper->pColRef[i];
229,045,242✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
458,109,352✔
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
458,121,921✔
194
    if (p->hasRef) {
229,066,122✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
136,171,143✔
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
136,165,495✔
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
136,165,286✔
198
    }
199
  }
200
  return 0;
17,526,890✔
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) {
695,473✔
220
  if (pSrc->nCols > 0) {
695,473✔
221
    pDst->nCols = pSrc->nCols;
695,473✔
222
    pDst->version = pSrc->version;
695,473✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
695,473✔
224
    if (NULL == pDst->pColRef) {
695,473✔
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
695,473✔
228
  }
229
  return 0;
695,473✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
44,519,377✔
233
  int32_t code = 0;
44,519,377✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
89,091,354✔
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
89,147,635✔
236
  uTrace("encode cols:%d", pw->nCols);
44,575,658✔
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;
44,664,419✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
44,386,001✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
44,386,001✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
44,406,501✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
834,643,578✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
834,643,578✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
1,669,364,423✔
252
  if (pWrapper->nCols == 0) {
834,548,090✔
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
1,669,208,948✔
257
  uDebug("dencode cols:%d", pWrapper->nCols);
834,671,966✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
1,669,180,239✔
259
  if (pWrapper->pColCmpr == NULL) {
834,658,360✔
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;
834,937,087✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
247,393✔
273

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

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

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

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
176,558,757✔
294
  if (pSrc->nCols > 0) {
176,558,757✔
295
    pDst->nCols = pSrc->nCols;
159,531,308✔
296
    pDst->version = pSrc->version;
159,523,373✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
159,518,617✔
298
    if (NULL == pDst->pColCmpr) {
159,507,176✔
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
159,512,375✔
302
  }
303
  return 0;
176,557,364✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
177,202,539✔
307
  if (pColRef) {
177,202,539✔
308
    taosMemoryFreeClear(pColRef->pColRef);
177,208,114✔
309
  }
310
}
177,239,559✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
177,214,313✔
313
  if (pCmpr) {
177,214,313✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
177,227,044✔
315
  }
316
}
177,235,564✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
188,791,658✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
188,791,658✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
377,621,704✔
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
377,564,520✔
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
377,491,542✔
323

324
  if (pME->type > 0) {
188,742,961✔
325
    if (pME->name == NULL) {
184,694,750✔
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
369,485,519✔
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
184,764,703✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
64,561,223✔
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
64,632,008✔
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
64,674,412✔
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
32,314,719✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
161,253✔
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
152,434,756✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
278,920,314✔
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
278,922,556✔
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
278,922,911✔
342
      if (pME->ctbEntry.commentLen > 0) {
139,462,756✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
15,528✔
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
278,901,853✔
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
139,446,853✔
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
12,981,208✔
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
25,962,416✔
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
25,962,416✔
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
25,962,416✔
351
      if (pME->ntbEntry.commentLen > 0) {
12,981,208✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
14,728✔
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
25,962,416✔
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
25,962,416✔
356
    } else if (pME->type == TSDB_TSMA_TABLE) {
×
357
      TAOS_CHECK_RETURN(tEncodeTSma(pCoder, pME->smaEntry.tsma));
×
358
    } else {
359
      metaError("meta/entry: invalide table type: %" PRId8 " encode failed.", pME->type);
×
360
      return TSDB_CODE_INVALID_PARA;
×
361
    }
362
    if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
184,738,292✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,371,752✔
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
183,433,174✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
32,279,395✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
151,175,951✔
368
        if (pME->colCmpr.nCols != 0) {
12,309,764✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
12,062,214✔
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
247,550✔
372
          SColCmprWrapper colCmprs = {0};
247,550✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
247,550✔
374
          if (code != 0) {
247,550✔
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
247,550✔
379
          taosMemoryFree(colCmprs.pColCmpr);
247,550✔
380
          TAOS_CHECK_RETURN(code);
247,550✔
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
184,744,688✔
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
188,780,455✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
64,574,422✔
388
  }
389

390
  tEndEncode(pCoder);
188,675,346✔
391
  return 0;
188,723,260✔
392
}
393

394
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
1,444,560,660✔
395
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
1,444,560,660✔
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,444,930,979✔
401
    tEndDecode(pCoder);
109,716✔
402
    return 0;
109,716✔
403
  }
404

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

408
    if (pME->type == TSDB_SUPER_TABLE) {
1,444,712,901✔
409
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,353,742,272✔
410
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,353,948,169✔
411
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,353,801,874✔
412
      if (TABLE_IS_ROLLUP(pME->flags)) {
676,819,981✔
413
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
593,598✔
414
      }
415
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
767,585,740✔
416
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
1,210,966,977✔
417
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
1,211,095,949✔
418
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
1,210,941,569✔
419
      if (pME->ctbEntry.commentLen > 0) {
605,396,499✔
420
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
21,974✔
421
      }
422
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
1,210,933,090✔
423
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
605,545,925✔
424
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
162,262,449✔
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
324,572,061✔
426
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
324,602,269✔
427
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
324,571,750✔
428
      if (pME->ntbEntry.commentLen > 0) {
162,272,006✔
429
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
22,082✔
430
      }
431
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
324,529,979✔
432
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
324,567,377✔
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,444,590,615✔
444
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
676,927,017✔
445
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
676,863,430✔
446

447
        if (pME->colCmpr.nCols == 0) {
676,965,760✔
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) {
767,542,845✔
455
      if (!tDecodeIsEnd(pCoder)) {
157,905,982✔
456
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
157,919,689✔
457
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
157,919,689✔
458
        if (pME->colCmpr.nCols == 0) {
157,913,514✔
459
          TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
460
        }
461
      } else {
UNCOV
462
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
UNCOV
463
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
464
      }
465
      TABLE_SET_COL_COMPRESSED(pME->flags);
157,905,452✔
466
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
609,664,311✔
467
      if (!tDecodeIsEnd(pCoder)) {
17,524,789✔
468
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
17,524,789✔
469
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
17,525,568✔
470
      } else {
UNCOV
471
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
472
        if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
188✔
473
           TAOS_CHECK_RETURN(metatInitDefaultSColRefWrapper(pCoder, &pME->colRef, &pME->ntbEntry.schemaRow));
×
474
        }
475
      }
476
    }
477
    if (!tDecodeIsEnd(pCoder)) {
1,444,523,075✔
478
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
684,171,630✔
479
    } else {
480
      pME->pExtSchemas = NULL;
760,351,445✔
481
    }
482
  }
483
  if (pME->type == TSDB_SUPER_TABLE) {
1,444,440,826✔
484
    if (!tDecodeIsEnd(pCoder)) {
676,751,427✔
485
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,353,707,448✔
486
    }
487
  }
488

489

490
  tEndDecode(pCoder);
1,444,518,976✔
491
  return 0;
1,444,470,675✔
492
}
493

494
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
1,444,415,188✔
495

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

501
  pDst->nCols = pSrc->nCols;
311,274,649✔
502
  pDst->version = pSrc->version;
311,265,905✔
503
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
311,252,163✔
504
  if (pDst->pSchema == NULL) {
311,185,305✔
505
    return terrno;
×
506
  }
507
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
311,205,996✔
508
  return TSDB_CODE_SUCCESS;
311,292,170✔
509
}
510

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

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

539
  return TSDB_CODE_SUCCESS;
90,364✔
540
}
541

542
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
311,238,353✔
543
  if (pSchema) {
311,238,353✔
544
    taosMemoryFreeClear(pSchema->pSchema);
311,247,835✔
545
  }
546
}
311,252,044✔
547

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

561
void metaCloneEntryFree(SMetaEntry **ppEntry) {
177,299,749✔
562
  if (ppEntry == NULL || *ppEntry == NULL) {
177,299,749✔
563
    return;
78,407✔
564
  }
565

566
  taosMemoryFreeClear((*ppEntry)->name);
177,232,408✔
567

568
  if ((*ppEntry)->type < 0) {
177,246,387✔
569
    taosMemoryFreeClear(*ppEntry);
×
570
    return;
×
571
  }
572

573
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
177,213,480✔
574
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
151,264,847✔
575
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
151,260,190✔
576
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
151,249,812✔
577
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
138,662✔
578
    }
579
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
25,971,260✔
580
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
17,259,498✔
581
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
17,257,740✔
582
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
8,712,952✔
583
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
8,712,952✔
584
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
8,712,952✔
585
  } else {
586
    return;
×
587
  }
588
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
177,227,823✔
589
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
177,228,393✔
590
  metaCloneColRefFree(&(*ppEntry)->colRef);
177,235,201✔
591

592
  taosMemoryFreeClear(*ppEntry);
177,200,661✔
593
  return;
177,216,588✔
594
}
595

596
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
177,241,671✔
597
  int32_t code = TSDB_CODE_SUCCESS;
177,241,671✔
598

599
  if (NULL == pEntry || NULL == ppEntry) {
177,241,671✔
600
    return TSDB_CODE_INVALID_PARA;
×
601
  }
602

603
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
177,268,411✔
604
  if (NULL == *ppEntry) {
177,199,701✔
605
    return terrno;
×
606
  }
607

608
  (*ppEntry)->version = pEntry->version;
177,228,202✔
609
  (*ppEntry)->type = pEntry->type;
177,243,820✔
610
  (*ppEntry)->uid = pEntry->uid;
177,229,456✔
611

612
  if (pEntry->type < 0) {
177,243,517✔
613
    return TSDB_CODE_SUCCESS;
×
614
  }
615

616
  if (pEntry->name) {
177,222,472✔
617
    (*ppEntry)->name = tstrdup(pEntry->name);
177,240,737✔
618
    if (NULL == (*ppEntry)->name) {
177,243,680✔
619
      code = terrno;
×
620
      metaCloneEntryFree(ppEntry);
×
621
      return code;
×
622
    }
623
  }
624

625
  if (pEntry->type == TSDB_SUPER_TABLE) {
177,214,982✔
626
    (*ppEntry)->flags = pEntry->flags;
151,268,418✔
627

628
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
151,249,950✔
629
    if (code) {
151,283,458✔
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633

634
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
151,283,458✔
635
    if (code) {
151,290,855✔
636
      metaCloneEntryFree(ppEntry);
×
637
      return code;
×
638
    }
639
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
151,290,855✔
640
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
91,143✔
641
      if (code) {
90,364✔
642
        metaCloneEntryFree(ppEntry);
×
643
        return code;
×
644
      }
645
    }
646
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
151,289,859✔
647
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
25,972,307✔
648
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
17,259,880✔
649
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
17,259,355✔
650
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
17,259,355✔
651

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

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

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

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

700
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
177,243,452✔
701
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
698,922✔
702
    if (code) {
695,473✔
703
      metaCloneEntryFree(ppEntry);
×
704
      return code;
×
705
    }
706
  } else {
707
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
176,543,096✔
708
    if (code) {
176,549,833✔
709
      metaCloneEntryFree(ppEntry);
×
710
      return code;
×
711
    }
712
  }
713
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
177,245,306✔
714
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
12,498,427✔
715
    if (!(*ppEntry)->pExtSchemas) {
12,492,576✔
716
      code = terrno;
×
717
      metaCloneEntryFree(ppEntry);
×
718
      return code;
×
719
    }
720
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
12,491,198✔
721
  }
722

723
  return code;
177,227,447✔
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