erlang ets可以找到记录,但不能删除它

vkc1a9a2  于 2023-10-14  发布在  Erlang
关注(0)|答案(1)|浏览(311)

我有一个简单的问题。ets:lookup()可以找到记录,但ets:delete()给出badarg错误。

  1. case ets:lookup(Connections, Next) of
  2. [] ->
  3. case ets:lookup(Connections, Prev) of
  4. [{Network, Node, Address}]->
  5. print_ets_table(Connections),
  6. ets:delete(Connections, Network), -> this gives error
  7. ets:insert(Connections, {Next, Node, Address}),
  8. Next;
  9. _ ->
  10. % Report
  11. Prev
  12. end;
  13. _ ->
  14. % Report
  15. Prev
  16. end;

print_ets_table(连接):

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

和误差

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

Network是二进制类型的数据,表是这样定义的

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

我读了ets文档,问了GPT,但找不到解决方案。

knsnq2tg

knsnq2tg1#

默认情况下,ets:new/2创建一个受保护的表,这意味着所有者进程可以读写表,但其他进程只能读取它。如果你想让任何进程都能删除表条目,你可以将表设置为公共的:

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

相关问题