summaryrefslogtreecommitdiff
path: root/src/dreyfus/src/dreyfus_fabric_cleanup.erl
blob: b5e030db002c99268b72be40de98ba4b2b8cd5fe (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
% 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.


%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-

-module(dreyfus_fabric_cleanup).

-include("dreyfus.hrl").
-include_lib("mem3/include/mem3.hrl").
-include_lib("couch/include/couch_db.hrl").

-export([go/1]).

go(DbName) ->
    {ok, DesignDocs} = fabric:design_docs(DbName),
    ActiveSigs = lists:usort(lists:flatmap(fun active_sigs/1,
        [couch_doc:from_json_obj(DD) || DD <- DesignDocs])),
    cleanup_local_purge_doc(DbName, ActiveSigs),
    clouseau_rpc:cleanup(DbName, ActiveSigs),
    ok.

active_sigs(#doc{body={Fields}}=Doc) ->
    {RawIndexes} = couch_util:get_value(<<"indexes">>, Fields, {[]}),
    {IndexNames, _} = lists:unzip(RawIndexes),
    [begin
         {ok, Index} = dreyfus_index:design_doc_to_index(Doc, IndexName),
         Index#index.sig
     end || IndexName <- IndexNames].

cleanup_local_purge_doc(DbName, ActiveSigs) ->
    {ok, BaseDir} = clouseau_rpc:get_root_dir(),
    DbNamePattern = <<DbName/binary, ".*">>,
    Pattern0 = filename:join([BaseDir, "shards", "*", DbNamePattern, "*"]),
    Pattern = binary_to_list(iolist_to_binary(Pattern0)),
    DirListStrs = filelib:wildcard(Pattern),
    DirList = [iolist_to_binary(DL) || DL <- DirListStrs],
    LocalShards = mem3:local_shards(DbName),
    ActiveDirs = lists:foldl(fun(LS, AccOuter) ->
        lists:foldl(fun(Sig, AccInner) ->
            DirName = filename:join([BaseDir, LS#shard.name, Sig]),
            [DirName | AccInner]
        end, AccOuter, ActiveSigs)
    end, [], LocalShards),

    DeadDirs = DirList -- ActiveDirs,
    lists:foldl(fun(IdxDir) ->
        Sig = dreyfus_util:get_signature_from_idxdir(IdxDir),
        case Sig of undefined -> ok; _ ->
            DocId = dreyfus_util:get_local_purge_doc_id(Sig),
            LocalShards = mem3:local_shards(DbName),
            lists:foldl(fun(LS, _AccOuter) ->
                ShardDbName = LS#shard.name,
                {ok, ShardDb} = couch_db:open_int(ShardDbName, []),
                case couch_db:open_doc(ShardDb, DocId, []) of
                    {ok, LocalPurgeDoc} ->
                        couch_db:update_doc(ShardDb,
                            LocalPurgeDoc#doc{deleted=true}, [?ADMIN_CTX]);
                    {not_found, _} ->
                        ok
                end,
                couch_db:close(ShardDb)
            end, [], LocalShards)
        end
    end, [], DeadDirs).