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

taosdata / TDengine / #3584

17 Jan 2025 07:28AM UTC coverage: 63.756% (-0.1%) from 63.876%
#3584

push

travis-ci

web-flow
Merge pull request #29594 from taosdata/fix/insert-when-2-replicas

fix/insert-when-2-replicas

141233 of 284535 branches covered (49.64%)

Branch coverage included in aggregate %.

0 of 2 new or added lines in 1 file covered. (0.0%)

684 existing lines in 111 files now uncovered.

219774 of 281695 relevant lines covered (78.02%)

18696822.13 hits per line

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

66.28
/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

18
int meteEncodeColCmprEntry(SEncoder *pCoder, const SMetaEntry *pME) {
475,031✔
19
  const SColCmprWrapper *pw = &pME->colCmpr;
475,031✔
20
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->nCols));
950,062!
21
  TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pw->version));
950,062!
22
  uDebug("encode cols:%d", pw->nCols);
475,031✔
23

24
  for (int32_t i = 0; i < pw->nCols; i++) {
5,955,888✔
25
    SColCmpr *p = &pw->pColCmpr[i];
5,480,916✔
26
    TAOS_CHECK_RETURN(tEncodeI16v(pCoder, p->id));
10,961,832!
27
    TAOS_CHECK_RETURN(tEncodeU32(pCoder, p->alg));
10,961,832!
28
  }
29
  return 0;
474,972✔
30
}
31
int meteDecodeColCmprEntry(SDecoder *pDecoder, SMetaEntry *pME) {
20,454,238✔
32
  SColCmprWrapper *pWrapper = &pME->colCmpr;
20,454,238✔
33
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->nCols));
40,775,259!
34
  if (pWrapper->nCols == 0) {
20,321,021!
35
    return 0;
×
36
  }
37

38
  TAOS_CHECK_RETURN(tDecodeI32v(pDecoder, &pWrapper->version));
40,640,663!
39
  uDebug("dencode cols:%d", pWrapper->nCols);
20,319,642✔
40
  pWrapper->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pWrapper->nCols * sizeof(SColCmpr));
20,319,775✔
41
  if (pWrapper->pColCmpr == NULL) {
20,477,658!
42
    return terrno;
×
43
  }
44

45
  for (int i = 0; i < pWrapper->nCols; i++) {
420,156,731✔
46
    SColCmpr *p = &pWrapper->pColCmpr[i];
401,810,428✔
47
    TAOS_CHECK_RETURN(tDecodeI16v(pDecoder, &p->id));
802,525,996!
48
    TAOS_CHECK_RETURN(tDecodeU32(pDecoder, &p->alg));
800,394,641!
49
  }
50
  return 0;
18,346,303✔
51
}
52
static FORCE_INLINE int32_t metatInitDefaultSColCmprWrapper(SDecoder *pDecoder, SColCmprWrapper *pCmpr,
53
                                                            SSchemaWrapper *pSchema) {
54
  pCmpr->nCols = pSchema->nCols;
×
55
  if ((pCmpr->pColCmpr = (SColCmpr *)tDecoderMalloc(pDecoder, pCmpr->nCols * sizeof(SColCmpr))) == NULL) {
24!
56
    return terrno;
×
57
  }
58

59
  for (int32_t i = 0; i < pCmpr->nCols; i++) {
252!
60
    SColCmpr *pColCmpr = &pCmpr->pColCmpr[i];
228✔
61
    SSchema  *pColSchema = &pSchema->pSchema[i];
228✔
62
    pColCmpr->id = pColSchema->colId;
228✔
63
    pColCmpr->alg = createDefaultColCmprByType(pColSchema->type);
228✔
64
  }
65
  return 0;
24✔
66
}
67

68
static int32_t metaCloneColCmpr(const SColCmprWrapper *pSrc, SColCmprWrapper *pDst) {
225,875✔
69
  if (pSrc->nCols > 0) {
225,875✔
70
    pDst->nCols = pSrc->nCols;
215,373✔
71
    pDst->version = pSrc->version;
215,373✔
72
    pDst->pColCmpr = (SColCmpr *)taosMemoryCalloc(pSrc->nCols, sizeof(SColCmpr));
215,373!
73
    if (NULL == pDst->pColCmpr) {
215,458!
74
      return terrno;
×
75
    }
76
    memcpy(pDst->pColCmpr, pSrc->pColCmpr, pSrc->nCols * sizeof(SColCmpr));
215,458✔
77
  }
78
  return 0;
225,960✔
79
}
80

81
static void metaCloneColCmprFree(SColCmprWrapper *pCmpr) {
225,942✔
82
  if (pCmpr) {
225,942!
83
    taosMemoryFreeClear(pCmpr->pColCmpr);
225,947!
84
  }
85
}
225,964✔
86

87
int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) {
484,490✔
88
  TAOS_CHECK_RETURN(tStartEncode(pCoder));
484,490✔
89
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->version));
970,270!
90
  TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->type));
970,270!
91
  TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->uid));
970,270!
92

93
  if (pME->type > 0) {
485,135✔
94
    if (pME->name == NULL) {
474,881!
95
      return TSDB_CODE_INVALID_PARA;
×
96
    }
97

98
    TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->name));
949,762!
99

100
    if (pME->type == TSDB_SUPER_TABLE) {
474,881✔
101
      TAOS_CHECK_RETURN(tEncodeI8(pCoder, pME->flags));
144,888!
102
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaRow));
144,888!
103
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->stbEntry.schemaTag));
144,888!
104
      if (TABLE_IS_ROLLUP(pME->flags)) {
72,444✔
105
        TAOS_CHECK_RETURN(tEncodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
14!
106
      }
107
    } else if (pME->type == TSDB_CHILD_TABLE) {
402,437✔
108
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.btime));
747,630!
109
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ctbEntry.ttlDays));
747,630!
110
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ctbEntry.commentLen));
747,630!
111
      if (pME->ctbEntry.commentLen > 0) {
373,815✔
112
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ctbEntry.comment));
140!
113
      }
114
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ctbEntry.suid));
747,630!
115
      TAOS_CHECK_RETURN(tEncodeTag(pCoder, (const STag *)pME->ctbEntry.pTags));
373,815!
116
    } else if (pME->type == TSDB_NORMAL_TABLE) {
28,622!
117
      TAOS_CHECK_RETURN(tEncodeI64(pCoder, pME->ntbEntry.btime));
57,944!
118
      TAOS_CHECK_RETURN(tEncodeI32(pCoder, pME->ntbEntry.ttlDays));
57,944!
119
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.commentLen));
57,944!
120
      if (pME->ntbEntry.commentLen > 0) {
28,972✔
121
        TAOS_CHECK_RETURN(tEncodeCStr(pCoder, pME->ntbEntry.comment));
140!
122
      }
123
      TAOS_CHECK_RETURN(tEncodeI32v(pCoder, pME->ntbEntry.ncid));
57,944!
124
      TAOS_CHECK_RETURN(tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schemaRow));
57,944!
125
    } else if (pME->type == TSDB_TSMA_TABLE) {
×
126
      TAOS_CHECK_RETURN(tEncodeTSma(pCoder, pME->smaEntry.tsma));
62!
127
    } else {
128
      metaError("meta/entry: invalide table type: %" PRId8 " encode failed.", pME->type);
×
129
      return TSDB_CODE_INVALID_PARA;
×
130
    }
131
    TAOS_CHECK_RETURN(meteEncodeColCmprEntry(pCoder, pME));
475,290!
132
  }
133

134
  tEndEncode(pCoder);
485,312✔
135
  return 0;
484,911✔
136
}
137

138
int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) {
29,851,346✔
139
  TAOS_CHECK_RETURN(tStartDecode(pCoder));
29,851,346!
140
  TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->version));
59,551,019!
141
  TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->type));
59,032,908!
142
  TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->uid));
58,779,034!
143

144
  if (pME->type > 0) {
29,350,538!
145
    TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->name));
59,344,726!
146

147
    if (pME->type == TSDB_SUPER_TABLE) {
29,878,369✔
148
      TAOS_CHECK_RETURN(tDecodeI8(pCoder, &pME->flags));
36,371,428!
149
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaRow));
35,218,738!
150
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->stbEntry.schemaTag));
35,897,853!
151
      if (TABLE_IS_ROLLUP(pME->flags)) {
18,792,274✔
152
        TAOS_CHECK_RETURN(tDecodeSRSmaParam(pCoder, &pME->stbEntry.rsmaParam));
268!
153
      }
154
    } else if (pME->type == TSDB_CHILD_TABLE) {
11,620,100✔
155
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.btime));
18,879,562!
156
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ctbEntry.ttlDays));
18,829,423!
157
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ctbEntry.commentLen));
18,836,031!
158
      if (pME->ctbEntry.commentLen > 0) {
9,426,837✔
159
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ctbEntry.comment));
290!
160
      }
161
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ctbEntry.suid));
18,849,054!
162
      TAOS_CHECK_RETURN(tDecodeTag(pCoder, (STag **)&pME->ctbEntry.pTags));
9,422,217!
163
    } else if (pME->type == TSDB_NORMAL_TABLE) {
2,160,767!
164
      TAOS_CHECK_RETURN(tDecodeI64(pCoder, &pME->ntbEntry.btime));
4,439,614!
165
      TAOS_CHECK_RETURN(tDecodeI32(pCoder, &pME->ntbEntry.ttlDays));
4,435,800!
166
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.commentLen));
4,436,252!
167
      if (pME->ntbEntry.commentLen > 0) {
2,218,735✔
168
        TAOS_CHECK_RETURN(tDecodeCStr(pCoder, &pME->ntbEntry.comment));
332!
169
      }
170
      TAOS_CHECK_RETURN(tDecodeI32v(pCoder, &pME->ntbEntry.ncid));
4,436,220!
171
      TAOS_CHECK_RETURN(tDecodeSSchemaWrapperEx(pCoder, &pME->ntbEntry.schemaRow));
4,496,286!
172
    } else if (pME->type == TSDB_TSMA_TABLE) {
×
173
      pME->smaEntry.tsma = tDecoderMalloc(pCoder, sizeof(STSma));
1✔
174
      if (!pME->smaEntry.tsma) {
1!
175
        return terrno;
×
176
      }
177
      TAOS_CHECK_RETURN(tDecodeTSma(pCoder, pME->smaEntry.tsma, true));
1!
178
    } else {
179
      metaError("meta/entry: invalide table type: %" PRId8 " decode failed.", pME->type);
×
180
      return TSDB_CODE_INVALID_PARA;
×
181
    }
182
    if (pME->type == TSDB_SUPER_TABLE) {
29,912,324✔
183
      if (TABLE_IS_COL_COMPRESSED(pME->flags)) {
18,236,409!
184
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
18,252,320!
185

186
        if (pME->colCmpr.nCols == 0) {
18,101,306!
187
          TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->stbEntry.schemaRow));
×
188
        }
189
      } else {
190
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->stbEntry.schemaRow));
×
191
        TABLE_SET_COL_COMPRESSED(pME->flags);
24✔
192
      }
193
    } else if (pME->type == TSDB_NORMAL_TABLE) {
11,675,915✔
194
      if (!tDecodeIsEnd(pCoder)) {
2,222,087!
195
        uDebug("set type: %d, tableName:%s", pME->type, pME->name);
2,222,244✔
196
        TAOS_CHECK_RETURN(meteDecodeColCmprEntry(pCoder, pME));
2,222,244✔
197
        if (pME->colCmpr.nCols == 0) {
2,218,525!
198
          TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
199
        }
200
      } else {
201
        uDebug("set default type: %d, tableName:%s", pME->type, pME->name);
×
202
        TAOS_CHECK_RETURN(metatInitDefaultSColCmprWrapper(pCoder, &pME->colCmpr, &pME->ntbEntry.schemaRow));
×
203
      }
204
      TABLE_SET_COL_COMPRESSED(pME->flags);
2,218,525✔
205
    }
206
  }
207

208
  tEndDecode(pCoder);
29,657,864✔
209
  return 0;
29,614,550✔
210
}
211

212
static int32_t metaCloneSchema(const SSchemaWrapper *pSrc, SSchemaWrapper *pDst) {
428,446✔
213
  if (pSrc == NULL || pDst == NULL) {
428,446!
214
    return TSDB_CODE_INVALID_PARA;
×
215
  }
216

217
  pDst->nCols = pSrc->nCols;
428,485✔
218
  pDst->version = pSrc->version;
428,485✔
219
  pDst->pSchema = (SSchema *)taosMemoryMalloc(pSrc->nCols * sizeof(SSchema));
428,485!
220
  if (pDst->pSchema == NULL) {
428,501!
221
    return terrno;
×
222
  }
223
  memcpy(pDst->pSchema, pSrc->pSchema, pSrc->nCols * sizeof(SSchema));
428,501✔
224
  return TSDB_CODE_SUCCESS;
428,501✔
225
}
226

227
static void metaCloneSchemaFree(SSchemaWrapper *pSchema) {
428,472✔
228
  if (pSchema) {
428,472!
229
    taosMemoryFreeClear(pSchema->pSchema);
428,494!
230
  }
231
}
428,549✔
232

233
void metaCloneEntryFree(SMetaEntry **ppEntry) {
225,902✔
234
  if (ppEntry == NULL || *ppEntry == NULL) {
225,902!
235
    return;
×
236
  }
237

238
  taosMemoryFreeClear((*ppEntry)->name);
225,938!
239

240
  if ((*ppEntry)->type < 0) {
225,953!
241
    taosMemoryFreeClear(*ppEntry);
×
242
    return;
×
243
  }
244

245
  if (TSDB_SUPER_TABLE == (*ppEntry)->type) {
225,953✔
246
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaRow);
213,165✔
247
    metaCloneSchemaFree(&(*ppEntry)->stbEntry.schemaTag);
213,181✔
248
  } else if (TSDB_CHILD_TABLE == (*ppEntry)->type) {
12,788✔
249
    taosMemoryFreeClear((*ppEntry)->ctbEntry.comment);
10,526!
250
    taosMemoryFreeClear((*ppEntry)->ctbEntry.pTags);
10,526!
251
  } else if (TSDB_NORMAL_TABLE == (*ppEntry)->type) {
2,262!
252
    metaCloneSchemaFree(&(*ppEntry)->ntbEntry.schemaRow);
2,262✔
253
    taosMemoryFreeClear((*ppEntry)->ntbEntry.comment);
2,262!
254
  } else {
UNCOV
255
    return;
×
256
  }
257
  metaCloneColCmprFree(&(*ppEntry)->colCmpr);
225,960✔
258

259
  taosMemoryFreeClear(*ppEntry);
225,959!
260
  return;
225,978✔
261
}
262

263
int32_t metaCloneEntry(const SMetaEntry *pEntry, SMetaEntry **ppEntry) {
225,852✔
264
  int32_t code = TSDB_CODE_SUCCESS;
225,852✔
265

266
  if (NULL == pEntry || NULL == ppEntry) {
225,852!
267
    return TSDB_CODE_INVALID_PARA;
×
268
  }
269

270
  *ppEntry = (SMetaEntry *)taosMemoryCalloc(1, sizeof(SMetaEntry));
225,889!
271
  if (NULL == *ppEntry) {
225,968!
272
    return terrno;
×
273
  }
274

275
  (*ppEntry)->version = pEntry->version;
225,968✔
276
  (*ppEntry)->type = pEntry->type;
225,968✔
277
  (*ppEntry)->uid = pEntry->uid;
225,968✔
278

279
  if (pEntry->type < 0) {
225,968!
280
    return TSDB_CODE_SUCCESS;
×
281
  }
282

283
  if (pEntry->name) {
225,968✔
284
    (*ppEntry)->name = tstrdup(pEntry->name);
225,963✔
285
    if (NULL == (*ppEntry)->name) {
225,904!
286
      code = terrno;
×
287
      metaCloneEntryFree(ppEntry);
×
288
      return code;
×
289
    }
290
  }
291

292
  if (pEntry->type == TSDB_SUPER_TABLE) {
225,956✔
293
    (*ppEntry)->flags = pEntry->flags;
213,168✔
294

295
    code = metaCloneSchema(&pEntry->stbEntry.schemaRow, &(*ppEntry)->stbEntry.schemaRow);
213,168✔
296
    if (code) {
213,102!
297
      metaCloneEntryFree(ppEntry);
×
298
      return code;
×
299
    }
300

301
    code = metaCloneSchema(&pEntry->stbEntry.schemaTag, &(*ppEntry)->stbEntry.schemaTag);
213,102✔
302
    if (code) {
213,108!
303
      metaCloneEntryFree(ppEntry);
×
304
      return code;
×
305
    }
306
  } else if (pEntry->type == TSDB_CHILD_TABLE) {
12,788✔
307
    (*ppEntry)->ctbEntry.btime = pEntry->ctbEntry.btime;
10,526✔
308
    (*ppEntry)->ctbEntry.ttlDays = pEntry->ctbEntry.ttlDays;
10,526✔
309
    (*ppEntry)->ctbEntry.suid = pEntry->ctbEntry.suid;
10,526✔
310

311
    // comment
312
    (*ppEntry)->ctbEntry.commentLen = pEntry->ctbEntry.commentLen;
10,526✔
313
    if (pEntry->ctbEntry.commentLen > 0) {
10,526✔
314
      (*ppEntry)->ctbEntry.comment = taosMemoryMalloc(pEntry->ctbEntry.commentLen + 1);
40!
315
      if (NULL == (*ppEntry)->ctbEntry.comment) {
40!
316
        code = terrno;
×
317
        metaCloneEntryFree(ppEntry);
×
318
        return code;
×
319
      }
320
      memcpy((*ppEntry)->ctbEntry.comment, pEntry->ctbEntry.comment, pEntry->ctbEntry.commentLen + 1);
40✔
321
    }
322

323
    // tags
324
    STag *pTags = (STag *)pEntry->ctbEntry.pTags;
10,526✔
325
    (*ppEntry)->ctbEntry.pTags = taosMemoryCalloc(1, pTags->len);
10,526!
326
    if (NULL == (*ppEntry)->ctbEntry.pTags) {
10,525!
327
      code = terrno;
×
328
      metaCloneEntryFree(ppEntry);
×
329
      return code;
×
330
    }
331
    memcpy((*ppEntry)->ctbEntry.pTags, pEntry->ctbEntry.pTags, pTags->len);
10,525✔
332
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
2,262!
333
    (*ppEntry)->ntbEntry.btime = pEntry->ntbEntry.btime;
2,262✔
334
    (*ppEntry)->ntbEntry.ttlDays = pEntry->ntbEntry.ttlDays;
2,262✔
335
    (*ppEntry)->ntbEntry.ncid = pEntry->ntbEntry.ncid;
2,262✔
336

337
    // schema
338
    code = metaCloneSchema(&pEntry->ntbEntry.schemaRow, &(*ppEntry)->ntbEntry.schemaRow);
2,262✔
339
    if (code) {
2,262!
340
      metaCloneEntryFree(ppEntry);
×
341
      return code;
×
342
    }
343

344
    // comment
345
    (*ppEntry)->ntbEntry.commentLen = pEntry->ntbEntry.commentLen;
2,262✔
346
    if (pEntry->ntbEntry.commentLen > 0) {
2,262✔
347
      (*ppEntry)->ntbEntry.comment = taosMemoryMalloc(pEntry->ntbEntry.commentLen + 1);
44!
348
      if (NULL == (*ppEntry)->ntbEntry.comment) {
44!
349
        code = terrno;
×
350
        metaCloneEntryFree(ppEntry);
×
351
        return code;
×
352
      }
353
      memcpy((*ppEntry)->ntbEntry.comment, pEntry->ntbEntry.comment, pEntry->ntbEntry.commentLen + 1);
44✔
354
    }
355
  } else {
356
    return TSDB_CODE_INVALID_PARA;
×
357
  }
358

359
  code = metaCloneColCmpr(&pEntry->colCmpr, &(*ppEntry)->colCmpr);
225,895✔
360
  if (code) {
225,929!
361
    metaCloneEntryFree(ppEntry);
×
362
    return code;
×
363
  }
364

365
  return code;
225,955✔
366
}
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