Skip to content

Commit

Permalink
fix script exists
Browse files Browse the repository at this point in the history
  • Loading branch information
PragmaTwice committed Mar 10, 2023
1 parent 4dea5c0 commit ce4bacd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/server/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "commands/commander.h"
#include "config.h"
#include "fmt/format.h"
#include "lua.h"
#include "redis_connection.h"
#include "redis_request.h"
#include "storage/compaction_checker.h"
Expand Down Expand Up @@ -1498,6 +1499,12 @@ Status Server::LookupAndCreateCommand(const std::string &cmd_name, std::unique_p
}

Status Server::ScriptExists(const std::string &sha) {
lua_getglobal(lua_, (REDIS_LUA_FUNC_SHA_PREFIX + sha).c_str());
if (!lua_isnil(lua_, -1)) {
return Status::OK();
}
lua_pop(lua_, 1);

std::string body;
return ScriptGet(sha, &body);
}
Expand Down
4 changes: 2 additions & 2 deletions src/storage/scripting.cc
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ Status evalGenericCommand(Redis::Connection *conn, const std::vector<std::string

/* We obtain the script SHA1, then check if this function is already
* defined into the Lua state */
char funcname[2 + 40 + 1] = "f_";
char funcname[2 + 40 + 1] = REDIS_LUA_FUNC_SHA_PREFIX;

if (!evalsha) {
SHA1Hex(funcname + 2, args[1].c_str(), args[1].size());
Expand Down Expand Up @@ -873,7 +873,7 @@ int redisMathRandomSeed(lua_State *L) {
* If 'c' is not NULL, on error the client is informed with an appropriate
* error describing the nature of the problem and the Lua interpreter error. */
Status createFunction(Server *srv, const std::string &body, std::string *sha, lua_State *lua, bool need_to_store) {
char funcname[2 + 40 + 1] = "f_";
char funcname[2 + 40 + 1] = REDIS_LUA_FUNC_SHA_PREFIX;

SHA1Hex(funcname + 2, body.c_str(), body.size());
*sha = funcname + 2;
Expand Down
2 changes: 2 additions & 0 deletions src/storage/scripting.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "server/redis_connection.h"
#include "status.h"

#define REDIS_LUA_FUNC_SHA_PREFIX "f_" // NOLINT

namespace Lua {

lua_State *CreateState(bool read_only = false);
Expand Down

0 comments on commit ce4bacd

Please sign in to comment.