/
sublevel.go
237 lines (196 loc) · 5.46 KB
/
sublevel.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
package sublevel
import (
"log"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/iterator"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/util"
)
/* basic db management */
/* opens an abstract DB for the given file.
the abstract DB is said to be "abstract" because it cannot
do any operation, it is there only to generate sublevels.
It is not possible to access the underlying leveldb with this
package.
*/
func Open(dbfile string, o *opt.Options) (*AbstractLevel, error) {
db, err := leveldb.OpenFile(dbfile, o)
if err != nil {
return nil, err
}
return &AbstractLevel{
leveldb: db,
}, nil
}
func MustOpen(dbfile string, o *opt.Options) *AbstractLevel {
db, err := Open(dbfile, o)
if err != nil {
log.Fatal("couldn't open leveldb at file " + dbfile)
}
return db
}
type AbstractLevel struct {
leveldb *leveldb.DB
}
func (a AbstractLevel) Close() error {
return a.leveldb.Close()
}
func (a AbstractLevel) Sub(store string) *Sublevel {
return &Sublevel{
namespace: makeNamespace(store),
db: a.leveldb,
}
}
func makeNamespace(store string) []byte {
return []byte("!" + store + "!")
}
type Sublevel struct {
namespace []byte
db *leveldb.DB
}
func (s Sublevel) Close() error {
return s.db.Close()
}
/* methods */
func (s Sublevel) Delete(key []byte, wo *opt.WriteOptions) error {
key = append(append([]byte(nil), s.namespace...), key...)
return s.db.Delete(key, wo)
}
func (s Sublevel) Get(key []byte, ro *opt.ReadOptions) (value []byte, err error) {
key = append(append([]byte(nil), s.namespace...), key...)
return s.db.Get(key, ro)
}
func (s Sublevel) Put(key []byte, value []byte, wo *opt.WriteOptions) error {
key = append(append([]byte(nil), s.namespace...), key...)
return s.db.Put(key, value, wo)
}
func (s Sublevel) Has(key []byte, ro *opt.ReadOptions) (ret bool, err error) {
key = append(append([]byte(nil), s.namespace...), key...)
return s.db.Has(key, ro)
}
/* iterator */
func (s Sublevel) NewIterator(slice *util.Range, ro *opt.ReadOptions) SubIterator {
slice = &util.Range{
Start: append(append([]byte(nil), s.namespace...), slice.Start...),
Limit: append(append([]byte(nil), s.namespace...), slice.Limit...),
}
return SubIterator{
namespace: s.namespace,
iterator: s.db.NewIterator(slice, ro),
}
}
type SubIterator struct {
namespace []byte
iterator iterator.Iterator
}
func (si SubIterator) Key() []byte {
key := si.iterator.Key()
return key[len(si.namespace):]
}
func (si SubIterator) Value() []byte {
return si.iterator.Value()
}
func (si SubIterator) Next() bool {
return si.iterator.Next()
}
func (si SubIterator) Prev() bool {
return si.iterator.Prev()
}
func (si SubIterator) Last() bool {
return si.iterator.Last()
}
func (si SubIterator) First() bool {
return si.iterator.First()
}
func (si SubIterator) Seek(key []byte) bool {
key = append(append([]byte(nil), si.namespace...), key...)
return si.iterator.Seek(key)
}
func (si SubIterator) Release() {
si.iterator.Release()
}
func (si SubIterator) Error() error {
return si.iterator.Error()
}
/* transactions */
type AbstractBatchOperation struct {
kind string // PUT or DELETE
key []byte
value []byte
}
// starts a new batch write for the specified sublevel.
func (s Sublevel) NewBatch() *SubBatch {
return &SubBatch{namespace: s.namespace}
}
type SubBatch struct {
namespace []byte
ops []AbstractBatchOperation
}
func (b *SubBatch) Delete(key []byte) {
key = append(append([]byte(nil), b.namespace...), key...)
b.ops = append(b.ops, AbstractBatchOperation{"DELETE", key, nil})
}
func (b *SubBatch) Put(key []byte, value []byte) {
key = append(append([]byte(nil), b.namespace...), key...)
b.ops = append(b.ops, AbstractBatchOperation{"PUT", key, value})
}
func (b *SubBatch) Dump() []byte {
return makeBatchWithOps(b.ops).Dump()
}
func (b *SubBatch) Len() int {
return len(b.ops)
}
func (b *SubBatch) Reset() {
b.ops = make([]AbstractBatchOperation, 0)
}
func (s Sublevel) Write(b *SubBatch, wo *opt.WriteOptions) error {
return s.db.Write(makeBatchWithOps(b.ops), wo)
}
/* transactions on different stores */
func (a AbstractLevel) NewBatch() *SuperBatch {
return &SuperBatch{}
}
type SuperBatch struct {
ops []AbstractBatchOperation
}
func (sb *SuperBatch) Put(store string, key []byte, value []byte) {
key = append(append([]byte(nil), makeNamespace(store)...), key...)
sb.ops = append(sb.ops, AbstractBatchOperation{"PUT", key, value})
}
func (sb *SuperBatch) Delete(store string, key []byte) {
key = append(append([]byte(nil), makeNamespace(store)...), key...)
sb.ops = append(sb.ops, AbstractBatchOperation{"DELETE", key, nil})
}
func (sb *SuperBatch) MergeSubBatch(b *SubBatch) {
sb.ops = append(sb.ops, b.ops...)
}
func (sb *SuperBatch) Dump() []byte {
return makeBatchWithOps(sb.ops).Dump()
}
func (sb *SuperBatch) Len() int {
return len(sb.ops)
}
func (sb *SuperBatch) Reset() {
sb.ops = make([]AbstractBatchOperation, 0)
}
func (a AbstractLevel) Write(sb *SuperBatch, wo *opt.WriteOptions) error {
return a.leveldb.Write(makeBatchWithOps(sb.ops), wo)
}
func (a AbstractLevel) MultiBatch(subbatches ...*SubBatch) *SuperBatch {
sb := a.NewBatch()
for _, b := range subbatches {
sb.MergeSubBatch(b)
}
return sb
}
func makeBatchWithOps(ops []AbstractBatchOperation) *leveldb.Batch {
batch := new(leveldb.Batch)
for _, op := range ops {
if op.kind == "PUT" {
batch.Put(op.key, op.value)
} else if op.kind == "DELETE" {
batch.Delete(op.key)
}
}
return batch
}