]> git.saurik.com Git - redis.git/blob - src/scripting.c
4b7a89c41acdf7fc3c89eb13f0b45c7df6fb2a77
[redis.git] / src / scripting.c
1 #include "redis.h"
2 #include "sha1.h"
3
4 #include <lua.h>
5 #include <lauxlib.h>
6 #include <lualib.h>
7
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);
11 char *redisProtocolToLuaType_Error(lua_State *lua, char *reply);
12 char *redisProtocolToLuaType_MultiBulk(lua_State *lua, char *reply);
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;
46 case '-':
47 p = redisProtocolToLuaType_Error(lua,reply);
48 break;
49 case '*':
50 p = redisProtocolToLuaType_MultiBulk(lua,reply);
51 break;
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);
70 if (bulklen == -1) {
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_newtable(lua);
83 lua_pushstring(lua,"ok");
84 lua_pushlstring(lua,reply+1,p-reply-1);
85 lua_settable(lua,-3);
86 return p+2;
87 }
88
89 char *redisProtocolToLuaType_Error(lua_State *lua, char *reply) {
90 char *p = strchr(reply+1,'\r');
91
92 lua_newtable(lua);
93 lua_pushstring(lua,"err");
94 lua_pushlstring(lua,reply+1,p-reply-1);
95 lua_settable(lua,-3);
96 return p+2;
97 }
98
99 char *redisProtocolToLuaType_MultiBulk(lua_State *lua, char *reply) {
100 char *p = strchr(reply+1,'\r');
101 long long mbulklen;
102 int j = 0;
103
104 string2ll(reply+1,p-reply-1,&mbulklen);
105 p += 2;
106 if (mbulklen == -1) {
107 lua_pushnil(lua);
108 return p;
109 }
110 lua_newtable(lua);
111 for (j = 0; j < mbulklen; j++) {
112 lua_pushnumber(lua,j+1);
113 p = redisProtocolToLuaType(lua,p);
114 lua_settable(lua,-3);
115 }
116 return p;
117 }
118
119 void luaPushError(lua_State *lua, char *error) {
120 lua_newtable(lua);
121 lua_pushstring(lua,"err");
122 lua_pushstring(lua, error);
123 lua_settable(lua,-3);
124 }
125
126 int luaRedisCommand(lua_State *lua) {
127 int j, argc = lua_gettop(lua);
128 struct redisCommand *cmd;
129 robj **argv;
130 redisClient *c = server.lua_client;
131 sds reply;
132
133 /* Build the arguments vector */
134 argv = zmalloc(sizeof(robj*)*argc);
135 for (j = 0; j < argc; j++) {
136 if (!lua_isstring(lua,j+1)) break;
137 argv[j] = createStringObject((char*)lua_tostring(lua,j+1),
138 lua_strlen(lua,j+1));
139 }
140
141 /* Check if one of the arguments passed by the Lua script
142 * is not a string or an integer (lua_isstring() return true for
143 * integers as well). */
144 if (j != argc) {
145 j--;
146 while (j >= 0) {
147 decrRefCount(argv[j]);
148 j--;
149 }
150 zfree(argv);
151 luaPushError(lua,
152 "Lua redis() command arguments must be strings or integers");
153 return 1;
154 }
155
156 /* Command lookup */
157 cmd = lookupCommand(argv[0]->ptr);
158 if (!cmd || ((cmd->arity > 0 && cmd->arity != argc) ||
159 (argc < -cmd->arity)))
160 {
161 for (j = 0; j < argc; j++) decrRefCount(argv[j]);
162 zfree(argv);
163 if (cmd)
164 luaPushError(lua,
165 "Wrong number of args calling Redis command From Lua script");
166 else
167 luaPushError(lua,"Unknown Redis command called from Lua script");
168 return 1;
169 }
170
171 /* Run the command in the context of a fake client */
172 c->argv = argv;
173 c->argc = argc;
174 cmd->proc(c);
175
176 /* Convert the result of the Redis command into a suitable Lua type.
177 * The first thing we need is to create a single string from the client
178 * output buffers. */
179 reply = sdsempty();
180 if (c->bufpos) {
181 reply = sdscatlen(reply,c->buf,c->bufpos);
182 c->bufpos = 0;
183 }
184 while(listLength(c->reply)) {
185 robj *o = listNodeValue(listFirst(c->reply));
186
187 reply = sdscatlen(reply,o->ptr,sdslen(o->ptr));
188 listDelNode(c->reply,listFirst(c->reply));
189 }
190 redisProtocolToLuaType(lua,reply);
191 sdsfree(reply);
192
193 /* Clean up. Command code may have changed argv/argc so we use the
194 * argv/argc of the client instead of the local variables. */
195 for (j = 0; j < c->argc; j++)
196 decrRefCount(c->argv[j]);
197 zfree(c->argv);
198
199 return 1;
200 }
201
202 void luaMaskCountHook(lua_State *lua, lua_Debug *ar) {
203 long long elapsed;
204 REDIS_NOTUSED(ar);
205
206 elapsed = (ustime()/1000) - server.lua_time_start;
207 if (elapsed >= server.lua_time_limit) {
208 redisLog(REDIS_NOTICE,"Lua script aborted for max execution time after %lld milliseconds of running time.",elapsed);
209 lua_pushstring(lua,"Script aborted for max execution time.");
210 lua_error(lua);
211 }
212 }
213
214 void scriptingInit(void) {
215 lua_State *lua = lua_open();
216 luaL_openlibs(lua);
217
218 /* Register the 'r' command */
219 lua_pushcfunction(lua,luaRedisCommand);
220 lua_setglobal(lua,"redis");
221
222 /* Create the (non connected) client that we use to execute Redis commands
223 * inside the Lua interpreter */
224 server.lua_client = createClient(-1);
225 server.lua_client->flags |= REDIS_LUA_CLIENT;
226
227 server.lua = lua;
228 }
229
230 /* Hash the scripit into a SHA1 digest. We use this as Lua function name.
231 * Digest should point to a 41 bytes buffer: 40 for SHA1 converted into an
232 * hexadecimal number, plus 1 byte for null term. */
233 void hashScript(char *digest, char *script, size_t len) {
234 SHA1_CTX ctx;
235 unsigned char hash[20];
236 char *cset = "0123456789abcdef";
237 int j;
238
239 SHA1Init(&ctx);
240 SHA1Update(&ctx,(unsigned char*)script,len);
241 SHA1Final(hash,&ctx);
242
243 for (j = 0; j < 20; j++) {
244 digest[j*2] = cset[((hash[j]&0xF0)>>4)];
245 digest[j*2+1] = cset[(hash[j]&0xF)];
246 }
247 digest[40] = '\0';
248 }
249
250 void luaReplyToRedisReply(redisClient *c, lua_State *lua) {
251 int t = lua_type(lua,1);
252
253 switch(t) {
254 case LUA_TSTRING:
255 addReplyBulkCBuffer(c,(char*)lua_tostring(lua,1),lua_strlen(lua,1));
256 break;
257 case LUA_TBOOLEAN:
258 addReply(c,lua_toboolean(lua,1) ? shared.cone : shared.czero);
259 break;
260 case LUA_TNUMBER:
261 addReplyLongLong(c,(long long)lua_tonumber(lua,1));
262 break;
263 case LUA_TTABLE:
264 /* We need to check if it is an array, an error, or a status reply.
265 * Error are returned as a single element table with 'err' field.
266 * Status replies are returned as single elment table with 'ok' field */
267 lua_pushstring(lua,"err");
268 lua_gettable(lua,-2);
269 t = lua_type(lua,-1);
270 if (t == LUA_TSTRING) {
271 addReplySds(c,sdscatprintf(sdsempty(),
272 "-%s\r\n",(char*)lua_tostring(lua,-1)));
273 lua_pop(lua,2);
274 return;
275 }
276
277 lua_pop(lua,1);
278 lua_pushstring(lua,"ok");
279 lua_gettable(lua,-2);
280 t = lua_type(lua,-1);
281 if (t == LUA_TSTRING) {
282 addReplySds(c,sdscatprintf(sdsempty(),
283 "+%s\r\n",(char*)lua_tostring(lua,-1)));
284 lua_pop(lua,1);
285 } else {
286 void *replylen = addDeferredMultiBulkLength(c);
287 int j = 1, mbulklen = 0;
288
289 lua_pop(lua,1); /* Discard the 'ok' field value we popped */
290 while(1) {
291 lua_pushnumber(lua,j++);
292 lua_gettable(lua,-2);
293 t = lua_type(lua,-1);
294 if (t == LUA_TNIL) {
295 lua_pop(lua,1);
296 break;
297 } else if (t == LUA_TSTRING) {
298 size_t len;
299 char *s = (char*) lua_tolstring(lua,-1,&len);
300
301 addReplyBulkCBuffer(c,s,len);
302 mbulklen++;
303 } else if (t == LUA_TNUMBER) {
304 addReplyLongLong(c,(long long)lua_tonumber(lua,-1));
305 mbulklen++;
306 }
307 lua_pop(lua,1);
308 }
309 setDeferredMultiBulkLength(c,replylen,mbulklen);
310 }
311 break;
312 default:
313 addReply(c,shared.nullbulk);
314 }
315 lua_pop(lua,1);
316 }
317
318 /* Set an array of Redis String Objects as a Lua array (table) stored into a
319 * global variable. */
320 void luaSetGlobalArray(lua_State *lua, char *var, robj **elev, int elec) {
321 int j;
322
323 lua_newtable(lua);
324 for (j = 0; j < elec; j++) {
325 lua_pushlstring(lua,(char*)elev[j]->ptr,sdslen(elev[j]->ptr));
326 lua_rawseti(lua,-2,j+1);
327 }
328 lua_setglobal(lua,var);
329 }
330
331 void evalCommand(redisClient *c) {
332 lua_State *lua = server.lua;
333 char funcname[43];
334 long long numkeys;
335
336 /* Get the number of arguments that are keys */
337 if (getLongLongFromObjectOrReply(c,c->argv[2],&numkeys,NULL) != REDIS_OK)
338 return;
339 if (numkeys > (c->argc - 3)) {
340 addReplyError(c,"Number of keys can't be greater than number of args");
341 return;
342 }
343
344 /* We obtain the script SHA1, then check if this function is already
345 * defined into the Lua state */
346 funcname[0] = 'f';
347 funcname[1] = '_';
348 hashScript(funcname+2,c->argv[1]->ptr,sdslen(c->argv[1]->ptr));
349 lua_getglobal(lua, funcname);
350 if (lua_isnil(lua,1)) {
351 /* Function not defined... let's define it. */
352 sds funcdef = sdsempty();
353
354 lua_pop(lua,1); /* remove the nil from the stack */
355 funcdef = sdscat(funcdef,"function ");
356 funcdef = sdscatlen(funcdef,funcname,42);
357 funcdef = sdscatlen(funcdef," ()\n",4);
358 funcdef = sdscatlen(funcdef,c->argv[1]->ptr,sdslen(c->argv[1]->ptr));
359 funcdef = sdscatlen(funcdef,"\nend\n",5);
360 /* printf("Defining:\n%s\n",funcdef); */
361
362 if (luaL_loadbuffer(lua,funcdef,sdslen(funcdef),"func definition")) {
363 addReplyErrorFormat(c,"Error compiling script (new function): %s\n",
364 lua_tostring(lua,-1));
365 lua_pop(lua,1);
366 sdsfree(funcdef);
367 return;
368 }
369 sdsfree(funcdef);
370 if (lua_pcall(lua,0,0,0)) {
371 addReplyErrorFormat(c,"Error running script (new function): %s\n",
372 lua_tostring(lua,-1));
373 lua_pop(lua,1);
374 return;
375 }
376 lua_getglobal(lua, funcname);
377 }
378
379 /* Populate the argv and keys table accordingly to the arguments that
380 * EVAL received. */
381 luaSetGlobalArray(lua,"KEYS",c->argv+3,numkeys);
382 luaSetGlobalArray(lua,"ARGV",c->argv+3+numkeys,c->argc-3-numkeys);
383
384 /* Select the right DB in the context of the Lua client */
385 selectDb(server.lua_client,c->db->id);
386
387 /* Set an hook in order to be able to stop the script execution if it
388 * is running for too much time.
389 * We set the hook only if the time limit is enabled as the hook will
390 * make the Lua script execution slower. */
391 if (server.lua_time_limit > 0) {
392 lua_sethook(lua,luaMaskCountHook,LUA_MASKCOUNT,100000);
393 server.lua_time_start = ustime()/1000;
394 } else {
395 lua_sethook(lua,luaMaskCountHook,0,0);
396 }
397
398 /* At this point whatever this script was never seen before or if it was
399 * already defined, we can call it. We have zero arguments and expect
400 * a single return value. */
401 if (lua_pcall(lua,0,1,0)) {
402 selectDb(c,server.lua_client->db->id); /* set DB ID from Lua client */
403 addReplyErrorFormat(c,"Error running script (call to %s): %s\n",
404 funcname, lua_tostring(lua,-1));
405 lua_pop(lua,1);
406 lua_gc(lua,LUA_GCCOLLECT,0);
407 return;
408 }
409 selectDb(c,server.lua_client->db->id); /* set DB ID from Lua client */
410 luaReplyToRedisReply(c,lua);
411 lua_gc(lua,LUA_GCSTEP,1);
412 }