forked from szferi/gomdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
227 lines (198 loc) · 6.03 KB
/
env.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
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 (
"errors"
"fmt"
"syscall"
"unsafe"
)
const SUCCESS = C.MDB_SUCCESS
// mdb_env Environment Flags
const (
FIXEDMAP = C.MDB_FIXEDMAP // mmap at a fixed address (experimental)
NOSUBDIR = C.MDB_NOSUBDIR // no environment directory
NOSYNC = C.MDB_NOSYNC // don't fsync after commit
RDONLY = C.MDB_RDONLY // read only
NOMETASYNC = C.MDB_NOMETASYNC // don't fsync metapage after commit
WRITEMAP = C.MDB_WRITEMAP // use writable mmap
MAPASYNC = C.MDB_MAPASYNC // use asynchronous msync when MDB_WRITEMAP is use
NOTLS = C.MDB_NOTLS // tie reader locktable slots to Txn objects instead of threads
)
type DBI uint
type Errno C.int
// minimum and maximum values produced for the Errno type. syscall.Errnos of
// other values may still be produced.
const minErrno, maxErrno C.int = C.MDB_KEYEXIST, C.MDB_LAST_ERRCODE
func (e Errno) Error() string {
s := C.GoString(C.mdb_strerror(C.int(e)))
if s == "" {
return fmt.Sprint("mdb errno:", int(e))
}
return s
}
// for tests that can't import C
func _errno(ret int) error {
return errno(C.int(ret))
}
func errno(ret C.int) error {
if ret == C.MDB_SUCCESS {
return nil
}
if minErrno <= ret && ret <= maxErrno {
return Errno(ret)
}
return syscall.Errno(ret)
}
// error codes
const (
KeyExist = Errno(C.MDB_KEYEXIST)
NotFound = Errno(C.MDB_NOTFOUND)
PageNotFound = Errno(C.MDB_PAGE_NOTFOUND)
Corrupted = Errno(C.MDB_CORRUPTED)
Panic = Errno(C.MDB_PANIC)
VersionMismatch = Errno(C.MDB_VERSION_MISMATCH)
Invalid = Errno(C.MDB_INVALID)
MapFull = Errno(C.MDB_MAP_FULL)
DbsFull = Errno(C.MDB_DBS_FULL)
ReadersFull = Errno(C.MDB_READERS_FULL)
TlsFull = Errno(C.MDB_TLS_FULL)
TxnFull = Errno(C.MDB_TXN_FULL)
CursorFull = Errno(C.MDB_CURSOR_FULL)
PageFull = Errno(C.MDB_PAGE_FULL)
MapResized = Errno(C.MDB_MAP_RESIZED)
Incompatibile = Errno(C.MDB_INCOMPATIBLE)
)
func Version() string {
var major, minor, patch *C.int
ver_str := C.mdb_version(major, minor, patch)
return C.GoString(ver_str)
}
// Env is opaque structure for a database environment.
// A DB environment supports multiple databases, all residing in the
// same shared-memory map.
type Env struct {
_env *C.MDB_env
}
// Create an MDB environment handle.
func NewEnv() (*Env, error) {
var _env *C.MDB_env
ret := C.mdb_env_create(&_env)
if ret != SUCCESS {
return nil, errno(ret)
}
return &Env{_env}, nil
}
// Open an environment handle. If this function fails Close() must be called to discard the Env handle.
func (env *Env) Open(path string, flags uint, mode uint) error {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
ret := C.mdb_env_open(env._env, cpath, C.uint(NOTLS|flags), C.mdb_mode_t(mode))
return errno(ret)
}
func (env *Env) Close() error {
if env._env == nil {
return errors.New("Environment already closed")
}
C.mdb_env_close(env._env)
env._env = nil
return nil
}
func (env *Env) Copy(path string) error {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
ret := C.mdb_env_copy(env._env, cpath)
return errno(ret)
}
// Statistics for a database in the environment
type Stat struct {
PSize uint // Size of a database page. This is currently the same for all databases.
Depth uint // Depth (height) of the B-tree
BranchPages uint64 // Number of internal (non-leaf) pages
LeafPages uint64 // Number of leaf pages
OverflowPages uint64 // Number of overflow pages
Entries uint64 // Number of data items
}
func (env *Env) Stat() (*Stat, error) {
var _stat C.MDB_stat
ret := C.mdb_env_stat(env._env, &_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
}
type Info struct {
MapSize uint64 // Size of the data memory map
LastPNO uint64 // ID of the last used page
LastTxnID uint64 // ID of the last committed transaction
MaxReaders uint // maximum number of threads for the environment
NumReaders uint // maximum number of threads used in the environment
}
func (env *Env) Info() (*Info, error) {
var _info C.MDB_envinfo
ret := C.mdb_env_info(env._env, &_info)
if ret != SUCCESS {
return nil, errno(ret)
}
info := Info{MapSize: uint64(_info.me_mapsize),
LastPNO: uint64(_info.me_last_pgno),
LastTxnID: uint64(_info.me_last_txnid),
MaxReaders: uint(_info.me_maxreaders),
NumReaders: uint(_info.me_numreaders)}
return &info, nil
}
func (env *Env) Sync(force int) error {
ret := C.mdb_env_sync(env._env, C.int(force))
return errno(ret)
}
func (env *Env) SetFlags(flags uint, onoff int) error {
ret := C.mdb_env_set_flags(env._env, C.uint(flags), C.int(onoff))
return errno(ret)
}
func (env *Env) Flags() (uint, error) {
var _flags C.uint
ret := C.mdb_env_get_flags(env._env, &_flags)
if ret != SUCCESS {
return 0, errno(ret)
}
return uint(_flags), nil
}
func (env *Env) Path() (string, error) {
var path string
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
ret := C.mdb_env_get_path(env._env, &cpath)
if ret != SUCCESS {
return "", errno(ret)
}
return C.GoString(cpath), nil
}
func (env *Env) SetMapSize(size uint64) error {
ret := C.mdb_env_set_mapsize(env._env, C.mdb_size_t(size))
return errno(ret)
}
func (env *Env) SetMaxReaders(size uint) error {
ret := C.mdb_env_set_maxreaders(env._env, C.uint(size))
return errno(ret)
}
func (env *Env) SetMaxDBs(size DBI) error {
ret := C.mdb_env_set_maxdbs(env._env, C.MDB_dbi(size))
return errno(ret)
}
func (env *Env) DBIClose(dbi DBI) {
C.mdb_dbi_close(env._env, C.MDB_dbi(dbi))
}