summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Kocoloski <adam@cloudant.com>2010-07-14 09:55:00 -0400
committerAdam Kocoloski <adam@cloudant.com>2010-07-14 09:55:00 -0400
commit24850155eee3a969d8f2daf127a5a782f6c273cd (patch)
tree0b2921e221bfbce03cb3dccd20f7c45d38df87d5
parent0df44a1122d7fd99f96992946692737361941b64 (diff)
fabric:get_doc_count/1 was broken for N>1
-rw-r--r--src/fabric_db_doc_count.erl40
1 files changed, 14 insertions, 26 deletions
diff --git a/src/fabric_db_doc_count.erl b/src/fabric_db_doc_count.erl
index 332b923d..12d5cbf8 100644
--- a/src/fabric_db_doc_count.erl
+++ b/src/fabric_db_doc_count.erl
@@ -9,36 +9,24 @@
go(DbName) ->
Shards = mem3:shards(DbName),
Workers = fabric_util:submit_jobs(Shards, get_doc_count, []),
- Acc0 = {length(Workers), [{Beg,nil} || #shard{range=[Beg,_]} <- Workers]},
+ Acc0 = {fabric_dict:init(Workers, nil), 0},
fabric_util:recv(Workers, #shard.ref, fun handle_message/3, Acc0).
-handle_message({ok, Count}, #shard{range=[Beg,_]}, {N, Infos0}) ->
- case couch_util:get_value(Beg, Infos0) of
+handle_message({ok, Count}, Shard, {Counters, Acc}) ->
+ case fabric_dict:lookup_element(Shard, Counters) of
+ undefined ->
+ % already heard from someone else in this range
+ {ok, {Counters, Acc}};
nil ->
- Infos = lists:keyreplace(Beg, 1, Infos0, {Beg, Count}),
- case is_complete(Infos) of
+ C1 = fabric_dict:store(Shard, ok, Counters),
+ C2 = fabric_view:remove_overlapping_shards(Shard, C1),
+ case fabric_dict:any(nil, C2) of
true ->
- {stop, lists:sum([C || {_, C} <- Infos])};
+ {ok, {C2, Count+Acc}};
false ->
- if N > 1 ->
- {ok, {N-1, Infos}};
- true ->
- report_error(Infos),
- {error, missing_shards}
- end
- end;
- _ ->
- {ok, {N-1, Infos0}}
+ {stop, Count+Acc}
+ end
end;
-handle_message(_, _, {1, Infos}) ->
- report_error(Infos),
- {error, missing_shards};
-handle_message(_Other, _, {N, Infos0}) ->
- {ok, {N-1, Infos0}}.
+handle_message(_, _, Acc) ->
+ {ok, Acc}.
-report_error(Infos) ->
- MissingShards = [S || {S,nil} <- Infos],
- ?LOG_ERROR("doc_count error, missing shards: ~p", [MissingShards]).
-
-is_complete(List) ->
- not lists:keymember(nil, 2, List).