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

Commit

Permalink
include cookie in ets table.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Newson committed Mar 11, 2011
1 parent 09d9aa9 commit 9a91ade
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion include/monic.hrl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-define(BUFFER_SIZE, 64*1024).
-define(INDEX_SIZE, 28).
-define(INDEX_SIZE, 32).
-define(HEADER_SIZE, 28).
-define(FOOTER_SIZE, 24).

Expand Down
30 changes: 15 additions & 15 deletions src/monic_file.erl
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ handle_call({write, Ref, {Bin, Next}}, _From, #state{main_fd=Fd, write_pos=Pos,
ok ->
Index = State#state.next_index,
monic_utils:write_index(State#state.index_fd, Index),
ets:insert(State#state.tid, {Index#index.key, Index#index.location,
Index#index.size, Index#index.version}),
ets:insert(State#state.tid, {Index#index.key, Index#index.cookie,
Index#index.location, Index#index.size, Index#index.version}),
{reply, {ok, {Index#index.key, Index#index.cookie}},
State#state{next_index=nil, next_key=State#state.next_key+1,
reset_pos=Pos1, write_pos=Pos1, writer=nil}};
Expand All @@ -128,12 +128,12 @@ handle_call({write, _Ref, _StreamBody}, _From, State) ->
{reply, {error, not_writing}, State};

handle_call({read, Key, Cookie}, _From, #state{main_fd=Fd, tid=Tid}=State) ->
case ets:lookup(Tid, Key) of
[] ->
{reply, {error, not_found}, State};
[{Key, Location, Size, _Version}] ->
case info_int(Tid, Key, Cookie) of
{ok, {Location, Size, _Version}} ->
Self = self(),
{reply, {ok, fun() -> stream_out(Self, Location + ?HEADER_SIZE, Size) end}, State}
{reply, {ok, fun() -> stream_out(Self, Location + ?HEADER_SIZE, Size) end}, State};
Else ->
{reply, Else, State}
end;

handle_call({read_hunk, Location, Size}, _From, #state{main_fd=Fd}=State) ->
Expand Down Expand Up @@ -176,9 +176,9 @@ load_index_items(Tid, Fd) ->

load_index_items(Tid, Fd, Hints) ->
case monic_utils:read_index(Fd) of
{ok, #index{key=Key,location=Location,size=Size,version=Version,flags= <<Deleted:1,_:15>>}} ->
{ok, #index{key=Key,cookie=Cookie,location=Location,size=Size,version=Version,flags= <<Deleted:1,_:15>>}} ->
case Deleted of
0 -> ets:insert(Tid, {Key, Location, Size, Version});
0 -> ets:insert(Tid, {Key, Cookie, Location, Size, Version});
1 -> ets:delete(Tid, Key)
end,
load_index_items(Tid, Fd, {Key + 1, Location + Size + ?HEADER_SIZE + ?FOOTER_SIZE});
Expand All @@ -203,9 +203,9 @@ load_main(Tid, Path, Hints) ->

load_main_items(Tid, Fd, {_, Location}=Hints) ->
case monic_utils:pread_header(Fd, Location) of
{ok, #header{key=Key,cookie=_Cookie,size=Size,version=Version,flags= <<Deleted:1,_:15>>}} ->
{ok, #header{key=Key,cookie=Cookie,size=Size,version=Version,flags= <<Deleted:1,_:15>>}} ->
case Deleted of
0 -> ets:insert(Tid, {Key, Location, Size, Version});
0 -> ets:insert(Tid, {Key, Cookie, Location, Size, Version});
1 -> ets:delete(Tid, Key)
end,
load_main_items(Tid, Fd, {Key + 1, Location + Size + ?HEADER_SIZE + ?FOOTER_SIZE});
Expand Down Expand Up @@ -250,10 +250,10 @@ stream_out(Pid, Location, Remaining) ->

info_int(Tid, Key, Cookie) ->
case ets:lookup(Tid, Key) of
[] ->
{error, not_found};
[{Key, Location, Size, Version}] ->
{ok, {Location, Size, Version}}
[{Key, Cookie, Location, Size, Version}] ->
{ok, {Location, Size, Version}};
_ ->
{error, not_found}
end.

cleanup(#state{tid=Tid,index_fd=IndexFd,main_fd=MainFd}=State) ->
Expand Down
8 changes: 4 additions & 4 deletions src/monic_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ pread_footer(Fd, Location) ->
Else
end.

write_index(Fd, #index{key=Key,location=Location,size=Size,version=Version,flags=Flags}) ->
Bin = <<Key:64/integer, Location:64/integer, Size:64/integer,
write_index(Fd, #index{cookie=Cookie,key=Key,location=Location,size=Size,version=Version,flags=Flags}) ->
Bin = <<Key:64/integer, Cookie:32/integer, Location:64/integer, Size:64/integer,
Version:16/integer, Flags:16/bitstring>>,
file:write(Fd, Bin).

read_index(Fd) ->
case file:read(Fd, ?INDEX_SIZE) of
{ok, <<Key:64/integer, Location:64/integer, Size:64/integer,
{ok, <<Key:64/integer, Cookie:32/integer, Location:64/integer, Size:64/integer,
Version:16/integer, Flags:16/bitstring>>} ->
{ok, #index{key=Key,location=Location,size=Size,
{ok, #index{cookie=Cookie,key=Key,location=Location,size=Size,
version=Version,flags=Flags}};
{ok, _} ->
{error, invalid_index};
Expand Down

0 comments on commit 9a91ade

Please sign in to comment.