vnodeSnapshot.c 2.9 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * 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 "vnodeInt.h"

struct SVSnapshotReader {
  SVnode              *pVnode;
  int64_t              sver;
  int64_t              ever;
  int8_t               isMetaEnd;
  int8_t               isTsdbEnd;
  SMetaSnapshotReader *pMetaReader;
  STsdbSnapshotReader *pTsdbReader;
H
Hongze Cheng 已提交
26 27
  void                *pData;
  int32_t              nData;
H
Hongze Cheng 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
};

int32_t vnodeSnapshotReaderOpen(SVnode *pVnode, SVSnapshotReader **ppReader, int64_t sver, int64_t ever) {
  SVSnapshotReader *pReader = NULL;

  pReader = (SVSnapshotReader *)taosMemoryCalloc(1, sizeof(*pReader));
  if (pReader == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  pReader->pVnode = pVnode;
  pReader->sver = sver;
  pReader->ever = ever;
  pReader->isMetaEnd = 0;
  pReader->isTsdbEnd = 0;

  if (metaSnapshotReaderOpen(pVnode->pMeta, &pReader->pMetaReader, sver, ever) < 0) {
    taosMemoryFree(pReader);
    goto _err;
  }

  if (tsdbSnapshotReaderOpen(pVnode->pTsdb, &pReader->pTsdbReader, sver, ever) < 0) {
    metaSnapshotReaderClose(pReader->pMetaReader);
    taosMemoryFree(pReader);
    goto _err;
  }

_exit:
  *ppReader = pReader;
  return 0;

_err:
  *ppReader = NULL;
  return -1;
}

int32_t vnodeSnapshotReaderClose(SVSnapshotReader *pReader) {
  if (pReader) {
H
Hongze Cheng 已提交
66
    vnodeFree(pReader->pData);
H
Hongze Cheng 已提交
67 68 69 70 71 72 73
    tsdbSnapshotReaderClose(pReader->pTsdbReader);
    metaSnapshotReaderClose(pReader->pMetaReader);
    taosMemoryFree(pReader);
  }
  return 0;
}

H
Hongze Cheng 已提交
74
int32_t vnodeSnapshotRead(SVSnapshotReader *pReader, const void **ppData, uint32_t *nData) {
H
Hongze Cheng 已提交
75 76 77
  int32_t code = 0;

  if (!pReader->isMetaEnd) {
H
Hongze Cheng 已提交
78
    code = metaSnapshotRead(pReader->pMetaReader, &pReader->pData, &pReader->nData);
H
Hongze Cheng 已提交
79 80 81 82 83 84 85
    if (code) {
      if (code == TSDB_CODE_VND_READ_END) {
        pReader->isMetaEnd = 1;
      } else {
        return code;
      }
    } else {
H
Hongze Cheng 已提交
86 87
      *ppData = pReader->pData;
      *nData = pReader->nData;
H
Hongze Cheng 已提交
88 89 90 91 92
      return code;
    }
  }

  if (!pReader->isTsdbEnd) {
H
Hongze Cheng 已提交
93
    code = tsdbSnapshotRead(pReader->pTsdbReader, &pReader->pData, &pReader->nData);
H
Hongze Cheng 已提交
94
    if (code) {
H
Hongze Cheng 已提交
95 96 97 98 99
      if (code == TSDB_CODE_VND_READ_END) {
        pReader->isTsdbEnd = 1;
      } else {
        return code;
      }
H
Hongze Cheng 已提交
100
    } else {
H
Hongze Cheng 已提交
101 102
      *ppData = pReader->pData;
      *nData = pReader->nData;
H
Hongze Cheng 已提交
103 104 105 106 107 108 109
      return code;
    }
  }

  code = TSDB_CODE_VND_READ_END;
  return code;
}