Search in sources :

Example 1 with Atomic

use of org.brackit.xquery.atomic.Atomic 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 2 with Atomic

use of org.brackit.xquery.atomic.Atomic in project sirix by sirixdb.

the class ScanCASIndex method execute.

@Override
public Sequence execute(StaticContext sctx, QueryContext ctx, Sequence[] args) throws QueryException {
    final DBNode doc = (DBNode) args[0];
    final XdmNodeReadTrx rtx = doc.getTrx();
    final IndexController controller = rtx.getResourceManager().getRtxIndexController(rtx.getRevisionNumber());
    if (controller == null) {
        throw new QueryException(new QNm("Document not found: " + ((Str) args[1]).stringValue()));
    }
    final int idx = FunUtil.getInt(args, 1, "$idx-no", -1, null, true);
    final IndexDef indexDef = controller.getIndexes().getIndexDef(idx, IndexType.CAS);
    if (indexDef == null) {
        throw new QueryException(SDBFun.ERR_INDEX_NOT_FOUND, "Index no %s for collection %s and document %s not found.", idx, doc.getCollection().getName(), doc.getTrx().getResourceManager().getResourceConfig().getResource().getFileName().toString());
    }
    if (indexDef.getType() != IndexType.CAS) {
        throw new QueryException(SDBFun.ERR_INVALID_INDEX_TYPE, "Index no %s for collection %s and document %s is not a CAS index.", idx, doc.getCollection().getName(), doc.getTrx().getResourceManager().getResourceConfig().getResource().getFileName().toString());
    }
    final Type keyType = indexDef.getContentType();
    final Atomic key = Cast.cast(sctx, (Atomic) args[2], keyType, true);
    FunUtil.getBoolean(args, 3, "$include-low-key", true, true);
    final int[] searchModes = new int[] { -2, -1, 0, 1, 2 };
    final int searchMode = FunUtil.getInt(args, 4, "$search-mode", 0, searchModes, true);
    final SearchMode mode;
    switch(searchMode) {
        case -2:
            mode = SearchMode.LESS;
            break;
        case -1:
            mode = SearchMode.LESS_OR_EQUAL;
            break;
        case 0:
            mode = SearchMode.EQUAL;
            break;
        case 1:
            mode = SearchMode.GREATER;
            break;
        case 2:
            mode = SearchMode.GREATER_OR_EQUAL;
            break;
        default:
            // May never happen.
            mode = SearchMode.EQUAL;
    }
    final String paths = FunUtil.getString(args, 5, "$paths", null, null, false);
    final CASFilter filter = (paths != null) ? controller.createCASFilter(paths.split(";"), key, mode, new PCRCollectorImpl(rtx)) : controller.createCASFilter(new String[] {}, key, mode, new PCRCollectorImpl(rtx));
    final IndexController ic = controller;
    final DBNode node = doc;
    return new LazySequence() {

        @Override
        public Iter iterate() {
            return new BaseIter() {

                Stream<?> s;

                @Override
                public Item next() throws QueryException {
                    if (s == null) {
                        s = new SirixNodeKeyStream(ic.openCASIndex(node.getTrx().getPageTrx(), indexDef, filter), node.getCollection(), node.getTrx());
                    }
                    return (Item) s.next();
                }

                @Override
                public void close() {
                    if (s != null) {
                        s.close();
                    }
                }
            };
        }
    };
}
Also used : PCRCollectorImpl(org.sirix.index.path.PCRCollectorImpl) SirixNodeKeyStream(org.sirix.xquery.stream.SirixNodeKeyStream) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) CASFilter(org.sirix.index.cas.CASFilter) IndexController(org.sirix.access.IndexController) Atomic(org.brackit.xquery.atomic.Atomic) DBNode(org.sirix.xquery.node.DBNode) BaseIter(org.brackit.xquery.sequence.BaseIter) QNm(org.brackit.xquery.atomic.QNm) Item(org.brackit.xquery.xdm.Item) QueryException(org.brackit.xquery.QueryException) Type(org.brackit.xquery.xdm.Type) AnyNodeType(org.brackit.xquery.xdm.type.AnyNodeType) IndexType(org.sirix.index.IndexType) SequenceType(org.brackit.xquery.xdm.type.SequenceType) AtomicType(org.brackit.xquery.xdm.type.AtomicType) Stream(org.brackit.xquery.xdm.Stream) SirixNodeKeyStream(org.sirix.xquery.stream.SirixNodeKeyStream) IndexDef(org.sirix.index.IndexDef) LazySequence(org.brackit.xquery.sequence.LazySequence) SearchMode(org.sirix.index.SearchMode)

Example 3 with Atomic

use of org.brackit.xquery.atomic.Atomic in project sirix by sirixdb.

the class ScanCASIndexRange method execute.

@Override
public Sequence execute(StaticContext sctx, QueryContext ctx, Sequence[] args) throws QueryException {
    final DBNode doc = (DBNode) args[0];
    final XdmNodeReadTrx rtx = doc.getTrx();
    final IndexController controller = rtx.getResourceManager().getRtxIndexController(rtx.getRevisionNumber());
    if (controller == null) {
        throw new QueryException(new QNm("Document not found: " + ((Str) args[1]).stringValue()));
    }
    final int idx = FunUtil.getInt(args, 1, "$idx-no", -1, null, true);
    final IndexDef indexDef = controller.getIndexes().getIndexDef(idx, IndexType.CAS);
    if (indexDef == null) {
        throw new QueryException(SDBFun.ERR_INDEX_NOT_FOUND, "Index no %s for collection %s and document %s not found.", idx, doc.getCollection().getName(), doc.getTrx().getResourceManager().getResourceConfig().getResource().getFileName().toString());
    }
    if (indexDef.getType() != IndexType.CAS) {
        throw new QueryException(SDBFun.ERR_INVALID_INDEX_TYPE, "Index no %s for collection %s and document %s is not a CAS index.", idx, doc.getCollection().getName(), doc.getTrx().getResourceManager().getResourceConfig().getResource().getFileName().toString());
    }
    final Type keyType = indexDef.getContentType();
    final Atomic min = Cast.cast(sctx, (Atomic) args[2], keyType, true);
    final Atomic max = Cast.cast(sctx, (Atomic) args[3], keyType, true);
    final boolean incMin = FunUtil.getBoolean(args, 4, "$include-low-key", true, true);
    final boolean incMax = FunUtil.getBoolean(args, 5, "$include-high-key", true, true);
    final String paths = FunUtil.getString(args, 6, "$paths", null, null, false);
    final String[] pathArray = paths == null ? new String[] {} : paths.split(";");
    final CASFilterRange filter = controller.createCASFilterRange(pathArray, min, max, incMin, incMax, new PCRCollectorImpl(rtx));
    final IndexController ic = controller;
    final DBNode node = doc;
    return new LazySequence() {

        @Override
        public Iter iterate() {
            return new BaseIter() {

                Stream<?> s;

                @Override
                public Item next() throws QueryException {
                    if (s == null) {
                        s = new SirixNodeKeyStream(ic.openCASIndex(node.getTrx().getPageTrx(), indexDef, filter), node.getCollection(), node.getTrx());
                    }
                    return (Item) s.next();
                }

                @Override
                public void close() {
                    if (s != null) {
                        s.close();
                    }
                }
            };
        }
    };
}
Also used : PCRCollectorImpl(org.sirix.index.path.PCRCollectorImpl) SirixNodeKeyStream(org.sirix.xquery.stream.SirixNodeKeyStream) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) IndexController(org.sirix.access.IndexController) Atomic(org.brackit.xquery.atomic.Atomic) DBNode(org.sirix.xquery.node.DBNode) BaseIter(org.brackit.xquery.sequence.BaseIter) QNm(org.brackit.xquery.atomic.QNm) Item(org.brackit.xquery.xdm.Item) QueryException(org.brackit.xquery.QueryException) Type(org.brackit.xquery.xdm.Type) AnyNodeType(org.brackit.xquery.xdm.type.AnyNodeType) IndexType(org.sirix.index.IndexType) SequenceType(org.brackit.xquery.xdm.type.SequenceType) AtomicType(org.brackit.xquery.xdm.type.AtomicType) CASFilterRange(org.sirix.index.cas.CASFilterRange) Stream(org.brackit.xquery.xdm.Stream) SirixNodeKeyStream(org.sirix.xquery.stream.SirixNodeKeyStream) IndexDef(org.sirix.index.IndexDef) LazySequence(org.brackit.xquery.sequence.LazySequence)

Example 4 with Atomic

use of org.brackit.xquery.atomic.Atomic in project sirix by sirixdb.

the class Load method add.

private TemporalCollection<?> add(final org.brackit.xquery.xdm.Store store, final DBCollection coll, final String resName, final Sequence resources) throws DocumentException, IOException {
    if (resources instanceof Atomic) {
        final Atomic res = (Atomic) resources;
        coll.add(resName, new DocumentParser(URIHandler.getInputStream(res.stringValue())));
        return coll;
    } else {
        final ParserStream parsers = new ParserStream(resources);
        try {
            for (SubtreeParser parser = parsers.next(); parser != null; parser = parsers.next()) {
                coll.add(resName, parser);
            }
        } finally {
            parsers.close();
        }
        return coll;
    }
}
Also used : DocumentParser(org.brackit.xquery.node.parser.DocumentParser) SubtreeParser(org.brackit.xquery.node.parser.SubtreeParser) StreamSubtreeParser(org.brackit.xquery.node.parser.StreamSubtreeParser) Atomic(org.brackit.xquery.atomic.Atomic)

Example 5 with Atomic

use of org.brackit.xquery.atomic.Atomic in project sirix by sirixdb.

the class CASValue method compareTo.

@Override
public int compareTo(@Nullable final CASValue other) {
    final CASValue otherValue = other;
    Atomic thisAtomic = null;
    Atomic otherAtomic = null;
    try {
        thisAtomic = mValue.asType(mType);
        otherAtomic = otherValue.mValue.asType(otherValue.mType);
    } catch (final QueryException e) {
        LOGGER.error(e.getMessage(), e);
    }
    return ComparisonChain.start().compare(mPathNodeKey, otherValue.mPathNodeKey).compare(thisAtomic, otherAtomic).result();
}
Also used : QueryException(org.brackit.xquery.QueryException) Atomic(org.brackit.xquery.atomic.Atomic)

Aggregations

Atomic (org.brackit.xquery.atomic.Atomic)6 QueryException (org.brackit.xquery.QueryException)4 QNm (org.brackit.xquery.atomic.QNm)3 IndexController (org.sirix.access.IndexController)3 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)3 IndexDef (org.sirix.index.IndexDef)3 DBNode (org.sirix.xquery.node.DBNode)3 BaseIter (org.brackit.xquery.sequence.BaseIter)2 LazySequence (org.brackit.xquery.sequence.LazySequence)2 Item (org.brackit.xquery.xdm.Item)2 Stream (org.brackit.xquery.xdm.Stream)2 Type (org.brackit.xquery.xdm.Type)2 AnyNodeType (org.brackit.xquery.xdm.type.AnyNodeType)2 AtomicType (org.brackit.xquery.xdm.type.AtomicType)2 SequenceType (org.brackit.xquery.xdm.type.SequenceType)2 IndexType (org.sirix.index.IndexType)2 SearchMode (org.sirix.index.SearchMode)2 PCRCollectorImpl (org.sirix.index.path.PCRCollectorImpl)2 SirixNodeKeyStream (org.sirix.xquery.stream.SirixNodeKeyStream)2 Iterator (java.util.Iterator)1