This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
forked from comtihon/mongodb-erlang
/
mongo_cursor.erl
180 lines (153 loc) · 4.19 KB
/
mongo_cursor.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
-module(mongo_cursor).
-export([
create/6,
next/1,
rest/1,
take/2,
foldl/4,
map/3,
close/1
]).
-export([
start_link/1
]).
-behaviour(gen_server).
-export([
init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3
]).
-record(state, {
connection :: mongo_connection:connection(),
database :: atom(),
collection :: atom(),
cursor :: integer(),
batchsize :: integer(),
batch :: [bson:document()],
monitor :: reference
}).
-include("mongo_protocol.hrl").
-spec create(mongo_connection:connection(), atom(), atom(), integer(), integer(), [bson:document()]) -> pid().
create(Connection, Database, Collection, Cursor, BatchSize, Batch) ->
{ok, Pid} = mongo_sup:start_cursor([self(), Connection, Database, Collection, Cursor, BatchSize, Batch]),
Pid.
-spec next(pid()) -> {} | {bson:document()}.
next(Cursor) ->
try gen_server:call(Cursor, next, infinity) of
Result -> Result
catch
exit:{noproc, _} -> {}
end.
-spec rest(pid()) -> [bson:document()].
rest(Cursor) ->
try gen_server:call(Cursor, rest, infinity) of
Result -> Result
catch
exit:{noproc, _} -> []
end.
-spec take(pid(), non_neg_integer()) -> [bson:document()].
take(Cursor, Limit) ->
try gen_server:call(Cursor, {rest, Limit}, infinity) of
Result -> Result
catch
exit:{noproc, _} -> []
end.
-spec foldl(fun((bson:document(), term()) -> term()), term(), pid(), non_neg_integer()) -> term().
foldl(_Fun, Acc, _Cursor, 0) ->
Acc;
foldl(Fun, Acc, Cursor, infinity) ->
lists:foldl(Fun, Acc, rest(Cursor));
foldl(Fun, Acc, Cursor, Max) ->
case next(Cursor) of
{} -> Acc;
{Doc} -> foldl(Fun, Fun(Doc, Acc), Cursor, Max - 1)
end.
-spec map(fun((bson:document()) -> term()), pid(), non_neg_integer()) -> [term()].
map(Fun, Cursor, Max) ->
lists:reverse(foldl(fun(Doc, Acc) ->
[Fun(Doc) | Acc]
end, [], Cursor, Max)).
-spec close(pid()) -> ok.
close(Cursor) ->
catch gen_server:call(Cursor, stop),
ok.
start_link(Args) ->
gen_server:start_link(?MODULE, Args, []).
%% @hidden
init([Owner, Connection, Database, Collection, Cursor, BatchSize, Batch]) ->
{ok, #state{
connection = Connection,
database = Database,
collection = Collection,
cursor = Cursor,
batchsize = BatchSize,
batch = Batch,
monitor = erlang:monitor(process, Owner)
}}.
%% @hidden
handle_call(next, _From, State) ->
case next_i(State) of
{Reply, #state{cursor = 0, batch = []} = UpdatedState} ->
{stop, normal, Reply, UpdatedState};
{Reply, UpdatedState} ->
{reply, Reply, UpdatedState}
end;
handle_call(rest, _From, State) ->
{Reply, UpdatedState} = rest_i(State, infinity),
{stop, normal, Reply, UpdatedState};
handle_call({rest, Limit}, _From, State) ->
case rest_i(State, Limit) of
{Reply, #state{cursor = 0} = UpdatedState} ->
{stop, normal, Reply, UpdatedState};
{Reply, UpdatedState} ->
{reply, Reply, UpdatedState}
end;
handle_call(stop, _From, State) ->
{stop, normal, ok, State}.
%% @hidden
handle_cast(_, State) ->
{noreply, State}.
%% @hidden
handle_info({'DOWN', Monitor, process, _, _}, #state{monitor = Monitor} = State) ->
{stop, normal, State};
handle_info(_, State) ->
{noreply, State}.
%% @hidden
terminate(_, #state{cursor = 0}) ->
ok;
terminate(_, State) ->
mongo_connection:request(State#state.connection, State#state.database, #killcursor{
cursorids = [State#state.cursor]
}).
%% @hidden
code_change(_Old, State, _Extra) ->
{ok, State}.
%% @private
next_i(#state{batch = [Doc | Rest]} = State) ->
{{Doc}, State#state{batch = Rest}};
next_i(#state{batch = [], cursor = 0} = State) ->
{{}, State};
next_i(#state{batch = []} = State) ->
{Cursor, Batch} = mongo_connection:request(State#state.connection, State#state.database, #getmore{
collection = State#state.collection,
batchsize = State#state.batchsize,
cursorid = State#state.cursor
}),
next_i(State#state{cursor = Cursor, batch = Batch}).
%% @private
rest_i(State, infinity) ->
rest_i(State, -1);
rest_i(State, Limit) ->
{Docs, UpdatedState} = rest_i(State, [], Limit),
{lists:reverse(Docs), UpdatedState}.
%% @private
rest_i(State, Acc, 0) ->
{Acc, State};
rest_i(State, Acc, Limit) ->
case next_i(State) of
{{}, UpdatedState} -> {Acc, UpdatedState};
{{Doc}, UpdatedState} -> rest_i(UpdatedState, [Doc | Acc], Limit - 1)
end.