reduce duplication in ManagedTransactionFactory impls (1/2) 31/75631/4
authorMichael Vorburger <vorburger@redhat.com>
Fri, 31 Aug 2018 16:12:56 +0000 (18:12 +0200)
committerMichael Vorburger <vorburger@redhat.com>
Tue, 4 Sep 2018 13:47:05 +0000 (13:47 +0000)
in ManagedNewTransactionRunnerImpl and ManagedTransactionFactoryImpl

Change-Id: I88a528691511f70c41d1169858747cc88e0a9fcc
Signed-off-by: Michael Vorburger <vorburger@redhat.com>
mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/infra/ManagedNewTransactionRunnerImpl.java
mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/infra/ManagedTransactionFactoryImpl.java

index cbd66c95f447112f047167ed83ea3addc4128413..3f9499e24c45b547decaa0ae7e7a7f2be86f1459 100644 (file)
@@ -74,14 +74,7 @@ public class ManagedNewTransactionRunnerImpl extends ManagedTransactionFactoryIm
         WriteTrackingReadWriteTransaction wrappedTx = new NonSubmitCancelableReadWriteTransaction(realTx);
         try {
             txConsumer.accept(wrappedTx);
-            if (wrappedTx.isWritten()) {
-                // The transaction contains changes, commit it
-                return realTx.submit();
-            } else {
-                // The transaction only handled reads, cancel it
-                realTx.cancel();
-                return Futures.immediateCheckedFuture(null);
-            }
+            return commit(realTx, null, wrappedTx);
         // catch Exception for both the <E extends Exception> thrown by accept() as well as any RuntimeException
         } catch (Exception e) {
             if (!realTx.cancel()) {
@@ -101,14 +94,7 @@ public class ManagedNewTransactionRunnerImpl extends ManagedTransactionFactoryIm
             new WriteTrackingTypedReadWriteTransactionImpl<>(datastoreType, realTx);
         try {
             txConsumer.accept(wrappedTx);
-            if (wrappedTx.isWritten()) {
-                // The transaction contains changes, commit it
-                return realTx.commit().transform(v -> null, MoreExecutors.directExecutor());
-            } else {
-                // The transaction only handled reads, cancel it
-                realTx.cancel();
-                return FluentFuture.from(Futures.immediateFuture(null));
-            }
+            return commit(realTx, null, wrappedTx);
             // catch Exception for both the <E extends Exception> thrown by accept() as well as any RuntimeException
         } catch (Exception e) {
             if (!realTx.cancel()) {
@@ -127,14 +113,7 @@ public class ManagedNewTransactionRunnerImpl extends ManagedTransactionFactoryIm
                 new WriteTrackingTypedReadWriteTransactionImpl<>(datastoreType, realTx);
         try {
             R result = txFunction.apply(wrappedTx);
-            if (wrappedTx.isWritten()) {
-                // The transaction contains changes, commit it
-                return realTx.commit().transform(v -> result, MoreExecutors.directExecutor());
-            } else {
-                // The transaction only handled reads, cancel it
-                realTx.cancel();
-                return FluentFuture.from(Futures.immediateFuture(result));
-            }
+            return commit(realTx, result, wrappedTx);
             // catch Exception for both the <E extends Exception> thrown by accept() as well as any RuntimeException
         } catch (Exception e) {
             if (!realTx.cancel()) {
@@ -161,4 +140,15 @@ public class ManagedNewTransactionRunnerImpl extends ManagedTransactionFactoryIm
             return chainConsumer.apply(new ManagedTransactionChainImpl(realTxChain));
         }
     }
+
+    private <R> FluentFuture<R> commit(ReadWriteTransaction realTx, R result, WriteTrackingTransaction wrappedTx) {
+        if (wrappedTx.isWritten()) {
+            // The transaction contains changes, commit it
+            return realTx.commit().transform(v -> result, MoreExecutors.directExecutor());
+        } else {
+            // The transaction only handled reads, cancel it
+            realTx.cancel();
+            return FluentFuture.from(Futures.immediateFuture(result));
+        }
+    }
 }
index ae82cec493094f3374cc52a2b12cc2ce7604dc88..ca1e60912394ef25ef738002c2de4ff5d38259e6 100644 (file)
@@ -52,8 +52,7 @@ class ManagedTransactionFactoryImpl implements ManagedTransactionFactory {
         ReadWriteTransaction realTx = transactionFactory.newReadWriteTransaction();
         TypedReadWriteTransaction<D> wrappedTx = new TypedReadWriteTransactionImpl<>(datastoreType, realTx);
         try {
-            R result = txFunction.apply(wrappedTx);
-            return realTx.commit().transform(v -> result, MoreExecutors.directExecutor());
+            return commit(realTx, txFunction.apply(wrappedTx));
         } catch (Exception e) {
             // catch Exception for both the <E extends Exception> thrown by accept() as well as any RuntimeException
             if (!realTx.cancel()) {
@@ -83,7 +82,7 @@ class ManagedTransactionFactoryImpl implements ManagedTransactionFactory {
         TypedReadWriteTransaction<D> wrappedTx = new TypedReadWriteTransactionImpl<>(datastoreType, realTx);
         try {
             txConsumer.accept(wrappedTx);
-            return realTx.commit().transform(commitInfo -> null, MoreExecutors.directExecutor());
+            return commit(realTx, null);
             // catch Exception for both the <E extends Exception> thrown by accept() as well as any RuntimeException
         } catch (Exception e) {
             if (!realTx.cancel()) {
@@ -102,7 +101,7 @@ class ManagedTransactionFactoryImpl implements ManagedTransactionFactory {
             new TypedWriteTransactionImpl<>(datastoreType, realTx);
         try {
             txConsumer.accept(wrappedTx);
-            return realTx.commit().transform(commitInfo -> null, MoreExecutors.directExecutor());
+            return commit(realTx, null);
             // catch Exception for both the <E extends Exception> thrown by accept() as well as any RuntimeException
         } catch (Exception e) {
             if (!realTx.cancel()) {
@@ -111,4 +110,8 @@ class ManagedTransactionFactoryImpl implements ManagedTransactionFactory {
             return FluentFuture.from(immediateFailedFuture(e));
         }
     }
+
+    private <R> FluentFuture<R> commit(WriteTransaction realTx, R result) {
+        return realTx.commit().transform(v -> result, MoreExecutors.directExecutor());
+    }
 }