Skip to content

Commit

Permalink
Made SchemaRule an interface
Browse files Browse the repository at this point in the history
  • Loading branch information
fickludd authored and ragadeeshu committed May 21, 2018
1 parent 9dd62e4 commit 1e2dc8f
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 105 deletions.
Expand Up @@ -103,6 +103,7 @@
import org.neo4j.kernel.impl.store.record.RelationshipGroupRecord; import org.neo4j.kernel.impl.store.record.RelationshipGroupRecord;
import org.neo4j.kernel.impl.store.record.RelationshipRecord; import org.neo4j.kernel.impl.store.record.RelationshipRecord;
import org.neo4j.kernel.impl.store.record.RelationshipTypeTokenRecord; import org.neo4j.kernel.impl.store.record.RelationshipTypeTokenRecord;
import org.neo4j.kernel.impl.store.record.SchemaRuleSerialization;
import org.neo4j.kernel.impl.transaction.state.storeview.NeoStoreIndexStoreView; import org.neo4j.kernel.impl.transaction.state.storeview.NeoStoreIndexStoreView;
import org.neo4j.kernel.impl.util.Bits; import org.neo4j.kernel.impl.util.Bits;
import org.neo4j.kernel.internal.GraphDatabaseAPI; import org.neo4j.kernel.internal.GraphDatabaseAPI;
Expand Down Expand Up @@ -1017,7 +1018,7 @@ protected void transactionData( GraphStoreFixture.TransactionDataBuilder tx,


schema.setNextBlock( next.schema() ); // Point to a record that isn't in use. schema.setNextBlock( next.schema() ); // Point to a record that isn't in use.
IndexRule rule = indexRule( schema.getId(), label1, key1, DESCRIPTOR ); IndexRule rule = indexRule( schema.getId(), label1, key1, DESCRIPTOR );
schema.setData( rule.serialize() ); schema.setData( SchemaRuleSerialization.serialize( rule ) );


tx.createSchema( asList( schemaBefore ), asList( schema ), rule ); tx.createSchema( asList( schemaBefore ), asList( schema ), rule );
} }
Expand Down Expand Up @@ -2379,7 +2380,7 @@ public void andThatsAllFolks()


private static Collection<DynamicRecord> serializeRule( SchemaRule rule, DynamicRecord... records ) private static Collection<DynamicRecord> serializeRule( SchemaRule rule, DynamicRecord... records )
{ {
byte[] data = rule.serialize(); byte[] data = SchemaRuleSerialization.serialize( rule );
DynamicRecordAllocator dynamicRecordAllocator = DynamicRecordAllocator dynamicRecordAllocator =
new ReusableRecordsCompositeAllocator( asList( records ), schemaAllocator ); new ReusableRecordsCompositeAllocator( asList( records ), schemaAllocator );
Collection<DynamicRecord> result = new ArrayList<>(); Collection<DynamicRecord> result = new ArrayList<>();
Expand Down
Expand Up @@ -413,12 +413,18 @@ private Object parameter( Class<?> type )


private SchemaRule simpleSchemaRule() private SchemaRule simpleSchemaRule()
{ {
return new SchemaRule( 0 ) return new SchemaRule()
{ {
@Override @Override
public byte[] serialize() public long getId()
{ {
return new byte[0]; return 0;
}

@Override
public String getName()
{
return null;
} }


@Override @Override
Expand Down
Expand Up @@ -73,7 +73,7 @@ public List<DynamicRecord> allocateFrom( SchemaRule rule )
List<DynamicRecord> records = new ArrayList<>(); List<DynamicRecord> records = new ArrayList<>();
DynamicRecord record = getRecord( rule.getId(), nextRecord(), CHECK ); DynamicRecord record = getRecord( rule.getId(), nextRecord(), CHECK );
DynamicRecordAllocator recordAllocator = new ReusableRecordsCompositeAllocator( singleton( record ), this ); DynamicRecordAllocator recordAllocator = new ReusableRecordsCompositeAllocator( singleton( record ), this );
allocateRecordsFromBytes( records, rule.serialize(), recordAllocator ); allocateRecordsFromBytes( records, SchemaRuleSerialization.serialize( rule ), recordAllocator );
return records; return records;
} }


Expand Down
Expand Up @@ -26,9 +26,11 @@


import static org.neo4j.internal.kernel.api.schema.SchemaUtil.idTokenNameLookup; import static org.neo4j.internal.kernel.api.schema.SchemaUtil.idTokenNameLookup;


public class ConstraintRule extends SchemaRule implements ConstraintDescriptor.Supplier public class ConstraintRule implements SchemaRule, ConstraintDescriptor.Supplier
{ {
private final Long ownedIndexRule; private final Long ownedIndexRule;
private final String name;
private final long id;
private final ConstraintDescriptor descriptor; private final ConstraintDescriptor descriptor;


public static ConstraintRule constraintRule( public static ConstraintRule constraintRule(
Expand Down Expand Up @@ -62,9 +64,10 @@ public static ConstraintRule constraintRule(


ConstraintRule( long id, ConstraintDescriptor descriptor, Long ownedIndexRule, String name ) ConstraintRule( long id, ConstraintDescriptor descriptor, Long ownedIndexRule, String name )
{ {
super( id, name ); this.id = id;
this.descriptor = descriptor; this.descriptor = descriptor;
this.ownedIndexRule = ownedIndexRule; this.ownedIndexRule = ownedIndexRule;
this.name = SchemaRule.nameOrDefault( name, "constraint_" + id );
} }


@Override @Override
Expand Down Expand Up @@ -96,12 +99,6 @@ public long getOwnedIndex()
return ownedIndexRule; return ownedIndexRule;
} }


@Override
public byte[] serialize()
{
return SchemaRuleSerialization.serialize( this );
}

@Override @Override
public boolean equals( Object o ) public boolean equals( Object o )
{ {
Expand All @@ -118,4 +115,16 @@ public int hashCode()
{ {
return descriptor.hashCode(); return descriptor.hashCode();
} }

@Override
public long getId()
{
return id;
}

@Override
public String getName()
{
return name;
}
} }
Expand Up @@ -30,11 +30,13 @@
/** /**
* A {@link Label} can have zero or more index rules which will have data specified in the rules indexed. * A {@link Label} can have zero or more index rules which will have data specified in the rules indexed.
*/ */
public class IndexRule extends SchemaRule implements SchemaIndexDescriptor.Supplier public class IndexRule implements SchemaRule, SchemaIndexDescriptor.Supplier
{ {
private final IndexProvider.Descriptor providerDescriptor; private final IndexProvider.Descriptor providerDescriptor;
private final SchemaIndexDescriptor descriptor; private final SchemaIndexDescriptor descriptor;
private final Long owningConstraint; private final Long owningConstraint;
private final long id;
private final String name;


public static IndexRule indexRule( long id, SchemaIndexDescriptor descriptor, public static IndexRule indexRule( long id, SchemaIndexDescriptor descriptor,
IndexProvider.Descriptor providerDescriptor ) IndexProvider.Descriptor providerDescriptor )
Expand Down Expand Up @@ -71,7 +73,8 @@ public static IndexRule constraintIndexRule( long id, SchemaIndexDescriptor desc
IndexRule( long id, IndexProvider.Descriptor providerDescriptor, IndexRule( long id, IndexProvider.Descriptor providerDescriptor,
SchemaIndexDescriptor descriptor, Long owningConstraint, String name ) SchemaIndexDescriptor descriptor, Long owningConstraint, String name )
{ {
super( id, name ); this.id = id;
this.name = SchemaRule.nameOrDefault( name, "index_" + id );
if ( providerDescriptor == null ) if ( providerDescriptor == null )
{ {
throw new IllegalArgumentException( "null provider descriptor prohibited" ); throw new IllegalArgumentException( "null provider descriptor prohibited" );
Expand Down Expand Up @@ -128,12 +131,6 @@ public IndexRule withOwningConstraint( long constraintId )
return constraintIndexRule( id, descriptor, providerDescriptor, constraintId ); return constraintIndexRule( id, descriptor, providerDescriptor, constraintId );
} }


@Override
public byte[] serialize()
{
return SchemaRuleSerialization.serialize( this );
}

@Override @Override
public String toString() public String toString()
{ {
Expand Down Expand Up @@ -175,4 +172,16 @@ public int hashCode()
{ {
return this.descriptor.hashCode(); return this.descriptor.hashCode();
} }

@Override
public long getId()
{
return id;
}

@Override
public String getName()
{
return name;
}
} }
Expand Up @@ -70,8 +70,27 @@ private SchemaRuleSerialization()


// PUBLIC // PUBLIC


/**
* Serialize the provided SchemaRule onto the target buffer
*
* @param schemaRule the SchemaRule to serialize
*/
public static byte[] serialize( SchemaRule schemaRule )
{
if ( schemaRule instanceof IndexRule )
{
return serialize( (IndexRule)schemaRule );
}
else if ( schemaRule instanceof ConstraintRule )
{
return serialize( (ConstraintRule)schemaRule );
}
throw new IllegalStateException( "Unknown schema rule type: "+schemaRule.getClass() );
}

/** /**
* Parse a SchemaRule from the provided buffer. * Parse a SchemaRule from the provided buffer.
*
* @param id the id to give the returned Schema Rule * @param id the id to give the returned Schema Rule
* @param source the buffer to parse from * @param source the buffer to parse from
* @return a SchemaRule * @return a SchemaRule
Expand Down Expand Up @@ -99,6 +118,7 @@ public static SchemaRule deserialize( long id, ByteBuffer source ) throws Malfor


/** /**
* Serialize the provided IndexRule onto the target buffer * Serialize the provided IndexRule onto the target buffer
*
* @param indexRule the IndexRule to serialize * @param indexRule the IndexRule to serialize
* @throws IllegalStateException if the IndexRule is of type unique, but the owning constrain has not been set * @throws IllegalStateException if the IndexRule is of type unique, but the owning constrain has not been set
*/ */
Expand Down Expand Up @@ -237,14 +257,14 @@ private static IndexRule readIndexRule( long id, ByteBuffer source ) throws Malf
{ {
case GENERAL_INDEX: case GENERAL_INDEX:
schema = readLabelSchema( source ); schema = readLabelSchema( source );
name = readRuleName( id, IndexRule.class, source ); name = readRuleName( source );
return IndexRule.indexRule( id, SchemaIndexDescriptorFactory.forSchema( schema ), indexProvider, name ); return IndexRule.indexRule( id, SchemaIndexDescriptorFactory.forSchema( schema ), indexProvider, name );


case UNIQUE_INDEX: case UNIQUE_INDEX:
long owningConstraint = source.getLong(); long owningConstraint = source.getLong();
schema = readLabelSchema( source ); schema = readLabelSchema( source );
SchemaIndexDescriptor descriptor = SchemaIndexDescriptorFactory.uniqueForSchema( schema ); SchemaIndexDescriptor descriptor = SchemaIndexDescriptorFactory.uniqueForSchema( schema );
name = readRuleName( id, IndexRule.class, source ); name = readRuleName( source );
return IndexRule.constraintIndexRule( id, descriptor, indexProvider, return IndexRule.constraintIndexRule( id, descriptor, indexProvider,
owningConstraint == NO_OWNING_CONSTRAINT_YET ? null : owningConstraint, name ); owningConstraint == NO_OWNING_CONSTRAINT_YET ? null : owningConstraint, name );


Expand Down Expand Up @@ -282,38 +302,38 @@ private static ConstraintRule readConstraintRule( long id, ByteBuffer source ) t
{ {
case EXISTS_CONSTRAINT: case EXISTS_CONSTRAINT:
schema = readSchema( source ); schema = readSchema( source );
name = readRuleName( id, ConstraintRule.class, source ); name = readRuleName( source );
return ConstraintRule.constraintRule( id, ConstraintDescriptorFactory.existsForSchema( schema ), name ); return ConstraintRule.constraintRule( id, ConstraintDescriptorFactory.existsForSchema( schema ), name );


case UNIQUE_CONSTRAINT: case UNIQUE_CONSTRAINT:
long ownedUniqueIndex = source.getLong(); long ownedUniqueIndex = source.getLong();
schema = readSchema( source ); schema = readSchema( source );
UniquenessConstraintDescriptor descriptor = ConstraintDescriptorFactory.uniqueForSchema( schema ); UniquenessConstraintDescriptor descriptor = ConstraintDescriptorFactory.uniqueForSchema( schema );
name = readRuleName( id, ConstraintRule.class, source ); name = readRuleName( source );
return ConstraintRule.constraintRule( id, descriptor, ownedUniqueIndex, name ); return ConstraintRule.constraintRule( id, descriptor, ownedUniqueIndex, name );


case UNIQUE_EXISTS_CONSTRAINT: case UNIQUE_EXISTS_CONSTRAINT:
long ownedNodeKeyIndex = source.getLong(); long ownedNodeKeyIndex = source.getLong();
schema = readSchema( source ); schema = readSchema( source );
NodeKeyConstraintDescriptor nodeKeyConstraintDescriptor = ConstraintDescriptorFactory.nodeKeyForSchema( schema ); NodeKeyConstraintDescriptor nodeKeyConstraintDescriptor = ConstraintDescriptorFactory.nodeKeyForSchema( schema );
name = readRuleName( id, ConstraintRule.class, source ); name = readRuleName( source );
return ConstraintRule.constraintRule( id, nodeKeyConstraintDescriptor, ownedNodeKeyIndex, name ); return ConstraintRule.constraintRule( id, nodeKeyConstraintDescriptor, ownedNodeKeyIndex, name );


default: default:
throw new MalformedSchemaRuleException( format( "Got unknown constraint rule type '%d'.", constraintRuleType ) ); throw new MalformedSchemaRuleException( format( "Got unknown constraint rule type '%d'.", constraintRuleType ) );
} }
} }


private static String readRuleName( long id, Class<? extends SchemaRule> type, ByteBuffer source ) private static String readRuleName( ByteBuffer source )
{ {
String ruleName = null; String ruleName = null;
if ( source.remaining() >= UTF8.MINIMUM_SERIALISED_LENGTH_BYTES ) if ( source.remaining() >= UTF8.MINIMUM_SERIALISED_LENGTH_BYTES )
{ {
ruleName = UTF8.getDecodedStringFrom( source ); ruleName = UTF8.getDecodedStringFrom( source );
} if ( ruleName.isEmpty() )
if ( ruleName == null || ruleName.isEmpty() ) {
{ ruleName = null;
ruleName = SchemaRule.generateName( id, type ); }
} }
return ruleName; return ruleName;
} }
Expand Down
Expand Up @@ -26,41 +26,32 @@
import org.neo4j.internal.kernel.api.schema.constraints.ConstraintDescriptor; import org.neo4j.internal.kernel.api.schema.constraints.ConstraintDescriptor;
import org.neo4j.kernel.api.exceptions.schema.MalformedSchemaRuleException; import org.neo4j.kernel.api.exceptions.schema.MalformedSchemaRuleException;
import org.neo4j.kernel.api.schema.index.SchemaIndexDescriptor; import org.neo4j.kernel.api.schema.index.SchemaIndexDescriptor;
import org.neo4j.kernel.impl.store.record.ConstraintRule;
import org.neo4j.kernel.impl.store.record.IndexRule;


/** /**
* Represents a stored schema rule. * Represents a stored schema rule.
*/ */
public abstract class SchemaRule implements SchemaDescriptorSupplier public interface SchemaRule extends SchemaDescriptorSupplier
{ {
protected final long id; static String nameOrDefault( String name, String defaultName )
protected final String name;

protected SchemaRule( long id )
{
this( id, null );
}

protected SchemaRule( long id, String name )
{ {
this.id = id; if ( name == null )
this.name = name == null ? generateName( id, getClass() ) : checkName( name ); {
} return defaultName;

}
private String checkName( String name ) else if ( name.isEmpty() )
{
int length = name.length();
if ( length == 0 )
{ {
throw new IllegalArgumentException( "Schema rule name cannot be the empty string" ); throw new IllegalArgumentException( "Schema rule name cannot be the empty string" );
} }
for ( int i = 0; i < length; i++ ) else
{ {
char ch = name.charAt( i ); int length = name.length();
if ( ch == '\0' ) for ( int i = 0; i < length; i++ )
{ {
throw new IllegalArgumentException( "Illegal schema rule name: '" + name + "'" ); char ch = name.charAt( i );
if ( ch == '\0' )
{
throw new IllegalArgumentException( "Illegal schema rule name: '" + name + "'" );
}
} }
} }
return name; return name;
Expand All @@ -69,33 +60,12 @@ private String checkName( String name )
/** /**
* The persistence id for this rule. * The persistence id for this rule.
*/ */
public final long getId() long getId();
{
return this.id;
}


/** /**
* @return The (possibly user supplied) name of this schema rule. * @return The (possibly user supplied) name of this schema rule.
*/ */
public final String getName() String getName();
{
return name;
}

public abstract byte[] serialize();

public static String generateName( long id, Class<? extends SchemaRule> type )
{
if ( type == IndexRule.class )
{
return "index_" + id;
}
if ( type == ConstraintRule.class )
{
return "constraint_" + id;
}
return "schema_" + id;
}


/** /**
* This enum is used for the legacy schema store, and should not be extended. * This enum is used for the legacy schema store, and should not be extended.
Expand Down
Expand Up @@ -91,7 +91,7 @@ public void storeAndLoadSchemaRule() throws Exception


// WHEN // WHEN
IndexRule readIndexRule = (IndexRule) SchemaRuleSerialization.deserialize( IndexRule readIndexRule = (IndexRule) SchemaRuleSerialization.deserialize(
indexRule.getId(), wrap( indexRule.serialize() ) ); indexRule.getId(), wrap( SchemaRuleSerialization.serialize( indexRule ) ) );


// THEN // THEN
assertEquals( indexRule.getId(), readIndexRule.getId() ); assertEquals( indexRule.getId(), readIndexRule.getId() );
Expand All @@ -110,7 +110,7 @@ public void storeAndLoadCompositeSchemaRule() throws Exception


// WHEN // WHEN
IndexRule readIndexRule = (IndexRule) SchemaRuleSerialization.deserialize( IndexRule readIndexRule = (IndexRule) SchemaRuleSerialization.deserialize(
indexRule.getId(), wrap( indexRule.serialize() ) ); indexRule.getId(), wrap( SchemaRuleSerialization.serialize( indexRule ) ) );


// THEN // THEN
assertEquals( indexRule.getId(), readIndexRule.getId() ); assertEquals( indexRule.getId(), readIndexRule.getId() );
Expand All @@ -128,7 +128,7 @@ public void storeAndLoad_Big_CompositeSchemaRule() throws Exception


// WHEN // WHEN
IndexRule readIndexRule = (IndexRule) SchemaRuleSerialization.deserialize( IndexRule readIndexRule = (IndexRule) SchemaRuleSerialization.deserialize(
indexRule.getId(), wrap( indexRule.serialize() ) ); indexRule.getId(), wrap( SchemaRuleSerialization.serialize( indexRule ) ) );


// THEN // THEN
assertEquals( indexRule.getId(), readIndexRule.getId() ); assertEquals( indexRule.getId(), readIndexRule.getId() );
Expand Down

0 comments on commit 1e2dc8f

Please sign in to comment.