GEODE-11: Added findValues() to LuceneQuery Added findValues() to LuceneQuery and added unit and intergration tests.
Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/59d471c0 Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/59d471c0 Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/59d471c0 Branch: refs/heads/develop Commit: 59d471c051932fb509214024117b8b7c6ccb5d28 Parents: 14437b7 Author: Aparna Dharmakkan <adharmak...@pivotal.io> Authored: Mon Jun 20 11:31:07 2016 -0700 Committer: Dan Smith <upthewatersp...@apache.org> Committed: Fri Jun 24 14:41:42 2016 -0700 ---------------------------------------------------------------------- .../gemfire/cache/lucene/LuceneQuery.java | 5 ++ .../cache/lucene/internal/LuceneQueryImpl.java | 22 +++++++++ .../lucene/LuceneQueriesIntegrationTest.java | 41 ++++++++++------ .../internal/LuceneQueryImplJUnitTest.java | 50 +++++++++++++++++--- 4 files changed, 96 insertions(+), 22 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/59d471c0/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/LuceneQuery.java ---------------------------------------------------------------------- diff --git a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/LuceneQuery.java b/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/LuceneQuery.java index 9915116..255bd79 100644 --- a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/LuceneQuery.java +++ b/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/LuceneQuery.java @@ -34,6 +34,11 @@ public interface LuceneQuery<K, V> { public Collection<K> findKeys() throws LuceneQueryException; /** + * Execute search and return values. + */ + public Collection<V> findValues() throws LuceneQueryException; + + /** * Execute the search and get results. */ public PageableLuceneQueryResults<K, V> findPages() throws LuceneQueryException; http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/59d471c0/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImpl.java ---------------------------------------------------------------------- diff --git a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImpl.java b/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImpl.java index cd49f72..b910f19 100644 --- a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImpl.java +++ b/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImpl.java @@ -21,6 +21,7 @@ package com.gemstone.gemfire.cache.lucene.internal; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -33,6 +34,7 @@ import com.gemstone.gemfire.cache.lucene.LuceneQuery; import com.gemstone.gemfire.cache.lucene.LuceneQueryException; import com.gemstone.gemfire.cache.lucene.LuceneQueryFactory; import com.gemstone.gemfire.cache.lucene.LuceneQueryProvider; +import com.gemstone.gemfire.cache.lucene.LuceneResultStruct; import com.gemstone.gemfire.cache.lucene.PageableLuceneQueryResults; import com.gemstone.gemfire.cache.lucene.internal.distributed.EntryScore; import com.gemstone.gemfire.cache.lucene.internal.distributed.LuceneFunction; @@ -74,12 +76,32 @@ public class LuceneQueryImpl<K, V> implements LuceneQuery<K, V> { } @Override + public Collection<V> findValues() throws LuceneQueryException { + PageableLuceneQueryResults<K, V> pages = findPages(0); + final List<LuceneResultStruct<K, V>> page = pages.getNextPage(); + if(page == null) { + return Collections.emptyList(); + } + + return page.stream() + .map(entry -> entry.getValue()) + .collect(Collectors.toList()); + } + + @Override public PageableLuceneQueryResults<K, V> findPages() throws LuceneQueryException { + return findPages(pageSize); + } + + private PageableLuceneQueryResults<K, V> findPages(int pageSize) throws LuceneQueryException { + TopEntries entries = findTopEntries(); return new PageableLuceneQueryResultsImpl<K, V>(entries.getHits(), region, pageSize); } + + private TopEntries findTopEntries() throws LuceneQueryException { TopEntriesCollectorManager manager = new TopEntriesCollectorManager(null, limit); LuceneFunctionContext<TopEntriesCollector> context = new LuceneFunctionContext<>(query, indexName, manager, limit); http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/59d471c0/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/LuceneQueriesIntegrationTest.java ---------------------------------------------------------------------- diff --git a/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/LuceneQueriesIntegrationTest.java b/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/LuceneQueriesIntegrationTest.java index cf1b5d2..d563f4e 100644 --- a/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/LuceneQueriesIntegrationTest.java +++ b/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/LuceneQueriesIntegrationTest.java @@ -21,6 +21,7 @@ import static org.junit.Assert.*; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -53,6 +54,7 @@ public class LuceneQueriesIntegrationTest extends LuceneIntegrationTest { public ExpectedException thrown = ExpectedException.none(); private static final String INDEX_NAME = "index"; protected static final String REGION_NAME = "index"; + private Region region; @Test() public void shouldNotTokenizeWordsWithKeywordAnalyzer() throws Exception { @@ -117,8 +119,30 @@ public class LuceneQueriesIntegrationTest extends LuceneIntegrationTest { @Test() public void shouldPaginateResults() throws Exception { + final LuceneQuery<Object, Object> query = addValuesAndCreateQuery(); + + final PageableLuceneQueryResults<Object, Object> pages = query.findPages(); + assertTrue(pages.hasNextPage()); + assertEquals(3, pages.size()); + final List<LuceneResultStruct<Object, Object>> page1 = pages.getNextPage(); + final List<LuceneResultStruct<Object, Object>> page2 = pages.getNextPage(); + List<LuceneResultStruct<Object, Object>> allEntries=new ArrayList<>(); + allEntries.addAll(page1); + allEntries.addAll(page2); + + assertEquals(region.keySet(), allEntries.stream().map(entry -> entry.getKey()).collect(Collectors.toSet())); + assertEquals(region.values(), allEntries.stream().map(entry -> entry.getValue()).collect(Collectors.toSet())); + + } + @Test + public void shouldReturnValuesFromFindValues() throws Exception { + final LuceneQuery<Object, Object> query = addValuesAndCreateQuery(); + assertEquals(region.values(), new HashSet(query.findValues())); + } + + private LuceneQuery<Object, Object> addValuesAndCreateQuery() { luceneService.createIndex(INDEX_NAME, REGION_NAME, "field1", "field2"); - Region region = cache.createRegionFactory(RegionShortcut.PARTITION) + region = cache.createRegionFactory(RegionShortcut.PARTITION) .create(REGION_NAME); final LuceneIndex index = luceneService.getIndex(INDEX_NAME, REGION_NAME); @@ -131,23 +155,10 @@ public class LuceneQueriesIntegrationTest extends LuceneIntegrationTest { region.put("C", new TestObject(value3, value3)); index.waitUntilFlushed(60000); - final LuceneQuery<Object, Object> query = luceneService.createLuceneQueryFactory() + return luceneService.createLuceneQueryFactory() .setPageSize(2) .create(INDEX_NAME, REGION_NAME, "one", "field1"); - - final PageableLuceneQueryResults<Object, Object> pages = query.findPages(); - assertTrue(pages.hasNextPage()); - assertEquals(3, pages.size()); - final List<LuceneResultStruct<Object, Object>> page1 = pages.getNextPage(); - final List<LuceneResultStruct<Object, Object>> page2 = pages.getNextPage(); - List<LuceneResultStruct<Object, Object>> allEntries=new ArrayList<>(); - allEntries.addAll(page1); - allEntries.addAll(page2); - - assertEquals(region.keySet(), allEntries.stream().map(entry -> entry.getKey()).collect(Collectors.toSet())); - assertEquals(region.values(), allEntries.stream().map(entry -> entry.getValue()).collect(Collectors.toSet())); - } @Test() http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/59d471c0/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImplJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImplJUnitTest.java b/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImplJUnitTest.java index 1b36e37..7b39e89 100644 --- a/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImplJUnitTest.java +++ b/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImplJUnitTest.java @@ -28,7 +28,9 @@ import static org.mockito.Mockito.*; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.junit.After; import org.junit.Before; @@ -64,23 +66,20 @@ public class LuceneQueryImplJUnitTest { private LuceneQueryImpl<Object, Object> query; private Execution execution; private LuceneQueryProvider provider; + private ResultCollector<TopEntriesCollector, TopEntries> collector; + private Region region; @Before public void createMocks() { - Region region = mock(Region.class); + region = mock(Region.class); execution = mock(Execution.class); - ResultCollector<TopEntriesCollector, TopEntries> collector = mock(ResultCollector.class); + collector = mock(ResultCollector.class); provider = mock(LuceneQueryProvider.class); when(execution.withArgs(any())).thenReturn(execution); when(execution.withCollector(any())).thenReturn(execution); when(execution.execute(anyString())).thenReturn((ResultCollector) collector); - TopEntries entries = new TopEntries(); - entries.addHit(new EntryScore("hi", 5)); - when(collector.getResult()).thenReturn(entries); - - query = new LuceneQueryImpl<Object, Object>("index", region, provider, null, LIMIT, 20) { @Override protected Execution onRegion() { return execution; @@ -88,14 +87,50 @@ public class LuceneQueryImplJUnitTest { }; } + private void addValueToResults() { + TopEntries entries = new TopEntries(); + entries.addHit(new EntryScore("hi", 5)); + when(collector.getResult()).thenReturn(entries); + + Map<String, String> getAllResult = new HashMap<String, String>(); + getAllResult.put("hi", "value"); + when(region.getAll(eq(Collections.singletonList("hi")))) + .thenReturn(getAllResult); + } + @Test public void shouldReturnKeysFromFindKeys() throws LuceneQueryException { + addValueToResults(); Collection<Object> results = query.findKeys(); assertEquals(Collections.singletonList("hi"), results); } @Test + public void shouldReturnEmptyListFromFindKeysWithNoResults() throws LuceneQueryException { + TopEntries entries = new TopEntries(); + when(collector.getResult()).thenReturn(entries); + Collection<Object> results = query.findKeys(); + assertEquals(Collections.emptyList(), results); + } + + @Test + public void shouldReturnValuesFromFindValues() throws LuceneQueryException { + addValueToResults(); + Collection<Object> results = query.findValues(); + assertEquals(Collections.singletonList("value"), results); + } + + @Test + public void shouldReturnEmptyListFromFindValuesWithNoResults() throws LuceneQueryException { + TopEntries entries = new TopEntries(); + when(collector.getResult()).thenReturn(entries); + Collection<Object> results = query.findValues(); + assertEquals(Collections.emptyList(), results); + } + + @Test public void shouldInvokeLuceneFunctionWithCorrectArguments() throws Exception { + addValueToResults(); PageableLuceneQueryResults<Object, Object> results = query.findPages(); verify(execution).execute(eq(LuceneFunction.ID)); @@ -111,6 +146,7 @@ public class LuceneQueryImplJUnitTest { assertEquals(1, page.size()); LuceneResultStruct element = page.iterator().next(); assertEquals("hi", element.getKey()); + assertEquals("value", element.getValue()); assertEquals(5, element.getScore(), 0.01); } }