Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Procedures now close acquired KernelStatements #9764

Merged
merged 3 commits into from Aug 16, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -37,8 +37,6 @@
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException;
import org.neo4j.kernel.api.index.IndexDescriptor;
import org.neo4j.kernel.api.proc.ProcedureSignature;
import org.neo4j.kernel.api.proc.UserFunctionSignature;
import org.neo4j.kernel.impl.api.TokenAccess;
import org.neo4j.kernel.impl.api.index.IndexingService;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
Expand Down Expand Up @@ -67,22 +65,30 @@ public class BuiltInProcedures
@Procedure( name = "db.labels", mode = READ )
public Stream<LabelResult> listLabels()
{
return TokenAccess.LABELS.inUse( tx.acquireStatement() ).map( LabelResult::new ).stream();
try ( Statement statement = tx.acquireStatement() )
{
return TokenAccess.LABELS.inUse( statement ).map( LabelResult::new ).stream();
}
}

@Description( "List all property keys in the database." )
@Procedure( name = "db.propertyKeys", mode = READ )
public Stream<PropertyKeyResult> listPropertyKeys()
{
return TokenAccess.PROPERTY_KEYS.inUse( tx.acquireStatement() ).map( PropertyKeyResult::new ).stream();
try ( Statement statement = tx.acquireStatement() )
{
return TokenAccess.PROPERTY_KEYS.inUse( statement ).map( PropertyKeyResult::new ).stream();
}
}

@Description( "List all relationship types in the database." )
@Procedure( name = "db.relationshipTypes", mode = READ )
public Stream<RelationshipTypeResult> listRelationshipTypes()
{
return TokenAccess.RELATIONSHIP_TYPES.inUse( tx.acquireStatement() )
.map( RelationshipTypeResult::new ).stream();
try ( Statement statement = tx.acquireStatement() )
{
return TokenAccess.RELATIONSHIP_TYPES.inUse( statement ).map( RelationshipTypeResult::new ).stream();
}
}

@Description( "List all indexes in the database." )
Expand Down Expand Up @@ -180,16 +186,17 @@ public Stream<SchemaProcedure.GraphResult> metaGraph() throws ProcedureException
@Procedure( name = "db.constraints", mode = READ )
public Stream<ConstraintResult> listConstraints()
{
Statement statement = tx.acquireStatement();
ReadOperations operations = statement.readOperations();
TokenNameLookup tokens = new StatementTokenNameLookup( operations );

return asList( operations.constraintsGetAll() )
.stream()
.map( ( constraint ) -> constraint.userDescription( tokens ) )
.sorted()
.map( ConstraintResult::new )
.onClose( statement::close );
try ( Statement statement = tx.acquireStatement() )
{
ReadOperations operations = statement.readOperations();
TokenNameLookup tokens = new StatementTokenNameLookup( operations );

return asList( operations.constraintsGetAll() )
.stream()
.map( ( constraint ) -> constraint.userDescription( tokens ) )
.sorted()
.map( ConstraintResult::new );
}
}

private IndexProcedures indexProcedures()
Expand Down
Expand Up @@ -40,6 +40,7 @@
import org.neo4j.graphdb.Transaction;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.ReadOperations;
import org.neo4j.kernel.api.Statement;
import org.neo4j.kernel.api.StatementTokenNameLookup;
import org.neo4j.kernel.api.constraints.NodePropertyConstraint;
import org.neo4j.kernel.api.index.IndexDescriptor;
Expand All @@ -62,82 +63,90 @@ public GraphResult buildSchemaGraph()
final Map<String,NodeImpl> nodes = new HashMap<>();
final Map<String,Set<RelationshipImpl>> relationships = new HashMap<>();

ReadOperations readOperations = kernelTransaction.acquireStatement().readOperations();
StatementTokenNameLookup statementTokenNameLookup = new StatementTokenNameLookup( readOperations );

try ( Transaction transaction = graphDatabaseAPI.beginTx(); )
try ( Statement statement = kernelTransaction.acquireStatement() )
{
// add all labelsInDatabase
ResourceIterator<Label> labelsInDatabase = graphDatabaseAPI.getAllLabelsInUse().iterator();
while ( labelsInDatabase.hasNext() )
{
Label label = labelsInDatabase.next();
Map<String,Object> properties = new HashMap<>();

Iterator<IndexDescriptor> indexDescriptorIterator =
readOperations.indexesGetForLabel( readOperations.labelGetForName( label.name() ) );
ArrayList<String> indexes = new ArrayList<>();
while ( indexDescriptorIterator.hasNext() )
{
indexes.add( statementTokenNameLookup
.propertyKeyGetName( indexDescriptorIterator.next().getPropertyKeyId() ) );
}
properties.put( "indexes", indexes );

Iterator<NodePropertyConstraint> nodePropertyConstraintIterator =
readOperations.constraintsGetForLabel( readOperations.labelGetForName( label.name() ) );
ArrayList<String> constraints = new ArrayList<>();
while ( nodePropertyConstraintIterator.hasNext() )
{
constraints
.add( nodePropertyConstraintIterator.next().userDescription( statementTokenNameLookup ) );
}
properties.put( "constraints", constraints );

getOrCreateLabel( label.name(), properties, nodes );
}

//add all relationships
ReadOperations readOperations = statement.readOperations();
StatementTokenNameLookup statementTokenNameLookup = new StatementTokenNameLookup( readOperations );

Iterator<RelationshipType> relationshipTypeIterator =
graphDatabaseAPI.getAllRelationshipTypesInUse().iterator();
while ( relationshipTypeIterator.hasNext() )
try ( Transaction transaction = graphDatabaseAPI.beginTx(); )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this inner transaction? We are already in a transaction, so this will be a dummy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would guess not, but cleaning this up is outside the scope of this PR.

{
RelationshipType relationshipType = relationshipTypeIterator.next();
String relationshipTypeGetName = relationshipType.name();
int relId = readOperations.relationshipTypeGetForName( relationshipTypeGetName );
ResourceIterator<Label> labelsInUse = graphDatabaseAPI.getAllLabelsInUse().iterator();

List<NodeImpl> startNodes = new LinkedList<>();
List<NodeImpl> endNodes = new LinkedList<>();

while ( labelsInUse.hasNext() )
// add all labelsInDatabase
try ( ResourceIterator<Label> labelsInDatabase = graphDatabaseAPI.getAllLabelsInUse().iterator() )
{
Label labelToken = labelsInUse.next();
String labelName = labelToken.name();
Map<String,Object> properties = new HashMap<>();
NodeImpl node = getOrCreateLabel( labelName, properties, nodes );
int labelId = readOperations.labelGetForName( labelName );

if ( readOperations.countsForRelationship( labelId, relId, ReadOperations.ANY_LABEL ) > 0 )
while ( labelsInDatabase.hasNext() )
{
startNodes.add( node );
}
if ( readOperations.countsForRelationship( ReadOperations.ANY_LABEL, relId, labelId ) > 0 )
{
endNodes.add( node );
Label label = labelsInDatabase.next();
Map<String,Object> properties = new HashMap<>();

Iterator<IndexDescriptor> indexDescriptorIterator =
readOperations.indexesGetForLabel( readOperations.labelGetForName( label.name() ) );
ArrayList<String> indexes = new ArrayList<>();
while ( indexDescriptorIterator.hasNext() )
{
indexes.add( statementTokenNameLookup
.propertyKeyGetName( indexDescriptorIterator.next().getPropertyKeyId() ) );
}
properties.put( "indexes", indexes );

Iterator<NodePropertyConstraint> nodePropertyConstraintIterator =
readOperations.constraintsGetForLabel( readOperations.labelGetForName( label.name() ) );
ArrayList<String> constraints = new ArrayList<>();
while ( nodePropertyConstraintIterator.hasNext() )
{
constraints
.add( nodePropertyConstraintIterator.next().userDescription( statementTokenNameLookup ) );
}
properties.put( "constraints", constraints );

getOrCreateLabel( label.name(), properties, nodes );
}
}
for ( NodeImpl startNode : startNodes )

//add all relationships

try ( ResourceIterator<RelationshipType> relationshipTypeIterator = graphDatabaseAPI.getAllRelationshipTypesInUse()
.iterator() )
{
for ( NodeImpl endNode : endNodes )
while ( relationshipTypeIterator.hasNext() )
{
RelationshipImpl relationship = addRelationship( startNode, endNode, relationshipTypeGetName, relationships );
RelationshipType relationshipType = relationshipTypeIterator.next();
String relationshipTypeGetName = relationshipType.name();
int relId = readOperations.relationshipTypeGetForName( relationshipTypeGetName );
ResourceIterator<Label> labelsInUse = graphDatabaseAPI.getAllLabelsInUse().iterator();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this resource iterator should be closed as well


List<NodeImpl> startNodes = new LinkedList<>();
List<NodeImpl> endNodes = new LinkedList<>();

while ( labelsInUse.hasNext() )
{
Label labelToken = labelsInUse.next();
String labelName = labelToken.name();
Map<String,Object> properties = new HashMap<>();
NodeImpl node = getOrCreateLabel( labelName, properties, nodes );
int labelId = readOperations.labelGetForName( labelName );

if ( readOperations.countsForRelationship( labelId, relId, ReadOperations.ANY_LABEL ) > 0 )
{
startNodes.add( node );
}
if ( readOperations.countsForRelationship( ReadOperations.ANY_LABEL, relId, labelId ) > 0 )
{
endNodes.add( node );
}
}
for ( NodeImpl startNode : startNodes )
{
for ( NodeImpl endNode : endNodes )
{
RelationshipImpl relationship =
addRelationship( startNode, endNode, relationshipTypeGetName, relationships );
}
}
}
}
transaction.success();
return getGraphResult( nodes, relationships );
}
transaction.success();
return getGraphResult( nodes, relationships );
}
}

Expand Down
Expand Up @@ -20,6 +20,7 @@
package org.neo4j.kernel.builtinprocs;

import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.Statement;
import org.neo4j.kernel.api.exceptions.schema.IllegalTokenNameException;
import org.neo4j.kernel.api.exceptions.schema.TooManyLabelsException;
import org.neo4j.procedure.Context;
Expand All @@ -39,21 +40,30 @@ public class TokenProcedures
public void createLabel( @Name( "newLabel" ) String newLabel )
throws IllegalTokenNameException, TooManyLabelsException
{
tx.acquireStatement().tokenWriteOperations().labelGetOrCreateForName( newLabel );
try ( Statement statement = tx.acquireStatement() )
{
statement.tokenWriteOperations().labelGetOrCreateForName( newLabel );
}
}

@Description( "Create a RelationshipType" )
@Procedure( name = "db.createRelationshipType", mode = WRITE )
public void createRelationshipType( @Name( "newRelationshipType" ) String newRelationshipType )
throws IllegalTokenNameException {
tx.acquireStatement().tokenWriteOperations().relationshipTypeGetOrCreateForName( newRelationshipType );
try ( Statement statement = tx.acquireStatement() )
{
statement.tokenWriteOperations().relationshipTypeGetOrCreateForName( newRelationshipType );
}
}

@Description( "Create a Property" )
@Procedure( name = "db.createProperty", mode = WRITE )
public void createProperty( @Name( "newProperty" ) String newProperty ) throws IllegalTokenNameException
{
tx.acquireStatement().tokenWriteOperations().propertyKeyGetOrCreateForName( newProperty );
try ( Statement statement = tx.acquireStatement() )
{
statement.tokenWriteOperations().propertyKeyGetOrCreateForName( newProperty );
}
}

}
Expand Up @@ -643,7 +643,7 @@ public String getUserMessage( KernelException e )
@Override
public void assertInOpenTransaction()
{
ctxSupplier.get();
ctxSupplier.get().close();
}
}
}
Expand Up @@ -131,7 +131,6 @@ public void testRelationShip() throws Throwable
contains( equalTo( Label.label( "Person" ) ) ) );
assertThat( relationships.get( 0 ).getEndNode().getLabels(),
contains( equalTo( Label.label( "Location" ) ) ) );

}
}
}
Expand Up @@ -40,6 +40,7 @@
import org.neo4j.kernel.api.ExecutingQuery;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.KernelTransactionHandle;
import org.neo4j.kernel.api.Statement;
import org.neo4j.kernel.api.bolt.BoltConnectionTracker;
import org.neo4j.kernel.api.bolt.ManagedBoltStateMachine;
import org.neo4j.kernel.api.exceptions.InvalidArgumentsException;
Expand Down Expand Up @@ -105,7 +106,10 @@ public void setTXMetaData( @Name( value = "data" ) Map<String,Object> data )
"keys and values to be less than %d, got %d", HARD_CHAR_LIMIT, totalCharSize ) );
}

getCurrentTx().acquireStatement().queryRegistration().setMetaData( data );
try ( Statement statement = getCurrentTx().acquireStatement() )
{
statement.queryRegistration().setMetaData( data );
}
}

private KernelTransaction getCurrentTx()
Expand Down