To allow data lookup, I would like to use an ETS table.
I have a 1 to n relation that I want to store, where I usually have the 1 and want to find the n.
My first thought was to use a :set with key->list() lookup and, if more options are added to the key, append the option to the list.
:ets.update_element
sounds like a good thing to use here, but I think I do not understand the syntax.
What I can do is:
:ets.new(:table, [:set, :protected, :named_table])
:ets.insert(:table, {:a, 10})
:ets.insert(:table, {:b, 11})
:ets.insert(:table, [c: 12, d: 13, e: 14])
:ets.lookup(:table, :d) # <- returns [d: 14]
Here, numbers are used for simplicity.
But I cannot figure out how to update a value, for example turn d: 14 into d: rem(14,3).
I managed to āupdateā the value using
:ets.update_element(:table, :d, {2, rem(14,3})
āupdateā is in quotation marks, because the table entry is updated, but the value is simply set without regard of its previous value. Is there a way to alter the value based on the previous value? Maybe something like
:ets.update_element(:table, :d, {2, &(rem(&1, 3))}) # actually stores the _function_, not the result
Or is the approach of using a :set and storing the list with n elements for 1 given key not a good fit in this scenario?