2
votes

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?

1
You mean like ets:insert(Table, {{"127.0.0.1", 4000}, value}).? That seems to be working fine. - Dogbert
More like ets:insert(Table, {{{0,0,0,0,0,0,0,1}, 4000}, value}).. It does seem to work fine for me, too. I however recently got some strange errors and could not find information on whether one could use tuples. I could imagine maybe erlang gets confused which of the nested tuples it should regard as key. - tnull
This should also be fine. Maybe add some relevant code and the exact error you get. - A. Sarid

1 Answers

4
votes

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.