-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_store.go
125 lines (110 loc) · 2.59 KB
/
log_store.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
package distribution
import (
"encoding/binary"
"github.com/dgraph-io/badger"
"github.com/elliotcourant/arkdb/pkg/storage"
"github.com/hashicorp/raft"
)
type raftLogStore struct {
db *badger.DB
}
func (r *raftLogStore) FirstIndex() (uint64, error) {
return r.index(false)
}
func (r *raftLogStore) LastIndex() (uint64, error) {
return r.index(true)
}
func (r *raftLogStore) GetLog(index uint64, log *raft.Log) error {
l := &storage.Log{}
err := r.db.View(func(txn *badger.Txn) error {
item, err := txn.Get(r.getKeyForIndex(index))
if err != nil {
return raft.ErrLogNotFound
}
v := make([]byte, 0)
_, err = item.ValueCopy(v)
if err != nil {
return err
}
return l.Decode(v)
})
*log = l.Log
return err
}
func (r *raftLogStore) StoreLog(log *raft.Log) error {
return r.StoreLogs([]*raft.Log{log})
}
func (r *raftLogStore) StoreLogs(logs []*raft.Log) error {
return r.db.Update(func(txn *badger.Txn) error {
for _, raftLog := range logs {
log := storage.NewLogFromRaft(raftLog)
if err := txn.Set(log.Path(), log.Encode()); err != nil {
return err
}
}
return nil
})
}
func (r *raftLogStore) DeleteRange(min, max uint64) error {
return r.db.Update(func(txn *badger.Txn) error {
it := txn.NewIterator(badger.IteratorOptions{
PrefetchValues: false,
})
defer it.Close()
start := r.getKeyForIndex(min)
keys := make([][]byte, 0)
for it.Seek(start); it.Valid(); it.Next() {
k := make([]byte, 0)
it.Item().KeyCopy(k)
index := r.getIndexForKey(k)
if index > max {
break
}
keys = append(keys, k)
}
for _, key := range keys {
if err := txn.Delete(key); err != nil {
return err
}
}
return nil
})
}
func (r *raftLogStore) index(reverse bool) (val uint64, err error) {
val = 0
err = r.db.View(func(txn *badger.Txn) error {
it := txn.NewIterator(badger.IteratorOptions{
PrefetchValues: false,
Reverse: reverse,
})
defer it.Close()
k := make([]byte, r.getKeySize())
for it.Seek(r.getPrefix()); it.ValidForPrefix(r.getPrefix()); {
it.Item().KeyCopy(k)
val = r.getIndexForKey(k)
return nil
}
return nil
})
return val, err
}
func (r raftLogStore) getPrefix() []byte {
return []byte{storage.MetaPrefix_Log}
}
func (r raftLogStore) getIndexForKey(key []byte) uint64 {
k := key[len(r.getPrefix()):]
if len(k) == 8 {
return binary.BigEndian.Uint64(k)
}
return 0
}
func (r raftLogStore) getKeyForIndex(index uint64) []byte {
k := r.getPrefix()
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, index)
k = append(k, b...)
return k
}
func (r raftLogStore) getKeySize() int {
return len(r.getPrefix()) + 8
}