Search in sources :

Example 6 with NodeIteratorAdapter

use of org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter 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 7 with NodeIteratorAdapter

use of org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter in project jackrabbit by apache.

the class TraversingNodeResolver method findNode.

/**
     * @inheritDoc
     */
@Override
public Node findNode(Name propertyName, String value, Name ntName) throws RepositoryException {
    String sr = getSearchRoot(ntName);
    if (getSession().nodeExists(sr)) {
        try {
            Node root = getSession().getNode(sr);
            Set<Node> matchSet = new HashSet<Node>();
            collectNodes(value, Collections.singleton(propertyName), ntName, root.getNodes(), matchSet, true, 1);
            NodeIterator it = new NodeIteratorAdapter(matchSet);
            if (it.hasNext()) {
                return it.nextNode();
            }
        } catch (PathNotFoundException e) {
            // should not get here
            log.warn("Error while retrieving node " + sr);
        }
    }
    // else: searchRoot does not exist yet -> omit the search
    return null;
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) NodeIteratorAdapter(org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter) PathNotFoundException(javax.jcr.PathNotFoundException) HashSet(java.util.HashSet)

Example 8 with NodeIteratorAdapter

use of org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter in project sling by apache.

the class MockNode method getNodes.

@Override
public NodeIterator getNodes(final String namePattern) throws RepositoryException {
    final Pattern pattern = Pattern.compile(namePattern);
    RangeIterator items = getMockedSession().listChildren(getPath(), new ItemFilter() {

        @Override
        public boolean accept(final ItemData item) throws RepositoryException {
            return item.isNode() && pattern.matcher(item.getName()).matches();
        }
    });
    return new NodeIteratorAdapter(items, items.getSize());
}
Also used : Pattern(java.util.regex.Pattern) RangeIterator(javax.jcr.RangeIterator) NodeIteratorAdapter(org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter) RepositoryException(javax.jcr.RepositoryException)

Example 9 with NodeIteratorAdapter

use of org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter in project hippo by NHS-digital-website.

the class MockNode method getNodes.

@Override
public NodeIterator getNodes(final String namePattern) throws RepositoryException {
    final Pattern pattern = Pattern.compile(namePattern);
    RangeIterator items = getMockedSession().listChildren(getPath(), new ItemFilter() {

        @Override
        public boolean accept(final ItemData item) throws RepositoryException {
            return item.isNode() && pattern.matcher(item.getName()).matches();
        }
    });
    return new NodeIteratorAdapter(items, items.getSize());
}
Also used : Pattern(java.util.regex.Pattern) RangeIterator(javax.jcr.RangeIterator) NodeIteratorAdapter(org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter) RepositoryException(javax.jcr.RepositoryException)

Aggregations

NodeIteratorAdapter (org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter)9 Node (javax.jcr.Node)4 NodeIterator (javax.jcr.NodeIterator)3 PathNotFoundException (javax.jcr.PathNotFoundException)3 RepositoryException (javax.jcr.RepositoryException)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Pattern (java.util.regex.Pattern)2 RangeIterator (javax.jcr.RangeIterator)2 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 NoSuchElementException (java.util.NoSuchElementException)1 Property (javax.jcr.Property)1 PropertyIterator (javax.jcr.PropertyIterator)1 Value (javax.jcr.Value)1 RowIterator (javax.jcr.query.RowIterator)1 PropertyIteratorAdapter (org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter)1 FilterContentPackage (org.apache.jackrabbit.commons.packaging.FilterContentPackage)1 IsNodePredicate (org.apache.jackrabbit.commons.predicate.IsNodePredicate)1