tsdbSttFileRW.c 27.6 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
  int32_t lino = 0;
H
Hongze Cheng 已提交
53
  int32_t vid = TD_VID(reader->config->tsdb->pVnode);
H
Hongze Cheng 已提交
54 55

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

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

_exit:
  if (code) {
H
Hongze Cheng 已提交
64
    TSDB_ERROR_LOG(vid, lino, code);
H
Hongze Cheng 已提交
65 66 67
    taosMemoryFree(segReader[0]);
    segReader[0] = NULL;
  }
H
Hongze Cheng 已提交
68 69 70
  return code;
}

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

H
Hongze Cheng 已提交
82
int32_t tsdbSttFileReaderOpen(const char *fname, const SSttFileReaderConfig *config, SSttFileReader **reader) {
H
Hongze Cheng 已提交
83
  int32_t code = 0;
H
Hongze Cheng 已提交
84 85 86 87 88 89
  int32_t lino = 0;
  int32_t vid = TD_VID(config->tsdb->pVnode);

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

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

  // 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 已提交
98
  int64_t size = config->file->size;
H
Hongze Cheng 已提交
99
  while (size > 0) {
H
Hongze Cheng 已提交
100
    SSttSegReader *reader1;
H
Hongze Cheng 已提交
101

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

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

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

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

_exit:
  if (code) {
H
Hongze Cheng 已提交
115
    TSDB_ERROR_LOG(vid, lino, code);
H
Hongze Cheng 已提交
116
    tsdbSttFileReaderClose(reader);
H
Hongze Cheng 已提交
117
  }
H
Hongze Cheng 已提交
118 119 120
  return code;
}

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
168 169 170 171
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 已提交
172

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

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

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
224
int32_t tsdbSttFileReadDataBlock(SSttSegReader *reader, const SSttBlk *sttBlk, SBlockData *bData) {
H
Hongze Cheng 已提交
225
  int32_t code = 0;
H
Hongze Cheng 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
  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 已提交
243 244 245
  return code;
}

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

H
Hongze Cheng 已提交
250
  tTombBlockClear(dData);
H
Hongze Cheng 已提交
251

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

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

  int64_t size = 0;
H
Hongze Cheng 已提交
259
  for (int32_t i = 0; i < ARRAY_SIZE(dData->dataArr); ++i) {
H
Hongze Cheng 已提交
260 261
    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 已提交
262
                          &reader->reader->config->bufArr[2]);
H
Hongze Cheng 已提交
263 264
    TSDB_CHECK_CODE(code, lino, _exit);

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

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

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

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

  tStatisBlockClear(sData);
H
Hongze Cheng 已提交
286 287

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

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

  int64_t size = 0;
H
Hongze Cheng 已提交
295
  for (int32_t i = 0; i < ARRAY_SIZE(sData->dataArr); ++i) {
H
Hongze Cheng 已提交
296 297 298
    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 已提交
299 300
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
301 302 303 304 305
    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 已提交
306 307 308 309 310 311 312
    size += statisBlk->size[i];
  }

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

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

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

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

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

  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 已提交
360 361 362 363 364 365 366
  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 已提交
367
  code = tCmprBlockData(writer->bData, writer->config->cmprAlg, NULL, NULL, writer->config->bufArr, writer->sizeArr);
H
Hongze Cheng 已提交
368
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
369

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

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

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

H
Hongze Cheng 已提交
385 386
  tBlockDataClear(writer->bData);

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

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

H
Hongze Cheng 已提交
397
  int32_t code = 0;
H
Hongze Cheng 已提交
398
  int32_t lino = 0;
H
Hongze Cheng 已提交
399

H
Hongze Cheng 已提交
400
  SStatisBlk statisBlk[1] = {{
H
Hongze Cheng 已提交
401
      .numRec = STATIS_BLOCK_SIZE(writer->sData),
H
Hongze Cheng 已提交
402 403 404 405 406 407 408 409 410 411
      .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 已提交
412 413
      .minVer = TARRAY2_FIRST(writer->sData->minVer),
      .maxVer = TARRAY2_FIRST(writer->sData->maxVer),
H
Hongze Cheng 已提交
414 415
  }};

H
Hongze Cheng 已提交
416 417 418 419
  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 已提交
420

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

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

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

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

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

H
Hongze Cheng 已提交
442 443
  tStatisBlockClear(writer->sData);

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

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

H
Hongze Cheng 已提交
454
  int32_t code = 0;
H
Hongze Cheng 已提交
455
  int32_t lino = 0;
H
Hongze Cheng 已提交
456

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

H
Hongze Cheng 已提交
478 479 480
  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 已提交
481 482
  }

H
Hongze Cheng 已提交
483
  for (int32_t i = 0; i < ARRAY_SIZE(writer->tData->dataArr); i++) {
H
Hongze Cheng 已提交
484
    int32_t size;
H
Hongze Cheng 已提交
485 486
    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 已提交
487
                        &writer->config->bufArr[0], 0, &size, &writer->config->bufArr[1]);
H
Hongze Cheng 已提交
488
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
489

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

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

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

H
Hongze Cheng 已提交
501
  tTombBlockClear(writer->tData);
H
Hongze Cheng 已提交
502

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

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

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
559 560 561
  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 已提交
562
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
563
    writer->file->size += writer->footer->tombBlkPtr->size;
H
Hongze Cheng 已提交
564 565 566 567
  }

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

H
Hongze Cheng 已提交
573
static int32_t tsdbSttFileDoWriteFooter(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
574 575
  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 已提交
576
  if (code) return code;
H
Hongze Cheng 已提交
577
  writer->file->size += sizeof(writer->footer);
H
Hongze Cheng 已提交
578
  return 0;
H
Hongze Cheng 已提交
579 580
}

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

H
Hongze Cheng 已提交
585
  // set
H
Hongze Cheng 已提交
586 587 588 589
  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 已提交
590
  if (!writer->config->bufArr) writer->config->bufArr = writer->bufArr;
H
Hongze Cheng 已提交
591

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

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

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

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

H
Hongze Cheng 已提交
613 614
  writer->ctx->opened = true;

H
Hongze Cheng 已提交
615
_exit:
H
Hongze Cheng 已提交
616
  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
  return code;
H
Hongze Cheng 已提交
620 621
}

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

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

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

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

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
674
  tsdbCloseFile(&writer->fd);
H
Hongze Cheng 已提交
675

H
Hongze Cheng 已提交
676
  ASSERT(writer->config->file.size < writer->file->size);
H
Hongze Cheng 已提交
677 678 679 680 681 682 683 684 685
  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 已提交
686

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

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

H
Hongze Cheng 已提交
708 709
  return 0;
}
H
Hongze Cheng 已提交
710

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

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

H
Hongze Cheng 已提交
720
int32_t tsdbSttFileWriterClose(SSttFileWriter **writer, int8_t abort, TFileOpArray *opArray) {
H
Hongze Cheng 已提交
721
  int32_t code = 0;
H
Hongze Cheng 已提交
722
  int32_t lino = 0;
H
Hongze Cheng 已提交
723
  int32_t vid = TD_VID(writer[0]->config->tsdb->pVnode);
H
Hongze Cheng 已提交
724

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

_exit:
  if (code) {
H
Hongze Cheng 已提交
740
    TSDB_ERROR_LOG(vid, lino, code);
H
Hongze Cheng 已提交
741
  }
H
Hongze Cheng 已提交
742
  return code;
H
Hongze Cheng 已提交
743 744
}

H
Hongze Cheng 已提交
745
int32_t tsdbSttFileWriteTSData(SSttFileWriter *writer, SRowInfo *row) {
H
Hongze Cheng 已提交
746
  int32_t code = 0;
H
Hongze Cheng 已提交
747
  int32_t lino = 0;
H
Hongze Cheng 已提交
748
  int32_t vid = TD_VID(writer->config->tsdb->pVnode);
H
Hongze Cheng 已提交
749

H
Hongze Cheng 已提交
750
  if (!writer->ctx->opened) {
H
Hongze Cheng 已提交
751 752 753
    code = tsdbSttFWriterDoOpen(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
754

H
Hongze Cheng 已提交
755 756
  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 已提交
757 758
    code = tsdbSttFileDoWriteTSDataBlock(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
759

H
Hongze Cheng 已提交
760 761 762 763 764 765 766 767 768 769 770 771
    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 已提交
772
    if (STATIS_BLOCK_SIZE(writer->sData) >= writer->config->maxRow) {
H
Hongze Cheng 已提交
773
      code = tsdbSttFileDoWriteStatisBlock(writer);
H
Hongze Cheng 已提交
774
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
775 776
    }

H
Hongze Cheng 已提交
777 778 779
    STbStatisRecord record[1] = {{
        .suid = row->suid,
        .uid = row->uid,
H
Hongze Cheng 已提交
780 781 782 783 784 785
        .firstKey = key->ts,
        .firstKeyVer = key->version,
        .lastKey = key->ts,
        .lastKeyVer = key->version,
        .minVer = key->version,
        .maxVer = key->version,
H
Hongze Cheng 已提交
786 787 788 789
        .count = 1,
    }};
    code = tStatisBlockPut(writer->sData, record);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
790 791 792 793 794 795 796 797 798 799 800 801
  } 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;
      TARRAY2_LAST(writer->sData->lastKeyVer) = key->version;
    } else if (key->ts == TARRAY2_LAST(writer->sData->lastKey)) {
      TARRAY2_LAST(writer->sData->lastKeyVer) = key->version;
    } else {
      ASSERTS(0, "timestamp should be in ascending order");
    }
H
Hongze Cheng 已提交
802 803
  }

H
Hongze Cheng 已提交
804
  if (row->row.type == TSDBROW_ROW_FMT) {
H
Hongze Cheng 已提交
805 806
    code = tsdbUpdateSkmRow(writer->config->tsdb, writer->ctx->tbid,  //
                            TSDBROW_SVERSION(&row->row), writer->config->skmRow);
H
Hongze Cheng 已提交
807
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
808 809
  }

H
Hongze Cheng 已提交
810
  // row to col conversion
H
Hongze Cheng 已提交
811 812 813 814 815 816 817
  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         //
  ) {
H
Hongze Cheng 已提交
818 819 820 821
    if (writer->bData->nRow == 1) {
      TARRAY2_LAST(writer->sData->firstKeyVer) = key->version;
    }

H
Hongze Cheng 已提交
822 823
    code = tBlockDataUpdateRow(writer->bData, &row->row, writer->config->skmRow->pTSchema);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
824 825 826 827 828
  } else {
    if (writer->bData->nRow >= writer->config->maxRow) {
      code = tsdbSttFileDoWriteTSDataBlock(writer);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
829

H
Hongze Cheng 已提交
830
    code = tBlockDataAppendRow(writer->bData, &row->row, writer->config->skmRow->pTSchema, row->uid);
H
Hongze Cheng 已提交
831
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
832 833 834 835
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
836
    TSDB_ERROR_LOG(vid, lino, code);
H
Hongze Cheng 已提交
837
  }
H
Hongze Cheng 已提交
838
  return code;
H
Hongze Cheng 已提交
839 840
}

H
Hongze Cheng 已提交
841
int32_t tsdbSttFileWriteTSDataBlock(SSttFileWriter *writer, SBlockData *bdata) {
H
Hongze Cheng 已提交
842 843 844
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
845 846
  SRowInfo row[1];
  row->suid = bdata->suid;
H
Hongze Cheng 已提交
847
  for (int32_t i = 0; i < bdata->nRow; i++) {
H
Hongze Cheng 已提交
848 849
    row->uid = bdata->uid ? bdata->uid : bdata->aUid[i];
    row->row = tsdbRowFromBlockData(bdata, i);
H
Hongze Cheng 已提交
850

H
Hongze Cheng 已提交
851
    code = tsdbSttFileWriteTSData(writer, row);
H
Hongze Cheng 已提交
852 853 854 855 856
    TSDB_CHECK_CODE(code, lino, _exit);
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
857
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
858
  }
H
Hongze Cheng 已提交
859
  return code;
H
Hongze Cheng 已提交
860 861
}

H
Hongze Cheng 已提交
862
int32_t tsdbSttFileWriteTombRecord(SSttFileWriter *writer, const STombRecord *record) {
H
Hongze Cheng 已提交
863
  int32_t code;
H
Hongze Cheng 已提交
864 865
  int32_t lino;

H
Hongze Cheng 已提交
866
  if (!writer->ctx->opened) {
H
Hongze Cheng 已提交
867 868 869 870
    code = tsdbSttFWriterDoOpen(writer);
    return code;
  }

H
Hongze Cheng 已提交
871 872 873 874 875
  // end time-series data write
  if (writer->bData->nRow > 0) {
    code = tsdbSttFileDoWriteTSDataBlock(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
876

H
Hongze Cheng 已提交
877 878 879 880
  if (STATIS_BLOCK_SIZE(writer->sData) > 0) {
    code = tsdbSttFileDoWriteStatisBlock(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
881

H
Hongze Cheng 已提交
882
  // write SDelRecord
H
Hongze Cheng 已提交
883
  code = tTombBlockPut(writer->tData, record);
H
Hongze Cheng 已提交
884
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
885

H
Hongze Cheng 已提交
886
  // write SDelBlock if need
H
Hongze Cheng 已提交
887
  if (TOMB_BLOCK_SIZE(writer->tData) >= writer->config->maxRow) {
H
Hongze Cheng 已提交
888
    code = tsdbSttFileDoWriteTombBlock(writer);
H
Hongze Cheng 已提交
889
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
890 891 892 893
  }

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