erlangelixirin-memory-databaseets

ets can find the record but can't delete it


i have a simple problem. ets:lookup() can find the record but ets:delete() gives badarg error.

case ets:lookup(Connections, Next) of
    [] ->
         case ets:lookup(Connections, Prev) of
             [{Network, Node, Address}]->
                  print_ets_table(Connections),
                  ets:delete(Connections, Network), -> this gives error
                  ets:insert(Connections, {Next, Node, Address}),
                  Next;
             _ ->
                  % Report
                  Prev
                  end;
    _ ->
        % Report
          Prev
         end;

print_ets_table(Connections):

[{<<134,176,18,190,115,242,102,213>>,
  {sslsocket,{gen_tcp,#Port<0.6>,tls_connection,
                      [{option_tracker,<0.110.0>},
                       {session_tickets_tracker,disabled},
                       {session_id_tracker,<0.111.0>}]},
             [<0.114.0>,<0.113.0>]},
  <<127,0,0,1>>}]

and the error

{badarg,[{ets,delete,
              [#Ref<0.3551319967.1683357697.51087>,
               <<134,176,18,190,115,242,102,213>>],
              [{error_info,#{cause => access,module => erl_stdlib_errors}}]}

Network is binary type data and this is how table was defined

Connections = ets:new(connections, [set])

i read the ets doc and ask GPT but could not find the solution


Solution

  • By default, ets:new/2 creates a protected table, which means the owner process can read and write the table but other processes can only read it. If you want any process to be able to delete table entries, you can make the table public instead:

    ets:new(connections, [set, public]).