Skip to content

Commit

Permalink
Made IndexRule extend SchemaIndexDescriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
fickludd authored and ragadeeshu committed May 21, 2018
1 parent 1e2dc8f commit 9c0cc41
Show file tree
Hide file tree
Showing 19 changed files with 111 additions and 141 deletions.
Expand Up @@ -67,7 +67,7 @@ public IndexAccessors( IndexProviderMap providers,
else else
{ {
if ( InternalIndexState.ONLINE == if ( InternalIndexState.ONLINE ==
provider( providers, indexRule ).getInitialState( indexRule.getId(), indexRule.getIndexDescriptor() ) ) provider( providers, indexRule ).getInitialState( indexRule.getId(), indexRule ) )
{ {
onlineIndexRules.add( indexRule ); onlineIndexRules.add( indexRule );
} }
Expand All @@ -92,13 +92,13 @@ public IndexAccessors( IndexProviderMap providers,
{ {
long indexId = indexRule.getId(); long indexId = indexRule.getId();
accessors.put( indexId, provider( providers, indexRule ) accessors.put( indexId, provider( providers, indexRule )
.getOnlineAccessor( indexId, indexRule.getIndexDescriptor(), samplingConfig ) ); .getOnlineAccessor( indexId, indexRule, samplingConfig ) );
} }
} }


private IndexProvider provider( IndexProviderMap providers, IndexRule indexRule ) private IndexProvider provider( IndexProviderMap providers, IndexRule indexRule )
{ {
return providers.apply( indexRule.getProviderDescriptor() ); return providers.apply( indexRule.providerDescriptor() );
} }


public Collection<IndexRule> notOnlineRules() public Collection<IndexRule> notOnlineRules()
Expand Down
Expand Up @@ -454,8 +454,8 @@ public void shouldNotReportIndexInconsistenciesIfIndexIsFailed() throws Exceptio
{ {
IndexRule rule = rules.next(); IndexRule rule = rules.next();
IndexSamplingConfig samplingConfig = new IndexSamplingConfig( Config.defaults() ); IndexSamplingConfig samplingConfig = new IndexSamplingConfig( Config.defaults() );
IndexPopulator populator = storeAccess.indexes().apply( rule.getProviderDescriptor() ) IndexPopulator populator = storeAccess.indexes().apply( rule.providerDescriptor() )
.getPopulator( rule.getId(), rule.getIndexDescriptor(), samplingConfig ); .getPopulator( rule.getId(), rule, samplingConfig );
populator.markAsFailed( "Oh noes! I was a shiny index and then I was failed" ); populator.markAsFailed( "Oh noes! I was a shiny index and then I was failed" );
populator.close( false ); populator.close( false );


Expand Down Expand Up @@ -563,9 +563,9 @@ public void shouldReportNodesThatAreNotIndexed() throws Exception
while ( indexRuleIterator.hasNext() ) while ( indexRuleIterator.hasNext() )
{ {
IndexRule indexRule = indexRuleIterator.next(); IndexRule indexRule = indexRuleIterator.next();
SchemaIndexDescriptor descriptor = indexRule.getIndexDescriptor(); SchemaIndexDescriptor descriptor = indexRule;
IndexAccessor accessor = fixture.directStoreAccess().indexes(). IndexAccessor accessor = fixture.directStoreAccess().indexes().
apply( indexRule.getProviderDescriptor() ).getOnlineAccessor( apply( indexRule.providerDescriptor() ).getOnlineAccessor(
indexRule.getId(), descriptor, samplingConfig ); indexRule.getId(), descriptor, samplingConfig );
try ( IndexUpdater updater = accessor.newUpdater( IndexUpdateMode.ONLINE ) ) try ( IndexUpdater updater = accessor.newUpdater( IndexUpdateMode.ONLINE ) )
{ {
Expand Down Expand Up @@ -600,10 +600,10 @@ public void shouldReportNodesWithDuplicatePropertyValueInUniqueIndex() throws Ex
while ( indexRuleIterator.hasNext() ) while ( indexRuleIterator.hasNext() )
{ {
IndexRule indexRule = indexRuleIterator.next(); IndexRule indexRule = indexRuleIterator.next();
IndexAccessor accessor = fixture.directStoreAccess().indexes().apply( indexRule.getProviderDescriptor() ) IndexAccessor accessor = fixture.directStoreAccess().indexes().apply( indexRule.providerDescriptor() )
.getOnlineAccessor( indexRule.getId(), indexRule.getIndexDescriptor(), samplingConfig ); .getOnlineAccessor( indexRule.getId(), indexRule, samplingConfig );
IndexUpdater updater = accessor.newUpdater( IndexUpdateMode.ONLINE ); IndexUpdater updater = accessor.newUpdater( IndexUpdateMode.ONLINE );
updater.process( IndexEntryUpdate.add( 42, indexRule.getIndexDescriptor().schema(), values( indexRule ) ) ); updater.process( IndexEntryUpdate.add( 42, indexRule.schema(), values( indexRule ) ) );
updater.close(); updater.close();
accessor.force( IOLimiter.unlimited() ); accessor.force( IOLimiter.unlimited() );
accessor.close(); accessor.close();
Expand Down
Expand Up @@ -30,6 +30,7 @@
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 static java.lang.String.format; import static java.lang.String.format;
import static org.neo4j.kernel.api.schema.index.SchemaIndexDescriptor.Filter.GENERAL; import static org.neo4j.kernel.api.schema.index.SchemaIndexDescriptor.Filter.GENERAL;
Expand Down Expand Up @@ -83,20 +84,17 @@ public interface Supplier
private final SchemaDescriptor schema; private final SchemaDescriptor schema;
private final SchemaIndexDescriptor.Type type; private final SchemaIndexDescriptor.Type type;
private final Optional<String> name; private final Optional<String> name;
private final String providerKey; private final IndexProvider.Descriptor providerDescriptor;
private final String providerVersion;


public SchemaIndexDescriptor( SchemaDescriptor schema, public SchemaIndexDescriptor( SchemaDescriptor schema,
Type type, Type type,
Optional<String> name, Optional<String> name,
String providerKey, IndexProvider.Descriptor providerDescriptor )
String providerVersion )
{ {
this.schema = schema; this.schema = schema;
this.type = type; this.type = type;
this.name = name; this.name = name;
this.providerKey = providerKey; this.providerDescriptor = providerDescriptor;
this.providerVersion = providerVersion;
} }


// METHODS // METHODS
Expand Down Expand Up @@ -134,6 +132,16 @@ public int[] properties()
return schema.getPropertyIds(); return schema.getPropertyIds();
} }


public Optional<String> name()
{
return name;
}

public IndexProvider.Descriptor providerDescriptor()
{
return providerDescriptor;
}

/** /**
* @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 @@ -23,6 +23,7 @@
import java.util.Optional; import java.util.Optional;


import org.neo4j.internal.kernel.api.schema.SchemaDescriptor; import org.neo4j.internal.kernel.api.schema.SchemaDescriptor;
import org.neo4j.kernel.api.index.IndexProvider;
import org.neo4j.kernel.api.schema.SchemaDescriptorFactory; import org.neo4j.kernel.api.schema.SchemaDescriptorFactory;


import static org.neo4j.kernel.api.schema.index.SchemaIndexDescriptor.Type.GENERAL; import static org.neo4j.kernel.api.schema.index.SchemaIndexDescriptor.Type.GENERAL;
Expand All @@ -46,16 +47,16 @@ public static SchemaIndexDescriptor uniqueForLabel( int labelId, int... property


public static SchemaIndexDescriptor forSchema( SchemaDescriptor schema ) public static SchemaIndexDescriptor forSchema( SchemaDescriptor schema )
{ {
return new SchemaIndexDescriptor( schema, GENERAL, null, null, null ); return new SchemaIndexDescriptor( schema, GENERAL, null, null );
} }


public static SchemaIndexDescriptor forSchema( SchemaDescriptor schema, Optional<String> name, String providerKey, String providerVersion ) public static SchemaIndexDescriptor forSchema( SchemaDescriptor schema, Optional<String> name, IndexProvider.Descriptor providerDescriptor )
{ {
return new SchemaIndexDescriptor( schema, GENERAL, name, providerKey, providerVersion ); return new SchemaIndexDescriptor( schema, GENERAL, name, providerDescriptor );
} }


public static SchemaIndexDescriptor uniqueForSchema( SchemaDescriptor schema ) public static SchemaIndexDescriptor uniqueForSchema( SchemaDescriptor schema )
{ {
return new SchemaIndexDescriptor( schema, UNIQUE, null, null, null ); return new SchemaIndexDescriptor( schema, UNIQUE, null, null );
} }
} }
Expand Up @@ -203,8 +203,8 @@ public void init()
IndexProxy indexProxy; IndexProxy indexProxy;


long indexId = indexRule.getId(); long indexId = indexRule.getId();
SchemaIndexDescriptor descriptor = indexRule.getIndexDescriptor(); SchemaIndexDescriptor descriptor = indexRule;
IndexProvider.Descriptor providerDescriptor = indexRule.getProviderDescriptor(); IndexProvider.Descriptor providerDescriptor = indexRule.providerDescriptor();
IndexProvider provider = providerMap.apply( providerDescriptor ); IndexProvider provider = providerMap.apply( providerDescriptor );
InternalIndexState initialState = provider.getInitialState( indexId, descriptor ); InternalIndexState initialState = provider.getInitialState( indexId, descriptor );
indexStates.computeIfAbsent( initialState, internalIndexState -> new ArrayList<>() ) indexStates.computeIfAbsent( initialState, internalIndexState -> new ArrayList<>() )
Expand Down Expand Up @@ -766,8 +766,8 @@ public IndexMap apply( IndexMap indexMap )
indexMap.putIndexProxy( ruleId, index ); indexMap.putIndexProxy( ruleId, index );
continue; continue;
} }
final SchemaIndexDescriptor descriptor = rule.getIndexDescriptor(); final SchemaIndexDescriptor descriptor = rule;
Descriptor providerDescriptor = rule.getProviderDescriptor(); Descriptor providerDescriptor = rule.providerDescriptor();
boolean flipToTentative = rule.canSupportUniqueConstraint(); boolean flipToTentative = rule.canSupportUniqueConstraint();
if ( state == State.RUNNING ) if ( state == State.RUNNING )
{ {
Expand Down
Expand Up @@ -284,7 +284,7 @@ else if ( rule instanceof IndexRule )
IndexRule indexRule = (IndexRule) rule; IndexRule indexRule = (IndexRule) rule;
indexRuleById.put( indexRule.getId(), indexRule ); indexRuleById.put( indexRule.getId(), indexRule );
SchemaDescriptor schemaDescriptor = indexRule.schema(); SchemaDescriptor schemaDescriptor = indexRule.schema();
SchemaIndexDescriptor schemaIndexDescriptor = indexRule.getIndexDescriptor(); SchemaIndexDescriptor schemaIndexDescriptor = indexRule;
indexDescriptors.put( schemaDescriptor, schemaIndexDescriptor ); indexDescriptors.put( schemaDescriptor, schemaIndexDescriptor );


Set<SchemaIndexDescriptor> forLabel = Set<SchemaIndexDescriptor> forLabel =
Expand Down Expand Up @@ -314,7 +314,7 @@ else if ( indexRuleById.containsKey( id ) )
indexDescriptors.remove( schema ); indexDescriptors.remove( schema );


Set<SchemaIndexDescriptor> forLabel = indexDescriptorsByLabel.get( schema.keyId() ); Set<SchemaIndexDescriptor> forLabel = indexDescriptorsByLabel.get( schema.keyId() );
forLabel.remove( rule.getIndexDescriptor() ); forLabel.remove( rule );
if ( forLabel.isEmpty() ) if ( forLabel.isEmpty() )
{ {
indexDescriptorsByLabel.remove( schema.keyId() ); indexDescriptorsByLabel.remove( schema.keyId() );
Expand All @@ -323,7 +323,7 @@ else if ( indexRuleById.containsKey( id ) )
for ( int propertyId : rule.schema().getPropertyIds() ) for ( int propertyId : rule.schema().getPropertyIds() )
{ {
List<SchemaIndexDescriptor> forProperty = indexByProperty.get( propertyId ); List<SchemaIndexDescriptor> forProperty = indexByProperty.get( propertyId );
forProperty.remove( rule.getIndexDescriptor() ); forProperty.remove( rule );
if ( forProperty.isEmpty() ) if ( forProperty.isEmpty() )
{ {
indexByProperty.remove( propertyId ); indexByProperty.remove( propertyId );
Expand Down
Expand Up @@ -895,8 +895,7 @@ public IndexReference indexCreate( SchemaDescriptor descriptor,
SchemaIndexDescriptor index = SchemaIndexDescriptor index =
SchemaIndexDescriptorFactory.forSchema( descriptor, SchemaIndexDescriptorFactory.forSchema( descriptor,
name, name,
providerDescriptor.getKey(), providerDescriptor );
providerDescriptor.getVersion() );
ktx.txState().indexRuleDoAdd( index ); ktx.txState().indexRuleDoAdd( index );
return index; return index;
} }
Expand Down
Expand Up @@ -286,7 +286,7 @@ public Iterator<SchemaIndexDescriptor> indexesGetForLabel( int labelId )
@Override @Override
public Iterator<SchemaIndexDescriptor> indexesGetAll() public Iterator<SchemaIndexDescriptor> indexesGetAll()
{ {
return toIndexDescriptors( schemaCache.indexRules() ); return (Iterator)schemaCache.indexRules().iterator();
} }


@Override @Override
Expand Down Expand Up @@ -687,7 +687,7 @@ private IndexRule indexRule( SchemaIndexDescriptor index )
{ {
for ( IndexRule rule : schemaCache.indexRules() ) for ( IndexRule rule : schemaCache.indexRules() )
{ {
if ( rule.getIndexDescriptor().equals( index ) ) if ( rule.equals( index ) )
{ {
return rule; return rule;
} }
Expand Down Expand Up @@ -809,11 +809,6 @@ private Direction directionOf( long nodeId, long relationshipId, long startNode,
" with startNode:" + startNode + " and endNode:" + endNode ); " with startNode:" + startNode + " and endNode:" + endNode );
} }


private static Iterator<SchemaIndexDescriptor> toIndexDescriptors( Iterable<IndexRule> rules )
{
return Iterators.map( IndexRule::getIndexDescriptor, rules.iterator() );
}

@Override @Override
public void acquire() public void acquire()
{ {
Expand Down
Expand Up @@ -56,7 +56,7 @@ public SchemaStorage( RecordStore<DynamicRecord> schemaStore )
*/ */
public IndexRule indexGetForSchema( final SchemaIndexDescriptor descriptor ) public IndexRule indexGetForSchema( final SchemaIndexDescriptor descriptor )
{ {
Iterator<IndexRule> rules = loadAllSchemaRules( descriptor::isSame, IndexRule.class, false ); Iterator<IndexRule> rules = loadAllSchemaRules( descriptor::equals, IndexRule.class, false );


IndexRule foundRule = null; IndexRule foundRule = null;


Expand Down
Expand Up @@ -19,8 +19,9 @@
*/ */
package org.neo4j.kernel.impl.store.record; package org.neo4j.kernel.impl.store.record;


import java.util.Optional;

import org.neo4j.graphdb.Label; import org.neo4j.graphdb.Label;
import org.neo4j.internal.kernel.api.schema.SchemaDescriptor;
import org.neo4j.kernel.api.index.IndexProvider; import org.neo4j.kernel.api.index.IndexProvider;
import org.neo4j.kernel.api.schema.index.SchemaIndexDescriptor; import org.neo4j.kernel.api.schema.index.SchemaIndexDescriptor;
import org.neo4j.storageengine.api.schema.SchemaRule; import org.neo4j.storageengine.api.schema.SchemaRule;
Expand All @@ -30,13 +31,10 @@
/** /**
* 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 implements SchemaRule, SchemaIndexDescriptor.Supplier public class IndexRule extends SchemaIndexDescriptor implements SchemaRule
{ {
private final IndexProvider.Descriptor providerDescriptor;
private final SchemaIndexDescriptor descriptor;
private final Long owningConstraint;
private final long id; private final long id;
private final String name; private final Long owningConstraintId;


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 @@ -65,34 +63,32 @@ public static IndexRule constraintIndexRule( long id, SchemaIndexDescriptor desc
} }


IndexRule( long id, IndexProvider.Descriptor providerDescriptor, IndexRule( long id, IndexProvider.Descriptor providerDescriptor,
SchemaIndexDescriptor descriptor, Long owningConstraint ) SchemaIndexDescriptor descriptor, Long owningConstraintId )
{ {
this( id, providerDescriptor, descriptor, owningConstraint, null ); this( id, providerDescriptor, descriptor, owningConstraintId, null );
} }


IndexRule( long id, IndexProvider.Descriptor providerDescriptor, IndexRule( long id, IndexProvider.Descriptor providerDescriptor,
SchemaIndexDescriptor descriptor, Long owningConstraint, String name ) SchemaIndexDescriptor descriptor, Long owningConstraintId, String name )
{ {
super( descriptor.schema(),
descriptor.type(),
Optional.of( descriptor.name().orElse( "index_" + id ) ),
descriptor.providerDescriptor() );

this.id = id; this.id = id;
this.name = SchemaRule.nameOrDefault( name, "index_" + id );
if ( providerDescriptor == null ) if ( descriptor.providerDescriptor() == null )
{ {
throw new IllegalArgumentException( "null provider descriptor prohibited" ); throw new IllegalArgumentException( "null provider descriptor prohibited" );
} }


this.descriptor = descriptor; this.owningConstraintId = owningConstraintId;
this.owningConstraint = owningConstraint;
this.providerDescriptor = providerDescriptor;
}

public IndexProvider.Descriptor getProviderDescriptor()
{
return providerDescriptor;
} }


public boolean canSupportUniqueConstraint() public boolean canSupportUniqueConstraint()
{ {
return descriptor.type() == SchemaIndexDescriptor.Type.UNIQUE; return type() == SchemaIndexDescriptor.Type.UNIQUE;
} }


/** /**
Expand All @@ -114,7 +110,7 @@ public Long getOwningConstraint()
{ {
throw new IllegalStateException( "Can only get owner from constraint indexes." ); throw new IllegalStateException( "Can only get owner from constraint indexes." );
} }
return owningConstraint; return owningConstraintId;
} }


public boolean isIndexWithoutOwningConstraint() public boolean isIndexWithoutOwningConstraint()
Expand All @@ -128,7 +124,7 @@ public IndexRule withOwningConstraint( long constraintId )
{ {
throw new IllegalStateException( this + " is not a constraint index" ); throw new IllegalStateException( this + " is not a constraint index" );
} }
return constraintIndexRule( id, descriptor, providerDescriptor, constraintId ); return constraintIndexRule( id, this, this.providerDescriptor(), constraintId );
} }


@Override @Override
Expand All @@ -137,40 +133,11 @@ public String toString()
String ownerString = ""; String ownerString = "";
if ( canSupportUniqueConstraint() ) if ( canSupportUniqueConstraint() )
{ {
ownerString = ", owner=" + owningConstraint; ownerString = ", owner=" + owningConstraintId;
}

return "IndexRule[id=" + id + ", descriptor=" + descriptor.userDescription( idTokenNameLookup ) +
", provider=" + providerDescriptor + ownerString + "]";
}

@Override
public SchemaDescriptor schema()
{
return descriptor.schema();
}

@Override
public SchemaIndexDescriptor getIndexDescriptor()
{
return descriptor;
}

@Override
public boolean equals( Object o )
{
if ( o instanceof IndexRule )
{
IndexRule that = (IndexRule) o;
return this.descriptor.equals( that.descriptor );
} }
return false;
}


@Override return "IndexRule[id=" + id + ", descriptor=" + this.userDescription( idTokenNameLookup ) +
public int hashCode() ", provider=" + this.providerDescriptor() + ownerString + "]";
{
return this.descriptor.hashCode();
} }


@Override @Override
Expand All @@ -182,6 +149,6 @@ public long getId()
@Override @Override
public String getName() public String getName()
{ {
return name; return name().get();
} }
} }

0 comments on commit 9c0cc41

Please sign in to comment.