usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From toddn...@apache.org
Subject [08/33] incubator-usergrid git commit: Fixed Entity document -> field parsing bugs
Date Mon, 13 Apr 2015 19:18:15 GMT
Fixed Entity document -> field parsing bugs

Added more tests on parsing conversion.


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

Branch: refs/heads/two-dot-o-dev
Commit: 0fa30b9e1c13cb5322d8066010ac053e8c1ff9f8
Parents: 1e3ea74
Author: Todd Nine <tnine@apigee.com>
Authored: Fri Apr 3 13:39:14 2015 -0600
Committer: Todd Nine <tnine@apigee.com>
Committed: Fri Apr 3 13:39:14 2015 -0600

----------------------------------------------------------------------
 .../model/entity/EntityToMapConverter.java      | 164 +++++----
 .../index/impl/EntityMappingParser.java         |  62 ++--
 .../index/impl/EntityToMapConverterTest.java    | 343 ++++++++++++++++++-
 3 files changed, 440 insertions(+), 129 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/0fa30b9e/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/EntityToMapConverter.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/EntityToMapConverter.java
b/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/EntityToMapConverter.java
index 3865e62..16915e2 100644
--- a/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/EntityToMapConverter.java
+++ b/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/EntityToMapConverter.java
@@ -18,119 +18,143 @@
  */
 package org.apache.usergrid.persistence.model.entity;
 
-import com.fasterxml.jackson.databind.ObjectMapper;
-import org.apache.usergrid.persistence.model.field.*;
-import org.apache.usergrid.persistence.model.field.value.EntityObject;
 
 import java.io.IOException;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Stack;
+
+import org.apache.usergrid.persistence.model.field.AbstractField;
+import org.apache.usergrid.persistence.model.field.ArrayField;
+import org.apache.usergrid.persistence.model.field.ByteArrayField;
+import org.apache.usergrid.persistence.model.field.EntityObjectField;
+import org.apache.usergrid.persistence.model.field.Field;
+import org.apache.usergrid.persistence.model.field.ListField;
+import org.apache.usergrid.persistence.model.field.LocationField;
+import org.apache.usergrid.persistence.model.field.SetField;
+import org.apache.usergrid.persistence.model.field.value.EntityObject;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
 
 /**
  * abstract conversion to Map<String,Object> form EntityObject
  */
-public class EntityToMapConverter{
+public class EntityToMapConverter {
     public static final String LAT = "lat";
     public static final String LON = "lon";
 
-    public static ObjectMapper objectMapper = new ObjectMapper(  );
+    public static ObjectMapper objectMapper = new ObjectMapper();
+
+    private final Stack<String> fieldName = new Stack();
+
     /**
-     * Convert Entity to Map, adding version_ug_field and a {name}_ug_analyzed field for
each
-     * StringField.
+     * Convert Entity to Map, adding version_ug_field and a {name}_ug_analyzed field for
each StringField.
      */
 
-    public EntityMap toMap(EntityObject entityObject) {
+    public EntityMap toMap( EntityObject entityObject ) {
         EntityMap map = null;
-        if(entityObject instanceof Entity){
-            Entity entity = (Entity)entityObject;
-            map =  new EntityMap(entity.getId(),entity.getVersion());
-        }else{
+        if ( entityObject instanceof Entity ) {
+            Entity entity = ( Entity ) entityObject;
+            map = new EntityMap( entity.getId(), entity.getVersion() );
+        }
+        else {
             map = new EntityMap();
         }
-        return toMap(entityObject,map);
+        return toMap( entityObject, map );
     }
 
 
-    public EntityMap toMap(EntityObject entity,EntityMap entityMap) {
-
-        for (Object f : entity.getFields().toArray()) {
-            Field field = (Field) f;
+    public EntityMap toMap( EntityObject entity, EntityMap entityMap ) {
 
-            if (f instanceof ListField || f instanceof ArrayField) {
-                List list = (List) field.getValue();
-                entityMap.put(field.getName(),
-                        new ArrayList( processCollectionForMap(list)));
+        for ( Field field : entity.getFields() ) {
 
-            } else if (f instanceof SetField) {
-                Set set = (Set) field.getValue();
-                entityMap.put(field.getName(),
-                        new ArrayList( processCollectionForMap(set)));
-
-            } else if (f instanceof EntityObjectField) {
-                EntityObject eo = (EntityObject) field.getValue();
-                entityMap.put( field.getName(), toMap(eo)); // recursion
-
-            } else if (f instanceof StringField) {
-                entityMap.put(field.getName(), ((String) field.getValue()));
-
-            } else if (f instanceof LocationField) {
-                LocationField locField = (LocationField) f;
+            if ( field instanceof ListField || field instanceof ArrayField  || field instanceof
SetField) {
+                Collection list = ( Collection ) field.getValue();
+                entityMap.put( field.getName(), processCollection( list )  );
+            }
+            else if ( field instanceof EntityObjectField ) {
+                EntityObject eo = ( EntityObject ) field.getValue();
+                entityMap.put( field.getName(), toMap( eo ) ); // recursion
+            }
+            else if ( field instanceof LocationField ) {
+                LocationField locField = ( LocationField ) field;
                 Map<String, Object> locMap = new HashMap<String, Object>();
 
                 // field names lat and lon trigger ElasticSearch geo location
-                locMap.put( LAT, locField.getValue().getLatitude());
-                locMap.put( LON, locField.getValue().getLongitude());
-                entityMap.put( field.getName(), locMap);
-
-            } else if (f instanceof ByteArrayField) {
-                ByteArrayField bf = ( ByteArrayField ) f;
+                locMap.put( LAT, locField.getValue().getLatitude() );
+                locMap.put( LON, locField.getValue().getLongitude() );
+                entityMap.put( field.getName(), locMap );
+            }
+            else if ( field instanceof ByteArrayField ) {
+                ByteArrayField bf = ( ByteArrayField ) field;
 
-                byte[] serilizedObj =  bf.getValue();
+                byte[] serilizedObj = bf.getValue();
                 Object o;
                 try {
                     o = objectMapper.readValue( serilizedObj, bf.getClassinfo() );
                 }
                 catch ( IOException e ) {
-                    throw new RuntimeException( "Can't deserialize object ",e );
+                    throw new RuntimeException( "Can't deserialize object ", e );
                 }
                 entityMap.put( bf.getName(), o );
             }
             else {
-                entityMap.put( field.getName(), field.getValue());
+                entityMap.put( field.getName(), field.getValue() );
             }
         }
 
         return entityMap;
     }
-    private Collection processCollectionForMap(Collection c) {
-        if (c.isEmpty()) {
-            return c;
-        }
-        List processed = new ArrayList();
-        Object sample = c.iterator().next();
 
-        if (sample instanceof Entity) {
-            for (Object o : c.toArray()) {
-                Entity e = (Entity) o;
-                processed.add(toMap(e));
-            }
 
-        } else if (sample instanceof List) {
-            for (Object o : c.toArray()) {
-                List list = (List) o;
-                processed.add(processCollectionForMap(list)); // recursion;
-            }
+    /**
+     * Process the collection for our map
+     * @param c
+     * @return
+     */
+    private List<?> processCollection( Collection c ) {
+        if ( c.isEmpty() ) {
+            return Collections.emptyList();
+        }
 
-        } else if (sample instanceof Set) {
-            for (Object o : c.toArray()) {
-                Set set = (Set) o;
-                processed.add(processCollectionForMap(set)); // recursion;
-            }
+        List processed = new ArrayList(c.size());
 
-        } else {
-            for (Object o : c.toArray()) {
-                processed.add(o);
-            }
+
+        for(final Object element: c){
+            processed.add( processCollectionElement( element ) );
         }
+
         return processed;
     }
+
+
+    /**
+     * Process each instance of data in our collection
+     * @param element
+     * @return
+     */
+    private Object processCollectionElement( final Object element ) {
+        if ( element instanceof EntityObject ) {
+
+            return toMap( ( EntityObject ) element );
+        }
+
+        //recurse into another list structure (2d + arrays)
+        if (element instanceof ListField || element instanceof ArrayField  || element instanceof
SetField){
+            return processCollection( ( Collection ) ( ( AbstractField ) element ).getValue()
);
+        }
+
+        if ( element instanceof List || element instanceof Set ) {
+
+            return processCollection( ( Collection ) element ); // recursion;
+        }
+
+        return element;
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/0fa30b9e/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EntityMappingParser.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EntityMappingParser.java
b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EntityMappingParser.java
index 406d879..db4853e 100644
--- a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EntityMappingParser.java
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EntityMappingParser.java
@@ -30,8 +30,10 @@ import java.util.List;
 import java.util.Map;
 import java.util.Stack;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 import org.apache.usergrid.persistence.model.entity.EntityMap;
-import org.apache.usergrid.persistence.model.field.value.Location;
 
 
 /**
@@ -42,12 +44,22 @@ import org.apache.usergrid.persistence.model.field.value.Location;
  */
 public class EntityMappingParser implements FieldParser {
 
+    private static final Logger log = LoggerFactory.getLogger( EntityMappingParser.class
);
+
 
     /**
      * Our stack for fields
      */
     private Stack<String> fieldStack = new Stack();
 
+    /**
+     * Keeps track fo our last field type.  Used for nested objects and nested collections
+     */
+    private Stack<Object> lastCollection = new Stack();
+
+    /**
+     * List of all field tuples to return
+     */
     private List<EntityField> fields = new ArrayList<>();
 
 
@@ -84,31 +96,25 @@ public class EntityMappingParser implements FieldParser {
 
 
 
-    private void visit( final Map<String, ?> value ) {
-        //it's a location field, index it as such
-        if ( EntityMap.isLocationField( value ) ) {
-//            fields.add( EntityField.create( fieldStack.peek(), value ) );
-            return;
-        }
-
-        iterate( value );
-    }
-
-
     /**
      * Iterate over a collection
      */
     private void iterate( final Collection value ) {
 
-        //no op
-        if ( value.size() == 0 ) {
+        //we don't support indexing 2 dimensional arrays.  Short circuit with a warning so
we can track operationally
+        if(!lastCollection.isEmpty() && lastCollection.peek() instanceof Collection){
+            log.warn( "Encountered 2 collections consecutively.  N+1 dimensional arrays are
unsupported, only arrays of depth 1 are supported" );
             return;
         }
 
+        lastCollection.push( value );
+
         //fisit all the object element
         for ( final Object element : value ) {
             visitValue( element );
         }
+
+        lastCollection.pop();
     }
 
 
@@ -117,7 +123,7 @@ public class EntityMappingParser implements FieldParser {
      */
     private void visitValue( final Object value ) {
 
-        if ( isMap( value ) ) {
+        if ( value instanceof Map ) {
             //if it's a location, then create a location field.
             if ( EntityMap.isLocationField( (Map)value ) ) {
                 fields.add( EntityField.create( fieldStack.peek(), ( Map ) value ) );
@@ -128,7 +134,7 @@ public class EntityMappingParser implements FieldParser {
         }
 
         //TODO figure out our nested array structure
-        else if ( isCollection( value ) ) {
+        else if ( value instanceof Collection) {
             iterate( ( Collection ) value );
         }
         else {
@@ -189,30 +195,6 @@ public class EntityMappingParser implements FieldParser {
 
 
     /**
-     * Return true if it's a map
-     */
-    private boolean isMap( final Object value ) {
-        return value instanceof Map;
-    }
-
-
-    /**
-     * Return true if it's a collection
-     */
-    private boolean isCollection( final Object value ) {
-        return value instanceof Collection;
-    }
-
-
-    /**
-     * Return true if this is a primitive (inverse of isMap and isCollection)
-     */
-    private boolean isPrimitive( final Object value ) {
-        return !isMap( value ) && !isCollection( value );
-    }
-
-
-    /**
      * Push a new fieldname on to the stack
      */
     private void pushField( final String fieldName ) {

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/0fa30b9e/stack/corepersistence/queryindex/src/test/java/org/apache/usergrid/persistence/index/impl/EntityToMapConverterTest.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/test/java/org/apache/usergrid/persistence/index/impl/EntityToMapConverterTest.java
b/stack/corepersistence/queryindex/src/test/java/org/apache/usergrid/persistence/index/impl/EntityToMapConverterTest.java
index c198020..020fad6 100644
--- a/stack/corepersistence/queryindex/src/test/java/org/apache/usergrid/persistence/index/impl/EntityToMapConverterTest.java
+++ b/stack/corepersistence/queryindex/src/test/java/org/apache/usergrid/persistence/index/impl/EntityToMapConverterTest.java
@@ -38,12 +38,14 @@ import org.apache.usergrid.persistence.model.entity.Entity;
 import org.apache.usergrid.persistence.model.field.ArrayField;
 import org.apache.usergrid.persistence.model.field.BooleanField;
 import org.apache.usergrid.persistence.model.field.DoubleField;
+import org.apache.usergrid.persistence.model.field.EntityObjectField;
 import org.apache.usergrid.persistence.model.field.Field;
 import org.apache.usergrid.persistence.model.field.FloatField;
 import org.apache.usergrid.persistence.model.field.IntegerField;
 import org.apache.usergrid.persistence.model.field.LocationField;
 import org.apache.usergrid.persistence.model.field.LongField;
 import org.apache.usergrid.persistence.model.field.StringField;
+import org.apache.usergrid.persistence.model.field.value.EntityObject;
 import org.apache.usergrid.persistence.model.field.value.Location;
 import org.apache.usergrid.persistence.model.util.EntityUtils;
 import org.apache.usergrid.persistence.model.util.UUIDGenerator;
@@ -128,16 +130,14 @@ public class EntityToMapConverterTest {
 
     @Test
     public void testStringField() {
-        testSingleField( new StringField( "name", "value" ),
-                ( field, entityField ) -> assertEquals( field.getValue(),
+        testSingleField( new StringField( "name", "value" ), ( field, entityField ) ->
assertEquals( field.getValue(),
                         entityField.get( IndexingUtils.FIELD_STRING ) ) );
     }
 
 
     @Test
     public void testBooleanField() {
-        testSingleField( new BooleanField( "name", true ),
-                ( field, entityField ) -> assertEquals( field.getValue(),
+        testSingleField( new BooleanField( "name", true ), ( field, entityField ) -> assertEquals(
field.getValue(),
                         entityField.get( IndexingUtils.FIELD_BOOLEAN ) ) );
     }
 
@@ -145,50 +145,45 @@ public class EntityToMapConverterTest {
     @Test
     public void testIntegerField() {
         testSingleField( new IntegerField( "name", 100 ),
-                ( field, entityField ) -> assertEquals( field.getValue(),
-                        entityField.get( IndexingUtils.FIELD_INT ) ) );
+                ( field, entityField ) -> assertEquals( field.getValue(), entityField.get(
IndexingUtils.FIELD_INT )
+                                                      ) );
     }
 
 
     @Test
     public void testLongField() {
-        testSingleField( new LongField( "name", 100l ),
-                ( field, entityField ) -> assertEquals( field.getValue(),
+        testSingleField( new LongField( "name", 100l ), ( field, entityField ) -> assertEquals(
field.getValue(),
                         entityField.get( IndexingUtils.FIELD_LONG ) ) );
     }
 
 
     @Test
     public void testFloadField() {
-        testSingleField( new FloatField( "name", 1.10f ),
-                ( field, entityField ) -> assertEquals( field.getValue(),
+        testSingleField( new FloatField( "name", 1.10f ), ( field, entityField ) -> assertEquals(
field.getValue(),
                         entityField.get( IndexingUtils.FIELD_FLOAT ) ) );
     }
 
 
     @Test
     public void testDoubleField() {
-        testSingleField( new DoubleField( "name", 2.20d ),
-                ( field, entityField ) -> assertEquals( field.getValue(),
+        testSingleField( new DoubleField( "name", 2.20d ), ( field, entityField ) -> assertEquals(
field.getValue(),
                         entityField.get( IndexingUtils.FIELD_DOUBLE ) ) );
     }
 
 
     @Test
     public void testLocationField() {
-        testSingleField( new LocationField( "name", new Location( 10, 20 ) ), (field, entityField)
-> {
+        testSingleField( new LocationField( "name", new Location( 10, 20 ) ), ( field, entityField
) -> {
             final Map<String, Double> latLong = ( Map<String, Double> ) entityField.get(
IndexingUtils.FIELD_LOCATION );
 
-            assertEquals(Double.valueOf( 10 ), latLong.get( org.apache.usergrid.persistence.model.entity.EntityToMapConverter.LAT
));
-            assertEquals(Double.valueOf( 20 ), latLong.get( org.apache.usergrid.persistence.model.entity.EntityToMapConverter.LON
));
+            assertEquals( Double.valueOf( 10 ),
+                    latLong.get( org.apache.usergrid.persistence.model.entity.EntityToMapConverter.LAT
) );
+            assertEquals( Double.valueOf( 20 ),
+                    latLong.get( org.apache.usergrid.persistence.model.entity.EntityToMapConverter.LON
) );
         } );
     }
 
 
-    private static void assertField() {
-
-    }
-
 
     /**
      * Test the single field in our root level
@@ -348,4 +343,314 @@ public class EntityToMapConverterTest {
             assertEquals( array.getValue().get( i ), field.get( indexType ) );
         }
     }
+
+
+
+    @Test
+    public void testStringFieldSubObject() {
+        testNestedField( new StringField( "name", "value" ), ( field, entityField ) ->
assertEquals( field.getValue(),
+                entityField.get( IndexingUtils.FIELD_STRING ) ) );
+    }
+
+
+    @Test
+    public void testBooleanFieldSubObject() {
+        testNestedField( new BooleanField( "name", true ), ( field, entityField ) -> assertEquals(
field.getValue(),
+                entityField.get( IndexingUtils.FIELD_BOOLEAN ) ) );
+    }
+
+
+    @Test
+    public void testIntegerFieldSubObject() {
+        testNestedField( new IntegerField( "name", 100 ),
+                ( field, entityField ) -> assertEquals( field.getValue(), entityField.get(
IndexingUtils.FIELD_INT )
+                                                      ) );
+    }
+
+
+    @Test
+    public void testLongFieldSubObject() {
+        testNestedField( new LongField( "name", 100l ), ( field, entityField ) -> assertEquals(
field.getValue(),
+                entityField.get( IndexingUtils.FIELD_LONG ) ) );
+    }
+
+
+    @Test
+    public void testFloadFieldSubObject() {
+        testNestedField( new FloatField( "name", 1.10f ), ( field, entityField ) -> assertEquals(
field.getValue(),
+                entityField.get( IndexingUtils.FIELD_FLOAT ) ) );
+    }
+
+
+    @Test
+    public void testDoubleFieldSubObject() {
+        testNestedField( new DoubleField( "name", 2.20d ), ( field, entityField ) -> assertEquals(
field.getValue(),
+                entityField.get( IndexingUtils.FIELD_DOUBLE ) ) );
+    }
+
+
+    @Test
+    public void testLocationFieldSubObject() {
+        testNestedField( new LocationField( "name", new Location( 10, 20 ) ), ( field, entityField
) -> {
+            final Map<String, Double> latLong = ( Map<String, Double> ) entityField.get(
IndexingUtils.FIELD_LOCATION );
+
+            assertEquals( Double.valueOf( 10 ),
+                    latLong.get( org.apache.usergrid.persistence.model.entity.EntityToMapConverter.LAT
) );
+            assertEquals( Double.valueOf( 20 ),
+                    latLong.get( org.apache.usergrid.persistence.model.entity.EntityToMapConverter.LON
) );
+        } );
+    }
+
+
+     /**
+      * Test primitive arrays in the root of an object
+      *
+      * @param storedField The field stored on the nested object
+      * @param assertFunction The function
+      */
+     private <T> void testNestedField( final Field<T> storedField,   Action2<Field,
EntityField> assertFunction ) {
+
+         EntityObject leafEntity = new EntityObject( );
+
+         leafEntity.setField( storedField );
+
+
+
+         final EntityObjectField nested2Field = new EntityObjectField( "nested2", leafEntity
);
+
+         final EntityObject nested2 = new EntityObject();
+         nested2.setField( nested2Field );
+
+         final EntityObjectField nested1Field = new EntityObjectField( "nested1", nested2
);
+
+
+         Entity rootEntity = new Entity( "test" );
+
+
+         rootEntity.setField( nested1Field );
+
+
+         final UUID version = UUIDGenerator.newTimeUUID();
+
+         EntityUtils.setVersion( rootEntity, version );
+
+         final ApplicationScope scope = new ApplicationScopeImpl( createId( "application"
) );
+
+         final IndexEdge indexEdge =
+                 new IndexEdgeImpl( createId( "source" ), "testEdgeType", SearchEdge.NodeType.SOURCE,
1000 );
+
+
+         final Map<String, Object> entityMap = EntityToMapConverter.convert( scope,
indexEdge, rootEntity );
+
+
+         final List<EntityField> fields = ( List<EntityField> ) entityMap.get(
IndexingUtils.ENTITY_FIELDS );
+
+         assertEquals( 1, fields.size() );
+
+
+         for ( int i = 0; i < fields.size(); i++ ) {
+             final EntityField field = fields.get( i );
+
+             final String path = "nested1.nested2." + storedField.getName();
+
+             assertEquals( path, field.get( IndexingUtils.FIELD_NAME ) );
+
+
+             assertFunction.call( storedField, field );
+         }
+     }
+
+
+    @Test
+    public void testStringFieldSubObjectArray() {
+        testNestedFieldArraySubObject( new StringField( "name", "value" ),
+                ( field, entityField ) -> assertEquals( field.getValue(),
+                        entityField.get( IndexingUtils.FIELD_STRING ) ) );
+    }
+
+
+    @Test
+    public void testBooleanFieldSubObjectArray() {
+        testNestedFieldArraySubObject( new BooleanField( "name", true ),
+                ( field, entityField ) -> assertEquals( field.getValue(),
+                        entityField.get( IndexingUtils.FIELD_BOOLEAN ) ) );
+    }
+
+
+    @Test
+    public void testIntegerFieldSubObjectArray() {
+        testNestedFieldArraySubObject( new IntegerField( "name", 100 ),
+                ( field, entityField ) -> assertEquals( field.getValue(), entityField.get(
IndexingUtils.FIELD_INT )
+                                                      ) );
+    }
+
+
+    @Test
+    public void testLongFieldSubObjectArray() {
+        testNestedFieldArraySubObject( new LongField( "name", 100l ),
+                ( field, entityField ) -> assertEquals( field.getValue(),
+                        entityField.get( IndexingUtils.FIELD_LONG ) ) );
+    }
+
+
+    @Test
+    public void testFloadFieldSubObjectArray() {
+        testNestedFieldArraySubObject( new FloatField( "name", 1.10f ),
+                ( field, entityField ) -> assertEquals( field.getValue(),
+                        entityField.get( IndexingUtils.FIELD_FLOAT ) ) );
+    }
+
+
+    @Test
+    public void testDoubleFieldSubObjectArray() {
+        testNestedFieldArraySubObject( new DoubleField( "name", 2.20d ),
+                ( field, entityField ) -> assertEquals( field.getValue(),
+                        entityField.get( IndexingUtils.FIELD_DOUBLE ) ) );
+    }
+
+
+    @Test
+    public void testLocationFieldSubObjectArray() {
+        testNestedFieldArraySubObject( new LocationField( "name", new Location( 10, 20 )
), ( field, entityField ) -> {
+            final Map<String, Double> latLong = ( Map<String, Double> ) entityField.get(
IndexingUtils.FIELD_LOCATION );
+
+            assertEquals( Double.valueOf( 10 ),
+                    latLong.get( org.apache.usergrid.persistence.model.entity.EntityToMapConverter.LAT
) );
+            assertEquals( Double.valueOf( 20 ),
+                    latLong.get( org.apache.usergrid.persistence.model.entity.EntityToMapConverter.LON
) );
+        } );
+    }
+
+
+     /**
+      * Test primitive arrays in the root of an object
+      *
+      * @param storedField The field stored on the nested object
+      * @param assertFunction The function
+      */
+     private <T> void testNestedFieldArraySubObject( final Field<T> storedField,
+                                                     Action2<Field, EntityField> assertFunction
) {
+
+         EntityObject leafEntity = new EntityObject( );
+
+         leafEntity.setField( storedField );
+
+
+
+         final EntityObjectField nested2Field = new EntityObjectField( "nested2", leafEntity
);
+
+         final EntityObject nested2 = new EntityObject();
+         nested2.setField( nested2Field );
+
+         final EntityObjectField nested1Field = new EntityObjectField( "nested1", nested2
);
+
+         final EntityObject nested1 = new EntityObject();
+         nested1.setField( nested1Field );
+
+
+
+         final ArrayField<EntityObject> array = new ArrayField<>( "array" );
+
+         array.add( nested1 );
+
+         Entity rootEntity = new Entity( "test" );
+
+         rootEntity.setField( array );
+
+
+         final UUID version = UUIDGenerator.newTimeUUID();
+
+         EntityUtils.setVersion( rootEntity, version );
+
+         final ApplicationScope scope = new ApplicationScopeImpl( createId( "application"
) );
+
+         final IndexEdge indexEdge =
+                 new IndexEdgeImpl( createId( "source" ), "testEdgeType", SearchEdge.NodeType.SOURCE,
1000 );
+
+
+         final Map<String, Object> entityMap = EntityToMapConverter.convert( scope,
indexEdge, rootEntity );
+
+
+         final List<EntityField> fields = ( List<EntityField> ) entityMap.get(
IndexingUtils.ENTITY_FIELDS );
+
+         assertEquals( 1, fields.size() );
+
+
+         for ( int i = 0; i < fields.size(); i++ ) {
+             final EntityField field = fields.get( i );
+
+             final String path = "array.nested1.nested2." + storedField.getName();
+
+             assertEquals( path, field.get( IndexingUtils.FIELD_NAME ) );
+
+
+             assertFunction.call( storedField, field );
+         }
+     }
+
+
+    /**
+     * 2d + arrays aren't supported, ensure we drop elements with a depth > 1
+     *
+     */
+    @Test
+    public void testNDimensionalArray() {
+
+
+
+        //create an object with a string value in it, this should be indexed
+        final StringField string = new StringField( "string", "value" );
+
+        final EntityObject nested1 = new EntityObject();
+        nested1.setField( string );
+
+
+        //create a sub array, this shouldn't get indexed
+        final ArrayField<String> subArray = new ArrayField<>( "subArray" );
+
+        subArray.add( "test2" );
+
+        final ArrayField<Object> array = new ArrayField<>( "array" );
+
+        //not indexed
+        array.add( subArray);
+
+        //indexed
+        array.add( nested1 );
+
+
+
+        Entity rootEntity = new Entity( "test" );
+
+        rootEntity.setField( array );
+
+
+        final UUID version = UUIDGenerator.newTimeUUID();
+
+        EntityUtils.setVersion( rootEntity, version );
+
+        final ApplicationScope scope = new ApplicationScopeImpl( createId( "application"
) );
+
+        final IndexEdge indexEdge =
+                new IndexEdgeImpl( createId( "source" ), "testEdgeType", SearchEdge.NodeType.SOURCE,
1000 );
+
+
+        final Map<String, Object> entityMap = EntityToMapConverter.convert( scope,
indexEdge, rootEntity );
+
+
+        final List<EntityField> fields = ( List<EntityField> ) entityMap.get(
IndexingUtils.ENTITY_FIELDS );
+
+        assertEquals( 1, fields.size() );
+
+
+        final EntityField field = fields.get( 0 );
+
+        final String path = "array.string";
+
+        assertEquals( path, field.get( IndexingUtils.FIELD_NAME ) );
+
+
+       assertEquals("value", field.get( IndexingUtils.FIELD_STRING ));
+
+    }
 }


Mime
View raw message