-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Introduce interface(s) for hashing CommandObject #3743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
17a6862
Introduce interface(s) for hashing CommandObject
sazzad16 9787d58
Merge branch '5.2.0' into csc-hashing-interface
sazzad16 6d1f0a8
Merge fix
sazzad16 479f0b2
Rename ByteArrayHashing to PrimitiveArrayHashing
sazzad16 9fdc7b7
Comment
sazzad16 7784a9b
JavaDoc
sazzad16 c620950
Rename variable
sazzad16 000e07b
Merge branch '5.2.0' into csc-hashing-interface
sazzad16 d8784af
Fix merge in GuavaCSC and re-organize
sazzad16 3b0f4f1
Fix javadoc
sazzad16 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/redis/clients/jedis/csc/hash/AbstractCommandHashing.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package redis.clients.jedis.csc.hash; | ||
|
||
import redis.clients.jedis.Builder; | ||
import redis.clients.jedis.CommandObject; | ||
import redis.clients.jedis.args.Rawable; | ||
|
||
public abstract class AbstractCommandHashing implements CommandLongHashing { | ||
|
||
@Override | ||
public final long hash(CommandObject command) { | ||
long[] nums = new long[command.getArguments().size() + 1]; | ||
int idx = 0; | ||
for (Rawable raw : command.getArguments()) { | ||
nums[idx++] = hashRawable(raw); | ||
} | ||
nums[idx] = hashBuilder(command.getBuilder()); | ||
return hashLongs(nums); | ||
} | ||
|
||
protected abstract long hashLongs(long[] longs); | ||
|
||
protected abstract long hashRawable(Rawable raw); | ||
|
||
protected long hashBuilder(Builder builder) { | ||
return builder.hashCode(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/redis/clients/jedis/csc/hash/CommandLongHashing.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package redis.clients.jedis.csc.hash; | ||
|
||
import redis.clients.jedis.CommandObject; | ||
|
||
/** | ||
* The interface for hashing a command object for client-side caching. | ||
*/ | ||
public interface CommandLongHashing { | ||
|
||
/** | ||
* Produce a 64-bit signed hash from a command object. | ||
* @param command the command object | ||
* @return 64-bit signed hash | ||
*/ | ||
long hash(CommandObject command); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/redis/clients/jedis/csc/hash/GuavaHashing.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package redis.clients.jedis.csc.hash; | ||
|
||
import com.google.common.hash.HashFunction; | ||
import com.google.common.hash.Hasher; | ||
import redis.clients.jedis.CommandObject; | ||
|
||
public class GuavaHashing implements CommandLongHashing { | ||
|
||
public static final HashFunction DEFAULT_HASH_FUNCTION = com.google.common.hash.Hashing.fingerprint2011(); | ||
|
||
private final HashFunction function; | ||
|
||
public GuavaHashing(HashFunction function) { | ||
this.function = function; | ||
} | ||
|
||
@Override | ||
public long hash(CommandObject command) { | ||
Hasher hasher = function.newHasher(); | ||
command.getArguments().forEach(raw -> hasher.putBytes(raw.getRaw())); | ||
hasher.putInt(command.getBuilder().hashCode()); | ||
return hasher.hash().asLong(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/redis/clients/jedis/csc/hash/OpenHftHashing.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package redis.clients.jedis.csc.hash; | ||
|
||
import net.openhft.hashing.LongHashFunction; | ||
|
||
public class OpenHftHashing extends PrimitiveArrayHashing implements CommandLongHashing { | ||
|
||
public static final LongHashFunction DEFAULT_HASH_FUNCTION = LongHashFunction.xx3(); | ||
|
||
private final LongHashFunction function; | ||
|
||
public OpenHftHashing(LongHashFunction function) { | ||
this.function = function; | ||
} | ||
|
||
@Override | ||
protected long hashLongs(long[] longs) { | ||
return function.hashLongs(longs); | ||
} | ||
|
||
@Override | ||
protected long hashBytes(byte[] bytes) { | ||
return function.hashBytes(bytes); | ||
} | ||
|
||
@Override | ||
protected long hashInt(int hashCode) { | ||
return function.hashInt(hashCode); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/redis/clients/jedis/csc/hash/PrimitiveArrayHashing.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package redis.clients.jedis.csc.hash; | ||
|
||
import redis.clients.jedis.Builder; | ||
import redis.clients.jedis.args.Rawable; | ||
|
||
public abstract class PrimitiveArrayHashing extends AbstractCommandHashing { | ||
|
||
@Override | ||
protected final long hashRawable(Rawable raw) { | ||
return hashBytes(raw.getRaw()); | ||
} | ||
|
||
@Override | ||
protected final long hashBuilder(Builder builder) { | ||
return hashInt(builder.hashCode()); | ||
} | ||
|
||
protected abstract long hashBytes(byte[] bytes); | ||
|
||
protected long hashInt(int hashCode) { | ||
return hashCode; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.