From c361a564aa78258f720c9e39aacb163a150ba6c3 Mon Sep 17 00:00:00 2001 From: Simon MacMullen Date: Fri, 16 May 2014 15:54:00 +0100 Subject: Make with_handle/{1,2} into part of the fhc API, and thus hide the extra obtain/release/transfer variants. --- src/file_handle_cache.erl | 31 ++++++++++++++++++------------- src/rabbit_file.erl | 45 +++++++++++++++++++-------------------------- 2 files changed, 37 insertions(+), 39 deletions(-) diff --git a/src/file_handle_cache.erl b/src/file_handle_cache.erl index 7005a929..3a7a692c 100644 --- a/src/file_handle_cache.erl +++ b/src/file_handle_cache.erl @@ -146,9 +146,8 @@ -export([open/3, close/1, read/2, append/2, needs_sync/1, sync/1, position/2, truncate/1, current_virtual_offset/1, current_raw_offset/1, flush/1, copy/3, set_maximum_since_use/1, delete/1, clear/1]). --export([obtain/0, obtain/1, obtain/2, - release/0, release/1, release/2, transfer/1, transfer/2, transfer/3, - set_limit/1, get_limit/0, info_keys/0, +-export([obtain/0, obtain/1, release/0, release/1, transfer/1, transfer/2, + set_limit/1, get_limit/0, info_keys/0, with_handle/1, with_handle/2, info/0, info/1]). -export([ulimit/0]). @@ -234,7 +233,6 @@ {('bof' |'eof'), non_neg_integer()} | {'cur', integer()})). -type(offset() :: non_neg_integer()). --type(obtain_type() :: 'file' | 'socket'). -spec(register_callback/3 :: (atom(), atom(), [any()]) -> 'ok'). -spec(open/3 :: @@ -258,13 +256,12 @@ -spec(set_maximum_since_use/1 :: (non_neg_integer()) -> 'ok'). -spec(obtain/0 :: () -> 'ok'). -spec(obtain/1 :: (non_neg_integer()) -> 'ok'). --spec(obtain/2 :: (non_neg_integer(), obtain_type()) -> 'ok'). -spec(release/0 :: () -> 'ok'). -spec(release/1 :: (non_neg_integer()) -> 'ok'). --spec(release/2 :: (non_neg_integer(), obtain_type()) -> 'ok'). -spec(transfer/1 :: (pid()) -> 'ok'). -spec(transfer/2 :: (pid(), non_neg_integer()) -> 'ok'). --spec(transfer/3 :: (pid(), non_neg_integer(), obtain_type()) -> 'ok'). +-spec(with_handle/1 :: (fun(() -> A)) -> A). +-spec(with_handle/2 :: (non_neg_integer(), fun(() -> A)) -> A). -spec(set_limit/1 :: (non_neg_integer()) -> 'ok'). -spec(get_limit/0 :: () -> non_neg_integer()). -spec(info_keys/0 :: () -> rabbit_types:info_keys()). @@ -503,7 +500,15 @@ transfer(Pid) -> transfer(Pid, 1). obtain(Count) -> obtain(Count, socket). release(Count) -> release(Count, socket). -transfer(Pid, Count) -> transfer(Pid, Count, socket). + +with_handle(Fun) -> + with_handle(1, Fun). + +with_handle(N, Fun) -> + ok = obtain(N, file), + try Fun() + after ok = release(N, file) + end. obtain(Count, Type) when Count > 0 -> %% If the FHC isn't running, obtains succeed immediately. @@ -516,8 +521,8 @@ obtain(Count, Type) when Count > 0 -> release(Count, Type) when Count > 0 -> gen_server2:cast(?SERVER, {release, Count, Type, self()}). -transfer(Pid, Count, Type) when Count > 0 -> - gen_server2:cast(?SERVER, {transfer, Count, Type, self(), Pid}). +transfer(Pid, Count) when Count > 0 -> + gen_server2:cast(?SERVER, {transfer, Count, self(), Pid}). set_limit(Limit) -> gen_server2:call(?SERVER, {set_limit, Limit}, infinity). @@ -968,11 +973,11 @@ handle_cast({close, Pid, EldestUnusedSince}, {noreply, adjust_alarm(State, process_pending( update_counts(open, Pid, -1, State)))}; -handle_cast({transfer, N, Type, FromPid, ToPid}, State) -> +handle_cast({transfer, N, FromPid, ToPid}, State) -> ok = track_client(ToPid, State#fhc_state.clients), {noreply, process_pending( - update_counts({obtain, Type}, ToPid, +N, - update_counts({obtain, Type}, FromPid, -N, + update_counts({obtain, socket}, ToPid, +N, + update_counts({obtain, socket}, FromPid, -N, State)))}. handle_info(check_counts, State) -> diff --git a/src/rabbit_file.erl b/src/rabbit_file.erl index d6f0f632..81a617a8 100644 --- a/src/rabbit_file.erl +++ b/src/rabbit_file.erl @@ -24,6 +24,8 @@ -export([rename/2, delete/1, recursive_delete/1, recursive_copy/2]). -export([lock_file/1]). +-import(file_handle_cache, [with_handle/1, with_handle/2]). + -define(TMP_EXT, ".tmp"). %%---------------------------------------------------------------------------- @@ -81,7 +83,7 @@ file_size(File) -> _ -> 0 end. -ensure_dir(File) -> with_fhc_handle(fun () -> ensure_dir_internal(File) end). +ensure_dir(File) -> with_handle(fun () -> ensure_dir_internal(File) end). ensure_dir_internal("/") -> ok; @@ -101,23 +103,14 @@ wildcard(Pattern, Dir) -> {error, _} -> [] end. -list_dir(Dir) -> with_fhc_handle(fun () -> prim_file:list_dir(Dir) end). +list_dir(Dir) -> with_handle(fun () -> prim_file:list_dir(Dir) end). read_file_info(File) -> - with_fhc_handle(fun () -> prim_file:read_file_info(File) end). - -with_fhc_handle(Fun) -> - with_fhc_handle(1, Fun). - -with_fhc_handle(N, Fun) -> - ok = file_handle_cache:obtain(N, file), - try Fun() - after ok = file_handle_cache:release(N, file) - end. + with_handle(fun () -> prim_file:read_file_info(File) end). read_term_file(File) -> try - {ok, Data} = with_fhc_handle(fun () -> prim_file:read_file(File) end), + {ok, Data} = with_handle(fun () -> prim_file:read_file(File) end), {ok, Tokens, _} = erl_scan:string(binary_to_list(Data)), TokenGroups = group_tokens(Tokens), {ok, [begin @@ -177,7 +170,7 @@ with_synced_copy(Path, Modes, Fun) -> true -> {error, append_not_supported, Path}; false -> - with_fhc_handle( + with_handle( fun () -> Bak = Path ++ ?TMP_EXT, case prim_file:open(Bak, Modes) of @@ -206,16 +199,16 @@ append_file(File, Suffix) -> append_file(_, _, "") -> ok; append_file(File, 0, Suffix) -> - with_fhc_handle(fun () -> - case prim_file:open([File, Suffix], [append]) of - {ok, Fd} -> prim_file:close(Fd); - Error -> Error - end - end); + with_handle(fun () -> + case prim_file:open([File, Suffix], [append]) of + {ok, Fd} -> prim_file:close(Fd); + Error -> Error + end + end); append_file(File, _, Suffix) -> - case with_fhc_handle(2, fun () -> + case with_handle(2, fun () -> file:copy(File, {[File, Suffix], [append]}) - end) of + end) of {ok, _BytesCopied} -> ok; Error -> Error end. @@ -227,12 +220,12 @@ ensure_parent_dirs_exist(Filename) -> throw({error, {cannot_create_parent_dirs, Filename, Reason}}) end. -rename(Old, New) -> with_fhc_handle(fun () -> prim_file:rename(Old, New) end). +rename(Old, New) -> with_handle(fun () -> prim_file:rename(Old, New) end). -delete(File) -> with_fhc_handle(fun () -> prim_file:delete(File) end). +delete(File) -> with_handle(fun () -> prim_file:delete(File) end). recursive_delete(Files) -> - with_fhc_handle( + with_handle( fun () -> lists:foldl(fun (Path, ok) -> recursive_delete1(Path); (_Path, {error, _Err} = Error) -> Error end, ok, Files) @@ -307,7 +300,7 @@ recursive_copy(Src, Dest) -> lock_file(Path) -> case is_file(Path) of true -> {error, eexist}; - false -> with_fhc_handle( + false -> with_handle( fun () -> {ok, Lock} = prim_file:open(Path, [write]), ok = prim_file:close(Lock) end) -- cgit v1.2.1