Skip to content

Commit

Permalink
Make it impossible to create an IndexDefinitionImpl that has both lab…
Browse files Browse the repository at this point in the history
…els and relationship types.
  • Loading branch information
chrisvest committed Aug 9, 2018
1 parent b6e4379 commit 98746b7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 28 deletions.
Expand Up @@ -39,15 +39,21 @@ public class IndexDefinitionImpl implements IndexDefinition
private final String[] propertyKeys;
private final boolean constraintIndex;

public IndexDefinitionImpl( InternalSchemaActions actions, Label label, String[] propertyKeys, boolean constraintIndex ) // TODO remove this constructor
public IndexDefinitionImpl( InternalSchemaActions actions, Label[] labels, String[] propertyKeys, boolean constraintIndex )
{
this( actions, new Label[]{label}, null, propertyKeys, constraintIndex );
this.actions = actions;
this.labels = labels;
this.relTypes = null;
this.propertyKeys = propertyKeys;
this.constraintIndex = constraintIndex;

assertInUnterminatedTransaction();
}

public IndexDefinitionImpl( InternalSchemaActions actions, Label[] labels, RelationshipType[] relTypes, String[] propertyKeys, boolean constraintIndex )
public IndexDefinitionImpl( InternalSchemaActions actions, RelationshipType[] relTypes, String[] propertyKeys, boolean constraintIndex )
{
this.actions = actions;
this.labels = labels;
this.labels = null;
this.relTypes = relTypes;
this.propertyKeys = propertyKeys;
this.constraintIndex = constraintIndex;
Expand Down
Expand Up @@ -46,8 +46,7 @@ public final ConstraintDefinition create()
assertInUnterminatedTransaction();

IndexDefinitionImpl definition =
new IndexDefinitionImpl( actions, label, propertyKeys.toArray( new String[propertyKeys.size()] ),
true );
new IndexDefinitionImpl( actions, new Label[]{label}, propertyKeys.toArray( new String[0] ), true );
return actions.createPropertyUniquenessConstraint( definition );
}
}
Expand Up @@ -148,31 +148,27 @@ private IndexDefinition descriptorToDefinition( final TokenRead tokenRead, Index
{
SchemaDescriptor schema = index.schema();
int[] entityTokenIds = schema.getEntityTokenIds();
Label[] labels = null;
RelationshipType[] relTypes = null;
boolean constraintIndex = index.isUnique();
String[] propertyNames = PropertyNameUtils.getPropertyKeys( tokenRead, index.properties() );
switch ( schema.entityType() )
{
case NODE:
labels = new Label[entityTokenIds.length];
Label[] labels = new Label[entityTokenIds.length];
for ( int i = 0; i < labels.length; i++ )
{
labels[i] = label( tokenRead.nodeLabelName( entityTokenIds[i] ) );
}
break;
return new IndexDefinitionImpl( actions, labels, propertyNames, constraintIndex );
case RELATIONSHIP:
relTypes = new RelationshipType[entityTokenIds.length];
RelationshipType[] relTypes = new RelationshipType[entityTokenIds.length];
for ( int i = 0; i < relTypes.length; i++ )
{
relTypes[i] = withName( tokenRead.relationshipTypeName( entityTokenIds[i] ) );
}
break;
return new IndexDefinitionImpl( actions, relTypes, propertyNames, constraintIndex );
default:
throw new IllegalArgumentException( "Cannot create IndexDefinition for " + schema.entityType() + " entity-typed schema." );
}

boolean constraintIndex = index.isUnique();
String[] propertyNames = PropertyNameUtils.getPropertyKeys( tokenRead, index.properties() );
return new IndexDefinitionImpl( actions, labels, relTypes, propertyNames, constraintIndex );
}
catch ( KernelException e )
{
Expand Down Expand Up @@ -475,22 +471,24 @@ private ConstraintDefinition asConstraintDefinition( ConstraintDescriptor constr
constraint instanceof UniquenessConstraintDescriptor )
{
SchemaDescriptor schemaDescriptor = constraint.schema();
Label label = Label.label( lookup.labelGetName( schemaDescriptor.keyId() ) );
String[] propertyKeys = Arrays.stream( schemaDescriptor.getPropertyIds() )
.mapToObj( lookup::propertyKeyGetName ).toArray( String[]::new );
int[] entityTokenIds = schemaDescriptor.getEntityTokenIds();
Label[] labels = new Label[entityTokenIds.length];
for ( int i = 0; i < entityTokenIds.length; i++ )
{
labels[i] = Label.label( lookup.labelGetName( entityTokenIds[i] ) );
}
String[] propertyKeys = Arrays.stream( schemaDescriptor.getPropertyIds() ).mapToObj( lookup::propertyKeyGetName ).toArray( String[]::new );
if ( constraint instanceof NodeExistenceConstraintDescriptor )
{
return new NodePropertyExistenceConstraintDefinition( actions, label, propertyKeys );
return new NodePropertyExistenceConstraintDefinition( actions, labels[0], propertyKeys );
}
else if ( constraint instanceof UniquenessConstraintDescriptor )
{
return new UniquenessConstraintDefinition( actions, new IndexDefinitionImpl( actions, label,
propertyKeys, true ) );
return new UniquenessConstraintDefinition( actions, new IndexDefinitionImpl( actions, labels, propertyKeys, true ) );
}
else
{
return new NodeKeyConstraintDefinition( actions, new IndexDefinitionImpl( actions, label,
propertyKeys, true ) );
return new NodeKeyConstraintDefinition( actions, new IndexDefinitionImpl( actions, labels, propertyKeys, true ) );
}
}
else if ( constraint instanceof RelExistenceConstraintDescriptor )
Expand Down Expand Up @@ -532,7 +530,7 @@ public IndexDefinition createIndexDefinition( Label label, String... propertyKey
{
try
{
IndexDefinition indexDefinition = new IndexDefinitionImpl( this, label, propertyKeys, false );
IndexDefinition indexDefinition = new IndexDefinitionImpl( this, new Label[]{label}, propertyKeys, false );
TokenWrite tokenWrite = transaction.tokenWrite();
int labelId = tokenWrite.labelGetOrCreateForName( indexDefinition.getLabel().name() );
int[] propertyKeyIds = getOrCreatePropertyKeyIds( tokenWrite, indexDefinition );
Expand Down
Expand Up @@ -1118,7 +1118,7 @@ public IndexDefinition createIndexDefinition( Label label, String... propertyKey
validateIndexCanBeCreated( labelId, propertyKeyIds );

createIndex( labelId, propertyKeyIds );
return new IndexDefinitionImpl( this, label, propertyKeys, false );
return new IndexDefinitionImpl( this, new Label[]{label}, propertyKeys, false );
}

@Override
Expand Down
Expand Up @@ -134,7 +134,7 @@ private ConstraintDefinition createUniquenessConstraint( Label label, String pro
SchemaHelper.createUniquenessConstraint( db, label, propertyKey );
SchemaHelper.awaitIndexes( db );
InternalSchemaActions actions = mock( InternalSchemaActions.class );
IndexDefinition index = new IndexDefinitionImpl( actions, label, new String[]{propertyKey}, true );
IndexDefinition index = new IndexDefinitionImpl( actions, new Label[]{label}, new String[]{propertyKey}, true );
return new UniquenessConstraintDefinition( actions, index );
}

Expand All @@ -143,7 +143,7 @@ private ConstraintDefinition createNodeKeyConstraint( Label label, String proper
SchemaHelper.createNodeKeyConstraint( db, label, propertyKey );
SchemaHelper.awaitIndexes( db );
InternalSchemaActions actions = mock( InternalSchemaActions.class );
IndexDefinition index = new IndexDefinitionImpl( actions, label, new String[]{propertyKey}, true );
IndexDefinition index = new IndexDefinitionImpl( actions, new Label[]{label}, new String[]{propertyKey}, true );
return new NodeKeyConstraintDefinition( actions, index );
}

Expand Down

0 comments on commit 98746b7

Please sign in to comment.