*/
package org.opendaylight.controller.cluster.datastore.persisted;
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Verify.verifyNotNull;
+import static java.util.Objects.requireNonNull;
+
import com.google.common.collect.ImmutableList;
-import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
import java.io.Serializable;
+import java.util.ArrayList;
import java.util.List;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
/**
* Stores a snapshot of the internal state of a data store.
*
* @author Thomas Pantelis
*/
-public class DatastoreSnapshot implements Serializable {
+public final class DatastoreSnapshot implements Serializable {
+ interface SerialForm extends Externalizable {
+
+ DatastoreSnapshot datastoreSnapshot();
+
+ Object readResolve();
+
+ void resolveTo(@NonNull DatastoreSnapshot newDatastoreSnapshot);
+
+ @Override
+ default void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+ final var type = (String)in.readObject();
+ final var snapshot = (ShardManagerSnapshot) in.readObject();
+
+ final int size = in.readInt();
+ var localShardSnapshots = new ArrayList<ShardSnapshot>(size);
+ for (int i = 0; i < size; i++) {
+ localShardSnapshots.add((ShardSnapshot) in.readObject());
+ }
+
+ resolveTo(new DatastoreSnapshot(type, snapshot, localShardSnapshots));
+ }
+
+ @Override
+ default void writeExternal(ObjectOutput out) throws IOException {
+ final var datastoreSnapshot = datastoreSnapshot();
+ out.writeObject(datastoreSnapshot.type);
+ out.writeObject(datastoreSnapshot.shardManagerSnapshot);
+
+ out.writeInt(datastoreSnapshot.shardSnapshots.size());
+ for (ShardSnapshot shardSnapshot: datastoreSnapshot.shardSnapshots) {
+ out.writeObject(shardSnapshot);
+ }
+ }
+ }
+
+ private static final class Proxy implements SerialForm {
+ private static final long serialVersionUID = 1L;
+
+ private DatastoreSnapshot datastoreSnapshot;
+
+ // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
+ // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
+ @SuppressWarnings("checkstyle:RedundantModifier")
+ public Proxy() {
+ // For Externalizable
+ }
+
+ Proxy(final DatastoreSnapshot datastoreSnapshot) {
+ this.datastoreSnapshot = requireNonNull(datastoreSnapshot);
+ }
+
+ @Override
+ public DatastoreSnapshot datastoreSnapshot() {
+ return datastoreSnapshot;
+ }
+
+ @Override
+ public void resolveTo(DatastoreSnapshot newDatastoreSnapshot) {
+ datastoreSnapshot = requireNonNull(newDatastoreSnapshot);
+ }
+
+ @Override
+ public Object readResolve() {
+ return verifyNotNull(datastoreSnapshot);
+ }
+ }
+
private static final long serialVersionUID = 1L;
- private final String type;
- private final byte[] shardManagerSnapshot;
- private final List<ShardSnapshot> shardSnapshots;
+ private final @NonNull String type;
+ private final ShardManagerSnapshot shardManagerSnapshot;
+ private final @NonNull ImmutableList<ShardSnapshot> shardSnapshots;
- @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "Stores a reference to an externally mutable byte[] "
- + "object but this is OK since this class is merely a DTO and does not process byte[] internally. "
- + "Also it would be inefficient to create a return copy as the byte[] could be large.")
- public DatastoreSnapshot(@Nonnull String type, @Nullable byte[] shardManagerSnapshot,
- @Nonnull List<ShardSnapshot> shardSnapshots) {
- this.type = Preconditions.checkNotNull(type);
+ public DatastoreSnapshot(@NonNull String type, @Nullable ShardManagerSnapshot shardManagerSnapshot,
+ @NonNull List<ShardSnapshot> shardSnapshots) {
+ this.type = requireNonNull(type);
this.shardManagerSnapshot = shardManagerSnapshot;
- this.shardSnapshots = ImmutableList.copyOf(Preconditions.checkNotNull(shardSnapshots));
+ this.shardSnapshots = ImmutableList.copyOf(shardSnapshots);
}
- @Nonnull
- public String getType() {
+ public @NonNull String getType() {
return type;
}
- @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Exposes a mutable object stored in a field but "
- + "this is OK since this class is merely a DTO and does not process byte[] internally. "
- + "Also it would be inefficient to create a return copy as the byte[] could be large.")
- @Nullable
- public byte[] getShardManagerSnapshot() {
+ public @Nullable ShardManagerSnapshot getShardManagerSnapshot() {
return shardManagerSnapshot;
}
- @Nonnull
- public List<ShardSnapshot> getShardSnapshots() {
+ public @NonNull List<ShardSnapshot> getShardSnapshots() {
return shardSnapshots;
}
- public static class ShardSnapshot implements Serializable {
+ private Object writeReplace() {
+ return new Proxy(this);
+ }
+
+ public static final class ShardSnapshot implements Serializable {
+ interface SerialForm extends Externalizable {
+
+ ShardSnapshot shardSnapshot();
+
+ Object readResolve();
+
+ void resolveTo(String name, Snapshot snapshot);
+
+ @Override
+ default void writeExternal(ObjectOutput out) throws IOException {
+ final var shardSnapshot = shardSnapshot();
+ out.writeObject(shardSnapshot.name);
+ out.writeObject(shardSnapshot.snapshot);
+ }
+
+ @Override
+ default void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+ resolveTo((String) in.readObject(), (Snapshot) in.readObject());
+ }
+ }
+
+ private static final class Proxy implements SerialForm {
+ private static final long serialVersionUID = 1L;
+
+ private ShardSnapshot shardSnapshot;
+
+ // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
+ // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
+ @SuppressWarnings("checkstyle:RedundantModifier")
+ public Proxy() {
+ // For Externalizable
+ }
+
+ Proxy(final ShardSnapshot shardSnapshot) {
+ this.shardSnapshot = shardSnapshot;
+ }
+
+ @Override
+ public ShardSnapshot shardSnapshot() {
+ return shardSnapshot;
+ }
+
+ @Override
+ public void resolveTo(final String name, final Snapshot snapshot) {
+ shardSnapshot = new ShardSnapshot(name, snapshot);
+ }
+
+ @Override
+ public Object readResolve() {
+ return verifyNotNull(shardSnapshot);
+ }
+ }
+
private static final long serialVersionUID = 1L;
- private final String name;
- private final byte[] snapshot;
+ private final @NonNull String name;
+ private final @NonNull Snapshot snapshot;
- public ShardSnapshot(@Nonnull String name, @Nonnull byte[] snapshot) {
- this.name = Preconditions.checkNotNull(name);
- this.snapshot = Preconditions.checkNotNull(snapshot);
+ public ShardSnapshot(@NonNull String name, @NonNull Snapshot snapshot) {
+ this.name = requireNonNull(name);
+ this.snapshot = requireNonNull(snapshot);
}
- @Nonnull
- public String getName() {
+ public @NonNull String getName() {
return name;
}
- @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Exposes a mutable object stored in a field but "
- + "this is OK since this class is merely a DTO and does not process byte[] internally. "
- + "Also it would be inefficient to create a return copy as the byte[] could be large.")
- @Nonnull
- public byte[] getSnapshot() {
+ public @NonNull Snapshot getSnapshot() {
return snapshot;
}
+
+ private Object writeReplace() {
+ return new Proxy(this);
+ }
}
}