Skip to content

Commit

Permalink
Pushed IndexDescriptor boundary in ReadOperations and SchemaReadOpera…
Browse files Browse the repository at this point in the history
…tions.
  • Loading branch information
fickludd authored and chrisvest committed Feb 14, 2017
1 parent 37bdbc8 commit 3a3c418
Show file tree
Hide file tree
Showing 51 changed files with 513 additions and 461 deletions.
Expand Up @@ -22,9 +22,9 @@ package org.neo4j.cypher.internal.spi.v2_3
import org.neo4j.cypher.internal.compiler.v2_3.planner.logical.{Cardinality, Selectivity}
import org.neo4j.cypher.internal.compiler.v2_3.spi.{GraphStatistics, StatisticsCompletingGraphStatistics}
import org.neo4j.cypher.internal.frontend.v2_3.{LabelId, NameId, PropertyKeyId, RelTypeId}
import org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException
import org.neo4j.kernel.api.ReadOperations
import org.neo4j.kernel.api.schema.{IndexDescriptorFactory, NodePropertyDescriptor}
import org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException
import org.neo4j.kernel.api.schema_new.index.NewIndexDescriptorFactory

object TransactionBoundGraphStatistics {
def apply(ops: ReadOperations) = new StatisticsCompletingGraphStatistics(new BaseTransactionBoundGraphStatistics(ops))
Expand All @@ -35,11 +35,11 @@ object TransactionBoundGraphStatistics {

def indexSelectivity(label: LabelId, property: PropertyKeyId): Option[Selectivity] =
try {
val indexDescriptor = IndexDescriptorFactory.of( label, property )
val indexDescriptor = NewIndexDescriptorFactory.forLabel( label, property )
val labeledNodes = operations.countsForNodeWithoutTxState( label ).toDouble

// Probability of any node with the given label, to have a property with a given value
val indexEntrySelectivity = operations.indexUniqueValuesSelectivity( indexDescriptor )
val indexEntrySelectivity = operations.indexUniqueValuesSelectivity(indexDescriptor)
val frequencyOfNodesWithSameValue = 1.0 / indexEntrySelectivity
val indexSelectivity = frequencyOfNodesWithSameValue / labeledNodes

Expand All @@ -51,11 +51,11 @@ object TransactionBoundGraphStatistics {

def indexPropertyExistsSelectivity(label: LabelId, property: PropertyKeyId): Option[Selectivity] =
try {
val indexDescriptor = IndexDescriptorFactory.of( label, property )
val indexDescriptor = NewIndexDescriptorFactory.forLabel( label, property )
val labeledNodes = operations.countsForNodeWithoutTxState( label ).toDouble

// Probability of any node with the given label, to have a given property
val indexSize = operations.indexSize( indexDescriptor )
val indexSize = operations.indexSize(indexDescriptor)
val indexSelectivity = indexSize / labeledNodes

Selectivity.of(indexSelectivity)
Expand Down
Expand Up @@ -39,7 +39,7 @@ object TransactionBoundGraphStatistics {
val labeledNodes = operations.countsForNodeWithoutTxState( label ).toDouble

// Probability of any node with the given label, to have a property with a given value
val indexEntrySelectivity = operations.indexUniqueValuesSelectivity( indexDescriptor )
val indexEntrySelectivity = operations.indexUniqueValuesSelectivity(indexDescriptor)
val frequencyOfNodesWithSameValue = 1.0 / indexEntrySelectivity
val indexSelectivity = frequencyOfNodesWithSameValue / labeledNodes

Expand All @@ -55,7 +55,7 @@ object TransactionBoundGraphStatistics {
val labeledNodes = operations.countsForNodeWithoutTxState( label ).toDouble

// Probability of any node with the given label, to have a given property
val indexSize = operations.indexSize( indexDescriptor )
val indexSize = operations.indexSize(indexDescriptor)
val indexSelectivity = indexSize / labeledNodes

Selectivity.of(indexSelectivity)
Expand Down
Expand Up @@ -39,7 +39,7 @@ object TransactionBoundGraphStatistics {
val labeledNodes = operations.countsForNodeWithoutTxState( label ).toDouble

// Probability of any node with the given label, to have a property with a given value
val indexEntrySelectivity = operations.indexUniqueValuesSelectivity( indexDescriptor )
val indexEntrySelectivity = operations.indexUniqueValuesSelectivity(indexDescriptor)
val frequencyOfNodesWithSameValue = 1.0 / indexEntrySelectivity
val indexSelectivity = frequencyOfNodesWithSameValue / labeledNodes

Expand All @@ -55,7 +55,7 @@ object TransactionBoundGraphStatistics {
val labeledNodes = operations.countsForNodeWithoutTxState( label ).toDouble

// Probability of any node with the given label, to have a given property
val indexSize = operations.indexSize( indexDescriptor )
val indexSize = operations.indexSize(indexDescriptor)
val indexSelectivity = indexSize / labeledNodes

Selectivity.of(indexSelectivity)
Expand Down
Expand Up @@ -49,6 +49,7 @@
import org.neo4j.kernel.api.schema.IndexDescriptor;
import org.neo4j.kernel.api.schema.NodePropertyDescriptor;
import org.neo4j.kernel.api.schema.RelationshipPropertyDescriptor;
import org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor;
import org.neo4j.kernel.impl.api.RelationshipVisitor;
import org.neo4j.kernel.impl.api.store.RelationshipIterator;
import org.neo4j.register.Register.DoubleLongRegister;
Expand Down Expand Up @@ -266,39 +267,44 @@ <EXCEPTION extends Exception> void relationshipVisit( long relId, RelationshipVi
//===========================================

/** Returns the index rule for the given labelId and propertyKey. */
IndexDescriptor indexGetForLabelAndPropertyKey( NodePropertyDescriptor descriptor )
NewIndexDescriptor indexGetForLabelAndPropertyKey( NodePropertyDescriptor descriptor )
throws SchemaRuleNotFoundException;

/** Get all indexes for a label. */
Iterator<IndexDescriptor> indexesGetForLabel( int labelId );
Iterator<NewIndexDescriptor> indexesGetForLabel( int labelId );

/** Returns all indexes. */
Iterator<IndexDescriptor> indexesGetAll();
Iterator<NewIndexDescriptor> indexesGetAll();

/** Returns the constraint index for the given labelId and propertyKey. */
IndexDescriptor uniqueIndexGetForLabelAndPropertyKey( NodePropertyDescriptor descriptor )
NewIndexDescriptor uniqueIndexGetForLabelAndPropertyKey( NodePropertyDescriptor descriptor )
throws SchemaRuleNotFoundException, DuplicateSchemaRuleException;

/** Get all constraint indexes for a label. */
Iterator<IndexDescriptor> uniqueIndexesGetForLabel( int labelId );
Iterator<NewIndexDescriptor> uniqueIndexesGetForLabel( int labelId );

/** Returns all constraint indexes. */
Iterator<IndexDescriptor> uniqueIndexesGetAll();
Iterator<NewIndexDescriptor> uniqueIndexesGetAll();

/** Retrieve the state of an index. */
InternalIndexState indexGetState( IndexDescriptor descriptor ) throws IndexNotFoundKernelException;
/** Retrieve the state of an index.
* @param descriptor*/
InternalIndexState indexGetState( NewIndexDescriptor descriptor ) throws IndexNotFoundKernelException;

/** Retrieve the population progress of an index. */
PopulationProgress indexGetPopulationProgress( IndexDescriptor descriptor ) throws IndexNotFoundKernelException;
/** Retrieve the population progress of an index.
* @param descriptor*/
PopulationProgress indexGetPopulationProgress( NewIndexDescriptor descriptor ) throws IndexNotFoundKernelException;

/** Get the index size. */
long indexSize( IndexDescriptor descriptor ) throws IndexNotFoundKernelException;
/** Get the index size.
* @param descriptor*/
long indexSize( NewIndexDescriptor descriptor ) throws IndexNotFoundKernelException;

/** Calculate the index unique values percentage (range: {@code 0.0} exclusive to {@code 1.0} inclusive). */
double indexUniqueValuesSelectivity( IndexDescriptor descriptor ) throws IndexNotFoundKernelException;
/** Calculate the index unique values percentage (range: {@code 0.0} exclusive to {@code 1.0} inclusive).
* @param descriptor*/
double indexUniqueValuesSelectivity( NewIndexDescriptor descriptor ) throws IndexNotFoundKernelException;

/** Returns the failure description of a failed index. */
String indexGetFailure( IndexDescriptor descriptor ) throws IndexNotFoundKernelException;
/** Returns the failure description of a failed index.
* @param descriptor*/
String indexGetFailure( NewIndexDescriptor descriptor ) throws IndexNotFoundKernelException;

/**
* Get all constraints applicable to label and propertyKey. There are only {@link NodePropertyConstraint}
Expand Down Expand Up @@ -334,7 +340,7 @@ Iterator<RelationshipPropertyConstraint> constraintsGetForRelationshipTypeAndPro
/**
* Get the owning constraint for a constraint index. Returns null if the index does not have an owning constraint.
*/
Long indexGetOwningUniquenessConstraintId( IndexDescriptor index ) throws SchemaRuleNotFoundException;
Long indexGetOwningUniquenessConstraintId( NewIndexDescriptor index ) throws SchemaRuleNotFoundException;

<K, V> V schemaStateGetOrCreate( K key, Function<K, V> creator );

Expand Down
Expand Up @@ -19,26 +19,26 @@
*/
package org.neo4j.kernel.api.exceptions.schema;

import org.neo4j.kernel.api.schema.NodePropertyDescriptor;
import org.neo4j.kernel.api.TokenNameLookup;
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor;

import static java.lang.String.format;

public class IndexBelongsToConstraintException extends SchemaKernelException
{
private final NodePropertyDescriptor index;
private final LabelSchemaDescriptor descriptor;
private static final String message = "Index belongs to constraint: %s";

public IndexBelongsToConstraintException( NodePropertyDescriptor index )
public IndexBelongsToConstraintException( LabelSchemaDescriptor descriptor )
{
super( Status.Schema.ForbiddenOnConstraintIndex, format( "Index belongs to constraint: %s", index ) );
this.index = index;
super( Status.Schema.ForbiddenOnConstraintIndex, format( "Index belongs to constraint: %s", descriptor ) );
this.descriptor = descriptor;
}

@Override
public String getUserMessage( TokenNameLookup tokenNameLookup )
{
return format( message, index.userDescription( tokenNameLookup ) );
return format( message, descriptor.userDescription( tokenNameLookup ) );
}
}
Expand Up @@ -19,20 +19,22 @@
*/
package org.neo4j.kernel.api.exceptions.schema;

import org.neo4j.kernel.api.schema.NodePropertyDescriptor;
import org.neo4j.kernel.api.TokenNameLookup;
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor;
import org.neo4j.kernel.api.schema_new.SchemaUtil;

import static java.lang.String.format;

public class NoSuchIndexException extends SchemaKernelException
{
private final NodePropertyDescriptor descriptor;
private final LabelSchemaDescriptor descriptor;
private static final String message = "No such INDEX ON %s.";

public NoSuchIndexException( NodePropertyDescriptor descriptor )
public NoSuchIndexException( LabelSchemaDescriptor descriptor )
{
super( Status.Schema.IndexNotFound, format( message, descriptor ) );
super( Status.Schema.IndexNotFound, format( message,
descriptor.userDescription( SchemaUtil.idTokenNameLookup ) ) );
this.descriptor = descriptor;
}

Expand Down
Expand Up @@ -44,7 +44,7 @@ protected SchemaRuleException( Status status, String messageTemplate, SchemaRule
SchemaDescriptor descriptor )
{
super( status, format( messageTemplate, kind.userString().toLowerCase(),
descriptor.userDescription( SchemaUtil.noopTokenNameLookup ) ) );
descriptor.userDescription( SchemaUtil.idTokenNameLookup ) ) );
this.descriptor = descriptor;
this.messageTemplate = messageTemplate;
this.kind = kind;
Expand Down
Expand Up @@ -21,6 +21,8 @@

import org.neo4j.kernel.api.TokenNameLookup;

import static java.lang.String.format;

public class SchemaUtil
{
private SchemaUtil()
Expand All @@ -37,32 +39,24 @@ public static String niceProperties( TokenNameLookup tokenNameLookup, int[] prop
return String.join( ", ", properties );
}

public static TokenNameLookup noopTokenNameLookup = new PrefixTokenName( "" );
public static class PrefixTokenName implements TokenNameLookup
{
private final String prefix;

public PrefixTokenName( String prefix )
{
this.prefix = prefix;
}
public static TokenNameLookup idTokenNameLookup = new TokenNameLookup() {

@Override
public String labelGetName( int labelId )
{
return prefix + labelId;
return format( "label[%d]", labelId );
}

@Override
public String relationshipTypeGetName( int relationshipTypeId )
{
return prefix + relationshipTypeId;
return format( "relType[%d]", relationshipTypeId );
}

@Override
public String propertyKeyGetName( int propertyKeyId )
{
return prefix + propertyKeyId;
return format( "property[%d]", propertyKeyId );
}
}
};
}
Expand Up @@ -38,6 +38,7 @@
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException;
import org.neo4j.kernel.api.schema.IndexDescriptor;
import org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor;
import org.neo4j.kernel.impl.api.TokenAccess;
import org.neo4j.kernel.impl.api.index.IndexingService;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
Expand Down Expand Up @@ -101,15 +102,14 @@ public Stream<IndexResult> listIndexes() throws ProcedureException
ReadOperations operations = statement.readOperations();
TokenNameLookup tokens = new StatementTokenNameLookup( operations );

List<IndexDescriptor> indexes =
asList( operations.indexesGetAll() );
List<NewIndexDescriptor> indexes = asList( operations.indexesGetAll() );

Set<IndexDescriptor> uniqueIndexes = asSet( operations.uniqueIndexesGetAll() );
Set<NewIndexDescriptor> uniqueIndexes = asSet( operations.uniqueIndexesGetAll() );
indexes.addAll( uniqueIndexes );
indexes.sort( Comparator.comparing( a -> a.userDescription( tokens ) ) );

ArrayList<IndexResult> result = new ArrayList<>();
for ( IndexDescriptor index : indexes )
for ( NewIndexDescriptor index : indexes )
{
try
{
Expand All @@ -123,7 +123,7 @@ public Stream<IndexResult> listIndexes() throws ProcedureException
type = IndexType.NODE_LABEL_PROPERTY.typeName();
}

result.add( new IndexResult( "INDEX ON " + index.userDescription( tokens ),
result.add( new IndexResult( "INDEX ON " + index.schema().userDescription( tokens ),
operations.indexGetState( index ).toString(), type ) );
}
catch ( IndexNotFoundKernelException e )
Expand Down
Expand Up @@ -33,6 +33,8 @@
import org.neo4j.kernel.api.exceptions.schema.SchemaRuleNotFoundException;
import org.neo4j.kernel.api.schema.IndexDescriptor;
import org.neo4j.kernel.api.index.InternalIndexState;
import org.neo4j.kernel.api.schema_new.index.IndexBoundary;
import org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor;
import org.neo4j.kernel.impl.api.index.IndexingService;
import org.neo4j.kernel.impl.api.index.sampling.IndexSamplingMode;

Expand Down Expand Up @@ -106,7 +108,7 @@ private int getPropertyKeyId( String propertyKeyName ) throws ProcedureException
return propertyKeyId;
}

private IndexDescriptor getIndex( int labelId, int propertyKeyId, IndexSpecifier index ) throws
private NewIndexDescriptor getIndex( int labelId, int propertyKeyId, IndexSpecifier index ) throws
ProcedureException
{
try
Expand All @@ -119,8 +121,8 @@ private IndexDescriptor getIndex( int labelId, int propertyKeyId, IndexSpecifier
}
}

private void waitUntilOnline( IndexDescriptor index, IndexSpecifier indexDescription, long timeout, TimeUnit
timeoutUnits )
private void waitUntilOnline( NewIndexDescriptor index, IndexSpecifier indexDescription,
long timeout, TimeUnit timeoutUnits )
throws ProcedureException
{
try
Expand All @@ -134,7 +136,7 @@ private void waitUntilOnline( IndexDescriptor index, IndexSpecifier indexDescrip
}
}

private boolean isOnline( IndexSpecifier indexDescription, IndexDescriptor index ) throws ProcedureException
private boolean isOnline( IndexSpecifier indexDescription, NewIndexDescriptor index ) throws ProcedureException
{
InternalIndexState state = getState( indexDescription, index );
switch ( state )
Expand All @@ -151,7 +153,7 @@ private boolean isOnline( IndexSpecifier indexDescription, IndexDescriptor index
}
}

private InternalIndexState getState( IndexSpecifier indexDescription, IndexDescriptor index )
private InternalIndexState getState( IndexSpecifier indexDescription, NewIndexDescriptor index )
throws ProcedureException
{
try
Expand All @@ -164,9 +166,9 @@ private InternalIndexState getState( IndexSpecifier indexDescription, IndexDescr
}
}

private void triggerSampling( IndexDescriptor index ) throws IndexNotFoundKernelException
private void triggerSampling( NewIndexDescriptor index ) throws IndexNotFoundKernelException
{
indexingService.triggerIndexSampling( index, IndexSamplingMode.TRIGGER_REBUILD_ALL );
indexingService.triggerIndexSampling( IndexBoundary.map( index ), IndexSamplingMode.TRIGGER_REBUILD_ALL );
}

@Override
Expand Down
Expand Up @@ -44,6 +44,7 @@
import org.neo4j.kernel.api.StatementTokenNameLookup;
import org.neo4j.kernel.api.constraints.NodePropertyConstraint;
import org.neo4j.kernel.api.schema.IndexDescriptor;
import org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor;
import org.neo4j.kernel.impl.coreapi.schema.PropertyNameUtils;
import org.neo4j.kernel.internal.GraphDatabaseAPI;

Expand Down Expand Up @@ -78,14 +79,14 @@ public GraphResult buildSchemaGraph()
Label label = labelsInDatabase.next();
Map<String,Object> properties = new HashMap<>();

Iterator<IndexDescriptor> indexDescriptorIterator =
Iterator<NewIndexDescriptor> indexDescriptorIterator =
readOperations.indexesGetForLabel( readOperations.labelGetForName( label.name() ) );
ArrayList<String> indexes = new ArrayList<>();
while ( indexDescriptorIterator.hasNext() )
{
IndexDescriptor index = indexDescriptorIterator.next();
String[] propertyNames =
PropertyNameUtils.getPropertyKeys( statementTokenNameLookup, index.descriptor() );
NewIndexDescriptor index = indexDescriptorIterator.next();
String[] propertyNames = PropertyNameUtils.getPropertyKeys(
statementTokenNameLookup, index.schema().getPropertyIds() );
indexes.add( String.join( ",", propertyNames ) );
}
properties.put( "indexes", indexes );
Expand Down

0 comments on commit 3a3c418

Please sign in to comment.