Remove redundant HashSet lookups in filter functions

The appendOnlyDataStoreService and map already have unique keys that
are based on the hash of the payload. This would catch instances
where:

PersistableNetworkPayload
- None: The key is based on ByteArray(payload.getHash()) which is the
        same as this check.

ProtectedStorageEntry
- Cases where multiple PSEs contain payloads that have equivalent
  hashCode(), but different data.toProtoMessage().toByteArray().
  I don't think it is a good idea to keep 2 "unique" methods on
  payloads. This is likely left over from a time when
  Payload hashCode() needed to be different than the hash of
  the payload.
This commit is contained in:
Julian Knutsen 2019-11-21 10:13:20 -08:00
parent 5630b35755
commit caf946dfe0
No known key found for this signature in database
GPG Key ID: D85F536DB3615B2D

View File

@ -263,8 +263,6 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
int maxEntries,
AtomicBoolean outPersistableNetworkPayloadOutputTruncated,
Capabilities peerCapabilities) {
Set<P2PDataStorage.ByteArray> tempLookupSet = new HashSet<>();
Set<P2PDataStorage.ByteArray> excludedKeysAsByteArray = P2PDataStorage.ByteArray.convertBytesSetToByteArraySet(getDataRequest.getExcludedKeys());
AtomicInteger maxSize = new AtomicInteger(maxEntries);
Set<PersistableNetworkPayload> result = this.appendOnlyDataStoreService.getMap().entrySet().stream()
@ -272,10 +270,6 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
.filter(e -> maxSize.decrementAndGet() >= 0)
.map(Map.Entry::getValue)
.filter(persistableNetworkPayload -> shouldTransmitPayloadToPeer(peerCapabilities, persistableNetworkPayload))
.filter(payload -> {
boolean notContained = tempLookupSet.add(new P2PDataStorage.ByteArray(payload.getHash()));
return notContained;
})
.collect(Collectors.toSet());
if (maxSize.get() <= 0)
@ -289,7 +283,6 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
AtomicBoolean outProtectedStorageEntryOutputTruncated,
Capabilities peerCapabilities) {
Set<ProtectedStorageEntry> filteredDataSet = new HashSet<>();
Set<Integer> lookupSet = new HashSet<>();
AtomicInteger maxSize = new AtomicInteger(maxEntries);
Set<P2PDataStorage.ByteArray> excludedKeysAsByteArray = P2PDataStorage.ByteArray.convertBytesSetToByteArraySet(getDataRequest.getExcludedKeys());
@ -315,9 +308,7 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
doAdd = true;
}
if (doAdd) {
boolean notContained = lookupSet.add(protectedStoragePayload.hashCode());
if (notContained)
filteredDataSet.add(protectedStorageEntry);
filteredDataSet.add(protectedStorageEntry);
}
}
return filteredDataSet;