forked from szferi/gomdb
/
txn.go
267 lines (241 loc) · 6.08 KB
/
txn.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
265
266
package mdb
/*
#cgo LDFLAGS: -L/usr/local/lib -llmdb
#cgo CFLAGS: -I/usr/local
#include <stdlib.h>
#include <stdio.h>
#include <lmdb.h>
*/
import "C"
import (
"math"
"unsafe"
"bytes"
"encoding/gob"
)
// DBIOpen Database Flags
const (
REVERSEKEY = C.MDB_REVERSEKEY // use reverse string keys
DUPSORT = C.MDB_DUPSORT // use sorted duplicates
INTEGERKEY = C.MDB_INTEGERKEY // numeric keys in native byte order. The keys must all be of the same size.
DUPFIXED = C.MDB_DUPFIXED // with DUPSORT, sorted dup items have fixed size
INTEGERDUP = C.MDB_INTEGERDUP // with DUPSORT, dups are numeric in native byte order
REVERSEDUP = C.MDB_REVERSEDUP // with DUPSORT, use reverse string dups
CREATE = C.MDB_CREATE // create DB if not already existing
)
// put flags
const (
NODUPDATA = C.MDB_NODUPDATA
NOOVERWRITE = C.MDB_NOOVERWRITE
RESERVE = C.MDB_RESERVE
APPEND = C.MDB_APPEND
APPENDDUP = C.MDB_APPENDDUP
)
// Txn is Opaque structure for a transaction handle.
// All database operations require a transaction handle.
// Transactions may be read-only or read-write.
type Txn struct {
_txn *C.MDB_txn
}
func (env *Env) BeginTxn(parent *Txn, flags uint) (*Txn, error) {
var _txn *C.MDB_txn
var ptxn *C.MDB_txn
if parent == nil {
ptxn = nil
} else {
ptxn = parent._txn
}
ret := C.mdb_txn_begin(env._env, ptxn, C.uint(flags), &_txn)
if ret != SUCCESS {
return nil, Errno(ret)
}
return &Txn{_txn}, nil
}
func (txn *Txn) Commit() error {
ret := C.mdb_txn_commit(txn._txn)
if ret != SUCCESS {
return Errno(ret)
}
return nil
}
func (txn *Txn) Abort() {
C.mdb_txn_abort(txn._txn)
txn._txn = nil
}
func (txn *Txn) Reset() {
C.mdb_txn_reset(txn._txn)
}
func (txn *Txn) Renew() error {
ret := C.mdb_txn_renew(txn._txn)
if ret != SUCCESS {
return Errno(ret)
}
return nil
}
func (txn *Txn) DBIOpen(name *string, flags uint) (DBI, error) {
var _dbi C.MDB_dbi
var cname *C.char
if name == nil {
cname = nil
} else {
cname = C.CString(*name)
defer C.free(unsafe.Pointer(cname))
}
ret := C.mdb_dbi_open(txn._txn, cname, C.uint(flags), &_dbi)
if ret != SUCCESS {
return DBI(math.NaN()), Errno(ret)
}
return DBI(_dbi), nil
}
func (txn *Txn) Stat(dbi DBI) (*Stat, error) {
var _stat C.MDB_stat
ret := C.mdb_stat(txn._txn, C.MDB_dbi(dbi), &_stat)
if ret != SUCCESS {
return nil, Errno(ret)
}
stat := Stat{PSize: uint(_stat.ms_psize),
Depth: uint(_stat.ms_depth),
BranchPages: uint64(_stat.ms_branch_pages),
LeafPages: uint64(_stat.ms_leaf_pages),
OwerflowPages: uint64(_stat.ms_overflow_pages),
Entries: uint64(_stat.ms_entries)}
return &stat, nil
}
func (txn *Txn) Drop(dbi DBI, del int) error {
ret := C.mdb_drop(txn._txn, C.MDB_dbi(dbi), C.int(del))
if ret != SUCCESS {
return Errno(ret)
}
return nil
}
func (txn *Txn) Get(dbi DBI, key []byte) ([]byte, error) {
ckey := &C.MDB_val{mv_size: C.size_t(len(key)),
mv_data: unsafe.Pointer(&key[0])}
var cval *C.MDB_val
ret := C.mdb_get(txn._txn, C.MDB_dbi(dbi), ckey, cval)
if ret != SUCCESS {
return nil, Errno(ret)
}
val := C.GoBytes(cval.mv_data, C.int(cval.mv_size))
return val, nil
}
func (txn *Txn) GetGo(dbi DBI, key, val interface{}) error {
var key_buffer bytes.Buffer
encoder := gob.NewEncoder(&key_buffer)
err := encoder.Encode(key)
if err != nil {
return err
}
gkey := key_buffer.Bytes()
var bval []byte
val, err = txn.Get(dbi, gkey)
if err != nil {
return err
}
val_buffer := bytes.NewReader(bval)
decoder := gob.NewDecoder(val_buffer)
err = decoder.Decode(val)
if err != nil {
return err
}
return nil
}
func (txn *Txn) Put(dbi DBI, key []byte, val []byte, flags uint) error {
ckey := &C.MDB_val{mv_size: C.size_t(len(key)),
mv_data: unsafe.Pointer(&key[0])}
cval := &C.MDB_val{mv_size: C.size_t(len(val)),
mv_data: unsafe.Pointer(&val[0])}
ret := C.mdb_put(txn._txn, C.MDB_dbi(dbi), ckey, cval, C.uint(flags))
if ret != SUCCESS {
return Errno(ret)
}
return nil
}
func (txn *Txn) PutGo(dbi DBI, key, val interface {}, flags uint) error {
var bkey bytes.Buffer
encoder := gob.NewEncoder(&bkey)
err := encoder.Encode(key)
if err != nil {
return err
}
var bval bytes.Buffer
encoder = gob.NewEncoder(&bval)
err = encoder.Encode(val)
if err != nil {
return err
}
return txn.Put(dbi, bkey.Bytes(), bval.Bytes(), flags)
}
func (txn *Txn) Del(dbi DBI, key, val []byte) error {
ckey := &C.MDB_val{mv_size: C.size_t(len(key)),
mv_data: unsafe.Pointer(&key[0])}
var cval *C.MDB_val
if val == nil {
cval = nil
} else {
cval = &C.MDB_val{mv_size: C.size_t(len(val)),
mv_data: unsafe.Pointer(&val[0])}
}
ret := C.mdb_del(txn._txn, C.MDB_dbi(dbi), ckey, cval)
if ret != SUCCESS {
return Errno(ret)
}
return nil
}
func (txn *Txn) DelGo(dbi DBI, key, val interface {}) error {
var bkey bytes.Buffer
encoder := gob.NewEncoder(&bkey)
err := encoder.Encode(key)
if err != nil {
return err
}
var bval []byte
if val == nil {
bval = nil
} else {
var val_buffer bytes.Buffer
encoder = gob.NewEncoder(&val_buffer)
err = encoder.Encode(val)
if err != nil {
return err
}
bval = val_buffer.Bytes()
}
return txn.Del(dbi, bkey.Bytes(), bval)
}
type Cursor struct {
_cursor *C.MDB_cursor
}
func (txn *Txn) CursorOpen(dbi DBI) (*Cursor, error) {
var _cursor *C.MDB_cursor
ret := C.mdb_cursor_open(txn._txn, C.MDB_dbi(dbi), &_cursor)
if ret != SUCCESS {
return nil, Errno(ret)
}
return &Cursor{_cursor}, nil
}
func (txn *Txn) CursorRenew(cursor *Cursor) error {
ret := C.mdb_cursor_renew(txn._txn, cursor._cursor)
if ret != SUCCESS {
return Errno(ret)
}
return nil
}
/*
type CmpFunc func(a, b []byte) int
func (txn *Txn) SetCompare(dbi DBI, cmp CmpFunc) error {
f := func(a, b *C.MDB_val) C.int {
ga := C.GoBytes(a.mv_data, C.int(a.mv_size))
gb := C.GoBytes(a.mv_data, C.int(a.mv_size))
return C.int(cmp(ga, gb))
}
ret := C.mdb_set_compare(txn._txn, C.MDB_dbi(dbi), *unsafe.Pointer(&f))
if ret != SUCCESS {
return Errno(ret)
}
return nil
}
*/
// func (txn *Txn) SetDupSort(dbi DBI, comp *C.MDB_comp_func) error
// func (txn *Txn) SetRelFunc(dbi DBI, rel *C.MDB_rel_func) error
// func (txn *Txn) SetRelCtx(dbi DBI, void *) error