This is an automated email from the ASF dual-hosted git repository. zhouxj pushed a commit to branch feature/GEODE-7208 in repository https://gitbox.apache.org/repos/asf/geode.git
commit 0b1d4a334ba24b66a9d1d1f196f4cbe5fbc5098e Author: zhouxh <[email protected]> AuthorDate: Fri Sep 13 15:32:41 2019 -0700 GEODE-7208: FlatFormatSerializer should index on inherited fields --- .../apache/geode/cache/lucene/test/Customer.java | 11 ++-- .../test/{Customer.java => LocalCustomer.java} | 27 ++------- .../test/{Customer.java => SpecialCustomer.java} | 27 ++------- .../NestedObjectSeralizerIntegrationTest.java | 65 +++++++++++++++++---- ...tSerializerOnGrandSubclassIntegrationTest.java} | 29 ++-------- ...ObjectSerializerOnSubclassIntegrationTest.java} | 30 ++-------- ...ctSerializerOnGrandSubclassIntegrationTest.java | 67 ++++++++++++++++++++++ .../geode/cache/lucene/FlatFormatSerializer.java | 18 +++--- .../lucene/FlatFormatSerializerJUnitTest.java | 19 ++++++ 9 files changed, 178 insertions(+), 115 deletions(-) diff --git a/geode-lucene/geode-lucene-test/src/main/java/org/apache/geode/cache/lucene/test/Customer.java b/geode-lucene/geode-lucene-test/src/main/java/org/apache/geode/cache/lucene/test/Customer.java index c405f87..d213c06 100644 --- a/geode-lucene/geode-lucene-test/src/main/java/org/apache/geode/cache/lucene/test/Customer.java +++ b/geode-lucene/geode-lucene-test/src/main/java/org/apache/geode/cache/lucene/test/Customer.java @@ -18,10 +18,10 @@ import java.io.Serializable; import java.util.Collection; public class Customer implements Serializable { - private String name; - private Collection<String> phoneNumbers; - private Collection<Person> contacts; - private Page[] myHomePages; + protected String name; + protected Collection<String> phoneNumbers; + protected Collection<Person> contacts; + protected Page[] myHomePages; public Customer(String name, Collection<String> phoneNumbers, Collection<Person> contacts, Page[] myHomePages) { @@ -37,7 +37,8 @@ public class Customer implements Serializable { @Override public String toString() { - return "Customer[name=" + name + ",phoneNumbers=" + phoneNumbers + ",contacts=" + contacts + return this.getClass() + "[name=" + name + ",phoneNumbers=" + phoneNumbers + ",contacts=" + + contacts + ",homepage=" + myHomePages + "]"; } } diff --git a/geode-lucene/geode-lucene-test/src/main/java/org/apache/geode/cache/lucene/test/Customer.java b/geode-lucene/geode-lucene-test/src/main/java/org/apache/geode/cache/lucene/test/LocalCustomer.java similarity index 56% copy from geode-lucene/geode-lucene-test/src/main/java/org/apache/geode/cache/lucene/test/Customer.java copy to geode-lucene/geode-lucene-test/src/main/java/org/apache/geode/cache/lucene/test/LocalCustomer.java index c405f87..931db49 100644 --- a/geode-lucene/geode-lucene-test/src/main/java/org/apache/geode/cache/lucene/test/Customer.java +++ b/geode-lucene/geode-lucene-test/src/main/java/org/apache/geode/cache/lucene/test/LocalCustomer.java @@ -14,30 +14,13 @@ */ package org.apache.geode.cache.lucene.test; -import java.io.Serializable; import java.util.Collection; -public class Customer implements Serializable { - private String name; - private Collection<String> phoneNumbers; - private Collection<Person> contacts; - private Page[] myHomePages; - - public Customer(String name, Collection<String> phoneNumbers, Collection<Person> contacts, +public class LocalCustomer extends Customer { + public LocalCustomer(String name, + Collection<String> phoneNumbers, + Collection<Person> contacts, Page[] myHomePages) { - this.name = name; - this.phoneNumbers = phoneNumbers; - this.contacts = contacts; - this.myHomePages = myHomePages; - } - - public void addContact(Person contact) { - this.contacts.add(contact); - } - - @Override - public String toString() { - return "Customer[name=" + name + ",phoneNumbers=" + phoneNumbers + ",contacts=" + contacts - + ",homepage=" + myHomePages + "]"; + super(name, phoneNumbers, contacts, myHomePages); } } diff --git a/geode-lucene/geode-lucene-test/src/main/java/org/apache/geode/cache/lucene/test/Customer.java b/geode-lucene/geode-lucene-test/src/main/java/org/apache/geode/cache/lucene/test/SpecialCustomer.java similarity index 56% copy from geode-lucene/geode-lucene-test/src/main/java/org/apache/geode/cache/lucene/test/Customer.java copy to geode-lucene/geode-lucene-test/src/main/java/org/apache/geode/cache/lucene/test/SpecialCustomer.java index c405f87..3740a04 100644 --- a/geode-lucene/geode-lucene-test/src/main/java/org/apache/geode/cache/lucene/test/Customer.java +++ b/geode-lucene/geode-lucene-test/src/main/java/org/apache/geode/cache/lucene/test/SpecialCustomer.java @@ -14,30 +14,13 @@ */ package org.apache.geode.cache.lucene.test; -import java.io.Serializable; import java.util.Collection; -public class Customer implements Serializable { - private String name; - private Collection<String> phoneNumbers; - private Collection<Person> contacts; - private Page[] myHomePages; - - public Customer(String name, Collection<String> phoneNumbers, Collection<Person> contacts, +public class SpecialCustomer extends LocalCustomer { + public SpecialCustomer(String name, + Collection<String> phoneNumbers, + Collection<Person> contacts, Page[] myHomePages) { - this.name = name; - this.phoneNumbers = phoneNumbers; - this.contacts = contacts; - this.myHomePages = myHomePages; - } - - public void addContact(Person contact) { - this.contacts.add(contact); - } - - @Override - public String toString() { - return "Customer[name=" + name + ",phoneNumbers=" + phoneNumbers + ",contacts=" + contacts - + ",homepage=" + myHomePages + "]"; + super(name, phoneNumbers, contacts, myHomePages); } } diff --git a/geode-lucene/src/integrationTest/java/org/apache/geode/cache/lucene/NestedObjectSeralizerIntegrationTest.java b/geode-lucene/src/integrationTest/java/org/apache/geode/cache/lucene/NestedObjectSeralizerIntegrationTest.java index 8696560..dcd6794 100644 --- a/geode-lucene/src/integrationTest/java/org/apache/geode/cache/lucene/NestedObjectSeralizerIntegrationTest.java +++ b/geode-lucene/src/integrationTest/java/org/apache/geode/cache/lucene/NestedObjectSeralizerIntegrationTest.java @@ -17,9 +17,12 @@ package org.apache.geode.cache.lucene; import static org.apache.geode.cache.lucene.test.LuceneTestUtilities.INDEX_NAME; import static org.apache.geode.cache.lucene.test.LuceneTestUtilities.REGION_NAME; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import java.io.Serializable; +import java.lang.reflect.Constructor; import java.util.ArrayList; +import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.concurrent.TimeUnit; @@ -44,12 +47,16 @@ import org.apache.geode.test.junit.categories.LuceneTest; @Category({LuceneTest.class}) public class NestedObjectSeralizerIntegrationTest extends LuceneIntegrationTest { - private static int WAIT_FOR_FLUSH_TIME = 10000; - private static final Logger logger = LogService.getLogger(); + protected static int WAIT_FOR_FLUSH_TIME = 10000; + protected static final Logger logger = LogService.getLogger(); LuceneQuery<Integer, Object> query; PageableLuceneQueryResults<Integer, Object> results; - private Region createRegionAndIndex() { + protected Class useWhichClass() { + return Customer.class; + } + + protected Region createRegionAndIndex() { luceneService.createIndexFactory().setLuceneSerializer(new FlatFormatSerializer()) .addField("name").addField("phoneNumbers").addField("myHomePages.content") .addField("contacts.name").addField("contacts.email", new KeywordAnalyzer()) @@ -61,7 +68,7 @@ public class NestedObjectSeralizerIntegrationTest extends LuceneIntegrationTest return region; } - private Region createRegionAndIndexOnInvalidFields() { + protected Region createRegionAndIndexOnInvalidFields() { luceneService.createIndexFactory().setLuceneSerializer(new FlatFormatSerializer()) .addField("name").addField("contacts").addField("contacts.page") .addField("contacts.missing").addField("missing2").create(INDEX_NAME, REGION_NAME); @@ -70,7 +77,16 @@ public class NestedObjectSeralizerIntegrationTest extends LuceneIntegrationTest return region; } - private void feedSomeNestedObjects(Region region) throws InterruptedException { + protected void feedSomeNestedObjects(Region region) throws InterruptedException { + Class clazz = useWhichClass(); + Constructor<Customer> constructor = null; + try { + constructor = clazz.getConstructor(String.class, Collection.class, Collection.class, + Page[].class); + } catch (NoSuchMethodException e) { + fail("Unexpected NoSuchMethodException"); + } + Person contact1 = new Person("Tommi Jackson", new String[] {"5036330001", "5036330002"}, 1); Person contact2 = new Person("Tommi2 Skywalker", new String[] {"5036330003", "5036330004"}, 2); HashSet<Person> contacts1 = new HashSet(); @@ -80,7 +96,12 @@ public class NestedObjectSeralizerIntegrationTest extends LuceneIntegrationTest ArrayList<String> phoneNumbers = new ArrayList(); phoneNumbers.add("5035330001"); phoneNumbers.add("5035330002"); - Customer customer13 = new Customer("Tommy Jackson", phoneNumbers, contacts1, myHomePages1); + Customer customer13 = null; + try { + customer13 = constructor.newInstance("Tommy Jackson", phoneNumbers, contacts1, myHomePages1); + } catch (Exception e) { + fail("Unexpected exception happened"); + } region.put("object-13", customer13); Person contact3 = new Person("Johnni Jackson", new String[] {"5036330005", "5036330006"}, 3); @@ -92,7 +113,13 @@ public class NestedObjectSeralizerIntegrationTest extends LuceneIntegrationTest phoneNumbers.add("5035330003"); phoneNumbers.add("5035330004"); Page[] myHomePages2 = new Page[] {new Page(14), new Page(141)}; - Customer customer14 = new Customer("Johnny Jackson", phoneNumbers, contacts2, myHomePages2); + + Customer customer14 = null; + try { + customer14 = constructor.newInstance("Johnny Jackson", phoneNumbers, contacts2, myHomePages2); + } catch (Exception e) { + fail("Unexpected exception happened"); + } region.put("object-14", customer14); Person contact5 = new Person("Johnni Jackson2", new String[] {"5036330009", "5036330010"}, 5); @@ -105,7 +132,14 @@ public class NestedObjectSeralizerIntegrationTest extends LuceneIntegrationTest phoneNumbers.add("5035330005"); phoneNumbers.add("5035330006"); Page[] myHomePages3 = new Page[] {new Page(15), new Page(151)}; - Customer customer15 = new Customer("Johnny Jackson2", phoneNumbers, contacts3, myHomePages3); + + Customer customer15 = null; + try { + customer15 = + constructor.newInstance("Johnny Jackson2", phoneNumbers, contacts3, myHomePages3); + } catch (Exception e) { + fail("Unexpected exception happened"); + } region.put("object-15", customer15); Person contact7 = new Person("Johnni Jackson21", new String[] {"5036330013", "5036330014"}, 7); @@ -118,7 +152,14 @@ public class NestedObjectSeralizerIntegrationTest extends LuceneIntegrationTest phoneNumbers.add("5035330007"); phoneNumbers.add("5035330008"); Page[] myHomePages4 = new Page[] {new Page(16), new Page(161)}; - Customer customer16 = new Customer("Johnny Jackson21", phoneNumbers, contacts4, myHomePages4); + + Customer customer16 = null; + try { + customer16 = + constructor.newInstance("Johnny Jackson21", phoneNumbers, contacts4, myHomePages4); + } catch (Exception e) { + fail("Unexpected exception happened"); + } region.put("object-16", customer16); region.put("key-1", "region value 1"); @@ -432,7 +473,7 @@ public class NestedObjectSeralizerIntegrationTest extends LuceneIntegrationTest assertEquals(0, results.size()); } - private void printResults(PageableLuceneQueryResults<Integer, Object> results) { + protected void printResults(PageableLuceneQueryResults<Integer, Object> results) { if (results.size() > 0) { while (results.hasNext()) { results.next().stream().forEach(struct -> { @@ -444,7 +485,7 @@ public class NestedObjectSeralizerIntegrationTest extends LuceneIntegrationTest } } - private Region createRegionAndIndexForPdxObject() { + protected Region createRegionAndIndexForPdxObject() { luceneService.createIndexFactory().setLuceneSerializer(new FlatFormatSerializer()) .addField("ID").addField("description").addField("status").addField("names") .addField("position1.country").addField("position1.secId").addField("positions.secId") @@ -454,7 +495,7 @@ public class NestedObjectSeralizerIntegrationTest extends LuceneIntegrationTest return region; } - private void feedSomePdxObjects(Region region) throws InterruptedException { + protected void feedSomePdxObjects(Region region) throws InterruptedException { SimplePortfolioPdx.resetCounter(); SimplePositionPdx.resetCounter(); for (int i = 1; i < 10; i++) { diff --git a/geode-lucene/geode-lucene-test/src/main/java/org/apache/geode/cache/lucene/test/Customer.java b/geode-lucene/src/integrationTest/java/org/apache/geode/cache/lucene/NestedObjectSerializerOnGrandSubclassIntegrationTest.java similarity index 50% copy from geode-lucene/geode-lucene-test/src/main/java/org/apache/geode/cache/lucene/test/Customer.java copy to geode-lucene/src/integrationTest/java/org/apache/geode/cache/lucene/NestedObjectSerializerOnGrandSubclassIntegrationTest.java index c405f87..36b0228 100644 --- a/geode-lucene/geode-lucene-test/src/main/java/org/apache/geode/cache/lucene/test/Customer.java +++ b/geode-lucene/src/integrationTest/java/org/apache/geode/cache/lucene/NestedObjectSerializerOnGrandSubclassIntegrationTest.java @@ -12,32 +12,15 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ -package org.apache.geode.cache.lucene.test; +package org.apache.geode.cache.lucene; -import java.io.Serializable; -import java.util.Collection; +import org.apache.geode.cache.lucene.test.SpecialCustomer; -public class Customer implements Serializable { - private String name; - private Collection<String> phoneNumbers; - private Collection<Person> contacts; - private Page[] myHomePages; - public Customer(String name, Collection<String> phoneNumbers, Collection<Person> contacts, - Page[] myHomePages) { - this.name = name; - this.phoneNumbers = phoneNumbers; - this.contacts = contacts; - this.myHomePages = myHomePages; +public class NestedObjectSerializerOnGrandSubclassIntegrationTest + extends NestedObjectSeralizerIntegrationTest { + protected Class useWhichClass() { + return SpecialCustomer.class; } - public void addContact(Person contact) { - this.contacts.add(contact); - } - - @Override - public String toString() { - return "Customer[name=" + name + ",phoneNumbers=" + phoneNumbers + ",contacts=" + contacts - + ",homepage=" + myHomePages + "]"; - } } diff --git a/geode-lucene/geode-lucene-test/src/main/java/org/apache/geode/cache/lucene/test/Customer.java b/geode-lucene/src/integrationTest/java/org/apache/geode/cache/lucene/NestedObjectSerializerOnSubclassIntegrationTest.java similarity index 50% copy from geode-lucene/geode-lucene-test/src/main/java/org/apache/geode/cache/lucene/test/Customer.java copy to geode-lucene/src/integrationTest/java/org/apache/geode/cache/lucene/NestedObjectSerializerOnSubclassIntegrationTest.java index c405f87..14f6cf1 100644 --- a/geode-lucene/geode-lucene-test/src/main/java/org/apache/geode/cache/lucene/test/Customer.java +++ b/geode-lucene/src/integrationTest/java/org/apache/geode/cache/lucene/NestedObjectSerializerOnSubclassIntegrationTest.java @@ -12,32 +12,14 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ -package org.apache.geode.cache.lucene.test; +package org.apache.geode.cache.lucene; -import java.io.Serializable; -import java.util.Collection; +import org.apache.geode.cache.lucene.test.LocalCustomer; -public class Customer implements Serializable { - private String name; - private Collection<String> phoneNumbers; - private Collection<Person> contacts; - private Page[] myHomePages; +public class NestedObjectSerializerOnSubclassIntegrationTest + extends NestedObjectSeralizerIntegrationTest { - public Customer(String name, Collection<String> phoneNumbers, Collection<Person> contacts, - Page[] myHomePages) { - this.name = name; - this.phoneNumbers = phoneNumbers; - this.contacts = contacts; - this.myHomePages = myHomePages; - } - - public void addContact(Person contact) { - this.contacts.add(contact); - } - - @Override - public String toString() { - return "Customer[name=" + name + ",phoneNumbers=" + phoneNumbers + ",contacts=" + contacts - + ",homepage=" + myHomePages + "]"; + protected Class useWhichClass() { + return LocalCustomer.class; } } diff --git a/geode-lucene/src/integrationTest/java/org/apache/geode/cache/lucene/ToplevelObjectSerializerOnGrandSubclassIntegrationTest.java b/geode-lucene/src/integrationTest/java/org/apache/geode/cache/lucene/ToplevelObjectSerializerOnGrandSubclassIntegrationTest.java new file mode 100644 index 0000000..6b99d9e --- /dev/null +++ b/geode-lucene/src/integrationTest/java/org/apache/geode/cache/lucene/ToplevelObjectSerializerOnGrandSubclassIntegrationTest.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.geode.cache.lucene; + +import static org.apache.geode.cache.lucene.test.LuceneTestUtilities.INDEX_NAME; +import static org.apache.geode.cache.lucene.test.LuceneTestUtilities.REGION_NAME; + +import org.apache.lucene.analysis.core.KeywordAnalyzer; +import org.junit.Test; + +import org.apache.geode.cache.Region; +import org.apache.geode.cache.RegionShortcut; + +public class ToplevelObjectSerializerOnGrandSubclassIntegrationTest + extends NestedObjectSerializerOnGrandSubclassIntegrationTest { + protected Region createRegionAndIndex() { + luceneService.createIndexFactory() + .addField("name").addField("phoneNumbers").addField("myHomePages.content") + .addField("contacts.name").addField("contacts.email", new KeywordAnalyzer()) + .addField("contacts.phoneNumbers").addField("contacts.address") + .addField("contacts.homepage.content").addField("contacts.homepage.id") + .addField(LuceneService.REGION_VALUE_FIELD).create(INDEX_NAME, REGION_NAME); + + Region region = createRegion(REGION_NAME, RegionShortcut.PARTITION); + return region; + } + + // override tests using nested object or collection + @Test + public void queryOnThreeLayerField() {} + + @Test + public void queryOnTopLevelStringCollectionField_OR_OneExist() {} + + @Test + public void queryOnContactAddressWithStandardAnalyzer() {} + + @Test + public void queryOnContactEmailWithAnalyzer() {} + + @Test + public void queryOnContactNameWithExpression() {} + + @Test + public void queryOnTopLevelObjectCollectionField_AND_BothExist() {} + + @Test + public void queryOnContactNameWithExactMath() {} + + @Test + public void queryOnTopLevelObjectCollectionField_OR_OneExist() {} + + @Test + public void queryOnTopLevelStringCollectionField_AND_BothExist() {} +} diff --git a/geode-lucene/src/main/java/org/apache/geode/cache/lucene/FlatFormatSerializer.java b/geode-lucene/src/main/java/org/apache/geode/cache/lucene/FlatFormatSerializer.java index 62006db..a56b1a0 100644 --- a/geode-lucene/src/main/java/org/apache/geode/cache/lucene/FlatFormatSerializer.java +++ b/geode-lucene/src/main/java/org/apache/geode/cache/lucene/FlatFormatSerializer.java @@ -140,13 +140,17 @@ public class FlatFormatSerializer implements LuceneSerializer { && SerializerUtil.supportedPrimitiveTypes().contains(clazz)) { return value; } - try { - Field field = clazz.getDeclaredField(fieldName); - field.setAccessible(true); - return field.get(value); - } catch (Exception e) { - return null; - } + + do { + try { + Field field = clazz.getDeclaredField(fieldName); + field.setAccessible(true); + return field.get(value); + } catch (Exception e) { + clazz = clazz.getSuperclass(); + } + } while (clazz != null && !clazz.equals(Object.class)); + return null; } } } diff --git a/geode-lucene/src/test/java/org/apache/geode/cache/lucene/FlatFormatSerializerJUnitTest.java b/geode-lucene/src/test/java/org/apache/geode/cache/lucene/FlatFormatSerializerJUnitTest.java index 89e7e16..201f3f0 100644 --- a/geode-lucene/src/test/java/org/apache/geode/cache/lucene/FlatFormatSerializerJUnitTest.java +++ b/geode-lucene/src/test/java/org/apache/geode/cache/lucene/FlatFormatSerializerJUnitTest.java @@ -31,6 +31,7 @@ import org.apache.geode.cache.lucene.internal.repository.serializer.SerializerTe import org.apache.geode.cache.lucene.test.Customer; import org.apache.geode.cache.lucene.test.Page; import org.apache.geode.cache.lucene.test.Person; +import org.apache.geode.cache.lucene.test.SpecialCustomer; import org.apache.geode.test.junit.categories.LuceneTest; @Category({LuceneTest.class}) @@ -87,6 +88,24 @@ public class FlatFormatSerializerJUnitTest { } @Test + public void shouldIndexOnInheritedFields() { + String[] fields = new String[] {"myHomePages.content"}; + + FlatFormatSerializer serializer = new FlatFormatSerializer(); + + Page[] myHomePages1 = new Page[] {new Page(131), new Page(132)}; + SpecialCustomer customer = new SpecialCustomer("Tommy Jackson", null, null, myHomePages1); + Document doc1 = SerializerTestHelper.invokeSerializer(serializer, customer, fields); + + IndexableField[] fieldsInDoc = doc1.getFields("myHomePages.content"); + Collection<Object> results = getResultCollection(fieldsInDoc, false); + assertEquals(2, results.size()); + Object value = results.iterator().next(); + assertTrue(results.contains("Hello world no 131")); + assertTrue(results.contains("Hello world no 132")); + } + + @Test public void shouldQueryOnFieldInCollectionObject() { String[] fields = new String[] {"contacts.name"};
