@Override public void delete() { KernelTransaction transaction = spi.kernelTransaction(); try { boolean deleted = transaction.dataWrite().relationshipDelete( id ); if ( !deleted ) { throw new NotFoundException( "Unable to delete relationship[" + getId() + "] since it is already deleted." ); } } catch ( InvalidTransactionTypeKernelException e ) { throw new ConstraintViolationException( e.getMessage(), e ); } catch ( AutoIndexingKernelException e ) { throw new IllegalStateException( "Auto indexing encountered a failure while deleting the relationship: " + e.getMessage(), e ); } }
@Test public void shouldNotDeleteRelationshipThatDoesNotExist() throws Exception { long relationship = 0; try ( Transaction tx = beginTransaction() ) { assertFalse( tx.dataWrite().relationshipDelete( relationship ) ); tx.failure(); } try ( Transaction tx = beginTransaction() ) { assertFalse( tx.dataWrite().relationshipDelete( relationship ) ); tx.success(); } // should not crash }
throw new IllegalStateException( "Oh ye be cursed, foul checkstyle!" ); tx.dataWrite().relationshipDelete( head.id ); expectedCounts.put( kv.getKey(), rs.size() + 1 );
@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 shouldDetectRelationshipDeletedInTransaction() throws Exception { // GIVEN long node; int relType; long deletedInTx, unaffected, addedInTx, addedAndRemovedInTx; try ( Transaction tx = beginTransaction() ) { node = tx.dataWrite().nodeCreate(); relType = tx.tokenWrite().relationshipTypeCreateForName( "REL_TYPE" ); deletedInTx = tx.dataWrite().relationshipCreate(node, relType, node); unaffected = tx.dataWrite().relationshipCreate(node, relType, node); tx.success(); } try ( Transaction tx = beginTransaction() ) { // WHEN addedInTx = tx.dataWrite().relationshipCreate(node, relType, node); addedAndRemovedInTx = tx.dataWrite().relationshipCreate(node, relType, node); tx.dataWrite().relationshipDelete( deletedInTx ); tx.dataWrite().relationshipDelete( addedAndRemovedInTx ); // THEN assertFalse( tx.dataRead().relationshipDeletedInTransaction( addedInTx ) ); assertFalse( tx.dataRead().relationshipDeletedInTransaction( unaffected ) ); assertTrue( tx.dataRead().relationshipDeletedInTransaction( addedAndRemovedInTx ) ); assertTrue( tx.dataRead().relationshipDeletedInTransaction( deletedInTx ) ); } }
@Test public void shouldDeleteRelationship() throws Exception { long n1, r; try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() ) { Node node1 = graphDb.createNode(); Node node2 = graphDb.createNode(); n1 = node1.getId(); r = node1.createRelationshipTo( node2, RelationshipType.withName( "R" ) ).getId(); tx.success(); } try ( Transaction tx = beginTransaction() ) { assertTrue( "should delete relationship", tx.dataWrite().relationshipDelete( r ) ); tx.success(); } try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { assertEquals( 0, graphDb.getNodeById( n1 ).getDegree() ); } }
@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 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 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(); } }
transaction.dataWrite().relationshipDelete( fromRefToOther1 ); long endNode = transaction.dataWrite().nodeCreate(); long localTxRel = transaction.dataWrite().relationshipCreate( refNode, relType1, endNode );
@Override public void delete() { KernelTransaction transaction = spi.kernelTransaction(); try { boolean deleted = transaction.dataWrite().relationshipDelete( id ); if ( !deleted ) { throw new NotFoundException( "Unable to delete relationship[" + getId() + "] since it is already deleted." ); } } catch ( InvalidTransactionTypeKernelException e ) { throw new ConstraintViolationException( e.getMessage(), e ); } catch ( AutoIndexingKernelException e ) { throw new IllegalStateException( "Auto indexing encountered a failure while deleting the relationship: " + e.getMessage(), e ); } }