Author: elecharny
Date: Tue Jul 31 16:25:28 2012
New Revision: 1367657

URL: http://svn.apache.org/viewvc?rev=1367657&view=rev
Log:
o Added the serializer packahe
o Added the ElementSerializer interface
o Added the IntSerializer
o Added a test for this class

Added:
    
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/
    
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ElementSerializer.java
    
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/IntSerializer.java
    
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/Serializer.java
    
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/
    
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/IntSerializerTest.java

Added: 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ElementSerializer.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ElementSerializer.java?rev=1367657&view=auto
==============================================================================
--- 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ElementSerializer.java
 (added)
+++ 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ElementSerializer.java
 Tue Jul 31 16:25:28 2012
@@ -0,0 +1,48 @@
+/*
+ *  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;
+
+
+/**
+ * This interface is used by implementations elements serializers.
+ * 
+ * @param <T> The type for the element
+ * 
+ * @author <a href="mailto:[email protected]";>Mavibot labs Project</a>
+ */
+public interface ElementSerializer<T>
+{
+    /**
+     * Produce the byte[] representation of the element
+     * 
+     * @param key The element to serialize
+     * @return The byte[] containing the serialized element
+     */
+    byte[] serialize( T key );
+
+
+    /**
+     * Deserialize an element from a byte[]
+     * 
+     * @param in The incoming byte[]
+     * @return The deserialized element
+     */
+    T deserialize( byte[] in );
+}

Added: 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/IntSerializer.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/IntSerializer.java?rev=1367657&view=auto
==============================================================================
--- 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/IntSerializer.java
 (added)
+++ 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/IntSerializer.java
 Tue Jul 31 16:25:28 2012
@@ -0,0 +1,57 @@
+/*
+ *  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;
+
+
+/**
+ * The Integer serializer.
+ * 
+ * @author <a href="mailto:[email protected]";>Mavibot labs Project</a>
+ */
+public class IntSerializer implements ElementSerializer<Integer>
+{
+    /**
+     * {@inheritDoc}
+     */
+    public byte[] serialize( Integer element )
+    {
+        byte[] bytes = new byte[4];
+        int value = element.intValue();
+
+        bytes[0] = ( byte ) ( value >>> 24 );
+        bytes[1] = ( byte ) ( value >>> 16 );
+        bytes[2] = ( byte ) ( value >>> 8 );
+        bytes[3] = ( byte ) ( value );
+
+        return bytes;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public Integer deserialize( byte[] in )
+    {
+        return ( in[0] << 24 ) +
+            ( ( in[1] & 0xFF ) << 16 ) +
+            ( ( in[2] & 0xFF ) << 8 ) +
+            ( in[3] & 0xFF );
+    }
+}

Added: 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/Serializer.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/Serializer.java?rev=1367657&view=auto
==============================================================================
--- 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/Serializer.java
 (added)
+++ 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/Serializer.java
 Tue Jul 31 16:25:28 2012
@@ -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.mavibot.btree.serializer;
+
+
+/**
+ * This interface is used by implementations of the key and value serializers.
+ * 
+ * @param <K> The type for the keys
+ * @param <V> The type for the stored values
+ * 
+ * @author <a href="mailto:[email protected]";>Mavibot labs Project</a>
+ */
+public interface Serializer<K, V>
+{
+    /**
+     * Produce the byte[] representation of the key
+     * 
+     * @param key The key to serialize
+     * @return The byte[] containing the serialized key
+     */
+    byte[] serializeKey( K key );
+
+
+    /**
+     * Deserialize a key from a byte[]
+     * 
+     * @param in The incoming byte[]
+     * @return The deserialized key
+     */
+    K deserializeKey( byte[] in );
+
+
+    /**
+     * Produce the byte[] representation of the value
+     * 
+     * @param value The value to serialize
+     * @return The byte[] containing the serialized value
+     */
+    byte[] serializeValue( V value );
+
+
+    /**
+     * Deserialize a value from a byte[]
+     * 
+     * @param in The incoming byte[]
+     * @return The deserialized value
+     */
+    V deserializeValue( byte[] in );
+}

Added: 
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/IntSerializerTest.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/IntSerializerTest.java?rev=1367657&view=auto
==============================================================================
--- 
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/IntSerializerTest.java
 (added)
+++ 
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/IntSerializerTest.java
 Tue Jul 31 16:25:28 2012
@@ -0,0 +1,139 @@
+/*
+ *  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 org.junit.Test;
+
+
+/**
+ * Test the IntSerializer class
+ * 
+ * @author <a href="mailto:[email protected]";>Mavibot labs Project</a>
+ */
+public class IntSerializerTest
+{
+    private static IntSerializer serializer = new IntSerializer();
+
+
+    @Test
+    public void testIntSerializer()
+    {
+        int value = 0x00000000;
+        byte[] result = serializer.serialize( value );
+
+        assertEquals( ( byte ) 0x00, result[3] );
+        assertEquals( ( byte ) 0x00, result[2] );
+        assertEquals( ( byte ) 0x00, result[1] );
+        assertEquals( ( byte ) 0x00, result[0] );
+
+        assertEquals( value, serializer.deserialize( result ).intValue() );
+
+        value = 0x000000FF;
+        result = serializer.serialize( value );
+
+        assertEquals( ( byte ) 0xFF, result[3] );
+        assertEquals( ( byte ) 0x00, result[2] );
+        assertEquals( ( byte ) 0x00, result[1] );
+        assertEquals( ( byte ) 0x00, result[0] );
+
+        value = 0x00000100;
+        result = serializer.serialize( value );
+
+        assertEquals( value, serializer.deserialize( result ).intValue() );
+
+        assertEquals( ( byte ) 0x00, result[3] );
+        assertEquals( ( byte ) 0x01, result[2] );
+        assertEquals( ( byte ) 0x00, result[1] );
+        assertEquals( ( byte ) 0x00, result[0] );
+
+        value = 0x0000FFFF;
+        result = serializer.serialize( value );
+
+        assertEquals( value, serializer.deserialize( result ).intValue() );
+
+        assertEquals( ( byte ) 0xFF, result[3] );
+        assertEquals( ( byte ) 0xFF, result[2] );
+        assertEquals( ( byte ) 0x00, result[1] );
+        assertEquals( ( byte ) 0x00, result[0] );
+
+        value = 0x00010000;
+        result = serializer.serialize( value );
+
+        assertEquals( value, serializer.deserialize( result ).intValue() );
+
+        assertEquals( ( byte ) 0x00, result[3] );
+        assertEquals( ( byte ) 0x00, result[2] );
+        assertEquals( ( byte ) 0x01, result[1] );
+        assertEquals( ( byte ) 0x00, result[0] );
+
+        value = 0x00FFFFFF;
+        result = serializer.serialize( value );
+
+        assertEquals( value, serializer.deserialize( result ).intValue() );
+
+        assertEquals( ( byte ) 0xFF, result[3] );
+        assertEquals( ( byte ) 0xFF, result[2] );
+        assertEquals( ( byte ) 0xFF, result[1] );
+        assertEquals( ( byte ) 0x00, result[0] );
+
+        value = 0x01000000;
+        result = serializer.serialize( value );
+
+        assertEquals( value, serializer.deserialize( result ).intValue() );
+
+        assertEquals( ( byte ) 0x00, result[3] );
+        assertEquals( ( byte ) 0x00, result[2] );
+        assertEquals( ( byte ) 0x00, result[1] );
+        assertEquals( ( byte ) 0x01, result[0] );
+
+        value = 0x7FFFFFFF;
+        result = serializer.serialize( value );
+
+        assertEquals( value, serializer.deserialize( result ).intValue() );
+
+        assertEquals( ( byte ) 0x00FF, result[3] );
+        assertEquals( ( byte ) 0x00FF, result[2] );
+        assertEquals( ( byte ) 0x00FF, result[1] );
+        assertEquals( ( byte ) 0x7F, result[0] );
+
+        value = 0x80000000;
+        result = serializer.serialize( value );
+
+        assertEquals( value, serializer.deserialize( result ).intValue() );
+
+        assertEquals( ( byte ) 0x00, result[3] );
+        assertEquals( ( byte ) 0x00, result[2] );
+        assertEquals( ( byte ) 0x00, result[1] );
+        assertEquals( ( byte ) 0x80, result[0] );
+
+        value = 0xFFFFFFFF;
+        result = serializer.serialize( value );
+
+        assertEquals( value, serializer.deserialize( result ).intValue() );
+
+        assertEquals( ( byte ) 0xFF, result[3] );
+        assertEquals( ( byte ) 0xFF, result[2] );
+        assertEquals( ( byte ) 0xFF, result[1] );
+        assertEquals( ( byte ) 0xFF, result[0] );
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to