sequence

This article focuses on claudb transaction Command

TransactionState

Claudb 1.7.1 / SRC/main/Java/com/lot/tonivade/claudb/TransactionState Java

public class TransactionState implements Iterable<Request> { private List<Request> requests = new LinkedList<>(); public void enqueue(Request request) { requests.add(request); } public int size() { return requests.size(); } @Override public Iterator<Request> iterator() { return requests.iterator(); }}Copy the code
  • TransactionState implements the Iterable

    interface, which defines the Requests attribute and provides the enqueue method to add requests to requests

MultiCommand

Claudb 1.7.1 / SRC/main/Java/com/lot/tonivade/claudb/command/transaction/MultiCommand. Java

@Command("multi")
@TxIgnore
public class MultiCommand implements DBCommand {

  private static final String TRASACTION_KEY = "tx";

  @Override
  public RedisToken execute(Database db, Request request) {
    if (!isTxActive(request.getSession())) {
      createTransaction(request.getSession());
      return responseOk();
    } else {
      return error("ERR MULTI calls can not be nested");
    }
  }

  private void createTransaction(Session session) {
    session.putValue(TRASACTION_KEY, new TransactionState());
  }

  private boolean isTxActive(Session session) {
    return session.getValue(TRASACTION_KEY).isPresent();
  }
}
Copy the code
  • MultiCommand implements DBCommand, and its execute method determines isTxActive before createTransaction

ExecCommand

Claudb 1.7.1 / SRC/main/Java/com/lot/tonivade/claudb/command/transaction/ExecCommand. Java

@Command("exec") @TxIgnore public class ExecCommand implements DBCommand { @Override public RedisToken execute(Database db, Request request) { Option<TransactionState> transaction = getTransactionIfExists(request.getSession()); if (transaction.isPresent()) { DBServerContext server = getClauDB(request.getServerContext()); List<RedisToken> responses = new ArrayList<>(); for (Request queuedRequest : transaction.get()) { responses.add(executeCommand(server, queuedRequest)); } return RedisToken.array(responses); } else { return RedisToken.error("ERR EXEC without MULTI"); } } private RedisToken executeCommand(DBServerContext server, Request queuedRequest) { RespCommand command = server.getCommand(queuedRequest.getCommand()); return command.execute(queuedRequest); } private Option<TransactionState> getTransactionIfExists(Session session) { return session.removeValue("tx"); }}Copy the code
  • ExecCommand implements the DBCommand interface. Its execute method uses getTransactionIfExists to get a transaction. If the transaction does not exist, an error is reported. Execute executeCommand(Server, queuedRequest) one by one by iterating through queuedRequest (server, queuedRequest)

DiscardCommand

Claudb 1.7.1 / SRC/main/Java/com/lot/tonivade/claudb/command/transaction/DiscardCommand. Java

@Command("discard") @TxIgnore public class DiscardCommand implements DBCommand { private static final String TX_KEY = "tx"; @Override public RedisToken execute(Database db, Request request) { removeTransactionIfExists(request.getSession()); return RedisToken.responseOk(); } private Option<TransactionState> removeTransactionIfExists(Session session) { return session.removeValue(TX_KEY); }}Copy the code
  • DiscardCommand DBCommand command interface is achieved, the execute method performs removeTransactionIfExists (request) getSession ())

summary

The commands related to claudb transaction are MultiCommand, ExecCommand, and DiscardCommand

doc

  • transaction