beamologist
Unexpected ETS behavior - when I delete the first record, the whole table seems to get wiped
Hi folks,
I am developing some fun project where I allow a multitude of Edge agents to connect to a Phoenix backend. I plan to store “edge info” in some ETS table, but the strange thing is that when I delete the first record, the whole table seems to get wiped.
Here is the code for the cache.
Any help much appreciated (especially at the “delete” part
Most Liked
al2o3cr
+1 to @cmo’s question about ownership - some of the places that could ultimately end up calling :ets.new are Cowboy workers and Liveviews, which will lead to the table disappearing before you want it to.
Giving the table a more-reliable parent can also reduce the need for all the “does the table exist” guards, since the table should be created during system boot.
Some other thoughts:
-
:ets.info/2will return:undefinedif the table doesn’t exist yet. Clearer + faster than searching the output of:ets.all -
this call to
:ets.lookupwill not succeed… ever, because the second argument tolookupshould be the key (edge_init.id) and not the whole tuple.edge_kv = {edge_init.id, edge_init} case :ets.lookup(:edges, edge_kv) do [] -> :ets.insert_new(:edges, edge_kv) _ -> :ets.insert(:edges, edge_kv) end -
also in that code chunk: the
casebetweeninsertandinsert_newis odd. The only difference between the two functions is their behavior if there’s an existing element with the same key, and thelookupprevents that from being relevant. What’s the intent of this structure? -
get_raw_by_idis inefficient; look into:ets.selectto avoid copying the entire table into your process. Alternatively, if lookups byedge_idare very frequent, you could maintain a separatebagtable of{edge_id, edge}pairs. -
delete_raw_by_idtries to pass a list of tuples to:ets.delete- likelookup, that function expects a single key.
cmo
Is there a process that owns the ETS table?







