tsdbSnapshot.c 56.9 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "tsdb.h"

H
Hongze Cheng 已提交
18
extern int32_t tsdbReadDataBlockEx(SDataFReader* pReader, SDataBlk* pDataBlk, SBlockData* pBlockData);
H
Hongze Cheng 已提交
19 20 21
extern int32_t tsdbUpdateTableSchema(SMeta* pMeta, int64_t suid, int64_t uid, SSkmInfo* pSkmInfo);
extern int32_t tsdbWriteDataBlock(SDataFWriter* pWriter, SBlockData* pBlockData, SMapData* mDataBlk, int8_t cmprAlg);
extern int32_t tsdbWriteSttBlock(SDataFWriter* pWriter, SBlockData* pBlockData, SArray* aSttBlk, int8_t cmprAlg);
H
Hongze Cheng 已提交
22 23 24 25 26

// STsdbDataIter2 ========================================
#define TSDB_MEM_TABLE_DATA_ITER 0
#define TSDB_DATA_FILE_DATA_ITER 1
#define TSDB_STT_FILE_DATA_ITER  2
H
Hongze Cheng 已提交
27
#define TSDB_TOMB_FILE_DATA_ITER 3
H
Hongze Cheng 已提交
28

H
Hongze Cheng 已提交
29 30 31
typedef struct STsdbDataIter2  STsdbDataIter2;
typedef struct STsdbFilterInfo STsdbFilterInfo;

H
Hongze Cheng 已提交
32
typedef struct {
H
Hongze Cheng 已提交
33 34 35 36 37
  int64_t  suid;
  int64_t  uid;
  SDelData delData;
} SDelInfo;

H
Hongze Cheng 已提交
38 39 40 41 42 43
struct STsdbDataIter2 {
  STsdbDataIter2* next;
  SRBTreeNode     rbtn;

  int32_t  type;
  SRowInfo rowInfo;
H
Hongze Cheng 已提交
44
  SDelInfo delInfo;
H
Hongze Cheng 已提交
45
  union {
H
Hongze Cheng 已提交
46
    // TSDB_MEM_TABLE_DATA_ITER
H
Hongze Cheng 已提交
47
    struct {
H
Hongze Cheng 已提交
48 49 50 51
      SMemTable* pMemTable;
    } mIter;

    // TSDB_DATA_FILE_DATA_ITER
H
Hongze Cheng 已提交
52
    struct {
H
Hongze Cheng 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
      SDataFReader* pReader;
      SArray*       aBlockIdx;  // SArray<SBlockIdx>
      SMapData      mDataBlk;
      SBlockData    bData;
      int32_t       iBlockIdx;
      int32_t       iDataBlk;
      int32_t       iRow;
    } dIter;

    // TSDB_STT_FILE_DATA_ITER
    struct {
      SDataFReader* pReader;
      int32_t       iStt;
      SArray*       aSttBlk;
      SBlockData    bData;
      int32_t       iSttBlk;
      int32_t       iRow;
    } sIter;
H
Hongze Cheng 已提交
71 72 73 74 75 76 77 78
    // TSDB_TOMB_FILE_DATA_ITER
    struct {
      SDelFReader* pReader;
      SArray*      aDelIdx;
      SArray*      aDelData;
      int32_t      iDelIdx;
      int32_t      iDelData;
    } tIter;
H
Hongze Cheng 已提交
79
  };
H
Hongze Cheng 已提交
80
};
H
Hongze Cheng 已提交
81

H
Hongze Cheng 已提交
82 83 84
#define TSDB_FILTER_FLAG_BY_VERSION 0x1
struct STsdbFilterInfo {
  int32_t flag;
H
more  
Hongze Cheng 已提交
85 86
  int64_t sver;
  int64_t ever;
H
Hongze Cheng 已提交
87 88
};

H
Hongze Cheng 已提交
89
#define TSDB_RBTN_TO_DATA_ITER(pNode) ((STsdbDataIter2*)(((char*)pNode) - offsetof(STsdbDataIter2, rbtn)))
H
Hongze Cheng 已提交
90

H
Hongze Cheng 已提交
91 92 93 94
/* open */
static int32_t tsdbOpenDataFileDataIter(SDataFReader* pReader, STsdbDataIter2** ppIter) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
95

H
Hongze Cheng 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  // create handle
  STsdbDataIter2* pIter = (STsdbDataIter2*)taosMemoryCalloc(1, sizeof(*pIter));
  if (pIter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  pIter->type = TSDB_DATA_FILE_DATA_ITER;
  pIter->dIter.pReader = pReader;
  if ((pIter->dIter.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx))) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  code = tBlockDataCreate(&pIter->dIter.bData);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
113 114 115
  pIter->dIter.iBlockIdx = 0;
  pIter->dIter.iDataBlk = 0;
  pIter->dIter.iRow = 0;
H
Hongze Cheng 已提交
116 117 118 119 120 121 122 123 124 125 126

  // read data
  code = tsdbReadBlockIdx(pReader, pIter->dIter.aBlockIdx);
  TSDB_CHECK_CODE(code, lino, _exit);

  if (taosArrayGetSize(pIter->dIter.aBlockIdx) == 0) goto _clear;

_exit:
  if (code) {
    if (pIter) {
    _clear:
127
      tBlockDataDestroy(&pIter->dIter.bData);
H
Hongze Cheng 已提交
128 129 130 131 132 133 134
      taosArrayDestroy(pIter->dIter.aBlockIdx);
      taosMemoryFree(pIter);
      pIter = NULL;
    }
  }
  *ppIter = pIter;
  return code;
H
Hongze Cheng 已提交
135 136
}

H
Hongze Cheng 已提交
137
static int32_t tsdbOpenSttFileDataIter(SDataFReader* pReader, int32_t iStt, STsdbDataIter2** ppIter) {
H
Hongze Cheng 已提交
138
  int32_t code = 0;
H
add log  
Hongze Cheng 已提交
139
  int32_t lino = 0;
H
Hongze Cheng 已提交
140

H
Hongze Cheng 已提交
141 142 143 144 145 146
  // create handle
  STsdbDataIter2* pIter = (STsdbDataIter2*)taosMemoryCalloc(1, sizeof(*pIter));
  if (pIter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
147

H
Hongze Cheng 已提交
148 149 150 151 152 153 154 155
  pIter->type = TSDB_STT_FILE_DATA_ITER;
  pIter->sIter.pReader = pReader;
  pIter->sIter.iStt = iStt;
  pIter->sIter.aSttBlk = taosArrayInit(0, sizeof(SSttBlk));
  if (pIter->sIter.aSttBlk == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
156

H
Hongze Cheng 已提交
157 158
  code = tBlockDataCreate(&pIter->sIter.bData);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
159

H
Hongze Cheng 已提交
160 161
  pIter->sIter.iSttBlk = 0;
  pIter->sIter.iRow = 0;
H
Hongze Cheng 已提交
162

H
Hongze Cheng 已提交
163 164
  // read data
  code = tsdbReadSttBlk(pReader, iStt, pIter->sIter.aSttBlk);
H
add log  
Hongze Cheng 已提交
165
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
166

H
Hongze Cheng 已提交
167 168 169 170 171 172 173
  if (taosArrayGetSize(pIter->sIter.aSttBlk) == 0) goto _clear;

_exit:
  if (code) {
    if (pIter) {
    _clear:
      taosArrayDestroy(pIter->sIter.aSttBlk);
174
      tBlockDataDestroy(&pIter->sIter.bData);
H
Hongze Cheng 已提交
175 176 177 178 179 180 181 182
      taosMemoryFree(pIter);
      pIter = NULL;
    }
  }
  *ppIter = pIter;
  return code;
}

H
Hongze Cheng 已提交
183 184 185
static int32_t tsdbOpenTombFileDataIter(SDelFReader* pReader, STsdbDataIter2** ppIter) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
186

H
Hongze Cheng 已提交
187 188 189
  STsdbDataIter2* pIter = (STsdbDataIter2*)taosMemoryCalloc(1, sizeof(*pIter));
  if (pIter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
add log  
Hongze Cheng 已提交
190
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
191 192 193 194 195 196 197 198 199 200 201 202
  }
  pIter->type = TSDB_TOMB_FILE_DATA_ITER;

  pIter->tIter.pReader = pReader;
  if ((pIter->tIter.aDelIdx = taosArrayInit(0, sizeof(SDelIdx))) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }
  if ((pIter->tIter.aDelData = taosArrayInit(0, sizeof(SDelData))) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
203

H
Hongze Cheng 已提交
204 205
  code = tsdbReadDelIdx(pReader, pIter->tIter.aDelIdx);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
206

H
Hongze Cheng 已提交
207
  if (taosArrayGetSize(pIter->tIter.aDelIdx) == 0) goto _clear;
H
Hongze Cheng 已提交
208

H
Hongze Cheng 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
  pIter->tIter.iDelIdx = 0;
  pIter->tIter.iDelData = 0;

_exit:
  if (code) {
    if (pIter) {
    _clear:
      taosArrayDestroy(pIter->tIter.aDelIdx);
      taosArrayDestroy(pIter->tIter.aDelData);
      taosMemoryFree(pIter);
      pIter = NULL;
    }
  }
  *ppIter = pIter;
  return code;
}

H
Hongze Cheng 已提交
226 227
/* close */
static void tsdbCloseDataFileDataIter(STsdbDataIter2* pIter) {
228
  tBlockDataDestroy(&pIter->dIter.bData);
H
Hongze Cheng 已提交
229 230 231 232 233 234
  tMapDataClear(&pIter->dIter.mDataBlk);
  taosArrayDestroy(pIter->dIter.aBlockIdx);
  taosMemoryFree(pIter);
}

static void tsdbCloseSttFileDataIter(STsdbDataIter2* pIter) {
235
  tBlockDataDestroy(&pIter->sIter.bData);
H
Hongze Cheng 已提交
236 237 238 239
  taosArrayDestroy(pIter->sIter.aSttBlk);
  taosMemoryFree(pIter);
}

H
Hongze Cheng 已提交
240 241 242 243 244 245
static void tsdbCloseTombFileDataIter(STsdbDataIter2* pIter) {
  taosArrayDestroy(pIter->tIter.aDelData);
  taosArrayDestroy(pIter->tIter.aDelIdx);
  taosMemoryFree(pIter);
}

H
Hongze Cheng 已提交
246 247 248 249 250 251 252
static void tsdbCloseDataIter2(STsdbDataIter2* pIter) {
  if (pIter->type == TSDB_MEM_TABLE_DATA_ITER) {
    ASSERT(0);
  } else if (pIter->type == TSDB_DATA_FILE_DATA_ITER) {
    tsdbCloseDataFileDataIter(pIter);
  } else if (pIter->type == TSDB_STT_FILE_DATA_ITER) {
    tsdbCloseSttFileDataIter(pIter);
H
Hongze Cheng 已提交
253 254
  } else if (pIter->type == TSDB_TOMB_FILE_DATA_ITER) {
    tsdbCloseTombFileDataIter(pIter);
H
Hongze Cheng 已提交
255 256 257 258
  } else {
    ASSERT(0);
  }
}
H
Hongze Cheng 已提交
259

H
Hongze Cheng 已提交
260 261 262 263 264 265
/* cmpr */
static int32_t tsdbDataIterCmprFn(const SRBTreeNode* pNode1, const SRBTreeNode* pNode2) {
  STsdbDataIter2* pIter1 = TSDB_RBTN_TO_DATA_ITER(pNode1);
  STsdbDataIter2* pIter2 = TSDB_RBTN_TO_DATA_ITER(pNode2);
  return tRowInfoCmprFn(&pIter1->rowInfo, &pIter2->rowInfo);
}
H
Hongze Cheng 已提交
266

H
Hongze Cheng 已提交
267
/* seek */
H
Hongze Cheng 已提交
268

H
Hongze Cheng 已提交
269
/* iter next */
H
Hongze Cheng 已提交
270
static int32_t tsdbDataFileDataIterNext(STsdbDataIter2* pIter, STsdbFilterInfo* pFilterInfo) {
H
Hongze Cheng 已提交
271 272 273 274
  int32_t code = 0;
  int32_t lino = 0;

  for (;;) {
H
Hongze Cheng 已提交
275 276 277 278 279 280 281 282
    while (pIter->dIter.iRow < pIter->dIter.bData.nRow) {
      if (pFilterInfo) {
        if (pFilterInfo->flag & TSDB_FILTER_FLAG_BY_VERSION) {
          if (pIter->dIter.bData.aVersion[pIter->dIter.iRow] < pFilterInfo->sver ||
              pIter->dIter.bData.aVersion[pIter->dIter.iRow] > pFilterInfo->ever) {
            pIter->dIter.iRow++;
            continue;
          }
H
Hongze Cheng 已提交
283
        }
H
Hongze Cheng 已提交
284
      }
H
Hongze Cheng 已提交
285

H
Hongze Cheng 已提交
286 287 288
      pIter->rowInfo.suid = pIter->dIter.bData.suid;
      pIter->rowInfo.uid = pIter->dIter.bData.uid;
      pIter->rowInfo.row = tsdbRowFromBlockData(&pIter->dIter.bData, pIter->dIter.iRow);
H
Hongze Cheng 已提交
289
      pIter->dIter.iRow++;
H
Hongze Cheng 已提交
290
      goto _exit;
H
Hongze Cheng 已提交
291
    }
H
Hongze Cheng 已提交
292

H
Hongze Cheng 已提交
293
    for (;;) {
H
Hongze Cheng 已提交
294
      while (pIter->dIter.iDataBlk < pIter->dIter.mDataBlk.nItem) {
H
Hongze Cheng 已提交
295 296 297
        SDataBlk dataBlk;
        tMapDataGetItemByIdx(&pIter->dIter.mDataBlk, pIter->dIter.iDataBlk, &dataBlk, tGetDataBlk);

H
Hongze Cheng 已提交
298 299 300 301 302 303 304 305 306
        // filter
        if (pFilterInfo) {
          if (pFilterInfo->flag & TSDB_FILTER_FLAG_BY_VERSION) {
            if (pFilterInfo->sver > dataBlk.maxVer || pFilterInfo->ever < dataBlk.minVer) {
              pIter->dIter.iDataBlk++;
              continue;
            }
          }
        }
H
Hongze Cheng 已提交
307

H
Hongze Cheng 已提交
308 309
        code = tsdbReadDataBlockEx(pIter->dIter.pReader, &dataBlk, &pIter->dIter.bData);
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
310

H
Hongze Cheng 已提交
311 312
        pIter->dIter.iDataBlk++;
        pIter->dIter.iRow = 0;
H
Hongze Cheng 已提交
313

H
Hongze Cheng 已提交
314 315
        break;
      }
H
Hongze Cheng 已提交
316

H
Hongze Cheng 已提交
317
      if (pIter->dIter.iRow < pIter->dIter.bData.nRow) break;
H
Hongze Cheng 已提交
318

H
Hongze Cheng 已提交
319
      for (;;) {
H
Hongze Cheng 已提交
320
        if (pIter->dIter.iBlockIdx < taosArrayGetSize(pIter->dIter.aBlockIdx)) {
H
Hongze Cheng 已提交
321
          SBlockIdx* pBlockIdx = taosArrayGet(pIter->dIter.aBlockIdx, pIter->dIter.iBlockIdx);
H
Hongze Cheng 已提交
322

H
Hongze Cheng 已提交
323 324
          code = tsdbReadDataBlk(pIter->dIter.pReader, pBlockIdx, &pIter->dIter.mDataBlk);
          TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
325

H
Hongze Cheng 已提交
326 327
          pIter->dIter.iBlockIdx++;
          pIter->dIter.iDataBlk = 0;
H
Hongze Cheng 已提交
328

H
Hongze Cheng 已提交
329 330 331 332
          break;
        } else {
          pIter->rowInfo = (SRowInfo){0};
          goto _exit;
H
Hongze Cheng 已提交
333 334
        }
      }
H
Hongze Cheng 已提交
335
    }
H
Hongze Cheng 已提交
336
  }
H
Hongze Cheng 已提交
337

H
add log  
Hongze Cheng 已提交
338 339
_exit:
  if (code) {
H
Hongze Cheng 已提交
340
    tsdbError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
H
add log  
Hongze Cheng 已提交
341
  }
H
Hongze Cheng 已提交
342 343 344
  return code;
}

H
Hongze Cheng 已提交
345
static int32_t tsdbSttFileDataIterNext(STsdbDataIter2* pIter, STsdbFilterInfo* pFilterInfo) {
H
Hongze Cheng 已提交
346
  int32_t code = 0;
H
Hongze Cheng 已提交
347
  int32_t lino = 0;
H
Hongze Cheng 已提交
348

H
Hongze Cheng 已提交
349
  for (;;) {
H
Hongze Cheng 已提交
350 351 352 353 354 355 356 357
    while (pIter->sIter.iRow < pIter->sIter.bData.nRow) {
      if (pFilterInfo) {
        if (pFilterInfo->flag & TSDB_FILTER_FLAG_BY_VERSION) {
          if (pFilterInfo->sver > pIter->sIter.bData.aVersion[pIter->sIter.iRow] ||
              pFilterInfo->ever < pIter->sIter.bData.aVersion[pIter->sIter.iRow]) {
            pIter->sIter.iRow++;
            continue;
          }
H
Hongze Cheng 已提交
358
        }
H
Hongze Cheng 已提交
359
      }
H
Hongze Cheng 已提交
360

H
Hongze Cheng 已提交
361 362 363
      pIter->rowInfo.suid = pIter->sIter.bData.suid;
      pIter->rowInfo.uid = pIter->sIter.bData.uid ? pIter->sIter.bData.uid : pIter->sIter.bData.aUid[pIter->sIter.iRow];
      pIter->rowInfo.row = tsdbRowFromBlockData(&pIter->sIter.bData, pIter->sIter.iRow);
H
Hongze Cheng 已提交
364
      pIter->sIter.iRow++;
H
Hongze Cheng 已提交
365
      goto _exit;
H
Hongze Cheng 已提交
366
    }
H
Hongze Cheng 已提交
367

H
Hongze Cheng 已提交
368 369 370
    for (;;) {
      if (pIter->sIter.iSttBlk < taosArrayGetSize(pIter->sIter.aSttBlk)) {
        SSttBlk* pSttBlk = taosArrayGet(pIter->sIter.aSttBlk, pIter->sIter.iSttBlk);
H
Hongze Cheng 已提交
371

H
Hongze Cheng 已提交
372 373 374 375 376 377
        if (pFilterInfo) {
          if (pFilterInfo->flag & TSDB_FILTER_FLAG_BY_VERSION) {
            if (pFilterInfo->sver > pSttBlk->maxVer || pFilterInfo->ever < pSttBlk->minVer) {
              pIter->sIter.iSttBlk++;
              continue;
            }
H
Hongze Cheng 已提交
378
          }
H
Hongze Cheng 已提交
379
        }
H
Hongze Cheng 已提交
380

H
Hongze Cheng 已提交
381 382
        code = tsdbReadSttBlockEx(pIter->sIter.pReader, pIter->sIter.iStt, pSttBlk, &pIter->sIter.bData);
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
383

H
Hongze Cheng 已提交
384 385
        pIter->sIter.iRow = 0;
        pIter->sIter.iSttBlk++;
H
Hongze Cheng 已提交
386
        break;
H
Hongze Cheng 已提交
387 388 389 390
      } else {
        pIter->rowInfo = (SRowInfo){0};
        goto _exit;
      }
H
Hongze Cheng 已提交
391 392
    }
  }
H
Hongze Cheng 已提交
393

H
Hongze Cheng 已提交
394 395 396 397 398 399
_exit:
  if (code) {
    tsdbError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
  }
  return code;
}
H
Hongze Cheng 已提交
400

H
Hongze Cheng 已提交
401 402 403
static int32_t tsdbTombFileDataIterNext(STsdbDataIter2* pIter, STsdbFilterInfo* pFilterInfo) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
404

H
Hongze Cheng 已提交
405 406 407 408 409 410 411 412 413 414
  for (;;) {
    while (pIter->tIter.iDelData < taosArrayGetSize(pIter->tIter.aDelData)) {
      SDelData* pDelData = taosArrayGet(pIter->tIter.aDelData, pIter->tIter.iDelData);

      if (pFilterInfo) {
        if (pFilterInfo->flag & TSDB_FILTER_FLAG_BY_VERSION) {
          if (pFilterInfo->sver > pDelData->version || pFilterInfo->ever < pDelData->version) {
            pIter->tIter.iDelData++;
            continue;
          }
H
Hongze Cheng 已提交
415
        }
H
Hongze Cheng 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428
      }

      pIter->delInfo.delData = *pDelData;
      pIter->tIter.iDelData++;
      goto _exit;
    }

    for (;;) {
      if (pIter->tIter.iDelIdx < taosArrayGetSize(pIter->tIter.aDelIdx)) {
        SDelIdx* pDelIdx = taosArrayGet(pIter->tIter.aDelIdx, pIter->tIter.iDelIdx);

        code = tsdbReadDelData(pIter->tIter.pReader, pDelIdx, pIter->tIter.aDelData);
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
429

H
Hongze Cheng 已提交
430 431 432 433
        pIter->delInfo.suid = pDelIdx->suid;
        pIter->delInfo.uid = pDelIdx->uid;
        pIter->tIter.iDelData = 0;
        pIter->tIter.iDelIdx++;
H
Hongze Cheng 已提交
434
        break;
H
Hongze Cheng 已提交
435
      } else {
H
Hongze Cheng 已提交
436 437
        pIter->delInfo = (SDelInfo){0};
        goto _exit;
H
Hongze Cheng 已提交
438 439
      }
    }
H
Hongze Cheng 已提交
440
  }
H
Hongze Cheng 已提交
441

H
Hongze Cheng 已提交
442 443 444 445 446 447 448
_exit:
  if (code) {
    tsdbError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
  }
  return code;
}

H
Hongze Cheng 已提交
449
static int32_t tsdbDataIterNext2(STsdbDataIter2* pIter, STsdbFilterInfo* pFilterInfo) {
H
Hongze Cheng 已提交
450 451 452 453 454 455
  int32_t code = 0;

  if (pIter->type == TSDB_MEM_TABLE_DATA_ITER) {
    ASSERT(0);
    return code;
  } else if (pIter->type == TSDB_DATA_FILE_DATA_ITER) {
H
Hongze Cheng 已提交
456
    return tsdbDataFileDataIterNext(pIter, pFilterInfo);
H
Hongze Cheng 已提交
457
  } else if (pIter->type == TSDB_STT_FILE_DATA_ITER) {
H
Hongze Cheng 已提交
458
    return tsdbSttFileDataIterNext(pIter, pFilterInfo);
H
Hongze Cheng 已提交
459 460
  } else if (pIter->type == TSDB_TOMB_FILE_DATA_ITER) {
    return tsdbTombFileDataIterNext(pIter, pFilterInfo);
H
Hongze Cheng 已提交
461 462 463 464 465 466 467 468
  } else {
    ASSERT(0);
    return code;
  }
}

/* get */

H
Hongze Cheng 已提交
469
// STsdbSnapReader ========================================
H
Hongze Cheng 已提交
470
struct STsdbSnapReader {
H
Hongze Cheng 已提交
471 472 473 474 475
  STsdb*   pTsdb;
  int64_t  sver;
  int64_t  ever;
  int8_t   type;
  uint8_t* aBuf[5];
H
Hongze Cheng 已提交
476

H
Hongze Cheng 已提交
477
  STsdbFS  fs;
H
Hongze Cheng 已提交
478
  TABLEID  tbid;
H
Hongze Cheng 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
  SSkmInfo skmTable;

  // timeseries data
  int8_t  dataDone;
  int32_t fid;

  SDataFReader*   pDataFReader;
  STsdbDataIter2* iterList;
  STsdbDataIter2* pIter;
  SRBTree         rbt;
  SBlockData      bData;

  // tombstone data
  int8_t          delDone;
  SDelFReader*    pDelFReader;
  STsdbDataIter2* pTIter;
H
Hongze Cheng 已提交
495
  SArray*         aDelData;
H
Hongze Cheng 已提交
496
};
H
Hongze Cheng 已提交
497

H
Hongze Cheng 已提交
498
static int32_t tsdbSnapReadFileDataStart(STsdbSnapReader* pReader) {
H
Hongze Cheng 已提交
499
  int32_t code = 0;
H
add log  
Hongze Cheng 已提交
500
  int32_t lino = 0;
H
Hongze Cheng 已提交
501

H
Hongze Cheng 已提交
502 503 504 505 506
  SDFileSet* pSet = taosArraySearch(pReader->fs.aDFileSet, &(SDFileSet){.fid = pReader->fid}, tDFileSetCmprFn, TD_GT);
  if (pSet == NULL) {
    pReader->fid = INT32_MAX;
    goto _exit;
  }
H
Hongze Cheng 已提交
507

H
Hongze Cheng 已提交
508 509
  pReader->fid = pSet->fid;

H
Hongze Cheng 已提交
510
  tRBTreeCreate(&pReader->rbt, tsdbDataIterCmprFn);
H
Hongze Cheng 已提交
511

H
Hongze Cheng 已提交
512
  code = tsdbDataFReaderOpen(&pReader->pDataFReader, pReader->pTsdb, pSet);
H
add log  
Hongze Cheng 已提交
513
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
514

H
Hongze Cheng 已提交
515 516
  code = tsdbOpenDataFileDataIter(pReader->pDataFReader, &pReader->pIter);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
517

H
Hongze Cheng 已提交
518 519 520 521 522 523
  if (pReader->pIter) {
    // iter to next with filter info (sver, ever)
    code = tsdbDataIterNext2(pReader->pIter,
                             &(STsdbFilterInfo){.flag = TSDB_FILTER_FLAG_BY_VERSION,  // flag
                                                .sver = pReader->sver,
                                                .ever = pReader->ever});
H
add log  
Hongze Cheng 已提交
524
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
525

H
Hongze Cheng 已提交
526 527 528
    if (pReader->pIter->rowInfo.suid || pReader->pIter->rowInfo.uid) {
      // add to rbtree
      tRBTreePut(&pReader->rbt, &pReader->pIter->rbtn);
H
Hongze Cheng 已提交
529

H
Hongze Cheng 已提交
530 531 532 533 534
      // add to iterList
      pReader->pIter->next = pReader->iterList;
      pReader->iterList = pReader->pIter;
    } else {
      tsdbCloseDataIter2(pReader->pIter);
H
Hongze Cheng 已提交
535 536 537
    }
  }

H
Hongze Cheng 已提交
538 539
  for (int32_t iStt = 0; iStt < pSet->nSttF; ++iStt) {
    code = tsdbOpenSttFileDataIter(pReader->pDataFReader, iStt, &pReader->pIter);
H
add log  
Hongze Cheng 已提交
540
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
541 542

    if (pReader->pIter) {
H
Hongze Cheng 已提交
543 544 545 546 547
      // iter to valid row
      code = tsdbDataIterNext2(pReader->pIter,
                               &(STsdbFilterInfo){.flag = TSDB_FILTER_FLAG_BY_VERSION,  // flag
                                                  .sver = pReader->sver,
                                                  .ever = pReader->ever});
H
add log  
Hongze Cheng 已提交
548
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
549

H
Hongze Cheng 已提交
550 551 552
      if (pReader->pIter->rowInfo.suid || pReader->pIter->rowInfo.uid) {
        // add to rbtree
        tRBTreePut(&pReader->rbt, &pReader->pIter->rbtn);
H
Hongze Cheng 已提交
553

H
Hongze Cheng 已提交
554 555 556 557 558
        // add to iterList
        pReader->pIter->next = pReader->iterList;
        pReader->iterList = pReader->pIter;
      } else {
        tsdbCloseDataIter2(pReader->pIter);
H
Hongze Cheng 已提交
559 560 561 562
      }
    }
  }

H
Hongze Cheng 已提交
563
  pReader->pIter = NULL;
H
Hongze Cheng 已提交
564

H
add log  
Hongze Cheng 已提交
565 566
_exit:
  if (code) {
H
Hongze Cheng 已提交
567
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pReader->pTsdb->pVnode), __func__, lino, tstrerror(code));
H
add log  
Hongze Cheng 已提交
568
  } else {
H
Hongze Cheng 已提交
569
    tsdbInfo("vgId:%d %s done, fid:%d", TD_VID(pReader->pTsdb->pVnode), __func__, pReader->fid);
H
add log  
Hongze Cheng 已提交
570
  }
H
Hongze Cheng 已提交
571 572 573
  return code;
}

H
Hongze Cheng 已提交
574 575 576 577 578 579
static void tsdbSnapReadFileDataEnd(STsdbSnapReader* pReader) {
  while (pReader->iterList) {
    STsdbDataIter2* pIter = pReader->iterList;
    pReader->iterList = pIter->next;
    tsdbCloseDataIter2(pIter);
  }
H
Hongze Cheng 已提交
580

H
Hongze Cheng 已提交
581 582
  tsdbDataFReaderClose(&pReader->pDataFReader);
}
H
Hongze Cheng 已提交
583

H
Hongze Cheng 已提交
584 585 586
static int32_t tsdbSnapReadNextRow(STsdbSnapReader* pReader, SRowInfo** ppRowInfo) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
587

H
Hongze Cheng 已提交
588
  if (pReader->pIter) {
H
Hongze Cheng 已提交
589 590 591 592
    code = tsdbDataIterNext2(pReader->pIter, &(STsdbFilterInfo){.flag = TSDB_FILTER_FLAG_BY_VERSION,  // flag
                                                                .sver = pReader->sver,
                                                                .ever = pReader->ever});
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
593

H
Hongze Cheng 已提交
594 595 596 597 598 599 600 601 602 603 604
    if (pReader->pIter->rowInfo.suid == 0 && pReader->pIter->rowInfo.uid == 0) {
      pReader->pIter = NULL;
    } else {
      SRBTreeNode* pNode = tRBTreeMin(&pReader->rbt);
      if (pNode) {
        int32_t c = tsdbDataIterCmprFn(&pReader->pIter->rbtn, pNode);
        if (c > 0) {
          tRBTreePut(&pReader->rbt, &pReader->pIter->rbtn);
          pReader->pIter = NULL;
        } else if (c == 0) {
          ASSERT(0);
H
Hongze Cheng 已提交
605
        }
H
Hongze Cheng 已提交
606 607
      }
    }
H
Hongze Cheng 已提交
608
  }
H
Hongze Cheng 已提交
609

H
Hongze Cheng 已提交
610 611 612 613 614
  if (pReader->pIter == NULL) {
    SRBTreeNode* pNode = tRBTreeMin(&pReader->rbt);
    if (pNode) {
      tRBTreeDrop(&pReader->rbt, pNode);
      pReader->pIter = TSDB_RBTN_TO_DATA_ITER(pNode);
H
Hongze Cheng 已提交
615 616
    }
  }
H
Hongze Cheng 已提交
617

H
Hongze Cheng 已提交
618
  if (ppRowInfo) {
H
Hongze Cheng 已提交
619
    if (pReader->pIter) {
H
Hongze Cheng 已提交
620
      *ppRowInfo = &pReader->pIter->rowInfo;
H
Hongze Cheng 已提交
621
    } else {
H
Hongze Cheng 已提交
622
      *ppRowInfo = NULL;
H
Hongze Cheng 已提交
623 624
    }
  }
H
Hongze Cheng 已提交
625

H
Hongze Cheng 已提交
626 627 628 629
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pReader->pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
H
Hongze Cheng 已提交
630 631 632
  return code;
}

H
Hongze Cheng 已提交
633
static int32_t tsdbSnapReadGetRow(STsdbSnapReader* pReader, SRowInfo** ppRowInfo) {
H
Hongze Cheng 已提交
634
  if (pReader->pIter) {
H
Hongze Cheng 已提交
635 636
    *ppRowInfo = &pReader->pIter->rowInfo;
    return 0;
H
Hongze Cheng 已提交
637
  }
H
Hongze Cheng 已提交
638 639

  return tsdbSnapReadNextRow(pReader, ppRowInfo);
H
Hongze Cheng 已提交
640 641
}

H
Hongze Cheng 已提交
642 643 644
static int32_t tsdbSnapCmprData(STsdbSnapReader* pReader, uint8_t** ppData) {
  int32_t code = 0;

645
  ASSERT(pReader->bData.nRow);
H
Hongze Cheng 已提交
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674

  int32_t aBufN[5] = {0};
  code = tCmprBlockData(&pReader->bData, TWO_STAGE_COMP, NULL, NULL, pReader->aBuf, aBufN);
  if (code) goto _exit;

  int32_t size = aBufN[0] + aBufN[1] + aBufN[2] + aBufN[3];
  *ppData = taosMemoryMalloc(sizeof(SSnapDataHdr) + size);
  if (*ppData == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

  SSnapDataHdr* pHdr = (SSnapDataHdr*)*ppData;
  pHdr->type = SNAP_DATA_TSDB;
  pHdr->size = size;

  memcpy(pHdr->data, pReader->aBuf[3], aBufN[3]);
  memcpy(pHdr->data + aBufN[3], pReader->aBuf[2], aBufN[2]);
  if (aBufN[1]) {
    memcpy(pHdr->data + aBufN[3] + aBufN[2], pReader->aBuf[1], aBufN[1]);
  }
  if (aBufN[0]) {
    memcpy(pHdr->data + aBufN[3] + aBufN[2] + aBufN[1], pReader->aBuf[0], aBufN[0]);
  }

_exit:
  return code;
}

H
Hongze Cheng 已提交
675
static int32_t tsdbSnapReadTimeSeriesData(STsdbSnapReader* pReader, uint8_t** ppData) {
H
Hongze Cheng 已提交
676
  int32_t code = 0;
H
add log  
Hongze Cheng 已提交
677 678 679
  int32_t lino = 0;

  STsdb* pTsdb = pReader->pTsdb;
H
Hongze Cheng 已提交
680

H
Hongze Cheng 已提交
681
  tBlockDataReset(&pReader->bData);
H
Hongze Cheng 已提交
682 683 684

  for (;;) {
    // start a new file read if need
H
Hongze Cheng 已提交
685
    if (pReader->pDataFReader == NULL) {
H
Hongze Cheng 已提交
686
      code = tsdbSnapReadFileDataStart(pReader);
H
add log  
Hongze Cheng 已提交
687
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
688 689 690 691
    }

    if (pReader->pDataFReader == NULL) break;

H
Hongze Cheng 已提交
692 693 694 695
    SRowInfo* pRowInfo;
    code = tsdbSnapReadGetRow(pReader, &pRowInfo);
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
696
    if (pRowInfo == NULL) {
H
Hongze Cheng 已提交
697
      tsdbSnapReadFileDataEnd(pReader);
H
Hongze Cheng 已提交
698 699 700
      continue;
    }

H
Hongze Cheng 已提交
701
    code = tsdbUpdateTableSchema(pTsdb->pVnode->pMeta, pRowInfo->suid, pRowInfo->uid, &pReader->skmTable);
H
add log  
Hongze Cheng 已提交
702
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
703

H
Hongze Cheng 已提交
704
    code = tBlockDataInit(&pReader->bData, (TABLEID*)pRowInfo, pReader->skmTable.pTSchema, NULL, 0);
H
add log  
Hongze Cheng 已提交
705
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
706

H
Hongze Cheng 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720
    do {
      if (!TABLE_SAME_SCHEMA(pReader->bData.suid, pReader->bData.uid, pRowInfo->suid, pRowInfo->uid)) break;

      if (pReader->bData.uid && pReader->bData.uid != pRowInfo->uid) {
        code = tRealloc((uint8_t**)&pReader->bData.aUid, sizeof(int64_t) * (pReader->bData.nRow + 1));
        TSDB_CHECK_CODE(code, lino, _exit);

        for (int32_t iRow = 0; iRow < pReader->bData.nRow; ++iRow) {
          pReader->bData.aUid[iRow] = pReader->bData.uid;
        }
        pReader->bData.uid = 0;
      }

      code = tBlockDataAppendRow(&pReader->bData, &pRowInfo->row, NULL, pRowInfo->uid);
H
add log  
Hongze Cheng 已提交
721
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
722

H
Hongze Cheng 已提交
723
      code = tsdbSnapReadNextRow(pReader, &pRowInfo);
H
add log  
Hongze Cheng 已提交
724
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
725

H
Hongze Cheng 已提交
726 727
      if (pReader->bData.nRow >= 4096) break;
    } while (pRowInfo);
H
Hongze Cheng 已提交
728

H
Hongze Cheng 已提交
729 730 731 732
    ASSERT(pReader->bData.nRow > 0);

    break;
  }
H
Hongze Cheng 已提交
733

H
Hongze Cheng 已提交
734
  if (pReader->bData.nRow > 0) {
H
Hongze Cheng 已提交
735 736
    ASSERT(pReader->bData.suid || pReader->bData.uid);

H
Hongze Cheng 已提交
737
    code = tsdbSnapCmprData(pReader, ppData);
H
add log  
Hongze Cheng 已提交
738
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
739 740
  }

H
add log  
Hongze Cheng 已提交
741 742
_exit:
  if (code) {
H
Hongze Cheng 已提交
743
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
add log  
Hongze Cheng 已提交
744
  }
H
Hongze Cheng 已提交
745 746 747
  return code;
}

H
Hongze Cheng 已提交
748
static int32_t tsdbSnapCmprTombData(STsdbSnapReader* pReader, uint8_t** ppData) {
H
add log  
Hongze Cheng 已提交
749 750 751
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
752 753 754 755
  int64_t size = sizeof(TABLEID);
  for (int32_t iDelData = 0; iDelData < taosArrayGetSize(pReader->aDelData); ++iDelData) {
    size += tPutDelData(NULL, taosArrayGet(pReader->aDelData, iDelData));
  }
H
Hongze Cheng 已提交
756

H
Hongze Cheng 已提交
757 758 759
  uint8_t* pData = (uint8_t*)taosMemoryMalloc(sizeof(SSnapDataHdr) + size);
  if (pData == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
add log  
Hongze Cheng 已提交
760
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
761
  }
H
Hongze Cheng 已提交
762

H
Hongze Cheng 已提交
763 764 765 766 767 768
  SSnapDataHdr* pHdr = (SSnapDataHdr*)pData;
  pHdr->type = SNAP_DATA_DEL;
  pHdr->size = size;

  TABLEID* pId = (TABLEID*)(pData + sizeof(SSnapDataHdr));
  *pId = pReader->tbid;
H
Hongze Cheng 已提交
769

H
Hongze Cheng 已提交
770 771 772
  size = sizeof(SSnapDataHdr) + sizeof(TABLEID);
  for (int32_t iDelData = 0; iDelData < taosArrayGetSize(pReader->aDelData); ++iDelData) {
    size += tPutDelData(pData + size, taosArrayGet(pReader->aDelData, iDelData));
H
Hongze Cheng 已提交
773 774
  }

H
Hongze Cheng 已提交
775 776 777 778 779 780
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pReader->pTsdb->pVnode), __func__, lino, tstrerror(code));
    if (pData) {
      taosMemoryFree(pData);
      pData = NULL;
H
Hongze Cheng 已提交
781
    }
H
Hongze Cheng 已提交
782 783 784 785
  }
  *ppData = pData;
  return code;
}
H
Hongze Cheng 已提交
786

H
Hongze Cheng 已提交
787 788 789 790 791
static void tsdbSnapReadGetTombData(STsdbSnapReader* pReader, SDelInfo** ppDelInfo) {
  if (pReader->pTIter == NULL || (pReader->pTIter->delInfo.suid == 0 && pReader->pTIter->delInfo.uid == 0)) {
    *ppDelInfo = NULL;
  } else {
    *ppDelInfo = &pReader->pTIter->delInfo;
H
Hongze Cheng 已提交
792 793
  }
}
H
Hongze Cheng 已提交
794

H
Hongze Cheng 已提交
795
static int32_t tsdbSnapReadNextTombData(STsdbSnapReader* pReader, SDelInfo** ppDelInfo) {
H
Hongze Cheng 已提交
796 797
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
798

H
Hongze Cheng 已提交
799 800 801 802 803 804 805
  code = tsdbDataIterNext2(
      pReader->pTIter,
      &(STsdbFilterInfo){.flag = TSDB_FILTER_FLAG_BY_VERSION, .sver = pReader->sver, .ever = pReader->ever});
  TSDB_CHECK_CODE(code, lino, _exit);

  if (ppDelInfo) {
    tsdbSnapReadGetTombData(pReader, ppDelInfo);
H
Hongze Cheng 已提交
806 807
  }

H
add log  
Hongze Cheng 已提交
808 809
_exit:
  if (code) {
H
Hongze Cheng 已提交
810
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pReader->pTsdb->pVnode), __func__, lino, tstrerror(code));
H
add log  
Hongze Cheng 已提交
811
  }
H
Hongze Cheng 已提交
812 813 814
  return code;
}

H
Hongze Cheng 已提交
815
static int32_t tsdbSnapReadTombData(STsdbSnapReader* pReader, uint8_t** ppData) {
H
add log  
Hongze Cheng 已提交
816 817 818
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
819
  STsdb* pTsdb = pReader->pTsdb;
H
Hongze Cheng 已提交
820

H
Hongze Cheng 已提交
821
  // open tombstone data iter if need
H
Hongze Cheng 已提交
822
  if (pReader->pDelFReader == NULL) {
H
Hongze Cheng 已提交
823
    if (pReader->fs.pDelFile == NULL) goto _exit;
H
Hongze Cheng 已提交
824 825

    // open
H
Hongze Cheng 已提交
826
    code = tsdbDelFReaderOpen(&pReader->pDelFReader, pReader->fs.pDelFile, pTsdb);
H
add log  
Hongze Cheng 已提交
827
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
828

H
Hongze Cheng 已提交
829
    code = tsdbOpenTombFileDataIter(pReader->pDelFReader, &pReader->pTIter);
H
add log  
Hongze Cheng 已提交
830
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
831

H
Hongze Cheng 已提交
832 833 834 835
    if (pReader->pTIter) {
      code = tsdbSnapReadNextTombData(pReader, NULL);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
836 837
  }

H
Hongze Cheng 已提交
838 839 840
  // loop to get tombstone data
  SDelInfo* pDelInfo;
  tsdbSnapReadGetTombData(pReader, &pDelInfo);
H
Hongze Cheng 已提交
841

H
Hongze Cheng 已提交
842
  if (pDelInfo == NULL) goto _exit;
H
Hongze Cheng 已提交
843

H
Hongze Cheng 已提交
844
  pReader->tbid = *(TABLEID*)pDelInfo;
H
Hongze Cheng 已提交
845

H
Hongze Cheng 已提交
846 847 848 849
  if (pReader->aDelData) {
    taosArrayClear(pReader->aDelData);
  } else if ((pReader->aDelData = taosArrayInit(16, sizeof(SDelData))) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
add log  
Hongze Cheng 已提交
850
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
851
  }
H
Hongze Cheng 已提交
852

H
Hongze Cheng 已提交
853
  while (pDelInfo && pDelInfo->suid == pReader->tbid.suid && pDelInfo->uid == pReader->tbid.uid) {
H
Hongze Cheng 已提交
854
    if (taosArrayPush(pReader->aDelData, &pDelInfo->delData) < 0) {
H
Hongze Cheng 已提交
855
      code = TSDB_CODE_OUT_OF_MEMORY;
H
add log  
Hongze Cheng 已提交
856
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
857
    }
H
Hongze Cheng 已提交
858

H
Hongze Cheng 已提交
859 860 861
    code = tsdbSnapReadNextTombData(pReader, &pDelInfo);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
862

H
Hongze Cheng 已提交
863
  // encode tombstone data
H
Hongze Cheng 已提交
864 865 866
  if (taosArrayGetSize(pReader->aDelData) > 0) {
    code = tsdbSnapCmprTombData(pReader, ppData);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
867
  }
H
Hongze Cheng 已提交
868 869

_exit:
H
add log  
Hongze Cheng 已提交
870
  if (code) {
H
Hongze Cheng 已提交
871 872 873
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
  } else {
    tsdbDebug("vgId:%d %s done", TD_VID(pTsdb->pVnode), __func__);
H
add log  
Hongze Cheng 已提交
874
  }
H
Hongze Cheng 已提交
875 876
  return code;
}
H
more  
Hongze Cheng 已提交
877

C
Cary Xu 已提交
878
int32_t tsdbSnapReaderOpen(STsdb* pTsdb, int64_t sver, int64_t ever, int8_t type, STsdbSnapReader** ppReader) {
H
Hongze Cheng 已提交
879 880
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
881

H
more  
Hongze Cheng 已提交
882
  // alloc
H
Hongze Cheng 已提交
883
  STsdbSnapReader* pReader = (STsdbSnapReader*)taosMemoryCalloc(1, sizeof(*pReader));
H
more  
Hongze Cheng 已提交
884 885
  if (pReader == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
886
    TSDB_CHECK_CODE(code, lino, _exit);
H
more  
Hongze Cheng 已提交
887 888 889 890
  }
  pReader->pTsdb = pTsdb;
  pReader->sver = sver;
  pReader->ever = ever;
C
Cary Xu 已提交
891
  pReader->type = type;
H
more  
Hongze Cheng 已提交
892

H
Hongze Cheng 已提交
893
  taosThreadRwlockRdlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
894 895 896
  code = tsdbFSRef(pTsdb, &pReader->fs);
  if (code) {
    taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
897
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
898
  }
H
Hongze Cheng 已提交
899
  taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
900

H
Hongze Cheng 已提交
901
  // init
H
Hongze Cheng 已提交
902
  pReader->fid = INT32_MIN;
H
Hongze Cheng 已提交
903 904

  code = tBlockDataCreate(&pReader->bData);
H
Hongze Cheng 已提交
905
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
906

H
Hongze Cheng 已提交
907 908
_exit:
  if (code) {
H
Hongze Cheng 已提交
909 910
    tsdbError("vgId:%d %s failed at line %d since %s, sver:%" PRId64 " ever:%" PRId64 " type:%d", TD_VID(pTsdb->pVnode),
              __func__, lino, tstrerror(code), sver, ever, type);
H
Hongze Cheng 已提交
911
    if (pReader) {
H
Hongze Cheng 已提交
912
      tBlockDataDestroy(&pReader->bData);
H
Hongze Cheng 已提交
913
      tsdbFSUnref(pTsdb, &pReader->fs);
H
Hongze Cheng 已提交
914
      taosMemoryFree(pReader);
H
Hongze Cheng 已提交
915
      pReader = NULL;
H
Hongze Cheng 已提交
916 917
    }
  } else {
H
Hongze Cheng 已提交
918 919
    tsdbInfo("vgId:%d %s done, sver:%" PRId64 " ever:%" PRId64 " type:%d", TD_VID(pTsdb->pVnode), __func__, sver, ever,
             type);
H
Hongze Cheng 已提交
920
  }
H
Hongze Cheng 已提交
921
  *ppReader = pReader;
H
more  
Hongze Cheng 已提交
922
  return code;
H
Hongze Cheng 已提交
923 924
}

H
Hongze Cheng 已提交
925
int32_t tsdbSnapReaderClose(STsdbSnapReader** ppReader) {
H
Hongze Cheng 已提交
926 927
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
928

H
Hongze Cheng 已提交
929
  STsdbSnapReader* pReader = *ppReader;
H
Hongze Cheng 已提交
930
  STsdb*           pTsdb = pReader->pTsdb;
H
Hongze Cheng 已提交
931

H
Hongze Cheng 已提交
932 933 934 935
  // tombstone
  if (pReader->pTIter) {
    tsdbCloseDataIter2(pReader->pTIter);
    pReader->pTIter = NULL;
H
Hongze Cheng 已提交
936
  }
H
Hongze Cheng 已提交
937 938
  if (pReader->pDelFReader) {
    tsdbDelFReaderClose(&pReader->pDelFReader);
H
Hongze Cheng 已提交
939
  }
H
Hongze Cheng 已提交
940
  taosArrayDestroy(pReader->aDelData);
H
Hongze Cheng 已提交
941

H
Hongze Cheng 已提交
942 943 944 945 946 947 948 949 950
  // timeseries
  while (pReader->iterList) {
    STsdbDataIter2* pIter = pReader->iterList;
    pReader->iterList = pIter->next;
    tsdbCloseDataIter2(pIter);
  }
  if (pReader->pDataFReader) {
    tsdbDataFReaderClose(&pReader->pDataFReader);
  }
H
Hongze Cheng 已提交
951
  tBlockDataDestroy(&pReader->bData);
H
Hongze Cheng 已提交
952

H
Hongze Cheng 已提交
953 954
  // other
  tDestroyTSchema(pReader->skmTable.pTSchema);
H
Hongze Cheng 已提交
955
  tsdbFSUnref(pReader->pTsdb, &pReader->fs);
H
Hongze Cheng 已提交
956 957 958
  for (int32_t iBuf = 0; iBuf < sizeof(pReader->aBuf) / sizeof(pReader->aBuf[0]); iBuf++) {
    tFree(pReader->aBuf[iBuf]);
  }
H
Hongze Cheng 已提交
959
  taosMemoryFree(pReader);
H
Hongze Cheng 已提交
960 961 962

_exit:
  if (code) {
H
Hongze Cheng 已提交
963
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
964
  } else {
H
Hongze Cheng 已提交
965
    tsdbDebug("vgId:%d %s done", TD_VID(pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
966
  }
H
Hongze Cheng 已提交
967
  *ppReader = NULL;
H
Hongze Cheng 已提交
968 969 970 971
  return code;
}

int32_t tsdbSnapRead(STsdbSnapReader* pReader, uint8_t** ppData) {
H
more  
Hongze Cheng 已提交
972
  int32_t code = 0;
H
add log  
Hongze Cheng 已提交
973
  int32_t lino = 0;
H
Hongze Cheng 已提交
974

H
Hongze Cheng 已提交
975 976
  *ppData = NULL;

H
Hongze Cheng 已提交
977
  // read data file
H
Hongze Cheng 已提交
978
  if (!pReader->dataDone) {
H
Hongze Cheng 已提交
979
    code = tsdbSnapReadTimeSeriesData(pReader, ppData);
H
add log  
Hongze Cheng 已提交
980 981 982
    TSDB_CHECK_CODE(code, lino, _exit);
    if (*ppData) {
      goto _exit;
H
Hongze Cheng 已提交
983
    } else {
H
add log  
Hongze Cheng 已提交
984
      pReader->dataDone = 1;
H
Hongze Cheng 已提交
985 986
    }
  }
H
Hongze Cheng 已提交
987 988

  // read del file
H
Hongze Cheng 已提交
989
  if (!pReader->delDone) {
H
Hongze Cheng 已提交
990
    code = tsdbSnapReadTombData(pReader, ppData);
H
add log  
Hongze Cheng 已提交
991 992 993
    TSDB_CHECK_CODE(code, lino, _exit);
    if (*ppData) {
      goto _exit;
H
Hongze Cheng 已提交
994
    } else {
H
add log  
Hongze Cheng 已提交
995
      pReader->delDone = 1;
H
Hongze Cheng 已提交
996 997
    }
  }
H
Hongze Cheng 已提交
998

H
Hongze Cheng 已提交
999
_exit:
H
add log  
Hongze Cheng 已提交
1000
  if (code) {
H
Hongze Cheng 已提交
1001
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pReader->pTsdb->pVnode), __func__, lino, tstrerror(code));
H
add log  
Hongze Cheng 已提交
1002
  } else {
H
Hongze Cheng 已提交
1003
    tsdbDebug("vgId:%d %s done", TD_VID(pReader->pTsdb->pVnode), __func__);
H
add log  
Hongze Cheng 已提交
1004
  }
H
more  
Hongze Cheng 已提交
1005 1006 1007
  return code;
}

H
Hongze Cheng 已提交
1008
// STsdbSnapWriter ========================================
H
Hongze Cheng 已提交
1009
struct STsdbSnapWriter {
H
Hongze Cheng 已提交
1010 1011 1012
  STsdb*   pTsdb;
  int64_t  sver;
  int64_t  ever;
H
Hongze Cheng 已提交
1013 1014 1015 1016 1017 1018
  int32_t  minutes;
  int8_t   precision;
  int32_t  minRow;
  int32_t  maxRow;
  int8_t   cmprAlg;
  int64_t  commitID;
H
Hongze Cheng 已提交
1019
  uint8_t* aBuf[5];
H
Hongze Cheng 已提交
1020

H
Hongze Cheng 已提交
1021
  STsdbFS fs;
H
Hongze Cheng 已提交
1022
  TABLEID tbid;
H
Hongze Cheng 已提交
1023

H
Hongze Cheng 已提交
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
  // time-series data
  SBlockData inData;

  int32_t  fid;
  SSkmInfo skmTable;

  /* reader */
  SDataFReader*   pDataFReader;
  STsdbDataIter2* iterList;
  STsdbDataIter2* pDIter;
H
Hongze Cheng 已提交
1034
  STsdbDataIter2* pSIter;
H
Hongze Cheng 已提交
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
  SRBTree         rbt;  // SRBTree<STsdbDataIter2>

  /* writer */
  SDataFWriter* pDataFWriter;
  SArray*       aBlockIdx;
  SMapData      mDataBlk;  // SMapData<SDataBlk>
  SArray*       aSttBlk;   // SArray<SSttBlk>
  SBlockData    bData;
  SBlockData    sData;

  // tombstone data
H
Hongze Cheng 已提交
1046 1047 1048 1049 1050
  /* reader */
  SDelFReader*    pDelFReader;
  STsdbDataIter2* pTIter;

  /* writer */
H
Hongze Cheng 已提交
1051
  SDelFWriter* pDelFWriter;
H
Hongze Cheng 已提交
1052
  SArray*      aDelIdx;
H
Hongze Cheng 已提交
1053
  SArray*      aDelData;
H
Hongze Cheng 已提交
1054 1055
};

H
Hongze Cheng 已提交
1056
// SNAP_DATA_TSDB
H
Hongze Cheng 已提交
1057
static int32_t tsdbSnapWriteTableDataStart(STsdbSnapWriter* pWriter, TABLEID* pId) {
H
Hongze Cheng 已提交
1058
  int32_t code = 0;
H
Hongze Cheng 已提交
1059 1060
  int32_t lino = 0;

H
Hongze Cheng 已提交
1061 1062 1063 1064 1065 1066
  if (pId) {
    pWriter->tbid = *pId;
  } else {
    pWriter->tbid = (TABLEID){INT64_MAX, INT64_MAX};
  }

H
Hongze Cheng 已提交
1067 1068
  if (pWriter->pDIter) {
    STsdbDataIter2* pIter = pWriter->pDIter;
H
Hongze Cheng 已提交
1069

H
Hongze Cheng 已提交
1070
    // assert last table data end
H
Hongze Cheng 已提交
1071 1072 1073
    ASSERT(pIter->dIter.iRow >= pIter->dIter.bData.nRow);
    ASSERT(pIter->dIter.iDataBlk >= pIter->dIter.mDataBlk.nItem);

H
Hongze Cheng 已提交
1074
    for (;;) {
H
Hongze Cheng 已提交
1075
      if (pIter->dIter.iBlockIdx >= taosArrayGetSize(pIter->dIter.aBlockIdx)) {
H
Hongze Cheng 已提交
1076 1077 1078 1079
        pWriter->pDIter = NULL;
        break;
      }

H
Hongze Cheng 已提交
1080
      SBlockIdx* pBlockIdx = (SBlockIdx*)taosArrayGet(pIter->dIter.aBlockIdx, pIter->dIter.iBlockIdx);
H
Hongze Cheng 已提交
1081

H
Hongze Cheng 已提交
1082
      int32_t c = tTABLEIDCmprFn(pBlockIdx, &pWriter->tbid);
H
Hongze Cheng 已提交
1083 1084 1085 1086 1087 1088
      if (c < 0) {
        code = tsdbReadDataBlk(pIter->dIter.pReader, pBlockIdx, &pIter->dIter.mDataBlk);
        TSDB_CHECK_CODE(code, lino, _exit);

        SBlockIdx* pNewBlockIdx = taosArrayReserve(pWriter->aBlockIdx, 1);
        if (pNewBlockIdx == NULL) {
H
Hongze Cheng 已提交
1089
          code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1090 1091 1092 1093 1094 1095
          TSDB_CHECK_CODE(code, lino, _exit);
        }

        pNewBlockIdx->suid = pBlockIdx->suid;
        pNewBlockIdx->uid = pBlockIdx->uid;

H
Hongze Cheng 已提交
1096
        code = tsdbWriteDataBlk(pWriter->pDataFWriter, &pIter->dIter.mDataBlk, pNewBlockIdx);
H
Hongze Cheng 已提交
1097 1098
        TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1099 1100
        pIter->dIter.iBlockIdx++;
      } else if (c == 0) {
H
Hongze Cheng 已提交
1101 1102 1103
        code = tsdbReadDataBlk(pIter->dIter.pReader, pBlockIdx, &pIter->dIter.mDataBlk);
        TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1104
        pIter->dIter.iDataBlk = 0;
H
Hongze Cheng 已提交
1105
        pIter->dIter.iBlockIdx++;
H
Hongze Cheng 已提交
1106 1107 1108

        break;
      } else {
H
Hongze Cheng 已提交
1109
        pIter->dIter.iDataBlk = pIter->dIter.mDataBlk.nItem;
H
Hongze Cheng 已提交
1110 1111 1112 1113
        break;
      }
    }
  }
H
Hongze Cheng 已提交
1114

H
Hongze Cheng 已提交
1115
  if (pId) {
H
Hongze Cheng 已提交
1116 1117
    code = tsdbUpdateTableSchema(pWriter->pTsdb->pVnode->pMeta, pId->suid, pId->uid, &pWriter->skmTable);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1118

H
Hongze Cheng 已提交
1119
    tMapDataReset(&pWriter->mDataBlk);
H
Hongze Cheng 已提交
1120

H
Hongze Cheng 已提交
1121 1122
    code = tBlockDataInit(&pWriter->bData, pId, pWriter->skmTable.pTSchema, NULL, 0);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1123
  }
H
Hongze Cheng 已提交
1124

H
Hongze Cheng 已提交
1125 1126 1127 1128 1129
  if (!TABLE_SAME_SCHEMA(pWriter->tbid.suid, pWriter->tbid.uid, pWriter->sData.suid, pWriter->sData.uid)) {
    if ((pWriter->sData.nRow > 0)) {
      code = tsdbWriteSttBlock(pWriter->pDataFWriter, &pWriter->sData, pWriter->aSttBlk, pWriter->cmprAlg);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
1130

H
Hongze Cheng 已提交
1131 1132 1133 1134 1135
    if (pId) {
      TABLEID id = {.suid = pWriter->tbid.suid, .uid = pWriter->tbid.suid ? 0 : pWriter->tbid.uid};
      code = tBlockDataInit(&pWriter->sData, &id, pWriter->skmTable.pTSchema, NULL, 0);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
1136
  }
H
Hongze Cheng 已提交
1137

H
Hongze Cheng 已提交
1138 1139 1140
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1141
  } else {
H
Hongze Cheng 已提交
1142 1143
    tsdbTrace("vgId:%d %s done, suid:%" PRId64 " uid:%" PRId64, TD_VID(pWriter->pTsdb->pVnode), __func__,
              pWriter->tbid.suid, pWriter->tbid.uid);
H
Hongze Cheng 已提交
1144
  }
H
Hongze Cheng 已提交
1145 1146 1147
  return code;
}

H
Hongze Cheng 已提交
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
static int32_t tsdbSnapWriteTableRowImpl(STsdbSnapWriter* pWriter, TSDBROW* pRow) {
  int32_t code = 0;
  int32_t lino = 0;

  code = tBlockDataAppendRow(&pWriter->bData, pRow, pWriter->skmTable.pTSchema, pWriter->tbid.uid);
  TSDB_CHECK_CODE(code, lino, _exit);

  if (pWriter->bData.nRow >= pWriter->maxRow) {
    code = tsdbWriteDataBlock(pWriter->pDataFWriter, &pWriter->bData, &pWriter->mDataBlk, pWriter->cmprAlg);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1158 1159 1160
  }

_exit:
H
Hongze Cheng 已提交
1161 1162 1163
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
H
Hongze Cheng 已提交
1164 1165 1166
  return code;
}

H
Hongze Cheng 已提交
1167
static int32_t tsdbSnapWriteTableRow(STsdbSnapWriter* pWriter, TSDBROW* pRow) {
H
Hongze Cheng 已提交
1168
  int32_t code = 0;
H
Hongze Cheng 已提交
1169 1170 1171
  int32_t lino = 0;

  TSDBKEY inKey = pRow ? TSDBROW_KEY(pRow) : TSDBKEY_MAX;
H
Hongze Cheng 已提交
1172

H
Hongze Cheng 已提交
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
  if (pWriter->pDIter == NULL || (pWriter->pDIter->dIter.iRow >= pWriter->pDIter->dIter.bData.nRow &&
                                  pWriter->pDIter->dIter.iDataBlk >= pWriter->pDIter->dIter.mDataBlk.nItem)) {
    goto _write_row;
  } else {
    for (;;) {
      while (pWriter->pDIter->dIter.iRow < pWriter->pDIter->dIter.bData.nRow) {
        TSDBROW row = tsdbRowFromBlockData(&pWriter->pDIter->dIter.bData, pWriter->pDIter->dIter.iRow);

        int32_t c = tsdbKeyCmprFn(&inKey, &TSDBROW_KEY(&row));
        if (c < 0) {
          goto _write_row;
        } else if (c > 0) {
          code = tsdbSnapWriteTableRowImpl(pWriter, &row);
          TSDB_CHECK_CODE(code, lino, _exit);

          pWriter->pDIter->dIter.iRow++;
        } else {
          ASSERT(0);
        }
      }
H
Hongze Cheng 已提交
1193

H
Hongze Cheng 已提交
1194 1195
      for (;;) {
        if (pWriter->pDIter->dIter.iDataBlk >= pWriter->pDIter->dIter.mDataBlk.nItem) goto _write_row;
H
Hongze Cheng 已提交
1196

H
Hongze Cheng 已提交
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
        // FIXME: Here can be slow, use array instead
        SDataBlk dataBlk;
        tMapDataGetItemByIdx(&pWriter->pDIter->dIter.mDataBlk, pWriter->pDIter->dIter.iDataBlk, &dataBlk, tGetDataBlk);

        int32_t c = tDataBlkCmprFn(&dataBlk, &(SDataBlk){.minKey = inKey, .maxKey = inKey});
        if (c > 0) {
          goto _write_row;
        } else if (c < 0) {
          if (pWriter->bData.nRow > 0) {
            code = tsdbWriteDataBlock(pWriter->pDataFWriter, &pWriter->bData, &pWriter->mDataBlk, pWriter->cmprAlg);
            TSDB_CHECK_CODE(code, lino, _exit);
          }

          tMapDataPutItem(&pWriter->pDIter->dIter.mDataBlk, &dataBlk, tPutDataBlk);
          pWriter->pDIter->dIter.iDataBlk++;
        } else {
          code = tsdbReadDataBlockEx(pWriter->pDataFReader, &dataBlk, &pWriter->pDIter->dIter.bData);
          TSDB_CHECK_CODE(code, lino, _exit);

          pWriter->pDIter->dIter.iRow = 0;
          pWriter->pDIter->dIter.iDataBlk++;
          break;
        }
      }
H
Hongze Cheng 已提交
1221
    }
H
Hongze Cheng 已提交
1222
  }
H
Hongze Cheng 已提交
1223

H
Hongze Cheng 已提交
1224 1225 1226 1227
_write_row:
  if (pRow) {
    code = tsdbSnapWriteTableRowImpl(pWriter, pRow);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1228 1229
  }

H
Hongze Cheng 已提交
1230
_exit:
H
Hongze Cheng 已提交
1231 1232 1233
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
H
Hongze Cheng 已提交
1234 1235 1236
  return code;
}

H
Hongze Cheng 已提交
1237 1238
static int32_t tsdbSnapWriteTableDataEnd(STsdbSnapWriter* pWriter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1239
  int32_t lino = 0;
H
Hongze Cheng 已提交
1240

H
Hongze Cheng 已提交
1241 1242 1243
  // write a NULL row to end current table data write
  code = tsdbSnapWriteTableRow(pWriter, NULL);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1244

H
Hongze Cheng 已提交
1245 1246
  if (pWriter->bData.nRow > 0) {
    if (pWriter->bData.nRow < pWriter->minRow) {
H
Hongze Cheng 已提交
1247
      ASSERT(TABLE_SAME_SCHEMA(pWriter->sData.suid, pWriter->sData.uid, pWriter->tbid.suid, pWriter->tbid.uid));
H
Hongze Cheng 已提交
1248
      for (int32_t iRow = 0; iRow < pWriter->bData.nRow; iRow++) {
H
Hongze Cheng 已提交
1249 1250
        code =
            tBlockDataAppendRow(&pWriter->sData, &tsdbRowFromBlockData(&pWriter->bData, iRow), NULL, pWriter->tbid.uid);
H
Hongze Cheng 已提交
1251
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1252

H
Hongze Cheng 已提交
1253 1254 1255 1256
        if (pWriter->sData.nRow >= pWriter->maxRow) {
          code = tsdbWriteSttBlock(pWriter->pDataFWriter, &pWriter->sData, pWriter->aSttBlk, pWriter->cmprAlg);
          TSDB_CHECK_CODE(code, lino, _exit);
        }
H
Hongze Cheng 已提交
1257
      }
H
Hongze Cheng 已提交
1258

H
Hongze Cheng 已提交
1259
      tBlockDataClear(&pWriter->bData);
H
Hongze Cheng 已提交
1260 1261 1262
    } else {
      code = tsdbWriteDataBlock(pWriter->pDataFWriter, &pWriter->bData, &pWriter->mDataBlk, pWriter->cmprAlg);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1263 1264
    }
  }
H
Hongze Cheng 已提交
1265

H
Hongze Cheng 已提交
1266 1267 1268
  if (pWriter->mDataBlk.nItem) {
    SBlockIdx* pBlockIdx = taosArrayReserve(pWriter->aBlockIdx, 1);
    if (pBlockIdx == NULL) {
H
Hongze Cheng 已提交
1269
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1270
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1271
    }
H
Hongze Cheng 已提交
1272

H
Hongze Cheng 已提交
1273 1274
    pBlockIdx->suid = pWriter->tbid.suid;
    pBlockIdx->uid = pWriter->tbid.uid;
H
Hongze Cheng 已提交
1275

H
Hongze Cheng 已提交
1276 1277 1278
    code = tsdbWriteDataBlk(pWriter->pDataFWriter, &pWriter->mDataBlk, pBlockIdx);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
1279

H
Hongze Cheng 已提交
1280 1281 1282 1283
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
H
Hongze Cheng 已提交
1284 1285 1286
  return code;
}

H
Hongze Cheng 已提交
1287
static int32_t tsdbSnapWriteFileDataStart(STsdbSnapWriter* pWriter, int32_t fid) {
H
Hongze Cheng 已提交
1288
  int32_t code = 0;
H
Hongze Cheng 已提交
1289
  int32_t lino = 0;
H
Hongze Cheng 已提交
1290

H
Hongze Cheng 已提交
1291
  ASSERT(pWriter->pDataFWriter == NULL && pWriter->fid < fid);
H
Hongze Cheng 已提交
1292

H
Hongze Cheng 已提交
1293
  STsdb* pTsdb = pWriter->pTsdb;
H
Hongze Cheng 已提交
1294 1295

  pWriter->fid = fid;
H
Hongze Cheng 已提交
1296
  pWriter->tbid = (TABLEID){0};
H
Hongze Cheng 已提交
1297 1298
  SDFileSet* pSet = taosArraySearch(pWriter->fs.aDFileSet, &(SDFileSet){.fid = fid}, tDFileSetCmprFn, TD_EQ);

H
Hongze Cheng 已提交
1299 1300 1301 1302
  // open reader
  pWriter->pDataFReader = NULL;
  pWriter->iterList = NULL;
  pWriter->pDIter = NULL;
H
Hongze Cheng 已提交
1303
  pWriter->pSIter = NULL;
H
Hongze Cheng 已提交
1304
  tRBTreeCreate(&pWriter->rbt, tsdbDataIterCmprFn);
H
Hongze Cheng 已提交
1305
  if (pSet) {
H
Hongze Cheng 已提交
1306 1307
    code = tsdbDataFReaderOpen(&pWriter->pDataFReader, pTsdb, pSet);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1308

H
Hongze Cheng 已提交
1309 1310 1311 1312 1313 1314
    code = tsdbOpenDataFileDataIter(pWriter->pDataFReader, &pWriter->pDIter);
    TSDB_CHECK_CODE(code, lino, _exit);
    if (pWriter->pDIter) {
      pWriter->pDIter->next = pWriter->iterList;
      pWriter->iterList = pWriter->pDIter;
    }
H
Hongze Cheng 已提交
1315

H
Hongze Cheng 已提交
1316
    for (int32_t iStt = 0; iStt < pSet->nSttF; iStt++) {
H
Hongze Cheng 已提交
1317
      code = tsdbOpenSttFileDataIter(pWriter->pDataFReader, iStt, &pWriter->pSIter);
H
Hongze Cheng 已提交
1318
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1319

H
Hongze Cheng 已提交
1320
      if (pWriter->pSIter) {
H
Hongze Cheng 已提交
1321
        code = tsdbSttFileDataIterNext(pWriter->pSIter, NULL);
H
Hongze Cheng 已提交
1322
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1323

H
Hongze Cheng 已提交
1324
        // add to tree
H
Hongze Cheng 已提交
1325
        tRBTreePut(&pWriter->rbt, &pWriter->pSIter->rbtn);
H
Hongze Cheng 已提交
1326

H
Hongze Cheng 已提交
1327
        // add to list
H
Hongze Cheng 已提交
1328 1329
        pWriter->pSIter->next = pWriter->iterList;
        pWriter->iterList = pWriter->pSIter;
H
Hongze Cheng 已提交
1330 1331 1332
      }
    }

H
Hongze Cheng 已提交
1333
    pWriter->pSIter = NULL;
H
Hongze Cheng 已提交
1334 1335 1336 1337 1338 1339
  }

  // open writer
  SDiskID diskId;
  if (pSet) {
    diskId = pSet->diskId;
H
Hongze Cheng 已提交
1340
  } else {
H
Hongze Cheng 已提交
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
    tfsAllocDisk(pTsdb->pVnode->pTfs, 0 /*TODO*/, &diskId);
    tfsMkdirRecurAt(pTsdb->pVnode->pTfs, pTsdb->path, diskId);
  }
  SDFileSet wSet = {.diskId = diskId,
                    .fid = fid,
                    .pHeadF = &(SHeadFile){.commitID = pWriter->commitID},
                    .pDataF = (pSet) ? pSet->pDataF : &(SDataFile){.commitID = pWriter->commitID},
                    .pSmaF = (pSet) ? pSet->pSmaF : &(SSmaFile){.commitID = pWriter->commitID},
                    .nSttF = 1,
                    .aSttF = {&(SSttFile){.commitID = pWriter->commitID}}};
  code = tsdbDataFWriterOpen(&pWriter->pDataFWriter, pTsdb, &wSet);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1353

H
Hongze Cheng 已提交
1354 1355 1356 1357 1358
  if (pWriter->aBlockIdx) {
    taosArrayClear(pWriter->aBlockIdx);
  } else if ((pWriter->aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx))) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1359
  }
H
Hongze Cheng 已提交
1360

H
Hongze Cheng 已提交
1361
  tMapDataReset(&pWriter->mDataBlk);
H
Hongze Cheng 已提交
1362

H
Hongze Cheng 已提交
1363 1364 1365 1366 1367
  if (pWriter->aSttBlk) {
    taosArrayClear(pWriter->aSttBlk);
  } else if ((pWriter->aSttBlk = taosArrayInit(0, sizeof(SSttBlk))) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1368 1369
  }

H
Hongze Cheng 已提交
1370
  tBlockDataReset(&pWriter->bData);
H
Hongze Cheng 已提交
1371
  tBlockDataReset(&pWriter->sData);
H
Hongze Cheng 已提交
1372

H
Hongze Cheng 已提交
1373 1374 1375 1376 1377 1378 1379
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s, fid:%d", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code),
              fid);
  } else {
    tsdbDebug("vgId:%d %s done, fid:%d", TD_VID(pTsdb->pVnode), __func__, fid);
  }
H
Hongze Cheng 已提交
1380 1381 1382
  return code;
}

H
Hongze Cheng 已提交
1383 1384 1385 1386 1387 1388
static int32_t tsdbSnapWriteTableData(STsdbSnapWriter* pWriter, SRowInfo* pRowInfo) {
  int32_t code = 0;
  int32_t lino = 0;

  // switch to new table if need
  if (pRowInfo == NULL || pRowInfo->uid != pWriter->tbid.uid) {
H
Hongze Cheng 已提交
1389
    if (pWriter->tbid.uid) {
H
Hongze Cheng 已提交
1390 1391
      code = tsdbSnapWriteTableDataEnd(pWriter);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1392
    }
H
Hongze Cheng 已提交
1393 1394 1395

    code = tsdbSnapWriteTableDataStart(pWriter, (TABLEID*)pRowInfo);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1396 1397
  }

H
Hongze Cheng 已提交
1398
  if (pRowInfo == NULL) goto _exit;
H
Hongze Cheng 已提交
1399

H
Hongze Cheng 已提交
1400 1401
  code = tsdbSnapWriteTableRow(pWriter, &pRowInfo->row);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1402

H
Hongze Cheng 已提交
1403 1404 1405 1406
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
H
Hongze Cheng 已提交
1407 1408 1409
  return code;
}

H
Hongze Cheng 已提交
1410
static int32_t tsdbSnapWriteNextRow(STsdbSnapWriter* pWriter, SRowInfo** ppRowInfo) {
H
Hongze Cheng 已提交
1411
  int32_t code = 0;
H
Hongze Cheng 已提交
1412
  int32_t lino = 0;
H
Hongze Cheng 已提交
1413

H
Hongze Cheng 已提交
1414
  if (pWriter->pSIter) {
H
Hongze Cheng 已提交
1415
    code = tsdbDataIterNext2(pWriter->pSIter, NULL);
H
Hongze Cheng 已提交
1416
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1417

H
Hongze Cheng 已提交
1418 1419
    if (pWriter->pSIter->rowInfo.suid == 0 && pWriter->pSIter->rowInfo.uid == 0) {
      pWriter->pSIter = NULL;
H
Hongze Cheng 已提交
1420 1421 1422
    } else {
      SRBTreeNode* pNode = tRBTreeMin(&pWriter->rbt);
      if (pNode) {
H
Hongze Cheng 已提交
1423
        int32_t c = tsdbDataIterCmprFn(&pWriter->pSIter->rbtn, pNode);
H
Hongze Cheng 已提交
1424
        if (c > 0) {
H
Hongze Cheng 已提交
1425 1426
          tRBTreePut(&pWriter->rbt, &pWriter->pSIter->rbtn);
          pWriter->pSIter = NULL;
H
Hongze Cheng 已提交
1427 1428 1429 1430 1431
        } else if (c == 0) {
          ASSERT(0);
        }
      }
    }
H
Hongze Cheng 已提交
1432
  }
H
Hongze Cheng 已提交
1433

H
Hongze Cheng 已提交
1434
  if (pWriter->pSIter == NULL) {
H
Hongze Cheng 已提交
1435 1436 1437
    SRBTreeNode* pNode = tRBTreeMin(&pWriter->rbt);
    if (pNode) {
      tRBTreeDrop(&pWriter->rbt, pNode);
H
Hongze Cheng 已提交
1438
      pWriter->pSIter = TSDB_RBTN_TO_DATA_ITER(pNode);
H
Hongze Cheng 已提交
1439
    }
H
Hongze Cheng 已提交
1440
  }
H
Hongze Cheng 已提交
1441

H
Hongze Cheng 已提交
1442
  if (ppRowInfo) {
H
Hongze Cheng 已提交
1443 1444
    if (pWriter->pSIter) {
      *ppRowInfo = &pWriter->pSIter->rowInfo;
H
Hongze Cheng 已提交
1445 1446 1447
    } else {
      *ppRowInfo = NULL;
    }
H
Hongze Cheng 已提交
1448
  }
H
Hongze Cheng 已提交
1449

H
Hongze Cheng 已提交
1450 1451 1452
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1453
  }
H
Hongze Cheng 已提交
1454 1455 1456
  return code;
}

H
Hongze Cheng 已提交
1457
static int32_t tsdbSnapWriteGetRow(STsdbSnapWriter* pWriter, SRowInfo** ppRowInfo) {
H
Hongze Cheng 已提交
1458
  int32_t code = 0;
H
Hongze Cheng 已提交
1459
  int32_t lino = 0;
H
Hongze Cheng 已提交
1460

H
Hongze Cheng 已提交
1461 1462
  if (pWriter->pSIter) {
    *ppRowInfo = &pWriter->pSIter->rowInfo;
H
Hongze Cheng 已提交
1463
    goto _exit;
H
Hongze Cheng 已提交
1464 1465
  }

H
Hongze Cheng 已提交
1466 1467
  code = tsdbSnapWriteNextRow(pWriter, ppRowInfo);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1468

H
Hongze Cheng 已提交
1469
_exit:
H
Hongze Cheng 已提交
1470 1471 1472
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
H
Hongze Cheng 已提交
1473
  return code;
H
Hongze Cheng 已提交
1474 1475
}

H
Hongze Cheng 已提交
1476
static int32_t tsdbSnapWriteFileDataEnd(STsdbSnapWriter* pWriter) {
H
Hongze Cheng 已提交
1477
  int32_t code = 0;
H
Hongze Cheng 已提交
1478
  int32_t lino = 0;
H
Hongze Cheng 已提交
1479

H
Hongze Cheng 已提交
1480
  ASSERT(pWriter->pDataFWriter);
H
Hongze Cheng 已提交
1481

H
Hongze Cheng 已提交
1482 1483 1484 1485 1486 1487 1488
  // consume remain data and end with a NULL table row
  SRowInfo* pRowInfo;
  code = tsdbSnapWriteGetRow(pWriter, &pRowInfo);
  TSDB_CHECK_CODE(code, lino, _exit);
  for (;;) {
    code = tsdbSnapWriteTableData(pWriter, pRowInfo);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1489

H
Hongze Cheng 已提交
1490
    if (pRowInfo == NULL) break;
H
Hongze Cheng 已提交
1491

H
Hongze Cheng 已提交
1492 1493 1494
    code = tsdbSnapWriteNextRow(pWriter, &pRowInfo);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
1495

H
Hongze Cheng 已提交
1496 1497 1498
  // do file-level updates
  code = tsdbWriteSttBlk(pWriter->pDataFWriter, pWriter->aSttBlk);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1499

H
Hongze Cheng 已提交
1500 1501
  code = tsdbWriteBlockIdx(pWriter->pDataFWriter, pWriter->aBlockIdx);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1502

H
Hongze Cheng 已提交
1503 1504
  code = tsdbUpdateDFileSetHeader(pWriter->pDataFWriter);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1505

H
Hongze Cheng 已提交
1506 1507
  code = tsdbFSUpsertFSet(&pWriter->fs, &pWriter->pDataFWriter->wSet);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1508

H
Hongze Cheng 已提交
1509 1510
  code = tsdbDataFWriterClose(&pWriter->pDataFWriter, 1);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1511

H
Hongze Cheng 已提交
1512 1513 1514
  if (pWriter->pDataFReader) {
    code = tsdbDataFReaderClose(&pWriter->pDataFReader);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1515
  }
H
Hongze Cheng 已提交
1516

H
Hongze Cheng 已提交
1517 1518 1519 1520 1521 1522
  // clear sources
  while (pWriter->iterList) {
    STsdbDataIter2* pIter = pWriter->iterList;
    pWriter->iterList = pIter->next;
    tsdbCloseDataIter2(pIter);
  }
H
Hongze Cheng 已提交
1523

H
Hongze Cheng 已提交
1524 1525 1526 1527 1528 1529
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, tstrerror(code));
  } else {
    tsdbDebug("vgId:%d %s is done", TD_VID(pWriter->pTsdb->pVnode), __func__);
  }
H
Hongze Cheng 已提交
1530 1531 1532
  return code;
}

H
Hongze Cheng 已提交
1533
static int32_t tsdbSnapWriteTimeSeriesData(STsdbSnapWriter* pWriter, SSnapDataHdr* pHdr) {
H
Hongze Cheng 已提交
1534
  int32_t code = 0;
H
Hongze Cheng 已提交
1535
  int32_t lino = 0;
H
Hongze Cheng 已提交
1536

H
Hongze Cheng 已提交
1537 1538
  code = tDecmprBlockData(pHdr->data, pHdr->size, &pWriter->inData, pWriter->aBuf);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1539

H
Hongze Cheng 已提交
1540
  ASSERT(pWriter->inData.nRow > 0);
H
Hongze Cheng 已提交
1541

H
Hongze Cheng 已提交
1542 1543 1544 1545
  // switch to new data file if need
  int32_t fid = tsdbKeyFid(pWriter->inData.aTSKEY[0], pWriter->minutes, pWriter->precision);
  if (pWriter->fid != fid) {
    if (pWriter->pDataFWriter) {
H
Hongze Cheng 已提交
1546
      code = tsdbSnapWriteFileDataEnd(pWriter);
H
Hongze Cheng 已提交
1547
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1548 1549
    }

H
Hongze Cheng 已提交
1550
    code = tsdbSnapWriteFileDataStart(pWriter, fid);
H
Hongze Cheng 已提交
1551
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1552 1553
  }

H
Hongze Cheng 已提交
1554 1555 1556 1557
  // loop write each row
  SRowInfo* pRowInfo;
  code = tsdbSnapWriteGetRow(pWriter, &pRowInfo);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1558
  for (int32_t iRow = 0; iRow < pWriter->inData.nRow; ++iRow) {
H
Hongze Cheng 已提交
1559 1560 1561
    SRowInfo rInfo = {.suid = pWriter->inData.suid,
                      .uid = pWriter->inData.uid ? pWriter->inData.uid : pWriter->inData.aUid[iRow],
                      .row = tsdbRowFromBlockData(&pWriter->inData, iRow)};
H
Hongze Cheng 已提交
1562

H
Hongze Cheng 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
    for (;;) {
      if (pRowInfo == NULL) {
        code = tsdbSnapWriteTableData(pWriter, &rInfo);
        TSDB_CHECK_CODE(code, lino, _exit);
        break;
      } else {
        int32_t c = tRowInfoCmprFn(&rInfo, pRowInfo);
        if (c < 0) {
          code = tsdbSnapWriteTableData(pWriter, &rInfo);
          TSDB_CHECK_CODE(code, lino, _exit);
          break;
        } else if (c > 0) {
          code = tsdbSnapWriteTableData(pWriter, pRowInfo);
          TSDB_CHECK_CODE(code, lino, _exit);

          code = tsdbSnapWriteNextRow(pWriter, &pRowInfo);
          TSDB_CHECK_CODE(code, lino, _exit);
        } else {
          ASSERT(0);
        }
      }
    }
H
Hongze Cheng 已提交
1585 1586
  }

H
Hongze Cheng 已提交
1587
_exit:
H
Hongze Cheng 已提交
1588 1589 1590 1591 1592 1593
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
  } else {
    tsdbDebug("vgId:%d %s done, suid:%" PRId64 " uid:%" PRId64 " nRow:%d", TD_VID(pWriter->pTsdb->pVnode), __func__,
              pWriter->inData.suid, pWriter->inData.uid, pWriter->inData.nRow);
  }
H
Hongze Cheng 已提交
1594 1595 1596 1597
  return code;
}

// SNAP_DATA_DEL
H
Hongze Cheng 已提交
1598
static int32_t tsdbSnapWriteDelTableDataStart(STsdbSnapWriter* pWriter, TABLEID* pId) {
H
Hongze Cheng 已提交
1599
  int32_t code = 0;
H
Hongze Cheng 已提交
1600
  int32_t lino = 0;
H
Hongze Cheng 已提交
1601

H
Hongze Cheng 已提交
1602 1603 1604 1605
  if (pId) {
    pWriter->tbid = *pId;
  } else {
    pWriter->tbid = (TABLEID){.suid = INT64_MAX, .uid = INT64_MAX};
H
Hongze Cheng 已提交
1606
  }
H
Hongze Cheng 已提交
1607

H
Hongze Cheng 已提交
1608
  taosArrayClear(pWriter->aDelData);
H
Hongze Cheng 已提交
1609

H
Hongze Cheng 已提交
1610 1611
  if (pWriter->pTIter) {
    while (pWriter->pTIter->tIter.iDelIdx < taosArrayGetSize(pWriter->pTIter->tIter.aDelIdx)) {
H
Hongze Cheng 已提交
1612
      SDelIdx* pDelIdx = taosArrayGet(pWriter->pTIter->tIter.aDelIdx, pWriter->pTIter->tIter.iDelIdx);
H
Hongze Cheng 已提交
1613

H
Hongze Cheng 已提交
1614 1615 1616 1617
      int32_t c = tTABLEIDCmprFn(pDelIdx, &pWriter->tbid);
      if (c < 0) {
        code = tsdbReadDelData(pWriter->pDelFReader, pDelIdx, pWriter->pTIter->tIter.aDelData);
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1618

H
Hongze Cheng 已提交
1619
        SDelIdx* pDelIdxNew = taosArrayReserve(pWriter->aDelIdx, 1);
H
Hongze Cheng 已提交
1620 1621 1622 1623
        if (pDelIdxNew == NULL) {
          code = TSDB_CODE_OUT_OF_MEMORY;
          TSDB_CHECK_CODE(code, lino, _exit);
        }
H
Hongze Cheng 已提交
1624

H
Hongze Cheng 已提交
1625 1626
        pDelIdxNew->suid = pDelIdx->suid;
        pDelIdxNew->uid = pDelIdx->uid;
H
Hongze Cheng 已提交
1627

H
Hongze Cheng 已提交
1628 1629
        code = tsdbWriteDelData(pWriter->pDelFWriter, pWriter->pTIter->tIter.aDelData, pDelIdxNew);
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1630

H
Hongze Cheng 已提交
1631 1632
        pWriter->pTIter->tIter.iDelIdx++;
      } else if (c == 0) {
H
Hongze Cheng 已提交
1633
        code = tsdbReadDelData(pWriter->pDelFReader, pDelIdx, pWriter->aDelData);
H
Hongze Cheng 已提交
1634
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1635

H
Hongze Cheng 已提交
1636 1637 1638 1639
        pWriter->pTIter->tIter.iDelIdx++;
        break;
      } else {
        break;
H
Hongze Cheng 已提交
1640
      }
H
Hongze Cheng 已提交
1641 1642 1643
    }
  }

H
Hongze Cheng 已提交
1644
_exit:
H
Hongze Cheng 已提交
1645 1646 1647 1648 1649 1650
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
  } else {
    tsdbTrace("vgId:%d %s done, suid:%" PRId64 " uid:%" PRId64, TD_VID(pWriter->pTsdb->pVnode), __func__, pId->suid,
              pId->uid);
  }
H
Hongze Cheng 已提交
1651 1652 1653
  return code;
}

H
Hongze Cheng 已提交
1654
static int32_t tsdbSnapWriteDelTableDataEnd(STsdbSnapWriter* pWriter) {
H
Hongze Cheng 已提交
1655
  int32_t code = 0;
H
Hongze Cheng 已提交
1656
  int32_t lino = 0;
H
Hongze Cheng 已提交
1657

H
Hongze Cheng 已提交
1658 1659 1660
  if (taosArrayGetSize(pWriter->aDelData) > 0) {
    SDelIdx* pDelIdx = taosArrayReserve(pWriter->aDelIdx, 1);
    if (pDelIdx == NULL) {
H
Hongze Cheng 已提交
1661
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1662
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1663 1664
    }

H
Hongze Cheng 已提交
1665 1666
    pDelIdx->suid = pWriter->tbid.suid;
    pDelIdx->uid = pWriter->tbid.uid;
H
Hongze Cheng 已提交
1667

H
Hongze Cheng 已提交
1668 1669
    code = tsdbWriteDelData(pWriter->pDelFWriter, pWriter->aDelData, pDelIdx);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1670 1671 1672
  }

_exit:
H
Hongze Cheng 已提交
1673 1674 1675 1676 1677
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
  } else {
    tsdbTrace("vgId:%d %s done", TD_VID(pWriter->pTsdb->pVnode), __func__);
  }
H
Hongze Cheng 已提交
1678 1679 1680
  return code;
}

H
Hongze Cheng 已提交
1681
static int32_t tsdbSnapWriteDelTableData(STsdbSnapWriter* pWriter, TABLEID* pId, uint8_t* pData, int64_t size) {
H
Hongze Cheng 已提交
1682
  int32_t code = 0;
H
Hongze Cheng 已提交
1683
  int32_t lino = 0;
H
Hongze Cheng 已提交
1684

H
Hongze Cheng 已提交
1685 1686 1687 1688
  if (pId == NULL || pId->uid != pWriter->tbid.uid) {
    if (pWriter->tbid.uid) {
      code = tsdbSnapWriteDelTableDataEnd(pWriter);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1689 1690
    }

H
Hongze Cheng 已提交
1691 1692
    code = tsdbSnapWriteDelTableDataStart(pWriter, pId);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1693 1694
  }

H
Hongze Cheng 已提交
1695
  if (pId == NULL) goto _exit;
H
Hongze Cheng 已提交
1696

H
Hongze Cheng 已提交
1697 1698 1699 1700
  int64_t n = 0;
  while (n < size) {
    SDelData delData;
    n += tGetDelData(pData + n, &delData);
H
Hongze Cheng 已提交
1701

H
Hongze Cheng 已提交
1702 1703 1704 1705 1706 1707
    if (taosArrayPush(pWriter->aDelData, &delData) < 0) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit);
    }
  }
  ASSERT(n == size);
H
Hongze Cheng 已提交
1708

H
Hongze Cheng 已提交
1709 1710 1711
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1712
  }
H
Hongze Cheng 已提交
1713 1714
  return code;
}
H
Hongze Cheng 已提交
1715

H
Hongze Cheng 已提交
1716 1717 1718
static int32_t tsdbSnapWriteDelDataStart(STsdbSnapWriter* pWriter) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1719

H
Hongze Cheng 已提交
1720 1721
  STsdb*    pTsdb = pWriter->pTsdb;
  SDelFile* pDelFile = pWriter->fs.pDelFile;
H
Hongze Cheng 已提交
1722

H
Hongze Cheng 已提交
1723
  pWriter->tbid = (TABLEID){0};
H
Hongze Cheng 已提交
1724

H
Hongze Cheng 已提交
1725 1726 1727 1728
  // reader
  if (pDelFile) {
    code = tsdbDelFReaderOpen(&pWriter->pDelFReader, pDelFile, pTsdb);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1729

H
Hongze Cheng 已提交
1730 1731
    code = tsdbOpenTombFileDataIter(pWriter->pDelFReader, &pWriter->pTIter);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1732
  }
H
Hongze Cheng 已提交
1733

H
Hongze Cheng 已提交
1734 1735 1736
  // writer
  code = tsdbDelFWriterOpen(&pWriter->pDelFWriter, &(SDelFile){.commitID = pWriter->commitID}, pTsdb);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1737

H
Hongze Cheng 已提交
1738
  if ((pWriter->aDelIdx = taosArrayInit(0, sizeof(SDelIdx))) == NULL) {
H
Hongze Cheng 已提交
1739
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1740 1741 1742 1743 1744
    TSDB_CHECK_CODE(code, lino, _exit);
  }
  if ((pWriter->aDelData = taosArrayInit(0, sizeof(SDelData))) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1745 1746
  }

H
Hongze Cheng 已提交
1747 1748 1749 1750 1751 1752
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
  } else {
    tsdbDebug("vgId:%d %s done", TD_VID(pTsdb->pVnode), __func__);
  }
H
Hongze Cheng 已提交
1753 1754 1755
  return code;
}

H
Hongze Cheng 已提交
1756
static int32_t tsdbSnapWriteDelDataEnd(STsdbSnapWriter* pWriter) {
H
Hongze Cheng 已提交
1757
  int32_t code = 0;
H
Hongze Cheng 已提交
1758
  int32_t lino = 0;
H
Hongze Cheng 已提交
1759

H
Hongze Cheng 已提交
1760
  STsdb* pTsdb = pWriter->pTsdb;
H
Hongze Cheng 已提交
1761

H
Hongze Cheng 已提交
1762
  // end remaining table with NULL data
H
Hongze Cheng 已提交
1763 1764
  code = tsdbSnapWriteDelTableData(pWriter, NULL, NULL, 0);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1765

H
Hongze Cheng 已提交
1766 1767 1768
  // update file-level info
  code = tsdbWriteDelIdx(pWriter->pDelFWriter, pWriter->aDelIdx);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1769

H
Hongze Cheng 已提交
1770
  code = tsdbUpdateDelFileHdr(pWriter->pDelFWriter);
H
Hongze Cheng 已提交
1771
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1772

H
Hongze Cheng 已提交
1773
  code = tsdbFSUpsertDelFile(&pWriter->fs, &pWriter->pDelFWriter->fDel);
H
Hongze Cheng 已提交
1774
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1775 1776

  code = tsdbDelFWriterClose(&pWriter->pDelFWriter, 1);
H
Hongze Cheng 已提交
1777
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1778 1779 1780

  if (pWriter->pDelFReader) {
    code = tsdbDelFReaderClose(&pWriter->pDelFReader);
H
Hongze Cheng 已提交
1781
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1782 1783
  }

H
Hongze Cheng 已提交
1784 1785 1786
  if (pWriter->pTIter) {
    tsdbCloseDataIter2(pWriter->pTIter);
    pWriter->pTIter = NULL;
H
Hongze Cheng 已提交
1787 1788
  }

H
Hongze Cheng 已提交
1789 1790 1791 1792 1793 1794
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
  } else {
    tsdbInfo("vgId:%d %s done", TD_VID(pTsdb->pVnode), __func__);
  }
H
Hongze Cheng 已提交
1795
  return code;
H
Hongze Cheng 已提交
1796
}
H
Hongze Cheng 已提交
1797

H
Hongze Cheng 已提交
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809
static int32_t tsdbSnapWriteDelData(STsdbSnapWriter* pWriter, SSnapDataHdr* pHdr) {
  int32_t code = 0;
  int32_t lino = 0;

  STsdb* pTsdb = pWriter->pTsdb;

  // start to write del data if need
  if (pWriter->pDelFWriter == NULL) {
    code = tsdbSnapWriteDelDataStart(pWriter);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
1810
  // do write del data
H
Hongze Cheng 已提交
1811 1812 1813 1814 1815 1816 1817 1818 1819 1820
  code = tsdbSnapWriteDelTableData(pWriter, (TABLEID*)pHdr->data, pHdr->data + sizeof(TABLEID),
                                   pHdr->size - sizeof(TABLEID));
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed since %s", TD_VID(pTsdb->pVnode), __func__, tstrerror(code));
  } else {
    tsdbTrace("vgId:%d %s done", TD_VID(pTsdb->pVnode), __func__);
  }
H
Hongze Cheng 已提交
1821 1822 1823
  return code;
}

H
Hongze Cheng 已提交
1824
// APIs
H
Hongze Cheng 已提交
1825
int32_t tsdbSnapWriterOpen(STsdb* pTsdb, int64_t sver, int64_t ever, STsdbSnapWriter** ppWriter) {
H
Hongze Cheng 已提交
1826 1827
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1828 1829

  // alloc
H
Hongze Cheng 已提交
1830
  STsdbSnapWriter* pWriter = (STsdbSnapWriter*)taosMemoryCalloc(1, sizeof(*pWriter));
H
Hongze Cheng 已提交
1831 1832
  if (pWriter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1833
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1834 1835 1836 1837
  }
  pWriter->pTsdb = pTsdb;
  pWriter->sver = sver;
  pWriter->ever = ever;
H
Hongze Cheng 已提交
1838 1839 1840 1841 1842
  pWriter->minutes = pTsdb->keepCfg.days;
  pWriter->precision = pTsdb->keepCfg.precision;
  pWriter->minRow = pTsdb->pVnode->config.tsdbCfg.minRows;
  pWriter->maxRow = pTsdb->pVnode->config.tsdbCfg.maxRows;
  pWriter->cmprAlg = pTsdb->pVnode->config.tsdbCfg.compression;
H
Hongze Cheng 已提交
1843 1844
  pWriter->commitID = pTsdb->pVnode->state.commitID;

H
Hongze Cheng 已提交
1845 1846 1847
  code = tsdbFSCopy(pTsdb, &pWriter->fs);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1848
  // SNAP_DATA_TSDB
H
Hongze Cheng 已提交
1849
  code = tBlockDataCreate(&pWriter->inData);
H
Hongze Cheng 已提交
1850
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1851

H
Hongze Cheng 已提交
1852
  pWriter->fid = INT32_MIN;
H
Hongze Cheng 已提交
1853

H
Hongze Cheng 已提交
1854
  code = tBlockDataCreate(&pWriter->bData);
H
Hongze Cheng 已提交
1855
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1856 1857

  code = tBlockDataCreate(&pWriter->sData);
H
Hongze Cheng 已提交
1858
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1859

H
Hongze Cheng 已提交
1860
  // SNAP_DATA_DEL
H
Hongze Cheng 已提交
1861

H
Hongze Cheng 已提交
1862 1863
_exit:
  if (code) {
H
Hongze Cheng 已提交
1864
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1865
    if (pWriter) {
1866
      tBlockDataDestroy(&pWriter->sData);
H
Hongze Cheng 已提交
1867
      tBlockDataDestroy(&pWriter->bData);
1868
      tBlockDataDestroy(&pWriter->inData);
H
Hongze Cheng 已提交
1869
      tsdbFSDestroy(&pWriter->fs);
H
Hongze Cheng 已提交
1870
      pWriter = NULL;
H
Hongze Cheng 已提交
1871 1872
    }
  } else {
H
Hongze Cheng 已提交
1873
    tsdbInfo("vgId:%d %s done, sver:%" PRId64 " ever:%" PRId64, TD_VID(pTsdb->pVnode), __func__, sver, ever);
H
Hongze Cheng 已提交
1874
  }
H
Hongze Cheng 已提交
1875
  *ppWriter = pWriter;
H
Hongze Cheng 已提交
1876 1877 1878
  return code;
}

H
Hongze Cheng 已提交
1879 1880
int32_t tsdbSnapWriterPrepareClose(STsdbSnapWriter* pWriter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1881 1882
  int32_t lino = 0;

H
Hongze Cheng 已提交
1883
  if (pWriter->pDataFWriter) {
H
Hongze Cheng 已提交
1884
    code = tsdbSnapWriteFileDataEnd(pWriter);
H
Hongze Cheng 已提交
1885
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1886 1887
  }

H
Hongze Cheng 已提交
1888 1889 1890 1891
  if (pWriter->pDelFWriter) {
    code = tsdbSnapWriteDelDataEnd(pWriter);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
1892 1893

  code = tsdbFSPrepareCommit(pWriter->pTsdb, &pWriter->fs);
H
Hongze Cheng 已提交
1894
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1895 1896 1897

_exit:
  if (code) {
H
Hongze Cheng 已提交
1898 1899 1900
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
  } else {
    tsdbDebug("vgId:%d %s done", TD_VID(pWriter->pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
1901 1902 1903 1904
  }
  return code;
}

H
Hongze Cheng 已提交
1905
int32_t tsdbSnapWriterClose(STsdbSnapWriter** ppWriter, int8_t rollback) {
H
Hongze Cheng 已提交
1906 1907 1908
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1909
  STsdbSnapWriter* pWriter = *ppWriter;
H
Hongze Cheng 已提交
1910
  STsdb*           pTsdb = pWriter->pTsdb;
H
Hongze Cheng 已提交
1911 1912

  if (rollback) {
H
Hongze Cheng 已提交
1913
    tsdbRollbackCommit(pWriter->pTsdb);
H
Hongze Cheng 已提交
1914
  } else {
H
Hongze Cheng 已提交
1915 1916 1917
    // lock
    taosThreadRwlockWrlock(&pTsdb->rwLock);

H
Hongze Cheng 已提交
1918
    code = tsdbFSCommit(pWriter->pTsdb);
H
Hongze Cheng 已提交
1919 1920
    if (code) {
      taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1921
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1922 1923 1924 1925
    }

    // unlock
    taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1926 1927
  }

H
Hongze Cheng 已提交
1928 1929
  // SNAP_DATA_DEL
  taosArrayDestroy(pWriter->aDelData);
H
Hongze Cheng 已提交
1930
  taosArrayDestroy(pWriter->aDelIdx);
H
Hongze Cheng 已提交
1931 1932

  // SNAP_DATA_TSDB
1933
  tBlockDataDestroy(&pWriter->sData);
H
Hongze Cheng 已提交
1934
  tBlockDataDestroy(&pWriter->bData);
H
Hongze Cheng 已提交
1935 1936 1937
  taosArrayDestroy(pWriter->aSttBlk);
  tMapDataClear(&pWriter->mDataBlk);
  taosArrayDestroy(pWriter->aBlockIdx);
1938
  tDestroyTSchema(pWriter->skmTable.pTSchema);
1939
  tBlockDataDestroy(&pWriter->inData);
H
Hongze Cheng 已提交
1940

H
Hongze Cheng 已提交
1941 1942 1943
  for (int32_t iBuf = 0; iBuf < sizeof(pWriter->aBuf) / sizeof(uint8_t*); iBuf++) {
    tFree(pWriter->aBuf[iBuf]);
  }
H
Hongze Cheng 已提交
1944
  tsdbFSDestroy(&pWriter->fs);
H
Hongze Cheng 已提交
1945 1946 1947
  taosMemoryFree(pWriter);
  *ppWriter = NULL;

H
Hongze Cheng 已提交
1948 1949 1950 1951 1952 1953
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
  } else {
    tsdbInfo("vgId:%d %s done", TD_VID(pTsdb->pVnode), __func__);
  }
H
Hongze Cheng 已提交
1954 1955 1956
  return code;
}

H
Hongze Cheng 已提交
1957 1958 1959
int32_t tsdbSnapWrite(STsdbSnapWriter* pWriter, SSnapDataHdr* pHdr) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1960

C
Cary Xu 已提交
1961
  if (pHdr->type == SNAP_DATA_TSDB) {
H
Hongze Cheng 已提交
1962 1963
    code = tsdbSnapWriteTimeSeriesData(pWriter, pHdr);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1964
    goto _exit;
H
Hongze Cheng 已提交
1965
  } else if (pWriter->pDataFWriter) {
H
Hongze Cheng 已提交
1966
    code = tsdbSnapWriteFileDataEnd(pWriter);
H
Hongze Cheng 已提交
1967
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1968 1969
  }

C
Cary Xu 已提交
1970
  if (pHdr->type == SNAP_DATA_DEL) {
H
Hongze Cheng 已提交
1971 1972 1973
    code = tsdbSnapWriteDelData(pWriter, pHdr);
    TSDB_CHECK_CODE(code, lino, _exit);
    goto _exit;
H
Hongze Cheng 已提交
1974 1975
  }

H
Hongze Cheng 已提交
1976
_exit:
H
Hongze Cheng 已提交
1977 1978 1979 1980 1981 1982 1983
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s, type:%d index:%" PRId64 " size:%" PRId64,
              TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code), pHdr->type, pHdr->index, pHdr->size);
  } else {
    tsdbDebug("vgId:%d %s done, type:%d index:%" PRId64 " size:%" PRId64, TD_VID(pWriter->pTsdb->pVnode), __func__,
              pHdr->type, pHdr->index, pHdr->size);
  }
H
Hongze Cheng 已提交
1984 1985
  return code;
}