tsdbSttFileRW.c 31.0 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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 已提交
16
#include "tsdbSttFileRW.h"
H
Hongze Cheng 已提交
17

H
Hongze Cheng 已提交
18
// SSttFReader ============================================================
H
Hongze Cheng 已提交
19
struct SSttFileReader {
H
Hongze Cheng 已提交
20
  SSttFileReaderConfig config[1];
H
Hongze Cheng 已提交
21
  STsdbFD             *fd;
22
  SSttFooter           footer[1];
H
Hongze Cheng 已提交
23 24 25
  struct {
    bool sttBlkLoaded;
    bool statisBlkLoaded;
H
Hongze Cheng 已提交
26
    bool tombBlkLoaded;
H
Hongze Cheng 已提交
27
  } ctx[1];
H
Hongze Cheng 已提交
28 29
  TSttBlkArray    sttBlkArray[1];
  TStatisBlkArray statisBlkArray[1];
H
Hongze Cheng 已提交
30
  TTombBlkArray   tombBlkArray[1];
31
  uint8_t        *bufArr[5];
H
Hongze Cheng 已提交
32 33
};

H
Hongze Cheng 已提交
34
// SSttFileReader
H
Hongze Cheng 已提交
35
int32_t tsdbSttFileReaderOpen(const char *fname, const SSttFileReaderConfig *config, SSttFileReader **reader) {
H
Hongze Cheng 已提交
36
  int32_t code = 0;
H
Hongze Cheng 已提交
37 38 39 40 41
  int32_t lino = 0;

  reader[0] = taosMemoryCalloc(1, sizeof(*reader[0]));
  if (reader[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;

H
Hongze Cheng 已提交
42
  reader[0]->config[0] = config[0];
H
Hongze Cheng 已提交
43 44 45
  if (reader[0]->config->bufArr == NULL) {
    reader[0]->config->bufArr = reader[0]->bufArr;
  }
H
Hongze Cheng 已提交
46 47

  // open file
H
Hongze Cheng 已提交
48 49 50 51 52 53 54 55 56
  if (fname) {
    code = tsdbOpenFile(fname, config->szPage, TD_FILE_READ, &reader[0]->fd);
    TSDB_CHECK_CODE(code, lino, _exit);
  } else {
    char fname1[TSDB_FILENAME_LEN];
    tsdbTFileName(config->tsdb, config->file, fname1);
    code = tsdbOpenFile(fname1, config->szPage, TD_FILE_READ, &reader[0]->fd);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
57

58 59 60
  // // open each segment reader
  int64_t offset = config->file->size - sizeof(SSttFooter);
  ASSERT(offset >= TSDB_FHDR_SIZE);
H
Hongze Cheng 已提交
61

62 63
  code = tsdbReadFile(reader[0]->fd, offset, (uint8_t *)(reader[0]->footer), sizeof(SSttFooter));
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
64 65 66

_exit:
  if (code) {
H
Hongze Cheng 已提交
67
    TSDB_ERROR_LOG(TD_VID(config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
68
    tsdbSttFileReaderClose(reader);
H
Hongze Cheng 已提交
69
  }
H
Hongze Cheng 已提交
70 71 72
  return code;
}

H
Hongze Cheng 已提交
73
int32_t tsdbSttFileReaderClose(SSttFileReader **reader) {
H
Hongze Cheng 已提交
74
  if (reader[0]) {
H
Hongze Cheng 已提交
75 76 77
    for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->bufArr); ++i) {
      tFree(reader[0]->bufArr[i]);
    }
H
Hongze Cheng 已提交
78
    tsdbCloseFile(&reader[0]->fd);
79 80 81
    TARRAY2_DESTROY(reader[0]->tombBlkArray, NULL);
    TARRAY2_DESTROY(reader[0]->statisBlkArray, NULL);
    TARRAY2_DESTROY(reader[0]->sttBlkArray, NULL);
H
Hongze Cheng 已提交
82 83 84
    taosMemoryFree(reader[0]);
    reader[0] = NULL;
  }
H
Hongze Cheng 已提交
85
  return 0;
H
Hongze Cheng 已提交
86 87
}

H
Hongze Cheng 已提交
88
// SSttFSegReader
89
int32_t tsdbSttFileReadStatisBlk(SSttFileReader *reader, const TStatisBlkArray **statisBlkArray) {
H
Hongze Cheng 已提交
90
  if (!reader->ctx->statisBlkLoaded) {
H
Hongze Cheng 已提交
91
    if (reader->footer->statisBlkPtr->size > 0) {
H
Hongze Cheng 已提交
92
      ASSERT(reader->footer->statisBlkPtr->size % sizeof(SStatisBlk) == 0);
H
Hongze Cheng 已提交
93

H
Hongze Cheng 已提交
94
      int32_t size = reader->footer->statisBlkPtr->size / sizeof(SStatisBlk);
H
Hongze Cheng 已提交
95
      void   *data = taosMemoryMalloc(reader->footer->statisBlkPtr->size);
H
Hongze Cheng 已提交
96 97
      if (!data) return TSDB_CODE_OUT_OF_MEMORY;

98 99
      int32_t code =
          tsdbReadFile(reader->fd, reader->footer->statisBlkPtr->offset, data, reader->footer->statisBlkPtr->size);
H
Hongze Cheng 已提交
100 101 102 103
      if (code) {
        taosMemoryFree(data);
        return code;
      }
H
Hongze Cheng 已提交
104

H
Hongze Cheng 已提交
105
      TARRAY2_INIT_EX(reader->statisBlkArray, size, size, data);
H
Hongze Cheng 已提交
106
    } else {
H
Hongze Cheng 已提交
107
      TARRAY2_INIT(reader->statisBlkArray);
H
Hongze Cheng 已提交
108 109
    }

H
Hongze Cheng 已提交
110
    reader->ctx->statisBlkLoaded = true;
H
Hongze Cheng 已提交
111 112
  }

H
Hongze Cheng 已提交
113
  statisBlkArray[0] = reader->statisBlkArray;
H
Hongze Cheng 已提交
114
  return 0;
H
Hongze Cheng 已提交
115 116
}

117
int32_t tsdbSttFileReadTombBlk(SSttFileReader *reader, const TTombBlkArray **tombBlkArray) {
H
Hongze Cheng 已提交
118 119 120
  if (!reader->ctx->tombBlkLoaded) {
    if (reader->footer->tombBlkPtr->size > 0) {
      ASSERT(reader->footer->tombBlkPtr->size % sizeof(STombBlk) == 0);
H
Hongze Cheng 已提交
121

H
Hongze Cheng 已提交
122 123
      int32_t size = reader->footer->tombBlkPtr->size / sizeof(STombBlk);
      void   *data = taosMemoryMalloc(reader->footer->tombBlkPtr->size);
H
Hongze Cheng 已提交
124 125
      if (!data) return TSDB_CODE_OUT_OF_MEMORY;

H
Hongze Cheng 已提交
126
      int32_t code =
127
          tsdbReadFile(reader->fd, reader->footer->tombBlkPtr->offset, data, reader->footer->tombBlkPtr->size);
H
Hongze Cheng 已提交
128 129 130 131
      if (code) {
        taosMemoryFree(data);
        return code;
      }
H
Hongze Cheng 已提交
132

H
Hongze Cheng 已提交
133
      TARRAY2_INIT_EX(reader->tombBlkArray, size, size, data);
H
Hongze Cheng 已提交
134
    } else {
H
Hongze Cheng 已提交
135
      TARRAY2_INIT(reader->tombBlkArray);
H
Hongze Cheng 已提交
136 137
    }

H
Hongze Cheng 已提交
138
    reader->ctx->tombBlkLoaded = true;
H
Hongze Cheng 已提交
139 140
  }

H
Hongze Cheng 已提交
141
  tombBlkArray[0] = reader->tombBlkArray;
H
Hongze Cheng 已提交
142 143 144
  return 0;
}

145
int32_t tsdbSttFileReadSttBlk(SSttFileReader *reader, const TSttBlkArray **sttBlkArray) {
H
Hongze Cheng 已提交
146
  if (!reader->ctx->sttBlkLoaded) {
H
Hongze Cheng 已提交
147 148
    if (reader->footer->sttBlkPtr->size > 0) {
      ASSERT(reader->footer->sttBlkPtr->size % sizeof(SSttBlk) == 0);
H
Hongze Cheng 已提交
149

H
Hongze Cheng 已提交
150 151
      int32_t size = reader->footer->sttBlkPtr->size / sizeof(SSttBlk);
      void   *data = taosMemoryMalloc(reader->footer->sttBlkPtr->size);
H
Hongze Cheng 已提交
152 153
      if (!data) return TSDB_CODE_OUT_OF_MEMORY;

154
      int32_t code = tsdbReadFile(reader->fd, reader->footer->sttBlkPtr->offset, data, reader->footer->sttBlkPtr->size);
H
Hongze Cheng 已提交
155 156 157 158
      if (code) {
        taosMemoryFree(data);
        return code;
      }
H
Hongze Cheng 已提交
159

H
Hongze Cheng 已提交
160
      TARRAY2_INIT_EX(reader->sttBlkArray, size, size, data);
H
Hongze Cheng 已提交
161
    } else {
H
Hongze Cheng 已提交
162
      TARRAY2_INIT(reader->sttBlkArray);
H
Hongze Cheng 已提交
163 164
    }

H
Hongze Cheng 已提交
165
    reader->ctx->sttBlkLoaded = true;
H
Hongze Cheng 已提交
166 167
  }

H
Hongze Cheng 已提交
168
  sttBlkArray[0] = reader->sttBlkArray;
H
Hongze Cheng 已提交
169
  return 0;
H
Hongze Cheng 已提交
170 171
}

172
int32_t tsdbSttFileReadBlockData(SSttFileReader *reader, const SSttBlk *sttBlk, SBlockData *bData) {
H
Hongze Cheng 已提交
173
  int32_t code = 0;
H
Hongze Cheng 已提交
174 175
  int32_t lino = 0;

176
  code = tRealloc(&reader->config->bufArr[0], sttBlk->bInfo.szBlock);
H
Hongze Cheng 已提交
177 178
  TSDB_CHECK_CODE(code, lino, _exit);

179
  code = tsdbReadFile(reader->fd, sttBlk->bInfo.offset, reader->config->bufArr[0], sttBlk->bInfo.szBlock);
H
Hongze Cheng 已提交
180 181
  TSDB_CHECK_CODE(code, lino, _exit);

182
  code = tDecmprBlockData(reader->config->bufArr[0], sttBlk->bInfo.szBlock, bData, &reader->config->bufArr[1]);
H
Hongze Cheng 已提交
183 184 185 186
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
187
    TSDB_ERROR_LOG(TD_VID(reader->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
188
  }
H
Hongze Cheng 已提交
189 190 191
  return code;
}

192
int32_t tsdbSttFileReadBlockDataByColumn(SSttFileReader *reader, const SSttBlk *sttBlk, SBlockData *bData,
H
Hongze Cheng 已提交
193 194 195 196 197 198 199 200 201
                                         STSchema *pTSchema, int16_t cids[], int32_t ncid) {
  int32_t code = 0;
  int32_t lino = 0;

  TABLEID tbid = {.suid = sttBlk->suid, .uid = 0};
  code = tBlockDataInit(bData, &tbid, pTSchema, cids, ncid);
  TSDB_CHECK_CODE(code, lino, _exit);

  // uid + version + tskey
202
  code = tRealloc(&reader->config->bufArr[0], sttBlk->bInfo.szKey);
H
Hongze Cheng 已提交
203 204
  TSDB_CHECK_CODE(code, lino, _exit);

205
  code = tsdbReadFile(reader->fd, sttBlk->bInfo.offset, reader->config->bufArr[0], sttBlk->bInfo.szKey);
H
Hongze Cheng 已提交
206 207 208 209 210 211
  TSDB_CHECK_CODE(code, lino, _exit);

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

212
  size += tGetDiskDataHdr(reader->config->bufArr[0] + size, hdr);
H
Hongze Cheng 已提交
213 214 215 216 217 218 219 220 221

  ASSERT(hdr->delimiter == TSDB_FILE_DLMT);

  bData->nRow = hdr->nRow;
  bData->uid = hdr->uid;

  // uid
  if (hdr->uid == 0) {
    ASSERT(hdr->szUid);
222 223
    code = tsdbDecmprData(reader->config->bufArr[0] + size, hdr->szUid, TSDB_DATA_TYPE_BIGINT, hdr->cmprAlg,
                          (uint8_t **)&bData->aUid, sizeof(int64_t) * hdr->nRow, &reader->config->bufArr[1]);
H
Hongze Cheng 已提交
224 225 226 227 228 229 230
    TSDB_CHECK_CODE(code, lino, _exit);
  } else {
    ASSERT(hdr->szUid == 0);
  }
  size += hdr->szUid;

  // version
231 232
  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]);
H
Hongze Cheng 已提交
233 234 235 236
  TSDB_CHECK_CODE(code, lino, _exit);
  size += hdr->szVer;

  // ts
237 238
  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]);
H
Hongze Cheng 已提交
239 240 241 242 243 244 245 246
  TSDB_CHECK_CODE(code, lino, _exit);
  size += hdr->szKey;

  ASSERT(size == sttBlk->bInfo.szKey);

  // other columns
  if (bData->nColData > 0) {
    if (hdr->szBlkCol > 0) {
247
      code = tRealloc(&reader->config->bufArr[0], hdr->szBlkCol);
H
Hongze Cheng 已提交
248 249
      TSDB_CHECK_CODE(code, lino, _exit);

250 251
      code = tsdbReadFile(reader->fd, sttBlk->bInfo.offset + sttBlk->bInfo.szKey, reader->config->bufArr[0],
                          hdr->szBlkCol);
H
Hongze Cheng 已提交
252 253 254 255 256 257 258 259 260 261 262 263
      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) {
264
          size += tGetBlockCol(reader->config->bufArr[0] + size, blockCol);
H
Hongze Cheng 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
        } 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;

288
          code = tRealloc(&reader->config->bufArr[1], size1);
H
Hongze Cheng 已提交
289 290
          TSDB_CHECK_CODE(code, lino, _exit);

291 292
          code = tsdbReadFile(reader->fd, sttBlk->bInfo.offset + sttBlk->bInfo.szKey + hdr->szBlkCol + blockCol->offset,
                              reader->config->bufArr[1], size1);
H
Hongze Cheng 已提交
293 294
          TSDB_CHECK_CODE(code, lino, _exit);

295 296
          code = tsdbDecmprColData(reader->config->bufArr[1], blockCol, hdr->cmprAlg, hdr->nRow, colData,
                                   &reader->config->bufArr[2]);
H
Hongze Cheng 已提交
297 298 299 300 301 302 303 304
          TSDB_CHECK_CODE(code, lino, _exit);
        }
      }
    }
  }

_exit:
  if (code) {
305
    TSDB_ERROR_LOG(TD_VID(reader->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
306 307 308 309
  }
  return code;
}

310
int32_t tsdbSttFileReadTombBlock(SSttFileReader *reader, const STombBlk *tombBlk, STombBlock *tombBlock) {
H
Hongze Cheng 已提交
311
  int32_t code = 0;
H
Hongze Cheng 已提交
312 313
  int32_t lino = 0;

314
  code = tRealloc(&reader->config->bufArr[0], tombBlk->dp->size);
H
Hongze Cheng 已提交
315 316
  TSDB_CHECK_CODE(code, lino, _exit);

317
  code = tsdbReadFile(reader->fd, tombBlk->dp->offset, reader->config->bufArr[0], tombBlk->dp->size);
H
Hongze Cheng 已提交
318 319 320
  if (code) TSDB_CHECK_CODE(code, lino, _exit);

  int64_t size = 0;
H
Hongze Cheng 已提交
321 322
  tTombBlockClear(tombBlock);
  for (int32_t i = 0; i < ARRAY_SIZE(tombBlock->dataArr); ++i) {
323 324
    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 已提交
325 326
    TSDB_CHECK_CODE(code, lino, _exit);

327
    code = TARRAY2_APPEND_BATCH(&tombBlock->dataArr[i], reader->config->bufArr[1], tombBlk->numRec);
H
Hongze Cheng 已提交
328
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
329

H
Hongze Cheng 已提交
330
    size += tombBlk->size[i];
H
Hongze Cheng 已提交
331 332
  }

H
Hongze Cheng 已提交
333
  ASSERT(size == tombBlk->dp->size);
H
Hongze Cheng 已提交
334 335
_exit:
  if (code) {
336
    TSDB_ERROR_LOG(TD_VID(reader->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
337
  }
H
Hongze Cheng 已提交
338 339 340
  return code;
}

341
int32_t tsdbSttFileReadStatisBlock(SSttFileReader *reader, const SStatisBlk *statisBlk, STbStatisBlock *statisBlock) {
H
Hongze Cheng 已提交
342
  int32_t code = 0;
H
Hongze Cheng 已提交
343 344
  int32_t lino = 0;

345
  code = tRealloc(&reader->config->bufArr[0], statisBlk->dp->size);
H
Hongze Cheng 已提交
346 347
  TSDB_CHECK_CODE(code, lino, _exit);

348
  code = tsdbReadFile(reader->fd, statisBlk->dp->offset, reader->config->bufArr[0], statisBlk->dp->size);
H
Hongze Cheng 已提交
349
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
350 351

  int64_t size = 0;
H
Hongze Cheng 已提交
352 353
  tStatisBlockClear(statisBlock);
  for (int32_t i = 0; i < ARRAY_SIZE(statisBlock->dataArr); ++i) {
354 355 356
    code =
        tsdbDecmprData(reader->config->bufArr[0] + size, statisBlk->size[i], TSDB_DATA_TYPE_BIGINT, statisBlk->cmprAlg,
                       &reader->config->bufArr[1], sizeof(int64_t) * statisBlk->numRec, &reader->config->bufArr[2]);
H
Hongze Cheng 已提交
357 358
    TSDB_CHECK_CODE(code, lino, _exit);

359
    code = TARRAY2_APPEND_BATCH(statisBlock->dataArr + i, reader->config->bufArr[1], statisBlk->numRec);
H
Hongze Cheng 已提交
360
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
361

H
Hongze Cheng 已提交
362 363 364 365 366 367 368
    size += statisBlk->size[i];
  }

  ASSERT(size == statisBlk->dp->size);

_exit:
  if (code) {
369
    TSDB_ERROR_LOG(TD_VID(reader->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
370
  }
H
Hongze Cheng 已提交
371 372 373
  return code;
}

H
Hongze Cheng 已提交
374
// SSttFWriter ============================================================
H
Hongze Cheng 已提交
375
struct SSttFileWriter {
H
Hongze Cheng 已提交
376
  SSttFileWriterConfig config[1];
H
Hongze Cheng 已提交
377
  struct {
H
Hongze Cheng 已提交
378 379
    bool    opened;
    TABLEID tbid[1];
H
Hongze Cheng 已提交
380
  } ctx[1];
H
Hongze Cheng 已提交
381
  // file
H
Hongze Cheng 已提交
382 383
  STsdbFD *fd;
  STFile   file[1];
H
Hongze Cheng 已提交
384
  // data
H
Hongze Cheng 已提交
385 386
  SSttFooter      footer[1];
  TTombBlkArray   tombBlkArray[1];
H
Hongze Cheng 已提交
387 388
  TSttBlkArray    sttBlkArray[1];
  TStatisBlkArray statisBlkArray[1];
H
Hongze Cheng 已提交
389 390 391
  STombBlock      tombBlock[1];
  STbStatisBlock  staticBlock[1];
  SBlockData      blockData[1];
H
Hongze Cheng 已提交
392
  // helper data
H
Hongze Cheng 已提交
393 394
  SSkmInfo skmTb[1];
  SSkmInfo skmRow[1];
H
Hongze Cheng 已提交
395
  uint8_t *bufArr[5];
H
Hongze Cheng 已提交
396
};
H
Hongze Cheng 已提交
397

H
Hongze Cheng 已提交
398 399 400
int32_t tsdbFileDoWriteBlockData(STsdbFD *fd, SBlockData *blockData, int8_t cmprAlg, int64_t *fileSize,
                                 TSttBlkArray *sttBlkArray, uint8_t **bufArr) {
  if (blockData->nRow == 0) return 0;
H
Hongze Cheng 已提交
401

H
Hongze Cheng 已提交
402
  int32_t code = 0;
H
Hongze Cheng 已提交
403 404

  SSttBlk sttBlk[1] = {{
H
Hongze Cheng 已提交
405 406 407 408 409 410 411 412
      .suid = blockData->suid,
      .minUid = blockData->uid ? blockData->uid : blockData->aUid[0],
      .maxUid = blockData->uid ? blockData->uid : blockData->aUid[blockData->nRow - 1],
      .minKey = blockData->aTSKEY[0],
      .maxKey = blockData->aTSKEY[0],
      .minVer = blockData->aVersion[0],
      .maxVer = blockData->aVersion[0],
      .nRow = blockData->nRow,
H
Hongze Cheng 已提交
413 414
  }};

H
Hongze Cheng 已提交
415 416 417 418 419
  for (int32_t iRow = 1; iRow < blockData->nRow; iRow++) {
    if (sttBlk->minKey > blockData->aTSKEY[iRow]) sttBlk->minKey = blockData->aTSKEY[iRow];
    if (sttBlk->maxKey < blockData->aTSKEY[iRow]) sttBlk->maxKey = blockData->aTSKEY[iRow];
    if (sttBlk->minVer > blockData->aVersion[iRow]) sttBlk->minVer = blockData->aVersion[iRow];
    if (sttBlk->maxVer < blockData->aVersion[iRow]) sttBlk->maxVer = blockData->aVersion[iRow];
H
Hongze Cheng 已提交
420 421
  }

H
Hongze Cheng 已提交
422
  int32_t sizeArr[5] = {0};
H
Hongze Cheng 已提交
423 424
  code = tCmprBlockData(blockData, cmprAlg, NULL, NULL, bufArr, sizeArr);
  if (code) return code;
H
Hongze Cheng 已提交
425

H
Hongze Cheng 已提交
426
  sttBlk->bInfo.offset = *fileSize;
H
Hongze Cheng 已提交
427 428
  sttBlk->bInfo.szKey = sizeArr[2] + sizeArr[3];
  sttBlk->bInfo.szBlock = sizeArr[0] + sizeArr[1] + sttBlk->bInfo.szKey;
H
Hongze Cheng 已提交
429

H
Hongze Cheng 已提交
430
  for (int32_t i = 3; i >= 0; i--) {
H
Hongze Cheng 已提交
431
    if (sizeArr[i]) {
H
Hongze Cheng 已提交
432 433 434
      code = tsdbWriteFile(fd, *fileSize, bufArr[i], sizeArr[i]);
      if (code) return code;
      *fileSize += sizeArr[i];
H
Hongze Cheng 已提交
435 436 437
    }
  }

H
Hongze Cheng 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450
  code = TARRAY2_APPEND_PTR(sttBlkArray, sttBlk);
  if (code) return code;

  tBlockDataClear(blockData);

  return 0;
}

static int32_t tsdbSttFileDoWriteBlockData(SSttFileWriter *writer) {
  if (writer->blockData->nRow == 0) return 0;

  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
451

H
Hongze Cheng 已提交
452 453 454
  code = tsdbFileDoWriteBlockData(writer->fd, writer->blockData, writer->config->cmprAlg, &writer->file->size,
                                  writer->sttBlkArray, writer->config->bufArr);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
455

H
Hongze Cheng 已提交
456 457
_exit:
  if (code) {
H
Hongze Cheng 已提交
458
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
459
  }
H
Hongze Cheng 已提交
460 461 462
  return code;
}

H
Hongze Cheng 已提交
463
static int32_t tsdbSttFileDoWriteStatisBlock(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
464
  if (STATIS_BLOCK_SIZE(writer->staticBlock) == 0) return 0;
H
Hongze Cheng 已提交
465

H
Hongze Cheng 已提交
466
  int32_t code = 0;
H
Hongze Cheng 已提交
467
  int32_t lino = 0;
H
Hongze Cheng 已提交
468

H
Hongze Cheng 已提交
469
  SStatisBlk statisBlk[1] = {{
H
Hongze Cheng 已提交
470 471 472 473 474
      .dp[0] =
          {
              .offset = writer->file->size,
              .size = 0,
          },
H
Hongze Cheng 已提交
475 476
      .minTbid =
          {
H
Hongze Cheng 已提交
477 478
              .suid = TARRAY2_FIRST(writer->staticBlock->suid),
              .uid = TARRAY2_FIRST(writer->staticBlock->uid),
H
Hongze Cheng 已提交
479 480 481
          },
      .maxTbid =
          {
H
Hongze Cheng 已提交
482 483
              .suid = TARRAY2_LAST(writer->staticBlock->suid),
              .uid = TARRAY2_LAST(writer->staticBlock->uid),
H
Hongze Cheng 已提交
484
          },
H
Hongze Cheng 已提交
485 486 487
      .minVer = TARRAY2_FIRST(writer->staticBlock->minVer),
      .maxVer = TARRAY2_FIRST(writer->staticBlock->maxVer),
      .numRec = STATIS_BLOCK_SIZE(writer->staticBlock),
H
Hongze Cheng 已提交
488
      .cmprAlg = writer->config->cmprAlg,
H
Hongze Cheng 已提交
489 490
  }};

H
Hongze Cheng 已提交
491 492 493 494 495 496 497
  for (int32_t i = 1; i < STATIS_BLOCK_SIZE(writer->staticBlock); i++) {
    if (statisBlk->minVer > TARRAY2_GET(writer->staticBlock->minVer, i)) {
      statisBlk->minVer = TARRAY2_GET(writer->staticBlock->minVer, i);
    }
    if (statisBlk->maxVer < TARRAY2_GET(writer->staticBlock->maxVer, i)) {
      statisBlk->maxVer = TARRAY2_GET(writer->staticBlock->maxVer, i);
    }
H
Hongze Cheng 已提交
498
  }
H
Hongze Cheng 已提交
499

H
Hongze Cheng 已提交
500
  for (int32_t i = 0; i < STATIS_RECORD_NUM_ELEM; i++) {
H
Hongze Cheng 已提交
501 502 503
    code = tsdbCmprData((uint8_t *)TARRAY2_DATA(writer->staticBlock->dataArr + i),
                        TARRAY2_DATA_LEN(&writer->staticBlock->dataArr[i]), TSDB_DATA_TYPE_BIGINT, statisBlk->cmprAlg,
                        &writer->config->bufArr[0], 0, &statisBlk->size[i], &writer->config->bufArr[1]);
H
Hongze Cheng 已提交
504
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
505

H
Hongze Cheng 已提交
506
    code = tsdbWriteFile(writer->fd, writer->file->size, writer->config->bufArr[0], statisBlk->size[i]);
H
Hongze Cheng 已提交
507 508
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
509 510
    statisBlk->dp->size += statisBlk->size[i];
    writer->file->size += statisBlk->size[i];
H
Hongze Cheng 已提交
511
  }
H
Hongze Cheng 已提交
512

H
Hongze Cheng 已提交
513
  code = TARRAY2_APPEND_PTR(writer->statisBlkArray, statisBlk);
H
Hongze Cheng 已提交
514
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
515

H
Hongze Cheng 已提交
516
  tStatisBlockClear(writer->staticBlock);
H
Hongze Cheng 已提交
517

H
Hongze Cheng 已提交
518 519
_exit:
  if (code) {
H
Hongze Cheng 已提交
520
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
521
  }
H
Hongze Cheng 已提交
522 523 524
  return code;
}

H
Hongze Cheng 已提交
525 526 527
int32_t tsdbFileDoWriteTombBlock(STsdbFD *fd, STombBlock *tombBlock, int8_t cmprAlg, int64_t *fileSize,
                                 TTombBlkArray *tombBlkArray, uint8_t **bufArr) {
  int32_t code;
H
Hongze Cheng 已提交
528

H
Hongze Cheng 已提交
529
  if (TOMB_BLOCK_SIZE(tombBlock) == 0) return 0;
H
Hongze Cheng 已提交
530

H
Hongze Cheng 已提交
531
  STombBlk tombBlk[1] = {{
H
Hongze Cheng 已提交
532 533
      .dp[0] =
          {
H
Hongze Cheng 已提交
534
              .offset = *fileSize,
H
Hongze Cheng 已提交
535 536
              .size = 0,
          },
H
Hongze Cheng 已提交
537
      .minTbid =
H
Hongze Cheng 已提交
538
          {
H
Hongze Cheng 已提交
539 540
              .suid = TARRAY2_FIRST(tombBlock->suid),
              .uid = TARRAY2_FIRST(tombBlock->uid),
H
Hongze Cheng 已提交
541
          },
H
Hongze Cheng 已提交
542
      .maxTbid =
H
Hongze Cheng 已提交
543
          {
H
Hongze Cheng 已提交
544 545
              .suid = TARRAY2_LAST(tombBlock->suid),
              .uid = TARRAY2_LAST(tombBlock->uid),
H
Hongze Cheng 已提交
546
          },
H
Hongze Cheng 已提交
547 548 549 550
      .minVer = TARRAY2_FIRST(tombBlock->version),
      .maxVer = TARRAY2_FIRST(tombBlock->version),
      .numRec = TOMB_BLOCK_SIZE(tombBlock),
      .cmprAlg = cmprAlg,
H
Hongze Cheng 已提交
551
  }};
H
Hongze Cheng 已提交
552

H
Hongze Cheng 已提交
553 554 555
  for (int32_t i = 1; i < TOMB_BLOCK_SIZE(tombBlock); i++) {
    if (tombBlk->minVer > TARRAY2_GET(tombBlock->version, i)) {
      tombBlk->minVer = TARRAY2_GET(tombBlock->version, i);
H
Hongze Cheng 已提交
556
    }
H
Hongze Cheng 已提交
557 558
    if (tombBlk->maxVer < TARRAY2_GET(tombBlock->version, i)) {
      tombBlk->maxVer = TARRAY2_GET(tombBlock->version, i);
H
Hongze Cheng 已提交
559
    }
H
Hongze Cheng 已提交
560 561
  }

H
Hongze Cheng 已提交
562 563 564 565
  for (int32_t i = 0; i < ARRAY_SIZE(tombBlock->dataArr); i++) {
    code = tsdbCmprData((uint8_t *)TARRAY2_DATA(&tombBlock->dataArr[i]), TARRAY2_DATA_LEN(&tombBlock->dataArr[i]),
                        TSDB_DATA_TYPE_BIGINT, tombBlk->cmprAlg, &bufArr[0], 0, &tombBlk->size[i], &bufArr[1]);
    if (code) return code;
H
Hongze Cheng 已提交
566

H
Hongze Cheng 已提交
567 568
    code = tsdbWriteFile(fd, *fileSize, bufArr[0], tombBlk->size[i]);
    if (code) return code;
H
Hongze Cheng 已提交
569

H
Hongze Cheng 已提交
570
    tombBlk->dp->size += tombBlk->size[i];
H
Hongze Cheng 已提交
571
    *fileSize += tombBlk->size[i];
H
Hongze Cheng 已提交
572 573
  }

H
Hongze Cheng 已提交
574 575 576 577 578 579 580 581 582
  code = TARRAY2_APPEND_PTR(tombBlkArray, tombBlk);
  if (code) return code;

  tTombBlockClear(tombBlock);
  return 0;
}

static int32_t tsdbSttFileDoWriteTombBlock(SSttFileWriter *writer) {
  if (TOMB_BLOCK_SIZE(writer->tombBlock) == 0) return 0;
H
Hongze Cheng 已提交
583

H
Hongze Cheng 已提交
584 585 586 587 588 589
  int32_t code = 0;
  int32_t lino = 0;

  code = tsdbFileDoWriteTombBlock(writer->fd, writer->tombBlock, writer->config->cmprAlg, &writer->file->size,
                                  writer->tombBlkArray, writer->config->bufArr);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
590

H
Hongze Cheng 已提交
591 592
_exit:
  if (code) {
H
Hongze Cheng 已提交
593
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
594
  }
H
Hongze Cheng 已提交
595 596 597
  return code;
}

H
Hongze Cheng 已提交
598
int32_t tsdbFileWriteSttBlk(STsdbFD *fd, const TSttBlkArray *sttBlkArray, SFDataPtr *ptr, int64_t *fileSize) {
H
Hongze Cheng 已提交
599 600 601 602 603 604 605 606 607 608 609 610 611 612
  ptr->size = TARRAY2_DATA_LEN(sttBlkArray);
  if (ptr->size > 0) {
    ptr->offset = *fileSize;

    int32_t code = tsdbWriteFile(fd, *fileSize, (const uint8_t *)TARRAY2_DATA(sttBlkArray), ptr->size);
    if (code) {
      return code;
    }

    *fileSize += ptr->size;
  }
  return 0;
}

H
Hongze Cheng 已提交
613
static int32_t tsdbSttFileDoWriteSttBlk(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
614
  int32_t code = 0;
H
Hongze Cheng 已提交
615 616
  int32_t lino;

H
Hongze Cheng 已提交
617
  code = tsdbFileWriteSttBlk(writer->fd, writer->sttBlkArray, writer->footer->sttBlkPtr, &writer->file->size);
H
Hongze Cheng 已提交
618
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
619 620 621

_exit:
  if (code) {
H
Hongze Cheng 已提交
622
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
623
  }
H
Hongze Cheng 已提交
624 625 626
  return code;
}

H
Hongze Cheng 已提交
627
static int32_t tsdbSttFileDoWriteStatisBlk(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
628
  int32_t code = 0;
H
Hongze Cheng 已提交
629
  int32_t lino;
H
Hongze Cheng 已提交
630

H
Hongze Cheng 已提交
631
  writer->footer->statisBlkPtr->size = TARRAY2_DATA_LEN(writer->statisBlkArray);
H
Hongze Cheng 已提交
632
  if (writer->footer->statisBlkPtr->size) {
H
Hongze Cheng 已提交
633
    writer->footer->statisBlkPtr->offset = writer->file->size;
H
Hongze Cheng 已提交
634
    code = tsdbWriteFile(writer->fd, writer->file->size, (const uint8_t *)TARRAY2_DATA(writer->statisBlkArray),
H
Hongze Cheng 已提交
635
                         writer->footer->statisBlkPtr->size);
H
Hongze Cheng 已提交
636
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
637
    writer->file->size += writer->footer->statisBlkPtr->size;
H
Hongze Cheng 已提交
638 639 640 641
  }

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

H
Hongze Cheng 已提交
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
int32_t tsdbFileDoWriteTombBlk(STsdbFD *fd, const TTombBlkArray *tombBlkArray, SFDataPtr *ptr, int64_t *fileSize) {
  ptr->size = TARRAY2_DATA_LEN(tombBlkArray);
  if (ptr->size > 0) {
    ptr->offset = *fileSize;

    int32_t code = tsdbWriteFile(fd, *fileSize, (const uint8_t *)TARRAY2_DATA(tombBlkArray), ptr->size);
    if (code) {
      return code;
    }

    *fileSize += ptr->size;
  }
  return 0;
}

H
Hongze Cheng 已提交
662
static int32_t tsdbSttFileDoWriteTombBlk(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
663
  int32_t code = 0;
H
Hongze Cheng 已提交
664
  int32_t lino = 0;
H
Hongze Cheng 已提交
665

H
Hongze Cheng 已提交
666 667
  code = tsdbFileDoWriteTombBlk(writer->fd, writer->tombBlkArray, writer->footer->tombBlkPtr, &writer->file->size);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
668 669 670

_exit:
  if (code) {
H
Hongze Cheng 已提交
671
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
672 673 674 675
  }
  return code;
}

H
Hongze Cheng 已提交
676
int32_t tsdbFileWriteSttFooter(STsdbFD *fd, const SSttFooter *footer, int64_t *fileSize) {
H
Hongze Cheng 已提交
677
  int32_t code = tsdbWriteFile(fd, *fileSize, (const uint8_t *)footer, sizeof(*footer));
H
Hongze Cheng 已提交
678
  if (code) return code;
H
Hongze Cheng 已提交
679
  *fileSize += sizeof(*footer);
H
Hongze Cheng 已提交
680
  return 0;
H
Hongze Cheng 已提交
681 682
}

H
Hongze Cheng 已提交
683
static int32_t tsdbSttFileDoWriteFooter(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
684
  return tsdbFileWriteSttFooter(writer->fd, writer->footer, &writer->file->size);
H
Hongze Cheng 已提交
685 686
}

H
Hongze Cheng 已提交
687
static int32_t tsdbSttFWriterDoOpen(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
688
  int32_t code = 0;
H
Hongze Cheng 已提交
689
  int32_t lino = 0;
H
Hongze Cheng 已提交
690

H
Hongze Cheng 已提交
691
  // set
H
Hongze Cheng 已提交
692 693
  if (!writer->config->skmTb) writer->config->skmTb = writer->skmTb;
  if (!writer->config->skmRow) writer->config->skmRow = writer->skmRow;
H
Hongze Cheng 已提交
694
  if (!writer->config->bufArr) writer->config->bufArr = writer->bufArr;
H
Hongze Cheng 已提交
695

H
Hongze Cheng 已提交
696 697 698 699 700 701 702 703 704 705 706
  writer->file[0] = (STFile){
      .type = TSDB_FTYPE_STT,
      .did = writer->config->did,
      .fid = writer->config->fid,
      .cid = writer->config->cid,
      .size = 0,
      .stt[0] =
          {
              .level = writer->config->level,
          },
  };
707

H
Hongze Cheng 已提交
708
  // open file
709
  int32_t flag = TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
H
Hongze Cheng 已提交
710
  char    fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
711

H
Hongze Cheng 已提交
712
  tsdbTFileName(writer->config->tsdb, writer->file, fname);
H
Hongze Cheng 已提交
713
  code = tsdbOpenFile(fname, writer->config->szPage, flag, &writer->fd);
H
Hongze Cheng 已提交
714 715
  TSDB_CHECK_CODE(code, lino, _exit);

716 717 718 719
  uint8_t hdr[TSDB_FHDR_SIZE] = {0};
  code = tsdbWriteFile(writer->fd, 0, hdr, sizeof(hdr));
  TSDB_CHECK_CODE(code, lino, _exit);
  writer->file->size += sizeof(hdr);
H
Hongze Cheng 已提交
720

H
Hongze Cheng 已提交
721 722
  writer->ctx->opened = true;

H
Hongze Cheng 已提交
723
_exit:
H
Hongze Cheng 已提交
724
  if (code) {
H
Hongze Cheng 已提交
725
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
726
  }
H
Hongze Cheng 已提交
727
  return code;
H
Hongze Cheng 已提交
728 729
}

H
Hongze Cheng 已提交
730
static void tsdbSttFWriterDoClose(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
731
  ASSERT(writer->fd == NULL);
H
Hongze Cheng 已提交
732

H
Hongze Cheng 已提交
733
  for (int32_t i = 0; i < ARRAY_SIZE(writer->bufArr); ++i) {
H
Hongze Cheng 已提交
734
    tFree(writer->bufArr[i]);
H
Hongze Cheng 已提交
735 736 737
  }
  tDestroyTSchema(writer->skmRow->pTSchema);
  tDestroyTSchema(writer->skmTb->pTSchema);
H
Hongze Cheng 已提交
738 739 740
  tTombBlockDestroy(writer->tombBlock);
  tStatisBlockDestroy(writer->staticBlock);
  tBlockDataDestroy(writer->blockData);
H
Hongze Cheng 已提交
741 742 743
  TARRAY2_DESTROY(writer->tombBlkArray, NULL);
  TARRAY2_DESTROY(writer->statisBlkArray, NULL);
  TARRAY2_DESTROY(writer->sttBlkArray, NULL);
H
Hongze Cheng 已提交
744
}
H
Hongze Cheng 已提交
745

H
Hongze Cheng 已提交
746 747 748 749
static int32_t tsdbSttFileDoUpdateHeader(SSttFileWriter *writer) {
  // TODO
  return 0;
}
H
Hongze Cheng 已提交
750

H
Hongze Cheng 已提交
751
static int32_t tsdbSttFWriterCloseCommit(SSttFileWriter *writer, TFileOpArray *opArray) {
H
Hongze Cheng 已提交
752 753
  int32_t lino;
  int32_t code;
H
Hongze Cheng 已提交
754

H
Hongze Cheng 已提交
755
  code = tsdbSttFileDoWriteBlockData(writer);
H
Hongze Cheng 已提交
756
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
757

H
Hongze Cheng 已提交
758
  code = tsdbSttFileDoWriteStatisBlock(writer);
H
Hongze Cheng 已提交
759 760
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
761
  code = tsdbSttFileDoWriteTombBlock(writer);
H
Hongze Cheng 已提交
762
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
763

H
Hongze Cheng 已提交
764 765
  code = tsdbSttFileDoWriteSttBlk(writer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
766

H
Hongze Cheng 已提交
767 768
  code = tsdbSttFileDoWriteStatisBlk(writer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
769

H
Hongze Cheng 已提交
770
  code = tsdbSttFileDoWriteTombBlk(writer);
H
Hongze Cheng 已提交
771
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
772

H
Hongze Cheng 已提交
773 774
  code = tsdbSttFileDoWriteFooter(writer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
775

H
Hongze Cheng 已提交
776
  code = tsdbSttFileDoUpdateHeader(writer);
H
Hongze Cheng 已提交
777 778
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
779
  code = tsdbFsyncFile(writer->fd);
H
Hongze Cheng 已提交
780 781
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
782
  tsdbCloseFile(&writer->fd);
H
Hongze Cheng 已提交
783

784 785 786
  ASSERT(writer->file->size > 0);
  STFileOp op = (STFileOp){
      .optype = TSDB_FOP_CREATE,
H
Hongze Cheng 已提交
787
      .fid = writer->config->fid,
788 789
      .nf = writer->file[0],
  };
H
Hongze Cheng 已提交
790 791 792

  code = TARRAY2_APPEND(opArray, op);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
793

H
Hongze Cheng 已提交
794 795
_exit:
  if (code) {
H
Hongze Cheng 已提交
796
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
797
  }
H
Hongze Cheng 已提交
798
  return code;
H
Hongze Cheng 已提交
799
}
H
Hongze Cheng 已提交
800

H
Hongze Cheng 已提交
801
static int32_t tsdbSttFWriterCloseAbort(SSttFileWriter *writer) {
802
  char fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
803
  tsdbTFileName(writer->config->tsdb, writer->file, fname);
804 805
  tsdbCloseFile(&writer->fd);
  taosRemoveFile(fname);
H
Hongze Cheng 已提交
806 807
  return 0;
}
H
Hongze Cheng 已提交
808

H
Hongze Cheng 已提交
809
int32_t tsdbSttFileWriterOpen(const SSttFileWriterConfig *config, SSttFileWriter **writer) {
H
Hongze Cheng 已提交
810
  writer[0] = taosMemoryCalloc(1, sizeof(*writer[0]));
H
Hongze Cheng 已提交
811 812
  if (writer[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;

H
Hongze Cheng 已提交
813
  writer[0]->config[0] = config[0];
H
Hongze Cheng 已提交
814
  writer[0]->ctx->opened = false;
H
Hongze Cheng 已提交
815 816 817
  return 0;
}

H
Hongze Cheng 已提交
818
int32_t tsdbSttFileWriterClose(SSttFileWriter **writer, int8_t abort, TFileOpArray *opArray) {
H
Hongze Cheng 已提交
819
  int32_t code = 0;
H
Hongze Cheng 已提交
820
  int32_t lino = 0;
H
Hongze Cheng 已提交
821

H
Hongze Cheng 已提交
822
  if (writer[0]->ctx->opened) {
H
Hongze Cheng 已提交
823 824
    if (abort) {
      code = tsdbSttFWriterCloseAbort(writer[0]);
H
Hongze Cheng 已提交
825
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
826
    } else {
H
Hongze Cheng 已提交
827
      code = tsdbSttFWriterCloseCommit(writer[0], opArray);
H
Hongze Cheng 已提交
828
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
829
    }
H
Hongze Cheng 已提交
830
    tsdbSttFWriterDoClose(writer[0]);
H
Hongze Cheng 已提交
831
  }
H
Hongze Cheng 已提交
832 833
  taosMemoryFree(writer[0]);
  writer[0] = NULL;
H
Hongze Cheng 已提交
834 835 836

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

H
Hongze Cheng 已提交
842
int32_t tsdbSttFileWriteRow(SSttFileWriter *writer, SRowInfo *row) {
H
Hongze Cheng 已提交
843
  int32_t code = 0;
H
Hongze Cheng 已提交
844
  int32_t lino = 0;
H
Hongze Cheng 已提交
845

H
Hongze Cheng 已提交
846
  if (!writer->ctx->opened) {
H
Hongze Cheng 已提交
847 848 849
    code = tsdbSttFWriterDoOpen(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
850

H
Hongze Cheng 已提交
851
  if (!TABLE_SAME_SCHEMA(row->suid, row->uid, writer->ctx->tbid->suid, writer->ctx->tbid->uid)) {
H
Hongze Cheng 已提交
852
    code = tsdbSttFileDoWriteBlockData(writer);
H
Hongze Cheng 已提交
853
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
854

H
Hongze Cheng 已提交
855 856 857 858
    code = tsdbUpdateSkmTb(writer->config->tsdb, (TABLEID *)row, writer->config->skmTb);
    TSDB_CHECK_CODE(code, lino, _exit);

    TABLEID id = {.suid = row->suid, .uid = row->suid ? 0 : row->uid};
H
Hongze Cheng 已提交
859
    code = tBlockDataInit(writer->blockData, &id, writer->config->skmTb->pTSchema, NULL, 0);
H
Hongze Cheng 已提交
860 861 862
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
863 864 865 866 867 868 869 870 871
  TSDBKEY key[1];
  if (row->row.type == TSDBROW_ROW_FMT) {
    key->ts = row->row.pTSRow->ts;
    key->version = row->row.version;
  } else {
    key->ts = row->row.pBlockData->aTSKEY[row->row.iRow];
    key->version = row->row.pBlockData->aVersion[row->row.iRow];
  }

H
Hongze Cheng 已提交
872 873 874 875
  if (writer->ctx->tbid->uid != row->uid) {
    writer->ctx->tbid->suid = row->suid;
    writer->ctx->tbid->uid = row->uid;

H
Hongze Cheng 已提交
876
    if (STATIS_BLOCK_SIZE(writer->staticBlock) >= writer->config->maxRow) {
H
Hongze Cheng 已提交
877
      code = tsdbSttFileDoWriteStatisBlock(writer);
H
Hongze Cheng 已提交
878
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
879 880
    }

H
Hongze Cheng 已提交
881
    STbStatisRecord record = {
H
Hongze Cheng 已提交
882 883
        .suid = row->suid,
        .uid = row->uid,
H
Hongze Cheng 已提交
884 885 886 887
        .firstKey = key->ts,
        .lastKey = key->ts,
        .minVer = key->version,
        .maxVer = key->version,
H
Hongze Cheng 已提交
888
        .count = 1,
H
Hongze Cheng 已提交
889
    };
H
Hongze Cheng 已提交
890
    code = tStatisBlockPut(writer->staticBlock, &record);
H
Hongze Cheng 已提交
891
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
892
  } else {
H
Hongze Cheng 已提交
893
    ASSERT(key->ts >= TARRAY2_LAST(writer->staticBlock->lastKey));
H
Hongze Cheng 已提交
894

H
Hongze Cheng 已提交
895 896 897 898 899 900 901 902 903
    if (TARRAY2_LAST(writer->staticBlock->minVer) > key->version) {
      TARRAY2_LAST(writer->staticBlock->minVer) = key->version;
    }
    if (TARRAY2_LAST(writer->staticBlock->maxVer) < key->version) {
      TARRAY2_LAST(writer->staticBlock->maxVer) = key->version;
    }
    if (key->ts > TARRAY2_LAST(writer->staticBlock->lastKey)) {
      TARRAY2_LAST(writer->staticBlock->count)++;
      TARRAY2_LAST(writer->staticBlock->lastKey) = key->ts;
H
Hongze Cheng 已提交
904
    }
H
Hongze Cheng 已提交
905 906
  }

H
Hongze Cheng 已提交
907
  if (row->row.type == TSDBROW_ROW_FMT) {
H
Hongze Cheng 已提交
908 909
    code = tsdbUpdateSkmRow(writer->config->tsdb, writer->ctx->tbid,  //
                            TSDBROW_SVERSION(&row->row), writer->config->skmRow);
H
Hongze Cheng 已提交
910
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
911 912
  }

H
Hongze Cheng 已提交
913
  // row to col conversion
H
Hongze Cheng 已提交
914 915 916 917 918 919
  if (key->version <= writer->config->compactVersion                               //
      && writer->blockData->nRow > 0                                               //
      && writer->blockData->aTSKEY[writer->blockData->nRow - 1] == key->ts         //
      && (writer->blockData->uid                                                   //
              ? writer->blockData->uid                                             //
              : writer->blockData->aUid[writer->blockData->nRow - 1]) == row->uid  //
H
Hongze Cheng 已提交
920
  ) {
H
Hongze Cheng 已提交
921
    code = tBlockDataUpdateRow(writer->blockData, &row->row, writer->config->skmRow->pTSchema);
H
Hongze Cheng 已提交
922
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
923
  } else {
H
Hongze Cheng 已提交
924
    if (writer->blockData->nRow >= writer->config->maxRow) {
H
Hongze Cheng 已提交
925
      code = tsdbSttFileDoWriteBlockData(writer);
H
Hongze Cheng 已提交
926 927
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
928

H
Hongze Cheng 已提交
929
    code = tBlockDataAppendRow(writer->blockData, &row->row, writer->config->skmRow->pTSchema, row->uid);
H
Hongze Cheng 已提交
930
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
931 932 933 934
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
935
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
936
  }
H
Hongze Cheng 已提交
937
  return code;
H
Hongze Cheng 已提交
938 939
}

H
Hongze Cheng 已提交
940
int32_t tsdbSttFileWriteBlockData(SSttFileWriter *writer, SBlockData *bdata) {
H
Hongze Cheng 已提交
941 942 943
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
944 945
  SRowInfo row[1];
  row->suid = bdata->suid;
H
Hongze Cheng 已提交
946
  for (int32_t i = 0; i < bdata->nRow; i++) {
H
Hongze Cheng 已提交
947 948
    row->uid = bdata->uid ? bdata->uid : bdata->aUid[i];
    row->row = tsdbRowFromBlockData(bdata, i);
H
Hongze Cheng 已提交
949

H
Hongze Cheng 已提交
950
    code = tsdbSttFileWriteRow(writer, row);
H
Hongze Cheng 已提交
951 952 953 954 955
    TSDB_CHECK_CODE(code, lino, _exit);
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
956
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
957
  }
H
Hongze Cheng 已提交
958
  return code;
H
Hongze Cheng 已提交
959 960
}

H
Hongze Cheng 已提交
961
int32_t tsdbSttFileWriteTombRecord(SSttFileWriter *writer, const STombRecord *record) {
H
Hongze Cheng 已提交
962
  int32_t code;
H
Hongze Cheng 已提交
963 964
  int32_t lino;

H
Hongze Cheng 已提交
965
  if (!writer->ctx->opened) {
H
Hongze Cheng 已提交
966 967
    code = tsdbSttFWriterDoOpen(writer);
    return code;
H
Hongze Cheng 已提交
968
  } else {
H
Hongze Cheng 已提交
969
    if (writer->blockData->nRow > 0) {
H
Hongze Cheng 已提交
970
      code = tsdbSttFileDoWriteBlockData(writer);
H
Hongze Cheng 已提交
971 972
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
973

H
Hongze Cheng 已提交
974
    if (STATIS_BLOCK_SIZE(writer->staticBlock) > 0) {
H
Hongze Cheng 已提交
975 976 977
      code = tsdbSttFileDoWriteStatisBlock(writer);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
978
  }
H
Hongze Cheng 已提交
979

H
Hongze Cheng 已提交
980
  code = tTombBlockPut(writer->tombBlock, record);
H
Hongze Cheng 已提交
981
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
982

H
Hongze Cheng 已提交
983
  if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
H
Hongze Cheng 已提交
984
    code = tsdbSttFileDoWriteTombBlock(writer);
H
Hongze Cheng 已提交
985
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
986 987 988 989
  }

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

bool tsdbSttFileWriterIsOpened(SSttFileWriter *writer) { return writer->ctx->opened; }