-
Notifications
You must be signed in to change notification settings - Fork 0
/
leveldb.go
185 lines (154 loc) · 3.52 KB
/
leveldb.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
package raft
import (
"bytes"
"context"
"encoding/gob"
"sync"
"github.com/syndtr/goleveldb/leveldb"
)
type LevelDBLogStorage struct {
db *leveldb.DB
cache map[int32]Entry
keys []int32
limit int
mu sync.Mutex
}
func NewLevelDBLogStorage(dbPath string, limit int) (*LevelDBLogStorage, error) {
db, err := leveldb.OpenFile(dbPath, nil)
if err != nil {
return nil, err
}
return &LevelDBLogStorage{
db: db,
cache: make(map[int32]Entry),
keys: make([]int32, 0),
limit: limit,
}, nil
}
func (ls *LevelDBLogStorage) Append(ctx context.Context, entry Entry) int32 {
data, err := encodeEntry(entry)
if err != nil {
return -1
}
err = ls.db.Put(encodeIndex(entry.Index), data, nil)
if err != nil {
return -1
}
ls.updateCache(entry.Index, entry)
return entry.Index
}
func (ls *LevelDBLogStorage) Extend(ctx context.Context, entries []Entry, from int32) int32 {
batch := new(leveldb.Batch)
iter := ls.db.NewIterator(nil, nil)
for iter.Seek(encodeIndex(from)); iter.Valid(); iter.Next() {
batch.Delete(iter.Key())
}
iter.Release()
for _, index := range ls.keys {
if index >= from {
delete(ls.cache, index)
}
}
ls.keys = filterKeys(ls.keys, from)
for _, entry := range entries {
data, err := encodeEntry(entry)
if err != nil {
return -1
}
batch.Put(encodeIndex(entry.Index), data)
ls.updateCache(entry.Index, entry)
}
err := ls.db.Write(batch, nil)
if err != nil {
return -1
}
if len(entries) == 0 {
return from
}
return entries[len(entries)-1].Index
}
func (ls *LevelDBLogStorage) Find(ctx context.Context, index int32) Entry {
if entry, ok := ls.cache[index]; ok {
return entry
}
data, err := ls.db.Get(encodeIndex(index), nil)
if err != nil {
return Entry{}
}
entry, _ := decodeEntry(data)
return entry
}
func (ls *LevelDBLogStorage) Slice(ctx context.Context, from int32) []Entry {
ls.mu.Lock()
defer ls.mu.Unlock()
var entries []Entry
cacheStartIndex := int32(-1)
for _, index := range ls.keys {
if index >= from {
cacheStartIndex = index
break
}
}
if cacheStartIndex != from {
iter := ls.db.NewIterator(nil, nil)
defer iter.Release()
for iter.Seek(encodeIndex(from)); iter.Valid(); iter.Next() {
index := decodeIndex(iter.Key())
if index >= cacheStartIndex {
break
}
entry, _ := decodeEntry(iter.Value())
entries = append(entries, entry)
}
}
for _, index := range ls.keys {
if index >= from {
if entry, ok := ls.cache[index]; ok {
entries = append(entries, entry)
}
}
}
return entries
}
func encodeIndex(index int32) []byte {
buf := new(bytes.Buffer)
gob.NewEncoder(buf).Encode(index)
return buf.Bytes()
}
func decodeIndex(data []byte) int32 {
var index int32
buf := bytes.NewBuffer(data)
gob.NewDecoder(buf).Decode(&index)
return index
}
func encodeEntry(entry Entry) ([]byte, error) {
buf := new(bytes.Buffer)
err := gob.NewEncoder(buf).Encode(entry)
return buf.Bytes(), err
}
func decodeEntry(data []byte) (Entry, error) {
var entry Entry
buf := bytes.NewBuffer(data)
err := gob.NewDecoder(buf).Decode(&entry)
return entry, err
}
func (ls *LevelDBLogStorage) updateCache(index int32, entry Entry) {
ls.cache[index] = entry
ls.keys = append(ls.keys, index)
if len(ls.cache) > ls.limit {
delete(ls.cache, ls.keys[0])
ls.keys = ls.keys[1:]
}
}
func filterKeys(keys []int32, from int32) []int32 {
var filtered []int32
for _, key := range keys {
if key < from {
filtered = append(filtered, key)
}
}
return filtered
}
func (ls *LevelDBLogStorage) Close() error {
return ls.db.Close()
}