forked from gfphoenix/unqlite
/
callback.go
46 lines (44 loc) · 1.5 KB
/
callback.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
package unqlite
//#cgo CFLAGS: -I.
//#include <unqlite.h>
//#include <string.h>
//int unqlite_kv_fetch_callback_CB(unqlite *pDb, const void *key, int nKeyLen, void *fn);
//int unqlite_kv_cursor_key_callback_CB(unqlite_kv_cursor *c, void *fn);
//int unqlite_kv_cursor_data_callback_CB(unqlite_kv_cursor *c, void *fn);
import "C"
import (
"unsafe"
)
// use closure instead of another parameter for func
func (u *Unqlite)KvFetchCallback(key []byte, xConsumer func(data[]byte)) error{
i := C.unqlite_kv_fetch_callback_CB(u.db, unsafe.Pointer(&key[0]), C.int(len(key)), unsafe.Pointer(&xConsumer))
return code2Error(i)
}
//export goKvFetchCallback
func goKvFetchCallback(a unsafe.Pointer, b uint, fn unsafe.Pointer) int {
if b==0 {
return 0
}
data := make([]byte, b)
C.memcpy(unsafe.Pointer(&data[0]), a, C.size_t(b))
Fn := *(*func([]byte)int)(fn)
return Fn(data)
}
func (c *Cursor)KeyCallback(xConsumer func(value []byte)int) error{
i := C.unqlite_kv_cursor_key_callback_CB(c.c, unsafe.Pointer(&xConsumer))
return code2Error(i)
}
func (c *Cursor)DataCallback(xConsumer func(value []byte)int)error {
i := C.unqlite_kv_cursor_data_callback_CB(c.c, unsafe.Pointer(&xConsumer))
return code2Error(i)
}
//export goKvCursorCallback
func goKvCursorCallback(data unsafe.Pointer, b uint, fn unsafe.Pointer) int{
if b==0 {
return 0
}
pdata := make([]byte, b)
C.memcpy(unsafe.Pointer(&pdata[0]), data, C.size_t(b))
f := *(*func([]byte)int)(fn)
return f(pdata)
}