tsdbReaderWriter.c 54.7 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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/>.
 */

#include "tsdb.h"

H
Hongze Cheng 已提交
18 19
#define TSDB_FILE_DLMT ((uint32_t)0xF00AFA0F)

H
Hongze Cheng 已提交
20
// SDelFWriter ====================================================
H
Hongze Cheng 已提交
21 22
int32_t tsdbDelFWriterOpen(SDelFWriter **ppWriter, SDelFile *pFile, STsdb *pTsdb) {
  int32_t      code = 0;
H
Hongze Cheng 已提交
23 24
  char         fname[TSDB_FILENAME_LEN];
  char         hdr[TSDB_FHDR_SIZE] = {0};
H
Hongze Cheng 已提交
25
  SDelFWriter *pDelFWriter;
H
Hongze Cheng 已提交
26
  int64_t      n;
H
Hongze Cheng 已提交
27

H
Hongze Cheng 已提交
28
  // alloc
H
Hongze Cheng 已提交
29 30 31 32 33 34
  pDelFWriter = (SDelFWriter *)taosMemoryCalloc(1, sizeof(*pDelFWriter));
  if (pDelFWriter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  pDelFWriter->pTsdb = pTsdb;
H
Hongze Cheng 已提交
35 36 37
  pDelFWriter->fDel = *pFile;

  tsdbDelFileName(pTsdb, pFile, fname);
H
Hongze Cheng 已提交
38 39 40 41 42 43
  pDelFWriter->pWriteH = taosOpenFile(fname, TD_FILE_WRITE | TD_FILE_CREATE);
  if (pDelFWriter->pWriteH == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
44 45 46
  // update header
  n = taosWriteFile(pDelFWriter->pWriteH, &hdr, TSDB_FHDR_SIZE);
  if (n < 0) {
H
Hongze Cheng 已提交
47 48 49 50
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
51
  pDelFWriter->fDel.size = TSDB_FHDR_SIZE;
H
Hongze Cheng 已提交
52
  pDelFWriter->fDel.offset = 0;
H
more  
Hongze Cheng 已提交
53

H
Hongze Cheng 已提交
54
  *ppWriter = pDelFWriter;
H
Hongze Cheng 已提交
55 56 57 58
  return code;

_err:
  tsdbError("vgId:%d failed to open del file writer since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
59
  *ppWriter = NULL;
H
Hongze Cheng 已提交
60 61 62
  return code;
}

H
Hongze Cheng 已提交
63 64 65
int32_t tsdbDelFWriterClose(SDelFWriter **ppWriter, int8_t sync) {
  int32_t      code = 0;
  SDelFWriter *pWriter = *ppWriter;
H
Hongze Cheng 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78

  // sync
  if (sync && taosFsyncFile(pWriter->pWriteH) < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  // close
  if (taosCloseFile(&pWriter->pWriteH) < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
79
  *ppWriter = NULL;
H
Hongze Cheng 已提交
80 81 82 83
  return code;

_err:
  tsdbError("vgId:%d failed to close del file writer since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
84 85 86
  return code;
}

H
Hongze Cheng 已提交
87 88 89 90 91 92 93 94
int32_t tsdbWriteDelData(SDelFWriter *pWriter, SArray *aDelData, uint8_t **ppBuf, SDelIdx *pDelIdx) {
  int32_t       code = 0;
  uint8_t      *pBuf = NULL;
  int64_t       size;
  int64_t       n;
  SBlockDataHdr hdr = {.delimiter = TSDB_FILE_DLMT, .suid = pDelIdx->suid, .uid = pDelIdx->uid};

  if (!ppBuf) ppBuf = &pBuf;
H
Hongze Cheng 已提交
95

H
Hongze Cheng 已提交
96
  // prepare
H
Hongze Cheng 已提交
97 98 99 100 101
  size = sizeof(hdr);
  for (int32_t iDelData = 0; iDelData < taosArrayGetSize(aDelData); iDelData++) {
    size += tPutDelData(NULL, taosArrayGet(aDelData, iDelData));
  }
  size += sizeof(TSCKSUM);
H
Hongze Cheng 已提交
102 103

  // alloc
H
Hongze Cheng 已提交
104
  code = tRealloc(ppBuf, size);
H
Hongze Cheng 已提交
105 106 107
  if (code) goto _err;

  // build
H
Hongze Cheng 已提交
108 109 110 111
  n = 0;
  *(SBlockDataHdr *)(*ppBuf) = hdr;
  n += sizeof(hdr);
  for (int32_t iDelData = 0; iDelData < taosArrayGetSize(aDelData); iDelData++) {
H
Hongze Cheng 已提交
112
    n += tPutDelData(*ppBuf + n, taosArrayGet(aDelData, iDelData));
H
Hongze Cheng 已提交
113
  }
H
Hongze Cheng 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127
  taosCalcChecksumAppend(0, *ppBuf, size);

  ASSERT(n + sizeof(TSCKSUM) == size);

  // write
  n = taosWriteFile(pWriter->pWriteH, *ppBuf, size);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  ASSERT(n == size);

  // update
H
Hongze Cheng 已提交
128
  pDelIdx->offset = pWriter->fDel.size;
H
Hongze Cheng 已提交
129
  pDelIdx->size = size;
H
Hongze Cheng 已提交
130
  pWriter->fDel.size += size;
H
Hongze Cheng 已提交
131

H
Hongze Cheng 已提交
132
  tFree(pBuf);
H
Hongze Cheng 已提交
133 134 135 136
  return code;

_err:
  tsdbError("vgId:%d failed to write del data since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
137
  tFree(pBuf);
H
Hongze Cheng 已提交
138 139 140
  return code;
}

H
Hongze Cheng 已提交
141
int32_t tsdbWriteDelIdx(SDelFWriter *pWriter, SArray *aDelIdx, uint8_t **ppBuf) {
H
Hongze Cheng 已提交
142
  int32_t  code = 0;
H
Hongze Cheng 已提交
143 144
  int64_t  size;
  int64_t  n;
H
Hongze Cheng 已提交
145
  uint8_t *pBuf = NULL;
H
Hongze Cheng 已提交
146 147 148
  SDelIdx *pDelIdx;

  if (!ppBuf) ppBuf = &pBuf;
H
Hongze Cheng 已提交
149

H
Hongze Cheng 已提交
150
  // prepare
H
Hongze Cheng 已提交
151
  size = 0;
H
Hongze Cheng 已提交
152
  size += tPutU32(NULL, TSDB_FILE_DLMT);
H
Hongze Cheng 已提交
153 154 155 156
  for (int32_t iDelIdx = 0; iDelIdx < taosArrayGetSize(aDelIdx); iDelIdx++) {
    size += tPutDelIdx(NULL, taosArrayGet(aDelIdx, iDelIdx));
  }
  size += sizeof(TSCKSUM);
H
Hongze Cheng 已提交
157 158

  // alloc
H
Hongze Cheng 已提交
159
  code = tRealloc(ppBuf, size);
H
Hongze Cheng 已提交
160
  if (code) goto _err;
H
Hongze Cheng 已提交
161

H
Hongze Cheng 已提交
162
  // build
H
Hongze Cheng 已提交
163
  n = 0;
H
Hongze Cheng 已提交
164
  n += tPutU32(*ppBuf + n, TSDB_FILE_DLMT);
H
Hongze Cheng 已提交
165 166 167
  for (int32_t iDelIdx = 0; iDelIdx < taosArrayGetSize(aDelIdx); iDelIdx++) {
    n += tPutDelIdx(*ppBuf + n, taosArrayGet(aDelIdx, iDelIdx));
  }
H
Hongze Cheng 已提交
168 169
  taosCalcChecksumAppend(0, *ppBuf, size);

H
Hongze Cheng 已提交
170 171
  ASSERT(n + sizeof(TSCKSUM) == size);

H
Hongze Cheng 已提交
172
  // write
H
Hongze Cheng 已提交
173 174
  n = taosWriteFile(pWriter->pWriteH, *ppBuf, size);
  if (n < 0) {
H
Hongze Cheng 已提交
175 176 177 178
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
179
  // update
H
Hongze Cheng 已提交
180 181
  pWriter->fDel.offset = pWriter->fDel.size;
  pWriter->fDel.size += size;
H
Hongze Cheng 已提交
182

H
Hongze Cheng 已提交
183
  tFree(pBuf);
H
Hongze Cheng 已提交
184 185 186
  return code;

_err:
H
more  
Hongze Cheng 已提交
187
  tsdbError("vgId:%d write del idx failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
188
  tFree(pBuf);
H
Hongze Cheng 已提交
189 190 191
  return code;
}

H
Hongze Cheng 已提交
192 193 194 195 196
int32_t tsdbUpdateDelFileHdr(SDelFWriter *pWriter) {
  int32_t code = 0;
  char    hdr[TSDB_FHDR_SIZE];
  int64_t size = TSDB_FHDR_SIZE;
  int64_t n;
H
Hongze Cheng 已提交
197 198

  // build
H
Hongze Cheng 已提交
199 200 201
  memset(hdr, 0, size);
  tPutDelFile(hdr, &pWriter->fDel);
  taosCalcChecksumAppend(0, hdr, size);
H
Hongze Cheng 已提交
202 203 204 205 206 207 208 209

  // seek
  if (taosLSeekFile(pWriter->pWriteH, 0, SEEK_SET) < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  // write
H
Hongze Cheng 已提交
210 211
  n = taosWriteFile(pWriter->pWriteH, hdr, size);
  if (n < 0) {
H
Hongze Cheng 已提交
212 213 214 215
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
216 217 218
  return code;

_err:
H
Hongze Cheng 已提交
219
  tsdbError("vgId:%d update del file hdr failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
220 221 222 223 224
  return code;
}

// SDelFReader ====================================================
struct SDelFReader {
H
Hongze Cheng 已提交
225
  STsdb    *pTsdb;
H
Hongze Cheng 已提交
226
  SDelFile  fDel;
H
Hongze Cheng 已提交
227
  TdFilePtr pReadH;
H
refact  
Hongze Cheng 已提交
228
};
H
Hongze Cheng 已提交
229

H
Hongze Cheng 已提交
230 231
int32_t tsdbDelFReaderOpen(SDelFReader **ppReader, SDelFile *pFile, STsdb *pTsdb, uint8_t **ppBuf) {
  int32_t      code = 0;
H
Hongze Cheng 已提交
232
  char         fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
233
  SDelFReader *pDelFReader;
H
Hongze Cheng 已提交
234
  int64_t      n;
H
Hongze Cheng 已提交
235 236 237 238 239 240 241 242 243 244

  // alloc
  pDelFReader = (SDelFReader *)taosMemoryCalloc(1, sizeof(*pDelFReader));
  if (pDelFReader == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  // open impl
  pDelFReader->pTsdb = pTsdb;
H
Hongze Cheng 已提交
245 246 247
  pDelFReader->fDel = *pFile;

  tsdbDelFileName(pTsdb, pFile, fname);
H
Hongze Cheng 已提交
248
  pDelFReader->pReadH = taosOpenFile(fname, TD_FILE_READ);
H
Hongze Cheng 已提交
249
  if (pDelFReader->pReadH == NULL) {
H
Hongze Cheng 已提交
250 251 252 253 254
    code = TAOS_SYSTEM_ERROR(errno);
    taosMemoryFree(pDelFReader);
    goto _err;
  }

H
Hongze Cheng 已提交
255
#if 0
H
Hongze Cheng 已提交
256 257
  // load and check hdr if buffer is given
  if (ppBuf) {
H
Hongze Cheng 已提交
258
    code = tRealloc(ppBuf, TSDB_FHDR_SIZE);
H
Hongze Cheng 已提交
259 260 261 262
    if (code) {
      goto _err;
    }

H
Hongze Cheng 已提交
263 264 265 266 267
    n = taosReadFile(pDelFReader->pReadH, *ppBuf, TSDB_FHDR_SIZE);
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    } else if (n < TSDB_FHDR_SIZE) {
H
Hongze Cheng 已提交
268 269 270 271 272 273 274 275 276 277 278
      code = TSDB_CODE_FILE_CORRUPTED;
      goto _err;
    }

    if (!taosCheckChecksumWhole(*ppBuf, TSDB_FHDR_SIZE)) {
      code = TSDB_CODE_FILE_CORRUPTED;
      goto _err;
    }

    // TODO: check the content
  }
H
Hongze Cheng 已提交
279
#endif
H
Hongze Cheng 已提交
280 281 282 283 284 285

_exit:
  *ppReader = pDelFReader;
  return code;

_err:
H
Hongze Cheng 已提交
286
  tsdbError("vgId:%d del file reader open failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
287
  *ppReader = NULL;
H
Hongze Cheng 已提交
288 289 290
  return code;
}

H
Hongze Cheng 已提交
291 292 293
int32_t tsdbDelFReaderClose(SDelFReader **ppReader) {
  int32_t      code = 0;
  SDelFReader *pReader = *ppReader;
H
Hongze Cheng 已提交
294 295

  if (pReader) {
H
Hongze Cheng 已提交
296 297 298 299
    if (taosCloseFile(&pReader->pReadH) < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _exit;
    }
H
Hongze Cheng 已提交
300 301
    taosMemoryFree(pReader);
  }
H
Hongze Cheng 已提交
302
  *ppReader = NULL;
H
Hongze Cheng 已提交
303

H
Hongze Cheng 已提交
304
_exit:
H
Hongze Cheng 已提交
305 306 307
  return code;
}

H
Hongze Cheng 已提交
308 309 310 311 312 313 314 315 316 317
int32_t tsdbReadDelData(SDelFReader *pReader, SDelIdx *pDelIdx, SArray *aDelData, uint8_t **ppBuf) {
  int32_t        code = 0;
  int64_t        offset = pDelIdx->offset;
  int64_t        size = pDelIdx->size;
  int64_t        n;
  uint8_t       *pBuf = NULL;
  SBlockDataHdr *pHdr;
  SDelData      *pDelData = &(SDelData){0};

  if (!ppBuf) ppBuf = &pBuf;
H
Hongze Cheng 已提交
318 319

  // seek
H
Hongze Cheng 已提交
320
  if (taosLSeekFile(pReader->pReadH, offset, SEEK_SET) < 0) {
H
Hongze Cheng 已提交
321 322 323 324 325
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  // alloc
H
Hongze Cheng 已提交
326
  code = tRealloc(ppBuf, size);
H
Hongze Cheng 已提交
327 328 329
  if (code) goto _err;

  // read
H
Hongze Cheng 已提交
330
  n = taosReadFile(pReader->pReadH, *ppBuf, size);
H
Hongze Cheng 已提交
331 332 333
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
H
Hongze Cheng 已提交
334
  } else if (n < size) {
H
Hongze Cheng 已提交
335 336
    code = TSDB_CODE_FILE_CORRUPTED;
    goto _err;
H
Hongze Cheng 已提交
337 338 339
  }

  // check
H
Hongze Cheng 已提交
340
  if (!taosCheckChecksumWhole(*ppBuf, size)) {
H
Hongze Cheng 已提交
341 342 343 344
    code = TSDB_CODE_FILE_CORRUPTED;
    goto _err;
  }

H
Hongze Cheng 已提交
345 346
  // // decode
  n = 0;
H
Hongze Cheng 已提交
347 348 349 350 351
  pHdr = (SBlockDataHdr *)(*ppBuf + n);
  ASSERT(pHdr->delimiter == TSDB_FILE_DLMT);
  ASSERT(pHdr->suid == pDelIdx->suid);
  ASSERT(pHdr->uid == pDelIdx->uid);
  n += sizeof(*pHdr);
H
Hongze Cheng 已提交
352
  taosArrayClear(aDelData);
H
Hongze Cheng 已提交
353 354
  while (n < size - sizeof(TSCKSUM)) {
    n += tGetDelData(*ppBuf + n, pDelData);
H
Hongze Cheng 已提交
355 356 357 358 359

    if (taosArrayPush(aDelData, pDelData) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
360
  }
H
Hongze Cheng 已提交
361

H
Hongze Cheng 已提交
362 363
  ASSERT(n == size - sizeof(TSCKSUM));

H
Hongze Cheng 已提交
364
  tFree(pBuf);
H
Hongze Cheng 已提交
365 366 367 368
  return code;

_err:
  tsdbError("vgId:%d read del data failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
369
  tFree(pBuf);
H
Hongze Cheng 已提交
370 371 372
  return code;
}

H
Hongze Cheng 已提交
373
int32_t tsdbReadDelIdx(SDelFReader *pReader, SArray *aDelIdx, uint8_t **ppBuf) {
H
Hongze Cheng 已提交
374 375
  int32_t  code = 0;
  int32_t  n;
H
Hongze Cheng 已提交
376 377
  int64_t  offset = pReader->fDel.offset;
  int64_t  size = pReader->fDel.size - offset;
H
Hongze Cheng 已提交
378
  uint32_t delimiter;
H
Hongze Cheng 已提交
379
  uint8_t *pBuf = NULL;
380
  SDelIdx *pDelIdx = &(SDelIdx){0};
H
Hongze Cheng 已提交
381

H
Hongze Cheng 已提交
382
  if (!ppBuf) ppBuf = &pBuf;
H
more  
Hongze Cheng 已提交
383

H
Hongze Cheng 已提交
384
  // seek
H
Hongze Cheng 已提交
385
  if (taosLSeekFile(pReader->pReadH, offset, SEEK_SET) < 0) {
H
Hongze Cheng 已提交
386 387 388 389
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
390
  // alloc
H
Hongze Cheng 已提交
391
  code = tRealloc(ppBuf, size);
H
Hongze Cheng 已提交
392
  if (code) goto _err;
H
Hongze Cheng 已提交
393

H
Hongze Cheng 已提交
394
  // read
H
Hongze Cheng 已提交
395 396
  n = taosReadFile(pReader->pReadH, *ppBuf, size);
  if (n < 0) {
H
Hongze Cheng 已提交
397
    code = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
398
    goto _err;
H
Hongze Cheng 已提交
399 400 401
  } else if (n < size) {
    code = TSDB_CODE_FILE_CORRUPTED;
    goto _err;
H
Hongze Cheng 已提交
402 403 404 405 406 407 408 409 410
  }

  // check
  if (!taosCheckChecksumWhole(*ppBuf, size)) {
    code = TSDB_CODE_FILE_CORRUPTED;
    goto _err;
  }

  // decode
H
Hongze Cheng 已提交
411 412 413
  n = 0;
  n += tGetU32(*ppBuf + n, &delimiter);
  ASSERT(delimiter == TSDB_FILE_DLMT);
H
Hongze Cheng 已提交
414 415 416 417 418 419 420 421 422 423 424 425

  taosArrayClear(aDelIdx);
  while (n < size - sizeof(TSCKSUM)) {
    n += tGetDelIdx(*ppBuf + n, pDelIdx);

    if (taosArrayPush(aDelIdx, pDelIdx) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
  }

  ASSERT(n == size - sizeof(TSCKSUM));
H
Hongze Cheng 已提交
426

H
Hongze Cheng 已提交
427
  tFree(pBuf);
H
Hongze Cheng 已提交
428 429 430
  return code;

_err:
H
Hongze Cheng 已提交
431
  tsdbError("vgId:%d read del idx failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
432
  tFree(pBuf);
H
Hongze Cheng 已提交
433
  return code;
H
Hongze Cheng 已提交
434 435 436 437 438 439
}

// SDataFReader ====================================================
struct SDataFReader {
  STsdb     *pTsdb;
  SDFileSet *pSet;
H
Hongze Cheng 已提交
440 441 442 443
  TdFilePtr  pHeadFD;
  TdFilePtr  pDataFD;
  TdFilePtr  pLastFD;
  TdFilePtr  pSmaFD;
H
Hongze Cheng 已提交
444 445 446
};

int32_t tsdbDataFReaderOpen(SDataFReader **ppReader, STsdb *pTsdb, SDFileSet *pSet) {
H
Hongze Cheng 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
  int32_t       code = 0;
  SDataFReader *pReader;
  char          fname[TSDB_FILENAME_LEN];

  // alloc
  pReader = (SDataFReader *)taosMemoryCalloc(1, sizeof(*pReader));
  if (pReader == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  pReader->pTsdb = pTsdb;
  pReader->pSet = pSet;

  // open impl
  // head
H
Hongze Cheng 已提交
462
  tsdbHeadFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pHeadF, fname);
H
Hongze Cheng 已提交
463 464 465 466 467 468 469
  pReader->pHeadFD = taosOpenFile(fname, TD_FILE_READ);
  if (pReader->pHeadFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  // data
H
Hongze Cheng 已提交
470
  tsdbDataFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pDataF, fname);
H
Hongze Cheng 已提交
471 472 473 474 475 476 477
  pReader->pDataFD = taosOpenFile(fname, TD_FILE_READ);
  if (pReader->pDataFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  // last
H
Hongze Cheng 已提交
478
  tsdbLastFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pLastF, fname);
H
Hongze Cheng 已提交
479 480 481 482 483 484 485
  pReader->pLastFD = taosOpenFile(fname, TD_FILE_READ);
  if (pReader->pLastFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  // sma
H
Hongze Cheng 已提交
486
  tsdbSmaFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pSmaF, fname);
H
Hongze Cheng 已提交
487 488 489 490 491 492 493 494 495 496 497 498
  pReader->pSmaFD = taosOpenFile(fname, TD_FILE_READ);
  if (pReader->pSmaFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  *ppReader = pReader;
  return code;

_err:
  tsdbError("vgId:%d tsdb data file reader open failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
  *ppReader = NULL;
H
Hongze Cheng 已提交
499 500 501
  return code;
}

H
Hongze Cheng 已提交
502
int32_t tsdbDataFReaderClose(SDataFReader **ppReader) {
H
Hongze Cheng 已提交
503
  int32_t code = 0;
H
Hongze Cheng 已提交
504
  if (*ppReader == NULL) goto _exit;
H
Hongze Cheng 已提交
505

H
Hongze Cheng 已提交
506
  if (taosCloseFile(&(*ppReader)->pHeadFD) < 0) {
H
Hongze Cheng 已提交
507 508 509 510
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
511
  if (taosCloseFile(&(*ppReader)->pDataFD) < 0) {
H
Hongze Cheng 已提交
512 513 514 515
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
516
  if (taosCloseFile(&(*ppReader)->pLastFD) < 0) {
H
Hongze Cheng 已提交
517 518 519 520
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
521
  if (taosCloseFile(&(*ppReader)->pSmaFD) < 0) {
H
Hongze Cheng 已提交
522 523 524 525
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
526
  taosMemoryFree(*ppReader);
H
Hongze Cheng 已提交
527 528

_exit:
H
Hongze Cheng 已提交
529
  *ppReader = NULL;
H
Hongze Cheng 已提交
530 531 532
  return code;

_err:
H
Hongze Cheng 已提交
533
  tsdbError("vgId:%d data file reader close failed since %s", TD_VID((*ppReader)->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
534 535 536
  return code;
}

H
Hongze Cheng 已提交
537 538
int32_t tsdbReadBlockIdx(SDataFReader *pReader, SArray *aBlockIdx, uint8_t **ppBuf) {
  int32_t   code = 0;
H
Hongze Cheng 已提交
539 540
  int64_t   offset = pReader->pSet->pHeadF->offset;
  int64_t   size = pReader->pSet->pHeadF->size - offset;
H
Hongze Cheng 已提交
541 542 543 544 545 546
  uint8_t  *pBuf = NULL;
  int64_t   n;
  uint32_t  delimiter;
  SBlockIdx blockIdx;

  if (!ppBuf) ppBuf = &pBuf;
H
Hongze Cheng 已提交
547 548

  // alloc
H
Hongze Cheng 已提交
549
  code = tRealloc(ppBuf, size);
H
Hongze Cheng 已提交
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
  if (code) goto _err;

  // seek
  if (taosLSeekFile(pReader->pHeadFD, offset, SEEK_SET) < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  // read
  n = taosReadFile(pReader->pHeadFD, *ppBuf, size);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  } else if (n < size) {
    code = TSDB_CODE_FILE_CORRUPTED;
    goto _err;
  }

  // check
  if (!taosCheckChecksumWhole(*ppBuf, size)) {
    code = TSDB_CODE_FILE_CORRUPTED;
    goto _err;
  }

  // decode
H
Hongze Cheng 已提交
575
  n = 0;
H
Hongze Cheng 已提交
576
  n = tGetU32(*ppBuf + n, &delimiter);
H
Hongze Cheng 已提交
577
  ASSERT(delimiter == TSDB_FILE_DLMT);
H
Hongze Cheng 已提交
578 579 580 581 582 583 584 585 586 587 588

  taosArrayClear(aBlockIdx);
  while (n < size - sizeof(TSCKSUM)) {
    n += tGetBlockIdx(*ppBuf + n, &blockIdx);

    if (taosArrayPush(aBlockIdx, &blockIdx) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
  }

H
Hongze Cheng 已提交
589 590
  ASSERT(n + sizeof(TSCKSUM) == size);

H
Hongze Cheng 已提交
591
  tFree(pBuf);
H
Hongze Cheng 已提交
592 593 594 595
  return code;

_err:
  tsdbError("vgId:%d read block idx failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
596
  tFree(pBuf);
H
Hongze Cheng 已提交
597 598 599
  return code;
}

H
Hongze Cheng 已提交
600
int32_t tsdbReadBlock(SDataFReader *pReader, SBlockIdx *pBlockIdx, SMapData *mBlock, uint8_t **ppBuf) {
H
Hongze Cheng 已提交
601 602 603
  int32_t       code = 0;
  int64_t       offset = pBlockIdx->offset;
  int64_t       size = pBlockIdx->size;
H
Hongze Cheng 已提交
604
  uint8_t      *pBuf = NULL;
H
Hongze Cheng 已提交
605
  int64_t       n;
H
Hongze Cheng 已提交
606
  int64_t       tn;
H
Hongze Cheng 已提交
607
  SBlockDataHdr hdr;
H
Hongze Cheng 已提交
608

H
Hongze Cheng 已提交
609 610
  if (!ppBuf) ppBuf = &pBuf;

H
Hongze Cheng 已提交
611
  // alloc
H
Hongze Cheng 已提交
612
  code = tRealloc(ppBuf, size);
H
Hongze Cheng 已提交
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
  if (code) goto _err;

  // seek
  if (taosLSeekFile(pReader->pHeadFD, offset, SEEK_SET) < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  // read
  n = taosReadFile(pReader->pHeadFD, *ppBuf, size);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  } else if (n < size) {
    code = TSDB_CODE_FILE_CORRUPTED;
    goto _err;
  }

  // check
  if (!taosCheckChecksumWhole(*ppBuf, size)) {
    code = TSDB_CODE_FILE_CORRUPTED;
    goto _err;
  }

  // decode
H
Hongze Cheng 已提交
638 639 640 641 642 643
  hdr = *(SBlockDataHdr *)(*ppBuf);
  ASSERT(hdr.delimiter == TSDB_FILE_DLMT);
  ASSERT(hdr.suid == pBlockIdx->suid);
  ASSERT(hdr.uid == pBlockIdx->uid);

  n = sizeof(hdr);
H
Hongze Cheng 已提交
644 645 646 647 648 649
  tn = tGetMapData(*ppBuf + n, mBlock);
  if (tn < 0) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  n += tn;
H
Hongze Cheng 已提交
650 651
  ASSERT(n + sizeof(TSCKSUM) == size);

H
Hongze Cheng 已提交
652
  tFree(pBuf);
H
Hongze Cheng 已提交
653 654 655 656
  return code;

_err:
  tsdbError("vgId:%d read block failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
657
  tFree(pBuf);
H
Hongze Cheng 已提交
658 659 660
  return code;
}

H
Hongze Cheng 已提交
661
static int32_t tsdbReadBlockDataKey(SBlockData *pBlockData, SSubBlock *pSubBlock, uint8_t *pBuf, uint8_t **ppBuf) {
H
Hongze Cheng 已提交
662
  int32_t code = 0;
H
refact  
Hongze Cheng 已提交
663
  int64_t size = pSubBlock->szVersion + pSubBlock->szTSKEY + sizeof(TSCKSUM);
H
Hongze Cheng 已提交
664 665 666 667 668 669 670
  int64_t n;

  if (!taosCheckChecksumWhole(pBuf, size)) {
    code = TSDB_CODE_FILE_CORRUPTED;
    goto _err;
  }

H
Hongze Cheng 已提交
671
  code = tRealloc((uint8_t **)&pBlockData->aVersion, sizeof(int64_t) * pSubBlock->nRow);
H
Hongze Cheng 已提交
672
  if (code) goto _err;
H
Hongze Cheng 已提交
673
  code = tRealloc((uint8_t **)&pBlockData->aTSKEY, sizeof(TSKEY) * pSubBlock->nRow);
H
Hongze Cheng 已提交
674 675 676
  if (code) goto _err;

  if (pSubBlock->cmprAlg == NO_COMPRESSION) {
H
refact  
Hongze Cheng 已提交
677 678
    ASSERT(pSubBlock->szVersion == sizeof(int64_t) * pSubBlock->nRow);
    ASSERT(pSubBlock->szTSKEY == sizeof(TSKEY) * pSubBlock->nRow);
H
Hongze Cheng 已提交
679 680

    // VERSION
H
refact  
Hongze Cheng 已提交
681
    memcpy(pBlockData->aVersion, pBuf, pSubBlock->szVersion);
H
Hongze Cheng 已提交
682 683

    // TSKEY
H
refact  
Hongze Cheng 已提交
684
    memcpy(pBlockData->aTSKEY, pBuf + pSubBlock->szVersion, pSubBlock->szTSKEY);
H
Hongze Cheng 已提交
685 686 687
  } else {
    size = sizeof(int64_t) * pSubBlock->nRow + COMP_OVERFLOW_BYTES;
    if (pSubBlock->cmprAlg == TWO_STAGE_COMP) {
H
Hongze Cheng 已提交
688
      code = tRealloc(ppBuf, size);
H
Hongze Cheng 已提交
689 690 691 692
      if (code) goto _err;
    }

    // VERSION
H
refact  
Hongze Cheng 已提交
693
    n = tsDecompressBigint(pBuf, pSubBlock->szVersion, pSubBlock->nRow, (char *)pBlockData->aVersion,
H
Hongze Cheng 已提交
694 695 696 697 698 699 700
                           sizeof(int64_t) * pSubBlock->nRow, pSubBlock->cmprAlg, *ppBuf, size);
    if (n < 0) {
      code = TSDB_CODE_COMPRESS_ERROR;
      goto _err;
    }

    // TSKEY
H
Hongze Cheng 已提交
701 702 703
    n = tsDecompressTimestamp(pBuf + pSubBlock->szVersion, pSubBlock->szTSKEY, pSubBlock->nRow,
                              (char *)pBlockData->aTSKEY, sizeof(TSKEY) * pSubBlock->nRow, pSubBlock->cmprAlg, *ppBuf,
                              size);
H
Hongze Cheng 已提交
704 705 706 707 708 709 710 711 712 713 714 715
    if (n < 0) {
      code = TSDB_CODE_COMPRESS_ERROR;
      goto _err;
    }
  }

  return code;

_err:
  return code;
}

H
Hongze Cheng 已提交
716 717
static int32_t tsdbReadColDataImpl(SSubBlock *pSubBlock, SBlockCol *pBlockCol, SColData *pColData, uint8_t *pBuf,
                                   uint8_t **ppBuf) {
H
Hongze Cheng 已提交
718 719 720 721
  int32_t code = 0;
  int64_t size;
  int64_t n;

H
Hongze Cheng 已提交
722
  if (!taosCheckChecksumWhole(pBuf, pBlockCol->szBitmap + pBlockCol->szOffset + pBlockCol->szValue + sizeof(TSCKSUM))) {
H
Hongze Cheng 已提交
723 724 725 726 727 728 729
    code = TSDB_CODE_FILE_CORRUPTED;
    goto _err;
  }

  pColData->nVal = pSubBlock->nRow;
  pColData->flag = pBlockCol->flag;

H
Hongze Cheng 已提交
730
  // BITMAP
H
Hongze Cheng 已提交
731
  if (pBlockCol->flag != HAS_VALUE) {
H
Hongze Cheng 已提交
732 733 734
    ASSERT(pBlockCol->szBitmap);

    size = BIT2_SIZE(pColData->nVal);
H
Hongze Cheng 已提交
735
    code = tRealloc(&pColData->pBitMap, size);
H
Hongze Cheng 已提交
736 737
    if (code) goto _err;

H
Hongze Cheng 已提交
738
    code = tRealloc(ppBuf, size + COMP_OVERFLOW_BYTES);
H
Hongze Cheng 已提交
739
    if (code) goto _err;
H
Hongze Cheng 已提交
740

H
Hongze Cheng 已提交
741 742 743 744 745 746 747 748
    n = tsDecompressTinyint(pBuf, pBlockCol->szBitmap, size, pColData->pBitMap, size, TWO_STAGE_COMP, *ppBuf,
                            size + COMP_OVERFLOW_BYTES);
    if (n <= 0) {
      code = TSDB_CODE_COMPRESS_ERROR;
      goto _err;
    }

    ASSERT(n == size);
H
Hongze Cheng 已提交
749
  } else {
H
Hongze Cheng 已提交
750
    ASSERT(pBlockCol->szBitmap == 0);
H
Hongze Cheng 已提交
751
  }
H
Hongze Cheng 已提交
752 753 754 755 756
  pBuf = pBuf + pBlockCol->szBitmap;

  // OFFSET
  if (IS_VAR_DATA_TYPE(pColData->type)) {
    ASSERT(pBlockCol->szOffset);
H
Hongze Cheng 已提交
757

H
Hongze Cheng 已提交
758
    size = sizeof(int32_t) * pColData->nVal;
H
Hongze Cheng 已提交
759
    code = tRealloc((uint8_t **)&pColData->aOffset, size);
H
Hongze Cheng 已提交
760 761
    if (code) goto _err;

H
Hongze Cheng 已提交
762
    code = tRealloc(ppBuf, size + COMP_OVERFLOW_BYTES);
H
Hongze Cheng 已提交
763 764 765 766 767 768 769 770 771 772
    if (code) goto _err;

    n = tsDecompressInt(pBuf, pBlockCol->szOffset, pColData->nVal, (char *)pColData->aOffset, size, TWO_STAGE_COMP,
                        *ppBuf, size + COMP_OVERFLOW_BYTES);
    if (n <= 0) {
      code = TSDB_CODE_COMPRESS_ERROR;
      goto _err;
    }

    ASSERT(n == size);
H
Hongze Cheng 已提交
773
  } else {
H
Hongze Cheng 已提交
774
    ASSERT(pBlockCol->szOffset == 0);
H
Hongze Cheng 已提交
775
  }
H
Hongze Cheng 已提交
776 777 778 779 780
  pBuf = pBuf + pBlockCol->szOffset;

  // VALUE
  pColData->nData = pBlockCol->szOrigin;

H
Hongze Cheng 已提交
781
  code = tRealloc(&pColData->pData, pColData->nData);
H
Hongze Cheng 已提交
782 783 784 785 786 787
  if (code) goto _err;

  if (pSubBlock->cmprAlg == NO_COMPRESSION) {
    memcpy(pColData->pData, pBuf, pColData->nData);
  } else {
    if (pSubBlock->cmprAlg == TWO_STAGE_COMP) {
H
Hongze Cheng 已提交
788
      code = tRealloc(ppBuf, pColData->nData + COMP_OVERFLOW_BYTES);
H
Hongze Cheng 已提交
789 790 791
      if (code) goto _err;
    }

H
Hongze Cheng 已提交
792 793 794
    n = tDataTypes[pBlockCol->type].decompFunc(pBuf, pBlockCol->szValue, pSubBlock->nRow, pColData->pData,
                                               pColData->nData, pSubBlock->cmprAlg, *ppBuf,
                                               pColData->nData + COMP_OVERFLOW_BYTES);
H
Hongze Cheng 已提交
795 796 797 798 799 800 801 802 803 804 805 806 807 808
    if (n < 0) {
      code = TSDB_CODE_COMPRESS_ERROR;
      goto _err;
    }

    ASSERT(n == pColData->nData);
  }

  return code;

_err:
  return code;
}

H
Hongze Cheng 已提交
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
static int32_t tsdbReadBlockCol(SSubBlock *pSubBlock, uint8_t *p, SArray *aBlockCol) {
  int32_t    code = 0;
  int32_t    n = 0;
  SBlockCol  blockCol;
  SBlockCol *pBlockCol = &blockCol;

  if (!taosCheckChecksumWhole(p, pSubBlock->szBlockCol + sizeof(TSCKSUM))) {
    code = TSDB_CODE_FILE_CORRUPTED;
    goto _err;
  }

  n += sizeof(SBlockDataHdr);
  while (n < pSubBlock->szBlockCol) {
    n += tGetBlockCol(p + n, pBlockCol);

    if (taosArrayPush(aBlockCol, pBlockCol) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
  }

  ASSERT(n == pSubBlock->szBlockCol);

  return code;

_err:
  return code;
}

static int32_t tsdbReadSubColData(SDataFReader *pReader, SBlockIdx *pBlockIdx, SBlock *pBlock, int32_t iSubBlock,
                                  int16_t *aColId, int32_t nCol, SBlockData *pBlockData, uint8_t **ppBuf1,
                                  uint8_t **ppBuf2) {
H
Hongze Cheng 已提交
841 842
  TdFilePtr  pFD = pBlock->last ? pReader->pLastFD : pReader->pDataFD;
  SSubBlock *pSubBlock = &pBlock->aSubBlock[iSubBlock];
H
Hongze Cheng 已提交
843
  SArray    *aBlockCol = NULL;
H
Hongze Cheng 已提交
844 845 846 847
  int32_t    code = 0;
  int64_t    offset;
  int64_t    size;
  int64_t    n;
H
Hongze Cheng 已提交
848

H
Hongze Cheng 已提交
849 850
  tBlockDataReset(pBlockData);
  pBlockData->nRow = pSubBlock->nRow;
H
Hongze Cheng 已提交
851

H
Hongze Cheng 已提交
852 853 854 855 856 857 858 859 860
  // TSDBKEY and SBlockCol
  if (nCol == 1) {
    offset = pSubBlock->offset + pSubBlock->szBlockCol + sizeof(TSCKSUM);
    size = pSubBlock->szVersion + pSubBlock->szTSKEY + sizeof(TSCKSUM);
  } else {
    offset = pSubBlock->offset;
    size = pSubBlock->szBlockCol + sizeof(TSCKSUM) + pSubBlock->szVersion + pSubBlock->szTSKEY + sizeof(TSCKSUM);
  }

H
Hongze Cheng 已提交
861
  code = tRealloc(ppBuf1, size);
H
Hongze Cheng 已提交
862
  if (code) goto _err;
H
Hongze Cheng 已提交
863

H
Hongze Cheng 已提交
864 865 866 867 868
  n = taosLSeekFile(pFD, offset, SEEK_SET);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
H
Hongze Cheng 已提交
869

H
Hongze Cheng 已提交
870 871 872 873 874 875 876 877
  n = taosReadFile(pFD, *ppBuf1, size);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  } else if (n < size) {
    code = TSDB_CODE_FILE_CORRUPTED;
    goto _err;
  }
H
Hongze Cheng 已提交
878

H
Hongze Cheng 已提交
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
  if (nCol == 1) {
    code = tsdbReadBlockDataKey(pBlockData, pSubBlock, *ppBuf1, ppBuf2);
    if (code) goto _err;

    goto _exit;
  } else {
    aBlockCol = taosArrayInit(0, sizeof(SBlockCol));
    if (aBlockCol == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }

    code = tsdbReadBlockCol(pSubBlock, *ppBuf1, aBlockCol);
    if (code) goto _err;

    code = tsdbReadBlockDataKey(pBlockData, pSubBlock, *ppBuf1 + pSubBlock->szBlockCol + sizeof(TSCKSUM), ppBuf2);
    if (code) goto _err;
  }
H
Hongze Cheng 已提交
897

H
Hongze Cheng 已提交
898
  for (int32_t iCol = 1; iCol < nCol; iCol++) {
H
Hongze Cheng 已提交
899 900 901 902 903 904 905
    void *p = taosArraySearch(aBlockCol, &(SBlockCol){.cid = aColId[iCol]}, tBlockColCmprFn, TD_EQ);

    if (p) {
      SBlockCol *pBlockCol = (SBlockCol *)p;
      SColData  *pColData;

      ASSERT(pBlockCol->flag && pBlockCol->flag != HAS_NONE);
H
Hongze Cheng 已提交
906

H
Hongze Cheng 已提交
907
      code = tBlockDataAddColData(pBlockData, taosArrayGetSize(pBlockData->aIdx), &pColData);
H
Hongze Cheng 已提交
908
      if (code) goto _err;
H
Hongze Cheng 已提交
909

H
Hongze Cheng 已提交
910
      tColDataInit(pColData, pBlockCol->cid, pBlockCol->type, pBlockCol->smaOn);
H
Hongze Cheng 已提交
911 912 913 914 915 916
      if (pBlockCol->flag == HAS_NULL) {
        for (int32_t iRow = 0; iRow < pSubBlock->nRow; iRow++) {
          code = tColDataAppendValue(pColData, &COL_VAL_NULL(pBlockCol->cid, pBlockCol->type));
          if (code) goto _err;
        }
      } else {
H
Hongze Cheng 已提交
917 918 919
        offset = pSubBlock->offset + pSubBlock->szBlockCol + sizeof(TSCKSUM) + pSubBlock->szVersion +
                 pSubBlock->szTSKEY + sizeof(TSCKSUM) + pBlockCol->offset;
        size = pBlockCol->szBitmap + pBlockCol->szOffset + pBlockCol->szValue + sizeof(TSCKSUM);
H
Hongze Cheng 已提交
920

H
Hongze Cheng 已提交
921
        code = tRealloc(ppBuf1, size);
H
Hongze Cheng 已提交
922 923
        if (code) goto _err;

H
Hongze Cheng 已提交
924 925 926 927 928 929
        // seek
        n = taosLSeekFile(pFD, offset, SEEK_SET);
        if (n < 0) {
          code = TAOS_SYSTEM_ERROR(errno);
          goto _err;
        }
H
Hongze Cheng 已提交
930

H
Hongze Cheng 已提交
931 932 933 934 935 936 937 938
        // read
        n = taosReadFile(pFD, *ppBuf1, size);
        if (n < 0) {
          code = TAOS_SYSTEM_ERROR(errno);
          goto _err;
        } else if (n < size) {
          code = TSDB_CODE_FILE_CORRUPTED;
          goto _err;
H
Hongze Cheng 已提交
939
        }
H
Hongze Cheng 已提交
940

H
Hongze Cheng 已提交
941
        code = tsdbReadColDataImpl(pSubBlock, pBlockCol, pColData, *ppBuf1, ppBuf2);
H
Hongze Cheng 已提交
942
        if (code) goto _err;
H
Hongze Cheng 已提交
943 944 945
      }
    }
  }
H
Hongze Cheng 已提交
946 947 948

_exit:
  taosArrayDestroy(aBlockCol);
H
Hongze Cheng 已提交
949 950 951
  return code;

_err:
H
Hongze Cheng 已提交
952
  taosArrayDestroy(aBlockCol);
H
Hongze Cheng 已提交
953 954 955 956 957 958 959 960 961
  return code;
}

int32_t tsdbReadColData(SDataFReader *pReader, SBlockIdx *pBlockIdx, SBlock *pBlock, int16_t *aColId, int32_t nCol,
                        SBlockData *pBlockData, uint8_t **ppBuf1, uint8_t **ppBuf2) {
  int32_t  code = 0;
  uint8_t *pBuf1 = NULL;
  uint8_t *pBuf2 = NULL;

962
  ASSERT(aColId[0] == PRIMARYKEY_TIMESTAMP_COL_ID);
H
Hongze Cheng 已提交
963 964 965 966

  if (!ppBuf1) ppBuf1 = &pBuf1;
  if (!ppBuf2) ppBuf2 = &pBuf2;

H
Hongze Cheng 已提交
967
  code = tsdbReadSubColData(pReader, pBlockIdx, pBlock, 0, aColId, nCol, pBlockData, ppBuf1, ppBuf2);
H
Hongze Cheng 已提交
968 969 970 971 972 973
  if (code) goto _err;

  if (pBlock->nSubBlock > 1) {
    SBlockData *pBlockData1 = &(SBlockData){0};
    SBlockData *pBlockData2 = &(SBlockData){0};

H
Hongze Cheng 已提交
974 975
    tBlockDataInit(pBlockData1);
    tBlockDataInit(pBlockData2);
H
Hongze Cheng 已提交
976
    for (int32_t iSubBlock = 1; iSubBlock < pBlock->nSubBlock; iSubBlock++) {
H
Hongze Cheng 已提交
977
      code = tsdbReadSubColData(pReader, pBlockIdx, pBlock, iSubBlock, aColId, nCol, pBlockData1, ppBuf1, ppBuf2);
H
Hongze Cheng 已提交
978 979 980 981
      if (code) goto _err;

      code = tBlockDataCopy(pBlockData, pBlockData2);
      if (code) {
H
Hongze Cheng 已提交
982 983
        tBlockDataClear(pBlockData1, 1);
        tBlockDataClear(pBlockData2, 1);
H
Hongze Cheng 已提交
984 985 986 987 988
        goto _err;
      }

      code = tBlockDataMerge(pBlockData1, pBlockData2, pBlockData);
      if (code) {
H
Hongze Cheng 已提交
989 990
        tBlockDataClear(pBlockData1, 1);
        tBlockDataClear(pBlockData2, 1);
H
Hongze Cheng 已提交
991 992 993 994
        goto _err;
      }
    }

H
Hongze Cheng 已提交
995 996
    tBlockDataClear(pBlockData1, 1);
    tBlockDataClear(pBlockData2, 1);
H
Hongze Cheng 已提交
997
  }
H
Hongze Cheng 已提交
998

H
Hongze Cheng 已提交
999 1000
  tFree(pBuf1);
  tFree(pBuf2);
H
Hongze Cheng 已提交
1001 1002 1003 1004
  return code;

_err:
  tsdbError("vgId:%d tsdb read col data failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1005 1006
  tFree(pBuf1);
  tFree(pBuf2);
H
Hongze Cheng 已提交
1007 1008 1009
  return code;
}

H
Hongze Cheng 已提交
1010 1011
static int32_t tsdbReadSubBlockData(SDataFReader *pReader, SBlockIdx *pBlockIdx, SBlock *pBlock, int32_t iSubBlock,
                                    SBlockData *pBlockData, uint8_t **ppBuf1, uint8_t **ppBuf2) {
H
Hongze Cheng 已提交
1012
  int32_t    code = 0;
H
Hongze Cheng 已提交
1013 1014 1015
  uint8_t   *p;
  int64_t    size;
  int64_t    n;
H
Hongze Cheng 已提交
1016
  TdFilePtr  pFD = pBlock->last ? pReader->pLastFD : pReader->pDataFD;
H
Hongze Cheng 已提交
1017
  SSubBlock *pSubBlock = &pBlock->aSubBlock[iSubBlock];
H
Hongze Cheng 已提交
1018
  SArray    *aBlockCol = NULL;
H
Hongze Cheng 已提交
1019 1020

  tBlockDataReset(pBlockData);
H
Hongze Cheng 已提交
1021

H
Hongze Cheng 已提交
1022
  // realloc
H
Hongze Cheng 已提交
1023
  code = tRealloc(ppBuf1, pSubBlock->szBlock);
H
Hongze Cheng 已提交
1024
  if (code) goto _err;
H
Hongze Cheng 已提交
1025

H
Hongze Cheng 已提交
1026 1027 1028 1029 1030 1031
  // seek
  n = taosLSeekFile(pFD, pSubBlock->offset, SEEK_SET);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
H
Hongze Cheng 已提交
1032

H
Hongze Cheng 已提交
1033
  // read
H
refact  
Hongze Cheng 已提交
1034
  n = taosReadFile(pFD, *ppBuf1, pSubBlock->szBlock);
H
Hongze Cheng 已提交
1035 1036 1037
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
H
refact  
Hongze Cheng 已提交
1038
  } else if (n < pSubBlock->szBlock) {
H
Hongze Cheng 已提交
1039 1040 1041
    code = TSDB_CODE_FILE_CORRUPTED;
    goto _err;
  }
H
Hongze Cheng 已提交
1042

H
Hongze Cheng 已提交
1043 1044
  pBlockData->nRow = pSubBlock->nRow;

H
Hongze Cheng 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
  // TSDBKEY
  p = *ppBuf1 + pSubBlock->szBlockCol + sizeof(TSCKSUM);
  code = tsdbReadBlockDataKey(pBlockData, pSubBlock, p, ppBuf2);
  if (code) goto _err;

  // COLUMNS
  aBlockCol = taosArrayInit(0, sizeof(SBlockCol));
  if (aBlockCol == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  code = tsdbReadBlockCol(pSubBlock, *ppBuf1, aBlockCol);
H
Hongze Cheng 已提交
1058
  if (code) goto _err;
H
Hongze Cheng 已提交
1059

H
Hongze Cheng 已提交
1060 1061 1062
  for (int32_t iBlockCol = 0; iBlockCol < taosArrayGetSize(aBlockCol); iBlockCol++) {
    SColData  *pColData;
    SBlockCol *pBlockCol = (SBlockCol *)taosArrayGet(aBlockCol, iBlockCol);
H
Hongze Cheng 已提交
1063

H
Hongze Cheng 已提交
1064
    ASSERT(pBlockCol->flag && pBlockCol->flag != HAS_NONE);
H
Hongze Cheng 已提交
1065

H
Hongze Cheng 已提交
1066 1067
    code = tBlockDataAddColData(pBlockData, iBlockCol, &pColData);
    if (code) goto _err;
H
Hongze Cheng 已提交
1068

H
Hongze Cheng 已提交
1069
    tColDataInit(pColData, pBlockCol->cid, pBlockCol->type, pBlockCol->smaOn);
H
Hongze Cheng 已提交
1070 1071 1072 1073
    if (pBlockCol->flag == HAS_NULL) {
      for (int32_t iRow = 0; iRow < pSubBlock->nRow; iRow++) {
        code = tColDataAppendValue(pColData, &COL_VAL_NULL(pBlockCol->cid, pBlockCol->type));
        if (code) goto _err;
H
Hongze Cheng 已提交
1074
      }
H
Hongze Cheng 已提交
1075
    } else {
H
Hongze Cheng 已提交
1076 1077 1078
      p = *ppBuf1 + pSubBlock->szBlockCol + sizeof(TSCKSUM) + pSubBlock->szVersion + pSubBlock->szTSKEY +
          sizeof(TSCKSUM) + pBlockCol->offset;
      code = tsdbReadColDataImpl(pSubBlock, pBlockCol, pColData, p, ppBuf2);
H
Hongze Cheng 已提交
1079
      if (code) goto _err;
H
Hongze Cheng 已提交
1080
    }
H
Hongze Cheng 已提交
1081 1082
  }

H
Hongze Cheng 已提交
1083
  taosArrayDestroy(aBlockCol);
H
Hongze Cheng 已提交
1084 1085 1086 1087
  return code;

_err:
  tsdbError("vgId:%d tsdb read sub block data failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1088
  taosArrayDestroy(aBlockCol);
H
Hongze Cheng 已提交
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
  return code;
}

int32_t tsdbReadBlockData(SDataFReader *pReader, SBlockIdx *pBlockIdx, SBlock *pBlock, SBlockData *pBlockData,
                          uint8_t **ppBuf1, uint8_t **ppBuf2) {
  int32_t   code = 0;
  TdFilePtr pFD = pBlock->last ? pReader->pLastFD : pReader->pDataFD;
  uint8_t  *pBuf1 = NULL;
  uint8_t  *pBuf2 = NULL;
  int32_t   iSubBlock;

  if (!ppBuf1) ppBuf1 = &pBuf1;
  if (!ppBuf2) ppBuf2 = &pBuf2;

  // read the first sub-block
  iSubBlock = 0;
  code = tsdbReadSubBlockData(pReader, pBlockIdx, pBlock, iSubBlock, pBlockData, ppBuf1, ppBuf2);
  if (code) goto _err;

  // read remain block data and do merg
H
Hongze Cheng 已提交
1109 1110 1111 1112
  if (pBlock->nSubBlock > 1) {
    SBlockData *pBlockData1 = &(SBlockData){0};
    SBlockData *pBlockData2 = &(SBlockData){0};

H
Hongze Cheng 已提交
1113 1114
    tBlockDataInit(pBlockData1);
    tBlockDataInit(pBlockData2);
H
Hongze Cheng 已提交
1115
    for (iSubBlock = 1; iSubBlock < pBlock->nSubBlock; iSubBlock++) {
H
Hongze Cheng 已提交
1116
      code = tsdbReadSubBlockData(pReader, pBlockIdx, pBlock, iSubBlock, pBlockData1, ppBuf1, ppBuf2);
H
Hongze Cheng 已提交
1117
      if (code) {
H
Hongze Cheng 已提交
1118 1119
        tBlockDataClear(pBlockData1, 1);
        tBlockDataClear(pBlockData2, 1);
H
Hongze Cheng 已提交
1120 1121 1122 1123 1124
        goto _err;
      }

      code = tBlockDataCopy(pBlockData, pBlockData2);
      if (code) {
H
Hongze Cheng 已提交
1125 1126
        tBlockDataClear(pBlockData1, 1);
        tBlockDataClear(pBlockData2, 1);
H
Hongze Cheng 已提交
1127 1128 1129 1130 1131 1132
        goto _err;
      }

      // merge two block data
      code = tBlockDataMerge(pBlockData1, pBlockData2, pBlockData);
      if (code) {
H
Hongze Cheng 已提交
1133 1134
        tBlockDataClear(pBlockData1, 1);
        tBlockDataClear(pBlockData2, 1);
H
Hongze Cheng 已提交
1135 1136 1137 1138
        goto _err;
      }
    }

H
Hongze Cheng 已提交
1139 1140
    tBlockDataClear(pBlockData1, 1);
    tBlockDataClear(pBlockData2, 1);
H
Hongze Cheng 已提交
1141 1142
  }

H
Hongze Cheng 已提交
1143 1144 1145 1146
  ASSERT(pBlock->nRow == pBlockData->nRow);
  ASSERT(tsdbKeyCmprFn(&pBlock->minKey, &TSDBROW_KEY(&tBlockDataFirstRow(pBlockData))) == 0);
  ASSERT(tsdbKeyCmprFn(&pBlock->maxKey, &TSDBROW_KEY(&tBlockDataLastRow(pBlockData))) == 0);

H
Hongze Cheng 已提交
1147 1148
  if (pBuf1) tFree(pBuf1);
  if (pBuf2) tFree(pBuf2);
H
Hongze Cheng 已提交
1149 1150 1151 1152
  return code;

_err:
  tsdbError("vgId:%d tsdb read block data failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1153 1154
  if (pBuf1) tFree(pBuf1);
  if (pBuf2) tFree(pBuf2);
H
Hongze Cheng 已提交
1155 1156 1157
  return code;
}

H
Hongze Cheng 已提交
1158 1159 1160
int32_t tsdbReadBlockSma(SDataFReader *pReader, SBlock *pBlock, SArray *aColumnDataAgg, uint8_t **ppBuf) {
  int32_t   code = 0;
  TdFilePtr pFD = pReader->pSmaFD;
H
Hongze Cheng 已提交
1161
  int64_t   offset = pBlock->aSubBlock[0].sOffset;
H
Hongze Cheng 已提交
1162 1163 1164 1165 1166 1167 1168
  int64_t   size = pBlock->aSubBlock[0].nSma * sizeof(SColumnDataAgg) + sizeof(TSCKSUM);
  uint8_t  *pBuf = NULL;
  int64_t   n;

  ASSERT(tBlockHasSma(pBlock));

  if (!ppBuf) ppBuf = &pBuf;
H
Hongze Cheng 已提交
1169
  code = tRealloc(ppBuf, size);
H
Hongze Cheng 已提交
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
  if (code) goto _err;

  // lseek
  n = taosLSeekFile(pFD, offset, SEEK_SET);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  // read
  n = taosReadFile(pFD, *ppBuf, size);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
H
Hongze Cheng 已提交
1184 1185 1186
  } else if (n < size) {
    code = TSDB_CODE_FILE_CORRUPTED;
    goto _err;
H
Hongze Cheng 已提交
1187 1188 1189
  }

  // check
H
Hongze Cheng 已提交
1190
  if (!taosCheckChecksumWhole(*ppBuf, size)) {
H
Hongze Cheng 已提交
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
    code = TSDB_CODE_FILE_CORRUPTED;
    goto _err;
  }

  // decode
  taosArrayClear(aColumnDataAgg);
  for (int32_t iSma = 0; iSma < pBlock->aSubBlock[0].nSma; iSma++) {
    if (taosArrayPush(aColumnDataAgg, &((SColumnDataAgg *)(*ppBuf))[iSma]) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
  }

H
Hongze Cheng 已提交
1204
  tFree(pBuf);
H
Hongze Cheng 已提交
1205 1206 1207 1208
  return code;

_err:
  tsdbError("vgId:%d read block sma failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1209
  tFree(pBuf);
H
Hongze Cheng 已提交
1210 1211 1212 1213 1214
  return code;
}

// SDataFWriter ====================================================
int32_t tsdbDataFWriterOpen(SDataFWriter **ppWriter, STsdb *pTsdb, SDFileSet *pSet) {
H
Hongze Cheng 已提交
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
  int32_t       code = 0;
  int32_t       flag;
  int64_t       n;
  SDataFWriter *pWriter = NULL;
  char          fname[TSDB_FILENAME_LEN];
  char          hdr[TSDB_FHDR_SIZE] = {0};

  // alloc
  pWriter = taosMemoryCalloc(1, sizeof(*pWriter));
  if (pWriter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  pWriter->pTsdb = pTsdb;
H
Hongze Cheng 已提交
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
  pWriter->wSet = (SDFileSet){.diskId = pSet->diskId,
                              .fid = pSet->fid,
                              .pHeadF = &pWriter->fHead,
                              .pDataF = &pWriter->fData,
                              .pLastF = &pWriter->fLast,
                              .pSmaF = &pWriter->fSma};
  pWriter->fHead = *pSet->pHeadF;
  pWriter->fData = *pSet->pDataF;
  pWriter->fLast = *pSet->pLastF;
  pWriter->fSma = *pSet->pSmaF;
H
Hongze Cheng 已提交
1239 1240 1241

  // head
  flag = TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
H
Hongze Cheng 已提交
1242
  tsdbHeadFileName(pTsdb, pWriter->wSet.diskId, pWriter->wSet.fid, &pWriter->fHead, fname);
H
Hongze Cheng 已提交
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
  pWriter->pHeadFD = taosOpenFile(fname, flag);
  if (pWriter->pHeadFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  n = taosWriteFile(pWriter->pHeadFD, hdr, TSDB_FHDR_SIZE);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  ASSERT(n == TSDB_FHDR_SIZE);

H
Hongze Cheng 已提交
1257
  pWriter->fHead.size += TSDB_FHDR_SIZE;
H
Hongze Cheng 已提交
1258 1259

  // data
H
Hongze Cheng 已提交
1260
  if (pWriter->fData.size == 0) {
H
Hongze Cheng 已提交
1261 1262 1263 1264
    flag = TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
  } else {
    flag = TD_FILE_WRITE;
  }
H
Hongze Cheng 已提交
1265
  tsdbDataFileName(pTsdb, pWriter->wSet.diskId, pWriter->wSet.fid, &pWriter->fData, fname);
H
Hongze Cheng 已提交
1266 1267 1268 1269 1270
  pWriter->pDataFD = taosOpenFile(fname, flag);
  if (pWriter->pDataFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
H
Hongze Cheng 已提交
1271
  if (pWriter->fData.size == 0) {
H
Hongze Cheng 已提交
1272 1273 1274 1275 1276 1277
    n = taosWriteFile(pWriter->pDataFD, hdr, TSDB_FHDR_SIZE);
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

H
Hongze Cheng 已提交
1278
    pWriter->fData.size += TSDB_FHDR_SIZE;
H
Hongze Cheng 已提交
1279 1280 1281 1282 1283 1284 1285
  } else {
    n = taosLSeekFile(pWriter->pDataFD, 0, SEEK_END);
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

H
Hongze Cheng 已提交
1286
    ASSERT(n == pWriter->fData.size);
H
Hongze Cheng 已提交
1287 1288 1289
  }

  // last
H
Hongze Cheng 已提交
1290
  if (pWriter->fLast.size == 0) {
H
Hongze Cheng 已提交
1291 1292 1293 1294
    flag = TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
  } else {
    flag = TD_FILE_WRITE;
  }
H
Hongze Cheng 已提交
1295
  tsdbLastFileName(pTsdb, pWriter->wSet.diskId, pWriter->wSet.fid, &pWriter->fLast, fname);
H
Hongze Cheng 已提交
1296 1297 1298 1299 1300
  pWriter->pLastFD = taosOpenFile(fname, flag);
  if (pWriter->pLastFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
H
Hongze Cheng 已提交
1301
  if (pWriter->fLast.size == 0) {
H
Hongze Cheng 已提交
1302 1303 1304 1305 1306 1307
    n = taosWriteFile(pWriter->pLastFD, hdr, TSDB_FHDR_SIZE);
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

H
Hongze Cheng 已提交
1308
    pWriter->fLast.size += TSDB_FHDR_SIZE;
H
Hongze Cheng 已提交
1309 1310 1311 1312 1313 1314 1315
  } else {
    n = taosLSeekFile(pWriter->pLastFD, 0, SEEK_END);
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

H
Hongze Cheng 已提交
1316
    ASSERT(n == pWriter->fLast.size);
H
Hongze Cheng 已提交
1317 1318 1319
  }

  // sma
H
Hongze Cheng 已提交
1320
  if (pWriter->fSma.size == 0) {
H
Hongze Cheng 已提交
1321 1322 1323 1324
    flag = TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
  } else {
    flag = TD_FILE_WRITE;
  }
H
Hongze Cheng 已提交
1325
  tsdbSmaFileName(pTsdb, pWriter->wSet.diskId, pWriter->wSet.fid, &pWriter->fSma, fname);
H
Hongze Cheng 已提交
1326 1327 1328 1329 1330
  pWriter->pSmaFD = taosOpenFile(fname, flag);
  if (pWriter->pSmaFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
H
Hongze Cheng 已提交
1331
  if (pWriter->fSma.size == 0) {
H
Hongze Cheng 已提交
1332 1333 1334 1335 1336 1337
    n = taosWriteFile(pWriter->pSmaFD, hdr, TSDB_FHDR_SIZE);
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

H
Hongze Cheng 已提交
1338
    pWriter->fSma.size += TSDB_FHDR_SIZE;
H
Hongze Cheng 已提交
1339 1340 1341 1342 1343 1344 1345
  } else {
    n = taosLSeekFile(pWriter->pSmaFD, 0, SEEK_END);
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

H
Hongze Cheng 已提交
1346
    ASSERT(n == pWriter->fSma.size);
H
Hongze Cheng 已提交
1347 1348 1349 1350 1351 1352 1353 1354
  }

  *ppWriter = pWriter;
  return code;

_err:
  tsdbError("vgId:%d tsdb data file writer open failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
  *ppWriter = NULL;
H
Hongze Cheng 已提交
1355 1356 1357
  return code;
}

H
Hongze Cheng 已提交
1358
int32_t tsdbDataFWriterClose(SDataFWriter **ppWriter, int8_t sync) {
H
Hongze Cheng 已提交
1359
  int32_t code = 0;
H
Hongze Cheng 已提交
1360
  STsdb  *pTsdb = (*ppWriter)->pTsdb;
H
Hongze Cheng 已提交
1361

H
Hongze Cheng 已提交
1362 1363
  if (*ppWriter == NULL) goto _exit;

H
Hongze Cheng 已提交
1364
  if (sync) {
H
Hongze Cheng 已提交
1365
    if (taosFsyncFile((*ppWriter)->pHeadFD) < 0) {
H
Hongze Cheng 已提交
1366 1367 1368 1369
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

H
Hongze Cheng 已提交
1370
    if (taosFsyncFile((*ppWriter)->pDataFD) < 0) {
H
Hongze Cheng 已提交
1371 1372 1373 1374
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

H
Hongze Cheng 已提交
1375
    if (taosFsyncFile((*ppWriter)->pLastFD) < 0) {
H
Hongze Cheng 已提交
1376 1377 1378 1379
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

H
Hongze Cheng 已提交
1380
    if (taosFsyncFile((*ppWriter)->pSmaFD) < 0) {
H
Hongze Cheng 已提交
1381 1382 1383 1384 1385
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }
  }

H
Hongze Cheng 已提交
1386
  if (taosCloseFile(&(*ppWriter)->pHeadFD) < 0) {
H
Hongze Cheng 已提交
1387 1388 1389 1390
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
1391
  if (taosCloseFile(&(*ppWriter)->pDataFD) < 0) {
H
Hongze Cheng 已提交
1392 1393 1394 1395
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
1396
  if (taosCloseFile(&(*ppWriter)->pLastFD) < 0) {
H
Hongze Cheng 已提交
1397 1398 1399 1400
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
1401
  if (taosCloseFile(&(*ppWriter)->pSmaFD) < 0) {
H
Hongze Cheng 已提交
1402 1403 1404 1405
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
1406
  taosMemoryFree(*ppWriter);
H
Hongze Cheng 已提交
1407
_exit:
H
Hongze Cheng 已提交
1408
  *ppWriter = NULL;
H
Hongze Cheng 已提交
1409 1410 1411
  return code;

_err:
H
Hongze Cheng 已提交
1412
  tsdbError("vgId:%d data file writer close failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1413 1414 1415
  return code;
}

H
Hongze Cheng 已提交
1416
int32_t tsdbUpdateDFileSetHeader(SDataFWriter *pWriter) {
H
Hongze Cheng 已提交
1417
  int32_t code = 0;
H
Hongze Cheng 已提交
1418 1419
  int64_t n;
  char    hdr[TSDB_FHDR_SIZE];
H
Hongze Cheng 已提交
1420 1421

  // head ==============
H
Hongze Cheng 已提交
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
  memset(hdr, 0, TSDB_FHDR_SIZE);
  tPutHeadFile(hdr, &pWriter->fHead);
  taosCalcChecksumAppend(0, hdr, TSDB_FHDR_SIZE);

  n = taosLSeekFile(pWriter->pHeadFD, 0, SEEK_SET);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  n = taosWriteFile(pWriter->pHeadFD, hdr, TSDB_FHDR_SIZE);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
H
Hongze Cheng 已提交
1437 1438

  // data ==============
H
Hongze Cheng 已提交
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
  memset(hdr, 0, TSDB_FHDR_SIZE);
  tPutDataFile(hdr, &pWriter->fData);
  taosCalcChecksumAppend(0, hdr, TSDB_FHDR_SIZE);

  n = taosLSeekFile(pWriter->pDataFD, 0, SEEK_SET);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  n = taosWriteFile(pWriter->pDataFD, hdr, TSDB_FHDR_SIZE);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
H
Hongze Cheng 已提交
1454 1455

  // last ==============
H
Hongze Cheng 已提交
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
  memset(hdr, 0, TSDB_FHDR_SIZE);
  tPutLastFile(hdr, &pWriter->fLast);
  taosCalcChecksumAppend(0, hdr, TSDB_FHDR_SIZE);

  n = taosLSeekFile(pWriter->pLastFD, 0, SEEK_SET);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  n = taosWriteFile(pWriter->pLastFD, hdr, TSDB_FHDR_SIZE);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
H
Hongze Cheng 已提交
1471 1472

  // sma ==============
H
Hongze Cheng 已提交
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
  memset(hdr, 0, TSDB_FHDR_SIZE);
  tPutSmaFile(hdr, &pWriter->fSma);
  taosCalcChecksumAppend(0, hdr, TSDB_FHDR_SIZE);

  n = taosLSeekFile(pWriter->pSmaFD, 0, SEEK_SET);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  n = taosWriteFile(pWriter->pSmaFD, hdr, TSDB_FHDR_SIZE);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
H
Hongze Cheng 已提交
1488 1489 1490 1491 1492

  return code;

_err:
  tsdbError("vgId:%d update DFileSet header failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1493 1494 1495
  return code;
}

H
Hongze Cheng 已提交
1496
int32_t tsdbWriteBlockIdx(SDataFWriter *pWriter, SArray *aBlockIdx, uint8_t **ppBuf) {
H
Hongze Cheng 已提交
1497
  int32_t    code = 0;
H
Hongze Cheng 已提交
1498
  SHeadFile *pHeadFile = &pWriter->fHead;
H
Hongze Cheng 已提交
1499
  uint8_t   *pBuf = NULL;
H
Hongze Cheng 已提交
1500 1501 1502 1503
  int64_t    size;
  int64_t    n;

  if (!ppBuf) ppBuf = &pBuf;
H
Hongze Cheng 已提交
1504 1505

  // prepare
H
Hongze Cheng 已提交
1506 1507 1508 1509 1510
  size = tPutU32(NULL, TSDB_FILE_DLMT);
  for (int32_t iBlockIdx = 0; iBlockIdx < taosArrayGetSize(aBlockIdx); iBlockIdx++) {
    size += tPutBlockIdx(NULL, taosArrayGet(aBlockIdx, iBlockIdx));
  }
  size += sizeof(TSCKSUM);
H
Hongze Cheng 已提交
1511 1512

  // alloc
H
Hongze Cheng 已提交
1513
  code = tRealloc(ppBuf, size);
H
Hongze Cheng 已提交
1514 1515 1516
  if (code) goto _err;

  // build
H
Hongze Cheng 已提交
1517
  n = 0;
H
Hongze Cheng 已提交
1518 1519 1520 1521
  n = tPutU32(*ppBuf + n, TSDB_FILE_DLMT);
  for (int32_t iBlockIdx = 0; iBlockIdx < taosArrayGetSize(aBlockIdx); iBlockIdx++) {
    n += tPutBlockIdx(*ppBuf + n, taosArrayGet(aBlockIdx, iBlockIdx));
  }
H
Hongze Cheng 已提交
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
  taosCalcChecksumAppend(0, *ppBuf, size);

  ASSERT(n + sizeof(TSCKSUM) == size);

  // write
  n = taosWriteFile(pWriter->pHeadFD, *ppBuf, size);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
1533 1534 1535
  // update
  pHeadFile->offset = pHeadFile->size;
  pHeadFile->size += size;
H
Hongze Cheng 已提交
1536

H
Hongze Cheng 已提交
1537
  tFree(pBuf);
H
Hongze Cheng 已提交
1538 1539 1540 1541
  return code;

_err:
  tsdbError("vgId:%d write block idx failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1542
  tFree(pBuf);
H
Hongze Cheng 已提交
1543 1544 1545
  return code;
}

H
Hongze Cheng 已提交
1546
int32_t tsdbWriteBlock(SDataFWriter *pWriter, SMapData *mBlock, uint8_t **ppBuf, SBlockIdx *pBlockIdx) {
H
Hongze Cheng 已提交
1547
  int32_t       code = 0;
H
Hongze Cheng 已提交
1548
  SHeadFile    *pHeadFile = &pWriter->fHead;
H
Hongze Cheng 已提交
1549 1550 1551 1552
  SBlockDataHdr hdr = {.delimiter = TSDB_FILE_DLMT, .suid = pBlockIdx->suid, .uid = pBlockIdx->uid};
  uint8_t      *pBuf = NULL;
  int64_t       size;
  int64_t       n;
H
Hongze Cheng 已提交
1553

H
Hongze Cheng 已提交
1554
  ASSERT(mBlock->nItem > 0);
H
Hongze Cheng 已提交
1555 1556

  // prepare
H
Hongze Cheng 已提交
1557
  size = sizeof(SBlockDataHdr) + tPutMapData(NULL, mBlock) + sizeof(TSCKSUM);
H
Hongze Cheng 已提交
1558 1559 1560

  // alloc
  if (!ppBuf) ppBuf = &pBuf;
H
Hongze Cheng 已提交
1561
  code = tRealloc(ppBuf, size);
H
Hongze Cheng 已提交
1562 1563 1564 1565
  if (code) goto _err;

  // build
  n = 0;
H
Hongze Cheng 已提交
1566 1567
  *(SBlockDataHdr *)(*ppBuf) = hdr;
  n += sizeof(hdr);
H
Hongze Cheng 已提交
1568
  n += tPutMapData(*ppBuf + n, mBlock);
H
Hongze Cheng 已提交
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
  taosCalcChecksumAppend(0, *ppBuf, size);

  ASSERT(n + sizeof(TSCKSUM) == size);

  // write
  n = taosWriteFile(pWriter->pHeadFD, *ppBuf, size);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
1580 1581
  // update
  pBlockIdx->offset = pHeadFile->size;
H
Hongze Cheng 已提交
1582
  pBlockIdx->size = size;
H
Hongze Cheng 已提交
1583
  pHeadFile->size += size;
H
Hongze Cheng 已提交
1584

H
Hongze Cheng 已提交
1585
  tFree(pBuf);
H
Hongze Cheng 已提交
1586 1587 1588 1589 1590
  tsdbTrace("vgId:%d write block, offset:%" PRId64 " size:%" PRId64, TD_VID(pWriter->pTsdb->pVnode), pBlockIdx->offset,
            pBlockIdx->size);
  return code;

_err:
H
Hongze Cheng 已提交
1591
  tFree(pBuf);
H
Hongze Cheng 已提交
1592
  tsdbError("vgId:%d write block failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1593 1594 1595
  return code;
}

H
Hongze Cheng 已提交
1596
static void tsdbUpdateBlockInfo(SBlockData *pBlockData, SBlock *pBlock) {
H
Hongze Cheng 已提交
1597
  for (int32_t iRow = 0; iRow < pBlockData->nRow; iRow++) {
H
Hongze Cheng 已提交
1598 1599
    TSDBKEY key = {.ts = pBlockData->aTSKEY[iRow], .version = pBlockData->aVersion[iRow]};

H
Hongze Cheng 已提交
1600
    if (iRow == 0) {
H
Hongze Cheng 已提交
1601 1602 1603 1604 1605 1606 1607
      if (tsdbKeyCmprFn(&pBlock->minKey, &key) > 0) {
        pBlock->minKey = key;
      }
    } else {
      if (pBlockData->aTSKEY[iRow] == pBlockData->aTSKEY[iRow - 1]) {
        pBlock->hasDup = 1;
      }
H
Hongze Cheng 已提交
1608 1609
    }

H
Hongze Cheng 已提交
1610 1611
    if (iRow == pBlockData->nRow - 1 && tsdbKeyCmprFn(&pBlock->maxKey, &key) < 0) {
      pBlock->maxKey = key;
H
Hongze Cheng 已提交
1612 1613 1614 1615 1616 1617
    }

    pBlock->minVersion = TMIN(pBlock->minVersion, key.version);
    pBlock->maxVersion = TMAX(pBlock->maxVersion, key.version);
  }
  pBlock->nRow += pBlockData->nRow;
H
Hongze Cheng 已提交
1618
}
H
Hongze Cheng 已提交
1619

H
Hongze Cheng 已提交
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629
static int32_t tsdbWriteBlockDataKey(SSubBlock *pSubBlock, SBlockData *pBlockData, uint8_t **ppBuf1, int64_t *nDataP,
                                     uint8_t **ppBuf2) {
  int32_t code = 0;
  int64_t size;
  int64_t tsize;

  if (pSubBlock->cmprAlg == NO_COMPRESSION) {
    pSubBlock->szVersion = sizeof(int64_t) * pSubBlock->nRow;
    pSubBlock->szTSKEY = sizeof(TSKEY) * pSubBlock->nRow;

H
Hongze Cheng 已提交
1630
    code = tRealloc(ppBuf1, *nDataP + pSubBlock->szVersion + pSubBlock->szTSKEY + sizeof(TSCKSUM));
H
Hongze Cheng 已提交
1631 1632 1633 1634 1635 1636 1637
    if (code) goto _err;

    // VERSION
    memcpy(*ppBuf1 + *nDataP, pBlockData->aVersion, pSubBlock->szVersion);

    // TSKEY
    memcpy(*ppBuf1 + *nDataP + pSubBlock->szVersion, pBlockData->aTSKEY, pSubBlock->szTSKEY);
H
Hongze Cheng 已提交
1638
  } else {
H
Hongze Cheng 已提交
1639
    size = (sizeof(int64_t) + sizeof(TSKEY)) * pSubBlock->nRow + COMP_OVERFLOW_BYTES * 2;
H
Hongze Cheng 已提交
1640

H
Hongze Cheng 已提交
1641
    code = tRealloc(ppBuf1, *nDataP + size + sizeof(TSCKSUM));
H
Hongze Cheng 已提交
1642
    if (code) goto _err;
H
Hongze Cheng 已提交
1643

H
Hongze Cheng 已提交
1644 1645
    tsize = sizeof(int64_t) * pSubBlock->nRow + COMP_OVERFLOW_BYTES;
    if (pSubBlock->cmprAlg == TWO_STAGE_COMP) {
H
Hongze Cheng 已提交
1646
      code = tRealloc(ppBuf2, tsize);
H
Hongze Cheng 已提交
1647 1648
      if (code) goto _err;
    }
H
Hongze Cheng 已提交
1649

H
Hongze Cheng 已提交
1650 1651 1652 1653 1654 1655
    // VERSION
    pSubBlock->szVersion =
        tsCompressBigint((char *)pBlockData->aVersion, sizeof(int64_t) * pBlockData->nRow, pBlockData->nRow,
                         *ppBuf1 + *nDataP, size, pSubBlock->cmprAlg, *ppBuf2, tsize);
    if (pSubBlock->szVersion <= 0) {
      code = TSDB_CODE_COMPRESS_ERROR;
H
Hongze Cheng 已提交
1656 1657 1658
      goto _err;
    }

H
Hongze Cheng 已提交
1659
    // TSKEY
H
Hongze Cheng 已提交
1660 1661 1662 1663 1664
    pSubBlock->szTSKEY = tsCompressTimestamp((char *)pBlockData->aTSKEY, sizeof(TSKEY) * pBlockData->nRow,
                                             pBlockData->nRow, *ppBuf1 + *nDataP + pSubBlock->szVersion,
                                             size - pSubBlock->szVersion, pSubBlock->cmprAlg, *ppBuf2, tsize);
    if (pSubBlock->szTSKEY <= 0) {
      code = TSDB_CODE_COMPRESS_ERROR;
H
Hongze Cheng 已提交
1665 1666 1667
      goto _err;
    }

H
Hongze Cheng 已提交
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
    ASSERT(pSubBlock->szVersion + pSubBlock->szTSKEY <= size);
  }

  // checksum
  size = pSubBlock->szVersion + pSubBlock->szTSKEY + sizeof(TSCKSUM);
  taosCalcChecksumAppend(0, *ppBuf1 + *nDataP, size);

  *nDataP += size;
  return code;

_err:
  return code;
}

static int32_t tsdbWriteColData(SColData *pColData, SBlockCol *pBlockCol, SSubBlock *pSubBlock, uint8_t **ppBuf1,
                                int64_t *nDataP, uint8_t **ppBuf2) {
  int32_t code = 0;
  int64_t size;
  int64_t n = 0;

  // BITMAP
  if (pColData->flag != HAS_VALUE) {
    size = BIT2_SIZE(pColData->nVal) + COMP_OVERFLOW_BYTES;

H
Hongze Cheng 已提交
1692
    code = tRealloc(ppBuf1, *nDataP + n + size);
H
Hongze Cheng 已提交
1693 1694
    if (code) goto _err;

H
Hongze Cheng 已提交
1695
    code = tRealloc(ppBuf2, size);
H
Hongze Cheng 已提交
1696 1697 1698 1699 1700 1701 1702
    if (code) goto _err;

    pBlockCol->szBitmap =
        tsCompressTinyint((char *)pColData->pBitMap, BIT2_SIZE(pColData->nVal), BIT2_SIZE(pColData->nVal),
                          *ppBuf1 + *nDataP + n, size, TWO_STAGE_COMP, *ppBuf2, size);
    if (pBlockCol->szBitmap <= 0) {
      code = TSDB_CODE_COMPRESS_ERROR;
H
Hongze Cheng 已提交
1703 1704 1705
      goto _err;
    }
  } else {
H
Hongze Cheng 已提交
1706 1707 1708
    pBlockCol->szBitmap = 0;
  }
  n += pBlockCol->szBitmap;
H
Hongze Cheng 已提交
1709

H
Hongze Cheng 已提交
1710 1711 1712
  // OFFSET
  if (IS_VAR_DATA_TYPE(pColData->type)) {
    size = sizeof(int32_t) * pColData->nVal + COMP_OVERFLOW_BYTES;
H
Hongze Cheng 已提交
1713

H
Hongze Cheng 已提交
1714
    code = tRealloc(ppBuf1, *nDataP + n + size);
H
Hongze Cheng 已提交
1715 1716
    if (code) goto _err;

H
Hongze Cheng 已提交
1717
    code = tRealloc(ppBuf2, size);
H
Hongze Cheng 已提交
1718
    if (code) goto _err;
H
Hongze Cheng 已提交
1719

H
Hongze Cheng 已提交
1720 1721 1722
    pBlockCol->szOffset = tsCompressInt((char *)pColData->aOffset, sizeof(int32_t) * pColData->nVal, pColData->nVal,
                                        *ppBuf1 + *nDataP + n, size, TWO_STAGE_COMP, *ppBuf2, size);
    if (pBlockCol->szOffset <= 0) {
H
Hongze Cheng 已提交
1723 1724 1725
      code = TSDB_CODE_COMPRESS_ERROR;
      goto _err;
    }
H
Hongze Cheng 已提交
1726 1727 1728 1729
  } else {
    pBlockCol->szOffset = 0;
  }
  n += pBlockCol->szOffset;
H
Hongze Cheng 已提交
1730

H
Hongze Cheng 已提交
1731 1732 1733 1734
  // VALUE
  if (pSubBlock->cmprAlg == NO_COMPRESSION) {
    pBlockCol->szValue = pColData->nData;

H
Hongze Cheng 已提交
1735
    code = tRealloc(ppBuf1, *nDataP + n + pBlockCol->szValue + sizeof(TSCKSUM));
H
Hongze Cheng 已提交
1736 1737 1738 1739 1740 1741
    if (code) goto _err;

    memcpy(*ppBuf1 + *nDataP + n, pColData->pData, pBlockCol->szValue);
  } else {
    size = pColData->nData + COMP_OVERFLOW_BYTES;

H
Hongze Cheng 已提交
1742
    code = tRealloc(ppBuf1, *nDataP + n + size + sizeof(TSCKSUM));
H
Hongze Cheng 已提交
1743 1744 1745
    if (code) goto _err;

    if (pSubBlock->cmprAlg == TWO_STAGE_COMP) {
H
Hongze Cheng 已提交
1746
      code = tRealloc(ppBuf2, size);
H
Hongze Cheng 已提交
1747 1748 1749 1750 1751 1752 1753
      if (code) goto _err;
    }

    pBlockCol->szValue =
        tDataTypes[pColData->type].compFunc((char *)pColData->pData, pColData->nData, pColData->nVal,
                                            *ppBuf1 + *nDataP + n, size, pSubBlock->cmprAlg, *ppBuf2, size);
    if (pBlockCol->szValue <= 0) {
H
Hongze Cheng 已提交
1754 1755 1756
      code = TSDB_CODE_COMPRESS_ERROR;
      goto _err;
    }
H
Hongze Cheng 已提交
1757 1758 1759
  }
  n += pBlockCol->szValue;
  pBlockCol->szOrigin = pColData->nData;
H
Hongze Cheng 已提交
1760

H
Hongze Cheng 已提交
1761 1762 1763
  // checksum
  n += sizeof(TSCKSUM);
  taosCalcChecksumAppend(0, *ppBuf1 + *nDataP, n);
H
Hongze Cheng 已提交
1764

H
Hongze Cheng 已提交
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
  *nDataP += n;

  return code;

_err:
  return code;
}

static int32_t tsdbWriteBlockDataImpl(TdFilePtr pFD, SSubBlock *pSubBlock, SBlockDataHdr hdr, SArray *aBlockCol,
                                      uint8_t *pData, int64_t nData, uint8_t **ppBuf) {
  int32_t code = 0;
  int32_t nBlockCol = taosArrayGetSize(aBlockCol);
  int64_t size;
  int64_t n;

  // HDR + SArray<SBlockCol>
  pSubBlock->szBlockCol = sizeof(hdr);
  for (int32_t iBlockCol = 0; iBlockCol < nBlockCol; iBlockCol++) {
    pSubBlock->szBlockCol += tPutBlockCol(NULL, taosArrayGet(aBlockCol, iBlockCol));
  }

H
Hongze Cheng 已提交
1786
  code = tRealloc(ppBuf, pSubBlock->szBlockCol + sizeof(TSCKSUM));
H
Hongze Cheng 已提交
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817
  if (code) goto _err;

  n = 0;
  memcpy(*ppBuf, &hdr, sizeof(hdr));
  n += sizeof(hdr);
  for (int32_t iBlockCol = 0; iBlockCol < nBlockCol; iBlockCol++) {
    n += tPutBlockCol(*ppBuf + n, taosArrayGet(aBlockCol, iBlockCol));
  }
  taosCalcChecksumAppend(0, *ppBuf, pSubBlock->szBlockCol + sizeof(TSCKSUM));

  ASSERT(n == pSubBlock->szBlockCol);

  n = taosWriteFile(pFD, *ppBuf, pSubBlock->szBlockCol + sizeof(TSCKSUM));
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  // SBlockData
  n = taosWriteFile(pFD, pData, nData);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  return code;

_err:
  return code;
}

H
Hongze Cheng 已提交
1818
static int32_t tsdbWriteBlockSma(TdFilePtr pFD, SBlockData *pBlockData, SSubBlock *pSubBlock, uint8_t **ppBuf) {
H
Hongze Cheng 已提交
1819 1820 1821
  int32_t   code = 0;
  int64_t   n;
  SColData *pColData;
H
Hongze Cheng 已提交
1822

H
Hongze Cheng 已提交
1823 1824
  // prepare
  pSubBlock->nSma = 0;
H
Hongze Cheng 已提交
1825 1826
  for (int32_t iColData = 0; iColData < taosArrayGetSize(pBlockData->aIdx); iColData++) {
    pColData = tBlockDataGetColDataByIdx(pBlockData, iColData);
H
Hongze Cheng 已提交
1827

H
Hongze Cheng 已提交
1828
    if (IS_VAR_DATA_TYPE(pColData->type) || (!pColData->smaOn)) continue;
H
Hongze Cheng 已提交
1829

H
Hongze Cheng 已提交
1830 1831 1832
    pSubBlock->nSma++;
  }
  if (pSubBlock->nSma == 0) goto _exit;
H
Hongze Cheng 已提交
1833

H
Hongze Cheng 已提交
1834
  // calc
H
Hongze Cheng 已提交
1835
  code = tRealloc(ppBuf, sizeof(SColumnDataAgg) * pSubBlock->nSma + sizeof(TSCKSUM));
H
Hongze Cheng 已提交
1836 1837
  if (code) goto _err;
  n = 0;
H
Hongze Cheng 已提交
1838 1839
  for (int32_t iColData = 0; iColData < taosArrayGetSize(pBlockData->aIdx); iColData++) {
    pColData = tBlockDataGetColDataByIdx(pBlockData, iColData);
H
Hongze Cheng 已提交
1840

H
Hongze Cheng 已提交
1841
    if (IS_VAR_DATA_TYPE(pColData->type) || (!pColData->smaOn)) continue;
H
Hongze Cheng 已提交
1842

H
Hongze Cheng 已提交
1843 1844 1845 1846
    tsdbCalcColDataSMA(pColData, &((SColumnDataAgg *)(*ppBuf))[n]);
    n++;
  }
  taosCalcChecksumAppend(0, *ppBuf, sizeof(SColumnDataAgg) * pSubBlock->nSma + sizeof(TSCKSUM));
H
Hongze Cheng 已提交
1847

H
Hongze Cheng 已提交
1848 1849 1850 1851 1852 1853
  // write
  n = taosWriteFile(pFD, *ppBuf, sizeof(SColumnDataAgg) * pSubBlock->nSma + sizeof(TSCKSUM));
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
H
Hongze Cheng 已提交
1854

H
Hongze Cheng 已提交
1855 1856
_exit:
  return code;
H
Hongze Cheng 已提交
1857

H
Hongze Cheng 已提交
1858 1859 1860
_err:
  return code;
}
H
Hongze Cheng 已提交
1861

H
Hongze Cheng 已提交
1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
int32_t tsdbWriteBlockData(SDataFWriter *pWriter, SBlockData *pBlockData, uint8_t **ppBuf1, uint8_t **ppBuf2,
                           SBlockIdx *pBlockIdx, SBlock *pBlock, int8_t cmprAlg) {
  int32_t       code = 0;
  SSubBlock    *pSubBlock = &pBlock->aSubBlock[pBlock->nSubBlock++];
  SBlockCol     blockCol;
  SBlockCol    *pBlockCol = &blockCol;
  int64_t       n;
  TdFilePtr     pFileFD = pBlock->last ? pWriter->pLastFD : pWriter->pDataFD;
  SBlockDataHdr hdr = {.delimiter = TSDB_FILE_DLMT, .suid = pBlockIdx->suid, .uid = pBlockIdx->uid};
  uint8_t      *p;
  int64_t       nData;
  uint8_t      *pBuf1 = NULL;
  uint8_t      *pBuf2 = NULL;
  SArray       *aBlockCol = NULL;
H
Hongze Cheng 已提交
1876

H
Hongze Cheng 已提交
1877 1878
  if (!ppBuf1) ppBuf1 = &pBuf1;
  if (!ppBuf2) ppBuf2 = &pBuf2;
H
Hongze Cheng 已提交
1879

H
Hongze Cheng 已提交
1880
  tsdbUpdateBlockInfo(pBlockData, pBlock);
H
Hongze Cheng 已提交
1881

H
Hongze Cheng 已提交
1882 1883 1884
  pSubBlock->nRow = pBlockData->nRow;
  pSubBlock->cmprAlg = cmprAlg;
  if (pBlock->last) {
H
Hongze Cheng 已提交
1885
    pSubBlock->offset = pWriter->fLast.size;
H
Hongze Cheng 已提交
1886
  } else {
H
Hongze Cheng 已提交
1887
    pSubBlock->offset = pWriter->fData.size;
H
Hongze Cheng 已提交
1888 1889 1890 1891 1892 1893 1894 1895 1896
  }

  // ======================= BLOCK DATA =======================
  // TSDBKEY
  nData = 0;
  code = tsdbWriteBlockDataKey(pSubBlock, pBlockData, ppBuf1, &nData, ppBuf2);
  if (code) goto _err;

  // COLUMNS
H
Hongze Cheng 已提交
1897
  aBlockCol = taosArrayInit(taosArrayGetSize(pBlockData->aIdx), sizeof(SBlockCol));
H
Hongze Cheng 已提交
1898 1899 1900 1901 1902
  if (aBlockCol == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  int32_t offset = 0;
H
Hongze Cheng 已提交
1903 1904
  for (int32_t iCol = 0; iCol < taosArrayGetSize(pBlockData->aIdx); iCol++) {
    SColData *pColData = tBlockDataGetColDataByIdx(pBlockData, iCol);
H
Hongze Cheng 已提交
1905 1906 1907 1908 1909 1910 1911

    ASSERT(pColData->flag);

    if (pColData->flag == HAS_NONE) continue;

    pBlockCol->cid = pColData->cid;
    pBlockCol->type = pColData->type;
H
Hongze Cheng 已提交
1912
    pBlockCol->smaOn = pColData->smaOn;
H
Hongze Cheng 已提交
1913 1914 1915 1916 1917 1918 1919 1920
    pBlockCol->flag = pColData->flag;

    if (pColData->flag != HAS_NULL) {
      code = tsdbWriteColData(pColData, pBlockCol, pSubBlock, ppBuf1, &nData, ppBuf2);
      if (code) goto _err;

      pBlockCol->offset = offset;
      offset = offset + pBlockCol->szBitmap + pBlockCol->szOffset + pBlockCol->szValue + sizeof(TSCKSUM);
H
Hongze Cheng 已提交
1921 1922
    }

H
Hongze Cheng 已提交
1923 1924 1925 1926
    if (taosArrayPush(aBlockCol, pBlockCol) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
1927 1928
  }

H
Hongze Cheng 已提交
1929 1930 1931 1932 1933
  // write
  code = tsdbWriteBlockDataImpl(pFileFD, pSubBlock, hdr, aBlockCol, *ppBuf1, nData, ppBuf2);
  if (code) goto _err;

  pSubBlock->szBlock = pSubBlock->szBlockCol + sizeof(TSCKSUM) + nData;
H
Hongze Cheng 已提交
1934
  if (pBlock->last) {
H
Hongze Cheng 已提交
1935
    pWriter->fLast.size += pSubBlock->szBlock;
H
Hongze Cheng 已提交
1936
  } else {
H
Hongze Cheng 已提交
1937
    pWriter->fData.size += pSubBlock->szBlock;
H
Hongze Cheng 已提交
1938 1939
  }

H
Hongze Cheng 已提交
1940 1941 1942 1943 1944 1945
  // ======================= BLOCK SMA =======================
  pSubBlock->sOffset = 0;
  pSubBlock->nSma = 0;

  if (pBlock->nSubBlock > 1 || pBlock->last || pBlock->hasDup) goto _exit;

H
Hongze Cheng 已提交
1946
  code = tsdbWriteBlockSma(pWriter->pSmaFD, pBlockData, pSubBlock, ppBuf1);
H
Hongze Cheng 已提交
1947 1948 1949
  if (code) goto _err;

  if (pSubBlock->nSma > 0) {
H
Hongze Cheng 已提交
1950 1951
    pSubBlock->sOffset = pWriter->fSma.size;
    pWriter->fSma.size += (sizeof(SColumnDataAgg) * pSubBlock->nSma + sizeof(TSCKSUM));
H
Hongze Cheng 已提交
1952 1953 1954
  }

_exit:
H
Hongze Cheng 已提交
1955 1956
  tFree(pBuf1);
  tFree(pBuf2);
H
Hongze Cheng 已提交
1957
  taosArrayDestroy(aBlockCol);
H
Hongze Cheng 已提交
1958 1959 1960
  return code;

_err:
H
Hongze Cheng 已提交
1961
  tsdbError("vgId:%d write block data failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1962 1963
  tFree(pBuf1);
  tFree(pBuf2);
H
Hongze Cheng 已提交
1964
  taosArrayDestroy(aBlockCol);
H
Hongze Cheng 已提交
1965 1966
  return code;
}
H
Hongze Cheng 已提交
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977

int32_t tsdbDFileSetCopy(STsdb *pTsdb, SDFileSet *pSetFrom, SDFileSet *pSetTo) {
  int32_t   code = 0;
  int64_t   n;
  int64_t   size;
  TdFilePtr pOutFD = NULL;  // TODO
  TdFilePtr PInFD = NULL;   // TODO
  char      fNameFrom[TSDB_FILENAME_LEN];
  char      fNameTo[TSDB_FILENAME_LEN];

  // head
H
Hongze Cheng 已提交
1978 1979
  tsdbHeadFileName(pTsdb, pSetFrom->diskId, pSetFrom->fid, pSetFrom->pHeadF, fNameFrom);
  tsdbHeadFileName(pTsdb, pSetTo->diskId, pSetTo->fid, pSetTo->pHeadF, fNameTo);
H
Hongze Cheng 已提交
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992

  pOutFD = taosOpenFile(fNameTo, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
  if (pOutFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  PInFD = taosOpenFile(fNameFrom, TD_FILE_READ);
  if (PInFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
1993
  n = taosFSendFile(pOutFD, PInFD, 0, pSetFrom->pHeadF->size);
H
Hongze Cheng 已提交
1994 1995 1996 1997 1998 1999 2000 2001
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
  taosCloseFile(&pOutFD);
  taosCloseFile(&PInFD);

  // data
H
Hongze Cheng 已提交
2002 2003
  tsdbDataFileName(pTsdb, pSetFrom->diskId, pSetFrom->fid, pSetFrom->pDataF, fNameFrom);
  tsdbDataFileName(pTsdb, pSetTo->diskId, pSetTo->fid, pSetTo->pDataF, fNameTo);
H
Hongze Cheng 已提交
2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016

  pOutFD = taosOpenFile(fNameTo, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
  if (pOutFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  PInFD = taosOpenFile(fNameFrom, TD_FILE_READ);
  if (PInFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
2017
  n = taosFSendFile(pOutFD, PInFD, 0, pSetFrom->pDataF->size);
H
Hongze Cheng 已提交
2018 2019 2020 2021 2022 2023 2024 2025
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
  taosCloseFile(&pOutFD);
  taosCloseFile(&PInFD);

  // last
H
Hongze Cheng 已提交
2026 2027 2028
  tsdbLastFileName(pTsdb, pSetFrom->diskId, pSetFrom->fid, pSetFrom->pLastF, fNameFrom);
  tsdbLastFileName(pTsdb, pSetTo->diskId, pSetTo->fid, pSetTo->pLastF, fNameTo);

H
Hongze Cheng 已提交
2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
  pOutFD = taosOpenFile(fNameTo, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
  if (pOutFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  PInFD = taosOpenFile(fNameFrom, TD_FILE_READ);
  if (PInFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
2041
  n = taosFSendFile(pOutFD, PInFD, 0, pSetFrom->pLastF->size);
H
Hongze Cheng 已提交
2042 2043 2044 2045 2046 2047 2048 2049
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
  taosCloseFile(&pOutFD);
  taosCloseFile(&PInFD);

  // sma
H
Hongze Cheng 已提交
2050 2051
  tsdbSmaFileName(pTsdb, pSetFrom->diskId, pSetFrom->fid, pSetFrom->pSmaF, fNameFrom);
  tsdbSmaFileName(pTsdb, pSetTo->diskId, pSetTo->fid, pSetTo->pSmaF, fNameTo);
H
Hongze Cheng 已提交
2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064

  pOutFD = taosOpenFile(fNameTo, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
  if (pOutFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  PInFD = taosOpenFile(fNameFrom, TD_FILE_READ);
  if (PInFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
2065
  n = taosFSendFile(pOutFD, PInFD, 0, pSetFrom->pSmaF->size);
H
Hongze Cheng 已提交
2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
  taosCloseFile(&pOutFD);
  taosCloseFile(&PInFD);

  return code;

_err:
  tsdbError("vgId:%d tsdb DFileSet copy failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
  return code;
}