geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ash...@apache.org
Subject incubator-geode git commit: GEODE-11: Pass limit to Collectors while searching
Date Sat, 17 Oct 2015 22:28:46 GMT
Repository: incubator-geode
Updated Branches:
  refs/heads/feature/GEODE-11 28a0eb817 -> b59c57d55


GEODE-11: Pass limit to Collectors while searching

User provides result limit while creating query. Query needs to relay the limit
information to CollectorManager and ResultCollector before executing search so
that the result list is trimmed.


Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/b59c57d5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/b59c57d5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/b59c57d5

Branch: refs/heads/feature/GEODE-11
Commit: b59c57d553d61689711c8e0e8532bede3a1a4322
Parents: 28a0eb8
Author: Ashvin Agrawal <ashvin@apache.org>
Authored: Sat Oct 17 15:24:59 2015 -0700
Committer: Ashvin Agrawal <ashvin@apache.org>
Committed: Sat Oct 17 15:24:59 2015 -0700

----------------------------------------------------------------------
 .../lucene/internal/LuceneQueryFactoryImpl.java | 10 +--
 .../cache/lucene/internal/LuceneQueryImpl.java  |  7 +--
 .../TopEntriesFunctionCollector.java            |  2 +-
 .../internal/LuceneQueryImplJUnitTest.java      | 66 +++++++++++++-------
 .../distributed/LuceneFunctionJUnitTest.java    |  9 +--
 5 files changed, 51 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b59c57d5/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryFactoryImpl.java
----------------------------------------------------------------------
diff --git a/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryFactoryImpl.java
b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryFactoryImpl.java
index 2a602a5..b377949 100644
--- a/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryFactoryImpl.java
+++ b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryFactoryImpl.java
@@ -1,10 +1,5 @@
 package com.gemstone.gemfire.cache.lucene.internal;
 
-import java.util.HashSet;
-import java.util.Set;
-
-import org.apache.lucene.queryparser.classic.ParseException;
-
 import com.gemstone.gemfire.cache.Cache;
 import com.gemstone.gemfire.cache.Region;
 import com.gemstone.gemfire.cache.lucene.LuceneQuery;
@@ -34,13 +29,12 @@ public class LuceneQueryFactoryImpl implements LuceneQueryFactory {
   }
 
   @Override
-  public <K, V> LuceneQuery<K, V> create(String indexName, String regionName,
-      String queryString) {
+  public <K, V> LuceneQuery<K, V> create(String indexName, String regionName,
String queryString) {
     return create(indexName, regionName, new StringQueryProvider(queryString));
   }
   
   public <K, V> LuceneQuery<K, V> create(String indexName, String regionName,
LuceneQueryProvider provider) {
-    Region region = cache.getRegion(regionName);
+    Region<K, V> region = cache.getRegion(regionName);
     LuceneQueryImpl<K, V> luceneQuery = new LuceneQueryImpl<K, V>(indexName,
region, provider, projectionFields, limit, pageSize);
     return luceneQuery;
   }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b59c57d5/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImpl.java
----------------------------------------------------------------------
diff --git a/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImpl.java
b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImpl.java
index c3e367b..222acdc 100644
--- a/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImpl.java
+++ b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImpl.java
@@ -36,9 +36,9 @@ public class LuceneQueryImpl<K, V> implements LuceneQuery<K, V>
{
 
   @Override
   public LuceneQueryResults<K, V> search() {
-    LuceneFunctionContext<TopEntriesCollector> context = new LuceneFunctionContext<>(query,
indexName,
-        new TopEntriesCollectorManager());
-    TopEntriesFunctionCollector collector = new TopEntriesFunctionCollector();
+    TopEntriesCollectorManager manager = new TopEntriesCollectorManager(null, limit);
+    LuceneFunctionContext<TopEntriesCollector> context = new LuceneFunctionContext<>(query,
indexName, manager, limit);
+    TopEntriesFunctionCollector collector = new TopEntriesFunctionCollector(context);
 
     ResultCollector<TopEntriesCollector, TopEntries> rc = (ResultCollector<TopEntriesCollector,
TopEntries>) FunctionService.onRegion(region)
         .withArgs(context)
@@ -65,5 +65,4 @@ public class LuceneQueryImpl<K, V> implements LuceneQuery<K, V>
{
   public String[] getProjectedFieldNames() {
     return this.projectedFieldNames;
   }
-  
 }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b59c57d5/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/distributed/TopEntriesFunctionCollector.java
----------------------------------------------------------------------
diff --git a/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/distributed/TopEntriesFunctionCollector.java
b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/distributed/TopEntriesFunctionCollector.java
index 032e136..96ec296 100644
--- a/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/distributed/TopEntriesFunctionCollector.java
+++ b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/distributed/TopEntriesFunctionCollector.java
@@ -43,7 +43,7 @@ public class TopEntriesFunctionCollector implements ResultCollector<TopEntriesCo
   private TopEntriesCollector mergedResults;
 
   public TopEntriesFunctionCollector() {
-    this(null, null);
+    this(null);
   }
 
   public TopEntriesFunctionCollector(LuceneFunctionContext<TopEntriesCollector> context)
{

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b59c57d5/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImplJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImplJUnitTest.java
b/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImplJUnitTest.java
index d3ffd19..e26ab00 100644
--- a/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImplJUnitTest.java
+++ b/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImplJUnitTest.java
@@ -1,6 +1,7 @@
 package com.gemstone.gemfire.cache.lucene.internal;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 import java.util.List;
 
@@ -17,62 +18,81 @@ import com.gemstone.gemfire.cache.execute.FunctionAdapter;
 import com.gemstone.gemfire.cache.execute.FunctionContext;
 import com.gemstone.gemfire.cache.execute.FunctionService;
 import com.gemstone.gemfire.cache.lucene.LuceneQueryResults;
+import com.gemstone.gemfire.cache.lucene.LuceneResultStruct;
 import com.gemstone.gemfire.cache.lucene.internal.distributed.LuceneFunction;
 import com.gemstone.gemfire.cache.lucene.internal.distributed.LuceneFunctionContext;
 import com.gemstone.gemfire.cache.lucene.internal.distributed.TopEntriesCollector;
+import com.gemstone.gemfire.cache.lucene.internal.distributed.TopEntriesCollectorManager;
+import com.gemstone.gemfire.cache.lucene.internal.repository.IndexResultCollector;
 import com.gemstone.gemfire.test.junit.categories.IntegrationTest;
 
 @Category(IntegrationTest.class)
 public class LuceneQueryImplJUnitTest {
-
+  private static int LIMIT = 123;
   private Cache cache;
   private Region<Object, Object> region;
+
   @Before
   public void createCache() {
     cache = new CacheFactory().set("mcast-port", "0").create();
     region = cache.createRegionFactory(RegionShortcut.REPLICATE).create("region");
   }
-  
+
   @After
   public void removeCache() {
     FunctionService.unregisterFunction(LuceneFunction.ID);
     cache.close();
   }
+
   @Test
   public void test() {
-    //Register a fake function to observe the function invocation
+    // Register a fake function to observe the function invocation
     FunctionService.unregisterFunction(LuceneFunction.ID);
     TestLuceneFunction function = new TestLuceneFunction();
     FunctionService.registerFunction(function);
-    
-    
+
     StringQueryProvider provider = new StringQueryProvider();
-    LuceneQueryImpl query = new LuceneQueryImpl("index", region, provider, null, 100, 20);
-    LuceneQueryResults results = query.search();
-    List nextPage = results.getNextPage();
-    assertEquals(3, nextPage.size());
-    assertEquals(.3f, results.getMaxScore(), 0.01);
+    LuceneQueryImpl<Object, Object> query = new LuceneQueryImpl<>("index", region,
provider, null, LIMIT, 20);
+    LuceneQueryResults<Object, Object> results = query.search();
+
     assertTrue(function.wasInvoked);
-    
-    LuceneFunctionContext args = (LuceneFunctionContext) function.args;
-    assertEquals(provider.getQueryString(), ((StringQueryProvider) args.getQueryProvider()).getQueryString());
-    assertEquals("index", args.getIndexName());
-    assertEquals(100, args.getLimit());
+    assertEquals(2f * LIMIT, results.getMaxScore(), 0.01);
+    int resultCount = 0;
+    while (results.hasNextPage()) {
+      List<LuceneResultStruct<Object, Object>> nextPage = results.getNextPage();
+      resultCount += nextPage.size();
+      if (results.hasNextPage()) {
+        assertEquals(20, nextPage.size());
+      }
+    }
+    assertEquals(LIMIT, resultCount);
+
+    LuceneFunctionContext<? extends IndexResultCollector> funcArgs = function.args;
+    assertEquals(provider.getQueryString(), ((StringQueryProvider) funcArgs.getQueryProvider()).getQueryString());
+    assertEquals("index", funcArgs.getIndexName());
+    assertEquals(LIMIT, funcArgs.getLimit());
   }
 
   private static class TestLuceneFunction extends FunctionAdapter {
-
+    private static final long serialVersionUID = 1L;
     private boolean wasInvoked;
-    private Object args;
+    private LuceneFunctionContext<? extends IndexResultCollector> args;
 
     @Override
     public void execute(FunctionContext context) {
-      this.args = context.getArguments();
+      this.args = (LuceneFunctionContext<?>) context.getArguments();
+      TopEntriesCollectorManager manager = (TopEntriesCollectorManager) args.getCollectorManager();
+
+      assertEquals(LIMIT, manager.getLimit());
+
       wasInvoked = true;
-      TopEntriesCollector lastResult = new TopEntriesCollector();
-      lastResult.collect(3, .3f);
-      lastResult.collect(2, .2f);
-      lastResult.collect(1, .1f);
+      TopEntriesCollector lastResult = new TopEntriesCollector(null, 2 * LIMIT);
+      // put more than LIMIT entries. The resultCollector should trim the results
+      for (int i = LIMIT * 2; i >= 0; i--) {
+        lastResult.collect(i, i * 1f);
+      }
+      assertEquals(LIMIT * 2, lastResult.getEntries().getHits().size());
+
       context.getResultSender().lastResult(lastResult);
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b59c57d5/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/distributed/LuceneFunctionJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/distributed/LuceneFunctionJUnitTest.java
b/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/distributed/LuceneFunctionJUnitTest.java
index 431ed4c..419aa26 100644
--- a/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/distributed/LuceneFunctionJUnitTest.java
+++ b/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/distributed/LuceneFunctionJUnitTest.java
@@ -1,6 +1,6 @@
 package com.gemstone.gemfire.cache.lucene.internal.distributed;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
 
 import java.io.IOException;
 import java.util.ArrayList;
@@ -21,11 +21,9 @@ import org.junit.Test;
 import org.junit.experimental.categories.Category;
 
 import com.gemstone.gemfire.cache.Region;
-import com.gemstone.gemfire.cache.execute.RegionFunctionContext;
 import com.gemstone.gemfire.cache.execute.ResultSender;
 import com.gemstone.gemfire.cache.lucene.LuceneQueryFactory;
 import com.gemstone.gemfire.cache.lucene.LuceneQueryProvider;
-import com.gemstone.gemfire.cache.lucene.LuceneService;
 import com.gemstone.gemfire.cache.lucene.internal.InternalLuceneIndex;
 import com.gemstone.gemfire.cache.lucene.internal.InternalLuceneService;
 import com.gemstone.gemfire.cache.lucene.internal.StringQueryProvider;
@@ -36,7 +34,6 @@ import com.gemstone.gemfire.cache.query.QueryException;
 import com.gemstone.gemfire.internal.cache.BucketNotFoundException;
 import com.gemstone.gemfire.internal.cache.InternalCache;
 import com.gemstone.gemfire.internal.cache.execute.InternalRegionFunctionContext;
-import com.gemstone.gemfire.internal.cache.extension.ExtensionPoint;
 import com.gemstone.gemfire.test.junit.categories.UnitTest;
 
 @Category(UnitTest.class)
@@ -63,7 +60,7 @@ public class LuceneFunctionJUnitTest {
   InternalLuceneIndex mockIndex;
 
   ArrayList<IndexRepository> repos;
-  LuceneFunctionContext searchArgs;
+  LuceneFunctionContext<IndexResultCollector> searchArgs;
   LuceneQueryProvider queryProvider;
   Query query;
 
@@ -381,7 +378,6 @@ public class LuceneFunctionJUnitTest {
     
     searchArgs = new LuceneFunctionContext<IndexResultCollector>(queryProvider, "indexName");
     
-    final ExtensionPoint mockExtensionPoint = mocker.mock(ExtensionPoint.class);
     mocker.checking(new Expectations() {{
       allowing(mockRegion).getCache();
       will(returnValue(mockCache));
@@ -395,7 +391,6 @@ public class LuceneFunctionJUnitTest {
       will(returnValue(mockRepoManager));
       allowing(mockIndex).getFieldNames();
       will(returnValue(new String[] {"gemfire"}));
-      
     }});
     
     query = queryProvider.getQuery(mockIndex);


Mime
View raw message