bezzoon
bezzoon

Reputation: 2019

Unable to delete and select from an ets table

I am having trouble retrieving and deleting records from ETS.

After inserting them like:

:ets.insert(
  :table_name,
  {id, location, [name, x, z]}
)

I am unable to delete them like so:

:ets.delete(:table_name, id) 

I have also tried and this doesn't return anything.

:ets.match(:table_name, {id, location, '_'}) 

Upvotes: 0

Views: 197

Answers (2)

Everett
Everett

Reputation: 9558

If you want to use an Elixir wrapper around ETS (and DETS), you can use pockets:

iex> Pockets.new(:table_name)
{:ok, :table_name}
iex> Pockets.put(:table_name, :id, "Foo")
:table_name
iex> Pockets.get(:table_name, :id)
"Foo"

Upvotes: 3

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

You always can review the content of :ets with :ets.foldl/3

ets = :ets.new(:table_name, [])

:ets.insert(ets, {1, 2, ["foo", 3, 4]})       
#⇒ true

:ets.foldl(&[&1|&2], [], ets)
#⇒ [{1, 2, ["foo", 3, 4]}]

:ets.delete/2 works out of the box:

:ets.delete ets, 1
#⇒ true

For :ets.match/2 one should use atoms ( differs from in how to denote them) and specify what to return back.

:ets.match(ets, {:_, :_, :'$1'})  
#⇒ [[["foo", 3, 4]]]

:ets.match(ets, {1, :'$1', :'$2'})
#⇒ [[2, ["foo", 3, 4]]]

Upvotes: 2

Related Questions