Skip to content

Commit

Permalink
Fix ets lookup for OTP 25
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed Nov 10, 2023
1 parent 1c57104 commit f7ee241
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions lib/orb/constants.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,31 @@ defmodule Orb.Constants do
:not_compiling

tid ->
case :ets.lookup_element(tid, string, 2, nil) do
offset when is_integer(offset) ->
{:ok, offset}

nil ->
count = byte_size(string) + 1
new_offset = :ets.update_counter(tid, :offset, {2, count})
offset = new_offset - count
:ets.insert(tid, {string, offset})
{:ok, offset}
end
upsert_offset(tid, string)
end
end

defp safe_ets_lookup(tid, key) do
# When OTP 26 is minimum version, can be replaced with:
# :ets.lookup_element(tid, key, 2, nil)

case :ets.lookup(tid, key) do
[{^key, value}] -> value
_ -> nil
end
end

defp upsert_offset(tid, string) do
case safe_ets_lookup(tid, string) do
offset when is_integer(offset) ->
{:ok, offset}

nil ->
count = byte_size(string) + 1
new_offset = :ets.update_counter(tid, :offset, {2, count})
offset = new_offset - count
:ets.insert(tid, {string, offset})
{:ok, offset}
end
end

Expand Down

0 comments on commit f7ee241

Please sign in to comment.