walInt.h 3.7 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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/>.
 */

#ifndef _TD_WAL_INT_H_
#define _TD_WAL_INT_H_

L
Liu Jicong 已提交
19
#include "wal.h"
L
Liu Jicong 已提交
20
#include "compare.h"
L
Liu Jicong 已提交
21
#include "tchecksum.h"
L
Liu Jicong 已提交
22

H
refact  
Hongze Cheng 已提交
23 24 25 26
#ifdef __cplusplus
extern "C" {
#endif

L
Liu Jicong 已提交
27 28 29 30 31 32 33 34 35
//meta section begin
typedef struct WalFileInfo {
  int64_t firstVer;
  int64_t lastVer;
  int64_t createTs;
  int64_t closeTs;
  int64_t fileSize;
} WalFileInfo;

L
Liu Jicong 已提交
36 37 38 39 40
typedef struct WalIdxEntry {
  int64_t ver;
  int64_t offset;
} WalIdxEntry;

L
Liu Jicong 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
static inline int32_t compareWalFileInfo(const void* pLeft, const void* pRight) {
  WalFileInfo* pInfoLeft = (WalFileInfo*)pLeft;
  WalFileInfo* pInfoRight = (WalFileInfo*)pRight;
  return compareInt64Val(&pInfoLeft->firstVer, &pInfoRight->firstVer);
}

static inline int64_t walGetLastFileSize(SWal* pWal) {
  WalFileInfo* pInfo = (WalFileInfo*)taosArrayGetLast(pWal->fileInfoSet);
  return pInfo->fileSize;
}

static inline int64_t walGetLastFileFirstVer(SWal* pWal) {
  WalFileInfo* pInfo = (WalFileInfo*)taosArrayGetLast(pWal->fileInfoSet);
  return pInfo->firstVer;
}

static inline int64_t walGetCurFileFirstVer(SWal* pWal) {
L
Liu Jicong 已提交
58
  WalFileInfo* pInfo = (WalFileInfo*)taosArrayGet(pWal->fileInfoSet, pWal->writeCur);
L
Liu Jicong 已提交
59 60 61 62
  return pInfo->firstVer;
}

static inline int64_t walGetCurFileLastVer(SWal* pWal) {
L
Liu Jicong 已提交
63
  WalFileInfo* pInfo = (WalFileInfo*)taosArrayGet(pWal->fileInfoSet, pWal->writeCur);
L
Liu Jicong 已提交
64 65 66 67
  return pInfo->firstVer;
}

static inline int64_t walGetCurFileOffset(SWal* pWal) {
L
Liu Jicong 已提交
68
  WalFileInfo* pInfo = (WalFileInfo*)taosArrayGet(pWal->fileInfoSet, pWal->writeCur);
L
Liu Jicong 已提交
69 70 71 72
  return pInfo->fileSize;
}

static inline bool walCurFileClosed(SWal* pWal) {
L
Liu Jicong 已提交
73
  return taosArrayGetSize(pWal->fileInfoSet) != pWal->writeCur;
L
Liu Jicong 已提交
74 75 76
}

static inline WalFileInfo* walGetCurFileInfo(SWal* pWal) {
L
Liu Jicong 已提交
77
  return (WalFileInfo*)taosArrayGet(pWal->fileInfoSet, pWal->writeCur);
L
Liu Jicong 已提交
78 79 80 81 82 83 84 85 86 87
}

static inline int walBuildLogName(SWal*pWal, int64_t fileFirstVer, char* buf) {
  return sprintf(buf, "%s/%" PRId64 "." WAL_LOG_SUFFIX, pWal->path, fileFirstVer);
}

static inline int walBuildIdxName(SWal*pWal, int64_t fileFirstVer, char* buf) {
  return sprintf(buf, "%s/%" PRId64 "." WAL_INDEX_SUFFIX, pWal->path, fileFirstVer);
}

L
Liu Jicong 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
static inline int walValidHeadCksum(SWalHead* pHead) {
  return taosCheckChecksum((uint8_t*)&pHead->head, sizeof(SWalReadHead), pHead->cksumHead);
}

static inline int walValidBodyCksum(SWalHead* pHead) {
  return taosCheckChecksum((uint8_t*)pHead->head.cont, pHead->head.len, pHead->cksumBody);
}

static inline int walValidCksum(SWalHead *pHead, void* body, int64_t bodyLen) {
  return walValidHeadCksum(pHead) && walValidBodyCksum(pHead);
}

static inline uint32_t walCalcHeadCksum(SWalHead *pHead) {
  return taosCalcChecksum(0, (uint8_t*)&pHead->head, sizeof(SWalReadHead));
}

static inline uint32_t walCalcBodyCksum(const void* body, uint32_t len) {
  return taosCalcChecksum(0, (uint8_t*)body, len);
}

L
Liu Jicong 已提交
108 109 110 111
int walReadMeta(SWal* pWal);
int walWriteMeta(SWal* pWal);
int walRollFileInfo(SWal* pWal);

112 113
char* walMetaSerialize(SWal* pWal);
int walMetaDeserialize(SWal* pWal, const char* bytes);
L
Liu Jicong 已提交
114
//meta section end
L
Liu Jicong 已提交
115

L
Liu Jicong 已提交
116 117 118 119
//seek section
int walChangeFile(SWal *pWal, int64_t ver);
//seek section end

L
Liu Jicong 已提交
120
int64_t walGetSeq();
L
Liu Jicong 已提交
121 122
int walSeekVer(SWal *pWal, int64_t ver);
int walRoll(SWal *pWal);
L
Liu Jicong 已提交
123

H
refact  
Hongze Cheng 已提交
124 125 126 127
#ifdef __cplusplus
}
#endif

L
Liu Jicong 已提交
128
#endif /*_TD_WAL_INT_H_*/