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 2013/03/06 15:54:20 UTC

svn commit: r1453356 - in /labs/mavibot/trunk/mavibot/src: main/java/org/apache/mavibot/btree/store/RecordManager.java test/java/org/apache/mavibot/btree/store/ReadTest.java

Author: elecharny
Date: Wed Mar  6 14:54:20 2013
New Revision: 1453356

URL: http://svn.apache.org/r1453356
Log:
Added the readLong() method and the associated test

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=1453356&r1=1453355&r2=1453356&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 14:54:20 2013
@@ -441,6 +441,12 @@ public class RecordManager
     }
 
 
+    /**
+     * 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
+     * @return The int we have read
+     */
     private int readInt( PageIO[] pageIos, long position )
     {
         // Compute the page in which we will store the data given the 
@@ -502,6 +508,102 @@ public class RecordManager
 
 
     /**
+     * Read a long from pages 
+     * @param pageIos The pages we want to read the long from
+     * @param position The position in the data stored in those pages
+     * @return The long we have read
+     */
+    private long readLong( PageIO[] pageIos, long position )
+    {
+        // 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;
+        long value = 0L;
+
+        if ( remaining >= LONG_SIZE )
+        {
+            value = pageData.getLong( pagePos );
+        }
+        else
+        {
+            switch ( remaining )
+            {
+                case 7:
+                    value += ( ( ( long ) pageData.get( pagePos + 6 ) & 0x00FF ) << 8 );
+                    // Fallthrough !!!
+
+                case 6:
+                    value += ( ( ( long ) pageData.get( pagePos + 5 ) & 0x00FF ) << 16 );
+                    // Fallthrough !!!
+
+                case 5:
+                    value += ( ( ( long ) pageData.get( pagePos + 4 ) & 0x00FF ) << 24 );
+                    // Fallthrough !!!
+
+                case 4:
+                    value += ( ( ( long ) pageData.get( pagePos + 3 ) & 0x00FF ) << 32 );
+                    // Fallthrough !!!
+
+                case 3:
+                    value += ( ( ( long ) pageData.get( pagePos + 2 ) & 0x00FF ) << 40 );
+                    // Fallthrough !!!
+
+                case 2:
+                    value += ( ( ( long ) pageData.get( pagePos + 1 ) & 0x00FF ) << 48 );
+                    // Fallthrough !!!
+
+                case 1:
+                    value += ( ( long ) pageData.get( pagePos ) << 56 );
+                    break;
+            }
+
+            // Now deal with the next page
+            pageData = pageIos[pageNb + 1].getData();
+            pagePos = LINK_SIZE;
+
+            switch ( remaining )
+            {
+                case 1:
+                    value += ( ( long ) pageData.get( pagePos ) & 0x00FF ) << 48;
+                    // fallthrough !!!
+
+                case 2:
+                    value += ( ( long ) pageData.get( pagePos + 2 - remaining ) & 0x00FF ) << 40;
+                    // fallthrough !!!
+
+                case 3:
+                    value += ( ( long ) pageData.get( pagePos + 3 - remaining ) & 0x00FF ) << 32;
+                    // fallthrough !!!
+
+                case 4:
+                    value += ( ( long ) pageData.get( pagePos + 4 - remaining ) & 0x00FF ) << 24;
+                    // fallthrough !!!
+
+                case 5:
+                    value += ( ( long ) pageData.get( pagePos + 5 - remaining ) & 0x00FF ) << 16;
+                    // fallthrough !!!
+
+                case 6:
+                    value += ( ( long ) pageData.get( pagePos + 6 - remaining ) & 0x00FF ) << 8;
+                    // fallthrough !!!
+
+                case 7:
+                    value += ( ( long ) pageData.get( pagePos + 7 - remaining ) & 0x00FF );
+                    break;
+            }
+        }
+
+        return value;
+    }
+
+
+    /**
      * Manage a BTree. The btree will be added and managed by this RecordManager. We will create a 
      * new RootPage for this added BTree, which will contain no data. 
      *  

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=1453356&r1=1453355&r2=1453356&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 14:54:20 2013
@@ -61,7 +61,7 @@ public class ReadTest
         pageIos[1].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
 
         // Set the int at the beginning
-        long position = ( Long ) storeMethod.invoke( recordManager, pageIos, 0, 0x12345678 );
+        storeMethod.invoke( recordManager, pageIos, 0, 0x12345678 );
 
         // Read it back
         int readValue = ( Integer ) readIntMethod.invoke( recordManager, pageIos, 0 );
@@ -69,7 +69,7 @@ public class ReadTest
         assertEquals( 0x12345678, readValue );
 
         // Set the int at the end of the first page
-        position = ( Long ) storeMethod.invoke( recordManager, pageIos, 16, 0x12345678 );
+        storeMethod.invoke( recordManager, pageIos, 16, 0x12345678 );
 
         // Read it back
         readValue = ( Integer ) readIntMethod.invoke( recordManager, pageIos, 16 );
@@ -78,7 +78,7 @@ public class ReadTest
 
         // Set the int at the end of the first page and overlapping on the second page
         // 1 byte overlapping
-        position = ( Long ) storeMethod.invoke( recordManager, pageIos, 17, 0x12345678 );
+        storeMethod.invoke( recordManager, pageIos, 17, 0x12345678 );
 
         // Read it back
         readValue = ( Integer ) readIntMethod.invoke( recordManager, pageIos, 17 );
@@ -87,7 +87,7 @@ public class ReadTest
 
         // Set the int at the end of the first page and overlapping on the second page
         // 2 bytes overlapping
-        position = ( Long ) storeMethod.invoke( recordManager, pageIos, 18, 0x12345678 );
+        storeMethod.invoke( recordManager, pageIos, 18, 0x12345678 );
 
         // Read it back
         readValue = ( Integer ) readIntMethod.invoke( recordManager, pageIos, 18 );
@@ -96,7 +96,7 @@ public class ReadTest
 
         // Set the int at the end of the first page and overlapping on the second page
         // 3 bytes overlapping
-        position = ( Long ) storeMethod.invoke( recordManager, pageIos, 19, 0x12345678 );
+        storeMethod.invoke( recordManager, pageIos, 19, 0x12345678 );
 
         // Read it back
         readValue = ( Integer ) readIntMethod.invoke( recordManager, pageIos, 19 );
@@ -104,9 +104,120 @@ public class ReadTest
         assertEquals( 0x12345678, readValue );
 
         // Set the int at the beginning of the second page
-        position = ( Long ) storeMethod.invoke( recordManager, pageIos, 20, 0x12345678 );
+        storeMethod.invoke( recordManager, pageIos, 20, 0x12345678 );
 
         // Read it back
         readValue = ( Integer ) readIntMethod.invoke( recordManager, pageIos, 20 );
     }
+
+
+    /**
+     * Test the readLong method
+     */
+    @Test
+    public void testReadLong() throws Exception
+    {
+        File tempFile = File.createTempFile( "mavibot", ".db" );
+        String tempFileName = tempFile.getAbsolutePath();
+        tempFile.deleteOnExit();
+
+        // Create page size of 32 only
+        RecordManager recordManager = new RecordManager( tempFileName, 32 );
+        Method storeMethod = RecordManager.class.getDeclaredMethod( "store", PageIO[].class, long.class, long.class );
+        Method readLongMethod = RecordManager.class.getDeclaredMethod( "readLong", PageIO[].class, long.class );
+        storeMethod.setAccessible( true );
+        readLongMethod.setAccessible( true );
+
+        // Allocate some Pages
+        PageIO[] pageIos = new PageIO[2];
+        pageIos[0] = new PageIO();
+        pageIos[0].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
+        pageIos[1] = new PageIO();
+        pageIos[1].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
+
+        // Set the int at the beginning
+        storeMethod.invoke( recordManager, pageIos, 0, 0x0123456789ABCDEFL );
+
+        // Read it back
+        long readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 0 );
+
+        assertEquals( 0x0123456789ABCDEFL, readValue );
+
+        // Set the int at the end of the first page
+        storeMethod.invoke( recordManager, pageIos, 12, 0x0123456789ABCDEFL );
+
+        // Read it back
+        readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 12 );
+
+        assertEquals( 0x0123456789ABCDEFL, readValue );
+
+        // Set the int at the end of the first page and overlapping on the second page
+        // 1 byte overlapping
+        storeMethod.invoke( recordManager, pageIos, 13, 0x0123456789ABCDEFL );
+
+        // Read it back
+        readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 13 );
+
+        assertEquals( 0x0123456789ABCDEFL, readValue );
+
+        // Set the int at the end of the first page and overlapping on the second page
+        // 2 bytes overlapping
+        storeMethod.invoke( recordManager, pageIos, 14, 0x0123456789ABCDEFL );
+
+        // Read it back
+        readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 14 );
+
+        assertEquals( 0x0123456789ABCDEFL, readValue );
+
+        // Set the int at the end of the first page and overlapping on the second page
+        // 3 bytes overlapping
+        storeMethod.invoke( recordManager, pageIos, 15, 0x0123456789ABCDEFL );
+
+        // Read it back
+        readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 15 );
+
+        assertEquals( 0x0123456789ABCDEFL, readValue );
+
+        // Set the int at the end of the first page and overlapping on the second page
+        // 4 bytes overlapping
+        storeMethod.invoke( recordManager, pageIos, 16, 0x0123456789ABCDEFL );
+
+        // Read it back
+        readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 16 );
+
+        assertEquals( 0x0123456789ABCDEFL, readValue );
+
+        // Set the int at the end of the first page and overlapping on the second page
+        // 5 bytes overlapping
+        storeMethod.invoke( recordManager, pageIos, 17, 0x0123456789ABCDEFL );
+
+        // Read it back
+        readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 17 );
+
+        assertEquals( 0x0123456789ABCDEFL, readValue );
+
+        // Set the int at the end of the first page and overlapping on the second page
+        // 6 bytes overlapping
+        storeMethod.invoke( recordManager, pageIos, 18, 0x0123456789ABCDEFL );
+
+        // Read it back
+        readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 18 );
+
+        assertEquals( 0x0123456789ABCDEFL, readValue );
+
+        // Set the int at the end of the first page and overlapping on the second page
+        // 7 bytes overlapping
+        storeMethod.invoke( recordManager, pageIos, 19, 0x0123456789ABCDEFL );
+
+        // Read it back
+        readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 19 );
+
+        assertEquals( 0x0123456789ABCDEFL, readValue );
+
+        // Set the int at the beginning of the second page
+        storeMethod.invoke( recordManager, pageIos, 20, 0x0123456789ABCDEFL );
+
+        // Read it back
+        readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 20 );
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org