Class ClusterSlotHashUtil

java.lang.Object
io.valkey.springframework.data.valkey.connection.ClusterSlotHashUtil

public abstract class ClusterSlotHashUtil extends Object
Utility class encapsulating functionality commonly used for cluster slot hashing.
Since:
1.7
Author:
Christoph Strobl, John Blum
  • Field Details

  • Constructor Details

    • ClusterSlotHashUtil

      public ClusterSlotHashUtil()
  • Method Details

    • isSameSlotForAllKeys

      public static boolean isSameSlotForAllKeys(byte[]... keys)
      Determines whether all keys will hash to the same slot.
      Parameters:
      keys - array of keys to evaluate3; must not be null.
      Returns:
      a boolean value indicating whether all keys will hash to the same slot.
      Throws:
      IllegalArgumentException - if the byte array of keys is null.
    • isSameSlotForAllKeys

      public static boolean isSameSlotForAllKeys(ByteBuffer... keys)
      Determines whether all keys will hash to the same slot.
      Parameters:
      keys - array of ByteBuffer objects containing the keys to evaluate; must not be null.
      Returns:
      a boolean value indicating whether all keys will hash to the same slot.
      Throws:
      IllegalArgumentException - if the array of keys is null.
      Since:
      2.0
      See Also:
    • isSameSlotForAllKeys

      public static boolean isSameSlotForAllKeys(Collection<ByteBuffer> keys)
      Determines whether all keys will hash to the same slot.
      Parameters:
      keys - Collection of ByteBuffer objects containing the keys to evaluate; must not be null.
      Returns:
      a boolean value indicating whether all keys will hash to the same slot.
      Throws:
      IllegalArgumentException - if the Collection of keys is null.
      Since:
      2.0
    • calculateSlot

      public static int calculateSlot(String key)
      Calculate the slot from the given key.
      Parameters:
      key - String containing the Valkey key to evaluate; must not be null or empty.
      Returns:
      the computed slot based on the given key.
      Throws:
      IllegalArgumentException - if the given key is null or empty.
      See Also:
    • calculateSlot

      public static int calculateSlot(byte[] key)
      Calculate the slot from the given key.
      Parameters:
      key - array of bytes containing the Valkey key to evaluate; must not be null.
      Returns:
      the computed slot based on the given key.
    • getKeyForSlot

      public static String getKeyForSlot(int targetSlot)
      Generates a key that will map to the specified cluster slot. This method iterates through key patterns until it finds one that matches the target slot.
      Parameters:
      targetSlot - the target slot number (0-16383)
      Returns:
      a key string that will hash to the specified slot
      Throws:
      IllegalArgumentException - if slot is not in valid range [0, 16383]