From e0759ebe1b3bb6ace828624541b56d87cab9b12b Mon Sep 17 00:00:00 2001 From: Ragnar Mellbin Date: Wed, 13 Jun 2018 08:35:25 +0200 Subject: [PATCH] Cleanup of schema processors and computers --- .../checking/SchemaRecordCheck.java | 21 ++++--- .../checking/full/MandatoryProperties.java | 6 +- .../kernel/api/schema/SchemaComputer.java | 2 +- .../kernel/api/schema/SchemaProcessor.java | 2 +- .../ConstraintDescriptorFactory.java | 28 +++++---- .../store/record/SchemaRuleSerialization.java | 58 +++++-------------- .../storageengine/api/schema/SchemaRule.java | 6 +- .../api/schema/SchemaProcessorTest.java | 4 +- .../enterprise/PropertyExistenceEnforcer.java | 6 +- 9 files changed, 57 insertions(+), 76 deletions(-) diff --git a/community/consistency-check/src/main/java/org/neo4j/consistency/checking/SchemaRecordCheck.java b/community/consistency-check/src/main/java/org/neo4j/consistency/checking/SchemaRecordCheck.java index 31fb568959cd..6d48e3cef4e9 100644 --- a/community/consistency-check/src/main/java/org/neo4j/consistency/checking/SchemaRecordCheck.java +++ b/community/consistency-check/src/main/java/org/neo4j/consistency/checking/SchemaRecordCheck.java @@ -26,8 +26,8 @@ import org.neo4j.consistency.report.ConsistencyReport; import org.neo4j.consistency.store.RecordAccess; import org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor; -import org.neo4j.internal.kernel.api.schema.MultiTokenSchemaDescriptor; import org.neo4j.internal.kernel.api.schema.RelationTypeSchemaDescriptor; +import org.neo4j.internal.kernel.api.schema.SchemaDescriptor; import org.neo4j.internal.kernel.api.schema.SchemaProcessor; import org.neo4j.kernel.api.exceptions.schema.MalformedSchemaRuleException; import org.neo4j.kernel.api.schema.index.StoreIndexDescriptor; @@ -250,24 +250,18 @@ static class CheckSchema implements SchemaProcessor public void processSpecific( LabelSchemaDescriptor schema ) { engine.comparativeCheck( records.label( schema.getLabelId() ), VALID_LABEL ); - for ( int propertyId : schema.getPropertyIds() ) - { - engine.comparativeCheck( records.propertyKey( propertyId ), VALID_PROPERTY_KEY ); - } + checkProperties( schema.getPropertyIds() ); } @Override public void processSpecific( RelationTypeSchemaDescriptor schema ) { engine.comparativeCheck( records.relationshipType( schema.getRelTypeId() ), VALID_RELATIONSHIP_TYPE ); - for ( int propertyId : schema.getPropertyIds() ) - { - engine.comparativeCheck( records.propertyKey( propertyId ), VALID_PROPERTY_KEY ); - } + checkProperties( schema.getPropertyIds() ); } @Override - public void processSpecific( MultiTokenSchemaDescriptor schema ) + public void processSpecific( SchemaDescriptor schema ) { switch ( schema.entityType() ) { @@ -287,7 +281,12 @@ public void processSpecific( MultiTokenSchemaDescriptor schema ) throw new IllegalArgumentException( "Schema with given entity type is not supported: " + schema.entityType() ); } - for ( int propertyId : schema.getPropertyIds() ) + checkProperties( schema.getPropertyIds() ); + } + + private void checkProperties( int[] propertyIds ) + { + for ( int propertyId : propertyIds ) { engine.comparativeCheck( records.propertyKey( propertyId ), VALID_PROPERTY_KEY ); } diff --git a/community/consistency-check/src/main/java/org/neo4j/consistency/checking/full/MandatoryProperties.java b/community/consistency-check/src/main/java/org/neo4j/consistency/checking/full/MandatoryProperties.java index 7cafc9f393bc..df7e6abb2fd4 100644 --- a/community/consistency-check/src/main/java/org/neo4j/consistency/checking/full/MandatoryProperties.java +++ b/community/consistency-check/src/main/java/org/neo4j/consistency/checking/full/MandatoryProperties.java @@ -32,8 +32,8 @@ import org.neo4j.consistency.report.ConsistencyReport; import org.neo4j.consistency.report.ConsistencyReporter; import org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor; -import org.neo4j.internal.kernel.api.schema.MultiTokenSchemaDescriptor; import org.neo4j.internal.kernel.api.schema.RelationTypeSchemaDescriptor; +import org.neo4j.internal.kernel.api.schema.SchemaDescriptor; import org.neo4j.internal.kernel.api.schema.SchemaProcessor; import org.neo4j.kernel.impl.store.SchemaStorage; import org.neo4j.kernel.impl.store.StoreAccess; @@ -84,9 +84,9 @@ public void processSpecific( RelationTypeSchemaDescriptor schema ) } @Override - public void processSpecific( MultiTokenSchemaDescriptor multiTokenSchemaDescriptor ) + public void processSpecific( SchemaDescriptor schema ) { - throw new IllegalStateException( "MultiTokenSchemaDescriptor cannot support constraints" ); + throw new IllegalStateException( "General SchemaDescriptors cannot support constraints" ); } }; diff --git a/community/kernel-api/src/main/java/org/neo4j/internal/kernel/api/schema/SchemaComputer.java b/community/kernel-api/src/main/java/org/neo4j/internal/kernel/api/schema/SchemaComputer.java index 470d8f630709..443281ec5a05 100644 --- a/community/kernel-api/src/main/java/org/neo4j/internal/kernel/api/schema/SchemaComputer.java +++ b/community/kernel-api/src/main/java/org/neo4j/internal/kernel/api/schema/SchemaComputer.java @@ -39,5 +39,5 @@ public interface SchemaComputer */ R computeSpecific( LabelSchemaDescriptor schema ); R computeSpecific( RelationTypeSchemaDescriptor schema ); - R computeSpecific( MultiTokenSchemaDescriptor schema ); + R computeSpecific( SchemaDescriptor schema ); } diff --git a/community/kernel-api/src/main/java/org/neo4j/internal/kernel/api/schema/SchemaProcessor.java b/community/kernel-api/src/main/java/org/neo4j/internal/kernel/api/schema/SchemaProcessor.java index cd2323b397dc..8e5c48e3e43b 100644 --- a/community/kernel-api/src/main/java/org/neo4j/internal/kernel/api/schema/SchemaProcessor.java +++ b/community/kernel-api/src/main/java/org/neo4j/internal/kernel/api/schema/SchemaProcessor.java @@ -32,5 +32,5 @@ public interface SchemaProcessor */ void processSpecific( LabelSchemaDescriptor schema ); void processSpecific( RelationTypeSchemaDescriptor schema ); - void processSpecific( MultiTokenSchemaDescriptor multiTokenSchemaDescriptor ); + void processSpecific( SchemaDescriptor schema ); } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/api/schema/constaints/ConstraintDescriptorFactory.java b/community/kernel/src/main/java/org/neo4j/kernel/api/schema/constaints/ConstraintDescriptorFactory.java index 92c8384acc1d..2b2f8f48ef7c 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/api/schema/constaints/ConstraintDescriptorFactory.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/api/schema/constaints/ConstraintDescriptorFactory.java @@ -20,7 +20,6 @@ package org.neo4j.kernel.api.schema.constaints; import org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor; -import org.neo4j.internal.kernel.api.schema.MultiTokenSchemaDescriptor; import org.neo4j.internal.kernel.api.schema.RelationTypeSchemaDescriptor; import org.neo4j.internal.kernel.api.schema.SchemaComputer; import org.neo4j.internal.kernel.api.schema.SchemaDescriptor; @@ -96,10 +95,13 @@ public ConstraintDescriptor computeSpecific( RelationTypeSchemaDescriptor schema } @Override - public ConstraintDescriptor computeSpecific( MultiTokenSchemaDescriptor schema ) + public ConstraintDescriptor computeSpecific( SchemaDescriptor schema ) { - throw new IllegalStateException( "MultiToken schema rules cannot support constraints" ); - } + throw new UnsupportedOperationException( + format( "Cannot create existence constraint for schema '%s' of type %s", + schema.userDescription( SchemaUtil.idTokenNameLookup ), + schema.getClass().getSimpleName() + ) ); } }; private static SchemaComputer convertToUniquenessConstraint = @@ -122,10 +124,13 @@ public UniquenessConstraintDescriptor computeSpecific( RelationTypeSchemaDescrip } @Override - public UniquenessConstraintDescriptor computeSpecific( MultiTokenSchemaDescriptor schema ) + public UniquenessConstraintDescriptor computeSpecific( SchemaDescriptor schema ) { - throw new IllegalStateException( "MultiToken schema rules cannot support constraints" ); - } + throw new UnsupportedOperationException( + format( "Cannot create uniqueness constraint for schema '%s' of type %s", + schema.userDescription( SchemaUtil.idTokenNameLookup ), + schema.getClass().getSimpleName() + ) ); } }; private static SchemaComputer convertToNodeKeyConstraint = @@ -148,9 +153,12 @@ public NodeKeyConstraintDescriptor computeSpecific( RelationTypeSchemaDescriptor } @Override - public NodeKeyConstraintDescriptor computeSpecific( MultiTokenSchemaDescriptor schema ) + public NodeKeyConstraintDescriptor computeSpecific( SchemaDescriptor schema ) { - throw new IllegalStateException( "MultiToken schema rules cannot support constraints" ); - } + throw new UnsupportedOperationException( + format( "Cannot create node key constraint for schema '%s' of type %s", + schema.userDescription( SchemaUtil.idTokenNameLookup ), + schema.getClass().getSimpleName() + ) ); } }; } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/record/SchemaRuleSerialization.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/record/SchemaRuleSerialization.java index 776c5f0af791..a85cb9837174 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/record/SchemaRuleSerialization.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/record/SchemaRuleSerialization.java @@ -23,7 +23,6 @@ import java.util.Optional; import org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor; -import org.neo4j.internal.kernel.api.schema.MultiTokenSchemaDescriptor; import org.neo4j.internal.kernel.api.schema.RelationTypeSchemaDescriptor; import org.neo4j.internal.kernel.api.schema.SchemaComputer; import org.neo4j.internal.kernel.api.schema.SchemaDescriptor; @@ -64,7 +63,7 @@ public class SchemaRuleSerialization // Schema type private static final byte SIMPLE_LABEL = 91; private static final byte SIMPLE_REL_TYPE = 92; - private static final byte MULTI_TOKEN_TYPE = 93; + private static final byte GENERIC_MULTI_TOKEN_TYPE = 93; private static final long NO_OWNING_CONSTRAINT_YET = -1; private static final int LEGACY_LABEL_OR_REL_TYPE_ID = -1; @@ -277,17 +276,6 @@ private static StoreIndexDescriptor readIndexRule( long id, ByteBuffer source ) } - private static MultiTokenSchemaDescriptor readMultiTokenSchema( ByteBuffer source ) throws MalformedSchemaRuleException - { - SchemaDescriptor schemaDescriptor = readMultitokenSchema( source ); - if ( !(schemaDescriptor instanceof MultiTokenSchemaDescriptor) ) - { - throw new MalformedSchemaRuleException( - "Non schema StoreIndexDescriptors must have MultiTokenSchemaDescriptor, got " + schemaDescriptor.getClass().getSimpleName() ); - } - return (MultiTokenSchemaDescriptor) schemaDescriptor; - } - private static IndexProvider.Descriptor readIndexProviderDescriptor( ByteBuffer source ) { String providerKey = getDecodedStringFrom( source ); @@ -354,7 +342,7 @@ private static SchemaDescriptor readSchema( ByteBuffer source ) throws Malformed int relTypeId = source.getInt(); propertyIds = readTokenIdList( source ); return SchemaDescriptorFactory.forRelType( relTypeId, propertyIds ); - case MULTI_TOKEN_TYPE: + case GENERIC_MULTI_TOKEN_TYPE: return readMultiTokenSchema( source ); default: throw new MalformedSchemaRuleException( format( "Got unknown schema descriptor type '%d'.", @@ -362,7 +350,7 @@ private static SchemaDescriptor readSchema( ByteBuffer source ) throws Malformed } } - private static SchemaDescriptor readMultitokenSchema( ByteBuffer source ) throws MalformedSchemaRuleException + private static SchemaDescriptor readMultiTokenSchema( ByteBuffer source ) throws MalformedSchemaRuleException { byte schemaDescriptorType = source.get(); EntityType type; @@ -409,13 +397,7 @@ public void processSpecific( LabelSchemaDescriptor schema ) { target.put( SIMPLE_LABEL ); target.putInt( schema.getLabelId() ); - - int[] propertyIds = schema.getPropertyIds(); - target.putShort( (short)propertyIds.length ); - for ( int propertyId : propertyIds ) - { - target.putInt( propertyId ); - } + putIds( schema.getPropertyIds() ); } @Override @@ -423,19 +405,13 @@ public void processSpecific( RelationTypeSchemaDescriptor schema ) { target.put( SIMPLE_REL_TYPE ); target.putInt( schema.getRelTypeId() ); - - int[] propertyIds = schema.getPropertyIds(); - target.putShort( (short)propertyIds.length ); - for ( int propertyId : propertyIds ) - { - target.putInt( propertyId ); - } + putIds( schema.getPropertyIds() ); } @Override - public void processSpecific( MultiTokenSchemaDescriptor schema ) + public void processSpecific( SchemaDescriptor schema ) { - target.put( MULTI_TOKEN_TYPE ); + target.put( GENERIC_MULTI_TOKEN_TYPE ); if ( schema.entityType() == EntityType.NODE ) { target.put( SIMPLE_LABEL ); @@ -445,18 +421,16 @@ public void processSpecific( MultiTokenSchemaDescriptor schema ) target.put( SIMPLE_REL_TYPE ); } - int[] entityTokenIds = schema.getEntityTokenIds(); - target.putShort( (short) entityTokenIds.length ); - for ( int entityTokenId : entityTokenIds ) - { - target.putInt( entityTokenId ); - } + putIds( schema.getEntityTokenIds() ); + putIds( schema.getPropertyIds() ); + } - int[] propertyIds = schema.getPropertyIds(); - target.putShort( (short)propertyIds.length ); - for ( int propertyId : propertyIds ) + private void putIds( int[] ids ) + { + target.putShort( (short) ids.length ); + for ( int entityTokenId : ids ) { - target.putInt( propertyId ); + target.putInt( entityTokenId ); } } } @@ -484,7 +458,7 @@ public Integer computeSpecific( RelationTypeSchemaDescriptor schema ) } @Override - public Integer computeSpecific( MultiTokenSchemaDescriptor schema ) + public Integer computeSpecific( SchemaDescriptor schema ) { return 1 // schema descriptor type + 1 // entity token type diff --git a/community/kernel/src/main/java/org/neo4j/storageengine/api/schema/SchemaRule.java b/community/kernel/src/main/java/org/neo4j/storageengine/api/schema/SchemaRule.java index 10ef22e8e476..c99abb11ac4b 100644 --- a/community/kernel/src/main/java/org/neo4j/storageengine/api/schema/SchemaRule.java +++ b/community/kernel/src/main/java/org/neo4j/storageengine/api/schema/SchemaRule.java @@ -20,9 +20,9 @@ package org.neo4j.storageengine.api.schema; import org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor; -import org.neo4j.internal.kernel.api.schema.MultiTokenSchemaDescriptor; import org.neo4j.internal.kernel.api.schema.RelationTypeSchemaDescriptor; import org.neo4j.internal.kernel.api.schema.SchemaComputer; +import org.neo4j.internal.kernel.api.schema.SchemaDescriptor; import org.neo4j.internal.kernel.api.schema.SchemaDescriptorSupplier; import org.neo4j.internal.kernel.api.schema.constraints.ConstraintDescriptor; import org.neo4j.kernel.api.exceptions.schema.MalformedSchemaRuleException; @@ -173,9 +173,9 @@ public Kind computeSpecific( RelationTypeSchemaDescriptor schema ) } @Override - public Kind computeSpecific( MultiTokenSchemaDescriptor schema ) + public Kind computeSpecific( SchemaDescriptor schema ) { - throw new IllegalStateException( "MultiToken schema rules cannot support constraints" ); + throw new IllegalStateException( "General schema rules cannot support constraints" ); } }; } diff --git a/community/kernel/src/test/java/org/neo4j/kernel/api/schema/SchemaProcessorTest.java b/community/kernel/src/test/java/org/neo4j/kernel/api/schema/SchemaProcessorTest.java index 911730790f27..1a1953845aed 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/api/schema/SchemaProcessorTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/api/schema/SchemaProcessorTest.java @@ -54,9 +54,9 @@ public void processSpecific( org.neo4j.internal.kernel.api.schema.RelationTypeSc } @Override - public void processSpecific( org.neo4j.internal.kernel.api.schema.MultiTokenSchemaDescriptor multiTokenSchemaDescriptor ) + public void processSpecific( org.neo4j.internal.kernel.api.schema.SchemaDescriptor schemaDescriptor ) { - callHistory.add( "MultiTokenSchemaDescriptor" ); + callHistory.add( "SchemaDescriptor" ); } }; diff --git a/enterprise/kernel/src/main/java/org/neo4j/kernel/impl/enterprise/PropertyExistenceEnforcer.java b/enterprise/kernel/src/main/java/org/neo4j/kernel/impl/enterprise/PropertyExistenceEnforcer.java index 355a81ae9221..c97a37bc4b4a 100644 --- a/enterprise/kernel/src/main/java/org/neo4j/kernel/impl/enterprise/PropertyExistenceEnforcer.java +++ b/enterprise/kernel/src/main/java/org/neo4j/kernel/impl/enterprise/PropertyExistenceEnforcer.java @@ -45,8 +45,8 @@ import org.neo4j.internal.kernel.api.RelationshipScanCursor; import org.neo4j.internal.kernel.api.exceptions.schema.ConstraintValidationException; import org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor; -import org.neo4j.internal.kernel.api.schema.MultiTokenSchemaDescriptor; import org.neo4j.internal.kernel.api.schema.RelationTypeSchemaDescriptor; +import org.neo4j.internal.kernel.api.schema.SchemaDescriptor; import org.neo4j.internal.kernel.api.schema.SchemaProcessor; import org.neo4j.internal.kernel.api.schema.constraints.ConstraintDescriptor; import org.neo4j.kernel.api.exceptions.schema.NodePropertyExistenceException; @@ -145,9 +145,9 @@ public void processSpecific( RelationTypeSchemaDescriptor schema ) } @Override - public void processSpecific( MultiTokenSchemaDescriptor multiTokenSchemaDescriptor ) + public void processSpecific( SchemaDescriptor schema ) { - throw new IllegalStateException( "MultiTokenSchemaDescriptor cannot support constraints" ); + throw new UnsupportedOperationException( "General SchemaDescriptor cannot support constraints" ); } } ); }