-
Notifications
You must be signed in to change notification settings - Fork 0
/
ndbm.go
351 lines (314 loc) · 8.28 KB
/
ndbm.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// State of the art structured storage for 1986.
//
// ndbm is a Go wrapper around the POSIX NDBM database interface.
// NDBM is built into Mac OS X and FreeBSD,
// and is available as a compatibility mode of the GDBM library on Linux.
package ndbm
// #include <stdlib.h>
// #include <string.h>
//
// #ifdef __linux
// #include "gdbm_compat.h"
// #else
// #define DBM_ITEM_NOT_FOUND 0
// #include <ndbm.h>
// #endif
//
// #cgo linux LDFLAGS: -lgdbm -lgdbm_compat
import "C"
import (
"fmt"
"os"
"syscall"
"unsafe"
)
// NDBM or compatible database.
// NDBM is not required by POSIX to be threadsafe, so this library isn't either.
type NDBM struct {
cDbm *C.DBM
}
// KeyAlreadyExists is returned when trying to insert a key that already exists.
type KeyAlreadyExists struct {
Key []byte
}
func (err KeyAlreadyExists) Error() string {
return fmt.Sprintf("Key already exists: %s", err.Key)
}
// KeyNotFound is returned when trying to fetch or delete a key that doesn't exist.
type KeyNotFound struct {
Key []byte
}
func (err KeyNotFound) Error() string {
return fmt.Sprintf("Key not found: %s", err.Key)
}
// Error is returned on unexpected NDBM or libc errors.
type Error struct {
dbErrNum int
errnoErr error
}
func (err Error) Error() string {
return fmt.Sprintf("NDBM error #%d (system error %v)", err.dbErrNum, err.errnoErr)
}
const (
checkError = -1
noError = 0
alreadyExists = 1
notFound = 1
)
// OpenWithDefaults opens an NDBM database in read-write mode
// and will create it if it doesn't exist with permissions ug=rw,o=.
func OpenWithDefaults(path string) (*NDBM, error) {
ndbm, err := Open(
path,
os.O_RDWR|os.O_CREATE,
syscall.S_IREAD|syscall.S_IWRITE|syscall.S_IRGRP|syscall.S_IWGRP)
return ndbm, err
}
// Open lets you specify how the database is opened, for example, if you want read-only mode.
func Open(path string, flags, mode int) (*NDBM, error) {
cPath := C.CString(path)
cDbm, err := C.dbm_open(cPath, C.int(flags), C.mode_t(mode))
C.free(unsafe.Pointer(cPath))
if cDbm == nil {
return nil, err
}
ndbm := &NDBM{
cDbm: cDbm,
}
return ndbm, nil
}
// Close closes the NDBM database.
func (ndbm *NDBM) Close() {
C.dbm_close(ndbm.cDbm)
}
// fd returns a file descriptor that can be used to lock the database.
// Not part of the POSIX standard but implemented in FreeBSD, Mac OS X, and GDBM.
func (ndbm *NDBM) fd() int {
return int(C.dbm_dirfno(ndbm.cDbm))
}
// ErrAlreadyLocked is returned if another process already has a lock.
var ErrAlreadyLocked = fmt.Errorf("Database is already locked")
func (ndbm *NDBM) lock(lockMode int) error {
err := syscall.Flock(ndbm.fd(), lockMode)
if err != nil {
if errno, ok := err.(syscall.Errno); ok {
if errno == syscall.EWOULDBLOCK {
return ErrAlreadyLocked
}
}
return err
}
return nil
}
// LockShared locks the database for multiple processes.
// For example, multiple readers can share a read-only database.
func (ndbm *NDBM) LockShared() error {
return ndbm.lock(syscall.LOCK_SH | syscall.LOCK_NB)
}
// LockExclusive locks the database for a single process.
func (ndbm *NDBM) LockExclusive() error {
return ndbm.lock(syscall.LOCK_EX | syscall.LOCK_NB)
}
// Unlock unlocks the database.
func (ndbm *NDBM) Unlock() error {
return ndbm.lock(syscall.LOCK_UN)
}
func datumToBytes(datum C.datum) []byte {
if datum.dptr == nil {
return nil
}
return C.GoBytes(unsafe.Pointer(datum.dptr), C.int(datum.dsize))
}
func (ndbm *NDBM) store(key, value []byte, mode C.int) (C.int, error) {
C.dbm_clearerr(ndbm.cDbm)
status, err := C.dbm_store(ndbm.cDbm, bytesToDatum(key), bytesToDatum(value), mode)
if status == checkError {
return status, Error{
dbErrNum: int(C.dbm_error(ndbm.cDbm)),
errnoErr: err,
}
}
return status, nil
}
// Insert inserts a new entry into the database.
// Returns KeyAlreadyExists if the key already exists.
func (ndbm *NDBM) Insert(key, value []byte) error {
status, err := ndbm.store(key, value, C.DBM_INSERT)
if err != nil {
return err
}
if status == alreadyExists {
return KeyAlreadyExists{key}
}
return nil
}
// Replace inserts a new entry or overwrites an existing entry.
func (ndbm *NDBM) Replace(key, value []byte) error {
_, err := ndbm.store(key, value, C.DBM_REPLACE)
return err
}
// Fetch retrieves an entry value by key.
// Returns KeyNotFound if the key can't be found.
func (ndbm *NDBM) Fetch(key []byte) ([]byte, error) {
C.dbm_clearerr(ndbm.cDbm)
datum, err := C.dbm_fetch(ndbm.cDbm, bytesToDatum(key))
value := datumToBytes(datum)
if value == nil {
dbErrNum := C.dbm_error(ndbm.cDbm)
if dbErrNum == noError {
return nil, KeyNotFound{key}
}
if dbErrNum == C.DBM_ITEM_NOT_FOUND {
return nil, KeyNotFound{key}
}
return nil, Error{
dbErrNum: int(dbErrNum),
errnoErr: err,
}
}
return value, nil
}
// Delete deletes an entry from the database.
// Returns KeyNotFound if the key can't be found.
func (ndbm *NDBM) Delete(key []byte) error {
C.dbm_clearerr(ndbm.cDbm)
status, err := C.dbm_delete(ndbm.cDbm, bytesToDatum(key))
if status == checkError {
dbErrNum := C.dbm_error(ndbm.cDbm)
if dbErrNum == C.DBM_ITEM_NOT_FOUND {
return KeyNotFound{key}
}
return Error{
dbErrNum: int(dbErrNum),
errnoErr: err,
}
}
if status == notFound {
return KeyNotFound{key}
}
return nil
}
var errNoMoreKeys = fmt.Errorf("No more keys")
func (ndbm *NDBM) firstKey() ([]byte, error) {
datum, err := C.dbm_firstkey(ndbm.cDbm)
key := datumToBytes(datum)
if key == nil {
dbErrNum := C.dbm_error(ndbm.cDbm)
if dbErrNum == noError {
return nil, errNoMoreKeys
}
return nil, Error{
dbErrNum: int(dbErrNum),
errnoErr: err,
}
}
return key, nil
}
func (ndbm *NDBM) nextKey() ([]byte, error) {
datum, err := C.dbm_nextkey(ndbm.cDbm)
key := datumToBytes(datum)
if key == nil {
dbErrNum := C.dbm_error(ndbm.cDbm)
if dbErrNum == noError {
return nil, errNoMoreKeys
}
return nil, Error{
dbErrNum: int(dbErrNum),
errnoErr: err,
}
}
return key, nil
}
// KeysCallback executes a callback function for every key in the database.
// The callback should take a key and return an error if there is a problem.
func (ndbm *NDBM) KeysCallback(callback func([]byte) error) error {
for key, err := ndbm.firstKey(); err == nil; key, err = ndbm.nextKey() {
if err != nil {
if err == errNoMoreKeys {
return nil
}
return err
}
err = callback(key)
if err != nil {
return err
}
}
return nil
}
// Keys lists every key in the database.
func (ndbm *NDBM) Keys() [][]byte {
keys := [][]byte{}
_ = ndbm.KeysCallback(func(key []byte) error {
keys = append(keys, key)
return nil
})
return keys
}
// Len returns the number of entries in the database.
func (ndbm *NDBM) Len() int {
count := 0
_ = ndbm.KeysCallback(func(_ []byte) error {
count++
return nil
})
return count
}
// ValuesCallback executes a callback function for every value in the database.
// The callback should take a value and return an error if there is a problem.
func (ndbm *NDBM) ValuesCallback(callback func([]byte) error) error {
return ndbm.KeysCallback(func(key []byte) error {
value, err := ndbm.Fetch(key)
if err != nil {
return nil
}
return callback(value)
})
}
// Values returns every value in the database.
func (ndbm *NDBM) Values() [][]byte {
values := [][]byte{}
_ = ndbm.ValuesCallback(func(value []byte) error {
values = append(values, value)
return nil
})
return values
}
// ItemsCallback executes a callback function for every entry in the database.
// The callback should take a key and value and return an error if there is a problem.
func (ndbm *NDBM) ItemsCallback(callback func(key, value []byte) error) error {
return ndbm.KeysCallback(func(key []byte) error {
value, err := ndbm.Fetch(key)
if err != nil {
return err
}
return callback(key, value)
})
}
// Item is a database entry.
type Item struct {
Key []byte
Value []byte
}
// Items returns every entry in the database.
func (ndbm *NDBM) Items() []Item {
items := []Item{}
_ = ndbm.ItemsCallback(func(key, value []byte) error {
items = append(items, Item{
Key: key,
Value: value,
})
return nil
})
return items
}
// Update takes a list of entries and upserts them into the database.
func (ndbm *NDBM) Update(items []Item) error {
for _, item := range items {
err := ndbm.Replace(item.Key, item.Value)
if err != nil {
return err
}
}
return nil
}