tsdbSttFileRW.c 30.3 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

657 658
  writer->file[0] = writer->config->file;

H
Hongze Cheng 已提交
659
  // open file
660
  int32_t flag = TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
H
Hongze Cheng 已提交
661
  char    fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
662

H
Hongze Cheng 已提交
663
  tsdbTFileName(writer->config->tsdb, writer->file, fname);
H
Hongze Cheng 已提交
664
  code = tsdbOpenFile(fname, writer->config->szPage, flag, &writer->fd);
H
Hongze Cheng 已提交
665 666
  TSDB_CHECK_CODE(code, lino, _exit);

667 668 669 670
  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 已提交
671

H
Hongze Cheng 已提交
672 673
  writer->ctx->opened = true;

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

H
Hongze Cheng 已提交
681
static void tsdbSttFWriterDoClose(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
682
  ASSERT(writer->fd == NULL);
H
Hongze Cheng 已提交
683

H
Hongze Cheng 已提交
684
  for (int32_t i = 0; i < ARRAY_SIZE(writer->bufArr); ++i) {
H
Hongze Cheng 已提交
685
    tFree(writer->bufArr[i]);
H
Hongze Cheng 已提交
686 687 688
  }
  tDestroyTSchema(writer->skmRow->pTSchema);
  tDestroyTSchema(writer->skmTb->pTSchema);
H
Hongze Cheng 已提交
689 690 691
  tTombBlockDestroy(writer->tombBlock);
  tStatisBlockDestroy(writer->staticBlock);
  tBlockDataDestroy(writer->blockData);
H
Hongze Cheng 已提交
692 693 694
  TARRAY2_DESTROY(writer->tombBlkArray, NULL);
  TARRAY2_DESTROY(writer->statisBlkArray, NULL);
  TARRAY2_DESTROY(writer->sttBlkArray, NULL);
H
Hongze Cheng 已提交
695
}
H
Hongze Cheng 已提交
696

H
Hongze Cheng 已提交
697 698 699 700
static int32_t tsdbSttFileDoUpdateHeader(SSttFileWriter *writer) {
  // TODO
  return 0;
}
H
Hongze Cheng 已提交
701

H
Hongze Cheng 已提交
702
static int32_t tsdbSttFWriterCloseCommit(SSttFileWriter *writer, TFileOpArray *opArray) {
H
Hongze Cheng 已提交
703 704
  int32_t lino;
  int32_t code;
H
Hongze Cheng 已提交
705

H
Hongze Cheng 已提交
706
  code = tsdbSttFileDoWriteBlockData(writer);
H
Hongze Cheng 已提交
707
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
708

H
Hongze Cheng 已提交
709
  code = tsdbSttFileDoWriteStatisBlock(writer);
H
Hongze Cheng 已提交
710 711
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
712
  code = tsdbSttFileDoWriteTombBlock(writer);
H
Hongze Cheng 已提交
713
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
714

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

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

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

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

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

H
Hongze Cheng 已提交
730
  code = tsdbFsyncFile(writer->fd);
H
Hongze Cheng 已提交
731 732
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
733
  tsdbCloseFile(&writer->fd);
H
Hongze Cheng 已提交
734

735 736 737 738 739 740
  ASSERT(writer->file->size > 0);
  STFileOp op = (STFileOp){
      .optype = TSDB_FOP_CREATE,
      .fid = writer->config->file.fid,
      .nf = writer->file[0],
  };
H
Hongze Cheng 已提交
741 742 743

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

H
Hongze Cheng 已提交
745 746
_exit:
  if (code) {
H
Hongze Cheng 已提交
747
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
748
  }
H
Hongze Cheng 已提交
749
  return code;
H
Hongze Cheng 已提交
750
}
H
Hongze Cheng 已提交
751

H
Hongze Cheng 已提交
752
static int32_t tsdbSttFWriterCloseAbort(SSttFileWriter *writer) {
753 754 755 756
  char fname[TSDB_FILENAME_LEN];
  tsdbTFileName(writer->config->tsdb, &writer->config->file, fname);
  tsdbCloseFile(&writer->fd);
  taosRemoveFile(fname);
H
Hongze Cheng 已提交
757 758
  return 0;
}
H
Hongze Cheng 已提交
759

H
Hongze Cheng 已提交
760
int32_t tsdbSttFileWriterOpen(const SSttFileWriterConfig *config, SSttFileWriter **writer) {
761 762 763
  ASSERT(config->file.type == TSDB_FTYPE_STT);
  ASSERT(config->file.size == 0);

H
Hongze Cheng 已提交
764
  writer[0] = taosMemoryCalloc(1, sizeof(*writer[0]));
H
Hongze Cheng 已提交
765 766
  if (writer[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;

H
Hongze Cheng 已提交
767
  writer[0]->config[0] = config[0];
H
Hongze Cheng 已提交
768
  writer[0]->ctx->opened = false;
H
Hongze Cheng 已提交
769 770 771
  return 0;
}

H
Hongze Cheng 已提交
772
int32_t tsdbSttFileWriterClose(SSttFileWriter **writer, int8_t abort, TFileOpArray *opArray) {
H
Hongze Cheng 已提交
773
  int32_t code = 0;
H
Hongze Cheng 已提交
774
  int32_t lino = 0;
H
Hongze Cheng 已提交
775

H
Hongze Cheng 已提交
776
  if (writer[0]->ctx->opened) {
H
Hongze Cheng 已提交
777 778
    if (abort) {
      code = tsdbSttFWriterCloseAbort(writer[0]);
H
Hongze Cheng 已提交
779
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
780
    } else {
H
Hongze Cheng 已提交
781
      code = tsdbSttFWriterCloseCommit(writer[0], opArray);
H
Hongze Cheng 已提交
782
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
783
    }
H
Hongze Cheng 已提交
784
    tsdbSttFWriterDoClose(writer[0]);
H
Hongze Cheng 已提交
785
  }
H
Hongze Cheng 已提交
786 787
  taosMemoryFree(writer[0]);
  writer[0] = NULL;
H
Hongze Cheng 已提交
788 789 790

_exit:
  if (code) {
H
Hongze Cheng 已提交
791
    TSDB_ERROR_LOG(TD_VID(writer[0]->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
792
  }
H
Hongze Cheng 已提交
793
  return code;
H
Hongze Cheng 已提交
794 795
}

H
Hongze Cheng 已提交
796
int32_t tsdbSttFileWriteRow(SSttFileWriter *writer, SRowInfo *row) {
H
Hongze Cheng 已提交
797
  int32_t code = 0;
H
Hongze Cheng 已提交
798
  int32_t lino = 0;
H
Hongze Cheng 已提交
799

H
Hongze Cheng 已提交
800
  if (!writer->ctx->opened) {
H
Hongze Cheng 已提交
801 802 803
    code = tsdbSttFWriterDoOpen(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
804

H
Hongze Cheng 已提交
805
  if (!TABLE_SAME_SCHEMA(row->suid, row->uid, writer->ctx->tbid->suid, writer->ctx->tbid->uid)) {
H
Hongze Cheng 已提交
806
    code = tsdbSttFileDoWriteBlockData(writer);
H
Hongze Cheng 已提交
807
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
808

H
Hongze Cheng 已提交
809 810 811 812
    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 已提交
813
    code = tBlockDataInit(writer->blockData, &id, writer->config->skmTb->pTSchema, NULL, 0);
H
Hongze Cheng 已提交
814 815 816
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
817 818 819 820 821 822 823 824 825
  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 已提交
826 827 828 829
  if (writer->ctx->tbid->uid != row->uid) {
    writer->ctx->tbid->suid = row->suid;
    writer->ctx->tbid->uid = row->uid;

H
Hongze Cheng 已提交
830
    if (STATIS_BLOCK_SIZE(writer->staticBlock) >= writer->config->maxRow) {
H
Hongze Cheng 已提交
831
      code = tsdbSttFileDoWriteStatisBlock(writer);
H
Hongze Cheng 已提交
832
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
833 834
    }

H
Hongze Cheng 已提交
835
    STbStatisRecord record = {
H
Hongze Cheng 已提交
836 837
        .suid = row->suid,
        .uid = row->uid,
H
Hongze Cheng 已提交
838 839 840 841
        .firstKey = key->ts,
        .lastKey = key->ts,
        .minVer = key->version,
        .maxVer = key->version,
H
Hongze Cheng 已提交
842
        .count = 1,
H
Hongze Cheng 已提交
843
    };
H
Hongze Cheng 已提交
844
    code = tStatisBlockPut(writer->staticBlock, &record);
H
Hongze Cheng 已提交
845
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
846
  } else {
H
Hongze Cheng 已提交
847
    ASSERT(key->ts >= TARRAY2_LAST(writer->staticBlock->lastKey));
H
Hongze Cheng 已提交
848

H
Hongze Cheng 已提交
849 850 851 852 853 854 855 856 857
    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 已提交
858
    }
H
Hongze Cheng 已提交
859 860
  }

H
Hongze Cheng 已提交
861
  if (row->row.type == TSDBROW_ROW_FMT) {
H
Hongze Cheng 已提交
862 863
    code = tsdbUpdateSkmRow(writer->config->tsdb, writer->ctx->tbid,  //
                            TSDBROW_SVERSION(&row->row), writer->config->skmRow);
H
Hongze Cheng 已提交
864
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
865 866
  }

H
Hongze Cheng 已提交
867
  // row to col conversion
H
Hongze Cheng 已提交
868 869 870 871 872 873
  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 已提交
874
  ) {
H
Hongze Cheng 已提交
875
    code = tBlockDataUpdateRow(writer->blockData, &row->row, writer->config->skmRow->pTSchema);
H
Hongze Cheng 已提交
876
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
877
  } else {
H
Hongze Cheng 已提交
878
    if (writer->blockData->nRow >= writer->config->maxRow) {
H
Hongze Cheng 已提交
879
      code = tsdbSttFileDoWriteBlockData(writer);
H
Hongze Cheng 已提交
880 881
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
882

H
Hongze Cheng 已提交
883
    code = tBlockDataAppendRow(writer->blockData, &row->row, writer->config->skmRow->pTSchema, row->uid);
H
Hongze Cheng 已提交
884
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
885 886 887 888
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
889
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
890
  }
H
Hongze Cheng 已提交
891
  return code;
H
Hongze Cheng 已提交
892 893
}

H
Hongze Cheng 已提交
894
int32_t tsdbSttFileWriteBlockData(SSttFileWriter *writer, SBlockData *bdata) {
H
Hongze Cheng 已提交
895 896 897
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
898 899
  SRowInfo row[1];
  row->suid = bdata->suid;
H
Hongze Cheng 已提交
900
  for (int32_t i = 0; i < bdata->nRow; i++) {
H
Hongze Cheng 已提交
901 902
    row->uid = bdata->uid ? bdata->uid : bdata->aUid[i];
    row->row = tsdbRowFromBlockData(bdata, i);
H
Hongze Cheng 已提交
903

H
Hongze Cheng 已提交
904
    code = tsdbSttFileWriteRow(writer, row);
H
Hongze Cheng 已提交
905 906 907 908 909
    TSDB_CHECK_CODE(code, lino, _exit);
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
910
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
911
  }
H
Hongze Cheng 已提交
912
  return code;
H
Hongze Cheng 已提交
913 914
}

H
Hongze Cheng 已提交
915
int32_t tsdbSttFileWriteTombRecord(SSttFileWriter *writer, const STombRecord *record) {
H
Hongze Cheng 已提交
916
  int32_t code;
H
Hongze Cheng 已提交
917 918
  int32_t lino;

H
Hongze Cheng 已提交
919
  if (!writer->ctx->opened) {
H
Hongze Cheng 已提交
920 921
    code = tsdbSttFWriterDoOpen(writer);
    return code;
H
Hongze Cheng 已提交
922
  } else {
H
Hongze Cheng 已提交
923
    if (writer->blockData->nRow > 0) {
H
Hongze Cheng 已提交
924
      code = tsdbSttFileDoWriteBlockData(writer);
H
Hongze Cheng 已提交
925 926
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
927

H
Hongze Cheng 已提交
928
    if (STATIS_BLOCK_SIZE(writer->staticBlock) > 0) {
H
Hongze Cheng 已提交
929 930 931
      code = tsdbSttFileDoWriteStatisBlock(writer);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
932
  }
H
Hongze Cheng 已提交
933

H
Hongze Cheng 已提交
934
  code = tTombBlockPut(writer->tombBlock, record);
H
Hongze Cheng 已提交
935
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
936

H
Hongze Cheng 已提交
937
  if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
H
Hongze Cheng 已提交
938
    code = tsdbSttFileDoWriteTombBlock(writer);
H
Hongze Cheng 已提交
939
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
940 941 942 943
  }

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

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