You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by el...@apache.org on 2012/08/01 18:01:02 UTC
svn commit: r1368081 - in /labs/mavibot/trunk/mavibot/src:
main/java/org/apache/mavibot/btree/BTree.java
test/java/org/apache/mavibot/btree/serializer/DefaultSerializerTest.java
Author: elecharny
Date: Wed Aug 1 16:01:01 2012
New Revision: 1368081
URL: http://svn.apache.org/viewvc?rev=1368081&view=rev
Log:
o Added a test to check the serialization/deserialization
o Injected the serialization into the BTree
o Added the File into the BTree
o Added a counter of element sin the tree
o Added a first draft of the flush() method
Added:
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/DefaultSerializerTest.java
Modified:
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java
Modified: labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java
URL: http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java?rev=1368081&r1=1368080&r2=1368081&view=diff
==============================================================================
--- labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java (original)
+++ labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java Wed Aug 1 16:01:01 2012
@@ -20,7 +20,9 @@
package org.apache.mavibot.btree;
+import java.io.File;
import java.io.IOException;
+import java.io.RandomAccessFile;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Comparator;
@@ -29,6 +31,8 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
+import org.apache.mavibot.btree.serializer.Serializer;
+
/**
* The B+Tree MVCC data structure.
@@ -64,33 +68,124 @@ public class BTree<K, V>
/** The type to use to create the keys */
protected Class<?> keyType;
+ /** The Key and Value serializer used for this tree. If none is provided,
+ * the BTree will deduce the serializer to use from the generic type, and
+ * use the default Java serialization */
+ private Serializer<K, V> serializer;
+
+ /** The associated file. If null, this is an in-memory btree */
+ private File file;
+
+ /** The number of elements in the current revision */
+ private AtomicLong nbElems = new AtomicLong( 0 );
+
/**
- * Creates a new BTree with a default page size and a comparator.
+ * Creates a new in-memory BTree with a default page size and a comparator.
*
* @param comparator The comparator to use
*/
public BTree( Comparator<K> comparator ) throws IOException
{
- this( comparator, DEFAULT_PAGE_SIZE );
+ this( null, comparator, null, DEFAULT_PAGE_SIZE );
+ }
+
+
+ /**
+ * Creates a new in-memory BTree with a default page size and a comparator.
+ *
+ * @param comparator The comparator to use
+ * @param serializer The serializer to use
+ */
+ public BTree( Comparator<K> comparator, Serializer<K, V> serializer ) throws IOException
+ {
+ this( null, comparator, serializer, DEFAULT_PAGE_SIZE );
+ }
+
+
+ /**
+ * Creates a new BTree with a default page size and a comparator, with an associated file.
+ *
+ * @param file The file storing the BTree data
+ * @param comparator The comparator to use
+ */
+ public BTree( File file, Comparator<K> comparator ) throws IOException
+ {
+ this( file, comparator, null, DEFAULT_PAGE_SIZE );
}
/**
- * Creates a new BTree with a specific page size and a comparator.
+ * Creates a new BTree with a default page size and a comparator, with an associated file.
+ *
+ * @param file The file storing the BTree data
+ * @param comparator The comparator to use
+ * @param serializer The serializer to use
+ */
+ public BTree( File file, Comparator<K> comparator, Serializer<K, V> serializer ) throws IOException
+ {
+ this( file, comparator, serializer, DEFAULT_PAGE_SIZE );
+ }
+
+
+ /**
+ * Creates a new in-memory BTree with a specific page size and a comparator.
*
* @param comparator The comparator to use
* @param pageSize The number of elements we can store in a page
*/
public BTree( Comparator<K> comparator, int pageSize ) throws IOException
{
+ this( null, comparator, null, pageSize );
+ }
+
+
+ /**
+ * Creates a new in-memory BTree with a specific page size and a comparator.
+ *
+ * @param comparator The comparator to use
+ * @param serializer The serializer to use
+ * @param pageSize The number of elements we can store in a page
+ */
+ public BTree( Comparator<K> comparator, Serializer<K, V> serializer, int pageSize ) throws IOException
+ {
+ this( null, comparator, serializer, pageSize );
+ }
+
+
+ /**
+ * Creates a new in-memory BTree with a specific page size and a comparator.
+ *
+ * @param file The file storing the BTree data
+ * @param comparator The comparator to use
+ * @param pageSize The number of elements we can store in a page
+ */
+ public BTree( File file, Comparator<K> comparator, int pageSize ) throws IOException
+ {
+ this( file, comparator, null, pageSize );
+ }
+
+
+ /**
+ * Creates a new BTree with a specific page size and a comparator, with an associated file.
+ *
+ * @param file The file storing the BTree data
+ * @param comparator The comparator to use
+ * @param serializer The serializer to use
+ * @param pageSize The number of elements we can store in a page
+ */
+ public BTree( File file, Comparator<K> comparator, Serializer<K, V> serializer, int pageSize )
+ throws IOException
+ {
if ( comparator == null )
{
throw new IllegalArgumentException( "Comparator should not be null" );
}
this.comparator = comparator;
+ this.file = file;
setPageSize( pageSize );
+ this.serializer = serializer;
// Create the map contaning all the revisions
roots = new ConcurrentHashMap<Long, Page<K, V>>();
@@ -219,7 +314,16 @@ public class BTree<K, V>
{
long revision = generateRevision();
- return insert( key, value, revision );
+ V existingValue = insert( key, value, revision );
+
+ // Increase the number of element in the current tree if the insertion is successful
+ // and does not replace an element
+ if ( existingValue == null )
+ {
+ nbElems.getAndIncrement();
+ }
+
+ return existingValue;
}
@@ -239,7 +343,15 @@ public class BTree<K, V>
long revision = generateRevision();
- return delete( key, revision );
+ Tuple<K, V> deleted = delete( key, revision );
+
+ // Decrease the number of element in the current tree if the delete is successful
+ if ( deleted != null )
+ {
+ nbElems.getAndDecrement();
+ }
+
+ return deleted;
}
@@ -471,6 +583,15 @@ public class BTree<K, V>
/**
+ * @param serializer the serializer to set
+ */
+ public void setSerializer( Serializer<K, V> serializer )
+ {
+ this.serializer = serializer;
+ }
+
+
+ /**
* @return the type for the keys
*/
public Class<?> getKeyType()
@@ -480,6 +601,82 @@ public class BTree<K, V>
/**
+ * Flush the latest revision to disk
+ * @param file The file into which the data will be written
+ */
+ public void flush( File file )
+ {
+
+ }
+
+
+ /**
+ * Flush the latest revision to disk. We will replace the current file by the new one, as
+ * we flush in a temporaty file.
+ */
+ public void flush() throws IOException
+ {
+ File tmpFileFD = File.createTempFile( "mavibot", null );
+ RandomAccessFile tempFile = new RandomAccessFile(
+ tmpFileFD.getCanonicalPath(), "rw" );
+
+ Cursor<K, V> cursor = browse();
+
+ if ( serializer == null )
+ {
+ serializer = new Serializer<K, V>()
+ {
+ public byte[] serializeKey( K key )
+ {
+ return null;
+ }
+
+
+ public K deserializeKey( byte[] in )
+ {
+ return null;
+ }
+
+
+ public byte[] serializeValue( V value )
+ {
+ return null;
+ }
+
+
+ public V deserializeValue( byte[] in )
+ {
+ return null;
+ }
+ };
+ }
+
+ // Write the number of elements first
+ tempFile.writeLong( nbElems.get() );
+
+ while ( cursor.hasNext() )
+ {
+ Tuple<K, V> tuple = cursor.next();
+
+ byte[] keyBuffer = serializer.serializeKey( tuple.getKey() );
+ tempFile.write( keyBuffer );
+
+ byte[] valueBuffer = serializer.serializeValue( tuple.getValue() );
+ tempFile.write( valueBuffer );
+ }
+
+ tempFile.close();
+ tempFile.getFD().sync();
+
+ // Rename the current file to save a backup
+ file.renameTo( new File( file.getName() + ".bak" ) );
+
+ // And rename the temporary file
+ tmpFileFD.renameTo( file.getCanonicalFile() );
+ }
+
+
+ /**
* @see Object#toString()
*/
public String toString()
Added: labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/DefaultSerializerTest.java
URL: http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/DefaultSerializerTest.java?rev=1368081&view=auto
==============================================================================
--- labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/DefaultSerializerTest.java (added)
+++ labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/DefaultSerializerTest.java Wed Aug 1 16:01:01 2012
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.mavibot.btree.serializer;
+
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+
+/**
+ * Test the DefaultSerializer class
+ *
+ * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
+ */
+public class DefaultSerializerTest
+{
+ @Test
+ public void testDefaultSerializerIntegerString()
+ {
+ DefaultSerializer<Integer, String> serializer = new DefaultSerializer<Integer, String>( Integer.class,
+ String.class );
+
+ byte[] keyBytes = serializer.serializeKey( 25 );
+
+ assertEquals( 0x00, keyBytes[0] );
+ assertEquals( 0x00, keyBytes[1] );
+ assertEquals( 0x00, keyBytes[2] );
+ assertEquals( 0x19, keyBytes[3] );
+
+ byte[] valueBytes = serializer.serializeValue( "test" );
+
+ assertEquals( 0x00, valueBytes[0] );
+ assertEquals( 0x00, valueBytes[1] );
+ assertEquals( 0x00, valueBytes[2] );
+ assertEquals( 0x04, valueBytes[3] );
+ assertEquals( 't', valueBytes[4] );
+ assertEquals( 'e', valueBytes[5] );
+ assertEquals( 's', valueBytes[6] );
+ assertEquals( 't', valueBytes[7] );
+
+ int key = serializer.deserializeKey( keyBytes );
+
+ assertEquals( 25, key );
+
+ String value = serializer.deserializeValue( valueBytes );
+
+ assertEquals( "test", value );
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org