-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.dats
116 lines (85 loc) · 3.08 KB
/
redis.dats
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
#include "share/atspre_staload.hats"
staload "./redis.sats"
staload "./redis.hats"
#define ATS_DYNLOADFLAG 0
(************************************
Exn Versions
************************************)
exception RuntimeException
implement redisConnectExn (ip, port) = let
val c = redisConnect (ip, port)
val _ = assertloc (redisContextIsNotNull c)
in
c
end
%{
redisReply* redisCommandExn(redisContext* c, char* fmt, ...) {
va_list vl;
va_start(vl, fmt);
redisReply* r = (redisReply*)redisvCommand(c, fmt, vl);
va_end(vl);
if (r != NULL) return r;
exit(-1);
}
%}
(************************************
Handle redisContext struct
************************************)
%{
int _redisContextErr(redisContext* c) { return c->err; }
char* _redisContextErrStr(redisContext* c) { return c->errstr; }
%}
implement redisContextErr (c) = let
val err = $extfcall(int, "_redisContextErr", $UNSAFE.castvwtp1{ptr} c)
in
case- err of
| _ when err = REDIS_ERR_IO => RedisErrIo
| _ when err = REDIS_ERR_EOF => RedisErrEof
| _ when err = REDIS_ERR_PROTOCOL => RedisErrProtocol
| _ when err = REDIS_ERR_OOM => RedisErrOom
| _ when err = REDIS_ERR_OTHER => RedisErrOther
end
implement redisContextErrStr (c) =
$extfcall(string, "_redisContextErrStr", $UNSAFE.castvwtp1{ptr} c)
(************************************
Handle redisReply struct
************************************)
%{
int _redisReplyGetType(redisReply* reply) { return reply->type; }
long long _redisReplyGetInteger(redisReply* reply) { return reply->integer; }
char* _redisReplyGetStr(redisReply* reply) { return reply->str; }
size_t _redisReplyGetElements(redisReply* reply) { return reply->elements; }
struct redisReply ** _redisReplyGetElement(redisReply* reply) { return reply->element; }
%}
implement
redisReplyGetType (reply) = let
val x = $extfcall(int, "_redisReplyGetType", $UNSAFE.castvwtp1{ptr} reply)
in
case- x of
| _ when x = REDIS_REPLY_STRING => RedisReplyString
| _ when x = REDIS_REPLY_ARRAY => RedisReplyArray
| _ when x = REDIS_REPLY_INTEGER => RedisReplyInteger
| _ when x = REDIS_REPLY_NIL => RedisReplyNil
| _ when x = REDIS_REPLY_STATUS => RedisReplyStatus
| _ when x = REDIS_REPLY_ERROR => RedisReplyError
end
implement
redisReplyGetInteger (reply) =
$extfcall(llint, "_redisReplyGetInteger", $UNSAFE.castvwtp1{ptr} reply)
implement
redisReplyGetStr (reply) =
$extfcall(string, "_redisReplyGetStr", $UNSAFE.castvwtp1{ptr} reply)
implement
redisReplyGetElement (reply) = let
val [n:int] len = $extfcall([n:int] int(n), "_redisReplyGetElements", $UNSAFE.castvwtp1{ptr} reply)
val ptr = $extfcall(arrayref(redisReply,n), "_redisReplyGetElement", $UNSAFE.castvwtp1{ptr} reply)
in
(len, ptr)
end
(************************************
Handle Pointer Nill
************************************)
%{
int pointerIsNull (void *ptr) { return ptr == NULL; }
int pointerIsNotNull (void *ptr) { return ptr != NULL; }
%}