summaryrefslogtreecommitdiff
path: root/apps/mem3/src/mem3_cache.erl
blob: 6614c29ee43d0eb5dbe7659a8a5298a484eeee76 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
% 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(mem3_cache).
-behaviour(gen_server).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
    code_change/3]).

-export([start_link/0]).

-record(state, {changes_pid}).

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

start_link() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

init([]) ->
    ets:new(partitions, [bag, public, named_table, {keypos,#shard.dbname}]),
    {Pid, _} = spawn_monitor(fun() -> listen_for_changes(0) end),
    {ok, #state{changes_pid = Pid}}.

handle_call(_Call, _From, State) ->
    {noreply, State}.

handle_cast(_Msg, State) ->
    {noreply, State}.

handle_info({'DOWN', _, _, Pid, {badarg, [{ets,delete,[partitions,_]}|_]}},
        #state{changes_pid=Pid} = State) ->
    % fatal error, somebody deleted our ets table
    {stop, ets_table_error, State};
handle_info({'DOWN', _, _, Pid, Reason}, #state{changes_pid=Pid} = State) ->
    ?LOG_INFO("~p changes listener died ~p", [?MODULE, Reason]),
    Seq = case Reason of {seq, EndSeq} -> EndSeq; _ -> 0 end,
    erlang:send_after(5000, self(), {start_listener, Seq}),
    {noreply, State};
handle_info({start_listener, Seq}, State) ->
    {NewPid, _} = spawn_monitor(fun() -> listen_for_changes(Seq) end),
    {noreply, State#state{changes_pid=NewPid}};
handle_info(_Msg, State) ->
    {noreply, State}.

terminate(_Reason, #state{changes_pid=Pid}) ->
    exit(Pid, kill),
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

%% internal functions

listen_for_changes(Since) ->
    DbName = ?l2b(couch_config:get("mem3", "db", "dbs")),
    {ok, Db} = ensure_exists(DbName),
    Args = #changes_args{
        feed = "continuous",
        since = Since,
        heartbeat = true,
        include_docs = true
    },
    ChangesFun = couch_changes:handle_changes(Args, nil, Db),
    ChangesFun(fun changes_callback/2).

ensure_exists(DbName) ->
    Options = [{user_ctx, #user_ctx{roles=[<<"_admin">>]}}],
    case couch_db:open(DbName, Options) of
    {ok, Db} ->
        {ok, Db};
    _ -> 
        couch_server:create(DbName, Options)
    end.

changes_callback(start, _) ->
    {ok, nil};
changes_callback({stop, EndSeq}, _) ->
    exit({seq, EndSeq});
changes_callback({change, {Change}, _}, _) ->
    DbName = couch_util:get_value(<<"id">>, Change),
    case couch_util:get_value(deleted, Change, false) of
    true ->
        ets:delete(partitions, DbName);
    false ->
        case couch_util:get_value(doc, Change) of
        {error, Reason} ->
            ?LOG_ERROR("missing partition table for ~s: ~p", [DbName, Reason]);
        {Doc} ->
            ets:delete(partitions, DbName),
            ets:insert(partitions, mem3_util:build_shards(DbName, Doc))
        end
    end,
    {ok, couch_util:get_value(<<"seq">>, Change)};
changes_callback(timeout, _) ->
    {ok, nil}.