Skip to content

Commit

Permalink
Make sure that NeoStore does not parse non-record data as record data.
Browse files Browse the repository at this point in the history
  • Loading branch information
ragadeeshu committed Jul 23, 2015
1 parent 1979436 commit 5c215aa
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
Expand Up @@ -73,6 +73,7 @@ public abstract static class Configuration

public static final String TYPE_DESCRIPTOR = "NeoStore";
// This value means the field has not been refreshed from the store. Normally, this should happen only once
public static final long FIELD_NOT_PRESENT = -1;
public static final long FIELD_NOT_INITIALIZED = Long.MIN_VALUE;
/*
* 9 longs in header (long + in use), time | random | version | txid | store version | graph next prop | latest
Expand Down Expand Up @@ -475,7 +476,7 @@ record = cursor.getLong();
{
throw new RuntimeException( e );
}
return -1;
return FIELD_NOT_PRESENT;
}

private static int getPageSize( PageCache pageCache )
Expand Down Expand Up @@ -642,10 +643,13 @@ private void readAllFields( PageCursor cursor ) throws IOException

private long getRecordValue( PageCursor cursor, Position position )
{
// The "+ 1" to skip over the inUse byte.
int offset = position.id * getEffectiveRecordSize() + 1;
int offset = position.id * getEffectiveRecordSize();
cursor.setOffset( offset );
return cursor.getLong();
if ( cursor.getByte() == Record.IN_USE.byteValue() )
{
return cursor.getLong();
}
return FIELD_NOT_PRESENT;
}

private void incrementVersion( PageCursor cursor ) throws IOException
Expand Down
Expand Up @@ -1098,6 +1098,46 @@ public void setVersion() throws Exception
neoStore.close();
}

@Test
public void shouldNotReadNonRecordDataAsRecord() throws Exception
{
FileSystemAbstraction fileSystem = fs.get();
File neoStoreDir = new File( "/tmp/graph.db/neostore" ).getAbsoluteFile();
StoreFactory factory =
new StoreFactory( fileSystem, neoStoreDir, pageCache, NullLogProvider.getInstance(), new Monitors() );
NeoStore neoStore = factory.newNeoStore( true );
neoStore.setCreationTime( 3 );
neoStore.setRandomNumber( 4 );
neoStore.setCurrentLogVersion( 5 );
neoStore.setLastCommittedAndClosedTransactionId( 6, 0, 0, 0 );
neoStore.setStoreVersion( 7 );
neoStore.setGraphNextProp( 8 );
neoStore.setLatestConstraintIntroducingTx( 9 );
neoStore.rebuildCountStoreIfNeeded();
neoStore.flush();
neoStore.close();

File file = new File( neoStoreDir, NeoStore.DEFAULT_NAME );
try ( StoreChannel channel = fileSystem.open( file, "rw" ) )
{
byte[] trailer =
UTF8.encode( CommonAbstractStore.buildTypeDescriptorAndVersion( neoStore.getTypeDescriptor() ) );
channel.position( 0 );
channel.write( ByteBuffer.wrap( UTF8.encode( "This is some data that is not a record." ) ) );
}

neoStore = factory.newNeoStore( false );
assertEquals( NeoStore.FIELD_NOT_PRESENT, neoStore.getCreationTime() );
assertEquals( NeoStore.FIELD_NOT_PRESENT, neoStore.getRandomNumber() );
assertEquals( NeoStore.FIELD_NOT_PRESENT, neoStore.getCurrentLogVersion() );
assertEquals( NeoStore.FIELD_NOT_PRESENT, neoStore.getLastCommittedTransactionId() );
assertEquals( NeoStore.FIELD_NOT_PRESENT, neoStore.getStoreVersion() );
assertEquals( 8, neoStore.getGraphNextProp() );
assertEquals( 9, neoStore.getLatestConstraintIntroducingTx() );

neoStore.close();
}

@Test
public void testSetLatestConstraintTx() throws Exception
{
Expand Down

0 comments on commit 5c215aa

Please sign in to comment.