final long start = System.currentTimeMillis();
if (contextItem != null)
{contextSequence = contextItem.toSequence();}
if (preselectResult == null && !checkForQNameIndex(contextSequence))
{contextQName = null;}
NodeSet result;
//If the expression does not depend on the current context item,
//we can evaluate it in one single step
if (path == null || !Dependency.dependsOn(path, Dependency.CONTEXT_ITEM)) {
final boolean canCache =
!Dependency.dependsOn(searchTerm, Dependency.CONTEXT_ITEM) &&
!Dependency.dependsOnVar(searchTerm);
if (canCache && cached != null && cached.isValid(contextSequence, contextItem)) {
return cached.getResult();
}
//Do we optimize this expression?
if (contextStep == null || preselectResult == null) {
//No optimization: process the whole expression
final NodeSet nodes = path == null ?
contextSequence.toNodeSet() : path.eval(contextSequence).toNodeSet();
final String arg = searchTerm.eval(contextSequence).getStringValue();
result = evalQuery(arg, nodes).toNodeSet();
if (context.getProfiler().traceFunctions())
{context.getProfiler().traceIndexUsage(context, FTIndex.ID, this,
PerformanceStats.BASIC_INDEX, System.currentTimeMillis() - start);}
} else {
contextStep.setPreloadedData(contextSequence.getDocumentSet(), preselectResult);
result = path.eval(contextSequence).toNodeSet();
}
if(canCache && contextSequence != null && contextSequence.isCacheable())
{cached = new CachedResult(contextSequence, contextItem, result);}
//Otherwise we have to walk through each item in the context
} else {
Item current;
String arg;
NodeSet nodes;
result = new ExtArrayNodeSet();
Sequence temp;
for (final SequenceIterator i = contextSequence.iterate(); i.hasNext();) {
current = i.nextItem();
arg = searchTerm.eval(current.toSequence()).getStringValue();