Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Commit

Permalink
auto format and remove 1 deprecation warning
Browse files Browse the repository at this point in the history
  • Loading branch information
comtihon committed May 28, 2014
1 parent e67d4c1 commit a6564df
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 92 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,3 +5,4 @@ logs/
*.beam
ct_run.*
variables-ct*
*.iml
File renamed without changes.
40 changes: 20 additions & 20 deletions src/mongo.erl
Expand Up @@ -23,7 +23,7 @@
count/2,
count/3
]).
-export ([
-export([
command/1,
ensure_index/2
]).
Expand All @@ -32,17 +32,17 @@
-include("mongo_protocol.hrl").

-type connection() :: pid().
-type database() :: atom().
-type cursor() :: 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).
-type read_mode() :: master | slave_ok.
-type action(A) :: fun (() -> A).

-record(context, {
write_mode :: write_mode(),
read_mode :: read_mode(),
read_mode :: read_mode(),
connection :: mongo_connection:connection(),
database :: database()
database :: database()
}).


Expand Down Expand Up @@ -94,12 +94,12 @@ 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.
-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.
-spec delete_one(collection(), selector()) -> ok.
delete_one(Coll, Selector) ->
write(#delete{collection = Coll, singleremove = true, selector = Selector}).

Expand All @@ -114,7 +114,7 @@ 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()}.
-spec find_one(collection(), selector(), projector(), skip()) -> {} | {bson:document()}.
find_one(Coll, Selector, Projector, Skip) ->
read_one(#'query'{
collection = Coll,
Expand All @@ -124,27 +124,27 @@ find_one(Coll, Selector, Projector, Skip) ->
}).

%% @doc Return selected documents.
-spec find (collection(), selector()) -> cursor().
-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().
-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().
-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
-spec find(collection(), selector(), projector(), skip(), batchsize()) -> cursor(). % Action
find(Coll, Selector, Projector, Skip, BatchSize) ->
read(#'query'{
collection = Coll,
Expand All @@ -155,7 +155,7 @@ find(Coll, Selector, Projector, Skip, BatchSize) ->
}).

%@doc Count selected documents
-spec count (collection(), selector()) -> integer().
-spec count(collection(), selector()) -> integer().
count(Coll, Selector) ->
count(Coll, Selector, 0).

Expand All @@ -165,9 +165,9 @@ count(Coll, Selector) ->
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),
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.
Expand All @@ -176,7 +176,7 @@ count(Coll, Selector, Limit) ->
%% name :: bson:utf8()
%% unique :: boolean()
%% dropDups :: boolean()
-spec ensure_index (collection(), bson:document()) -> ok.
-spec ensure_index(collection(), bson:document()) -> ok.
ensure_index(Coll, IndexSpec) ->
#context{database = Database} = erlang:get(mongo_action_context),
Key = bson:at(key, IndexSpec),
Expand All @@ -186,7 +186,7 @@ ensure_index(Coll, IndexSpec) ->
ok.

%% @doc Execute given MongoDB command and return its result.
-spec command (bson:document()) -> bson:document(). % Action
-spec command(bson:document()) -> bson:document(). % Action
command(Command) ->
{Doc} = read_one(#'query'{
collection = '$cmd',
Expand Down
4 changes: 2 additions & 2 deletions src/mongo_app.erl
@@ -1,15 +1,15 @@
-module(mongo_app).

-behaviour(application).
-export ([
-export([
start/2,
stop/1
]).


%% @hidden
start(_, _) ->
mongo_sup:start_link ().
mongo_sup:start_link().

%% @hidden
stop(_) ->
Expand Down
24 changes: 12 additions & 12 deletions src/mongo_connection.erl
Expand Up @@ -22,16 +22,16 @@
]).

-record(state, {
socket :: gen_tcp:socket(),
requests :: orddict:orddict(),
buffer :: binary()
socket :: gen_tcp:socket(),
requests :: orddict:orddict(),
buffer :: binary()
}).

-include("mongo_protocol.hrl").
-type connection() :: pid().
-type service() :: {Host :: inet:hostname() | inet:ip_address(), Post :: 0..65535}.
-type options() :: [option()].
-type option() :: {timeout, timeout()} | {ssl, boolean()} | ssl.
-type service() :: {Host :: inet:hostname() | inet:ip_address(), Post :: 0..65535}.
-type options() :: [option()].
-type option() :: {timeout, timeout()} | {ssl, boolean()} | ssl.


-spec start_link(service()) -> {ok, pid()}.
Expand Down Expand Up @@ -107,12 +107,12 @@ handle_info({tcp, _Socket, Data}, State) ->
{Responses, Pending} = decode_responses(Buffer),
{noreply, State#state{
requests = case process_responses(Responses, State#state.requests) of
[] ->
[];
Requests ->
inet:setopts(State#state.socket, [{active, once}]),
Requests
end,
[] ->
[];
Requests ->
inet:setopts(State#state.socket, [{active, once}]),
Requests
end,
buffer = Pending
}};

Expand Down
16 changes: 8 additions & 8 deletions src/mongo_cursor.erl
Expand Up @@ -24,16 +24,16 @@
]).

-record(state, {
connection :: mongo_connection:connection(),
database :: atom(),
collection :: atom(),
cursor :: integer(),
batchsize :: integer(),
batch :: [bson:document()],
monitor :: reference
connection :: mongo_connection:connection(),
database :: atom(),
collection :: atom(),
cursor :: integer(),
batchsize :: integer(),
batch :: [bson:document()],
monitor :: reference
}).

-include ("mongo_protocol.hrl").
-include("mongo_protocol.hrl").


-spec create(mongo_connection:connection(), atom(), atom(), integer(), integer(), [bson:document()]) -> pid().
Expand Down
2 changes: 1 addition & 1 deletion src/mongo_id_server.erl
Expand Up @@ -25,7 +25,7 @@ request_id() ->
ets:update_counter(?MODULE, requestid_counter, 1).

%% @doc Fresh object id
-spec object_id () -> bson:objectid().
-spec object_id() -> bson:objectid().
object_id() ->
Now = bson:unixtime_to_secs(bson:timenow()),
MPid = ets:lookup_element(?MODULE, oid_machineprocid, 2),
Expand Down
6 changes: 3 additions & 3 deletions src/mongo_pool.erl
Expand Up @@ -16,9 +16,9 @@
]).

-record(state, {
supervisor :: pid(),
connections :: array(),
monitors :: orddict:orddict()
supervisor :: pid(),
connections :: array:array(),
monitors :: orddict:orddict()
}).


Expand Down
76 changes: 38 additions & 38 deletions src/mongo_protocol.erl
Expand Up @@ -18,7 +18,7 @@
-type request() :: #'query'{} | #getmore{}.
% A reply to a request
-type reply() :: #reply{}.
% message id
% message id
-type requestid() :: integer().
-type message() :: notice() | request().

Expand All @@ -36,61 +36,61 @@
-define(KillcursorOpcode, 2007).


-spec dbcoll (db(), collection()) -> bson:utf8().
-spec dbcoll(db(), collection()) -> bson:utf8().
%@doc Concat db and collection name with period (.) in between
dbcoll (Db, Coll) -> <<(atom_to_binary (Db, utf8)) /binary, $., (atom_to_binary (Coll, utf8)) /binary>>.
dbcoll(Db, Coll) -> <<(atom_to_binary(Db, utf8))/binary, $., (atom_to_binary(Coll, utf8))/binary>>.

-spec put_message(db(), message(), requestid()) -> binary().
put_message(Db, #insert{collection = Coll, documents = Docs}, RequestId) ->
<<?put_header(?InsertOpcode),
?put_int32(0),
(bson_binary:put_cstring(dbcoll(Db, Coll))) /binary,
<< <<(bson_binary:put_document(Doc))/binary>> || Doc <- Docs>>/binary >>;
?put_int32(0),
(bson_binary:put_cstring(dbcoll(Db, Coll)))/binary,
<<<<(bson_binary:put_document(Doc))/binary>> || Doc <- Docs>>/binary>>;
put_message(Db, #update{collection = Coll, upsert = U, multiupdate = M, selector = Sel, updater = Up}, RequestId) ->
<<?put_header(?UpdateOpcode),
?put_int32(0),
(bson_binary:put_cstring(dbcoll(Db, Coll)))/binary,
?put_bits32(0,0,0,0,0,0, bit(M), bit(U)),
(bson_binary:put_document(Sel))/binary,
(bson_binary:put_document (Up))/binary>>;
?put_int32(0),
(bson_binary:put_cstring(dbcoll(Db, Coll)))/binary,
?put_bits32(0, 0, 0, 0, 0, 0, bit(M), bit(U)),
(bson_binary:put_document(Sel))/binary,
(bson_binary:put_document(Up))/binary>>;
put_message(Db, #delete{collection = Coll, singleremove = R, selector = Sel}, RequestId) ->
<<?put_header(?DeleteOpcode),
?put_int32(0),
(bson_binary:put_cstring(dbcoll(Db, Coll)))/binary,
?put_bits32(0,0,0,0,0,0,0, bit(R)),
(bson_binary:put_document(Sel))/binary>>;
?put_int32(0),
(bson_binary:put_cstring(dbcoll(Db, Coll)))/binary,
?put_bits32(0, 0, 0, 0, 0, 0, 0, bit(R)),
(bson_binary:put_document(Sel))/binary>>;
put_message(_Db, #killcursor{cursorids = Cids}, RequestId) ->
<<?put_header(?KillcursorOpcode),
?put_int32(0),
?put_int32(length(Cids)),
<< <<?put_int64(Cid)>> || Cid <- Cids>>/binary >>;
?put_int32(0),
?put_int32(length(Cids)),
<<<<?put_int64(Cid)>> || Cid <- Cids>>/binary>>;
put_message(Db, #'query'{tailablecursor = TC, slaveok = SOK, nocursortimeout = NCT, awaitdata = AD,
collection = Coll, skip = Skip, batchsize = Batch, selector = Sel, projector = Proj}, RequestId) ->
collection = Coll, skip = Skip, batchsize = Batch, selector = Sel, projector = Proj}, RequestId) ->
<<?put_header(?QueryOpcode),
?put_bits32(0, 0, bit(AD), bit(NCT), 0, bit(SOK), bit(TC), 0),
(bson_binary:put_cstring (dbcoll (Db, Coll)))/binary,
?put_int32(Skip),
?put_int32(Batch),
(bson_binary:put_document(Sel))/binary,
(case Proj of [] -> <<>>; _ -> bson_binary:put_document(Proj) end)/binary >>;
?put_bits32(0, 0, bit(AD), bit(NCT), 0, bit(SOK), bit(TC), 0),
(bson_binary:put_cstring(dbcoll(Db, Coll)))/binary,
?put_int32(Skip),
?put_int32(Batch),
(bson_binary:put_document(Sel))/binary,
(case Proj of [] -> <<>>; _ -> bson_binary:put_document(Proj) end)/binary>>;
put_message(Db, #getmore{collection = Coll, batchsize = Batch, cursorid = Cid}, RequestId) ->
<<?put_header(?GetmoreOpcode),
?put_int32 (0),
(bson_binary:put_cstring(dbcoll (Db, Coll))) /binary,
?put_int32(Batch),
?put_int64(Cid)>>.
?put_int32(0),
(bson_binary:put_cstring(dbcoll(Db, Coll)))/binary,
?put_int32(Batch),
?put_int64(Cid)>>.


-spec get_reply(binary()) -> {requestid(), reply(), binary()}.
get_reply(Message) ->
<<?get_header(?ReplyOpcode, ResponseTo),
?get_bits32 (_,_,_,_, AwaitCapable, _, QueryError, CursorNotFound),
?get_int64 (CursorId),
?get_int32 (StartingFrom),
?get_int32 (NumDocs),
Bin /binary >> = Message,
{Docs, BinRest} = get_docs (NumDocs, Bin),
Reply = #reply {
?get_bits32(_, _, _, _, AwaitCapable, _, QueryError, CursorNotFound),
?get_int64(CursorId),
?get_int32(StartingFrom),
?get_int32(NumDocs),
Bin/binary>> = Message,
{Docs, BinRest} = get_docs(NumDocs, Bin),
Reply = #reply{
cursornotfound = bool(CursorNotFound),
queryerror = bool(QueryError),
awaitcapable = bool(AwaitCapable),
Expand All @@ -104,8 +104,8 @@ get_reply(Message) ->
%% @private
get_docs(0, Bin) -> {[], Bin};
get_docs(NumDocs, Bin) when NumDocs > 0 ->
{Doc, Bin1} = bson_binary:get_document (Bin),
{Docs, Bin2} = get_docs (NumDocs - 1, Bin1),
{Doc, Bin1} = bson_binary:get_document(Bin),
{Docs, Bin2} = get_docs(NumDocs - 1, Bin1),
{[Doc | Docs], Bin2}.

%% @private
Expand Down
6 changes: 3 additions & 3 deletions src/mongo_sup.erl
Expand Up @@ -7,13 +7,13 @@
]).

-behaviour(supervisor).
-export ([
-export([
init/1
]).

-define(SUPERVISOR(Id, Tag), {Id, {supervisor, start_link, [?MODULE, Tag]}, permanent, infinity, supervisor, [?MODULE]}).
-define(SUPERVISOR(Id, Tag), {Id, {supervisor, start_link, [?MODULE, Tag]}, permanent, infinity, supervisor, [?MODULE]}).
-define(SUPERVISOR(Id, Name, Tag), {Id, {supervisor, start_link, [{local, Name}, ?MODULE, Tag]}, permanent, infinity, supervisor, [?MODULE]}).
-define(WORKER(M, F, A, R), {M, {M, F, A}, R, 5000, worker, [M]}).
-define(WORKER(M, F, A, R), {M, {M, F, A}, R, 5000, worker, [M]}).


-spec start_link() -> {ok, pid()}.
Expand Down
10 changes: 5 additions & 5 deletions src/mongodb.app.src
@@ -1,8 +1,8 @@
%% ex: ts=4 sw=4 noexpandtab syntax=erlang
{application, mongodb, [
{description, "Client interface to MongoDB, also known as the driver. See www.mongodb.org"},
{vsn, git},
{registered, []},
{applications, [kernel, stdlib, bson]},
{mod, {mongo_app, []}}
{description, "Client interface to MongoDB, also known as the driver. See www.mongodb.org"},
{vsn, git},
{registered, []},
{applications, [kernel, stdlib, bson]},
{mod, {mongo_app, []}}
]}.

0 comments on commit a6564df

Please sign in to comment.