@Immutable public class ScanningQueryEngine extends Object implements QueryEngine
QueryEngine
implementation that always scans all nodes in the workspace(s) and filtering out any node that does not
satisfy the criteria. This scanning is not very efficient and can result in slow queries, especially when the repository is
quite large or when the number of nodes that satisfies the query's criteria is a small fraction of all possible nodes in the
workspace(s).
However, this fully-functional QueryEngine implementation is designed to be subclassed when the nodes for a particular source
(and optionally criteria) can be found more quickly. In such cases, the subclass should override the
#createNodeSequenceForSource(QueryCommand, QueryContext, PlanNode, Columns, QuerySources)
method or the
#createNodeSequenceForSource(QueryCommand, QueryContext, PlanNode, IndexPlan, Columns, QuerySources)
method and return
a NodeSequence that contains only the applicable nodes.
Modifier and Type | Class and Description |
---|---|
static class |
ScanningQueryEngine.Builder |
protected static class |
ScanningQueryEngine.DynamicOperandFilter
Interface for evaluating a
DynamicOperand against the current row in a NodeSequence.Batch and returning the
corresponding value. |
protected static class |
ScanningQueryEngine.PropertyValueExtractor |
static class |
ScanningQueryEngine.ResultColumns |
protected static class |
ScanningQueryEngine.RowFilterSupplier
A
NodeSequence.RowFilter implementation that lazily initializes the real RowFilter implementation the first time it's needed and
thereafter will simply delegate to the implementation. |
Modifier and Type | Field and Description |
---|---|
protected static Logger |
LOGGER
We don't use the standard logging convention here; we want clients to easily configure logging for the indexes
|
protected Optimizer |
optimizer |
protected Planner |
planner |
protected String |
repositoryName |
Constructor and Description |
---|
ScanningQueryEngine(ExecutionContext context,
String repositoryName,
Planner planner,
Optimizer optimizer) |
Modifier and Type | Method and Description |
---|---|
static ScanningQueryEngine.Builder |
builder() |
protected boolean |
checkUnionCompatible(QueryResults.Columns results1,
QueryResults.Columns results2,
org.modeshape.jcr.query.engine.ScanningQueryEngine.ScanQueryContext context,
QueryCommand query)
Determine whether this column and the other are union-compatible (that is, having the same columns).
|
protected RowExtractors.ExtractFromRow |
createExtractFromRow(DynamicOperand operand,
QueryContext context,
QueryResults.Columns columns,
QuerySources sources,
TypeSystem.TypeFactory<?> defaultType,
boolean allowMultiValued,
boolean isLike)
Create a
RowExtractors.ExtractFromRow implementation that performs the supplied DynamicOperand against a current row in
the current batch. |
protected RowExtractors.ExtractFromRow |
createExtractFromRow(String selectorName,
String propertyName,
QueryContext context,
QueryResults.Columns columns,
QuerySources sources,
TypeSystem.TypeFactory<?> defaultType,
boolean allowMultiValued)
Create a
RowExtractors.ExtractFromRow implementation that accesses the value(s) in the property identified by the supplied
selector and property names. |
protected RowExtractors.ExtractFromRow |
createExtractReferencesFromRow(String selectorName,
QueryContext context,
QueryResults.Columns columns,
QuerySources sources,
TypeSystem.TypeFactory<?> defaultType)
Create a
RowExtractors.ExtractFromRow implementation that accesses the REFERENCE value(s) in the properties of the node
identified by the supplied selector names. |
protected NodeSequence |
createNodeSequence(QueryCommand originalQuery,
org.modeshape.jcr.query.engine.ScanningQueryEngine.ScanQueryContext context,
PlanNode plan,
QueryResults.Columns columns,
QuerySources sources)
Create a node sequence containing the results of the original query as defined by the supplied plan.
|
protected NodeSequence |
createNodeSequenceForSource(QueryCommand originalQuery,
QueryContext context,
PlanNode sourceNode,
IndexPlan index,
QueryResults.Columns columns,
QuerySources sources)
Create a node sequence for the given index
|
protected NodeSequence |
createNodeSequenceForSource(QueryCommand originalQuery,
QueryContext context,
PlanNode sourceNode,
QueryResults.Columns columns,
QuerySources sources)
Create a node sequence for the given source.
|
QueryContext |
createQueryContext(ExecutionContext context,
RepositoryCache repositoryCache,
Set<String> workspaceNames,
Map<String,NodeCache> overriddenNodeCachesByWorkspaceName,
Schemata schemata,
RepositoryIndexes indexDefns,
NodeTypes nodeTypes,
BufferManager bufferManager,
PlanHints hints,
Map<String,Object> variables)
Create a new context for query execution.
|
protected NodeSequence.RowFilter |
createRowFilter(Constraint constraint,
QueryContext context,
QueryResults.Columns columns,
QuerySources sources)
Create a
NodeSequence.RowFilter implementation given the supplied constraints. |
protected RowExtractors.ExtractFromRow |
createSortingExtractor(List<Ordering> orderings,
Map<SelectorName,SelectorName> sourceNamesByAlias,
QueryContext context,
QueryResults.Columns columns,
QuerySources sources)
Create an
RowExtractors.ExtractFromRow instance that produces for given row a single object that can be used to sort all rows in
the specified order. |
protected RowExtractors.ExtractFromRow |
createSortingExtractor(Ordering ordering,
Map<SelectorName,SelectorName> sourceNamesByAlias,
QueryContext context,
QueryResults.Columns columns,
QuerySources sources)
Create an
RowExtractors.ExtractFromRow instance that produces for given row a single object that can be used to sort all rows in
the specified order. |
protected QueryResults.Columns |
determineProjectedColumns(PlanNode optimizedPlan,
org.modeshape.jcr.query.engine.ScanningQueryEngine.ScanQueryContext context)
Compute the columns that are defined in the supplied
plan node . |
protected TypeSystem.TypeFactory<?> |
determineType(DynamicOperand operand,
QueryContext context,
QueryResults.Columns columns) |
QueryResults |
execute(QueryContext queryContext,
QueryCommand query)
Execute the supplied query by planning, optimizing, and then processing it.
|
protected QueryResults |
executeOptimizedQuery(org.modeshape.jcr.query.engine.ScanningQueryEngine.ScanQueryContext context,
QueryCommand command,
QueryResults.Statistics statistics,
PlanNode plan)
Execute the optimized query defined by the supplied
plan node . |
protected static Object |
literalValue(StaticOperand staticOperand,
QueryContext context,
TypeSystem.TypeFactory<?> type)
Get the literal value that is defined in the supplied
StaticOperand . |
protected static Set<?> |
literalValues(SetCriteria setCriteria,
QueryContext context,
TypeSystem.TypeFactory<?> type) |
void |
shutdown()
Signal that the engine is no longer needed and should clean up and/or close any resources.
|
static String |
toRegularExpression(String likeExpression)
Convert the JCR like expression to a regular expression.
|
protected static String |
toWildcardExpression(String likeExpression)
Convert the JCR like expression to a Lucene wildcard expression.
|
protected static final Logger LOGGER
protected final String repositoryName
protected final Planner planner
protected final Optimizer optimizer
public ScanningQueryEngine(ExecutionContext context, String repositoryName, Planner planner, Optimizer optimizer)
public static ScanningQueryEngine.Builder builder()
public QueryResults execute(QueryContext queryContext, QueryCommand query) throws QueryCancelledException, RepositoryException
execute
in interface QueryEngine
queryContext
- the context in which the query should be executed; same instance as returned by
createQueryContext(org.modeshape.jcr.ExecutionContext, org.modeshape.jcr.cache.RepositoryCache, java.util.Set<java.lang.String>, java.util.Map<java.lang.String, org.modeshape.jcr.cache.NodeCache>, org.modeshape.jcr.query.validate.Schemata, org.modeshape.jcr.RepositoryIndexes, org.modeshape.jcr.NodeTypes, org.modeshape.jcr.query.BufferManager, org.modeshape.jcr.query.plan.PlanHints, java.util.Map<java.lang.String, java.lang.Object>)
query
- the query that is to be executedIllegalArgumentException
- if the context or query references are nullQueryCancelledException
- if the query was cancelledRepositoryException
- if there was a problem executing the queryprotected boolean checkUnionCompatible(QueryResults.Columns results1, QueryResults.Columns results2, org.modeshape.jcr.query.engine.ScanningQueryEngine.ScanQueryContext context, QueryCommand query)
results1
- the first result set; may not be nullresults2
- the second result set; may not be nullcontext
- the query execution context; may not be nullquery
- the query being executed; may not be nullprotected QueryResults.Columns determineProjectedColumns(PlanNode optimizedPlan, org.modeshape.jcr.query.engine.ScanningQueryEngine.ScanQueryContext context)
plan node
. If the supplied plan node is not a
project node
, the method finds the first PROJECT node below the given node.optimizedPlan
- the optimized plan node in a query plan; may not be nullcontext
- the query context; may not be nullpublic void shutdown()
QueryEngine
shutdown
in interface QueryEngine
public QueryContext createQueryContext(ExecutionContext context, RepositoryCache repositoryCache, Set<String> workspaceNames, Map<String,NodeCache> overriddenNodeCachesByWorkspaceName, Schemata schemata, RepositoryIndexes indexDefns, NodeTypes nodeTypes, BufferManager bufferManager, PlanHints hints, Map<String,Object> variables)
QueryEngine
createQueryContext
in interface QueryEngine
context
- the context in which the query is being executed; may not be nullrepositoryCache
- the repository cache that should be used to load results; may be null if no results are to be loadedworkspaceNames
- the name of each workspace to be queried, or an empty set if all the workspaces should be queried;
may not be nulloverriddenNodeCachesByWorkspaceName
- the NodeCache instances that should be used to load results, which will be used
instead of the RepositoryCache's NodeCache for a given workspace name; may be null or emptyschemata
- the schemataindexDefns
- the definitions for the currently-defined indexes; never nullnodeTypes
- the snapshot of node types; may not be nullbufferManager
- the buffer manager; may not be nullhints
- the hints, or null if there are no hintsvariables
- the mapping of variables and values, or null if there are no such variablesprotected QueryResults executeOptimizedQuery(org.modeshape.jcr.query.engine.ScanningQueryEngine.ScanQueryContext context, QueryCommand command, QueryResults.Statistics statistics, PlanNode plan)
plan node
.context
- the context in which the query is to be executed; may not be nullcommand
- the original query; may not be nullstatistics
- the statistics for the current query executionplan
- the optimized plan for the query; may not be nullprotected NodeSequence createNodeSequence(QueryCommand originalQuery, org.modeshape.jcr.query.engine.ScanningQueryEngine.ScanQueryContext context, PlanNode plan, QueryResults.Columns columns, QuerySources sources)
originalQuery
- the original query command; may not be nullcontext
- the context in which the query is to be executed; may not be nullplan
- the optimized plan for the query; may not be nullcolumns
- the result column definition; may not be nullsources
- the query sources for the repository; may not be nullprotected NodeSequence createNodeSequenceForSource(QueryCommand originalQuery, QueryContext context, PlanNode sourceNode, QueryResults.Columns columns, QuerySources sources)
originalQuery
- the original query command; may not be nullcontext
- the context in which the query is to be executed; may not be nullsourceNode
- the PlanNode.Type.SOURCE
plan node for one part of a query; may not be nullcolumns
- the result column definition; may not be nullsources
- the query sources for the repository; may not be nullprotected NodeSequence createNodeSequenceForSource(QueryCommand originalQuery, QueryContext context, PlanNode sourceNode, IndexPlan index, QueryResults.Columns columns, QuerySources sources)
originalQuery
- the original query command; may not be nullcontext
- the context in which the query is to be executed; may not be nullsourceNode
- the PlanNode.Type.SOURCE
plan node for one part of a query; may not be nullindex
- the IndexPlan
specification; may not be nullcolumns
- the result column definition; may not be nullsources
- the query sources for the repository; may not be nullprotected RowExtractors.ExtractFromRow createSortingExtractor(List<Ordering> orderings, Map<SelectorName,SelectorName> sourceNamesByAlias, QueryContext context, QueryResults.Columns columns, QuerySources sources)
RowExtractors.ExtractFromRow
instance that produces for given row a single object that can be used to sort all rows in
the specified order.orderings
- the specification of the sort order; may not be null or emptysourceNamesByAlias
- the map of selector names keyed by their aliases; may not be null but may be emptycontext
- the context in which the query is to be executed; may not be nullcolumns
- the result column definition; may not be nullsources
- the query sources for the repository; may not be nullprotected RowExtractors.ExtractFromRow createSortingExtractor(Ordering ordering, Map<SelectorName,SelectorName> sourceNamesByAlias, QueryContext context, QueryResults.Columns columns, QuerySources sources)
RowExtractors.ExtractFromRow
instance that produces for given row a single object that can be used to sort all rows in
the specified order.ordering
- the specification of the sort order; may not be null or emptysourceNamesByAlias
- the map of selector names keyed by their aliases; may not be null but may be emptycontext
- the context in which the query is to be executed; may not be nullcolumns
- the result column definition; may not be nullsources
- the query sources for the repository; may not be nullprotected NodeSequence.RowFilter createRowFilter(Constraint constraint, QueryContext context, QueryResults.Columns columns, QuerySources sources)
NodeSequence.RowFilter
implementation given the supplied constraints. The resulting filter can be applied to a
NodeSequence by using NodeSequence.filter(NodeSequence, RowFilter)
.constraint
- the constraints to be applied by the filter; may not be null;context
- the context in which the query is to be executed; may not be nullcolumns
- the result column definition; may not be nullsources
- the query sources for the repository; may not be nullprotected TypeSystem.TypeFactory<?> determineType(DynamicOperand operand, QueryContext context, QueryResults.Columns columns)
protected RowExtractors.ExtractFromRow createExtractFromRow(DynamicOperand operand, QueryContext context, QueryResults.Columns columns, QuerySources sources, TypeSystem.TypeFactory<?> defaultType, boolean allowMultiValued, boolean isLike)
RowExtractors.ExtractFromRow
implementation that performs the supplied DynamicOperand
against a current row in
the current batch.operand
- the dynamic operandcontext
- the context in which the query is to be executed; may not be nullcolumns
- the result column definition; may not be nullsources
- the query sources for the repository; may not be nulldefaultType
- the type that should be used by default, or null if an exception should be thrown when the type for the
property name could not be determinedallowMultiValued
- true if the extractor called upon a particular node and multi-valued property return an Object[]
that contains all the resulting values of the property, or false if only the first value should be returnedisLike
- true if the result will be used in a LIKE operation, or false otherwise; this may affect the number of
results that will be returnedprotected RowExtractors.ExtractFromRow createExtractFromRow(String selectorName, String propertyName, QueryContext context, QueryResults.Columns columns, QuerySources sources, TypeSystem.TypeFactory<?> defaultType, boolean allowMultiValued)
RowExtractors.ExtractFromRow
implementation that accesses the value(s) in the property identified by the supplied
selector and property names.selectorName
- the name of the selector containing the node(s) to be accessed; may not be nullpropertyName
- the name of the property on the node(s) to be accessed; may not be nullcontext
- the context in which the query is to be executed; may not be nullcolumns
- the result column definition; may not be nullsources
- the query sources for the repository; may not be nulldefaultType
- the type that should be used by default, or null if an exception should be thrown when the type for the
property name could not be determinedallowMultiValued
- true if the extractor called upon a particular node and multi-valued property return an Object[]
that contains all the resulting values of the property, or false if only the first value should be returnedprotected RowExtractors.ExtractFromRow createExtractReferencesFromRow(String selectorName, QueryContext context, QueryResults.Columns columns, QuerySources sources, TypeSystem.TypeFactory<?> defaultType)
RowExtractors.ExtractFromRow
implementation that accesses the REFERENCE value(s) in the properties of the node
identified by the supplied selector names.selectorName
- the name of the selector containing the node(s) to be accessed; may not be nullcontext
- the context in which the query is to be executed; may not be nullcolumns
- the result column definition; may not be nullsources
- the query sources for the repository; may not be nulldefaultType
- the type that should be used by default, or null if an exception should be thrown when the type for the
property name could not be determinedprotected static Object literalValue(StaticOperand staticOperand, QueryContext context, TypeSystem.TypeFactory<?> type)
StaticOperand
. If the supplied static operand is a
BindVariableValue
, the the variable value is obtained from the variables
in the
QueryContext
. Otherwise, this method simply casts the StaticOperand
to a Literal
value.staticOperand
- the static operand; may be nullcontext
- the query context; may not be nulltype
- the type factory for the expected type; may be null if the value should be used as-isprotected static Set<?> literalValues(SetCriteria setCriteria, QueryContext context, TypeSystem.TypeFactory<?> type)
protected static String toWildcardExpression(String likeExpression)
likeExpression
- the like expression; may not be nullpublic static String toRegularExpression(String likeExpression)
likeExpression
- the like expression; may not be nullCopyright © 2008–2016 JBoss, a division of Red Hat. All rights reserved.