forked from nobonobo/unqlitego
-
Notifications
You must be signed in to change notification settings - Fork 2
/
unqlite.go
579 lines (489 loc) · 13.4 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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
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 "./wrappers.h"
#include <stdlib.h>
*/
import "C"
import (
"fmt"
"runtime"
"unsafe"
)
// UnQLiteError ... standard error for this module
type GlobaLError string
func (s GlobaLError) Error() string {
return string(s)
}
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
}
type VM struct {
vm *C.unqlite_vm
}
type Unqlite_value struct {
unqlite_value *C.unqlite_value
}
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
}
func NewVM() (vm *VM) {
vm = &VM{}
return
}
func (db *Database,) Unqlite_compile(jx9_script string,vm *VM) (error,string ){
res := C.unqlite_compile(db.handle,C.CString(jx9_script),C.int(len(jx9_script)),&vm.vm)
if res != C.UNQLITE_OK{
if res == C.UNQLITE_COMPILE_ERR{
err:=UnQLiteError(res)
error_log:=new(C.char)
err_msg:=C.extract_unqlite_log_error(db.handle,error_log)
g_err_msg:=C.GoString(err_msg)
//C.free(unsafe.Pointer(err_msg))
return err,g_err_msg
}
}
return nil,""
}
func (vm VM) Free(){
C.free(unsafe.Pointer(vm.vm))
}
func (vm *VM)Unqlite_vm_extract_variable(variable_name string) (*Unqlite_value){
/*This function must be used with extra causion since it might return
a variable from the type of *C.unqlite_value ,be sure to free this pointer
In case of no such variable of out-of-memory issue NULL is returned
In case where the VM have not been executed the return value will be C.int(0)
In case of unqlite is compiled with threads support and the vm.vm instance have been releases
by a different thread 0 will be returned
For summary:
-----------
*) 0 or NULL = Bad
*) *C.unqlite_value = Good */
c_variable_name := C.CString(variable_name)
defer C.free(unsafe.Pointer(c_variable_name))
unqlite_value:=C.unqlite_vm_extract_variable(vm.vm,c_variable_name)
return &Unqlite_value{unqlite_value}
}
func (vm *VM)VM_extract_output() string{
//extract the output from a vm after execution
var len C.int
var buff *C.char
buff=C.extract_vm_output(vm.vm,&len)
q:=C.GoStringN(buff,len)
fmt.Printf("%s",q)
return ""
}
func (vm *VM)VM_exacute() int{
res:=C.unqlite_vm_exec(vm.vm)
return int(res)
}
func unqlite_value_ok(unqlite_value *Unqlite_value)(bool) {
switch unqlite_value.unqlite_value{
case nil:return false//User Data is wrtong
default:return true
}
}
func (vm *VM)Extract_variable_as_int(variable_name string) (int,error){
/*Extract a variable from the VM after if have been executed
If something went wrong return nil
*/
var unqlite_value *Unqlite_value
unqlite_value=vm.Unqlite_vm_extract_variable(variable_name)
if ! unqlite_value_ok(unqlite_value){
return 0,nil
}
res:=int(C.unqlite_value_to_int(unqlite_value.unqlite_value))
return res,GlobaLError("OK")
}
func (vm *VM)Extract_variable_as_string(variable_name string) (string,error){
/*Extract a variable from the VM after if have been executed
If something went wrong return nil
*/
var unqlite_value *Unqlite_value
unqlite_value=vm.Unqlite_vm_extract_variable(variable_name)
if ! unqlite_value_ok(unqlite_value){
return "",nil
}
var plen C.int
c_res:=C.extract_variable_as_string(unqlite_value.unqlite_value,&plen)
res:=C.GoStringN(c_res,plen)
return res,GlobaLError("OK")
}
func (vm *VM)Extract_variable_as_bool(variable_name string) (bool,error){
/*Extract a variable from the VM after if have been executed
If something went wrong return nil
*/
var unqlite_value *Unqlite_value
unqlite_value=vm.Unqlite_vm_extract_variable(variable_name)
if ! unqlite_value_ok(unqlite_value){
return false,nil
}
res:=int(C.unqlite_value_to_bool(unqlite_value.unqlite_value))
return res!=0,GlobaLError("OK")
}
func (vm *VM)Extract_variable_as_int64(variable_name string) (int64,error){
/*Extract a variable from the VM after if have been executed
If something went wrong return nil
*/
var unqlite_value *Unqlite_value
unqlite_value=vm.Unqlite_vm_extract_variable(variable_name)
if ! unqlite_value_ok(unqlite_value){
return 0,nil
}
res:=int64(C.unqlite_value_to_int64(unqlite_value.unqlite_value))
return res,GlobaLError("OK")
}
func (vm *VM)Extract_variable_as_double(variable_name string) (float64,error){
/*Extract a variable from the VM after if have been executed
If something went wrong return nil
*/
var unqlite_value *Unqlite_value
unqlite_value=vm.Unqlite_vm_extract_variable(variable_name)
if ! unqlite_value_ok(unqlite_value){
return 0.0,nil
}
res:=float64(C.unqlite_value_to_double(unqlite_value.unqlite_value))
return res,GlobaLError("OK")
}
// 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,...);
*/