This module is an interface to the Erlang built-in term storage BIFs.
These provide the ability to store very large quantities of data in
an Erlang runtime system, and to have constant access time to the
data. (In the case of ordered_set
, see below, access time is
proportional to the logarithm of the number of objects stored).
Data is organized as a set of dynamic tables, which can store tuples. Each table is created by a process. When the process terminates, the table is automatically destroyed. Every table has access rights set at creation.
Tables are divided into four different types, set
,
ordered_set
, bag
and duplicate_bag
.
A set
or ordered_set
table can only have one object
associated with each key. A bag
or duplicate_bag
can
have many objects associated with each key.
The number of tables stored at one Erlang node is limited.
The current default limit is approximately 1400 tables. The upper
limit can be increased by setting the environment variable
ERL_MAX_ETS_TABLES
before starting the Erlang runtime system
(i.e. with the -env
option to erl
/werl
).
The actual limit may be slightly higher than the one specified, but
never lower.
Note that there is no automatic garbage collection for tables.
Even if there are no references to a table from any process, it will
not automatically be destroyed unless the owner process terminates.
It can be destroyed explicitly by using delete/1
.
Some implementation details:
safe_fixtable/2
function can
be used to guarantee that a sequence of first/1
and
next/2
calls will traverse the table without errors even if
another process (or the same process) simultaneously deletes or
inserts objects in the table.'$end_of_table'
should not be used as a key since this
atom is used to mark the end of the table when using
first
/next
.In general, the functions below will exit with reason badarg
if
any argument is of the wrong format, or if the table identifier is
invalid.
The type tid()
is used to denote a table identifier. Note that
the internal structure of this type is implementation-specific.
Tab = tid() | atom()
Returns a list of all tables at the node. Named tables are given by their names, unnamed tables are given by their table identifiers.
Tab = tid() | atom()
Deletes the entire table Tab
.
Tab = tid() | atom()
Key = term()
Deletes all objects with the key Key
from the table
Tab
.
file2tab(Filename) -> {ok,Tab} | {error,Reason}
Filename = string() | atom()
Tab = tid() | atom()
Reason = term()
Reads a file produced by tab2file/2
and creates the
corresponding table Tab
.
first(Tab) -> Key | '$end_of_table'
Tab = tid() | atom()
Key = term()
Returns the first key Key
in the table Tab
.
If the table is of the ordered_set
type, the first key
in Erlang term order will be returned. If the table is of any
other type, the first key according to the table's internal
order will be returned. If the table is empty,
'$end_of_table'
will be returned.
Use next/2
to find subsequent keys in the table.
fixtable(Tab, true|false) -> true | false
Tab = tid() | atom()
The function is retained for backwards compatibility only.
Use |
Fixes a table for safe traversal. The function is primarily used by the Mnesia DBMS to implement functions which allow write operations in a table, although the table is in the process of being copied to disk or to another node. It does not keep track of when and how tables are fixed.
foldl(Function, Acc0, Tab) -> Acc1
Function = fun(A, AccIn) -> AccOut
Tab = tid() | atom()
Acc0 = Acc1 = AccIn = AccOut = term()
Acc0
is returned if the table is empty.
This function is similar to lists:foldl/3
. The order in
which the elements of the table are traversed is unspecified,
except for tables of type ordered_set
, for which they
are traversed first to last.
foldr(Function, Acc0, Tab) -> Acc1
Function = fun(A, AccIn) -> AccOut
Tab = tid() | atom()
Acc0 = Acc1 = AccIn = AccOut = term()
Acc0
is returned if the table is empty.
This function is similar to lists:foldr/3
. The order in
which the elements of the table are traversed is unspecified,
except for tables of type ordered_set
, for which they
are traversed last to first.
Displays information about all ETS tables on tty.
Tab = tid() | atom()
Browses the table Tab
on tty.
info(Tab) -> [{Item,Value}] | undefined
Tab = tid() | atom()
Item, Value - see below
Returns information about the table Tab
as a list of
{Item,Value}
tuples:
Item=memory, Value=int()
Item=owner, Value=pid()
Item=name, Value=atom()
Item=size, Value=int()
Item=node, Value=atom()
Item=named_table, Value=true|false
Item=type, Value=set|ordered_set|bag|duplicate_bag
Item=keypos, Value=int()
Item=protection, Value=public|protected|private
info(Tab, Item) -> Value | undefined
Tab = tid() | atom()
Item, Value - see below
Returns the information associated with Item
for
the table Tab
. In addition to the {Item,Value}
pairs defined for info/1
, the following items are
allowed:
Item=fixed, Value=true|false
Item=safe_fixed, Value={FirstFixed,Info}|false
safe_fixtable/2
,
the call returns a tuple where FirstFixed
is the time
when the table was first fixed by a process, which may or may
not be one of the processes it is fixed by right now.Info
is a possibly empty lists of tuples
{Pid,RefCount}
, one tuple for every process the table is
fixed by right now. RefCount
is the value of the
reference counter, keeping track of how many times the table
has been fixed by the process.false
.Tab = tid() | atom()
Object = tuple()
Inserts the object Object
into the table Tab
.
If there already exists an object with the same key as
Object
, and the table is a set
or ordered_set
table, the old object will be replaced.
last(Tab) -> Key | '$end_of_table'
Tab = tid() | atom()
Key = term()
Returns the last key Key
according to Erlang term order
in the table Tab
of the ordered_set
type. If
the table is of any other type, the function is synonymous to
first/2
. If the table is empty, '$end_of_table'
is
returned.
Use prev/2
to find preceding keys in the table.
Tab = tid() | atom()
Key = term()
Object = tuple()
Returns a list of all objects with the key Key
in
the table Tab
.
If the table is of type set
or ordered_set
,
the function returns either the empty list or a list with one
element, as there cannot be more than one object with the same
key. If the table is of type bag
or duplicate_bag
,
the function returns a list of arbitrary length.
Note that the time order of object insertions is preserved; The first object inserted with the given key will be first in the resulting list, and so on.
Insert and look-up times in tables of type set
, bag
and duplicate_bag
are constant, regardless of the size of
the table. For the ordered_set
data-type, time is
proportional to the (binary) logarithm of the number of objects.
lookup_element(Tab, Key, Pos) -> Elem
Tab = tid() | atom()
Key = term()
Pos = int()
Elem = term() | [term()]
If the table Tab
is of type set
or
ordered_set
, the function returns the Pos
:th
element of the object with the key Key
.
If the table is of type bag
or duplicate_bag
,
the functions returns a list with the Pos
:th element of
every object with the key Key
.
If no object with the key Key
exists, the function will
exit with reason badarg
.
match(Tab, Pattern) -> [Match]
Tab = tid() | atom()
Pattern = tuple()
Match = [term()]
Matches the objects in the table Tab
against the pattern
Pattern
.
A pattern is a term that may contain:
'_'
which matches any Erlang term, and'$N'
where N
=0,1,...The function returns a list with one element for each matching object, where each element is an ordered list of pattern variable bindings. An example:
> ets:match(T, '$1'). % Matches every object in the table [{rufsen,dog,7},{brunte,horse,5},{ludde,dog,5}] > ets:match(T, {'_',dog,'$1'}). [[7],[5]] > ets:match(T, {'_',cow,'$1'}). []
If the key is specified in the pattern, the match is very efficient. If the key is not specified, i.e. if it is a variable or an underscore, the entire table must be searched. The search time can be substantial if the table is very large.
On tables of the ordered_set
type, the result is in the
same order as in a first/next
traversal.
match_delete(Tab, Pattern) -> true
Tab = tid() | atom()
Pattern = tuple()
Deletes all objects which match the pattern Pattern
from
the table Tab
. See match/2
for a description of
patterns.
match_object(Tab, Pattern) -> [Object]
Tab = tid() | atom()
Pattern = Object = tuple()
Matches the objects in the table Tab
against the pattern
Pattern
. See match/2
for a description of patterns.
The function returns a list of all objects which match the pattern.
If the key is specified in the pattern, the match is very efficient. If the key is not specified, i.e. if it is a variable or an underscore, the entire table must be searched. The search time can be substantial if the table is very large.
On tables of the ordered_set
type, the result is in the
same order as in a first/next
traversal.
Name = atom()
Options = [Option]
Option = Type | Access | named_table | {keypos,Pos}
Type = set | ordered_set | bag | duplicate_bag
Access = public | protected | private
Pos = int()
Creates a new table and returns a table identifier which can be used in subsequent operations. The table identifier can be sent to other processes so that a table can be shared between different processes within a node.
The parameter Options
is a list of atoms which specifies
table type, access rights, key position and if the table is named
or not. If one or more options are left out, the default values
are used. This means that not specifying any options ([]
) is
the same as specifying [set,protected,{keypos,1}]
.
set
The table is a set
table - one key, one object, no
order among objects. This is the default table type.ordered_set
The table is a ordered_set
table - one key, one
object, ordered in Erlang term order, which is the order
implied by the < and > operators. Tables of this type
have a somewhat different behavior in some situations
than tables of the other types.bag
The table is a bag
table which can have many objects,
but only one instance of each object, per key.duplicate_bag
The table is a duplicate_bag
table which can have many
objects, including multiple copies of the same object, per
key.public
Any process may read or write to the table.protected
The owner process can read and write to the table. Other
processes can only read the table. This is the default
setting for the access rights.private
Only the owner process can read or write to the table.named_table
If this option is present, the name Name
is associated
with the table identifier. The name can then be used
instead of the table identifier in subsequent operations.{keypos,Pos}
Specfies which element in the stored tuples should be used as
key. By default, it is the first element, i.e. Pos=1
.
However, this is not always appropriate. In particular,
we do not want the first element to be the key if we want to
store Erlang records in a table.Pos
number of elements.next(Tab, Key1) -> Key2 | '$end_of_table'
Tab = tid() | atom()
Key1 = Key2 = term()
Returns the next key Key2
, following the key Key1
in the table Tab
. If the table is of the ordered_set
type, the next key in Erlang term order is returned. If the table
is of any other type, the next key according to the table's
internal order is returned. If there is no next key,
'$end_of_table'
is returned.
Use first/1
to find the first key in the table.
Unless a table of type set
, bag
or
duplicate_bag
is protected using safe_fixtable/2
,
see below, a traversal may fail if concurrent updates are made
to the table.
If the table is of type ordered_set
, the function returns
the next key in order, even if the object does no longer exist.
prev(Tab, Key1) -> Key2 | '$end_of_table'
Tab = tid() | atom()
Key1 = Key2 = term()
Returns the previous key Key2
, preceding the key
Key1
according the Erlang term order in the table
Tab
of the ordered_set
type. If the table is of
any other type, the function is synonymous to next/2
.
If there is no previous key, '$end_of_table'
is returned.
Use last/1
to find the last key in the table.
Tab = Name = atom()
Renames the named table Tab
to the new name Name
.
Afterwards, the old name can not be used to access the table.
Renaming an unnamed table has no effect.
safe_fixtable(Tab, true|false) -> true | false
Tab = tid() | atom()
Fixes a table of the set
, bag
or
duplicate_bag
table type for safe traversal.
A process fixes a table by calling safe_fixtable(Tab,true)
.
The table remains fixed until the process releases it by calling
safe_fixtable(Tab,false)
, or until the process terminates.
If several processes fix a table, the table will remain fixed until all processes have released it (or terminated). A reference counter is kept on a per process basis, and N consecutive fixes requires N releases to actually release the table.
When a table is fixed, a sequence of first/1
and
next/2
calls are guaranteed to succeed even if objects
are removed during the traversal. An example:
clean_all_with_value(Tab,X) -> safe_fixtable(Tab,true), clean_all_with_value(Tab,X,ets:first(Tab)), safe_fixtable(Tab,false). clean_all_with_value(Tab,X,'$end_of_table') -> true; clean_all_with_value(Tab,X,Key) -> case ets:lookup(Tab,Key) of [{Key,X}] -> ets:delete(Tab,Key); _ -> true end, clean_all_with_value(Tab,X,ets:next(Tab,Key)).
Note that no deleted objects are actually removed from a fixed table until it has been released. If a process fixes a table but never releases it, the memory used by the deleted objects will never be freed. The performance of operations on the table will also degrade significantly.
Use info/2
to retrieve information about which processes
have fixed which tables. A system with a lot of processes fixing
tables may need a monitor which sends alarms when tables have
been fixed for too long.
Note that for tables of the ordered_set
type,
safe_fixtable/2
is not necessary as calls to first/1
and next/2
will always succeed.
slot(Tab, I) -> [Object] | '$end_of_table'
Tab = tid() | atom()
I = int()
Object = tuple()
The function is deprecated and may be removed from future
releases. Use |
Returns all objects in the I
:th slot of the table
Tab
. A table can be traversed by repeatedly calling
the function, starting with the first slot I=0
and
ending when '$end_of_table'
is returned.
The function will fail with reason badarg
if the I
argument is out of range.
Unless a table of type set
, bag
or
duplicate_bag
is protected using safe_fixtable/2
,
see above, a traversal may fail if concurrent updates are made
to the table.
If the table is of type ordered_set
, the function returns
a list containing the I
:th object in Erlang term order.
tab2file(Tab, Filename) -> ok | {error,Reason}
Tab = tid() | atom()
Filename = string() | atom()
Reason = term()
Dumps the table Tab
to the file Filename
.
The implementation of this function is not efficient.
Tab = tid() | atom()
Object = tuple()
Returns a list of all objects in the table Tab
.
update_counter(Tab, Key, {Pos,Incr}) -> Result
update_counter(Tab, Key, Incr) -> Result
Tab = tid() | atom()
Key = term()
Pos = Incr = Result = int()
This functions provides an efficient way to update a counter, without the hassle of having to look up an object, update the object by incrementing an element and insert the resulting object into the table again.
It will destructively update the object with key Key
in
the table Tab
by adding Incr
to the element at
the Pos
:th position. The new counter value is returned.
If no position is specified, the element directly following
the key (<keypos>+1
) is updated.
The function will fail with reason badarg
if:
set
or ordered_set
,