Commits

Anonymous committed ce3203f

adding more api functions

Comments (0)

Files changed (4)

 -record(pool, {id, host, port, database, size=1, conn_pids=[], req_id=1}).
 -record(header, {message_length, request_id, response_to, op_code}).
 -record(response, {header, response_flag, cursor_id, starting_from, number_returned, documents}).
--record(emo_query, {opts=[], num_to_skip=0, num_to_return=0, q={obj, []}, field_selector=[]}).
+-record(emo_query, {opts=[], num_to_skip=0, num_to_return=0, q=[], field_selector=[]}).
 
 -define(TIMEOUT, 5000).
 
 -export([start_link/0, init/1, handle_call/3, handle_cast/2, 
 		 handle_info/2, terminate/2, code_change/3]).
 
--export([pools/0, add_pool/5, find/3, find/4, insert/3]).
+-export([pools/0, add_pool/5, find/3, find/4, find_one/3, find_one/4, 
+		 insert/3]).
 
 -include("emongo.hrl").
 
 
 %%use_db(PoolId) -> ok.
 
-find(PoolId, Collection, Obj) ->
-	find(PoolId, Collection, Obj, ?TIMEOUT).
+find(PoolId, Collection, Document) ->
+	find(PoolId, Collection, Document, ?TIMEOUT).
 	
-find(PoolId, Collection, {obj, _}=Obj, Timeout) ->
-	find(PoolId, Collection, #emo_query{q=Obj}, Timeout);
-
-find(PoolId, Collection, Bin, Timeout) when is_binary(Bin) ->
-	find(PoolId, Collection, #emo_query{q=Bin}, Timeout);
+find(PoolId, Collection, Document, Timeout) when is_list(Document) ->
+	find(PoolId, Collection, #emo_query{q=Document}, Timeout);
+	
+find(PoolId, Collection, {oid, OID}, Timeout) when is_binary(OID) ->
+	find(PoolId, Collection, #emo_query{q=[{"_id", {oid, OID}}], num_to_return=1}, Timeout);
 	
 find(PoolId, Collection, Query, Timeout) when is_record(Query, emo_query) ->
 	{Pid, Pool} = gen_server:call(?MODULE, {pid, PoolId}, infinity),
 	Packet = emongo_packet:do_query(Pool#pool.database, Collection, Pool#pool.req_id, Query),
+	io:format("packet ~p~n", [Packet]),
 	emongo_conn:send_recv(Pid, Pool#pool.req_id, Packet, Timeout).
 	
-%find_one(PoolId, Collection, {obj, _}=Obj) ->
+find_one(PoolId, Collection, Document) ->
+	find_one(PoolId, Collection, Document, ?TIMEOUT).
 
-insert(PoolId, Collection, {obj, _}=Obj) ->
+find_one(PoolId, Collection, Document, Timeout) when is_list(Document) ->
+	find(PoolId, Collection, #emo_query{q=Document, num_to_return=1}, Timeout);
+
+find_one(PoolId, Collection, {oid, OID}, Timeout) when is_binary(OID) ->
+	find(PoolId, Collection, #emo_query{q=[{"_id", {oid, OID}}], num_to_return=1}, Timeout).
+
+insert(PoolId, Collection, Document) ->
 	{Pid, Pool} = gen_server:call(?MODULE, {pid, PoolId}, infinity),
-	Packet = emongo_packet:insert(Pool#pool.database, Collection, Pool#pool.req_id, Obj),
+	Packet = emongo_packet:insert(Pool#pool.database, Collection, Pool#pool.req_id, Document),
 	emongo_conn:send(Pid, Pool#pool.req_id, Packet).
 
 %%update
 %% OTHER DEALINGS IN THE SOFTWARE.
 -module(emongo_bson).
 -export([encode/1, decode/1]).
+-compile(export_all).
 
 encode(undefined) ->
 	<<>>;
 decode_key(<<0, Tail/binary>>, Acc) ->
 	{Acc, Tail};
 	
-decode_key(<<H, Tail/binary>>, Acc) ->
-	decode_key(Tail, <<Acc/binary, H/binary>>).
+decode_key(<<H:8, Tail/binary>>, Acc) ->
+	decode_key(Tail, <<Acc/binary, H>>).
 	
 %% DOUBLE
 decode_value(1, <<Val:64/little-signed-float, Tail/binary>>) ->

src/emongo_packet.erl

 
 -include("emongo.hrl").
 	
-update(Database, Collection, ReqID, Upsert, {obj, _}=Selector, {obj, _}=Obj) ->
-	FullName = mongodb_bson:encode_cstring(lists:concat([Database, ".", Collection])),
-	EncodedSelector = mongodb_bson:encode(Selector),
-	EncodedDocument = mongodb_bson:encode(Obj),
+update(Database, Collection, ReqID, Upsert, Selector, Document) ->
+	FullName = unicode:characters_to_binary([Database, ".", Collection]),
+	EncodedSelector = emongo_bson:encode(Selector),
+	EncodedDocument = emongo_bson:encode(Document),
 	BinUpsert = if Upsert == true -> 1; true -> 0 end,
-	Message = <<0:32, FullName/binary, BinUpsert:32/little-signed, EncodedSelector/binary, EncodedDocument/binary>>,
+	Message = <<0:32, FullName/binary, 0, BinUpsert:32/little-signed, EncodedSelector/binary, EncodedDocument/binary>>,
 	Length = byte_size(Message),
     <<(Length+16):32/little-signed, ReqID:32/little-signed, 0:32, ?OP_UPDATE:32/little-signed, Message/binary>>.
 
-insert(Database, Collection, ReqID, {obj, _}=Obj) ->
-	FullName = mongodb_bson:encode_cstring(lists:concat([Database, ".", Collection])),
-	EncodedDocument = mongodb_bson:encode(Obj),
-	Message = <<0:32, FullName/binary, EncodedDocument/binary>>,
+insert(Database, Collection, ReqID, Document) ->
+	FullName = unicode:characters_to_binary([Database, ".", Collection]),
+	EncodedDocument = emongo_bson:encode(Document),
+	Message = <<0:32, FullName/binary, 0, EncodedDocument/binary>>,
 	Length = byte_size(Message),
     <<(Length+16):32/little-signed, ReqID:32/little-signed, 0:32, ?OP_INSERT:32/little-signed, Message/binary>>.
 
 do_query(Database, Collection, ReqID, Query) when is_record(Query, emo_query) ->
 	OptsSum = lists:foldl(fun(X, Acc) -> Acc+X end, 0, Query#emo_query.opts),
-	FullName = mongodb_bson:encode_cstring(lists:concat([Database, ".", Collection])),
+	FullName = unicode:characters_to_binary([Database, ".", Collection]),
 	EncodedDocument = if 
 		is_binary(Query#emo_query.q) -> Query#emo_query.q; 
-		true -> mongodb_bson:encode(Query#emo_query.q)
+		true -> emongo_bson:encode(Query#emo_query.q)
 	end,
 	EncodedFieldSelector = if 
 		Query#emo_query.field_selector == [] -> <<>>; 
-		true -> mongodb_bson:encode(Query#emo_query.field_selector) 
+		true -> emongo_bson:encode(Query#emo_query.field_selector) 
 	end,
-	Message = <<OptsSum:32/little-signed, FullName/binary, 
+	Message = <<OptsSum:32/little-signed, FullName/binary, 0:8,
 				(Query#emo_query.num_to_skip):32/little-signed, 
 				(Query#emo_query.num_to_return):32/little-signed, 
 				EncodedDocument/binary, EncodedFieldSelector/binary>>,
     <<(Length+16):32/little-signed, ReqID:32/little-signed, 0:32, ?OP_QUERY:32/little-signed, Message/binary>>.
 
 get_more(Database, Collection, ReqID, NumToReturn, CursorID) ->
-	FullName = mongodb_bson:encode_cstring(lists:concat([Database, ".", Collection])),
-	Message = <<0:32, FullName/binary, NumToReturn:32/little-signed, CursorID:64/little-signed>>,
+	FullName = unicode:characters_to_binary([Database, ".", Collection]),
+	Message = <<0:32, FullName/binary, 0, NumToReturn:32/little-signed, CursorID:64/little-signed>>,
 	Length = byte_size(Message),
     <<(Length+16):32/little-signed, ReqID:32/little-signed, 0:32, ?OP_GET_MORE:32/little-signed, Message/binary>>.
 	
-delete(Database, Collection, ReqID, {obj, _}=Selector) ->
-	FullName = mongodb_bson:encode_cstring(lists:concat([Database, ".", Collection])),
-	EncodedDocument = mongodb_bson:encode(Selector),
-	Message = <<0:32, FullName/binary, 0:32, EncodedDocument/binary>>,
+delete(Database, Collection, ReqID, Selector) ->
+	FullName = unicode:characters_to_binary([Database, ".", Collection]),
+	EncodedDocument = emongo_bson:encode(Selector),
+	Message = <<0:32, FullName/binary, 0, 0:32, EncodedDocument/binary>>,
 	Length = byte_size(Message),
     <<(Length+16):32/little-signed, ReqID:32/little-signed, 0:32, ?OP_DELETE:32/little-signed, Message/binary>>.
 
 		cursor_id = CursorID, 
 		starting_from = StartingFrom, 
 		number_returned = NumRet, 
-		documents = if Documents == <<>> -> []; true -> mongodb_bson:decode(Documents) end
+		documents = if Documents == <<>> -> []; true -> emongo_bson:decode(Documents) end
 	}.