Author: elecharny
Date: Wed Mar 6 17:15:50 2013
New Revision: 1453434
URL: http://svn.apache.org/r1453434
Log:
Added the readBytes() method and the associated tests
Modified:
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/store/RecordManager.java
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/store/ReadTest.java
Modified:
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/store/RecordManager.java
URL:
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/store/RecordManager.java?rev=1453434&r1=1453433&r2=1453434&view=diff
==============================================================================
---
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/store/RecordManager.java
(original)
+++
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/store/RecordManager.java
Wed Mar 6 17:15:50 2013
@@ -367,7 +367,6 @@ public class RecordManager
private long loadBTree( PageIO[] pageIos, long position, BTree<?, ?> btree
)
{
// The tree name
- /*
byte[] btreeNameBytes = readBytes( pageIos, position );
position += INT_SIZE;
@@ -427,21 +426,81 @@ public class RecordManager
// The nb elems in the tree
long nbElems = readLong( pageIos, position );
- btree.setNbElems( nbElems )
+ btree.setNbElems( nbElems );
position += LONG_SIZE;
// The BTree rootPage offset
long rootPageOffset = readLong( pageIos, position );
-
- List<PageIO> rootPage = readPages( rootPageOffset );
+
+ //List<PageIO> rootPage = readPages( rootPageOffset );
position += LONG_SIZE;
- */
return position;
}
/**
+ * Read a byte[] from pages.
+ * @param pageIos The pages we want to read the byte[] from
+ * @param position The position in the data stored in those pages
+ * @return The byte[] we have read
+ */
+ private byte[] readBytes( PageIO[] pageIos, long position )
+ {
+ // Read the byte[] length first
+ int length = readInt( pageIos, position );
+ position += INT_SIZE;
+
+ // Compute the page in which we will store the data given the
+ // current position
+ int pageNb = computePageNb( position );
+
+ // Compute the position in the current page
+ int pagePos = ( int ) ( position + ( pageNb + 1 ) * 8 + 4 ) - pageNb *
pageSize;
+
+ ByteBuffer pageData = pageIos[pageNb].getData();
+ int remaining = pageData.capacity() - pagePos;
+
+ if ( length == 0 )
+ {
+ // No bytes to read : return null;
+ return null;
+ }
+ else
+ {
+ byte[] bytes = new byte[length];
+ int bytesPos = 0;
+
+ while ( length > 0 )
+ {
+ if ( length <= remaining )
+ {
+ pageData.mark();
+ pageData.position( pagePos );
+ pageData.get( bytes, bytesPos, length );
+ pageData.reset();
+
+ return bytes;
+ }
+
+ pageData.mark();
+ pageData.position( pagePos );
+ pageData.get( bytes, bytesPos, remaining );
+ pageData.reset();
+ pageNb++;
+ pagePos = LINK_SIZE;
+ bytesPos += remaining;
+ pageData = pageIos[pageNb].getData();
+ length -= remaining;
+ remaining = pageData.capacity() - pagePos;
+ }
+
+ return bytes;
+ }
+ }
+
+
+ /**
* Read an int from pages
* @param pageIos The pages we want to read the int from
* @param position The position in the data stored in those pages
Modified:
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/store/ReadTest.java
URL:
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/store/ReadTest.java?rev=1453434&r1=1453433&r2=1453434&view=diff
==============================================================================
---
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/store/ReadTest.java
(original)
+++
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/store/ReadTest.java
Wed Mar 6 17:15:50 2013
@@ -21,6 +21,7 @@ package org.apache.mavibot.btree.store;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
import java.io.File;
import java.lang.reflect.Method;
@@ -220,4 +221,132 @@ public class ReadTest
// Read it back
readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 20
);
}
+
+
+ /**
+ * Test the readBytes() method
+ */
+ @Test
+ public void testReadBytes() throws Exception
+ {
+ File tempFile = File.createTempFile( "mavibot", ".db" );
+ String tempFileName = tempFile.getAbsolutePath();
+ tempFile.deleteOnExit();
+
+ // We use smaller pages
+ RecordManager recordManager = new RecordManager( tempFileName, 32 );
+ Method storeMethod = RecordManager.class.getDeclaredMethod( "store",
PageIO[].class, long.class, byte[].class );
+ Method readBytesMethod = RecordManager.class.getDeclaredMethod(
"readBytes", PageIO[].class, long.class );
+ storeMethod.setAccessible( true );
+ readBytesMethod.setAccessible( true );
+
+ // Allocate some Pages
+ PageIO[] pageIos = new PageIO[4];
+ pageIos[0] = new PageIO();
+ pageIos[0].setData( ByteBuffer.allocate( recordManager.getPageSize() )
);
+ pageIos[1] = new PageIO();
+ pageIos[1].setData( ByteBuffer.allocate( recordManager.getPageSize() )
);
+ pageIos[2] = new PageIO();
+ pageIos[2].setData( ByteBuffer.allocate( recordManager.getPageSize() )
);
+ pageIos[3] = new PageIO();
+ pageIos[3].setData( ByteBuffer.allocate( recordManager.getPageSize() )
);
+
+ // We start with 4 bytes
+ byte[] bytes = new byte[]
+ { 0x01, 0x23, 0x45, 0x67 };
+
+ // Set the bytes at the beginning
+ long position = ( Long ) storeMethod.invoke( recordManager, pageIos,
0L, bytes );
+
+ // Read the bytes back
+ byte[] readBytes = ( byte[] ) readBytesMethod.invoke( recordManager,
pageIos, 0L );
+
+ // The byte length
+ int pos = 0;
+ assertNotNull( readBytes );
+ assertEquals( 4, readBytes.length );
+ // The data
+ assertEquals( 0x01, readBytes[pos++] );
+ assertEquals( 0x23, readBytes[pos++] );
+ assertEquals( 0x45, readBytes[pos++] );
+ assertEquals( 0x67, readBytes[pos++] );
+
+ // Set the bytes at the end of the first page
+ position = ( Long ) storeMethod.invoke( recordManager, pageIos, 12L,
bytes );
+
+ // Read the bytes back
+ readBytes = ( byte[] ) readBytesMethod.invoke( recordManager, pageIos,
12L );
+
+ // The byte length
+ pos = 0;
+ assertNotNull( readBytes );
+ assertEquals( 4, readBytes.length );
+ // The data
+ assertEquals( 0x01, readBytes[pos++] );
+ assertEquals( 0x23, readBytes[pos++] );
+ assertEquals( 0x45, readBytes[pos++] );
+ assertEquals( 0x67, readBytes[pos++] );
+
+ // Set A full page of bytes in the first page
+ bytes = new byte[16];
+
+ for ( int i = 0; i < 16; i++ )
+ {
+ bytes[i] = ( byte ) ( i + 1 );
+ }
+
+ position = ( Long ) storeMethod.invoke( recordManager, pageIos, 0L,
bytes );
+
+ // Read the bytes back
+ readBytes = ( byte[] ) readBytesMethod.invoke( recordManager, pageIos,
0L );
+
+ // The byte length
+ pos = 0;
+ assertNotNull( readBytes );
+ assertEquals( 16, readBytes.length );
+ // The data
+ for ( int i = 0; i < 16; i++ )
+ {
+ assertEquals( i + 1, readBytes[pos++] );
+ }
+
+ // Write the bytes over 2 pages
+ position = ( Long ) storeMethod.invoke( recordManager, pageIos, 15L,
bytes );
+
+ // Read the bytes back
+ readBytes = ( byte[] ) readBytesMethod.invoke( recordManager, pageIos,
15L );
+
+ // The byte length
+ pos = 0;
+ assertNotNull( readBytes );
+ assertEquals( 16, readBytes.length );
+ // The data
+ for ( int i = 0; i < 16; i++ )
+ {
+ assertEquals( i + 1, readBytes[pos++] );
+ }
+
+ // Write the bytes over 4 pages
+ bytes = new byte[80];
+
+ for ( int i = 0; i < 80; i++ )
+ {
+ bytes[i] = ( byte ) ( i + 1 );
+ }
+
+ position = ( Long ) storeMethod.invoke( recordManager, pageIos, 2L,
bytes );
+
+ // Read the bytes back
+ readBytes = ( byte[] ) readBytesMethod.invoke( recordManager, pageIos,
2L );
+
+ // The byte length
+ pos = 0;
+ assertNotNull( readBytes );
+ assertEquals( 80, readBytes.length );
+ // The data
+ for ( int i = 0; i < 80; i++ )
+ {
+ assertEquals( i + 1, readBytes[pos++] );
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]