metaSnapshot.c 2.8 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
// SMetaSnapReader ========================================
H
Hongze Cheng 已提交
19
struct SMetaSnapReader {
H
Hongze Cheng 已提交
20 21 22
  SMeta*  pMeta;
  int64_t sver;
  int64_t ever;
H
Hongze Cheng 已提交
23
  TBC*    pTbc;
H
Hongze Cheng 已提交
24 25
};

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

H
Hongze Cheng 已提交
31 32 33
  // alloc
  pMetaSnapReader = (SMetaSnapReader*)taosMemoryCalloc(1, sizeof(*pMetaSnapReader));
  if (pMetaSnapReader == NULL) {
H
Hongze Cheng 已提交
34 35 36
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
37 38 39 40 41 42
  pMetaSnapReader->pMeta = pMeta;
  pMetaSnapReader->sver = sver;
  pMetaSnapReader->ever = ever;

  // impl
  code = tdbTbcOpen(pMeta->pTbDb, &pMetaSnapReader->pTbc, NULL);
H
Hongze Cheng 已提交
43 44 45 46
  if (code) {
    goto _err;
  }

H
Hongze Cheng 已提交
47
  code = tdbTbcMoveTo(pMetaSnapReader->pTbc, &(STbDbKey){.version = sver, .uid = INT64_MIN}, sizeof(STbDbKey), &c);
H
Hongze Cheng 已提交
48 49 50 51
  if (code) {
    goto _err;
  }

H
Hongze Cheng 已提交
52
  *ppReader = pMetaSnapReader;
H
Hongze Cheng 已提交
53 54 55
  return code;

_err:
H
Hongze Cheng 已提交
56
  metaError("vgId:%d meta snap reader open failed since %s", TD_VID(pMeta->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
57 58
  *ppReader = NULL;
  return code;
H
Hongze Cheng 已提交
59 60
}

H
Hongze Cheng 已提交
61 62 63 64
int32_t metaSnapReaderClose(SMetaSnapReader** ppReader) {
  tdbTbcClose((*ppReader)->pTbc);
  taosMemoryFree(*ppReader);
  *ppReader = NULL;
H
Hongze Cheng 已提交
65 66 67
  return 0;
}

H
Hongze Cheng 已提交
68
int32_t metaSnapRead(SMetaSnapReader* pReader, uint8_t** ppData) {
H
Hongze Cheng 已提交
69 70 71 72 73 74 75 76 77
  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) {
H
Hongze Cheng 已提交
78 79
      code = TSDB_CODE_VND_READ_END;
      goto _exit;
H
Hongze Cheng 已提交
80 81 82
    }

    if (((STbDbKey*)pData)->version < pReader->sver) {
H
Hongze Cheng 已提交
83
      tdbTbcMoveToNext(pReader->pTbc);
H
Hongze Cheng 已提交
84 85 86
      continue;
    }

H
Hongze Cheng 已提交
87
    tdbTbcMoveToNext(pReader->pTbc);
H
Hongze Cheng 已提交
88 89 90 91
    break;
  }

  // copy the data
H
Hongze Cheng 已提交
92
  if (tRealloc(ppData, sizeof(SSnapDataHdr) + nData) < 0) {
H
Hongze Cheng 已提交
93 94 95
    code = TSDB_CODE_OUT_OF_MEMORY;
    return code;
  }
H
Hongze Cheng 已提交
96 97 98
  ((SSnapDataHdr*)(*ppData))->type = 0;  // TODO: use macro
  ((SSnapDataHdr*)(*ppData))->size = nData;
  memcpy(((SSnapDataHdr*)(*ppData))->data, pData, nData);
H
Hongze Cheng 已提交
99 100

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

// SMetaSnapWriter ========================================