Skip to content

Commit

Permalink
Let SchemaCache store CapableIndexDescriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
fickludd authored and ragadeeshu committed May 21, 2018
1 parent 756d86f commit f151fe8
Show file tree
Hide file tree
Showing 23 changed files with 169 additions and 123 deletions.
Expand Up @@ -19,8 +19,10 @@
*/ */
package org.neo4j.internal.kernel.api.schema; package org.neo4j.internal.kernel.api.schema;


import java.util.Arrays;
import java.util.function.Predicate; import java.util.function.Predicate;


import org.neo4j.internal.kernel.api.IndexReference;
import org.neo4j.internal.kernel.api.TokenNameLookup; import org.neo4j.internal.kernel.api.TokenNameLookup;
import org.neo4j.storageengine.api.lock.ResourceType; import org.neo4j.storageengine.api.lock.ResourceType;


Expand Down
Expand Up @@ -25,6 +25,7 @@
import org.neo4j.internal.kernel.api.IndexCapability; import org.neo4j.internal.kernel.api.IndexCapability;
import org.neo4j.kernel.api.index.IndexProvider; import org.neo4j.kernel.api.index.IndexProvider;
import org.neo4j.kernel.impl.api.index.CapableIndexDescriptor; import org.neo4j.kernel.impl.api.index.CapableIndexDescriptor;
import org.neo4j.kernel.impl.api.index.IndexProviderMap;
import org.neo4j.storageengine.api.schema.SchemaRule; import org.neo4j.storageengine.api.schema.SchemaRule;


import static org.neo4j.internal.kernel.api.schema.SchemaUtil.idTokenNameLookup; import static org.neo4j.internal.kernel.api.schema.SchemaUtil.idTokenNameLookup;
Expand Down Expand Up @@ -157,4 +158,10 @@ public CapableIndexDescriptor withoutCapabilities()
{ {
return new CapableIndexDescriptor( id, providerDescriptor, this, owningConstraintId, IndexCapability.NO_CAPABILITY ); return new CapableIndexDescriptor( id, providerDescriptor, this, owningConstraintId, IndexCapability.NO_CAPABILITY );
} }

public CapableIndexDescriptor withCapabilities( IndexProviderMap indexProviderMap )
{
IndexCapability capability = indexProviderMap.apply( providerDescriptor ).getCapability();
return new CapableIndexDescriptor( id, providerDescriptor, this, owningConstraintId, capability );
}
} }
Expand Up @@ -25,12 +25,15 @@
import java.util.function.Predicate; import java.util.function.Predicate;


import org.neo4j.helpers.collection.Iterators; import org.neo4j.helpers.collection.Iterators;
import org.neo4j.internal.kernel.api.IndexReference; import org.neo4j.internal.kernel.api.CapableIndexReference;
import org.neo4j.internal.kernel.api.IndexOrder;
import org.neo4j.internal.kernel.api.IndexValueCapability;
import org.neo4j.internal.kernel.api.TokenNameLookup; import org.neo4j.internal.kernel.api.TokenNameLookup;
import org.neo4j.internal.kernel.api.schema.SchemaDescriptor; import org.neo4j.internal.kernel.api.schema.SchemaDescriptor;
import org.neo4j.internal.kernel.api.schema.SchemaDescriptorSupplier; import org.neo4j.internal.kernel.api.schema.SchemaDescriptorSupplier;
import org.neo4j.internal.kernel.api.schema.SchemaUtil; import org.neo4j.internal.kernel.api.schema.SchemaUtil;
import org.neo4j.kernel.api.index.IndexProvider; import org.neo4j.kernel.api.index.IndexProvider;
import org.neo4j.values.storable.ValueCategory;


import static java.lang.String.format; import static java.lang.String.format;
import static org.neo4j.kernel.api.schema.index.PendingIndexDescriptor.Filter.GENERAL; import static org.neo4j.kernel.api.schema.index.PendingIndexDescriptor.Filter.GENERAL;
Expand All @@ -40,7 +43,7 @@
* Internal representation of a graph index, including the schema unit it targets (eg. label-property combination) * Internal representation of a graph index, including the schema unit it targets (eg. label-property combination)
* and the type of index. UNIQUE indexes are used to back uniqueness constraints. * and the type of index. UNIQUE indexes are used to back uniqueness constraints.
*/ */
public class PendingIndexDescriptor implements SchemaDescriptorSupplier, IndexReference public class PendingIndexDescriptor implements SchemaDescriptorSupplier, CapableIndexReference
{ {
public enum Type public enum Type
{ {
Expand Down Expand Up @@ -154,6 +157,18 @@ public IndexProvider.Descriptor providerDescriptor()
return providerDescriptor; return providerDescriptor;
} }


@Override
public IndexOrder[] orderCapability( ValueCategory... valueCategories )
{
return ORDER_NONE;
}

@Override
public IndexValueCapability valueCapability( ValueCategory... valueCategories )
{
return IndexValueCapability.NO;
}

/** /**
* @param tokenNameLookup used for looking up names for token ids. * @param tokenNameLookup used for looking up names for token ids.
* @return a user friendly description of what this index indexes. * @return a user friendly description of what this index indexes.
Expand Down
Expand Up @@ -19,7 +19,6 @@
*/ */
package org.neo4j.kernel.impl.api.index; package org.neo4j.kernel.impl.api.index;


import org.neo4j.internal.kernel.api.CapableIndexReference;
import org.neo4j.internal.kernel.api.IndexCapability; import org.neo4j.internal.kernel.api.IndexCapability;
import org.neo4j.internal.kernel.api.IndexOrder; import org.neo4j.internal.kernel.api.IndexOrder;
import org.neo4j.internal.kernel.api.IndexValueCapability; import org.neo4j.internal.kernel.api.IndexValueCapability;
Expand All @@ -28,7 +27,7 @@
import org.neo4j.kernel.api.schema.index.PendingIndexDescriptor; import org.neo4j.kernel.api.schema.index.PendingIndexDescriptor;
import org.neo4j.values.storable.ValueCategory; import org.neo4j.values.storable.ValueCategory;


public class CapableIndexDescriptor extends IndexDescriptor implements CapableIndexReference public class CapableIndexDescriptor extends IndexDescriptor
{ {
private final IndexCapability indexCapability; private final IndexCapability indexCapability;


Expand Down
Expand Up @@ -32,4 +32,25 @@ public interface IndexProviderMap extends Function<IndexProvider.Descriptor,Inde
IndexProvider getDefaultProvider(); IndexProvider getDefaultProvider();


void accept( Consumer<IndexProvider> visitor ); void accept( Consumer<IndexProvider> visitor );

IndexProviderMap EMPTY = new IndexProviderMap()
{
@Override
public IndexProvider apply( IndexProvider.Descriptor descriptor ) throws IndexProviderNotFoundException
{
return IndexProvider.EMPTY;
}

@Override
public IndexProvider getDefaultProvider()
{
return IndexProvider.EMPTY;
}

@Override
public void accept( Consumer<IndexProvider> visitor )
{
// yey!
}
};
} }
Expand Up @@ -79,13 +79,6 @@ public static IndexReference general( int label, int...properties )
return new DefaultIndexReference( false, label, properties ); return new DefaultIndexReference( false, label, properties );
} }


public static IndexReference fromDescriptor( PendingIndexDescriptor descriptor )
{
boolean unique = descriptor.type() == PendingIndexDescriptor.Type.UNIQUE;
SchemaDescriptor schema = descriptor.schema();
return new DefaultIndexReference( unique, schema.keyId(), schema.getPropertyIds() );
}

public static PendingIndexDescriptor toDescriptor( IndexReference reference ) public static PendingIndexDescriptor toDescriptor( IndexReference reference )
{ {
if ( reference.isUnique() ) if ( reference.isUnique() )
Expand Down
Expand Up @@ -40,10 +40,11 @@
import org.neo4j.internal.kernel.api.schema.SchemaDescriptor; import org.neo4j.internal.kernel.api.schema.SchemaDescriptor;
import org.neo4j.internal.kernel.api.schema.SchemaDescriptorPredicates; import org.neo4j.internal.kernel.api.schema.SchemaDescriptorPredicates;
import org.neo4j.internal.kernel.api.schema.constraints.ConstraintDescriptor; import org.neo4j.internal.kernel.api.schema.constraints.ConstraintDescriptor;
import org.neo4j.kernel.api.schema.index.PendingIndexDescriptor; import org.neo4j.kernel.api.schema.index.IndexDescriptor;
import org.neo4j.kernel.impl.api.index.CapableIndexDescriptor;
import org.neo4j.kernel.impl.api.index.IndexProviderMap;
import org.neo4j.kernel.impl.constraints.ConstraintSemantics; import org.neo4j.kernel.impl.constraints.ConstraintSemantics;
import org.neo4j.kernel.impl.store.record.ConstraintRule; import org.neo4j.kernel.impl.store.record.ConstraintRule;
import org.neo4j.kernel.api.schema.index.IndexDescriptor;
import org.neo4j.storageengine.api.schema.SchemaRule; import org.neo4j.storageengine.api.schema.SchemaRule;


import static java.util.Collections.emptyIterator; import static java.util.Collections.emptyIterator;
Expand All @@ -55,14 +56,18 @@
public class SchemaCache public class SchemaCache
{ {
private final Lock cacheUpdateLock = new StampedLock().asWriteLock(); private final Lock cacheUpdateLock = new StampedLock().asWriteLock();
private final IndexProviderMap indexProviderMap;
private volatile SchemaCacheState schemaCacheState; private volatile SchemaCacheState schemaCacheState;


public SchemaCache( ConstraintSemantics constraintSemantics, Iterable<SchemaRule> initialRules ) public SchemaCache( ConstraintSemantics constraintSemantics,
Iterable<SchemaRule> initialRules,
IndexProviderMap indexProviderMap )
{ {
this.schemaCacheState = new SchemaCacheState( constraintSemantics, initialRules ); this.indexProviderMap = indexProviderMap;
this.schemaCacheState = new SchemaCacheState( constraintSemantics, initialRules, indexProviderMap );
} }


public Iterable<IndexDescriptor> indexRules() public Iterable<CapableIndexDescriptor> indexRules()
{ {
return schemaCacheState.indexRules(); return schemaCacheState.indexRules();
} }
Expand Down Expand Up @@ -118,7 +123,7 @@ public void load( Iterable<SchemaRule> rules )
try try
{ {
ConstraintSemantics constraintSemantics = schemaCacheState.constraintSemantics; ConstraintSemantics constraintSemantics = schemaCacheState.constraintSemantics;
this.schemaCacheState = new SchemaCacheState( constraintSemantics, rules ); this.schemaCacheState = new SchemaCacheState( constraintSemantics, rules, indexProviderMap );
} }
finally finally
{ {
Expand Down Expand Up @@ -156,37 +161,40 @@ public void removeSchemaRule( long id )
} }
} }


public PendingIndexDescriptor indexDescriptor( SchemaDescriptor descriptor ) public CapableIndexDescriptor indexDescriptor( SchemaDescriptor descriptor )
{ {
return schemaCacheState.indexDescriptor( descriptor ); return schemaCacheState.indexDescriptor( descriptor );
} }


public Iterator<PendingIndexDescriptor> indexDescriptorsForLabel( int labelId ) public Iterator<CapableIndexDescriptor> indexDescriptorsForLabel( int labelId )
{ {
return schemaCacheState.indexDescriptorsForLabel( labelId ); return schemaCacheState.indexDescriptorsForLabel( labelId );
} }


public Iterator<PendingIndexDescriptor> indexesByProperty( int propertyId ) public Iterator<CapableIndexDescriptor> indexesByProperty( int propertyId )
{ {
return schemaCacheState.indexesByProperty( propertyId ); return schemaCacheState.indexesByProperty( propertyId );
} }


private static class SchemaCacheState private static class SchemaCacheState
{ {
private final ConstraintSemantics constraintSemantics; private final ConstraintSemantics constraintSemantics;
private final IndexProviderMap indexProviderMap;
private final Set<ConstraintDescriptor> constraints; private final Set<ConstraintDescriptor> constraints;
private final MutableLongObjectMap<IndexDescriptor> indexRuleById; private final MutableLongObjectMap<CapableIndexDescriptor> indexRuleById;
private final MutableLongObjectMap<ConstraintRule> constraintRuleById; private final MutableLongObjectMap<ConstraintRule> constraintRuleById;


private final Map<SchemaDescriptor,PendingIndexDescriptor> indexDescriptors; private final Map<SchemaDescriptor,CapableIndexDescriptor> indexDescriptors;
private final MutableIntObjectMap<Set<PendingIndexDescriptor>> indexDescriptorsByLabel; private final MutableIntObjectMap<Set<CapableIndexDescriptor>> indexDescriptorsByLabel;


private final Map<Class<?>,Object> dependantState; private final Map<Class<?>,Object> dependantState;
private final MutableIntObjectMap<List<PendingIndexDescriptor>> indexByProperty; private final MutableIntObjectMap<List<CapableIndexDescriptor>> indexByProperty;


SchemaCacheState( ConstraintSemantics constraintSemantics, Iterable<SchemaRule> rules ) SchemaCacheState( ConstraintSemantics constraintSemantics, Iterable<SchemaRule> rules,
IndexProviderMap indexProviderMap )
{ {
this.constraintSemantics = constraintSemantics; this.constraintSemantics = constraintSemantics;
this.indexProviderMap = indexProviderMap;
this.constraints = new HashSet<>(); this.constraints = new HashSet<>();
this.indexRuleById = new LongObjectHashMap<>(); this.indexRuleById = new LongObjectHashMap<>();
this.constraintRuleById = new LongObjectHashMap<>(); this.constraintRuleById = new LongObjectHashMap<>();
Expand All @@ -209,6 +217,7 @@ private static class SchemaCacheState
this.indexDescriptorsByLabel = IntObjectHashMap.newMap( schemaCacheState.indexDescriptorsByLabel ); this.indexDescriptorsByLabel = IntObjectHashMap.newMap( schemaCacheState.indexDescriptorsByLabel );
this.dependantState = new ConcurrentHashMap<>(); this.dependantState = new ConcurrentHashMap<>();
this.indexByProperty = IntObjectHashMap.newMap( schemaCacheState.indexByProperty ); this.indexByProperty = IntObjectHashMap.newMap( schemaCacheState.indexByProperty );
this.indexProviderMap = schemaCacheState.indexProviderMap;
} }


private void load( Iterable<SchemaRule> schemaRuleIterator ) private void load( Iterable<SchemaRule> schemaRuleIterator )
Expand All @@ -219,7 +228,7 @@ private void load( Iterable<SchemaRule> schemaRuleIterator )
} }
} }


Iterable<IndexDescriptor> indexRules() Iterable<CapableIndexDescriptor> indexRules()
{ {
return indexRuleById.values(); return indexRuleById.values();
} }
Expand Down Expand Up @@ -249,20 +258,20 @@ Iterator<ConstraintDescriptor> constraints()
return constraints.iterator(); return constraints.iterator();
} }


PendingIndexDescriptor indexDescriptor( SchemaDescriptor descriptor ) CapableIndexDescriptor indexDescriptor( SchemaDescriptor descriptor )
{ {
return indexDescriptors.get( descriptor ); return indexDescriptors.get( descriptor );
} }


Iterator<PendingIndexDescriptor> indexesByProperty( int propertyId ) Iterator<CapableIndexDescriptor> indexesByProperty( int propertyId )
{ {
List<PendingIndexDescriptor> indexes = indexByProperty.get( propertyId ); List<CapableIndexDescriptor> indexes = indexByProperty.get( propertyId );
return (indexes == null) ? emptyIterator() : indexes.iterator(); return (indexes == null) ? emptyIterator() : indexes.iterator();
} }


Iterator<PendingIndexDescriptor> indexDescriptorsForLabel( int labelId ) Iterator<CapableIndexDescriptor> indexDescriptorsForLabel( int labelId )
{ {
Set<PendingIndexDescriptor> forLabel = indexDescriptorsByLabel.get( labelId ); Set<CapableIndexDescriptor> forLabel = indexDescriptorsByLabel.get( labelId );
return forLabel == null ? emptyIterator() : forLabel.iterator(); return forLabel == null ? emptyIterator() : forLabel.iterator();
} }


Expand All @@ -281,21 +290,20 @@ void addSchemaRule( SchemaRule rule )
} }
else if ( rule instanceof IndexDescriptor ) else if ( rule instanceof IndexDescriptor )
{ {
IndexDescriptor indexRule = (IndexDescriptor) rule; CapableIndexDescriptor index = ((IndexDescriptor) rule).withCapabilities( indexProviderMap );
indexRuleById.put( indexRule.getId(), indexRule ); indexRuleById.put( index.getId(), index );
SchemaDescriptor schemaDescriptor = indexRule.schema(); SchemaDescriptor schemaDescriptor = index.schema();
PendingIndexDescriptor schemaIndexDescriptor = indexRule; indexDescriptors.put( schemaDescriptor, index );
indexDescriptors.put( schemaDescriptor, schemaIndexDescriptor );


Set<PendingIndexDescriptor> forLabel = Set<CapableIndexDescriptor> forLabel =
indexDescriptorsByLabel.getIfAbsentPut( schemaDescriptor.keyId(), HashSet::new ); indexDescriptorsByLabel.getIfAbsentPut( schemaDescriptor.keyId(), HashSet::new );
forLabel.add( schemaIndexDescriptor ); forLabel.add( index );


for ( int propertyId : indexRule.schema().getPropertyIds() ) for ( int propertyId : index.schema().getPropertyIds() )
{ {
List<PendingIndexDescriptor> indexesForProperty = List<CapableIndexDescriptor> indexesForProperty =
indexByProperty.getIfAbsentPut( propertyId, ArrayList::new ); indexByProperty.getIfAbsentPut( propertyId, ArrayList::new );
indexesForProperty.add( schemaIndexDescriptor ); indexesForProperty.add( index );
} }
} }
} }
Expand All @@ -309,21 +317,21 @@ void removeSchemaRule( long id )
} }
else if ( indexRuleById.containsKey( id ) ) else if ( indexRuleById.containsKey( id ) )
{ {
IndexDescriptor rule = indexRuleById.remove( id ); CapableIndexDescriptor index = indexRuleById.remove( id );
SchemaDescriptor schema = rule.schema(); SchemaDescriptor schema = index.schema();
indexDescriptors.remove( schema ); indexDescriptors.remove( schema );


Set<PendingIndexDescriptor> forLabel = indexDescriptorsByLabel.get( schema.keyId() ); Set<CapableIndexDescriptor> forLabel = indexDescriptorsByLabel.get( schema.keyId() );
forLabel.remove( rule ); forLabel.remove( index );
if ( forLabel.isEmpty() ) if ( forLabel.isEmpty() )
{ {
indexDescriptorsByLabel.remove( schema.keyId() ); indexDescriptorsByLabel.remove( schema.keyId() );
} }


for ( int propertyId : rule.schema().getPropertyIds() ) for ( int propertyId : index.schema().getPropertyIds() )
{ {
List<PendingIndexDescriptor> forProperty = indexByProperty.get( propertyId ); List<CapableIndexDescriptor> forProperty = indexByProperty.get( propertyId );
forProperty.remove( rule ); forProperty.remove( index );
if ( forProperty.isEmpty() ) if ( forProperty.isEmpty() )
{ {
indexByProperty.remove( propertyId ); indexByProperty.remove( propertyId );
Expand Down

0 comments on commit f151fe8

Please sign in to comment.