Define DataStoreVersions.MAGNESIUM_VERSION
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / BatchedModifications.java
index 0ab93ebaa675bc882218179b9d6c117661cbc2cd..77d2687ccba2a723dd5041891c8c1d8b4dbde566 100644 (file)
@@ -7,11 +7,18 @@
  */
 package org.opendaylight.controller.cluster.datastore.messages;
 
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.base.Preconditions;
 import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
+import java.util.Optional;
+import java.util.SortedSet;
+import java.util.TreeSet;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
+import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
 
 /**
@@ -25,22 +32,34 @@ public class BatchedModifications extends MutableCompositeModification {
     private boolean ready;
     private boolean doCommitOnReady;
     private int totalMessagesSent;
-    private TransactionIdentifier transactionID;
+    private TransactionIdentifier transactionId;
+
+    private @Nullable SortedSet<String> participatingShardNames;
 
     public BatchedModifications() {
     }
 
-    public BatchedModifications(TransactionIdentifier transactionID, short version) {
+    public BatchedModifications(TransactionIdentifier transactionId, short version) {
         super(version);
-        this.transactionID = Preconditions.checkNotNull(transactionID, "transactionID can't be null");
+        this.transactionId = requireNonNull(transactionId, "transactionID can't be null");
     }
 
     public boolean isReady() {
         return ready;
     }
 
-    public void setReady(boolean ready) {
-        this.ready = ready;
+    public void setReady(Optional<SortedSet<String>> possibleParticipatingShardNames) {
+        this.ready = true;
+        this.participatingShardNames = requireNonNull(possibleParticipatingShardNames).orElse(null);
+        Preconditions.checkArgument(this.participatingShardNames == null || this.participatingShardNames.size() > 1);
+    }
+
+    public void setReady() {
+        setReady(Optional.empty());
+    }
+
+    public Optional<SortedSet<String>> getParticipatingShardNames() {
+        return Optional.ofNullable(participatingShardNames);
     }
 
     public boolean isDoCommitOnReady() {
@@ -59,35 +78,57 @@ public class BatchedModifications extends MutableCompositeModification {
         this.totalMessagesSent = totalMessagesSent;
     }
 
-    public TransactionIdentifier getTransactionID() {
-        return transactionID;
+    public TransactionIdentifier getTransactionId() {
+        return transactionId;
     }
 
-
     @Override
     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
         super.readExternal(in);
-        transactionID = TransactionIdentifier.readFrom(in);
+        transactionId = TransactionIdentifier.readFrom(in);
         ready = in.readBoolean();
         totalMessagesSent = in.readInt();
         doCommitOnReady = in.readBoolean();
+
+        if (getVersion() >= DataStoreVersions.FLUORINE_VERSION) {
+            final int count = in.readInt();
+            if (count != 0) {
+                SortedSet<String> shardNames = new TreeSet<>();
+                for (int i = 0; i < count; i++) {
+                    shardNames.add((String) in.readObject());
+                }
+
+                participatingShardNames = shardNames;
+            }
+        }
     }
 
     @Override
     public void writeExternal(ObjectOutput out) throws IOException {
         super.writeExternal(out);
-        transactionID.writeTo(out);
+        transactionId.writeTo(out);
         out.writeBoolean(ready);
         out.writeInt(totalMessagesSent);
         out.writeBoolean(doCommitOnReady);
+
+        if (getVersion() >= DataStoreVersions.FLUORINE_VERSION) {
+            if (participatingShardNames != null) {
+                out.writeInt(participatingShardNames.size());
+                for (String shardName: participatingShardNames) {
+                    out.writeObject(shardName);
+                }
+            } else {
+                out.writeInt(0);
+            }
+        }
     }
 
     @Override
     public String toString() {
-        StringBuilder builder = new StringBuilder();
-        builder.append("BatchedModifications [transactionID=").append(transactionID).append(", ready=").append(ready)
-            .append(", totalMessagesSent=").append(totalMessagesSent).append(", modifications size=")
-            .append(getModifications().size()).append("]");
-        return builder.toString();
+        return "BatchedModifications [transactionId=" + transactionId
+                + ", ready=" + isReady()
+                + ", participatingShardNames=" + participatingShardNames
+                + ", totalMessagesSent=" + totalMessagesSent
+                + ", modifications size=" + getModifications().size() + "]";
     }
 }