| 140 | } |
| 141 | |
| 142 | Expected<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 | |
| 177 | Expected<std::string> ScriptManager::checkIfScriptExists(Session* sess) { |
| 178 | const std::vector<std::string>& args = sess->getArgs(); |
no test coverage detected