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

taosdata / TDengine / #5006

29 Mar 2026 04:32AM UTC coverage: 72.274% (+0.1%) from 72.152%
#5006

push

travis-ci

web-flow
refactor: do some internal refactor for TDgpt. (#34955)

253711 of 351039 relevant lines covered (72.27%)

131490495.89 hits per line

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

91.33
/source/dnode/vnode/src/tsdb/tsdbDataFileRW.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 "tsdbDataFileRW.h"
17
#include "meta.h"
18

19
// SDataFileReader =============================================
20
struct SDataFileReader {
21
  SDataFileReaderConfig config[1];
22

23
  SBuffer  local[10];
24
  SBuffer *buffers;
25

26
  struct {
27
    bool headFooterLoaded;
28
    bool tombFooterLoaded;
29
    bool brinBlkLoaded;
30
    bool tombBlkLoaded;
31
  } ctx[1];
32

33
  STsdbFD *fd[TSDB_FTYPE_MAX];
34

35
  SHeadFooter   headFooter[1];
36
  STombFooter   tombFooter[1];
37
  TBrinBlkArray brinBlkArray[1];
38
  TTombBlkArray tombBlkArray[1];
39
};
40

41
static int32_t tsdbDataFileReadHeadFooter(SDataFileReader *reader) {
27,024,384✔
42
  if (reader->ctx->headFooterLoaded) {
27,024,384✔
43
    return 0;
×
44
  }
45

46
  int32_t code = 0;
27,024,104✔
47
  int32_t lino = 0;
27,024,104✔
48

49
  int32_t ftype = TSDB_FTYPE_HEAD;
27,026,424✔
50
  if (reader->fd[ftype]) {
27,026,424✔
51
    SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
24,160,498✔
52

53
#if 1
54
    TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[ftype], reader->config->files[ftype].file.size - sizeof(SHeadFooter),
24,162,818✔
55
                                 (uint8_t *)reader->headFooter, sizeof(SHeadFooter), 0, pEncryptData),
56
                    &lino, _exit);
57
#else
58
    int64_t size = reader->config->files[ftype].file.size;
59
    for (; size > TSDB_FHDR_SIZE; size--) {
60
      code = tsdbReadFile(reader->fd[ftype], size - sizeof(SHeadFooter), (uint8_t *)reader->headFooter,
61
                          sizeof(SHeadFooter), 0, encryptAlgorithm, encryptKey);
62
      if (code) continue;
63
      if (reader->headFooter->brinBlkPtr->offset + reader->headFooter->brinBlkPtr->size + sizeof(SHeadFooter) == size) {
64
        break;
65
      }
66
    }
67
    if (size <= TSDB_FHDR_SIZE) {
68
      TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
69
    }
70
#endif
71
  }
72

73
  reader->ctx->headFooterLoaded = true;
27,025,046✔
74

75
_exit:
27,030,146✔
76
  if (code) {
27,031,370✔
77
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
78
              tstrerror(code));
79
  }
80
  return code;
27,024,710✔
81
}
82

83
static int32_t tsdbDataFileReadTombFooter(SDataFileReader *reader) {
3,121,804✔
84
  if (reader->ctx->tombFooterLoaded) {
3,121,804✔
85
    return 0;
×
86
  }
87

88
  int32_t code = 0;
3,121,804✔
89
  int32_t lino = 0;
3,121,804✔
90

91
  int32_t ftype = TSDB_FTYPE_TOMB;
3,121,804✔
92
  if (reader->fd[ftype]) {
3,121,804✔
93
    SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2,969,039✔
94

95
    TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[ftype], reader->config->files[ftype].file.size - sizeof(STombFooter),
2,969,039✔
96
                                 (uint8_t *)reader->tombFooter, sizeof(STombFooter), 0, pEncryptData),
97
                    &lino, _exit);
98
  }
99
  reader->ctx->tombFooterLoaded = true;
3,121,804✔
100

101
_exit:
3,121,804✔
102
  if (code) {
3,121,804✔
103
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
104
              tstrerror(code));
105
  }
106
  return code;
3,121,804✔
107
}
108

109
int32_t tsdbDataFileReaderOpen(const char *fname[], const SDataFileReaderConfig *config, SDataFileReader **reader) {
27,272,146✔
110
  int32_t code = 0;
27,272,146✔
111
  int32_t lino = 0;
27,272,146✔
112

113
  if ((*reader = taosMemoryCalloc(1, sizeof(**reader))) == NULL) {
27,277,738✔
114
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
115
  }
116

117
  for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->local); i++) {
299,939,574✔
118
    tBufferInit(reader[0]->local + i);
272,648,412✔
119
  }
120

121
  reader[0]->config[0] = config[0];
27,291,162✔
122
  reader[0]->buffers = config->buffers;
27,280,522✔
123
  if (reader[0]->buffers == NULL) {
27,271,606✔
124
    reader[0]->buffers = reader[0]->local;
26,885,357✔
125
  }
126

127
  if (fname) {
27,276,514✔
128
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
133,846,822✔
129
      if (fname[i]) {
107,072,860✔
130
        int32_t lcn = config->files[i].file.lcn;
74,786,028✔
131
        TAOS_CHECK_GOTO(tsdbOpenFile(fname[i], config->tsdb, TD_FILE_READ, &reader[0]->fd[i], lcn), &lino, _exit);
74,793,592✔
132
      }
133
    }
134
  } else {
135
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
2,552,200✔
136
      if (config->files[i].exist) {
2,041,760✔
137
        char fname1[TSDB_FILENAME_LEN];
1,044,042✔
138
        tsdbTFileName(config->tsdb, &config->files[i].file, fname1);
1,044,042✔
139
        int32_t lcn = config->files[i].file.lcn;
1,044,042✔
140
        TAOS_CHECK_GOTO(tsdbOpenFile(fname1, config->tsdb, TD_FILE_READ, &reader[0]->fd[i], lcn), &lino, _exit);
1,044,042✔
141
      }
142
    }
143
  }
144

145
_exit:
27,284,402✔
146
  if (code) {
27,280,662✔
147
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(config->tsdb->pVnode), __func__, __FILE__, lino,
×
148
              tstrerror(code));
149
  }
150
  return code;
27,280,662✔
151
}
152

153
void tsdbDataFileReaderClose(SDataFileReader **reader) {
41,922,799✔
154
  if (reader[0] == NULL) {
41,922,799✔
155
    return;
14,648,113✔
156
  }
157

158
  TARRAY2_DESTROY(reader[0]->tombBlkArray, NULL);
27,277,949✔
159
  TARRAY2_DESTROY(reader[0]->brinBlkArray, NULL);
27,281,610✔
160

161
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
136,400,914✔
162
    if (reader[0]->fd[i]) {
109,116,852✔
163
      tsdbCloseFile(&reader[0]->fd[i]);
75,838,936✔
164
    }
165
  }
166

167
  for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->local); ++i) {
300,026,830✔
168
    tBufferDestroy(reader[0]->local + i);
272,750,260✔
169
  }
170

171
  taosMemoryFree(reader[0]);
27,276,570✔
172
  reader[0] = NULL;
27,279,434✔
173
}
174

175
int32_t tsdbDataFileReadBrinBlk(SDataFileReader *reader, const TBrinBlkArray **brinBlkArray) {
27,027,036✔
176
  int32_t code = 0;
27,027,036✔
177
  int32_t lino = 0;
27,027,036✔
178
  void   *data = NULL;
27,028,872✔
179

180
  if (!reader->ctx->brinBlkLoaded) {
27,028,872✔
181
    TAOS_CHECK_GOTO(tsdbDataFileReadHeadFooter(reader), &lino, _exit);
27,026,900✔
182

183
    if (reader->headFooter->brinBlkPtr->size > 0) {
27,026,037✔
184
      data = taosMemoryMalloc(reader->headFooter->brinBlkPtr->size);
24,159,272✔
185
      if (data == NULL) {
24,153,128✔
186
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
187
      }
188

189
      SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
24,153,128✔
190

191
      TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[TSDB_FTYPE_HEAD], reader->headFooter->brinBlkPtr->offset, data,
24,159,266✔
192
                                   reader->headFooter->brinBlkPtr->size, 0, pEncryptData),
193
                      &lino, _exit);
194

195
      int32_t size = reader->headFooter->brinBlkPtr->size / sizeof(SBrinBlk);
24,167,020✔
196
      TARRAY2_INIT_EX(reader->brinBlkArray, size, size, data);
24,164,170✔
197
    } else {
198
      TARRAY2_INIT(reader->brinBlkArray);
2,866,827✔
199
    }
200

201
    reader->ctx->brinBlkLoaded = true;
27,025,675✔
202
  }
203
  brinBlkArray[0] = reader->brinBlkArray;
27,030,783✔
204

205
_exit:
27,027,028✔
206
  if (code) {
27,025,520✔
207
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
208
              tstrerror(code));
209
    taosMemoryFree(data);
×
210
  }
211
  return code;
27,025,520✔
212
}
213

214
int32_t tsdbDataFileReadBrinBlock(SDataFileReader *reader, const SBrinBlk *brinBlk, SBrinBlock *brinBlock) {
22,537,027✔
215
  int32_t code = 0;
22,537,027✔
216
  int32_t lino = 0;
22,537,027✔
217

218
  SBuffer *buffer = reader->buffers + 0;
22,524,077✔
219
  SBuffer *assist = reader->buffers + 1;
22,533,553✔
220

221
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
22,540,493✔
222

223
  // load data
224
  tBufferClear(buffer);
225
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_HEAD], brinBlk->dp->offset, brinBlk->dp->size, buffer, 0,
22,535,463✔
226
                                       pEncryptData),
227
                  &lino, _exit);
228

229
  // decode brin block
230
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer);
22,546,493✔
231
  tBrinBlockClear(brinBlock);
22,542,613✔
232
  brinBlock->numOfPKs = brinBlk->numOfPKs;
22,551,729✔
233
  brinBlock->numOfRecords = brinBlk->numRec;
22,547,581✔
234
  for (int32_t i = 0; i < 10; i++) {  // int64_t
248,030,663✔
235

236
    SCompressInfo cinfo = {
225,478,322✔
237
        .cmprAlg = brinBlk->cmprAlg,
225,474,174✔
238
        .dataType = TSDB_DATA_TYPE_BIGINT,
239
        .compressedSize = brinBlk->size[i],
225,474,452✔
240
        .originalSize = brinBlk->numRec * sizeof(int64_t),
225,481,176✔
241
    };
242
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, brinBlock->buffers + i, assist), &lino, _exit);
225,491,876✔
243
    br.offset += brinBlk->size[i];
225,480,926✔
244
  }
245

246
  for (int32_t i = 10; i < 15; i++) {  // int32_t
135,304,994✔
247
    SCompressInfo cinfo = {
112,749,725✔
248
        .cmprAlg = brinBlk->cmprAlg,
112,752,017✔
249
        .dataType = TSDB_DATA_TYPE_INT,
250
        .compressedSize = brinBlk->size[i],
112,753,401✔
251
        .originalSize = brinBlk->numRec * sizeof(int32_t),
112,751,085✔
252
    };
253
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, brinBlock->buffers + i, assist), &lino, _exit);
112,752,649✔
254
    br.offset += brinBlk->size[i];
112,756,193✔
255
  }
256

257
  // primary keys
258
  if (brinBlk->numOfPKs > 0) {  // decode the primary keys
22,555,269✔
259
    SValueColumnCompressInfo firstInfos[TD_MAX_PK_COLS];
4,087✔
260
    SValueColumnCompressInfo lastInfos[TD_MAX_PK_COLS];
4,087✔
261

262
    for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
8,174✔
263
      TAOS_CHECK_GOTO(tValueColumnCompressInfoDecode(&br, firstInfos + i), &lino, _exit);
4,087✔
264
    }
265
    for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
8,174✔
266
      TAOS_CHECK_GOTO(tValueColumnCompressInfoDecode(&br, lastInfos + i), &lino, _exit);
4,087✔
267
    }
268

269
    for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
8,174✔
270
      SValueColumnCompressInfo *info = firstInfos + i;
4,087✔
271

272
      TAOS_CHECK_GOTO(tValueColumnDecompress(BR_PTR(&br), info, brinBlock->firstKeyPKs + i, assist), &lino, _exit);
4,087✔
273
      br.offset += (info->offsetCompressedSize + info->dataCompressedSize);
4,087✔
274
    }
275

276
    for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
8,174✔
277
      SValueColumnCompressInfo *info = lastInfos + i;
4,087✔
278

279
      TAOS_CHECK_GOTO(tValueColumnDecompress(BR_PTR(&br), info, brinBlock->lastKeyPKs + i, assist), &lino, _exit);
4,087✔
280
      br.offset += (info->offsetCompressedSize + info->dataCompressedSize);
4,087✔
281
    }
282
  }
283

284
  if (br.offset != br.buffer->size) {
22,552,817✔
285
    tsdbError("vgId:%d %s failed at %s:%d since brin block size mismatch, expected: %u, actual: %u, fname:%s",
×
286
              TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino, br.buffer->size, br.offset,
287
              reader->fd[TSDB_FTYPE_HEAD]->path);
288
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
289
  }
290

291
_exit:
22,545,873✔
292
  if (code) {
22,550,369✔
293
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
294
              tstrerror(code));
295
  }
296
  return code;
22,550,369✔
297
}
298

299
extern int32_t tBlockDataDecompress(SBufferReader *br, SBlockData *blockData, SBuffer *assist);
300

301
int32_t tsdbDataFileReadBlockData(SDataFileReader *reader, const SBrinRecord *record, SBlockData *bData) {
2,612,488✔
302
  int32_t code = 0;
2,612,488✔
303
  int32_t lino = 0;
2,612,488✔
304
  int32_t fid = reader->config->files[TSDB_FTYPE_DATA].file.fid;
2,612,488✔
305

306
  SBuffer *buffer = reader->buffers + 0;
2,612,488✔
307
  SBuffer *assist = reader->buffers + 1;
2,612,488✔
308

309
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2,612,488✔
310

311
  // load data
312
  tBufferClear(buffer);
313
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA], record->blockOffset, record->blockSize, buffer, 0,
2,612,488✔
314
                                       pEncryptData),
315
                  &lino, _exit);
316

317
  // decompress
318
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer);
2,612,488✔
319
  TAOS_CHECK_GOTO(tBlockDataDecompress(&br, bData, assist), &lino, _exit);
2,612,488✔
320

321
  if (br.offset != buffer->size) {
2,612,488✔
322
    tsdbError("vgId:%d %s failed at %s:%d since block data size mismatch, expected: %u, actual: %u, fname:%s",
×
323
              TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, buffer->size, br.offset,
324
              reader->fd[TSDB_FTYPE_DATA]->path);
325
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
326
  }
327

328
_exit:
2,612,488✔
329
  if (code) {
2,612,488✔
330
    tsdbError("vgId:%d %s fid %d failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, fid,
×
331
              __FILE__, lino, tstrerror(code));
332
  }
333
  return code;
2,612,488✔
334
}
335

336
int32_t tsdbDataFileReadBlockDataByColumn(SDataFileReader *reader, const SBrinRecord *record, SBlockData *bData,
86,183,971✔
337
                                          STSchema *pTSchema, int16_t cids[], int32_t ncid) {
338
  int32_t code = 0;
86,183,971✔
339
  int32_t lino = 0;
86,183,971✔
340
  int32_t fid = reader->config->files[TSDB_FTYPE_DATA].file.fid;
86,189,589✔
341

342
  SDiskDataHdr hdr;
86,196,592✔
343
  SBuffer     *buffer0 = reader->buffers + 0;
86,197,427✔
344
  SBuffer     *buffer1 = reader->buffers + 1;
86,197,505✔
345
  SBuffer     *assist = reader->buffers + 2;
86,197,277✔
346

347
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
86,196,301✔
348

349
  // load key part
350
  tBufferClear(buffer0);
351
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA], record->blockOffset, record->blockKeySize, buffer0,
86,192,141✔
352
                                       0, pEncryptData),
353
                  &lino, _exit);
354

355
  // SDiskDataHdr
356
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
86,164,719✔
357
  TAOS_CHECK_GOTO(tGetDiskDataHdr(&br, &hdr), &lino, _exit);
86,191,657✔
358

359
  if (hdr.delimiter != TSDB_FILE_DLMT) {
86,165,634✔
360
    tsdbError("vgId:%d %s failed at %s:%d since disk data header delimiter is invalid, fname:%s",
×
361
              TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, reader->fd[TSDB_FTYPE_DATA]->path);
362
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
363
  }
364

365
  tBlockDataReset(bData);
86,165,634✔
366
  bData->suid = hdr.suid;
86,187,994✔
367
  bData->uid = hdr.uid;
86,196,842✔
368
  bData->nRow = hdr.nRow;
86,184,600✔
369

370
  // Key part
371
  TAOS_CHECK_GOTO(tBlockDataDecompressKeyPart(&hdr, &br, bData, assist), &lino, _exit);
86,195,083✔
372
  if (br.offset != buffer0->size) {
86,196,798✔
373
    tsdbError("vgId:%d %s failed at %s:%d since key part size mismatch, expected: %u, actual: %u, fname:%s",
16,548✔
374
              TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, buffer0->size, br.offset,
375
              reader->fd[TSDB_FTYPE_DATA]->path);
376
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
377
  }
378

379
  int extraColIdx = -1;
86,181,474✔
380
  for (int i = 0; i < ncid; i++) {
86,194,150✔
381
    if (tBlockDataGetColData(bData, cids[i]) == NULL) {
84,083,774✔
382
      extraColIdx = i;
84,073,225✔
383
      break;
84,073,225✔
384
    }
385
  }
386

387
  if (extraColIdx < 0) {
86,183,601✔
388
    goto _exit;
2,111,653✔
389
  }
390

391
  // load SBlockCol part
392
  tBufferClear(buffer0);
393
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA], record->blockOffset + record->blockKeySize,
84,066,617✔
394
                                       hdr.szBlkCol, buffer0, 0, pEncryptData),
395
                  &lino, _exit);
396

397
  // calc szHint
398
  int64_t szHint = 0;
84,079,229✔
399
  int     extraCols = 1;
84,079,229✔
400
  for (int i = extraColIdx + 1; i < ncid; ++i) {
84,079,229✔
401
    if (tBlockDataGetColData(bData, cids[i]) == NULL) {
45,343,298✔
402
      ++extraCols;
45,345,882✔
403
      break;
45,345,882✔
404
    }
405
  }
406

407
  if (extraCols >= 2) {
84,081,813✔
408
    br = BUFFER_READER_INITIALIZER(0, buffer0);
45,345,274✔
409

410
    SBlockCol blockCol = {.cid = 0};
45,345,274✔
411
    for (int32_t i = extraColIdx; i < ncid; ++i) {
45,344,310✔
412
      int16_t extraColCid = cids[i];
45,336,478✔
413

414
      while (extraColCid > blockCol.cid) {
151,187,456✔
415
        if (br.offset >= buffer0->size) {
105,837,274✔
416
          blockCol.cid = INT16_MAX;
×
417
          break;
×
418
        }
419

420
        TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
105,846,954✔
421
      }
422

423
      if (extraColCid == blockCol.cid || blockCol.cid == INT16_MAX) {
45,350,182✔
424
        extraColIdx = i;
45,350,182✔
425
        break;
45,350,182✔
426
      }
427
    }
428

429
    if (blockCol.cid > 0 && blockCol.cid < INT16_MAX /*&& blockCol->flag == HAS_VALUE*/) {
45,358,014✔
430
      int64_t   offset = blockCol.offset;
45,350,658✔
431
      SBlockCol lastNonNoneBlockCol = {.cid = 0};
45,350,658✔
432

433
      for (int32_t i = extraColIdx; i < ncid; ++i) {
245,852,798✔
434
        int16_t extraColCid = cids[i];
200,501,320✔
435

436
        while (extraColCid > blockCol.cid) {
551,283,968✔
437
          if (br.offset >= buffer0->size) {
350,781,828✔
438
            blockCol.cid = INT16_MAX;
×
439
            break;
×
440
          }
441

442
          TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
350,781,680✔
443
        }
444

445
        if (extraColCid == blockCol.cid) {
200,502,140✔
446
          lastNonNoneBlockCol = blockCol;
200,502,140✔
447
          continue;
200,502,140✔
448
        }
449

450
        if (blockCol.cid == INT16_MAX) {
×
451
          break;
×
452
        }
453
      }
454

455
      if (lastNonNoneBlockCol.cid > 0) {
45,351,478✔
456
        szHint = lastNonNoneBlockCol.offset + lastNonNoneBlockCol.szBitmap + lastNonNoneBlockCol.szOffset +
45,350,658✔
457
                 lastNonNoneBlockCol.szValue - offset;
45,350,658✔
458
      }
459
    }
460
  }
461

462
  // load each column
463
  SBlockCol blockCol = {
84,087,197✔
464
      .cid = 0,
465
  };
466
  bool firstRead = true;
84,082,229✔
467
  br = BUFFER_READER_INITIALIZER(0, buffer0);
84,082,229✔
468
  for (int32_t i = 0; i < ncid; i++) {
323,324,324✔
469
    int16_t cid = cids[i];
239,230,195✔
470

471
    if (tBlockDataGetColData(bData, cid)) {  // already loaded
239,231,804✔
472
      continue;
1,444✔
473
    }
474

475
    while (cid > blockCol.cid) {
848,564,628✔
476
      if (br.offset >= buffer0->size) {
609,302,852✔
477
        blockCol.cid = INT16_MAX;
×
478
        break;
×
479
      }
480

481
      TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
609,290,025✔
482
    }
483

484
    if (cid < blockCol.cid) {
239,261,776✔
485
      const STColumn *tcol = tTSchemaSearchColumn(pTSchema, cid);
×
486
      TSDB_CHECK_NULL(tcol, code, lino, _exit, TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER);
×
487
      SBlockCol none = {
×
488
          .cid = cid,
489
          .type = tcol->type,
×
490
          .cflag = tcol->flags,
×
491
          .flag = HAS_NONE,
492
          .szOrigin = 0,
493
          .szBitmap = 0,
494
          .szOffset = 0,
495
          .szValue = 0,
496
          .offset = 0,
497
      };
498
      TAOS_CHECK_GOTO(tBlockDataDecompressColData(&hdr, &none, &br, bData, assist), &lino, _exit);
×
499
    } else if (cid == blockCol.cid) {
239,261,776✔
500
      SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
239,236,227✔
501

502
      // load from file
503
      tBufferClear(buffer1);
504
      TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA],
239,248,377✔
505
                                           record->blockOffset + record->blockKeySize + hdr.szBlkCol + blockCol.offset,
506
                                           blockCol.szBitmap + blockCol.szOffset + blockCol.szValue, buffer1,
507
                                           firstRead ? szHint : 0, pEncryptData),
508
                      &lino, _exit);
509

510
      firstRead = false;
239,239,627✔
511

512
      // decode the buffer
513
      SBufferReader br1 = BUFFER_READER_INITIALIZER(0, buffer1);
239,239,627✔
514
      TAOS_CHECK_GOTO(tBlockDataDecompressColData(&hdr, &blockCol, &br1, bData, assist), &lino, _exit);
239,239,487✔
515
    }
516
  }
517

518
_exit:
86,225,483✔
519
  if (code) {
86,195,567✔
520
    tsdbError("vgId:%d %s fid:%d failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, fid,
×
521
              __FILE__, lino, tstrerror(code));
522
  }
523
  return code;
86,195,567✔
524
}
525

526
int32_t tsdbDataFileReadBlockSma(SDataFileReader *reader, const SBrinRecord *record,
33,027,533✔
527
                                 TColumnDataAggArray *columnDataAggArray) {
528
  int32_t  code = 0;
33,027,533✔
529
  int32_t  lino = 0;
33,027,533✔
530
  SBuffer *buffer = reader->buffers + 0;
33,052,641✔
531

532
  TARRAY2_CLEAR(columnDataAggArray, NULL);
33,081,250✔
533
  if (record->smaSize > 0) {
33,081,862✔
534
    tBufferClear(buffer);
535
    SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
33,081,862✔
536

537
    TAOS_CHECK_GOTO(
33,081,592✔
538
        tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_SMA], record->smaOffset, record->smaSize, buffer, 0, pEncryptData),
539
        &lino, _exit);
540

541
    // decode sma data
542
    SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer);
33,054,978✔
543
    while (br.offset < record->smaSize) {
420,697,633✔
544
      SColumnDataAgg sma[1];
387,612,432✔
545

546
      TAOS_CHECK_GOTO(tGetColumnDataAgg(&br, sma), &lino, _exit);
387,591,771✔
547
      TAOS_CHECK_GOTO(TARRAY2_APPEND_PTR(columnDataAggArray, sma), &lino, _exit);
774,992,436✔
548
    }
549
    if (br.offset != record->smaSize) {
33,081,592✔
550
      tsdbError("vgId:%d %s failed at %s:%d since sma data size mismatch, expected: %u, actual: %u, fname:%s",
×
551
                TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, record->smaSize, br.offset,
552
                reader->fd[TSDB_FTYPE_SMA]->path);
553
      TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
554
    }
555
  }
556

557
_exit:
33,076,541✔
558
  if (code) {
33,080,658✔
559
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
560
              tstrerror(code));
561
  }
562
  return code;
33,080,658✔
563
}
564

565
int32_t tsdbDataFileReadTombBlk(SDataFileReader *reader, const TTombBlkArray **tombBlkArray) {
5,061,235✔
566
  int32_t code = 0;
5,061,235✔
567
  int32_t lino = 0;
5,061,235✔
568
  void   *data = NULL;
5,061,890✔
569

570
  if (!reader->ctx->tombBlkLoaded) {
5,061,890✔
571
    TAOS_CHECK_GOTO(tsdbDataFileReadTombFooter(reader), &lino, _exit);
3,121,804✔
572

573
    if (reader->tombFooter->tombBlkPtr->size > 0) {
3,121,804✔
574
      if ((data = taosMemoryMalloc(reader->tombFooter->tombBlkPtr->size)) == NULL) {
2,969,039✔
575
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
576
      }
577

578
      SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2,969,039✔
579

580
      TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[TSDB_FTYPE_TOMB], reader->tombFooter->tombBlkPtr->offset, data,
2,969,039✔
581
                                   reader->tombFooter->tombBlkPtr->size, 0, pEncryptData),
582
                      &lino, _exit);
583

584
      int32_t size = reader->tombFooter->tombBlkPtr->size / sizeof(STombBlk);
2,969,039✔
585
      TARRAY2_INIT_EX(reader->tombBlkArray, size, size, data);
2,969,039✔
586
    } else {
587
      TARRAY2_INIT(reader->tombBlkArray);
152,765✔
588
    }
589

590
    reader->ctx->tombBlkLoaded = true;
3,121,804✔
591
  }
592
  tombBlkArray[0] = reader->tombBlkArray;
5,061,235✔
593

594
_exit:
5,061,235✔
595
  if (code) {
5,061,235✔
596
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
597
              tstrerror(code));
598
    taosMemoryFree(data);
×
599
  }
600
  return code;
5,061,235✔
601
}
602

603
int32_t tsdbDataFileReadTombBlock(SDataFileReader *reader, const STombBlk *tombBlk, STombBlock *tData) {
1,835,005✔
604
  int32_t code = 0;
1,835,005✔
605
  int32_t lino = 0;
1,835,005✔
606

607
  SBuffer *buffer0 = reader->buffers + 0;
1,835,005✔
608
  SBuffer *assist = reader->buffers + 1;
1,835,005✔
609

610
  tBufferClear(buffer0);
611
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
1,835,005✔
612

613
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_TOMB], tombBlk->dp->offset, tombBlk->dp->size, buffer0, 0,
1,835,005✔
614
                                       pEncryptData),
615
                  &lino, _exit);
616

617
  int32_t       size = 0;
1,835,005✔
618
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
1,835,005✔
619
  tTombBlockClear(tData);
1,835,005✔
620
  tData->numOfRecords = tombBlk->numRec;
1,835,005✔
621
  for (int32_t i = 0; i < ARRAY_SIZE(tData->buffers); ++i) {
11,010,030✔
622
    SCompressInfo cinfo = {
9,175,025✔
623
        .cmprAlg = tombBlk->cmprAlg,
9,175,025✔
624
        .dataType = TSDB_DATA_TYPE_BIGINT,
625
        .originalSize = tombBlk->numRec * sizeof(int64_t),
9,175,025✔
626
        .compressedSize = tombBlk->size[i],
9,175,025✔
627
    };
628
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, tData->buffers + i, assist), &lino, _exit);
9,175,025✔
629
    br.offset += tombBlk->size[i];
9,175,025✔
630
  }
631

632
_exit:
1,835,005✔
633
  if (code) {
1,835,005✔
634
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
635
              tstrerror(code));
636
  }
637
  return code;
1,835,005✔
638
}
639

640
// SDataFileWriter =============================================
641
struct SDataFileWriter {
642
  SDataFileWriterConfig config[1];
643

644
  SSkmInfo skmTb[1];
645
  SSkmInfo skmRow[1];
646
  SBuffer  local[10];
647
  SBuffer *buffers;
648

649
  struct {
650
    bool             opened;
651
    SDataFileReader *reader;
652

653
    // for ts data
654
    TABLEID tbid[1];
655
    bool    tbHasOldData;
656

657
    const TBrinBlkArray *brinBlkArray;
658
    int32_t              brinBlkArrayIdx;
659
    SBrinBlock           brinBlock[1];
660
    int32_t              brinBlockIdx;
661
    SBlockData           blockData[1];
662
    int32_t              blockDataIdx;
663
    // for tomb data
664
    bool                 hasOldTomb;
665
    const TTombBlkArray *tombBlkArray;
666
    int32_t              tombBlkArrayIdx;
667
    STombBlock           tombBlock[1];
668
    int32_t              tombBlockIdx;
669
    // range
670
    SVersionRange range;
671
    SVersionRange tombRange;
672
  } ctx[1];
673

674
  STFile   files[TSDB_FTYPE_MAX];
675
  STsdbFD *fd[TSDB_FTYPE_MAX];
676

677
  SHeadFooter headFooter[1];
678
  STombFooter tombFooter[1];
679

680
  TBrinBlkArray brinBlkArray[1];
681
  SBrinBlock    brinBlock[1];
682
  SBlockData    blockData[1];
683

684
  TTombBlkArray tombBlkArray[1];
685
  STombBlock    tombBlock[1];
686
};
687

688
static int32_t tsdbDataFileWriterCloseAbort(SDataFileWriter *writer) {
×
689
  tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, __LINE__,
×
690
            "not implemented");
691
  return 0;
×
692
}
693

694
static void tsdbDataFileWriterDoClose(SDataFileWriter *writer) {
798,930✔
695
  if (writer->ctx->reader) {
798,930✔
696
    tsdbDataFileReaderClose(&writer->ctx->reader);
391,028✔
697
  }
698

699
  tTombBlockDestroy(writer->tombBlock);
798,930✔
700
  TARRAY2_DESTROY(writer->tombBlkArray, NULL);
798,930✔
701
  tBlockDataDestroy(writer->blockData);
798,930✔
702
  tBrinBlockDestroy(writer->brinBlock);
798,930✔
703
  TARRAY2_DESTROY(writer->brinBlkArray, NULL);
798,930✔
704

705
  tTombBlockDestroy(writer->ctx->tombBlock);
798,366✔
706
  tBlockDataDestroy(writer->ctx->blockData);
798,930✔
707
  tBrinBlockDestroy(writer->ctx->brinBlock);
798,930✔
708

709
  for (int32_t i = 0; i < ARRAY_SIZE(writer->local); ++i) {
8,786,150✔
710
    tBufferDestroy(writer->local + i);
7,987,220✔
711
  }
712

713
  tDestroyTSchema(writer->skmRow->pTSchema);
798,930✔
714
  tDestroyTSchema(writer->skmTb->pTSchema);
798,930✔
715
}
798,842✔
716

717
static int32_t tsdbDataFileWriterDoOpenReader(SDataFileWriter *writer) {
798,930✔
718
  int32_t code = 0;
798,930✔
719
  int32_t lino = 0;
798,930✔
720

721
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
2,736,305✔
722
    if (writer->config->files[i].exist) {
2,328,879✔
723
      SDataFileReaderConfig config[1] = {{
391,028✔
724
          .tsdb = writer->config->tsdb,
391,028✔
725
          .szPage = writer->config->szPage,
391,028✔
726
          .buffers = writer->buffers,
391,028✔
727
      }};
728

729
      for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
1,955,140✔
730
        config->files[i].exist = writer->config->files[i].exist;
1,564,112✔
731
        if (config->files[i].exist) {
1,564,112✔
732
          config->files[i].file = writer->config->files[i].file;
968,922✔
733
        }
734
      }
735

736
      TAOS_CHECK_GOTO(tsdbDataFileReaderOpen(NULL, config, &writer->ctx->reader), &lino, _exit);
391,028✔
737
      break;
391,028✔
738
    }
739
  }
740

741
_exit:
798,454✔
742
  if (code) {
798,454✔
743
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
744
              tstrerror(code));
745
  }
746
  return code;
798,454✔
747
}
748

749
static int32_t tsdbDataFileWriterDoOpen(SDataFileWriter *writer) {
798,930✔
750
  int32_t code = 0;
798,930✔
751
  int32_t lino = 0;
798,930✔
752
  int32_t ftype;
753
  SDiskID diskId = {0};
798,930✔
754

755
  if (!writer->config->skmTb) writer->config->skmTb = writer->skmTb;
798,930✔
756
  if (!writer->config->skmRow) writer->config->skmRow = writer->skmRow;
798,930✔
757
  writer->buffers = writer->config->buffers;
798,930✔
758
  if (writer->buffers == NULL) {
798,930✔
759
    writer->buffers = writer->local;
×
760
  }
761

762
  // open reader
763
  TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpenReader(writer), &lino, _exit);
798,930✔
764

765
  // .head
766
  ftype = TSDB_FTYPE_HEAD;
798,454✔
767
  code = tsdbAllocateDisk(writer->config->tsdb, tsdbFTypeLabel(ftype), writer->config->expLevel, &diskId);
798,454✔
768
  TSDB_CHECK_CODE(code, lino, _exit);
798,454✔
769
  writer->files[ftype] = (STFile){
1,596,908✔
770
      .type = ftype,
771
      .did = diskId,
772
      .fid = writer->config->fid,
798,454✔
773
      .cid = writer->config->cid,
798,454✔
774
      .size = 0,
775
      .minVer = VERSION_MAX,
776
      .maxVer = VERSION_MIN,
777
  };
778

779
  // .data
780
  ftype = TSDB_FTYPE_DATA;
798,930✔
781
  if (writer->config->files[ftype].exist) {
798,930✔
782
    writer->files[ftype] = writer->config->files[ftype].file;
288,471✔
783
  } else {
784
    code = tsdbAllocateDisk(writer->config->tsdb, tsdbFTypeLabel(ftype), writer->config->expLevel, &diskId);
509,983✔
785
    TSDB_CHECK_CODE(code, lino, _exit);
510,459✔
786
    writer->files[ftype] = (STFile){
1,020,918✔
787
        .type = ftype,
788
        .did = diskId,
789
        .fid = writer->config->fid,
510,459✔
790
        .cid = writer->config->cid,
510,459✔
791
        .size = 0,
792
        .lcn = writer->config->lcn == 0 ? -1 : 0,
510,459✔
793
        .minVer = VERSION_MAX,
794
        .maxVer = VERSION_MIN,
795
    };
796
  }
797

798
  // .sma
799
  ftype = TSDB_FTYPE_SMA;
798,930✔
800
  if (writer->config->files[ftype].exist) {
798,930✔
801
    writer->files[ftype] = writer->config->files[ftype].file;
288,471✔
802
  } else {
803
    code = tsdbAllocateDisk(writer->config->tsdb, tsdbFTypeLabel(ftype), writer->config->expLevel, &diskId);
510,459✔
804
    TSDB_CHECK_CODE(code, lino, _exit);
510,459✔
805
    writer->files[ftype] = (STFile){
1,020,918✔
806
        .type = ftype,
807
        .did = diskId,
808
        .fid = writer->config->fid,
510,459✔
809
        .cid = writer->config->cid,
510,459✔
810
        .size = 0,
811
        .minVer = VERSION_MAX,
812
        .maxVer = VERSION_MIN,
813
    };
814
  }
815

816
  // .tomb
817
  ftype = TSDB_FTYPE_TOMB;
798,930✔
818
  code = tsdbAllocateDisk(writer->config->tsdb, tsdbFTypeLabel(ftype), writer->config->expLevel, &diskId);
798,930✔
819
  TSDB_CHECK_CODE(code, lino, _exit);
798,930✔
820
  writer->files[ftype] = (STFile){
1,597,860✔
821
      .type = ftype,
822
      .did = diskId,
823
      .fid = writer->config->fid,
798,930✔
824
      .cid = writer->config->cid,
798,930✔
825
      .size = 0,
826
      .minVer = VERSION_MAX,
827
      .maxVer = VERSION_MIN,
828
  };
829

830
  // range
831
  writer->ctx->range = (SVersionRange){.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
798,930✔
832
  writer->ctx->tombRange = (SVersionRange){.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
798,930✔
833

834
  writer->ctx->opened = true;
798,930✔
835

836
_exit:
798,930✔
837
  if (code) {
798,930✔
838
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
839
              tstrerror(code));
840
  }
841
  return code;
798,930✔
842
}
843

844
void tsdbWriterUpdVerRange(SVersionRange *range, int64_t minVer, int64_t maxVer) {
77,639,952✔
845
  range->minVer = TMIN(range->minVer, minVer);
77,639,952✔
846
  range->maxVer = TMAX(range->maxVer, maxVer);
77,647,130✔
847
}
77,646,712✔
848

849
int32_t tsdbFileWriteBrinBlock(STsdbFD *fd, SBrinBlock *brinBlock, uint32_t cmprAlg, int64_t *fileSize,
755,626✔
850
                               TBrinBlkArray *brinBlkArray, SBuffer *buffers, SVersionRange *range,
851
                               SEncryptData *encryptData) {
852
  if (brinBlock->numOfRecords == 0) {
755,626✔
853
    return 0;
×
854
  }
855

856
  int32_t  code;
857
  SBuffer *buffer0 = buffers + 0;
755,626✔
858
  SBuffer *buffer1 = buffers + 1;
755,626✔
859
  SBuffer *assist = buffers + 2;
755,626✔
860

861
  SBrinBlk brinBlk = {
755,626✔
862
      .dp[0] =
863
          {
864
              .offset = *fileSize,
755,626✔
865
              .size = 0,
866
          },
867
      .numRec = brinBlock->numOfRecords,
755,626✔
868
      .numOfPKs = brinBlock->numOfPKs,
755,626✔
869
      .cmprAlg = cmprAlg,
870
  };
871
  for (int i = 0; i < brinBlock->numOfRecords; i++) {
47,386,245✔
872
    SBrinRecord record;
46,630,619✔
873

874
    TAOS_CHECK_RETURN(tBrinBlockGet(brinBlock, i, &record));
46,630,619✔
875
    if (i == 0) {
46,630,619✔
876
      brinBlk.minTbid.suid = record.suid;
755,626✔
877
      brinBlk.minTbid.uid = record.uid;
755,626✔
878
      brinBlk.minVer = record.minVer;
755,626✔
879
      brinBlk.maxVer = record.maxVer;
755,626✔
880
    }
881
    if (i == brinBlock->numOfRecords - 1) {
46,630,619✔
882
      brinBlk.maxTbid.suid = record.suid;
755,626✔
883
      brinBlk.maxTbid.uid = record.uid;
755,626✔
884
    }
885
    if (record.minVer < brinBlk.minVer) {
46,630,619✔
886
      brinBlk.minVer = record.minVer;
479,986✔
887
    }
888
    if (record.maxVer > brinBlk.maxVer) {
46,630,619✔
889
      brinBlk.maxVer = record.maxVer;
13,317,879✔
890
    }
891
  }
892

893
  tsdbWriterUpdVerRange(range, brinBlk.minVer, brinBlk.maxVer);
755,626✔
894

895
  // write to file
896
  for (int32_t i = 0; i < 10; ++i) {
8,311,410✔
897
    SCompressInfo info = {
7,555,784✔
898
        .cmprAlg = cmprAlg,
899
        .dataType = TSDB_DATA_TYPE_BIGINT,
900
        .originalSize = brinBlock->buffers[i].size,
7,555,784✔
901
    };
902

903
    tBufferClear(buffer0);
904
    TAOS_CHECK_RETURN(tCompressDataToBuffer(brinBlock->buffers[i].data, &info, buffer0, assist));
7,556,260✔
905
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptData));
7,556,260✔
906
    brinBlk.size[i] = info.compressedSize;
7,556,260✔
907
    brinBlk.dp->size += info.compressedSize;
7,555,784✔
908
    *fileSize += info.compressedSize;
7,555,784✔
909
  }
910
  for (int32_t i = 10; i < 15; ++i) {
4,533,756✔
911
    SCompressInfo info = {
3,778,130✔
912
        .cmprAlg = cmprAlg,
913
        .dataType = TSDB_DATA_TYPE_INT,
914
        .originalSize = brinBlock->buffers[i].size,
3,778,130✔
915
    };
916

917
    tBufferClear(buffer0);
918
    TAOS_CHECK_RETURN(tCompressDataToBuffer(brinBlock->buffers[i].data, &info, buffer0, assist));
3,778,130✔
919
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptData));
3,778,130✔
920
    brinBlk.size[i] = info.compressedSize;
3,778,130✔
921
    brinBlk.dp->size += info.compressedSize;
3,778,130✔
922
    *fileSize += info.compressedSize;
3,778,130✔
923
  }
924

925
  // write primary keys to file
926
  if (brinBlock->numOfPKs > 0) {
755,626✔
927
    tBufferClear(buffer0);
928
    tBufferClear(buffer1);
929

930
    // encode
931
    for (int i = 0; i < brinBlock->numOfPKs; i++) {
6,548✔
932
      SValueColumnCompressInfo info = {.cmprAlg = cmprAlg};
3,274✔
933
      TAOS_CHECK_RETURN(tValueColumnCompress(&brinBlock->firstKeyPKs[i], &info, buffer1, assist));
3,274✔
934
      TAOS_CHECK_RETURN(tValueColumnCompressInfoEncode(&info, buffer0));
3,274✔
935
    }
936
    for (int i = 0; i < brinBlock->numOfPKs; i++) {
6,548✔
937
      SValueColumnCompressInfo info = {.cmprAlg = cmprAlg};
3,274✔
938
      TAOS_CHECK_RETURN(tValueColumnCompress(&brinBlock->lastKeyPKs[i], &info, buffer1, assist));
3,274✔
939
      TAOS_CHECK_RETURN(tValueColumnCompressInfoEncode(&info, buffer0));
3,274✔
940
    }
941

942
    // write to file
943
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptData));
3,274✔
944
    *fileSize += buffer0->size;
3,274✔
945
    brinBlk.dp->size += buffer0->size;
3,274✔
946
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer1->data, buffer1->size, encryptData));
3,274✔
947
    *fileSize += buffer1->size;
3,274✔
948
    brinBlk.dp->size += buffer1->size;
3,274✔
949
  }
950

951
  // append to brinBlkArray
952
  TAOS_CHECK_RETURN(TARRAY2_APPEND_PTR(brinBlkArray, &brinBlk));
1,511,252✔
953

954
  tBrinBlockClear(brinBlock);
755,626✔
955

956
  return 0;
755,626✔
957
}
958

959
static int32_t tsdbDataFileWriteBrinBlock(SDataFileWriter *writer) {
755,626✔
960
  if (writer->brinBlock->numOfRecords == 0) {
755,626✔
961
    return 0;
×
962
  }
963

964
  int32_t code = 0;
755,626✔
965
  int32_t lino = 0;
755,626✔
966

967
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
755,626✔
968

969
  TAOS_CHECK_GOTO(tsdbFileWriteBrinBlock(writer->fd[TSDB_FTYPE_HEAD], writer->brinBlock, writer->config->cmprAlg,
755,626✔
970
                                         &writer->files[TSDB_FTYPE_HEAD].size, writer->brinBlkArray, writer->buffers,
971
                                         &writer->ctx->range, pEncryptData),
972
                  &lino, _exit);
973

974
_exit:
755,626✔
975
  if (code) {
755,626✔
976
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
977
              tstrerror(code));
978
  }
979
  return code;
755,626✔
980
}
981

982
static int32_t tsdbDataFileWriteBrinRecord(SDataFileWriter *writer, const SBrinRecord *record) {
46,627,626✔
983
  int32_t code = 0;
46,627,626✔
984
  int32_t lino = 0;
46,627,626✔
985

986
  for (;;) {
987
    code = tBrinBlockPut(writer->brinBlock, record);
46,628,854✔
988
    if (code == TSDB_CODE_INVALID_PARA) {
46,624,174✔
989
      // different records with different primary keys
990
      TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlock(writer), &lino, _exit);
×
991
      continue;
×
992
    } else {
993
      TSDB_CHECK_CODE(code, lino, _exit);
46,624,174✔
994
    }
995
    break;
46,624,174✔
996
  }
997

998
  if ((writer->brinBlock->numOfRecords) >= 256) {
46,624,174✔
999
    TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlock(writer), &lino, _exit);
93,888✔
1000
  }
1001

1002
_exit:
46,622,751✔
1003
  if (code) {
46,619,192✔
1004
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1005
              tstrerror(code));
1006
  }
1007
  return code;
46,619,192✔
1008
}
1009

1010
static int32_t tsdbDataFileDoWriteBlockData(SDataFileWriter *writer, SBlockData *bData) {
46,504,712✔
1011
  if (bData->nRow == 0) {
46,504,712✔
1012
    return 0;
13,010,800✔
1013
  }
1014

1015
  if (!bData->uid) {
33,496,027✔
1016
    return TSDB_CODE_INVALID_PARA;
×
1017
  }
1018

1019
  int32_t  code = 0;
33,494,187✔
1020
  int32_t  lino = 0;
33,494,187✔
1021
  SBuffer *buffers = writer->buffers;
33,496,363✔
1022
  SBuffer *assist = writer->buffers + 4;
33,495,295✔
1023

1024
  SColCompressInfo cmprInfo = {.pColCmpr = NULL, .defaultCmprAlg = writer->config->cmprAlg};
33,495,468✔
1025

1026
  SBrinRecord record[1] = {{
33,494,973✔
1027
      .suid = bData->suid,
33,494,626✔
1028
      .uid = bData->uid,
33,494,467✔
1029
      .minVer = bData->aVersion[0],
33,494,801✔
1030
      .maxVer = bData->aVersion[0],
33,495,135✔
1031
      .blockOffset = writer->files[TSDB_FTYPE_DATA].size,
33,497,469✔
1032
      .smaOffset = writer->files[TSDB_FTYPE_SMA].size,
33,496,857✔
1033
      .blockSize = 0,
1034
      .blockKeySize = 0,
1035
      .smaSize = 0,
1036
      .numRow = bData->nRow,
33,496,374✔
1037
      .count = 1,
1038
  }};
1039

1040
  tsdbRowGetKey(&tsdbRowFromBlockData(bData, 0), &record->firstKey);
33,495,468✔
1041
  tsdbRowGetKey(&tsdbRowFromBlockData(bData, bData->nRow - 1), &record->lastKey);
33,496,089✔
1042

1043
  for (int32_t i = 1; i < bData->nRow; ++i) {
2,147,483,647✔
1044
    if (tsdbRowCompareWithoutVersion(&tsdbRowFromBlockData(bData, i - 1), &tsdbRowFromBlockData(bData, i)) != 0) {
2,147,483,647✔
1045
      record->count++;
2,147,483,647✔
1046
    }
1047
    if (bData->aVersion[i] < record->minVer) {
2,147,483,647✔
1048
      record->minVer = bData->aVersion[i];
5,422,680✔
1049
    }
1050
    if (bData->aVersion[i] > record->maxVer) {
2,147,483,647✔
1051
      record->maxVer = bData->aVersion[i];
106,362,169✔
1052
    }
1053
  }
1054

1055
  tsdbWriterUpdVerRange(&writer->ctx->range, record->minVer, record->maxVer);
33,497,469✔
1056

1057
  code = metaGetColCmpr(writer->config->tsdb->pVnode->pMeta, bData->suid != 0 ? bData->suid : bData->uid,
33,497,469✔
1058
                        &cmprInfo.pColCmpr);
1059
  if (code) {
33,492,231✔
1060
    tsdbWarn("vgId:%d failed to get column compress algrithm", TD_VID(writer->config->tsdb->pVnode));
×
1061
  }
1062

1063
  TAOS_CHECK_GOTO(tBlockDataCompress(bData, &cmprInfo, buffers, assist), &lino, _exit);
33,492,231✔
1064

1065
  record->blockKeySize = buffers[0].size + buffers[1].size;
33,490,365✔
1066
  record->blockSize = record->blockKeySize + buffers[2].size + buffers[3].size;
33,495,700✔
1067

1068
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
33,493,253✔
1069

1070
  for (int i = 0; i < 4; i++) {
167,469,971✔
1071
    TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd[TSDB_FTYPE_DATA], writer->files[TSDB_FTYPE_DATA].size, buffers[i].data,
133,972,765✔
1072
                                  buffers[i].size, pEncryptData),
1073
                    &lino, _exit);
1074
    writer->files[TSDB_FTYPE_DATA].size += buffers[i].size;
133,971,716✔
1075
  }
1076

1077
  // to .sma file
1078
  tBufferClear(&buffers[0]);
1079
  for (int32_t i = 0; i < bData->nColData; ++i) {
261,717,841✔
1080
    SColData *colData = bData->aColData + i;
228,222,833✔
1081
    if ((colData->cflag & COL_SMA_ON) == 0 || ((colData->flag & HAS_VALUE) == 0)) continue;
228,222,829✔
1082

1083
    SColumnDataAgg sma[1] = {{.colId = colData->cid}};
222,669,938✔
1084
    tColDataCalcSMA[colData->type](colData, sma);
222,666,749✔
1085

1086
    TAOS_CHECK_GOTO(tPutColumnDataAgg(&buffers[0], sma), &lino, _exit);
222,662,921✔
1087
  }
1088
  record->smaSize = buffers[0].size;
33,495,911✔
1089

1090
  if (record->smaSize > 0) {
33,496,523✔
1091
    TAOS_CHECK_GOTO(
33,494,840✔
1092
        tsdbWriteFile(writer->fd[TSDB_FTYPE_SMA], record->smaOffset, buffers[0].data, record->smaSize, pEncryptData),
1093
        &lino, _exit);
1094
    writer->files[TSDB_FTYPE_SMA].size += record->smaSize;
33,490,896✔
1095
  }
1096

1097
  // append SBrinRecord
1098
  TAOS_CHECK_GOTO(tsdbDataFileWriteBrinRecord(writer, record), &lino, _exit);
33,493,807✔
1099

1100
  tBlockDataClear(bData);
33,489,912✔
1101

1102
_exit:
33,495,291✔
1103
  if (code) {
33,492,575✔
1104
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1105
              tstrerror(code));
1106
  }
1107
  taosHashCleanup(cmprInfo.pColCmpr);
33,492,575✔
1108
  return code;
33,491,632✔
1109
}
1110

1111
static int32_t tsdbDataFileDoWriteTSRow(SDataFileWriter *writer, TSDBROW *row) {
2,147,483,647✔
1112
  int32_t code = 0;
2,147,483,647✔
1113
  int32_t lino = 0;
2,147,483,647✔
1114

1115
  // update/append
1116
  if (row->type == TSDBROW_ROW_FMT) {
2,147,483,647✔
1117
    TAOS_CHECK_GOTO(
×
1118
        tsdbUpdateSkmRow(writer->config->tsdb, writer->ctx->tbid, TSDBROW_SVERSION(row), writer->config->skmRow), &lino,
1119
        _exit);
1120
  }
1121

1122
  if (TSDBROW_VERSION(row) <= writer->config->compactVersion  //
2,147,483,647✔
1123
      && writer->blockData->nRow > 0                          //
2,147,483,647✔
1124
      &&
2,147,483,647✔
1125
      tsdbRowCompareWithoutVersion(row, &tsdbRowFromBlockData(writer->blockData, writer->blockData->nRow - 1)) == 0  //
2,147,483,647✔
1126
  ) {
1127
    TAOS_CHECK_GOTO(tBlockDataUpdateRow(writer->blockData, row, writer->config->skmRow->pTSchema), &lino, _exit);
2,147,483,647✔
1128
  } else {
1129
    if (writer->blockData->nRow >= writer->config->maxRow) {
2,147,483,647✔
1130
      TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, writer->blockData), &lino, _exit);
2,715,530✔
1131
    }
1132

1133
    TAOS_CHECK_GOTO(
2,147,483,647✔
1134
        tBlockDataAppendRow(writer->blockData, row, writer->config->skmRow->pTSchema, writer->ctx->tbid->uid), &lino,
1135
        _exit);
1136
  }
1137

1138
_exit:
2,147,483,647✔
1139
  if (code) {
2,147,483,647✔
1140
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1141
              tstrerror(code));
1142
  }
1143
  return code;
2,147,483,647✔
1144
}
1145

1146
static FORCE_INLINE int32_t tsdbRowKeyCmprNullAsLargest(const STsdbRowKey *key1, const STsdbRowKey *key2) {
1147
  if (key1 == NULL) {
2,147,483,647✔
1148
    return 1;
528,968✔
1149
  } else if (key2 == NULL) {
2,147,483,647✔
1150
    return -1;
6,930,532✔
1151
  } else {
1152
    return tsdbRowKeyCmpr(key1, key2);
2,147,483,647✔
1153
  }
1154
}
1155

1156
static int32_t tsdbDataFileDoWriteTableOldData(SDataFileWriter *writer, const STsdbRowKey *key) {
2,147,483,647✔
1157
  if (writer->ctx->tbHasOldData == false) {
2,147,483,647✔
1158
    return 0;
×
1159
  }
1160

1161
  int32_t     code = 0;
2,147,483,647✔
1162
  int32_t     lino = 0;
2,147,483,647✔
1163
  STsdbRowKey rowKey;
2,147,483,647✔
1164

1165
  for (;;) {
6,246✔
1166
    for (;;) {
1167
      // SBlockData
1168
      for (; writer->ctx->blockDataIdx < writer->ctx->blockData->nRow; writer->ctx->blockDataIdx++) {
2,147,483,647✔
1169
        TSDBROW row = tsdbRowFromBlockData(writer->ctx->blockData, writer->ctx->blockDataIdx);
2,147,483,647✔
1170

1171
        tsdbRowGetKey(&row, &rowKey);
2,147,483,647✔
1172
        if (tsdbRowKeyCmprNullAsLargest(&rowKey, key) < 0) {  // key <= rowKey
2,147,483,647✔
1173
          TAOS_CHECK_GOTO(tsdbDataFileDoWriteTSRow(writer, &row), &lino, _exit);
2,147,483,647✔
1174
        } else {
1175
          goto _exit;
2,147,483,647✔
1176
        }
1177
      }
1178

1179
      // SBrinBlock
1180
      if (writer->ctx->brinBlockIdx >= writer->ctx->brinBlock->numOfRecords) {
6,671,635✔
1181
        break;
184,426✔
1182
      }
1183

1184
      for (; writer->ctx->brinBlockIdx < writer->ctx->brinBlock->numOfRecords; writer->ctx->brinBlockIdx++) {
8,711,143✔
1185
        SBrinRecord record;
8,595,914✔
1186
        code = tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, &record);
8,595,914✔
1187
        TSDB_CHECK_CODE(code, lino, _exit);
8,595,914✔
1188
        if (record.uid != writer->ctx->tbid->uid) {
8,595,914✔
1189
          writer->ctx->tbHasOldData = false;
191,012✔
1190
          goto _exit;
191,012✔
1191
        }
1192

1193
        if (tsdbRowKeyCmprNullAsLargest(key, &record.firstKey) < 0) {  // key < record->firstKey
8,404,902✔
1194
          goto _exit;
3,903,174✔
1195
        } else {
1196
          SBrinRecord record[1];
4,501,728✔
1197
          code = tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, record);
4,501,728✔
1198
          TSDB_CHECK_CODE(code, lino, _exit);
4,501,728✔
1199
          if (tsdbRowKeyCmprNullAsLargest(key, &record->lastKey) > 0) {  // key > record->lastKey
4,501,728✔
1200
            if (writer->blockData->nRow > 0) {
2,223,934✔
1201
              TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, writer->blockData), &lino, _exit);
1,558✔
1202
            }
1203

1204
            TAOS_CHECK_GOTO(tsdbDataFileWriteBrinRecord(writer, record), &lino, _exit);
2,223,934✔
1205
          } else {
1206
            TAOS_CHECK_GOTO(tsdbDataFileReadBlockData(writer->ctx->reader, record, writer->ctx->blockData), &lino,
2,277,794✔
1207
                            _exit);
1208

1209
            writer->ctx->blockDataIdx = 0;
2,277,794✔
1210
            writer->ctx->brinBlockIdx++;
2,277,794✔
1211
            break;
2,277,794✔
1212
          }
1213
        }
1214
      }
1215
    }
1216

1217
    // SBrinBlk
1218
    if (writer->ctx->brinBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->brinBlkArray)) {
184,426✔
1219
      writer->ctx->brinBlkArray = NULL;
178,180✔
1220
      writer->ctx->tbHasOldData = false;
178,180✔
1221
      goto _exit;
178,180✔
1222
    } else {
1223
      const SBrinBlk *brinBlk = TARRAY2_GET_PTR(writer->ctx->brinBlkArray, writer->ctx->brinBlkArrayIdx);
6,246✔
1224

1225
      if (brinBlk->minTbid.uid != writer->ctx->tbid->uid) {
6,246✔
1226
        writer->ctx->tbHasOldData = false;
×
1227
        goto _exit;
×
1228
      }
1229

1230
      TAOS_CHECK_GOTO(tsdbDataFileReadBrinBlock(writer->ctx->reader, brinBlk, writer->ctx->brinBlock), &lino, _exit);
6,246✔
1231

1232
      writer->ctx->brinBlockIdx = 0;
6,246✔
1233
      writer->ctx->brinBlkArrayIdx++;
6,246✔
1234
    }
1235
  }
1236

1237
_exit:
2,147,483,647✔
1238
  if (code) {
2,147,483,647✔
1239
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1240
              tstrerror(code));
1241
  }
1242
  return code;
2,147,483,647✔
1243
}
1244

1245
static int32_t tsdbDataFileDoWriteTSData(SDataFileWriter *writer, TSDBROW *row) {
2,147,483,647✔
1246
  int32_t code = 0;
2,147,483,647✔
1247
  int32_t lino = 0;
2,147,483,647✔
1248

1249
  if (writer->ctx->tbHasOldData) {
2,147,483,647✔
1250
    STsdbRowKey key;
2,147,483,647✔
1251
    tsdbRowGetKey(row, &key);
2,147,483,647✔
1252
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTableOldData(writer, &key), &lino, _exit);
2,147,483,647✔
1253
  }
1254

1255
  TAOS_CHECK_GOTO(tsdbDataFileDoWriteTSRow(writer, row), &lino, _exit);
2,147,483,647✔
1256

1257
_exit:
2,147,483,647✔
1258
  if (code) {
2,147,483,647✔
1259
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1260
              tstrerror(code));
1261
  }
1262
  return code;
2,147,483,647✔
1263
}
1264

1265
static int32_t tsdbDataFileWriteTableDataEnd(SDataFileWriter *writer) {
14,611,517✔
1266
  if (writer->ctx->tbid->uid == 0) {
14,611,517✔
1267
    return 0;
661,738✔
1268
  }
1269

1270
  int32_t code = 0;
13,950,448✔
1271
  int32_t lino = 0;
13,950,448✔
1272

1273
  if (writer->ctx->tbHasOldData) {
13,950,113✔
1274
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTableOldData(writer, NULL /* as the largest key */), &lino, _exit);
134,549✔
1275
  }
1276

1277
  TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, writer->blockData), &lino, _exit);
13,950,113✔
1278

1279
_exit:
13,949,940✔
1280
  if (code) {
13,949,940✔
1281
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1282
              tstrerror(code));
1283
  }
1284
  return code;
13,949,940✔
1285
}
1286

1287
static int32_t tsdbDataFileWriteTableDataBegin(SDataFileWriter *writer, const TABLEID *tbid) {
14,611,517✔
1288
  int32_t code = 0;
14,611,517✔
1289
  int32_t lino = 0;
14,611,517✔
1290

1291
  SMetaInfo info;
14,611,851✔
1292
  bool      drop = false;
14,611,851✔
1293
  TABLEID   tbid1[1];
14,611,851✔
1294
  writer->ctx->tbHasOldData = false;
14,611,851✔
1295
  while (writer->ctx->brinBlkArray) {  // skip data of previous table
14,885,988✔
1296
    for (; writer->ctx->brinBlockIdx < writer->ctx->brinBlock->numOfRecords; writer->ctx->brinBlockIdx++) {
14,011,478✔
1297
      SBrinRecord record;
13,659,282✔
1298
      TAOS_CHECK_GOTO(tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, &record), &lino, _exit);
13,659,282✔
1299

1300
      if (record.uid == tbid->uid) {
13,658,709✔
1301
        writer->ctx->tbHasOldData = true;
369,192✔
1302
        goto _begin;
369,192✔
1303
      } else if (record.suid > tbid->suid || (record.suid == tbid->suid && record.uid > tbid->uid)) {
13,289,929✔
1304
        goto _begin;
2,343,154✔
1305
      } else {
1306
        if (record.uid != writer->ctx->tbid->uid) {
10,946,775✔
1307
          if (drop && tbid1->uid == record.uid) {
4,008,737✔
1308
            continue;
×
1309
          } else if (metaGetInfo(writer->config->tsdb->pVnode->pMeta, record.uid, &info, NULL) != 0) {
4,008,737✔
1310
            drop = true;
38,493✔
1311
            tbid1->suid = record.suid;
38,493✔
1312
            tbid1->uid = record.uid;
38,493✔
1313
            continue;
38,493✔
1314
          } else {
1315
            drop = false;
3,970,244✔
1316
            writer->ctx->tbid->suid = record.suid;
3,970,244✔
1317
            writer->ctx->tbid->uid = record.uid;
3,970,244✔
1318
          }
1319
        }
1320

1321
        TAOS_CHECK_GOTO(tsdbDataFileWriteBrinRecord(writer, &record), &lino, _exit);
10,908,604✔
1322
      }
1323
    }
1324

1325
    if (writer->ctx->brinBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->brinBlkArray)) {
352,035✔
1326
      writer->ctx->brinBlkArray = NULL;
77,447✔
1327
      break;
77,447✔
1328
    } else {
1329
      const SBrinBlk *brinBlk = TARRAY2_GET_PTR(writer->ctx->brinBlkArray, writer->ctx->brinBlkArrayIdx);
274,588✔
1330

1331
      TAOS_CHECK_GOTO(tsdbDataFileReadBrinBlock(writer->ctx->reader, brinBlk, writer->ctx->brinBlock), &lino, _exit);
274,588✔
1332

1333
      writer->ctx->brinBlockIdx = 0;
274,588✔
1334
      writer->ctx->brinBlkArrayIdx++;
274,588✔
1335
    }
1336
  }
1337

1338
_begin:
14,611,691✔
1339
  writer->ctx->tbid[0] = *tbid;
14,611,691✔
1340

1341
  if (tbid->uid == INT64_MAX) {
14,611,852✔
1342
    goto _exit;
661,738✔
1343
  }
1344

1345
  TAOS_CHECK_GOTO(tsdbUpdateSkmTb(writer->config->tsdb, tbid, writer->config->skmTb), &lino, _exit);
13,950,275✔
1346
  TAOS_CHECK_GOTO(tBlockDataInit(writer->blockData, writer->ctx->tbid, writer->config->skmTb->pTSchema, NULL, 0), &lino,
13,949,941✔
1347
                  _exit);
1348

1349
_exit:
14,612,347✔
1350
  if (code) {
14,612,347✔
1351
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1352
              tstrerror(code));
1353
  }
1354
  return code;
14,612,347✔
1355
}
1356

1357
int32_t tsdbFileWriteHeadFooter(STsdbFD *fd, int64_t *fileSize, const SHeadFooter *footer, SEncryptData *encryptData) {
661,738✔
1358
  TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, (const uint8_t *)footer, sizeof(*footer), encryptData));
661,738✔
1359
  *fileSize += sizeof(*footer);
661,738✔
1360
  return 0;
661,738✔
1361
}
1362

1363
int32_t tsdbFileWriteTombBlock(STsdbFD *fd, STombBlock *tombBlock, int8_t cmprAlg, int64_t *fileSize,
1,683,526✔
1364
                               TTombBlkArray *tombBlkArray, SBuffer *buffers, SVersionRange *range,
1365
                               SEncryptData *encryptData) {
1366
  int32_t code;
1367

1368
  if (TOMB_BLOCK_SIZE(tombBlock) == 0) {
1,683,526✔
1369
    return 0;
×
1370
  }
1371

1372
  SBuffer *buffer0 = buffers + 0;
1,683,526✔
1373
  SBuffer *assist = buffers + 1;
1,683,526✔
1374

1375
  STombBlk tombBlk = {
1,683,526✔
1376
      .dp[0] =
1377
          {
1378
              .offset = *fileSize,
1,683,526✔
1379
              .size = 0,
1380
          },
1381
      .numRec = TOMB_BLOCK_SIZE(tombBlock),
1,683,526✔
1382
      .cmprAlg = cmprAlg,
1383
  };
1384
  for (int i = 0; i < TOMB_BLOCK_SIZE(tombBlock); i++) {
41,091,830✔
1385
    STombRecord record;
39,408,304✔
1386
    TAOS_CHECK_RETURN(tTombBlockGet(tombBlock, i, &record));
39,408,304✔
1387

1388
    if (i == 0) {
39,408,304✔
1389
      tombBlk.minTbid.suid = record.suid;
1,683,526✔
1390
      tombBlk.minTbid.uid = record.uid;
1,683,526✔
1391
      tombBlk.minVer = record.version;
1,683,526✔
1392
      tombBlk.maxVer = record.version;
1,683,526✔
1393
    }
1394
    if (i == TOMB_BLOCK_SIZE(tombBlock) - 1) {
39,408,304✔
1395
      tombBlk.maxTbid.suid = record.suid;
1,683,526✔
1396
      tombBlk.maxTbid.uid = record.uid;
1,683,526✔
1397
    }
1398
    if (record.version < tombBlk.minVer) {
39,408,304✔
1399
      tombBlk.minVer = record.version;
3,029✔
1400
    }
1401
    if (record.version > tombBlk.maxVer) {
39,408,304✔
1402
      tombBlk.maxVer = record.version;
35,491,175✔
1403
    }
1404
  }
1405

1406
  tsdbWriterUpdVerRange(range, tombBlk.minVer, tombBlk.maxVer);
1,683,526✔
1407

1408
  for (int32_t i = 0; i < ARRAY_SIZE(tombBlock->buffers); i++) {
10,101,156✔
1409
    tBufferClear(buffer0);
1410

1411
    SCompressInfo cinfo = {
8,417,630✔
1412
        .cmprAlg = cmprAlg,
1413
        .dataType = TSDB_DATA_TYPE_BIGINT,
1414
        .originalSize = tombBlock->buffers[i].size,
8,417,630✔
1415
    };
1416
    TAOS_CHECK_RETURN(tCompressDataToBuffer(tombBlock->buffers[i].data, &cinfo, buffer0, assist));
8,417,630✔
1417
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptData));
8,417,630✔
1418

1419
    tombBlk.size[i] = cinfo.compressedSize;
8,417,630✔
1420
    tombBlk.dp->size += tombBlk.size[i];
8,417,630✔
1421
    *fileSize += tombBlk.size[i];
8,417,630✔
1422
  }
1423

1424
  TAOS_CHECK_RETURN(TARRAY2_APPEND_PTR(tombBlkArray, &tombBlk));
3,367,052✔
1425

1426
  tTombBlockClear(tombBlock);
1,683,526✔
1427
  return 0;
1,683,526✔
1428
}
1429

1430
static int32_t tsdbDataFileWriteHeadFooter(SDataFileWriter *writer) {
661,738✔
1431
  int32_t code = 0;
661,738✔
1432
  int32_t lino = 0;
661,738✔
1433

1434
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
661,738✔
1435

1436
  TAOS_CHECK_GOTO(tsdbFileWriteHeadFooter(writer->fd[TSDB_FTYPE_HEAD], &writer->files[TSDB_FTYPE_HEAD].size,
661,738✔
1437
                                          writer->headFooter, pEncryptData),
1438
                  &lino, _exit);
1439

1440
_exit:
661,738✔
1441
  if (code) {
661,738✔
1442
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1443
              tstrerror(code));
1444
  }
1445
  return code;
661,738✔
1446
}
1447

1448
static int32_t tsdbDataFileDoWriteTombBlock(SDataFileWriter *writer) {
139,753✔
1449
  if (TOMB_BLOCK_SIZE(writer->tombBlock) == 0) return 0;
139,753✔
1450

1451
  int32_t code = 0;
139,753✔
1452
  int32_t lino = 0;
139,753✔
1453

1454
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
139,753✔
1455

1456
  TAOS_CHECK_GOTO(tsdbFileWriteTombBlock(writer->fd[TSDB_FTYPE_TOMB], writer->tombBlock, writer->config->cmprAlg,
139,753✔
1457
                                         &writer->files[TSDB_FTYPE_TOMB].size, writer->tombBlkArray, writer->buffers,
1458
                                         &writer->ctx->tombRange, pEncryptData),
1459
                  &lino, _exit);
1460

1461
_exit:
139,753✔
1462
  if (code) {
139,753✔
1463
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1464
              tstrerror(code));
1465
  }
1466
  return code;
139,753✔
1467
}
1468

1469
int32_t tsdbFileWriteTombBlk(STsdbFD *fd, const TTombBlkArray *tombBlkArray, SFDataPtr *ptr, int64_t *fileSize,
18,378,675✔
1470
                             SEncryptData *encryptData) {
1471
  ptr->size = TARRAY2_DATA_LEN(tombBlkArray);
18,378,675✔
1472
  if (ptr->size > 0) {
18,384,720✔
1473
    ptr->offset = *fileSize;
1,683,526✔
1474

1475
    TAOS_CHECK_RETURN(
1,683,526✔
1476
        tsdbWriteFile(fd, *fileSize, (const uint8_t *)TARRAY2_DATA(tombBlkArray), ptr->size, encryptData));
1477

1478
    *fileSize += ptr->size;
1,683,526✔
1479
  }
1480
  return 0;
18,381,380✔
1481
}
1482

1483
static int32_t tsdbDataFileDoWriteTombBlk(SDataFileWriter *writer) {
139,753✔
1484
  if (TARRAY2_SIZE(writer->tombBlkArray) <= 0) {
139,753✔
1485
    return TSDB_CODE_INVALID_PARA;
×
1486
  }
1487

1488
  int32_t code = 0;
139,753✔
1489
  int32_t lino = 0;
139,753✔
1490

1491
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
139,753✔
1492

1493
  TAOS_CHECK_GOTO(
139,753✔
1494
      tsdbFileWriteTombBlk(writer->fd[TSDB_FTYPE_TOMB], writer->tombBlkArray, writer->tombFooter->tombBlkPtr,
1495
                           &writer->files[TSDB_FTYPE_TOMB].size, pEncryptData),
1496
      &lino, _exit);
1497

1498
_exit:
139,753✔
1499
  if (code) {
139,753✔
1500
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1501
              tstrerror(code));
1502
  }
1503
  return code;
139,753✔
1504
}
1505

1506
int32_t tsdbFileWriteTombFooter(STsdbFD *fd, const STombFooter *footer, int64_t *fileSize, SEncryptData *encryptData) {
139,753✔
1507
  TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, (const uint8_t *)footer, sizeof(*footer), encryptData));
139,753✔
1508
  *fileSize += sizeof(*footer);
139,753✔
1509
  return 0;
139,753✔
1510
}
1511

1512
static int32_t tsdbDataFileWriteTombFooter(SDataFileWriter *writer) {
139,753✔
1513
  int32_t code = 0;
139,753✔
1514
  int32_t lino = 0;
139,753✔
1515

1516
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
139,753✔
1517

1518
  TAOS_CHECK_GOTO(tsdbFileWriteTombFooter(writer->fd[TSDB_FTYPE_TOMB], writer->tombFooter,
139,753✔
1519
                                          &writer->files[TSDB_FTYPE_TOMB].size, pEncryptData),
1520
                  &lino, _exit);
1521

1522
_exit:
139,753✔
1523
  if (code) {
139,753✔
1524
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1525
              tstrerror(code));
1526
  }
1527
  return code;
139,753✔
1528
}
1529

1530
static int32_t tsdbDataFileDoWriteTombRecord(SDataFileWriter *writer, const STombRecord *record) {
543,416✔
1531
  int32_t code = 0;
543,416✔
1532
  int32_t lino = 0;
543,416✔
1533

1534
  while (writer->ctx->hasOldTomb) {
645,973✔
1535
    for (; writer->ctx->tombBlockIdx < TOMB_BLOCK_SIZE(writer->ctx->tombBlock); writer->ctx->tombBlockIdx++) {
33,014,581✔
1536
      STombRecord record1[1];
32,809,467✔
1537
      TAOS_CHECK_GOTO(tTombBlockGet(writer->ctx->tombBlock, writer->ctx->tombBlockIdx, record1), &lino, _exit);
32,809,467✔
1538

1539
      int32_t c = tTombRecordCompare(record, record1);
32,809,467✔
1540
      if (c < 0) {
32,809,467✔
1541
        goto _write;
×
1542
      } else if (c > 0) {
32,809,467✔
1543
        TAOS_CHECK_GOTO(tTombBlockPut(writer->tombBlock, record1), &lino, _exit);
32,809,467✔
1544

1545
        tsdbTrace("vgId:%d write tomb record to tomb file:%s, cid:%" PRId64 ", suid:%" PRId64 ", uid:%" PRId64
32,809,467✔
1546
                  ", version:%" PRId64,
1547
                  TD_VID(writer->config->tsdb->pVnode), writer->fd[TSDB_FTYPE_TOMB]->path, writer->config->cid,
1548
                  record1->suid, record1->uid, record1->version);
1549

1550
        if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
32,809,467✔
1551
          TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlock(writer), &lino, _exit);
×
1552
        }
1553
      } else {
1554
        tsdbError("vgId:%d duplicate tomb record, cid:%" PRId64 ", suid:%" PRId64 ", uid:%" PRId64 ", version:%" PRId64,
×
1555
                  TD_VID(writer->config->tsdb->pVnode), writer->config->cid, record->suid, record->uid,
1556
                  record->version);
1557
      }
1558
    }
1559

1560
    if (writer->ctx->tombBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->tombBlkArray)) {
205,114✔
1561
      writer->ctx->hasOldTomb = false;
102,557✔
1562
      break;
102,557✔
1563
    } else {
1564
      const STombBlk *tombBlk = TARRAY2_GET_PTR(writer->ctx->tombBlkArray, writer->ctx->tombBlkArrayIdx);
102,557✔
1565

1566
      TAOS_CHECK_GOTO(tsdbDataFileReadTombBlock(writer->ctx->reader, tombBlk, writer->ctx->tombBlock), &lino, _exit);
102,557✔
1567

1568
      writer->ctx->tombBlockIdx = 0;
102,557✔
1569
      writer->ctx->tombBlkArrayIdx++;
102,557✔
1570
    }
1571
  }
1572

1573
_write:
543,416✔
1574
  if (record->suid == INT64_MAX) {
543,416✔
1575
    goto _exit;
139,753✔
1576
  }
1577

1578
  TAOS_CHECK_GOTO(tTombBlockPut(writer->tombBlock, record), &lino, _exit);
403,663✔
1579

1580
  tsdbTrace("vgId:%d write tomb record to tomb file:%s, cid:%" PRId64 ", suid:%" PRId64 ", uid:%" PRId64
403,663✔
1581
            ", version:%" PRId64,
1582
            TD_VID(writer->config->tsdb->pVnode), writer->fd[TSDB_FTYPE_TOMB]->path, writer->config->cid, record->suid,
1583
            record->uid, record->version);
1584

1585
  if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
403,663✔
1586
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlock(writer), &lino, _exit);
×
1587
  }
1588

1589
_exit:
543,416✔
1590
  if (code) {
543,416✔
1591
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1592
              tstrerror(code));
1593
  }
1594
  return code;
543,416✔
1595
}
1596

1597
int32_t tsdbFileWriteBrinBlk(STsdbFD *fd, TBrinBlkArray *brinBlkArray, SFDataPtr *ptr, int64_t *fileSize,
661,738✔
1598
                             SEncryptData *encryptData) {
1599
  if (TARRAY2_SIZE(brinBlkArray) <= 0) {
661,738✔
1600
    return TSDB_CODE_INVALID_PARA;
×
1601
  }
1602
  ptr->offset = *fileSize;
661,738✔
1603
  ptr->size = TARRAY2_DATA_LEN(brinBlkArray);
661,738✔
1604

1605
  TAOS_CHECK_RETURN(tsdbWriteFile(fd, ptr->offset, (uint8_t *)TARRAY2_DATA(brinBlkArray), ptr->size, encryptData));
661,738✔
1606

1607
  *fileSize += ptr->size;
661,738✔
1608
  return 0;
661,738✔
1609
}
1610

1611
static int32_t tsdbDataFileWriteBrinBlk(SDataFileWriter *writer) {
661,738✔
1612
  int32_t code = 0;
661,738✔
1613
  int32_t lino = 0;
661,738✔
1614

1615
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
661,738✔
1616

1617
  TAOS_CHECK_GOTO(
661,738✔
1618
      tsdbFileWriteBrinBlk(writer->fd[TSDB_FTYPE_HEAD], writer->brinBlkArray, writer->headFooter->brinBlkPtr,
1619
                           &writer->files[TSDB_FTYPE_HEAD].size, pEncryptData),
1620
      &lino, _exit);
1621

1622
_exit:
661,738✔
1623
  if (code) {
661,738✔
1624
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1625
              tstrerror(code));
1626
  }
1627
  return code;
661,738✔
1628
}
1629

1630
void tsdbTFileUpdVerRange(STFile *f, SVersionRange range) {
21,158,704✔
1631
  f->minVer = TMIN(f->minVer, range.minVer);
21,158,704✔
1632
  f->maxVer = TMAX(f->maxVer, range.maxVer);
21,164,448✔
1633
}
21,168,841✔
1634

1635
static int32_t tsdbDataFileWriterCloseCommit(SDataFileWriter *writer, TFileOpArray *opArr) {
798,930✔
1636
  int32_t code = 0;
798,930✔
1637
  int32_t lino = 0;
798,930✔
1638

1639
  int32_t  ftype;
1640
  STFileOp op;
798,930✔
1641

1642
  if (writer->fd[TSDB_FTYPE_HEAD]) {
798,930✔
1643
    TABLEID tbid[1] = {{
661,738✔
1644
        .suid = INT64_MAX,
1645
        .uid = INT64_MAX,
1646
    }};
1647

1648
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataEnd(writer), &lino, _exit);
661,738✔
1649
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataBegin(writer, tbid), &lino, _exit);
661,738✔
1650
    TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlock(writer), &lino, _exit);
661,738✔
1651
    TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlk(writer), &lino, _exit);
661,738✔
1652
    TAOS_CHECK_GOTO(tsdbDataFileWriteHeadFooter(writer), &lino, _exit);
661,738✔
1653

1654
    SVersionRange ofRange = {.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
661,738✔
1655

1656
    // .head
1657
    ftype = TSDB_FTYPE_HEAD;
661,738✔
1658
    if (writer->config->files[ftype].exist) {
661,738✔
1659
      op = (STFileOp){
255,627✔
1660
          .optype = TSDB_FOP_REMOVE,
1661
          .fid = writer->config->fid,
255,627✔
1662
          .of = writer->config->files[ftype].file,
255,627✔
1663
      };
1664
      ofRange = (SVersionRange){.minVer = op.of.minVer, .maxVer = op.of.maxVer};
255,627✔
1665
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
511,254✔
1666
    }
1667
    op = (STFileOp){
661,738✔
1668
        .optype = TSDB_FOP_CREATE,
1669
        .fid = writer->config->fid,
661,738✔
1670
        .nf = writer->files[ftype],
661,738✔
1671
    };
1672
    tsdbTFileUpdVerRange(&op.nf, ofRange);
661,738✔
1673
    tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
661,738✔
1674
    TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
1,323,476✔
1675

1676
    // .data
1677
    ftype = TSDB_FTYPE_DATA;
661,738✔
1678
    if (!writer->config->files[ftype].exist) {
661,738✔
1679
      op = (STFileOp){
406,111✔
1680
          .optype = TSDB_FOP_CREATE,
1681
          .fid = writer->config->fid,
406,111✔
1682
          .nf = writer->files[ftype],
406,111✔
1683
      };
1684
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
406,111✔
1685
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
812,222✔
1686
    } else if (writer->config->files[ftype].file.size != writer->files[ftype].size) {
255,627✔
1687
      op = (STFileOp){
255,627✔
1688
          .optype = TSDB_FOP_MODIFY,
1689
          .fid = writer->config->fid,
255,627✔
1690
          .of = writer->config->files[ftype].file,
255,627✔
1691
          .nf = writer->files[ftype],
255,627✔
1692
      };
1693
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
255,627✔
1694
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
511,254✔
1695
    }
1696

1697
    // .sma
1698
    ftype = TSDB_FTYPE_SMA;
661,738✔
1699
    if (!writer->config->files[ftype].exist) {
661,738✔
1700
      op = (STFileOp){
406,111✔
1701
          .optype = TSDB_FOP_CREATE,
1702
          .fid = writer->config->fid,
406,111✔
1703
          .nf = writer->files[ftype],
406,111✔
1704
      };
1705
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
406,111✔
1706
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
812,222✔
1707
    } else if (writer->config->files[ftype].file.size != writer->files[ftype].size) {
255,627✔
1708
      op = (STFileOp){
255,627✔
1709
          .optype = TSDB_FOP_MODIFY,
1710
          .fid = writer->config->fid,
255,627✔
1711
          .of = writer->config->files[ftype].file,
255,627✔
1712
          .nf = writer->files[ftype],
255,627✔
1713
      };
1714
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
255,627✔
1715
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
511,254✔
1716
    }
1717
  }
1718

1719
  if (writer->fd[TSDB_FTYPE_TOMB]) {
798,930✔
1720
    STombRecord record[1] = {{
139,753✔
1721
        .suid = INT64_MAX,
1722
        .uid = INT64_MAX,
1723
        .version = INT64_MAX,
1724
    }};
1725

1726
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombRecord(writer, record), &lino, _exit);
139,753✔
1727
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlock(writer), &lino, _exit);
139,753✔
1728
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlk(writer), &lino, _exit);
139,753✔
1729
    TAOS_CHECK_GOTO(tsdbDataFileWriteTombFooter(writer), &lino, _exit);
139,753✔
1730

1731
    SVersionRange ofRange = (SVersionRange){.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
139,753✔
1732

1733
    ftype = TSDB_FTYPE_TOMB;
139,753✔
1734
    if (writer->config->files[ftype].exist) {
139,753✔
1735
      op = (STFileOp){
102,557✔
1736
          .optype = TSDB_FOP_REMOVE,
1737
          .fid = writer->config->fid,
102,557✔
1738
          .of = writer->config->files[ftype].file,
102,557✔
1739
      };
1740
      ofRange = (SVersionRange){.minVer = op.of.minVer, .maxVer = op.of.maxVer};
102,557✔
1741
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
205,114✔
1742
    }
1743
    op = (STFileOp){
139,753✔
1744
        .optype = TSDB_FOP_CREATE,
1745
        .fid = writer->config->fid,
139,753✔
1746
        .nf = writer->files[ftype],
139,753✔
1747
    };
1748
    tsdbTFileUpdVerRange(&op.nf, ofRange);
139,753✔
1749
    tsdbTFileUpdVerRange(&op.nf, writer->ctx->tombRange);
139,753✔
1750
    TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
279,506✔
1751
  }
1752
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
798,930✔
1753
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
3,994,650✔
1754
    if (writer->fd[i]) {
3,195,720✔
1755
      TAOS_CHECK_GOTO(tsdbFsyncFile(writer->fd[i], pEncryptData), &lino, _exit);
2,124,967✔
1756
      tsdbCloseFile(&writer->fd[i]);
2,124,491✔
1757
    }
1758
  }
1759

1760
_exit:
798,930✔
1761
  if (code) {
798,930✔
1762
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1763
              tstrerror(code));
1764
  }
1765
  return code;
798,930✔
1766
}
1767

1768
static int32_t tsdbDataFileWriterOpenDataFD(SDataFileWriter *writer) {
661,738✔
1769
  int32_t code = 0;
661,738✔
1770
  int32_t lino = 0;
661,738✔
1771

1772
  int32_t ftypes[] = {TSDB_FTYPE_HEAD, TSDB_FTYPE_DATA, TSDB_FTYPE_SMA};
661,738✔
1773

1774
  for (int32_t i = 0; i < ARRAY_SIZE(ftypes); ++i) {
2,646,952✔
1775
    int32_t ftype = ftypes[i];
1,985,214✔
1776

1777
    char    fname[TSDB_FILENAME_LEN];
1,985,214✔
1778
    int32_t flag = TD_FILE_READ | TD_FILE_WRITE;
1,985,214✔
1779

1780
    if (writer->files[ftype].size == 0) {
1,985,214✔
1781
      flag |= (TD_FILE_CREATE | TD_FILE_TRUNC);
1,473,960✔
1782
    }
1783

1784
    int32_t lcn = writer->files[ftype].lcn;
1,985,214✔
1785
    tsdbTFileName(writer->config->tsdb, &writer->files[ftype], fname);
1,985,214✔
1786
    TAOS_CHECK_GOTO(tsdbOpenFile(fname, writer->config->tsdb, flag, &writer->fd[ftype], lcn), &lino, _exit);
1,985,214✔
1787

1788
    if (writer->files[ftype].size == 0) {
1,985,214✔
1789
      uint8_t hdr[TSDB_FHDR_SIZE] = {0};
1,473,960✔
1790

1791
      SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
1,473,960✔
1792

1793
      TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd[ftype], 0, hdr, TSDB_FHDR_SIZE, pEncryptData), &lino, _exit);
1,473,960✔
1794

1795
      writer->files[ftype].size += TSDB_FHDR_SIZE;
1,473,960✔
1796
    }
1797
  }
1798

1799
  if (writer->ctx->reader) {
661,738✔
1800
    TAOS_CHECK_GOTO(tsdbDataFileReadBrinBlk(writer->ctx->reader, &writer->ctx->brinBlkArray), &lino, _exit);
255,627✔
1801
  }
1802

1803
_exit:
661,738✔
1804
  if (code) {
661,738✔
1805
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1806
              tstrerror(code));
1807
  }
1808
  return code;
661,738✔
1809
}
1810

1811
int32_t tsdbDataFileWriterOpen(const SDataFileWriterConfig *config, SDataFileWriter **writer) {
4,420,913✔
1812
  writer[0] = taosMemoryCalloc(1, sizeof(*writer[0]));
4,420,913✔
1813
  if (!writer[0]) {
4,422,757✔
1814
    return terrno;
×
1815
  }
1816

1817
  writer[0]->config[0] = config[0];
4,423,233✔
1818
  return 0;
4,424,185✔
1819
}
1820

1821
int32_t tsdbDataFileWriterClose(SDataFileWriter **writer, bool abort, TFileOpArray *opArr) {
4,422,757✔
1822
  if (writer == NULL || writer[0] == NULL) return 0;
4,422,757✔
1823

1824
  int32_t code = 0;
4,422,757✔
1825
  int32_t lino = 0;
4,422,757✔
1826

1827
  if (writer[0]->ctx->opened) {
4,423,233✔
1828
    if (abort) {
798,930✔
1829
      TAOS_CHECK_GOTO(tsdbDataFileWriterCloseAbort(writer[0]), &lino, _exit);
×
1830
    } else {
1831
      TAOS_CHECK_GOTO(tsdbDataFileWriterCloseCommit(writer[0], opArr), &lino, _exit);
798,930✔
1832
    }
1833
    tsdbDataFileWriterDoClose(writer[0]);
798,930✔
1834
  }
1835
  taosMemoryFree(writer[0]);
4,423,621✔
1836
  writer[0] = NULL;
4,422,193✔
1837

1838
_exit:
4,422,193✔
1839
  if (code) {
4,422,193✔
1840
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer[0]->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1841
              tstrerror(code));
1842
  }
1843
  return code;
4,422,193✔
1844
}
1845

1846
int32_t tsdbDataFileWriteRow(SDataFileWriter *writer, SRowInfo *row) {
2,147,483,647✔
1847
  int32_t code = 0;
2,147,483,647✔
1848
  int32_t lino = 0;
2,147,483,647✔
1849

1850
  if (!writer->ctx->opened) {
2,147,483,647✔
1851
    TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpen(writer), &lino, _exit);
81,681✔
1852
  }
1853

1854
  if (writer->fd[TSDB_FTYPE_HEAD] == NULL) {
2,147,483,647✔
1855
    TAOS_CHECK_GOTO(tsdbDataFileWriterOpenDataFD(writer), &lino, _exit);
81,681✔
1856
  }
1857

1858
  if (row->uid != writer->ctx->tbid->uid) {
2,147,483,647✔
1859
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataEnd(writer), &lino, _exit);
167,238✔
1860
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataBegin(writer, (TABLEID *)row), &lino, _exit);
167,238✔
1861
  }
1862

1863
  TAOS_CHECK_GOTO(tsdbDataFileDoWriteTSData(writer, &row->row), &lino, _exit);
2,147,483,647✔
1864

1865
_exit:
2,147,483,647✔
1866
  if (code) {
2,147,483,647✔
1867
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1868
              tstrerror(code));
1869
  }
1870
  return code;
2,147,483,647✔
1871
}
1872

1873
int32_t tsdbDataFileWriteBlockData(SDataFileWriter *writer, SBlockData *bData) {
31,103,660✔
1874
  if (bData->nRow == 0) {
31,103,660✔
1875
    return 0;
×
1876
  }
1877

1878
  int32_t code = 0;
31,104,328✔
1879
  int32_t lino = 0;
31,104,328✔
1880

1881
  if (!bData->uid) {
31,103,382✔
1882
    return TSDB_CODE_INVALID_PARA;
×
1883
  }
1884

1885
  if (!writer->ctx->opened) {
31,104,502✔
1886
    TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpen(writer), &lino, _exit);
579,187✔
1887
  }
1888

1889
  if (writer->fd[TSDB_FTYPE_DATA] == NULL) {
31,104,663✔
1890
    TAOS_CHECK_GOTO(tsdbDataFileWriterOpenDataFD(writer), &lino, _exit);
580,057✔
1891
  }
1892

1893
  if (bData->uid != writer->ctx->tbid->uid) {
31,104,663✔
1894
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataEnd(writer), &lino, _exit);
13,783,371✔
1895
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataBegin(writer, (TABLEID *)bData), &lino, _exit);
13,781,929✔
1896
  }
1897

1898
  if (writer->ctx->tbHasOldData) {
31,104,332✔
1899
    STsdbRowKey key;
2,157,393✔
1900

1901
    tsdbRowGetKey(&tsdbRowFromBlockData(bData, 0), &key);
2,157,393✔
1902
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTableOldData(writer, &key), &lino, _exit);
2,157,393✔
1903
  }
1904

1905
  if (!writer->ctx->tbHasOldData       //
31,103,439✔
1906
      && writer->blockData->nRow == 0  //
29,172,311✔
1907
  ) {
1908
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, bData), &lino, _exit);
29,041,373✔
1909

1910
  } else {
1911
    for (int32_t i = 0; i < bData->nRow; ++i) {
2,147,483,647✔
1912
      TSDBROW row[1] = {tsdbRowFromBlockData(bData, i)};
2,147,483,647✔
1913
      TAOS_CHECK_GOTO(tsdbDataFileDoWriteTSData(writer, row), &lino, _exit);
2,147,483,647✔
1914
    }
1915
  }
1916

1917
_exit:
31,090,921✔
1918
  if (code) {
31,097,201✔
1919
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1920
              tstrerror(code));
1921
  }
1922
  return code;
31,098,648✔
1923
}
1924

1925
int32_t tsdbDataFileFlush(SDataFileWriter *writer) {
918,068✔
1926
  if (!writer->ctx->opened) {
918,068✔
1927
    return TSDB_CODE_INVALID_PARA;
×
1928
  }
1929

1930
  if (writer->blockData->nRow == 0) return 0;
918,068✔
1931
  if (writer->ctx->tbHasOldData) return 0;
918,068✔
1932

1933
  return tsdbDataFileDoWriteBlockData(writer, writer->blockData);
798,703✔
1934
}
1935

1936
static int32_t tsdbDataFileWriterOpenTombFD(SDataFileWriter *writer) {
139,753✔
1937
  int32_t code = 0;
139,753✔
1938
  int32_t lino = 0;
139,753✔
1939

1940
  char    fname[TSDB_FILENAME_LEN];
139,753✔
1941
  int32_t ftype = TSDB_FTYPE_TOMB;
139,753✔
1942

1943
  int32_t flag = (TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
139,753✔
1944

1945
  int32_t lcn = writer->files[ftype].lcn;
139,753✔
1946
  tsdbTFileName(writer->config->tsdb, writer->files + ftype, fname);
139,753✔
1947

1948
  TAOS_CHECK_GOTO(tsdbOpenFile(fname, writer->config->tsdb, flag, &writer->fd[ftype], lcn), &lino, _exit);
139,753✔
1949

1950
  uint8_t hdr[TSDB_FHDR_SIZE] = {0};
139,753✔
1951
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
139,753✔
1952

1953
  TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd[ftype], 0, hdr, TSDB_FHDR_SIZE, pEncryptData), &lino, _exit);
139,753✔
1954
  writer->files[ftype].size += TSDB_FHDR_SIZE;
139,753✔
1955

1956
  if (writer->ctx->reader) {
139,753✔
1957
    TAOS_CHECK_GOTO(tsdbDataFileReadTombBlk(writer->ctx->reader, &writer->ctx->tombBlkArray), &lino, _exit);
136,003✔
1958

1959
    if (TARRAY2_SIZE(writer->ctx->tombBlkArray) > 0) {
136,003✔
1960
      writer->ctx->hasOldTomb = true;
102,557✔
1961
    }
1962

1963
    writer->ctx->tombBlkArrayIdx = 0;
136,003✔
1964
    tTombBlockClear(writer->ctx->tombBlock);
136,003✔
1965
    writer->ctx->tombBlockIdx = 0;
136,003✔
1966
  }
1967

1968
_exit:
139,753✔
1969
  if (code) {
139,753✔
1970
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1971
              tstrerror(code));
1972
  }
1973
  return code;
139,753✔
1974
}
1975

1976
int32_t tsdbDataFileWriteTombRecord(SDataFileWriter *writer, const STombRecord *record) {
403,663✔
1977
  int32_t code = 0;
403,663✔
1978
  int32_t lino = 0;
403,663✔
1979

1980
  if (!writer->ctx->opened) {
403,663✔
1981
    TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpen(writer), &lino, _exit);
138,062✔
1982
  }
1983

1984
  if (writer->fd[TSDB_FTYPE_TOMB] == NULL) {
403,663✔
1985
    TAOS_CHECK_GOTO(tsdbDataFileWriterOpenTombFD(writer), &lino, _exit);
139,753✔
1986
  }
1987

1988
  TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombRecord(writer, record), &lino, _exit);
403,663✔
1989

1990
_exit:
403,663✔
1991
  if (code) {
403,663✔
1992
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1993
              tstrerror(code));
1994
  }
1995
  return code;
403,663✔
1996
}
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