Erlang ets chooses weird behavior

I have weird behavior in erlang using ets: select.

I get the correct select statement (4 and 5 below), then I make a mistake in my statement (see below), and then again I try to fulfill the same statement as in 4 and 5, and it no longer works.

What's happening? any idea?

Erlang R14B01 (erts-5.8.2) [source] [smp:2:2] [rq:2] [async-threads:0] [kernel-poll:false] Eshell V5.8.2 (abort with ^G) 1> Tab = ets:new(x, [private]). 16400 2> ets:insert(Tab, {c, "rhino"}). true 3> ets:insert(Tab, {a, "lion"}). true 4> ets:select(Tab,[{{'$1','$2'},[],['$1', '$2']}]). ["rhino","lion"] 5> ets:select(Tab,[{{'$1','$2'},[],['$1', '$2']}]). ["rhino","lion"] 6> ets:select(Tab,[{{'$1','$2'},[],['$1', '$2', '$3']}]). ** exception error: bad argument in function ets:select/2 called as ets:select(16400,[{{'$1','$2'},[],['$1','$2','$3']}]) 7> ets:select(Tab,[{{'$1','$2'},[],['$1', '$2']}]). ** exception error: bad argument in function ets:select/2 called as ets:select(16400,[{{'$1','$2'},[],['$1','$2']}]) 

Is my table table killed? Will this be an ets error?

Thanks.

+4
source share
2 answers

The shell process created the ETS table and is its owner. When the owner process dies, the ETS table is automatically deleted.

Therefore, when you get an exception of 6 , the shell process dies, so the ETS table is deleted.

Make it private also means that no other process can access it (so even if the table was saved, the new shell will not be able to access it), but in this case it is even worse since the table has been deleted.

+6
source

(too big to comment on the correct answer thanosQR)

if you want the table to withstand the exception in the shell, you can pass it to another process. eg:

 1> Pid = spawn(fun () -> receive foo -> ok end end). % sit and wait for 'foo' message <0.62.0> 2> Tab = ets:new(x, [public]). % Tab must be public if you plan to give it away and still have access 24593 3> ets:give_away(Tab, Pid, []). true 4> ets:insert(Tab, {a,1}). true 5> ets:tab2list(Tab). [{a,1}] 6> 3=4. ** exception error: no match of right hand side value 4 7> ets:tab2list(Tab). % Tab survives exception [{a,1}] 8> Pid ! foo. % cause owning process to exit foo 9> ets:tab2list(Tab). % Tab is now gone ** exception error: bad argument in function ets:match_object/2 called as ets:match_object(24593,'_') in call from ets:tab2list/1 (ets.erl, line 323) 
+3
source

Source: https://habr.com/ru/post/1442492/


All Articles