MCPcopy Create free account
hub / github.com/apache/kvrocks / Execute

Method Execute

src/commands/cmd_script.cc:66–108  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

64 }
65
66 Status Execute([[maybe_unused]] engine::Context &ctx, Server *srv, [[maybe_unused]] Connection *conn,
67 std::string *output) override {
68 // There's a little tricky here since the script command was the write type
69 // command but some subcommands like `exists` were readonly, so we want to allow
70 // executing on slave here. Maybe we should find other way to do this.
71 if (srv->IsSlave() && subcommand_ != "exists") {
72 return {Status::RedisReadOnly, "You can't write against a read only slave"};
73 }
74
75 if (args_.size() == 2 && subcommand_ == "flush") {
76 auto s = srv->ScriptFlush();
77 if (!s) {
78 ERROR("Failed to flush scripts: {}", s.Msg());
79 return s;
80 }
81 s = srv->Propagate(engine::kPropagateScriptCommand, args_);
82 if (!s) {
83 ERROR("Failed to propagate script command: {}", s.Msg());
84 return s;
85 }
86 *output = redis::RESP_OK;
87 } else if (args_.size() >= 3 && subcommand_ == "exists") {
88 *output = redis::MultiLen(args_.size() - 2);
89 for (size_t j = 2; j < args_.size(); j++) {
90 if (srv->ScriptExists(args_[j]).IsOK()) {
91 *output += redis::Integer(1);
92 } else {
93 *output += redis::Integer(0);
94 }
95 }
96 } else if (args_.size() == 3 && subcommand_ == "load") {
97 std::string sha;
98 auto s = lua::CreateFunction(srv, args_[2], &sha, conn->Owner()->Lua(), true);
99 if (!s.IsOK()) {
100 return s;
101 }
102
103 *output = redis::BulkString(sha);
104 } else {
105 return {Status::NotOK, "Unknown SCRIPT subcommand or wrong number of arguments"};
106 }
107 return Status::OK();
108 }
109
110 private:
111 std::string subcommand_;

Callers

nothing calls this directly

Calls 12

MultiLenFunction · 0.85
CreateFunctionFunction · 0.85
BulkStringFunction · 0.85
ScriptFlushMethod · 0.80
PropagateMethod · 0.80
ScriptExistsMethod · 0.80
LuaMethod · 0.80
OwnerMethod · 0.80
IntegerFunction · 0.50
IsSlaveMethod · 0.45
MsgMethod · 0.45
IsOKMethod · 0.45

Tested by

no test coverage detected