tsdbDataFileRW.c 49.8 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 706
  }

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

H
Hongze Cheng 已提交
710 711 712 713 714 715 716 717 718 719 720 721 722 723
  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 已提交
724 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

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

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

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

H
Hongze Cheng 已提交
759 760 761 762 763 764 765
  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 已提交
766 767
      .minVer = bData->aVersion[0],
      .maxVer = bData->aVersion[0],
H
Hongze Cheng 已提交
768 769 770 771 772 773 774
      .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 已提交
775 776 777
  }};

  for (int32_t i = 1; i < bData->nRow; ++i) {
H
Hongze Cheng 已提交
778 779 780 781 782 783 784 785
    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 已提交
786 787
    }
  }
H
Hongze Cheng 已提交
788

H
Hongze Cheng 已提交
789
  // to .data file
H
Hongze Cheng 已提交
790 791 792 793
  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 已提交
794

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

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

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
843 844 845 846 847 848
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 已提交
849 850 851 852 853 854 855
  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 已提交
856

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
916
  for (;;) {
H
Hongze Cheng 已提交
917
    for (;;) {
H
Hongze Cheng 已提交
918 919 920 921 922 923 924
      // 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 已提交
925 926
          TSDBROW row = tsdbRowFromBlockData(writer->ctx->blockData, writer->ctx->blockDataIdx);
          code = tsdbDataFileDoWriteTSRow(writer, &row);
H
Hongze Cheng 已提交
927
          TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
928
        }
H
Hongze Cheng 已提交
929 930 931 932 933 934
      }

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

H
Hongze Cheng 已提交
936 937 938 939
      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 已提交
940
        }
H
Hongze Cheng 已提交
941

H
Hongze Cheng 已提交
942 943 944 945 946 947 948
        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 已提交
949
          if (key->ts > record->lastKey || (key->ts == record->lastKey && key->version > record->lastKeyVer)) {
H
Hongze Cheng 已提交
950 951 952 953
            if (writer->blockData->nRow > 0) {
              code = tsdbDataFileDoWriteBlockData(writer, writer->blockData);
              TSDB_CHECK_CODE(code, lino, _exit);
            }
H
Hongze Cheng 已提交
954

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
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 已提交
1012
    }
H
Hongze Cheng 已提交
1013 1014 1015

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

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

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

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

  int32_t code = 0;
  int32_t lino = 0;

  if (writer->ctx->tbHasOldData) {
H
Hongze Cheng 已提交
1035 1036 1037 1038 1039 1040 1041 1042 1043
    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 已提交
1044
  }
H
Hongze Cheng 已提交
1045

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

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

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

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

H
Hongze Cheng 已提交
1063 1064 1065
  SMetaInfo info;
  bool      drop = false;
  TABLEID   tbid1[1];
H
Hongze Cheng 已提交
1066
  writer->ctx->tbHasOldData = false;
H
Hongze Cheng 已提交
1067 1068
  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 已提交
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
      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 已提交
1091
        }
H
Hongze Cheng 已提交
1092

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

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

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

H
Hongze Cheng 已提交
1106 1107
    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 已提交
1108

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
1136 1137 1138 1139 1140 1141 1142
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 已提交
1143
static int32_t tsdbDataFileWriteHeadFooter(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
1144 1145 1146
  int32_t code = 0;
  int32_t lino = 0;

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

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

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

  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1163 1164
  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 已提交
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
  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 已提交
1175
  ASSERT(TARRAY2_SIZE(writer->tombBlkArray) > 0);
H
Hongze Cheng 已提交
1176

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

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

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

H
Hongze Cheng 已提交
1191 1192 1193 1194 1195 1196 1197
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 已提交
1198
static int32_t tsdbDataFileWriteTombFooter(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
1199 1200
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1201

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

H
Hongze Cheng 已提交
1205 1206 1207 1208 1209 1210
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}
H
Hongze Cheng 已提交
1211

H
Hongze Cheng 已提交
1212 1213 1214
static int32_t tsdbDataFileDoWriteTombRecord(SDataFileWriter *writer, const STombRecord *record) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1215

H
Hongze Cheng 已提交
1216
  while (writer->ctx->hasOldTomb) {
H
Hongze Cheng 已提交
1217
    for (; writer->ctx->tombBlockIdx < TOMB_BLOCK_SIZE(writer->ctx->tombBlock); writer->ctx->tombBlockIdx++) {
H
Hongze Cheng 已提交
1218 1219
      STombRecord record1[1];
      tTombBlockGet(writer->ctx->tombBlock, writer->ctx->tombBlockIdx, record1);
H
Hongze Cheng 已提交
1220

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

H
Hongze Cheng 已提交
1228
        if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
H
Hongze Cheng 已提交
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
          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 已提交
1241

H
Hongze Cheng 已提交
1242 1243 1244
    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 已提交
1245
      code = tsdbDataFileReadTombBlock(writer->ctx->reader, tombBlk, writer->ctx->tombBlock);
H
Hongze Cheng 已提交
1246 1247
      TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1248
      writer->ctx->tombBlockIdx = 0;
H
Hongze Cheng 已提交
1249 1250 1251 1252 1253 1254 1255 1256
      writer->ctx->tombBlkArrayIdx++;
      break;
    }
  }

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

H
Hongze Cheng 已提交
1257
  code = tTombBlockPut(writer->tombBlock, record);
H
Hongze Cheng 已提交
1258 1259
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1260
  if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
H
Hongze Cheng 已提交
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
    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 已提交
1272 1273 1274 1275 1276 1277 1278
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 已提交
1279

H
Hongze Cheng 已提交
1280 1281 1282 1283 1284
  *fileSize += ptr->size;
  return 0;
}

static int32_t tsdbDataFileWriteBrinBlk(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
1285 1286 1287
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1288 1289
  code = tsdbFileWriteBrinBlk(writer->fd[TSDB_FTYPE_HEAD], writer->brinBlkArray, writer->headFooter->brinBlkPtr,
                              &writer->files[TSDB_FTYPE_HEAD].size);
H
Hongze Cheng 已提交
1290 1291 1292 1293 1294 1295 1296 1297 1298
  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 已提交
1299 1300 1301 1302
static int32_t tsdbDataFileWriterCloseCommit(SDataFileWriter *writer, TFileOpArray *opArr) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1303
  int32_t  ftype;
H
Hongze Cheng 已提交
1304
  STFileOp op;
H
Hongze Cheng 已提交
1305

H
Hongze Cheng 已提交
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
  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 已提交
1318 1319 1320
    code = tsdbDataFileWriteBrinBlock(writer);
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1321
    code = tsdbDataFileWriteBrinBlk(writer);
H
Hongze Cheng 已提交
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
    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 已提交
1338
    op = (STFileOp){
H
Hongze Cheng 已提交
1339
        .optype = TSDB_FOP_CREATE,
H
Hongze Cheng 已提交
1340
        .fid = writer->config->fid,
H
Hongze Cheng 已提交
1341
        .nf = writer->files[ftype],
H
Hongze Cheng 已提交
1342 1343 1344
    };
    code = TARRAY2_APPEND(opArr, op);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1345

H
Hongze Cheng 已提交
1346 1347
    // .data
    ftype = TSDB_FTYPE_DATA;
H
Hongze Cheng 已提交
1348
    if (!writer->config->files[ftype].exist) {
H
Hongze Cheng 已提交
1349
      op = (STFileOp){
H
Hongze Cheng 已提交
1350 1351 1352 1353
          .optype = TSDB_FOP_CREATE,
          .fid = writer->config->fid,
          .nf = writer->files[ftype],
      };
H
Hongze Cheng 已提交
1354 1355 1356 1357
      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 已提交
1358 1359 1360 1361 1362
          .optype = TSDB_FOP_MODIFY,
          .fid = writer->config->fid,
          .of = writer->config->files[ftype].file,
          .nf = writer->files[ftype],
      };
H
Hongze Cheng 已提交
1363 1364
      code = TARRAY2_APPEND(opArr, op);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1365
    }
H
Hongze Cheng 已提交
1366

H
Hongze Cheng 已提交
1367 1368
    // .sma
    ftype = TSDB_FTYPE_SMA;
H
Hongze Cheng 已提交
1369
    if (!writer->config->files[ftype].exist) {
H
Hongze Cheng 已提交
1370
      op = (STFileOp){
H
Hongze Cheng 已提交
1371 1372 1373 1374
          .optype = TSDB_FOP_CREATE,
          .fid = writer->config->fid,
          .nf = writer->files[ftype],
      };
H
Hongze Cheng 已提交
1375 1376 1377 1378
      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 已提交
1379 1380 1381 1382 1383
          .optype = TSDB_FOP_MODIFY,
          .fid = writer->config->fid,
          .of = writer->config->files[ftype].file,
          .nf = writer->files[ftype],
      };
H
Hongze Cheng 已提交
1384 1385
      code = TARRAY2_APPEND(opArr, op);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1386 1387
    }
  }
H
Hongze Cheng 已提交
1388

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

H
Hongze Cheng 已提交
1396 1397 1398 1399
    code = tsdbDataFileDoWriteTombRecord(writer, record);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = tsdbDataFileDoWriteTombBlock(writer);
H
Hongze Cheng 已提交
1400
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
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 1429 1430 1431 1432

    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 已提交
1433
  }
H
Hongze Cheng 已提交
1434 1435 1436

_exit:
  if (code) {
H
Hongze Cheng 已提交
1437
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
1438 1439 1440
  }
  return code;
}
H
Hongze Cheng 已提交
1441

H
Hongze Cheng 已提交
1442 1443 1444 1445
static int32_t tsdbDataFileWriterOpenDataFD(SDataFileWriter *writer) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1446 1447 1448 1449 1450
  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 已提交
1451 1452 1453
    char    fname[TSDB_FILENAME_LEN];
    int32_t flag = TD_FILE_READ | TD_FILE_WRITE;

H
Hongze Cheng 已提交
1454
    if (writer->files[ftype].size == 0) {
H
Hongze Cheng 已提交
1455 1456 1457
      flag |= (TD_FILE_CREATE | TD_FILE_TRUNC);
    }

H
Hongze Cheng 已提交
1458
    tsdbTFileName(writer->config->tsdb, &writer->files[ftype], fname);
H
Hongze Cheng 已提交
1459
    code = tsdbOpenFile(fname, writer->config->szPage, flag, &writer->fd[ftype]);
H
Hongze Cheng 已提交
1460 1461
    TSDB_CHECK_CODE(code, lino, _exit);

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

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

H
Hongze Cheng 已提交
1468
      writer->files[ftype].size += TSDB_FHDR_SIZE;
H
Hongze Cheng 已提交
1469 1470 1471
    }
  }

H
Hongze Cheng 已提交
1472
  if (writer->ctx->reader) {
H
Hongze Cheng 已提交
1473 1474
    code = tsdbDataFileReadBrinBlk(writer->ctx->reader, &writer->ctx->brinBlkArray);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1475 1476
  }

H
Hongze Cheng 已提交
1477 1478 1479 1480 1481 1482 1483
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}

H
Hongze Cheng 已提交
1484
int32_t tsdbDataFileWriterOpen(const SDataFileWriterConfig *config, SDataFileWriter **writer) {
H
Hongze Cheng 已提交
1485
  writer[0] = taosMemoryCalloc(1, sizeof(*writer[0]));
H
Hongze Cheng 已提交
1486 1487 1488 1489 1490 1491 1492
  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) {
1493 1494
  if (writer[0] == NULL) return 0;

H
Hongze Cheng 已提交
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
  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 已提交
1518
int32_t tsdbDataFileWriteRow(SDataFileWriter *writer, SRowInfo *row) {
H
Hongze Cheng 已提交
1519 1520
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1521 1522 1523 1524 1525 1526

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

H
Hongze Cheng 已提交
1527
  if (writer->fd[TSDB_FTYPE_HEAD] == NULL) {
H
Hongze Cheng 已提交
1528 1529 1530 1531
    code = tsdbDataFileWriterOpenDataFD(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
1532
  if (row->uid != writer->ctx->tbid->uid) {
H
Hongze Cheng 已提交
1533 1534 1535
    code = tsdbDataFileWriteTableDataEnd(writer);
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1536
    code = tsdbDataFileWriteTableDataBegin(writer, (TABLEID *)row);
H
Hongze Cheng 已提交
1537 1538 1539
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
1540
  code = tsdbDataFileDoWriteTSData(writer, &row->row);
H
Hongze Cheng 已提交
1541 1542
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1543 1544
_exit:
  if (code) {
H
Hongze Cheng 已提交
1545
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
1546 1547
  }
  return code;
H
Hongze Cheng 已提交
1548
}
H
Hongze Cheng 已提交
1549

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

H
Hongze Cheng 已提交
1553 1554 1555
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1556
  ASSERT(bData->uid);
H
Hongze Cheng 已提交
1557

H
Hongze Cheng 已提交
1558 1559 1560 1561
  if (!writer->ctx->opened) {
    code = tsdbDataFileWriterDoOpen(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
1562

H
Hongze Cheng 已提交
1563
  if (writer->fd[TSDB_FTYPE_DATA] == NULL) {
H
Hongze Cheng 已提交
1564 1565 1566
    code = tsdbDataFileWriterOpenDataFD(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
1567

H
Hongze Cheng 已提交
1568 1569 1570
  if (bData->uid != writer->ctx->tbid->uid) {
    code = tsdbDataFileWriteTableDataEnd(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1571

H
Hongze Cheng 已提交
1572 1573 1574 1575
    code = tsdbDataFileWriteTableDataBegin(writer, (TABLEID *)bData);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
  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 已提交
1586 1587
  if (!writer->ctx->tbHasOldData       //
      && writer->blockData->nRow == 0  //
H
Hongze Cheng 已提交
1588
  ) {
H
Hongze Cheng 已提交
1589
    code = tsdbDataFileDoWriteBlockData(writer, bData);
H
Hongze Cheng 已提交
1590 1591 1592 1593 1594 1595 1596 1597
    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 已提交
1598 1599 1600 1601 1602 1603

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

H
Hongze Cheng 已提交
1606
int32_t tsdbDataFileFlush(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
1607 1608
  ASSERT(writer->ctx->opened);

H
Hongze Cheng 已提交
1609
  if (writer->blockData->nRow == 0) return 0;
H
Hongze Cheng 已提交
1610 1611
  if (writer->ctx->tbHasOldData) return 0;

H
Hongze Cheng 已提交
1612
  return tsdbDataFileDoWriteBlockData(writer, writer->blockData);
H
Hongze Cheng 已提交
1613
}
H
Hongze Cheng 已提交
1614

H
Hongze Cheng 已提交
1615 1616 1617 1618 1619
static int32_t tsdbDataFileWriterOpenTombFD(SDataFileWriter *writer) {
  int32_t code = 0;
  int32_t lino = 0;

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

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

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

H
Hongze Cheng 已提交
1626 1627 1628
  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 已提交
1629

H
Hongze Cheng 已提交
1630 1631 1632 1633
  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 已提交
1634

H
Hongze Cheng 已提交
1635 1636 1637
  if (writer->ctx->reader) {
    code = tsdbDataFileReadTombBlk(writer->ctx->reader, &writer->ctx->tombBlkArray);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1638

H
Hongze Cheng 已提交
1639 1640
    if (TARRAY2_SIZE(writer->ctx->tombBlkArray) > 0) {
      writer->ctx->hasOldTomb = true;
H
Hongze Cheng 已提交
1641 1642
    }

H
Hongze Cheng 已提交
1643
    writer->ctx->tombBlkArrayIdx = 0;
H
Hongze Cheng 已提交
1644 1645
    tTombBlockClear(writer->ctx->tombBlock);
    writer->ctx->tombBlockIdx = 0;
H
Hongze Cheng 已提交
1646
  }
H
Hongze Cheng 已提交
1647 1648 1649 1650 1651 1652 1653 1654

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

H
Hongze Cheng 已提交
1655 1656 1657 1658 1659 1660 1661 1662 1663
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 已提交
1664
  if (writer->fd[TSDB_FTYPE_TOMB] == NULL) {
H
Hongze Cheng 已提交
1665 1666
    code = tsdbDataFileWriterOpenTombFD(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1667 1668
  }

H
Hongze Cheng 已提交
1669
  code = tsdbDataFileDoWriteTombRecord(writer, record);
H
Hongze Cheng 已提交
1670 1671 1672 1673 1674 1675 1676 1677
  TSDB_CHECK_CODE(code, lino, _exit);

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