Skip to content

Commit

Permalink
Change SchemaProcedure to use Kernel API
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Mar 7, 2018
1 parent 87135c4 commit 709c67e
Showing 1 changed file with 20 additions and 21 deletions.
Expand Up @@ -38,18 +38,18 @@
import org.neo4j.graphdb.RelationshipType; import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.ResourceIterator; import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.graphdb.Transaction; import org.neo4j.graphdb.Transaction;
import org.neo4j.internal.kernel.api.CapableIndexReference;
import org.neo4j.internal.kernel.api.Read;
import org.neo4j.internal.kernel.api.SchemaRead;
import org.neo4j.internal.kernel.api.TokenNameLookup; import org.neo4j.internal.kernel.api.TokenNameLookup;
import org.neo4j.internal.kernel.api.TokenRead;
import org.neo4j.internal.kernel.api.schema.constraints.ConstraintDescriptor; import org.neo4j.internal.kernel.api.schema.constraints.ConstraintDescriptor;
import org.neo4j.kernel.api.KernelTransaction; import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.ReadOperations;
import org.neo4j.kernel.api.SilentTokenNameLookup; import org.neo4j.kernel.api.SilentTokenNameLookup;
import org.neo4j.kernel.api.Statement; import org.neo4j.kernel.api.Statement;
import org.neo4j.kernel.api.schema.index.IndexDescriptor;
import org.neo4j.kernel.impl.coreapi.schema.PropertyNameUtils; import org.neo4j.kernel.impl.coreapi.schema.PropertyNameUtils;
import org.neo4j.kernel.internal.GraphDatabaseAPI; import org.neo4j.kernel.internal.GraphDatabaseAPI;


import static org.neo4j.kernel.api.schema.index.IndexDescriptor.Type.GENERAL;

public class SchemaProcedure public class SchemaProcedure
{ {


Expand All @@ -69,9 +69,10 @@ public GraphResult buildSchemaGraph()


try ( Statement statement = kernelTransaction.acquireStatement() ) try ( Statement statement = kernelTransaction.acquireStatement() )
{ {
ReadOperations readOperations = statement.readOperations(); Read dataRead = kernelTransaction.dataRead();
TokenNameLookup tokenNameLookup = new SilentTokenNameLookup( kernelTransaction.tokenRead() ); TokenRead tokenRead = kernelTransaction.tokenRead();

TokenNameLookup tokenNameLookup = new SilentTokenNameLookup( tokenRead );
SchemaRead schemaRead = kernelTransaction.schemaRead();
try ( Transaction transaction = graphDatabaseAPI.beginTx() ) try ( Transaction transaction = graphDatabaseAPI.beginTx() )
{ {
// add all labelsInDatabase // add all labelsInDatabase
Expand All @@ -80,25 +81,25 @@ public GraphResult buildSchemaGraph()
while ( labelsInDatabase.hasNext() ) while ( labelsInDatabase.hasNext() )
{ {
Label label = labelsInDatabase.next(); Label label = labelsInDatabase.next();
int labelId = readOperations.labelGetForName( label.name() ); int labelId = tokenRead.nodeLabel( label.name() );
Map<String,Object> properties = new HashMap<>(); Map<String,Object> properties = new HashMap<>();


Iterator<IndexDescriptor> indexDescriptorIterator = readOperations.indexesGetForLabel( labelId ); Iterator<CapableIndexReference> indexReferences = schemaRead.indexesGetForLabel( labelId );
ArrayList<String> indexes = new ArrayList<>(); ArrayList<String> indexes = new ArrayList<>();
while ( indexDescriptorIterator.hasNext() ) while ( indexReferences.hasNext() )
{ {
IndexDescriptor index = indexDescriptorIterator.next(); CapableIndexReference index = indexReferences.next();
if ( index.type() == GENERAL ) if ( !index.isUnique() )
{ {
String[] propertyNames = PropertyNameUtils.getPropertyKeys( String[] propertyNames = PropertyNameUtils.getPropertyKeys(
tokenNameLookup, index.schema().getPropertyIds() ); tokenNameLookup, index.properties() );
indexes.add( String.join( ",", propertyNames ) ); indexes.add( String.join( ",", propertyNames ) );
} }
} }
properties.put( "indexes", indexes ); properties.put( "indexes", indexes );


Iterator<ConstraintDescriptor> nodePropertyConstraintIterator = Iterator<ConstraintDescriptor> nodePropertyConstraintIterator =
readOperations.constraintsGetForLabel( labelId ); schemaRead.constraintsGetForLabel( labelId );
ArrayList<String> constraints = new ArrayList<>(); ArrayList<String> constraints = new ArrayList<>();
while ( nodePropertyConstraintIterator.hasNext() ) while ( nodePropertyConstraintIterator.hasNext() )
{ {
Expand All @@ -120,7 +121,7 @@ public GraphResult buildSchemaGraph()
{ {
RelationshipType relationshipType = relationshipTypeIterator.next(); RelationshipType relationshipType = relationshipTypeIterator.next();
String relationshipTypeGetName = relationshipType.name(); String relationshipTypeGetName = relationshipType.name();
int relId = readOperations.relationshipTypeGetForName( relationshipTypeGetName ); int relId = tokenRead.relationshipType( relationshipTypeGetName );
try ( ResourceIterator<Label> labelsInUse = graphDatabaseAPI.getAllLabelsInUse().iterator() ) try ( ResourceIterator<Label> labelsInUse = graphDatabaseAPI.getAllLabelsInUse().iterator() )
{ {
List<VirtualNodeHack> startNodes = new LinkedList<>(); List<VirtualNodeHack> startNodes = new LinkedList<>();
Expand All @@ -132,13 +133,13 @@ public GraphResult buildSchemaGraph()
String labelName = labelToken.name(); String labelName = labelToken.name();
Map<String,Object> properties = new HashMap<>(); Map<String,Object> properties = new HashMap<>();
VirtualNodeHack node = getOrCreateLabel( labelName, properties, nodes ); VirtualNodeHack node = getOrCreateLabel( labelName, properties, nodes );
int labelId = readOperations.labelGetForName( labelName ); int labelId = tokenRead.nodeLabel( labelName );


if ( readOperations.countsForRelationship( labelId, relId, ReadOperations.ANY_LABEL ) > 0 ) if ( dataRead.countsForRelationship( labelId, relId, Read.ANY_LABEL ) > 0 )
{ {
startNodes.add( node ); startNodes.add( node );
} }
if ( readOperations.countsForRelationship( ReadOperations.ANY_LABEL, relId, labelId ) > 0 ) if ( dataRead.countsForRelationship( Read.ANY_LABEL, relId, labelId ) > 0 )
{ {
endNodes.add( node ); endNodes.add( node );
} }
Expand All @@ -148,7 +149,6 @@ public GraphResult buildSchemaGraph()
{ {
for ( VirtualNodeHack endNode : endNodes ) for ( VirtualNodeHack endNode : endNodes )
{ {
VirtualRelationshipHack relationship =
addRelationship( startNode, endNode, relationshipTypeGetName, relationships ); addRelationship( startNode, endNode, relationshipTypeGetName, relationships );
} }
} }
Expand Down Expand Up @@ -185,7 +185,7 @@ private VirtualNodeHack getOrCreateLabel( String label, Map<String,Object> prope
return node; return node;
} }


private VirtualRelationshipHack addRelationship( VirtualNodeHack startNode, VirtualNodeHack endNode, String relType, private void addRelationship( VirtualNodeHack startNode, VirtualNodeHack endNode, String relType,
final Map<String,Set<VirtualRelationshipHack>> relationshipMap ) final Map<String,Set<VirtualRelationshipHack>> relationshipMap )
{ {
Set<VirtualRelationshipHack> relationshipsForType; Set<VirtualRelationshipHack> relationshipsForType;
Expand All @@ -203,7 +203,6 @@ private VirtualRelationshipHack addRelationship( VirtualNodeHack startNode, Virt
{ {
relationshipsForType.add( relationship ); relationshipsForType.add( relationship );
} }
return relationship;
} }


private GraphResult getGraphResult( final Map<String,VirtualNodeHack> nodeMap, private GraphResult getGraphResult( final Map<String,VirtualNodeHack> nodeMap,
Expand Down

0 comments on commit 709c67e

Please sign in to comment.