Skip to content

Commit

Permalink
JCR-3474: Add JackrabbitQueryResult.getTotalSize()
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/jackrabbit/trunk@1415968 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
jukka committed Dec 1, 2012
1 parent ad814e9 commit e068fb6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jackrabbit.api.query;

import javax.jcr.query.QueryResult;

/**
* The Jackrabbit query result interface. This interface contains the
* Jackrabbit-specific extensions to the JCR {@link QueryResult} interface.
*
* @since Jackrabbit 2.6
*/
public interface JackrabbitQueryResult extends QueryResult {

/**
* Returns the total number of hits. This is the number of results you
* would get without any limit or offset settings. This method may return
* <code>-1</code> if the total size is unknown.
*
* @return the total number of hits, or <code>-1</code>
*/
int getTotalSize();

}
Expand Up @@ -26,9 +26,9 @@
import javax.jcr.ItemNotFoundException; import javax.jcr.ItemNotFoundException;
import javax.jcr.NodeIterator; import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException; import javax.jcr.RepositoryException;
import javax.jcr.query.QueryResult;
import javax.jcr.query.RowIterator; import javax.jcr.query.RowIterator;


import org.apache.jackrabbit.api.query.JackrabbitQueryResult;
import org.apache.jackrabbit.core.session.SessionContext; import org.apache.jackrabbit.core.session.SessionContext;
import org.apache.jackrabbit.spi.Name; import org.apache.jackrabbit.spi.Name;
import org.apache.jackrabbit.spi.commons.query.qom.ColumnImpl; import org.apache.jackrabbit.spi.commons.query.qom.ColumnImpl;
Expand All @@ -38,7 +38,7 @@
/** /**
* Implements the <code>QueryResult</code> interface. * Implements the <code>QueryResult</code> interface.
*/ */
public abstract class QueryResultImpl implements QueryResult { public abstract class QueryResultImpl implements JackrabbitQueryResult {


/** /**
* The logger instance for this class * The logger instance for this class
Expand Down Expand Up @@ -364,10 +364,12 @@ private boolean isAccessGranted(ScoreNode[] nodes)


/** /**
* Returns the total number of hits. This is the number of results you * Returns the total number of hits. This is the number of results you
* will get get if you don't set any limit or offset. Keep in mind that this * will get get if you don't set any limit or offset. This method may return
* number may get smaller if nodes are found in the result set which the
* current session has no permission to access. This method may return
* <code>-1</code> if the total size is unknown. * <code>-1</code> if the total size is unknown.
* <p>
* Keep in mind that this number may get smaller if nodes are found in
* the result set which the current session has no permission to access.
* FIXME: This might be a security problem.
* *
* @return the total number of hits. * @return the total number of hits.
*/ */
Expand Down
Expand Up @@ -49,17 +49,8 @@ public class AbstractQueryTest extends AbstractJCRTest {


protected QueryObjectModelFactory qomFactory; protected QueryObjectModelFactory qomFactory;


/**
* true if this is a Jackrabbit JCR implementation
*/
boolean isJackrabbitImpl = true;
private static final String DESCRIPTOR_NAME = "jcr.repository.name";
private static final String DESCRIPTOR_VALUE = "Jackrabbit";

protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
isJackrabbitImpl = DESCRIPTOR_VALUE.equals(getHelper().getRepository()
.getDescriptor(DESCRIPTOR_NAME));
qm = superuser.getWorkspace().getQueryManager(); qm = superuser.getWorkspace().getQueryManager();
qomFactory = qm.getQOMFactory(); qomFactory = qm.getQOMFactory();
} }
Expand Down
Expand Up @@ -22,7 +22,7 @@
import javax.jcr.query.Query; import javax.jcr.query.Query;
import javax.jcr.query.QueryResult; import javax.jcr.query.QueryResult;


import org.apache.jackrabbit.core.query.lucene.QueryResultImpl; import org.apache.jackrabbit.api.query.JackrabbitQueryResult;


public class LimitAndOffsetTest extends AbstractQueryTest { public class LimitAndOffsetTest extends AbstractQueryTest {


Expand Down Expand Up @@ -129,8 +129,8 @@ public void testOffsetAndLimitWithGetSize() throws Exception {
QueryResult result = query.execute(); QueryResult result = query.execute();
NodeIterator nodes = result.getNodes(); NodeIterator nodes = result.getNodes();
assertEquals(2, nodes.getSize()); assertEquals(2, nodes.getSize());
if (isJackrabbitImpl) { if (result instanceof JackrabbitQueryResult) {
assertEquals(3, ((QueryResultImpl) result).getTotalSize()); assertEquals(3, ((JackrabbitQueryResult) result).getTotalSize());
} }


// JCR-2684: offset higher than total result => size == 0 // JCR-2684: offset higher than total result => size == 0
Expand All @@ -139,17 +139,17 @@ public void testOffsetAndLimitWithGetSize() throws Exception {
nodes = result.getNodes(); nodes = result.getNodes();
assertFalse(nodes.hasNext()); assertFalse(nodes.hasNext());
assertEquals(0, nodes.getSize()); assertEquals(0, nodes.getSize());
if (isJackrabbitImpl) { if (result instanceof JackrabbitQueryResult) {
assertEquals(3, ((QueryResultImpl) result).getTotalSize()); assertEquals(3, ((JackrabbitQueryResult) result).getTotalSize());
} }


query.setOffset(1); query.setOffset(1);
query.setLimit(1); query.setLimit(1);
result = query.execute(); result = query.execute();
nodes = result.getNodes(); nodes = result.getNodes();
assertEquals(1, nodes.getSize()); assertEquals(1, nodes.getSize());
if (isJackrabbitImpl) { if (result instanceof JackrabbitQueryResult) {
assertEquals(3, ((QueryResultImpl) result).getTotalSize()); assertEquals(3, ((JackrabbitQueryResult) result).getTotalSize());
} }
} }


Expand Down

0 comments on commit e068fb6

Please sign in to comment.