use of org.exist.dom.persistent.Match 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);
}
use of org.exist.dom.persistent.Match in project exist by eXist-db.
the class AddMatch method eval.
@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
if (args[0].isEmpty()) {
return args[0];
}
final NodeValue nv = (NodeValue) args[0].itemAt(0);
if (!nv.isPersistentSet()) {
return nv;
}
final NodeProxy node = (NodeProxy) nv;
final int thisLevel = node.getNodeId().getTreeLevel();
String matchStr = null;
NodeId nodeId = null;
try {
for (final XMLStreamReader reader = context.getBroker().getXMLStreamReader(node, true); reader.hasNext(); ) {
final int status = reader.next();
if (status == XMLStreamConstants.CHARACTERS) {
matchStr = reader.getText();
nodeId = (NodeId) reader.getProperty(ExtendedXMLStreamReader.PROPERTY_NODE_ID);
break;
}
final NodeId otherId = (NodeId) reader.getProperty(ExtendedXMLStreamReader.PROPERTY_NODE_ID);
final int otherLevel = otherId.getTreeLevel();
if (status == XMLStreamConstants.END_ELEMENT && otherLevel == thisLevel) {
// exit-for
break;
}
}
} catch (IOException | XMLStreamException e) {
throw new XPathException(this, ErrorCodes.FOER0000, "Exception caught while reading document");
}
if (nodeId != null) {
Match match = new NGramMatch(getContextId(), node.getNodeId(), matchStr);
match.addOffset(0, matchStr.length());
node.addMatch(match);
}
return node;
}
use of org.exist.dom.persistent.Match in project exist by eXist-db.
the class NodeProxies method transformOwnMatches.
/**
* Applies a supplied function to all matches with the supplied expression id on the supplied NodeProxy and returns
* the NodeProxy with the modified matches if at least one match with the supplied expression id was not transformed
* to null or null otherwise.
*
* @param node
* the NodeProxy to modify
* @param transform
* the function to apply to all matches with the supplied expression id
* @param ownExpressionId
* the expression id of the matches to be transformed
* @return the modified node if at least one match with the supplied expression id was not transformed to null or
* null otherwise
*/
public static NodeProxy transformOwnMatches(final NodeProxy node, final Function<Match, Match> transform, int ownExpressionId) {
Match m = node.getMatches();
node.setMatches(null);
boolean ownMatch = false;
while (m != null) {
if (m.getContextId() != ownExpressionId) {
node.addMatch(m);
} else {
final Match nm = transform.apply(m);
if (nm != null) {
node.addMatch(nm);
ownMatch = true;
}
}
m = m.getNextMatch();
}
return ownMatch ? node : null;
}
use of org.exist.dom.persistent.Match in project exist by eXist-db.
the class NGramIndexWorker method getMatchListener.
public MatchListener getMatchListener(final DBBroker broker, final NodeProxy proxy, final NGramMatchCallback callback) {
boolean needToFilter = false;
Match nextMatch = proxy.getMatches();
while (nextMatch != null) {
if (nextMatch.getIndexId().equals(org.exist.indexing.ngram.NGramIndex.ID)) {
needToFilter = true;
break;
}
nextMatch = nextMatch.getNextMatch();
}
if (!needToFilter) {
return null;
}
if (matchListener == null) {
matchListener = new NGramMatchListener(broker, proxy);
} else {
matchListener.reset(broker, proxy);
}
matchListener.setMatchCallback(callback);
return matchListener;
}
use of org.exist.dom.persistent.Match in project exist by eXist-db.
the class WildcardedExpressionTriple method getMatchingNode.
private NodeProxy getMatchingNode(NodeProxy headNode, NodeProxy tailNode, final int expressionId) {
NodeProxy result = null;
Match match = null;
boolean found = false;
for (Match headMatch = headNode.getMatches(); headMatch != null && !found; headMatch = headMatch.getNextMatch()) {
for (Match tailMatch = tailNode.getMatches(); tailMatch != null && !found; tailMatch = tailMatch.getNextMatch()) {
match = headMatch.followedBy(tailMatch, wildcard.getMinimumLength(), wildcard.getMaximumLength());
found = (match != null);
}
}
if (found) {
// Remove own (partial) matches and add new complete match
NodeProxies.filterMatches(tailNode, a -> a.getContextId() != expressionId);
tailNode.addMatch(match);
result = tailNode;
}
return result;
}
Aggregations