Skip to content

Commit

Permalink
Add common BaseCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
davidegrohmann committed Dec 22, 2015
1 parent 2f4eef0 commit 64ba396
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 113 deletions.
Expand Up @@ -23,6 +23,7 @@
import java.util.Collection;

import org.neo4j.kernel.impl.api.CommandVisitor;
import org.neo4j.kernel.impl.store.record.Abstract64BitRecord;
import org.neo4j.kernel.impl.store.record.AbstractBaseRecord;
import org.neo4j.kernel.impl.store.record.DynamicRecord;
import org.neo4j.kernel.impl.store.record.LabelTokenRecord;
Expand Down Expand Up @@ -125,23 +126,17 @@ protected String beforeAndAfterToString( AbstractBaseRecord before, AbstractBase
return format( " -%s%n +%s", before, after );
}

public static class NodeCommand extends Command

public static abstract class BaseCommand<RECORD extends Abstract64BitRecord> extends Command
{
private NodeRecord before;
private NodeRecord after;
protected RECORD before;
protected RECORD after;

public NodeCommand init( NodeRecord before, NodeRecord after )
protected void initialize( RECORD before, RECORD after )
{
setup( after.getId(), Mode.fromRecordState( after ) );
this.before = before;
this.after = after;
return this;
}

@Override
public boolean handle( CommandVisitor handler ) throws IOException
{
return handler.visitNodeCommand( this );
}

@Override
Expand All @@ -150,173 +145,93 @@ public String toString()
return beforeAndAfterToString( before, after );
}

public NodeRecord getBefore()
public RECORD getBefore()
{
return before;
}

public NodeRecord getAfter()
public RECORD getAfter()
{
return after;
}
}

public static class RelationshipCommand extends Command
public static class NodeCommand extends BaseCommand<NodeRecord>
{
private RelationshipRecord before;
private RelationshipRecord after;

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

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

@Override
public boolean handle( CommandVisitor handler ) throws IOException
{
return handler.visitRelationshipCommand( this );
return handler.visitNodeCommand( this );
}
}

public RelationshipRecord getBefore()
public static class RelationshipCommand extends BaseCommand<RelationshipRecord>
{
public RelationshipCommand init( RelationshipRecord before, RelationshipRecord after )
{
return before;
initialize( before, after );
return this;
}

public RelationshipRecord getAfter()
@Override
public boolean handle( CommandVisitor handler ) throws IOException
{
return after;
return handler.visitRelationshipCommand( this );
}
}

public static class RelationshipGroupCommand extends Command
public static class RelationshipGroupCommand extends BaseCommand<RelationshipGroupRecord>
{
private RelationshipGroupRecord before;
private RelationshipGroupRecord after;

public RelationshipGroupCommand init( RelationshipGroupRecord before, RelationshipGroupRecord after )
{
setup( after.getId(), Mode.fromRecordState( after ) );
this.before = before;
this.after = after;
initialize( before, after );
return this;
}

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

@Override
public boolean handle( CommandVisitor handler ) throws IOException
{
return handler.visitRelationshipGroupCommand( this );
}

public RelationshipGroupRecord getBefore()
{
return before;
}

public RelationshipGroupRecord getAfter()
{
return after;
}
}

public static class NeoStoreCommand extends Command
public static class NeoStoreCommand extends BaseCommand<NeoStoreRecord>
{
private NeoStoreRecord before;
private NeoStoreRecord after;

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

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

@Override
public boolean handle( CommandVisitor handler ) throws IOException
{
return handler.visitNeoStoreCommand( this );
}

public NeoStoreRecord getBefore()
{
return before;
}

public NeoStoreRecord getAfter()
{
return after;
}
}

public static class PropertyKeyTokenCommand extends TokenCommand<PropertyKeyTokenRecord>
public static class PropertyCommand extends BaseCommand<PropertyRecord> implements PropertyRecordChange
{
@Override
public boolean handle( CommandVisitor handler ) throws IOException
{
return handler.visitPropertyKeyTokenCommand( this );
}
}

public static class PropertyCommand extends Command implements PropertyRecordChange
{
private PropertyRecord before;
private PropertyRecord after;

// TODO as optimization the deserialized key/values could be passed in here
// so that the cost of deserializing them only applies in recovery/HA
public PropertyCommand init( PropertyRecord before, PropertyRecord after )
{
setup( after.getId(), Mode.fromRecordState( after ) );
this.before = before;
this.after = after;
initialize( before, after );
return this;
}

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

@Override
public boolean handle( CommandVisitor handler ) throws IOException
{
return handler.visitPropertyCommand( this );
}

@Override
public PropertyRecord getBefore()
{
return before;
}

@Override
public PropertyRecord getAfter()
{
return after;
}

public long getNodeId()
{
return after.getNodeId();
Expand Down Expand Up @@ -358,6 +273,15 @@ public String toString()
}
}

public static class PropertyKeyTokenCommand extends TokenCommand<PropertyKeyTokenRecord>
{
@Override
public boolean handle( CommandVisitor handler ) throws IOException
{
return handler.visitPropertyKeyTokenCommand( this );
}
}

public static class RelationshipTypeTokenCommand extends TokenCommand<RelationshipTypeTokenRecord>
{
@Override
Expand Down
Expand Up @@ -278,7 +278,7 @@ private int extractTokenId( Collection<Command> commands ) throws NoSuchEntryExc
{
if( command instanceof Command.TokenCommand )
{
return ((Command.TokenCommand) command).getAfter().getId();
return ((Command.TokenCommand<? extends TokenRecord>) command).getAfter().getId();
}
}
throw new NoSuchEntryException( "Expected command not found" );
Expand Down

0 comments on commit 64ba396

Please sign in to comment.