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

taosdata / TDengine / #5138

05 Nov 2026 03:32AM UTC coverage: 73.942% (-0.3%) from 74.268%
#5138

push

travis-ci

jbjia
fix: capture coverage once from per-thread gcda

Per-case lcov walked the shared build tree and kept each case busy for minutes. Pass GCOV_PREFIX into the test container so each thread writes its own counters, then merge and capture after the suite.

373256 of 504798 relevant lines covered (73.94%)

4305711.12 hits per line

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

91.39
/source/dnode/vnode/src/tsdb/tsdbDataFileRW.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * SPDX-License-Identifier: AGPL-3.0-or-later
5
 *
6
 * This program is free software: you can use, redistribute, and/or modify
7
 * it under the terms of the GNU Affero General Public License, version 3
8
 * or later ("AGPL"), as published by the Free Software Foundation.
9
 *
10
 * This program is distributed in the hope that it will be useful, but WITHOUT
11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
 * FITNESS FOR A PARTICULAR PURPOSE.
13
 *
14
 * You should have received a copy of the GNU Affero General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17

18
#include "tsdbDataFileRW.h"
19
#include "meta.h"
20
#include "tglobal.h"
21

22
// SThrottle ===============================================
23
// deadline-based throttle struct (forward declared in tsdbDataFileRW.h):
24
//   - nextAllowed: ms timestamp; the next write may start at/after this time
25
//   - fracBytes:   sub-ms quota debt (< limitBytes), carried over across
26
//                  writes so tiny writes still accumulate and get throttled
27
struct SThrottle {
28
  int64_t limitBytes;   // bytes per second; 0 or negative means no limit
29
  int64_t nextAllowed;  // next allowed write time in ms
30
  int64_t fracBytes;    // sub-ms quota remainder, 0 <= fracBytes < limitBytes
31
};
32

33
// process-level singleton throttle shared by all compact tasks
34
static SThrottle    gCompactThrottle;
35
static TdThreadMutex gCompactThrottleLock;               // lazily initialized (CRITICAL_SECTION on Windows cannot be statically initialized)
36
static TdThreadOnce  gCompactThrottleOnce = PTHREAD_ONCE_INIT;
37

38
static void tsdbCompactThrottleLockInit(void) { taosThreadMutexInit(&gCompactThrottleLock, NULL); }
×
39

40
SThrottle *tsdbCompactThrottle(void) { return &gCompactThrottle; }
333 ✔
41

42
void tsdbThrottleConsume(SThrottle *throttle, int64_t bytes) {
1,069,157 ✔
43
  if (throttle == NULL || bytes == 0) {
1,069,157 ✔
44
    return;
999,515 ✔
45
  }
46

47
  // read the latest config on every check (dynamic change takes effect
48
  // immediately without a config-change callback)
49
  int64_t limitBytes = (int64_t)tsCompactSpeedLimitMB * 1024 * 1024;
9,018 ✔
50
  if (limitBytes <= 0) {
9,018 ✔
51
    return;  // no limit
9,017 ✔
52
  }
53

54
  // a single wait (and a single write's deadline advance) is capped at this
55
  // much, so one oversized write can never stall concurrent compact tasks for
56
  // an unbounded time. quota beyond the cap is dropped: the oversized write
57
  // was already delayed up to the cap, and dropping keeps the deadline
58
  // moving; rate stays correct on average since such huge writes are rare.
59
  const int64_t maxWaitMs = 1LL * 60 * 1000;  // 1 minute
1 ✔
60

61
  (void)taosThreadOnce(&gCompactThrottleOnce, tsdbCompactThrottleLockInit);
1 ✔
62
  (void)taosThreadMutexLock(&gCompactThrottleLock);
×
63

64
  int64_t now = taosGetTimestampMs();
×
65

66
  // re-init the throttle when the config changes (also serves as lazy init)
67
  if (throttle->limitBytes != limitBytes) {
×
68
    throttle->limitBytes = limitBytes;
×
69
    throttle->nextAllowed = now;
×
70
    throttle->fracBytes = 0;
×
71
  }
72

73
  // wait until this write is allowed by the deadline. the lock is held during
74
  // sleep so concurrent compact tasks serialize on the shared deadline, which
75
  // is exactly what a global rate limit wants. the wait is bounded by
76
  // maxWaitMs: if the quota debt exceeds it, the write proceeds anyway and
77
  // the unpaid remainder is absorbed by the monotonic deadline below.
78
  if (now < throttle->nextAllowed) {
×
79
    taosMsleep((int32_t)TMIN(throttle->nextAllowed - now, maxWaitMs));
×
80
    now = taosGetTimestampMs();
×
81
  }
82

83
  // a write of `bytes` consumes bytes/limitBytes seconds of quota; advance the
84
  // deadline by that many whole ms. the sub-ms remainder is carried in
85
  // fracBytes: once it accumulates to one full ms of quota (limitBytes bytes)
86
  // the deadline is pushed by another ms. without this, a long run of writes
87
  // each smaller than limitBytes/1000 would never move the ms deadline and
88
  // would escape the throttle entirely.
89
  int64_t ms = (bytes * 1000) / limitBytes;
×
90
  throttle->fracBytes += (bytes * 1000) % limitBytes;
×
91
  if (throttle->fracBytes >= limitBytes) {
×
92
    ms += 1;
×
93
    throttle->fracBytes -= limitBytes;
×
94
  }
95

96
  // sanity cap: a single write must not push the deadline unreasonably far,
97
  // which would stall concurrent compact tasks for an unbounded time. the
98
  // un-paid quota beyond the cap is dropped: such an oversized write was
99
  // already delayed up to the cap by its own wait above, and dropping keeps
100
  // the deadline moving. rate is still correct on average because these huge
101
  // writes are rare (a block is normally far smaller than the cap).
102
  if (ms > maxWaitMs) {
×
103
    tsdbWarn("throttle consume %" PRId64 " bytes, quota ms %" PRId64 " capped to %" PRId64, bytes, ms, maxWaitMs);
×
104
    ms = maxWaitMs;
×
105
  }
106

107
  // TMAX keeps the deadline monotonic: never rewind it, otherwise concurrent
108
  // tasks could alternate and gradually slide the deadline forward, diluting
109
  // the rate limit.
110
  throttle->nextAllowed = TMAX(now, throttle->nextAllowed) + ms;
×
111
  (void)taosThreadMutexUnlock(&gCompactThrottleLock);
×
112
}
113

114
// SDataFileReader =============================================
115
struct SDataFileReader {
116
  SDataFileReaderConfig config[1];
117

118
  SBuffer  local[10];
119
  SBuffer *buffers;
120

121
  struct {
122
    bool headFooterLoaded;
123
    bool tombFooterLoaded;
124
    bool brinBlkLoaded;
125
    bool tombBlkLoaded;
126
  } ctx[1];
127

128
  STsdbFD *fd[TSDB_FTYPE_MAX];
129

130
  SHeadFooter   headFooter[1];
131
  STombFooter   tombFooter[1];
132
  TBrinBlkArray brinBlkArray[1];
133
  TTombBlkArray tombBlkArray[1];
134
};
135

136
static int32_t tsdbDataFileReadHeadFooter(SDataFileReader *reader) {
74,163 ✔
137
  if (reader->ctx->headFooterLoaded) {
74,163 ✔
138
    return 0;
×
139
  }
140

141
  int32_t code = 0;
74,163 ✔
142
  int32_t lino = 0;
74,163 ✔
143

144
  int32_t ftype = TSDB_FTYPE_HEAD;
74,173 ✔
145
  if (reader->fd[ftype]) {
74,173 ✔
146
    SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
68,733 ✔
147

148
#if 1
149
    TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[ftype], reader->config->files[ftype].file.size - sizeof(SHeadFooter),
68,733 ✔
150
                                 (uint8_t *)reader->headFooter, sizeof(SHeadFooter), 0, pEncryptData),
151
                    &lino, _exit);
152
#else
153
    int64_t size = reader->config->files[ftype].file.size;
154
    for (; size > TSDB_FHDR_SIZE; size--) {
155
      code = tsdbReadFile(reader->fd[ftype], size - sizeof(SHeadFooter), (uint8_t *)reader->headFooter,
156
                          sizeof(SHeadFooter), 0, encryptAlgorithm, encryptKey);
157
      if (code) continue;
158
      if (reader->headFooter->brinBlkPtr->offset + reader->headFooter->brinBlkPtr->size + sizeof(SHeadFooter) == size) {
159
        break;
160
      }
161
    }
162
    if (size <= TSDB_FHDR_SIZE) {
163
      TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
164
    }
165
#endif
166
  }
167

168
  reader->ctx->headFooterLoaded = true;
74,173 ✔
169

170
_exit:
73,754 ✔
171
  if (code) {
74,175 ✔
172
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
173
              tstrerror(code));
174
  }
175
  return code;
73,716 ✔
176
}
177

178
static int32_t tsdbDataFileReadTombFooter(SDataFileReader *reader) {
5,965 ✔
179
  if (reader->ctx->tombFooterLoaded) {
5,965 ✔
180
    return 0;
×
181
  }
182

183
  int32_t code = 0;
5,965 ✔
184
  int32_t lino = 0;
5,965 ✔
185

186
  int32_t ftype = TSDB_FTYPE_TOMB;
5,965 ✔
187
  if (reader->fd[ftype]) {
5,965 ✔
188
    SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
5,559 ✔
189

190
    TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[ftype], reader->config->files[ftype].file.size - sizeof(STombFooter),
5,559 ✔
191
                                 (uint8_t *)reader->tombFooter, sizeof(STombFooter), 0, pEncryptData),
192
                    &lino, _exit);
193
  }
194
  reader->ctx->tombFooterLoaded = true;
5,966 ✔
195

196
_exit:
5,919 ✔
197
  if (code) {
5,966 ✔
198
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
199
              tstrerror(code));
200
  }
201
  return code;
5,919 ✔
202
}
203

204
int32_t tsdbDataFileReaderOpen(const char *fname[], const SDataFileReaderConfig *config, SDataFileReader **reader) {
74,396 ✔
205
  int32_t code = 0;
74,396 ✔
206
  int32_t lino = 0;
74,396 ✔
207

208
  if ((*reader = taosMemoryCalloc(1, sizeof(**reader))) == NULL) {
74,396 ✔
209
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
210
  }
211

212
  for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->local); i++) {
818,359 ✔
213
    tBufferInit(reader[0]->local + i);
743,955 ✔
214
  }
215

216
  reader[0]->config[0] = config[0];
74,404 ✔
217
  reader[0]->buffers = config->buffers;
74,404 ✔
218
  if (reader[0]->buffers == NULL) {
74,404 ✔
219
    reader[0]->buffers = reader[0]->local;
73,771 ✔
220
  }
221

222
  if (fname) {
74,404 ✔
223
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
367,240 ✔
224
      if (fname[i]) {
293,789 ✔
225
        int32_t lcn = config->files[i].file.lcn;
210,252 ✔
226
        TAOS_CHECK_GOTO(tsdbOpenFile(fname[i], config->tsdb, TD_FILE_READ, &reader[0]->fd[i], lcn), &lino, _exit);
210,624 ✔
227
      }
228
    }
229
  } else {
230
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
5,031 ✔
231
      if (config->files[i].exist) {
4,056 ✔
232
        char fname1[TSDB_FILENAME_LEN];
1,847 ✔
233
        tsdbTFileName(config->tsdb, &config->files[i].file, fname1);
1,847 ✔
234
        int32_t lcn = config->files[i].file.lcn;
1,847 ✔
235
        TAOS_CHECK_GOTO(tsdbOpenFile(fname1, config->tsdb, TD_FILE_READ, &reader[0]->fd[i], lcn), &lino, _exit);
1,847 ✔
236
      }
237
    }
238
  }
239

240
_exit:
74,007 ✔
241
  if (code) {
74,474 ✔
242
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(config->tsdb->pVnode), __func__, __FILE__, lino,
12 ✔
243
              tstrerror(code));
244
  }
245
  return code;
74,474 ✔
246
}
247

248
void tsdbDataFileReaderClose(SDataFileReader **reader) {
1,092,392 ✔
249
  if (reader[0] == NULL) {
1,092,392 ✔
250
    return;
1,009,460 ✔
251
  }
252

253
  TARRAY2_DESTROY(reader[0]->tombBlkArray, NULL);
73,493 ✔
254
  TARRAY2_DESTROY(reader[0]->brinBlkArray, NULL);
73,493 ✔
255

256
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
371,311 ✔
257
    if (reader[0]->fd[i]) {
297,804 ✔
258
      tsdbCloseFile(&reader[0]->fd[i]);
212,086 ✔
259
    }
260
  }
261

262
  for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->local); ++i) {
818,068 ✔
263
    tBufferDestroy(reader[0]->local + i);
745,307 ✔
264
  }
265

266
  taosMemoryFree(reader[0]);
73,492 ✔
267
  reader[0] = NULL;
74,446 ✔
268
}
269

270
int32_t tsdbDataFileReadBrinBlk(SDataFileReader *reader, const TBrinBlkArray **brinBlkArray) {
74,185 ✔
271
  int32_t code = 0;
74,185 ✔
272
  int32_t lino = 0;
74,185 ✔
273
  void   *data = NULL;
74,186 ✔
274

275
  if (!reader->ctx->brinBlkLoaded) {
74,186 ✔
276
    TAOS_CHECK_GOTO(tsdbDataFileReadHeadFooter(reader), &lino, _exit);
74,212 ✔
277

278
    if (reader->headFooter->brinBlkPtr->size > 0) {
74,157 ✔
279
      data = taosMemoryMalloc(reader->headFooter->brinBlkPtr->size);
68,725 ✔
280
      if (data == NULL) {
68,691 ✔
281
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
282
      }
283

284
      SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
68,691 ✔
285

286
      TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[TSDB_FTYPE_HEAD], reader->headFooter->brinBlkPtr->offset, data,
68,691 ✔
287
                                   reader->headFooter->brinBlkPtr->size, 0, pEncryptData),
288
                      &lino, _exit);
289

290
      int32_t size = reader->headFooter->brinBlkPtr->size / sizeof(SBrinBlk);
68,786 ✔
291
      TARRAY2_INIT_EX(reader->brinBlkArray, size, size, data);
68,786 ✔
292
    } else {
293
      TARRAY2_INIT(reader->brinBlkArray);
5,432 ✔
294
    }
295

296
    reader->ctx->brinBlkLoaded = true;
74,218 ✔
297
  }
298
  brinBlkArray[0] = reader->brinBlkArray;
74,192 ✔
299

300
_exit:
74,178 ✔
301
  if (code) {
74,183 ✔
302
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
303
              tstrerror(code));
304
    taosMemoryFree(data);
×
305
  }
306
  return code;
74,183 ✔
307
}
308

309
int32_t tsdbDataFileReadBrinBlock(SDataFileReader *reader, const SBrinBlk *brinBlk, SBrinBlock *brinBlock) {
57,301 ✔
310
  int32_t code = 0;
57,301 ✔
311
  int32_t lino = 0;
57,301 ✔
312

313
  if (brinBlk->numOfPKs > TD_MAX_PK_COLS) {
57,306 ✔
314
    tsdbError("vgId:%d %s failed at %s:%d since brin block numOfPKs %d exceeds max %d (file corrupted)",
×
315
              TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, (int)brinBlk->numOfPKs,
316
              (int)TD_MAX_PK_COLS);
317
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
445 ✔
318
  }
319

320
  SBuffer *buffer = reader->buffers + 0;
57,329 ✔
321
  SBuffer *assist = reader->buffers + 1;
57,329 ✔
322

323
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
57,329 ✔
324

325
  // load data
326
  tBufferClear(buffer);
445 ✔
327
  code = tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_HEAD], brinBlk->dp->offset, brinBlk->dp->size, buffer, 0,
57,329 ✔
328
                              pEncryptData);
329
  TAOS_CHECK_GOTO(code, &lino, _exit);
57,345 ✔
330

331
  // decode brin block
332
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer);
57,344 ✔
333
  tBrinBlockClear(brinBlock);
57,345 ✔
334
  brinBlock->numOfPKs = brinBlk->numOfPKs;
57,347 ✔
335
  brinBlock->numOfRecords = brinBlk->numRec;
57,347 ✔
336
  for (int32_t i = 0; i < 10; i++) {  // int64_t
630,543 ✔
337

338
    SCompressInfo cinfo = {
573,184 ✔
339
        .cmprAlg = brinBlk->cmprAlg,
573,180 ✔
340
        .dataType = TSDB_DATA_TYPE_BIGINT,
341
        .compressedSize = brinBlk->size[i],
573,180 ✔
342
        .originalSize = brinBlk->numRec * sizeof(int64_t),
573,180 ✔
343
    };
344
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, brinBlock->buffers + i, assist), &lino, _exit);
573,180 ✔
345
    br.offset += brinBlk->size[i];
573,177 ✔
346
  }
347

348
  for (int32_t i = 10; i < 15; i++) {  // int32_t
344,075 ✔
349
    SCompressInfo cinfo = {
286,718 ✔
350
        .cmprAlg = brinBlk->cmprAlg,
286,716 ✔
351
        .dataType = TSDB_DATA_TYPE_INT,
352
        .compressedSize = brinBlk->size[i],
286,716 ✔
353
        .originalSize = brinBlk->numRec * sizeof(int32_t),
286,716 ✔
354
    };
355
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, brinBlock->buffers + i, assist), &lino, _exit);
286,716 ✔
356
    br.offset += brinBlk->size[i];
286,710 ✔
357
  }
358

359
  // primary keys
360
  if (brinBlk->numOfPKs > 0) {  // decode the primary keys
57,357 ✔
361
    SValueColumnCompressInfo firstInfos[TD_MAX_PK_COLS];
2,392 ✔
362
    SValueColumnCompressInfo lastInfos[TD_MAX_PK_COLS];
2,392 ✔
363

364
    for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
5,670 ✔
365
      TAOS_CHECK_GOTO(tValueColumnCompressInfoDecode(&br, firstInfos + i), &lino, _exit);
2,835 ✔
366
    }
367
    for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
5,670 ✔
368
      TAOS_CHECK_GOTO(tValueColumnCompressInfoDecode(&br, lastInfos + i), &lino, _exit);
2,835 ✔
369
    }
370

371
    for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
5,670 ✔
372
      SValueColumnCompressInfo *info = firstInfos + i;
2,835 ✔
373

374
      TAOS_CHECK_GOTO(tValueColumnDecompress(BR_PTR(&br), info, brinBlock->firstKeyPKs + i, assist), &lino, _exit);
2,835 ✔
375
      br.offset += (info->offsetCompressedSize + info->dataCompressedSize);
2,835 ✔
376
    }
377

378
    for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
5,670 ✔
379
      SValueColumnCompressInfo *info = lastInfos + i;
2,835 ✔
380

381
      TAOS_CHECK_GOTO(tValueColumnDecompress(BR_PTR(&br), info, brinBlock->lastKeyPKs + i, assist), &lino, _exit);
2,835 ✔
382
      br.offset += (info->offsetCompressedSize + info->dataCompressedSize);
2,835 ✔
383
    }
384
  }
385

386
  if (br.offset != br.buffer->size) {
57,350 ✔
387
    tsdbError("vgId:%d %s failed at %s:%d since brin block size mismatch, expected: %u, actual: %u, fname:%s",
×
388
              TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino, br.buffer->size, br.offset,
389
              reader->fd[TSDB_FTYPE_HEAD]->path);
390
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
445 ✔
391
  }
392

393
_exit:
57,362 ✔
394
  if (code) {
57,355 ✔
395
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
2 ✔
396
              tstrerror(code));
397
  }
398
  return code;
57,355 ✔
399
}
400

401
extern int32_t tBlockDataDecompress(SBufferReader *br, SBlockData *blockData, SBuffer *assist);
402

403
int32_t tsdbDataFileReadBlockData(SDataFileReader *reader, const SBrinRecord *record, SBlockData *bData) {
1,417 ✔
404
  int32_t code = 0;
1,417 ✔
405
  int32_t lino = 0;
1,417 ✔
406
  int32_t fid = reader->config->files[TSDB_FTYPE_DATA].file.fid;
1,417 ✔
407

408
  SBuffer *buffer = reader->buffers + 0;
1,417 ✔
409
  SBuffer *assist = reader->buffers + 1;
1,417 ✔
410

411
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
1,417 ✔
412

413
  // load data
414
  tBufferClear(buffer);
×
415
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA], record->blockOffset, record->blockSize, buffer, 0,
1,417 ✔
416
                                       pEncryptData),
417
                  &lino, _exit);
418

419
  // decompress
420
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer);
1,417 ✔
421
  TAOS_CHECK_GOTO(tBlockDataDecompress(&br, bData, assist), &lino, _exit);
1,417 ✔
422

423
  if (br.offset != buffer->size) {
1,417 ✔
424
    tsdbError("vgId:%d %s failed at %s:%d since block data size mismatch, expected: %u, actual: %u, fname:%s",
×
425
              TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, buffer->size, br.offset,
426
              reader->fd[TSDB_FTYPE_DATA]->path);
427
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
428
  }
429

430
_exit:
1,417 ✔
431
  if (code) {
1,417 ✔
432
    tsdbError("vgId:%d %s fid %d failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, fid,
×
433
              __FILE__, lino, tstrerror(code));
434
  }
435
  return code;
1,417 ✔
436
}
437

438
int32_t tsdbDataFileReadBlockDataByColumn(SDataFileReader *reader, const SBrinRecord *record, SBlockData *bData,
223,622 ✔
439
                                          STSchema *pTSchema, int16_t cids[], int32_t ncid) {
440
  int32_t code = 0;
223,622 ✔
441
  int32_t lino = 0;
223,622 ✔
442
  int32_t fid = reader->config->files[TSDB_FTYPE_DATA].file.fid;
223,629 ✔
443

444
  SDiskDataHdr hdr;
223,629 ✔
445
  SBuffer     *buffer0 = reader->buffers + 0;
223,686 ✔
446
  SBuffer     *buffer1 = reader->buffers + 1;
223,686 ✔
447
  SBuffer     *assist = reader->buffers + 2;
223,686 ✔
448

449
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
223,686 ✔
450

451
  // load key part
452
  tBufferClear(buffer0);
19,318 ✔
453
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA], record->blockOffset, record->blockKeySize, buffer0,
223,686 ✔
454
                                       0, pEncryptData),
455
                  &lino, _exit);
456

457
  // SDiskDataHdr
458
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
223,699 ✔
459
  TAOS_CHECK_GOTO(tGetDiskDataHdr(&br, &hdr), &lino, _exit);
223,695 ✔
460

461
  if (hdr.delimiter != TSDB_FILE_DLMT) {
223,648 ✔
462
    tsdbError("vgId:%d %s failed at %s:%d since disk data header delimiter is invalid, fname:%s",
×
463
              TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, reader->fd[TSDB_FTYPE_DATA]->path);
464
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
19,318 ✔
465
  }
466

467
  tBlockDataReset(bData);
223,648 ✔
468
  bData->suid = hdr.suid;
223,684 ✔
469
  bData->uid = hdr.uid;
223,684 ✔
470
  bData->nRow = hdr.nRow;
223,684 ✔
471

472
  // Key part
473
  TAOS_CHECK_GOTO(tBlockDataDecompressKeyPart(&hdr, &br, bData, assist), &lino, _exit);
223,684 ✔
474
  if (br.offset != buffer0->size) {
223,684 ✔
475
    tsdbError("vgId:%d %s failed at %s:%d since key part size mismatch, expected: %u, actual: %u, fname:%s",
×
476
              TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, buffer0->size, br.offset,
477
              reader->fd[TSDB_FTYPE_DATA]->path);
478
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
479
  }
480

481
  int extraColIdx = -1;
243,002 ✔
482
  for (int i = 0; i < ncid; i++) {
261,394 ✔
483
    if (tBlockDataGetColData(bData, cids[i]) == NULL) {
238,630 ✔
484
      extraColIdx = i;
181,573 ✔
485
      break;
181,573 ✔
486
    }
487
  }
488

489
  if (extraColIdx < 0) {
223,655 ✔
490
    goto _exit;
22,782 ✔
491
  }
492

493
  // load SBlockCol part
494
  tBufferClear(buffer0);
19,318 ✔
495
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA], record->blockOffset + record->blockKeySize,
200,873 ✔
496
                                       hdr.szBlkCol, buffer0, 0, pEncryptData),
497
                  &lino, _exit);
498

499
  // calc szHint
500
  int64_t szHint = 0;
200,907 ✔
501
  int     extraCols = 1;
200,907 ✔
502
  for (int i = extraColIdx + 1; i < ncid; ++i) {
200,896 ✔
503
    if (tBlockDataGetColData(bData, cids[i]) == NULL) {
92,173 ✔
504
      ++extraCols;
92,198 ✔
505
      break;
92,198 ✔
506
    }
507
  }
508

509
  if (extraCols >= 2) {
200,921 ✔
510
    br = BUFFER_READER_INITIALIZER(0, buffer0);
92,198 ✔
511

512
    SBlockCol blockCol = {.cid = 0};
92,198 ✔
513
    for (int32_t i = extraColIdx; i < ncid; ++i) {
92,200 ✔
514
      int16_t extraColCid = cids[i];
92,136 ✔
515

516
      while (extraColCid > blockCol.cid) {
293,507 ✔
517
        if (br.offset >= buffer0->size) {
201,328 ✔
518
          blockCol.cid = INT16_MAX;
×
519
          break;
×
520
        }
521

522
        TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
201,328 ✔
523
      }
524

525
      if (extraColCid == blockCol.cid || blockCol.cid == INT16_MAX) {
92,179 ✔
526
        extraColIdx = i;
92,179 ✔
527
        break;
92,179 ✔
528
      }
529
    }
530

531
    if (blockCol.cid > 0 && blockCol.cid < INT16_MAX /*&& blockCol->flag == HAS_VALUE*/) {
92,243 ✔
532
      int64_t   offset = blockCol.offset;
92,216 ✔
533
      SBlockCol lastNonNoneBlockCol = {.cid = 0};
92,216 ✔
534

535
      for (int32_t i = extraColIdx; i < ncid; ++i) {
499,959 ✔
536
        int16_t extraColCid = cids[i];
407,711 ✔
537

538
        while (extraColCid > blockCol.cid) {
1,086,841 ✔
539
          if (br.offset >= buffer0->size) {
679,098 ✔
540
            blockCol.cid = INT16_MAX;
9 ✔
541
            break;
9 ✔
542
          }
543

544
          TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
679,089 ✔
545
        }
546

547
        if (extraColCid == blockCol.cid) {
407,752 ✔
548
          lastNonNoneBlockCol = blockCol;
407,695 ✔
549
          continue;
407,695 ✔
550
        }
551

552
        if (blockCol.cid == INT16_MAX) {
57 ✔
553
          break;
9 ✔
554
        }
555
      }
556

557
      if (lastNonNoneBlockCol.cid > 0) {
92,257 ✔
558
        szHint = lastNonNoneBlockCol.offset + lastNonNoneBlockCol.szBitmap + lastNonNoneBlockCol.szOffset +
92,219 ✔
559
                 lastNonNoneBlockCol.szValue - offset;
92,219 ✔
560
      }
561
    }
562
  }
563

564
  // load each column
565
  SBlockCol blockCol = {
200,943 ✔
566
      .cid = 0,
567
  };
568
  bool firstRead = true;
200,930 ✔
569
  br = BUFFER_READER_INITIALIZER(0, buffer0);
200,930 ✔
570
  for (int32_t i = 0; i < ncid; i++) {
736,733 ✔
571
    int16_t cid = cids[i];
535,761 ✔
572

573
    if (tBlockDataGetColData(bData, cid)) {  // already loaded
535,761 ✔
574
      continue;
19,320 ✔
575
    }
576

577
    while (cid > blockCol.cid) {
1,745,995 ✔
578
      if (br.offset >= buffer0->size) {
1,229,508 ✔
579
        blockCol.cid = INT16_MAX;
10 ✔
580
        break;
10 ✔
581
      }
582

583
      TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
1,248,816 ✔
584
    }
585

586
    if (cid < blockCol.cid) {
516,497 ✔
587
      const STColumn *tcol = tTSchemaSearchColumn(pTSchema, cid);
10 ✔
588
      TSDB_CHECK_NULL(tcol, code, lino, _exit, TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER);
10 ✔
589
      SBlockCol none = {
10 ✔
590
          .cid = cid,
591
          .type = tcol->type,
10 ✔
592
          .cflag = tcol->flags,
10 ✔
593
          .flag = HAS_NONE,
594
          .szOrigin = 0,
595
          .szBitmap = 0,
596
          .szOffset = 0,
597
          .szValue = 0,
598
          .offset = 0,
599
      };
600
      TAOS_CHECK_GOTO(tBlockDataDecompressColData(&hdr, &none, &br, bData, assist), &lino, _exit);
10 ✔
601
    } else if (cid == blockCol.cid) {
516,487 ✔
602
      SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
516,452 ✔
603

604
      // load from file
605
      tBufferClear(buffer1);
19,318 ✔
606
      TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA],
516,452 ✔
607
                                           record->blockOffset + record->blockKeySize + hdr.szBlkCol + blockCol.offset,
608
                                           blockCol.szBitmap + blockCol.szOffset + blockCol.szValue, buffer1,
609
                                           firstRead ? szHint : 0, pEncryptData),
610
                      &lino, _exit);
611

612
      firstRead = false;
516,460 ✔
613

614
      // decode the buffer
615
      SBufferReader br1 = BUFFER_READER_INITIALIZER(0, buffer1);
516,460 ✔
616
      TAOS_CHECK_GOTO(tBlockDataDecompressColData(&hdr, &blockCol, &br1, bData, assist), &lino, _exit);
516,462 ✔
617
    }
618
  }
619

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

628
int32_t tsdbDataFileReadBlockSma(SDataFileReader *reader, const SBrinRecord *record,
65,039 ✔
629
                                 TColumnDataAggArray *columnDataAggArray) {
630
  int32_t  code = 0;
65,039 ✔
631
  int32_t  lino = 0;
65,039 ✔
632
  SBuffer *buffer = reader->buffers + 0;
65,038 ✔
633

634
  TARRAY2_CLEAR(columnDataAggArray, NULL);
65,038 ✔
635
  if (record->smaSize > 0) {
65,038 ✔
636
    tBufferClear(buffer);
×
637
    SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
65,046 ✔
638

639
    TAOS_CHECK_GOTO(
65,046 ✔
640
        tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_SMA], record->smaOffset, record->smaSize, buffer, 0, pEncryptData),
641
        &lino, _exit);
642

643
    // decode sma data
644
    SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer);
65,042 ✔
645
    while (br.offset < record->smaSize) {
393,838 ✔
646
      SColumnDataAgg sma[1];
328,784 ✔
647

648
      TAOS_CHECK_GOTO(tGetColumnDataAgg(&br, sma), &lino, _exit);
328,767 ✔
649
      TAOS_CHECK_GOTO(TARRAY2_APPEND_PTR(columnDataAggArray, sma), &lino, _exit);
657,621 ✔
650
    }
651
    if (br.offset != record->smaSize) {
65,054 ✔
652
      tsdbError("vgId:%d %s failed at %s:%d since sma data size mismatch, expected: %u, actual: %u, fname:%s",
×
653
                TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, record->smaSize, br.offset,
654
                reader->fd[TSDB_FTYPE_SMA]->path);
655
      TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
×
656
    }
657
  }
658

659
_exit:
65,033 ✔
660
  if (code) {
65,041 ✔
661
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
662
              tstrerror(code));
663
  }
664
  return code;
65,041 ✔
665
}
666

667
int32_t tsdbDataFileReadTombBlk(SDataFileReader *reader, const TTombBlkArray **tombBlkArray) {
9,868 ✔
668
  int32_t code = 0;
9,868 ✔
669
  int32_t lino = 0;
9,868 ✔
670
  void   *data = NULL;
9,868 ✔
671

672
  if (!reader->ctx->tombBlkLoaded) {
9,868 ✔
673
    TAOS_CHECK_GOTO(tsdbDataFileReadTombFooter(reader), &lino, _exit);
5,965 ✔
674

675
    if (reader->tombFooter->tombBlkPtr->size > 0) {
5,966 ✔
676
      if ((data = taosMemoryMalloc(reader->tombFooter->tombBlkPtr->size)) == NULL) {
5,560 ✔
677
        TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
678
      }
679

680
      SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
5,560 ✔
681

682
      TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[TSDB_FTYPE_TOMB], reader->tombFooter->tombBlkPtr->offset, data,
5,560 ✔
683
                                   reader->tombFooter->tombBlkPtr->size, 0, pEncryptData),
684
                      &lino, _exit);
685

686
      int32_t size = reader->tombFooter->tombBlkPtr->size / sizeof(STombBlk);
5,560 ✔
687
      TARRAY2_INIT_EX(reader->tombBlkArray, size, size, data);
5,560 ✔
688
    } else {
689
      TARRAY2_INIT(reader->tombBlkArray);
406 ✔
690
    }
691

692
    reader->ctx->tombBlkLoaded = true;
5,966 ✔
693
  }
694
  tombBlkArray[0] = reader->tombBlkArray;
9,869 ✔
695

696
_exit:
9,822 ✔
697
  if (code) {
9,869 ✔
698
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
699
              tstrerror(code));
700
    taosMemoryFree(data);
×
701
  }
702
  return code;
9,869 ✔
703
}
704

705
int32_t tsdbDataFileReadTombBlock(SDataFileReader *reader, const STombBlk *tombBlk, STombBlock *tData) {
7,371 ✔
706
  int32_t code = 0;
7,371 ✔
707
  int32_t lino = 0;
7,371 ✔
708

709
  SBuffer *buffer0 = reader->buffers + 0;
7,371 ✔
710
  SBuffer *assist = reader->buffers + 1;
7,371 ✔
711

712
  tBufferClear(buffer0);
94 ✔
713
  SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
7,371 ✔
714

715
  TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_TOMB], tombBlk->dp->offset, tombBlk->dp->size, buffer0, 0,
7,371 ✔
716
                                       pEncryptData),
717
                  &lino, _exit);
718

719
  int32_t       size = 0;
7,371 ✔
720
  SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
7,371 ✔
721
  tTombBlockClear(tData);
7,371 ✔
722
  tData->numOfRecords = tombBlk->numRec;
7,371 ✔
723
  for (int32_t i = 0; i < ARRAY_SIZE(tData->buffers); ++i) {
44,226 ✔
724
    SCompressInfo cinfo = {
36,855 ✔
725
        .cmprAlg = tombBlk->cmprAlg,
36,855 ✔
726
        .dataType = TSDB_DATA_TYPE_BIGINT,
727
        .originalSize = tombBlk->numRec * sizeof(int64_t),
36,855 ✔
728
        .compressedSize = tombBlk->size[i],
36,855 ✔
729
    };
730
    TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, tData->buffers + i, assist), &lino, _exit);
36,855 ✔
731
    br.offset += tombBlk->size[i];
36,855 ✔
732
  }
733

734
_exit:
7,371 ✔
735
  if (code) {
7,371 ✔
736
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
×
737
              tstrerror(code));
738
  }
739
  return code;
7,371 ✔
740
}
741

742
// SDataFileWriter =============================================
743
struct SDataFileWriter {
744
  SDataFileWriterConfig config[1];
745

746
  SSkmInfo skmTb[1];
747
  SSkmInfo skmRow[1];
748
  SBuffer  local[10];
749
  SBuffer *buffers;
750

751
  struct {
752
    bool             opened;
753
    SDataFileReader *reader;
754

755
    // for ts data
756
    TABLEID tbid[1];
757
    bool    tbHasOldData;
758

759
    const TBrinBlkArray *brinBlkArray;
760
    int32_t              brinBlkArrayIdx;
761
    SBrinBlock           brinBlock[1];
762
    int32_t              brinBlockIdx;
763
    SBlockData           blockData[1];
764
    int32_t              blockDataIdx;
765
    // for tomb data
766
    bool                 hasOldTomb;
767
    const TTombBlkArray *tombBlkArray;
768
    int32_t              tombBlkArrayIdx;
769
    STombBlock           tombBlock[1];
770
    int32_t              tombBlockIdx;
771
    // range
772
    SVersionRange range;
773
    SVersionRange tombRange;
774
  } ctx[1];
775

776
  STFile   files[TSDB_FTYPE_MAX];
777
  STsdbFD *fd[TSDB_FTYPE_MAX];
778

779
  SHeadFooter headFooter[1];
780
  STombFooter tombFooter[1];
781

782
  TBrinBlkArray brinBlkArray[1];
783
  SBrinBlock    brinBlock[1];
784
  SBlockData    blockData[1];
785

786
  TTombBlkArray tombBlkArray[1];
787
  STombBlock    tombBlock[1];
788
};
789

790
static int32_t tsdbDataFileWriterCloseAbort(SDataFileWriter *writer) {
×
791
  tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, __LINE__,
×
792
            "not implemented");
793
  return 0;
×
794
}
795

796
static void tsdbDataFileWriterDoClose(SDataFileWriter *writer) {
1,805 ✔
797
  if (writer->ctx->reader) {
1,805 ✔
798
    tsdbDataFileReaderClose(&writer->ctx->reader);
677 ✔
799
  }
800

801
  tTombBlockDestroy(writer->tombBlock);
1,801 ✔
802
  TARRAY2_DESTROY(writer->tombBlkArray, NULL);
1,804 ✔
803
  tBlockDataDestroy(writer->blockData);
1,804 ✔
804
  tBrinBlockDestroy(writer->brinBlock);
1,802 ✔
805
  TARRAY2_DESTROY(writer->brinBlkArray, NULL);
1,807 ✔
806

807
  tTombBlockDestroy(writer->ctx->tombBlock);
1,804 ✔
808
  tBlockDataDestroy(writer->ctx->blockData);
1,807 ✔
809
  tBrinBlockDestroy(writer->ctx->brinBlock);
1,810 ✔
810

811
  for (int32_t i = 0; i < ARRAY_SIZE(writer->local); ++i) {
19,878 ✔
812
    tBufferDestroy(writer->local + i);
18,693 ✔
813
  }
814

815
  tDestroyTSchema(writer->skmRow->pTSchema);
1,809 ✔
816
  tDestroyTSchema(writer->skmTb->pTSchema);
1,809 ✔
817
}
1,809 ✔
818

819
static int32_t tsdbDataFileWriterDoOpenReader(SDataFileWriter *writer) {
1,810 ✔
820
  int32_t code = 0;
1,810 ✔
821
  int32_t lino = 0;
1,810 ✔
822

823
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
6,891 ✔
824
    if (writer->config->files[i].exist) {
5,758 ✔
825
      SDataFileReaderConfig config[1] = {{
677 ✔
826
          .tsdb = writer->config->tsdb,
677 ✔
827
          .szPage = writer->config->szPage,
677 ✔
828
          .buffers = writer->buffers,
677 ✔
829
      }};
830

831
      for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
3,385 ✔
832
        config->files[i].exist = writer->config->files[i].exist;
2,708 ✔
833
        if (config->files[i].exist) {
2,708 ✔
834
          config->files[i].file = writer->config->files[i].file;
1,667 ✔
835
        }
836
      }
837

838
      TAOS_CHECK_GOTO(tsdbDataFileReaderOpen(NULL, config, &writer->ctx->reader), &lino, _exit);
677 ✔
839
      break;
677 ✔
840
    }
841
  }
842

843
_exit:
1,810 ✔
844
  if (code) {
1,654 ✔
845
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
846
              tstrerror(code));
847
  }
848
  return code;
1,810 ✔
849
}
850

851
static int32_t tsdbDataFileWriterDoOpen(SDataFileWriter *writer) {
1,810 ✔
852
  int32_t code = 0;
1,810 ✔
853
  int32_t lino = 0;
1,810 ✔
854
  int32_t ftype;
156 ✔
855
  SDiskID diskId = {0};
1,810 ✔
856

857
  if (!writer->config->skmTb) writer->config->skmTb = writer->skmTb;
1,810 ✔
858
  if (!writer->config->skmRow) writer->config->skmRow = writer->skmRow;
1,810 ✔
859
  writer->buffers = writer->config->buffers;
1,810 ✔
860
  if (writer->buffers == NULL) {
1,810 ✔
861
    writer->buffers = writer->local;
156 ✔
862
  }
863

864
  // open reader
865
  TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpenReader(writer), &lino, _exit);
1,810 ✔
866

867
  // .head
868
  ftype = TSDB_FTYPE_HEAD;
1,810 ✔
869
  code = tsdbAllocateDisk(writer->config->tsdb, tsdbFTypeLabel(ftype), writer->config->expLevel, &diskId);
1,810 ✔
870
  TSDB_CHECK_CODE(code, lino, _exit);
1,809 ✔
871
  writer->files[ftype] = (STFile){
1,809 ✔
872
      .type = ftype,
873
      .did = diskId,
874
      .fid = writer->config->fid,
1,809 ✔
875
      .cid = writer->config->cid,
1,809 ✔
876
      .size = 0,
877
      .minVer = VERSION_MAX,
878
      .maxVer = VERSION_MIN,
879
  };
880

881
  // .data
882
  ftype = TSDB_FTYPE_DATA;
1,809 ✔
883
  if (writer->config->files[ftype].exist) {
1,809 ✔
884
    writer->files[ftype] = writer->config->files[ftype].file;
494 ✔
885
  } else {
886
    code = tsdbAllocateDisk(writer->config->tsdb, tsdbFTypeLabel(ftype), writer->config->expLevel, &diskId);
1,315 ✔
887
    TSDB_CHECK_CODE(code, lino, _exit);
1,316 ✔
888
    writer->files[ftype] = (STFile){
2,632 ✔
889
        .type = ftype,
890
        .did = diskId,
891
        .fid = writer->config->fid,
1,316 ✔
892
        .cid = writer->config->cid,
1,316 ✔
893
        .size = 0,
894
        .lcn = writer->config->lcn == 0 ? -1 : 0,
1,316 ✔
895
        .minVer = VERSION_MAX,
896
        .maxVer = VERSION_MIN,
897
    };
898
  }
899

900
  // .sma
901
  ftype = TSDB_FTYPE_SMA;
1,810 ✔
902
  if (writer->config->files[ftype].exist) {
1,810 ✔
903
    writer->files[ftype] = writer->config->files[ftype].file;
494 ✔
904
  } else {
905
    code = tsdbAllocateDisk(writer->config->tsdb, tsdbFTypeLabel(ftype), writer->config->expLevel, &diskId);
1,316 ✔
906
    TSDB_CHECK_CODE(code, lino, _exit);
1,316 ✔
907
    writer->files[ftype] = (STFile){
1,316 ✔
908
        .type = ftype,
909
        .did = diskId,
910
        .fid = writer->config->fid,
1,316 ✔
911
        .cid = writer->config->cid,
1,316 ✔
912
        .size = 0,
913
        .minVer = VERSION_MAX,
914
        .maxVer = VERSION_MIN,
915
    };
916
  }
917

918
  // .tomb
919
  ftype = TSDB_FTYPE_TOMB;
1,810 ✔
920
  code = tsdbAllocateDisk(writer->config->tsdb, tsdbFTypeLabel(ftype), writer->config->expLevel, &diskId);
1,810 ✔
921
  TSDB_CHECK_CODE(code, lino, _exit);
1,810 ✔
922
  writer->files[ftype] = (STFile){
1,810 ✔
923
      .type = ftype,
924
      .did = diskId,
925
      .fid = writer->config->fid,
1,810 ✔
926
      .cid = writer->config->cid,
1,810 ✔
927
      .size = 0,
928
      .minVer = VERSION_MAX,
929
      .maxVer = VERSION_MIN,
930
  };
931

932
  // range
933
  writer->ctx->range = (SVersionRange){.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
1,810 ✔
934
  writer->ctx->tombRange = (SVersionRange){.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
1,810 ✔
935

936
  writer->ctx->opened = true;
1,810 ✔
937

938
_exit:
1,810 ✔
939
  if (code) {
1,810 ✔
940
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
941
              tstrerror(code));
942
  }
943
  return code;
1,810 ✔
944
}
945

946
void tsdbWriterUpdVerRange(SVersionRange *range, int64_t minVer, int64_t maxVer) {
248,672 ✔
947
  range->minVer = TMIN(range->minVer, minVer);
248,672 ✔
948
  range->maxVer = TMAX(range->maxVer, maxVer);
248,672 ✔
949
}
248,672 ✔
950

951
int32_t tsdbFileWriteBrinBlock(STsdbFD *fd, SBrinBlock *brinBlock, uint32_t cmprAlg, int64_t *fileSize,
3,637 ✔
952
                               TBrinBlkArray *brinBlkArray, SBuffer *buffers, SVersionRange *range,
953
                               SEncryptData *encryptData) {
954
  if (brinBlock->numOfRecords == 0) {
3,637 ✔
955
    return 0;
×
956
  }
957

958
  int32_t  code;
212 ✔
959
  SBuffer *buffer0 = buffers + 0;
3,637 ✔
960
  SBuffer *buffer1 = buffers + 1;
3,637 ✔
961
  SBuffer *assist = buffers + 2;
3,637 ✔
962

963
  SBrinBlk brinBlk = {
3,637 ✔
964
      .dp[0] =
965
          {
966
              .offset = *fileSize,
3,637 ✔
967
              .size = 0,
968
          },
969
      .numRec = brinBlock->numOfRecords,
3,425 ✔
970
      .numOfPKs = brinBlock->numOfPKs,
3,637 ✔
971
      .cmprAlg = cmprAlg,
972
  };
973
  for (int i = 0; i < brinBlock->numOfRecords; i++) {
584,487 ✔
974
    SBrinRecord record;
580,849 ✔
975

976
    TAOS_CHECK_RETURN(tBrinBlockGet(brinBlock, i, &record));
580,849 ✔
977
    if (i == 0) {
580,850 ✔
978
      brinBlk.minTbid.suid = record.suid;
3,637 ✔
979
      brinBlk.minTbid.uid = record.uid;
3,637 ✔
980
      brinBlk.minVer = record.minVer;
3,637 ✔
981
      brinBlk.maxVer = record.maxVer;
3,637 ✔
982
    }
983
    if (i == brinBlock->numOfRecords - 1) {
580,850 ✔
984
      brinBlk.maxTbid.suid = record.suid;
3,637 ✔
985
      brinBlk.maxTbid.uid = record.uid;
3,637 ✔
986
    }
987
    if (record.minVer < brinBlk.minVer) {
580,850 ✔
988
      brinBlk.minVer = record.minVer;
2,492 ✔
989
    }
990
    if (record.maxVer > brinBlk.maxVer) {
580,850 ✔
991
      brinBlk.maxVer = record.maxVer;
217,935 ✔
992
    }
993
  }
994

995
  tsdbWriterUpdVerRange(range, brinBlk.minVer, brinBlk.maxVer);
3,638 ✔
996

997
  // write to file
998
  for (int32_t i = 0; i < 10; ++i) {
40,006 ✔
999
    SCompressInfo info = {
36,369 ✔
1000
        .cmprAlg = cmprAlg,
1001
        .dataType = TSDB_DATA_TYPE_BIGINT,
1002
        .originalSize = brinBlock->buffers[i].size,
36,369 ✔
1003
    };
1004

1005
    tBufferClear(buffer0);
2,120 ✔
1006
    TAOS_CHECK_RETURN(tCompressDataToBuffer(brinBlock->buffers[i].data, &info, buffer0, assist));
36,369 ✔
1007
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptData));
36,369 ✔
1008
    brinBlk.size[i] = info.compressedSize;
36,369 ✔
1009
    brinBlk.dp->size += info.compressedSize;
36,369 ✔
1010
    *fileSize += info.compressedSize;
36,369 ✔
1011
  }
1012
  for (int32_t i = 10; i < 15; ++i) {
21,821 ✔
1013
    SCompressInfo info = {
18,184 ✔
1014
        .cmprAlg = cmprAlg,
1015
        .dataType = TSDB_DATA_TYPE_INT,
1016
        .originalSize = brinBlock->buffers[i].size,
18,184 ✔
1017
    };
1018

1019
    tBufferClear(buffer0);
1,060 ✔
1020
    TAOS_CHECK_RETURN(tCompressDataToBuffer(brinBlock->buffers[i].data, &info, buffer0, assist));
18,184 ✔
1021
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptData));
18,185 ✔
1022
    brinBlk.size[i] = info.compressedSize;
18,184 ✔
1023
    brinBlk.dp->size += info.compressedSize;
18,184 ✔
1024
    *fileSize += info.compressedSize;
18,184 ✔
1025
  }
1026

1027
  // write primary keys to file
1028
  if (brinBlock->numOfPKs > 0) {
3,637 ✔
1029
    tBufferClear(buffer0);
212 ✔
1030
    tBufferClear(buffer1);
212 ✔
1031

1032
    // encode
1033
    for (int i = 0; i < brinBlock->numOfPKs; i++) {
4,568 ✔
1034
      SValueColumnCompressInfo info = {.cmprAlg = cmprAlg};
2,284 ✔
1035
      TAOS_CHECK_RETURN(tValueColumnCompress(&brinBlock->firstKeyPKs[i], &info, buffer1, assist));
2,284 ✔
1036
      TAOS_CHECK_RETURN(tValueColumnCompressInfoEncode(&info, buffer0));
2,284 ✔
1037
    }
1038
    for (int i = 0; i < brinBlock->numOfPKs; i++) {
4,568 ✔
1039
      SValueColumnCompressInfo info = {.cmprAlg = cmprAlg};
2,284 ✔
1040
      TAOS_CHECK_RETURN(tValueColumnCompress(&brinBlock->lastKeyPKs[i], &info, buffer1, assist));
2,284 ✔
1041
      TAOS_CHECK_RETURN(tValueColumnCompressInfoEncode(&info, buffer0));
2,284 ✔
1042
    }
1043

1044
    // write to file
1045
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptData));
2,284 ✔
1046
    *fileSize += buffer0->size;
2,284 ✔
1047
    brinBlk.dp->size += buffer0->size;
2,284 ✔
1048
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer1->data, buffer1->size, encryptData));
2,284 ✔
1049
    *fileSize += buffer1->size;
2,284 ✔
1050
    brinBlk.dp->size += buffer1->size;
2,284 ✔
1051
  }
1052

1053
  // append to brinBlkArray
1054
  TAOS_CHECK_RETURN(TARRAY2_APPEND_PTR(brinBlkArray, &brinBlk));
7,062 ✔
1055

1056
  tBrinBlockClear(brinBlock);
3,637 ✔
1057

1058
  return 0;
3,637 ✔
1059
}
1060

1061
static int32_t tsdbDataFileWriteBrinBlock(SDataFileWriter *writer) {
3,637 ✔
1062
  if (writer->brinBlock->numOfRecords == 0) {
3,637 ✔
1063
    return 0;
×
1064
  }
1065

1066
  int32_t code = 0;
3,637 ✔
1067
  int32_t lino = 0;
3,637 ✔
1068

1069
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
3,637 ✔
1070

1071
  TAOS_CHECK_GOTO(tsdbFileWriteBrinBlock(writer->fd[TSDB_FTYPE_HEAD], writer->brinBlock, writer->config->cmprAlg,
3,637 ✔
1072
                                         &writer->files[TSDB_FTYPE_HEAD].size, writer->brinBlkArray, writer->buffers,
1073
                                         &writer->ctx->range, pEncryptData),
1074
                  &lino, _exit);
1075

1076
_exit:
3,637 ✔
1077
  if (code) {
3,425 ✔
1078
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1079
              tstrerror(code));
1080
  }
1081
  return code;
3,425 ✔
1082
}
1083

1084
static int32_t tsdbDataFileWriteBrinRecord(SDataFileWriter *writer, const SBrinRecord *record) {
580,844 ✔
1085
  int32_t code = 0;
580,844 ✔
1086
  int32_t lino = 0;
580,844 ✔
1087

1088
  for (;;) {
14,892 ✔
1089
    code = tBrinBlockPut(writer->brinBlock, record);
580,845 ✔
1090
    if (code == TSDB_CODE_INVALID_PARA) {
580,831 ✔
1091
      // different records with different primary keys
1092
      TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlock(writer), &lino, _exit);
3 ✔
1093
      continue;
×
1094
    } else {
1095
      TSDB_CHECK_CODE(code, lino, _exit);
580,828 ✔
1096
    }
1097
    break;
580,828 ✔
1098
  }
1099

1100
  if ((writer->brinBlock->numOfRecords) >= 256) {
580,828 ✔
1101
    TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlock(writer), &lino, _exit);
2,083 ✔
1102
  }
1103

1104
_exit:
580,828 ✔
1105
  if (code) {
580,770 ✔
1106
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1107
              tstrerror(code));
1108
  }
1109
  return code;
580,826 ✔
1110
}
1111

1112
static int32_t tsdbDataFileDoWriteBlockData(SDataFileWriter *writer, SBlockData *bData) {
146,977 ✔
1113
  if (bData->nRow == 0) {
146,977 ✔
1114
    return 0;
29,097 ✔
1115
  }
1116

1117
  if (!bData->uid) {
117,568 ✔
1118
    return TSDB_CODE_INVALID_PARA;
×
1119
  }
1120

1121
  int32_t  code = 0;
117,568 ✔
1122
  int32_t  lino = 0;
117,568 ✔
1123
  SBuffer *buffers = writer->buffers;
117,578 ✔
1124
  SBuffer *assist = writer->buffers + 4;
117,578 ✔
1125

1126
  SColCompressInfo cmprInfo = {.pColCmpr = NULL, .defaultCmprAlg = writer->config->cmprAlg};
117,578 ✔
1127

1128
  SBrinRecord record[1] = {{
117,577 ✔
1129
      .suid = bData->suid,
117,573 ✔
1130
      .uid = bData->uid,
102,681 ✔
1131
      .minVer = bData->aVersion[0],
117,573 ✔
1132
      .maxVer = bData->aVersion[0],
102,681 ✔
1133
      .blockOffset = writer->files[TSDB_FTYPE_DATA].size,
117,573 ✔
1134
      .smaOffset = writer->files[TSDB_FTYPE_SMA].size,
117,573 ✔
1135
      .blockSize = 0,
1136
      .blockKeySize = 0,
1137
      .smaSize = 0,
1138
      .numRow = bData->nRow,
102,681 ✔
1139
      .count = 1,
1140
  }};
1141

1142
  tsdbRowGetKey(&tsdbRowFromBlockData(bData, 0), &record->firstKey);
117,573 ✔
1143
  tsdbRowGetKey(&tsdbRowFromBlockData(bData, bData->nRow - 1), &record->lastKey);
117,579 ✔
1144

1145
  for (int32_t i = 1; i < bData->nRow; ++i) {
218,515,394 ✔
1146
    if (tsdbRowCompareWithoutVersion(&tsdbRowFromBlockData(bData, i - 1), &tsdbRowFromBlockData(bData, i)) != 0) {
218,397,812 ✔
1147
      record->count++;
218,396,237 ✔
1148
    }
1149
    if (bData->aVersion[i] < record->minVer) {
218,380,955 ✔
1150
      record->minVer = bData->aVersion[i];
861 ✔
1151
    }
1152
    if (bData->aVersion[i] > record->maxVer) {
218,380,955 ✔
1153
      record->maxVer = bData->aVersion[i];
256,934 ✔
1154
    }
1155
  }
1156

1157
  tsdbWriterUpdVerRange(&writer->ctx->range, record->minVer, record->maxVer);
117,582 ✔
1158

1159
  code = metaGetColCmpr(writer->config->tsdb->pVnode->pMeta, bData->suid != 0 ? bData->suid : bData->uid,
117,581 ✔
1160
                        &cmprInfo.pColCmpr);
1161
  if (code) {
117,582 ✔
1162
    tsdbWarn("vgId:%d failed to get column compress algorithm", TD_VID(writer->config->tsdb->pVnode));
×
1163
  }
1164

1165
  TAOS_CHECK_GOTO(tBlockDataCompress(bData, &cmprInfo, buffers, assist), &lino, _exit);
117,582 ✔
1166

1167
  record->blockKeySize = buffers[0].size + buffers[1].size;
117,568 ✔
1168

1169
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
117,568 ✔
1170
  for (int i = 0; i < 4; i++) {
587,819 ✔
1171
    tsdbThrottleConsume(writer->config->throttle, buffers[i].size);
470,259 ✔
1172
    TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd[TSDB_FTYPE_DATA], writer->files[TSDB_FTYPE_DATA].size, buffers[i].data,
470,238 ✔
1173
                                  buffers[i].size, pEncryptData),
1174
                    &lino, _exit);
1175
    writer->files[TSDB_FTYPE_DATA].size += buffers[i].size;
470,251 ✔
1176
    record->blockSize += buffers[i].size;
470,251 ✔
1177
  }
1178

1179
  // to .sma file
1180
  tBufferClear(&buffers[0]);
14,892 ✔
1181
  for (int32_t i = 0; i < bData->nColData; ++i) {
972,096 ✔
1182
    SColData *colData = bData->aColData + i;
854,541 ✔
1183
    if ((colData->cflag & COL_SMA_ON) == 0 || ((colData->flag & HAS_VALUE) == 0)) continue;
854,541 ✔
1184

1185
    SColumnDataAgg sma[1] = {{.colId = colData->cid}};
823,791 ✔
1186
    tColDataCalcSMA[colData->type](colData, sma);
823,797 ✔
1187

1188
    TAOS_CHECK_GOTO(tPutColumnDataAgg(&buffers[0], sma), &lino, _exit);
823,780 ✔
1189
  }
1190

1191
  record->smaSize = buffers[0].size;
117,555 ✔
1192
  if (record->smaSize > 0) {
117,555 ✔
1193
    tsdbThrottleConsume(writer->config->throttle, record->smaSize);
102,688 ✔
1194
    TAOS_CHECK_GOTO(
102,686 ✔
1195
        tsdbWriteFile(writer->fd[TSDB_FTYPE_SMA], record->smaOffset, buffers[0].data, record->smaSize, pEncryptData),
1196
        &lino, _exit);
1197
    writer->files[TSDB_FTYPE_SMA].size += record->smaSize;
102,686 ✔
1198
  }
1199

1200
  // append SBrinRecord
1201
  TAOS_CHECK_GOTO(tsdbDataFileWriteBrinRecord(writer, record), &lino, _exit);
117,553 ✔
1202

1203
  tBlockDataClear(bData);
117,564 ✔
1204

1205
_exit:
117,573 ✔
1206
  if (code) {
117,577 ✔
1207
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1208
              tstrerror(code));
1209
  }
1210
  taosHashCleanup(cmprInfo.pColCmpr);
117,577 ✔
1211
  return code;
117,574 ✔
1212
}
1213

1214
static int32_t tsdbDataFileDoWriteTSRow(SDataFileWriter *writer, TSDBROW *row) {
156,607,621 ✔
1215
  int32_t code = 0;
156,607,621 ✔
1216
  int32_t lino = 0;
156,607,621 ✔
1217

1218
  // update/append
1219
  if (row->type == TSDBROW_ROW_FMT) {
156,611,334 ✔
1220
    TAOS_CHECK_GOTO(
×
1221
        tsdbUpdateSkmRow(writer->config->tsdb, writer->ctx->tbid, TSDBROW_SVERSION(row), writer->config->skmRow), &lino,
1222
        _exit);
1223
  }
1224

1225
  if (TSDBROW_VERSION(row) <= writer->config->compactVersion  //
156,611,334 ✔
1226
      && writer->blockData->nRow > 0                          //
156,684,824 ✔
1227
      &&
156,484,071 ✔
1228
      tsdbRowCompareWithoutVersion(row, &tsdbRowFromBlockData(writer->blockData, writer->blockData->nRow - 1)) == 0  //
156,609,212 ✔
1229
  ) {
1230
    TAOS_CHECK_GOTO(tBlockDataUpdateRow(writer->blockData, row, writer->config->skmRow->pTSchema), &lino, _exit);
898,594 ✔
1231
  } else {
1232
    if (writer->blockData->nRow >= writer->config->maxRow) {
155,587,599 ✔
1233
      TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, writer->blockData), &lino, _exit);
400 ✔
1234
    }
1235

1236
    TAOS_CHECK_GOTO(
155,587,599 ✔
1237
        tBlockDataAppendRow(writer->blockData, row, writer->config->skmRow->pTSchema, writer->ctx->tbid->uid), &lino,
1238
        _exit);
1239
  }
1240

1241
_exit:
156,469,897 ✔
1242
  if (code) {
156,473,661 ✔
1243
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1244
              tstrerror(code));
1245
  }
1246
  return code;
156,473,661 ✔
1247
}
1248

1249
static FORCE_INLINE int32_t tsdbRowKeyCmprNullAsLargest(const STsdbRowKey *key1, const STsdbRowKey *key2) {
×
1250
  if (key1 == NULL) {
2,717,843 ✔
1251
    return 1;
2 ✔
1252
  } else if (key2 == NULL) {
2,717,841 ✔
1253
    return -1;
6,921 ✔
1254
  } else {
1255
    return tsdbRowKeyCmpr(key1, key2);
2,710,920 ✔
1256
  }
1257
}
1258

1259
static int32_t tsdbDataFileDoWriteTableOldData(SDataFileWriter *writer, const STsdbRowKey *key) {
908,856 ✔
1260
  if (writer->ctx->tbHasOldData == false) {
908,856 ✔
1261
    return 0;
×
1262
  }
1263

1264
  int32_t     code = 0;
908,856 ✔
1265
  int32_t     lino = 0;
908,856 ✔
1266
  STsdbRowKey rowKey;
908,922 ✔
1267

1268
  for (;;) {
9 ✔
1269
    for (;;) {
1270
      // SBlockData
1271
      for (; writer->ctx->blockDataIdx < writer->ctx->blockData->nRow; writer->ctx->blockDataIdx++) {
2,712,173 ✔
1272
        TSDBROW row = tsdbRowFromBlockData(writer->ctx->blockData, writer->ctx->blockDataIdx);
2,701,722 ✔
1273

1274
        tsdbRowGetKey(&row, &rowKey);
2,702,046 ✔
1275
        if (tsdbRowKeyCmprNullAsLargest(&rowKey, key) < 0) {  // key <= rowKey
2,700,741 ✔
1276
          TAOS_CHECK_GOTO(tsdbDataFileDoWriteTSRow(writer, &row), &lino, _exit);
1,801,343 ✔
1277
        } else {
1278
          goto _exit;
899,398 ✔
1279
        }
1280
      }
1281

1282
      // SBrinBlock
1283
      if (writer->ctx->brinBlockIdx >= writer->ctx->brinBlock->numOfRecords) {
10,451 ✔
1284
        break;
286 ✔
1285
      }
1286

1287
      for (; writer->ctx->brinBlockIdx < writer->ctx->brinBlock->numOfRecords; writer->ctx->brinBlockIdx++) {
14,944 ✔
1288
        SBrinRecord record;
10,071 ✔
1289
        code = tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, &record);
10,071 ✔
1290
        TSDB_CHECK_CODE(code, lino, _exit);
10,071 ✔
1291
        if (record.uid != writer->ctx->tbid->uid) {
10,071 ✔
1292
          writer->ctx->tbHasOldData = false;
462 ✔
1293
          goto _exit;
462 ✔
1294
        }
1295

1296
        if (tsdbRowKeyCmprNullAsLargest(key, &record.firstKey) < 0) {  // key < record->firstKey
9,609 ✔
1297
          goto _exit;
4,197 ✔
1298
        } else {
1299
          SBrinRecord record[1];
5,412 ✔
1300
          code = tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, record);
5,412 ✔
1301
          TSDB_CHECK_CODE(code, lino, _exit);
5,412 ✔
1302
          if (tsdbRowKeyCmprNullAsLargest(key, &record->lastKey) > 0) {  // key > record->lastKey
5,412 ✔
1303
            if (writer->blockData->nRow > 0) {
4,779 ✔
1304
              TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, writer->blockData), &lino, _exit);
1 ✔
1305
            }
1306

1307
            TAOS_CHECK_GOTO(tsdbDataFileWriteBrinRecord(writer, record), &lino, _exit);
4,779 ✔
1308
          } else {
1309
            TAOS_CHECK_GOTO(tsdbDataFileReadBlockData(writer->ctx->reader, record, writer->ctx->blockData), &lino,
633 ✔
1310
                            _exit);
1311

1312
            writer->ctx->blockDataIdx = 0;
633 ✔
1313
            writer->ctx->brinBlockIdx++;
633 ✔
1314
            break;
633 ✔
1315
          }
1316
        }
1317
      }
1318
    }
1319

1320
    // SBrinBlk
1321
    if (writer->ctx->brinBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->brinBlkArray)) {
286 ✔
1322
      writer->ctx->brinBlkArray = NULL;
274 ✔
1323
      writer->ctx->tbHasOldData = false;
274 ✔
1324
      goto _exit;
274 ✔
1325
    } else {
1326
      const SBrinBlk *brinBlk = TARRAY2_GET_PTR(writer->ctx->brinBlkArray, writer->ctx->brinBlkArrayIdx);
12 ✔
1327

1328
      if (brinBlk->minTbid.uid != writer->ctx->tbid->uid) {
12 ✔
1329
        writer->ctx->tbHasOldData = false;
3 ✔
1330
        goto _exit;
3 ✔
1331
      }
1332

1333
      TAOS_CHECK_GOTO(tsdbDataFileReadBrinBlock(writer->ctx->reader, brinBlk, writer->ctx->brinBlock), &lino, _exit);
9 ✔
1334

1335
      writer->ctx->brinBlockIdx = 0;
9 ✔
1336
      writer->ctx->brinBlkArrayIdx++;
9 ✔
1337
    }
1338
  }
1339

1340
_exit:
909,924 ✔
1341
  if (code) {
908,970 ✔
1342
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1343
              tstrerror(code));
1344
  }
1345
  return code;
908,979 ✔
1346
}
1347

1348
static int32_t tsdbDataFileDoWriteTSData(SDataFileWriter *writer, TSDBROW *row) {
154,728,071 ✔
1349
  int32_t code = 0;
154,728,071 ✔
1350
  int32_t lino = 0;
154,728,071 ✔
1351

1352
  if (writer->ctx->tbHasOldData) {
154,731,553 ✔
1353
    STsdbRowKey key;
908,925 ✔
1354
    tsdbRowGetKey(row, &key);
908,931 ✔
1355
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTableOldData(writer, &key), &lino, _exit);
908,686 ✔
1356
  }
1357

1358
  TAOS_CHECK_GOTO(tsdbDataFileDoWriteTSRow(writer, row), &lino, _exit);
154,731,119 ✔
1359

1360
_exit:
154,671,422 ✔
1361
  if (code) {
154,671,524 ✔
1362
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1363
              tstrerror(code));
1364
  }
1365
  return code;
154,671,524 ✔
1366
}
1367

1368
static int32_t tsdbDataFileWriteTableDataEnd(SDataFileWriter *writer) {
62,170 ✔
1369
  if (writer->ctx->tbid->uid == 0) {
62,170 ✔
1370
    return 0;
1,398 ✔
1371
  }
1372

1373
  int32_t code = 0;
60,616 ✔
1374
  int32_t lino = 0;
60,616 ✔
1375

1376
  if (writer->ctx->tbHasOldData) {
60,617 ✔
1377
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTableOldData(writer, NULL /* as the largest key */), &lino, _exit);
238 ✔
1378
  }
1379

1380
  TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, writer->blockData), &lino, _exit);
60,617 ✔
1381

1382
_exit:
60,613 ✔
1383
  if (code) {
60,301 ✔
1384
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1385
              tstrerror(code));
1386
  }
1387
  return code;
60,300 ✔
1388
}
1389

1390
static int32_t tsdbDataFileWriteTableDataBegin(SDataFileWriter *writer, const TABLEID *tbid) {
62,163 ✔
1391
  int32_t code = 0;
62,163 ✔
1392
  int32_t lino = 0;
62,163 ✔
1393

1394
  SMetaInfo info;
62,163 ✔
1395
  bool      drop = false;
62,163 ✔
1396
  TABLEID   tbid1[1];
62,163 ✔
1397
  writer->ctx->tbHasOldData = false;
62,165 ✔
1398
  while (writer->ctx->brinBlkArray) {  // skip data of previous table
64,307 ✔
1399
    for (; writer->ctx->brinBlockIdx < writer->ctx->brinBlock->numOfRecords; writer->ctx->brinBlockIdx++) {
488,105 ✔
1400
      SBrinRecord record;
485,811 ✔
1401
      TAOS_CHECK_GOTO(tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, &record), &lino, _exit);
485,811 ✔
1402

1403
      if (record.uid == tbid->uid) {
485,809 ✔
1404
        writer->ctx->tbHasOldData = true;
739 ✔
1405
        goto _begin;
739 ✔
1406
      } else if (record.suid > tbid->suid || (record.suid == tbid->suid && record.uid > tbid->uid)) {
485,070 ✔
1407
        goto _begin;
26,583 ✔
1408
      } else {
1409
        if (record.uid != writer->ctx->tbid->uid) {
458,487 ✔
1410
          if (drop && tbid1->uid == record.uid) {
229,623 ✔
1411
            continue;
×
1412
          } else if (metaGetInfo(writer->config->tsdb->pVnode->pMeta, record.uid, &info, NULL) != 0) {
229,623 ✔
1413
            drop = true;
2 ✔
1414
            tbid1->suid = record.suid;
2 ✔
1415
            tbid1->uid = record.uid;
2 ✔
1416
            continue;
2 ✔
1417
          } else {
1418
            drop = false;
229,622 ✔
1419
            writer->ctx->tbid->suid = record.suid;
229,622 ✔
1420
            writer->ctx->tbid->uid = record.uid;
229,622 ✔
1421
          }
1422
        }
1423

1424
        TAOS_CHECK_GOTO(tsdbDataFileWriteBrinRecord(writer, &record), &lino, _exit);
458,486 ✔
1425
      }
1426
    }
1427

1428
    if (writer->ctx->brinBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->brinBlkArray)) {
2,294 ✔
1429
      writer->ctx->brinBlkArray = NULL;
151 ✔
1430
      break;
151 ✔
1431
    } else {
1432
      const SBrinBlk *brinBlk = TARRAY2_GET_PTR(writer->ctx->brinBlkArray, writer->ctx->brinBlkArrayIdx);
2,143 ✔
1433

1434
      TAOS_CHECK_GOTO(tsdbDataFileReadBrinBlock(writer->ctx->reader, brinBlk, writer->ctx->brinBlock), &lino, _exit);
2,143 ✔
1435

1436
      writer->ctx->brinBlockIdx = 0;
2,142 ✔
1437
      writer->ctx->brinBlkArrayIdx++;
2,142 ✔
1438
    }
1439
  }
1440

1441
_begin:
62,164 ✔
1442
  writer->ctx->tbid[0] = *tbid;
62,165 ✔
1443

1444
  if (tbid->uid == INT64_MAX) {
62,165 ✔
1445
    goto _exit;
1,554 ✔
1446
  }
1447

1448
  TAOS_CHECK_GOTO(tsdbUpdateSkmTb(writer->config->tsdb, tbid, writer->config->skmTb), &lino, _exit);
60,611 ✔
1449
  TAOS_CHECK_GOTO(tBlockDataInit(writer->blockData, writer->ctx->tbid, writer->config->skmTb->pTSchema, NULL, 0), &lino,
60,610 ✔
1450
                  _exit);
1451

1452
_exit:
62,011 ✔
1453
  if (code) {
61,853 ✔
1454
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1455
              tstrerror(code));
1456
  }
1457
  return code;
62,165 ✔
1458
}
1459

1460
int32_t tsdbFileWriteHeadFooter(STsdbFD *fd, int64_t *fileSize, const SHeadFooter *footer, SEncryptData *encryptData) {
1,554 ✔
1461
  TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, (const uint8_t *)footer, sizeof(*footer), encryptData));
1,554 ✔
1462
  *fileSize += sizeof(*footer);
1,554 ✔
1463
  return 0;
1,554 ✔
1464
}
1465

1466
int32_t tsdbFileWriteTombBlock(STsdbFD *fd, STombBlock *tombBlock, int8_t cmprAlg, int64_t *fileSize,
3,205 ✔
1467
                               TTombBlkArray *tombBlkArray, SBuffer *buffers, SVersionRange *range,
1468
                               SEncryptData *encryptData) {
1469
  int32_t code;
41 ✔
1470

1471
  if (TOMB_BLOCK_SIZE(tombBlock) == 0) {
3,205 ✔
1472
    return 0;
×
1473
  }
1474

1475
  SBuffer *buffer0 = buffers + 0;
3,205 ✔
1476
  SBuffer *assist = buffers + 1;
3,205 ✔
1477

1478
  STombBlk tombBlk = {
3,205 ✔
1479
      .dp[0] =
1480
          {
1481
              .offset = *fileSize,
3,205 ✔
1482
              .size = 0,
1483
          },
1484
      .numRec = TOMB_BLOCK_SIZE(tombBlock),
3,164 ✔
1485
      .cmprAlg = cmprAlg,
1486
  };
1487
  for (int i = 0; i < TOMB_BLOCK_SIZE(tombBlock); i++) {
89,483 ✔
1488
    STombRecord record;
86,278 ✔
1489
    TAOS_CHECK_RETURN(tTombBlockGet(tombBlock, i, &record));
86,278 ✔
1490

1491
    if (i == 0) {
86,278 ✔
1492
      tombBlk.minTbid.suid = record.suid;
3,205 ✔
1493
      tombBlk.minTbid.uid = record.uid;
3,205 ✔
1494
      tombBlk.minVer = record.version;
3,205 ✔
1495
      tombBlk.maxVer = record.version;
3,205 ✔
1496
    }
1497
    if (i == TOMB_BLOCK_SIZE(tombBlock) - 1) {
86,278 ✔
1498
      tombBlk.maxTbid.suid = record.suid;
3,205 ✔
1499
      tombBlk.maxTbid.uid = record.uid;
3,205 ✔
1500
    }
1501
    if (record.version < tombBlk.minVer) {
86,278 ✔
1502
      tombBlk.minVer = record.version;
2,308 ✔
1503
    }
1504
    if (record.version > tombBlk.maxVer) {
86,278 ✔
1505
      tombBlk.maxVer = record.version;
10,517 ✔
1506
    }
1507
  }
1508

1509
  tsdbWriterUpdVerRange(range, tombBlk.minVer, tombBlk.maxVer);
3,205 ✔
1510

1511
  for (int32_t i = 0; i < ARRAY_SIZE(tombBlock->buffers); i++) {
19,230 ✔
1512
    tBufferClear(buffer0);
205 ✔
1513

1514
    SCompressInfo cinfo = {
16,025 ✔
1515
        .cmprAlg = cmprAlg,
1516
        .dataType = TSDB_DATA_TYPE_BIGINT,
1517
        .originalSize = tombBlock->buffers[i].size,
16,025 ✔
1518
    };
1519
    TAOS_CHECK_RETURN(tCompressDataToBuffer(tombBlock->buffers[i].data, &cinfo, buffer0, assist));
16,025 ✔
1520
    TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptData));
16,025 ✔
1521

1522
    tombBlk.size[i] = cinfo.compressedSize;
16,025 ✔
1523
    tombBlk.dp->size += tombBlk.size[i];
16,025 ✔
1524
    *fileSize += tombBlk.size[i];
16,025 ✔
1525
  }
1526

1527
  TAOS_CHECK_RETURN(TARRAY2_APPEND_PTR(tombBlkArray, &tombBlk));
6,369 ✔
1528

1529
  tTombBlockClear(tombBlock);
3,205 ✔
1530
  return 0;
3,205 ✔
1531
}
1532

1533
static int32_t tsdbDataFileWriteHeadFooter(SDataFileWriter *writer) {
1,554 ✔
1534
  int32_t code = 0;
1,554 ✔
1535
  int32_t lino = 0;
1,554 ✔
1536

1537
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
1,554 ✔
1538

1539
  TAOS_CHECK_GOTO(tsdbFileWriteHeadFooter(writer->fd[TSDB_FTYPE_HEAD], &writer->files[TSDB_FTYPE_HEAD].size,
1,554 ✔
1540
                                          writer->headFooter, pEncryptData),
1541
                  &lino, _exit);
1542

1543
_exit:
1,554 ✔
1544
  if (code) {
1,398 ✔
1545
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1546
              tstrerror(code));
1547
  }
1548
  return code;
1,554 ✔
1549
}
1550

1551
static int32_t tsdbDataFileDoWriteTombBlock(SDataFileWriter *writer) {
269 ✔
1552
  if (TOMB_BLOCK_SIZE(writer->tombBlock) == 0) return 0;
269 ✔
1553

1554
  int32_t code = 0;
269 ✔
1555
  int32_t lino = 0;
269 ✔
1556

1557
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
269 ✔
1558

1559
  TAOS_CHECK_GOTO(tsdbFileWriteTombBlock(writer->fd[TSDB_FTYPE_TOMB], writer->tombBlock, writer->config->cmprAlg,
269 ✔
1560
                                         &writer->files[TSDB_FTYPE_TOMB].size, writer->tombBlkArray, writer->buffers,
1561
                                         &writer->ctx->tombRange, pEncryptData),
1562
                  &lino, _exit);
1563

1564
_exit:
269 ✔
1565
  if (code) {
261 ✔
1566
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1567
              tstrerror(code));
1568
  }
1569
  return code;
261 ✔
1570
}
1571

1572
int32_t tsdbFileWriteTombBlk(STsdbFD *fd, const TTombBlkArray *tombBlkArray, SFDataPtr *ptr, int64_t *fileSize,
39,026 ✔
1573
                             SEncryptData *encryptData) {
1574
  ptr->size = TARRAY2_DATA_LEN(tombBlkArray);
39,026 ✔
1575
  if (ptr->size > 0) {
39,026 ✔
1576
    ptr->offset = *fileSize;
3,205 ✔
1577

1578
    TAOS_CHECK_RETURN(
3,205 ✔
1579
        tsdbWriteFile(fd, *fileSize, (const uint8_t *)TARRAY2_DATA(tombBlkArray), ptr->size, encryptData));
1580

1581
    *fileSize += ptr->size;
3,205 ✔
1582
  }
1583
  return 0;
38,901 ✔
1584
}
1585

1586
static int32_t tsdbDataFileDoWriteTombBlk(SDataFileWriter *writer) {
269 ✔
1587
  if (TARRAY2_SIZE(writer->tombBlkArray) <= 0) {
269 ✔
1588
    return TSDB_CODE_INVALID_PARA;
×
1589
  }
1590

1591
  int32_t code = 0;
269 ✔
1592
  int32_t lino = 0;
269 ✔
1593

1594
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
269 ✔
1595

1596
  TAOS_CHECK_GOTO(
269 ✔
1597
      tsdbFileWriteTombBlk(writer->fd[TSDB_FTYPE_TOMB], writer->tombBlkArray, writer->tombFooter->tombBlkPtr,
1598
                           &writer->files[TSDB_FTYPE_TOMB].size, pEncryptData),
1599
      &lino, _exit);
1600

1601
_exit:
269 ✔
1602
  if (code) {
261 ✔
1603
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1604
              tstrerror(code));
1605
  }
1606
  return code;
261 ✔
1607
}
1608

1609
int32_t tsdbFileWriteTombFooter(STsdbFD *fd, const STombFooter *footer, int64_t *fileSize, SEncryptData *encryptData) {
269 ✔
1610
  TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, (const uint8_t *)footer, sizeof(*footer), encryptData));
269 ✔
1611
  *fileSize += sizeof(*footer);
269 ✔
1612
  return 0;
269 ✔
1613
}
1614

1615
static int32_t tsdbDataFileWriteTombFooter(SDataFileWriter *writer) {
269 ✔
1616
  int32_t code = 0;
269 ✔
1617
  int32_t lino = 0;
269 ✔
1618

1619
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
269 ✔
1620

1621
  TAOS_CHECK_GOTO(tsdbFileWriteTombFooter(writer->fd[TSDB_FTYPE_TOMB], writer->tombFooter,
269 ✔
1622
                                          &writer->files[TSDB_FTYPE_TOMB].size, pEncryptData),
1623
                  &lino, _exit);
1624

1625
_exit:
269 ✔
1626
  if (code) {
261 ✔
1627
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1628
              tstrerror(code));
1629
  }
1630
  return code;
269 ✔
1631
}
1632

1633
static int32_t tsdbDataFileDoWriteTombRecord(SDataFileWriter *writer, const STombRecord *record) {
1,123 ✔
1634
  int32_t code = 0;
1,123 ✔
1635
  int32_t lino = 0;
1,123 ✔
1636

1637
  while (writer->ctx->hasOldTomb) {
1,306 ✔
1638
    for (; writer->ctx->tombBlockIdx < TOMB_BLOCK_SIZE(writer->ctx->tombBlock); writer->ctx->tombBlockIdx++) {
70,340 ✔
1639
      STombRecord record1[1];
69,974 ✔
1640
      TAOS_CHECK_GOTO(tTombBlockGet(writer->ctx->tombBlock, writer->ctx->tombBlockIdx, record1), &lino, _exit);
69,974 ✔
1641

1642
      int32_t c = tTombRecordCompare(record, record1);
69,974 ✔
1643
      if (c < 0) {
69,974 ✔
1644
        goto _write;
726 ✔
1645
      } else if (c > 0) {
69,248 ✔
1646
        TAOS_CHECK_GOTO(tTombBlockPut(writer->tombBlock, record1), &lino, _exit);
69,248 ✔
1647

1648
        tsdbTrace("vgId:%d write tomb record to tomb file:%s, cid:%" PRId64 ", suid:%" PRId64 ", uid:%" PRId64
69,248 ✔
1649
                  ", version:%" PRId64,
1650
                  TD_VID(writer->config->tsdb->pVnode), writer->fd[TSDB_FTYPE_TOMB]->path, writer->config->cid,
1651
                  record1->suid, record1->uid, record1->version);
1652

1653
        if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
69,248 ✔
1654
          TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlock(writer), &lino, _exit);
×
1655
        }
1656
      } else {
1657
        tsdbError("vgId:%d duplicate tomb record, cid:%" PRId64 ", suid:%" PRId64 ", uid:%" PRId64 ", version:%" PRId64,
×
1658
                  TD_VID(writer->config->tsdb->pVnode), writer->config->cid, record->suid, record->uid,
1659
                  record->version);
1660
      }
1661
    }
1662

1663
    if (writer->ctx->tombBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->tombBlkArray)) {
366 ✔
1664
      writer->ctx->hasOldTomb = false;
183 ✔
1665
      break;
183 ✔
1666
    } else {
1667
      const STombBlk *tombBlk = TARRAY2_GET_PTR(writer->ctx->tombBlkArray, writer->ctx->tombBlkArrayIdx);
183 ✔
1668

1669
      TAOS_CHECK_GOTO(tsdbDataFileReadTombBlock(writer->ctx->reader, tombBlk, writer->ctx->tombBlock), &lino, _exit);
183 ✔
1670

1671
      writer->ctx->tombBlockIdx = 0;
183 ✔
1672
      writer->ctx->tombBlkArrayIdx++;
183 ✔
1673
    }
1674
  }
1675

1676
_write:
1,123 ✔
1677
  if (record->suid == INT64_MAX) {
1,123 ✔
1678
    goto _exit;
269 ✔
1679
  }
1680

1681
  TAOS_CHECK_GOTO(tTombBlockPut(writer->tombBlock, record), &lino, _exit);
854 ✔
1682

1683
  tsdbTrace("vgId:%d write tomb record to tomb file:%s, cid:%" PRId64 ", suid:%" PRId64 ", uid:%" PRId64
854 ✔
1684
            ", version:%" PRId64,
1685
            TD_VID(writer->config->tsdb->pVnode), writer->fd[TSDB_FTYPE_TOMB]->path, writer->config->cid, record->suid,
1686
            record->uid, record->version);
1687

1688
  if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
854 ✔
1689
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlock(writer), &lino, _exit);
×
1690
  }
1691

1692
_exit:
1,115 ✔
1693
  if (code) {
1,123 ✔
1694
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1695
              tstrerror(code));
1696
  }
1697
  return code;
1,123 ✔
1698
}
1699

1700
int32_t tsdbFileWriteBrinBlk(STsdbFD *fd, TBrinBlkArray *brinBlkArray, SFDataPtr *ptr, int64_t *fileSize,
1,554 ✔
1701
                             SEncryptData *encryptData) {
1702
  if (TARRAY2_SIZE(brinBlkArray) <= 0) {
1,554 ✔
1703
    return TSDB_CODE_INVALID_PARA;
×
1704
  }
1705
  ptr->offset = *fileSize;
1,554 ✔
1706
  ptr->size = TARRAY2_DATA_LEN(brinBlkArray);
1,554 ✔
1707

1708
  TAOS_CHECK_RETURN(tsdbWriteFile(fd, ptr->offset, (uint8_t *)TARRAY2_DATA(brinBlkArray), ptr->size, encryptData));
1,554 ✔
1709

1710
  *fileSize += ptr->size;
1,554 ✔
1711
  return 0;
1,554 ✔
1712
}
1713

1714
static int32_t tsdbDataFileWriteBrinBlk(SDataFileWriter *writer) {
1,554 ✔
1715
  int32_t code = 0;
1,554 ✔
1716
  int32_t lino = 0;
1,554 ✔
1717

1718
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
1,554 ✔
1719

1720
  TAOS_CHECK_GOTO(
1,554 ✔
1721
      tsdbFileWriteBrinBlk(writer->fd[TSDB_FTYPE_HEAD], writer->brinBlkArray, writer->headFooter->brinBlkPtr,
1722
                           &writer->files[TSDB_FTYPE_HEAD].size, pEncryptData),
1723
      &lino, _exit);
1724

1725
_exit:
1,554 ✔
1726
  if (code) {
1,398 ✔
1727
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1728
              tstrerror(code));
1729
  }
1730
  return code;
1,554 ✔
1731
}
1732

1733
void tsdbTFileUpdVerRange(STFile *f, SVersionRange range) {
45,496 ✔
1734
  f->minVer = TMIN(f->minVer, range.minVer);
45,496 ✔
1735
  f->maxVer = TMAX(f->maxVer, range.maxVer);
45,496 ✔
1736
}
45,496 ✔
1737

1738
static int32_t tsdbDataFileWriterCloseCommit(SDataFileWriter *writer, TFileOpArray *opArr) {
1,810 ✔
1739
  int32_t code = 0;
1,810 ✔
1740
  int32_t lino = 0;
1,810 ✔
1741

1742
  int32_t  ftype;
156 ✔
1743
  STFileOp op;
1,810 ✔
1744

1745
  if (writer->fd[TSDB_FTYPE_HEAD]) {
1,810 ✔
1746
    TABLEID tbid[1] = {{
1,554 ✔
1747
        .suid = INT64_MAX,
1748
        .uid = INT64_MAX,
1749
    }};
1750

1751
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataEnd(writer), &lino, _exit);
1,554 ✔
1752
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataBegin(writer, tbid), &lino, _exit);
1,554 ✔
1753
    TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlock(writer), &lino, _exit);
1,554 ✔
1754
    TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlk(writer), &lino, _exit);
1,554 ✔
1755
    TAOS_CHECK_GOTO(tsdbDataFileWriteHeadFooter(writer), &lino, _exit);
1,554 ✔
1756

1757
    SVersionRange ofRange = {.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
1,554 ✔
1758

1759
    // .head
1760
    ftype = TSDB_FTYPE_HEAD;
1,554 ✔
1761
    if (writer->config->files[ftype].exist) {
1,554 ✔
1762
      op = (STFileOp){
425 ✔
1763
          .optype = TSDB_FOP_REMOVE,
1764
          .fid = writer->config->fid,
425 ✔
1765
          .of = writer->config->files[ftype].file,
425 ✔
1766
      };
1767
      ofRange = (SVersionRange){.minVer = op.of.minVer, .maxVer = op.of.maxVer};
425 ✔
1768
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
850 ✔
1769
    }
1770
    op = (STFileOp){
1,554 ✔
1771
        .optype = TSDB_FOP_CREATE,
1772
        .fid = writer->config->fid,
1,554 ✔
1773
        .nf = writer->files[ftype],
1,398 ✔
1774
    };
1775
    tsdbTFileUpdVerRange(&op.nf, ofRange);
1,554 ✔
1776
    tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
1,554 ✔
1777
    TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
2,952 ✔
1778

1779
    // .data
1780
    ftype = TSDB_FTYPE_DATA;
1,554 ✔
1781
    if (!writer->config->files[ftype].exist) {
1,554 ✔
1782
      op = (STFileOp){
1,129 ✔
1783
          .optype = TSDB_FOP_CREATE,
1784
          .fid = writer->config->fid,
1,129 ✔
1785
          .nf = writer->files[ftype],
973 ✔
1786
      };
1787
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
1,129 ✔
1788
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
2,258 ✔
1789
    } else if (writer->config->files[ftype].file.size != writer->files[ftype].size) {
425 ✔
1790
      op = (STFileOp){
425 ✔
1791
          .optype = TSDB_FOP_MODIFY,
1792
          .fid = writer->config->fid,
425 ✔
1793
          .of = writer->config->files[ftype].file,
425 ✔
1794
          .nf = writer->files[ftype],
425 ✔
1795
      };
1796
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
425 ✔
1797
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
850 ✔
1798
    }
1799

1800
    // .sma
1801
    ftype = TSDB_FTYPE_SMA;
1,554 ✔
1802
    if (!writer->config->files[ftype].exist) {
1,554 ✔
1803
      op = (STFileOp){
1,129 ✔
1804
          .optype = TSDB_FOP_CREATE,
1805
          .fid = writer->config->fid,
1,129 ✔
1806
          .nf = writer->files[ftype],
973 ✔
1807
      };
1808
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
1,129 ✔
1809
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
2,258 ✔
1810
    } else if (writer->config->files[ftype].file.size != writer->files[ftype].size) {
425 ✔
1811
      op = (STFileOp){
425 ✔
1812
          .optype = TSDB_FOP_MODIFY,
1813
          .fid = writer->config->fid,
425 ✔
1814
          .of = writer->config->files[ftype].file,
425 ✔
1815
          .nf = writer->files[ftype],
425 ✔
1816
      };
1817
      tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
425 ✔
1818
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
850 ✔
1819
    }
1820
  }
1821

1822
  if (writer->fd[TSDB_FTYPE_TOMB]) {
1,810 ✔
1823
    STombRecord record[1] = {{
269 ✔
1824
        .suid = INT64_MAX,
1825
        .uid = INT64_MAX,
1826
        .version = INT64_MAX,
1827
    }};
1828

1829
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombRecord(writer, record), &lino, _exit);
269 ✔
1830
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlock(writer), &lino, _exit);
269 ✔
1831
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlk(writer), &lino, _exit);
269 ✔
1832
    TAOS_CHECK_GOTO(tsdbDataFileWriteTombFooter(writer), &lino, _exit);
269 ✔
1833

1834
    SVersionRange ofRange = (SVersionRange){.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
269 ✔
1835

1836
    ftype = TSDB_FTYPE_TOMB;
269 ✔
1837
    if (writer->config->files[ftype].exist) {
269 ✔
1838
      op = (STFileOp){
183 ✔
1839
          .optype = TSDB_FOP_REMOVE,
1840
          .fid = writer->config->fid,
183 ✔
1841
          .of = writer->config->files[ftype].file,
183 ✔
1842
      };
1843
      ofRange = (SVersionRange){.minVer = op.of.minVer, .maxVer = op.of.maxVer};
183 ✔
1844
      TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
366 ✔
1845
    }
1846
    op = (STFileOp){
269 ✔
1847
        .optype = TSDB_FOP_CREATE,
1848
        .fid = writer->config->fid,
269 ✔
1849
        .nf = writer->files[ftype],
261 ✔
1850
    };
1851
    tsdbTFileUpdVerRange(&op.nf, ofRange);
269 ✔
1852
    tsdbTFileUpdVerRange(&op.nf, writer->ctx->tombRange);
269 ✔
1853
    TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
538 ✔
1854
  }
1855
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
1,810 ✔
1856
  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
9,032 ✔
1857
    if (writer->fd[i]) {
7,230 ✔
1858
      TAOS_CHECK_GOTO(tsdbFsyncFile(writer->fd[i], pEncryptData), &lino, _exit);
4,926 ✔
1859
      tsdbCloseFile(&writer->fd[i]);
4,923 ✔
1860
    }
1861
  }
1862

1863
_exit:
1,802 ✔
1864
  if (code) {
1,808 ✔
1865
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1866
              tstrerror(code));
1867
  }
1868
  return code;
1,808 ✔
1869
}
1870

1871
static int32_t tsdbDataFileWriterOpenDataFD(SDataFileWriter *writer) {
1,553 ✔
1872
  int32_t code = 0;
1,553 ✔
1873
  int32_t lino = 0;
1,553 ✔
1874

1875
  int32_t ftypes[] = {TSDB_FTYPE_HEAD, TSDB_FTYPE_DATA, TSDB_FTYPE_SMA};
1,553 ✔
1876

1877
  for (int32_t i = 0; i < ARRAY_SIZE(ftypes); ++i) {
6,210 ✔
1878
    int32_t ftype = ftypes[i];
4,657 ✔
1879

1880
    char    fname[TSDB_FILENAME_LEN];
4,657 ✔
1881
    int32_t flag = TD_FILE_READ | TD_FILE_WRITE;
4,659 ✔
1882

1883
    if (writer->files[ftype].size == 0) {
4,659 ✔
1884
      flag |= (TD_FILE_CREATE | TD_FILE_TRUNC);
3,810 ✔
1885
    }
1886

1887
    int32_t lcn = writer->files[ftype].lcn;
4,659 ✔
1888
    tsdbTFileName(writer->config->tsdb, &writer->files[ftype], fname);
4,659 ✔
1889
    TAOS_CHECK_GOTO(tsdbOpenFile(fname, writer->config->tsdb, flag, &writer->fd[ftype], lcn), &lino, _exit);
4,659 ✔
1890

1891
    if (writer->files[ftype].size == 0) {
4,660 ✔
1892
      uint8_t hdr[TSDB_FHDR_SIZE] = {0};
3,810 ✔
1893

1894
      SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
3,811 ✔
1895

1896
      TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd[ftype], 0, hdr, TSDB_FHDR_SIZE, pEncryptData), &lino, _exit);
3,811 ✔
1897

1898
      writer->files[ftype].size += TSDB_FHDR_SIZE;
3,809 ✔
1899
    }
1900
  }
1901

1902
  if (writer->ctx->reader) {
1,553 ✔
1903
    TAOS_CHECK_GOTO(tsdbDataFileReadBrinBlk(writer->ctx->reader, &writer->ctx->brinBlkArray), &lino, _exit);
425 ✔
1904
  }
1905

1906
_exit:
1,553 ✔
1907
  if (code) {
1,554 ✔
1908
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1909
              tstrerror(code));
1910
  }
1911
  return code;
1,554 ✔
1912
}
1913

1914
int32_t tsdbDataFileWriterOpen(const SDataFileWriterConfig *config, SDataFileWriter **writer) {
9,288 ✔
1915
  writer[0] = taosMemoryCalloc(1, sizeof(*writer[0]));
9,288 ✔
1916
  if (!writer[0]) {
9,272 ✔
1917
    return terrno;
×
1918
  }
1919

1920
  writer[0]->config[0] = config[0];
9,298 ✔
1921
  return 0;
9,298 ✔
1922
}
1923

1924
int32_t tsdbDataFileWriterClose(SDataFileWriter **writer, bool abort, TFileOpArray *opArr) {
9,292 ✔
1925
  if (writer == NULL || writer[0] == NULL) return 0;
9,292 ✔
1926

1927
  int32_t code = 0;
9,295 ✔
1928
  int32_t lino = 0;
9,295 ✔
1929

1930
  if (writer[0]->ctx->opened) {
9,296 ✔
1931
    if (abort) {
1,810 ✔
1932
      TAOS_CHECK_GOTO(tsdbDataFileWriterCloseAbort(writer[0]), &lino, _exit);
×
1933
    } else {
1934
      TAOS_CHECK_GOTO(tsdbDataFileWriterCloseCommit(writer[0], opArr), &lino, _exit);
1,810 ✔
1935
    }
1936
    tsdbDataFileWriterDoClose(writer[0]);
1,807 ✔
1937
  }
1938
  taosMemoryFree(writer[0]);
9,294 ✔
1939
  writer[0] = NULL;
9,286 ✔
1940

1941
_exit:
9,286 ✔
1942
  if (code) {
9,288 ✔
1943
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer[0]->config->tsdb->pVnode), __func__, __FILE__, lino,
×
1944
              tstrerror(code));
1945
  }
1946
  return code;
9,129 ✔
1947
}
1948

1949
int32_t tsdbDataFileWriteRow(SDataFileWriter *writer, SRowInfo *row) {
154,802,558 ✔
1950
  int32_t code = 0;
154,802,558 ✔
1951
  int32_t lino = 0;
154,802,558 ✔
1952

1953
  if (!writer->ctx->opened) {
154,808,124 ✔
1954
    TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpen(writer), &lino, _exit);
207 ✔
1955
  }
1956

1957
  if (writer->fd[TSDB_FTYPE_HEAD] == NULL) {
154,808,124 ✔
1958
    TAOS_CHECK_GOTO(tsdbDataFileWriterOpenDataFD(writer), &lino, _exit);
207 ✔
1959
  }
1960

1961
  if (row->uid != writer->ctx->tbid->uid) {
154,808,124 ✔
1962
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataEnd(writer), &lino, _exit);
29,742 ✔
1963
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataBegin(writer, (TABLEID *)row), &lino, _exit);
29,742 ✔
1964
  }
1965

1966
  TAOS_CHECK_GOTO(tsdbDataFileDoWriteTSData(writer, &row->row), &lino, _exit);
154,808,124 ✔
1967

1968
_exit:
154,609,186 ✔
1969
  if (code) {
154,610,634 ✔
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;
154,610,634 ✔
1974
}
1975

1976
int32_t tsdbDataFileWriteBlockData(SDataFileWriter *writer, SBlockData *bData) {
55,046 ✔
1977
  if (bData->nRow == 0) {
55,046 ✔
1978
    return 0;
×
1979
  }
1980

1981
  int32_t code = 0;
55,046 ✔
1982
  int32_t lino = 0;
55,046 ✔
1983

1984
  if (!bData->uid) {
55,047 ✔
1985
    return TSDB_CODE_INVALID_PARA;
×
1986
  }
1987

1988
  if (!writer->ctx->opened) {
55,047 ✔
1989
    TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpen(writer), &lino, _exit);
1,345 ✔
1990
  }
1991

1992
  if (writer->fd[TSDB_FTYPE_DATA] == NULL) {
55,046 ✔
1993
    TAOS_CHECK_GOTO(tsdbDataFileWriterOpenDataFD(writer), &lino, _exit);
1,346 ✔
1994
  }
1995

1996
  if (bData->uid != writer->ctx->tbid->uid) {
55,046 ✔
1997
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataEnd(writer), &lino, _exit);
30,874 ✔
1998
    TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataBegin(writer, (TABLEID *)bData), &lino, _exit);
30,870 ✔
1999
  }
2000

2001
  if (writer->ctx->tbHasOldData) {
55,043 ✔
2002
    STsdbRowKey key;
536 ✔
2003

2004
    tsdbRowGetKey(&tsdbRowFromBlockData(bData, 0), &key);
536 ✔
2005
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteTableOldData(writer, &key), &lino, _exit);
536 ✔
2006
  }
2007

2008
  if (!writer->ctx->tbHasOldData       //
55,045 ✔
2009
      && writer->blockData->nRow == 0  //
55,006 ✔
2010
  ) {
2011
    TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, bData), &lino, _exit);
55,006 ✔
2012

2013
  } else {
2014
    for (int32_t i = 0; i < bData->nRow; ++i) {
50,377 ✔
2015
      TSDBROW row[1] = {tsdbRowFromBlockData(bData, i)};
50,338 ✔
2016
      TAOS_CHECK_GOTO(tsdbDataFileDoWriteTSData(writer, row), &lino, _exit);
50,335 ✔
2017
    }
2018
  }
2019

2020
_exit:
40,143 ✔
2021
  if (code) {
40,146 ✔
2022
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
2023
              tstrerror(code));
2024
  }
2025
  return code;
40,144 ✔
2026
}
2027

2028
int32_t tsdbDataFileFlush(SDataFileWriter *writer) {
31,165 ✔
2029
  if (!writer->ctx->opened) {
31,165 ✔
2030
    return TSDB_CODE_INVALID_PARA;
×
2031
  }
2032

2033
  if (writer->blockData->nRow == 0) return 0;
31,165 ✔
2034
  if (writer->ctx->tbHasOldData) return 0;
31,165 ✔
2035

2036
  return tsdbDataFileDoWriteBlockData(writer, writer->blockData);
30,966 ✔
2037
}
2038

2039
static int32_t tsdbDataFileWriterOpenTombFD(SDataFileWriter *writer) {
269 ✔
2040
  int32_t code = 0;
269 ✔
2041
  int32_t lino = 0;
269 ✔
2042

2043
  char    fname[TSDB_FILENAME_LEN];
269 ✔
2044
  int32_t ftype = TSDB_FTYPE_TOMB;
269 ✔
2045

2046
  int32_t flag = (TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
269 ✔
2047

2048
  int32_t lcn = writer->files[ftype].lcn;
269 ✔
2049
  tsdbTFileName(writer->config->tsdb, writer->files + ftype, fname);
269 ✔
2050

2051
  TAOS_CHECK_GOTO(tsdbOpenFile(fname, writer->config->tsdb, flag, &writer->fd[ftype], lcn), &lino, _exit);
269 ✔
2052

2053
  uint8_t hdr[TSDB_FHDR_SIZE] = {0};
269 ✔
2054
  SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
269 ✔
2055

2056
  TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd[ftype], 0, hdr, TSDB_FHDR_SIZE, pEncryptData), &lino, _exit);
269 ✔
2057
  writer->files[ftype].size += TSDB_FHDR_SIZE;
269 ✔
2058

2059
  if (writer->ctx->reader) {
269 ✔
2060
    TAOS_CHECK_GOTO(tsdbDataFileReadTombBlk(writer->ctx->reader, &writer->ctx->tombBlkArray), &lino, _exit);
253 ✔
2061

2062
    if (TARRAY2_SIZE(writer->ctx->tombBlkArray) > 0) {
253 ✔
2063
      writer->ctx->hasOldTomb = true;
183 ✔
2064
    }
2065

2066
    writer->ctx->tombBlkArrayIdx = 0;
253 ✔
2067
    tTombBlockClear(writer->ctx->tombBlock);
253 ✔
2068
    writer->ctx->tombBlockIdx = 0;
253 ✔
2069
  }
2070

2071
_exit:
269 ✔
2072
  if (code) {
261 ✔
2073
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
2074
              tstrerror(code));
2075
  }
2076
  return code;
269 ✔
2077
}
2078

2079
int32_t tsdbDataFileWriteTombRecord(SDataFileWriter *writer, const STombRecord *record) {
854 ✔
2080
  int32_t code = 0;
854 ✔
2081
  int32_t lino = 0;
854 ✔
2082

2083
  if (!writer->ctx->opened) {
854 ✔
2084
    TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpen(writer), &lino, _exit);
258 ✔
2085
  }
2086

2087
  if (writer->fd[TSDB_FTYPE_TOMB] == NULL) {
854 ✔
2088
    TAOS_CHECK_GOTO(tsdbDataFileWriterOpenTombFD(writer), &lino, _exit);
269 ✔
2089
  }
2090

2091
  TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombRecord(writer, record), &lino, _exit);
854 ✔
2092

2093
_exit:
854 ✔
2094
  if (code) {
838 ✔
2095
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
×
2096
              tstrerror(code));
2097
  }
2098
  return code;
854 ✔
2099
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc