Modified: labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ByteSerializer.java URL: http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ByteSerializer.java?rev=1470325&r1=1470324&r2=1470325&view=diff ============================================================================== --- labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ByteSerializer.java (original) +++ labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ByteSerializer.java Sun Apr 21 17:58:23 2013 @@ -22,7 +22,6 @@ package org.apache.mavibot.btree.seriali import java.io.IOException; import java.nio.ByteBuffer; -import java.util.Comparator; import org.apache.mavibot.btree.comparator.ByteComparator; @@ -32,18 +31,14 @@ import org.apache.mavibot.btree.comparat * * @author <a href="mailto:[email protected]">Mavibot labs Project</a> */ -public class ByteSerializer implements ElementSerializer<Byte> +public class ByteSerializer extends AbstractElementSerializer<Byte> { - /** The associated comparator */ - private final Comparator<Byte> comparator; - - /** * Create a new instance of ByteSerializer */ public ByteSerializer() { - comparator = new ByteComparator(); + super( new ByteComparator() ); } @@ -53,90 +48,85 @@ public class ByteSerializer implements E public byte[] serialize( Byte element ) { byte[] bytes = new byte[1]; - bytes[0] = element.byteValue(); - return bytes; + return serialize( bytes, 0, element ); } /** - * A static method used to deserialize a Byte from a byte array. - * @param in The byte array containing the Byte - * @return A Byte + * Serialize a byte + * + * @param value the value to serialize + * @return The byte[] containing the serialized byte */ - public static Byte deserialize( byte[] in ) + public static byte[] serialize( byte value ) { - if ( ( in == null ) || ( in.length < 1 ) ) - { - throw new RuntimeException( "Cannot extract a Byte from a buffer with not enough bytes" ); - } + byte[] bytes = new byte[1]; - return in[0]; + return serialize( bytes, 0, value ); } /** - * {@inheritDoc} + * Serialize a byte + * + * @param buffer the Buffer that will contain the serialized value + * @param start the position in the buffer we will store the serialized byte + * @param value the value to serialize + * @return The byte[] containing the serialized byte */ - public Byte deserialize( ByteBuffer buffer ) throws IOException + public static byte[] serialize( byte[] buffer, int start, byte value ) { - return buffer.get(); + buffer[start] = value; + + return buffer; } /** - * {@inheritDoc} + * A static method used to deserialize a Byte from a byte array. + * @param in The byte array containing the Byte + * @return A Byte */ - public Byte deserialize( BufferHandler bufferHandler ) throws IOException + public static Byte deserialize( byte[] in ) { - byte[] in = bufferHandler.read( 1 ); - - return deserialize( in ); + return deserialize( in, 0 ); } /** - * {@inheritDoc} + * A static method used to deserialize a Byte from a byte array. + * @param in The byte array containing the Byte + * @param start the position in the byte[] we will deserialize the byte from + * @return A Byte */ - @Override - public int compare( Byte type1, Byte type2 ) + public static Byte deserialize( byte[] in, int start ) { - if ( type1 == type2 ) + if ( ( in == null ) || ( in.length < 1 + start ) ) { - return 0; + throw new RuntimeException( "Cannot extract a Byte from a buffer with not enough bytes" ); } - if ( type1 == null ) - { - if ( type2 == null ) - { - return 0; - } - else - { - return -1; - } - } - else - { - if ( type2 == null ) - { - return 1; - } - else - { - return type1.compareTo( type2 ); - } - } + return in[start]; + } + + + /** + * {@inheritDoc} + */ + public Byte deserialize( ByteBuffer buffer ) throws IOException + { + return buffer.get(); } /** * {@inheritDoc} */ - @Override - public Comparator<Byte> getComparator() + public Byte deserialize( BufferHandler bufferHandler ) throws IOException { - return comparator; + byte[] in = bufferHandler.read( 1 ); + + return deserialize( in ); } }
Modified: labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/CharSerializer.java URL: http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/CharSerializer.java?rev=1470325&r1=1470324&r2=1470325&view=diff ============================================================================== --- labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/CharSerializer.java (original) +++ labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/CharSerializer.java Sun Apr 21 17:58:23 2013 @@ -22,7 +22,6 @@ package org.apache.mavibot.btree.seriali import java.io.IOException; import java.nio.ByteBuffer; -import java.util.Comparator; import org.apache.mavibot.btree.comparator.CharComparator; @@ -32,18 +31,14 @@ import org.apache.mavibot.btree.comparat * * @author <a href="mailto:[email protected]">Mavibot labs Project</a> */ -public class CharSerializer implements ElementSerializer<Character> +public class CharSerializer extends AbstractElementSerializer<Character> { - /** The associated comparator */ - private final Comparator<Character> comparator; - - /** * Create a new instance of CharSerializer */ public CharSerializer() { - comparator = new CharComparator(); + super( new CharComparator() ); } @@ -53,94 +48,87 @@ public class CharSerializer implements E public byte[] serialize( Character element ) { byte[] bytes = new byte[2]; - char value = element.charValue(); - - bytes[0] = ( byte ) ( value >>> 8 ); - bytes[1] = ( byte ) ( value ); - return bytes; + return serialize( bytes, 0, element ); } /** - * A static method used to deserialize a Character from a byte array. - * @param in The byte array containing the Character - * @return A Character + * Serialize a char + * + * @param value the value to serialize + * @return The byte[] containing the serialized char */ - public static Character deserialize( byte[] in ) + public static byte[] serialize( char value ) { - if ( ( in == null ) || ( in.length < 2 ) ) - { - throw new RuntimeException( "Cannot extract a Character from a buffer with not enough bytes" ); - } + byte[] bytes = new byte[2]; - return Character.valueOf( ( char ) ( ( in[0] << 8 ) + - ( in[1] & 0xFF ) ) ); + return serialize( bytes, 0, value ); } /** - * {@inheritDoc} + * Serialize a char + * + * @param buffer the Buffer that will contain the serialized value + * @param start the position in the buffer we will store the serialized char + * @param value the value to serialize + * @return The byte[] containing the serialized char */ - public Character deserialize( ByteBuffer buffer ) throws IOException + public static byte[] serialize( byte[] buffer, int start, char value ) { - return buffer.getChar(); + buffer[start] = ( byte ) ( value >>> 8 ); + buffer[start + 1] = ( byte ) ( value ); + + return buffer; } /** - * {@inheritDoc} + * A static method used to deserialize a Character from a byte array. + * @param in The byte array containing the Character + * @return A Character */ - public Character deserialize( BufferHandler bufferHandler ) throws IOException + public static Character deserialize( byte[] in ) { - byte[] in = bufferHandler.read( 2 ); - - return deserialize( in ); + return deserialize( in, 0 ); } /** - * {@inheritDoc} + * A static method used to deserialize a Character from a byte array. + * @param in The byte array containing the Character + * @param start the position in the byte[] we will deserialize the char from + * @return A Character */ - @Override - public int compare( Character type1, Character type2 ) + public static Character deserialize( byte[] in, int start ) { - if ( type1 == type2 ) + if ( ( in == null ) || ( in.length < 2 + start ) ) { - return 0; + throw new RuntimeException( "Cannot extract a Character from a buffer with not enough bytes" ); } - if ( type1 == null ) - { - if ( type2 == null ) - { - return 0; - } - else - { - return -1; - } - } - else - { - if ( type2 == null ) - { - return 1; - } - else - { - return type1.compareTo( type2 ); - } - } + return Character.valueOf( ( char ) ( ( in[start] << 8 ) + + ( in[start + 1] & 0xFF ) ) ); } /** * {@inheritDoc} */ - @Override - public Comparator<Character> getComparator() + public Character deserialize( ByteBuffer buffer ) throws IOException { - return comparator; + return buffer.getChar(); + } + + + /** + * {@inheritDoc} + */ + public Character deserialize( BufferHandler bufferHandler ) throws IOException + { + byte[] in = bufferHandler.read( 2 ); + + return deserialize( in ); } } Modified: 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=1470325&r1=1470324&r2=1470325&view=diff ============================================================================== --- labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ElementSerializer.java (original) +++ labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ElementSerializer.java Sun Apr 21 17:58:23 2013 @@ -26,7 +26,7 @@ import java.util.Comparator; /** - * This interface is used by implementations of serializer, deserializr and comparator. + * This interface is used by implementations of serializer, deserializer and comparator. * * @param <T> The type for the element to serialize and compare * @@ -44,7 +44,7 @@ public interface ElementSerializer<T> /** - * Deserialize an element from a byte[] + * Deserialize an element from a BufferHandler * * @param bufferHandler The incoming bufferHandler * @return The deserialized element @@ -54,9 +54,9 @@ public interface ElementSerializer<T> /** - * Deserialize an element from a byte[] + * Deserialize an element from a ByteBuffer * - * @param buffer The incoming bufferHandler + * @param buffer The incoming ByteBuffer * @return The deserialized element * @throws IOException If the deserialization failed */ Modified: 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=1470325&r1=1470324&r2=1470325&view=diff ============================================================================== --- labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/IntSerializer.java (original) +++ labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/IntSerializer.java Sun Apr 21 17:58:23 2013 @@ -22,7 +22,6 @@ package org.apache.mavibot.btree.seriali import java.io.IOException; import java.nio.ByteBuffer; -import java.util.Comparator; import org.apache.mavibot.btree.comparator.IntComparator; @@ -32,62 +31,45 @@ import org.apache.mavibot.btree.comparat * * @author <a href="mailto:[email protected]">Mavibot labs Project</a> */ -public class IntSerializer implements ElementSerializer<Integer> +public class IntSerializer extends AbstractElementSerializer<Integer> { - /** The associated comparator */ - private final Comparator<Integer> comparator; - - /** * Create a new instance of IntSerializer */ public IntSerializer() { - comparator = new IntComparator(); + super( new IntComparator() ); } /** - * {@inheritDoc} + * A static method used to deserialize an Integer from a byte array. + * @param in The byte array containing the Integer + * @return An Integer */ - public byte[] serialize( Integer element ) + public static Integer deserialize( byte[] in ) { - return serialize( element.intValue() ); + return deserialize( in, 0 ); } /** * A static method used to deserialize an Integer from a byte array. * @param in The byte array containing the Integer + * @param start the position in the byte[] we will deserialize the int from * @return An Integer */ - public static Integer deserialize( byte[] in ) + public static Integer deserialize( byte[] in, int start ) { - if ( ( in == null ) || ( in.length < 4 ) ) + if ( ( in == null ) || ( in.length < 4 + start ) ) { throw new RuntimeException( "Cannot extract a Integer from a buffer with not enough bytes" ); } - return ( in[0] << 24 ) + - ( ( in[1] & 0xFF ) << 16 ) + - ( ( in[2] & 0xFF ) << 8 ) + - ( in[3] & 0xFF ); - } - - - /** - * {@inheritDoc} - */ - public static byte[] serialize( int value ) - { - byte[] bytes = new byte[4]; - - bytes[0] = ( byte ) ( value >>> 24 ); - bytes[1] = ( byte ) ( value >>> 16 ); - bytes[2] = ( byte ) ( value >>> 8 ); - bytes[3] = ( byte ) ( value ); - - return bytes; + return ( in[start] << 24 ) + + ( ( in[start + 1] & 0xFF ) << 16 ) + + ( ( in[start + 2] & 0xFF ) << 8 ) + + ( in[start + 3] & 0xFF ); } @@ -114,45 +96,41 @@ public class IntSerializer implements El /** * {@inheritDoc} */ - @Override - public int compare( Integer type1, Integer type2 ) + public byte[] serialize( Integer element ) { - if ( type1 == type2 ) - { - return 0; - } + return serialize( element.intValue() ); + } - if ( type1 == null ) - { - if ( type2 == null ) - { - return 0; - } - else - { - return -1; - } - } - else - { - if ( type2 == null ) - { - return 1; - } - else - { - return type1.compareTo( type2 ); - } - } + + /** + * Serialize an int + * + * @param value the value to serialize + * @return The byte[] containing the serialized int + */ + public static byte[] serialize( int value ) + { + byte[] bytes = new byte[4]; + + return serialize( bytes, 0, value ); } /** - * {@inheritDoc} + * Serialize an int + * + * @param buffer the Buffer that will contain the serialized value + * @param start the position in the buffer we will store the serialized int + * @param value the value to serialize + * @return The byte[] containing the serialized int */ - @Override - public Comparator<Integer> getComparator() + public static byte[] serialize( byte[] buffer, int start, int value ) { - return comparator; + buffer[start] = ( byte ) ( value >>> 24 ); + buffer[start + 1] = ( byte ) ( value >>> 16 ); + buffer[start + 2] = ( byte ) ( value >>> 8 ); + buffer[start + 3] = ( byte ) ( value ); + + return buffer; } } Modified: labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/LongSerializer.java URL: http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/LongSerializer.java?rev=1470325&r1=1470324&r2=1470325&view=diff ============================================================================== --- labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/LongSerializer.java (original) +++ labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/LongSerializer.java Sun Apr 21 17:58:23 2013 @@ -22,7 +22,6 @@ package org.apache.mavibot.btree.seriali import java.io.IOException; import java.nio.ByteBuffer; -import java.util.Comparator; import org.apache.mavibot.btree.comparator.LongComparator; @@ -32,18 +31,14 @@ import org.apache.mavibot.btree.comparat * * @author <a href="mailto:[email protected]">Mavibot labs Project</a> */ -public class LongSerializer implements ElementSerializer<Long> +public class LongSerializer extends AbstractElementSerializer<Long> { - /** The associated comparator */ - private final Comparator<Long> comparator; - - /** * Create a new instance of LongSerializer */ public LongSerializer() { - comparator = new LongComparator(); + super( new LongComparator() ); } @@ -57,47 +52,75 @@ public class LongSerializer implements E /** - * A static method used to derialize a long into a byte array. - * @param in The byte array containing the long - * @return A long + * Serialize an long + * + * @param value the value to serialize + * @return The byte[] containing the serialized long */ public static byte[] serialize( long value ) { byte[] bytes = new byte[8]; - bytes[0] = ( byte ) ( value >>> 56 ); - bytes[1] = ( byte ) ( value >>> 48 ); - bytes[2] = ( byte ) ( value >>> 40 ); - bytes[3] = ( byte ) ( value >>> 32 ); - bytes[4] = ( byte ) ( value >>> 24 ); - bytes[5] = ( byte ) ( value >>> 16 ); - bytes[6] = ( byte ) ( value >>> 8 ); - bytes[7] = ( byte ) ( value ); + return serialize( bytes, 0, value ); + } - return bytes; + + /** + * Serialize an long + * + * @param buffer the Buffer that will contain the serialized value + * @param start the position in the buffer we will store the serialized long + * @param value the value to serialize + * @return The byte[] containing the serialized long + */ + public static byte[] serialize( byte[] buffer, int start, long value ) + { + buffer[start] = ( byte ) ( value >>> 56 ); + buffer[start + 1] = ( byte ) ( value >>> 48 ); + buffer[start + 2] = ( byte ) ( value >>> 40 ); + buffer[start + 3] = ( byte ) ( value >>> 32 ); + buffer[start + 4] = ( byte ) ( value >>> 24 ); + buffer[start + 5] = ( byte ) ( value >>> 16 ); + buffer[start + 6] = ( byte ) ( value >>> 8 ); + buffer[start + 7] = ( byte ) ( value ); + + return buffer; } /** * A static method used to deserialize a Long from a byte array. * @param in The byte array containing the Long + * @param start the position in the byte[] we will deserialize the long from * @return A Long */ public static Long deserialize( byte[] in ) { - if ( ( in == null ) || ( in.length < 8 ) ) + return deserialize( in, 0 ); + } + + + /** + * A static method used to deserialize an Integer from a byte array. + * @param in The byte array containing the Integer + * @param start the position in the byte[] we will deserialize the long from + * @return An Integer + */ + public static Long deserialize( byte[] in, int start ) + { + if ( ( in == null ) || ( in.length < 8 + start ) ) { throw new RuntimeException( "Cannot extract a Long from a buffer with not enough bytes" ); } - long result = ( ( long ) in[0] << 56 ) + - ( ( in[1] & 0xFFL ) << 48 ) + - ( ( in[2] & 0xFFL ) << 40 ) + - ( ( in[3] & 0xFFL ) << 32 ) + - ( ( in[4] & 0xFFL ) << 24 ) + - ( ( in[5] & 0xFFL ) << 16 ) + - ( ( in[6] & 0xFFL ) << 8 ) + - ( in[7] & 0xFFL ); + long result = ( ( long ) in[start] << 56 ) + + ( ( in[start + 1] & 0xFFL ) << 48 ) + + ( ( in[start + 2] & 0xFFL ) << 40 ) + + ( ( in[start + 3] & 0xFFL ) << 32 ) + + ( ( in[start + 4] & 0xFFL ) << 24 ) + + ( ( in[start + 5] & 0xFFL ) << 16 ) + + ( ( in[start + 6] & 0xFFL ) << 8 ) + + ( in[start + 7] & 0xFFL ); return result; } @@ -121,50 +144,4 @@ public class LongSerializer implements E { return buffer.getLong(); } - - - /** - * {@inheritDoc} - */ - @Override - public int compare( Long type1, Long type2 ) - { - if ( type1 == type2 ) - { - return 0; - } - - if ( type1 == null ) - { - if ( type2 == null ) - { - return 0; - } - else - { - return -1; - } - } - else - { - if ( type2 == null ) - { - return 1; - } - else - { - return type1.compareTo( type2 ); - } - } - } - - - /** - * {@inheritDoc} - */ - @Override - public Comparator<Long> getComparator() - { - return comparator; - } } Modified: labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ShortSerializer.java URL: http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ShortSerializer.java?rev=1470325&r1=1470324&r2=1470325&view=diff ============================================================================== --- labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ShortSerializer.java (original) +++ labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ShortSerializer.java Sun Apr 21 17:58:23 2013 @@ -22,7 +22,6 @@ package org.apache.mavibot.btree.seriali import java.io.IOException; import java.nio.ByteBuffer; -import java.util.Comparator; import org.apache.mavibot.btree.comparator.ShortComparator; @@ -32,18 +31,14 @@ import org.apache.mavibot.btree.comparat * * @author <a href="mailto:[email protected]">Mavibot labs Project</a> */ -public class ShortSerializer implements ElementSerializer<Short> +public class ShortSerializer extends AbstractElementSerializer<Short> { - /** The associated comparator */ - private final Comparator<Short> comparator; - - /** * Create a new instance of ShortSerializer */ public ShortSerializer() { - comparator = new ShortComparator(); + super( new ShortComparator() ); } @@ -53,93 +48,86 @@ public class ShortSerializer implements public byte[] serialize( Short element ) { byte[] bytes = new byte[2]; - short value = element.shortValue(); - - bytes[0] = ( byte ) ( value >>> 8 ); - bytes[1] = ( byte ) ( value ); - return bytes; + return serialize( bytes, 0, element ); } /** - * A static method used to deserialize a Short from a byte array. - * @param in The byte array containing the Short - * @return A Short + * Serialize a short + * + * @param value the value to serialize + * @return The byte[] containing the serialized short */ - public static Short deserialize( byte[] in ) + public static byte[] serialize( short value ) { - if ( ( in == null ) || ( in.length < 2 ) ) - { - throw new RuntimeException( "Cannot extract a Short from a buffer with not enough bytes" ); - } + byte[] bytes = new byte[2]; - return ( short ) ( ( in[0] << 8 ) + ( in[1] & 0xFF ) ); + return serialize( bytes, 0, value ); } /** - * {@inheritDoc} + * Serialize a short + * + * @param buffer the Buffer that will contain the serialized value + * @param start the position in the buffer we will store the serialized short + * @param value the value to serialize + * @return The byte[] containing the serialized short */ - public Short deserialize( ByteBuffer buffer ) throws IOException + public static byte[] serialize( byte[] buffer, int start, short value ) { - return buffer.getShort(); + buffer[start] = ( byte ) ( value >>> 8 ); + buffer[start + 1] = ( byte ) ( value ); + + return buffer; } /** - * {@inheritDoc} + * A static method used to deserialize a Short from a byte array. + * @param in The byte array containing the Short + * @return A Short */ - public Short deserialize( BufferHandler bufferHandler ) throws IOException + public static Short deserialize( byte[] in ) { - byte[] in = bufferHandler.read( 2 ); - - return deserialize( in ); + return deserialize( in, 0 ); } /** - * {@inheritDoc} + * A static method used to deserialize a Short from a byte array. + * @param in The byte array containing the Short + * @param start the position in the byte[] we will deserialize the short from + * @return A Short */ - @Override - public int compare( Short type1, Short type2 ) + public static Short deserialize( byte[] in, int start ) { - if ( type1 == type2 ) + if ( ( in == null ) || ( in.length < 2 + start ) ) { - return 0; + throw new RuntimeException( "Cannot extract a Short from a buffer with not enough bytes" ); } - if ( type1 == null ) - { - if ( type2 == null ) - { - return 0; - } - else - { - return -1; - } - } - else - { - if ( type2 == null ) - { - return 1; - } - else - { - return type1.compareTo( type2 ); - } - } + return ( short ) ( ( in[start] << 8 ) + ( in[start + 1] & 0xFF ) ); } /** * {@inheritDoc} */ - @Override - public Comparator<Short> getComparator() + public Short deserialize( ByteBuffer buffer ) throws IOException { - return comparator; + return buffer.getShort(); + } + + + /** + * {@inheritDoc} + */ + public Short deserialize( BufferHandler bufferHandler ) throws IOException + { + byte[] in = bufferHandler.read( 2 ); + + return deserialize( in ); } } Modified: labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/StringSerializer.java URL: http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/StringSerializer.java?rev=1470325&r1=1470324&r2=1470325&view=diff ============================================================================== --- labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/StringSerializer.java (original) +++ labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/StringSerializer.java Sun Apr 21 17:58:23 2013 @@ -23,7 +23,6 @@ package org.apache.mavibot.btree.seriali import java.io.IOException; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; -import java.util.Comparator; import org.apache.mavibot.btree.comparator.StringComparator; import org.apache.mavibot.btree.util.Strings; @@ -34,18 +33,108 @@ import org.apache.mavibot.btree.util.Str * * @author <a href="mailto:[email protected]">Mavibot labs Project</a> */ -public class StringSerializer implements ElementSerializer<String> +public class StringSerializer extends AbstractElementSerializer<String> { - /** The associated comparator */ - private final Comparator<String> comparator; - - /** * Create a new instance of StringSerializer */ public StringSerializer() { - comparator = new StringComparator(); + super( new StringComparator() ); + } + + + /** + * A static method used to deserialize a String from a byte array. + * @param in The byte array containing the String + * @return A String + */ + public static String deserialize( byte[] in ) + { + return deserialize( in, 0 ); + } + + + /** + * A static method used to deserialize a String from a byte array. + * @param in The byte array containing the String + * @return A String + */ + public static String deserialize( byte[] in, int start ) + { + int length = IntSerializer.deserialize( in, start ); + + if ( length == 0xFFFFFFFF ) + { + return null; + } + + if ( in.length < length + 4 + start ) + { + throw new RuntimeException( "Cannot extract a String from a buffer with not enough bytes" ); + } + + return Strings.utf8ToString( in, start + 4, length ); + } + + + /** + * Serialize a String. We store the length on 4 bytes, then the String + * + * @param buffer the Buffer that will contain the serialized value + * @param start the position in the buffer we will store the serialized String + * @param value the value to serialize + * @return The byte[] containing the serialized String + */ + public static byte[] serialize( byte[] buffer, int start, String element ) + { + int len = -1; + + if ( element != null ) + { + len = element.length(); + } + + switch ( len ) + { + case 0: + buffer[start] = 0x00; + buffer[start + 1] = 0x00; + buffer[start + 2] = 0x00; + buffer[start + 3] = 0x00; + + break; + + case -1: + buffer[start] = ( byte ) 0xFF; + buffer[start + 1] = ( byte ) 0xFF; + buffer[start + 2] = ( byte ) 0xFF; + buffer[start + 3] = ( byte ) 0xFF; + + break; + + default: + try + { + byte[] strBytes = element.getBytes( "UTF-8" ); + + buffer = new byte[strBytes.length + 4]; + + System.arraycopy( strBytes, 0, buffer, 4, strBytes.length ); + + buffer[start] = ( byte ) ( strBytes.length >>> 24 ); + buffer[start + 1] = ( byte ) ( strBytes.length >>> 16 ); + buffer[start + 2] = ( byte ) ( strBytes.length >>> 8 ); + buffer[start + 3] = ( byte ) ( strBytes.length ); + } + catch ( UnsupportedEncodingException uee ) + { + // if this happens something is really strange + throw new RuntimeException( uee ); + } + } + + return buffer; } @@ -192,14 +281,4 @@ public class StringSerializer implements } } } - - - /** - * {@inheritDoc} - */ - @Override - public Comparator<String> getComparator() - { - return comparator; - } } Modified: labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/BTreeConfigurationTest.java URL: http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/BTreeConfigurationTest.java?rev=1470325&r1=1470324&r2=1470325&view=diff ============================================================================== --- labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/BTreeConfigurationTest.java (original) +++ labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/BTreeConfigurationTest.java Sun Apr 21 17:58:23 2013 @@ -114,6 +114,7 @@ public class BTreeConfigurationTest public void testConfigurationBasic() throws IOException, KeyNotFoundException { BTreeConfiguration<Integer, String> config = new BTreeConfiguration<Integer, String>(); + config.setName( "basic" ); config.setPageSize( 32 ); config.setSerializers( new IntSerializer(), new StringSerializer() ); @@ -159,7 +160,7 @@ public class BTreeConfigurationTest config.setSerializers( new IntSerializer(), new StringSerializer() ); config.setFilePath( parent ); - config.setFileName( "mavibot" ); + config.setName( "mavibot" ); // Create the BTree BTree<Integer, String> btree = new BTree<Integer, String>( config ); Modified: labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/BTreeFlushTest.java URL: http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/BTreeFlushTest.java?rev=1470325&r1=1470324&r2=1470325&view=diff ============================================================================== --- labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/BTreeFlushTest.java (original) +++ labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/BTreeFlushTest.java Sun Apr 21 17:58:23 2013 @@ -215,17 +215,17 @@ public class BTreeFlushTest // Create the file, it will be deleted on exit File tempFile = File.createTempFile( "testFlush", null ); String path = tempFile.getParent(); - String fileName = "mavibot"; tempFile.delete(); - File journal = new File( path, fileName + BTree.JOURNAL_SUFFIX ); - File data = new File( path, fileName + BTree.DATA_SUFFIX ); + BTree<Integer, String> btree = new BTree<Integer, String>( "test", path, new IntSerializer(), new StringSerializer() ); + btree.setName( "flush" ); + btree.setPageSize( 8 ); + + File journal = btree.getJournal(); + File data = btree.getFile(); + try { - BTree<Integer, String> btree = new BTree<Integer, String>( "test", path, fileName, new IntSerializer(), - new StringSerializer() ); - btree.setPageSize( 8 ); - // Inject the values for ( int value : sortedValues ) { @@ -244,8 +244,7 @@ public class BTreeFlushTest assertEquals( 0, journal.length() ); // Load the data into a new tree - BTree<Integer, String> btreeLoaded = new BTree<Integer, String>( "test", path, fileName, - new IntSerializer(), + BTree<Integer, String> btreeLoaded = new BTree<Integer, String>( "test", path, new IntSerializer(), new StringSerializer() ); btree.setPageSize( 8 ); @@ -288,7 +287,6 @@ public class BTreeFlushTest BTree<Long, String> btree = new BTree<Long, String>( "test", dataFile.getParent(), - dataFile.getName(), new LongSerializer(), new StringSerializer() ); btree.setPageSize( 32 ); Modified: labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/InMemoryBTreeTest.java URL: http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/InMemoryBTreeTest.java?rev=1470325&r1=1470324&r2=1470325&view=diff ============================================================================== --- labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/InMemoryBTreeTest.java (original) +++ labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/InMemoryBTreeTest.java Sun Apr 21 17:58:23 2013 @@ -625,7 +625,7 @@ public class InMemoryBTreeTest // Browse starting at position 10 int pos = 10; - Cursor<Integer, String> cursor = btree.browse( sortedValues[pos] ); + Cursor<Integer, String> cursor = btree.browseFrom( sortedValues[pos] ); while ( cursor.hasNext() ) { @@ -641,7 +641,7 @@ public class InMemoryBTreeTest cursor.close(); // Now, start on a non existing key (7) - cursor = btree.browse( 7 ); + cursor = btree.browseFrom( 7 ); // We should start reading values superior to 7, so value 8 at position 6 in the array pos = 6; @@ -709,7 +709,7 @@ public class InMemoryBTreeTest // Browse starting at position 10 int pos = 10; - Cursor<Integer, String> cursor = btree.browse( sortedValues[pos] ); + Cursor<Integer, String> cursor = btree.browseFrom( sortedValues[pos] ); while ( cursor.hasPrev() ) { @@ -726,7 +726,7 @@ public class InMemoryBTreeTest cursor.close(); // Now, start on a non existing key (7) - cursor = btree.browse( 7 ); + cursor = btree.browseFrom( 7 ); // We should start reading values superior to 7, so value 8 at position 6 in the array pos = 6; @@ -793,7 +793,7 @@ public class InMemoryBTreeTest } // Start to browse in the middle - Cursor<Integer, String> cursor = btree.browse( 8 ); + Cursor<Integer, String> cursor = btree.browseFrom( 8 ); assertTrue( cursor.hasNext() ); @@ -812,6 +812,8 @@ public class InMemoryBTreeTest // get 12 (now, we must have gone through at least 2 pages) assertEquals( 12, cursor.next().getKey().intValue() ); + assertTrue( cursor.hasPrev() ); + // Lets go backward. We should get the same value, as the next() call have incremented the counter assertEquals( 12, cursor.prev().getKey().intValue() ); @@ -904,11 +906,11 @@ public class InMemoryBTreeTest for ( int i = 1; i < 21; i++ ) { - assertTrue( btree.exist( 5 ) ); + assertTrue( btree.hasKey( 5 ) ); } - assertFalse( btree.exist( 0 ) ); - assertFalse( btree.exist( 21 ) ); + assertFalse( btree.hasKey( 0 ) ); + assertFalse( btree.hasKey( 21 ) ); } @@ -1002,6 +1004,34 @@ public class InMemoryBTreeTest } + /** + * Test the browse method with a non existing key + * @throws Exception + */ + @Test + public void testBrowseNonExistingKey() throws Exception + { + // Create a BTree with pages containing 8 elements + BTree<Integer, String> btree = new BTree<Integer, String>( "test", new IntSerializer(), new StringSerializer() ); + btree.setPageSize( 8 ); + for ( int i = 0; i < 11; i++ ) + { + btree.insert( i, String.valueOf( i ) ); + } + + for ( int i = 0; i < 11; i++ ) + { + assertNotNull( btree.get( i ) ); + } + + assertTrue( btree.hasKey( 8 ) ); + assertFalse( btree.hasKey( 11 ) ); + + Cursor<Integer, String> cursor = btree.browseFrom( 11 ); + assertFalse( cursor.hasNext() ); + } + + private Page<Integer, String> createLeaf( BTree<Integer, String> btree, long revision, Tuple<Integer, String>... tuples ) { @@ -1679,7 +1709,7 @@ public class InMemoryBTreeTest // Adding an element with a null value btree.insert( 100, null ); - assertTrue( btree.exist( 100 ) ); + assertTrue( btree.hasKey( 100 ) ); try { @@ -1804,4 +1834,103 @@ public class InMemoryBTreeTest // expected } } + + /** + * Test a browse forward and backward + */ + @Test + public void testBrowseForwardBackwardExtremes() throws Exception + { + // Create a BTree with pages containing 4 elements + BTree<Integer, String> btree = new BTree<Integer, String>( "test", new IntSerializer(), new StringSerializer() ); + btree.setPageSize( 4 ); + + for ( int i = 8; i < 13; i++ ) + { + String strValue = "V" + i; + btree.insert( i, strValue ); + } + + // Start to browse in the middle + Cursor<Integer, String> cursor = btree.browseFrom( 8 ); + + assertTrue( cursor.hasNext() ); + + // Get 8 + assertEquals( 8, cursor.next().getKey().intValue() ); + + // get 9 + assertEquals( 9, cursor.next().getKey().intValue() ); + + // get 10 + assertEquals( 10, cursor.next().getKey().intValue() ); + + // get 11 + assertEquals( 11, cursor.next().getKey().intValue() ); + + // get 12 (now, we must have gone through at least 2 pages) + assertEquals( 12, cursor.next().getKey().intValue() ); + + assertFalse( cursor.hasNext() ); + assertTrue( cursor.hasPrev() ); + + // Lets go backward. We should get the same value, as the next() call have incremented the counter + assertEquals( 12, cursor.prev().getKey().intValue() ); + + // Get 11 + assertEquals( 11, cursor.prev().getKey().intValue() ); + + // Get 10 + assertEquals( 10, cursor.prev().getKey().intValue() ); + + // Get 9 + assertEquals( 9, cursor.prev().getKey().intValue() ); + + // Get 8 + assertEquals( 8, cursor.prev().getKey().intValue() ); + + assertFalse(cursor.hasPrev()); + assertTrue(cursor.hasNext()); + + cursor.close(); + btree.close(); + } + + @Test + public void testNextAfterPrev() throws Exception + { + IntSerializer serializer = new IntSerializer(); + + BTreeConfiguration<Integer, Integer> config = new BTreeConfiguration<Integer, Integer>(); + config.setName( "master" ); + config.setPageSize( 4 ); + config.setSerializers( serializer, serializer ); + BTree<Integer, Integer> btree = new BTree<Integer, Integer>( config ); + + int i = 7; + for ( int k=0; k < i; k++ ) + { + btree.insert( k, k ); + } + + // 3 is the last element of the first leaf + Cursor<Integer, Integer> cursor = btree.browseFrom(4); + + assertTrue( cursor.hasNext() ); + Tuple<Integer, Integer> tuple = cursor.next(); + assertEquals( Integer.valueOf( 4 ), tuple.getKey() ); + assertEquals( Integer.valueOf( 4 ), tuple.getValue() ); + + assertTrue( cursor.hasPrev() ); + tuple = cursor.prev(); + assertEquals( Integer.valueOf( 4 ), tuple.getKey() ); + assertEquals( Integer.valueOf( 4 ), tuple.getValue() ); + + assertTrue( cursor.hasNext() ); + tuple = cursor.next(); + assertEquals( Integer.valueOf( 4 ), tuple.getKey() ); + assertEquals( Integer.valueOf( 4 ), tuple.getValue() ); + cursor.close(); + } + } Modified: labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/LeafTest.java URL: http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/LeafTest.java?rev=1470325&r1=1470324&r2=1470325&view=diff ============================================================================== --- labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/LeafTest.java (original) +++ labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/LeafTest.java Sun Apr 21 17:58:23 2013 @@ -83,7 +83,7 @@ public class LeafTest { Leaf<Long, String> leaf = new Leaf<Long, String>( btree ); - DeleteResult<Long, String> result = leaf.delete( 1L, 1L, null, -1 ); + DeleteResult<Long, String> result = leaf.delete( 1L, 1L, null, null, -1 ); assertEquals( NotPresentResult.NOT_PRESENT, result ); } @@ -102,7 +102,7 @@ public class LeafTest leaf = insert( leaf, 3L, "v3" ); leaf = insert( leaf, 4L, "v4" ); - DeleteResult<Long, String> result = leaf.delete( 2L, 5L, null, -1 ); + DeleteResult<Long, String> result = leaf.delete( 2L, 5L, null, null, -1 ); assertEquals( NotPresentResult.NOT_PRESENT, result ); } @@ -121,7 +121,7 @@ public class LeafTest leaf = insert( leaf, 3L, "v3" ); leaf = insert( leaf, 4L, "v4" ); - DeleteResult<Long, String> result = leaf.delete( 4L, 3L, null, -1 ); + DeleteResult<Long, String> result = leaf.delete( 4L, 3L, null, null, -1 ); assertTrue( result instanceof RemoveResult ); @@ -168,7 +168,7 @@ public class LeafTest leaf = insert( leaf, 3L, "v3" ); leaf = insert( leaf, 4L, "v4" ); - DeleteResult<Long, String> result = leaf.delete( 4L, 1L, null, -1 ); + DeleteResult<Long, String> result = leaf.delete( 4L, 1L, null, null, -1 ); assertTrue( result instanceof RemoveResult ); @@ -248,7 +248,7 @@ public class LeafTest parent.keys[1] = 10L; // Now, delete the element from the target page - DeleteResult<Long, String> result = target.delete( 2L, 7L, parent, 1 ); + DeleteResult<Long, String> result = target.delete( 2L, 7L, null, parent, 1 ); assertTrue( result instanceof BorrowedFromLeftResult ); @@ -321,7 +321,7 @@ public class LeafTest parent.keys[1] = 10L; // Now, delete the element from the target page - DeleteResult<Long, String> result = target.delete( 2L, 7L, parent, 1 ); + DeleteResult<Long, String> result = target.delete( 2L, 7L, null, parent, 1 ); assertTrue( result instanceof BorrowedFromRightResult ); @@ -394,7 +394,7 @@ public class LeafTest parent.keys[1] = 9L; // Now, delete the element from the target page - DeleteResult<Long, String> result = target.delete( 2L, 7L, parent, 1 ); + DeleteResult<Long, String> result = target.delete( 2L, 7L, null, parent, 1 ); assertTrue( result instanceof MergedWithSiblingResult ); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
