]>
git.saurik.com Git - redis.git/blob - src/scripting.c
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
);
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.
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.
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.
29 * Errors are returned as a table with a single 'err' field set to the
33 char *redisProtocolToLuaType(lua_State
*lua
, char* reply
) {
38 p
= redisProtocolToLuaType_Int(lua
,reply
);
41 p
= redisProtocolToLuaType_Bulk(lua
,reply
);
44 p
= redisProtocolToLuaType_Status(lua
,reply
);
47 p
= redisProtocolToLuaType_Error(lua
,reply
);
50 p
= redisProtocolToLuaType_MultiBulk(lua
,reply
);
56 char *redisProtocolToLuaType_Int(lua_State
*lua
, char *reply
) {
57 char *p
= strchr(reply
+1,'\r');
60 string2ll(reply
+1,p
-reply
-1,&value
);
61 lua_pushnumber(lua
,(lua_Number
)value
);
65 char *redisProtocolToLuaType_Bulk(lua_State
*lua
, char *reply
) {
66 char *p
= strchr(reply
+1,'\r');
69 string2ll(reply
+1,p
-reply
-1,&bulklen
);
74 lua_pushlstring(lua
,p
+2,bulklen
);
79 char *redisProtocolToLuaType_Status(lua_State
*lua
, char *reply
) {
80 char *p
= strchr(reply
+1,'\r');
83 lua_pushstring(lua
,"ok");
84 lua_pushlstring(lua
,reply
+1,p
-reply
-1);
89 char *redisProtocolToLuaType_Error(lua_State
*lua
, char *reply
) {
90 char *p
= strchr(reply
+1,'\r');
93 lua_pushstring(lua
,"err");
94 lua_pushlstring(lua
,reply
+1,p
-reply
-1);
99 char *redisProtocolToLuaType_MultiBulk(lua_State
*lua
, char *reply
) {
100 char *p
= strchr(reply
+1,'\r');
104 string2ll(reply
+1,p
-reply
-1,&mbulklen
);
106 if (mbulklen
== -1) {
111 for (j
= 0; j
< mbulklen
; j
++) {
112 lua_pushnumber(lua
,j
+1);
113 p
= redisProtocolToLuaType(lua
,p
);
114 lua_settable(lua
,-3);
119 void luaPushError(lua_State
*lua
, char *error
) {
121 lua_pushstring(lua
,"err");
122 lua_pushstring(lua
, error
);
123 lua_settable(lua
,-3);
126 int luaRedisCommand(lua_State
*lua
) {
127 int j
, argc
= lua_gettop(lua
);
128 struct redisCommand
*cmd
;
130 redisClient
*c
= server
.lua_client
;
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));
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). */
147 decrRefCount(argv
[j
]);
152 "Lua redis() command arguments must be strings or integers");
157 cmd
= lookupCommand(argv
[0]->ptr
);
158 if (!cmd
|| ((cmd
->arity
> 0 && cmd
->arity
!= argc
) ||
159 (argc
< -cmd
->arity
)))
161 for (j
= 0; j
< argc
; j
++) decrRefCount(argv
[j
]);
165 "Wrong number of args calling Redis command From Lua script");
167 luaPushError(lua
,"Unknown Redis command called from Lua script");
171 /* Run the command in the context of a fake client */
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
181 reply
= sdscatlen(reply
,c
->buf
,c
->bufpos
);
184 while(listLength(c
->reply
)) {
185 robj
*o
= listNodeValue(listFirst(c
->reply
));
187 reply
= sdscatlen(reply
,o
->ptr
,sdslen(o
->ptr
));
188 listDelNode(c
->reply
,listFirst(c
->reply
));
190 redisProtocolToLuaType(lua
,reply
);
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
]);
202 void luaMaskCountHook(lua_State
*lua
, lua_Debug
*ar
) {
206 if (server
.lua_time_limit
<= 0) return;
207 elapsed
= (ustime()/1000) - server
.lua_time_start
;
208 if (elapsed
>= server
.lua_time_limit
) {
209 lua_pushstring(lua
,"Script aborted for max execution time...");
211 redisLog(REDIS_NOTICE
,"Lua script aborted for max execution time after %lld milliseconds of running time",elapsed
);
215 void scriptingInit(void) {
216 lua_State
*lua
= lua_open();
219 /* Register the 'r' command */
220 lua_pushcfunction(lua
,luaRedisCommand
);
221 lua_setglobal(lua
,"redis");
223 /* Create the (non connected) client that we use to execute Redis commands
224 * inside the Lua interpreter */
225 server
.lua_client
= createClient(-1);
226 server
.lua_client
->flags
|= REDIS_LUA_CLIENT
;
228 /* Set an hook in order to be able to stop the script execution if it
229 * is running for too much time. */
230 lua_sethook(lua
,luaMaskCountHook
,LUA_MASKCOUNT
,100000);
235 /* Hash the scripit into a SHA1 digest. We use this as Lua function name.
236 * Digest should point to a 41 bytes buffer: 40 for SHA1 converted into an
237 * hexadecimal number, plus 1 byte for null term. */
238 void hashScript(char *digest
, char *script
, size_t len
) {
240 unsigned char hash
[20];
241 char *cset
= "0123456789abcdef";
245 SHA1Update(&ctx
,(unsigned char*)script
,len
);
246 SHA1Final(hash
,&ctx
);
248 for (j
= 0; j
< 20; j
++) {
249 digest
[j
*2] = cset
[((hash
[j
]&0xF0)>>4)];
250 digest
[j
*2+1] = cset
[(hash
[j
]&0xF)];
255 void luaReplyToRedisReply(redisClient
*c
, lua_State
*lua
) {
256 int t
= lua_type(lua
,1);
260 addReplyBulkCBuffer(c
,(char*)lua_tostring(lua
,1),lua_strlen(lua
,1));
263 addReply(c
,lua_toboolean(lua
,1) ? shared
.cone
: shared
.czero
);
266 addReplyLongLong(c
,(long long)lua_tonumber(lua
,1));
269 /* We need to check if it is an array, an error, or a status reply.
270 * Error are returned as a single element table with 'err' field.
271 * Status replies are returned as single elment table with 'ok' field */
272 lua_pushstring(lua
,"err");
273 lua_gettable(lua
,-2);
274 t
= lua_type(lua
,-1);
275 if (t
== LUA_TSTRING
) {
276 addReplySds(c
,sdscatprintf(sdsempty(),
277 "-%s\r\n",(char*)lua_tostring(lua
,-1)));
283 lua_pushstring(lua
,"ok");
284 lua_gettable(lua
,-2);
285 t
= lua_type(lua
,-1);
286 if (t
== LUA_TSTRING
) {
287 addReplySds(c
,sdscatprintf(sdsempty(),
288 "+%s\r\n",(char*)lua_tostring(lua
,-1)));
291 void *replylen
= addDeferredMultiBulkLength(c
);
292 int j
= 1, mbulklen
= 0;
294 lua_pop(lua
,1); /* Discard the 'ok' field value we popped */
296 lua_pushnumber(lua
,j
++);
297 lua_gettable(lua
,-2);
298 t
= lua_type(lua
,-1);
302 } else if (t
== LUA_TSTRING
) {
304 char *s
= (char*) lua_tolstring(lua
,-1,&len
);
306 addReplyBulkCBuffer(c
,s
,len
);
308 } else if (t
== LUA_TNUMBER
) {
309 addReplyLongLong(c
,(long long)lua_tonumber(lua
,-1));
314 setDeferredMultiBulkLength(c
,replylen
,mbulklen
);
318 addReply(c
,shared
.nullbulk
);
323 /* Set an array of Redis String Objects as a Lua array (table) stored into a
324 * global variable. */
325 void luaSetGlobalArray(lua_State
*lua
, char *var
, robj
**elev
, int elec
) {
329 for (j
= 0; j
< elec
; j
++) {
330 lua_pushlstring(lua
,(char*)elev
[j
]->ptr
,sdslen(elev
[j
]->ptr
));
331 lua_rawseti(lua
,-2,j
+1);
333 lua_setglobal(lua
,var
);
336 void evalCommand(redisClient
*c
) {
337 lua_State
*lua
= server
.lua
;
341 /* Get the number of arguments that are keys */
342 if (getLongLongFromObjectOrReply(c
,c
->argv
[2],&numkeys
,NULL
) != REDIS_OK
)
344 if (numkeys
> (c
->argc
- 3)) {
345 addReplyError(c
,"Number of keys can't be greater than number of args");
349 /* We obtain the script SHA1, then check if this function is already
350 * defined into the Lua state */
353 hashScript(funcname
+2,c
->argv
[1]->ptr
,sdslen(c
->argv
[1]->ptr
));
354 lua_getglobal(lua
, funcname
);
355 if (lua_isnil(lua
,1)) {
356 /* Function not defined... let's define it. */
357 sds funcdef
= sdsempty();
359 lua_pop(lua
,1); /* remove the nil from the stack */
360 funcdef
= sdscat(funcdef
,"function ");
361 funcdef
= sdscatlen(funcdef
,funcname
,42);
362 funcdef
= sdscatlen(funcdef
," ()\n",4);
363 funcdef
= sdscatlen(funcdef
,c
->argv
[1]->ptr
,sdslen(c
->argv
[1]->ptr
));
364 funcdef
= sdscatlen(funcdef
,"\nend\n",5);
365 /* printf("Defining:\n%s\n",funcdef); */
367 if (luaL_loadbuffer(lua
,funcdef
,sdslen(funcdef
),"func definition")) {
368 addReplyErrorFormat(c
,"Error compiling script (new function): %s\n",
369 lua_tostring(lua
,-1));
375 if (lua_pcall(lua
,0,0,0)) {
376 addReplyErrorFormat(c
,"Error running script (new function): %s\n",
377 lua_tostring(lua
,-1));
381 lua_getglobal(lua
, funcname
);
384 /* Populate the argv and keys table accordingly to the arguments that
386 luaSetGlobalArray(lua
,"KEYS",c
->argv
+3,numkeys
);
387 luaSetGlobalArray(lua
,"ARGV",c
->argv
+3+numkeys
,c
->argc
-3-numkeys
);
389 /* Select the right DB in the context of the Lua client */
390 selectDb(server
.lua_client
,c
->db
->id
);
392 /* At this point whatever this script was never seen before or if it was
393 * already defined, we can call it. We have zero arguments and expect
394 * a single return value. */
395 server
.lua_time_start
= ustime()/1000;
396 if (lua_pcall(lua
,0,1,0)) {
397 selectDb(c
,server
.lua_client
->db
->id
); /* set DB ID from Lua client */
398 addReplyErrorFormat(c
,"Error running script (call to %s): %s\n",
399 funcname
, lua_tostring(lua
,-1));
401 lua_gc(lua
,LUA_GCCOLLECT
,0);
404 selectDb(c
,server
.lua_client
->db
->id
); /* set DB ID from Lua client */
405 luaReplyToRedisReply(c
,lua
);
406 lua_gc(lua
,LUA_GCSTEP
,1);