Search in sources :

Example 1 with NodeReferences

use of org.sirix.index.avltree.keyvalue.NodeReferences in project sirix by sirixdb.

the class CASIndexBuilder method process.

private VisitResult process(final ImmutableNode node) {
    try {
        if (node.getKind() == Kind.TEXT) {
            mRtx.moveTo(node.getParentKey());
        }
        final long PCR = mRtx.isDocumentRoot() ? 0 : mRtx.getNameNode().getPathNodeKey();
        if (mPaths.isEmpty() || mPathSummaryReader.getPCRsForPaths(mPaths).contains(PCR)) {
            final Str strValue = new Str(((ImmutableValueNode) node).getValue());
            boolean isOfType = false;
            try {
                if (mType != Type.STR)
                    AtomicUtil.toType(strValue, mType);
                isOfType = true;
            } catch (final SirixRuntimeException e) {
            }
            if (isOfType) {
                final CASValue value = new CASValue(strValue, mType, PCR);
                final Optional<NodeReferences> textReferences = mAVLTreeWriter.get(value, SearchMode.EQUAL);
                if (textReferences.isPresent()) {
                    setNodeReferences(node, textReferences.get(), value);
                } else {
                    setNodeReferences(node, new NodeReferences(), value);
                }
            }
        }
        mRtx.moveTo(node.getNodeKey());
    } catch (final PathException | SirixIOException e) {
        LOGGER.error(e.getMessage(), e);
    }
    return VisitResultType.CONTINUE;
}
Also used : Str(org.brackit.xquery.atomic.Str) SirixRuntimeException(org.sirix.exception.SirixRuntimeException) PathException(org.brackit.xquery.util.path.PathException) CASValue(org.sirix.index.avltree.keyvalue.CASValue) NodeReferences(org.sirix.index.avltree.keyvalue.NodeReferences) SirixIOException(org.sirix.exception.SirixIOException)

Example 2 with NodeReferences

use of org.sirix.index.avltree.keyvalue.NodeReferences 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));
    }
}
Also used : Optional(java.util.Optional) IndexFilterAxis(org.sirix.index.IndexFilterAxis) NodeReferences(org.sirix.index.avltree.keyvalue.NodeReferences) Atomic(org.brackit.xquery.atomic.Atomic) AVLNode(org.sirix.index.avltree.AVLNode) CASValue(org.sirix.index.avltree.keyvalue.CASValue) Iterator(java.util.Iterator) SearchMode(org.sirix.index.SearchMode)

Example 3 with NodeReferences

use of org.sirix.index.avltree.keyvalue.NodeReferences in project sirix by sirixdb.

the class CASIndexListener method insert.

private void insert(final ValueNode node, final long pathNodeKey) throws SirixIOException {
    final Str strValue = new Str(node.getValue());
    boolean isOfType = false;
    try {
        AtomicUtil.toType(strValue, mType);
        isOfType = true;
    } catch (final SirixRuntimeException e) {
    }
    if (isOfType) {
        final CASValue indexValue = new CASValue(strValue, mType, pathNodeKey);
        final Optional<NodeReferences> textReferences = mAVLTreeWriter.get(indexValue, SearchMode.EQUAL);
        if (textReferences.isPresent()) {
            setNodeReferences(node, textReferences.get(), indexValue);
        } else {
            setNodeReferences(node, new NodeReferences(), indexValue);
        }
    }
}
Also used : Str(org.brackit.xquery.atomic.Str) SirixRuntimeException(org.sirix.exception.SirixRuntimeException) CASValue(org.sirix.index.avltree.keyvalue.CASValue) NodeReferences(org.sirix.index.avltree.keyvalue.NodeReferences)

Example 4 with NodeReferences

use of org.sirix.index.avltree.keyvalue.NodeReferences in project sirix by sirixdb.

the class NameIndexBuilder method visit.

@Override
public VisitResult visit(final ImmutableElement node) {
    final QNm name = node.getName();
    final boolean included = (mIncludes.isEmpty() || mIncludes.contains(name));
    final boolean excluded = (!mExcludes.isEmpty() && mExcludes.contains(name));
    if (!included || excluded) {
        return VisitResultType.CONTINUE;
    }
    final Optional<NodeReferences> textReferences = mAVLTreeWriter.get(name, SearchMode.EQUAL);
    try {
        if (textReferences.isPresent()) {
            setNodeReferences(node, textReferences.get(), name);
        } else {
            setNodeReferences(node, new NodeReferences(), name);
        }
    } catch (final SirixIOException e) {
        LOGGER.error(e.getMessage(), e);
    }
    return VisitResultType.CONTINUE;
}
Also used : QNm(org.brackit.xquery.atomic.QNm) NodeReferences(org.sirix.index.avltree.keyvalue.NodeReferences) SirixIOException(org.sirix.exception.SirixIOException)

Example 5 with NodeReferences

use of org.sirix.index.avltree.keyvalue.NodeReferences in project sirix by sirixdb.

the class NameIndexListener method listen.

@Override
public void listen(ChangeType type, @Nonnull ImmutableNode node, long pathNodeKey) throws SirixIOException {
    if (node instanceof NameNode) {
        final NameNode nameNode = (NameNode) node;
        final QNm name = nameNode.getName();
        final boolean included = (mIncludes.isEmpty() || mIncludes.contains(name));
        final boolean excluded = (!mExcludes.isEmpty() && mExcludes.contains(name));
        if (!included || excluded) {
            return;
        }
        switch(type) {
            case INSERT:
                final Optional<NodeReferences> textReferences = mAVLTreeWriter.get(name, SearchMode.EQUAL);
                if (textReferences.isPresent()) {
                    setNodeReferences(node, textReferences.get(), name);
                } else {
                    setNodeReferences(node, new NodeReferences(), name);
                }
                break;
            case DELETE:
                mAVLTreeWriter.remove(name, node.getNodeKey());
                break;
            default:
        }
    }
}
Also used : NameNode(org.sirix.node.interfaces.NameNode) QNm(org.brackit.xquery.atomic.QNm) NodeReferences(org.sirix.index.avltree.keyvalue.NodeReferences)

Aggregations

NodeReferences (org.sirix.index.avltree.keyvalue.NodeReferences)6 CASValue (org.sirix.index.avltree.keyvalue.CASValue)3 QNm (org.brackit.xquery.atomic.QNm)2 Str (org.brackit.xquery.atomic.Str)2 SirixIOException (org.sirix.exception.SirixIOException)2 SirixRuntimeException (org.sirix.exception.SirixRuntimeException)2 Iterator (java.util.Iterator)1 Optional (java.util.Optional)1 Atomic (org.brackit.xquery.atomic.Atomic)1 PathException (org.brackit.xquery.util.path.PathException)1 IndexFilterAxis (org.sirix.index.IndexFilterAxis)1 SearchMode (org.sirix.index.SearchMode)1 AVLNode (org.sirix.index.avltree.AVLNode)1 NameNode (org.sirix.node.interfaces.NameNode)1 DBNode (org.sirix.xquery.node.DBNode)1