Skip to content

Commit

Permalink
Add before state to neo store commands
Browse files Browse the repository at this point in the history
  • Loading branch information
davidegrohmann committed Dec 22, 2015
1 parent acd61ec commit b9317d9
Show file tree
Hide file tree
Showing 20 changed files with 115 additions and 56 deletions.
Expand Up @@ -220,9 +220,9 @@ public void relationshipType( int id, String relationshipType )
writer.relationshipType( id, relationshipType, id + 1 ); writer.relationshipType( id, relationshipType, id + 1 );
} }


public void update( NeoStoreRecord record ) public void update( NeoStoreRecord before, NeoStoreRecord after )
{ {
writer.update( record ); writer.update( before, after );
} }


public void create( NodeRecord node ) public void create( NodeRecord node )
Expand Down
Expand Up @@ -81,10 +81,10 @@ public void relationshipType( int id, String label, int... dynamicIds )
addCommand( command ); addCommand( command );
} }


public void update( NeoStoreRecord record ) public void update( NeoStoreRecord before, NeoStoreRecord after )
{ {
Command.NeoStoreCommand command = new Command.NeoStoreCommand(); Command.NeoStoreCommand command = new Command.NeoStoreCommand();
command.init( record ); command.init( before, after );
addCommand( command ); addCommand( command );
} }


Expand Down Expand Up @@ -272,10 +272,10 @@ public void add( PropertyKeyTokenRecord record )
addCommand( command ); addCommand( command );
} }


public void add( NeoStoreRecord record ) public void add( NeoStoreRecord before, NeoStoreRecord after )
{ {
Command.NeoStoreCommand command = new Command.NeoStoreCommand(); Command.NeoStoreCommand command = new Command.NeoStoreCommand();
command.init( record ); command.init( before, after );
addCommand( command ); addCommand( command );
} }


Expand Down
Expand Up @@ -246,9 +246,10 @@ public void shouldReportNeoStoreInconsistencies() throws Exception
protected void transactionData( GraphStoreFixture.TransactionDataBuilder tx, protected void transactionData( GraphStoreFixture.TransactionDataBuilder tx,
GraphStoreFixture.IdGenerator next ) GraphStoreFixture.IdGenerator next )
{ {
NeoStoreRecord record = new NeoStoreRecord(); NeoStoreRecord before = new NeoStoreRecord();
record.setNextProp( next.property() ); NeoStoreRecord after = new NeoStoreRecord();
tx.update( record ); after.setNextProp( next.property() );
tx.update( before, after );
// We get exceptions when only the above happens in a transaction... // We get exceptions when only the above happens in a transaction...
tx.create( new NodeRecord( next.node(), false, -1, -1 ) ); tx.create( new NodeRecord( next.node(), false, -1, -1 ) );
} }
Expand Down
Expand Up @@ -262,9 +262,9 @@ public void relationshipType( int id, String relationshipType )
writer.relationshipType( id, relationshipType, id + 1 ); writer.relationshipType( id, relationshipType, id + 1 );
} }


public void update( NeoStoreRecord record ) public void update( NeoStoreRecord before, NeoStoreRecord after )
{ {
writer.update( record ); writer.update( before, after );
} }


public void create( NodeRecord node ) public void create( NodeRecord node )
Expand Down
Expand Up @@ -81,10 +81,10 @@ public void relationshipType( int id, String label, int... dynamicIds )
addCommand( command ); addCommand( command );
} }


public void update( NeoStoreRecord record ) public void update( NeoStoreRecord before, NeoStoreRecord after )
{ {
Command.NeoStoreCommand command = new Command.NeoStoreCommand(); Command.NeoStoreCommand command = new Command.NeoStoreCommand();
command.init( record ); command.init( before, after );
addCommand( command ); addCommand( command );
} }


Expand Down Expand Up @@ -272,10 +272,10 @@ public void add( PropertyKeyTokenRecord record )
addCommand( command ); addCommand( command );
} }


public void add( NeoStoreRecord record ) public void add( NeoStoreRecord before, NeoStoreRecord after )
{ {
Command.NeoStoreCommand command = new Command.NeoStoreCommand(); Command.NeoStoreCommand command = new Command.NeoStoreCommand();
command.init( record ); command.init( before, after );
addCommand( command ); addCommand( command );
} }


Expand Down
Expand Up @@ -281,9 +281,10 @@ public void shouldReportNeoStoreInconsistencies() throws Exception
protected void transactionData( GraphStoreFixture.TransactionDataBuilder tx, protected void transactionData( GraphStoreFixture.TransactionDataBuilder tx,
GraphStoreFixture.IdGenerator next ) GraphStoreFixture.IdGenerator next )
{ {
NeoStoreRecord record = new NeoStoreRecord(); NeoStoreRecord before = new NeoStoreRecord();
record.setNextProp( next.property() ); NeoStoreRecord after = new NeoStoreRecord();
tx.update( record ); after.setNextProp( next.property() );
tx.update( before, after );
// We get exceptions when only the above happens in a transaction... // We get exceptions when only the above happens in a transaction...
tx.create( new NodeRecord( next.node(), false, -1, -1 ) ); tx.create( new NodeRecord( next.node(), false, -1, -1 ) );
} }
Expand Down
Expand Up @@ -41,4 +41,12 @@ public String toString()
public void setIdTo( PropertyRecord property ) public void setIdTo( PropertyRecord property )
{ {
} }

@Override
public NeoStoreRecord clone()
{
NeoStoreRecord neoStoreRecord = new NeoStoreRecord();
neoStoreRecord.setNextProp( getNextProp() );
return neoStoreRecord;
}
} }
Expand Up @@ -88,7 +88,7 @@ public static Mode fromRecordState( AbstractBaseRecord record )
protected final void setup( long key, Mode mode ) protected final void setup( long key, Mode mode )
{ {
this.mode = mode; this.mode = mode;
this.keyHash = (int) (( key >>> 32 ) ^ key ); this.keyHash = (int) ((key >>> 32) ^ key);
this.key = key; this.key = key;
} }


Expand Down Expand Up @@ -235,22 +235,21 @@ public RelationshipGroupRecord getAfter()


public static class NeoStoreCommand extends Command public static class NeoStoreCommand extends Command
{ {
private NeoStoreRecord record; private NeoStoreRecord before;
private NeoStoreRecord after;


public NeoStoreCommand init( NeoStoreRecord record ) public NeoStoreCommand init( NeoStoreRecord before, NeoStoreRecord after )
{ {
if( record != null ) setup( after.getId(), Mode.fromRecordState( after ) );
{ this.before = before;
setup( record.getId(), Mode.fromRecordState( record ) ); this.after = after;
}
this.record = record;
return this; return this;
} }


@Override @Override
public String toString() public String toString()
{ {
return record.toString(); return beforeAndAfterToString( before, after );
} }


@Override @Override
Expand All @@ -259,9 +258,14 @@ public boolean handle( CommandVisitor handler ) throws IOException
return handler.visitNeoStoreCommand( this ); return handler.visitNeoStoreCommand( this );
} }


public NeoStoreRecord getRecord() public NeoStoreRecord getBefore()
{ {
return record; return before;
}

public NeoStoreRecord getAfter()
{
return after;
} }
} }


Expand Down Expand Up @@ -375,7 +379,7 @@ public static class SchemaRuleCommand extends Command
private SchemaRule schemaRule; private SchemaRule schemaRule;


public SchemaRuleCommand init( Collection<DynamicRecord> recordsBefore, public SchemaRuleCommand init( Collection<DynamicRecord> recordsBefore,
Collection<DynamicRecord> recordsAfter, SchemaRule schemaRule ) Collection<DynamicRecord> recordsAfter, SchemaRule schemaRule )
{ {
setup( first( recordsAfter ).getId(), Mode.fromRecordState( first( recordsAfter ) ) ); setup( first( recordsAfter ).getId(), Mode.fromRecordState( first( recordsAfter ) ) );
this.recordsBefore = recordsBefore; this.recordsBefore = recordsBefore;
Expand Down Expand Up @@ -434,7 +438,7 @@ public NodeCountsCommand init( int labelId, long delta )
public String toString() public String toString()
{ {
return String.format( "UpdateCounts[(%s) %s %d]", return String.format( "UpdateCounts[(%s) %s %d]",
label( labelId ), delta < 0 ? "-" : "+", Math.abs( delta ) ); label( labelId ), delta < 0 ? "-" : "+", Math.abs( delta ) );
} }


@Override @Override
Expand Down Expand Up @@ -464,7 +468,8 @@ public static class RelationshipCountsCommand extends Command
public RelationshipCountsCommand init( int startLabelId, int typeId, int endLabelId, long delta ) public RelationshipCountsCommand init( int startLabelId, int typeId, int endLabelId, long delta )
{ {
setup( typeId, Mode.UPDATE ); setup( typeId, Mode.UPDATE );
assert delta != 0 : "Tried to create a RelationshipCountsCommand for something that didn't change any count"; assert delta !=
0 : "Tried to create a RelationshipCountsCommand for something that didn't change any count";
this.startLabelId = startLabelId; this.startLabelId = startLabelId;
this.typeId = typeId; this.typeId = typeId;
this.endLabelId = endLabelId; this.endLabelId = endLabelId;
Expand Down
Expand Up @@ -178,7 +178,7 @@ public boolean visitSchemaRuleCommand( Command.SchemaRuleCommand command ) throw
@Override @Override
public boolean visitNeoStoreCommand( Command.NeoStoreCommand command ) throws IOException public boolean visitNeoStoreCommand( Command.NeoStoreCommand command ) throws IOException
{ {
neoStores.getMetaDataStore().setGraphNextProp( command.getRecord().getNextProp() ); neoStores.getMetaDataStore().setGraphNextProp( command.getAfter().getNextProp() );
return false; return false;
} }
} }
Expand Up @@ -199,7 +199,7 @@ private Command visitNeoStoreCommand( ReadableLogChannel channel ) throws IOExce
NeoStoreRecord record = new NeoStoreRecord(); NeoStoreRecord record = new NeoStoreRecord();
record.setNextProp( nextProp ); record.setNextProp( nextProp );
Command.NeoStoreCommand command = new Command.NeoStoreCommand(); Command.NeoStoreCommand command = new Command.NeoStoreCommand();
command.init( record ); command.init( null, record );
return command; return command;
} }


Expand Down
Expand Up @@ -285,7 +285,7 @@ private Command visitNeoStoreCommand( ReadableLogChannel channel ) throws IOExce
NeoStoreRecord record = new NeoStoreRecord(); NeoStoreRecord record = new NeoStoreRecord();
record.setNextProp( nextProp ); record.setNextProp( nextProp );
Command.NeoStoreCommand command = new Command.NeoStoreCommand(); Command.NeoStoreCommand command = new Command.NeoStoreCommand();
command.init( record ); command.init( null, record );
return command; return command;
} }


Expand Down
Expand Up @@ -306,7 +306,7 @@ private Command visitNeoStoreCommand( ReadableLogChannel channel ) throws IOExce
NeoStoreRecord record = new NeoStoreRecord(); NeoStoreRecord record = new NeoStoreRecord();
record.setNextProp( nextProp ); record.setNextProp( nextProp );
Command.NeoStoreCommand command = new Command.NeoStoreCommand(); Command.NeoStoreCommand command = new Command.NeoStoreCommand();
command.init( record ); command.init( null, record );
return command; return command;
} }


Expand Down
Expand Up @@ -355,7 +355,7 @@ private Command visitNeoStoreCommand( ReadableLogChannel channel ) throws IOExce
NeoStoreRecord record = new NeoStoreRecord(); NeoStoreRecord record = new NeoStoreRecord();
record.setNextProp( nextProp ); record.setNextProp( nextProp );
Command.NeoStoreCommand command = new Command.NeoStoreCommand(); Command.NeoStoreCommand command = new Command.NeoStoreCommand();
command.init( record ); command.init( null, record );
return command; return command;
} }


Expand Down
Expand Up @@ -355,7 +355,7 @@ private Command visitNeoStoreCommand( ReadableLogChannel channel ) throws IOExce
NeoStoreRecord record = new NeoStoreRecord(); NeoStoreRecord record = new NeoStoreRecord();
record.setNextProp( nextProp ); record.setNextProp( nextProp );
Command.NeoStoreCommand command = new Command.NeoStoreCommand(); Command.NeoStoreCommand command = new Command.NeoStoreCommand();
command.init( record ); command.init( null, record );
return command; return command;
} }


Expand Down
Expand Up @@ -344,13 +344,20 @@ private Command visitSchemaRuleCommand( ReadableLogChannel channel ) throws IOEx
} }


private Command visitNeoStoreCommand( ReadableLogChannel channel ) throws IOException private Command visitNeoStoreCommand( ReadableLogChannel channel ) throws IOException
{
NeoStoreRecord before = readNeoStoreRecord( channel );
NeoStoreRecord after = readNeoStoreRecord( channel );
Command.NeoStoreCommand command = new Command.NeoStoreCommand();
command.init( before, after );
return command;
}

private NeoStoreRecord readNeoStoreRecord( ReadableLogChannel channel ) throws IOException
{ {
long nextProp = channel.getLong(); long nextProp = channel.getLong();
NeoStoreRecord record = new NeoStoreRecord(); NeoStoreRecord record = new NeoStoreRecord();
record.setNextProp( nextProp ); record.setNextProp( nextProp );
Command.NeoStoreCommand command = new Command.NeoStoreCommand(); return record;
command.init( record );
return command;
} }


private NodeRecord readNodeRecord( long id, ReadableLogChannel channel ) throws IOException private NodeRecord readNodeRecord( long id, ReadableLogChannel channel ) throws IOException
Expand Down
Expand Up @@ -33,6 +33,7 @@
import org.neo4j.kernel.impl.index.IndexDefineCommand; import org.neo4j.kernel.impl.index.IndexDefineCommand;
import org.neo4j.kernel.impl.store.record.DynamicRecord; import org.neo4j.kernel.impl.store.record.DynamicRecord;
import org.neo4j.kernel.impl.store.record.LabelTokenRecord; import org.neo4j.kernel.impl.store.record.LabelTokenRecord;
import org.neo4j.kernel.impl.store.record.NeoStoreRecord;
import org.neo4j.kernel.impl.store.record.NodeRecord; import org.neo4j.kernel.impl.store.record.NodeRecord;
import org.neo4j.kernel.impl.store.record.PropertyBlock; import org.neo4j.kernel.impl.store.record.PropertyBlock;
import org.neo4j.kernel.impl.store.record.PropertyKeyTokenRecord; import org.neo4j.kernel.impl.store.record.PropertyKeyTokenRecord;
Expand Down Expand Up @@ -221,10 +222,17 @@ public boolean visitSchemaRuleCommand( Command.SchemaRuleCommand command ) throw
@Override @Override
public boolean visitNeoStoreCommand( Command.NeoStoreCommand command ) throws IOException public boolean visitNeoStoreCommand( Command.NeoStoreCommand command ) throws IOException
{ {
channel.put( NeoCommandType.NEOSTORE_COMMAND ).putLong( command.getRecord().getNextProp() ); channel.put( NeoCommandType.NEOSTORE_COMMAND );
writeNeoStoreRecord( command.getBefore() );
writeNeoStoreRecord( command.getAfter() );
return false; return false;
} }


private void writeNeoStoreRecord( NeoStoreRecord record ) throws IOException
{
channel.putLong( record.getNextProp() );
}

@Override @Override
public boolean visitIndexAddNodeCommand( AddNodeCommand command ) throws IOException public boolean visitIndexAddNodeCommand( AddNodeCommand command ) throws IOException
{ {
Expand Down
Expand Up @@ -210,7 +210,7 @@ public void extractCommands( Collection<Command> commands ) throws TransactionFa
for ( RecordProxy<Long,NeoStoreRecord, Void> change : neoStoreRecord.changes() ) for ( RecordProxy<Long,NeoStoreRecord, Void> change : neoStoreRecord.changes() )
{ {
Command.NeoStoreCommand command = new Command.NeoStoreCommand(); Command.NeoStoreCommand command = new Command.NeoStoreCommand();
command.init( change.forReadingData() ); command.init( change.getBefore(), change.forReadingData() );
commands.add( command ); commands.add( command );
} }
} }
Expand Down Expand Up @@ -485,11 +485,11 @@ public void ensureHeavy( NeoStoreRecord record )
} }


@Override @Override
public NeoStoreRecord clone(NeoStoreRecord neoStoreRecord ) { public NeoStoreRecord clone( NeoStoreRecord neoStoreRecord )
// We do not expect to manage the before state, so this operation will not be called. {
throw new UnsupportedOperationException("Clone on NeoStoreRecord"); return neoStoreRecord.clone();
} }
}, false, new IntCounter() ); }, true, new IntCounter() );
} }
return neoStoreRecord.getOrLoad( 0L, null ); return neoStoreRecord.getOrLoad( 0L, null );
} }
Expand Down
Expand Up @@ -857,35 +857,37 @@ public void shouldApplyNeoStoreCommandToTheStore() throws Exception
{ {
// given // given
final BatchTransactionApplier applier = newApplier( false ); final BatchTransactionApplier applier = newApplier( false );
final NeoStoreRecord record = new NeoStoreRecord(); final NeoStoreRecord before = new NeoStoreRecord();
record.setNextProp( 42 ); final NeoStoreRecord after = new NeoStoreRecord();
final Command command = new Command.NeoStoreCommand().init( record ); after.setNextProp( 42 );
final Command command = new Command.NeoStoreCommand().init( before, after );


// when // when
boolean result = apply( applier, command::handle, transactionToApply ); boolean result = apply( applier, command::handle, transactionToApply );


// then // then
assertFalse( result ); assertFalse( result );


verify( metaDataStore, times( 1 ) ).setGraphNextProp( record.getNextProp() ); verify( metaDataStore, times( 1 ) ).setGraphNextProp( after.getNextProp() );
} }


@Test @Test
public void shouldApplyNeoStoreCommandToTheStoreInRecovery() throws Exception public void shouldApplyNeoStoreCommandToTheStoreInRecovery() throws Exception
{ {
// given // given
final BatchTransactionApplier applier = newApplier( true ); final BatchTransactionApplier applier = newApplier( true );
final NeoStoreRecord record = new NeoStoreRecord(); final NeoStoreRecord before = new NeoStoreRecord();
record.setNextProp( 42 ); final NeoStoreRecord after = new NeoStoreRecord();
final Command command = new Command.NeoStoreCommand().init( record ); after.setNextProp( 42 );
final Command command = new Command.NeoStoreCommand().init( before, after );


// when // when
boolean result = apply( applier, command::handle, transactionToApply ); boolean result = apply( applier, command::handle, transactionToApply );


// then // then
assertFalse( result ); assertFalse( result );


verify( metaDataStore, times( 1 ) ).setGraphNextProp( record.getNextProp() ); verify( metaDataStore, times( 1 ) ).setGraphNextProp( after.getNextProp() );
} }


private BatchTransactionApplier newApplier( boolean recovery ) private BatchTransactionApplier newApplier( boolean recovery )
Expand Down

0 comments on commit b9317d9

Please sign in to comment.