This repository has been archived by the owner on Apr 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
rocksdb.go
238 lines (203 loc) · 5.28 KB
/
rocksdb.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
//go:build rocksdb
// +build rocksdb
package db
import (
"fmt"
"path/filepath"
"runtime"
"github.com/line/gorocksdb"
)
func init() {
dbCreator := func(name string, dir string) (DB, error) {
return NewRocksDB(name, dir)
}
registerDBCreator(RocksDBBackend, dbCreator, false)
}
// RocksDB is a RocksDB backend.
type RocksDB struct {
name string
db *gorocksdb.DB
ro *gorocksdb.ReadOptions
wo *gorocksdb.WriteOptions
woSync *gorocksdb.WriteOptions
woLowPri *gorocksdb.WriteOptions
}
var _ DB = (*RocksDB)(nil)
func NewRocksDB(name string, dir string) (*RocksDB, error) {
// default rocksdb option, good enough for most cases, including heavy workloads.
// 1GB table cache, 512MB write buffer(may use 50% more on heavy workloads).
// compression: snappy as default, need to -lsnappy to enable.
bbto := gorocksdb.NewDefaultBlockBasedTableOptions()
bbto.SetBlockCache(gorocksdb.NewLRUCache(1 << 30))
bbto.SetFilterPolicy(gorocksdb.NewBloomFilter(10))
defer bbto.Destroy()
opts := gorocksdb.NewDefaultOptions()
opts.SetBlockBasedTableFactory(bbto)
opts.SetCreateIfMissing(true)
opts.IncreaseParallelism(runtime.NumCPU())
// 1.5GB maximum memory use for writebuffer.
opts.OptimizeLevelStyleCompaction(512 * 1024 * 1024)
defer opts.Destroy()
return NewRocksDBWithOptions(name, dir, opts)
}
func NewRocksDBWithOptions(name string, dir string, opts *gorocksdb.Options) (*RocksDB, error) {
err := makePath(dir)
if err != nil {
return nil, err
}
dbPath := filepath.Join(dir, name+".db")
db, err := gorocksdb.OpenDb(opts, dbPath)
if err != nil {
return nil, err
}
ro := gorocksdb.NewDefaultReadOptions()
wo := gorocksdb.NewDefaultWriteOptions()
woSync := gorocksdb.NewDefaultWriteOptions()
woSync.SetSync(true)
woLowPri := gorocksdb.NewDefaultWriteOptions()
// TODO: gorocksdb doesn't have rocksdb_writeoptions_set_low_pri() yet.
// woLowPri.SetLowPri(true)
database := &RocksDB{
name: name,
db: db,
ro: ro,
wo: wo,
woSync: woSync,
woLowPri: woLowPri,
}
return database, nil
}
func (db *RocksDB) Name() string {
return db.name
}
// Get implements DB.
func (db *RocksDB) Get(key []byte) ([]byte, error) {
if len(key) == 0 {
return nil, errKeyEmpty
}
return db.db.GetBytes(db.ro, key)
}
// Has implements DB.
func (db *RocksDB) Has(key []byte) (bool, error) {
if len(key) == 0 {
return false, errKeyEmpty
}
s, err := db.db.Get(db.ro, key)
if s != nil {
defer s.Free()
}
if err != nil {
return false, err
}
return s.Exists(), nil
}
// Set implements DB.
func (db *RocksDB) Set(key []byte, value []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
if value == nil {
return errValueNil
}
return db.db.Put(db.wo, key, value)
}
// SetSync implements DB.
func (db *RocksDB) SetSync(key []byte, value []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
if value == nil {
return errValueNil
}
return db.db.Put(db.woSync, key, value)
}
// Delete implements DB.
func (db *RocksDB) Delete(key []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
return db.db.Delete(db.wo, key)
}
// DeleteSync implements DB.
func (db *RocksDB) DeleteSync(key []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
return db.db.Delete(db.woSync, key)
}
// FIXME This should not be exposed
func (db *RocksDB) DB() *gorocksdb.DB {
return db.db
}
// FIXME Unsafe db.db.Close() with multiple calling
// Close implements DB.
func (db *RocksDB) Close() error {
db.ro.Destroy()
db.wo.Destroy()
db.woSync.Destroy()
db.db.Close()
return nil
}
// Print implements DB.
func (db *RocksDB) Print() error {
itr, err := db.Iterator(nil, nil)
if err != nil {
return err
}
defer itr.Close()
for ; itr.Valid(); itr.Next() {
key := itr.Key()
value := itr.Value()
fmt.Printf("[%X]:\t[%X]\n", key, value)
}
return nil
}
// Stats implements DB.
func (db *RocksDB) Stats() map[string]string {
keys := []string{"rocksdb.stats"}
stats := make(map[string]string, len(keys))
for _, key := range keys {
stats[key] = db.db.GetProperty(key)
}
return stats
}
// NewBatch implements DB.
func (db *RocksDB) NewBatch() Batch {
return newRocksDBBatch(db)
}
// Iterator implements DB.
func (db *RocksDB) Iterator(start, end []byte) (Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errKeyEmpty
}
ro := newRockDBRangeOptions(start, end)
itr := db.db.NewIterator(ro)
return newRocksDBIterator(itr, ro, false), nil
}
func (db *RocksDB) PrefixIterator(prefix []byte) (Iterator, error) {
start, end, err := PrefixToRange(prefix)
if err != nil {
return nil, err
}
ro := newRockDBRangeOptions(start, end)
itr := db.db.NewIterator(ro)
return newRocksDBIterator(itr, ro, false), nil
}
// ReverseIterator implements DB.
func (db *RocksDB) ReverseIterator(start, end []byte) (Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errKeyEmpty
}
ro := newRockDBRangeOptions(start, end)
itr := db.db.NewIterator(ro)
return newRocksDBIterator(itr, ro, true), nil
}
func (db *RocksDB) ReversePrefixIterator(prefix []byte) (Iterator, error) {
start, end, err := PrefixToRange(prefix)
if err != nil {
return nil, err
}
ro := newRockDBRangeOptions(start, end)
itr := db.db.NewIterator(ro)
return newRocksDBIterator(itr, ro, true), nil
}