@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(); } }
private long createNodeWithValues( Value value1, Value value2 ) throws KernelException { Write write = dataWriteInNewTransaction(); long nodeId = write.nodeCreate(); write.nodeAddLabel( nodeId, labelId ); write.nodeSetProperty( nodeId, propertyId1, value1 ); write.nodeSetProperty( nodeId, propertyId2, value2 ); commit(); return nodeId; }
@Test public void shouldHandleMultipleNodeDeletions() throws Exception { long nodeId; try ( Transaction tx = beginTransaction() ) { nodeId = tx.dataWrite().nodeCreate(); tx.success(); } try ( Transaction tx = beginTransaction() ) { assertTrue( tx.dataWrite().nodeDelete( nodeId ) ); assertFalse( tx.dataWrite().nodeDelete( nodeId ) ); tx.success(); } }
private long createNode( Write write, int... labels ) throws KernelException { long nodeId = write.nodeCreate(); for ( int label : labels ) { write.nodeAddLabel( nodeId, label ); } return nodeId; } }
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!" ); } } }
tx.dataWrite().relationshipCreate( tx.dataWrite().nodeCreate(), type, start.id ); tx.dataWrite().relationshipCreate( tx.dataWrite().nodeCreate(), type, start.id ); break; case OUTGOING: tx.dataWrite().relationshipCreate( start.id, type, tx.dataWrite().nodeCreate() ); tx.dataWrite().relationshipCreate( start.id, type, tx.dataWrite().nodeCreate() ); break; case BOTH: tx.dataWrite().relationshipCreate( start.id, type, start.id ); tx.dataWrite().relationshipCreate( start.id, type, start.id ); break; default: throw new IllegalStateException( "Oh ye be cursed, foul checkstyle!" ); tx.dataWrite().relationshipDelete( head.id ); expectedCounts.put( kv.getKey(), rs.size() + 1 ); tx.dataWrite().relationshipCreate( tx.dataWrite().nodeCreate(), newType, start.id ); tx.dataWrite().relationshipCreate( start.id, newType, tx.dataWrite().nodeCreate() ); tx.dataWrite().relationshipCreate( start.id, newType, start.id );
@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 ) ); } } }
try ( Transaction tx = beginTransaction() ) node = tx.dataWrite().nodeCreate(); prop1 = tx.tokenWrite().propertyKeyGetOrCreateForName( "prop1" ); prop2 = tx.tokenWrite().propertyKeyGetOrCreateForName( "prop2" ); prop3 = tx.tokenWrite().propertyKeyGetOrCreateForName( "prop3" ); tx.dataWrite().nodeSetProperty( node, prop1, longValue( 1 ) ); tx.dataWrite().nodeSetProperty( node, prop2, longValue( 2 ) ); tx.dataWrite().nodeSetProperty( node, prop3, longValue( 3 ) ); tx.success(); tx.dataWrite().nodeRemoveProperty( node, prop1 ); assertTrue( hasProperties( cursor, props ) ); tx.dataWrite().nodeRemoveProperty( node, prop2 ); assertTrue( hasProperties( cursor, props ) ); tx.dataWrite().nodeRemoveProperty( node, prop3 ); assertFalse( hasProperties( cursor, props ) );
private void createNodeWithProperty( Transaction tx, int propId1 ) throws KernelException { long node = tx.dataWrite().nodeCreate(); tx.dataWrite().nodeSetProperty( node, propId1, Values.intValue( 42 ) ); }
private void setProperty( long nodeId, int propertyId, Object value ) throws KernelException { transaction.dataWrite().nodeSetProperty( nodeId, propertyId, Values.of( value ) ); }
@Parameterized.Parameters( name = "{0}" ) public static Collection<Object[]> parameters() { // Some samples of operations that should react to transaction killing. Not exhaustive. return Arrays.asList( operation( "nodeExists", ( read, write, schema ) -> read.nodeExists( 0 ) ), operation( "singleRelationship", ( read, write, schema ) -> read.singleRelationship( 0, null ) ), operation( "nodeCreate", ( read, write, schema ) -> write.nodeCreate() ), operation( "relationshipSetProperty", ( read, write, schema ) -> write.relationshipSetProperty( 0, 2, Values.longValue( 42 ) ) ), operation( "indexesGetAll", ( read, write, schema ) -> schema.indexesGetAll() ) ); }
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 shouldRemoveReSetAndTwiceRemovePropertyOnNode() throws Exception { // given long node = createNodeWithProperty( propertyKey, "bar" ); // when try ( Transaction tx = beginTransaction() ) { int prop = tx.token().propertyKeyGetOrCreateForName( propertyKey ); tx.dataWrite().nodeRemoveProperty( node, prop ); tx.dataWrite().nodeSetProperty( node, prop, Values.of( "bar" ) ); tx.dataWrite().nodeRemoveProperty( node, prop ); tx.dataWrite().nodeRemoveProperty( node, prop ); tx.success(); } // then assertNoProperty( node, propertyKey ); }
private void addLabel( long nodeId, int labelId ) throws KernelException { transaction.dataWrite().nodeAddLabel( nodeId, labelId ); }
@Test public void shouldFindSwappedNodeInLabelScan() throws Exception { // Given Node node1 = createNode( "label" ); Node node2 = createNode(); try ( org.neo4j.internal.kernel.api.Transaction tx = beginTransaction(); NodeLabelIndexCursor cursor = tx.cursors().allocateNodeLabelIndexCursor() ) { // when tx.dataWrite().nodeRemoveLabel( node1.node, node1.labels[0] ); tx.dataWrite().nodeAddLabel( node2.node, node1.labels[0] ); tx.dataRead().nodeLabelScan( node1.labels[0], cursor ); // then assertTrue( cursor.next() ); assertEquals( node2.node, cursor.nodeReference() ); } }
@Ignore public void shouldNotFindNodeWithAllRemovedLabelsInDisjunctionLabelScan() throws Exception { // Given Node node = createNode( "label1", "label2" ); try ( org.neo4j.internal.kernel.api.Transaction tx = beginTransaction(); NodeLabelIndexCursor cursor = tx.cursors().allocateNodeLabelIndexCursor() ) { // when tx.dataWrite().nodeRemoveLabel( node.node, node.labels[0] ); tx.dataWrite().nodeRemoveLabel( node.node, node.labels[1] ); tx.dataRead().nodeLabelUnionScan( cursor, node.labels ); // then assertFalse( cursor.next() ); } }
@Test public void shouldUpdatePropertyToRelationshipInTransaction() throws Exception { // Given long relationshipId; String propertyKey = "prop"; try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() ) { Node node1 = graphDb.createNode(); Node node2 = graphDb.createNode(); relationshipId = node1.createRelationshipTo( node2, RelationshipType.withName( "R" ) ).getId(); tx.success(); } // When try ( Transaction tx = beginTransaction() ) { int token = tx.token().propertyKeyGetOrCreateForName( propertyKey ); assertThat( tx.dataWrite().relationshipSetProperty( relationshipId, token, stringValue( "hello" ) ), equalTo( NO_VALUE ) ); assertThat( tx.dataWrite().relationshipSetProperty( relationshipId, token, stringValue( "world" ) ), equalTo( stringValue( "hello" ) ) ); assertThat( tx.dataWrite().relationshipSetProperty( relationshipId, token, intValue( 1337 ) ), equalTo( stringValue( "world" ) ) ); tx.success(); } // Then try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { assertThat( graphDb.getRelationshipById( relationshipId ).getProperty( "prop" ), equalTo( 1337 ) ); } }
@Test public void shouldNotRemoveNodeThatDoesNotExist() throws Exception { long node = 0; try ( Transaction tx = beginTransaction() ) { assertFalse( tx.dataWrite().nodeDelete( node ) ); tx.failure(); } try ( Transaction tx = beginTransaction() ) { assertFalse( tx.dataWrite().nodeDelete( node ) ); tx.success(); } // should not crash }
@Test public void txReturnsCorrectIdWhenFailedAndMarkedForTermination() throws Exception { executeDummyTxs( db, 42 ); org.neo4j.internal.kernel.api.Transaction tx = newTransaction( AUTH_DISABLED ); tx.dataWrite().nodeCreate(); tx.failure(); tx.markForTermination( Status.Transaction.Terminated ); assertEquals( KernelTransaction.ROLLBACK, tx.closeTransaction() ); assertFalse( tx.isOpen() ); }
@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() ); } }