public Cursor query(IndexPlan plan, NodeState root) {
LOG.debug("query(IndexPlan, NodeState)");
LOG.debug("query() - plan: {}", plan);
LOG.debug("query() - rootState: {}", root);
Filter filter = plan.getFilter();
List<OrderEntry> sortOrder = plan.getSortOrder();
String pathPrefix = plan.getPathPrefix();
Iterable<String> paths = null;
OrderedContentMirrorStoreStrategy strategy
= OrderedPropertyIndexLookup.getStrategy(plan.getDefinition());
int depth = 1;
PropertyRestriction pr = plan.getPropertyRestriction();
if (pr != null) {
String propertyName = PathUtils.getName(pr.propertyName);
depth = PathUtils.getDepth(pr.propertyName);
paths = strategy.query(plan.getFilter(), propertyName,
plan.getDefinition(), pr, pathPrefix);
}
if (paths == null && sortOrder != null && !sortOrder.isEmpty()) {
// we could be here if we have a query where the ORDER BY makes us play it.
for (OrderEntry oe : sortOrder) {
String propertyName = PathUtils.getName(oe.getPropertyName());
depth = PathUtils.getDepth(oe.getPropertyName());
paths = strategy.query(plan.getFilter(), propertyName,
plan.getDefinition(), new PropertyRestriction(), pathPrefix);
}
}
if (paths == null) {
// if still here then something went wrong.
throw new IllegalStateException(
"OrderedPropertyIndex index is used even when no index is available for filter "
+ filter);
}
Cursor cursor = Cursors.newPathCursor(paths, filter.getQueryEngineSettings());
if (depth > 1) {
cursor = Cursors.newAncestorCursor(cursor, depth - 1, filter.getQueryEngineSettings());
}
return cursor;
}