tsdbDataFileRW.c 50.2 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
extern int32_t tsdbFileWriteTombBlock(STsdbFD *fd, STombBlock *tombBlock, int8_t cmprAlg, int64_t *fileSize,
                                      TTombBlkArray *tombBlkArray, uint8_t **bufArr);
extern int32_t tsdbFileWriteTombBlk(STsdbFD *fd, const TTombBlkArray *tombBlkArray, SFDataPtr *ptr, int64_t *fileSize);
H
Hongze Cheng 已提交
21

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

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

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

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

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

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

  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
49 50 51 52 53 54 55
  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 已提交
56 57 58 59 60 61 62 63 64 65
  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 已提交
66
  if (reader->ctx->tombFooterLoaded) return 0;
H
Hongze Cheng 已提交
67 68 69 70

  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
71 72 73 74 75 76
  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 已提交
77
  reader->ctx->tombFooterLoaded = true;
H
Hongze Cheng 已提交
78 79 80 81 82 83 84 85

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

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

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

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

  if (fname) {
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
H
Hongze Cheng 已提交
103 104 105 106
      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 已提交
107
    }
H
Hongze Cheng 已提交
108 109
  } else {
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
H
Hongze Cheng 已提交
110 111 112 113 114 115
      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 已提交
116 117
    }
  }
H
Hongze Cheng 已提交
118 119 120

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
152 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
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 已提交
252 253
int32_t tsdbDataFileReadBlockDataByColumn(SDataFileReader *reader, const SBrinRecord *record, SBlockData *bData,
                                          STSchema *pTSchema, int16_t cids[], int32_t ncid) {
H
Hongze Cheng 已提交
254 255 256
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
257 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
  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 已提交
355 356 357 358 359 360 361 362

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

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

H
Hongze Cheng 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
  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 已提交
388 389 390 391 392 393 394 395

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

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

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

H
Hongze Cheng 已提交
404 405 406 407 408 409
    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 已提交
410

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

      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 已提交
422 423 424 425 426 427
    }

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

H
Hongze Cheng 已提交
428 429 430 431 432 433 434 435 436 437 438
_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 已提交
439 440 441 442 443 444 445
  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 已提交
446
  tTombBlockClear(tData);
H
Hongze Cheng 已提交
447
  for (int32_t i = 0; i < ARRAY_SIZE(tData->dataArr); ++i) {
H
Hongze Cheng 已提交
448 449
    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 已提交
450 451
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
452 453 454 455
    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 已提交
456
  }
H
Hongze Cheng 已提交
457
  ASSERT(size == tombBlk->dp->size);
H
Hongze Cheng 已提交
458

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

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

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

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

H
Hongze Cheng 已提交
478
    // for ts data
H
Hongze Cheng 已提交
479 480 481 482 483 484 485 486 487
    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 已提交
488 489 490 491
    // for tomb data
    bool                 hasOldTomb;
    const TTombBlkArray *tombBlkArray;
    int32_t              tombBlkArrayIdx;
H
Hongze Cheng 已提交
492 493
    STombBlock           tombBlock[1];
    int32_t              tombBlockIdx;
H
Hongze Cheng 已提交
494
  } ctx[1];
H
Hongze Cheng 已提交
495

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

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

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

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

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

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

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

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

  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 已提交
536 537
  return 0;
}
H
Hongze Cheng 已提交
538

H
Hongze Cheng 已提交
539 540 541 542 543 544 545 546 547
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 已提交
548
          .bufArr = writer->config->bufArr,
H
Hongze Cheng 已提交
549 550 551 552
      }};

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

      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 已提交
571
static int32_t tsdbDataFileWriterDoOpen(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
572 573
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
574
  int32_t ftype;
H
Hongze Cheng 已提交
575 576 577 578 579 580

  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 已提交
581 582
  code = tsdbDataFileWriterDoOpenReader(writer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
583

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

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

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

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

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

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

H
Hongze Cheng 已提交
641 642 643
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 已提交
644

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

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

H
Hongze Cheng 已提交
672 673 674
  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 已提交
675
    }
H
Hongze Cheng 已提交
676 677
    if (brinBlk->maxVer < TARRAY2_GET(brinBlock->maxVer, i)) {
      brinBlk->maxVer = TARRAY2_GET(brinBlock->maxVer, i);
H
Hongze Cheng 已提交
678 679 680 681
    }
  }

  // write to file
H
Hongze Cheng 已提交
682 683 684 685
  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 已提交
686

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

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

H
Hongze Cheng 已提交
694 695 696 697
  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 已提交
698

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

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

H
Hongze Cheng 已提交
706 707 708 709 710 711 712 713 714
#if 0
  SBrinRecord record;
  for (int32_t i = 0; i < BRIN_BLOCK_SIZE(brinBlock); i++) {
    tBrinBlockGet(brinBlock, i, &record);
    tsdbInfo("write brin block, block num:%04d, idx:%04d suid:%ld, uid:%ld, offset:%ld, numRow:%d, count:%d",
             TARRAY2_SIZE(brinBlkArray), i, record.suid, record.uid, record.blockOffset, record.numRow, record.count);
  }
#endif

H
Hongze Cheng 已提交
715
  // append to brinBlkArray
H
Hongze Cheng 已提交
716 717
  code = TARRAY2_APPEND_PTR(brinBlkArray, brinBlk);
  if (code) return code;
H
Hongze Cheng 已提交
718

H
Hongze Cheng 已提交
719 720 721 722 723 724 725 726 727 728 729 730 731 732
  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 已提交
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760

_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 已提交
761 762
  if (bData->nRow == 0) return 0;

H
Hongze Cheng 已提交
763 764
  ASSERT(bData->uid);

H
Hongze Cheng 已提交
765 766 767
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
768 769 770 771 772 773 774
  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 已提交
775 776
      .minVer = bData->aVersion[0],
      .maxVer = bData->aVersion[0],
H
Hongze Cheng 已提交
777 778 779 780 781 782 783
      .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 已提交
784 785 786
  }};

  for (int32_t i = 1; i < bData->nRow; ++i) {
H
Hongze Cheng 已提交
787 788 789 790 791 792 793 794
    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 已提交
795 796
    }
  }
H
Hongze Cheng 已提交
797

H
Hongze Cheng 已提交
798
  // to .data file
H
Hongze Cheng 已提交
799 800 801 802
  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 已提交
803

H
Hongze Cheng 已提交
804 805
  record->blockKeySize = sizeArr[3] + sizeArr[2];
  record->blockSize = sizeArr[0] + sizeArr[1] + record->blockKeySize;
H
Hongze Cheng 已提交
806

H
Hongze Cheng 已提交
807 808
  for (int32_t i = 3; i >= 0; --i) {
    if (sizeArr[i]) {
H
Hongze Cheng 已提交
809
      code = tsdbWriteFile(writer->fd[TSDB_FTYPE_DATA], writer->files[TSDB_FTYPE_DATA].size, writer->config->bufArr[i],
H
Hongze Cheng 已提交
810 811
                           sizeArr[i]);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
812
      writer->files[TSDB_FTYPE_DATA].size += sizeArr[i];
H
Hongze Cheng 已提交
813 814 815
    }
  }

H
Hongze Cheng 已提交
816
  // to .sma file
H
Hongze Cheng 已提交
817 818
  for (int32_t i = 0; i < bData->nColData; ++i) {
    SColData *colData = bData->aColData + i;
H
Hongze Cheng 已提交
819
    if ((!colData->smaOn) || ((colData->flag & HAS_VALUE) == 0)) continue;
H
Hongze Cheng 已提交
820 821 822 823

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

H
Hongze Cheng 已提交
824 825 826
    int32_t size = tPutColumnDataAgg(NULL, sma);

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

H
Hongze Cheng 已提交
829 830 831
    tPutColumnDataAgg(writer->config->bufArr[0] + record->smaSize, sma);
    record->smaSize += size;
  }
H
Hongze Cheng 已提交
832

H
Hongze Cheng 已提交
833
  if (record->smaSize > 0) {
H
Hongze Cheng 已提交
834
    code = tsdbWriteFile(writer->fd[TSDB_FTYPE_SMA], record->smaOffset, writer->config->bufArr[0], record->smaSize);
H
Hongze Cheng 已提交
835
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
836
    writer->files[TSDB_FTYPE_SMA].size += record->smaSize;
H
Hongze Cheng 已提交
837 838
  }

H
Hongze Cheng 已提交
839 840
  // append SBrinRecord
  code = tsdbDataFileWriteBrinRecord(writer, record);
H
Hongze Cheng 已提交
841 842
  TSDB_CHECK_CODE(code, lino, _exit);

H
fix bug  
Hongze Cheng 已提交
843
  tBlockDataClear(bData);
H
Hongze Cheng 已提交
844 845 846

_exit:
  if (code) {
H
Hongze Cheng 已提交
847
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
848 849 850
  }
  return code;
}
H
Hongze Cheng 已提交
851

H
Hongze Cheng 已提交
852 853 854 855 856 857
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 已提交
858 859 860 861 862 863 864
  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 已提交
865

H
Hongze Cheng 已提交
866 867
  code =
      tsdbWriteFile(writer->fd[ftype], blockIdx->offset, (const uint8_t *)TARRAY2_DATA(dataBlkArray), blockIdx->size);
H
Hongze Cheng 已提交
868
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
869
  writer->files[ftype].size += blockIdx->size;
H
Hongze Cheng 已提交
870

H
Hongze Cheng 已提交
871 872
_exit:
  if (code) {
H
Hongze Cheng 已提交
873
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
874 875 876
  }
  return code;
}
H
Hongze Cheng 已提交
877

H
Hongze Cheng 已提交
878 879 880 881
static int32_t tsdbDataFileDoWriteTSRow(SDataFileWriter *writer, TSDBROW *row) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
882
  // update/append
H
Hongze Cheng 已提交
883
  if (row->type == TSDBROW_ROW_FMT) {
H
Hongze Cheng 已提交
884 885
    code = tsdbUpdateSkmRow(writer->config->tsdb, writer->ctx->tbid, TSDBROW_SVERSION(row), writer->config->skmRow);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
886 887
  }

H
Hongze Cheng 已提交
888 889 890 891 892 893 894 895
  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 已提交
896 897 898
  if (key->version <= writer->config->compactVersion                        //
      && writer->blockData->nRow > 0                                        //
      && writer->blockData->aTSKEY[writer->blockData->nRow - 1] == key->ts  //
H
Hongze Cheng 已提交
899
  ) {
H
Hongze Cheng 已提交
900
    code = tBlockDataUpdateRow(writer->blockData, row, writer->config->skmRow->pTSchema);
H
Hongze Cheng 已提交
901 902
    TSDB_CHECK_CODE(code, lino, _exit);
  } else {
H
Hongze Cheng 已提交
903
    if (writer->blockData->nRow >= writer->config->maxRow) {
H
Hongze Cheng 已提交
904
      code = tsdbDataFileDoWriteBlockData(writer, writer->blockData);
H
Hongze Cheng 已提交
905 906
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
907

H
Hongze Cheng 已提交
908
    code = tBlockDataAppendRow(writer->blockData, row, writer->config->skmRow->pTSchema, writer->ctx->tbid->uid);
H
Hongze Cheng 已提交
909 910 911
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
912 913 914 915 916 917 918
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}

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

H
Hongze Cheng 已提交
922 923 924
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
925
  for (;;) {
H
Hongze Cheng 已提交
926
    for (;;) {
H
Hongze Cheng 已提交
927 928 929 930 931 932 933
      // 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 已提交
934 935
          TSDBROW row = tsdbRowFromBlockData(writer->ctx->blockData, writer->ctx->blockDataIdx);
          code = tsdbDataFileDoWriteTSRow(writer, &row);
H
Hongze Cheng 已提交
936
          TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
937
        }
H
Hongze Cheng 已提交
938 939 940 941 942 943
      }

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

H
Hongze Cheng 已提交
945 946 947 948
      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 已提交
949
        }
H
Hongze Cheng 已提交
950

H
Hongze Cheng 已提交
951 952 953 954 955 956 957
        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);
958
          if (key->ts > record->lastKey || (key->ts == record->lastKey && key->version > record->maxVer)) {
H
Hongze Cheng 已提交
959 960 961 962
            if (writer->blockData->nRow > 0) {
              code = tsdbDataFileDoWriteBlockData(writer, writer->blockData);
              TSDB_CHECK_CODE(code, lino, _exit);
            }
H
Hongze Cheng 已提交
963

H
Hongze Cheng 已提交
964 965
            code = tsdbDataFileWriteBrinRecord(writer, record);
            TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
966
          } else {
H
Hongze Cheng 已提交
967 968
            code = tsdbDataFileReadBlockData(writer->ctx->reader, record, writer->ctx->blockData);
            TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
969

H
Hongze Cheng 已提交
970 971 972
            writer->ctx->blockDataIdx = 0;
            writer->ctx->brinBlockIdx++;
            break;
H
Hongze Cheng 已提交
973 974 975
          }
        }
      }
H
Hongze Cheng 已提交
976 977 978 979 980 981 982 983 984 985 986
    }

    // 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 已提交
987

H
Hongze Cheng 已提交
988
      if (brinBlk->minTbid.uid != writer->ctx->tbid->uid) {
H
Hongze Cheng 已提交
989
        writer->ctx->tbHasOldData = false;
H
Hongze Cheng 已提交
990
        goto _exit;
H
Hongze Cheng 已提交
991 992
      }

H
Hongze Cheng 已提交
993 994
      code = tsdbDataFileReadBrinBlock(writer->ctx->reader, brinBlk, writer->ctx->brinBlock);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
995

H
Hongze Cheng 已提交
996 997 998 999 1000
      writer->ctx->brinBlockIdx = 0;
      writer->ctx->brinBlkArrayIdx++;
      break;
    }
  }
H
Hongze Cheng 已提交
1001

H
Hongze Cheng 已提交
1002 1003 1004 1005 1006 1007
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}
H
Hongze Cheng 已提交
1008

H
Hongze Cheng 已提交
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
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 已提交
1021
    }
H
Hongze Cheng 已提交
1022 1023 1024

    code = tsdbDataFileDoWriteTableOldData(writer, key);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1025 1026 1027 1028 1029
  }

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

H
Hongze Cheng 已提交
1030 1031
_exit:
  if (code) {
H
Hongze Cheng 已提交
1032
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
1033 1034 1035
  }
  return code;
}
H
Hongze Cheng 已提交
1036

H
Hongze Cheng 已提交
1037
static int32_t tsdbDataFileWriteTableDataEnd(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
1038
  if (writer->ctx->tbid->uid == 0) return 0;
H
Hongze Cheng 已提交
1039 1040 1041 1042 1043

  int32_t code = 0;
  int32_t lino = 0;

  if (writer->ctx->tbHasOldData) {
H
Hongze Cheng 已提交
1044 1045 1046 1047 1048 1049 1050 1051 1052
    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 已提交
1053
  }
H
Hongze Cheng 已提交
1054

H
Hongze Cheng 已提交
1055
  code = tsdbDataFileDoWriteBlockData(writer, writer->blockData);
H
Hongze Cheng 已提交
1056 1057 1058 1059
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
H
Hongze Cheng 已提交
1060
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
1061 1062 1063
  }
  return code;
}
H
Hongze Cheng 已提交
1064

H
Hongze Cheng 已提交
1065
static int32_t tsdbDataFileWriteTableDataBegin(SDataFileWriter *writer, const TABLEID *tbid) {
H
Hongze Cheng 已提交
1066 1067
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1068

H
Hongze Cheng 已提交
1069 1070 1071
  ASSERT(writer->ctx->blockDataIdx == writer->ctx->blockData->nRow);
  ASSERT(writer->blockData->nRow == 0);

H
Hongze Cheng 已提交
1072 1073 1074
  SMetaInfo info;
  bool      drop = false;
  TABLEID   tbid1[1];
H
Hongze Cheng 已提交
1075
  writer->ctx->tbHasOldData = false;
H
Hongze Cheng 已提交
1076 1077
  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 已提交
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
      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 已提交
1100
        }
H
Hongze Cheng 已提交
1101

H
Hongze Cheng 已提交
1102 1103
        SBrinRecord record[1];
        tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, record);
H
Hongze Cheng 已提交
1104

H
Hongze Cheng 已提交
1105 1106 1107 1108
        code = tsdbDataFileWriteBrinRecord(writer, record);
        TSDB_CHECK_CODE(code, lino, _exit);
      }
    }
H
Hongze Cheng 已提交
1109

H
Hongze Cheng 已提交
1110 1111 1112 1113
    if (writer->ctx->brinBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->brinBlkArray)) {
      writer->ctx->brinBlkArray = NULL;
      break;
    }
H
Hongze Cheng 已提交
1114

H
Hongze Cheng 已提交
1115 1116
    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 已提交
1117

H
Hongze Cheng 已提交
1118 1119
      code = tsdbDataFileReadBrinBlock(writer->ctx->reader, brinBlk, writer->ctx->brinBlock);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1120

H
Hongze Cheng 已提交
1121 1122 1123
      writer->ctx->brinBlockIdx = 0;
      writer->ctx->brinBlkArrayIdx++;
      break;
H
Hongze Cheng 已提交
1124 1125
    }
  }
H
Hongze Cheng 已提交
1126

H
Hongze Cheng 已提交
1127 1128
_begin:
  writer->ctx->tbid[0] = *tbid;
H
Hongze Cheng 已提交
1129

H
Hongze Cheng 已提交
1130
  if (tbid->uid == INT64_MAX) goto _exit;
H
Hongze Cheng 已提交
1131 1132 1133 1134

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

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

H
Hongze Cheng 已提交
1138 1139
_exit:
  if (code) {
H
Hongze Cheng 已提交
1140
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
1141 1142 1143
  }
  return code;
}
H
Hongze Cheng 已提交
1144

H
Hongze Cheng 已提交
1145 1146 1147 1148 1149 1150 1151
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 已提交
1152
static int32_t tsdbDataFileWriteHeadFooter(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
1153 1154 1155
  int32_t code = 0;
  int32_t lino = 0;

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

H
Hongze Cheng 已提交
1159 1160 1161 1162 1163 1164
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}
H
Hongze Cheng 已提交
1165

H
Hongze Cheng 已提交
1166
static int32_t tsdbDataFileDoWriteTombBlock(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
1167
  if (TOMB_BLOCK_SIZE(writer->tombBlock) == 0) return 0;
H
Hongze Cheng 已提交
1168 1169 1170 1171

  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1172 1173
  code = tsdbFileWriteTombBlock(writer->fd[TSDB_FTYPE_TOMB], writer->tombBlock, writer->config->cmprAlg,
                                &writer->files[TSDB_FTYPE_TOMB].size, writer->tombBlkArray, writer->config->bufArr);
H
Hongze Cheng 已提交
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
  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 已提交
1184
  ASSERT(TARRAY2_SIZE(writer->tombBlkArray) > 0);
H
Hongze Cheng 已提交
1185

H
Hongze Cheng 已提交
1186 1187 1188
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1189 1190
  code = tsdbFileWriteTombBlk(writer->fd[TSDB_FTYPE_TOMB], writer->tombBlkArray, writer->tombFooter->tombBlkPtr,
                              &writer->files[TSDB_FTYPE_TOMB].size);
H
Hongze Cheng 已提交
1191
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1192 1193 1194 1195 1196 1197 1198 1199

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

H
Hongze Cheng 已提交
1200 1201 1202 1203 1204 1205 1206
int32_t tsdbFileWriteTombFooter(STsdbFD *fd, const STombFooter *footer, int64_t *fileSize) {
  int32_t code = tsdbWriteFile(fd, *fileSize, (const uint8_t *)footer, sizeof(*footer));
  if (code) return code;
  *fileSize += sizeof(*footer);
  return 0;
}

H
Hongze Cheng 已提交
1207
static int32_t tsdbDataFileWriteTombFooter(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
1208 1209
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1210

H
Hongze Cheng 已提交
1211
  code = tsdbFileWriteTombFooter(writer->fd[TSDB_FTYPE_TOMB], writer->tombFooter, &writer->files[TSDB_FTYPE_TOMB].size);
H
Hongze Cheng 已提交
1212
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1213

H
Hongze Cheng 已提交
1214 1215 1216 1217 1218 1219
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}
H
Hongze Cheng 已提交
1220

H
Hongze Cheng 已提交
1221 1222 1223
static int32_t tsdbDataFileDoWriteTombRecord(SDataFileWriter *writer, const STombRecord *record) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1224

H
Hongze Cheng 已提交
1225
  while (writer->ctx->hasOldTomb) {
H
Hongze Cheng 已提交
1226
    for (; writer->ctx->tombBlockIdx < TOMB_BLOCK_SIZE(writer->ctx->tombBlock); writer->ctx->tombBlockIdx++) {
H
Hongze Cheng 已提交
1227 1228
      STombRecord record1[1];
      tTombBlockGet(writer->ctx->tombBlock, writer->ctx->tombBlockIdx, record1);
H
Hongze Cheng 已提交
1229

H
Hongze Cheng 已提交
1230
      int32_t c = tTombRecordCompare(record, record1);
H
Hongze Cheng 已提交
1231 1232 1233
      if (c < 0) {
        break;
      } else if (c > 0) {
H
Hongze Cheng 已提交
1234
        code = tTombBlockPut(writer->tombBlock, record1);
H
Hongze Cheng 已提交
1235 1236
        TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1237
        if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
H
Hongze Cheng 已提交
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
          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 已提交
1250

H
Hongze Cheng 已提交
1251 1252 1253
    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 已提交
1254
      code = tsdbDataFileReadTombBlock(writer->ctx->reader, tombBlk, writer->ctx->tombBlock);
H
Hongze Cheng 已提交
1255 1256
      TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1257
      writer->ctx->tombBlockIdx = 0;
H
Hongze Cheng 已提交
1258 1259 1260 1261 1262 1263 1264 1265
      writer->ctx->tombBlkArrayIdx++;
      break;
    }
  }

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

H
Hongze Cheng 已提交
1266
  code = tTombBlockPut(writer->tombBlock, record);
H
Hongze Cheng 已提交
1267 1268
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1269
  if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
H
Hongze Cheng 已提交
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
    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 已提交
1281 1282 1283 1284 1285 1286 1287
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 已提交
1288

H
Hongze Cheng 已提交
1289 1290 1291 1292 1293
  *fileSize += ptr->size;
  return 0;
}

static int32_t tsdbDataFileWriteBrinBlk(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
1294 1295 1296
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1297 1298
  code = tsdbFileWriteBrinBlk(writer->fd[TSDB_FTYPE_HEAD], writer->brinBlkArray, writer->headFooter->brinBlkPtr,
                              &writer->files[TSDB_FTYPE_HEAD].size);
H
Hongze Cheng 已提交
1299 1300 1301 1302 1303 1304 1305 1306 1307
  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 已提交
1308 1309 1310 1311
static int32_t tsdbDataFileWriterCloseCommit(SDataFileWriter *writer, TFileOpArray *opArr) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1312
  int32_t  ftype;
H
Hongze Cheng 已提交
1313
  STFileOp op;
H
Hongze Cheng 已提交
1314

H
Hongze Cheng 已提交
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
  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 已提交
1327 1328 1329
    code = tsdbDataFileWriteBrinBlock(writer);
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1330
    code = tsdbDataFileWriteBrinBlk(writer);
H
Hongze Cheng 已提交
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
    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 已提交
1347
    op = (STFileOp){
H
Hongze Cheng 已提交
1348
        .optype = TSDB_FOP_CREATE,
H
Hongze Cheng 已提交
1349
        .fid = writer->config->fid,
H
Hongze Cheng 已提交
1350
        .nf = writer->files[ftype],
H
Hongze Cheng 已提交
1351 1352 1353
    };
    code = TARRAY2_APPEND(opArr, op);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1354

H
Hongze Cheng 已提交
1355 1356
    // .data
    ftype = TSDB_FTYPE_DATA;
H
Hongze Cheng 已提交
1357
    if (!writer->config->files[ftype].exist) {
H
Hongze Cheng 已提交
1358
      op = (STFileOp){
H
Hongze Cheng 已提交
1359 1360 1361 1362
          .optype = TSDB_FOP_CREATE,
          .fid = writer->config->fid,
          .nf = writer->files[ftype],
      };
H
Hongze Cheng 已提交
1363 1364 1365 1366
      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 已提交
1367 1368 1369 1370 1371
          .optype = TSDB_FOP_MODIFY,
          .fid = writer->config->fid,
          .of = writer->config->files[ftype].file,
          .nf = writer->files[ftype],
      };
H
Hongze Cheng 已提交
1372 1373
      code = TARRAY2_APPEND(opArr, op);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1374
    }
H
Hongze Cheng 已提交
1375

H
Hongze Cheng 已提交
1376 1377
    // .sma
    ftype = TSDB_FTYPE_SMA;
H
Hongze Cheng 已提交
1378
    if (!writer->config->files[ftype].exist) {
H
Hongze Cheng 已提交
1379
      op = (STFileOp){
H
Hongze Cheng 已提交
1380 1381 1382 1383
          .optype = TSDB_FOP_CREATE,
          .fid = writer->config->fid,
          .nf = writer->files[ftype],
      };
H
Hongze Cheng 已提交
1384 1385 1386 1387
      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 已提交
1388 1389 1390 1391 1392
          .optype = TSDB_FOP_MODIFY,
          .fid = writer->config->fid,
          .of = writer->config->files[ftype].file,
          .nf = writer->files[ftype],
      };
H
Hongze Cheng 已提交
1393 1394
      code = TARRAY2_APPEND(opArr, op);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1395 1396
    }
  }
H
Hongze Cheng 已提交
1397

H
Hongze Cheng 已提交
1398 1399 1400 1401 1402 1403
  if (writer->fd[TSDB_FTYPE_TOMB]) {
    STombRecord record[1] = {{
        .suid = INT64_MAX,
        .uid = INT64_MAX,
        .version = INT64_MAX,
    }};
H
Hongze Cheng 已提交
1404

H
Hongze Cheng 已提交
1405 1406 1407 1408
    code = tsdbDataFileDoWriteTombRecord(writer, record);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = tsdbDataFileDoWriteTombBlock(writer);
H
Hongze Cheng 已提交
1409
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1410 1411 1412 1413 1414 1415 1416

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

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

H
fix bug  
Hongze Cheng 已提交
1417
    ftype = TSDB_FTYPE_TOMB;
H
Hongze Cheng 已提交
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
    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 已提交
1442
  }
H
Hongze Cheng 已提交
1443 1444 1445

_exit:
  if (code) {
H
Hongze Cheng 已提交
1446
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
1447 1448 1449
  }
  return code;
}
H
Hongze Cheng 已提交
1450

H
Hongze Cheng 已提交
1451 1452 1453 1454
static int32_t tsdbDataFileWriterOpenDataFD(SDataFileWriter *writer) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1455 1456 1457 1458 1459
  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 已提交
1460 1461 1462
    char    fname[TSDB_FILENAME_LEN];
    int32_t flag = TD_FILE_READ | TD_FILE_WRITE;

H
Hongze Cheng 已提交
1463
    if (writer->files[ftype].size == 0) {
H
Hongze Cheng 已提交
1464 1465 1466
      flag |= (TD_FILE_CREATE | TD_FILE_TRUNC);
    }

H
Hongze Cheng 已提交
1467
    tsdbTFileName(writer->config->tsdb, &writer->files[ftype], fname);
H
Hongze Cheng 已提交
1468
    code = tsdbOpenFile(fname, writer->config->szPage, flag, &writer->fd[ftype]);
H
Hongze Cheng 已提交
1469 1470
    TSDB_CHECK_CODE(code, lino, _exit);

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

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

H
Hongze Cheng 已提交
1477
      writer->files[ftype].size += TSDB_FHDR_SIZE;
H
Hongze Cheng 已提交
1478 1479 1480
    }
  }

H
Hongze Cheng 已提交
1481
  if (writer->ctx->reader) {
H
Hongze Cheng 已提交
1482 1483
    code = tsdbDataFileReadBrinBlk(writer->ctx->reader, &writer->ctx->brinBlkArray);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1484 1485
  }

H
Hongze Cheng 已提交
1486 1487 1488 1489 1490 1491 1492
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}

H
Hongze Cheng 已提交
1493
int32_t tsdbDataFileWriterOpen(const SDataFileWriterConfig *config, SDataFileWriter **writer) {
H
Hongze Cheng 已提交
1494
  writer[0] = taosMemoryCalloc(1, sizeof(*writer[0]));
H
Hongze Cheng 已提交
1495 1496 1497 1498 1499 1500 1501
  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) {
1502 1503
  if (writer[0] == NULL) return 0;

H
Hongze Cheng 已提交
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526
  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 已提交
1527
int32_t tsdbDataFileWriteRow(SDataFileWriter *writer, SRowInfo *row) {
H
Hongze Cheng 已提交
1528 1529
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1530 1531 1532 1533 1534 1535

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

H
Hongze Cheng 已提交
1536
  if (writer->fd[TSDB_FTYPE_HEAD] == NULL) {
H
Hongze Cheng 已提交
1537 1538 1539 1540
    code = tsdbDataFileWriterOpenDataFD(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
1541
  if (row->uid != writer->ctx->tbid->uid) {
H
Hongze Cheng 已提交
1542 1543 1544
    code = tsdbDataFileWriteTableDataEnd(writer);
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1545
    code = tsdbDataFileWriteTableDataBegin(writer, (TABLEID *)row);
H
Hongze Cheng 已提交
1546 1547 1548
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
1549
  code = tsdbDataFileDoWriteTSData(writer, &row->row);
H
Hongze Cheng 已提交
1550 1551
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1552 1553
_exit:
  if (code) {
H
Hongze Cheng 已提交
1554
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
1555 1556
  }
  return code;
H
Hongze Cheng 已提交
1557
}
H
Hongze Cheng 已提交
1558

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

H
Hongze Cheng 已提交
1562 1563 1564
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1565
  ASSERT(bData->uid);
H
Hongze Cheng 已提交
1566

H
Hongze Cheng 已提交
1567 1568 1569 1570
  if (!writer->ctx->opened) {
    code = tsdbDataFileWriterDoOpen(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
1571

H
Hongze Cheng 已提交
1572
  if (writer->fd[TSDB_FTYPE_DATA] == NULL) {
H
Hongze Cheng 已提交
1573 1574 1575
    code = tsdbDataFileWriterOpenDataFD(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
1576

H
Hongze Cheng 已提交
1577 1578 1579
  if (bData->uid != writer->ctx->tbid->uid) {
    code = tsdbDataFileWriteTableDataEnd(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1580

H
Hongze Cheng 已提交
1581 1582 1583 1584
    code = tsdbDataFileWriteTableDataBegin(writer, (TABLEID *)bData);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
  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 已提交
1595 1596
  if (!writer->ctx->tbHasOldData       //
      && writer->blockData->nRow == 0  //
H
Hongze Cheng 已提交
1597
  ) {
H
Hongze Cheng 已提交
1598
    code = tsdbDataFileDoWriteBlockData(writer, bData);
H
Hongze Cheng 已提交
1599 1600 1601 1602 1603 1604 1605 1606
    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 已提交
1607 1608 1609 1610 1611 1612

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

H
Hongze Cheng 已提交
1615
int32_t tsdbDataFileFlush(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
1616 1617
  ASSERT(writer->ctx->opened);

H
Hongze Cheng 已提交
1618
  if (writer->blockData->nRow == 0) return 0;
H
Hongze Cheng 已提交
1619 1620
  if (writer->ctx->tbHasOldData) return 0;

H
Hongze Cheng 已提交
1621
  return tsdbDataFileDoWriteBlockData(writer, writer->blockData);
H
Hongze Cheng 已提交
1622
}
H
Hongze Cheng 已提交
1623

H
Hongze Cheng 已提交
1624 1625 1626 1627 1628
static int32_t tsdbDataFileWriterOpenTombFD(SDataFileWriter *writer) {
  int32_t code = 0;
  int32_t lino = 0;

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

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

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

H
Hongze Cheng 已提交
1635 1636 1637
  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 已提交
1638

H
Hongze Cheng 已提交
1639 1640 1641 1642
  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 已提交
1643

H
Hongze Cheng 已提交
1644 1645 1646
  if (writer->ctx->reader) {
    code = tsdbDataFileReadTombBlk(writer->ctx->reader, &writer->ctx->tombBlkArray);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1647

H
Hongze Cheng 已提交
1648 1649
    if (TARRAY2_SIZE(writer->ctx->tombBlkArray) > 0) {
      writer->ctx->hasOldTomb = true;
H
Hongze Cheng 已提交
1650 1651
    }

H
Hongze Cheng 已提交
1652
    writer->ctx->tombBlkArrayIdx = 0;
H
Hongze Cheng 已提交
1653 1654
    tTombBlockClear(writer->ctx->tombBlock);
    writer->ctx->tombBlockIdx = 0;
H
Hongze Cheng 已提交
1655
  }
H
Hongze Cheng 已提交
1656 1657 1658 1659 1660 1661 1662 1663

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

H
Hongze Cheng 已提交
1664 1665 1666 1667 1668 1669 1670 1671 1672
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 已提交
1673
  if (writer->fd[TSDB_FTYPE_TOMB] == NULL) {
H
Hongze Cheng 已提交
1674 1675
    code = tsdbDataFileWriterOpenTombFD(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1676 1677
  }

H
Hongze Cheng 已提交
1678
  code = tsdbDataFileDoWriteTombRecord(writer, record);
H
Hongze Cheng 已提交
1679 1680 1681 1682 1683 1684 1685 1686
  TSDB_CHECK_CODE(code, lino, _exit);

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