sequence

This article focuses on claudb’s scripting command

AbstractEvalCommand

Claudb 1.7.1 / SRC/main/Java/com/lot/tonivade/claudb/command/scripting/AbstractEvalCommand. Java

abstract class AbstractEvalCommand implements DBCommand { @Override public RedisToken execute(Database db, Request request) { return script(request).map(script -> execute(request, script)) .getOrElse(error("NOSCRIPT No matching script. Please use EVAL")); } private RedisToken execute(Request request, SafeString script) { int numParams = parseInt(request.getParam(1).toString()); if (numParams + 2 > request.getLength()) { return error("invalid number of arguments"); } List<SafeString> params = request.getParams().stream().skip(2).collect(toList()); List<SafeString> keys = readParams(numParams, params); List<SafeString> argv = readArguments(numParams, params); return LuaInterpreter.buildFor(request).execute(script, keys, argv); } protected abstract Option<SafeString> script(Request request); private List<SafeString> readParams(int numParams, List<SafeString> params) { List<SafeString> keys = new LinkedList<>(); for (int i = 0; i < numParams; i++) { keys.add(params.get(i)); } return keys; } private List<SafeString> readArguments(int numParams, List<SafeString> params) { List<SafeString> argv = new LinkedList<>(); for (int i = numParams; i < params.size(); i++) { argv.add(params.get(i)); } return argv; }}Copy the code
  • AbstractEvalCommand implements the DBCommand interface. Its execute method obtains SafeString from the script method implemented by subclasses and then executes the script using the internal Execute method. The execute method first parses keys and argv and then executes the Lua script through luainterpreter.buildfor (request).execute(script, keys, argv)

EvalCommand

Claudb 1.7.1 / SRC/main/Java/com/lot/tonivade/claudb/command/scripting/EvalCommand. Java

@Command("eval") @ParamLength(2) public class EvalCommand extends AbstractEvalCommand { @Override protected Option<SafeString> script(Request request) { return Option.some(request.getParam(0)); }}Copy the code
  • EvalCommand inherits AbstractEvalCommand and its script method takes Request.getParam (0).

EvalShaCommand

Claudb 1.7.1 / SRC/main/Java/com/lot/tonivade/claudb/command/scripting/EvalShaCommand. Java

@Command("evalsha") @ParamLength(2) public class EvalShaCommand extends AbstractEvalCommand { @Override protected Option<SafeString> script(Request request) { DBServerState server = getServerState(request.getServerContext()); return server.getScript(request.getParam(0)); }}Copy the code
  • EvalShaCommand inherits AbstractEvalCommand. Its script method uses getServerState(request.getServerContext()) to get the server. To perform server. GetScript (request. GetParam (0))

ScriptCommands

Claudb 1.7.1 / SRC/main/Java/com/lot/tonivade/claudb/command/scripting/ScriptCommands. Java

@ParamLength(1) @Command("script") public class ScriptCommands implements DBCommand { @Override public RedisToken execute(Database db, Request request) { return Pattern1.<Request, RedisToken>build() .when(isCommand("load")) .then(this::load) .when(isCommand("exists")) .then(this::exists) .when(isCommand("flush")) .then(this::flush) .otherwise() .then(this::unknownCommand) .apply(request); } private RedisToken unknownCommand(Request request) { return RedisToken.error("Unknown SCRIPT subcommand: " + request.getParam(0)); } private RedisToken load(Request request) { SafeString script = request.getParam(1); return Try.of(() -> digest(script)).map(sha1 -> { DBServerState server = getServerState(request.getServerContext()); server.saveScript(safeString(sha1), script); return RedisToken.string(sha1); }).getOrElse(RedisToken.error("ERR cannot generate sha1 sum for script: " + script)); } private RedisToken exists(Request request) { DBServerState server = getServerState(request.getServerContext()); return integer(server.getScript(request.getParam(1)).isPresent()); } private RedisToken flush(Request request) { getServerState(request.getServerContext()).cleanScripts(); return RedisToken.responseOk(); } private String digest(SafeString script) throws NoSuchAlgorithmException { MessageDigest digest = MessageDigest.getInstance("SHA-1"); return new SafeString(digest.digest(script.getBytes())).toHexString(); } private Matcher1<Request> isCommand(String command) { return request -> request.getParam(0).toString().toLowerCase().equals(command); }}Copy the code
  • ScriptCommands implements DBCommand, and its execute method supports load, EXISTS, and flush commands. SaveScript (safeString(sha1), script); The exists method uses server.getScript(request.getParam(1)).ispresent () to determine whether a script exists. Its flush method executes getServerState(request.getServerContext()).cleanscripts ()

summary

The commands for claudb scripting are EvalCommand, EvalShaCommand, and ScriptCommands

doc

  • command/scripting