tsdbSttFileRW.c 30.4 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
static int32_t tsdbSttFileDoWriteBlockData(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
406
  if (writer->blockData->nRow == 0) return 0;
H
Hongze Cheng 已提交
407

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

  SSttBlk sttBlk[1] = {{
H
Hongze Cheng 已提交
412 413 414 415 416 417 418 419
      .suid = writer->blockData->suid,
      .minUid = writer->blockData->uid ? writer->blockData->uid : writer->blockData->aUid[0],
      .maxUid = writer->blockData->uid ? writer->blockData->uid : writer->blockData->aUid[writer->blockData->nRow - 1],
      .minKey = writer->blockData->aTSKEY[0],
      .maxKey = writer->blockData->aTSKEY[0],
      .minVer = writer->blockData->aVersion[0],
      .maxVer = writer->blockData->aVersion[0],
      .nRow = writer->blockData->nRow,
H
Hongze Cheng 已提交
420 421
  }};

H
Hongze Cheng 已提交
422 423 424 425 426
  for (int32_t iRow = 1; iRow < writer->blockData->nRow; iRow++) {
    if (sttBlk->minKey > writer->blockData->aTSKEY[iRow]) sttBlk->minKey = writer->blockData->aTSKEY[iRow];
    if (sttBlk->maxKey < writer->blockData->aTSKEY[iRow]) sttBlk->maxKey = writer->blockData->aTSKEY[iRow];
    if (sttBlk->minVer > writer->blockData->aVersion[iRow]) sttBlk->minVer = writer->blockData->aVersion[iRow];
    if (sttBlk->maxVer < writer->blockData->aVersion[iRow]) sttBlk->maxVer = writer->blockData->aVersion[iRow];
H
Hongze Cheng 已提交
427 428
  }

H
Hongze Cheng 已提交
429
  int32_t sizeArr[5] = {0};
H
Hongze Cheng 已提交
430
  code = tCmprBlockData(writer->blockData, writer->config->cmprAlg, NULL, NULL, writer->config->bufArr, sizeArr);
H
Hongze Cheng 已提交
431
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
432

H
Hongze Cheng 已提交
433
  sttBlk->bInfo.offset = writer->file->size;
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 439
    if (sizeArr[i]) {
      code = tsdbWriteFile(writer->fd, writer->file->size, writer->config->bufArr[i], sizeArr[i]);
H
Hongze Cheng 已提交
440
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
441
      writer->file->size += sizeArr[i];
H
Hongze Cheng 已提交
442 443 444
    }
  }

H
Hongze Cheng 已提交
445
  code = TARRAY2_APPEND_PTR(writer->sttBlkArray, sttBlk);
H
Hongze Cheng 已提交
446
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
447

H
Hongze Cheng 已提交
448
  tBlockDataClear(writer->blockData);
H
Hongze Cheng 已提交
449

H
Hongze Cheng 已提交
450 451
_exit:
  if (code) {
H
Hongze Cheng 已提交
452
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
453
  }
H
Hongze Cheng 已提交
454 455 456
  return code;
}

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

H
Hongze Cheng 已提交
460
  int32_t code = 0;
H
Hongze Cheng 已提交
461
  int32_t lino = 0;
H
Hongze Cheng 已提交
462

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

H
Hongze Cheng 已提交
485 486 487 488 489 490 491
  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 已提交
492
  }
H
Hongze Cheng 已提交
493

H
Hongze Cheng 已提交
494
  for (int32_t i = 0; i < STATIS_RECORD_NUM_ELEM; i++) {
H
Hongze Cheng 已提交
495 496 497
    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 已提交
498
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
499

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

H
Hongze Cheng 已提交
503 504
    statisBlk->dp->size += statisBlk->size[i];
    writer->file->size += statisBlk->size[i];
H
Hongze Cheng 已提交
505
  }
H
Hongze Cheng 已提交
506

H
Hongze Cheng 已提交
507
  code = TARRAY2_APPEND_PTR(writer->statisBlkArray, statisBlk);
H
Hongze Cheng 已提交
508
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
509

H
Hongze Cheng 已提交
510
  tStatisBlockClear(writer->staticBlock);
H
Hongze Cheng 已提交
511

H
Hongze Cheng 已提交
512 513
_exit:
  if (code) {
H
Hongze Cheng 已提交
514
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
515
  }
H
Hongze Cheng 已提交
516 517 518
  return code;
}

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

H
Hongze Cheng 已提交
522
  int32_t code = 0;
H
Hongze Cheng 已提交
523
  int32_t lino = 0;
H
Hongze Cheng 已提交
524

H
Hongze Cheng 已提交
525
  STombBlk tombBlk[1] = {{
H
Hongze Cheng 已提交
526 527 528 529 530
      .dp[0] =
          {
              .offset = writer->file->size,
              .size = 0,
          },
H
Hongze Cheng 已提交
531
      .minTbid =
H
Hongze Cheng 已提交
532
          {
H
Hongze Cheng 已提交
533 534
              .suid = TARRAY2_FIRST(writer->tombBlock->suid),
              .uid = TARRAY2_FIRST(writer->tombBlock->uid),
H
Hongze Cheng 已提交
535
          },
H
Hongze Cheng 已提交
536
      .maxTbid =
H
Hongze Cheng 已提交
537
          {
H
Hongze Cheng 已提交
538 539
              .suid = TARRAY2_LAST(writer->tombBlock->suid),
              .uid = TARRAY2_LAST(writer->tombBlock->uid),
H
Hongze Cheng 已提交
540
          },
H
Hongze Cheng 已提交
541 542 543
      .minVer = TARRAY2_FIRST(writer->tombBlock->version),
      .maxVer = TARRAY2_FIRST(writer->tombBlock->version),
      .numRec = TOMB_BLOCK_SIZE(writer->tombBlock),
H
Hongze Cheng 已提交
544
      .cmprAlg = writer->config->cmprAlg,
H
Hongze Cheng 已提交
545
  }};
H
Hongze Cheng 已提交
546

H
Hongze Cheng 已提交
547 548 549 550 551 552 553
  for (int32_t i = 1; i < TOMB_BLOCK_SIZE(writer->tombBlock); i++) {
    if (tombBlk->minVer > TARRAY2_GET(writer->tombBlock->version, i)) {
      tombBlk->minVer = TARRAY2_GET(writer->tombBlock->version, i);
    }
    if (tombBlk->maxVer < TARRAY2_GET(writer->tombBlock->version, i)) {
      tombBlk->maxVer = TARRAY2_GET(writer->tombBlock->version, i);
    }
H
Hongze Cheng 已提交
554 555
  }

H
Hongze Cheng 已提交
556 557 558 559
  for (int32_t i = 0; i < ARRAY_SIZE(writer->tombBlock->dataArr); i++) {
    code = tsdbCmprData((uint8_t *)TARRAY2_DATA(&writer->tombBlock->dataArr[i]),
                        TARRAY2_DATA_LEN(&writer->tombBlock->dataArr[i]), TSDB_DATA_TYPE_BIGINT, tombBlk->cmprAlg,
                        &writer->config->bufArr[0], 0, &tombBlk->size[i], &writer->config->bufArr[1]);
H
Hongze Cheng 已提交
560
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
561

H
Hongze Cheng 已提交
562
    code = tsdbWriteFile(writer->fd, writer->file->size, writer->config->bufArr[0], tombBlk->size[i]);
H
Hongze Cheng 已提交
563 564
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
565 566
    tombBlk->dp->size += tombBlk->size[i];
    writer->file->size += tombBlk->size[i];
H
Hongze Cheng 已提交
567 568
  }

H
Hongze Cheng 已提交
569
  code = TARRAY2_APPEND_PTR(writer->tombBlkArray, tombBlk);
H
Hongze Cheng 已提交
570
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
571

H
Hongze Cheng 已提交
572
  tTombBlockClear(writer->tombBlock);
H
Hongze Cheng 已提交
573

H
Hongze Cheng 已提交
574 575
_exit:
  if (code) {
H
Hongze Cheng 已提交
576
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
577
  }
H
Hongze Cheng 已提交
578 579 580
  return code;
}

H
Hongze Cheng 已提交
581
static int32_t tsdbSttFileDoWriteSttBlk(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
582
  int32_t code = 0;
H
Hongze Cheng 已提交
583 584
  int32_t lino;

H
Hongze Cheng 已提交
585
  writer->footer->sttBlkPtr->size = TARRAY2_DATA_LEN(writer->sttBlkArray);
H
Hongze Cheng 已提交
586
  if (writer->footer->sttBlkPtr->size) {
H
Hongze Cheng 已提交
587
    writer->footer->sttBlkPtr->offset = writer->file->size;
H
Hongze Cheng 已提交
588
    code = tsdbWriteFile(writer->fd, writer->file->size, (const uint8_t *)TARRAY2_DATA(writer->sttBlkArray),
H
Hongze Cheng 已提交
589
                         writer->footer->sttBlkPtr->size);
H
Hongze Cheng 已提交
590
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
591
    writer->file->size += writer->footer->sttBlkPtr->size;
H
Hongze Cheng 已提交
592 593 594 595
  }

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

H
Hongze Cheng 已提交
601
static int32_t tsdbSttFileDoWriteStatisBlk(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
602
  int32_t code = 0;
H
Hongze Cheng 已提交
603
  int32_t lino;
H
Hongze Cheng 已提交
604

H
Hongze Cheng 已提交
605
  writer->footer->statisBlkPtr->size = TARRAY2_DATA_LEN(writer->statisBlkArray);
H
Hongze Cheng 已提交
606
  if (writer->footer->statisBlkPtr->size) {
H
Hongze Cheng 已提交
607
    writer->footer->statisBlkPtr->offset = writer->file->size;
H
Hongze Cheng 已提交
608
    code = tsdbWriteFile(writer->fd, writer->file->size, (const uint8_t *)TARRAY2_DATA(writer->statisBlkArray),
H
Hongze Cheng 已提交
609
                         writer->footer->statisBlkPtr->size);
H
Hongze Cheng 已提交
610
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
611
    writer->file->size += writer->footer->statisBlkPtr->size;
H
Hongze Cheng 已提交
612 613 614 615
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
616
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
617
  }
H
Hongze Cheng 已提交
618 619 620
  return code;
}

H
Hongze Cheng 已提交
621
static int32_t tsdbSttFileDoWriteTombBlk(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
622
  int32_t code = 0;
H
Hongze Cheng 已提交
623
  int32_t lino = 0;
H
Hongze Cheng 已提交
624

H
Hongze Cheng 已提交
625 626
  writer->footer->tombBlkPtr->size = TARRAY2_DATA_LEN(writer->tombBlkArray);
  if (writer->footer->tombBlkPtr->size) {
H
Hongze Cheng 已提交
627
    writer->footer->tombBlkPtr->offset = writer->file->size;
H
Hongze Cheng 已提交
628 629
    code = tsdbWriteFile(writer->fd, writer->file->size, (const uint8_t *)TARRAY2_DATA(writer->tombBlkArray),
                         writer->footer->tombBlkPtr->size);
H
Hongze Cheng 已提交
630
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
631
    writer->file->size += writer->footer->tombBlkPtr->size;
H
Hongze Cheng 已提交
632 633 634 635
  }

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

H
Hongze Cheng 已提交
641
static int32_t tsdbSttFileDoWriteFooter(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
642
  int32_t code = tsdbWriteFile(writer->fd, writer->file->size, (const uint8_t *)writer->footer, sizeof(writer->footer));
H
Hongze Cheng 已提交
643
  if (code) return code;
H
Hongze Cheng 已提交
644
  writer->file->size += sizeof(writer->footer);
H
Hongze Cheng 已提交
645
  return 0;
H
Hongze Cheng 已提交
646 647
}

H
Hongze Cheng 已提交
648
static int32_t tsdbSttFWriterDoOpen(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
649
  int32_t code = 0;
H
Hongze Cheng 已提交
650
  int32_t lino = 0;
H
Hongze Cheng 已提交
651

H
Hongze Cheng 已提交
652
  // set
H
Hongze Cheng 已提交
653 654
  if (!writer->config->skmTb) writer->config->skmTb = writer->skmTb;
  if (!writer->config->skmRow) writer->config->skmRow = writer->skmRow;
H
Hongze Cheng 已提交
655
  if (!writer->config->bufArr) writer->config->bufArr = writer->bufArr;
H
Hongze Cheng 已提交
656

H
Hongze Cheng 已提交
657 658 659 660 661 662 663 664 665 666 667
  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,
          },
  };
668

H
Hongze Cheng 已提交
669
  // open file
670
  int32_t flag = TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
H
Hongze Cheng 已提交
671
  char    fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
672

H
Hongze Cheng 已提交
673
  tsdbTFileName(writer->config->tsdb, writer->file, fname);
H
Hongze Cheng 已提交
674
  code = tsdbOpenFile(fname, writer->config->szPage, flag, &writer->fd);
H
Hongze Cheng 已提交
675 676
  TSDB_CHECK_CODE(code, lino, _exit);

677 678 679 680
  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 已提交
681

H
Hongze Cheng 已提交
682 683
  writer->ctx->opened = true;

H
Hongze Cheng 已提交
684
_exit:
H
Hongze Cheng 已提交
685
  if (code) {
H
Hongze Cheng 已提交
686
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
687
  }
H
Hongze Cheng 已提交
688
  return code;
H
Hongze Cheng 已提交
689 690
}

H
Hongze Cheng 已提交
691
static void tsdbSttFWriterDoClose(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
692
  ASSERT(writer->fd == NULL);
H
Hongze Cheng 已提交
693

H
Hongze Cheng 已提交
694
  for (int32_t i = 0; i < ARRAY_SIZE(writer->bufArr); ++i) {
H
Hongze Cheng 已提交
695
    tFree(writer->bufArr[i]);
H
Hongze Cheng 已提交
696 697 698
  }
  tDestroyTSchema(writer->skmRow->pTSchema);
  tDestroyTSchema(writer->skmTb->pTSchema);
H
Hongze Cheng 已提交
699 700 701
  tTombBlockDestroy(writer->tombBlock);
  tStatisBlockDestroy(writer->staticBlock);
  tBlockDataDestroy(writer->blockData);
H
Hongze Cheng 已提交
702 703 704
  TARRAY2_DESTROY(writer->tombBlkArray, NULL);
  TARRAY2_DESTROY(writer->statisBlkArray, NULL);
  TARRAY2_DESTROY(writer->sttBlkArray, NULL);
H
Hongze Cheng 已提交
705
}
H
Hongze Cheng 已提交
706

H
Hongze Cheng 已提交
707 708 709 710
static int32_t tsdbSttFileDoUpdateHeader(SSttFileWriter *writer) {
  // TODO
  return 0;
}
H
Hongze Cheng 已提交
711

H
Hongze Cheng 已提交
712
static int32_t tsdbSttFWriterCloseCommit(SSttFileWriter *writer, TFileOpArray *opArray) {
H
Hongze Cheng 已提交
713 714
  int32_t lino;
  int32_t code;
H
Hongze Cheng 已提交
715

H
Hongze Cheng 已提交
716
  code = tsdbSttFileDoWriteBlockData(writer);
H
Hongze Cheng 已提交
717
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
718

H
Hongze Cheng 已提交
719
  code = tsdbSttFileDoWriteStatisBlock(writer);
H
Hongze Cheng 已提交
720 721
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
722
  code = tsdbSttFileDoWriteTombBlock(writer);
H
Hongze Cheng 已提交
723
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
724

H
Hongze Cheng 已提交
725 726
  code = tsdbSttFileDoWriteSttBlk(writer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
727

H
Hongze Cheng 已提交
728 729
  code = tsdbSttFileDoWriteStatisBlk(writer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
730

H
Hongze Cheng 已提交
731
  code = tsdbSttFileDoWriteTombBlk(writer);
H
Hongze Cheng 已提交
732
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
733

H
Hongze Cheng 已提交
734 735
  code = tsdbSttFileDoWriteFooter(writer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
736

H
Hongze Cheng 已提交
737
  code = tsdbSttFileDoUpdateHeader(writer);
H
Hongze Cheng 已提交
738 739
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
740
  code = tsdbFsyncFile(writer->fd);
H
Hongze Cheng 已提交
741 742
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
743
  tsdbCloseFile(&writer->fd);
H
Hongze Cheng 已提交
744

745 746 747
  ASSERT(writer->file->size > 0);
  STFileOp op = (STFileOp){
      .optype = TSDB_FOP_CREATE,
H
Hongze Cheng 已提交
748
      .fid = writer->config->fid,
749 750
      .nf = writer->file[0],
  };
H
Hongze Cheng 已提交
751 752 753

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

H
Hongze Cheng 已提交
755 756
_exit:
  if (code) {
H
Hongze Cheng 已提交
757
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
758
  }
H
Hongze Cheng 已提交
759
  return code;
H
Hongze Cheng 已提交
760
}
H
Hongze Cheng 已提交
761

H
Hongze Cheng 已提交
762
static int32_t tsdbSttFWriterCloseAbort(SSttFileWriter *writer) {
763
  char fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
764
  tsdbTFileName(writer->config->tsdb, writer->file, fname);
765 766
  tsdbCloseFile(&writer->fd);
  taosRemoveFile(fname);
H
Hongze Cheng 已提交
767 768
  return 0;
}
H
Hongze Cheng 已提交
769

H
Hongze Cheng 已提交
770
int32_t tsdbSttFileWriterOpen(const SSttFileWriterConfig *config, SSttFileWriter **writer) {
H
Hongze Cheng 已提交
771
  writer[0] = taosMemoryCalloc(1, sizeof(*writer[0]));
H
Hongze Cheng 已提交
772 773
  if (writer[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;

H
Hongze Cheng 已提交
774
  writer[0]->config[0] = config[0];
H
Hongze Cheng 已提交
775
  writer[0]->ctx->opened = false;
H
Hongze Cheng 已提交
776 777 778
  return 0;
}

H
Hongze Cheng 已提交
779
int32_t tsdbSttFileWriterClose(SSttFileWriter **writer, int8_t abort, TFileOpArray *opArray) {
H
Hongze Cheng 已提交
780
  int32_t code = 0;
H
Hongze Cheng 已提交
781
  int32_t lino = 0;
H
Hongze Cheng 已提交
782

H
Hongze Cheng 已提交
783
  if (writer[0]->ctx->opened) {
H
Hongze Cheng 已提交
784 785
    if (abort) {
      code = tsdbSttFWriterCloseAbort(writer[0]);
H
Hongze Cheng 已提交
786
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
787
    } else {
H
Hongze Cheng 已提交
788
      code = tsdbSttFWriterCloseCommit(writer[0], opArray);
H
Hongze Cheng 已提交
789
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
790
    }
H
Hongze Cheng 已提交
791
    tsdbSttFWriterDoClose(writer[0]);
H
Hongze Cheng 已提交
792
  }
H
Hongze Cheng 已提交
793 794
  taosMemoryFree(writer[0]);
  writer[0] = NULL;
H
Hongze Cheng 已提交
795 796 797

_exit:
  if (code) {
H
Hongze Cheng 已提交
798
    TSDB_ERROR_LOG(TD_VID(writer[0]->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
799
  }
H
Hongze Cheng 已提交
800
  return code;
H
Hongze Cheng 已提交
801 802
}

H
Hongze Cheng 已提交
803
int32_t tsdbSttFileWriteRow(SSttFileWriter *writer, SRowInfo *row) {
H
Hongze Cheng 已提交
804
  int32_t code = 0;
H
Hongze Cheng 已提交
805
  int32_t lino = 0;
H
Hongze Cheng 已提交
806

H
Hongze Cheng 已提交
807
  if (!writer->ctx->opened) {
H
Hongze Cheng 已提交
808 809 810
    code = tsdbSttFWriterDoOpen(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
811

H
Hongze Cheng 已提交
812
  if (!TABLE_SAME_SCHEMA(row->suid, row->uid, writer->ctx->tbid->suid, writer->ctx->tbid->uid)) {
H
Hongze Cheng 已提交
813
    code = tsdbSttFileDoWriteBlockData(writer);
H
Hongze Cheng 已提交
814
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
815

H
Hongze Cheng 已提交
816 817 818 819
    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 已提交
820
    code = tBlockDataInit(writer->blockData, &id, writer->config->skmTb->pTSchema, NULL, 0);
H
Hongze Cheng 已提交
821 822 823
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
824 825 826 827 828 829 830 831 832
  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 已提交
833 834 835 836
  if (writer->ctx->tbid->uid != row->uid) {
    writer->ctx->tbid->suid = row->suid;
    writer->ctx->tbid->uid = row->uid;

H
Hongze Cheng 已提交
837
    if (STATIS_BLOCK_SIZE(writer->staticBlock) >= writer->config->maxRow) {
H
Hongze Cheng 已提交
838
      code = tsdbSttFileDoWriteStatisBlock(writer);
H
Hongze Cheng 已提交
839
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
840 841
    }

H
Hongze Cheng 已提交
842
    STbStatisRecord record = {
H
Hongze Cheng 已提交
843 844
        .suid = row->suid,
        .uid = row->uid,
H
Hongze Cheng 已提交
845 846 847 848
        .firstKey = key->ts,
        .lastKey = key->ts,
        .minVer = key->version,
        .maxVer = key->version,
H
Hongze Cheng 已提交
849
        .count = 1,
H
Hongze Cheng 已提交
850
    };
H
Hongze Cheng 已提交
851
    code = tStatisBlockPut(writer->staticBlock, &record);
H
Hongze Cheng 已提交
852
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
853
  } else {
H
Hongze Cheng 已提交
854
    ASSERT(key->ts >= TARRAY2_LAST(writer->staticBlock->lastKey));
H
Hongze Cheng 已提交
855

H
Hongze Cheng 已提交
856 857 858 859 860 861 862 863 864
    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 已提交
865
    }
H
Hongze Cheng 已提交
866 867
  }

H
Hongze Cheng 已提交
868
  if (row->row.type == TSDBROW_ROW_FMT) {
H
Hongze Cheng 已提交
869 870
    code = tsdbUpdateSkmRow(writer->config->tsdb, writer->ctx->tbid,  //
                            TSDBROW_SVERSION(&row->row), writer->config->skmRow);
H
Hongze Cheng 已提交
871
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
872 873
  }

H
Hongze Cheng 已提交
874
  // row to col conversion
H
Hongze Cheng 已提交
875 876 877 878 879 880
  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 已提交
881
  ) {
H
Hongze Cheng 已提交
882
    code = tBlockDataUpdateRow(writer->blockData, &row->row, writer->config->skmRow->pTSchema);
H
Hongze Cheng 已提交
883
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
884
  } else {
H
Hongze Cheng 已提交
885
    if (writer->blockData->nRow >= writer->config->maxRow) {
H
Hongze Cheng 已提交
886
      code = tsdbSttFileDoWriteBlockData(writer);
H
Hongze Cheng 已提交
887 888
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
889

H
Hongze Cheng 已提交
890
    code = tBlockDataAppendRow(writer->blockData, &row->row, writer->config->skmRow->pTSchema, row->uid);
H
Hongze Cheng 已提交
891
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
892 893 894 895
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
896
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
897
  }
H
Hongze Cheng 已提交
898
  return code;
H
Hongze Cheng 已提交
899 900
}

H
Hongze Cheng 已提交
901
int32_t tsdbSttFileWriteBlockData(SSttFileWriter *writer, SBlockData *bdata) {
H
Hongze Cheng 已提交
902 903 904
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
905 906
  SRowInfo row[1];
  row->suid = bdata->suid;
H
Hongze Cheng 已提交
907
  for (int32_t i = 0; i < bdata->nRow; i++) {
H
Hongze Cheng 已提交
908 909
    row->uid = bdata->uid ? bdata->uid : bdata->aUid[i];
    row->row = tsdbRowFromBlockData(bdata, i);
H
Hongze Cheng 已提交
910

H
Hongze Cheng 已提交
911
    code = tsdbSttFileWriteRow(writer, row);
H
Hongze Cheng 已提交
912 913 914 915 916
    TSDB_CHECK_CODE(code, lino, _exit);
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
917
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
918
  }
H
Hongze Cheng 已提交
919
  return code;
H
Hongze Cheng 已提交
920 921
}

H
Hongze Cheng 已提交
922
int32_t tsdbSttFileWriteTombRecord(SSttFileWriter *writer, const STombRecord *record) {
H
Hongze Cheng 已提交
923
  int32_t code;
H
Hongze Cheng 已提交
924 925
  int32_t lino;

H
Hongze Cheng 已提交
926
  if (!writer->ctx->opened) {
H
Hongze Cheng 已提交
927 928
    code = tsdbSttFWriterDoOpen(writer);
    return code;
H
Hongze Cheng 已提交
929
  } else {
H
Hongze Cheng 已提交
930
    if (writer->blockData->nRow > 0) {
H
Hongze Cheng 已提交
931
      code = tsdbSttFileDoWriteBlockData(writer);
H
Hongze Cheng 已提交
932 933
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
934

H
Hongze Cheng 已提交
935
    if (STATIS_BLOCK_SIZE(writer->staticBlock) > 0) {
H
Hongze Cheng 已提交
936 937 938
      code = tsdbSttFileDoWriteStatisBlock(writer);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
939
  }
H
Hongze Cheng 已提交
940

H
Hongze Cheng 已提交
941
  code = tTombBlockPut(writer->tombBlock, record);
H
Hongze Cheng 已提交
942
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
943

H
Hongze Cheng 已提交
944
  if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
H
Hongze Cheng 已提交
945
    code = tsdbSttFileDoWriteTombBlock(writer);
H
Hongze Cheng 已提交
946
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
947 948 949 950
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
951
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
952 953
  }
  return code;
H
Hongze Cheng 已提交
954 955 956
}

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