Skip to content

Commit

Permalink
Set cursor exception inside GenericKeyState
Browse files Browse the repository at this point in the history
... instead of in CompositeGenericKey to get more details about error.
  • Loading branch information
burqen committed Aug 22, 2018
1 parent 9110120 commit daa1e61
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ void read( PageCursor cursor, int keySize )
{
if ( keySize < ENTITY_ID_SIZE )
{
initializeToDummyValue( cursor, format( "keySize < ENTITY_ID_SIZE, more precisely %d", keySize ) );
initializeToDummyValue( cursor );
cursor.setCursorException( format( "Failed to read CompositeGenericKey due to keySize < ENTITY_ID_SIZE, more precisely %d", keySize ) );
return;
}

Expand All @@ -165,7 +166,7 @@ void read( PageCursor cursor, int keySize )
{
if ( !state.read( cursor, keySize ) )
{
initializeToDummyValue( cursor, format( "Unable to read state[%d] from offset:%d and keySize:%d", stateOffset, offset, keySize ) );
initializeToDummyValue( cursor );
return;
}
int offsetAfterRead = cursor.getOffset();
Expand All @@ -175,14 +176,13 @@ void read( PageCursor cursor, int keySize )
}
}

private void initializeToDummyValue( PageCursor cursor, String reason )
private void initializeToDummyValue( PageCursor cursor )
{
setEntityId( Long.MIN_VALUE );
for ( GenericKeyState state : states )
{
state.initializeToDummyValue();
}
cursor.setCursorException( format( "Initializing key state to dummy value due to %s", reason ) );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.neo4j.values.storable.Values;

import static java.lang.Integer.min;
import static java.lang.String.format;
import static org.neo4j.io.pagecache.PageCache.PAGE_SIZE;
import static org.neo4j.kernel.impl.index.schema.DurationIndexKey.AVG_DAY_SECONDS;
import static org.neo4j.kernel.impl.index.schema.DurationIndexKey.AVG_MONTH_SECONDS;
Expand Down Expand Up @@ -1011,13 +1012,13 @@ boolean read( PageCursor cursor, int size )
{
if ( size <= TYPE_ID_SIZE )
{
return false;
return setCursorException( cursor, "slot size less than TYPE_ID_SIZE, " + size );
}

byte typeId = cursor.getByte();
if ( typeId < 0 || typeId >= GenericLayout.TYPES.length )
{
return false;
return setCursorException( cursor, "non-valid typeId, " + typeId );
}

size -= TYPE_ID_SIZE;
Expand Down Expand Up @@ -1062,7 +1063,7 @@ boolean read( PageCursor cursor, int size )
case NUMBER_ARRAY:
return readNumberArray( cursor );
default:
return false;
return setCursorException( cursor, "non-valid type, " + type );
}
}

Expand Down Expand Up @@ -1090,7 +1091,7 @@ private boolean readNumberArray( PageCursor cursor )
ArrayType numberType = numberArrayTypeOf( (byte) long1 );
if ( numberType == null )
{
return false;
return setCursorException( cursor, "non-valid number type for array, " + long1 );
}
return readArray( cursor, numberType, numberArrayElementReader( long1 ) );
}
Expand Down Expand Up @@ -1174,7 +1175,7 @@ private boolean readTextArray( PageCursor cursor, int maxSize )
short bytesLength = cursor.getShort();
if ( bytesLength <= 0 || bytesLength > maxSize )
{
return false;
return setCursorException( cursor, "non-valid bytes length, " + bytesLength );
}

byteArrayArray[i] = ensureBigEnough( byteArrayArray[i], bytesLength );
Expand All @@ -1188,7 +1189,11 @@ private boolean readTextArray( PageCursor cursor, int maxSize )
private boolean setArrayLengthWhenReading( PageCursor cursor )
{
arrayLength = cursor.getInt();
return arrayLength >= 0 && arrayLength < BIGGEST_REASONABLE_ARRAY_LENGTH;
if ( arrayLength < 0 || arrayLength > BIGGEST_REASONABLE_ARRAY_LENGTH )
{
return setCursorException( cursor, "non-valid array length, " + arrayLength );
}
return true;
}

private boolean readNumber( PageCursor cursor )
Expand Down Expand Up @@ -1227,7 +1232,7 @@ private boolean readText( PageCursor cursor, int maxSize )
short bytesLength = cursor.getShort();
if ( bytesLength < 0 || bytesLength > maxSize )
{
return false;
return setCursorException( cursor, "non-valid bytes length for text, " + bytesLength );
}
setBytesLength( bytesLength );
cursor.getBytes( byteArray, 0, bytesLength );
Expand Down Expand Up @@ -1832,6 +1837,12 @@ private void copyZonedDateTimeArrayFrom( GenericKeyState key, int length )
/* </copyFrom.helpers> */

/* <helpers> */
private boolean setCursorException( PageCursor cursor, String reason )
{
cursor.setCursorException( format( "Unable to read generic key slot due to %s", reason ) );
return false;
}

private void setBytesLength( int length )
{
if ( booleanOf( long1 ) || byteArray == null || byteArray.length < length )
Expand Down

0 comments on commit daa1e61

Please sign in to comment.