Commit 9545957a authored by antirez's avatar antirez
Browse files

Scripting: native lua.exists() implementation.

This was a test to check how much faster native implementation would be.
In initial tests it does not look like is this huge win.

After all this is at least in part obvious. Now scripting.c tries to
avoid allocations of argument vectors, and turning ":1" accumulated in
the client buffer into a Lua type is a fast operation.
parent 2bf27c33
......@@ -458,6 +458,35 @@ int luaRedisPCallCommand(lua_State *lua) {
return luaRedisGenericCommand(lua,0);
}
int luaExistsCommand(lua_State *lua) {
int argc = lua_gettop(lua);
redisClient *c = server.lua_client;
char *obj_s;
size_t obj_len;
robj *key, *val;
/* Require at least one argument */
if (argc != 1) {
luaPushError(lua,
"redis.exists() needs exactly one argument");
return 1;
}
obj_s = (char*)lua_tolstring(lua,1,&obj_len);
if (obj_s == NULL) {
luaPushError(lua,
"redis.exists() argument must be a string");
return 1;
}
key = createStringObject(obj_s,obj_len);
val = lookupKeyRead(c->db,key);
decrRefCount(key);
lua_pushnumber(lua,val != NULL);
return 1;
}
/* This adds redis.sha1hex(string) to Lua scripts using the same hashing
* function used for sha1ing lua scripts. */
int luaRedisSha1hexCommand(lua_State *lua) {
......@@ -664,6 +693,11 @@ void scriptingInit(void) {
lua_pushcfunction(lua,luaRedisPCallCommand);
lua_settable(lua,-3);
/* redis.exists */
lua_pushstring(lua,"exists");
lua_pushcfunction(lua,luaExistsCommand);
lua_settable(lua,-3);
/* redis.log and log levels. */
lua_pushstring(lua,"log");
lua_pushcfunction(lua,luaLogCommand);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment