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

Commit

Permalink
dump of work-in-progress. Hideous code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Newson committed Mar 10, 2011
1 parent ad612bf commit ba220cd
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 16 deletions.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
* round-robin writing
* LastModified instead of Version
* monic_file:new and monic_file:delete
* keep monic_file servers alive.
1 change: 1 addition & 0 deletions include/monic.hrl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-define(BUFFER_SIZE, 64*1024).
-define(INDEX_SIZE, 28).
-define(HEADER_SIZE, 28).
-define(FOOTER_SIZE, 24).
Expand Down
19 changes: 13 additions & 6 deletions src/monic_file.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@
tid=nil
}).

-define(BUFFER_SIZE, 16384).

%% public API
-export([open/1, add/3, read/4, close/1]).
-export([open/1, add/3, lookup/3, 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,8 +38,8 @@ open(Path) ->
add(Pid, Size, StreamBody) ->
gen_server:call(Pid, {add, Size, StreamBody}, infinity).

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

close(Pid) ->
gen_server:call(Pid, close, infinity).
Expand All @@ -68,6 +66,14 @@ init(Path) ->
{stop, Else}
end.

handle_call({lookup, Key, _Cookie}, _From, #state{tid=Tid}=State) ->
Reply = case ets:lookup(Tid, Key) of
[] ->
{error, not_found};
[{Key, Location, Size, _}] ->
{ok, Location, Size}
end,
{reply, Reply, State};
handle_call({read, Key, Cookie, Fun}, _From, #state{tid=Tid, main_fd=Fd}=State) ->
case ets:lookup(Tid, Key) of
[{Key, Location, Size, _}] ->
Expand Down Expand Up @@ -223,7 +229,8 @@ copy_in(Fd, {Bin, Next}, Location, Remaining, Sha) ->
end
end.

copy_out(_Fd, _Fun, _Location, 0) ->
copy_out(_Fd, Fun, _Location, 0) ->
Fun(eof),
ok;
copy_out(Fd, Fun, Location, Remaining) ->
case file:pread(Fd, Location, min(Remaining, ?BUFFER_SIZE)) of
Expand Down
3 changes: 1 addition & 2 deletions src/monic_file_resource.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
-export([show_file/2, create_file/2, add_item/2]).

-include_lib("webmachine/include/webmachine.hrl").
-define(BUFFER_SIZE, 64*1024).
-include("monic.hrl").

allowed_methods(ReqData, Context) ->
{['DELETE', 'GET', 'PUT', 'POST'], ReqData, Context}.
Expand Down Expand Up @@ -89,7 +89,6 @@ create_file(ReqData, Context) ->
end.

add_item(ReqData, Context) ->
erlang:display({disp, wrq:disp_path(ReqData)}),
case monic_file:open(monic_utils:path(ReqData, Context)) of
{ok, Pid} ->
try
Expand Down
67 changes: 60 additions & 7 deletions src/monic_item_resource.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,73 @@
-export([init/1,
allowed_methods/2,
content_types_provided/2,
delete_resource/2,
resource_exists/2]).
-export([fetch/2]).

-include_lib("webmachine/include/webmachine.hrl").
-include("monic.hrl").

allowed_methods(ReqData, Context) ->
{['DELETE', 'GET', 'POST'], ReqData, Context}.
{['GET'], ReqData, Context}.

content_types_provided(ReqData, Context) ->
{[{"application/octet-stream", to_binary}], ReqData, Context}.

delete_resource(ReqData, Context) ->
{false, ReqData, Context}.
{[{"application/octet-stream", fetch}], ReqData, Context}.

init(ConfigProps) ->
{ok, ConfigProps}.

resource_exists(ReqData, Context) ->
{true, ReqData, Context}.
Key = list_to_integer(wrq:path_info(key, ReqData)),
Cookie = list_to_integer(wrq:path_info(cookie, ReqData)),
Exists = case monic_file:open(monic_utils:path(ReqData, Context)) of
{ok, Pid} ->
try
case monic_file:lookup(Pid, Key, Cookie) of
{ok, _, _} ->
true;
{error, not_found} ->
false
end
after
monic_file:close(Pid)
end;
_ ->
false
end,
{Exists, ReqData, Context}.

fetch(ReqData, Context) ->
Path = monic_utils:path(ReqData, Context),
Key = list_to_integer(wrq:path_info(key, ReqData)),
Cookie = list_to_integer(wrq:path_info(cookie, ReqData)),
case monic_file:open(Path) of
{ok, Pid} ->
try
StreamBody = case monic_file:lookup(Pid, Key, Cookie) of
{ok, Location, Size} ->
case file:open(Path, [read]) of
{ok, Fd} ->
{stream, pump(Fd, Location, Size)};
_ ->
<<>>
end;
{error, not_found} ->
<<>>
end,
{StreamBody, ReqData, Context}
after
monic_file:close(Pid)
end;
_ ->
{<<>>, ReqData, Context}
end.

pump(Fd, Location, Remaining) ->
{ok, Bin} = file:pread(Fd, Location, min(Remaining, ?BUFFER_SIZE)),
Size = iolist_size(Bin),
case Size == Remaining of
true ->
{Bin, done};
false ->
{Bin, fun() -> pump(Fd, Location + Size, Remaining - Size) end}
end.
3 changes: 2 additions & 1 deletion test/monic_file_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ cleanup(Pid) ->
monic_file:close(Pid).

add(Pid) ->
?_assertMatch({ok, 0, _}, monic_file:add(Pid, 3, {<<"123">>, done})).
[?_assertMatch({ok, 0, _}, monic_file:add(Pid, 3, {<<"123">>, done})),
?_assertMatch(true, monic_file:exists(Pid, 0, 0))].

add_long(Pid) ->
?_assertMatch({ok, 0, _}, monic_file:add(Pid, 6,
Expand Down

0 comments on commit ba220cd

Please sign in to comment.