beamologist

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

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/2 will return :undefined if the table doesn’t exist yet. Clearer + faster than searching the output of :ets.all

  • this call to :ets.lookup will not succeed… ever, because the second argument to lookup should 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 case between insert and insert_new is odd. The only difference between the two functions is their behavior if there’s an existing element with the same key, and the lookup prevents that from being relevant. What’s the intent of this structure?

  • get_raw_by_id is inefficient; look into :ets.select to avoid copying the entire table into your process. Alternatively, if lookups by edge_id are very frequent, you could maintain a separate bag table of {edge_id, edge} pairs.

  • delete_raw_by_id tries to pass a list of tuples to :ets.delete - like lookup, that function expects a single key.

cmo

cmo

Is there a process that owns the ETS table?

dimitarvp

dimitarvp

Why not use Cachex? It’s battle tested, works flawlessly, and has a ton of features.

Where Next?

Popular in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New

We're in Beta

About us Mission Statement