jackrabbit-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alexander Klimetschek <aklim...@adobe.com>
Subject Re: count of all subfolders
Date Wed, 22 Dec 2010 10:11:01 GMT
On 22.12.10 09:12, "danisevsky" <danisevsky@gmail.com> wrote:

>Hi, could you tell me what is the best way how to get count of
>subfolders? There may be thousands so I think recursion:
>    private int countAllChildren(int c, Node node) {
>        try {
>            NodeIterator iterator = node.getNodes();
>            while (iterator.hasNext()) {
>                Node child = (Node) iterator.next();
>                if (child.isNodeType("nt:folder")) {
>                    c = countAllChildren(++c, child);
>                }
>            }
>            return c;
>        } catch (RepositoryException e) {
>            throw new RuntimeException(e);
>        }
>    }
>is not good choice.

You don't have to do that yourself, there is the TraversingItemVisitor in
the JCR API for that [1]. Have a look at its source though, as the API
docs seem to omit the all-important protected methods entering/leaving().
You'd extend from it and increment the count on each entering(Node node,
int level) call.

Performance or better memory consumption is not an issue unless you have
an unusually high tree depth (hundreds of thousands or millions maybe,
very very unrealistic for all content I know ;-)).

>I would like to use JCR-SQL2 query language, but every query what I
>tried didn't work, e.g.:
>SELECT * FROM [nt:folder] WHERE ISCHILDNODE(["+node.getPath()+"])
>SELECT * FROM [nt:folder] as fld WHERE fld.[jcr:path] LIKE

Yeah, SQL-2 is sometimes a bit tricky. In Xpath it would be simple as doing


and counting the rows in the returned iterator (getSize() will most likely
be -1 as optimization). But this will probably be not as efficient as the
traversal approach (search index does not speed up those queries, and you
only get all the overhead of the search).


Alexander Klimetschek
Developer // Adobe (Day) // Berlin - Basel

View raw message