This repository has been archived by the owner on Apr 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
bdb.go
253 lines (197 loc) · 5.87 KB
/
bdb.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
package berkeleydb
// #cgo LDFLAGS: -ldb
// #include <db.h>
// #include <stdlib.h>
// #include "bdb.h"
import "C"
import (
"errors"
"fmt"
"unsafe"
)
const version string = "0.0.1"
// Flags for opening a database or environment.
const (
DbCreate = C.DB_CREATE
DbExcl = C.DB_EXCL
DbRdOnly = C.DB_RDONLY
DbTruncate = C.DB_TRUNCATE
// DbInitMpool is used in environment only
DbInitMpool = C.DB_INIT_MPOOL
)
// Database types.
const (
DbBtree = C.DB_BTREE
DbHash = C.DB_HASH
DbRecno = C.DB_RECNO
DbQueue = C.DB_QUEUE
DbUnknown = C.DB_UNKNOWN
)
// Db is the structure that holds the database connection
type Db struct {
db *C.DB
}
// Cursor holds the current cursor position
type Cursor struct {
dbc *C.DBC
}
// Errno is the error number
type Errno int
// NewDB initialises a new bdb connection
func NewDB() (*Db, error) {
var db *C.DB
err := C.db_create(&db, nil, 0)
if err > 0 {
return nil, createError(err)
}
return &Db{db}, nil
}
// NewDBInEnvironment initialises a new bdb connection in an environment
func NewDBInEnvironment(env *Environment) (*Db, error) {
var db *C.DB
err := C.db_create(&db, env.environ, 0)
if err > 0 {
return nil, createError(err)
}
return &Db{db}, nil
}
// OpenWithTxn opens the database in transaction mode (transactions are not yet supported by all
// funtions)
func (handle *Db) OpenWithTxn(filename string, txn *C.DB_TXN, dbtype C.DBTYPE, flags C.u_int32_t) error {
db := handle.db
file := C.CString(filename)
defer C.free(unsafe.Pointer(file))
ret := C.go_db_open(db, txn, file, nil, dbtype, flags, 0)
return createError(ret)
}
// Open a database file
func (handle *Db) Open(filename string, dbtype C.DBTYPE, flags C.u_int32_t) error {
file := C.CString(filename)
defer C.free(unsafe.Pointer(file))
ret := C.go_db_open(handle.db, nil, file, nil, dbtype, flags, 0)
return createError(ret)
}
// Close the database file
func (handle *Db) Close() error {
ret := C.go_db_close(handle.db, 0)
return createError(ret)
}
// Flags returns the flags of the database connection
func (handle *Db) Flags() (C.u_int32_t, error) {
var flags C.u_int32_t
ret := C.go_db_get_open_flags(handle.db, &flags)
return flags, createError(ret)
}
// Remove the database
func (handle *Db) Remove(filename string) error {
file := C.CString(filename)
defer C.free(unsafe.Pointer(file))
ret := C.go_db_remove(handle.db, file)
return createError(ret)
}
// Rename the database filename
func (handle *Db) Rename(oldname, newname string) error {
oname := C.CString(oldname)
defer C.free(unsafe.Pointer(oname))
nname := C.CString(newname)
defer C.free(unsafe.Pointer(nname))
ret := C.go_db_rename(handle.db, oname, nname)
return createError(ret)
}
// Put a key/value pair into the database
func (handle *Db) Put(name, value string) error {
nname := C.CString(name)
defer C.free(unsafe.Pointer(nname))
nvalue := C.CString(value)
defer C.free(unsafe.Pointer(nvalue))
ret := C.go_db_put_string(handle.db, nname, nvalue, 0)
if ret > 0 {
return createError(ret)
}
return nil
}
// Get a value from the database by key
func (handle *Db) Get(name string) (string, error) {
value := C.CString("")
defer C.free(unsafe.Pointer(value))
nname := C.CString(name)
defer C.free(unsafe.Pointer(nname))
ret := C.go_db_get_string(handle.db, nname, value)
return C.GoString(value), createError(ret)
}
// Delete a value from the database by key
func (handle *Db) Delete(name string) error {
nname := C.CString(name)
defer C.free(unsafe.Pointer(nname))
ret := C.go_db_del_string(handle.db, nname)
return createError(ret)
}
//Cursor returns a handle for the database cursor
func (handle *Db) Cursor() (*Cursor, error) {
var dbc *C.DBC
err := C.go_db_cursor(handle.db, &dbc)
if err > 0 {
return nil, createError(err)
}
return &Cursor{dbc}, nil
}
//GetNext moves the cursor to the next entry and returns the key/value pair
func (cursor *Cursor) GetNext() (string, string, error) {
value := C.CString("")
defer C.free(unsafe.Pointer(value))
key := C.CString("")
defer C.free(unsafe.Pointer(key))
ret := C.go_cursor_get_next(cursor.dbc, key, value)
return C.GoString(key), C.GoString(value), createError(ret)
}
//GetPrevious moves the cursor to the previous entry and returns the key/value pair
func (cursor *Cursor) GetPrevious() (string, string, error) {
value := C.CString("")
defer C.free(unsafe.Pointer(value))
key := C.CString("")
defer C.free(unsafe.Pointer(key))
ret := C.go_cursor_get_prev(cursor.dbc, key, value)
return C.GoString(key), C.GoString(value), createError(ret)
}
//GetFirst moves the cursor to the first entry and returns the key/value pair
func (cursor *Cursor) GetFirst() (string, string, error) {
value := C.CString("")
defer C.free(unsafe.Pointer(value))
key := C.CString("")
defer C.free(unsafe.Pointer(key))
ret := C.go_cursor_get_first(cursor.dbc, key, value)
return C.GoString(key), C.GoString(value), createError(ret)
}
//GetLast moves the cursor to the last entry and returns the key/value pair
func (cursor *Cursor) GetLast() (string, string, error) {
value := C.CString("")
defer C.free(unsafe.Pointer(value))
key := C.CString("")
defer C.free(unsafe.Pointer(key))
ret := C.go_cursor_get_last(cursor.dbc, key, value)
return C.GoString(key), C.GoString(value), createError(ret)
}
// UTILITY FUNCTIONS
// Version returns the version of the database and binding
func Version() string {
libVersion := C.GoString(C.db_full_version(nil, nil, nil, nil, nil))
tpl := "%s (Go bindings v%s)"
return fmt.Sprintf(tpl, libVersion, version)
}
// DBError contains the database Error
type DBError struct {
Code int
Message string
}
func createError(code C.int) error {
if code == 0 {
return nil
}
msg := C.db_strerror(code)
e := DBError{int(code), C.GoString(msg)}
return errors.New(e.Error())
}
// Error return the string representation of the error
func (e *DBError) Error() string {
return fmt.Sprintf("%d: %s", e.Code, e.Message)
}