diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-07-05 16:06:56 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-07-05 16:06:56 +0200 |
commit | 0409e74ad21d1fccf3909f96bda64bacd54b102d (patch) | |
tree | 6c53453bbecc0965004cfac45b8870519c4b98e7 /vespajlib | |
parent | c84211d12966c9a3a7c62a1f0440064f52921d94 (diff) |
Towards transactional application delete
Diffstat (limited to 'vespajlib')
4 files changed, 62 insertions, 34 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/transaction/AbstractTransaction.java b/vespajlib/src/main/java/com/yahoo/transaction/AbstractTransaction.java new file mode 100644 index 00000000000..667e4406277 --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/transaction/AbstractTransaction.java @@ -0,0 +1,50 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.transaction; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Logger; + +/** + * A convenience base transaction class for multi-operation transactions + * which maintains the list of operations to commit and provides a default + * implementation of rollbackOrLog which logs a SEVERE message. + * + * @author bratseth + */ +public abstract class AbstractTransaction<OPERATION extends Transaction.Operation> implements Transaction<OPERATION> { + + private static final Logger log = Logger.getLogger(AbstractTransaction.class.getName()); + private final List<OPERATION> operations = new ArrayList<>(); + + protected AbstractTransaction() { } + + @Override + public Transaction add(OPERATION operation) { + this.operations.add(operation); + return this; + } + + @Override + public Transaction add(List<OPERATION> operations) { + this.operations.addAll(operations); + return this; + } + + @Override + public List<OPERATION> operations() { return operations; } + + /** Default implementations which logs a severe message. Operations should implement toString to use this. */ + @Override + public void rollbackOrLog() { + log.severe("The following operations were incorrectly committed and probably require " + + "manual correction: " + operations()); + } + + /** Default implementation which only clears operations */ + @Override + public void close() { + operations.clear(); + } + +} diff --git a/vespajlib/src/main/java/com/yahoo/transaction/NestedTransaction.java b/vespajlib/src/main/java/com/yahoo/transaction/NestedTransaction.java index 4be0a32ffe8..f437aa8ac19 100644 --- a/vespajlib/src/main/java/com/yahoo/transaction/NestedTransaction.java +++ b/vespajlib/src/main/java/com/yahoo/transaction/NestedTransaction.java @@ -21,9 +21,6 @@ public final class NestedTransaction implements AutoCloseable { /** Nested transactions with ordering constraints, in the order they are added */ private final List<ConstrainedTransaction> transactions = new ArrayList<>(2); - /** Transaction ordering pairs */ - //private final List<OrderingConstraint> transactionOrders = new ArrayList<>(2); - /** A list of (non-transactional) operations to execute after this transaction has committed successfully */ private final List<Runnable> onCommitted = new ArrayList<>(2); diff --git a/vespajlib/src/main/java/com/yahoo/transaction/Transaction.java b/vespajlib/src/main/java/com/yahoo/transaction/Transaction.java index 642438dda0a..e1983e13933 100644 --- a/vespajlib/src/main/java/com/yahoo/transaction/Transaction.java +++ b/vespajlib/src/main/java/com/yahoo/transaction/Transaction.java @@ -4,13 +4,15 @@ package com.yahoo.transaction; import java.util.List; /** - * An interface for building a transaction and committing it. Implementations are required to atomically apply changes + * A transaction against a single system, which may include several operations against the system, + * to be committed as one. + * Implementations are required to atomically apply changes * in the commit step or throw an exception if it fails. * * @author lulf * @author bratseth */ -public interface Transaction extends AutoCloseable { +public interface Transaction<OPERATION extends Transaction.Operation> extends AutoCloseable { /** * Adds an operation to this transaction. Return self for chaining. @@ -18,7 +20,7 @@ public interface Transaction extends AutoCloseable { * @param operation {@link Operation} to append * @return self, for chaining */ - Transaction add(Operation operation); + Transaction add(OPERATION operation); /** * Adds multiple operations to this transaction. Return self for chaining. @@ -26,13 +28,13 @@ public interface Transaction extends AutoCloseable { * @param operation {@link Operation} to append * @return self, for chaining */ - Transaction add(List<Operation> operation); + Transaction add(List<OPERATION> operation); /** * Returns the operations of this. * Ownership of the returned list is transferred to the caller. The ist may be ready only. */ - List<Operation> operations(); + List<OPERATION> operations(); /** * Checks whether or not the transaction is able to commit in its current state and do any transient preparatory @@ -65,8 +67,9 @@ public interface Transaction extends AutoCloseable { /** * Operations that a transaction supports should implement this interface. + * It does not define any methods because the interface to use is a contract between the + * specific transaction and operation type used. */ - public interface Operation { - } + interface Operation { } } diff --git a/vespajlib/src/test/java/com/yahoo/transaction/NestedTransactionTestCase.java b/vespajlib/src/test/java/com/yahoo/transaction/NestedTransactionTestCase.java index d75daa506b4..887c3909f73 100644 --- a/vespajlib/src/test/java/com/yahoo/transaction/NestedTransactionTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/transaction/NestedTransactionTestCase.java @@ -111,30 +111,12 @@ public class NestedTransactionTestCase { } } - private static class MockTransaction implements Transaction { + private static class MockTransaction extends AbstractTransaction { public boolean prepared = false, committed = false, rolledback = false; - private List<Operation> operations = new ArrayList<>(); public MockTransaction(String name) { - operations.add(new MockOperation(name)); - } - - @Override - public Transaction add(Operation operation) { - operations.add(operation); - return this; - } - - @Override - public Transaction add(List<Operation> operation) { - operations.addAll(operation); - return this; - } - - @Override - public List<Operation> operations() { - return operations; + add(new MockOperation(name)); } @Override @@ -156,10 +138,6 @@ public class NestedTransactionTestCase { rolledback = true; } - @Override - public void close() { - } - } private static class MockOperation implements Transaction.Operation { |