From 68d53c01b0b8e9a007a6a30158c19e34b2d2a34e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= Date: Wed, 18 May 2016 15:53:35 +0200 Subject: Update STDLIB documentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Language cleaned up by the technical writers xsipewe and tmanevik from Combitech. Proofreading and corrections by Björn Gustavsson and Hans Bolinder. --- lib/stdlib/doc/src/qlc.xml | 1428 +++++++++++++++++++++++--------------------- 1 file changed, 738 insertions(+), 690 deletions(-) (limited to 'lib/stdlib/doc/src/qlc.xml') diff --git a/lib/stdlib/doc/src/qlc.xml b/lib/stdlib/doc/src/qlc.xml index 2966e94ec1..fe14a6334c 100644 --- a/lib/stdlib/doc/src/qlc.xml +++ b/lib/stdlib/doc/src/qlc.xml @@ -24,102 +24,121 @@ qlc Hans Bolinder - nobody + - nobody - no + + 2004-08-25 PA1 - qlc.sgml + qlc.xml qlc - Query Interface to Mnesia, ETS, Dets, etc + Query interface to Mnesia, ETS, Dets, and so on. + -

The qlc module provides a query interface to Mnesia, ETS, - Dets and other data structures that implement an iterator style - traversal of objects.

+

This module provides a query interface to + Mnesia, + ETS, + Dets, + and other data structures that provide an iterator style + traversal of objects.

-
Overview - -

The qlc module implements a query interface to QLC - tables. 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 query is stated using +

+ Overview +

This module provides a query interface to QLC + tables. Typical QLC tables are Mnesia, ETS, and + Dets tables. Support is also provided for user-defined tables, see section + + Implementing a QLC Table. + + A query is expressed using Query List Comprehensions (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 - cache and unique (see below), every QLC free of - QLC tables evaluates to the same list of answers as the + to ordinary list comprehensions as described in + + Erlang Reference Manual and + + Programming Examples, except that variables + introduced in patterns cannot be used in list expressions. + In the absence of optimizations and options such as + cache and unique (see section + Common Options, every + QLC free of QLC tables evaluates to the same list of answers as the identical ordinary list comprehension.

While ordinary list comprehensions evaluate to lists, calling - qlc:q/1,2 returns a Query - Handle. To obtain all the answers to a query, qlc:eval/1,2 should be called with the + q/1,2 returns a + query handle. + To obtain all the answers to a query, + eval/1,2 is to be called with the query handle as first argument. Query handles are essentially - functional objects ("funs") created in the module calling q/1,2. - 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 Query Cursor. Query cursors are - created by calling qlc:cursor/1,2 with a query handle as - first argument. Query cursors are essentially Erlang processes. + functional objects (funs) created in the module calling q/1,2. + As the funs refer to the module code, be careful not to keep query + handles too long if the module code is to be replaced. + Code replacement is described in section + + Compilation and Code Loading in the Erlang Reference Manual. + The list of answers can also be traversed in chunks by use of a + query cursor. + Query cursors are created by calling + 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.

-
-
Syntax - +
+ Syntax

Syntactically QLCs have the same parts as ordinary list comprehensions:

- [Expression || Qualifier1, Qualifier2, ...] + +[Expression || Qualifier1, Qualifier2, ...] -

Expression (the template) is an arbitrary +

Expression (the template) is any Erlang expression. Qualifiers are either filters or generators. Filters are Erlang expressions returning - bool(). Generators have the form + boolean(). Generators have the form , where ListExpression is an expression evaluating to a query handle or a list. Query handles are returned from - qlc:table/2, qlc:append/1,2, qlc:sort/1,2, - qlc:keysort/2,3, qlc:q/1,2, and - qlc:string_to_handle/1,2,3.

- + append/1,2, + keysort/2,3, + q/1,2, + sort/1,2, + + string_to_handle/1,2,3, and + table/2.

-
Evaluation - -

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 false, or - collecting the template when all filters return true.

- -

Filters that do not return bool() but fail are handled - differently depending on their syntax: if the filter is a guard +

+ Evaluation +

A query handle is evaluated in the following order:

+ + + +

Inspection of options and the collection of information about + tables. As a result, qualifiers are modified during the optimization + phase.

+
+ +

All list expressions are evaluated. If a cursor has been created, + evaluation takes place in the cursor process. For list expressions + that are QLCs, the list expressions of the generators of the QLCs + are evaluated as well. Be careful if list expressions have side + effects, as list expressions are evaluated in unspecified order.

+
+ +

The answers are found by evaluating the qualifiers from left to + right, backtracking when some filter returns false, or + collecting the template when all filters return true.

+
+
+ +

Filters that do not return boolean() but fail are handled + differently depending on their syntax: if the filter is a guard, it returns false, otherwise the query evaluation fails. This behavior makes it possible for the qlc module to do some optimizations without affecting the meaning of a query. For @@ -131,302 +150,311 @@ 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 + immediately after the table generator, otherwise the candidate + objects are not 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.

- + be excluded by looking up a key or running a match specification.

-
Join - +
+ Join

The qlc module supports fast join of two query handles. Fast join is possible if some position P1 of one query handler and some position P2 of another query handler are - tested for equality. Two fast join methods have been - implemented:

+ tested for equality. Two fast join methods are provided:

- Lookup join traverses all objects of one query handle and - finds objects of the other handle (a QLC table) such that the +

Lookup join traverses all objects of one query handle + and finds objects of the other handle (a QLC table) such that the values at P1 and P2 match or compare equal. The qlc module does not create - any indices but looks up values using the key position and - the indexed positions of the QLC table. + any indexes but looks up values using the key position and + the indexed positions of the QLC table.

- Merge join sorts the objects of each query handle if +

Merge join sorts the objects of each query handle if necessary and filters out objects where the values at - P1 and P2 do not compare equal. If there are - many objects with the same value of P2 a temporary - file will be used for the equivalence classes. + P1 and P2 do not compare equal. If + many objects with the same value of P2 exist, a temporary + file is used for the equivalence classes.

The qlc 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 + possible. That is, no query planner is provided that can + select a good order between possible join operations. It is up to the user to order the joins by introducing query handles.

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 qlc module inspects + but the two joined generators. The qlc module inspects the operands of =:=/2, ==/2, is_record/2, element/2, and logical operators (and/2, or/2, andalso/2, orelse/2, xor/2) when determining which joins to consider.

-
-
Common options - -

The following options are accepted by cursor/2, - eval/2, fold/4, and info/2:

+
+ + Common Options +

The following options are accepted by + cursor/2, + eval/2, + fold/4, and + info/2:

- {cache_all, Cache} where Cache is +

{cache_all, Cache}, where Cache is equal to ets or list adds a {cache, Cache} option to every list expression - of the query except tables and lists. Default is - {cache_all, no}. The option cache_all is - equivalent to {cache_all, ets}. + of the query except tables and lists. Defaults to + {cache_all, no}. Option cache_all is + equivalent to {cache_all, ets}.

- {max_list_size, MaxListSize} where MaxListSize is the +

{max_list_size, + MaxListSize}, where MaxListSize is the size in bytes of terms on the external format. If the accumulated size of collected objects exceeds - MaxListSize the objects are written onto a temporary - file. This option is used by the {cache, list} - option as well as by the merge join method. Default is - 512*1024 bytes. + MaxListSize, the objects are written onto a temporary + file. This option is used by option {cache, list} + and by the merge join method. Defaults to 512*1024 bytes.

- {tmpdir_usage, TmpFileUsage} determines the +

{tmpdir_usage, TmpFileUsage} determines the action taken when qlc is about to create temporary - files on the directory set by the tmpdir option. If the - value is not_allowed an error tuple is returned, + files on the directory set by option tmpdir. If the + value is not_allowed, an error tuple is returned, otherwise temporary files are created as needed. Default is - allowed which means that no further action is taken. + allowed, which means that no further action is taken. The values info_msg, warning_msg, and error_msg mean that the function with the corresponding - name in the module error_logger is called for printing - some information (currently the stacktrace). + name in module + error_logger + is called for printing some information (currently the stacktrace).

- {tmpdir, TempDirectory} sets the directory used by - merge join for temporary files and by the - {cache, list} option. The option also overrides - the tmpdir option of keysort/3 and - sort/2. The default value is "" which means that - the directory returned by file:get_cwd() is used. +

{tmpdir, TempDirectory} sets the directory used by + merge join for temporary files and by option + {cache, list}. The option also overrides + option tmpdir of + keysort/3 and + sort/2. + Defaults to "", which means that + the directory returned by file:get_cwd() is used.

- {unique_all, true} adds a +

{unique_all, true} adds a {unique, true} option to every list expression of - the query. Default is {unique_all, false}. The - option unique_all is equivalent to - {unique_all, true}. + the query. Defaults to {unique_all, false}. + Option unique_all is equivalent to + {unique_all, true}.

-
-
Getting started - -

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 +

+ + Getting Started +

As mentioned earlier, + queries are expressed in the list comprehension syntax as described + in section + Expressions + in Erlang Reference Manual. In the following, some familiarity + with list comprehensions is assumed. The examples in section + + List Comprehensions in Programming Examples can get you + started. Notice 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 + comprehensions can also be done without them. But they add + syntax for expressing simple search problems, which is compact and clear once you get used to it.

Many list comprehension expressions can be evaluated by the - qlc module. Exceptions are expressions such that + qlc 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): - . - Y is introduced in the first generator and used in the second. + generator later in the list comprehension. As an example, + consider an implementation of lists:append(L): + . + 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 - qlc:eval/1,2 collects answers in a list which is finally + eval/1,2 + collects answers in a list that is finally reversed, while list comprehensions collect answers on the stack - which is finally unwound.

+ that is finally unwound.

What the qlc 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 qlc:table/2. + chunks. A QLC table is created by calling + qlc:table/2. Usually qlc:table/2 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: - mnesia:table/1,2, ets:table/1,2, and - dets:table/1,2. 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 - qlc:table/2. 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 + but through an interface function of some data structure. + Erlang/OTP includes a few examples of such functions: + mnesia:table/1,2, + ets:table/1,2, and + dets:table/1,2. + For a given data structure, many functions can create QLC tables, but + common for these functions is that they return a query handle created by + qlc:table/2. + Using the QLC tables provided by Erlang/OTP is usually + probably sufficient, but for the more advanced user section + Implementing a QLC + Table describes the implementation of a function calling qlc:table/2.

-

Besides qlc:table/2 there are other functions that - return query handles. They might not be used as often as tables, - but are useful from time to time. qlc:append traverses - objects from several tables or lists after each other. If, for - instance, you want to traverse all answers to a query QH and +

Besides qlc:table/2, other functions + return query handles. They are used more seldom than tables, + but are sometimes useful. + qlc:append/1,2 traverses + objects from many tables or lists after each other. If, for + example, you want to traverse all answers to a query QH and then finish off by a term {finished}, you can do that by - calling qlc:append(QH, [{finished}]). append first - returns all objects of QH, then {finished}. If there is - one tuple {finished} among the answers to QH it will be - returned twice from append.

+ calling qlc:append(QH, [{finished}]). append/2 first + returns all objects of QH, then {finished}. If a tuple + {finished} exists among the answers to QH, it is + returned twice from append/2.

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 unique option:

+ queries QH1 and QH2 while removing all duplicates. This is + accomplished by using option unique:

- + + -

The cost is substantial: every returned answer will be stored - in an ETS table. Before returning an answer it is looked up in +

The cost is substantial: every returned answer is 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 unique options all answers to QH1 would be returned - followed by all answers to QH2. The unique options keeps + the unique option, all answers to QH1 would be returned + followed by all answers to QH2. The unique option keeps the order between the remaining answers.

-

If the order of the answers is not important there is the - alternative to sort the answers uniquely:

+

If the order of the answers is not important, there is an + alternative to the unique option, namely to sort the + answers uniquely:

- + + -

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. Both alternatives find duplicates - by comparing answers, that is, if A1 and A2 are answers found in - that order, then A2 is a removed if A1 == A2.

+

This query also removes duplicates but the answers are + sorted. If there are many answers, temporary files are used. + Notice that to get the first unique answer, all answers + must be found and sorted. Both alternatives find duplicates by comparing + answers, that is, if A1 and A2 are answers found in + that order, then A2 is a removed if A1 == A2.

-

To return just a few answers cursors can be used. The following +

To return only 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:

- + -

Query list comprehensions are convenient for stating - constraints on data from two or more tables. An example that +

QLCs are convenient for stating + constraints on data from two or more tables. The following example does a natural join on two query handles on position 2:

- + -

The qlc module will evaluate this differently depending on - the query - handles QH1 and QH2. If, for example, X2 is - matched against the key of a QLC table the lookup join method - will traverse the objects of QH2 while looking up key - values in the table. On the other hand, if neither X2 nor +

The qlc module evaluates this differently depending on the + query handles QH1 and QH2. If, for example, X2 is + matched against the key of a QLC table, the lookup join method + traverses the objects of QH2 while looking up key + values in the table. However, if not X2 or Y2 is matched against the key or an indexed position of a - QLC table, the merge join method will make sure that QH1 + QLC table, the merge join method ensures that QH1 and QH2 are both sorted on position 2 and next do the join by traversing the objects one by one.

-

The join option can be used to force the qlc module - to use a - certain join method. For the rest of this section it is assumed +

Option join can be used to force the qlc 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:

- + -

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.

- -

If QH2 is a call to the function for gb_trees as defined - in the Implementing - a QLC table section, gb_table:table/1, 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 +

In this case the filter is 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 is run M*N times.

+ +

If QH2 is a call to the function for + gb_trees, as defined + in section Implementing + a QLC Table, then gb_table:table/1, the + iterator for the gb-tree is initiated for each answer to + QH1. The objects of the gb-tree are then 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 + the table in that case, as 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 to 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 cache option. It is stated like this:

- - - -

or just

- - - -

The effect of the cache 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 - unique option the cost is a possibly substantial amount - of RAM memory. The {cache, list} option offers the + 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 option cache. It is expressed like this:

+ + + + +

or only

+ + + + +

The effect of option cache is that when + generator QH2' is run the first time, every answer is stored in + an ETS table. When the next answer of QH1 is tried, + answers to QH2' + are copied from the ETS table, which is very fast. As for + option unique the cost is a possibly substantial amount + of RAM memory.

+ +

Option {cache, list} 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.

-

There is an option cache_all that can be set to +

Option cache_all can be set to ets or list when evaluating a query. It adds a cache or {cache, list} 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.

- + efficiency at all. If the answer is yes, further testing is + needed to pinpoint the generators that are to be cached.

-
Implementing a QLC table - -

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:

+
+ + Implementing a QLC Table +

As an example of + how to use function table/2, + the implementation of a QLC table for the gb_trees module is given:

- +

TF is the traversal function. The qlc module requires that there is a way of traversing all objects of the - data structure; in gb_trees there is an iterator function - suitable for that purpose. Note that for each object returned a + data structure. gb_trees has an iterator function + suitable for that purpose. Notice that for each object returned, a new fun is created. As long as the list is not terminated by - [] it is assumed that the tail of the list is a nullary + [], it is assumed that the tail of the list is a nullary function and that calling the function returns further objects (and functions).

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 qlc_next 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.

+ key. As qlc_next/1 returns the objects as {Key, Value} + pairs, the position is 1. Notice that the lookup function is to return + {Key, Value} pairs, as the traversal function does.

The format function is also optional. It is called by - qlc:info 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 + info/1,2 + to give feedback at runtime of how the query + is to be evaluated. Try to give as good feedback as + possible without showing too much details. In the example, at + most seven objects of the table are shown. The format function handles two cases: all means that all objects of the - table will be traversed; {lookup, 1, KeyValues} - means that the lookup function will be used for looking up key + table are traversed; {lookup, 1, KeyValues} + means that the lookup function is used for looking up key values.

-

Whether the whole table will be traversed or just some keys - looked up depends on how the query is stated. If the query has +

Whether the whole table is traversed or only some keys + looked up depends on how the query is expressed. If the query has the form

- + + -

and P is a tuple, the qlc module analyzes P and F in - compile time to find positions of the tuple P that are tested +

and P is a tuple, the qlc module analyzes + P and F in + compile time to find positions of tuple P that are tested for equality 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 InfoFun that returns the key position. + otherwise all objects of the table must be traversed. + The info function InfoFun 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 qlc + some position fast. Mnesia maintains indexes upon request, + and introduces so called secondary keys. The qlc module prefers to look up objects using the key before secondary keys regardless of the number of constants to look up.

-
-
Key equality - -

In Erlang there are two operators for testing term equality, - namely ==/2 and =:=/2. The difference between them - is all about the integers that can be represented by floats. For - instance, 2 == 2.0 evaluates to +

+ Key Equality +

Erlang/OTP has two operators for testing term equality: ==/2 + and =:=/2. The difference is all about the integers that can be + represented by floats. For example, 2 == 2.0 evaluates to true while 2 =:= 2.0 evaluates to false. Normally this is a minor issue, but the qlc module cannot ignore the difference, which affects the user's choice of operators in QLCs.

-

If the qlc module can find out at compile time that some +

If the qlc module at compile time can determine that some constant is free of integers, it does not matter which one of ==/2 or =:=/2 is used:

@@ -560,16 +587,16 @@ ets:match_spec_run(lists:flatmap(fun(V) -> [a,2.71]), ets:match_spec_compile([{{'$1'},[],['$1']}])) -

In the example the ==/2 operator has been handled - exactly as =:=/2 would have been handled. On the other - hand, if it cannot be determined at compile time that some - constant is free of integers and the table uses =:=/2 - when comparing keys for equality (see the option key_equality), the - qlc module will not try to look up the constant. The +

In the example, operator ==/2 has been handled + exactly as =:=/2 would have been handled. However, + if it cannot be determined at compile time that some + constant is free of integers, and the table uses =:=/2 + when comparing keys for equality (see option key_equality), then the + qlc module does not try to look up the constant. The reason is that there is in the general case no upper limit on the number of key values that can compare equal to such a - constant; every combination of integers and floats has to be + constant; every combination of integers and floats must be looked up:

@@ -586,11 +613,11 @@ ets:table(53264,
 3> lists:sort(qlc:e(Q2)).
 [a,b,c]
-

Looking up just {2,2} would not return b and +

Looking up only {2,2} would not return b and c.

If the table uses ==/2 when comparing keys for equality, - the qlc module will look up the constant regardless of + the qlc module looks up the constant regardless of which operator is used in the QLC. However, ==/2 is to be preferred:

@@ -608,19 +635,18 @@ ets:match_spec_run(ets:lookup(86033, {2,2}), [b]

Lookup join is handled analogously to lookup of constants in a - table: if the join operator is ==/2 and the table where + table: if the join operator is ==/2, and the table where constants are to be looked up uses =:=/2 when testing - keys for equality, the qlc module will not consider + keys for equality, then the qlc module does not consider lookup join for that table.

-
-

Parse trees for Erlang expression, see the abstract format - documentation in the ERTS User's Guide.

+

Parse trees for Erlang expression, see section The Abstract Format + in the ERTS User's Guide.

@@ -633,14 +659,14 @@ ets:match_spec_run(ets:lookup(86033, {2,2}), -

Match specification, see the match specification - documentation in the ERTS User's Guide and ms_transform(3).

+

Match specification, see section Match Specifications in Erlang + in the ERTS User's Guide and ms_transform(3).

-

Actually an integer > 1.

+

An integer > 1.

@@ -671,7 +697,7 @@ ets:match_spec_run(ets:lookup(86033, {2,2}),

A literal query - list comprehension.

+ list comprehension.

@@ -682,7 +708,7 @@ ets:match_spec_run(ets:lookup(86033, {2,2}),

See file_sorter(3).

+ marker="file_sorter">file_sorter(3).

@@ -693,15 +719,14 @@ ets:match_spec_run(ets:lookup(86033, {2,2}),
- Return a query handle. -

Returns a query handle. When evaluating the query handle - QH all answers to the first query handle in - QHL are returned followed by all answers - to the rest of the query handles in QHL.

+

Returns a query handle. When evaluating query handle + QH, all answers to the first query handle in + QHL are returned, followed by all answers + to the remaining query handles in QHL.

@@ -709,11 +734,10 @@ ets:match_spec_run(ets:lookup(86033, {2,2}), Return a query handle. -

Returns a query handle. When evaluating the query handle - QH3 all answers to - QH1 are returned followed by all answers +

Returns a query handle. When evaluating query handle + QH3, all answers to + QH1 are returned, followed by all answers to QH2.

-

append(QH1, QH2) is equivalent to append([QH1, QH2]).

@@ -724,15 +748,18 @@ ets:match_spec_run(ets:lookup(86033, {2,2}), Create a query cursor. -

Creates a query cursor and +

Creates a query cursor and makes the calling process the owner of the cursor. The - cursor is to be used as argument to next_answers/1,2 - and (eventually) delete_cursor/1. Calls - erlang:spawn_opt to spawn and link a process which - will evaluate the query handle. The value of the option + cursor is to be used as argument to + + next_answers/1,2 and (eventually) + delete_cursor/1. + Calls + erlang:spawn_opt/2 to spawn and link to + a process that evaluates the query handle. The value of option spawn_options is used as last argument when calling - spawn_opt. The default value is [link].

- + spawn_opt/2. Defaults to [link].

+

Example:

 1> QH = qlc:q([{X,Y} || X <- [a,b], Y <- [1,2]]),
 QC = qlc:cursor(QH),
@@ -759,15 +786,15 @@ ok
- - + + Return all answers to a query. -

Evaluates a query handle in the +

Evaluates a query handle in the calling process and collects all answers in a list.

- +

Example:

 1> QH = qlc:q([{X,Y} || X <- [a,b], Y <- [1,2]]),
 qlc:eval(QH).
@@ -786,11 +813,11 @@ ok
the query handle together with an extra argument AccIn. The query handle and the function are evaluated in the calling process. - Function must return a new accumulator + Function must return a new accumulator, which is passed to the next call. Acc0 is returned if there are no answers to the query handle.

- +

Example:

 1> QH = [1,2,3,4,5,6],
 qlc:fold(fun(X, Sum) -> X + Sum end, 0, QH).
@@ -818,30 +845,46 @@ ok
Return code describing a query handle. -

Returns information about a +

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.

- + query for evaluation. This function is probably mainly useful + during debugging.

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.

- -

The default is to return a sequence of QLCs in a block, but - if the option {flat, false} is given, one single - QLC is returned. The default is to return a string, but if - the option {format, abstract_code} 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 - {n_elements, NElements} 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 - {depth, Depth} option is given, parts of terms - below a certain depth are replaced by '...'.

- + mentioned QLC tables, it is not certain that the information + is absolutely accurate.

+

Options:

+ + +

The default is to return a sequence of QLCs in a block, but + if option {flat, false} is specified, one single + QLC is returned.

+
+ +

The default is to return a string, but if + option {format, abstract_code} is specified, + 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 + option {n_elements, NElements} is specified, only + a limited number of elements are returned.

+
+ +

The default is to show all parts of + objects and match specifications, + but if option {depth, Depth} is specified, parts + of terms below a certain depth are replaced by '...'.

+
+
+

info(QH) is equivalent to + info(QH, []).

+

Examples:

+

In the following example two simple QLCs are inserted only to + hold option {unique, true}:

 1> QH = qlc:q([{X,Y} || X <- [x,y], Y <- [a,b]]),
 io:format("~s~n", [qlc:info(QH, unique_all)]).
@@ -865,10 +908,11 @@ begin
           ],
           [{unique,true}])
 end
- -

In this example two simple QLCs have been inserted just to - hold the {unique, true} option.

- +

In the following example QLC V2 has + been inserted to show the joined generators and the join + method chosen. A convention is used for lookup join: the + first generator (G2) is the one traversed, the second + (G1) is the table where constants are looked up.

 1> E1 = ets:new(e1, []),
 E2 = ets:new(e2, []),
@@ -898,15 +942,6 @@ begin
                [{X,Z}|{W,Y}] <- V2
           ])
 end
- -

In this example the query list comprehension V2 has - been inserted to show the joined generators and the join - method chosen. A convention is used for lookup join: the - first generator (G2) is the one traversed, the second - one (G1) is the table where constants are looked up.

- -

info(QH) is equivalent to - info(QH, []).

@@ -915,18 +950,16 @@ end Return a query handle. -

Returns a query handle. When evaluating the query handle - QH2 the answers to the query handle +

Returns a query handle. When evaluating query handle + QH2, the answers to query handle QH1 are sorted by file_sorter:keysort/4 + marker="file_sorter#keysort/4">file_sorter:keysort/4 according to the options.

- -

The sorter will use temporary files only if +

The sorter uses temporary files only if QH1 does not evaluate to a list and the size of the binary representation of the answers exceeds - Size bytes, where Size is the value of the - size option.

- + Size bytes, where Size is the value of option + size.

keysort(KeyPos, QH1) is equivalent to keysort(KeyPos, QH1, []).

@@ -941,10 +974,10 @@ end

Returns some or all of the remaining answers to a query cursor. Only the owner of QueryCursor can retrieve answers.

-

The optional argument NumberOfAnswersdetermines the - maximum number of answers returned. The default value is +

Optional argument NumberOfAnswers determines the + maximum number of answers returned. Defaults to 10. If less than the requested number of answers is - returned, subsequent calls to next_answers will + returned, subsequent calls to next_answers return [].

@@ -954,92 +987,87 @@ end Return a handle for a query list comprehension. -

Returns a query handle for a query - list comprehension. The query list comprehension must be the - first argument to qlc:q/1,2 or it will be evaluated - as an ordinary list comprehension. It is also necessary to - add the line

- +

Returns a query handle for a QLC. + The QLC must be the first argument to this function, otherwise + it is evaluated as an ordinary list comprehension. It is also + necessary to add the following line to the source code:

-include_lib("stdlib/include/qlc.hrl"). - -

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.

- -

When calling qlc:q/1,2 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 erl_eval(3). This - is also true when expressions are evaluated by means of +

This causes a parse transform to substitute a fun for the QLC. The + (compiled) fun is called when the query handle is evaluated.

+

When calling qlc:q/1,2 from the Erlang shell, the + parse transform is automatically called. When this occurs, the fun + substituted for the QLC is not compiled but is evaluated by + erl_eval(3). This + is also true when expressions are evaluated by file:eval/1,2 or in the debugger.

- -

To be very explicit, this will not work:

- +

To be explicit, this does not work:

 ...
 A = [X || {X} <- [{1},{2}]],
 QH = qlc:q(A),
 ...
- -

The variable A will be bound to the evaluated value +

Variable A is bound to the evaluated value of the list comprehension ([1,2]). The compiler complains with an error message ("argument is not a query list comprehension"); the shell process stops with a badarg reason.

-

q(QLC) is equivalent to q(QLC, []).

- -

The {cache, ets} 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 - cache is equivalent to {cache, ets}.

- -

The {cache, list} option can be used to cache - the answers to a query list comprehension just like - {cache, ets}. 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 - max_list_size sets the limit in bytes and the temporary - file is put on the directory set by the tmpdir option.

- -

The cache 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 - cache option.

- -

The {unique, true} 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 unique is equivalent to - {unique, true}. If the unique option is - combined with the {cache, ets} option, two ETS - tables are used, but the full answers are stored in one - table only. If the unique option is combined with the - {cache, list} option the answers are sorted - twice using keysort/3; once to remove duplicates, and - once to restore the order.

- -

The cache and unique 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.

- +

Options:

+ + +

Option {cache, ets} can be used to cache + the answers to a QLC. The answers are stored in one ETS + table for each cached QLC. When a cached QLC is + evaluated again, answers are fetched from the table without + any further computations. Therefore, when all answers to a + cached QLC have been found, the ETS tables used for + caching answers to the qualifiers of the QLC can be emptied. + Option cache is equivalent to {cache, ets}.

+
+ +

Option {cache, list} can be used to cache + the answers to a QLC like + {cache, ets}. 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. Option + max_list_size sets the limit in bytes and the temporary + file is put on the directory set by option tmpdir.

+

Option cache has no effect if it is known that + the QLC is to be evaluated at most once. + This is always true for the top-most QLC + and also for the list expression of the first + generator in a list of qualifiers. Notice that in the presence + of side effects in filters or callback functions, the answers + to QLCs can be affected by option cache.

+
+ +

Option {unique, true} can be used to remove + duplicate answers to a QLC. The unique + answers are stored in one ETS table for each QLC. + The table is emptied every time it is known + that there are no more answers to the QLC. + Option unique is equivalent to + {unique, true}. If option unique is + combined with option {cache, ets}, two ETS + tables are used, but the full answers are stored in one + table only. If option unique is combined with option + {cache, list}, the answers are sorted + twice using + keysort/3; + once to remove duplicates and once to restore the order.

+
+
+

Options cache and unique apply not only + to the QLC itself but also to the results of looking up constants, + running match specifications, and joining handles.

+

Example:

+

In the following example the cached results of the merge join are + traversed for each value of A. Notice that without option + cache the join would have been carried out + three times, once for each value of A.

 1> Q = qlc:q([{A,X,Z,W} ||
 A <- [a,b,c],
@@ -1076,29 +1104,31 @@ begin
                X =:= Y
           ])
 end
- -

In this example the cached results of the merge join are - traversed for each value of A. Note that without the - cache option the join would have been carried out - three times, once for each value of A

- -

sort/1,2 and keysort/2,3 can also be used for +

sort/1,2 and + keysort/2,3 + 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.

-

Sometimes (see qlc:table/2 below) traversal + marker="#table/2">table/2) 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 - {max_lookup, MaxLookup} option can then be used + assumed to be fast. Under certain (rare) circumstances + there can be too many key values to look up. + + Option {max_lookup, MaxLookup} can then be used to limit the number of lookups: if more than - MaxLookup lookups would be required no lookups are - done but the table traversed instead. The default value is - infinity which means that there is no limit on the + MaxLookup lookups would be required, no lookups are + done but the table is traversed instead. Defaults to + infinity, which means that there is no limit on the number of keys to look up.

+

Example:

+

In the following example, using the gb_table module from + section Implementing a + QLC Table, there are six keys to look up: + {1,a}, {1,b}, {1,c}, {2,a}, + {2,b}, and {2,c}. The reason is that the two + elements of key {X, Y} are compared separately.

 1> T = gb_trees:empty(),
 QH = qlc:q([X || {{X,Y},_} <- gb_table:table(T),
@@ -1119,39 +1149,41 @@ ets:match_spec_run(
                      end,
                      [{1,a},{1,b},{1,c},{2,a},{2,b},{2,c}]),
        ets:match_spec_compile([{{{'$1','$2'},'_'},[],['$1']}]))
- -

In this example using the gb_table module from the - Implementing a - QLC table section there are six keys to look up: - {1,a}, {1,b}, {1,c}, {2,a}, - {2,b}, and {2,c}. The reason is that the two - elements of the key {X, Y} are compared separately.

- -

The {lookup, true} option can be used to ensure - that the qlc 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 lookup option to - false ensures that no constants will be looked up - ({max_lookup, 0} has the same effect). The - default value is any which means that constants will - be looked up whenever possible.

- -

The {join, Join} option can be used to ensure - that a certain join method will be used: - {join, lookup} invokes the lookup join method; - {join, merge} invokes the merge join method; and - {join, nested_loop} 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 qlc module cannot carry out the chosen - join method. The - default value is any which means that some fast join - method will be used if possible.

+

Options:

+ + +

Option {lookup, true} can be used to ensure + that the qlc module looks up constants in some + QLC table. If there are more than one QLC table among the + list expressions of the generators, + constants must 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 + when it would be unacceptable to traverse all + objects in some table. Setting option lookup to + false ensures that no constants are looked up + ({max_lookup, 0} has the same effect). + Defaults to any, which means that constants are + looked up whenever possible.

+
+ +

Option {join, Join} can be used to ensure + that a certain join method is used:

+ + {join, lookup} invokes the lookup join + method. + {join, merge} invokes the merge join + method. + {join, nested_loop} invokes the method of + matching every pair of objects from two handles. This + method is mostly very slow. + +

The evaluation of the query fails if the qlc module + cannot carry out the chosen join method. Defaults to + any, which means that some fast join + method is used if possible.

+
+
@@ -1160,21 +1192,18 @@ ets:match_spec_run( Return a query handle. -

Returns a query handle. When evaluating the query handle - QH2 the answers to the query handle +

Returns a query handle. When evaluating query handle + QH2, the answers to query handle QH1 are sorted by file_sorter:sort/3 according - to the options.

- -

The sorter will use temporary files only if + marker="file_sorter#sort/3">file_sorter:sort/3 + according to the options.

+

The sorter uses temporary files only if QH1 does not evaluate to a list and the size of the binary representation of the answers exceeds - Size bytes, where Size is the value of the - size option.

- + Size bytes, where Size is the value of option + size.

sort(QH1) is equivalent to sort(QH1, []).

-
@@ -1184,31 +1213,27 @@ ets:match_spec_run( Return a handle for a query list comprehension. -

A string version of qlc:q/1,2. When the query handle - is evaluated the fun created by the parse transform is - interpreted by erl_eval(3). The query string is to be - one single query list comprehension terminated by a - period.

- +

A string version of q/1,2. + When the query handle is evaluated, the fun created by the parse + transform is interpreted by + erl_eval(3). + The query string is to be one single QLC terminated by a period.

+

Example:

 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]
-

string_to_handle(QueryString) is equivalent to string_to_handle(QueryString, []).

-

string_to_handle(QueryString, - Options) - is equivalent to + Options) is equivalent to string_to_handle(QueryString, Options, erl_eval:new_bindings()).

- -

This function is probably useful mostly when called from - outside of Erlang, for instance from a driver written in C.

+

This function is probably mainly useful when called from + outside of Erlang, for example from a driver written in C.

@@ -1216,199 +1241,222 @@ ets:match_spec_run( Return a query handle for a table. -

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 - qlc:table/2. The different ways to traverse the table - as well as properties of the table are handled by callback +

Returns a query handle for a QLC table. + In Erlang/OTP there is support for ETS, Dets, and + Mnesia tables, but many other data structures can be turned + into QLC tables. This is accomplished by letting function(s) in the + module implementing the data structure create a query handle by + calling qlc:table/2. The different ways to traverse the table + and properties of the table are handled by callback functions provided as options to qlc:table/2.

- -

The callback function TraverseFun is - used for traversing the table. It is to return a list of - objects terminated by either [] 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 - TraverseFuns 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 qlc:table/2 and filters using - variables introduced in the pattern. If the parse transform - cannot find a match specification equivalent to the pattern - and filters, TraverseFun will be called - with a match specification returning every object. Modules - that can utilize match specifications for optimized - traversal of tables should call qlc:table/2 with a - unary - TraverseFun while other modules can - provide a nullary - TraverseFun. ets:table/2 is an - example of the former; gb_table:table/1 in the - Implementing a - QLC table section is an example of the latter.

- -

PreFun 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 - PostFun is called once after the table - was last read. The return value, which is caught, is - ignored. If PreFun has been called for a - table, - PostFun 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 - InfoFun, is assumed to be OK at any - time. The argument PreArgs is a list of - tagged values. Currently there are two tags, - parent_value and stop_fun, used by Mnesia for - managing transactions. The value of parent_value is - the value returned by ParentFun, or - undefined if there is no ParentFun. - ParentFun is called once just before the - call of - PreFun in the context of the process - calling - eval, fold, or - cursor. The value of stop_fun is a nullary fun - that deletes the cursor if called from the parent, or - undefined if there is no cursor.

- -

The binary callback - function LookupFun is used for looking - up objects in the table. The first argument - Position is the key position or an - indexed position and the second argument - Keys is a sorted list of unique values. - The return value is to be a list of all objects (tuples) - such that the element at Position is a member of - Keys. Any other return value is - immediately returned as value of the query evaluation. - LookupFun 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 Position in such a way that only - Keys need to be looked up in order to - find all potential answers. The key position is obtained by - calling - InfoFun(keypos) and the indexed - positions by calling - InfoFun(indices). 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 - InfoFun is chosen. Positions requiring - more than max_lookup - lookups are ignored.

- -

The unary callback function InfoFun is - to return information about the table. undefined - should be returned if the value of some tag is unknown:

- - indices. Returns a list of indexed - positions, a list of positive integers. - - is_unique_objects. Returns true if - the objects returned by TraverseFun are unique. + +

Callback function TraverseFun is + used for traversing the table. It is to return a list of + objects terminated by either [] 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 + TraverseFuns 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 qlc:table/2 and filters using + variables introduced in the pattern. If the parse transform + cannot find a match specification equivalent to the pattern + and filters, TraverseFun is called + with a match specification returning every object.

+ + +

Modules that can use match specifications for optimized + traversal of tables are to call qlc:table/2 with an unary + TraverseFun. An example is + + ets:table/2.

+
+ +

Other modules can provide a nullary + TraverseFun. An example is + gb_table:table/1 in section + Implementing a + QLC Table.

+
+
- keypos. Returns the position of the table's - key, a positive integer. + +

Unary callback function PreFun is + called once before the table is read for the first time. + If the call fails, the query evaluation fails.

+

Argument PreArgs is a list of tagged values. + There are two tags, parent_value and stop_fun, used + by Mnesia for managing transactions.

+ + +

The value of parent_value is + the value returned by ParentFun, or + undefined if there is no ParentFun. + ParentFun is called once just before the + call of PreFun in the context of the + process calling + eval/1,2, + fold/3,4, or + cursor/1,2. +

+
+ +

The value of stop_fun is a nullary fun + that deletes the cursor if called from the parent, or + undefined if there is no cursor.

+
+
- is_sorted_key. Returns true if - the objects returned by TraverseFun are sorted - on the key. + +

Nullary callback function + PostFun is called once after the table + was last read. The return value, which is caught, is ignored. + If PreFun has been called for a table, + PostFun is guaranteed to be called for + that table, even if the evaluation of the query fails for + some reason.

+

The pre (post) functions for different tables are evaluated in + unspecified order.

+

Other table access than reading, such as calling + InfoFun, is assumed to be OK at any time.

- num_of_objects. Returns the number of - objects in the table, a non-negative integer. + +

Binary callback + function LookupFun is used for looking + up objects in the table. The first argument + Position is the key position or an + indexed position and the second argument + Keys is a sorted list of unique values. + The return value is to be a list of all objects (tuples), + such that the element at Position is a member of + Keys. Any other return value is + immediately returned as value of the query evaluation. + LookupFun is called instead of + traversing the table if the parse transform at compile time + can determine that the filters match and compare the element + at Position in such a way that only + Keys need to be looked up to + find all potential answers.

+

The key position is obtained by calling + InfoFun(keypos) and the indexed + positions by calling + InfoFun(indices). 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 + InfoFun is chosen. Positions requiring + more than max_lookup + lookups are ignored.

-
- -

The unary callback function FormatFun - is used by qlc:info/1,2 - for displaying the call that created the table's query - handle. The default value, undefined, means that - info/1,2 displays a call to '$MOD':'$FUN'/0. - It is up to FormatFun 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 qlc:info though). - FormatFun 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 - qlc:table/2 occurs. The possible values of the - argument are:

- - - {lookup, Position, Keys, NElements, DepthFun}. - LookupFun is used for looking up objects in the - table. + +

Unary callback function InfoFun is + to return information about the table. undefined + is to be returned if the value of some tag is unknown:

+ + indices + Returns a list of indexed positions, a list of positive + integers. + is_unique_objects + Returns true if the objects returned by + TraverseFun are unique. + + keypos + Returns the position of the table key, a positive integer. + + is_sorted_key + Returns true if the objects returned by + TraverseFun are sorted on the key. + + num_of_objects + Returns the number of objects in the table, a non-negative + integer. + +
- {match_spec, MatchExpression}. 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 - TraverseFun(MatchExpression). + +

Unary callback function FormatFun + is used by info/1,2 + for displaying the call that created the query handle of the + table. Defaults to undefined, which means that + info/1,2 displays a call to '$MOD':'$FUN'/0. + It is up to FormatFun 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 is added by info/1,2 though).

+

FormatFun 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 qlc:table/2 occurs. The argument can have the + following values:

+ + {lookup, Position, Keys, NElements, DepthFun}. + +

LookupFun is used for looking up objects in the + table.

+
+ {match_spec, MatchExpression} + +

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 + TraverseFun(MatchExpression).

+
+ {all, NElements, DepthFun} + +

No optimization was found. A match specification matching + all objects is used if TraverseFun is unary.

+

NElements is the value of the info/1,2 option + n_elements.

+

DepthFun is a function that can be used for + limiting the size of terms; calling + DepthFun(Term) substitutes '...' for + parts of Term below the depth specified by the + info/1,2 option depth.

+

If calling FormatFun with an + argument including NElements and + DepthFun fails, FormatFun + is called once again with an argument excluding + NElements and DepthFun + ({lookup, Position, Keys} or + all).

+
+
- {all, NElements, DepthFun}. No optimization was - found. A match specification matching all objects will be - used if TraverseFun is unary. +

The value of option + key_equality is to be '=:=' if the table + considers two keys equal if they match, and to be + '==' if two keys are equal if they compare equal. + Defaults to '=:='.

- -

NElements is the value of the info/1,2 option - n_elements, and DepthFun is a function that - can be used for limiting the size of terms; calling - DepthFun(Term) substitutes '...' for parts of - Term below the depth specified by the info/1,2 - option depth. If calling - FormatFun with an argument including - NElements and DepthFun fails, - FormatFun is called once again with an - argument excluding - NElements and DepthFun - ({lookup, Position, Keys} or - all).

- -

The value of - key_equality is to be '=:=' if the table - considers two keys equal if they match, and to be - '==' if two keys are equal if they compare equal. The - default is '=:='.

- -

See ets(3), - dets(3) and - mnesia(3) - for the various options recognized by table/1,2 in - respective module.

+

For the various options recognized by table/1,2 + in respective module, see + ets(3), + dets(3), and + mnesia(3). +

-
See Also -

dets(3), +

dets(3), + erl_eval(3), + erlang(3), + error_logger(3), + ets(3), + file(3), + file_sorter(3), + mnesia(3), + shell(3), - Erlang Reference Manual, - erl_eval(3), - erlang(3), - ets(3), - file(3), - error_logger(3), - file_sorter(3), - mnesia(3), + Erlang Reference Manual, - Programming Examples, - shell(3)

+ Programming Examples

- -- cgit v1.2.3