You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jackrabbit.apache.org by Amir Mistric <am...@nemours.org> on 2007/08/08 03:50:59 UTC
Finding out if node is a last child of a parent
Hi everyone
This may be an obvious question, but here it is anyway...
- What is the *best* way to find out if a child node is the last child of its parent (based on certain node type)?
- Is there an API that already does this or would I have to create my own method.
- If I have to create my own method, what would you recommend? Do an Xpath query using last() or get a count of children and compare last child node with passed in one?
Thanks
Amir
RE: Finding out if node is a last child of a parent
Posted by Amir Mistric <am...@nemours.org>.
OK
Thanks for the pointers, I will go with the iteration then.
Thanks
Amir
> -----Original Message-----
> From: Jukka Zitting [mailto:jukka.zitting@gmail.com]
> Sent: Wednesday, August 08, 2007 7:59 AM
> To: users@jackrabbit.apache.org
> Subject: Re: Finding out if node is a last child of a parent
>
> Hi,
>
> On 8/8/07, Amir Mistric <am...@nemours.org> wrote:
> > Now the question is - what is *faster* - iterating or
> xpath? What do you think?
>
> Iterating will definitely be faster, unless your node
> hierarchy is very flat (i.e. lots of child nodes per parent)
> which is discouraged in any case.
>
> I agree that the solution is suboptimal, but it's about the
> best you can do with the current API.
>
> BR,
>
> Jukka Zitting
>
Re: Finding out if node is a last child of a parent
Posted by Jukka Zitting <ju...@gmail.com>.
Hi,
On 8/8/07, Amir Mistric <am...@nemours.org> wrote:
> Now the question is - what is *faster* - iterating or xpath? What do you think?
Iterating will definitely be faster, unless your node hierarchy is
very flat (i.e. lots of child nodes per parent) which is discouraged
in any case.
I agree that the solution is suboptimal, but it's about the best you
can do with the current API.
BR,
Jukka Zitting
Re: Finding out if node is a last child of a parent
Posted by Jukka Zitting <ju...@gmail.com>.
Hi,
On 8/14/07, Marcel Reutegger <ma...@gmx.net> wrote:
> Brian Thompson wrote:
> > Why not do something like
> >
> > Node node = ...
> > NodeIterator iterator = node.getParent().getNodes();
> > long numSibs = iterator.getSize();
> > iterator.skip(numSibs-1);
> > if (node.isSame(iterator.nextNode())) {
> > return /* this is the last sibling */;
> > } else {
> > return /* this is not the last sibling */;
> > }
> >
> > Even if iterator.skip(n) is no more efficient than n calls of iterator.next(),
> > it saves a few operations in the body of the while loop.
>
> I'd also say the above code is considerably more efficient than actually
> iterating over the nodes because skipping in jackrabbit does not load the node
> instances but works on the list of child node entries.
Could we make the loading of the underlying node states lazy? We could
considerably speed up traversal operations that only need to invoke
methods like getName() or isSame() as we already have the names and
identifiers of the child nodes.
> please note that getSize() may also return -1 if the size cannot be determined.
That's a big reason why I typically avoid using getSize(). A failsafe
implementation that uses getSize() and skip() would still need to
include the next() iteration as a fallback to remain independent of
the underlying implementation.
BR,
Jukka Zitting
Re: Finding out if node is a last child of a parent
Posted by Marcel Reutegger <ma...@gmx.net>.
Brian Thompson wrote:
> Why not do something like
>
> Node node = ...
> NodeIterator iterator = node.getParent().getNodes();
> long numSibs = iterator.getSize();
> iterator.skip(numSibs-1);
> if (node.isSame(iterator.nextNode())) {
> return /* this is the last sibling */;
> } else {
> return /* this is not the last sibling */;
> }
>
> Even if iterator.skip(n) is no more efficient than n calls of iterator.next(),
> it saves a few operations in the body of the while loop.
I'd also say the above code is considerably more efficient than actually
iterating over the nodes because skipping in jackrabbit does not load the node
instances but works on the list of child node entries.
please note that getSize() may also return -1 if the size cannot be determined.
regards
marcel
Re: Finding out if node is a last child of a parent
Posted by Brian Thompson <el...@gmail.com>.
Comments below
On 8/8/07, Jukka Zitting <ju...@gmail.com> wrote:
>
> Hi,
>
> On 8/8/07, Amir Mistric <am...@nemours.org> wrote:
> > - What is the *best* way to find out if a child node is the last child
> of its
> > parent (based on certain node type)?
> > - Is there an API that already does this or would I have to create my
> > own method.
> > - If I have to create my own method, what would you recommend?
> > Do an Xpath query using last() or get a count of children and compare
> > last child node with passed in one?
>
> JCR doesn't have methods like Node.getNext() or Node.getPrevious()
> that would allow you to do sideways traversal.
>
> I guess the best way to achieve your goal would be:
>
> Node node = ...
> NodeIterator iterator = node.getParent().getNodes();
> while (iterator.hasNext()) {
> if (node.isSame(iterator.nextNode())) {
> if (iterator.hasNext()) {
> return /* this is not the last sibling */;
> } else {
> return /* this is the last sibling */;
> }
> }
> }
>
> This approach is obviously not too efficient, but can be used to
> implement your use case as well as the "next"/"previous" links
> discussed in another post a while ago.
>
> Perhaps we should ask for JSR 283 to consider adding support for
> sibling access methods?
>
> BR,
>
> Jukka Zitting
Why not do something like
Node node = ...
NodeIterator iterator = node.getParent().getNodes();
long numSibs = iterator.getSize();
iterator.skip(numSibs-1);
if (node.isSame(iterator.nextNode())) {
return /* this is the last sibling */;
} else {
return /* this is not the last sibling */;
}
Even if iterator.skip(n) is no more efficient than n calls of iterator.next(),
it saves a few operations in the body of the while loop.
-Brian
RE: Finding out if node is a last child of a parent
Posted by Amir Mistric <am...@nemours.org>.
Hi Jukka
Iterating is what I am trying to avoid as it seems to be *expensive*.
Honestly, I don't need to the node itself just the fact if it is last.
I was experimenting with some Xpaht queries and something like
/jcr:root/library/image/banner/*[last()]
Seems to work pretty good....
Now the question is - what is *faster* - iterating or xpath? What do you think?
I do agree 2.0 should have support for sibling methods (isLast, isFirst, isOnly)
Thanks
Amir
> -----Original Message-----
> From: Jukka Zitting [mailto:jukka.zitting@gmail.com]
> Sent: Wednesday, August 08, 2007 5:42 AM
> To: users@jackrabbit.apache.org
> Subject: Re: Finding out if node is a last child of a parent
>
> Hi,
>
> On 8/8/07, Amir Mistric <am...@nemours.org> wrote:
> > - What is the *best* way to find out if a child node is the
> last child
> > of its parent (based on certain node type)?
> > - Is there an API that already does this or would I have to
> create my
> > own method.
> > - If I have to create my own method, what would you recommend?
> > Do an Xpath query using last() or get a count of children
> and compare
> > last child node with passed in one?
>
> JCR doesn't have methods like Node.getNext() or
> Node.getPrevious() that would allow you to do sideways traversal.
>
> I guess the best way to achieve your goal would be:
>
> Node node = ...
> NodeIterator iterator = node.getParent().getNodes();
> while (iterator.hasNext()) {
> if (node.isSame(iterator.nextNode())) {
> if (iterator.hasNext()) {
> return /* this is not the last sibling */;
> } else {
> return /* this is the last sibling */;
> }
> }
> }
>
> This approach is obviously not too efficient, but can be used
> to implement your use case as well as the "next"/"previous"
> links discussed in another post a while ago.
>
> Perhaps we should ask for JSR 283 to consider adding support
> for sibling access methods?
>
> BR,
>
> Jukka Zitting
>
Re: Finding out if node is a last child of a parent
Posted by Jukka Zitting <ju...@gmail.com>.
Hi,
On 8/8/07, Amir Mistric <am...@nemours.org> wrote:
> - What is the *best* way to find out if a child node is the last child of its
> parent (based on certain node type)?
> - Is there an API that already does this or would I have to create my
> own method.
> - If I have to create my own method, what would you recommend?
> Do an Xpath query using last() or get a count of children and compare
> last child node with passed in one?
JCR doesn't have methods like Node.getNext() or Node.getPrevious()
that would allow you to do sideways traversal.
I guess the best way to achieve your goal would be:
Node node = ...
NodeIterator iterator = node.getParent().getNodes();
while (iterator.hasNext()) {
if (node.isSame(iterator.nextNode())) {
if (iterator.hasNext()) {
return /* this is not the last sibling */;
} else {
return /* this is the last sibling */;
}
}
}
This approach is obviously not too efficient, but can be used to
implement your use case as well as the "next"/"previous" links
discussed in another post a while ago.
Perhaps we should ask for JSR 283 to consider adding support for
sibling access methods?
BR,
Jukka Zitting