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

Commit

Permalink
add binary names support (from eddieantonio/mongodb-erlang), update doc)
Browse files Browse the repository at this point in the history
  • Loading branch information
comtihon committed Jun 5, 2014
1 parent af50b4c commit 3e86384
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 23 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ Although the mongodb application includes several modules, you should only need

To connect to a database `test` on mongodb server listening on `localhost:27017` (or any address & port of your choosing) use `mongo:connect/3/4/6`.

> Host = localhost.
> Host = "127.0.0.1".
> Port = 27017.
> Database = test.
> Database = <<"test">>.
> {ok, Connection} = mongo:connect (Host, Port, Database).

`mongo:connect` returns `{error, Reason}` if it failed to connect.
Expand All @@ -48,6 +48,7 @@ To connect to a database `test` on mongodb server listening on `localhost:27017`

After you connected to your database - you can carry out write operations, such as `insert`, `update` and `delete`:

> Collection = <"test">>.
> mongo:insert(Connection, Collection, [
{name, <<"Yankees">>, home, {city, <<"New York">>, state, <<"NY">>}, league, <<"American">>},
{name, <<"Mets">>, home, {city, <<"New York">>, state, <<"NY">>}, league, <<"National">>},
Expand Down
4 changes: 2 additions & 2 deletions include/mongo_protocol.hrl
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
% Wire protocol message types (records)

-type db() :: atom().
-type collection() :: atom(). % without db prefix
-type collection() :: binary() | atom(). % without db prefix
-type cursorid() :: integer().
-type selector() :: bson:document().
-type projector() :: bson:document().
-type skip() :: integer().
-type batchsize() :: integer(). % 0 = default batch size. negative closes cursor
-type modifier() :: bson:document().
-type connection() :: pid().
-type database() :: atom().
-type database() :: binary | atom().
-type write_mode() :: unsafe | safe | {safe, bson:document()}.
-type read_mode() :: master | slave_ok.
-type action(A) :: fun (() -> A).
Expand Down
3 changes: 1 addition & 2 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
{edoc_opts, [{index_columns, 1}, {sort_functions, false}, {preprocess, true}]}.

{deps, [
{bson, ".*", {git, "git://github.com/soundrop/bson-erlang", "HEAD"}},
{uuid, ".*", {git, "https://github.com/afiskon/erlang-uuid-v4.git", {branch, "master"}}}
{bson, ".*", {git, "git://github.com/soundrop/bson-erlang", "HEAD"}}
]}.

{clean_files, [
Expand Down
2 changes: 1 addition & 1 deletion src/api/mongo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ count(Connection, Coll, Selector) ->
% Ie. stops counting when max is reached to save processing time.
-spec count(pid() | term(), collection(), selector(), integer()) -> integer().
count(Connection, Coll, Selector, Limit) ->
CollStr = atom_to_binary(Coll, utf8),
CollStr = mc_connection_man:value_to_binary(Coll),
{true, Doc} = command(Connection, case Limit =< 0 of
true -> {count, CollStr, 'query', Selector};
false -> {count, CollStr, 'query', Selector, limit, Limit}
Expand Down
13 changes: 11 additions & 2 deletions src/connection/mc_connection_man.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
-include("mongo_protocol.hrl").

%% API
-export([reply/1, request/2, pw_key/3, process_reply/2]).
-export([reply/1, request/2, pw_key/3, process_reply/2, value_to_binary/1]).

-spec request(pid(), term()) -> ok | {non_neg_integer(), [bson:document()]}.
request(Connection, Request) -> %request to worker
Expand Down Expand Up @@ -50,4 +50,13 @@ pw_hash(Username, Password) ->

%% @private
binary_to_hexstr(Bin) ->
lists:flatten([io_lib:format("~2.16.0b", [X]) || X <- binary_to_list(Bin)]).
lists:flatten([io_lib:format("~2.16.0b", [X]) || X <- binary_to_list(Bin)]).

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) ->
<<>>.
15 changes: 3 additions & 12 deletions src/connection/mc_worker_logic.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,10 @@ process_responses(Responses, Ets) ->
gen_index_name(KeyOrder) ->
bson:doc_foldl(
fun(Label, Order, Acc) ->
<<Acc/binary, $_, (value_to_binary(Label))/binary, $_, (value_to_binary(Order))/binary>>
<<Acc/binary, $_, (mc_connection_man:value_to_binary(Label))/binary,
$_, (mc_connection_man:value_to_binary(Order))/binary>>
end, <<"i">>, KeyOrder).

make_request(Socket, Database, Request) ->
{Packet, Id} = encode_request(Database, Request),
{gen_tcp:send(Socket, Packet), Id}.

%% @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) ->
<<>>.
{gen_tcp:send(Socket, Packet), Id}.
6 changes: 5 additions & 1 deletion src/core/mongo_protocol.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
-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>>.
<<(binarize(Db))/binary, $., (binarize(Coll))/binary>>.

-spec put_message(db(), message(), requestid()) -> binary().
put_message(Db, #insert{collection = Coll, documents = Docs}, _RequestId) ->
Expand Down Expand Up @@ -101,6 +101,10 @@ get_reply(Message) ->
},
{ResponseTo, Reply, BinRest}.

-spec binarize(binary() | atom()) -> binary().
%@doc Ensures the given term is converted to a UTF-8 binary.
binarize(Term) when is_binary(Term) -> Term;
binarize(Term) when is_atom(Term) -> atom_to_binary(Term, utf8).

%% @private
get_docs(0, Bin) -> {[], Bin};
Expand Down
4 changes: 3 additions & 1 deletion test/mongo_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ find(Connection, Collection, Selector, Projector) ->
%% @private
collection(Case) ->
Now = now_to_seconds(erlang:now()),
list_to_atom(atom_to_list(?MODULE) ++ "-" ++ atom_to_list(Case) ++ "-" ++ integer_to_list(Now)).
<<(atom_to_binary(?MODULE, utf8))/binary, $-,
(atom_to_binary(Case, utf8))/binary, $-,
(list_to_binary(integer_to_list(Now)))/binary>>.

%% @private
now_to_seconds({Mega, Sec, _}) ->
Expand Down

0 comments on commit 3e86384

Please sign in to comment.