tsdbSttFileRW.c 31.1 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
typedef struct {
H
Hongze Cheng 已提交
19 20
  SFDataPtr sttBlkPtr[1];
  SFDataPtr statisBlkPtr[1];
H
Hongze Cheng 已提交
21
  SFDataPtr tombBlkPtr[1];
H
Hongze Cheng 已提交
22 23
  SFDataPtr rsrvd[2];
} SSttFooter;
H
Hongze Cheng 已提交
24

H
Hongze Cheng 已提交
25
// SSttFReader ============================================================
H
Hongze Cheng 已提交
26
struct SSttFileReader {
H
Hongze Cheng 已提交
27
  SSttFileReaderConfig config[1];
H
Hongze Cheng 已提交
28
  STsdbFD             *fd;
29
  SSttFooter           footer[1];
H
Hongze Cheng 已提交
30 31 32
  struct {
    bool sttBlkLoaded;
    bool statisBlkLoaded;
H
Hongze Cheng 已提交
33
    bool tombBlkLoaded;
H
Hongze Cheng 已提交
34
  } ctx[1];
H
Hongze Cheng 已提交
35 36
  TSttBlkArray    sttBlkArray[1];
  TStatisBlkArray statisBlkArray[1];
H
Hongze Cheng 已提交
37
  TTombBlkArray   tombBlkArray[1];
38
  uint8_t        *bufArr[5];
H
Hongze Cheng 已提交
39 40
};

H
Hongze Cheng 已提交
41
// SSttFileReader
H
Hongze Cheng 已提交
42
int32_t tsdbSttFileReaderOpen(const char *fname, const SSttFileReaderConfig *config, SSttFileReader **reader) {
H
Hongze Cheng 已提交
43
  int32_t code = 0;
H
Hongze Cheng 已提交
44 45 46 47 48
  int32_t lino = 0;

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

H
Hongze Cheng 已提交
49
  reader[0]->config[0] = config[0];
H
Hongze Cheng 已提交
50 51 52
  if (reader[0]->config->bufArr == NULL) {
    reader[0]->config->bufArr = reader[0]->bufArr;
  }
H
Hongze Cheng 已提交
53 54

  // open file
H
Hongze Cheng 已提交
55 56 57 58 59 60 61 62 63
  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 已提交
64

65 66 67
  // // open each segment reader
  int64_t offset = config->file->size - sizeof(SSttFooter);
  ASSERT(offset >= TSDB_FHDR_SIZE);
H
Hongze Cheng 已提交
68

69 70
  code = tsdbReadFile(reader[0]->fd, offset, (uint8_t *)(reader[0]->footer), sizeof(SSttFooter));
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
71 72 73

_exit:
  if (code) {
H
Hongze Cheng 已提交
74
    TSDB_ERROR_LOG(TD_VID(config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
75
    tsdbSttFileReaderClose(reader);
H
Hongze Cheng 已提交
76
  }
H
Hongze Cheng 已提交
77 78 79
  return code;
}

H
Hongze Cheng 已提交
80
int32_t tsdbSttFileReaderClose(SSttFileReader **reader) {
H
Hongze Cheng 已提交
81
  if (reader[0]) {
H
Hongze Cheng 已提交
82 83 84
    for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->bufArr); ++i) {
      tFree(reader[0]->bufArr[i]);
    }
H
Hongze Cheng 已提交
85
    tsdbCloseFile(&reader[0]->fd);
86 87 88
    TARRAY2_DESTROY(reader[0]->tombBlkArray, NULL);
    TARRAY2_DESTROY(reader[0]->statisBlkArray, NULL);
    TARRAY2_DESTROY(reader[0]->sttBlkArray, NULL);
H
Hongze Cheng 已提交
89 90 91
    taosMemoryFree(reader[0]);
    reader[0] = NULL;
  }
H
Hongze Cheng 已提交
92
  return 0;
H
Hongze Cheng 已提交
93 94
}

H
Hongze Cheng 已提交
95
// SSttFSegReader
96
int32_t tsdbSttFileReadStatisBlk(SSttFileReader *reader, const TStatisBlkArray **statisBlkArray) {
H
Hongze Cheng 已提交
97
  if (!reader->ctx->statisBlkLoaded) {
H
Hongze Cheng 已提交
98
    if (reader->footer->statisBlkPtr->size > 0) {
H
Hongze Cheng 已提交
99
      ASSERT(reader->footer->statisBlkPtr->size % sizeof(SStatisBlk) == 0);
H
Hongze Cheng 已提交
100

H
Hongze Cheng 已提交
101
      int32_t size = reader->footer->statisBlkPtr->size / sizeof(SStatisBlk);
H
Hongze Cheng 已提交
102
      void   *data = taosMemoryMalloc(reader->footer->statisBlkPtr->size);
H
Hongze Cheng 已提交
103 104
      if (!data) return TSDB_CODE_OUT_OF_MEMORY;

105 106
      int32_t code =
          tsdbReadFile(reader->fd, reader->footer->statisBlkPtr->offset, data, reader->footer->statisBlkPtr->size);
H
Hongze Cheng 已提交
107 108 109 110
      if (code) {
        taosMemoryFree(data);
        return code;
      }
H
Hongze Cheng 已提交
111

H
Hongze Cheng 已提交
112
      TARRAY2_INIT_EX(reader->statisBlkArray, size, size, data);
H
Hongze Cheng 已提交
113
    } else {
H
Hongze Cheng 已提交
114
      TARRAY2_INIT(reader->statisBlkArray);
H
Hongze Cheng 已提交
115 116
    }

H
Hongze Cheng 已提交
117
    reader->ctx->statisBlkLoaded = true;
H
Hongze Cheng 已提交
118 119
  }

H
Hongze Cheng 已提交
120
  statisBlkArray[0] = reader->statisBlkArray;
H
Hongze Cheng 已提交
121
  return 0;
H
Hongze Cheng 已提交
122 123
}

124
int32_t tsdbSttFileReadTombBlk(SSttFileReader *reader, const TTombBlkArray **tombBlkArray) {
H
Hongze Cheng 已提交
125 126 127
  if (!reader->ctx->tombBlkLoaded) {
    if (reader->footer->tombBlkPtr->size > 0) {
      ASSERT(reader->footer->tombBlkPtr->size % sizeof(STombBlk) == 0);
H
Hongze Cheng 已提交
128

H
Hongze Cheng 已提交
129 130
      int32_t size = reader->footer->tombBlkPtr->size / sizeof(STombBlk);
      void   *data = taosMemoryMalloc(reader->footer->tombBlkPtr->size);
H
Hongze Cheng 已提交
131 132
      if (!data) return TSDB_CODE_OUT_OF_MEMORY;

H
Hongze Cheng 已提交
133
      int32_t code =
134
          tsdbReadFile(reader->fd, reader->footer->tombBlkPtr->offset, data, reader->footer->tombBlkPtr->size);
H
Hongze Cheng 已提交
135 136 137 138
      if (code) {
        taosMemoryFree(data);
        return code;
      }
H
Hongze Cheng 已提交
139

H
Hongze Cheng 已提交
140
      TARRAY2_INIT_EX(reader->tombBlkArray, size, size, data);
H
Hongze Cheng 已提交
141
    } else {
H
Hongze Cheng 已提交
142
      TARRAY2_INIT(reader->tombBlkArray);
H
Hongze Cheng 已提交
143 144
    }

H
Hongze Cheng 已提交
145
    reader->ctx->tombBlkLoaded = true;
H
Hongze Cheng 已提交
146 147
  }

H
Hongze Cheng 已提交
148
  tombBlkArray[0] = reader->tombBlkArray;
H
Hongze Cheng 已提交
149 150 151
  return 0;
}

152
int32_t tsdbSttFileReadSttBlk(SSttFileReader *reader, const TSttBlkArray **sttBlkArray) {
H
Hongze Cheng 已提交
153
  if (!reader->ctx->sttBlkLoaded) {
H
Hongze Cheng 已提交
154 155
    if (reader->footer->sttBlkPtr->size > 0) {
      ASSERT(reader->footer->sttBlkPtr->size % sizeof(SSttBlk) == 0);
H
Hongze Cheng 已提交
156

H
Hongze Cheng 已提交
157 158
      int32_t size = reader->footer->sttBlkPtr->size / sizeof(SSttBlk);
      void   *data = taosMemoryMalloc(reader->footer->sttBlkPtr->size);
H
Hongze Cheng 已提交
159 160
      if (!data) return TSDB_CODE_OUT_OF_MEMORY;

161
      int32_t code = tsdbReadFile(reader->fd, reader->footer->sttBlkPtr->offset, data, reader->footer->sttBlkPtr->size);
H
Hongze Cheng 已提交
162 163 164 165
      if (code) {
        taosMemoryFree(data);
        return code;
      }
H
Hongze Cheng 已提交
166

H
Hongze Cheng 已提交
167
      TARRAY2_INIT_EX(reader->sttBlkArray, size, size, data);
H
Hongze Cheng 已提交
168
    } else {
H
Hongze Cheng 已提交
169
      TARRAY2_INIT(reader->sttBlkArray);
H
Hongze Cheng 已提交
170 171
    }

H
Hongze Cheng 已提交
172
    reader->ctx->sttBlkLoaded = true;
H
Hongze Cheng 已提交
173 174
  }

H
Hongze Cheng 已提交
175
  sttBlkArray[0] = reader->sttBlkArray;
H
Hongze Cheng 已提交
176
  return 0;
H
Hongze Cheng 已提交
177 178
}

179
int32_t tsdbSttFileReadBlockData(SSttFileReader *reader, const SSttBlk *sttBlk, SBlockData *bData) {
H
Hongze Cheng 已提交
180
  int32_t code = 0;
H
Hongze Cheng 已提交
181 182
  int32_t lino = 0;

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

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

189
  code = tDecmprBlockData(reader->config->bufArr[0], sttBlk->bInfo.szBlock, bData, &reader->config->bufArr[1]);
H
Hongze Cheng 已提交
190 191 192 193
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
194
    TSDB_ERROR_LOG(TD_VID(reader->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
195
  }
H
Hongze Cheng 已提交
196 197 198
  return code;
}

199
int32_t tsdbSttFileReadBlockDataByColumn(SSttFileReader *reader, const SSttBlk *sttBlk, SBlockData *bData,
H
Hongze Cheng 已提交
200 201 202 203 204 205 206 207 208
                                         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
209
  code = tRealloc(&reader->config->bufArr[0], sttBlk->bInfo.szKey);
H
Hongze Cheng 已提交
210 211
  TSDB_CHECK_CODE(code, lino, _exit);

212
  code = tsdbReadFile(reader->fd, sttBlk->bInfo.offset, reader->config->bufArr[0], sttBlk->bInfo.szKey);
H
Hongze Cheng 已提交
213 214 215 216 217 218
  TSDB_CHECK_CODE(code, lino, _exit);

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

219
  size += tGetDiskDataHdr(reader->config->bufArr[0] + size, hdr);
H
Hongze Cheng 已提交
220 221 222 223 224 225 226 227 228

  ASSERT(hdr->delimiter == TSDB_FILE_DLMT);

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

  // uid
  if (hdr->uid == 0) {
    ASSERT(hdr->szUid);
229 230
    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 已提交
231 232 233 234 235 236 237
    TSDB_CHECK_CODE(code, lino, _exit);
  } else {
    ASSERT(hdr->szUid == 0);
  }
  size += hdr->szUid;

  // version
238 239
  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 已提交
240 241 242 243
  TSDB_CHECK_CODE(code, lino, _exit);
  size += hdr->szVer;

  // ts
244 245
  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 已提交
246 247 248 249 250 251 252 253
  TSDB_CHECK_CODE(code, lino, _exit);
  size += hdr->szKey;

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

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

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

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

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

302 303
          code = tsdbDecmprColData(reader->config->bufArr[1], blockCol, hdr->cmprAlg, hdr->nRow, colData,
                                   &reader->config->bufArr[2]);
H
Hongze Cheng 已提交
304 305 306 307 308 309 310 311
          TSDB_CHECK_CODE(code, lino, _exit);
        }
      }
    }
  }

_exit:
  if (code) {
312
    TSDB_ERROR_LOG(TD_VID(reader->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
313 314 315 316
  }
  return code;
}

317
int32_t tsdbSttFileReadTombBlock(SSttFileReader *reader, const STombBlk *tombBlk, STombBlock *tombBlock) {
H
Hongze Cheng 已提交
318
  int32_t code = 0;
H
Hongze Cheng 已提交
319 320
  int32_t lino = 0;

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

324
  code = tsdbReadFile(reader->fd, tombBlk->dp->offset, reader->config->bufArr[0], tombBlk->dp->size);
H
Hongze Cheng 已提交
325 326 327
  if (code) TSDB_CHECK_CODE(code, lino, _exit);

  int64_t size = 0;
H
Hongze Cheng 已提交
328 329
  tTombBlockClear(tombBlock);
  for (int32_t i = 0; i < ARRAY_SIZE(tombBlock->dataArr); ++i) {
330 331
    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 已提交
332 333
    TSDB_CHECK_CODE(code, lino, _exit);

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

H
Hongze Cheng 已提交
337
    size += tombBlk->size[i];
H
Hongze Cheng 已提交
338 339
  }

H
Hongze Cheng 已提交
340
  ASSERT(size == tombBlk->dp->size);
H
Hongze Cheng 已提交
341 342
_exit:
  if (code) {
343
    TSDB_ERROR_LOG(TD_VID(reader->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
344
  }
H
Hongze Cheng 已提交
345 346 347
  return code;
}

348
int32_t tsdbSttFileReadStatisBlock(SSttFileReader *reader, const SStatisBlk *statisBlk, STbStatisBlock *statisBlock) {
H
Hongze Cheng 已提交
349
  int32_t code = 0;
H
Hongze Cheng 已提交
350 351
  int32_t lino = 0;

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

355
  code = tsdbReadFile(reader->fd, statisBlk->dp->offset, reader->config->bufArr[0], statisBlk->dp->size);
H
Hongze Cheng 已提交
356
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
357 358

  int64_t size = 0;
H
Hongze Cheng 已提交
359 360
  tStatisBlockClear(statisBlock);
  for (int32_t i = 0; i < ARRAY_SIZE(statisBlock->dataArr); ++i) {
361 362 363
    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 已提交
364 365
    TSDB_CHECK_CODE(code, lino, _exit);

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

H
Hongze Cheng 已提交
369 370 371 372 373 374 375
    size += statisBlk->size[i];
  }

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

_exit:
  if (code) {
376
    TSDB_ERROR_LOG(TD_VID(reader->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
377
  }
H
Hongze Cheng 已提交
378 379 380
  return code;
}

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

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

H
Hongze Cheng 已提交
409
  int32_t code = 0;
H
Hongze Cheng 已提交
410 411

  SSttBlk sttBlk[1] = {{
H
Hongze Cheng 已提交
412 413 414 415 416 417 418 419
      .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 已提交
420 421
  }};

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

H
Hongze Cheng 已提交
429
  int32_t sizeArr[5] = {0};
H
Hongze Cheng 已提交
430 431
  code = tCmprBlockData(blockData, cmprAlg, NULL, NULL, bufArr, sizeArr);
  if (code) return code;
H
Hongze Cheng 已提交
432

H
Hongze Cheng 已提交
433
  sttBlk->bInfo.offset = *fileSize;
H
Hongze Cheng 已提交
434 435
  sttBlk->bInfo.szKey = sizeArr[2] + sizeArr[3];
  sttBlk->bInfo.szBlock = sizeArr[0] + sizeArr[1] + sttBlk->bInfo.szKey;
H
Hongze Cheng 已提交
436

H
Hongze Cheng 已提交
437
  for (int32_t i = 3; i >= 0; i--) {
H
Hongze Cheng 已提交
438
    if (sizeArr[i]) {
H
Hongze Cheng 已提交
439 440 441
      code = tsdbWriteFile(fd, *fileSize, bufArr[i], sizeArr[i]);
      if (code) return code;
      *fileSize += sizeArr[i];
H
Hongze Cheng 已提交
442 443 444
    }
  }

H
Hongze Cheng 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457
  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 已提交
458

H
Hongze Cheng 已提交
459 460 461
  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 已提交
462

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

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

H
Hongze Cheng 已提交
473
  int32_t code = 0;
H
Hongze Cheng 已提交
474
  int32_t lino = 0;
H
Hongze Cheng 已提交
475

H
Hongze Cheng 已提交
476
  SStatisBlk statisBlk[1] = {{
H
Hongze Cheng 已提交
477 478 479 480 481
      .dp[0] =
          {
              .offset = writer->file->size,
              .size = 0,
          },
H
Hongze Cheng 已提交
482 483
      .minTbid =
          {
H
Hongze Cheng 已提交
484 485
              .suid = TARRAY2_FIRST(writer->staticBlock->suid),
              .uid = TARRAY2_FIRST(writer->staticBlock->uid),
H
Hongze Cheng 已提交
486 487 488
          },
      .maxTbid =
          {
H
Hongze Cheng 已提交
489 490
              .suid = TARRAY2_LAST(writer->staticBlock->suid),
              .uid = TARRAY2_LAST(writer->staticBlock->uid),
H
Hongze Cheng 已提交
491
          },
H
Hongze Cheng 已提交
492 493 494
      .minVer = TARRAY2_FIRST(writer->staticBlock->minVer),
      .maxVer = TARRAY2_FIRST(writer->staticBlock->maxVer),
      .numRec = STATIS_BLOCK_SIZE(writer->staticBlock),
H
Hongze Cheng 已提交
495
      .cmprAlg = writer->config->cmprAlg,
H
Hongze Cheng 已提交
496 497
  }};

H
Hongze Cheng 已提交
498 499 500 501 502 503 504
  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 已提交
505
  }
H
Hongze Cheng 已提交
506

H
Hongze Cheng 已提交
507
  for (int32_t i = 0; i < STATIS_RECORD_NUM_ELEM; i++) {
H
Hongze Cheng 已提交
508 509 510
    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 已提交
511
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
512

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

H
Hongze Cheng 已提交
516 517
    statisBlk->dp->size += statisBlk->size[i];
    writer->file->size += statisBlk->size[i];
H
Hongze Cheng 已提交
518
  }
H
Hongze Cheng 已提交
519

H
Hongze Cheng 已提交
520
  code = TARRAY2_APPEND_PTR(writer->statisBlkArray, statisBlk);
H
Hongze Cheng 已提交
521
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
522

H
Hongze Cheng 已提交
523
  tStatisBlockClear(writer->staticBlock);
H
Hongze Cheng 已提交
524

H
Hongze Cheng 已提交
525 526
_exit:
  if (code) {
H
Hongze Cheng 已提交
527
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
528
  }
H
Hongze Cheng 已提交
529 530 531
  return code;
}

H
Hongze Cheng 已提交
532 533 534
int32_t tsdbFileDoWriteTombBlock(STsdbFD *fd, STombBlock *tombBlock, int8_t cmprAlg, int64_t *fileSize,
                                 TTombBlkArray *tombBlkArray, uint8_t **bufArr) {
  int32_t code;
H
Hongze Cheng 已提交
535

H
Hongze Cheng 已提交
536
  if (TOMB_BLOCK_SIZE(tombBlock) == 0) return 0;
H
Hongze Cheng 已提交
537

H
Hongze Cheng 已提交
538
  STombBlk tombBlk[1] = {{
H
Hongze Cheng 已提交
539 540
      .dp[0] =
          {
H
Hongze Cheng 已提交
541
              .offset = *fileSize,
H
Hongze Cheng 已提交
542 543
              .size = 0,
          },
H
Hongze Cheng 已提交
544
      .minTbid =
H
Hongze Cheng 已提交
545
          {
H
Hongze Cheng 已提交
546 547
              .suid = TARRAY2_FIRST(tombBlock->suid),
              .uid = TARRAY2_FIRST(tombBlock->uid),
H
Hongze Cheng 已提交
548
          },
H
Hongze Cheng 已提交
549
      .maxTbid =
H
Hongze Cheng 已提交
550
          {
H
Hongze Cheng 已提交
551 552
              .suid = TARRAY2_LAST(tombBlock->suid),
              .uid = TARRAY2_LAST(tombBlock->uid),
H
Hongze Cheng 已提交
553
          },
H
Hongze Cheng 已提交
554 555 556 557
      .minVer = TARRAY2_FIRST(tombBlock->version),
      .maxVer = TARRAY2_FIRST(tombBlock->version),
      .numRec = TOMB_BLOCK_SIZE(tombBlock),
      .cmprAlg = cmprAlg,
H
Hongze Cheng 已提交
558
  }};
H
Hongze Cheng 已提交
559

H
Hongze Cheng 已提交
560 561 562
  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 已提交
563
    }
H
Hongze Cheng 已提交
564 565
    if (tombBlk->maxVer < TARRAY2_GET(tombBlock->version, i)) {
      tombBlk->maxVer = TARRAY2_GET(tombBlock->version, i);
H
Hongze Cheng 已提交
566
    }
H
Hongze Cheng 已提交
567 568
  }

H
Hongze Cheng 已提交
569 570 571 572
  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 已提交
573

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

H
Hongze Cheng 已提交
577
    tombBlk->dp->size += tombBlk->size[i];
H
Hongze Cheng 已提交
578
    *fileSize += tombBlk->size[i];
H
Hongze Cheng 已提交
579 580
  }

H
Hongze Cheng 已提交
581 582 583 584 585 586 587 588 589
  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 已提交
590

H
Hongze Cheng 已提交
591 592 593 594 595 596
  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 已提交
597

H
Hongze Cheng 已提交
598 599
_exit:
  if (code) {
H
Hongze Cheng 已提交
600
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
601
  }
H
Hongze Cheng 已提交
602 603 604
  return code;
}

H
Hongze Cheng 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
int32_t tsdbFileDoWriteSttBlk(STsdbFD *fd, const TSttBlkArray *sttBlkArray, SFDataPtr *ptr, int64_t *fileSize) {
  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 已提交
620
static int32_t tsdbSttFileDoWriteSttBlk(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
621
  int32_t code = 0;
H
Hongze Cheng 已提交
622 623
  int32_t lino;

H
Hongze Cheng 已提交
624 625
  code = tsdbFileDoWriteSttBlk(writer->fd, writer->sttBlkArray, writer->footer->sttBlkPtr, &writer->file->size);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
626 627 628

_exit:
  if (code) {
H
Hongze Cheng 已提交
629
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
630
  }
H
Hongze Cheng 已提交
631 632 633
  return code;
}

H
Hongze Cheng 已提交
634
static int32_t tsdbSttFileDoWriteStatisBlk(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
635
  int32_t code = 0;
H
Hongze Cheng 已提交
636
  int32_t lino;
H
Hongze Cheng 已提交
637

H
Hongze Cheng 已提交
638
  writer->footer->statisBlkPtr->size = TARRAY2_DATA_LEN(writer->statisBlkArray);
H
Hongze Cheng 已提交
639
  if (writer->footer->statisBlkPtr->size) {
H
Hongze Cheng 已提交
640
    writer->footer->statisBlkPtr->offset = writer->file->size;
H
Hongze Cheng 已提交
641
    code = tsdbWriteFile(writer->fd, writer->file->size, (const uint8_t *)TARRAY2_DATA(writer->statisBlkArray),
H
Hongze Cheng 已提交
642
                         writer->footer->statisBlkPtr->size);
H
Hongze Cheng 已提交
643
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
644
    writer->file->size += writer->footer->statisBlkPtr->size;
H
Hongze Cheng 已提交
645 646 647 648
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
649
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
650
  }
H
Hongze Cheng 已提交
651 652 653
  return code;
}

H
Hongze Cheng 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
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 已提交
669
static int32_t tsdbSttFileDoWriteTombBlk(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
670
  int32_t code = 0;
H
Hongze Cheng 已提交
671
  int32_t lino = 0;
H
Hongze Cheng 已提交
672

H
Hongze Cheng 已提交
673 674
  code = tsdbFileDoWriteTombBlk(writer->fd, writer->tombBlkArray, writer->footer->tombBlkPtr, &writer->file->size);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
675 676 677

_exit:
  if (code) {
H
Hongze Cheng 已提交
678
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
679 680 681 682
  }
  return code;
}

H
Hongze Cheng 已提交
683 684
int32_t tsdbSttFileDoWriteFooterImpl(STsdbFD *fd, const SSttFooter *footer, int64_t *fileSize) {
  int32_t code = tsdbWriteFile(fd, *fileSize, (const uint8_t *)footer, sizeof(*footer));
H
Hongze Cheng 已提交
685
  if (code) return code;
H
Hongze Cheng 已提交
686
  *fileSize += sizeof(*footer);
H
Hongze Cheng 已提交
687
  return 0;
H
Hongze Cheng 已提交
688 689
}

H
Hongze Cheng 已提交
690 691 692 693
static int32_t tsdbSttFileDoWriteFooter(SSttFileWriter *writer) {
  return tsdbSttFileDoWriteFooterImpl(writer->fd, writer->footer, &writer->file->size);
}

H
Hongze Cheng 已提交
694
static int32_t tsdbSttFWriterDoOpen(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
695
  int32_t code = 0;
H
Hongze Cheng 已提交
696
  int32_t lino = 0;
H
Hongze Cheng 已提交
697

H
Hongze Cheng 已提交
698
  // set
H
Hongze Cheng 已提交
699 700
  if (!writer->config->skmTb) writer->config->skmTb = writer->skmTb;
  if (!writer->config->skmRow) writer->config->skmRow = writer->skmRow;
H
Hongze Cheng 已提交
701
  if (!writer->config->bufArr) writer->config->bufArr = writer->bufArr;
H
Hongze Cheng 已提交
702

H
Hongze Cheng 已提交
703 704 705 706 707 708 709 710 711 712 713
  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,
          },
  };
714

H
Hongze Cheng 已提交
715
  // open file
716
  int32_t flag = TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
H
Hongze Cheng 已提交
717
  char    fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
718

H
Hongze Cheng 已提交
719
  tsdbTFileName(writer->config->tsdb, writer->file, fname);
H
Hongze Cheng 已提交
720
  code = tsdbOpenFile(fname, writer->config->szPage, flag, &writer->fd);
H
Hongze Cheng 已提交
721 722
  TSDB_CHECK_CODE(code, lino, _exit);

723 724 725 726
  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 已提交
727

H
Hongze Cheng 已提交
728 729
  writer->ctx->opened = true;

H
Hongze Cheng 已提交
730
_exit:
H
Hongze Cheng 已提交
731
  if (code) {
H
Hongze Cheng 已提交
732
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
733
  }
H
Hongze Cheng 已提交
734
  return code;
H
Hongze Cheng 已提交
735 736
}

H
Hongze Cheng 已提交
737
static void tsdbSttFWriterDoClose(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
738
  ASSERT(writer->fd == NULL);
H
Hongze Cheng 已提交
739

H
Hongze Cheng 已提交
740
  for (int32_t i = 0; i < ARRAY_SIZE(writer->bufArr); ++i) {
H
Hongze Cheng 已提交
741
    tFree(writer->bufArr[i]);
H
Hongze Cheng 已提交
742 743 744
  }
  tDestroyTSchema(writer->skmRow->pTSchema);
  tDestroyTSchema(writer->skmTb->pTSchema);
H
Hongze Cheng 已提交
745 746 747
  tTombBlockDestroy(writer->tombBlock);
  tStatisBlockDestroy(writer->staticBlock);
  tBlockDataDestroy(writer->blockData);
H
Hongze Cheng 已提交
748 749 750
  TARRAY2_DESTROY(writer->tombBlkArray, NULL);
  TARRAY2_DESTROY(writer->statisBlkArray, NULL);
  TARRAY2_DESTROY(writer->sttBlkArray, NULL);
H
Hongze Cheng 已提交
751
}
H
Hongze Cheng 已提交
752

H
Hongze Cheng 已提交
753 754 755 756
static int32_t tsdbSttFileDoUpdateHeader(SSttFileWriter *writer) {
  // TODO
  return 0;
}
H
Hongze Cheng 已提交
757

H
Hongze Cheng 已提交
758
static int32_t tsdbSttFWriterCloseCommit(SSttFileWriter *writer, TFileOpArray *opArray) {
H
Hongze Cheng 已提交
759 760
  int32_t lino;
  int32_t code;
H
Hongze Cheng 已提交
761

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

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

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

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

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

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

H
Hongze Cheng 已提交
780 781
  code = tsdbSttFileDoWriteFooter(writer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
782

H
Hongze Cheng 已提交
783
  code = tsdbSttFileDoUpdateHeader(writer);
H
Hongze Cheng 已提交
784 785
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
786
  code = tsdbFsyncFile(writer->fd);
H
Hongze Cheng 已提交
787 788
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
789
  tsdbCloseFile(&writer->fd);
H
Hongze Cheng 已提交
790

791 792 793
  ASSERT(writer->file->size > 0);
  STFileOp op = (STFileOp){
      .optype = TSDB_FOP_CREATE,
H
Hongze Cheng 已提交
794
      .fid = writer->config->fid,
795 796
      .nf = writer->file[0],
  };
H
Hongze Cheng 已提交
797 798 799

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

H
Hongze Cheng 已提交
801 802
_exit:
  if (code) {
H
Hongze Cheng 已提交
803
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
804
  }
H
Hongze Cheng 已提交
805
  return code;
H
Hongze Cheng 已提交
806
}
H
Hongze Cheng 已提交
807

H
Hongze Cheng 已提交
808
static int32_t tsdbSttFWriterCloseAbort(SSttFileWriter *writer) {
809
  char fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
810
  tsdbTFileName(writer->config->tsdb, writer->file, fname);
811 812
  tsdbCloseFile(&writer->fd);
  taosRemoveFile(fname);
H
Hongze Cheng 已提交
813 814
  return 0;
}
H
Hongze Cheng 已提交
815

H
Hongze Cheng 已提交
816
int32_t tsdbSttFileWriterOpen(const SSttFileWriterConfig *config, SSttFileWriter **writer) {
H
Hongze Cheng 已提交
817
  writer[0] = taosMemoryCalloc(1, sizeof(*writer[0]));
H
Hongze Cheng 已提交
818 819
  if (writer[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;

H
Hongze Cheng 已提交
820
  writer[0]->config[0] = config[0];
H
Hongze Cheng 已提交
821
  writer[0]->ctx->opened = false;
H
Hongze Cheng 已提交
822 823 824
  return 0;
}

H
Hongze Cheng 已提交
825
int32_t tsdbSttFileWriterClose(SSttFileWriter **writer, int8_t abort, TFileOpArray *opArray) {
H
Hongze Cheng 已提交
826
  int32_t code = 0;
H
Hongze Cheng 已提交
827
  int32_t lino = 0;
H
Hongze Cheng 已提交
828

H
Hongze Cheng 已提交
829
  if (writer[0]->ctx->opened) {
H
Hongze Cheng 已提交
830 831
    if (abort) {
      code = tsdbSttFWriterCloseAbort(writer[0]);
H
Hongze Cheng 已提交
832
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
833
    } else {
H
Hongze Cheng 已提交
834
      code = tsdbSttFWriterCloseCommit(writer[0], opArray);
H
Hongze Cheng 已提交
835
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
836
    }
H
Hongze Cheng 已提交
837
    tsdbSttFWriterDoClose(writer[0]);
H
Hongze Cheng 已提交
838
  }
H
Hongze Cheng 已提交
839 840
  taosMemoryFree(writer[0]);
  writer[0] = NULL;
H
Hongze Cheng 已提交
841 842 843

_exit:
  if (code) {
H
Hongze Cheng 已提交
844
    TSDB_ERROR_LOG(TD_VID(writer[0]->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
845
  }
H
Hongze Cheng 已提交
846
  return code;
H
Hongze Cheng 已提交
847 848
}

H
Hongze Cheng 已提交
849
int32_t tsdbSttFileWriteRow(SSttFileWriter *writer, SRowInfo *row) {
H
Hongze Cheng 已提交
850
  int32_t code = 0;
H
Hongze Cheng 已提交
851
  int32_t lino = 0;
H
Hongze Cheng 已提交
852

H
Hongze Cheng 已提交
853
  if (!writer->ctx->opened) {
H
Hongze Cheng 已提交
854 855 856
    code = tsdbSttFWriterDoOpen(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
857

H
Hongze Cheng 已提交
858
  if (!TABLE_SAME_SCHEMA(row->suid, row->uid, writer->ctx->tbid->suid, writer->ctx->tbid->uid)) {
H
Hongze Cheng 已提交
859
    code = tsdbSttFileDoWriteBlockData(writer);
H
Hongze Cheng 已提交
860
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
861

H
Hongze Cheng 已提交
862 863 864 865
    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 已提交
866
    code = tBlockDataInit(writer->blockData, &id, writer->config->skmTb->pTSchema, NULL, 0);
H
Hongze Cheng 已提交
867 868 869
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
870 871 872 873 874 875 876 877 878
  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 已提交
879 880 881 882
  if (writer->ctx->tbid->uid != row->uid) {
    writer->ctx->tbid->suid = row->suid;
    writer->ctx->tbid->uid = row->uid;

H
Hongze Cheng 已提交
883
    if (STATIS_BLOCK_SIZE(writer->staticBlock) >= writer->config->maxRow) {
H
Hongze Cheng 已提交
884
      code = tsdbSttFileDoWriteStatisBlock(writer);
H
Hongze Cheng 已提交
885
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
886 887
    }

H
Hongze Cheng 已提交
888
    STbStatisRecord record = {
H
Hongze Cheng 已提交
889 890
        .suid = row->suid,
        .uid = row->uid,
H
Hongze Cheng 已提交
891 892 893 894
        .firstKey = key->ts,
        .lastKey = key->ts,
        .minVer = key->version,
        .maxVer = key->version,
H
Hongze Cheng 已提交
895
        .count = 1,
H
Hongze Cheng 已提交
896
    };
H
Hongze Cheng 已提交
897
    code = tStatisBlockPut(writer->staticBlock, &record);
H
Hongze Cheng 已提交
898
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
899
  } else {
H
Hongze Cheng 已提交
900
    ASSERT(key->ts >= TARRAY2_LAST(writer->staticBlock->lastKey));
H
Hongze Cheng 已提交
901

H
Hongze Cheng 已提交
902 903 904 905 906 907 908 909 910
    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 已提交
911
    }
H
Hongze Cheng 已提交
912 913
  }

H
Hongze Cheng 已提交
914
  if (row->row.type == TSDBROW_ROW_FMT) {
H
Hongze Cheng 已提交
915 916
    code = tsdbUpdateSkmRow(writer->config->tsdb, writer->ctx->tbid,  //
                            TSDBROW_SVERSION(&row->row), writer->config->skmRow);
H
Hongze Cheng 已提交
917
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
918 919
  }

H
Hongze Cheng 已提交
920
  // row to col conversion
H
Hongze Cheng 已提交
921 922 923 924 925 926
  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 已提交
927
  ) {
H
Hongze Cheng 已提交
928
    code = tBlockDataUpdateRow(writer->blockData, &row->row, writer->config->skmRow->pTSchema);
H
Hongze Cheng 已提交
929
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
930
  } else {
H
Hongze Cheng 已提交
931
    if (writer->blockData->nRow >= writer->config->maxRow) {
H
Hongze Cheng 已提交
932
      code = tsdbSttFileDoWriteBlockData(writer);
H
Hongze Cheng 已提交
933 934
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
935

H
Hongze Cheng 已提交
936
    code = tBlockDataAppendRow(writer->blockData, &row->row, writer->config->skmRow->pTSchema, row->uid);
H
Hongze Cheng 已提交
937
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
938 939 940 941
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
942
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
943
  }
H
Hongze Cheng 已提交
944
  return code;
H
Hongze Cheng 已提交
945 946
}

H
Hongze Cheng 已提交
947
int32_t tsdbSttFileWriteBlockData(SSttFileWriter *writer, SBlockData *bdata) {
H
Hongze Cheng 已提交
948 949 950
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
951 952
  SRowInfo row[1];
  row->suid = bdata->suid;
H
Hongze Cheng 已提交
953
  for (int32_t i = 0; i < bdata->nRow; i++) {
H
Hongze Cheng 已提交
954 955
    row->uid = bdata->uid ? bdata->uid : bdata->aUid[i];
    row->row = tsdbRowFromBlockData(bdata, i);
H
Hongze Cheng 已提交
956

H
Hongze Cheng 已提交
957
    code = tsdbSttFileWriteRow(writer, row);
H
Hongze Cheng 已提交
958 959 960 961 962
    TSDB_CHECK_CODE(code, lino, _exit);
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
963
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
964
  }
H
Hongze Cheng 已提交
965
  return code;
H
Hongze Cheng 已提交
966 967
}

H
Hongze Cheng 已提交
968
int32_t tsdbSttFileWriteTombRecord(SSttFileWriter *writer, const STombRecord *record) {
H
Hongze Cheng 已提交
969
  int32_t code;
H
Hongze Cheng 已提交
970 971
  int32_t lino;

H
Hongze Cheng 已提交
972
  if (!writer->ctx->opened) {
H
Hongze Cheng 已提交
973 974
    code = tsdbSttFWriterDoOpen(writer);
    return code;
H
Hongze Cheng 已提交
975
  } else {
H
Hongze Cheng 已提交
976
    if (writer->blockData->nRow > 0) {
H
Hongze Cheng 已提交
977
      code = tsdbSttFileDoWriteBlockData(writer);
H
Hongze Cheng 已提交
978 979
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
980

H
Hongze Cheng 已提交
981
    if (STATIS_BLOCK_SIZE(writer->staticBlock) > 0) {
H
Hongze Cheng 已提交
982 983 984
      code = tsdbSttFileDoWriteStatisBlock(writer);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
985
  }
H
Hongze Cheng 已提交
986

H
Hongze Cheng 已提交
987
  code = tTombBlockPut(writer->tombBlock, record);
H
Hongze Cheng 已提交
988
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
989

H
Hongze Cheng 已提交
990
  if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
H
Hongze Cheng 已提交
991
    code = tsdbSttFileDoWriteTombBlock(writer);
H
Hongze Cheng 已提交
992
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
993 994 995 996
  }

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

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