forked from tecbot/gorocksdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transactiondb.go
143 lines (130 loc) · 3.64 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
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.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
}
// 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.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.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.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.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
}