tsdbReaderWriter.c 41.5 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 52
  pDelFWriter->fDel.size = TSDB_FHDR_SIZE;
  pDelFWriter->fDel.size = 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
int32_t tsdbDelFWriterClose(SDelFWriter *pWriter, int8_t sync) {
H
Hongze Cheng 已提交
64
  int32_t code = 0;
H
Hongze Cheng 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

  // 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;
  }

  return code;

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

H
Hongze Cheng 已提交
85 86 87 88 89 90 91 92
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 已提交
93

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

  // alloc
  code = tsdbRealloc(ppBuf, size);
  if (code) goto _err;

  // build
H
Hongze Cheng 已提交
106 107 108 109
  n = 0;
  *(SBlockDataHdr *)(*ppBuf) = hdr;
  n += sizeof(hdr);
  for (int32_t iDelData = 0; iDelData < taosArrayGetSize(aDelData); iDelData++) {
H
Hongze Cheng 已提交
110
    n += tPutDelData(*ppBuf + n, taosArrayGet(aDelData, iDelData));
H
Hongze Cheng 已提交
111
  }
H
Hongze Cheng 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125
  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 已提交
126
  pDelIdx->offset = pWriter->fDel.size;
H
Hongze Cheng 已提交
127
  pDelIdx->size = size;
H
Hongze Cheng 已提交
128
  pWriter->fDel.size += size;
H
Hongze Cheng 已提交
129 130 131 132 133 134 135

  tsdbFree(pBuf);
  return code;

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
181 182 183 184
  tsdbFree(pBuf);
  return code;

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

H
Hongze Cheng 已提交
190 191 192 193 194
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 已提交
195 196

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

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

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

H
Hongze Cheng 已提交
214 215 216
  return code;

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

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

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

  // 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 已提交
243 244 245
  pDelFReader->fDel = *pFile;

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

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

H
Hongze Cheng 已提交
261 262 263 264 265
    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 已提交
266 267 268 269 270 271 272 273 274 275 276
      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 已提交
277
#endif
H
Hongze Cheng 已提交
278 279 280 281 282 283

_exit:
  *ppReader = pDelFReader;
  return code;

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

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

  if (pReader) {
H
Hongze Cheng 已提交
293 294 295 296
    if (taosCloseFile(&pReader->pReadH) < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _exit;
    }
H
Hongze Cheng 已提交
297 298 299
    taosMemoryFree(pReader);
  }

H
Hongze Cheng 已提交
300
_exit:
H
Hongze Cheng 已提交
301 302 303
  return code;
}

H
Hongze Cheng 已提交
304 305 306 307 308 309 310 311 312 313
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 已提交
314 315

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

  // alloc
H
Hongze Cheng 已提交
322
  code = tsdbRealloc(ppBuf, size);
H
Hongze Cheng 已提交
323 324 325
  if (code) goto _err;

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

  // check
H
Hongze Cheng 已提交
336
  if (!taosCheckChecksumWhole(*ppBuf, size)) {
H
Hongze Cheng 已提交
337 338 339 340
    code = TSDB_CODE_FILE_CORRUPTED;
    goto _err;
  }

H
Hongze Cheng 已提交
341 342
  // // decode
  n = 0;
H
Hongze Cheng 已提交
343 344 345 346 347 348 349 350
  pHdr = (SBlockDataHdr *)(*ppBuf + n);
  ASSERT(pHdr->delimiter == TSDB_FILE_DLMT);
  ASSERT(pHdr->suid == pDelIdx->suid);
  ASSERT(pHdr->uid == pDelIdx->uid);
  n += sizeof(*pHdr);
  while (n < size - sizeof(TSCKSUM)) {
    n += tGetDelData(*ppBuf + n, pDelData);
  }
H
Hongze Cheng 已提交
351

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

  tsdbFree(pBuf);
H
Hongze Cheng 已提交
355 356 357 358
  return code;

_err:
  tsdbError("vgId:%d read del data failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
359
  tsdbFree(pBuf);
H
Hongze Cheng 已提交
360 361 362
  return code;
}

H
Hongze Cheng 已提交
363
int32_t tsdbReadDelIdx(SDelFReader *pReader, SArray *aDelIdx, uint8_t **ppBuf) {
H
Hongze Cheng 已提交
364 365
  int32_t  code = 0;
  int32_t  n;
H
Hongze Cheng 已提交
366 367
  int64_t  offset = pReader->fDel.offset;
  int64_t  size = pReader->fDel.size - offset;
H
Hongze Cheng 已提交
368
  uint32_t delimiter;
H
Hongze Cheng 已提交
369 370
  uint8_t *pBuf = NULL;
  SDelIdx *pDelIdx = &(SDelIdx){};
H
Hongze Cheng 已提交
371

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

H
Hongze Cheng 已提交
374
  // seek
H
Hongze Cheng 已提交
375
  if (taosLSeekFile(pReader->pReadH, offset, SEEK_SET) < 0) {
H
Hongze Cheng 已提交
376 377 378 379
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
380 381
  // alloc
  code = tsdbRealloc(ppBuf, size);
H
Hongze Cheng 已提交
382
  if (code) goto _err;
H
Hongze Cheng 已提交
383

H
Hongze Cheng 已提交
384
  // read
H
Hongze Cheng 已提交
385 386
  n = taosReadFile(pReader->pReadH, *ppBuf, size);
  if (n < 0) {
H
Hongze Cheng 已提交
387
    code = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
388
    goto _err;
H
Hongze Cheng 已提交
389 390 391
  } else if (n < size) {
    code = TSDB_CODE_FILE_CORRUPTED;
    goto _err;
H
Hongze Cheng 已提交
392 393 394 395 396 397 398 399 400
  }

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

  // decode
H
Hongze Cheng 已提交
401 402 403
  n = 0;
  n += tGetU32(*ppBuf + n, &delimiter);
  ASSERT(delimiter == TSDB_FILE_DLMT);
H
Hongze Cheng 已提交
404 405 406 407 408 409 410 411 412 413 414 415

  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 已提交
416 417 418 419

  return code;

_err:
H
Hongze Cheng 已提交
420
  tsdbError("vgId:%d read del idx failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
421
  return code;
H
Hongze Cheng 已提交
422 423 424 425 426 427
}

// SDataFReader ====================================================
struct SDataFReader {
  STsdb     *pTsdb;
  SDFileSet *pSet;
H
Hongze Cheng 已提交
428 429 430 431
  TdFilePtr  pHeadFD;
  TdFilePtr  pDataFD;
  TdFilePtr  pLastFD;
  TdFilePtr  pSmaFD;
H
Hongze Cheng 已提交
432 433 434
};

int32_t tsdbDataFReaderOpen(SDataFReader **ppReader, STsdb *pTsdb, SDFileSet *pSet) {
H
Hongze Cheng 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
  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
  tsdbDataFileName(pTsdb, pSet, TSDB_HEAD_FILE, fname);
  pReader->pHeadFD = taosOpenFile(fname, TD_FILE_READ);
  if (pReader->pHeadFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  // data
  tsdbDataFileName(pTsdb, pSet, TSDB_DATA_FILE, fname);
  pReader->pDataFD = taosOpenFile(fname, TD_FILE_READ);
  if (pReader->pDataFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  // last
  tsdbDataFileName(pTsdb, pSet, TSDB_LAST_FILE, fname);
  pReader->pLastFD = taosOpenFile(fname, TD_FILE_READ);
  if (pReader->pLastFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  // sma
  tsdbDataFileName(pTsdb, pSet, TSDB_SMA_FILE, fname);
  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 已提交
487 488 489
  return code;
}

H
Hongze Cheng 已提交
490
int32_t tsdbDataFReaderClose(SDataFReader **ppReader) {
H
Hongze Cheng 已提交
491
  int32_t code = 0;
H
Hongze Cheng 已提交
492

H
Hongze Cheng 已提交
493
  if (taosCloseFile(&(*ppReader)->pHeadFD) < 0) {
H
Hongze Cheng 已提交
494 495 496 497
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
498
  if (taosCloseFile(&(*ppReader)->pDataFD) < 0) {
H
Hongze Cheng 已提交
499 500 501 502
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
503
  if (taosCloseFile(&(*ppReader)->pLastFD) < 0) {
H
Hongze Cheng 已提交
504 505 506 507
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

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

H
Hongze Cheng 已提交
513 514
  taosMemoryFree(*ppReader);
  *ppReader = NULL;
H
Hongze Cheng 已提交
515 516 517
  return code;

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

H
Hongze Cheng 已提交
522
int32_t tsdbReadBlockIdx(SDataFReader *pReader, SMapData *mBlockIdx, uint8_t **ppBuf) {
H
Hongze Cheng 已提交
523
  int32_t  code = 0;
H
Hongze Cheng 已提交
524 525
  int64_t  offset = pReader->pSet->fHead.offset;
  int64_t  size = pReader->pSet->fHead.size - offset;
H
Hongze Cheng 已提交
526 527 528 529
  int64_t  n;
  uint32_t delimiter;

  // alloc
H
Hongze Cheng 已提交
530
  if (!ppBuf) ppBuf = &mBlockIdx->pBuf;
H
Hongze Cheng 已提交
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
  code = tsdbRealloc(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);
H
Hongze Cheng 已提交
560
  n += tGetMapData(*ppBuf + n, mBlockIdx);
H
Hongze Cheng 已提交
561 562 563 564 565 566
  ASSERT(n + sizeof(TSCKSUM) == size);

  return code;

_err:
  tsdbError("vgId:%d read block idx failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
567 568 569
  return code;
}

H
Hongze Cheng 已提交
570
int32_t tsdbReadBlock(SDataFReader *pReader, SBlockIdx *pBlockIdx, SMapData *mBlock, uint8_t **ppBuf) {
H
Hongze Cheng 已提交
571 572 573 574 575
  int32_t       code = 0;
  int64_t       offset = pBlockIdx->offset;
  int64_t       size = pBlockIdx->size;
  int64_t       n;
  SBlockDataHdr hdr;
H
Hongze Cheng 已提交
576 577

  // alloc
H
Hongze Cheng 已提交
578
  if (!ppBuf) ppBuf = &mBlock->pBuf;
H
Hongze Cheng 已提交
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
  code = tsdbRealloc(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
H
Hongze Cheng 已提交
605 606 607 608 609 610
  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 已提交
611
  n += tGetMapData(*ppBuf + n, mBlock);
H
Hongze Cheng 已提交
612 613 614 615 616 617
  ASSERT(n + sizeof(TSCKSUM) == size);

  return code;

_err:
  tsdbError("vgId:%d read block failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
618 619 620
  return code;
}

H
Hongze Cheng 已提交
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 662 663 664 665 666 667 668 669 670 671 672 673 674 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 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
static int32_t tsdbRecoverBlockDataKey(SBlockData *pBlockData, SSubBlock *pSubBlock, uint8_t *pBuf, uint8_t **ppBuf) {
  int32_t code = 0;
  int64_t size = pSubBlock->vsize + pSubBlock->ksize + sizeof(TSCKSUM);
  int64_t n;

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

  code = tsdbRealloc((uint8_t **)&pBlockData->aVersion, sizeof(int64_t) * pSubBlock->nRow);
  if (code) goto _err;
  code = tsdbRealloc((uint8_t **)&pBlockData->aTSKEY, sizeof(TSKEY) * pSubBlock->nRow);
  if (code) goto _err;

  if (pSubBlock->cmprAlg == NO_COMPRESSION) {
    ASSERT(pSubBlock->vsize == sizeof(int64_t) * pSubBlock->nRow);
    ASSERT(pSubBlock->ksize == sizeof(TSKEY) * pSubBlock->nRow);

    // VERSION
    memcpy(pBlockData->aVersion, pBuf, pSubBlock->vsize);

    // TSKEY
    pBuf = pBuf + pSubBlock->vsize;
    memcpy(pBlockData->aTSKEY, pBuf + pSubBlock->vsize, pSubBlock->ksize);
  } else {
    size = sizeof(int64_t) * pSubBlock->nRow + COMP_OVERFLOW_BYTES;
    if (pSubBlock->cmprAlg == TWO_STAGE_COMP) {
      code = tsdbRealloc(ppBuf, size);
      if (code) goto _err;
    }

    // VERSION
    n = tsDecompressBigint(pBuf, pSubBlock->vsize, pSubBlock->nRow, (char *)pBlockData->aVersion,
                           sizeof(int64_t) * pSubBlock->nRow, pSubBlock->cmprAlg, *ppBuf, size);
    if (n < 0) {
      code = TSDB_CODE_COMPRESS_ERROR;
      goto _err;
    }

    // TSKEY
    pBuf = pBuf + pSubBlock->vsize;
    n = tsDecompressTimestamp(pBuf, pSubBlock->ksize, pSubBlock->nRow, (char *)pBlockData->aTSKEY,
                              sizeof(TSKEY) * pSubBlock->nRow, pSubBlock->cmprAlg, *ppBuf, size);
    if (n < 0) {
      code = TSDB_CODE_COMPRESS_ERROR;
      goto _err;
    }
  }

  return code;

_err:
  return code;
}

static int32_t tsdbRecoverColData(SBlockData *pBlockData, SSubBlock *pSubBlock, SBlockCol *pBlockCol,
                                  SColData *pColData, uint8_t *pBuf, uint8_t **ppBuf) {
  int32_t code = 0;
  int64_t size;
  int64_t n;

  ASSERT(pBlockCol->flag != HAS_NULL);

  if (!taosCheckChecksumWhole(pBuf, pBlockCol->bsize + pBlockCol->csize + sizeof(TSCKSUM))) {
    code = TSDB_CODE_FILE_CORRUPTED;
    goto _err;
  }

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

  // bitmap
  if (pBlockCol->flag != HAS_VALUE) {
    size = BIT2_SIZE(pSubBlock->nRow);
    code = tsdbRealloc(&pColData->pBitMap, size);
    if (code) goto _err;

    ASSERT(pBlockCol->bsize == size);

    memcpy(pColData->pBitMap, pBuf, size);
  } else {
    ASSERT(pBlockCol->bsize == 0);
  }
  pBuf = pBuf + pBlockCol->bsize;

  // value
  if (IS_VAR_DATA_TYPE(pBlockCol->type)) {
    pColData->nData = pBlockCol->osize;
  } else {
    pColData->nData = tDataTypes[pBlockCol->type].bytes * pSubBlock->nRow;
  }
  code = tsdbRealloc(&pColData->pData, pColData->nData);
  if (code) goto _err;

  if (pSubBlock->cmprAlg == NO_COMPRESSION) {
    memcpy(pColData->pData, pBuf, pColData->nData);
  } else {
    size = pColData->nData + COMP_OVERFLOW_BYTES;
    if (pSubBlock->cmprAlg == TWO_STAGE_COMP) {
      code = tsdbRealloc(ppBuf, size);
      if (code) goto _err;
    }

    n = tDataTypes[pBlockCol->type].decompFunc(pBuf, pBlockCol->csize, pSubBlock->nRow, pColData->pData,
                                               pColData->nData, pSubBlock->cmprAlg, *ppBuf, size);
    if (n < 0) {
      code = TSDB_CODE_COMPRESS_ERROR;
      goto _err;
    }

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

  return code;

_err:
  return code;
}

H
Hongze Cheng 已提交
741
int32_t tsdbReadColData(SDataFReader *pReader, SBlockIdx *pBlockIdx, SBlock *pBlock, int16_t *aColId, int32_t nCol,
H
Hongze Cheng 已提交
742
                        SBlockData *pBlockData, uint8_t **ppBuf1, uint8_t **ppBuf2) {
H
Hongze Cheng 已提交
743 744
  int32_t   code = 0;
  TdFilePtr pFD = pBlock->last ? pReader->pLastFD : pReader->pDataFD;
H
Hongze Cheng 已提交
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 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 841 842
  uint8_t  *pBuf1 = NULL;
  uint8_t  *pBuf2 = NULL;

  ASSERT(nCol == 0 || aColId[0] != PRIMARYKEY_TIMESTAMP_COL_ID);

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

  for (int32_t iSubBlock = 0; iSubBlock < pBlock->nSubBlock; iSubBlock++) {
    SSubBlock *pSubBlock = &pBlock->aSubBlock[iSubBlock];
    int64_t    offset;
    int64_t    size;
    int64_t    n;

    tBlockDataReset(pBlockData);
    pBlockData->nRow = pSubBlock->nRow;

    // TSDBKEY
    offset = pSubBlock->offset + sizeof(SBlockDataHdr);
    size = pSubBlock->vsize + pSubBlock->ksize + sizeof(TSCKSUM);
    code = tsdbRealloc(ppBuf1, size);
    if (code) goto _err;

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

    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;
    }

    code = tsdbRecoverBlockDataKey(pBlockData, pSubBlock, *ppBuf1, ppBuf2);
    if (code) goto _err;

    // OTHER
    SBlockCol  blockCol;
    SBlockCol *pBlockCol = &blockCol;
    SColData  *pColData;
    for (int32_t iCol = 0; iCol < nCol; iCol++) {
      int16_t cid = aColId[iCol];

      if (tMapDataSearch(&pSubBlock->mBlockCol, &(SBlockCol){.cid = cid}, tGetBlockCol, tBlockColCmprFn, pBlockCol) ==
          0) {
        code = tBlockDataAddColData(pBlockData, taosArrayGetSize(pBlockData->aColDataP), &pColData);
        if (code) goto _err;

        tColDataReset(pColData, pBlockCol->cid, pBlockCol->type);
        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 {
          offset = pSubBlock->offset + sizeof(SBlockDataHdr) + pSubBlock->vsize + pSubBlock->ksize + pBlockCol->offset;
          size = pBlockCol->bsize + pBlockCol->csize + sizeof(TSCKSUM);

          code = tsdbRealloc(ppBuf1, size);
          if (code) goto _err;

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

          // 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;
          }

          code = tsdbRecoverColData(pBlockData, pSubBlock, pBlockCol, pColData, *ppBuf1, ppBuf2);
          if (code) goto _err;
        }
      }
    }
  }

  tsdbFree(pBuf1);
  tsdbFree(pBuf2);
  return code;

_err:
  tsdbError("vgId:%d tsdb read col data failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
  tsdbFree(pBuf1);
  tsdbFree(pBuf2);
H
Hongze Cheng 已提交
843 844 845
  return code;
}

H
Hongze Cheng 已提交
846 847
static int32_t tsdbReadSubBlockData(SDataFReader *pReader, SBlockIdx *pBlockIdx, SBlock *pBlock, int32_t iSubBlock,
                                    SBlockData *pBlockData, uint8_t **ppBuf1, uint8_t **ppBuf2) {
H
Hongze Cheng 已提交
848
  int32_t    code = 0;
H
Hongze Cheng 已提交
849 850 851
  uint8_t   *p;
  int64_t    size;
  int64_t    n;
H
Hongze Cheng 已提交
852
  TdFilePtr  pFD = pBlock->last ? pReader->pLastFD : pReader->pDataFD;
H
Hongze Cheng 已提交
853
  SSubBlock *pSubBlock = &pBlock->aSubBlock[iSubBlock];
H
Hongze Cheng 已提交
854 855 856
  SBlockCol *pBlockCol = &(SBlockCol){0};

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

H
Hongze Cheng 已提交
858 859 860
  // realloc
  code = tsdbRealloc(ppBuf1, pSubBlock->bsize);
  if (code) goto _err;
H
Hongze Cheng 已提交
861

H
Hongze Cheng 已提交
862 863 864 865 866 867
  // seek
  n = taosLSeekFile(pFD, pSubBlock->offset, SEEK_SET);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
H
Hongze Cheng 已提交
868

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

H
more  
Hongze Cheng 已提交
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
  // // check
  // p = *ppBuf1;
  // SBlockDataHdr *pHdr = (SBlockDataHdr *)p;
  // ASSERT(pHdr->delimiter == TSDB_FILE_DLMT);
  // ASSERT(pHdr->suid == pBlockIdx->suid);
  // ASSERT(pHdr->uid == pBlockIdx->uid);
  // p += sizeof(*pHdr);

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

  // for (int32_t iBlockCol = 0; iBlockCol < pSubBlock->mBlockCol.nItem; iBlockCol++) {
  //   tMapDataGetItemByIdx(&pSubBlock->mBlockCol, iBlockCol, pBlockCol, tGetBlockCol);

  //   ASSERT(pBlockCol->flag && pBlockCol->flag != HAS_NONE);

  //   if (pBlockCol->flag == HAS_NULL) continue;

  //   if (!taosCheckChecksumWhole(p, pBlockCol->bsize + pBlockCol->csize + sizeof(TSCKSUM))) {
  //     code = TSDB_CODE_FILE_CORRUPTED;
  //     goto _err;
  //   }
  //   p = p + pBlockCol->bsize + pBlockCol->csize + sizeof(TSCKSUM);
  // }
H
Hongze Cheng 已提交
906 907 908

  // recover
  pBlockData->nRow = pSubBlock->nRow;
H
more  
Hongze Cheng 已提交
909
  p = *ppBuf1 + sizeof(SBlockDataHdr);
H
Hongze Cheng 已提交
910

H
Hongze Cheng 已提交
911
  code = tsdbRecoverBlockDataKey(pBlockData, pSubBlock, p, ppBuf2);
H
Hongze Cheng 已提交
912 913
  if (code) goto _err;
  p = p + pSubBlock->vsize + pSubBlock->ksize + sizeof(TSCKSUM);
H
Hongze Cheng 已提交
914

H
Hongze Cheng 已提交
915 916
  for (int32_t iBlockCol = 0; iBlockCol < pSubBlock->mBlockCol.nItem; iBlockCol++) {
    SColData *pColData;
H
Hongze Cheng 已提交
917

H
Hongze Cheng 已提交
918 919
    tMapDataGetItemByIdx(&pSubBlock->mBlockCol, iBlockCol, pBlockCol, tGetBlockCol);
    ASSERT(pBlockCol->flag && pBlockCol->flag != HAS_NONE);
H
Hongze Cheng 已提交
920

H
Hongze Cheng 已提交
921 922
    code = tBlockDataAddColData(pBlockData, iBlockCol, &pColData);
    if (code) goto _err;
H
Hongze Cheng 已提交
923

H
Hongze Cheng 已提交
924 925 926 927 928
    tColDataReset(pColData, pBlockCol->cid, pBlockCol->type);
    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 已提交
929
      }
H
Hongze Cheng 已提交
930
    } else {
H
Hongze Cheng 已提交
931 932
      code = tsdbRecoverColData(pBlockData, pSubBlock, pBlockCol, pColData, p, ppBuf2);
      if (code) goto _err;
H
Hongze Cheng 已提交
933

H
Hongze Cheng 已提交
934
      p = p + pBlockCol->bsize + pBlockCol->csize + sizeof(TSCKSUM);
H
Hongze Cheng 已提交
935
    }
H
Hongze Cheng 已提交
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
  }

  return code;

_err:
  tsdbError("vgId:%d tsdb read sub block data failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
  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 已提交
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
  if (pBlock->nSubBlock > 1) {
    SBlockData *pBlockData1 = &(SBlockData){0};
    SBlockData *pBlockData2 = &(SBlockData){0};

    for (iSubBlock = 1; iSubBlock < pBlock->nSubBlock; iSubBlock++) {
      code = tsdbReadSubBlockData(pReader, pBlockIdx, pBlock, iSubBlock, pBlockData, ppBuf1, ppBuf2);
      if (code) {
        tBlockDataClear(pBlockData1);
        tBlockDataClear(pBlockData2);
        goto _err;
      }

      code = tBlockDataCopy(pBlockData, pBlockData2);
      if (code) {
        tBlockDataClear(pBlockData1);
        tBlockDataClear(pBlockData2);
        goto _err;
      }

      // merge two block data
      code = tBlockDataMerge(pBlockData1, pBlockData2, pBlockData);
      if (code) {
        tBlockDataClear(pBlockData1);
        tBlockDataClear(pBlockData2);
        goto _err;
      }
    }

    tBlockDataClear(pBlockData1);
    tBlockDataClear(pBlockData2);
H
Hongze Cheng 已提交
992 993
  }

H
Hongze Cheng 已提交
994 995 996 997
  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 已提交
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
  if (pBuf1) tsdbFree(pBuf1);
  if (pBuf2) tsdbFree(pBuf2);
  return code;

_err:
  tsdbError("vgId:%d tsdb read block data failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
  if (pBuf1) tsdbFree(pBuf1);
  if (pBuf2) tsdbFree(pBuf2);
  return code;
}

H
Hongze Cheng 已提交
1009 1010 1011 1012 1013 1014 1015 1016
int32_t tsdbReadBlockSMA(SDataFReader *pReader, SBlockSMA *pBlkSMA) {
  int32_t code = 0;
  // TODO
  return code;
}

// SDataFWriter ====================================================
struct SDataFWriter {
H
Hongze Cheng 已提交
1017 1018 1019 1020 1021 1022
  STsdb    *pTsdb;
  SDFileSet wSet;
  TdFilePtr pHeadFD;
  TdFilePtr pDataFD;
  TdFilePtr pLastFD;
  TdFilePtr pSmaFD;
H
Hongze Cheng 已提交
1023 1024
};

H
Hongze Cheng 已提交
1025 1026
SDFileSet *tsdbDataFWriterGetWSet(SDataFWriter *pWriter) { return &pWriter->wSet; }

H
Hongze Cheng 已提交
1027
int32_t tsdbDataFWriterOpen(SDataFWriter **ppWriter, STsdb *pTsdb, SDFileSet *pSet) {
H
Hongze Cheng 已提交
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
  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 已提交
1042 1043
  pWriter->wSet = *pSet;
  pSet = &pWriter->wSet;
H
Hongze Cheng 已提交
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159

  // head
  flag = TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
  tsdbDataFileName(pTsdb, pSet, TSDB_HEAD_FILE, fname);
  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);

  pSet->fHead.size += TSDB_FHDR_SIZE;

  // data
  if (pSet->fData.size == 0) {
    flag = TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
  } else {
    flag = TD_FILE_WRITE;
  }
  tsdbDataFileName(pTsdb, pSet, TSDB_DATA_FILE, fname);
  pWriter->pDataFD = taosOpenFile(fname, flag);
  if (pWriter->pDataFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
  if (pSet->fData.size == 0) {
    n = taosWriteFile(pWriter->pDataFD, hdr, TSDB_FHDR_SIZE);
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

    pSet->fData.size += TSDB_FHDR_SIZE;
  } else {
    n = taosLSeekFile(pWriter->pDataFD, 0, SEEK_END);
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

    ASSERT(n == pSet->fData.size);
  }

  // last
  if (pSet->fLast.size == 0) {
    flag = TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
  } else {
    flag = TD_FILE_WRITE;
  }
  tsdbDataFileName(pTsdb, pSet, TSDB_LAST_FILE, fname);
  pWriter->pLastFD = taosOpenFile(fname, flag);
  if (pWriter->pLastFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
  if (pSet->fLast.size == 0) {
    n = taosWriteFile(pWriter->pLastFD, hdr, TSDB_FHDR_SIZE);
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

    pSet->fLast.size += TSDB_FHDR_SIZE;
  } else {
    n = taosLSeekFile(pWriter->pLastFD, 0, SEEK_END);
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

    ASSERT(n == pSet->fLast.size);
  }

  // sma
  if (pSet->fSma.size == 0) {
    flag = TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
  } else {
    flag = TD_FILE_WRITE;
  }
  tsdbDataFileName(pTsdb, pSet, TSDB_SMA_FILE, fname);
  pWriter->pSmaFD = taosOpenFile(fname, flag);
  if (pWriter->pSmaFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
  if (pSet->fSma.size == 0) {
    n = taosWriteFile(pWriter->pSmaFD, hdr, TSDB_FHDR_SIZE);
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

    pSet->fSma.size += TSDB_FHDR_SIZE;
  } else {
    n = taosLSeekFile(pWriter->pSmaFD, 0, SEEK_END);
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

    ASSERT(n == pSet->fSma.size);
  }

  *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 已提交
1160 1161 1162
  return code;
}

H
Hongze Cheng 已提交
1163
int32_t tsdbDataFWriterClose(SDataFWriter **ppWriter, int8_t sync) {
H
Hongze Cheng 已提交
1164
  int32_t code = 0;
H
Hongze Cheng 已提交
1165
  STsdb  *pTsdb = (*ppWriter)->pTsdb;
H
Hongze Cheng 已提交
1166 1167

  if (sync) {
H
Hongze Cheng 已提交
1168
    if (taosFsyncFile((*ppWriter)->pHeadFD) < 0) {
H
Hongze Cheng 已提交
1169 1170 1171 1172
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

H
Hongze Cheng 已提交
1173
    if (taosFsyncFile((*ppWriter)->pDataFD) < 0) {
H
Hongze Cheng 已提交
1174 1175 1176 1177
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

H
Hongze Cheng 已提交
1178
    if (taosFsyncFile((*ppWriter)->pLastFD) < 0) {
H
Hongze Cheng 已提交
1179 1180 1181 1182
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

H
Hongze Cheng 已提交
1183
    if (taosFsyncFile((*ppWriter)->pSmaFD) < 0) {
H
Hongze Cheng 已提交
1184 1185 1186 1187 1188
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }
  }

H
Hongze Cheng 已提交
1189
  if (taosCloseFile(&(*ppWriter)->pHeadFD) < 0) {
H
Hongze Cheng 已提交
1190 1191 1192 1193
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
1194
  if (taosCloseFile(&(*ppWriter)->pDataFD) < 0) {
H
Hongze Cheng 已提交
1195 1196 1197 1198
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
1199
  if (taosCloseFile(&(*ppWriter)->pLastFD) < 0) {
H
Hongze Cheng 已提交
1200 1201 1202 1203
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
1204
  if (taosCloseFile(&(*ppWriter)->pSmaFD) < 0) {
H
Hongze Cheng 已提交
1205 1206 1207 1208
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
1209 1210
  taosMemoryFree(*ppWriter);
  *ppWriter = NULL;
H
Hongze Cheng 已提交
1211 1212 1213
  return code;

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

H
Hongze Cheng 已提交
1218
int32_t tsdbUpdateDFileSetHeader(SDataFWriter *pWriter) {
H
Hongze Cheng 已提交
1219 1220 1221
  int32_t    code = 0;
  int64_t    size = TSDB_FHDR_SIZE;
  int64_t    n;
H
Hongze Cheng 已提交
1222
  uint8_t    hdr[TSDB_FHDR_SIZE];
H
Hongze Cheng 已提交
1223 1224 1225 1226
  SHeadFile *pHeadFile = &pWriter->wSet.fHead;
  SDataFile *pDataFile = &pWriter->wSet.fData;
  SLastFile *pLastFile = &pWriter->wSet.fLast;
  SSmaFile  *pSmaFile = &pWriter->wSet.fSma;
H
Hongze Cheng 已提交
1227 1228

  // head ==============
H
Hongze Cheng 已提交
1229 1230
  code = tsdbUpdateDFileHdr(pWriter->pHeadFD, &pWriter->wSet, TSDB_HEAD_FILE);
  if (code) goto _err;
H
Hongze Cheng 已提交
1231 1232

  // data ==============
H
Hongze Cheng 已提交
1233 1234
  code = tsdbUpdateDFileHdr(pWriter->pHeadFD, &pWriter->wSet, TSDB_DATA_FILE);
  if (code) goto _err;
H
Hongze Cheng 已提交
1235 1236

  // last ==============
H
Hongze Cheng 已提交
1237 1238
  code = tsdbUpdateDFileHdr(pWriter->pHeadFD, &pWriter->wSet, TSDB_LAST_FILE);
  if (code) goto _err;
H
Hongze Cheng 已提交
1239 1240

  // sma ==============
H
Hongze Cheng 已提交
1241 1242
  code = tsdbUpdateDFileHdr(pWriter->pHeadFD, &pWriter->wSet, TSDB_SMA_FILE);
  if (code) goto _err;
H
Hongze Cheng 已提交
1243 1244 1245 1246 1247

  return code;

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

H
Hongze Cheng 已提交
1251 1252
int32_t tsdbWriteBlockIdx(SDataFWriter *pWriter, SMapData *mBlockIdx, uint8_t **ppBuf) {
  int32_t    code = 0;
H
Hongze Cheng 已提交
1253
  int64_t    size;
H
Hongze Cheng 已提交
1254
  SHeadFile *pHeadFile = &pWriter->wSet.fHead;
H
Hongze Cheng 已提交
1255
  int64_t    n;
H
Hongze Cheng 已提交
1256
  uint8_t   *pBuf = NULL;
H
Hongze Cheng 已提交
1257 1258

  // prepare
H
Hongze Cheng 已提交
1259
  size = 0;
H
Hongze Cheng 已提交
1260
  size += tPutU32(NULL, TSDB_FILE_DLMT);
H
Hongze Cheng 已提交
1261
  size = size + tPutMapData(NULL, mBlockIdx) + sizeof(TSCKSUM);
H
Hongze Cheng 已提交
1262 1263 1264 1265 1266 1267 1268

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

  // build
H
Hongze Cheng 已提交
1269
  n = 0;
H
Hongze Cheng 已提交
1270
  n += tPutU32(*ppBuf + n, TSDB_FILE_DLMT);
H
Hongze Cheng 已提交
1271
  n += tPutMapData(*ppBuf + n, mBlockIdx);
H
Hongze Cheng 已提交
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
  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 已提交
1283 1284 1285
  // update
  pHeadFile->offset = pHeadFile->size;
  pHeadFile->size += size;
H
Hongze Cheng 已提交
1286 1287 1288 1289 1290 1291 1292

  tsdbFree(pBuf);
  return code;

_err:
  tsdbError("vgId:%d write block idx failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
  tsdbFree(pBuf);
H
Hongze Cheng 已提交
1293 1294 1295
  return code;
}

H
Hongze Cheng 已提交
1296
int32_t tsdbWriteBlock(SDataFWriter *pWriter, SMapData *mBlock, uint8_t **ppBuf, SBlockIdx *pBlockIdx) {
H
Hongze Cheng 已提交
1297 1298 1299 1300 1301 1302
  int32_t       code = 0;
  SHeadFile    *pHeadFile = &pWriter->wSet.fHead;
  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 已提交
1303

H
Hongze Cheng 已提交
1304
  ASSERT(mBlock->nItem > 0);
H
Hongze Cheng 已提交
1305 1306

  // prepare
H
Hongze Cheng 已提交
1307
  size = sizeof(SBlockDataHdr) + tPutMapData(NULL, mBlock) + sizeof(TSCKSUM);
H
Hongze Cheng 已提交
1308 1309 1310 1311 1312 1313 1314 1315

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

  // build
  n = 0;
H
Hongze Cheng 已提交
1316 1317
  *(SBlockDataHdr *)(*ppBuf) = hdr;
  n += sizeof(hdr);
H
Hongze Cheng 已提交
1318
  n += tPutMapData(*ppBuf + n, mBlock);
H
Hongze Cheng 已提交
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
  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 已提交
1330 1331
  // update
  pBlockIdx->offset = pHeadFile->size;
H
Hongze Cheng 已提交
1332
  pBlockIdx->size = size;
H
Hongze Cheng 已提交
1333
  pHeadFile->size += size;
H
Hongze Cheng 已提交
1334

H
Hongze Cheng 已提交
1335
  tsdbFree(pBuf);
H
Hongze Cheng 已提交
1336 1337 1338 1339 1340
  tsdbTrace("vgId:%d write block, offset:%" PRId64 " size:%" PRId64, TD_VID(pWriter->pTsdb->pVnode), pBlockIdx->offset,
            pBlockIdx->size);
  return code;

_err:
H
Hongze Cheng 已提交
1341
  tsdbFree(pBuf);
H
Hongze Cheng 已提交
1342
  tsdbError("vgId:%d write block failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1343 1344 1345
  return code;
}

H
Hongze Cheng 已提交
1346
int32_t tsdbWriteBlockData(SDataFWriter *pWriter, SBlockData *pBlockData, uint8_t **ppBuf1, uint8_t **ppBuf2,
H
Hongze Cheng 已提交
1347
                           SBlockIdx *pBlockIdx, SBlock *pBlock, int8_t cmprAlg) {
H
Hongze Cheng 已提交
1348 1349
  int32_t       code = 0;
  SSubBlock    *pSubBlock = &pBlock->aSubBlock[pBlock->nSubBlock++];
H
Hongze Cheng 已提交
1350
  SBlockCol    *pBlockCol = &(SBlockCol){0};
H
Hongze Cheng 已提交
1351 1352
  int64_t       size;
  int64_t       n;
H
Hongze Cheng 已提交
1353
  TdFilePtr     pFileFD = pBlock->last ? pWriter->pLastFD : pWriter->pDataFD;
H
Hongze Cheng 已提交
1354 1355 1356 1357
  SBlockDataHdr hdr = {.delimiter = TSDB_FILE_DLMT, .suid = pBlockIdx->suid, .uid = pBlockIdx->uid};
  TSCKSUM       cksm;
  uint8_t      *p;
  int64_t       offset;
H
Hongze Cheng 已提交
1358 1359
  uint8_t      *pBuf1 = NULL;
  uint8_t      *pBuf2 = NULL;
H
Hongze Cheng 已提交
1360

H
Hongze Cheng 已提交
1361 1362
  if (!ppBuf1) ppBuf1 = &pBuf1;
  if (!ppBuf2) ppBuf2 = &pBuf2;
H
Hongze Cheng 已提交
1363

H
Hongze Cheng 已提交
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
  TSKEY lastKey = TSKEY_MIN;
  for (int32_t iRow = 0; iRow < pBlockData->nRow; iRow++) {
    TSDBKEY key = TSDBROW_KEY(&tsdbRowFromBlockData(pBlockData, iRow));
    if (iRow == 0) {
      pBlock->minKey = MIN_TSDBKEY(pBlock->minKey, key);
    }

    if (iRow == pBlockData->nRow - 1) {
      pBlock->maxKey = MAX_TSDBKEY(pBlock->maxKey, key);
    }

    pBlock->minVersion = TMIN(pBlock->minVersion, key.version);
    pBlock->maxVersion = TMAX(pBlock->maxVersion, key.version);
    if (key.ts == lastKey) {
      pBlock->hasDup = 1;
    }
    lastKey = key.ts;
  }
  pBlock->nRow += pBlockData->nRow;
H
Hongze Cheng 已提交
1383

H
Hongze Cheng 已提交
1384 1385
  pSubBlock->nRow = pBlockData->nRow;
  pSubBlock->cmprAlg = cmprAlg;
H
Hongze Cheng 已提交
1386 1387 1388 1389 1390
  if (pBlock->last) {
    pSubBlock->offset = pWriter->wSet.fLast.size;
  } else {
    pSubBlock->offset = pWriter->wSet.fData.size;
  }
H
Hongze Cheng 已提交
1391
  pSubBlock->bsize = 0;
H
Hongze Cheng 已提交
1392 1393 1394 1395 1396 1397 1398 1399

  // HDR
  n = taosWriteFile(pFileFD, &hdr, sizeof(hdr));
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
  pSubBlock->bsize += n;
H
Hongze Cheng 已提交
1400 1401

  // TSDBKEY
H
Hongze Cheng 已提交
1402
  if (cmprAlg == NO_COMPRESSION) {
H
Hongze Cheng 已提交
1403 1404 1405 1406 1407
    cksm = 0;

    // version
    pSubBlock->vsize = sizeof(int64_t) * pBlockData->nRow;
    n = taosWriteFile(pFileFD, pBlockData->aVersion, pSubBlock->vsize);
H
Hongze Cheng 已提交
1408 1409 1410 1411
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }
H
Hongze Cheng 已提交
1412
    cksm = taosCalcChecksum(cksm, (uint8_t *)pBlockData->aVersion, pSubBlock->vsize);
H
Hongze Cheng 已提交
1413

H
Hongze Cheng 已提交
1414 1415 1416
    // TSKEY
    pSubBlock->ksize = sizeof(TSKEY) * pBlockData->nRow;
    n = taosWriteFile(pFileFD, pBlockData->aTSKEY, pSubBlock->ksize);
H
Hongze Cheng 已提交
1417 1418 1419 1420
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }
H
Hongze Cheng 已提交
1421
    cksm = taosCalcChecksum(cksm, (uint8_t *)pBlockData->aTSKEY, pSubBlock->ksize);
H
Hongze Cheng 已提交
1422 1423 1424 1425 1426 1427 1428 1429 1430

    // cksm
    size = sizeof(cksm);
    n = taosWriteFile(pFileFD, (uint8_t *)&cksm, size);
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }
  } else {
H
Hongze Cheng 已提交
1431
    ASSERT(cmprAlg == ONE_STAGE_COMP || cmprAlg == TWO_STAGE_COMP);
H
Hongze Cheng 已提交
1432

H
Hongze Cheng 已提交
1433
    size = (sizeof(int64_t) + sizeof(TSKEY)) * pBlockData->nRow + COMP_OVERFLOW_BYTES * 2 + sizeof(TSCKSUM);
H
Hongze Cheng 已提交
1434 1435 1436 1437

    code = tsdbRealloc(ppBuf1, size);
    if (code) goto _err;

H
Hongze Cheng 已提交
1438
    if (cmprAlg == TWO_STAGE_COMP) {
H
Hongze Cheng 已提交
1439 1440 1441 1442
      code = tsdbRealloc(ppBuf2, size);
      if (code) goto _err;
    }

H
Hongze Cheng 已提交
1443 1444 1445
    // version
    n = tsCompressBigint((char *)pBlockData->aVersion, sizeof(int64_t) * pBlockData->nRow, pBlockData->nRow, *ppBuf1,
                         size, cmprAlg, *ppBuf2, size);
H
Hongze Cheng 已提交
1446 1447 1448 1449
    if (n <= 0) {
      code = TSDB_CODE_COMPRESS_ERROR;
      goto _err;
    }
H
Hongze Cheng 已提交
1450
    pSubBlock->vsize = n;
H
Hongze Cheng 已提交
1451

H
Hongze Cheng 已提交
1452 1453 1454
    // TSKEY
    n = tsCompressTimestamp((char *)pBlockData->aTSKEY, sizeof(TSKEY) * pBlockData->nRow, pBlockData->nRow,
                            *ppBuf1 + pSubBlock->vsize, size - pSubBlock->vsize, cmprAlg, *ppBuf2, size);
H
Hongze Cheng 已提交
1455 1456 1457 1458
    if (n <= 0) {
      code = TSDB_CODE_COMPRESS_ERROR;
      goto _err;
    }
H
Hongze Cheng 已提交
1459
    pSubBlock->ksize = n;
H
Hongze Cheng 已提交
1460 1461

    // cksm
H
Hongze Cheng 已提交
1462 1463 1464
    n = pSubBlock->vsize + pSubBlock->ksize + sizeof(TSCKSUM);
    ASSERT(n <= size);
    taosCalcChecksumAppend(0, *ppBuf1, n);
H
Hongze Cheng 已提交
1465 1466

    // write
H
Hongze Cheng 已提交
1467
    n = taosWriteFile(pFileFD, *ppBuf1, n);
H
Hongze Cheng 已提交
1468 1469 1470 1471 1472
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }
  }
H
Hongze Cheng 已提交
1473
  pSubBlock->bsize += (pSubBlock->vsize + pSubBlock->ksize + sizeof(TSCKSUM));
H
Hongze Cheng 已提交
1474 1475

  // other columns
H
Hongze Cheng 已提交
1476
  offset = 0;
H
Hongze Cheng 已提交
1477
  tMapDataReset(&pSubBlock->mBlockCol);
H
Hongze Cheng 已提交
1478 1479
  for (int32_t iCol = 0; iCol < taosArrayGetSize(pBlockData->aColDataP); iCol++) {
    SColData *pColData = (SColData *)taosArrayGetP(pBlockData->aColDataP, iCol);
H
Hongze Cheng 已提交
1480

H
Hongze Cheng 已提交
1481
    ASSERT(pColData->flag);
H
Hongze Cheng 已提交
1482

H
Hongze Cheng 已提交
1483
    if (pColData->flag == HAS_NONE) continue;
H
Hongze Cheng 已提交
1484

H
Hongze Cheng 已提交
1485 1486 1487
    pBlockCol->cid = pColData->cid;
    pBlockCol->type = pColData->type;
    pBlockCol->flag = pColData->flag;
H
Hongze Cheng 已提交
1488

H
Hongze Cheng 已提交
1489
    if (pColData->flag != HAS_NULL) {
H
Hongze Cheng 已提交
1490
      cksm = 0;
H
Hongze Cheng 已提交
1491
      pBlockCol->offset = offset;
H
Hongze Cheng 已提交
1492 1493

      // bitmap
H
Hongze Cheng 已提交
1494 1495 1496 1497 1498
      if (pColData->flag == HAS_VALUE) {
        pBlockCol->bsize = 0;
      } else {
        pBlockCol->bsize = BIT2_SIZE(pBlockData->nRow);
        n = taosWriteFile(pFileFD, pColData->pBitMap, pBlockCol->bsize);
H
Hongze Cheng 已提交
1499 1500 1501 1502 1503
        if (n < 0) {
          code = TAOS_SYSTEM_ERROR(errno);
          goto _err;
        }
        cksm = taosCalcChecksum(cksm, pColData->pBitMap, n);
H
Hongze Cheng 已提交
1504 1505
      }

H
Hongze Cheng 已提交
1506
      // data
H
Hongze Cheng 已提交
1507
      if (cmprAlg == NO_COMPRESSION) {
H
Hongze Cheng 已提交
1508 1509 1510 1511 1512 1513
        // data
        n = taosWriteFile(pFileFD, pColData->pData, pColData->nData);
        if (n < 0) {
          code = TAOS_SYSTEM_ERROR(errno);
          goto _err;
        }
H
Hongze Cheng 已提交
1514 1515
        pBlockCol->csize = n;
        pBlockCol->osize = n;
H
Hongze Cheng 已提交
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529

        // checksum
        cksm = taosCalcChecksum(cksm, pColData->pData, pColData->nData);
        n = taosWriteFile(pFileFD, &cksm, sizeof(cksm));
        if (n < 0) {
          code = TAOS_SYSTEM_ERROR(errno);
          goto _err;
        }
      } else {
        size = pColData->nData + COMP_OVERFLOW_BYTES + sizeof(TSCKSUM);

        code = tsdbRealloc(ppBuf1, size);
        if (code) goto _err;

H
Hongze Cheng 已提交
1530
        if (cmprAlg == TWO_STAGE_COMP) {
H
Hongze Cheng 已提交
1531 1532 1533 1534 1535
          code = tsdbRealloc(ppBuf2, size);
          if (code) goto _err;
        }

        // data
H
Hongze Cheng 已提交
1536
        n = tDataTypes[pColData->type].compFunc(pColData->pData, pColData->nData, pBlockData->nRow, *ppBuf1, size,
H
Hongze Cheng 已提交
1537
                                                cmprAlg, *ppBuf2, size);
H
Hongze Cheng 已提交
1538 1539 1540 1541
        if (n <= 0) {
          code = TSDB_CODE_COMPRESS_ERROR;
          goto _err;
        }
H
Hongze Cheng 已提交
1542 1543
        pBlockCol->csize = n;
        pBlockCol->osize = pColData->nData;
H
Hongze Cheng 已提交
1544 1545 1546 1547 1548 1549 1550

        // cksm
        n += sizeof(TSCKSUM);
        ASSERT(n <= size);
        taosCalcChecksumAppend(cksm, *ppBuf1, n);

        // write
H
Hongze Cheng 已提交
1551
        n = taosWriteFile(pFileFD, *ppBuf1, n);
H
Hongze Cheng 已提交
1552 1553 1554 1555 1556
        if (n < 0) {
          code = TAOS_SYSTEM_ERROR(errno);
          goto _err;
        }
      }
H
Hongze Cheng 已提交
1557

H
Hongze Cheng 已提交
1558
      // state
H
Hongze Cheng 已提交
1559 1560
      offset = offset + pBlockCol->bsize + pBlockCol->csize + sizeof(TSCKSUM);
      pSubBlock->bsize = pSubBlock->bsize + pBlockCol->bsize + pBlockCol->csize + sizeof(TSCKSUM);
H
Hongze Cheng 已提交
1561 1562
    }

H
Hongze Cheng 已提交
1563
    code = tMapDataPutItem(&pSubBlock->mBlockCol, pBlockCol, tPutBlockCol);
H
Hongze Cheng 已提交
1564 1565 1566
    if (code) goto _err;
  }

H
Hongze Cheng 已提交
1567 1568 1569 1570 1571 1572 1573 1574
  if (pBlock->last) {
    pWriter->wSet.fLast.size += pSubBlock->bsize;
  } else {
    pWriter->wSet.fData.size += pSubBlock->bsize;
  }

  tsdbFree(pBuf1);
  tsdbFree(pBuf2);
H
Hongze Cheng 已提交
1575 1576 1577
  return code;

_err:
H
Hongze Cheng 已提交
1578
  tsdbError("vgId:%d write block data failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1579 1580
  tsdbFree(pBuf1);
  tsdbFree(pBuf2);
H
Hongze Cheng 已提交
1581 1582 1583 1584 1585 1586 1587 1588
  return code;
}

int32_t tsdbWriteBlockSMA(SDataFWriter *pWriter, SBlockSMA *pBlockSMA, int64_t *rOffset, int64_t *rSize) {
  int32_t code = 0;
  // TODO
  return code;
}