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

taosdata / TDengine / #4872

04 Dec 2025 01:55AM UTC coverage: 64.678% (+0.02%) from 64.654%
#4872

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

880 of 2219 new or added lines in 36 files covered. (39.66%)

6146 existing lines in 122 files now uncovered.

159679 of 246882 relevant lines covered (64.68%)

110947965.82 hits per line

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

80.5
/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) {
679,957,496✔
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;
38,738,034✔
26
    }
27
  }
28
  return false;
640,965,620✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
177,906,660✔
32
  if (pME->pExtSchemas) {
177,906,660✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
14,343,953✔
34
    bool                  hasTypeMods = false;
14,343,953✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
14,343,953✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
14,200,872✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
151,680✔
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
151,680✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
14,357,023✔
43

44
    for (int32_t i = 0; i < pSchWrapper->nCols && hasTypeMods; ++i) {
2,123,544,293✔
45
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->pExtSchemas[i].typeMod));
2,147,483,647✔
46
    }
47
  }
48
  return 0;
177,951,871✔
49
}
50

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

57
  for (int32_t i = 0; i < pw->nCols; i++) {
11,552,862✔
58
    SColRef *p = &pw->pColRef[i];
10,333,124✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
20,666,248✔
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
20,666,248✔
61
    if (p->hasRef) {
10,333,124✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
11,521,184✔
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
11,521,184✔
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
11,521,184✔
65
    }
66
  }
67
  return 0;
1,219,738✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
651,576,426✔
71
  bool hasExtSchema = false;
651,576,426✔
72
  SSchemaWrapper* pSchWrapper = NULL;
651,576,426✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
651,576,426✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
644,485,468✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
7,028,773✔
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
7,028,773✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
651,686,650✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
651,578,761✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
54,842,359✔
84
    if (pME->pExtSchemas == NULL) {
27,423,333✔
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;
624,160,864✔
93
  }
94

95
  return 0;
651,592,810✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
14,046,532✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
14,046,532✔
100
  bool                  hasTypeMods = false;
14,046,532✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
14,046,532✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
12,484,713✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
1,599,696✔
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
1,600,016✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
14,078,921✔
109

110
  if (hasTypeMods) {
14,029,762✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
50,437✔
112
    if (ret != NULL) {
50,437✔
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
50,437✔
114
    }
115
    return ret;
50,437✔
116
  }
117
  return NULL;
13,979,325✔
118
}
119

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

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

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

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
55,296✔
151
  int32_t    i = 0, j = 0;
55,296✔
152
  for (i = 0; i < nCols; ++i) {
425,472✔
153
    while (j < pParam->nFuncs) {
672,768✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
646,656✔
155
        pFuncIds[i] = pParam->funcIds[j];
302,592✔
156
        break;
302,592✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
344,064✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
67,584✔
160
        break;
67,584✔
161
      }
162
      ++j;
276,480✔
163
    }
164
    if (j >= pParam->nFuncs) {
396,288✔
165
      for (; i < nCols; ++i) {
52,224✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
26,112✔
167
      }
168
      break;
26,112✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
55,296✔
172

173
  return 0;
55,296✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
8,843,605✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
8,843,605✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
17,686,816✔
179
  if (pWrapper->nCols == 0) {
8,843,211✔
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
17,678,193✔
184
  uDebug("decode cols:%d", pWrapper->nCols);
8,834,588✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
17,677,813✔
186
  if (pWrapper->pColRef == NULL) {
8,843,225✔
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
93,357,612✔
191
    SColRef *p = &pWrapper->pColRef[i];
84,510,270✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
169,029,345✔
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
169,035,223✔
194
    if (p->hasRef) {
84,520,262✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
49,893,484✔
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
49,894,629✔
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
49,894,440✔
198
    }
199
  }
200
  return 0;
8,842,840✔
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) {
692,938✔
220
  if (pSrc->nCols > 0) {
692,938✔
221
    pDst->nCols = pSrc->nCols;
692,938✔
222
    pDst->version = pSrc->version;
692,938✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
692,938✔
224
    if (NULL == pDst->pColRef) {
692,938✔
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
692,938✔
228
  }
229
  return 0;
692,938✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
43,018,949✔
233
  int32_t code = 0;
43,018,949✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
86,045,205✔
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
86,086,238✔
236
  uTrace("encode cols:%d", pw->nCols);
43,059,982✔
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;
43,065,244✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
42,833,929✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
42,833,929✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
42,835,868✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
729,371,887✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
729,371,887✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
1,458,820,779✔
252
  if (pWrapper->nCols == 0) {
729,326,216✔
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
1,458,507,494✔
257
  uDebug("dencode cols:%d", pWrapper->nCols);
729,144,083✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
1,458,469,499✔
259
  if (pWrapper->pColCmpr == NULL) {
729,430,777✔
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;
729,591,633✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
239,762✔
273

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

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

284
  for (int32_t i = 0; i < pCmpr->nCols; i++) {
1,207,180✔
285
    SColCmpr *pColCmpr = &pCmpr->pColCmpr[i];
967,418✔
286
    SSchema  *pColSchema = &pSchema->pSchema[i];
967,418✔
287
    pColCmpr->id = pColSchema->colId;
967,418✔
288
    pColCmpr->alg = createDefaultColCmprByType(pColSchema->type);
967,418✔
289
  }
290
  return 0;
239,762✔
291
}
292

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
170,884,178✔
294
  if (pSrc->nCols > 0) {
170,884,178✔
295
    pDst->nCols = pSrc->nCols;
153,777,931✔
296
    pDst->version = pSrc->version;
153,761,694✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
153,765,318✔
298
    if (NULL == pDst->pColCmpr) {
153,762,843✔
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
153,762,085✔
302
  }
303
  return 0;
170,868,754✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
171,566,299✔
307
  if (pColRef) {
171,566,299✔
308
    taosMemoryFreeClear(pColRef->pColRef);
171,576,193✔
309
  }
310
}
171,567,757✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
171,570,035✔
313
  if (pCmpr) {
171,570,035✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
171,574,444✔
315
  }
316
}
171,581,691✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
181,812,805✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
181,812,805✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
363,616,661✔
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
363,607,621✔
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
363,604,925✔
323

324
  if (pME->type > 0) {
181,803,200✔
325
    if (pME->name == NULL) {
177,936,201✔
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
355,867,876✔
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
177,927,668✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
62,898,752✔
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
62,960,978✔
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
62,932,029✔
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
31,427,535✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
156,672✔
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
146,475,560✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
268,474,146✔
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
268,472,199✔
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
268,465,619✔
342
      if (pME->ctbEntry.commentLen > 0) {
134,232,074✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
15,088✔
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
268,464,668✔
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
134,231,989✔
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
12,242,698✔
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
24,485,396✔
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
24,485,396✔
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
24,485,396✔
351
      if (pME->ntbEntry.commentLen > 0) {
12,242,698✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
14,512✔
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
24,485,396✔
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
24,485,396✔
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) {
177,927,730✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,272,363✔
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
176,666,447✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
31,468,127✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
145,265,147✔
368
        if (pME->colCmpr.nCols != 0) {
11,597,484✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
11,357,722✔
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
239,762✔
372
          SColCmprWrapper colCmprs = {0};
239,762✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
239,762✔
374
          if (code != 0) {
239,762✔
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
239,762✔
379
          taosMemoryFree(colCmprs.pColCmpr);
239,762✔
380
          TAOS_CHECK_RETURN(code);
239,762✔
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
177,938,083✔
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
181,810,169✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
62,868,831✔
388
  }
389

390
  tEndEncode(pCoder);
181,746,639✔
391
  return 0;
181,794,288✔
392
}
393

394
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
1,279,871,302✔
395
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
1,279,871,302✔
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,280,134,398✔
401
    tEndDecode(pCoder);
146,052✔
402
    return 0;
146,052✔
403
  }
404

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

408
    if (pME->type == TSDB_SUPER_TABLE) {
1,279,808,161✔
409
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,289,389,276✔
410
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,289,406,601✔
411
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,289,377,373✔
412
      if (TABLE_IS_ROLLUP(pME->flags)) {
644,668,588✔
413
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
586,752✔
414
      }
415
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
634,979,533✔
416
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
1,097,144,374✔
417
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
1,097,269,242✔
418
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
1,097,140,127✔
419
      if (pME->ctbEntry.commentLen > 0) {
548,503,988✔
420
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
21,108✔
421
      }
422
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
1,097,129,433✔
423
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
548,632,985✔
424
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
86,589,896✔
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
173,204,124✔
426
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
173,215,444✔
427
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
173,196,312✔
428
      if (pME->ntbEntry.commentLen > 0) {
86,589,130✔
429
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
21,468✔
430
      }
431
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
173,195,776✔
432
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
173,202,886✔
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,279,627,546✔
444
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
644,698,713✔
445
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
644,687,463✔
446

447
        if (pME->colCmpr.nCols == 0) {
644,748,665✔
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) {
634,934,914✔
455
      if (!tDecodeIsEnd(pCoder)) {
84,830,330✔
456
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
84,835,291✔
457
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
84,836,056✔
458
        if (pME->colCmpr.nCols == 0) {
84,822,260✔
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);
84,823,599✔
466
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
550,108,080✔
467
      if (!tDecodeIsEnd(pCoder)) {
8,842,446✔
468
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
8,842,446✔
469
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
8,842,446✔
470
      } else {
471
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
472
        if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
×
473
           TAOS_CHECK_RETURN(metatInitDefaultSColRefWrapper(pCoder, &pME->colRef, &pME->ntbEntry.schemaRow));
×
474
        }
475
      }
476
    }
477
    if (!tDecodeIsEnd(pCoder)) {
1,279,851,405✔
478
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
651,709,902✔
479
    } else {
480
      pME->pExtSchemas = NULL;
628,141,503✔
481
    }
482
  }
483
  if (pME->type == TSDB_SUPER_TABLE) {
1,279,634,328✔
484
    if (!tDecodeIsEnd(pCoder)) {
644,587,838✔
485
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,289,307,907✔
486
    }
487
  }
488

489

490
  tEndDecode(pCoder);
1,279,771,244✔
491
  return 0;
1,279,727,256✔
492
}
493

494
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
1,279,758,812✔
495

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

501
  pDst->nCols = pSrc->nCols;
299,836,688✔
502
  pDst->version = pSrc->version;
299,833,938✔
503
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
299,841,650✔
504
  if (pDst->pSchema == NULL) {
299,787,853✔
505
    return terrno;
×
506
  }
507
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
299,810,872✔
508
  return TSDB_CODE_SUCCESS;
299,854,952✔
509
}
510

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

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

539
  return TSDB_CODE_SUCCESS;
90,624✔
540
}
541

542
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
299,826,551✔
543
  if (pSchema) {
299,826,551✔
544
    taosMemoryFreeClear(pSchema->pSchema);
299,830,192✔
545
  }
546
}
299,832,567✔
547

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

561
void metaCloneEntryFree(SMetaEntry **ppEntry) {
171,650,625✔
562
  if (ppEntry == NULL || *ppEntry == NULL) {
171,650,625✔
563
    return;
78,811✔
564
  }
565

566
  taosMemoryFreeClear((*ppEntry)->name);
171,573,985✔
567

568
  if ((*ppEntry)->type < 0) {
171,585,427✔
569
    taosMemoryFreeClear(*ppEntry);
×
570
    return;
×
571
  }
572

573
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
171,552,468✔
574
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
145,598,077✔
575
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
145,590,840✔
576
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
145,592,458✔
577
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
136,704✔
578
    }
579
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
25,965,329✔
580
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
17,337,516✔
581
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
17,337,218✔
582
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
8,628,111✔
583
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
8,628,111✔
584
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
8,628,111✔
585
  } else {
586
    return;
×
587
  }
588
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
171,556,043✔
589
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
171,553,708✔
590
  metaCloneColRefFree(&(*ppEntry)->colRef);
171,560,439✔
591

592
  taosMemoryFreeClear(*ppEntry);
171,538,213✔
593
  return;
171,564,878✔
594
}
595

596
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
171,565,499✔
597
  int32_t code = TSDB_CODE_SUCCESS;
171,565,499✔
598

599
  if (NULL == pEntry || NULL == ppEntry) {
171,565,499✔
600
    return TSDB_CODE_INVALID_PARA;
×
601
  }
602

603
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
171,574,088✔
604
  if (NULL == *ppEntry) {
171,541,216✔
605
    return terrno;
×
606
  }
607

608
  (*ppEntry)->version = pEntry->version;
171,549,708✔
609
  (*ppEntry)->type = pEntry->type;
171,553,170✔
610
  (*ppEntry)->uid = pEntry->uid;
171,557,585✔
611

612
  if (pEntry->type < 0) {
171,575,108✔
613
    return TSDB_CODE_SUCCESS;
×
614
  }
615

616
  if (pEntry->name) {
171,574,390✔
617
    (*ppEntry)->name = tstrdup(pEntry->name);
171,565,958✔
618
    if (NULL == (*ppEntry)->name) {
171,564,078✔
619
      code = terrno;
×
620
      metaCloneEntryFree(ppEntry);
×
621
      return code;
×
622
    }
623
  }
624

625
  if (pEntry->type == TSDB_SUPER_TABLE) {
171,567,871✔
626
    (*ppEntry)->flags = pEntry->flags;
145,591,553✔
627

628
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
145,603,074✔
629
    if (code) {
145,614,245✔
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633

634
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
145,614,245✔
635
    if (code) {
145,613,380✔
636
      metaCloneEntryFree(ppEntry);
×
637
      return code;
×
638
    }
639
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
145,613,380✔
640
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
90,624✔
641
      if (code) {
90,624✔
642
        metaCloneEntryFree(ppEntry);
×
643
        return code;
×
644
      }
645
    }
646
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
145,612,134✔
647
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
25,965,411✔
648
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
17,337,962✔
649
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
17,337,598✔
650
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
17,337,962✔
651

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

664
    // tags
665
    STag *pTags = (STag *)pEntry->ctbEntry.pTags;
17,337,962✔
666
    (*ppEntry)->ctbEntry.pTags = taosMemoryCalloc(1, pTags->len);
17,337,962✔
667
    if (NULL == (*ppEntry)->ctbEntry.pTags) {
17,337,962✔
668
      code = terrno;
×
669
      metaCloneEntryFree(ppEntry);
×
670
      return code;
×
671
    }
672
    memcpy((*ppEntry)->ctbEntry.pTags, pEntry->ctbEntry.pTags, pTags->len);
17,336,936✔
673
  } else if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
8,628,111✔
674
    (*ppEntry)->ntbEntry.btime = pEntry->ntbEntry.btime;
8,628,111✔
675
    (*ppEntry)->ntbEntry.ttlDays = pEntry->ntbEntry.ttlDays;
8,628,111✔
676
    (*ppEntry)->ntbEntry.ncid = pEntry->ntbEntry.ncid;
8,628,111✔
677

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

685
    // comment
686
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
8,628,111✔
687
    if (pEntry->ntbEntry.commentLen > 0) {
8,628,111✔
688
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
4,550✔
689
      if (NULL == (*ppEntry)->ntbEntry.comment) {
4,550✔
690
        code = terrno;
×
691
        metaCloneEntryFree(ppEntry);
×
692
        return code;
×
693
      }
694
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
4,550✔
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) {
171,568,319✔
701
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
708,660✔
702
    if (code) {
692,938✔
703
      metaCloneEntryFree(ppEntry);
×
704
      return code;
×
705
    }
706
  } else {
707
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
170,875,252✔
708
    if (code) {
170,879,271✔
709
      metaCloneEntryFree(ppEntry);
×
710
      return code;
×
711
    }
712
  }
713
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
171,572,209✔
714
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
12,112,708✔
715
    if (!(*ppEntry)->pExtSchemas) {
12,110,747✔
716
      code = terrno;
×
717
      metaCloneEntryFree(ppEntry);
×
718
      return code;
×
719
    }
720
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
12,107,843✔
721
  }
722

723
  return code;
171,569,766✔
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