You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by el...@apache.org on 2013/04/02 19:37:46 UTC

svn commit: r1463635 - in /labs/mavibot/branches/mavibot-multivalue-support/mavibot/src: main/java/org/apache/mavibot/btree/store/ test/java/org/apache/mavibot/btree/serializer/

Author: elecharny
Date: Tue Apr  2 17:37:46 2013
New Revision: 1463635

URL: http://svn.apache.org/r1463635
Log:
o Added a equals() method in RevisionName
o Added a serializer and some test for this class

Added:
    labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/store/RevisionNameSerializer.java
    labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/serializer/RevisionNameSerializerTest.java
Modified:
    labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/store/RevisionName.java

Modified: labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/store/RevisionName.java
URL: http://svn.apache.org/viewvc/labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/store/RevisionName.java?rev=1463635&r1=1463634&r2=1463635&view=diff
==============================================================================
--- labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/store/RevisionName.java (original)
+++ labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/store/RevisionName.java Tue Apr  2 17:37:46 2013
@@ -84,6 +84,38 @@ public class RevisionName
 
 
     /**
+     * @see Object#equals(Object)
+     */
+    public boolean equals( Object that )
+    {
+        if ( this == that )
+        {
+            return true;
+        }
+
+        if ( !( that instanceof RevisionName ) )
+        {
+            return false;
+        }
+
+        RevisionName revisionName = ( RevisionName ) that;
+
+        if ( revision != revisionName.revision )
+        {
+            return false;
+        }
+
+        if ( name == null )
+        {
+            return revisionName.name == null;
+        }
+
+        return ( name.equals( revisionName.name ) );
+
+    }
+
+
+    /**
      * @see Object#toString()
      */
     public String toString()

Added: labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/store/RevisionNameSerializer.java
URL: http://svn.apache.org/viewvc/labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/store/RevisionNameSerializer.java?rev=1463635&view=auto
==============================================================================
--- labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/store/RevisionNameSerializer.java (added)
+++ labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/store/RevisionNameSerializer.java Tue Apr  2 17:37:46 2013
@@ -0,0 +1,180 @@
+/*
+ *  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.mavibot.btree.store;
+
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import org.apache.mavibot.btree.serializer.AbstractElementSerializer;
+import org.apache.mavibot.btree.serializer.BufferHandler;
+import org.apache.mavibot.btree.serializer.ByteArraySerializer;
+import org.apache.mavibot.btree.serializer.IntSerializer;
+import org.apache.mavibot.btree.serializer.LongSerializer;
+import org.apache.mavibot.btree.serializer.StringSerializer;
+import org.apache.mavibot.btree.util.Strings;
+
+
+/**
+ * A serializer for the RevisionName object. The RevisionName will be serialized 
+ * as a long (the revision), followed by the String.
+ * 
+ * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
+ */
+public class RevisionNameSerializer extends AbstractElementSerializer<RevisionName>
+{
+
+    public RevisionNameSerializer()
+    {
+        super( new RevisionNameComparator() );
+    }
+
+
+    /**
+     * A static method used to deserialize a RevisionName from a byte array.
+     * @param in The byte array containing the RevisionName
+     * @return A RevisionName instance
+     */
+    public static RevisionName deserialize( byte[] in )
+    {
+        return deserialize( in, 0 );
+    }
+
+
+    /**
+     * A static method used to deserialize a RevisionName from a byte array.
+     * @param in The byte array containing the RevisionName
+     * @param start the position in the byte[] we will deserialize the RevisionName from
+     * @return A RevisionName instance
+     */
+    public static RevisionName deserialize( byte[] in, int start )
+    {
+        // The buffer must be 8 bytes plus 4 bytes long (the revision is a long, and the name is a String
+        if ( ( in == null ) || ( in.length < 12 + start ) )
+        {
+            throw new RuntimeException( "Cannot extract a RevisionName from a buffer with not enough bytes" );
+        }
+
+        long revision = LongSerializer.deserialize( in, start );
+        String name = StringSerializer.deserialize( in, 8 + start );
+
+        RevisionName revisionName = new RevisionName( revision, name );
+
+        return revisionName;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public byte[] serialize( RevisionName revisionName )
+    {
+        if ( revisionName == null )
+        {
+            throw new RuntimeException( "The revisionName instance should not be null " );
+        }
+
+        byte[] result = null;
+
+        if ( revisionName.getName() != null )
+        {
+            byte[] stringBytes = Strings.getBytesUtf8( revisionName.getName() );
+            int stringLen = stringBytes.length;
+            result = new byte[8 + 4 + stringBytes.length];
+            LongSerializer.serialize( result, 0, revisionName.getRevision() );
+
+            if ( stringLen > 0 )
+            {
+                ByteArraySerializer.serialize( result, 8, stringBytes );
+            }
+        }
+        else
+        {
+            result = new byte[8 + 4];
+            LongSerializer.serialize( result, 0, revisionName.getRevision() );
+            StringSerializer.serialize( result, 8, null );
+        }
+
+        return result;
+    }
+
+
+    /**
+     * Serialize a RevisionName
+     * 
+     * @param buffer the Buffer that will contain the serialized value
+     * @param start the position in the buffer we will store the serialized RevisionName
+     * @param value the value to serialize
+     * @return The byte[] containing the serialized RevisionName
+     */
+    public static byte[] serialize( byte[] buffer, int start, RevisionName revisionName )
+    {
+        if ( revisionName.getName() != null )
+        {
+            byte[] stringBytes = Strings.getBytesUtf8( revisionName.getName() );
+            int stringLen = stringBytes.length;
+            LongSerializer.serialize( buffer, start, revisionName.getRevision() );
+            IntSerializer.serialize( buffer, 8 + start, stringLen );
+            ByteArraySerializer.serialize( buffer, 12 + start, stringBytes );
+        }
+        else
+        {
+            LongSerializer.serialize( buffer, start, revisionName.getRevision() );
+            StringSerializer.serialize( buffer, 8, null );
+        }
+
+        return buffer;
+    }
+
+
+    @Override
+    public RevisionName deserialize( BufferHandler bufferHandler ) throws IOException
+    {
+        byte[] revisionBytes = bufferHandler.read( 8 );
+        long revision = LongSerializer.deserialize( revisionBytes );
+
+        byte[] lengthBytes = bufferHandler.read( 4 );
+
+        int len = IntSerializer.deserialize( lengthBytes );
+
+        switch ( len )
+        {
+            case 0:
+                return new RevisionName( revision, "" );
+
+            case -1:
+                return new RevisionName( revision, null );
+
+            default:
+                byte[] nameBytes = bufferHandler.read( len );
+
+                return new RevisionName( revision, Strings.utf8ToString( nameBytes ) );
+        }
+    }
+
+
+    @Override
+    public RevisionName deserialize( ByteBuffer buffer ) throws IOException
+    {
+        // TODO Auto-generated method stub
+        return null;
+    }
+}

Added: labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/serializer/RevisionNameSerializerTest.java
URL: http://svn.apache.org/viewvc/labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/serializer/RevisionNameSerializerTest.java?rev=1463635&view=auto
==============================================================================
--- labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/serializer/RevisionNameSerializerTest.java (added)
+++ labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/serializer/RevisionNameSerializerTest.java Tue Apr  2 17:37:46 2013
@@ -0,0 +1,82 @@
+/*
+ *  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.mavibot.btree.serializer;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+
+import org.apache.mavibot.btree.store.RevisionName;
+import org.apache.mavibot.btree.store.RevisionNameSerializer;
+import org.junit.Test;
+
+
+/**
+ * Test the RevisionNameSerializer class
+ * 
+ * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
+ */
+public class RevisionNameSerializerTest
+{
+    private static RevisionNameSerializer serializer = new RevisionNameSerializer();
+
+
+    @Test
+    public void testRevisionNameSerializer() throws IOException
+    {
+        RevisionName value = null;
+
+        try
+        {
+            serializer.serialize( value );
+            fail();
+        }
+        catch ( Exception e )
+        {
+            //exptected
+        }
+
+        // ------------------------------------------------------------------
+        value = new RevisionName( 1L, null );
+        byte[] result = serializer.serialize( value );
+
+        assertEquals( 12, result.length );
+
+        assertEquals( 1L, ( long ) LongSerializer.deserialize( result ) );
+        assertNull( StringSerializer.deserialize( result, 8 ) );
+
+        assertEquals( value, serializer.deserialize( new BufferHandler( result ) ) );
+
+        // ------------------------------------------------------------------
+        value = new RevisionName( 0L, "" );
+        result = serializer.serialize( value );
+
+        assertEquals( value, serializer.deserialize( new BufferHandler( result ) ) );
+
+        // ------------------------------------------------------------------
+        value = new RevisionName( 0L, "L\u00E9charny" );
+        result = serializer.serialize( value );
+
+        assertEquals( value, serializer.deserialize( new BufferHandler( result ) ) );
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org