Search in sources :

Example 1 with PrefetchOptions

use of org.apache.jackrabbit.oak.jcr.query.PrefetchIterator.PrefetchOptions in project jackrabbit-oak by apache.

the class QueryResultImpl method getRows.

@Override
public RowIterator getRows() throws RepositoryException {
    Iterator<RowImpl> rowIterator = new Iterator<RowImpl>() {

        private final Iterator<? extends ResultRow> it = result.getRows().iterator();

        private final String pathSelector;

        private RowImpl current;

        private int rowCount;

        //Avoid log check for every row access
        private final boolean debugEnabled = queryOpsLogger.isDebugEnabled();

        {
            String[] columnSelectorNames = result.getColumnSelectorNames();
            if (columnSelectorNames.length == 1) {
                pathSelector = columnSelectorNames[0];
            } else {
                pathSelector = null;
            }
            fetch();
        }

        private void fetch() {
            if (it.hasNext()) {
                current = new RowImpl(QueryResultImpl.this, it.next(), pathSelector);
                if (debugEnabled) {
                    rowCount++;
                    if (rowCount % 100 == 0) {
                        queryOpsLogger.debug("Iterated over [{}] results so far", rowCount);
                    }
                }
            } else {
                current = null;
            }
        }

        @Override
        public boolean hasNext() {
            return current != null;
        }

        @Override
        public RowImpl next() {
            if (current == null) {
                throw new NoSuchElementException();
            }
            RowImpl r = current;
            fetch();
            return r;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
    final PrefetchIterator<RowImpl> prefIt = new PrefetchIterator<RowImpl>(sessionDelegate.sync(rowIterator), new PrefetchOptions() {

        {
            size = result.getSize();
            fastSize = sessionContext.getFastQueryResultSize();
            fastSizeCallback = result;
        }
    });
    return new RowIteratorAdapter(prefIt) {

        @Override
        public long getSize() {
            return prefIt.size();
        }
    };
}
Also used : ResultRow(org.apache.jackrabbit.oak.api.ResultRow) RowIteratorAdapter(org.apache.jackrabbit.commons.iterator.RowIteratorAdapter) PrefetchOptions(org.apache.jackrabbit.oak.jcr.query.PrefetchIterator.PrefetchOptions) Iterator(java.util.Iterator) RowIterator(javax.jcr.query.RowIterator) NodeIterator(javax.jcr.NodeIterator) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with PrefetchOptions

use of org.apache.jackrabbit.oak.jcr.query.PrefetchIterator.PrefetchOptions in project jackrabbit-oak by apache.

the class QueryResultImpl method getNodes.

@Override
public NodeIterator getNodes() throws RepositoryException {
    String[] columnSelectorNames = result.getColumnSelectorNames();
    if (columnSelectorNames.length != 1) {
        throw new RepositoryException("Query contains more than one selector: " + Arrays.toString(columnSelectorNames));
    }
    final String selectorName = columnSelectorNames[0];
    if (selectorName == null) {
        throw new RepositoryException("Query does not contain a selector: " + Arrays.toString(columnSelectorNames));
    }
    Iterator<NodeImpl<? extends NodeDelegate>> nodeIterator = new Iterator<NodeImpl<? extends NodeDelegate>>() {

        private final Iterator<? extends ResultRow> it = result.getRows().iterator();

        private NodeImpl<? extends NodeDelegate> current;

        {
            fetch();
        }

        private void fetch() {
            current = null;
            while (it.hasNext()) {
                ResultRow r = it.next();
                Tree tree = r.getTree(selectorName);
                if (tree != null && tree.exists()) {
                    try {
                        current = getNode(tree);
                        break;
                    } catch (RepositoryException e) {
                        LOG.warn("Unable to fetch result node for path " + tree.getPath(), e);
                    }
                }
            }
        }

        @Override
        public boolean hasNext() {
            return current != null;
        }

        @Override
        public NodeImpl<? extends NodeDelegate> next() {
            if (current == null) {
                throw new NoSuchElementException();
            }
            NodeImpl<? extends NodeDelegate> n = current;
            fetch();
            return n;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
    final PrefetchIterator<NodeImpl<? extends NodeDelegate>> prefIt = new PrefetchIterator<NodeImpl<? extends NodeDelegate>>(sessionDelegate.sync(nodeIterator), new PrefetchOptions() {

        {
            size = result.getSize();
            fastSize = sessionContext.getFastQueryResultSize();
            fastSizeCallback = result;
        }
    });
    return new NodeIteratorAdapter(prefIt) {

        @Override
        public long getSize() {
            return prefIt.size();
        }
    };
}
Also used : ResultRow(org.apache.jackrabbit.oak.api.ResultRow) NodeImpl(org.apache.jackrabbit.oak.jcr.session.NodeImpl) PrefetchOptions(org.apache.jackrabbit.oak.jcr.query.PrefetchIterator.PrefetchOptions) NodeIteratorAdapter(org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter) RepositoryException(javax.jcr.RepositoryException) NodeDelegate(org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate) Iterator(java.util.Iterator) RowIterator(javax.jcr.query.RowIterator) NodeIterator(javax.jcr.NodeIterator) Tree(org.apache.jackrabbit.oak.api.Tree) NoSuchElementException(java.util.NoSuchElementException)

Example 3 with PrefetchOptions

use of org.apache.jackrabbit.oak.jcr.query.PrefetchIterator.PrefetchOptions in project jackrabbit-oak by apache.

the class PrefetchIteratorTest method test.

@Test
public void test() {
    // for (int size = 0; size < 100; size++)
    for (int size : seq(0, 100)) {
        for (int readBefore : seq(0, 30)) {
            // every 3th time, use a timeout
            final long testTimeout = size % 3 == 0 ? 100 : 0;
            Iterable<Integer> s = seq(0, size);
            PrefetchIterator<Integer> it = new PrefetchIterator<Integer>(s.iterator(), new PrefetchOptions() {

                {
                    min = 20;
                    timeout = testTimeout;
                    max = 30;
                    size = -1;
                }
            });
            for (int x : seq(0, readBefore)) {
                boolean hasNext = it.hasNext();
                if (!hasNext) {
                    assertEquals(x, size);
                    break;
                }
                String m = "s:" + size + " b:" + readBefore + " x:" + x;
                assertTrue(m, hasNext);
                assertEquals(m, x, it.next().intValue());
            }
            String m = "s:" + size + " b:" + readBefore;
            int max = testTimeout <= 0 ? 20 : 30;
            if (size > max && readBefore <= size) {
                assertEquals(m, -1, it.size());
                // calling it twice must not change the result
                assertEquals(m, -1, it.size());
            } else {
                assertEquals(m, size, it.size());
                // calling it twice must not change the result
                assertEquals(m, size, it.size());
            }
            for (int x : seq(readBefore, size)) {
                m = "s:" + size + " b:" + readBefore + " x:" + x;
                assertTrue(m, it.hasNext());
                assertEquals(m, x, it.next().intValue());
            }
            assertFalse(it.hasNext());
            try {
                it.next();
                fail();
            } catch (NoSuchElementException e) {
            // expected
            }
            try {
                it.remove();
                fail();
            } catch (UnsupportedOperationException e) {
            // expected
            }
        }
    }
}
Also used : PrefetchOptions(org.apache.jackrabbit.oak.jcr.query.PrefetchIterator.PrefetchOptions) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.Test)

Aggregations

NoSuchElementException (java.util.NoSuchElementException)3 PrefetchOptions (org.apache.jackrabbit.oak.jcr.query.PrefetchIterator.PrefetchOptions)3 Iterator (java.util.Iterator)2 NodeIterator (javax.jcr.NodeIterator)2 RowIterator (javax.jcr.query.RowIterator)2 ResultRow (org.apache.jackrabbit.oak.api.ResultRow)2 RepositoryException (javax.jcr.RepositoryException)1 NodeIteratorAdapter (org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter)1 RowIteratorAdapter (org.apache.jackrabbit.commons.iterator.RowIteratorAdapter)1 Tree (org.apache.jackrabbit.oak.api.Tree)1 NodeDelegate (org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate)1 NodeImpl (org.apache.jackrabbit.oak.jcr.session.NodeImpl)1 Test (org.junit.Test)1