Rework BindingRuntimeTypes
[mdsal.git] / dom / mdsal-dom-spi / src / main / java / org / opendaylight / mdsal / dom / spi / store / SnapshotBackedReadTransaction.java
index 7fe884194f45c8645a8a2fcd64ab8923d7515663..0f61bfb8ea9d980007f12483b8b5a83c59a6686e 100644 (file)
@@ -7,14 +7,15 @@
  */
 package org.opendaylight.mdsal.dom.spi.store;
 
-import static com.google.common.base.Preconditions.checkNotNull;
+import static java.util.Objects.requireNonNull;
 
 import com.google.common.annotations.Beta;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-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;
@@ -29,9 +30,16 @@ import org.slf4j.LoggerFactory;
  */
 @Beta
 public final class SnapshotBackedReadTransaction<T> extends
-        AbstractDOMStoreTransaction<T> implements DOMStoreReadTransaction {
-
+        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;
 
     /**
@@ -41,46 +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 = Preconditions.checkNotNull(snapshot);
+        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);
-        checkNotNull(path, "Path must not be null.");
+        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);
-        checkNotNull(path, "Path must not be null.");
+        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 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);
     }
 }