metaSnapshot.c 2.3 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 "meta.h"

H
Hongze Cheng 已提交
18
struct SMetaSnapReader {
H
Hongze Cheng 已提交
19 20 21 22
  SMeta*  pMeta;
  TBC*    pTbc;
  int64_t sver;
  int64_t ever;
H
Hongze Cheng 已提交
23 24
};

H
Hongze Cheng 已提交
25
int32_t metaSnapReaderOpen(SMeta* pMeta, int64_t sver, int64_t ever, SMetaSnapReader** ppReader) {
H
Hongze Cheng 已提交
26 27 28
  int32_t          code = 0;
  int32_t          c = 0;
  SMetaSnapReader* pMetaReader = NULL;
H
Hongze Cheng 已提交
29

H
Hongze Cheng 已提交
30
  pMetaReader = (SMetaSnapReader*)taosMemoryCalloc(1, sizeof(*pMetaReader));
H
Hongze Cheng 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  if (pMetaReader == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  pMetaReader->pMeta = pMeta;
  pMetaReader->sver = sver;
  pMetaReader->ever = ever;
  code = tdbTbcOpen(pMeta->pTbDb, &pMetaReader->pTbc, NULL);
  if (code) {
    goto _err;
  }

  code = tdbTbcMoveTo(pMetaReader->pTbc, &(STbDbKey){.version = sver, .uid = INT64_MIN}, sizeof(STbDbKey), &c);
  if (code) {
    goto _err;
  }

  *ppReader = pMetaReader;
  return code;

_err:
  *ppReader = NULL;
  return code;
H
Hongze Cheng 已提交
54 55
}

H
Hongze Cheng 已提交
56
int32_t metaSnapReaderClose(SMetaSnapReader* pReader) {
H
Hongze Cheng 已提交
57 58 59 60
  if (pReader) {
    tdbTbcClose(pReader->pTbc);
    taosMemoryFree(pReader);
  }
H
Hongze Cheng 已提交
61 62 63
  return 0;
}

H
Hongze Cheng 已提交
64
int32_t metaSnapRead(SMetaSnapReader* pReader, void** ppData, uint32_t* nDatap) {
H
Hongze Cheng 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  const void* pKey = NULL;
  const void* pData = NULL;
  int32_t     nKey = 0;
  int32_t     nData = 0;
  int32_t     code = 0;

  for (;;) {
    code = tdbTbcGet(pReader->pTbc, &pKey, &nKey, &pData, &nData);
    if (code || ((STbDbKey*)pData)->version > pReader->ever) {
      return TSDB_CODE_VND_READ_END;
    }

    if (((STbDbKey*)pData)->version < pReader->sver) {
      continue;
    }

    break;
  }

  // copy the data
  if (vnodeRealloc(ppData, nData) < 0) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    return code;
  }

  memcpy(*ppData, pData, nData);
  *nDatap = nData;
  return code;
H
Hongze Cheng 已提交
93
}