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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

197 198 199 200 201 202 203
  TABLEID tbid = {.suid = sttBlk->suid};
  if (tbid.suid == 0) {
    tbid.uid = sttBlk->minUid;
  } else {
    tbid.uid = 0;
  }

H
Hongze Cheng 已提交
204 205 206 207
  code = tBlockDataInit(bData, &tbid, pTSchema, cids, ncid);
  TSDB_CHECK_CODE(code, lino, _exit);

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

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

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

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

  ASSERT(hdr->delimiter == TSDB_FILE_DLMT);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
520 521
int32_t tsdbFileWriteTombBlock(STsdbFD *fd, STombBlock *tombBlock, int8_t cmprAlg, int64_t *fileSize,
                               TTombBlkArray *tombBlkArray, uint8_t **bufArr) {
H
Hongze Cheng 已提交
522
  int32_t code;
H
Hongze Cheng 已提交
523

H
Hongze Cheng 已提交
524
  if (TOMB_BLOCK_SIZE(tombBlock) == 0) return 0;
H
Hongze Cheng 已提交
525

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

H
Hongze Cheng 已提交
548 549 550
  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 已提交
551
    }
H
Hongze Cheng 已提交
552 553
    if (tombBlk->maxVer < TARRAY2_GET(tombBlock->version, i)) {
      tombBlk->maxVer = TARRAY2_GET(tombBlock->version, i);
H
Hongze Cheng 已提交
554
    }
H
Hongze Cheng 已提交
555 556
  }

H
Hongze Cheng 已提交
557 558 559 560
  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 已提交
561

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

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

H
Hongze Cheng 已提交
569 570 571 572 573 574 575 576 577
  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 已提交
578

H
Hongze Cheng 已提交
579 580 581
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
582 583
  code = tsdbFileWriteTombBlock(writer->fd, writer->tombBlock, writer->config->cmprAlg, &writer->file->size,
                                writer->tombBlkArray, writer->config->bufArr);
H
Hongze Cheng 已提交
584
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
585

H
Hongze Cheng 已提交
586 587
_exit:
  if (code) {
H
Hongze Cheng 已提交
588
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
589
  }
H
Hongze Cheng 已提交
590 591 592
  return code;
}

H
Hongze Cheng 已提交
593
int32_t tsdbFileWriteSttBlk(STsdbFD *fd, const TSttBlkArray *sttBlkArray, SFDataPtr *ptr, int64_t *fileSize) {
H
Hongze Cheng 已提交
594 595 596 597 598 599 600 601 602 603 604 605 606 607
  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 已提交
608
static int32_t tsdbSttFileDoWriteSttBlk(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
609
  int32_t code = 0;
H
Hongze Cheng 已提交
610 611
  int32_t lino;

H
Hongze Cheng 已提交
612
  code = tsdbFileWriteSttBlk(writer->fd, writer->sttBlkArray, writer->footer->sttBlkPtr, &writer->file->size);
H
Hongze Cheng 已提交
613
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
614 615 616

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

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

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

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

H
Hongze Cheng 已提交
642
int32_t tsdbFileWriteTombBlk(STsdbFD *fd, const TTombBlkArray *tombBlkArray, SFDataPtr *ptr, int64_t *fileSize) {
H
Hongze Cheng 已提交
643 644 645 646 647 648 649 650 651 652 653 654 655 656
  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 已提交
657
static int32_t tsdbSttFileDoWriteTombBlk(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
658
  int32_t code = 0;
H
Hongze Cheng 已提交
659
  int32_t lino = 0;
H
Hongze Cheng 已提交
660

H
Hongze Cheng 已提交
661
  code = tsdbFileWriteTombBlk(writer->fd, writer->tombBlkArray, writer->footer->tombBlkPtr, &writer->file->size);
H
Hongze Cheng 已提交
662
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
663 664 665

_exit:
  if (code) {
H
Hongze Cheng 已提交
666
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
667 668 669 670
  }
  return code;
}

H
Hongze Cheng 已提交
671
int32_t tsdbFileWriteSttFooter(STsdbFD *fd, const SSttFooter *footer, int64_t *fileSize) {
H
Hongze Cheng 已提交
672
  int32_t code = tsdbWriteFile(fd, *fileSize, (const uint8_t *)footer, sizeof(*footer));
H
Hongze Cheng 已提交
673
  if (code) return code;
H
Hongze Cheng 已提交
674
  *fileSize += sizeof(*footer);
H
Hongze Cheng 已提交
675
  return 0;
H
Hongze Cheng 已提交
676 677
}

H
Hongze Cheng 已提交
678
static int32_t tsdbSttFileDoWriteFooter(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
679
  return tsdbFileWriteSttFooter(writer->fd, writer->footer, &writer->file->size);
H
Hongze Cheng 已提交
680 681
}

H
Hongze Cheng 已提交
682
static int32_t tsdbSttFWriterDoOpen(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
683
  int32_t code = 0;
H
Hongze Cheng 已提交
684
  int32_t lino = 0;
H
Hongze Cheng 已提交
685

H
Hongze Cheng 已提交
686
  // set
H
Hongze Cheng 已提交
687 688
  if (!writer->config->skmTb) writer->config->skmTb = writer->skmTb;
  if (!writer->config->skmRow) writer->config->skmRow = writer->skmRow;
H
Hongze Cheng 已提交
689
  if (!writer->config->bufArr) writer->config->bufArr = writer->bufArr;
H
Hongze Cheng 已提交
690

H
Hongze Cheng 已提交
691 692 693 694 695 696 697 698 699 700 701
  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,
          },
  };
702

H
Hongze Cheng 已提交
703
  // open file
704
  int32_t flag = TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
H
Hongze Cheng 已提交
705
  char    fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
706

H
Hongze Cheng 已提交
707
  tsdbTFileName(writer->config->tsdb, writer->file, fname);
H
Hongze Cheng 已提交
708
  code = tsdbOpenFile(fname, writer->config->szPage, flag, &writer->fd);
H
Hongze Cheng 已提交
709 710
  TSDB_CHECK_CODE(code, lino, _exit);

711 712 713 714
  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 已提交
715

H
Hongze Cheng 已提交
716 717
  writer->ctx->opened = true;

H
Hongze Cheng 已提交
718
_exit:
H
Hongze Cheng 已提交
719
  if (code) {
H
Hongze Cheng 已提交
720
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
721
  }
H
Hongze Cheng 已提交
722
  return code;
H
Hongze Cheng 已提交
723 724
}

H
Hongze Cheng 已提交
725
static void tsdbSttFWriterDoClose(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
726
  ASSERT(writer->fd == NULL);
H
Hongze Cheng 已提交
727

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

H
Hongze Cheng 已提交
741 742 743 744
static int32_t tsdbSttFileDoUpdateHeader(SSttFileWriter *writer) {
  // TODO
  return 0;
}
H
Hongze Cheng 已提交
745

H
Hongze Cheng 已提交
746
static int32_t tsdbSttFWriterCloseCommit(SSttFileWriter *writer, TFileOpArray *opArray) {
H
Hongze Cheng 已提交
747 748
  int32_t lino;
  int32_t code;
H
Hongze Cheng 已提交
749

H
Hongze Cheng 已提交
750
  code = tsdbSttFileDoWriteBlockData(writer);
H
Hongze Cheng 已提交
751
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
752

H
Hongze Cheng 已提交
753
  code = tsdbSttFileDoWriteStatisBlock(writer);
H
Hongze Cheng 已提交
754 755
  TSDB_CHECK_CODE(code, lino, _exit);

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

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

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

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

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

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

H
Hongze Cheng 已提交
774
  code = tsdbFsyncFile(writer->fd);
H
Hongze Cheng 已提交
775 776
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
777
  tsdbCloseFile(&writer->fd);
H
Hongze Cheng 已提交
778

779 780 781
  ASSERT(writer->file->size > 0);
  STFileOp op = (STFileOp){
      .optype = TSDB_FOP_CREATE,
H
Hongze Cheng 已提交
782
      .fid = writer->config->fid,
783 784
      .nf = writer->file[0],
  };
H
Hongze Cheng 已提交
785 786 787

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

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

H
Hongze Cheng 已提交
796
static int32_t tsdbSttFWriterCloseAbort(SSttFileWriter *writer) {
797
  char fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
798
  tsdbTFileName(writer->config->tsdb, writer->file, fname);
799 800
  tsdbCloseFile(&writer->fd);
  taosRemoveFile(fname);
H
Hongze Cheng 已提交
801 802
  return 0;
}
H
Hongze Cheng 已提交
803

H
Hongze Cheng 已提交
804
int32_t tsdbSttFileWriterOpen(const SSttFileWriterConfig *config, SSttFileWriter **writer) {
H
Hongze Cheng 已提交
805
  writer[0] = taosMemoryCalloc(1, sizeof(*writer[0]));
H
Hongze Cheng 已提交
806 807
  if (writer[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;

H
Hongze Cheng 已提交
808
  writer[0]->config[0] = config[0];
H
Hongze Cheng 已提交
809
  writer[0]->ctx->opened = false;
H
Hongze Cheng 已提交
810 811 812
  return 0;
}

H
Hongze Cheng 已提交
813
int32_t tsdbSttFileWriterClose(SSttFileWriter **writer, int8_t abort, TFileOpArray *opArray) {
H
Hongze Cheng 已提交
814
  int32_t code = 0;
H
Hongze Cheng 已提交
815
  int32_t lino = 0;
H
Hongze Cheng 已提交
816

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

_exit:
  if (code) {
H
Hongze Cheng 已提交
832
    TSDB_ERROR_LOG(TD_VID(writer[0]->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
833
  }
H
Hongze Cheng 已提交
834
  return code;
H
Hongze Cheng 已提交
835 836
}

H
Hongze Cheng 已提交
837
int32_t tsdbSttFileWriteRow(SSttFileWriter *writer, SRowInfo *row) {
H
Hongze Cheng 已提交
838
  int32_t code = 0;
H
Hongze Cheng 已提交
839
  int32_t lino = 0;
H
Hongze Cheng 已提交
840

H
Hongze Cheng 已提交
841
  if (!writer->ctx->opened) {
H
Hongze Cheng 已提交
842 843 844
    code = tsdbSttFWriterDoOpen(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
845

H
Hongze Cheng 已提交
846
  if (!TABLE_SAME_SCHEMA(row->suid, row->uid, writer->ctx->tbid->suid, writer->ctx->tbid->uid)) {
H
Hongze Cheng 已提交
847
    code = tsdbSttFileDoWriteBlockData(writer);
H
Hongze Cheng 已提交
848
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
849

H
Hongze Cheng 已提交
850 851 852 853
    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 已提交
854
    code = tBlockDataInit(writer->blockData, &id, writer->config->skmTb->pTSchema, NULL, 0);
H
Hongze Cheng 已提交
855 856 857
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
858 859 860 861 862 863 864 865 866
  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 已提交
867 868 869 870
  if (writer->ctx->tbid->uid != row->uid) {
    writer->ctx->tbid->suid = row->suid;
    writer->ctx->tbid->uid = row->uid;

H
Hongze Cheng 已提交
871
    if (STATIS_BLOCK_SIZE(writer->staticBlock) >= writer->config->maxRow) {
H
Hongze Cheng 已提交
872
      code = tsdbSttFileDoWriteStatisBlock(writer);
H
Hongze Cheng 已提交
873
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
874 875
    }

H
Hongze Cheng 已提交
876
    STbStatisRecord record = {
H
Hongze Cheng 已提交
877 878
        .suid = row->suid,
        .uid = row->uid,
H
Hongze Cheng 已提交
879 880
        .firstKey = key->ts,
        .lastKey = key->ts,
H
Hongze Cheng 已提交
881
        .count = 1,
H
Hongze Cheng 已提交
882
    };
H
Hongze Cheng 已提交
883
    code = tStatisBlockPut(writer->staticBlock, &record);
H
Hongze Cheng 已提交
884
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
885
  } else {
H
Hongze Cheng 已提交
886
    ASSERT(key->ts >= TARRAY2_LAST(writer->staticBlock->lastKey));
H
Hongze Cheng 已提交
887

H
Hongze Cheng 已提交
888 889 890
    if (key->ts > TARRAY2_LAST(writer->staticBlock->lastKey)) {
      TARRAY2_LAST(writer->staticBlock->count)++;
      TARRAY2_LAST(writer->staticBlock->lastKey) = key->ts;
H
Hongze Cheng 已提交
891
    }
H
Hongze Cheng 已提交
892 893
  }

H
Hongze Cheng 已提交
894
  if (row->row.type == TSDBROW_ROW_FMT) {
H
Hongze Cheng 已提交
895 896
    code = tsdbUpdateSkmRow(writer->config->tsdb, writer->ctx->tbid,  //
                            TSDBROW_SVERSION(&row->row), writer->config->skmRow);
H
Hongze Cheng 已提交
897
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
898 899
  }

H
Hongze Cheng 已提交
900
  // row to col conversion
H
Hongze Cheng 已提交
901 902 903 904 905 906
  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 已提交
907
  ) {
H
Hongze Cheng 已提交
908
    code = tBlockDataUpdateRow(writer->blockData, &row->row, writer->config->skmRow->pTSchema);
H
Hongze Cheng 已提交
909
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
910
  } else {
H
Hongze Cheng 已提交
911
    if (writer->blockData->nRow >= writer->config->maxRow) {
H
Hongze Cheng 已提交
912
      code = tsdbSttFileDoWriteBlockData(writer);
H
Hongze Cheng 已提交
913 914
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
915

H
Hongze Cheng 已提交
916
    code = tBlockDataAppendRow(writer->blockData, &row->row, writer->config->skmRow->pTSchema, row->uid);
H
Hongze Cheng 已提交
917
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
918 919 920 921
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
922
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
923
  }
H
Hongze Cheng 已提交
924
  return code;
H
Hongze Cheng 已提交
925 926
}

H
Hongze Cheng 已提交
927
int32_t tsdbSttFileWriteBlockData(SSttFileWriter *writer, SBlockData *bdata) {
H
Hongze Cheng 已提交
928 929 930
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
931 932
  SRowInfo row[1];
  row->suid = bdata->suid;
H
Hongze Cheng 已提交
933
  for (int32_t i = 0; i < bdata->nRow; i++) {
H
Hongze Cheng 已提交
934 935
    row->uid = bdata->uid ? bdata->uid : bdata->aUid[i];
    row->row = tsdbRowFromBlockData(bdata, i);
H
Hongze Cheng 已提交
936

H
Hongze Cheng 已提交
937
    code = tsdbSttFileWriteRow(writer, row);
H
Hongze Cheng 已提交
938 939 940 941 942
    TSDB_CHECK_CODE(code, lino, _exit);
  }

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

H
Hongze Cheng 已提交
948
int32_t tsdbSttFileWriteTombRecord(SSttFileWriter *writer, const STombRecord *record) {
H
Hongze Cheng 已提交
949
  int32_t code;
H
Hongze Cheng 已提交
950 951
  int32_t lino;

H
Hongze Cheng 已提交
952
  if (!writer->ctx->opened) {
H
Hongze Cheng 已提交
953
    code = tsdbSttFWriterDoOpen(writer);
H
Hongze Cheng 已提交
954
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
955
  } else {
H
Hongze Cheng 已提交
956
    if (writer->blockData->nRow > 0) {
H
Hongze Cheng 已提交
957
      code = tsdbSttFileDoWriteBlockData(writer);
H
Hongze Cheng 已提交
958 959
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
960

H
Hongze Cheng 已提交
961
    if (STATIS_BLOCK_SIZE(writer->staticBlock) > 0) {
H
Hongze Cheng 已提交
962 963 964
      code = tsdbSttFileDoWriteStatisBlock(writer);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
965
  }
H
Hongze Cheng 已提交
966

H
Hongze Cheng 已提交
967
  code = tTombBlockPut(writer->tombBlock, record);
H
Hongze Cheng 已提交
968
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
969

H
Hongze Cheng 已提交
970
  if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
H
Hongze Cheng 已提交
971
    code = tsdbSttFileDoWriteTombBlock(writer);
H
Hongze Cheng 已提交
972
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
973 974 975 976
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
977
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
978 979
  }
  return code;
H
Hongze Cheng 已提交
980 981 982
}

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