Skip to content

Commit

Permalink
Make SlotHash methods public #2199
Browse files Browse the repository at this point in the history
  • Loading branch information
mp911de committed Sep 26, 2022
1 parent 39d2677 commit c46f615
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/main/java/io/lettuce/core/cluster/SlotHash.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
package io.lettuce.core.cluster;

import java.nio.ByteBuffer;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.lettuce.core.codec.CRC16;
import io.lettuce.core.codec.RedisCodec;
Expand Down Expand Up @@ -54,7 +58,7 @@ private SlotHash() {
* @param key the key
* @return slot
*/
public static final int getSlot(String key) {
public static int getSlot(String key) {
return getSlot(key.getBytes());
}

Expand Down Expand Up @@ -116,16 +120,16 @@ private static int indexOf(ByteBuffer haystack, int start, byte needle) {
/**
* Partition keys by slot-hash. The resulting map honors order of the keys.
*
* @param codec codec to encode the key
* @param keys iterable of keys
* @param codec codec to encode the key.
* @param keys iterable of keys.
* @param <K> Key type.
* @param <V> Value type.
* @result map between slot-hash and an ordered list of keys.
*
* @return map between slot-hash and an ordered list of keys.
*/
static <K, V> Map<Integer, List<K>> partition(RedisCodec<K, V> codec, Iterable<K> keys) {
public static <K, V> Map<Integer, List<K>> partition(RedisCodec<K, V> codec, Iterable<K> keys) {

Map<Integer, List<K>> partitioned = new HashMap<>();

for (K key : keys) {
int slot = getSlot(codec.encodeKey(key));
if (!partitioned.containsKey(slot)) {
Expand All @@ -134,19 +138,23 @@ static <K, V> Map<Integer, List<K>> partition(RedisCodec<K, V> codec, Iterable<K
Collection<K> list = partitioned.get(slot);
list.add(key);
}

return partitioned;
}

/**
* Create mapping between the Key and hash slot.
*
* @param partitioned map partitioned by slothash and keys
* @param <K>
* @param partitioned map partitioned by slot-hash and keys.
* @return map of keys to their slot-hash.
* @param <K> key type
* @param <S> slot-hash number type.
*/
static <K> Map<K, Integer> getSlots(Map<Integer, ? extends Iterable<K>> partitioned) {
public static <S extends Number, K> Map<K, S> getSlots(Map<S, ? extends Iterable<K>> partitioned) {

Map<K, S> result = new HashMap<>();

Map<K, Integer> result = new HashMap<>();
for (Map.Entry<Integer, ? extends Iterable<K>> entry : partitioned.entrySet()) {
for (Map.Entry<S, ? extends Iterable<K>> entry : partitioned.entrySet()) {
for (K key : entry.getValue()) {
result.put(key, entry.getKey());
}
Expand Down

0 comments on commit c46f615

Please sign in to comment.