Skip to content

Commit

Permalink
More consistent responsibilities in GenericKey
Browse files Browse the repository at this point in the history
- GenericLayout has no logic at all, forwards all to GenericKey
- GenericKey accessor methods like: size, put, read, toString
  have "global" key state handling and calls internal versions
  of those methods for value logic
- CompositeGenericKey overrides internal methods and essentially
  just loops over all state slots, calling the internal method
  in each slot instead
  • Loading branch information
tinwelint committed Oct 2, 2018
1 parent 46bd5d4 commit f326e17
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 49 deletions.
Expand Up @@ -40,7 +40,7 @@ class CompositeGenericKey extends GenericKey
{
super( spatialSettings );
states = new GenericKey[slots];
for ( int i = 0; i < states.length; i++ )
for ( int i = 0; i < slots; i++ )
{
states[i] = new GenericKey( spatialSettings );
}
Expand All @@ -49,22 +49,22 @@ class CompositeGenericKey extends GenericKey
@Override
void writeValue( int stateSlot, Value value, Inclusion inclusion )
{
states[stateSlot].writeValue( value, inclusion );
stateSlot( stateSlot ).writeValue( value, inclusion );
}

@Override
void assertValidValue( int stateSlot, Value value )
{
Preconditions.requireBetween( stateSlot, 0, states.length );
Preconditions.requireBetween( stateSlot, 0, numberOfStateSlots() );
}

@Override
Value[] asValues()
{
Value[] values = new Value[states.length];
for ( int i = 0; i < states.length; i++ )
Value[] values = new Value[numberOfStateSlots()];
for ( int i = 0; i < values.length; i++ )
{
values[i] = states[i].asValue();
values[i] = stateSlot( i ).asValue();
}
return values;
}
Expand All @@ -82,11 +82,12 @@ void initValueAsHighest( int stateSlot, ValueGroup valueGroup )
}

@Override
int compareValueTo( GenericKey other )
int compareValueToInternal( GenericKey other )
{
for ( int i = 0; i < states.length; i++ )
int slots = numberOfStateSlots();
for ( int i = 0; i < slots; i++ )
{
int comparison = states[i].compareValueToInternal( other.stateSlot( i ) );
int comparison = stateSlot( i ).compareValueToInternal( other.stateSlot( i ) );
if ( comparison != 0 )
{
return comparison;
Expand All @@ -96,47 +97,50 @@ int compareValueTo( GenericKey other )
}

@Override
void copyFrom( GenericKey key )
void copyFromInternal( GenericKey key )
{
if ( key.numberOfStateSlots() != states.length )
int slots = numberOfStateSlots();
if ( key.numberOfStateSlots() != slots )
{
throw new IllegalArgumentException( "Different state lengths " + key.numberOfStateSlots() + " vs " + states.length );
throw new IllegalArgumentException( "Different state lengths " + key.numberOfStateSlots() + " vs " + slots );
}

for ( int i = 0; i < states.length; i++ )
for ( int i = 0; i < slots; i++ )
{
states[i].copyFromInternal( key.stateSlot( i ) );
stateSlot( i ).copyFromInternal( key.stateSlot( i ) );
}
}

@Override
int size()
int sizeInternal()
{
int size = ENTITY_ID_SIZE;
for ( GenericKey state : states )
int size = 0;
int slots = numberOfStateSlots();
for ( int i = 0; i < slots; i++ )
{
size += state.sizeInternal();
size += stateSlot( i ).sizeInternal();
}
return size;
}

@Override
void put( PageCursor cursor )
void putInternal( PageCursor cursor )
{
cursor.putLong( getEntityId() );
for ( GenericKey state : states )
int slots = numberOfStateSlots();
for ( int i = 0; i < slots; i++ )
{
state.putInternal( cursor );
stateSlot( i ).putInternal( cursor );
}
}

@Override
boolean get( PageCursor cursor, int keySize )
boolean getInternal( PageCursor cursor, int keySize )
{
int offset = cursor.getOffset();
for ( GenericKey state : states )
int slots = numberOfStateSlots();
for ( int i = 0; i < slots; i++ )
{
if ( !state.getInternal( cursor, keySize ) )
if ( !stateSlot( i ).getInternal( cursor, keySize ) )
{
initializeToDummyValue();
return false;
Expand All @@ -149,12 +153,12 @@ boolean get( PageCursor cursor, int keySize )
}

@Override
void initializeToDummyValue()
void initializeToDummyValueInternal()
{
setEntityId( Long.MIN_VALUE );
for ( GenericKey state : states )
int slots = numberOfStateSlots();
for ( int i = 0; i < slots; i++ )
{
state.initializeToDummyValueInternal();
stateSlot( i ).initializeToDummyValueInternal();
}
}

Expand All @@ -165,9 +169,9 @@ int numberOfStateSlots()
}

@Override
public String toString()
public String toStringInternal()
{
StringJoiner joiner = new StringJoiner( ",", "[", "]" );
StringJoiner joiner = new StringJoiner( "," );
for ( GenericKey state : states )
{
joiner.add( state.toStringInternal() );
Expand All @@ -176,7 +180,7 @@ public String toString()
}

@Override
void minimalSplitter( GenericKey left, GenericKey right, GenericKey into )
void minimalSplitterInternal( GenericKey left, GenericKey right, GenericKey into )
{
int firstStateToDiffer = 0;
int compare = 0;
Expand Down
Expand Up @@ -153,6 +153,7 @@ void clear()

void initializeToDummyValue()
{
setEntityId( Long.MIN_VALUE );
initializeToDummyValueInternal();
}

Expand Down Expand Up @@ -196,6 +197,8 @@ void initAsPrefixHigh( String prefix )
/* </initializers> */
void copyFrom( GenericKey key )
{
setEntityId( key.getEntityId() );
setCompareId( key.getCompareId() );
copyFromInternal( key );
}

Expand Down Expand Up @@ -304,6 +307,8 @@ int compareValueToInternal( GenericKey other )
void minimalSplitter( GenericKey left, GenericKey right, GenericKey into )
{
minimalSplitterInternal( left, right, into );
into.setCompareId( right.getCompareId() );
into.setEntityId( right.getEntityId() );
}

void minimalSplitterInternal( GenericKey left, GenericKey right, GenericKey into )
Expand Down Expand Up @@ -342,6 +347,14 @@ void putInternal( PageCursor cursor )

boolean get( PageCursor cursor, int size )
{
if ( size < ENTITY_ID_SIZE )
{
initializeToDummyValue();
cursor.setCursorException( format( "Failed to read " + getClass().getSimpleName() +
" due to keySize < ENTITY_ID_SIZE, more precisely %d", size ) );
}

initialize( cursor.getLong() );
return getInternal( cursor, size );
}

Expand Down Expand Up @@ -665,12 +678,12 @@ public void endArray()
@Override
public String toString()
{
return toStringInternal();
return "[" + toStringInternal() + "],entityId=" + getEntityId();
}

String toStringInternal()
{
return type.toString( this ) + "," + getEntityId();
return type.toString( this );
}

static void setCursorException( PageCursor cursor, String reason )
Expand Down
Expand Up @@ -22,9 +22,6 @@
import org.neo4j.io.pagecache.PageCursor;
import org.neo4j.kernel.impl.index.schema.config.IndexSpecificSpaceFillingCurveSettingsCache;

import static java.lang.String.format;
import static org.neo4j.kernel.impl.index.schema.NativeIndexKey.ENTITY_ID_SIZE;

class GenericLayout extends IndexLayout<GenericKey,NativeIndexValue>
{
private final int numberOfSlots;
Expand All @@ -50,8 +47,6 @@ public GenericKey newKey()
@Override
public GenericKey copyKey( GenericKey key, GenericKey into )
{
into.setEntityId( key.getEntityId() );
into.setCompareId( key.getCompareId() );
into.copyFrom( key );
return into;
}
Expand All @@ -71,14 +66,6 @@ public void writeKey( PageCursor cursor, GenericKey key )
@Override
public void readKey( PageCursor cursor, GenericKey into, int keySize )
{
if ( keySize < ENTITY_ID_SIZE )
{
into.initializeToDummyValue();
cursor.setCursorException( format( "Failed to read " + into.getClass().getSimpleName() +
" due to keySize < ENTITY_ID_SIZE, more precisely %d", keySize ) );
}

into.initialize( cursor.getLong() );
into.get( cursor, keySize );
}

Expand All @@ -92,8 +79,6 @@ public boolean fixedSize()
public void minimalSplitter( GenericKey left, GenericKey right, GenericKey into )
{
right.minimalSplitter( left, right, into );
into.setCompareId( right.getCompareId() );
into.setEntityId( right.getEntityId() );
}

IndexSpecificSpaceFillingCurveSettingsCache getSpaceFillingCurveSettings()
Expand Down

0 comments on commit f326e17

Please sign in to comment.