Skip to content

Commit

Permalink
Extracts writing of header into separate method
Browse files Browse the repository at this point in the history
  • Loading branch information
tinwelint committed Mar 7, 2017
1 parent d1a2593 commit d10305c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
Expand Up @@ -446,34 +446,39 @@ private void writeState( PagedFile pagedFile, Header.Writer headerWriter ) throw
freeList.lastId(), freeList.writePageId(), freeList.readPageId(),
freeList.writePos(), freeList.readPos() );

// Write/carry over header
int headerOffset = cursor.getOffset();
int headerDataOffset = headerOffset + Integer.BYTES; // will contain length of written header data (below)
TreeState otherState = other( states, oldestState );
if ( otherState.isValid() )
{
PageCursor previousCursor = pagedFile.io( otherState.pageId(), PagedFile.PF_SHARED_READ_LOCK );
PageCursorUtil.goTo( previousCursor, "previous state page", otherState.pageId() );
do
{
// Place the previous state cursor after state data
TreeState.read( previousCursor );
// Read length of previous header
int previousLength = previousCursor.getInt();
// Reserve space to store length
cursor.setOffset( headerDataOffset );
// Write
headerWriter.write( previousCursor, previousLength, cursor );
}
while ( previousCursor.shouldRetry() );
checkOutOfBounds( previousCursor );
checkOutOfBounds( cursor );
writerHeader( pagedFile, headerWriter, other( states, oldestState ), cursor );

int length = cursor.getOffset() - headerDataOffset;
cursor.putInt( headerOffset, length );
}
checkOutOfBounds( cursor );
}
}

private static void writerHeader( PagedFile pagedFile, Header.Writer headerWriter,
TreeState otherState, PageCursor cursor ) throws IOException
{
// Write/carry over header
int headerOffset = cursor.getOffset();
int headerDataOffset = headerOffset + Integer.BYTES; // will contain length of written header data (below)
if ( otherState.isValid() )
{
PageCursor previousCursor = pagedFile.io( otherState.pageId(), PagedFile.PF_SHARED_READ_LOCK );
PageCursorUtil.goTo( previousCursor, "previous state page", otherState.pageId() );
do
{
// Place the previous state cursor after state data
TreeState.read( previousCursor );
// Read length of previous header
int previousLength = previousCursor.getInt();
// Reserve space to store length
cursor.setOffset( headerDataOffset );
// Write
headerWriter.write( previousCursor, previousLength, cursor );
}
while ( previousCursor.shouldRetry() );
checkOutOfBounds( previousCursor );
checkOutOfBounds( cursor );

int length = cursor.getOffset() - headerDataOffset;
cursor.putInt( headerOffset, length );
}
}

Expand Down
Expand Up @@ -46,8 +46,6 @@ public interface Writer
void write( PageCursor from, int length, PageCursor to );
}

final Consumer<PageCursor> CARRY_OVER = cursor -> {};

static final Writer CARRY_OVER_PREVIOUS_HEADER = (from,length,to) ->
{
from.copyTo( from.getOffset(), to, to.getOffset(), length );
Expand Down

0 comments on commit d10305c

Please sign in to comment.