forked from jmhodges/levigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
388 lines (343 loc) · 11.1 KB
/
db.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package levigo
/*
#cgo LDFLAGS: -lleveldb
#include <stdlib.h>
#include "leveldb/c.h"
// This function exists only to clean up lack-of-const warnings when
// leveldb_approximate_sizes is called from Go-land.
void levigo_leveldb_approximate_sizes(
leveldb_t* db,
int num_ranges,
char** range_start_key, const size_t* range_start_key_len,
char** range_limit_key, const size_t* range_limit_key_len,
uint64_t* sizes) {
leveldb_approximate_sizes(db,
num_ranges,
(const char* const*)range_start_key,
range_start_key_len,
(const char* const*)range_limit_key,
range_limit_key_len,
sizes);
}
*/
import "C"
import (
"errors"
"unsafe"
)
type DatabaseError string
func (e DatabaseError) Error() string {
return string(e)
}
var ErrDBClosed = errors.New("database is closed")
// DB is a reusable handle to a LevelDB database on disk, created by Open.
//
// To avoid memory and file descriptor leaks, call Close when the process no
// longer needs the handle. Calls to any DB method made after Close will
// panic.
//
// The DB instance may be shared between goroutines. The usual data race
// conditions will occur if the same key is written to from more than one, of
// course.
type DB struct {
Ldb *C.leveldb_t
// TLDR: Closed is not racey, it's a best attempt. If `-race` says it's racey,
// then it's your code that is racey, not levigo.
//
// This indicates if the DB is closed or not. LevelDB provides it's own closed
// detection that appears in the form of a sigtrap panic, which can be quite
// confusing. So rather than users hit that, we attempt to give them a better
// panic by checking this flag in functions that LevelDB would have done
// it's own panic. This is not protected by a mutex because it's a best case
// attempt to catch invalid usage. If access in racey and gets to LevelDB,
// so be it, the user will see the sigtrap panic.
// Because LevelDB has it's own mutex to detect the usage, we didn't want to
// put another one up here and drive performance down even more.
// So if you use `-race` and it says closed is racey, it's your code that is
// racey, not levigo.
closed bool
}
// Range is a range of keys in the database. GetApproximateSizes calls with it
// begin at the key Start and end right before the key Limit.
type Range struct {
Start []byte
Limit []byte
}
// Snapshot provides a consistent view of read operations in a DB.
//
// Snapshot is used in read operations by setting it on a
// ReadOptions. Snapshots are created by calling DB.NewSnapshot.
//
// To prevent memory leaks and resource strain in the database, the snapshot
// returned must be released with DB.ReleaseSnapshot method on the DB that
// created it.
type Snapshot struct {
snap *C.leveldb_snapshot_t
}
// Open opens a database.
//
// Creating a new database is done by calling SetCreateIfMissing(true) on the
// Options passed to Open.
//
// It is usually wise to set a Cache object on the Options with SetCache to
// keep recently used data from that database in memory.
func Open(dbname string, o *Options) (*DB, error) {
var errStr *C.char
ldbname := C.CString(dbname)
defer C.free(unsafe.Pointer(ldbname))
leveldb := C.leveldb_open(o.Opt, ldbname, &errStr)
if errStr != nil {
gs := C.GoString(errStr)
C.leveldb_free(unsafe.Pointer(errStr))
return nil, DatabaseError(gs)
}
return &DB{leveldb, false}, nil
}
// DestroyDatabase removes a database entirely, removing everything from the
// filesystem.
func DestroyDatabase(dbname string, o *Options) error {
var errStr *C.char
ldbname := C.CString(dbname)
defer C.free(unsafe.Pointer(ldbname))
C.leveldb_destroy_db(o.Opt, ldbname, &errStr)
if errStr != nil {
gs := C.GoString(errStr)
C.leveldb_free(unsafe.Pointer(errStr))
return DatabaseError(gs)
}
return nil
}
// RepairDatabase attempts to repair a database.
//
// If the database is unrepairable, an error is returned.
func RepairDatabase(dbname string, o *Options) error {
var errStr *C.char
ldbname := C.CString(dbname)
defer C.free(unsafe.Pointer(ldbname))
C.leveldb_repair_db(o.Opt, ldbname, &errStr)
if errStr != nil {
gs := C.GoString(errStr)
C.leveldb_free(unsafe.Pointer(errStr))
return DatabaseError(gs)
}
return nil
}
// Put writes data associated with a key to the database.
//
// If a nil []byte is passed in as value, it will be returned by Get
// as an zero-length slice. The WriteOptions passed in can be reused
// by multiple calls to this and if the WriteOptions is left unchanged.
//
// The key and value byte slices may be reused safely. Put takes a copy of
// them before returning.
func (db *DB) Put(wo *WriteOptions, key, value []byte) error {
if db.closed {
panic(ErrDBClosed)
}
var errStr *C.char
// leveldb_put, _get, and _delete call memcpy() (by way of Memtable::Add)
// when called, so we do not need to worry about these []byte being
// reclaimed by GC.
var k, v *C.char
if len(key) != 0 {
k = (*C.char)(unsafe.Pointer(&key[0]))
}
if len(value) != 0 {
v = (*C.char)(unsafe.Pointer(&value[0]))
}
lenk := len(key)
lenv := len(value)
C.leveldb_put(
db.Ldb, wo.Opt, k, C.size_t(lenk), v, C.size_t(lenv), &errStr)
if errStr != nil {
gs := C.GoString(errStr)
C.leveldb_free(unsafe.Pointer(errStr))
return DatabaseError(gs)
}
return nil
}
// Get returns the data associated with the key from the database.
//
// If the key does not exist in the database, a nil []byte is returned. If the
// key does exist, but the data is zero-length in the database, a zero-length
// []byte will be returned.
//
// The key byte slice may be reused safely. Get takes a copy of
// them before returning.
func (db *DB) Get(ro *ReadOptions, key []byte) ([]byte, error) {
if db.closed {
panic(ErrDBClosed)
}
var errStr *C.char
var vallen C.size_t
var k *C.char
if len(key) != 0 {
k = (*C.char)(unsafe.Pointer(&key[0]))
}
value := C.leveldb_get(
db.Ldb, ro.Opt, k, C.size_t(len(key)), &vallen, &errStr)
if errStr != nil {
gs := C.GoString(errStr)
C.leveldb_free(unsafe.Pointer(errStr))
return nil, DatabaseError(gs)
}
if value == nil {
return nil, nil
}
defer C.leveldb_free(unsafe.Pointer(value))
return C.GoBytes(unsafe.Pointer(value), C.int(vallen)), nil
}
// Delete removes the data associated with the key from the database.
//
// The key byte slice may be reused safely. Delete takes a copy of
// them before returning. The WriteOptions passed in can be reused by
// multiple calls to this and if the WriteOptions is left unchanged.
func (db *DB) Delete(wo *WriteOptions, key []byte) error {
if db.closed {
panic(ErrDBClosed)
}
var errStr *C.char
var k *C.char
if len(key) != 0 {
k = (*C.char)(unsafe.Pointer(&key[0]))
}
C.leveldb_delete(
db.Ldb, wo.Opt, k, C.size_t(len(key)), &errStr)
if errStr != nil {
gs := C.GoString(errStr)
C.leveldb_free(unsafe.Pointer(errStr))
return DatabaseError(gs)
}
return nil
}
// Write atomically writes a WriteBatch to disk. The WriteOptions
// passed in can be reused by multiple calls to this and other methods.
func (db *DB) Write(wo *WriteOptions, w *WriteBatch) error {
if db.closed {
panic(ErrDBClosed)
}
var errStr *C.char
C.leveldb_write(db.Ldb, wo.Opt, w.wbatch, &errStr)
if errStr != nil {
gs := C.GoString(errStr)
C.leveldb_free(unsafe.Pointer(errStr))
return DatabaseError(gs)
}
return nil
}
// NewIterator returns an Iterator over the the database that uses the
// ReadOptions given.
//
// Often, this is used for large, offline bulk reads while serving live
// traffic. In that case, it may be wise to disable caching so that the data
// processed by the returned Iterator does not displace the already cached
// data. This can be done by calling SetFillCache(false) on the ReadOptions
// before passing it here.
//
// Similarly, ReadOptions.SetSnapshot is also useful.
//
// The ReadOptions passed in can be reused by multiple calls to this
// and other methods if the ReadOptions is left unchanged.
func (db *DB) NewIterator(ro *ReadOptions) *Iterator {
if db.closed {
panic(ErrDBClosed)
}
it := C.leveldb_create_iterator(db.Ldb, ro.Opt)
return &Iterator{Iter: it}
}
// GetApproximateSizes returns the approximate number of bytes of file system
// space used by one or more key ranges.
//
// The keys counted will begin at Range.Start and end on the key before
// Range.Limit.
func (db *DB) GetApproximateSizes(ranges []Range) []uint64 {
starts := make([]*C.char, len(ranges))
limits := make([]*C.char, len(ranges))
startLens := make([]C.size_t, len(ranges))
limitLens := make([]C.size_t, len(ranges))
for i, r := range ranges {
starts[i] = C.CString(string(r.Start))
startLens[i] = C.size_t(len(r.Start))
limits[i] = C.CString(string(r.Limit))
limitLens[i] = C.size_t(len(r.Limit))
}
sizes := make([]uint64, len(ranges))
numranges := C.int(len(ranges))
startsPtr := &starts[0]
limitsPtr := &limits[0]
startLensPtr := &startLens[0]
limitLensPtr := &limitLens[0]
sizesPtr := (*C.uint64_t)(&sizes[0])
C.levigo_leveldb_approximate_sizes(
db.Ldb, numranges, startsPtr, startLensPtr,
limitsPtr, limitLensPtr, sizesPtr)
for i := range ranges {
C.free(unsafe.Pointer(starts[i]))
C.free(unsafe.Pointer(limits[i]))
}
return sizes
}
// PropertyValue returns the value of a database property.
//
// Examples of properties include "leveldb.stats", "leveldb.sstables",
// and "leveldb.num-files-at-level0".
func (db *DB) PropertyValue(propName string) string {
if db.closed {
panic(ErrDBClosed)
}
cname := C.CString(propName)
value := C.GoString(C.leveldb_property_value(db.Ldb, cname))
C.free(unsafe.Pointer(cname))
return value
}
// NewSnapshot creates a new snapshot of the database.
//
// The Snapshot, when used in a ReadOptions, provides a consistent
// view of state of the database at the the snapshot was created.
//
// To prevent memory leaks and resource strain in the database, the snapshot
// returned must be released with DB.ReleaseSnapshot method on the DB that
// created it.
//
// See the LevelDB documentation for details.
func (db *DB) NewSnapshot() *Snapshot {
if db.closed {
panic(ErrDBClosed)
}
return &Snapshot{C.leveldb_create_snapshot(db.Ldb)}
}
// ReleaseSnapshot removes the snapshot from the database's list of snapshots,
// and deallocates it.
func (db *DB) ReleaseSnapshot(snap *Snapshot) {
if db.closed {
panic(ErrDBClosed)
}
C.leveldb_release_snapshot(db.Ldb, snap.snap)
}
// CompactRange runs a manual compaction on the Range of keys given. This is
// not likely to be needed for typical usage.
func (db *DB) CompactRange(r Range) {
if db.closed {
panic(ErrDBClosed)
}
var start, limit *C.char
if len(r.Start) != 0 {
start = (*C.char)(unsafe.Pointer(&r.Start[0]))
}
if len(r.Limit) != 0 {
limit = (*C.char)(unsafe.Pointer(&r.Limit[0]))
}
C.leveldb_compact_range(
db.Ldb, start, C.size_t(len(r.Start)), limit, C.size_t(len(r.Limit)))
}
// Close closes the database, rendering it unusable for I/O, by deallocating
// the underlying handle.
//
// Any attempts to use the DB after Close is called will panic.
func (db *DB) Close() {
if db.closed {
return
}
db.closed = true
C.leveldb_close(db.Ldb)
}