anthonyl

anthonyl

Have been trying to figure out the correct way to code and use a persistent term, and am stuck.

  1. How do I assign a variable to a persistent term that I can access outside the module it was created in?
  2. How would I write the following code?

def build_store do

setup Persistent Term variable here

stream CSV File. I’m ok with this part using File.stream!() and parsing using split
|> Stream.map(fn x → #Add to persistent term here# end) this bit has me stumped

end

#Once term is created
#How do I read it here from another module?

Any assistance will be greatly appreciated :grinning:

Showing Posts 1 to 10

kip

kip

ex_cldr Core Team

:persistent_term is VM-wide storage - so accessible from any module. This can create a bit of a challenge for naming so I typically use {module_name, key} as keys for persistent term.

The other consideration is balancing between a persistent storage and an amount of copying. Therefore I find myself using :persistent term when I can flatten the structure somewhat. For example, instead of storing a full map, I store each key of the map (and its value) in :persistent_term.

So to your example: Any module can access :persistent_term so stream and store to your hearts content.

anthonyl

anthonyl OP

Thx Kip.

Do you perhaps have a code sample of the correct use of persistent term? I have searched high and low and been unsuccessful in finding one.

kip

kip

ex_cldr Core Team

I don’t seem to have a good example immediately to hand. I do have an example, but arguably its not a good one. Anyway, you’re welcome to whatever value it may contribute:

Really, its a very niche, almost experimental, use case to emit a logger warning only once. Probably not a good example, but the only one I have to hand at the moment.

anthonyl

anthonyl OP

Thx Kip,

Those examples are actually quite helpful. Please confirm if my understanding is correct. You basically use it as follows:

  1. Create a unique key with the structure: modulename_key e.g mynamestore_firstname, mynamestore_lastname, mypricestore_socks

  2. Save your value as:
    `
    :persistent_term.put(“mynamestore_firstname”, “Mark”)
    :persistent_term.put(“mynamestore_lastname”, “Johnson”)
    :persistent_term.put(“mypricestore_socks”, “25.00”)
    etc…

  3. Then to access you use:

first_name = :persistent_term.get(“mynamestore_firstname”)
last_name = :persistent_term.get(“mynamestore_lastname”)
sock_price = first_name = :persistent_term.get(“mypricestore_socks”)

Is that correct?

Nicd

Nicd

Be sure to check out the performance implications of using persistent_term: persistent_term — OTP 29.0.2 (erts 17.0.2)

Namely updating and deleting terms may result in a global garbage collection pass that may or may not be acceptable on your use case.

anthonyl

anthonyl OP

Thx Nicd,

Yes, that is a very important consideration. The use case is a global lookup table that is only read once from a csv file and never updated. I believe it is ok in this use case. Is that correct?

Nicd

Nicd

Yes AFAIK that would be a good use case for persistent_term.

anthonyl

anthonyl OP

I have tried this code and it is not working:

defmodule LookupTable do

def build_lookup do

load_path = Path.join([".", "data", "lookup.csv"])


File.stream!(load_path,[:read, :utf8]) #Read File
|> Stream.map(&String.trim(&1)) #Trim Each Element
|> Stream.map(&String.split(&1, [","])) #Split at Delimiters
|> Stream.map(fn n -> :persistent_term.put(hd(n),tl(n)) end)
|> Enum.map(&IO.inspect(&1))

:persisent_term.get("global_name")

end

end

I get the following error:

** (UndefinedFunctionError) function :persisent_term.get/1
is undefined (module :persisent_term is not available)
:persisent_term.get(“global_name”)

kip

kip

ex_cldr Core Team

:persistent_term is only available on OTP 21.2 and later so I suspect you are on an older OTP release.

anthonyl

anthonyl OP

Thx Kip,

I did upgrade both Erlang and Elixir to the latest release. :persistent_term does work in iex.

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews