forked from tecbot/gorocksdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
transaction.go
207 lines (189 loc) · 5.73 KB
/
transaction.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
package gorocksdb
// #include <stdlib.h>
// #include "rocksdb/c.h"
import "C"
import (
"errors"
"runtime"
"unsafe"
)
// Transaction is used with TransactionDB for transaction support.
type Transaction struct {
c *C.rocksdb_transaction_t
}
// NewNativeTransaction creates a Transaction object.
func NewNativeTransaction(c *C.rocksdb_transaction_t) *Transaction {
return &Transaction{c}
}
// Commit commits the transaction to the database.
func (transaction *Transaction) Commit() error {
var (
cErr *C.char
)
C.rocksdb_transaction_commit(transaction.c, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// Rollback performs a rollback on the transaction.
func (transaction *Transaction) Rollback() error {
var (
cErr *C.char
)
C.rocksdb_transaction_rollback(transaction.c, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// Get returns the data associated with the key from the database given this transaction.
func (transaction *Transaction) Get(opts *ReadOptions, key []byte) (*Slice, error) {
var (
cErr *C.char
cValLen C.size_t
cKey = byteToChar(key)
)
cValue := C.rocksdb_transaction_get(
transaction.c, opts.c, cKey, C.size_t(len(key)), &cValLen, &cErr,
)
runtime.KeepAlive(key)
if cErr != nil {
defer C.rocksdb_free(unsafe.Pointer(cErr))
return nil, errors.New(C.GoString(cErr))
}
return NewSlice(cValue, cValLen), nil
}
// Get returns the data associated with the key from the database given this transaction and column family.
func (transaction *Transaction) GetCF(opts *ReadOptions, cf *ColumnFamilyHandle, key []byte) (*Slice, error) {
var (
cErr *C.char
cValLen C.size_t
cKey = byteToChar(key)
)
cValue := C.rocksdb_transaction_get_cf(
transaction.c, opts.c, cf.c, cKey, C.size_t(len(key)), &cValLen, &cErr,
)
runtime.KeepAlive(key)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return nil, errors.New(C.GoString(cErr))
}
return NewSlice(cValue, cValLen), nil
}
// GetForUpdate queries the data associated with the key and puts an exclusive lock on the key from the database given this transaction.
func (transaction *Transaction) GetForUpdate(opts *ReadOptions, key []byte) (*Slice, error) {
var (
cErr *C.char
cValLen C.size_t
cKey = byteToChar(key)
)
cValue := C.rocksdb_transaction_get_for_update(
transaction.c, opts.c, cKey, C.size_t(len(key)), &cValLen, C.uchar(byte(1)) /*exclusive*/, &cErr,
)
runtime.KeepAlive(key)
if cErr != nil {
defer C.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 transaction.
func (transaction *Transaction) Put(key, value []byte) error {
var (
cErr *C.char
cKey = byteToChar(key)
cValue = byteToChar(value)
)
C.rocksdb_transaction_put(
transaction.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)), &cErr,
)
runtime.KeepAlive(key)
runtime.KeepAlive(value)
if cErr != nil {
defer C.rocksdb_free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// Put writes data associated with a key to the transaction and column family.
func (transaction *Transaction) PutCF(cf *ColumnFamilyHandle, key, value []byte) error {
var (
cErr *C.char
cKey = byteToChar(key)
cValue = byteToChar(value)
)
C.rocksdb_transaction_put_cf(
transaction.c, cf.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)), &cErr,
)
runtime.KeepAlive(key)
runtime.KeepAlive(value)
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 transaction.
func (transaction *Transaction) Delete(key []byte) error {
var (
cErr *C.char
cKey = byteToChar(key)
)
C.rocksdb_transaction_delete(transaction.c, cKey, C.size_t(len(key)), &cErr)
runtime.KeepAlive(key)
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 transaction and column family.
func (transaction *Transaction) DeleteCF(cf *ColumnFamilyHandle, key []byte) error {
var (
cErr *C.char
cKey = byteToChar(key)
)
C.rocksdb_transaction_delete_cf(transaction.c, cf.c, cKey, C.size_t(len(key)), &cErr)
runtime.KeepAlive(key)
if cErr != nil {
defer C.rocksdb_free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// Merge merges the data associated with the key with the actual data in the database.
func (transaction *Transaction) Merge(key []byte, value []byte) error {
var (
cErr *C.char
cKey = byteToChar(key)
cValue = byteToChar(value)
)
C.rocksdb_transaction_merge(transaction.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)), &cErr)
runtime.KeepAlive(key)
runtime.KeepAlive(value)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// NewIterator returns an Iterator over the database that uses the
// ReadOptions given.
func (transaction *Transaction) NewIterator(opts *ReadOptions) *Iterator {
return NewNativeIterator(
unsafe.Pointer(C.rocksdb_transaction_create_iterator(transaction.c, opts.c)))
}
// NewIterator returns an Iterator over the database that uses the
// ReadOptions given and column family.
func (transaction *Transaction) NewIteratorCF(opts *ReadOptions, cf *ColumnFamilyHandle) *Iterator {
return NewNativeIterator(
unsafe.Pointer(C.rocksdb_transaction_create_iterator_cf(transaction.c, opts.c, cf.c)))
}
// Destroy deallocates the transaction object.
func (transaction *Transaction) Destroy() {
C.rocksdb_transaction_destroy(transaction.c)
transaction.c = nil
}