X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2Futils%2FInMemorySnapshotStore.java;h=662a063788fd522412d778eff71000bfcdaa9507;hb=refs%2Fchanges%2F32%2F83832%2F8;hp=49aec66e724c3f45471d6e213d35cbd4b9d0ae4e;hpb=e1eca73a5ae2ffae8dd78c6fe5281cd2f45d5ef3;p=controller.git diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/utils/InMemorySnapshotStore.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/utils/InMemorySnapshotStore.java index 49aec66e72..662a063788 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/utils/InMemorySnapshotStore.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/utils/InMemorySnapshotStore.java @@ -5,7 +5,6 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.controller.cluster.raft.utils; import akka.dispatch.Futures; @@ -13,7 +12,6 @@ import akka.persistence.SelectedSnapshot; import akka.persistence.SnapshotMetadata; import akka.persistence.SnapshotSelectionCriteria; import akka.persistence.snapshot.japi.SnapshotStore; -import com.google.common.collect.Lists; import com.google.common.util.concurrent.Uninterruptibles; import java.util.ArrayList; import java.util.Collections; @@ -41,13 +39,8 @@ public class InMemorySnapshotStore extends SnapshotStore { private static final Map SNAPSHOT_DELETED_LATCHES = new ConcurrentHashMap<>(); private static Map> snapshots = new ConcurrentHashMap<>(); - public static void addSnapshot(String persistentId, Object snapshot) { - List snapshotList = snapshots.get(persistentId); - - if (snapshotList == null) { - snapshotList = new ArrayList<>(); - snapshots.put(persistentId, snapshotList); - } + public static void addSnapshot(final String persistentId, final Object snapshot) { + List snapshotList = snapshots.computeIfAbsent(persistentId, k -> new ArrayList<>()); synchronized (snapshotList) { snapshotList.add(new StoredSnapshot(new SnapshotMetadata(persistentId, snapshotList.size(), @@ -56,7 +49,7 @@ public class InMemorySnapshotStore extends SnapshotStore { } @SuppressWarnings("unchecked") - public static List getSnapshots(String persistentId, Class type) { + public static List getSnapshots(final String persistentId, final Class type) { List stored = snapshots.get(persistentId); if (stored == null) { return Collections.emptyList(); @@ -64,7 +57,7 @@ public class InMemorySnapshotStore extends SnapshotStore { List retList; synchronized (stored) { - retList = Lists.newArrayListWithCapacity(stored.size()); + retList = new ArrayList<>(stored.size()); for (StoredSnapshot s: stored) { if (type.isInstance(s.data)) { retList.add((T) s.data); @@ -75,19 +68,23 @@ public class InMemorySnapshotStore extends SnapshotStore { return retList; } + public static void clearSnapshotsFor(final String persistenceId) { + snapshots.remove(persistenceId); + } + public static void clear() { snapshots.clear(); } - public static void addSnapshotSavedLatch(String persistenceId) { + public static void addSnapshotSavedLatch(final String persistenceId) { SNAPSHOT_SAVED_LATCHES.put(persistenceId, new CountDownLatch(1)); } - public static void addSnapshotDeletedLatch(String persistenceId) { + public static void addSnapshotDeletedLatch(final String persistenceId) { SNAPSHOT_DELETED_LATCHES.put(persistenceId, new CountDownLatch(1)); } - public static T waitForSavedSnapshot(String persistenceId, Class type) { + public static T waitForSavedSnapshot(final String persistenceId, final Class type) { if (!Uninterruptibles.awaitUninterruptibly(SNAPSHOT_SAVED_LATCHES.get(persistenceId), 5, TimeUnit.SECONDS)) { throw new AssertionError("Snapshot was not saved"); } @@ -95,15 +92,15 @@ public class InMemorySnapshotStore extends SnapshotStore { return getSnapshots(persistenceId, type).get(0); } - public static void waitForDeletedSnapshot(String persistenceId) { + public static void waitForDeletedSnapshot(final String persistenceId) { if (!Uninterruptibles.awaitUninterruptibly(SNAPSHOT_DELETED_LATCHES.get(persistenceId), 5, TimeUnit.SECONDS)) { throw new AssertionError("Snapshot was not deleted"); } } @Override - public Future> doLoadAsync(String persistenceId, - SnapshotSelectionCriteria snapshotSelectionCriteria) { + public Future> doLoadAsync(final String persistenceId, + final SnapshotSelectionCriteria snapshotSelectionCriteria) { List snapshotList = snapshots.get(persistenceId); if (snapshotList == null) { return Futures.successful(Optional.empty()); @@ -122,13 +119,13 @@ public class InMemorySnapshotStore extends SnapshotStore { return Futures.successful(Optional.empty()); } - private static boolean matches(StoredSnapshot snapshot, SnapshotSelectionCriteria criteria) { + private static boolean matches(final StoredSnapshot snapshot, final SnapshotSelectionCriteria criteria) { return snapshot.metadata.sequenceNr() <= criteria.maxSequenceNr() && snapshot.metadata.timestamp() <= criteria.maxTimestamp(); } @Override - public Future doSaveAsync(SnapshotMetadata snapshotMetadata, Object obj) { + public Future doSaveAsync(final SnapshotMetadata snapshotMetadata, final Object obj) { List snapshotList = snapshots.get(snapshotMetadata.persistenceId()); LOG.trace("doSaveAsync: persistentId {}: sequenceNr: {}: timestamp {}: {}", snapshotMetadata.persistenceId(), @@ -151,7 +148,7 @@ public class InMemorySnapshotStore extends SnapshotStore { } @Override - public Future doDeleteAsync(SnapshotMetadata metadata) { + public Future doDeleteAsync(final SnapshotMetadata metadata) { List snapshotList = snapshots.get(metadata.persistenceId()); if (snapshotList != null) { @@ -170,7 +167,7 @@ public class InMemorySnapshotStore extends SnapshotStore { } @Override - public Future doDeleteAsync(String persistenceId, SnapshotSelectionCriteria criteria) { + public Future doDeleteAsync(final String persistenceId, final SnapshotSelectionCriteria criteria) { LOG.trace("doDelete: persistentId {}: maxSequenceNr: {}: maxTimestamp {}", persistenceId, criteria.maxSequenceNr(), criteria.maxTimestamp()); @@ -198,11 +195,11 @@ public class InMemorySnapshotStore extends SnapshotStore { return Futures.successful(null); } - private static class StoredSnapshot { + private static final class StoredSnapshot { private final SnapshotMetadata metadata; private final Object data; - private StoredSnapshot(SnapshotMetadata metadata, Object data) { + StoredSnapshot(final SnapshotMetadata metadata, final Object data) { this.metadata = metadata; this.data = data; }