This repository has been archived by the owner on Mar 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtxn.go
197 lines (175 loc) · 4.94 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
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
}
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
}
if 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)
}
return &Txn{_txn}, nil
}
func (txn *Txn) Commit() error {
ret := C.mdb_txn_commit(txn._txn)
runtime.UnlockOSThread()
// 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)
runtime.UnlockOSThread()
// 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)
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)
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)
if val == nil {
ret := C.mdb_del(txn._txn, C.MDB_dbi(dbi), (*C.MDB_val)(&ckey), nil)
return errno(ret)
}
cval := Wrap(val)
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
}
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)
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