melmoth
Matching a value in an ETS stored list
Hi community,
Being a newbie, this question may seems obvious for you, functional veterans (i’m discovering new skies with elixir and erlang and i have to admit, i like that :).
My use case is pretty close from the elixir school:
https://elixirschool.com/lessons/specifics/ets/#advanced-lookup
(actually i store an id => map which store lists)
So considering a stored tuple containing a list, what is the golden path to match a tuple whose list contains a given value ?
To follow the elixir school example, how could i retrieve people knowing Java ?
I ve read the erlang doc / stackoverflow and it seems that lists.member is not allowed in guard.
Does the solution imply reordering data in a second ETS set of known language => user ?
Thanks for your tips !!
Fred
Most Liked
peerreynders
The point with ets:foldl/3 is that you don’t have to handle the full table scan manually - the ets module does it for you (it’s still a full table scan though). You simply give it the table, an initial value (likely an empty list) and a function fun((Element :: term(), AccIn) -> AccOut). The function is presented with each element in the table in turn and it can then stuff any element “it likes” in the accumulator (e.g. the list). The completed accumulator is then returned to you.
You should be able to test your function with Lists.foldl/3.
sasajuric
If you’re thinking about indexing, you might want to consider Mnesia, because it supports that feature out of the box. As others suggested, you’ll likely need a bag table, with fields user_id (key), and language. Then, you can add a secondary index on the language field.
minhajuddin
With :ets you get fast lookups if your key is part of the match.
In the elixir school example, “Java” is not part of the key but is a value. In which case ets has to do a full table scan which means it has to try and match all the rows in it. You are on the right track about reordering data. Inverting your values and keys and storing them in another ets table should give you the best performance. Also know that there is a :bag type of ets table available which can be used in this case.
I had a similar situation where I had to first lookup on a country and then city, which wouldn’t be the right fit performance wise for ets. So, I flipped the order and maintained a bag with {city, country}.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








