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

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Newson committed Mar 13, 2011
1 parent bdb22a4 commit 5125e37
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 89 deletions.
65 changes: 32 additions & 33 deletions src/monic_file.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
-export([init/1, terminate/2, code_change/3,handle_call/3, handle_cast/2, handle_info/2]).

-record(state, {
data_start_pos,
tid,
index_fd=nil,
main_fd=nil,
next_index,
reset_pos,
write_pos,
writer=nil
}).
data_start_pos,
tid,
index_fd=nil,
main_fd=nil,
next_index,
reset_pos,
write_pos,
writer=nil
}).

%% public functions

Expand Down Expand Up @@ -79,12 +79,12 @@ init(Path) ->
case load_main(Tid, Path, LastLoc) of
{ok, MainFd, Eof} ->
{ok, #state{
index_fd=IndexFd,
main_fd=MainFd,
reset_pos=Eof,
write_pos=Eof,
tid=Tid
}};
index_fd=IndexFd,
main_fd=MainFd,
reset_pos=Eof,
write_pos=Eof,
tid=Tid
}};
Else ->
{stop, Else}
end;
Expand All @@ -93,7 +93,7 @@ init(Path) ->
end.

handle_call({start_writing, Key, Size}, _From,
#state{main_fd=MainFd, write_pos=Pos, writer=nil}=State) ->
#state{main_fd=MainFd, write_pos=Pos, writer=nil}=State) ->
Ref = make_ref(),
Cookie = monic_utils:new_cookie(),
LastModified = now(),
Expand All @@ -102,7 +102,7 @@ handle_call({start_writing, Key, Size}, _From,
case monic_utils:pwrite_term(MainFd, Pos, Header) of
{ok, HeaderSize} ->
{reply, {ok, Ref}, State#state{data_start_pos=Pos + HeaderSize,
next_index=Index, write_pos=Pos + HeaderSize, writer=Ref}};
next_index=Index, write_pos=Pos + HeaderSize, writer=Ref}};
Else ->
{reply, Else, abandon_write(State)}
end;
Expand All @@ -114,27 +114,27 @@ handle_call({write, Ref, {Bin, Next}}, _From, #state{main_fd=Fd, write_pos=Pos,
Size = iolist_size(Bin),
Remaining = Index#index.size - (Pos + Size - State#state.data_start_pos),
Write = case {Next, Remaining} of
{_, Remaining} when Remaining < 0 ->
{error, overflow};
{done, Remaining} when Remaining > 0 ->
{error, underflow};
_ ->
file:pwrite(Fd, Pos, Bin)
end,
{_, Remaining} when Remaining < 0 ->
{error, overflow};
{done, Remaining} when Remaining > 0 ->
{error, underflow};
_ ->
file:pwrite(Fd, Pos, Bin)
end,
case {Next, Write} of
{done, ok} ->
case file:datasync(Fd) of
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}),
Index#index.location, Index#index.size, Index#index.last_modified}),
{reply, {ok, Index#index.cookie},
State#state{next_index=nil, reset_pos=Pos + Size,
write_pos=Pos + Size, writer=nil}};
State#state{next_index=nil, reset_pos=Pos + Size,
write_pos=Pos + Size, writer=nil}};
Else ->
{reply, Else, abandon_write(State)}
end;
end;
{_, ok} ->
{reply, {continue, Next}, State#state{write_pos=Pos + Size}};
{_, Else} ->
Expand Down Expand Up @@ -171,7 +171,7 @@ terminate(_Reason, State) ->
cleanup(State).

code_change(_OldVsn, State, _Extra) ->
{ok, State}.
{ok, State}.

%% private functions

Expand All @@ -194,7 +194,7 @@ load_index_items(Tid, Fd) ->
load_index_items(Tid, Fd, IndexLocation, LastLoc) ->
case monic_utils:pread_term(Fd, IndexLocation) of
{ok, IndexSize, #index{key=Key,cookie=Cookie,location=Location,size=Size,
last_modified=LastModified,deleted=Deleted}} ->
last_modified=LastModified,deleted=Deleted}} ->
case Deleted of
false -> ets:insert(Tid, {Key, Cookie, Location, Size, LastModified});
true -> ets:delete(Tid, Key)
Expand Down Expand Up @@ -269,7 +269,7 @@ stream_out(Pid, Location, Remaining) ->
end.

info_int(Tid, Key, Cookie) ->
case ets:lookup(Tid, Key) of
case ets:lookup(Tid, Key) of
[{Key, Cookie, Location, Size, Version}] ->
{ok, {Location, Size, Version}};
_ ->
Expand All @@ -281,7 +281,7 @@ cleanup(#state{tid=Tid,index_fd=IndexFd,main_fd=MainFd}=State) ->
close_int(MainFd),
close_ets(Tid),
State#state{tid=nil,index_fd=nil,main_fd=nil}.

close_int(nil) ->
ok;
close_int(Fd) ->
Expand All @@ -291,4 +291,3 @@ close_ets(nil) ->
ok;
close_ets(Tid) ->
ets:delete(Tid).

20 changes: 10 additions & 10 deletions src/monic_file_lru.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
-export([init/1, terminate/2, code_change/3,handle_call/3, handle_cast/2, handle_info/2]).

-record(state, {
by_time,
by_file,
capacity
}).
by_time,
by_file,
capacity
}).

%% public functions.

Expand All @@ -43,10 +43,10 @@ init({0, _}) ->
{stop, capacity_too_low};
init(Capacity) ->
State = #state{
capacity = Capacity,
by_file = ets:new(monic_by_file, [set, private]),
by_time = ets:new(monic_by_time, [ordered_set, private])
},
capacity = Capacity,
by_file = ets:new(monic_by_file, [set, private]),
by_time = ets:new(monic_by_time, [ordered_set, private])
},
{ok, State}.

handle_call({update, File}, _From, State) ->
Expand All @@ -66,10 +66,10 @@ terminate(_Reason, #state{by_file=ByFile,by_time=ByTime}) ->
ets:delete(ByTime).

code_change(_OldVsn, State, _Extra) ->
{ok, State}.
{ok, State}.

maybe_evict_files(#state{by_file=ByFile, by_time=ByTime,
capacity=Capacity}) ->
capacity=Capacity}) ->
case ets:info(ByFile, size) of
Capacity ->
[{OldestTime, OldestFile}] = ets:lookup(ByTime, ets:first(ByTime)),
Expand Down
66 changes: 33 additions & 33 deletions src/monic_file_resource.erl
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
% Copyright 2011 Cloudant
%
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.
%% Copyright 2011 Cloudant
%%
%% Licensed under the Apache License, Version 2.0 (the "License"); you may not
%% use this file except in compliance with the License. You may obtain a copy of
%% the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
%% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
%% License for the specific language governing permissions and limitations under
%% the License.

-module(monic_file_resource).
-export([init/1,
allowed_methods/2,
content_types_accepted/2,
content_types_provided/2,
create_path/2,
delete_resource/2,
is_conflict/2,
post_is_create/2,
resource_exists/2,
valid_entity_length/2]).
allowed_methods/2,
content_types_accepted/2,
content_types_provided/2,
create_path/2,
delete_resource/2,
is_conflict/2,
post_is_create/2,
resource_exists/2,
valid_entity_length/2]).
-export([show_file/2, create_file/2, add_item/2]).

-include_lib("webmachine/include/webmachine.hrl").
Expand All @@ -37,9 +37,9 @@ content_types_accepted(ReqData, Context) ->
{[{"application/json", create_file}], ReqData, Context};
'POST' ->
CT = case wrq:get_req_header("Content-Type", ReqData) of
undefined -> "application/octet-stream";
Other -> Other
end,
undefined -> "application/octet-stream";
Other -> Other
end,
{[{CT, add_item}], ReqData, Context}
end.

Expand All @@ -51,9 +51,9 @@ create_path(ReqData, Context) ->

delete_resource(ReqData, Context) ->
Result = case file:delete(monic_utils:path(ReqData, Context)) of
ok -> true;
_ -> false
end,
ok -> true;
_ -> false
end,
{Result, ReqData, Context}.

init(ConfigProps) ->
Expand All @@ -70,11 +70,11 @@ resource_exists(ReqData, Context) ->

valid_entity_length(ReqData, Context) ->
Valid = case wrq:method(ReqData) of
'POST' ->
wrq:get_req_header("Content-Length", ReqData) /= undefined;
_ ->
true
end,
'POST' ->
wrq:get_req_header("Content-Length", ReqData) /= undefined;
_ ->
true
end,
{Valid, ReqData, Context}.

%% private functions
Expand Down
26 changes: 13 additions & 13 deletions src/monic_utils.erl
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
% Copyright 2011 Cloudant
%
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.
%% Copyright 2011 Cloudant
%%
%% Licensed under the Apache License, Version 2.0 (the "License"); you may not
%% use this file except in compliance with the License. You may obtain a copy of
%% the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
%% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
%% License for the specific language governing permissions and limitations under
%% the License.

-module(monic_utils).
-export([path/2, exists/2, open/2]).
Expand Down

0 comments on commit 5125e37

Please sign in to comment.