Skip to content

Commit

Permalink
Simplify SchemaRule classes by moving Descriptor to parent classes
Browse files Browse the repository at this point in the history
  • Loading branch information
craigtaverner authored and fickludd committed Jan 23, 2017
1 parent f069c3a commit 8ef4abe
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,24 @@
import java.nio.ByteBuffer;

import org.neo4j.kernel.api.exceptions.schema.MalformedSchemaRuleException;
import org.neo4j.kernel.api.schema.EntityPropertyDescriptor;
import org.neo4j.storageengine.api.schema.SchemaRule;

public abstract class AbstractSchemaRule implements SchemaRule, RecordSerializable
{
protected final Kind kind;
protected final long id;
protected final EntityPropertyDescriptor descriptor;

public AbstractSchemaRule( long id, Kind kind )
public AbstractSchemaRule( long id, Kind kind, EntityPropertyDescriptor descriptor )
{
this.id = id;
this.kind = kind;
this.descriptor = descriptor;
}

@Override
public long getId()
public final long getId()
{
return this.id;
}
Expand All @@ -54,24 +57,24 @@ public final Kind getKind()
public abstract void serialize( ByteBuffer target );

@Override
public boolean equals( Object o )
public final boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
if ( o != null && o instanceof AbstractSchemaRule )
{
return false;
AbstractSchemaRule that = (AbstractSchemaRule) o;
return kind == that.kind && this.descriptor.equals( that.descriptor() );
}
AbstractSchemaRule that = (AbstractSchemaRule) o;
return kind == that.kind;
return false;
}

@Override
public int hashCode()
public final int hashCode()
{
return kind.hashCode();
return 31 * kind.hashCode() + descriptor.hashCode();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public class IndexRule extends AbstractSchemaRule implements IndexSchemaRule
{
private static final long NO_OWNING_CONSTRAINT = -1;
private final SchemaIndexProvider.Descriptor providerDescriptor;
private final NodePropertyDescriptor descriptor;
/**
* Non-null for constraint indexes, equal to {@link #NO_OWNING_CONSTRAINT} for
* constraint indexes with no owning constraint record.
Expand Down Expand Up @@ -77,7 +76,7 @@ public static IndexRule constraintIndexRule( long id, NodePropertyDescriptor des
public IndexRule( long id, NodePropertyDescriptor descriptor, SchemaIndexProvider.Descriptor providerDescriptor,
Long owningConstraint )
{
super( id, indexKind( owningConstraint ) );
super( id, indexKind( owningConstraint ), descriptor );
this.owningConstraint = owningConstraint;

if ( providerDescriptor == null )
Expand All @@ -86,7 +85,6 @@ public IndexRule( long id, NodePropertyDescriptor descriptor, SchemaIndexProvide
}

this.providerDescriptor = providerDescriptor;
this.descriptor = descriptor;
}

private static Kind indexKind( Long owningConstraint )
Expand Down Expand Up @@ -129,7 +127,7 @@ public SchemaIndexProvider.Descriptor getProviderDescriptor()
@Override
public NodePropertyDescriptor descriptor()
{
return descriptor;
return (NodePropertyDescriptor) descriptor;
}

@Override
Expand All @@ -156,7 +154,7 @@ public Long getOwningConstraint()
@Override
public int getLabel()
{
return descriptor.getLabelId();
return descriptor().getLabelId();
}

@Override
Expand All @@ -168,7 +166,7 @@ public int getRelationshipType()
@Override
public int length()
{
int propertyCount = descriptor.isComposite() ? descriptor.getPropertyKeyIds().length : 1;
int propertyCount = descriptor().isComposite() ? descriptor.getPropertyKeyIds().length : 1;
return 4 /* label id */
+ 1 /* kind id */
+ UTF8.computeRequiredByteBufferSize( providerDescriptor.getKey() )
Expand All @@ -182,8 +180,8 @@ public int length()
public void serialize( ByteBuffer target )
{
int[] propertyKeys =
descriptor.isComposite() ? descriptor.getPropertyKeyIds() : new int[]{descriptor.getPropertyKeyId()};
target.putInt( descriptor.getLabelId() );
descriptor().isComposite() ? descriptor.getPropertyKeyIds() : new int[]{descriptor.getPropertyKeyId()};
target.putInt( descriptor().getLabelId() );
// 0 is reserved, so use ordinal + 1
target.put( (byte) (kind.ordinal()+1) );
UTF8.putEncodedStringInto( providerDescriptor.getKey(), target );
Expand All @@ -199,31 +197,6 @@ public void serialize( ByteBuffer target )
}
}

@Override
public int hashCode()
{
// TODO: Think if this needs to be extended with providerDescriptor
return 31 * super.hashCode() + descriptor.hashCode();
}

@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;
}
return descriptor.equals( ((IndexRule) o).descriptor );
}

public boolean matches(NodePropertyDescriptor descriptor)
{
return descriptor.equals( this.descriptor );
Expand All @@ -238,7 +211,7 @@ public String toString()
ownerString = ", owner=" + (owningConstraint == -1 ? "<not set>" : owningConstraint);
}

return "IndexRule[id=" + id + ", label=" + descriptor.getLabelId() + ", kind=" + kind +
return "IndexRule[id=" + id + ", label=" + descriptor().getLabelId() + ", kind=" + kind +
", provider=" + providerDescriptor + ", properties=" + descriptor.propertyIdText() + ownerString + "]";
}

Expand All @@ -248,6 +221,6 @@ public IndexRule withOwningConstraint( long constraintId )
{
throw new IllegalStateException( this + " is not a constraint index" );
}
return constraintIndexRule( getId(), descriptor, getProviderDescriptor(), constraintId );
return constraintIndexRule( getId(), descriptor(), getProviderDescriptor(), constraintId );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,21 @@

public abstract class NodePropertyConstraintRule extends PropertyConstraintRule
{
protected final NodePropertyDescriptor descriptor;

public NodePropertyConstraintRule( long id, NodePropertyDescriptor descriptor, Kind kind )
{
super( id, kind );
this.descriptor = descriptor;
super( id, kind, descriptor );
}

@Override
public final NodePropertyDescriptor descriptor()
{
return descriptor;
return (NodePropertyDescriptor) descriptor;
}

@Override
public final int getLabel()
{
return descriptor.getLabelId();
return descriptor().getLabelId();
}

@Override
Expand All @@ -63,28 +60,4 @@ public boolean matches(NodePropertyDescriptor descriptor)

@Override
public abstract NodePropertyConstraint toConstraint();

@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;
}
return descriptor.equals( ((NodePropertyConstraintRule) o).descriptor );
}

@Override
public int hashCode()
{
return 31 * super.hashCode() + descriptor.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private NodePropertyExistenceConstraintRule( long id, NodePropertyDescriptor des
@Override
public String toString()
{
return "NodePropertyExistenceConstraintRule[id=" + id + ", label=" + descriptor.getLabelId() + ", kind=" +
return "NodePropertyExistenceConstraintRule[id=" + id + ", label=" + descriptor().getLabelId() + ", kind=" +
kind + ", propertyKeyIds=" + descriptor.propertyIdText() + "]";
}

Expand All @@ -65,14 +65,14 @@ public int length()
public void serialize( ByteBuffer target )
{
//TODO: Support composite indexes (requires format update)
target.putInt( descriptor.getLabelId() );
target.putInt( descriptor().getLabelId() );
target.put( kind.id() );
target.putInt( descriptor.getPropertyKeyId() );
}

@Override
public NodePropertyConstraint toConstraint()
{
return new NodePropertyExistenceConstraint( descriptor );
return new NodePropertyExistenceConstraint( descriptor() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@
package org.neo4j.kernel.impl.store.record;

import org.neo4j.kernel.api.constraints.PropertyConstraint;
import org.neo4j.kernel.api.schema.EntityPropertyDescriptor;

public abstract class PropertyConstraintRule extends AbstractSchemaRule
{
PropertyConstraintRule( long id, Kind kind )
PropertyConstraintRule( long id, Kind kind, EntityPropertyDescriptor descriptor )
{
super( id, kind );
super( id, kind, descriptor );
}

public abstract PropertyConstraint toConstraint();

public abstract boolean containsPropertyKeyId( int propertyKeyId );
public boolean containsPropertyKeyId( int propertyKeyId )
{
return propertyKeyId == descriptor.getPropertyKeyId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@
package org.neo4j.kernel.impl.store.record;

import org.neo4j.kernel.api.constraints.RelationshipPropertyConstraint;
import org.neo4j.kernel.api.schema.RelationshipPropertyDescriptor;

public abstract class RelationshipPropertyConstraintRule extends PropertyConstraintRule
{
protected final int relationshipType;

public RelationshipPropertyConstraintRule( long id, int relationshipType, Kind kind )
public RelationshipPropertyConstraintRule( long id, RelationshipPropertyDescriptor descriptor, Kind kind )
{
super( id, kind );
this.relationshipType = relationshipType;
super( id, kind, descriptor );
}

@Override
Expand All @@ -37,36 +35,17 @@ public final int getLabel()
throw new IllegalStateException( "Constraint rule is associated with relationships" );
}

@Override
public final int getRelationshipType()
public final RelationshipPropertyDescriptor descriptor()
{
return relationshipType;
return (RelationshipPropertyDescriptor) descriptor;
}

@Override
public abstract RelationshipPropertyConstraint toConstraint();

@Override
public boolean equals( Object o )
public final int getRelationshipType()
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
if ( !super.equals( o ) )
{
return false;
}
return relationshipType == ((RelationshipPropertyConstraintRule) o).relationshipType;
return descriptor().getRelationshipTypeId();
}

@Override
public int hashCode()
{
return 31 * super.hashCode() + relationshipType;
}
public abstract RelationshipPropertyConstraint toConstraint();
}

0 comments on commit 8ef4abe

Please sign in to comment.