You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jackrabbit.apache.org by Gadbury <ga...@googlemail.com> on 2010/04/14 10:13:48 UTC

NodeIterator.getSize() returns -1 for query with order by clause

Hi all,

I have the following the code:

		QueryManager qm = jcrSession.getWorkspace().getQueryManager();

		String queryString = 	"select * from [my:order] as order " +
								"left outer join [my:orderItem] as orderElement on
orderElement.[orderUUID] = order.[jcr:uuid] " + 
								"left outer join [my:product] as product on
orderElement.[productUUID] = product.[jcr:uuid] " +
								"where product.[providerUUID]='" + providerUUID + "' " +
								"order by order.[jcr:created] asc";
						
		Query query = qm.createQuery(queryString, Query.JCR_SQL2); 
		QueryResult queryResult = query.execute();

		NodeIterator nodeIterator = queryResult.getNodes();

		long totalSize = nodeIterator.getSize();
		int anotherTotalSize = ((QueryResultImpl)queryResult).getTotalSize();

totalSize and anotherTotalSize both equal -1 even though I specify an order
by clause.  As far as I understood, specifying an order by clause should
cause the full result set to load and nodeIterator.getSize() should not read
-1.  In an example, I later iterate over the nodeIterator and it has three
my:order nodes, so getSize should be 3.

Does anyone know why getSize is returning -1 in this case?

Is there a quicker way to get the size of the result set?  Perhaps iterate
through the nodeIterator and do my own count?

Thanks and kind regards,

James
-- 
View this message in context: http://n4.nabble.com/NodeIterator-getSize-returns-1-for-query-with-order-by-clause-tp1839436p1839436.html
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.

Re: NodeIterator.getSize() returns -1 for query with order by clause

Posted by Simon Gaeremynck <ga...@gmail.com>.
Response inline
On 25 Apr 2010, at 21:32, Alexander Klimetschek wrote:

> On Fri, Apr 23, 2010 at 15:47, Simon Gaeremynck <ga...@gmail.com> wrote:
>> I'm aware that according to the spec -1 can be returned (for various performance issues) and that's all fine.
>> But I thought you would be able to get the amount of results when the result set count is smaller then the
>> resultFetchSize you can specify for SearchIndex in the repository.xml ?
> 
> Not necessarily, I believe. This was the case for Jackrabbit 1.x, if I
> am not mistaken.

I had a feeling it might be something like this.

> 
>> As previously stated, an order by gives a correct number, but as it is possible that we might have huge datasets at some point,
>> I really don't want to sort them.
> 
> AFAIK "order by @jcr:score descending" is the default sort order, so I
> don't think it is way much slower than not sorting.

Thanks, I'll give it a try!

> 
>> This is also for doing paging, and we're already doing the "Page 1 of hundreds." thing, but our UI developers are
>> complaining that it should be possible to get a count when they only get a small resultset.
> 
> Any implementation that would by default count (ie. not having the -1
> option) would be slower, or as slow as adding that order by. The -1 is
> there to make it faster, if you don't need the count. I thought I made
> that clear in my earlier mail.
> 

Yes, I know, I was just hoping for something to please our UI guys I guess. ;)
Sorry for bringing this up again.

Regards,
Simon

Re: NodeIterator.getSize() returns -1 for query with order by clause

Posted by Alexander Klimetschek <ak...@day.com>.
On Fri, Apr 23, 2010 at 15:47, Simon Gaeremynck <ga...@gmail.com> wrote:
> I'm aware that according to the spec -1 can be returned (for various performance issues) and that's all fine.
> But I thought you would be able to get the amount of results when the result set count is smaller then the
> resultFetchSize you can specify for SearchIndex in the repository.xml ?

Not necessarily, I believe. This was the case for Jackrabbit 1.x, if I
am not mistaken.

> As previously stated, an order by gives a correct number, but as it is possible that we might have huge datasets at some point,
> I really don't want to sort them.

AFAIK "order by @jcr:score descending" is the default sort order, so I
don't think it is way much slower than not sorting.

> This is also for doing paging, and we're already doing the "Page 1 of hundreds." thing, but our UI developers are
> complaining that it should be possible to get a count when they only get a small resultset.

Any implementation that would by default count (ie. not having the -1
option) would be slower, or as slow as adding that order by. The -1 is
there to make it faster, if you don't need the count. I thought I made
that clear in my earlier mail.

Regards,
Alex

-- 
Alexander Klimetschek
alexander.klimetschek@day.com

Re: NodeIterator.getSize() returns -1 for query with order by clause

Posted by Simon Gaeremynck <ga...@gmail.com>.
I don't want to hijack this thread, but I'm having a similar issue.

I'm aware that according to the spec -1 can be returned (for various performance issues) and that's all fine.
But I thought you would be able to get the amount of results when the result set count is smaller then the 
resultFetchSize you can specify for SearchIndex in the repository.xml ?

I've went trough SearchIndex and the QueryResultImpl classes and poked around a bit, but didn't see anything that seems to do this.
As previously stated, an order by gives a correct number, but as it is possible that we might have huge datasets at some point, 
I really don't want to sort them.

This is also for doing paging, and we're already doing the "Page 1 of hundreds." thing, but our UI developers are 
complaining that it should be possible to get a count when they only get a small resultset.

Any thoughts ?

Regards,
Simon

On 14 Apr 2010, at 20:42, Alexander Klimetschek wrote:

> On Wed, Apr 14, 2010 at 18:58, Gadbury <ga...@googlemail.com> wrote:
>> The order by trick does not always work.
> 
> Did you try "order by @jcr:score descending" (for xpath)? That gives
> you the same result as not specifying a sort oder (afaik "stable" in
> jackrabbit 2.0) but should provide a count.
> 
> Regards,
> Alex
> 
> -- 
> Alexander Klimetschek
> alexander.klimetschek@day.com


Re: NodeIterator.getSize() returns -1 for query with order by clause

Posted by Gadbury <ga...@googlemail.com>.
Yes, you're right, it is SQL2.  For SQL2 queries, the order by trick does
work in some cases, but not all, which is causing me a real headache.
-- 
View this message in context: http://jackrabbit.510166.n4.nabble.com/NodeIterator-getSize-returns-1-for-query-with-order-by-clause-tp1839436p2067702.html
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.

Re: NodeIterator.getSize() returns -1 for query with order by clause

Posted by Alexander Klimetschek <ak...@day.com>.
On Tue, Apr 27, 2010 at 10:13, Gadbury <ga...@googlemail.com> wrote:
> Supplying an order by constraint does not always solve the problem of
> node/rowIterator.getSize() returning -1.
>
>
>                select * from [my:order] as orders
>                where orders.[orderStatus] = 2
>                order by orders.[jcr:created] asc
>
> The above query returns the correct amount of nodes BUT
> nodeIterator.getSize() returns -1.
>
>
>
>                select * from [my:order] as orders
>                order by orders.[jcr:created] asc
>
> The above query returns the correct amount of nodes AND
> nodeIterator.getSize() returns the correct value.
>
> So there are cases when supplying order by DOES NOT force a count.

The above queries seem to be JCR-SQL2 queries, because of the "AS"
usage. As I almost expected, that implementation, which is a bit
different from xpath/jcr-sql, doesn't seem to work with the order by
trick.

Regards,
Alex

Re: NodeIterator.getSize() returns -1 for query with order by clause

Posted by Gadbury <ga...@googlemail.com>.
Hi Alex,

Supplying an order by constraint does not always solve the problem of
node/rowIterator.getSize() returning -1.


                select * from [my:order] as orders
                where orders.[orderStatus] = 2
                order by orders.[jcr:created] asc

The above query returns the correct amount of nodes BUT
nodeIterator.getSize() returns -1.



                select * from [my:order] as orders
                order by orders.[jcr:created] asc

The above query returns the correct amount of nodes AND
nodeIterator.getSize() returns the correct value.

So there are cases when supplying order by DOES NOT force a count.

Sorry if I am misunderstanding you.
-- 
View this message in context: http://jackrabbit.510166.n4.nabble.com/NodeIterator-getSize-returns-1-for-query-with-order-by-clause-tp1839436p2067212.html
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.

Re: NodeIterator.getSize() returns -1 for query with order by clause

Posted by Alexander Klimetschek <ak...@day.com>.
On Mon, Apr 26, 2010 at 15:14, Gadbury <ga...@googlemail.com> wrote:
> No I haven't tried that.  Does that always work for XPath queries and only
> for XPath queries?  What about SQL2 queries?

The order by trick should work with both xpath and jcr-sql (from the
JCR 1.0 spec), since they share the same query implementation. I guess
it also works with jcr-sql2 / jqom from the JCR 2.0 spec, but I am not
sure.

Regards,
Alex

-- 
Alexander Klimetschek
alexander.klimetschek@day.com

Re: NodeIterator.getSize() returns -1 for query with order by clause

Posted by Gadbury <ga...@googlemail.com>.
Thanks for the response.

No I haven't tried that.  Does that always work for XPath queries and only
for XPath queries?  What about SQL2 queries?
-- 
View this message in context: http://jackrabbit.510166.n4.nabble.com/NodeIterator-getSize-returns-1-for-query-with-order-by-clause-tp1839436p2065160.html
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.

Re: NodeIterator.getSize() returns -1 for query with order by clause

Posted by Alexander Klimetschek <ak...@day.com>.
On Wed, Apr 14, 2010 at 18:58, Gadbury <ga...@googlemail.com> wrote:
> The order by trick does not always work.

Did you try "order by @jcr:score descending" (for xpath)? That gives
you the same result as not specifying a sort oder (afaik "stable" in
jackrabbit 2.0) but should provide a count.

Regards,
Alex

-- 
Alexander Klimetschek
alexander.klimetschek@day.com

Re: NodeIterator.getSize() returns -1 for query with order by clause

Posted by Gadbury <ga...@googlemail.com>.
Thanks for the response, Alex.

The order by trick does not always work.  Not being able to determine if a
certain query returns -1 or a valid number when sorted is bad news.  I had
assumed that I would not get a -1 if I used order by.

Back to the drawing board then, I guess.  I thought I had nailed our paging
solution.  The example I have read regarding Google Mail's count of your
inbox seems invalid as mine knows precisely how many items are in my inbox
on the first load.

On 14 April 2010 14:18, Alexander Klimetschek [via Jackrabbit] <
ml-node+1839776-23308717-35600@n4.nabble.com<ml...@n4.nabble.com>
> wrote:

> On Wed, Apr 14, 2010 at 12:40, Gadbury <[hidden email]<http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1839776&i=0>>
> wrote:
> > But no real answer as to why with a where clause the getSize() returns
> -1.
>
> The thing is that the spec allows the implementation to return -1.
> This makes it free to optimize certain queries so that they are
> fetched on demand (iteration over the result) without having to know
> the entire result and thus it's count in advance. Jackrabbit's search
> implementation was improved for 2.0 so that the -1 is seen more often
> now.
>
> Which queries trigger a -1 or the actual size can change any time, so
> client code should not rely on it (or be prepared to change). Counting
> yourself by iterating over the result set is an option, albeit it can
> be slower. A trick is to sort the result set (order by in xpath or
> sql), which will be slower than not sorting, but can be faster than
> manual counting (for large result sets).
>
> Regards,
> Alex
>
> --
> Alexander Klimetschek
> [hidden email]<http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1839776&i=1>
>
>
> ------------------------------
>  View message @
> http://n4.nabble.com/NodeIterator-getSize-returns-1-for-query-with-order-by-clause-tp1839436p1839776.html
> To unsubscribe from Re: NodeIterator.getSize() returns -1 for query with
> order by clause, click here< (link removed) =>.
>
>
>

-- 
View this message in context: http://n4.nabble.com/NodeIterator-getSize-returns-1-for-query-with-order-by-clause-tp1839436p1840146.html
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.

Re: NodeIterator.getSize() returns -1 for query with order by clause

Posted by Alexander Klimetschek <ak...@day.com>.
On Wed, Apr 14, 2010 at 12:40, Gadbury <ga...@googlemail.com> wrote:
> But no real answer as to why with a where clause the getSize() returns -1.

The thing is that the spec allows the implementation to return -1.
This makes it free to optimize certain queries so that they are
fetched on demand (iteration over the result) without having to know
the entire result and thus it's count in advance. Jackrabbit's search
implementation was improved for 2.0 so that the -1 is seen more often
now.

Which queries trigger a -1 or the actual size can change any time, so
client code should not rely on it (or be prepared to change). Counting
yourself by iterating over the result set is an option, albeit it can
be slower. A trick is to sort the result set (order by in xpath or
sql), which will be slower than not sorting, but can be faster than
manual counting (for large result sets).

Regards,
Alex

-- 
Alexander Klimetschek
alexander.klimetschek@day.com

Re: NodeIterator.getSize() returns -1 for query with order by clause

Posted by Gadbury <ga...@googlemail.com>.
Thanks Jacco.  The most relevant post to me was from Guo Du:

"Not always working on 2.0-alpha12 (when has where condition) :(

WORKING: select * from [nt:base] order by title .size() RETURN 1330
NOT WORKING: select * from [nt:base] where title like 'a%' order by
title .size() RETURN -1 (there are 2 records) "

But no real answer as to why with a where clause the getSize() returns -1.
-- 
View this message in context: http://n4.nabble.com/NodeIterator-getSize-returns-1-for-query-with-order-by-clause-tp1839436p1839593.html
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.

Re: NodeIterator.getSize() returns -1 for query with order by clause

Posted by Jacco van Weert <11...@gmail.com>.
See this thread for some answers

http://n4.nabble.com/Queries-not-working-in-Jackrabbit-2-0-td1558394.html


On Wed, Apr 14, 2010 at 10:58 AM, Gadbury <ga...@googlemail.com> wrote:

>
> To make things a little more simple, I have been experimenting with smaller
> queries that do not use joins.
>
>                select * from [my:order] as orders
>                where orders.[orderStatus] = 2
>                order by orders.[jcr:created] asc
>
> The above query returns the correct amount of nodes BUT
> nodeIterator.getSize() returns -1.
>
>                select * from [my:order] as orders
>                order by orders.[jcr:created] asc
>
>
> The above query returns the correct amount of nodes AND
> nodeIterator.getSize() returns the correct value.
>
> Why when I supply the property constraint is nodeIterator.getSize()
> returning -1?
> --
> View this message in context:
> http://n4.nabble.com/NodeIterator-getSize-returns-1-for-query-with-order-by-clause-tp1839436p1839484.html
> Sent from the Jackrabbit - Users mailing list archive at Nabble.com.
>



-- 
-------------------------------------
Jacco van Weert -- 1111software@gmail.com
JCR Controller -- http://www.xs4all.nl/~weertj/jcr
JeCARS -- http://code.google.com/p/jecars/

Re: NodeIterator.getSize() returns -1 for query with order by clause

Posted by Gadbury <ga...@googlemail.com>.
To make things a little more simple, I have been experimenting with smaller
queries that do not use joins.

		select * from [my:order] as orders
		where orders.[orderStatus] = 2
		order by orders.[jcr:created] asc

The above query returns the correct amount of nodes BUT
nodeIterator.getSize() returns -1.

		select * from [my:order] as orders
		order by orders.[jcr:created] asc


The above query returns the correct amount of nodes AND
nodeIterator.getSize() returns the correct value.

Why when I supply the property constraint is nodeIterator.getSize()
returning -1?
-- 
View this message in context: http://n4.nabble.com/NodeIterator-getSize-returns-1-for-query-with-order-by-clause-tp1839436p1839484.html
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.