This repository has been archived by the owner on May 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockage.go
356 lines (323 loc) · 8.39 KB
/
dockage.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Package dockage is an embedded document (json) database.
package dockage
import (
"bytes"
"encoding/binary"
"encoding/hex"
"encoding/json"
"strings"
"github.com/dgraph-io/badger"
)
//-----------------------------------------------------------------------------
// DB represents a database instance.
type DB struct {
db *badger.DB
views views
sqView View
sq *badger.Sequence
}
// Open opens the database with provided options.
func Open(opt Options) (resdb *DB, reserr error) {
bopt := badger.DefaultOptions
bopt.Dir = opt.Dir
bopt.ValueDir = opt.ValueDir
bdb, err := badger.Open(bopt)
if err != nil {
return nil, err
}
sq, err := bdb.GetSequence([]byte(pat4Sys(dbseq)), 512)
if err != nil {
reserr = err
return
}
resdb = &DB{db: bdb, sq: sq}
resdb.sqView = newView(viewdbseq,
func(em Emitter, id string, doc interface{}) (inf interface{}, err error) {
sq, err := resdb.sq.Next()
if err != nil {
return nil, err
}
// TODO:
if sq > 1000 && sq%1000 == 0 {
go resdb.db.RunValueLogGC(0.5)
}
ix := make([]byte, 8)
binary.BigEndian.PutUint64(ix, sq)
ix = []byte(hex.EncodeToString(ix))
em.Emit(ix, nil)
return ix, nil
})
return
}
// Close closes the database.
func (db *DB) Close() error {
db.sq.Release()
return db.db.Close()
}
// AddView adds a view. All views must be added right after Open(...). It
// is not safe to call this method concurrently.
func (db *DB) AddView(v View) { db.views = append(db.views, v) }
// DeleteView deletes the data of a view.
func (db *DB) DeleteView(v string) (reserr error) {
name := string(fnvhash([]byte(v)))
prefix := []byte(pat4View(name))
reserr = db.db.Update(func(txn *badger.Txn) error {
opt := badger.DefaultIteratorOptions
opt.PrefetchValues = false
itr := txn.NewIterator(opt)
defer itr.Close()
var todelete [][]byte
for itr.Seek(prefix); itr.ValidForPrefix(prefix); itr.Next() {
item := itr.Item()
k := item.KeyCopy(nil)
v, err := item.ValueCopy(nil)
if err != nil {
return err
}
if len(k) != 0 {
todelete = append(todelete, k)
}
if len(v) != 0 {
todelete = append(todelete, v)
}
}
for _, vd := range todelete {
if err := txn.Delete(vd); err != nil {
return err
}
}
return nil
})
return
}
// Put a list of documents inside database, in a single transaction.
// Document must have a json field named "id" and a json field named "rev".
// All documents passed by docs parameter will be inserted into the database
// in one transaction. Also all views will be computer in the same transaction.
func (db *DB) Put(docs ...interface{}) (reserr error) {
if len(docs) == 0 {
return
}
reserr = db.db.Update(func(txn *badger.Txn) error {
var builds []idd
for _, vdoc := range docs {
id, frev, err := prepdoc(vdoc)
if err != nil {
return err
}
qres, _, qerr := db.queryView(Q{View: viewdbseq, Start: id, Prefix: id}, txn, true)
if qerr != nil {
return qerr
}
if frev == nil {
if len(qres) > 0 {
return ErrNoMatchRev
}
}
if len(qres) > 0 && bytes.Compare(qres[0].Key, []byte(frev.Value().(string))) != 0 {
return ErrNoMatchRev
}
em := newViewEmitter(newTransaction(txn), db.sqView)
resinf, reserr := em.build(string(id), vdoc)
if reserr != nil {
return reserr
}
frev.Set(string(resinf.([]byte)))
js, err := json.Marshal(vdoc)
if err != nil {
return err
}
if err := txn.Set(append([]byte(keysp), id...), js); err != nil {
return err
}
builds = append(builds, idd{ID: string(id), Doc: vdoc})
}
for _, v := range builds {
tx := newTransaction(txn)
if _, err := db.views.buildAll(tx, v.ID, v.Doc); err != nil {
return err
}
}
return nil
})
return
}
// Get a list of documents based on their ids. Param docs is pointer to
// slice of struct. All documents will be read from database in one read transaction.
func (db *DB) Get(docs interface{}, firstID string, restID ...string) (reserr error) {
ids := append([]string{firstID}, restID...)
reserr = db.db.View(func(txn *badger.Txn) error {
var reslist []string
for _, vid := range ids {
vid := pat4Key(vid)
item, err := txn.Get([]byte(vid))
if err != nil {
return err
}
v, err := item.ValueCopy(nil)
if err != nil {
return err
}
reslist = append(reslist, string(v))
}
js := "[" + strings.Join(reslist, ",") + "]"
return json.Unmarshal([]byte(js), docs)
})
return
}
// Delete a list of documents based on their ids.
// All documents will be deleted from database in one write transaction.
func (db *DB) Delete(ids ...string) (reserr error) {
if len(ids) == 0 {
return
}
reserr = db.db.Update(func(txn *badger.Txn) error {
var viewList views = append([]View{db.sqView}, db.views...)
for _, vid := range ids {
if err := txn.Delete([]byte(keysp + vid)); err != nil {
return err
}
}
for _, vid := range ids {
tx := newTransaction(txn)
if _, err := viewList.buildAll(tx, vid, nil); err != nil {
return err
}
}
return nil
})
return
}
// Query queries a view using provided parameters. If no View is provided, it searches
// all ids using parameters. Number of results is always limited - default 100 documents.
// If total count for a query is needed by setting params.Count to true, no documents
// will be returned - because it might be a costly action. All documents will be read
// from database in one read transaction.
func (db *DB) Query(params Q) (reslist []Res, rescount int, reserr error) {
reslist, rescount, reserr = db.queryView(params, nil)
return
}
func (db *DB) queryView(params Q, parentTxn *badger.Txn, forIndexedKeys ...bool) (reslist []Res, rescount int, reserr error) {
params.init()
start, end, prefix := stopWords(params, forIndexedKeys...)
skip, limit, applySkip, applyLimit := getlimits(params)
body := func(itr interface{ Item() *badger.Item }) error {
if params.Count {
rescount++
skip--
if applySkip && skip >= 0 {
return nil
}
if applyLimit && limit <= 0 {
return nil
}
limit--
if len(end) > 0 {
item := itr.Item()
k := item.Key()
if bytes.Compare(k, end) > 0 {
return nil
}
}
return nil
}
item := itr.Item()
k := item.KeyCopy(nil)
skip--
if applySkip && skip >= 0 {
return nil
}
v, err := item.ValueCopy(nil)
if err != nil {
return err
}
if applyLimit && limit <= 0 {
return nil
}
limit--
if len(end) > 0 {
if bytes.Compare(k, end) > 0 {
return nil
}
}
var index []byte
polishedKey := k
sppfx := []byte(keysp)
if bytes.HasPrefix(polishedKey, sppfx) {
polishedKey = bytes.TrimPrefix(polishedKey, sppfx)
}
sppfx = []byte(viewsp)
if bytes.HasPrefix(polishedKey, sppfx) {
parts := bytes.Split(polishedKey, sppfx)
index = parts[2]
polishedKey = parts[3]
}
var rs Res
rs.Key = polishedKey
rs.Val = v
rs.Index = index
reslist = append(reslist, rs)
return nil
}
qfn := func(txn *badger.Txn) error {
var opt badger.IteratorOptions
opt.PrefetchValues = true
opt.PrefetchSize = limit
return itrFunc(txn, opt, start, prefix, body)
}
if parentTxn == nil {
reserr = db.db.View(qfn)
} else {
reserr = qfn(parentTxn)
}
if rescount == 0 {
rescount = len(reslist)
}
return
}
func (db *DB) unboundAll() (reslist []KV, reserr error) {
reserr = db.db.View(func(txn *badger.Txn) error {
opt := badger.DefaultIteratorOptions
opt.PrefetchValues = false
itr := txn.NewIterator(opt)
defer itr.Close()
for itr.Rewind(); itr.Valid(); itr.Next() {
itm := itr.Item()
var kv KV
kv.Key = itm.KeyCopy(nil)
var err error
kv.Val, err = itm.ValueCopy(nil)
if err != nil {
return err
}
reslist = append(reslist, kv)
}
return nil
})
return
}
//-----------------------------------------------------------------------------
// Q query parameters
type Q struct {
View string
Start, End, Prefix []byte
Skip, Limit int
Count bool
}
func (q *Q) init() {
if q.Limit <= 0 {
q.Limit = 100
}
}
//-----------------------------------------------------------------------------
// Options are params for creating DB object.
type Options struct {
// 1. Mandatory flags
// -------------------
// Directory to store the data in. Should exist and be writable.
Dir string
// Directory to store the value log in. Can be the same as Dir. Should
// exist and be writable.
ValueDir string
}
//-----------------------------------------------------------------------------