forked from tecbot/gorocksdb
-
Notifications
You must be signed in to change notification settings - Fork 7
/
transactiondb.go
264 lines (239 loc) · 7.13 KB
/
transactiondb.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
package gorocksdb
// #include <stdlib.h>
// #include "rocksdb/c.h"
import "C"
import (
"errors"
"unsafe"
)
// TransactionDB is a reusable handle to a RocksDB transactional database on disk, created by OpenTransactionDb.
type TransactionDB struct {
c *C.rocksdb_transactiondb_t
name string
opts *Options
transactionDBOpts *TransactionDBOptions
}
// OpenTransactionDb opens a database with the specified options.
func OpenTransactionDb(
opts *Options,
transactionDBOpts *TransactionDBOptions,
name string,
) (*TransactionDB, error) {
var (
cErr *C.char
cName = C.CString(name)
)
defer C.free(unsafe.Pointer(cName))
db := C.rocksdb_transactiondb_open(
opts.c, transactionDBOpts.c, cName, &cErr)
if cErr != nil {
defer C.rocksdb_free(unsafe.Pointer(cErr))
return nil, errors.New(C.GoString(cErr))
}
return &TransactionDB{
name: name,
c: db,
opts: opts,
transactionDBOpts: transactionDBOpts,
}, nil
}
// NewSnapshot creates a new snapshot of the database.
func (db *TransactionDB) NewSnapshot() *Snapshot {
return NewNativeSnapshot(C.rocksdb_transactiondb_create_snapshot(db.c))
}
// ReleaseSnapshot releases the snapshot and its resources.
func (db *TransactionDB) ReleaseSnapshot(snapshot *Snapshot) {
C.rocksdb_transactiondb_release_snapshot(db.c, snapshot.c)
snapshot.c = nil
}
// GetProperty returns the value of a database property.
func (db *TransactionDB) GetProperty(propName string) string {
cprop := C.CString(propName)
defer C.free(unsafe.Pointer(cprop))
cValue := C.rocksdb_transactiondb_property_value(db.c, cprop)
defer C.rocksdb_free(unsafe.Pointer(cValue))
return C.GoString(cValue)
}
// TransactionBegin begins a new transaction
// with the WriteOptions and TransactionOptions given.
func (db *TransactionDB) TransactionBegin(
opts *WriteOptions,
transactionOpts *TransactionOptions,
oldTransaction *Transaction,
) *Transaction {
if oldTransaction != nil {
return NewNativeTransaction(C.rocksdb_transaction_begin(
db.c,
opts.c,
transactionOpts.c,
oldTransaction.c,
))
}
return NewNativeTransaction(C.rocksdb_transaction_begin(
db.c, opts.c, transactionOpts.c, nil))
}
// Get returns the data associated with the key from the database.
func (db *TransactionDB) Get(opts *ReadOptions, key []byte) (*Slice, error) {
var (
cErr *C.char
cValLen C.size_t
cKey = byteToChar(key)
)
cValue := C.rocksdb_transactiondb_get(
db.c, opts.c, cKey, C.size_t(len(key)), &cValLen, &cErr,
)
if cErr != nil {
defer C.rocksdb_free(unsafe.Pointer(cErr))
return nil, errors.New(C.GoString(cErr))
}
return NewSlice(cValue, cValLen), nil
}
// Put writes data associated with a key to the database.
func (db *TransactionDB) Put(opts *WriteOptions, key, value []byte) error {
var (
cErr *C.char
cKey = byteToChar(key)
cValue = byteToChar(value)
)
C.rocksdb_transactiondb_put(
db.c, opts.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)), &cErr,
)
if cErr != nil {
defer C.rocksdb_free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// Write writes a WriteBatch to the database
func (db *TransactionDB) Write(opts *WriteOptions, batch *WriteBatch) error {
var cErr *C.char
C.rocksdb_transactiondb_write(db.c, opts.c, batch.c, &cErr)
if cErr != nil {
defer C.rocksdb_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 *TransactionDB) Delete(opts *WriteOptions, key []byte) error {
var (
cErr *C.char
cKey = byteToChar(key)
)
C.rocksdb_transactiondb_delete(db.c, opts.c, cKey, C.size_t(len(key)), &cErr)
if cErr != nil {
defer C.rocksdb_free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// NewCheckpoint creates a new Checkpoint for this db.
func (db *TransactionDB) NewCheckpoint() (*Checkpoint, error) {
var (
cErr *C.char
)
cCheckpoint := C.rocksdb_transactiondb_checkpoint_object_create(
db.c, &cErr,
)
if cErr != nil {
defer C.rocksdb_free(unsafe.Pointer(cErr))
return nil, errors.New(C.GoString(cErr))
}
return NewNativeCheckpoint(cCheckpoint), nil
}
// Close closes the database.
func (transactionDB *TransactionDB) Close() {
C.rocksdb_transactiondb_close(transactionDB.c)
transactionDB.c = nil
}
// OptimisticTransactionDB is a reusable handle to a RocksDB optimistic transactional database on disk, created by OpenOptimisticTransactionDb.
type OptimisticTransactionDB struct {
c *C.rocksdb_optimistictransactiondb_t
name string
opts *Options
}
// OpenTransactionDb opens a database with the specified options.
func OpenOptimisticTransactionDb(
opts *Options,
name string,
) (*OptimisticTransactionDB, error) {
var (
cErr *C.char
cName = C.CString(name)
)
defer C.free(unsafe.Pointer(cName))
db := C.rocksdb_optimistictransactiondb_open(opts.c, cName, &cErr)
if cErr != nil {
defer C.rocksdb_free(unsafe.Pointer(cErr))
return nil, errors.New(C.GoString(cErr))
}
return &OptimisticTransactionDB{
name: name,
c: db,
opts: opts,
}, nil
}
// GetBaseDB returns the base database.
func (db *OptimisticTransactionDB) GetBaseDB() *DB {
return &DB{
c: C.rocksdb_optimistictransactiondb_get_base_db(db.c),
closer: func(c *C.rocksdb_t) { C.rocksdb_optimistictransactiondb_close_base_db(c) },
name: db.name,
opts: db.opts,
}
}
// GetProperty returns the value of a database property.
func (db *OptimisticTransactionDB) GetProperty(propName string) string {
cprop := C.CString(propName)
defer C.free(unsafe.Pointer(cprop))
cValue := C.rocksdb_optimistictransactiondb_property_value(db.c, cprop)
defer C.rocksdb_free(unsafe.Pointer(cValue))
return C.GoString(cValue)
}
// TransactionBegin begins a new transaction
// with the WriteOptions and TransactionOptions given.
func (db *OptimisticTransactionDB) TransactionBegin(
opts *WriteOptions,
transactionOpts *OptimisticTransactionOptions,
oldTransaction *Transaction,
) *Transaction {
if oldTransaction != nil {
return NewNativeTransaction(C.rocksdb_optimistictransaction_begin(
db.c,
opts.c,
transactionOpts.c,
oldTransaction.c,
))
}
return NewNativeTransaction(C.rocksdb_optimistictransaction_begin(
db.c, opts.c, transactionOpts.c, nil))
}
// Write writes a WriteBatch to the database
func (db *OptimisticTransactionDB) Write(opts *WriteOptions, batch *WriteBatch) error {
var cErr *C.char
C.rocksdb_optimistictransactiondb_write(db.c, opts.c, batch.c, &cErr)
if cErr != nil {
defer C.rocksdb_free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// Close closes the database.
func (transactionDB *OptimisticTransactionDB) Close() {
C.rocksdb_optimistictransactiondb_close(transactionDB.c)
transactionDB.c = nil
}
// NewCheckpoint creates a new Checkpoint for this db.
func (db *OptimisticTransactionDB) NewCheckpoint() (*Checkpoint, error) {
var (
cErr *C.char
)
cCheckpoint := C.rocksdb_optimistictransactiondb_checkpoint_object_create(
db.c, &cErr,
)
if cErr != nil {
defer C.rocksdb_free(unsafe.Pointer(cErr))
return nil, errors.New(C.GoString(cErr))
}
return NewNativeCheckpoint(cCheckpoint), nil
}