Search in sources :

Example 11 with QueryException

use of org.brackit.xquery.QueryException 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)

Example 12 with QueryException

use of org.brackit.xquery.QueryException in project sirix by sirixdb.

the class FindNameIndex 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 QNm qnm = (QNm) Cast.cast(sctx, (Atomic) args[1], Type.QNM, false);
    final Optional<IndexDef> indexDef = controller.getIndexes().findNameIndex(qnm);
    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) Atomic(org.brackit.xquery.atomic.Atomic) IndexDef(org.sirix.index.IndexDef)

Example 13 with QueryException

use of org.brackit.xquery.QueryException 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)

Example 14 with QueryException

use of org.brackit.xquery.QueryException in project sirix by sirixdb.

the class Doc method execute.

@Override
public Sequence execute(StaticContext sctx, QueryContext ctx, Sequence[] args) throws QueryException {
    if (args.length < 2 || args.length > 4) {
        throw new QueryException(new QNm("No valid arguments specified!"));
    }
    final DBCollection col = (DBCollection) ctx.getStore().lookup(((Str) args[0]).stringValue());
    if (col == null) {
        throw new QueryException(new QNm("No valid arguments specified!"));
    }
    final String expResName = ((Str) args[1]).stringValue();
    final int revision = FunUtil.getInt(args, 2, "revision", -1, null, false);
    final boolean updatable = FunUtil.getBoolean(args, 3, "updatable", false, false);
    return col.getDocument(revision, expResName, updatable);
}
Also used : Str(org.brackit.xquery.atomic.Str) QNm(org.brackit.xquery.atomic.QNm) DBCollection(org.sirix.xquery.node.DBCollection) QueryException(org.brackit.xquery.QueryException)

Example 15 with QueryException

use of org.brackit.xquery.QueryException in project sirix by sirixdb.

the class CreateCASIndex method execute.

@Override
public Sequence execute(StaticContext sctx, QueryContext ctx, Sequence[] args) throws QueryException {
    if (args.length != 2 && args.length != 3) {
        throw new QueryException(new QNm("No valid arguments specified!"));
    }
    final DBNode doc = ((DBNode) args[0]);
    final XdmNodeReadTrx rtx = doc.getTrx();
    final IndexController controller = rtx.getResourceManager().getWtxIndexController(rtx.getRevisionNumber() - 1);
    if (!(doc.getTrx() instanceof XdmNodeWriteTrx)) {
        throw new QueryException(new QNm("Collection must be updatable!"));
    }
    if (controller == null) {
        throw new QueryException(new QNm("Document not found: " + ((Str) args[1]).stringValue()));
    }
    Type type = null;
    if (args.length > 1 && args[1] != null) {
        final QNm name = new QNm(Namespaces.XS_NSURI, ((Str) args[1]).stringValue());
        type = sctx.getTypes().resolveAtomicType(name);
    }
    final Set<Path<QNm>> paths = new HashSet<>();
    if (args.length == 3 && args[2] != null) {
        final Iter it = args[2].iterate();
        Item next = it.next();
        while (next != null) {
            paths.add(Path.parse(((Str) next).stringValue()));
            next = it.next();
        }
    }
    final IndexDef idxDef = IndexDefs.createCASIdxDef(false, Optional.fromNullable(type), paths, controller.getIndexes().getNrOfIndexDefsWithType(IndexType.CAS));
    try {
        controller.createIndexes(ImmutableSet.of(idxDef), (XdmNodeWriteTrx) doc.getTrx());
    } catch (final SirixIOException e) {
        throw new QueryException(new QNm("I/O exception: " + e.getMessage()), e);
    }
    return idxDef.materialize();
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) Path(org.brackit.xquery.util.path.Path) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) IndexController(org.sirix.access.IndexController) SirixIOException(org.sirix.exception.SirixIOException) DBNode(org.sirix.xquery.node.DBNode) Str(org.brackit.xquery.atomic.Str) QNm(org.brackit.xquery.atomic.QNm) Item(org.brackit.xquery.xdm.Item) QueryException(org.brackit.xquery.QueryException) Type(org.brackit.xquery.xdm.Type) IndexType(org.sirix.index.IndexType) Iter(org.brackit.xquery.xdm.Iter) IndexDef(org.sirix.index.IndexDef) HashSet(java.util.HashSet)

Aggregations

QueryException (org.brackit.xquery.QueryException)18 QNm (org.brackit.xquery.atomic.QNm)15 DBNode (org.sirix.xquery.node.DBNode)13 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)11 IndexDef (org.sirix.index.IndexDef)11 IndexController (org.sirix.access.IndexController)10 Item (org.brackit.xquery.xdm.Item)8 Atomic (org.brackit.xquery.atomic.Atomic)4 BaseIter (org.brackit.xquery.sequence.BaseIter)4 LazySequence (org.brackit.xquery.sequence.LazySequence)4 Sequence (org.brackit.xquery.xdm.Sequence)4 Stream (org.brackit.xquery.xdm.Stream)4 Type (org.brackit.xquery.xdm.Type)4 SirixNodeKeyStream (org.sirix.xquery.stream.SirixNodeKeyStream)4 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 Int32 (org.brackit.xquery.atomic.Int32)3 Str (org.brackit.xquery.atomic.Str)3 Iter (org.brackit.xquery.xdm.Iter)3 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)3