Skip to content

Commit a687e61

Browse files
committed
Include value count in hashcode
1 parent 1f3c301 commit a687e61

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

libs/exponential-histogram/src/main/java/org/elasticsearch/exponentialhistogram/ExponentialHistogram.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ public abstract class ExponentialHistogram implements Accountable {
102102
*/
103103
public abstract double sum();
104104

105+
/**
106+
* Returns the sum of the counts of all values represented by this histogram.
107+
*
108+
* @return the value count, guaranteed to be zero for empty histograms
109+
*/
110+
public long valueCount() {
111+
return zeroBucket().count() + positiveBuckets().valueCount() + negativeBuckets().valueCount();
112+
}
113+
105114
/**
106115
* Represents a bucket range of an {@link ExponentialHistogram}, either the positive or the negative range.
107116
*/
@@ -163,8 +172,10 @@ public int hashCode() {
163172
int hash = scale();
164173
hash = 31 * hash + Double.hashCode(sum());
165174
hash = 31 * hash + zeroBucket().hashCode();
175+
hash = 31 * hash + Long.hashCode(valueCount());
166176
// we intentionally don't include the hash of the buckets here, because that is likely expensive to compute
167-
// we assume that the sum is a good enough differentiator for most use cases
177+
// instead, we assume that the value count and sum are a good enough approximation in most cases to minimize collisions
178+
// the value count is typically available as a cached value and doesn't involve iterating over all buckets
168179
return hash;
169180
}
170181

libs/exponential-histogram/src/test/java/org/elasticsearch/exponentialhistogram/ExponentialHistogramEqualityTests.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,6 @@ public void testEquality() {
6868
}
6969

7070
public void testHashQuality() {
71-
switch (modification) {
72-
// we intentionally don't include the buckets in the hash, so skip them in tests
73-
case POSITIVE_BUCKETS, NEGATIVE_BUCKETS:
74-
return;
75-
}
76-
7771
ExponentialHistogram histo = randomHistogram();
7872
// of 10 tries, at least one should produce a different hash code
7973
for (int i = 0; i < 10; i++) {

x-pack/plugin/mapper-exponential-histogram/src/main/java/org/elasticsearch/xpack/exponentialhistogram/CompressedExponentialHistogram.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ public double sum() {
5959
return sum;
6060
}
6161

62+
@Override
63+
public long valueCount() {
64+
return valueCount;
65+
}
66+
6267
@Override
6368
public ExponentialHistogram.Buckets positiveBuckets() {
6469
return positiveBuckets;

0 commit comments

Comments
 (0)