-
Notifications
You must be signed in to change notification settings - Fork 1
/
rlite.go
98 lines (87 loc) · 2.46 KB
/
rlite.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
package rlite
/*
#cgo CFLAGS: -std=gnu99
#cgo LDFLAGS: -lhirlite -lm
#include <hirlite/hirlite.h>
// I don't know how to cast in go
static inline rliteReply* ptor_pos(struct rliteReply **p, size_t pos) {
rliteReply **element = p;
return element[pos];
}
static inline rliteReply* ptor(void *p) {
return (rliteReply*)p;
}
*/
import "C"
import (
"errors"
"fmt"
"reflect"
"unsafe"
)
func StringToBytes(s string) []byte {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := reflect.SliceHeader{sh.Data, sh.Len, 0}
return *(*[]byte)(unsafe.Pointer(&bh))
}
type Conn struct {
db *C.rliteContext
}
func cStr(s string) *C.char {
h := (*reflect.StringHeader)(unsafe.Pointer(&s))
return (*C.char)(unsafe.Pointer(h.Data))
}
func Open(name string) (*Conn, error) {
name += "\x00"
db := C.rliteConnect(cStr(name), 0)
if db == nil {
return nil, errors.New("Unable to open database")
}
return &Conn{db: db}, nil
}
func Close(c *Conn) {
C.rliteFree(c.db)
}
func GetReply(reply *C.rliteReply) (interface{}, error) {
if reply._type == C.RLITE_REPLY_ERROR {
return nil, errors.New(C.GoStringN(reply.str, reply.len))
}
if reply._type == C.RLITE_REPLY_STRING || reply._type == C.RLITE_REPLY_STATUS {
return C.GoStringN(reply.str, reply.len), nil
}
if reply._type == C.RLITE_REPLY_INTEGER {
return int(reply.integer), nil
}
if reply._type == C.RLITE_REPLY_NIL {
return nil, nil
}
if reply._type == C.RLITE_REPLY_ARRAY {
arr := make([]interface{}, reply.elements)
for i := C.size_t(0); i < reply.elements; i++ {
// TODO: what if the array has an error?
arr[i], _ = GetReply(C.ptor_pos(reply.element, i))
}
return arr, nil
}
return nil, errors.New(fmt.Sprintf("Unknown type %d", reply._type))
}
func Command(c *Conn, list []string) (interface{}, error) {
var b *C.char
argvSize := unsafe.Sizeof(b)
argv := C.malloc(C.size_t(len(list)) * C.size_t(argvSize))
defer C.free(argv)
var d *C.size_t
argvlenSize := unsafe.Sizeof(d)
argvlen := C.malloc(C.size_t(len(list)) * C.size_t(argvlenSize))
for i := 0; i < len(list); i++ {
b := StringToBytes(list[i])
element := (**C.char)(unsafe.Pointer(uintptr(argv) + uintptr(i)*argvSize))
*element = (*C.char)(unsafe.Pointer(&b[0]))
elementlen := (*C.size_t)(unsafe.Pointer(uintptr(argvlen) + uintptr(i)*argvSize))
*elementlen = C.size_t(len(b))
}
p := C.rliteCommandArgv(c.db, C.int(len(list)), (**C.char)(argv), (*C.size_t)(argvlen))
r := C.ptor(p)
defer C.rliteFreeReplyObject(p)
return GetReply(r)
}