geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ash...@apache.org
Subject [1/3] incubator-geode git commit: GEODE-11: Make StringQueryProvider DataSerializable
Date Mon, 21 Sep 2015 23:46:35 GMT
Repository: incubator-geode
Updated Branches:
  refs/heads/feature/GEODE-11 378176869 -> 74d293d17


GEODE-11: Make StringQueryProvider DataSerializable

StringQueryProvider will be sent over wire and should be serialized efficiently
using DataSerializableFixedId

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


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

Branch: refs/heads/feature/GEODE-11
Commit: 34a856bdbf175f1f6d4c864f661a727f60bc2059
Parents: 3781768
Author: Ashvin Agrawal <ashvin@apache.org>
Authored: Mon Sep 21 15:02:32 2015 -0700
Committer: Ashvin Agrawal <ashvin@apache.org>
Committed: Mon Sep 21 16:45:41 2015 -0700

----------------------------------------------------------------------
 .../internal/DataSerializableFixedID.java       |  2 +
 .../lucene/internal/LuceneServiceImpl.java      | 10 ++-
 .../lucene/internal/StringQueryProvider.java    | 93 +++++++++++++++++---
 .../internal/StringQueryProviderJUnitTest.java  | 23 ++++-
 4 files changed, 112 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/34a856bd/gemfire-core/src/main/java/com/gemstone/gemfire/internal/DataSerializableFixedID.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/DataSerializableFixedID.java
b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/DataSerializableFixedID.java
index 98efb3e..4d1d60a 100644
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/DataSerializableFixedID.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/DataSerializableFixedID.java
@@ -811,6 +811,8 @@ public interface DataSerializableFixedID extends SerializationVersions
{
   
   public static final short LUCENE_CHUNK_KEY = 2169;
   public static final short LUCENE_FILE = 2170;
+  public static final short LUCENE_FUNCTION_CONTEXT = 2171;
+  public static final short LUCENE_STRING_QUERY_PROVIDER = 2172;
   
   // NOTE, codes > 65535 will take 4 bytes to serialize
   

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/34a856bd/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneServiceImpl.java
----------------------------------------------------------------------
diff --git a/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneServiceImpl.java
b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneServiceImpl.java
index cee9dc9..ad569dd 100644
--- a/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneServiceImpl.java
+++ b/gemfire-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneServiceImpl.java
@@ -13,6 +13,7 @@ import com.gemstone.gemfire.cache.lucene.LuceneIndex;
 import com.gemstone.gemfire.cache.lucene.LuceneQueryFactory;
 import com.gemstone.gemfire.cache.lucene.LuceneService;
 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.filesystem.ChunkKey;
 import com.gemstone.gemfire.cache.lucene.internal.filesystem.File;
 import com.gemstone.gemfire.internal.DSFIDFactory;
@@ -141,6 +142,13 @@ public class LuceneServiceImpl implements LuceneService, Extension<Cache>
{
     DSFIDFactory.registerDSFID(
         DataSerializableFixedID.LUCENE_FILE,
         File.class);
+    
+    DSFIDFactory.registerDSFID(
+        DataSerializableFixedID.LUCENE_FUNCTION_CONTEXT,
+        LuceneFunctionContext.class);
+    
+    DSFIDFactory.registerDSFID(
+        DataSerializableFixedID.LUCENE_STRING_QUERY_PROVIDER,
+        StringQueryProvider.class);
   }
-
 }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/34a856bd/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
index e249aea..7055615 100644
--- 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
@@ -1,43 +1,69 @@
 package com.gemstone.gemfire.cache.lucene.internal;
 
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+
 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.DataSerializer;
 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.DataSerializableFixedID;
+import com.gemstone.gemfire.internal.Version;
 import com.gemstone.gemfire.internal.logging.LogService;
 
-public class StringQueryProvider implements LuceneQueryProvider {
+/**
+ * Constructs a Lucene Query object by parsing a search string. The class uses {@link MultiFieldQueryParser}.
It sets
+ * searchable fields in a {@link LuceneIndex} as default fields.
+ */
+public class StringQueryProvider implements LuceneQueryProvider, DataSerializableFixedID
{
   private static final long serialVersionUID = 1L;
 
-  private Query luceneQuery;
   private static final Logger logger = LogService.getLogger();
-  
-  final QueryParser parser;
-  final String query;
+
+  // the following members hold input data and needs to be sent on wire
+  private String query;
+  private String indexName;
+  private String regionPath;
+
+  // the following members hold derived objects and need not be serialized
+  private Query luceneQuery;
+  private LuceneIndex index;
+
+  public StringQueryProvider() {
+    this(null);
+  }
 
   public StringQueryProvider(String query) {
     this(null, query);
   }
 
   public StringQueryProvider(LuceneIndex index, String query) {
-    String[] fields = null;
-    
     this.query = query;
+    this.index = index;
     if (index != null) {
-      fields = index.getFieldNames();
+      this.indexName = index.getName();
+      this.regionPath = index.getRegionPath();
     }
-    parser = new MultiFieldQueryParser(fields, new SimpleAnalyzer());
   }
-  
+
   @Override
   public synchronized Query getQuery() throws QueryException {
     if (luceneQuery == null) {
+      String[] fields = null;
+      if (index != null) {
+        fields = index.getFieldNames();
+      } else {
+        // TODO get index from lucene query service
+      }
+
+      MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new SimpleAnalyzer());
       try {
         luceneQuery = parser.parse(query);
       } catch (ParseException e) {
@@ -47,4 +73,49 @@ public class StringQueryProvider implements LuceneQueryProvider {
     }
     return luceneQuery;
   }
+
+  /**
+   * @return the query string used to construct this query provider
+   */
+  public String getQueryString() {
+    return query;
+  }
+
+  /**
+   * @return name of the index on which this query will be executed
+   */
+  public String getIndexName() {
+    return indexName;
+  }
+
+  /**
+   * @return path of the region on which this query will be executed
+   */
+  public String getRegionPath() {
+    return regionPath;
+  }
+
+  @Override
+  public Version[] getSerializationVersions() {
+    return null;
+  }
+
+  @Override
+  public int getDSFID() {
+    return LUCENE_STRING_QUERY_PROVIDER;
+  }
+
+  @Override
+  public void toData(DataOutput out) throws IOException {
+    DataSerializer.writeString(query, out);
+    DataSerializer.writeString(index.getName(), out);
+    DataSerializer.writeString(index.getRegionPath(), out);
+  }
+
+  @Override
+  public void fromData(DataInput in) throws IOException, ClassNotFoundException {
+    query = DataSerializer.readString(in);
+    this.indexName = DataSerializer.readString(in);
+    this.regionPath = DataSerializer.readString(in);
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/34a856bd/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
index 9d8b88c..1ddc91c 100644
--- 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
@@ -1,5 +1,7 @@
 package com.gemstone.gemfire.cache.lucene.internal;
 
+import static org.junit.Assert.assertEquals;
+
 import org.apache.lucene.search.Query;
 import org.junit.Assert;
 import org.junit.Before;
@@ -7,6 +9,7 @@ import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.mockito.Mockito;
 
+import com.gemstone.gemfire.CopyHelper;
 import com.gemstone.gemfire.cache.lucene.LuceneIndex;
 import com.gemstone.gemfire.cache.query.QueryException;
 import com.gemstone.gemfire.test.junit.categories.UnitTest;
@@ -19,9 +22,11 @@ public class StringQueryProviderJUnitTest {
 
   @Before
   public void initMocksAndCommonObjects() {
-    mockIndex = Mockito.mock(LuceneIndex.class);
+    mockIndex = Mockito.mock(LuceneIndex.class, "mockIndex");
     String[] fields = { "field-1", "field-2" };
     Mockito.doReturn(fields).when(mockIndex).getFieldNames();
+    Mockito.doReturn("mockIndex").when(mockIndex).getName();
+    Mockito.doReturn("mockRegionPath").when(mockIndex).getRegionPath();
   }
 
   @Test
@@ -29,7 +34,7 @@ public class StringQueryProviderJUnitTest {
     StringQueryProvider provider = new StringQueryProvider("foo:bar");
     Query query = provider.getQuery();
     Assert.assertNotNull(query);
-    Assert.assertEquals("foo:bar", query.toString());
+    assertEquals("foo:bar", query.toString());
   }
 
   @Test
@@ -37,7 +42,7 @@ public class StringQueryProviderJUnitTest {
     StringQueryProvider provider = new StringQueryProvider(mockIndex, "findThis");
     Query query = provider.getQuery();
     Assert.assertNotNull(query);
-    Assert.assertEquals("field-1:findthis field-2:findthis", query.toString());
+    assertEquals("field-1:findthis field-2:findthis", query.toString());
   }
 
   @Test
@@ -45,7 +50,7 @@ public class StringQueryProviderJUnitTest {
     StringQueryProvider provider = new StringQueryProvider(mockIndex, "findThis");
     Query query = provider.getQuery();
     Assert.assertNotNull(query);
-    Assert.assertEquals("field-1:findthis field-2:findthis", query.toString());
+    assertEquals("field-1:findthis field-2:findthis", query.toString());
   }
 
   @Test(expected = QueryException.class)
@@ -53,4 +58,14 @@ public class StringQueryProviderJUnitTest {
     StringQueryProvider provider = new StringQueryProvider(mockIndex, "invalid:lucene:query:string");
     provider.getQuery();
   }
+  
+  @Test
+  public void testSerialization() {
+    LuceneServiceImpl.registerDataSerializables();
+    StringQueryProvider provider = new StringQueryProvider(mockIndex, "text:search");
+    StringQueryProvider copy = CopyHelper.deepCopy(provider);
+    assertEquals("text:search", copy.getQueryString());
+    assertEquals(mockIndex.getName(), copy.getIndexName());
+    assertEquals(mockIndex.getRegionPath(), copy.getRegionPath());
+  }
 }


Mime
View raw message