@Override public List<E> list() throws SearchException { List<E> values = new ArrayList<>(); try (ResultIterator<E> iterator = iterator(new FetchOptions().fetchMode(FetchOptions.FetchMode.EAGER))) { while (iterator.hasNext()) { values.add(iterator.next()); } } return values; }
@Test(expectedExceptions = IllegalArgumentException.class) public void testLazyIteratorWithInvalidFetchSize() throws ParseException { loadTestingData(); queryParser = createQueryParser("blurb"); Query luceneQuery = queryParser.parse("Eats"); CacheQuery<?> cacheQuery = Search.getSearchManager(cache).<Person>getQuery(luceneQuery).firstResult(1); cacheQuery.iterator(new FetchOptions().fetchMode(FetchOptions.FetchMode.LAZY).fetchSize(0)); }
@Override public ResultIterator<E> iterator(FetchOptions fetchOptions) throws SearchException { if (fetchOptions.getFetchMode() == FetchOptions.FetchMode.EAGER) { hSearchQuery.getTimeoutManager().start(); List<EntityInfo> entityInfos = hSearchQuery.queryEntityInfos(); return filterNulls(new EagerIterator<>(entityInfos, getResultLoader(), fetchOptions.getFetchSize())); } else if (fetchOptions.getFetchMode() == FetchOptions.FetchMode.LAZY) { DocumentExtractor extractor = hSearchQuery.queryDocumentExtractor(); //triggers actual Lucene search return filterNulls(new LazyIterator<>(extractor, getResultLoader(), fetchOptions.getFetchSize())); } else { throw new IllegalArgumentException("Unknown FetchMode " + fetchOptions.getFetchMode()); } }
@Override public List<E> list() throws SearchException { partitionHandlingSupport.checkCacheAvailable(); List<E> values = new ArrayList<>(); try (ResultIterator<E> iterator = iterator(new FetchOptions().fetchMode(FetchOptions.FetchMode.EAGER))) { while (iterator.hasNext()) { values.add(iterator.next()); } } return values; }
@Override public ResultIterator<E> iterator(FetchOptions fetchOptions) throws SearchException { partitionHandlingSupport.checkCacheAvailable(); HSQuery hSearchQuery = queryDefinition.getHsQuery(); if (fetchOptions.getFetchMode() == FetchOptions.FetchMode.EAGER) { hSearchQuery.getTimeoutManager().start(); List<EntityInfo> entityInfos = hSearchQuery.queryEntityInfos(); return filterNulls(new EagerIterator<>(entityInfos, getResultLoader(hSearchQuery), fetchOptions.getFetchSize())); } else if (fetchOptions.getFetchMode() == FetchOptions.FetchMode.LAZY) { DocumentExtractor extractor = hSearchQuery.queryDocumentExtractor(); //triggers actual Lucene search return filterNulls(new LazyIterator<>(extractor, getResultLoader(hSearchQuery), fetchOptions.getFetchSize())); } else { throw new IllegalArgumentException("Unknown FetchMode " + fetchOptions.getFetchMode()); } }
private void assertQueryReturns(CacheQuery<?> cacheQuery, Object[] expected) { assertQueryListContains(cacheQuery.list(), expected); try (ResultIterator<?> eagerIterator = cacheQuery.iterator(new FetchOptions().fetchMode(FetchOptions.FetchMode.EAGER))) { assertQueryIteratorContains(eagerIterator, expected); } try (ResultIterator<?> lazyIterator = cacheQuery.iterator(new FetchOptions().fetchMode(FetchOptions.FetchMode.LAZY))) { assertQueryIteratorContains(lazyIterator, expected); } }
@Override public ResultIterator<E> iterator(FetchOptions fetchOptions) throws SearchException { hSearchQuery.maxResults(getNodeMaxResults()); switch (fetchOptions.getFetchMode()) { case EAGER: { ClusteredQueryCommand command = ClusteredQueryCommand.createEagerIterator(hSearchQuery, cache); HashMap<UUID, ClusteredTopDocs> topDocsResponses = broadcastQuery(command); return new DistributedIterator<>(sort, fetchOptions.getFetchSize(), this.resultSize, maxResults, firstResult, topDocsResponses, cache); } case LAZY: { UUID lazyItId = UUID.randomUUID(); ClusteredQueryCommand command = ClusteredQueryCommand.createLazyIterator(hSearchQuery, cache, lazyItId); HashMap<UUID, ClusteredTopDocs> topDocsResponses = broadcastQuery(command); // Make a sort copy to avoid reversed results return new DistributedLazyIterator<>(sort, fetchOptions.getFetchSize(), this.resultSize, maxResults, firstResult, lazyItId, topDocsResponses, asyncExecutor, cache); } default: throw new IllegalArgumentException("Unknown FetchMode " + fetchOptions.getFetchMode()); } }
public void testLazyNonOrdered() { try (ResultIterator<Person> ignored = cacheQuery.iterator(new FetchOptions().fetchMode(FetchOptions.FetchMode.LAZY))) { assert cacheQuery.getResultSize() == 10 : cacheQuery.getResultSize(); } StaticTestingErrorHandler.assertAllGood(cacheAMachine1, cacheAMachine2); }
@Override public ResultIterator<E> iterator(FetchOptions fetchOptions) throws SearchException { partitionHandlingSupport.checkCacheAvailable(); queryDefinition.setMaxResults(getNodeMaxResults()); switch (fetchOptions.getFetchMode()) { case EAGER: { ClusteredQueryCommand command = ClusteredQueryCommand.createEagerIterator(queryDefinition, cache); Map<Address, NodeTopDocs> topDocsResponses = broadcastQuery(command); return new DistributedIterator<>(queryDefinition.getSort(), fetchOptions.getFetchSize(), resultSize, maxResults, firstResult, topDocsResponses, cache); } case LAZY: { UUID queryId = UUID.randomUUID(); ClusteredQueryCommand command = ClusteredQueryCommand.createLazyIterator(queryDefinition, cache, queryId); Map<Address, NodeTopDocs> topDocsResponses = broadcastQuery(command); // Make a sort copy to avoid reversed results return new DistributedLazyIterator<>(queryDefinition.getSort(), fetchOptions.getFetchSize(), resultSize, maxResults, firstResult, queryId, topDocsResponses, invoker, cache); } default: throw new IllegalArgumentException("Unknown FetchMode " + fetchOptions.getFetchMode()); } }
@Test(expectedExceptions = UnsupportedOperationException.class) public void testIteratorRemove() { cacheQuery.maxResults(1); try (ResultIterator<Person> iterator = cacheQuery.iterator(new FetchOptions().fetchMode(FetchOptions.FetchMode.EAGER))) { assert iterator.hasNext(); iterator.remove(); } StaticTestingErrorHandler.assertAllGood(cacheAMachine1, cacheAMachine2); }
@Test(expectedExceptions = UnsupportedOperationException.class) public void testEagerIteratorRemove() throws ParseException { loadTestingData(); CacheQuery<?> cacheQuery = createCacheQuery(cache, "blurb", "playing"); ResultIterator<?> found = cacheQuery.iterator(new FetchOptions().fetchMode(FetchOptions.FetchMode.EAGER)); try { assertTrue(found.hasNext()); found.remove(); } finally { found.close(); } }
@Test(expectedExceptions = NoSuchElementException.class) public void testIteratorNextOutOfBounds() { cacheQuery.maxResults(1); try (ResultIterator<Person> iterator = cacheQuery.iterator(new FetchOptions().fetchMode(FetchOptions.FetchMode.EAGER))) { assert iterator.hasNext(); iterator.next(); assert !iterator.hasNext(); iterator.next(); } StaticTestingErrorHandler.assertAllGood(cacheAMachine1, cacheAMachine2); }
@Test(expectedExceptions = NoSuchElementException.class) public void testLazyIteratorWithNoElementsFound() throws ParseException { loadTestingData(); queryParser = createQueryParser("blurb"); Query luceneQuery = queryParser.parse("fish"); CacheQuery<?> cacheQuery = Search.getSearchManager(cache).<Person>getQuery(luceneQuery).firstResult(1); ResultIterator<?> found = cacheQuery.iterator(new FetchOptions().fetchMode(FetchOptions.FetchMode.LAZY)); try { found.next(); } finally { found.close(); } }
@Test(expectedExceptions = IllegalArgumentException.class) public void testIteratorWithNullFetchMode() throws ParseException { loadTestingData(); queryParser = createQueryParser("blurb"); Query luceneQuery = queryParser.parse("Eats"); CacheQuery<?> cacheQuery = Search.getSearchManager(cache).<Person>getQuery(luceneQuery).firstResult(1); ResultIterator<?> found = cacheQuery.iterator(new FetchOptions().fetchMode(null)); try { found.next(); } finally { found.close(); } }
@Test(expectedExceptions = NoSuchElementException.class) public void testEagerIteratorExCase() throws ParseException { loadTestingData(); CacheQuery<?> cacheQuery = createCacheQuery(cache, "blurb", "playing"); ResultIterator<?> found = cacheQuery.iterator(new FetchOptions().fetchMode(FetchOptions.FetchMode.EAGER)); try { assertTrue(found.hasNext()); found.next(); assertFalse(found.hasNext()); found.next(); } finally { found.close(); } }
public void testEagerIterator() throws ParseException { loadTestingData(); CacheQuery<?> cacheQuery = createCacheQuery(cache, "blurb", "playing"); ResultIterator<?> found = cacheQuery.iterator(new FetchOptions().fetchMode(FetchOptions.FetchMode.EAGER)); try { assertTrue(found.hasNext()); found.next(); assertFalse(found.hasNext()); } finally { found.close(); } StaticTestingErrorHandler.assertAllGood(cache); }
public void testLazyIteratorWithOffset() throws ParseException { loadTestingData(); queryParser = createQueryParser("blurb"); Query luceneQuery = queryParser.parse("Eats"); CacheQuery<?> cacheQuery = Search.getSearchManager(cache).<Person>getQuery(luceneQuery).firstResult(1); ResultIterator<?> iterator = cacheQuery.iterator(new FetchOptions().fetchMode(FetchOptions.FetchMode.LAZY)); try { assertEquals(2, countElements(iterator)); } finally { iterator.close(); } StaticTestingErrorHandler.assertAllGood(cache); }
public void testSearchKeyTransformer() throws ParseException { SearchManagerImplementor manager = (SearchManagerImplementor) Search.getSearchManager(cache); manager.registerKeyTransformer(CustomKey3.class, CustomKey3Transformer.class); loadTestingDataWithCustomKey(); queryParser = createQueryParser("blurb"); Query luceneQuery = queryParser.parse("Eats"); CacheQuery<?> cacheQuery = manager.getQuery(luceneQuery); ResultIterator<?> iterator = cacheQuery.iterator(new FetchOptions().fetchMode(FetchOptions.FetchMode.LAZY)); try { assertEquals(3, countElements(iterator)); } finally { iterator.close(); } StaticTestingErrorHandler.assertAllGood(cache); }
@Test public void testQueryReturnsNullWhenProjectingCacheValue() throws Exception { withTx(tm(), (Callable<Void>) () -> { cache.remove("1"); // cache will now be out of sync with the index Query query = createQueryBuilder().keyword().onField("bar").matching("1").createQuery(); ResultIterator<Object[]> iterator = searchManager.getQuery(query).projection(ProjectionConstants.VALUE, "bar").iterator(new FetchOptions().fetchMode(LAZY)); assertTrue(iterator.hasNext()); Object[] projection = iterator.next(); assertNull(projection[0]); assertEquals("1", projection[1]); return null; }); }