On Mon, Jun 4, 2018 at 11:45 AM, Robert Munteanu <romb...@apache.org> wrote:

> Hi Jason,
>
> On Thu, 2018-05-31 at 10:39 -0400, Jason E Bailey wrote:
> > https://github.com/apache/sling-org-apache-sling-api/pull/4
> >
> > I've created a pull request to add a new class to the Sling API
> > bundle that would allow you to create a Stream<Resource> object which
> > traverses a resource tree.
> >
> > Additionally, the ResourceStream class has convenience methods to
> > specify a predicate that identifies valid resources, a limit to the
> > number of valid items that are returned and a range method. There is
> > also a core feature of branch selection that allows you to customize
> > the traversal so that it you can limit the branches that the
> > traversal proceeds down.
> >
> > If you have concerns or questions, now's the time to raise it.
>
> Again, thanks for looking into this, I think it's a nice addition.
>
> There are two questions I want to raise:
>
> 1. API related
>
> We have two methods:
>
> - stream()
> - stream(Predicate<Resource>)
>
> which traverse the whole tree.
>
> There were questions of whether this is OK or if we maybe need to
> stream the children only. Could the API additions allow that?
>
> We can start with something like
>
> - stream(Predicate<Resource>, Set<TraversalOption>... )
> - stream(Predicate<Resource>)
> - stream(Set<TraversalOption>...)
>
> where
>
> enum TraversalOption {
>   DIRECT_CHILDREN_ONLY, ALL_CHILDREN;
> }
>
> (yes, the names are bad, just wanted to raise the question and start a
> discussion)
>

Rather than having another parameter, what about providing a
ResourceChildrenPredicate? Based on the current API it looks like you'd
have to provide the current resource to make this work, but it seems like
it would be very useful to have a predicate which would only allow for both
direct children or children up to a particular depth. I'd see it useful to
provide 2-3 different default predicates to help with common activities:

ResourceChildrenPredicate - filter the stream of resources based on their
child status
ResourceTypePredicate - filter the stream of resource based on their
resource type
ValuePredicate - filter the stream of resources based on a value in the
resource's ValueMap


>
> 2. Implementation related
>
> I think that the 'hasNext' method is not lazy, eagerly loading the
> direct children of a resource when there are no more resources to load.
>
> If the resource has a large number of children we can end up taking
> lots of memory, so I think we need to be more lazy with it, since the
> streams framework can take advantage of that (IIRC). Is it possible to
> ajust the implementation this way?
>
> Thanks,
>
> Robert
>

Reply via email to