Skip to content

Commit

Permalink
Add pid_registered? helper to check if a pid is in ets
Browse files Browse the repository at this point in the history
Just hides one ets usage behind a helper. No other functional changes.
  • Loading branch information
iteratee committed Jan 18, 2024
1 parent 70931cb commit 9878f71
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions lib/briefly/entry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,22 @@ defmodule Briefly.Entry do
@doc false
def give_away(path, to_pid, from_pid)
when is_binary(path) and is_pid(to_pid) and is_pid(from_pid) do
with [{^from_pid, _tmp}] <- :ets.lookup(@dir_table, from_pid),
with true <- pid_registered?(from_pid),
true <- path_owner?(from_pid, path) do
case :ets.lookup(@dir_table, to_pid) do
[{^to_pid, _tmp}] ->
:ets.insert(@path_table, {to_pid, path})
:ets.delete_object(@path_table, {from_pid, path})

:ok

[] ->
server = server()
if pid_registered?(to_pid) do
:ets.insert(@path_table, {to_pid, path})
:ets.delete_object(@path_table, {from_pid, path})
:ok
else
server = server()

{:ok, tmps} = GenServer.call(server, :roots)
{:ok, tmp} = generate_tmp_dir(tmps)
:ok = GenServer.call(server, {:give_away, to_pid, tmp, path})
{:ok, tmps} = GenServer.call(server, :roots)
{:ok, tmp} = generate_tmp_dir(tmps)
:ok = GenServer.call(server, {:give_away, to_pid, tmp, path})

:ets.delete_object(@path_table, {from_pid, path})
:ets.delete_object(@path_table, {from_pid, path})

:ok
:ok
end
else
_ ->
Expand Down Expand Up @@ -255,6 +252,10 @@ defmodule Briefly.Entry do
defp extname(%{extname: value}), do: value
defp extname(_), do: Briefly.Config.default_extname()

defp pid_registered?(pid) do
:ets.member(@dir_table, pid)
end

defp path_owner?(pid, path) do
owned_paths = :ets.lookup(@path_table, pid)
Enum.any?(owned_paths, fn {_pid, p} -> p == path end)
Expand Down

0 comments on commit 9878f71

Please sign in to comment.