forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
161 lines (147 loc) · 4.06 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
package rdb
// #include <stdint.h>
// #include <stdlib.h>
// #include "rdbc.h"
import "C"
import (
"errors"
"unsafe"
)
// 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
}
// DB is a reusable handle to a RocksDB database on disk, created by Open.
type DB struct {
c *C.rdb_t
name string
opts *Options
}
// OpenDb opens a database with the specified options.
func OpenDb(opts *Options, name string) (*DB, error) {
var (
cErr *C.char
cName = C.CString(name)
)
defer C.free(unsafe.Pointer(cName))
db := C.rdb_open(opts.c, cName, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return nil, errors.New(C.GoString(cErr))
}
return &DB{
name: name,
c: db,
opts: opts,
}, nil
}
// OpenDbForReadOnly opens a database with the specified options for readonly usage.
func OpenDbForReadOnly(opts *Options, name string, errorIfLogFileExist bool) (*DB, error) {
var (
cErr *C.char
cName = C.CString(name)
)
defer C.free(unsafe.Pointer(cName))
db := C.rdb_open_for_read_only(opts.c, cName, boolToChar(errorIfLogFileExist), &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return nil, errors.New(C.GoString(cErr))
}
return &DB{
name: name,
c: db,
opts: opts,
}, nil
}
// Close closes the database.
func (db *DB) Close() {
C.rdb_close(db.c)
}
// Get returns the data associated with the key from the database. Remember
// to deallocate the returned Slice.
func (db *DB) Get(opts *ReadOptions, key []byte) (*Slice, error) {
var (
cErr *C.char
cValLen C.size_t
cKey = byteToChar(key)
)
cValue := C.rdb_get(db.c, opts.c, cKey, C.size_t(len(key)), &cValLen, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return nil, errors.New(C.GoString(cErr))
}
return NewSlice(cValue, cValLen), nil
}
// GetBytes is like Get but returns a copy of the data.
func (db *DB) GetBytes(opts *ReadOptions, key []byte) ([]byte, error) {
var (
cErr *C.char
cValLen C.size_t
cKey = byteToChar(key)
)
cValue := C.rdb_get(db.c, opts.c, cKey, C.size_t(len(key)), &cValLen, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return nil, errors.New(C.GoString(cErr))
}
if cValue == nil {
return nil, nil
}
defer C.free(unsafe.Pointer(cValue))
return C.GoBytes(unsafe.Pointer(cValue), C.int(cValLen)), nil
}
// Put writes data associated with a key to the database.
func (db *DB) Put(opts *WriteOptions, key, value []byte) error {
var (
cErr *C.char
cKey = byteToChar(key)
cValue = byteToChar(value)
)
C.rdb_put(db.c, opts.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)), &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// Delete removes the data associated with the key from the database.
func (db *DB) Delete(opts *WriteOptions, key []byte) error {
var (
cErr *C.char
cKey = byteToChar(key)
)
C.rdb_delete(db.c, opts.c, cKey, C.size_t(len(key)), &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// Write writes a WriteBatch to the database
func (db *DB) Write(opts *WriteOptions, batch *WriteBatch) error {
var cErr *C.char
C.rdb_write(db.c, opts.c, batch.c, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// NewIterator returns an Iterator over the the database that uses the
// ReadOptions given.
func (db *DB) NewIterator(opts *ReadOptions) *Iterator {
cIter := C.rdb_create_iterator(db.c, opts.c)
return NewNativeIterator(unsafe.Pointer(cIter))
}
// GetProperty returns the value of a database property.
func (db *DB) GetProperty(propName string) string {
cprop := C.CString(propName)
defer C.free(unsafe.Pointer(cprop))
cValue := C.rdb_property_value(db.c, cprop)
defer C.free(unsafe.Pointer(cValue))
return C.GoString(cValue)
}
// GetStats returns stats of our data store.
func (db *DB) GetStats() string { return db.GetProperty("rocksdb.stats") }