]>
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');
82 lua_pushlstring(lua
,reply
+1,p
-reply
-1);
86 char *redisProtocolToLuaType_Error(lua_State
*lua
, char *reply
) {
87 char *p
= strchr(reply
+1,'\r');
90 lua_pushstring(lua
,"err");
91 lua_pushlstring(lua
,reply
+1,p
-reply
-1);
96 char *redisProtocolToLuaType_MultiBulk(lua_State
*lua
, char *reply
) {
97 char *p
= strchr(reply
+1,'\r');
101 string2ll(reply
+1,p
-reply
-1,&mbulklen
);
103 if (mbulklen
== -1) {
108 for (j
= 0; j
< mbulklen
; j
++) {
109 lua_pushnumber(lua
,j
+1);
110 p
= redisProtocolToLuaType(lua
,p
);
111 lua_settable(lua
,-3);
116 int luaRedisCommand(lua_State
*lua
) {
117 int j
, argc
= lua_gettop(lua
);
118 struct redisCommand
*cmd
;
120 redisClient
*c
= server
.lua_client
;
123 /* Build the arguments vector */
124 argv
= zmalloc(sizeof(robj
*)*argc
);
125 for (j
= 0; j
< argc
; j
++)
126 argv
[j
] = createStringObject((char*)lua_tostring(lua
,j
+1),
127 lua_strlen(lua
,j
+1));
130 cmd
= lookupCommand(argv
[0]->ptr
);
131 if (!cmd
|| ((cmd
->arity
> 0 && cmd
->arity
!= argc
) ||
132 (argc
< -cmd
->arity
)))
134 for (j
= 0; j
< argc
; j
++) decrRefCount(argv
[j
]);
137 lua_pushstring(lua
,"err");
140 "Wrong number of args calling Redis command From Lua script");
142 lua_pushstring(lua
,"Unknown Redis command called from Lua script");
143 lua_settable(lua
,-3);
147 /* Run the command in the context of a fake client */
152 /* Convert the result of the Redis command into a suitable Lua type.
153 * The first thing we need is to create a single string from the client
157 reply
= sdscatlen(reply
,c
->buf
,c
->bufpos
);
160 while(listLength(c
->reply
)) {
161 robj
*o
= listNodeValue(listFirst(c
->reply
));
163 sdscatlen(reply
,o
->ptr
,sdslen(o
->ptr
));
164 listDelNode(c
->reply
,listFirst(c
->reply
));
166 redisProtocolToLuaType(lua
,reply
);
169 /* Clean up. Command code may have changed argv/argc so we use the
170 * argv/argc of the client instead of the local variables. */
171 for (j
= 0; j
< c
->argc
; j
++)
172 decrRefCount(c
->argv
[j
]);
178 void scriptingInit(void) {
179 lua_State
*lua
= lua_open();
182 /* Register the 'r' command */
183 lua_pushcfunction(lua
,luaRedisCommand
);
184 lua_setglobal(lua
,"redis");
186 /* Create the (non connected) client that we use to execute Redis commands
187 * inside the Lua interpreter */
188 server
.lua_client
= createClient(-1);
189 server
.lua_client
->flags
|= REDIS_LUA_CLIENT
;
194 /* Hash the scripit into a SHA1 digest. We use this as Lua function name.
195 * Digest should point to a 41 bytes buffer: 40 for SHA1 converted into an
196 * hexadecimal number, plus 1 byte for null term. */
197 void hashScript(char *digest
, char *script
, size_t len
) {
199 unsigned char hash
[20];
200 char *cset
= "0123456789abcdef";
204 SHA1Update(&ctx
,(unsigned char*)script
,len
);
205 SHA1Final(hash
,&ctx
);
207 for (j
= 0; j
< 20; j
++) {
208 digest
[j
*2] = cset
[((hash
[j
]&0xF0)>>4)];
209 digest
[j
*2+1] = cset
[(hash
[j
]&0xF)];
214 void luaReplyToRedisReply(redisClient
*c
, lua_State
*lua
) {
215 int t
= lua_type(lua
,1);
219 addReplyBulkCBuffer(c
,(char*)lua_tostring(lua
,1),lua_strlen(lua
,1));
222 addReply(c
,lua_toboolean(lua
,1) ? shared
.cone
: shared
.czero
);
225 addReplyLongLong(c
,(long long)lua_tonumber(lua
,1));
228 /* We need to check if it is an array or an error.
229 * Error are returned as a single element table with 'err' field. */
230 lua_pushstring(lua
,"err");
231 lua_gettable(lua
,-2);
232 t
= lua_type(lua
,-1);
233 if (t
== LUA_TSTRING
) {
234 addReplySds(c
,sdscatprintf(sdsempty(),
235 "-%s\r\n",(char*)lua_tostring(lua
,-1)));
238 void *replylen
= addDeferredMultiBulkLength(c
);
239 int j
= 1, mbulklen
= 0;
241 lua_pop(lua
,1); /* Discard the 'err' field value we popped */
243 lua_pushnumber(lua
,j
++);
244 lua_gettable(lua
,-2);
245 t
= lua_type(lua
,-1);
249 } else if (t
== LUA_TSTRING
) {
251 char *s
= (char*) lua_tolstring(lua
,-1,&len
);
253 addReplyBulkCBuffer(c
,s
,len
);
255 } else if (t
== LUA_TNUMBER
) {
256 addReplyLongLong(c
,(long long)lua_tonumber(lua
,-1));
261 setDeferredMultiBulkLength(c
,replylen
,mbulklen
);
265 addReply(c
,shared
.nullbulk
);
270 /* Set an array of Redis String Objects as a Lua array (table) stored into a
271 * global variable. */
272 void luaSetGlobalArray(lua_State
*lua
, char *var
, robj
**elev
, int elec
) {
276 for (j
= 0; j
< elec
; j
++) {
277 lua_pushlstring(lua
,(char*)elev
[j
]->ptr
,sdslen(elev
[j
]->ptr
));
278 lua_rawseti(lua
,-2,j
+1);
280 lua_setglobal(lua
,var
);
283 void evalCommand(redisClient
*c
) {
284 lua_State
*lua
= server
.lua
;
288 /* Get the number of arguments that are keys */
289 if (getLongLongFromObjectOrReply(c
,c
->argv
[2],&numkeys
,NULL
) != REDIS_OK
)
291 if (numkeys
> (c
->argc
- 3)) {
292 addReplyError(c
,"Number of keys can't be greater than number of args");
296 /* We obtain the script SHA1, then check if this function is already
297 * defined into the Lua state */
300 hashScript(funcname
+2,c
->argv
[1]->ptr
,sdslen(c
->argv
[1]->ptr
));
301 lua_getglobal(lua
, funcname
);
302 if (lua_isnil(lua
,1)) {
303 /* Function not defined... let's define it. */
304 sds funcdef
= sdsempty();
306 lua_pop(lua
,1); /* remove the nil from the stack */
307 funcdef
= sdscat(funcdef
,"function ");
308 funcdef
= sdscatlen(funcdef
,funcname
,42);
309 funcdef
= sdscatlen(funcdef
," ()\n",4);
310 funcdef
= sdscatlen(funcdef
,c
->argv
[1]->ptr
,sdslen(c
->argv
[1]->ptr
));
311 funcdef
= sdscatlen(funcdef
,"\nend\n",5);
312 printf("Defining:\n%s\n",funcdef
);
314 if (luaL_loadbuffer(lua
,funcdef
,sdslen(funcdef
),"func definition")) {
315 addReplyErrorFormat(c
,"Error compiling script (new function): %s\n",
316 lua_tostring(lua
,-1));
322 if (lua_pcall(lua
,0,0,0)) {
323 addReplyErrorFormat(c
,"Error running script (new function): %s\n",
324 lua_tostring(lua
,-1));
328 lua_getglobal(lua
, funcname
);
331 /* Populate the argv and keys table accordingly to the arguments that
333 luaSetGlobalArray(lua
,"KEYS",c
->argv
+3,numkeys
);
334 luaSetGlobalArray(lua
,"ARGV",c
->argv
+3+numkeys
,c
->argc
-3-numkeys
);
336 /* At this point whatever this script was never seen before or if it was
337 * already defined, we can call it. We have zero arguments and expect
338 * a single return value. */
339 if (lua_pcall(lua
,0,1,0)) {
340 addReplyErrorFormat(c
,"Error running script (call to %s): %s\n",
341 funcname
, lua_tostring(lua
,-1));
345 luaReplyToRedisReply(c
,lua
);