]>
git.saurik.com Git - redis.git/blob - src/scripting.c
5f169037ae1b8f8252c3ef9e6724a2f6321310bb
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
);
12 /* Take a Redis reply in the Redis protocol format and convert it into a
13 * Lua type. Thanks to this function, and the introduction of not connected
14 * clients, it is trvial to implement the redis() lua function.
16 * Basically we take the arguments, execute the Redis command in the context
17 * of a non connected client, then take the generated reply and convert it
18 * into a suitable Lua type. With this trick the scripting feature does not
19 * need the introduction of a full Redis internals API. Basically the script
20 * is like a normal client that bypasses all the slow I/O paths.
22 * Note: in this function we do not do any sanity check as the reply is
23 * generated by Redis directly. This allows use to go faster.
24 * The reply string can be altered during the parsing as it is discared
25 * after the conversion is completed.
27 * Errors are returned as a table with a single 'err' field set to the
31 char *redisProtocolToLuaType(lua_State
*lua
, char* reply
) {
36 p
= redisProtocolToLuaType_Int(lua
,reply
);
39 p
= redisProtocolToLuaType_Bulk(lua
,reply
);
42 p
= redisProtocolToLuaType_Status(lua
,reply
);
48 char *redisProtocolToLuaType_Int(lua_State
*lua
, char *reply
) {
49 char *p
= strchr(reply
+1,'\r');
52 string2ll(reply
+1,p
-reply
-1,&value
);
53 lua_pushnumber(lua
,(lua_Number
)value
);
57 char *redisProtocolToLuaType_Bulk(lua_State
*lua
, char *reply
) {
58 char *p
= strchr(reply
+1,'\r');
61 string2ll(reply
+1,p
-reply
-1,&bulklen
);
66 lua_pushlstring(lua
,p
+2,bulklen
);
71 char *redisProtocolToLuaType_Status(lua_State
*lua
, char *reply
) {
72 char *p
= strchr(reply
+1,'\r');
74 lua_pushlstring(lua
,reply
+1,p
-reply
-1);
78 int luaRedisCommand(lua_State
*lua
) {
79 int j
, argc
= lua_gettop(lua
);
80 struct redisCommand
*cmd
;
82 redisClient
*c
= server
.lua_client
;
85 /* Build the arguments vector */
86 argv
= zmalloc(sizeof(robj
*)*argc
);
87 for (j
= 0; j
< argc
; j
++)
88 argv
[j
] = createStringObject((char*)lua_tostring(lua
,j
+1),
92 cmd
= lookupCommand(argv
[0]->ptr
);
94 for (j
= 0; j
< argc
; j
++) decrRefCount(argv
[j
]);
97 lua_pushstring(lua
,"err");
98 lua_pushstring(lua
,"Unknown Redis command called from Lua script");
103 /* Run the command in the context of a fake client */
108 /* Convert the result of the Redis command into a suitable Lua type.
109 * The first thing we need is to create a single string from the client
113 reply
= sdscatlen(reply
,c
->buf
,c
->bufpos
);
116 while(listLength(c
->reply
)) {
117 robj
*o
= listNodeValue(listFirst(c
->reply
));
119 sdscatlen(reply
,o
->ptr
,sdslen(o
->ptr
));
120 listDelNode(c
->reply
,listFirst(c
->reply
));
122 redisProtocolToLuaType(lua
,reply
);
125 /* Clean up. Command code may have changed argv/argc so we use the
126 * argv/argc of the client instead of the local variables. */
127 for (j
= 0; j
< c
->argc
; j
++)
128 decrRefCount(c
->argv
[j
]);
134 void scriptingInit(void) {
135 lua_State
*lua
= lua_open();
138 /* Register the 'r' command */
139 lua_pushcfunction(lua
,luaRedisCommand
);
140 lua_setglobal(lua
,"redis");
142 /* Create the (non connected) client that we use to execute Redis commands
143 * inside the Lua interpreter */
144 server
.lua_client
= createClient(-1);
145 server
.lua_client
->flags
|= REDIS_LUA_CLIENT
;
150 /* Hash the scripit into a SHA1 digest. We use this as Lua function name.
151 * Digest should point to a 41 bytes buffer: 40 for SHA1 converted into an
152 * hexadecimal number, plus 1 byte for null term. */
153 void hashScript(char *digest
, char *script
, size_t len
) {
155 unsigned char hash
[20];
156 char *cset
= "0123456789abcdef";
160 SHA1Update(&ctx
,(unsigned char*)script
,len
);
161 SHA1Final(hash
,&ctx
);
163 for (j
= 0; j
< 20; j
++) {
164 digest
[j
*2] = cset
[((hash
[j
]&0xF0)>>4)];
165 digest
[j
*2+1] = cset
[(hash
[j
]&0xF)];
170 void luaReplyToRedisReply(redisClient
*c
, lua_State
*lua
) {
171 int t
= lua_type(lua
,1);
175 addReplyBulkCBuffer(c
,(char*)lua_tostring(lua
,1),lua_strlen(lua
,1));
178 addReply(c
,lua_toboolean(lua
,1) ? shared
.cone
: shared
.czero
);
181 addReplyLongLong(c
,(long long)lua_tonumber(lua
,1));
184 /* We need to check if it is an array or an error.
185 * Error are returned as a single element table with 'err' field. */
186 lua_pushstring(lua
,"err");
187 lua_gettable(lua
,-2);
188 t
= lua_type(lua
,-1);
189 if (t
== LUA_TSTRING
) {
190 addReplyError(c
,(char*)lua_tostring(lua
,-1));
193 void *replylen
= addDeferredMultiBulkLength(c
);
194 int j
= 1, mbulklen
= 0;
196 lua_pop(lua
,1); /* Discard the 'err' field value we popped */
198 lua_pushnumber(lua
,j
++);
199 lua_gettable(lua
,-2);
200 t
= lua_type(lua
,-1);
204 } else if (t
== LUA_TSTRING
) {
206 char *s
= (char*) lua_tolstring(lua
,-1,&len
);
208 addReplyBulkCBuffer(c
,s
,len
);
210 } else if (t
== LUA_TNUMBER
) {
211 addReplyLongLong(c
,(long long)lua_tonumber(lua
,-1));
216 setDeferredMultiBulkLength(c
,replylen
,mbulklen
);
220 addReply(c
,shared
.nullbulk
);
225 /* Set an array of Redis String Objects as a Lua array (table) stored into a
226 * global variable. */
227 void luaSetGlobalArray(lua_State
*lua
, char *var
, robj
**elev
, int elec
) {
231 for (j
= 0; j
< elec
; j
++) {
232 lua_pushlstring(lua
,(char*)elev
[j
]->ptr
,sdslen(elev
[j
]->ptr
));
233 lua_rawseti(lua
,-2,j
+1);
235 lua_setglobal(lua
,var
);
238 void evalCommand(redisClient
*c
) {
239 lua_State
*lua
= server
.lua
;
243 /* Get the number of arguments that are keys */
244 if (getLongLongFromObjectOrReply(c
,c
->argv
[2],&numkeys
,NULL
) != REDIS_OK
)
246 if (numkeys
> (c
->argc
- 3)) {
247 addReplyError(c
,"Number of keys can't be greater than number of args");
251 /* We obtain the script SHA1, then check if this function is already
252 * defined into the Lua state */
255 hashScript(funcname
+2,c
->argv
[1]->ptr
,sdslen(c
->argv
[1]->ptr
));
256 lua_getglobal(lua
, funcname
);
257 if (lua_isnil(lua
,1)) {
258 /* Function not defined... let's define it. */
259 sds funcdef
= sdsempty();
261 lua_pop(lua
,1); /* remove the nil from the stack */
262 funcdef
= sdscat(funcdef
,"function ");
263 funcdef
= sdscatlen(funcdef
,funcname
,42);
264 funcdef
= sdscatlen(funcdef
," ()\n",4);
265 funcdef
= sdscatlen(funcdef
,c
->argv
[1]->ptr
,sdslen(c
->argv
[1]->ptr
));
266 funcdef
= sdscatlen(funcdef
,"\nend\n",5);
267 printf("Defining:\n%s\n",funcdef
);
269 if (luaL_loadbuffer(lua
,funcdef
,sdslen(funcdef
),"func definition")) {
270 addReplyErrorFormat(c
,"Error compiling script (new function): %s\n",
271 lua_tostring(lua
,-1));
277 if (lua_pcall(lua
,0,0,0)) {
278 addReplyErrorFormat(c
,"Error running script (new function): %s\n",
279 lua_tostring(lua
,-1));
283 lua_getglobal(lua
, funcname
);
286 /* Populate the argv and keys table accordingly to the arguments that
288 luaSetGlobalArray(lua
,"KEYS",c
->argv
+3,numkeys
);
289 luaSetGlobalArray(lua
,"ARGV",c
->argv
+3+numkeys
,c
->argc
-3-numkeys
);
291 /* At this point whatever this script was never seen before or if it was
292 * already defined, we can call it. We have zero arguments and expect
293 * a single return value. */
294 if (lua_pcall(lua
,0,1,0)) {
295 addReplyErrorFormat(c
,"Error running script (call to %s): %s\n",
296 funcname
, lua_tostring(lua
,-1));
300 luaReplyToRedisReply(c
,lua
);