Anonymous avatar Anonymous committed 0baef53

adding initial ensure_index code

Comments (0)

Files changed (2)

 		 find_all/2, find_all/3, find_all/4, get_more/4,
 		 get_more/5, find_one/3, find_one/4, kill_cursors/2,
 		 insert/3, update/4, update/5, delete/2, delete/3,
-		 count/2, dec2hex/1, hex2dec/1]).
+		 ensure_index/3, count/2, dec2hex/1, hex2dec/1]).
 
 -include("emongo.hrl").
 
 find(PoolId, Collection, Selector) when ?IS_DOCUMENT(Selector) ->
 	find(PoolId, Collection, Selector, [{timeout, ?TIMEOUT}]);
 	
+%% this function has been deprecated
 find(PoolId, Collection, Query) 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),
 	Packet = emongo_packet:delete(Pool#pool.database, Collection, Pool#pool.req_id, Selector),
 	emongo_conn:send(Pid, Pool#pool.req_id, Packet).
 
-%%ensure_index
+%%------------------------------------------------------------------------------
+%% ensure index
+%%------------------------------------------------------------------------------
+ensure_index(PoolId, Collection, Keys) when ?IS_DOCUMENT(Keys)->
+	{Pid, Pool} = gen_server:call(?MODULE, {pid, PoolId}, infinity),
+	Packet = emongo_packet:ensure_index(Pool#pool.database, Collection, Pool#pool.req_id, Keys),
+	emongo_conn:send(Pid, Pool#pool.req_id, Packet).
 
 count(PoolId, Collection) ->
 	{Pid, Pool} = gen_server:call(?MODULE, {pid, PoolId}, infinity),

src/emongo_packet.erl

 -module(emongo_packet).
 
 -export([update/6, insert/4, do_query/4, get_more/5, 
-		 delete/4, kill_cursors/2, msg/2, decode_response/1]).
+		 delete/4, kill_cursors/2, msg/2, decode_response/1,
+		 ensure_index/4]).
 
 -include("emongo.hrl").
 	
 	Length = byte_size(Message),
     <<(Length+16):32/little-signed, ReqID:32/little-signed, 0:32, ?OP_DELETE:32/little-signed, Message/binary>>.
 
+ensure_index(Database, Collection, ReqID, Keys) ->
+	FullName = unicode:characters_to_binary([Database, ".system.indexes"]),
+	Selector = [
+		{<<"name">>, index_name(Keys, <<>>)},
+		{<<"ns">>, unicode:characters_to_binary([Database, ".", Collection])},
+		{<<"key">>, Keys}],
+	EncodedDocument = emongo_bson:encode(Selector),
+	io:format("index doc ~p~n", [EncodedDocument]),
+	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_INSERT:32/little-signed, Message/binary>>.
+
 kill_cursors(ReqID, CursorIDs) ->
 	CursorIDsBin = lists:foldl(fun(ID, Bin) -> <<Bin/binary, ID:64/little-signed>> end, <<>>, CursorIDs),
 	Message = <<0:32, (length(CursorIDs)):32/little-signed, CursorIDsBin/binary>>,
 			},
 			{Resp, Tail}
 	end.
+
+index_name([], Bin) -> Bin;
+index_name([{Key, Val}|Tail], Bin) ->
+	KeyBin = unicode:characters_to_binary(Key),
+	ValBin = if
+		is_integer(Val) -> list_to_binary(integer_to_list(Val));
+		true -> <<>>
+	end,
+	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.