Speed up root overwrite check
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardDataTree.java
index d4dabe5e85caf2d6d1aac564a939f58af8fd2374..b3688a87fa2baf0c71f46d47292cd8818525fc80 100644 (file)
@@ -12,11 +12,11 @@ import static com.google.common.base.Preconditions.checkState;
 import static com.google.common.base.Verify.verify;
 import static com.google.common.base.Verify.verifyNotNull;
 import static java.util.Objects.requireNonNull;
+import static java.util.Objects.requireNonNullElse;
 
 import akka.actor.ActorRef;
 import akka.util.Timeout;
 import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.MoreObjects;
 import com.google.common.base.Stopwatch;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
@@ -389,7 +389,7 @@ public class ShardDataTree extends ShardDataTreeTransactionParent {
 
     private void applyReplicatedCandidate(final CommitTransactionPayload payload)
             throws DataValidationFailedException, IOException {
-        final Entry<TransactionIdentifier, DataTreeCandidateWithVersion> entry = payload.getCandidate();
+        final Entry<TransactionIdentifier, DataTreeCandidateWithVersion> entry = payload.acquireCandidate();
         final TransactionIdentifier identifier = entry.getKey();
         LOG.debug("{}: Applying foreign transaction {}", logContext, identifier);
 
@@ -475,26 +475,22 @@ public class ShardDataTree extends ShardDataTreeTransactionParent {
         }
     }
 
-    private void checkRootOverwrite(DataTreeCandidate candidate) {
+    private void checkRootOverwrite(final DataTreeCandidate candidate) {
         final DatastoreContext datastoreContext = shard.getDatastoreContext();
         if (!datastoreContext.isSnapshotOnRootOverwrite()) {
             return;
         }
 
         if (!datastoreContext.isPersistent()) {
-            return;
-        }
-
-        if (candidate.getRootNode().getModificationType().equals(ModificationType.UNMODIFIED)) {
+            // FIXME: why don't we want a snapshot in non-persistent state?
             return;
         }
 
         // top level container ie "/"
-        if ((candidate.getRootPath().equals(YangInstanceIdentifier.empty())
-                && candidate.getRootNode().getModificationType().equals(ModificationType.WRITE))) {
+        if (candidate.getRootPath().isEmpty()
+                && candidate.getRootNode().getModificationType() == ModificationType.WRITE) {
             LOG.debug("{}: shard root overwritten, enqueuing snapshot", logContext);
             shard.self().tell(new InitiateCaptureSnapshot(), noSender());
-            return;
         }
     }
 
@@ -1284,7 +1280,7 @@ public class ShardDataTree extends ShardDataTreeTransactionParent {
             return false;
         }
 
-        DataTreeTip newTip = MoreObjects.firstNonNull(first.cohort.getCandidate(), dataTree);
+        DataTreeTip newTip = requireNonNullElse(first.cohort.getCandidate(), dataTree);
         while (it.hasNext()) {
             final CommitEntry e = it.next();
             if (cohort.equals(e.cohort)) {
@@ -1297,7 +1293,7 @@ public class ShardDataTree extends ShardDataTreeTransactionParent {
 
                 return true;
             } else {
-                newTip = MoreObjects.firstNonNull(e.cohort.getCandidate(), newTip);
+                newTip = requireNonNullElse(e.cohort.getCandidate(), newTip);
             }
         }