From 9545957a67a4b51cfede58e6e42f9a9c0d89a029 Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 6 Mar 2015 16:24:44 -0800 Subject: [PATCH] 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. --- src/scripting.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/scripting.c b/src/scripting.c index c5dd4e71..8a7954cd 100644 --- a/src/scripting.c +++ b/src/scripting.c @@ -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);