Commits

Anonymous committed 4490303 Merge

Merge branch 'master' of git@github.com:JacobVorreuter/emongo

  • Participants
  • Parent commits 055f748, 1b26043

Comments (0)

Files changed (2)

include/emongo.hrl

--record(pool, {id, host, port, database, size=1, conn_pids=[], req_id=1}).
+-record(pool, {id, host, port, database, size=1, conn_pids=queue:new(), req_id=1}).
 -record(header, {message_length, request_id, response_to, op_code}).
 -record(response, {header, response_flag, cursor_id, offset, limit, documents}).
 -record(emo_query, {opts=[], offset=0, limit=0, q=[], field_selector=[]}).
 	find(PoolId, Collection, [], [{timeout, ?TIMEOUT}]).
 
 find(PoolId, Collection, Selector) when ?IS_DOCUMENT(Selector) ->
-	find(PoolId, Collection, Selector, [{timeout, ?TIMEOUT}]).
+	find(PoolId, Collection, Selector, [{timeout, ?TIMEOUT}]);
+	
+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),
+	emongo_conn:send_recv(Pid, Pool#pool.req_id, Packet, ?TIMEOUT).
 	
 %% @spec find(PoolId, Collection, Selector, Options) -> Result
 %%		 PoolId = atom()
 			undefined ->
 				State;
 			{Pool, Others} ->
-				io:format("delete ~p from ~p~n", [Pid, Pool#pool.conn_pids]),
-				Pids1 = lists:delete(Pid, Pool#pool.conn_pids),
-				io:format("new pids ~p~n", [Pids1]),
+				Pids1 = queue:filter(fun(Item) -> Item =/= Pid end, Pool#pool.conn_pids),
 				Pool1 = Pool#pool{conn_pids = Pids1},
 				Pool2 = do_open_connections(Pool1),
 				Pools1 = [{PoolId, Pool2}|Others],
-				io:format("new pool ~p~n", [Pools1]),
 				State#state{pools=Pools1}
 		end,
 	{noreply, State1};
 			 end || {PoolId, Props} <- Pools]
 	end.
 		
-do_open_connections(#pool{conn_pids=Pids, size=Size}=Pool) when length(Pids) < Size -> 
-	Pid = emongo_conn:start_link(Pool#pool.id, Pool#pool.host, Pool#pool.port),
-	do_open_connections(Pool#pool{conn_pids = [Pid|Pids]});
-	
-do_open_connections(Pool) -> 
-	Pool.
+do_open_connections(#pool{conn_pids=Pids, size=Size}=Pool) -> 
+	case queue:len(Pids) < Size of
+		true ->
+			Pid = emongo_conn:start_link(Pool#pool.id, Pool#pool.host, Pool#pool.port),
+			do_open_connections(Pool#pool{conn_pids = queue:in(Pid, Pids)});
+		false ->
+			Pool
+	end.
 
 get_pool(PoolId, Pools) ->
 	get_pool(PoolId, Pools, []).