Skip to content

Commit

Permalink
Allows specific provider name for all index creation methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tinwelint authored and MishaDemianenko committed Jun 9, 2018
1 parent eeb58d8 commit 4e115df
Show file tree
Hide file tree
Showing 16 changed files with 193 additions and 119 deletions.
Expand Up @@ -716,7 +716,7 @@ final class TransactionBoundQueryContext(tc: TransactionalContextWrapper, val re
def addIndexRule(labelId: Int, propertyKeyId: Int): IdempotentResult[SchemaTypes.IndexDescriptor] = try { def addIndexRule(labelId: Int, propertyKeyId: Int): IdempotentResult[SchemaTypes.IndexDescriptor] = try {
IdempotentResult( IdempotentResult(
DefaultIndexReference.toDescriptor( DefaultIndexReference.toDescriptor(
tc.kernelTransaction.schemaWrite().indexCreate(SchemaDescriptorFactory.forLabel(labelId, propertyKeyId), null)) tc.kernelTransaction.schemaWrite().indexCreate(SchemaDescriptorFactory.forLabel(labelId, propertyKeyId)))
) )
} catch { } catch {
case _: AlreadyIndexedException => case _: AlreadyIndexedException =>
Expand Down
Expand Up @@ -731,7 +731,7 @@ final class TransactionBoundQueryContext(txContext: TransactionalContextWrapper,
override def addIndexRule(labelId: Int, propertyKeyId: Int): IdempotentResult[IndexDescriptor] = try { override def addIndexRule(labelId: Int, propertyKeyId: Int): IdempotentResult[IndexDescriptor] = try {
IdempotentResult( IdempotentResult(
DefaultIndexReference.toDescriptor( DefaultIndexReference.toDescriptor(
txContext.kernelTransaction.schemaWrite().indexCreate(SchemaDescriptorFactory.forLabel(labelId, propertyKeyId), null)) txContext.kernelTransaction.schemaWrite().indexCreate(SchemaDescriptorFactory.forLabel(labelId, propertyKeyId)))
) )
} catch { } catch {
case _: AlreadyIndexedException => case _: AlreadyIndexedException =>
Expand Down
Expand Up @@ -640,7 +640,7 @@ sealed class TransactionBoundQueryContext(val transactionalContext: Transactiona
override def addIndexRule(descriptor: IndexDescriptor): IdempotentResult[IndexReference] = { override def addIndexRule(descriptor: IndexDescriptor): IdempotentResult[IndexReference] = {
val kernelDescriptor = cypherToKernelSchema(descriptor) val kernelDescriptor = cypherToKernelSchema(descriptor)
try { try {
IdempotentResult(transactionalContext.kernelTransaction.schemaWrite().indexCreate(kernelDescriptor, null)) IdempotentResult(transactionalContext.kernelTransaction.schemaWrite().indexCreate(kernelDescriptor))
} catch { } catch {
case _: AlreadyIndexedException => case _: AlreadyIndexedException =>
val indexReference = transactionalContext.kernelTransaction.schemaRead().index(kernelDescriptor.getLabelId, kernelDescriptor.getPropertyIds: _*) val indexReference = transactionalContext.kernelTransaction.schemaRead().index(kernelDescriptor.getLabelId, kernelDescriptor.getPropertyIds: _*)
Expand Down
Expand Up @@ -30,6 +30,16 @@
*/ */
public interface SchemaWrite public interface SchemaWrite
{ {
/**
* Create index from schema descriptor. Default configured index provider will be used.
*
* @param descriptor description of the index
*/
default IndexReference indexCreate( SchemaDescriptor descriptor ) throws SchemaKernelException
{
return indexCreate( descriptor, null );
}

/** /**
* Create index from schema descriptor. Optionally a specific provider name can be specified. * Create index from schema descriptor. Optionally a specific provider name can be specified.
* *
Expand All @@ -47,18 +57,42 @@ public interface SchemaWrite
void indexDrop( IndexReference index ) throws SchemaKernelException; void indexDrop( IndexReference index ) throws SchemaKernelException;


/** /**
* Create unique property constraint * Create unique property constraint. Default configured index provider will be used.
* *
* @param descriptor description of the constraint * @param descriptor description of the constraint
*/ */
ConstraintDescriptor uniquePropertyConstraintCreate( SchemaDescriptor descriptor ) throws SchemaKernelException; default ConstraintDescriptor uniquePropertyConstraintCreate( SchemaDescriptor descriptor ) throws SchemaKernelException
{
return uniquePropertyConstraintCreate( descriptor, null );
}


/** /**
* Create node key constraint * Create unique property constraint. Optionally a specific provider name can be specified.
* *
* @param descriptor description of the constraint * @param descriptor description of the constraint
* @param providerName specific index provider this index will be created for. If {@code null} then the default configured will be used.
* @return the newly created index
*/
ConstraintDescriptor uniquePropertyConstraintCreate( SchemaDescriptor descriptor, String providerName ) throws SchemaKernelException;

/**
* Create node key constraint. Default configured index provider will be used.
*
* @param descriptor description of the constraint
*/
default ConstraintDescriptor nodeKeyConstraintCreate( LabelSchemaDescriptor descriptor ) throws SchemaKernelException
{
return nodeKeyConstraintCreate( descriptor, null );
}

/**
* Create node key constraint. Optionally a specific provider name can be specified.
*
* @param descriptor description of the constraint
* @param providerName specific index provider this index will be created for. If {@code null} then the default configured will be used.
* @return the newly created index
*/ */
ConstraintDescriptor nodeKeyConstraintCreate( LabelSchemaDescriptor descriptor ) throws SchemaKernelException; ConstraintDescriptor nodeKeyConstraintCreate( LabelSchemaDescriptor descriptor, String providerName ) throws SchemaKernelException;


/** /**
* Create node property existence constraint * Create node property existence constraint
Expand Down
Expand Up @@ -19,6 +19,8 @@
*/ */
package org.neo4j.kernel.api.txstate; package org.neo4j.kernel.api.txstate;


import javax.annotation.Nullable;

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.index.IndexProvider; import org.neo4j.kernel.api.index.IndexProvider;
Expand Down Expand Up @@ -85,7 +87,7 @@ public interface TransactionState extends ReadableTransactionState
* @param providerDescriptor specific {@link IndexProvider.Descriptor} to use for this index to be created. * @param providerDescriptor specific {@link IndexProvider.Descriptor} to use for this index to be created.
* This provider descriptor is allowed to be null, which will be interpreted as simply using the default instead. * This provider descriptor is allowed to be null, which will be interpreted as simply using the default instead.
*/ */
void indexRuleDoAdd( SchemaIndexDescriptor descriptor, IndexProvider.Descriptor providerDescriptor ); void indexRuleDoAdd( SchemaIndexDescriptor descriptor, @Nullable IndexProvider.Descriptor providerDescriptor );


void indexDoDrop( SchemaIndexDescriptor descriptor ); void indexDoDrop( SchemaIndexDescriptor descriptor );


Expand Down

This file was deleted.

Expand Up @@ -27,7 +27,9 @@
public interface IndexProviderMap extends Function<IndexProvider.Descriptor,IndexProvider> public interface IndexProviderMap extends Function<IndexProvider.Descriptor,IndexProvider>
{ {
@Override @Override
IndexProvider apply( IndexProvider.Descriptor descriptor ) throws IndexProviderNotFoundException; IndexProvider apply( IndexProvider.Descriptor providerDescriptor ) throws IndexProviderNotFoundException;

IndexProvider apply( String providerDescriptorName ) throws IndexProviderNotFoundException;


IndexProvider getDefaultProvider(); IndexProvider getDefaultProvider();


Expand Down
Expand Up @@ -42,12 +42,14 @@
import org.neo4j.kernel.api.exceptions.schema.AlreadyIndexedException; import org.neo4j.kernel.api.exceptions.schema.AlreadyIndexedException;
import org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException; import org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException;
import org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException; import org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException;
import org.neo4j.kernel.api.index.IndexProvider;
import org.neo4j.kernel.api.index.PropertyAccessor; import org.neo4j.kernel.api.index.PropertyAccessor;
import org.neo4j.kernel.api.schema.constaints.ConstraintDescriptorFactory; import org.neo4j.kernel.api.schema.constaints.ConstraintDescriptorFactory;
import org.neo4j.kernel.api.schema.constaints.UniquenessConstraintDescriptor; import org.neo4j.kernel.api.schema.constaints.UniquenessConstraintDescriptor;
import org.neo4j.kernel.api.schema.index.SchemaIndexDescriptor; import org.neo4j.kernel.api.schema.index.SchemaIndexDescriptor;
import org.neo4j.kernel.api.schema.index.SchemaIndexDescriptor.Type; import org.neo4j.kernel.api.schema.index.SchemaIndexDescriptor.Type;
import org.neo4j.kernel.api.schema.index.SchemaIndexDescriptorFactory; import org.neo4j.kernel.api.schema.index.SchemaIndexDescriptorFactory;
import org.neo4j.kernel.api.txstate.TransactionState;
import org.neo4j.kernel.impl.api.KernelTransactionImplementation; import org.neo4j.kernel.impl.api.KernelTransactionImplementation;
import org.neo4j.kernel.impl.api.index.IndexProxy; import org.neo4j.kernel.impl.api.index.IndexProxy;
import org.neo4j.kernel.impl.api.index.IndexingService; import org.neo4j.kernel.impl.api.index.IndexingService;
Expand Down Expand Up @@ -91,9 +93,11 @@ public ConstraintIndexCreator( Supplier<Kernel> kernelSupplier, IndexingService
* <li>Leave this method, knowing that the uniqueness constraint rule will be added to tx state * <li>Leave this method, knowing that the uniqueness constraint rule will be added to tx state
* and this tx committed, which will create the uniqueness constraint</li> * and this tx committed, which will create the uniqueness constraint</li>
* </ol> * </ol>
*
* Btw providerDescriptor is allowed to be null, where default configured will be used.
*/ */
public long createUniquenessConstraintIndex( KernelTransactionImplementation transaction, public long createUniquenessConstraintIndex( KernelTransactionImplementation transaction,
SchemaDescriptor descriptor ) throws TransactionFailureException, CreateConstraintFailureException, SchemaDescriptor descriptor, IndexProvider.Descriptor providerDescriptor ) throws TransactionFailureException, CreateConstraintFailureException,
UniquePropertyValueValidationException, AlreadyConstrainedException UniquePropertyValueValidationException, AlreadyConstrainedException
{ {
UniquenessConstraintDescriptor constraint = ConstraintDescriptorFactory.uniqueForSchema( descriptor ); UniquenessConstraintDescriptor constraint = ConstraintDescriptorFactory.uniqueForSchema( descriptor );
Expand All @@ -102,7 +106,7 @@ public long createUniquenessConstraintIndex( KernelTransactionImplementation tra
SchemaRead schemaRead = transaction.schemaRead(); SchemaRead schemaRead = transaction.schemaRead();
try try
{ {
index = getOrCreateUniquenessConstraintIndex( schemaRead, transaction.tokenRead(), descriptor ); index = getOrCreateUniquenessConstraintIndex( schemaRead, transaction.tokenRead(), descriptor, providerDescriptor );
} }
catch ( AlreadyConstrainedException e ) catch ( AlreadyConstrainedException e )
{ {
Expand Down Expand Up @@ -242,7 +246,7 @@ private void awaitConstrainIndexPopulation( UniquenessConstraintDescriptor const
} }


private CapableIndexReference getOrCreateUniquenessConstraintIndex( SchemaRead schemaRead, private CapableIndexReference getOrCreateUniquenessConstraintIndex( SchemaRead schemaRead,
TokenRead tokenRead, SchemaDescriptor schema ) TokenRead tokenRead, SchemaDescriptor schema, IndexProvider.Descriptor providerDescriptor )
throws SchemaKernelException, IndexNotFoundKernelException throws SchemaKernelException, IndexNotFoundKernelException
{ {
CapableIndexReference descriptor = schemaRead.index( schema.keyId(), schema.getPropertyIds() ); CapableIndexReference descriptor = schemaRead.index( schema.keyId(), schema.getPropertyIds() );
Expand All @@ -265,21 +269,22 @@ private CapableIndexReference getOrCreateUniquenessConstraintIndex( SchemaRead s
// There's already an index for this schema descriptor, which isn't of the type we're after. // There's already an index for this schema descriptor, which isn't of the type we're after.
throw new AlreadyIndexedException( schema, CONSTRAINT_CREATION ); throw new AlreadyIndexedException( schema, CONSTRAINT_CREATION );
} }
SchemaIndexDescriptor indexDescriptor = createConstraintIndex( schema ); SchemaIndexDescriptor indexDescriptor = createConstraintIndex( schema, providerDescriptor );
IndexProxy indexProxy = indexingService.getIndexProxy( indexDescriptor.schema() ); IndexProxy indexProxy = indexingService.getIndexProxy( indexDescriptor.schema() );
return new DefaultCapableIndexReference( indexDescriptor.type() == Type.UNIQUE, indexProxy.getIndexCapability(), return new DefaultCapableIndexReference( indexDescriptor.type() == Type.UNIQUE, indexProxy.getIndexCapability(),
indexProxy.getProviderDescriptor(), indexDescriptor.schema().keyId(), indexProxy.getProviderDescriptor(), indexDescriptor.schema().keyId(),
indexDescriptor.schema().getPropertyIds() ); indexDescriptor.schema().getPropertyIds() );
} }


public SchemaIndexDescriptor createConstraintIndex( final SchemaDescriptor schema ) public SchemaIndexDescriptor createConstraintIndex( final SchemaDescriptor schema, IndexProvider.Descriptor providerDescriptor )
{ {
try ( Session session = kernelSupplier.get().beginSession( AUTH_DISABLED ); try ( Session session = kernelSupplier.get().beginSession( AUTH_DISABLED );
Transaction transaction = session.beginTransaction( Transaction.Type.implicit ); Transaction transaction = session.beginTransaction( Transaction.Type.implicit );
Statement ignore = ((KernelTransaction)transaction).acquireStatement() ) Statement ignore = ((KernelTransaction)transaction).acquireStatement() )
{ {
SchemaIndexDescriptor index = SchemaIndexDescriptorFactory.uniqueForSchema( schema ); SchemaIndexDescriptor index = SchemaIndexDescriptorFactory.uniqueForSchema( schema );
((KernelTransactionImplementation) transaction).txState().indexRuleDoAdd( index, null ); TransactionState transactionState = ((KernelTransactionImplementation) transaction).txState();
transactionState.indexRuleDoAdd( index, providerDescriptor );
transaction.success(); transaction.success();
return index; return index;
} }
Expand Down
Expand Up @@ -810,6 +810,10 @@ public void indexRuleDoAdd( SchemaIndexDescriptor descriptor, IndexProvider.Desc
public void indexDoDrop( SchemaIndexDescriptor descriptor ) public void indexDoDrop( SchemaIndexDescriptor descriptor )
{ {
indexChangesDiffSets().remove( descriptor ); indexChangesDiffSets().remove( descriptor );
if ( specificIndexProviders != null )
{
specificIndexProviders.remove( descriptor );
}
changed(); changed();
} }


Expand Down
Expand Up @@ -461,7 +461,7 @@ public IndexDefinition createIndexDefinition( Label label, String... propertyKey
int labelId = tokenWrite.labelGetOrCreateForName( indexDefinition.getLabel().name() ); int labelId = tokenWrite.labelGetOrCreateForName( indexDefinition.getLabel().name() );
int[] propertyKeyIds = getOrCreatePropertyKeyIds( tokenWrite, indexDefinition ); int[] propertyKeyIds = getOrCreatePropertyKeyIds( tokenWrite, indexDefinition );
LabelSchemaDescriptor descriptor = forLabel( labelId, propertyKeyIds ); LabelSchemaDescriptor descriptor = forLabel( labelId, propertyKeyIds );
transaction.schemaWrite().indexCreate( descriptor, null ); transaction.schemaWrite().indexCreate( descriptor );
return indexDefinition; return indexDefinition;
} }


Expand Down
Expand Up @@ -82,7 +82,6 @@
import org.neo4j.kernel.api.txstate.ExplicitIndexTransactionState; import org.neo4j.kernel.api.txstate.ExplicitIndexTransactionState;
import org.neo4j.kernel.api.txstate.TransactionState; import org.neo4j.kernel.api.txstate.TransactionState;
import org.neo4j.kernel.impl.api.KernelTransactionImplementation; import org.neo4j.kernel.impl.api.KernelTransactionImplementation;
import org.neo4j.kernel.impl.api.index.IndexProviderDescriptorByName;
import org.neo4j.kernel.impl.api.index.IndexProviderMap; import org.neo4j.kernel.impl.api.index.IndexProviderMap;
import org.neo4j.kernel.impl.api.state.ConstraintIndexCreator; import org.neo4j.kernel.impl.api.state.ConstraintIndexCreator;
import org.neo4j.kernel.impl.api.store.DefaultIndexReference; import org.neo4j.kernel.impl.api.store.DefaultIndexReference;
Expand Down Expand Up @@ -887,16 +886,7 @@ public IndexReference indexCreate( SchemaDescriptor descriptor, String providerN
assertIndexDoesNotExist( SchemaKernelException.OperationContext.INDEX_CREATION, descriptor ); assertIndexDoesNotExist( SchemaKernelException.OperationContext.INDEX_CREATION, descriptor );


SchemaIndexDescriptor indexDescriptor = SchemaIndexDescriptorFactory.forSchema( descriptor ); SchemaIndexDescriptor indexDescriptor = SchemaIndexDescriptorFactory.forSchema( descriptor );
IndexProvider.Descriptor providerDescriptor = null; IndexProvider.Descriptor providerDescriptor = providerName != null ? indexProviderMap.apply( providerName ).getProviderDescriptor() : null;
if ( providerName != null )
{
IndexProviderDescriptorByName candidates = new IndexProviderDescriptorByName( providerName );
indexProviderMap.accept( candidates );

// We have to have a one-to-one mapping to the specified provider name, otherwise we can't be sure which.
// Having this method fail if that's not the case is OK since that's a user error and way before commit.
providerDescriptor = candidates.single();
}
ktx.txState().indexRuleDoAdd( indexDescriptor, providerDescriptor ); ktx.txState().indexRuleDoAdd( indexDescriptor, providerDescriptor );
return DefaultIndexReference.fromDescriptor( indexDescriptor ); return DefaultIndexReference.fromDescriptor( indexDescriptor );
} }
Expand Down Expand Up @@ -934,7 +924,7 @@ public void indexDrop( IndexReference index ) throws SchemaKernelException
} }


@Override @Override
public ConstraintDescriptor uniquePropertyConstraintCreate( SchemaDescriptor descriptor ) public ConstraintDescriptor uniquePropertyConstraintCreate( SchemaDescriptor descriptor, String providerName )
throws SchemaKernelException throws SchemaKernelException
{ {
//Lock //Lock
Expand All @@ -949,12 +939,13 @@ public ConstraintDescriptor uniquePropertyConstraintCreate( SchemaDescriptor des
assertIndexDoesNotExist( SchemaKernelException.OperationContext.CONSTRAINT_CREATION, descriptor ); assertIndexDoesNotExist( SchemaKernelException.OperationContext.CONSTRAINT_CREATION, descriptor );


// Create constraints // Create constraints
indexBackedConstraintCreate( constraint ); IndexProvider.Descriptor providerDescriptor = providerName != null ? indexProviderMap.apply( providerName ).getProviderDescriptor() : null;
indexBackedConstraintCreate( constraint, providerDescriptor );
return constraint; return constraint;
} }


@Override @Override
public ConstraintDescriptor nodeKeyConstraintCreate( LabelSchemaDescriptor descriptor ) throws SchemaKernelException public ConstraintDescriptor nodeKeyConstraintCreate( LabelSchemaDescriptor descriptor, String providerName ) throws SchemaKernelException
{ {
//Lock //Lock
acquireExclusiveLabelLock( descriptor.getLabelId() ); acquireExclusiveLabelLock( descriptor.getLabelId() );
Expand All @@ -975,7 +966,8 @@ public ConstraintDescriptor nodeKeyConstraintCreate( LabelSchemaDescriptor descr
} }


//create constraint //create constraint
indexBackedConstraintCreate( constraint ); IndexProvider.Descriptor providerDescriptor = providerName != null ? indexProviderMap.apply( providerName ).getProviderDescriptor() : null;
indexBackedConstraintCreate( constraint, providerDescriptor );
return constraint; return constraint;
} }


Expand Down Expand Up @@ -1204,7 +1196,7 @@ private org.neo4j.kernel.api.schema.LabelSchemaDescriptor labelDescriptor( Index
return SchemaDescriptorFactory.forLabel( index.label(), index.properties() ); return SchemaDescriptorFactory.forLabel( index.label(), index.properties() );
} }


private void indexBackedConstraintCreate( IndexBackedConstraintDescriptor constraint ) private void indexBackedConstraintCreate( IndexBackedConstraintDescriptor constraint, IndexProvider.Descriptor providerDescriptor )
throws CreateConstraintFailureException throws CreateConstraintFailureException
{ {
SchemaDescriptor descriptor = constraint.schema(); SchemaDescriptor descriptor = constraint.schema();
Expand All @@ -1229,7 +1221,7 @@ private void indexBackedConstraintCreate( IndexBackedConstraintDescriptor constr
return; return;
} }
} }
long indexId = constraintIndexCreator.createUniquenessConstraintIndex( ktx, descriptor ); long indexId = constraintIndexCreator.createUniquenessConstraintIndex( ktx, descriptor, providerDescriptor );
if ( !allStoreHolder.constraintExists( constraint ) ) if ( !allStoreHolder.constraintExists( constraint ) )
{ {
// This looks weird, but since we release the label lock while awaiting population of the index // This looks weird, but since we release the label lock while awaiting population of the index
Expand Down

0 comments on commit 4e115df

Please sign in to comment.