Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

ETS

notebooks/ets_basics.livemd

ETS

Mix.install([
  {:kino, "~> 0.9.0"}
])

ETS

  • general purpose data storage
  • the owning process of an ETS table needs to be running

https://www.erlang.org/doc/man/ets.html

:ets.new/2

  • create a table instance
  • data structures
    • :set
    • :ordered_set
    • :bag
    • :duplicated_bag
  • access level
    • :public
    • :protected
    • :private
  • named table
    • :named_table

https://www.erlang.org/doc/man/ets.html#new-2

:ets.new(:my_ets_set, [:set, :named_table])

:ets.insert/2

  • insert an entry

https://www.erlang.org/doc/man/ets.html#insert-2

grouped_users = [
  {1, %{name: "foo", group: :a}},
  {2, %{name: "bar", group: :b}},
  {3, %{name: "baz", group: :a}}
]

# insert data entries
for user <- grouped_users do
  true = :ets.insert(:my_ets_set, user)
end

Kino.ETS.new(:my_ets_set)

:ets.tab2list/1

  • dump all entries

https://www.erlang.org/doc/man/ets.html#tab2list-1

:ets.tab2list(:my_ets_set)

:ets.info/1

  • return information about a table as a list of tuples

https://www.erlang.org/doc/man/ets.html#info-1

table_info = :ets.info(:my_ets_set)

:ets.lookup/2

  • list all objects with a key in a table
  • used when IDs are known

https://www.erlang.org/doc/man/ets.html#lookup-2

:ets.lookup(:my_ets_set, 1)