use of org.exist.dom.persistent.NewArrayNodeSet in project exist by eXist-db.
the class NativeStructuralIndexWorker method findElementsByTagName.
public NodeSet findElementsByTagName(byte type, DocumentSet docs, QName qname, NodeSelector selector, Expression parent) {
final NewArrayNodeSet result = new NewArrayNodeSet();
final FindElementsCallback callback = new FindElementsCallback(type, qname, result, docs, selector, parent);
// for each document id range, scan the index to find matches
for (final Range range : getDocIdRanges(docs)) {
final byte[] fromKey = computeKey(type, qname, range.start);
final byte[] toKey = computeKey(type, qname, range.end + 1);
final IndexQuery query = new IndexQuery(IndexQuery.RANGE, new Value(fromKey), new Value(toKey));
try (final ManagedLock<ReentrantLock> btreeLock = index.lockManager.acquireBtreeReadLock(index.btree.getLockName())) {
index.btree.query(query, callback);
} catch (final LockException e) {
NativeStructuralIndex.LOG.warn("Lock problem while searching structural index: {}", e.getMessage(), e);
} catch (final TerminatedException e) {
NativeStructuralIndex.LOG.warn("Query was terminated while searching structural index: {}", e.getMessage(), e);
} catch (final Exception e) {
NativeStructuralIndex.LOG.error("Error while searching structural index: {}", e.getMessage(), e);
}
}
return result;
}
use of org.exist.dom.persistent.NewArrayNodeSet in project exist by eXist-db.
the class RangeIndexWorker method queryField.
public NodeSet queryField(int contextId, DocumentSet docs, NodeSet contextSet, Sequence fields, Sequence[] keys, RangeIndex.Operator[] operators, int axis) throws IOException, XPathException {
return index.withSearcher(searcher -> {
BooleanQuery query = new BooleanQuery();
int j = 0;
for (SequenceIterator i = fields.iterate(); i.hasNext(); j++) {
String field = i.nextItem().getStringValue();
if (keys[j].getItemCount() > 1) {
BooleanQuery bool = new BooleanQuery();
bool.setMinimumNumberShouldMatch(1);
for (SequenceIterator ki = keys[j].iterate(); ki.hasNext(); ) {
Item key = ki.nextItem();
Query q = toQuery(field, null, key.atomize(), operators[j], docs);
bool.add(q, BooleanClause.Occur.SHOULD);
}
query.add(bool, BooleanClause.Occur.MUST);
} else {
Query q = toQuery(field, null, keys[j].itemAt(0).atomize(), operators[j], docs);
query.add(q, BooleanClause.Occur.MUST);
}
}
Query qu = query;
BooleanClause[] clauses = query.getClauses();
if (clauses.length == 1) {
qu = clauses[0].getQuery();
}
final NodeSet resultSet = new NewArrayNodeSet();
resultSet.addAll(doQuery(contextId, docs, contextSet, axis, searcher.searcher, Node.ELEMENT_NODE, qu, null));
return resultSet;
});
}
use of org.exist.dom.persistent.NewArrayNodeSet in project exist by eXist-db.
the class RangeIndexWorker method query.
public NodeSet query(int contextId, DocumentSet docs, NodeSet contextSet, List<QName> qnames, AtomicValue[] keys, RangeIndex.Operator operator, int axis) throws IOException, XPathException {
return index.withSearcher(searcher -> {
List<QName> definedIndexes = getDefinedIndexes(qnames);
NodeSet resultSet = new NewArrayNodeSet();
for (QName qname : definedIndexes) {
Query query;
String field = LuceneUtil.encodeQName(qname, index.getBrokerPool().getSymbols());
if (keys.length > 1) {
BooleanQuery bool = new BooleanQuery();
for (AtomicValue key : keys) {
bool.add(toQuery(field, qname, key, operator, docs), BooleanClause.Occur.SHOULD);
}
query = bool;
} else {
query = toQuery(field, qname, keys[0], operator, docs);
}
final short nodeType = qname.getNameType() == ElementValue.ATTRIBUTE ? Node.ATTRIBUTE_NODE : Node.ELEMENT_NODE;
resultSet.addAll(doQuery(contextId, docs, contextSet, axis, searcher.searcher, nodeType, query, null));
}
return resultSet;
});
}
use of org.exist.dom.persistent.NewArrayNodeSet in project exist by eXist-db.
the class NativeStructuralIndexWorker method findDescendantsByTagName.
public NodeSet findDescendantsByTagName(byte type, QName qname, int axis, DocumentSet docs, NodeSet contextSet, int contextId, Expression parent) {
final NewArrayNodeSet result = new NewArrayNodeSet();
final FindDescendantsCallback callback = new FindDescendantsCallback(type, axis, qname, contextId, result, parent);
try (final ManagedLock<ReentrantLock> btreeLock = index.lockManager.acquireBtreeReadLock(index.btree.getLockName())) {
for (final NodeProxy ancestor : contextSet) {
final DocumentImpl doc = ancestor.getOwnerDocument();
final NodeId ancestorId = ancestor.getNodeId();
callback.setAncestor(doc, ancestor);
final byte[] fromKey;
final byte[] toKey;
if (ancestorId == NodeId.DOCUMENT_NODE) {
fromKey = computeKey(type, qname, doc.getDocId());
toKey = computeKey(type, qname, doc.getDocId() + 1);
} else {
fromKey = computeKey(type, qname, doc.getDocId(), ancestorId);
toKey = computeKey(type, qname, doc.getDocId(), ancestorId.nextSibling());
}
final IndexQuery query = new IndexQuery(IndexQuery.RANGE, new Value(fromKey), new Value(toKey));
try {
index.btree.query(query, callback);
} catch (final Exception e) {
NativeStructuralIndex.LOG.error("Error while searching structural index: {}", e.getMessage(), e);
}
}
} catch (final LockException e) {
NativeStructuralIndex.LOG.warn("Lock problem while searching structural index: {}", e.getMessage(), e);
}
result.updateNoSort();
return result;
}
use of org.exist.dom.persistent.NewArrayNodeSet in project exist by eXist-db.
the class NativeStructuralIndexWorker method scanByType.
public NodeSet scanByType(byte type, int axis, NodeTest test, boolean useSelfAsContext, DocumentSet docs, NodeSet contextSet, int contextId) {
final NewArrayNodeSet result = new NewArrayNodeSet();
final FindDescendantsCallback callback = new FindDescendantsCallback(type, axis, null, contextId, useSelfAsContext, result, null);
for (final NodeProxy ancestor : contextSet) {
final DocumentImpl doc = ancestor.getOwnerDocument();
final NodeId ancestorId = ancestor.getNodeId();
final List<QName> qnames = getQNamesForDoc(doc);
try (final ManagedLock<ReentrantLock> btreeLock = index.lockManager.acquireBtreeReadLock(index.btree.getLockName())) {
for (final QName qname : qnames) {
if (test.getName() == null || test.matches(qname)) {
callback.setAncestor(doc, ancestor);
byte[] fromKey, toKey;
if (ancestorId == NodeId.DOCUMENT_NODE) {
fromKey = computeKey(type, qname, doc.getDocId());
toKey = computeKey(type, qname, doc.getDocId() + 1);
} else {
fromKey = computeKey(type, qname, doc.getDocId(), ancestorId);
toKey = computeKey(type, qname, doc.getDocId(), ancestorId.nextSibling());
}
final IndexQuery query = new IndexQuery(IndexQuery.RANGE, new Value(fromKey), new Value(toKey));
try {
index.btree.query(query, callback);
} catch (final Exception e) {
NativeStructuralIndex.LOG.error("Error while searching structural index: {}", e.getMessage(), e);
}
}
}
} catch (final LockException e) {
NativeStructuralIndex.LOG.warn("Lock problem while searching structural index: {}", e.getMessage(), e);
}
}
// result.updateNoSort();
return result;
}
Aggregations