├── CONTRIBUTING.md ├── hashing-api ├── build.gradle ├── src │ └── main │ │ └── java │ │ └── io │ │ └── github │ │ └── ykayacan │ │ └── hashing │ │ └── api │ │ ├── package-info.java │ │ ├── HashFunction.java │ │ ├── Node.java │ │ └── NodeRouter.java └── .gitignore ├── gradle ├── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties └── bintray-publish.gradle ├── hashing-consistent ├── build.gradle ├── src │ ├── main │ │ └── java │ │ │ └── io │ │ │ └── github │ │ │ └── ykayacan │ │ │ └── hashing │ │ │ └── consistent │ │ │ ├── package-info.java │ │ │ ├── VirtualNode.java │ │ │ ├── PhysicalNode.java │ │ │ └── ConsistentNodeRouter.java │ └── test │ │ └── java │ │ └── io │ │ └── github │ │ └── ykayacan │ │ └── hashing │ │ └── consistent │ │ ├── util │ │ ├── StreamUtil.java │ │ └── MurmurHash.java │ │ ├── MurMurHashFunction.java │ │ └── ConsistentNodeRouterTest.java └── .gitignore ├── hashing-rendezvous ├── build.gradle ├── src │ ├── main │ │ └── java │ │ │ └── io │ │ │ └── github │ │ │ └── ykayacan │ │ │ └── hashing │ │ │ └── rendezvous │ │ │ ├── package-info.java │ │ │ ├── strategy │ │ │ ├── package-info.java │ │ │ ├── RendezvousStrategy.java │ │ │ ├── DefaultRendezvousStrategy.java │ │ │ └── WeightedRendezvousStrategy.java │ │ │ ├── WeightedNode.java │ │ │ └── RendezvousNodeRouter.java │ └── test │ │ └── java │ │ └── io │ │ └── github │ │ └── ykayacan │ │ └── hashing │ │ └── rendezvous │ │ ├── util │ │ ├── StreamUtil.java │ │ └── MurmurHash.java │ │ ├── MurMurHashFunction.java │ │ └── RendezvousNodeRouterTest.java └── .gitignore ├── settings.gradle ├── CHANGELOG.md ├── .travis.yml ├── gradle.properties ├── gradlew.bat ├── .gitignore ├── CODE_OF_CONDUCT.md ├── README.md ├── gradlew └── LICENSE /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /hashing-api/build.gradle: -------------------------------------------------------------------------------- 1 | dependencies { 2 | api "org.checkerframework:checker" 3 | } -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ilhan-mstf/hashing-utils/master/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /hashing-consistent/build.gradle: -------------------------------------------------------------------------------- 1 | dependencies { 2 | api project(":hashing-api") 3 | testImplementation "org.junit.jupiter:junit-jupiter" 4 | } -------------------------------------------------------------------------------- /hashing-rendezvous/build.gradle: -------------------------------------------------------------------------------- 1 | dependencies { 2 | api project(":hashing-api") 3 | testImplementation "org.junit.jupiter:junit-jupiter" 4 | } -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'hashing-utils' 2 | 3 | include 'hashing-api' 4 | include 'hashing-consistent' 5 | include 'hashing-rendezvous' -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | ## 0.1.0 (2019-12-09) 6 | 7 | ## 0.1.0 (2019-12-09) 8 | -------------------------------------------------------------------------------- /hashing-api/src/main/java/io/github/ykayacan/hashing/api/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Yasin Sinan Kayacan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | @DefaultQualifier(value = NonNull.class, locations = TypeUseLocation.FIELD) 18 | package io.github.ykayacan.hashing.api; 19 | 20 | import org.checkerframework.checker.nullness.qual.NonNull; 21 | import org.checkerframework.framework.qual.DefaultQualifier; 22 | import org.checkerframework.framework.qual.TypeUseLocation; 23 | -------------------------------------------------------------------------------- /hashing-consistent/src/main/java/io/github/ykayacan/hashing/consistent/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Yasin Sinan Kayacan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | @DefaultQualifier(value = NonNull.class, locations = TypeUseLocation.FIELD) 18 | package io.github.ykayacan.hashing.consistent; 19 | 20 | import org.checkerframework.checker.nullness.qual.NonNull; 21 | import org.checkerframework.framework.qual.DefaultQualifier; 22 | import org.checkerframework.framework.qual.TypeUseLocation; 23 | -------------------------------------------------------------------------------- /hashing-rendezvous/src/main/java/io/github/ykayacan/hashing/rendezvous/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Yasin Sinan Kayacan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | @DefaultQualifier(value = NonNull.class, locations = TypeUseLocation.FIELD) 18 | package io.github.ykayacan.hashing.rendezvous; 19 | 20 | import org.checkerframework.checker.nullness.qual.NonNull; 21 | import org.checkerframework.framework.qual.DefaultQualifier; 22 | import org.checkerframework.framework.qual.TypeUseLocation; 23 | -------------------------------------------------------------------------------- /hashing-consistent/src/test/java/io/github/ykayacan/hashing/consistent/util/StreamUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Yasin Sinan Kayacan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ykayacan.hashing.consistent.util; 18 | 19 | import java.util.stream.IntStream; 20 | 21 | public final class StreamUtil { 22 | 23 | private StreamUtil() {} 24 | 25 | public static IntStream reverseRange(int from, int to) { 26 | return IntStream.range(from, to).map(i -> to - i + from - 1); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /hashing-rendezvous/src/test/java/io/github/ykayacan/hashing/rendezvous/util/StreamUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Yasin Sinan Kayacan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ykayacan.hashing.rendezvous.util; 18 | 19 | import java.util.stream.IntStream; 20 | 21 | public final class StreamUtil { 22 | 23 | private StreamUtil() {} 24 | 25 | public static IntStream reverseRange(int from, int to) { 26 | return IntStream.range(from, to).map(i -> to - i + from - 1); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | 3 | install: skip 4 | 5 | git: 6 | depth: false 7 | 8 | stages: 9 | - name: test 10 | - name: snapshot 11 | if: branch = master AND tag IS blank 12 | - name: release 13 | if: tag IS present 14 | 15 | jobs: 16 | include: 17 | - stage: test 18 | jdk: openjdk8 19 | script: ./gradlew check --no-daemon 20 | - stage: snapshot 21 | jdk: openjdk8 22 | script: skip 23 | deploy: 24 | provider: script 25 | script: ./gradlew artifactoryPublish -x test --no-daemon 26 | - stage: release 27 | jdk: openjdk8 28 | script: skip 29 | deploy: 30 | provider: script 31 | script: ./gradlew bintrayUpload -x test --no-daemon 32 | on: 33 | tags: true 34 | 35 | notifications: 36 | email: false 37 | 38 | before_cache: 39 | - rm -rf $HOME/.gradle/caches/modules-2/modules-2.lock 40 | - rm -rf $HOME/.gradle/caches/*/plugin-resolution/ 41 | 42 | cache: 43 | directories: 44 | - $HOME/.gradle/caches/ 45 | - $HOME/.gradle/wrapper/ 46 | - $HOME/.m2 -------------------------------------------------------------------------------- /hashing-api/src/main/java/io/github/ykayacan/hashing/api/HashFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Yasin Sinan Kayacan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ykayacan.hashing.api; 18 | 19 | /** The base interface to implement Hash function. */ 20 | @FunctionalInterface 21 | public interface HashFunction { 22 | 23 | /** 24 | * Hash String key to long value. 25 | * 26 | * @param key the key 27 | * @return the long The hashed long value 28 | */ 29 | long hash(String key); 30 | } 31 | -------------------------------------------------------------------------------- /hashing-rendezvous/src/main/java/io/github/ykayacan/hashing/rendezvous/strategy/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Yasin Sinan Kayacan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | @DefaultQualifier(value = NonNull.class, locations = TypeUseLocation.FIELD) 18 | package io.github.ykayacan.hashing.rendezvous.strategy; 19 | 20 | import org.checkerframework.checker.nullness.qual.NonNull; 21 | import org.checkerframework.framework.qual.DefaultQualifier; 22 | import org.checkerframework.framework.qual.TypeUseLocation; 23 | -------------------------------------------------------------------------------- /hashing-api/src/main/java/io/github/ykayacan/hashing/api/Node.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Yasin Sinan Kayacan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ykayacan.hashing.api; 18 | 19 | import java.util.Optional; 20 | 21 | /** The interface Node. */ 22 | public interface Node { 23 | 24 | /** 25 | * Gets node id. 26 | * 27 | * @return the node id 28 | */ 29 | String getNodeId(); 30 | 31 | /** 32 | * Gets data. 33 | * 34 | * @return the data 35 | */ 36 | Optional getData(); 37 | } 38 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | org.gradle.parallel=true 2 | org.gradle.caching=true 3 | 4 | GROUP=io.github.ykayacan.hashing 5 | 6 | POM_DESCRIPTION=A basic Java implementation of Consistent Hashing, Rendezvous Hashing and Weighted Rendezvous Hashing. 7 | 8 | POM_URL=https://github.com/ykayacan/hashing-utils 9 | POM_SCM_URL=https://github.com/ykayacan/hashing-utils 10 | POM_SCM_CONNECTION=scm:git:git://github.com/ykayacan/hashing-utils.git 11 | POM_SCM_DEV_CONNECTION=scm:git:ssh://git@github.com/ykayacan/hashing-utils.git 12 | 13 | POM_LICENCE_NAME=The Apache Software License, Version 2.0 14 | POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt 15 | 16 | POM_DEVELOPER_ID=ykayacan 17 | POM_DEVELOPER_NAME=Yasin Sinan Kayacan 18 | 19 | BINTRAY_PUBLISH=true 20 | 21 | BINTRAY_PKG_REPO=hashing-utils 22 | BINTRAY_PKG_LICENSES=Apache-2.0 23 | BINTRAY_PKG_WEBSITE_URL=https://github.com/ykayacan/hashing-utils 24 | BINTRAY_PKG_ISSUE_TRACKER_URL=https://github.com/ykayacan/hashing-utils/issues 25 | BINTRAY_PKG_VCS_URL=https://github.com/ykayacan/hashing-utils.git 26 | BINTRAY_PKG_PUBLIC_DOWNLOAD_NUMBERS=true 27 | BINTRAY_PKG_GITHUB_REPO=ykayacan/hashing-utils 28 | BINTRAY_PKG_LABELS=hashing,consistent,rendezvous -------------------------------------------------------------------------------- /hashing-rendezvous/src/test/java/io/github/ykayacan/hashing/rendezvous/MurMurHashFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Yasin Sinan Kayacan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ykayacan.hashing.rendezvous; 18 | 19 | import io.github.ykayacan.hashing.rendezvous.util.MurmurHash; 20 | import io.github.ykayacan.hashing.api.HashFunction; 21 | 22 | final class MurMurHashFunction implements HashFunction { 23 | 24 | static HashFunction create() { 25 | return new MurMurHashFunction(); 26 | } 27 | 28 | @Override 29 | public long hash(String key) { 30 | return MurmurHash.hash64(key); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /hashing-consistent/src/test/java/io/github/ykayacan/hashing/consistent/MurMurHashFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Yasin Sinan Kayacan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ykayacan.hashing.consistent; 18 | 19 | import io.github.ykayacan.hashing.consistent.util.MurmurHash; 20 | import io.github.ykayacan.hashing.api.HashFunction; 21 | 22 | final class MurMurHashFunction implements HashFunction { 23 | 24 | private MurMurHashFunction() {} 25 | 26 | static HashFunction create() { 27 | return new MurMurHashFunction(); 28 | } 29 | 30 | @Override 31 | public long hash(String key) { 32 | return MurmurHash.hash64(key); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /hashing-rendezvous/src/main/java/io/github/ykayacan/hashing/rendezvous/strategy/RendezvousStrategy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Yasin Sinan Kayacan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ykayacan.hashing.rendezvous.strategy; 18 | 19 | import io.github.ykayacan.hashing.api.HashFunction; 20 | import io.github.ykayacan.hashing.api.Node; 21 | import java.util.Collection; 22 | import java.util.Optional; 23 | 24 | /** 25 | * The interface Rendezvous strategy. 26 | * 27 | * @param the type parameter 28 | */ 29 | public interface RendezvousStrategy { 30 | 31 | /** 32 | * Gets node. 33 | * 34 | * @param key the key 35 | * @param ring the ring 36 | * @param hashFunction the hash function 37 | * @return the node 38 | */ 39 | Optional getNode(String key, Collection ring, HashFunction hashFunction); 40 | } 41 | -------------------------------------------------------------------------------- /hashing-api/src/main/java/io/github/ykayacan/hashing/api/NodeRouter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Yasin Sinan Kayacan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ykayacan.hashing.api; 18 | 19 | import java.util.Optional; 20 | 21 | /** 22 | * The base interface for {@link NodeRouter} 23 | * 24 | * @param the {@link Node} parameter 25 | */ 26 | public interface NodeRouter { 27 | 28 | /** 29 | * Returns node for given nodeId. 30 | * 31 | * @param nodeId Any string value 32 | * @return Node assigned to the nodeId given as an argument 33 | * @throws NullPointerException if nodeId value is null 34 | */ 35 | Optional getNode(String nodeId); 36 | 37 | /** 38 | * @param node node to be added 39 | * @throws NullPointerException if {@code nodeId} is null 40 | */ 41 | void addNode(N node); 42 | 43 | /** 44 | * @param nodes nodes to be added 45 | * @throws NullPointerException if {@code nodes} is null 46 | */ 47 | default void addNodes(Iterable nodes) { 48 | nodes.forEach(this::addNode); 49 | } 50 | 51 | /** 52 | * @param nodeId nodeId to be removed 53 | * @throws NullPointerException if {@code nodeId} is null 54 | */ 55 | void removeNode(String nodeId); 56 | 57 | /** 58 | * Returns same instance of {@link NodeRouter}. 59 | * 60 | * @param nodeIds nodeIds to be removed 61 | * @throws NullPointerException if {@code nodeIds} is null 62 | */ 63 | default void removeNodes(Iterable nodeIds) { 64 | nodeIds.forEach(this::removeNode); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /hashing-rendezvous/src/main/java/io/github/ykayacan/hashing/rendezvous/strategy/DefaultRendezvousStrategy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Yasin Sinan Kayacan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ykayacan.hashing.rendezvous.strategy; 18 | 19 | import io.github.ykayacan.hashing.rendezvous.WeightedNode; 20 | import io.github.ykayacan.hashing.api.HashFunction; 21 | import java.util.Collection; 22 | import java.util.Objects; 23 | import java.util.Optional; 24 | 25 | /** 26 | * The type Default rendezvous strategy. 27 | * 28 | * @param the type parameter 29 | */ 30 | public class DefaultRendezvousStrategy implements RendezvousStrategy { 31 | 32 | private DefaultRendezvousStrategy() {} 33 | 34 | /** 35 | * Create rendezvous strategy. 36 | * 37 | * @param the type parameter 38 | * @return the rendezvous strategy 39 | */ 40 | public static RendezvousStrategy create() { 41 | return new DefaultRendezvousStrategy<>(); 42 | } 43 | 44 | @Override 45 | public Optional getNode(String key, Collection ring, HashFunction hashFunction) { 46 | Objects.requireNonNull(key); 47 | Objects.requireNonNull(ring); 48 | Objects.requireNonNull(hashFunction); 49 | 50 | if (ring.isEmpty()) { 51 | return Optional.empty(); 52 | } 53 | 54 | double highestScore = Long.MIN_VALUE; 55 | N champion = null; 56 | for (N node : ring) { 57 | double score = hashFunction.hash(node.getNodeId() + ":" + key); 58 | if (score > highestScore) { 59 | champion = node; 60 | highestScore = score; 61 | } 62 | } 63 | 64 | return Optional.ofNullable(champion); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /hashing-rendezvous/src/main/java/io/github/ykayacan/hashing/rendezvous/strategy/WeightedRendezvousStrategy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Yasin Sinan Kayacan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ykayacan.hashing.rendezvous.strategy; 18 | 19 | import io.github.ykayacan.hashing.rendezvous.WeightedNode; 20 | import io.github.ykayacan.hashing.api.HashFunction; 21 | import java.util.Collection; 22 | import java.util.Objects; 23 | import java.util.Optional; 24 | 25 | /** 26 | * The type Weighted rendezvous strategy. 27 | * 28 | * @param the type parameter 29 | */ 30 | public class WeightedRendezvousStrategy implements RendezvousStrategy { 31 | 32 | private static final long FTO = (0xFF_FF_FF_FF_FF_FF_FF_FFL >> (64 - 53)); 33 | private static final double FTZ = (double) (1L << 53); 34 | 35 | private WeightedRendezvousStrategy() {} 36 | 37 | private static double toDouble(long hash) { 38 | return (hash & FTO) / FTZ; 39 | } 40 | 41 | private static double computeWeightedScore(WeightedNode node, HashFunction hashFunction) { 42 | long hash = hashFunction.hash(node.getNodeId()); 43 | double score = 1.0 / -Math.log(toDouble(hash)); 44 | return node.getWeight() * score; 45 | } 46 | 47 | /** 48 | * Create rendezvous strategy. 49 | * 50 | * @param the type parameter 51 | * @return the rendezvous strategy 52 | */ 53 | public static RendezvousStrategy create() { 54 | return new WeightedRendezvousStrategy<>(); 55 | } 56 | 57 | @Override 58 | public Optional getNode(String key, Collection ring, HashFunction hashFunction) { 59 | Objects.requireNonNull(key); 60 | Objects.requireNonNull(ring); 61 | Objects.requireNonNull(hashFunction); 62 | 63 | if (ring.isEmpty()) { 64 | return Optional.empty(); 65 | } 66 | 67 | double highestScore = -1; 68 | N champion = null; 69 | for (N node : ring) { 70 | double score = computeWeightedScore(node, hashFunction); 71 | if (score > highestScore) { 72 | champion = node; 73 | highestScore = score; 74 | } 75 | } 76 | 77 | return Optional.ofNullable(champion); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io/api/macos,gradle,intellij+iml 2 | # Edit at https://www.gitignore.io/?templates=macos,gradle,intellij+iml 3 | 4 | ### Intellij+iml ### 5 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 6 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 7 | 8 | # User-specific stuff 9 | .idea/ 10 | 11 | # Sensitive or high-churn files 12 | .idea/**/dataSources/ 13 | .idea/**/dataSources.ids 14 | .idea/**/dataSources.local.xml 15 | .idea/**/sqlDataSources.xml 16 | .idea/**/dynamic.xml 17 | .idea/**/uiDesigner.xml 18 | .idea/**/dbnavigator.xml 19 | 20 | # Gradle and Maven with auto-import 21 | # When using Gradle or Maven with auto-import, you should exclude module files, 22 | # since they will be recreated, and may cause churn. Uncomment if using 23 | # auto-import. 24 | # .idea/modules.xml 25 | # .idea/*.iml 26 | # .idea/modules 27 | 28 | # CMake 29 | cmake-build-*/ 30 | 31 | # Mongo Explorer plugin 32 | .idea/**/mongoSettings.xml 33 | 34 | # File-based project format 35 | *.iws 36 | 37 | # IntelliJ 38 | out/ 39 | 40 | # mpeltonen/sbt-idea plugin 41 | .idea_modules/ 42 | 43 | # JIRA plugin 44 | atlassian-ide-plugin.xml 45 | 46 | # Cursive Clojure plugin 47 | .idea/replstate.xml 48 | 49 | # Crashlytics plugin (for Android Studio and IntelliJ) 50 | com_crashlytics_export_strings.xml 51 | crashlytics.properties 52 | crashlytics-build.properties 53 | fabric.properties 54 | 55 | # Editor-based Rest Client 56 | .idea/httpRequests 57 | 58 | # Android studio 3.1+ serialized cache file 59 | .idea/caches/build_file_checksums.ser 60 | 61 | ### Intellij+iml Patch ### 62 | # Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023 63 | 64 | *.iml 65 | modules.xml 66 | .idea/misc.xml 67 | *.ipr 68 | 69 | ### macOS ### 70 | # General 71 | .DS_Store 72 | .AppleDouble 73 | .LSOverride 74 | 75 | # Icon must end with two \r 76 | Icon 77 | 78 | # Thumbnails 79 | ._* 80 | 81 | # Files that might appear in the root of a volume 82 | .DocumentRevisions-V100 83 | .fseventsd 84 | .Spotlight-V100 85 | .TemporaryItems 86 | .Trashes 87 | .VolumeIcon.icns 88 | .com.apple.timemachine.donotpresent 89 | 90 | # Directories potentially created on remote AFP share 91 | .AppleDB 92 | .AppleDesktop 93 | Network Trash Folder 94 | Temporary Items 95 | .apdisk 96 | 97 | ### Gradle ### 98 | .gradle 99 | # Ignore Gradle GUI config 100 | gradle-app.setting 101 | 102 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 103 | !gradle-wrapper.jar 104 | 105 | # Cache of project 106 | .gradletasknamecache 107 | 108 | # # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 109 | # gradle/wrapper/gradle-wrapper.properties 110 | 111 | ### Gradle Patch ### 112 | **/build/ 113 | 114 | # End of https://www.gitignore.io/api/macos,gradle,intellij+iml 115 | 116 | publish/bintray.properties -------------------------------------------------------------------------------- /hashing-consistent/src/main/java/io/github/ykayacan/hashing/consistent/VirtualNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Yasin Sinan Kayacan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ykayacan.hashing.consistent; 18 | 19 | import io.github.ykayacan.hashing.api.Node; 20 | import java.util.Objects; 21 | 22 | /** 23 | * The type Virtual node. 24 | * 25 | * @param the type parameter 26 | */ 27 | final class VirtualNode { 28 | private final N physicalNode; 29 | private final int replicaIndex; 30 | private final String nodeId; 31 | 32 | private VirtualNode(N physicalNode, int replicaIndex) { 33 | this.physicalNode = physicalNode; 34 | this.replicaIndex = replicaIndex; 35 | nodeId = physicalNode.getNodeId() + "-" + replicaIndex; 36 | } 37 | 38 | /** 39 | * Create virtual node. 40 | * 41 | * @param the type parameter 42 | * @param physicalNode the physical node 43 | * @param replicaIndex the replica index 44 | * @return the virtual node 45 | */ 46 | static VirtualNode create(N physicalNode, int replicaIndex) { 47 | return new VirtualNode<>(physicalNode, replicaIndex); 48 | } 49 | 50 | /** 51 | * Gets node id. 52 | * 53 | * @return the node id 54 | */ 55 | String getNodeId() { 56 | return nodeId; 57 | } 58 | 59 | /** 60 | * Gets physical node. 61 | * 62 | * @return the physical node 63 | */ 64 | N getPhysicalNode() { 65 | return physicalNode; 66 | } 67 | 68 | /** 69 | * Checks if current node is virtual node of given physical node id. 70 | * 71 | * @param nodeId the physical nodeId 72 | * @return the boolean 73 | */ 74 | boolean isVirtualNodeOf(String nodeId) { 75 | return physicalNode.getNodeId().equals(nodeId); 76 | } 77 | 78 | @Override 79 | public boolean equals(Object o) { 80 | if (this == o) { 81 | return true; 82 | } 83 | if (o == null || getClass() != o.getClass()) { 84 | return false; 85 | } 86 | VirtualNode that = (VirtualNode) o; 87 | return replicaIndex == that.replicaIndex && Objects.equals(physicalNode, that.physicalNode); 88 | } 89 | 90 | @Override 91 | public int hashCode() { 92 | return Objects.hash(physicalNode, replicaIndex); 93 | } 94 | 95 | @Override 96 | public String toString() { 97 | return "VirtualNode{" 98 | + "physicalNode=" 99 | + physicalNode 100 | + ", replicaIndex=" 101 | + replicaIndex 102 | + ", nodeId='" 103 | + nodeId 104 | + '\'' 105 | + '}'; 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /hashing-api/.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io/api/macos,gradle,intellij+iml 2 | # Edit at https://www.gitignore.io/?templates=macos,gradle,intellij+iml 3 | 4 | ### Intellij+iml ### 5 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 6 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 7 | 8 | # User-specific stuff 9 | .idea/**/workspace.xml 10 | .idea/**/tasks.xml 11 | .idea/**/usage.statistics.xml 12 | .idea/**/dictionaries 13 | .idea/**/shelf 14 | 15 | # Generated files 16 | .idea/**/contentModel.xml 17 | 18 | # Sensitive or high-churn files 19 | .idea/**/dataSources/ 20 | .idea/**/dataSources.ids 21 | .idea/**/dataSources.local.xml 22 | .idea/**/sqlDataSources.xml 23 | .idea/**/dynamic.xml 24 | .idea/**/uiDesigner.xml 25 | .idea/**/dbnavigator.xml 26 | 27 | # Gradle 28 | .idea/**/gradle.xml 29 | .idea/**/libraries 30 | 31 | # Gradle and Maven with auto-import 32 | # When using Gradle or Maven with auto-import, you should exclude module files, 33 | # since they will be recreated, and may cause churn. Uncomment if using 34 | # auto-import. 35 | # .idea/modules.xml 36 | # .idea/*.iml 37 | # .idea/modules 38 | 39 | # CMake 40 | cmake-build-*/ 41 | 42 | # Mongo Explorer plugin 43 | .idea/**/mongoSettings.xml 44 | 45 | # File-based project format 46 | *.iws 47 | 48 | # IntelliJ 49 | out/ 50 | 51 | # mpeltonen/sbt-idea plugin 52 | .idea_modules/ 53 | 54 | # JIRA plugin 55 | atlassian-ide-plugin.xml 56 | 57 | # Cursive Clojure plugin 58 | .idea/replstate.xml 59 | 60 | # Crashlytics plugin (for Android Studio and IntelliJ) 61 | com_crashlytics_export_strings.xml 62 | crashlytics.properties 63 | crashlytics-build.properties 64 | fabric.properties 65 | 66 | # Editor-based Rest Client 67 | .idea/httpRequests 68 | 69 | # Android studio 3.1+ serialized cache file 70 | .idea/caches/build_file_checksums.ser 71 | 72 | ### Intellij+iml Patch ### 73 | # Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023 74 | 75 | *.iml 76 | modules.xml 77 | .idea/misc.xml 78 | *.ipr 79 | 80 | ### macOS ### 81 | # General 82 | .DS_Store 83 | .AppleDouble 84 | .LSOverride 85 | 86 | # Icon must end with two \r 87 | Icon 88 | 89 | # Thumbnails 90 | ._* 91 | 92 | # Files that might appear in the root of a volume 93 | .DocumentRevisions-V100 94 | .fseventsd 95 | .Spotlight-V100 96 | .TemporaryItems 97 | .Trashes 98 | .VolumeIcon.icns 99 | .com.apple.timemachine.donotpresent 100 | 101 | # Directories potentially created on remote AFP share 102 | .AppleDB 103 | .AppleDesktop 104 | Network Trash Folder 105 | Temporary Items 106 | .apdisk 107 | 108 | ### Gradle ### 109 | .gradle 110 | /build/ 111 | 112 | # Ignore Gradle GUI config 113 | gradle-app.setting 114 | 115 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 116 | !gradle-wrapper.jar 117 | 118 | # Cache of project 119 | .gradletasknamecache 120 | 121 | # # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 122 | # gradle/wrapper/gradle-wrapper.properties 123 | 124 | ### Gradle Patch ### 125 | **/build/ 126 | 127 | # End of https://www.gitignore.io/api/macos,gradle,intellij+iml -------------------------------------------------------------------------------- /hashing-consistent/.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io/api/macos,gradle,intellij+iml 2 | # Edit at https://www.gitignore.io/?templates=macos,gradle,intellij+iml 3 | 4 | ### Intellij+iml ### 5 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 6 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 7 | 8 | # User-specific stuff 9 | .idea/**/workspace.xml 10 | .idea/**/tasks.xml 11 | .idea/**/usage.statistics.xml 12 | .idea/**/dictionaries 13 | .idea/**/shelf 14 | 15 | # Generated files 16 | .idea/**/contentModel.xml 17 | 18 | # Sensitive or high-churn files 19 | .idea/**/dataSources/ 20 | .idea/**/dataSources.ids 21 | .idea/**/dataSources.local.xml 22 | .idea/**/sqlDataSources.xml 23 | .idea/**/dynamic.xml 24 | .idea/**/uiDesigner.xml 25 | .idea/**/dbnavigator.xml 26 | 27 | # Gradle 28 | .idea/**/gradle.xml 29 | .idea/**/libraries 30 | 31 | # Gradle and Maven with auto-import 32 | # When using Gradle or Maven with auto-import, you should exclude module files, 33 | # since they will be recreated, and may cause churn. Uncomment if using 34 | # auto-import. 35 | # .idea/modules.xml 36 | # .idea/*.iml 37 | # .idea/modules 38 | 39 | # CMake 40 | cmake-build-*/ 41 | 42 | # Mongo Explorer plugin 43 | .idea/**/mongoSettings.xml 44 | 45 | # File-based project format 46 | *.iws 47 | 48 | # IntelliJ 49 | out/ 50 | 51 | # mpeltonen/sbt-idea plugin 52 | .idea_modules/ 53 | 54 | # JIRA plugin 55 | atlassian-ide-plugin.xml 56 | 57 | # Cursive Clojure plugin 58 | .idea/replstate.xml 59 | 60 | # Crashlytics plugin (for Android Studio and IntelliJ) 61 | com_crashlytics_export_strings.xml 62 | crashlytics.properties 63 | crashlytics-build.properties 64 | fabric.properties 65 | 66 | # Editor-based Rest Client 67 | .idea/httpRequests 68 | 69 | # Android studio 3.1+ serialized cache file 70 | .idea/caches/build_file_checksums.ser 71 | 72 | ### Intellij+iml Patch ### 73 | # Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023 74 | 75 | *.iml 76 | modules.xml 77 | .idea/misc.xml 78 | *.ipr 79 | 80 | ### macOS ### 81 | # General 82 | .DS_Store 83 | .AppleDouble 84 | .LSOverride 85 | 86 | # Icon must end with two \r 87 | Icon 88 | 89 | # Thumbnails 90 | ._* 91 | 92 | # Files that might appear in the root of a volume 93 | .DocumentRevisions-V100 94 | .fseventsd 95 | .Spotlight-V100 96 | .TemporaryItems 97 | .Trashes 98 | .VolumeIcon.icns 99 | .com.apple.timemachine.donotpresent 100 | 101 | # Directories potentially created on remote AFP share 102 | .AppleDB 103 | .AppleDesktop 104 | Network Trash Folder 105 | Temporary Items 106 | .apdisk 107 | 108 | ### Gradle ### 109 | .gradle 110 | /build/ 111 | 112 | # Ignore Gradle GUI config 113 | gradle-app.setting 114 | 115 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 116 | !gradle-wrapper.jar 117 | 118 | # Cache of project 119 | .gradletasknamecache 120 | 121 | # # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 122 | # gradle/wrapper/gradle-wrapper.properties 123 | 124 | ### Gradle Patch ### 125 | **/build/ 126 | 127 | # End of https://www.gitignore.io/api/macos,gradle,intellij+iml -------------------------------------------------------------------------------- /hashing-rendezvous/.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io/api/macos,gradle,intellij+iml 2 | # Edit at https://www.gitignore.io/?templates=macos,gradle,intellij+iml 3 | 4 | ### Intellij+iml ### 5 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 6 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 7 | 8 | # User-specific stuff 9 | .idea/**/workspace.xml 10 | .idea/**/tasks.xml 11 | .idea/**/usage.statistics.xml 12 | .idea/**/dictionaries 13 | .idea/**/shelf 14 | 15 | # Generated files 16 | .idea/**/contentModel.xml 17 | 18 | # Sensitive or high-churn files 19 | .idea/**/dataSources/ 20 | .idea/**/dataSources.ids 21 | .idea/**/dataSources.local.xml 22 | .idea/**/sqlDataSources.xml 23 | .idea/**/dynamic.xml 24 | .idea/**/uiDesigner.xml 25 | .idea/**/dbnavigator.xml 26 | 27 | # Gradle 28 | .idea/**/gradle.xml 29 | .idea/**/libraries 30 | 31 | # Gradle and Maven with auto-import 32 | # When using Gradle or Maven with auto-import, you should exclude module files, 33 | # since they will be recreated, and may cause churn. Uncomment if using 34 | # auto-import. 35 | # .idea/modules.xml 36 | # .idea/*.iml 37 | # .idea/modules 38 | 39 | # CMake 40 | cmake-build-*/ 41 | 42 | # Mongo Explorer plugin 43 | .idea/**/mongoSettings.xml 44 | 45 | # File-based project format 46 | *.iws 47 | 48 | # IntelliJ 49 | out/ 50 | 51 | # mpeltonen/sbt-idea plugin 52 | .idea_modules/ 53 | 54 | # JIRA plugin 55 | atlassian-ide-plugin.xml 56 | 57 | # Cursive Clojure plugin 58 | .idea/replstate.xml 59 | 60 | # Crashlytics plugin (for Android Studio and IntelliJ) 61 | com_crashlytics_export_strings.xml 62 | crashlytics.properties 63 | crashlytics-build.properties 64 | fabric.properties 65 | 66 | # Editor-based Rest Client 67 | .idea/httpRequests 68 | 69 | # Android studio 3.1+ serialized cache file 70 | .idea/caches/build_file_checksums.ser 71 | 72 | ### Intellij+iml Patch ### 73 | # Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023 74 | 75 | *.iml 76 | modules.xml 77 | .idea/misc.xml 78 | *.ipr 79 | 80 | ### macOS ### 81 | # General 82 | .DS_Store 83 | .AppleDouble 84 | .LSOverride 85 | 86 | # Icon must end with two \r 87 | Icon 88 | 89 | # Thumbnails 90 | ._* 91 | 92 | # Files that might appear in the root of a volume 93 | .DocumentRevisions-V100 94 | .fseventsd 95 | .Spotlight-V100 96 | .TemporaryItems 97 | .Trashes 98 | .VolumeIcon.icns 99 | .com.apple.timemachine.donotpresent 100 | 101 | # Directories potentially created on remote AFP share 102 | .AppleDB 103 | .AppleDesktop 104 | Network Trash Folder 105 | Temporary Items 106 | .apdisk 107 | 108 | ### Gradle ### 109 | .gradle 110 | /build/ 111 | 112 | # Ignore Gradle GUI config 113 | gradle-app.setting 114 | 115 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 116 | !gradle-wrapper.jar 117 | 118 | # Cache of project 119 | .gradletasknamecache 120 | 121 | # # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 122 | # gradle/wrapper/gradle-wrapper.properties 123 | 124 | ### Gradle Patch ### 125 | **/build/ 126 | 127 | # End of https://www.gitignore.io/api/macos,gradle,intellij+iml -------------------------------------------------------------------------------- /hashing-consistent/src/main/java/io/github/ykayacan/hashing/consistent/PhysicalNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Yasin Sinan Kayacan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ykayacan.hashing.consistent; 18 | 19 | import io.github.ykayacan.hashing.api.Node; 20 | import java.util.Objects; 21 | import java.util.Optional; 22 | import org.checkerframework.checker.nullness.qual.Nullable; 23 | 24 | /** The type Physical node. */ 25 | public class PhysicalNode implements Node { 26 | 27 | private final String nodeId; 28 | @Nullable private final Object data; 29 | 30 | /** 31 | * Instantiates a new Physical node. 32 | * 33 | * @param nodeId the node id 34 | * @param data the data 35 | */ 36 | private PhysicalNode(String nodeId, @Nullable Object data) { 37 | this.nodeId = nodeId; 38 | this.data = data; 39 | } 40 | 41 | public static PhysicalNode of(String nodeId) { 42 | return newBuilder(nodeId).build(); 43 | } 44 | 45 | public static Builder newBuilder(String nodeId) { 46 | return new Builder(nodeId); 47 | } 48 | 49 | @Override 50 | public String getNodeId() { 51 | return nodeId; 52 | } 53 | 54 | @Override 55 | public Optional getData() { 56 | return Optional.ofNullable(data); 57 | } 58 | 59 | @Override 60 | public boolean equals(Object o) { 61 | if (this == o) { 62 | return true; 63 | } 64 | if (!(o instanceof PhysicalNode)) { 65 | return false; 66 | } 67 | PhysicalNode that = (PhysicalNode) o; 68 | return Objects.equals(nodeId, that.nodeId) && Objects.equals(data, that.data); 69 | } 70 | 71 | @Override 72 | public int hashCode() { 73 | return Objects.hash(nodeId, data); 74 | } 75 | 76 | @Override 77 | public String toString() { 78 | return "PhysicalNode{" + "nodeId='" + nodeId + '\'' + ", data=" + data + '}'; 79 | } 80 | 81 | /** The type Builder. */ 82 | public static final class Builder { 83 | 84 | private String nodeId; 85 | @Nullable private Object data; 86 | 87 | private Builder(String nodeId) { 88 | Objects.requireNonNull(nodeId); 89 | this.nodeId = nodeId; 90 | } 91 | 92 | /** 93 | * Data builder. 94 | * 95 | * @param data the data 96 | * @return the builder 97 | */ 98 | public Builder data(@Nullable Object data) { 99 | this.data = data; 100 | return this; 101 | } 102 | 103 | /** 104 | * Build physical node. 105 | * 106 | * @return the physical node 107 | */ 108 | public PhysicalNode build() { 109 | return new PhysicalNode(nodeId, data); 110 | } 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /gradle/bintray-publish.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'maven-publish' 2 | apply plugin: 'com.jfrog.bintray' 3 | apply plugin: 'com.jfrog.artifactory' 4 | apply plugin: 'io.wusa.semver-git-plugin' 5 | 6 | semver { 7 | nextVersion = "none" 8 | } 9 | 10 | group = property('GROUP') 11 | version = semver.info.version 12 | 13 | java { 14 | withJavadocJar() 15 | withSourcesJar() 16 | } 17 | 18 | publishing { 19 | publications { 20 | maven(MavenPublication) { 21 | from components.java 22 | 23 | pom { 24 | name = project.name 25 | description = property('POM_DESCRIPTION') 26 | url = property('POM_URL') 27 | licenses { 28 | license { 29 | name = property('POM_LICENCE_NAME') 30 | url = property('POM_LICENCE_URL') 31 | } 32 | } 33 | developers { 34 | developer { 35 | id = property('POM_DEVELOPER_ID') 36 | name = property('POM_DEVELOPER_NAME') 37 | } 38 | } 39 | scm { 40 | url = property('POM_SCM_URL') 41 | connection = property('POM_SCM_CONNECTION') 42 | developerConnection = property('POM_SCM_DEV_CONNECTION') 43 | } 44 | } 45 | } 46 | } 47 | repositories { 48 | maven { 49 | url = 'https://oss.jfrog.org' 50 | credentials { 51 | username System.getenv('BINTRAY_USER') 52 | password System.getenv('BINTRAY_KEY') 53 | } 54 | } 55 | } 56 | } 57 | 58 | bintray { 59 | user = System.getenv('BINTRAY_USER') 60 | key = System.getenv('BINTRAY_KEY') 61 | publications = ['maven'] 62 | publish = property('BINTRAY_PUBLISH') 63 | pkg { 64 | repo = property('BINTRAY_PKG_REPO') 65 | name = project.name 66 | licenses = property('BINTRAY_PKG_LICENSES').split(',') 67 | websiteUrl = property('BINTRAY_PKG_WEBSITE_URL') 68 | issueTrackerUrl = property('BINTRAY_PKG_ISSUE_TRACKER_URL') 69 | vcsUrl = property('BINTRAY_PKG_VCS_URL') 70 | publicDownloadNumbers = property('BINTRAY_PKG_PUBLIC_DOWNLOAD_NUMBERS') 71 | githubRepo = property('BINTRAY_PKG_GITHUB_REPO') 72 | labels = property('BINTRAY_PKG_LABELS').split(',') 73 | version { 74 | name = project.version 75 | desc = project.description 76 | vcsTag = "v" + project.version 77 | released = new Date() 78 | } 79 | } 80 | } 81 | 82 | artifactory { 83 | contextUrl = 'https://oss.jfrog.org/artifactory' 84 | publish { 85 | repository { 86 | repoKey = 'oss-snapshot-local' 87 | username = System.getenv('BINTRAY_USER') 88 | password = System.getenv('BINTRAY_KEY') 89 | } 90 | defaults { 91 | publications('maven') 92 | publishArtifacts = true 93 | publishPom = true 94 | } 95 | } 96 | resolve { 97 | repoKey = 'jcenter' 98 | } 99 | } 100 | 101 | javadoc { 102 | if (JavaVersion.current().isJava9Compatible()) { 103 | options.addBooleanOption('html5', true) 104 | } 105 | } -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at yasinsinan707@gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq -------------------------------------------------------------------------------- /hashing-rendezvous/src/main/java/io/github/ykayacan/hashing/rendezvous/WeightedNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Yasin Sinan Kayacan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ykayacan.hashing.rendezvous; 18 | 19 | import io.github.ykayacan.hashing.api.Node; 20 | import java.util.Objects; 21 | import java.util.Optional; 22 | import org.checkerframework.checker.nullness.qual.Nullable; 23 | 24 | /** The type Weighted node. */ 25 | public class WeightedNode implements Node { 26 | 27 | private final String nodeId; 28 | @Nullable private final Object data; 29 | private final int weight; 30 | 31 | /** 32 | * Instantiates a new Weighted node. 33 | * 34 | * @param nodeId the node id 35 | * @param weight the weight 36 | * @param data the data 37 | */ 38 | protected WeightedNode(String nodeId, int weight, @Nullable Object data) { 39 | this.nodeId = nodeId; 40 | this.data = data; 41 | this.weight = weight; 42 | } 43 | 44 | public static WeightedNode of(String nodeId) { 45 | return newBuilder(nodeId).build(); 46 | } 47 | 48 | public static Builder newBuilder(String nodeId) { 49 | return new Builder(nodeId); 50 | } 51 | 52 | @Override 53 | public String getNodeId() { 54 | return nodeId; 55 | } 56 | 57 | @Override 58 | public Optional getData() { 59 | return Optional.ofNullable(data); 60 | } 61 | 62 | /** 63 | * Gets weight. 64 | * 65 | * @return the weight 66 | */ 67 | public int getWeight() { 68 | return weight; 69 | } 70 | 71 | @Override 72 | public boolean equals(Object o) { 73 | if (this == o) { 74 | return true; 75 | } 76 | if (!(o instanceof WeightedNode)) { 77 | return false; 78 | } 79 | WeightedNode that = (WeightedNode) o; 80 | return weight == that.weight 81 | && Objects.equals(nodeId, that.nodeId) 82 | && Objects.equals(data, that.data); 83 | } 84 | 85 | @Override 86 | public int hashCode() { 87 | return Objects.hash(nodeId, data, weight); 88 | } 89 | 90 | @Override 91 | public String toString() { 92 | return "WeightedNode{" 93 | + "nodeId='" 94 | + nodeId 95 | + '\'' 96 | + ", data=" 97 | + data 98 | + ", weight=" 99 | + weight 100 | + '}'; 101 | } 102 | 103 | /** The type Builder. */ 104 | public static class Builder { 105 | 106 | private String nodeId; 107 | @Nullable private Object data; 108 | private int weight; 109 | 110 | private Builder(String nodeId) { 111 | this.nodeId = nodeId; 112 | } 113 | 114 | /** 115 | * Data builder. 116 | * 117 | * @param data the data 118 | * @return the builder 119 | */ 120 | public Builder data(@Nullable Object data) { 121 | this.data = data; 122 | return this; 123 | } 124 | 125 | /** 126 | * Weight builder. 127 | * 128 | * @param weight the weight 129 | * @return the builder 130 | */ 131 | public Builder weight(int weight) { 132 | this.weight = weight; 133 | return this; 134 | } 135 | 136 | /** 137 | * Build weighted node. 138 | * 139 | * @return the weighted node 140 | */ 141 | public WeightedNode build() { 142 | return new WeightedNode(nodeId, weight, data); 143 | } 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /hashing-consistent/src/main/java/io/github/ykayacan/hashing/consistent/ConsistentNodeRouter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Yasin Sinan Kayacan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ykayacan.hashing.consistent; 18 | 19 | import io.github.ykayacan.hashing.api.HashFunction; 20 | import io.github.ykayacan.hashing.api.NodeRouter; 21 | import java.util.Collection; 22 | import java.util.Collections; 23 | import java.util.NavigableMap; 24 | import java.util.Objects; 25 | import java.util.Optional; 26 | import java.util.concurrent.ConcurrentSkipListMap; 27 | import org.checkerframework.checker.index.qual.Positive; 28 | import org.checkerframework.checker.nullness.qual.NonNull; 29 | 30 | public class ConsistentNodeRouter implements NodeRouter { 31 | 32 | /** All the current nodes in the pool */ 33 | private final NavigableMap> ring; 34 | 35 | private final HashFunction hashFunction; 36 | 37 | private final int replicaCount; 38 | 39 | private ConsistentNodeRouter( 40 | Collection initialNodes, int replicaCount, HashFunction hashFunction) { 41 | Objects.requireNonNull(initialNodes); 42 | Objects.requireNonNull(hashFunction); 43 | 44 | if (replicaCount < 0) { 45 | throw new IllegalArgumentException("Illegal partition count: " + replicaCount); 46 | } 47 | 48 | this.ring = new ConcurrentSkipListMap<>(); 49 | this.replicaCount = replicaCount; 50 | this.hashFunction = hashFunction; 51 | initialNodes.forEach(this::addNode); 52 | } 53 | 54 | /** 55 | * Create node router. 56 | * 57 | * @param the type parameter 58 | * @param replicaCount the replica count 59 | * @param hashFunction the hash function 60 | * @return the node router 61 | */ 62 | public static NodeRouter create( 63 | @Positive int replicaCount, HashFunction hashFunction) { 64 | return create(Collections.emptyList(), replicaCount, hashFunction); 65 | } 66 | 67 | /** 68 | * Create node router. 69 | * 70 | * @param the type parameter 71 | * @param initialNodes the initial nodes 72 | * @param replicaCount the replica count 73 | * @param hashFunction the hash function 74 | * @return the node router 75 | */ 76 | public static NodeRouter create( 77 | Collection initialNodes, int replicaCount, HashFunction hashFunction) { 78 | return new ConsistentNodeRouter<>(initialNodes, replicaCount, hashFunction); 79 | } 80 | 81 | @Override 82 | public Optional getNode(@NonNull String nodeId) { 83 | Objects.requireNonNull(nodeId); 84 | 85 | if (ring.isEmpty()) { 86 | return Optional.empty(); 87 | } 88 | 89 | Long nodeHash = ring.ceilingKey(hashFunction.hash(nodeId)); 90 | return Optional.ofNullable(ring.get(nodeHash)).map(VirtualNode::getPhysicalNode); 91 | } 92 | 93 | @Override 94 | public void addNode(N node) { 95 | Objects.requireNonNull(node); 96 | 97 | for (int i = 0; i < replicaCount; i++) { 98 | VirtualNode virtualNode = VirtualNode.create(node, i); 99 | long hash = hashFunction.hash(virtualNode.getNodeId()); 100 | ring.put(hash, virtualNode); 101 | } 102 | } 103 | 104 | @Override 105 | public void removeNode(String nodeId) { 106 | Objects.requireNonNull(nodeId); 107 | 108 | ring.values().removeIf(virtualNode -> virtualNode.isVirtualNodeOf(nodeId)); 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hashing Utils 2 | 3 | [![Build Status](https://travis-ci.com/ykayacan/hashing-utils.svg?branch=master)](https://travis-ci.com/ykayacan/hashing-utils) 4 | ![GitHub](https://img.shields.io/github/license/ykayacan/hashing-utils) 5 | 6 | ## Table of Contents 7 | + [About](#about) 8 | + [Getting Started](#getting_started) 9 | + [Usage](#usage) 10 | + [Contributing](CONTRIBUTING.md) 11 | 12 | ## About 13 | A basic Java implementation of [Consistent Hashing](https://en.wikipedia.org/wiki/Consistent_hashing), 14 | [Rendezvous Hashing](https://en.wikipedia.org/wiki/Rendezvous_hashing) and Weighted Rendezvous Hashing. 15 | 16 | ## Getting Started 17 | 18 | ### Latest Stable Releases 19 | ![Bintray](https://img.shields.io/bintray/v/ykayacan/hashing-utils/hashing-api?label=hashing-api) 20 | ![Bintray](https://img.shields.io/bintray/v/ykayacan/hashing-utils/hashing-consistent?label=hashing-consistent) 21 | ![Bintray](https://img.shields.io/bintray/v/ykayacan/hashing-utils/hashing-rendezvous?label=hashing-rendezvous) 22 | 23 | #### Gradle 24 | 25 | ```groovy 26 | repositories { 27 | jcenter() 28 | } 29 | 30 | dependencies { 31 | implementation 'io.github.ykayacan.hashing:hashing-api:LATEST_VERSION' 32 | implementation 'io.github.ykayacan.hashing:hashing-consistent:LATEST_VERSION' 33 | implementation 'io.github.ykayacan.hashing:hashing-rendezvous:LATEST_VERSION' 34 | } 35 | ``` 36 | 37 | #### Maven 38 | 39 | ```xml 40 | 41 | 42 | jcenter 43 | https://jcenter.bintray.com 44 | 45 | 46 | 47 | 48 | 49 | io.github.ykayacan.hashing 50 | hashing-api 51 | LATEST_VERSION 52 | 53 | 54 | 55 | io.github.ykayacan.hashing 56 | hashing-consistent 57 | LATEST_VERSION 58 | 59 | 60 | 61 | io.github.ykayacan.hashing 62 | hashing-rendezvous 63 | LATEST_VERSION 64 | 65 | 66 | ``` 67 | 68 | ### Snapshots 69 | 70 | You can access the latest snapshot by adding the repository `https://oss.jfrog.org/artifactory/oss-snapshot-local` 71 | to your build. 72 | 73 | Snapshots of the development version are available in [Jfrog's snapshots repository](https://oss.jfrog.org/list/oss-snapshot-local/io/github/ykayacan/hashing). 74 | 75 | ## Usage 76 | 77 | #### Consistent Hashing 78 | 79 | ``` 80 | NodeRouter router = 81 | ConsistentNodeRouter.create(15, MurMurHashFunction.create()); 82 | 83 | List initialNodes = 84 | Arrays.asList(PhysicalNode.of("node1"), PhysicalNode.of("node2")); 85 | 86 | router.addNodes(initialNodes); 87 | 88 | // get 89 | Optional nodeOpt = router.getNode("node1"); 90 | 91 | // add 92 | router.addNode(PhysicalNode.of("node3")); 93 | 94 | // remove 95 | router.removeNode("node1"); 96 | ``` 97 | 98 | #### Rendezvous Hashing 99 | 100 | ``` 101 | NodeRouter router = RendezvousNodeRouter.create( 102 | MurMurHashFunction.create(), DefaultRendezvousStrategy.create()); 103 | 104 | List initialNodes = 105 | Arrays.asList(WeightedNode.of("node1"), WeightedNode.of("node2")); 106 | 107 | router.addNodes(initialNodes); 108 | 109 | // get 110 | Optional nodeOpt = router.getNode("node1"); 111 | 112 | // add 113 | router.addNode(WeightedNode.of("node3")); 114 | 115 | // remove 116 | router.removeNode("node1"); 117 | ``` 118 | 119 | ## License 120 | 121 | ```text 122 | Copyright 2019 Yasin Sinan Kayacan 123 | 124 | Licensed under the Apache License, Version 2.0 (the "License"); 125 | you may not use this file except in compliance with the License. 126 | You may obtain a copy of the License at 127 | 128 | http://www.apache.org/licenses/LICENSE-2.0 129 | 130 | Unless required by applicable law or agreed to in writing, software 131 | distributed under the License is distributed on an "AS IS" BASIS, 132 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 133 | See the License for the specific language governing permissions and 134 | limitations under the License. 135 | ``` 136 | -------------------------------------------------------------------------------- /hashing-consistent/src/test/java/io/github/ykayacan/hashing/consistent/ConsistentNodeRouterTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Yasin Sinan Kayacan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ykayacan.hashing.consistent; 18 | 19 | import static org.junit.jupiter.api.Assertions.assertEquals; 20 | import static org.junit.jupiter.api.Assertions.assertFalse; 21 | import static org.junit.jupiter.api.Assertions.assertNotEquals; 22 | import static org.junit.jupiter.api.Assertions.assertTrue; 23 | 24 | import io.github.ykayacan.hashing.api.NodeRouter; 25 | import io.github.ykayacan.hashing.consistent.util.StreamUtil; 26 | import java.util.Arrays; 27 | import java.util.HashSet; 28 | import java.util.Optional; 29 | import java.util.Random; 30 | import java.util.Set; 31 | import java.util.stream.IntStream; 32 | import org.junit.jupiter.api.Test; 33 | 34 | class ConsistentNodeRouterTest { 35 | 36 | @Test 37 | void testEmpty() { 38 | NodeRouter router = createConsistentRouter(); 39 | assertFalse(router.getNode("key").isPresent()); 40 | } 41 | 42 | /** Ensure the same node returned for same key after a large change to the pool of nodes */ 43 | @Test 44 | void testConsistentAfterRemove() { 45 | NodeRouter router = createConsistentRouter(); 46 | IntStream.range(0, 1000) 47 | .parallel() 48 | .mapToObj(index -> PhysicalNode.of("node" + index)) 49 | .forEach(router::addNode); 50 | 51 | Optional node = router.getNode("key"); 52 | 53 | Random random = new Random(); 54 | IntStream.range(0, 250) 55 | .mapToObj(value -> "node" + random.nextInt(1000)) 56 | .filter(nodeId -> node.isPresent() && !nodeId.equals(node.get().getNodeId())) 57 | .forEach(router::removeNode); 58 | 59 | assertEquals(node, router.getNode("key")); 60 | } 61 | 62 | /** Ensure that a new node returned after deleted */ 63 | @Test 64 | void testPreviousDeleted() { 65 | NodeRouter router = createConsistentRouter(); 66 | router.addNodes(Arrays.asList(PhysicalNode.of("node1"), PhysicalNode.of("node2"))); 67 | 68 | Optional node = router.getNode("key"); 69 | node.ifPresent(physicalNode -> router.removeNode(physicalNode.getNodeId())); 70 | 71 | Set set = new HashSet<>(); 72 | set.add("node1"); 73 | set.add("node2"); 74 | 75 | router 76 | .getNode("key") 77 | .ifPresent(physicalNode -> assertTrue(set.contains(physicalNode.getNodeId()))); 78 | 79 | assertNotEquals(node, router.getNode("key")); 80 | } 81 | 82 | /** Ensure same node will still be returned if removed/readded */ 83 | @Test 84 | void testReAdd() { 85 | NodeRouter router = createConsistentRouter(); 86 | router.addNodes(Arrays.asList(PhysicalNode.of("node1"), PhysicalNode.of("node2"))); 87 | 88 | Optional node = router.getNode("key"); 89 | node.ifPresent( 90 | physicalNode -> { 91 | router.removeNode(physicalNode.getNodeId()); 92 | router.addNode(physicalNode); 93 | }); 94 | assertEquals(node, router.getNode("key")); 95 | } 96 | 97 | /** Ensure 2 hashes if have nodes added in different order will have same results */ 98 | @Test 99 | void testDifferentOrder() { 100 | NodeRouter router1 = createConsistentRouter(); 101 | NodeRouter router2 = createConsistentRouter(); 102 | 103 | IntStream.range(0, 1000) 104 | .parallel() 105 | .mapToObj(index -> PhysicalNode.of("node" + index)) 106 | .forEach(router1::addNode); 107 | StreamUtil.reverseRange(0, 1000) 108 | .parallel() 109 | .mapToObj(index -> PhysicalNode.of("node" + index)) 110 | .forEach(router2::addNode); 111 | 112 | assertEquals(router2.getNode("key"), router1.getNode("key")); 113 | } 114 | 115 | private NodeRouter createConsistentRouter() { 116 | return ConsistentNodeRouter.create(15, MurMurHashFunction.create()); 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /hashing-rendezvous/src/main/java/io/github/ykayacan/hashing/rendezvous/RendezvousNodeRouter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Yasin Sinan Kayacan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ykayacan.hashing.rendezvous; 18 | 19 | import io.github.ykayacan.hashing.rendezvous.strategy.RendezvousStrategy; 20 | import io.github.ykayacan.hashing.api.HashFunction; 21 | import io.github.ykayacan.hashing.api.NodeRouter; 22 | import java.util.Collection; 23 | import java.util.Collections; 24 | import java.util.Objects; 25 | import java.util.Optional; 26 | import java.util.Set; 27 | import java.util.concurrent.ConcurrentHashMap; 28 | 29 | /** 30 | * A high performance thread safe implementation of Rendezvous (Highest Random Weight, HRW) hashing 31 | * is an algorithm that allows clients to achieve distributed agreement on which node (or proxy) a 32 | * given * key is to be placed in. This implementation has the following properties. * 33 | * 34 | *
    35 | *
  • Non-blocking reads : Determining which node a key belongs to is always non-blocking. Adding 36 | * and removing ring however blocks each other * 37 | *
  • Low overhead: providing using a hash function of low overhead * 38 | *
  • Load balancing: Since the hash function is randomizing, each of the n ring is equally 39 | * likely to receive the key K. Loads are uniform across the sites. * 40 | *
  • High hit rate: Since all clients agree on placing an key K into the same node N , each 41 | * fetch or placement of K into N yields the maximum utility in terms of hit rate. The key K 42 | * will always be found unless it is evicted by some replacement algorithm at N. * 43 | *
  • Minimal disruption: When a node is removed, only the keys mapped to that node need to be 44 | * remapped and they will be distributed evenly * 45 | *
46 | * 47 | * source: https://en.wikipedia.org/wiki/Rendezvous_hashing 48 | * 49 | * @param the type parameter 50 | */ 51 | public class RendezvousNodeRouter implements NodeRouter { 52 | 53 | /** All the current ring in the pool */ 54 | private final Set ring; 55 | 56 | private final HashFunction hashFunction; 57 | 58 | private final RendezvousStrategy strategy; 59 | 60 | private RendezvousNodeRouter( 61 | Collection initialNodes, HashFunction hashFunction, RendezvousStrategy strategy) { 62 | Objects.requireNonNull(initialNodes); 63 | Objects.requireNonNull(hashFunction); 64 | Objects.requireNonNull(strategy); 65 | 66 | this.ring = ConcurrentHashMap.newKeySet(); 67 | this.hashFunction = hashFunction; 68 | this.strategy = strategy; 69 | 70 | ring.addAll(initialNodes); 71 | } 72 | 73 | /** 74 | * Create node router. 75 | * 76 | * @param the type parameter 77 | * @param hashFunction the hash function 78 | * @param strategy the strategy 79 | * @return the node router 80 | */ 81 | public static NodeRouter create( 82 | HashFunction hashFunction, RendezvousStrategy strategy) { 83 | return create(Collections.emptyList(), hashFunction, strategy); 84 | } 85 | 86 | /** 87 | * Create node router. 88 | * 89 | * @param the type parameter 90 | * @param initialNodes the initial nodes 91 | * @param hashFunction the hash function 92 | * @param strategy the strategy 93 | * @return the node router 94 | */ 95 | public static NodeRouter create( 96 | Collection initialNodes, HashFunction hashFunction, RendezvousStrategy strategy) { 97 | return new RendezvousNodeRouter<>(initialNodes, hashFunction, strategy); 98 | } 99 | 100 | @Override 101 | public Optional getNode(String nodeId) { 102 | return strategy.getNode(nodeId, ring, hashFunction); 103 | } 104 | 105 | @Override 106 | public void addNode(N node) { 107 | Objects.requireNonNull(node); 108 | 109 | ring.add(node); 110 | } 111 | 112 | @Override 113 | public void removeNode(String nodeId) { 114 | Objects.requireNonNull(nodeId); 115 | 116 | ring.removeIf(node -> node.getNodeId().equals(nodeId)); 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /hashing-rendezvous/src/test/java/io/github/ykayacan/hashing/rendezvous/RendezvousNodeRouterTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Yasin Sinan Kayacan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ykayacan.hashing.rendezvous; 18 | 19 | import static org.junit.jupiter.api.Assertions.assertEquals; 20 | import static org.junit.jupiter.api.Assertions.assertFalse; 21 | import static org.junit.jupiter.api.Assertions.assertNotEquals; 22 | import static org.junit.jupiter.api.Assertions.assertTrue; 23 | 24 | import io.github.ykayacan.hashing.api.NodeRouter; 25 | import io.github.ykayacan.hashing.rendezvous.strategy.DefaultRendezvousStrategy; 26 | import io.github.ykayacan.hashing.rendezvous.util.StreamUtil; 27 | import java.util.Arrays; 28 | import java.util.HashSet; 29 | import java.util.Optional; 30 | import java.util.Random; 31 | import java.util.Set; 32 | import java.util.stream.IntStream; 33 | import org.junit.jupiter.api.Test; 34 | 35 | class RendezvousNodeRouterTest { 36 | 37 | @Test 38 | void testEmpty() { 39 | NodeRouter router = createRendezvousRouter(); 40 | assertFalse(router.getNode("key").isPresent()); 41 | } 42 | 43 | /** Ensure the same node returned for same key after a large change to the pool of nodes */ 44 | @Test 45 | void testConsistentAfterRemove() { 46 | NodeRouter router = createRendezvousRouter(); 47 | IntStream.range(0, 1000) 48 | .parallel() 49 | .mapToObj(index -> WeightedNode.of("node" + index)) 50 | .forEach(router::addNode); 51 | 52 | Optional node = router.getNode("key"); 53 | 54 | Random random = new Random(); 55 | for (int i = 0; i < 250; i++) { 56 | String nodeId = "node" + random.nextInt(1000); 57 | if (node.isPresent() && !nodeId.equals(node.get().getNodeId())) { 58 | router.removeNode(nodeId); 59 | } 60 | } 61 | 62 | assertEquals(node, router.getNode("key")); 63 | } 64 | 65 | /** Ensure that a new node returned after deleted */ 66 | @Test 67 | void testPreviousDeleted() { 68 | NodeRouter router = createRendezvousRouter(); 69 | router.addNodes(Arrays.asList(WeightedNode.of("node1"), WeightedNode.of("node2"))); 70 | 71 | Optional node = router.getNode("key"); 72 | node.ifPresent(weightedNode -> router.removeNode(weightedNode.getNodeId())); 73 | 74 | Set set = new HashSet<>(); 75 | set.add("node1"); 76 | set.add("node2"); 77 | 78 | router 79 | .getNode("key") 80 | .ifPresent(weightedNode -> assertTrue(set.contains(weightedNode.getNodeId()))); 81 | 82 | assertNotEquals(node, router.getNode("key")); 83 | } 84 | 85 | /** Ensure same node will still be returned if removed/readded */ 86 | @Test 87 | void testReAdd() { 88 | NodeRouter router = createRendezvousRouter(); 89 | router.addNodes(Arrays.asList(WeightedNode.of("node1"), WeightedNode.of("node2"))); 90 | 91 | Optional node = router.getNode("key"); 92 | node.ifPresent( 93 | weightedNode -> { 94 | router.removeNode(weightedNode.getNodeId()); 95 | router.addNode(weightedNode); 96 | }); 97 | assertEquals(node, router.getNode("key")); 98 | } 99 | 100 | /** Ensure 2 hashes if have nodes added in different order will have same results */ 101 | @Test 102 | void testDifferentOrder() { 103 | NodeRouter router1 = createRendezvousRouter(); 104 | NodeRouter router2 = createRendezvousRouter(); 105 | 106 | IntStream.range(0, 1000) 107 | .parallel() 108 | .mapToObj(index -> WeightedNode.of("node" + index)) 109 | .forEach(router1::addNode); 110 | StreamUtil.reverseRange(0, 1000) 111 | .parallel() 112 | .mapToObj(index -> WeightedNode.of("node" + index)) 113 | .forEach(router2::addNode); 114 | 115 | assertEquals(router2.getNode("key"), router1.getNode("key")); 116 | } 117 | 118 | private NodeRouter createRendezvousRouter() { 119 | return RendezvousNodeRouter.create( 120 | MurMurHashFunction.create(), DefaultRendezvousStrategy.create()); 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /hashing-consistent/src/test/java/io/github/ykayacan/hashing/consistent/util/MurmurHash.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Yasin Sinan Kayacan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ykayacan.hashing.consistent.util; 18 | 19 | /** 20 | * This is a very fast, non-cryptographic hash suitable for general hash-based lookup. See 21 | * http://murmurhash.googlepages.com/ for more details. 22 | * 23 | *

24 | * 25 | *

The C version of MurmurHash 2.0 found at that site was ported to Java by Andrzej Bialecki (ab 26 | * at getopt org). 27 | */ 28 | public class MurmurHash { 29 | 30 | public static int hash(Object o) { 31 | if (o == null) { 32 | return 0; 33 | } 34 | if (o instanceof Long) { 35 | return hashLong((Long) o); 36 | } 37 | if (o instanceof Integer) { 38 | return hashLong((Integer) o); 39 | } 40 | if (o instanceof Double) { 41 | return hashLong(Double.doubleToRawLongBits((Double) o)); 42 | } 43 | if (o instanceof Float) { 44 | return hashLong(Float.floatToRawIntBits((Float) o)); 45 | } 46 | if (o instanceof String) { 47 | return hash(((String) o).getBytes()); 48 | } 49 | if (o instanceof byte[]) { 50 | return hash((byte[]) o); 51 | } 52 | return hash(o.toString()); 53 | } 54 | 55 | public static int hash(byte[] data) { 56 | return hash(data, data.length, -1); 57 | } 58 | 59 | public static int hash(byte[] data, int seed) { 60 | return hash(data, data.length, seed); 61 | } 62 | 63 | public static int hash(byte[] data, int length, int seed) { 64 | int m = 0x5bd1e995; 65 | int r = 24; 66 | 67 | int h = seed ^ length; 68 | 69 | int len_4 = length >> 2; 70 | 71 | for (int i = 0; i < len_4; i++) { 72 | int i_4 = i << 2; 73 | int k = data[i_4 + 3]; 74 | k = k << 8; 75 | k = k | (data[i_4 + 2] & 0xff); 76 | k = k << 8; 77 | k = k | (data[i_4 + 1] & 0xff); 78 | k = k << 8; 79 | k = k | (data[i_4] & 0xff); 80 | k *= m; 81 | k ^= k >>> r; 82 | k *= m; 83 | h *= m; 84 | h ^= k; 85 | } 86 | 87 | // avoid calculating modulo 88 | int len_m = len_4 << 2; 89 | int left = length - len_m; 90 | 91 | if (left != 0) { 92 | if (left >= 3) { 93 | h ^= (int) data[length - 3] << 16; 94 | } 95 | if (left >= 2) { 96 | h ^= (int) data[length - 2] << 8; 97 | } 98 | if (left >= 1) { 99 | h ^= (int) data[length - 1]; 100 | } 101 | 102 | h *= m; 103 | } 104 | 105 | h ^= h >>> 13; 106 | h *= m; 107 | h ^= h >>> 15; 108 | 109 | return h; 110 | } 111 | 112 | public static int hashLong(long data) { 113 | int m = 0x5bd1e995; 114 | int r = 24; 115 | 116 | int h = 0; 117 | 118 | int k = (int) data * m; 119 | k ^= k >>> r; 120 | h ^= k * m; 121 | 122 | k = (int) (data >> 32) * m; 123 | k ^= k >>> r; 124 | h *= m; 125 | h ^= k * m; 126 | 127 | h ^= h >>> 13; 128 | h *= m; 129 | h ^= h >>> 15; 130 | 131 | return h; 132 | } 133 | 134 | public static long hash64(Object o) { 135 | if (o == null) { 136 | return 0l; 137 | } else if (o instanceof String) { 138 | final byte[] bytes = ((String) o).getBytes(); 139 | return hash64(bytes, bytes.length); 140 | } else if (o instanceof byte[]) { 141 | final byte[] bytes = (byte[]) o; 142 | return hash64(bytes, bytes.length); 143 | } 144 | return hash64(o.toString()); 145 | } 146 | 147 | // 64 bit implementation copied from here: https://github.com/tnm/murmurhash-java 148 | 149 | /** 150 | * Generates 64 bit hash from byte array with default seed value. 151 | * 152 | * @param data byte array to hash 153 | * @param length length of the array to hash 154 | * @return 64 bit hash of the given string 155 | */ 156 | public static long hash64(final byte[] data, int length) { 157 | return hash64(data, length, 0xe17a1465); 158 | } 159 | 160 | /** 161 | * Generates 64 bit hash from byte array of the given length and seed. 162 | * 163 | * @param data byte array to hash 164 | * @param length length of the array to hash 165 | * @param seed initial seed value 166 | * @return 64 bit hash of the given array 167 | */ 168 | public static long hash64(final byte[] data, int length, int seed) { 169 | final long m = 0xc6a4a7935bd1e995L; 170 | final int r = 47; 171 | 172 | long h = (seed & 0xffffffffL) ^ (length * m); 173 | 174 | int length8 = length / 8; 175 | 176 | for (int i = 0; i < length8; i++) { 177 | final int i8 = i * 8; 178 | long k = 179 | ((long) data[i8] & 0xff) 180 | + (((long) data[i8 + 1] & 0xff) << 8) 181 | + (((long) data[i8 + 2] & 0xff) << 16) 182 | + (((long) data[i8 + 3] & 0xff) << 24) 183 | + (((long) data[i8 + 4] & 0xff) << 32) 184 | + (((long) data[i8 + 5] & 0xff) << 40) 185 | + (((long) data[i8 + 6] & 0xff) << 48) 186 | + (((long) data[i8 + 7] & 0xff) << 56); 187 | 188 | k *= m; 189 | k ^= k >>> r; 190 | k *= m; 191 | 192 | h ^= k; 193 | h *= m; 194 | } 195 | 196 | switch (length % 8) { 197 | case 7: 198 | h ^= (long) (data[(length & ~7) + 6] & 0xff) << 48; 199 | case 6: 200 | h ^= (long) (data[(length & ~7) + 5] & 0xff) << 40; 201 | case 5: 202 | h ^= (long) (data[(length & ~7) + 4] & 0xff) << 32; 203 | case 4: 204 | h ^= (long) (data[(length & ~7) + 3] & 0xff) << 24; 205 | case 3: 206 | h ^= (long) (data[(length & ~7) + 2] & 0xff) << 16; 207 | case 2: 208 | h ^= (long) (data[(length & ~7) + 1] & 0xff) << 8; 209 | case 1: 210 | h ^= (long) (data[length & ~7] & 0xff); 211 | h *= m; 212 | } 213 | ; 214 | 215 | h ^= h >>> r; 216 | h *= m; 217 | h ^= h >>> r; 218 | 219 | return h; 220 | } 221 | } 222 | -------------------------------------------------------------------------------- /hashing-rendezvous/src/test/java/io/github/ykayacan/hashing/rendezvous/util/MurmurHash.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Yasin Sinan Kayacan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.github.ykayacan.hashing.rendezvous.util; 18 | 19 | /** 20 | * This is a very fast, non-cryptographic hash suitable for general hash-based lookup. See 21 | * http://murmurhash.googlepages.com/ for more details. 22 | * 23 | *

24 | * 25 | *

The C version of MurmurHash 2.0 found at that site was ported to Java by Andrzej Bialecki (ab 26 | * at getopt org). 27 | */ 28 | public class MurmurHash { 29 | 30 | public static int hash(Object o) { 31 | if (o == null) { 32 | return 0; 33 | } 34 | if (o instanceof Long) { 35 | return hashLong((Long) o); 36 | } 37 | if (o instanceof Integer) { 38 | return hashLong((Integer) o); 39 | } 40 | if (o instanceof Double) { 41 | return hashLong(Double.doubleToRawLongBits((Double) o)); 42 | } 43 | if (o instanceof Float) { 44 | return hashLong(Float.floatToRawIntBits((Float) o)); 45 | } 46 | if (o instanceof String) { 47 | return hash(((String) o).getBytes()); 48 | } 49 | if (o instanceof byte[]) { 50 | return hash((byte[]) o); 51 | } 52 | return hash(o.toString()); 53 | } 54 | 55 | public static int hash(byte[] data) { 56 | return hash(data, data.length, -1); 57 | } 58 | 59 | public static int hash(byte[] data, int seed) { 60 | return hash(data, data.length, seed); 61 | } 62 | 63 | public static int hash(byte[] data, int length, int seed) { 64 | int m = 0x5bd1e995; 65 | int r = 24; 66 | 67 | int h = seed ^ length; 68 | 69 | int len_4 = length >> 2; 70 | 71 | for (int i = 0; i < len_4; i++) { 72 | int i_4 = i << 2; 73 | int k = data[i_4 + 3]; 74 | k = k << 8; 75 | k = k | (data[i_4 + 2] & 0xff); 76 | k = k << 8; 77 | k = k | (data[i_4 + 1] & 0xff); 78 | k = k << 8; 79 | k = k | (data[i_4] & 0xff); 80 | k *= m; 81 | k ^= k >>> r; 82 | k *= m; 83 | h *= m; 84 | h ^= k; 85 | } 86 | 87 | // avoid calculating modulo 88 | int len_m = len_4 << 2; 89 | int left = length - len_m; 90 | 91 | if (left != 0) { 92 | if (left >= 3) { 93 | h ^= (int) data[length - 3] << 16; 94 | } 95 | if (left >= 2) { 96 | h ^= (int) data[length - 2] << 8; 97 | } 98 | if (left >= 1) { 99 | h ^= (int) data[length - 1]; 100 | } 101 | 102 | h *= m; 103 | } 104 | 105 | h ^= h >>> 13; 106 | h *= m; 107 | h ^= h >>> 15; 108 | 109 | return h; 110 | } 111 | 112 | public static int hashLong(long data) { 113 | int m = 0x5bd1e995; 114 | int r = 24; 115 | 116 | int h = 0; 117 | 118 | int k = (int) data * m; 119 | k ^= k >>> r; 120 | h ^= k * m; 121 | 122 | k = (int) (data >> 32) * m; 123 | k ^= k >>> r; 124 | h *= m; 125 | h ^= k * m; 126 | 127 | h ^= h >>> 13; 128 | h *= m; 129 | h ^= h >>> 15; 130 | 131 | return h; 132 | } 133 | 134 | public static long hash64(Object o) { 135 | if (o == null) { 136 | return 0l; 137 | } else if (o instanceof String) { 138 | final byte[] bytes = ((String) o).getBytes(); 139 | return hash64(bytes, bytes.length); 140 | } else if (o instanceof byte[]) { 141 | final byte[] bytes = (byte[]) o; 142 | return hash64(bytes, bytes.length); 143 | } 144 | return hash64(o.toString()); 145 | } 146 | 147 | // 64 bit implementation copied from here: https://github.com/tnm/murmurhash-java 148 | 149 | /** 150 | * Generates 64 bit hash from byte array with default seed value. 151 | * 152 | * @param data byte array to hash 153 | * @param length length of the array to hash 154 | * @return 64 bit hash of the given string 155 | */ 156 | public static long hash64(final byte[] data, int length) { 157 | return hash64(data, length, 0xe17a1465); 158 | } 159 | 160 | /** 161 | * Generates 64 bit hash from byte array of the given length and seed. 162 | * 163 | * @param data byte array to hash 164 | * @param length length of the array to hash 165 | * @param seed initial seed value 166 | * @return 64 bit hash of the given array 167 | */ 168 | public static long hash64(final byte[] data, int length, int seed) { 169 | final long m = 0xc6a4a7935bd1e995L; 170 | final int r = 47; 171 | 172 | long h = (seed & 0xffffffffL) ^ (length * m); 173 | 174 | int length8 = length / 8; 175 | 176 | for (int i = 0; i < length8; i++) { 177 | final int i8 = i * 8; 178 | long k = 179 | ((long) data[i8] & 0xff) 180 | + (((long) data[i8 + 1] & 0xff) << 8) 181 | + (((long) data[i8 + 2] & 0xff) << 16) 182 | + (((long) data[i8 + 3] & 0xff) << 24) 183 | + (((long) data[i8 + 4] & 0xff) << 32) 184 | + (((long) data[i8 + 5] & 0xff) << 40) 185 | + (((long) data[i8 + 6] & 0xff) << 48) 186 | + (((long) data[i8 + 7] & 0xff) << 56); 187 | 188 | k *= m; 189 | k ^= k >>> r; 190 | k *= m; 191 | 192 | h ^= k; 193 | h *= m; 194 | } 195 | 196 | switch (length % 8) { 197 | case 7: 198 | h ^= (long) (data[(length & ~7) + 6] & 0xff) << 48; 199 | case 6: 200 | h ^= (long) (data[(length & ~7) + 5] & 0xff) << 40; 201 | case 5: 202 | h ^= (long) (data[(length & ~7) + 4] & 0xff) << 32; 203 | case 4: 204 | h ^= (long) (data[(length & ~7) + 3] & 0xff) << 24; 205 | case 3: 206 | h ^= (long) (data[(length & ~7) + 2] & 0xff) << 16; 207 | case 2: 208 | h ^= (long) (data[(length & ~7) + 1] & 0xff) << 8; 209 | case 1: 210 | h ^= (long) (data[length & ~7] & 0xff); 211 | h *= m; 212 | } 213 | ; 214 | 215 | h ^= h >>> r; 216 | h *= m; 217 | h ^= h >>> r; 218 | 219 | return h; 220 | } 221 | } 222 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. --------------------------------------------------------------------------------