ELIXIR – How do I retrieve the ETS key list without scanning the entire table?

I use ets to store and retrieve keys through elixir as a simple memory persistence layer, and also for occasional foldl, which involves reducing many duplicate keys with different values. I am using Baggage options.

Is there a simple O(1) method to retrieve the list of current keys without more involved table traversal or matching or folding?

Erlang or Elixir syntax response welcome.

:ets.new(:cache, [:bag, :named_table, :protected])

I have a static map of atomic keys, an integer index that I use to help insert. But not all keys are used..

chunk_key_map =% {2 => :chunk_key_2, ..... 28 => :chunk_key_28}

If there is no quick way, I know I can do an ets: find each of my static atomic keys and test them ! = [] And generate my own list, but want to see if ets supports such a function.

Thank you

Thanks, this put me on the right track 🙂

Same thing, but pass the previous key as an accumulator:

def key_stream(table_name) do
Stream.resource(
fn -> :ets.first(table_name) end,
fn :"$end_of_table" -> {:halt, nil}
previous_key -> {[previous_key], :ets.next(table_name, previous_key)} end,
fn _ -> :ok end)
end< /pre>

I use ets to store and retrieve keys through elixir as a simple memory persistence layer, and also for occasional foldl, which involves reducing many duplicate keys with different values. I am using the baggage option.

Is there an easy O(1) method to retrieve the list of current keys without more involved table traversal or matching or folding?

Erlang or Elixir syntax response welcome.

:ets.new(:cache, [:bag, :named_table, :protected])

I have a static map of atomic keys, an integer index that I use to help insert. But not all keys are used..

chunk_key_map =% {2 => :chunk_key_2, ..... 28 => :chunk_key_28}

If there is no quick way, I know I can do an ets: find each of my static atomic keys and test them ! = [] and generate my own list, but want to see if ets supports such a function.

Thank you

Thank you, this Put me on the right track 🙂

Same thing, but pass the previous key as an accumulator:

def key_stream (table_name) do
Stream.resource(
fn -> :ets.first(table_name) end,
fn :"$end_of_table" -> {:halt, nil}
previous_key -> {[previous_key], :ets.next(table_name, previous_key)} end,
fn _ -> :ok end)
end

Leave a Comment

Your email address will not be published.