diff options
author | Jon Bratseth <jonbratseth@yahoo.com> | 2016-07-08 15:16:01 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-08 15:16:01 +0200 |
commit | a2d774f4d8412862b425de61d7c8ac8d82c6e593 (patch) | |
tree | dc521c2f68afea0d1a137bd97cbd81c6c3da8895 /vespajlib | |
parent | f85ca3c2afdb7e909e7814a6f1f34a8b32d7019c (diff) | |
parent | c990441c379cb20e5049c7dae842b1f03c4624c1 (diff) |
Merge pull request #342 from yahoo/bratseth/transactional-application-remove
Bratseth/transactional application remove
Diffstat (limited to 'vespajlib')
4 files changed, 59 insertions, 30 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..cff02f4da53 --- /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 ordered list of operations to commit and provides a default + * implementation of rollbackOrLog which logs a SEVERE message. + * + * @author bratseth + */ +public abstract class AbstractTransaction implements Transaction { + + 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..bde458cb150 100644 --- a/vespajlib/src/main/java/com/yahoo/transaction/Transaction.java +++ b/vespajlib/src/main/java/com/yahoo/transaction/Transaction.java @@ -4,8 +4,11 @@ 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. + * Operations are performed in the order they are added. * * @author lulf * @author bratseth @@ -65,8 +68,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 { |