Skip to content

Commit

Permalink
Small bit of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
SaschaPeukert committed Jul 26, 2018
1 parent e7a1c3e commit 6f9d862
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 96 deletions.
Expand Up @@ -61,6 +61,7 @@
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Description;
import org.neo4j.procedure.Mode;
import org.neo4j.procedure.Name;
import org.neo4j.procedure.Procedure;

Expand Down Expand Up @@ -193,6 +194,21 @@ public void resampleOutdatedIndexes()
}
}

@Procedure( name = "db.schemaAsTable", mode = Mode.READ ) // I am open about a better name for this. Something like db.propertySchema maybe?
@Description( "Show the schema of the data in tabular form." )
public Stream<SchemaInfoResult> schemaAsTable()
{
return new SchemaCalculator( tx ).calculateTabularResultStream();
}

// TODO: Next step -> graph with more info then db.schema currently has
// @Procedure( value = "db.schema.graph", mode = Mode.READ )
// @Description( "Show the schema of the data." ) // old description
// public Stream<SchemaProcedure.GraphResult> schemaAsGraph()
// {
// return new SchemaCalculator( db, tx ).calculateGraphResultStream();
// }

@Description( "Show the schema of the data." )
@Procedure( name = "db.schema", mode = READ )
public Stream<SchemaProcedure.GraphResult> metaGraph()
Expand Down

This file was deleted.

Expand Up @@ -76,20 +76,20 @@ public class SchemaCalculator
this.ktx = ktx;
}

public Stream<BuiltInSchemaProcedures.SchemaInfoResult> calculateTabularResultStream()
public Stream<SchemaInfoResult> calculateTabularResultStream()
{
calculateSchema();

List<BuiltInSchemaProcedures.SchemaInfoResult> results = new ArrayList<>();
List<SchemaInfoResult> results = new ArrayList<>();
results.addAll( produceResultsForNodes() );
results.addAll( produceResultsForRelationships() );

return results.stream();
}

private List<BuiltInSchemaProcedures.SchemaInfoResult> produceResultsForRelationships()
private List<SchemaInfoResult> produceResultsForRelationships()
{
List<BuiltInSchemaProcedures.SchemaInfoResult> results = new ArrayList<>();
List<SchemaInfoResult> results = new ArrayList<>();
for ( Integer typeId : relationshipTypeIdToPropertyKeysMapping.keySet() )
{
// lookup typ name
Expand All @@ -99,7 +99,7 @@ private List<BuiltInSchemaProcedures.SchemaInfoResult> produceResultsForRelation
Set<Integer> propertyIds = relationshipTypeIdToPropertyKeysMapping.get( typeId );
if ( propertyIds.size() == 0 )
{
results.add( new BuiltInSchemaProcedures.SchemaInfoResult( RELATIONSHIP, Collections.singletonList( name ), null, null ) );
results.add( new SchemaInfoResult( RELATIONSHIP, Collections.singletonList( name ), null, null ) );
}
else
{
Expand All @@ -108,17 +108,17 @@ private List<BuiltInSchemaProcedures.SchemaInfoResult> produceResultsForRelation
// lookup propId name and valueGroup
String propName = propertyIdToPropertylNameMapping.get( propId );
ValueTypeDecider valueTypeDecider = relationshipTypeIdANDPropertyTypeIdToValueTypeMapping.get( Pair.of( typeId, propId ) );
results.add( new BuiltInSchemaProcedures.SchemaInfoResult( RELATIONSHIP, Collections.singletonList( name ), propName,
results.add( new SchemaInfoResult( RELATIONSHIP, Collections.singletonList( name ), propName,
valueTypeDecider.getCypherTypeString() ) );
}
}
}
return results;
}

private List<BuiltInSchemaProcedures.SchemaInfoResult> produceResultsForNodes()
private List<SchemaInfoResult> produceResultsForNodes()
{
List<BuiltInSchemaProcedures.SchemaInfoResult> results = new ArrayList<>();
List<SchemaInfoResult> results = new ArrayList<>();
for ( LabelSet labelSet : labelSetToPropertyKeysMapping.keySet() )
{
// lookup label names and produce list of names
Expand All @@ -133,7 +133,7 @@ private List<BuiltInSchemaProcedures.SchemaInfoResult> produceResultsForNodes()
Set<Integer> propertyIds = labelSetToPropertyKeysMapping.get( labelSet );
if ( propertyIds.size() == 0 )
{
results.add( new BuiltInSchemaProcedures.SchemaInfoResult( NODE, labelNames, null, null ) );
results.add( new SchemaInfoResult( NODE, labelNames, null, null ) );
}
else
{
Expand All @@ -142,7 +142,7 @@ private List<BuiltInSchemaProcedures.SchemaInfoResult> produceResultsForNodes()
// lookup propId name and valueGroup
String propName = propertyIdToPropertylNameMapping.get( propId );
ValueTypeDecider valueTypeDecider = labelSetANDNodePropertyKeyIdToValueTypeMapping.get( Pair.of( labelSet, propId ) );
results.add( new BuiltInSchemaProcedures.SchemaInfoResult( NODE, labelNames, propName, valueTypeDecider.getCypherTypeString() ) );
results.add( new SchemaInfoResult( NODE, labelNames, propName, valueTypeDecider.getCypherTypeString() ) );
}
}
}
Expand Down Expand Up @@ -222,7 +222,7 @@ private void scanEverythingBelongingToRelationships( Read dataRead, CursorFactor
{
// we can and need to skip this if we found the empty set
oldPropertyKeySet.removeAll( propertyIds );
oldPropertyKeySet.forEach( ( id ) -> {
oldPropertyKeySet.forEach( id -> {
Pair<Integer,Integer> key = Pair.of( typeId, id );
relationshipTypeIdANDPropertyTypeIdToValueTypeMapping.get( key ).setNullable();
} );
Expand Down Expand Up @@ -264,7 +264,7 @@ private void scanEverythingBelongingToNodes( Read dataRead, CursorFactory cursor
{
// we can and need (!) to skip this if we found the empty set
oldPropertyKeySet.removeAll( propertyIds );
oldPropertyKeySet.forEach( ( id ) -> {
oldPropertyKeySet.forEach( id -> {
Pair<LabelSet,Integer> key = Pair.of( labels, id );
labelSetANDNodePropertyKeyIdToValueTypeMapping.get( key ).setNullable();
} );
Expand Down
@@ -0,0 +1,50 @@
/*
* 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.builtinprocs;

import java.util.List;

public class SchemaInfoResult
{
/**
* Indicates whether the entry is a node or a relationship
*/
public final String type;
/**
* A combination of labels or a relationship
*/
public final List<String> nodeLabelsOrRelType;
/**
* A property that occurs on the given label combination / relationship type
*/
public final String property;
/**
* The CypherType of the given property on the given label combination / relationship type
*/
public final String cypherType;

public SchemaInfoResult( String type, List<String> nodeLabelsOrRelType, String property, String cypherType )
{
this.type = type;
this.nodeLabelsOrRelType = nodeLabelsOrRelType;
this.property = property;
this.cypherType = cypherType;
}
}
Expand Up @@ -204,6 +204,7 @@ public void shouldEscapeLabelNameContainingColons() throws Throwable
@Test
public void shouldListCorrectBuiltinProcedures() throws Throwable
{
// TODO: Add new schema procedure here (once its name is decided)
// When/Then
assertThat( call( "dbms.procedures" ), containsInAnyOrder(
record( "dbms.listConfig",
Expand Down Expand Up @@ -502,7 +503,6 @@ public void setup() throws Exception
new SpecialBuiltInProcedures( "1.3.37", Edition.enterprise.toString() ).accept( procs );
procs.registerProcedure( BuiltInProcedures.class );
procs.registerProcedure( BuiltInDbmsProcedures.class );
procs.registerProcedure( BuiltInSchemaProcedures.class );

when( tx.acquireStatement() ).thenReturn( statement );
when( tx.tokenRead() ).thenReturn( tokens );
Expand Down

0 comments on commit 6f9d862

Please sign in to comment.