use of org.exist.dom.persistent.NodeProxy in project exist by eXist-db.
the class FunRoot method eval.
/* (non-Javadoc)
* @see org.exist.xquery.Expression#eval(org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
*/
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
if (context.getProfiler().isEnabled()) {
context.getProfiler().start(this);
context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
if (contextSequence != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
}
if (contextItem != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
}
}
Sequence seq;
Sequence result;
Item item;
if (contextItem != null) {
contextSequence = contextItem.toSequence();
}
if (contextSequence == null || contextSequence.isEmpty()) {
result = Sequence.EMPTY_SEQUENCE;
}
// If we have one argumment, we take it into account
if (getSignature().getArgumentCount() > 0) {
seq = getArgument(0).eval(contextSequence, contextItem);
} else // Otherwise, we take the context sequence and we iterate over it
{
seq = contextSequence;
}
if (seq == null) {
throw new XPathException(this, ErrorCodes.XPDY0002, "Undefined context item");
}
if (seq.isPersistentSet()) {
result = new ExtArrayNodeSet(seq.getItemCount());
} else {
result = new ValueSequence(seq.getItemCount());
}
for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
item = i.nextItem();
if (!Type.subTypeOf(item.getType(), Type.NODE)) {
throw new XPathException(this, ErrorCodes.XPTY0004, "Item is not a node; got '" + item + "'", seq);
}
final Sequence s = item.toSequence();
if (s.isPersistentSet()) {
final NodeProxy p = s.toNodeSet().get(0);
result.add(new NodeProxy(p.getOwnerDocument()));
} else {
if (seq.hasOne() && item.getType() == Type.ATTRIBUTE) {
result.add(item);
} else if (item.getType() == Type.DOCUMENT) {
result.add((DocumentImpl) item);
} else {
result.add(((NodeImpl) item).getOwnerDocument());
}
}
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.dom.persistent.NodeProxy in project exist by eXist-db.
the class IntersectTest method memtree_intersect_persistent.
/**
* Tests the XQuery `intersect` operator against an
* in-memory node on the left and a persistent node on the right
*/
@Test
public void memtree_intersect_persistent() throws XPathException, NoSuchMethodException {
final XQueryContext mockContext = createMock(XQueryContext.class);
final PathExpr mockLeft = createMock(PathExpr.class);
final PathExpr mockRight = createMock(PathExpr.class);
final Sequence mockContextSequence = createMock(Sequence.class);
final Item mockContextItem = createMock(Item.class);
final Profiler mockProfiler = createMock(Profiler.class);
final DocumentImpl mockPersistentDoc = createMock(DocumentImpl.class);
final NodeProxy mockPersistentNode = createMockBuilder(NodeProxy.class).withConstructor(DocumentImpl.class, NodeId.class).withArgs(mockPersistentDoc, new DLN(1)).addMockedMethods(NodeProxy.class.getMethod("isEmpty", new Class[] {}), NodeProxy.class.getMethod("getItemType", new Class[] {}), NodeProxy.class.getMethod("equals", new Class[] { Object.class })).createMock();
expect(mockContext.nextExpressionId()).andReturn(1);
expect(mockContext.getProfiler()).andReturn(mockProfiler);
// memtree node
expect(mockLeft.eval(mockContextSequence, mockContextItem)).andReturn((org.exist.dom.memtree.ElementImpl) createInMemoryDocument().getDocumentElement());
// persistent node
expect(mockRight.eval(mockContextSequence, mockContextItem)).andReturn(mockPersistentNode);
expect(mockPersistentNode.isEmpty()).andReturn(false);
expect(mockPersistentNode.getItemType()).andReturn(Type.NODE);
expect(mockContext.getProfiler()).andReturn(mockProfiler);
replay(mockPersistentDoc, mockPersistentNode, mockRight, mockLeft, mockContext);
// test
final Intersect intersect = new Intersect(mockContext, mockLeft, mockRight);
final Sequence result = intersect.eval(mockContextSequence, mockContextItem);
assertEquals(0, ((ValueSequence) result).size());
verify(mockPersistentDoc, mockPersistentNode, mockRight, mockLeft, mockContext);
}
use of org.exist.dom.persistent.NodeProxy in project exist by eXist-db.
the class LuceneMatchListener method reset.
protected void reset(final DBBroker broker, final NodeProxy proxy) {
this.broker = broker;
this.match = proxy.getMatches();
setNextInChain(null);
final IndexSpec indexConf = proxy.getOwnerDocument().getCollection().getIndexConfiguration(broker);
if (indexConf != null) {
config = (LuceneConfig) indexConf.getCustomIndexSpec(LuceneIndex.ID);
} else {
config = LuceneConfig.DEFAULT_CONFIG;
}
getTerms();
nodesWithMatch = new TreeMap<>();
/* Check if an index is defined on an ancestor of the current node.
* If yes, scan the ancestor to get the offset of the first character
* in the current node. For example, if the indexed node is <a>abc<b>de</b></a>
* and we query for //a[text:ngram-contains(., 'de')]/b, proxy will be a <b> node, but
* the offsets of the matches are relative to the start of <a>.
*/
NodeSet ancestors = null;
Match nextMatch = this.match;
while (nextMatch != null) {
if (proxy.getNodeId().isDescendantOf(nextMatch.getNodeId())) {
if (ancestors == null) {
ancestors = new NewArrayNodeSet();
}
ancestors.add(new NodeProxy(proxy.getOwnerDocument(), nextMatch.getNodeId()));
}
nextMatch = nextMatch.getNextMatch();
}
if (ancestors != null && !ancestors.isEmpty()) {
for (final NodeProxy p : ancestors) {
scanMatches(p);
}
}
}
use of org.exist.dom.persistent.NodeProxy in project exist by eXist-db.
the class Score method eval.
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
NodeValue nodeValue = (NodeValue) args[0].itemAt(0);
if (nodeValue.getImplementationType() != NodeValue.PERSISTENT_NODE) {
return Sequence.EMPTY_SEQUENCE;
}
NodeProxy proxy = (NodeProxy) nodeValue;
Match match = proxy.getMatches();
float score = 0.0f;
while (match != null) {
if (match.getIndexId().equals(LuceneIndex.ID)) {
float currentScore = ((LuceneMatch) match).getScore();
score += currentScore;
}
match = match.getNextMatch();
}
return new FloatValue(score);
}
use of org.exist.dom.persistent.NodeProxy in project exist by eXist-db.
the class Facets method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
final String dimension = args[1].getStringValue();
int count = Integer.MAX_VALUE;
if (getArgumentCount() == 3 && args[2].hasOne()) {
count = ((IntegerValue) args[2].itemAt(0)).getInt();
}
String[] paths = null;
if (getArgumentCount() == 4 && !args[3].isEmpty()) {
paths = new String[args[3].getItemCount()];
int j = 0;
for (SequenceIterator i = args[3].unorderedIterator(); i.hasNext(); j++) {
paths[j] = i.nextItem().getStringValue();
}
}
// Find all lucene queries referenced from the input sequence and remember
// the first match for each. Every query will have its own facets attached,
// so we have to merge them below.
final Map<Query, LuceneMatch> luceneQueries = new IdentityHashMap<>();
for (final SequenceIterator i = args[0].unorderedIterator(); i.hasNext(); ) {
final NodeValue nv = (NodeValue) i.nextItem();
if (nv.getImplementationType() == NodeValue.PERSISTENT_NODE) {
final NodeProxy proxy = (NodeProxy) nv;
Match match = proxy.getMatches();
while (match != null) {
if (match.getIndexId().equals(LuceneIndex.ID)) {
final LuceneMatch luceneMatch = (LuceneMatch) match;
luceneQueries.putIfAbsent(luceneMatch.getQuery(), luceneMatch);
}
match = match.getNextMatch();
}
}
}
// Iterate the found queries/matches and collect facets for each
final IMap<AtomicValue, Sequence> map = newLinearMap(null);
for (LuceneMatch match : luceneQueries.values()) {
try {
addFacetsToMap(map, dimension, count, paths, match);
} catch (IOException e) {
throw new XPathException(this, LuceneModule.EXXQDYFT0002, e.getMessage());
}
}
return new MapType(context, map.forked(), Type.STRING);
}
Aggregations