-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10833 from fickludd/3.4-tx-state-aware-rel-cursor
Tx-state aware RelationshipScanCursor and relationship writes
- Loading branch information
Showing
18 changed files
with
712 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...api/src/test/java/org/neo4j/internal/kernel/api/RelationshipTransactionStateTestBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright (c) 2002-2018 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.internal.kernel.api; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
@SuppressWarnings( "Duplicates" ) | ||
public abstract class RelationshipTransactionStateTestBase<G extends KernelAPIWriteTestSupport> extends KernelAPIWriteTestBase<G> | ||
{ | ||
@Test | ||
public void shouldSeeSingleRelationshipInTransaction() throws Exception | ||
{ | ||
int label; | ||
long n1, n2; | ||
try ( Transaction tx = session.beginTransaction() ) | ||
{ | ||
n1 = tx.dataWrite().nodeCreate(); | ||
n2 = tx.dataWrite().nodeCreate(); | ||
|
||
// setup extra relationship to challenge the implementation | ||
long decoyNode = tx.dataWrite().nodeCreate(); | ||
label = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ); | ||
tx.dataWrite().relationshipCreate( n2, label, decoyNode ); | ||
tx.success(); | ||
} | ||
|
||
try ( Transaction tx = session.beginTransaction() ) | ||
{ | ||
long r = tx.dataWrite().relationshipCreate( n1, label, n2 ); | ||
try ( RelationshipScanCursor relationship = cursors.allocateRelationshipScanCursor() ) | ||
{ | ||
tx.dataRead().singleRelationship( r, relationship ); | ||
assertTrue( "should find relationship", relationship.next() ); | ||
|
||
assertEquals( label, relationship.label() ); | ||
assertEquals( n1, relationship.sourceNodeReference() ); | ||
assertEquals( n2, relationship.targetNodeReference() ); | ||
assertEquals( r, relationship.relationshipReference() ); | ||
|
||
assertFalse( "should only find one relationship", relationship.next() ); | ||
} | ||
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(); | ||
} | ||
} | ||
} |
193 changes: 193 additions & 0 deletions
193
...ity/kernel-api/src/test/java/org/neo4j/internal/kernel/api/RelationshipWriteTestBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
/* | ||
* Copyright (c) 2002-2018 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.internal.kernel.api; | ||
|
||
import org.junit.Ignore; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
import java.util.List; | ||
|
||
import org.neo4j.graphdb.Node; | ||
import org.neo4j.graphdb.Relationship; | ||
import org.neo4j.graphdb.RelationshipType; | ||
import org.neo4j.helpers.collection.Iterables; | ||
import org.neo4j.internal.kernel.api.exceptions.KernelException; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.neo4j.graphdb.Label.label; | ||
import static org.neo4j.values.storable.Values.NO_VALUE; | ||
import static org.neo4j.values.storable.Values.intValue; | ||
import static org.neo4j.values.storable.Values.stringValue; | ||
|
||
@SuppressWarnings( "Duplicates" ) | ||
public abstract class RelationshipWriteTestBase<G extends KernelAPIWriteTestSupport> extends KernelAPIWriteTestBase<G> | ||
{ | ||
@Rule | ||
public ExpectedException exception = ExpectedException.none(); | ||
|
||
@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 = session.beginTransaction() ) | ||
{ | ||
int label = session.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 shouldCreateRelationshipBetweenInTransactionNodes() throws Exception | ||
{ | ||
long n1, n2, r; | ||
try ( Transaction tx = session.beginTransaction() ) | ||
{ | ||
n1 = tx.dataWrite().nodeCreate(); | ||
n2 = tx.dataWrite().nodeCreate(); | ||
int label = session.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 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 = session.beginTransaction() ) | ||
{ | ||
int label = session.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 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() ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
community/kernel/src/main/java/org/neo4j/kernel/impl/newapi/HasChanges.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright (c) 2002-2018 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.kernel.impl.newapi; | ||
|
||
enum HasChanges | ||
{ | ||
MAYBE, | ||
YES, | ||
NO | ||
} |
Oops, something went wrong.