Skip to content

Commit

Permalink
InternalTreeLogic, remove unused tmp byte[] fields and methods
Browse files Browse the repository at this point in the history
Move ByteArrayPageCursor to test
  • Loading branch information
burqen committed Dec 21, 2016
1 parent fe21efa commit d63715b
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 106 deletions.
Expand Up @@ -79,9 +79,6 @@ class InternalTreeLogic<KEY,VALUE>
{
private final IdProvider idProvider;
private final TreeNode<KEY,VALUE> bTreeNode;
private final byte[] tmpForKeys;
private final byte[] tmpForValues;
private final byte[] tmpForChildren;
private final Layout<KEY,VALUE> layout;
private final KEY primKeyPlaceHolder;
private final KEY readKey;
Expand All @@ -93,9 +90,6 @@ class InternalTreeLogic<KEY,VALUE>
this.bTreeNode = bTreeNode;
this.layout = layout;
int maxKeyCount = max( bTreeNode.internalMaxKeyCount(), bTreeNode.leafMaxKeyCount() );
this.tmpForKeys = new byte[(maxKeyCount + 1) * layout.keySize()];
this.tmpForValues = new byte[(maxKeyCount + 1) * layout.valueSize()];
this.tmpForChildren = new byte[(maxKeyCount + 2) * bTreeNode.childSize()];
this.primKeyPlaceHolder = layout.newKey();
this.readKey = layout.newKey();
this.readValue = layout.newValue();
Expand Down
Expand Up @@ -370,101 +370,12 @@ Comparator<KEY> keyComparator()
return layout;
}

int readKeysWithInsertRecordInPosition( PageCursor cursor, Consumer<PageCursor> newRecordWriter,
int insertPosition, int totalNumberOfRecords, byte[] into )
{
return readRecordsWithInsertRecordInPosition( cursor, newRecordWriter, insertPosition, totalNumberOfRecords,
keySize, keyOffset( 0 ), into );
}

int readValuesWithInsertRecordInPosition( PageCursor cursor, Consumer<PageCursor> newRecordWriter,
int insertPosition, int totalNumberOfRecords, byte[] into )
{
return readRecordsWithInsertRecordInPosition( cursor, newRecordWriter, insertPosition, totalNumberOfRecords,
valueSize, valueOffset( 0 ), into );
}

int readChildrenWithInsertRecordInPosition( PageCursor cursor, Consumer<PageCursor> newRecordWriter,
int insertPosition, int totalNumberOfRecords, byte[] into )
{
return readRecordsWithInsertRecordInPosition( cursor, newRecordWriter, insertPosition, totalNumberOfRecords,
childSize(), childOffset( 0 ), into );
}

void goTo( PageCursor cursor, String messageOnError, long nodeId )
throws IOException
{
PageCursorUtil.goTo( cursor, messageOnError, GenSafePointerPair.pointer( nodeId ) );
}

/**
* Leaves cursor on same page as when called. No guarantees on offset.
* <p>
* Create a byte[] with totalNumberOfRecords of recordSize from cursor reading from baseRecordOffset
* with newRecord inserted in insertPosition, with the following records shifted to the right.
* <p>
* Simply: Records of size recordSize that can be read from offset baseRecordOffset in page pinned by cursor has
* some ordering. This ordering is preserved with new record inserted in insertPosition in the returned byte[],
* NOT in the page.
*
* @param cursor {@link org.neo4j.io.pagecache.PageCursor} pinned to page to read records from
* @param newRecordWriter new data to be inserted in insertPosition in returned byte[].
* This is a {@link Consumer} since the type of data can differ (value/child),
* although perhaps this can be done in a better way
* @param insertPosition position of newRecord. 0 being before all other records,
* (totalNumberOfRecords - 1) being after all other records
* @param totalNumberOfRecords the total number of records to be contained in returned byte[], including newRecord
* @param recordSize the size in number of bytes of one record
* @param baseRecordOffset the offset from where cursor should start read records
* @param into byte array to copy bytes into
* @return number of bytes copied into the {@code into} byte[],
* that is insertPosition * recordSize
*/
private int readRecordsWithInsertRecordInPosition( PageCursor cursor, Consumer<PageCursor> newRecordWriter,
int insertPosition, int totalNumberOfRecords, int recordSize, int baseRecordOffset, byte[] into )
{
int length = (totalNumberOfRecords) * recordSize;

// First read all records

// Read all records on previous to insertPosition
cursor.setOffset( baseRecordOffset );
cursor.getBytes( into, 0, insertPosition * recordSize );

// Read newRecord
PageCursor buffer = ByteArrayPageCursor.wrap( into, insertPosition * recordSize, recordSize );
newRecordWriter.accept( buffer );

// Read all records following insertPosition
cursor.setOffset( baseRecordOffset + insertPosition * recordSize );
cursor.getBytes( into, (insertPosition + 1) * recordSize,
((totalNumberOfRecords - 1) - insertPosition) * recordSize );
return length;
}

private void writeAll( PageCursor cursor, byte[] source, int sourcePos, int targetPos, int count,
int baseOffset, int recordSize )
{
int arrayOffset = sourcePos * recordSize;
cursor.setOffset( baseOffset + recordSize * targetPos );
cursor.putBytes( source, arrayOffset, count * recordSize );
}

void writeKeys( PageCursor cursor, byte[] source, int sourcePos, int targetPos, int count )
{
writeAll( cursor, source, sourcePos, targetPos, count, keyOffset( 0 ), keySize() );
}

void writeValues( PageCursor cursor, byte[] source, int sourcePos, int targetPos, int count )
{
writeAll( cursor, source, sourcePos, targetPos, count, valueOffset( 0 ), valueSize() );
}

void writeChildren( PageCursor cursor, byte[] source, int sourcePos, int targetPos, int count )
{
writeAll( cursor, source, sourcePos, targetPos, count, childOffset( 0 ), childSize() );
}

@Override
public String toString()
{
Expand Down
Expand Up @@ -28,10 +28,6 @@
import org.neo4j.io.pagecache.PageCursor;

/**
* TODO: Adding another implementation of PageCursor (this implementation)
* TODO: will mean Neo as a whole have three implementations in total.
* TODO: This results in a slower method dispatch for ALL PageCursors. Is it worth it?
* <p>
* Wraps a byte array and present it as a PageCursor.
* <p>
* This class is bridging something which would otherwise make {@link InternalTreeLogic} code slightly more
Expand Down
Expand Up @@ -376,8 +376,8 @@ public void shouldReadAndInsertKeys() throws Exception
node.insertKeyAt( cursor, key, 1, 1 );

// WHEN
node.readKeysWithInsertRecordInPosition( cursor, c -> c.putLong( 2 ), 1, 3, tmp );
node.writeKeys( cursor, tmp, 0, 0, 3 );
key.setValue( 2 );
node.insertKeyAt( cursor, key, 1, 2 );

// THEN
assertEquals( 1, node.keyAt( cursor, key, 0 ).longValue() );
Expand All @@ -397,8 +397,8 @@ public void shouldReadAndInsertValues() throws Exception
node.insertValueAt( cursor, value, 1, 1 );

// WHEN
node.readValuesWithInsertRecordInPosition( cursor, c -> c.putLong( 2 ), 1, 3, tmp );
node.writeValues( cursor, tmp, 0, 0, 3 );
value.setValue( 2 );
node.insertValueAt( cursor, value, 1, 2 );

// THEN
assertEquals( 1, node.valueAt( cursor, value, 0 ).longValue() );
Expand All @@ -418,9 +418,7 @@ public void shouldReadAndInsertChildren() throws Exception
node.insertChildAt( cursor, thirdChild, 1, 1, STABLE_GENERATION, UNSTABLE_GENERATION );

// WHEN
node.readChildrenWithInsertRecordInPosition( cursor,
c -> node.writeChild( c, secondChild, STABLE_GENERATION, UNSTABLE_GENERATION ), 1, 3, tmp );
node.writeChildren( cursor, tmp, 0, 0, 3 );
node.insertChildAt( cursor, secondChild, 1, 2, STABLE_GENERATION, UNSTABLE_GENERATION );

// THEN
assertEquals( firstChild, childAt( cursor, 0, STABLE_GENERATION, UNSTABLE_GENERATION ) );
Expand Down

0 comments on commit d63715b

Please sign in to comment.