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

Commit

Permalink
use #index record in ets directly.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Newson committed Mar 15, 2011
1 parent 69b8256 commit 68d8791
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
33 changes: 15 additions & 18 deletions src/monic_file.erl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ read(Pid, Key, Cookie) ->
%% gen_server functions

init(Path) ->
Tid = ets:new(index, [set, private]),
Tid = ets:new(index, [{keypos, 2}, set, private]),
case load_index(Tid, Path) of
{ok, IndexFd, LastLoc} ->
case load_main(Tid, Path, LastLoc) of
Expand Down Expand Up @@ -119,8 +119,7 @@ handle_call({start_writing, Key, Cookie, Size}, _From,
handle_call({start_writing, _Key, _Cookie, _Size}, _From, State) ->
{reply, {error, already_writing}, State};

handle_call({write, Ref, {Bin, Next}}, _From, #state{main_fd=Fd, sha=Sha, write_pos=Pos, writer=Ref}=State) ->
Index = State#state.next_index,
handle_call({write, Ref, {Bin, Next}}, _From, #state{next_index=Index, main_fd=Fd, sha=Sha, write_pos=Pos, writer=Ref}=State) ->
Size = iolist_size(Bin),
Remaining = Index#index.size - (Pos + Size - State#state.data_start_pos),
Write = case {Next, Remaining} of
Expand All @@ -141,8 +140,7 @@ handle_call({write, Ref, {Bin, Next}}, _From, #state{main_fd=Fd, sha=Sha, write_
ok ->
{ok, IndexPos} = file:position(Fd, cur), %% TODO track this in state.
monic_utils:pwrite_term(State#state.index_fd, IndexPos, Index),
ets:insert(State#state.tid, {Index#index.key, Index#index.cookie,
Index#index.location, Index#index.size, Index#index.last_modified}),
ets:insert(State#state.tid, Index),
{reply, ok, State#state{next_index=nil, reset_pos=Pos + Size + FooterSize,
write_pos=Pos + Size + FooterSize, writer=nil}};
Else ->
Expand Down Expand Up @@ -230,27 +228,26 @@ load_index(Tid, Path) ->
load_index_items(Tid, Fd) ->
load_index_items(Tid, Fd, 0, 0).

load_index_items(Tid, Fd, IndexLocation, LastLoc) ->
load_index_items(Tid, Fd, IndexLocation, Eof) ->
case monic_utils:pread_term(Fd, IndexLocation) of
{ok, IndexSize, #index{key=Key,cookie=Cookie,location=Location,size=Size,
last_modified=LastModified,deleted=Deleted}} ->
{ok, IndexSize, #index{deleted=Deleted,key=Key,location=Location}=Index} ->
case Deleted of
false -> ets:insert(Tid, {Key, Cookie, Location, Size, LastModified});
false -> ets:insert(Tid, Index);
true -> ets:delete(Tid, Key)
end,
load_index_items(Tid, Fd, IndexLocation + IndexSize, Location);
eof ->
{ok, LastLoc};
{ok, Eof};
Else ->
Else
end.

load_main(Tid, Path, LastLoc) ->
load_main(Tid, Path, Eof) ->
case file:open(Path, [binary, raw, read, append]) of
{ok, Fd} ->
case load_main_items(Tid, Fd, LastLoc) of
{ok, Eof} ->
{ok, Fd, Eof};
case load_main_items(Tid, Fd, Eof) of
{ok, Eof1} ->
{ok, Fd, Eof1};
Else ->
Else
end;
Expand All @@ -260,10 +257,10 @@ load_main(Tid, Path, LastLoc) ->

load_main_items(Tid, Fd, Location) ->
case monic_utils:pread_term(Fd, Location) of
{ok, HeaderSize, #header{key=Key,cookie=Cookie,size=Size,last_modified=LastModified,deleted=Deleted}} ->
{ok, HeaderSize, #header{deleted=Deleted,key=Key,size=Size}=Header} ->
case Deleted of
false ->
ets:insert(Tid, {Key, Cookie, Location, Size, LastModified}),
ets:insert(Tid, monic_utils:header_to_index(Header)),
case monic_utils:pread_term(Fd, Location + HeaderSize + Size) of
{ok, FooterSize, _} ->
load_main_items(Tid, Fd, Location + HeaderSize + Size + FooterSize);
Expand Down Expand Up @@ -321,8 +318,8 @@ stream_out(Pid, Location, Remaining) ->

info_int(Tid, Key, Cookie) ->
case ets:lookup(Tid, Key) of
[{Key, Cookie, Location, Size, Version}] ->
{ok, {Location, Size, Version}};
[#index{cookie=Cookie,location=Location,size=Size,last_modified=LastModified}] ->
{ok, {Location, Size, LastModified}};
_ ->
{error, not_found}
end.
Expand Down
7 changes: 6 additions & 1 deletion src/monic_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

-module(monic_utils).
-export([path/2, exists/2, open/2]).
-export([pwrite_term/3, pread_term/2]).
-export([pwrite_term/3, pread_term/2, header_to_index/2]).

-include("monic.hrl").

Expand Down Expand Up @@ -64,3 +64,8 @@ pread_term(Fd, Location) ->
Else ->
Else
end.

-spec header_to_index(#header{}, integer()) -> #index{}.
header_to_index(#header{key=Key,cookie=Cookie,size=Size,last_modified=LastModified, deleted=Deleted}, Location) ->
#index{key=Key,cookie=Cookie,size=Size,last_modified=LastModified,deleted=Deleted,location=Location}.

0 comments on commit 68d8791

Please sign in to comment.