Search in sources :

Example 1 with CASFilter

use of org.sirix.index.cas.CASFilter 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)

Aggregations

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