tsdbDataFileRW.c 51.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
typedef struct {
H
Hongze Cheng 已提交
19
  SFDataPtr brinBlkPtr[1];
H
Hongze Cheng 已提交
20
  SFDataPtr rsrvd[2];
H
Hongze Cheng 已提交
21 22 23 24 25 26
} SHeadFooter;

typedef struct {
  SFDataPtr tombBlkPtr[1];
  SFDataPtr rsrvd[2];
} STombFooter;
H
Hongze Cheng 已提交
27

H
Hongze Cheng 已提交
28 29
// SDataFileReader =============================================
struct SDataFileReader {
H
Hongze Cheng 已提交
30
  SDataFileReaderConfig config[1];
H
Hongze Cheng 已提交
31

H
Hongze Cheng 已提交
32 33
  uint8_t *bufArr[5];

H
Hongze Cheng 已提交
34
  struct {
H
Hongze Cheng 已提交
35 36 37 38
    bool headFooterLoaded;
    bool tombFooterLoaded;
    bool brinBlkLoaded;
    bool tombBlkLoaded;
H
Hongze Cheng 已提交
39 40
  } ctx[1];

H
Hongze Cheng 已提交
41 42
  STsdbFD *fd[TSDB_FTYPE_MAX];

H
Hongze Cheng 已提交
43 44 45 46
  SHeadFooter   headFooter[1];
  STombFooter   tombFooter[1];
  TBrinBlkArray brinBlkArray[1];
  TTombBlkArray tombBlkArray[1];
H
Hongze Cheng 已提交
47
};
H
Hongze Cheng 已提交
48

H
Hongze Cheng 已提交
49
static int32_t tsdbDataFileReadHeadFooter(SDataFileReader *reader) {
H
Hongze Cheng 已提交
50
  if (reader->ctx->headFooterLoaded) return 0;
H
Hongze Cheng 已提交
51 52 53 54

  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
55 56 57 58 59 60 61
  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 已提交
62 63 64 65 66 67 68 69 70 71
  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 已提交
72
  if (reader->ctx->tombFooterLoaded) return 0;
H
Hongze Cheng 已提交
73 74 75 76

  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
77 78 79 80 81 82
  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 已提交
83
  reader->ctx->tombFooterLoaded = true;
H
Hongze Cheng 已提交
84 85 86 87 88 89 90 91

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

H
Hongze Cheng 已提交
92 93
int32_t tsdbDataFileReaderOpen(const char *fname[], const SDataFileReaderConfig *config, SDataFileReader **reader) {
  int32_t code = 0;
H
Hongze Cheng 已提交
94
  int32_t lino = 0;
H
Hongze Cheng 已提交
95

H
Hongze Cheng 已提交
96
  reader[0] = taosMemoryCalloc(1, sizeof(**reader));
H
Hongze Cheng 已提交
97
  if (reader[0] == NULL) {
H
Hongze Cheng 已提交
98 99 100 101 102
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  reader[0]->config[0] = config[0];
H
Hongze Cheng 已提交
103
  if (reader[0]->config->bufArr == NULL) {
H
Hongze Cheng 已提交
104 105 106 107 108
    reader[0]->config->bufArr = reader[0]->bufArr;
  }

  if (fname) {
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
H
Hongze Cheng 已提交
109 110 111 112
      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 已提交
113
    }
H
Hongze Cheng 已提交
114 115
  } else {
    for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
H
Hongze Cheng 已提交
116 117 118 119 120 121
      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 已提交
122 123
    }
  }
H
Hongze Cheng 已提交
124 125 126

_exit:
  if (code) {
H
Hongze Cheng 已提交
127
    TSDB_ERROR_LOG(TD_VID(config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
128 129 130 131
  }
  return code;
}

H
Hongze Cheng 已提交
132 133 134
int32_t tsdbDataFileReaderClose(SDataFileReader **reader) {
  if (reader[0] == NULL) return 0;

H
Hongze Cheng 已提交
135 136 137
  TARRAY2_DESTROY(reader[0]->tombBlkArray, NULL);
  TARRAY2_DESTROY(reader[0]->brinBlkArray, NULL);

H
Hongze Cheng 已提交
138
#if 0
H
Hongze Cheng 已提交
139 140
  TARRAY2_DESTROY(reader[0]->dataBlkArray, NULL);
  TARRAY2_DESTROY(reader[0]->blockIdxArray, NULL);
H
Hongze Cheng 已提交
141
#endif
H
Hongze Cheng 已提交
142 143

  for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
H
Hongze Cheng 已提交
144 145 146
    if (reader[0]->fd[i]) {
      tsdbCloseFile(&reader[0]->fd[i]);
    }
H
Hongze Cheng 已提交
147 148 149 150 151
  }

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

H
Hongze Cheng 已提交
153 154
  taosMemoryFree(reader[0]);
  reader[0] = NULL;
H
Hongze Cheng 已提交
155 156 157
  return 0;
}

H
Hongze Cheng 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
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 已提交
258 259
int32_t tsdbDataFileReadBlockDataByColumn(SDataFileReader *reader, const SBrinRecord *record, SBlockData *bData,
                                          STSchema *pTSchema, int16_t cids[], int32_t ncid) {
H
Hongze Cheng 已提交
260 261 262
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
  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 已提交
361 362 363 364 365 366 367 368

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

H
Hongze Cheng 已提交
369 370
int32_t tsdbDataFileReadBlockSma(SDataFileReader *reader, const SBrinRecord *record,
                                 TColumnDataAggArray *columnDataAggArray) {
H
Hongze Cheng 已提交
371 372 373
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
  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 已提交
394 395 396 397 398 399 400 401

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

H
Hongze Cheng 已提交
402 403 404 405
int32_t tsdbDataFileReadTombBlk(SDataFileReader *reader, const TTombBlkArray **tombBlkArray) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
406
  if (!reader->ctx->tombBlkLoaded) {
H
Hongze Cheng 已提交
407 408
    code = tsdbDataFileReadTombFooter(reader);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
409

H
Hongze Cheng 已提交
410 411 412 413 414 415
    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 已提交
416

H
Hongze Cheng 已提交
417 418 419 420
      code = tsdbReadFile(reader->fd[TSDB_FTYPE_TOMB], reader->tombFooter->tombBlkPtr->offset, data,
                          reader->tombFooter->tombBlkPtr->size);
      if (code) {
        taosMemoryFree(data);
H
Hongze Cheng 已提交
421 422
        TSDB_CHECK_CODE(code, lino, _exit);
      }
H
Hongze Cheng 已提交
423 424 425 426 427

      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 已提交
428 429 430 431 432 433
    }

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

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

H
Hongze Cheng 已提交
458 459 460 461
    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 已提交
462
  }
H
Hongze Cheng 已提交
463
  ASSERT(size == tombBlk->dp->size);
H
Hongze Cheng 已提交
464

H
Hongze Cheng 已提交
465 466 467 468 469 470 471
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(reader->config->tsdb->pVnode), lino, code);
  }
  return code;
}

H
Hongze Cheng 已提交
472 473
// SDataFileWriter =============================================
struct SDataFileWriter {
H
Hongze Cheng 已提交
474
  SDataFileWriterConfig config[1];
H
Hongze Cheng 已提交
475

H
Hongze Cheng 已提交
476 477 478 479
  SSkmInfo skmTb[1];
  SSkmInfo skmRow[1];
  uint8_t *bufArr[5];

H
Hongze Cheng 已提交
480
  struct {
H
Hongze Cheng 已提交
481 482
    bool             opened;
    SDataFileReader *reader;
H
Hongze Cheng 已提交
483

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

H
Hongze Cheng 已提交
502
  STFile   files[TSDB_FTYPE_MAX];
H
Hongze Cheng 已提交
503 504
  STsdbFD *fd[TSDB_FTYPE_MAX];

H
Hongze Cheng 已提交
505 506 507 508 509 510 511 512 513
  SHeadFooter headFooter[1];
  STombFooter tombFooter[1];

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

  TTombBlkArray tombBlkArray[1];
  STombBlock    tombBlock[1];
H
Hongze Cheng 已提交
514 515
};

H
Hongze Cheng 已提交
516
static int32_t tsdbDataFileWriterCloseAbort(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
517
  ASSERT(0);
H
Hongze Cheng 已提交
518 519
  return 0;
}
H
Hongze Cheng 已提交
520

H
Hongze Cheng 已提交
521
static int32_t tsdbDataFileWriterDoClose(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
522 523 524 525
  if (writer->ctx->reader) {
    tsdbDataFileReaderClose(&writer->ctx->reader);
  }

H
Hongze Cheng 已提交
526
  tTombBlockDestroy(writer->tombBlock);
H
Hongze Cheng 已提交
527
  TARRAY2_DESTROY(writer->tombBlkArray, NULL);
H
Hongze Cheng 已提交
528 529 530 531
  tBlockDataDestroy(writer->blockData);
  tBrinBlockDestroy(writer->brinBlock);
  TARRAY2_DESTROY(writer->brinBlkArray, NULL);

H
Hongze Cheng 已提交
532
  tTombBlockDestroy(writer->ctx->tombBlock);
H
Hongze Cheng 已提交
533 534
  tBlockDataDestroy(writer->ctx->blockData);
  tBrinBlockDestroy(writer->ctx->brinBlock);
H
Hongze Cheng 已提交
535 536 537 538 539 540 541

  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 已提交
542 543
  return 0;
}
H
Hongze Cheng 已提交
544

H
Hongze Cheng 已提交
545 546 547 548 549 550 551 552 553
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 已提交
554
          .bufArr = writer->config->bufArr,
H
Hongze Cheng 已提交
555 556 557 558
      }};

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

      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 已提交
577
static int32_t tsdbDataFileWriterDoOpen(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
578 579
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
580
  int32_t ftype;
H
Hongze Cheng 已提交
581 582 583 584 585 586

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

H
Hongze Cheng 已提交
590
  // .head
H
Hongze Cheng 已提交
591 592 593
  ftype = TSDB_FTYPE_HEAD;
  writer->files[ftype] = (STFile){
      .type = ftype,
H
Hongze Cheng 已提交
594 595 596 597 598
      .did = writer->config->did,
      .fid = writer->config->fid,
      .cid = writer->config->cid,
      .size = 0,
  };
H
Hongze Cheng 已提交
599

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

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

H
Hongze Cheng 已提交
628 629 630 631
  // .tomb
  ftype = TSDB_FTYPE_TOMB;
  writer->files[ftype] = (STFile){
      .type = ftype,
H
Hongze Cheng 已提交
632 633 634 635 636
      .did = writer->config->did,
      .fid = writer->config->fid,
      .cid = writer->config->cid,
      .size = 0,
  };
H
Hongze Cheng 已提交
637

H
Hongze Cheng 已提交
638
  writer->ctx->opened = true;
H
Hongze Cheng 已提交
639

H
Hongze Cheng 已提交
640 641 642 643 644
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
H
Hongze Cheng 已提交
645
}
H
Hongze Cheng 已提交
646

H
Hongze Cheng 已提交
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
static int32_t tsdbDataFileWriteBrinBlock(SDataFileWriter *writer) {
  if (BRIN_BLOCK_SIZE(writer->brinBlock) == 0) return 0;

  int32_t code = 0;
  int32_t lino = 0;

  // get SBrinBlk
  SBrinBlk brinBlk[1] = {
      {
          .dp[0] =
              {
                  .offset = writer->files[TSDB_FTYPE_HEAD].size,
                  .size = 0,
              },
          .minTbid =
              {
                  .suid = TARRAY2_FIRST(writer->brinBlock->suid),
                  .uid = TARRAY2_FIRST(writer->brinBlock->uid),
              },
          .maxTbid =
              {
                  .suid = TARRAY2_LAST(writer->brinBlock->suid),
                  .uid = TARRAY2_LAST(writer->brinBlock->uid),
              },
          .minVer = TARRAY2_FIRST(writer->brinBlock->minVer),
          .maxVer = TARRAY2_FIRST(writer->brinBlock->minVer),
          .numRec = BRIN_BLOCK_SIZE(writer->brinBlock),
          .cmprAlg = writer->config->cmprAlg,
      },
  };

  for (int32_t i = 1; i < BRIN_BLOCK_SIZE(writer->brinBlock); i++) {
    if (brinBlk->minVer > TARRAY2_GET(writer->brinBlock->minVer, i)) {
      brinBlk->minVer = TARRAY2_GET(writer->brinBlock->minVer, i);
    }
    if (brinBlk->maxVer < TARRAY2_GET(writer->brinBlock->maxVer, i)) {
      brinBlk->maxVer = TARRAY2_GET(writer->brinBlock->maxVer, i);
    }
  }

  // write to file
  for (int32_t i = 0; i < ARRAY_SIZE(writer->brinBlock->dataArr1); i++) {
    code = tsdbCmprData((uint8_t *)TARRAY2_DATA(writer->brinBlock->dataArr1 + i),
                        TARRAY2_DATA_LEN(writer->brinBlock->dataArr1 + i), TSDB_DATA_TYPE_BIGINT, brinBlk->cmprAlg,
                        &writer->config->bufArr[0], 0, &brinBlk->size[i], &writer->config->bufArr[1]);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = tsdbWriteFile(writer->fd[TSDB_FTYPE_HEAD], writer->files[TSDB_FTYPE_HEAD].size, writer->config->bufArr[0],
                         brinBlk->size[i]);
    TSDB_CHECK_CODE(code, lino, _exit);

H
fix  
Hongze Cheng 已提交
698
    brinBlk->dp->size += brinBlk->size[i];
H
Hongze Cheng 已提交
699 700 701 702 703 704 705 706 707 708 709 710 711 712
    writer->files[TSDB_FTYPE_HEAD].size += brinBlk->size[i];
  }

  for (int32_t i = 0, j = ARRAY_SIZE(writer->brinBlock->dataArr1); i < ARRAY_SIZE(writer->brinBlock->dataArr2);
       i++, j++) {
    code = tsdbCmprData((uint8_t *)TARRAY2_DATA(writer->brinBlock->dataArr2 + i),
                        TARRAY2_DATA_LEN(writer->brinBlock->dataArr2 + i), TSDB_DATA_TYPE_INT, brinBlk->cmprAlg,
                        &writer->config->bufArr[0], 0, &brinBlk->size[j], &writer->config->bufArr[1]);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = tsdbWriteFile(writer->fd[TSDB_FTYPE_HEAD], writer->files[TSDB_FTYPE_HEAD].size, writer->config->bufArr[0],
                         brinBlk->size[j]);
    TSDB_CHECK_CODE(code, lino, _exit);

H
fix  
Hongze Cheng 已提交
713
    brinBlk->dp->size += brinBlk->size[j];
H
Hongze Cheng 已提交
714 715 716 717 718 719 720 721 722 723 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
    writer->files[TSDB_FTYPE_HEAD].size += brinBlk->size[j];
  }

  // append to brinBlkArray
  code = TARRAY2_APPEND_PTR(writer->brinBlkArray, brinBlk);
  TSDB_CHECK_CODE(code, lino, _exit);

  tBrinBlockClear(writer->brinBlock);

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

H
Hongze Cheng 已提交
752 753
  ASSERT(bData->uid);

H
Hongze Cheng 已提交
754 755 756
  int32_t code = 0;
  int32_t lino = 0;

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

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

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

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

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

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

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

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

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

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

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

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

H
fix bug  
Hongze Cheng 已提交
832
  tBlockDataClear(bData);
H
Hongze Cheng 已提交
833 834 835

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

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
911 912 913
  int32_t code = 0;
  int32_t lino = 0;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  int32_t code = 0;
  int32_t lino = 0;

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
1104 1105
    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 已提交
1106

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
1134
static int32_t tsdbDataFileWriteHeadFooter(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
1135 1136 1137
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1138 1139 1140
  int32_t ftype = TSDB_FTYPE_HEAD;
  code = tsdbWriteFile(writer->fd[ftype], writer->files[ftype].size, (const uint8_t *)writer->headFooter,
                       sizeof(SHeadFooter));
H
Hongze Cheng 已提交
1141
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1142
  writer->files[ftype].size += sizeof(SHeadFooter);
H
Hongze Cheng 已提交
1143

H
Hongze Cheng 已提交
1144 1145 1146 1147 1148 1149
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}
H
Hongze Cheng 已提交
1150

H
Hongze Cheng 已提交
1151
static int32_t tsdbDataFileDoWriteTombBlock(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
1152
  if (TOMB_BLOCK_SIZE(writer->tombBlock) == 0) return 0;
H
Hongze Cheng 已提交
1153 1154 1155 1156 1157

  int32_t code = 0;
  int32_t lino = 0;

  STombBlk tombBlk[1] = {{
H
Hongze Cheng 已提交
1158
      .numRec = TOMB_BLOCK_SIZE(writer->tombBlock),
H
Hongze Cheng 已提交
1159
      .minTbid =
H
Hongze Cheng 已提交
1160
          {
H
Hongze Cheng 已提交
1161 1162
              .suid = TARRAY2_FIRST(writer->tombBlock->suid),
              .uid = TARRAY2_FIRST(writer->tombBlock->uid),
H
Hongze Cheng 已提交
1163
          },
H
Hongze Cheng 已提交
1164
      .maxTbid =
H
Hongze Cheng 已提交
1165
          {
H
Hongze Cheng 已提交
1166 1167
              .suid = TARRAY2_LAST(writer->tombBlock->suid),
              .uid = TARRAY2_LAST(writer->tombBlock->uid),
H
Hongze Cheng 已提交
1168
          },
H
Hongze Cheng 已提交
1169 1170
      .minVer = TARRAY2_FIRST(writer->tombBlock->version),
      .maxVer = TARRAY2_FIRST(writer->tombBlock->version),
H
Hongze Cheng 已提交
1171 1172 1173 1174 1175 1176 1177
      .dp[0] =
          {
              .offset = writer->files[TSDB_FTYPE_TOMB].size,
              .size = 0,
          },
  }};

H
Hongze Cheng 已提交
1178 1179 1180
  for (int32_t i = 1; i < TOMB_BLOCK_SIZE(writer->tombBlock); i++) {
    tombBlk->minVer = TMIN(tombBlk->minVer, TARRAY2_GET(writer->tombBlock->version, i));
    tombBlk->maxVer = TMAX(tombBlk->maxVer, TARRAY2_GET(writer->tombBlock->version, i));
H
Hongze Cheng 已提交
1181 1182
  }

H
Hongze Cheng 已提交
1183
  for (int32_t i = 0; i < ARRAY_SIZE(writer->tombBlock->dataArr); i++) {
H
Hongze Cheng 已提交
1184
    int32_t size;
H
Hongze Cheng 已提交
1185 1186
    code = tsdbCmprData((uint8_t *)TARRAY2_DATA(&writer->tombBlock->dataArr[i]),
                        TARRAY2_DATA_LEN(&writer->tombBlock->dataArr[i]), TSDB_DATA_TYPE_BIGINT, TWO_STAGE_COMP,
H
Hongze Cheng 已提交
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
                        &writer->config->bufArr[0], 0, &size, &writer->config->bufArr[1]);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = tsdbWriteFile(writer->fd[TSDB_FTYPE_TOMB], writer->files[TSDB_FTYPE_TOMB].size, writer->config->bufArr[0],
                         size);
    TSDB_CHECK_CODE(code, lino, _exit);

    tombBlk->size[i] = size;
    tombBlk->dp[0].size += size;
    writer->files[TSDB_FTYPE_TOMB].size += size;
  }

  code = TARRAY2_APPEND_PTR(writer->tombBlkArray, tombBlk);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1202
  tTombBlockClear(writer->tombBlock);
H
Hongze Cheng 已提交
1203 1204 1205 1206 1207 1208 1209 1210 1211

_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 已提交
1212
  ASSERT(TARRAY2_SIZE(writer->tombBlkArray) > 0);
H
Hongze Cheng 已提交
1213

H
Hongze Cheng 已提交
1214 1215 1216
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1217 1218 1219
  int32_t ftype = TSDB_FTYPE_TOMB;
  writer->tombFooter->tombBlkPtr->offset = writer->files[ftype].size;
  writer->tombFooter->tombBlkPtr->size = TARRAY2_DATA_LEN(writer->tombBlkArray);
H
Hongze Cheng 已提交
1220

H
Hongze Cheng 已提交
1221 1222 1223 1224
  code = tsdbWriteFile(writer->fd[ftype], writer->tombFooter->tombBlkPtr->offset,
                       (const uint8_t *)TARRAY2_DATA(writer->tombBlkArray), writer->tombFooter->tombBlkPtr->size);
  TSDB_CHECK_CODE(code, lino, _exit);
  writer->files[ftype].size += writer->tombFooter->tombBlkPtr->size;
H
Hongze Cheng 已提交
1225 1226 1227 1228 1229 1230 1231 1232

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

H
Hongze Cheng 已提交
1233
static int32_t tsdbDataFileWriteTombFooter(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
1234 1235
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1236

H
Hongze Cheng 已提交
1237 1238
  code = tsdbWriteFile(writer->fd[TSDB_FTYPE_TOMB], writer->files[TSDB_FTYPE_TOMB].size,
                       (const uint8_t *)writer->tombFooter, sizeof(STombFooter));
H
Hongze Cheng 已提交
1239
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1240
  writer->files[TSDB_FTYPE_TOMB].size += sizeof(STombFooter);
H
Hongze Cheng 已提交
1241

H
Hongze Cheng 已提交
1242 1243 1244 1245 1246 1247
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}
H
Hongze Cheng 已提交
1248

H
Hongze Cheng 已提交
1249 1250 1251
static int32_t tsdbDataFileDoWriteTombRecord(SDataFileWriter *writer, const STombRecord *record) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1252

H
Hongze Cheng 已提交
1253
  while (writer->ctx->hasOldTomb) {
H
Hongze Cheng 已提交
1254
    for (; writer->ctx->tombBlockIdx < TOMB_BLOCK_SIZE(writer->ctx->tombBlock); writer->ctx->tombBlockIdx++) {
H
Hongze Cheng 已提交
1255 1256
      STombRecord record1[1];
      tTombBlockGet(writer->ctx->tombBlock, writer->ctx->tombBlockIdx, record1);
H
Hongze Cheng 已提交
1257

H
Hongze Cheng 已提交
1258
      int32_t c = tTombRecordCompare(record, record1);
H
Hongze Cheng 已提交
1259 1260 1261
      if (c < 0) {
        break;
      } else if (c > 0) {
H
Hongze Cheng 已提交
1262
        code = tTombBlockPut(writer->tombBlock, record1);
H
Hongze Cheng 已提交
1263 1264
        TSDB_CHECK_CODE(code, lino, _exit);

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

H
Hongze Cheng 已提交
1279 1280 1281
    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 已提交
1282
      code = tsdbDataFileReadTombBlock(writer->ctx->reader, tombBlk, writer->ctx->tombBlock);
H
Hongze Cheng 已提交
1283 1284
      TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1285
      writer->ctx->tombBlockIdx = 0;
H
Hongze Cheng 已提交
1286 1287 1288 1289 1290 1291 1292 1293
      writer->ctx->tombBlkArrayIdx++;
      break;
    }
  }

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

H
Hongze Cheng 已提交
1294
  code = tTombBlockPut(writer->tombBlock, record);
H
Hongze Cheng 已提交
1295 1296
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1297
  if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
H
Hongze Cheng 已提交
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
    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 已提交
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
static int32_t tsdbDataFileWriteBrinBlk(SDataFileWriter *writer) {
  ASSERT(TARRAY2_SIZE(writer->brinBlkArray) > 0);

  int32_t code = 0;
  int32_t lino = 0;

  int32_t ftype = TSDB_FTYPE_HEAD;
  writer->headFooter->brinBlkPtr->offset = writer->files[ftype].size;
  writer->headFooter->brinBlkPtr->size = TARRAY2_DATA_LEN(writer->brinBlkArray);

  code = tsdbWriteFile(writer->fd[ftype], writer->headFooter->brinBlkPtr->offset,
                       (uint8_t *)TARRAY2_DATA(writer->brinBlkArray), writer->headFooter->brinBlkPtr->size);
  TSDB_CHECK_CODE(code, lino, _exit);
  writer->files[ftype].size += writer->headFooter->brinBlkPtr->size;

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

H
Hongze Cheng 已提交
1331 1332 1333 1334
static int32_t tsdbDataFileWriterCloseCommit(SDataFileWriter *writer, TFileOpArray *opArr) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1335
  int32_t  ftype;
H
Hongze Cheng 已提交
1336
  STFileOp op;
H
Hongze Cheng 已提交
1337

H
Hongze Cheng 已提交
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
  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 已提交
1350 1351 1352
    code = tsdbDataFileWriteBrinBlock(writer);
    TSDB_CHECK_CODE(code, lino, _exit);

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

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

H
Hongze Cheng 已提交
1399 1400
    // .sma
    ftype = TSDB_FTYPE_SMA;
H
Hongze Cheng 已提交
1401
    if (!writer->config->files[ftype].exist) {
H
Hongze Cheng 已提交
1402
      op = (STFileOp){
H
Hongze Cheng 已提交
1403 1404 1405 1406
          .optype = TSDB_FOP_CREATE,
          .fid = writer->config->fid,
          .nf = writer->files[ftype],
      };
H
Hongze Cheng 已提交
1407 1408 1409 1410
      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 已提交
1411 1412 1413 1414 1415
          .optype = TSDB_FOP_MODIFY,
          .fid = writer->config->fid,
          .of = writer->config->files[ftype].file,
          .nf = writer->files[ftype],
      };
H
Hongze Cheng 已提交
1416 1417
      code = TARRAY2_APPEND(opArr, op);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1418 1419
    }
  }
H
Hongze Cheng 已提交
1420

H
Hongze Cheng 已提交
1421 1422 1423 1424 1425 1426
  if (writer->fd[TSDB_FTYPE_TOMB]) {
    STombRecord record[1] = {{
        .suid = INT64_MAX,
        .uid = INT64_MAX,
        .version = INT64_MAX,
    }};
H
Hongze Cheng 已提交
1427

H
Hongze Cheng 已提交
1428 1429 1430 1431
    code = tsdbDataFileDoWriteTombRecord(writer, record);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = tsdbDataFileDoWriteTombBlock(writer);
H
Hongze Cheng 已提交
1432
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464

    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 已提交
1465
  }
H
Hongze Cheng 已提交
1466 1467 1468

_exit:
  if (code) {
H
Hongze Cheng 已提交
1469
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
1470 1471 1472
  }
  return code;
}
H
Hongze Cheng 已提交
1473

H
Hongze Cheng 已提交
1474 1475 1476 1477
static int32_t tsdbDataFileWriterOpenDataFD(SDataFileWriter *writer) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1478 1479 1480 1481 1482
  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 已提交
1483 1484 1485
    char    fname[TSDB_FILENAME_LEN];
    int32_t flag = TD_FILE_READ | TD_FILE_WRITE;

H
Hongze Cheng 已提交
1486
    if (writer->files[ftype].size == 0) {
H
Hongze Cheng 已提交
1487 1488 1489
      flag |= (TD_FILE_CREATE | TD_FILE_TRUNC);
    }

H
Hongze Cheng 已提交
1490
    tsdbTFileName(writer->config->tsdb, &writer->files[ftype], fname);
H
Hongze Cheng 已提交
1491
    code = tsdbOpenFile(fname, writer->config->szPage, flag, &writer->fd[ftype]);
H
Hongze Cheng 已提交
1492 1493
    TSDB_CHECK_CODE(code, lino, _exit);

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

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

H
Hongze Cheng 已提交
1500
      writer->files[ftype].size += TSDB_FHDR_SIZE;
H
Hongze Cheng 已提交
1501 1502 1503
    }
  }

H
Hongze Cheng 已提交
1504
  if (writer->ctx->reader) {
H
Hongze Cheng 已提交
1505 1506
    code = tsdbDataFileReadBrinBlk(writer->ctx->reader, &writer->ctx->brinBlkArray);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1507 1508
  }

H
Hongze Cheng 已提交
1509 1510 1511 1512 1513 1514 1515
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
  }
  return code;
}

H
Hongze Cheng 已提交
1516
int32_t tsdbDataFileWriterOpen(const SDataFileWriterConfig *config, SDataFileWriter **writer) {
H
Hongze Cheng 已提交
1517
  writer[0] = taosMemoryCalloc(1, sizeof(*writer[0]));
H
Hongze Cheng 已提交
1518 1519 1520 1521 1522 1523 1524
  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) {
1525 1526
  if (writer[0] == NULL) return 0;

H
Hongze Cheng 已提交
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
  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 已提交
1550
int32_t tsdbDataFileWriteRow(SDataFileWriter *writer, SRowInfo *row) {
H
Hongze Cheng 已提交
1551 1552
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1553 1554 1555 1556 1557 1558

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

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

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

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

H
Hongze Cheng 已提交
1572
  code = tsdbDataFileDoWriteTSData(writer, &row->row);
H
Hongze Cheng 已提交
1573 1574
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1575 1576
_exit:
  if (code) {
H
Hongze Cheng 已提交
1577
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
1578 1579
  }
  return code;
H
Hongze Cheng 已提交
1580
}
H
Hongze Cheng 已提交
1581

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

H
Hongze Cheng 已提交
1585 1586 1587
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1588
  ASSERT(bData->uid);
H
Hongze Cheng 已提交
1589

H
Hongze Cheng 已提交
1590 1591 1592 1593
  if (!writer->ctx->opened) {
    code = tsdbDataFileWriterDoOpen(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
1594

H
Hongze Cheng 已提交
1595
  if (writer->fd[TSDB_FTYPE_DATA] == NULL) {
H
Hongze Cheng 已提交
1596 1597 1598
    code = tsdbDataFileWriterOpenDataFD(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
1599

H
Hongze Cheng 已提交
1600 1601 1602
  if (bData->uid != writer->ctx->tbid->uid) {
    code = tsdbDataFileWriteTableDataEnd(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1603

H
Hongze Cheng 已提交
1604 1605 1606 1607
    code = tsdbDataFileWriteTableDataBegin(writer, (TABLEID *)bData);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
  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 已提交
1618 1619
  if (!writer->ctx->tbHasOldData       //
      && writer->blockData->nRow == 0  //
H
Hongze Cheng 已提交
1620
  ) {
H
Hongze Cheng 已提交
1621
    code = tsdbDataFileDoWriteBlockData(writer, bData);
H
Hongze Cheng 已提交
1622 1623 1624 1625 1626 1627 1628 1629
    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 已提交
1630 1631 1632 1633 1634 1635

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

H
Hongze Cheng 已提交
1638
int32_t tsdbDataFileFlush(SDataFileWriter *writer) {
H
Hongze Cheng 已提交
1639 1640
  ASSERT(writer->ctx->opened);

H
Hongze Cheng 已提交
1641
  if (writer->blockData->nRow == 0) return 0;
H
Hongze Cheng 已提交
1642 1643
  if (writer->ctx->tbHasOldData) return 0;

H
Hongze Cheng 已提交
1644
  return tsdbDataFileDoWriteBlockData(writer, writer->blockData);
H
Hongze Cheng 已提交
1645
}
H
Hongze Cheng 已提交
1646

H
Hongze Cheng 已提交
1647 1648 1649 1650 1651
static int32_t tsdbDataFileWriterOpenTombFD(SDataFileWriter *writer) {
  int32_t code = 0;
  int32_t lino = 0;

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

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

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

H
Hongze Cheng 已提交
1658 1659 1660
  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 已提交
1661

H
Hongze Cheng 已提交
1662 1663 1664 1665
  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 已提交
1666

H
Hongze Cheng 已提交
1667 1668 1669
  if (writer->ctx->reader) {
    code = tsdbDataFileReadTombBlk(writer->ctx->reader, &writer->ctx->tombBlkArray);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1670

H
Hongze Cheng 已提交
1671 1672
    if (TARRAY2_SIZE(writer->ctx->tombBlkArray) > 0) {
      writer->ctx->hasOldTomb = true;
H
Hongze Cheng 已提交
1673 1674
    }

H
Hongze Cheng 已提交
1675
    writer->ctx->tombBlkArrayIdx = 0;
H
Hongze Cheng 已提交
1676 1677
    tTombBlockClear(writer->ctx->tombBlock);
    writer->ctx->tombBlockIdx = 0;
H
Hongze Cheng 已提交
1678
  }
H
Hongze Cheng 已提交
1679 1680 1681 1682 1683 1684 1685 1686

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

H
Hongze Cheng 已提交
1687 1688 1689 1690 1691 1692 1693 1694 1695
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 已提交
1696
  if (writer->fd[TSDB_FTYPE_TOMB] == NULL) {
H
Hongze Cheng 已提交
1697 1698
    code = tsdbDataFileWriterOpenTombFD(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1699 1700
  }

H
Hongze Cheng 已提交
1701
  code = tsdbDataFileDoWriteTombRecord(writer, record);
H
Hongze Cheng 已提交
1702 1703 1704 1705 1706 1707 1708 1709
  TSDB_CHECK_CODE(code, lino, _exit);

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