Skip to content

Commit

Permalink
Cleaned up SchemaStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
fickludd committed Jan 30, 2017
1 parent feb6d37 commit 1b646e6
Show file tree
Hide file tree
Showing 17 changed files with 174 additions and 195 deletions.
Expand Up @@ -131,43 +131,9 @@ public Function<NodeRecord,Check<NodeRecord,ConsistencyReport.NodeConsistencyRep
}; };
} }


/**
* The iterators returned by the SchemaStorage will throw a `MalformedSchemaRuleException` wrapped in a
* `RuntimeException` if it encounters malformed schema rules. As the iterator implementation is prefetching this
* will manifest in the hasNext() call. Attempting again will actually attempt to read the schema rule after the
* malformed one. This method will therefore execute `hasNext()` until 1) it reads a complete schema rule, 2) all
* schema rules are read, or 3) it get's an exception that is not wrapping a `MalformedSchemaRuleException`.
*/
private Iterable<ConstraintRule> constraintsIgnoringMalformed( SchemaStorage schemaStorage ) private Iterable<ConstraintRule> constraintsIgnoringMalformed( SchemaStorage schemaStorage )
{ {
final Iterator<ConstraintRule> ruleIterator = schemaStorage.allConstraintRules(); return schemaStorage::constraintsGetAllIgnoreMalformed;
return () -> new Iterator<ConstraintRule>()
{
@Override
public boolean hasNext()
{
while ( true )
{
try
{
return ruleIterator.hasNext();
}
catch ( Exception e )
{
if ( !MalformedSchemaRuleException.class.isInstance( e.getCause() ) )
{
throw e;
}
}
}
}

@Override
public ConstraintRule next()
{
return ruleIterator.next();
}
};
} }


private static void recordConstraint( int labelOrRelType, int propertyKey, PrimitiveIntObjectMap<int[]> storage ) private static void recordConstraint( int labelOrRelType, int propertyKey, PrimitiveIntObjectMap<int[]> storage )
Expand Down
Expand Up @@ -35,7 +35,7 @@ public static Iterable<IndexRule> loadAllIndexRules( final RecordStore<DynamicRe
@Override @Override
public Iterator<IndexRule> iterator() public Iterator<IndexRule> iterator()
{ {
return new SchemaStorage( schemaStore ).allIndexRules(); return new SchemaStorage( schemaStore ).indexesGetAll();
} }
}; };
} }
Expand Down
Expand Up @@ -410,7 +410,7 @@ public void shouldNotReportIndexInconsistenciesIfIndexIsFailed() throws Exceptio
DirectStoreAccess storeAccess = fixture.directStoreAccess(); DirectStoreAccess storeAccess = fixture.directStoreAccess();


// fail all indexes // fail all indexes
Iterator<IndexRule> rules = new SchemaStorage( storeAccess.nativeStores().getSchemaStore() ).allIndexRules(); Iterator<IndexRule> rules = new SchemaStorage( storeAccess.nativeStores().getSchemaStore() ).indexesGetAll();
while ( rules.hasNext() ) while ( rules.hasNext() )
{ {
IndexRule rule = rules.next(); IndexRule rule = rules.next();
Expand Down
Expand Up @@ -34,4 +34,19 @@ public interface SchemaDescriptor
* @return a user friendly description of what this index indexes. * @return a user friendly description of what this index indexes.
*/ */
String userDescription( TokenNameLookup tokenNameLookup ); String userDescription( TokenNameLookup tokenNameLookup );

/**
* Checks whether a schema descriptor Supplier supplies this schema descriptor.
* @param supplier supplier to get a schema descriptor from
* @return true if the supplied schema descriptor equals this schema descriptor
*/
default boolean isSame( Supplier supplier )
{
return this.equals( supplier.getSchemaDescriptor() );
}

interface Supplier
{
SchemaDescriptor getSchemaDescriptor();
}
} }
Expand Up @@ -25,19 +25,19 @@


public class SchemaDescriptorPredicates public class SchemaDescriptorPredicates
{ {
public static boolean hasLabel( SchemaDescriptorSupplier supplier, int labelId ) public static boolean hasLabel( SchemaDescriptor.Supplier supplier, int labelId )
{ {
Optional<Integer> labelOpt = getLabel.compute( supplier.getSchemaDescriptor() ); Optional<Integer> labelOpt = getLabel.compute( supplier.getSchemaDescriptor() );
return labelOpt.isPresent() && labelOpt.get() == labelId; return labelOpt.isPresent() && labelOpt.get() == labelId;
} }


public static boolean hasRelType( SchemaDescriptorSupplier supplier, int relTypeId ) public static boolean hasRelType( SchemaDescriptor.Supplier supplier, int relTypeId )
{ {
Optional<Integer> relTypeOpt = getRelType.compute( supplier.getSchemaDescriptor() ); Optional<Integer> relTypeOpt = getRelType.compute( supplier.getSchemaDescriptor() );
return relTypeOpt.isPresent() && relTypeOpt.get() == relTypeId; return relTypeOpt.isPresent() && relTypeOpt.get() == relTypeId;
} }


public static boolean hasProperty( SchemaDescriptorSupplier supplier, int propertyId ) public static boolean hasProperty( SchemaDescriptor.Supplier supplier, int propertyId )
{ {
List<Integer> properties = getProperties.compute( supplier.getSchemaDescriptor() ); List<Integer> properties = getProperties.compute( supplier.getSchemaDescriptor() );
return properties.contains( propertyId ); return properties.contains( propertyId );
Expand Down

This file was deleted.

Expand Up @@ -35,4 +35,19 @@ enum Type { UNIQUE, EXISTS }
* @return a user friendly description of what this index indexes. * @return a user friendly description of what this index indexes.
*/ */
String userDescription( TokenNameLookup tokenNameLookup ); String userDescription( TokenNameLookup tokenNameLookup );

/**
* Checks whether a constraint descriptor Supplier supplies this constraint descriptor.
* @param supplier supplier to get a constraint descriptor from
* @return true if the supplied constraint descriptor equals this constraint descriptor
*/
default boolean isSame( Supplier supplier )
{
return this.equals( supplier.getConstraintDescriptor() );
}

interface Supplier
{
ConstraintDescriptor getConstraintDescriptor();
}
} }
Expand Up @@ -39,4 +39,19 @@ enum Type { GENERAL, UNIQUE }
* @return a user friendly description of what this index indexes. * @return a user friendly description of what this index indexes.
*/ */
String userDescription( TokenNameLookup tokenNameLookup ); String userDescription( TokenNameLookup tokenNameLookup );

/**
* Checks whether a constraint descriptor Supplier supplies this constraint descriptor.
* @param supplier supplier to get a constraint descriptor from
* @return true if the supplied constraint descriptor equals this constraint descriptor
*/
default boolean isSame( Supplier supplier )
{
return this.equals( supplier.getNewIndexDescriptor() );
}

interface Supplier
{
NewIndexDescriptor getNewIndexDescriptor();
}
} }
Expand Up @@ -45,6 +45,7 @@
import org.neo4j.kernel.api.index.InternalIndexState; import org.neo4j.kernel.api.index.InternalIndexState;
import org.neo4j.kernel.api.properties.PropertyKeyIdIterator; import org.neo4j.kernel.api.properties.PropertyKeyIdIterator;
import org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor; import org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor;
import org.neo4j.kernel.api.schema_new.RelationTypeSchemaDescriptor;
import org.neo4j.kernel.api.schema_new.SchemaDescriptorFactory; import org.neo4j.kernel.api.schema_new.SchemaDescriptorFactory;
import org.neo4j.kernel.api.schema_new.SchemaDescriptorPredicates; import org.neo4j.kernel.api.schema_new.SchemaDescriptorPredicates;
import org.neo4j.kernel.api.schema_new.constaints.ConstraintBoundary; import org.neo4j.kernel.api.schema_new.constaints.ConstraintBoundary;
Expand Down Expand Up @@ -95,6 +96,9 @@ public class DiskLayer implements StoreReadLayer
private static final Function<ConstraintRule,RelationshipPropertyConstraint> RULE_TO_REL_CONSTRAINT = private static final Function<ConstraintRule,RelationshipPropertyConstraint> RULE_TO_REL_CONSTRAINT =
constraintRule -> ConstraintBoundary.mapRelationship( constraintRule.getConstraintDescriptor() ); constraintRule -> ConstraintBoundary.mapRelationship( constraintRule.getConstraintDescriptor() );


private static final Predicate<IndexRule> INDEX_RULES = rule -> !rule.canSupportUniqueConstraint();
private static final Predicate<IndexRule> UNIQUENESS_INDEX_RULES = rule -> rule.canSupportUniqueConstraint();

// These token holders should perhaps move to the cache layer.. not really any reason to have them here? // These token holders should perhaps move to the cache layer.. not really any reason to have them here?
private final PropertyKeyTokenHolder propertyKeyTokenHolder; private final PropertyKeyTokenHolder propertyKeyTokenHolder;
private final LabelTokenHolder labelTokenHolder; private final LabelTokenHolder labelTokenHolder;
Expand Down Expand Up @@ -181,7 +185,7 @@ public PrimitiveLongIterator nodesGetForLabel( StorageStatement statement, int l
@Override @Override
public IndexDescriptor indexGetForLabelAndPropertyKey( NodePropertyDescriptor descriptor ) public IndexDescriptor indexGetForLabelAndPropertyKey( NodePropertyDescriptor descriptor )
{ {
return descriptor( schemaStorage.indexRule( return descriptor( schemaStorage.indexGetForSchema(
SchemaDescriptorFactory.forLabel( descriptor.getLabelId(), descriptor.getPropertyKeyId() ) ) ); SchemaDescriptorFactory.forLabel( descriptor.getLabelId(), descriptor.getPropertyKeyId() ) ) );
} }


Expand Down Expand Up @@ -213,38 +217,35 @@ public Iterator<IndexDescriptor> uniquenessIndexesGetForLabel( int labelId )
@Override @Override
public Iterator<IndexDescriptor> uniquenessIndexesGetAll() public Iterator<IndexDescriptor> uniquenessIndexesGetAll()
{ {
return getIndexDescriptorsFor( CONSTRAINT_INDEX_RULES ); return getIndexDescriptorsFor( UNIQUENESS_INDEX_RULES );
} }


private static final Predicate<IndexRule> INDEX_RULES = rule -> !rule.canSupportUniqueConstraint();
private static final Predicate<IndexRule> CONSTRAINT_INDEX_RULES = rule -> rule.canSupportUniqueConstraint();

private Iterator<IndexDescriptor> getIndexDescriptorsFor( Predicate<IndexRule> filter ) private Iterator<IndexDescriptor> getIndexDescriptorsFor( Predicate<IndexRule> filter )
{ {
Iterator<IndexRule> filtered = Iterators.filter( filter, schemaStorage.allIndexRules() ); Iterator<IndexRule> filtered = Iterators.filter( filter, schemaStorage.indexesGetAll() );


return Iterators.map( DiskLayer::descriptor, filtered ); return Iterators.map( DiskLayer::descriptor, filtered );
} }


@Override @Override
public Long indexGetOwningUniquenessConstraintId( IndexDescriptor index ) throws SchemaRuleNotFoundException public Long indexGetOwningUniquenessConstraintId( IndexDescriptor index ) throws SchemaRuleNotFoundException
{ {
return schemaStorage.indexRule( return schemaStorage.indexGetForSchema(
SchemaDescriptorFactory.forLabel( index.getLabelId(), index.getPropertyKeyId() ) ).getOwningConstraint(); SchemaDescriptorFactory.forLabel( index.getLabelId(), index.getPropertyKeyId() ) ).getOwningConstraint();
} }


@Override @Override
public IndexRule indexRule( IndexDescriptor index, Predicate<IndexRule> filter ) public IndexRule indexRule( IndexDescriptor index, Predicate<IndexRule> filter )
{ {
return schemaStorage.indexRule( return schemaStorage.indexGetForSchema(
SchemaDescriptorFactory.forLabel( index.getLabelId(), index.getPropertyKeyId() ), filter ); SchemaDescriptorFactory.forLabel( index.getLabelId(), index.getPropertyKeyId() ), filter );
} }


@Override @Override
public long indexGetCommittedId( IndexDescriptor index, Predicate<IndexRule> filter ) public long indexGetCommittedId( IndexDescriptor index, Predicate<IndexRule> filter )
throws SchemaRuleNotFoundException throws SchemaRuleNotFoundException
{ {
return schemaStorage.indexRule( return schemaStorage.indexGetForSchema(
SchemaDescriptorFactory.forLabel( index.getLabelId(), index.getPropertyKeyId() ) ).getId(); SchemaDescriptorFactory.forLabel( index.getLabelId(), index.getPropertyKeyId() ) ).getId();
} }


Expand Down Expand Up @@ -285,40 +286,36 @@ public Iterator<NodePropertyConstraint> constraintsGetForLabelAndPropertyKey( No
{ {
LabelSchemaDescriptor schemaDescriptor = LabelSchemaDescriptor schemaDescriptor =
SchemaDescriptorFactory.forLabel( descriptor.getLabelId(), descriptor.getPropertyKeyId() ); SchemaDescriptorFactory.forLabel( descriptor.getLabelId(), descriptor.getPropertyKeyId() );
return schemaStorage.mapConstraintRulesFor(
rule -> rule.getSchemaDescriptor().equals( schemaDescriptor ), return Iterators.map( RULE_TO_NODE_CONSTRAINT, schemaStorage.constraintsGetForSchema( schemaDescriptor ) );
RULE_TO_NODE_CONSTRAINT );
} }


@Override @Override
public Iterator<NodePropertyConstraint> constraintsGetForLabel( int labelId ) public Iterator<NodePropertyConstraint> constraintsGetForLabel( int labelId )
{ {
return schemaStorage.mapConstraintRulesFor( return Iterators.map( RULE_TO_NODE_CONSTRAINT, schemaStorage.constraintsGetForLabel( labelId ) );
rule -> SchemaDescriptorPredicates.hasLabel( rule, labelId ),
RULE_TO_NODE_CONSTRAINT );
} }


@Override @Override
public Iterator<RelationshipPropertyConstraint> constraintsGetForRelationshipTypeAndPropertyKey( public Iterator<RelationshipPropertyConstraint> constraintsGetForRelationshipTypeAndPropertyKey(
RelationshipPropertyDescriptor descriptor ) RelationshipPropertyDescriptor descriptor )
{ {
return schemaStorage.mapConstraintRulesFor( RelationTypeSchemaDescriptor schemaDescriptor =
rule -> rule.getSchemaDescriptor().equals( descriptor ), SchemaDescriptorFactory.forRelType( descriptor.getRelationshipTypeId(), descriptor.getPropertyKeyId() );
RULE_TO_REL_CONSTRAINT );
return Iterators.map( RULE_TO_REL_CONSTRAINT, schemaStorage.constraintsGetForSchema( schemaDescriptor ) );
} }


@Override @Override
public Iterator<RelationshipPropertyConstraint> constraintsGetForRelationshipType( int typeId ) public Iterator<RelationshipPropertyConstraint> constraintsGetForRelationshipType( int typeId )
{ {
return schemaStorage.mapConstraintRulesFor( return Iterators.map( RULE_TO_REL_CONSTRAINT, schemaStorage.constraintsGetForRelType( typeId ) );
rule -> SchemaDescriptorPredicates.hasRelType( rule, typeId ),
RULE_TO_REL_CONSTRAINT );
} }


@Override @Override
public Iterator<PropertyConstraint> constraintsGetAll() public Iterator<PropertyConstraint> constraintsGetAll()
{ {
return schemaStorage.mapConstraintRulesFor( always -> true, RULE_TO_CONSTRAINT ); return Iterators.map( RULE_TO_CONSTRAINT, schemaStorage.constraintsGetAll() );
} }


@Override @Override
Expand Down
Expand Up @@ -225,7 +225,7 @@ public RecordStorageEngine(
indexStoreView = new DynamicIndexStoreView( labelScanStore, lockService, neoStores, logProvider ); indexStoreView = new DynamicIndexStoreView( labelScanStore, lockService, neoStores, logProvider );
indexingService = IndexingServiceFactory.createIndexingService( config, scheduler, schemaIndexProviderMap, indexingService = IndexingServiceFactory.createIndexingService( config, scheduler, schemaIndexProviderMap,
indexStoreView, tokenNameLookup, indexStoreView, tokenNameLookup,
Iterators.asList( new SchemaStorage( neoStores.getSchemaStore() ).allIndexRules() ), logProvider, Iterators.asList( new SchemaStorage( neoStores.getSchemaStore() ).indexesGetAll() ), logProvider,
indexingServiceMonitor, schemaStateChangeCallback ); indexingServiceMonitor, schemaStateChangeCallback );


integrityValidator = new IntegrityValidator( neoStores, indexingService ); integrityValidator = new IntegrityValidator( neoStores, indexingService );
Expand Down
Expand Up @@ -208,7 +208,7 @@ public void visitRemovedIndex( IndexDescriptor desc, boolean isConstraintIndex )
SchemaStorage.IndexRuleKind kind = isConstraintIndex ? SchemaStorage.IndexRuleKind kind = isConstraintIndex ?
SchemaStorage.IndexRuleKind.CONSTRAINT SchemaStorage.IndexRuleKind.CONSTRAINT
: SchemaStorage.IndexRuleKind.INDEX; : SchemaStorage.IndexRuleKind.INDEX;
IndexRule rule = schemaStorage.indexRule( IndexRule rule = schemaStorage.indexGetForSchema(
SchemaDescriptorFactory.forLabel( desc.getLabelId(), desc.getPropertyKeyId() ), kind ); SchemaDescriptorFactory.forLabel( desc.getLabelId(), desc.getPropertyKeyId() ), kind );
recordState.dropSchemaRule( rule ); recordState.dropSchemaRule( rule );
} }
Expand All @@ -220,7 +220,7 @@ public void visitAddedUniquePropertyConstraint( UniquenessConstraint element )
long constraintId = schemaStorage.newRuleId(); long constraintId = schemaStorage.newRuleId();
int propertyKeyId = element.descriptor().getPropertyKeyId(); int propertyKeyId = element.descriptor().getPropertyKeyId();


IndexRule indexRule = schemaStorage.indexRule( IndexRule indexRule = schemaStorage.indexGetForSchema(
SchemaDescriptorFactory.forLabel( element.label(), propertyKeyId ), SchemaDescriptorFactory.forLabel( element.label(), propertyKeyId ),
SchemaStorage.IndexRuleKind.CONSTRAINT ); SchemaStorage.IndexRuleKind.CONSTRAINT );
recordState.createSchemaRule( recordState.createSchemaRule(
Expand All @@ -237,7 +237,7 @@ public void visitRemovedUniquePropertyConstraint( UniquenessConstraint element )
clearSchemaState = true; clearSchemaState = true;
int propertyKeyId = element.descriptor().getPropertyKeyId(); int propertyKeyId = element.descriptor().getPropertyKeyId();


recordState.dropSchemaRule( schemaStorage.singleConstraintRule( recordState.dropSchemaRule( schemaStorage.constraintsGetSingle(
ConstraintDescriptorFactory.uniqueForLabel( element.label(), propertyKeyId ) ConstraintDescriptorFactory.uniqueForLabel( element.label(), propertyKeyId )
) ); ) );
} }
Expand Down Expand Up @@ -272,7 +272,7 @@ public void visitRemovedNodePropertyExistenceConstraint( NodePropertyExistenceCo
clearSchemaState = true; clearSchemaState = true;
int propertyKeyId = element.descriptor().getPropertyKeyId(); int propertyKeyId = element.descriptor().getPropertyKeyId();


recordState.dropSchemaRule( schemaStorage.singleConstraintRule( recordState.dropSchemaRule( schemaStorage.constraintsGetSingle(
ConstraintDescriptorFactory.existsForLabel( element.label(), propertyKeyId ) ConstraintDescriptorFactory.existsForLabel( element.label(), propertyKeyId )
) ); ) );
} }
Expand Down Expand Up @@ -306,7 +306,7 @@ public void visitRemovedRelationshipPropertyExistenceConstraint( RelationshipPro
//TODO: Support composite indexes //TODO: Support composite indexes
clearSchemaState = true; clearSchemaState = true;


recordState.dropSchemaRule( schemaStorage.singleConstraintRule( recordState.dropSchemaRule( schemaStorage.constraintsGetSingle(
ConstraintDescriptorFactory.existsForRelType( ConstraintDescriptorFactory.existsForRelType(
element.descriptor().getRelationshipTypeId(), element.descriptor().getPropertyKeyId() ) element.descriptor().getRelationshipTypeId(), element.descriptor().getPropertyKeyId() )
) ); ) );
Expand Down

0 comments on commit 1b646e6

Please sign in to comment.