Search in sources :

Example 1 with Item

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

use of org.brackit.xquery.xdm.Item 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 3 with Item

use of org.brackit.xquery.xdm.Item in project sirix by sirixdb.

the class ScanPathIndex 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.PATH);
    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.PATH) {
        throw new QueryException(SDBFun.ERR_INVALID_INDEX_TYPE, "Index no %s for collection %s and document %s is not a path index.", idx, doc.getCollection().getName(), doc.getTrx().getResourceManager().getResourceConfig().getResource().getFileName().toString());
    }
    final String paths = FunUtil.getString(args, 2, "$paths", null, null, false);
    final PathFilter filter = (paths != null) ? controller.createPathFilter(paths.split(";"), doc.getTrx()) : null;
    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.openPathIndex(node.getTrx().getPageTrx(), indexDef, filter), node.getCollection(), node.getTrx());
                    }
                    return (Item) s.next();
                }

                @Override
                public void close() {
                    if (s != null) {
                        s.close();
                    }
                }
            };
        }
    };
}
Also used : SirixNodeKeyStream(org.sirix.xquery.stream.SirixNodeKeyStream) PathFilter(org.sirix.index.path.PathFilter) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) IndexController(org.sirix.access.IndexController) 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) 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 Item

use of org.brackit.xquery.xdm.Item in project sirix by sirixdb.

the class XQueryUsage method loadDocumentAndQueryTemporal.

/**
 * Load a document and query it (temporal enhancements).
 */
private static void loadDocumentAndQueryTemporal() throws QueryException, IOException, SirixException {
    // Initialize query context and store (implicit transaction commit).
    try (final DBStore store = DBStore.newBuilder().build()) {
        final QueryContext ctx1 = new SirixQueryContext(store);
        final CompileChain compileChain = new SirixCompileChain(store);
        final Path doc1 = generateSampleDoc("sample1");
        final URI docUri = doc1.toUri();
        // Use XQuery to load sample document into store.
        System.out.println("Loading document:");
        final String xq1 = String.format("sdb:load('mydocs.col', 'resource1', '%s')", docUri.toString());
        System.out.println(xq1);
        new XQuery(compileChain, xq1).evaluate(ctx1);
    }
    try (final DBStore store = DBStore.newBuilder().build()) {
        final QueryContext ctx = new QueryContext(store);
        System.out.println();
        System.out.println("Query loaded document:");
        final String xq3 = "let $doc:= doc('mydocs.col')/log return sdb:select-node($doc, 7) ";
        System.out.println(xq3);
        XQuery q = new XQuery(new SirixCompileChain(store), xq3);
        q.prettyPrint();
        q.serialize(ctx, System.out);
    }
    try (final DBStore store = DBStore.newBuilder().build()) {
        final QueryContext ctx = new SirixQueryContext(store);
        System.out.println();
        System.out.println("Query loaded document:");
        final String xq3 = "doc('mydocs.col')/log/all-time::*";
        System.out.println(xq3);
        XQuery q = new XQuery(new SirixCompileChain(store), xq3);
        q.prettyPrint();
        q.serialize(ctx, System.out);
    }
    // Create and commit CAS indexes on all attribute- and text-nodes.
    try (final DBStore store = DBStore.newBuilder().build()) {
        final QueryContext ctx3 = new QueryContext(store);
        System.out.println();
        System.out.println("Create a cas index for all attributes and another one for text-nodes. A third one is created for all integers:");
        final XQuery q = new XQuery(new SirixCompileChain(store), "let $doc := sdb:doc('mydocs.col', 'resource1', (), fn:boolean(1)) " + "let $casStats1 := sdb:create-cas-index($doc, 'xs:string', '//@*') " + "let $casStats2 := sdb:create-cas-index($doc, 'xs:string', '//*') " + "let $casStats3 := sdb:create-cas-index($doc, 'xs:integer', '//*') " + "return <rev>{sdb:commit($doc)}</rev>");
        q.serialize(ctx3, System.out);
        System.out.println();
        System.out.println("CAS index creation done.");
    }
    // Create and commit path index on all elements.
    try (final DBStore store = DBStore.newBuilder().build()) {
        final QueryContext ctx3 = new QueryContext(store);
        System.out.println();
        System.out.println("Create path index for all elements (all paths):");
        final XQuery q = new XQuery(new SirixCompileChain(store), "let $doc := sdb:doc('mydocs.col', 'resource1', (), fn:boolean(1)) " + "let $stats := sdb:create-path-index($doc, '//*') " + "return <rev>{sdb:commit($doc)}</rev>");
        q.serialize(ctx3, System.out);
        System.out.println();
        System.out.println("Path index creation done.");
    }
    // Create and commit name index on all elements with QName 'src' or 'msg'.
    try (final DBStore store = DBStore.newBuilder().build()) {
        final QueryContext ctx3 = new SirixQueryContext(store, CommitStrategy.EXPLICIT);
        System.out.println();
        System.out.println("Create name index for all elements with name 'src' or 'msg':");
        final XQuery q = new XQuery(new SirixCompileChain(store), "let $doc := sdb:doc('mydocs.col', 'resource1', (), fn:boolean(1)) " + "let $stats := sdb:create-name-index($doc, fn:QName((), 'src')) " + "return <rev>{sdb:commit($doc)}</rev>");
        q.serialize(ctx3, System.out);
        System.out.println();
        System.out.println("Name index creation done.");
    }
    // Query CAS index.
    try (final DBStore store = DBStore.newBuilder().build()) {
        System.out.println("");
        System.out.println("Find CAS index for all attribute values.");
        final QueryContext ctx3 = new SirixQueryContext(store);
        final String query = "let $doc := sdb:doc('mydocs.col', 'resource1') return sdb:scan-cas-index($doc, sdb:find-cas-index($doc, 'xs:string', '//@*'), 'bar', true(), 0, ())";
        final Sequence seq = new XQuery(new SirixCompileChain(store), query).execute(ctx3);
        // final Iter iter = seq.iterate();
        // for (Item item = iter.next(); item != null; item = iter.next()) {
        // System.out.println(item);
        // }
        final Comparator<Tuple> comparator = (o1, o2) -> ((Node<?>) o1).cmp((Node<?>) o2);
        final Sequence sortedSeq = new SortedNodeSequence(comparator, seq, true);
        final Iter sortedIter = sortedSeq.iterate();
        System.out.println("Sorted index entries in document order: ");
        for (Item item = sortedIter.next(); item != null; item = sortedIter.next()) {
            System.out.println(item);
        }
    }
    // Query CAS index.
    try (final DBStore store = DBStore.newBuilder().build()) {
        System.out.println("");
        System.out.println("Find CAS index for all text values which are integers between 10 and 100.");
        final QueryContext ctx3 = new SirixQueryContext(store);
        final String query = "let $doc := sdb:doc('mydocs.col', 'resource1') return sdb:scan-cas-index-range($doc, sdb:find-cas-index($doc, 'xs:integer', '//*'), 10, 100, true(), true(), ())";
        final Sequence seq = new XQuery(new SirixCompileChain(store), query).execute(ctx3);
        // final Iter iter = seq.iterate();
        // for (Item item = iter.next(); item != null; item = iter.next()) {
        // System.out.println(item);
        // }
        final Comparator<Tuple> comparator = (o1, o2) -> ((Node<?>) o1).cmp((Node<?>) o2);
        final Sequence sortedSeq = new SortedNodeSequence(comparator, seq, true);
        final Iter sortedIter = sortedSeq.iterate();
        System.out.println("Sorted index entries in document order: ");
        for (Item item = sortedIter.next(); item != null; item = sortedIter.next()) {
            System.out.println(item);
        }
    }
    // Query path index which are children of the log-element (only elements).
    try (final DBStore store = DBStore.newBuilder().build()) {
        System.out.println("");
        System.out.println("Find path index for all elements which are children of the log-element (only elements).");
        final QueryContext ctx3 = new SirixQueryContext(store);
        final DBNode node = (DBNode) new XQuery(new SirixCompileChain(store), "doc('mydocs.col')").execute(ctx3);
        final Optional<IndexDef> index = node.getTrx().getResourceManager().getRtxIndexController(node.getTrx().getRevisionNumber()).getIndexes().findPathIndex(org.brackit.xquery.util.path.Path.parse("//log/*"));
        System.out.println(index);
        // last param '()' queries whole index.
        final String query = "let $doc := sdb:doc('mydocs.col', 'resource1') " + "return sdb:scan-path-index($doc, " + index.get().getID() + ", '//log/*')";
        final Sequence seq = new XQuery(new SirixCompileChain(store), query).execute(ctx3);
        final Comparator<Tuple> comparator = (o1, o2) -> ((Node<?>) o1).cmp((Node<?>) o2);
        final Sequence sortedSeq = new SortedNodeSequence(comparator, seq, true);
        final Iter sortedIter = sortedSeq.iterate();
        System.out.println("Sorted index entries in document order: ");
        for (Item item = sortedIter.next(); item != null; item = sortedIter.next()) {
            System.out.println(item);
        }
    }
    // Query name index.
    try (final DBStore store = DBStore.newBuilder().build()) {
        System.out.println("");
        System.out.println("Query name index (src-element).");
        final QueryContext ctx3 = new QueryContext(store);
        final String query = "let $doc := sdb:doc('mydocs.col', 'resource1')" + " let $sequence := sdb:scan-name-index($doc, sdb:find-name-index($doc, fn:QName((), 'src')), fn:QName((), 'src'))" + " return sdb:sort($sequence)";
        final XQuery q = new XQuery(new SirixCompileChain(store), query);
        q.prettyPrint();
        q.serialize(ctx3, System.out);
    }
    try (final DBStore store = DBStore.newBuilder().build()) {
        final QueryContext ctx = new SirixQueryContext(store);
        System.out.println();
        System.out.println("Query loaded document:");
        final String xq3 = "doc('mydocs.col')/log/all-time::*";
        System.out.println(xq3);
        XQuery q = new XQuery(new SirixCompileChain(store), xq3);
        q.prettyPrint();
        q.serialize(ctx, System.out);
        // Serialize first version to XML
        // ($user.home$/sirix-data/output-revision-1.xml).
        final QueryContext ctx4 = new QueryContext(store);
        final String xq4 = "doc('mydocs.col', 1)";
        q = new XQuery(xq4);
        try (final PrintStream out = new PrintStream(new FileOutputStream(LOCATION.resolve("output-revision-1.xml").toFile()))) {
            q.prettyPrint().serialize(ctx4, out);
        }
        System.out.println();
        // Serialize second version to XML
        // ($user.home$/sirix-data/output-revision-1.xml).
        final QueryContext ctx5 = new SirixQueryContext(store);
        final String xq5 = "sdb:serialize(doc('mydocs.col', 2), fn:boolean(1), 'output-revision-2.xml')";
        q = new XQuery(xq5);
        q.execute(ctx5);
        System.out.println();
        // Serialize first, second and third version to XML
        // ($user.home$/sirix-data/output-revisions.xml).
        final QueryContext ctx6 = new SirixQueryContext(store);
        final String xq6 = "for $i in ((doc('mydocs.col', 1), doc('mydocs.col', 2), doc('mydocs.col', 3))) return $i";
        q = new XQuery(xq6);
        try (final PrintStream out = new PrintStream(new FileOutputStream(LOCATION.resolve("output-revisions.xml").toFile()))) {
            q.prettyPrint().serialize(ctx6, out);
        }
        System.out.println();
    }
    try (final DBStore store = DBStore.newBuilder().build()) {
        final Path doc = Paths.get("src", "main", "resources", "test.xml");
        final QueryContext ctx = new SirixQueryContext(store);
        System.out.println();
        final String xq3 = String.format("sdb:load('mycoll.col', 'mydoc.xml', '%s')", doc.toUri().toString());
        System.out.println(xq3);
        final XQuery q = new XQuery(new SirixCompileChain(store), xq3);
        q.execute(ctx);
    }
}
Also used : Path(java.nio.file.Path) SortedNodeSequence(org.brackit.xquery.sequence.SortedNodeSequence) Date(java.util.Date) DBNode(org.sirix.xquery.node.DBNode) Random(java.util.Random) Sequence(org.brackit.xquery.xdm.Sequence) SirixCompileChain(org.sirix.xquery.SirixCompileChain) QueryException(org.brackit.xquery.QueryException) Tuple(org.brackit.xquery.Tuple) Databases(org.sirix.access.Databases) Node(org.brackit.xquery.xdm.Node) XQuery(org.brackit.xquery.XQuery) URI(java.net.URI) Path(java.nio.file.Path) PrintStream(java.io.PrintStream) SirixException(org.sirix.exception.SirixException) IndexDef(org.sirix.index.IndexDef) Iter(org.brackit.xquery.xdm.Iter) DBStore(org.sirix.xquery.node.DBStore) Files(java.nio.file.Files) Item(org.brackit.xquery.xdm.Item) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) CompileChain(org.brackit.xquery.compiler.CompileChain) Paths(java.nio.file.Paths) Database(org.sirix.api.Database) QueryContext(org.brackit.xquery.QueryContext) Optional(java.util.Optional) Comparator(java.util.Comparator) CommitStrategy(org.sirix.xquery.SirixQueryContext.CommitStrategy) SirixQueryContext(org.sirix.xquery.SirixQueryContext) PrintStream(java.io.PrintStream) XQuery(org.brackit.xquery.XQuery) DBNode(org.sirix.xquery.node.DBNode) Node(org.brackit.xquery.xdm.Node) SortedNodeSequence(org.brackit.xquery.sequence.SortedNodeSequence) SirixCompileChain(org.sirix.xquery.SirixCompileChain) QueryContext(org.brackit.xquery.QueryContext) SirixQueryContext(org.sirix.xquery.SirixQueryContext) SortedNodeSequence(org.brackit.xquery.sequence.SortedNodeSequence) Sequence(org.brackit.xquery.xdm.Sequence) DBStore(org.sirix.xquery.node.DBStore) URI(java.net.URI) DBNode(org.sirix.xquery.node.DBNode) Item(org.brackit.xquery.xdm.Item) FileOutputStream(java.io.FileOutputStream) SirixQueryContext(org.sirix.xquery.SirixQueryContext) SirixCompileChain(org.sirix.xquery.SirixCompileChain) CompileChain(org.brackit.xquery.compiler.CompileChain) Iter(org.brackit.xquery.xdm.Iter) IndexDef(org.sirix.index.IndexDef) Tuple(org.brackit.xquery.Tuple)

Example 5 with Item

use of org.brackit.xquery.xdm.Item in project sirix by sirixdb.

the class ScanNameIndex 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.NAME);
    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.NAME) {
        throw new QueryException(SDBFun.ERR_INVALID_INDEX_TYPE, "Index no %s for collection %s and document %s is not a path index.", idx, doc.getCollection().getName(), doc.getTrx().getResourceManager().getResourceConfig().getResource().getFileName().toString());
    }
    final String names = FunUtil.getString(args, 2, "$names", null, null, false);
    final NameFilter filter = (names != null) ? controller.createNameFilter(names.split(";")) : null;
    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.openNameIndex(node.getTrx().getPageTrx(), indexDef, filter), node.getCollection(), node.getTrx());
                    }
                    return (Item) s.next();
                }

                @Override
                public void close() {
                    if (s != null) {
                        s.close();
                    }
                }
            };
        }
    };
}
Also used : SirixNodeKeyStream(org.sirix.xquery.stream.SirixNodeKeyStream) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) IndexController(org.sirix.access.IndexController) 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) NameFilter(org.sirix.index.name.NameFilter) Stream(org.brackit.xquery.xdm.Stream) SirixNodeKeyStream(org.sirix.xquery.stream.SirixNodeKeyStream) IndexDef(org.sirix.index.IndexDef) LazySequence(org.brackit.xquery.sequence.LazySequence)

Aggregations

QueryException (org.brackit.xquery.QueryException)8 Item (org.brackit.xquery.xdm.Item)8 IndexDef (org.sirix.index.IndexDef)8 DBNode (org.sirix.xquery.node.DBNode)8 QNm (org.brackit.xquery.atomic.QNm)7 IndexController (org.sirix.access.IndexController)7 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)7 BaseIter (org.brackit.xquery.sequence.BaseIter)4 LazySequence (org.brackit.xquery.sequence.LazySequence)4 Iter (org.brackit.xquery.xdm.Iter)4 Stream (org.brackit.xquery.xdm.Stream)4 SirixNodeKeyStream (org.sirix.xquery.stream.SirixNodeKeyStream)4 HashSet (java.util.HashSet)3 Type (org.brackit.xquery.xdm.Type)3 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)3 SirixIOException (org.sirix.exception.SirixIOException)3 IndexType (org.sirix.index.IndexType)3 Atomic (org.brackit.xquery.atomic.Atomic)2 Str (org.brackit.xquery.atomic.Str)2 Path (org.brackit.xquery.util.path.Path)2