Class ModCycle<T extends Number & Comparable<T>>

java.lang.Object
me.ahoo.cosid.sharding.ModCycle<T>
Type Parameters:
T - the type of number that is comparable
All Implemented Interfaces:
PreciseSharding<T>, RangeSharding<T>, Sharding<T>

public class ModCycle<T extends Number & Comparable<T>> extends Object implements Sharding<T>
Modulo-based cyclical sharding algorithm.

Distributes IDs across nodes using modulo arithmetic. Maps a sharding value to a node by computing: value % divisor.

CosIdModShardingAlgorithm

  • Constructor Details

    • ModCycle

      public ModCycle(int divisor, String logicNamePrefix)
      Creates a ModCycle sharding algorithm.
      Parameters:
      divisor - the number of nodes to distribute across
      logicNamePrefix - the prefix for node names
  • Method Details

    • getDivisor

      public int getDivisor()
      Gets the divisor.
      Returns:
      the divisor
    • sharding

      public @NonNull String sharding(T shardingValue)
      Description copied from interface: PreciseSharding
      Gets the node for a single sharding value.
      Specified by:
      sharding in interface PreciseSharding<T extends Number & Comparable<T>>
      Parameters:
      shardingValue - the sharding value
      Returns:
      the node name
    • sharding

      public @NonNull Collection<String> sharding(com.google.common.collect.Range<T> shardingValue)
      Description copied from interface: RangeSharding
      Gets the nodes that should handle the given range of IDs.
      Specified by:
      sharding in interface RangeSharding<T extends Number & Comparable<T>>
      Parameters:
      shardingValue - the range of sharding values
      Returns:
      collection of node names that should handle the range
    • getEffectiveNodes

      public @NonNull Collection<String> getEffectiveNodes()
      Description copied from interface: Sharding
      Gets all effective node names.
      Specified by:
      getEffectiveNodes in interface Sharding<T extends Number & Comparable<T>>
      Returns:
      collection of effective node names