This repository has been archived by the owner on Dec 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
unqlite.go
419 lines (360 loc) · 9.24 KB
/
unqlite.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package unqlitego
/*
#cgo linux CFLAGS: -DUNQLITE_ENABLE_THREADS=1 -Wno-unused-but-set-variable
#cgo darwin CFLAGS: -DUNQLITE_ENABLE_THREADS=1
#cgo windows CFLAGS: -DUNQLITE_ENABLE_THREADS=1
#include "./unqlite.h"
#include <stdlib.h>
*/
import "C"
import (
"fmt"
"runtime"
"unsafe"
)
// UnQLiteError ... standard error for this module
type UnQLiteError int
func (e UnQLiteError) Error() string {
s := errString[e]
if s == "" {
return fmt.Sprintf("errno %d", int(e))
}
return s
}
var errString = map[UnQLiteError]string{
C.UNQLITE_LOCKERR: "Locking protocol error",
C.UNQLITE_READ_ONLY: "Read only Key/Value storage engine",
C.UNQLITE_CANTOPEN: "Unable to open the database file",
C.UNQLITE_FULL: "Full database",
C.UNQLITE_VM_ERR: "Virtual machine error",
C.UNQLITE_COMPILE_ERR: "Compilation error",
C.UNQLITE_DONE: "Operation done", // Not an error.
C.UNQLITE_CORRUPT: "Corrupt pointer",
C.UNQLITE_NOOP: "No such method",
C.UNQLITE_PERM: "Permission error",
C.UNQLITE_EOF: "End Of Input",
C.UNQLITE_NOTIMPLEMENTED: "Method not implemented by the underlying Key/Value storage engine",
C.UNQLITE_BUSY: "The database file is locked",
C.UNQLITE_UNKNOWN: "Unknown configuration option",
C.UNQLITE_EXISTS: "Record exists",
C.UNQLITE_ABORT: "Another thread have released this instance",
C.UNQLITE_INVALID: "Invalid parameter",
C.UNQLITE_LIMIT: "Database limit reached",
C.UNQLITE_NOTFOUND: "No such record",
C.UNQLITE_LOCKED: "Forbidden Operation",
C.UNQLITE_EMPTY: "Empty record",
C.UNQLITE_IOERR: "IO error",
C.UNQLITE_NOMEM: "Out of memory",
}
// Database ...
type Database struct {
handle *C.unqlite
}
// Cursor ...
type Cursor struct {
parent *Database
handle *C.unqlite_kv_cursor
}
func init() {
C.unqlite_lib_init()
if !IsThreadSafe() {
panic("unqlite library was not compiled for thread-safe option UNQLITE_ENABLE_THREADS=1")
}
}
// NewDatabase ...
func NewDatabase(filename string) (db *Database, err error) {
db = &Database{}
name := C.CString(filename)
defer C.free(unsafe.Pointer(name))
res := C.unqlite_open(&db.handle, name, C.UNQLITE_OPEN_CREATE)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
if db.handle != nil {
runtime.SetFinalizer(db, (*Database).Close)
}
return
}
// Close ...
func (db *Database) Close() (err error) {
if db.handle != nil {
res := C.unqlite_close(db.handle)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
db.handle = nil
}
return
}
// Store ...
func (db *Database) Store(key, value []byte) (err error) {
var k, v unsafe.Pointer
if len(key) > 0 {
k = unsafe.Pointer(&key[0])
}
if len(value) > 0 {
v = unsafe.Pointer(&value[0])
}
res := C.unqlite_kv_store(db.handle,
k, C.int(len(key)),
v, C.unqlite_int64(len(value)))
if res == C.UNQLITE_OK {
return nil
}
return UnQLiteError(res)
}
// Append ...
func (db *Database) Append(key, value []byte) (err error) {
var k, v unsafe.Pointer
if len(key) > 0 {
k = unsafe.Pointer(&key[0])
}
if len(value) > 0 {
v = unsafe.Pointer(&value[0])
}
res := C.unqlite_kv_append(db.handle,
k, C.int(len(key)),
v, C.unqlite_int64(len(value)))
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}
// Fetch ...
func (db *Database) Fetch(key []byte) (value []byte, err error) {
var k unsafe.Pointer
if len(key) > 0 {
k = unsafe.Pointer(&key[0])
}
var n C.unqlite_int64
res := C.unqlite_kv_fetch(db.handle, k, C.int(len(key)), nil, &n)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
return
}
value = make([]byte, int(n))
res = C.unqlite_kv_fetch(db.handle, k, C.int(len(key)), unsafe.Pointer(&value[0]), &n)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}
// Delete ...
func (db *Database) Delete(key []byte) (err error) {
var k unsafe.Pointer
if len(key) > 0 {
k = unsafe.Pointer(&key[0])
}
res := C.unqlite_kv_delete(db.handle, k, C.int(len(key)))
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}
// Begin ...
func (db *Database) Begin() (err error) {
res := C.unqlite_begin(db.handle)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}
// Commit ...
func (db *Database) Commit() (err error) {
res := C.unqlite_commit(db.handle)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}
// Rollback ...
func (db *Database) Rollback() (err error) {
res := C.unqlite_rollback(db.handle)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}
// NewCursor ...
func (db *Database) NewCursor() (cursor *Cursor, err error) {
cursor = &Cursor{parent: db}
res := C.unqlite_kv_cursor_init(db.handle, &cursor.handle)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
runtime.SetFinalizer(cursor, (*Cursor).Close)
return
}
// Close ...
func (curs *Cursor) Close() (err error) {
if curs.parent.handle != nil && curs.handle != nil {
res := C.unqlite_kv_cursor_release(curs.parent.handle, curs.handle)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
curs.handle = nil
}
return
}
// Seek ...
func (curs *Cursor) Seek(key []byte) (err error) {
var k unsafe.Pointer
if len(key) > 0 {
k = unsafe.Pointer(&key[0])
}
res := C.unqlite_kv_cursor_seek(curs.handle, k, C.int(len(key)), C.UNQLITE_CURSOR_MATCH_EXACT)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}
// SeekLE ...
func (curs *Cursor) SeekLE(key []byte) (err error) {
var k unsafe.Pointer
if len(key) > 0 {
k = unsafe.Pointer(&key[0])
}
res := C.unqlite_kv_cursor_seek(curs.handle, k, C.int(len(key)), C.UNQLITE_CURSOR_MATCH_LE)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}
// SeekGE ...
func (curs *Cursor) SeekGE(key []byte) (err error) {
var k unsafe.Pointer
if len(key) > 0 {
k = unsafe.Pointer(&key[0])
}
res := C.unqlite_kv_cursor_seek(curs.handle, k, C.int(len(key)), C.UNQLITE_CURSOR_MATCH_GE)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}
// First ...
func (curs *Cursor) First() (err error) {
res := C.unqlite_kv_cursor_first_entry(curs.handle)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}
// Last ...
func (curs *Cursor) Last() (err error) {
res := C.unqlite_kv_cursor_last_entry(curs.handle)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}
// IsValid ...
func (curs *Cursor) IsValid() (ok bool) {
return C.unqlite_kv_cursor_valid_entry(curs.handle) == 1
}
// Next ...
func (curs *Cursor) Next() (err error) {
res := C.unqlite_kv_cursor_next_entry(curs.handle)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}
// Prev ...
func (curs *Cursor) Prev() (err error) {
res := C.unqlite_kv_cursor_prev_entry(curs.handle)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}
// Delete ...
func (curs *Cursor) Delete() (err error) {
res := C.unqlite_kv_cursor_delete_entry(curs.handle)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}
// Reset ...
func (curs *Cursor) Reset() (err error) {
res := C.unqlite_kv_cursor_reset(curs.handle)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}
// Key ...
func (curs *Cursor) Key() (key []byte, err error) {
var n C.int
res := C.unqlite_kv_cursor_key(curs.handle, nil, &n)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
return
}
key = make([]byte, int(n))
res = C.unqlite_kv_cursor_key(curs.handle, unsafe.Pointer(&key[0]), &n)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}
// Value ...
func (curs *Cursor) Value() (value []byte, err error) {
var n C.unqlite_int64
res := C.unqlite_kv_cursor_data(curs.handle, nil, &n)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
return
}
value = make([]byte, int(n))
res = C.unqlite_kv_cursor_data(curs.handle, unsafe.Pointer(&value[0]), &n)
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}
// Shutdown ...
func Shutdown() (err error) {
res := C.unqlite_lib_shutdown()
if res != C.UNQLITE_OK {
err = UnQLiteError(res)
}
return
}
// IsThreadSafe ...
func IsThreadSafe() bool {
return C.unqlite_lib_is_threadsafe() == 1
}
// Version ...
func Version() string {
return C.GoString(C.unqlite_lib_version())
}
// Signature ...
func Signature() string {
return C.GoString(C.unqlite_lib_signature())
}
// Ident ...
func Ident() string {
return C.GoString(C.unqlite_lib_ident())
}
// Copyright ...
func Copyright() string {
return C.GoString(C.unqlite_lib_copyright())
}
/* TODO: implement
// Database Engine Handle
int unqlite_config(unqlite *pDb,int nOp,...);
// Key/Value (KV) Store Interfaces
int unqlite_kv_fetch_callback(unqlite *pDb,const void *pKey,
int nKeyLen,int (*xConsumer)(const void *,unsigned int,void *),void *pUserData);
int unqlite_kv_config(unqlite *pDb,int iOp,...);
// Cursor Iterator Interfaces
int unqlite_kv_cursor_key_callback(unqlite_kv_cursor *pCursor,int (*xConsumer)(const void *,unsigned int,void *),void *pUserData);
int unqlite_kv_cursor_data_callback(unqlite_kv_cursor *pCursor,int (*xConsumer)(const void *,unsigned int,void *),void *pUserData);
// Utility interfaces
int unqlite_util_load_mmaped_file(const char *zFile,void **ppMap,unqlite_int64 *pFileSize);
int unqlite_util_release_mmaped_file(void *pMap,unqlite_int64 iFileSize);
// Global Library Management Interfaces
int unqlite_lib_config(int nConfigOp,...);
*/