This section describes the basic steps when designing a
The configuration of a
Unlike data tables, information in schema tables can only be accessed and modified by using the schema-related functions described in this section.
An important aspect of these functions is that the system can access a table while it is being reconfigured. For example, it is possible to move a table and simultaneously perform write operations to the same table. This feature is essential for applications that require continuous service.
This section describes the functions available for schema management, all which return either of the following tuples:
The schema functions are as follows:
Example:
-record(old, {key, val}).
-record(new, {key, val, extra}).
Transformer =
fun(X) when record(X, old) ->
#new{key = X#old.key,
val = X#old.val,
extra = 42}
end,
{atomic, ok} = mnesia:transform_table(foo, Transformer,
record_info(fields, new),
new),
Argument
The data model employed by
Each Object Identifier (OID) is made up of a table name and a key.
For example, if an employee record is represented by the tuple
Thus, each table is made up of records, where the first element
is a record name and the second element of the table is a key,
which identifies the particular record in that table. The
combination of the table name and a key is an arity two tuple
What makes the
Before starting
When running a distributed system with two or more
participating nodes, the function
Let us use the example database
Specify the
%erl -mnesia dir '"/ldisc/scratch/Mnesia.Company"'
To start the
On the node
gin %erl -sname a -mnesia dir '"/ldisc/scratch/Mnesia.company"'
On the node
skeppet %erl -sname b -mnesia dir '"/ldisc/scratch/Mnesia.company"'
On one of the two nodes:
(a@gin)1>mnesia:create_schema([a@gin, b@skeppet]).
To initialize the database, execute the following code on one of the two nodes:
As illustrated, the two directories reside on different nodes,
because
By executing these commands, two Erlang nodes are configured to
run the
In a system of
The function
Start
mnesia:start().
This function initiates the DBMS locally.
The choice of configuration alters the location and load order of the tables. The alternatives are as follows:
Table initialization is asynchronous. The function
call
A problem can arise if a replicated table on one node is
initiated, but
However, this procedure can be time-consuming, the shortcut function
Thus, it can be assumed that if an application wants to use
tables
case mnesia:wait_for_tables([a, b], 20000) of {timeout, RemainingTabs} -> panic(RemainingTabs); ok -> synced end.
When tables are forcefully loaded from the local disc, all operations that were performed on the replicated table while the local node was down, and the remote replica was alive, are lost. This can cause the database to become inconsistent.
If the startup procedure fails, the function
The function
The function arguments are as follows:
Notice that currently
A table of type
The following example illustrates the difference between
type
f() -> F = fun() -> mnesia:write({foo, 1, 2}), mnesia:write({foo, 1, 3}), mnesia:read({foo, 1}) end, mnesia:transaction(F).
This transaction returns the list
Write operations to a table replica of type
It is possible to have a
replicated table of type
A write operation on a
Table replicas of type
The first field of a record is the second element of the tuple, which is the representation of the record.
It is easy to design applications that use SNMP to
manipulate and control the system.
The expression
It is recommended to use the
As an example, consider the following record definition:
-record(funky, {x, y}).
The following call would create a table that is replicated on two
nodes, has an extra index on attribute
mnesia:create_table(funky, [{disc_copies, [N1, N2]}, {index, [y]}, {type, bag}, {attributes, record_info(fields, funky)}]).
Whereas a call to the following default code values would return
a table with a RAM copy on the local node, no extra indexes, and the
attributes defaulted to the list
mnesia:create_table(stuff, [])