Search in sources :

Example 1 with NodeImpl

use of org.apache.jackrabbit.oak.jcr.session.NodeImpl 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)

Aggregations

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