forked from szferi/gomdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxn.go
221 lines (197 loc) · 5.54 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
package mdb
/*
#cgo CFLAGS: -pthread -W -Wall -Wno-unused-parameter -Wbad-function-cast -O2 -g
#cgo freebsd CFLAGS: -DMDB_DSYNC=O_SYNC
#cgo openbsd CFLAGS: -DMDB_DSYNC=O_SYNC
#cgo netbsd CFLAGS: -DMDB_DSYNC=O_SYNC
#include <stdlib.h>
#include <stdio.h>
#include "lmdb.h"
*/
import "C"
import (
"math"
"runtime"
"unsafe"
)
// 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
nested bool
}
// BeginTxn wraps mdb_txn_begin. http://goo.gl/KoSCV1
//
// BUG: Due to the nature of Go and the lightweight nature of the gomdb API all
// operations on the returned transaction must be made within the caller's
// goroutine to ensure operations are serialized on the same OS thread. This
// same-goroutine restriction also applies to the creation of nested
// transactions.
func (env *Env) BeginTxn(parent *Txn, flags uint) (*Txn, error) {
var _txn *C.MDB_txn
var ptxn *C.MDB_txn
var nested bool
if parent == nil {
ptxn = nil
} else {
nested = true
ptxn = parent._txn
}
// top-level writable transactions must be serialized on an os thread.
if !nested && flags&RDONLY == 0 {
runtime.LockOSThread()
}
ret := C.mdb_txn_begin(env._env, ptxn, C.uint(flags), &_txn)
if ret != SUCCESS {
runtime.UnlockOSThread()
return nil, errno(ret)
}
txn := &Txn{_txn, nested}
return txn, nil
}
func (txn *Txn) unlockThread() {
if !txn.nested {
runtime.UnlockOSThread()
}
}
func (txn *Txn) Commit() error {
ret := C.mdb_txn_commit(txn._txn)
txn.unlockThread()
// The transaction handle is freed if there was no error
if ret == C.MDB_SUCCESS {
txn._txn = nil
}
return errno(ret)
}
func (txn *Txn) Abort() {
if txn._txn == nil {
return
}
C.mdb_txn_abort(txn._txn)
txn.unlockThread()
// The transaction handle is always freed.
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)
return errno(ret)
}
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),
OverflowPages: 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))
return errno(ret)
}
func (txn *Txn) Get(dbi DBI, key []byte) ([]byte, error) {
val, err := txn.GetVal(dbi, key)
if err != nil {
return nil, err
}
return val.Bytes(), nil
}
func (txn *Txn) GetVal(dbi DBI, key []byte) (*Val, error) {
ckey := Wrap(key)
defer ckey.Free()
var cval Val
ret := C.mdb_get(txn._txn, C.MDB_dbi(dbi), (*C.MDB_val)(ckey), (*C.MDB_val)(&cval))
return &cval, errno(ret)
}
func (txn *Txn) Put(dbi DBI, key []byte, val []byte, flags uint) error {
ckey := Wrap(key)
cval := Wrap(val)
defer ckey.Free()
defer cval.Free()
ret := C.mdb_put(txn._txn, C.MDB_dbi(dbi), (*C.MDB_val)(ckey), (*C.MDB_val)(cval), C.uint(flags))
return errno(ret)
}
func (txn *Txn) Del(dbi DBI, key, val []byte) error {
ckey := Wrap(key)
defer ckey.Free()
var cval *Val
if val != nil {
cval = Wrap(val)
defer cval.Free()
}
ret := C.mdb_del(txn._txn, C.MDB_dbi(dbi), (*C.MDB_val)(ckey), (*C.MDB_val)(cval))
return errno(ret)
}
type Cursor struct {
_cursor *C.MDB_cursor
nested bool
}
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, txn.nested}, nil
}
func (txn *Txn) CursorRenew(cursor *Cursor) error {
ret := C.mdb_cursor_renew(txn._txn, cursor._cursor)
return errno(ret)
}
/*
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))
return errno(ret)
}
*/
// 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