private void relateNTimes( int nRelationshipsInStore, int type, long n1, long n2, Transaction tx ) throws KernelException { for ( int i = 0; i < nRelationshipsInStore; i++ ) { tx.dataWrite().relationshipCreate( n1, type, n2 ); } }
private void createRelationship( RelationshipDirection direction, long start, int type, Write write ) throws EntityNotFoundException { switch ( direction ) { case OUT: write.relationshipCreate( start, type, write.nodeCreate() ); break; case IN: write.relationshipCreate( write.nodeCreate(), type, start ); break; case LOOP: write.relationshipCreate( start, type, start ); break; default: throw new IllegalStateException( "Checkstyle, you win again!" ); } } }
private void createRelationship( long startNode, String type, long endNode, List<String> propKeys, List<Value> propValues ) throws Throwable { assert type != null && !type.equals( "" ); assert propKeys.size() == propValues.size(); Transaction transaction = newTransaction( AnonymousContext.writeToken() ); int typeId = transaction.tokenWrite().relationshipTypeGetOrCreateForName( type ); long relId = transaction.dataWrite().relationshipCreate( startNode, typeId, endNode ); for ( int i = 0; i < propKeys.size(); i++ ) { String propKeyName = propKeys.get( i ); Value propValue = propValues.get( i ); int propKeyId = transaction.tokenWrite().propertyKeyGetOrCreateForName( propKeyName ); transaction.dataWrite().relationshipSetProperty( relId, propKeyId, propValue ); } commit(); }
@Test public void listRelationshipTypes() throws Throwable { // Given Transaction 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 RawIterator<Object[],ProcedureException> stream = procs().procedureCallRead( procs().procedureGet( procedureName( "db", "relationshipTypes" ) ).id(), new Object[0] ); // Then assertThat( asList( stream ), contains( equalTo( new Object[]{"MyRelType"} ) ) ); }
@Test public void shouldCreateRelationshipBetweenInTransactionNodes() throws Exception { long n1, n2, r; try ( Transaction tx = beginTransaction() ) { n1 = tx.dataWrite().nodeCreate(); n2 = tx.dataWrite().nodeCreate(); int label = tx.token().relationshipTypeGetOrCreateForName( "R" ); r = tx.dataWrite().relationshipCreate( n1, label, n2 ); tx.success(); } try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { List<Relationship> relationships = Iterables.asList( graphDb.getNodeById( n1 ).getRelationships() ); assertEquals( 1, relationships.size() ); assertEquals( relationships.get( 0 ).getId(), r ); } }
@Test public void shouldCountNewRelationships() throws Exception { int relationship; try ( Transaction tx = beginTransaction() ) { Write write = tx.dataWrite(); relationship = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ); write.relationshipCreate( write.nodeCreate(), relationship, write.nodeCreate() ); tx.success(); } try ( Transaction tx = beginTransaction() ) { Write write = tx.dataWrite(); write.relationshipCreate( write.nodeCreate(), relationship, write.nodeCreate() ); long countsTxState = tx.dataRead().countsForRelationship( -1, relationship, -1 ); long countsNoTxState = tx.dataRead().countsForRelationshipWithoutTxState( -1, relationship, -1 ); assertEquals( 2, countsTxState ); assertEquals( 1, countsNoTxState ); } }
@Test public void shouldFailToCreateRelationshipPropertyExistenceConstraintIfConstraintNotSatisfied() throws Exception { //Given try ( Transaction transaction = beginTransaction() ) { Write write = transaction.dataWrite(); write.relationshipCreate( write.nodeCreate(), type, write.nodeCreate() ); transaction.success(); } //Expect exception.expect( SchemaKernelException.class ); //When try ( Transaction transaction = beginTransaction() ) { transaction.schemaWrite().relationshipPropertyExistenceConstraintCreate( typeDescriptor( type, prop1 ) ); } }
@Test public void shouldCreateRelationship() throws Exception { long n1, n2; try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() ) { n1 = graphDb.createNode().getId(); n2 = graphDb.createNode().getId(); tx.success(); } long r; try ( Transaction tx = beginTransaction() ) { int label = tx.token().relationshipTypeGetOrCreateForName( "R" ); r = tx.dataWrite().relationshipCreate( n1, label, n2 ); tx.success(); } try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { List<Relationship> relationships = Iterables.asList( graphDb.getNodeById( n1 ).getRelationships() ); assertEquals( 1, relationships.size() ); assertEquals( relationships.get( 0 ).getId(), r ); } }
@Test public void shouldReturnRelsWhenAskingForRelsWhereOnlySomeTypesExistInCurrentRel() throws Exception { Transaction transaction = newTransaction( AnonymousContext.writeToken() ); int relType1 = transaction.tokenWrite().relationshipTypeGetOrCreateForName( "Type1" ); int relType2 = transaction.tokenWrite().relationshipTypeGetOrCreateForName( "Type2" ); long refNode = transaction.dataWrite().nodeCreate(); long otherNode = transaction.dataWrite().nodeCreate(); long theRel = transaction.dataWrite().relationshipCreate( refNode, relType1, otherNode ); assertRels( nodeGetRelationships( transaction, refNode, OUTGOING, new int[]{relType2,relType1} ), theRel ); commit(); }
@Test public void shouldNotSeeSingleRelationshipWhichWasDeletedInTransaction() throws Exception { int label; long n1, n2, r; try ( Transaction tx = beginTransaction() ) { n1 = tx.dataWrite().nodeCreate(); n2 = tx.dataWrite().nodeCreate(); label = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ); long decoyNode = tx.dataWrite().nodeCreate(); tx.dataWrite().relationshipCreate( n2, label, decoyNode ); // to have >1 relationship in the db r = tx.dataWrite().relationshipCreate( n1, label, n2 ); tx.success(); } try ( Transaction tx = beginTransaction() ) { assertTrue( "should delete relationship", tx.dataWrite().relationshipDelete( r ) ); try ( RelationshipScanCursor relationship = tx.cursors().allocateRelationshipScanCursor() ) { tx.dataRead().singleRelationship( r, relationship ); assertFalse( "should not find relationship", relationship.next() ); } tx.success(); } }
@Test public void shouldRollbackRelationshipOnFailure() throws Exception { long n1, n2; try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() ) { n1 = graphDb.createNode().getId(); n2 = graphDb.createNode().getId(); tx.success(); } try ( Transaction tx = beginTransaction() ) { int label = tx.token().relationshipTypeGetOrCreateForName( "R" ); tx.dataWrite().relationshipCreate( n1, label, n2 ); tx.failure(); } try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { assertEquals( 0, graphDb.getNodeById( n1 ).getDegree() ); } }
@Test public void shouldNotAllowModifyingPropertiesOnDeletedRelationship() throws Exception { // given Transaction transaction = newTransaction( AnonymousContext.writeToken() ); int prop1 = transaction.tokenWrite().propertyKeyGetOrCreateForName( "prop1" ); int type = transaction.tokenWrite().relationshipTypeGetOrCreateForName( "RELATED" ); long startNodeId = transaction.dataWrite().nodeCreate(); long endNodeId = transaction.dataWrite().nodeCreate(); long rel = transaction.dataWrite().relationshipCreate( startNodeId, type, endNodeId ); transaction.dataWrite().relationshipSetProperty( rel, prop1, Values.stringValue( "As" ) ); transaction.dataWrite().relationshipDelete( rel ); // When try { transaction.dataWrite().relationshipRemoveProperty( rel, prop1 ); fail( "Should have failed." ); } catch ( EntityNotFoundException e ) { assertThat( e.getMessage(), equalTo( "Unable to load RELATIONSHIP with id " + rel + "." ) ); } commit(); }
@Test public void shouldNotCountRemovedRelationships() throws Exception { int relationshipId; long relationship; try ( Transaction tx = beginTransaction() ) { Write write = tx.dataWrite(); relationshipId = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ); relationship = write.relationshipCreate( write.nodeCreate(), relationshipId, write.nodeCreate() ); tx.success(); } try ( Transaction tx = beginTransaction() ) { Write write = tx.dataWrite(); write.relationshipDelete( relationship ); long countsTxState = tx.dataRead().countsForRelationship( -1, relationshipId, -1 ); long countsNoTxState = tx.dataRead().countsForRelationshipWithoutTxState( -1, relationshipId, -1 ); assertEquals( 0, countsTxState ); assertEquals( 1, countsNoTxState ); } }
@Test public void shouldScanRelationshipInTransaction() throws Exception { final int nRelationshipsInStore = 10; int type; long n1, n2; try ( Transaction tx = beginTransaction() ) { n1 = tx.dataWrite().nodeCreate(); n2 = tx.dataWrite().nodeCreate(); // setup some in store relationships type = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ); relateNTimes( nRelationshipsInStore, type, n1, n2, tx ); tx.success(); } try ( Transaction tx = beginTransaction() ) { long r = tx.dataWrite().relationshipCreate( n1, type, n2 ); try ( RelationshipScanCursor relationship = tx.cursors().allocateRelationshipScanCursor() ) { tx.dataRead().allRelationshipsScan( relationship ); assertCountRelationships( relationship, nRelationshipsInStore + 1, n1, type, n2 ); } tx.success(); } }
@Test public void shouldDeleteRelationshipAddedInTransaction() throws Exception { long n1, n2; try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() ) { n1 = graphDb.createNode().getId(); n2 = graphDb.createNode().getId(); tx.success(); } try ( Transaction tx = beginTransaction() ) { int label = tx.token().relationshipTypeGetOrCreateForName( "R" ); long r = tx.dataWrite().relationshipCreate( n1, label, n2 ); assertTrue( tx.dataWrite().relationshipDelete( r ) ); tx.success(); } try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { assertEquals( 0, graphDb.getNodeById( n1 ).getDegree() ); } }
@Test public void shouldBeAbleToRemoveResetAndTwiceRemovePropertyOnRelationship() throws Exception { // given Transaction transaction = newTransaction( AnonymousContext.writeToken() ); int prop = transaction.tokenWrite().propertyKeyGetOrCreateForName( "foo" ); int type = transaction.tokenWrite().relationshipTypeGetOrCreateForName( "RELATED" ); long startNodeId = transaction.dataWrite().nodeCreate(); long endNodeId = transaction.dataWrite().nodeCreate(); long rel = transaction.dataWrite().relationshipCreate( startNodeId, type, endNodeId ); transaction.dataWrite().relationshipSetProperty( rel, prop, Values.of( "bar" ) ); commit(); // when Write write = dataWriteInNewTransaction(); write.relationshipRemoveProperty( rel, prop ); write.relationshipSetProperty( rel, prop, Values.of( "bar" ) ); write.relationshipRemoveProperty( rel, prop ); write.relationshipRemoveProperty( rel, prop ); commit(); // then transaction = newTransaction(); assertThat( relationshipGetProperty(transaction, rel, prop ), equalTo( Values.NO_VALUE ) ); commit(); } }
@Test public void hasPropertiesShouldSeeNewlyCreatedPropertiesOnNewlyCreatedRelationship() throws Exception { try ( Transaction tx = beginTransaction() ) { Write write = tx.dataWrite(); int token = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ); long relationship = write.relationshipCreate( write.nodeCreate(), token, write.nodeCreate() ); try ( RelationshipScanCursor cursor = tx.cursors().allocateRelationshipScanCursor() ) { tx.dataRead().singleRelationship( relationship, cursor ); assertTrue( cursor.next() ); assertFalse( hasProperties( cursor, tx ) ); tx.dataWrite().relationshipSetProperty( relationship, tx.tokenWrite().propertyKeyGetOrCreateForName( "prop" ), stringValue( "foo" ) ); assertTrue( hasProperties( cursor, tx ) ); } } }
@Test public void shouldNotScanRelationshipWhichWasDeletedInTransaction() throws Exception { final int nRelationshipsInStore = 5 + 1 + 5; int type; long n1, n2, r; try ( Transaction tx = beginTransaction() ) { n1 = tx.dataWrite().nodeCreate(); n2 = tx.dataWrite().nodeCreate(); type = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ); relateNTimes( 5, type, n1, n2, tx ); r = tx.dataWrite().relationshipCreate( n1, type, n2 ); relateNTimes( 5, type, n1, n2, tx ); tx.success(); } try ( Transaction tx = beginTransaction() ) { assertTrue( "should delete relationship", tx.dataWrite().relationshipDelete( r ) ); try ( RelationshipScanCursor relationship = tx.cursors().allocateRelationshipScanCursor() ) { tx.dataRead().allRelationshipsScan( relationship ); assertCountRelationships( relationship, nRelationshipsInStore - 1, n1, type, n2 ); } tx.success(); } }
@Test public void shouldNotSeeRelationshipDeletedInTransaction() throws Exception { long n1, n2, r; try ( Transaction tx = beginTransaction() ) { n1 = tx.dataWrite().nodeCreate(); n2 = tx.dataWrite().nodeCreate(); int label = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ); r = tx.dataWrite().relationshipCreate( n1, label, n2 ); tx.success(); } try ( Transaction tx = beginTransaction() ) { tx.dataWrite().relationshipDelete( r ); try ( NodeCursor node = tx.cursors().allocateNodeCursor(); RelationshipTraversalCursor relationship = tx.cursors().allocateRelationshipTraversalCursor() ) { tx.dataRead().singleNode( n1, node ); assertTrue( "should access node", node.next() ); node.allRelationships( relationship ); assertFalse( "should not find relationship", relationship.next() ); } tx.success(); } }
@Test public void shouldSeeRelationshipInTransaction() throws Exception { long n1, n2; try ( Transaction tx = beginTransaction() ) { n1 = tx.dataWrite().nodeCreate(); n2 = tx.dataWrite().nodeCreate(); tx.success(); } try ( Transaction tx = beginTransaction() ) { int label = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ); long r = tx.dataWrite().relationshipCreate( n1, label, n2 ); try ( NodeCursor node = tx.cursors().allocateNodeCursor(); RelationshipTraversalCursor relationship = tx.cursors().allocateRelationshipTraversalCursor() ) { tx.dataRead().singleNode( n1, node ); assertTrue( "should access node", node.next() ); node.allRelationships( relationship ); assertTrue( "should find relationship", relationship.next() ); assertEquals( r, relationship.relationshipReference() ); assertFalse( "should only find one relationship", relationship.next() ); } tx.success(); } }