directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kayyag...@apache.org
Subject svn commit: r1510115 [8/13] - in /directory/mavibot/trunk: ./ mavibot/ mavibot/src/main/java/org/apache/directory/ mavibot/src/main/java/org/apache/directory/mavibot/ mavibot/src/main/java/org/apache/directory/mavibot/btree/ mavibot/src/main/java/org/a...
Date Sun, 04 Aug 2013 09:22:58 GMT
Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/AbstractElementSerializer.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/AbstractElementSerializer.java?rev=1510115&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/AbstractElementSerializer.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/AbstractElementSerializer.java Sun Aug  4 09:22:56 2013
@@ -0,0 +1,65 @@
+/*
+ *  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.directory.mavibot.btree.serializer;
+
+
+import java.util.Comparator;
+
+
+/**
+ * An abstract ElementSerializer that implements comon methods
+ * 
+ * @param <T> The type for the element to serialize and compare
+ *
+ * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
+ */
+public abstract class AbstractElementSerializer<T> implements ElementSerializer<T>
+{
+    /** The associated comparator */
+    private final Comparator<T> comparator;
+
+
+    /**
+     * Create a new instance of BooleanSerializer
+     */
+    public AbstractElementSerializer( Comparator<T> comparator )
+    {
+        this.comparator = comparator;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public int compare( T type1, T type2 )
+    {
+        return comparator.compare( type1, type2 );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public Comparator<T> getComparator()
+    {
+        return comparator;
+    }
+}

Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/BooleanSerializer.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/BooleanSerializer.java?rev=1510115&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/BooleanSerializer.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/BooleanSerializer.java Sun Aug  4 09:22:56 2013
@@ -0,0 +1,135 @@
+/*
+ *  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.directory.mavibot.btree.serializer;
+
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import org.apache.directory.mavibot.btree.comparator.BooleanComparator;
+
+
+/**
+ * The Boolean serializer.
+ * 
+ * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
+ */
+public class BooleanSerializer extends AbstractElementSerializer<Boolean>
+{
+    /**
+     * Create a new instance of BooleanSerializer
+     */
+    public BooleanSerializer()
+    {
+        super( new BooleanComparator() );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public byte[] serialize( Boolean element )
+    {
+        byte[] bytes = new byte[1];
+
+        return serialize( bytes, 0, element );
+    }
+
+
+    /**
+     * Serialize a boolean
+     * 
+     * @param value the value to serialize
+     * @return The byte[] containing the serialized boolean
+     */
+    public static byte[] serialize( boolean element )
+    {
+        byte[] bytes = new byte[1];
+
+        return serialize( bytes, 0, element );
+    }
+
+
+    /**
+     * Serialize a boolean
+     * 
+     * @param buffer the Buffer that will contain the serialized value
+     * @param start the position in the buffer we will store the serialized boolean
+     * @param value the value to serialize
+     * @return The byte[] containing the serialized boolean
+     */
+    public static byte[] serialize( byte[] buffer, int start, boolean element )
+    {
+        buffer[start] = element ? ( byte ) 0x01 : ( byte ) 0x00;
+
+        return buffer;
+    }
+
+
+    /**
+     * A static method used to deserialize a Boolean from a byte array.
+     * 
+     * @param in The byte array containing the boolean
+     * @return A boolean
+     */
+    public static Boolean deserialize( byte[] in )
+    {
+        return deserialize( in, 0 );
+    }
+
+
+    /**
+     * A static method used to deserialize a Boolean from a byte array.
+     * 
+     * @param in The byte array containing the boolean
+     * @param start the position in the byte[] we will deserialize the boolean from
+     * @return A boolean
+     */
+    public static Boolean deserialize( byte[] in, int start )
+    {
+        if ( ( in == null ) || ( in.length < 1 + start ) )
+        {
+            throw new RuntimeException( "Cannot extract a Boolean from a buffer with not enough bytes" );
+        }
+
+        return in[start] == 0x01;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public Boolean deserialize( ByteBuffer buffer ) throws IOException
+    {
+        return buffer.get() != 0x00;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public Boolean deserialize( BufferHandler bufferHandler ) throws IOException
+    {
+        byte[] in = bufferHandler.read( 1 );
+
+        return deserialize( in );
+    }
+}

Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/BufferHandler.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/BufferHandler.java?rev=1510115&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/BufferHandler.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/BufferHandler.java Sun Aug  4 09:22:56 2013
@@ -0,0 +1,144 @@
+/*
+ *  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.directory.mavibot.btree.serializer;
+
+
+import java.io.EOFException;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+
+
+/**
+ * A class used to hide the buffer read from the underlying file.
+ * 
+ * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
+ */
+public class BufferHandler
+{
+    /** The channel we read bytes from */
+    private FileChannel channel;
+
+    /** The buffer containing the bytes we read from the channel */
+    private ByteBuffer buffer;
+
+
+    /**
+     * Create a new BufferHandler 
+     * @param buffer The buffer used to transfer data
+     */
+    public BufferHandler( byte[] buffer )
+    {
+        this.buffer = ByteBuffer.allocate( buffer.length );
+        this.buffer.put( buffer );
+        this.buffer.flip();
+    }
+
+
+    /**
+     * Create a new BufferHandler 
+     * @param channel The channel to read
+     * @param buffer The buffer used to transfer data
+     */
+    public BufferHandler( FileChannel channel, ByteBuffer buffer )
+    {
+        this.channel = channel;
+        this.buffer = buffer;
+
+        try
+        {
+            // Initial read
+            channel.read( buffer );
+            buffer.flip();
+        }
+        catch ( IOException ioe )
+        {
+
+        }
+    }
+
+
+    public byte[] getBuffer()
+    {
+        byte[] bytes = new byte[buffer.capacity()];
+
+        buffer.get( bytes );
+
+        return bytes;
+    }
+
+
+    /**
+     * Read a buffer containing the given number of bytes
+     * @param len The number of bytes to read
+     * @return
+     */
+    public byte[] read( int len ) throws IOException
+    {
+        byte[] result = new byte[len];
+
+        if ( len <= buffer.remaining() )
+        {
+            buffer.get( result );
+
+            return result;
+        }
+
+        int requested = len;
+        int position = 0;
+
+        while ( requested != 0 )
+        {
+            int nbRemainingRead = buffer.limit() - buffer.position();
+
+            if ( nbRemainingRead > requested )
+            {
+                buffer.get( result, position, requested );
+                break;
+            }
+            else
+            {
+                System.arraycopy( buffer.array(), buffer.position(), result, position, nbRemainingRead );
+                position += nbRemainingRead;
+            }
+
+            buffer.clear();
+
+            if ( channel != null )
+            {
+                int nbReads = channel.read( buffer );
+                buffer.flip();
+
+                if ( nbReads <= 0 )
+                {
+                    throw new EOFException();
+                }
+            }
+            else
+            {
+                throw new IOException( "Not enough bytes in the buffer" );
+            }
+
+            requested -= nbRemainingRead;
+        }
+
+        return result;
+    }
+}

Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/ByteArraySerializer.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/ByteArraySerializer.java?rev=1510115&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/ByteArraySerializer.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/ByteArraySerializer.java Sun Aug  4 09:22:56 2013
@@ -0,0 +1,273 @@
+/*
+ *  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.directory.mavibot.btree.serializer;
+
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Comparator;
+
+import org.apache.directory.mavibot.btree.comparator.ByteArrayComparator;
+
+
+/**
+ * A serializer for a byte[].
+ * 
+ * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
+ */
+public class ByteArraySerializer extends AbstractElementSerializer<byte[]>
+{
+    /**
+     * Create a new instance of ByteArraySerializer
+     */
+    public ByteArraySerializer()
+    {
+        super( new ByteArrayComparator() );
+    }
+
+
+    /**
+     * Create a new instance of ByteArraySerializer with custom comparator
+     */
+    public ByteArraySerializer( Comparator comparator )
+    {
+        super( comparator );
+    }
+
+    
+    /**
+     * {@inheritDoc}
+     */
+    public byte[] serialize( byte[] element )
+    {
+        int len = -1;
+
+        if ( element != null )
+        {
+            len = element.length;
+        }
+
+        byte[] bytes = null;
+
+        switch ( len )
+        {
+            case 0:
+                bytes = new byte[4];
+
+                bytes[0] = 0x00;
+                bytes[1] = 0x00;
+                bytes[2] = 0x00;
+                bytes[3] = 0x00;
+
+                break;
+
+            case -1:
+                bytes = new byte[4];
+
+                bytes[0] = ( byte ) 0xFF;
+                bytes[1] = ( byte ) 0xFF;
+                bytes[2] = ( byte ) 0xFF;
+                bytes[3] = ( byte ) 0xFF;
+
+                break;
+
+            default:
+                bytes = new byte[len + 4];
+
+                System.arraycopy( element, 0, bytes, 4, len );
+
+                bytes[0] = ( byte ) ( len >>> 24 );
+                bytes[1] = ( byte ) ( len >>> 16 );
+                bytes[2] = ( byte ) ( len >>> 8 );
+                bytes[3] = ( byte ) ( len );
+        }
+
+        return bytes;
+    }
+
+
+    /**
+     * 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 static byte[] serialize( byte[] buffer, int start, byte[] 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:
+
+                buffer[start] = ( byte ) ( len >>> 24 );
+                buffer[start + 1] = ( byte ) ( len >>> 16 );
+                buffer[start + 2] = ( byte ) ( len >>> 8 );
+                buffer[start + 3] = ( byte ) ( len );
+
+                System.arraycopy( element, 0, buffer, 4 + start, len );
+        }
+
+        return buffer;
+
+    }
+
+
+    /**
+     * A static method used to deserialize a byte array from a byte array.
+     * 
+     * @param in The byte array containing the byte array
+     * @return A byte[]
+     */
+    public static byte[] deserialize( byte[] in )
+    {
+        if ( ( in == null ) || ( in.length < 4 ) )
+        {
+            throw new RuntimeException( "Cannot extract a byte[] from a buffer with not enough bytes" );
+        }
+
+        int len = IntSerializer.deserialize( in );
+
+        switch ( len )
+        {
+            case 0:
+                return new byte[]
+                    {};
+
+            case -1:
+                return null;
+
+            default:
+                byte[] result = new byte[len];
+                System.arraycopy( in, 4, result, 0, len );
+
+                return result;
+        }
+    }
+
+
+    /**
+     * A static method used to deserialize a byte array from a byte array.
+     * 
+     * @param in The byte array containing the byte array
+     * @param start the position in the byte[] we will deserialize the byte[] from
+     * @return A byte[]
+     */
+    public static byte[] deserialize( byte[] in, int start )
+    {
+        if ( ( in == null ) || ( in.length < 4 + start ) )
+        {
+            throw new RuntimeException( "Cannot extract a byte[] from a buffer with not enough bytes" );
+        }
+
+        int len = IntSerializer.deserialize( in, start );
+
+        switch ( len )
+        {
+            case 0:
+                return new byte[]
+                    {};
+
+            case -1:
+                return null;
+
+            default:
+                byte[] result = new byte[len];
+                System.arraycopy( in, 4 + start, result, 0, len );
+
+                return result;
+        }
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public byte[] deserialize( BufferHandler bufferHandler ) throws IOException
+    {
+        byte[] in = bufferHandler.read( 4 );
+
+        int len = IntSerializer.deserialize( in );
+
+        switch ( len )
+        {
+            case 0:
+                return new byte[]
+                    {};
+
+            case -1:
+                return null;
+
+            default:
+                in = bufferHandler.read( len );
+
+                return in;
+        }
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public byte[] deserialize( ByteBuffer buffer ) throws IOException
+    {
+        int len = buffer.getInt();
+
+        switch ( len )
+        {
+            case 0:
+                return new byte[]
+                    {};
+
+            case -1:
+                return null;
+
+            default:
+                byte[] bytes = new byte[len];
+
+                buffer.get( bytes );
+
+                return bytes;
+        }
+    }
+}

Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/ByteSerializer.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/ByteSerializer.java?rev=1510115&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/ByteSerializer.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/ByteSerializer.java Sun Aug  4 09:22:56 2013
@@ -0,0 +1,132 @@
+/*
+ *  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.directory.mavibot.btree.serializer;
+
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import org.apache.directory.mavibot.btree.comparator.ByteComparator;
+
+
+/**
+ * The Byte serializer.
+ * 
+ * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
+ */
+public class ByteSerializer extends AbstractElementSerializer<Byte>
+{
+    /**
+     * Create a new instance of ByteSerializer
+     */
+    public ByteSerializer()
+    {
+        super( new ByteComparator() );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public byte[] serialize( Byte element )
+    {
+        byte[] bytes = new byte[1];
+
+        return serialize( bytes, 0, element );
+    }
+
+
+    /**
+     * Serialize a byte
+     * 
+     * @param value the value to serialize
+     * @return The byte[] containing the serialized byte
+     */
+    public static byte[] serialize( byte value )
+    {
+        byte[] bytes = new byte[1];
+
+        return serialize( bytes, 0, value );
+    }
+
+
+    /**
+     * 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 static byte[] serialize( byte[] buffer, int start, byte value )
+    {
+        buffer[start] = value;
+
+        return buffer;
+    }
+
+
+    /**
+     * A static method used to deserialize a Byte from a byte array.
+     * @param in The byte array containing the Byte
+     * @return A Byte
+     */
+    public static Byte deserialize( byte[] in )
+    {
+        return deserialize( in, 0 );
+    }
+
+
+    /**
+     * 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
+     */
+    public static Byte deserialize( byte[] in, int start )
+    {
+        if ( ( in == null ) || ( in.length < 1 + start ) )
+        {
+            throw new RuntimeException( "Cannot extract a Byte from a buffer with not enough bytes" );
+        }
+
+        return in[start];
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public Byte deserialize( ByteBuffer buffer ) throws IOException
+    {
+        return buffer.get();
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public Byte deserialize( BufferHandler bufferHandler ) throws IOException
+    {
+        byte[] in = bufferHandler.read( 1 );
+
+        return deserialize( in );
+    }
+}

Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/CharArraySerializer.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/CharArraySerializer.java?rev=1510115&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/CharArraySerializer.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/CharArraySerializer.java Sun Aug  4 09:22:56 2013
@@ -0,0 +1,199 @@
+/*
+ *  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.directory.mavibot.btree.serializer;
+
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import org.apache.directory.mavibot.btree.comparator.CharArrayComparator;
+
+
+/**
+ * A serializer for a char[].
+ * 
+ * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
+ */
+public class CharArraySerializer extends AbstractElementSerializer<char[]>
+{
+    /**
+     * Create a new instance of CharArraySerializer
+     */
+    public CharArraySerializer()
+    {
+        super( new CharArrayComparator() );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public byte[] serialize( char[] element )
+    {
+        int len = -1;
+
+        if ( element != null )
+        {
+            len = element.length;
+        }
+
+        byte[] bytes = null;
+
+        switch ( len )
+        {
+            case 0:
+                bytes = new byte[4];
+
+                bytes[0] = 0x00;
+                bytes[1] = 0x00;
+                bytes[2] = 0x00;
+                bytes[3] = 0x00;
+
+                break;
+
+            case -1:
+                bytes = new byte[4];
+
+                bytes[0] = ( byte ) 0xFF;
+                bytes[1] = ( byte ) 0xFF;
+                bytes[2] = ( byte ) 0xFF;
+                bytes[3] = ( byte ) 0xFF;
+
+                break;
+
+            default:
+                bytes = new byte[len * 2 + 4];
+                int pos = 4;
+
+                bytes[0] = ( byte ) ( len >>> 24 );
+                bytes[1] = ( byte ) ( len >>> 16 );
+                bytes[2] = ( byte ) ( len >>> 8 );
+                bytes[3] = ( byte ) ( len );
+
+                for ( char c : element )
+                {
+                    bytes[pos++] = ( byte ) ( c >>> 8 );
+                    bytes[pos++] = ( byte ) ( c );
+                }
+        }
+
+        return bytes;
+    }
+
+
+    /**
+     * A static method used to deserialize a char array from a byte array.
+     * 
+     * @param in The byte array containing the char array
+     * @return A char[]
+     */
+    public static char[] deserialize( byte[] in )
+    {
+        if ( ( in == null ) || ( in.length < 4 ) )
+        {
+            throw new RuntimeException( "Cannot extract a byte[] from a buffer with not enough bytes" );
+        }
+
+        int len = IntSerializer.deserialize( in );
+
+        switch ( len )
+        {
+            case 0:
+                return new char[]
+                    {};
+
+            case -1:
+                return null;
+
+            default:
+                char[] result = new char[len];
+
+                for ( int i = 4; i < len * 2 + 4; i += 2 )
+                {
+                    result[i] = Character.valueOf( ( char ) ( ( in[i] << 8 ) +
+                        ( in[i + 1] & 0xFF ) ) );
+                }
+
+                return result;
+        }
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public char[] deserialize( BufferHandler bufferHandler ) throws IOException
+    {
+        byte[] in = bufferHandler.read( 4 );
+
+        int len = IntSerializer.deserialize( in );
+
+        switch ( len )
+        {
+            case 0:
+                return new char[]
+                    {};
+
+            case -1:
+                return null;
+
+            default:
+                char[] result = new char[len];
+                byte[] buffer = bufferHandler.read( len * 2 );
+
+                for ( int i = 0; i < len * 2; i += 2 )
+                {
+                    result[i] = Character.valueOf( ( char ) ( ( buffer[i] << 8 ) +
+                        ( buffer[i + 1] & 0xFF ) ) );
+                }
+
+                return result;
+        }
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public char[] deserialize( ByteBuffer buffer ) throws IOException
+    {
+        int len = buffer.getInt();
+
+        switch ( len )
+        {
+            case 0:
+                return new char[]
+                    {};
+
+            case -1:
+                return null;
+
+            default:
+                char[] result = new char[len];
+
+                for ( int i = 0; i < len; i++ )
+                {
+                    result[i] = buffer.getChar();
+                }
+
+                return result;
+        }
+    }
+}

Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/CharSerializer.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/CharSerializer.java?rev=1510115&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/CharSerializer.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/CharSerializer.java Sun Aug  4 09:22:56 2013
@@ -0,0 +1,134 @@
+/*
+ *  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.directory.mavibot.btree.serializer;
+
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import org.apache.directory.mavibot.btree.comparator.CharComparator;
+
+
+/**
+ * The Character serializer.
+ * 
+ * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
+ */
+public class CharSerializer extends AbstractElementSerializer<Character>
+{
+    /**
+     * Create a new instance of CharSerializer
+     */
+    public CharSerializer()
+    {
+        super( new CharComparator() );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public byte[] serialize( Character element )
+    {
+        byte[] bytes = new byte[2];
+
+        return serialize( bytes, 0, element );
+    }
+
+
+    /**
+     * Serialize a char
+     * 
+     * @param value the value to serialize
+     * @return The byte[] containing the serialized char
+     */
+    public static byte[] serialize( char value )
+    {
+        byte[] bytes = new byte[2];
+
+        return serialize( bytes, 0, value );
+    }
+
+
+    /**
+     * 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 static byte[] serialize( byte[] buffer, int start, char value )
+    {
+        buffer[start] = ( byte ) ( value >>> 8 );
+        buffer[start + 1] = ( byte ) ( value );
+
+        return buffer;
+    }
+
+
+    /**
+     * A static method used to deserialize a Character from a byte array.
+     * @param in The byte array containing the Character
+     * @return A Character
+     */
+    public static Character deserialize( byte[] in )
+    {
+        return deserialize( in, 0 );
+    }
+
+
+    /**
+     * 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
+     */
+    public static Character deserialize( byte[] in, int start )
+    {
+        if ( ( in == null ) || ( in.length < 2 + start ) )
+        {
+            throw new RuntimeException( "Cannot extract a Character from a buffer with not enough bytes" );
+        }
+
+        return Character.valueOf( ( char ) ( ( in[start] << 8 ) +
+            ( in[start + 1] & 0xFF ) ) );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public Character deserialize( ByteBuffer buffer ) throws IOException
+    {
+        return buffer.getChar();
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public Character deserialize( BufferHandler bufferHandler ) throws IOException
+    {
+        byte[] in = bufferHandler.read( 2 );
+
+        return deserialize( in );
+    }
+}

Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/ElementSerializer.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/ElementSerializer.java?rev=1510115&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/ElementSerializer.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/ElementSerializer.java Sun Aug  4 09:22:56 2013
@@ -0,0 +1,85 @@
+/*
+ *  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.directory.mavibot.btree.serializer;
+
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Comparator;
+
+
+/**
+ * This interface is used by implementations of serializer, deserializer and comparator.
+ * 
+ * @param <T> The type for the element to serialize and compare
+ * 
+ * @author <a href="mailto:labs@labs.apache.org">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 BufferHandler
+     * 
+     * @param bufferHandler The incoming bufferHandler
+     * @return The deserialized element
+     * @throws IOException If the deserialization failed
+     */
+    T deserialize( BufferHandler bufferHandler ) throws IOException;
+
+
+    /**
+     * Deserialize an element from a ByteBuffer
+     * 
+     * @param buffer The incoming ByteBuffer
+     * @return The deserialized element
+     * @throws IOException If the deserialization failed
+     */
+    T deserialize( ByteBuffer buffer ) throws IOException;
+
+
+    /**
+     * Returns the comparison of two types. <br/>
+     * <ul>
+     * <li>If type1 < type2, return -1</li>
+     * <li>If type1 > type2, return 1</li>
+     * <li>If type1 == type2, return 0</li>
+     * </ul>
+     * 
+     * @param type1 The first type to compare 
+     * @param type2 The second type to compare 
+     * @return The comparison result
+     */
+    int compare( T type1, T type2 );
+
+
+    /**
+     * @return the comparator for the used type
+     */
+    Comparator<T> getComparator();
+}

Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/IntSerializer.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/IntSerializer.java?rev=1510115&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/IntSerializer.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/IntSerializer.java Sun Aug  4 09:22:56 2013
@@ -0,0 +1,136 @@
+/*
+ *  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.directory.mavibot.btree.serializer;
+
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import org.apache.directory.mavibot.btree.comparator.IntComparator;
+
+
+/**
+ * The Integer serializer.
+ * 
+ * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
+ */
+public class IntSerializer extends AbstractElementSerializer<Integer>
+{
+    /**
+     * Create a new instance of IntSerializer
+     */
+    public IntSerializer()
+    {
+        super( new IntComparator() );
+    }
+
+
+    /**
+     * A static method used to deserialize an Integer from a byte array.
+     * @param in The byte array containing the Integer
+     * @return An Integer
+     */
+    public static Integer deserialize( byte[] in )
+    {
+        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, int start )
+    {
+        if ( ( in == null ) || ( in.length < 4 + start ) )
+        {
+            throw new RuntimeException( "Cannot extract a Integer from a buffer with not enough bytes" );
+        }
+
+        return ( in[start] << 24 ) +
+            ( ( in[start + 1] & 0xFF ) << 16 ) +
+            ( ( in[start + 2] & 0xFF ) << 8 ) +
+            ( in[start + 3] & 0xFF );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public Integer deserialize( ByteBuffer buffer ) throws IOException
+    {
+        return buffer.getInt();
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public Integer deserialize( BufferHandler bufferHandler ) throws IOException
+    {
+        byte[] in = bufferHandler.read( 4 );
+
+        return deserialize( in );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public byte[] serialize( Integer element )
+    {
+        return serialize( element.intValue() );
+    }
+
+
+    /**
+     * 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 );
+    }
+
+
+    /**
+     * 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
+     */
+    public static byte[] serialize( byte[] buffer, int start, int value )
+    {
+        buffer[start] = ( byte ) ( value >>> 24 );
+        buffer[start + 1] = ( byte ) ( value >>> 16 );
+        buffer[start + 2] = ( byte ) ( value >>> 8 );
+        buffer[start + 3] = ( byte ) ( value );
+
+        return buffer;
+    }
+}

Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/LongArraySerializer.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/LongArraySerializer.java?rev=1510115&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/LongArraySerializer.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/LongArraySerializer.java Sun Aug  4 09:22:56 2013
@@ -0,0 +1,274 @@
+/*
+ *  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.directory.mavibot.btree.serializer;
+
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Comparator;
+
+import org.apache.directory.mavibot.btree.comparator.LongArrayComparator;
+
+
+/**
+ * A serializer for a Long[].
+ * 
+ * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
+ */
+public class LongArraySerializer implements ElementSerializer<long[]>
+{
+    /** The associated comparator */
+    private final Comparator<long[]> comparator;
+
+
+    /**
+     * Create a new instance of LongSerializer
+     */
+    public LongArraySerializer()
+    {
+        comparator = new LongArrayComparator();
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public byte[] serialize( long[] element )
+    {
+        int len = -1;
+
+        if ( element != null )
+        {
+            len = element.length;
+        }
+
+        byte[] bytes = null;
+
+        switch ( len )
+        {
+            case 0:
+                bytes = new byte[4];
+
+                bytes[0] = 0x00;
+                bytes[1] = 0x00;
+                bytes[2] = 0x00;
+                bytes[3] = 0x00;
+
+                break;
+
+            case -1:
+                bytes = new byte[4];
+
+                bytes[0] = ( byte ) 0xFF;
+                bytes[1] = ( byte ) 0xFF;
+                bytes[2] = ( byte ) 0xFF;
+                bytes[3] = ( byte ) 0xFF;
+
+                break;
+
+            default:
+                bytes = new byte[len * 8 + 4];
+                int pos = 0;
+
+                // The number of longs
+                bytes[pos++] = ( byte ) ( len >>> 24 );
+                bytes[pos++] = ( byte ) ( len >>> 16 );
+                bytes[pos++] = ( byte ) ( len >>> 8 );
+                bytes[pos++] = ( byte ) ( len );
+
+                // Serialize the longs now
+                for ( long value : element )
+                {
+                    bytes[pos++] = ( byte ) ( value >>> 56 );
+                    bytes[pos++] = ( byte ) ( value >>> 48 );
+                    bytes[pos++] = ( byte ) ( value >>> 40 );
+                    bytes[pos++] = ( byte ) ( value >>> 32 );
+                    bytes[pos++] = ( byte ) ( value >>> 24 );
+                    bytes[pos++] = ( byte ) ( value >>> 16 );
+                    bytes[pos++] = ( byte ) ( value >>> 8 );
+                    bytes[pos++] = ( byte ) ( value );
+                }
+        }
+
+        return bytes;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public long[] deserialize( BufferHandler bufferHandler ) throws IOException
+    {
+        byte[] in = bufferHandler.read( 4 );
+
+        int len = IntSerializer.deserialize( in );
+
+        switch ( len )
+        {
+            case 0:
+                return new long[]
+                    {};
+
+            case -1:
+                return null;
+
+            default:
+                long[] longs = new long[len];
+
+                int pos = 4;
+
+                for ( int i = 0; i < len; i++ )
+                {
+                    longs[i] = ( ( long ) in[pos++] << 56 ) +
+                        ( ( in[pos++] & 0xFFL ) << 48 ) +
+                        ( ( in[pos++] & 0xFFL ) << 40 ) +
+                        ( ( in[pos++] & 0xFFL ) << 32 ) +
+                        ( ( in[pos++] & 0xFFL ) << 24 ) +
+                        ( ( in[pos++] & 0xFFL ) << 16 ) +
+                        ( ( in[pos++] & 0xFFL ) << 8 ) +
+                        ( in[pos++] & 0xFFL );
+                }
+
+                return longs;
+        }
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public long[] deserialize( ByteBuffer buffer ) throws IOException
+    {
+        int len = buffer.getInt();
+
+        switch ( len )
+        {
+            case 0:
+                return new long[]
+                    {};
+
+            case -1:
+                return null;
+
+            default:
+                long[] longs = new long[len];
+
+                for ( int i = 0; i < len; i++ )
+                {
+                    longs[i] = buffer.getLong();
+                }
+
+                return longs;
+        }
+    }
+
+
+    /**
+     * {@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
+            {
+                if ( type1.length < type2.length )
+                {
+                    int pos = 0;
+
+                    for ( long b1 : type1 )
+                    {
+                        long b2 = type2[pos];
+
+                        if ( b1 == b2 )
+                        {
+                            pos++;
+                        }
+                        else if ( b1 < b2 )
+                        {
+                            return -1;
+                        }
+                        else
+                        {
+                            return 1;
+                        }
+                    }
+
+                    return 1;
+                }
+                else
+                {
+                    int pos = 0;
+
+                    for ( long b2 : type2 )
+                    {
+                        long b1 = type1[pos];
+
+                        if ( b1 == b2 )
+                        {
+                            pos++;
+                        }
+                        else if ( b1 < b2 )
+                        {
+                            return -1;
+                        }
+                        else
+                        {
+                            return 1;
+                        }
+                    }
+
+                    return -11;
+                }
+            }
+        }
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public Comparator<long[]> getComparator()
+    {
+        return comparator;
+    }
+}

Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/LongSerializer.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/LongSerializer.java?rev=1510115&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/LongSerializer.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/LongSerializer.java Sun Aug  4 09:22:56 2013
@@ -0,0 +1,147 @@
+/*
+ *  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.directory.mavibot.btree.serializer;
+
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import org.apache.directory.mavibot.btree.comparator.LongComparator;
+
+
+/**
+ * The Long serializer.
+ * 
+ * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
+ */
+public class LongSerializer extends AbstractElementSerializer<Long>
+{
+    /**
+     * Create a new instance of LongSerializer
+     */
+    public LongSerializer()
+    {
+        super( new LongComparator() );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public byte[] serialize( Long element )
+    {
+        return serialize( element.longValue() );
+    }
+
+
+    /**
+     * 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];
+
+        return serialize( bytes, 0, value );
+    }
+
+
+    /**
+     * 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 )
+    {
+        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[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;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public Long deserialize( BufferHandler bufferHandler ) throws IOException
+    {
+        byte[] in = bufferHandler.read( 8 );
+
+        return deserialize( in );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public Long deserialize( ByteBuffer buffer ) throws IOException
+    {
+        return buffer.getLong();
+    }
+}

Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/Serializer.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/Serializer.java?rev=1510115&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/Serializer.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/Serializer.java Sun Aug  4 09:22:56 2013
@@ -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.directory.mavibot.btree.serializer;
+
+
+import java.io.IOException;
+
+
+/**
+ * This interface is used by implementations of serializer, deserializr and comparator.
+ * 
+ * @param <T> The type for the element to serialize
+ * 
+ * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
+ */
+public interface Serializer<T>
+{
+    /**
+     * Produce the byte[] representation of the type
+     * 
+     * @param type The type to serialize
+     * @return The byte[] containing the serialized type
+     */
+    byte[] serialize( T type );
+
+
+    /**
+     * Deserialize a type from a byte[]
+     * 
+     * @param bufferHandler The incoming BufferHandler
+     * @return The deserialized type
+     * @throws IOException If the deserialization failed
+     */
+    T deserialize( BufferHandler bufferHandler ) throws IOException;
+
+
+    /**
+     * Returns the comparison of two types. <br/>
+     * <ul>
+     * <li>If type1 < type2, return -1</li>
+     * <li>If type1 > type2, return 1</li>
+     * <li>If type1 == type2, return 0</li>
+     * </ul>
+     * 
+     * @param type1 The first type to compare 
+     * @param type2 The second type to compare 
+     * @return The comparison result
+     */
+    int compare( T type1, T type2 );
+}

Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/ShortSerializer.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/ShortSerializer.java?rev=1510115&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/ShortSerializer.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/ShortSerializer.java Sun Aug  4 09:22:56 2013
@@ -0,0 +1,133 @@
+/*
+ *  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.directory.mavibot.btree.serializer;
+
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import org.apache.directory.mavibot.btree.comparator.ShortComparator;
+
+
+/**
+ * The Short serializer.
+ * 
+ * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
+ */
+public class ShortSerializer extends AbstractElementSerializer<Short>
+{
+    /**
+     * Create a new instance of ShortSerializer
+     */
+    public ShortSerializer()
+    {
+        super( new ShortComparator() );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public byte[] serialize( Short element )
+    {
+        byte[] bytes = new byte[2];
+
+        return serialize( bytes, 0, element );
+    }
+
+
+    /**
+     * Serialize a short
+     * 
+     * @param value the value to serialize
+     * @return The byte[] containing the serialized short
+     */
+    public static byte[] serialize( short value )
+    {
+        byte[] bytes = new byte[2];
+
+        return serialize( bytes, 0, value );
+    }
+
+
+    /**
+     * 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 static byte[] serialize( byte[] buffer, int start, short value )
+    {
+        buffer[start] = ( byte ) ( value >>> 8 );
+        buffer[start + 1] = ( byte ) ( value );
+
+        return buffer;
+    }
+
+
+    /**
+     * A static method used to deserialize a Short from a byte array.
+     * @param in The byte array containing the Short
+     * @return A Short
+     */
+    public static Short deserialize( byte[] in )
+    {
+        return deserialize( in, 0 );
+    }
+
+
+    /**
+     * 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
+     */
+    public static Short deserialize( byte[] in, int start )
+    {
+        if ( ( in == null ) || ( in.length < 2 + start ) )
+        {
+            throw new RuntimeException( "Cannot extract a Short from a buffer with not enough bytes" );
+        }
+
+        return ( short ) ( ( in[start] << 8 ) + ( in[start + 1] & 0xFF ) );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public Short deserialize( ByteBuffer buffer ) throws IOException
+    {
+        return buffer.getShort();
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public Short deserialize( BufferHandler bufferHandler ) throws IOException
+    {
+        byte[] in = bufferHandler.read( 2 );
+
+        return deserialize( in );
+    }
+}

Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/StringSerializer.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/StringSerializer.java?rev=1510115&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/StringSerializer.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/serializer/StringSerializer.java Sun Aug  4 09:22:56 2013
@@ -0,0 +1,345 @@
+/*
+ *  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.directory.mavibot.btree.serializer;
+
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.nio.ByteBuffer;
+import java.util.Comparator;
+
+import org.apache.directory.mavibot.btree.comparator.StringComparator;
+import org.apache.directory.mavibot.btree.util.Strings;
+
+
+/**
+ * The String serializer.
+ * 
+ * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
+ */
+public class StringSerializer extends AbstractElementSerializer<String>
+{
+    public static final StringSerializer INSTANCE = new StringSerializer();
+    
+    /**
+     * Create a new instance of StringSerializer
+     */
+    public StringSerializer()
+    {
+        super( new StringComparator() );
+    }
+
+
+    /**
+     * Create a new instance of StringSerializer with custom comparator
+     */
+    public StringSerializer( Comparator comparator )
+    {
+        super( comparator );
+    }
+
+    
+    /**
+     * 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;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public byte[] serialize( String element )
+    {
+        int len = -1;
+
+        if ( element != null )
+        {
+            len = element.length();
+        }
+
+        byte[] bytes = null;
+
+        switch ( len )
+        {
+            case 0:
+                bytes = new byte[4];
+
+                bytes[0] = 0x00;
+                bytes[1] = 0x00;
+                bytes[2] = 0x00;
+                bytes[3] = 0x00;
+
+                break;
+
+            case -1:
+                bytes = new byte[4];
+
+                bytes[0] = ( byte ) 0xFF;
+                bytes[1] = ( byte ) 0xFF;
+                bytes[2] = ( byte ) 0xFF;
+                bytes[3] = ( byte ) 0xFF;
+
+                break;
+
+            default:
+                char[] chars = element.toCharArray();
+                byte[] tmpBytes = new byte[chars.length * 2];
+
+                int pos = 0;
+                len = 0;
+
+                for ( char c : chars )
+                {
+                    if ( ( c & 0xFF80 ) == 0 )
+                    {
+                        tmpBytes[pos++] = ( byte ) c;
+                    }
+                    else if ( ( c & 0xF800 ) == 0 )
+                    {
+                        tmpBytes[pos++] = ( byte ) ( ( byte ) 0x00C0 | ( byte ) ( ( c & 0x07C0 ) >> 6 ) );
+                        tmpBytes[pos++] = ( byte ) ( ( byte ) 0x80 | ( byte ) ( c & 0x003F ) );
+                    }
+                    else
+                    {
+                        tmpBytes[pos++] = ( byte ) ( ( byte ) 0x80 | ( byte ) ( c & 0x001F ) );
+                        tmpBytes[pos++] = ( byte ) ( ( byte ) 0x80 | ( byte ) ( c & 0x07C0 ) );
+                        tmpBytes[pos++] = ( byte ) ( ( byte ) 0xE0 | ( byte ) ( c & 0x7800 ) );
+                    }
+                }
+
+                bytes = new byte[pos + 4];
+
+                bytes[0] = ( byte ) ( pos >>> 24 );
+                bytes[1] = ( byte ) ( pos >>> 16 );
+                bytes[2] = ( byte ) ( pos >>> 8 );
+                bytes[3] = ( byte ) ( pos );
+
+                System.arraycopy( tmpBytes, 0, bytes, 4, pos );
+        }
+
+        return bytes;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     * @throws IOException 
+     */
+    public String deserialize( BufferHandler bufferHandler ) throws IOException
+    {
+        byte[] in = bufferHandler.read( 4 );
+
+        int len = IntSerializer.deserialize( in );
+
+        switch ( len )
+        {
+            case 0:
+                return "";
+
+            case -1:
+                return null;
+
+            default:
+                in = bufferHandler.read( len );
+
+                return Strings.utf8ToString( in );
+        }
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public String deserialize( ByteBuffer buffer ) throws IOException
+    {
+        int len = buffer.getInt();
+
+        switch ( len )
+        {
+            case 0:
+                return "";
+
+            case -1:
+                return null;
+
+            default:
+                byte[] bytes = new byte[len];
+
+                buffer.get( bytes );
+                char[] chars = new char[len];
+                int clen = 0;
+
+                for ( int i = 0; i < len; i++ )
+                {
+                    byte b = bytes[i];
+
+                    if ( b >= 0 )
+                    {
+                        chars[clen++] = ( char ) b;
+                    }
+                    else
+                    {
+                        if ( ( b & 0xE0 ) == 0 )
+                        {
+                            // 3 bytes long char
+                            i++;
+                            byte b2 = bytes[i];
+                            i++;
+                            byte b3 = bytes[i];
+                            chars[clen++] = ( char ) ( ( ( b & 0x000F ) << 12 ) | ( ( b2 & 0x003F ) << 6 ) | ( ( b3 & 0x003F ) ) );
+                        }
+                        else
+                        {
+                            // 2 bytes long char
+                            i++;
+                            byte b2 = bytes[i];
+                            chars[clen++] = ( char ) ( ( ( b & 0x001F ) << 6 ) | ( b2 & 0x003F ) );
+                        }
+                    }
+                }
+
+                return new String( chars, 0, clen );
+        }
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int compare( String type1, String 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 );
+            }
+        }
+    }
+}

Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/util/BulkDataSorter.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/util/BulkDataSorter.java?rev=1510115&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/util/BulkDataSorter.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/util/BulkDataSorter.java Sun Aug  4 09:22:56 2013
@@ -0,0 +1,273 @@
+/*
+ *   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.directory.mavibot.btree.util;
+
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.lang.reflect.Array;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.UUID;
+
+import org.apache.directory.mavibot.btree.BTreeBuilder;
+import org.apache.directory.mavibot.btree.Tuple;
+
+
+/**
+ * A utility class for sorting a large number of keys before building a BTree using {@link BTreeBuilder}.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class BulkDataSorter<K, V>
+{
+    private File workDir;
+
+    private int splitAfter = 1000;
+
+    private Comparator<Tuple<K, V>> tupleComparator;
+
+    private TupleReaderWriter<K, V> readerWriter;
+
+    private boolean sorted;
+
+
+    public BulkDataSorter( TupleReaderWriter<K, V> readerWriter, Comparator<Tuple<K, V>> tupleComparator,
+        int splitAfter )
+    {
+        if ( splitAfter <= 0 )
+        {
+            throw new IllegalArgumentException( "Value of splitAfter parameter cannot be null" );
+        }
+
+        this.splitAfter = splitAfter;
+
+        this.workDir = new File( System.getProperty( "java.io.tmpdir" ), System.currentTimeMillis() + "-sort" );
+        workDir.mkdir();
+
+        this.readerWriter = readerWriter;
+        this.tupleComparator = tupleComparator;
+    }
+
+
+    public void sort( File dataFile ) throws IOException
+    {
+        int i = 0;
+
+        Tuple<K, V>[] arr = ( Tuple<K, V>[] ) Array.newInstance( Tuple.class, splitAfter );
+
+        Tuple<K, V> t = null;
+
+        DataInputStream in = new DataInputStream( new FileInputStream( dataFile ) );
+
+        while ( ( t = readerWriter.readTuple( in ) ) != null )
+        {
+            arr[i++] = t;
+
+            if ( ( i % splitAfter ) == 0 )
+            {
+                i = 0;
+                Arrays.sort( arr, tupleComparator );
+
+                storeSortedData( arr );
+            }
+        }
+
+        if ( i != 0 )
+        {
+            Tuple<K, V>[] tmp = ( Tuple<K, V>[] ) Array.newInstance( Tuple.class, i );
+            System.arraycopy( arr, 0, tmp, 0, i );
+            Arrays.sort( tmp, tupleComparator );
+
+            storeSortedData( tmp );
+        }
+
+        sorted = true;
+    }
+
+
+    private void storeSortedData( Tuple<K, V>[] arr ) throws IOException
+    {
+        File tempFile = File.createTempFile( UUID.randomUUID().toString(), ".batch", workDir );
+        DataOutputStream out = new DataOutputStream( new FileOutputStream( tempFile ) );
+
+        for ( Tuple<K, V> t : arr )
+        {
+            readerWriter.writeTuple( t, out );
+        }
+
+        out.flush();
+        out.close();
+    }
+
+
+    public File getWorkDir()
+    {
+        return workDir;
+    }
+
+
+    public Iterator<Tuple<K, V>> getMergeSortedTuples() throws IOException
+    {
+        if ( !sorted )
+        {
+            throw new IllegalStateException( "Data is not sorted" );
+        }
+
+        File[] batches = workDir.listFiles();
+
+        if ( batches.length == 0 )
+        {
+            return Collections.EMPTY_LIST.iterator();
+        }
+
+        final DataInputStream[] streams = new DataInputStream[batches.length];
+
+        for ( int i = 0; i < batches.length; i++ )
+        {
+            streams[i] = new DataInputStream( new FileInputStream( batches[i] ) );
+        }
+
+        Iterator<Tuple<K, V>> itr = new Iterator<Tuple<K, V>>()
+        {
+            private Tuple<K, V>[] heads = ( Tuple<K, V>[] ) Array.newInstance( Tuple.class, streams.length );
+
+            private Tuple<K, V> candidate = null;
+
+            private boolean closed;
+
+            private int candidatePos = -1;
+
+
+            @Override
+            public boolean hasNext()
+            {
+
+                if ( closed )
+                {
+                    throw new IllegalStateException( "No elements to read" );
+                }
+
+                Tuple<K, V> available = null;
+
+                for ( int i = 0; i < streams.length; i++ )
+                {
+                    if ( heads[i] == null )
+                    {
+                        heads[i] = readerWriter.readTuple( streams[i] );
+                    }
+
+                    if ( available == null )
+                    {
+                        available = heads[i];
+                        candidatePos = i;
+                    }
+                    else
+                    {
+                        if ( ( available != null ) && ( heads[i] != null ) )
+                        {
+                            int comp = tupleComparator.compare( heads[i], available );
+                            if ( comp <= 0 )
+                            {
+                                available = heads[i];
+                                candidatePos = i;
+                            }
+                        }
+                    }
+                }
+
+                heads[candidatePos] = null;
+
+                if ( available == null )
+                {
+                    for ( int i = 0; i < streams.length; i++ )
+                    {
+                        if ( heads[i] != null )
+                        {
+                            available = heads[i];
+                            heads[i] = readerWriter.readTuple( streams[i] );
+                            break;
+                        }
+                    }
+                }
+
+                if ( available != null )
+                {
+                    candidate = available;
+                    return true;
+                }
+
+                // finally close the streams
+                for ( DataInputStream in : streams )
+                {
+                    try
+                    {
+                        in.close();
+                    }
+                    catch ( Exception e )
+                    {
+                        e.printStackTrace();
+                    }
+                }
+
+                closed = true;
+
+                return false;
+            }
+
+
+            @Override
+            public Tuple<K, V> next()
+            {
+                if ( candidate == null )
+                {
+                    if ( !closed )
+                    {
+                        hasNext();
+                    }
+                }
+
+                if ( candidate == null )
+                {
+                    throw new NoSuchElementException( "No tuples found" );
+                }
+
+                return candidate;
+            }
+
+
+            @Override
+            public void remove()
+            {
+                throw new UnsupportedOperationException( "Not supported" );
+            }
+
+        };
+
+        return itr;
+    }
+}

Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/util/IntTupleReaderWriter.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/util/IntTupleReaderWriter.java?rev=1510115&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/util/IntTupleReaderWriter.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/util/IntTupleReaderWriter.java Sun Aug  4 09:22:56 2013
@@ -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.directory.mavibot.btree.util;
+
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+
+import org.apache.directory.mavibot.btree.Tuple;
+
+
+/**
+ * TODO IntTupleReaderWriter.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class IntTupleReaderWriter implements TupleReaderWriter<Integer, Integer>
+{
+
+    @Override
+    public void writeTuple( Tuple<Integer, Integer> t, DataOutputStream out ) throws IOException
+    {
+        out.writeInt( t.getKey() );
+        out.writeInt( t.getValue() );
+    }
+
+
+    @Override
+    public Tuple<Integer, Integer> readTuple( DataInputStream in )
+    {
+
+        try
+        {
+            if ( in.available() <= 0 )
+            {
+                return null;
+            }
+
+            Tuple<Integer, Integer> t = new Tuple<Integer, Integer>( in.readInt(), in.readInt() );
+
+            return t;
+        }
+        catch ( IOException e )
+        {
+        }
+
+        return null;
+    }
+}



Mime
View raw message