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

H
Hongze Cheng 已提交
16
#include "tsdbDataFileRW.h"
H
Hongze Cheng 已提交
17

H
Hongze Cheng 已提交
18 19 20 21 22
extern int32_t tsdbFileDoWriteTombBlock(STsdbFD *fd, STombBlock *tombBlock, int8_t cmprAlg, int64_t *fileSize,
                                        TTombBlkArray *tombBlkArray, uint8_t **bufArr);
extern int32_t tsdbFileDoWriteTombBlk(STsdbFD *fd, const TTombBlkArray *tombBlkArray, SFDataPtr *ptr,
                                      int64_t *fileSize);

H
Hongze Cheng 已提交
23 24
// SDataFileReader =============================================
struct SDataFileReader {
H
Hongze Cheng 已提交
25
  SDataFileReaderConfig config[1];
H
Hongze Cheng 已提交
26

H
Hongze Cheng 已提交
27 28
  uint8_t *bufArr[5];

H
Hongze Cheng 已提交
29
  struct {
H
Hongze Cheng 已提交
30 31 32 33
    bool headFooterLoaded;
    bool tombFooterLoaded;
    bool brinBlkLoaded;
    bool tombBlkLoaded;
H
Hongze Cheng 已提交
34 35
  } ctx[1];

H
Hongze Cheng 已提交
36 37
  STsdbFD *fd[TSDB_FTYPE_MAX];

H
Hongze Cheng 已提交
38 39 40 41
  SHeadFooter   headFooter[1];
  STombFooter   tombFooter[1];
  TBrinBlkArray brinBlkArray[1];
  TTombBlkArray tombBlkArray[1];
H
Hongze Cheng 已提交
42
};
H
Hongze Cheng 已提交
43

H
Hongze Cheng 已提交
44
static int32_t tsdbDataFileReadHeadFooter(SDataFileReader *reader) {
H
Hongze Cheng 已提交
45
  if (reader->ctx->headFooterLoaded) return 0;
H
Hongze Cheng 已提交
46 47 48 49

  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
50 51 52 53 54 55 56
  int32_t ftype = TSDB_FTYPE_HEAD;
  if (reader->fd[ftype]) {
    code = tsdbReadFile(reader->fd[ftype], reader->config->files[ftype].file.size - sizeof(SHeadFooter),
                        (uint8_t *)reader->headFooter, sizeof(SHeadFooter));
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
57 58 59 60 61 62 63 64 65 66
  reader->ctx->headFooterLoaded = true;

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(reader->config->tsdb->pVnode), lino, code);
  }
  return code;
}

static int32_t tsdbDataFileReadTombFooter(SDataFileReader *reader) {
H
Hongze Cheng 已提交
67
  if (reader->ctx->tombFooterLoaded) return 0;
H
Hongze Cheng 已提交
68 69 70 71

  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
72 73 74 75 76 77
  int32_t ftype = TSDB_FTYPE_TOMB;
  if (reader->fd[ftype]) {
    code = tsdbReadFile(reader->fd[ftype], reader->config->files[ftype].file.size - sizeof(STombFooter),
                        (uint8_t *)reader->tombFooter, sizeof(STombFooter));
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
78
  reader->ctx->tombFooterLoaded = true;
H
Hongze Cheng 已提交
79 80 81 82 83 84 85 86

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(reader->config->tsdb->pVnode), lino, code);
  }
  return code;
}

H
Hongze Cheng 已提交
87 88
int32_t tsdbDataFileReaderOpen(const char *fname[], const SDataFileReaderConfig *config, SDataFileReader **reader) {
  int32_t code = 0;
H
Hongze Cheng 已提交
89
  int32_t lino = 0;
H
Hongze Cheng 已提交
90

H
Hongze Cheng 已提交
91
  reader[0] = taosMemoryCalloc(1, sizeof(**reader));
H
Hongze Cheng 已提交
92
  if (reader[0] == NULL) {
H
Hongze Cheng 已提交
93 94 95 96 97
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  reader[0]->config[0] = config[0];
H
Hongze Cheng 已提交
98
  if (reader[0]->config->bufArr == NULL) {
H
Hongze Cheng 已提交
99 100 101 102 103
    reader[0]->config->bufArr = reader[0]->bufArr;
  }

  if (fname) {
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
H
Hongze Cheng 已提交
104 105 106 107
      if (fname[i]) {
        code = tsdbOpenFile(fname[i], config->szPage, TD_FILE_READ, &reader[0]->fd[i]);
        TSDB_CHECK_CODE(code, lino, _exit);
      }
H
Hongze Cheng 已提交
108
    }
H
Hongze Cheng 已提交
109 110
  } else {
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
H
Hongze Cheng 已提交
111 112 113 114 115 116
      if (config->files[i].exist) {
        char fname1[TSDB_FILENAME_LEN];
        tsdbTFileName(config->tsdb, &config->files[i].file, fname1);
        code = tsdbOpenFile(fname1, config->szPage, TD_FILE_READ, &reader[0]->fd[i]);
        TSDB_CHECK_CODE(code, lino, _exit);
      }
H
Hongze Cheng 已提交
117 118
    }
  }
H
Hongze Cheng 已提交
119 120 121

_exit:
  if (code) {
H
Hongze Cheng 已提交
122
    TSDB_ERROR_LOG(TD_VID(config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
123 124 125 126
  }
  return code;
}

H
Hongze Cheng 已提交
127 128 129
int32_t tsdbDataFileReaderClose(SDataFileReader **reader) {
  if (reader[0] == NULL) return 0;

H
Hongze Cheng 已提交
130 131 132
  TARRAY2_DESTROY(reader[0]->tombBlkArray, NULL);
  TARRAY2_DESTROY(reader[0]->brinBlkArray, NULL);

H
Hongze Cheng 已提交
133
#if 0
H
Hongze Cheng 已提交
134 135
  TARRAY2_DESTROY(reader[0]->dataBlkArray, NULL);
  TARRAY2_DESTROY(reader[0]->blockIdxArray, NULL);
H
Hongze Cheng 已提交
136
#endif
H
Hongze Cheng 已提交
137 138

  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
H
Hongze Cheng 已提交
139 140 141
    if (reader[0]->fd[i]) {
      tsdbCloseFile(&reader[0]->fd[i]);
    }
H
Hongze Cheng 已提交
142 143 144 145 146
  }

  for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->bufArr); ++i) {
    tFree(reader[0]->bufArr[i]);
  }
H
Hongze Cheng 已提交
147

H
Hongze Cheng 已提交
148 149
  taosMemoryFree(reader[0]);
  reader[0] = NULL;
H
Hongze Cheng 已提交
150 151 152
  return 0;
}

H
Hongze Cheng 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
int32_t tsdbDataFileReadBrinBlk(SDataFileReader *reader, const TBrinBlkArray **brinBlkArray) {
  int32_t code = 0;
  int32_t lino = 0;

  if (!reader->ctx->brinBlkLoaded) {
    code = tsdbDataFileReadHeadFooter(reader);
    TSDB_CHECK_CODE(code, lino, _exit);

    if (reader->headFooter->brinBlkPtr->size > 0) {
      void *data = taosMemoryMalloc(reader->headFooter->brinBlkPtr->size);
      if (data == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        TSDB_CHECK_CODE(code, lino, _exit);
      }

      code = tsdbReadFile(reader->fd[TSDB_FTYPE_HEAD], reader->headFooter->brinBlkPtr->offset, data,
                          reader->headFooter->brinBlkPtr->size);
      if (code) {
        taosMemoryFree(data);
        TSDB_CHECK_CODE(code, lino, _exit);
      }

      int32_t size = reader->headFooter->brinBlkPtr->size / sizeof(SBrinBlk);
      TARRAY2_INIT_EX(reader->brinBlkArray, size, size, data);
    } else {
      TARRAY2_INIT(reader->brinBlkArray);
    }

    reader->ctx->brinBlkLoaded = true;
  }
  brinBlkArray[0] = reader->brinBlkArray;

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(reader->config->tsdb->pVnode), lino, code);
  }
  return code;
}

int32_t tsdbDataFileReadBrinBlock(SDataFileReader *reader, const SBrinBlk *brinBlk, SBrinBlock *brinBlock) {
  int32_t code = 0;
  int32_t lino = 0;

  code = tRealloc(&reader->config->bufArr[0], brinBlk->dp->size);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tsdbReadFile(reader->fd[TSDB_FTYPE_HEAD], brinBlk->dp->offset, reader->config->bufArr[0], brinBlk->dp->size);
  TSDB_CHECK_CODE(code, lino, _exit);

  int32_t size = 0;
  tBrinBlockClear(brinBlock);
  for (int32_t i = 0; i < ARRAY_SIZE(brinBlock->dataArr1); i++) {
    code = tsdbDecmprData(reader->config->bufArr[0] + size, brinBlk->size[i], TSDB_DATA_TYPE_BIGINT, brinBlk->cmprAlg,
                          &reader->config->bufArr[1], brinBlk->numRec * sizeof(int64_t), &reader->config->bufArr[2]);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = TARRAY2_APPEND_BATCH(&brinBlock->dataArr1[i], reader->config->bufArr[1], brinBlk->numRec);
    TSDB_CHECK_CODE(code, lino, _exit);

    size += brinBlk->size[i];
  }

  for (int32_t i = 0, j = ARRAY_SIZE(brinBlock->dataArr1); i < ARRAY_SIZE(brinBlock->dataArr2); i++, j++) {
    code = tsdbDecmprData(reader->config->bufArr[0] + size, brinBlk->size[j], TSDB_DATA_TYPE_INT, brinBlk->cmprAlg,
                          &reader->config->bufArr[1], brinBlk->numRec * sizeof(int32_t), &reader->config->bufArr[2]);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = TARRAY2_APPEND_BATCH(&brinBlock->dataArr2[i], reader->config->bufArr[1], brinBlk->numRec);
    TSDB_CHECK_CODE(code, lino, _exit);

    size += brinBlk->size[j];
  }

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(reader->config->tsdb->pVnode), lino, code);
  }
  return code;
}

int32_t tsdbDataFileReadBlockData(SDataFileReader *reader, const SBrinRecord *record, SBlockData *bData) {
  int32_t code = 0;
  int32_t lino = 0;

  code = tRealloc(&reader->config->bufArr[0], record->blockSize);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tsdbReadFile(reader->fd[TSDB_FTYPE_DATA], record->blockOffset, reader->config->bufArr[0], record->blockSize);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tDecmprBlockData(reader->config->bufArr[0], record->blockSize, bData, &reader->config->bufArr[1]);
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(reader->config->tsdb->pVnode), lino, code);
  }
  return code;
}

H
Hongze Cheng 已提交
253 254
int32_t tsdbDataFileReadBlockDataByColumn(SDataFileReader *reader, const SBrinRecord *record, SBlockData *bData,
                                          STSchema *pTSchema, int16_t cids[], int32_t ncid) {
H
Hongze Cheng 已提交
255 256 257
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
  code = tBlockDataInit(bData, (TABLEID *)record, pTSchema, cids, ncid);
  TSDB_CHECK_CODE(code, lino, _exit);

  // uid + version + tskey
  code = tRealloc(&reader->config->bufArr[0], record->blockKeySize);
  TSDB_CHECK_CODE(code, lino, _exit);

  code =
      tsdbReadFile(reader->fd[TSDB_FTYPE_DATA], record->blockOffset, reader->config->bufArr[0], record->blockKeySize);
  TSDB_CHECK_CODE(code, lino, _exit);

  // hdr
  SDiskDataHdr hdr[1];
  int32_t      size = 0;

  size += tGetDiskDataHdr(reader->config->bufArr[0] + size, hdr);

  ASSERT(hdr->delimiter == TSDB_FILE_DLMT);
  ASSERT(record->uid == hdr->uid);

  bData->nRow = hdr->nRow;

  // uid
  ASSERT(hdr->uid);

  // version
  code = tsdbDecmprData(reader->config->bufArr[0] + size, hdr->szVer, TSDB_DATA_TYPE_BIGINT, hdr->cmprAlg,
                        (uint8_t **)&bData->aVersion, sizeof(int64_t) * hdr->nRow, &reader->config->bufArr[1]);
  TSDB_CHECK_CODE(code, lino, _exit);
  size += hdr->szVer;

  // ts
  code = tsdbDecmprData(reader->config->bufArr[0] + size, hdr->szKey, TSDB_DATA_TYPE_TIMESTAMP, hdr->cmprAlg,
                        (uint8_t **)&bData->aTSKEY, sizeof(TSKEY) * hdr->nRow, &reader->config->bufArr[1]);
  TSDB_CHECK_CODE(code, lino, _exit);
  size += hdr->szKey;

  ASSERT(size == record->blockKeySize);

  // other columns
  if (bData->nColData > 0) {
    if (hdr->szBlkCol > 0) {
      code = tRealloc(&reader->config->bufArr[0], hdr->szBlkCol);
      TSDB_CHECK_CODE(code, lino, _exit);

      code = tsdbReadFile(reader->fd[TSDB_FTYPE_DATA], record->blockOffset + record->blockKeySize,
                          reader->config->bufArr[0], hdr->szBlkCol);
      TSDB_CHECK_CODE(code, lino, _exit);
    }

    SBlockCol  bc[1] = {{.cid = 0}};
    SBlockCol *blockCol = bc;

    size = 0;
    for (int32_t i = 0; i < bData->nColData; i++) {
      SColData *colData = tBlockDataGetColDataByIdx(bData, i);

      while (blockCol && blockCol->cid < colData->cid) {
        if (size < hdr->szBlkCol) {
          size += tGetBlockCol(reader->config->bufArr[0] + size, blockCol);
        } else {
          ASSERT(size == hdr->szBlkCol);
          blockCol = NULL;
        }
      }

      if (blockCol == NULL || blockCol->cid > colData->cid) {
        for (int32_t iRow = 0; iRow < hdr->nRow; iRow++) {
          code = tColDataAppendValue(colData, &COL_VAL_NONE(colData->cid, colData->type));
          TSDB_CHECK_CODE(code, lino, _exit);
        }
      } else {
        ASSERT(blockCol->type == colData->type);
        ASSERT(blockCol->flag && blockCol->flag != HAS_NONE);

        if (blockCol->flag == HAS_NULL) {
          for (int32_t iRow = 0; iRow < hdr->nRow; iRow++) {
            code = tColDataAppendValue(colData, &COL_VAL_NULL(blockCol->cid, blockCol->type));
            TSDB_CHECK_CODE(code, lino, _exit);
          }
        } else {
          int32_t size1 = blockCol->szBitmap + blockCol->szOffset + blockCol->szValue;

          code = tRealloc(&reader->config->bufArr[1], size1);
          TSDB_CHECK_CODE(code, lino, _exit);

          code = tsdbReadFile(reader->fd[TSDB_FTYPE_DATA],
                              record->blockOffset + record->blockKeySize + hdr->szBlkCol + blockCol->offset,
                              reader->config->bufArr[1], size1);
          TSDB_CHECK_CODE(code, lino, _exit);

          code = tsdbDecmprColData(reader->config->bufArr[1], blockCol, hdr->cmprAlg, hdr->nRow, colData,
                                   &reader->config->bufArr[2]);
          TSDB_CHECK_CODE(code, lino, _exit);
        }
      }
    }
  }
H
Hongze Cheng 已提交
356 357 358 359 360 361 362 363

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(reader->config->tsdb->pVnode), lino, code);
  }
  return code;
}

H
Hongze Cheng 已提交
364 365
int32_t tsdbDataFileReadBlockSma(SDataFileReader *reader, const SBrinRecord *record,
                                 TColumnDataAggArray *columnDataAggArray) {
H
Hongze Cheng 已提交
366 367 368
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
  TARRAY2_CLEAR(columnDataAggArray, NULL);
  if (record->smaSize > 0) {
    code = tRealloc(&reader->config->bufArr[0], record->smaSize);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = tsdbReadFile(reader->fd[TSDB_FTYPE_SMA], record->smaOffset, reader->config->bufArr[0], record->smaSize);
    TSDB_CHECK_CODE(code, lino, _exit);

    // decode sma data
    int32_t size = 0;
    while (size < record->smaSize) {
      SColumnDataAgg sma[1];

      size += tGetColumnDataAgg(reader->config->bufArr[0] + size, sma);

      code = TARRAY2_APPEND_PTR(columnDataAggArray, sma);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
    ASSERT(size == record->smaSize);
  }
H
Hongze Cheng 已提交
389 390 391 392 393 394 395 396

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(reader->config->tsdb->pVnode), lino, code);
  }
  return code;
}

H
Hongze Cheng 已提交
397 398 399 400
int32_t tsdbDataFileReadTombBlk(SDataFileReader *reader, const TTombBlkArray **tombBlkArray) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
401
  if (!reader->ctx->tombBlkLoaded) {
H
Hongze Cheng 已提交
402 403
    code = tsdbDataFileReadTombFooter(reader);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
404

H
Hongze Cheng 已提交
405 406 407 408 409 410
    if (reader->tombFooter->tombBlkPtr->size > 0) {
      void *data = taosMemoryMalloc(reader->tombFooter->tombBlkPtr->size);
      if (data == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        TSDB_CHECK_CODE(code, lino, _exit);
      }
H
Hongze Cheng 已提交
411

H
Hongze Cheng 已提交
412 413 414 415
      code = tsdbReadFile(reader->fd[TSDB_FTYPE_TOMB], reader->tombFooter->tombBlkPtr->offset, data,
                          reader->tombFooter->tombBlkPtr->size);
      if (code) {
        taosMemoryFree(data);
H
Hongze Cheng 已提交
416 417
        TSDB_CHECK_CODE(code, lino, _exit);
      }
H
Hongze Cheng 已提交
418 419 420 421 422

      int32_t size = reader->tombFooter->tombBlkPtr->size / sizeof(STombBlk);
      TARRAY2_INIT_EX(reader->tombBlkArray, size, size, data);
    } else {
      TARRAY2_INIT(reader->tombBlkArray);
H
Hongze Cheng 已提交
423 424 425 426 427 428
    }

    reader->ctx->tombBlkLoaded = true;
  }
  tombBlkArray[0] = reader->tombBlkArray;

H
Hongze Cheng 已提交
429 430 431 432 433 434 435 436 437 438 439
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(reader->config->tsdb->pVnode), lino, code);
  }
  return code;
}

int32_t tsdbDataFileReadTombBlock(SDataFileReader *reader, const STombBlk *tombBlk, STombBlock *tData) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
440 441 442 443 444 445 446
  code = tRealloc(&reader->config->bufArr[0], tombBlk->dp->size);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tsdbReadFile(reader->fd[TSDB_FTYPE_TOMB], tombBlk->dp->offset, reader->config->bufArr[0], tombBlk->dp->size);
  TSDB_CHECK_CODE(code, lino, _exit);

  int32_t size = 0;
H
Hongze Cheng 已提交
447
  tTombBlockClear(tData);
H
Hongze Cheng 已提交
448
  for (int32_t i = 0; i < ARRAY_SIZE(tData->dataArr); ++i) {
H
Hongze Cheng 已提交
449 450
    code = tsdbDecmprData(reader->config->bufArr[0] + size, tombBlk->size[i], TSDB_DATA_TYPE_BIGINT, tombBlk->cmprAlg,
                          &reader->config->bufArr[1], sizeof(int64_t) * tombBlk->numRec, &reader->config->bufArr[2]);
H
Hongze Cheng 已提交
451 452
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
453 454 455 456
    code = TARRAY2_APPEND_BATCH(&tData->dataArr[i], reader->config->bufArr[1], tombBlk->numRec);
    TSDB_CHECK_CODE(code, lino, _exit);

    size += tombBlk->size[i];
H
Hongze Cheng 已提交
457
  }
H
Hongze Cheng 已提交
458
  ASSERT(size == tombBlk->dp->size);
H
Hongze Cheng 已提交
459

H
Hongze Cheng 已提交
460 461 462 463 464 465 466
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(reader->config->tsdb->pVnode), lino, code);
  }
  return code;
}

H
Hongze Cheng 已提交
467 468
// SDataFileWriter =============================================
struct SDataFileWriter {
H
Hongze Cheng 已提交
469
  SDataFileWriterConfig config[1];
H
Hongze Cheng 已提交
470

H
Hongze Cheng 已提交
471 472 473 474
  SSkmInfo skmTb[1];
  SSkmInfo skmRow[1];
  uint8_t *bufArr[5];

H
Hongze Cheng 已提交
475
  struct {
H
Hongze Cheng 已提交
476 477
    bool             opened;
    SDataFileReader *reader;
H
Hongze Cheng 已提交
478

H
Hongze Cheng 已提交
479
    // for ts data
H
Hongze Cheng 已提交
480 481 482 483 484 485 486 487 488
    TABLEID tbid[1];
    bool    tbHasOldData;

    const TBrinBlkArray *brinBlkArray;
    int32_t              brinBlkArrayIdx;
    SBrinBlock           brinBlock[1];
    int32_t              brinBlockIdx;
    SBlockData           blockData[1];
    int32_t              blockDataIdx;
H
Hongze Cheng 已提交
489 490 491 492
    // for tomb data
    bool                 hasOldTomb;
    const TTombBlkArray *tombBlkArray;
    int32_t              tombBlkArrayIdx;
H
Hongze Cheng 已提交
493 494
    STombBlock           tombBlock[1];
    int32_t              tombBlockIdx;
H
Hongze Cheng 已提交
495
  } ctx[1];
H
Hongze Cheng 已提交
496

H
Hongze Cheng 已提交
497
  STFile   files[TSDB_FTYPE_MAX];
H
Hongze Cheng 已提交
498 499
  STsdbFD *fd[TSDB_FTYPE_MAX];

H
Hongze Cheng 已提交
500 501 502 503 504 505 506 507 508
  SHeadFooter headFooter[1];
  STombFooter tombFooter[1];

  TBrinBlkArray brinBlkArray[1];
  SBrinBlock    brinBlock[1];
  SBlockData    blockData[1];

  TTombBlkArray tombBlkArray[1];
  STombBlock    tombBlock[1];
H
Hongze Cheng 已提交
509 510
};

H
Hongze Cheng 已提交
511
static int32_t tsdbDataFileWriterCloseAbort(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
512
  ASSERT(0);
H
Hongze Cheng 已提交
513 514
  return 0;
}
H
Hongze Cheng 已提交
515

H
Hongze Cheng 已提交
516
static int32_t tsdbDataFileWriterDoClose(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
517 518 519 520
  if (writer->ctx->reader) {
    tsdbDataFileReaderClose(&writer->ctx->reader);
  }

H
Hongze Cheng 已提交
521
  tTombBlockDestroy(writer->tombBlock);
H
Hongze Cheng 已提交
522
  TARRAY2_DESTROY(writer->tombBlkArray, NULL);
H
Hongze Cheng 已提交
523 524 525 526
  tBlockDataDestroy(writer->blockData);
  tBrinBlockDestroy(writer->brinBlock);
  TARRAY2_DESTROY(writer->brinBlkArray, NULL);

H
Hongze Cheng 已提交
527
  tTombBlockDestroy(writer->ctx->tombBlock);
H
Hongze Cheng 已提交
528 529
  tBlockDataDestroy(writer->ctx->blockData);
  tBrinBlockDestroy(writer->ctx->brinBlock);
H
Hongze Cheng 已提交
530 531 532 533 534 535 536

  for (int32_t i = 0; i < ARRAY_SIZE(writer->bufArr); ++i) {
    tFree(writer->bufArr[i]);
  }

  tDestroyTSchema(writer->skmRow->pTSchema);
  tDestroyTSchema(writer->skmTb->pTSchema);
H
Hongze Cheng 已提交
537 538
  return 0;
}
H
Hongze Cheng 已提交
539

H
Hongze Cheng 已提交
540 541 542 543 544 545 546 547 548
static int32_t tsdbDataFileWriterDoOpenReader(SDataFileWriter *writer) {
  int32_t code = 0;
  int32_t lino = 0;

  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
    if (writer->config->files[i].exist) {
      SDataFileReaderConfig config[1] = {{
          .tsdb = writer->config->tsdb,
          .szPage = writer->config->szPage,
H
Hongze Cheng 已提交
549
          .bufArr = writer->config->bufArr,
H
Hongze Cheng 已提交
550 551 552 553
      }};

      for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
        config->files[i].exist = writer->config->files[i].exist;
H
Hongze Cheng 已提交
554 555 556
        if (config->files[i].exist) {
          config->files[i].file = writer->config->files[i].file;
        }
H
Hongze Cheng 已提交
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
      }

      code = tsdbDataFileReaderOpen(NULL, config, &writer->ctx->reader);
      TSDB_CHECK_CODE(code, lino, _exit);
      break;
    }
  }

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}

H
Hongze Cheng 已提交
572
static int32_t tsdbDataFileWriterDoOpen(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
573 574
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
575
  int32_t ftype;
H
Hongze Cheng 已提交
576 577 578 579 580 581

  if (!writer->config->skmTb) writer->config->skmTb = writer->skmTb;
  if (!writer->config->skmRow) writer->config->skmRow = writer->skmRow;
  if (!writer->config->bufArr) writer->config->bufArr = writer->bufArr;

  // open reader
H
Hongze Cheng 已提交
582 583
  code = tsdbDataFileWriterDoOpenReader(writer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
584

H
Hongze Cheng 已提交
585
  // .head
H
Hongze Cheng 已提交
586 587 588
  ftype = TSDB_FTYPE_HEAD;
  writer->files[ftype] = (STFile){
      .type = ftype,
H
Hongze Cheng 已提交
589 590 591 592 593
      .did = writer->config->did,
      .fid = writer->config->fid,
      .cid = writer->config->cid,
      .size = 0,
  };
H
Hongze Cheng 已提交
594

H
Hongze Cheng 已提交
595
  // .data
H
Hongze Cheng 已提交
596 597 598
  ftype = TSDB_FTYPE_DATA;
  if (writer->config->files[ftype].exist) {
    writer->files[ftype] = writer->config->files[ftype].file;
H
Hongze Cheng 已提交
599
  } else {
H
Hongze Cheng 已提交
600 601 602 603 604 605 606
    writer->files[ftype] = (STFile){
        .type = ftype,
        .did = writer->config->did,
        .fid = writer->config->fid,
        .cid = writer->config->cid,
        .size = 0,
    };
H
Hongze Cheng 已提交
607
  }
H
Hongze Cheng 已提交
608

H
Hongze Cheng 已提交
609
  // .sma
H
Hongze Cheng 已提交
610 611 612
  ftype = TSDB_FTYPE_SMA;
  if (writer->config->files[ftype].exist) {
    writer->files[ftype] = writer->config->files[ftype].file;
H
Hongze Cheng 已提交
613
  } else {
H
Hongze Cheng 已提交
614 615 616 617 618 619 620
    writer->files[ftype] = (STFile){
        .type = ftype,
        .did = writer->config->did,
        .fid = writer->config->fid,
        .cid = writer->config->cid,
        .size = 0,
    };
H
Hongze Cheng 已提交
621 622
  }

H
Hongze Cheng 已提交
623 624 625 626
  // .tomb
  ftype = TSDB_FTYPE_TOMB;
  writer->files[ftype] = (STFile){
      .type = ftype,
H
Hongze Cheng 已提交
627 628 629 630 631
      .did = writer->config->did,
      .fid = writer->config->fid,
      .cid = writer->config->cid,
      .size = 0,
  };
H
Hongze Cheng 已提交
632

H
Hongze Cheng 已提交
633
  writer->ctx->opened = true;
H
Hongze Cheng 已提交
634

H
Hongze Cheng 已提交
635 636 637 638 639
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
H
Hongze Cheng 已提交
640
}
H
Hongze Cheng 已提交
641

H
Hongze Cheng 已提交
642 643 644
int32_t tsdbFileWriteBrinBlock(STsdbFD *fd, SBrinBlock *brinBlock, int8_t cmprAlg, int64_t *fileSize,
                               TBrinBlkArray *brinBlkArray, uint8_t **bufArr) {
  if (BRIN_BLOCK_SIZE(brinBlock) == 0) return 0;
H
Hongze Cheng 已提交
645

H
Hongze Cheng 已提交
646
  int32_t code;
H
Hongze Cheng 已提交
647 648 649 650 651 652

  // get SBrinBlk
  SBrinBlk brinBlk[1] = {
      {
          .dp[0] =
              {
H
Hongze Cheng 已提交
653
                  .offset = *fileSize,
H
Hongze Cheng 已提交
654 655 656 657
                  .size = 0,
              },
          .minTbid =
              {
H
Hongze Cheng 已提交
658 659
                  .suid = TARRAY2_FIRST(brinBlock->suid),
                  .uid = TARRAY2_FIRST(brinBlock->uid),
H
Hongze Cheng 已提交
660 661 662
              },
          .maxTbid =
              {
H
Hongze Cheng 已提交
663 664
                  .suid = TARRAY2_LAST(brinBlock->suid),
                  .uid = TARRAY2_LAST(brinBlock->uid),
H
Hongze Cheng 已提交
665
              },
H
Hongze Cheng 已提交
666 667 668 669
          .minVer = TARRAY2_FIRST(brinBlock->minVer),
          .maxVer = TARRAY2_FIRST(brinBlock->minVer),
          .numRec = BRIN_BLOCK_SIZE(brinBlock),
          .cmprAlg = cmprAlg,
H
Hongze Cheng 已提交
670 671 672
      },
  };

H
Hongze Cheng 已提交
673 674 675
  for (int32_t i = 1; i < BRIN_BLOCK_SIZE(brinBlock); i++) {
    if (brinBlk->minVer > TARRAY2_GET(brinBlock->minVer, i)) {
      brinBlk->minVer = TARRAY2_GET(brinBlock->minVer, i);
H
Hongze Cheng 已提交
676
    }
H
Hongze Cheng 已提交
677 678
    if (brinBlk->maxVer < TARRAY2_GET(brinBlock->maxVer, i)) {
      brinBlk->maxVer = TARRAY2_GET(brinBlock->maxVer, i);
H
Hongze Cheng 已提交
679 680 681 682
    }
  }

  // write to file
H
Hongze Cheng 已提交
683 684 685 686
  for (int32_t i = 0; i < ARRAY_SIZE(brinBlock->dataArr1); i++) {
    code = tsdbCmprData((uint8_t *)TARRAY2_DATA(brinBlock->dataArr1 + i), TARRAY2_DATA_LEN(brinBlock->dataArr1 + i),
                        TSDB_DATA_TYPE_BIGINT, brinBlk->cmprAlg, &bufArr[0], 0, &brinBlk->size[i], &bufArr[1]);
    if (code) return code;
H
Hongze Cheng 已提交
687

H
Hongze Cheng 已提交
688 689
    code = tsdbWriteFile(fd, *fileSize, bufArr[0], brinBlk->size[i]);
    if (code) return code;
H
Hongze Cheng 已提交
690

H
fix  
Hongze Cheng 已提交
691
    brinBlk->dp->size += brinBlk->size[i];
H
Hongze Cheng 已提交
692
    *fileSize += brinBlk->size[i];
H
Hongze Cheng 已提交
693 694
  }

H
Hongze Cheng 已提交
695 696 697 698
  for (int32_t i = 0, j = ARRAY_SIZE(brinBlock->dataArr1); i < ARRAY_SIZE(brinBlock->dataArr2); i++, j++) {
    code = tsdbCmprData((uint8_t *)TARRAY2_DATA(brinBlock->dataArr2 + i), TARRAY2_DATA_LEN(brinBlock->dataArr2 + i),
                        TSDB_DATA_TYPE_INT, brinBlk->cmprAlg, &bufArr[0], 0, &brinBlk->size[j], &bufArr[1]);
    if (code) return code;
H
Hongze Cheng 已提交
699

H
Hongze Cheng 已提交
700 701
    code = tsdbWriteFile(fd, *fileSize, bufArr[0], brinBlk->size[j]);
    if (code) return code;
H
Hongze Cheng 已提交
702

H
fix  
Hongze Cheng 已提交
703
    brinBlk->dp->size += brinBlk->size[j];
H
Hongze Cheng 已提交
704
    *fileSize += brinBlk->size[j];
H
Hongze Cheng 已提交
705 706 707
  }

  // append to brinBlkArray
H
Hongze Cheng 已提交
708 709
  code = TARRAY2_APPEND_PTR(brinBlkArray, brinBlk);
  if (code) return code;
H
Hongze Cheng 已提交
710

H
Hongze Cheng 已提交
711 712 713 714 715 716 717 718 719 720 721 722 723 724
  tBrinBlockClear(brinBlock);

  return 0;
}

static int32_t tsdbDataFileWriteBrinBlock(SDataFileWriter *writer) {
  if (BRIN_BLOCK_SIZE(writer->brinBlock) == 0) return 0;

  int32_t code = 0;
  int32_t lino = 0;

  code = tsdbFileWriteBrinBlock(writer->fd[TSDB_FTYPE_HEAD], writer->brinBlock, writer->config->cmprAlg,
                                &writer->files[TSDB_FTYPE_HEAD].size, writer->brinBlkArray, writer->config->bufArr);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}

static int32_t tsdbDataFileWriteBrinRecord(SDataFileWriter *writer, const SBrinRecord *record) {
  int32_t code = 0;
  int32_t lino = 0;

  code = tBrinBlockPut(writer->brinBlock, record);
  TSDB_CHECK_CODE(code, lino, _exit);

  if (BRIN_BLOCK_SIZE(writer->brinBlock) >= writer->config->maxRow) {
    code = tsdbDataFileWriteBrinBlock(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}

static int32_t tsdbDataFileDoWriteBlockData(SDataFileWriter *writer, SBlockData *bData) {
H
Hongze Cheng 已提交
753 754
  if (bData->nRow == 0) return 0;

H
Hongze Cheng 已提交
755 756
  ASSERT(bData->uid);

H
Hongze Cheng 已提交
757 758 759
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
760 761 762 763 764 765 766
  SBrinRecord record[1] = {{
      .suid = bData->suid,
      .uid = bData->uid,
      .firstKey = bData->aTSKEY[0],
      .firstKeyVer = bData->aVersion[0],
      .lastKey = bData->aTSKEY[bData->nRow - 1],
      .lastKeyVer = bData->aVersion[bData->nRow - 1],
H
Hongze Cheng 已提交
767 768
      .minVer = bData->aVersion[0],
      .maxVer = bData->aVersion[0],
H
Hongze Cheng 已提交
769 770 771 772 773 774 775
      .blockOffset = writer->files[TSDB_FTYPE_DATA].size,
      .smaOffset = writer->files[TSDB_FTYPE_SMA].size,
      .blockSize = 0,
      .blockKeySize = 0,
      .smaSize = 0,
      .numRow = bData->nRow,
      .count = 1,
H
Hongze Cheng 已提交
776 777 778
  }};

  for (int32_t i = 1; i < bData->nRow; ++i) {
H
Hongze Cheng 已提交
779 780 781 782 783 784 785 786
    if (bData->aTSKEY[i] != bData->aTSKEY[i - 1]) {
      record->count++;
    }
    if (bData->aVersion[i] < record->minVer) {
      record->minVer = bData->aVersion[i];
    }
    if (bData->aVersion[i] > record->maxVer) {
      record->maxVer = bData->aVersion[i];
H
Hongze Cheng 已提交
787 788
    }
  }
H
Hongze Cheng 已提交
789

H
Hongze Cheng 已提交
790
  // to .data file
H
Hongze Cheng 已提交
791 792 793 794
  int32_t sizeArr[5] = {0};

  code = tCmprBlockData(bData, writer->config->cmprAlg, NULL, NULL, writer->config->bufArr, sizeArr);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
795

H
Hongze Cheng 已提交
796 797
  record->blockKeySize = sizeArr[3] + sizeArr[2];
  record->blockSize = sizeArr[0] + sizeArr[1] + record->blockKeySize;
H
Hongze Cheng 已提交
798

H
Hongze Cheng 已提交
799 800
  for (int32_t i = 3; i >= 0; --i) {
    if (sizeArr[i]) {
H
Hongze Cheng 已提交
801
      code = tsdbWriteFile(writer->fd[TSDB_FTYPE_DATA], writer->files[TSDB_FTYPE_DATA].size, writer->config->bufArr[i],
H
Hongze Cheng 已提交
802 803
                           sizeArr[i]);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
804
      writer->files[TSDB_FTYPE_DATA].size += sizeArr[i];
H
Hongze Cheng 已提交
805 806 807
    }
  }

H
Hongze Cheng 已提交
808
  // to .sma file
H
Hongze Cheng 已提交
809 810
  for (int32_t i = 0; i < bData->nColData; ++i) {
    SColData *colData = bData->aColData + i;
H
Hongze Cheng 已提交
811
    if ((!colData->smaOn) || ((colData->flag & HAS_VALUE) == 0)) continue;
H
Hongze Cheng 已提交
812 813 814 815

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

H
Hongze Cheng 已提交
816 817 818
    int32_t size = tPutColumnDataAgg(NULL, sma);

    code = tRealloc(&writer->config->bufArr[0], record->smaSize + size);
H
Hongze Cheng 已提交
819 820
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
821 822 823
    tPutColumnDataAgg(writer->config->bufArr[0] + record->smaSize, sma);
    record->smaSize += size;
  }
H
Hongze Cheng 已提交
824

H
Hongze Cheng 已提交
825
  if (record->smaSize > 0) {
H
Hongze Cheng 已提交
826
    code = tsdbWriteFile(writer->fd[TSDB_FTYPE_SMA], record->smaOffset, writer->config->bufArr[0], record->smaSize);
H
Hongze Cheng 已提交
827
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
828
    writer->files[TSDB_FTYPE_SMA].size += record->smaSize;
H
Hongze Cheng 已提交
829 830
  }

H
Hongze Cheng 已提交
831 832
  // append SBrinRecord
  code = tsdbDataFileWriteBrinRecord(writer, record);
H
Hongze Cheng 已提交
833 834
  TSDB_CHECK_CODE(code, lino, _exit);

H
fix bug  
Hongze Cheng 已提交
835
  tBlockDataClear(bData);
H
Hongze Cheng 已提交
836 837 838

_exit:
  if (code) {
H
Hongze Cheng 已提交
839
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
840 841 842
  }
  return code;
}
H
Hongze Cheng 已提交
843

H
Hongze Cheng 已提交
844 845 846 847 848 849
static int32_t tsdbDataFileWriteDataBlk(SDataFileWriter *writer, const TDataBlkArray *dataBlkArray) {
  if (TARRAY2_SIZE(dataBlkArray) == 0) return 0;

  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
850 851 852 853 854 855 856
  int32_t   ftype = TSDB_FTYPE_HEAD;
  SBlockIdx blockIdx[1] = {{
      .suid = writer->ctx->tbid->suid,
      .uid = writer->ctx->tbid->uid,
      .offset = writer->files[ftype].size,
      .size = TARRAY2_DATA_LEN(dataBlkArray),
  }};
H
Hongze Cheng 已提交
857

H
Hongze Cheng 已提交
858 859
  code =
      tsdbWriteFile(writer->fd[ftype], blockIdx->offset, (const uint8_t *)TARRAY2_DATA(dataBlkArray), blockIdx->size);
H
Hongze Cheng 已提交
860
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
861
  writer->files[ftype].size += blockIdx->size;
H
Hongze Cheng 已提交
862

H
Hongze Cheng 已提交
863 864
_exit:
  if (code) {
H
Hongze Cheng 已提交
865
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
866 867 868
  }
  return code;
}
H
Hongze Cheng 已提交
869

H
Hongze Cheng 已提交
870 871 872 873
static int32_t tsdbDataFileDoWriteTSRow(SDataFileWriter *writer, TSDBROW *row) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
874
  // update/append
H
Hongze Cheng 已提交
875
  if (row->type == TSDBROW_ROW_FMT) {
H
Hongze Cheng 已提交
876 877
    code = tsdbUpdateSkmRow(writer->config->tsdb, writer->ctx->tbid, TSDBROW_SVERSION(row), writer->config->skmRow);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
878 879
  }

H
Hongze Cheng 已提交
880 881 882 883 884 885 886 887
  TSDBKEY key[1];
  if (row->type == TSDBROW_ROW_FMT) {
    key->ts = row->pTSRow->ts;
    key->version = row->version;
  } else {
    key->ts = row->pBlockData->aTSKEY[row->iRow];
    key->version = row->pBlockData->aVersion[row->iRow];
  }
H
Hongze Cheng 已提交
888 889 890
  if (key->version <= writer->config->compactVersion                        //
      && writer->blockData->nRow > 0                                        //
      && writer->blockData->aTSKEY[writer->blockData->nRow - 1] == key->ts  //
H
Hongze Cheng 已提交
891
  ) {
H
Hongze Cheng 已提交
892
    code = tBlockDataUpdateRow(writer->blockData, row, writer->config->skmRow->pTSchema);
H
Hongze Cheng 已提交
893 894
    TSDB_CHECK_CODE(code, lino, _exit);
  } else {
H
Hongze Cheng 已提交
895
    if (writer->blockData->nRow >= writer->config->maxRow) {
H
Hongze Cheng 已提交
896
      code = tsdbDataFileDoWriteBlockData(writer, writer->blockData);
H
Hongze Cheng 已提交
897 898
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
899

H
Hongze Cheng 已提交
900
    code = tBlockDataAppendRow(writer->blockData, row, writer->config->skmRow->pTSchema, writer->ctx->tbid->uid);
H
Hongze Cheng 已提交
901 902 903
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
904 905 906 907 908 909 910
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}

H
Hongze Cheng 已提交
911 912 913
static int32_t tsdbDataFileDoWriteTableOldData(SDataFileWriter *writer, const TSDBKEY *key) {
  if (writer->ctx->tbHasOldData == false) return 0;

H
Hongze Cheng 已提交
914 915 916
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
917
  for (;;) {
H
Hongze Cheng 已提交
918
    for (;;) {
H
Hongze Cheng 已提交
919 920 921 922 923 924 925
      // SBlockData
      for (; writer->ctx->blockDataIdx < writer->ctx->blockData->nRow; writer->ctx->blockDataIdx++) {
        if (key->ts < writer->ctx->blockData->aTSKEY[writer->ctx->blockDataIdx]  //
            || (key->ts == writer->ctx->blockData->aTSKEY[writer->ctx->blockDataIdx] &&
                key->version < writer->ctx->blockData->aVersion[writer->ctx->blockDataIdx])) {
          goto _exit;
        } else {
H
Hongze Cheng 已提交
926 927
          TSDBROW row = tsdbRowFromBlockData(writer->ctx->blockData, writer->ctx->blockDataIdx);
          code = tsdbDataFileDoWriteTSRow(writer, &row);
H
Hongze Cheng 已提交
928
          TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
929
        }
H
Hongze Cheng 已提交
930 931 932 933 934 935
      }

      // SBrinBlock
      if (writer->ctx->brinBlockIdx >= BRIN_BLOCK_SIZE(writer->ctx->brinBlock)) {
        break;
      }
H
Hongze Cheng 已提交
936

H
Hongze Cheng 已提交
937 938 939 940
      for (; writer->ctx->brinBlockIdx < BRIN_BLOCK_SIZE(writer->ctx->brinBlock); writer->ctx->brinBlockIdx++) {
        if (TARRAY2_GET(writer->ctx->brinBlock->uid, writer->ctx->brinBlockIdx) != writer->ctx->tbid->uid) {
          writer->ctx->tbHasOldData = false;
          goto _exit;
H
Hongze Cheng 已提交
941
        }
H
Hongze Cheng 已提交
942

H
Hongze Cheng 已提交
943 944 945 946 947 948 949
        if (key->ts < TARRAY2_GET(writer->ctx->brinBlock->firstKey, writer->ctx->brinBlockIdx)  //
            || (key->ts == TARRAY2_GET(writer->ctx->brinBlock->firstKey, writer->ctx->brinBlockIdx) &&
                key->version < TARRAY2_GET(writer->ctx->brinBlock->firstKeyVer, writer->ctx->brinBlockIdx))) {
          goto _exit;
        } else {
          SBrinRecord record[1];
          tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, record);
H
Hongze Cheng 已提交
950
          if (key->ts > record->lastKey || (key->ts == record->lastKey && key->version > record->lastKeyVer)) {
H
Hongze Cheng 已提交
951 952 953 954
            if (writer->blockData->nRow > 0) {
              code = tsdbDataFileDoWriteBlockData(writer, writer->blockData);
              TSDB_CHECK_CODE(code, lino, _exit);
            }
H
Hongze Cheng 已提交
955

H
Hongze Cheng 已提交
956 957
            code = tsdbDataFileWriteBrinRecord(writer, record);
            TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
958
          } else {
H
Hongze Cheng 已提交
959 960
            code = tsdbDataFileReadBlockData(writer->ctx->reader, record, writer->ctx->blockData);
            TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
961

H
Hongze Cheng 已提交
962 963 964
            writer->ctx->blockDataIdx = 0;
            writer->ctx->brinBlockIdx++;
            break;
H
Hongze Cheng 已提交
965 966 967
          }
        }
      }
H
Hongze Cheng 已提交
968 969 970 971 972 973 974 975 976 977 978
    }

    // SBrinBlk
    if (writer->ctx->brinBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->brinBlkArray)) {
      writer->ctx->brinBlkArray = NULL;
      writer->ctx->tbHasOldData = false;
      goto _exit;
    }

    for (; writer->ctx->brinBlkArrayIdx < TARRAY2_SIZE(writer->ctx->brinBlkArray); writer->ctx->brinBlkArrayIdx++) {
      const SBrinBlk *brinBlk = TARRAY2_GET_PTR(writer->ctx->brinBlkArray, writer->ctx->brinBlkArrayIdx);
H
Hongze Cheng 已提交
979

H
Hongze Cheng 已提交
980
      if (brinBlk->minTbid.uid != writer->ctx->tbid->uid) {
H
Hongze Cheng 已提交
981
        writer->ctx->tbHasOldData = false;
H
Hongze Cheng 已提交
982
        goto _exit;
H
Hongze Cheng 已提交
983 984
      }

H
Hongze Cheng 已提交
985 986
      code = tsdbDataFileReadBrinBlock(writer->ctx->reader, brinBlk, writer->ctx->brinBlock);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
987

H
Hongze Cheng 已提交
988 989 990 991 992
      writer->ctx->brinBlockIdx = 0;
      writer->ctx->brinBlkArrayIdx++;
      break;
    }
  }
H
Hongze Cheng 已提交
993

H
Hongze Cheng 已提交
994 995 996 997 998 999
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}
H
Hongze Cheng 已提交
1000

H
Hongze Cheng 已提交
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
static int32_t tsdbDataFileDoWriteTSData(SDataFileWriter *writer, TSDBROW *row) {
  int32_t code = 0;
  int32_t lino = 0;

  if (writer->ctx->tbHasOldData) {
    TSDBKEY key[1];
    if (row->type == TSDBROW_ROW_FMT) {
      key->ts = row->pTSRow->ts;
      key->version = row->version;
    } else {
      key->ts = row->pBlockData->aTSKEY[row->iRow];
      key->version = row->pBlockData->aVersion[row->iRow];
H
Hongze Cheng 已提交
1013
    }
H
Hongze Cheng 已提交
1014 1015 1016

    code = tsdbDataFileDoWriteTableOldData(writer, key);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1017 1018 1019 1020 1021
  }

  code = tsdbDataFileDoWriteTSRow(writer, row);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1022 1023
_exit:
  if (code) {
H
Hongze Cheng 已提交
1024
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
1025 1026 1027
  }
  return code;
}
H
Hongze Cheng 已提交
1028

H
Hongze Cheng 已提交
1029
static int32_t tsdbDataFileWriteTableDataEnd(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
1030
  if (writer->ctx->tbid->uid == 0) return 0;
H
Hongze Cheng 已提交
1031 1032 1033 1034 1035

  int32_t code = 0;
  int32_t lino = 0;

  if (writer->ctx->tbHasOldData) {
H
Hongze Cheng 已提交
1036 1037 1038 1039 1040 1041 1042 1043 1044
    TSDBKEY key = {
        .ts = TSKEY_MAX,
        .version = VERSION_MAX,
    };

    code = tsdbDataFileDoWriteTableOldData(writer, &key);
    TSDB_CHECK_CODE(code, lino, _exit);

    ASSERT(writer->ctx->tbHasOldData == false);
H
Hongze Cheng 已提交
1045
  }
H
Hongze Cheng 已提交
1046

H
Hongze Cheng 已提交
1047
  code = tsdbDataFileDoWriteBlockData(writer, writer->blockData);
H
Hongze Cheng 已提交
1048 1049 1050 1051
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
H
Hongze Cheng 已提交
1052
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
1053 1054 1055
  }
  return code;
}
H
Hongze Cheng 已提交
1056

H
Hongze Cheng 已提交
1057
static int32_t tsdbDataFileWriteTableDataBegin(SDataFileWriter *writer, const TABLEID *tbid) {
H
Hongze Cheng 已提交
1058 1059
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1060

H
Hongze Cheng 已提交
1061 1062 1063
  ASSERT(writer->ctx->blockDataIdx == writer->ctx->blockData->nRow);
  ASSERT(writer->blockData->nRow == 0);

H
Hongze Cheng 已提交
1064 1065 1066
  SMetaInfo info;
  bool      drop = false;
  TABLEID   tbid1[1];
H
Hongze Cheng 已提交
1067
  writer->ctx->tbHasOldData = false;
H
Hongze Cheng 已提交
1068 1069
  while (writer->ctx->brinBlkArray) {  // skip data of previous table
    for (; writer->ctx->brinBlockIdx < BRIN_BLOCK_SIZE(writer->ctx->brinBlock); writer->ctx->brinBlockIdx++) {
H
Hongze Cheng 已提交
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
      TABLEID tbid2[1] = {{
          .suid = TARRAY2_GET(writer->ctx->brinBlock->suid, writer->ctx->brinBlockIdx),
          .uid = TARRAY2_GET(writer->ctx->brinBlock->uid, writer->ctx->brinBlockIdx),
      }};

      if (tbid2->uid == tbid->uid) {
        writer->ctx->tbHasOldData = true;
        goto _begin;
      } else if (tbid2->suid > tbid->suid || (tbid2->suid == tbid->suid && tbid2->uid > tbid->uid)) {
        goto _begin;
      } else {
        if (tbid2->uid != writer->ctx->tbid->uid) {
          if (drop && tbid1->uid == tbid2->uid) {
            continue;
          } else if (metaGetInfo(writer->config->tsdb->pVnode->pMeta, tbid2->uid, &info, NULL) != 0) {
            drop = true;
            *tbid1 = *tbid2;
            continue;
          } else {
            drop = false;
            writer->ctx->tbid[0] = *tbid2;
          }
H
Hongze Cheng 已提交
1092
        }
H
Hongze Cheng 已提交
1093

H
Hongze Cheng 已提交
1094 1095
        SBrinRecord record[1];
        tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, record);
H
Hongze Cheng 已提交
1096

H
Hongze Cheng 已提交
1097 1098 1099 1100
        code = tsdbDataFileWriteBrinRecord(writer, record);
        TSDB_CHECK_CODE(code, lino, _exit);
      }
    }
H
Hongze Cheng 已提交
1101

H
Hongze Cheng 已提交
1102 1103 1104 1105
    if (writer->ctx->brinBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->brinBlkArray)) {
      writer->ctx->brinBlkArray = NULL;
      break;
    }
H
Hongze Cheng 已提交
1106

H
Hongze Cheng 已提交
1107 1108
    for (; writer->ctx->brinBlkArrayIdx < TARRAY2_SIZE(writer->ctx->brinBlkArray); writer->ctx->brinBlkArrayIdx++) {
      const SBrinBlk *brinBlk = TARRAY2_GET_PTR(writer->ctx->brinBlkArray, writer->ctx->brinBlkArrayIdx);
H
Hongze Cheng 已提交
1109

H
Hongze Cheng 已提交
1110 1111
      code = tsdbDataFileReadBrinBlock(writer->ctx->reader, brinBlk, writer->ctx->brinBlock);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1112

H
Hongze Cheng 已提交
1113 1114 1115
      writer->ctx->brinBlockIdx = 0;
      writer->ctx->brinBlkArrayIdx++;
      break;
H
Hongze Cheng 已提交
1116 1117
    }
  }
H
Hongze Cheng 已提交
1118

H
Hongze Cheng 已提交
1119 1120
_begin:
  writer->ctx->tbid[0] = *tbid;
H
Hongze Cheng 已提交
1121

H
Hongze Cheng 已提交
1122
  if (tbid->uid == INT64_MAX) goto _exit;
H
Hongze Cheng 已提交
1123 1124 1125 1126

  code = tsdbUpdateSkmTb(writer->config->tsdb, tbid, writer->config->skmTb);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1127
  code = tBlockDataInit(writer->blockData, writer->ctx->tbid, writer->config->skmTb->pTSchema, NULL, 0);
H
Hongze Cheng 已提交
1128 1129
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1130 1131
_exit:
  if (code) {
H
Hongze Cheng 已提交
1132
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
1133 1134 1135
  }
  return code;
}
H
Hongze Cheng 已提交
1136

H
Hongze Cheng 已提交
1137 1138 1139 1140 1141 1142 1143
int32_t tsdbFileWriteHeadFooter(STsdbFD *fd, int64_t *fileSize, const SHeadFooter *footer) {
  int32_t code = tsdbWriteFile(fd, *fileSize, (const uint8_t *)footer, sizeof(*footer));
  if (code) return code;
  *fileSize += sizeof(*footer);
  return 0;
}

H
Hongze Cheng 已提交
1144
static int32_t tsdbDataFileWriteHeadFooter(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
1145 1146 1147
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1148
  code = tsdbFileWriteHeadFooter(writer->fd[TSDB_FTYPE_HEAD], &writer->files[TSDB_FTYPE_HEAD].size, writer->headFooter);
H
Hongze Cheng 已提交
1149
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1150

H
Hongze Cheng 已提交
1151 1152 1153 1154 1155 1156
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}
H
Hongze Cheng 已提交
1157

H
Hongze Cheng 已提交
1158
static int32_t tsdbDataFileDoWriteTombBlock(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
1159
  if (TOMB_BLOCK_SIZE(writer->tombBlock) == 0) return 0;
H
Hongze Cheng 已提交
1160 1161 1162 1163

  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1164 1165
  code = tsdbFileDoWriteTombBlock(writer->fd[TSDB_FTYPE_TOMB], writer->tombBlock, writer->config->cmprAlg,
                                  &writer->files[TSDB_FTYPE_TOMB].size, writer->tombBlkArray, writer->config->bufArr);
H
Hongze Cheng 已提交
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}

static int32_t tsdbDataFileDoWriteTombBlk(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
1176
  ASSERT(TARRAY2_SIZE(writer->tombBlkArray) > 0);
H
Hongze Cheng 已提交
1177

H
Hongze Cheng 已提交
1178 1179 1180
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1181 1182
  code = tsdbFileDoWriteTombBlk(writer->fd[TSDB_FTYPE_TOMB], writer->tombBlkArray, writer->tombFooter->tombBlkPtr,
                                &writer->files[TSDB_FTYPE_TOMB].size);
H
Hongze Cheng 已提交
1183
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1184 1185 1186 1187 1188 1189 1190 1191

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}

H
Hongze Cheng 已提交
1192
static int32_t tsdbDataFileWriteTombFooter(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
1193 1194
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1195

H
Hongze Cheng 已提交
1196 1197
  code = tsdbWriteFile(writer->fd[TSDB_FTYPE_TOMB], writer->files[TSDB_FTYPE_TOMB].size,
                       (const uint8_t *)writer->tombFooter, sizeof(STombFooter));
H
Hongze Cheng 已提交
1198
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1199
  writer->files[TSDB_FTYPE_TOMB].size += sizeof(STombFooter);
H
Hongze Cheng 已提交
1200

H
Hongze Cheng 已提交
1201 1202 1203 1204 1205 1206
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}
H
Hongze Cheng 已提交
1207

H
Hongze Cheng 已提交
1208 1209 1210
static int32_t tsdbDataFileDoWriteTombRecord(SDataFileWriter *writer, const STombRecord *record) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1211

H
Hongze Cheng 已提交
1212
  while (writer->ctx->hasOldTomb) {
H
Hongze Cheng 已提交
1213
    for (; writer->ctx->tombBlockIdx < TOMB_BLOCK_SIZE(writer->ctx->tombBlock); writer->ctx->tombBlockIdx++) {
H
Hongze Cheng 已提交
1214 1215
      STombRecord record1[1];
      tTombBlockGet(writer->ctx->tombBlock, writer->ctx->tombBlockIdx, record1);
H
Hongze Cheng 已提交
1216

H
Hongze Cheng 已提交
1217
      int32_t c = tTombRecordCompare(record, record1);
H
Hongze Cheng 已提交
1218 1219 1220
      if (c < 0) {
        break;
      } else if (c > 0) {
H
Hongze Cheng 已提交
1221
        code = tTombBlockPut(writer->tombBlock, record1);
H
Hongze Cheng 已提交
1222 1223
        TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1224
        if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
H
Hongze Cheng 已提交
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
          code = tsdbDataFileDoWriteTombBlock(writer);
          TSDB_CHECK_CODE(code, lino, _exit);
        }
      } else {
        ASSERT(0);
      }
    }

    if (writer->ctx->tombBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->tombBlkArray)) {
      writer->ctx->hasOldTomb = false;
      break;
    }
H
Hongze Cheng 已提交
1237

H
Hongze Cheng 已提交
1238 1239 1240
    for (; writer->ctx->tombBlkArrayIdx < TARRAY2_SIZE(writer->ctx->tombBlkArray); ++writer->ctx->tombBlkArrayIdx) {
      const STombBlk *tombBlk = TARRAY2_GET_PTR(writer->ctx->tombBlkArray, writer->ctx->tombBlkArrayIdx);

H
Hongze Cheng 已提交
1241
      code = tsdbDataFileReadTombBlock(writer->ctx->reader, tombBlk, writer->ctx->tombBlock);
H
Hongze Cheng 已提交
1242 1243
      TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1244
      writer->ctx->tombBlockIdx = 0;
H
Hongze Cheng 已提交
1245 1246 1247 1248 1249 1250 1251 1252
      writer->ctx->tombBlkArrayIdx++;
      break;
    }
  }

_write:
  if (record->suid == INT64_MAX) goto _exit;

H
Hongze Cheng 已提交
1253
  code = tTombBlockPut(writer->tombBlock, record);
H
Hongze Cheng 已提交
1254 1255
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1256
  if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
H
Hongze Cheng 已提交
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
    code = tsdbDataFileDoWriteTombBlock(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}

H
Hongze Cheng 已提交
1268 1269 1270 1271 1272 1273 1274
int32_t tsdbFileWriteBrinBlk(STsdbFD *fd, TBrinBlkArray *brinBlkArray, SFDataPtr *ptr, int64_t *fileSize) {
  ASSERT(TARRAY2_SIZE(brinBlkArray) > 0);
  ptr->offset = *fileSize;
  ptr->size = TARRAY2_DATA_LEN(brinBlkArray);

  int32_t code = tsdbWriteFile(fd, ptr->offset, (uint8_t *)TARRAY2_DATA(brinBlkArray), ptr->size);
  if (code) return code;
H
Hongze Cheng 已提交
1275

H
Hongze Cheng 已提交
1276 1277 1278 1279 1280
  *fileSize += ptr->size;
  return 0;
}

static int32_t tsdbDataFileWriteBrinBlk(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
1281 1282 1283
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1284 1285
  code = tsdbFileWriteBrinBlk(writer->fd[TSDB_FTYPE_HEAD], writer->brinBlkArray, writer->headFooter->brinBlkPtr,
                              &writer->files[TSDB_FTYPE_HEAD].size);
H
Hongze Cheng 已提交
1286 1287 1288 1289 1290 1291 1292 1293 1294
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}

H
Hongze Cheng 已提交
1295 1296 1297 1298
static int32_t tsdbDataFileWriterCloseCommit(SDataFileWriter *writer, TFileOpArray *opArr) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1299
  int32_t  ftype;
H
Hongze Cheng 已提交
1300
  STFileOp op;
H
Hongze Cheng 已提交
1301

H
Hongze Cheng 已提交
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
  if (writer->fd[TSDB_FTYPE_HEAD]) {
    TABLEID tbid[1] = {{
        .suid = INT64_MAX,
        .uid = INT64_MAX,
    }};

    code = tsdbDataFileWriteTableDataEnd(writer);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = tsdbDataFileWriteTableDataBegin(writer, tbid);
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1314 1315 1316
    code = tsdbDataFileWriteBrinBlock(writer);
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1317
    code = tsdbDataFileWriteBrinBlk(writer);
H
Hongze Cheng 已提交
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
    TSDB_CHECK_CODE(code, lino, _exit);

    code = tsdbDataFileWriteHeadFooter(writer);
    TSDB_CHECK_CODE(code, lino, _exit);

    // .head
    ftype = TSDB_FTYPE_HEAD;
    if (writer->config->files[ftype].exist) {
      op = (STFileOp){
          .optype = TSDB_FOP_REMOVE,
          .fid = writer->config->fid,
          .of = writer->config->files[ftype].file,
      };
      code = TARRAY2_APPEND(opArr, op);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
1334
    op = (STFileOp){
H
Hongze Cheng 已提交
1335
        .optype = TSDB_FOP_CREATE,
H
Hongze Cheng 已提交
1336
        .fid = writer->config->fid,
H
Hongze Cheng 已提交
1337
        .nf = writer->files[ftype],
H
Hongze Cheng 已提交
1338 1339 1340
    };
    code = TARRAY2_APPEND(opArr, op);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1341

H
Hongze Cheng 已提交
1342 1343
    // .data
    ftype = TSDB_FTYPE_DATA;
H
Hongze Cheng 已提交
1344
    if (!writer->config->files[ftype].exist) {
H
Hongze Cheng 已提交
1345
      op = (STFileOp){
H
Hongze Cheng 已提交
1346 1347 1348 1349
          .optype = TSDB_FOP_CREATE,
          .fid = writer->config->fid,
          .nf = writer->files[ftype],
      };
H
Hongze Cheng 已提交
1350 1351 1352 1353
      code = TARRAY2_APPEND(opArr, op);
      TSDB_CHECK_CODE(code, lino, _exit);
    } else if (writer->config->files[ftype].file.size != writer->files[ftype].size) {
      op = (STFileOp){
H
Hongze Cheng 已提交
1354 1355 1356 1357 1358
          .optype = TSDB_FOP_MODIFY,
          .fid = writer->config->fid,
          .of = writer->config->files[ftype].file,
          .nf = writer->files[ftype],
      };
H
Hongze Cheng 已提交
1359 1360
      code = TARRAY2_APPEND(opArr, op);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1361
    }
H
Hongze Cheng 已提交
1362

H
Hongze Cheng 已提交
1363 1364
    // .sma
    ftype = TSDB_FTYPE_SMA;
H
Hongze Cheng 已提交
1365
    if (!writer->config->files[ftype].exist) {
H
Hongze Cheng 已提交
1366
      op = (STFileOp){
H
Hongze Cheng 已提交
1367 1368 1369 1370
          .optype = TSDB_FOP_CREATE,
          .fid = writer->config->fid,
          .nf = writer->files[ftype],
      };
H
Hongze Cheng 已提交
1371 1372 1373 1374
      code = TARRAY2_APPEND(opArr, op);
      TSDB_CHECK_CODE(code, lino, _exit);
    } else if (writer->config->files[ftype].file.size != writer->files[ftype].size) {
      op = (STFileOp){
H
Hongze Cheng 已提交
1375 1376 1377 1378 1379
          .optype = TSDB_FOP_MODIFY,
          .fid = writer->config->fid,
          .of = writer->config->files[ftype].file,
          .nf = writer->files[ftype],
      };
H
Hongze Cheng 已提交
1380 1381
      code = TARRAY2_APPEND(opArr, op);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1382 1383
    }
  }
H
Hongze Cheng 已提交
1384

H
Hongze Cheng 已提交
1385 1386 1387 1388 1389 1390
  if (writer->fd[TSDB_FTYPE_TOMB]) {
    STombRecord record[1] = {{
        .suid = INT64_MAX,
        .uid = INT64_MAX,
        .version = INT64_MAX,
    }};
H
Hongze Cheng 已提交
1391

H
Hongze Cheng 已提交
1392 1393 1394 1395
    code = tsdbDataFileDoWriteTombRecord(writer, record);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = tsdbDataFileDoWriteTombBlock(writer);
H
Hongze Cheng 已提交
1396
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428

    code = tsdbDataFileDoWriteTombBlk(writer);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = tsdbDataFileWriteTombFooter(writer);
    TSDB_CHECK_CODE(code, lino, _exit);

    ftype = TSDB_FTYPE_SMA;
    if (writer->config->files[ftype].exist) {
      op = (STFileOp){
          .optype = TSDB_FOP_REMOVE,
          .fid = writer->config->fid,
          .of = writer->config->files[ftype].file,
      };
      code = TARRAY2_APPEND(opArr, op);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
    op = (STFileOp){
        .optype = TSDB_FOP_CREATE,
        .fid = writer->config->fid,
        .nf = writer->files[ftype],
    };
    code = TARRAY2_APPEND(opArr, op);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
    if (writer->fd[i]) {
      code = tsdbFsyncFile(writer->fd[i]);
      TSDB_CHECK_CODE(code, lino, _exit);
      tsdbCloseFile(&writer->fd[i]);
    }
H
Hongze Cheng 已提交
1429
  }
H
Hongze Cheng 已提交
1430 1431 1432

_exit:
  if (code) {
H
Hongze Cheng 已提交
1433
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
1434 1435 1436
  }
  return code;
}
H
Hongze Cheng 已提交
1437

H
Hongze Cheng 已提交
1438 1439 1440 1441
static int32_t tsdbDataFileWriterOpenDataFD(SDataFileWriter *writer) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1442 1443 1444 1445 1446
  int32_t ftypes[] = {TSDB_FTYPE_HEAD, TSDB_FTYPE_DATA, TSDB_FTYPE_SMA};

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

H
Hongze Cheng 已提交
1447 1448 1449
    char    fname[TSDB_FILENAME_LEN];
    int32_t flag = TD_FILE_READ | TD_FILE_WRITE;

H
Hongze Cheng 已提交
1450
    if (writer->files[ftype].size == 0) {
H
Hongze Cheng 已提交
1451 1452 1453
      flag |= (TD_FILE_CREATE | TD_FILE_TRUNC);
    }

H
Hongze Cheng 已提交
1454
    tsdbTFileName(writer->config->tsdb, &writer->files[ftype], fname);
H
Hongze Cheng 已提交
1455
    code = tsdbOpenFile(fname, writer->config->szPage, flag, &writer->fd[ftype]);
H
Hongze Cheng 已提交
1456 1457
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1458
    if (writer->files[ftype].size == 0) {
H
Hongze Cheng 已提交
1459 1460
      uint8_t hdr[TSDB_FHDR_SIZE] = {0};

H
Hongze Cheng 已提交
1461
      code = tsdbWriteFile(writer->fd[ftype], 0, hdr, TSDB_FHDR_SIZE);
H
Hongze Cheng 已提交
1462 1463
      TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1464
      writer->files[ftype].size += TSDB_FHDR_SIZE;
H
Hongze Cheng 已提交
1465 1466 1467
    }
  }

H
Hongze Cheng 已提交
1468
  if (writer->ctx->reader) {
H
Hongze Cheng 已提交
1469 1470
    code = tsdbDataFileReadBrinBlk(writer->ctx->reader, &writer->ctx->brinBlkArray);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1471 1472
  }

H
Hongze Cheng 已提交
1473 1474 1475 1476 1477 1478 1479
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}

H
Hongze Cheng 已提交
1480
int32_t tsdbDataFileWriterOpen(const SDataFileWriterConfig *config, SDataFileWriter **writer) {
H
Hongze Cheng 已提交
1481
  writer[0] = taosMemoryCalloc(1, sizeof(*writer[0]));
H
Hongze Cheng 已提交
1482 1483 1484 1485 1486 1487 1488
  if (!writer[0]) return TSDB_CODE_OUT_OF_MEMORY;

  writer[0]->config[0] = config[0];
  return 0;
}

int32_t tsdbDataFileWriterClose(SDataFileWriter **writer, bool abort, TFileOpArray *opArr) {
1489 1490
  if (writer[0] == NULL) return 0;

H
Hongze Cheng 已提交
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
  int32_t code = 0;
  int32_t lino = 0;

  if (writer[0]->ctx->opened) {
    if (abort) {
      code = tsdbDataFileWriterCloseAbort(writer[0]);
      TSDB_CHECK_CODE(code, lino, _exit);
    } else {
      code = tsdbDataFileWriterCloseCommit(writer[0], opArr);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
    tsdbDataFileWriterDoClose(writer[0]);
  }
  taosMemoryFree(writer[0]);
  writer[0] = NULL;

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer[0]->config->tsdb->pVnode), lino, code);
  }
  return code;
}

H
Hongze Cheng 已提交
1514
int32_t tsdbDataFileWriteRow(SDataFileWriter *writer, SRowInfo *row) {
H
Hongze Cheng 已提交
1515 1516
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1517 1518 1519 1520 1521 1522

  if (!writer->ctx->opened) {
    code = tsdbDataFileWriterDoOpen(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
1523
  if (writer->fd[TSDB_FTYPE_HEAD] == NULL) {
H
Hongze Cheng 已提交
1524 1525 1526 1527
    code = tsdbDataFileWriterOpenDataFD(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
1528
  if (row->uid != writer->ctx->tbid->uid) {
H
Hongze Cheng 已提交
1529 1530 1531
    code = tsdbDataFileWriteTableDataEnd(writer);
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1532
    code = tsdbDataFileWriteTableDataBegin(writer, (TABLEID *)row);
H
Hongze Cheng 已提交
1533 1534 1535
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
1536
  code = tsdbDataFileDoWriteTSData(writer, &row->row);
H
Hongze Cheng 已提交
1537 1538
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1539 1540
_exit:
  if (code) {
H
Hongze Cheng 已提交
1541
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
1542 1543
  }
  return code;
H
Hongze Cheng 已提交
1544
}
H
Hongze Cheng 已提交
1545

H
Hongze Cheng 已提交
1546
int32_t tsdbDataFileWriteBlockData(SDataFileWriter *writer, SBlockData *bData) {
H
Hongze Cheng 已提交
1547 1548
  if (bData->nRow == 0) return 0;

H
Hongze Cheng 已提交
1549 1550 1551
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1552
  ASSERT(bData->uid);
H
Hongze Cheng 已提交
1553

H
Hongze Cheng 已提交
1554 1555 1556 1557
  if (!writer->ctx->opened) {
    code = tsdbDataFileWriterDoOpen(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
1558

H
Hongze Cheng 已提交
1559
  if (writer->fd[TSDB_FTYPE_DATA] == NULL) {
H
Hongze Cheng 已提交
1560 1561 1562
    code = tsdbDataFileWriterOpenDataFD(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
1563

H
Hongze Cheng 已提交
1564 1565 1566
  if (bData->uid != writer->ctx->tbid->uid) {
    code = tsdbDataFileWriteTableDataEnd(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1567

H
Hongze Cheng 已提交
1568 1569 1570 1571
    code = tsdbDataFileWriteTableDataBegin(writer, (TABLEID *)bData);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
  if (writer->ctx->tbHasOldData) {
    TSDBKEY key = {
        .ts = bData->aTSKEY[0],
        .version = bData->aVersion[0],
    };

    code = tsdbDataFileDoWriteTableOldData(writer, &key);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
1582 1583
  if (!writer->ctx->tbHasOldData       //
      && writer->blockData->nRow == 0  //
H
Hongze Cheng 已提交
1584
  ) {
H
Hongze Cheng 已提交
1585
    code = tsdbDataFileDoWriteBlockData(writer, bData);
H
Hongze Cheng 已提交
1586 1587 1588 1589 1590 1591 1592 1593
    TSDB_CHECK_CODE(code, lino, _exit);
  } else {
    for (int32_t i = 0; i < bData->nRow; ++i) {
      TSDBROW row[1] = {tsdbRowFromBlockData(bData, i)};
      code = tsdbDataFileDoWriteTSData(writer, row);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
  }
H
Hongze Cheng 已提交
1594 1595 1596 1597 1598 1599

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
H
Hongze Cheng 已提交
1600 1601
}

H
Hongze Cheng 已提交
1602
int32_t tsdbDataFileFlush(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
1603 1604
  ASSERT(writer->ctx->opened);

H
Hongze Cheng 已提交
1605
  if (writer->blockData->nRow == 0) return 0;
H
Hongze Cheng 已提交
1606 1607
  if (writer->ctx->tbHasOldData) return 0;

H
Hongze Cheng 已提交
1608
  return tsdbDataFileDoWriteBlockData(writer, writer->blockData);
H
Hongze Cheng 已提交
1609
}
H
Hongze Cheng 已提交
1610

H
Hongze Cheng 已提交
1611 1612 1613 1614 1615
static int32_t tsdbDataFileWriterOpenTombFD(SDataFileWriter *writer) {
  int32_t code = 0;
  int32_t lino = 0;

  char    fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
1616
  int32_t ftype = TSDB_FTYPE_TOMB;
H
Hongze Cheng 已提交
1617

H
Hongze Cheng 已提交
1618
  ASSERT(writer->files[ftype].size == 0);
H
Hongze Cheng 已提交
1619

H
Hongze Cheng 已提交
1620
  int32_t flag = (TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
H
Hongze Cheng 已提交
1621

H
Hongze Cheng 已提交
1622 1623 1624
  tsdbTFileName(writer->config->tsdb, writer->files + ftype, fname);
  code = tsdbOpenFile(fname, writer->config->szPage, flag, &writer->fd[ftype]);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1625

H
Hongze Cheng 已提交
1626 1627 1628 1629
  uint8_t hdr[TSDB_FHDR_SIZE] = {0};
  code = tsdbWriteFile(writer->fd[ftype], 0, hdr, TSDB_FHDR_SIZE);
  TSDB_CHECK_CODE(code, lino, _exit);
  writer->files[ftype].size += TSDB_FHDR_SIZE;
H
Hongze Cheng 已提交
1630

H
Hongze Cheng 已提交
1631 1632 1633
  if (writer->ctx->reader) {
    code = tsdbDataFileReadTombBlk(writer->ctx->reader, &writer->ctx->tombBlkArray);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1634

H
Hongze Cheng 已提交
1635 1636
    if (TARRAY2_SIZE(writer->ctx->tombBlkArray) > 0) {
      writer->ctx->hasOldTomb = true;
H
Hongze Cheng 已提交
1637 1638
    }

H
Hongze Cheng 已提交
1639
    writer->ctx->tombBlkArrayIdx = 0;
H
Hongze Cheng 已提交
1640 1641
    tTombBlockClear(writer->ctx->tombBlock);
    writer->ctx->tombBlockIdx = 0;
H
Hongze Cheng 已提交
1642
  }
H
Hongze Cheng 已提交
1643 1644 1645 1646 1647 1648 1649 1650

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}

H
Hongze Cheng 已提交
1651 1652 1653 1654 1655 1656 1657 1658 1659
int32_t tsdbDataFileWriteTombRecord(SDataFileWriter *writer, const STombRecord *record) {
  int32_t code = 0;
  int32_t lino = 0;

  if (!writer->ctx->opened) {
    code = tsdbDataFileWriterDoOpen(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
1660
  if (writer->fd[TSDB_FTYPE_TOMB] == NULL) {
H
Hongze Cheng 已提交
1661 1662
    code = tsdbDataFileWriterOpenTombFD(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1663 1664
  }

H
Hongze Cheng 已提交
1665
  code = tsdbDataFileDoWriteTombRecord(writer, record);
H
Hongze Cheng 已提交
1666 1667 1668 1669 1670 1671 1672 1673
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}