Package me.ahoo.cosid.sharding
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>
Modulo-based cyclical sharding algorithm.
Distributes IDs across nodes using modulo arithmetic.
Maps a sharding value to a node by computing: value % divisor.

-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintGets the divisor.Gets all effective node names.sharding(ShardingRange<T> shardingValue) The nodes of every value in a range: the nodessharding(Number)routes the range's values to, or a few more, never fewer.Gets the node for a single sharding value.
-
Constructor Details
-
ModCycle
Creates a ModCycle sharding algorithm.- Parameters:
divisor- the number of nodes to distribute acrosslogicNamePrefix- the prefix for node names
-
-
Method Details
-
getDivisor
public int getDivisor()Gets the divisor.- Returns:
- the divisor
-
sharding
Description copied from interface:PreciseShardingGets the node for a single sharding value.- Specified by:
shardingin interfacePreciseSharding<T extends Number & Comparable<T>>- Parameters:
shardingValue- the sharding value- Returns:
- the node name
-
sharding
The nodes of every value in a range: the nodessharding(Number)routes the range's values to, or a few more, never fewer.A range is taken in the domain of
Number.longValue(), which routes a value: an integral type (Long,Integer,Short,Byte,BigInteger) by its exact values, so an open bound excludes its endpoint; a missing bound reaches to the end of the domain (Long.MIN_VALUEorLong.MAX_VALUE, aBigIntegerwithout end), negative values included. A fractional type (BigDecimal,Double,Float) routes by its integer part, which the values beside an open endpoint share with it, so its bounds are taken as closed. ABigIntegerorBigDecimalbeyond a long routes by the low 64 bits of its integer part, aslongValue()does.- Specified by:
shardingin interfaceRangeSharding<T extends Number & Comparable<T>>- Parameters:
shardingValue- the range- Returns:
- the nodes of the range's values
-
getEffectiveNodes
Description copied from interface:ShardingGets all effective node names.- Specified by:
getEffectiveNodesin interfaceSharding<T extends Number & Comparable<T>>- Returns:
- collection of effective node names
-