Skip to content

Commit

Permalink
Made SchemaIndexDescriptor a IndexReference and changed indexCreate
Browse files Browse the repository at this point in the history
  • Loading branch information
fickludd authored and ragadeeshu committed May 21, 2018
1 parent 94a3b00 commit 9dd62e4
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 11 deletions.
Expand Up @@ -19,6 +19,8 @@
*/
package org.neo4j.internal.kernel.api;

import java.util.Optional;

import org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException;
import org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor;
import org.neo4j.internal.kernel.api.schema.RelationTypeSchemaDescriptor;
Expand All @@ -38,6 +40,10 @@ public interface SchemaWrite
*/
IndexReference indexCreate( SchemaDescriptor descriptor ) throws SchemaKernelException;

IndexReference indexCreate( SchemaDescriptor descriptor,
Optional<String> provider,
Optional<String> name ) throws SchemaKernelException;

/**
* Drop the given index
*
Expand Down
Expand Up @@ -21,9 +21,11 @@

import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

import org.neo4j.helpers.collection.Iterators;
import org.neo4j.internal.kernel.api.IndexReference;
import org.neo4j.internal.kernel.api.TokenNameLookup;
import org.neo4j.internal.kernel.api.schema.SchemaDescriptor;
import org.neo4j.internal.kernel.api.schema.SchemaDescriptorSupplier;
Expand All @@ -37,7 +39,7 @@
* Internal representation of a graph index, including the schema unit it targets (eg. label-property combination)
* and the type of index. UNIQUE indexes are used to back uniqueness constraints.
*/
public class SchemaIndexDescriptor implements SchemaDescriptorSupplier
public class SchemaIndexDescriptor implements SchemaDescriptorSupplier, IndexReference
{
public enum Type
{
Expand Down Expand Up @@ -80,11 +82,21 @@ public interface Supplier

private final SchemaDescriptor schema;
private final SchemaIndexDescriptor.Type type;

public SchemaIndexDescriptor( SchemaDescriptor schema, Type type )
private final Optional<String> name;
private final String providerKey;
private final String providerVersion;

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

// METHODS
Expand All @@ -104,6 +116,24 @@ public SchemaDescriptor schema()
return schema;
}

@Override
public boolean isUnique()
{
return type == Type.UNIQUE;
}

@Override
public int label()
{
return schema.keyId();
}

@Override
public int[] properties()
{
return schema.getPropertyIds();
}

/**
* @param tokenNameLookup used for looking up names for token ids.
* @return a user friendly description of what this index indexes.
Expand Down
Expand Up @@ -20,6 +20,8 @@
package org.neo4j.kernel.api.schema.index;


import java.util.Optional;

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

Expand All @@ -44,11 +46,16 @@ public static SchemaIndexDescriptor uniqueForLabel( int labelId, int... property

public static SchemaIndexDescriptor forSchema( SchemaDescriptor schema )
{
return new SchemaIndexDescriptor( schema, GENERAL );
return new SchemaIndexDescriptor( schema, GENERAL, null, null, null );
}

public static SchemaIndexDescriptor forSchema( SchemaDescriptor schema, Optional<String> name, String providerKey, String providerVersion )
{
return new SchemaIndexDescriptor( schema, GENERAL, name, providerKey, providerVersion );
}

public static SchemaIndexDescriptor uniqueForSchema( SchemaDescriptor schema )
{
return new SchemaIndexDescriptor( schema, UNIQUE );
return new SchemaIndexDescriptor( schema, UNIQUE, null, null, null );
}
}
Expand Up @@ -66,6 +66,7 @@
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException;
import org.neo4j.kernel.api.explicitindex.AutoIndexing;
import org.neo4j.kernel.api.index.IndexProvider;
import org.neo4j.kernel.api.schema.index.SchemaIndexDescriptor;
import org.neo4j.kernel.api.txstate.ExplicitIndexTransactionState;
import org.neo4j.kernel.api.txstate.TransactionState;
Expand Down Expand Up @@ -1043,6 +1044,11 @@ public Statistics getStatistics()
return statistics;
}

public IndexProvider.Descriptor indexProviderForOrDefault( Optional<String> provider )
{
return storageEngine.indexProviderForOrDefault( provider );
}

public static class Statistics
{
private volatile long cpuTimeNanosWhenQueryStarted;
Expand Down
Expand Up @@ -562,7 +562,8 @@ public LongDiffSets nodesWithLabelChanged( long label )
}

@Override
public void indexRuleDoAdd( SchemaIndexDescriptor descriptor )
public void
indexRuleDoAdd( SchemaIndexDescriptor descriptor )
{
DiffSets<SchemaIndexDescriptor> diff = indexChangesDiffSets();
if ( !diff.unRemove( descriptor ) )
Expand Down
Expand Up @@ -25,6 +25,7 @@
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;

import org.neo4j.helpers.collection.CastingIterator;
import org.neo4j.helpers.collection.Iterators;
Expand Down Expand Up @@ -71,6 +72,7 @@
import org.neo4j.kernel.api.exceptions.schema.UnableToValidateConstraintException;
import org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException;
import org.neo4j.kernel.api.explicitindex.AutoIndexing;
import org.neo4j.kernel.api.index.IndexProvider;
import org.neo4j.kernel.api.schema.SchemaDescriptorFactory;
import org.neo4j.kernel.api.schema.constaints.ConstraintDescriptorFactory;
import org.neo4j.kernel.api.schema.constaints.IndexBackedConstraintDescriptor;
Expand All @@ -81,6 +83,7 @@
import org.neo4j.kernel.api.txstate.ExplicitIndexTransactionState;
import org.neo4j.kernel.api.txstate.TransactionState;
import org.neo4j.kernel.impl.api.KernelTransactionImplementation;
import org.neo4j.kernel.impl.api.index.IndexProviderMap;
import org.neo4j.kernel.impl.api.state.ConstraintIndexCreator;
import org.neo4j.kernel.impl.api.store.DefaultIndexReference;
import org.neo4j.kernel.impl.constraints.ConstraintSemantics;
Expand Down Expand Up @@ -874,15 +877,28 @@ public DefaultPropertyCursor propertyCursor()

@Override
public IndexReference indexCreate( SchemaDescriptor descriptor ) throws SchemaKernelException
{
return indexCreate( descriptor, Optional.empty(), Optional.empty() );
}

@Override
public IndexReference indexCreate( SchemaDescriptor descriptor,
Optional<String> provider,
Optional<String> name ) throws SchemaKernelException
{
acquireExclusiveLabelLock( descriptor.keyId() );
ktx.assertOpen();
assertValidDescriptor( descriptor, SchemaKernelException.OperationContext.INDEX_CREATION );
assertIndexDoesNotExist( SchemaKernelException.OperationContext.INDEX_CREATION, descriptor );

SchemaIndexDescriptor indexDescriptor = SchemaIndexDescriptorFactory.forSchema( descriptor );
ktx.txState().indexRuleDoAdd( indexDescriptor );
return DefaultIndexReference.fromDescriptor( indexDescriptor );
IndexProvider.Descriptor providerDescriptor = ktx.indexProviderForOrDefault( provider );
SchemaIndexDescriptor index =
SchemaIndexDescriptorFactory.forSchema( descriptor,
name,
providerDescriptor.getKey(),
providerDescriptor.getVersion() );
ktx.txState().indexRuleDoAdd( index );
return index;
}

@Override
Expand Down
Expand Up @@ -24,6 +24,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

import org.neo4j.graphdb.factory.GraphDatabaseSettings;
Expand All @@ -38,6 +39,7 @@
import org.neo4j.io.pagecache.tracing.cursor.context.VersionContextSupplier;
import org.neo4j.kernel.api.exceptions.TransactionApplyKernelException;
import org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException;
import org.neo4j.kernel.api.index.IndexProvider;
import org.neo4j.kernel.api.labelscan.LabelScanStore;
import org.neo4j.kernel.api.labelscan.LabelScanWriter;
import org.neo4j.kernel.api.labelscan.LoggingMonitor;
Expand Down Expand Up @@ -246,6 +248,12 @@ public StorageReader newReader()
schemaCache, indexReaderFactory, labelScanStore::newReader, lockService, allocateCommandCreationContext() );
}

@Override
public IndexProvider.Descriptor indexProviderForOrDefault( Optional<String> providerName )
{
return indexProviderMap.getDefaultProvider().getProviderDescriptor();
}

@Override
public RecordStorageCommandCreationContext allocateCommandCreationContext()
{
Expand Down
Expand Up @@ -20,12 +20,15 @@
package org.neo4j.storageengine.api;

import java.util.Collection;
import java.util.Optional;
import java.util.stream.Stream;

import org.neo4j.internal.kernel.api.exceptions.TransactionFailureException;
import org.neo4j.internal.kernel.api.exceptions.schema.ConstraintValidationException;
import org.neo4j.io.pagecache.IOLimiter;
import org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException;
import org.neo4j.kernel.api.index.IndexProvider;
import org.neo4j.kernel.impl.api.index.IndexProviderMap;
import org.neo4j.kernel.info.DiagnosticsManager;
import org.neo4j.storageengine.api.lock.ResourceLocker;
import org.neo4j.storageengine.api.txstate.ReadableTransactionState;
Expand All @@ -45,6 +48,8 @@ public interface StorageEngine
*/
StorageReader newReader();

IndexProvider.Descriptor indexProviderForOrDefault( Optional<String> providerName );

/**
* @return a new {@link CommandCreationContext} meant to be kept for multiple calls to
* {@link #createCommands(Collection, ReadableTransactionState, StorageReader, ResourceLocker,
Expand Down
Expand Up @@ -134,8 +134,8 @@ public void setUp() throws InvalidTransactionTypeKernelException
allStoreHolder = new AllStoreHolder( storageReader, transaction, cursors, mock(
ExplicitIndexStore.class ), mock( Procedures.class ), mock( SchemaState.class ) );
constraintIndexCreator = mock( ConstraintIndexCreator.class );
operations = new Operations( allStoreHolder, mock( IndexTxStateUpdater.class ), storageReader,
transaction, new KernelToken( storageReader, transaction ), cursors, autoindexing,
operations = new Operations( allStoreHolder, mock( IndexTxStateUpdater.class ),storageReader,
transaction, new KernelToken( storageReader, transaction ), cursors, autoindexing,
constraintIndexCreator, mock( ConstraintSemantics.class ) );
operations.initialize();

Expand Down

0 comments on commit 9dd62e4

Please sign in to comment.