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

H
Hongze Cheng 已提交
18
// =============== PAGE-WISE FILE ===============
H
Hongze Cheng 已提交
19
static int32_t tsdbOpenFile(const char *path, int32_t szPage, int32_t flag, STsdbFD **ppFD) {
H
Hongze Cheng 已提交
20
  int32_t  code = 0;
H
Hongze Cheng 已提交
21
  STsdbFD *pFD = NULL;
H
refact  
Hongze Cheng 已提交
22

H
Hongze Cheng 已提交
23 24
  *ppFD = NULL;

H
Hongze Cheng 已提交
25
  pFD = (STsdbFD *)taosMemoryCalloc(1, sizeof(*pFD) + strlen(path) + 1);
H
Hongze Cheng 已提交
26 27 28 29
  if (pFD == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }
H
Hongze Cheng 已提交
30

H
Hongze Cheng 已提交
31 32 33 34 35
  pFD->path = (char *)&pFD[1];
  strcpy(pFD->path, path);
  pFD->szPage = szPage;
  pFD->flag = flag;
  pFD->pFD = taosOpenFile(path, flag);
H
Hongze Cheng 已提交
36
  if (pFD->pFD == NULL) {
H
Hongze Cheng 已提交
37
    code = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
38
    taosMemoryFree(pFD);
H
Hongze Cheng 已提交
39
    goto _exit;
H
Hongze Cheng 已提交
40
  }
H
Hongze Cheng 已提交
41
  pFD->szPage = szPage;
H
Hongze Cheng 已提交
42
  pFD->pgno = 0;
H
Hongze Cheng 已提交
43
  pFD->pBuf = taosMemoryCalloc(1, szPage);
H
Hongze Cheng 已提交
44
  if (pFD->pBuf == NULL) {
H
Hongze Cheng 已提交
45
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
46
    taosCloseFile(&pFD->pFD);
H
Hongze Cheng 已提交
47
    taosMemoryFree(pFD);
H
Hongze Cheng 已提交
48
    goto _exit;
H
Hongze Cheng 已提交
49
  }
H
Hongze Cheng 已提交
50 51
  if (taosStatFile(path, &pFD->szFile, NULL) < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
52 53 54
    taosMemoryFree(pFD->pBuf);
    taosCloseFile(&pFD->pFD);
    taosMemoryFree(pFD);
H
Hongze Cheng 已提交
55 56 57 58
    goto _exit;
  }
  ASSERT(pFD->szFile % szPage == 0);
  pFD->szFile = pFD->szFile / szPage;
H
Hongze Cheng 已提交
59
  *ppFD = pFD;
H
Hongze Cheng 已提交
60 61 62

_exit:
  return code;
H
Hongze Cheng 已提交
63
}
H
Hongze Cheng 已提交
64

H
Hongze Cheng 已提交
65 66
static void tsdbCloseFile(STsdbFD **ppFD) {
  STsdbFD *pFD = *ppFD;
H
Hongze Cheng 已提交
67 68 69 70 71 72
  if (pFD) {
    taosMemoryFree(pFD->pBuf);
    taosCloseFile(&pFD->pFD);
    taosMemoryFree(pFD);
    *ppFD = NULL;
  }
H
Hongze Cheng 已提交
73 74
}

H
Hongze Cheng 已提交
75
static int32_t tsdbWriteFilePage(STsdbFD *pFD) {
H
Hongze Cheng 已提交
76
  int32_t code = 0;
H
Hongze Cheng 已提交
77

H
Hongze Cheng 已提交
78 79 80 81 82 83
  if (pFD->pgno > 0) {
    int64_t n = taosLSeekFile(pFD->pFD, PAGE_OFFSET(pFD->pgno, pFD->szPage), SEEK_SET);
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _exit;
    }
H
Hongze Cheng 已提交
84

H
Hongze Cheng 已提交
85
    taosCalcChecksumAppend(0, pFD->pBuf, pFD->szPage);
H
Hongze Cheng 已提交
86

H
Hongze Cheng 已提交
87 88 89 90 91
    n = taosWriteFile(pFD->pFD, pFD->pBuf, pFD->szPage);
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _exit;
    }
H
Hongze Cheng 已提交
92

H
Hongze Cheng 已提交
93
    if (pFD->szFile < pFD->pgno) {
H
Hongze Cheng 已提交
94
      pFD->szFile = pFD->pgno;
H
Hongze Cheng 已提交
95
    }
H
Hongze Cheng 已提交
96
  }
H
Hongze Cheng 已提交
97
  pFD->pgno = 0;
H
Hongze Cheng 已提交
98 99

_exit:
H
Hongze Cheng 已提交
100 101 102
  return code;
}

H
Hongze Cheng 已提交
103
static int32_t tsdbReadFilePage(STsdbFD *pFD, int64_t pgno) {
H
Hongze Cheng 已提交
104
  int32_t code = 0;
H
more  
Hongze Cheng 已提交
105

H
Hongze Cheng 已提交
106 107
  ASSERT(pgno <= pFD->szFile);

H
Hongze Cheng 已提交
108 109 110
  // seek
  int64_t offset = PAGE_OFFSET(pgno, pFD->szPage);
  int64_t n = taosLSeekFile(pFD->pFD, offset, SEEK_SET);
H
Hongze Cheng 已提交
111
  if (n < 0) {
H
Hongze Cheng 已提交
112
    code = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
113
    goto _exit;
H
Hongze Cheng 已提交
114 115
  }

H
Hongze Cheng 已提交
116
  // read
H
Hongze Cheng 已提交
117
  n = taosReadFile(pFD->pFD, pFD->pBuf, pFD->szPage);
H
Hongze Cheng 已提交
118
  if (n < 0) {
H
Hongze Cheng 已提交
119
    code = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
120 121
    goto _exit;
  } else if (n < pFD->szPage) {
H
Hongze Cheng 已提交
122
    code = TSDB_CODE_FILE_CORRUPTED;
H
Hongze Cheng 已提交
123
    goto _exit;
H
Hongze Cheng 已提交
124 125
  }

H
Hongze Cheng 已提交
126
  // check
H
Hongze Cheng 已提交
127
  if (!taosCheckChecksumWhole(pFD->pBuf, pFD->szPage)) {
H
Hongze Cheng 已提交
128
    code = TSDB_CODE_FILE_CORRUPTED;
H
Hongze Cheng 已提交
129
    goto _exit;
H
Hongze Cheng 已提交
130 131
  }

H
Hongze Cheng 已提交
132
  pFD->pgno = pgno;
H
Hongze Cheng 已提交
133

H
Hongze Cheng 已提交
134 135 136
_exit:
  return code;
}
H
Hongze Cheng 已提交
137

H
Hongze Cheng 已提交
138
static int32_t tsdbWriteFile(STsdbFD *pFD, int64_t offset, const uint8_t *pBuf, int64_t size) {
H
Hongze Cheng 已提交
139 140 141 142 143 144 145 146 147 148 149
  int32_t code = 0;
  int64_t fOffset = LOGIC_TO_FILE_OFFSET(offset, pFD->szPage);
  int64_t pgno = OFFSET_PGNO(fOffset, pFD->szPage);
  int64_t bOffset = fOffset % pFD->szPage;
  int64_t n = 0;

  do {
    if (pFD->pgno != pgno) {
      code = tsdbWriteFilePage(pFD);
      if (code) goto _exit;

H
Hongze Cheng 已提交
150
      if (pgno <= pFD->szFile) {
H
Hongze Cheng 已提交
151 152 153 154 155 156 157
        code = tsdbReadFilePage(pFD, pgno);
        if (code) goto _exit;
      } else {
        pFD->pgno = pgno;
      }
    }

H
Hongze Cheng 已提交
158 159
    int64_t nWrite = TMIN(PAGE_CONTENT_SIZE(pFD->szPage) - bOffset, size - n);
    memcpy(pFD->pBuf + bOffset, pBuf + n, nWrite);
H
Hongze Cheng 已提交
160 161 162

    pgno++;
    bOffset = 0;
H
Hongze Cheng 已提交
163
    n += nWrite;
H
Hongze Cheng 已提交
164 165 166 167 168 169
  } while (n < size);

_exit:
  return code;
}

H
Hongze Cheng 已提交
170
static int32_t tsdbReadFile(STsdbFD *pFD, int64_t offset, uint8_t *pBuf, int64_t size) {
H
Hongze Cheng 已提交
171
  int32_t code = 0;
H
Hongze Cheng 已提交
172
  int64_t n = 0;
H
Hongze Cheng 已提交
173 174
  int64_t fOffset = LOGIC_TO_FILE_OFFSET(offset, pFD->szPage);
  int64_t pgno = OFFSET_PGNO(fOffset, pFD->szPage);
H
Hongze Cheng 已提交
175
  int32_t szPgCont = PAGE_CONTENT_SIZE(pFD->szPage);
H
fix bug  
Hongze Cheng 已提交
176
  int64_t bOffset = fOffset % pFD->szPage;
H
Hongze Cheng 已提交
177

H
Hongze Cheng 已提交
178
  ASSERT(pgno && pgno <= pFD->szFile);
H
fix bug  
Hongze Cheng 已提交
179
  ASSERT(bOffset < szPgCont);
H
Hongze Cheng 已提交
180

H
Hongze Cheng 已提交
181
  while (n < size) {
H
fix bug  
Hongze Cheng 已提交
182 183 184 185
    if (pFD->pgno != pgno) {
      code = tsdbReadFilePage(pFD, pgno);
      if (code) goto _exit;
    }
H
Hongze Cheng 已提交
186

H
fix bug  
Hongze Cheng 已提交
187 188
    int64_t nRead = TMIN(szPgCont - bOffset, size - n);
    memcpy(pBuf + n, pFD->pBuf + bOffset, nRead);
H
Hongze Cheng 已提交
189

H
Hongze Cheng 已提交
190
    n += nRead;
H
Hongze Cheng 已提交
191
    pgno++;
H
fix bug  
Hongze Cheng 已提交
192
    bOffset = 0;
H
Hongze Cheng 已提交
193 194 195
  }

_exit:
H
Hongze Cheng 已提交
196
  return code;
H
Hongze Cheng 已提交
197 198
}

H
Hongze Cheng 已提交
199
static int32_t tsdbFsyncFile(STsdbFD *pFD) {
H
Hongze Cheng 已提交
200
  int32_t code = 0;
H
Hongze Cheng 已提交
201 202 203 204 205 206 207 208 209 210

  code = tsdbWriteFilePage(pFD);
  if (code) goto _exit;

  if (taosFsyncFile(pFD->pFD) < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _exit;
  }

_exit:
H
Hongze Cheng 已提交
211 212 213
  return code;
}

H
Hongze Cheng 已提交
214 215
// SDataFWriter ====================================================
int32_t tsdbDataFWriterOpen(SDataFWriter **ppWriter, STsdb *pTsdb, SDFileSet *pSet) {
H
Hongze Cheng 已提交
216
  int32_t       code = 0;
H
Hongze Cheng 已提交
217 218
  int32_t       flag;
  int64_t       n;
H
Hongze Cheng 已提交
219
  int32_t       szPage = pTsdb->pVnode->config.tsdbPageSize;
H
Hongze Cheng 已提交
220
  SDataFWriter *pWriter = NULL;
H
Hongze Cheng 已提交
221
  char          fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
222
  char          hdr[TSDB_FHDR_SIZE] = {0};
H
Hongze Cheng 已提交
223 224

  // alloc
H
Hongze Cheng 已提交
225 226
  pWriter = taosMemoryCalloc(1, sizeof(*pWriter));
  if (pWriter == NULL) {
H
Hongze Cheng 已提交
227 228 229
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
230
  pWriter->pTsdb = pTsdb;
H
Hongze Cheng 已提交
231 232 233 234 235
  pWriter->wSet = (SDFileSet){.diskId = pSet->diskId,
                              .fid = pSet->fid,
                              .pHeadF = &pWriter->fHead,
                              .pDataF = &pWriter->fData,
                              .pSmaF = &pWriter->fSma,
H
Hongze Cheng 已提交
236
                              .nSttF = pSet->nSttF};
H
Hongze Cheng 已提交
237 238 239
  pWriter->fHead = *pSet->pHeadF;
  pWriter->fData = *pSet->pDataF;
  pWriter->fSma = *pSet->pSmaF;
H
Hongze Cheng 已提交
240 241 242
  for (int8_t iStt = 0; iStt < pSet->nSttF; iStt++) {
    pWriter->wSet.aSttF[iStt] = &pWriter->fStt[iStt];
    pWriter->fStt[iStt] = *pSet->aSttF[iStt];
H
Hongze Cheng 已提交
243
  }
H
Hongze Cheng 已提交
244 245

  // head
H
Hongze Cheng 已提交
246
  flag = TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
H
Hongze Cheng 已提交
247
  tsdbHeadFileName(pTsdb, pWriter->wSet.diskId, pWriter->wSet.fid, &pWriter->fHead, fname);
H
Hongze Cheng 已提交
248 249
  code = tsdbOpenFile(fname, szPage, flag, &pWriter->pHeadFD);
  if (code) goto _err;
H
Hongze Cheng 已提交
250

H
Hongze Cheng 已提交
251
  code = tsdbWriteFile(pWriter->pHeadFD, 0, hdr, TSDB_FHDR_SIZE);
H
Hongze Cheng 已提交
252
  if (code) goto _err;
H
Hongze Cheng 已提交
253 254
  pWriter->fHead.size += TSDB_FHDR_SIZE;

H
Hongze Cheng 已提交
255
  // data
H
Hongze Cheng 已提交
256
  if (pWriter->fData.size == 0) {
H
Hongze Cheng 已提交
257
    flag = TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
H
Hongze Cheng 已提交
258
  } else {
H
Hongze Cheng 已提交
259
    flag = TD_FILE_READ | TD_FILE_WRITE;
H
Hongze Cheng 已提交
260 261
  }
  tsdbDataFileName(pTsdb, pWriter->wSet.diskId, pWriter->wSet.fid, &pWriter->fData, fname);
H
Hongze Cheng 已提交
262 263
  code = tsdbOpenFile(fname, szPage, flag, &pWriter->pDataFD);
  if (code) goto _err;
H
Hongze Cheng 已提交
264
  if (pWriter->fData.size == 0) {
H
Hongze Cheng 已提交
265
    code = tsdbWriteFile(pWriter->pDataFD, 0, hdr, TSDB_FHDR_SIZE);
H
Hongze Cheng 已提交
266
    if (code) goto _err;
H
Hongze Cheng 已提交
267 268
    pWriter->fData.size += TSDB_FHDR_SIZE;
  }
H
Hongze Cheng 已提交
269 270

  // sma
H
Hongze Cheng 已提交
271
  if (pWriter->fSma.size == 0) {
H
Hongze Cheng 已提交
272
    flag = TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
H
Hongze Cheng 已提交
273
  } else {
H
Hongze Cheng 已提交
274
    flag = TD_FILE_READ | TD_FILE_WRITE;
H
Hongze Cheng 已提交
275 276
  }
  tsdbSmaFileName(pTsdb, pWriter->wSet.diskId, pWriter->wSet.fid, &pWriter->fSma, fname);
H
Hongze Cheng 已提交
277 278
  code = tsdbOpenFile(fname, szPage, flag, &pWriter->pSmaFD);
  if (code) goto _err;
H
Hongze Cheng 已提交
279
  if (pWriter->fSma.size == 0) {
H
Hongze Cheng 已提交
280
    code = tsdbWriteFile(pWriter->pSmaFD, 0, hdr, TSDB_FHDR_SIZE);
H
Hongze Cheng 已提交
281
    if (code) goto _err;
H
Hongze Cheng 已提交
282

H
Hongze Cheng 已提交
283
    pWriter->fSma.size += TSDB_FHDR_SIZE;
H
Hongze Cheng 已提交
284 285
  }

H
Hongze Cheng 已提交
286 287
  // stt
  ASSERT(pWriter->fStt[pSet->nSttF - 1].size == 0);
H
Hongze Cheng 已提交
288
  flag = TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
H
Hongze Cheng 已提交
289 290
  tsdbSttFileName(pTsdb, pWriter->wSet.diskId, pWriter->wSet.fid, &pWriter->fStt[pSet->nSttF - 1], fname);
  code = tsdbOpenFile(fname, szPage, flag, &pWriter->pSttFD);
H
Hongze Cheng 已提交
291
  if (code) goto _err;
H
Hongze Cheng 已提交
292
  code = tsdbWriteFile(pWriter->pSttFD, 0, hdr, TSDB_FHDR_SIZE);
H
Hongze Cheng 已提交
293
  if (code) goto _err;
H
Hongze Cheng 已提交
294
  pWriter->fStt[pWriter->wSet.nSttF - 1].size += TSDB_FHDR_SIZE;
H
Hongze Cheng 已提交
295 296

  *ppWriter = pWriter;
H
Hongze Cheng 已提交
297 298 299
  return code;

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

H
Hongze Cheng 已提交
305
int32_t tsdbDataFWriterClose(SDataFWriter **ppWriter, int8_t sync) {
H
Hongze Cheng 已提交
306
  int32_t code = 0;
H
Hongze Cheng 已提交
307
  STsdb  *pTsdb = NULL;
H
Hongze Cheng 已提交
308

H
Hongze Cheng 已提交
309 310 311 312
  if (*ppWriter == NULL) goto _exit;

  pTsdb = (*ppWriter)->pTsdb;
  if (sync) {
H
Hongze Cheng 已提交
313 314
    code = tsdbFsyncFile((*ppWriter)->pHeadFD);
    if (code) goto _err;
H
Hongze Cheng 已提交
315

H
Hongze Cheng 已提交
316 317
    code = tsdbFsyncFile((*ppWriter)->pDataFD);
    if (code) goto _err;
H
Hongze Cheng 已提交
318

H
Hongze Cheng 已提交
319 320
    code = tsdbFsyncFile((*ppWriter)->pSmaFD);
    if (code) goto _err;
H
Hongze Cheng 已提交
321

H
Hongze Cheng 已提交
322
    code = tsdbFsyncFile((*ppWriter)->pSttFD);
H
Hongze Cheng 已提交
323
    if (code) goto _err;
H
Hongze Cheng 已提交
324 325
  }

H
Hongze Cheng 已提交
326 327 328
  tsdbCloseFile(&(*ppWriter)->pHeadFD);
  tsdbCloseFile(&(*ppWriter)->pDataFD);
  tsdbCloseFile(&(*ppWriter)->pSmaFD);
H
Hongze Cheng 已提交
329
  tsdbCloseFile(&(*ppWriter)->pSttFD);
H
Hongze Cheng 已提交
330

H
Hongze Cheng 已提交
331 332
  for (int32_t iBuf = 0; iBuf < sizeof((*ppWriter)->aBuf) / sizeof(uint8_t *); iBuf++) {
    tFree((*ppWriter)->aBuf[iBuf]);
H
Hongze Cheng 已提交
333
  }
H
Hongze Cheng 已提交
334
  taosMemoryFree(*ppWriter);
H
Hongze Cheng 已提交
335
_exit:
H
Hongze Cheng 已提交
336
  *ppWriter = NULL;
H
Hongze Cheng 已提交
337 338 339
  return code;

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

H
Hongze Cheng 已提交
344 345 346 347
int32_t tsdbUpdateDFileSetHeader(SDataFWriter *pWriter) {
  int32_t code = 0;
  int64_t n;
  char    hdr[TSDB_FHDR_SIZE];
H
Hongze Cheng 已提交
348

H
Hongze Cheng 已提交
349 350 351
  // head ==============
  memset(hdr, 0, TSDB_FHDR_SIZE);
  tPutHeadFile(hdr, &pWriter->fHead);
H
Hongze Cheng 已提交
352
  code = tsdbWriteFile(pWriter->pHeadFD, 0, hdr, TSDB_FHDR_SIZE);
H
Hongze Cheng 已提交
353
  if (code) goto _err;
H
Hongze Cheng 已提交
354 355 356 357

  // data ==============
  memset(hdr, 0, TSDB_FHDR_SIZE);
  tPutDataFile(hdr, &pWriter->fData);
H
Hongze Cheng 已提交
358
  code = tsdbWriteFile(pWriter->pDataFD, 0, hdr, TSDB_FHDR_SIZE);
H
Hongze Cheng 已提交
359
  if (code) goto _err;
H
Hongze Cheng 已提交
360

H
Hongze Cheng 已提交
361 362 363
  // sma ==============
  memset(hdr, 0, TSDB_FHDR_SIZE);
  tPutSmaFile(hdr, &pWriter->fSma);
H
Hongze Cheng 已提交
364
  code = tsdbWriteFile(pWriter->pSmaFD, 0, hdr, TSDB_FHDR_SIZE);
H
Hongze Cheng 已提交
365
  if (code) goto _err;
H
Hongze Cheng 已提交
366

H
Hongze Cheng 已提交
367
  // stt ==============
H
Hongze Cheng 已提交
368
  memset(hdr, 0, TSDB_FHDR_SIZE);
H
Hongze Cheng 已提交
369 370
  tPutSttFile(hdr, &pWriter->fStt[pWriter->wSet.nSttF - 1]);
  code = tsdbWriteFile(pWriter->pSttFD, 0, hdr, TSDB_FHDR_SIZE);
H
Hongze Cheng 已提交
371
  if (code) goto _err;
H
Hongze Cheng 已提交
372 373 374 375

  return code;

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

H
Hongze Cheng 已提交
380 381 382
int32_t tsdbWriteBlockIdx(SDataFWriter *pWriter, SArray *aBlockIdx) {
  int32_t    code = 0;
  SHeadFile *pHeadFile = &pWriter->fHead;
H
Hongze Cheng 已提交
383
  int64_t    size;
H
Hongze Cheng 已提交
384
  int64_t    n;
H
Hongze Cheng 已提交
385

H
Hongze Cheng 已提交
386 387 388
  // check
  if (taosArrayGetSize(aBlockIdx) == 0) {
    pHeadFile->offset = pHeadFile->size;
H
Hongze Cheng 已提交
389 390
    goto _exit;
  }
H
Hongze Cheng 已提交
391

H
Hongze Cheng 已提交
392
  // prepare
H
Hongze Cheng 已提交
393
  size = 0;
H
Hongze Cheng 已提交
394 395 396 397
  for (int32_t iBlockIdx = 0; iBlockIdx < taosArrayGetSize(aBlockIdx); iBlockIdx++) {
    size += tPutBlockIdx(NULL, taosArrayGet(aBlockIdx, iBlockIdx));
  }

H
Hongze Cheng 已提交
398
  // alloc
H
Hongze Cheng 已提交
399
  code = tRealloc(&pWriter->aBuf[0], size);
H
Hongze Cheng 已提交
400 401
  if (code) goto _err;

H
Hongze Cheng 已提交
402 403 404 405
  // build
  n = 0;
  for (int32_t iBlockIdx = 0; iBlockIdx < taosArrayGetSize(aBlockIdx); iBlockIdx++) {
    n += tPutBlockIdx(pWriter->aBuf[0] + n, taosArrayGet(aBlockIdx, iBlockIdx));
H
Hongze Cheng 已提交
406
  }
H
Hongze Cheng 已提交
407
  ASSERT(n == size);
H
Hongze Cheng 已提交
408 409

  // write
H
Hongze Cheng 已提交
410
  code = tsdbWriteFile(pWriter->pHeadFD, pHeadFile->size, pWriter->aBuf[0], size);
H
Hongze Cheng 已提交
411
  if (code) goto _err;
H
Hongze Cheng 已提交
412

H
Hongze Cheng 已提交
413 414 415
  // update
  pHeadFile->offset = pHeadFile->size;
  pHeadFile->size += size;
H
Hongze Cheng 已提交
416

H
Hongze Cheng 已提交
417
_exit:
H
Hongze Cheng 已提交
418 419 420
  // tsdbTrace("vgId:%d write block idx, offset:%" PRId64 " size:%" PRId64 " nBlockIdx:%d",
  // TD_VID(pWriter->pTsdb->pVnode),
  //           pHeadFile->offset, size, taosArrayGetSize(aBlockIdx));
H
Hongze Cheng 已提交
421 422 423
  return code;

_err:
H
Hongze Cheng 已提交
424
  tsdbError("vgId:%d, write block idx failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
425 426 427
  return code;
}

H
Hongze Cheng 已提交
428
int32_t tsdbWriteDataBlk(SDataFWriter *pWriter, SMapData *mDataBlk, SBlockIdx *pBlockIdx) {
H
Hongze Cheng 已提交
429 430 431 432 433
  int32_t    code = 0;
  SHeadFile *pHeadFile = &pWriter->fHead;
  int64_t    size;
  int64_t    n;

H
Hongze Cheng 已提交
434
  ASSERT(mDataBlk->nItem > 0);
H
Hongze Cheng 已提交
435

H
Hongze Cheng 已提交
436
  // alloc
H
Hongze Cheng 已提交
437
  size = tPutMapData(NULL, mDataBlk);
H
Hongze Cheng 已提交
438
  code = tRealloc(&pWriter->aBuf[0], size);
H
Hongze Cheng 已提交
439 440
  if (code) goto _err;

H
Hongze Cheng 已提交
441
  // build
H
Hongze Cheng 已提交
442
  n = tPutMapData(pWriter->aBuf[0], mDataBlk);
H
Hongze Cheng 已提交
443 444

  // write
H
Hongze Cheng 已提交
445
  code = tsdbWriteFile(pWriter->pHeadFD, pHeadFile->size, pWriter->aBuf[0], size);
H
Hongze Cheng 已提交
446
  if (code) goto _err;
H
Hongze Cheng 已提交
447

H
Hongze Cheng 已提交
448 449 450 451
  // update
  pBlockIdx->offset = pHeadFile->size;
  pBlockIdx->size = size;
  pHeadFile->size += size;
H
Hongze Cheng 已提交
452

H
Hongze Cheng 已提交
453
  tsdbTrace("vgId:%d, write block, file ID:%d commit ID:%" PRId64 " suid:%" PRId64 " uid:%" PRId64 " offset:%" PRId64
H
Hongze Cheng 已提交
454 455
            " size:%" PRId64 " nItem:%d",
            TD_VID(pWriter->pTsdb->pVnode), pWriter->wSet.fid, pHeadFile->commitID, pBlockIdx->suid, pBlockIdx->uid,
H
Hongze Cheng 已提交
456
            pBlockIdx->offset, pBlockIdx->size, mDataBlk->nItem);
H
Hongze Cheng 已提交
457 458 459
  return code;

_err:
H
Hongze Cheng 已提交
460
  tsdbError("vgId:%d, write block failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
461 462 463
  return code;
}

H
Hongze Cheng 已提交
464
int32_t tsdbWriteSttBlk(SDataFWriter *pWriter, SArray *aSttBlk) {
H
Hongze Cheng 已提交
465
  int32_t   code = 0;
H
Hongze Cheng 已提交
466
  SSttFile *pSttFile = &pWriter->fStt[pWriter->wSet.nSttF - 1];
H
Hongze Cheng 已提交
467
  int64_t   size = 0;
H
Hongze Cheng 已提交
468
  int64_t   n;
H
Hongze Cheng 已提交
469

H
Hongze Cheng 已提交
470
  // check
H
Hongze Cheng 已提交
471 472
  if (taosArrayGetSize(aSttBlk) == 0) {
    pSttFile->offset = pSttFile->size;
H
Hongze Cheng 已提交
473 474
    goto _exit;
  }
H
Hongze Cheng 已提交
475

H
Hongze Cheng 已提交
476
  // size
H
Hongze Cheng 已提交
477
  size = 0;
H
Hongze Cheng 已提交
478 479
  for (int32_t iBlockL = 0; iBlockL < taosArrayGetSize(aSttBlk); iBlockL++) {
    size += tPutSttBlk(NULL, taosArrayGet(aSttBlk, iBlockL));
H
Hongze Cheng 已提交
480
  }
H
Hongze Cheng 已提交
481 482

  // alloc
H
Hongze Cheng 已提交
483
  code = tRealloc(&pWriter->aBuf[0], size);
H
Hongze Cheng 已提交
484 485
  if (code) goto _err;

H
Hongze Cheng 已提交
486 487
  // encode
  n = 0;
H
Hongze Cheng 已提交
488 489
  for (int32_t iBlockL = 0; iBlockL < taosArrayGetSize(aSttBlk); iBlockL++) {
    n += tPutSttBlk(pWriter->aBuf[0] + n, taosArrayGet(aSttBlk, iBlockL));
H
Hongze Cheng 已提交
490
  }
H
Hongze Cheng 已提交
491 492

  // write
H
Hongze Cheng 已提交
493
  code = tsdbWriteFile(pWriter->pSttFD, pSttFile->size, pWriter->aBuf[0], size);
H
Hongze Cheng 已提交
494
  if (code) goto _err;
H
Hongze Cheng 已提交
495

H
Hongze Cheng 已提交
496
  // update
H
Hongze Cheng 已提交
497 498
  pSttFile->offset = pSttFile->size;
  pSttFile->size += size;
H
Hongze Cheng 已提交
499

H
Hongze Cheng 已提交
500
_exit:
H
Hongze Cheng 已提交
501 502
  tsdbTrace("vgId:%d tsdb write stt block, loffset:%" PRId64 " size:%" PRId64, TD_VID(pWriter->pTsdb->pVnode),
            pSttFile->offset, size);
H
Hongze Cheng 已提交
503 504 505
  return code;

_err:
H
Hongze Cheng 已提交
506
  tsdbError("vgId:%d tsdb write blockl failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
507 508 509
  return code;
}

H
Hongze Cheng 已提交
510
static int32_t tsdbWriteBlockSma(SDataFWriter *pWriter, SBlockData *pBlockData, SSmaInfo *pSmaInfo) {
H
Hongze Cheng 已提交
511 512
  int32_t code = 0;

H
Hongze Cheng 已提交
513 514
  pSmaInfo->offset = 0;
  pSmaInfo->size = 0;
H
Hongze Cheng 已提交
515

H
Hongze Cheng 已提交
516 517 518
  // encode
  for (int32_t iColData = 0; iColData < taosArrayGetSize(pBlockData->aIdx); iColData++) {
    SColData *pColData = tBlockDataGetColDataByIdx(pBlockData, iColData);
H
Hongze Cheng 已提交
519

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

H
Hongze Cheng 已提交
522 523
    SColumnDataAgg sma;
    tsdbCalcColDataSMA(pColData, &sma);
H
Hongze Cheng 已提交
524

H
Hongze Cheng 已提交
525 526 527 528
    code = tRealloc(&pWriter->aBuf[0], pSmaInfo->size + tPutColumnDataAgg(NULL, &sma));
    if (code) goto _err;
    pSmaInfo->size += tPutColumnDataAgg(pWriter->aBuf[0] + pSmaInfo->size, &sma);
  }
H
Hongze Cheng 已提交
529

H
Hongze Cheng 已提交
530 531
  // write
  if (pSmaInfo->size) {
H
Hongze Cheng 已提交
532
    code = tsdbWriteFile(pWriter->pSmaFD, pWriter->fSma.size, pWriter->aBuf[0], pSmaInfo->size);
H
Hongze Cheng 已提交
533
    if (code) goto _err;
H
Hongze Cheng 已提交
534

H
Hongze Cheng 已提交
535
    pSmaInfo->offset = pWriter->fSma.size;
H
Hongze Cheng 已提交
536
    pWriter->fSma.size += pSmaInfo->size;
H
Hongze Cheng 已提交
537 538 539 540 541
  }

  return code;

_err:
H
Hongze Cheng 已提交
542
  tsdbError("vgId:%d tsdb write block sma failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
543 544 545
  return code;
}

H
Hongze Cheng 已提交
546 547
int32_t tsdbWriteBlockData(SDataFWriter *pWriter, SBlockData *pBlockData, SBlockInfo *pBlkInfo, SSmaInfo *pSmaInfo,
                           int8_t cmprAlg, int8_t toLast) {
H
Hongze Cheng 已提交
548 549
  int32_t code = 0;

H
Hongze Cheng 已提交
550
  ASSERT(pBlockData->nRow > 0);
H
Hongze Cheng 已提交
551

H
Hongze Cheng 已提交
552
  if (toLast) {
H
Hongze Cheng 已提交
553
    pBlkInfo->offset = pWriter->fStt[pWriter->wSet.nSttF - 1].size;
H
Hongze Cheng 已提交
554 555 556
  } else {
    pBlkInfo->offset = pWriter->fData.size;
  }
H
Hongze Cheng 已提交
557 558
  pBlkInfo->szBlock = 0;
  pBlkInfo->szKey = 0;
H
Hongze Cheng 已提交
559

H
Hongze Cheng 已提交
560 561 562
  int32_t aBufN[4] = {0};
  code = tCmprBlockData(pBlockData, cmprAlg, NULL, NULL, pWriter->aBuf, aBufN);
  if (code) goto _err;
H
Hongze Cheng 已提交
563

H
Hongze Cheng 已提交
564
  // write =================
H
Hongze Cheng 已提交
565
  STsdbFD *pFD = toLast ? pWriter->pSttFD : pWriter->pDataFD;
H
Hongze Cheng 已提交
566

H
Hongze Cheng 已提交
567 568
  pBlkInfo->szKey = aBufN[3] + aBufN[2];
  pBlkInfo->szBlock = aBufN[0] + aBufN[1] + aBufN[2] + aBufN[3];
H
Hongze Cheng 已提交
569

H
Hongze Cheng 已提交
570 571
  int64_t offset = pBlkInfo->offset;
  code = tsdbWriteFile(pFD, offset, pWriter->aBuf[3], aBufN[3]);
H
Hongze Cheng 已提交
572
  if (code) goto _err;
H
Hongze Cheng 已提交
573
  offset += aBufN[3];
H
Hongze Cheng 已提交
574

H
Hongze Cheng 已提交
575
  code = tsdbWriteFile(pFD, offset, pWriter->aBuf[2], aBufN[2]);
H
Hongze Cheng 已提交
576
  if (code) goto _err;
H
Hongze Cheng 已提交
577
  offset += aBufN[2];
H
Hongze Cheng 已提交
578

H
Hongze Cheng 已提交
579
  if (aBufN[1]) {
H
Hongze Cheng 已提交
580
    code = tsdbWriteFile(pFD, offset, pWriter->aBuf[1], aBufN[1]);
H
Hongze Cheng 已提交
581
    if (code) goto _err;
H
Hongze Cheng 已提交
582
    offset += aBufN[1];
H
Hongze Cheng 已提交
583
  }
H
Hongze Cheng 已提交
584

H
Hongze Cheng 已提交
585
  if (aBufN[0]) {
H
Hongze Cheng 已提交
586
    code = tsdbWriteFile(pFD, offset, pWriter->aBuf[0], aBufN[0]);
H
Hongze Cheng 已提交
587
    if (code) goto _err;
H
Hongze Cheng 已提交
588
  }
H
Hongze Cheng 已提交
589

H
Hongze Cheng 已提交
590 591
  // update info
  if (toLast) {
H
Hongze Cheng 已提交
592
    pWriter->fStt[pWriter->wSet.nSttF - 1].size += pBlkInfo->szBlock;
H
Hongze Cheng 已提交
593 594 595
  } else {
    pWriter->fData.size += pBlkInfo->szBlock;
  }
H
Hongze Cheng 已提交
596

H
Hongze Cheng 已提交
597 598 599 600 601
  // ================= SMA ====================
  if (pSmaInfo) {
    code = tsdbWriteBlockSma(pWriter, pBlockData, pSmaInfo);
    if (code) goto _err;
  }
H
Hongze Cheng 已提交
602

H
Hongze Cheng 已提交
603 604 605 606
_exit:
  tsdbTrace("vgId:%d tsdb write block data, suid:%" PRId64 " uid:%" PRId64 " nRow:%d, offset:%" PRId64 " size:%d",
            TD_VID(pWriter->pTsdb->pVnode), pBlockData->suid, pBlockData->uid, pBlockData->nRow, pBlkInfo->offset,
            pBlkInfo->szBlock);
H
Hongze Cheng 已提交
607 608 609
  return code;

_err:
H
Hongze Cheng 已提交
610
  tsdbError("vgId:%d tsdb write block data failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
611 612
  return code;
}
H
Hongze Cheng 已提交
613

H
Hongze Cheng 已提交
614
int32_t tsdbWriteDiskData(SDataFWriter *pWriter, const SDiskData *pDiskData, SBlockInfo *pBlkInfo, SSmaInfo *pSmaInfo) {
H
Hongze Cheng 已提交
615 616 617
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
618 619 620 621 622 623 624 625 626 627
  STsdbFD *pFD = NULL;
  if (pSmaInfo) {
    pFD = pWriter->pDataFD;
    pBlkInfo->offset = pWriter->fData.size;
  } else {
    pFD = pWriter->pSttFD;
    pBlkInfo->offset = pWriter->fStt[pWriter->wSet.nSttF - 1].size;
  }
  pBlkInfo->szBlock = 0;
  pBlkInfo->szKey = 0;
H
Hongze Cheng 已提交
628 629 630 631 632 633 634 635

  // hdr
  int32_t n = tPutDiskDataHdr(NULL, &pDiskData->hdr);
  code = tRealloc(&pWriter->aBuf[0], n);
  TSDB_CHECK_CODE(code, lino, _exit);

  tPutDiskDataHdr(pWriter->aBuf[0], &pDiskData->hdr);

H
Hongze Cheng 已提交
636
  code = tsdbWriteFile(pFD, pBlkInfo->offset, pWriter->aBuf[0], n);
H
Hongze Cheng 已提交
637
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
638 639
  pBlkInfo->szKey += n;
  pBlkInfo->szBlock += n;
H
Hongze Cheng 已提交
640 641

  // uid + ver + key
H
Hongze Cheng 已提交
642 643
  if (pDiskData->pUid) {
    code = tsdbWriteFile(pFD, pBlkInfo->offset + pBlkInfo->szBlock, pDiskData->pUid, pDiskData->hdr.szUid);
H
Hongze Cheng 已提交
644
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
645 646
    pBlkInfo->szKey += pDiskData->hdr.szUid;
    pBlkInfo->szBlock += pDiskData->hdr.szUid;
H
Hongze Cheng 已提交
647 648
  }

H
Hongze Cheng 已提交
649
  code = tsdbWriteFile(pFD, pBlkInfo->offset + pBlkInfo->szBlock, pDiskData->pVer, pDiskData->hdr.szVer);
H
Hongze Cheng 已提交
650
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
651 652
  pBlkInfo->szKey += pDiskData->hdr.szVer;
  pBlkInfo->szBlock += pDiskData->hdr.szVer;
H
Hongze Cheng 已提交
653

H
Hongze Cheng 已提交
654
  code = tsdbWriteFile(pFD, pBlkInfo->offset + pBlkInfo->szBlock, pDiskData->pKey, pDiskData->hdr.szKey);
H
Hongze Cheng 已提交
655
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
656 657
  pBlkInfo->szKey += pDiskData->hdr.szKey;
  pBlkInfo->szBlock += pDiskData->hdr.szKey;
H
Hongze Cheng 已提交
658

H
Hongze Cheng 已提交
659
  // aBlockCol
H
Hongze Cheng 已提交
660 661 662 663 664 665 666
  if (pDiskData->hdr.szBlkCol) {
    code = tRealloc(&pWriter->aBuf[0], pDiskData->hdr.szBlkCol);
    TSDB_CHECK_CODE(code, lino, _exit);

    n = 0;
    for (int32_t iDiskCol = 0; iDiskCol < taosArrayGetSize(pDiskData->aDiskCol); iDiskCol++) {
      SDiskCol *pDiskCol = (SDiskCol *)taosArrayGet(pDiskData->aDiskCol, iDiskCol);
H
Hongze Cheng 已提交
667
      n += tPutBlockCol(pWriter->aBuf[0] + n, pDiskCol);
H
Hongze Cheng 已提交
668 669 670
    }
    ASSERT(n == pDiskData->hdr.szBlkCol);

H
Hongze Cheng 已提交
671
    code = tsdbWriteFile(pFD, pBlkInfo->offset + pBlkInfo->szBlock, pWriter->aBuf[0], pDiskData->hdr.szBlkCol);
H
Hongze Cheng 已提交
672 673
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
674
    pBlkInfo->szBlock += pDiskData->hdr.szBlkCol;
H
Hongze Cheng 已提交
675 676
  }

H
Hongze Cheng 已提交
677
  // aDiskCol
H
Hongze Cheng 已提交
678 679 680
  for (int32_t iDiskCol = 0; iDiskCol < taosArrayGetSize(pDiskData->aDiskCol); iDiskCol++) {
    SDiskCol *pDiskCol = (SDiskCol *)taosArrayGet(pDiskData->aDiskCol, iDiskCol);

H
Hongze Cheng 已提交
681 682
    if (pDiskCol->pBit) {
      code = tsdbWriteFile(pFD, pBlkInfo->offset + pBlkInfo->szBlock, pDiskCol->pBit, pDiskCol->bCol.szBitmap);
H
Hongze Cheng 已提交
683
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
684 685

      pBlkInfo->szBlock += pDiskCol->bCol.szBitmap;
H
Hongze Cheng 已提交
686 687
    }

H
Hongze Cheng 已提交
688 689
    if (pDiskCol->pOff) {
      code = tsdbWriteFile(pFD, pBlkInfo->offset + pBlkInfo->szBlock, pDiskCol->pOff, pDiskCol->bCol.szOffset);
H
Hongze Cheng 已提交
690
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
691 692

      pBlkInfo->szBlock += pDiskCol->bCol.szOffset;
H
Hongze Cheng 已提交
693 694
    }

H
Hongze Cheng 已提交
695 696
    if (pDiskCol->pVal) {
      code = tsdbWriteFile(pFD, pBlkInfo->offset + pBlkInfo->szBlock, pDiskCol->pVal, pDiskCol->bCol.szValue);
H
Hongze Cheng 已提交
697
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
698 699

      pBlkInfo->szBlock += pDiskCol->bCol.szValue;
H
Hongze Cheng 已提交
700 701 702
    }
  }

H
Hongze Cheng 已提交
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
  if (pSmaInfo) {
    pWriter->fData.size += pBlkInfo->szBlock;
  } else {
    pWriter->fStt[pWriter->wSet.nSttF - 1].size += pBlkInfo->szBlock;
    goto _exit;
  }

  pSmaInfo->offset = 0;
  pSmaInfo->size = 0;
  for (int32_t iDiskCol = 0; iDiskCol < taosArrayGetSize(pDiskData->aDiskCol); iDiskCol++) {
    SDiskCol *pDiskCol = (SDiskCol *)taosArrayGet(pDiskData->aDiskCol, iDiskCol);

    if (IS_VAR_DATA_TYPE(pDiskCol->bCol.type)) continue;
    if (pDiskCol->bCol.flag == HAS_NULL || pDiskCol->bCol.flag == (HAS_NULL | HAS_NONE)) continue;
    if (!pDiskCol->bCol.smaOn) continue;

    code = tRealloc(&pWriter->aBuf[0], pSmaInfo->size + tPutColumnDataAgg(NULL, &pDiskCol->agg));
    TSDB_CHECK_CODE(code, lino, _exit);
    pSmaInfo->size += tPutColumnDataAgg(pWriter->aBuf[0] + pSmaInfo->size, &pDiskCol->agg);
  }

  if (pSmaInfo->size) {
    pSmaInfo->offset = pWriter->fSma.size;

    code = tsdbWriteFile(pWriter->pSmaFD, pSmaInfo->offset, pWriter->aBuf[0], pSmaInfo->size);
    TSDB_CHECK_CODE(code, lino, _exit);

    pWriter->fSma.size += pSmaInfo->size;
  }

H
Hongze Cheng 已提交
733 734 735 736 737 738 739
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
  return code;
}

H
Hongze Cheng 已提交
740 741 742 743
int32_t tsdbDFileSetCopy(STsdb *pTsdb, SDFileSet *pSetFrom, SDFileSet *pSetTo) {
  int32_t   code = 0;
  int64_t   n;
  int64_t   size;
H
Hongze Cheng 已提交
744 745 746
  TdFilePtr pOutFD = NULL;
  TdFilePtr PInFD = NULL;
  int32_t   szPage = pTsdb->pVnode->config.szPage;
H
Hongze Cheng 已提交
747 748
  char      fNameFrom[TSDB_FILENAME_LEN];
  char      fNameTo[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
749

H
Hongze Cheng 已提交
750 751 752 753 754 755
  // head
  tsdbHeadFileName(pTsdb, pSetFrom->diskId, pSetFrom->fid, pSetFrom->pHeadF, fNameFrom);
  tsdbHeadFileName(pTsdb, pSetTo->diskId, pSetTo->fid, pSetTo->pHeadF, fNameTo);
  pOutFD = taosOpenFile(fNameTo, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
  if (pOutFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
756 757
    goto _err;
  }
H
Hongze Cheng 已提交
758 759
  PInFD = taosOpenFile(fNameFrom, TD_FILE_READ);
  if (PInFD == NULL) {
H
Hongze Cheng 已提交
760 761 762
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
H
Hongze Cheng 已提交
763
  n = taosFSendFile(pOutFD, PInFD, 0, tsdbLogicToFileSize(pSetFrom->pHeadF->size, szPage));
H
Hongze Cheng 已提交
764 765 766 767
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
H
Hongze Cheng 已提交
768 769
  taosCloseFile(&pOutFD);
  taosCloseFile(&PInFD);
H
Hongze Cheng 已提交
770 771

  // data
H
Hongze Cheng 已提交
772 773 774 775 776 777
  tsdbDataFileName(pTsdb, pSetFrom->diskId, pSetFrom->fid, pSetFrom->pDataF, fNameFrom);
  tsdbDataFileName(pTsdb, pSetTo->diskId, pSetTo->fid, pSetTo->pDataF, fNameTo);
  pOutFD = taosOpenFile(fNameTo, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
  if (pOutFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
H
Hongze Cheng 已提交
778
  }
H
Hongze Cheng 已提交
779 780
  PInFD = taosOpenFile(fNameFrom, TD_FILE_READ);
  if (PInFD == NULL) {
H
Hongze Cheng 已提交
781 782 783
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
H
Hongze Cheng 已提交
784
  n = taosFSendFile(pOutFD, PInFD, 0, LOGIC_TO_FILE_OFFSET(pSetFrom->pDataF->size, szPage));
H
Hongze Cheng 已提交
785
  if (n < 0) {
H
Hongze Cheng 已提交
786 787 788
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
H
Hongze Cheng 已提交
789 790
  taosCloseFile(&pOutFD);
  taosCloseFile(&PInFD);
H
Hongze Cheng 已提交
791

H
Hongze Cheng 已提交
792 793 794 795 796
  // sma
  tsdbSmaFileName(pTsdb, pSetFrom->diskId, pSetFrom->fid, pSetFrom->pSmaF, fNameFrom);
  tsdbSmaFileName(pTsdb, pSetTo->diskId, pSetTo->fid, pSetTo->pSmaF, fNameTo);
  pOutFD = taosOpenFile(fNameTo, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
  if (pOutFD == NULL) {
H
Hongze Cheng 已提交
797 798 799
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
H
Hongze Cheng 已提交
800 801
  PInFD = taosOpenFile(fNameFrom, TD_FILE_READ);
  if (PInFD == NULL) {
H
Hongze Cheng 已提交
802 803 804
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
H
Hongze Cheng 已提交
805
  n = taosFSendFile(pOutFD, PInFD, 0, tsdbLogicToFileSize(pSetFrom->pSmaF->size, szPage));
H
Hongze Cheng 已提交
806 807 808
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
H
Hongze Cheng 已提交
809
  }
H
Hongze Cheng 已提交
810 811 812
  taosCloseFile(&pOutFD);
  taosCloseFile(&PInFD);

H
Hongze Cheng 已提交
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
  // stt
  for (int8_t iStt = 0; iStt < pSetFrom->nSttF; iStt++) {
    tsdbSttFileName(pTsdb, pSetFrom->diskId, pSetFrom->fid, pSetFrom->aSttF[iStt], fNameFrom);
    tsdbSttFileName(pTsdb, pSetTo->diskId, pSetTo->fid, pSetTo->aSttF[iStt], fNameTo);
    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;
    }
    n = taosFSendFile(pOutFD, PInFD, 0, tsdbLogicToFileSize(pSetFrom->aSttF[iStt]->size, szPage));
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }
    taosCloseFile(&pOutFD);
    taosCloseFile(&PInFD);
  }

H
Hongze Cheng 已提交
836 837 838
  return code;

_err:
H
Hongze Cheng 已提交
839
  tsdbError("vgId:%d, tsdb DFileSet copy failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
840 841 842
  return code;
}

H
Hongze Cheng 已提交
843 844 845 846
// SDataFReader ====================================================
int32_t tsdbDataFReaderOpen(SDataFReader **ppReader, STsdb *pTsdb, SDFileSet *pSet) {
  int32_t       code = 0;
  SDataFReader *pReader;
H
Hongze Cheng 已提交
847
  int32_t       szPage = pTsdb->pVnode->config.tsdbPageSize;
H
Hongze Cheng 已提交
848
  char          fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
849

H
Hongze Cheng 已提交
850 851 852 853
  // alloc
  pReader = (SDataFReader *)taosMemoryCalloc(1, sizeof(*pReader));
  if (pReader == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
854 855
    goto _err;
  }
H
Hongze Cheng 已提交
856 857
  pReader->pTsdb = pTsdb;
  pReader->pSet = pSet;
H
Hongze Cheng 已提交
858

H
Hongze Cheng 已提交
859 860
  // head
  tsdbHeadFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pHeadF, fname);
H
Hongze Cheng 已提交
861 862
  code = tsdbOpenFile(fname, szPage, TD_FILE_READ, &pReader->pHeadFD);
  if (code) goto _err;
H
Hongze Cheng 已提交
863

H
Hongze Cheng 已提交
864 865
  // data
  tsdbDataFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pDataF, fname);
H
Hongze Cheng 已提交
866 867
  code = tsdbOpenFile(fname, szPage, TD_FILE_READ, &pReader->pDataFD);
  if (code) goto _err;
H
Hongze Cheng 已提交
868

H
Hongze Cheng 已提交
869 870
  // sma
  tsdbSmaFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pSmaF, fname);
H
Hongze Cheng 已提交
871 872
  code = tsdbOpenFile(fname, szPage, TD_FILE_READ, &pReader->pSmaFD);
  if (code) goto _err;
H
Hongze Cheng 已提交
873

H
Hongze Cheng 已提交
874 875 876 877
  // stt
  for (int32_t iStt = 0; iStt < pSet->nSttF; iStt++) {
    tsdbSttFileName(pTsdb, pSet->diskId, pSet->fid, pSet->aSttF[iStt], fname);
    code = tsdbOpenFile(fname, szPage, TD_FILE_READ, &pReader->aSttFD[iStt]);
H
Hongze Cheng 已提交
878
    if (code) goto _err;
H
Hongze Cheng 已提交
879
  }
H
Hongze Cheng 已提交
880

H
Hongze Cheng 已提交
881 882 883 884 885 886 887 888 889 890 891
  *ppReader = pReader;
  return code;

_err:
  tsdbError("vgId:%d, tsdb data file reader open failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
  *ppReader = NULL;
  return code;
}

int32_t tsdbDataFReaderClose(SDataFReader **ppReader) {
  int32_t code = 0;
H
Hongze Cheng 已提交
892
  if (*ppReader == NULL) return code;
H
Hongze Cheng 已提交
893 894

  // head
H
Hongze Cheng 已提交
895
  tsdbCloseFile(&(*ppReader)->pHeadFD);
H
Hongze Cheng 已提交
896

H
Hongze Cheng 已提交
897
  // data
H
Hongze Cheng 已提交
898
  tsdbCloseFile(&(*ppReader)->pDataFD);
H
Hongze Cheng 已提交
899

H
Hongze Cheng 已提交
900
  // sma
H
Hongze Cheng 已提交
901
  tsdbCloseFile(&(*ppReader)->pSmaFD);
H
Hongze Cheng 已提交
902

H
Hongze Cheng 已提交
903
  // stt
H
Hongze Cheng 已提交
904
  for (int32_t iStt = 0; iStt < TSDB_MAX_STT_TRIGGER; iStt++) {
H
Hongze Cheng 已提交
905 906
    if ((*ppReader)->aSttFD[iStt]) {
      tsdbCloseFile(&(*ppReader)->aSttFD[iStt]);
H
Hongze Cheng 已提交
907
    }
H
Hongze Cheng 已提交
908 909 910 911
  }

  for (int32_t iBuf = 0; iBuf < sizeof((*ppReader)->aBuf) / sizeof(uint8_t *); iBuf++) {
    tFree((*ppReader)->aBuf[iBuf]);
H
Hongze Cheng 已提交
912
  }
H
Hongze Cheng 已提交
913 914
  taosMemoryFree(*ppReader);
  *ppReader = NULL;
H
Hongze Cheng 已提交
915
  return code;
H
Hongze Cheng 已提交
916 917
}

H
Hongze Cheng 已提交
918
int32_t tsdbReadBlockIdx(SDataFReader *pReader, SArray *aBlockIdx) {
H
Hongze Cheng 已提交
919 920 921 922
  int32_t    code = 0;
  SHeadFile *pHeadFile = pReader->pSet->pHeadF;
  int64_t    offset = pHeadFile->offset;
  int64_t    size = pHeadFile->size - offset;
H
Hongze Cheng 已提交
923

H
Hongze Cheng 已提交
924
  taosArrayClear(aBlockIdx);
H
Hongze Cheng 已提交
925
  if (size == 0) return code;
H
Hongze Cheng 已提交
926 927

  // alloc
H
Hongze Cheng 已提交
928
  code = tRealloc(&pReader->aBuf[0], size);
H
Hongze Cheng 已提交
929 930
  if (code) goto _err;

H
Hongze Cheng 已提交
931
  // read
H
Hongze Cheng 已提交
932 933
  code = tsdbReadFile(pReader->pHeadFD, offset, pReader->aBuf[0], size);
  if (code) goto _err;
H
Hongze Cheng 已提交
934 935

  // decode
H
Hongze Cheng 已提交
936
  int64_t n = 0;
H
Hongze Cheng 已提交
937
  while (n < size) {
H
Hongze Cheng 已提交
938 939 940 941 942 943 944
    SBlockIdx blockIdx;
    n += tGetBlockIdx(pReader->aBuf[0] + n, &blockIdx);

    if (taosArrayPush(aBlockIdx, &blockIdx) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
945
  }
H
Hongze Cheng 已提交
946
  ASSERT(n == size);
H
Hongze Cheng 已提交
947 948 949 950

  return code;

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

H
Hongze Cheng 已提交
955
int32_t tsdbReadSttBlk(SDataFReader *pReader, int32_t iStt, SArray *aSttBlk) {
H
Hongze Cheng 已提交
956
  int32_t   code = 0;
H
Hongze Cheng 已提交
957 958 959
  SSttFile *pSttFile = pReader->pSet->aSttF[iStt];
  int64_t   offset = pSttFile->offset;
  int64_t   size = pSttFile->size - offset;
H
Hongze Cheng 已提交
960

H
Hongze Cheng 已提交
961
  taosArrayClear(aSttBlk);
H
Hongze Cheng 已提交
962
  if (size == 0) return code;
H
Hongze Cheng 已提交
963 964

  // alloc
H
Hongze Cheng 已提交
965
  code = tRealloc(&pReader->aBuf[0], size);
H
Hongze Cheng 已提交
966 967
  if (code) goto _err;

H
Hongze Cheng 已提交
968
  // read
H
Hongze Cheng 已提交
969
  code = tsdbReadFile(pReader->aSttFD[iStt], offset, pReader->aBuf[0], size);
H
Hongze Cheng 已提交
970
  if (code) goto _err;
H
Hongze Cheng 已提交
971

H
Hongze Cheng 已提交
972
  // decode
H
Hongze Cheng 已提交
973
  int64_t n = 0;
H
Hongze Cheng 已提交
974
  while (n < size) {
H
Hongze Cheng 已提交
975 976
    SSttBlk sttBlk;
    n += tGetSttBlk(pReader->aBuf[0] + n, &sttBlk);
H
Hongze Cheng 已提交
977

H
Hongze Cheng 已提交
978
    if (taosArrayPush(aSttBlk, &sttBlk) == NULL) {
H
Hongze Cheng 已提交
979 980 981 982
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
  }
H
Hongze Cheng 已提交
983
  ASSERT(n == size);
H
Hongze Cheng 已提交
984

H
Hongze Cheng 已提交
985 986 987
  return code;

_err:
H
Hongze Cheng 已提交
988
  tsdbError("vgId:%d read stt blk failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
989 990 991
  return code;
}

H
Hongze Cheng 已提交
992
int32_t tsdbReadDataBlk(SDataFReader *pReader, SBlockIdx *pBlockIdx, SMapData *mDataBlk) {
H
Hongze Cheng 已提交
993 994 995
  int32_t code = 0;
  int64_t offset = pBlockIdx->offset;
  int64_t size = pBlockIdx->size;
H
Hongze Cheng 已提交
996 997

  // alloc
H
Hongze Cheng 已提交
998
  code = tRealloc(&pReader->aBuf[0], size);
H
Hongze Cheng 已提交
999 1000
  if (code) goto _err;

H
Hongze Cheng 已提交
1001
  // read
H
Hongze Cheng 已提交
1002 1003
  code = tsdbReadFile(pReader->pHeadFD, offset, pReader->aBuf[0], size);
  if (code) goto _err;
H
Hongze Cheng 已提交
1004

H
Hongze Cheng 已提交
1005
  // decode
H
Hongze Cheng 已提交
1006
  int64_t n = tGetMapData(pReader->aBuf[0], mDataBlk);
H
Hongze Cheng 已提交
1007
  if (n < 0) {
H
Hongze Cheng 已提交
1008 1009 1010
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
1011
  ASSERT(n == size);
H
Hongze Cheng 已提交
1012

H
Hongze Cheng 已提交
1013
  return code;
H
Hongze Cheng 已提交
1014

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

H
Hongze Cheng 已提交
1020 1021 1022
int32_t tsdbReadBlockSma(SDataFReader *pReader, SDataBlk *pDataBlk, SArray *aColumnDataAgg) {
  int32_t   code = 0;
  SSmaInfo *pSmaInfo = &pDataBlk->smaInfo;
H
Hongze Cheng 已提交
1023

H
Hongze Cheng 已提交
1024
  ASSERT(pSmaInfo->size > 0);
H
Hongze Cheng 已提交
1025

H
Hongze Cheng 已提交
1026
  taosArrayClear(aColumnDataAgg);
H
Hongze Cheng 已提交
1027

H
Hongze Cheng 已提交
1028
  // alloc
H
Hongze Cheng 已提交
1029
  code = tRealloc(&pReader->aBuf[0], pSmaInfo->size);
H
Hongze Cheng 已提交
1030
  if (code) goto _err;
H
Hongze Cheng 已提交
1031

H
Hongze Cheng 已提交
1032
  // read
H
Hongze Cheng 已提交
1033
  code = tsdbReadFile(pReader->pSmaFD, pSmaInfo->offset, pReader->aBuf[0], pSmaInfo->size);
H
Hongze Cheng 已提交
1034
  if (code) goto _err;
H
Hongze Cheng 已提交
1035

H
Hongze Cheng 已提交
1036
  // decode
H
Hongze Cheng 已提交
1037
  int32_t n = 0;
H
Hongze Cheng 已提交
1038 1039 1040
  while (n < pSmaInfo->size) {
    SColumnDataAgg sma;
    n += tGetColumnDataAgg(pReader->aBuf[0] + n, &sma);
H
Hongze Cheng 已提交
1041

H
Hongze Cheng 已提交
1042 1043
    if (taosArrayPush(aColumnDataAgg, &sma) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1044 1045
      goto _err;
    }
H
Hongze Cheng 已提交
1046
  }
H
Hongze Cheng 已提交
1047
  ASSERT(n == pSmaInfo->size);
H
Hongze Cheng 已提交
1048
  return code;
H
Hongze Cheng 已提交
1049

H
Hongze Cheng 已提交
1050
_err:
H
Hongze Cheng 已提交
1051
  tsdbError("vgId:%d tsdb read block sma failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1052 1053
  return code;
}
H
Hongze Cheng 已提交
1054

H
Hongze Cheng 已提交
1055 1056
static int32_t tsdbReadBlockDataImpl(SDataFReader *pReader, SBlockInfo *pBlkInfo, SBlockData *pBlockData,
                                     int32_t iStt) {
H
Hongze Cheng 已提交
1057
  int32_t code = 0;
H
Hongze Cheng 已提交
1058

H
Hongze Cheng 已提交
1059 1060
  tBlockDataClear(pBlockData);

H
Hongze Cheng 已提交
1061
  STsdbFD *pFD = (iStt < 0) ? pReader->pDataFD : pReader->aSttFD[iStt];
H
Hongze Cheng 已提交
1062 1063

  // uid + version + tskey
H
Hongze Cheng 已提交
1064 1065 1066 1067 1068 1069
  code = tRealloc(&pReader->aBuf[0], pBlkInfo->szKey);
  if (code) goto _err;

  code = tsdbReadFile(pFD, pBlkInfo->offset, pReader->aBuf[0], pBlkInfo->szKey);
  if (code) goto _err;

H
Hongze Cheng 已提交
1070 1071 1072 1073 1074 1075
  SDiskDataHdr hdr;
  uint8_t     *p = pReader->aBuf[0] + tGetDiskDataHdr(pReader->aBuf[0], &hdr);

  ASSERT(hdr.delimiter == TSDB_FILE_DLMT);
  ASSERT(pBlockData->suid == hdr.suid);

H
Hongze Cheng 已提交
1076
  pBlockData->uid = hdr.uid;
H
Hongze Cheng 已提交
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
  pBlockData->nRow = hdr.nRow;

  // uid
  if (hdr.uid == 0) {
    ASSERT(hdr.szUid);
    code = tsdbDecmprData(p, hdr.szUid, TSDB_DATA_TYPE_BIGINT, hdr.cmprAlg, (uint8_t **)&pBlockData->aUid,
                          sizeof(int64_t) * hdr.nRow, &pReader->aBuf[1]);
    if (code) goto _err;
  } else {
    ASSERT(!hdr.szUid);
  }
  p += hdr.szUid;
H
Hongze Cheng 已提交
1089

H
Hongze Cheng 已提交
1090 1091 1092 1093 1094
  // version
  code = tsdbDecmprData(p, hdr.szVer, TSDB_DATA_TYPE_BIGINT, hdr.cmprAlg, (uint8_t **)&pBlockData->aVersion,
                        sizeof(int64_t) * hdr.nRow, &pReader->aBuf[1]);
  if (code) goto _err;
  p += hdr.szVer;
H
Hongze Cheng 已提交
1095

H
Hongze Cheng 已提交
1096 1097 1098
  // TSKEY
  code = tsdbDecmprData(p, hdr.szKey, TSDB_DATA_TYPE_TIMESTAMP, hdr.cmprAlg, (uint8_t **)&pBlockData->aTSKEY,
                        sizeof(TSKEY) * hdr.nRow, &pReader->aBuf[1]);
H
Hongze Cheng 已提交
1099
  if (code) goto _err;
H
Hongze Cheng 已提交
1100
  p += hdr.szKey;
H
Hongze Cheng 已提交
1101

H
Hongze Cheng 已提交
1102
  ASSERT(p - pReader->aBuf[0] == pBlkInfo->szKey);
H
Hongze Cheng 已提交
1103

H
Hongze Cheng 已提交
1104 1105
  // read and decode columns
  if (taosArrayGetSize(pBlockData->aIdx) == 0) goto _exit;
H
Hongze Cheng 已提交
1106

H
Hongze Cheng 已提交
1107 1108
  if (hdr.szBlkCol > 0) {
    int64_t offset = pBlkInfo->offset + pBlkInfo->szKey;
H
Hongze Cheng 已提交
1109 1110 1111 1112

    code = tRealloc(&pReader->aBuf[0], hdr.szBlkCol);
    if (code) goto _err;

H
Hongze Cheng 已提交
1113 1114
    code = tsdbReadFile(pFD, offset, pReader->aBuf[0], hdr.szBlkCol);
    if (code) goto _err;
H
Hongze Cheng 已提交
1115 1116
  }

H
Hongze Cheng 已提交
1117 1118 1119
  SBlockCol  blockCol = {.cid = 0};
  SBlockCol *pBlockCol = &blockCol;
  int32_t    n = 0;
H
Hongze Cheng 已提交
1120

H
Hongze Cheng 已提交
1121 1122
  for (int32_t iColData = 0; iColData < taosArrayGetSize(pBlockData->aIdx); iColData++) {
    SColData *pColData = tBlockDataGetColDataByIdx(pBlockData, iColData);
H
Hongze Cheng 已提交
1123

H
Hongze Cheng 已提交
1124 1125 1126 1127 1128 1129 1130
    while (pBlockCol && pBlockCol->cid < pColData->cid) {
      if (n < hdr.szBlkCol) {
        n += tGetBlockCol(pReader->aBuf[0] + n, pBlockCol);
      } else {
        ASSERT(n == hdr.szBlkCol);
        pBlockCol = NULL;
      }
H
Hongze Cheng 已提交
1131
    }
H
Hongze Cheng 已提交
1132

H
Hongze Cheng 已提交
1133 1134 1135 1136 1137 1138 1139 1140 1141
    if (pBlockCol == NULL || pBlockCol->cid > pColData->cid) {
      // add a lot of NONE
      for (int32_t iRow = 0; iRow < hdr.nRow; iRow++) {
        code = tColDataAppendValue(pColData, &COL_VAL_NONE(pColData->cid, pColData->type));
        if (code) goto _err;
      }
    } else {
      ASSERT(pBlockCol->type == pColData->type);
      ASSERT(pBlockCol->flag && pBlockCol->flag != HAS_NONE);
H
Hongze Cheng 已提交
1142

H
Hongze Cheng 已提交
1143 1144 1145 1146 1147 1148 1149 1150
      if (pBlockCol->flag == HAS_NULL) {
        // add a lot of NULL
        for (int32_t iRow = 0; iRow < hdr.nRow; iRow++) {
          code = tColDataAppendValue(pColData, &COL_VAL_NULL(pBlockCol->cid, pBlockCol->type));
          if (code) goto _err;
        }
      } else {
        // decode from binary
H
Hongze Cheng 已提交
1151 1152
        int64_t offset = pBlkInfo->offset + pBlkInfo->szKey + hdr.szBlkCol + pBlockCol->offset;
        int32_t size = pBlockCol->szBitmap + pBlockCol->szOffset + pBlockCol->szValue;
H
Hongze Cheng 已提交
1153

H
Hongze Cheng 已提交
1154 1155 1156
        code = tRealloc(&pReader->aBuf[1], size);
        if (code) goto _err;

H
Hongze Cheng 已提交
1157 1158
        code = tsdbReadFile(pFD, offset, pReader->aBuf[1], size);
        if (code) goto _err;
H
Hongze Cheng 已提交
1159 1160 1161 1162 1163

        code = tsdbDecmprColData(pReader->aBuf[1], pBlockCol, hdr.cmprAlg, hdr.nRow, pColData, &pReader->aBuf[2]);
        if (code) goto _err;
      }
    }
H
Hongze Cheng 已提交
1164
  }
H
Hongze Cheng 已提交
1165

H
Hongze Cheng 已提交
1166
_exit:
H
Hongze Cheng 已提交
1167 1168
  return code;

H
Hongze Cheng 已提交
1169
_err:
H
Hongze Cheng 已提交
1170
  tsdbError("vgId:%d tsdb read block data impl failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1171
  return code;
H
Hongze Cheng 已提交
1172
}
H
Hongze Cheng 已提交
1173

H
Hongze Cheng 已提交
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
int32_t tsdbReadDataBlockEx(SDataFReader *pReader, SDataBlk *pDataBlk, SBlockData *pBlockData) {
  int32_t     code = 0;
  SBlockInfo *pBlockInfo = &pDataBlk->aSubBlock[0];

  // alloc
  code = tRealloc(&pReader->aBuf[0], pBlockInfo->szBlock);
  if (code) goto _err;

  // read
  code = tsdbReadFile(pReader->pDataFD, pBlockInfo->offset, pReader->aBuf[0], pBlockInfo->szBlock);
  if (code) goto _err;

  // decmpr
  code = tDecmprBlockData(pReader->aBuf[0], pBlockInfo->szBlock, pBlockData, &pReader->aBuf[1]);
  if (code) goto _err;

  return code;

_err:
  tsdbError("vgId:%d tsdb read data block ex failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1194
  return code;
H
Hongze Cheng 已提交
1195
}
H
Hongze Cheng 已提交
1196

H
Hongze Cheng 已提交
1197 1198
int32_t tsdbReadDataBlock(SDataFReader *pReader, SDataBlk *pDataBlk, SBlockData *pBlockData) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1199

H
Hongze Cheng 已提交
1200
  code = tsdbReadBlockDataImpl(pReader, &pDataBlk->aSubBlock[0], pBlockData, -1);
H
Hongze Cheng 已提交
1201
  if (code) goto _err;
H
Hongze Cheng 已提交
1202

H
Hongze Cheng 已提交
1203 1204 1205
  ASSERT(pDataBlk->nSubBlock == 1);

#if 0
H
Hongze Cheng 已提交
1206 1207 1208
  if (pDataBlk->nSubBlock > 1) {
    SBlockData bData1;
    SBlockData bData2;
H
Hongze Cheng 已提交
1209

H
Hongze Cheng 已提交
1210 1211 1212 1213 1214
    // create
    code = tBlockDataCreate(&bData1);
    if (code) goto _err;
    code = tBlockDataCreate(&bData2);
    if (code) goto _err;
H
Hongze Cheng 已提交
1215

H
Hongze Cheng 已提交
1216 1217 1218
    // init
    tBlockDataInitEx(&bData1, pBlockData);
    tBlockDataInitEx(&bData2, pBlockData);
H
Hongze Cheng 已提交
1219

H
Hongze Cheng 已提交
1220
    for (int32_t iSubBlock = 1; iSubBlock < pDataBlk->nSubBlock; iSubBlock++) {
H
Hongze Cheng 已提交
1221
      code = tsdbReadBlockDataImpl(pReader, &pDataBlk->aSubBlock[iSubBlock], &bData1);
H
Hongze Cheng 已提交
1222 1223 1224 1225 1226
      if (code) {
        tBlockDataDestroy(&bData1, 1);
        tBlockDataDestroy(&bData2, 1);
        goto _err;
      }
H
Hongze Cheng 已提交
1227

H
Hongze Cheng 已提交
1228 1229 1230 1231 1232 1233
      code = tBlockDataCopy(pBlockData, &bData2);
      if (code) {
        tBlockDataDestroy(&bData1, 1);
        tBlockDataDestroy(&bData2, 1);
        goto _err;
      }
H
Hongze Cheng 已提交
1234

H
Hongze Cheng 已提交
1235 1236 1237 1238 1239 1240 1241
      code = tBlockDataMerge(&bData1, &bData2, pBlockData);
      if (code) {
        tBlockDataDestroy(&bData1, 1);
        tBlockDataDestroy(&bData2, 1);
        goto _err;
      }
    }
H
Hongze Cheng 已提交
1242

H
Hongze Cheng 已提交
1243 1244
    tBlockDataDestroy(&bData1, 1);
    tBlockDataDestroy(&bData2, 1);
H
Hongze Cheng 已提交
1245
  }
H
Hongze Cheng 已提交
1246
#endif
H
Hongze Cheng 已提交
1247

H
Hongze Cheng 已提交
1248
  return code;
H
Hongze Cheng 已提交
1249

H
Hongze Cheng 已提交
1250 1251 1252 1253
_err:
  tsdbError("vgId:%d tsdb read data block failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
  return code;
}
H
Hongze Cheng 已提交
1254

H
Hongze Cheng 已提交
1255
int32_t tsdbReadSttBlock(SDataFReader *pReader, int32_t iStt, SSttBlk *pSttBlk, SBlockData *pBlockData) {
H
Hongze Cheng 已提交
1256
  int32_t code = 0;
H
Hongze Cheng 已提交
1257
  int32_t lino = 0;
H
Hongze Cheng 已提交
1258

H
Hongze Cheng 已提交
1259
  code = tsdbReadBlockDataImpl(pReader, &pSttBlk->bInfo, pBlockData, iStt);
H
Hongze Cheng 已提交
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at %d since %s", TD_VID(pReader->pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
  return code;
}

int32_t tsdbReadSttBlockEx(SDataFReader *pReader, int32_t iStt, SSttBlk *pSttBlk, SBlockData *pBlockData) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1272

H
Hongze Cheng 已提交
1273
  // alloc
H
Hongze Cheng 已提交
1274
  code = tRealloc(&pReader->aBuf[0], pSttBlk->bInfo.szBlock);
H
Hongze Cheng 已提交
1275
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1276

H
Hongze Cheng 已提交
1277
  // read
H
Hongze Cheng 已提交
1278
  code = tsdbReadFile(pReader->aSttFD[iStt], pSttBlk->bInfo.offset, pReader->aBuf[0], pSttBlk->bInfo.szBlock);
H
Hongze Cheng 已提交
1279
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1280

H
Hongze Cheng 已提交
1281
  // decmpr
H
Hongze Cheng 已提交
1282
  code = tDecmprBlockData(pReader->aBuf[0], pSttBlk->bInfo.szBlock, pBlockData, &pReader->aBuf[1]);
H
Hongze Cheng 已提交
1283
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1284

H
Hongze Cheng 已提交
1285 1286 1287 1288
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at %d since %s", TD_VID(pReader->pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
H
Hongze Cheng 已提交
1289 1290 1291 1292 1293 1294
  return code;
}

// SDelFWriter ====================================================
int32_t tsdbDelFWriterOpen(SDelFWriter **ppWriter, SDelFile *pFile, STsdb *pTsdb) {
  int32_t      code = 0;
H
Hongze Cheng 已提交
1295
  int32_t      lino = 0;
H
Hongze Cheng 已提交
1296
  char         fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
1297
  uint8_t      hdr[TSDB_FHDR_SIZE] = {0};
H
Hongze Cheng 已提交
1298
  SDelFWriter *pDelFWriter = NULL;
H
Hongze Cheng 已提交
1299 1300 1301 1302 1303 1304
  int64_t      n;

  // alloc
  pDelFWriter = (SDelFWriter *)taosMemoryCalloc(1, sizeof(*pDelFWriter));
  if (pDelFWriter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1305
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1306 1307 1308 1309 1310
  }
  pDelFWriter->pTsdb = pTsdb;
  pDelFWriter->fDel = *pFile;

  tsdbDelFileName(pTsdb, pFile, fname);
H
Hongze Cheng 已提交
1311 1312
  code = tsdbOpenFile(fname, pTsdb->pVnode->config.tsdbPageSize, TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE,
                      &pDelFWriter->pWriteH);
H
Hongze Cheng 已提交
1313
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1314 1315

  // update header
H
Hongze Cheng 已提交
1316
  code = tsdbWriteFile(pDelFWriter->pWriteH, 0, hdr, TSDB_FHDR_SIZE);
H
Hongze Cheng 已提交
1317
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1318 1319 1320 1321 1322 1323

  pDelFWriter->fDel.size = TSDB_FHDR_SIZE;
  pDelFWriter->fDel.offset = 0;

  *ppWriter = pDelFWriter;

H
Hongze Cheng 已提交
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
_exit:
  if (code) {
    if (pDelFWriter) {
      taosMemoryFree(pDelFWriter);
      tsdbCloseFile(&pDelFWriter->pWriteH);
    }
    *ppWriter = NULL;
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(errno));
  } else {
    *ppWriter = pDelFWriter;
  }
H
Hongze Cheng 已提交
1335 1336 1337 1338 1339 1340 1341 1342 1343
  return code;
}

int32_t tsdbDelFWriterClose(SDelFWriter **ppWriter, int8_t sync) {
  int32_t      code = 0;
  SDelFWriter *pWriter = *ppWriter;
  STsdb       *pTsdb = pWriter->pTsdb;

  // sync
H
Hongze Cheng 已提交
1344 1345 1346
  if (sync) {
    code = tsdbFsyncFile(pWriter->pWriteH);
    if (code) goto _err;
H
Hongze Cheng 已提交
1347 1348 1349
  }

  // close
H
Hongze Cheng 已提交
1350
  tsdbCloseFile(&pWriter->pWriteH);
H
Hongze Cheng 已提交
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370

  for (int32_t iBuf = 0; iBuf < sizeof(pWriter->aBuf) / sizeof(uint8_t *); iBuf++) {
    tFree(pWriter->aBuf[iBuf]);
  }
  taosMemoryFree(pWriter);

  *ppWriter = NULL;
  return code;

_err:
  tsdbError("vgId:%d, failed to close del file writer since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
  return code;
}

int32_t tsdbWriteDelData(SDelFWriter *pWriter, SArray *aDelData, SDelIdx *pDelIdx) {
  int32_t code = 0;
  int64_t size;
  int64_t n;

  // prepare
H
Hongze Cheng 已提交
1371
  size = 0;
H
Hongze Cheng 已提交
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
  for (int32_t iDelData = 0; iDelData < taosArrayGetSize(aDelData); iDelData++) {
    size += tPutDelData(NULL, taosArrayGet(aDelData, iDelData));
  }

  // alloc
  code = tRealloc(&pWriter->aBuf[0], size);
  if (code) goto _err;

  // build
  n = 0;
  for (int32_t iDelData = 0; iDelData < taosArrayGetSize(aDelData); iDelData++) {
    n += tPutDelData(pWriter->aBuf[0] + n, taosArrayGet(aDelData, iDelData));
  }
H
Hongze Cheng 已提交
1385
  ASSERT(n == size);
H
Hongze Cheng 已提交
1386 1387

  // write
H
Hongze Cheng 已提交
1388 1389
  code = tsdbWriteFile(pWriter->pWriteH, pWriter->fDel.size, pWriter->aBuf[0], size);
  if (code) goto _err;
H
Hongze Cheng 已提交
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409

  // update
  pDelIdx->offset = pWriter->fDel.size;
  pDelIdx->size = size;
  pWriter->fDel.size += size;

  return code;

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

int32_t tsdbWriteDelIdx(SDelFWriter *pWriter, SArray *aDelIdx) {
  int32_t  code = 0;
  int64_t  size;
  int64_t  n;
  SDelIdx *pDelIdx;

  // prepare
H
Hongze Cheng 已提交
1410
  size = 0;
H
Hongze Cheng 已提交
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
  for (int32_t iDelIdx = 0; iDelIdx < taosArrayGetSize(aDelIdx); iDelIdx++) {
    size += tPutDelIdx(NULL, taosArrayGet(aDelIdx, iDelIdx));
  }

  // alloc
  code = tRealloc(&pWriter->aBuf[0], size);
  if (code) goto _err;

  // build
  n = 0;
  for (int32_t iDelIdx = 0; iDelIdx < taosArrayGetSize(aDelIdx); iDelIdx++) {
    n += tPutDelIdx(pWriter->aBuf[0] + n, taosArrayGet(aDelIdx, iDelIdx));
  }
H
Hongze Cheng 已提交
1424
  ASSERT(n == size);
H
Hongze Cheng 已提交
1425 1426

  // write
H
Hongze Cheng 已提交
1427 1428
  code = tsdbWriteFile(pWriter->pWriteH, pWriter->fDel.size, pWriter->aBuf[0], size);
  if (code) goto _err;
H
Hongze Cheng 已提交
1429

H
Hongze Cheng 已提交
1430 1431 1432
  // update
  pWriter->fDel.offset = pWriter->fDel.size;
  pWriter->fDel.size += size;
H
Hongze Cheng 已提交
1433

H
Hongze Cheng 已提交
1434
  return code;
H
Hongze Cheng 已提交
1435

H
Hongze Cheng 已提交
1436 1437 1438 1439 1440 1441 1442
_err:
  tsdbError("vgId:%d, write del idx failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
  return code;
}

int32_t tsdbUpdateDelFileHdr(SDelFWriter *pWriter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1443
  char    hdr[TSDB_FHDR_SIZE] = {0};
H
Hongze Cheng 已提交
1444 1445 1446 1447 1448
  int64_t size = TSDB_FHDR_SIZE;
  int64_t n;

  // build
  tPutDelFile(hdr, &pWriter->fDel);
H
Hongze Cheng 已提交
1449

H
Hongze Cheng 已提交
1450
  // write
H
Hongze Cheng 已提交
1451 1452
  code = tsdbWriteFile(pWriter->pWriteH, 0, hdr, size);
  if (code) goto _err;
H
Hongze Cheng 已提交
1453 1454 1455 1456

  return code;

_err:
H
Hongze Cheng 已提交
1457
  tsdbError("vgId:%d, update del file hdr failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1458
  return code;
H
Hongze Cheng 已提交
1459
}
H
Hongze Cheng 已提交
1460 1461
// SDelFReader ====================================================
struct SDelFReader {
H
Hongze Cheng 已提交
1462 1463 1464
  STsdb   *pTsdb;
  SDelFile fDel;
  STsdbFD *pReadH;
H
Hongze Cheng 已提交
1465 1466
  uint8_t *aBuf[1];
};
H
Hongze Cheng 已提交
1467

H
Hongze Cheng 已提交
1468 1469
int32_t tsdbDelFReaderOpen(SDelFReader **ppReader, SDelFile *pFile, STsdb *pTsdb) {
  int32_t      code = 0;
H
Hongze Cheng 已提交
1470
  int32_t      lino = 0;
H
Hongze Cheng 已提交
1471
  char         fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
1472
  SDelFReader *pDelFReader = NULL;
H
Hongze Cheng 已提交
1473

H
Hongze Cheng 已提交
1474 1475 1476
  // alloc
  pDelFReader = (SDelFReader *)taosMemoryCalloc(1, sizeof(*pDelFReader));
  if (pDelFReader == NULL) {
H
Hongze Cheng 已提交
1477
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1478
    goto _exit;
H
Hongze Cheng 已提交
1479 1480 1481 1482 1483 1484 1485
  }

  // open impl
  pDelFReader->pTsdb = pTsdb;
  pDelFReader->fDel = *pFile;

  tsdbDelFileName(pTsdb, pFile, fname);
H
Hongze Cheng 已提交
1486
  code = tsdbOpenFile(fname, pTsdb->pVnode->config.tsdbPageSize, TD_FILE_READ, &pDelFReader->pReadH);
H
Hongze Cheng 已提交
1487 1488 1489 1490
  if (code) {
    taosMemoryFree(pDelFReader);
    goto _exit;
  }
H
Hongze Cheng 已提交
1491

H
Hongze Cheng 已提交
1492 1493 1494 1495 1496 1497 1498
_exit:
  if (code) {
    *ppReader = NULL;
    tsdbError("vgId:%d %s failed at %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
  } else {
    *ppReader = pDelFReader;
  }
H
Hongze Cheng 已提交
1499
  return code;
H
Hongze Cheng 已提交
1500 1501
}

H
Hongze Cheng 已提交
1502 1503 1504
int32_t tsdbDelFReaderClose(SDelFReader **ppReader) {
  int32_t      code = 0;
  SDelFReader *pReader = *ppReader;
H
Hongze Cheng 已提交
1505

H
Hongze Cheng 已提交
1506
  if (pReader) {
H
Hongze Cheng 已提交
1507
    tsdbCloseFile(&pReader->pReadH);
H
Hongze Cheng 已提交
1508 1509 1510 1511
    for (int32_t iBuf = 0; iBuf < sizeof(pReader->aBuf) / sizeof(uint8_t *); iBuf++) {
      tFree(pReader->aBuf[iBuf]);
    }
    taosMemoryFree(pReader);
H
Hongze Cheng 已提交
1512
  }
H
Hongze Cheng 已提交
1513
  *ppReader = NULL;
H
Hongze Cheng 已提交
1514 1515 1516 1517 1518

_exit:
  return code;
}

H
Hongze Cheng 已提交
1519
int32_t tsdbReadDelData(SDelFReader *pReader, SDelIdx *pDelIdx, SArray *aDelData) {
H
Hongze Cheng 已提交
1520
  int32_t code = 0;
H
Hongze Cheng 已提交
1521 1522 1523
  int64_t offset = pDelIdx->offset;
  int64_t size = pDelIdx->size;
  int64_t n;
H
Hongze Cheng 已提交
1524

H
Hongze Cheng 已提交
1525
  taosArrayClear(aDelData);
H
Hongze Cheng 已提交
1526

H
Hongze Cheng 已提交
1527 1528 1529
  // alloc
  code = tRealloc(&pReader->aBuf[0], size);
  if (code) goto _err;
H
Hongze Cheng 已提交
1530

H
Hongze Cheng 已提交
1531
  // read
H
Hongze Cheng 已提交
1532 1533
  code = tsdbReadFile(pReader->pReadH, offset, pReader->aBuf[0], size);
  if (code) goto _err;
H
Hongze Cheng 已提交
1534 1535 1536

  // // decode
  n = 0;
H
Hongze Cheng 已提交
1537
  while (n < size) {
H
Hongze Cheng 已提交
1538 1539 1540 1541 1542 1543
    SDelData delData;
    n += tGetDelData(pReader->aBuf[0] + n, &delData);

    if (taosArrayPush(aDelData, &delData) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
H
Hongze Cheng 已提交
1544 1545
    }
  }
H
Hongze Cheng 已提交
1546
  ASSERT(n == size);
H
Hongze Cheng 已提交
1547 1548 1549 1550 1551

  return code;

_err:
  tsdbError("vgId:%d, read del data failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1552 1553 1554
  return code;
}

H
Hongze Cheng 已提交
1555
int32_t tsdbReadDelIdx(SDelFReader *pReader, SArray *aDelIdx) {
H
Hongze Cheng 已提交
1556
  int32_t code = 0;
H
Hongze Cheng 已提交
1557 1558 1559
  int32_t n;
  int64_t offset = pReader->fDel.offset;
  int64_t size = pReader->fDel.size - offset;
H
Hongze Cheng 已提交
1560

H
Hongze Cheng 已提交
1561 1562 1563 1564 1565 1566 1567
  taosArrayClear(aDelIdx);

  // alloc
  code = tRealloc(&pReader->aBuf[0], size);
  if (code) goto _err;

  // read
H
Hongze Cheng 已提交
1568 1569
  code = tsdbReadFile(pReader->pReadH, offset, pReader->aBuf[0], size);
  if (code) goto _err;
H
Hongze Cheng 已提交
1570

H
Hongze Cheng 已提交
1571 1572
  // decode
  n = 0;
H
Hongze Cheng 已提交
1573
  while (n < size) {
H
Hongze Cheng 已提交
1574
    SDelIdx delIdx;
H
Hongze Cheng 已提交
1575

H
Hongze Cheng 已提交
1576
    n += tGetDelIdx(pReader->aBuf[0] + n, &delIdx);
H
Hongze Cheng 已提交
1577

H
Hongze Cheng 已提交
1578 1579 1580 1581
    if (taosArrayPush(aDelIdx, &delIdx) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
1582 1583
  }

H
Hongze Cheng 已提交
1584
  ASSERT(n == size);
H
Hongze Cheng 已提交
1585

H
Hongze Cheng 已提交
1586
  return code;
H
Hongze Cheng 已提交
1587

H
Hongze Cheng 已提交
1588 1589
_err:
  tsdbError("vgId:%d, read del idx failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1590
  return code;
H
Hongze Cheng 已提交
1591
}