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 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
|
.TH ets 3 "stdlib 1.15.3" "Ericsson AB" "ERLANG MODULE DEFINITION"
.SH MODULE
ets \- Built-In Term Storage
.SH DESCRIPTION
.LP
This module is an interface to the Erlang built-in term storage BIFs\&. These provide the ability to store very large quantities of data in an Erlang runtime system, and to have constant access time to the data\&. (In the case of \fIordered_set\fR, see below, access time is proportional to the logarithm of the number of objects stored)\&.
.LP
Data is organized as a set of dynamic tables, which can store tuples\&. Each table is created by a process\&. When the process terminates, the table is automatically destroyed\&. Every table has access rights set at creation\&.
.LP
Tables are divided into four different types, \fIset\fR, \fIordered_set\fR, \fIbag\fR and \fIduplicate_bag\fR\&. A \fIset\fR or \fIordered_set\fR table can only have one object associated with each key\&. A \fIbag\fR or \fIduplicate_bag\fR can have many objects associated with each key\&.
.LP
The number of tables stored at one Erlang node is limited\&. The current default limit is approximately 1400 tables\&. The upper limit can be increased by setting the environment variable \fIERL_MAX_ETS_TABLES\fR before starting the Erlang runtime system (i\&.e\&. with the \fI-env\fR option to \fIerl\fR/\fIwerl\fR)\&. The actual limit may be slightly higher than the one specified, but never lower\&.
.LP
Note that there is no automatic garbage collection for tables\&. Even if there are no references to a table from any process, it will not automatically be destroyed unless the owner process terminates\&. It can be destroyed explicitly by using \fIdelete/1\fR\&.
.LP
Some implementation details:
.RS 2
.TP 2
*
In the current implementation, every object insert and look-up operation results in a copy of the object\&.
.TP 2
*
This module provides very limited support for concurrent updates\&. No locking is available, but the \fIsafe_fixtable/2\fR function can be used to guarantee that a sequence of \fIfirst/1\fR and \fInext/2\fR calls will traverse the table without errors and that each object in the table is visited exactly once, even if another process (or the same process) simultaneously deletes or inserts objects into the table\&. Nothing more is guaranteed; in particular any object inserted during a traversal \fImay\fR be visited in the traversal\&.
.TP 2
*
\fI\&'$end_of_table\&'\fR should not be used as a key since this atom is used to mark the end of the table when using \fIfirst\fR/\fInext\fR\&.
.RE
.LP
Also worth noting is the subtle difference between \fImatching\fR and \fIcomparing equal\fR, which is demonstrated by the different table types \fIset\fR and \fIordered_set\fR\&. Two Erlang terms \fImatch\fR if they are of the same type and have the same value, so that \fI1\fR matches \fI1\fR, but not \fI1\&.0\fR (as \fI1\&.0\fR is a \fIfloat()\fR and not an \fIinteger()\fR)\&. Two Erlang terms \fIcompare equal\fR if they either are of the same type and value, or if both are numeric types and extend to the same value, so that \fI1\fR compares equal to both \fI1\fR and \fI1\&.0\fR\&. The \fIordered_set\fR works on the \fIErlang term order\fR and there is no defined order between an \fIinteger()\fR and a \fIfloat()\fR that extends to the same value, hence the key \fI1\fR and the key \fI1\&.0\fR are regarded as equal in an \fIordered_set\fR table\&.
.LP
In general, the functions below will exit with reason \fIbadarg\fR if any argument is of the wrong format, or if the table identifier is invalid\&.
.SH MATCH SPECIFICATIONS
.LP
Some of the functions uses a \fImatch specification\fR, match_spec\&. A brief explanation is given in select/2\&. For a detailed description, see the chapter "Match specifications in Erlang" in \fIERTS User\&'s Guide\fR\&.
.SH DATA TYPES
.nf
match_spec()
a match specification, see above
tid()
a table identifier, as returned by new/2
.fi
.SH EXPORTS
.LP
.B
all() -> [Tab]
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
.RE
.RS
.LP
Returns a list of all tables at the node\&. Named tables are given by their names, unnamed tables are given by their table identifiers\&.
.RE
.LP
.B
delete(Tab) -> true
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
.RE
.RS
.LP
Deletes the entire table \fITab\fR\&.
.RE
.LP
.B
delete(Tab, Key) -> true
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Key = term()
.br
.RE
.RS
.LP
Deletes all objects with the key \fIKey\fR from the table \fITab\fR\&.
.RE
.LP
.B
delete_all_objects(Tab) -> true
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
.RE
.RS
.LP
Delete all objects in the ETS table \fITab\fR\&. The deletion is atomic\&.
.RE
.LP
.B
delete_object(Tab,Object) -> true
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Object = tuple()
.br
.RE
.RS
.LP
Delete the exact object \fIObject\fR from the ETS table, leaving objects with the same key but other differences (useful for type \fIbag\fR)\&.
.RE
.LP
.B
file2tab(Filename) -> {ok,Tab} | {error,Reason}
.br
.RS
.TP
Types
Filename = string() | atom()
.br
Tab = tid() | atom()
.br
Reason = term()
.br
.RE
.RS
.LP
Reads a file produced by tab2file/2 or tab2file/3 and creates the corresponding table \fITab\fR\&.
.LP
Equivalent to \fIfile2tab(Filename, [])\fR\&.
.RE
.LP
.B
file2tab(Filename,Options) -> {ok,Tab} | {error,Reason}
.br
.RS
.TP
Types
Filename = string() | atom()
.br
Tab = tid() | atom()
.br
Options = [Option]
.br
Option = {verify, bool()}
.br
Reason = term()
.br
.RE
.RS
.LP
Reads a file produced by tab2file/2 or tab2file/3 and creates the corresponding table \fITab\fR\&.
.LP
The currently only supported option is \fI{verify, bool()}\fR\&. If verification is turned on (by means of specifying \fI{verify, true}\fR), the function utilizes whatever information is present in the file to assert that the information is not damaged\&. How this is done depends on which \fIextended_info\fR was written using tab2file/3\&.
.LP
If no \fIextended_info\fR is present in the file and \fI{verify, true}\fR is specified, the number of objects written is compared to the size of the original table when the dump was started\&. This might make verification fail if the table was \fIpublic\fR and objects were added or removed while the table was dumped to file\&. To avoid this type of problems, either do not verify files dumped while updated simultaneously or use the \fI{extended_info, [object_count]}\fR option to tab2file/3, which extends the information in the file with the number of objects actually written\&.
.LP
If verification is turned on and the file was written with the option \fI{extended_info, [md5sum]}\fR, reading the file is slower and consumes radically more CPU time than otherwise\&.
.LP
\fI{verify, false}\fR is the default\&.
.RE
.LP
.B
first(Tab) -> Key | \&'$end_of_table\&'
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Key = term()
.br
.RE
.RS
.LP
Returns the first key \fIKey\fR in the table \fITab\fR\&. If the table is of the \fIordered_set\fR type, the first key in Erlang term order will be returned\&. If the table is of any other type, the first key according to the table\&'s internal order will be returned\&. If the table is empty, \fI\&'$end_of_table\&'\fR will be returned\&.
.LP
Use \fInext/2\fR to find subsequent keys in the table\&.
.RE
.LP
.B
fixtable(Tab, true|false) -> true | false
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
.RE
.RS
.SS Warning:
.LP
The function is retained for backwards compatibility only\&. Use \fIsafe_fixtable/2\fR instead\&.
.LP
Fixes a table for safe traversal\&. The function is primarily used by the Mnesia DBMS to implement functions which allow write operations in a table, although the table is in the process of being copied to disk or to another node\&. It does not keep track of when and how tables are fixed\&.
.RE
.LP
.B
foldl(Function, Acc0, Tab) -> Acc1
.br
.RS
.TP
Types
Function = fun(A, AccIn) -> AccOut
.br
Tab = tid() | atom()
.br
Acc0 = Acc1 = AccIn = AccOut = term()
.br
.RE
.RS
.LP
\fIAcc0\fR is returned if the table is empty\&. This function is similar to \fIlists:foldl/3\fR\&. The order in which the elements of the table are traversed is unspecified, except for tables of type \fIordered_set\fR, for which they are traversed first to last\&.
.LP
If \fIFunction\fR inserts objects into the table, or another process inserts objects into the table, those objects \fImay\fR (depending on key ordering) be included in the traversal\&.
.RE
.LP
.B
foldr(Function, Acc0, Tab) -> Acc1
.br
.RS
.TP
Types
Function = fun(A, AccIn) -> AccOut
.br
Tab = tid() | atom()
.br
Acc0 = Acc1 = AccIn = AccOut = term()
.br
.RE
.RS
.LP
\fIAcc0\fR is returned if the table is empty\&. This function is similar to \fIlists:foldr/3\fR\&. The order in which the elements of the table are traversed is unspecified, except for tables of type \fIordered_set\fR, for which they are traversed last to first\&.
.LP
If \fIFunction\fR inserts objects into the table, or another process inserts objects into the table, those objects \fImay\fR (depending on key ordering) be included in the traversal\&.
.RE
.LP
.B
from_dets(Tab, DetsTab) -> true
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
DetsTab = atom()
.br
.RE
.RS
.LP
Fills an already created ETS table with the objects in the already opened Dets table named \fIDetsTab\fR\&. The existing objects of the ETS table are kept unless overwritten\&.
.LP
Throws a badarg error if any of the tables does not exist or the dets table is not open\&.
.RE
.LP
.B
fun2ms(LiteralFun) -> MatchSpec
.br
.RS
.TP
Types
LiteralFun -- see below
.br
MatchSpec = match_spec()
.br
.RE
.RS
.LP
Pseudo function that by means of a \fIparse_transform\fR translates \fILiteralFun\fR typed as parameter in the function call to a match_spec\&. With "literal" is meant that the fun needs to textually be written as the parameter of the function, it cannot be held in a variable which in turn is passed to the function)\&.
.LP
The parse transform is implemented in the module \fIms_transform\fR and the source \fImust\fR include the file \fIms_transform\&.hrl\fR in \fIstdlib\fR for this pseudo function to work\&. Failing to include the hrl file in the source will result in a runtime error, not a compile time ditto\&. The include file is easiest included by adding the line \fI-include_lib("stdlib/include/ms_transform\&.hrl")\&.\fR to the source file\&.
.LP
The fun is very restricted, it can take only a single parameter (the object to match): a sole variable or a tuple\&. It needs to use the \fIis_\fRXXX guard tests\&. Language constructs that have no representation in a match_spec (like \fIif\fR, \fIcase\fR, \fIreceive\fR etc) are not allowed\&.
.LP
The return value is the resulting match_spec\&.
.LP
Example:
.nf
1> ets:fun2ms(fun({M,N}) when N > 3 -> M end)\&.
[{{\&'$1\&',\&'$2\&'},[{\&'>\&',\&'$2\&',3}],[\&'$1\&']}]
.fi
.LP
Variables from the environment can be imported, so that this works:
.nf
2> X=3\&.
3
3> ets:fun2ms(fun({M,N}) when N > X -> M end)\&.
[{{\&'$1\&',\&'$2\&'},[{\&'>\&',\&'$2\&',{const,3}}],[\&'$1\&']}]
.fi
.LP
The imported variables will be replaced by match_spec \fIconst\fR expressions, which is consistent with the static scoping for Erlang funs\&. Local or global function calls can not be in the guard or body of the fun however\&. Calls to builtin match_spec functions of course is allowed:
.nf
4> ets:fun2ms(fun({M,N}) when N > X, is_atomm(M) -> M end)\&.
Error: fun containing local Erlang function calls
(\&'is_atomm\&' called in guard) cannot be translated into match_spec
{error,transform_error}
5> ets:fun2ms(fun({M,N}) when N > X, is_atom(M) -> M end)\&.
[{{\&'$1\&',\&'$2\&'},[{\&'>\&',\&'$2\&',{const,3}},{is_atom,\&'$1\&'}],[\&'$1\&']}]
.fi
.LP
As can be seen by the example, the function can be called from the shell too\&. The fun needs to be literally in the call when used from the shell as well\&. Other means than the parse_transform are used in the shell case, but more or less the same restrictions apply (the exception being records, as they are not handled by the shell)\&.
.SS Warning:
.LP
If the parse_transform is not applied to a module which calls this pseudo function, the call will fail in runtime (with a \fIbadarg\fR)\&. The module \fIets\fR actually exports a function with this name, but it should never really be called except for when using the function in the shell\&. If the \fIparse_transform\fR is properly applied by including the \fIms_transform\&.hrl\fR header file, compiled code will never call the function, but the function call is replaced by a literal match_spec\&.
.LP
For more information, see ms_transform(3)\&.
.RE
.LP
.B
i() -> void()
.br
.RS
.LP
Displays information about all ETS tables on tty\&.
.RE
.LP
.B
i(Tab) -> void()
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
.RE
.RS
.LP
Browses the table \fITab\fR on tty\&.
.RE
.LP
.B
info(Tab) -> [{Item, Value}] | undefined
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Item = atom(), see below
.br
Value = term(), see below
.br
.RE
.RS
.LP
Returns information about the table \fITab\fR as a list of \fI{Item, Value}\fR tuples\&. If \fITab\fR has the correct type for a table identifier, but does not refer to an existing ETS table, \fIundefined\fR is returned\&. If \fITab\fR is not of the correct type, this function fails with reason \fIbadarg\fR\&.
.RS 2
.TP 2
*
\fIItem=memory, Value=int()\fR
.br
The number of words allocated to the table\&.
.TP 2
*
\fIItem=owner, Value=pid()\fR
.br
The pid of the owner of the table\&.
.TP 2
*
\fIItem=name, Value=atom()\fR
.br
The name of the table\&.
.TP 2
*
\fIItem=size, Value=int()\fR
.br
The number of objects inserted in the table\&.
.TP 2
*
\fIItem=node, Value=atom()\fR
.br
The node where the table is stored\&. This field is no longer meaningful as tables cannot be accessed from other nodes\&.
.TP 2
*
\fIItem=named_table, Value=true|false\fR
.br
Indicates if the table is named or not\&.
.TP 2
*
\fIItem=type, Value=set|ordered_set|bag|duplicate_bag\fR
.br
The table type\&.
.TP 2
*
\fIItem=keypos, Value=int()\fR
.br
The key position\&.
.TP 2
*
\fIItem=protection, Value=public|protected|private\fR
.br
The table access rights\&.
.RE
.RE
.LP
.B
info(Tab, Item) -> Value | undefined
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Item, Value - see below
.br
.RE
.RS
.LP
Returns the information associated with \fIItem\fR for the table \fITab\fR, or returns \fIundefined\fR if \fITab\fR does not refer an existing ETS table\&. If \fITab\fR is not of the correct type, or if \fIItem\fR is not one of the allowed values, this function fails with reason \fIbadarg\fR\&.
.SS Warning:
.LP
In R11B and earlier, this function would not fail but return \fIundefined\fR for invalid values for \fIItem\fR\&.
.LP
In addition to the \fI{Item, Value}\fR pairs defined for \fIinfo/1\fR, the following items are allowed:
.RS 2
.TP 2
*
\fIItem=fixed, Value=true|false\fR
.br
Indicates if the table is fixed by any process or not\&.
.TP 2
*
\fIItem=safe_fixed, Value={FirstFixed, Info}|false\fR
.br
.RS 2
.LP
.LP
If the table has been fixed using \fIsafe_fixtable/2\fR, the call returns a tuple where \fIFirstFixed\fR is the time when the table was first fixed by a process, which may or may not be one of the processes it is fixed by right now\&.
.LP
.LP
\fIInfo\fR is a possibly empty lists of tuples \fI{Pid, RefCount}\fR, one tuple for every process the table is fixed by right now\&. \fIRefCount\fR is the value of the reference counter, keeping track of how many times the table has been fixed by the process\&.
.LP
.LP
If the table never has been fixed, the call returns \fIfalse\fR\&.
.RE
.RE
.RE
.LP
.B
init_table(Name, InitFun) -> true
.br
.RS
.TP
Types
Name = atom()
.br
InitFun = fun(Arg) -> Res
.br
Arg = read | close
.br
Res = end_of_input | {[object()], InitFun} | term()
.br
.RE
.RS
.LP
Replaces the existing objects of the table \fITab\fR with objects created by calling the input function \fIInitFun\fR, see below\&. This function is provided for compatibility with the \fIdets\fR module, it is not more efficient than filling a table by using \fIets:insert/2\fR\&.
.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\&. This holds also for duplicated objects stored in tables of type \fIduplicate_bag\fR\&.
.RE
.LP
.B
insert(Tab, ObjectOrObjects) -> true
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
ObjectOrObjects = tuple() | [tuple()]
.br
.RE
.RS
.LP
Inserts the object or all of the objects in the list \fIObjectOrObjects\fR into the table \fITab\fR\&. If the table is a \fIset\fR and the key of the inserted objects \fImatches\fR the key of any object in the table, the old object will be replaced\&. If the table is an \fIordered_set\fR and the key of the inserted object \fIcompares equal\fR to the key of any object in the table, the old object is also replaced\&. If the list contains more than one object with \fImatching\fR keys and the table is a \fIset\fR, one will be inserted, which one is not defined\&. The same thing holds for \fIordered_set\fR, but will also happen if the keys \fIcompare equal\fR\&.
.RE
.LP
.B
insert_new(Tab, ObjectOrObjects) -> bool()
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
ObjectOrObjects = tuple() | [tuple()]
.br
.RE
.RS
.LP
This function works exactly like \fIinsert/2\fR, with the exception that instead of overwriting objects with the same key (in the case of \fIset\fR or \fIordered_set\fR) or adding more objects with keys already existing in the table (in the case of \fIbag\fR and \fIduplicate_bag\fR), it simply returns \fIfalse\fR\&. If \fIObjectOrObjects\fR is a list, the function checks \fIevery\fR key prior to inserting anything\&. Nothing will be inserted if not \fIall\fR keys present in the list are absent from the table\&.
.RE
.LP
.B
is_compiled_ms(Term) -> bool()
.br
.RS
.TP
Types
Term = term()
.br
.RE
.RS
.LP
This function is used to check if a term is a valid compiled match_spec\&. The compiled match_spec is an opaque datatype which can \fInot\fR be sent between Erlang nodes nor be stored on disk\&. Any attempt to create an external representation of a compiled match_spec will result in an empty binary (\fI<<>>\fR)\&. As an example, the following expression:
.nf
ets:is_compiled_ms(ets:match_spec_compile([{\&'_\&',[],[true]}]))\&.
.fi
.LP
will yield \fItrue\fR, while the following expressions:
.nf
MS = ets:match_spec_compile([{\&'_\&',[],[true]}]),
Broken = binary_to_term(term_to_binary(MS)),
ets:is_compiled_ms(Broken)\&.
.fi
.LP
will yield false, as the variable \fIBroken\fR will contain a compiled match_spec that has passed through external representation\&.
.SS Note:
.LP
The fact that compiled match_specs has no external representation is for performance reasons\&. It may be subject to change in future releases, while this interface will still remain for backward compatibility reasons\&.
.RE
.LP
.B
last(Tab) -> Key | \&'$end_of_table\&'
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Key = term()
.br
.RE
.RS
.LP
Returns the last key \fIKey\fR according to Erlang term order in the table \fITab\fR of the \fIordered_set\fR type\&. If the table is of any other type, the function is synonymous to \fIfirst/2\fR\&. If the table is empty, \fI\&'$end_of_table\&'\fR is returned\&.
.LP
Use \fIprev/2\fR to find preceding keys in the table\&.
.RE
.LP
.B
lookup(Tab, Key) -> [Object]
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Key = term()
.br
Object = tuple()
.br
.RE
.RS
.LP
Returns a list of all objects with the key \fIKey\fR in the table \fITab\fR\&.
.LP
In the case of \fIset, bag and duplicate_bag\fR, an object is returned only if the given key \fImatches\fR the key of the object in the table\&. If the table is an \fIordered_set\fR however, an object is returned if the key given \fIcompares equal\fR to the key of an object in the table\&. The difference being the same as between \fI=:=\fR and \fI==\fR\&. As an example, one might insert an object with the \fIinteger()\fR\fI1\fR as a key in an \fIordered_set\fR and get the object returned as a result of doing a \fIlookup/2\fR with the \fIfloat()\fR\fI1\&.0\fR as the key to search for\&.
.LP
If the table is of type \fIset\fR or \fIordered_set\fR, the function returns either the empty list or a list with one element, as there cannot be more than one object with the same 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 time order of object insertions is preserved; The first object inserted with the given key will be first in the resulting list, and so on\&.
.LP
Insert and look-up times in tables of type \fIset\fR, \fIbag\fR and \fIduplicate_bag\fR are constant, regardless of the size of the table\&. For the \fIordered_set\fR data-type, time is proportional to the (binary) logarithm of the number of objects\&.
.RE
.LP
.B
lookup_element(Tab, Key, Pos) -> Elem
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Key = term()
.br
Pos = int()
.br
Elem = term() | [term()]
.br
.RE
.RS
.LP
If the table \fITab\fR is of type \fIset\fR or \fIordered_set\fR, the function returns the \fIPos\fR:th element of the object with the key \fIKey\fR\&.
.LP
If the table is of type \fIbag\fR or \fIduplicate_bag\fR, the functions returns a list with the \fIPos\fR:th element of every object with the key \fIKey\fR\&.
.LP
If no object with the key \fIKey\fR exists, the function will exit with reason \fIbadarg\fR\&.
.LP
The difference between \fIset\fR, \fIbag\fR and \fIduplicate_bag\fR on one hand, and \fIordered_set\fR on the other, regarding the fact that \fIordered_set\fR\&'s view keys as equal when they \fIcompare equal\fR whereas the other table types only regard them equal when they \fImatch\fR, naturally holds for \fIlookup_element\fR as well as for \fIlookup\fR\&.
.RE
.LP
.B
match(Tab, Pattern) -> [Match]
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Pattern = tuple()
.br
Match = [term()]
.br
.RE
.RS
.LP
Matches the objects in the table \fITab\fR against the pattern \fIPattern\fR\&.
.LP
A pattern is a term that may contain:
.RS 2
.TP 2
*
bound parts (Erlang terms),
.TP 2
*
\fI\&'_\&'\fR which matches any Erlang term, and
.TP 2
*
pattern variables: \fI\&'$N\&'\fR where \fIN\fR=0,1,\&.\&.\&.
.RE
.LP
The function returns a list with one element for each matching object, where each element is an ordered list of pattern variable bindings\&. An example:
.nf
6> ets:match(T, \&'$1\&')\&.
% Matches every object in the table
[[{rufsen,dog,7}],[{brunte,horse,5}],[{ludde,dog,5}]]
7> ets:match(T, {\&'_\&',dog,\&'$1\&'})\&.
[[7],[5]]
8> ets:match(T, {\&'_\&',cow,\&'$1\&'})\&.
[]
.fi
.LP
If the key is specified in the pattern, the match is very efficient\&. If the key is not specified, i\&.e\&. if it is a variable or an underscore, the entire table must be searched\&. The search time can be substantial if the table is very large\&.
.LP
On tables of the \fIordered_set\fR type, the result is in the same order as in a \fIfirst/next\fR traversal\&.
.RE
.LP
.B
match(Tab, Pattern, Limit) -> {[Match],Continuation} | \&'$end_of_table\&'
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Pattern = tuple()
.br
Match = [term()]
.br
Continuation = term()
.br
.RE
.RS
.LP
Works like \fIets:match/2\fR but only returns a limited (\fILimit\fR) number of matching objects\&. The \fIContinuation\fR term can then be used in subsequent calls to \fIets:match/1\fR to get the next chunk of matching objects\&. This is a space efficient way to work on objects in a table which is still faster than traversing the table object by object using \fIets:first/1\fR and \fIets:next/1\fR\&.
.LP
\fI\&'$end_of_table\&'\fR is returned if the table is empty\&.
.RE
.LP
.B
match(Continuation) -> {[Match],Continuation} | \&'$end_of_table\&'
.br
.RS
.TP
Types
Match = [term()]
.br
Continuation = term()
.br
.RE
.RS
.LP
Continues a match started with \fIets:match/3\fR\&. The next chunk of the size given in the initial \fIets:match/3\fR call is returned together with a new \fIContinuation\fR that can be used in subsequent calls to this function\&.
.LP
\fI\&'$end_of_table\&'\fR is returned when there are no more objects in the table\&.
.RE
.LP
.B
match_delete(Tab, Pattern) -> true
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Pattern = tuple()
.br
.RE
.RS
.LP
Deletes all objects which match the pattern \fIPattern\fR from the table \fITab\fR\&. See \fImatch/2\fR for a description of patterns\&.
.RE
.LP
.B
match_object(Tab, Pattern) -> [Object]
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Pattern = Object = tuple()
.br
.RE
.RS
.LP
Matches the objects in the table \fITab\fR against the pattern \fIPattern\fR\&. See \fImatch/2\fR for a description of patterns\&. The function returns a list of all objects which match the pattern\&.
.LP
If the key is specified in the pattern, the match is very efficient\&. If the key is not specified, i\&.e\&. if it is a variable or an underscore, the entire table must be searched\&. The search time can be substantial if the table is very large\&.
.LP
On tables of the \fIordered_set\fR type, the result is in the same order as in a \fIfirst/next\fR traversal\&.
.RE
.LP
.B
match_object(Tab, Pattern, Limit) -> {[Match],Continuation} | \&'$end_of_table\&'
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Pattern = tuple()
.br
Match = [term()]
.br
Continuation = term()
.br
.RE
.RS
.LP
Works like \fIets:match_object/2\fR but only returns a limited (\fILimit\fR) number of matching objects\&. The \fIContinuation\fR term can then be used in subsequent calls to \fIets:match_object/1\fR to get the next chunk of matching objects\&. This is a space efficient way to work on objects in a table which is still faster than traversing the table object by object using \fIets:first/1\fR and \fIets:next/1\fR\&.
.LP
\fI\&'$end_of_table\&'\fR is returned if the table is empty\&.
.RE
.LP
.B
match_object(Continuation) -> {[Match],Continuation} | \&'$end_of_table\&'
.br
.RS
.TP
Types
Match = [term()]
.br
Continuation = term()
.br
.RE
.RS
.LP
Continues a match started with \fIets:match_object/3\fR\&. The next chunk of the size given in the initial \fIets:match_object/3\fR call is returned together with a new \fIContinuation\fR that can be used in subsequent calls to this function\&.
.LP
\fI\&'$end_of_table\&'\fR is returned when there are no more objects in the table\&.
.RE
.LP
.B
match_spec_compile(MatchSpec) -> CompiledMatchSpec
.br
.RS
.TP
Types
MatchSpec = match_spec()
.br
CompiledMatchSpec = comp_match_spec()
.br
.RE
.RS
.LP
This function transforms a match_spec into an internal representation that can be used in subsequent calls to \fIets:match_spec_run/2\fR\&. The internal representation is opaque and can not be converted to external term format and then back again without losing its properties (meaning it can not be sent to a process on another node and still remain a valid compiled match_spec, nor can it be stored on disk)\&. The validity of a compiled match_spec can be checked using \fIets:is_compiled_ms/1\fR\&.
.LP
If the term \fIMatchSpec\fR can not be compiled (does not represent a valid match_spec), a \fIbadarg\fR fault is thrown\&.
.SS Note:
.LP
This function has limited use in normal code, it is used by Dets to perform the \fIdets:select\fR operations\&.
.RE
.LP
.B
match_spec_run(List,CompiledMatchSpec) -> list()
.br
.RS
.TP
Types
List = [ tuple() ]
.br
CompiledMatchSpec = comp_match_spec()
.br
.RE
.RS
.LP
This function executes the matching specified in a compiled match_spec on a list of tuples\&. The \fICompiledMatchSpec\fR term should be the result of a call to \fIets:match_spec_compile/1\fR and is hence the internal representation of the match_spec one wants to use\&.
.LP
The matching will be executed on each element in \fIList\fR and the function returns a list containing all results\&. If an element in \fIList\fR does not match, nothing is returned for that element\&. The length of the result list is therefore equal or less than the the length of the parameter \fIList\fR\&. The two calls in the following example will give the same result (but certainly not the same execution time\&.\&.\&.):
.nf
Table = ets:new\&.\&.\&.
MatchSpec = \&.\&.\&.\&.
% The following call\&.\&.\&.
ets:match_spec_run(ets:tab2list(Table),
ets:match_spec_compile(MatchSpec)),
% \&.\&.\&.will give the same result as the more common (and more efficient)
ets:select(Table,MatchSpec),
.fi
.SS Note:
.LP
This function has limited use in normal code, it is used by Dets to perform the \fIdets:select\fR operations and by Mnesia during transactions\&.
.RE
.LP
.B
member(Tab, Key) -> true | false
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Key = term()
.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 in the table has the key \fIKey\fR, \fIfalse\fR otherwise\&.
.RE
.LP
.B
new(Name, Options) -> tid()
.br
.RS
.TP
Types
Name = atom()
.br
Options = [Option]
.br
Option = Type | Access | named_table | {keypos, Pos}
.br
Type = set | ordered_set | bag | duplicate_bag
.br
Access = public | protected | private
.br
Pos = int()
.br
.RE
.RS
.LP
Creates a new table and returns a table identifier which can be used in subsequent operations\&. The table identifier can be sent to other processes so that a table can be shared between different processes within a node\&.
.LP
The parameter \fIOptions\fR is a list of atoms which specifies table type, access rights, key position and if the table is named or not\&. If one or more options are left out, the default values are used\&. This means that not specifying any options (\fI[]\fR) is the same as specifying \fI[set, protected, {keypos, 1}]\fR\&.
.RS 2
.TP 2
*
\fIset\fR The table is a \fIset\fR table - one key, one object, no order among objects\&. This is the default table type\&.
.TP 2
*
\fIordered_set\fR The table is a \fIordered_set\fR table - one key, one object, ordered in Erlang term order, which is the order implied by the < and > operators\&. Tables of this type have a somewhat different behavior in some situations than tables of the other types\&. Most notably the \fIordered_set\fR tables regard keys as equal when they \fIcompare equal\fR, not only when they match\&. This means that to an \fIordered_set\fR, the \fIinteger()\fR\fI1\fR and the \fIfloat()\fR\fI1\&.0\fR are regarded as equal\&. This also means that the key used to lookup an element not necessarily \fImatches\fR the key in the elements returned, if \fIfloat()\fR\&'s and \fIinteger()\fR\&'s are mixed in keys of a table\&.
.TP 2
*
\fIbag\fR The table is a \fIbag\fR table which can have many objects, but only one instance of each object, per key\&.
.TP 2
*
\fIduplicate_bag\fR The table is a \fIduplicate_bag\fR table which can have many objects, including multiple copies of the same object, per key\&.
.TP 2
*
\fIpublic\fR Any process may read or write to the table\&.
.TP 2
*
\fIprotected\fR The owner process can read and write to the table\&. Other processes can only read the table\&. This is the default setting for the access rights\&.
.TP 2
*
\fIprivate\fR Only the owner process can read or write to the table\&.
.TP 2
*
\fInamed_table\fR If this option is present, the name \fIName\fR is associated with the table identifier\&. The name can then be used instead of the table identifier in subsequent operations\&.
.TP 2
*
\fI{keypos, Pos}\fR Specfies which element in the stored tuples should be used as key\&. By default, it is the first element, i\&.e\&. \fIPos=1\fR\&. However, this is not always appropriate\&. In particular, we do not want the first element to be the key if we want to store Erlang records in a table\&.
.RS 2
.LP
.LP
Note that any tuple stored in the table must have at least \fIPos\fR number of elements\&.
.RE
.RE
.RE
.LP
.B
next(Tab, Key1) -> Key2 | \&'$end_of_table\&'
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Key1 = Key2 = term()
.br
.RE
.RS
.LP
Returns the next key \fIKey2\fR, following the key \fIKey1\fR in the table \fITab\fR\&. If the table is of the \fIordered_set\fR type, the next key in Erlang term order is returned\&. If the table is of any other type, the next key according to the table\&'s internal order is returned\&. If there is no next key, \fI\&'$end_of_table\&'\fR is returned\&.
.LP
Use \fIfirst/1\fR to find the first key in the table\&.
.LP
Unless a table of type \fIset\fR, \fIbag\fR or \fIduplicate_bag\fR is protected using \fIsafe_fixtable/2\fR, see below, a traversal may fail if concurrent updates are made to the table\&. If the table is of type \fIordered_set\fR, the function returns the next key in order, even if the object does no longer exist\&.
.RE
.LP
.B
prev(Tab, Key1) -> Key2 | \&'$end_of_table\&'
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Key1 = Key2 = term()
.br
.RE
.RS
.LP
Returns the previous key \fIKey2\fR, preceding the key \fIKey1\fR according the Erlang term order in the table \fITab\fR of the \fIordered_set\fR type\&. If the table is of any other type, the function is synonymous to \fInext/2\fR\&. If there is no previous key, \fI\&'$end_of_table\&'\fR is returned\&.
.LP
Use \fIlast/1\fR to find the last key in the table\&.
.RE
.LP
.B
rename(Tab, Name) -> Name
.br
.RS
.TP
Types
Tab = Name = atom()
.br
.RE
.RS
.LP
Renames the named table \fITab\fR to the new name \fIName\fR\&. Afterwards, the old name can not be used to access the table\&. Renaming an unnamed table has no effect\&.
.RE
.LP
.B
repair_continuation(Continuation, MatchSpec) -> Continuation
.br
.RS
.TP
Types
Continuation = term()
.br
MatchSpec = match_spec()
.br
.RE
.RS
.LP
This function can be used to restore an opaque continuation returned by \fIets:select/3\fR or \fIets:select/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_specs and therefore will be invalidated if converted to external term format\&. Given that the original match_spec is kept intact, the continuation can be restored, meaning it can once again be used in subsequent \fIets:select/1\fR calls even though it has been stored on disk or on another node\&.
.LP
As an example, the following seqence of calls will fail:
.nf
T=ets:new(x,[]),
\&.\&.\&.
{_,C} = ets:select(T,ets:fun2ms(fun({N,_}=A)
when (N rem 10) =:= 0 ->
A
end),10),
Broken = binary_to_term(term_to_binary(C)),
ets:select(Broken)\&.
.fi
.LP
\&.\&.\&.while the following sequence will work:
.nf
T=ets:new(x,[]),
\&.\&.\&.
MS = ets:fun2ms(fun({N,_}=A)
when (N rem 10) =:= 0 ->
A
end),
{_,C} = ets:select(T,MS,10),
Broken = binary_to_term(term_to_binary(C)),
ets:select(ets:repair_continuation(Broken,MS))\&.
.fi
.LP
\&.\&.\&.as the call to \fIets:repair_continuation/2\fR will reestablish the (deliberately) invalidated continuation \fIBroken\fR\&.
.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 a compiled match_spec is performance\&. It may be subject to change in future releases, while this interface will remain for backward compatibility\&.
.RE
.LP
.B
safe_fixtable(Tab, true|false) -> true
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
.RE
.RS
.LP
Fixes a table of the \fIset\fR, \fIbag\fR or \fIduplicate_bag\fR table type for safe traversal\&.
.LP
A process fixes a table by calling \fIsafe_fixtable(Tab, true)\fR\&. The table remains fixed until the process releases it by calling \fIsafe_fixtable(Tab, false)\fR, or until the 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 requires N releases to actually release the table\&.
.LP
When a table is fixed, a sequence of \fIfirst/1\fR and \fInext/2\fR calls are guaranteed to succeed and each object in the table will only be returned once, even if objects are removed or inserted during the traversal\&. The keys for new objects inserted during the traversal \fImay\fR be returned by next/2 (it depends on the internal ordering of the keys)\&. An example:
.nf
clean_all_with_value(Tab,X) ->
safe_fixtable(Tab,true),
clean_all_with_value(Tab,X,ets:first(Tab)),
safe_fixtable(Tab,false)\&.
clean_all_with_value(Tab,X,\&'$end_of_table\&') ->
true;
clean_all_with_value(Tab,X,Key) ->
case ets:lookup(Tab,Key) of
[{Key,X}] ->
ets:delete(Tab,Key);
_ ->
true
end,
clean_all_with_value(Tab,X,ets:next(Tab,Key))\&.
.fi
.LP
Note that no deleted objects are actually removed from a fixed table until it has been released\&. If a process fixes a table but never releases it, the memory used by the deleted objects will never be freed\&. The performance of operations on the table will also degrade significantly\&.
.LP
Use \fIinfo/2\fR to retrieve information about which processes have fixed which tables\&. A system with a lot of processes fixing tables may need a monitor which sends alarms when tables have been fixed for too long\&.
.LP
Note that for tables of the \fIordered_set\fR type, \fIsafe_fixtable/2\fR is not necessary as calls to \fIfirst/1\fR and \fInext/2\fR will always succeed\&.
.RE
.LP
.B
select(Tab, MatchSpec) -> [Match]
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Match = term()
.br
MatchSpec = match_spec()
.br
.RE
.RS
.LP
Matches the objects in the table \fITab\fR using a match_spec\&. This is a more general call than the \fIets:match/2\fR and \fIets:match_object/2\fR calls\&. In its simplest forms the match_specs look like this:
.RS 2
.TP 2
*
MatchSpec = [MatchFunction]
.TP 2
*
MatchFunction = {MatchHead, [Guard], [Result]}
.TP 2
*
MatchHead = "Pattern as in ets:match"
.TP 2
*
Guard = {"Guardtest name", \&.\&.\&.}
.TP 2
*
Result = "Term construct"
.RE
.LP
This means that the match_spec is always a list of one or more tuples (of arity 3)\&. The tuples first element should be a pattern as described in the documentation of \fIets:match/2\fR\&. The second element of the tuple should be a list of 0 or more guard tests (described below)\&. The third element of the tuple should be a list containing a description of the value to actually return\&. In almost all normal cases the list contains exactly one term which fully describes the value to return for each object\&.
.LP
The return value is constructed using the "match variables" bound in the MatchHead or using the special match variables \fI\&'$_\&'\fR (the whole matching object) and \fI\&'$$\&'\fR (all match variables in a list), so that the following \fIets:match/2\fR expression:
.nf
ets:match(Tab,{\&'$1\&',\&'$2\&',\&'$3\&'})
.fi
.LP
is exactly equivalent to:
.nf
ets:select(Tab,[{{\&'$1\&',\&'$2\&',\&'$3\&'},[],[\&'$$\&']}])
.fi
.LP
- and the following \fIets:match_object/2\fR call:
.nf
ets:match_object(Tab,{\&'$1\&',\&'$2\&',\&'$1\&'})
.fi
.LP
is exactly equivalent to
.nf
ets:select(Tab,[{{\&'$1\&',\&'$2\&',\&'$1\&'},[],[\&'$_\&']}])
.fi
.LP
Composite terms can be constructed in the \fIResult\fR part either by simply writing a list, so that this code:
.nf
ets:select(Tab,[{{\&'$1\&',\&'$2\&',\&'$3\&'},[],[\&'$$\&']}])
.fi
.LP
gives the same output as:
.nf
ets:select(Tab,[{{\&'$1\&',\&'$2\&',\&'$3\&'},[],[[\&'$1\&',\&'$2\&',\&'$3\&']]}])
.fi
.LP
i\&.e\&. all the bound variables in the match head as a list\&. If tuples are to be constructed, one has to write a tuple of arity 1 with the single element in the tuple being the tuple one wants to construct (as an ordinary tuple could be mistaken for a \fIGuard\fR)\&. Therefore the following call:
.nf
ets:select(Tab,[{{\&'$1\&',\&'$2\&',\&'$1\&'},[],[\&'$_\&']}])
.fi
.LP
gives the same output as:
.nf
ets:select(Tab,[{{\&'$1\&',\&'$2\&',\&'$1\&'},[],[{{\&'$1\&',\&'$2\&',\&'$3\&'}}]}])
.fi
.LP
- this syntax is equivalent to the syntax used in the trace patterns (see dbg(3))\&.
.LP
The \fIGuard\fRs are constructed as tuples where the first element is the name of the test and the rest of the elements are the parameters of the test\&. To check for a specific type (say a list) of the element bound to the match variable \fI\&'$1\&'\fR, one would write the test as \fI{is_list, \&'$1\&'}\fR\&. If the test fails, the object in the table will not match and the next \fIMatchFunction\fR (if any) will be tried\&. Most guard tests present in Erlang can be used, but only the new versions prefixed \fIis_\fR are allowed (like \fIis_float\fR, \fIis_atom\fR etc)\&.
.LP
The \fIGuard\fR section can also contain logic and arithmetic operations, which are written with the same syntax as the guard tests (prefix notation), so that a guard test written in Erlang looking like this:
.nf
is_integer(X), is_integer(Y), X + Y < 4711
.fi
.LP
is expressed like this (X replaced with \&'$1\&' and Y with \&'$2\&'):
.nf
[{is_integer, \&'$1\&'}, {is_integer, \&'$2\&'}, {\&'<\&', {\&'+\&', \&'$1\&', \&'$2\&'}, 4711}]
.fi
.RE
.LP
.B
select(Tab, MatchSpec, Limit) -> {[Match],Continuation} | \&'$end_of_table\&'
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Match = term()
.br
MatchSpec = match_spec()
.br
Continuation = term()
.br
.RE
.RS
.LP
Works like \fIets:select/2\fR but only returns a limited (\fILimit\fR) number of matching objects\&. The \fIContinuation\fR term can then be used in subsequent calls to \fIets:select/1\fR to get the next chunk of matching objects\&. This is a space efficient way to work on objects in a table which is still faster than traversing the table object by object using \fIets:first/1\fR and \fIets:next/1\fR\&.
.LP
\fI\&'$end_of_table\&'\fR is returned if the table is empty\&.
.RE
.LP
.B
select(Continuation) -> {[Match],Continuation} | \&'$end_of_table\&'
.br
.RS
.TP
Types
Match = term()
.br
Continuation = term()
.br
.RE
.RS
.LP
Continues a match started with \fIets:select/3\fR\&. The next chunk of the size given in the initial \fIets:select/3\fR call is returned together with a new \fIContinuation\fR that can be used in subsequent calls to this function\&.
.LP
\fI\&'$end_of_table\&'\fR is returned when there are no more objects in the table\&.
.RE
.LP
.B
select_delete(Tab, MatchSpec) -> NumDeleted
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Object = tuple()
.br
MatchSpec = match_spec()
.br
NumDeleted = integer()
.br
.RE
.RS
.LP
Matches the objects in the table \fITab\fR using a match_spec\&. If the match_spec returns \fItrue\fR for an object, that object is removed from the table\&. For any other result from the match_spec the object is retained\&. This is a more general call than the \fIets:match_delete/2\fR call\&.
.LP
The function returns the number of objects actually deleted from the table\&.
.SS Note:
.LP
The \fImatch_spec\fR has to return the atom \fItrue\fR if the object is to be deleted\&. No other return value will get the object deleted, why one can not use the same match specification for looking up elements as for deleting them\&.
.RE
.LP
.B
select_count(Tab, MatchSpec) -> NumMatched
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Object = tuple()
.br
MatchSpec = match_spec()
.br
NumMatched = integer()
.br
.RE
.RS
.LP
Matches the objects in the table \fITab\fR using a match_spec\&. If the match_spec returns \fItrue\fR for an object, that object considered a match and is counted\&. For any other result from the match_spec the object is not considered a match and is therefore not counted\&.
.LP
The function could be described as a \fImatch_delete/2\fR that does not actually delete any elements, but only counts them\&.
.LP
The function returns the number of objects matched\&.
.RE
.LP
.B
slot(Tab, I) -> [Object] | \&'$end_of_table\&'
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
I = int()
.br
Object = tuple()
.br
.RE
.RS
.LP
This function is mostly for debugging purposes, Normally one should use \fIfirst/next\fR or \fIlast/prev\fR instead\&.
.LP
Returns all objects in the \fII\fR:th slot of the table \fITab\fR\&. A table can be traversed by repeatedly calling the function, starting with the first slot \fII=0\fR and ending when \fI\&'$end_of_table\&'\fR is returned\&. The function will fail with reason \fIbadarg\fR if the \fII\fR argument is out of range\&.
.LP
Unless a table of type \fIset\fR, \fIbag\fR or \fIduplicate_bag\fR is protected using \fIsafe_fixtable/2\fR, see above, a traversal may fail if concurrent updates are made to the table\&. If the table is of type \fIordered_set\fR, the function returns a list containing the \fII\fR:th object in Erlang term order\&.
.RE
.LP
.B
tab2file(Tab, Filename) -> ok | {error,Reason}
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Filename = string() | atom()
.br
Reason = term()
.br
.RE
.RS
.LP
Dumps the table \fITab\fR to the file \fIFilename\fR\&.
.LP
Equivalent to \fItab2file(Tab, Filename, [])\fR
.RE
.LP
.B
tab2file(Tab, Filename, Options) -> ok | {error,Reason}
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Filename = string() | atom()
.br
Options = [Option]
.br
Option = {extended_info, [ExtInfo]}
.br
ExtInfo = object_count | md5sum
.br
Reason = term()
.br
.RE
.RS
.LP
Dumps the table \fITab\fR to the file \fIFilename\fR\&.
.LP
When dumping the table, certain information about the table is dumped to a header at the beginning of the dump\&. This information contains data about the table type, name, protection, size, version and if it\&'s a named table\&. It also contains notes about what extended information is added to the file, which can be a count of the objects in the file or a MD5 sum of the header and records in the file\&.
.LP
The size field in the header might not correspond to the actual number of records in the file if the table is public and records are added or removed from the table during dumping\&. Public tables updated during dump, and that one wants to verify when reading, needs at least one field of extended information for the read verification process to be reliable later\&.
.LP
The \fIextended_info\fR option specifies what extra information is written to the table dump:
.RS 2
.TP 4
.B
\fIobject_count\fR:
The number of objects actually written to the file is noted in the file footer, why verification of file truncation is possible even if the file was updated during dump\&.
.TP 4
.B
\fImd5sum\fR:
The header and objects in the file are checksummed using the built in MD5 functions\&. The MD5 sum of all objects is written in the file footer, so that verification while reading will detect the slightest bitflip in the file data\&. Using this costs a fair amount of CPU time\&.
.RE
.LP
Whenever the \fIextended_info\fR option is used, it results in a file not readable by versions of ets prior to that in stdlib-1\&.15\&.1
.RE
.LP
.B
tab2list(Tab) -> [Object]
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Object = tuple()
.br
.RE
.RS
.LP
Returns a list of all objects in the table \fITab\fR\&.
.RE
.LP
.B
tabfile_info(Filename) -> {ok, TableInfo} | {error, Reason}
.br
.RS
.TP
Types
Filename = string() | atom()
.br
TableInfo = [InfoItem]
.br
InfoItem = {InfoTag, term()}
.br
InfoTag = name | type | protection | named_table | keypos | size | extended_info | version
.br
Reason = term()
.br
.RE
.RS
.LP
Returns information about the table dumped to file by tab2file/2 or tab2file/3
.LP
The following items are returned:
.RS 2
.TP 4
.B
name:
The name of the dumped table\&. If the table was a named table, a table with the same name cannot exist when the table is loaded from file with file2tab/2\&. If the table is not saved as a named table, this field has no significance at all when loading the table from file\&.
.TP 4
.B
type:
The ets type of the dumped table (i\&.e\&. \fIset\fR, \fIbag\fR, \fIduplicate_bag\fR or \fIordered_set\fR)\&. This type will be used when loading the table again\&.
.TP 4
.B
protection:
The protection of the dumped table (i\&.e\&. \fIprivate\fR, \fIprotected\fR or \fIpublic\fR)\&. A table loaded from the file will get the same protection\&.
.TP 4
.B
named_table:
\fItrue\fR if the table was a named table when dumped to file, otherwise \fIfalse\fR\&. Note that when a named table is loaded from a file, there cannot exist a table in the system with the same name\&.
.TP 4
.B
keypos:
The \fIkeypos\fR of the table dumped to file, which will be used when loading the table again\&.
.TP 4
.B
size:
The number of objects in the table when the table dump to file started, which in case of a \fIpublic\fR table need not correspond to the number of objects actually saved to the file, as objects might have been added or deleted by another process during table dump\&.
.TP 4
.B
extended_info:
The extended information written in the file footer to allow stronger verification during table loading from file, as specified to tab2file/3\&. Note that this function only tells \fIwhich\fR information is present, not the values in the file footer\&. The value is a list containing one or more of the atoms \fIobject_count\fR and \fImd5sum\fR\&.
.TP 4
.B
version:
A tuple \fI{Major, Minor}\fR containing the major and minor version of the file format for ets table dumps\&. This version field was added beginning with stdlib-1\&.5\&.1, files dumped with older versions will return \fI{0, 0}\fR in this field\&.
.RE
.LP
An error is returned if the file is inaccessible, badly damaged or not an file produced with tab2file/2 or tab2file/3\&.
.RE
.LP
.B
table(Tab [, Options]) -> QueryHandle
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
QueryHandle = -a query handle, see qlc(3)-
.br
Options = [Option] | Option
.br
Option = {n_objects, NObjects} | {traverse, TraverseMethod}
.br
NObjects = default | integer() > 0
.br
TraverseMethod = first_next | last_prev | 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 \fIets:table/1, 2\fR is the means to make the ETS table \fITab\fR usable to QLC\&.
.LP
When there are only simple restrictions on the key position QLC uses \fIets: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 \fIets:first/1\fR and \fIets:next/2\fR\&.
.TP 2
*
\fIlast_prev\fR\&. The table is traversed one key at a time by calling \fIets:last/1\fR and \fIets:prev/2\fR\&.
.TP 2
*
\fIselect\fR\&. The table is traversed by calling \fIets:select/3\fR and \fIets:select/1\fR\&. The option \fIn_objects\fR determines the number of objects returned (the third argument of \fIselect/3\fR); the default is to return \fI100\fR objects at a time\&. The match_spec (the second argument of \fIselect/3\fR) is assembled by QLC: simple filters are translated into equivalent match_specs while more complicated filters have to be applied to all objects returned by \fIselect/3\fR given a match_spec that matches all objects\&.
.TP 2
*
\fI{select, MatchSpec}\fR\&. As for \fIselect\fR the table is traversed by calling \fIets:select/3\fR and \fIets:select/1\fR\&. The difference is that the match_spec is explicitly given\&. This is how to state match_specs that cannot easily be expressed within the syntax provided by QLC\&.
.RE
.LP
The following example uses an explicit match_spec to traverse the table:
.nf
9> ets:insert(Tab = ets:new(t, []), [{1,a},{2,b},{3,c},{4,d}]),
MS = ets:fun2ms(fun({X,Y}) when (X > 1) or (X < 5) -> {Y} end),
QH1 = ets:table(Tab, [{traverse, {select, MS}}])\&.
.fi
.LP
An example with implicit match_spec:
.nf
10> QH2 = qlc:q([{Y} || {X,Y} <- ets:table(Tab), (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
11> 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
test_ms(Tuple, MatchSpec) -> {ok, Result} | {error, Errors}
.br
.RS
.TP
Types
Tuple = tuple()
.br
MatchSpec = match_spec()
.br
Result = term()
.br
Errors = [{warning|error, string()}]
.br
.RE
.RS
.LP
This function is a utility to test a match_spec used in calls to \fIets:select/2\fR\&. The function both tests \fIMatchSpec\fR for "syntactic" correctness and runs the match_spec against the object \fITuple\fR\&. If the match_spec contains errors, the tuple \fI{error, Errors}\fR is returned where \fIErrors\fR is a list of natural language descriptions of what was wrong with the match_spec\&. If the match_spec is syntactically OK, the function returns \fI{ok, Term}\fR where \fITerm\fR is what would have been the result in a real \fIets:select/2\fR call or \fIfalse\fR if the match_spec does not match the object \fITuple\fR\&.
.LP
This is a useful debugging and test tool, especially when writing complicated \fIets:select/2\fR calls\&.
.RE
.LP
.B
to_dets(Tab, DetsTab) -> Tab
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
DetsTab = atom()
.br
.RE
.RS
.LP
Fills an already created/opened Dets table with the objects in the already opened ETS table named \fITab\fR\&. The Dets table is emptied before the objects are inserted\&.
.RE
.LP
.B
update_counter(Tab, Key, UpdateOp) -> Result
.br
.B
update_counter(Tab, Key, [UpdateOp]) -> [Result]
.br
.B
update_counter(Tab, Key, Incr) -> Result
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Key = term()
.br
UpdateOp = {Pos, Incr} | {Pos, Incr, Threshold, SetValue}
.br
Pos = Incr = Threshold = SetValue = Result = int()
.br
.RE
.RS
.LP
This function provides an efficient way to update one or more counters, without the hassle of having to look up an object, update the object by incrementing an element and insert the resulting object into the table again\&. (The update is done atomically; i\&.e\&. no process can access the ets table in the middle of the operation\&.)
.LP
It will destructively update the object with key \fIKey\fR in the table \fITab\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 (\fI<keypos>+1\fR) is updated\&.
.LP
If a \fIThreshold\fR is specified, the counter will be reset to the value \fISetValue\fR if the following conditions occur:
.RS 2
.TP 2
*
The \fIIncr\fR is not negative (\fI>= 0\fR) and the result would be greater than (\fI>\fR) \fIThreshold\fR
.TP 2
*
The \fIIncr\fR is negative (\fI< 0\fR) and the result would be less than (\fI<\fR) \fIThreshold\fR
.RE
.LP
A list of \fIUpdateOp\fR can be supplied to do several update operations within the object\&. The operations are carried out in the order specified in the list\&. If the same counter position occurs more than one time in the list, the corresponding counter will thus be updated several times, each time based on the previous result\&. The return value is a list of the new counter values from each update operation in the same order as in the operation list\&. If an empty list is specified, nothing is updated and an empty list is returned\&. If the function should fail, no updates will be done at all\&.
.LP
The given Key is used to identify the object by either \fImatching\fR the key of an object in a \fIset\fR table, or \fIcompare equal\fR to the key of an object in an \fIordered_set\fR table (see lookup/2 and new/2 for details on the difference)\&.
.LP
The function will fail with reason \fIbadarg\fR if:
.RS 2
.TP 2
*
the table is not of type \fIset\fR or \fIordered_set\fR,
.TP 2
*
no object with the right key exists,
.TP 2
*
the object has the wrong arity,
.TP 2
*
the element to update is not an integer,
.TP 2
*
the element to update is also the key, or,
.TP 2
*
any of \fIPos\fR, \fIIncr\fR, \fIThreshold\fR or \fISetValue\fR is not an integer
.RE
.RE
.LP
.B
update_element(Tab, Key, {Pos,Value}) -> true | false
.br
.B
update_element(Tab, Key, [{Pos,Value}]) -> true | false
.br
.RS
.TP
Types
Tab = tid() | atom()
.br
Key = Value = term()
.br
Pos = int()
.br
.RE
.RS
.LP
This function provides an efficient way to update one or more elements within an object, without the hassle of having to look up, update and write back the entire object\&.
.LP
It will destructively update the object with key \fIKey\fR in the table \fITab\fR\&. The element at the \fIPos\fR:th position will be given the value \fIValue\fR\&.
.LP
A list of \fI{Pos, Value}\fR can be supplied to update several elements within the same object\&. If the same position occurs more than one in the list, the last value in the list will be written\&. If the list is empty or the function fails, no updates will be done at all\&. The function is also atomic in the sense that other processes can never see any intermediate results\&.
.LP
The function returns \fItrue\fR if an object with the key \fIKey\fR was found, \fIfalse\fR otherwise\&.
.LP
The given Key is used to identify the object by either \fImatching\fR the key of an object in a \fIset\fR table, or \fIcompare equal\fR to the key of an object in an \fIordered_set\fR table (see lookup/2 and new/2 for details on the difference)\&.
.LP
The function will fail with reason \fIbadarg\fR if:
.RS 2
.TP 2
*
the table is not of type \fIset\fR or \fIordered_set\fR,
.TP 2
*
\fIPos\fR is less than 1 or greater than the object arity, or,
.TP 2
*
the element to update is also the key
.RE
.RE
|