Skip to content

Commit

Permalink
Ues SchemaDescriptor instead of LabelSchemaDescriptor where possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvest committed Mar 19, 2018
1 parent f5cc200 commit ee07b7b
Show file tree
Hide file tree
Showing 108 changed files with 543 additions and 465 deletions.
Expand Up @@ -38,7 +38,7 @@ public IndexCheck( IndexRule indexRule )
@Override @Override
public void check( IndexEntry record, CheckerEngine<IndexEntry, ConsistencyReport.IndexConsistencyReport> engine, RecordAccess records ) public void check( IndexEntry record, CheckerEngine<IndexEntry, ConsistencyReport.IndexConsistencyReport> engine, RecordAccess records )
{ {
int labelId = indexRule.schema().getLabelId(); int labelId = indexRule.schema().keyId();
engine.comparativeCheck( records.node( record.getId() ), engine.comparativeCheck( records.node( record.getId() ),
new NodeInUseWithCorrectLabelsCheck<>( new long[]{labelId}, false ) ); new NodeInUseWithCorrectLabelsCheck<>( new long[]{labelId}, false ) );
} }
Expand Down
Expand Up @@ -36,7 +36,7 @@
import org.neo4j.consistency.report.ConsistencyReport; import org.neo4j.consistency.report.ConsistencyReport;
import org.neo4j.consistency.store.RecordAccess; import org.neo4j.consistency.store.RecordAccess;
import org.neo4j.internal.kernel.api.IndexQuery; import org.neo4j.internal.kernel.api.IndexQuery;
import org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor; import org.neo4j.internal.kernel.api.schema.SchemaDescriptor;
import org.neo4j.kernel.api.exceptions.index.IndexNotApplicableKernelException; import org.neo4j.kernel.api.exceptions.index.IndexNotApplicableKernelException;
import org.neo4j.kernel.impl.api.LookupFilter; import org.neo4j.kernel.impl.api.LookupFilter;
import org.neo4j.kernel.impl.store.record.IndexRule; import org.neo4j.kernel.impl.store.record.IndexRule;
Expand Down Expand Up @@ -95,7 +95,7 @@ private void matchIndexesToNode(
PrimitiveIntObjectMap<PropertyBlock> nodePropertyMap = null; PrimitiveIntObjectMap<PropertyBlock> nodePropertyMap = null;
for ( IndexRule indexRule : indexes.onlineRules() ) for ( IndexRule indexRule : indexes.onlineRules() )
{ {
long labelId = indexRule.schema().getLabelId(); long labelId = indexRule.schema().keyId();
if ( labels.contains( labelId ) ) if ( labels.contains( labelId ) )
{ {
if ( nodePropertyMap == null ) if ( nodePropertyMap == null )
Expand Down Expand Up @@ -219,13 +219,14 @@ private PrimitiveIntObjectMap<PropertyBlock> properties( List<PropertyBlock> pro
return propertyIds; return propertyIds;
} }


private IndexQuery[] seek( LabelSchemaDescriptor schema, Value[] propertyValues ) private IndexQuery[] seek( SchemaDescriptor schema, Value[] propertyValues )
{ {
assert schema.getPropertyIds().length == propertyValues.length; int[] propertyIds = schema.getPropertyIds();
assert propertyIds.length == propertyValues.length;
IndexQuery[] query = new IndexQuery[propertyValues.length]; IndexQuery[] query = new IndexQuery[propertyValues.length];
for ( int i = 0; i < query.length; i++ ) for ( int i = 0; i < query.length; i++ )
{ {
query[i] = IndexQuery.exact( schema.getPropertyIds()[i], propertyValues[i] ); query[i] = IndexQuery.exact( propertyIds[i], propertyValues[i] );
} }
return query; return query;
} }
Expand Down
Expand Up @@ -29,7 +29,7 @@ trait SchemaDescriptorTranslation {


implicit def kernelToCypher(index: KernelIndexDescriptor): SchemaTypes.IndexDescriptor = implicit def kernelToCypher(index: KernelIndexDescriptor): SchemaTypes.IndexDescriptor =
if (index.schema().getPropertyIds.length == 1) if (index.schema().getPropertyIds.length == 1)
SchemaTypes.IndexDescriptor(index.schema().getLabelId, index.schema().getPropertyId) SchemaTypes.IndexDescriptor(index.schema().keyId, index.schema().getPropertyId)
else else
throw new UnsupportedOperationException("Cypher 2.3 does not support composite indexes") throw new UnsupportedOperationException("Cypher 2.3 does not support composite indexes")


Expand Down
Expand Up @@ -30,7 +30,7 @@ trait SchemaDescriptorTranslation {


implicit def toCypher(index: KernelIndexDescriptor): SchemaTypes.IndexDescriptor = { implicit def toCypher(index: KernelIndexDescriptor): SchemaTypes.IndexDescriptor = {
assertSingleProperty(index.schema()) assertSingleProperty(index.schema())
SchemaTypes.IndexDescriptor(index.schema().getLabelId, index.schema().getPropertyId()) SchemaTypes.IndexDescriptor(index.schema().keyId, index.schema().getPropertyId())
} }


implicit def toKernel(constraint: SchemaTypes.UniquenessConstraint): KernelUniquenessConstraint = implicit def toKernel(constraint: SchemaTypes.UniquenessConstraint): KernelUniquenessConstraint =
Expand Down
Expand Up @@ -29,7 +29,7 @@ trait IndexDescriptorCompatibility {
SchemaIndexDescriptorFactory.forLabel(index.label.id, index.properties.map(_.id):_*) SchemaIndexDescriptorFactory.forLabel(index.label.id, index.properties.map(_.id):_*)


def kernelToCypher(index: KernelIndexDescriptor): CypherIndexDescriptor = def kernelToCypher(index: KernelIndexDescriptor): CypherIndexDescriptor =
CypherIndexDescriptor(index.schema().getLabelId, index.schema().getPropertyIds) CypherIndexDescriptor(index.schema().keyId, index.schema().getPropertyIds)


def cypherToKernelSchema(index: CypherIndexDescriptor): LabelSchemaDescriptor = def cypherToKernelSchema(index: CypherIndexDescriptor): LabelSchemaDescriptor =
SchemaDescriptorFactory.forLabel(index.label.id, index.properties.map(_.id):_*) SchemaDescriptorFactory.forLabel(index.label.id, index.properties.map(_.id):_*)
Expand Down
Expand Up @@ -86,7 +86,7 @@ class TransactionBoundPlanContext(readOperationsSupplier: () => ReadOperations,


private def getOnlineIndex(descriptor: KernelIndexDescriptor): Option[IndexDescriptor] = private def getOnlineIndex(descriptor: KernelIndexDescriptor): Option[IndexDescriptor] =
readOperationsSupplier().indexGetState(descriptor) match { readOperationsSupplier().indexGetState(descriptor) match {
case InternalIndexState.ONLINE => Some(IndexDescriptor(descriptor.schema().getLabelId, descriptor.schema().getPropertyIds)) case InternalIndexState.ONLINE => Some(IndexDescriptor(descriptor.schema().keyId, descriptor.schema().getPropertyIds))
case _ => None case _ => None
} }


Expand Down
Expand Up @@ -29,7 +29,7 @@ trait IndexDescriptorCompatibility {
SchemaIndexDescriptorFactory.forLabel(index.label.id, index.properties.map(_.id):_*) SchemaIndexDescriptorFactory.forLabel(index.label.id, index.properties.map(_.id):_*)


def kernelToCypher(index: KernelIndexDescriptor): CypherIndexDescriptor = def kernelToCypher(index: KernelIndexDescriptor): CypherIndexDescriptor =
CypherIndexDescriptor(index.schema().getLabelId, index.schema().getPropertyIds) CypherIndexDescriptor(index.schema().keyId, index.schema().getPropertyIds)


def cypherToKernelSchema(index: CypherIndexDescriptor): LabelSchemaDescriptor = def cypherToKernelSchema(index: CypherIndexDescriptor): LabelSchemaDescriptor =
SchemaDescriptorFactory.forLabel(index.label.id, index.properties.map(_.id):_*) SchemaDescriptorFactory.forLabel(index.label.id, index.properties.map(_.id):_*)
Expand Down
Expand Up @@ -91,7 +91,7 @@ class TransactionBoundPlanContext(tc: TransactionalContextWrapper, logger: Inter


private def getOnlineIndex(descriptor: KernelIndexDescriptor): Option[IndexDescriptor] = private def getOnlineIndex(descriptor: KernelIndexDescriptor): Option[IndexDescriptor] =
tc.statement.readOperations().indexGetState(descriptor) match { tc.statement.readOperations().indexGetState(descriptor) match {
case InternalIndexState.ONLINE => Some(IndexDescriptor(descriptor.schema().getLabelId, descriptor.schema().getPropertyIds)) case InternalIndexState.ONLINE => Some(IndexDescriptor(descriptor.schema().keyId, descriptor.schema().getPropertyIds))
case _ => None case _ => None
} }


Expand Down
Expand Up @@ -22,6 +22,7 @@
import org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException; import org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException;
import org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor; import org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor;
import org.neo4j.internal.kernel.api.schema.RelationTypeSchemaDescriptor; import org.neo4j.internal.kernel.api.schema.RelationTypeSchemaDescriptor;
import org.neo4j.internal.kernel.api.schema.SchemaDescriptor;
import org.neo4j.internal.kernel.api.schema.constraints.ConstraintDescriptor; import org.neo4j.internal.kernel.api.schema.constraints.ConstraintDescriptor;


/** /**
Expand All @@ -30,12 +31,12 @@
public interface SchemaWrite public interface SchemaWrite
{ {
/** /**
* Create index from label descriptor * Create index from schema descriptor
* *
* @param descriptor description of the index * @param descriptor description of the index
* @return the newly created index * @return the newly created index
*/ */
IndexReference indexCreate( LabelSchemaDescriptor descriptor ) throws SchemaKernelException; IndexReference indexCreate( SchemaDescriptor descriptor ) throws SchemaKernelException;


/** /**
* Drop the given index * Drop the given index
Expand All @@ -49,7 +50,7 @@ public interface SchemaWrite
* *
* @param descriptor description of the constraint * @param descriptor description of the constraint
*/ */
ConstraintDescriptor uniquePropertyConstraintCreate( LabelSchemaDescriptor descriptor ) throws SchemaKernelException; ConstraintDescriptor uniquePropertyConstraintCreate( SchemaDescriptor descriptor ) throws SchemaKernelException;


/** /**
* Create node key constraint * Create node key constraint
Expand Down
Expand Up @@ -24,7 +24,7 @@


import org.neo4j.internal.kernel.api.TokenNameLookup; import org.neo4j.internal.kernel.api.TokenNameLookup;
import org.neo4j.internal.kernel.api.exceptions.KernelException; import org.neo4j.internal.kernel.api.exceptions.KernelException;
import org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor; import org.neo4j.internal.kernel.api.schema.SchemaDescriptor;
import org.neo4j.kernel.api.exceptions.Status; import org.neo4j.kernel.api.exceptions.Status;


/** /**
Expand Down Expand Up @@ -54,7 +54,7 @@ public SchemaKernelException( Status statusCode, String message )
} }


protected static String messageWithLabelAndPropertyName( TokenNameLookup tokenNameLookup, String formatString, protected static String messageWithLabelAndPropertyName( TokenNameLookup tokenNameLookup, String formatString,
LabelSchemaDescriptor descriptor ) SchemaDescriptor descriptor )
{ {
int[] propertyIds = descriptor.getPropertyIds(); int[] propertyIds = descriptor.getPropertyIds();


Expand All @@ -66,7 +66,7 @@ protected static String messageWithLabelAndPropertyName( TokenNameLookup tokenNa
.mapToObj( i -> "'" + tokenNameLookup.propertyKeyGetName( i ) + "'" ) .mapToObj( i -> "'" + tokenNameLookup.propertyKeyGetName( i ) + "'" )
.collect( Collectors.joining( " and " )); .collect( Collectors.joining( " and " ));
return String.format( formatString, return String.format( formatString,
tokenNameLookup.labelGetName( descriptor.getLabelId() ), propertyString); tokenNameLookup.labelGetName( descriptor.keyId() ), propertyString);
} }
else else
{ {
Expand All @@ -75,7 +75,7 @@ protected static String messageWithLabelAndPropertyName( TokenNameLookup tokenNa
.mapToObj( Integer::toString ) .mapToObj( Integer::toString )
.collect( Collectors.joining( ", " )) + "]"; .collect( Collectors.joining( ", " )) + "]";
return String.format( formatString, return String.format( formatString,
"label[" + descriptor.getLabelId() + "]", keyString ); "label[" + descriptor.keyId() + "]", keyString );
} }
} }
} }
Expand Up @@ -22,6 +22,4 @@
public interface LabelSchemaDescriptor extends SchemaDescriptor, LabelSchemaSupplier public interface LabelSchemaDescriptor extends SchemaDescriptor, LabelSchemaSupplier
{ {
int getLabelId(); int getLabelId();

int getPropertyId();
} }
Expand Up @@ -19,7 +19,7 @@
*/ */
package org.neo4j.internal.kernel.api.schema; package org.neo4j.internal.kernel.api.schema;


public interface LabelSchemaSupplier extends SchemaDescriptor.Supplier public interface LabelSchemaSupplier extends SchemaDescriptorSupplier
{ {
@Override @Override
LabelSchemaDescriptor schema(); LabelSchemaDescriptor schema();
Expand Down
Expand Up @@ -22,6 +22,4 @@
public interface RelationTypeSchemaDescriptor extends SchemaDescriptor public interface RelationTypeSchemaDescriptor extends SchemaDescriptor
{ {
int getRelTypeId(); int getRelTypeId();

int getPropertyId();
} }
Expand Up @@ -34,7 +34,7 @@
* how this is done in eg. LabelSchemaDescriptor, and the SchemaProcessor and SchemaComputer interfaces need to be * how this is done in eg. LabelSchemaDescriptor, and the SchemaProcessor and SchemaComputer interfaces need to be
* extended with methods taking the new concrete type as argument. * extended with methods taking the new concrete type as argument.
*/ */
public interface SchemaDescriptor public interface SchemaDescriptor extends SchemaDescriptorSupplier
{ {
/** /**
* Computes some value by feeding this object into the given SchemaComputer. * Computes some value by feeding this object into the given SchemaComputer.
Expand Down Expand Up @@ -62,6 +62,13 @@ public interface SchemaDescriptor
*/ */
String userDescription( TokenNameLookup tokenNameLookup ); String userDescription( TokenNameLookup tokenNameLookup );


/**
* Translate the schema key to a key name using the given {@link TokenNameLookup}.
* @param tokenNameLookup used for looking up names for token ids.
* @return The string name of the key token.
*/
String keyName( TokenNameLookup tokenNameLookup );

/** /**
* This method return the property ids that are relevant to this Schema Descriptor. * This method return the property ids that are relevant to this Schema Descriptor.
* *
Expand All @@ -72,6 +79,23 @@ public interface SchemaDescriptor
*/ */
int[] getPropertyIds(); int[] getPropertyIds();


/**
* Assume that this schema descriptor describes a schema that includes a single property id, and return that id.
*
* @return The presumed single property id of this schema.
* @throws IllegalStateException if this schema does not have exactly one property.
*/
default int getPropertyId()
{
int[] propertyIds = getPropertyIds();
if ( propertyIds.length != 1 )
{
throw new IllegalStateException(
"Single property schema requires one property but had " + propertyIds.length );
}
return propertyIds[0];
}

/** /**
* Id of underlying schema descriptor key. * Id of underlying schema descriptor key.
* Key is part of schema unit that determines which resources with specified properties are applicable. * Key is part of schema unit that determines which resources with specified properties are applicable.
Expand All @@ -92,7 +116,7 @@ public interface SchemaDescriptor
* @return A predicate that returns {@code true} if it is given a schema descriptor supplier that supplies the * @return A predicate that returns {@code true} if it is given a schema descriptor supplier that supplies the
* same schema descriptor as the given schema descriptor. * same schema descriptor as the given schema descriptor.
*/ */
static <T extends SchemaDescriptor.Supplier> Predicate<T> equalTo( SchemaDescriptor descriptor ) static <T extends SchemaDescriptorSupplier> Predicate<T> equalTo( SchemaDescriptor descriptor )
{ {
return supplier -> descriptor.equals( supplier.schema() ); return supplier -> descriptor.equals( supplier.schema() );
} }
Expand Down
Expand Up @@ -28,7 +28,7 @@ private SchemaDescriptorPredicates()
{ {
} }


public static <T extends SchemaDescriptor.Supplier> Predicate<T> hasLabel( int labelId ) public static <T extends SchemaDescriptorSupplier> Predicate<T> hasLabel( int labelId )
{ {
return supplier -> return supplier ->
{ {
Expand All @@ -37,7 +37,7 @@ public static <T extends SchemaDescriptor.Supplier> Predicate<T> hasLabel( int l
}; };
} }


public static <T extends SchemaDescriptor.Supplier> Predicate<T> hasRelType( int relTypeId ) public static <T extends SchemaDescriptorSupplier> Predicate<T> hasRelType( int relTypeId )
{ {
return supplier -> return supplier ->
{ {
Expand All @@ -46,24 +46,24 @@ public static <T extends SchemaDescriptor.Supplier> Predicate<T> hasRelType( int
}; };
} }


public static <T extends SchemaDescriptor.Supplier> Predicate<T> hasProperty( int propertyId ) public static <T extends SchemaDescriptorSupplier> Predicate<T> hasProperty( int propertyId )
{ {
return supplier -> hasProperty( supplier, propertyId ); return supplier -> hasProperty( supplier, propertyId );
} }


public static boolean hasLabel( SchemaDescriptor.Supplier supplier, int labelId ) public static boolean hasLabel( SchemaDescriptorSupplier supplier, int labelId )
{ {
Optional<Integer> labelOpt = supplier.schema().computeWith( getLabel ); Optional<Integer> labelOpt = supplier.schema().computeWith( getLabel );
return labelOpt.isPresent() && labelOpt.get() == labelId; return labelOpt.isPresent() && labelOpt.get() == labelId;
} }


public static boolean hasRelType( SchemaDescriptor.Supplier supplier, int relTypeId ) public static boolean hasRelType( SchemaDescriptorSupplier supplier, int relTypeId )
{ {
Optional<Integer> relTypeOpt = supplier.schema().computeWith( getRelType ); Optional<Integer> relTypeOpt = supplier.schema().computeWith( getRelType );
return relTypeOpt.isPresent() && relTypeOpt.get() == relTypeId; return relTypeOpt.isPresent() && relTypeOpt.get() == relTypeId;
} }


public static boolean hasProperty( SchemaDescriptor.Supplier supplier, int propertyId ) public static boolean hasProperty( SchemaDescriptorSupplier supplier, int propertyId )
{ {
int[] schemaProperties = supplier.schema().getPropertyIds(); int[] schemaProperties = supplier.schema().getPropertyIds();
for ( int schemaProp : schemaProperties ) for ( int schemaProp : schemaProperties )
Expand Down
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2002-2018 "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.internal.kernel.api.schema;

public interface SchemaDescriptorSupplier
{
SchemaDescriptor schema();
}
Expand Up @@ -21,8 +21,9 @@


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;


public interface ConstraintDescriptor extends SchemaDescriptor.Supplier public interface ConstraintDescriptor extends SchemaDescriptorSupplier
{ {
enum Type enum Type
{ {
Expand Down
Expand Up @@ -80,7 +80,7 @@ public void shouldFindConstraintsBySchema() throws Exception


// THEN // THEN
assertThat( constraints, hasSize( 1 ) ); assertThat( constraints, hasSize( 1 ) );
assertThat( constraints.get( 0 ).schema().getPropertyIds()[0], equalTo( prop ) ); assertThat( constraints.get( 0 ).schema().getPropertyId(), equalTo( prop ) );
} }
} }


Expand Down
Expand Up @@ -542,7 +542,7 @@ private Value getPropertyValueFromStore( long nodeReference )
} }


@Test @Test
public void shouldGetNoIndex() public void shouldGetNoIndexForMissingTokens()
{ {
int label = token.nodeLabel( "Node" ); int label = token.nodeLabel( "Node" );
int prop = token.propertyKey( "prop" ); int prop = token.propertyKey( "prop" );
Expand All @@ -554,6 +554,19 @@ public void shouldGetNoIndex()
assertEquals( "just bad", CapableIndexReference.NO_INDEX, schemaRead.index( badLabel, badProp ) ); assertEquals( "just bad", CapableIndexReference.NO_INDEX, schemaRead.index( badLabel, badProp ) );
} }


@Test
public void shouldGetNoIndexForUnknownTokens()
{
int label = token.nodeLabel( "Node" );
int prop = token.propertyKey( "prop" );
int badLabel = Integer.MAX_VALUE;
int badProp = Integer.MAX_VALUE;

assertEquals( "bad label", CapableIndexReference.NO_INDEX, schemaRead.index( badLabel, prop ) );
assertEquals( "bad prop", CapableIndexReference.NO_INDEX, schemaRead.index( label, badProp ) );
assertEquals( "just bad", CapableIndexReference.NO_INDEX, schemaRead.index( badLabel, badProp ) );
}

@Test @Test
public void shouldGetVersionAndKeyFromIndexReference() throws Exception public void shouldGetVersionAndKeyFromIndexReference() throws Exception
{ {
Expand Down
Expand Up @@ -41,7 +41,6 @@
import org.neo4j.internal.kernel.api.procs.QualifiedName; import org.neo4j.internal.kernel.api.procs.QualifiedName;
import org.neo4j.internal.kernel.api.procs.UserFunctionHandle; import org.neo4j.internal.kernel.api.procs.UserFunctionHandle;
import org.neo4j.internal.kernel.api.procs.UserFunctionSignature; import org.neo4j.internal.kernel.api.procs.UserFunctionSignature;
import org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor;
import org.neo4j.internal.kernel.api.schema.SchemaDescriptor; import org.neo4j.internal.kernel.api.schema.SchemaDescriptor;
import org.neo4j.internal.kernel.api.schema.constraints.ConstraintDescriptor; import org.neo4j.internal.kernel.api.schema.constraints.ConstraintDescriptor;
import org.neo4j.kernel.api.exceptions.RelationshipTypeIdNotFoundKernelException; import org.neo4j.kernel.api.exceptions.RelationshipTypeIdNotFoundKernelException;
Expand Down Expand Up @@ -226,8 +225,10 @@ <EXCEPTION extends Exception> void relationshipVisit( long relId, RelationshipVi
//== SCHEMA OPERATIONS ====================== //== SCHEMA OPERATIONS ======================
//=========================================== //===========================================


/** Returns the index rule for the given LabelSchemaDescriptor. */ /**
SchemaIndexDescriptor indexGetForSchema( LabelSchemaDescriptor descriptor ) * Returns the index rule for the given SchemaDescriptor.
*/
SchemaIndexDescriptor indexGetForSchema( SchemaDescriptor descriptor )
throws SchemaRuleNotFoundException; throws SchemaRuleNotFoundException;


/** Get all indexes for a label. */ /** Get all indexes for a label. */
Expand Down

0 comments on commit ee07b7b

Please sign in to comment.