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

taosdata / TDengine / #3599

08 Feb 2025 11:23AM UTC coverage: 1.77% (-61.6%) from 63.396%
#3599

push

travis-ci

web-flow
Merge pull request #29712 from taosdata/fix/TD-33652-3.0

fix: reduce write rows from 30w to 3w

3776 of 278949 branches covered (1.35%)

Branch coverage included in aggregate %.

6012 of 274147 relevant lines covered (2.19%)

1642.73 hits per line

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

0.0
/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 "meta.h"
17
#include "tsdbDataFileRW.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) {
×
42
  if (reader->ctx->headFooterLoaded) {
×
43
    return 0;
×
44
  }
45

46
  int32_t code = 0;
×
47
  int32_t lino = 0;
×
48

49
  int32_t ftype = TSDB_FTYPE_HEAD;
×
50
  if (reader->fd[ftype]) {
×
51
    int32_t encryptAlgorithm = reader->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
×
52
    char   *encryptKey = reader->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
×
53
#if 1
54
    TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[ftype], reader->config->files[ftype].file.size - sizeof(SHeadFooter),
×
55
                                 (uint8_t *)reader->headFooter, sizeof(SHeadFooter), 0, encryptAlgorithm, encryptKey),
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;
×
74

75
_exit:
×
76
  if (code) {
×
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;
×
81
}
82

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

88
  int32_t code = 0;
×
89
  int32_t lino = 0;
×
90

91
  int32_t ftype = TSDB_FTYPE_TOMB;
×
92
  if (reader->fd[ftype]) {
×
93
    int32_t encryptAlgorithm = reader->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
×
94
    char   *encryptKey = reader->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
×
95
    TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[ftype], reader->config->files[ftype].file.size - sizeof(STombFooter),
×
96
                                 (uint8_t *)reader->tombFooter, sizeof(STombFooter), 0, encryptAlgorithm, encryptKey),
97
                    &lino, _exit);
98
  }
99
  reader->ctx->tombFooterLoaded = true;
×
100

101
_exit:
×
102
  if (code) {
×
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;
×
107
}
108

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

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

117
  for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->local); i++) {
×
118
    tBufferInit(reader[0]->local + i);
×
119
  }
120

121
  reader[0]->config[0] = config[0];
×
122
  reader[0]->buffers = config->buffers;
×
123
  if (reader[0]->buffers == NULL) {
×
124
    reader[0]->buffers = reader[0]->local;
×
125
  }
126

127
  if (fname) {
×
128
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
×
129
      if (fname[i]) {
×
130
        int32_t lcn = config->files[i].file.lcn;
×
131
        TAOS_CHECK_GOTO(tsdbOpenFile(fname[i], config->tsdb, TD_FILE_READ, &reader[0]->fd[i], lcn), &lino, _exit);
×
132
      }
133
    }
134
  } else {
135
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
×
136
      if (config->files[i].exist) {
×
137
        char fname1[TSDB_FILENAME_LEN];
138
        tsdbTFileName(config->tsdb, &config->files[i].file, fname1);
×
139
        int32_t lcn = config->files[i].file.lcn;
×
140
        TAOS_CHECK_GOTO(tsdbOpenFile(fname1, config->tsdb, TD_FILE_READ, &reader[0]->fd[i], lcn), &lino, _exit);
×
141
      }
142
    }
143
  }
144

145
_exit:
×
146
  if (code) {
×
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;
×
151
}
152

153
void tsdbDataFileReaderClose(SDataFileReader **reader) {
×
154
  if (reader[0] == NULL) {
×
155
    return;
×
156
  }
157

158
  TARRAY2_DESTROY(reader[0]->tombBlkArray, NULL);
×
159
  TARRAY2_DESTROY(reader[0]->brinBlkArray, NULL);
×
160

161
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
×
162
    if (reader[0]->fd[i]) {
×
163
      tsdbCloseFile(&reader[0]->fd[i]);
×
164
    }
165
  }
166

167
  for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->local); ++i) {
×
168
    tBufferDestroy(reader[0]->local + i);
×
169
  }
170

171
  taosMemoryFree(reader[0]);
×
172
  reader[0] = NULL;
×
173
}
174

175
int32_t tsdbDataFileReadBrinBlk(SDataFileReader *reader, const TBrinBlkArray **brinBlkArray) {
×
176
  int32_t code = 0;
×
177
  int32_t lino = 0;
×
178
  void   *data = NULL;
×
179

180
  if (!reader->ctx->brinBlkLoaded) {
×
181
    TAOS_CHECK_GOTO(tsdbDataFileReadHeadFooter(reader), &lino, _exit);
×
182

183
    if (reader->headFooter->brinBlkPtr->size > 0) {
×
184
      data = taosMemoryMalloc(reader->headFooter->brinBlkPtr->size);
×
185
      if (data == NULL) {
×
186
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
187
      }
188

189
      int32_t encryptAlgorithm = reader->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
×
190
      char   *encryptKey = reader->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
×
191

192
      TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[TSDB_FTYPE_HEAD], reader->headFooter->brinBlkPtr->offset, data,
×
193
                                   reader->headFooter->brinBlkPtr->size, 0, encryptAlgorithm, encryptKey),
194
                      &lino, _exit);
195

196
      int32_t size = reader->headFooter->brinBlkPtr->size / sizeof(SBrinBlk);
×
197
      TARRAY2_INIT_EX(reader->brinBlkArray, size, size, data);
×
198
    } else {
199
      TARRAY2_INIT(reader->brinBlkArray);
×
200
    }
201

202
    reader->ctx->brinBlkLoaded = true;
×
203
  }
204
  brinBlkArray[0] = reader->brinBlkArray;
×
205

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

215
int32_t tsdbDataFileReadBrinBlock(SDataFileReader *reader, const SBrinBlk *brinBlk, SBrinBlock *brinBlock) {
×
216
  int32_t code = 0;
×
217
  int32_t lino = 0;
×
218

219
  SBuffer *buffer = reader->buffers + 0;
×
220
  SBuffer *assist = reader->buffers + 1;
×
221

222
  int32_t encryptAlgorithm = reader->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
×
223
  char   *encryptKey = reader->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
×
224
  // load data
225
  tBufferClear(buffer);
226
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_HEAD], brinBlk->dp->offset, brinBlk->dp->size, buffer, 0,
×
227
                                       encryptAlgorithm, encryptKey),
228
                  &lino, _exit);
229

230
  // decode brin block
231
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer);
×
232
  tBrinBlockClear(brinBlock);
×
233
  brinBlock->numOfPKs = brinBlk->numOfPKs;
×
234
  brinBlock->numOfRecords = brinBlk->numRec;
×
235
  for (int32_t i = 0; i < 10; i++) {  // int64_t
×
236

237
    SCompressInfo cinfo = {
×
238
        .cmprAlg = brinBlk->cmprAlg,
×
239
        .dataType = TSDB_DATA_TYPE_BIGINT,
240
        .compressedSize = brinBlk->size[i],
×
241
        .originalSize = brinBlk->numRec * sizeof(int64_t),
×
242
    };
243
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, brinBlock->buffers + i, assist), &lino, _exit);
×
244
    br.offset += brinBlk->size[i];
×
245
  }
246

247
  for (int32_t i = 10; i < 15; i++) {  // int32_t
×
248
    SCompressInfo cinfo = {
×
249
        .cmprAlg = brinBlk->cmprAlg,
×
250
        .dataType = TSDB_DATA_TYPE_INT,
251
        .compressedSize = brinBlk->size[i],
×
252
        .originalSize = brinBlk->numRec * sizeof(int32_t),
×
253
    };
254
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, brinBlock->buffers + i, assist), &lino, _exit);
×
255
    br.offset += brinBlk->size[i];
×
256
  }
257

258
  // primary keys
259
  if (brinBlk->numOfPKs > 0) {  // decode the primary keys
×
260
    SValueColumnCompressInfo firstInfos[TD_MAX_PK_COLS];
261
    SValueColumnCompressInfo lastInfos[TD_MAX_PK_COLS];
262

263
    for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
×
264
      TAOS_CHECK_GOTO(tValueColumnCompressInfoDecode(&br, firstInfos + i), &lino, _exit);
×
265
    }
266
    for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
×
267
      TAOS_CHECK_GOTO(tValueColumnCompressInfoDecode(&br, lastInfos + i), &lino, _exit);
×
268
    }
269

270
    for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
×
271
      SValueColumnCompressInfo *info = firstInfos + i;
×
272

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

277
    for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
×
278
      SValueColumnCompressInfo *info = lastInfos + i;
×
279

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

285
  if (br.offset != br.buffer->size) {
×
286
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
287
  }
288

289
_exit:
×
290
  if (code) {
×
291
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
292
              tstrerror(code));
293
  }
294
  return code;
×
295
}
296

297
extern int32_t tBlockDataDecompress(SBufferReader *br, SBlockData *blockData, SBuffer *assist);
298

299
int32_t tsdbDataFileReadBlockData(SDataFileReader *reader, const SBrinRecord *record, SBlockData *bData) {
×
300
  int32_t code = 0;
×
301
  int32_t lino = 0;
×
302
  int32_t fid = reader->config->files[TSDB_FTYPE_DATA].file.fid;
×
303

304
  SBuffer *buffer = reader->buffers + 0;
×
305
  SBuffer *assist = reader->buffers + 1;
×
306

307
  int32_t encryptAlgorithm = reader->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
×
308
  char   *encryptKey = reader->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
×
309
  // load data
310
  tBufferClear(buffer);
311
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA], record->blockOffset, record->blockSize, buffer, 0,
×
312
                                       encryptAlgorithm, encryptKey),
313
                  &lino, _exit);
314

315
  // decompress
316
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer);
×
317
  TAOS_CHECK_GOTO(tBlockDataDecompress(&br, bData, assist), &lino, _exit);
×
318

319
  if (br.offset != buffer->size) {
×
320
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
321
  }
322

323
_exit:
×
324
  if (code) {
×
325
    tsdbError("vgId:%d %s fid %d failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, fid,
×
326
              __FILE__, lino, tstrerror(code));
327
  }
328
  return code;
×
329
}
330

331
int32_t tsdbDataFileReadBlockDataByColumn(SDataFileReader *reader, const SBrinRecord *record, SBlockData *bData,
×
332
                                          STSchema *pTSchema, int16_t cids[], int32_t ncid) {
333
  int32_t code = 0;
×
334
  int32_t lino = 0;
×
335
  int32_t fid = reader->config->files[TSDB_FTYPE_DATA].file.fid;
×
336

337
  SDiskDataHdr hdr;
338
  SBuffer     *buffer0 = reader->buffers + 0;
×
339
  SBuffer     *buffer1 = reader->buffers + 1;
×
340
  SBuffer     *assist = reader->buffers + 2;
×
341

342
  int32_t encryptAlgorithm = reader->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
×
343
  char   *encryptKey = reader->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
×
344
  // load key part
345
  tBufferClear(buffer0);
346
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA], record->blockOffset, record->blockKeySize, buffer0,
×
347
                                       0, encryptAlgorithm, encryptKey),
348
                  &lino, _exit);
349

350
  // SDiskDataHdr
351
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
×
352
  TAOS_CHECK_GOTO(tGetDiskDataHdr(&br, &hdr), &lino, _exit);
×
353

354
  if (hdr.delimiter != TSDB_FILE_DLMT) {
×
355
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
356
  }
357

358
  tBlockDataReset(bData);
×
359
  bData->suid = hdr.suid;
×
360
  bData->uid = hdr.uid;
×
361
  bData->nRow = hdr.nRow;
×
362

363
  // Key part
364
  TAOS_CHECK_GOTO(tBlockDataDecompressKeyPart(&hdr, &br, bData, assist), &lino, _exit);
×
365
  if (br.offset != buffer0->size) {
×
366
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
367
  }
368

369
  int extraColIdx = -1;
×
370
  for (int i = 0; i < ncid; i++) {
×
371
    if (tBlockDataGetColData(bData, cids[i]) == NULL) {
×
372
      extraColIdx = i;
×
373
      break;
×
374
    }
375
  }
376

377
  if (extraColIdx < 0) {
×
378
    goto _exit;
×
379
  }
380

381
  // load SBlockCol part
382
  tBufferClear(buffer0);
383
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA], record->blockOffset + record->blockKeySize,
×
384
                                       hdr.szBlkCol, buffer0, 0, encryptAlgorithm, encryptKey),
385
                  &lino, _exit);
386

387
  // calc szHint
388
  int64_t szHint = 0;
×
389
  int     extraCols = 1;
×
390
  for (int i = extraColIdx + 1; i < ncid; ++i) {
×
391
    if (tBlockDataGetColData(bData, cids[i]) == NULL) {
×
392
      ++extraCols;
×
393
      break;
×
394
    }
395
  }
396

397
  if (extraCols >= 2) {
×
398
    br = BUFFER_READER_INITIALIZER(0, buffer0);
×
399

400
    SBlockCol blockCol = {.cid = 0};
×
401
    for (int32_t i = extraColIdx; i < ncid; ++i) {
×
402
      int16_t extraColCid = cids[i];
×
403

404
      while (extraColCid > blockCol.cid) {
×
405
        if (br.offset >= buffer0->size) {
×
406
          blockCol.cid = INT16_MAX;
×
407
          break;
×
408
        }
409

410
        TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
×
411
      }
412

413
      if (extraColCid == blockCol.cid || blockCol.cid == INT16_MAX) {
×
414
        extraColIdx = i;
×
415
        break;
×
416
      }
417
    }
418

419
    if (blockCol.cid > 0 && blockCol.cid < INT16_MAX /*&& blockCol->flag == HAS_VALUE*/) {
×
420
      int64_t   offset = blockCol.offset;
×
421
      SBlockCol lastNonNoneBlockCol = {.cid = 0};
×
422

423
      for (int32_t i = extraColIdx; i < ncid; ++i) {
×
424
        int16_t extraColCid = cids[i];
×
425

426
        while (extraColCid > blockCol.cid) {
×
427
          if (br.offset >= buffer0->size) {
×
428
            blockCol.cid = INT16_MAX;
×
429
            break;
×
430
          }
431

432
          TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
×
433
        }
434

435
        if (extraColCid == blockCol.cid) {
×
436
          lastNonNoneBlockCol = blockCol;
×
437
          continue;
×
438
        }
439

440
        if (blockCol.cid == INT16_MAX) {
×
441
          break;
×
442
        }
443
      }
444

445
      if (lastNonNoneBlockCol.cid > 0) {
×
446
        szHint = lastNonNoneBlockCol.offset + lastNonNoneBlockCol.szBitmap + lastNonNoneBlockCol.szOffset +
×
447
                 lastNonNoneBlockCol.szValue - offset;
×
448
      }
449
    }
450
  }
451

452
  // load each column
453
  SBlockCol blockCol = {
×
454
      .cid = 0,
455
  };
456
  bool firstRead = true;
×
457
  br = BUFFER_READER_INITIALIZER(0, buffer0);
×
458
  for (int32_t i = 0; i < ncid; i++) {
×
459
    int16_t cid = cids[i];
×
460

461
    if (tBlockDataGetColData(bData, cid)) {  // already loaded
×
462
      continue;
×
463
    }
464

465
    while (cid > blockCol.cid) {
×
466
      if (br.offset >= buffer0->size) {
×
467
        blockCol.cid = INT16_MAX;
×
468
        break;
×
469
      }
470

471
      TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
×
472
    }
473

474
    if (cid < blockCol.cid) {
×
475
      const STColumn *tcol = tTSchemaSearchColumn(pTSchema, cid);
×
476
      TSDB_CHECK_NULL(tcol, code, lino, _exit, TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER);
×
477
      SBlockCol none = {
×
478
          .cid = cid,
479
          .type = tcol->type,
×
480
          .cflag = tcol->flags,
×
481
          .flag = HAS_NONE,
482
          .szOrigin = 0,
483
          .szBitmap = 0,
484
          .szOffset = 0,
485
          .szValue = 0,
486
          .offset = 0,
487
      };
488
      TAOS_CHECK_GOTO(tBlockDataDecompressColData(&hdr, &none, &br, bData, assist), &lino, _exit);
×
489
    } else if (cid == blockCol.cid) {
×
490
      int32_t encryptAlgorithm = reader->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
×
491
      char   *encryptKey = reader->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
×
492
      // load from file
493
      tBufferClear(buffer1);
494
      TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA],
×
495
                                           record->blockOffset + record->blockKeySize + hdr.szBlkCol + blockCol.offset,
496
                                           blockCol.szBitmap + blockCol.szOffset + blockCol.szValue, buffer1,
497
                                           firstRead ? szHint : 0, encryptAlgorithm, encryptKey),
498
                      &lino, _exit);
499

500
      firstRead = false;
×
501

502
      // decode the buffer
503
      SBufferReader br1 = BUFFER_READER_INITIALIZER(0, buffer1);
×
504
      TAOS_CHECK_GOTO(tBlockDataDecompressColData(&hdr, &blockCol, &br1, bData, assist), &lino, _exit);
×
505
    }
506
  }
507

508
_exit:
×
509
  if (code) {
×
510
    tsdbError("vgId:%d %s fid:%d failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, fid,
×
511
              __FILE__, lino, tstrerror(code));
512
  }
513
  return code;
×
514
}
515

516
int32_t tsdbDataFileReadBlockSma(SDataFileReader *reader, const SBrinRecord *record,
×
517
                                 TColumnDataAggArray *columnDataAggArray) {
518
  int32_t  code = 0;
×
519
  int32_t  lino = 0;
×
520
  SBuffer *buffer = reader->buffers + 0;
×
521

522
  TARRAY2_CLEAR(columnDataAggArray, NULL);
×
523
  if (record->smaSize > 0) {
×
524
    tBufferClear(buffer);
525
    int32_t encryptAlgorithm = reader->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
×
526
    char   *encryptKey = reader->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
×
527
    TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_SMA], record->smaOffset, record->smaSize, buffer, 0,
×
528
                                         encryptAlgorithm, encryptKey),
529
                    &lino, _exit);
530

531
    // decode sma data
532
    SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer);
×
533
    while (br.offset < record->smaSize) {
×
534
      SColumnDataAgg sma[1];
535

536
      TAOS_CHECK_GOTO(tGetColumnDataAgg(&br, sma), &lino, _exit);
×
537
      TAOS_CHECK_GOTO(TARRAY2_APPEND_PTR(columnDataAggArray, sma), &lino, _exit);
×
538
    }
539
    if (br.offset != record->smaSize) {
×
540
      TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
541
    }
542
  }
543

544
_exit:
×
545
  if (code) {
×
546
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
547
              tstrerror(code));
548
  }
549
  return code;
×
550
}
551

552
int32_t tsdbDataFileReadTombBlk(SDataFileReader *reader, const TTombBlkArray **tombBlkArray) {
×
553
  int32_t code = 0;
×
554
  int32_t lino = 0;
×
555
  void   *data = NULL;
×
556

557
  if (!reader->ctx->tombBlkLoaded) {
×
558
    TAOS_CHECK_GOTO(tsdbDataFileReadTombFooter(reader), &lino, _exit);
×
559

560
    if (reader->tombFooter->tombBlkPtr->size > 0) {
×
561
      if ((data = taosMemoryMalloc(reader->tombFooter->tombBlkPtr->size)) == NULL) {
×
562
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
563
      }
564

565
      int32_t encryptAlgorithm = reader->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
×
566
      char   *encryptKey = reader->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
×
567
      TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[TSDB_FTYPE_TOMB], reader->tombFooter->tombBlkPtr->offset, data,
×
568
                                   reader->tombFooter->tombBlkPtr->size, 0, encryptAlgorithm, encryptKey),
569
                      &lino, _exit);
570

571
      int32_t size = reader->tombFooter->tombBlkPtr->size / sizeof(STombBlk);
×
572
      TARRAY2_INIT_EX(reader->tombBlkArray, size, size, data);
×
573
    } else {
574
      TARRAY2_INIT(reader->tombBlkArray);
×
575
    }
576

577
    reader->ctx->tombBlkLoaded = true;
×
578
  }
579
  tombBlkArray[0] = reader->tombBlkArray;
×
580

581
_exit:
×
582
  if (code) {
×
583
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
584
              tstrerror(code));
585
    taosMemoryFree(data);
×
586
  }
587
  return code;
×
588
}
589

590
int32_t tsdbDataFileReadTombBlock(SDataFileReader *reader, const STombBlk *tombBlk, STombBlock *tData) {
×
591
  int32_t code = 0;
×
592
  int32_t lino = 0;
×
593

594
  SBuffer *buffer0 = reader->buffers + 0;
×
595
  SBuffer *assist = reader->buffers + 1;
×
596

597
  tBufferClear(buffer0);
598
  int32_t encryptAlgorithm = reader->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
×
599
  char   *encryptKey = reader->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
×
600
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_TOMB], tombBlk->dp->offset, tombBlk->dp->size, buffer0, 0,
×
601
                                       encryptAlgorithm, encryptKey),
602
                  &lino, _exit);
603

604
  int32_t       size = 0;
×
605
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
×
606
  tTombBlockClear(tData);
×
607
  tData->numOfRecords = tombBlk->numRec;
×
608
  for (int32_t i = 0; i < ARRAY_SIZE(tData->buffers); ++i) {
×
609
    SCompressInfo cinfo = {
×
610
        .cmprAlg = tombBlk->cmprAlg,
×
611
        .dataType = TSDB_DATA_TYPE_BIGINT,
612
        .originalSize = tombBlk->numRec * sizeof(int64_t),
×
613
        .compressedSize = tombBlk->size[i],
×
614
    };
615
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, tData->buffers + i, assist), &lino, _exit);
×
616
    br.offset += tombBlk->size[i];
×
617
  }
618

619
_exit:
×
620
  if (code) {
×
621
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
622
              tstrerror(code));
623
  }
624
  return code;
×
625
}
626

627
// SDataFileWriter =============================================
628
struct SDataFileWriter {
629
  SDataFileWriterConfig config[1];
630

631
  SSkmInfo skmTb[1];
632
  SSkmInfo skmRow[1];
633
  SBuffer  local[10];
634
  SBuffer *buffers;
635

636
  struct {
637
    bool             opened;
638
    SDataFileReader *reader;
639

640
    // for ts data
641
    TABLEID tbid[1];
642
    bool    tbHasOldData;
643

644
    const TBrinBlkArray *brinBlkArray;
645
    int32_t              brinBlkArrayIdx;
646
    SBrinBlock           brinBlock[1];
647
    int32_t              brinBlockIdx;
648
    SBlockData           blockData[1];
649
    int32_t              blockDataIdx;
650
    // for tomb data
651
    bool                 hasOldTomb;
652
    const TTombBlkArray *tombBlkArray;
653
    int32_t              tombBlkArrayIdx;
654
    STombBlock           tombBlock[1];
655
    int32_t              tombBlockIdx;
656
    // range
657
    SVersionRange range;
658
    SVersionRange tombRange;
659
  } ctx[1];
660

661
  STFile   files[TSDB_FTYPE_MAX];
662
  STsdbFD *fd[TSDB_FTYPE_MAX];
663

664
  SHeadFooter headFooter[1];
665
  STombFooter tombFooter[1];
666

667
  TBrinBlkArray brinBlkArray[1];
668
  SBrinBlock    brinBlock[1];
669
  SBlockData    blockData[1];
670

671
  TTombBlkArray tombBlkArray[1];
672
  STombBlock    tombBlock[1];
673
};
674

675
static int32_t tsdbDataFileWriterCloseAbort(SDataFileWriter *writer) {
×
676
  tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, __LINE__,
×
677
            "not implemented");
678
  return 0;
×
679
}
680

681
static void tsdbDataFileWriterDoClose(SDataFileWriter *writer) {
×
682
  if (writer->ctx->reader) {
×
683
    tsdbDataFileReaderClose(&writer->ctx->reader);
×
684
  }
685

686
  tTombBlockDestroy(writer->tombBlock);
×
687
  TARRAY2_DESTROY(writer->tombBlkArray, NULL);
×
688
  tBlockDataDestroy(writer->blockData);
×
689
  tBrinBlockDestroy(writer->brinBlock);
×
690
  TARRAY2_DESTROY(writer->brinBlkArray, NULL);
×
691

692
  tTombBlockDestroy(writer->ctx->tombBlock);
×
693
  tBlockDataDestroy(writer->ctx->blockData);
×
694
  tBrinBlockDestroy(writer->ctx->brinBlock);
×
695

696
  for (int32_t i = 0; i < ARRAY_SIZE(writer->local); ++i) {
×
697
    tBufferDestroy(writer->local + i);
×
698
  }
699

700
  tDestroyTSchema(writer->skmRow->pTSchema);
×
701
  tDestroyTSchema(writer->skmTb->pTSchema);
×
702
}
×
703

704
static int32_t tsdbDataFileWriterDoOpenReader(SDataFileWriter *writer) {
×
705
  int32_t code = 0;
×
706
  int32_t lino = 0;
×
707

708
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
×
709
    if (writer->config->files[i].exist) {
×
710
      SDataFileReaderConfig config[1] = {{
×
711
          .tsdb = writer->config->tsdb,
×
712
          .szPage = writer->config->szPage,
×
713
          .buffers = writer->buffers,
×
714
      }};
715

716
      for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
×
717
        config->files[i].exist = writer->config->files[i].exist;
×
718
        if (config->files[i].exist) {
×
719
          config->files[i].file = writer->config->files[i].file;
×
720
        }
721
      }
722

723
      TAOS_CHECK_GOTO(tsdbDataFileReaderOpen(NULL, config, &writer->ctx->reader), &lino, _exit);
×
724
      break;
×
725
    }
726
  }
727

728
_exit:
×
729
  if (code) {
×
730
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
731
              tstrerror(code));
732
  }
733
  return code;
×
734
}
735

736
static int32_t tsdbDataFileWriterDoOpen(SDataFileWriter *writer) {
×
737
  int32_t code = 0;
×
738
  int32_t lino = 0;
×
739
  int32_t ftype;
740

741
  if (!writer->config->skmTb) writer->config->skmTb = writer->skmTb;
×
742
  if (!writer->config->skmRow) writer->config->skmRow = writer->skmRow;
×
743
  writer->buffers = writer->config->buffers;
×
744
  if (writer->buffers == NULL) {
×
745
    writer->buffers = writer->local;
×
746
  }
747

748
  // open reader
749
  TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpenReader(writer), &lino, _exit);
×
750

751
  // .head
752
  ftype = TSDB_FTYPE_HEAD;
×
753
  writer->files[ftype] = (STFile){
×
754
      .type = ftype,
755
      .did = writer->config->did,
×
756
      .fid = writer->config->fid,
×
757
      .cid = writer->config->cid,
×
758
      .size = 0,
759
      .minVer = VERSION_MAX,
760
      .maxVer = VERSION_MIN,
761
  };
762

763
  // .data
764
  ftype = TSDB_FTYPE_DATA;
×
765
  if (writer->config->files[ftype].exist) {
×
766
    writer->files[ftype] = writer->config->files[ftype].file;
×
767
  } else {
768
    writer->files[ftype] = (STFile){
×
769
        .type = ftype,
770
        .did = writer->config->did,
×
771
        .fid = writer->config->fid,
×
772
        .cid = writer->config->cid,
×
773
        .size = 0,
774
        .lcn = writer->config->lcn == -1 ? 0 : -1,
×
775
        .minVer = VERSION_MAX,
776
        .maxVer = VERSION_MIN,
777
    };
778
  }
779

780
  // .sma
781
  ftype = TSDB_FTYPE_SMA;
×
782
  if (writer->config->files[ftype].exist) {
×
783
    writer->files[ftype] = writer->config->files[ftype].file;
×
784
  } else {
785
    writer->files[ftype] = (STFile){
×
786
        .type = ftype,
787
        .did = writer->config->did,
×
788
        .fid = writer->config->fid,
×
789
        .cid = writer->config->cid,
×
790
        .size = 0,
791
        .minVer = VERSION_MAX,
792
        .maxVer = VERSION_MIN,
793
    };
794
  }
795

796
  // .tomb
797
  ftype = TSDB_FTYPE_TOMB;
×
798
  writer->files[ftype] = (STFile){
×
799
      .type = ftype,
800
      .did = writer->config->did,
×
801
      .fid = writer->config->fid,
×
802
      .cid = writer->config->cid,
×
803
      .size = 0,
804
      .minVer = VERSION_MAX,
805
      .maxVer = VERSION_MIN,
806
  };
807

808
  // range
809
  writer->ctx->range = (SVersionRange){.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
×
810
  writer->ctx->tombRange = (SVersionRange){.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
×
811

812
  writer->ctx->opened = true;
×
813

814
_exit:
×
815
  if (code) {
×
816
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
817
              tstrerror(code));
818
  }
819
  return code;
×
820
}
821

822
void tsdbWriterUpdVerRange(SVersionRange *range, int64_t minVer, int64_t maxVer) {
×
823
  range->minVer = TMIN(range->minVer, minVer);
×
824
  range->maxVer = TMAX(range->maxVer, maxVer);
×
825
}
×
826

827
int32_t tsdbFileWriteBrinBlock(STsdbFD *fd, SBrinBlock *brinBlock, uint32_t cmprAlg, int64_t *fileSize,
×
828
                               TBrinBlkArray *brinBlkArray, SBuffer *buffers, SVersionRange *range,
829
                               int32_t encryptAlgorithm, char *encryptKey) {
830
  if (brinBlock->numOfRecords == 0) {
×
831
    return 0;
×
832
  }
833

834
  int32_t  code;
835
  SBuffer *buffer0 = buffers + 0;
×
836
  SBuffer *buffer1 = buffers + 1;
×
837
  SBuffer *assist = buffers + 2;
×
838

839
  SBrinBlk brinBlk = {
×
840
      .dp[0] =
841
          {
842
              .offset = *fileSize,
×
843
              .size = 0,
844
          },
845
      .numRec = brinBlock->numOfRecords,
×
846
      .numOfPKs = brinBlock->numOfPKs,
×
847
      .cmprAlg = cmprAlg,
848
  };
849
  for (int i = 0; i < brinBlock->numOfRecords; i++) {
×
850
    SBrinRecord record;
851

852
    TAOS_CHECK_RETURN(tBrinBlockGet(brinBlock, i, &record));
×
853
    if (i == 0) {
×
854
      brinBlk.minTbid.suid = record.suid;
×
855
      brinBlk.minTbid.uid = record.uid;
×
856
      brinBlk.minVer = record.minVer;
×
857
      brinBlk.maxVer = record.maxVer;
×
858
    }
859
    if (i == brinBlock->numOfRecords - 1) {
×
860
      brinBlk.maxTbid.suid = record.suid;
×
861
      brinBlk.maxTbid.uid = record.uid;
×
862
    }
863
    if (record.minVer < brinBlk.minVer) {
×
864
      brinBlk.minVer = record.minVer;
×
865
    }
866
    if (record.maxVer > brinBlk.maxVer) {
×
867
      brinBlk.maxVer = record.maxVer;
×
868
    }
869
  }
870

871
  tsdbWriterUpdVerRange(range, brinBlk.minVer, brinBlk.maxVer);
×
872

873
  // write to file
874
  for (int32_t i = 0; i < 10; ++i) {
×
875
    SCompressInfo info = {
×
876
        .cmprAlg = cmprAlg,
877
        .dataType = TSDB_DATA_TYPE_BIGINT,
878
        .originalSize = brinBlock->buffers[i].size,
×
879
    };
880

881
    tBufferClear(buffer0);
882
    TAOS_CHECK_RETURN(tCompressDataToBuffer(brinBlock->buffers[i].data, &info, buffer0, assist));
×
883
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptAlgorithm, encryptKey));
×
884
    brinBlk.size[i] = info.compressedSize;
×
885
    brinBlk.dp->size += info.compressedSize;
×
886
    *fileSize += info.compressedSize;
×
887
  }
888
  for (int32_t i = 10; i < 15; ++i) {
×
889
    SCompressInfo info = {
×
890
        .cmprAlg = cmprAlg,
891
        .dataType = TSDB_DATA_TYPE_INT,
892
        .originalSize = brinBlock->buffers[i].size,
×
893
    };
894

895
    tBufferClear(buffer0);
896
    TAOS_CHECK_RETURN(tCompressDataToBuffer(brinBlock->buffers[i].data, &info, buffer0, assist));
×
897
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptAlgorithm, encryptKey));
×
898
    brinBlk.size[i] = info.compressedSize;
×
899
    brinBlk.dp->size += info.compressedSize;
×
900
    *fileSize += info.compressedSize;
×
901
  }
902

903
  // write primary keys to file
904
  if (brinBlock->numOfPKs > 0) {
×
905
    tBufferClear(buffer0);
906
    tBufferClear(buffer1);
907

908
    // encode
909
    for (int i = 0; i < brinBlock->numOfPKs; i++) {
×
910
      SValueColumnCompressInfo info = {.cmprAlg = cmprAlg};
×
911
      TAOS_CHECK_RETURN(tValueColumnCompress(&brinBlock->firstKeyPKs[i], &info, buffer1, assist));
×
912
      TAOS_CHECK_RETURN(tValueColumnCompressInfoEncode(&info, buffer0));
×
913
    }
914
    for (int i = 0; i < brinBlock->numOfPKs; i++) {
×
915
      SValueColumnCompressInfo info = {.cmprAlg = cmprAlg};
×
916
      TAOS_CHECK_RETURN(tValueColumnCompress(&brinBlock->lastKeyPKs[i], &info, buffer1, assist));
×
917
      TAOS_CHECK_RETURN(tValueColumnCompressInfoEncode(&info, buffer0));
×
918
    }
919

920
    // write to file
921
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptAlgorithm, encryptKey));
×
922
    *fileSize += buffer0->size;
×
923
    brinBlk.dp->size += buffer0->size;
×
924
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer1->data, buffer1->size, encryptAlgorithm, encryptKey));
×
925
    *fileSize += buffer1->size;
×
926
    brinBlk.dp->size += buffer1->size;
×
927
  }
928

929
  // append to brinBlkArray
930
  TAOS_CHECK_RETURN(TARRAY2_APPEND_PTR(brinBlkArray, &brinBlk));
×
931

932
  tBrinBlockClear(brinBlock);
×
933

934
  return 0;
×
935
}
936

937
static int32_t tsdbDataFileWriteBrinBlock(SDataFileWriter *writer) {
×
938
  if (writer->brinBlock->numOfRecords == 0) {
×
939
    return 0;
×
940
  }
941

942
  int32_t code = 0;
×
943
  int32_t lino = 0;
×
944

945
  int32_t encryptAlgorithm = writer->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
×
946
  char   *encryptKey = writer->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
×
947

948
  TAOS_CHECK_GOTO(tsdbFileWriteBrinBlock(writer->fd[TSDB_FTYPE_HEAD], writer->brinBlock, writer->config->cmprAlg,
×
949
                                         &writer->files[TSDB_FTYPE_HEAD].size, writer->brinBlkArray, writer->buffers,
950
                                         &writer->ctx->range, encryptAlgorithm, encryptKey),
951
                  &lino, _exit);
952

953
_exit:
×
954
  if (code) {
×
955
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
956
              tstrerror(code));
957
  }
958
  return code;
×
959
}
960

961
static int32_t tsdbDataFileWriteBrinRecord(SDataFileWriter *writer, const SBrinRecord *record) {
×
962
  int32_t code = 0;
×
963
  int32_t lino = 0;
×
964

965
  for (;;) {
966
    code = tBrinBlockPut(writer->brinBlock, record);
×
967
    if (code == TSDB_CODE_INVALID_PARA) {
×
968
      // different records with different primary keys
969
      TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlock(writer), &lino, _exit);
×
970
      continue;
×
971
    } else {
972
      TSDB_CHECK_CODE(code, lino, _exit);
×
973
    }
974
    break;
×
975
  }
976

977
  if ((writer->brinBlock->numOfRecords) >= 256) {
×
978
    TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlock(writer), &lino, _exit);
×
979
  }
980

981
_exit:
×
982
  if (code) {
×
983
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
984
              tstrerror(code));
985
  }
986
  return code;
×
987
}
988

989
static int32_t tsdbDataFileDoWriteBlockData(SDataFileWriter *writer, SBlockData *bData) {
×
990
  if (bData->nRow == 0) {
×
991
    return 0;
×
992
  }
993

994
  if (!bData->uid) {
×
995
    return TSDB_CODE_INVALID_PARA;
×
996
  }
997

998
  int32_t  code = 0;
×
999
  int32_t  lino = 0;
×
1000
  SBuffer *buffers = writer->buffers;
×
1001
  SBuffer *assist = writer->buffers + 4;
×
1002

1003
  SColCompressInfo cmprInfo = {.pColCmpr = NULL, .defaultCmprAlg = writer->config->cmprAlg};
×
1004

1005
  SBrinRecord record[1] = {{
×
1006
      .suid = bData->suid,
×
1007
      .uid = bData->uid,
×
1008
      .minVer = bData->aVersion[0],
×
1009
      .maxVer = bData->aVersion[0],
×
1010
      .blockOffset = writer->files[TSDB_FTYPE_DATA].size,
×
1011
      .smaOffset = writer->files[TSDB_FTYPE_SMA].size,
×
1012
      .blockSize = 0,
1013
      .blockKeySize = 0,
1014
      .smaSize = 0,
1015
      .numRow = bData->nRow,
×
1016
      .count = 1,
1017
  }};
1018

1019
  tsdbRowGetKey(&tsdbRowFromBlockData(bData, 0), &record->firstKey);
×
1020
  tsdbRowGetKey(&tsdbRowFromBlockData(bData, bData->nRow - 1), &record->lastKey);
×
1021

1022
  for (int32_t i = 1; i < bData->nRow; ++i) {
×
1023
    if (tsdbRowCompareWithoutVersion(&tsdbRowFromBlockData(bData, i - 1), &tsdbRowFromBlockData(bData, i)) != 0) {
×
1024
      record->count++;
×
1025
    }
1026
    if (bData->aVersion[i] < record->minVer) {
×
1027
      record->minVer = bData->aVersion[i];
×
1028
    }
1029
    if (bData->aVersion[i] > record->maxVer) {
×
1030
      record->maxVer = bData->aVersion[i];
×
1031
    }
1032
  }
1033

1034
  tsdbWriterUpdVerRange(&writer->ctx->range, record->minVer, record->maxVer);
×
1035

1036
  code = metaGetColCmpr(writer->config->tsdb->pVnode->pMeta, bData->suid != 0 ? bData->suid : bData->uid,
×
1037
                        &cmprInfo.pColCmpr);
1038
  if (code) {
×
1039
    tsdbWarn("vgId:%d failed to get column compress algrithm", TD_VID(writer->config->tsdb->pVnode));
×
1040
  }
1041

1042
  TAOS_CHECK_GOTO(tBlockDataCompress(bData, &cmprInfo, buffers, assist), &lino, _exit);
×
1043

1044
  record->blockKeySize = buffers[0].size + buffers[1].size;
×
1045
  record->blockSize = record->blockKeySize + buffers[2].size + buffers[3].size;
×
1046

1047
  int32_t encryptAlgorithm = writer->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
×
1048
  char   *encryptKey = writer->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
×
1049
  for (int i = 0; i < 4; i++) {
×
1050
    TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd[TSDB_FTYPE_DATA], writer->files[TSDB_FTYPE_DATA].size, buffers[i].data,
×
1051
                                  buffers[i].size, encryptAlgorithm, encryptKey),
1052
                    &lino, _exit);
1053
    writer->files[TSDB_FTYPE_DATA].size += buffers[i].size;
×
1054
  }
1055

1056
  // to .sma file
1057
  tBufferClear(&buffers[0]);
1058
  for (int32_t i = 0; i < bData->nColData; ++i) {
×
1059
    SColData *colData = bData->aColData + i;
×
1060
    if ((colData->cflag & COL_SMA_ON) == 0 || ((colData->flag & HAS_VALUE) == 0)) continue;
×
1061

1062
    SColumnDataAgg sma[1] = {{.colId = colData->cid}};
×
1063
    tColDataCalcSMA[colData->type](colData, &sma->sum, &sma->max, &sma->min, &sma->numOfNull);
×
1064

1065
    TAOS_CHECK_GOTO(tPutColumnDataAgg(&buffers[0], sma), &lino, _exit);
×
1066
  }
1067
  record->smaSize = buffers[0].size;
×
1068

1069
  if (record->smaSize > 0) {
×
1070
    TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd[TSDB_FTYPE_SMA], record->smaOffset, buffers[0].data, record->smaSize,
×
1071
                                  encryptAlgorithm, encryptKey),
1072
                    &lino, _exit);
1073
    writer->files[TSDB_FTYPE_SMA].size += record->smaSize;
×
1074
  }
1075

1076
  // append SBrinRecord
1077
  TAOS_CHECK_GOTO(tsdbDataFileWriteBrinRecord(writer, record), &lino, _exit);
×
1078

1079
  tBlockDataClear(bData);
×
1080

1081
_exit:
×
1082
  if (code) {
×
1083
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1084
              tstrerror(code));
1085
  }
1086
  taosHashCleanup(cmprInfo.pColCmpr);
×
1087
  return code;
×
1088
}
1089

1090
static int32_t tsdbDataFileDoWriteTSRow(SDataFileWriter *writer, TSDBROW *row) {
×
1091
  int32_t code = 0;
×
1092
  int32_t lino = 0;
×
1093

1094
  // update/append
1095
  if (row->type == TSDBROW_ROW_FMT) {
×
1096
    TAOS_CHECK_GOTO(
×
1097
        tsdbUpdateSkmRow(writer->config->tsdb, writer->ctx->tbid, TSDBROW_SVERSION(row), writer->config->skmRow), &lino,
1098
        _exit);
1099
  }
1100

1101
  if (TSDBROW_VERSION(row) <= writer->config->compactVersion  //
×
1102
      && writer->blockData->nRow > 0                          //
×
1103
      &&
×
1104
      tsdbRowCompareWithoutVersion(row, &tsdbRowFromBlockData(writer->blockData, writer->blockData->nRow - 1)) == 0  //
×
1105
  ) {
1106
    TAOS_CHECK_GOTO(tBlockDataUpdateRow(writer->blockData, row, writer->config->skmRow->pTSchema), &lino, _exit);
×
1107
  } else {
1108
    if (writer->blockData->nRow >= writer->config->maxRow) {
×
1109
      TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, writer->blockData), &lino, _exit);
×
1110
    }
1111

1112
    TAOS_CHECK_GOTO(
×
1113
        tBlockDataAppendRow(writer->blockData, row, writer->config->skmRow->pTSchema, writer->ctx->tbid->uid), &lino,
1114
        _exit);
1115
  }
1116

1117
_exit:
×
1118
  if (code) {
×
1119
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1120
              tstrerror(code));
1121
  }
1122
  return code;
×
1123
}
1124

1125
static FORCE_INLINE int32_t tsdbRowKeyCmprNullAsLargest(const STsdbRowKey *key1, const STsdbRowKey *key2) {
1126
  if (key1 == NULL) {
×
1127
    return 1;
×
1128
  } else if (key2 == NULL) {
×
1129
    return -1;
×
1130
  } else {
1131
    return tsdbRowKeyCmpr(key1, key2);
×
1132
  }
1133
}
1134

1135
static int32_t tsdbDataFileDoWriteTableOldData(SDataFileWriter *writer, const STsdbRowKey *key) {
×
1136
  if (writer->ctx->tbHasOldData == false) {
×
1137
    return 0;
×
1138
  }
1139

1140
  int32_t     code = 0;
×
1141
  int32_t     lino = 0;
×
1142
  STsdbRowKey rowKey;
1143

1144
  for (;;) {
×
1145
    for (;;) {
1146
      // SBlockData
1147
      for (; writer->ctx->blockDataIdx < writer->ctx->blockData->nRow; writer->ctx->blockDataIdx++) {
×
1148
        TSDBROW row = tsdbRowFromBlockData(writer->ctx->blockData, writer->ctx->blockDataIdx);
×
1149

1150
        tsdbRowGetKey(&row, &rowKey);
×
1151
        if (tsdbRowKeyCmprNullAsLargest(&rowKey, key) < 0) {  // key <= rowKey
×
1152
          TAOS_CHECK_GOTO(tsdbDataFileDoWriteTSRow(writer, &row), &lino, _exit);
×
1153
        } else {
1154
          goto _exit;
×
1155
        }
1156
      }
1157

1158
      // SBrinBlock
1159
      if (writer->ctx->brinBlockIdx >= writer->ctx->brinBlock->numOfRecords) {
×
1160
        break;
×
1161
      }
1162

1163
      for (; writer->ctx->brinBlockIdx < writer->ctx->brinBlock->numOfRecords; writer->ctx->brinBlockIdx++) {
×
1164
        SBrinRecord record;
1165
        code = tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, &record);
×
1166
        TSDB_CHECK_CODE(code, lino, _exit);
×
1167
        if (record.uid != writer->ctx->tbid->uid) {
×
1168
          writer->ctx->tbHasOldData = false;
×
1169
          goto _exit;
×
1170
        }
1171

1172
        if (tsdbRowKeyCmprNullAsLargest(key, &record.firstKey) < 0) {  // key < record->firstKey
×
1173
          goto _exit;
×
1174
        } else {
1175
          SBrinRecord record[1];
1176
          code = tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, record);
×
1177
          TSDB_CHECK_CODE(code, lino, _exit);
×
1178
          if (tsdbRowKeyCmprNullAsLargest(key, &record->lastKey) > 0) {  // key > record->lastKey
×
1179
            if (writer->blockData->nRow > 0) {
×
1180
              TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, writer->blockData), &lino, _exit);
×
1181
            }
1182

1183
            TAOS_CHECK_GOTO(tsdbDataFileWriteBrinRecord(writer, record), &lino, _exit);
×
1184
          } else {
1185
            TAOS_CHECK_GOTO(tsdbDataFileReadBlockData(writer->ctx->reader, record, writer->ctx->blockData), &lino,
×
1186
                            _exit);
1187

1188
            writer->ctx->blockDataIdx = 0;
×
1189
            writer->ctx->brinBlockIdx++;
×
1190
            break;
×
1191
          }
1192
        }
1193
      }
1194
    }
1195

1196
    // SBrinBlk
1197
    if (writer->ctx->brinBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->brinBlkArray)) {
×
1198
      writer->ctx->brinBlkArray = NULL;
×
1199
      writer->ctx->tbHasOldData = false;
×
1200
      goto _exit;
×
1201
    } else {
1202
      const SBrinBlk *brinBlk = TARRAY2_GET_PTR(writer->ctx->brinBlkArray, writer->ctx->brinBlkArrayIdx);
×
1203

1204
      if (brinBlk->minTbid.uid != writer->ctx->tbid->uid) {
×
1205
        writer->ctx->tbHasOldData = false;
×
1206
        goto _exit;
×
1207
      }
1208

1209
      TAOS_CHECK_GOTO(tsdbDataFileReadBrinBlock(writer->ctx->reader, brinBlk, writer->ctx->brinBlock), &lino, _exit);
×
1210

1211
      writer->ctx->brinBlockIdx = 0;
×
1212
      writer->ctx->brinBlkArrayIdx++;
×
1213
    }
1214
  }
1215

1216
_exit:
×
1217
  if (code) {
×
1218
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1219
              tstrerror(code));
1220
  }
1221
  return code;
×
1222
}
1223

1224
static int32_t tsdbDataFileDoWriteTSData(SDataFileWriter *writer, TSDBROW *row) {
×
1225
  int32_t code = 0;
×
1226
  int32_t lino = 0;
×
1227

1228
  if (writer->ctx->tbHasOldData) {
×
1229
    STsdbRowKey key;
1230
    tsdbRowGetKey(row, &key);
×
1231
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTableOldData(writer, &key), &lino, _exit);
×
1232
  }
1233

1234
  TAOS_CHECK_GOTO(tsdbDataFileDoWriteTSRow(writer, row), &lino, _exit);
×
1235

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

1244
static int32_t tsdbDataFileWriteTableDataEnd(SDataFileWriter *writer) {
×
1245
  if (writer->ctx->tbid->uid == 0) {
×
1246
    return 0;
×
1247
  }
1248

1249
  int32_t code = 0;
×
1250
  int32_t lino = 0;
×
1251

1252
  if (writer->ctx->tbHasOldData) {
×
1253
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTableOldData(writer, NULL /* as the largest key */), &lino, _exit);
×
1254
  }
1255

1256
  TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, writer->blockData), &lino, _exit);
×
1257

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

1266
static int32_t tsdbDataFileWriteTableDataBegin(SDataFileWriter *writer, const TABLEID *tbid) {
×
1267
  int32_t code = 0;
×
1268
  int32_t lino = 0;
×
1269

1270
  SMetaInfo info;
1271
  bool      drop = false;
×
1272
  TABLEID   tbid1[1];
1273
  writer->ctx->tbHasOldData = false;
×
1274
  while (writer->ctx->brinBlkArray) {  // skip data of previous table
×
1275
    for (; writer->ctx->brinBlockIdx < writer->ctx->brinBlock->numOfRecords; writer->ctx->brinBlockIdx++) {
×
1276
      SBrinRecord record;
1277
      TAOS_CHECK_GOTO(tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, &record), &lino, _exit);
×
1278

1279
      if (record.uid == tbid->uid) {
×
1280
        writer->ctx->tbHasOldData = true;
×
1281
        goto _begin;
×
1282
      } else if (record.suid > tbid->suid || (record.suid == tbid->suid && record.uid > tbid->uid)) {
×
1283
        goto _begin;
×
1284
      } else {
1285
        if (record.uid != writer->ctx->tbid->uid) {
×
1286
          if (drop && tbid1->uid == record.uid) {
×
1287
            continue;
×
1288
          } else if (metaGetInfo(writer->config->tsdb->pVnode->pMeta, record.uid, &info, NULL) != 0) {
×
1289
            drop = true;
×
1290
            tbid1->suid = record.suid;
×
1291
            tbid1->uid = record.uid;
×
1292
            continue;
×
1293
          } else {
1294
            drop = false;
×
1295
            writer->ctx->tbid->suid = record.suid;
×
1296
            writer->ctx->tbid->uid = record.uid;
×
1297
          }
1298
        }
1299

1300
        TAOS_CHECK_GOTO(tsdbDataFileWriteBrinRecord(writer, &record), &lino, _exit);
×
1301
      }
1302
    }
1303

1304
    if (writer->ctx->brinBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->brinBlkArray)) {
×
1305
      writer->ctx->brinBlkArray = NULL;
×
1306
      break;
×
1307
    } else {
1308
      const SBrinBlk *brinBlk = TARRAY2_GET_PTR(writer->ctx->brinBlkArray, writer->ctx->brinBlkArrayIdx);
×
1309

1310
      TAOS_CHECK_GOTO(tsdbDataFileReadBrinBlock(writer->ctx->reader, brinBlk, writer->ctx->brinBlock), &lino, _exit);
×
1311

1312
      writer->ctx->brinBlockIdx = 0;
×
1313
      writer->ctx->brinBlkArrayIdx++;
×
1314
    }
1315
  }
1316

1317
_begin:
×
1318
  writer->ctx->tbid[0] = *tbid;
×
1319

1320
  if (tbid->uid == INT64_MAX) {
×
1321
    goto _exit;
×
1322
  }
1323

1324
  TAOS_CHECK_GOTO(tsdbUpdateSkmTb(writer->config->tsdb, tbid, writer->config->skmTb), &lino, _exit);
×
1325
  TAOS_CHECK_GOTO(tBlockDataInit(writer->blockData, writer->ctx->tbid, writer->config->skmTb->pTSchema, NULL, 0), &lino,
×
1326
                  _exit);
1327

1328
_exit:
×
1329
  if (code) {
×
1330
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1331
              tstrerror(code));
1332
  }
1333
  return code;
×
1334
}
1335

1336
int32_t tsdbFileWriteHeadFooter(STsdbFD *fd, int64_t *fileSize, const SHeadFooter *footer, int32_t encryptAlgorithm,
×
1337
                                char *encryptKey) {
1338
  TAOS_CHECK_RETURN(
×
1339
      tsdbWriteFile(fd, *fileSize, (const uint8_t *)footer, sizeof(*footer), encryptAlgorithm, encryptKey));
1340
  *fileSize += sizeof(*footer);
×
1341
  return 0;
×
1342
}
1343

1344
int32_t tsdbFileWriteTombBlock(STsdbFD *fd, STombBlock *tombBlock, int8_t cmprAlg, int64_t *fileSize,
×
1345
                               TTombBlkArray *tombBlkArray, SBuffer *buffers, SVersionRange *range,
1346
                               int32_t encryptAlgorithm, char *encryptKey) {
1347
  int32_t code;
1348

1349
  if (TOMB_BLOCK_SIZE(tombBlock) == 0) {
×
1350
    return 0;
×
1351
  }
1352

1353
  SBuffer *buffer0 = buffers + 0;
×
1354
  SBuffer *assist = buffers + 1;
×
1355

1356
  STombBlk tombBlk = {
×
1357
      .dp[0] =
1358
          {
1359
              .offset = *fileSize,
×
1360
              .size = 0,
1361
          },
1362
      .numRec = TOMB_BLOCK_SIZE(tombBlock),
×
1363
      .cmprAlg = cmprAlg,
1364
  };
1365
  for (int i = 0; i < TOMB_BLOCK_SIZE(tombBlock); i++) {
×
1366
    STombRecord record;
1367
    TAOS_CHECK_RETURN(tTombBlockGet(tombBlock, i, &record));
×
1368

1369
    if (i == 0) {
×
1370
      tombBlk.minTbid.suid = record.suid;
×
1371
      tombBlk.minTbid.uid = record.uid;
×
1372
      tombBlk.minVer = record.version;
×
1373
      tombBlk.maxVer = record.version;
×
1374
    }
1375
    if (i == TOMB_BLOCK_SIZE(tombBlock) - 1) {
×
1376
      tombBlk.maxTbid.suid = record.suid;
×
1377
      tombBlk.maxTbid.uid = record.uid;
×
1378
    }
1379
    if (record.version < tombBlk.minVer) {
×
1380
      tombBlk.minVer = record.version;
×
1381
    }
1382
    if (record.version > tombBlk.maxVer) {
×
1383
      tombBlk.maxVer = record.version;
×
1384
    }
1385
  }
1386

1387
  tsdbWriterUpdVerRange(range, tombBlk.minVer, tombBlk.maxVer);
×
1388

1389
  for (int32_t i = 0; i < ARRAY_SIZE(tombBlock->buffers); i++) {
×
1390
    tBufferClear(buffer0);
1391

1392
    SCompressInfo cinfo = {
×
1393
        .cmprAlg = cmprAlg,
1394
        .dataType = TSDB_DATA_TYPE_BIGINT,
1395
        .originalSize = tombBlock->buffers[i].size,
×
1396
    };
1397
    TAOS_CHECK_RETURN(tCompressDataToBuffer(tombBlock->buffers[i].data, &cinfo, buffer0, assist));
×
1398
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptAlgorithm, encryptKey));
×
1399

1400
    tombBlk.size[i] = cinfo.compressedSize;
×
1401
    tombBlk.dp->size += tombBlk.size[i];
×
1402
    *fileSize += tombBlk.size[i];
×
1403
  }
1404

1405
  TAOS_CHECK_RETURN(TARRAY2_APPEND_PTR(tombBlkArray, &tombBlk));
×
1406

1407
  tTombBlockClear(tombBlock);
×
1408
  return 0;
×
1409
}
1410

1411
static int32_t tsdbDataFileWriteHeadFooter(SDataFileWriter *writer) {
×
1412
  int32_t code = 0;
×
1413
  int32_t lino = 0;
×
1414

1415
  int32_t encryptAlgorithm = writer->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
×
1416
  char   *encryptKey = writer->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
×
1417

1418
  TAOS_CHECK_GOTO(tsdbFileWriteHeadFooter(writer->fd[TSDB_FTYPE_HEAD], &writer->files[TSDB_FTYPE_HEAD].size,
×
1419
                                          writer->headFooter, encryptAlgorithm, encryptKey),
1420
                  &lino, _exit);
1421

1422
_exit:
×
1423
  if (code) {
×
1424
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1425
              tstrerror(code));
1426
  }
1427
  return code;
×
1428
}
1429

1430
static int32_t tsdbDataFileDoWriteTombBlock(SDataFileWriter *writer) {
×
1431
  if (TOMB_BLOCK_SIZE(writer->tombBlock) == 0) return 0;
×
1432

1433
  int32_t code = 0;
×
1434
  int32_t lino = 0;
×
1435

1436
  int32_t encryptAlgorithm = writer->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
×
1437
  char   *encryptKey = writer->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
×
1438

1439
  TAOS_CHECK_GOTO(tsdbFileWriteTombBlock(writer->fd[TSDB_FTYPE_TOMB], writer->tombBlock, writer->config->cmprAlg,
×
1440
                                         &writer->files[TSDB_FTYPE_TOMB].size, writer->tombBlkArray, writer->buffers,
1441
                                         &writer->ctx->tombRange, encryptAlgorithm, encryptKey),
1442
                  &lino, _exit);
1443

1444
_exit:
×
1445
  if (code) {
×
1446
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1447
              tstrerror(code));
1448
  }
1449
  return code;
×
1450
}
1451

1452
int32_t tsdbFileWriteTombBlk(STsdbFD *fd, const TTombBlkArray *tombBlkArray, SFDataPtr *ptr, int64_t *fileSize,
×
1453
                             int32_t encryptAlgorithm, char *encryptKey) {
1454
  ptr->size = TARRAY2_DATA_LEN(tombBlkArray);
×
1455
  if (ptr->size > 0) {
×
1456
    ptr->offset = *fileSize;
×
1457

1458
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, (const uint8_t *)TARRAY2_DATA(tombBlkArray), ptr->size,
×
1459
                                    encryptAlgorithm, encryptKey));
1460

1461
    *fileSize += ptr->size;
×
1462
  }
1463
  return 0;
×
1464
}
1465

1466
static int32_t tsdbDataFileDoWriteTombBlk(SDataFileWriter *writer) {
×
1467
  if (TARRAY2_SIZE(writer->tombBlkArray) <= 0) {
×
1468
    return TSDB_CODE_INVALID_PARA;
×
1469
  }
1470

1471
  int32_t code = 0;
×
1472
  int32_t lino = 0;
×
1473

1474
  int32_t encryptAlgorithm = writer->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
×
1475
  char   *encryptKey = writer->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
×
1476

1477
  TAOS_CHECK_GOTO(
×
1478
      tsdbFileWriteTombBlk(writer->fd[TSDB_FTYPE_TOMB], writer->tombBlkArray, writer->tombFooter->tombBlkPtr,
1479
                           &writer->files[TSDB_FTYPE_TOMB].size, encryptAlgorithm, encryptKey),
1480
      &lino, _exit);
1481

1482
_exit:
×
1483
  if (code) {
×
1484
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1485
              tstrerror(code));
1486
  }
1487
  return code;
×
1488
}
1489

1490
int32_t tsdbFileWriteTombFooter(STsdbFD *fd, const STombFooter *footer, int64_t *fileSize, int32_t encryptAlgorithm,
×
1491
                                char *encryptKey) {
1492
  TAOS_CHECK_RETURN(
×
1493
      tsdbWriteFile(fd, *fileSize, (const uint8_t *)footer, sizeof(*footer), encryptAlgorithm, encryptKey));
1494
  *fileSize += sizeof(*footer);
×
1495
  return 0;
×
1496
}
1497

1498
static int32_t tsdbDataFileWriteTombFooter(SDataFileWriter *writer) {
×
1499
  int32_t code = 0;
×
1500
  int32_t lino = 0;
×
1501

1502
  int32_t encryptAlgorithm = writer->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
×
1503
  char   *encryptKey = writer->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
×
1504

1505
  TAOS_CHECK_GOTO(tsdbFileWriteTombFooter(writer->fd[TSDB_FTYPE_TOMB], writer->tombFooter,
×
1506
                                          &writer->files[TSDB_FTYPE_TOMB].size, encryptAlgorithm, encryptKey),
1507
                  &lino, _exit);
1508

1509
_exit:
×
1510
  if (code) {
×
1511
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1512
              tstrerror(code));
1513
  }
1514
  return code;
×
1515
}
1516

1517
static int32_t tsdbDataFileDoWriteTombRecord(SDataFileWriter *writer, const STombRecord *record) {
×
1518
  int32_t code = 0;
×
1519
  int32_t lino = 0;
×
1520

1521
  while (writer->ctx->hasOldTomb) {
×
1522
    for (; writer->ctx->tombBlockIdx < TOMB_BLOCK_SIZE(writer->ctx->tombBlock); writer->ctx->tombBlockIdx++) {
×
1523
      STombRecord record1[1];
1524
      TAOS_CHECK_GOTO(tTombBlockGet(writer->ctx->tombBlock, writer->ctx->tombBlockIdx, record1), &lino, _exit);
×
1525

1526
      int32_t c = tTombRecordCompare(record, record1);
×
1527
      if (c < 0) {
×
1528
        goto _write;
×
1529
      } else if (c > 0) {
×
1530
        TAOS_CHECK_GOTO(tTombBlockPut(writer->tombBlock, record1), &lino, _exit);
×
1531

1532
        tsdbTrace("vgId:%d write tomb record to tomb file:%s, cid:%" PRId64 ", suid:%" PRId64 ", uid:%" PRId64
×
1533
                  ", version:%" PRId64,
1534
                  TD_VID(writer->config->tsdb->pVnode), writer->fd[TSDB_FTYPE_TOMB]->path, writer->config->cid,
1535
                  record1->suid, record1->uid, record1->version);
1536

1537
        if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
×
1538
          TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlock(writer), &lino, _exit);
×
1539
        }
1540
      } else {
1541
        tsdbError("vgId:%d duplicate tomb record, cid:%" PRId64 ", suid:%" PRId64 ", uid:%" PRId64 ", version:%" PRId64,
×
1542
                  TD_VID(writer->config->tsdb->pVnode), writer->config->cid, record->suid, record->uid,
1543
                  record->version);
1544
      }
1545
    }
1546

1547
    if (writer->ctx->tombBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->tombBlkArray)) {
×
1548
      writer->ctx->hasOldTomb = false;
×
1549
      break;
×
1550
    } else {
1551
      const STombBlk *tombBlk = TARRAY2_GET_PTR(writer->ctx->tombBlkArray, writer->ctx->tombBlkArrayIdx);
×
1552

1553
      TAOS_CHECK_GOTO(tsdbDataFileReadTombBlock(writer->ctx->reader, tombBlk, writer->ctx->tombBlock), &lino, _exit);
×
1554

1555
      writer->ctx->tombBlockIdx = 0;
×
1556
      writer->ctx->tombBlkArrayIdx++;
×
1557
    }
1558
  }
1559

1560
_write:
×
1561
  if (record->suid == INT64_MAX) {
×
1562
    goto _exit;
×
1563
  }
1564

1565
  TAOS_CHECK_GOTO(tTombBlockPut(writer->tombBlock, record), &lino, _exit);
×
1566

1567
  tsdbTrace("vgId:%d write tomb record to tomb file:%s, cid:%" PRId64 ", suid:%" PRId64 ", uid:%" PRId64
×
1568
            ", version:%" PRId64,
1569
            TD_VID(writer->config->tsdb->pVnode), writer->fd[TSDB_FTYPE_TOMB]->path, writer->config->cid, record->suid,
1570
            record->uid, record->version);
1571

1572
  if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
×
1573
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlock(writer), &lino, _exit);
×
1574
  }
1575

1576
_exit:
×
1577
  if (code) {
×
1578
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1579
              tstrerror(code));
1580
  }
1581
  return code;
×
1582
}
1583

1584
int32_t tsdbFileWriteBrinBlk(STsdbFD *fd, TBrinBlkArray *brinBlkArray, SFDataPtr *ptr, int64_t *fileSize,
×
1585
                             int32_t encryptAlgorithm, char *encryptKey) {
1586
  if (TARRAY2_SIZE(brinBlkArray) <= 0) {
×
1587
    return TSDB_CODE_INVALID_PARA;
×
1588
  }
1589
  ptr->offset = *fileSize;
×
1590
  ptr->size = TARRAY2_DATA_LEN(brinBlkArray);
×
1591

1592
  TAOS_CHECK_RETURN(
×
1593
      tsdbWriteFile(fd, ptr->offset, (uint8_t *)TARRAY2_DATA(brinBlkArray), ptr->size, encryptAlgorithm, encryptKey));
1594

1595
  *fileSize += ptr->size;
×
1596
  return 0;
×
1597
}
1598

1599
static int32_t tsdbDataFileWriteBrinBlk(SDataFileWriter *writer) {
×
1600
  int32_t code = 0;
×
1601
  int32_t lino = 0;
×
1602

1603
  int32_t encryptAlgorithm = writer->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
×
1604
  char   *encryptKey = writer->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
×
1605

1606
  TAOS_CHECK_GOTO(
×
1607
      tsdbFileWriteBrinBlk(writer->fd[TSDB_FTYPE_HEAD], writer->brinBlkArray, writer->headFooter->brinBlkPtr,
1608
                           &writer->files[TSDB_FTYPE_HEAD].size, encryptAlgorithm, encryptKey),
1609
      &lino, _exit);
1610

1611
_exit:
×
1612
  if (code) {
×
1613
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1614
              tstrerror(code));
1615
  }
1616
  return code;
×
1617
}
1618

1619
void tsdbTFileUpdVerRange(STFile *f, SVersionRange range) {
×
1620
  f->minVer = TMIN(f->minVer, range.minVer);
×
1621
  f->maxVer = TMAX(f->maxVer, range.maxVer);
×
1622
}
×
1623

1624
static int32_t tsdbDataFileWriterCloseCommit(SDataFileWriter *writer, TFileOpArray *opArr) {
×
1625
  int32_t code = 0;
×
1626
  int32_t lino = 0;
×
1627

1628
  int32_t  ftype;
1629
  STFileOp op;
1630

1631
  if (writer->fd[TSDB_FTYPE_HEAD]) {
×
1632
    TABLEID tbid[1] = {{
×
1633
        .suid = INT64_MAX,
1634
        .uid = INT64_MAX,
1635
    }};
1636

1637
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataEnd(writer), &lino, _exit);
×
1638
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataBegin(writer, tbid), &lino, _exit);
×
1639
    TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlock(writer), &lino, _exit);
×
1640
    TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlk(writer), &lino, _exit);
×
1641
    TAOS_CHECK_GOTO(tsdbDataFileWriteHeadFooter(writer), &lino, _exit);
×
1642

1643
    SVersionRange ofRange = {.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
×
1644

1645
    // .head
1646
    ftype = TSDB_FTYPE_HEAD;
×
1647
    if (writer->config->files[ftype].exist) {
×
1648
      op = (STFileOp){
×
1649
          .optype = TSDB_FOP_REMOVE,
1650
          .fid = writer->config->fid,
×
1651
          .of = writer->config->files[ftype].file,
×
1652
      };
1653
      ofRange = (SVersionRange){.minVer = op.of.minVer, .maxVer = op.of.maxVer};
×
1654
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
×
1655
    }
1656
    op = (STFileOp){
×
1657
        .optype = TSDB_FOP_CREATE,
1658
        .fid = writer->config->fid,
×
1659
        .nf = writer->files[ftype],
×
1660
    };
1661
    tsdbTFileUpdVerRange(&op.nf, ofRange);
×
1662
    tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
×
1663
    TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
×
1664

1665
    // .data
1666
    ftype = TSDB_FTYPE_DATA;
×
1667
    if (!writer->config->files[ftype].exist) {
×
1668
      op = (STFileOp){
×
1669
          .optype = TSDB_FOP_CREATE,
1670
          .fid = writer->config->fid,
×
1671
          .nf = writer->files[ftype],
×
1672
      };
1673
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
×
1674
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
×
1675
    } else if (writer->config->files[ftype].file.size != writer->files[ftype].size) {
×
1676
      op = (STFileOp){
×
1677
          .optype = TSDB_FOP_MODIFY,
1678
          .fid = writer->config->fid,
×
1679
          .of = writer->config->files[ftype].file,
×
1680
          .nf = writer->files[ftype],
×
1681
      };
1682
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
×
1683
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
×
1684
    }
1685

1686
    // .sma
1687
    ftype = TSDB_FTYPE_SMA;
×
1688
    if (!writer->config->files[ftype].exist) {
×
1689
      op = (STFileOp){
×
1690
          .optype = TSDB_FOP_CREATE,
1691
          .fid = writer->config->fid,
×
1692
          .nf = writer->files[ftype],
×
1693
      };
1694
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
×
1695
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
×
1696
    } else if (writer->config->files[ftype].file.size != writer->files[ftype].size) {
×
1697
      op = (STFileOp){
×
1698
          .optype = TSDB_FOP_MODIFY,
1699
          .fid = writer->config->fid,
×
1700
          .of = writer->config->files[ftype].file,
×
1701
          .nf = writer->files[ftype],
×
1702
      };
1703
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
×
1704
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
×
1705
    }
1706
  }
1707

1708
  if (writer->fd[TSDB_FTYPE_TOMB]) {
×
1709
    STombRecord record[1] = {{
×
1710
        .suid = INT64_MAX,
1711
        .uid = INT64_MAX,
1712
        .version = INT64_MAX,
1713
    }};
1714

1715
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombRecord(writer, record), &lino, _exit);
×
1716
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlock(writer), &lino, _exit);
×
1717
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlk(writer), &lino, _exit);
×
1718
    TAOS_CHECK_GOTO(tsdbDataFileWriteTombFooter(writer), &lino, _exit);
×
1719

1720
    SVersionRange ofRange = (SVersionRange){.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
×
1721

1722
    ftype = TSDB_FTYPE_TOMB;
×
1723
    if (writer->config->files[ftype].exist) {
×
1724
      op = (STFileOp){
×
1725
          .optype = TSDB_FOP_REMOVE,
1726
          .fid = writer->config->fid,
×
1727
          .of = writer->config->files[ftype].file,
×
1728
      };
1729
      ofRange = (SVersionRange){.minVer = op.of.minVer, .maxVer = op.of.maxVer};
×
1730
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
×
1731
    }
1732
    op = (STFileOp){
×
1733
        .optype = TSDB_FOP_CREATE,
1734
        .fid = writer->config->fid,
×
1735
        .nf = writer->files[ftype],
×
1736
    };
1737
    tsdbTFileUpdVerRange(&op.nf, ofRange);
×
1738
    tsdbTFileUpdVerRange(&op.nf, writer->ctx->tombRange);
×
1739
    TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
×
1740
  }
1741
  int32_t encryptAlgorithm = writer->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
×
1742
  char   *encryptKey = writer->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
×
1743
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
×
1744
    if (writer->fd[i]) {
×
1745
      TAOS_CHECK_GOTO(tsdbFsyncFile(writer->fd[i], encryptAlgorithm, encryptKey), &lino, _exit);
×
1746
      tsdbCloseFile(&writer->fd[i]);
×
1747
    }
1748
  }
1749

1750
_exit:
×
1751
  if (code) {
×
1752
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1753
              tstrerror(code));
1754
  }
1755
  return code;
×
1756
}
1757

1758
static int32_t tsdbDataFileWriterOpenDataFD(SDataFileWriter *writer) {
×
1759
  int32_t code = 0;
×
1760
  int32_t lino = 0;
×
1761

1762
  int32_t ftypes[] = {TSDB_FTYPE_HEAD, TSDB_FTYPE_DATA, TSDB_FTYPE_SMA};
×
1763

1764
  for (int32_t i = 0; i < ARRAY_SIZE(ftypes); ++i) {
×
1765
    int32_t ftype = ftypes[i];
×
1766

1767
    char    fname[TSDB_FILENAME_LEN];
1768
    int32_t flag = TD_FILE_READ | TD_FILE_WRITE;
×
1769

1770
    if (writer->files[ftype].size == 0) {
×
1771
      flag |= (TD_FILE_CREATE | TD_FILE_TRUNC);
×
1772
    }
1773

1774
    int32_t lcn = writer->files[ftype].lcn;
×
1775
    tsdbTFileName(writer->config->tsdb, &writer->files[ftype], fname);
×
1776
    TAOS_CHECK_GOTO(tsdbOpenFile(fname, writer->config->tsdb, flag, &writer->fd[ftype], lcn), &lino, _exit);
×
1777

1778
    if (writer->files[ftype].size == 0) {
×
1779
      uint8_t hdr[TSDB_FHDR_SIZE] = {0};
×
1780

1781
      int32_t encryptAlgorithm = writer->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
×
1782
      char   *encryptKey = writer->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
×
1783

1784
      TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd[ftype], 0, hdr, TSDB_FHDR_SIZE, encryptAlgorithm, encryptKey), &lino,
×
1785
                      _exit);
1786

1787
      writer->files[ftype].size += TSDB_FHDR_SIZE;
×
1788
    }
1789
  }
1790

1791
  if (writer->ctx->reader) {
×
1792
    TAOS_CHECK_GOTO(tsdbDataFileReadBrinBlk(writer->ctx->reader, &writer->ctx->brinBlkArray), &lino, _exit);
×
1793
  }
1794

1795
_exit:
×
1796
  if (code) {
×
1797
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1798
              tstrerror(code));
1799
  }
1800
  return code;
×
1801
}
1802

1803
int32_t tsdbDataFileWriterOpen(const SDataFileWriterConfig *config, SDataFileWriter **writer) {
×
1804
  writer[0] = taosMemoryCalloc(1, sizeof(*writer[0]));
×
1805
  if (!writer[0]) {
×
1806
    return terrno;
×
1807
  }
1808

1809
  writer[0]->config[0] = config[0];
×
1810
  return 0;
×
1811
}
1812

1813
int32_t tsdbDataFileWriterClose(SDataFileWriter **writer, bool abort, TFileOpArray *opArr) {
×
1814
  if (writer[0] == NULL) return 0;
×
1815

1816
  int32_t code = 0;
×
1817
  int32_t lino = 0;
×
1818

1819
  if (writer[0]->ctx->opened) {
×
1820
    if (abort) {
×
1821
      TAOS_CHECK_GOTO(tsdbDataFileWriterCloseAbort(writer[0]), &lino, _exit);
×
1822
    } else {
1823
      TAOS_CHECK_GOTO(tsdbDataFileWriterCloseCommit(writer[0], opArr), &lino, _exit);
×
1824
    }
1825
    tsdbDataFileWriterDoClose(writer[0]);
×
1826
  }
1827
  taosMemoryFree(writer[0]);
×
1828
  writer[0] = NULL;
×
1829

1830
_exit:
×
1831
  if (code) {
×
1832
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer[0]->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1833
              tstrerror(code));
1834
  }
1835
  return code;
×
1836
}
1837

1838
int32_t tsdbDataFileWriteRow(SDataFileWriter *writer, SRowInfo *row) {
×
1839
  int32_t code = 0;
×
1840
  int32_t lino = 0;
×
1841

1842
  if (!writer->ctx->opened) {
×
1843
    TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpen(writer), &lino, _exit);
×
1844
  }
1845

1846
  if (writer->fd[TSDB_FTYPE_HEAD] == NULL) {
×
1847
    TAOS_CHECK_GOTO(tsdbDataFileWriterOpenDataFD(writer), &lino, _exit);
×
1848
  }
1849

1850
  if (row->uid != writer->ctx->tbid->uid) {
×
1851
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataEnd(writer), &lino, _exit);
×
1852
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataBegin(writer, (TABLEID *)row), &lino, _exit);
×
1853
  }
1854

1855
  TAOS_CHECK_GOTO(tsdbDataFileDoWriteTSData(writer, &row->row), &lino, _exit);
×
1856

1857
_exit:
×
1858
  if (code) {
×
1859
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1860
              tstrerror(code));
1861
  }
1862
  return code;
×
1863
}
1864

1865
int32_t tsdbDataFileWriteBlockData(SDataFileWriter *writer, SBlockData *bData) {
×
1866
  if (bData->nRow == 0) {
×
1867
    return 0;
×
1868
  }
1869

1870
  int32_t code = 0;
×
1871
  int32_t lino = 0;
×
1872

1873
  if (!bData->uid) {
×
1874
    return TSDB_CODE_INVALID_PARA;
×
1875
  }
1876

1877
  if (!writer->ctx->opened) {
×
1878
    TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpen(writer), &lino, _exit);
×
1879
  }
1880

1881
  if (writer->fd[TSDB_FTYPE_DATA] == NULL) {
×
1882
    TAOS_CHECK_GOTO(tsdbDataFileWriterOpenDataFD(writer), &lino, _exit);
×
1883
  }
1884

1885
  if (bData->uid != writer->ctx->tbid->uid) {
×
1886
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataEnd(writer), &lino, _exit);
×
1887
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataBegin(writer, (TABLEID *)bData), &lino, _exit);
×
1888
  }
1889

1890
  if (writer->ctx->tbHasOldData) {
×
1891
    STsdbRowKey key;
1892

1893
    tsdbRowGetKey(&tsdbRowFromBlockData(bData, 0), &key);
×
1894
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTableOldData(writer, &key), &lino, _exit);
×
1895
  }
1896

1897
  if (!writer->ctx->tbHasOldData       //
×
1898
      && writer->blockData->nRow == 0  //
×
1899
  ) {
1900
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, bData), &lino, _exit);
×
1901

1902
  } else {
1903
    for (int32_t i = 0; i < bData->nRow; ++i) {
×
1904
      TSDBROW row[1] = {tsdbRowFromBlockData(bData, i)};
×
1905
      TAOS_CHECK_GOTO(tsdbDataFileDoWriteTSData(writer, row), &lino, _exit);
×
1906
    }
1907
  }
1908

1909
_exit:
×
1910
  if (code) {
×
1911
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1912
              tstrerror(code));
1913
  }
1914
  return code;
×
1915
}
1916

1917
int32_t tsdbDataFileFlush(SDataFileWriter *writer) {
×
1918
  if (!writer->ctx->opened) {
×
1919
    return TSDB_CODE_INVALID_PARA;
×
1920
  }
1921

1922
  if (writer->blockData->nRow == 0) return 0;
×
1923
  if (writer->ctx->tbHasOldData) return 0;
×
1924

1925
  return tsdbDataFileDoWriteBlockData(writer, writer->blockData);
×
1926
}
1927

1928
static int32_t tsdbDataFileWriterOpenTombFD(SDataFileWriter *writer) {
×
1929
  int32_t code = 0;
×
1930
  int32_t lino = 0;
×
1931

1932
  char    fname[TSDB_FILENAME_LEN];
1933
  int32_t ftype = TSDB_FTYPE_TOMB;
×
1934

1935
  int32_t flag = (TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
×
1936

1937
  int32_t lcn = writer->files[ftype].lcn;
×
1938
  tsdbTFileName(writer->config->tsdb, writer->files + ftype, fname);
×
1939

1940
  TAOS_CHECK_GOTO(tsdbOpenFile(fname, writer->config->tsdb, flag, &writer->fd[ftype], lcn), &lino, _exit);
×
1941

1942
  uint8_t hdr[TSDB_FHDR_SIZE] = {0};
×
1943
  int32_t encryptAlgorithm = writer->config->tsdb->pVnode->config.tsdbCfg.encryptAlgorithm;
×
1944
  char   *encryptKey = writer->config->tsdb->pVnode->config.tsdbCfg.encryptKey;
×
1945

1946
  TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd[ftype], 0, hdr, TSDB_FHDR_SIZE, encryptAlgorithm, encryptKey), &lino, _exit);
×
1947
  writer->files[ftype].size += TSDB_FHDR_SIZE;
×
1948

1949
  if (writer->ctx->reader) {
×
1950
    TAOS_CHECK_GOTO(tsdbDataFileReadTombBlk(writer->ctx->reader, &writer->ctx->tombBlkArray), &lino, _exit);
×
1951

1952
    if (TARRAY2_SIZE(writer->ctx->tombBlkArray) > 0) {
×
1953
      writer->ctx->hasOldTomb = true;
×
1954
    }
1955

1956
    writer->ctx->tombBlkArrayIdx = 0;
×
1957
    tTombBlockClear(writer->ctx->tombBlock);
×
1958
    writer->ctx->tombBlockIdx = 0;
×
1959
  }
1960

1961
_exit:
×
1962
  if (code) {
×
1963
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1964
              tstrerror(code));
1965
  }
1966
  return code;
×
1967
}
1968

1969
int32_t tsdbDataFileWriteTombRecord(SDataFileWriter *writer, const STombRecord *record) {
×
1970
  int32_t code = 0;
×
1971
  int32_t lino = 0;
×
1972

1973
  if (!writer->ctx->opened) {
×
1974
    TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpen(writer), &lino, _exit);
×
1975
  }
1976

1977
  if (writer->fd[TSDB_FTYPE_TOMB] == NULL) {
×
1978
    TAOS_CHECK_GOTO(tsdbDataFileWriterOpenTombFD(writer), &lino, _exit);
×
1979
  }
1980

1981
  TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombRecord(writer, record), &lino, _exit);
×
1982

1983
_exit:
×
1984
  if (code) {
×
1985
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1986
              tstrerror(code));
1987
  }
1988
  return code;
×
1989
}
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