Skip to content

Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance

fix ConcurrentScoreMap.set() calculation of totalCount() + test case

reger39dd24469387
diff --git a/source/net/yacy/cora/sorting/ConcurrentScoreMap.java b/source/net/yacy/cora/sorting/ConcurrentScoreMap.java
index 95b2ce3b8..77304e576 100644
--- a/source/net/yacy/cora/sorting/ConcurrentScoreMap.java
+++ b/source/net/yacy/cora/sorting/ConcurrentScoreMap.java
@@ -133,7 +133,10 @@ public class ConcurrentScoreMap<E> extends AbstractScoreMap<E> implements ScoreM
if (obj == null) return;
// use atomic operations
- this.map.putIfAbsent(obj, new AtomicLong(0));
+ final AtomicLong old = this.map.putIfAbsent(obj, new AtomicLong(0));
+ // adjust overall counter if value replaced
+ if (old != null) this.gcount -= old.longValue(); // must use old befor setting a new value (it's a object reference)
+
this.map.get(obj).set(newScore);
// increase overall counter
diff --git a/test/java/net/yacy/cora/sorting/ConcurrentScoreMapTest.java b/test/java/net/yacy/cora/sorting/ConcurrentScoreMapTest.java
new file mode 100644
index 000000000..443a5436a
--- /dev/null
+++ b/test/java/net/yacy/cora/sorting/ConcurrentScoreMapTest.java
@@ -0,0 +1,34 @@
+
+package net.yacy.cora.sorting;
+
+import java.util.Iterator;
+import static org.junit.Assert.assertEquals;
+import org.junit.Test;
+
+
+public class ConcurrentScoreMapTest {
+
+ /**
+ * Test of totalCount method, of class ConcurrentScoreMap.
+ */
+ @Test
+ public void testTotalCount() {
+ final ConcurrentScoreMap<String> csm = new ConcurrentScoreMap<String>();
+ csm.set("first", 10);
+ csm.set("second", 5);
+ csm.set("third", 13);
+
+ csm.set("first", 100);
+
+ final Iterator<String> it = csm.keys(true);
+ long sum = 0;
+ while (it.hasNext()) {
+ String x = it.next();
+ long val = csm.get(x);
+ sum += val;
+ }
+
+ assertEquals(sum, csm.totalCount());
+ }
+
+}