walRead.c 2.4 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * 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/>.
S
Shengliang Guan 已提交
14 15
 */

L
Liu Jicong 已提交
16 17
#include "walInt.h"
#include "tfile.h"
L
Liu Jicong 已提交
18
#include "tchecksum.h"
S
Shengliang Guan 已提交
19

L
Liu Jicong 已提交
20 21
static inline int walValidHeadCksum(SWalHead* pHead) {
  return taosCheckChecksum((uint8_t*)pHead, sizeof(SWalHead) - sizeof(uint32_t)*2, pHead->cksumHead);
L
Liu Jicong 已提交
22
}
23

L
Liu Jicong 已提交
24 25 26 27 28 29 30
static inline int walValidBodyCksum(SWalHead* pHead) {
  return taosCheckChecksum((uint8_t*)pHead->cont, pHead->len, pHead->cksumBody);
}

static int walValidCksum(SWalHead *pHead, void* body, int64_t bodyLen) {
  return walValidHeadCksum(pHead) && walValidBodyCksum(pHead);
}
S
Shengliang Guan 已提交
31

L
Liu Jicong 已提交
32
int32_t walRead(SWal *pWal, SWalHead **ppHead, int64_t ver) {
L
Liu Jicong 已提交
33 34 35 36 37 38 39 40 41 42 43 44
  int code;
  code = walSeekVer(pWal, ver);
  if(code != 0) {
    return code;
  }
  if(*ppHead == NULL) {
    void* ptr = realloc(*ppHead, sizeof(SWalHead));
    if(ptr == NULL) {
      return -1;
    }
    *ppHead = ptr;
  }
L
Liu Jicong 已提交
45
  if(tfRead(pWal->writeLogTfd, *ppHead, sizeof(SWalHead)) != sizeof(SWalHead)) {
L
Liu Jicong 已提交
46 47 48 49 50 51 52 53 54 55 56 57
    return -1;
  }
  //TODO: endian compatibility processing after read
  if(walValidHeadCksum(*ppHead) != 0) {
    return -1;
  }
  void* ptr = realloc(*ppHead, sizeof(SWalHead) + (*ppHead)->len);
  if(ptr == NULL) {
    free(*ppHead);
    *ppHead = NULL;
    return -1;
  }
L
Liu Jicong 已提交
58
  if(tfRead(pWal->writeLogTfd, (*ppHead)->cont, (*ppHead)->len) != (*ppHead)->len) {
L
Liu Jicong 已提交
59 60 61 62 63 64 65
    return -1;
  }
  //TODO: endian compatibility processing after read
  if(walValidBodyCksum(*ppHead) != 0) {
    return -1;
  }
  
L
Liu Jicong 已提交
66 67
  return 0;
}
S
Shengliang Guan 已提交
68

L
Liu Jicong 已提交
69 70 71
int32_t walReadWithFp(SWal *pWal, FWalWrite writeFp, int64_t verStart, int32_t readNum) {
  return 0;
}
L
Liu Jicong 已提交
72

L
Liu Jicong 已提交
73
int64_t walGetFirstVer(SWal *pWal) {
L
Liu Jicong 已提交
74 75
  if (pWal == NULL) return 0;
  return pWal->firstVersion;
L
Liu Jicong 已提交
76
}
L
Liu Jicong 已提交
77

L
Liu Jicong 已提交
78 79 80
int64_t walGetSnaphostVer(SWal *pWal) {
  if (pWal == NULL) return 0;
  return pWal->snapshotVersion;
L
Liu Jicong 已提交
81
}
L
Liu Jicong 已提交
82

L
Liu Jicong 已提交
83
int64_t walGetLastVer(SWal *pWal) {
L
Liu Jicong 已提交
84 85
  if (pWal == NULL) return 0;
  return pWal->lastVersion;
L
Liu Jicong 已提交
86
}