Skip to content

Commit

Permalink
Added take/2 function to gridfs_cursor.
Browse files Browse the repository at this point in the history
  • Loading branch information
hammingweight committed May 7, 2012
1 parent 1af0d03 commit d56ffcb
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/gridfs_cursor.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
new/4,
next/1,
rest/1,
set_timeout/2]).
set_timeout/2,
take/2]).

%% gen_server callbacks
-export([init/1,
Expand Down Expand Up @@ -57,6 +58,9 @@ rest(Cursor) ->
set_timeout(Cursor, Timeout) ->
gen_server:call(Cursor, {set_timeout, Timeout}, infinity).

take(Limit, Cursor) when Limit >= 0 ->
gen_server:call(Cursor, {take, Limit}, infinity).

%% Server functions

%% @doc Initializes the server with connection parameters, a bucket and a mongo cursor.
Expand Down Expand Up @@ -84,7 +88,10 @@ handle_call(rest, _From, State) ->
Reply = [create_file(State, Id) || {'_id', Id} <- Ids],
{stop, normal, Reply, State};
handle_call({set_timeout, Timeout}, _From, State) ->
{reply, ok, State#state{die_with_parent=false, timeout=Timeout}, Timeout}.
{reply, ok, State#state{die_with_parent=false, timeout=Timeout}, Timeout};
handle_call({take, Limit}, _From, State) ->
Files = take(State, Limit, []),
{stop, normal, Files, State}.

%% @doc Handles asynchronous messages.
handle_cast(_Msg, State) ->
Expand Down Expand Up @@ -119,3 +126,16 @@ create_file(State, Id) ->
gridfs_file:set_timeout(File, State#state.timeout),
File
end.

% Reads files from a cursor up to a limit and returns them as a list.
take(_State, 0, Files) ->
lists:reverse(Files);
take(State, Limit, Files) ->
MongoCursor = State#state.mongo_cursor,
case mongo_cursor:next(MongoCursor) of
{} ->
lists:reverse(Files);
{{'_id', Id}} ->
File = create_file(State, Id),
take(State, Limit-1, [File|Files])
end.

0 comments on commit d56ffcb

Please sign in to comment.