summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdam Kocoloski <adam@cloudant.com>2010-06-02 12:09:15 -0400
committerAdam Kocoloski <adam@cloudant.com>2010-06-02 12:09:15 -0400
commitca58314b5605906917d5033bc9e75a6c6857f88d (patch)
treed7414a4bc5cfd172b95ea7f81fd7edf85340b19f /src
parent0340953f4ff3cc6ee09b786031442f97f79090c3 (diff)
forgot to commit new file, BugzID 10242
Diffstat (limited to 'src')
-rw-r--r--src/fabric_doc_count.erl42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/fabric_doc_count.erl b/src/fabric_doc_count.erl
new file mode 100644
index 00000000..75a7a052
--- /dev/null
+++ b/src/fabric_doc_count.erl
@@ -0,0 +1,42 @@
+-module(fabric_doc_count).
+
+-export([go/1]).
+
+-include("fabric.hrl").
+
+go(DbName) ->
+ Shards = partitions:all_parts(DbName),
+ Workers = fabric_util:submit_jobs(Shards, get_doc_count, []),
+ Acc0 = {length(Workers), [{Beg,nil} || #shard{range=[Beg,_]} <- Workers]},
+ 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
+ nil ->
+ Infos = lists:keyreplace(Beg, 1, Infos0, {Beg, Count}),
+ case is_complete(Infos) of
+ true ->
+ {stop, lists:sum([C || {_, C} <- Infos])};
+ false ->
+ if N > 1 ->
+ {ok, {N-1, Infos}};
+ true ->
+ report_error(Infos),
+ {error, missing_shards}
+ end
+ end;
+ _ ->
+ {ok, {N-1, Infos0}}
+ end;
+handle_message(_, _, {1, Infos}) ->
+ report_error(Infos),
+ {error, missing_shards};
+handle_message(_Other, _, {N, Infos0}) ->
+ {ok, {N-1, Infos0}}.
+
+report_error(Infos) ->
+ MissingShards = [S || {S,nil} <- Infos],
+ ?LOG_ERROR("get_db_info error, missing shards: ~p", [MissingShards]).
+
+is_complete(List) ->
+ not lists:keymember(nil, 2, List).