-
Notifications
You must be signed in to change notification settings - Fork 272
/
transaction.go
125 lines (112 loc) · 3.27 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
package gorocksdb
// #include <stdlib.h>
// #include "rocksdb/c.h"
import "C"
import (
"errors"
"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.rocksdb_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.rocksdb_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,
)
if cErr != nil {
defer C.rocksdb_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,
)
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 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,
)
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.
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)
if cErr != nil {
defer C.rocksdb_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)))
}
// Destroy deallocates the transaction object.
func (transaction *Transaction) Destroy() {
C.rocksdb_transaction_destroy(transaction.c)
transaction.c = nil
}