/
rocks.go
58 lines (47 loc) · 1.06 KB
/
rocks.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
package rocksiterbench
import "github.com/tecbot/gorocksdb"
type RocksDB struct {
db *gorocksdb.DB
}
func NewRocksDB(path string) (*RocksDB, error) {
opts := gorocksdb.NewDefaultOptions()
//opts.SetBlockCache(gorocksdb.NewLRUCache(3 << 30))
opts.SetCreateIfMissing(true)
db, err := gorocksdb.OpenDb(opts, path)
return &RocksDB{db: db}, err
}
var wo = gorocksdb.NewDefaultWriteOptions()
func (r *RocksDB) Write(k, v []byte) error {
return r.db.Put(wo, k, v)
}
func (r *RocksDB) Close() {
r.db.Close()
}
type rocksIter struct {
iter *gorocksdb.Iterator
}
func (r *rocksIter) Next() ([]byte, bool) {
if !r.iter.Valid() {
return nil, false
}
key := r.iter.Key()
defer key.Free()
value := r.iter.Value()
defer value.Free()
res := value.Data()
r.iter.Next()
return res, true
}
func (r *rocksIter) Err() error {
return r.iter.Err()
}
func (r *rocksIter) Close() {
r.iter.Close()
}
func (r *RocksDB) Iterate(key []byte) Iter {
ro := gorocksdb.NewDefaultReadOptions()
ro.SetFillCache(false)
it := r.db.NewIterator(ro)
it.Seek(key)
return &rocksIter{iter: it}
}