Search in sources :

Example 11 with QNm

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

the class FindPathIndex 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 Path<QNm> path = Path.parse(((Str) args[1]).stringValue());
    final Optional<IndexDef> indexDef = controller.getIndexes().findPathIndex(path);
    if (indexDef.isPresent())
        return new Int32(indexDef.get().getID());
    return new Int32(-1);
}
Also used : DBNode(org.sirix.xquery.node.DBNode) QNm(org.brackit.xquery.atomic.QNm) QueryException(org.brackit.xquery.QueryException) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) Int32(org.brackit.xquery.atomic.Int32) IndexController(org.sirix.access.IndexController) IndexDef(org.sirix.index.IndexDef)

Example 12 with QNm

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

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

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

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

the class Load method execute.

@Override
public Sequence execute(final StaticContext sctx, final QueryContext ctx, final Sequence[] args) throws QueryException {
    try {
        final String collName = FunUtil.getString(args, 0, "collName", "collection", null, true);
        final Sequence resources = args[2];
        if (resources == null)
            throw new QueryException(new QNm("No sequence of resources specified!"));
        final boolean createNew = args.length == 4 ? args[3].booleanValue() : true;
        final String resName = FunUtil.getString(args, 1, "resName", "resource", null, createNew ? false : true);
        final DBStore store = (DBStore) ctx.getStore();
        DBCollection coll;
        if (createNew) {
            coll = (DBCollection) create(store, collName, resName, resources);
        } else {
            try {
                coll = (DBCollection) store.lookup(collName);
                add(store, coll, resName, resources);
            } catch (DocumentException e) {
                // collection does not exist
                coll = (DBCollection) create(store, collName, resName, resources);
            }
        }
        return coll;
    } catch (final Exception e) {
        throw new QueryException(new QNm(e.getMessage()), e);
    }
}
Also used : QNm(org.brackit.xquery.atomic.QNm) DBCollection(org.sirix.xquery.node.DBCollection) QueryException(org.brackit.xquery.QueryException) DocumentException(org.brackit.xquery.xdm.DocumentException) Sequence(org.brackit.xquery.xdm.Sequence) DBStore(org.sirix.xquery.node.DBStore) QueryException(org.brackit.xquery.QueryException) DocumentException(org.brackit.xquery.xdm.DocumentException) IOException(java.io.IOException)

Aggregations

QNm (org.brackit.xquery.atomic.QNm)89 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)20 Test (org.junit.Test)18 QueryException (org.brackit.xquery.QueryException)15 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)15 DBNode (org.sirix.xquery.node.DBNode)12 QName (javax.xml.namespace.QName)11 PathSummaryReader (org.sirix.index.path.summary.PathSummaryReader)11 IndexController (org.sirix.access.IndexController)10 IndexDef (org.sirix.index.IndexDef)10 Item (org.brackit.xquery.xdm.Item)7 Axis (org.sirix.api.Axis)7 DescendantAxis (org.sirix.axis.DescendantAxis)7 SirixException (org.sirix.exception.SirixException)7 Attribute (javax.xml.stream.events.Attribute)5 Namespace (javax.xml.stream.events.Namespace)5 DocumentException (org.brackit.xquery.xdm.DocumentException)5 SirixIOException (org.sirix.exception.SirixIOException)5 Ignore (org.junit.Ignore)4 SirixUsageException (org.sirix.exception.SirixUsageException)4