Update DOMStoreThreePhaseCommitCohort design
[mdsal.git] / dom / mdsal-dom-spi / src / main / java / org / opendaylight / mdsal / dom / spi / store / SnapshotBackedReadTransaction.java
index dc1053dc6a83d97b24a60043365d44298c148081..babefecaaffcdbcf1a0e38f6c8bc1e16cc6a681c 100644 (file)
@@ -10,13 +10,15 @@ package org.opendaylight.mdsal.dom.spi.store;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.annotations.Beta;
-import com.google.common.base.Optional;
-import com.google.common.util.concurrent.CheckedFuture;
-import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.FluentFuture;
+import com.google.common.util.concurrent.MoreExecutors;
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
 import org.opendaylight.mdsal.common.api.ReadFailedException;
+import org.opendaylight.yangtools.util.concurrent.FluentFutures;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
+import org.opendaylight.yangtools.yang.data.tree.api.DataTreeSnapshot;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -29,8 +31,15 @@ import org.slf4j.LoggerFactory;
 @Beta
 public final class SnapshotBackedReadTransaction<T> extends
         AbstractDOMStoreTransaction<T> implements DOMStoreReadTransaction, SnapshotBackedTransaction {
-
     private static final Logger LOG = LoggerFactory.getLogger(SnapshotBackedReadTransaction.class);
+
+    @SuppressWarnings("rawtypes")
+    private static final AtomicReferenceFieldUpdater<SnapshotBackedReadTransaction, DataTreeSnapshot> SNAPSHOT_UPDATER =
+            AtomicReferenceFieldUpdater.newUpdater(SnapshotBackedReadTransaction.class, DataTreeSnapshot.class,
+                "stableSnapshot");
+
+    // Guarded by stableSnapshot CAS, hence it does not need to be volatile
+    private TransactionClosePrototype<T> closeImpl;
     private volatile DataTreeSnapshot stableSnapshot;
 
     /**
@@ -40,51 +49,77 @@ public final class SnapshotBackedReadTransaction<T> extends
      * @param debug Enable transaction debugging
      * @param snapshot Snapshot which will be modified.
      */
-    SnapshotBackedReadTransaction(final T identifier, final boolean debug, final DataTreeSnapshot snapshot) {
+    SnapshotBackedReadTransaction(final T identifier, final boolean debug, final DataTreeSnapshot snapshot,
+            final TransactionClosePrototype<T> closeImpl) {
         super(identifier, debug);
         this.stableSnapshot = requireNonNull(snapshot);
+        this.closeImpl = closeImpl;
         LOG.debug("ReadOnly Tx: {} allocated with snapshot {}", identifier, snapshot);
     }
 
     @Override
     public void close() {
+        final DataTreeSnapshot prev = SNAPSHOT_UPDATER.getAndSet(this, null);
+        if (prev == null) {
+            LOG.debug("Store transaction: {} : previously closed", getIdentifier());
+            return;
+        }
+
         LOG.debug("Store transaction: {} : Closed", getIdentifier());
-        stableSnapshot = null;
+        if (closeImpl != null) {
+            closeImpl.transactionClosed(this);
+            closeImpl = null;
+        }
     }
 
     @SuppressWarnings("checkstyle:IllegalCatch")
     @Override
-    public CheckedFuture<Optional<NormalizedNode<?,?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
+    public FluentFuture<Optional<NormalizedNode>> read(final YangInstanceIdentifier path) {
         LOG.debug("Tx: {} Read: {}", getIdentifier(), path);
         requireNonNull(path, "Path must not be null.");
 
         final DataTreeSnapshot snapshot = stableSnapshot;
         if (snapshot == null) {
-            return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
+            return FluentFutures.immediateFailedFluentFuture(new ReadFailedException("Transaction is closed"));
         }
 
         try {
-            return Futures.immediateCheckedFuture(Optional.fromJavaUtil(snapshot.readNode(path)));
+            return FluentFutures.immediateFluentFuture(snapshot.readNode(path));
         } catch (Exception e) {
             LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
-            return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read failed", e));
+            return FluentFutures.immediateFailedFluentFuture(new ReadFailedException("Read failed", e));
         }
     }
 
     @Override
-    public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
+    public FluentFuture<Boolean> exists(final YangInstanceIdentifier path) {
         LOG.debug("Tx: {} Exists: {}", getIdentifier(), path);
         requireNonNull(path, "Path must not be null.");
 
-        try {
-            return Futures.immediateCheckedFuture(read(path).checkedGet().isPresent());
-        } catch (ReadFailedException e) {
-            return Futures.immediateFailedCheckedFuture(e);
-        }
+        return read(path).transform(Optional::isPresent, MoreExecutors.directExecutor());
     }
 
     @Override
-    public java.util.Optional<DataTreeSnapshot> getSnapshot() {
-        return java.util.Optional.ofNullable(stableSnapshot);
+    public Optional<DataTreeSnapshot> getSnapshot() {
+        return Optional.ofNullable(stableSnapshot);
+    }
+
+    /**
+     * Prototype implementation of {@link SnapshotBackedReadTransaction#close()}.
+     *
+     * <p>
+     * This class is intended to be implemented by Transaction factories responsible for allocation
+     * of {@link org.opendaylight.mdsal.dom.spi.store.SnapshotBackedReadTransaction} and
+     * providing underlying logic for applying implementation.
+     *
+     * @param <T> identifier type
+     */
+    public interface TransactionClosePrototype<T> {
+        /**
+         * Called when a transaction is closed. This is not invoked at most once for every transaction.
+         *
+         * @param tx Transaction which got closed.
+         */
+        void transactionClosed(SnapshotBackedReadTransaction<T> tx);
     }
 }