Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tuple as Key for ets table?

Tags:

erlang

ets

I want to store tuples regarding information on other network participants in an ETS table. I would like to use a tuple of {ip_address(), port_number()} (return of inet:peername(Socket) as the key of the entries. Aka I want to use the combination of address / port as an identifier for the other nodes / entries in the ETS table.

Is this possible with ets? Or do I have to use an other type as a key?

like image 634
tnull Avatar asked May 27 '26 03:05

tnull


1 Answers

ETS stands for "Erlang Term Storage" and it can store any Erlang term, including tuples and tuples of tuples. The description section of the ets man page details how terms are compared and matched for particular table types, and the documentation for ets:lookup/2 discusses this as well. For some table types, Erlang term order is important.

There is no confusion regarding which part of a stored tuple is the key; the ets:new/2 function, used to create a table, includes the {keypos, Pos} option to indicate the key element. If {keypos, Pos} is not provided, the first element is the key.

like image 96
Steve Vinoski Avatar answered May 28 '26 15:05

Steve Vinoski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!