-
Notifications
You must be signed in to change notification settings - Fork 2
/
wrappers.go
125 lines (104 loc) · 3 KB
/
wrappers.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
package sophia
import (
"unsafe"
)
/*
#cgo CFLAGS: -I.
#cgo LDFLAGS: -lrt
#include <sophia.h>
extern void *sp_env(void);
extern void *sp_document(void*);
extern int sp_setstring(void*, const char*, const void*, int);
extern int sp_setint(void*, const char*, int64_t);
extern void *sp_getobject(void*, const char*);
extern void *sp_getstring(void*, const char*, int*);
extern int64_t sp_getint(void*, const char*);
extern int sp_open(void*);
extern int sp_destroy(void*);
extern int sp_set(void*, void*);
extern int sp_upsert(void*, void*);
extern int sp_delete(void*, void*);
extern void *sp_get(void*, void*);
extern void *sp_cursor(void*);
extern void *sp_begin(void*);
extern int sp_prepare(void*);
extern int sp_commit(void*);
*/
import "C"
// spDestroy wrapper for sp_destroy
// destroys C sophia object
func spDestroy(p unsafe.Pointer) bool {
return C.sp_destroy(p) == 0
}
// spSetString wrapper for sp_setstring for common cases
func spSetString(obj unsafe.Pointer, path *C.char, val unsafe.Pointer, size int) bool {
return C.sp_setstring(obj, path, val, C.int(size)) == 0
}
func spGetString(obj unsafe.Pointer, path *C.char, size *int) unsafe.Pointer {
cSize := C.int(*size)
ptr := C.sp_getstring(obj, path, &cSize)
*size = int(cSize)
return ptr
}
// spSetInt wrapper for sp_setint
func spSetInt(obj unsafe.Pointer, path *C.char, val int64) bool {
return C.sp_setint(obj, path, C.int64_t(val)) == 0
}
// spGetInt wrapper for sp_getint
func spGetInt(obj unsafe.Pointer, path *C.char) int64 {
ptr := C.sp_getint(obj, path)
return *(*int64)(unsafe.Pointer(&ptr))
}
// spGetObject wrapper for sp_getobject
func spGetObject(obj unsafe.Pointer, path *C.char) unsafe.Pointer {
return C.sp_getobject(obj, path)
}
// spOpen wrapper for sp_open
func spOpen(ptr unsafe.Pointer) bool {
return C.sp_open(ptr) == 0
}
// spEnv wrapper for sp_env
func spEnv() unsafe.Pointer {
return C.sp_env()
}
// spCursor wrapper for sp_cursor
func spCursor(ptr unsafe.Pointer) unsafe.Pointer {
return C.sp_cursor(ptr)
}
// spDocument wrapper for sp_document
func spDocument(ptr unsafe.Pointer) unsafe.Pointer {
return C.sp_document(ptr)
}
// spGet wrapper for sp_get
func spGet(obj, doc unsafe.Pointer) unsafe.Pointer {
return C.sp_get(obj, doc)
}
// spSet wrapper for sp_set
func spSet(obj, doc unsafe.Pointer) bool {
return C.sp_set(obj, doc) == 0
}
// spUpsert wrapper for sp_upsert
func spUpsert(obj, doc unsafe.Pointer) bool {
return C.sp_upsert(obj, doc) == 0
}
// spDelete wrapper for sp_delete
func spDelete(obj, doc unsafe.Pointer) bool {
return C.sp_delete(obj, doc) == 0
}
// spCommit wrapper for sp_commit
func spCommit(tx unsafe.Pointer) int {
return int(C.sp_commit(tx))
}
// spBegin wrapper for sp_begin
func spBegin(env unsafe.Pointer) unsafe.Pointer {
return C.sp_begin(env)
}
func free(ptr unsafe.Pointer) {
C.free(ptr)
}
func goString(ptr unsafe.Pointer) string {
return C.GoString((*C.char)(ptr))
}
func cString(str string) *C.char {
return C.CString(str)
}