walInt.h 4.2 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 "compare.h"
L
Liu Jicong 已提交
20
#include "tchecksum.h"
L
Liu Jicong 已提交
21
#include "wal.h"
L
Liu Jicong 已提交
22

23 24
#include "taoserror.h"

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

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

L
Liu Jicong 已提交
38 39 40
typedef struct WalIdxEntry {
  int64_t ver;
  int64_t offset;
L
Liu Jicong 已提交
41
} SWalIdxEntry;
L
Liu Jicong 已提交
42

L
Liu Jicong 已提交
43
static inline int32_t compareWalFileInfo(const void* pLeft, const void* pRight) {
L
Liu Jicong 已提交
44 45
  SWalFileInfo* pInfoLeft = (SWalFileInfo*)pLeft;
  SWalFileInfo* pInfoRight = (SWalFileInfo*)pRight;
L
Liu Jicong 已提交
46 47 48 49
  return compareInt64Val(&pInfoLeft->firstVer, &pInfoRight->firstVer);
}

static inline int64_t walGetLastFileSize(SWal* pWal) {
L
Liu Jicong 已提交
50
  SWalFileInfo* pInfo = (SWalFileInfo*)taosArrayGetLast(pWal->fileInfoSet);
L
Liu Jicong 已提交
51 52 53 54
  return pInfo->fileSize;
}

static inline int64_t walGetLastFileFirstVer(SWal* pWal) {
L
Liu Jicong 已提交
55
  SWalFileInfo* pInfo = (SWalFileInfo*)taosArrayGetLast(pWal->fileInfoSet);
L
Liu Jicong 已提交
56 57 58 59
  return pInfo->firstVer;
}

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

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

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

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

L
Liu Jicong 已提交
76 77
static inline SWalFileInfo* walGetCurFileInfo(SWal* pWal) {
  return (SWalFileInfo*)taosArrayGet(pWal->fileInfoSet, pWal->writeCur);
L
Liu Jicong 已提交
78 79
}

L
Liu Jicong 已提交
80
static inline int walBuildLogName(SWal* pWal, int64_t fileFirstVer, char* buf) {
L
Liu Jicong 已提交
81
  return sprintf(buf, "%s/%020" PRId64 "." WAL_LOG_SUFFIX, pWal->path, fileFirstVer);
L
Liu Jicong 已提交
82 83
}

L
Liu Jicong 已提交
84
static inline int walBuildIdxName(SWal* pWal, int64_t fileFirstVer, char* buf) {
L
Liu Jicong 已提交
85
  return sprintf(buf, "%s/%020" PRId64 "." WAL_INDEX_SUFFIX, pWal->path, fileFirstVer);
L
Liu Jicong 已提交
86 87
}

L
Liu Jicong 已提交
88 89 90 91 92
static inline int walValidHeadCksum(SWalHead* pHead) {
  return taosCheckChecksum((uint8_t*)&pHead->head, sizeof(SWalReadHead), pHead->cksumHead);
}

static inline int walValidBodyCksum(SWalHead* pHead) {
L
Liu Jicong 已提交
93
  return taosCheckChecksum((uint8_t*)pHead->head.body, pHead->head.len, pHead->cksumBody);
L
Liu Jicong 已提交
94 95
}

L
Liu Jicong 已提交
96
static inline int walValidCksum(SWalHead* pHead, void* body, int64_t bodyLen) {
L
Liu Jicong 已提交
97 98 99
  return walValidHeadCksum(pHead) && walValidBodyCksum(pHead);
}

L
Liu Jicong 已提交
100
static inline uint32_t walCalcHeadCksum(SWalHead* pHead) {
L
Liu Jicong 已提交
101 102 103 104 105 106 107
  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
static inline int64_t walGetVerIdxOffset(SWal* pWal, int64_t ver) {
L
Liu Jicong 已提交
109
  return (ver - walGetCurFileFirstVer(pWal)) * sizeof(SWalIdxEntry);
L
Liu Jicong 已提交
110 111
}

L
Liu Jicong 已提交
112 113 114 115 116 117 118 119 120 121
static inline void walResetVer(SWalVer* pVer) {
  pVer->firstVer = -1;
  pVer->verInSnapshotting = -1;
  pVer->snapshotVer = -1;
  pVer->commitVer = -1;
  pVer->lastVer = -1;
}

int walLoadMeta(SWal* pWal);
int walSaveMeta(SWal* pWal);
L
Liu Jicong 已提交
122 123
int walRollFileInfo(SWal* pWal);

L
Liu Jicong 已提交
124 125 126 127
int walCheckAndRepairMeta(SWal* pWal);

int walCheckAndRepairIdx(SWal* pWal);

128
char* walMetaSerialize(SWal* pWal);
L
Liu Jicong 已提交
129 130
int   walMetaDeserialize(SWal* pWal, const char* bytes);
// meta section end
L
Liu Jicong 已提交
131

L
Liu Jicong 已提交
132 133 134
// seek section
int walChangeFile(SWal* pWal, int64_t ver);
// seek section end
L
Liu Jicong 已提交
135

L
Liu Jicong 已提交
136
int64_t walGetSeq();
L
Liu Jicong 已提交
137 138
int     walSeekVer(SWal* pWal, int64_t ver);
int     walRoll(SWal* pWal);
L
Liu Jicong 已提交
139

H
refact  
Hongze Cheng 已提交
140 141 142 143
#ifdef __cplusplus
}
#endif

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