geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From upthewatersp...@apache.org
Subject [2/2] incubator-geode git commit: Adding a query method to IndexRepository
Date Tue, 01 Sep 2015 16:14:29 GMT
Adding a query method to IndexRepository

This method takes a result collector that receives a gemfire key and
score for each result.


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

Branch: refs/heads/feature/GEODE-11
Commit: e4220113e0939436eeea90acb32ad4c5a77957de
Parents: 6682507
Author: Dan Smith <upthewaterspout@apache.org>
Authored: Mon Aug 31 17:13:15 2015 -0700
Committer: Dan Smith <upthewaterspout@apache.org>
Committed: Mon Aug 31 17:13:15 2015 -0700

----------------------------------------------------------------------
 .../internal/repository/IndexRepository.java    | 15 +++++++++
 .../repository/IndexRepositoryImpl.java         | 35 +++++++++++++++++---
 .../repository/IndexResultCollector.java        | 16 +++++++++
 .../IndexRepositoryImplJUnitTest.java           | 28 ++++++++--------
 4 files changed, 76 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/e4220113/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/repository/IndexRepository.java
----------------------------------------------------------------------
diff --git a/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/repository/IndexRepository.java
b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/repository/IndexRepository.java
index dac206d..549bf21 100644
--- a/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/repository/IndexRepository.java
+++ b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/repository/IndexRepository.java
@@ -2,6 +2,8 @@ package com.gemstone.gemfire.cache.lucene.internal.repository;
 
 import java.io.IOException;
 
+import org.apache.lucene.search.Query;
+
 /**
  * An Repository interface for the writing data to lucene.
  */
@@ -24,6 +26,19 @@ public interface IndexRepository {
    * @throws IOException 
    */
   void delete(Object key) throws IOException;
+  
+  /**
+   * Query the index index repository, passing the results to the collector
+   * Only the documents with the top scores, up to the limit, will be passed
+   * to the collector, in order of score.
+   * 
+   * @param query
+   * @param limit the maximum number of hits to return
+   * @param collector the class to aggregate the hits
+   * 
+   * @throws IOException
+   */
+  public void query(Query query, int limit, IndexResultCollector collector) throws IOException;
 
   /**
    * Commit the changes to all lucene index

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/e4220113/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/repository/IndexRepositoryImpl.java
----------------------------------------------------------------------
diff --git a/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/repository/IndexRepositoryImpl.java
b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/repository/IndexRepositoryImpl.java
index 5fb7ae7..57037d1 100644
--- a/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/repository/IndexRepositoryImpl.java
+++ b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/repository/IndexRepositoryImpl.java
@@ -3,22 +3,34 @@ package com.gemstone.gemfire.cache.lucene.internal.repository;
 import java.io.IOException;
 
 import org.apache.lucene.document.Document;
+import org.apache.lucene.index.DirectoryReader;
 import org.apache.lucene.index.IndexWriter;
 import org.apache.lucene.index.Term;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.ScoreDoc;
+import org.apache.lucene.search.TopDocs;
 
-import com.gemstone.gemfire.cache.lucene.internal.repository.serializer.SerializerUtil;
 import com.gemstone.gemfire.cache.lucene.internal.repository.serializer.LuceneSerializer;
+import com.gemstone.gemfire.cache.lucene.internal.repository.serializer.SerializerUtil;
 
 /**
  * A repository that writes to a single lucene index writer
  */
 public class IndexRepositoryImpl implements IndexRepository {
   
+  private static final boolean APPLY_ALL_DELETES = System
+      .getProperty("gemfire.IndexRepository.APPLY_ALL_DELETES", "true")
+      .equalsIgnoreCase("true");
+  
   private final IndexWriter writer;
   private final LuceneSerializer serializer;
+  private volatile DirectoryReader reader;
+  private volatile IndexSearcher searcher;
   
-  public IndexRepositoryImpl(IndexWriter writer, LuceneSerializer serializer) {
+  public IndexRepositoryImpl(IndexWriter writer, LuceneSerializer serializer) throws IOException
{
     this.writer = writer;
+    reader = DirectoryReader.open(writer, APPLY_ALL_DELETES);
     this.serializer = serializer;
   }
 
@@ -45,8 +57,23 @@ public class IndexRepositoryImpl implements IndexRepository {
   }
 
   @Override
-  public void commit() throws IOException {
-    writer.commit();
+  public void query(Query query, int limit, IndexResultCollector collector) throws IOException
{
+    IndexSearcher searcherSnapshot = searcher;
+    TopDocs docs = searcherSnapshot.search(query, limit);
+    for(ScoreDoc scoreDoc : docs.scoreDocs) {
+      Document doc = searcher.doc(scoreDoc.doc);
+      Object key = SerializerUtil.getKey(doc);
+      collector.collect(key, scoreDoc.score);
+    }
   }
 
+  @Override
+  public synchronized void commit() throws IOException {
+    writer.commit();
+    DirectoryReader newReader = DirectoryReader.openIfChanged(reader);
+    if(newReader != null) {
+      reader = newReader;
+      searcher = new IndexSearcher(reader);
+    }
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/e4220113/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/repository/IndexResultCollector.java
----------------------------------------------------------------------
diff --git a/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/repository/IndexResultCollector.java
b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/repository/IndexResultCollector.java
new file mode 100644
index 0000000..fd78b11
--- /dev/null
+++ b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/repository/IndexResultCollector.java
@@ -0,0 +1,16 @@
+package com.gemstone.gemfire.cache.lucene.internal.repository;
+
+/**
+ * Interface for collection results of a query on 
+ * an IndexRepository. See {@link IndexRepository#query(org.apache.lucene.search.Query, int,
IndexResultCollector)}
+ */
+public interface IndexResultCollector {
+
+  /**
+   * Collect a single document
+   * @param key the gemfire key of the object
+   * @param score the lucene score of this object
+   */
+  void collect(Object key, float score);
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/e4220113/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/repository/IndexRepositoryImplJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/repository/IndexRepositoryImplJUnitTest.java
b/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/repository/IndexRepositoryImplJUnitTest.java
index e47b7bd..f4a994b 100644
--- a/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/repository/IndexRepositoryImplJUnitTest.java
+++ b/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/repository/IndexRepositoryImplJUnitTest.java
@@ -42,7 +42,6 @@ public class IndexRepositoryImplJUnitTest {
 
   private IndexRepositoryImpl repo;
   private HeterogenousLuceneSerializer mapper;
-  private DirectoryReader reader;
   private StandardAnalyzer analyzer = new StandardAnalyzer();
   private IndexWriter writer;
 
@@ -53,7 +52,6 @@ public class IndexRepositoryImplJUnitTest {
     RegionDirectory dir = new RegionDirectory(fileRegion, chunkRegion);
     IndexWriterConfig config = new IndexWriterConfig(analyzer);
     writer = new IndexWriter(dir, config);
-    reader = DirectoryReader.open(writer, true);
     String[] indexedFields= new String[] {"s", "i", "l", "d", "f", "s2", "missing"};
     mapper = new HeterogenousLuceneSerializer(indexedFields);
     repo = new IndexRepositoryImpl(writer, mapper);
@@ -118,22 +116,24 @@ public class IndexRepositoryImplJUnitTest {
 
   private void checkQuery(String queryTerm, String queryField, Object ... expectedKeys)
       throws IOException, ParseException {
+    Set<Object> expectedSet = new HashSet<Object>();
+    expectedSet.addAll(Arrays.asList(expectedKeys));
+    
     QueryParser parser = new QueryParser(queryField, analyzer);
     
-    DirectoryReader result = DirectoryReader.openIfChanged(reader);
-    reader = result == null ? reader : result;
-    IndexSearcher searcher = new IndexSearcher(reader);
-    TopDocs results = searcher.search(parser.parse(queryTerm), 100);
+    KeyCollector collector = new KeyCollector();
+    repo.query(parser.parse(queryTerm), 100, collector);
     
-    Set<Object> expectedSet = new HashSet<Object>();
-    expectedSet.addAll(Arrays.asList(expectedKeys));
-    Set<Object> actualKeys = new HashSet<Object>();
-    for(ScoreDoc scoreDoc: results.scoreDocs) {
-      Document doc = searcher.doc(scoreDoc.doc);
-      assertEquals(1, doc.getFields().size());
-      actualKeys.add(SerializerUtil.getKey(doc));
+    assertEquals(expectedSet, collector.results);
+  }
+  
+  private static class KeyCollector implements IndexResultCollector {
+
+    Set<Object> results = new HashSet<Object>();
+    @Override
+    public void collect(Object key, float score) {
+      results.add(key);
     }
-    assertEquals(expectedSet, actualKeys);
   }
 
   /**


Mime
View raw message