MCPcopy Create free account
hub / github.com/Tencent/Tendis / saveLuaScript

Method saveLuaScript

src/tendisplus/script/script_manager.cpp:142–175  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

140}
141
142Expected<std::string> ScriptManager::saveLuaScript(Session* sess,
143 const std::string& sha,
144 const std::string& script) {
145 auto luaState = getLuaStateBelongToThisThread();
146 auto expLoadState = luaState->tryLoadLuaScript(script);
147 RET_IF_ERR_EXPECTED(expLoadState);
148 {
149 std::shared_lock<std::shared_timed_mutex> lock(_mutex);
150 luaState->setRunning(false);
151 }
152 auto expdb = _svr->getSegmentMgr()->getDb(
153 sess, LUASCRIPT_DEFAULT_DBID, mgl::LockMode::LOCK_IX);
154 RET_IF_ERR_EXPECTED(expdb);
155 auto kvstore = expdb.value().store;
156 auto ptxn = kvstore->createTransaction(sess);
157 RET_IF_ERR_EXPECTED(ptxn);
158 auto& txn = ptxn.value();
159 // if sha is empty, calculate sha code for script first.
160 std::string tmpSha;
161 if (sha.empty()) {
162 tmpSha = LuaState::getShaEncode(script);
163 } else {
164 tmpSha = toLower(sha);
165 }
166 RecordKey rk(
167 LuaScript::CHUNKID, LuaScript::DBID, RecordType::RT_META, tmpSha, "");
168 RecordValue rv(script, RecordType::RT_META, -1);
169 auto s = kvstore->setKV(rk, rv, txn.get());
170 RET_IF_ERR(s);
171 auto commitStatus = txn->commit();
172 RET_IF_ERR_EXPECTED(commitStatus);
173
174 return Command::fmtBulk(tmpSha);
175}
176
177Expected<std::string> ScriptManager::checkIfScriptExists(Session* sess) {
178 const std::vector<std::string>& args = sess->getArgs();

Callers 1

runMethod · 0.80

Calls 9

toLowerFunction · 0.85
tryLoadLuaScriptMethod · 0.80
setRunningMethod · 0.80
getDbMethod · 0.80
getSegmentMgrMethod · 0.80
setKVMethod · 0.80
commitMethod · 0.80
createTransactionMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected