[REFACTOR] removeFromMapAndDataStore can operate on Collections

Minor performance overhead for constructing MapEntry and Collections
of one element, but keeps the code cleaner and all removes can still
use the same logic to remove from map, delete from data store, signal
listeners, etc.

The MapEntry type is used instead of Pair since it will require less
operations when this is eventually used in the removeExpiredEntries path.
This commit is contained in:
Julian Knutsen 2019-11-16 11:29:28 -08:00
parent b281566e14
commit 4f08588717
No known key found for this signature in database
GPG Key ID: D85F536DB3615B2D

View File

@ -68,6 +68,8 @@ import com.google.inject.name.Named;
import javax.inject.Inject;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Maps;
import java.security.KeyPair;
import java.security.PublicKey;
@ -75,6 +77,7 @@ import java.security.PublicKey;
import java.time.Clock;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
@ -643,6 +646,15 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
///////////////////////////////////////////////////////////////////////////////////////////
private void removeFromMapAndDataStore(ProtectedStorageEntry protectedStorageEntry, ByteArray hashOfPayload) {
removeFromMapAndDataStore(Collections.singletonList(Maps.immutableEntry(hashOfPayload, protectedStorageEntry)));
}
private void removeFromMapAndDataStore(
Collection<Map.Entry<ByteArray, ProtectedStorageEntry>> entriesToRemoveWithPayloadHash) {
entriesToRemoveWithPayloadHash.forEach(entryToRemoveWithPayloadHash -> {
ByteArray hashOfPayload = entryToRemoveWithPayloadHash.getKey();
ProtectedStorageEntry protectedStorageEntry = entryToRemoveWithPayloadHash.getValue();
map.remove(hashOfPayload);
hashMapChangedListeners.forEach(e -> e.onRemoved(Collections.singletonList(protectedStorageEntry)));
@ -656,6 +668,7 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
log.info("We cannot remove the protectedStorageEntry from the persistedEntryMap as it does not exist.");
}
}
});
}
private boolean hasSequenceNrIncreased(int newSequenceNumber, ByteArray hashOfData) {