-
Notifications
You must be signed in to change notification settings - Fork 0
/
sstorage.go
141 lines (111 loc) · 2.5 KB
/
sstorage.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
package spaxos
import (
"fmt"
"path"
"strconv"
"github.com/golang/leveldb"
ldb "github.com/golang/leveldb/db"
// pb "spaxos/spaxospb"
pb "github.com/dengoswei/spaxos/spaxospb"
)
var keyMinIndex = []byte("min_index")
var keyMaxIndex = []byte("max_index")
type SStorage struct {
db *leveldb.DB
}
func makeDataKey(index uint64) []byte {
skey := fmt.Sprintf("index_%d", index)
assert(0 < len(skey))
return []byte(skey)
}
func NewStorage(c *Config) (*SStorage, error) {
assert(nil != c)
// TODO: using leveldb/memfs ?
db, err := leveldb.Open(
path.Join(c.Path, strconv.FormatUint(c.Selfid, 10)), nil)
if nil != err {
return nil, err
}
store := &SStorage{db: db}
return store, nil
}
func (store *SStorage) Store(data []pb.HardState) error {
assert(nil != store.db)
// group into one batch
var batch leveldb.Batch
for _, hs := range data {
key := makeDataKey(hs.Index)
assert(0 < len(key))
val, err := hs.Marshal()
if nil != err {
return err
}
batch.Set(key, val)
}
// store using default option
return store.db.Apply(batch, nil)
}
func (store *SStorage) Get(index uint64) (pb.HardState, error) {
assert(nil != store.db)
key := makeDataKey(index)
assert(0 < len(key))
val, err := store.db.Get(key, nil)
if nil != err {
return pb.HardState{}, err
}
hs := pb.HardState{}
err = hs.Unmarshal(val)
if nil != err {
return pb.HardState{}, err
}
return hs, nil
}
func (store *SStorage) setIndex(key []byte, index uint64) error {
assert(nil != store.db)
assert(nil != key)
hs := pb.HardState{Index: index}
val, err := hs.Marshal()
if nil != err {
return err
}
return store.db.Set(key, val, nil)
}
func (store *SStorage) getIndex(key []byte) (uint64, error) {
assert(nil != store.db)
assert(nil != key)
val, err := store.db.Get(key, nil)
if nil != err {
if ldb.ErrNotFound == err {
return 0, nil
}
return 0, err
}
hs := pb.HardState{}
err = hs.Unmarshal(val)
if nil != err {
return 0, err
}
return hs.Index, nil
}
func (store *SStorage) SetIndex(minIndex, maxIndex uint64) error {
err := store.setIndex(keyMinIndex, minIndex)
if nil != err {
return err
}
return store.setIndex(keyMaxIndex, maxIndex)
}
func (store *SStorage) GetIndex() (uint64, uint64, error) {
minIndex, err := store.getIndex(keyMinIndex)
if nil != err {
return 0, 0, err
}
maxIndex, err := store.getIndex(keyMaxIndex)
if nil != err {
return 0, 0, err
}
return minIndex, maxIndex, nil
}
func (store *SStorage) Close() error {
assert(nil != store.db)
return store.db.Close()
}