use of org.sirix.index.avltree.AVLNode in project sirix by sirixdb.
the class CASIndexImpl method openIndex.
@Override
public Iterator<NodeReferences> openIndex(PageReadTrx pageReadTrx, IndexDef indexDef, CASFilter filter) {
final AVLTreeReader<CASValue, NodeReferences> reader = AVLTreeReader.getInstance(pageReadTrx, indexDef.getType(), indexDef.getID());
// PCRs requested.
final Set<Long> pcrsRequested = filter.getPCRs();
// PCRs available in index.
final Set<Long> pcrsAvailable = filter.getPCRCollector().getPCRsForPaths(indexDef.getPaths()).getPCRs();
// CASValue.
if (pcrsAvailable.size() <= 1 && pcrsRequested.size() == 1) {
final Atomic atomic = filter.getKey();
final long pcr = pcrsRequested.iterator().next();
final SearchMode mode = filter.getMode();
final CASValue value = new CASValue(atomic, atomic.type(), pcr);
if (mode == SearchMode.EQUAL) {
// Compare for equality by PCR and atomic value.
final Optional<AVLNode<CASValue, NodeReferences>> node = reader.getAVLNode(value, mode);
if (node.isPresent()) {
return Iterators.forArray(node.get().getValue());
}
return Collections.emptyIterator();
} else {
// Compare for search criteria by PCR and atomic value.
final Optional<AVLNode<CASValue, NodeReferences>> node = reader.getAVLNode(value, mode);
if (node.isPresent()) {
// Iterate over subtree.
final Iterator<AVLNode<CASValue, NodeReferences>> iter = reader.new AVLNodeIterator(node.get().getNodeKey());
return Iterators.concat(Iterators.forArray(node.get().getValue()), new IndexFilterAxis<CASValue>(iter, ImmutableSet.of(filter)));
}
return Collections.emptyIterator();
}
} else if (pcrsRequested.size() == 1) {
final Atomic atomic = filter.getKey();
final long pcr = pcrsRequested.iterator().next();
final SearchMode mode = filter.getMode();
final CASValue value = new CASValue(atomic, atomic.type(), pcr);
if (mode == SearchMode.EQUAL) {
// Compare for equality by PCR and atomic value.
final Optional<AVLNode<CASValue, NodeReferences>> node = reader.getAVLNode(value, mode);
if (node.isPresent()) {
// Iterate over subtree.
final Iterator<AVLNode<CASValue, NodeReferences>> iter = reader.new AVLNodeIterator(node.get().getNodeKey());
return Iterators.concat(Iterators.forArray(node.get().getValue()), new IndexFilterAxis<CASValue>(iter, ImmutableSet.of(filter)));
}
return Collections.emptyIterator();
} else {
// Compare for equality only by PCR.
final Optional<AVLNode<CASValue, NodeReferences>> node = reader.getAVLNode(value, SearchMode.EQUAL, (CASValue v1, CASValue v2) -> ((Long) v1.getPathNodeKey()).compareTo(v2.getPathNodeKey()));
if (node.isPresent()) {
// Now compare for equality by PCR and atomic value and find first
// node which satisfies criteria.
final Optional<AVLNode<CASValue, NodeReferences>> firstFoundNode = reader.getAVLNode(node.get().getNodeKey(), value, mode);
if (firstFoundNode.isPresent()) {
// Iterate over subtree.
final Iterator<AVLNode<CASValue, NodeReferences>> iter = reader.new AVLNodeIterator(firstFoundNode.get().getNodeKey());
return Iterators.concat(Iterators.forArray(firstFoundNode.get().getValue()), new IndexFilterAxis<CASValue>(iter, ImmutableSet.of(filter)));
} else {
return Iterators.forArray(firstFoundNode.get().getValue());
}
}
return Collections.emptyIterator();
}
} else {
final Iterator<AVLNode<CASValue, NodeReferences>> iter = reader.new AVLNodeIterator(Fixed.DOCUMENT_NODE_KEY.getStandardProperty());
return new IndexFilterAxis<CASValue>(iter, ImmutableSet.of(filter));
}
}
Aggregations