forked from ngaut/unistore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom.go
260 lines (218 loc) · 5.85 KB
/
custom.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright 2019-present PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package raftlog
import (
"encoding/binary"
"fmt"
"unsafe"
"github.com/pingcap/kvproto/pkg/raft_cmdpb"
)
type CustomRaftLogType byte
const (
CustomRaftLogFlag byte = 64
TypePrewrite CustomRaftLogType = 1
TypeCommit CustomRaftLogType = 2
TypeRolback CustomRaftLogType = 3
TypePessimisticLock CustomRaftLogType = 4
TypePessimisticRollback CustomRaftLogType = 5
)
// CustomRaftLog is the raft log format for unistore to store Prewrite/Commit/PessimisticLock.
// | flag(1) | type(1) | version(2) | header(40) | entries
//
// It reduces the cost of marshal/unmarshal and avoid DB lookup during apply.
type CustomRaftLog struct {
header *CustomHeader
Data []byte
}
func NewCustom(data []byte) *CustomRaftLog {
rlog := &CustomRaftLog{}
rlog.header = (*CustomHeader)(unsafe.Pointer(&data[4]))
rlog.Data = data
return rlog
}
func (c *CustomRaftLog) Type() CustomRaftLogType {
return CustomRaftLogType(c.Data[1])
}
func (c *CustomRaftLog) RegionID() uint64 {
return c.header.RegionID
}
func (c *CustomRaftLog) PeerID() uint64 {
return c.header.PeerID
}
func (c *CustomRaftLog) StoreID() uint64 {
return c.header.StoreID
}
func (c *CustomRaftLog) Epoch() Epoch {
return c.header.Epoch
}
func (c *CustomRaftLog) Term() uint64 {
return c.header.Term
}
func (c *CustomRaftLog) Marshal() []byte {
return c.Data
}
func (c *CustomRaftLog) GetRaftCmdRequest() *raft_cmdpb.RaftCmdRequest {
return nil
}
type CustomHeader struct {
RegionID uint64
Epoch Epoch
PeerID uint64
StoreID uint64
Term uint64
}
const headerSize = int(unsafe.Sizeof(CustomHeader{}))
func (h *CustomHeader) Marshal() []byte {
data := make([]byte, headerSize)
*(*CustomHeader)(unsafe.Pointer(&data[0])) = *h
return data
}
type Epoch struct {
ver uint32
confVer uint32
}
func NewEpoch(ver, confVer uint64) Epoch {
return Epoch{ver: uint32(ver), confVer: uint32(confVer)}
}
func (e Epoch) Ver() uint64 {
return uint64(e.ver)
}
func (e Epoch) ConfVer() uint64 {
return uint64(e.confVer)
}
func (e Epoch) String() string {
return fmt.Sprintf("{Ver:%d, ConfVer:%d}", e.ver, e.confVer)
}
func (rl *CustomRaftLog) IterateLock(itFunc func(key, val []byte)) {
i := 4 + headerSize
for i < len(rl.Data) {
keyLen := endian.Uint16(rl.Data[i:])
i += 2
key := rl.Data[i : i+int(keyLen)]
i += int(keyLen)
valLen := endian.Uint32(rl.Data[i:])
i += 4
val := rl.Data[i : i+int(valLen)]
i += int(valLen)
itFunc(key, val)
}
}
func (rl *CustomRaftLog) IterateCommit(itFunc func(key, val []byte, commitTS uint64)) {
i := 4 + headerSize
for i < len(rl.Data) {
keyLen := endian.Uint16(rl.Data[i:])
i += 2
key := rl.Data[i : i+int(keyLen)]
i += int(keyLen)
valLen := endian.Uint32(rl.Data[i:])
i += 4
val := rl.Data[i : i+int(valLen)]
i += int(valLen)
commitTS := endian.Uint64(rl.Data[i:])
i += 8
itFunc(key, val, commitTS)
}
}
func (rl *CustomRaftLog) IterateRollback(itFunc func(key []byte, startTS uint64, deleteLock bool)) {
i := 4 + headerSize
for i < len(rl.Data) {
keyLen := endian.Uint16(rl.Data[i:])
i += 2
key := rl.Data[i : i+int(keyLen)]
i += int(keyLen)
startTS := endian.Uint64(rl.Data[i:])
i += 8
del := rl.Data[i]
i++
itFunc(key, startTS, del > 0)
}
}
func (rl *CustomRaftLog) IteratePessimisticRollback(itFunc func(key []byte)) {
i := 4 + headerSize
for i < len(rl.Data) {
keyLen := endian.Uint16(rl.Data[i:])
i += 2
key := rl.Data[i : i+int(keyLen)]
i += int(keyLen)
itFunc(key)
}
}
type CustomBuilder struct {
data []byte
cnt int
}
func NewBuilder(header CustomHeader) *CustomBuilder {
b := &CustomBuilder{}
b.data = append(b.data, CustomRaftLogFlag, 0, 0, 0)
b.data = append(b.data, header.Marshal()...)
return b
}
func (b *CustomBuilder) AppendLock(key, value []byte) {
b.data = append(b.data, u16ToBytes(uint16(len(key)))...)
b.data = append(b.data, key...)
b.data = append(b.data, u32ToBytes(uint32(len(value)))...)
b.data = append(b.data, value...)
b.cnt++
}
func (b *CustomBuilder) AppendCommit(key, value []byte, commitTS uint64) {
b.data = append(b.data, u16ToBytes(uint16(len(key)))...)
b.data = append(b.data, key...)
b.data = append(b.data, u32ToBytes(uint32(len(value)))...)
b.data = append(b.data, value...)
b.data = append(b.data, u64ToBytes(commitTS)...)
b.cnt++
}
func (b *CustomBuilder) AppendRollback(key []byte, startTS uint64, deleteLock bool) {
b.data = append(b.data, u16ToBytes(uint16(len(key)))...)
b.data = append(b.data, key...)
b.data = append(b.data, u64ToBytes(startTS)...)
if deleteLock {
b.data = append(b.data, 1)
} else {
b.data = append(b.data, 0)
}
b.cnt++
}
func (b *CustomBuilder) AppendPessimisticRollback(key []byte) {
b.data = append(b.data, u16ToBytes(uint16(len(key)))...)
b.data = append(b.data, key...)
b.cnt++
}
func (b *CustomBuilder) SetType(tp CustomRaftLogType) {
b.data[1] = byte(tp)
}
func (b *CustomBuilder) GetType() CustomRaftLogType {
return CustomRaftLogType(b.data[1])
}
func (b *CustomBuilder) Build() *CustomRaftLog {
return NewCustom(b.data)
}
func (b *CustomBuilder) Len() int {
return b.cnt
}
var endian = binary.LittleEndian
func u16ToBytes(v uint16) []byte {
b := make([]byte, 2)
endian.PutUint16(b, v)
return b
}
func u32ToBytes(v uint32) []byte {
b := make([]byte, 4)
endian.PutUint32(b, v)
return b
}
func u64ToBytes(v uint64) []byte {
b := make([]byte, 8)
endian.PutUint64(b, v)
return b
}