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

taosdata / TDengine / #4858

17 Nov 2025 09:53AM UTC coverage: 64.048% (-0.09%) from 64.135%
#4858

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

218 of 311 new or added lines in 32 files covered. (70.1%)

4856 existing lines in 134 files now uncovered.

151096 of 235910 relevant lines covered (64.05%)

115767925.69 hits per line

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

80.92
/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) {
694,406,396✔
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;
39,637,756✔
26
    }
27
  }
28
  return false;
651,560,492✔
29
}
30

31
static int32_t metaEncodeExtSchema(SEncoder* pCoder, const SMetaEntry* pME) {
178,163,528✔
32
  if (pME->pExtSchemas) {
178,163,528✔
33
    const SSchemaWrapper *pSchWrapper = NULL;
13,800,639✔
34
    bool                  hasTypeMods = false;
13,800,639✔
35
    if (pME->type == TSDB_SUPER_TABLE) {
13,800,639✔
36
      pSchWrapper = &pME->stbEntry.schemaRow;
13,661,902✔
37
    } else if (pME->type == TSDB_NORMAL_TABLE) {
151,502✔
38
      pSchWrapper = &pME->ntbEntry.schemaRow;
151,502✔
39
    } else {
40
      return 0;
×
41
    }
42
    hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
13,816,326✔
43

44
    for (int32_t i = 0; i < pSchWrapper->nCols && hasTypeMods; ++i) {
1,931,577,224✔
45
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->pExtSchemas[i].typeMod));
2,147,483,647✔
46
    }
47
  }
48
  return 0;
178,208,043✔
49
}
50

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

57
  for (int32_t i = 0; i < pw->nCols; i++) {
12,277,370✔
58
    SColRef *p = &pw->pColRef[i];
11,039,828✔
59
    TAOS_CHECK_RETURN(tEncodeI8(pCoder, p->hasRef));
22,079,656✔
60
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
22,079,656✔
61
    if (p->hasRef) {
11,039,828✔
62
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refDbName));
12,266,828✔
63
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refTableName));
12,266,828✔
64
      TAOS_CHECK_RETURN(tEncodeCStr(pCoder, p->refColName));
12,266,828✔
65
    }
66
  }
67
  return 0;
1,237,542✔
68
}
69

70
static int32_t metaDecodeExtSchemas(SDecoder* pDecoder, SMetaEntry* pME) {
666,496,268✔
71
  bool hasExtSchema = false;
666,496,268✔
72
  SSchemaWrapper* pSchWrapper = NULL;
666,496,268✔
73
  if (pME->type == TSDB_SUPER_TABLE) {
666,496,268✔
74
    pSchWrapper = &pME->stbEntry.schemaRow;
659,240,822✔
75
  } else if (pME->type == TSDB_NORMAL_TABLE) {
7,315,665✔
76
    pSchWrapper = &pME->ntbEntry.schemaRow;
7,315,665✔
77
  } else {
78
    return 0;
×
79
  }
80

81
  hasExtSchema = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
666,610,729✔
82
  if (hasExtSchema && pSchWrapper->nCols > 0) {
666,492,288✔
83
    pME->pExtSchemas = (SExtSchema*)tDecoderMalloc(pDecoder, sizeof(SExtSchema) * pSchWrapper->nCols);
57,543,603✔
84
    if (pME->pExtSchemas == NULL) {
28,777,418✔
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;
637,725,534✔
93
  }
94

95
  return 0;
666,536,549✔
96
}
97

98
SExtSchema* metaGetSExtSchema(const SMetaEntry *pME) {
14,119,295✔
99
  const SSchemaWrapper *pSchWrapper = NULL;
14,119,295✔
100
  bool                  hasTypeMods = false;
14,119,295✔
101
  if (pME->type == TSDB_SUPER_TABLE) {
14,119,295✔
102
    pSchWrapper = &pME->stbEntry.schemaRow;
12,483,044✔
103
  } else if (pME->type == TSDB_NORMAL_TABLE) {
1,639,181✔
104
    pSchWrapper = &pME->ntbEntry.schemaRow;
1,639,755✔
105
  } else {
106
    return NULL;
×
107
  }
108
  hasTypeMods = schemasHasTypeMod(pSchWrapper->pSchema, pSchWrapper->nCols);
14,121,695✔
109

110
  if (hasTypeMods) {
14,116,228✔
111
    SExtSchema *ret = taosMemoryMalloc(sizeof(SExtSchema) * pSchWrapper->nCols);
50,013✔
112
    if (ret != NULL) {
50,013✔
113
      memcpy(ret, pME->pExtSchemas, pSchWrapper->nCols * sizeof(SExtSchema));
50,013✔
114
    }
115
    return ret;
50,013✔
116
  }
117
  return NULL;
14,066,215✔
118
}
119

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

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

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

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

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

150
  func_id_t *pFuncIds = (*rsmaSchema)->funcIds;
54,648✔
151
  int32_t    i = 0, j = 0;
54,648✔
152
  for (i = 0; i < nCols; ++i) {
420,486✔
153
    while (j < pParam->nFuncs) {
664,884✔
154
      if (pParam->funcColIds[j] == pSchema[i].colId) {
639,078✔
155
        pFuncIds[i] = pParam->funcIds[j];
299,046✔
156
        break;
299,046✔
157
      }
158
      if (pParam->funcColIds[j] > pSchema[i].colId) {
340,032✔
159
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
66,792✔
160
        break;
66,792✔
161
      }
162
      ++j;
273,240✔
163
    }
164
    if (j >= pParam->nFuncs) {
391,644✔
165
      for (; i < nCols; ++i) {
51,612✔
166
        pFuncIds[i] = 36;  // use last if not specified, fmGetFuncId("last") = 36
25,806✔
167
      }
168
      break;
25,806✔
169
    }
170
  }
171
  pFuncIds[0] = 0;  // Primary TS column has no function
54,648✔
172

173
  return 0;
54,648✔
174
}
175

176
int meteDecodeColRefEntry(SDecoder *pDecoder, SMetaEntry *pME) {
13,740,246✔
177
  SColRefWrapper *pWrapper = &pME->colRef;
13,740,246✔
178
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
27,478,620✔
179
  if (pWrapper->nCols == 0) {
13,739,310✔
180
    return 0;
×
181
  }
182

183
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
27,480,492✔
184
  uDebug("decode cols:%d", pWrapper->nCols);
13,740,246✔
185
  pWrapper->pColRef = (SColRef *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColRef));
27,479,892✔
186
  if (pWrapper->pColRef == NULL) {
13,739,646✔
187
    return terrno;
×
188
  }
189

190
  for (int i = 0; i < pWrapper->nCols; i++) {
220,657,572✔
191
    SColRef *p = &pWrapper->pColRef[i];
206,915,247✔
192
    TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t *)&p->hasRef));
413,847,286✔
193
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
413,851,272✔
194
    if (p->hasRef) {
206,925,619✔
195
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refDbName));
119,670,497✔
196
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refTableName));
119,670,054✔
197
      TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, p->refColName));
119,671,727✔
198
    }
199
  }
200
  return 0;
13,740,246✔
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) {
684,875✔
220
  if (pSrc->nCols > 0) {
684,875✔
221
    pDst->nCols = pSrc->nCols;
684,875✔
222
    pDst->version = pSrc->version;
684,875✔
223
    pDst->pColRef = (SColRef*)taosMemoryCalloc(pSrc->nCols, sizeof(SColRef));
684,875✔
224
    if (NULL == pDst->pColRef) {
684,875✔
225
      return terrno;
×
226
    }
227
    memcpy(pDst->pColRef, pSrc->pColRef, pSrc->nCols * sizeof(SColRef));
684,875✔
228
  }
229
  return 0;
684,875✔
230
}
231

232
static int32_t metaEncodeComprEntryImpl(SEncoder *pCoder, SColCmprWrapper *pw) {
42,536,480✔
233
  int32_t code = 0;
42,536,480✔
234
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
85,078,438✔
235
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
85,109,799✔
236
  uTrace("encode cols:%d", pw->nCols);
42,567,841✔
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;
42,606,478✔
244
}
245
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
42,454,371✔
246
  const SColCmprWrapper *pw = &pME->colCmpr;
42,454,371✔
247
  return metaEncodeComprEntryImpl(pCoder, (SColCmprWrapper *)pw);
42,469,640✔
248
}
249
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
808,338,448✔
250
  SColCmprWrapper *pWrapper = &pME->colCmpr;
808,338,448✔
251
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
1,616,781,289✔
252
  if (pWrapper->nCols == 0) {
808,359,748✔
253
    return 0;
×
254
  }
255

256
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
1,616,734,322✔
257
  uDebug("dencode cols:%d", pWrapper->nCols);
808,411,295✔
258
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
1,616,702,059✔
259
  if (pWrapper->pColCmpr == NULL) {
808,455,692✔
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;
808,618,980✔
269
}
270
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
271
                                                            SSchemaWrapper *pSchema) {
272
  pCmpr->nCols = pSchema->nCols;
160,844✔
273

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

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

284
  for (int32_t i = 0; i < pCmpr->nCols; i++) {
732,870✔
285
    SColCmpr *pColCmpr = &pCmpr->pColCmpr[i];
572,026✔
286
    SSchema  *pColSchema = &pSchema->pSchema[i];
572,026✔
287
    pColCmpr->id = pColSchema->colId;
572,026✔
288
    pColCmpr->alg = createDefaultColCmprByType(pColSchema->type);
572,026✔
289
  }
290
  return 0;
160,844✔
291
}
292

293
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
170,411,725✔
294
  if (pSrc->nCols > 0) {
170,411,725✔
295
    pDst->nCols = pSrc->nCols;
153,597,745✔
296
    pDst->version = pSrc->version;
153,601,564✔
297
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
153,580,663✔
298
    if (NULL == pDst->pColCmpr) {
153,571,055✔
299
      return terrno;
×
300
    }
301
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
153,576,175✔
302
  }
303
  return 0;
170,400,051✔
304
}
305

306
static void metaCloneColRefFree(SColRefWrapper *pColRef) {
171,097,625✔
307
  if (pColRef) {
171,097,625✔
308
    taosMemoryFreeClear(pColRef->pColRef);
171,102,877✔
309
  }
310
}
171,101,215✔
311

312
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
171,083,536✔
313
  if (pCmpr) {
171,083,536✔
314
    taosMemoryFreeClear(pCmpr->pColCmpr);
171,093,253✔
315
  }
316
}
171,095,749✔
317

318
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
182,081,006✔
319
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
182,081,006✔
320
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
364,190,695✔
321
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
364,177,255✔
322
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
364,182,455✔
323

324
  if (pME->type > 0) {
182,094,174✔
325
    if (pME->name == NULL) {
178,201,616✔
326
      return TSDB_CODE_INVALID_PARA;
×
327
    }
328

329
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
356,351,353✔
330

331
    if (pME->type == TSDB_SUPER_TABLE) {
178,180,250✔
332
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
62,410,038✔
333
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
62,495,552✔
334
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
62,484,507✔
335
      if (TABLE_IS_ROLLUP(pME->flags)) {
31,209,504✔
336
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
155,595✔
337
      }
338
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
146,967,957✔
339
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
269,893,626✔
340
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
269,892,096✔
341
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
269,891,317✔
342
      if (pME->ctbEntry.commentLen > 0) {
134,946,463✔
343
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
15,040✔
344
      }
345
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
269,894,235✔
346
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
134,948,958✔
347
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
12,024,944✔
348
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
24,049,888✔
349
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
24,049,888✔
350
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
24,049,888✔
351
      if (pME->ntbEntry.commentLen > 0) {
12,024,944✔
352
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
13,920✔
353
      }
354
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
24,049,888✔
355
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
24,049,888✔
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) {
178,198,203✔
363
      TAOS_CHECK_RETURN(meteEncodeColRefEntry(pCoder, pME));
1,298,995✔
364
    } else {
365
      if (pME->type == TSDB_SUPER_TABLE && TABLE_IS_COL_COMPRESSED(pME->flags)) {
176,905,386✔
366
        TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
31,254,932✔
367
      } else if (pME->type == TSDB_NORMAL_TABLE) {
145,734,031✔
368
        if (pME->colCmpr.nCols != 0) {
11,363,844✔
369
          TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
11,203,000✔
370
        } else {
371
          metaWarn("meta/entry: failed to get compress cols, type:%d", pME->type);
160,844✔
372
          SColCmprWrapper colCmprs = {0};
160,844✔
373
          int32_t code = metatInitDefaultSColCmprWrapper(NULL, &colCmprs, (SSchemaWrapper *)&pME->ntbEntry.schemaRow);
160,844✔
374
          if (code != 0) {
160,844✔
375
            taosMemoryFree(colCmprs.pColCmpr);
×
376
            TAOS_CHECK_RETURN(code);
×
377
          }
378
          code = metaEncodeComprEntryImpl(pCoder, &colCmprs);
160,844✔
379
          taosMemoryFree(colCmprs.pColCmpr);
160,844✔
380
          TAOS_CHECK_RETURN(code);
160,844✔
381
        }
382
      }
383
    }
384
    TAOS_CHECK_RETURN(metaEncodeExtSchema(pCoder, pME));
178,209,657✔
385
  }
386
  if (pME->type == TSDB_SUPER_TABLE) {
182,085,655✔
387
    TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->stbEntry.keep));
62,441,862✔
388
  }
389

390
  tEndEncode(pCoder);
182,062,946✔
391
  return 0;
182,077,173✔
392
}
393

394
int metaDecodeEntryImpl(SDecoder *pCoder, SMetaEntry *pME, bool headerOnly) {
1,401,614,338✔
395
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
1,401,614,338✔
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,401,988,334✔
401
    tEndDecode(pCoder);
97,936✔
402
    return 0;
97,936✔
403
  }
404

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

408
    if (pME->type == TSDB_SUPER_TABLE) {
1,401,783,154✔
409
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
1,318,716,628✔
410
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
1,318,745,761✔
411
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
1,318,631,679✔
412
      if (TABLE_IS_ROLLUP(pME->flags)) {
659,260,734✔
413
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
581,394✔
414
      }
415
    } else if (pME->type == TSDB_CHILD_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
742,239,850✔
416
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
1,177,997,464✔
417
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
1,178,109,137✔
418
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
1,178,041,445✔
419
      if (pME->ctbEntry.commentLen > 0) {
588,989,668✔
420
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
21,160✔
421
      }
422
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
1,177,985,368✔
423
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
589,050,711✔
424
    } else if (pME->type == TSDB_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_NORMAL_TABLE) {
153,422,099✔
425
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
306,876,986✔
426
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
306,893,456✔
427
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
306,881,799✔
428
      if (pME->ntbEntry.commentLen > 0) {
153,434,963✔
429
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
20,656✔
430
      }
431
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
306,873,256✔
432
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
306,882,617✔
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,401,673,100✔
444
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
659,304,487✔
445
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
659,333,425✔
446

447
        if (pME->colCmpr.nCols == 0) {
659,421,666✔
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) {
742,222,304✔
455
      if (!tDecodeIsEnd(pCoder)) {
149,181,163✔
456
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
149,180,387✔
457
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
149,180,387✔
458
        if (pME->colCmpr.nCols == 0) {
149,171,984✔
459
          TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
460
        }
461
      } else {
462
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
776✔
463
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
776✔
464
      }
465
      TABLE_SET_COL_COMPRESSED(pME->flags);
149,175,297✔
466
    } else if (pME->type == TSDB_VIRTUAL_NORMAL_TABLE || pME->type == TSDB_VIRTUAL_CHILD_TABLE) {
592,985,782✔
467
      if (!tDecodeIsEnd(pCoder)) {
13,739,688✔
468
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
13,739,688✔
469
        TAOS_CHECK_RETURN(meteDecodeColRefEntry(pCoder, pME));
13,739,688✔
470
      } else {
UNCOV
471
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
UNCOV
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,401,746,107✔
478
      TAOS_CHECK_RETURN(metaDecodeExtSchemas(pCoder, pME));
666,658,573✔
479
    } else {
480
      pME->pExtSchemas = NULL;
735,087,534✔
481
    }
482
  }
483
  if (pME->type == TSDB_SUPER_TABLE) {
1,401,569,306✔
484
    if (!tDecodeIsEnd(pCoder)) {
659,211,254✔
485
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->stbEntry.keep));
1,318,660,286✔
486
    }
487
  }
488

489

490
  tEndDecode(pCoder);
1,401,692,385✔
491
  return 0;
1,401,458,310✔
492
}
493

494
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { return metaDecodeEntryImpl(pCoder, pME, false); }
1,401,576,219✔
495

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

501
  pDst->nCols = pSrc->nCols;
299,970,986✔
502
  pDst->version = pSrc->version;
299,966,776✔
503
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
299,956,015✔
504
  if (pDst->pSchema == NULL) {
299,865,859✔
505
    return terrno;
×
506
  }
507
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
299,892,048✔
508
  return TSDB_CODE_SUCCESS;
299,974,770✔
509
}
510

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

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

539
  return TSDB_CODE_SUCCESS;
89,562✔
540
}
541

542
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
299,957,457✔
543
  if (pSchema) {
299,957,457✔
544
    taosMemoryFreeClear(pSchema->pSchema);
299,952,304✔
545
  }
546
}
299,969,398✔
547

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

561
void metaCloneEntryFree(SMetaEntry **ppEntry) {
171,173,083✔
562
  if (ppEntry == NULL || *ppEntry == NULL) {
171,173,083✔
563
    return;
77,211✔
564
  }
565

566
  taosMemoryFreeClear((*ppEntry)->name);
171,101,649✔
567

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

573
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
171,074,430✔
574
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
145,919,773✔
575
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
145,897,940✔
576
    if (TABLE_IS_ROLLUP((*ppEntry)->flags)) {
145,899,430✔
577
      metaFreeRsmaParam(&(*ppEntry)->stbEntry.rsmaParam, 1);
133,584✔
578
    }
579
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_CHILD_TABLE == (*ppEntry)->type) {
25,176,845✔
580
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
17,043,651✔
581
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
17,045,087✔
582
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type || TSDB_VIRTUAL_NORMAL_TABLE == (*ppEntry)->type) {
8,132,653✔
583
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
8,132,653✔
584
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
8,132,653✔
585
  } else {
586
    return;
×
587
  }
588
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
171,074,764✔
589
  taosMemoryFreeClear((*ppEntry)->pExtSchemas);
171,076,740✔
590
  metaCloneColRefFree(&(*ppEntry)->colRef);
171,085,523✔
591

592
  taosMemoryFreeClear(*ppEntry);
171,061,498✔
593
  return;
171,077,805✔
594
}
595

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

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

603
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
171,100,291✔
604
  if (NULL == *ppEntry) {
171,047,389✔
605
    return terrno;
×
606
  }
607

608
  (*ppEntry)->version = pEntry->version;
171,056,555✔
609
  (*ppEntry)->type = pEntry->type;
171,069,713✔
610
  (*ppEntry)->uid = pEntry->uid;
171,083,785✔
611

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

616
  if (pEntry->name) {
171,099,465✔
617
    (*ppEntry)->name = tstrdup(pEntry->name);
171,078,124✔
618
    if (NULL == (*ppEntry)->name) {
171,092,473✔
619
      code = terrno;
×
620
      metaCloneEntryFree(ppEntry);
×
621
      return code;
×
622
    }
623
  }
624

625
  if (pEntry->type == TSDB_SUPER_TABLE) {
171,101,227✔
626
    (*ppEntry)->flags = pEntry->flags;
145,889,705✔
627

628
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
145,915,402✔
629
    if (code) {
145,921,157✔
630
      metaCloneEntryFree(ppEntry);
×
631
      return code;
×
632
    }
633

634
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
145,921,157✔
635
    if (code) {
145,923,722✔
636
      metaCloneEntryFree(ppEntry);
×
637
      return code;
×
638
    }
639
    if (TABLE_IS_ROLLUP(pEntry->flags)) {
145,923,722✔
640
      code = metaCloneRsmaParam(&pEntry->stbEntry.rsmaParam, &(*ppEntry)->stbEntry.rsmaParam);
88,044✔
641
      if (code) {
88,803✔
642
        metaCloneEntryFree(ppEntry);
×
643
        return code;
×
644
      }
645
    }
646
    (*ppEntry)->stbEntry.keep = pEntry->stbEntry.keep;
145,926,007✔
647
  } else if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
25,177,281✔
648
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
17,044,628✔
649
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
17,045,087✔
650
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
17,045,087✔
651

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

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

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

685
    // comment
686
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
8,132,653✔
687
    if (pEntry->ntbEntry.commentLen > 0) {
8,132,653✔
688
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
4,372✔
689
      if (NULL == (*ppEntry)->ntbEntry.comment) {
4,372✔
690
        code = terrno;
×
691
        metaCloneEntryFree(ppEntry);
×
692
        return code;
×
693
      }
694
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
4,372✔
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,096,042✔
701
    code = metaCloneColRef(&pEntry->colRef, &(*ppEntry)->colRef);
711,567✔
702
    if (code) {
684,875✔
703
      metaCloneEntryFree(ppEntry);
×
704
      return code;
×
705
    }
706
  } else {
707
    code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
170,396,991✔
708
    if (code) {
170,401,968✔
709
      metaCloneEntryFree(ppEntry);
×
710
      return code;
×
711
    }
712
  }
713
  if (pEntry->pExtSchemas && pEntry->colCmpr.nCols > 0) {
171,086,843✔
714
    (*ppEntry)->pExtSchemas = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
11,669,760✔
715
    if (!(*ppEntry)->pExtSchemas) {
11,667,331✔
716
      code = terrno;
×
717
      metaCloneEntryFree(ppEntry);
×
718
      return code;
×
719
    }
720
    memcpy((*ppEntry)->pExtSchemas, pEntry->pExtSchemas, sizeof(SExtSchema) * pEntry->colCmpr.nCols);
11,662,716✔
721
  }
722

723
  return code;
171,091,347✔
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