org.exist.xquery
Class GeneralComparison

java.lang.Object
  extended by org.exist.xquery.AbstractExpression
      extended by org.exist.xquery.PathExpr
          extended by org.exist.xquery.BinaryOp
              extended by org.exist.xquery.GeneralComparison
All Implemented Interfaces:
CompiledXQuery, Expression, IndexUseReporter, Optimizable, RewritableExpression, org.xmldb.api.base.CompiledExpression
Direct Known Subclasses:
ValueComparison

public class GeneralComparison
extends BinaryOp
implements Optimizable, IndexUseReporter

A general XQuery/XPath2 comparison expression.


Nested Class Summary
static class GeneralComparison.IndexFlags
           
 
Field Summary
protected  CachedResult cached
          The class might cache the entire results of a previous execution.
protected  java.lang.Object collationArg
          Extra argument (to standard functions starts-with/contains etc.) to indicate the collation to be used for string comparisons.
protected  boolean invalidNodeEvaluation
           
protected  boolean inWhereClause
          Set to true if this expression is called within the where clause of a FLWOR expression.
protected  boolean optimizeChild
           
protected  boolean optimizeSelf
           
protected  int relation
          The type of operator used for the comparison, i.e.
protected  int rightOpDeps
           
protected  int truncation
          Truncation flags: when comparing with a string value, the search string may be truncated with a single * wildcard.
 
Fields inherited from class org.exist.xquery.PathExpr
inPredicate, keepVirtual, LOG, parent, staticContext, steps
 
Fields inherited from class org.exist.xquery.AbstractExpression
column, context, contextDocSet, contextId, line, unordered
 
Fields inherited from interface org.exist.xquery.Expression
DOT_TEST, EXPRESSION_ID_INVALID, IGNORE_CONTEXT, IN_NODE_CONSTRUCTOR, IN_PREDICATE, IN_UPDATE, IN_WHERE_CLAUSE, NEED_INDEX_INFO, NO_CONTEXT_ID, NON_STREAMABLE, POSITIONAL_PREDICATE, SINGLE_STEP_EXECUTION, UNORDERED, USE_TREE_TRAVERSAL
 
Constructor Summary
GeneralComparison(XQueryContext context, Expression left, Expression right, int relation)
           
GeneralComparison(XQueryContext context, Expression left, Expression right, int relation, int truncation)
           
GeneralComparison(XQueryContext context, int relation)
           
GeneralComparison(XQueryContext context, int relation, int truncation)
           
 
Method Summary
 void accept(ExpressionVisitor visitor)
          Start traversing the expression tree using the specified ExpressionVisitor.
 void analyze(AnalyzeContextInfo contextInfo)
          Statically analyze the expression and its subexpressions.
 boolean canOptimize(Sequence contextSequence)
           
static GeneralComparison.IndexFlags checkForQNameIndex(GeneralComparison.IndexFlags idxflags, XQueryContext context, Sequence contextSequence, QName contextQName)
           
 void dump(ExpressionDumper dumper)
          Write a diagnostic dump of the expression to the passed ExpressionDumper.
 Sequence eval(Sequence contextSequence, Item contextItem)
          Evaluate the expression represented by this object.
protected  Sequence genericCompare(Sequence contextSequence, Item contextItem)
          Generic, slow implementation.
protected  Sequence genericCompare(Sequence ls, Sequence contextSequence, Item contextItem)
           
protected  java.text.Collator getCollator(Sequence contextSequence)
           
 int getDependencies()
          Returns Dependency.DEFAULT_DEPENDENCIES.
 int getOptimizeAxis()
           
 int getRelation()
           
 boolean hasUsedIndex()
           
protected  Sequence nodeSetCompare(NodeSet nodes, Sequence contextSequence)
          Optimized implementation, which can be applied if the left operand returns a node set.
 boolean optimizeOnChild()
           
 boolean optimizeOnSelf()
           
 NodeSet preSelect(Sequence contextSequence, boolean useContext)
           
protected  Sequence quickNodeSetCompare(Sequence contextSequence)
          Optimized implementation: first checks if a range index is defined on the nodes in the left argument.
 void resetState(boolean postOptimization)
          Called to inform an expression that it should reset to its initial state.
 int returnsType()
          The static return type of the expression.
 void setCollation(java.lang.Object collationArg)
           
protected  void simplifyOperands()
          Possibly switch operands to simplify execution.
protected  void switchOperands()
           
 java.lang.String toString()
           
 
Methods inherited from class org.exist.xquery.BinaryOp
getLeft, getRight, setContextDocSet, setLeft, setRight, simplify
 
Methods inherited from class org.exist.xquery.PathExpr
add, add, addPath, addPredicate, allowMixNodesInReturn, dump, getCardinality, getColumn, getContext, getDocumentSet, getExpression, getLastExpression, getLength, getLine, getLiteralValue, getParent, getPrimaryAxis, getSubExpression, getSubExpressionCount, isValid, replaceExpression, replaceLastExpression, reset, setContext, setPrimaryAxis, setUseStaticContext
 
Methods inherited from class org.exist.xquery.AbstractExpression
eval, getContextDocSet, getContextId, getExpressionId, getSource, match, needsReset, setASTNode, setContextId, setLocation
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.exist.xquery.Expression
allowMixNodesInReturn, eval, getCardinality, getColumn, getContext, getContextDocSet, getContextId, getExpressionId, getLine, getParent, getPrimaryAxis, getSource, getSubExpression, getSubExpressionCount, match, needsReset, setASTNode, setContextDocSet, setContextId, setLocation, setPrimaryAxis, simplify
 
Methods inherited from interface org.exist.xquery.CompiledXQuery
eval, getSource
 

Field Detail

relation

protected int relation
The type of operator used for the comparison, i.e. =, !=, <, > ... One of the constants declared in class Constants.


truncation

protected int truncation
Truncation flags: when comparing with a string value, the search string may be truncated with a single * wildcard. See the constants declared in class Constants.

The standard functions starts-with, ends-with and contains are transformed into a general comparison with wildcard. Hence the need to consider wildcards here.


cached

protected CachedResult cached
The class might cache the entire results of a previous execution.


collationArg

protected java.lang.Object collationArg
Extra argument (to standard functions starts-with/contains etc.) to indicate the collation to be used for string comparisons.


inWhereClause

protected boolean inWhereClause
Set to true if this expression is called within the where clause of a FLWOR expression.


invalidNodeEvaluation

protected boolean invalidNodeEvaluation

rightOpDeps

protected int rightOpDeps

optimizeSelf

protected boolean optimizeSelf

optimizeChild

protected boolean optimizeChild
Constructor Detail

GeneralComparison

public GeneralComparison(XQueryContext context,
                         int relation)

GeneralComparison

public GeneralComparison(XQueryContext context,
                         int relation,
                         int truncation)

GeneralComparison

public GeneralComparison(XQueryContext context,
                         Expression left,
                         Expression right,
                         int relation)

GeneralComparison

public GeneralComparison(XQueryContext context,
                         Expression left,
                         Expression right,
                         int relation,
                         int truncation)
Method Detail

analyze

public void analyze(AnalyzeContextInfo contextInfo)
             throws XPathException
Description copied from interface: Expression
Statically analyze the expression and its subexpressions. During the static analysis phase, the query engine can detect unknown variables and some type errors.

Specified by:
analyze in interface Expression
Overrides:
analyze in class BinaryOp
Throws:
XPathException

canOptimize

public boolean canOptimize(Sequence contextSequence)
Specified by:
canOptimize in interface Optimizable

optimizeOnSelf

public boolean optimizeOnSelf()
Specified by:
optimizeOnSelf in interface Optimizable

optimizeOnChild

public boolean optimizeOnChild()
Specified by:
optimizeOnChild in interface Optimizable

getOptimizeAxis

public int getOptimizeAxis()
Specified by:
getOptimizeAxis in interface Optimizable

returnsType

public int returnsType()
Description copied from interface: Expression
The static return type of the expression. This method should return one of the type constants defined in class Type. If the return type cannot be determined statically, return Type.ITEM.

Specified by:
returnsType in interface Expression
Overrides:
returnsType in class BinaryOp

getDependencies

public int getDependencies()
Description copied from class: AbstractExpression
Returns Dependency.DEFAULT_DEPENDENCIES.

Specified by:
getDependencies in interface Expression
Overrides:
getDependencies in class PathExpr
Returns:
set of bit-flags
See Also:
Expression.getDependencies()

getRelation

public int getRelation()

preSelect

public NodeSet preSelect(Sequence contextSequence,
                         boolean useContext)
                  throws XPathException
Specified by:
preSelect in interface Optimizable
Throws:
XPathException

eval

public Sequence eval(Sequence contextSequence,
                     Item contextItem)
              throws XPathException
Description copied from interface: Expression
Evaluate the expression represented by this object. Depending on the context in which this expression is executed, either the context sequence, the context item or both of them may be set. An implementing class should know how to handle this. The general contract is as follows: if the Dependency.CONTEXT_ITEM bit is set in the bit field returned by Expression.getDependencies(), the eval method will be called once for every item in the context sequence. The contextItem parameter will be set to the current item. Otherwise, the eval method will only be called once for the whole context sequence and contextItem will be null. eXist tries to process the entire context set in one, single step whenever possible. Thus, most classes only expect context to contain a list of nodes which represents the current context of the expression. The position() function in XPath is an example for an expression, which requires both, context sequence and context item to be set. The context sequence might be a node set, a sequence of atomic values or a single node or atomic value.

Specified by:
eval in interface Expression
Specified by:
eval in class BinaryOp
Parameters:
contextSequence - the current context sequence.
contextItem - a single item, taken from context. This defines the item, the expression should work on.
Throws:
XPathException

genericCompare

protected Sequence genericCompare(Sequence contextSequence,
                                  Item contextItem)
                           throws XPathException
Generic, slow implementation. Applied if none of the possible optimizations can be used.

Parameters:
contextSequence -
contextItem -
Returns:
The Sequence resulting from the comparison
Throws:
XPathException

genericCompare

protected Sequence genericCompare(Sequence ls,
                                  Sequence contextSequence,
                                  Item contextItem)
                           throws XPathException
Throws:
XPathException

nodeSetCompare

protected Sequence nodeSetCompare(NodeSet nodes,
                                  Sequence contextSequence)
                           throws XPathException
Optimized implementation, which can be applied if the left operand returns a node set. In this case, the left expression is executed first. All matching context nodes are then passed to the right expression.

Parameters:
nodes - DOCUMENT ME!
contextSequence - DOCUMENT ME!
Returns:
DOCUMENT ME!
Throws:
XPathException - DOCUMENT ME!

quickNodeSetCompare

protected Sequence quickNodeSetCompare(Sequence contextSequence)
                                throws XPathException
Optimized implementation: first checks if a range index is defined on the nodes in the left argument. If that fails, check if we can use the fulltext index to speed up the search. Otherwise, fall back to nodeSetCompare(NodeSet, Sequence).

Parameters:
contextSequence - DOCUMENT ME!
Returns:
DOCUMENT ME!
Throws:
XPathException - DOCUMENT ME!

hasUsedIndex

public boolean hasUsedIndex()
Specified by:
hasUsedIndex in interface IndexUseReporter

dump

public void dump(ExpressionDumper dumper)
Description copied from interface: Expression
Write a diagnostic dump of the expression to the passed ExpressionDumper.

Specified by:
dump in interface Expression
Overrides:
dump in class PathExpr
Parameters:
dumper - the expression dumper to write to

toString

public java.lang.String toString()
Overrides:
toString in class PathExpr

switchOperands

protected void switchOperands()

simplifyOperands

protected void simplifyOperands()
Possibly switch operands to simplify execution.


getCollator

protected java.text.Collator getCollator(Sequence contextSequence)
                                  throws XPathException
Throws:
XPathException

setCollation

public void setCollation(java.lang.Object collationArg)

checkForQNameIndex

public static final GeneralComparison.IndexFlags checkForQNameIndex(GeneralComparison.IndexFlags idxflags,
                                                                    XQueryContext context,
                                                                    Sequence contextSequence,
                                                                    QName contextQName)

resetState

public void resetState(boolean postOptimization)
Description copied from interface: Expression
Called to inform an expression that it should reset to its initial state. All cached data in the expression object should be dropped. For example, the xmldb:document() function calls this method whenever the input document set has changed.

Specified by:
resetState in interface Expression
Overrides:
resetState in class PathExpr

accept

public void accept(ExpressionVisitor visitor)
Description copied from interface: Expression
Start traversing the expression tree using the specified ExpressionVisitor.

Specified by:
accept in interface Expression
Overrides:
accept in class PathExpr