walInt.h 4.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 "taoserror.h"
L
Liu Jicong 已提交
20
#include "tchecksum.h"
L
Liu Jicong 已提交
21
#include "tcoding.h"
L
Liu Jicong 已提交
22
#include "tcompare.h"
L
Liu Jicong 已提交
23
#include "wal.h"
L
Liu Jicong 已提交
24

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 int tSerializeWalIdxEntry(void** buf, SWalIdxEntry* pIdxEntry) {
L
Liu Jicong 已提交
44
  int tlen = 0;
L
Liu Jicong 已提交
45 46
  tlen += taosEncodeFixedI64(buf, pIdxEntry->ver);
  tlen += taosEncodeFixedI64(buf, pIdxEntry->offset);
L
Liu Jicong 已提交
47
  return tlen;
L
Liu Jicong 已提交
48 49 50 51 52 53 54 55
}

static inline void* tDeserializeWalIdxEntry(void* buf, SWalIdxEntry* pIdxEntry) {
  buf = taosDecodeFixedI64(buf, &pIdxEntry->ver);
  buf = taosDecodeFixedI64(buf, &pIdxEntry->offset);
  return buf;
}

L
Liu Jicong 已提交
56
static inline int32_t compareWalFileInfo(const void* pLeft, const void* pRight) {
L
Liu Jicong 已提交
57 58
  SWalFileInfo* pInfoLeft = (SWalFileInfo*)pLeft;
  SWalFileInfo* pInfoRight = (SWalFileInfo*)pRight;
L
Liu Jicong 已提交
59 60 61 62
  return compareInt64Val(&pInfoLeft->firstVer, &pInfoRight->firstVer);
}

static inline int64_t walGetLastFileSize(SWal* pWal) {
L
Liu Jicong 已提交
63
  SWalFileInfo* pInfo = (SWalFileInfo*)taosArrayGetLast(pWal->fileInfoSet);
L
Liu Jicong 已提交
64 65 66 67
  return pInfo->fileSize;
}

static inline int64_t walGetLastFileFirstVer(SWal* pWal) {
L
Liu Jicong 已提交
68
  SWalFileInfo* pInfo = (SWalFileInfo*)taosArrayGetLast(pWal->fileInfoSet);
L
Liu Jicong 已提交
69 70 71 72
  return pInfo->firstVer;
}

static inline int64_t walGetCurFileFirstVer(SWal* pWal) {
L
Liu Jicong 已提交
73
  SWalFileInfo* pInfo = (SWalFileInfo*)taosArrayGet(pWal->fileInfoSet, pWal->writeCur);
L
Liu Jicong 已提交
74 75 76 77
  return pInfo->firstVer;
}

static inline int64_t walGetCurFileLastVer(SWal* pWal) {
L
Liu Jicong 已提交
78
  SWalFileInfo* pInfo = (SWalFileInfo*)taosArrayGet(pWal->fileInfoSet, pWal->writeCur);
L
Liu Jicong 已提交
79 80 81 82
  return pInfo->firstVer;
}

static inline int64_t walGetCurFileOffset(SWal* pWal) {
L
Liu Jicong 已提交
83
  SWalFileInfo* pInfo = (SWalFileInfo*)taosArrayGet(pWal->fileInfoSet, pWal->writeCur);
L
Liu Jicong 已提交
84 85 86
  return pInfo->fileSize;
}

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

L
Liu Jicong 已提交
89 90
static inline SWalFileInfo* walGetCurFileInfo(SWal* pWal) {
  return (SWalFileInfo*)taosArrayGet(pWal->fileInfoSet, pWal->writeCur);
L
Liu Jicong 已提交
91 92
}

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

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

L
Liu Jicong 已提交
101 102 103 104 105
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 已提交
106
  return taosCheckChecksum((uint8_t*)pHead->head.body, pHead->head.bodyLen, pHead->cksumBody);
L
Liu Jicong 已提交
107 108
}

L
Liu Jicong 已提交
109
static inline int walValidCksum(SWalHead* pHead, void* body, int64_t bodyLen) {
L
Liu Jicong 已提交
110 111 112
  return walValidHeadCksum(pHead) && walValidBodyCksum(pHead);
}

L
Liu Jicong 已提交
113
static inline uint32_t walCalcHeadCksum(SWalHead* pHead) {
L
Liu Jicong 已提交
114 115 116 117 118 119 120
  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 已提交
121
static inline int64_t walGetVerIdxOffset(SWal* pWal, int64_t ver) {
L
Liu Jicong 已提交
122
  return (ver - walGetCurFileFirstVer(pWal)) * sizeof(SWalIdxEntry);
L
Liu Jicong 已提交
123 124
}

L
Liu Jicong 已提交
125 126 127 128 129 130 131 132 133 134
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 已提交
135 136
int walRollFileInfo(SWal* pWal);

L
Liu Jicong 已提交
137 138 139 140
int walCheckAndRepairMeta(SWal* pWal);

int walCheckAndRepairIdx(SWal* pWal);

141
char* walMetaSerialize(SWal* pWal);
L
Liu Jicong 已提交
142 143
int   walMetaDeserialize(SWal* pWal, const char* bytes);
// meta section end
L
Liu Jicong 已提交
144

L
Liu Jicong 已提交
145
// seek section
L
Liu Jicong 已提交
146 147
int walChangeWrite(SWal* pWal, int64_t ver);
int walSetWrite(SWal* pWal);
L
Liu Jicong 已提交
148
// seek section end
L
Liu Jicong 已提交
149

L
Liu Jicong 已提交
150
int64_t walGetSeq();
L
Liu Jicong 已提交
151
int     walSeekWriteVer(SWal* pWal, int64_t ver);
L
Liu Jicong 已提交
152
int     walRoll(SWal* pWal);
L
Liu Jicong 已提交
153

H
refact  
Hongze Cheng 已提交
154 155 156 157
#ifdef __cplusplus
}
#endif

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