Permalink
Browse files

insert2.js runs now without errors. Implemented doc check for $ field…

… names
  • Loading branch information...
1 parent 9513e7a commit 23a2ec8820436693d0660b02ffe1fcc9df63923d @pavlobaron committed May 6, 2012
Showing with 28 additions and 2 deletions.
  1. +28 −2 src/riak_mongo_message.erl
View
@@ -28,6 +28,7 @@
-include ("riak_mongo_protocol.hrl").
-include("riak_mongo_state.hrl").
+-include("riak_mongo_bson.hrl").
-include_lib("bson/include/bson_binary.hrl").
-define(CMD, <<"$cmd">>).
@@ -101,8 +102,13 @@ process_message(#mongo_killcursor{ cursorids=IDs }, State = #worker_state { curs
{noreply, State#worker_state{ cursors=NewDict }};
process_message(#mongo_insert{}=Insert, State) ->
- State2 = riak_mongo_riak:insert(Insert, State),
- {noreply, State2};
+ case check_docs(Insert) of
+ [] ->
+ State2 = riak_mongo_riak:insert(Insert, State),
+ {noreply, State2};
+ Errors ->
+ {noreply, State#worker_state{ lastError=Errors }}
+ end;
process_message(#mongo_delete{}=Delete, State) ->
State2 = riak_mongo_riak:delete(Delete, State),
@@ -117,6 +123,26 @@ process_message(Message, State) ->
{noreply, State}.
%% internals
+
+check_docs(#mongo_insert{documents=Docs, continueonerror=ContinueOnError}) ->
+ lists:foldl(fun(#bson_raw_document{ body = Doc}, Err)
+ when Err =:= []; ContinueOnError =:= true ->
+ {{struct, Fields}, _} = riak_mongo_bson:get_document(Doc),
+ lists:foldl(fun({K, _V}, Erro) when Erro =:= [] ->
+ try <<"$", _Rest/binary>> = K of
+ _ ->
+ error_logger:info_msg("$ field found: ~p~n", [K]),
+ [{error, "can't insert with $ fields"}|Err]
+ catch
+ _:_ -> []
+ end
+ end,
+ [],
+ Fields)
+ end,
+ [],
+ Docs).
+
you(#worker_state{sock=Sock}) ->
{ok, {{A, B, C, D}, P}} = inet:peername(Sock), %IPv6???
io_lib:format("~p.~p.~p.~p:~p", [A, B, C, D, P]).

0 comments on commit 23a2ec8

Please sign in to comment.