Given a collection of elements X
, a sharding function should map every element of X
into a shard.
trait Sharder[X] {
/// The final output must always be in the range [0, totalShards-1], hopefully evenly distributed in that range.
def apply(totalShards: Int): X => Int
}
Note that we have to fix the number of shards we want before getting our sharding function. One thing we may want is to change the number of shards. When we do this, it is expected that a bunch of x: X
will now map to new shards. Depending on how we intend to change the number of shards, we can be clever about defining Sharder[X]
so as to minimize the number of x: X
which move to a new shard.