tsdbSttFileRW.c 27.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 "inc/tsdbSttFileRW.h"
H
Hongze Cheng 已提交
17

H
Hongze Cheng 已提交
18 19
typedef struct {
  int64_t   prevFooter;
H
Hongze Cheng 已提交
20 21
  SFDataPtr sttBlkPtr[1];
  SFDataPtr statisBlkPtr[1];
H
Hongze Cheng 已提交
22
  SFDataPtr tombBlkPtr[1];
H
Hongze Cheng 已提交
23 24
  SFDataPtr rsrvd[2];
} SSttFooter;
H
Hongze Cheng 已提交
25

H
Hongze Cheng 已提交
26
// SSttFReader ============================================================
H
Hongze Cheng 已提交
27
struct SSttFileReader {
H
Hongze Cheng 已提交
28
  SSttFileReaderConfig config[1];
H
Hongze Cheng 已提交
29
  TSttSegReaderArray   readerArray[1];
H
Hongze Cheng 已提交
30
  STsdbFD             *fd;
H
Hongze Cheng 已提交
31
  uint8_t             *bufArr[5];
H
Hongze Cheng 已提交
32 33 34 35
};

struct SSttSegReader {
  SSttFileReader *reader;
H
Hongze Cheng 已提交
36
  SSttFooter      footer[1];
H
Hongze Cheng 已提交
37 38 39
  struct {
    bool sttBlkLoaded;
    bool statisBlkLoaded;
H
Hongze Cheng 已提交
40
    bool tombBlkLoaded;
H
Hongze Cheng 已提交
41
  } ctx[1];
H
Hongze Cheng 已提交
42 43
  TSttBlkArray    sttBlkArray[1];
  TStatisBlkArray statisBlkArray[1];
H
Hongze Cheng 已提交
44
  TTombBlkArray   tombBlkArray[1];
H
Hongze Cheng 已提交
45 46
};

H
Hongze Cheng 已提交
47
// SSttFileReader
H
Hongze Cheng 已提交
48
static int32_t tsdbSttSegReaderOpen(SSttFileReader *reader, int64_t offset, SSttSegReader **segReader) {
H
Hongze Cheng 已提交
49 50
  ASSERT(offset >= TSDB_FHDR_SIZE);

H
Hongze Cheng 已提交
51
  int32_t code = 0;
H
Hongze Cheng 已提交
52 53 54
  int32_t lino = 0;

  segReader[0] = taosMemoryCalloc(1, sizeof(*segReader[0]));
H
Hongze Cheng 已提交
55
  if (!segReader[0]) return TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
56 57

  segReader[0]->reader = reader;
H
Hongze Cheng 已提交
58
  code = tsdbReadFile(reader->fd, offset, (uint8_t *)(segReader[0]->footer), sizeof(SSttFooter));
H
Hongze Cheng 已提交
59 60 61 62
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
H
Hongze Cheng 已提交
63
    TSDB_ERROR_LOG(TD_VID(reader->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
64 65 66
    taosMemoryFree(segReader[0]);
    segReader[0] = NULL;
  }
H
Hongze Cheng 已提交
67 68 69
  return code;
}

H
Hongze Cheng 已提交
70
static int32_t tsdbSttSegReaderClose(SSttSegReader **reader) {
H
Hongze Cheng 已提交
71
  if (reader[0]) {
H
Hongze Cheng 已提交
72 73 74
    TARRAY2_DESTROY(reader[0]->sttBlkArray, NULL);
    TARRAY2_DESTROY(reader[0]->tombBlkArray, NULL);
    TARRAY2_DESTROY(reader[0]->statisBlkArray, NULL);
H
Hongze Cheng 已提交
75 76
    taosMemoryFree(reader[0]);
    reader[0] = NULL;
H
Hongze Cheng 已提交
77 78
  }
  return 0;
H
Hongze Cheng 已提交
79 80
}

H
Hongze Cheng 已提交
81
int32_t tsdbSttFileReaderOpen(const char *fname, const SSttFileReaderConfig *config, SSttFileReader **reader) {
H
Hongze Cheng 已提交
82
  int32_t code = 0;
H
Hongze Cheng 已提交
83 84 85 86 87
  int32_t lino = 0;

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

H
Hongze Cheng 已提交
88
  reader[0]->config[0] = config[0];
H
Hongze Cheng 已提交
89
  if (!reader[0]->config->bufArr) reader[0]->config->bufArr = reader[0]->bufArr;
H
Hongze Cheng 已提交
90 91 92 93 94 95

  // open file
  code = tsdbOpenFile(fname, config->szPage, TD_FILE_READ, &reader[0]->fd);
  TSDB_CHECK_CODE(code, lino, _exit);

  // open each segment reader
H
Hongze Cheng 已提交
96
  int64_t size = config->file->size;
H
Hongze Cheng 已提交
97
  while (size > 0) {
H
Hongze Cheng 已提交
98
    SSttSegReader *reader1;
H
Hongze Cheng 已提交
99

H
Hongze Cheng 已提交
100
    code = tsdbSttSegReaderOpen(reader[0], size - sizeof(SSttFooter), &reader1);
H
Hongze Cheng 已提交
101 102
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
103
    code = TARRAY2_APPEND(reader[0]->readerArray, reader1);
H
Hongze Cheng 已提交
104 105
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
106
    size = reader1->footer->prevFooter;
H
Hongze Cheng 已提交
107 108
  }

H
Hongze Cheng 已提交
109
  ASSERT(TARRAY2_SIZE(reader[0]->readerArray) == config->file->stt->nseg);
H
Hongze Cheng 已提交
110 111 112

_exit:
  if (code) {
H
Hongze Cheng 已提交
113
    TSDB_ERROR_LOG(TD_VID(config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
114
    tsdbSttFileReaderClose(reader);
H
Hongze Cheng 已提交
115
  }
H
Hongze Cheng 已提交
116 117 118
  return code;
}

H
Hongze Cheng 已提交
119
int32_t tsdbSttFileReaderClose(SSttFileReader **reader) {
H
Hongze Cheng 已提交
120
  if (reader[0]) {
H
Hongze Cheng 已提交
121 122 123
    for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->bufArr); ++i) {
      tFree(reader[0]->bufArr[i]);
    }
H
Hongze Cheng 已提交
124
    tsdbCloseFile(&reader[0]->fd);
H
Hongze Cheng 已提交
125
    TARRAY2_DESTROY(reader[0]->readerArray, tsdbSttSegReaderClose);
H
Hongze Cheng 已提交
126 127 128
    taosMemoryFree(reader[0]);
    reader[0] = NULL;
  }
H
Hongze Cheng 已提交
129
  return 0;
H
Hongze Cheng 已提交
130 131
}

H
Hongze Cheng 已提交
132
int32_t tsdbSttFileReaderGetSegReader(SSttFileReader *reader, const TSttSegReaderArray **readerArray) {
H
Hongze Cheng 已提交
133
  readerArray[0] = reader->readerArray;
H
Hongze Cheng 已提交
134
  return 0;
H
Hongze Cheng 已提交
135 136
}

H
Hongze Cheng 已提交
137
// SSttFSegReader
H
Hongze Cheng 已提交
138
int32_t tsdbSttFileReadStatisBlk(SSttSegReader *reader, const TStatisBlkArray **statisBlkArray) {
H
Hongze Cheng 已提交
139
  if (!reader->ctx->statisBlkLoaded) {
H
Hongze Cheng 已提交
140
    if (reader->footer->statisBlkPtr->size > 0) {
H
Hongze Cheng 已提交
141
      ASSERT(reader->footer->statisBlkPtr->size % sizeof(SStatisBlk) == 0);
H
Hongze Cheng 已提交
142

H
Hongze Cheng 已提交
143
      int32_t size = reader->footer->statisBlkPtr->size / sizeof(SStatisBlk);
H
Hongze Cheng 已提交
144
      void   *data = taosMemoryMalloc(reader->footer->statisBlkPtr->size);
H
Hongze Cheng 已提交
145 146
      if (!data) return TSDB_CODE_OUT_OF_MEMORY;

H
Hongze Cheng 已提交
147 148
      int32_t code = tsdbReadFile(reader->reader->fd, reader->footer->statisBlkPtr->offset, data,
                                  reader->footer->statisBlkPtr->size);
H
Hongze Cheng 已提交
149 150 151 152
      if (code) {
        taosMemoryFree(data);
        return code;
      }
H
Hongze Cheng 已提交
153

H
Hongze Cheng 已提交
154
      TARRAY2_INIT_EX(reader->statisBlkArray, size, size, data);
H
Hongze Cheng 已提交
155
    } else {
H
Hongze Cheng 已提交
156
      TARRAY2_INIT(reader->statisBlkArray);
H
Hongze Cheng 已提交
157 158
    }

H
Hongze Cheng 已提交
159
    reader->ctx->statisBlkLoaded = true;
H
Hongze Cheng 已提交
160 161
  }

H
Hongze Cheng 已提交
162
  statisBlkArray[0] = reader->statisBlkArray;
H
Hongze Cheng 已提交
163
  return 0;
H
Hongze Cheng 已提交
164 165
}

H
Hongze Cheng 已提交
166 167 168 169
int32_t tsdbSttFileReadTombBlk(SSttSegReader *reader, const TTombBlkArray **tombBlkArray) {
  if (!reader->ctx->tombBlkLoaded) {
    if (reader->footer->tombBlkPtr->size > 0) {
      ASSERT(reader->footer->tombBlkPtr->size % sizeof(STombBlk) == 0);
H
Hongze Cheng 已提交
170

H
Hongze Cheng 已提交
171 172
      int32_t size = reader->footer->tombBlkPtr->size / sizeof(STombBlk);
      void   *data = taosMemoryMalloc(reader->footer->tombBlkPtr->size);
H
Hongze Cheng 已提交
173 174
      if (!data) return TSDB_CODE_OUT_OF_MEMORY;

H
Hongze Cheng 已提交
175
      int32_t code =
H
Hongze Cheng 已提交
176
          tsdbReadFile(reader->reader->fd, reader->footer->tombBlkPtr->offset, data, reader->footer->tombBlkPtr->size);
H
Hongze Cheng 已提交
177 178 179 180
      if (code) {
        taosMemoryFree(data);
        return code;
      }
H
Hongze Cheng 已提交
181

H
Hongze Cheng 已提交
182
      TARRAY2_INIT_EX(reader->tombBlkArray, size, size, data);
H
Hongze Cheng 已提交
183
    } else {
H
Hongze Cheng 已提交
184
      TARRAY2_INIT(reader->tombBlkArray);
H
Hongze Cheng 已提交
185 186
    }

H
Hongze Cheng 已提交
187
    reader->ctx->tombBlkLoaded = true;
H
Hongze Cheng 已提交
188 189
  }

H
Hongze Cheng 已提交
190
  tombBlkArray[0] = reader->tombBlkArray;
H
Hongze Cheng 已提交
191 192 193
  return 0;
}

H
Hongze Cheng 已提交
194
int32_t tsdbSttFileReadSttBlk(SSttSegReader *reader, const TSttBlkArray **sttBlkArray) {
H
Hongze Cheng 已提交
195
  if (!reader->ctx->sttBlkLoaded) {
H
Hongze Cheng 已提交
196 197
    if (reader->footer->sttBlkPtr->size > 0) {
      ASSERT(reader->footer->sttBlkPtr->size % sizeof(SSttBlk) == 0);
H
Hongze Cheng 已提交
198

H
Hongze Cheng 已提交
199 200
      int32_t size = reader->footer->sttBlkPtr->size / sizeof(SSttBlk);
      void   *data = taosMemoryMalloc(reader->footer->sttBlkPtr->size);
H
Hongze Cheng 已提交
201 202
      if (!data) return TSDB_CODE_OUT_OF_MEMORY;

H
Hongze Cheng 已提交
203 204
      int32_t code =
          tsdbReadFile(reader->reader->fd, reader->footer->sttBlkPtr->offset, data, reader->footer->sttBlkPtr->size);
H
Hongze Cheng 已提交
205 206 207 208
      if (code) {
        taosMemoryFree(data);
        return code;
      }
H
Hongze Cheng 已提交
209

H
Hongze Cheng 已提交
210
      TARRAY2_INIT_EX(reader->sttBlkArray, size, size, data);
H
Hongze Cheng 已提交
211
    } else {
H
Hongze Cheng 已提交
212
      TARRAY2_INIT(reader->sttBlkArray);
H
Hongze Cheng 已提交
213 214
    }

H
Hongze Cheng 已提交
215
    reader->ctx->sttBlkLoaded = true;
H
Hongze Cheng 已提交
216 217
  }

H
Hongze Cheng 已提交
218
  sttBlkArray[0] = reader->sttBlkArray;
H
Hongze Cheng 已提交
219
  return 0;
H
Hongze Cheng 已提交
220 221
}

H
Hongze Cheng 已提交
222
int32_t tsdbSttFileReadDataBlock(SSttSegReader *reader, const SSttBlk *sttBlk, SBlockData *bData) {
H
Hongze Cheng 已提交
223
  int32_t code = 0;
H
Hongze Cheng 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
  int32_t lino = 0;

  code = tRealloc(&reader->reader->config->bufArr[0], sttBlk->bInfo.szBlock);
  TSDB_CHECK_CODE(code, lino, _exit);

  code =
      tsdbReadFile(reader->reader->fd, sttBlk->bInfo.offset, reader->reader->config->bufArr[0], sttBlk->bInfo.szBlock);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tDecmprBlockData(reader->reader->config->bufArr[0], sttBlk->bInfo.szBlock, bData,
                          &reader->reader->config->bufArr[1]);
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(reader->reader->config->tsdb->pVnode), lino, code);
  }
H
Hongze Cheng 已提交
241 242 243
  return code;
}

H
Hongze Cheng 已提交
244
int32_t tsdbSttFileReadTombBlock(SSttSegReader *reader, const STombBlk *tombBlk, STombBlock *dData) {
H
Hongze Cheng 已提交
245
  int32_t code = 0;
H
Hongze Cheng 已提交
246 247
  int32_t lino = 0;

H
Hongze Cheng 已提交
248
  tTombBlockClear(dData);
H
Hongze Cheng 已提交
249

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

H
Hongze Cheng 已提交
253
  code = tsdbReadFile(reader->reader->fd, tombBlk->dp->offset, reader->reader->config->bufArr[0], tombBlk->dp->size);
H
Hongze Cheng 已提交
254 255 256
  if (code) TSDB_CHECK_CODE(code, lino, _exit);

  int64_t size = 0;
H
Hongze Cheng 已提交
257
  for (int32_t i = 0; i < ARRAY_SIZE(dData->dataArr); ++i) {
H
Hongze Cheng 已提交
258 259
    code = tsdbDecmprData(reader->reader->config->bufArr[0] + size, tombBlk->size[i], TSDB_DATA_TYPE_BIGINT,
                          TWO_STAGE_COMP, &reader->reader->config->bufArr[1], sizeof(int64_t) * tombBlk->numRec,
H
Hongze Cheng 已提交
260
                          &reader->reader->config->bufArr[2]);
H
Hongze Cheng 已提交
261 262
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
263
    for (int32_t j = 0; j < tombBlk->numRec; ++j) {
H
Hongze Cheng 已提交
264 265 266 267
      code = TARRAY2_APPEND(&dData->dataArr[i], ((int64_t *)(reader->reader->config->bufArr[1]))[j]);
      continue;
    }

H
Hongze Cheng 已提交
268
    size += tombBlk->size[i];
H
Hongze Cheng 已提交
269 270
  }

H
Hongze Cheng 已提交
271
  ASSERT(size == tombBlk->dp->size);
H
Hongze Cheng 已提交
272 273
_exit:
  if (code) {
H
Hongze Cheng 已提交
274
    TSDB_ERROR_LOG(TD_VID(reader->reader->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
275
  }
H
Hongze Cheng 已提交
276 277 278
  return code;
}

H
Hongze Cheng 已提交
279
int32_t tsdbSttFileReadStatisBlock(SSttSegReader *reader, const SStatisBlk *statisBlk, STbStatisBlock *sData) {
H
Hongze Cheng 已提交
280
  int32_t code = 0;
H
Hongze Cheng 已提交
281 282 283
  int32_t lino = 0;

  tStatisBlockClear(sData);
H
Hongze Cheng 已提交
284 285

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

H
Hongze Cheng 已提交
288 289
  code =
      tsdbReadFile(reader->reader->fd, statisBlk->dp->offset, reader->reader->config->bufArr[0], statisBlk->dp->size);
H
Hongze Cheng 已提交
290 291 292
  if (code) TSDB_CHECK_CODE(code, lino, _exit);

  int64_t size = 0;
H
Hongze Cheng 已提交
293
  for (int32_t i = 0; i < ARRAY_SIZE(sData->dataArr); ++i) {
H
Hongze Cheng 已提交
294 295 296
    code = tsdbDecmprData(reader->reader->config->bufArr[0] + size, statisBlk->size[i], TSDB_DATA_TYPE_BIGINT,
                          TWO_STAGE_COMP, &reader->reader->config->bufArr[1], sizeof(int64_t) * statisBlk->numRec,
                          &reader->reader->config->bufArr[2]);
H
Hongze Cheng 已提交
297 298
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
299 300 301 302 303
    for (int32_t j = 0; j < statisBlk->numRec; ++j) {
      code = TARRAY2_APPEND(sData->dataArr + i, ((int64_t *)reader->reader->config->bufArr[1])[j]);
      TSDB_CHECK_CODE(code, lino, _exit);
    }

H
Hongze Cheng 已提交
304 305 306 307 308 309 310
    size += statisBlk->size[i];
  }

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

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

H
Hongze Cheng 已提交
316
// SSttFWriter ============================================================
H
Hongze Cheng 已提交
317
struct SSttFileWriter {
H
Hongze Cheng 已提交
318
  SSttFileWriterConfig config[1];
H
Hongze Cheng 已提交
319
  struct {
H
Hongze Cheng 已提交
320 321
    bool    opened;
    TABLEID tbid[1];
H
Hongze Cheng 已提交
322
  } ctx[1];
H
Hongze Cheng 已提交
323
  // file
H
Hongze Cheng 已提交
324
  STFile file[1];
H
Hongze Cheng 已提交
325
  // data
H
Hongze Cheng 已提交
326 327
  TSttBlkArray    sttBlkArray[1];
  TStatisBlkArray statisBlkArray[1];
H
Hongze Cheng 已提交
328
  TTombBlkArray   tombBlkArray[1];
H
Hongze Cheng 已提交
329
  SSttFooter      footer[1];
H
Hongze Cheng 已提交
330 331
  SBlockData      bData[1];
  STbStatisBlock  sData[1];
H
Hongze Cheng 已提交
332
  STombBlock      tData[1];
H
Hongze Cheng 已提交
333
  // helper data
H
Hongze Cheng 已提交
334 335
  SSkmInfo skmTb[1];
  SSkmInfo skmRow[1];
H
Hongze Cheng 已提交
336 337
  int32_t  sizeArr[5];
  uint8_t *bufArr[5];
H
Hongze Cheng 已提交
338
  STsdbFD *fd;
H
Hongze Cheng 已提交
339
};
H
Hongze Cheng 已提交
340

H
Hongze Cheng 已提交
341 342 343
static int32_t tsdbSttFileDoWriteTSDataBlock(SSttFileWriter *writer) {
  if (writer->bData->nRow == 0) return 0;

H
Hongze Cheng 已提交
344
  int32_t code = 0;
H
Hongze Cheng 已提交
345
  int32_t lino = 0;
H
Hongze Cheng 已提交
346 347 348 349 350 351 352 353 354 355 356 357

  SSttBlk sttBlk[1] = {{
      .suid = writer->bData->suid,
      .minUid = writer->bData->uid ? writer->bData->uid : writer->bData->aUid[0],
      .maxUid = writer->bData->uid ? writer->bData->uid : writer->bData->aUid[writer->bData->nRow - 1],
      .minKey = writer->bData->aTSKEY[0],
      .maxKey = writer->bData->aTSKEY[0],
      .minVer = writer->bData->aVersion[0],
      .maxVer = writer->bData->aVersion[0],
      .nRow = writer->bData->nRow,
  }};

H
Hongze Cheng 已提交
358 359 360 361 362 363 364
  for (int32_t iRow = 1; iRow < writer->bData->nRow; iRow++) {
    if (sttBlk->minKey > writer->bData->aTSKEY[iRow]) sttBlk->minKey = writer->bData->aTSKEY[iRow];
    if (sttBlk->maxKey < writer->bData->aTSKEY[iRow]) sttBlk->maxKey = writer->bData->aTSKEY[iRow];
    if (sttBlk->minVer > writer->bData->aVersion[iRow]) sttBlk->minVer = writer->bData->aVersion[iRow];
    if (sttBlk->maxVer < writer->bData->aVersion[iRow]) sttBlk->maxVer = writer->bData->aVersion[iRow];
  }

H
Hongze Cheng 已提交
365
  code = tCmprBlockData(writer->bData, writer->config->cmprAlg, NULL, NULL, writer->config->bufArr, writer->sizeArr);
H
Hongze Cheng 已提交
366
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
367

H
Hongze Cheng 已提交
368
  sttBlk->bInfo.offset = writer->file->size;
H
Hongze Cheng 已提交
369 370
  sttBlk->bInfo.szKey = writer->sizeArr[2] + writer->sizeArr[3];
  sttBlk->bInfo.szBlock = writer->sizeArr[0] + writer->sizeArr[1] + sttBlk->bInfo.szKey;
H
Hongze Cheng 已提交
371

H
Hongze Cheng 已提交
372
  for (int32_t i = 3; i >= 0; i--) {
H
Hongze Cheng 已提交
373
    if (writer->sizeArr[i]) {
H
Hongze Cheng 已提交
374
      code = tsdbWriteFile(writer->fd, writer->file->size, writer->config->bufArr[i], writer->sizeArr[i]);
H
Hongze Cheng 已提交
375
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
376
      writer->file->size += writer->sizeArr[i];
H
Hongze Cheng 已提交
377 378 379
    }
  }

H
Hongze Cheng 已提交
380
  code = TARRAY2_APPEND_PTR(writer->sttBlkArray, sttBlk);
H
Hongze Cheng 已提交
381
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
382

H
Hongze Cheng 已提交
383 384
  tBlockDataClear(writer->bData);

H
Hongze Cheng 已提交
385 386
_exit:
  if (code) {
H
Hongze Cheng 已提交
387
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
388
  }
H
Hongze Cheng 已提交
389 390 391
  return code;
}

H
Hongze Cheng 已提交
392
static int32_t tsdbSttFileDoWriteStatisBlock(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
393
  if (STATIS_BLOCK_SIZE(writer->sData)) return 0;
H
Hongze Cheng 已提交
394

H
Hongze Cheng 已提交
395
  int32_t code = 0;
H
Hongze Cheng 已提交
396
  int32_t lino = 0;
H
Hongze Cheng 已提交
397

H
Hongze Cheng 已提交
398
  SStatisBlk statisBlk[1] = {{
H
Hongze Cheng 已提交
399
      .numRec = STATIS_BLOCK_SIZE(writer->sData),
H
Hongze Cheng 已提交
400 401 402 403 404 405 406 407 408 409
      .minTbid =
          {
              .suid = TARRAY2_FIRST(writer->sData->suid),
              .uid = TARRAY2_FIRST(writer->sData->uid),
          },
      .maxTbid =
          {
              .suid = TARRAY2_LAST(writer->sData->suid),
              .uid = TARRAY2_LAST(writer->sData->uid),
          },
H
Hongze Cheng 已提交
410 411
      .minVer = TARRAY2_FIRST(writer->sData->minVer),
      .maxVer = TARRAY2_FIRST(writer->sData->maxVer),
H
Hongze Cheng 已提交
412 413
  }};

H
Hongze Cheng 已提交
414 415 416 417
  for (int32_t i = 1; i < STATIS_BLOCK_SIZE(writer->sData); i++) {
    statisBlk->minVer = TMIN(statisBlk->minVer, TARRAY2_GET(writer->sData->minVer, i));
    statisBlk->maxVer = TMAX(statisBlk->maxVer, TARRAY2_GET(writer->sData->maxVer, i));
  }
H
Hongze Cheng 已提交
418

H
Hongze Cheng 已提交
419 420
  statisBlk->dp->offset = writer->file->size;
  statisBlk->dp->size = 0;
H
Hongze Cheng 已提交
421

H
Hongze Cheng 已提交
422
  for (int32_t i = 0; i < STATIS_RECORD_NUM_ELEM; i++) {
H
Hongze Cheng 已提交
423
    int32_t size;
H
Hongze Cheng 已提交
424
    code = tsdbCmprData((uint8_t *)TARRAY2_DATA(writer->sData->dataArr + i),
H
Hongze Cheng 已提交
425
                        TARRAY2_DATA_LEN(&writer->sData->dataArr[i]), TSDB_DATA_TYPE_BIGINT, TWO_STAGE_COMP,
H
Hongze Cheng 已提交
426
                        &writer->config->bufArr[0], 0, &size, &writer->config->bufArr[1]);
H
Hongze Cheng 已提交
427
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
428

H
Hongze Cheng 已提交
429
    code = tsdbWriteFile(writer->fd, writer->file->size, writer->config->bufArr[0], size);
H
Hongze Cheng 已提交
430 431
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
432 433
    statisBlk->size[i] = size;
    statisBlk->dp->size += size;
H
Hongze Cheng 已提交
434
    writer->file->size += size;
H
Hongze Cheng 已提交
435
  }
H
Hongze Cheng 已提交
436

H
Hongze Cheng 已提交
437
  code = TARRAY2_APPEND_PTR(writer->statisBlkArray, statisBlk);
H
Hongze Cheng 已提交
438
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
439

H
Hongze Cheng 已提交
440 441
  tStatisBlockClear(writer->sData);

H
Hongze Cheng 已提交
442 443
_exit:
  if (code) {
H
Hongze Cheng 已提交
444
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
445
  }
H
Hongze Cheng 已提交
446 447 448
  return code;
}

H
Hongze Cheng 已提交
449
static int32_t tsdbSttFileDoWriteTombBlock(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
450
  if (TOMB_BLOCK_SIZE(writer->tData) == 0) return 0;
H
Hongze Cheng 已提交
451

H
Hongze Cheng 已提交
452
  int32_t code = 0;
H
Hongze Cheng 已提交
453
  int32_t lino = 0;
H
Hongze Cheng 已提交
454

H
Hongze Cheng 已提交
455
  STombBlk tombBlk[1] = {{
H
Hongze Cheng 已提交
456
      .numRec = TOMB_BLOCK_SIZE(writer->tData),
H
Hongze Cheng 已提交
457
      .minTbid =
H
Hongze Cheng 已提交
458
          {
H
Hongze Cheng 已提交
459 460
              .suid = TARRAY2_FIRST(writer->tData->suid),
              .uid = TARRAY2_FIRST(writer->tData->uid),
H
Hongze Cheng 已提交
461
          },
H
Hongze Cheng 已提交
462
      .maxTbid =
H
Hongze Cheng 已提交
463
          {
H
Hongze Cheng 已提交
464 465
              .suid = TARRAY2_LAST(writer->tData->suid),
              .uid = TARRAY2_LAST(writer->tData->uid),
H
Hongze Cheng 已提交
466
          },
H
Hongze Cheng 已提交
467 468
      .minVer = TARRAY2_FIRST(writer->tData->version),
      .maxVer = TARRAY2_FIRST(writer->tData->version),
H
Hongze Cheng 已提交
469 470 471 472 473 474
      .dp[0] =
          {
              .offset = writer->file->size,
              .size = 0,
          },
  }};
H
Hongze Cheng 已提交
475

H
Hongze Cheng 已提交
476 477 478
  for (int32_t i = 1; i < TOMB_BLOCK_SIZE(writer->tData); i++) {
    tombBlk->minVer = TMIN(tombBlk->minVer, TARRAY2_GET(writer->tData->version, i));
    tombBlk->maxVer = TMAX(tombBlk->maxVer, TARRAY2_GET(writer->tData->version, i));
H
Hongze Cheng 已提交
479 480
  }

H
Hongze Cheng 已提交
481
  for (int32_t i = 0; i < ARRAY_SIZE(writer->tData->dataArr); i++) {
H
Hongze Cheng 已提交
482
    int32_t size;
H
Hongze Cheng 已提交
483 484
    code = tsdbCmprData((uint8_t *)TARRAY2_DATA(&writer->tData->dataArr[i]),
                        TARRAY2_DATA_LEN(&writer->tData->dataArr[i]), TSDB_DATA_TYPE_BIGINT, TWO_STAGE_COMP,
H
Hongze Cheng 已提交
485
                        &writer->config->bufArr[0], 0, &size, &writer->config->bufArr[1]);
H
Hongze Cheng 已提交
486
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
487

H
Hongze Cheng 已提交
488
    code = tsdbWriteFile(writer->fd, writer->file->size, writer->config->bufArr[0], size);
H
Hongze Cheng 已提交
489 490
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
491
    tombBlk->size[i] = size;
H
Hongze Cheng 已提交
492
    tombBlk->dp->size += size;
H
Hongze Cheng 已提交
493
    writer->file->size += size;
H
Hongze Cheng 已提交
494 495
  }

H
Hongze Cheng 已提交
496
  code = TARRAY2_APPEND_PTR(writer->tombBlkArray, tombBlk);
H
Hongze Cheng 已提交
497
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
498

H
Hongze Cheng 已提交
499
  tTombBlockClear(writer->tData);
H
Hongze Cheng 已提交
500

H
Hongze Cheng 已提交
501 502
_exit:
  if (code) {
H
Hongze Cheng 已提交
503
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
504
  }
H
Hongze Cheng 已提交
505 506 507
  return code;
}

H
Hongze Cheng 已提交
508
static int32_t tsdbSttFileDoWriteSttBlk(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
509
  int32_t code = 0;
H
Hongze Cheng 已提交
510 511
  int32_t lino;

H
Hongze Cheng 已提交
512
  writer->footer->sttBlkPtr->offset = writer->file->size;
H
Hongze Cheng 已提交
513
  writer->footer->sttBlkPtr->size = TARRAY2_DATA_LEN(writer->sttBlkArray);
H
Hongze Cheng 已提交
514

H
Hongze Cheng 已提交
515
  if (writer->footer->sttBlkPtr->size) {
H
Hongze Cheng 已提交
516
    code = tsdbWriteFile(writer->fd, writer->file->size, (const uint8_t *)TARRAY2_DATA(writer->sttBlkArray),
H
Hongze Cheng 已提交
517
                         writer->footer->sttBlkPtr->size);
H
Hongze Cheng 已提交
518
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
519
    writer->file->size += writer->footer->sttBlkPtr->size;
H
Hongze Cheng 已提交
520 521 522 523
  }

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

H
Hongze Cheng 已提交
529
static int32_t tsdbSttFileDoWriteStatisBlk(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
530
  int32_t code = 0;
H
Hongze Cheng 已提交
531
  int32_t lino;
H
Hongze Cheng 已提交
532

H
Hongze Cheng 已提交
533
  writer->footer->statisBlkPtr->offset = writer->file->size;
H
Hongze Cheng 已提交
534
  writer->footer->statisBlkPtr->size = TARRAY2_DATA_LEN(writer->statisBlkArray);
H
Hongze Cheng 已提交
535

H
Hongze Cheng 已提交
536
  if (writer->footer->statisBlkPtr->size) {
H
Hongze Cheng 已提交
537
    code = tsdbWriteFile(writer->fd, writer->file->size, (const uint8_t *)TARRAY2_DATA(writer->statisBlkArray),
H
Hongze Cheng 已提交
538
                         writer->footer->statisBlkPtr->size);
H
Hongze Cheng 已提交
539
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
540
    writer->file->size += writer->footer->statisBlkPtr->size;
H
Hongze Cheng 已提交
541 542 543 544
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
545
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
546
  }
H
Hongze Cheng 已提交
547 548 549
  return code;
}

H
Hongze Cheng 已提交
550
static int32_t tsdbSttFileDoWriteTombBlk(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
551
  int32_t code = 0;
H
Hongze Cheng 已提交
552
  int32_t lino = 0;
H
Hongze Cheng 已提交
553

H
Hongze Cheng 已提交
554 555
  writer->footer->tombBlkPtr->offset = writer->file->size;
  writer->footer->tombBlkPtr->size = TARRAY2_DATA_LEN(writer->tombBlkArray);
H
Hongze Cheng 已提交
556

H
Hongze Cheng 已提交
557 558 559
  if (writer->footer->tombBlkPtr->size) {
    code = tsdbWriteFile(writer->fd, writer->file->size, (const uint8_t *)TARRAY2_DATA(writer->tombBlkArray),
                         writer->footer->tombBlkPtr->size);
H
Hongze Cheng 已提交
560
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
561
    writer->file->size += writer->footer->tombBlkPtr->size;
H
Hongze Cheng 已提交
562 563 564 565
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
566
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
567 568 569 570
  }
  return code;
}

H
Hongze Cheng 已提交
571
static int32_t tsdbSttFileDoWriteFooter(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
572 573
  writer->footer->prevFooter = writer->config->file.size;
  int32_t code = tsdbWriteFile(writer->fd, writer->file->size, (const uint8_t *)writer->footer, sizeof(writer->footer));
H
Hongze Cheng 已提交
574
  if (code) return code;
H
Hongze Cheng 已提交
575
  writer->file->size += sizeof(writer->footer);
H
Hongze Cheng 已提交
576
  return 0;
H
Hongze Cheng 已提交
577 578
}

H
Hongze Cheng 已提交
579
static int32_t tsdbSttFWriterDoOpen(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
580
  int32_t code = 0;
H
Hongze Cheng 已提交
581
  int32_t lino = 0;
H
Hongze Cheng 已提交
582

H
Hongze Cheng 已提交
583
  // set
H
Hongze Cheng 已提交
584 585 586 587
  writer->file[0] = writer->config->file;
  writer->file->stt->nseg++;
  if (!writer->config->skmTb) writer->config->skmTb = writer->skmTb;
  if (!writer->config->skmRow) writer->config->skmRow = writer->skmRow;
H
Hongze Cheng 已提交
588
  if (!writer->config->bufArr) writer->config->bufArr = writer->bufArr;
H
Hongze Cheng 已提交
589

H
Hongze Cheng 已提交
590 591 592
  // open file
  int32_t flag;
  char    fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
593

H
Hongze Cheng 已提交
594
  if (writer->file->size) {
H
Hongze Cheng 已提交
595 596 597
    flag = TD_FILE_READ | TD_FILE_WRITE;
  } else {
    flag = TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
H
Hongze Cheng 已提交
598
  }
H
Hongze Cheng 已提交
599

H
Hongze Cheng 已提交
600
  tsdbTFileName(writer->config->tsdb, writer->file, fname);
H
Hongze Cheng 已提交
601
  code = tsdbOpenFile(fname, writer->config->szPage, flag, &writer->fd);
H
Hongze Cheng 已提交
602 603
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
604
  if (!writer->file->size) {
H
Hongze Cheng 已提交
605 606 607
    uint8_t hdr[TSDB_FHDR_SIZE] = {0};
    code = tsdbWriteFile(writer->fd, 0, hdr, sizeof(hdr));
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
608
    writer->file->size += sizeof(hdr);
H
Hongze Cheng 已提交
609
  }
H
Hongze Cheng 已提交
610

H
Hongze Cheng 已提交
611 612
  writer->ctx->opened = true;

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

H
Hongze Cheng 已提交
620 621
static void tsdbSttFWriterDoClose(SSttFileWriter *writer) {
  ASSERT(!writer->fd);
H
Hongze Cheng 已提交
622

H
Hongze Cheng 已提交
623 624
  for (int32_t i = 0; i < ARRAY_SIZE(writer->sizeArr); ++i) {
    tFree(writer->bufArr[i]);
H
Hongze Cheng 已提交
625 626 627
  }
  tDestroyTSchema(writer->skmRow->pTSchema);
  tDestroyTSchema(writer->skmTb->pTSchema);
H
Hongze Cheng 已提交
628
  tTombBlockDestroy(writer->tData);
H
Hongze Cheng 已提交
629
  tStatisBlockDestroy(writer->sData);
H
Hongze Cheng 已提交
630
  tBlockDataDestroy(writer->bData);
H
Hongze Cheng 已提交
631 632 633
  TARRAY2_DESTROY(writer->tombBlkArray, NULL);
  TARRAY2_DESTROY(writer->statisBlkArray, NULL);
  TARRAY2_DESTROY(writer->sttBlkArray, NULL);
H
Hongze Cheng 已提交
634
}
H
Hongze Cheng 已提交
635

H
Hongze Cheng 已提交
636 637 638 639
static int32_t tsdbSttFileDoUpdateHeader(SSttFileWriter *writer) {
  // TODO
  return 0;
}
H
Hongze Cheng 已提交
640

H
Hongze Cheng 已提交
641
static int32_t tsdbSttFWriterCloseCommit(SSttFileWriter *writer, TFileOpArray *opArray) {
H
Hongze Cheng 已提交
642 643
  int32_t lino;
  int32_t code;
H
Hongze Cheng 已提交
644

H
Hongze Cheng 已提交
645 646
  code = tsdbSttFileDoWriteTSDataBlock(writer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
647

H
Hongze Cheng 已提交
648
  code = tsdbSttFileDoWriteStatisBlock(writer);
H
Hongze Cheng 已提交
649 650
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
651
  code = tsdbSttFileDoWriteTombBlock(writer);
H
Hongze Cheng 已提交
652
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
653

H
Hongze Cheng 已提交
654 655
  code = tsdbSttFileDoWriteSttBlk(writer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
656

H
Hongze Cheng 已提交
657 658
  code = tsdbSttFileDoWriteStatisBlk(writer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
659

H
Hongze Cheng 已提交
660
  code = tsdbSttFileDoWriteTombBlk(writer);
H
Hongze Cheng 已提交
661
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
662

H
Hongze Cheng 已提交
663 664
  code = tsdbSttFileDoWriteFooter(writer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
665

H
Hongze Cheng 已提交
666
  code = tsdbSttFileDoUpdateHeader(writer);
H
Hongze Cheng 已提交
667 668
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
669
  code = tsdbFsyncFile(writer->fd);
H
Hongze Cheng 已提交
670 671
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
672
  tsdbCloseFile(&writer->fd);
H
Hongze Cheng 已提交
673

H
Hongze Cheng 已提交
674
  ASSERT(writer->config->file.size < writer->file->size);
H
Hongze Cheng 已提交
675 676 677 678 679 680 681 682 683
  STFileOp op = {
      .optype = writer->config->file.size ? TSDB_FOP_MODIFY : TSDB_FOP_CREATE,
      .fid = writer->config->file.fid,
      .of = writer->config->file,
      .nf = writer->file[0],
  };

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

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

H
Hongze Cheng 已提交
692
static int32_t tsdbSttFWriterCloseAbort(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
693
  if (writer->config->file.size) {  // truncate the file to the original size
H
Hongze Cheng 已提交
694 695
    ASSERT(writer->config->file.size <= writer->file->size);
    if (writer->config->file.size < writer->file->size) {
H
Hongze Cheng 已提交
696
      taosFtruncateFile(writer->fd->pFD, writer->config->file.size);
H
Hongze Cheng 已提交
697 698 699
      tsdbCloseFile(&writer->fd);
    }
  } else {  // remove the file
H
Hongze Cheng 已提交
700 701
    char fname[TSDB_FILENAME_LEN];
    tsdbTFileName(writer->config->tsdb, &writer->config->file, fname);
H
Hongze Cheng 已提交
702 703 704 705
    tsdbCloseFile(&writer->fd);
    taosRemoveFile(fname);
  }

H
Hongze Cheng 已提交
706 707
  return 0;
}
H
Hongze Cheng 已提交
708

H
Hongze Cheng 已提交
709
int32_t tsdbSttFileWriterOpen(const SSttFileWriterConfig *config, SSttFileWriter **writer) {
H
Hongze Cheng 已提交
710
  writer[0] = taosMemoryCalloc(1, sizeof(*writer[0]));
H
Hongze Cheng 已提交
711 712
  if (writer[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;

H
Hongze Cheng 已提交
713
  writer[0]->config[0] = config[0];
H
Hongze Cheng 已提交
714
  writer[0]->ctx->opened = false;
H
Hongze Cheng 已提交
715 716 717
  return 0;
}

H
Hongze Cheng 已提交
718
int32_t tsdbSttFileWriterClose(SSttFileWriter **writer, int8_t abort, TFileOpArray *opArray) {
H
Hongze Cheng 已提交
719
  int32_t code = 0;
H
Hongze Cheng 已提交
720
  int32_t lino = 0;
H
Hongze Cheng 已提交
721

H
Hongze Cheng 已提交
722
  if (writer[0]->ctx->opened) {
H
Hongze Cheng 已提交
723 724
    if (abort) {
      code = tsdbSttFWriterCloseAbort(writer[0]);
H
Hongze Cheng 已提交
725
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
726
    } else {
H
Hongze Cheng 已提交
727
      code = tsdbSttFWriterCloseCommit(writer[0], opArray);
H
Hongze Cheng 已提交
728
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
729
    }
H
Hongze Cheng 已提交
730
    tsdbSttFWriterDoClose(writer[0]);
H
Hongze Cheng 已提交
731
  }
H
Hongze Cheng 已提交
732 733
  taosMemoryFree(writer[0]);
  writer[0] = NULL;
H
Hongze Cheng 已提交
734 735 736

_exit:
  if (code) {
H
Hongze Cheng 已提交
737
    TSDB_ERROR_LOG(TD_VID(writer[0]->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
738
  }
H
Hongze Cheng 已提交
739
  return code;
H
Hongze Cheng 已提交
740 741
}

H
Hongze Cheng 已提交
742
int32_t tsdbSttFileWriteTSData(SSttFileWriter *writer, SRowInfo *row) {
H
Hongze Cheng 已提交
743
  int32_t code = 0;
H
Hongze Cheng 已提交
744
  int32_t lino = 0;
H
Hongze Cheng 已提交
745

H
Hongze Cheng 已提交
746
  if (!writer->ctx->opened) {
H
Hongze Cheng 已提交
747 748 749
    code = tsdbSttFWriterDoOpen(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
750

H
Hongze Cheng 已提交
751 752
  TSDBKEY key[1] = {TSDBROW_KEY(&row->row)};
  if (!TABLE_SAME_SCHEMA(row->suid, row->uid, writer->ctx->tbid->suid, writer->ctx->tbid->uid)) {
H
Hongze Cheng 已提交
753 754
    code = tsdbSttFileDoWriteTSDataBlock(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
755

H
Hongze Cheng 已提交
756 757 758 759 760 761 762 763 764 765 766 767
    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};
    code = tBlockDataInit(writer->bData, &id, writer->config->skmTb->pTSchema, NULL, 0);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  if (writer->ctx->tbid->uid != row->uid) {
    writer->ctx->tbid->suid = row->suid;
    writer->ctx->tbid->uid = row->uid;

H
Hongze Cheng 已提交
768
    if (STATIS_BLOCK_SIZE(writer->sData) >= writer->config->maxRow) {
H
Hongze Cheng 已提交
769
      code = tsdbSttFileDoWriteStatisBlock(writer);
H
Hongze Cheng 已提交
770
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
771 772
    }

H
Hongze Cheng 已提交
773 774 775
    STbStatisRecord record[1] = {{
        .suid = row->suid,
        .uid = row->uid,
H
Hongze Cheng 已提交
776 777 778 779
        .firstKey = key->ts,
        .lastKey = key->ts,
        .minVer = key->version,
        .maxVer = key->version,
H
Hongze Cheng 已提交
780 781 782 783
        .count = 1,
    }};
    code = tStatisBlockPut(writer->sData, record);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
784 785 786 787 788 789 790 791 792 793
  } else {
    TARRAY2_LAST(writer->sData->minVer) = TMIN(TARRAY2_LAST(writer->sData->minVer), key->version);
    TARRAY2_LAST(writer->sData->maxVer) = TMAX(TARRAY2_LAST(writer->sData->maxVer), key->version);
    if (key->ts > TARRAY2_LAST(writer->sData->lastKey)) {
      TARRAY2_LAST(writer->sData->count)++;
      TARRAY2_LAST(writer->sData->lastKey) = key->ts;
    } else if (key->ts == TARRAY2_LAST(writer->sData->lastKey)) {
    } else {
      ASSERTS(0, "timestamp should be in ascending order");
    }
H
Hongze Cheng 已提交
794 795
  }

H
Hongze Cheng 已提交
796
  if (row->row.type == TSDBROW_ROW_FMT) {
H
Hongze Cheng 已提交
797 798
    code = tsdbUpdateSkmRow(writer->config->tsdb, writer->ctx->tbid,  //
                            TSDBROW_SVERSION(&row->row), writer->config->skmRow);
H
Hongze Cheng 已提交
799
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
800 801
  }

H
Hongze Cheng 已提交
802
  // row to col conversion
H
Hongze Cheng 已提交
803 804 805 806 807 808 809 810 811
  if (key->version <= writer->config->compactVersion                       //
      && writer->bData->nRow > 0                                           //
      && (writer->bData->uid                                               //
              ? writer->bData->uid                                         //
              : writer->bData->aUid[writer->bData->nRow - 1]) == row->uid  //
      && writer->bData->aTSKEY[writer->bData->nRow - 1] == key->ts         //
  ) {
    code = tBlockDataUpdateRow(writer->bData, &row->row, writer->config->skmRow->pTSchema);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
812 813 814 815 816
  } else {
    if (writer->bData->nRow >= writer->config->maxRow) {
      code = tsdbSttFileDoWriteTSDataBlock(writer);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
817

H
Hongze Cheng 已提交
818
    code = tBlockDataAppendRow(writer->bData, &row->row, writer->config->skmRow->pTSchema, row->uid);
H
Hongze Cheng 已提交
819
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
820 821 822 823
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
824
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
825
  }
H
Hongze Cheng 已提交
826
  return code;
H
Hongze Cheng 已提交
827 828
}

H
Hongze Cheng 已提交
829
int32_t tsdbSttFileWriteTSDataBlock(SSttFileWriter *writer, SBlockData *bdata) {
H
Hongze Cheng 已提交
830 831 832
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
833 834
  SRowInfo row[1];
  row->suid = bdata->suid;
H
Hongze Cheng 已提交
835
  for (int32_t i = 0; i < bdata->nRow; i++) {
H
Hongze Cheng 已提交
836 837
    row->uid = bdata->uid ? bdata->uid : bdata->aUid[i];
    row->row = tsdbRowFromBlockData(bdata, i);
H
Hongze Cheng 已提交
838

H
Hongze Cheng 已提交
839
    code = tsdbSttFileWriteTSData(writer, row);
H
Hongze Cheng 已提交
840 841 842 843 844
    TSDB_CHECK_CODE(code, lino, _exit);
  }

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

H
Hongze Cheng 已提交
850
int32_t tsdbSttFileWriteTombRecord(SSttFileWriter *writer, const STombRecord *record) {
H
Hongze Cheng 已提交
851
  int32_t code;
H
Hongze Cheng 已提交
852 853
  int32_t lino;

H
Hongze Cheng 已提交
854
  if (!writer->ctx->opened) {
H
Hongze Cheng 已提交
855 856 857 858
    code = tsdbSttFWriterDoOpen(writer);
    return code;
  }

H
Hongze Cheng 已提交
859 860 861 862 863
  // end time-series data write
  if (writer->bData->nRow > 0) {
    code = tsdbSttFileDoWriteTSDataBlock(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
864

H
Hongze Cheng 已提交
865 866 867 868
  if (STATIS_BLOCK_SIZE(writer->sData) > 0) {
    code = tsdbSttFileDoWriteStatisBlock(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
869

H
Hongze Cheng 已提交
870
  // write SDelRecord
H
Hongze Cheng 已提交
871
  code = tTombBlockPut(writer->tData, record);
H
Hongze Cheng 已提交
872
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
873

H
Hongze Cheng 已提交
874
  // write SDelBlock if need
H
Hongze Cheng 已提交
875
  if (TOMB_BLOCK_SIZE(writer->tData) >= writer->config->maxRow) {
H
Hongze Cheng 已提交
876
    code = tsdbSttFileDoWriteTombBlock(writer);
H
Hongze Cheng 已提交
877
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
878 879 880 881
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
882
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
883 884
  }
  return code;
H
Hongze Cheng 已提交
885 886 887
}

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