How to Use ETS (Erlang Term Storage) For Data Storage?

9 minutes read

ETS (Erlang Term Storage) is a mechanism in the Erlang programming language that allows for efficient storage and retrieval of data. It is primarily used for in-memory storage within an Erlang application.


To use ETS for data storage, you first need to create a table using the ets:new/2 function. This function takes two arguments: a table name (an atom) and a set of options (a list of key-value pairs). The options define the type of table and its characteristics, such as whether it is ordered or unordered, protected or private.


Once the table is created, you can insert data into it using the ets:insert/2 or ets:insert_new/2 functions. The former overwrites any existing data with the same key, while the latter only inserts if the key does not already exist. You can use any Erlang term as the key and value pair.


To retrieve data from the table, you can use the ets:lookup/2 function, specifying the table name and the key. It returns a list of matching values or an empty list if there are no matches.


In addition to single-key retrieval, ETS supports more advanced operations such as matching patterns using ets:match/2, iterating over all elements using ets:tab2list/1, and performing aggregations with ets:foldl/3 or ets:foldr/3.


If you need to update or delete data in the table, you can use the ets:update_element/3 or ets:delete/2 functions, respectively. These functions modify the data in place within the table.


Finally, when you are done with a table, you can delete it using the ets:delete/1 function, providing the table name as an argument.


It is important to note that ETS tables are not persisted between different Erlang nodes or system restarts. If you need persistent storage, you would need to implement additional logic to handle that.


ETS provides a fast and efficient way of storing and accessing data in Erlang applications, making it a valuable tool for various use cases such as caching, storing transient data, or maintaining shared state in concurrent systems.

Best Erlang Books to Read in 2024

1
Handbook of Neuroevolution Through Erlang

Rating is 5 out of 5

Handbook of Neuroevolution Through Erlang

2
Designing for Scalability with Erlang/OTP: Implement Robust, Fault-Tolerant Systems

Rating is 4.9 out of 5

Designing for Scalability with Erlang/OTP: Implement Robust, Fault-Tolerant Systems

3
Learn You Some Erlang for Great Good!: A Beginner's Guide

Rating is 4.8 out of 5

Learn You Some Erlang for Great Good!: A Beginner's Guide

4
Erlang Programming: A Concurrent Approach to Software Development

Rating is 4.7 out of 5

Erlang Programming: A Concurrent Approach to Software Development

5
Programming Erlang: Software for a Concurrent World

Rating is 4.6 out of 5

Programming Erlang: Software for a Concurrent World

6
Erlang and OTP in Action

Rating is 4.5 out of 5

Erlang and OTP in Action

7
Erlang and Elixir for Imperative Programmers

Rating is 4.4 out of 5

Erlang and Elixir for Imperative Programmers

8
Property-Based Testing with PropEr, Erlang, and Elixir: Find Bugs Before Your Users Do

Rating is 4.3 out of 5

Property-Based Testing with PropEr, Erlang, and Elixir: Find Bugs Before Your Users Do


How to retrieve data from an ETS table?

To retrieve data from an ETS (Erlang Term Storage) table in Erlang/OTP, you can use the ets:tab2list/1 function to convert the table into a list of tuples or the ets:tab2list/2 function to convert the table into a list of terms.


Here is an example of how to retrieve data from an ETS table:

  1. Create an ETS table and insert some data into it:
1
2
3
4
5
6
7
% Create an ordered set table
Table = ets:new(my_table, [ordered_set, named_table]).

% Insert data into the table
ets:insert(Table, {1, "Alice"}).
ets:insert(Table, {2, "Bob"}).
ets:insert(Table, {3, "Charlie"}).


  1. Retrieve the data from the ETS table:
1
2
3
4
5
% Retrieve the data as a list of tuples
DataList = ets:tab2list(Table).

% Print the data
io:format("~p~n", [DataList]).


The output will be:

1
[{1,"Alice"},{2,"Bob"},{3,"Charlie"}]


You can also use ets:tab2list/2 if you want to retrieve only specific fields from the table:

1
2
3
4
5
% Retrieve only the names from the table as a list
NameList = ets:tab2list(Table, 2).

% Print the names
io:format("~p~n", [NameList]).


The output will be:

1
["Alice","Bob","Charlie"]


Note: Remember to delete the table when you no longer need it using the ets:delete/1 function:

1
ets:delete(Table).


This will free the memory occupied by the table.


What is the purpose of a bag table in ETS?

In the context of ETS (Educational Testing Service), a bag table is used to conduct the test security procedures during an examination. The purpose of a bag table is to provide a designated area for test takers to securely store their personal belongings, such as bags, backpacks, coats, electronic devices, etc., during the exam. By requiring test takers to place their belongings on a bag table, it helps ensure that they do not take any prohibited items into the testing room that could potentially compromise the integrity and fairness of the test.


How to enable or disable automatic garbage collection in ETS?

By default, Erlang Term Storage (ETS) performs automatic garbage collection. However, you can enable or disable it using the setopts/2 function.


To enable automatic garbage collection in ETS:

1
ets:setopts(Table, {auto_garbage_collection, true})


To disable automatic garbage collection in ETS:

1
ets:setopts(Table, {auto_garbage_collection, false})


Here, Table is the identifier of the ETS table you want to modify. By setting the {auto_garbage_collection, true} option, ETS will automatically reclaim memory occupied by removed items. Conversely, setting the {auto_garbage_collection, false} option will disable automatic garbage collection.


Remember to choose the appropriate option based on your specific use case and the impact it may have on memory usage and performance.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

Erlang is a programming language that has gained popularity for developing scalable and fault-tolerant systems, including web applications. When it comes to web development, Erlang offers several frameworks and libraries that facilitate the process. Here is an...
To send and receive messages between Erlang processes, you can use the message-passing mechanism provided by the Erlang programming language. Here are the key points to understand:Process Identification: In Erlang, processes are identified by a unique process ...
To install Erlang on Windows, follow these steps:Visit the official Erlang website at www.erlang.org.Go to the "Download" section of the website.Choose the Windows option under the "OTP" (Open Telecom Platform) category.Select the latest versio...
Working with binary data in Erlang allows for efficient manipulation and processing of binary data structures. Erlang provides a set of built-in functions and syntax for working with binary data. Here are some important aspects of working with binary data in E...
To configure Docker to expose an Erlang node, you need to follow these steps:Create a Dockerfile: First, create a Dockerfile in your project directory. This file will specify the base image, dependencies, and configurations for your Docker container. Choose an...
To install Erlang on macOS, follow these steps:Download the Erlang package for macOS from the official Erlang website.Open the downloaded package file.Follow the on-screen instructions to begin the installation process.Choose the desired installation location ...