This repository has been archived by the owner on May 22, 2020. It is now read-only.
forked from comtihon/mongodb-erlang
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mongo.erl
283 lines (249 loc) · 9.2 KB
/
mongo.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
-module(mongo).
-export([
do/5
]).
-export([
insert/2,
update/3,
update/4,
update/5,
delete/2,
delete_one/2
]).
-export([
find_one/2,
find_one/3,
find_one/4,
find/2,
find/3,
find/4,
find/5,
find/6
]).
-export([
count/2,
count/3
]).
-export ([
command/1,
ensure_index/2
]).
% TODO: add auth/2
-include("mongo_protocol.hrl").
-type connection() :: pid().
-type database() :: atom().
-type cursor() :: pid().
-type write_mode() :: unsafe | safe | {safe, bson:document()}.
-type read_mode() :: master | slave_ok.
-type action(A) :: fun (() -> A).
-record(context, {
write_mode :: write_mode(),
read_mode :: read_mode(),
connection :: mongo_connection:connection(),
database :: database()
}).
%% @doc Execute mongo action under given write_mode, read_mode, connection, and database.
-spec do(write_mode(), read_mode(), connection(), database(), action(A)) -> A.
do(WriteMode, ReadMode, Connection, Database, Action) ->
PrevContext = erlang:get(mongo_action_context),
erlang:put(mongo_action_context, #context{
write_mode = WriteMode,
read_mode = ReadMode,
connection = Connection,
database = Database
}),
try Action() of
Result -> Result
after
case PrevContext of
undefined ->
erlang:erase(mongo_action_context);
_ ->
erlang:put(mongo_action_context, PrevContext)
end
end.
%% @doc Insert a document or multiple documents into a collection.
%% Returns the document or documents with an auto-generated _id if missing.
-spec insert(collection(), A) -> A.
insert(Coll, Doc) when is_tuple(Doc) ->
hd(insert(Coll, [Doc]));
insert(Coll, Docs) ->
Docs1 = [assign_id(Doc) || Doc <- Docs],
write(#insert{collection = Coll, documents = Docs1}),
Docs1.
%% @doc Replace the document matching criteria entirely with the new Document.
-spec update(collection(), selector(), bson:document()) -> ok.
update(Coll, Selector, Doc) ->
update(Coll, Selector, Doc, false, false).
%% @doc Replace the document matching criteria entirely with the new Document.
-spec update(collection(), selector(), bson:document(), boolean()) -> ok.
update(Coll, Selector, Doc, Upsert) ->
update(Coll, Selector, Doc, Upsert, false).
%% @doc Replace the document matching criteria entirely with the new Document.
-spec update(collection(), selector(), bson:document(), boolean(), boolean()) -> ok.
update(Coll, Selector, Doc, Upsert, MultiUpdate) ->
write(#update{collection = Coll, selector = Selector, updater = Doc, upsert = Upsert, multiupdate = MultiUpdate}).
%% @doc Delete selected documents
-spec delete (collection(), selector()) -> ok.
delete(Coll, Selector) ->
write(#delete{collection = Coll, singleremove = false, selector = Selector}).
%% @doc Delete first selected document.
-spec delete_one (collection(), selector()) -> ok.
delete_one(Coll, Selector) ->
write(#delete{collection = Coll, singleremove = true, selector = Selector}).
%% @doc Return first selected document, if any
-spec find_one(collection(), selector()) -> {} | {bson:document()}.
find_one(Coll, Selector) ->
find_one(Coll, Selector, []).
%% @doc Return projection of first selected document, if any. Empty projection [] means full projection.
-spec find_one(collection(), selector(), projector()) -> {} | {bson:document()}.
find_one(Coll, Selector, Projector) ->
find_one(Coll, Selector, Projector, 0).
%% @doc Return projection of Nth selected document, if any. Empty projection [] means full projection.
-spec find_one (collection(), selector(), projector(), skip()) -> {} | {bson:document()}.
find_one(Coll, Selector, Projector, Skip) ->
read_one(#'query'{
collection = Coll,
selector = Selector,
projector = Projector,
skip = Skip
}).
%% @doc Return selected documents.
-spec find (collection(), selector()) -> cursor().
find(Coll, Selector) ->
find(Coll, Selector, []).
%% @doc Return projection of selected documents.
%% Empty projection [] means full projection.
-spec find (collection(), selector(), projector()) -> cursor().
find(Coll, Selector, Projector) ->
find(Coll, Selector, Projector, 0).
%% @doc Return projection of selected documents starting from Nth document.
%% Empty projection means full projection.
-spec find (collection(), selector(), projector(), skip()) -> cursor().
find(Coll, Selector, Projector, Skip) ->
find(Coll, Selector, Projector, Skip, 0).
%% @doc Return projection of selected documents starting from Nth document in batches of batchsize.
%% 0 batchsize means default batch size.
%% Negative batch size means one batch only.
%% Empty projection means full projection.
-spec find (collection(), selector(), projector(), skip(), batchsize()) -> cursor(). % Action
find(Coll, Selector, Projector, Skip, BatchSize) ->
find(Coll, Selector, Projector, Skip, BatchSize, []).
-spec find (collection(), selector(), projector(), skip(), batchsize(), [find_option()]) -> cursor(). % Action
-type find_option() :: no_timeout | tail.
find(Coll, Selector, Projector, Skip, BatchSize, Options) ->
read(#'query'{
tailablecursor = lists:member(tail, Options),
nocursortimeout = lists:member(no_timeout, Options),
awaitdata = lists:member(tail, Options),
collection = Coll,
selector = Selector,
projector = Projector,
skip = Skip,
batchsize = BatchSize
}).
%@doc Count selected documents
-spec count (collection(), selector()) -> integer().
count(Coll, Selector) ->
count(Coll, Selector, 0).
%@doc Count selected documents up to given max number; 0 means no max.
% Ie. stops counting when max is reached to save processing time.
-spec count(collection(), selector(), integer()) -> integer().
count(Coll, Selector, Limit) ->
CollStr = atom_to_binary(Coll, utf8),
Doc = command(case Limit =< 0 of
true -> {count, CollStr, 'query', Selector};
false -> {count, CollStr, 'query', Selector, limit, Limit}
end),
trunc(bson:at(n, Doc)). % Server returns count as float
%% @doc Create index on collection according to given spec.
%% The key specification is a bson documents with the following fields:
%% key :: bson document, for e.g. {field, 1, other, -1, location, 2d}, <strong>required</strong>
%% name :: bson:utf8()
%% unique :: boolean()
%% dropDups :: boolean()
-spec ensure_index (collection(), bson:document()) -> ok.
ensure_index(Coll, IndexSpec) ->
#context{database = Database} = erlang:get(mongo_action_context),
Key = bson:at(key, IndexSpec),
Defaults = {name, gen_index_name(Key), unique, false, dropDups, false},
Index = bson:update(ns, mongo_protocol:dbcoll(Database, Coll), bson:merge(IndexSpec, Defaults)),
insert('system.indexes', Index),
ok.
%% @doc Execute given MongoDB command and return its result.
-spec command (bson:document()) -> bson:document(). % Action
command(Command) ->
{Doc} = read_one(#'query'{
collection = '$cmd',
selector = Command
}),
case bson:at(ok, Doc) of
true -> Doc;
N when N == 1 -> Doc;
_ -> erlang:error({bad_command, Doc}, [Command])
end.
%% @private
-spec assign_id(bson:document()) -> bson:document().
assign_id(Doc) ->
case bson:lookup('_id', Doc) of
{_Value} -> Doc;
{} -> bson:update('_id', mongo_id_server:object_id(), Doc)
end.
%% @private
gen_index_name(KeyOrder) ->
bson:doc_foldl(fun(Label, Order, Acc) ->
<<Acc/binary, $_, (value_to_binary(Label))/binary, $_, (value_to_binary(Order))/binary>>
end, <<"i">>, KeyOrder).
%% @private
value_to_binary(Value) when is_integer(Value) ->
bson:utf8(integer_to_list(Value));
value_to_binary(Value) when is_atom(Value) ->
atom_to_binary(Value, utf8);
value_to_binary(Value) when is_binary(Value) ->
Value;
value_to_binary(_Value) ->
<<>>.
%% @private
write(Request) ->
Context = erlang:get(mongo_action_context),
case Context#context.write_mode of
unsafe ->
write(Context#context.connection, Context#context.database, Request);
SafeMode ->
Params = case SafeMode of safe -> {}; {safe, Param} -> Param end,
Ack = write(Context#context.connection, Context#context.database, Request, Params),
case bson:lookup(err, Ack, undefined) of
undefined -> ok;
String ->
case bson:at(code, Ack) of
10058 -> erlang:exit(not_master);
Code -> erlang:exit({write_failure, Code, String})
end
end
end.
%% @private
write(Connection, Database, Request) ->
mongo_connection:request(Connection, Database, Request).
%% @private
write(Connection, Database, Request, GetLastErrorParams) ->
ok = mongo_connection:request(Connection, Database, Request),
{0, [Doc | _]} = mongo_connection:request(Connection, Database, #'query'{
batchsize = -1,
collection = '$cmd',
selector = bson:append({getlasterror, 1}, GetLastErrorParams)
}),
Doc.
%% @private
read(Request) ->
#context{connection = Connection, database = Database, read_mode = ReadMode} = erlang:get(mongo_action_context),
#'query'{collection = Collection, batchsize = BatchSize} = Request,
{Cursor, Batch} = mongo_connection:request(Connection, Database, Request#'query'{slaveok = ReadMode =:= slave_ok}),
mongo_cursor:create(Connection, Database, Collection, Cursor, BatchSize, Batch).
%% @private
read_one(Request) ->
#context{connection = Connection, database = Database, read_mode = ReadMode} = erlang:get(mongo_action_context),
{0, Docs} = mongo_connection:request(Connection, Database, Request#'query'{batchsize = -1, slaveok = ReadMode =:= slave_ok}),
case Docs of
[] -> {};
[Doc | _] -> {Doc}
end.