Skip to content

Commit e6a0bbe

Browse files
tomekl007KevinGilmore
authored andcommitted
BAEL-1010 HLL article code (eugenp#2188)
* BAEL-1010 HLL article code * BAEL-1010 moved tolerated difference to a variable * Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-1010_hll # Conflicts: # libraries/pom.xml
1 parent 2f651dd commit e6a0bbe

2 files changed

Lines changed: 78 additions & 5 deletions

File tree

‎libraries/pom.xml‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -485,22 +485,22 @@
485485
<groupId>org.eclipse.jetty.websocket</groupId>
486486
<artifactId>websocket-server</artifactId>
487487
<version>${jetty.version}</version>
488-
</dependency>
488+
</dependency>
489489
<dependency>
490490
<groupId>org.eclipse.jetty.websocket</groupId>
491491
<artifactId>websocket-client</artifactId>
492492
<version>${jetty.version}</version>
493-
</dependency>
493+
</dependency>
494494
<dependency>
495495
<groupId>org.eclipse.jetty.websocket</groupId>
496496
<artifactId>websocket-api</artifactId>
497497
<version>${jetty.version}</version>
498-
</dependency>
498+
</dependency>
499499
<dependency>
500500
<groupId>org.eclipse.jetty.websocket</groupId>
501501
<artifactId>websocket-common</artifactId>
502502
<version>${jetty.version}</version>
503-
</dependency>
503+
</dependency>
504504
<dependency>
505505
<groupId>org.eclipse.jetty</groupId>
506506
<artifactId>jetty-continuation</artifactId>
@@ -510,7 +510,7 @@
510510
<groupId>org.eclipse.jetty</groupId>
511511
<artifactId>jetty-util</artifactId>
512512
<version>${jetty.version}</version>
513-
</dependency>
513+
</dependency>
514514
<dependency>
515515
<groupId>org.seleniumhq.selenium</groupId>
516516
<artifactId>selenium-api</artifactId>
@@ -519,6 +519,11 @@
519519
<type>jar</type>
520520
</dependency>
521521
<!-- /Vaadin -->
522+
<dependency>
523+
<groupId>net.agkn</groupId>
524+
<artifactId>hll</artifactId>
525+
<version>${hll.version}</version>
526+
</dependency>
522527
</dependencies>
523528
<properties>
524529
<multiverse.version>0.7.0</multiverse.version>
@@ -559,6 +564,7 @@
559564
<vaadin.plugin.version>8.0.6</vaadin.plugin.version>
560565
<vaadin.theme>mytheme</vaadin.theme>
561566
<!-- /Vaadin -->
567+
<hll.version>1.6.0</hll.version>
562568
</properties>
563569
<profiles>
564570
<!-- Vaadin -->
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.baeldung.hll;
2+
3+
4+
import com.google.common.hash.HashFunction;
5+
import com.google.common.hash.Hashing;
6+
import net.agkn.hll.HLL;
7+
import org.junit.Test;
8+
9+
import java.util.stream.LongStream;
10+
11+
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
12+
13+
public class HLLUnitTest {
14+
15+
@Test
16+
public void givenHLL_whenAddHugeAmountOfNumbers_thenShouldReturnEstimatedCardinality() {
17+
//given
18+
int numberOfElements = 100_000_000;
19+
int toleratedDifference = 1_000_000;
20+
HashFunction hashFunction = Hashing.murmur3_128();
21+
HLL hll = new HLL(14, 5);
22+
23+
//when
24+
LongStream.range(0, numberOfElements).forEach(element -> {
25+
long hashedValue = hashFunction.newHasher().putLong(element).hash().asLong();
26+
hll.addRaw(hashedValue);
27+
}
28+
);
29+
30+
//then
31+
long cardinality = hll.cardinality();
32+
assertThat(isSimilarTo(cardinality, numberOfElements, toleratedDifference)).isTrue();
33+
}
34+
35+
@Test
36+
public void givenTwoHLLs_whenAddHugeAmountOfNumbers_thenShouldReturnEstimatedCardinalityForUnionOfHLLs() {
37+
//given
38+
int numberOfElements = 100_000_000;
39+
int toleratedDifference = 1_000_000;
40+
HashFunction hashFunction = Hashing.murmur3_128();
41+
HLL firstHll = new HLL(15, 5);
42+
HLL secondHLL = new HLL(15, 5);
43+
44+
//when
45+
LongStream.range(0, numberOfElements).forEach(element -> {
46+
long hashedValue = hashFunction.newHasher().putLong(element).hash().asLong();
47+
firstHll.addRaw(hashedValue);
48+
}
49+
);
50+
51+
LongStream.range(numberOfElements, numberOfElements * 2).forEach(element -> {
52+
long hashedValue = hashFunction.newHasher().putLong(element).hash().asLong();
53+
secondHLL.addRaw(hashedValue);
54+
}
55+
);
56+
57+
//then
58+
firstHll.union(secondHLL);
59+
long cardinality = firstHll.cardinality();
60+
assertThat(isSimilarTo(cardinality, numberOfElements * 2, toleratedDifference)).isTrue();
61+
}
62+
63+
private boolean isSimilarTo(long cardinality, int numberOfElements, int maxToleratedDifference) {
64+
System.out.println(cardinality);
65+
return Math.abs(cardinality - numberOfElements) <= maxToleratedDifference;
66+
}
67+
}

0 commit comments

Comments
 (0)