tsdbReaderWriter.c 57.2 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 601 602 603 604 605 606 607 608 609 610 611 612 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 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
int32_t tsdbReadBlockL(SDataFReader *pReader, SArray *aBlockL, uint8_t **ppBuf) {
  int32_t  code = 0;
  int64_t  offset = pReader->pSet->pHeadF->loffset;
  int64_t  size = pReader->pSet->pHeadF->offset - offset;
  int64_t  n;
  uint32_t delimiter;
  uint8_t *pBuf = NULL;
  SBlockL  blockl;

  if (!ppBuf) ppBuf = &pBuf;

  // alloc
  code = tRealloc(ppBuf, size);
  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
  n = 0;
  n = tGetU32(*ppBuf + n, &delimiter);
  ASSERT(delimiter == TSDB_FILE_DLMT);

  taosArrayClear(aBlockL);
  while (n < size - sizeof(TSCKSUM)) {
    n += tGetBlockL(*ppBuf + n, &blockl);

    if (taosArrayPush(aBlockL, &blockl) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
  }

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

  tFree(pBuf);
  return code;

_err:
  tsdbError("vgId:%d read blockl failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
  return code;
}

H
Hongze Cheng 已提交
662
int32_t tsdbReadBlock(SDataFReader *pReader, SBlockIdx *pBlockIdx, SMapData *mBlock, uint8_t **ppBuf) {
H
Hongze Cheng 已提交
663 664 665
  int32_t       code = 0;
  int64_t       offset = pBlockIdx->offset;
  int64_t       size = pBlockIdx->size;
H
Hongze Cheng 已提交
666
  uint8_t      *pBuf = NULL;
H
Hongze Cheng 已提交
667
  int64_t       n;
H
Hongze Cheng 已提交
668
  int64_t       tn;
H
Hongze Cheng 已提交
669
  SBlockDataHdr hdr;
H
Hongze Cheng 已提交
670

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

H
Hongze Cheng 已提交
673
  // alloc
H
Hongze Cheng 已提交
674
  code = tRealloc(ppBuf, size);
H
Hongze Cheng 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
  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 已提交
700 701 702 703 704 705
  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 已提交
706 707 708 709 710 711
  tn = tGetMapData(*ppBuf + n, mBlock);
  if (tn < 0) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  n += tn;
H
Hongze Cheng 已提交
712 713
  ASSERT(n + sizeof(TSCKSUM) == size);

H
Hongze Cheng 已提交
714
  tFree(pBuf);
H
Hongze Cheng 已提交
715 716 717 718
  return code;

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

H
Hongze Cheng 已提交
723
static int32_t tsdbReadBlockDataKey(SBlockData *pBlockData, SSubBlock *pSubBlock, uint8_t *pBuf, uint8_t **ppBuf) {
H
Hongze Cheng 已提交
724
  int32_t code = 0;
H
refact  
Hongze Cheng 已提交
725
  int64_t size = pSubBlock->szVersion + pSubBlock->szTSKEY + sizeof(TSCKSUM);
H
Hongze Cheng 已提交
726 727 728 729 730 731 732
  int64_t n;

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

H
Hongze Cheng 已提交
733
  code = tRealloc((uint8_t **)&pBlockData->aVersion, sizeof(int64_t) * pSubBlock->nRow);
H
Hongze Cheng 已提交
734
  if (code) goto _err;
H
Hongze Cheng 已提交
735
  code = tRealloc((uint8_t **)&pBlockData->aTSKEY, sizeof(TSKEY) * pSubBlock->nRow);
H
Hongze Cheng 已提交
736 737 738
  if (code) goto _err;

  if (pSubBlock->cmprAlg == NO_COMPRESSION) {
H
refact  
Hongze Cheng 已提交
739 740
    ASSERT(pSubBlock->szVersion == sizeof(int64_t) * pSubBlock->nRow);
    ASSERT(pSubBlock->szTSKEY == sizeof(TSKEY) * pSubBlock->nRow);
H
Hongze Cheng 已提交
741 742

    // VERSION
H
refact  
Hongze Cheng 已提交
743
    memcpy(pBlockData->aVersion, pBuf, pSubBlock->szVersion);
H
Hongze Cheng 已提交
744 745

    // TSKEY
H
refact  
Hongze Cheng 已提交
746
    memcpy(pBlockData->aTSKEY, pBuf + pSubBlock->szVersion, pSubBlock->szTSKEY);
H
Hongze Cheng 已提交
747 748 749
  } else {
    size = sizeof(int64_t) * pSubBlock->nRow + COMP_OVERFLOW_BYTES;
    if (pSubBlock->cmprAlg == TWO_STAGE_COMP) {
H
Hongze Cheng 已提交
750
      code = tRealloc(ppBuf, size);
H
Hongze Cheng 已提交
751 752 753 754
      if (code) goto _err;
    }

    // VERSION
H
refact  
Hongze Cheng 已提交
755
    n = tsDecompressBigint(pBuf, pSubBlock->szVersion, pSubBlock->nRow, (char *)pBlockData->aVersion,
H
Hongze Cheng 已提交
756 757 758 759 760 761 762
                           sizeof(int64_t) * pSubBlock->nRow, pSubBlock->cmprAlg, *ppBuf, size);
    if (n < 0) {
      code = TSDB_CODE_COMPRESS_ERROR;
      goto _err;
    }

    // TSKEY
H
Hongze Cheng 已提交
763 764 765
    n = tsDecompressTimestamp(pBuf + pSubBlock->szVersion, pSubBlock->szTSKEY, pSubBlock->nRow,
                              (char *)pBlockData->aTSKEY, sizeof(TSKEY) * pSubBlock->nRow, pSubBlock->cmprAlg, *ppBuf,
                              size);
H
Hongze Cheng 已提交
766 767 768 769 770 771 772 773 774 775 776 777
    if (n < 0) {
      code = TSDB_CODE_COMPRESS_ERROR;
      goto _err;
    }
  }

  return code;

_err:
  return code;
}

H
Hongze Cheng 已提交
778 779
static int32_t tsdbReadColDataImpl(SSubBlock *pSubBlock, SBlockCol *pBlockCol, SColData *pColData, uint8_t *pBuf,
                                   uint8_t **ppBuf) {
H
Hongze Cheng 已提交
780 781 782 783
  int32_t code = 0;
  int64_t size;
  int64_t n;

H
Hongze Cheng 已提交
784
  if (!taosCheckChecksumWhole(pBuf, pBlockCol->szBitmap + pBlockCol->szOffset + pBlockCol->szValue + sizeof(TSCKSUM))) {
H
Hongze Cheng 已提交
785 786 787 788 789 790 791
    code = TSDB_CODE_FILE_CORRUPTED;
    goto _err;
  }

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

H
Hongze Cheng 已提交
792
  // BITMAP
H
Hongze Cheng 已提交
793
  if (pBlockCol->flag != HAS_VALUE) {
H
Hongze Cheng 已提交
794 795 796
    ASSERT(pBlockCol->szBitmap);

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

H
Hongze Cheng 已提交
800
    code = tRealloc(ppBuf, size + COMP_OVERFLOW_BYTES);
H
Hongze Cheng 已提交
801
    if (code) goto _err;
H
Hongze Cheng 已提交
802

H
Hongze Cheng 已提交
803 804 805 806 807 808 809 810
    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 已提交
811
  } else {
H
Hongze Cheng 已提交
812
    ASSERT(pBlockCol->szBitmap == 0);
H
Hongze Cheng 已提交
813
  }
H
Hongze Cheng 已提交
814 815 816 817 818
  pBuf = pBuf + pBlockCol->szBitmap;

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

H
Hongze Cheng 已提交
820
    size = sizeof(int32_t) * pColData->nVal;
H
Hongze Cheng 已提交
821
    code = tRealloc((uint8_t **)&pColData->aOffset, size);
H
Hongze Cheng 已提交
822 823
    if (code) goto _err;

H
Hongze Cheng 已提交
824
    code = tRealloc(ppBuf, size + COMP_OVERFLOW_BYTES);
H
Hongze Cheng 已提交
825 826 827 828 829 830 831 832 833 834
    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 已提交
835
  } else {
H
Hongze Cheng 已提交
836
    ASSERT(pBlockCol->szOffset == 0);
H
Hongze Cheng 已提交
837
  }
H
Hongze Cheng 已提交
838 839 840 841 842
  pBuf = pBuf + pBlockCol->szOffset;

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

H
Hongze Cheng 已提交
843
  code = tRealloc(&pColData->pData, pColData->nData);
H
Hongze Cheng 已提交
844 845 846 847 848 849
  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 已提交
850
      code = tRealloc(ppBuf, pColData->nData + COMP_OVERFLOW_BYTES);
H
Hongze Cheng 已提交
851 852 853
      if (code) goto _err;
    }

H
Hongze Cheng 已提交
854 855 856
    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 已提交
857 858 859 860 861 862 863 864 865 866 867 868 869 870
    if (n < 0) {
      code = TSDB_CODE_COMPRESS_ERROR;
      goto _err;
    }

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

  return code;

_err:
  return code;
}

H
Hongze Cheng 已提交
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
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 已提交
903 904
  TdFilePtr  pFD = pBlock->last ? pReader->pLastFD : pReader->pDataFD;
  SSubBlock *pSubBlock = &pBlock->aSubBlock[iSubBlock];
H
Hongze Cheng 已提交
905
  SArray    *aBlockCol = NULL;
H
Hongze Cheng 已提交
906 907 908 909
  int32_t    code = 0;
  int64_t    offset;
  int64_t    size;
  int64_t    n;
H
Hongze Cheng 已提交
910

H
Hongze Cheng 已提交
911 912
  tBlockDataReset(pBlockData);
  pBlockData->nRow = pSubBlock->nRow;
H
Hongze Cheng 已提交
913

H
Hongze Cheng 已提交
914 915 916 917 918 919 920 921 922
  // 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 已提交
923
  code = tRealloc(ppBuf1, size);
H
Hongze Cheng 已提交
924
  if (code) goto _err;
H
Hongze Cheng 已提交
925

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

H
Hongze Cheng 已提交
932 933 934 935 936 937 938 939
  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 已提交
940

H
Hongze Cheng 已提交
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
  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 已提交
959

H
Hongze Cheng 已提交
960
  for (int32_t iCol = 1; iCol < nCol; iCol++) {
H
Hongze Cheng 已提交
961 962 963 964 965 966 967
    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 已提交
968

H
Hongze Cheng 已提交
969
      code = tBlockDataAddColData(pBlockData, taosArrayGetSize(pBlockData->aIdx), &pColData);
H
Hongze Cheng 已提交
970
      if (code) goto _err;
H
Hongze Cheng 已提交
971

H
Hongze Cheng 已提交
972
      tColDataInit(pColData, pBlockCol->cid, pBlockCol->type, pBlockCol->smaOn);
H
Hongze Cheng 已提交
973 974 975 976 977 978
      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 已提交
979 980 981
        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 已提交
982

H
Hongze Cheng 已提交
983
        code = tRealloc(ppBuf1, size);
H
Hongze Cheng 已提交
984 985
        if (code) goto _err;

H
Hongze Cheng 已提交
986 987 988 989 990 991
        // seek
        n = taosLSeekFile(pFD, offset, SEEK_SET);
        if (n < 0) {
          code = TAOS_SYSTEM_ERROR(errno);
          goto _err;
        }
H
Hongze Cheng 已提交
992

H
Hongze Cheng 已提交
993 994 995 996 997 998 999 1000
        // 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 已提交
1001
        }
H
Hongze Cheng 已提交
1002

H
Hongze Cheng 已提交
1003
        code = tsdbReadColDataImpl(pSubBlock, pBlockCol, pColData, *ppBuf1, ppBuf2);
H
Hongze Cheng 已提交
1004
        if (code) goto _err;
H
Hongze Cheng 已提交
1005 1006 1007
      }
    }
  }
H
Hongze Cheng 已提交
1008 1009 1010

_exit:
  taosArrayDestroy(aBlockCol);
H
Hongze Cheng 已提交
1011 1012 1013
  return code;

_err:
H
Hongze Cheng 已提交
1014
  taosArrayDestroy(aBlockCol);
H
Hongze Cheng 已提交
1015 1016 1017 1018 1019 1020 1021 1022 1023
  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;

1024
  ASSERT(aColId[0] == PRIMARYKEY_TIMESTAMP_COL_ID);
H
Hongze Cheng 已提交
1025 1026 1027 1028

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

H
Hongze Cheng 已提交
1029
  code = tsdbReadSubColData(pReader, pBlockIdx, pBlock, 0, aColId, nCol, pBlockData, ppBuf1, ppBuf2);
H
Hongze Cheng 已提交
1030 1031 1032 1033 1034 1035
  if (code) goto _err;

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

H
Hongze Cheng 已提交
1036 1037
    tBlockDataInit(pBlockData1);
    tBlockDataInit(pBlockData2);
H
Hongze Cheng 已提交
1038
    for (int32_t iSubBlock = 1; iSubBlock < pBlock->nSubBlock; iSubBlock++) {
H
Hongze Cheng 已提交
1039
      code = tsdbReadSubColData(pReader, pBlockIdx, pBlock, iSubBlock, aColId, nCol, pBlockData1, ppBuf1, ppBuf2);
H
Hongze Cheng 已提交
1040 1041 1042 1043
      if (code) goto _err;

      code = tBlockDataCopy(pBlockData, pBlockData2);
      if (code) {
H
Hongze Cheng 已提交
1044 1045
        tBlockDataClear(pBlockData1, 1);
        tBlockDataClear(pBlockData2, 1);
H
Hongze Cheng 已提交
1046 1047 1048 1049 1050
        goto _err;
      }

      code = tBlockDataMerge(pBlockData1, pBlockData2, pBlockData);
      if (code) {
H
Hongze Cheng 已提交
1051 1052
        tBlockDataClear(pBlockData1, 1);
        tBlockDataClear(pBlockData2, 1);
H
Hongze Cheng 已提交
1053 1054 1055 1056
        goto _err;
      }
    }

H
Hongze Cheng 已提交
1057 1058
    tBlockDataClear(pBlockData1, 1);
    tBlockDataClear(pBlockData2, 1);
H
Hongze Cheng 已提交
1059
  }
H
Hongze Cheng 已提交
1060

H
Hongze Cheng 已提交
1061 1062
  tFree(pBuf1);
  tFree(pBuf2);
H
Hongze Cheng 已提交
1063 1064 1065 1066
  return code;

_err:
  tsdbError("vgId:%d tsdb read col data failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1067 1068
  tFree(pBuf1);
  tFree(pBuf2);
H
Hongze Cheng 已提交
1069 1070 1071
  return code;
}

H
Hongze Cheng 已提交
1072 1073
static int32_t tsdbReadSubBlockData(SDataFReader *pReader, SBlockIdx *pBlockIdx, SBlock *pBlock, int32_t iSubBlock,
                                    SBlockData *pBlockData, uint8_t **ppBuf1, uint8_t **ppBuf2) {
H
Hongze Cheng 已提交
1074
  int32_t    code = 0;
H
Hongze Cheng 已提交
1075 1076 1077
  uint8_t   *p;
  int64_t    size;
  int64_t    n;
H
Hongze Cheng 已提交
1078
  TdFilePtr  pFD = pBlock->last ? pReader->pLastFD : pReader->pDataFD;
H
Hongze Cheng 已提交
1079
  SSubBlock *pSubBlock = &pBlock->aSubBlock[iSubBlock];
H
Hongze Cheng 已提交
1080
  SArray    *aBlockCol = NULL;
H
Hongze Cheng 已提交
1081 1082

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

H
Hongze Cheng 已提交
1084
  // realloc
H
Hongze Cheng 已提交
1085
  code = tRealloc(ppBuf1, pSubBlock->szBlock);
H
Hongze Cheng 已提交
1086
  if (code) goto _err;
H
Hongze Cheng 已提交
1087

H
Hongze Cheng 已提交
1088 1089 1090 1091 1092 1093
  // seek
  n = taosLSeekFile(pFD, pSubBlock->offset, SEEK_SET);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
H
Hongze Cheng 已提交
1094

H
Hongze Cheng 已提交
1095
  // read
H
refact  
Hongze Cheng 已提交
1096
  n = taosReadFile(pFD, *ppBuf1, pSubBlock->szBlock);
H
Hongze Cheng 已提交
1097 1098 1099
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
H
refact  
Hongze Cheng 已提交
1100
  } else if (n < pSubBlock->szBlock) {
H
Hongze Cheng 已提交
1101 1102 1103
    code = TSDB_CODE_FILE_CORRUPTED;
    goto _err;
  }
H
Hongze Cheng 已提交
1104

H
Hongze Cheng 已提交
1105 1106
  pBlockData->nRow = pSubBlock->nRow;

H
Hongze Cheng 已提交
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
  // 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 已提交
1120
  if (code) goto _err;
H
Hongze Cheng 已提交
1121

H
Hongze Cheng 已提交
1122 1123 1124
  for (int32_t iBlockCol = 0; iBlockCol < taosArrayGetSize(aBlockCol); iBlockCol++) {
    SColData  *pColData;
    SBlockCol *pBlockCol = (SBlockCol *)taosArrayGet(aBlockCol, iBlockCol);
H
Hongze Cheng 已提交
1125

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

H
Hongze Cheng 已提交
1128 1129
    code = tBlockDataAddColData(pBlockData, iBlockCol, &pColData);
    if (code) goto _err;
H
Hongze Cheng 已提交
1130

H
Hongze Cheng 已提交
1131
    tColDataInit(pColData, pBlockCol->cid, pBlockCol->type, pBlockCol->smaOn);
H
Hongze Cheng 已提交
1132 1133 1134 1135
    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 已提交
1136
      }
H
Hongze Cheng 已提交
1137
    } else {
H
Hongze Cheng 已提交
1138 1139 1140
      p = *ppBuf1 + pSubBlock->szBlockCol + sizeof(TSCKSUM) + pSubBlock->szVersion + pSubBlock->szTSKEY +
          sizeof(TSCKSUM) + pBlockCol->offset;
      code = tsdbReadColDataImpl(pSubBlock, pBlockCol, pColData, p, ppBuf2);
H
Hongze Cheng 已提交
1141
      if (code) goto _err;
H
Hongze Cheng 已提交
1142
    }
H
Hongze Cheng 已提交
1143 1144
  }

H
Hongze Cheng 已提交
1145
  taosArrayDestroy(aBlockCol);
H
Hongze Cheng 已提交
1146 1147 1148 1149
  return code;

_err:
  tsdbError("vgId:%d tsdb read sub block data failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1150
  taosArrayDestroy(aBlockCol);
H
Hongze Cheng 已提交
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
  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 已提交
1171 1172 1173 1174
  if (pBlock->nSubBlock > 1) {
    SBlockData *pBlockData1 = &(SBlockData){0};
    SBlockData *pBlockData2 = &(SBlockData){0};

H
Hongze Cheng 已提交
1175 1176
    tBlockDataInit(pBlockData1);
    tBlockDataInit(pBlockData2);
H
Hongze Cheng 已提交
1177
    for (iSubBlock = 1; iSubBlock < pBlock->nSubBlock; iSubBlock++) {
H
Hongze Cheng 已提交
1178
      code = tsdbReadSubBlockData(pReader, pBlockIdx, pBlock, iSubBlock, pBlockData1, ppBuf1, ppBuf2);
H
Hongze Cheng 已提交
1179
      if (code) {
H
Hongze Cheng 已提交
1180 1181
        tBlockDataClear(pBlockData1, 1);
        tBlockDataClear(pBlockData2, 1);
H
Hongze Cheng 已提交
1182 1183 1184 1185 1186
        goto _err;
      }

      code = tBlockDataCopy(pBlockData, pBlockData2);
      if (code) {
H
Hongze Cheng 已提交
1187 1188
        tBlockDataClear(pBlockData1, 1);
        tBlockDataClear(pBlockData2, 1);
H
Hongze Cheng 已提交
1189 1190 1191 1192 1193 1194
        goto _err;
      }

      // merge two block data
      code = tBlockDataMerge(pBlockData1, pBlockData2, pBlockData);
      if (code) {
H
Hongze Cheng 已提交
1195 1196
        tBlockDataClear(pBlockData1, 1);
        tBlockDataClear(pBlockData2, 1);
H
Hongze Cheng 已提交
1197 1198 1199 1200
        goto _err;
      }
    }

H
Hongze Cheng 已提交
1201 1202
    tBlockDataClear(pBlockData1, 1);
    tBlockDataClear(pBlockData2, 1);
H
Hongze Cheng 已提交
1203 1204
  }

H
Hongze Cheng 已提交
1205 1206 1207 1208
  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 已提交
1209 1210
  if (pBuf1) tFree(pBuf1);
  if (pBuf2) tFree(pBuf2);
H
Hongze Cheng 已提交
1211 1212 1213 1214
  return code;

_err:
  tsdbError("vgId:%d tsdb read block data failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1215 1216
  if (pBuf1) tFree(pBuf1);
  if (pBuf2) tFree(pBuf2);
H
Hongze Cheng 已提交
1217 1218 1219
  return code;
}

H
Hongze Cheng 已提交
1220 1221 1222
int32_t tsdbReadBlockSma(SDataFReader *pReader, SBlock *pBlock, SArray *aColumnDataAgg, uint8_t **ppBuf) {
  int32_t   code = 0;
  TdFilePtr pFD = pReader->pSmaFD;
H
Hongze Cheng 已提交
1223
  int64_t   offset = pBlock->aSubBlock[0].sOffset;
H
Hongze Cheng 已提交
1224 1225 1226 1227 1228 1229 1230
  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 已提交
1231
  code = tRealloc(ppBuf, size);
H
Hongze Cheng 已提交
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
  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 已提交
1246 1247 1248
  } else if (n < size) {
    code = TSDB_CODE_FILE_CORRUPTED;
    goto _err;
H
Hongze Cheng 已提交
1249 1250 1251
  }

  // check
H
Hongze Cheng 已提交
1252
  if (!taosCheckChecksumWhole(*ppBuf, size)) {
H
Hongze Cheng 已提交
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
    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 已提交
1266
  tFree(pBuf);
H
Hongze Cheng 已提交
1267 1268 1269 1270
  return code;

_err:
  tsdbError("vgId:%d read block sma failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1271
  tFree(pBuf);
H
Hongze Cheng 已提交
1272 1273 1274 1275 1276
  return code;
}

// SDataFWriter ====================================================
int32_t tsdbDataFWriterOpen(SDataFWriter **ppWriter, STsdb *pTsdb, SDFileSet *pSet) {
H
Hongze Cheng 已提交
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
  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 已提交
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
  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 已提交
1301 1302 1303

  // head
  flag = TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
H
Hongze Cheng 已提交
1304
  tsdbHeadFileName(pTsdb, pWriter->wSet.diskId, pWriter->wSet.fid, &pWriter->fHead, fname);
H
Hongze Cheng 已提交
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
  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 已提交
1319
  pWriter->fHead.size += TSDB_FHDR_SIZE;
H
Hongze Cheng 已提交
1320 1321

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

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

H
Hongze Cheng 已提交
1348
    ASSERT(n == pWriter->fData.size);
H
Hongze Cheng 已提交
1349 1350 1351
  }

  // last
H
Hongze Cheng 已提交
1352
  if (pWriter->fLast.size == 0) {
H
Hongze Cheng 已提交
1353 1354 1355 1356
    flag = TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
  } else {
    flag = TD_FILE_WRITE;
  }
H
Hongze Cheng 已提交
1357
  tsdbLastFileName(pTsdb, pWriter->wSet.diskId, pWriter->wSet.fid, &pWriter->fLast, fname);
H
Hongze Cheng 已提交
1358 1359 1360 1361 1362
  pWriter->pLastFD = taosOpenFile(fname, flag);
  if (pWriter->pLastFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
H
Hongze Cheng 已提交
1363
  if (pWriter->fLast.size == 0) {
H
Hongze Cheng 已提交
1364 1365 1366 1367 1368 1369
    n = taosWriteFile(pWriter->pLastFD, hdr, TSDB_FHDR_SIZE);
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

H
Hongze Cheng 已提交
1370
    pWriter->fLast.size += TSDB_FHDR_SIZE;
H
Hongze Cheng 已提交
1371 1372 1373 1374 1375 1376 1377
  } else {
    n = taosLSeekFile(pWriter->pLastFD, 0, SEEK_END);
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

H
Hongze Cheng 已提交
1378
    ASSERT(n == pWriter->fLast.size);
H
Hongze Cheng 已提交
1379 1380 1381
  }

  // sma
H
Hongze Cheng 已提交
1382
  if (pWriter->fSma.size == 0) {
H
Hongze Cheng 已提交
1383 1384 1385 1386
    flag = TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
  } else {
    flag = TD_FILE_WRITE;
  }
H
Hongze Cheng 已提交
1387
  tsdbSmaFileName(pTsdb, pWriter->wSet.diskId, pWriter->wSet.fid, &pWriter->fSma, fname);
H
Hongze Cheng 已提交
1388 1389 1390 1391 1392
  pWriter->pSmaFD = taosOpenFile(fname, flag);
  if (pWriter->pSmaFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
H
Hongze Cheng 已提交
1393
  if (pWriter->fSma.size == 0) {
H
Hongze Cheng 已提交
1394 1395 1396 1397 1398 1399
    n = taosWriteFile(pWriter->pSmaFD, hdr, TSDB_FHDR_SIZE);
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

H
Hongze Cheng 已提交
1400
    pWriter->fSma.size += TSDB_FHDR_SIZE;
H
Hongze Cheng 已提交
1401 1402 1403 1404 1405 1406 1407
  } else {
    n = taosLSeekFile(pWriter->pSmaFD, 0, SEEK_END);
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

H
Hongze Cheng 已提交
1408
    ASSERT(n == pWriter->fSma.size);
H
Hongze Cheng 已提交
1409 1410 1411 1412 1413 1414 1415 1416
  }

  *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 已提交
1417 1418 1419
  return code;
}

H
Hongze Cheng 已提交
1420
int32_t tsdbDataFWriterClose(SDataFWriter **ppWriter, int8_t sync) {
H
Hongze Cheng 已提交
1421
  int32_t code = 0;
H
Hongze Cheng 已提交
1422
  STsdb  *pTsdb = (*ppWriter)->pTsdb;
H
Hongze Cheng 已提交
1423

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

H
Hongze Cheng 已提交
1426
  if (sync) {
H
Hongze Cheng 已提交
1427
    if (taosFsyncFile((*ppWriter)->pHeadFD) < 0) {
H
Hongze Cheng 已提交
1428 1429 1430 1431
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

H
Hongze Cheng 已提交
1432
    if (taosFsyncFile((*ppWriter)->pDataFD) < 0) {
H
Hongze Cheng 已提交
1433 1434 1435 1436
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

H
Hongze Cheng 已提交
1437
    if (taosFsyncFile((*ppWriter)->pLastFD) < 0) {
H
Hongze Cheng 已提交
1438 1439 1440 1441
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

H
Hongze Cheng 已提交
1442
    if (taosFsyncFile((*ppWriter)->pSmaFD) < 0) {
H
Hongze Cheng 已提交
1443 1444 1445 1446 1447
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }
  }

H
Hongze Cheng 已提交
1448
  if (taosCloseFile(&(*ppWriter)->pHeadFD) < 0) {
H
Hongze Cheng 已提交
1449 1450 1451 1452
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
1453
  if (taosCloseFile(&(*ppWriter)->pDataFD) < 0) {
H
Hongze Cheng 已提交
1454 1455 1456 1457
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
1458
  if (taosCloseFile(&(*ppWriter)->pLastFD) < 0) {
H
Hongze Cheng 已提交
1459 1460 1461 1462
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
1463
  if (taosCloseFile(&(*ppWriter)->pSmaFD) < 0) {
H
Hongze Cheng 已提交
1464 1465 1466 1467
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
1468
  taosMemoryFree(*ppWriter);
H
Hongze Cheng 已提交
1469
_exit:
H
Hongze Cheng 已提交
1470
  *ppWriter = NULL;
H
Hongze Cheng 已提交
1471 1472 1473
  return code;

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

H
Hongze Cheng 已提交
1478
int32_t tsdbUpdateDFileSetHeader(SDataFWriter *pWriter) {
H
Hongze Cheng 已提交
1479
  int32_t code = 0;
H
Hongze Cheng 已提交
1480 1481
  int64_t n;
  char    hdr[TSDB_FHDR_SIZE];
H
Hongze Cheng 已提交
1482 1483

  // head ==============
H
Hongze Cheng 已提交
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
  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 已提交
1499 1500

  // data ==============
H
Hongze Cheng 已提交
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
  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 已提交
1516 1517

  // last ==============
H
Hongze Cheng 已提交
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
  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 已提交
1533 1534

  // sma ==============
H
Hongze Cheng 已提交
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
  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 已提交
1550 1551 1552 1553 1554

  return code;

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

H
Hongze Cheng 已提交
1558
int32_t tsdbWriteBlockIdx(SDataFWriter *pWriter, SArray *aBlockIdx, uint8_t **ppBuf) {
H
Hongze Cheng 已提交
1559
  int32_t    code = 0;
H
Hongze Cheng 已提交
1560
  SHeadFile *pHeadFile = &pWriter->fHead;
H
Hongze Cheng 已提交
1561
  uint8_t   *pBuf = NULL;
H
Hongze Cheng 已提交
1562 1563 1564 1565
  int64_t    size;
  int64_t    n;

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

  // prepare
H
Hongze Cheng 已提交
1568 1569 1570 1571 1572
  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 已提交
1573 1574

  // alloc
H
Hongze Cheng 已提交
1575
  code = tRealloc(ppBuf, size);
H
Hongze Cheng 已提交
1576 1577 1578
  if (code) goto _err;

  // build
H
Hongze Cheng 已提交
1579
  n = 0;
H
Hongze Cheng 已提交
1580 1581 1582 1583
  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 已提交
1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
  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 已提交
1595 1596 1597
  // update
  pHeadFile->offset = pHeadFile->size;
  pHeadFile->size += size;
H
Hongze Cheng 已提交
1598

H
Hongze Cheng 已提交
1599
  tFree(pBuf);
H
Hongze Cheng 已提交
1600 1601 1602 1603
  return code;

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

H
Hongze Cheng 已提交
1608
int32_t tsdbWriteBlock(SDataFWriter *pWriter, SMapData *mBlock, uint8_t **ppBuf, SBlockIdx *pBlockIdx) {
H
Hongze Cheng 已提交
1609
  int32_t       code = 0;
H
Hongze Cheng 已提交
1610
  SHeadFile    *pHeadFile = &pWriter->fHead;
H
Hongze Cheng 已提交
1611 1612 1613 1614
  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 已提交
1615

H
Hongze Cheng 已提交
1616
  ASSERT(mBlock->nItem > 0);
H
Hongze Cheng 已提交
1617 1618

  // prepare
H
Hongze Cheng 已提交
1619
  size = sizeof(SBlockDataHdr) + tPutMapData(NULL, mBlock) + sizeof(TSCKSUM);
H
Hongze Cheng 已提交
1620 1621 1622

  // alloc
  if (!ppBuf) ppBuf = &pBuf;
H
Hongze Cheng 已提交
1623
  code = tRealloc(ppBuf, size);
H
Hongze Cheng 已提交
1624 1625 1626 1627
  if (code) goto _err;

  // build
  n = 0;
H
Hongze Cheng 已提交
1628 1629
  *(SBlockDataHdr *)(*ppBuf) = hdr;
  n += sizeof(hdr);
H
Hongze Cheng 已提交
1630
  n += tPutMapData(*ppBuf + n, mBlock);
H
Hongze Cheng 已提交
1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
  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 已提交
1642 1643
  // update
  pBlockIdx->offset = pHeadFile->size;
H
Hongze Cheng 已提交
1644
  pBlockIdx->size = size;
H
Hongze Cheng 已提交
1645
  pHeadFile->size += size;
H
Hongze Cheng 已提交
1646

H
Hongze Cheng 已提交
1647
  tFree(pBuf);
H
Hongze Cheng 已提交
1648 1649 1650 1651 1652
  tsdbTrace("vgId:%d write block, offset:%" PRId64 " size:%" PRId64, TD_VID(pWriter->pTsdb->pVnode), pBlockIdx->offset,
            pBlockIdx->size);
  return code;

_err:
H
Hongze Cheng 已提交
1653
  tFree(pBuf);
H
Hongze Cheng 已提交
1654
  tsdbError("vgId:%d write block failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1655 1656 1657
  return code;
}

H
Hongze Cheng 已提交
1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704
int32_t tsdbWriteBlockL(SDataFWriter *pWriter, SArray *aBlockL, uint8_t **ppBuf) {
  int32_t    code = 0;
  SHeadFile *pHeadFile = &pWriter->fHead;
  uint8_t   *pBuf = NULL;
  int64_t    size;
  int64_t    n;

  // size
  size = sizeof(uint32_t);
  for (int32_t iBlockL = 0; iBlockL < taosArrayGetSize(aBlockL); iBlockL++) {
    size += tPutBlockL(NULL, taosArrayGet(aBlockL, iBlockL));
  }
  size += sizeof(TSCKSUM);

  // alloc
  if (!ppBuf) ppBuf = &pBuf;
  code = tRealloc(ppBuf, size);
  if (code) goto _err;

  // encode
  n = 0;
  n += tPutU32(*ppBuf + n, TSDB_FILE_DLMT);
  for (int32_t iBlockL = 0; iBlockL < taosArrayGetSize(aBlockL); iBlockL++) {
    n += tPutBlockL(*ppBuf + n, taosArrayGet(aBlockL, iBlockL));
  }
  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;
  }

  // update
  pHeadFile->loffset = pHeadFile->size;
  pHeadFile->size += size;

  return code;

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

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

H
Hongze Cheng 已提交
1709
    if (iRow == 0) {
H
Hongze Cheng 已提交
1710 1711 1712 1713 1714 1715 1716
      if (tsdbKeyCmprFn(&pBlock->minKey, &key) > 0) {
        pBlock->minKey = key;
      }
    } else {
      if (pBlockData->aTSKEY[iRow] == pBlockData->aTSKEY[iRow - 1]) {
        pBlock->hasDup = 1;
      }
H
Hongze Cheng 已提交
1717 1718
    }

H
Hongze Cheng 已提交
1719 1720
    if (iRow == pBlockData->nRow - 1 && tsdbKeyCmprFn(&pBlock->maxKey, &key) < 0) {
      pBlock->maxKey = key;
H
Hongze Cheng 已提交
1721 1722 1723 1724 1725 1726
    }

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

H
Hongze Cheng 已提交
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738
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 已提交
1739
    code = tRealloc(ppBuf1, *nDataP + pSubBlock->szVersion + pSubBlock->szTSKEY + sizeof(TSCKSUM));
H
Hongze Cheng 已提交
1740 1741 1742 1743 1744 1745 1746
    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 已提交
1747
  } else {
H
Hongze Cheng 已提交
1748
    size = (sizeof(int64_t) + sizeof(TSKEY)) * pSubBlock->nRow + COMP_OVERFLOW_BYTES * 2;
H
Hongze Cheng 已提交
1749

H
Hongze Cheng 已提交
1750
    code = tRealloc(ppBuf1, *nDataP + size + sizeof(TSCKSUM));
H
Hongze Cheng 已提交
1751
    if (code) goto _err;
H
Hongze Cheng 已提交
1752

H
Hongze Cheng 已提交
1753 1754
    tsize = sizeof(int64_t) * pSubBlock->nRow + COMP_OVERFLOW_BYTES;
    if (pSubBlock->cmprAlg == TWO_STAGE_COMP) {
H
Hongze Cheng 已提交
1755
      code = tRealloc(ppBuf2, tsize);
H
Hongze Cheng 已提交
1756 1757
      if (code) goto _err;
    }
H
Hongze Cheng 已提交
1758

H
Hongze Cheng 已提交
1759 1760 1761 1762 1763 1764
    // 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 已提交
1765 1766 1767
      goto _err;
    }

H
Hongze Cheng 已提交
1768
    // TSKEY
H
Hongze Cheng 已提交
1769 1770 1771 1772 1773
    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 已提交
1774 1775 1776
      goto _err;
    }

H
Hongze Cheng 已提交
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
    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 已提交
1801
    code = tRealloc(ppBuf1, *nDataP + n + size);
H
Hongze Cheng 已提交
1802 1803
    if (code) goto _err;

H
Hongze Cheng 已提交
1804
    code = tRealloc(ppBuf2, size);
H
Hongze Cheng 已提交
1805 1806 1807 1808 1809 1810 1811
    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 已提交
1812 1813 1814
      goto _err;
    }
  } else {
H
Hongze Cheng 已提交
1815 1816 1817
    pBlockCol->szBitmap = 0;
  }
  n += pBlockCol->szBitmap;
H
Hongze Cheng 已提交
1818

H
Hongze Cheng 已提交
1819 1820 1821
  // OFFSET
  if (IS_VAR_DATA_TYPE(pColData->type)) {
    size = sizeof(int32_t) * pColData->nVal + COMP_OVERFLOW_BYTES;
H
Hongze Cheng 已提交
1822

H
Hongze Cheng 已提交
1823
    code = tRealloc(ppBuf1, *nDataP + n + size);
H
Hongze Cheng 已提交
1824 1825
    if (code) goto _err;

H
Hongze Cheng 已提交
1826
    code = tRealloc(ppBuf2, size);
H
Hongze Cheng 已提交
1827
    if (code) goto _err;
H
Hongze Cheng 已提交
1828

H
Hongze Cheng 已提交
1829 1830 1831
    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 已提交
1832 1833 1834
      code = TSDB_CODE_COMPRESS_ERROR;
      goto _err;
    }
H
Hongze Cheng 已提交
1835 1836 1837 1838
  } else {
    pBlockCol->szOffset = 0;
  }
  n += pBlockCol->szOffset;
H
Hongze Cheng 已提交
1839

H
Hongze Cheng 已提交
1840 1841 1842 1843
  // VALUE
  if (pSubBlock->cmprAlg == NO_COMPRESSION) {
    pBlockCol->szValue = pColData->nData;

H
Hongze Cheng 已提交
1844
    code = tRealloc(ppBuf1, *nDataP + n + pBlockCol->szValue + sizeof(TSCKSUM));
H
Hongze Cheng 已提交
1845 1846 1847 1848 1849 1850
    if (code) goto _err;

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

H
Hongze Cheng 已提交
1851
    code = tRealloc(ppBuf1, *nDataP + n + size + sizeof(TSCKSUM));
H
Hongze Cheng 已提交
1852 1853 1854
    if (code) goto _err;

    if (pSubBlock->cmprAlg == TWO_STAGE_COMP) {
H
Hongze Cheng 已提交
1855
      code = tRealloc(ppBuf2, size);
H
Hongze Cheng 已提交
1856 1857 1858 1859 1860 1861 1862
      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 已提交
1863 1864 1865
      code = TSDB_CODE_COMPRESS_ERROR;
      goto _err;
    }
H
Hongze Cheng 已提交
1866 1867 1868
  }
  n += pBlockCol->szValue;
  pBlockCol->szOrigin = pColData->nData;
H
Hongze Cheng 已提交
1869

H
Hongze Cheng 已提交
1870 1871 1872
  // checksum
  n += sizeof(TSCKSUM);
  taosCalcChecksumAppend(0, *ppBuf1 + *nDataP, n);
H
Hongze Cheng 已提交
1873

H
Hongze Cheng 已提交
1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894
  *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 已提交
1895
  code = tRealloc(ppBuf, pSubBlock->szBlockCol + sizeof(TSCKSUM));
H
Hongze Cheng 已提交
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926
  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 已提交
1927
static int32_t tsdbWriteBlockSma(TdFilePtr pFD, SBlockData *pBlockData, SSubBlock *pSubBlock, uint8_t **ppBuf) {
H
Hongze Cheng 已提交
1928 1929 1930
  int32_t   code = 0;
  int64_t   n;
  SColData *pColData;
H
Hongze Cheng 已提交
1931

H
Hongze Cheng 已提交
1932 1933
  // prepare
  pSubBlock->nSma = 0;
H
Hongze Cheng 已提交
1934 1935
  for (int32_t iColData = 0; iColData < taosArrayGetSize(pBlockData->aIdx); iColData++) {
    pColData = tBlockDataGetColDataByIdx(pBlockData, iColData);
H
Hongze Cheng 已提交
1936

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

H
Hongze Cheng 已提交
1939 1940 1941
    pSubBlock->nSma++;
  }
  if (pSubBlock->nSma == 0) goto _exit;
H
Hongze Cheng 已提交
1942

H
Hongze Cheng 已提交
1943
  // calc
H
Hongze Cheng 已提交
1944
  code = tRealloc(ppBuf, sizeof(SColumnDataAgg) * pSubBlock->nSma + sizeof(TSCKSUM));
H
Hongze Cheng 已提交
1945 1946
  if (code) goto _err;
  n = 0;
H
Hongze Cheng 已提交
1947 1948
  for (int32_t iColData = 0; iColData < taosArrayGetSize(pBlockData->aIdx); iColData++) {
    pColData = tBlockDataGetColDataByIdx(pBlockData, iColData);
H
Hongze Cheng 已提交
1949

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

H
Hongze Cheng 已提交
1952 1953 1954 1955
    tsdbCalcColDataSMA(pColData, &((SColumnDataAgg *)(*ppBuf))[n]);
    n++;
  }
  taosCalcChecksumAppend(0, *ppBuf, sizeof(SColumnDataAgg) * pSubBlock->nSma + sizeof(TSCKSUM));
H
Hongze Cheng 已提交
1956

H
Hongze Cheng 已提交
1957 1958 1959 1960 1961 1962
  // write
  n = taosWriteFile(pFD, *ppBuf, sizeof(SColumnDataAgg) * pSubBlock->nSma + sizeof(TSCKSUM));
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
H
Hongze Cheng 已提交
1963

H
Hongze Cheng 已提交
1964 1965
_exit:
  return code;
H
Hongze Cheng 已提交
1966

H
Hongze Cheng 已提交
1967 1968 1969
_err:
  return code;
}
H
Hongze Cheng 已提交
1970

H
Hongze Cheng 已提交
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
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 已提交
1985

H
Hongze Cheng 已提交
1986 1987
  if (!ppBuf1) ppBuf1 = &pBuf1;
  if (!ppBuf2) ppBuf2 = &pBuf2;
H
Hongze Cheng 已提交
1988

H
Hongze Cheng 已提交
1989
  tsdbUpdateBlockInfo(pBlockData, pBlock);
H
Hongze Cheng 已提交
1990

H
Hongze Cheng 已提交
1991 1992 1993
  pSubBlock->nRow = pBlockData->nRow;
  pSubBlock->cmprAlg = cmprAlg;
  if (pBlock->last) {
H
Hongze Cheng 已提交
1994
    pSubBlock->offset = pWriter->fLast.size;
H
Hongze Cheng 已提交
1995
  } else {
H
Hongze Cheng 已提交
1996
    pSubBlock->offset = pWriter->fData.size;
H
Hongze Cheng 已提交
1997 1998 1999 2000 2001 2002 2003 2004 2005
  }

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

  // COLUMNS
H
Hongze Cheng 已提交
2006
  aBlockCol = taosArrayInit(taosArrayGetSize(pBlockData->aIdx), sizeof(SBlockCol));
H
Hongze Cheng 已提交
2007 2008 2009 2010 2011
  if (aBlockCol == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  int32_t offset = 0;
H
Hongze Cheng 已提交
2012 2013
  for (int32_t iCol = 0; iCol < taosArrayGetSize(pBlockData->aIdx); iCol++) {
    SColData *pColData = tBlockDataGetColDataByIdx(pBlockData, iCol);
H
Hongze Cheng 已提交
2014 2015 2016 2017 2018 2019 2020

    ASSERT(pColData->flag);

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

    pBlockCol->cid = pColData->cid;
    pBlockCol->type = pColData->type;
H
Hongze Cheng 已提交
2021
    pBlockCol->smaOn = pColData->smaOn;
H
Hongze Cheng 已提交
2022 2023 2024 2025 2026 2027 2028 2029
    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 已提交
2030 2031
    }

H
Hongze Cheng 已提交
2032 2033 2034 2035
    if (taosArrayPush(aBlockCol, pBlockCol) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
2036 2037
  }

H
Hongze Cheng 已提交
2038 2039 2040 2041 2042
  // write
  code = tsdbWriteBlockDataImpl(pFileFD, pSubBlock, hdr, aBlockCol, *ppBuf1, nData, ppBuf2);
  if (code) goto _err;

  pSubBlock->szBlock = pSubBlock->szBlockCol + sizeof(TSCKSUM) + nData;
H
Hongze Cheng 已提交
2043
  if (pBlock->last) {
H
Hongze Cheng 已提交
2044
    pWriter->fLast.size += pSubBlock->szBlock;
H
Hongze Cheng 已提交
2045
  } else {
H
Hongze Cheng 已提交
2046
    pWriter->fData.size += pSubBlock->szBlock;
H
Hongze Cheng 已提交
2047 2048
  }

H
Hongze Cheng 已提交
2049 2050 2051 2052 2053 2054
  // ======================= BLOCK SMA =======================
  pSubBlock->sOffset = 0;
  pSubBlock->nSma = 0;

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

H
Hongze Cheng 已提交
2055
  code = tsdbWriteBlockSma(pWriter->pSmaFD, pBlockData, pSubBlock, ppBuf1);
H
Hongze Cheng 已提交
2056 2057 2058
  if (code) goto _err;

  if (pSubBlock->nSma > 0) {
H
Hongze Cheng 已提交
2059 2060
    pSubBlock->sOffset = pWriter->fSma.size;
    pWriter->fSma.size += (sizeof(SColumnDataAgg) * pSubBlock->nSma + sizeof(TSCKSUM));
H
Hongze Cheng 已提交
2061 2062 2063
  }

_exit:
H
Hongze Cheng 已提交
2064 2065
  tFree(pBuf1);
  tFree(pBuf2);
H
Hongze Cheng 已提交
2066
  taosArrayDestroy(aBlockCol);
H
Hongze Cheng 已提交
2067 2068 2069
  return code;

_err:
H
Hongze Cheng 已提交
2070
  tsdbError("vgId:%d write block data failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
2071 2072
  tFree(pBuf1);
  tFree(pBuf2);
H
Hongze Cheng 已提交
2073
  taosArrayDestroy(aBlockCol);
H
Hongze Cheng 已提交
2074 2075
  return code;
}
H
Hongze Cheng 已提交
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086

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 已提交
2087 2088
  tsdbHeadFileName(pTsdb, pSetFrom->diskId, pSetFrom->fid, pSetFrom->pHeadF, fNameFrom);
  tsdbHeadFileName(pTsdb, pSetTo->diskId, pSetTo->fid, pSetTo->pHeadF, fNameTo);
H
Hongze Cheng 已提交
2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101

  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 已提交
2102
  n = taosFSendFile(pOutFD, PInFD, 0, pSetFrom->pHeadF->size);
H
Hongze Cheng 已提交
2103 2104 2105 2106 2107 2108 2109 2110
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
  taosCloseFile(&pOutFD);
  taosCloseFile(&PInFD);

  // data
H
Hongze Cheng 已提交
2111 2112
  tsdbDataFileName(pTsdb, pSetFrom->diskId, pSetFrom->fid, pSetFrom->pDataF, fNameFrom);
  tsdbDataFileName(pTsdb, pSetTo->diskId, pSetTo->fid, pSetTo->pDataF, fNameTo);
H
Hongze Cheng 已提交
2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125

  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 已提交
2126
  n = taosFSendFile(pOutFD, PInFD, 0, pSetFrom->pDataF->size);
H
Hongze Cheng 已提交
2127 2128 2129 2130 2131 2132 2133 2134
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
  taosCloseFile(&pOutFD);
  taosCloseFile(&PInFD);

  // last
H
Hongze Cheng 已提交
2135 2136 2137
  tsdbLastFileName(pTsdb, pSetFrom->diskId, pSetFrom->fid, pSetFrom->pLastF, fNameFrom);
  tsdbLastFileName(pTsdb, pSetTo->diskId, pSetTo->fid, pSetTo->pLastF, fNameTo);

H
Hongze Cheng 已提交
2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149
  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 已提交
2150
  n = taosFSendFile(pOutFD, PInFD, 0, pSetFrom->pLastF->size);
H
Hongze Cheng 已提交
2151 2152 2153 2154 2155 2156 2157 2158
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
  taosCloseFile(&pOutFD);
  taosCloseFile(&PInFD);

  // sma
H
Hongze Cheng 已提交
2159 2160
  tsdbSmaFileName(pTsdb, pSetFrom->diskId, pSetFrom->fid, pSetFrom->pSmaF, fNameFrom);
  tsdbSmaFileName(pTsdb, pSetTo->diskId, pSetTo->fid, pSetTo->pSmaF, fNameTo);
H
Hongze Cheng 已提交
2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173

  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 已提交
2174
  n = taosFSendFile(pOutFD, PInFD, 0, pSetFrom->pSmaF->size);
H
Hongze Cheng 已提交
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
  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;
}