-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_bolt.go
269 lines (251 loc) · 5.8 KB
/
log_bolt.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
261
262
263
264
265
266
267
268
269
package raft
import (
"fmt"
"github.com/sumimakito/raft/pb"
"go.etcd.io/bbolt"
"google.golang.org/protobuf/proto"
)
const (
boltLogStoreBucketLogs = "logs"
boltLogStoreBucketCmdIndexes = "cmd_indexes"
boltLogStoreBucketConfIndexes = "conf_indexes"
)
// BoltLogStore is a LogStore that uses bbolt as a backend.
type BoltLogStore struct {
db *bbolt.DB
}
func NewBoltLogStore(db *bbolt.DB) *BoltLogStore {
return &BoltLogStore{db: db}
}
func (s *BoltLogStore) encodeLog(log *pb.Log) ([]byte, error) {
b, err := proto.Marshal(log)
if err != nil {
return nil, err
}
return b, nil
}
func (s *BoltLogStore) decodeLog(in []byte) (*pb.Log, error) {
var pbLog pb.Log
if err := proto.Unmarshal(in, &pbLog); err != nil {
return nil, err
}
return &pbLog, nil
}
func (s *BoltLogStore) putLogIndex(tx *bbolt.Tx, t pb.LogType, index uint64) error {
var bucket *bbolt.Bucket
var err error
switch t {
case pb.LogType_COMMAND:
bucket, err = tx.CreateBucketIfNotExists([]byte(boltLogStoreBucketCmdIndexes))
case pb.LogType_CONFIGURATION:
bucket, err = tx.CreateBucketIfNotExists([]byte(boltLogStoreBucketConfIndexes))
}
if err != nil {
return err
}
return bucket.Put(EncodeUint64(index), nil)
}
func (s *BoltLogStore) deleteLogIndex(tx *bbolt.Tx, t pb.LogType, index uint64) error {
var bucket *bbolt.Bucket
var err error
switch t {
case pb.LogType_COMMAND:
bucket, err = tx.CreateBucketIfNotExists([]byte(boltLogStoreBucketCmdIndexes))
case pb.LogType_CONFIGURATION:
bucket, err = tx.CreateBucketIfNotExists([]byte(boltLogStoreBucketCmdIndexes))
}
if err != nil {
return err
}
return bucket.Delete(EncodeUint64(index))
}
func (s *BoltLogStore) AppendLogs(logs []*pb.Log) error {
return s.db.Update(func(t *bbolt.Tx) error {
bucket, err := t.CreateBucketIfNotExists([]byte(boltLogStoreBucketLogs))
if err != nil {
return err
}
for i := range logs {
logBytes, err := s.encodeLog(logs[i])
if err != nil {
return err
}
if err := bucket.Put(EncodeUint64(logs[i].Meta.Index), logBytes); err != nil {
return err
}
if err := s.putLogIndex(t, logs[i].Body.Type, logs[i].Meta.Index); err != nil {
return err
}
}
return nil
})
}
func (s *BoltLogStore) TrimPrefix(index uint64) error {
return s.db.Update(func(t *bbolt.Tx) error {
bucket := t.Bucket([]byte(boltLogStoreBucketLogs))
if bucket == nil {
return nil
}
c := bucket.Cursor()
key, value := c.First()
for key != nil && DecodeUint64(key) < index {
log, err := s.decodeLog(value)
if err != nil {
return err
}
if err := s.deleteLogIndex(t, log.Body.Type, DecodeUint64(key)); err != nil {
return err
}
if err := c.Delete(); err != nil {
return err
}
key, value = c.Next()
}
return nil
})
}
func (s *BoltLogStore) TrimSuffix(index uint64) error {
return s.db.Update(func(t *bbolt.Tx) error {
bucket := t.Bucket([]byte(boltLogStoreBucketLogs))
if bucket == nil {
return nil
}
c := bucket.Cursor()
key, value := c.Last()
for key != nil && DecodeUint64(key) > index {
log, err := s.decodeLog(value)
if err != nil {
return err
}
if err := s.deleteLogIndex(t, log.Body.Type, DecodeUint64(key)); err != nil {
return err
}
if err := c.Delete(); err != nil {
return err
}
key, value = c.Prev()
}
return nil
})
}
func (s *BoltLogStore) FirstIndex() (uint64, error) {
var index uint64
return index, s.db.View(func(t *bbolt.Tx) error {
bucket := t.Bucket([]byte(boltLogStoreBucketLogs))
if bucket == nil {
return nil
}
c := bucket.Cursor()
key, _ := c.First()
if key == nil {
return nil
}
index = DecodeUint64(key)
return nil
})
}
func (s *BoltLogStore) LastIndex() (uint64, error) {
var index uint64
return index, s.db.View(func(t *bbolt.Tx) error {
bucket := t.Bucket([]byte(boltLogStoreBucketLogs))
if bucket == nil {
return nil
}
c := bucket.Cursor()
key, _ := c.Last()
if key == nil {
return nil
}
index = DecodeUint64(key)
return nil
})
}
func (s *BoltLogStore) Entry(index uint64) (*pb.Log, error) {
var log *pb.Log
return log, s.db.View(func(t *bbolt.Tx) error {
bucket := t.Bucket([]byte(boltLogStoreBucketLogs))
if bucket == nil {
return nil
}
value := bucket.Get(EncodeUint64(index))
if value == nil {
return nil
}
if l, err := s.decodeLog(value); err != nil {
return err
} else {
log = l
}
return nil
})
}
func (s *BoltLogStore) LastEntry(t pb.LogType) (*pb.Log, error) {
var log *pb.Log
return log, s.db.View(func(tx *bbolt.Tx) error {
var lastKey []byte
if t != 0 {
var bucket *bbolt.Bucket
switch t {
case pb.LogType_COMMAND:
bucket = tx.Bucket([]byte(boltLogStoreBucketCmdIndexes))
case pb.LogType_CONFIGURATION:
bucket = tx.Bucket([]byte(boltLogStoreBucketConfIndexes))
default:
return nil
}
if bucket == nil {
return nil
}
key, _ := bucket.Cursor().Last()
if key == nil {
return nil
}
lastKey = key
}
bucket := tx.Bucket([]byte(boltLogStoreBucketLogs))
if bucket == nil {
return nil
}
var lastValue []byte
if lastKey != nil {
lastValue = bucket.Get(lastKey)
} else {
key, value := bucket.Cursor().Last()
if key == nil {
return nil
}
lastValue = value
}
if l, err := s.decodeLog(lastValue); err != nil {
return err
} else {
log = l
}
return nil
})
}
func (s *BoltLogStore) DebugPrint() {
if err := s.db.View(func(t *bbolt.Tx) error {
bucket := t.Bucket([]byte(boltLogStoreBucketLogs))
if bucket == nil {
return nil
}
c := bucket.Cursor()
key, value := c.First()
for key != nil {
log, err := s.decodeLog(value)
if err != nil {
return err
}
fmt.Println(DecodeUint64(key), log)
key, value = c.Next()
}
fmt.Println()
return nil
}); err != nil {
panic(err)
}
}
func (p *BoltLogStore) Close() error {
return p.db.Close()
}