Package io.etcd.jetcd

Interface Txn

  • All Known Implementing Classes:
    TxnImpl

    public interface Txn
    Txn is the interface that wraps mini-transactions.

    Usage examples

     
     txn.If(
        new Cmp(KEY, Cmp.Op.GREATER, CmpTarget.value(VALUE)),
        new Cmp(KEY, cmp.Op.EQUAL, CmpTarget.version(2))
     ).Then(
        Op.put(KEY2, VALUE2, PutOption.DEFAULT),
        Op.put(KEY3, VALUE3, PutOption.DEFAULT)
     ).Else(
        Op.put(KEY4, VALUE4, PutOption.DEFAULT),
        Op.put(KEY4, VALUE4, PutOption.DEFAULT)
     ).commit();
     
     

    Txn also supports If, Then, and Else chaining. e.g.

     
     txn.If(
        new Cmp(KEY, Cmp.Op.GREATER, CmpTarget.value(VALUE))
     ).If(
        new Cmp(KEY, cmp.Op.EQUAL, CmpTarget.version(VERSION))
     ).Then(
        Op.put(KEY2, VALUE2, PutOption.DEFAULT)
     ).Then(
        Op.put(KEY3, VALUE3, PutOption.DEFAULT)
     ).Else(
        Op.put(KEY4, VALUE4, PutOption.DEFAULT)
     ).Else(
        Op.put(KEY4, VALUE4, PutOption.DEFAULT)
     ).commit();
     
     
    • Method Detail

      • If

        Txn If​(Cmp... cmps)
        takes a list of comparison. If all comparisons passed in succeed, the operations passed into Then() will be executed. Or the operations passed into Else() will be executed.
        Parameters:
        cmps - the comparisons
        Returns:
        this object
      • Then

        Txn Then​(Op... ops)
        takes a list of operations. The Ops list will be executed, if the comparisons passed in If() succeed.
        Parameters:
        ops - the operations
        Returns:
        this object
      • Else

        Txn Else​(Op... ops)
        takes a list of operations. The Ops list will be executed, if the comparisons passed in If() fail.
        Parameters:
        ops - the operations
        Returns:
        this object
      • commit

        java.util.concurrent.CompletableFuture<TxnResponse> commit()
        tries to commit the transaction.
        Returns:
        a TxnResponse wrapped in CompletableFuture