Skip to content

Commit

Permalink
Remove almost all instances of readOperationsInNewTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Mar 14, 2018
1 parent b95cf23 commit 97f5c1d
Show file tree
Hide file tree
Showing 11 changed files with 335 additions and 147 deletions.
Expand Up @@ -44,7 +44,6 @@
import org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException; import org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException;
import org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor; import org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor;
import org.neo4j.kernel.api.KernelTransaction; import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.ReadOperations;
import org.neo4j.kernel.api.Statement; import org.neo4j.kernel.api.Statement;
import org.neo4j.kernel.api.index.PropertyAccessor; import org.neo4j.kernel.api.index.PropertyAccessor;
import org.neo4j.kernel.api.schema.SchemaDescriptorFactory; import org.neo4j.kernel.api.schema.SchemaDescriptorFactory;
Expand Down Expand Up @@ -172,8 +171,8 @@ public void rollBackIndexRuleShouldNotBeCommitted() throws Exception
rollback(); rollback();


// THEN // THEN
ReadOperations readOperations = readOperationsInNewTransaction(); KernelTransaction transaction = newTransaction();
assertEquals( emptySet(), asSet( readOperations.indexesGetForLabel( labelId ) ) ); assertEquals( emptySet(), asSet( transaction.schemaRead().indexesGetForLabel( labelId ) ) );
commit(); commit();
} }


Expand All @@ -186,8 +185,8 @@ public void shouldBeAbleToRemoveAConstraintIndexWithoutOwner() throws Exception


SchemaIndexDescriptor constraintIndex = creator.createConstraintIndex( descriptor ); SchemaIndexDescriptor constraintIndex = creator.createConstraintIndex( descriptor );
// then // then
ReadOperations readOperations = readOperationsInNewTransaction(); KernelTransaction transaction = newTransaction();
assertEquals( emptySet(), asSet( readOperations.constraintsGetForLabel( labelId ) ) ); assertEquals( emptySet(), asSet( transaction.schemaRead().constraintsGetForLabel( labelId ) ) );
commit(); commit();


// when // when
Expand All @@ -196,8 +195,8 @@ public void shouldBeAbleToRemoveAConstraintIndexWithoutOwner() throws Exception
commit(); commit();


// then // then
readOperations = readOperationsInNewTransaction(); transaction = newTransaction();
assertEquals( emptySet(), asSet( readOperations.indexesGetForLabel( labelId ) ) ); assertEquals( emptySet(), asSet( transaction.schemaRead().indexesGetForLabel( labelId ) ) );
commit(); commit();
} }


Expand Down
Expand Up @@ -24,9 +24,18 @@
import org.junit.Rule; import org.junit.Rule;
import org.junit.rules.RuleChain; import org.junit.rules.RuleChain;


import java.util.Iterator;

import org.neo4j.collection.primitive.Primitive;
import org.neo4j.collection.primitive.PrimitiveIntIterator;
import org.neo4j.collection.primitive.PrimitiveIntSet;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.factory.GraphDatabaseBuilder; import org.neo4j.graphdb.factory.GraphDatabaseBuilder;
import org.neo4j.internal.kernel.api.NodeCursor;
import org.neo4j.internal.kernel.api.Procedures; import org.neo4j.internal.kernel.api.Procedures;
import org.neo4j.internal.kernel.api.PropertyCursor;
import org.neo4j.internal.kernel.api.RelationshipScanCursor;
import org.neo4j.internal.kernel.api.SchemaWrite; import org.neo4j.internal.kernel.api.SchemaWrite;
import org.neo4j.internal.kernel.api.TokenWrite; import org.neo4j.internal.kernel.api.TokenWrite;
import org.neo4j.internal.kernel.api.Write; import org.neo4j.internal.kernel.api.Write;
Expand All @@ -45,8 +54,14 @@
import org.neo4j.test.TestGraphDatabaseFactory; import org.neo4j.test.TestGraphDatabaseFactory;
import org.neo4j.test.rule.TestDirectory; import org.neo4j.test.rule.TestDirectory;
import org.neo4j.test.rule.fs.DefaultFileSystemRule; import org.neo4j.test.rule.fs.DefaultFileSystemRule;
import org.neo4j.values.storable.Value;


import static java.util.Collections.emptyIterator;
import static org.neo4j.internal.kernel.api.helpers.RelationshipSelections.allIterator;
import static org.neo4j.internal.kernel.api.helpers.RelationshipSelections.incomingIterator;
import static org.neo4j.internal.kernel.api.helpers.RelationshipSelections.outgoingIterator;
import static org.neo4j.internal.kernel.api.security.LoginContext.AUTH_DISABLED; import static org.neo4j.internal.kernel.api.security.LoginContext.AUTH_DISABLED;
import static org.neo4j.values.storable.Values.NO_VALUE;


public abstract class KernelIntegrationTest public abstract class KernelIntegrationTest
{ {
Expand Down Expand Up @@ -209,4 +224,137 @@ protected void restartDb() throws TransactionFailureException
stopDb(); stopDb();
startDb(); startDb();
} }

boolean nodeHasLabel( KernelTransaction transaction, long node, int label )
{
try( NodeCursor cursor = transaction.cursors().allocateNodeCursor() )
{
transaction.dataRead().singleNode( node, cursor );
return cursor.next() && cursor.labels().contains( label );
}
}

boolean nodeHasProperty( KernelTransaction transaction, long node, int property )
{
try ( NodeCursor cursor = transaction.cursors().allocateNodeCursor();
PropertyCursor properties = transaction.cursors().allocatePropertyCursor() )
{
transaction.dataRead().singleNode( node, cursor );
if ( !cursor.next() )
{
return false;
}
else
{
cursor.properties( properties );
while ( properties.next() )
{
if ( properties.propertyKey() == property )
{
return true;
}
}
return false;
}
}
}

Value nodeGetProperty( KernelTransaction transaction, long node, int property )
{
try ( NodeCursor cursor = transaction.cursors().allocateNodeCursor();
PropertyCursor properties = transaction.cursors().allocatePropertyCursor() )
{
transaction.dataRead().singleNode( node, cursor );
if ( !cursor.next() )
{
return NO_VALUE;
}
else
{
cursor.properties( properties );
while ( properties.next() )
{
if ( properties.propertyKey() == property )
{
return properties.propertyValue();
}
}
return NO_VALUE;
}
}
}

PrimitiveIntIterator nodeGetPropertyKeys( KernelTransaction transaction, long node )
{
try ( NodeCursor cursor = transaction.cursors().allocateNodeCursor();
PropertyCursor properties = transaction.cursors().allocatePropertyCursor() )
{
PrimitiveIntSet props = Primitive.intSet();
transaction.dataRead().singleNode( node, cursor );
if ( cursor.next() )
{
cursor.properties( properties );
while ( properties.next() )
{
props.add( properties.propertyKey() );
}
}
return props.iterator();
}
}

Value relationshipGetProperty( KernelTransaction transaction, long relationship, int property )
{
try ( RelationshipScanCursor cursor = transaction.cursors().allocateRelationshipScanCursor();
PropertyCursor properties = transaction.cursors().allocatePropertyCursor() )
{
transaction.dataRead().singleRelationship( relationship, cursor );
if ( !cursor.next() )
{
return NO_VALUE;
}
else
{
cursor.properties( properties );
while ( properties.next() )
{
if ( properties.propertyKey() == property )
{
return properties.propertyValue();
}
}
return NO_VALUE;
}
}
}

Iterator<Long> nodeGetRelationships( KernelTransaction transaction, long node, Direction direction )
{
return nodeGetRelationships( transaction, node, direction, null );
}

Iterator<Long> nodeGetRelationships( KernelTransaction transaction, long node, Direction direction, int[] types )
{
NodeCursor cursor = transaction.cursors().allocateNodeCursor();
transaction.dataRead().singleNode( node, cursor );
if ( !cursor.next() )
{
return emptyIterator();
}

switch ( direction )
{
case OUTGOING:
return outgoingIterator( transaction.cursors(), cursor, types,
( id, startNodeId, typeId, endNodeId ) -> id );
case INCOMING:
return incomingIterator( transaction.cursors(), cursor, types,
( id, startNodeId, typeId, endNodeId ) -> id );
case BOTH:
return allIterator( transaction.cursors(), cursor, types,
( id, startNodeId, typeId, endNodeId ) -> id );
default:
throw new IllegalStateException( direction + " is not a valid direction" );
}
}
} }
Expand Up @@ -23,8 +23,9 @@


import java.util.Iterator; import java.util.Iterator;


import org.neo4j.internal.kernel.api.NamedToken;
import org.neo4j.internal.kernel.api.Write; import org.neo4j.internal.kernel.api.Write;
import org.neo4j.kernel.api.ReadOperations; import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.Statement; import org.neo4j.kernel.api.Statement;
import org.neo4j.kernel.api.security.AnonymousContext; import org.neo4j.kernel.api.security.AnonymousContext;
import org.neo4j.storageengine.api.Token; import org.neo4j.storageengine.api.Token;
Expand Down Expand Up @@ -54,12 +55,12 @@ public void shouldListAllLabels() throws Exception
// when // when
commit(); commit();


ReadOperations readOperations = readOperationsInNewTransaction(); KernelTransaction transaction = newTransaction();
Iterator<Token> labelIdsAfterCommit = readOperations.labelsGetAllTokens(); Iterator<NamedToken> labelIdsAfterCommit = transaction.tokenRead().labelsGetAllTokens();


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


Expand All @@ -82,7 +83,8 @@ public void addingAndRemovingLabelInSameTxShouldHaveNoEffect() throws Exception
commit(); commit();


// And then the node should have the label // And then the node should have the label
assertTrue( readOperationsInNewTransaction().nodeHasLabel( node, label ) );
assertTrue( nodeHasLabel( newTransaction(), node, label ) );
commit(); commit();
} }
} }
Expand Up @@ -95,7 +95,7 @@ public void shouldGetAllProcedures() throws Throwable


// When // When
List<ProcedureSignature> signatures = List<ProcedureSignature> signatures =
Iterables.asList( readOperationsInNewTransaction().proceduresGetAll() ); Iterables.asList( newTransaction().procedures().proceduresGetAll() );


// Then // Then
assertThat( signatures, hasItems( assertThat( signatures, hasItems(
Expand Down

0 comments on commit 97f5c1d

Please sign in to comment.