diff --git a/include/riak_mongo_protocol.hrl b/include/riak_mongo_protocol.hrl index 022ed3b..a3c0e3a 100644 --- a/include/riak_mongo_protocol.hrl +++ b/include/riak_mongo_protocol.hrl @@ -32,7 +32,10 @@ -record (mongo_query, { request_id :: integer(), + dbcoll :: binary(), + db :: binary(), + coll :: binary(), tailablecursor = false :: boolean(), slaveok = false :: boolean(), diff --git a/src/riak_mongo_message.erl b/src/riak_mongo_message.erl index 637ec21..6dc6e40 100644 --- a/src/riak_mongo_message.erl +++ b/src/riak_mongo_message.erl @@ -27,23 +27,15 @@ -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 @@ -51,8 +43,6 @@ process_message(#mongo_query{ dbcoll= <<"collection.$cmd">>, 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}; @@ -60,6 +50,10 @@ 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}. @@ -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=[]}}; @@ -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}. diff --git a/src/riak_mongo_protocol.erl b/src/riak_mongo_protocol.erl index b79f64d..0771149 100644 --- a/src/riak_mongo_protocol.erl +++ b/src/riak_mongo_protocol.erl @@ -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),