-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from dynatrace-oss/readme
Readme
- Loading branch information
Showing
5 changed files
with
177 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/test/java/com/dynatrace/hash4j/consistent/ConsistentHashingDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2023 Dynatrace LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.dynatrace.hash4j.consistent; | ||
|
||
import static java.util.stream.Collectors.groupingBy; | ||
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; | ||
|
||
import com.dynatrace.hash4j.random.PseudoRandomGeneratorProvider; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.LongStream; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ConsistentHashingDemo { | ||
|
||
@Test | ||
void demoJumphash() { | ||
|
||
// create a consistent bucket hasher | ||
ConsistentBucketHasher consistentBucketHasher = | ||
ConsistentHashing.jumpHash(PseudoRandomGeneratorProvider.splitMix64_V1()); | ||
|
||
long[] hashValues = {9184114998275508886L, 7090183756869893925L, -8795772374088297157L}; | ||
|
||
// determine assignment of hash value to 2 buckets | ||
Map<Integer, List<Long>> assignment2Buckets = | ||
LongStream.of(hashValues) | ||
.boxed() | ||
.collect(groupingBy(hash -> consistentBucketHasher.getBucket(hash, 2))); | ||
// gives {0=[7090183756869893925, -8795772374088297157], 1=[9184114998275508886]} | ||
|
||
// determine assignment of hash value to 3 buckets | ||
Map<Integer, List<Long>> assignment3Buckets = | ||
LongStream.of(hashValues) | ||
.boxed() | ||
.collect(groupingBy(hash -> consistentBucketHasher.getBucket(hash, 3))); | ||
// gives {0=[-8795772374088297157], 1=[9184114998275508886], 2=[7090183756869893925]} | ||
// hash value 7090183756869893925 got reassigned from bucket 0 to bucket 2 | ||
// probability of reassignment is equal to 1/3 | ||
|
||
assertThat(assignment2Buckets) | ||
.hasToString("{0=[7090183756869893925, -8795772374088297157], 1=[9184114998275508886]}"); | ||
assertThat(assignment3Buckets) | ||
.hasToString( | ||
"{0=[-8795772374088297157], 1=[9184114998275508886], 2=[7090183756869893925]}"); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/test/java/com/dynatrace/hash4j/file/FileHashingDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2023 Dynatrace LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.dynatrace.hash4j.file; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.dynatrace.hash4j.hashing.HashValue128; | ||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
class FileHashingDemo { | ||
|
||
@Test | ||
void demoImohash(@TempDir Path path) throws IOException { | ||
|
||
// create some file in the given path | ||
File file = path.resolve("test.txt").toFile(); | ||
try (FileWriter fileWriter = new FileWriter(file)) { | ||
fileWriter.write("this is the file content"); | ||
} | ||
|
||
// use ImoHash to hash that file | ||
HashValue128 hash = FileHashing.imohash1_0_2().hashFileTo128Bits(file); | ||
// returns 0xd317f2dad6ea7ae56ff7fdb517e33918 | ||
|
||
assertThat(hash).hasToString("0xd317f2dad6ea7ae56ff7fdb517e33918"); | ||
} | ||
} |