-
Notifications
You must be signed in to change notification settings - Fork 7
/
forestdb.go
182 lines (141 loc) · 3.06 KB
/
forestdb.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
package goforestdb
/*
#cgo LDFLAGS: -lforestdb
#include <stdlib.h>
#include <string.h>
#include "forestdb/forestdb.h"
void init_fdb_config(fdb_config *config)
{
memset(config, 0, sizeof(fdb_config));
config->chunksize = config->offsetsize = sizeof(uint64_t);
config->buffercache_size = 1 * 1024 * 1024;
config->wal_threshold = 1024;
config->seqtree = FDB_SEQTREE_USE;
config->flag = 0;
}
*/
import "C"
import (
"errors"
"fmt"
"unsafe"
// "log"
)
type Errno int
func (e Errno) Error() string {
s := errText[e]
if s == "" {
return fmt.Sprintf("errno %d", int(e))
}
return s
}
var errText = map[Errno]string{
1: "DB Operation Failed",
2: "Invalid args for operation",
}
type Conn struct {
db *C.fdb_handle
}
func Open(filename string) (*Conn, error) {
var db C.fdb_handle
var config C.fdb_config
C.init_fdb_config(&config)
dbname := C.CString(filename)
defer C.free(unsafe.Pointer(dbname))
rv := C.fdb_open(&db, dbname, config)
if rv != 0 {
return nil, errors.New(Errno(rv).Error())
}
/*
if db == nil {
return nil, errors.New("forestdb succeeded without returning a database")
}
*/
return &Conn{&db}, nil
}
func (c *Conn) Put(key, meta, value []byte) error {
var k, m, v unsafe.Pointer
if len(key) != 0 {
k = unsafe.Pointer(&key[0])
}
if len(meta) != 0 {
m = unsafe.Pointer(&meta[0])
}
if len(value) != 0 {
v = unsafe.Pointer(&value[0])
}
lenk := len(key)
lenm := len(meta)
lenv := len(value)
var doc *C.fdb_doc
C.fdb_doc_create(&doc,
k, C.size_t(lenk), m, C.size_t(lenm), v, C.size_t(lenv))
defer C.fdb_doc_free(doc)
rv := C.fdb_set(c.db, doc)
if rv != 0 {
return errors.New(Errno(rv).Error())
}
return nil
}
func (c *Conn) Get(key []byte) ([]byte, error) {
var k unsafe.Pointer
if len(key) != 0 {
k = unsafe.Pointer(&key[0])
}
lenk := len(key)
var doc *C.fdb_doc
C.fdb_doc_create(&doc, k, C.size_t(lenk), nil, C.size_t(0), nil, C.size_t(0))
defer C.fdb_doc_free(doc)
rv := C.fdb_get(c.db, doc)
if rv != 0 {
return nil, errors.New(Errno(rv).Error())
}
value := doc.body
vallen := doc.bodylen
return C.GoBytes(unsafe.Pointer(value), C.int(vallen)), nil
}
func (c *Conn) Delete(key []byte) error {
var k unsafe.Pointer
if len(key) != 0 {
k = unsafe.Pointer(&key[0])
}
lenk := len(key)
var doc *C.fdb_doc
C.fdb_doc_create(&doc, k, C.size_t(lenk), nil, C.size_t(0), nil, C.size_t(0))
defer C.fdb_doc_free(doc)
rv := C.fdb_set(c.db, doc)
if rv != 0 {
return errors.New(Errno(rv).Error())
}
return nil
}
func (c *Conn) Compact(newfilename string) error {
f := C.CString(newfilename)
defer C.free(unsafe.Pointer(f))
rv := C.fdb_compact(c.db, f)
if rv != 0 {
return errors.New(Errno(rv).Error())
}
return nil
}
func (c *Conn) Commit() error {
rv := C.fdb_commit(c.db)
if rv != 0 {
return errors.New(Errno(rv).Error())
}
return nil
}
func (c *Conn) Close() error {
rv := C.fdb_close(c.db)
if rv != 0 {
return errors.New(Errno(rv).Error())
}
return nil
}
func Shutdown() error {
rv := C.fdb_shutdown()
if rv != 0 {
return errors.New(Errno(rv).Error())
}
return nil
}