-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathtransaction.go
257 lines (215 loc) · 7.41 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
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
package grocksdb
// #include <stdlib.h>
// #include "rocksdb/c.h"
import "C"
import (
"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() (err error) {
var cErr *C.char
C.rocksdb_transaction_commit(transaction.c, &cErr)
err = fromCError(cErr)
return
}
// Rollback performs a rollback on the transaction.
func (transaction *Transaction) Rollback() (err error) {
var cErr *C.char
C.rocksdb_transaction_rollback(transaction.c, &cErr)
err = fromCError(cErr)
return
}
// Get returns the data associated with the key from the database given this transaction.
func (transaction *Transaction) Get(opts *ReadOptions, key []byte) (slice *Slice, err 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,
)
if err = fromCError(cErr); err == nil {
slice = NewSlice(cValue, cValLen)
}
return
}
// GetWithCF returns the data associated with the key from the database, with column family, given this transaction.
func (transaction *Transaction) GetWithCF(opts *ReadOptions, cf *ColumnFamilyHandle, key []byte) (slice *Slice, err 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,
)
if err = fromCError(cErr); err == nil {
slice = NewSlice(cValue, cValLen)
}
return
}
// 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 *Slice, err 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,
)
if err = fromCError(cErr); err == nil {
slice = NewSlice(cValue, cValLen)
}
return
}
// GetForUpdateWithCF queries the data associated with the key and puts an exclusive lock on the key
// from the database, with column family, given this transaction.
func (transaction *Transaction) GetForUpdateWithCF(opts *ReadOptions, cf *ColumnFamilyHandle, key []byte) (slice *Slice, err error) {
var (
cErr *C.char
cValLen C.size_t
cKey = byteToChar(key)
)
cValue := C.rocksdb_transaction_get_for_update_cf(
transaction.c, opts.c, cf.c, cKey, C.size_t(len(key)), &cValLen, C.uchar(byte(1)) /*exclusive*/, &cErr,
)
if err = fromCError(cErr); err == nil {
slice = NewSlice(cValue, cValLen)
}
return
}
// Put writes data associated with a key to the transaction.
func (transaction *Transaction) Put(key, value []byte) (err 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,
)
err = fromCError(cErr)
return
}
// PutCF writes data associated with a key to the transaction. Key belongs to column family.
func (transaction *Transaction) PutCF(cf *ColumnFamilyHandle, key, value []byte) (err 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,
)
err = fromCError(cErr)
return
}
// Merge key, value to the transaction.
func (transaction *Transaction) Merge(key, value []byte) (err 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,
)
err = fromCError(cErr)
return
}
// MergeCF key, value to the transaction on specific column family.
func (transaction *Transaction) MergeCF(cf *ColumnFamilyHandle, key, value []byte) (err error) {
var (
cErr *C.char
cKey = byteToChar(key)
cValue = byteToChar(value)
)
C.rocksdb_transaction_merge_cf(
transaction.c, cf.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)), &cErr,
)
err = fromCError(cErr)
return
}
// Delete removes the data associated with the key from the transaction.
func (transaction *Transaction) Delete(key []byte) (err error) {
var (
cErr *C.char
cKey = byteToChar(key)
)
C.rocksdb_transaction_delete(transaction.c, cKey, C.size_t(len(key)), &cErr)
err = fromCError(cErr)
return
}
// DeleteCF removes the data associated with the key (belongs to specific column family) from the transaction.
func (transaction *Transaction) DeleteCF(cf *ColumnFamilyHandle, key []byte) (err error) {
var (
cErr *C.char
cKey = byteToChar(key)
)
C.rocksdb_transaction_delete_cf(transaction.c, cf.c, cKey, C.size_t(len(key)), &cErr)
err = fromCError(cErr)
return
}
// NewIterator returns an iterator that will iterate on all keys in the default
// column family including both keys in the DB and uncommitted keys in this
// transaction.
//
// Setting read_options.snapshot will affect what is read from the
// DB but will NOT change which keys are read from this transaction (the keys
// in this transaction do not yet belong to any snapshot and will be fetched
// regardless).
//
// Caller is responsible for deleting the returned Iterator.
func (transaction *Transaction) NewIterator(opts *ReadOptions) *Iterator {
return NewNativeIterator(
unsafe.Pointer(C.rocksdb_transaction_create_iterator(transaction.c, opts.c)))
}
// NewIteratorCF returns an iterator that will iterate on all keys in the specific
// column family including both keys in the DB and uncommitted keys in this
// transaction.
//
// Setting read_options.snapshot will affect what is read from the
// DB but will NOT change which keys are read from this transaction (the keys
// in this transaction do not yet belong to any snapshot and will be fetched
// regardless).
//
// Caller is responsible for deleting the returned Iterator.
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)))
}
// SetSavePoint records the state of the transaction for future calls to
// RollbackToSavePoint(). May be called multiple times to set multiple save
// points.
func (transaction *Transaction) SetSavePoint() {
C.rocksdb_transaction_set_savepoint(transaction.c)
}
// RollbackToSavePoint undo all operations in this transaction (Put, Merge, Delete, PutLogData)
// since the most recent call to SetSavePoint() and removes the most recent
// SetSavePoint().
func (transaction *Transaction) RollbackToSavePoint() (err error) {
var cErr *C.char
C.rocksdb_transaction_rollback_to_savepoint(transaction.c, &cErr)
err = fromCError(cErr)
return
}
// GetSnapshot returns the Snapshot created by the last call to SetSnapshot().
func (transaction *Transaction) GetSnapshot() *Snapshot {
return NewNativeSnapshot(C.rocksdb_transaction_get_snapshot(transaction.c))
}
// Destroy deallocates the transaction object.
func (transaction *Transaction) Destroy() {
C.rocksdb_transaction_destroy(transaction.c)
transaction.c = nil
}