Skip to content

Commit

Permalink
Pushed IndexDescriptor boundary on StoreReadLayer. 4 methods left.
Browse files Browse the repository at this point in the history
  • Loading branch information
fickludd committed Feb 9, 2017
1 parent 3a3e2ea commit d9b57fc
Show file tree
Hide file tree
Showing 16 changed files with 231 additions and 148 deletions.
Expand Up @@ -19,10 +19,9 @@
*/
package org.neo4j.kernel.api.constraints;

import org.neo4j.kernel.api.schema.NodePropertyDescriptor;
import org.neo4j.kernel.api.TokenNameLookup;
import org.neo4j.kernel.api.schema.IndexDescriptor;
import org.neo4j.kernel.api.schema.IndexDescriptorFactory;
import org.neo4j.kernel.api.schema.NodePropertyDescriptor;
import org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor;

/**
* Base class describing property constraint on nodes.
Expand Down Expand Up @@ -51,6 +50,14 @@ public boolean matches( NodePropertyDescriptor descriptor )
return this.descriptor.equals( descriptor );
}

public boolean matches( LabelSchemaDescriptor other )
{
return other != null &&
descriptor.getLabelId() == other.getLabelId() &&
descriptor.getPropertyKeyId() == other.getPropertyIds()[0];
// this is safe because we are replacing this class before introducing composite constraints
}

@Override
public boolean equals( Object o )
{
Expand Down
Expand Up @@ -20,6 +20,7 @@
package org.neo4j.kernel.api.constraints;

import org.neo4j.kernel.api.schema.RelationshipPropertyDescriptor;
import org.neo4j.kernel.api.schema_new.RelationTypeSchemaDescriptor;

/**
* Base class describing property constraint on relationships.
Expand All @@ -44,6 +45,14 @@ public boolean matches( RelationshipPropertyDescriptor descriptor )
return this.descriptor.equals( descriptor );
}

public boolean matches( RelationTypeSchemaDescriptor other )
{
return other != null &&
descriptor.getRelationshipTypeId() == other.getRelTypeId() &&
descriptor.getPropertyKeyId() == other.getPropertyIds()[0];
// this is safe because we are replacing this class before introducing composite constraints
}

@Override
public boolean equals( Object o )
{
Expand Down
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.api.schema_new;

import org.neo4j.kernel.api.schema.NodePropertyDescriptor;
import org.neo4j.kernel.api.schema.RelationshipPropertyDescriptor;

/**
* This class represents the boundary of where new schema descriptors are converted to old descriptors. This class
* should disappear once the old schema descriptors are no longer used.
*/
public class SchemaBoundary
{
private SchemaBoundary()
{
}

public static LabelSchemaDescriptor map( NodePropertyDescriptor descriptor )
{
return descriptor.isComposite() ?
SchemaDescriptorFactory.forLabel( descriptor.getLabelId(), descriptor.getPropertyKeyIds() ) :
SchemaDescriptorFactory.forLabel( descriptor.getLabelId(), descriptor.getPropertyKeyId() );
}

public static RelationTypeSchemaDescriptor map( RelationshipPropertyDescriptor descriptor )
{
return SchemaDescriptorFactory.forRelType( descriptor.getRelationshipTypeId(), descriptor.getPropertyKeyId() );
}
}
Expand Up @@ -32,7 +32,8 @@
import org.neo4j.kernel.api.schema_new.SchemaComputer;

/**
* This class represents the boundary of where new constraint descriptors are converted to old constraints
* This class represents the boundary of where new constraint descriptors are converted to old constraints. This class
* should disappear once the old constraints are no longer used.
*/
public class ConstraintBoundary
{
Expand Down
Expand Up @@ -19,23 +19,38 @@
*/
package org.neo4j.kernel.api.schema_new.index;

import java.util.Iterator;

import org.neo4j.helpers.collection.Iterators;
import org.neo4j.kernel.api.schema.IndexDescriptor;
import org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor;
import org.neo4j.kernel.api.schema.IndexDescriptorFactory;

/**
* This class represents the boundary of where new index descriptors are converted to old index descriptors
* This class represents the boundary of where new index descriptors are converted to old index descriptors. Take me
* away when possible...
*/
public class IndexBoundary
{
public static IndexDescriptor map( NewIndexDescriptor descriptor )
{
LabelSchemaDescriptor labelSchema = (LabelSchemaDescriptor) descriptor.schema();
return org.neo4j.kernel.api.schema.IndexDescriptorFactory.of(
labelSchema.getLabelId(), labelSchema.getPropertyIds()[0] );
if ( descriptor == null )
{
return null;
}
return IndexDescriptorFactory.of( descriptor.schema().getLabelId(), descriptor.schema().getPropertyIds()[0] );
}

public static NewIndexDescriptor map( IndexDescriptor descriptor )
{
if ( descriptor == null )
{
return null;
}
return NewIndexDescriptorFactory.forLabel( descriptor.getLabelId(), descriptor.getPropertyKeyId() );
}

public static Iterator<IndexDescriptor> map( Iterator<NewIndexDescriptor> iterator )
{
return Iterators.map( IndexBoundary::map, iterator );
}
}
Expand Up @@ -67,6 +67,8 @@
import org.neo4j.kernel.api.schema.IndexDescriptorFactory;
import org.neo4j.kernel.api.schema.NodePropertyDescriptor;
import org.neo4j.kernel.api.schema.RelationshipPropertyDescriptor;
import org.neo4j.kernel.api.schema_new.SchemaBoundary;
import org.neo4j.kernel.api.schema_new.index.IndexBoundary;
import org.neo4j.kernel.api.txstate.TransactionCountingStateVisitor;
import org.neo4j.kernel.api.txstate.TransactionState;
import org.neo4j.kernel.impl.api.operations.CountsOperations;
Expand Down Expand Up @@ -406,7 +408,8 @@ public UniquenessConstraint uniquePropertyConstraintCreate( KernelStatement stat
}
else // *CREATE*
{ // create from scratch
for ( Iterator<NodePropertyConstraint> it = storeLayer.constraintsGetForLabelAndPropertyKey( descriptor );
for ( Iterator<NodePropertyConstraint> it = storeLayer.constraintsGetForLabelAndPropertyKey(
SchemaBoundary.map( descriptor) );
it.hasNext(); )
{
if ( it.next().equals( constraint ) )
Expand Down Expand Up @@ -451,7 +454,7 @@ public Iterator<NodePropertyConstraint> constraintsGetForLabelAndPropertyKey( Ke
NodePropertyDescriptor descriptor )
{
Iterator<NodePropertyConstraint> constraints =
storeLayer.constraintsGetForLabelAndPropertyKey(descriptor );
storeLayer.constraintsGetForLabelAndPropertyKey( SchemaBoundary.map( descriptor ) );
if ( state.hasTxStateWithChanges() )
{
return state.txState().constraintsChangesForLabelAndProperty( descriptor ).apply( constraints );
Expand All @@ -475,7 +478,7 @@ public Iterator<RelationshipPropertyConstraint> constraintsGetForRelationshipTyp
KernelStatement state, RelationshipPropertyDescriptor descriptor )
{
Iterator<RelationshipPropertyConstraint> constraints =
storeLayer.constraintsGetForRelationshipTypeAndPropertyKey( descriptor );
storeLayer.constraintsGetForRelationshipTypeAndPropertyKey( SchemaBoundary.map( descriptor ) );
if ( state.hasTxStateWithChanges() )
{
return state.txState()
Expand Down Expand Up @@ -524,7 +527,8 @@ public void constraintDrop( KernelStatement state, RelationshipPropertyConstrain
@Override
public IndexDescriptor indexGetForLabelAndPropertyKey( KernelStatement state, NodePropertyDescriptor descriptor )
{
IndexDescriptor indexDescriptor = storeLayer.indexGetForLabelAndPropertyKey( descriptor );
IndexDescriptor indexDescriptor = IndexBoundary.map(
storeLayer.indexGetForLabelAndPropertyKey( SchemaBoundary.map( descriptor ) ) );

Iterator<IndexDescriptor> rules = iterator( indexDescriptor );
if ( state.hasTxStateWithChanges() )
Expand Down Expand Up @@ -610,21 +614,21 @@ public Iterator<IndexDescriptor> indexesGetForLabel( KernelStatement state, int
if ( state.hasTxStateWithChanges() )
{
return state.txState().indexDiffSetsByLabel( labelId )
.apply( storeLayer.indexesGetForLabel( labelId ) );
.apply( IndexBoundary.map( storeLayer.indexesGetForLabel( labelId ) ) );
}

return storeLayer.indexesGetForLabel( labelId );
return IndexBoundary.map( storeLayer.indexesGetForLabel( labelId ) );
}

@Override
public Iterator<IndexDescriptor> indexesGetAll( KernelStatement state )
{
if ( state.hasTxStateWithChanges() )
{
return state.txState().indexChanges().apply( storeLayer.indexesGetAll() );
return state.txState().indexChanges().apply( IndexBoundary.map( storeLayer.indexesGetAll() ) );
}

return storeLayer.indexesGetAll();
return IndexBoundary.map( storeLayer.indexesGetAll() );
}

@Override
Expand All @@ -633,10 +637,10 @@ public Iterator<IndexDescriptor> uniqueIndexesGetForLabel( KernelStatement state
if ( state.hasTxStateWithChanges() )
{
return state.txState().constraintIndexDiffSetsByLabel( labelId )
.apply( storeLayer.uniquenessIndexesGetForLabel( labelId ) );
.apply( IndexBoundary.map( storeLayer.uniquenessIndexesGetForLabel( labelId ) ) );
}

return storeLayer.uniquenessIndexesGetForLabel( labelId );
return IndexBoundary.map( storeLayer.uniquenessIndexesGetForLabel( labelId ) );
}

@Override
Expand All @@ -645,10 +649,10 @@ public Iterator<IndexDescriptor> uniqueIndexesGetAll( KernelStatement state )
if ( state.hasTxStateWithChanges() )
{
return state.txState().constraintIndexChanges()
.apply( storeLayer.uniquenessIndexesGetAll() );
.apply( IndexBoundary.map( storeLayer.uniquenessIndexesGetAll() ) );
}

return storeLayer.uniquenessIndexesGetAll();
return IndexBoundary.map( storeLayer.uniquenessIndexesGetAll() );
}

@Override
Expand Down Expand Up @@ -1168,14 +1172,14 @@ public DoubleLongRegister indexSample( KernelStatement statement, IndexDescripto
public Long indexGetOwningUniquenessConstraintId( KernelStatement state, IndexDescriptor index )
throws SchemaRuleNotFoundException
{
return storeLayer.indexGetOwningUniquenessConstraintId( index );
return storeLayer.indexGetOwningUniquenessConstraintId( IndexBoundary.map( index ) );
}

@Override
public long indexGetCommittedId( KernelStatement state, IndexDescriptor index, Predicate<IndexRule> filter )
throws SchemaRuleNotFoundException
{
return storeLayer.indexGetCommittedId( index, filter );
return storeLayer.indexGetCommittedId( IndexBoundary.map( index ), filter );
}

@Override
Expand Down
Expand Up @@ -25,19 +25,16 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.neo4j.helpers.collection.Iterators;
import org.neo4j.kernel.api.constraints.NodePropertyConstraint;
import org.neo4j.kernel.api.constraints.PropertyConstraint;
import org.neo4j.kernel.api.constraints.RelationshipPropertyConstraint;
import org.neo4j.kernel.api.schema.IndexDescriptor;
import org.neo4j.kernel.api.schema.IndexDescriptorFactory;
import org.neo4j.kernel.api.schema.NodePropertyDescriptor;
import org.neo4j.kernel.api.schema.RelationshipPropertyDescriptor;
import org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor;
import org.neo4j.kernel.api.schema_new.RelationTypeSchemaDescriptor;
import org.neo4j.kernel.api.schema_new.SchemaDescriptor;
import org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor;
import org.neo4j.kernel.api.schema_new.index.IndexBoundary;
import org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor;
import org.neo4j.kernel.impl.constraints.ConstraintSemantics;
import org.neo4j.kernel.impl.store.record.ConstraintRule;
import org.neo4j.kernel.impl.store.record.IndexRule;
Expand All @@ -59,7 +56,7 @@ public class SchemaCache

private final Collection<NodePropertyConstraint> nodeConstraints = new HashSet<>();
private final Collection<RelationshipPropertyConstraint> relationshipConstraints = new HashSet<>();
private final Set<IndexDescriptor> indexDescriptors = new HashSet<>();
private final Map<SchemaDescriptor, NewIndexDescriptor> indexDescriptors = new HashMap<>();
private final ConstraintSemantics constraintSemantics;

public SchemaCache( ConstraintSemantics constraintSemantics, Iterable<SchemaRule> initialRules )
Expand Down Expand Up @@ -126,7 +123,7 @@ public Iterator<NodePropertyConstraint> constraintsForLabel( final int label )
nodeConstraints.iterator() );
}

public Iterator<NodePropertyConstraint> constraintsForLabelAndProperty( NodePropertyDescriptor descriptor )
public Iterator<NodePropertyConstraint> constraintsForLabelAndProperty( LabelSchemaDescriptor descriptor )
{
return Iterators.filter(
constraint -> constraint.matches( descriptor ),
Expand All @@ -141,7 +138,7 @@ public Iterator<RelationshipPropertyConstraint> constraintsForRelationshipType(
}

public Iterator<RelationshipPropertyConstraint> constraintsForRelationshipTypeAndProperty(
RelationshipPropertyDescriptor descriptor )
RelationTypeSchemaDescriptor descriptor )
{
return Iterators.filter(
constraint -> constraint.matches( descriptor ),
Expand All @@ -168,7 +165,7 @@ else if ( rule instanceof IndexRule )
{
IndexRule indexRule = (IndexRule) rule;
indexRuleById.put( indexRule.getId(), indexRule );
indexDescriptors.add( IndexBoundary.map( indexRule.getIndexDescriptor() ) );
indexDescriptors.put( indexRule.getSchemaDescriptor(), indexRule.getIndexDescriptor() );
}
}

Expand Down Expand Up @@ -208,13 +205,12 @@ else if ( constraint instanceof RelationshipPropertyConstraint )
else if ( indexRuleById.containsKey( id ) )
{
IndexRule rule = indexRuleById.remove( id );
indexDescriptors.remove( IndexBoundary.map( rule.getIndexDescriptor() ) );
indexDescriptors.remove( rule.getSchemaDescriptor() );
}
}

public IndexDescriptor indexDescriptor( NodePropertyDescriptor descriptor )
public NewIndexDescriptor indexDescriptor( LabelSchemaDescriptor descriptor )
{
IndexDescriptor key = IndexDescriptorFactory.of( descriptor );
return indexDescriptors.contains( key ) ? key : null;
return indexDescriptors.get( descriptor );
}
}

0 comments on commit d9b57fc

Please sign in to comment.