Skip to content

Commit

Permalink
Relationship delete over Kernel API
Browse files Browse the repository at this point in the history
  • Loading branch information
fickludd committed Jan 23, 2018
1 parent 6f8f4f9 commit cfcfe2e
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 5 deletions.
Expand Up @@ -55,7 +55,7 @@ public interface Write
* Delete a relationship * Delete a relationship
* @param relationship the internal id of the relationship to delete * @param relationship the internal id of the relationship to delete
*/ */
void relationshipDelete( long relationship ); boolean relationshipDelete( long relationship ) throws AutoIndexingKernelException;


/** /**
* Add a label to a node * Add a label to a node
Expand Down
Expand Up @@ -62,4 +62,34 @@ public void shouldSeeSingleRelationshipInTransaction() throws Exception
tx.success(); tx.success();
} }
} }

@Test
public void shouldNotSeeSingleRelationshipWhichWasDeletedInTransaction() throws Exception
{
int label;
long n1, n2, r;
try ( Transaction tx = session.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 = session.beginTransaction() )
{
assertTrue( "should delete relationship", tx.dataWrite().relationshipDelete( r ) );
try ( RelationshipScanCursor relationship = cursors.allocateRelationshipScanCursor() )
{
tx.dataRead().singleRelationship( r, relationship );
assertFalse( "should not find relationship", relationship.next() );
}
tx.success();
}
}
} }
Expand Up @@ -27,8 +27,8 @@
import java.util.List; import java.util.List;


import org.neo4j.graphdb.Node; import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.NotFoundException;
import org.neo4j.graphdb.Relationship; import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.helpers.collection.Iterables; import org.neo4j.helpers.collection.Iterables;
import org.neo4j.internal.kernel.api.exceptions.KernelException; import org.neo4j.internal.kernel.api.exceptions.KernelException;


Expand All @@ -37,7 +37,6 @@
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.neo4j.graphdb.Label.label; import static org.neo4j.graphdb.Label.label;
import static org.neo4j.values.storable.Values.NO_VALUE; import static org.neo4j.values.storable.Values.NO_VALUE;
import static org.neo4j.values.storable.Values.intValue; import static org.neo4j.values.storable.Values.intValue;
Expand Down Expand Up @@ -120,4 +119,75 @@ public void shouldRollbackRelationshipOnFailure() throws Exception
assertEquals( 0, graphDb.getNodeById( n1 ).getDegree() ); assertEquals( 0, graphDb.getNodeById( n1 ).getDegree() );
} }
} }

@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 = session.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 shouldNotDeleteRelationshipThatDoesNotExist() throws Exception
{
long relationship = 0;

try ( Transaction tx = session.beginTransaction() )
{
assertFalse( tx.dataWrite().relationshipDelete( relationship ) );
tx.failure();
}
try ( Transaction tx = session.beginTransaction() )
{
assertFalse( tx.dataWrite().relationshipDelete( relationship ) );
tx.success();
}
// should not crash
}

@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 = session.beginTransaction() )
{
int label = session.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() );
}
}
} }
Expand Up @@ -83,6 +83,7 @@ public class Operations implements Write, ExplicitIndexWrite
private org.neo4j.kernel.impl.newapi.NodeCursor nodeCursor; private org.neo4j.kernel.impl.newapi.NodeCursor nodeCursor;
private final IndexTxStateUpdater updater; private final IndexTxStateUpdater updater;
private PropertyCursor propertyCursor; private PropertyCursor propertyCursor;
private RelationshipScanCursor relationshipCursor;
private final Cursors cursors; private final Cursors cursors;
private final NodeSchemaMatcher schemaMatcher; private final NodeSchemaMatcher schemaMatcher;


Expand Down Expand Up @@ -110,6 +111,7 @@ public void initialize()
{ {
this.nodeCursor = cursors.allocateNodeCursor(); this.nodeCursor = cursors.allocateNodeCursor();
this.propertyCursor = cursors.allocatePropertyCursor(); this.propertyCursor = cursors.allocatePropertyCursor();
this.relationshipCursor = cursors.allocateRelationshipScanCursor();
} }


@Override @Override
Expand Down Expand Up @@ -166,10 +168,22 @@ public long relationshipCreate( long sourceNode, int relationshipLabel, long tar
} }


@Override @Override
public void relationshipDelete( long relationship ) public boolean relationshipDelete( long relationship ) throws AutoIndexingKernelException
{ {
ktx.assertOpen(); ktx.assertOpen();
throw new UnsupportedOperationException();
allStoreHolder.singleRelationship( relationship, relationshipCursor ); // tx-state aware

if ( relationshipCursor.next() )
{
autoIndexing.relationships().entityRemoved( this, relationship );
ktx.txState().relationshipDoDelete( relationship, relationshipCursor.getType(),
relationshipCursor.sourceNodeReference(), relationshipCursor.targetNodeReference() );
return true;
}

// tried to delete relationship that does not exist
return false;
} }


@Override @Override
Expand Down

0 comments on commit cfcfe2e

Please sign in to comment.