Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Clean up query iterators (encapsulate fields in AbstractIterator, etc.)

  • Loading branch information...
commit 05bc78d12920c72cddd9c68a3640993c0330a74f 1 parent 2ca5b07
@luksa luksa authored Sanne committed
View
22 query/src/main/java/org/infinispan/query/impl/AbstractIterator.java
@@ -39,34 +39,38 @@
*/
public abstract class AbstractIterator implements ResultIterator {
- protected final Object[] buffer;
+ private final Object[] buffer;
/**
* Index of the element that will be returned by next()
*/
- protected int index = 0;
+ private int index = 0;
/**
* The index at which the buffer starts (the global index of the element at buffer[0])
*/
- protected int bufferIndex = -1;
+ private int bufferIndex = -1;
+
+ /**
+ * The index of the last element that will be returned by this iterator.
+ */
+ private final int lastIndex;
- protected int max;
- protected final int fetchSize;
private final QueryResultLoader resultLoader;
- protected AbstractIterator(QueryResultLoader resultLoader, int fetchSize) {
+ protected AbstractIterator(QueryResultLoader resultLoader, int firstIndex, int lastIndex, int fetchSize) {
if (fetchSize < 1) {
throw new IllegalArgumentException("Incorrect value for fetchSize passed. Your fetchSize is less than 1");
}
this.resultLoader = resultLoader;
- this.fetchSize = fetchSize;
+ this.index = firstIndex;
+ this.lastIndex = lastIndex;
this.buffer = new Object[fetchSize];
}
@Override
public boolean hasNext() {
- return index <= max;
+ return index <= lastIndex;
}
/**
@@ -98,7 +102,7 @@ private boolean mustInitializeBuffer() {
private void fillBuffer(int startIndex) {
bufferIndex = startIndex;
- int resultsToLoad = Math.min( buffer.length, max + 1 - bufferIndex);
+ int resultsToLoad = Math.min( buffer.length, lastIndex + 1 - bufferIndex);
for (int i = 0; i < resultsToLoad; i++) {
buffer[i] = loadResult(bufferIndex + i);
}
View
4 query/src/main/java/org/infinispan/query/impl/CacheQueryImpl.java
@@ -33,6 +33,7 @@
import org.apache.lucene.search.Sort;
import org.hibernate.search.FullTextFilter;
import org.hibernate.search.SearchException;
+import org.hibernate.search.query.engine.spi.DocumentExtractor;
import org.hibernate.search.query.engine.spi.EntityInfo;
import org.hibernate.search.query.engine.spi.FacetManager;
import org.hibernate.search.query.engine.spi.HSQuery;
@@ -166,7 +167,8 @@ public ResultIterator iterator(FetchOptions fetchOptions) throws SearchException
List<EntityInfo> entityInfos = hSearchQuery.queryEntityInfos();
return new EagerIterator(entityInfos, getResultLoader(), fetchOptions.getFetchSize() );
} else if (fetchOptions.getFetchMode() == FetchOptions.FetchMode.LAZY) {
- return new LazyIterator(hSearchQuery, getResultLoader(), fetchOptions.getFetchSize());
+ DocumentExtractor extractor = hSearchQuery.queryDocumentExtractor(); //triggers actual Lucene search
+ return new LazyIterator(extractor, getResultLoader(), fetchOptions.getFetchSize());
} else {
throw new IllegalArgumentException("Unknown FetchMode " + fetchOptions.getFetchMode());
}
View
4 query/src/main/java/org/infinispan/query/impl/EagerIterator.java
@@ -44,10 +44,8 @@
private List<EntityInfo> entityInfos;
public EagerIterator(List<EntityInfo> entityInfos, QueryResultLoader resultLoader, int fetchSize) {
- super(resultLoader, fetchSize);
+ super(resultLoader, 0, entityInfos.size() - 1, fetchSize);
this.entityInfos = entityInfos;
-
- max = entityInfos.size() - 1;
}
@Override
View
9 query/src/main/java/org/infinispan/query/impl/LazyIterator.java
@@ -28,7 +28,6 @@
import net.jcip.annotations.NotThreadSafe;
import org.hibernate.search.query.engine.spi.DocumentExtractor;
import org.hibernate.search.query.engine.spi.EntityInfo;
-import org.hibernate.search.query.engine.spi.HSQuery;
import org.infinispan.CacheException;
/**
@@ -44,11 +43,9 @@
private final DocumentExtractor extractor;
- public LazyIterator(HSQuery hSearchQuery, QueryResultLoader resultLoader, int fetchSize) {
- super(resultLoader, fetchSize);
- this.extractor = hSearchQuery.queryDocumentExtractor(); //triggers actual Lucene search
- this.index = extractor.getFirstIndex();
- this.max = extractor.getMaxIndex();
+ public LazyIterator(DocumentExtractor extractor, QueryResultLoader resultLoader, int fetchSize) {
+ super(resultLoader, extractor.getFirstIndex(), extractor.getMaxIndex(), fetchSize);
+ this.extractor = extractor;
}
@Override
View
22 query/src/test/java/org/infinispan/query/impl/InvalidIteratorTest.java
@@ -24,7 +24,6 @@
import org.hibernate.search.query.engine.spi.DocumentExtractor;
import org.hibernate.search.query.engine.spi.EntityInfo;
-import org.hibernate.search.query.engine.spi.HSQuery;
import org.infinispan.AdvancedCache;
import org.infinispan.query.backend.KeyTransformationHandler;
import org.mockito.invocation.InvocationOnMock;
@@ -60,31 +59,14 @@ public EntityInfo answer(InvocationOnMock invocation) throws Throwable {
}
});
- HSQuery hsQuery = mock(HSQuery.class);
cache = mock(AdvancedCache.class);
- when(hsQuery.queryDocumentExtractor()).thenReturn(extractor);
- when(hsQuery.queryResultSize()).thenReturn(entityInfos.size());
-
- LazyIterator iterator = new LazyIterator(hsQuery, new EntityLoader(cache, new KeyTransformationHandler()), getFetchSize());
+ new LazyIterator(extractor, new EntityLoader(cache, new KeyTransformationHandler()), getFetchSize());
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testEagerIteratorInitWithInvalidFetchSize() throws IOException {
- DocumentExtractor extractor = mock(DocumentExtractor.class);
- when(extractor.extract(anyInt())).thenAnswer(new Answer<EntityInfo>() {
- @Override
- public EntityInfo answer(InvocationOnMock invocation) throws Throwable {
- int index = (Integer) invocation.getArguments()[0];
- return entityInfos.get(index);
- }
- });
-
- HSQuery hsQuery = mock(HSQuery.class);
cache = mock(AdvancedCache.class);
- when(hsQuery.queryDocumentExtractor()).thenReturn(extractor);
- when(hsQuery.queryResultSize()).thenReturn(entityInfos.size());
-
- EagerIterator iterator = new EagerIterator(entityInfos, new EntityLoader(cache, new KeyTransformationHandler()), getFetchSize());
+ new EagerIterator(entityInfos, new EntityLoader(cache, new KeyTransformationHandler()), getFetchSize());
}
private int getFetchSize() {
View
6 query/src/test/java/org/infinispan/query/impl/LazyIteratorTest.java
@@ -29,7 +29,6 @@
import org.hibernate.search.query.engine.spi.DocumentExtractor;
import org.hibernate.search.query.engine.spi.EntityInfo;
-import org.hibernate.search.query.engine.spi.HSQuery;
import org.infinispan.query.backend.KeyTransformationHandler;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
@@ -60,10 +59,7 @@ public EntityInfo answer(InvocationOnMock invocation) throws Throwable {
}
});
- HSQuery hsQuery = mock(HSQuery.class);
- when(hsQuery.queryDocumentExtractor()).thenReturn(extractor);
-
- iterator = new LazyIterator(hsQuery, new EntityLoader(cache, new KeyTransformationHandler()), getFetchSize());
+ iterator = new LazyIterator(extractor, new EntityLoader(cache, new KeyTransformationHandler()), getFetchSize());
}
@AfterMethod(alwaysRun = false)
Please sign in to comment.
Something went wrong with that request. Please try again.