Skip to content

Commit

Permalink
Split DB and Collection
Browse files Browse the repository at this point in the history
  • Loading branch information
krestenkrab committed Apr 13, 2012
1 parent 429f1c2 commit 281efcc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
3 changes: 3 additions & 0 deletions include/riak_mongo_protocol.hrl
Expand Up @@ -32,7 +32,10 @@

-record (mongo_query, {
request_id :: integer(),

dbcoll :: binary(),
db :: binary(),
coll :: binary(),

tailablecursor = false :: boolean(),
slaveok = false :: boolean(),
Expand Down
34 changes: 13 additions & 21 deletions src/riak_mongo_message.erl
Expand Up @@ -27,39 +27,33 @@
-include ("riak_mongo_protocol.hrl").
-include_lib("riak_mongo_state.hrl").

process_message(#mongo_query{ dbcoll= <<"admin.$cmd">>,
selector=Selector }, State) ->
-define(CMD,<<"$cmd">>).
-define(ADM,<<"admin">>).

{struct, [{Command,1}|Options]} = Selector,

case admin_command(Command, Options, State) of
{ok, Reply, State2} ->
{reply, #mongo_reply{ documents=[ {struct, Reply} ]} , State2}
end
;

process_message(#mongo_query{ dbcoll= <<"collection.$cmd">>,
process_message(#mongo_query{ db=DataBase, coll=?CMD,
selector=Selector}, State) ->

{struct, [{Command,_}|Options]} = Selector,

case collection_command(Command, Options, State) of
case db_command(DataBase, Command, Options, State) of
{ok, Reply, State2} ->
{reply, #mongo_reply{ documents=[ {struct, Reply} ]} , State2}
end
;

process_message(#mongo_query{}=Message, State) ->

error_logger:info_msg("query: ~p~n", [Message]),

Result = riak_mongo_store:find(Message),
{reply, #mongo_reply{ documents=Result, queryerror=false }, State};

process_message(#mongo_insert{}=Insert, State) ->
State2 = riak_mongo_store:insert(Insert, State),
{noreply, State2};

process_message(#mongo_delete{}=Delete, State) ->
State2 = riak_mongo_store:delete(Delete, State),
{noreply, State2};

process_message(Message, State) ->
error_logger:info_msg("unhandled message: ~p~n", [Message]),
{noreply, State}.
Expand All @@ -70,17 +64,15 @@ you(#state{peer=Peer}) ->
io_lib:format("~p.~p.~p.~p:~p", [A, B, C, D, P]).


admin_command(<<"whatsmyuri">>, _Options, State) ->
db_command(?ADM, <<"whatsmyuri">>, _Options, State) ->
{ok, [{you, {utf8, you(State)}}, {ok, 1}], State};

admin_command(<<"replSetGetStatus">>, _Options, State) ->
db_command(?ADM, <<"replSetGetStatus">>, _Options, State) ->
_IsForShell = proplists:is_defined(forShell, _Options),
{ok, [{ok, false}], State};

admin_command(Command, _Options, State) ->
{ok, [{err, <<"unknown command: ", Command/binary>>}, {ok, false}], State}.

collection_command(<<"getlasterror">>, _Options, State) ->
db_command(_DataBase, <<"getlasterror">>, _Options, State) ->
case State#state.lastError of
[] ->
{ok, [{ok,true}], State#state{lastError=[]}};
Expand All @@ -89,6 +81,6 @@ collection_command(<<"getlasterror">>, _Options, State) ->
end;


collection_command(Command, _Options, State) ->
{ok, [{err, <<"unknown command: ", Command/binary>>}, {ok, false}], State}.
db_command(DataBase, Command, _Options, State) ->
{ok, [{err, <<"unknown command: db=", DataBase, ", cmd=", Command/binary>>}, {ok, false}], State}.

4 changes: 4 additions & 0 deletions src/riak_mongo_protocol.erl
Expand Up @@ -111,8 +111,12 @@ decode_packet(<< ?HDR(_, ?QueryOpcode),
<< ?get_int32(NumberToSkip), ?get_int32(NumberToReturn), Rest2/binary >> = Rest1,
[Query | ReturnFieldSelectors ] = get_all_docs(Rest2),

{DB,Coll} = split_dbcoll(DBColl),

{ok, #mongo_query{ request_id=RequestId,
dbcoll=DBColl,
db=DB,
coll=Coll,
tailablecursor=bool(Tailable),
slaveok=bool(SlaveOK),
nocursortimeout=bool(NoCursorTimeout),
Expand Down

0 comments on commit 281efcc

Please sign in to comment.