Skip to content

Commit

Permalink
Refactor the store trailer handling code
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvest committed Aug 4, 2015
1 parent 0486e14 commit ede7761
Showing 1 changed file with 28 additions and 28 deletions.
Expand Up @@ -79,21 +79,21 @@ public static String readTrailer( PagedFile pagedFile, String expectedTrailer )


try ( PageCursor pageCursor = pagedFile.io( firstPageThatTrailerMightBeIn, PagedFile.PF_SHARED_LOCK ) ) try ( PageCursor pageCursor = pagedFile.io( firstPageThatTrailerMightBeIn, PagedFile.PF_SHARED_LOCK ) )
{ {
byte[] allData = getTheBytesThatWillContainTheTrailer( pageSize, firstPageThatTrailerMightBeIn, lastPageId, byte[] data = getTheBytesThatWillContainTheTrailer(
pageCursor ); pageSize, firstPageThatTrailerMightBeIn, lastPageId, pageCursor );
int trailerPositionRelativeToFirstPageTrailerMightBeIn = int trailerPositionRelativeToFirstPageTrailerMightBeIn =
findTrailerPositionInArray( allData, UTF8.encode( expectedTrailer.split( " " )[0] ) ); findTrailerPositionInArray( data, UTF8.encode( expectedTrailer.split( " " )[0] ) );
if ( trailerPositionRelativeToFirstPageTrailerMightBeIn != -1 ) if ( trailerPositionRelativeToFirstPageTrailerMightBeIn != -1 )
{ {
version = new String( allData, trailerPositionRelativeToFirstPageTrailerMightBeIn, version = new String( data, trailerPositionRelativeToFirstPageTrailerMightBeIn,
encodedExpectedTrailerLength, Charsets.UTF_8 ); encodedExpectedTrailerLength, Charsets.UTF_8 );
} }
} }
return version; return version;
} }


/** /**
* Write the given trailer att the given offset into the file. * Write the given trailer at the given offset into the file.
* *
* @param pagedFile The paged file to write the trailer into * @param pagedFile The paged file to write the trailer into
* @param trailer The trailer to be written, encoded in UTF-8 * @param trailer The trailer to be written, encoded in UTF-8
Expand All @@ -103,26 +103,28 @@ public static String readTrailer( PagedFile pagedFile, String expectedTrailer )
public static void writeTrailer( PagedFile pagedFile, byte[] trailer, long trailerOffset ) throws IOException public static void writeTrailer( PagedFile pagedFile, byte[] trailer, long trailerOffset ) throws IOException
{ {
int pageSize = pagedFile.pageSize(); int pageSize = pagedFile.pageSize();
long pageIdTrailerStartsIn = trailerOffset / pageSize; long startPageId = trailerOffset / pageSize;
int pageOffset = (int) (trailerOffset % pageSize);
int bytesLeftToWrite = trailer.length;


try ( PageCursor pageCursor = pagedFile.io( pageIdTrailerStartsIn, PagedFile.PF_EXCLUSIVE_LOCK ) ) try ( PageCursor cursor = pagedFile.io( startPageId, PagedFile.PF_EXCLUSIVE_LOCK ) )
{ {
int writtenOffset = 0; do
while ( writtenOffset < trailer.length )
{ {
pageCursor.next(); cursor.next(); // Should always be true because we take an exclusive lock
pageCursor.setOffset( (int) ((writtenOffset + trailerOffset) % pageSize) ); cursor.setOffset( pageOffset );

int bytesInThisPage = Math.min( bytesLeftToWrite, pageSize - pageOffset );
do do
{ {
do cursor.putBytes( trailer, trailer.length - bytesLeftToWrite, bytesInThisPage );
{
pageCursor.putByte( trailer[writtenOffset] );
}
while ( pageCursor.shouldRetry() );
writtenOffset++;
} }
while ( (writtenOffset + trailerOffset) % pageSize != 0 && writtenOffset < trailer.length ); while ( cursor.shouldRetry() );

bytesLeftToWrite -= bytesInThisPage;
pageOffset = 0;
} }
while ( bytesLeftToWrite > 0 );
} }
} }


Expand All @@ -133,12 +135,12 @@ private static long getFirstPageThatTrailerMightBeIn( long lastPageId, int pageS
return Math.max( lastPageId + 1 - maximumNumberOfPagesVersionSpans, 0 ); return Math.max( lastPageId + 1 - maximumNumberOfPagesVersionSpans, 0 );
} }


private static int findTrailerPositionInArray( byte[] dataThatShouldContailTrailer, byte[] trailer ) private static int findTrailerPositionInArray( byte[] dataThatShouldContainTrailer, byte[] trailer )
{ {
for ( int i = dataThatShouldContailTrailer.length - trailer.length; i >= 0; i-- ) for ( int i = dataThatShouldContainTrailer.length - trailer.length; i >= 0; i-- )
{ {
int pos = 0; int pos = 0;
while ( pos < trailer.length && dataThatShouldContailTrailer[i + pos] == trailer[pos] ) while ( pos < trailer.length && dataThatShouldContainTrailer[i + pos] == trailer[pos] )
{ {
pos++; pos++;
} }
Expand All @@ -158,19 +160,17 @@ private static int getMaximumNumberOfPagesVersionSpans( int trailerLength, int p
private static byte[] getTheBytesThatWillContainTheTrailer( int pageSize, long firstPageThatTrailerMightBeIn, private static byte[] getTheBytesThatWillContainTheTrailer( int pageSize, long firstPageThatTrailerMightBeIn,
long lastPageId, PageCursor pageCursor ) throws IOException long lastPageId, PageCursor pageCursor ) throws IOException
{ {
byte[] allData = new byte[(int) (pageSize * (lastPageId - firstPageThatTrailerMightBeIn + 1))]; byte[] data = new byte[(int) (pageSize * (lastPageId - firstPageThatTrailerMightBeIn + 1))];
byte[] data = new byte[pageSize]; int arrayOffset = 0;
int currentPage = 0;
while ( pageCursor.next() ) while ( pageCursor.next() )
{ {
do do
{ {
pageCursor.getBytes( data ); pageCursor.getBytes( data, arrayOffset, pageSize );
} }
while ( pageCursor.shouldRetry() ); while ( pageCursor.shouldRetry() );
System.arraycopy( data, 0, allData, currentPage * data.length, data.length ); arrayOffset += pageSize;
currentPage++;
} }
return allData; return data;
} }
} }

0 comments on commit ede7761

Please sign in to comment.