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

taosdata / TDengine / #4905

29 Dec 2025 02:08PM UTC coverage: 65.423% (-0.3%) from 65.734%
#4905

push

travis-ci

web-flow
enh: sign connect request (#34067)

23 of 29 new or added lines in 4 files covered. (79.31%)

11614 existing lines in 186 files now uncovered.

193476 of 295730 relevant lines covered (65.42%)

115752566.53 hits per line

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

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

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

22
static bool schemasHasTypeMod(const SSchema *pSchema, int32_t nCols) {
997,884,007✔
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;
25,848,435✔
26
    }
27
  }
28
  return false;
970,244,647✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
182,463,682✔
32
  if (pME->pExtSchemas) {
182,463,682✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
11,285,688✔
34
    bool                  hasTypeMods = false;
11,285,688✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
11,285,688✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
11,197,383✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
85,748✔
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
85,748✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
11,280,493✔
43

44
    for (int32_t i = 0; i < pSchWrapper->nCols && hasTypeMods; ++i) {
1,081,628,263✔
45
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->pExtSchemas[i].typeMod));
2,140,822,782✔
46
    }
47
  }
48
  return 0;
182,496,067✔
49
}
50

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

57
  for (int32_t i = 0; i < pw->nCols; i++) {
182,238,778✔
58
    SColRef *p = &pw->pColRef[i];
181,184,508✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
362,369,016✔
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
362,369,016✔
61
    if (p->hasRef) {
181,184,508✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
216,348,760✔
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
216,348,760✔
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
216,348,760✔
65
    }
66
  }
67
  return 0;
1,054,270✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
947,432,365✔
71
  bool hasExtSchema = false;
947,432,365✔
72
  SSchemaWrapper* pSchWrapper = NULL;
947,432,365✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
947,432,365✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
759,195,290✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
188,381,812✔
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
188,403,318✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
947,600,484✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
947,663,857✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
34,819,099✔
84
    if (pME->pExtSchemas == NULL) {
17,407,796✔
85
      return terrno;
×
86
    }
87

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

95
  return 0;
947,916,647✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
39,224,192✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
39,224,192✔
100
  bool                  hasTypeMods = false;
39,224,192✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
39,224,192✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
28,475,965✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
10,758,629✔
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
10,757,015✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
39,234,906✔
109

110
  if (hasTypeMods) {
39,220,992✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
36,553✔
112
    if (ret != NULL) {
36,553✔
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
36,553✔
114
    }
115
    return ret;
36,553✔
116
  }
117
  return NULL;
39,184,439✔
118
}
119

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

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

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

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
25,308✔
151
  int32_t    i = 0, j = 0;
25,308✔
152
  for (i = 0; i < nCols; ++i) {
194,731✔
153
    while (j < pParam->nFuncs) {
307,914✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
295,963✔
155
        pFuncIds[i] = pParam->funcIds[j];
138,491✔
156
        break;
138,491✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
157,472✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
30,932✔
160
        break;
30,932✔
161
      }
162
      ++j;
126,540✔
163
    }
164
    if (j >= pParam->nFuncs) {
181,374✔
165
      for (; i < nCols; ++i) {
23,902✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
11,951✔
167
      }
168
      break;
11,951✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
25,308✔
172

173
  return 0;
25,308✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
5,048,250✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
5,048,250✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
10,098,892✔
179
  if (pWrapper->nCols == 0) {
5,050,044✔
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
10,096,500✔
184
  uDebug("decode cols:%d", pWrapper->nCols);
5,048,250✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
10,098,294✔
186
  if (pWrapper->pColRef == NULL) {
5,050,044✔
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
171,119,462✔
191
    SColRef *p = &pWrapper->pColRef[i];
166,066,331✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
332,159,420✔
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
332,174,202✔
194
    if (p->hasRef) {
166,090,917✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
95,076,475✔
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
95,075,088✔
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
95,076,232✔
198
    }
199
  }
200
  return 0;
5,050,044✔
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) {
625,631✔
220
  if (pSrc->nCols > 0) {
625,631✔
221
    pDst->nCols = pSrc->nCols;
625,631✔
222
    pDst->version = pSrc->version;
625,631✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
625,631✔
224
    if (NULL == pDst->pColRef) {
625,631✔
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
625,631✔
228
  }
229
  return 0;
625,631✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
53,629,753✔
233
  int32_t code = 0;
53,629,753✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
107,196,905✔
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
107,211,009✔
236
  uTrace("encode cols:%d", pw->nCols);
53,643,857✔
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;
53,702,897✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
53,504,708✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
53,504,708✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
53,547,673✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
947,467,189✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
947,467,189✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
1,895,528,020✔
252
  if (pWrapper->nCols == 0) {
947,511,839✔
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
1,895,159,466✔
257
  uDebug("dencode cols:%d", pWrapper->nCols);
947,787,333✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
1,894,772,319✔
259
  if (pWrapper->pColCmpr == NULL) {
947,854,563✔
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;
948,358,843✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
163,692✔
273

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

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

284
  for (int32_t i = 0; i < pCmpr->nCols; i++) {
854,524✔
285
    SColCmpr *pColCmpr = &pCmpr->pColCmpr[i];
690,832✔
286
    SSchema  *pColSchema = &pSchema->pSchema[i];
690,832✔
287
    pColCmpr->id = pColSchema->colId;
690,832✔
288
    pColCmpr->alg = createDefaultColCmprByType(pColSchema->type);
690,832✔
289
  }
290
  return 0;
163,692✔
291
}
292

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
160,578,598✔
294
  if (pSrc->nCols > 0) {
160,578,598✔
295
    pDst->nCols = pSrc->nCols;
144,814,847✔
296
    pDst->version = pSrc->version;
144,810,161✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
144,805,005✔
298
    if (NULL == pDst->pColCmpr) {
144,788,936✔
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
144,761,366✔
302
  }
303
  return 0;
160,582,223✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
161,184,886✔
307
  if (pColRef) {
161,184,886✔
308
    taosMemoryFreeClear(pColRef->pColRef);
161,197,759✔
309
  }
310
}
161,196,106✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
161,189,468✔
313
  if (pCmpr) {
161,189,468✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
161,200,183✔
315
  }
316
}
161,174,691✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
186,742,161✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
186,742,161✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
373,537,355✔
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
373,501,678✔
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
373,456,795✔
323

324
  if (pME->type > 0) {
186,731,849✔
325
    if (pME->name == NULL) {
182,494,823✔
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
364,950,082✔
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
182,488,442✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
45,517,519✔
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
45,557,486✔
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
45,588,477✔
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
22,771,751✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
73,112✔
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
159,694,784✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
256,431,956✔
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
256,445,330✔
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
256,446,755✔
342
      if (pME->ctbEntry.commentLen > 0) {
128,219,712✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
27,064✔
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
256,438,136✔
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
128,218,296✔
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
31,493,954✔
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
62,987,908✔
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
62,987,908✔
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
62,987,908✔
351
      if (pME->ntbEntry.commentLen > 0) {
31,493,954✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
36,616✔
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
62,987,908✔
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
62,987,908✔
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) {
182,494,179✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,143,520✔
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
181,370,029✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
22,750,471✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
158,688,538✔
368
        if (pME->colCmpr.nCols != 0) {
30,908,788✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
30,745,096✔
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
163,692✔
372
          SColCmprWrapper colCmprs = {0};
163,692✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
163,692✔
374
          if (code != 0) {
163,692✔
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
163,692✔
379
          taosMemoryFree(colCmprs.pColCmpr);
163,692✔
380
          TAOS_CHECK_RETURN(code);
163,692✔
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
182,488,763✔
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
186,715,879✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
45,532,782✔
388
    TAOS_CHECK_RETURN(tEncodeI64v(pCoder, pME->stbEntry.ownerId));
45,464,119✔
389
  } else if (pME->type == TSDB_NORMAL_TABLE) {
163,942,980✔
390
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.ownerId));
61,817,576✔
391
  }
392

393
  tEndEncode(pCoder);
186,700,932✔
394
  return 0;
186,741,988✔
395
}
396

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

403
  if (headerOnly) {
1,509,863,824✔
404
    tEndDecode(pCoder);
300,868✔
405
    return 0;
300,868✔
406
  }
407

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

411
    if (pME->type == TSDB_SUPER_TABLE) {
1,509,276,507✔
412
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,519,235,217✔
413
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,519,365,165✔
414
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,518,902,003✔
415
      if (TABLE_IS_ROLLUP(pME->flags)) {
759,334,763✔
416
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
267,843✔
417
      }
418
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
749,446,624✔
419
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
1,120,409,244✔
420
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
1,120,393,074✔
421
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
1,120,297,474✔
422
      if (pME->ctbEntry.commentLen > 0) {
560,093,578✔
423
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
46,824✔
424
      }
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
1,120,269,761✔
426
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
560,196,206✔
427
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
189,425,486✔
428
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
379,021,782✔
429
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
379,100,457✔
430
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
378,987,866✔
431
      if (pME->ntbEntry.commentLen > 0) {
189,432,865✔
432
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
67,776✔
433
      }
434
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
378,941,889✔
435
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
379,012,562✔
436
    } else if (pME->type == TSDB_TSMA_TABLE) {
×
UNCOV
437
      pME->smaEntry.tsma = tDecoderMalloc(pCoder, sizeof(STSma));
×
438
      if (!pME->smaEntry.tsma) {
×
UNCOV
439
        return terrno;
×
440
      }
441
      TAOS_CHECK_RETURN(tDecodeTSma(pCoder, pME->smaEntry.tsma, true));
×
442
    } else {
UNCOV
443
      metaError("meta/entry: invalide table type: %" PRId8 " decode failed.", pME->type);
×
UNCOV
444
      return TSDB_CODE_INVALID_PARA;
×
445
    }
446
    if (pME->type == TSDB_SUPER_TABLE) {
1,509,081,808✔
447
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
759,581,267✔
448
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
759,405,114✔
449

450
        if (pME->colCmpr.nCols == 0) {
759,365,290✔
451
          TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->stbEntry.schemaRow));
×
452
        }
453
      } else {
UNCOV
454
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->stbEntry.schemaRow));
×
UNCOV
455
        TABLE_SET_COL_COMPRESSED(pME->flags);
×
456
      }
457
    } else if (pME->type == TSDB_NORMAL_TABLE) {
749,327,130✔
458
      if (!tDecodeIsEnd(pCoder)) {
188,397,431✔
459
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
188,449,469✔
460
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
188,449,664✔
461
        if (pME->colCmpr.nCols == 0) {
188,448,932✔
462
          TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
463
        }
464
      } else {
UNCOV
465
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
UNCOV
466
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
467
      }
468
      TABLE_SET_COL_COMPRESSED(pME->flags);
188,455,690✔
469
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
561,011,368✔
470
      if (!tDecodeIsEnd(pCoder)) {
5,049,341✔
471
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
5,049,341✔
472
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
5,049,341✔
473
      } else {
UNCOV
474
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
UNCOV
475
        if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
×
UNCOV
476
           TAOS_CHECK_RETURN(metatInitDefaultSColRefWrapper(pCoder, &pME->colRef, &pME->ntbEntry.schemaRow));
×
477
        }
478
      }
479
    }
480
    if (!tDecodeIsEnd(pCoder)) {
1,508,987,441✔
481
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
947,830,516✔
482
    } else {
483
      pME->pExtSchemas = NULL;
561,156,925✔
484
    }
485
  }
486
  if (pME->type == TSDB_SUPER_TABLE) {
1,508,735,716✔
487
    if (!tDecodeIsEnd(pCoder)) {
759,157,585✔
488
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,518,021,887✔
489
    }
490
    if (!tDecodeIsEnd(pCoder)) {
759,120,228✔
491
      TAOS_CHECK_RETURN(tDecodeI64v(pCoder, &pME->stbEntry.ownerId));
1,518,388,147✔
492
    }
493
  } else if (pME->type == TSDB_NORMAL_TABLE) {
749,293,352✔
494
    if (!tDecodeIsEnd(pCoder)) {
188,484,727✔
495
      TAOS_CHECK_RETURN(tDecodeI64v(pCoder, &pME->ntbEntry.ownerId));
376,972,498✔
496
    }
497
  }
498

499

500
  tEndDecode(pCoder);
1,508,271,139✔
501
  return 0;
1,508,782,753✔
502
}
503

504
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
1,508,925,683✔
505

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

511
  pDst->nCols = pSrc->nCols;
282,270,824✔
512
  pDst->version = pSrc->version;
282,226,124✔
513
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
282,252,359✔
514
  if (pDst->pSchema == NULL) {
282,147,937✔
UNCOV
515
    return terrno;
×
516
  }
517
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
282,168,590✔
518
  return TSDB_CODE_SUCCESS;
282,270,453✔
519
}
520

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

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

549
  return TSDB_CODE_SUCCESS;
41,477✔
550
}
551

552
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
282,208,435✔
553
  if (pSchema) {
282,208,435✔
554
    taosMemoryFreeClear(pSchema->pSchema);
282,222,372✔
555
  }
556
}
282,211,206✔
557

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

571
void metaCloneEntryFree(SMetaEntry **ppEntry) {
161,262,227✔
572
  if (ppEntry == NULL || *ppEntry == NULL) {
161,262,227✔
573
    return;
70,383✔
574
  }
575

576
  taosMemoryFreeClear((*ppEntry)->name);
161,187,678✔
577

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

583
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
161,174,848✔
584
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
137,013,276✔
585
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
136,997,497✔
586
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
137,004,940✔
587
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
62,567✔
588
    }
589
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
24,178,799✔
590
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
15,972,471✔
591
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
15,973,098✔
592
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
8,205,701✔
593
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
8,205,701✔
594
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
8,205,701✔
595
  } else {
UNCOV
596
    return;
×
597
  }
598
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
161,200,768✔
599
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
161,169,743✔
600
  metaCloneColRefFree(&(*ppEntry)->colRef);
161,172,648✔
601

602
  taosMemoryFreeClear(*ppEntry);
161,152,365✔
603
  return;
161,167,546✔
604
}
605

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

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

613
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
161,208,577✔
614
  if (NULL == *ppEntry) {
161,133,829✔
UNCOV
615
    return terrno;
×
616
  }
617

618
  (*ppEntry)->version = pEntry->version;
161,167,945✔
619
  (*ppEntry)->type = pEntry->type;
161,181,704✔
620
  (*ppEntry)->uid = pEntry->uid;
161,188,555✔
621

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

626
  if (pEntry->name) {
161,183,549✔
627
    (*ppEntry)->name = tstrdup(pEntry->name);
161,198,827✔
628
    if (NULL == (*ppEntry)->name) {
161,208,174✔
UNCOV
629
      code = terrno;
×
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633
  }
634

635
  if (pEntry->type == TSDB_SUPER_TABLE) {
161,196,819✔
636
    (*ppEntry)->flags = pEntry->flags;
137,033,882✔
637

638
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
137,005,537✔
639
    if (code) {
137,025,715✔
UNCOV
640
      metaCloneEntryFree(ppEntry);
×
UNCOV
641
      return code;
×
642
    }
643

644
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
137,025,715✔
645
    if (code) {
137,042,399✔
UNCOV
646
      metaCloneEntryFree(ppEntry);
×
UNCOV
647
      return code;
×
648
    }
649
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
137,042,399✔
650
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
41,477✔
651
      if (code) {
41,477✔
UNCOV
652
        metaCloneEntryFree(ppEntry);
×
UNCOV
653
        return code;
×
654
      }
655
    }
656
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
137,043,445✔
657
    (*ppEntry)->stbEntry.ownerId = pEntry->stbEntry.ownerId;
137,027,941✔
658
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
24,178,799✔
659
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
15,973,098✔
660
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
15,973,098✔
661
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
15,973,098✔
662

663
    // comment
664
    (*ppEntry)->ctbEntry.commentLen = pEntry->ctbEntry.commentLen;
15,973,098✔
665
    if (pEntry->ctbEntry.commentLen > 0) {
15,973,098✔
666
      (*ppEntry)->ctbEntry.comment = taosMemoryMalloc(pEntry->ctbEntry.commentLen + 1);
7,994✔
667
      if (NULL == (*ppEntry)->ctbEntry.comment) {
7,994✔
668
        code = terrno;
×
669
        metaCloneEntryFree(ppEntry);
×
670
        return code;
×
671
      }
672
      memcpy((*ppEntry)->ctbEntry.comment, pEntry->ctbEntry.comment, pEntry->ctbEntry.commentLen + 1);
7,994✔
673
    }
674

675
    // tags
676
    STag *pTags = (STag *)pEntry->ctbEntry.pTags;
15,973,098✔
677
    (*ppEntry)->ctbEntry.pTags = taosMemoryCalloc(1, pTags->len);
15,973,098✔
678
    if (NULL == (*ppEntry)->ctbEntry.pTags) {
15,973,098✔
UNCOV
679
      code = terrno;
×
UNCOV
680
      metaCloneEntryFree(ppEntry);
×
681
      return code;
×
682
    }
683
    memcpy((*ppEntry)->ctbEntry.pTags, pEntry->ctbEntry.pTags, pTags->len);
15,973,098✔
684
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
8,205,701✔
685
    (*ppEntry)->ntbEntry.btime = pEntry->ntbEntry.btime;
8,205,701✔
686
    (*ppEntry)->ntbEntry.ttlDays = pEntry->ntbEntry.ttlDays;
8,205,701✔
687
    (*ppEntry)->ntbEntry.ncid = pEntry->ntbEntry.ncid;
8,205,701✔
688
    (*ppEntry)->ntbEntry.ownerId = pEntry->ntbEntry.ownerId;
8,205,701✔
689

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

697
    // comment
698
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
8,205,701✔
699
    if (pEntry->ntbEntry.commentLen > 0) {
8,205,701✔
700
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
11,354✔
701
      if (NULL == (*ppEntry)->ntbEntry.comment) {
11,354✔
UNCOV
702
        code = terrno;
×
703
        metaCloneEntryFree(ppEntry);
×
704
        return code;
×
705
      }
706
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
11,354✔
707
    }
708
  } else {
709
    return TSDB_CODE_INVALID_PARA;
×
710
  }
711

712
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
161,205,750✔
713
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
639,885✔
714
    if (code) {
625,631✔
UNCOV
715
      metaCloneEntryFree(ppEntry);
×
716
      return code;
×
717
    }
718
  } else {
719
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
160,581,460✔
720
    if (code) {
160,580,217✔
UNCOV
721
      metaCloneEntryFree(ppEntry);
×
UNCOV
722
      return code;
×
723
    }
724
  }
725
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
161,205,848✔
726
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
8,805,199✔
727
    if (!(*ppEntry)->pExtSchemas) {
8,795,930✔
UNCOV
728
      code = terrno;
×
UNCOV
729
      metaCloneEntryFree(ppEntry);
×
UNCOV
730
      return code;
×
731
    }
732
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
8,793,654✔
733
  }
734

735
  return code;
161,196,639✔
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