1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
|
<HTML>
<HEAD>
<!-- refpage -->
<TITLE>dets</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<CENTER>
<A HREF="http://www.erlang.se"><IMG BORDER=0 ALT="[Erlang Systems]" SRC="min_head.gif"></A>
<H1>dets</H1>
</CENTER>
<H3>MODULE</H3>
<UL>
dets</UL>
<H3>MODULE SUMMARY</H3>
<UL>
A Disk Based Term Storage</UL>
<H3>DESCRIPTION</H3>
<UL>
<P>The module <CODE>dets</CODE> provides a term storage on file. The
stored terms, in this module called <STRONG>objects</STRONG>, are tuples
such that one element is defined to be the key. A Dets
<STRONG>table</STRONG> is a collection of objects with the key at the same
position stored on a file.<P>Dets is used by the Mnesia application, and is provided as is
for users who are interested in an efficient storage of Erlang
terms on disk only. Many applications just need to store some
terms in a file. Mnesia adds transactions, queries, and
distribution.<P>There are three types of Dets tables: set, bag and
duplicate_bag. A table of type <STRONG>set</STRONG> has at most one object
with a given key. If an object with a key already present in the
table is inserted, the existing object is overwritten by the new
object. A table of type <STRONG>bag</STRONG> has zero or more different
objects with a given key. A table of type <STRONG>duplicate_bag</STRONG>
has zero or more possibly equal objects with a given key.<P>Dets tables must be opened before they can be updated or read,
and when finished they must be properly closed. If a table has not
been properly closed, Dets will automatically repair the table.
This can take a substantial time if the table is large. A Dets
table is closed when the process which opened the table
terminates. If several Erlang processes (users) open the same Dets
table, they will share the table. The table is properly closed
when all users have either terminated or closed the table. Dets
tables are not properly closed if the Erlang runtime system is
terminated abnormally.<P><TABLE CELLPADDING=4>
<TR>
<TD VALIGN=TOP><IMG ALT="Note!" SRC="note.gif"></TD>
<TD>
<P>A ^C command abnormally terminates an Erlang runtime
system in a Unix environment with a break-handler.</TD>
</TR>
</TABLE>
<P>Since all operations performed by Dets are disk operations, it
is important to realize that a single look-up operation involves a
series of disk seek and read operations. For this reason, the Dets
functions are much slower than the corresponding Ets functions,
although Dets exports a similar interface.<P>Dets organizes data as a linear hash list and the hash list
grows gracefully as more data is inserted into the table. Space
management on the file is performed by what is called a buddy
system. The current implementation keeps the entire buddy system
in RAM, which implies that if the table gets heavily fragmented,
quite some memory can be used up. The only way to defragment a
table is to close it and then open it again with the <CODE>repair</CODE>
option set to <CODE>force</CODE>.<P>It is worth noting that the ordered_set type present in Ets is
not yet implemented by Dets, neither is the limited support for
concurrent updates which makes a sequence of <CODE>first</CODE> and
<CODE>next</CODE> calls safe to use on fixed Ets tables. Both these
features will be implemented by Dets in a future release of
Erlang/OTP. Until then, the Mnesia application (or some user
implemented method for locking) has to be used to implement safe
concurrency. Currently, no library of Erlang/OTP has support for
ordered disk based term storage.<P>Two versions of the format used for storing objects on file are
supported by Dets. The first version, 8, is the format always used
for tables created by OTP R7 and earlier. The second version, 9,
is the default version of tables created by OTP R8 (and later OTP
releases). OTP R8 can create version 8 tables, and convert version
8 tables to version 9, and vice versa, upon request.
<P>All Dets functions return <CODE>{error, Reason}</CODE> if an error
occurs (<CODE>first/1</CODE> and <CODE>next/2</CODE> are exceptions, they exit
the process with the error tuple). If given badly formed
arguments, all functions exit the process with a <CODE>badarg</CODE>
message.<P><STRONG>Types</STRONG>
<PRE>access() = read | read_write
auto_save() = infinity | int()
bindings_cont() = tuple()
bool() = true | false
file() = string()
int() = integer() >= 0
keypos() = integer() >= 1
name() = atom() | ref()
no_slots() = integer() >= 1 | default
object() = tuple()
object_cont() = tuple()
select_cont() = tuple()
type() = bag | duplicate_bag | set
version() = 8 | 9 | default</PRE>
</UL>
<H3>EXPORTS</H3>
<P><A NAME="all%0"><STRONG><CODE>all() -> [Name]</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Returns a list of the names of all open tables on this
node.</UL>
<P><A NAME="close%1"><STRONG><CODE>close(Name) -> ok | {error, Reason} </CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Closes a table. Only processes that have opened a table are
allowed to close it.
<P>All open tables must be closed before the system is
stopped. If an attempt is made to open a table which has not
been properly closed, Dets automatically tries to repair the
table.</UL>
<P><A NAME="delete%2"><STRONG><CODE>delete(Name, Key) -> ok | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Deletes all objects with the key <CODE>Key</CODE> from the table
<CODE>Name</CODE>.</UL>
<P><A NAME="delete_all_objects%1"><STRONG><CODE>delete_all_objects(Name) -> ok | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Deletes all objects from a table in almost constant
time.</UL>
<P><A NAME="delete_object%2"><STRONG><CODE>delete_object(Name, Object) -> ok | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
<STRONG><CODE>Object = object()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Deletes all instances of a given object from a table. If a
table is of type <CODE>bag</CODE> or <CODE>duplicate_bag</CODE>, the
<CODE>delete/2</CODE> function cannot be used to delete only some of
the objects with a given key. This function makes this
possible.</UL>
<P><A NAME="first%1"><STRONG><CODE>first(Name) -> Key | '$end_of_table'</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Key = term()</CODE></STRONG><BR>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Returns the first key stored in the table <CODE>Name</CODE>
according to the table's internal order, or
<CODE>'$end_of_table'</CODE> if the table is empty.<P>Unless the table is protected using <CODE>safe_fixtable/2</CODE>,
subsequent calls to <CODE>next/2</CODE> may not work as expected if
concurrent updates are made to the table.<P>Should an error occur, the process is exited with an error
tuple <CODE>{error, Reason}</CODE>. The reason for not returning the
error tuple is that it cannot be distinguished from a key.<P>There are two reasons why <CODE>first/1</CODE> and <CODE>next/2</CODE>
should not be used: they are not very efficient, and they
prevent the use of the key <CODE>'$end_of_table'</CODE> since this
atom is used to indicate the end of the table. If possible,
the <CODE>match</CODE>, <CODE>match_object</CODE>, and <CODE>select</CODE>
functions should be used for traversing tables.</UL>
<P><A NAME="foldl%3"><STRONG><CODE>foldl(Function, Acc0, Name) -> Acc1 | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Function = fun(Object, AccIn) -> AccOut</CODE></STRONG><BR>
<STRONG><CODE>Acc0 = Acc1 = AccIn = AccOut = term()</CODE></STRONG><BR>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
<STRONG><CODE>Object = object()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Calls <CODE>Function</CODE> on successive elements of the table
<CODE>Name</CODE> together with an extra argument <CODE>AccIn</CODE>. The
order in which the elements of the table are traversed is
unspecified. <CODE>Function</CODE> must return a new accumulator
which is passed to the next call. <CODE>Acc0</CODE> is returned if
the table is empty.
</UL>
<P><A NAME="foldr%3"><STRONG><CODE>foldr(Function, Acc0, Name) -> Acc1 | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Function = fun(Object, AccIn) -> AccOut</CODE></STRONG><BR>
<STRONG><CODE>Acc0 = Acc1 = AccIn = AccOut = term()</CODE></STRONG><BR>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
<STRONG><CODE>Object = object()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Calls <CODE>Function</CODE> on successive elements of the table
<CODE>Name</CODE> together with an extra argument <CODE>AccIn</CODE>. The
order in which the elements of the table are traversed is
unspecified. <CODE>Function</CODE> must return a new accumulator
which is passed to the next call. <CODE>Acc0</CODE> is returned if
the table is empty.
</UL>
<P><A NAME="from_ets%2"><STRONG><CODE>from_ets(Name, EtsTab) -> ok | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
<STRONG><CODE>EtsTab = - see ets(3) -
</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Replaces the objects of the table <CODE>Name</CODE> with the
objects of the Ets table <CODE>EtsTab</CODE>. The order in which the
objects are inserted is not specified. Since
<CODE>ets:safe_fixtable/2</CODE> is called, the Ets table must be
public or owned by the calling process.
</UL>
<P><A NAME="info%1"><STRONG><CODE>info(Name) -> InfoList | undefined</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
<STRONG><CODE>InfoList = [{Item, Value}]</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Returns information about the table <CODE>Name</CODE> as a list of
<CODE>{Item, Value}</CODE> tuples:<P><UL>
<LI><CODE>{file_size, int()}</CODE>, the size of the file in
bytes.<BR>
</LI><BR>
<LI><CODE>{filename, file()}</CODE>, the name of the file
where objects are stored.<BR>
</LI><BR>
<LI><CODE>{keypos, keypos()}</CODE>, the position of the
key.<BR>
</LI><BR>
<LI><CODE>{size, int()}</CODE>, the number of objects stored
in the table.<BR>
</LI><BR>
<LI><CODE>{type, type()}</CODE>, the type of the table.<BR>
</LI><BR>
</UL>
</UL>
<P><A NAME="info%2"><STRONG><CODE>info(Name, Item) -> Value | undefined</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Returns the information associated with <CODE>Item</CODE> for the
table <CODE>Name</CODE>. In addition to the <CODE>{Item, Value}</CODE>
pairs defined for <CODE>info/1</CODE>, the following items are
allowed:<P><UL>
<LI>{<CODE>access, access()}</CODE>, the access mode.<BR>
</LI><BR>
<LI>{<CODE>auto_save, auto_save()}</CODE>, the auto save
interval.<BR>
</LI><BR>
<LI>{<CODE>hash,</CODE> Hash<CODE>}</CODE>. Describes which BIF is
used to calculate the hash values of the objects stored in
the Dets table. Possible values of Hash are <CODE>hash</CODE>,
which implies that the <CODE>erlang:hash/2</CODE> BIF is used,
and <CODE>phash</CODE>, which implies that the
<CODE>erlang:phash/2</CODE> BIF is used.<BR>
</LI><BR>
<LI>{<CODE>memory, int()}</CODE>, the size of the file in
bytes. The same value is associated with the item
<CODE>file_size</CODE>.
<BR>
</LI><BR>
<LI><CODE>{no_keys, int()}</CODE>, the number of different
keys stored in the table. Only available for version 9
tables.<BR>
</LI><BR>
<LI><CODE>{no_objects, int()}</CODE>, the number of objects
stored in the table.<BR>
</LI><BR>
<LI><CODE>{no_slots, {Min, Used, Max}}</CODE>, the number of
slots of the table. <CODE>Min</CODE> is the minimum number of
slots, <CODE>Used</CODE> is the number of currently used slots,
and <CODE>Max</CODE> is the maximum number of slots. Only
available for version 9 tables.<BR>
</LI><BR>
<LI>{<CODE>owner, pid()}</CODE>, the pid of the process that
handles requests to the Dets table.
<BR>
</LI><BR>
<LI>{<CODE>ram_file, bool()}</CODE>, whether the table is
kept in RAM.
<BR>
</LI><BR>
<LI>{<CODE>safe_fixed,</CODE> SafeFixed<CODE>}</CODE>. If the table
is fixed, SafeFixed is a tuple <CODE>{FixedAtTime,
[{Pid,RefCount}]}</CODE>. <CODE>FixedAtTime</CODE> is the time when
the table was first fixed, and <CODE>Pid</CODE> is the pid of
the process that fixes the table <CODE>RefCount</CODE> times.
There may be any number of processes in the list. If the
table is not fixed, SafeFixed is the atom <CODE>false</CODE>.
<BR>
</LI><BR>
<LI>{<CODE>version, int()}</CODE>, the version of the format
of the table.
<BR>
</LI><BR>
</UL>
</UL>
<P><A NAME="init_table%2"><STRONG><CODE>init_table(Name, InitFun) -> ok | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = atom()</CODE></STRONG><BR>
<STRONG><CODE>InitFun = fun(Arg) -> Res</CODE></STRONG><BR>
<STRONG><CODE>Arg = read | close</CODE></STRONG><BR>
<STRONG><CODE>Res = end_of_input | {[object()], InitFun} | term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Replaces the existing objects of the table <CODE>Name</CODE> with
objects created by calling the input function <CODE>InitFun</CODE>,
see below. The reason for using this function rather than
calling <CODE>insert/2</CODE> is that of efficiency.<P>When called with the argument <CODE>read</CODE> the function
<CODE>InitFun</CODE> is assumed to return <CODE>end_of_input</CODE> when
there is no more input, or <CODE>{Objects, Fun}</CODE>, where
<CODE>Objects</CODE> is a list of objects and <CODE>Fun</CODE> is a new
input function. Any other value Value is returned as an error
<CODE>{error, {init_fun, Value}}</CODE>. Each input function will be
called exactly once, and should an error occur, the last
function is called with the argument <CODE>close</CODE>, the reply
of which is ignored.<P>If the type of the table is <CODE>set</CODE> and there is more
than one object with a given key, one of the objects is
chosen. This is not necessarily the last object with the given
key in the sequence of objects returned by the input
functions. Extra objects should be avoided, or the file will
be unnecessarily fragmented. This holds also for duplicated
objects stored in tables of type <CODE>duplicate_bag</CODE>.</UL>
<P><A NAME="insert%2"><STRONG><CODE>insert(Name, Objects) -> ok | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
<STRONG><CODE>Objects = object() | [object()]</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Inserts one or more objects into the table <CODE>Name</CODE>. If
there already exists an object with the same key as some of
the given objects and the table type is <CODE>set</CODE>, the old
object will be replaced.</UL>
<P><A NAME="is_dets_file%1"><STRONG><CODE>is_dets_file(FileName) -> Bool | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>FileName = file()</CODE></STRONG><BR>
<STRONG><CODE>Bool = bool()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Returns <CODE>true</CODE> if the file <CODE>FileName</CODE> is a Dets
table, <CODE>false</CODE> otherwise.
</UL>
<P><A NAME="lookup%2"><STRONG><CODE>lookup(Name, Key) -> [Object] | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Key = term()</CODE></STRONG><BR>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
<STRONG><CODE>Object = object()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Returns a list of all objects with the key <CODE>Key</CODE>
stored in the table <CODE>Name</CODE>. For example:<PRE>2> <STRONG>dets:open_file(abc, [{type, bag}]).</STRONG>
{ok,abc}
3> <STRONG>dets:insert(abc, {1,2,3}).</STRONG>
ok
4> <STRONG>dets:insert(abc, {1,3,4}).</STRONG>
ok
5> <STRONG>dets:lookup(abc, 1).</STRONG>
[{1,2,3},{1,3,4}]</PRE>
<P>If the table is of type <CODE>set</CODE>, the function returns
either the empty list or a list with one object, as there
cannot be more than one object with a given key. If the table
is of type <CODE>bag</CODE> or <CODE>duplicate_bag</CODE>, the function
returns a list of arbitrary length.<P>Note that the order of objects returned is unspecified. In
particular, the order in which objects were inserted is not
reflected.</UL>
<P><A NAME="match%1"><STRONG><CODE>match(Continuation) -> {[Match], Continuation2} | '$end_of_table'
| {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Continuation = Continuation2 = bindings_cont()</CODE></STRONG><BR>
<STRONG><CODE>Match = [term()]</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Matches some objects stored in a table and returns a list
of the bindings that match a given pattern in some unspecified
order. The table, the pattern, and the number of objects that
are matched are all defined by <CODE>Continuation</CODE>, which has
been returned by a prior call to <CODE>match/1</CODE> or
<CODE>match/3</CODE>.<P>When all objects of the table have been matched,
<CODE>'$end_of_table'</CODE> is returned.</UL>
<P><A NAME="match%2"><STRONG><CODE>match(Name, Pattern) -> [Match] | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
<STRONG><CODE>Pattern = tuple()</CODE></STRONG><BR>
<STRONG><CODE>Match = [term()]</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Returns for each object of the table <CODE>Name</CODE> that
matches <CODE>Pattern</CODE> a list of bindings in some unspecified
order. See <A HREF="ets.html">ets(3)</A> for a
description of patterns. If the keypos'th element of
<CODE>Pattern</CODE> is unbound, all objects of the table are
matched. If the keypos'th element is bound, only the
objects with the right key are matched.</UL>
<P><A NAME="match%3"><STRONG><CODE>match(Name, Pattern, N) -> {[Match], Continuation}
| '$end_of_table' | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
<STRONG><CODE>Pattern = tuple()</CODE></STRONG><BR>
<STRONG><CODE>N = default | int()</CODE></STRONG><BR>
<STRONG><CODE>Match = [term()]</CODE></STRONG><BR>
<STRONG><CODE>Continuation = bindings_cont()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Matches some or all objects of the table <CODE>Name</CODE> and
returns a list of the bindings that match <CODE>Pattern</CODE> in
some unspecified order. See <A HREF="ets.html">ets(3)</A> for a description of
patterns.<P>A tuple of the bindings and a continuation is returned,
unless the table is empty, in which case
<CODE>'$end_of_table'</CODE> is returned. The continuation is to be
used when matching further objects by calling
<CODE>match/1</CODE>.<P>If the keypos'th element of <CODE>Pattern</CODE> is bound,
all objects of the table are matched. If the keypos'th
element is unbound, all objects of the table are matched,
<CODE>N</CODE> objects at a time. The default, indicated by giving
<CODE>N</CODE> the value <CODE>default</CODE>, is to let the number of
objects vary depending on the sizes of the objects. If
<CODE>Name</CODE> is a version 9 table, all objects with the same
key are always matched at the same time which implies that
more than N objects may sometimes be matched.
<P>The table should always be protected using
<CODE>safe_fixtable/2</CODE> before calling <CODE>match/3</CODE>, or
errors may occur when calling <CODE>match/1</CODE>.</UL>
<P><A NAME="match_delete%2"><STRONG><CODE>match_delete(Name, Pattern) -> N | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
<STRONG><CODE>N = int()</CODE></STRONG><BR>
<STRONG><CODE>Pattern = tuple()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Deletes all objects that match <CODE>Pattern</CODE> from the
table <CODE>Name</CODE>, and returns the number of deleted
objects. See <A HREF="ets.html">ets(3)</A> for a
description of patterns.<P>If the keypos'th element of <CODE>Pattern</CODE> is bound,
only the objects with the right key are matched.</UL>
<P><A NAME="match_object%1"><STRONG><CODE>match_object(Continuation) -> {[Object], Continuation2}
| '$end_of_table' | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Continuation = Continuation2 = object_cont()</CODE></STRONG><BR>
<STRONG><CODE>Object = object()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Returns a list of some objects stored in a table that match
a given pattern in some unspecified order. The table, the
pattern, and the number of objects that are matched are all
defined by <CODE>Continuation</CODE>, which has been returned by a
prior call to <CODE>match_object/1</CODE> or
<CODE>match_object/3</CODE>.<P>When all objects of the table have been matched,
<CODE>'$end_of_table'</CODE> is returned.</UL>
<P><A NAME="match_object%2"><STRONG><CODE>match_object(Name, Pattern) -> [Object] | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
<STRONG><CODE>Pattern = tuple()</CODE></STRONG><BR>
<STRONG><CODE>Object = object()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Returns a list of all objects of the table <CODE>Name</CODE> that
match <CODE>Pattern</CODE> in some unspecified order. See <A HREF="ets.html">ets(3)</A> for a description of patterns.
<P>If the keypos'th element of <CODE>Pattern</CODE> is
unbound, all objects of the table are matched. If the
keypos'th element of <CODE>Pattern</CODE> is bound, only the
objects with the right key are matched.<P>Using the <CODE>match_object</CODE> functions for traversing all
objects of a table is more efficient than calling
<CODE>first/1</CODE> and <CODE>next/2</CODE> or <CODE>slot/2</CODE>.
</UL>
<P><A NAME="match_object%3"><STRONG><CODE>match_object(Name, Pattern, N) -> {[Object], Continuation}
| '$end_of_table' | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
<STRONG><CODE>Pattern = tuple()</CODE></STRONG><BR>
<STRONG><CODE>N = default | int()</CODE></STRONG><BR>
<STRONG><CODE>Object = object()</CODE></STRONG><BR>
<STRONG><CODE>Continuation = object_cont()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Matches some or all objects stored in the table <CODE>Name</CODE>
and returns a list of the objects that match <CODE>Pattern</CODE> in
some unspecified order. See <A HREF="ets.html">ets(3)</A> for a description of
patterns.<P>A list of objects and a continuation is returned, unless
the table is empty, in which case <CODE>'$end_of_table'</CODE>
is returned. The continuation is to be used when matching
further objects by calling <CODE>match_object/1</CODE>.<P>If the keypos'th element of <CODE>Pattern</CODE> is bound,
all objects of the table are matched. If the keypos'th
element is unbound, all objects of the table are matched,
<CODE>N</CODE> objects at a time. The default, indicated by giving
<CODE>N</CODE> the value <CODE>default</CODE>, is to let the number of
objects vary depending on the sizes of the objects. If
<CODE>Name</CODE> is a version 9 table, all matching objects with
the same key are always returned in the same reply which
implies that more than N objects may sometimes be returned.
<P>The table should always be protected using
<CODE>safe_fixtable/2</CODE> before calling <CODE>match_object/3</CODE>,
or errors may occur when calling <CODE>match_object/1</CODE>.</UL>
<P><A NAME="member%2"><STRONG><CODE>member(Name, Key) -> Bool | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
<STRONG><CODE>Key = term()</CODE></STRONG><BR>
<STRONG><CODE>Bool = bool()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Works like <CODE>lookup/2</CODE>, but does not return the
objects. The function returns <CODE>true</CODE> if one or more
elements of the table has the key <CODE>Key</CODE>, <CODE>false</CODE>
otherwise.</UL>
<P><A NAME="next%2"><STRONG><CODE>next(Name, Key1) -> Key2 | '$end_of_table'</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
<STRONG><CODE>Key1 = Key2 = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Returns the key following <CODE>Key1</CODE> in the table
<CODE>Name</CODE> according to the table's internal order, or
<CODE>'$end_of_table'</CODE> if there is no next key.<P>Should an error occur, the process is exited with an error
tuple <CODE>{error, Reason}</CODE>.<P>Use <CODE>first/1</CODE> to find the first key in the table.</UL>
<P><A NAME="open_file%1"><STRONG><CODE>open_file(Filename) -> {ok, Reference} | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>FileName = file()</CODE></STRONG><BR>
<STRONG><CODE>Reference = ref()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Opens an existing table. If the table has not been properly
closed, the error <CODE>{error, need_repair}</CODE> is returned. The
returned reference is to be used as the name of the table.
This function is most useful for debugging purposes.</UL>
<P><A NAME="open_file%2"><STRONG><CODE>open_file(Name, Args) -> {ok, Name} | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = atom()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Opens a table. An empty Dets table is created if no file
exists.<P>The atom <CODE>Name</CODE> is the name of the table. The table
name must be provided in all subsequent operations on the
table. The name can be used by other processes as well, and
several process can share one table.
<P>If two processes open the same table by giving the same
name and arguments, then the table will have two users. If one
user closes the table, it still remains open until the second
user closes the table.<P>The <CODE>Args</CODE> argument is a list of <CODE>{Key, Val}</CODE>
tuples where the following values are allowed:<P><UL>
<LI> <CODE>{access, access()}</CODE>. It is possible to open
existing tables in read-only mode. A table which is opened
in read-only mode is not subjected to the automatic file
reparation algorithm if it is later opened after a crash.
The default value is <CODE>read_write</CODE>.<BR>
</LI><BR>
<LI><CODE>{auto_save, auto_save()}</CODE>, the auto save
interval. If the interval is an integer <CODE>Time</CODE>, the
table is flushed to disk whenever it is not accessed for
<CODE>Time</CODE> milliseconds. A table that has been flushed
will require no reparation when reopened after an
uncontrolled emulator halt. If the interval is the atom
<CODE>infinity</CODE>, auto save is disabled. The default value
is 180000 (3 minutes).<BR>
</LI><BR>
<LI> <CODE>{estimated_no_objects, int()}</CODE>. Equivalent to the
<CODE>min_no_slots</CODE> option.
<BR>
</LI><BR>
<LI> <CODE>{file, file()}</CODE>, the name of the file to be
opened. The default value is the name of the table.<BR>
</LI><BR>
<LI> <CODE>{max_no_slots, no_slots()}</CODE>, the maximum number
of slots that will be used. The default value is 2 M, and
the maximal value is 32 M. Note that a higher value may
increase the fragmentation of the table, and conversely,
that a smaller value may decrease the fragmentation, at
the expense of execution time. Only available for version
9 tables.<BR>
</LI><BR>
<LI> <CODE>{min_no_slots, no_slots()}</CODE>. Application
performance can be enhanced with this flag by specifying,
when the table is created, the estimated number of
different keys that will be stored in the table. The
default value as well as the minimum value is 256.<BR>
</LI><BR>
<LI><CODE>{keypos, keypos()}</CODE>, the position of the
element of each object to be used as key. The default
value is 1. The ability to explicitly state the key
position is most convenient when we want to store Erlang
records in which the first position of the record is the
name of the record type.<BR>
</LI><BR>
<LI><CODE>{ram_file, bool()}</CODE>, whether the table is to
be kept in RAM. Keeping the table in RAM may sound like an
anomaly, but can enhance the performance of applications
which open a table, insert a set of objects, and then
close the table. When the table is closed, its contents
are written to the disk file. The default value is
<CODE>false</CODE>.<BR>
</LI><BR>
<LI><CODE>{repair, Value}</CODE>. <CODE>Value</CODE> can be either
a <CODE>bool()</CODE> or the atom <CODE>force</CODE>. The flag
specifies whether the Dets server should invoke the
automatic file reparation algorithm. The default is
<CODE>true</CODE>. If <CODE>false</CODE> is specified, there is no
attempt to repair the file and <CODE>{error, need_repair}</CODE>
is returned if the table needs to be repaired.<BR>
The value <CODE>force</CODE> means that a reparation will
take place even if the table has been properly closed.
This is how to convert tables created by older versions of
STDLIB. An example is tables hashed with the deprecated
<CODE>erlang:hash/2</CODE> BIF. Tables created with Dets from a
STDLIB version of 1.8.2 and later use the new
<CODE>erlang:phash/2</CODE> function, which is preferred.<BR>
</LI><BR>
<LI> <CODE>{type, type()}</CODE>, the type of the table. The
default value is <CODE>set</CODE>.<BR>
</LI><BR>
<LI> <CODE>{version, version()}</CODE>, the version of the format
used for the table. The default value is <CODE>9</CODE>. Tables
on the format used before OTP R8 can be created by giving
the value <CODE>8</CODE>. A version 8 table can be converted to
a version 9 table by giving the options <CODE>{version,9}</CODE>
and <CODE>{repair,force}</CODE>.<BR>
</LI><BR>
</UL>
</UL>
<P><A NAME="pid2name%1"><STRONG><CODE>pid2name(Pid) -> {ok, Name} | undefined</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
<STRONG><CODE>Pid = pid()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Returns the name of the table given the pid of a process
that handles requests to a table, or <CODE>undefined</CODE> if
there is no such table.<P>This function is meant to be used for debugging only.</UL>
<P><A NAME="safe_fixtable%2"><STRONG><CODE>safe_fixtable(Name, Fix)</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
<STRONG><CODE>Fix = bool()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>If <CODE>Fix</CODE> is <CODE>true</CODE>, the table <CODE>Name</CODE> is
fixed (once more) by the calling process, otherwise the table
is released. The table is also released when a fixing process
terminates.
<P>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 require N releases to release the table.<P>It is not guaranteed that calls to <CODE>first/1</CODE>,
<CODE>next/2</CODE>, select and match functions work as expected
even if the table has been fixed; the limited support for
concurrency implemented in Ets has not yet been implementeded
in Dets. Fixing a table currently only disables resizing of
the hash list of the table.</UL>
<P><A NAME="select%1"><STRONG><CODE>select(Continuation) -> {Selection, Continuation2}
| '$end_of_table' | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Continuation = Continuation2 = select_cont()</CODE></STRONG><BR>
<STRONG><CODE>Selection = [term()]</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Returns the results of applying a match specification to
some objects stored in a table. The table, the match
specification, and the number of objects that are matched
are all defined by <CODE>Continuation</CODE>, which has been
returned by a prior call to <CODE>select/1</CODE> or
<CODE>select/3</CODE>.<P>When all objects of the table have been matched,
<CODE>'$end_of_table'</CODE> is returned.</UL>
<P><A NAME="select%2"><STRONG><CODE>select(Name, MatchSpec) -> Selection | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
<STRONG><CODE>MatchSpec = match_spec()</CODE></STRONG><BR>
<STRONG><CODE>Selection = [term()]</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Returns the results of applying the match specification
<CODE>MatchSpec</CODE> to all or some objects stored in the table
<CODE>Name</CODE>. The order of the objects is not specified. See
the ERTS User's Guide for a description of match
specifications.<P>If the keypos'th element of <CODE>MatchSpec</CODE> is
unbound, the match specification is applied to all objects of
the table. If the keypos'th element is bound, the match
specification is applied to the objects with the right key(s)
only.<P>Using the <CODE>select</CODE> functions for traversing all
objects of a table is more efficient than calling
<CODE>first/1</CODE> and <CODE>next/2</CODE> or <CODE>slot/2</CODE>.
</UL>
<P><A NAME="select%3"><STRONG><CODE>select(Name, MatchSpec, N) -> {Selection, Continuation}
| '$end_of_table' | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
<STRONG><CODE>MatchSpec = match_spec()</CODE></STRONG><BR>
<STRONG><CODE>N = default | int()</CODE></STRONG><BR>
<STRONG><CODE>Selection = [term()]</CODE></STRONG><BR>
<STRONG><CODE>Continuation = select_cont()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Returns the results of applying the match specification
<CODE>MatchSpec</CODE> to some or all objects stored in the table
<CODE>Name</CODE>. The order of the objects is not specified. See
the ERTS User's Guide for a description of match
specifications.<P>A tuple of the results of applying the match specification
and a continuation is returned, unless the table is empty,
in which case <CODE>'$end_of_table'</CODE> is returned. The
continuation is to be used when matching further objects by
calling <CODE>select/1</CODE>.<P>If the keypos'th element of <CODE>MatchSpec</CODE> is
bound, the match specification is applied to all objects of
the table with the right key(s). If the keypos'th
element of <CODE>MatchSpec</CODE> is unbound, the match
specification is applied to all objects of the table, <CODE>N</CODE>
objects at a time. The default, indicated by giving <CODE>N</CODE>
the value <CODE>default</CODE>, is to let the number of objects vary
depending on the sizes of the objects. If <CODE>Name</CODE> is a
version 9 table, all objects with the same key are always
handled at the same time which implies that the match
specification may be applied to more than N objects.
<P>The table should always be protected using
<CODE>safe_fixtable/2</CODE> before calling <CODE>select/3</CODE>, or
errors may occur when calling <CODE>select/1</CODE>.</UL>
<P><A NAME="select_delete%2"><STRONG><CODE>select_delete(Name, MatchSpec) -> N | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
<STRONG><CODE>MatchSpec = match_spec()</CODE></STRONG><BR>
<STRONG><CODE>N = int()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Deletes each object from the table <CODE>Name</CODE> such that
applying the match specification <CODE>MatchSpec</CODE> to the
object returns the value <CODE>true</CODE>. See the ERTS
User's Guide for a description of match
specifications. Returns the number of deleted objects.<P>If the keypos'th element of <CODE>MatchSpec</CODE> is
bound, the match specification is applied to the objects
with the right key(s) only.</UL>
<P><A NAME="slot%2"><STRONG><CODE>slot(Name, I) -> '$end_of_table' | [Object] | {error, Reason}
</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
<STRONG><CODE>I = int()</CODE></STRONG><BR>
<STRONG><CODE>Object = object()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>The objects of a table are distributed among slots,
starting with slot <CODE>0</CODE> and ending with slot n. This
function returns the list of objects associated with slot
<CODE>I</CODE>. If <CODE>I</CODE> is greater than n <CODE>'$end_of_table'</CODE>
is returned.</UL>
<P><A NAME="sync%1"><STRONG><CODE>sync(Name) -> ok | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Ensures that all updates made to the table <CODE>Name</CODE> are
written to disk. This also applies to tables which have been
opened with the <CODE>ram_file</CODE> flag set to <CODE>true</CODE>. In
this case, the contents of the RAM file are flushed to
disk.<P>Note that the space management data structures kept in RAM,
the buddy system, is also written to the disk. This may take
some time if the table is fragmented.</UL>
<P><A NAME="to_ets%2"><STRONG><CODE>to_ets(Name, EtsTab) -> EtsTab | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
<STRONG><CODE>EtsTab = - see ets(3) -
</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Inserts the objects of the Dets table <CODE>Name</CODE> into the
Ets table <CODE>EtsTab</CODE>. The order in which the objects are
inserted is not specified. The existing objects of the Ets
table are kept unless overwritten.
</UL>
<P><A NAME="traverse%2"><STRONG><CODE>traverse(Name, Fun) -> Return | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Fun = fun(Object) -> FunReturn</CODE></STRONG><BR>
<STRONG><CODE>FunReturn = continue | {continue, Val} | {done, Value}</CODE></STRONG><BR>
<STRONG><CODE>Val = Value = term()</CODE></STRONG><BR>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
<STRONG><CODE>Object = object()</CODE></STRONG><BR>
<STRONG><CODE>Return = [term()]</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Applies <CODE>Fun</CODE> to each object stored in the table
<CODE>Name</CODE> in some unspecified order. Different actions are
taken depending on the return value of <CODE>Fun</CODE>. The
following <CODE>Fun</CODE> return values are allowed:<P><DL>
<DT><CODE>continue</CODE></DT>
<DD> Continue to perform the traversal. For example, the
following function can be used to print out the contents
of a table:<BR>
<PRE>fun(X) -> io:format("~p~n", [X]), continue end.</PRE>
</DD>
<DT><CODE>{continue, Val}</CODE></DT>
<DD> Continue the traversal and accumulate <CODE>Val</CODE>. The
following function is supplied in order to collect all
objects of a table in a list: <BR>
<PRE>fun(X) -> {continue, X} end.</PRE>
</DD>
<DT><CODE>{done, Value}</CODE></DT>
<DD> Terminate the traversal and return <CODE>[Value | Acc]</CODE>.<BR>
</DD>
</DL>
</UL>
<P><A NAME="update_counter%3"><STRONG><CODE>update_counter(Name, Key, Increment) -> Result</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Name = name()</CODE></STRONG><BR>
<STRONG><CODE>Key = term()</CODE></STRONG><BR>
<STRONG><CODE>Increment = {Pos, Incr} | Incr</CODE></STRONG><BR>
<STRONG><CODE>Pos = Incr = Result = integer()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Updates the object with key <CODE>Key</CODE> stored in the table
<CODE>Name</CODE> of type <CODE>set</CODE> by adding <CODE>Incr</CODE> to the
element at the <CODE>Pos</CODE>:th position. The new counter value
is returned. If no position is specified, the element directly
following the key is updated.<P>This functions provides a way of updating a counter,
without having to look up an object, update the object by
incrementing an element and insert the resulting object into
the table again.</UL>
<H3>See Also</H3>
<UL>
<P><A HREF="ets.html">ets(3)</A>, mnesia(3)
</UL>
<H3>AUTHORS</H3>
<UL>
Claes Wikström - support@erlang.ericsson.se<BR>
Hans Bolinder - support@erlang.ericsson.se<BR>
</UL>
<CENTER>
<HR>
<FONT SIZE=-1>stdlib 1.10<BR>
Copyright © 1991-2001
<A HREF="http://www.erlang.se">Ericsson Utvecklings AB</A><BR>
<!--#include virtual="/ssi/otp_footer.html"-->
</FONT>
</CENTER>
</BODY>
</HTML>
|