lux.compiler
Class PathOptimizer

java.lang.Object
  extended by lux.xpath.ExpressionVisitor
      extended by lux.xpath.ExpressionVisitorBase
          extended by lux.compiler.PathOptimizer

public class PathOptimizer
extends ExpressionVisitorBase

Prepares an XPath expression tree for indexed execution against a Lux data store. This class is part of the Lux internal API and is not intended to be called by consumers of the API. The general strategy here is to consider each expression in isolation, determining whether it imposes any restriction on its context, and then to compose such constraints into queries, to be executed by a searcher, with the XPath/XQuery expressions evaluated against the resulting documents. Absolute expressions are targets for optimization; the optimizer attempts to form queries that retrieve the smallest possible subset of available documents for which the expression generates a non-empty result sequence. The queries for these sub-expressions are composed according to the semantics of the combining expressions and functions. Absolute sequences (occurrences of /) generally are not composed together - they form independent subqueries. When optimizing for path indexes, we attempt to compute the "vertical" or node distances between adjacent sub-expressions. Child path steps introduce a zero distance when there is a named step on either side; wildcard child steps introduce a distance of one, and descendant (or ancestor?) steps count as infinite distance, although order is preserved.


Constructor Summary
PathOptimizer(IndexConfiguration indexConfig)
           
 
Method Summary
 AbstractExpression getBoundExpression(QName name)
           
 boolean isOptimizedForOrderedResults()
           
static ParseableQuery makeAttributeValueQuery(QName qname, String value, IndexConfiguration config)
           
static NodeTextQuery makeElementValueQuery(QName qname, String value, IndexConfiguration config)
           
static ParseableQuery makeTextQuery(String value, IndexConfiguration config)
           
 AbstractExpression optimize(AbstractExpression expr)
          Prepares an XQuery expression for indexed execution against a Lux data store.
 XQuery optimize(XQuery query)
          Prepares an XQuery module for indexed execution against a Lux data store.
 XPathQuery peek()
           
 void setSearchStrategy(Compiler.SearchStrategy searchStrategy)
           
 AbstractExpression visit(BinaryOperation op)
           
 AbstractExpression visit(Dot dot)
           
 AbstractExpression visit(FLWOR flwor)
           Optimizing FLWOR expressions is more complicated than path expressions and other simpler XPath expressions because the relationship among the clauses is not a simple dependency, but is mediated by variables.
 ForClause visit(ForClause forClause)
           
 AbstractExpression visit(FunCall funcall)
          If a function F is emptiness-preserving, in other words F(a,b,c...) is empty ( =()) if *any* of its arguments are empty, and is non-empty if *all* of its arguments are non-empty, then its argument's queries can be combined with Occur.MUST.
 LetClause visit(LetClause letClause)
           
 AbstractExpression visit(LiteralExpression literal)
           
 OrderByClause visit(OrderByClause orderByClause)
           
 AbstractExpression visit(PathExpression pathExpr)
          Conjoin the queries for the two expressions joined by the path.
 AbstractExpression visit(PathStep step)
           
 AbstractExpression visit(Predicate predicate)
           
 AbstractExpression visit(Root expr)
           
 AbstractExpression visit(Sequence sequence)
           
 AbstractExpression visit(Subsequence subsequence)
           
 AbstractExpression visit(Variable variable)
           
 WhereClause visit(WhereClause whereClause)
           
 AbstractExpression visitDefault(AbstractExpression expr)
          This method is called by every visit() method in this class.
 
Methods inherited from class lux.xpath.ExpressionVisitorBase
visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visitSubs
 
Methods inherited from class lux.xpath.ExpressionVisitor
isDone, isReverse, setReverse
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PathOptimizer

public PathOptimizer(IndexConfiguration indexConfig)
Method Detail

optimize

public XQuery optimize(XQuery query)
Prepares an XQuery module for indexed execution against a Lux data store. See optimize(AbstractExpression).

Parameters:
query - the query to optimize
Returns:
the optimized query

optimize

public AbstractExpression optimize(AbstractExpression expr)
Prepares an XQuery expression for indexed execution against a Lux data store. Inserts calls to lux:search that execute queries selecting a set of documents against which the expression, or some of its sub-expressions, are evaluated. The queries will always select a superset of the documents that actually contribute results so that the result is the same as if the expression was evaluated against all the documents in collection() as its context (sequence of context items?).

Parameters:
expr - the expression to optimize
Returns:
the optimized expression

peek

public XPathQuery peek()

visit

public AbstractExpression visit(Root expr)
Overrides:
visit in class ExpressionVisitorBase

visit

public AbstractExpression visit(PathExpression pathExpr)
Conjoin the queries for the two expressions joined by the path. We handle predicates as separate path queries, to be combined with AND, not as path steps

Overrides:
visit in class ExpressionVisitorBase

visit

public AbstractExpression visit(Predicate predicate)
Overrides:
visit in class ExpressionVisitorBase

visit

public AbstractExpression visit(PathStep step)
Overrides:
visit in class ExpressionVisitorBase

visit

public AbstractExpression visit(FunCall funcall)
If a function F is emptiness-preserving, in other words F(a,b,c...) is empty ( =()) if *any* of its arguments are empty, and is non-empty if *all* of its arguments are non-empty, then its argument's queries can be combined with Occur.MUST. At first I thought that was the way most functions work, but it's not: string(()) = '', not () Otherwise, no optimization is possible in the general case, which we indicate by combining with Occur.SHOULD. count(), exists() and empty() (and maybe later max(), min(), and avg()? and string()?) are optimized as special cases.

Overrides:
visit in class ExpressionVisitorBase
Parameters:
funcall - the function call expression to optimize
Returns:
the same function call expression, after having possibly optimized its arguments

visit

public AbstractExpression visit(Dot dot)
Overrides:
visit in class ExpressionVisitorBase

visit

public AbstractExpression visit(BinaryOperation op)
Overrides:
visit in class ExpressionVisitorBase

makeElementValueQuery

public static NodeTextQuery makeElementValueQuery(QName qname,
                                                  String value,
                                                  IndexConfiguration config)

makeAttributeValueQuery

public static ParseableQuery makeAttributeValueQuery(QName qname,
                                                     String value,
                                                     IndexConfiguration config)

makeTextQuery

public static ParseableQuery makeTextQuery(String value,
                                           IndexConfiguration config)

visit

public AbstractExpression visit(LiteralExpression literal)
Overrides:
visit in class ExpressionVisitorBase

visit

public AbstractExpression visit(Variable variable)
Overrides:
visit in class ExpressionVisitorBase

visit

public AbstractExpression visit(Subsequence subsequence)
Overrides:
visit in class ExpressionVisitorBase

visit

public AbstractExpression visit(Sequence sequence)
Overrides:
visit in class ExpressionVisitorBase

visitDefault

public AbstractExpression visitDefault(AbstractExpression expr)
Description copied from class: ExpressionVisitorBase
This method is called by every visit() method in this class. Subclasses may extend this convenience method in order to provide a default behavior for all expressions for which they don't provide an explicit visit() override.

Overrides:
visitDefault in class ExpressionVisitorBase
Parameters:
expr - an expression to visit
Returns:
the expression

visit

public AbstractExpression visit(FLWOR flwor)

Optimizing FLWOR expressions is more complicated than path expressions and other simpler XPath expressions because the relationship among the clauses is not a simple dependency, but is mediated by variables.

The strategy is to use constraints from each clause and its dependent for, where and return clauses to filter that clause's sequence. Dependent let and order by clauses are not considered when filtering enclosing clauses. Order by clauses *do* contribute to ordering relations in their enclosing for clause and may be folded together with them when searchable.

Additionally, constraints may come in via variable references, but this is not handled explicitly as part of the FLWOR optimization; rather it is handled in the visit method for each clause.

Overrides:
visit in class ExpressionVisitorBase

visit

public OrderByClause visit(OrderByClause orderByClause)
Overrides:
visit in class ExpressionVisitorBase

visit

public ForClause visit(ForClause forClause)
Overrides:
visit in class ExpressionVisitorBase

visit

public WhereClause visit(WhereClause whereClause)
Overrides:
visit in class ExpressionVisitorBase

visit

public LetClause visit(LetClause letClause)
Overrides:
visit in class ExpressionVisitorBase

getBoundExpression

public AbstractExpression getBoundExpression(QName name)

isOptimizedForOrderedResults

public boolean isOptimizedForOrderedResults()
Returns:
whether to generate code that relies on our ability to assert that lux:search() returns results in "document order" - ie ordered by document ID.

setSearchStrategy

public void setSearchStrategy(Compiler.SearchStrategy searchStrategy)


Copyright © 2013. All Rights Reserved.