DETS
DETS is the Erlang table that lives in a file. It has the same calls as ETS for the common jobs, it survives a restart of the node, and it is part of the standard library, so a program that must remember a few thousand rows needs no database and no dependency. The ETS lesson is the memory table; this one is the disk table and the choices that come with it.
A table on disk
dets:open_file/2 takes a name and a file, and answers {ok, Table}; every later call uses that name. dets:insert/2 writes into a buffer, and dets:sync/1 pushes the buffer to the disk, which is the call that turns a write into a durable one. The program writes two rows, closes the table, opens it again and reads them back:
store.erl
-module(store). -export([main/0]). main() -> {ok, Table} = dets:open_file(store, [{file, "store.dets"}, {type, set}]), ok = dets:insert(Table, [{ada, 36}, {grace, 45}]), ok = dets:sync(Table), io:format("lookup: ~p~n", [dets:lookup(Table, ada)]), ok = dets:close(Table), {ok, Table} = dets:open_file(store, [{file, "store.dets"}, {type, set}]), io:format("after reopen: ~p~n", [dets:lookup(Table, grace)]), io:format("size: ~p~n", [dets:info(Table, size)]), io:format("rows: ~p~n", [lists:sort(dets:foldl(fun(Row, Acc) -> [Row | Acc] end, [], Table))]), ok = dets:delete(Table, ada), io:format("after delete: ~p~n", [dets:lookup(Table, ada)]), ok = dets:close(Table), ok = file:delete("store.dets").
erlc store.erl
erl -noshell -eval 'store:main(), halt().'
lookup: [{ada,36}] after reopen: [{grace,45}] size: 2 rows: [{ada,36},{grace,45}] after delete: []
dets:lookup/2 answers a list, like its ETS twin, and the table answered the same rows after a close and an open, which is the whole difference from ETS. A close flushes and writes the header that marks the file clean; a program that ends without dets:close/1 leaves the file to be repaired on the next open, which works but takes time proportional to the table.
Closed and read only
A DETS table is open or closed, and a call on a table that is not open raises badarg instead of answering an error tuple. A table can also be opened with {access, read}, which lets a second process read the same file while one process writes it; a write through a read only handle fails. The program shows a lookup on a closed name, a bag and a read only handle:
modes.erl
-module(modes). -export([main/0]). main() -> io:format("lookup on a closed table: ~p~n", [caught(fun() -> dets:lookup(store, ada) end)]), {ok, Table} = dets:open_file(modes, [{file, "modes.dets"}, {type, bag}]), ok = dets:insert(Table, [{tag, a}, {tag, b}, {tag, a}]), io:format("a bag drops an exact duplicate: ~p~n", [lists:sort(dets:lookup(Table, tag))]), ok = dets:close(Table), {ok, ReadOnly} = dets:open_file(modes, [{file, "modes.dets"}, {type, bag}, {access, read}]), io:format("read only: ~p~n", [caught(fun() -> dets:insert(ReadOnly, {tag, c}) end)]), io:format("rows: ~p~n", [length(dets:lookup(ReadOnly, tag))]), ok = dets:close(ReadOnly), ok = file:delete("modes.dets"). caught(Fun) -> try Fun() catch Class:Reason -> {Class, Reason} end.
erlc modes.erl
erl -noshell -eval 'modes:main(), halt().'
lookup on a closed table: {error,badarg} a bag drops an exact duplicate: [{tag,a},{tag,b}] read only: {error,{access_mode,"modes.dets"}} rows: 2
The bag kept one copy of {tag, a} and both values, exactly as an ETS bag would. The read only handle refused the insert with an error tuple and still answered its two rows, so a report process can read a table that another process owns. Only one process at a time may open a table for writing: the second writer gets {error, {already_open, Name}}.
A crash and the data
The point of a disk table is the data that outlives the process. The program opens a table in a spawned process, inserts a row, syncs it, and kills the process with exit(Pid, kill), which no try can catch. The main process then opens the same file:
unclean.erl
-module(unclean). -export([main/0]). main() -> Self = self(), Pid = spawn(fun() -> {ok, Table} = dets:open_file(unclean, [{file, "unclean.dets"}, {type, set}]), ok = dets:insert(Table, {k, v}), ok = dets:sync(Table), Self ! ready, receive stop -> ok end end), receive ready -> ok end, exit(Pid, kill), wait_down(Pid), {ok, Table} = dets:open_file(unclean, [{file, "unclean.dets"}, {type, set}]), io:format("after the crash: ~p~n", [dets:lookup(Table, k)]), ok = dets:close(Table), ok = file:delete("unclean.dets"). wait_down(Pid) -> Ref = monitor(process, Pid), receive {'DOWN', Ref, process, Pid, _Reason} -> ok end.
erlc unclean.erl
erl -noshell -eval 'unclean:main(), halt().'
after the crash: [{k,v}]
The row is there because dets:sync/1 ran before the kill; without it, the insert could still be in the buffer and the crash would lose it. That is the trade a DETS writer makes: a sync costs a disk write, and a program that syncs after every row is as slow as the disk. Sync at the end of a batch, or after the rows a crash must not lose.
DETS against ETS
The two tables share a shape and differ in what they cost. ETS lives in memory, allows many concurrent readers and writers, and dies with the node. DETS lives in a file, allows one writer and any number of read only handles, and survives the node; it also has a limit of about 2 GB per table and a repair pass when it was not closed. A table of counters that a hundred processes update belongs in ETS. A table of settings, of accounts or of finished jobs that must be there after a restart belongs in DETS, and a program that needs both speed and durability keeps an ETS cache in front of a DETS store and writes through on a change.
When the data outgrows one file or several writers need it, the next step is Mnesia, which is an OTP application on top of the same idea: tables with a schema, transactions and copies on more than one node. For a few thousand rows that one process owns, DETS is the smaller answer, and it needs no application to start.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
dets:open_file/2answer?Answer
{ok, Table}The call answers the table name to use in every later call.
-
What does
dets:sync/1do?Answer
Pushes the buffer to the disk
The write is durable only after the buffer reaches the disk.
-
What does a call on a name that is not open raise?
Answer
An
error:badargexceptionDETS raises for a table it does not know, unlike a lookup on a missing key.
-
How many processes may open one DETS table for writing?
Answer
One
A second writer gets
{error, {already_open, Name}}; readers use the read access mode. -
What is the size limit of one DETS table?
Answer
About 2 GB
A DETS file is limited to about 2 GB, which is why a large store moves to Mnesia or a database.
-
Which table dies with the node?
Answer
ETS
ETS is memory owned by a process, and DETS is a file that outlives the node.
-
What does this module print?
-module(q1). -export([main/0]). main() -> {ok, T} = dets:open_file(q1, [{file, "q1.dets"}, {type, set}]), ok = dets:insert(T, {a, 1}), ok = dets:close(T), {ok, T} = dets:open_file(q1, [{file, "q1.dets"}, {type, set}]), io:format("~p~n", [dets:lookup(T, a)]), ok = dets:close(T), ok = file:delete("q1.dets").Answer
[{a,1}]A close flushes the row, and the second open reads the same file.
-
What does this module print?
-module(q2). -export([main/0]). main() -> {ok, T} = dets:open_file(q2, [{file, "q2.dets"}, {type, bag}]), ok = dets:insert(T, [{k, 1}, {k, 2}, {k, 1}]), io:format("~p~n", [lists:sort(dets:lookup(T, k))]), ok = dets:close(T), ok = file:delete("q2.dets").Answer
[{k,1},{k,2}]A bag keeps one row per key and value, so the repeated pair is dropped.
-
What does this module print?
-module(q3). -export([main/0]). main() -> {ok, T} = dets:open_file(q3, [{file, "q3.dets"}, {type, set}]), ok = dets:insert(T, {a, 1}), ok = dets:close(T), {ok, R} = dets:open_file(q3, [{file, "q3.dets"}, {type, set}, {access, read}]), io:format("~p~n", [dets:info(R, size)]), ok = dets:close(R), ok = file:delete("q3.dets").Answer
1The row was written and closed before the read only handle opened the file.
-
What does this module print?
-module(q4). -export([main/0]). main() -> {ok, T} = dets:open_file(q4, [{file, "q4.dets"}, {type, set}]), io:format("~p~n", [dets:insert(T, {a, 1})]), ok = dets:close(T), ok = file:delete("q4.dets").Answer
okdets:insert/2answersok, unlike thetrueof its ETS twin.
The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Автор статьи: Arthur Isaev