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
|
.TH qlc 3 "stdlib 1.15.3" "Ericsson AB" "ERLANG MODULE DEFINITION"
.SH MODULE
qlc \- Query Interface to Mnesia, ETS, Dets, etc
.SH DESCRIPTION
.LP
The \fIqlc\fR module provides a query interface to Mnesia, ETS, Dets and other data structures that implement an iterator style traversal of objects\&.
.SH OVERVIEW
.LP
The \fIqlc\fR module implements a query interface to \fIQLC tables\fR\&. Typical QLC tables are ETS, Dets, and Mnesia tables\&. There is also support for user defined tables, see the Implementing a QLC table section\&. A \fIquery\fR is stated using \fIQuery List Comprehensions\fR (QLCs)\&. The answers to a query are determined by data in QLC tables that fulfill the constraints expressed by the QLCs of the query\&. QLCs are similar to ordinary list comprehensions as described in the Erlang Reference Manual and Programming Examples except that variables introduced in patterns cannot be used in list expressions\&. In fact, in the absence of optimizations and options such as \fIcache\fR and \fIunique\fR (see below), every QLC free of QLC tables evaluates to the same list of answers as the identical ordinary list comprehension\&.
.LP
While ordinary list comprehensions evaluate to lists, calling qlc:q/1,2 returns a \fIQuery Handle\fR\&. To obtain all the answers to a query, qlc:eval/1,2 should be called with the query handle as first argument\&. Query handles are essentially functional objects ("funs") created in the module calling \fIq/1, 2\fR\&. As the funs refer to the module\&'s code, one should be careful not to keep query handles too long if the module\&'s code is to be replaced\&. Code replacement is described in the Erlang Reference Manual\&. The list of answers can also be traversed in chunks by use of a \fIQuery Cursor\fR\&. Query cursors are created by calling qlc:cursor/1,2 with a query handle as first argument\&. Query cursors are essentially Erlang processes\&. One answer at a time is sent from the query cursor process to the process that created the cursor\&.
.SH SYNTAX
.LP
Syntactically QLCs have the same parts as ordinary list comprehensions:
.nf
[Expression || Qualifier1, Qualifier2, \&.\&.\&.]
.fi
.LP
\fIExpression\fR (the \fItemplate\fR) is an arbitrary Erlang expression\&. Qualifiers are either \fIfilters\fR or \fIgenerators\fR\&. Filters are Erlang expressions returning \fIbool()\fR\&. Generators have the form \fIPattern <- ListExpression\fR, where \fIListExpression\fR is an expression evaluating to a query handle or a list\&. Query handles are returned from \fIqlc:table/2\fR, \fIqlc:append/1, 2\fR, \fIqlc:sort/1, 2\fR, \fIqlc:keysort/2, 3\fR, \fIqlc:q/1, 2\fR, and \fIqlc:string_to_handle/1, 2, 3\fR\&.
.SH EVALUATION
.LP
The evaluation of a query handle begins by the inspection of options and the collection of information about tables\&. As a result qualifiers are modified during the optimization phase\&. Next all list expressions are evaluated\&. If a cursor has been created evaluation takes place in the cursor process\&. For those list expressions that are QLCs, the list expressions of the QLCs\&' generators are evaluated as well\&. One has to be careful if list expressions have side effects since the order in which list expressions are evaluated is unspecified\&. Finally the answers are found by evaluating the qualifiers from left to right, backtracking when some filter returns \fIfalse\fR, or collecting the template when all filters return \fItrue\fR\&.
.LP
Filters that do not return \fIbool()\fR but fail are handled differently depending on their syntax: if the filter is a guard it returns \fIfalse\fR, otherwise the query evaluation fails\&. This behavior makes it possible for the \fIqlc\fR module to do some optimizations without affecting the meaning of a query\&. For example, when some position of a table is compared to one or more constants, only the objects with matching values are candidates for further evaluation\&. The other objects are guaranteed to make the filter return \fIfalse\fR, but never fail\&. The (small) set of candidate objects can often be found by looking up some key values of the table or by traversing the table using a match specification\&. It is necessary to place the guard filters immediately after the table\&'s generator, otherwise the candidate objects will not be restricted to a small set\&. The reason is that objects that could make the query evaluation fail must not be excluded by looking up a key or running a match specification\&.
.SH JOIN
.LP
The \fIqlc\fR module supports fast join of two query handles\&. Fast join is possible if some position (\fIP1\fR) of one query handler is compared to or matched against some position (\fIP2\fR) of another query handle\&. Two fast join methods have been implemented:
.RS 2
.TP 2
*
Lookup join traverses all objects of one query handle and finds objects of the other handle (a QLC table) such that the values at \fIP1\fR and \fIP2\fR match\&. The \fIqlc\fR module does not create any indices but looks up values using the key position and the indexed positions of the QLC table\&.
.TP 2
*
Merge join sorts the objects of each query handle if necessary and filters out objects where the values at \fIP1\fR and \fIP2\fR do not compare equal\&. If there are many objects with the same value of \fIP2\fR a temporary file will be used for the equivalence classes\&.
.RE
.LP
The \fIqlc\fR module warns at compile time if a QLC combines query handles in such a way that more than one join is possible\&. In other words, there is no query planner that can choose a good order between possible join operations\&. It is up to the user to order the joins by introducing query handles\&.
.LP
The join is to be expressed as a guard filter\&. The filter must be placed immediately after the two joined generators, possibly after guard filters that use variables from no other generators but the two joined generators\&. The \fIqlc\fR module inspects the operands of \fI=:=/2\fR, \fI==/2\fR, \fIis_record/2\fR, \fIelement/2\fR, and logical operators (\fIand/2\fR, \fIor/2\fR, \fIandalso/2\fR, \fIorelse/2\fR, \fIxor/2\fR) when determining which joins to consider\&.
.SH COMMON OPTIONS
.LP
The following options are accepted by \fIcursor/2\fR, \fIeval/2\fR, \fIfold/4\fR, and \fIinfo/2\fR:
.RS 2
.TP 2
*
\fI{cache_all, Cache}\fR where \fICache\fR is equal to \fIets\fR or \fIlist\fR adds a \fI{cache, Cache}\fR option to every list expression of the query except tables and lists\&. Default is \fI{cache_all, no}\fR\&. The option \fIcache_all\fR is equivalent to \fI{cache_all, ets}\fR\&.
.TP 2
*
\fI{max_list_size, MaxListSize}\fR where \fIMaxListSize\fR is the size in bytes of terms on the external format\&. If the accumulated size of collected objects exceeds \fIMaxListSize\fR the objects are written onto a temporary file\&. This option is used by the \fI{cache, list}\fR option as well as by the merge join method\&. Default is 512*1024 bytes\&.
.TP 2
*
\fI{tmpdir_usage, TmpFileUsage}\fR determines the action taken when \fIqlc\fR is about to create temporary files on the directory set by the \fItmpdir\fR option\&. If the value is \fInot_allowed\fR an error tuple is returned, otherwise temporary files are created as needed\&. Default is \fIallowed\fR which means that no further action is taken\&. The values \fIinfo_msg\fR, \fIwarning_msg\fR, and \fIerror_msg\fR mean that the function with the corresponding name in the module \fIerror_logger\fR is called for printing some information (currently the stacktrace)\&.
.TP 2
*
\fI{tmpdir, TempDirectory}\fR sets the directory used by merge join for temporary files and by the \fI{cache, list}\fR option\&. The option also overrides the \fItmpdir\fR option of \fIkeysort/3\fR and \fIsort/2\fR\&. The default value is \fI""\fR which means that the directory returned by \fIfile:get_cwd()\fR is used\&.
.TP 2
*
\fI{unique_all, true}\fR adds a \fI{unique, true}\fR option to every list expression of the query\&. Default is \fI{unique_all, false}\fR\&. The option \fIunique_all\fR is equivalent to \fI{unique_all, true}\fR\&.
.RE
.SH COMMON DATA TYPES
.RS 2
.TP 2
*
\fIQueryCursor = {qlc_cursor, term()}\fR
.TP 2
*
\fIQueryHandle = {qlc_handle, term()}\fR
.TP 2
*
\fIQueryHandleOrList = QueryHandle | list()\fR
.TP 2
*
\fIAnswers = [Answer]\fR
.TP 2
*
\fIAnswer = term()\fR
.TP 2
*
\fIAbstractExpression =\fR -parse trees for Erlang expressions, see the abstract format documentation in the ERTS User\&'s Guide-
.TP 2
*
\fIMatchExpression =\fR -matchspecifications, see the match specification documentation in the ERTS User\&'s Guide and ms_transform(3)-
.TP 2
*
\fISpawnOptions = default | spawn_options()\fR
.TP 2
*
\fISortOptions = [SortOption] | SortOption\fR
.TP 2
*
\fISortOption = {compressed, bool()} | {no_files, NoFiles} | {order, Order} | {size, Size} | {tmpdir, TempDirectory} | {unique, bool()}\fR -see file_sorter(3)-
.TP 2
*
\fIOrder = ascending | descending | OrderFun\fR
.TP 2
*
\fIOrderFun = fun(term(), term()) -> bool()\fR
.TP 2
*
\fITempDirectory = "" | filename()\fR
.TP 2
*
\fISize = int() > 0\fR
.TP 2
*
\fINoFiles = int() > 1\fR
.TP 2
*
\fIKeyPos = int() > 0 | [int() > 0]\fR
.TP 2
*
\fIMaxListSize = int() >= 0\fR
.TP 2
*
\fIbool() = true | false\fR
.TP 2
*
\fICache = ets | list | no\fR
.TP 2
*
\fITmpFileUsage = allowed | not_allowed | info_msg | warning_msg | error_msg\fR
.TP 2
*
\fIfilename() =\fR -see filename(3)-
.TP 2
*
\fIspawn_options() =\fR -see erlang(3)-
.RE
.SH GETTING STARTED
.LP
As already mentioned queries are stated in the list comprehension syntax as described in the Erlang Reference Manual\&. In the following some familiarity with list comprehensions is assumed\&. There are examples in Programming Examples that can get you started\&. It should be stressed that list comprehensions do not add any computational power to the language; anything that can be done with list comprehensions can also be done without them\&. But they add a syntax for expressing simple search problems which is compact and clear once you get used to it\&.
.LP
Many list comprehension expressions can be evaluated by the \fIqlc\fR module\&. Exceptions are expressions such that variables introduced in patterns (or filters) are used in some generator later in the list comprehension\&. As an example consider an implementation of lists:append(L): \fI[X ||Y <- L, X <- Y]\fR\&. Y is introduced in the first generator and used in the second\&. The ordinary list comprehension is normally to be preferred when there is a choice as to which to use\&. One difference is that \fIqlc:eval/1, 2\fR collects answers in a list which is finally reversed, while list comprehensions collect answers on the stack which is finally unwound\&.
.LP
What the \fIqlc\fR module primarily adds to list comprehensions is that data can be read from QLC tables in small chunks\&. A QLC table is created by calling \fIqlc:table/2\fR\&. Usually \fIqlc:table/2\fR is not called directly from the query but via an interface function of some data structure\&. There are a few examples of such functions in Erlang/OTP: \fImnesia:table/1, 2\fR, \fIets:table/1, 2\fR, and \fIdets:table/1, 2\fR\&. For a given data structure there can be several functions that create QLC tables, but common for all these functions is that they return a query handle created by \fIqlc:table/2\fR\&. Using the QLC tables provided by OTP is probably sufficient in most cases, but for the more advanced user the section Implementing a QLC table describes the implementation of a function calling \fIqlc:table/2\fR\&.
.LP
Besides \fIqlc:table/2\fR there are other functions that return query handles\&. They might not be used as often as tables, but are useful from time to time\&. \fIqlc:append\fR traverses objects from several tables or lists after each other\&. If, for instance, you want to traverse all answers to a query QH and then finish off by a term \fI{finished}\fR, you can do that by calling \fIqlc:append(QH, [{finished}])\fR\&. \fIappend\fR first returns all objects of QH, then \fI{finished}\fR\&. If there is one tuple \fI{finished}\fR among the answers to QH it will be returned twice from \fIappend\fR\&.
.LP
As another example, consider concatenating the answers to two queries QH1 and QH2 while removing all duplicates\&. The means to accomplish this is to use the \fIunique\fR option:
.nf
qlc:q([X || X <- qlc:append(QH1, QH2)], {unique, true})
.fi
.LP
The cost is substantial: every returned answer will be stored in an ETS table\&. Before returning an answer it is looked up in the ETS table to check if it has already been returned\&. Without the \fIunique\fR options all answers to QH1 would be returned followed by all answers to QH2\&. The \fIunique\fR options keeps the order between the remaining answers\&.
.LP
If the order of the answers is not important there is the alternative to sort the answers uniquely:
.nf
qlc:sort(qlc:q([X || X <- qlc:append(QH1, QH2)], {unique, true}))\&.
.fi
.LP
This query also removes duplicates but the answers will be sorted\&. If there are many answers temporary files will be used\&. Note that in order to get the first unique answer all answers have to be found and sorted\&.
.LP
To return just a few answers cursors can be used\&. The following code returns no more than five answers using an ETS table for storing the unique answers:
.nf
C = qlc:cursor(qlc:q([X || X <- qlc:append(QH1, QH2)],{unique,true})),
R = qlc:next_answers(C, 5),
ok = qlc:delete_cursor(C),
R\&.
.fi
.LP
Query list comprehensions are convenient for stating constraints on data from two or more tables\&. An example that does a natural join on two query handles on position 2:
.nf
qlc:q([{X1,X2,X3,Y1} ||
{X1,X2,X3} <- QH1,
{Y1,Y2} <- QH2,
X2 =:= Y2])
.fi
.LP
The \fIqlc\fR module will evaluate this differently depending on the query handles \fIQH1\fR and \fIQH2\fR\&. If, for example, \fIX2\fR is matched against the key of a QLC table the lookup join method will traverse the objects of \fIQH2\fR while looking up key values in the table\&. On the other hand, if neither \fIX2\fR nor \fIY2\fR is matched against the key or an indexed position of a QLC table, the merge join method will make sure that \fIQH1\fR and \fIQH2\fR are both sorted on position 2 and next do the join by traversing the objects one by one\&.
.LP
The \fIjoin\fR option can be used to force the \fIqlc\fR module to use a certain join method\&. For the rest of this section it is assumed that the excessively slow join method called "nested loop" has been chosen:
.nf
qlc:q([{X1,X2,X3,Y1} ||
{X1,X2,X3} <- QH1,
{Y1,Y2} <- QH2,
X2 =:= Y2],
{join, nested_loop})
.fi
.LP
In this case the filter will be applied to every possible pair of answers to QH1 and QH2, one at a time\&. If there are M answers to QH1 and N answers to QH2 the filter will be run M*N times\&.
.LP
If QH2 is a call to the function for \fIgb_trees\fR as defined in the Implementing a QLC table section, \fIgb_table:table/1\fR, the iterator for the gb-tree will be initiated for each answer to QH1 after which the objects of the gb-tree will be returned one by one\&. This is probably the most efficient way of traversing the table in that case since it takes minimal computational power to get the following object\&. But if QH2 is not a table but a more complicated QLC, it can be more efficient use some RAM memory for collecting the answers in a cache, particularly if there are only a few answers\&. It must then be assumed that evaluating QH2 has no side effects so that the meaning of the query does not change if QH2 is evaluated only once\&. One way of caching the answers is to evaluate QH2 first of all and substitute the list of answers for QH2 in the query\&. Another way is to use the \fIcache\fR option\&. It is stated like this:
.nf
QH2\&' = qlc:q([X || X <- QH2], {cache, ets})
.fi
.LP
or just
.nf
QH2\&' = qlc:q([X || X <- QH2], cache)
.fi
.LP
The effect of the \fIcache\fR option is that when the generator QH2\&' is run the first time every answer is stored in an ETS table\&. When next answer of QH1 is tried, answers to QH2\&' are copied from the ETS table which is very fast\&. As for the \fIunique\fR option the cost is a possibly substantial amount of RAM memory\&. The \fI{cache, list}\fR option offers the possibility to store the answers in a list on the process heap\&. While this has the potential of being faster than ETS tables since there is no need to copy answers from the table it can often result in slower evaluation due to more garbage collections of the process\&' heap as well as increased RAM memory consumption due to larger heaps\&. Another drawback with cache lists is that if the size of the list exceeds a limit a temporary file will be used\&. Reading the answers from a file is very much slower than copying them from an ETS table\&. But if the available RAM memory is scarce setting the limit to some low value is an alternative\&.
.LP
There is an option \fIcache_all\fR that can be set to \fIets\fR or \fIlist\fR when evaluating a query\&. It adds a \fIcache\fR or \fI{cache, list}\fR option to every list expression except QLC tables and lists on all levels of the query\&. This can be used for testing if caching would improve efficiency at all\&. If the answer is yes further testing is needed to pinpoint the generators that should be cached\&.
.SH IMPLEMENTING A QLC TABLE
.LP
As an example of how to use the qlc:table/2 function the implementation of a QLC table for the gb_trees module is given:
.nf
-module(gb_table)\&.
-export([table/1])\&.
table(T) ->
TF = fun() -> qlc_next(gb_trees:next(gb_trees:iterator(T))) end,
InfoFun = fun(num_of_objects) -> gb_trees:size(T);
(keypos) -> 1;
(_) -> undefined
end,
LookupFun =
fun(1, Ks) ->
lists:flatmap(fun(K) ->
case gb_trees:lookup(K, T) of
{value, V} -> [{K,V}];
none -> []
end
end, Ks)
end,
FormatFun =
fun({all, NElements, ElementFun}) ->
Vals = gb_nodes(T, NElements, ElementFun),
{gb_trees, from_orddict, [Vals]};
({lookup, 1, KeyValues, _NElements, ElementFun}) ->
ValsS = io_lib:format("gb_trees:from_orddict(~w)",
[gb_nodes(T, infinity, ElementFun)]),
io_lib:format("lists:flatmap(fun(K) -> "
"case gb_trees:lookup(K, ~s) of "
"{value, V} -> [{K,V}];none -> [] end "
"end, ~w)",
[ValsS, [ElementFun(KV) || KV <- KeyValues]])
end,
qlc:table(TF, [{info_fun, InfoFun}, {format_fun, FormatFun},
{lookup_fun, LookupFun}])\&.
qlc_next({X, V, S}) ->
[{X,V} | fun() -> qlc_next(gb_trees:next(S)) end];
qlc_next(none) ->
[]\&.
gb_nodes(T, infinity, ElementFun) ->
gb_nodes(T, -1, ElementFun);
gb_nodes(T, NElements, ElementFun) ->
gb_iter(gb_trees:iterator(T), NElements, ElementFun)\&.
gb_iter(_I, 0, _EFun) ->
\&'\&.\&.\&.\&';
gb_iter(I0, N, EFun) ->
case gb_trees:next(I0) of
{X, V, I} ->
[EFun({X,V}) | gb_iter(I, N-1, EFun)];
none ->
[]
end\&.
.fi
.LP
\fITF\fR is the traversal function\&. The \fIqlc\fR module requires that there is a way of traversing all objects of the data structure; in \fIgb_trees\fR there is an iterator function suitable for that purpose\&. Note that for each object returned a new fun is created\&. As long as the list is not terminated by \fI[]\fR it is assumed that the tail of the list is a nullary function and that calling the function returns further objects (and functions)\&.
.LP
The lookup function is optional\&. It is assumed that the lookup function always finds values much faster than it would take to traverse the table\&. The first argument is the position of the key\&. Since \fIqlc_next\fR returns the objects as {Key,Value} pairs the position is 1\&. Note that the lookup function should return {Key,Value} pairs, just as the traversal function does\&.
.LP
The format function is also optional\&. It is called by \fIqlc:info\fR to give feedback at runtime of how the query will be evaluated\&. One should try to give as good feedback as possible without showing too much details\&. In the example at most 7 objects of the table are shown\&. The format function handles two cases: \fIall\fR means that all objects of the table will be traversed; \fI{lookup, 1, KeyValues}\fR means that the lookup function will be used for looking up key values\&.
.LP
Whether the whole table will be traversed or just some keys looked up depends on how the query is stated\&. If the query has the form
.nf
qlc:q([T || P <- LE, F])
.fi
.LP
and P is a tuple, the \fIqlc\fR module analyzes P and F in compile time to find positions of the tuple P that are matched or compared to constants\&. If such a position at runtime turns out to be the key position, the lookup function can be used, otherwise all objects of the table have to be traversed\&. It is the info function \fIInfoFun\fR that returns the key position\&. There can be indexed positions as well, also returned by the info function\&. An index is an extra table that makes lookup on some position fast\&. Mnesia maintains indices upon request, thereby introducing so called secondary keys\&. The \fIqlc\fR module prefers to look up objects using the key before secondary keys regardless of the number of constants to look up\&.
.SH EXPORTS
.LP
.B
append(QHL) -> QH
.br
.RS
.TP
Types
QHL = [QueryHandleOrList]
.br
QH = QueryHandle
.br
.RE
.RS
.LP
Returns a query handle\&. When evaluating the query handle \fIQH\fR all answers to the first query handle in \fIQHL\fR is returned followed by all answers to the rest of the query handles in \fIQHL\fR\&.
.RE
.LP
.B
append(QH1, QH2) -> QH3
.br
.RS
.TP
Types
QH1 = QH2 = QueryHandleOrList
.br
QH3 = QueryHandle
.br
.RE
.RS
.LP
Returns a query handle\&. When evaluating the query handle \fIQH3\fR all answers to \fIQH1\fR are returned followed by all answers to \fIQH2\fR\&.
.LP
\fIappend(QH1, QH2)\fR is equivalent to \fIappend([QH1, QH2])\fR\&.
.RE
.LP
.B
cursor(QueryHandleOrList [, Options]) -> QueryCursor
.br
.RS
.TP
Types
Options = [Option] | Option
.br
Option = {cache_all, Cache} | cache_all | {max_list_size, MaxListSize} | {spawn_options, SpawnOptions} | {tmpdir_usage, TmpFileUsage} | {tmpdir, TempDirectory} | {unique_all, bool()} | unique_all
.br
.RE
.RS
.LP
Creates a query cursor and makes the calling process the owner of the cursor\&. The cursor is to be used as argument to \fInext_answers/1, 2\fR and (eventually) \fIdelete_cursor/1\fR\&. Calls \fIerlang:spawn_opt\fR to spawn and link a process which will evaluate the query handle\&. The value of the option \fIspawn_options\fR is used as last argument when calling \fIspawn_opt\fR\&. The default value is \fI[link]\fR\&.
.nf
1> QH = qlc:q([{X,Y} || X <- [a,b], Y <- [1,2]]),
QC = qlc:cursor(QH),
qlc:next_answers(QC, 1)\&.
[{a,1}]
2> qlc:next_answers(QC, 1)\&.
[{a,2}]
3> qlc:next_answers(QC, all_remaining)\&.
[{b,1},{b,2}]
4> qlc:delete_cursor(QC)\&.
ok
.fi
.RE
.LP
.B
delete_cursor(QueryCursor) -> ok
.br
.RS
.LP
Deletes a query cursor\&. Only the owner of the cursor can delete the cursor\&.
.RE
.LP
.B
eval(QueryHandleOrList [, Options]) -> Answers | Error
.br
.B
e(QueryHandleOrList [, Options]) -> Answers
.br
.RS
.TP
Types
Options = [Option] | Option
.br
Option = {cache_all, Cache} | cache_all | {max_list_size, MaxListSize} | {tmpdir_usage, TmpFileUsage} | {tmpdir, TempDirectory} | {unique_all, bool()} | unique_all
.br
Error = {error, module(), Reason}
.br
Reason =-as returned by file_sorter(3)-
.br
.RE
.RS
.LP
Evaluates a query handle in the calling process and collects all answers in a list\&.
.nf
1> QH = qlc:q([{X,Y} || X <- [a,b], Y <- [1,2]]),
qlc:eval(QH)\&.
[{a,1},{a,2},{b,1},{b,2}]
.fi
.RE
.LP
.B
fold(Function, Acc0, QueryHandleOrList [, Options]) -> Acc1 | Error
.br
.RS
.TP
Types
Function = fun(Answer, AccIn) -> AccOut
.br
Acc0 = Acc1 = AccIn = AccOut = term()
.br
Options = [Option] | Option
.br
Option = {cache_all, Cache} | cache_all | {max_list_size, MaxListSize} | {tmpdir_usage, TmpFileUsage} | {tmpdir, TempDirectory} | {unique_all, bool()} | unique_all
.br
Error = {error, module(), Reason}
.br
Reason =-as returned by file_sorter(3)-
.br
.RE
.RS
.LP
Calls \fIFunction\fR on successive answers to the query handle together with an extra argument \fIAccIn\fR\&. The query handle and the function are evaluated in the calling process\&. \fIFunction\fR must return a new accumulator which is passed to the next call\&. \fIAcc0\fR is returned if there are no answers to the query handle\&.
.nf
1> QH = [1,2,3,4,5,6],
qlc:fold(fun(X, Sum) -> X + Sum end, 0, QH)\&.
21
.fi
.RE
.LP
.B
format_error(Error) -> Chars
.br
.RS
.TP
Types
Error = {error, module(), term()}
.br
Chars = [char() | Chars]
.br
.RE
.RS
.LP
Returns a descriptive string in English of an error tuple returned by some of the functions of the \fIqlc\fR module or the parse transform\&. This function is mainly used by the compiler invoking the parse transform\&.
.RE
.LP
.B
info(QueryHandleOrList [, Options]) -> Info
.br
.RS
.TP
Types
Options = [Option] | Option
.br
Option = EvalOption | ReturnOption
.br
EvalOption = {cache_all, Cache} | cache_all | {max_list_size, MaxListSize} | {tmpdir_usage, TmpFileUsage} | {tmpdir, TempDirectory} | {unique_all, bool()} | unique_all
.br
ReturnOption = {depth, Depth} | {flat, bool()} | {format, Format} | {n_elements, NElements}
.br
Depth = infinity | int() >= 0
.br
Format = abstract_code | string
.br
NElements = infinity | int() > 0
.br
Info = AbstractExpression | string()
.br
.RE
.RS
.LP
Returns information about a query handle\&. The information describes the simplifications and optimizations that are the results of preparing the query for evaluation\&. This function is probably useful mostly during debugging\&.
.LP
The information has the form of an Erlang expression where QLCs most likely occur\&. Depending on the format functions of mentioned QLC tables it may not be absolutely accurate\&.
.LP
The default is to return a sequence of QLCs in a block, but if the option \fI{flat, false}\fR is given, one single QLC is returned\&. The default is to return a string, but if the option \fI{format, abstract_code}\fR is given, abstract code is returned instead\&. In the abstract code port identifiers, references, and pids are represented by strings\&. The default is to return all elements in lists, but if the \fI{n_elements, NElements}\fR option is given, only a limited number of elements are returned\&. The default is to show all of objects and match specifications, but if the \fI{depth, Depth}\fR option is given, parts of terms below a certain depth are replaced by \fI\&'\&.\&.\&.\&'\fR\&.
.nf
1> QH = qlc:q([{X,Y} || X <- [x,y], Y <- [a,b]]),
io:format("~s~n", [qlc:info(QH, unique_all)])\&.
begin
V1 =
qlc:q([SQV ||
SQV <- [x,y]],
[{unique,true}]),
V2 =
qlc:q([SQV ||
SQV <- [a,b]],
[{unique,true}]),
qlc:q([{X,Y} ||
X <- V1,
Y <- V2],
[{unique,true}])
end
.fi
.LP
In this example two simple QLCs have been inserted just to hold the \fI{unique, true}\fR option\&.
.nf
1> E1 = ets:new(e1, []),
E2 = ets:new(e2, []),
true = ets:insert(E1, [{1,a},{2,b}]),
true = ets:insert(E2, [{a,1},{b,2}]),
Q = qlc:q([{X,Z,W} ||
{X, Z} <- ets:table(E1),
{W, Y} <- ets:table(E2),
X =:= Y]),
io:format("~s~n", [qlc:info(Q)])\&.
begin
V1 =
qlc:q([P0 ||
P0 = {W,Y} <- ets:table(17)]),
V2 =
qlc:q([[G1|G2] ||
G2 <- V1,
G1 <- ets:table(16),
element(2, G1) =:= element(1, G2)],
[{join,lookup}]),
qlc:q([{X,Z,W} ||
[{X,Z}|{W,Y}] <- V2,
X =:= Y])
end
.fi
.LP
In this example the query list comprehension \fIV2\fR has been inserted to show the joined generators and the join method chosen\&. A convention is used for lookup join: the first generator (\fIG2\fR) is the one traversed, the second one (\fIG1\fR) is the table where constants are looked up\&.
.RE
.LP
.B
keysort(KeyPos, QH1 [, SortOptions]) -> QH2
.br
.RS
.TP
Types
QH1 = QueryHandleOrList
.br
QH2 = QueryHandle
.br
.RE
.RS
.LP
Returns a query handle\&. When evaluating the query handle \fIQH2\fR the answers to the query handle \fIQH1\fR are sorted by file_sorter:keysort/4 according to the options\&.
.LP
The sorter will use temporary files only if \fIQH1\fR does not evaluate to a list and the size of the binary representation of the answers exceeds \fISize\fR bytes, where \fISize\fR is the value of the \fIsize\fR option\&.
.RE
.LP
.B
next_answers(QueryCursor [, NumberOfAnswers]) -> Answers | Error
.br
.RS
.TP
Types
NumberOfAnswers = all_remaining | int() > 0
.br
Error = {error, module(), Reason}
.br
Reason =-as returned by file_sorter(3)-
.br
.RE
.RS
.LP
Returns some or all of the remaining answers to a query cursor\&. Only the owner of \fICursor\fR can retrieve answers\&.
.LP
The optional argument \fINumberOfAnswers\fRdetermines the maximum number of answers returned\&. The default value is \fI10\fR\&. If less than the requested number of answers is returned, subsequent calls to \fInext_answers\fR will return \fI[]\fR\&.
.RE
.LP
.B
q(QueryListComprehension [, Options]) -> QueryHandle
.br
.RS
.TP
Types
QueryListComprehension = -literal query listcomprehension-
.br
Options = [Option] | Option
.br
Option = {max_lookup, MaxLookup} | {cache, Cache} | cache | {join, Join} | {lookup, Lookup} | {unique, bool()} | unique
.br
MaxLookup = int() >= 0 | infinity
.br
Join = any | lookup | merge | nested_loop
.br
Lookup = bool() | any
.br
.RE
.RS
.LP
Returns a query handle for a query list comprehension\&. The query list comprehension must be the first argument to \fIqlc:q/1, 2\fR or it will be evaluated as an ordinary list comprehension\&. It is also necessary to add the line
.nf
-include_lib("stdlib/include/qlc\&.hrl")\&.
.fi
.LP
to the source file\&. This causes a parse transform to substitute a fun for the query list comprehension\&. The (compiled) fun will be called when the query handle is evaluated\&.
.LP
When calling \fIqlc:q/1, 2\fR from the Erlang shell the parse transform is automatically called\&. When this happens the fun substituted for the query list comprehension is not compiled but will be evaluated by \fIerl_eval(3)\fR\&. This is also true when expressions are evaluated by means of \fIfile:eval/1, 2\fR or in the debugger\&.
.LP
To be very explicit, this will not work:
.nf
\&.\&.\&.
A = [X || {X} <- [{1},{2}]],
QH = qlc:q(A),
\&.\&.\&.
.fi
.LP
The variable \fIA\fR will be bound to the evaluated value of the list comprehension (\fI[1, 2]\fR)\&. The compiler complains with an error message ("argument is not a query list comprehension"); the shell process stops with a \fIbadarg\fR reason\&.
.LP
The \fI{cache, ets}\fR option can be used to cache the answers to a query list comprehension\&. The answers are stored in one ETS table for each cached query list comprehension\&. When a cached query list comprehension is evaluated again, answers are fetched from the table without any further computations\&. As a consequence, when all answers to a cached query list comprehension have been found, the ETS tables used for caching answers to the query list comprehension\&'s qualifiers can be emptied\&. The option \fIcache\fR is equivalent to \fI{cache, ets}\fR\&.
.LP
The \fI{cache, list}\fR option can be used to cache the answers to a query list comprehension just like \fI{cache, ets}\fR\&. The difference is that the answers are kept in a list (on the process heap)\&. If the answers would occupy more than a certain amount of RAM memory a temporary file is used for storing the answers\&. The option \fImax_list_size\fR sets the limit in bytes and the temporary file is put on the directory set by the \fItmpdir\fR option\&.
.LP
The \fIcache\fR option has no effect if it is known that the query list comprehension will be evaluated at most once\&. This is always true for the top-most query list comprehension and also for the list expression of the first generator in a list of qualifiers\&. Note that in the presence of side effects in filters or callback functions the answers to query list comprehensions can be affected by the \fIcache\fR option\&.
.LP
The \fI{unique, true}\fR option can be used to remove duplicate answers to a query list comprehension\&. The unique answers are stored in one ETS table for each query list comprehension\&. The table is emptied every time it is known that there are no more answers to the query list comprehension\&. The option \fIunique\fR is equivalent to \fI{unique, true}\fR\&. If the \fIunique\fR option is combined with the \fI{cache, ets}\fR option, two ETS tables are used, but the full answers are stored in one table only\&. If the \fIunique\fR option is combined with the \fI{cache, list}\fR option the answers are sorted twice using \fIkeysort/3\fR; once to remove duplicates, and once to restore the order\&.
.LP
The \fIcache\fR and \fIunique\fR options apply not only to the query list comprehension itself but also to the results of looking up constants, running match specifications, and joining handles\&.
.nf
1> Q = qlc:q([{A,X,Z,W} ||
A <- [a,b,c],
{X,Z} <- [{a,1},{b,4},{c,6}],
{W,Y} <- [{2,a},{3,b},{4,c}],
X =:= Y],
{cache, list}),
io:format("~s~n", [qlc:info(Q)])\&.
begin
V1 =
qlc:q([P0 ||
P0 = {X,Z} <- qlc:keysort(1, [{a,1},{b,4},{c,6}], [])]),
V2 =
qlc:q([P0 ||
P0 = {W,Y} <- qlc:keysort(2, [{2,a},{3,b},{4,c}], [])]),
V3 =
qlc:q([[G1|G2] ||
G1 <- V1,
G2 <- V2,
element(1, G1) == element(2, G2)],
[{join,merge},{cache,list}]),
qlc:q([{A,X,Z,W} ||
A <- [a,b,c],
[{X,Z}|{W,Y}] <- V3,
X =:= Y])
end
.fi
.LP
In this example the cached results of the merge join are traversed for each value of \fIA\fR\&. Note that without the \fIcache\fR option the join would have been carried out three times, once for each value of \fIA\fR
.LP
\fIsort/1, 2\fR and \fIkeysort/2, 3\fR can also be used for caching answers and for removing duplicates\&. When sorting answers are cached in a list, possibly stored on a temporary file, and no ETS tables are used\&.
.LP
Sometimes (see qlc:table/2 below) traversal of tables can be done by looking up key values, which is assumed to be fast\&. Under certain (rare) circumstances it could happen that there are too many key values to look up\&. The \fI{max_lookup, MaxLookup}\fR option can then be used to limit the number of lookups: if more than \fIMaxLookup\fR lookups would be required no lookups are done but the table traversed instead\&. The default value is \fIinfinity\fR which means that there is no limit on the number of keys to look up\&.
.nf
1> T = gb_trees:empty(),
QH = qlc:q([X || {{X,Y},_} <- gb_table:table(T),
((X =:= 1) or (X =:= 2)),
((Y =:= a) or (Y =:= b) or (Y =:= c))]),
io:format("~s~n", [qlc:info(QH)])\&.
ets:match_spec_run(
lists:flatmap(fun(K) ->
case
gb_trees:lookup(K,
gb_trees:from_orddict([]))
of
{value,V} ->
[{K,V}];
none ->
[]
end
end,
[{1,a},{1,b},{1,c},{2,a},{2,b},{2,c}]),
ets:match_spec_compile([{{{\&'$1\&',\&'$2\&'},\&'_\&'},
[{\&'andalso\&',
{\&'or\&',
{\&'=:=\&',\&'$1\&',1},
{\&'=:=\&',\&'$1\&',2}},
{\&'or\&',
{\&'or\&',
{\&'=:=\&',\&'$2\&',a},
{\&'=:=\&',\&'$2\&',b}},
{\&'=:=\&',\&'$2\&',c}}}],
[\&'$1\&']}]))
.fi
.LP
In this example using the \fIgb_table\fR module from the Implementing a QLC table section there are six keys to look up: \fI{1, a}\fR, \fI{1, b}\fR, \fI{1, c}\fR, \fI{2, a}\fR, \fI{2, b}\fR, and \fI{2, c}\fR\&. The reason is that the two elements of the key {X,Y} are matched separately\&.
.LP
The \fI{lookup, true}\fR option can be used to ensure that the \fIqlc\fR module will look up constants in some QLC table\&. If there are more than one QLC table among the generators\&' list expressions, constants have to be looked up in at least one of the tables\&. The evaluation of the query fails if there are no constants to look up\&. This option is useful in situations when it would be unacceptable to traverse all objects in some table\&. Setting the \fIlookup\fR option to \fIfalse\fR ensures that no constants will be looked up (\fI{max_lookup, 0}\fR has the same effect)\&. The default value is \fIany\fR which means that constants will be looked up whenever possible\&.
.LP
The \fI{join, Join}\fR option can be used to ensure that a certain join method will be used: \fI{join, lookup}\fR invokes the lookup join method; \fI{join, merge}\fR invokes the merge join method; and \fI{join, nested_loop}\fR invokes the method of matching every pair of objects from two handles\&. The last method is mostly very slow\&. The evaluation of the query fails if the \fIqlc\fR module cannot carry out the chosen join method\&. The default value is \fIany\fR which means that some fast join method will be used if possible\&.
.RE
.LP
.B
sort(QH1 [, SortOptions]) -> QH2
.br
.RS
.TP
Types
QH1 = QueryHandleOrList
.br
QH2 = QueryHandle
.br
.RE
.RS
.LP
Returns a query handle\&. When evaluating the query handle \fIQH2\fR the answers to the query handle \fIQH1\fR are sorted by file_sorter:sort/3 according to the options\&.
.LP
The sorter will use temporary files only if \fIQH1\fR does not evaluate to a list and the size of the binary representation of the answers exceeds \fISize\fR bytes, where \fISize\fR is the value of the \fIsize\fR option\&.
.RE
.LP
.B
string_to_handle(QueryString [, Options [, Bindings]]) -> QueryHandle | Error
.br
.RS
.TP
Types
QueryString = string()
.br
Options = [Option] | Option
.br
Option = {max_lookup, MaxLookup} | {cache, Cache} | cache | {join, Join} | {lookup, Lookup} | {unique, bool()} | unique
.br
MaxLookup = int() >= 0 | infinity
.br
Join = any | lookup | merge | nested_loop
.br
Lookup = bool() | any
.br
Bindings =-as returned by erl_eval:bindings/1-
.br
Error = {error, module(), Reason}
.br
Reason = -ErrorInfo as returned by erl_scan:string/1 or erl_parse:parse_exprs/1-
.br
.RE
.RS
.LP
A string version of \fIqlc:q/1, 2\fR\&. When the query handle is evaluated the fun created by the parse transform is interpreted by \fIerl_eval(3)\fR\&. The query string is to be one single query list comprehension terminated by a period\&.
.nf
1> L = [1,2,3],
Bs = erl_eval:add_binding(\&'L\&', L, erl_eval:new_bindings()),
QH = qlc:string_to_handle("[X+1 || X <- L]\&.", [], Bs),
qlc:eval(QH)\&.
[2,3,4]
.fi
.LP
This function is probably useful mostly when called from outside of Erlang, for instance from a driver written in C\&.
.RE
.LP
.B
table(TraverseFun, Options) -> QueryHandle
.br
.RS
.TP
Types
TraverseFun = TraverseFun0 | TraverseFun1
.br
TraverseFun0 = fun() -> TraverseResult
.br
TraverseFun1 = fun(MatchExpression) -> TraverseResult
.br
TraverseResult = Objects | term()
.br
Objects = [] | [term() | ObjectList]
.br
ObjectList = TraverseFun0 | Objects
.br
Options = [Option] | Option
.br
Option = {format_fun, FormatFun} | {info_fun, InfoFun} | {lookup_fun, LookupFun} | {parent_fun, ParentFun} | {post_fun, PostFun} | {pre_fun, PreFun}
.br
FormatFun = undefined | fun(SelectedObjects) -> FormatedTable
.br
SelectedObjects = all | {all, NElements, DepthFun} | {match_spec, MatchExpression} | {lookup, Position, Keys} | {lookup, Position, Keys, NElements, DepthFun}
.br
NElements = infinity | int() > 0
.br
DepthFun = fun(term()) -> term()
.br
FormatedTable = {Mod, Fun, Args} | AbstractExpression | character_list()
.br
InfoFun = undefined | fun(InfoTag) -> InfoValue
.br
InfoTag = indices | is_unique_objects | keypos | num_of_objects
.br
InfoValue = undefined | term()
.br
LookupFun = undefined | fun(Position, Keys) -> LookupResult
.br
LookupResult = [term()] | term()
.br
ParentFun = undefined | fun() -> ParentFunValue
.br
PostFun = undefined | fun() -> void()
.br
PreFun = undefined | fun([PreArg]) -> void()
.br
PreArg = {parent_value, ParentFunValue} | {stop_fun, StopFun}
.br
ParentFunValue = undefined | term()
.br
StopFun = undefined | fun() -> void()
.br
Position = int() > 0
.br
Keys = [term()]
.br
Mod = Fun = atom()
.br
Args = [term()]
.br
.RE
.RS
.LP
Returns a query handle for a QLC table\&. In Erlang/OTP there is support for ETS, Dets and Mnesia tables, but it is also possible to turn many other data structures into QLC tables\&. The way to accomplish this is to let function(s) in the module implementing the data structure create a query handle by calling \fIqlc:table/2\fR\&. The different ways to traverse the table as well as properties of the table are handled by callback functions provided as options to \fIqlc:table/2\fR\&.
.LP
The callback function \fITraverseFun\fR is used for traversing the table\&. It is to return a list of objects terminated by either \fI[]\fR or a nullary fun to be used for traversing the not yet traversed objects of the table\&. Any other return value is immediately returned as value of the query evaluation\&. Unary \fITraverseFun\fRs are to accept a match specification as argument\&. The match specification is created by the parse transform by analyzing the pattern of the generator calling \fIqlc:table/2\fR and filters using variables introduced in the pattern\&. If the parse transform cannot find a match specification equivalent to the pattern and filters, \fITraverseFun\fR will be called with a match specification returning every object\&. Modules that can utilize match specifications for optimized traversal of tables should call \fIqlc:table/2\fR with a unary \fITraverseFun\fR while other modules can provide a nullary \fITraverseFun\fR\&. \fIets:table/2\fR is an example of the former; \fIgb_table:table/1\fR in the Implementing a QLC table section is an example of the latter\&.
.LP
\fIPreFun\fR is a unary callback function that is called once before the table is read for the first time\&. If the call fails, the query evaluation fails\&. Similarly, the nullary callback function \fIPostFun\fR is called once after the table was last read\&. The return value, which is caught, is ignored\&. If \fIPreFun\fR has been called for a table, \fIPostFun\fR is guaranteed to be called for that table, even if the evaluation of the query fails for some reason\&. The order in which pre (post) functions for different tables are evaluated is not specified\&. Other table access than reading, such as calling \fIInfoFun\fR, is assumed to be OK at any time\&. The argument \fIPreArgs\fR is a list of tagged values\&. Currently there are two tags, \fIparent_value\fR and \fIstop_fun\fR, used by Mnesia for managing transactions\&. The value of \fIparent_value\fR is the value returned by \fIParentFun\fR, or \fIundefined\fR if there is no \fIParentFun\fR\&. \fIParentFun\fR is called once just before the call of \fIPreFun\fR in the context of the process calling \fIeval\fR, \fIfold\fR, or \fIcursor\fR\&. The value of \fIstop_fun\fR is a nullary fun that deletes the cursor if called from the parent, or \fIundefined\fR if there is no cursor\&.
.LP
The binary callback function \fILookupFun\fR is used for looking up objects in the table\&. The first argument \fIPosition\fR is the key position or an indexed position and the second argument \fIKeys\fR is a sorted list of unique values\&. The return value is to be a list of all objects (tuples) such that the element at \fIPosition\fR is a member of \fIKeys\fR\&. Any other return value is immediately returned as value of the query evaluation\&. \fILookupFun\fR is called instead of traversing the table if the parse transform at compile time can find out that the filters match and compare the element at \fIPosition\fR in such a way that only \fIKeys\fR need to be looked up in order to find all potential answers\&. The key position is obtained by calling \fIInfoFun(keypos)\fR and the indexed positions by calling \fIInfoFun(indices)\fR\&. If the key position can be used for lookup it is always chosen, otherwise the indexed position requiring the least number of lookups is chosen\&. If there is a tie between two indexed positions the one occurring first in the list returned by \fIInfoFun\fR is chosen\&. Positions requiring more than max_lookup lookups are ignored\&.
.LP
The unary callback function \fIInfoFun\fR is to return information about the table\&. \fIundefined\fR should be returned if the value of some tag is unknown:
.RS 2
.TP 2
*
\fIindices\fR\&. Returns a list of indexed positions, a list of positive integers\&.
.TP 2
*
\fIis_unique_objects\fR\&. Returns \fItrue\fR if the objects returned by \fITraverseFun\fR are unique\&.
.TP 2
*
\fIkeypos\fR\&. Returns the position of the table\&'s key, a positive integer\&.
.TP 2
*
\fIis_sorted_key\fR\&. Returns \fItrue\fR if the objects returned by \fITraverseFun\fR are sorted on the key\&.
.TP 2
*
\fInum_of_objects\fR\&. Returns the number of objects in the table, a non-negative integer\&.
.RE
.LP
The unary callback function \fIFormatFun\fR is used by qlc:info/1,2 for displaying the call that created the table\&'s query handle\&. The default value, \fIundefined\fR, means that \fIinfo/1, 2\fR displays a call to \fI\&'$MOD\&':\&'$FUN\&'/0\fR\&. It is up to \fIFormatFun\fR to present the selected objects of the table in a suitable way\&. However, if a character list is chosen for presentation it must be an Erlang expression that can be scanned and parsed (a trailing dot will be added by \fIqlc:info\fR though)\&. \fIFormatFun\fR is called with an argument that describes the selected objects based on optimizations done as a result of analyzing the filters of the QLC where the call to \fIqlc:table/2\fR occurs\&. The possible values of the argument are:
.RS 2
.TP 2
*
\fI{lookup, Position, Keys, NElements, DepthFun}\fR\&. \fILookupFun\fR is used for looking up objects in the table\&.
.TP 2
*
\fI{match_spec, MatchExpression}\fR\&. No way of finding all possible answers by looking up keys was found, but the filters could be transformed into a match specification\&. All answers are found by calling \fITraverseFun(MatchExpression)\fR\&.
.TP 2
*
\fI{all, NElements, DepthFun}\fR\&. No optimization was found\&. A match specification matching all objects will be used if \fITraverseFun\fR is unary\&.
.RE
.LP
\fINElements\fR is the value of the \fIinfo/1, 2\fR option \fIn_elements\fR, and \fIDepthFun\fR is a function that can be used for limiting the size of terms; calling \fIDepthFun(Term)\fR substitutes \fI\&'\&.\&.\&.\&'\fR for parts of \fITerm\fR below the depth specified by the \fIinfo/1, 2\fR option \fIdepth\fR\&. If calling \fIFormatFun\fR with an argument including \fINElements\fR and \fIDepthFun\fR fails, \fIFormatFun\fR is called once again with an argument excluding \fINElements\fR and \fIDepthFun\fR (\fI{lookup, Position, Keys}\fR or \fIall\fR)\&.
.LP
See ets(3), dets(3) and mnesia(3) for the various options recognized by \fItable/1, 2\fR in respective module\&.
.RE
.SH SEE ALSO
.LP
dets(3), Erlang Reference Manual, erl_eval(3), erlang(3), ets(3), file(3), error_logger(3), file_sorter(3), mnesia(3), Programming Examples, shell(3)
|