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
{
if ( InternalIndexState.ONLINE ==
provider( providers, indexRule ).getInitialState( indexRule.getId(), indexRule.getIndexDescriptor() ) )
provider( providers, indexRule ).getInitialState( indexRule.getId(), indexRule ) )
{
onlineIndexRules.add( indexRule );
}
Expand All @@ -92,13 +92,13 @@ public IndexAccessors( IndexProviderMap providers,
{
long indexId = indexRule.getId();
accessors.put( indexId, provider( providers, indexRule )
.getOnlineAccessor( indexId, indexRule.getIndexDescriptor(), samplingConfig ) );
.getOnlineAccessor( indexId, indexRule, samplingConfig ) );
}
}

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

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

Expand Down Expand Up @@ -563,9 +563,9 @@ public void shouldReportNodesThatAreNotIndexed() throws Exception
while ( indexRuleIterator.hasNext() )
{
IndexRule indexRule = indexRuleIterator.next();
SchemaIndexDescriptor descriptor = indexRule.getIndexDescriptor();
SchemaIndexDescriptor descriptor = indexRule;
IndexAccessor accessor = fixture.directStoreAccess().indexes().
apply( indexRule.getProviderDescriptor() ).getOnlineAccessor(
apply( indexRule.providerDescriptor() ).getOnlineAccessor(
indexRule.getId(), descriptor, samplingConfig );
try ( IndexUpdater updater = accessor.newUpdater( IndexUpdateMode.ONLINE ) )
{
Expand Down Expand Up @@ -600,10 +600,10 @@ public void shouldReportNodesWithDuplicatePropertyValueInUniqueIndex() throws Ex
while ( indexRuleIterator.hasNext() )
{
IndexRule indexRule = indexRuleIterator.next();
IndexAccessor accessor = fixture.directStoreAccess().indexes().apply( indexRule.getProviderDescriptor() )
.getOnlineAccessor( indexRule.getId(), indexRule.getIndexDescriptor(), samplingConfig );
IndexAccessor accessor = fixture.directStoreAccess().indexes().apply( indexRule.providerDescriptor() )
.getOnlineAccessor( indexRule.getId(), indexRule, samplingConfig );
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();
accessor.force( IOLimiter.unlimited() );
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.SchemaDescriptorSupplier;
import org.neo4j.internal.kernel.api.schema.SchemaUtil;
import org.neo4j.kernel.api.index.IndexProvider;

import static java.lang.String.format;
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 SchemaIndexDescriptor.Type type;
private final Optional<String> name;
private final String providerKey;
private final String providerVersion;
private final IndexProvider.Descriptor providerDescriptor;

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

// METHODS
Expand Down Expand Up @@ -134,6 +132,16 @@ public int[] properties()
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.
* @return a user friendly description of what this index indexes.
Expand Down
Expand Up @@ -23,6 +23,7 @@
import java.util.Optional;

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

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 )
{
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 )
{
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;

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

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

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

@Override
Expand Down Expand Up @@ -687,7 +687,7 @@ private IndexRule indexRule( SchemaIndexDescriptor index )
{
for ( IndexRule rule : schemaCache.indexRules() )
{
if ( rule.getIndexDescriptor().equals( index ) )
if ( rule.equals( index ) )
{
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 );
}

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

@Override
public void acquire()
{
Expand Down
Expand Up @@ -56,7 +56,7 @@ public SchemaStorage( RecordStore<DynamicRecord> schemaStore )
*/
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;

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

import java.util.Optional;

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.schema.index.SchemaIndexDescriptor;
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.
*/
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 String name;
private final Long owningConstraintId;

public static IndexRule indexRule( long id, SchemaIndexDescriptor descriptor,
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,
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,
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.name = SchemaRule.nameOrDefault( name, "index_" + id );
if ( providerDescriptor == null )

if ( descriptor.providerDescriptor() == null )
{
throw new IllegalArgumentException( "null provider descriptor prohibited" );
}

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

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

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." );
}
return owningConstraint;
return owningConstraintId;
}

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

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

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 );
ownerString = ", owner=" + owningConstraintId;
}
return false;
}

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

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

0 comments on commit 9c0cc41

Please sign in to comment.