Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
rename files, add read support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Newson committed Mar 8, 2011
1 parent 96b3bc5 commit 6474eaa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 71 deletions.
34 changes: 32 additions & 2 deletions src/monic_file_writer.erl → src/monic_file.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
%% License for the specific language governing permissions and limitations under
%% the License.

-module(monic_file_writer).
-module(monic_file).
-behavior(gen_server).
-include("monic.hrl").

Expand All @@ -27,7 +27,7 @@
-define(BUFFER_SIZE, 16384).

%% public API
-export([open/1, write/3, close/1]).
-export([open/1, write/3, read/4, close/1]).

%% gen_server API
-export([init/1, terminate/2, code_change/3,handle_call/3, handle_cast/2, handle_info/2]).
Expand All @@ -40,6 +40,9 @@ open(Path) ->
write(Pid, Size, Fun) ->
gen_server:call(Pid, {write, Size, Fun}, infinity).

read(Pid, Key, Cookie, Fun) ->
gen_server:call(Pid, {read, Key, Cookie, Fun}, infinity).

close(Pid) ->
gen_server:call(Pid, close, infinity).

Expand All @@ -65,6 +68,21 @@ init(Path) ->
{stop, Else}
end.

handle_call({read, Key, Cookie, Fun}, _From, #state{tid=Tid, main_fd=Fd}=State) ->
case ets:lookup(Tid, Key) of
[{Key, Location, Size, _}] ->
case monic_utils:pread_header(Fd, Location) of
{ok, #header{cookie=Cookie,size=Size}} ->
copy_out(Fd, Fun, Location + ?HEADER_SIZE, Size),
{reply, ok, State};
{ok, _} ->
{reply, {error, wrong_cookie}, State};
Else ->
{reply, Else, State}
end;
[] ->
{reply, not_found, State}
end;
handle_call({write, Size, Fun}, _From, #state{main_fd=Fd,next_location=NextLocation}=State) ->
case new_item(Size, Fun, State) of
{ok, Key, Cookie} ->
Expand Down Expand Up @@ -209,6 +227,18 @@ copy_in(Fd, Fun, Location, Remaining, Sha) ->
Else
end.

copy_out(_Fd, _Fun, _Location, 0) ->
ok;
copy_out(Fd, Fun, Location, Remaining) ->
case file:pread(Fd, Location, min(Remaining, ?BUFFER_SIZE)) of
{ok, Bin} ->
Size = iolist_size(Bin),
Fun({ok, Bin}),
copy_out(Fd, Fun, Location + Size, Remaining - Size);
Else ->
Else
end.

cleanup(#state{tid=Tid,index_fd=IndexFd,main_fd=MainFd}=State) ->
close_int(IndexFd),
close_int(MainFd),
Expand Down
63 changes: 0 additions & 63 deletions src/monic_lookup.erl

This file was deleted.

16 changes: 10 additions & 6 deletions test/monic_file_writer_test.erl → test/monic_file_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
%% License for the specific language governing permissions and limitations under
%% the License.

-module(monic_file_writer_test).
-module(monic_file_test).
-include("monic.hrl").
-include_lib("eunit/include/eunit.hrl").

Expand All @@ -21,22 +21,26 @@ all_test_() ->
fun setup/0,
fun cleanup/1,
[fun write/1,
fun write_read/1,
fun overflow/1
]}.

setup() ->
file:delete("foo.monic"),
file:delete("foo.monic.idx"),
{ok, Pid} = monic_file_writer:open("foo.monic"),
{ok, Pid} = monic_file:open("foo.monic"),
Pid.

cleanup(Pid) ->
monic_file_writer:close(Pid).
monic_file:close(Pid).

write(Pid) ->
Res = monic_file_writer:write(Pid, 3, fun(_Max) -> {ok, <<"123">>} end),
?_assertMatch({ok, 0, _}, Res).
?_assertMatch({ok, 0, _}, monic_file:write(Pid, 3, fun(_Max) -> {ok, <<"123">>} end)).

write_read(Pid) ->
{ok, Key, Cookie} = monic_file:write(Pid, 3, fun(_Max) -> {ok, <<"123">>} end),
?_assertEqual(ok, monic_file:read(Pid, Key, Cookie, fun(_) -> ok end)).

overflow(Pid) ->
Res = monic_file_writer:write(Pid, 3, fun(_Max) -> {ok, <<"1234">>} end),
Res = monic_file:write(Pid, 3, fun(_Max) -> {ok, <<"1234">>} end),
?_assertEqual({error, overflow}, Res).

0 comments on commit 6474eaa

Please sign in to comment.