Anonymous avatar Anonymous committed 167aaf8

Comments (0)

Files changed (2)

 		 insert/3, update/4, update/5, delete/2, delete/3,
 		 ensure_index/3, count/2, dec2hex/1, hex2dec/1]).
 
+-export([update_sync/5]).
+
 -include("emongo.hrl").
 
 -record(state, {oid_index, hashed_hostn}).
 	Packet = emongo_packet:update(Pool#pool.database, Collection, Pool#pool.req_id, Upsert, Selector, Document),
 	emongo_server:send(Pid, Pool#pool.req_id, Packet).
 
+update_sync(PoolId, Collection, Selector, Document, Upsert) ->
+    {Pid, #pool{database=Database, req_id=ReqId}} = get_pid_pool(PoolId),
+    Packet1 = emongo_packet:update(Database, Collection, ReqId, Upsert, Selector, Document),
+    Packet2 = emongo_packet:get_last_error(Database, ReqId),
+    Resp = emongo_server:send_recv(Pid, ReqId, [Packet1, Packet2], ?TIMEOUT),
+    case lists:keysearch(<<"updatedExisting">>, 1, lists:nth(1, Resp#response.documents)) of
+        false ->
+            undefined;
+        {value, {<<"updatedExisting">>, true}} ->
+            ok;
+        {value, {<<"updatedExisting">>, false}} ->
+            {error, not_updated}
+    end.
+
 %%------------------------------------------------------------------------------
 %% delete
 %%------------------------------------------------------------------------------

src/emongo_packet.erl

 
 -export([update/6, insert/4, do_query/4, get_more/5, 
 		 delete/4, kill_cursors/2, msg/2, decode_response/1,
-		 ensure_index/4]).
+		 ensure_index/4, get_last_error/2]).
 
 -include("emongo.hrl").
-	
+
+get_last_error(Database, ReqId) ->
+    %%Query = #emo_query{q=[{<<"getlasterror">>, 1}], limit=1},
+    %%do_query(Database, "$cmd", ReqId, Query).
+    DatabaseLength = byte_size(Database),
+    <<(57+DatabaseLength):32/little-signed, ReqId:32/little-signed, 0:32,
+     ?OP_QUERY:32/little-signed, 0:32, Database/binary, ".$cmd", 0, 0:32, 1:32/little-signed,
+     %% Encoded document
+     23:32/little-signed, 16, "getlasterror", 0, 1:32/little-signed, 0>>.
+
 update(Database, Collection, ReqID, Upsert, Selector, Document) ->
 	FullName = unicode:characters_to_binary([Database, ".", Collection]),
 	EncodedSelector = emongo_bson:encode(Selector),
 		is_integer(Val) -> list_to_binary(integer_to_list(Val));
 		true -> <<>>
 	end,
-	index_name(Tail, <<Bin/binary, KeyBin/binary, "_", ValBin/binary>>).
+	index_name(Tail, <<Bin/binary, KeyBin/binary, "_", ValBin/binary>>).
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.