Skip to content

Commit

Permalink
Optimise writing stub files
Browse files Browse the repository at this point in the history
ensure_dir only if the write failed
use raw files
  • Loading branch information
mkuratczyk committed Mar 21, 2023
1 parent 3a66d09 commit 9161634
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions deps/rabbit/src/rabbit_classic_queue_index_v2.erl
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ init1(Name, Dir, OnSyncFun, OnSyncMsgFun) ->

ensure_queue_name_stub_file(#resource{virtual_host = VHost, name = QName}, Dir) ->
QueueNameFile = filename:join(Dir, ?QUEUE_NAME_STUB_FILE),
ok = filelib:ensure_dir(QueueNameFile),
ok = file:write_file(QueueNameFile, <<"VHOST: ", VHost/binary, "\n",
ok = write_file_and_ensure_dir(QueueNameFile, <<"VHOST: ", VHost/binary, "\n",
"QUEUE: ", QName/binary, "\n",
"INDEX: v2\n">>).

Expand Down Expand Up @@ -1292,3 +1291,14 @@ highest_continuous_seq_id([SeqId1, SeqId2|Tail], EndSeqId)
highest_continuous_seq_id([SeqId2|Tail], EndSeqId);
highest_continuous_seq_id([SeqId|Tail], _) ->
{SeqId, Tail}.

write_file_and_ensure_dir(Name, IOData) ->
case file:write_file(Name, IOData, [raw]) of
ok -> ok;
{error, enoent} ->
case filelib:ensure_dir(Name) of
ok -> file:write_file(Name, IOData, [raw]);
Err -> Err
end;
Err -> Err
end.

0 comments on commit 9161634

Please sign in to comment.