tsdbReaderWriter.c 43.3 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
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
  }
50 51 52

  // not check file size when reading data files.
  if (flag != TD_FILE_READ) {
53 54 55 56 57 58 59
    if (taosStatFile(path, &pFD->szFile, NULL) < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      taosMemoryFree(pFD->pBuf);
      taosCloseFile(&pFD->pFD);
      taosMemoryFree(pFD);
      goto _exit;
    }
60

61 62
    ASSERT(pFD->szFile % szPage == 0);
    pFD->szFile = pFD->szFile / szPage;
H
Hongze Cheng 已提交
63
  }
64

H
Hongze Cheng 已提交
65
  *ppFD = pFD;
H
Hongze Cheng 已提交
66 67 68

_exit:
  return code;
H
Hongze Cheng 已提交
69
}
H
Hongze Cheng 已提交
70

H
Hongze Cheng 已提交
71
void tsdbCloseFile(STsdbFD **ppFD) {
H
Hongze Cheng 已提交
72
  STsdbFD *pFD = *ppFD;
H
Hongze Cheng 已提交
73 74 75 76 77 78
  if (pFD) {
    taosMemoryFree(pFD->pBuf);
    taosCloseFile(&pFD->pFD);
    taosMemoryFree(pFD);
    *ppFD = NULL;
  }
H
Hongze Cheng 已提交
79 80
}

H
Hongze Cheng 已提交
81
static int32_t tsdbWriteFilePage(STsdbFD *pFD) {
H
Hongze Cheng 已提交
82
  int32_t code = 0;
H
Hongze Cheng 已提交
83

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

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

H
Hongze Cheng 已提交
93 94 95 96 97
    n = taosWriteFile(pFD->pFD, pFD->pBuf, pFD->szPage);
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _exit;
    }
H
Hongze Cheng 已提交
98

H
Hongze Cheng 已提交
99
    if (pFD->szFile < pFD->pgno) {
H
Hongze Cheng 已提交
100
      pFD->szFile = pFD->pgno;
H
Hongze Cheng 已提交
101
    }
H
Hongze Cheng 已提交
102
  }
H
Hongze Cheng 已提交
103
  pFD->pgno = 0;
H
Hongze Cheng 已提交
104 105

_exit:
H
Hongze Cheng 已提交
106 107 108
  return code;
}

H
Hongze Cheng 已提交
109
static int32_t tsdbReadFilePage(STsdbFD *pFD, int64_t pgno) {
H
Hongze Cheng 已提交
110
  int32_t code = 0;
H
more  
Hongze Cheng 已提交
111

112
  // ASSERT(pgno <= pFD->szFile);
H
Hongze Cheng 已提交
113

H
Hongze Cheng 已提交
114 115 116
  // seek
  int64_t offset = PAGE_OFFSET(pgno, pFD->szPage);
  int64_t n = taosLSeekFile(pFD->pFD, offset, SEEK_SET);
H
Hongze Cheng 已提交
117
  if (n < 0) {
H
Hongze Cheng 已提交
118
    code = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
119
    goto _exit;
H
Hongze Cheng 已提交
120 121
  }

H
Hongze Cheng 已提交
122
  // read
H
Hongze Cheng 已提交
123
  n = taosReadFile(pFD->pFD, pFD->pBuf, pFD->szPage);
H
Hongze Cheng 已提交
124
  if (n < 0) {
H
Hongze Cheng 已提交
125
    code = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
126 127
    goto _exit;
  } else if (n < 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
  // check
133
  if (pgno > 1 && !taosCheckChecksumWhole(pFD->pBuf, pFD->szPage)) {
H
Hongze Cheng 已提交
134
    code = TSDB_CODE_FILE_CORRUPTED;
H
Hongze Cheng 已提交
135
    goto _exit;
H
Hongze Cheng 已提交
136 137
  }

H
Hongze Cheng 已提交
138
  pFD->pgno = pgno;
H
Hongze Cheng 已提交
139

H
Hongze Cheng 已提交
140 141 142
_exit:
  return code;
}
H
Hongze Cheng 已提交
143

H
Hongze Cheng 已提交
144
int32_t tsdbWriteFile(STsdbFD *pFD, int64_t offset, const uint8_t *pBuf, int64_t size) {
H
Hongze Cheng 已提交
145 146 147 148 149 150 151 152 153 154 155
  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 已提交
156
      if (pgno <= pFD->szFile) {
H
Hongze Cheng 已提交
157 158 159 160 161 162 163
        code = tsdbReadFilePage(pFD, pgno);
        if (code) goto _exit;
      } else {
        pFD->pgno = pgno;
      }
    }

H
Hongze Cheng 已提交
164 165
    int64_t nWrite = TMIN(PAGE_CONTENT_SIZE(pFD->szPage) - bOffset, size - n);
    memcpy(pFD->pBuf + bOffset, pBuf + n, nWrite);
H
Hongze Cheng 已提交
166 167 168

    pgno++;
    bOffset = 0;
H
Hongze Cheng 已提交
169
    n += nWrite;
H
Hongze Cheng 已提交
170 171 172 173 174 175
  } while (n < size);

_exit:
  return code;
}

H
Hongze Cheng 已提交
176
int32_t tsdbReadFile(STsdbFD *pFD, int64_t offset, uint8_t *pBuf, int64_t size) {
H
Hongze Cheng 已提交
177
  int32_t code = 0;
H
Hongze Cheng 已提交
178
  int64_t n = 0;
H
Hongze Cheng 已提交
179 180
  int64_t fOffset = LOGIC_TO_FILE_OFFSET(offset, pFD->szPage);
  int64_t pgno = OFFSET_PGNO(fOffset, pFD->szPage);
H
Hongze Cheng 已提交
181
  int32_t szPgCont = PAGE_CONTENT_SIZE(pFD->szPage);
H
fix bug  
Hongze Cheng 已提交
182
  int64_t bOffset = fOffset % pFD->szPage;
H
Hongze Cheng 已提交
183

184
  // ASSERT(pgno && pgno <= pFD->szFile);
185
  ASSERT(bOffset < szPgCont);
H
Hongze Cheng 已提交
186

H
Hongze Cheng 已提交
187
  while (n < size) {
H
fix bug  
Hongze Cheng 已提交
188 189 190 191
    if (pFD->pgno != pgno) {
      code = tsdbReadFilePage(pFD, pgno);
      if (code) goto _exit;
    }
H
Hongze Cheng 已提交
192

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

H
Hongze Cheng 已提交
196
    n += nRead;
H
Hongze Cheng 已提交
197
    pgno++;
H
fix bug  
Hongze Cheng 已提交
198
    bOffset = 0;
H
Hongze Cheng 已提交
199 200 201
  }

_exit:
H
Hongze Cheng 已提交
202
  return code;
H
Hongze Cheng 已提交
203 204
}

H
Hongze Cheng 已提交
205
int32_t tsdbFsyncFile(STsdbFD *pFD) {
H
Hongze Cheng 已提交
206
  int32_t code = 0;
H
Hongze Cheng 已提交
207 208 209 210 211 212 213 214 215 216

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

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

_exit:
H
Hongze Cheng 已提交
217 218 219
  return code;
}

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

  // alloc
H
Hongze Cheng 已提交
231 232
  pWriter = taosMemoryCalloc(1, sizeof(*pWriter));
  if (pWriter == NULL) {
H
Hongze Cheng 已提交
233 234 235
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
236
  pWriter->pTsdb = pTsdb;
H
Hongze Cheng 已提交
237 238 239 240 241
  pWriter->wSet = (SDFileSet){.diskId = pSet->diskId,
                              .fid = pSet->fid,
                              .pHeadF = &pWriter->fHead,
                              .pDataF = &pWriter->fData,
                              .pSmaF = &pWriter->fSma,
H
Hongze Cheng 已提交
242
                              .nSttF = pSet->nSttF};
H
Hongze Cheng 已提交
243 244 245
  pWriter->fHead = *pSet->pHeadF;
  pWriter->fData = *pSet->pDataF;
  pWriter->fSma = *pSet->pSmaF;
H
Hongze Cheng 已提交
246 247 248
  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 已提交
249
  }
H
Hongze Cheng 已提交
250 251

  // head
H
Hongze Cheng 已提交
252
  flag = TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC;
H
Hongze Cheng 已提交
253
  tsdbHeadFileName(pTsdb, pWriter->wSet.diskId, pWriter->wSet.fid, &pWriter->fHead, fname);
H
Hongze Cheng 已提交
254 255
  code = tsdbOpenFile(fname, szPage, flag, &pWriter->pHeadFD);
  if (code) goto _err;
H
Hongze Cheng 已提交
256

H
Hongze Cheng 已提交
257
  code = tsdbWriteFile(pWriter->pHeadFD, 0, hdr, TSDB_FHDR_SIZE);
H
Hongze Cheng 已提交
258
  if (code) goto _err;
H
Hongze Cheng 已提交
259 260
  pWriter->fHead.size += TSDB_FHDR_SIZE;

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

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

H
Hongze Cheng 已提交
289
    pWriter->fSma.size += TSDB_FHDR_SIZE;
H
Hongze Cheng 已提交
290 291
  }

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

  *ppWriter = pWriter;
H
Hongze Cheng 已提交
303 304 305
  return code;

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

H
Hongze Cheng 已提交
311
int32_t tsdbDataFWriterClose(SDataFWriter **ppWriter, int8_t sync) {
H
Hongze Cheng 已提交
312
  int32_t code = 0;
H
Hongze Cheng 已提交
313
  STsdb  *pTsdb = NULL;
H
Hongze Cheng 已提交
314

H
Hongze Cheng 已提交
315 316 317 318
  if (*ppWriter == NULL) goto _exit;

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

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

H
Hongze Cheng 已提交
325 326
    code = tsdbFsyncFile((*ppWriter)->pSmaFD);
    if (code) goto _err;
H
Hongze Cheng 已提交
327

H
Hongze Cheng 已提交
328
    code = tsdbFsyncFile((*ppWriter)->pSttFD);
H
Hongze Cheng 已提交
329
    if (code) goto _err;
H
Hongze Cheng 已提交
330 331
  }

H
Hongze Cheng 已提交
332 333 334
  tsdbCloseFile(&(*ppWriter)->pHeadFD);
  tsdbCloseFile(&(*ppWriter)->pDataFD);
  tsdbCloseFile(&(*ppWriter)->pSmaFD);
H
Hongze Cheng 已提交
335
  tsdbCloseFile(&(*ppWriter)->pSttFD);
H
Hongze Cheng 已提交
336

H
Hongze Cheng 已提交
337 338
  for (int32_t iBuf = 0; iBuf < sizeof((*ppWriter)->aBuf) / sizeof(uint8_t *); iBuf++) {
    tFree((*ppWriter)->aBuf[iBuf]);
H
Hongze Cheng 已提交
339
  }
H
Hongze Cheng 已提交
340
  taosMemoryFree(*ppWriter);
H
Hongze Cheng 已提交
341
_exit:
H
Hongze Cheng 已提交
342
  *ppWriter = NULL;
H
Hongze Cheng 已提交
343 344 345
  return code;

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

H
Hongze Cheng 已提交
350 351 352 353
int32_t tsdbUpdateDFileSetHeader(SDataFWriter *pWriter) {
  int32_t code = 0;
  int64_t n;
  char    hdr[TSDB_FHDR_SIZE];
H
Hongze Cheng 已提交
354

H
Hongze Cheng 已提交
355 356 357
  // head ==============
  memset(hdr, 0, TSDB_FHDR_SIZE);
  tPutHeadFile(hdr, &pWriter->fHead);
H
Hongze Cheng 已提交
358
  code = tsdbWriteFile(pWriter->pHeadFD, 0, hdr, TSDB_FHDR_SIZE);
H
Hongze Cheng 已提交
359
  if (code) goto _err;
H
Hongze Cheng 已提交
360 361 362 363

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

H
Hongze Cheng 已提交
367 368 369
  // sma ==============
  memset(hdr, 0, TSDB_FHDR_SIZE);
  tPutSmaFile(hdr, &pWriter->fSma);
H
Hongze Cheng 已提交
370
  code = tsdbWriteFile(pWriter->pSmaFD, 0, hdr, TSDB_FHDR_SIZE);
H
Hongze Cheng 已提交
371
  if (code) goto _err;
H
Hongze Cheng 已提交
372

H
Hongze Cheng 已提交
373
  // stt ==============
H
Hongze Cheng 已提交
374
  memset(hdr, 0, TSDB_FHDR_SIZE);
H
Hongze Cheng 已提交
375 376
  tPutSttFile(hdr, &pWriter->fStt[pWriter->wSet.nSttF - 1]);
  code = tsdbWriteFile(pWriter->pSttFD, 0, hdr, TSDB_FHDR_SIZE);
H
Hongze Cheng 已提交
377
  if (code) goto _err;
H
Hongze Cheng 已提交
378 379 380 381

  return code;

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

H
Hongze Cheng 已提交
386 387 388
int32_t tsdbWriteBlockIdx(SDataFWriter *pWriter, SArray *aBlockIdx) {
  int32_t    code = 0;
  SHeadFile *pHeadFile = &pWriter->fHead;
H
Hongze Cheng 已提交
389
  int64_t    size;
H
Hongze Cheng 已提交
390
  int64_t    n;
H
Hongze Cheng 已提交
391

H
Hongze Cheng 已提交
392 393 394
  // check
  if (taosArrayGetSize(aBlockIdx) == 0) {
    pHeadFile->offset = pHeadFile->size;
H
Hongze Cheng 已提交
395 396
    goto _exit;
  }
H
Hongze Cheng 已提交
397

H
Hongze Cheng 已提交
398
  // prepare
H
Hongze Cheng 已提交
399
  size = 0;
H
Hongze Cheng 已提交
400 401 402 403
  for (int32_t iBlockIdx = 0; iBlockIdx < taosArrayGetSize(aBlockIdx); iBlockIdx++) {
    size += tPutBlockIdx(NULL, taosArrayGet(aBlockIdx, iBlockIdx));
  }

H
Hongze Cheng 已提交
404
  // alloc
H
Hongze Cheng 已提交
405
  code = tRealloc(&pWriter->aBuf[0], size);
H
Hongze Cheng 已提交
406 407
  if (code) goto _err;

H
Hongze Cheng 已提交
408 409 410 411
  // build
  n = 0;
  for (int32_t iBlockIdx = 0; iBlockIdx < taosArrayGetSize(aBlockIdx); iBlockIdx++) {
    n += tPutBlockIdx(pWriter->aBuf[0] + n, taosArrayGet(aBlockIdx, iBlockIdx));
H
Hongze Cheng 已提交
412
  }
413
  ASSERT(n == size);
H
Hongze Cheng 已提交
414 415

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

H
Hongze Cheng 已提交
419 420 421
  // update
  pHeadFile->offset = pHeadFile->size;
  pHeadFile->size += size;
H
Hongze Cheng 已提交
422

H
Hongze Cheng 已提交
423
_exit:
S
Shengliang Guan 已提交
424
  // tsdbTrace("vgId:%d, write block idx, offset:%" PRId64 " size:%" PRId64 " nBlockIdx:%d",
H
Hongze Cheng 已提交
425 426
  // TD_VID(pWriter->pTsdb->pVnode),
  //           pHeadFile->offset, size, taosArrayGetSize(aBlockIdx));
H
Hongze Cheng 已提交
427 428 429
  return code;

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

H
Hongze Cheng 已提交
434
int32_t tsdbWriteDataBlk(SDataFWriter *pWriter, SMapData *mDataBlk, SBlockIdx *pBlockIdx) {
H
Hongze Cheng 已提交
435 436 437 438 439
  int32_t    code = 0;
  SHeadFile *pHeadFile = &pWriter->fHead;
  int64_t    size;
  int64_t    n;

440
  ASSERT(mDataBlk->nItem > 0);
H
Hongze Cheng 已提交
441

H
Hongze Cheng 已提交
442
  // alloc
H
Hongze Cheng 已提交
443
  size = tPutMapData(NULL, mDataBlk);
H
Hongze Cheng 已提交
444
  code = tRealloc(&pWriter->aBuf[0], size);
H
Hongze Cheng 已提交
445 446
  if (code) goto _err;

H
Hongze Cheng 已提交
447
  // build
H
Hongze Cheng 已提交
448
  n = tPutMapData(pWriter->aBuf[0], mDataBlk);
H
Hongze Cheng 已提交
449 450

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

H
Hongze Cheng 已提交
454 455 456 457
  // update
  pBlockIdx->offset = pHeadFile->size;
  pBlockIdx->size = size;
  pHeadFile->size += size;
H
Hongze Cheng 已提交
458

H
Hongze Cheng 已提交
459
  tsdbTrace("vgId:%d, write block, file ID:%d commit ID:%" PRId64 " suid:%" PRId64 " uid:%" PRId64 " offset:%" PRId64
H
Hongze Cheng 已提交
460 461
            " size:%" PRId64 " nItem:%d",
            TD_VID(pWriter->pTsdb->pVnode), pWriter->wSet.fid, pHeadFile->commitID, pBlockIdx->suid, pBlockIdx->uid,
H
Hongze Cheng 已提交
462
            pBlockIdx->offset, pBlockIdx->size, mDataBlk->nItem);
H
Hongze Cheng 已提交
463 464 465
  return code;

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

H
Hongze Cheng 已提交
470
int32_t tsdbWriteSttBlk(SDataFWriter *pWriter, SArray *aSttBlk) {
H
Hongze Cheng 已提交
471
  int32_t   code = 0;
H
Hongze Cheng 已提交
472
  SSttFile *pSttFile = &pWriter->fStt[pWriter->wSet.nSttF - 1];
H
Hongze Cheng 已提交
473
  int64_t   size = 0;
H
Hongze Cheng 已提交
474
  int64_t   n;
H
Hongze Cheng 已提交
475

H
Hongze Cheng 已提交
476
  // check
H
Hongze Cheng 已提交
477 478
  if (taosArrayGetSize(aSttBlk) == 0) {
    pSttFile->offset = pSttFile->size;
H
Hongze Cheng 已提交
479 480
    goto _exit;
  }
H
Hongze Cheng 已提交
481

H
Hongze Cheng 已提交
482
  // size
H
Hongze Cheng 已提交
483
  size = 0;
H
Hongze Cheng 已提交
484 485
  for (int32_t iBlockL = 0; iBlockL < taosArrayGetSize(aSttBlk); iBlockL++) {
    size += tPutSttBlk(NULL, taosArrayGet(aSttBlk, iBlockL));
H
Hongze Cheng 已提交
486
  }
H
Hongze Cheng 已提交
487 488

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

H
Hongze Cheng 已提交
492 493
  // encode
  n = 0;
H
Hongze Cheng 已提交
494 495
  for (int32_t iBlockL = 0; iBlockL < taosArrayGetSize(aSttBlk); iBlockL++) {
    n += tPutSttBlk(pWriter->aBuf[0] + n, taosArrayGet(aSttBlk, iBlockL));
H
Hongze Cheng 已提交
496
  }
H
Hongze Cheng 已提交
497 498

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

H
Hongze Cheng 已提交
502
  // update
H
Hongze Cheng 已提交
503 504
  pSttFile->offset = pSttFile->size;
  pSttFile->size += size;
H
Hongze Cheng 已提交
505

H
Hongze Cheng 已提交
506
_exit:
S
Shengliang Guan 已提交
507
  tsdbTrace("vgId:%d, tsdb write stt block, loffset:%" PRId64 " size:%" PRId64, TD_VID(pWriter->pTsdb->pVnode),
H
Hongze Cheng 已提交
508
            pSttFile->offset, size);
H
Hongze Cheng 已提交
509 510 511
  return code;

_err:
S
Shengliang Guan 已提交
512
  tsdbError("vgId:%d, tsdb write blockl failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
513 514 515
  return code;
}

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

H
Hongze Cheng 已提交
519 520
  pSmaInfo->offset = 0;
  pSmaInfo->size = 0;
H
Hongze Cheng 已提交
521

H
Hongze Cheng 已提交
522
  // encode
H
Hongze Cheng 已提交
523
  for (int32_t iColData = 0; iColData < pBlockData->nColData; iColData++) {
H
Hongze Cheng 已提交
524
    SColData *pColData = tBlockDataGetColDataByIdx(pBlockData, iColData);
H
Hongze Cheng 已提交
525

H
Hongze Cheng 已提交
526
    if ((!pColData->smaOn) || ((pColData->flag & HAS_VALUE) == 0)) continue;
H
Hongze Cheng 已提交
527

H
Hongze Cheng 已提交
528 529
    SColumnDataAgg sma = {.colId = pColData->cid};
    tColDataCalcSMA[pColData->type](pColData, &sma.sum, &sma.max, &sma.min, &sma.numOfNull);
H
Hongze Cheng 已提交
530

H
Hongze Cheng 已提交
531 532 533 534
    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 已提交
535

H
Hongze Cheng 已提交
536 537
  // write
  if (pSmaInfo->size) {
H
Hongze Cheng 已提交
538
    code = tsdbWriteFile(pWriter->pSmaFD, pWriter->fSma.size, pWriter->aBuf[0], pSmaInfo->size);
H
Hongze Cheng 已提交
539
    if (code) goto _err;
H
Hongze Cheng 已提交
540

H
Hongze Cheng 已提交
541
    pSmaInfo->offset = pWriter->fSma.size;
H
Hongze Cheng 已提交
542
    pWriter->fSma.size += pSmaInfo->size;
H
Hongze Cheng 已提交
543 544 545 546 547
  }

  return code;

_err:
S
Shengliang Guan 已提交
548
  tsdbError("vgId:%d, tsdb write block sma failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
549 550 551
  return code;
}

H
Hongze Cheng 已提交
552 553
int32_t tsdbWriteBlockData(SDataFWriter *pWriter, SBlockData *pBlockData, SBlockInfo *pBlkInfo, SSmaInfo *pSmaInfo,
                           int8_t cmprAlg, int8_t toLast) {
H
Hongze Cheng 已提交
554 555
  int32_t code = 0;

556
  ASSERT(pBlockData->nRow > 0);
H
Hongze Cheng 已提交
557

H
Hongze Cheng 已提交
558
  if (toLast) {
H
Hongze Cheng 已提交
559
    pBlkInfo->offset = pWriter->fStt[pWriter->wSet.nSttF - 1].size;
H
Hongze Cheng 已提交
560 561 562
  } else {
    pBlkInfo->offset = pWriter->fData.size;
  }
H
Hongze Cheng 已提交
563 564
  pBlkInfo->szBlock = 0;
  pBlkInfo->szKey = 0;
H
Hongze Cheng 已提交
565

H
Hongze Cheng 已提交
566 567 568
  int32_t aBufN[4] = {0};
  code = tCmprBlockData(pBlockData, cmprAlg, NULL, NULL, pWriter->aBuf, aBufN);
  if (code) goto _err;
H
Hongze Cheng 已提交
569

H
Hongze Cheng 已提交
570
  // write =================
H
Hongze Cheng 已提交
571
  STsdbFD *pFD = toLast ? pWriter->pSttFD : pWriter->pDataFD;
H
Hongze Cheng 已提交
572

H
Hongze Cheng 已提交
573 574
  pBlkInfo->szKey = aBufN[3] + aBufN[2];
  pBlkInfo->szBlock = aBufN[0] + aBufN[1] + aBufN[2] + aBufN[3];
H
Hongze Cheng 已提交
575

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

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

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

H
Hongze Cheng 已提交
591
  if (aBufN[0]) {
H
Hongze Cheng 已提交
592
    code = tsdbWriteFile(pFD, offset, pWriter->aBuf[0], aBufN[0]);
H
Hongze Cheng 已提交
593
    if (code) goto _err;
H
Hongze Cheng 已提交
594
  }
H
Hongze Cheng 已提交
595

H
Hongze Cheng 已提交
596 597
  // update info
  if (toLast) {
H
Hongze Cheng 已提交
598
    pWriter->fStt[pWriter->wSet.nSttF - 1].size += pBlkInfo->szBlock;
H
Hongze Cheng 已提交
599 600 601
  } else {
    pWriter->fData.size += pBlkInfo->szBlock;
  }
H
Hongze Cheng 已提交
602

H
Hongze Cheng 已提交
603 604 605 606 607
  // ================= SMA ====================
  if (pSmaInfo) {
    code = tsdbWriteBlockSma(pWriter, pBlockData, pSmaInfo);
    if (code) goto _err;
  }
H
Hongze Cheng 已提交
608

H
Hongze Cheng 已提交
609
_exit:
S
Shengliang Guan 已提交
610
  tsdbTrace("vgId:%d, tsdb write block data, suid:%" PRId64 " uid:%" PRId64 " nRow:%d, offset:%" PRId64 " size:%d",
H
Hongze Cheng 已提交
611 612
            TD_VID(pWriter->pTsdb->pVnode), pBlockData->suid, pBlockData->uid, pBlockData->nRow, pBlkInfo->offset,
            pBlkInfo->szBlock);
H
Hongze Cheng 已提交
613 614 615
  return code;

_err:
S
Shengliang Guan 已提交
616
  tsdbError("vgId:%d, tsdb write block data failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
617 618
  return code;
}
H
Hongze Cheng 已提交
619

H
Hongze Cheng 已提交
620
int32_t tsdbWriteDiskData(SDataFWriter *pWriter, const SDiskData *pDiskData, SBlockInfo *pBlkInfo, SSmaInfo *pSmaInfo) {
H
Hongze Cheng 已提交
621 622 623
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
624 625 626 627 628 629 630 631 632 633
  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 已提交
634 635 636 637 638 639 640 641

  // 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 已提交
642
  code = tsdbWriteFile(pFD, pBlkInfo->offset, pWriter->aBuf[0], n);
H
Hongze Cheng 已提交
643
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
644 645
  pBlkInfo->szKey += n;
  pBlkInfo->szBlock += n;
H
Hongze Cheng 已提交
646 647

  // uid + ver + key
H
Hongze Cheng 已提交
648 649
  if (pDiskData->pUid) {
    code = tsdbWriteFile(pFD, pBlkInfo->offset + pBlkInfo->szBlock, pDiskData->pUid, pDiskData->hdr.szUid);
H
Hongze Cheng 已提交
650
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
651 652
    pBlkInfo->szKey += pDiskData->hdr.szUid;
    pBlkInfo->szBlock += pDiskData->hdr.szUid;
H
Hongze Cheng 已提交
653 654
  }

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

H
Hongze Cheng 已提交
660
  code = tsdbWriteFile(pFD, pBlkInfo->offset + pBlkInfo->szBlock, pDiskData->pKey, pDiskData->hdr.szKey);
H
Hongze Cheng 已提交
661
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
662 663
  pBlkInfo->szKey += pDiskData->hdr.szKey;
  pBlkInfo->szBlock += pDiskData->hdr.szKey;
H
Hongze Cheng 已提交
664

H
Hongze Cheng 已提交
665
  // aBlockCol
H
Hongze Cheng 已提交
666 667 668 669 670 671 672
  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 已提交
673
      n += tPutBlockCol(pWriter->aBuf[0] + n, pDiskCol);
H
Hongze Cheng 已提交
674
    }
675
    ASSERT(n == pDiskData->hdr.szBlkCol);
H
Hongze Cheng 已提交
676

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

H
Hongze Cheng 已提交
680
    pBlkInfo->szBlock += pDiskData->hdr.szBlkCol;
H
Hongze Cheng 已提交
681 682
  }

H
Hongze Cheng 已提交
683
  // aDiskCol
H
Hongze Cheng 已提交
684 685 686
  for (int32_t iDiskCol = 0; iDiskCol < taosArrayGetSize(pDiskData->aDiskCol); iDiskCol++) {
    SDiskCol *pDiskCol = (SDiskCol *)taosArrayGet(pDiskData->aDiskCol, iDiskCol);

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

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

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

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

H
Hongze Cheng 已提交
701 702
    if (pDiskCol->pVal) {
      code = tsdbWriteFile(pFD, pBlkInfo->offset + pBlkInfo->szBlock, pDiskCol->pVal, pDiskCol->bCol.szValue);
H
Hongze Cheng 已提交
703
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
704 705

      pBlkInfo->szBlock += pDiskCol->bCol.szValue;
H
Hongze Cheng 已提交
706 707 708
    }
  }

H
Hongze Cheng 已提交
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
  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 已提交
739 740
_exit:
  if (code) {
S
Shengliang Guan 已提交
741
    tsdbError("vgId:%d, %s failed at %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
742 743 744 745
  }
  return code;
}

H
Hongze Cheng 已提交
746 747 748 749
int32_t tsdbDFileSetCopy(STsdb *pTsdb, SDFileSet *pSetFrom, SDFileSet *pSetTo) {
  int32_t   code = 0;
  int64_t   n;
  int64_t   size;
H
Hongze Cheng 已提交
750 751 752
  TdFilePtr pOutFD = NULL;
  TdFilePtr PInFD = NULL;
  int32_t   szPage = pTsdb->pVnode->config.szPage;
H
Hongze Cheng 已提交
753 754
  char      fNameFrom[TSDB_FILENAME_LEN];
  char      fNameTo[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
755

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

  // data
H
Hongze Cheng 已提交
778 779
  tsdbDataFileName(pTsdb, pSetFrom->diskId, pSetFrom->fid, pSetFrom->pDataF, fNameFrom);
  tsdbDataFileName(pTsdb, pSetTo->diskId, pSetTo->fid, pSetTo->pDataF, fNameTo);
K
kailixu 已提交
780
  pOutFD = taosCreateFile(fNameTo, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
H
Hongze Cheng 已提交
781 782 783
  if (pOutFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
H
Hongze Cheng 已提交
784
  }
H
Hongze Cheng 已提交
785 786
  PInFD = taosOpenFile(fNameFrom, TD_FILE_READ);
  if (PInFD == NULL) {
H
Hongze Cheng 已提交
787 788 789
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
K
kailixu 已提交
790
  n = taosFSendFile(pOutFD, PInFD, 0, tsdbLogicToFileSize(pSetFrom->pDataF->size, szPage));
H
Hongze Cheng 已提交
791
  if (n < 0) {
H
Hongze Cheng 已提交
792 793 794
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
H
Hongze Cheng 已提交
795 796
  taosCloseFile(&pOutFD);
  taosCloseFile(&PInFD);
H
Hongze Cheng 已提交
797

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

H
Hongze Cheng 已提交
819 820 821 822
  // 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);
K
kailixu 已提交
823
    pOutFD = taosCreateFile(fNameTo, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
H
Hongze Cheng 已提交
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
    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 已提交
842 843 844
  return code;

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

H
Hongze Cheng 已提交
849 850 851
// SDataFReader ====================================================
int32_t tsdbDataFReaderOpen(SDataFReader **ppReader, STsdb *pTsdb, SDFileSet *pSet) {
  int32_t       code = 0;
H
Hongze Cheng 已提交
852 853
  int32_t       lino = 0;
  SDataFReader *pReader = NULL;
H
Hongze Cheng 已提交
854
  int32_t       szPage = pTsdb->pVnode->config.tsdbPageSize;
H
Hongze Cheng 已提交
855
  char          fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
856

H
Hongze Cheng 已提交
857 858 859 860
  // alloc
  pReader = (SDataFReader *)taosMemoryCalloc(1, sizeof(*pReader));
  if (pReader == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
861
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
862
  }
H
Hongze Cheng 已提交
863 864
  pReader->pTsdb = pTsdb;
  pReader->pSet = pSet;
H
Hongze Cheng 已提交
865

H
Hongze Cheng 已提交
866 867
  // head
  tsdbHeadFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pHeadF, fname);
H
Hongze Cheng 已提交
868
  code = tsdbOpenFile(fname, szPage, TD_FILE_READ, &pReader->pHeadFD);
H
Hongze Cheng 已提交
869
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
870

H
Hongze Cheng 已提交
871 872
  // data
  tsdbDataFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pDataF, fname);
H
Hongze Cheng 已提交
873
  code = tsdbOpenFile(fname, szPage, TD_FILE_READ, &pReader->pDataFD);
H
Hongze Cheng 已提交
874
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
875

H
Hongze Cheng 已提交
876 877
  // sma
  tsdbSmaFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pSmaF, fname);
H
Hongze Cheng 已提交
878
  code = tsdbOpenFile(fname, szPage, TD_FILE_READ, &pReader->pSmaFD);
H
Hongze Cheng 已提交
879
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
880

H
Hongze Cheng 已提交
881 882 883 884
  // 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 已提交
885
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
886
  }
H
Hongze Cheng 已提交
887

H
Hongze Cheng 已提交
888 889 890
_exit:
  if (code) {
    *ppReader = NULL;
S
Shengliang Guan 已提交
891
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
892 893 894 895 896 897 898 899 900 901 902

    if (pReader) {
      for (int32_t iStt = 0; iStt < pSet->nSttF; iStt++) tsdbCloseFile(&pReader->aSttFD[iStt]);
      tsdbCloseFile(&pReader->pSmaFD);
      tsdbCloseFile(&pReader->pDataFD);
      tsdbCloseFile(&pReader->pHeadFD);
      taosMemoryFree(pReader);
    }
  } else {
    *ppReader = pReader;
  }
H
Hongze Cheng 已提交
903 904 905 906 907
  return code;
}

int32_t tsdbDataFReaderClose(SDataFReader **ppReader) {
  int32_t code = 0;
H
Hongze Cheng 已提交
908
  if (*ppReader == NULL) return code;
H
Hongze Cheng 已提交
909 910

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

H
Hongze Cheng 已提交
913
  // data
H
Hongze Cheng 已提交
914
  tsdbCloseFile(&(*ppReader)->pDataFD);
H
Hongze Cheng 已提交
915

H
Hongze Cheng 已提交
916
  // sma
H
Hongze Cheng 已提交
917
  tsdbCloseFile(&(*ppReader)->pSmaFD);
H
Hongze Cheng 已提交
918

H
Hongze Cheng 已提交
919
  // stt
H
Hongze Cheng 已提交
920
  for (int32_t iStt = 0; iStt < TSDB_MAX_STT_TRIGGER; iStt++) {
H
Hongze Cheng 已提交
921 922
    if ((*ppReader)->aSttFD[iStt]) {
      tsdbCloseFile(&(*ppReader)->aSttFD[iStt]);
H
Hongze Cheng 已提交
923
    }
H
Hongze Cheng 已提交
924 925 926 927
  }

  for (int32_t iBuf = 0; iBuf < sizeof((*ppReader)->aBuf) / sizeof(uint8_t *); iBuf++) {
    tFree((*ppReader)->aBuf[iBuf]);
H
Hongze Cheng 已提交
928
  }
H
Hongze Cheng 已提交
929 930
  taosMemoryFree(*ppReader);
  *ppReader = NULL;
H
Hongze Cheng 已提交
931
  return code;
H
Hongze Cheng 已提交
932 933
}

H
Hongze Cheng 已提交
934
int32_t tsdbReadBlockIdx(SDataFReader *pReader, SArray *aBlockIdx) {
H
Hongze Cheng 已提交
935 936 937 938
  int32_t    code = 0;
  SHeadFile *pHeadFile = pReader->pSet->pHeadF;
  int64_t    offset = pHeadFile->offset;
  int64_t    size = pHeadFile->size - offset;
H
Hongze Cheng 已提交
939

H
Hongze Cheng 已提交
940
  taosArrayClear(aBlockIdx);
H
Hongze Cheng 已提交
941
  if (size == 0) return code;
H
Hongze Cheng 已提交
942 943

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

H
Hongze Cheng 已提交
947
  // read
H
Hongze Cheng 已提交
948 949
  code = tsdbReadFile(pReader->pHeadFD, offset, pReader->aBuf[0], size);
  if (code) goto _err;
H
Hongze Cheng 已提交
950 951

  // decode
H
Hongze Cheng 已提交
952
  int64_t n = 0;
H
Hongze Cheng 已提交
953
  while (n < size) {
H
Hongze Cheng 已提交
954 955 956 957 958 959 960
    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 已提交
961
  }
962
  ASSERT(n == size);
H
Hongze Cheng 已提交
963 964 965 966

  return code;

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

H
Hongze Cheng 已提交
971
int32_t tsdbReadSttBlk(SDataFReader *pReader, int32_t iStt, SArray *aSttBlk) {
H
Hongze Cheng 已提交
972
  int32_t   code = 0;
H
Hongze Cheng 已提交
973 974 975
  SSttFile *pSttFile = pReader->pSet->aSttF[iStt];
  int64_t   offset = pSttFile->offset;
  int64_t   size = pSttFile->size - offset;
H
Hongze Cheng 已提交
976

H
Hongze Cheng 已提交
977
  taosArrayClear(aSttBlk);
H
Hongze Cheng 已提交
978
  if (size == 0) return code;
H
Hongze Cheng 已提交
979 980

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

H
Hongze Cheng 已提交
984
  // read
H
Hongze Cheng 已提交
985
  code = tsdbReadFile(pReader->aSttFD[iStt], offset, pReader->aBuf[0], size);
H
Hongze Cheng 已提交
986
  if (code) goto _err;
H
Hongze Cheng 已提交
987

H
Hongze Cheng 已提交
988
  // decode
H
Hongze Cheng 已提交
989
  int64_t n = 0;
H
Hongze Cheng 已提交
990
  while (n < size) {
H
Hongze Cheng 已提交
991 992
    SSttBlk sttBlk;
    n += tGetSttBlk(pReader->aBuf[0] + n, &sttBlk);
H
Hongze Cheng 已提交
993

H
Hongze Cheng 已提交
994
    if (taosArrayPush(aSttBlk, &sttBlk) == NULL) {
H
Hongze Cheng 已提交
995 996 997 998
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
  }
999
  ASSERT(n == size);
H
Hongze Cheng 已提交
1000

H
Hongze Cheng 已提交
1001 1002 1003
  return code;

_err:
S
Shengliang Guan 已提交
1004
  tsdbError("vgId:%d, read stt blk failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1005 1006 1007
  return code;
}

H
Hongze Cheng 已提交
1008
int32_t tsdbReadDataBlk(SDataFReader *pReader, SBlockIdx *pBlockIdx, SMapData *mDataBlk) {
H
Hongze Cheng 已提交
1009 1010 1011
  int32_t code = 0;
  int64_t offset = pBlockIdx->offset;
  int64_t size = pBlockIdx->size;
H
Hongze Cheng 已提交
1012 1013

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

H
Hongze Cheng 已提交
1017
  // read
H
Hongze Cheng 已提交
1018 1019
  code = tsdbReadFile(pReader->pHeadFD, offset, pReader->aBuf[0], size);
  if (code) goto _err;
H
Hongze Cheng 已提交
1020

H
Hongze Cheng 已提交
1021
  // decode
H
Hongze Cheng 已提交
1022
  int64_t n = tGetMapData(pReader->aBuf[0], mDataBlk);
H
Hongze Cheng 已提交
1023
  if (n < 0) {
H
Hongze Cheng 已提交
1024 1025 1026
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
1027
  ASSERT(n == size);
H
Hongze Cheng 已提交
1028

H
Hongze Cheng 已提交
1029
  return code;
H
Hongze Cheng 已提交
1030

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

H
Hongze Cheng 已提交
1036 1037 1038
int32_t tsdbReadBlockSma(SDataFReader *pReader, SDataBlk *pDataBlk, SArray *aColumnDataAgg) {
  int32_t   code = 0;
  SSmaInfo *pSmaInfo = &pDataBlk->smaInfo;
H
Hongze Cheng 已提交
1039

1040
  ASSERT(pSmaInfo->size > 0);
H
Hongze Cheng 已提交
1041

H
Hongze Cheng 已提交
1042
  taosArrayClear(aColumnDataAgg);
H
Hongze Cheng 已提交
1043

H
Hongze Cheng 已提交
1044
  // alloc
H
Hongze Cheng 已提交
1045
  code = tRealloc(&pReader->aBuf[0], pSmaInfo->size);
H
Hongze Cheng 已提交
1046
  if (code) goto _err;
H
Hongze Cheng 已提交
1047

H
Hongze Cheng 已提交
1048
  // read
H
Hongze Cheng 已提交
1049
  code = tsdbReadFile(pReader->pSmaFD, pSmaInfo->offset, pReader->aBuf[0], pSmaInfo->size);
H
Hongze Cheng 已提交
1050
  if (code) goto _err;
H
Hongze Cheng 已提交
1051

H
Hongze Cheng 已提交
1052
  // decode
H
Hongze Cheng 已提交
1053
  int32_t n = 0;
H
Hongze Cheng 已提交
1054 1055 1056
  while (n < pSmaInfo->size) {
    SColumnDataAgg sma;
    n += tGetColumnDataAgg(pReader->aBuf[0] + n, &sma);
H
Hongze Cheng 已提交
1057

H
Hongze Cheng 已提交
1058 1059
    if (taosArrayPush(aColumnDataAgg, &sma) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1060 1061
      goto _err;
    }
H
Hongze Cheng 已提交
1062
  }
1063
  ASSERT(n == pSmaInfo->size);
H
Hongze Cheng 已提交
1064
  return code;
H
Hongze Cheng 已提交
1065

H
Hongze Cheng 已提交
1066
_err:
S
Shengliang Guan 已提交
1067
  tsdbError("vgId:%d, tsdb read block sma failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1068 1069
  return code;
}
H
Hongze Cheng 已提交
1070

H
Hongze Cheng 已提交
1071 1072
static int32_t tsdbReadBlockDataImpl(SDataFReader *pReader, SBlockInfo *pBlkInfo, SBlockData *pBlockData,
                                     int32_t iStt) {
H
Hongze Cheng 已提交
1073
  int32_t code = 0;
H
Hongze Cheng 已提交
1074

H
Hongze Cheng 已提交
1075 1076
  tBlockDataClear(pBlockData);

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

  // uid + version + tskey
H
Hongze Cheng 已提交
1080 1081 1082 1083 1084 1085
  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 已提交
1086 1087 1088
  SDiskDataHdr hdr;
  uint8_t     *p = pReader->aBuf[0] + tGetDiskDataHdr(pReader->aBuf[0], &hdr);

1089 1090
  ASSERT(hdr.delimiter == TSDB_FILE_DLMT);
  ASSERT(pBlockData->suid == hdr.suid);
H
Hongze Cheng 已提交
1091

H
Hongze Cheng 已提交
1092
  pBlockData->uid = hdr.uid;
H
Hongze Cheng 已提交
1093 1094 1095 1096
  pBlockData->nRow = hdr.nRow;

  // uid
  if (hdr.uid == 0) {
1097
    ASSERT(hdr.szUid);
H
Hongze Cheng 已提交
1098 1099 1100 1101
    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 {
1102
    ASSERT(!hdr.szUid);
H
Hongze Cheng 已提交
1103 1104
  }
  p += hdr.szUid;
H
Hongze Cheng 已提交
1105

H
Hongze Cheng 已提交
1106 1107 1108 1109 1110
  // 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 已提交
1111

H
Hongze Cheng 已提交
1112 1113 1114
  // 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 已提交
1115
  if (code) goto _err;
H
Hongze Cheng 已提交
1116
  p += hdr.szKey;
H
Hongze Cheng 已提交
1117

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

H
Hongze Cheng 已提交
1120
  // read and decode columns
H
Hongze Cheng 已提交
1121
  if (pBlockData->nColData == 0) goto _exit;
H
Hongze Cheng 已提交
1122

H
Hongze Cheng 已提交
1123 1124
  if (hdr.szBlkCol > 0) {
    int64_t offset = pBlkInfo->offset + pBlkInfo->szKey;
H
Hongze Cheng 已提交
1125 1126 1127 1128

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

H
Hongze Cheng 已提交
1129 1130
    code = tsdbReadFile(pFD, offset, pReader->aBuf[0], hdr.szBlkCol);
    if (code) goto _err;
H
Hongze Cheng 已提交
1131 1132
  }

H
Hongze Cheng 已提交
1133 1134 1135
  SBlockCol  blockCol = {.cid = 0};
  SBlockCol *pBlockCol = &blockCol;
  int32_t    n = 0;
H
Hongze Cheng 已提交
1136

H
Hongze Cheng 已提交
1137
  for (int32_t iColData = 0; iColData < pBlockData->nColData; iColData++) {
H
Hongze Cheng 已提交
1138
    SColData *pColData = tBlockDataGetColDataByIdx(pBlockData, iColData);
H
Hongze Cheng 已提交
1139

H
Hongze Cheng 已提交
1140 1141 1142 1143
    while (pBlockCol && pBlockCol->cid < pColData->cid) {
      if (n < hdr.szBlkCol) {
        n += tGetBlockCol(pReader->aBuf[0] + n, pBlockCol);
      } else {
1144
        ASSERT(n == hdr.szBlkCol);
H
Hongze Cheng 已提交
1145 1146
        pBlockCol = NULL;
      }
H
Hongze Cheng 已提交
1147
    }
H
Hongze Cheng 已提交
1148

H
Hongze Cheng 已提交
1149 1150 1151 1152 1153 1154 1155
    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 {
1156 1157
      ASSERT(pBlockCol->type == pColData->type);
      ASSERT(pBlockCol->flag && pBlockCol->flag != HAS_NONE);
H
Hongze Cheng 已提交
1158

H
Hongze Cheng 已提交
1159 1160 1161 1162 1163 1164 1165 1166
      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 已提交
1167 1168
        int64_t offset = pBlkInfo->offset + pBlkInfo->szKey + hdr.szBlkCol + pBlockCol->offset;
        int32_t size = pBlockCol->szBitmap + pBlockCol->szOffset + pBlockCol->szValue;
H
Hongze Cheng 已提交
1169

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

H
Hongze Cheng 已提交
1173 1174
        code = tsdbReadFile(pFD, offset, pReader->aBuf[1], size);
        if (code) goto _err;
H
Hongze Cheng 已提交
1175 1176 1177 1178 1179

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

H
Hongze Cheng 已提交
1182
_exit:
H
Hongze Cheng 已提交
1183 1184
  return code;

H
Hongze Cheng 已提交
1185
_err:
S
Shengliang Guan 已提交
1186
  tsdbError("vgId:%d, tsdb read block data impl failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1187
  return code;
H
Hongze Cheng 已提交
1188
}
H
Hongze Cheng 已提交
1189

H
Hongze Cheng 已提交
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
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:
S
Shengliang Guan 已提交
1209
  tsdbError("vgId:%d, tsdb read data block ex failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1210
  return code;
H
Hongze Cheng 已提交
1211
}
H
Hongze Cheng 已提交
1212

H
Hongze Cheng 已提交
1213 1214
int32_t tsdbReadDataBlock(SDataFReader *pReader, SDataBlk *pDataBlk, SBlockData *pBlockData) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1215

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

1219
  ASSERT(pDataBlk->nSubBlock == 1);
H
Hongze Cheng 已提交
1220

H
Hongze Cheng 已提交
1221
  return code;
H
Hongze Cheng 已提交
1222

H
Hongze Cheng 已提交
1223
_err:
S
Shengliang Guan 已提交
1224
  tsdbError("vgId:%d, tsdb read data block failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1225 1226
  return code;
}
H
Hongze Cheng 已提交
1227

H
Hongze Cheng 已提交
1228
int32_t tsdbReadSttBlock(SDataFReader *pReader, int32_t iStt, SSttBlk *pSttBlk, SBlockData *pBlockData) {
H
Hongze Cheng 已提交
1229
  int32_t code = 0;
H
Hongze Cheng 已提交
1230
  int32_t lino = 0;
H
Hongze Cheng 已提交
1231

H
Hongze Cheng 已提交
1232
  code = tsdbReadBlockDataImpl(pReader, &pSttBlk->bInfo, pBlockData, iStt);
H
Hongze Cheng 已提交
1233 1234 1235 1236
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
S
Shengliang Guan 已提交
1237
    tsdbError("vgId:%d, %s failed at %d since %s", TD_VID(pReader->pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1238 1239 1240 1241 1242 1243 1244
  }
  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 已提交
1245

H
Hongze Cheng 已提交
1246
  // alloc
H
Hongze Cheng 已提交
1247
  code = tRealloc(&pReader->aBuf[0], pSttBlk->bInfo.szBlock);
H
Hongze Cheng 已提交
1248
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1249

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

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

H
Hongze Cheng 已提交
1258 1259
_exit:
  if (code) {
S
Shengliang Guan 已提交
1260
    tsdbError("vgId:%d, %s failed at %d since %s", TD_VID(pReader->pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1261
  }
H
Hongze Cheng 已提交
1262 1263 1264 1265 1266 1267
  return code;
}

// SDelFWriter ====================================================
int32_t tsdbDelFWriterOpen(SDelFWriter **ppWriter, SDelFile *pFile, STsdb *pTsdb) {
  int32_t      code = 0;
H
Hongze Cheng 已提交
1268
  int32_t      lino = 0;
H
Hongze Cheng 已提交
1269
  char         fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
1270
  uint8_t      hdr[TSDB_FHDR_SIZE] = {0};
H
Hongze Cheng 已提交
1271
  SDelFWriter *pDelFWriter = NULL;
H
Hongze Cheng 已提交
1272 1273 1274 1275 1276 1277
  int64_t      n;

  // alloc
  pDelFWriter = (SDelFWriter *)taosMemoryCalloc(1, sizeof(*pDelFWriter));
  if (pDelFWriter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1278
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1279 1280 1281 1282 1283
  }
  pDelFWriter->pTsdb = pTsdb;
  pDelFWriter->fDel = *pFile;

  tsdbDelFileName(pTsdb, pFile, fname);
H
Hongze Cheng 已提交
1284 1285
  code = tsdbOpenFile(fname, pTsdb->pVnode->config.tsdbPageSize, TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE,
                      &pDelFWriter->pWriteH);
H
Hongze Cheng 已提交
1286
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1287 1288

  // update header
H
Hongze Cheng 已提交
1289
  code = tsdbWriteFile(pDelFWriter->pWriteH, 0, hdr, TSDB_FHDR_SIZE);
H
Hongze Cheng 已提交
1290
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1291 1292 1293 1294 1295 1296

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

  *ppWriter = pDelFWriter;

H
Hongze Cheng 已提交
1297 1298 1299 1300
_exit:
  if (code) {
    if (pDelFWriter) {
      tsdbCloseFile(&pDelFWriter->pWriteH);
H
Hongze Cheng 已提交
1301
      taosMemoryFree(pDelFWriter);
H
Hongze Cheng 已提交
1302 1303
    }
    *ppWriter = NULL;
S
Shengliang Guan 已提交
1304
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(errno));
H
Hongze Cheng 已提交
1305 1306 1307
  } else {
    *ppWriter = pDelFWriter;
  }
H
Hongze Cheng 已提交
1308 1309 1310 1311 1312 1313 1314 1315 1316
  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 已提交
1317 1318 1319
  if (sync) {
    code = tsdbFsyncFile(pWriter->pWriteH);
    if (code) goto _err;
H
Hongze Cheng 已提交
1320 1321 1322
  }

  // close
H
Hongze Cheng 已提交
1323
  tsdbCloseFile(&pWriter->pWriteH);
H
Hongze Cheng 已提交
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343

  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 已提交
1344
  size = 0;
H
Hongze Cheng 已提交
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
  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));
  }
1358
  ASSERT(n == size);
H
Hongze Cheng 已提交
1359 1360

  // write
H
Hongze Cheng 已提交
1361 1362
  code = tsdbWriteFile(pWriter->pWriteH, pWriter->fDel.size, pWriter->aBuf[0], size);
  if (code) goto _err;
H
Hongze Cheng 已提交
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382

  // 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 已提交
1383
  size = 0;
H
Hongze Cheng 已提交
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
  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));
  }
1397
  ASSERT(n == size);
H
Hongze Cheng 已提交
1398 1399

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

H
Hongze Cheng 已提交
1403 1404 1405
  // update
  pWriter->fDel.offset = pWriter->fDel.size;
  pWriter->fDel.size += size;
H
Hongze Cheng 已提交
1406

H
Hongze Cheng 已提交
1407
  return code;
H
Hongze Cheng 已提交
1408

H
Hongze Cheng 已提交
1409 1410 1411 1412 1413 1414 1415
_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 已提交
1416
  char    hdr[TSDB_FHDR_SIZE] = {0};
H
Hongze Cheng 已提交
1417 1418 1419 1420 1421
  int64_t size = TSDB_FHDR_SIZE;
  int64_t n;

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

H
Hongze Cheng 已提交
1423
  // write
H
Hongze Cheng 已提交
1424 1425
  code = tsdbWriteFile(pWriter->pWriteH, 0, hdr, size);
  if (code) goto _err;
H
Hongze Cheng 已提交
1426 1427 1428 1429

  return code;

_err:
H
Hongze Cheng 已提交
1430
  tsdbError("vgId:%d, update del file hdr failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1431
  return code;
H
Hongze Cheng 已提交
1432
}
H
Hongze Cheng 已提交
1433 1434
// SDelFReader ====================================================
struct SDelFReader {
H
Hongze Cheng 已提交
1435 1436 1437
  STsdb   *pTsdb;
  SDelFile fDel;
  STsdbFD *pReadH;
H
Hongze Cheng 已提交
1438 1439
  uint8_t *aBuf[1];
};
H
Hongze Cheng 已提交
1440

H
Hongze Cheng 已提交
1441 1442
int32_t tsdbDelFReaderOpen(SDelFReader **ppReader, SDelFile *pFile, STsdb *pTsdb) {
  int32_t      code = 0;
H
Hongze Cheng 已提交
1443
  int32_t      lino = 0;
H
Hongze Cheng 已提交
1444
  char         fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
1445
  SDelFReader *pDelFReader = NULL;
H
Hongze Cheng 已提交
1446

H
Hongze Cheng 已提交
1447 1448 1449
  // alloc
  pDelFReader = (SDelFReader *)taosMemoryCalloc(1, sizeof(*pDelFReader));
  if (pDelFReader == NULL) {
H
Hongze Cheng 已提交
1450
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1451
    goto _exit;
H
Hongze Cheng 已提交
1452 1453 1454 1455 1456 1457 1458
  }

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

  tsdbDelFileName(pTsdb, pFile, fname);
H
Hongze Cheng 已提交
1459
  code = tsdbOpenFile(fname, pTsdb->pVnode->config.tsdbPageSize, TD_FILE_READ, &pDelFReader->pReadH);
H
Hongze Cheng 已提交
1460 1461 1462 1463
  if (code) {
    taosMemoryFree(pDelFReader);
    goto _exit;
  }
H
Hongze Cheng 已提交
1464

H
Hongze Cheng 已提交
1465 1466 1467
_exit:
  if (code) {
    *ppReader = NULL;
S
Shengliang Guan 已提交
1468
    tsdbError("vgId:%d, %s failed at %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1469 1470 1471
  } else {
    *ppReader = pDelFReader;
  }
H
Hongze Cheng 已提交
1472
  return code;
H
Hongze Cheng 已提交
1473 1474
}

H
Hongze Cheng 已提交
1475 1476 1477
int32_t tsdbDelFReaderClose(SDelFReader **ppReader) {
  int32_t      code = 0;
  SDelFReader *pReader = *ppReader;
H
Hongze Cheng 已提交
1478

H
Hongze Cheng 已提交
1479
  if (pReader) {
H
Hongze Cheng 已提交
1480
    tsdbCloseFile(&pReader->pReadH);
H
Hongze Cheng 已提交
1481 1482 1483 1484
    for (int32_t iBuf = 0; iBuf < sizeof(pReader->aBuf) / sizeof(uint8_t *); iBuf++) {
      tFree(pReader->aBuf[iBuf]);
    }
    taosMemoryFree(pReader);
H
Hongze Cheng 已提交
1485 1486
  }

1487
  *ppReader = NULL;
H
Hongze Cheng 已提交
1488 1489 1490
  return code;
}

H
Hongze Cheng 已提交
1491
int32_t tsdbReadDelData(SDelFReader *pReader, SDelIdx *pDelIdx, SArray *aDelData) {
H
Hongze Cheng 已提交
1492
  return tsdbReadDelDatav1(pReader, pDelIdx, aDelData, INT64_MAX);
H
Haojun Liao 已提交
1493 1494
}

H
Haojun Liao 已提交
1495
int32_t tsdbReadDelDatav1(SDelFReader *pReader, SDelIdx *pDelIdx, SArray *aDelData, int64_t maxVer) {
H
Hongze Cheng 已提交
1496
  int32_t code = 0;
H
Hongze Cheng 已提交
1497 1498 1499
  int64_t offset = pDelIdx->offset;
  int64_t size = pDelIdx->size;
  int64_t n;
H
Hongze Cheng 已提交
1500

H
Hongze Cheng 已提交
1501
  taosArrayClear(aDelData);
H
Hongze Cheng 已提交
1502

H
Hongze Cheng 已提交
1503 1504 1505
  // alloc
  code = tRealloc(&pReader->aBuf[0], size);
  if (code) goto _err;
H
Hongze Cheng 已提交
1506

H
Hongze Cheng 已提交
1507
  // read
H
Hongze Cheng 已提交
1508 1509
  code = tsdbReadFile(pReader->pReadH, offset, pReader->aBuf[0], size);
  if (code) goto _err;
H
Hongze Cheng 已提交
1510 1511 1512

  // // decode
  n = 0;
H
Hongze Cheng 已提交
1513
  while (n < size) {
H
Hongze Cheng 已提交
1514 1515 1516
    SDelData delData;
    n += tGetDelData(pReader->aBuf[0] + n, &delData);

1517 1518
    if (delData.version > maxVer) {
      continue;
H
Hongze Cheng 已提交
1519
    }
H
Hongze Cheng 已提交
1520 1521 1522 1523
    if (taosArrayPush(aDelData, &delData) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
1524
  }
1525

1526
  ASSERT(n == size);
H
Hongze Cheng 已提交
1527 1528 1529 1530 1531

  return code;

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

H
Hongze Cheng 已提交
1535
int32_t tsdbReadDelIdx(SDelFReader *pReader, SArray *aDelIdx) {
H
Hongze Cheng 已提交
1536
  int32_t code = 0;
H
Hongze Cheng 已提交
1537 1538 1539
  int32_t n;
  int64_t offset = pReader->fDel.offset;
  int64_t size = pReader->fDel.size - offset;
H
Hongze Cheng 已提交
1540

H
Hongze Cheng 已提交
1541 1542 1543 1544 1545 1546 1547
  taosArrayClear(aDelIdx);

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

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

H
Hongze Cheng 已提交
1551 1552
  // decode
  n = 0;
H
Hongze Cheng 已提交
1553
  while (n < size) {
H
Hongze Cheng 已提交
1554
    SDelIdx delIdx;
H
Hongze Cheng 已提交
1555

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

H
Hongze Cheng 已提交
1558 1559 1560 1561
    if (taosArrayPush(aDelIdx, &delIdx) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
1562 1563
  }

1564
  ASSERT(n == size);
H
Hongze Cheng 已提交
1565

H
Hongze Cheng 已提交
1566
  return code;
H
Hongze Cheng 已提交
1567

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