Skip to content

Commit

Permalink
IndexReference include label name when printing user friendly index d…
Browse files Browse the repository at this point in the history
…escription

Also collect all index description formatting into SchemaUtil.
  • Loading branch information
burqen committed Oct 11, 2018
1 parent f7a5ce5 commit 7570f4f
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 9 deletions.
Expand Up @@ -25,8 +25,6 @@
import org.neo4j.helpers.collection.Iterators;
import org.neo4j.internal.kernel.api.schema.SchemaUtil;

import static java.lang.String.format;

/**
* Reference to a specific index. This reference is valid until the schema of the database changes (that is a
* create/drop of an index or constraint occurs).
Expand All @@ -46,7 +44,8 @@ public interface IndexReference
default String userDescription( TokenNameLookup tokenNameLookup )
{
String type = isUnique() ? "UNIQUE" : "GENERAL";
return format( "Index( %s, %s )", type, SchemaUtil.niceProperties( tokenNameLookup, properties() ) );
String schemaDescription = SchemaUtil.niceLabelAndProperties( tokenNameLookup, label(), properties() );
return SchemaUtil.withType( type, schemaDescription );
}

/**
Expand Down
Expand Up @@ -57,6 +57,25 @@ public static String niceProperties( TokenNameLookup tokenNameLookup, int[] prop
return properties.toString();
}

public static String niceLabelAndProperties( TokenNameLookup tokenNameLookup, int labelId, int[] propertyIds )
{
String label = tokenNameLookup.labelGetName( labelId );
String properties = SchemaUtil.niceProperties( tokenNameLookup, propertyIds );
return String.format( ":%s(%s)", label, properties );
}

public static String niceRelTypeAndProperties( TokenNameLookup tokenNameLookup, int relTypeId, int[] propertyIds )
{
String relationshipTypeGetName = tokenNameLookup.relationshipTypeGetName( relTypeId );
String properties = SchemaUtil.niceProperties( tokenNameLookup, propertyIds );
return String.format( "-[:%s(%s)]-", relationshipTypeGetName, properties );
}

public static String withType( String type, String schemaDescription )
{
return format( "Index( %s, %s )", type, schemaDescription );
}

public static final TokenNameLookup idTokenNameLookup = new TokenNameLookup()
{

Expand Down
Expand Up @@ -54,8 +54,7 @@ public void processWith( SchemaProcessor processor )
@Override
public String userDescription( TokenNameLookup tokenNameLookup )
{
return String.format( ":%s(%s)", tokenNameLookup.labelGetName( labelId ),
SchemaUtil.niceProperties( tokenNameLookup, propertyIds ) );
return SchemaUtil.niceLabelAndProperties( tokenNameLookup, labelId, propertyIds );
}

@Override
Expand Down
Expand Up @@ -55,8 +55,7 @@ public void processWith( SchemaProcessor processor )
@Override
public String userDescription( TokenNameLookup tokenNameLookup )
{
return String.format( "-[:%s(%s)]-", tokenNameLookup.relationshipTypeGetName( relTypeId ),
SchemaUtil.niceProperties( tokenNameLookup, propertyIds ) );
return SchemaUtil.niceRelTypeAndProperties( tokenNameLookup, relTypeId, propertyIds );
}

@Override
Expand Down
Expand Up @@ -29,7 +29,6 @@
import org.neo4j.internal.kernel.api.schema.SchemaDescriptorSupplier;
import org.neo4j.internal.kernel.api.schema.SchemaUtil;

import static java.lang.String.format;
import static org.neo4j.kernel.api.schema.index.SchemaIndexDescriptor.Filter.GENERAL;
import static org.neo4j.kernel.api.schema.index.SchemaIndexDescriptor.Filter.UNIQUE;

Expand Down Expand Up @@ -110,7 +109,7 @@ public SchemaDescriptor schema()
*/
public String userDescription( TokenNameLookup tokenNameLookup )
{
return format( "Index( %s, %s )", type.name(), schema.userDescription( tokenNameLookup ) );
return SchemaUtil.withType( type.name(), schema.userDescription( tokenNameLookup ) );
}

/**
Expand Down
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.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 org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;

import org.neo4j.internal.kernel.api.IndexCapability;
import org.neo4j.internal.kernel.api.IndexReference;
import org.neo4j.kernel.api.index.IndexProvider;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.neo4j.kernel.api.schema.SchemaTestUtil.simpleNameLookup;

@RunWith( Parameterized.class )
public class IndexReferenceTest
{
private final String expectedUserDescription;
private final IndexReference indexReference;

@Parameterized.Parameters( name = "{0}")
public static Iterable<Object[]> data()
{
return Arrays.asList(
new Object[]{"Index( GENERAL, :Label1(property2) )", DefaultIndexReference.general( 1, 2 )},
new Object[]{"Index( UNIQUE, :Label10(property200) )", DefaultIndexReference.unique( 10, 200 )},
new Object[]{"Index( GENERAL, :Label42(property3) )", defaultCapableIndexReference( false, 42, 3 )},
new Object[]{"Index( UNIQUE, :Label42(property3) )", defaultCapableIndexReference( true, 42, 3 )},
new Object[]{"Index( GENERAL, :Label1(property2, property20) )", DefaultIndexReference.general( 1, 2, 20 )},
new Object[]{"Index( UNIQUE, :Label10(property200, property2000) )", DefaultIndexReference.unique( 10, 200, 2000 )},
new Object[]{"Index( GENERAL, :Label42(property3, property30) )", defaultCapableIndexReference( false, 42, 3, 30 )},
new Object[]{"Index( UNIQUE, :Label42(property3, property30) )", defaultCapableIndexReference( true, 42, 3, 30 )}
);
}

public IndexReferenceTest( String expectedUserDescription, IndexReference indexReference )
{
this.expectedUserDescription = expectedUserDescription;
this.indexReference = indexReference;
}

@Test
public void shouldGiveNiceUserDescriptions()
{
assertThat( indexReference.userDescription( simpleNameLookup ), equalTo( expectedUserDescription ) );
}

private static DefaultCapableIndexReference defaultCapableIndexReference( boolean unique, int labelId, int... propertyIds )
{
return new DefaultCapableIndexReference( unique, IndexCapability.NO_CAPABILITY,
new IndexProvider.Descriptor( "no-desc", "1.0"), labelId, propertyIds );
}
}

0 comments on commit 7570f4f

Please sign in to comment.