public int GetShardId(string path, int shardCount) { if (String.IsNullOrWhiteSpace(path)) throw new ArgumentException(nameof(path) + " cannot be empty", nameof(path)); //no need to calculate anything if there is only one shard if (shardCount == 1) { return 0; } return Math.Abs(path.GetHashCode()) % shardCount; } }
public void Given_the_same_path_twice_it_should_return_the_same_id() { var subject = new HashShardingStrategy(); var result = subject.GetShardId("any old string", 4); var result2 = subject.GetShardId("any old string", 4); Assert.AreEqual(result, result2); }