Skip to content

Commit

Permalink
Remove almost all instances of statementInNewTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Mar 14, 2018
1 parent 97f5c1d commit c7cfa99
Show file tree
Hide file tree
Showing 11 changed files with 368 additions and 342 deletions.
Expand Up @@ -34,7 +34,7 @@
import org.neo4j.internal.kernel.api.Procedures;
import org.neo4j.internal.kernel.api.SchemaWrite;
import org.neo4j.internal.kernel.api.exceptions.ProcedureException;
import org.neo4j.kernel.api.Statement;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.schema.SchemaDescriptorFactory;
import org.neo4j.kernel.api.security.AnonymousContext;
import org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest;
Expand Down Expand Up @@ -73,13 +73,13 @@ public void testEmptyGraph() throws Throwable
public void testLabelIndex() throws Throwable
{
// Given there is label with index and a constraint
Statement statement = statementInNewTransaction( AnonymousContext.writeToken() );
long nodeId = statement.dataWriteOperations().nodeCreate();
int labelId = statement.tokenWriteOperations().labelGetOrCreateForName( "Person" );
statement.dataWriteOperations().nodeAddLabel( nodeId, labelId );
int propertyIdName = statement.tokenWriteOperations().propertyKeyGetOrCreateForName( "name" );
int propertyIdAge = statement.tokenWriteOperations().propertyKeyGetOrCreateForName( "age" );
statement.dataWriteOperations()
KernelTransaction transaction = newTransaction( AnonymousContext.writeToken() );
long nodeId = transaction.dataWrite().nodeCreate();
int labelId = transaction.tokenWrite().labelGetOrCreateForName( "Person" );
transaction.dataWrite().nodeAddLabel( nodeId, labelId );
int propertyIdName = transaction.tokenWrite().propertyKeyGetOrCreateForName( "name" );
int propertyIdAge = transaction.tokenWrite().propertyKeyGetOrCreateForName( "age" );
transaction.dataWrite()
.nodeSetProperty( nodeId, propertyIdName, Values.of( "Emil" ) );
commit();

Expand Down Expand Up @@ -113,15 +113,15 @@ public void testLabelIndex() throws Throwable
public void testRelationShip() throws Throwable
{
// Given there ar
Statement statement = statementInNewTransaction( AnonymousContext.writeToken() );
long nodeIdPerson = statement.dataWriteOperations().nodeCreate();
int labelIdPerson = statement.tokenWriteOperations().labelGetOrCreateForName( "Person" );
statement.dataWriteOperations().nodeAddLabel( nodeIdPerson, labelIdPerson );
long nodeIdLocation = statement.dataWriteOperations().nodeCreate();
int labelIdLocation = statement.tokenWriteOperations().labelGetOrCreateForName( "Location" );
statement.dataWriteOperations().nodeAddLabel( nodeIdLocation, labelIdLocation );
int relationshipTypeId = statement.tokenWriteOperations().relationshipTypeGetOrCreateForName( "LIVES_IN" );
statement.dataWriteOperations().relationshipCreate( relationshipTypeId, nodeIdPerson, nodeIdLocation );
KernelTransaction transaction = newTransaction( AnonymousContext.writeToken() );
long nodeIdPerson = transaction.dataWrite().nodeCreate();
int labelIdPerson = transaction.tokenWrite().labelGetOrCreateForName( "Person" );
transaction.dataWrite().nodeAddLabel( nodeIdPerson, labelIdPerson );
long nodeIdLocation = transaction.dataWrite().nodeCreate();
int labelIdLocation = transaction.tokenWrite().labelGetOrCreateForName( "Location" );
transaction.dataWrite().nodeAddLabel( nodeIdLocation, labelIdLocation );
int relationshipTypeId = transaction.tokenWrite().relationshipTypeGetOrCreateForName( "LIVES_IN" );
transaction.dataWrite().relationshipCreate( nodeIdPerson, relationshipTypeId, nodeIdLocation );
commit();

// When
Expand Down
Expand Up @@ -44,7 +44,6 @@
import org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException;
import org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.Statement;
import org.neo4j.kernel.api.index.PropertyAccessor;
import org.neo4j.kernel.api.schema.SchemaDescriptorFactory;
import org.neo4j.kernel.api.schema.constaints.IndexBackedConstraintDescriptor;
Expand Down Expand Up @@ -264,11 +263,11 @@ public void shouldFailToCreateIndexWhereAConstraintAlreadyExists() throws Except
public void shouldListConstraintIndexesInTheBeansAPI() throws Exception
{
// given
Statement statement = statementInNewTransaction( AUTH_DISABLED );
statement.schemaWriteOperations().uniquePropertyConstraintCreate(
KernelTransaction transaction = newTransaction( AUTH_DISABLED );
transaction.schemaWrite().uniquePropertyConstraintCreate(
SchemaDescriptorFactory.forLabel(
statement.tokenWriteOperations().labelGetOrCreateForName( "Label1" ),
statement.tokenWriteOperations().propertyKeyGetOrCreateForName( "property1" )
transaction.tokenWrite().labelGetOrCreateForName( "Label1" ),
transaction.tokenWrite().propertyKeyGetOrCreateForName( "property1" )
) );
commit();

Expand Down
Expand Up @@ -33,8 +33,8 @@
import org.neo4j.helpers.collection.MapUtil;
import org.neo4j.internal.kernel.api.TokenWrite;
import org.neo4j.internal.kernel.api.exceptions.ProcedureException;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.ResourceTracker;
import org.neo4j.kernel.api.Statement;
import org.neo4j.kernel.api.StubResourceManager;
import org.neo4j.kernel.api.security.AnonymousContext;
import org.neo4j.kernel.impl.api.index.inmemory.InMemoryIndexProviderFactory;
Expand Down Expand Up @@ -63,10 +63,10 @@ public class BuiltInProceduresIT extends KernelIntegrationTest
public void listAllLabels() throws Throwable
{
// Given
Statement statement = statementInNewTransaction( AnonymousContext.writeToken() );
long nodeId = statement.dataWriteOperations().nodeCreate();
int labelId = statement.tokenWriteOperations().labelGetOrCreateForName( "MyLabel" );
statement.dataWriteOperations().nodeAddLabel( nodeId, labelId );
KernelTransaction transaction = newTransaction( AnonymousContext.writeToken() );
long nodeId = transaction.dataWrite().nodeCreate();
int labelId = transaction.tokenWrite().labelGetOrCreateForName( "MyLabel" );
transaction.dataWrite().nodeAddLabel( nodeId, labelId );
commit();

// When
Expand Down Expand Up @@ -98,11 +98,11 @@ public void listPropertyKeys() throws Throwable
public void listRelationshipTypes() throws Throwable
{
// Given
Statement statement = statementInNewTransaction( AnonymousContext.writeToken() );
int relType = statement.tokenWriteOperations().relationshipTypeGetOrCreateForName( "MyRelType" );
long startNodeId = statement.dataWriteOperations().nodeCreate();
long endNodeId = statement.dataWriteOperations().nodeCreate();
statement.dataWriteOperations().relationshipCreate( relType, startNodeId, endNodeId );
KernelTransaction transaction = newTransaction( AnonymousContext.writeToken() );
int relType = transaction.tokenWrite().relationshipTypeGetOrCreateForName( "MyRelType" );
long startNodeId = transaction.dataWrite().nodeCreate();
long endNodeId = transaction.dataWrite().nodeCreate();
transaction.dataWrite().relationshipCreate( startNodeId, relType, endNodeId );
commit();

// When
Expand Down Expand Up @@ -304,15 +304,15 @@ public void listAllComponents() throws Throwable
public void listAllIndexes() throws Throwable
{
// Given
Statement statement = statementInNewTransaction( AUTH_DISABLED );
int labelId1 = statement.tokenWriteOperations().labelGetOrCreateForName( "Person" );
int labelId2 = statement.tokenWriteOperations().labelGetOrCreateForName( "Age" );
int propertyKeyId1 = statement.tokenWriteOperations().propertyKeyGetOrCreateForName( "foo" );
int propertyKeyId2 = statement.tokenWriteOperations().propertyKeyGetOrCreateForName( "bar" );
KernelTransaction transaction = newTransaction( AUTH_DISABLED );
int labelId1 = transaction.tokenWrite().labelGetOrCreateForName( "Person" );
int labelId2 = transaction.tokenWrite().labelGetOrCreateForName( "Age" );
int propertyKeyId1 = transaction.tokenWrite().propertyKeyGetOrCreateForName( "foo" );
int propertyKeyId2 = transaction.tokenWrite().propertyKeyGetOrCreateForName( "bar" );
//TODO: Add test support for composite indexes
statement.schemaWriteOperations().indexCreate( forLabel( labelId1, propertyKeyId1 ) );
statement.schemaWriteOperations().uniquePropertyConstraintCreate( forLabel( labelId2, propertyKeyId1 ) );
statement.schemaWriteOperations().indexCreate( forLabel( labelId1, propertyKeyId1, propertyKeyId2 ) );
transaction.schemaWrite().indexCreate( forLabel( labelId1, propertyKeyId1 ) );
transaction.schemaWrite().uniquePropertyConstraintCreate( forLabel( labelId2, propertyKeyId1 ) );
transaction.schemaWrite().indexCreate( forLabel( labelId1, propertyKeyId1, propertyKeyId2 ) );
commit();

//let indexes come online
Expand Down
Expand Up @@ -124,7 +124,7 @@ protected KernelTransaction newTransaction() throws TransactionFailureException
return transaction;
}

protected KernelTransaction newTransaction(LoginContext loginContext) throws TransactionFailureException
protected KernelTransaction newTransaction( LoginContext loginContext ) throws TransactionFailureException
{
transaction = kernel.newTransaction( KernelTransaction.Type.implicit, loginContext );
return transaction;
Expand Down Expand Up @@ -227,7 +227,7 @@ protected void restartDb() throws TransactionFailureException

boolean nodeHasLabel( KernelTransaction transaction, long node, int label )
{
try( NodeCursor cursor = transaction.cursors().allocateNodeCursor() )
try ( NodeCursor cursor = transaction.cursors().allocateNodeCursor() )
{
transaction.dataRead().singleNode( node, cursor );
return cursor.next() && cursor.labels().contains( label );
Expand Down Expand Up @@ -357,4 +357,18 @@ Iterator<Long> nodeGetRelationships( KernelTransaction transaction, long node, D
throw new IllegalStateException( direction + " is not a valid direction" );
}
}

int countNodes( KernelTransaction transaction )
{
int result = 0;
try ( NodeCursor cursor = transaction.cursors().allocateNodeCursor() )
{
transaction.dataRead().allNodesScan( cursor );
while ( cursor.next() )
{
result++;
}
}
return result;
}
}
Expand Up @@ -26,9 +26,7 @@
import org.neo4j.internal.kernel.api.NamedToken;
import org.neo4j.internal.kernel.api.Write;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.Statement;
import org.neo4j.kernel.api.security.AnonymousContext;
import org.neo4j.storageengine.api.Token;

import static org.hamcrest.core.IsCollectionContaining.hasItems;
import static org.junit.Assert.assertThat;
Expand All @@ -41,21 +39,21 @@ public class LabelIT extends KernelIntegrationTest
public void shouldListAllLabels() throws Exception
{
// given
Statement statement = statementInNewTransaction( AnonymousContext.writeToken() );
int label1Id = statement.tokenWriteOperations().labelGetOrCreateForName( "label1" );
int label2Id = statement.tokenWriteOperations().labelGetOrCreateForName( "label2" );
KernelTransaction transaction = newTransaction( AnonymousContext.writeToken() );
int label1Id = transaction.tokenWrite().labelGetOrCreateForName( "label1" );
int label2Id = transaction.tokenWrite().labelGetOrCreateForName( "label2" );

// when
Iterator<Token> labelIdsBeforeCommit = statement.readOperations().labelsGetAllTokens();
Iterator<NamedToken> labelIdsBeforeCommit = transaction.tokenRead().labelsGetAllTokens();

// then
assertThat( asCollection( labelIdsBeforeCommit ),
hasItems( new Token( "label1", label1Id ), new Token( "label2", label2Id ) ) );
hasItems( new NamedToken( "label1", label1Id ), new NamedToken( "label2", label2Id ) ) );

// when
commit();

KernelTransaction transaction = newTransaction();
transaction = newTransaction();
Iterator<NamedToken> labelIdsAfterCommit = transaction.tokenRead().labelsGetAllTokens();

// then
Expand All @@ -68,10 +66,10 @@ public void shouldListAllLabels() throws Exception
public void addingAndRemovingLabelInSameTxShouldHaveNoEffect() throws Exception
{
// Given a node with a label
Statement statement = statementInNewTransaction( AnonymousContext.writeToken() );
int label = statement.tokenWriteOperations().labelGetOrCreateForName( "Label 1" );
long node = statement.dataWriteOperations().nodeCreate();
statement.dataWriteOperations().nodeAddLabel( node, label );
KernelTransaction transaction = newTransaction( AnonymousContext.writeToken() );
int label = transaction.tokenWrite().labelGetOrCreateForName( "Label 1" );
long node = transaction.dataWrite().nodeCreate();
transaction.dataWrite().nodeAddLabel( node, label );
commit();

// When I add and remove that label in the same tx
Expand Down

0 comments on commit c7cfa99

Please sign in to comment.