You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by sn...@apache.org on 2014/04/07 22:22:01 UTC

[10/28] git commit: Added working smile binary serialization. Added working annotation to the corepersistance teir for the serialization of setters and getters. Ignoring the Id when doing serialization. Added test to prove it is working as intended.

Added working smile binary serialization.
Added working annotation to the corepersistance teir for the serialization of setters and getters.
Ignoring the Id when doing serialization.
Added test to prove it is working as intended.


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

Branch: refs/heads/two-dot-o
Commit: b4b5564bfd16f7aa1204d1af1df8e8249dfa55b4
Parents: ccdf99a
Author: grey <gr...@apigee.com>
Authored: Mon Mar 17 18:03:17 2014 -0700
Committer: grey <gr...@apigee.com>
Committed: Mon Mar 17 18:03:17 2014 -0700

----------------------------------------------------------------------
 .../MvccEntitySerializationStrategyImpl.java    | 146 +++----------------
 ...MvccEntitySerializationStrategyImplTest.java |   2 +-
 stack/corepersistence/model/pom.xml             |   5 -
 .../persistence/model/entity/Entity.java        |   5 +-
 .../model/field/value/EntityObject.java         |  24 +++
 5 files changed, 47 insertions(+), 135 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/b4b5564b/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyImpl.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyImpl.java b/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyImpl.java
index d68b1aa..5d95fa5 100644
--- a/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyImpl.java
+++ b/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyImpl.java
@@ -23,13 +23,13 @@ import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
-import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.UUID;
 
 import org.codehaus.jackson.JsonGenerationException;
 import org.codehaus.jackson.map.JsonMappingException;
 import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.smile.SmileFactory;
 
 import org.apache.cassandra.db.marshal.BytesType;
 import org.apache.cassandra.db.marshal.ReversedType;
@@ -64,8 +64,8 @@ import com.netflix.astyanax.model.CompositeBuilder;
 import com.netflix.astyanax.model.CompositeParser;
 import com.netflix.astyanax.model.Composites;
 import com.netflix.astyanax.serializers.AbstractSerializer;
+import com.netflix.astyanax.serializers.ByteBufferSerializer;
 import com.netflix.astyanax.serializers.BytesArraySerializer;
-import com.netflix.astyanax.serializers.ObjectSerializer;
 import com.netflix.astyanax.serializers.UUIDSerializer;
 
 
@@ -80,6 +80,11 @@ public class MvccEntitySerializationStrategyImpl implements MvccEntitySerializat
 
     private static final IdRowCompositeSerializer ID_SER = IdRowCompositeSerializer.get();
 
+    private static final ByteBufferSerializer BUFFER_SERIALIZER = ByteBufferSerializer.get();
+
+    private static final BytesArraySerializer BYTES_ARRAY_SERIALIZER = BytesArraySerializer.get();
+
+
     private static final CollectionScopedRowKeySerializer<Id> ROW_KEY_SER =
             new CollectionScopedRowKeySerializer<Id>( ID_SER );
 
@@ -276,11 +281,9 @@ public class MvccEntitySerializationStrategyImpl implements MvccEntitySerializat
 
     public static class EntitySerializer extends AbstractSerializer<EntityWrapper> {
 
-        //public static final SmileFactory f = new SmileFactory(  );
+        public static final SmileFactory f = new SmileFactory(  );
 
         public static ObjectMapper mapper = new ObjectMapper(  );
-        private static final BytesArraySerializer BYTES_ARRAY_SERIALIZER = BytesArraySerializer.get();
-
 
         private static byte[] STATE_COMPLETE = new byte[] { 0 };
         private static byte[] STATE_DELETED = new byte[] { 1 };
@@ -293,7 +296,7 @@ public class MvccEntitySerializationStrategyImpl implements MvccEntitySerializat
         private static final byte[] EMPTY = new byte[] { 0x0 };
         String helper;
 
-//TODO:Make sure your exceptions provide descriptive error messages. And to make sure you even needs the descriptions
+//TODO:Make sure your exceptions provide descriptive error messages.
         @Override
         public ByteBuffer toByteBuffer( final EntityWrapper wrapper ) {
             if ( wrapper == null ) {
@@ -302,11 +305,7 @@ public class MvccEntitySerializationStrategyImpl implements MvccEntitySerializat
 
             CompositeBuilder builder = Composites.newCompositeBuilder();
 
-
             builder.addBytes( VERSION );
-           // f.disable( SmileParser.Feature.REQUIRE_HEADER );
-
-           // mapper = new ObjectMapper( f );
 
             //mark this version as empty
             if ( !wrapper.entity.isPresent() ) {
@@ -328,7 +327,6 @@ public class MvccEntitySerializationStrategyImpl implements MvccEntitySerializat
 
             try {
                 builder.addBytes( mapper.writeValueAsBytes( wrapper.entity.get() ) );
-                helper = mapper.writeValueAsString( wrapper.entity.get() );
             }
             catch ( JsonMappingException e ) {
                 e.printStackTrace();
@@ -345,31 +343,15 @@ public class MvccEntitySerializationStrategyImpl implements MvccEntitySerializat
 
         @Override
         public EntityWrapper fromByteBuffer( final ByteBuffer byteBuffer ) {
-            CompositeParser parser = Composites.newCompositeParser( byteBuffer );
-            byte[] b = new byte[byteBuffer.remaining()];
-
-            //byteBuffer.get( b, 0, b.length );
-            //return mapper.readValue(b, Entity.class);
+           CompositeParser parser = Composites.newCompositeParser( byteBuffer );
 
             byte[] version = parser.read( BYTES_ARRAY_SERIALIZER );
-//            try {
-//                version = mapper.readValue( byteBuffer.array(),0,1, byte[].class);
-//            }
-//            catch ( IOException e ) {
-//                e.printStackTrace();
-//            }
 
             if ( !Arrays.equals( VERSION, version ) ) {
                 throw new UnsupportedOperationException( "A version of type " + version + " is unsupported" );
             }
 
             byte[] state = parser.read( BYTES_ARRAY_SERIALIZER );
-//            try {
-//                state = mapper.readValue( byteBuffer.array(),1,2, new TypeReference<Byte>() {});
-//            }
-//            catch ( IOException e ) {
-//                e.printStackTrace();
-//            }
 
             /**
              * It's been deleted, remove it
@@ -378,113 +360,22 @@ public class MvccEntitySerializationStrategyImpl implements MvccEntitySerializat
                 return new EntityWrapper( MvccEntity.Status.COMPLETE, Optional.<Entity>absent() );
             }
 
-            LinkedHashMap storedObject = null;
             Entity storedEntity = null;
-            String derper = null;
-            byteBuffer.position( 0 );
-            try {
-                storedEntity = mapper.readValue( helper,Entity.class);
-            }
-            catch ( IOException e ) {
-                e.printStackTrace();
-            }
-            storedEntity = convertToEntity(storedObject);
 
-            final Optional<Entity> entity = Optional.of( storedEntity );
-
-            if ( Arrays.equals( STATE_COMPLETE, state ) ) {
-                return new EntityWrapper( MvccEntity.Status.COMPLETE, entity );
-            }
-
-            //it's partial by default
-            return new EntityWrapper( MvccEntity.Status.PARTIAL, entity );
-        }
-
-        private Entity convertToEntity( final Object storedObject ) {
-            Entity storedEntity = null;
-            return null;
-        }
-    }
-
-    /**
-     * TODO: Serializer for the entity. This just uses object serialization, change this to use SMILE before production!
-     * We want to retain the Optional wrapper.  It helps us easily mark something as cleaned without removing the column
-     * and makes it obvious that the entity could be missing in the api
-     */
-    private static class ObjSerializer extends AbstractSerializer<EntityWrapper> {
-
-        private static final BytesArraySerializer BYTES_ARRAY_SERIALIZER = BytesArraySerializer.get();
-        private static final ObjectSerializer SER = ObjectSerializer.get();
-
-        private static byte[] STATE_COMPLETE = new byte[] { 0 };
-        private static byte[] STATE_DELETED = new byte[] { 1 };
-        private static byte[] STATE_PARTIAL = new byte[] { 2 };
-
-        private static byte[] VERSION = new byte[] { 0 };
-
-
-        //the marker for when we're passed a "null" value
-        private static final byte[] EMPTY = new byte[] { 0x0 };
-
-
-        @Override
-        public ByteBuffer toByteBuffer( final EntityWrapper wrapper ) {
-
-
-            CompositeBuilder builder = Composites.newCompositeBuilder();
-
-
-            builder.addBytes( VERSION );
-
-
-            //mark this version as empty
-            if ( !wrapper.entity.isPresent() ) {
-                //we're empty
-                builder.addBytes( STATE_DELETED );
-
-                return builder.build();
-            }
-
-            //we have an entity
-
-            if ( wrapper.status == MvccEntity.Status.COMPLETE ) {
-                builder.addBytes( STATE_COMPLETE );
-            }
-
-            else {
-                builder.addBytes( STATE_PARTIAL );
-            }
-
-            builder.addBytes( SER.toByteBuffer( wrapper.entity.get() ));
-
-            return builder.build();
-        }
-
-
-        @Override
-        public EntityWrapper fromByteBuffer( final ByteBuffer byteBuffer ) {
-
-            CompositeParser parser = Composites.newCompositeParser( byteBuffer );
+            ByteBuffer jsonBytes = parser.read(  BUFFER_SERIALIZER );
 
+            try {
 
-            final byte[] version = parser.read( BYTES_ARRAY_SERIALIZER );
+                byte[] array = jsonBytes.array();
+                int start = jsonBytes.arrayOffset();
+                int length = jsonBytes.remaining();
 
-            if ( !Arrays.equals( VERSION, version ) ) {
-                throw new UnsupportedOperationException( "A version of type " + version + " is unsupported" );
+                storedEntity = mapper.readValue( array,start,length,Entity.class);
             }
-
-
-            final byte[] state = parser.read( BYTES_ARRAY_SERIALIZER );
-
-            /**
-             * It's been deleted, remove it
-             */
-            if ( Arrays.equals( STATE_DELETED, state ) ) {
-                return new EntityWrapper( MvccEntity.Status.COMPLETE, Optional.<Entity>absent() );
+            catch ( IOException e ) {
+                e.printStackTrace();
             }
 
-            final Entity storedEntity = ( Entity ) parser.read( SER );
-
             final Optional<Entity> entity = Optional.of( storedEntity );
 
             if ( Arrays.equals( STATE_COMPLETE, state ) ) {
@@ -492,7 +383,6 @@ public class MvccEntitySerializationStrategyImpl implements MvccEntitySerializat
             }
 
             //it's partial by default
-
             return new EntityWrapper( MvccEntity.Status.PARTIAL, entity );
         }
     }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/b4b5564b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyImplTest.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyImplTest.java b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyImplTest.java
index bc4485d..ea4cb7b 100644
--- a/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyImplTest.java
+++ b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyImplTest.java
@@ -322,7 +322,7 @@ public class MvccEntitySerializationStrategyImplTest {
         //YES THIS SHOULD BE DOING WHA TI THOUGHT< BUT ITSN:T
         Field<Boolean> boolFieldReturned = returned.getEntity().get().getField( boolField.getName() );
 
-        assertSame( boolField, boolFieldReturned );
+        assertEquals( boolField, boolFieldReturned );
 
         Set<Field> results = new HashSet<Field>();
         results.addAll( entity.getFields() );

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/b4b5564b/stack/corepersistence/model/pom.xml
----------------------------------------------------------------------
diff --git a/stack/corepersistence/model/pom.xml b/stack/corepersistence/model/pom.xml
index 418ae53..8a7af5b 100644
--- a/stack/corepersistence/model/pom.xml
+++ b/stack/corepersistence/model/pom.xml
@@ -40,11 +40,6 @@
             <version>${commons.lang.version}</version>
             <scope>test</scope>
         </dependency>
-      <dependency>
-        <groupId>com.facebook.giraph.hive</groupId>
-        <artifactId>hive-io-exp-cmdline</artifactId>
-        <version>0.8-SNAPSHOT</version>
-      </dependency>
 
     </dependencies>
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/b4b5564b/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/Entity.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/Entity.java b/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/Entity.java
index ee6e656..1444961 100644
--- a/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/Entity.java
+++ b/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/Entity.java
@@ -20,9 +20,11 @@ package org.apache.usergrid.persistence.model.entity;
 
 
 import java.util.UUID;
+
+import org.codehaus.jackson.annotate.JsonIgnore;
+
 import org.apache.usergrid.persistence.model.field.value.EntityObject;
 
-import com.fasterxml.jackson.annotation.JsonIgnore;
 import com.google.common.base.Preconditions;
 
 
@@ -73,6 +75,7 @@ public class Entity extends EntityObject {
     }
 
 
+    @JsonIgnore
     public Id getId() {
         return id;
     }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/b4b5564b/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/field/value/EntityObject.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/field/value/EntityObject.java b/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/field/value/EntityObject.java
index e898392..331516e 100644
--- a/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/field/value/EntityObject.java
+++ b/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/field/value/EntityObject.java
@@ -19,10 +19,16 @@
 package org.apache.usergrid.persistence.model.field.value;
 
 import java.io.Serializable;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.codehaus.jackson.annotate.JsonAnyGetter;
+import org.codehaus.jackson.annotate.JsonAnySetter;
+import org.codehaus.jackson.annotate.JsonIgnore;
+import org.codehaus.jackson.annotate.JsonTypeInfo;
+
 import org.apache.usergrid.persistence.model.field.Field;
 
 /**
@@ -33,11 +39,13 @@ public class EntityObject implements Serializable {
     /**
      * Fields the users can set
      */
+    @JsonTypeInfo( use= JsonTypeInfo.Id.CLASS,include= JsonTypeInfo.As.WRAPPER_OBJECT,property="@class" )
     private final Map<String, Field> fields = new HashMap<String, Field>();
 
     /**
      * Add the field, return the old one if it existed
      */
+    @JsonIgnore
     public <T extends java.lang.Object> Field<T> setField( Field<T> value ) {
         return fields.put( value.getName(), value );
     }
@@ -45,10 +53,26 @@ public class EntityObject implements Serializable {
     /**
      * Get the field by name the user has set into the entity
      */
+    @JsonIgnore
     public <T extends java.lang.Object> Field<T> getField( String name ) {
         return fields.get( name );
     }
 
+    @JsonAnySetter
+    public void setFields(ArrayList al) {
+        if(al.size() == 0)
+            return;
+
+        for(int i = 0; i < al.size(); i++) {
+            Field fd = ( Field ) al.get( i );
+            fields.put( fd.getName(), fd);
+        }
+    }
+
+    @JsonAnyGetter
+    public void getFields( String name) {
+        fields.get( name );
+    }
     /**
      * Get all fields in the entity
      */