Return-Path: X-Original-To: apmail-jackrabbit-oak-commits-archive@minotaur.apache.org Delivered-To: apmail-jackrabbit-oak-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id C9B579EE8 for ; Wed, 24 Oct 2012 11:51:41 +0000 (UTC) Received: (qmail 45694 invoked by uid 500); 24 Oct 2012 11:51:41 -0000 Delivered-To: apmail-jackrabbit-oak-commits-archive@jackrabbit.apache.org Received: (qmail 45649 invoked by uid 500); 24 Oct 2012 11:51:40 -0000 Mailing-List: contact oak-commits-help@jackrabbit.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: oak-dev@jackrabbit.apache.org Delivered-To: mailing list oak-commits@jackrabbit.apache.org Received: (qmail 45619 invoked by uid 99); 24 Oct 2012 11:51:39 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 24 Oct 2012 11:51:39 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 24 Oct 2012 11:51:38 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 02FD523888FD; Wed, 24 Oct 2012 11:50:55 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1401633 - /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Query.java Date: Wed, 24 Oct 2012 11:50:54 -0000 To: oak-commits@jackrabbit.apache.org From: thomasm@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20121024115055.02FD523888FD@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: thomasm Date: Wed Oct 24 11:50:54 2012 New Revision: 1401633 URL: http://svn.apache.org/viewvc?rev=1401633&view=rev Log: OAK-308 NodeIterator limit and offset don't work as expected Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Query.java Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Query.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Query.java?rev=1401633&r1=1401632&r2=1401633&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Query.java (original) +++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Query.java Wed Oct 24 11:50:54 2012 @@ -308,7 +308,13 @@ public class Query { null); it = Arrays.asList(r).iterator(); } else { - it = new RowIterator(root, limit, offset); + if (orderings == null) { + // can apply limit and offset directly + it = new RowIterator(root, limit, offset); + } else { + // read and order first; skip and limit afterwards + it = new RowIterator(root, Long.MAX_VALUE, 0); + } long resultCount = 0; if (orderings != null) { // TODO "order by" is not necessary if the used index returns @@ -318,9 +324,25 @@ public class Query { ResultRowImpl r = it.next(); list.add(r); } - resultCount = size = list.size(); Collections.sort(list); + // if limit is set, possibly remove the tailing entries + // for list size 10, offset 2, limit 5: remove 3 + resultCount = list.size(); + // avoid overflow (both offset and limit could be Long.MAX_VALUE) + long keep = Math.min(list.size(), offset) + Math.min(list.size(), limit); + while (list.size() > keep) { + // remove tail entries right now, to save memory (don't copy) + // remove the entries starting at the end, + // to avoid n^2 performance + list.remove(list.size() - 1); + } it = list.iterator(); + // skip the head (this is more efficient than removing + // if there are many entries) + for (int i = 0; i < offset && it.hasNext(); i++) { + it.next(); + } + size = list.size() - offset; } else if (measure) { while (it.hasNext()) { resultCount++;