Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

Commit

Permalink
change: Improve de-duplication reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
jellysquid3 committed Jun 18, 2021
1 parent 905e7b0 commit 0841d80
Showing 1 changed file with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
import java.util.Objects;

public class DeduplicationCache<T> {
private final ObjectOpenCustomHashSet<T> cache;
private final ObjectOpenCustomHashSet<T> pool;

private int attemptedInsertions = 0;
private int deduplicated = 0;

public DeduplicationCache(Hash.Strategy<T> strategy) {
this.cache = new ObjectOpenCustomHashSet<>(strategy);
this.pool = new ObjectOpenCustomHashSet<>(strategy);
}

public DeduplicationCache() {
this.cache = new ObjectOpenCustomHashSet<>(new Hash.Strategy<T>() {
this.pool = new ObjectOpenCustomHashSet<>(new Hash.Strategy<T>() {
@Override
public int hashCode(T o) {
return Objects.hashCode(o);
Expand All @@ -31,26 +32,25 @@ public boolean equals(T a, T b) {
public synchronized T deduplicate(T item) {
this.attemptedInsertions++;

return this.cache.addOrGet(item);
}
T result = this.pool.addOrGet(item);

public synchronized void clearCache() {
this.attemptedInsertions = 0;
if (result != item) {
this.deduplicated++;
}

this.cache.clear();
return result;
}

public synchronized int getSize() {
return this.cache.size();
}
public synchronized void clearCache() {
this.attemptedInsertions = 0;
this.deduplicated = 0;

public synchronized int getDeduplicatedCount() {
return this.attemptedInsertions - this.cache.size();
this.pool.clear();
}

@Override
public synchronized String toString() {
return String.format("DeduplicationCache ( %d de-duplicated, %d entries )",
this.getDeduplicatedCount(), this.getSize());
return String.format("DeduplicationCache ( %d/%d de-duplicated, %d pooled )",
this.deduplicated, this.attemptedInsertions, this.pool.size());
}
}

0 comments on commit 0841d80

Please sign in to comment.