tsdbSttFileRW.c 26.1 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 22 23 24
  SFDataPtr sttBlkPtr[1];
  SFDataPtr delBlkPtr[1];
  SFDataPtr statisBlkPtr[1];
  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 31 32 33 34
  STsdbFD             *fd;
};

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

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

H
Hongze Cheng 已提交
50
  int32_t code = 0;
H
Hongze Cheng 已提交
51
  int32_t lino = 0;
H
Hongze Cheng 已提交
52
  int32_t vid = TD_VID(reader->config->tsdb->pVnode);
H
Hongze Cheng 已提交
53 54

  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(vid, 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_FREE(reader[0]->sttBlkArray);
    TARRAY2_FREE(reader[0]->delBlkArray);
    TARRAY2_FREE(reader[0]->statisBlkArray);
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 88
  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 已提交
89
  reader[0]->config[0] = config[0];
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(vid, 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 121 122 123 124 125
  if (reader[0]) {
    tsdbCloseFile(&reader[0]->fd);
    TARRAY2_CLEAR_FREE(reader[0]->readerArray, tsdbSttSegReaderClose);
    taosMemoryFree(reader[0]);
    reader[0] = NULL;
  }
H
Hongze Cheng 已提交
126
  return 0;
H
Hongze Cheng 已提交
127 128
}

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

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

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

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

H
Hongze Cheng 已提交
151
      TARRAY2_INIT_EX(reader->statisBlkArray, size, size, data);
H
Hongze Cheng 已提交
152
    } else {
H
Hongze Cheng 已提交
153
      TARRAY2_INIT(reader->statisBlkArray);
H
Hongze Cheng 已提交
154 155
    }

H
Hongze Cheng 已提交
156
    reader->ctx->statisBlkLoaded = true;
H
Hongze Cheng 已提交
157 158
  }

H
Hongze Cheng 已提交
159
  statisBlkArray[0] = reader->statisBlkArray;
H
Hongze Cheng 已提交
160
  return 0;
H
Hongze Cheng 已提交
161 162
}

H
Hongze Cheng 已提交
163
int32_t tsdbSttFileReadDelBlk(SSttSegReader *reader, const TDelBlkArray **delBlkArray) {
H
Hongze Cheng 已提交
164
  if (!reader->ctx->delBlkLoaded) {
H
Hongze Cheng 已提交
165 166
    if (reader->footer->delBlkPtr->size > 0) {
      ASSERT(reader->footer->delBlkPtr->size % sizeof(SDelBlk) == 0);
H
Hongze Cheng 已提交
167

H
Hongze Cheng 已提交
168 169
      int32_t size = reader->footer->delBlkPtr->size / sizeof(SDelBlk);
      void   *data = taosMemoryMalloc(reader->footer->delBlkPtr->size);
H
Hongze Cheng 已提交
170 171
      if (!data) return TSDB_CODE_OUT_OF_MEMORY;

H
Hongze Cheng 已提交
172 173
      int32_t code =
          tsdbReadFile(reader->reader->fd, reader->footer->delBlkPtr->offset, data, reader->footer->delBlkPtr->size);
H
Hongze Cheng 已提交
174 175 176 177
      if (code) {
        taosMemoryFree(data);
        return code;
      }
H
Hongze Cheng 已提交
178

H
Hongze Cheng 已提交
179
      TARRAY2_INIT_EX(reader->delBlkArray, size, size, data);
H
Hongze Cheng 已提交
180
    } else {
H
Hongze Cheng 已提交
181
      TARRAY2_INIT(reader->delBlkArray);
H
Hongze Cheng 已提交
182 183
    }

H
Hongze Cheng 已提交
184
    reader->ctx->delBlkLoaded = true;
H
Hongze Cheng 已提交
185 186
  }

H
Hongze Cheng 已提交
187
  delBlkArray[0] = reader->delBlkArray;
H
Hongze Cheng 已提交
188 189 190
  return 0;
}

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

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

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

H
Hongze Cheng 已提交
207
      TARRAY2_INIT_EX(reader->sttBlkArray, size, size, data);
H
Hongze Cheng 已提交
208
    } else {
H
Hongze Cheng 已提交
209
      TARRAY2_INIT(reader->sttBlkArray);
H
Hongze Cheng 已提交
210 211
    }

H
Hongze Cheng 已提交
212
    reader->ctx->sttBlkLoaded = true;
H
Hongze Cheng 已提交
213 214
  }

H
Hongze Cheng 已提交
215
  sttBlkArray[0] = reader->sttBlkArray;
H
Hongze Cheng 已提交
216
  return 0;
H
Hongze Cheng 已提交
217 218
}

H
Hongze Cheng 已提交
219
int32_t tsdbSttFileReadSttBlock(SSttSegReader *reader, const SSttBlk *sttBlk, SBlockData *bData) {
H
Hongze Cheng 已提交
220 221 222 223 224
  int32_t code = 0;
  // TODO
  return code;
}

H
Hongze Cheng 已提交
225
int32_t tsdbSttFileReadDelBlock(SSttSegReader *reader, const SDelBlk *delBlk, SDelBlock *dData) {
H
Hongze Cheng 已提交
226
  int32_t code = 0;
H
Hongze Cheng 已提交
227 228 229 230 231 232 233 234 235 236 237
  int32_t lino = 0;
  int32_t vid = TD_VID(reader->reader->config->tsdb->pVnode);

  tDelBlockClear(dData);
  code = tRealloc(&reader->reader->config->aBuf[0], delBlk->dp->size);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tsdbReadFile(reader->reader->fd, delBlk->dp->offset, reader->reader->config->aBuf[0], delBlk->dp->size);
  if (code) TSDB_CHECK_CODE(code, lino, _exit);

  int64_t size = 0;
H
Hongze Cheng 已提交
238
  for (int32_t i = 0; i < ARRAY_SIZE(dData->dataArr); ++i) {
H
Hongze Cheng 已提交
239 240 241 242 243 244 245 246 247 248 249 250
    code = tsdbDecmprData(reader->reader->config->aBuf[0] + size, delBlk->size[i], TSDB_DATA_TYPE_BIGINT,
                          TWO_STAGE_COMP, NULL, 0, NULL);  // TODO
    TSDB_CHECK_CODE(code, lino, _exit);

    size += delBlk->size[i];
  }

  ASSERT(size == delBlk->dp->size);
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d, reason:%s", vid, __func__, lino, tstrerror(code));
  }
H
Hongze Cheng 已提交
251 252 253
  return code;
}

H
Hongze Cheng 已提交
254
int32_t tsdbSttFileReadStatisBlock(SSttSegReader *reader, const SStatisBlk *statisBlk, STbStatisBlock *sData) {
H
Hongze Cheng 已提交
255
  int32_t code = 0;
H
Hongze Cheng 已提交
256 257 258 259 260 261 262 263 264 265 266
  int32_t lino = 0;
  int32_t vid = TD_VID(reader->reader->config->tsdb->pVnode);

  tStatisBlockClear(sData);
  code = tRealloc(&reader->reader->config->aBuf[0], statisBlk->dp->size);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tsdbReadFile(reader->reader->fd, statisBlk->dp->offset, reader->reader->config->aBuf[0], statisBlk->dp->size);
  if (code) TSDB_CHECK_CODE(code, lino, _exit);

  int64_t size = 0;
H
Hongze Cheng 已提交
267
  for (int32_t i = 0; i < ARRAY_SIZE(sData->dataArr); ++i) {
H
Hongze Cheng 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280
    code = tsdbDecmprData(reader->reader->config->aBuf[0] + size, statisBlk->size[i], TSDB_DATA_TYPE_BIGINT,
                          TWO_STAGE_COMP, NULL, 0, NULL);  // TODO
    TSDB_CHECK_CODE(code, lino, _exit);

    size += statisBlk->size[i];
  }

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

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d, reason:%s", vid, __func__, lino, tstrerror(code));
  }
H
Hongze Cheng 已提交
281 282 283
  return code;
}

H
Hongze Cheng 已提交
284
// SSttFWriter ============================================================
H
Hongze Cheng 已提交
285
struct SSttFileWriter {
H
Hongze Cheng 已提交
286
  SSttFileWriterConfig config[1];
H
Hongze Cheng 已提交
287
  struct {
H
Hongze Cheng 已提交
288 289
    bool    opened;
    TABLEID tbid[1];
H
Hongze Cheng 已提交
290
  } ctx[1];
H
Hongze Cheng 已提交
291
  // file
H
Hongze Cheng 已提交
292
  STFile file[1];
H
Hongze Cheng 已提交
293
  // data
H
Hongze Cheng 已提交
294 295
  TSttBlkArray    sttBlkArray[1];
  TStatisBlkArray statisBlkArray[1];
H
Hongze Cheng 已提交
296
  TDelBlkArray    delBlkArray[1];
H
Hongze Cheng 已提交
297
  SSttFooter      footer[1];
H
Hongze Cheng 已提交
298 299
  SBlockData      bData[1];
  STbStatisBlock  sData[1];
H
Hongze Cheng 已提交
300
  SDelBlock       dData[1];
H
Hongze Cheng 已提交
301
  // helper data
H
Hongze Cheng 已提交
302 303
  SSkmInfo skmTb[1];
  SSkmInfo skmRow[1];
H
Hongze Cheng 已提交
304 305
  int32_t  sizeArr[5];
  uint8_t *bufArr[5];
H
Hongze Cheng 已提交
306
  STsdbFD *fd;
H
Hongze Cheng 已提交
307
};
H
Hongze Cheng 已提交
308

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

H
Hongze Cheng 已提交
312
  int32_t code = 0;
H
Hongze Cheng 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
  int32_t lino = 0;
  SSttBlk sttBlk[1];

  sttBlk->suid = writer->bData->suid;
  sttBlk->minUid = writer->bData->uid ? writer->bData->uid : writer->bData->aUid[0];
  sttBlk->maxUid = writer->bData->uid ? writer->bData->uid : writer->bData->aUid[writer->bData->nRow - 1];
  sttBlk->minKey = sttBlk->maxKey = writer->bData->aTSKEY[0];
  sttBlk->minVer = sttBlk->maxVer = writer->bData->aVersion[0];
  sttBlk->nRow = writer->bData->nRow;
  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 已提交
329
  code = tCmprBlockData(writer->bData, writer->config->cmprAlg, NULL, NULL, writer->config->aBuf, writer->sizeArr);
H
Hongze Cheng 已提交
330
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
331

H
Hongze Cheng 已提交
332
  sttBlk->bInfo.offset = writer->file->size;
H
Hongze Cheng 已提交
333 334
  sttBlk->bInfo.szKey = writer->sizeArr[2] + writer->sizeArr[3];
  sttBlk->bInfo.szBlock = writer->sizeArr[0] + writer->sizeArr[1] + sttBlk->bInfo.szKey;
H
Hongze Cheng 已提交
335

H
Hongze Cheng 已提交
336
  for (int32_t i = 3; i >= 0; i--) {
H
Hongze Cheng 已提交
337 338
    if (writer->sizeArr[i]) {
      code = tsdbWriteFile(writer->fd, writer->file->size, writer->config->aBuf[i], writer->sizeArr[i]);
H
Hongze Cheng 已提交
339
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
340
      writer->file->size += writer->sizeArr[i];
H
Hongze Cheng 已提交
341 342 343
    }
  }

H
Hongze Cheng 已提交
344
  code = TARRAY2_APPEND_PTR(writer->sttBlkArray, sttBlk);
H
Hongze Cheng 已提交
345
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
346

H
Hongze Cheng 已提交
347 348
  tBlockDataClear(writer->bData);

H
Hongze Cheng 已提交
349 350
_exit:
  if (code) {
H
Hongze Cheng 已提交
351
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
352
  }
H
Hongze Cheng 已提交
353 354 355
  return code;
}

H
Hongze Cheng 已提交
356
static int32_t tsdbSttFileDoWriteStatisBlock(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
357
  if (STATIS_BLOCK_SIZE(writer->sData)) return 0;
H
Hongze Cheng 已提交
358

H
Hongze Cheng 已提交
359
  int32_t code = 0;
H
Hongze Cheng 已提交
360
  int32_t lino = 0;
H
Hongze Cheng 已提交
361

H
Hongze Cheng 已提交
362
  SStatisBlk statisBlk[1] = {{
H
Hongze Cheng 已提交
363
      .numRec = STATIS_BLOCK_SIZE(writer->sData),
H
Hongze Cheng 已提交
364 365 366 367 368 369 370 371 372 373
      .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 已提交
374 375
      .minVer = TARRAY2_FIRST(writer->sData->minVer),
      .maxVer = TARRAY2_FIRST(writer->sData->maxVer),
H
Hongze Cheng 已提交
376 377
  }};

H
Hongze Cheng 已提交
378 379 380 381
  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 已提交
382

H
Hongze Cheng 已提交
383 384
  statisBlk->dp->offset = writer->file->size;
  statisBlk->dp->size = 0;
H
Hongze Cheng 已提交
385

H
Hongze Cheng 已提交
386
  for (int32_t i = 0; i < STATIS_RECORD_NUM_ELEM; i++) {
H
Hongze Cheng 已提交
387
    int32_t size;
H
Hongze Cheng 已提交
388 389 390
    code = tsdbCmprData((uint8_t *)TARRAY2_DATA(&writer->sData->dataArr[i]),
                        TARRAY2_DATA_LEN(&writer->sData->dataArr[i]), TSDB_DATA_TYPE_BIGINT, TWO_STAGE_COMP,
                        &writer->config->aBuf[0], 0, &size, &writer->config->aBuf[1]);
H
Hongze Cheng 已提交
391
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
392 393 394 395

    code = tsdbWriteFile(writer->fd, writer->file->size, writer->config->aBuf[0], size);
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
396 397
    statisBlk->size[i] = size;
    statisBlk->dp->size += size;
H
Hongze Cheng 已提交
398
    writer->file->size += size;
H
Hongze Cheng 已提交
399
  }
H
Hongze Cheng 已提交
400

H
Hongze Cheng 已提交
401
  code = TARRAY2_APPEND_PTR(writer->statisBlkArray, statisBlk);
H
Hongze Cheng 已提交
402
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
403

H
Hongze Cheng 已提交
404 405
  tStatisBlockClear(writer->sData);

H
Hongze Cheng 已提交
406 407
_exit:
  if (code) {
H
Hongze Cheng 已提交
408
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
409
  }
H
Hongze Cheng 已提交
410 411 412
  return code;
}

H
Hongze Cheng 已提交
413
static int32_t tsdbSttFileDoWriteDelBlock(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
414
  return 0;
H
Hongze Cheng 已提交
415
#if 0
H
Hongze Cheng 已提交
416 417
  if (writer->dData->nRow == 0) return 0;

H
Hongze Cheng 已提交
418
  int32_t code = 0;
H
Hongze Cheng 已提交
419 420
  int32_t lino;

H
Hongze Cheng 已提交
421
  SDelBlk delBlk[1];
H
Hongze Cheng 已提交
422

H
Hongze Cheng 已提交
423 424 425 426 427 428 429 430 431
  delBlk->nRow = writer->sData->nRow;
  delBlk->minTid.suid = writer->sData->aData[0][0];
  delBlk->minTid.uid = writer->sData->aData[1][0];
  delBlk->maxTid.suid = writer->sData->aData[0][writer->sData->nRow - 1];
  delBlk->maxTid.uid = writer->sData->aData[1][writer->sData->nRow - 1];
  delBlk->minVer = delBlk->maxVer = delBlk->maxVer = writer->sData->aData[2][0];
  for (int32_t iRow = 1; iRow < writer->sData->nRow; iRow++) {
    if (delBlk->minVer > writer->sData->aData[2][iRow]) delBlk->minVer = writer->sData->aData[2][iRow];
    if (delBlk->maxVer < writer->sData->aData[2][iRow]) delBlk->maxVer = writer->sData->aData[2][iRow];
H
Hongze Cheng 已提交
432 433
  }

H
Hongze Cheng 已提交
434
  delBlk->dp.offset = writer->file->size;
H
Hongze Cheng 已提交
435
  delBlk->dp.size = 0;  // TODO
H
Hongze Cheng 已提交
436

H
Hongze Cheng 已提交
437 438
  int64_t tsize = sizeof(int64_t) * writer->dData->nRow;
  for (int32_t i = 0; i < ARRAY_SIZE(writer->dData->aData); i++) {
H
Hongze Cheng 已提交
439
    code = tsdbWriteFile(writer->fd, writer->file->size, (const uint8_t *)writer->dData->aData[i], tsize);
H
Hongze Cheng 已提交
440 441
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
442
    delBlk->dp.size += tsize;
H
Hongze Cheng 已提交
443
    writer->file->size += tsize;
H
Hongze Cheng 已提交
444
  }
H
Hongze Cheng 已提交
445
  tDelBlockDestroy(writer->dData);
H
Hongze Cheng 已提交
446

H
Hongze Cheng 已提交
447
  code = TARRAY2_APPEND_PTR(writer->delBlkArray, delBlk);
H
Hongze Cheng 已提交
448
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
449 450 451

_exit:
  if (code) {
H
Hongze Cheng 已提交
452
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
453 454 455 456
              tstrerror(code));
  } else {
    // tsdbTrace();
  }
H
Hongze Cheng 已提交
457
  return code;
H
Hongze Cheng 已提交
458
#endif
H
Hongze Cheng 已提交
459 460
}

H
Hongze Cheng 已提交
461
static int32_t tsdbSttFileDoWriteSttBlk(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
462
  int32_t code = 0;
H
Hongze Cheng 已提交
463 464
  int32_t lino;

H
Hongze Cheng 已提交
465
  writer->footer->sttBlkPtr->offset = writer->file->size;
H
Hongze Cheng 已提交
466
  writer->footer->sttBlkPtr->size = TARRAY2_DATA_LEN(writer->sttBlkArray);
H
Hongze Cheng 已提交
467

H
Hongze Cheng 已提交
468
  if (writer->footer->sttBlkPtr->size) {
H
Hongze Cheng 已提交
469
    code = tsdbWriteFile(writer->fd, writer->file->size, (const uint8_t *)TARRAY2_DATA(writer->sttBlkArray),
H
Hongze Cheng 已提交
470
                         writer->footer->sttBlkPtr->size);
H
Hongze Cheng 已提交
471
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
472
    writer->file->size += writer->footer->sttBlkPtr->size;
H
Hongze Cheng 已提交
473 474 475 476
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
477
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
478
  }
H
Hongze Cheng 已提交
479 480 481
  return code;
}

H
Hongze Cheng 已提交
482
static int32_t tsdbSttFileDoWriteStatisBlk(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
483
  int32_t code = 0;
H
Hongze Cheng 已提交
484
  int32_t lino;
H
Hongze Cheng 已提交
485

H
Hongze Cheng 已提交
486
  writer->footer->statisBlkPtr->offset = writer->file->size;
H
Hongze Cheng 已提交
487
  writer->footer->statisBlkPtr->size = TARRAY2_DATA_LEN(writer->statisBlkArray);
H
Hongze Cheng 已提交
488

H
Hongze Cheng 已提交
489
  if (writer->footer->statisBlkPtr->size) {
H
Hongze Cheng 已提交
490
    code = tsdbWriteFile(writer->fd, writer->file->size, (const uint8_t *)TARRAY2_DATA(writer->statisBlkArray),
H
Hongze Cheng 已提交
491
                         writer->footer->statisBlkPtr->size);
H
Hongze Cheng 已提交
492
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
493
    writer->file->size += writer->footer->statisBlkPtr->size;
H
Hongze Cheng 已提交
494 495 496 497
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
498
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
499
  }
H
Hongze Cheng 已提交
500 501 502
  return code;
}

H
Hongze Cheng 已提交
503
static int32_t tsdbSttFileDoWriteDelBlk(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
504
  int32_t code = 0;
H
Hongze Cheng 已提交
505 506
  int32_t lino;

H
Hongze Cheng 已提交
507
  writer->footer->delBlkPtr->offset = writer->file->size;
H
Hongze Cheng 已提交
508
  writer->footer->delBlkPtr->size = TARRAY2_DATA_LEN(writer->delBlkArray);
H
Hongze Cheng 已提交
509

H
Hongze Cheng 已提交
510
  if (writer->footer->delBlkPtr->size) {
H
Hongze Cheng 已提交
511
    code = tsdbWriteFile(writer->fd, writer->file->size, (const uint8_t *)TARRAY2_DATA(writer->delBlkArray),
H
Hongze Cheng 已提交
512
                         writer->footer->delBlkPtr->size);
H
Hongze Cheng 已提交
513
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
514
    writer->file->size += writer->footer->delBlkPtr->size;
H
Hongze Cheng 已提交
515 516 517 518
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
519
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
520
              tstrerror(code));
H
Hongze Cheng 已提交
521 522 523 524
  }
  return code;
}

H
Hongze Cheng 已提交
525
static int32_t tsdbSttFileDoWriteFooter(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
526 527
  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 已提交
528
  writer->file->size += sizeof(writer->footer);
H
Hongze Cheng 已提交
529 530 531
  return code;
}

H
Hongze Cheng 已提交
532
static int32_t tsdbSttFWriterDoOpen(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
533
  int32_t code = 0;
H
Hongze Cheng 已提交
534
  int32_t lino = 0;
H
Hongze Cheng 已提交
535
  int32_t vid = TD_VID(writer->config->tsdb->pVnode);
H
Hongze Cheng 已提交
536

H
Hongze Cheng 已提交
537
  // set
H
Hongze Cheng 已提交
538 539 540 541
  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 已提交
542
  if (!writer->config->aBuf) writer->config->aBuf = writer->bufArr;
H
Hongze Cheng 已提交
543

H
Hongze Cheng 已提交
544 545 546
  // open file
  int32_t flag;
  char    fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
547

H
Hongze Cheng 已提交
548
  if (writer->file->size) {
H
Hongze Cheng 已提交
549 550 551
    flag = TD_FILE_READ | TD_FILE_WRITE;
  } else {
    flag = TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
H
Hongze Cheng 已提交
552
  }
H
Hongze Cheng 已提交
553

H
Hongze Cheng 已提交
554
  tsdbTFileName(writer->config->tsdb, writer->file, fname);
H
Hongze Cheng 已提交
555
  code = tsdbOpenFile(fname, writer->config->szPage, flag, &writer->fd);
H
Hongze Cheng 已提交
556 557
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
558
  if (!writer->file->size) {
H
Hongze Cheng 已提交
559 560 561
    uint8_t hdr[TSDB_FHDR_SIZE] = {0};
    code = tsdbWriteFile(writer->fd, 0, hdr, sizeof(hdr));
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
562
    writer->file->size += sizeof(hdr);
H
Hongze Cheng 已提交
563
  }
H
Hongze Cheng 已提交
564

H
Hongze Cheng 已提交
565
_exit:
H
Hongze Cheng 已提交
566
  if (code) {
H
Hongze Cheng 已提交
567
    TSDB_ERROR_LOG(vid, lino, code);
H
Hongze Cheng 已提交
568
  } else {
H
Hongze Cheng 已提交
569
    writer->ctx->opened = true;
H
Hongze Cheng 已提交
570
  }
H
Hongze Cheng 已提交
571
  return code;
H
Hongze Cheng 已提交
572 573
}

H
Hongze Cheng 已提交
574 575
static void tsdbSttFWriterDoClose(SSttFileWriter *writer) {
  ASSERT(!writer->fd);
H
Hongze Cheng 已提交
576

H
Hongze Cheng 已提交
577 578
  for (int32_t i = 0; i < ARRAY_SIZE(writer->sizeArr); ++i) {
    tFree(writer->bufArr[i]);
H
Hongze Cheng 已提交
579 580 581 582 583 584 585
  }
  tDestroyTSchema(writer->skmRow->pTSchema);
  tDestroyTSchema(writer->skmTb->pTSchema);
  tStatisBlockFree(writer->sData);
  tDelBlockFree(writer->dData);
  tBlockDataDestroy(writer->bData);
  TARRAY2_FREE(writer->delBlkArray);
H
Hongze Cheng 已提交
586
  TARRAY2_FREE(writer->statisBlkArray);
H
Hongze Cheng 已提交
587
  TARRAY2_FREE(writer->sttBlkArray);
H
Hongze Cheng 已提交
588
}
H
Hongze Cheng 已提交
589

H
Hongze Cheng 已提交
590 591 592 593
static int32_t tsdbSttFileDoUpdateHeader(SSttFileWriter *writer) {
  // TODO
  return 0;
}
H
Hongze Cheng 已提交
594

H
Hongze Cheng 已提交
595 596 597
static int32_t tsdbSttFWriterCloseCommit(SSttFileWriter *writer, STFileOp *op) {
  int32_t lino;
  int32_t code;
H
Hongze Cheng 已提交
598
  int32_t vid = TD_VID(writer->config->tsdb->pVnode);
H
Hongze Cheng 已提交
599

H
Hongze Cheng 已提交
600 601
  code = tsdbSttFileDoWriteTSDataBlock(writer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
602

H
Hongze Cheng 已提交
603
  code = tsdbSttFileDoWriteStatisBlock(writer);
H
Hongze Cheng 已提交
604 605
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
606 607
  code = tsdbSttFileDoWriteDelBlock(writer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
608

H
Hongze Cheng 已提交
609 610
  code = tsdbSttFileDoWriteSttBlk(writer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
611

H
Hongze Cheng 已提交
612 613
  code = tsdbSttFileDoWriteStatisBlk(writer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
614

H
Hongze Cheng 已提交
615 616
  code = tsdbSttFileDoWriteDelBlk(writer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
617

H
Hongze Cheng 已提交
618 619
  code = tsdbSttFileDoWriteFooter(writer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
620

H
Hongze Cheng 已提交
621
  code = tsdbSttFileDoUpdateHeader(writer);
H
Hongze Cheng 已提交
622 623
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
624
  code = tsdbFsyncFile(writer->fd);
H
Hongze Cheng 已提交
625 626
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
627
  tsdbCloseFile(&writer->fd);
H
Hongze Cheng 已提交
628

H
Hongze Cheng 已提交
629
  ASSERT(writer->config->file.size < writer->file->size);
H
Hongze Cheng 已提交
630 631 632
  op->optype = writer->config->file.size ? TSDB_FOP_MODIFY : TSDB_FOP_CREATE;
  op->fid = writer->config->file.fid;
  op->of = writer->config->file;
H
Hongze Cheng 已提交
633
  op->nf = writer->file[0];
H
Hongze Cheng 已提交
634

H
Hongze Cheng 已提交
635 636
_exit:
  if (code) {
H
Hongze Cheng 已提交
637
    TSDB_ERROR_LOG(vid, lino, code);
H
Hongze Cheng 已提交
638
  }
H
Hongze Cheng 已提交
639
  return code;
H
Hongze Cheng 已提交
640
}
H
Hongze Cheng 已提交
641

H
Hongze Cheng 已提交
642
static int32_t tsdbSttFWriterCloseAbort(SSttFileWriter *writer) {
H
Hongze Cheng 已提交
643
  if (writer->config->file.size) {  // truncate the file to the original size
H
Hongze Cheng 已提交
644 645
    ASSERT(writer->config->file.size <= writer->file->size);
    if (writer->config->file.size < writer->file->size) {
H
Hongze Cheng 已提交
646
      taosFtruncateFile(writer->fd->pFD, writer->config->file.size);
H
Hongze Cheng 已提交
647 648 649
      tsdbCloseFile(&writer->fd);
    }
  } else {  // remove the file
H
Hongze Cheng 已提交
650 651
    char fname[TSDB_FILENAME_LEN];
    tsdbTFileName(writer->config->tsdb, &writer->config->file, fname);
H
Hongze Cheng 已提交
652 653 654 655
    tsdbCloseFile(&writer->fd);
    taosRemoveFile(fname);
  }

H
Hongze Cheng 已提交
656 657
  return 0;
}
H
Hongze Cheng 已提交
658

H
Hongze Cheng 已提交
659
int32_t tsdbSttFileWriterOpen(const SSttFileWriterConfig *config, SSttFileWriter **writer) {
H
Hongze Cheng 已提交
660
  writer[0] = taosMemoryCalloc(1, sizeof(*writer[0]));
H
Hongze Cheng 已提交
661 662
  if (writer[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;

H
Hongze Cheng 已提交
663
  writer[0]->config[0] = config[0];
H
Hongze Cheng 已提交
664
  writer[0]->ctx->opened = false;
H
Hongze Cheng 已提交
665 666 667
  return 0;
}

H
Hongze Cheng 已提交
668
int32_t tsdbSttFileWriterClose(SSttFileWriter **writer, int8_t abort, STFileOp *op) {
H
Hongze Cheng 已提交
669
  int32_t code = 0;
H
Hongze Cheng 已提交
670
  int32_t lino = 0;
H
Hongze Cheng 已提交
671
  int32_t vid = TD_VID(writer[0]->config->tsdb->pVnode);
H
Hongze Cheng 已提交
672

H
Hongze Cheng 已提交
673
  if (!writer[0]->ctx->opened) {
H
Hongze Cheng 已提交
674
    if (op) op->optype = TSDB_FOP_NONE;
H
Hongze Cheng 已提交
675
  } else {
H
Hongze Cheng 已提交
676 677
    if (abort) {
      code = tsdbSttFWriterCloseAbort(writer[0]);
H
Hongze Cheng 已提交
678
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
679
    } else {
H
Hongze Cheng 已提交
680
      code = tsdbSttFWriterCloseCommit(writer[0], op);
H
Hongze Cheng 已提交
681
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
682
    }
H
Hongze Cheng 已提交
683
    tsdbSttFWriterDoClose(writer[0]);
H
Hongze Cheng 已提交
684
  }
H
Hongze Cheng 已提交
685 686
  taosMemoryFree(writer[0]);
  writer[0] = NULL;
H
Hongze Cheng 已提交
687 688 689

_exit:
  if (code) {
H
Hongze Cheng 已提交
690
    TSDB_ERROR_LOG(vid, lino, code);
H
Hongze Cheng 已提交
691
  }
H
Hongze Cheng 已提交
692
  return code;
H
Hongze Cheng 已提交
693 694
}

H
Hongze Cheng 已提交
695
int32_t tsdbSttFileWriteTSData(SSttFileWriter *writer, SRowInfo *row) {
H
Hongze Cheng 已提交
696
  int32_t code = 0;
H
Hongze Cheng 已提交
697
  int32_t lino = 0;
H
Hongze Cheng 已提交
698
  int32_t vid = TD_VID(writer->config->tsdb->pVnode);
H
Hongze Cheng 已提交
699

H
Hongze Cheng 已提交
700
  if (!writer->ctx->opened) {
H
Hongze Cheng 已提交
701 702 703
    code = tsdbSttFWriterDoOpen(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
704

H
Hongze Cheng 已提交
705 706
  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 已提交
707 708
    code = tsdbSttFileDoWriteTSDataBlock(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
709

H
Hongze Cheng 已提交
710 711 712 713 714 715 716 717 718 719 720 721
    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 已提交
722
    if (STATIS_BLOCK_SIZE(writer->sData) >= writer->config->maxRow) {
H
Hongze Cheng 已提交
723
      code = tsdbSttFileDoWriteStatisBlock(writer);
H
Hongze Cheng 已提交
724
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
725 726
    }

H
Hongze Cheng 已提交
727 728 729
    STbStatisRecord record[1] = {{
        .suid = row->suid,
        .uid = row->uid,
H
Hongze Cheng 已提交
730 731 732 733 734 735
        .firstKey = key->ts,
        .firstKeyVer = key->version,
        .lastKey = key->ts,
        .lastKeyVer = key->version,
        .minVer = key->version,
        .maxVer = key->version,
H
Hongze Cheng 已提交
736 737 738 739
        .count = 1,
    }};
    code = tStatisBlockPut(writer->sData, record);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
740 741
  }

H
Hongze Cheng 已提交
742
  if (row->row.type == TSDBROW_ROW_FMT) {
H
Hongze Cheng 已提交
743 744
    code = tsdbUpdateSkmRow(writer->config->tsdb, writer->ctx->tbid,  //
                            TSDBROW_SVERSION(&row->row), writer->config->skmRow);
H
Hongze Cheng 已提交
745
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
746 747
  }

H
Hongze Cheng 已提交
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
  // row to col conversion
  if (key->version <= writer->config->compactVersion) {
    if (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);
    } else {
      code = tBlockDataAppendRow(writer->bData, &row->row, writer->config->skmRow->pTSchema, row->uid);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
  } else {
    if (writer->bData->nRow >= writer->config->maxRow) {
      code = tsdbSttFileDoWriteTSDataBlock(writer);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
767

H
Hongze Cheng 已提交
768
    code = tBlockDataAppendRow(writer->bData, &row->row, writer->config->skmRow->pTSchema, row->uid);
H
Hongze Cheng 已提交
769
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
770 771
  }

H
Hongze Cheng 已提交
772 773 774 775 776 777 778 779
  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->lastKey) = key->ts;
    TARRAY2_LAST(writer->sData->lastKeyVer) = key->version;
    TARRAY2_LAST(writer->sData->count)++;
  } else if (key->ts == TARRAY2_LAST(writer->sData->lastKey)) {
    TARRAY2_LAST(writer->sData->lastKeyVer) = key->version;
H
Hongze Cheng 已提交
780 781 782 783
  } else {
    ASSERTS(0, "timestamp should be in ascending order");
  }

H
Hongze Cheng 已提交
784 785
_exit:
  if (code) {
H
Hongze Cheng 已提交
786
    TSDB_ERROR_LOG(vid, lino, code);
H
Hongze Cheng 已提交
787
  }
H
Hongze Cheng 已提交
788
  return code;
H
Hongze Cheng 已提交
789 790
}

H
Hongze Cheng 已提交
791
int32_t tsdbSttFileWriteTSDataBlock(SSttFileWriter *writer, SBlockData *bdata) {
H
Hongze Cheng 已提交
792 793 794
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
795 796
  SRowInfo row[1];
  row->suid = bdata->suid;
H
Hongze Cheng 已提交
797
  for (int32_t i = 0; i < bdata->nRow; i++) {
H
Hongze Cheng 已提交
798 799
    row->uid = bdata->uid ? bdata->uid : bdata->aUid[i];
    row->row = tsdbRowFromBlockData(bdata, i);
H
Hongze Cheng 已提交
800

H
Hongze Cheng 已提交
801
    code = tsdbSttFileWriteTSData(writer, row);
H
Hongze Cheng 已提交
802 803 804 805 806
    TSDB_CHECK_CODE(code, lino, _exit);
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
807
    TSDB_ERROR_LOG(TD_VID(writer->config->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
808
  }
H
Hongze Cheng 已提交
809
  return code;
H
Hongze Cheng 已提交
810 811
}

H
Hongze Cheng 已提交
812
int32_t tsdbSttFileWriteDLData(SSttFileWriter *writer, TABLEID *tbid, SDelData *pDelData) {
H
Hongze Cheng 已提交
813 814
  ASSERTS(0, "TODO: Not implemented yet");

H
Hongze Cheng 已提交
815
  int32_t code;
H
Hongze Cheng 已提交
816 817 818
  int32_t lino;
  int32_t vid = TD_VID(writer->config->tsdb->pVnode);

H
Hongze Cheng 已提交
819
  if (!writer->ctx->opened) {
H
Hongze Cheng 已提交
820 821 822 823
    code = tsdbSttFWriterDoOpen(writer);
    return code;
  }

H
Hongze Cheng 已提交
824 825
  code = tsdbSttFileDoWriteTSDataBlock(writer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
826

H
Hongze Cheng 已提交
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
  code = tsdbSttFileDoWriteStatisBlock(writer);
  TSDB_CHECK_CODE(code, lino, _exit);

#if 0
  writer->dData[0].aData[0][writer->dData[0].nRow] = tbid->suid;         // suid
  writer->dData[0].aData[1][writer->dData[0].nRow] = tbid->uid;          // uid
  writer->dData[0].aData[2][writer->dData[0].nRow] = pDelData->version;  // version
  writer->dData[0].aData[3][writer->dData[0].nRow] = pDelData->sKey;     // skey
  writer->dData[0].aData[4][writer->dData[0].nRow] = pDelData->eKey;     // ekey
  writer->dData[0].nRow++;

  if (writer->dData[0].nRow >= writer->config->maxRow) {
    return tsdbSttFileDoWriteDelBlock(writer);
  } else {
    return 0;
  }
#endif

_exit:
  if (code) {
    TSDB_ERROR_LOG(vid, lino, code);
  }
  return code;
H
Hongze Cheng 已提交
850
}