geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ash...@apache.org
Subject [1/2] incubator-geode git commit: GEODE-11: Add LuceneQueryProvider
Date Fri, 18 Sep 2015 23:38:16 GMT
Repository: incubator-geode
Updated Branches:
  refs/heads/feature/GEODE-11 37bbebb92 -> ac7c9286d


GEODE-11: Add LuceneQueryProvider

Lucene query distribution depends on ability to serialize and distribute Lucene
Query object. For advanced query objects, user can provide a query provider
which is expected to take care of query serialization. Add a default
implementation based on valid lucene query string for the provider.

https://reviews.apache.org/r/38513/


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

Branch: refs/heads/feature/GEODE-11
Commit: ce1c6b247ea83797d8df1e62f494a345dbdcd543
Parents: 37bbebb
Author: Ashvin Agrawal <ashvin@apache.org>
Authored: Tue Sep 15 15:16:53 2015 -0700
Committer: Ashvin Agrawal <ashvin@apache.org>
Committed: Fri Sep 18 16:37:30 2015 -0700

----------------------------------------------------------------------
 .../cache/lucene/LuceneQueryFactory.java        | 17 +++---
 .../cache/lucene/LuceneQueryProvider.java       | 23 ++++++++
 .../lucene/internal/LuceneQueryFactoryImpl.java | 14 ++---
 .../lucene/internal/StringQueryProvider.java    | 50 +++++++++++++++++
 .../internal/StringQueryProviderJUnitTest.java  | 56 ++++++++++++++++++++
 5 files changed, 144 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/ce1c6b24/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/LuceneQueryFactory.java
----------------------------------------------------------------------
diff --git a/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/LuceneQueryFactory.java
b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/LuceneQueryFactory.java
index 5636ea4..4bb8180 100644
--- a/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/LuceneQueryFactory.java
+++ b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/LuceneQueryFactory.java
@@ -2,10 +2,6 @@ package com.gemstone.gemfire.cache.lucene;
 
 import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.queryparser.classic.ParseException;
-import org.apache.lucene.search.Query;
-
-import com.gemstone.gemfire.cache.Cache;
-import com.gemstone.gemfire.cache.DiskStore;
 
 /**
  * Factory for creating instances of {@link LuceneQuery}.
@@ -107,15 +103,16 @@ public interface LuceneQueryFactory {
    */
   public LuceneQuery create(String indexName, String regionName, String queryString) 
       throws ParseException;
-  
+
   /**
-   * Create wrapper object for lucene's Query object.
-   * Advanced lucene users can customized their own Query object and directly use in this
API.  
+   * Creates a wrapper object for Lucene's Query object. This {@link LuceneQuery} builder
method could be used in
+   * advanced cases, such as cases where Lucene's Query object construction needs Lucene's
API over query string. The
+   * {@link QueryDeserializer} will be used to re-construct the Lucene Query object on remote
hosts.
    * 
-   * @param regionName region name
    * @param indexName index name
-   * @param query lucene Query object
+   * @param regionName region name
+   * @param provider constructs and provides a Lucene Query object
    * @return LuceneQuery object
    */
-  public LuceneQuery create(String indexName, String regionName, Query query);
+  public LuceneQuery create(String indexName, String regionName, LuceneQueryProvider provider);
 }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/ce1c6b24/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/LuceneQueryProvider.java
----------------------------------------------------------------------
diff --git a/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/LuceneQueryProvider.java
b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/LuceneQueryProvider.java
new file mode 100644
index 0000000..767bfa8
--- /dev/null
+++ b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/LuceneQueryProvider.java
@@ -0,0 +1,23 @@
+package com.gemstone.gemfire.cache.lucene;
+
+import java.io.Serializable;
+
+import org.apache.lucene.search.Query;
+
+import com.gemstone.gemfire.cache.query.QueryException;
+
+/**
+ * The instances of this class will be used for distributing Lucene Query objects and re-constructing
the Query object.
+ * If necessary the implementation needs to take care of serializing and de-serializing Lucene
Query object. GemFire
+ * respects the DataSerializable contract to provide optimal object serialization. For instance,
+ * {@link LuceneQueryProvider}'s toData method will be used to serialize it when it is sent
to another member of the
+ * distributed system. Implementation of DataSerializable can provide a zero-argument constructor
that will be invoked
+ * when they are read with DataSerializer.readObject.
+ */
+public interface LuceneQueryProvider extends Serializable {
+  /**
+   * @return A Lucene Query object which could be used for executing Lucene Search on indexed
data
+   * @throws QueryException if the provider fails to construct the query object
+   */
+  public Query getQuery() throws QueryException;
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/ce1c6b24/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 3bd5b0c..8bb18e8 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
@@ -12,6 +12,7 @@ import org.apache.lucene.search.Query;
 import com.gemstone.gemfire.cache.lucene.LuceneIndex;
 import com.gemstone.gemfire.cache.lucene.LuceneQuery;
 import com.gemstone.gemfire.cache.lucene.LuceneQueryFactory;
+import com.gemstone.gemfire.cache.lucene.LuceneQueryProvider;
 
 public class LuceneQueryFactoryImpl implements LuceneQueryFactory {
   private int limit_attr = DEFAULT_LIMIT;
@@ -63,13 +64,14 @@ public class LuceneQueryFactoryImpl implements LuceneQueryFactory {
     return create(indexName, regionName, queryString, analyzer);
   }
   
-  @Override
-  public LuceneQuery create(String indexName, String regionName,
-      Query query) {
-    LuceneQueryImpl luceneQuery = new LuceneQueryImpl(indexName, regionName, limit_attr,
pageSize_attr, 
-        resultType_attr, projection_fields_attr, query);
-    return luceneQuery;
+  public LuceneQuery create(String indexName, String regionName, LuceneQueryProvider provider)
{
+//    LuceneQueryImpl luceneQuery = new LuceneQueryImpl(indexName, regionName, limit_attr,
pageSize_attr, 
+//        resultType_attr, projection_fields_attr, query);
+//    return luceneQuery;
+    // TODO Auto-generated method stub
+    return null;
   }
+  
 
   public LuceneIndex getRelatedIndex() {
     return this.relatedIndex;

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/ce1c6b24/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/StringQueryProvider.java
----------------------------------------------------------------------
diff --git a/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/StringQueryProvider.java
b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/StringQueryProvider.java
new file mode 100644
index 0000000..e249aea
--- /dev/null
+++ b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/StringQueryProvider.java
@@ -0,0 +1,50 @@
+package com.gemstone.gemfire.cache.lucene.internal;
+
+import org.apache.logging.log4j.Logger;
+import org.apache.lucene.analysis.core.SimpleAnalyzer;
+import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
+import org.apache.lucene.queryparser.classic.ParseException;
+import org.apache.lucene.queryparser.classic.QueryParser;
+import org.apache.lucene.search.Query;
+
+import com.gemstone.gemfire.cache.lucene.LuceneIndex;
+import com.gemstone.gemfire.cache.lucene.LuceneQueryProvider;
+import com.gemstone.gemfire.cache.query.QueryException;
+import com.gemstone.gemfire.internal.logging.LogService;
+
+public class StringQueryProvider implements LuceneQueryProvider {
+  private static final long serialVersionUID = 1L;
+
+  private Query luceneQuery;
+  private static final Logger logger = LogService.getLogger();
+  
+  final QueryParser parser;
+  final String query;
+
+  public StringQueryProvider(String query) {
+    this(null, query);
+  }
+
+  public StringQueryProvider(LuceneIndex index, String query) {
+    String[] fields = null;
+    
+    this.query = query;
+    if (index != null) {
+      fields = index.getFieldNames();
+    }
+    parser = new MultiFieldQueryParser(fields, new SimpleAnalyzer());
+  }
+  
+  @Override
+  public synchronized Query getQuery() throws QueryException {
+    if (luceneQuery == null) {
+      try {
+        luceneQuery = parser.parse(query);
+      } catch (ParseException e) {
+        logger.debug("Malformed lucene query: " + query, e);
+        throw new QueryException(e);
+      }
+    }
+    return luceneQuery;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/ce1c6b24/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/StringQueryProviderJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/StringQueryProviderJUnitTest.java
b/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/StringQueryProviderJUnitTest.java
new file mode 100644
index 0000000..9d8b88c
--- /dev/null
+++ b/gemfire-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/internal/StringQueryProviderJUnitTest.java
@@ -0,0 +1,56 @@
+package com.gemstone.gemfire.cache.lucene.internal;
+
+import org.apache.lucene.search.Query;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.mockito.Mockito;
+
+import com.gemstone.gemfire.cache.lucene.LuceneIndex;
+import com.gemstone.gemfire.cache.query.QueryException;
+import com.gemstone.gemfire.test.junit.categories.UnitTest;
+
+@Category(UnitTest.class)
+public class StringQueryProviderJUnitTest {
+  static final String INDEXED_REGION = "indexedRegion";
+
+  LuceneIndex mockIndex;
+
+  @Before
+  public void initMocksAndCommonObjects() {
+    mockIndex = Mockito.mock(LuceneIndex.class);
+    String[] fields = { "field-1", "field-2" };
+    Mockito.doReturn(fields).when(mockIndex).getFieldNames();
+  }
+
+  @Test
+  public void testQueryConstruction() throws QueryException {
+    StringQueryProvider provider = new StringQueryProvider("foo:bar");
+    Query query = provider.getQuery();
+    Assert.assertNotNull(query);
+    Assert.assertEquals("foo:bar", query.toString());
+  }
+
+  @Test
+  public void usesSearchableFieldsAsDefaults() throws QueryException {
+    StringQueryProvider provider = new StringQueryProvider(mockIndex, "findThis");
+    Query query = provider.getQuery();
+    Assert.assertNotNull(query);
+    Assert.assertEquals("field-1:findthis field-2:findthis", query.toString());
+  }
+
+  @Test
+  public void usesCustomAnalyzer() throws QueryException {
+    StringQueryProvider provider = new StringQueryProvider(mockIndex, "findThis");
+    Query query = provider.getQuery();
+    Assert.assertNotNull(query);
+    Assert.assertEquals("field-1:findthis field-2:findthis", query.toString());
+  }
+
+  @Test(expected = QueryException.class)
+  public void errorsOnMalformedQueryString() throws QueryException {
+    StringQueryProvider provider = new StringQueryProvider(mockIndex, "invalid:lucene:query:string");
+    provider.getQuery();
+  }
+}


Mime
View raw message