forked from benoitc/couchbeam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_changes.erl
166 lines (139 loc) · 5.79 KB
/
gen_changes.erl
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
%%% -*- erlang -*-
%%%
%%% This file is part of couchbeam released under the MIT license.
%%% See the NOTICE for more information.
%% @doc gen_changes CouchDB continuous changes consumer behavior
%% This behaviour allows you to create easily a server that consume
%% Couchdb continuous changes
-module(gen_changes).
-include("couchbeam.hrl").
-behavior(gen_server).
-export([start_link/4]).
-export([init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3]).
-export([behaviour_info/1]).
-export([call/2,
call/3,
cast/2]).
-export([stop/1, get_seq/1]).
behaviour_info(callbacks) ->
[{init, 1},
{handle_change, 2},
{handle_call, 3},
{handle_cast, 2},
{handle_info, 2},
{terminate, 2}];
behaviour_info(_) ->
undefined.
call(Name, Request) ->
gen_server:call(Name, Request).
call(Name, Request, Timeout) ->
gen_server:call(Name, Request, Timeout).
cast(Dest, Request) ->
gen_server:cast(Dest, Request).
%% @doc create a gen_changes process as part of a supervision tree.
%% The function should be called, directly or indirectly, by the supervisor.
%% @spec start_link(Module, Db::db(), Options::changesoptions(),
%% InitArgs::list()) -> term()
%% changesoptions() = [changeoption()]
%% changeoption() = {include_docs, string()} |
%% {filter, string()} |
%% {since, integer()|string()} |
%% {heartbeat, string()|boolean()}
start_link(Module, Db, Options, InitArgs) ->
gen_server:start_link(?MODULE, [Module, Db, Options, InitArgs], []).
init([Module, Db, Options, InitArgs]) ->
case Module:init(InitArgs) of
{ok, ModState} ->
case couchbeam_changes:follow(Db, Options) of
{ok, StreamRef} ->
LastSeq = proplists:get_value(since, Options, 0),
{ok, #gen_changes_state{stream_ref=StreamRef,
mod=Module,
modstate=ModState,
db=Db,
options=Options,
last_seq=LastSeq}};
{error, Error} ->
Module:terminate(Error, ModState),
{stop, Error}
end;
Error ->
Error
end.
stop(Pid) when is_pid(Pid) ->
gen_server:cast(Pid, stop).
get_seq(Pid) when is_pid(Pid) ->
gen_server:call(Pid, get_seq).
handle_call(get_seq, _From, State=#gen_changes_state{last_seq=Seq}) ->
{reply, Seq, State};
handle_call(Request, From,
State=#gen_changes_state{mod=Module, modstate=ModState}) ->
case Module:handle_call(Request, From, ModState) of
{reply, Reply, NewModState} ->
{reply, Reply, State#gen_changes_state{modstate=NewModState}};
{reply, Reply, NewModState, A}
when A =:= hibernate orelse is_number(A) ->
{reply, Reply, State#gen_changes_state{modstate=NewModState}, A};
{noreply, NewModState} ->
{noreply, State#gen_changes_state{modstate=NewModState}};
{noreply, NewModState, A} when A =:= hibernate orelse is_number(A) ->
{noreply, State#gen_changes_state{modstate=NewModState}, A};
{stop, Reason, NewModState} ->
{stop, Reason, State#gen_changes_state{modstate=NewModState}};
{stop, Reason, Reply, NewModState} ->
{stop, Reason, Reply, State#gen_changes_state{modstate=NewModState}}
end.
handle_cast(stop, State) ->
{stop, normal, State};
handle_cast(Msg, State=#gen_changes_state{mod=Module, modstate=ModState}) ->
case Module:handle_cast(Msg, ModState) of
{noreply, NewModState} ->
{noreply, State#gen_changes_state{modstate=NewModState}};
{noreply, NewModState, A} when A =:= hibernate orelse is_number(A) ->
{noreply, State#gen_changes_state{modstate=NewModState}, A};
{stop, Reason, NewModState} ->
{stop, Reason, State#gen_changes_state{modstate=NewModState}}
end.
handle_info({Ref, Msg},
State=#gen_changes_state{mod=Module, modstate=ModState,
stream_ref=Ref}) ->
State2 = case Msg of
{done, LastSeq} ->
State#gen_changes_state{last_seq=LastSeq};
{change, Change} ->
Seq = couchbeam_doc:get_value(<<"seq">>, Change),
State#gen_changes_state{last_seq=Seq}
end,
case catch Module:handle_change(Msg, ModState) of
{noreply, NewModState} ->
{noreply, State2#gen_changes_state{modstate=NewModState}};
{noreply, NewModState, A} when A =:= hibernate orelse is_number(A) ->
{noreply, State2#gen_changes_state{modstate=NewModState}, A};
{stop, Reason, NewModState} ->
{stop, Reason, State2#gen_changes_state{modstate=NewModState}}
end;
handle_info({Ref, {error, Error}},
State=#gen_changes_state{stream_ref=Ref, last_seq=LastSeq}) ->
handle_info({error, [Error, {last_seq, LastSeq}]}, State);
handle_info(Info, State=#gen_changes_state{mod=Module, modstate=ModState}) ->
case Module:handle_info(Info, ModState) of
{noreply, NewModState} ->
{noreply, State#gen_changes_state{modstate=NewModState}};
{noreply, NewModState, A} when A =:= hibernate orelse is_number(A) ->
{noreply, State#gen_changes_state{modstate=NewModState}, A};
{stop, Reason, NewModState} ->
{stop, Reason, State#gen_changes_state{modstate=NewModState}}
end.
code_change(_OldVersion, State, _Extra) ->
%% TODO: support code changes?
{ok, State}.
terminate(Reason, #gen_changes_state{stream_ref=Ref,
mod=Module, modstate=ModState}) ->
Module:terminate(Reason, ModState),
couchbeam_changes:cancel_stream(Ref),
ok.