Skip to content

Commit

Permalink
Minor cleaup IndexReference vs CapableIndexReference
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Mar 15, 2018
1 parent d8a4ea4 commit 2a297d8
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 78 deletions.
Expand Up @@ -19,10 +19,6 @@
*/
package org.neo4j.internal.kernel.api;

import java.util.Iterator;
import java.util.List;

import org.neo4j.helpers.collection.Iterators;
import org.neo4j.values.storable.ValueGroup;

/**
Expand Down Expand Up @@ -79,20 +75,4 @@ public String providerVersion()
return null;
}
};

/**
* Sorts indexes by type, returning first GENERAL indexes, followed by UNIQUE. Implementation is not suitable in
* hot path.
*
* @param indexes Indexes to sort
* @return sorted indexes
*/
static Iterator<CapableIndexReference> sortByType( Iterator<CapableIndexReference> indexes )
{
List<CapableIndexReference> materialized = Iterators.asList( indexes );
return Iterators.concat(
Iterators.filter( i -> !i.isUnique(), materialized.iterator() ),
Iterators.filter( IndexReference::isUnique, materialized.iterator() ) );

}
}
Expand Up @@ -19,6 +19,10 @@
*/
package org.neo4j.internal.kernel.api;

import java.util.Iterator;
import java.util.List;

import org.neo4j.helpers.collection.Iterators;
import org.neo4j.internal.kernel.api.schema.SchemaUtil;

import static java.lang.String.format;
Expand All @@ -44,4 +48,20 @@ default String userDescription( TokenNameLookup tokenNameLookup )
String type = isUnique() ? "UNIQUE" : "GENERAL";
return format( "Index( %s, %s )", type, SchemaUtil.niceProperties( tokenNameLookup, properties() ) );
}

/**
* Sorts indexes by type, returning first GENERAL indexes, followed by UNIQUE. Implementation is not suitable in
* hot path.
*
* @param indexes Indexes to sort
* @return sorted indexes
*/
static Iterator<IndexReference> sortByType( Iterator<IndexReference> indexes )
{
List<IndexReference> materialized = Iterators.asList( indexes );
return Iterators.concat(
Iterators.filter( i -> !i.isUnique(), materialized.iterator() ),
Iterators.filter( IndexReference::isUnique, materialized.iterator() ) );

}
}
Expand Up @@ -48,14 +48,14 @@ public interface SchemaRead
* @param labelId The id of the label which associated indexes you are looking for
* @return The indexes associated with the given label
*/
Iterator<CapableIndexReference> indexesGetForLabel( int labelId );
Iterator<IndexReference> indexesGetForLabel( int labelId );

/**
* Returns all indexes used in the database
*
* @return all indexes used in the database
*/
Iterator<CapableIndexReference> indexesGetAll();
Iterator<IndexReference> indexesGetAll();

/**
* Retrieves the state of an index
Expand All @@ -64,7 +64,7 @@ public interface SchemaRead
* @return The state of the provided index
* @throws IndexNotFoundKernelException if the index was not found in the database
*/
InternalIndexState indexGetState( CapableIndexReference index ) throws IndexNotFoundKernelException;
InternalIndexState indexGetState( IndexReference index ) throws IndexNotFoundKernelException;

/**
* Retrives the population progress of the index
Expand All @@ -73,14 +73,14 @@ public interface SchemaRead
* @return The population progress of the given index
* @throws IndexNotFoundKernelException if the index was not found in the database
*/
PopulationProgress indexGetPopulationProgress( CapableIndexReference index ) throws
PopulationProgress indexGetPopulationProgress( IndexReference index ) throws
IndexNotFoundKernelException;

/**
* Get the index id (the id or the schema rule record) for a committed index
* - throws exception for indexes that aren't committed.
*/
long indexGetCommittedId( CapableIndexReference index ) throws SchemaKernelException;
long indexGetCommittedId( IndexReference index ) throws SchemaKernelException;

/**
* Returns the failure description of a failed index.
Expand All @@ -89,7 +89,7 @@ PopulationProgress indexGetPopulationProgress( CapableIndexReference index ) thr
* @return The failure message from the index
* @throws IndexNotFoundKernelException if the index was not found in the database
*/
String indexGetFailure( CapableIndexReference index ) throws IndexNotFoundKernelException;
String indexGetFailure( IndexReference index ) throws IndexNotFoundKernelException;

/**
* Finds all constraints for the given schema
Expand Down Expand Up @@ -134,7 +134,7 @@ PopulationProgress indexGetPopulationProgress( CapableIndexReference index ) thr
* Get the owning constraint for a constraint index or <tt>null</tt> if the index does not have an owning
* constraint.
*/
Long indexGetOwningUniquenessConstraintId( CapableIndexReference index );
Long indexGetOwningUniquenessConstraintId( IndexReference index );

/**
* Returns schema state for the given key or create a new state if not there
Expand Down
Expand Up @@ -42,7 +42,7 @@ public interface SchemaWrite
*
* @param index the index to drop
*/
void indexDrop( CapableIndexReference index ) throws SchemaKernelException;
void indexDrop( IndexReference index ) throws SchemaKernelException;

/**
* Create unique property constraint
Expand Down
Expand Up @@ -63,7 +63,7 @@ public void setUp() throws Exception
{
schemaWrite.constraintDrop( constraints.next() );
}
Iterator<CapableIndexReference> indexes = schemaRead.indexesGetAll();
Iterator<IndexReference> indexes = schemaRead.indexesGetAll();
while ( indexes.hasNext() )
{
schemaWrite.indexDrop( indexes.next() );
Expand Down
Expand Up @@ -38,7 +38,7 @@
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.graphdb.Transaction;
import org.neo4j.internal.kernel.api.CapableIndexReference;
import org.neo4j.internal.kernel.api.IndexReference;
import org.neo4j.internal.kernel.api.Read;
import org.neo4j.internal.kernel.api.SchemaRead;
import org.neo4j.internal.kernel.api.TokenNameLookup;
Expand Down Expand Up @@ -84,11 +84,11 @@ public GraphResult buildSchemaGraph()
int labelId = tokenRead.nodeLabel( label.name() );
Map<String,Object> properties = new HashMap<>();

Iterator<CapableIndexReference> indexReferences = schemaRead.indexesGetForLabel( labelId );
Iterator<IndexReference> indexReferences = schemaRead.indexesGetForLabel( labelId );
ArrayList<String> indexes = new ArrayList<>();
while ( indexReferences.hasNext() )
{
CapableIndexReference index = indexReferences.next();
IndexReference index = indexReferences.next();
if ( !index.isUnique() )
{
String[] propertyNames = PropertyNameUtils.getPropertyKeys(
Expand Down
Expand Up @@ -25,10 +25,9 @@
import org.neo4j.internal.kernel.api.IndexCapability;
import org.neo4j.internal.kernel.api.IndexOrder;
import org.neo4j.internal.kernel.api.IndexValueCapability;
import org.neo4j.kernel.api.index.IndexProvider;
import org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor;
import org.neo4j.kernel.api.index.IndexProvider;
import org.neo4j.kernel.api.schema.index.SchemaIndexDescriptor;
import org.neo4j.kernel.api.schema.index.SchemaIndexDescriptorFactory;
import org.neo4j.values.storable.ValueGroup;

public class DefaultCapableIndexReference implements CapableIndexReference
Expand Down Expand Up @@ -131,16 +130,4 @@ public static CapableIndexReference fromDescriptor( SchemaIndexDescriptor descri
return new DefaultCapableIndexReference( unique, IndexCapability.NO_CAPABILITY, null,
schema.getLabelId(), schema.getPropertyIds() );
}

public static SchemaIndexDescriptor fromReference( CapableIndexReference index )
{
if ( index.isUnique() )
{
return SchemaIndexDescriptorFactory.uniqueForLabel( index.label(), index.properties() );
}
else
{
return SchemaIndexDescriptorFactory.forLabel( index.label(), index.properties() );
}
}
}
@@ -0,0 +1,100 @@
/*
* 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.kernel.impl.api.store;

import java.util.Arrays;

import org.neo4j.internal.kernel.api.IndexReference;
import org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor;
import org.neo4j.kernel.api.schema.index.SchemaIndexDescriptor;

public class DefaultIndexReference implements IndexReference
{
private final boolean unique;
private final int label;
private final int[] properties;

private DefaultIndexReference( boolean unique, int label, int[] properties )
{
this.unique = unique;
this.label = label;
this.properties = properties;
}

@Override
public boolean isUnique()
{
return unique;
}

@Override
public int label()
{
return label;
}

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

public static IndexReference unique( int label, int...properties )
{
return new DefaultIndexReference( true, label, properties );
}

public static IndexReference general( int label, int...properties )
{
return new DefaultIndexReference( false, label, properties );
}

public static IndexReference fromDescriptor( SchemaIndexDescriptor descriptor )
{
boolean unique = descriptor.type() == SchemaIndexDescriptor.Type.UNIQUE;
LabelSchemaDescriptor schema = descriptor.schema();
return new DefaultIndexReference( unique, schema.getLabelId(), schema.getPropertyIds() );
}

@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}

DefaultIndexReference that = (DefaultIndexReference) o;
return unique == that.unique && label == that.label && Arrays.equals( properties, that.properties );
}

@Override
public int hashCode()
{
int result = unique ? 1 : 0;
result = 31 * result + label;
result = 31 * result + Arrays.hashCode( properties );
return result;
}
}
Expand Up @@ -40,6 +40,7 @@
import org.neo4j.graphdb.schema.Schema;
import org.neo4j.helpers.collection.Iterables;
import org.neo4j.internal.kernel.api.CapableIndexReference;
import org.neo4j.internal.kernel.api.IndexReference;
import org.neo4j.internal.kernel.api.InternalIndexState;
import org.neo4j.internal.kernel.api.SchemaRead;
import org.neo4j.internal.kernel.api.TokenRead;
Expand Down Expand Up @@ -72,7 +73,6 @@
import org.neo4j.kernel.api.schema.constaints.NodeKeyConstraintDescriptor;
import org.neo4j.kernel.api.schema.constaints.RelExistenceConstraintDescriptor;
import org.neo4j.kernel.api.schema.constaints.UniquenessConstraintDescriptor;
import org.neo4j.kernel.api.schema.index.SchemaIndexDescriptor;
import org.neo4j.kernel.impl.api.operations.KeyReadOperations;
import org.neo4j.storageengine.api.schema.PopulationProgress;
import org.neo4j.storageengine.api.schema.SchemaRule;
Expand Down Expand Up @@ -119,8 +119,8 @@ public Iterable<IndexDefinition> getIndexes( final Label label )
{
return emptyList();
}
Iterator<CapableIndexReference> indexes = schemaRead.indexesGetForLabel( labelId );
addDefinitions( definitions, tokenRead, CapableIndexReference.sortByType( indexes ) );
Iterator<IndexReference> indexes = schemaRead.indexesGetForLabel( labelId );
addDefinitions( definitions, tokenRead, IndexReference.sortByType( indexes ) );
return definitions;
}
}
Expand All @@ -134,13 +134,13 @@ public Iterable<IndexDefinition> getIndexes()
{
List<IndexDefinition> definitions = new ArrayList<>();

Iterator<CapableIndexReference> indexes = schemaRead.indexesGetAll();
addDefinitions( definitions, transaction.tokenRead(), CapableIndexReference.sortByType( indexes ) );
Iterator<IndexReference> indexes = schemaRead.indexesGetAll();
addDefinitions( definitions, transaction.tokenRead(), IndexReference.sortByType( indexes ) );
return definitions;
}
}

private IndexDefinition descriptorToDefinition( final TokenRead tokenRead, CapableIndexReference index )
private IndexDefinition descriptorToDefinition( final TokenRead tokenRead, IndexReference index )
{
try
{
Expand All @@ -156,7 +156,7 @@ private IndexDefinition descriptorToDefinition( final TokenRead tokenRead, Capab
}

private void addDefinitions( List<IndexDefinition> definitions, final TokenRead tokenRead,
Iterator<CapableIndexReference> indexes )
Iterator<IndexReference> indexes )
{
addToCollection(
map( index -> descriptorToDefinition( tokenRead, index ), indexes ),
Expand Down

0 comments on commit 2a297d8

Please sign in to comment.