/
db_extension.go
71 lines (64 loc) · 1.77 KB
/
db_extension.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
package gorocksext
// #include <stdlib.h>
// #include "extensions.h"
import "C"
import (
"errors"
"unsafe"
"github.com/tecbot/gorocksdb"
)
// NewIterators returns an array of iterators
// corresponding to the given column families for a given db
// iterators is allocated by this method -> responsibility of
// caller to destroy the underlying c objects
// opts, db, cfs will not be destroyed -> resonsibility of caller
func NewIterators(
opts *gorocksdb.ReadOptions,
db *gorocksdb.DB,
cfHandles []*gorocksdb.ColumnFamilyHandle,
) ([]*gorocksdb.Iterator, error) {
size := len(cfHandles)
cCFHandles := make([]*C.rocksdb_column_family_handle_t, size, size)
for i, cfHandle := range cfHandles {
cCFHandles[i] = (*C.rocksdb_column_family_handle_t)(cfHandle.UnsafeGetCFHandler())
}
iters := make([]*C.rocksdb_iterator_t, size, size)
var cErr *C.char
C.get_iterators(
(*C.rocksdb_readoptions_t)(opts.UnsafeGetReadOptions()),
(*C.rocksdb_t)(db.UnsafeGetDB()),
&cCFHandles[0],
&iters[0],
C.int(size),
&cErr,
)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return nil, errors.New(C.GoString(cErr))
}
var iterators []*gorocksdb.Iterator
for _, iter := range iters {
iterators = append(
iterators,
gorocksdb.NewNativeIterator(unsafe.Pointer(iter)),
)
}
return iterators, nil
}
// CreateCheckpoint creates an openable snapshot of the db
// db is not destroyed -> responsibility of the caller
func CreateCheckpoint(db *gorocksdb.DB, checkpointDir string) error {
cCheckpointDir := C.CString(checkpointDir)
defer C.free(unsafe.Pointer(cCheckpointDir))
var cErr *C.char
C.create_checkpoint(
(*C.rocksdb_t)(db.UnsafeGetDB()),
cCheckpointDir,
&cErr,
)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}