-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodule.jai
194 lines (153 loc) · 5.39 KB
/
module.jai
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
// jai-redis
// Redis bindings for Jai
// Smári McCarthy <[email protected]>
#load "resp.jai";
Redis :: struct {
socket: Socket;
host: string;
port: u16;
}
init :: (redis: *Redis, host: string, port: u16, user: string = "default", password: string = "") -> bool {
socket_init();
redis.host = host;
redis.port = port;
redis.socket = socket(AF_INET, .SOCK_STREAM, .TCP);
if redis.socket < 0 {
log("Failed to create socket for redis.\n");
return false;
}
servaddr : sockaddr_in;
memset(*servaddr, 0, size_of(sockaddr_in));
// assign IP, PORT
server_address.sin_family = AF_INET;
#if OS == .WINDOWS {
server_address.sin_addr.S_un.S_addr = htonl(address);
} else {
server_address.sin_addr.s_addr = htonl(address);
// connect the client socket to server socket
if (connect(redis.socket, xx *server_address, xx size_of(sockaddr_in)) != 0) {
log("Failed to connect to redis server (%:%)\n", host, port);
return false;
}
resp : RESPMessage;
if password.count > 0 {
resp = redis_send(redis, "HELLO", 2, "AUTH", user, password);
} else {
resp = redis_send(redis, "HELLO", 2);
}
if resp.type == .ERROR {
log("Redis handshake failed (%:%) - '%'!\n", host, port, resp.value.s, flags=.ERROR);
return false;
}
return true;
}
deinit :: (redis: *Redis) {
close_and_reset(*redis.socket);
}
redis_select_db :: (redis: *Redis, db: int) -> bool {
ret, suc := redis_send(redis, "SELECT", db);
return suc;
}
redis_send :: (redis: *Redis, msg: .. Any) -> RESPMessage, bool {
write(redis.socket, tprint("*%\r\n", msg.count));
for a:msg {
if a.type.type == {
case .INTEGER;
s := tprint("%", << cast(*int)a.value_pointer);
write(redis.socket, tprint("$%\r\n", s.count));
write(redis.socket, tprint("%\r\n", s));
case .STRING;
s := << cast(*string)a.value_pointer;
write(redis.socket, tprint("$%\r\n", s.count));
write(redis.socket, s);
write(redis.socket, "\r\n");
}
}
resp : RESPMessage;
resp = resp_parse(redis.socket);
return resp, false;
}
redis_pop :: (redis: *Redis, list: string, blocking: bool=false) -> string, bool {
ret, suc := redis_send(redis, ifx blocking then "BLPOP" else "LPOP", list, 60);
if ret.type == .ERROR return ret.value.s, false;
if ret.type == .NULL return "", false;
if ret.type == .ARRAY {
if ret.value.a.count > 1 {
// index 0 is the name of the list we retrieved from...
result := ret.value.a[1].value.s;
return result, true;
}
}
return "", false;
}
redis_push :: (redis: *Redis, list: string, value: Any) -> string, bool {
ret, suc := redis_send(redis, "LPUSH", list, value);
if ret.type == .ERROR return ret.value.s, false;
if ret.type == .NULL return "", false;
return ret.value.s, true;
}
redis_info :: (redis: *Redis) {
res := redis_send(redis, "INFO");
}
redis_set :: (redis: *Redis, key: string, value: Any, ex:=0, px:=0, nx:=false, _xx:=false, getset:=false) -> bool, RESPMessage {
/* Options:
* [EX seconds|PX milliseconds|KEEPTTL] [NX|XX] [GET]
* See https://redis.io/commands/set
*/
ret: RESPMessage;
suc: bool;
opts: [..]Any;
if getset array_add(*opts, "GETSET");
else array_add(*opts, "SET");
array_add(*opts, key);
array_add(*opts, value);
if ex {
array_add(*opts, "EX");
array_add(*opts, ex);
} else if px {
array_add(*opts, "PX");
array_add(*opts, ex);
}
if nx array_add(*opts, "NX");
else if _xx array_add(*opts, "XX");
ret, suc = redis_send(redis, ..opts);
if ret.type == .SIMPLE_STRING && ret.value.s == "OK" return true, ret;
return false, ret;
}
redis_get :: (redis: *Redis, key: string) -> string, bool {
ret, suc := redis_send(redis, "GET", key);
if ret.type == .ERROR return ret.value.s, false;
if ret.type == .NULL return "", false;
return ret.value.s, true;
}
redis_getset :: (redis: *Redis, key: string, value: Any, ex:=0, px:=0, nx:=false, _xx:=false) -> string, bool {
suc, ret := redis_set(redis, key, value, ex, px, nx, _xx, getset=true);
if ret.type == .ERROR return ret.value.s, false;
if ret.type == .NULL return "", false;
return ret.value.s, true;
}
redis_strlen :: (redis: *Redis, key: string) -> s64 {
ret, suc := redis_send(redis, "STRLEN", key);
return ret.value.i;
}
redis_append :: (redis: *Redis, key: string, append: string) -> bool {
ret, suc := redis_send(redis, "APPEND", key, append);
return suc;
}
redis_getrange :: (redis: *Redis, key: string, start: int, end: int) -> string, bool {
ret, suc := redis_send(redis, "GETRANGE", key, start, end);
return ret.value.s, suc;
}
redis_setrange :: (redis: *Redis, key: string, start: int, value: Any) -> bool {
ret, suc := redis_send(redis, "SETRANGE", key, start, value);
return suc;
}
#scope_file
#import "Socket";
write :: inline (fd: Socket, msg: string) -> s64 {
return send(fd, cast(*void) msg.data, xx msg.count, 0);
}
write :: inline (fd: Socket, buf: string, args: ..Any) -> s64 {
tmp := tprint(buf, ..args);
return send(fd, tmp.data, xx tmp.count, 0);
}