-
Notifications
You must be signed in to change notification settings - Fork 15
/
writer.go
75 lines (63 loc) · 1.51 KB
/
writer.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
package bbadger
import (
"fmt"
"github.com/blevesearch/bleve/index/store"
)
// Writer bleve.search/store/Writer implementation
// I (alash3al) adopted it from bleve/store/boltdb
type Writer struct {
s *Store
}
// NewBatch implements NewBatch
func (w *Writer) NewBatch() store.KVBatch {
return store.NewEmulatedBatch(w.s.mo)
}
// NewBatchEx implements bleve NewBatchEx
func (w *Writer) NewBatchEx(options store.KVBatchOptions) ([]byte, store.KVBatch, error) {
return make([]byte, options.TotalBytes), w.NewBatch(), nil
}
// ExecuteBatch implements bleve ExecuteBatch
func (w *Writer) ExecuteBatch(batch store.KVBatch) (err error) {
emulatedBatch, ok := batch.(*store.EmulatedBatch)
if !ok {
return fmt.Errorf("wrong type of batch")
}
txn := w.s.db.NewTransaction(true)
defer (func() {
txn.Commit()
})()
for k, mergeOps := range emulatedBatch.Merger.Merges {
kb := []byte(k)
item, err := txn.Get(kb)
existingVal := []byte{}
if err == nil {
existingVal, _ = item.ValueCopy(nil)
}
mergedVal, fullMergeOk := w.s.mo.FullMerge(kb, existingVal, mergeOps)
if !fullMergeOk {
return fmt.Errorf("merge operator returned failure")
}
err = txn.Set(kb, mergedVal)
if err != nil {
return err
}
}
for _, op := range emulatedBatch.Ops {
if op.V != nil {
err = txn.Set(op.K, op.V)
if err != nil {
return err
}
} else {
err = txn.Delete(op.K)
if err != nil {
return err
}
}
}
return nil
}
// Close closes the current writer
func (w *Writer) Close() error {
return nil
}