-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
252 lines (214 loc) · 5.34 KB
/
db.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
// Copyright (c) 2020, HuguesGuilleus. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package db
import (
"bytes"
"encoding/gob"
"github.com/prologic/bitcask"
"log"
"reflect"
"sort"
)
// DB is a data base
type DB struct {
intern *bitcask.Bitcask
maxIndex Key // The max id
name string
}
// Open a new DabatBase
func New(name string, opt ...bitcask.Option) (db *DB) {
intern, err := bitcask.Open(name, opt...)
if err != nil {
log.Println("[DB ERROR]", err)
}
db = &DB{
intern: intern,
name: name,
}
db.intern.Fold(func(key []byte) error {
if k := keyBytes(key); k > db.maxIndex {
db.maxIndex = k
}
return nil
})
return
}
// Print the error if the error is not nil and return true.
// If no error return false.
func (db *DB) print(err error, key []byte) bool {
if err != nil {
if len(key) > 20 {
key = key[:20]
}
log.Printf("[DB ERROR] %q <key:%x>: %v\n", db.name, key, err)
}
return false
}
/* BASIC MANIPULATION */
// Create a new number index in the DB.
func (db *DB) New() Key {
db.maxIndex++
return db.maxIndex - 1
}
// Return true if the key is unknown in the DB.
func (db *DB) Unknown(key Key) bool {
return !db.intern.Has(key.bytes())
}
func (db *DB) UnknownS(key string) bool {
return !db.intern.Has([]byte(key))
}
// Delete on element
func (db *DB) Delete(key Key) {
k := key.bytes()
db.print(db.intern.Delete(k), k)
}
// Delete on element
func (db *DB) DeleteS(key string) {
k := []byte(key)
db.print(db.intern.Delete(k), k)
}
// Remove all keys in the DB.
func (db *DB) DeleteAll() {
db.maxIndex = 0
db.print(db.intern.DeleteAll(), nil)
}
/* GET */
// Get the element with a number key and save it in v.
func (db *DB) Get(key Key, v interface{}) (noExist bool) {
return db.parse(key.bytes(), v)
}
// Get the element with a string key and save it in v.
func (db *DB) GetS(key string, v interface{}) (noExist bool) {
return db.parse([]byte(key), v)
}
// Get the value from the DB, decode and save it the v.
func (db *DB) parse(key []byte, v interface{}) (noExist bool) {
// Set v to Zero
p := reflect.ValueOf(v).Elem()
p.Set(reflect.Zero(p.Type()))
// get the value
data, err := db.intern.Get(key)
if err == bitcask.ErrKeyNotFound {
return true
} else if db.print(err, key) {
return true
}
// Decode the value
err = gob.NewDecoder(bytes.NewReader(data)).Decode(v)
if db.print(err, key) {
db.intern.Delete(key)
return true
}
return false
}
// Get the value in without decoding with a number key.
func (db *DB) GetRaw(key Key) []byte {
k := key.bytes()
data, err := db.intern.Get(k)
if err == bitcask.ErrKeyNotFound {
return nil
}
db.print(err, k)
return data
}
// Get the value in without decoding with a string key.
func (db *DB) GetSRaw(key string) []byte {
k := []byte(key)
data, err := db.intern.Get(k)
if err == bitcask.ErrKeyNotFound {
return nil
}
db.print(err, k)
return data
}
/* FOR */
// Make an iteration on all the element in the DB that begins with prefix.
//
// Page is the index of the page of length size. If zero, web take all keys.
//
// We can select some elements with them key, so we use filter.
// filter can be nil.
//
// It must be a function that take a string and a other type for the value,
// else it panic.
// MyDB.For(func(k db.Key, v MyType){...})
//
// total is the number of elements in the DB with prefix and that pass filter.
func (db *DB) ForS(prefix string, page, size int, filter func(string) bool, it interface{}) (total int) {
f := reflect.ValueOf(it)
t := f.Type()
if t.Kind() != reflect.Func ||
t.NumIn() != 2 ||
t.In(0) != reflect.TypeOf("") {
log.Panic("DB.ForS() need a iteration function")
}
v := reflect.New(t.In(1)).Elem()
if filter == nil {
filter = func(string) bool { return true }
}
// TODO: optimize me
all := make([]string, 0)
db.intern.Scan([]byte(prefix), func(key []byte) error {
k := string(key)
if filter(k) {
total++
all = append(all, k)
}
return nil
})
// Get a page
if size != 0 {
sort.Strings(all)
begin := page * size
if begin < 0 || begin > len(all) {
begin = 0
}
end := size + begin
if end > len(all) || end < begin {
end = len(all)
}
all = all[begin:end]
}
for _, key := range all {
k := []byte(key)
data, err := db.intern.Get(k)
if db.print(err, k) {
continue
}
v.Set(reflect.Zero(v.Type()))
err = gob.NewDecoder(bytes.NewReader(data)).DecodeValue(v)
if db.print(err, k) {
continue
}
f.Call([]reflect.Value{reflect.ValueOf(string(key)), v})
}
return
}
/* SET */
// Save with a number key, the value v in the DB with serialization.
func (db *DB) Set(key Key, v interface{}) {
db.set(key.bytes(), v)
}
// Save with a string key, the value v in the DB with serialization.
func (db *DB) SetS(key string, v interface{}) {
db.set([]byte(key), v)
}
// Serialization the value v and save it in the DB.
func (db *DB) set(k []byte, v interface{}) {
w := bytes.Buffer{}
if db.print(gob.NewEncoder(&w).Encode(v), k) {
return
}
db.print(db.intern.Put(k, w.Bytes()), k)
}
// Set with a number key, the value v without serialization.
func (db *DB) SetRaw(key Key, raw []byte) {
k := key.bytes()
db.print(db.intern.Put(k, raw), k)
}
// Set with a string key, the value v without serialization.
func (db *DB) SetSRaw(key string, raw []byte) {
k := []byte(key)
db.print(db.intern.Put(k, raw), k)
}