]>
Commit | Line | Data |
---|---|---|
7585836e | 1 | #include "redis.h" |
2 | #include "sha1.h" | |
3 | ||
4 | #include <lua.h> | |
5 | #include <lauxlib.h> | |
6 | #include <lualib.h> | |
7 | ||
532e0f5d | 8 | char *redisProtocolToLuaType_Int(lua_State *lua, char *reply); |
9 | char *redisProtocolToLuaType_Bulk(lua_State *lua, char *reply); | |
10 | char *redisProtocolToLuaType_Status(lua_State *lua, char *reply); | |
3791000f | 11 | char *redisProtocolToLuaType_Error(lua_State *lua, char *reply); |
12 | char *redisProtocolToLuaType_MultiBulk(lua_State *lua, char *reply); | |
532e0f5d | 13 | |
14 | /* Take a Redis reply in the Redis protocol format and convert it into a | |
15 | * Lua type. Thanks to this function, and the introduction of not connected | |
16 | * clients, it is trvial to implement the redis() lua function. | |
17 | * | |
18 | * Basically we take the arguments, execute the Redis command in the context | |
19 | * of a non connected client, then take the generated reply and convert it | |
20 | * into a suitable Lua type. With this trick the scripting feature does not | |
21 | * need the introduction of a full Redis internals API. Basically the script | |
22 | * is like a normal client that bypasses all the slow I/O paths. | |
23 | * | |
24 | * Note: in this function we do not do any sanity check as the reply is | |
25 | * generated by Redis directly. This allows use to go faster. | |
26 | * The reply string can be altered during the parsing as it is discared | |
27 | * after the conversion is completed. | |
28 | * | |
29 | * Errors are returned as a table with a single 'err' field set to the | |
30 | * error string. | |
31 | */ | |
32 | ||
33 | char *redisProtocolToLuaType(lua_State *lua, char* reply) { | |
34 | char *p = reply; | |
35 | ||
36 | switch(*p) { | |
37 | case ':': | |
38 | p = redisProtocolToLuaType_Int(lua,reply); | |
39 | break; | |
40 | case '$': | |
41 | p = redisProtocolToLuaType_Bulk(lua,reply); | |
42 | break; | |
43 | case '+': | |
44 | p = redisProtocolToLuaType_Status(lua,reply); | |
45 | break; | |
3791000f | 46 | case '-': |
47 | p = redisProtocolToLuaType_Error(lua,reply); | |
48 | break; | |
49 | case '*': | |
50 | p = redisProtocolToLuaType_MultiBulk(lua,reply); | |
51 | break; | |
532e0f5d | 52 | } |
53 | return p; | |
54 | } | |
55 | ||
56 | char *redisProtocolToLuaType_Int(lua_State *lua, char *reply) { | |
57 | char *p = strchr(reply+1,'\r'); | |
58 | long long value; | |
59 | ||
60 | string2ll(reply+1,p-reply-1,&value); | |
61 | lua_pushnumber(lua,(lua_Number)value); | |
62 | return p+2; | |
63 | } | |
64 | ||
65 | char *redisProtocolToLuaType_Bulk(lua_State *lua, char *reply) { | |
66 | char *p = strchr(reply+1,'\r'); | |
67 | long long bulklen; | |
68 | ||
69 | string2ll(reply+1,p-reply-1,&bulklen); | |
379789cc | 70 | if (bulklen == -1) { |
532e0f5d | 71 | lua_pushnil(lua); |
72 | return p+2; | |
73 | } else { | |
74 | lua_pushlstring(lua,p+2,bulklen); | |
75 | return p+2+bulklen+2; | |
76 | } | |
77 | } | |
78 | ||
79 | char *redisProtocolToLuaType_Status(lua_State *lua, char *reply) { | |
80 | char *p = strchr(reply+1,'\r'); | |
81 | ||
82 | lua_pushlstring(lua,reply+1,p-reply-1); | |
83 | return p+2; | |
84 | } | |
85 | ||
3791000f | 86 | char *redisProtocolToLuaType_Error(lua_State *lua, char *reply) { |
87 | char *p = strchr(reply+1,'\r'); | |
88 | ||
89 | lua_newtable(lua); | |
90 | lua_pushstring(lua,"err"); | |
91 | lua_pushlstring(lua,reply+1,p-reply-1); | |
92 | lua_settable(lua,-3); | |
93 | return p+2; | |
94 | } | |
95 | ||
96 | char *redisProtocolToLuaType_MultiBulk(lua_State *lua, char *reply) { | |
97 | char *p = strchr(reply+1,'\r'); | |
98 | long long mbulklen; | |
99 | int j = 0; | |
100 | ||
3791000f | 101 | string2ll(reply+1,p-reply-1,&mbulklen); |
102 | p += 2; | |
103 | if (mbulklen == -1) { | |
104 | lua_pushnil(lua); | |
105 | return p; | |
106 | } | |
3791000f | 107 | lua_newtable(lua); |
108 | for (j = 0; j < mbulklen; j++) { | |
10a6da7a | 109 | lua_pushnumber(lua,j+1); |
3791000f | 110 | p = redisProtocolToLuaType(lua,p); |
111 | lua_settable(lua,-3); | |
112 | } | |
113 | return p; | |
114 | } | |
115 | ||
379789cc | 116 | void luaPushError(lua_State *lua, char *error) { |
117 | lua_newtable(lua); | |
118 | lua_pushstring(lua,"err"); | |
119 | lua_pushstring(lua, error); | |
120 | lua_settable(lua,-3); | |
121 | } | |
122 | ||
0f1d64ca | 123 | int luaRedisCommand(lua_State *lua) { |
124 | int j, argc = lua_gettop(lua); | |
125 | struct redisCommand *cmd; | |
126 | robj **argv; | |
127 | redisClient *c = server.lua_client; | |
128 | sds reply; | |
129 | ||
532e0f5d | 130 | /* Build the arguments vector */ |
0f1d64ca | 131 | argv = zmalloc(sizeof(robj*)*argc); |
379789cc | 132 | for (j = 0; j < argc; j++) { |
133 | if (!lua_isstring(lua,j+1)) break; | |
532e0f5d | 134 | argv[j] = createStringObject((char*)lua_tostring(lua,j+1), |
135 | lua_strlen(lua,j+1)); | |
379789cc | 136 | } |
137 | ||
138 | /* Check if one of the arguments passed by the Lua script | |
139 | * is not a string or an integer (lua_isstring() return true for | |
140 | * integers as well). */ | |
141 | if (j != argc) { | |
142 | j--; | |
143 | while (j >= 0) { | |
144 | decrRefCount(argv[j]); | |
145 | j--; | |
146 | } | |
147 | zfree(argv); | |
148 | luaPushError(lua, | |
149 | "Lua redis() command arguments must be strings or integers"); | |
150 | return 1; | |
151 | } | |
0f1d64ca | 152 | |
153 | /* Command lookup */ | |
154 | cmd = lookupCommand(argv[0]->ptr); | |
3791000f | 155 | if (!cmd || ((cmd->arity > 0 && cmd->arity != argc) || |
156 | (argc < -cmd->arity))) | |
157 | { | |
532e0f5d | 158 | for (j = 0; j < argc; j++) decrRefCount(argv[j]); |
0f1d64ca | 159 | zfree(argv); |
3791000f | 160 | if (cmd) |
379789cc | 161 | luaPushError(lua, |
3791000f | 162 | "Wrong number of args calling Redis command From Lua script"); |
163 | else | |
379789cc | 164 | luaPushError(lua,"Unknown Redis command called from Lua script"); |
532e0f5d | 165 | return 1; |
0f1d64ca | 166 | } |
532e0f5d | 167 | |
0f1d64ca | 168 | /* Run the command in the context of a fake client */ |
169 | c->argv = argv; | |
170 | c->argc = argc; | |
171 | cmd->proc(c); | |
172 | ||
173 | /* Convert the result of the Redis command into a suitable Lua type. | |
174 | * The first thing we need is to create a single string from the client | |
175 | * output buffers. */ | |
176 | reply = sdsempty(); | |
177 | if (c->bufpos) { | |
7156f43c | 178 | reply = sdscatlen(reply,c->buf,c->bufpos); |
0f1d64ca | 179 | c->bufpos = 0; |
180 | } | |
181 | while(listLength(c->reply)) { | |
182 | robj *o = listNodeValue(listFirst(c->reply)); | |
183 | ||
184 | sdscatlen(reply,o->ptr,sdslen(o->ptr)); | |
185 | listDelNode(c->reply,listFirst(c->reply)); | |
186 | } | |
532e0f5d | 187 | redisProtocolToLuaType(lua,reply); |
7156f43c | 188 | sdsfree(reply); |
0f1d64ca | 189 | |
190 | /* Clean up. Command code may have changed argv/argc so we use the | |
191 | * argv/argc of the client instead of the local variables. */ | |
192 | for (j = 0; j < c->argc; j++) | |
193 | decrRefCount(c->argv[j]); | |
194 | zfree(c->argv); | |
195 | ||
196 | return 1; | |
197 | } | |
198 | ||
7585836e | 199 | void scriptingInit(void) { |
200 | lua_State *lua = lua_open(); | |
201 | luaL_openlibs(lua); | |
0f1d64ca | 202 | |
203 | /* Register the 'r' command */ | |
204 | lua_pushcfunction(lua,luaRedisCommand); | |
00b7541b | 205 | lua_setglobal(lua,"redis"); |
0f1d64ca | 206 | |
207 | /* Create the (non connected) client that we use to execute Redis commands | |
208 | * inside the Lua interpreter */ | |
209 | server.lua_client = createClient(-1); | |
7156f43c | 210 | server.lua_client->flags |= REDIS_LUA_CLIENT; |
0f1d64ca | 211 | |
7585836e | 212 | server.lua = lua; |
213 | } | |
214 | ||
215 | /* Hash the scripit into a SHA1 digest. We use this as Lua function name. | |
216 | * Digest should point to a 41 bytes buffer: 40 for SHA1 converted into an | |
217 | * hexadecimal number, plus 1 byte for null term. */ | |
218 | void hashScript(char *digest, char *script, size_t len) { | |
219 | SHA1_CTX ctx; | |
220 | unsigned char hash[20]; | |
221 | char *cset = "0123456789abcdef"; | |
222 | int j; | |
223 | ||
224 | SHA1Init(&ctx); | |
225 | SHA1Update(&ctx,(unsigned char*)script,len); | |
226 | SHA1Final(hash,&ctx); | |
227 | ||
228 | for (j = 0; j < 20; j++) { | |
229 | digest[j*2] = cset[((hash[j]&0xF0)>>4)]; | |
230 | digest[j*2+1] = cset[(hash[j]&0xF)]; | |
231 | } | |
232 | digest[40] = '\0'; | |
233 | } | |
234 | ||
235 | void luaReplyToRedisReply(redisClient *c, lua_State *lua) { | |
236 | int t = lua_type(lua,1); | |
237 | ||
238 | switch(t) { | |
239 | case LUA_TSTRING: | |
240 | addReplyBulkCBuffer(c,(char*)lua_tostring(lua,1),lua_strlen(lua,1)); | |
241 | break; | |
242 | case LUA_TBOOLEAN: | |
243 | addReply(c,lua_toboolean(lua,1) ? shared.cone : shared.czero); | |
244 | break; | |
245 | case LUA_TNUMBER: | |
246 | addReplyLongLong(c,(long long)lua_tonumber(lua,1)); | |
247 | break; | |
532e0f5d | 248 | case LUA_TTABLE: |
249 | /* We need to check if it is an array or an error. | |
250 | * Error are returned as a single element table with 'err' field. */ | |
251 | lua_pushstring(lua,"err"); | |
252 | lua_gettable(lua,-2); | |
253 | t = lua_type(lua,-1); | |
254 | if (t == LUA_TSTRING) { | |
3791000f | 255 | addReplySds(c,sdscatprintf(sdsempty(), |
256 | "-%s\r\n",(char*)lua_tostring(lua,-1))); | |
532e0f5d | 257 | lua_pop(lua,1); |
258 | } else { | |
259 | void *replylen = addDeferredMultiBulkLength(c); | |
260 | int j = 1, mbulklen = 0; | |
261 | ||
262 | lua_pop(lua,1); /* Discard the 'err' field value we popped */ | |
263 | while(1) { | |
264 | lua_pushnumber(lua,j++); | |
265 | lua_gettable(lua,-2); | |
266 | t = lua_type(lua,-1); | |
267 | if (t == LUA_TNIL) { | |
268 | lua_pop(lua,1); | |
269 | break; | |
270 | } else if (t == LUA_TSTRING) { | |
271 | size_t len; | |
272 | char *s = (char*) lua_tolstring(lua,-1,&len); | |
273 | ||
274 | addReplyBulkCBuffer(c,s,len); | |
275 | mbulklen++; | |
276 | } else if (t == LUA_TNUMBER) { | |
277 | addReplyLongLong(c,(long long)lua_tonumber(lua,-1)); | |
278 | mbulklen++; | |
279 | } | |
280 | lua_pop(lua,1); | |
281 | } | |
282 | setDeferredMultiBulkLength(c,replylen,mbulklen); | |
283 | } | |
284 | break; | |
7585836e | 285 | default: |
286 | addReply(c,shared.nullbulk); | |
287 | } | |
288 | lua_pop(lua,1); | |
289 | } | |
290 | ||
4ae5b5e1 | 291 | /* Set an array of Redis String Objects as a Lua array (table) stored into a |
292 | * global variable. */ | |
293 | void luaSetGlobalArray(lua_State *lua, char *var, robj **elev, int elec) { | |
294 | int j; | |
295 | ||
296 | lua_newtable(lua); | |
297 | for (j = 0; j < elec; j++) { | |
298 | lua_pushlstring(lua,(char*)elev[j]->ptr,sdslen(elev[j]->ptr)); | |
299 | lua_rawseti(lua,-2,j+1); | |
300 | } | |
301 | lua_setglobal(lua,var); | |
302 | } | |
303 | ||
7585836e | 304 | void evalCommand(redisClient *c) { |
305 | lua_State *lua = server.lua; | |
306 | char funcname[43]; | |
4ae5b5e1 | 307 | long long numkeys; |
308 | ||
309 | /* Get the number of arguments that are keys */ | |
310 | if (getLongLongFromObjectOrReply(c,c->argv[2],&numkeys,NULL) != REDIS_OK) | |
311 | return; | |
312 | if (numkeys > (c->argc - 3)) { | |
313 | addReplyError(c,"Number of keys can't be greater than number of args"); | |
314 | return; | |
315 | } | |
7585836e | 316 | |
317 | /* We obtain the script SHA1, then check if this function is already | |
318 | * defined into the Lua state */ | |
319 | funcname[0] = 'f'; | |
320 | funcname[1] = '_'; | |
321 | hashScript(funcname+2,c->argv[1]->ptr,sdslen(c->argv[1]->ptr)); | |
322 | lua_getglobal(lua, funcname); | |
323 | if (lua_isnil(lua,1)) { | |
324 | /* Function not defined... let's define it. */ | |
325 | sds funcdef = sdsempty(); | |
326 | ||
327 | lua_pop(lua,1); /* remove the nil from the stack */ | |
328 | funcdef = sdscat(funcdef,"function "); | |
329 | funcdef = sdscatlen(funcdef,funcname,42); | |
330 | funcdef = sdscatlen(funcdef," ()\n",4); | |
331 | funcdef = sdscatlen(funcdef,c->argv[1]->ptr,sdslen(c->argv[1]->ptr)); | |
332 | funcdef = sdscatlen(funcdef,"\nend\n",5); | |
379789cc | 333 | /* printf("Defining:\n%s\n",funcdef); */ |
7585836e | 334 | |
335 | if (luaL_loadbuffer(lua,funcdef,sdslen(funcdef),"func definition")) { | |
336 | addReplyErrorFormat(c,"Error compiling script (new function): %s\n", | |
337 | lua_tostring(lua,-1)); | |
338 | lua_pop(lua,1); | |
7156f43c | 339 | sdsfree(funcdef); |
7585836e | 340 | return; |
341 | } | |
7156f43c | 342 | sdsfree(funcdef); |
7585836e | 343 | if (lua_pcall(lua,0,0,0)) { |
344 | addReplyErrorFormat(c,"Error running script (new function): %s\n", | |
345 | lua_tostring(lua,-1)); | |
346 | lua_pop(lua,1); | |
347 | return; | |
348 | } | |
349 | lua_getglobal(lua, funcname); | |
350 | } | |
4ae5b5e1 | 351 | |
352 | /* Populate the argv and keys table accordingly to the arguments that | |
353 | * EVAL received. */ | |
354 | luaSetGlobalArray(lua,"KEYS",c->argv+3,numkeys); | |
355 | luaSetGlobalArray(lua,"ARGV",c->argv+3+numkeys,c->argc-3-numkeys); | |
7585836e | 356 | |
357 | /* At this point whatever this script was never seen before or if it was | |
358 | * already defined, we can call it. We have zero arguments and expect | |
359 | * a single return value. */ | |
360 | if (lua_pcall(lua,0,1,0)) { | |
361 | addReplyErrorFormat(c,"Error running script (call to %s): %s\n", | |
362 | funcname, lua_tostring(lua,-1)); | |
363 | lua_pop(lua,1); | |
364 | return; | |
365 | } | |
366 | luaReplyToRedisReply(c,lua); | |
367 | } |