-
Notifications
You must be signed in to change notification settings - Fork 0
/
nemo_storage.go
111 lines (88 loc) · 2.41 KB
/
nemo_storage.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
// +build freebsd openbsd netbsd dragonfly linux
package storage
import (
gonemo "github.com/deepfabric/go-nemo"
)
type nemoWriteBatch struct {
wb *gonemo.WriteBatch
}
func newNemoWriteBatch(wb *gonemo.WriteBatch) WriteBatch {
return &nemoWriteBatch{
wb: wb,
}
}
func (n *nemoWriteBatch) Delete(key []byte) error {
n.wb.WriteBatchDel(key)
return nil
}
func (n *nemoWriteBatch) Set(key []byte, value []byte) error {
n.wb.WriteBatchPut(key, value)
return nil
}
type nemoStorage struct {
opts *options
nemo *gonemo.NEMO
nemoDB *gonemo.DBNemo
}
func newNemoStorage(opts *options) Storage {
var nemoOpts *gonemo.Options
if opts.nemoOptions != "" {
nemoOpts, _ = gonemo.NewOptions(opts.nemoOptions)
} else {
nemoOpts = gonemo.NewDefaultOptions()
}
nemo := gonemo.OpenNemo(nemoOpts, opts.nemoDataPath)
nemoDB := nemo.GetMetaHandle()
return &nemoStorage{
nemo: nemo,
nemoDB: nemoDB,
}
}
func (ns *nemoStorage) Set(key []byte, value []byte, sync bool) error {
return ns.nemo.PutWithHandle(ns.nemoDB, key, value, sync)
}
func (ns *nemoStorage) Get(key []byte) ([]byte, error) {
return ns.nemo.GetWithHandle(ns.nemoDB, key)
}
func (ns *nemoStorage) Delete(key []byte, sync bool) error {
return ns.nemo.DeleteWithHandle(ns.nemoDB, key, sync)
}
func (ns *nemoStorage) RangeDelete(start, end []byte) error {
return ns.nemo.RangeDelWithHandle(ns.nemoDB, start, end)
}
func (ns *nemoStorage) Seek(key []byte) ([]byte, []byte, error) {
return ns.nemo.SeekWithHandle(ns.nemoDB, key)
}
func (ns *nemoStorage) Scan(start, end []byte, handler func(key, value []byte) (bool, error)) error {
return ns.ScanWithPooledKey(start, end, handler, false)
}
func (ns *nemoStorage) ScanWithPooledKey(start, end []byte, handler func(key, value []byte) (bool, error), pooledKey bool) error {
var key []byte
var err error
c := false
it := ns.nemo.KScanWithHandle(ns.nemoDB, start, end, true)
for ; it.Valid(); it.Next() {
if pooledKey {
key = it.PooledKey()
} else {
key = it.Key()
}
c, err = handler(key, it.Value())
if err != nil || !c {
break
}
}
it.Free()
return err
}
func (ns *nemoStorage) Free(pooled []byte) {
gonemo.MemPool.Free(pooled)
}
func (ns *nemoStorage) NewWriteBatch() WriteBatch {
wb := gonemo.NewWriteBatch()
return newNemoWriteBatch(wb)
}
func (ns *nemoStorage) Write(wb WriteBatch, sync bool) error {
nwb := wb.(*nemoWriteBatch)
return ns.nemo.BatchWrite(ns.nemoDB, nwb.wb, sync)
}