This is an automated email from the ASF dual-hosted git repository. vatamane pushed a commit to branch shard-split in repository https://gitbox.apache.org/repos/asf/couchdb.git
The following commit(s) were added to refs/heads/shard-split by this push: new e9b94c8 Add calculate_max_n to mem3_util module e9b94c8 is described below commit e9b94c894e89fdd8a8b74b90a9c3cda50cdb0c77 Author: Nick Vatamaniuc <vatam...@apache.org> AuthorDate: Tue Feb 26 17:59:32 2019 -0500 Add calculate_max_n to mem3_util module This can be used to calculate the maximum N (number of copies) for a given set of shards. --- src/mem3/src/mem3_util.erl | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/mem3/src/mem3_util.erl b/src/mem3/src/mem3_util.erl index e81ceb2..e8cba5d 100644 --- a/src/mem3/src/mem3_util.erl +++ b/src/mem3/src/mem3_util.erl @@ -27,7 +27,8 @@ get_ring/3, get_ring/4, non_overlapping_shards/1, - non_overlapping_shards/3 + non_overlapping_shards/3, + calculate_max_n/1 ]). %% do not use outside mem3. @@ -409,6 +410,25 @@ non_overlapping_shards(Shards, Start, End) -> end, Shards). +% Given a list of shards, return the maximum number of copies +% across all the ranges. If the ring is incomplete it will return 0. +% If there it is an n = 1 database, it should return 1, etc. +calculate_max_n(Shards) -> + Ranges = lists:map(fun(Shard) -> + [B, E] = mem3:range(Shard), + {B, E} + end, Shards), + calculate_max_n(Ranges, get_ring(Ranges), 0). + + +calculate_max_n(_Ranges, [], N) -> + N; + +calculate_max_n(Ranges, Ring, N) -> + NewRanges = Ranges -- Ring, + calculate_max_n(NewRanges, get_ring(NewRanges), N + 1). + + get_ring(Ranges) -> get_ring(Ranges, fun sort_ranges_fun/2, 0, ?RING_END). @@ -530,6 +550,18 @@ non_overlapping_shards_test() -> ]]. +calculate_max_n_test_() -> + [?_assertEqual(Res, calculate_max_n(Shards)) || {Res, Shards} <- [ + {0, []}, + {0, [shard(1, ?RING_END)]}, + {1, [shard(0, ?RING_END)]}, + {1, [shard(0, ?RING_END), shard(1, ?RING_END)]}, + {2, [shard(0, ?RING_END), shard(0, ?RING_END)]}, + {2, [shard(0, 1), shard(2, ?RING_END), shard(0, ?RING_END)]}, + {0, [shard(0, 3), shard(5, ?RING_END), shard(1, ?RING_END)]} + ]]. + + shard(Begin, End) -> #shard{range = [Begin, End]}.