Skip to content

Commit

Permalink
Add before state to relationship commands
Browse files Browse the repository at this point in the history
  • Loading branch information
davidegrohmann committed Dec 22, 2015
1 parent c995e6a commit 0533390
Show file tree
Hide file tree
Showing 25 changed files with 964 additions and 129 deletions.
Expand Up @@ -244,19 +244,19 @@ public void delete( NodeRecord node )
writer.delete( node ); writer.delete( node );
} }


public void create( RelationshipRecord relationship ) public void create( RelationshipRecord record )
{ {
writer.create( relationship ); writer.create( record );
} }


public void update( RelationshipRecord relationship ) public void update( RelationshipRecord before, RelationshipRecord after )
{ {
writer.update( relationship ); writer.update( before, after );
} }


public void delete( RelationshipRecord relationship ) public void delete( RelationshipRecord record )
{ {
writer.delete( relationship ); writer.delete( record );
} }


public void create( RelationshipGroupRecord group ) public void create( RelationshipGroupRecord group )
Expand Down
Expand Up @@ -143,10 +143,10 @@ public void delete( NodeRecord node )
add( node, new NodeRecord( node.getId(), false, NO_PREV_RELATIONSHIP.intValue(), NO_NEXT_PROPERTY.intValue() ) ); add( node, new NodeRecord( node.getId(), false, NO_PREV_RELATIONSHIP.intValue(), NO_NEXT_PROPERTY.intValue() ) );
} }


public void create( RelationshipRecord relationship ) public void create( RelationshipRecord record )
{ {
relationship.setCreated(); record.setCreated();
update( relationship ); update( new RelationshipRecord( record.getId() ), record );
} }


public void delete( RelationshipGroupRecord group ) public void delete( RelationshipGroupRecord group )
Expand Down Expand Up @@ -175,10 +175,10 @@ public void updateSchema( Collection<DynamicRecord> beforeRecords, Collection<Dy
addSchema( beforeRecords, afterRecords, rule ); addSchema( beforeRecords, afterRecords, rule );
} }


public void update( RelationshipRecord relationship ) public void update( RelationshipRecord before, RelationshipRecord after )
{ {
relationship.setInUse( true ); after.setInUse( true );
add( relationship ); add( before, after );
} }


public void update( RelationshipGroupRecord group ) public void update( RelationshipGroupRecord group )
Expand All @@ -187,10 +187,10 @@ public void update( RelationshipGroupRecord group )
add( group ); add( group );
} }


public void delete( RelationshipRecord relationship ) public void delete( RelationshipRecord record )
{ {
relationship.setInUse( false ); record.setInUse( false );
add( relationship ); add( record, new RelationshipRecord( record.getId() ) );
} }


public void create( PropertyRecord property ) public void create( PropertyRecord property )
Expand Down Expand Up @@ -233,14 +233,14 @@ private void addSchema( Collection<DynamicRecord> beforeRecords, Collection<Dyna
public void add( NodeRecord before, NodeRecord after ) public void add( NodeRecord before, NodeRecord after )
{ {
Command.NodeCommand command = new Command.NodeCommand(); Command.NodeCommand command = new Command.NodeCommand();
command.init( before, after ); command.init( before, after );
addCommand( command ); addCommand( command );
} }


public void add( RelationshipRecord relationship ) public void add( RelationshipRecord before, RelationshipRecord after )
{ {
Command.RelationshipCommand command = new Command.RelationshipCommand(); Command.RelationshipCommand command = new Command.RelationshipCommand();
command.init( relationship ); command.init( before, after );
addCommand( command ); addCommand( command );
} }


Expand Down
Expand Up @@ -291,9 +291,9 @@ public void create( RelationshipRecord relationship )
writer.create( relationship ); writer.create( relationship );
} }


public void update( RelationshipRecord relationship ) public void update( RelationshipRecord before, RelationshipRecord after )
{ {
writer.update( relationship ); writer.update( before, after );
} }


public void delete( RelationshipRecord relationship ) public void delete( RelationshipRecord relationship )
Expand Down
Expand Up @@ -143,10 +143,10 @@ public void delete( NodeRecord node )
add( node, new NodeRecord( node.getId(), false, NO_PREV_RELATIONSHIP.intValue(), NO_NEXT_PROPERTY.intValue() ) ); add( node, new NodeRecord( node.getId(), false, NO_PREV_RELATIONSHIP.intValue(), NO_NEXT_PROPERTY.intValue() ) );
} }


public void create( RelationshipRecord relationship ) public void create( RelationshipRecord record )
{ {
relationship.setCreated(); record.setCreated();
update( relationship ); update( new RelationshipRecord( record.getId() ), record );
} }


public void delete( RelationshipGroupRecord group ) public void delete( RelationshipGroupRecord group )
Expand Down Expand Up @@ -175,10 +175,10 @@ public void updateSchema( Collection<DynamicRecord> beforeRecords, Collection<Dy
addSchema( beforeRecords, afterRecords, rule ); addSchema( beforeRecords, afterRecords, rule );
} }


public void update( RelationshipRecord relationship ) public void update( RelationshipRecord before, RelationshipRecord after )
{ {
relationship.setInUse( true ); after.setInUse( true );
add( relationship ); add( before, after );
} }


public void update( RelationshipGroupRecord group ) public void update( RelationshipGroupRecord group )
Expand All @@ -187,10 +187,10 @@ public void update( RelationshipGroupRecord group )
add( group ); add( group );
} }


public void delete( RelationshipRecord relationship ) public void delete( RelationshipRecord record )
{ {
relationship.setInUse( false ); record.setInUse( false );
add( relationship ); add( record, new RelationshipRecord( record.getId() ) );
} }


public void create( PropertyRecord property ) public void create( PropertyRecord property )
Expand Down Expand Up @@ -237,10 +237,10 @@ public void add( NodeRecord before, NodeRecord after )
addCommand( command ); addCommand( command );
} }


public void add( RelationshipRecord relationship ) public void add( RelationshipRecord before, RelationshipRecord after )
{ {
Command.RelationshipCommand command = new Command.RelationshipCommand(); Command.RelationshipCommand command = new Command.RelationshipCommand();
command.init( relationship ); command.init( before, after );
addCommand( command ); addCommand( command );
} }


Expand Down
Expand Up @@ -24,8 +24,9 @@
import org.neo4j.kernel.impl.transaction.command.PhysicalLogCommandReaderV1_9; import org.neo4j.kernel.impl.transaction.command.PhysicalLogCommandReaderV1_9;
import org.neo4j.kernel.impl.transaction.command.PhysicalLogCommandReaderV2_0; import org.neo4j.kernel.impl.transaction.command.PhysicalLogCommandReaderV2_0;
import org.neo4j.kernel.impl.transaction.command.PhysicalLogCommandReaderV2_1; import org.neo4j.kernel.impl.transaction.command.PhysicalLogCommandReaderV2_1;
import org.neo4j.kernel.impl.transaction.command.PhysicalLogCommandReaderV2_2_4;
import org.neo4j.kernel.impl.transaction.command.PhysicalLogCommandReaderV2_2; import org.neo4j.kernel.impl.transaction.command.PhysicalLogCommandReaderV2_2;
import org.neo4j.kernel.impl.transaction.command.PhysicalLogCommandReaderV2_2_4;
import org.neo4j.kernel.impl.transaction.command.PhysicalLogCommandReaderV3_0;
import org.neo4j.kernel.impl.transaction.log.entry.LogEntryVersion; import org.neo4j.kernel.impl.transaction.log.entry.LogEntryVersion;


public class RecordStorageCommandReaderFactory implements CommandReaderFactory public class RecordStorageCommandReaderFactory implements CommandReaderFactory
Expand All @@ -43,6 +44,7 @@ public RecordStorageCommandReaderFactory()
readers[LogEntryVersion.V2_2.ordinal()] = new PhysicalLogCommandReaderV2_2(); readers[LogEntryVersion.V2_2.ordinal()] = new PhysicalLogCommandReaderV2_2();
readers[LogEntryVersion.V2_2_4.ordinal()] = new PhysicalLogCommandReaderV2_2_4(); readers[LogEntryVersion.V2_2_4.ordinal()] = new PhysicalLogCommandReaderV2_2_4();
readers[LogEntryVersion.V2_3.ordinal()] = new PhysicalLogCommandReaderV2_2_4(); readers[LogEntryVersion.V2_3.ordinal()] = new PhysicalLogCommandReaderV2_2_4();
readers[LogEntryVersion.V3_0.ordinal()] = new PhysicalLogCommandReaderV3_0();
for ( int i = 0; i < versions.length; i++ ) for ( int i = 0; i < versions.length; i++ )
{ {
if ( versions[i] == null ) if ( versions[i] == null )
Expand Down
Expand Up @@ -19,6 +19,8 @@
*/ */
package org.neo4j.kernel.impl.store.record; package org.neo4j.kernel.impl.store.record;


import java.util.Objects;

public class RelationshipRecord extends PrimitiveRecord public class RelationshipRecord extends PrimitiveRecord
{ {
private long firstNode; private long firstNode;
Expand All @@ -31,6 +33,11 @@ public class RelationshipRecord extends PrimitiveRecord
private boolean firstInFirstChain = true; private boolean firstInFirstChain = true;
private boolean firstInSecondChain = true; private boolean firstInSecondChain = true;


public RelationshipRecord( long id )
{
super( id, Record.NO_NEXT_PROPERTY.intValue() );
}

public RelationshipRecord( long id, long firstNode, long secondNode, int type ) public RelationshipRecord( long id, long firstNode, long secondNode, int type )
{ {
this( id ); this( id );
Expand All @@ -39,20 +46,12 @@ public RelationshipRecord( long id, long firstNode, long secondNode, int type )
this.type = type; this.type = type;
} }


public RelationshipRecord( long id )
{
super( id, Record.NO_NEXT_PROPERTY.intValue() );
}

public RelationshipRecord( long id, boolean inUse, long firstNode, long secondNode, int type, public RelationshipRecord( long id, boolean inUse, long firstNode, long secondNode, int type,
long firstPrevRel, long firstNextRel, long secondPrevRel, long secondNextRel, long firstPrevRel, long firstNextRel, long secondPrevRel, long secondNextRel,
boolean firstInFirstChain, boolean firstInSecondChain ) boolean firstInFirstChain, boolean firstInSecondChain )
{ {
this( id ); this( id, firstNode, secondNode, type );
setInUse( inUse ); setInUse( inUse );
this.firstNode = firstNode;
this.secondNode = secondNode;
this.type = type;
this.firstPrevRel = firstPrevRel; this.firstPrevRel = firstPrevRel;
this.firstNextRel = firstNextRel; this.firstNextRel = firstNextRel;
this.secondPrevRel = secondPrevRel; this.secondPrevRel = secondPrevRel;
Expand Down Expand Up @@ -177,9 +176,46 @@ public String toString()
.append( "]" ).toString(); .append( "]" ).toString();
} }



@Override
public RelationshipRecord clone()
{
return new RelationshipRecord( getId(), inUse(),
firstNode, secondNode, type,
firstPrevRel, firstNextRel, secondPrevRel, secondNextRel, firstInFirstChain, firstInSecondChain );
}

@Override @Override
public void setIdTo( PropertyRecord property ) public void setIdTo( PropertyRecord property )
{ {
property.setRelId( getId() ); property.setRelId( getId() );
} }
}
@Override
public boolean equals( Object o )
{
if ( this == o )
{ return true; }
if ( o == null || getClass() != o.getClass() )
{ return false; }
if ( !super.equals( o ) )
{ return false; }
RelationshipRecord that = (RelationshipRecord) o;
return firstNode == that.firstNode &&
secondNode == that.secondNode &&
type == that.type &&
firstPrevRel == that.firstPrevRel &&
firstNextRel == that.firstNextRel &&
secondPrevRel == that.secondPrevRel &&
secondNextRel == that.secondNextRel &&
firstInFirstChain == that.firstInFirstChain &&
firstInSecondChain == that.firstInSecondChain;
}

@Override
public int hashCode()
{
return Objects.hash( super.hashCode(), firstNode, secondNode, type, firstPrevRel, firstNextRel, secondPrevRel,
secondNextRel, firstInFirstChain, firstInSecondChain );
}
}
Expand Up @@ -39,7 +39,6 @@


import static java.lang.String.format; import static java.lang.String.format;
import static java.util.Collections.unmodifiableCollection; import static java.util.Collections.unmodifiableCollection;

import static org.neo4j.helpers.collection.IteratorUtil.first; import static org.neo4j.helpers.collection.IteratorUtil.first;
import static org.neo4j.kernel.impl.util.IdPrettyPrinter.label; import static org.neo4j.kernel.impl.util.IdPrettyPrinter.label;
import static org.neo4j.kernel.impl.util.IdPrettyPrinter.relationshipType; import static org.neo4j.kernel.impl.util.IdPrettyPrinter.relationshipType;
Expand Down Expand Up @@ -164,19 +163,21 @@ public NodeRecord getAfter()


public static class RelationshipCommand extends Command public static class RelationshipCommand extends Command
{ {
private RelationshipRecord record; private RelationshipRecord before;
private RelationshipRecord after;


public RelationshipCommand init( RelationshipRecord record ) public RelationshipCommand init( RelationshipRecord before, RelationshipRecord after )
{ {
setup( record.getId(), Mode.fromRecordState( record ) ); setup( after.getId(), Mode.fromRecordState( after ) );
this.record = record; this.before = before;
this.after = after;
return this; return this;
} }


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


@Override @Override
Expand All @@ -185,9 +186,14 @@ public boolean handle( CommandVisitor handler ) throws IOException
return handler.visitRelationshipCommand( this ); return handler.visitRelationshipCommand( this );
} }


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

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


Expand Down
Expand Up @@ -27,11 +27,9 @@
import org.neo4j.kernel.impl.locking.LockGroup; import org.neo4j.kernel.impl.locking.LockGroup;
import org.neo4j.kernel.impl.locking.LockService; import org.neo4j.kernel.impl.locking.LockService;
import org.neo4j.kernel.impl.store.NeoStores; import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.store.NodeStore;
import org.neo4j.kernel.impl.store.SchemaStore; import org.neo4j.kernel.impl.store.SchemaStore;
import org.neo4j.kernel.impl.store.record.DynamicRecord; import org.neo4j.kernel.impl.store.record.DynamicRecord;
import org.neo4j.kernel.impl.store.record.PropertyConstraintRule; import org.neo4j.kernel.impl.store.record.PropertyConstraintRule;
import org.neo4j.kernel.impl.store.record.RelationshipRecord;


/** /**
* Visits commands targeted towards the {@link NeoStores} and update corresponding stores. * Visits commands targeted towards the {@link NeoStores} and update corresponding stores.
Expand Down Expand Up @@ -72,9 +70,7 @@ public boolean visitNodeCommand( Command.NodeCommand command ) throws IOExceptio
lockGroup.add( lockService.acquireNodeLock( command.getKey(), LockService.LockType.WRITE_LOCK ) ); lockGroup.add( lockService.acquireNodeLock( command.getKey(), LockService.LockType.WRITE_LOCK ) );


// update store // update store
NodeStore nodeStore = neoStores.getNodeStore(); neoStores.getNodeStore().updateRecord( command.getAfter() );
nodeStore.updateRecord( command.getAfter() );

return false; return false;
} }


Expand All @@ -83,8 +79,7 @@ public boolean visitRelationshipCommand( Command.RelationshipCommand command ) t
{ {
lockGroup.add( lockService.acquireRelationshipLock( command.getKey(), LockService.LockType.WRITE_LOCK ) ); lockGroup.add( lockService.acquireRelationshipLock( command.getKey(), LockService.LockType.WRITE_LOCK ) );


RelationshipRecord record = command.getRecord(); neoStores.getRelationshipStore().updateRecord( command.getAfter() );
neoStores.getRelationshipStore().updateRecord( record );
return false; return false;
} }


Expand Down
Expand Up @@ -115,7 +115,7 @@ record = new RelationshipRecord( id, -1, -1, -1 );
record.setInUse( false ); record.setInUse( false );
} }
Command.RelationshipCommand command = new Command.RelationshipCommand(); Command.RelationshipCommand command = new Command.RelationshipCommand();
command.init( record ); command.init( null, record );
return command; return command;
} }


Expand Down
Expand Up @@ -25,7 +25,6 @@
import java.util.Collection; import java.util.Collection;


import org.neo4j.kernel.api.exceptions.schema.MalformedSchemaRuleException; import org.neo4j.kernel.api.exceptions.schema.MalformedSchemaRuleException;
import org.neo4j.kernel.impl.api.CommandVisitor;
import org.neo4j.kernel.impl.store.AbstractDynamicStore; import org.neo4j.kernel.impl.store.AbstractDynamicStore;
import org.neo4j.kernel.impl.store.PropertyType; import org.neo4j.kernel.impl.store.PropertyType;
import org.neo4j.kernel.impl.store.record.DynamicRecord; import org.neo4j.kernel.impl.store.record.DynamicRecord;
Expand Down Expand Up @@ -140,7 +139,7 @@ record = new RelationshipRecord( id, -1, -1, -1 );
record.setInUse( false ); record.setInUse( false );
} }
Command.RelationshipCommand command = new Command.RelationshipCommand(); Command.RelationshipCommand command = new Command.RelationshipCommand();
command.init( record ); command.init( null, record );
return command; return command;
} }


Expand Down
Expand Up @@ -139,7 +139,7 @@ record = new RelationshipRecord( id, -1, -1, -1 );
record.setCreated(); record.setCreated();
} }
Command.RelationshipCommand command = new Command.RelationshipCommand(); Command.RelationshipCommand command = new Command.RelationshipCommand();
command.init( record ); command.init( null, record );
return command; return command;
} }


Expand Down
Expand Up @@ -190,7 +190,7 @@ record = new RelationshipRecord( id, -1, -1, channel.getInt() );
record.setCreated(); record.setCreated();
} }
Command.RelationshipCommand command = new Command.RelationshipCommand(); Command.RelationshipCommand command = new Command.RelationshipCommand();
command.init( record ); command.init( null, record );
return command; return command;
} }


Expand Down

0 comments on commit 0533390

Please sign in to comment.