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 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
|
.TH dets 3 "stdlib 1.15.3" "Ericsson AB" "ERLANG MODULE DEFINITION"
.SH MODULE
dets \- A Disk Based Term Storage
.SH DESCRIPTION
.LP
The module \fIdets\fR provides a term storage on file\&. The stored terms, in this module called \fIobjects\fR, are tuples such that one element is defined to be the key\&. A Dets \fItable\fR is a collection of objects with the key at the same position stored on a file\&.
.LP
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\&. The size of Dets files cannot exceed 2 GB\&. If larger tables are needed, Mnesia\&'s table fragmentation can be used\&.
.LP
There are three types of Dets tables: set, bag and duplicate_bag\&. A table of type \fIset\fR 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 \fIbag\fR has zero or more different objects with a given key\&. A table of type \fIduplicate_bag\fR has zero or more possibly matching objects with a given key\&.
.LP
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\&.
.SS Note:
.LP
A ^C command abnormally terminates an Erlang runtime system in a Unix environment with a break-handler\&.
.LP
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\&.
.LP
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 \fIrepair\fR option set to \fIforce\fR\&.
.LP
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 \fIfirst\fR and \fInext\fR 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\&.
.LP
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\&.
.LP
All Dets functions return \fI{error, Reason}\fR if an error occurs (\fIfirst/1\fR and \fInext/2\fR are exceptions, they exit the process with the error tuple)\&. If given badly formed arguments, all functions exit the process with a \fIbadarg\fR message\&.
.LP
\fITypes\fR
.nf
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() >= 0 | default
object() = tuple()
object_cont() = tuple()
select_cont() = tuple()
type() = bag | duplicate_bag | set
version() = 8 | 9 | default
.fi
.SH EXPORTS
.LP
.B
all() -> [Name]
.br
.RS
.TP
Types
Name = name()
.br
.RE
.RS
.LP
Returns a list of the names of all open tables on this node\&.
.RE
.LP
.B
bchunk(Name, Continuation) -> {Continuation2, Data} | \&'$end_of_table\&' | {error, Reason}
.br
.RS
.TP
Types
Name = name()
.br
Continuation = start | cont()
.br
Continuation2 = cont()
.br
Data = binary() | tuple()
.br
.RE
.RS
.LP
Returns a list of objects stored in a table\&. The exact representation of the returned objects is not public\&. The lists of data can be used for initializing a table by giving the value \fIbchunk\fR to the \fIformat\fR option of the \fIinit_table/3\fR function\&. The Mnesia application uses this function for copying open tables\&.
.LP
Unless the table is protected using \fIsafe_fixtable/2\fR, calls to \fIbchunk/2\fR may not work as expected if concurrent updates are made to the table\&.
.LP
The first time \fIbchunk/2\fR is called, an initial continuation, the atom \fIstart\fR, must be provided\&.
.LP
The \fIbchunk/2\fR function returns a tuple \fI{Continuation2, Data}\fR, where \fIData\fR is a list of objects\&. \fIContinuation2\fR is another continuation which is to be passed on to a subsequent call to \fIbchunk/2\fR\&. With a series of calls to \fIbchunk/2\fR it is possible to extract all objects of the table\&.
.LP
\fIbchunk/2\fR returns \fI\&'$end_of_table\&'\fR when all objects have been returned, or \fI{error, Reason}\fR if an error occurs\&.
.RE
.LP
.B
close(Name) -> ok | {error, Reason}
.br
.RS
.TP
Types
Name = name()
.br
.RE
.RS
.LP
Closes a table\&. Only processes that have opened a table are allowed to close it\&.
.LP
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\&.
.RE
.LP
.B
delete(Name, Key) -> ok | {error, Reason}
.br
.RS
.TP
Types
Name = name()
.br
.RE
.RS
.LP
Deletes all objects with the key \fIKey\fR from the table \fIName\fR\&.
.RE
.LP
.B
delete_all_objects(Name) -> ok | {error, Reason}
.br
.RS
.TP
Types
Name = name()
.br
.RE
.RS
.LP
Deletes all objects from a table in almost constant time\&. However, if the table if fixed, \fIdelete_all_objects(T)\fR is equivalent to \fImatch_delete(T, \&'_\&')\fR\&.
.RE
.LP
.B
delete_object(Name, Object) -> ok | {error, Reason}
.br
.RS
.TP
Types
Name = name()
.br
Object = object()
.br
.RE
.RS
.LP
Deletes all instances of a given object from a table\&. If a table is of type \fIbag\fR or \fIduplicate_bag\fR, the \fIdelete/2\fR function cannot be used to delete only some of the objects with a given key\&. This function makes this possible\&.
.RE
.LP
.B
first(Name) -> Key | \&'$end_of_table\&'
.br
.RS
.TP
Types
Key = term()
.br
Name = name()
.br
.RE
.RS
.LP
Returns the first key stored in the table \fIName\fR according to the table\&'s internal order, or \fI\&'$end_of_table\&'\fR if the table is empty\&.
.LP
Unless the table is protected using \fIsafe_fixtable/2\fR, subsequent calls to \fInext/2\fR may not work as expected if concurrent updates are made to the table\&.
.LP
Should an error occur, the process is exited with an error tuple \fI{error, Reason}\fR\&. The reason for not returning the error tuple is that it cannot be distinguished from a key\&.
.LP
There are two reasons why \fIfirst/1\fR and \fInext/2\fR should not be used: they are not very efficient, and they prevent the use of the key \fI\&'$end_of_table\&'\fR since this atom is used to indicate the end of the table\&. If possible, the \fImatch\fR, \fImatch_object\fR, and \fIselect\fR functions should be used for traversing tables\&.
.RE
.LP
.B
foldl(Function, Acc0, Name) -> Acc1 | {error, Reason}
.br
.RS
.TP
Types
Function = fun(Object, AccIn) -> AccOut
.br
Acc0 = Acc1 = AccIn = AccOut = term()
.br
Name = name()
.br
Object = object()
.br
.RE
.RS
.LP
Calls \fIFunction\fR on successive elements of the table \fIName\fR together with an extra argument \fIAccIn\fR\&. The order in which the elements of the table are traversed is unspecified\&. \fIFunction\fR must return a new accumulator which is passed to the next call\&. \fIAcc0\fR is returned if the table is empty\&.
.RE
.LP
.B
foldr(Function, Acc0, Name) -> Acc1 | {error, Reason}
.br
.RS
.TP
Types
Function = fun(Object, AccIn) -> AccOut
.br
Acc0 = Acc1 = AccIn = AccOut = term()
.br
Name = name()
.br
Object = object()
.br
.RE
.RS
.LP
Calls \fIFunction\fR on successive elements of the table \fIName\fR together with an extra argument \fIAccIn\fR\&. The order in which the elements of the table are traversed is unspecified\&. \fIFunction\fR must return a new accumulator which is passed to the next call\&. \fIAcc0\fR is returned if the table is empty\&.
.RE
.LP
.B
from_ets(Name, EtsTab) -> ok | {error, Reason}
.br
.RS
.TP
Types
Name = name()
.br
EtsTab = -see ets(3)-
.br
.RE
.RS
.LP
Deletes all objects of the table \fIName\fR and then inserts all the objects of the Ets table \fIEtsTab\fR\&. The order in which the objects are inserted is not specified\&. Since \fIets:safe_fixtable/2\fR is called the Ets table must be public or owned by the calling process\&.
.RE
.LP
.B
info(Name) -> InfoList | undefined
.br
.RS
.TP
Types
Name = name()
.br
InfoList = [{Item, Value}]
.br
.RE
.RS
.LP
Returns information about the table \fIName\fR as a list of \fI{Item, Value}\fR tuples:
.RS 2
.TP 2
*
\fI{file_size, int()}\fR, the size of the file in bytes\&.
.TP 2
*
\fI{filename, file()}\fR, the name of the file where objects are stored\&.
.TP 2
*
\fI{keypos, keypos()}\fR, the position of the key\&.
.TP 2
*
\fI{size, int()}\fR, the number of objects stored in the table\&.
.TP 2
*
\fI{type, type()}\fR, the type of the table\&.
.RE
.RE
.LP
.B
info(Name, Item) -> Value | undefined
.br
.RS
.TP
Types
Name = name()
.br
.RE
.RS
.LP
Returns the information associated with \fIItem\fR for the table \fIName\fR\&. In addition to the \fI{Item, Value}\fR pairs defined for \fIinfo/1\fR, the following items are allowed:
.RS 2
.TP 2
*
\fI{access, access()}\fR, the access mode\&.
.TP 2
*
\fI{auto_save, auto_save()}\fR, the auto save interval\&.
.TP 2
*
\fI{bchunk_format, binary()}\fR, an opaque binary describing the format of the objects returned by \fIbchunk/2\fR\&. The binary can be used as argument to \fIis_compatible_chunk_format/2\fR\&. Only available for version 9 tables\&.
.TP 2
*
\fI{hash, \fR Hash\fI}\fR\&. Describes which BIF is used to calculate the hash values of the objects stored in the Dets table\&. Possible values of Hash are \fIhash\fR, which implies that the \fIerlang:hash/2\fR BIF is used, \fIphash\fR, which implies that the \fIerlang:phash/2\fR BIF is used, and \fIphash2\fR, which implies that the \fIerlang:phash2/1\fR BIF is used\&.
.TP 2
*
\fI{memory, int()}\fR, the size of the file in bytes\&. The same value is associated with the item \fIfile_size\fR\&.
.TP 2
*
\fI{no_keys, int()}\fR, the number of different keys stored in the table\&. Only available for version 9 tables\&.
.TP 2
*
\fI{no_objects, int()}\fR, the number of objects stored in the table\&.
.TP 2
*
\fI{no_slots, {Min, Used, Max}}\fR, the number of slots of the table\&. \fIMin\fR is the minimum number of slots, \fIUsed\fR is the number of currently used slots, and \fIMax\fR is the maximum number of slots\&. Only available for version 9 tables\&.
.TP 2
*
\fI{owner, pid()}\fR, the pid of the process that handles requests to the Dets table\&.
.TP 2
*
\fI{ram_file, bool()}\fR, whether the table is kept in RAM\&.
.TP 2
*
\fI{safe_fixed, \fR SafeFixed\fI}\fR\&. If the table is fixed, SafeFixed is a tuple \fI{FixedAtTime, [{Pid, RefCount}]}\fR\&. \fIFixedAtTime\fR is the time when the table was first fixed, and \fIPid\fR is the pid of the process that fixes the table \fIRefCount\fR times\&. There may be any number of processes in the list\&. If the table is not fixed, SafeFixed is the atom \fIfalse\fR\&.
.TP 2
*
\fI{version, int()}\fR, the version of the format of the table\&.
.RE
.RE
.LP
.B
init_table(Name, InitFun [, Options]) -> ok | {error, Reason}
.br
.RS
.TP
Types
Name = atom()
.br
InitFun = fun(Arg) -> Res
.br
Arg = read | close
.br
Res = end_of_input | {[object()], InitFun} | {Data, InitFun} | term()
.br
Data = binary() | tuple()
.br
.RE
.RS
.LP
Replaces the existing objects of the table \fIName\fR with objects created by calling the input function \fIInitFun\fR, see below\&. The reason for using this function rather than calling \fIinsert/2\fR is that of efficiency\&. It should be noted that the input functions are called by the process that handles requests to the Dets table, not by the calling process\&.
.LP
When called with the argument \fIread\fR the function \fIInitFun\fR is assumed to return \fIend_of_input\fR when there is no more input, or \fI{Objects, Fun}\fR, where \fIObjects\fR is a list of objects and \fIFun\fR is a new input function\&. Any other value Value is returned as an error \fI{error, {init_fun, Value}}\fR\&. Each input function will be called exactly once, and should an error occur, the last function is called with the argument \fIclose\fR, the reply of which is ignored\&.
.LP
If the type of the table is \fIset\fR 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 \fIduplicate_bag\fR\&.
.LP
It is important that the table has a sufficient number of slots for the objects\&. If not, the hash list will start to grow when \fIinit_table/2\fR returns which will significantly slow down access to the table for a period of time\&. The minimum number of slots is set by the \fIopen_file/2\fR option \fImin_no_slots\fR and returned by the \fIinfo/2\fR item \fIno_slots\fR\&. See also the \fImin_no_slots\fR option below\&.
.LP
The \fIOptions\fR argument is a list of \fI{Key, Val}\fR tuples where the following values are allowed:
.RS 2
.TP 2
*
\fI{min_no_slots, no_slots()}\fR\&. Specifies the estimated number of different keys that will be stored in the table\&. The \fIopen_file\fR option with the same name is ignored unless the table is created, and in that case performance can be enhanced by supplying an estimate when initializing the table\&.
.TP 2
*
\fI{format, Format}\fR\&. Specifies the format of the objects returned by the function \fIInitFun\fR\&. If \fIFormat\fR is \fIterm\fR (the default), \fIInitFun\fR is assumed to return a list of tuples\&. If \fIFormat\fR is \fIbchunk\fR, \fIInitFun\fR is assumed to return \fIData\fR as returned by \fIbchunk/2\fR\&. This option overrides the \fImin_no_slots\fR option\&.
.RE
.RE
.LP
.B
insert(Name, Objects) -> ok | {error, Reason}
.br
.RS
.TP
Types
Name = name()
.br
Objects = object() | [object()]
.br
.RE
.RS
.LP
Inserts one or more objects into the table \fIName\fR\&. If there already exists an object with a key matching the key of some of the given objects and the table type is \fIset\fR, the old object will be replaced\&.
.RE
.LP
.B
insert_new(Name, Objects) -> Bool
.br
.RS
.TP
Types
Name = name()
.br
Objects = object() | [object()]
.br
Bool = bool()
.br
.RE
.RS
.LP
Inserts one or more objects into the table \fIName\fR\&. If there already exists some object with a key matching the key of any of the given objects the table is not updated and \fIfalse\fR is returned, otherwise the objects are inserted and \fItrue\fR returned\&.
.RE
.LP
.B
is_compatible_bchunk_format(Name, BchunkFormat) -> Bool
.br
.RS
.TP
Types
Name = name()
.br
BchunkFormat = binary()
.br
Bool = bool()
.br
.RE
.RS
.LP
Returns \fItrue\fR if it would be possible to initialize the table \fIName\fR, using \fIinit_table/3\fR with the option \fI{format, bchunk}\fR, with objects read with \fIbchunk/2\fR from some table \fIT\fR such that calling \fIinfo(T, bchunk_format)\fR returns \fIBchunkFormat\fR\&.
.RE
.LP
.B
is_dets_file(FileName) -> Bool | {error, Reason}
.br
.RS
.TP
Types
FileName = file()
.br
Bool = bool()
.br
.RE
.RS
.LP
Returns \fItrue\fR if the file \fIFileName\fR is a Dets table, \fIfalse\fR otherwise\&.
.RE
.LP
.B
lookup(Name, Key) -> [Object] | {error, Reason}
.br
.RS
.TP
Types
Key = term()
.br
Name = name()
.br
Object = object()
.br
.RE
.RS
.LP
Returns a list of all objects with the key \fIKey\fR stored in the table \fIName\fR\&. For example:
.nf
2> dets:open_file(abc, [{type, bag}])\&.
{ok,abc}
3> dets:insert(abc, {1,2,3})\&.
ok
4> dets:insert(abc, {1,3,4})\&.
ok
5> dets:lookup(abc, 1)\&.
[{1,2,3},{1,3,4}]
.fi
.LP
If the table is of type \fIset\fR, 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 \fIbag\fR or \fIduplicate_bag\fR, the function returns a list of arbitrary length\&.
.LP
Note that the order of objects returned is unspecified\&. In particular, the order in which objects were inserted is not reflected\&.
.RE
.LP
.B
match(Continuation) -> {[Match], Continuation2} | \&'$end_of_table\&' | {error, Reason}
.br
.RS
.TP
Types
Continuation = Continuation2 = bindings_cont()
.br
Match = [term()]
.br
.RE
.RS
.LP
Matches some objects stored in a table and returns a non-empty 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 \fIContinuation\fR, which has been returned by a prior call to \fImatch/1\fR or \fImatch/3\fR\&.
.LP
When all objects of the table have been matched, \fI\&'$end_of_table\&'\fR is returned\&.
.RE
.LP
.B
match(Name, Pattern) -> [Match] | {error, Reason}
.br
.RS
.TP
Types
Name = name()
.br
Pattern = tuple()
.br
Match = [term()]
.br
.RE
.RS
.LP
Returns for each object of the table \fIName\fR that matches \fIPattern\fR a list of bindings in some unspecified order\&. See ets(3) for a description of patterns\&. If the keypos\&'th element of \fIPattern\fR 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\&.
.RE
.LP
.B
match(Name, Pattern, N) -> {[Match], Continuation} | \&'$end_of_table\&' | {error, Reason}
.br
.RS
.TP
Types
Name = name()
.br
Pattern = tuple()
.br
N = default | int()
.br
Match = [term()]
.br
Continuation = bindings_cont()
.br
.RE
.RS
.LP
Matches some or all objects of the table \fIName\fR and returns a non-empty list of the bindings that match \fIPattern\fR in some unspecified order\&. See ets(3) for a description of patterns\&.
.LP
A tuple of the bindings and a continuation is returned, unless the table is empty, in which case \fI\&'$end_of_table\&'\fR is returned\&. The continuation is to be used when matching further objects by calling \fImatch/1\fR\&.
.LP
If the keypos\&'th element of \fIPattern\fR is bound, all objects of the table are matched\&. If the keypos\&'th element is unbound, all objects of the table are matched, \fIN\fR objects at a time, until at least one object matches or the end of the table has been reached\&. The default, indicated by giving \fIN\fR the value \fIdefault\fR, is to let the number of objects vary depending on the sizes of the objects\&. If \fIName\fR 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\&.
.LP
The table should always be protected using \fIsafe_fixtable/2\fR before calling \fImatch/3\fR, or errors may occur when calling \fImatch/1\fR\&.
.RE
.LP
.B
match_delete(Name, Pattern) -> N | {error, Reason}
.br
.RS
.TP
Types
Name = name()
.br
N = int()
.br
Pattern = tuple()
.br
.RE
.RS
.LP
Deletes all objects that match \fIPattern\fR from the table \fIName\fR, and returns the number of deleted objects\&. See ets(3) for a description of patterns\&.
.LP
If the keypos\&'th element of \fIPattern\fR is bound, only the objects with the right key are matched\&.
.RE
.LP
.B
match_object(Continuation) -> {[Object], Continuation2} | \&'$end_of_table\&' | {error, Reason}
.br
.RS
.TP
Types
Continuation = Continuation2 = object_cont()
.br
Object = object()
.br
.RE
.RS
.LP
Returns a non-empty 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 \fIContinuation\fR, which has been returned by a prior call to \fImatch_object/1\fR or \fImatch_object/3\fR\&.
.LP
When all objects of the table have been matched, \fI\&'$end_of_table\&'\fR is returned\&.
.RE
.LP
.B
match_object(Name, Pattern) -> [Object] | {error, Reason}
.br
.RS
.TP
Types
Name = name()
.br
Pattern = tuple()
.br
Object = object()
.br
.RE
.RS
.LP
Returns a list of all objects of the table \fIName\fR that match \fIPattern\fR in some unspecified order\&. See ets(3) for a description of patterns\&.
.LP
If the keypos\&'th element of \fIPattern\fR is unbound, all objects of the table are matched\&. If the keypos\&'th element of \fIPattern\fR is bound, only the objects with the right key are matched\&.
.LP
Using the \fImatch_object\fR functions for traversing all objects of a table is more efficient than calling \fIfirst/1\fR and \fInext/2\fR or \fIslot/2\fR\&.
.RE
.LP
.B
match_object(Name, Pattern, N) -> {[Object], Continuation} | \&'$end_of_table\&' | {error, Reason}
.br
.RS
.TP
Types
Name = name()
.br
Pattern = tuple()
.br
N = default | int()
.br
Object = object()
.br
Continuation = object_cont()
.br
.RE
.RS
.LP
Matches some or all objects stored in the table \fIName\fR and returns a non-empty list of the objects that match \fIPattern\fR in some unspecified order\&. See ets(3) for a description of patterns\&.
.LP
A list of objects and a continuation is returned, unless the table is empty, in which case \fI\&'$end_of_table\&'\fR is returned\&. The continuation is to be used when matching further objects by calling \fImatch_object/1\fR\&.
.LP
If the keypos\&'th element of \fIPattern\fR is bound, all objects of the table are matched\&. If the keypos\&'th element is unbound, all objects of the table are matched, \fIN\fR objects at a time, until at least one object matches or the end of the table has been reached\&. The default, indicated by giving \fIN\fR the value \fIdefault\fR, is to let the number of objects vary depending on the sizes of the objects\&. If \fIName\fR 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\&.
.LP
The table should always be protected using \fIsafe_fixtable/2\fR before calling \fImatch_object/3\fR, or errors may occur when calling \fImatch_object/1\fR\&.
.RE
.LP
.B
member(Name, Key) -> Bool | {error, Reason}
.br
.RS
.TP
Types
Name = name()
.br
Key = term()
.br
Bool = bool()
.br
.RE
.RS
.LP
Works like \fIlookup/2\fR, but does not return the objects\&. The function returns \fItrue\fR if one or more elements of the table has the key \fIKey\fR, \fIfalse\fR otherwise\&.
.RE
.LP
.B
next(Name, Key1) -> Key2 | \&'$end_of_table\&'
.br
.RS
.TP
Types
Name = name()
.br
Key1 = Key2 = term()
.br
.RE
.RS
.LP
Returns the key following \fIKey1\fR in the table \fIName\fR according to the table\&'s internal order, or \fI\&'$end_of_table\&'\fR if there is no next key\&.
.LP
Should an error occur, the process is exited with an error tuple \fI{error, Reason}\fR\&.
.LP
Use \fIfirst/1\fR to find the first key in the table\&.
.RE
.LP
.B
open_file(Filename) -> {ok, Reference} | {error, Reason}
.br
.RS
.TP
Types
FileName = file()
.br
Reference = ref()
.br
.RE
.RS
.LP
Opens an existing table\&. If the table has not been properly closed, the error \fI{error, need_repair}\fR is returned\&. The returned reference is to be used as the name of the table\&. This function is most useful for debugging purposes\&.
.RE
.LP
.B
open_file(Name, Args) -> {ok, Name} | {error, Reason}
.br
.RS
.TP
Types
Name = atom()
.br
.RE
.RS
.LP
Opens a table\&. An empty Dets table is created if no file exists\&.
.LP
The atom \fIName\fR 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\&.
.LP
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\&.
.LP
The \fIArgs\fR argument is a list of \fI{Key, Val}\fR tuples where the following values are allowed:
.RS 2
.TP 2
*
\fI{access, access()}\fR\&. 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 \fIread_write\fR\&.
.TP 2
*
\fI{auto_save, auto_save()}\fR, the auto save interval\&. If the interval is an integer \fITime\fR, the table is flushed to disk whenever it is not accessed for \fITime\fR milliseconds\&. A table that has been flushed will require no reparation when reopened after an uncontrolled emulator halt\&. If the interval is the atom \fIinfinity\fR, auto save is disabled\&. The default value is 180000 (3 minutes)\&.
.TP 2
*
\fI{estimated_no_objects, int()}\fR\&. Equivalent to the \fImin_no_slots\fR option\&.
.TP 2
*
\fI{file, file()}\fR, the name of the file to be opened\&. The default value is the name of the table\&.
.TP 2
*
\fI{max_no_slots, no_slots()}\fR, 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\&.
.TP 2
*
\fI{min_no_slots, no_slots()}\fR\&. 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\&.
.TP 2
*
\fI{keypos, keypos()}\fR, 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\&.
.TP 2
*
\fI{ram_file, bool()}\fR, 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 \fIfalse\fR\&.
.TP 2
*
\fI{repair, Value}\fR\&. \fIValue\fR can be either a \fIbool()\fR or the atom \fIforce\fR\&. The flag specifies whether the Dets server should invoke the automatic file reparation algorithm\&. The default is \fItrue\fR\&. If \fIfalse\fR is specified, there is no attempt to repair the file and \fI{error, need_repair}\fR is returned if the table needs to be repaired\&.
.RS 2
.LP
.LP
The value \fIforce\fR 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 \fIerlang:hash/2\fR BIF\&. Tables created with Dets from a STDLIB version of 1\&.8\&.2 and later use the \fIerlang:phash/2\fR function or the \fIerlang:phash2/1\fR function, which is preferred\&.
.LP
.LP
The \fIrepair\fR option is ignored if the table is already open\&.
.RE
.TP 2
*
\fI{type, type()}\fR, the type of the table\&. The default value is \fIset\fR\&.
.TP 2
*
\fI{version, version()}\fR, the version of the format used for the table\&. The default value is \fI9\fR\&. Tables on the format used before OTP R8 can be created by giving the value \fI8\fR\&. A version 8 table can be converted to a version 9 table by giving the options \fI{version, 9}\fR and \fI{repair, force}\fR\&.
.RE
.RE
.LP
.B
pid2name(Pid) -> {ok, Name} | undefined
.br
.RS
.TP
Types
Name = name()
.br
Pid = pid()
.br
.RE
.RS
.LP
Returns the name of the table given the pid of a process that handles requests to a table, or \fIundefined\fR if there is no such table\&.
.LP
This function is meant to be used for debugging only\&.
.RE
.LP
.B
repair_continuation(Continuation, MatchSpec) -> Continuation2
.br
.RS
.TP
Types
Continuation = Continuation2 = select_cont()
.br
MatchSpec = match_spec()
.br
.RE
.RS
.LP
This function can be used to restore an opaque continuation returned by \fIselect/3\fR or \fIselect/1\fR if the continuation has passed through external term format (been sent between nodes or stored on disk)\&.
.LP
The reason for this function is that continuation terms contain compiled match specifications and therefore will be invalidated if converted to external term format\&. Given that the original match specification is kept intact, the continuation can be restored, meaning it can once again be used in subsequent \fIselect/1\fR calls even though it has been stored on disk or on another node\&.
.LP
See also \fIets(3)\fR for further explanations and examples\&.
.SS Note:
.LP
This function is very rarely needed in application code\&. It is used by Mnesia to implement distributed \fIselect/3\fR and \fIselect/1\fR sequences\&. A normal application would either use Mnesia or keep the continuation from being converted to external format\&.
.LP
The reason for not having an external representation of compiled match specifications is performance\&. It may be subject to change in future releases, while this interface will remain for backward compatibility\&.
.RE
.LP
.B
safe_fixtable(Name, Fix)
.br
.RS
.TP
Types
Name = name()
.br
Fix = bool()
.br
.RE
.RS
.LP
If \fIFix\fR is \fItrue\fR, the table \fIName\fR is fixed (once more) by the calling process, otherwise the table is released\&. The table is also released when a fixing process terminates\&.
.LP
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\&.
.LP
It is not guaranteed that calls to \fIfirst/1\fR, \fInext/2\fR, 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\&.
.LP
If objects have been added while the table was fixed, the hash list will start to grow when the table is released which will significantly slow down access to the table for a period of time\&.
.RE
.LP
.B
select(Continuation) -> {Selection, Continuation2} | \&'$end_of_table\&' | {error, Reason}
.br
.RS
.TP
Types
Continuation = Continuation2 = select_cont()
.br
Selection = [term()]
.br
.RE
.RS
.LP
Applies a match specification to some objects stored in a table and returns a non-empty list of the results\&. The table, the match specification, and the number of objects that are matched are all defined by \fIContinuation\fR, which has been returned by a prior call to \fIselect/1\fR or \fIselect/3\fR\&.
.LP
When all objects of the table have been matched, \fI\&'$end_of_table\&'\fR is returned\&.
.RE
.LP
.B
select(Name, MatchSpec) -> Selection | {error, Reason}
.br
.RS
.TP
Types
Name = name()
.br
MatchSpec = match_spec()
.br
Selection = [term()]
.br
.RE
.RS
.LP
Returns the results of applying the match specification \fIMatchSpec\fR to all or some objects stored in the table \fIName\fR\&. The order of the objects is not specified\&. See the ERTS User\&'s Guide for a description of match specifications\&.
.LP
If the keypos\&'th element of \fIMatchSpec\fR 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\&.
.LP
Using the \fIselect\fR functions for traversing all objects of a table is more efficient than calling \fIfirst/1\fR and \fInext/2\fR or \fIslot/2\fR\&.
.RE
.LP
.B
select(Name, MatchSpec, N) -> {Selection, Continuation} | \&'$end_of_table\&' | {error, Reason}
.br
.RS
.TP
Types
Name = name()
.br
MatchSpec = match_spec()
.br
N = default | int()
.br
Selection = [term()]
.br
Continuation = select_cont()
.br
.RE
.RS
.LP
Returns the results of applying the match specification \fIMatchSpec\fR to some or all objects stored in the table \fIName\fR\&. The order of the objects is not specified\&. See the ERTS User\&'s Guide for a description of match specifications\&.
.LP
A tuple of the results of applying the match specification and a continuation is returned, unless the table is empty, in which case \fI\&'$end_of_table\&'\fR is returned\&. The continuation is to be used when matching further objects by calling \fIselect/1\fR\&.
.LP
If the keypos\&'th element of \fIMatchSpec\fR is bound, the match specification is applied to all objects of the table with the right key(s)\&. If the keypos\&'th element of \fIMatchSpec\fR is unbound, the match specification is applied to all objects of the table, \fIN\fR objects at a time, until at least one object matches or the end of the table has been reached\&. The default, indicated by giving \fIN\fR the value \fIdefault\fR, is to let the number of objects vary depending on the sizes of the objects\&. If \fIName\fR 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\&.
.LP
The table should always be protected using \fIsafe_fixtable/2\fR before calling \fIselect/3\fR, or errors may occur when calling \fIselect/1\fR\&.
.RE
.LP
.B
select_delete(Name, MatchSpec) -> N | {error, Reason}
.br
.RS
.TP
Types
Name = name()
.br
MatchSpec = match_spec()
.br
N = int()
.br
.RE
.RS
.LP
Deletes each object from the table \fIName\fR such that applying the match specification \fIMatchSpec\fR to the object returns the value \fItrue\fR\&. See the ERTS User\&'s Guide for a description of match specifications\&. Returns the number of deleted objects\&.
.LP
If the keypos\&'th element of \fIMatchSpec\fR is bound, the match specification is applied to the objects with the right key(s) only\&.
.RE
.LP
.B
slot(Name, I) -> \&'$end_of_table\&' | [Object] | {error, Reason}
.br
.RS
.TP
Types
Name = name()
.br
I = int()
.br
Object = object()
.br
.RE
.RS
.LP
The objects of a table are distributed among slots, starting with slot \fI0\fR and ending with slot n\&. This function returns the list of objects associated with slot \fII\fR\&. If \fII\fR is greater than n \fI\&'$end_of_table\&'\fR is returned\&.
.RE
.LP
.B
sync(Name) -> ok | {error, Reason}
.br
.RS
.TP
Types
Name = name()
.br
.RE
.RS
.LP
Ensures that all updates made to the table \fIName\fR are written to disk\&. This also applies to tables which have been opened with the \fIram_file\fR flag set to \fItrue\fR\&. In this case, the contents of the RAM file are flushed to disk\&.
.LP
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\&.
.RE
.LP
.B
table(Name [, Options]) -> QueryHandle
.br
.RS
.TP
Types
Name = name()
.br
QueryHandle = -a query handle, see qlc(3)-
.br
Options = [Option] | Option
.br
Option = {n_objects, Limit} | {traverse, TraverseMethod}
.br
Limit = default | integer() >= 1
.br
TraverseMethod = first_next | select | {select, MatchSpec}
.br
MatchSpec = match_spec()
.br
.RE
.RS
.LP
Returns a QLC (Query List Comprehension) query handle\&. The module \fIqlc\fR implements a query language aimed mainly at Mnesia but Ets tables, Dets tables, and lists are also recognized by QLC as sources of data\&. Calling \fIdets:table/1, 2\fR is the means to make the Dets table \fIName\fR usable to QLC\&.
.LP
When there are only simple restrictions on the key position QLC uses \fIdets:lookup/2\fR to look up the keys, but when that is not possible the whole table is traversed\&. The option \fItraverse\fR determines how this is done:
.RS 2
.TP 2
*
\fIfirst_next\fR\&. The table is traversed one key at a time by calling \fIdets:first/1\fR and \fIdets:next/2\fR\&.
.TP 2
*
\fIselect\fR\&. The table is traversed by calling \fIdets:select/3\fR and \fIdets:select/1\fR\&. The option \fIn_objects\fR determines the number of objects returned (the third argument of \fIselect/3\fR)\&. The match specification (the second argument of \fIselect/3\fR) is assembled by QLC: simple filters are translated into equivalent match specifications while more complicated filters have to be applied to all objects returned by \fIselect/3\fR given a match specification that matches all objects\&.
.TP 2
*
\fI{select, MatchSpec}\fR\&. As for \fIselect\fR the table is traversed by calling \fIdets:select/3\fR and \fIdets:select/1\fR\&. The difference is that the match specification is explicitly given\&. This is how to state match specifications that cannot easily be expressed within the syntax provided by QLC\&.
.RE
.LP
The following example uses an explicit match specification to traverse the table:
.nf
1> dets:open_file(t, []),
dets:insert(t, [{1,a},{2,b},{3,c},{4,d}]),
MS = ets:fun2ms(fun({X,Y}) when (X > 1) or (X < 5) -> {Y} end),
QH1 = dets:table(t, [{traverse, {select, MS}}])\&.
.fi
.LP
An example with implicit match specification:
.nf
2> QH2 = qlc:q([{Y} || {X,Y} <- dets:table(t), (X > 1) or (X < 5)])\&.
.fi
.LP
The latter example is in fact equivalent to the former which can be verified using the function \fIqlc:info/1\fR:
.nf
3> qlc:info(QH1) =:= qlc:info(QH2)\&.
true
.fi
.LP
\fIqlc:info/1\fR returns information about a query handle, and in this case identical information is returned for the two query handles\&.
.RE
.LP
.B
to_ets(Name, EtsTab) -> EtsTab | {error, Reason}
.br
.RS
.TP
Types
Name = name()
.br
EtsTab = -see ets(3)-
.br
.RE
.RS
.LP
Inserts the objects of the Dets table \fIName\fR into the Ets table \fIEtsTab\fR\&. The order in which the objects are inserted is not specified\&. The existing objects of the Ets table are kept unless overwritten\&.
.RE
.LP
.B
traverse(Name, Fun) -> Return | {error, Reason}
.br
.RS
.TP
Types
Fun = fun(Object) -> FunReturn
.br
FunReturn = continue | {continue, Val} | {done, Value}
.br
Val = Value = term()
.br
Name = name()
.br
Object = object()
.br
Return = [term()]
.br
.RE
.RS
.LP
Applies \fIFun\fR to each object stored in the table \fIName\fR in some unspecified order\&. Different actions are taken depending on the return value of \fIFun\fR\&. The following \fIFun\fR return values are allowed:
.RS 2
.TP 4
.B
\fIcontinue\fR:
Continue to perform the traversal\&. For example, the following function can be used to print out the contents of a table:
.RS 4
.LP
.nf
fun(X) -> io:format("~p~n", [X]), continue end\&.
.fi
.RE
.TP 4
.B
\fI{continue, Val}\fR:
Continue the traversal and accumulate \fIVal\fR\&. The following function is supplied in order to collect all objects of a table in a list:
.RS 4
.LP
.nf
fun(X) -> {continue, X} end\&.
.fi
.RE
.TP 4
.B
\fI{done, Value}\fR:
Terminate the traversal and return \fI[Value | Acc]\fR\&.
.RE
.LP
Any other value returned by \fIFun\fR terminates the traversal and is immediately returned\&.
.RE
.LP
.B
update_counter(Name, Key, Increment) -> Result
.br
.RS
.TP
Types
Name = name()
.br
Key = term()
.br
Increment = {Pos, Incr} | Incr
.br
Pos = Incr = Result = integer()
.br
.RE
.RS
.LP
Updates the object with key \fIKey\fR stored in the table \fIName\fR of type \fIset\fR by adding \fIIncr\fR to the element at the \fIPos\fR:th position\&. The new counter value is returned\&. If no position is specified, the element directly following the key is updated\&.
.LP
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\&.
.RE
.SH SEE ALSO
.LP
ets(3), mnesia(3), qlc(3)
|