2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.datastore.messages;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.Preconditions;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import java.util.Optional;
17 import java.util.SortedSet;
18 import java.util.TreeSet;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
21 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
22 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
25 * Message used to batch write, merge, delete modification operations to the ShardTransaction actor.
27 * @author Thomas Pantelis
29 public class BatchedModifications extends MutableCompositeModification {
30 private static final long serialVersionUID = 1L;
32 private boolean ready;
33 private boolean doCommitOnReady;
34 private int totalMessagesSent;
35 private TransactionIdentifier transactionId;
37 private @Nullable SortedSet<String> participatingShardNames;
39 public BatchedModifications() {
42 public BatchedModifications(TransactionIdentifier transactionId, short version) {
44 this.transactionId = requireNonNull(transactionId, "transactionID can't be null");
47 public boolean isReady() {
51 public void setReady(Optional<SortedSet<String>> possibleParticipatingShardNames) {
53 this.participatingShardNames = requireNonNull(possibleParticipatingShardNames).orElse(null);
54 Preconditions.checkArgument(this.participatingShardNames == null || this.participatingShardNames.size() > 1);
57 public void setReady() {
58 setReady(Optional.empty());
61 public Optional<SortedSet<String>> getParticipatingShardNames() {
62 return Optional.ofNullable(participatingShardNames);
65 public boolean isDoCommitOnReady() {
66 return doCommitOnReady;
69 public void setDoCommitOnReady(boolean doCommitOnReady) {
70 this.doCommitOnReady = doCommitOnReady;
73 public int getTotalMessagesSent() {
74 return totalMessagesSent;
77 public void setTotalMessagesSent(int totalMessagesSent) {
78 this.totalMessagesSent = totalMessagesSent;
81 public TransactionIdentifier getTransactionId() {
86 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
87 super.readExternal(in);
88 transactionId = TransactionIdentifier.readFrom(in);
89 ready = in.readBoolean();
90 totalMessagesSent = in.readInt();
91 doCommitOnReady = in.readBoolean();
93 if (getVersion() >= DataStoreVersions.FLUORINE_VERSION) {
94 final int count = in.readInt();
96 SortedSet<String> shardNames = new TreeSet<>();
97 for (int i = 0; i < count; i++) {
98 shardNames.add((String) in.readObject());
101 participatingShardNames = shardNames;
107 public void writeExternal(ObjectOutput out) throws IOException {
108 super.writeExternal(out);
109 transactionId.writeTo(out);
110 out.writeBoolean(ready);
111 out.writeInt(totalMessagesSent);
112 out.writeBoolean(doCommitOnReady);
114 if (getVersion() >= DataStoreVersions.FLUORINE_VERSION) {
115 if (participatingShardNames != null) {
116 out.writeInt(participatingShardNames.size());
117 for (String shardName: participatingShardNames) {
118 out.writeObject(shardName);
127 public String toString() {
128 return "BatchedModifications [transactionId=" + transactionId
129 + ", ready=" + isReady()
130 + ", participatingShardNames=" + participatingShardNames
131 + ", totalMessagesSent=" + totalMessagesSent
132 + ", modifications size=" + getModifications().size() + "]";