summaryrefslogtreecommitdiff
path: root/deps/fabric/src/fabric_doc_missing_revs.erl
blob: 2dd04a70703ecbb4964e78c8e3634f5abac4fe03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
% Copyright 2010 Cloudant
%
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
%   http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

-module(fabric_doc_missing_revs).

-export([go/2, go/3]).

-include("fabric.hrl").
-include_lib("mem3/include/mem3.hrl").

go(DbName, AllIdsRevs) ->
    go(DbName, AllIdsRevs, []).

go(DbName, AllIdsRevs, Options) ->
    Workers = lists:map(fun({#shard{name=Name, node=Node} = Shard, IdsRevs}) ->
        Ref = rexi:cast(Node, {fabric_rpc, get_missing_revs, [Name, IdsRevs,
            Options]}),
        Shard#shard{ref=Ref}
    end, group_idrevs_by_shard(DbName, AllIdsRevs)),
    ResultDict = dict:from_list([{Id, {{nil,Revs},[]}} || {Id, Revs} <- AllIdsRevs]),
    RexiMon = fabric_util:create_monitors(Workers),
    Acc0 = {length(Workers), ResultDict, Workers},
    try
        fabric_util:recv(Workers, #shard.ref, fun handle_message/3, Acc0)
    after
        rexi_monitor:stop(RexiMon)
    end.

handle_message({rexi_DOWN, _, {_,NodeRef},_}, _Shard, {_WorkerLen, ResultDict, Workers}) ->
    NewWorkers = [W || #shard{node=Node} = W <- Workers, Node =/= NodeRef],
    skip_message({fabric_dict:size(NewWorkers), ResultDict, NewWorkers});
handle_message({rexi_EXIT, _}, Worker, {W, D, Workers}) ->
    skip_message({W-1,D,lists:delete(Worker, Workers)});
handle_message({ok, Results}, _Worker, {1, D0, _}) ->
    D = update_dict(D0, Results),
    {stop, dict:fold(fun force_reply/3, [], D)};
handle_message({ok, Results}, Worker, {WaitingCount, D0, Workers}) ->
    D = update_dict(D0, Results),
    case dict:fold(fun maybe_reply/3, {stop, []}, D) of
    continue ->
        % still haven't heard about some Ids
        {ok, {WaitingCount - 1, D, lists:delete(Worker,Workers)}};
    {stop, FinalReply} ->
        % finished, stop the rest of the jobs
        fabric_util:cleanup(lists:delete(Worker,Workers)),
        {stop, FinalReply}
    end.

force_reply(Id, {{nil,Revs}, Anc}, Acc) ->
    % never heard about this ID, assume it's missing
    [{Id, Revs, Anc} | Acc];
force_reply(_, {[], _}, Acc) ->
    Acc;
force_reply(Id, {Revs, Anc}, Acc) ->
    [{Id, Revs, Anc} | Acc].

maybe_reply(_, _, continue) ->
    continue;
maybe_reply(_, {{nil, _}, _}, _) ->
    continue;
maybe_reply(_, {[], _}, {stop, Acc}) ->
    {stop, Acc};
maybe_reply(Id, {Revs, Anc}, {stop, Acc}) ->
    {stop, [{Id, Revs, Anc} | Acc]}.

group_idrevs_by_shard(DbName, IdsRevs) ->
    dict:to_list(lists:foldl(fun({Id, Revs}, D0) ->
        lists:foldl(fun(Shard, D1) ->
            dict:append(Shard, {Id, Revs}, D1)
        end, D0, mem3:shards(DbName,Id))
    end, dict:new(), IdsRevs)).

update_dict(D0, KVs) ->
    lists:foldl(fun({K,V,A}, D1) -> dict:store(K, {V,A}, D1) end, D0, KVs).

skip_message({0, Dict, _Workers}) ->
    {stop, dict:fold(fun force_reply/3, [], Dict)};
skip_message(Acc) ->
    {ok, Acc}.