Erlang (programming language)/Tutorials/ETS: Difference between revisions

From Citizendium
Jump to navigation Jump to search
imported>Eric Evers
imported>Tom Morris
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{subpages}}
=ETS local data storage=
=ETS local data storage=


ETS is the erlang table storage system, which provides hash storage and access functions.
ETS is the erlang table storage system, which provides hash storage and access functions.
ETS data is stored in a process as long as it is running.
ETS data is stored in a process as long as it is running.
Here is a simple sample of how to use the most simple functions in ETS
Here is a sample of how to use some simple functions in ETS


==Sample program: test_ets.erl==
==Sample program: test_ets.erl==
Line 17: Line 19:
  populate( Kingdom, [{micky,mouse}, {mini,mouse}, {goofy}] ),
  populate( Kingdom, [{micky,mouse}, {mini,mouse}, {goofy}] ),
  Member = ets:member( Kingdom, micky ),
  Member = ets:member( Kingdom, micky ),
  io:format( " ~w ~n ", [ Member ] ),
  io:format( " member ~w ~n ", [ Member ] ),
  show_next_key( Kingdom, micky ),
  show_next_key( Kingdom, micky ),
  find_animal( Kingdom, Animal ).
  find_animal( Kingdom, Animal ).
Line 39: Line 41:
  % ==============
  % ==============
  % 53> test_ets:start().
  % 53> test_ets:start().
  % true  
  % member true  
  %  next mini  
  %  next mini  
  %  next goofy  
  %  next goofy  
  %  next '$end_of_table'  
  %  next '$end_of_table'  
  %  [[mini],[micky]]
  %  [[mini],[micky]]
==Advanced options==
There are a number of options for ets data.
Data structure options include:
set, ordered set, bag, and duplicate bag.
A set has only unique keys and values. A sorted version of set exists. A bag can have duplicate keys. A duplicate bag can have duplicate keys and values.
set          [{zod, alice},{smith, joe}]
ordered set  [{smith, joe},{zod, alice}]
bag          [{smith, joe},{smith, ann}]
duplicate bag [{smith, joe},{smith, joe]]
Access options include:
private, protected, and public.
Private ets data is limited to the owning process. Protected ets tables are readable by other processes. Public ets tables have read and write access by other processes.
The options are passed in the List_of_options.
ets:new( 'magic', List_of_options)
An empty List_of_options leaves the default values active:
[set, protected, {keypos,1}]

Latest revision as of 07:07, 8 August 2009


ETS local data storage

ETS is the erlang table storage system, which provides hash storage and access functions. ETS data is stored in a process as long as it is running. Here is a sample of how to use some simple functions in ETS

Sample program: test_ets.erl

-module(test_ets).
-compile(export_all).

start() -> start( mouse ).

start( Animal ) ->
	Kingdom = ets:new( 'magic',  [] ),
	% note: table is not square
	populate( Kingdom, [{micky,mouse}, {mini,mouse}, {goofy}] ),
	Member = ets:member( Kingdom, micky ),
	io:format( " member ~w ~n ", [ Member ] ),
	show_next_key( Kingdom, micky ),
	find_animal( Kingdom, Animal ).
	
show_next_key( _Kingdom, '$end_of_table' ) -> done;
show_next_key( Kingdom,  Key) ->
	Next = ets:next( Kingdom, Key ),
	io:format( " next ~w ~n ", [ Next ] ),
	show_next_key( Kingdom, Next ).

populate( _Kingdom, [] ) -> {done,start};
populate( Kingdom, [H | T] ) ->
		ets:insert( Kingdom, H ),
		populate( Kingdom, T ).
	
find_animal( Kingdom, Animal ) ->
	ets:match( Kingdom, { '$1', Animal } ).
% ==============
% sample output
% ==============
% 53> test_ets:start().
% member true 
%  next mini 
%  next goofy 
%  next '$end_of_table' 
%  [[mini],[micky]]

Advanced options

There are a number of options for ets data.

Data structure options include: set, ordered set, bag, and duplicate bag. A set has only unique keys and values. A sorted version of set exists. A bag can have duplicate keys. A duplicate bag can have duplicate keys and values.

set           [{zod, alice},{smith, joe}]
ordered set   [{smith, joe},{zod, alice}]
bag           [{smith, joe},{smith, ann}]
duplicate bag [{smith, joe},{smith, joe]]

Access options include: private, protected, and public. Private ets data is limited to the owning process. Protected ets tables are readable by other processes. Public ets tables have read and write access by other processes.

The options are passed in the List_of_options.

ets:new( 'magic', List_of_options)

An empty List_of_options leaves the default values active:

[set, protected, {keypos,1}]