transaction.dataWrite().relationshipSetProperty( id, propertyKeyId, Values.of( value, false ) );
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 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 ) ); } }
@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() ) ); }
@Test public void shouldNotWriteWhenSettingPropertyToSameValue() throws Exception { // Given long relationshipId; String propertyKey = "prop"; Value theValue = stringValue( "The Value" ); try ( org.neo4j.graphdb.Transaction ctx = graphDb.beginTx() ) { Node node1 = graphDb.createNode(); Node node2 = graphDb.createNode(); Relationship r = node1.createRelationshipTo( node2, RelationshipType.withName( "R" ) ); r.setProperty( propertyKey, theValue.asObject() ); relationshipId = r.getId(); ctx.success(); } // When Transaction tx = beginTransaction(); int property = tx.token().propertyKeyGetOrCreateForName( propertyKey ); assertThat( tx.dataWrite().relationshipSetProperty( relationshipId, property, theValue ), equalTo( theValue ) ); tx.success(); assertThat( tx.closeTransaction(), equalTo( Transaction.READ_ONLY ) ); } }
@Test public void shouldAddPropertyToRelationship() 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 ) ); tx.success(); } // Then try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { assertThat( graphDb.getRelationshipById( relationshipId ).getProperty( "prop" ), equalTo( "hello" ) ); } }
@Test public void shouldUpdatePropertyToRelationship() throws Exception { // Given long relationshipId; String propertyKey = "prop"; try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() ) { Node node1 = graphDb.createNode(); Node node2 = graphDb.createNode(); Relationship r = node1.createRelationshipTo( node2, RelationshipType.withName( "R" ) ); r.setProperty( propertyKey, 42 ); relationshipId = r.getId(); tx.success(); } // When try ( Transaction tx = beginTransaction() ) { int token = tx.token().propertyKeyGetOrCreateForName( propertyKey ); assertThat( tx.dataWrite().relationshipSetProperty( relationshipId, token, stringValue( "hello" ) ), equalTo( intValue( 42 ) ) ); tx.success(); } // Then try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { assertThat( graphDb.getRelationshipById( relationshipId ).getProperty( "prop" ), equalTo( "hello" ) ); } }
@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 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 propertyTypeShouldBeTxStateAware() throws Exception { // Given long relationship; try ( Transaction tx = beginTransaction() ) { Write write = tx.dataWrite(); int token = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ); relationship = write.relationshipCreate( write.nodeCreate(), token, write.nodeCreate() ); tx.success(); } // Then try ( Transaction tx = beginTransaction() ) { try ( RelationshipScanCursor relationships = tx.cursors().allocateRelationshipScanCursor(); PropertyCursor properties = tx.cursors().allocatePropertyCursor() ) { tx.dataRead().singleRelationship( relationship, relationships ); assertTrue( relationships.next() ); assertFalse( hasProperties( relationships, tx ) ); int prop = tx.tokenWrite().propertyKeyGetOrCreateForName( "prop" ); tx.dataWrite().relationshipSetProperty( relationship, prop, stringValue( "foo" ) ); relationships.properties( properties ); assertTrue( properties.next() ); assertThat( properties.propertyType(), equalTo( ValueGroup.TEXT ) ); } } }
tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ), write.nodeCreate() ); propToken1 = tx.token().propertyKeyGetOrCreateForName( propKey1 ); assertEquals( write.relationshipSetProperty( relationshipId, propToken1, stringValue( "hello" ) ), NO_VALUE ); tx.success(); assertEquals( tx.dataWrite().relationshipSetProperty( relationshipId, propToken2, stringValue( "world" ) ), NO_VALUE );
prop2 = tx.tokenWrite().propertyKeyGetOrCreateForName( "prop2" ); prop3 = tx.tokenWrite().propertyKeyGetOrCreateForName( "prop3" ); tx.dataWrite().relationshipSetProperty( relationship, prop1, longValue( 1 ) ); tx.dataWrite().relationshipSetProperty( relationship, prop2, longValue( 2 ) ); tx.dataWrite().relationshipSetProperty( relationship, prop3, longValue( 3 ) ); tx.success();
tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ), write.nodeCreate() ); propToken = tx.token().propertyKeyGetOrCreateForName( propKey ); assertEquals( write.relationshipSetProperty( relationshipId, propToken, stringValue( "hello" ) ), NO_VALUE ); tx.success(); assertEquals( tx.dataWrite().relationshipSetProperty( relationshipId, propToken, stringValue( "world" ) ), stringValue( "hello" ) ); try ( RelationshipScanCursor relationship = tx.cursors().allocateRelationshipScanCursor();
tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ), write.nodeCreate() ); propToken = tx.token().propertyKeyGetOrCreateForName( propKey ); assertEquals( write.relationshipSetProperty( relationshipId, propToken, stringValue( "hello" ) ), NO_VALUE ); tx.success(); assertEquals( tx.dataWrite().relationshipSetProperty( relationshipId, propToken, stringValue( "world" ) ), NO_VALUE ); try ( RelationshipScanCursor relationship = tx.cursors().allocateRelationshipScanCursor();
@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 ) ); } } }
assertEquals( tx.dataWrite().relationshipSetProperty( relationshipId, propToken, stringValue( "hello" ) ), NO_VALUE );
@Test public void hasPropertiesShouldSeeNewlyCreatedProperties() throws Exception { // Given long relationship; try ( Transaction tx = beginTransaction() ) { Write write = tx.dataWrite(); int token = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ); relationship = write.relationshipCreate( write.nodeCreate(), token, write.nodeCreate() ); tx.success(); } // Then try ( Transaction tx = beginTransaction() ) { 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 ) ); } } }
int prop1 = tx.token().propertyKeyGetOrCreateForName( propKey1 ); int prop2 = tx.token().propertyKeyGetOrCreateForName( propKey2 ); assertEquals( tx.dataWrite().relationshipSetProperty( r, prop1, stringValue( "hello" ) ), NO_VALUE ); assertEquals( tx.dataWrite().relationshipSetProperty( r, prop2, stringValue( "world" ) ), NO_VALUE );
tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ), write.nodeCreate() ); propToken = tx.token().propertyKeyGetOrCreateForName( propKey ); assertEquals( write.relationshipSetProperty( relationshipId, propToken, stringValue( "hello" ) ), NO_VALUE ); tx.success();
private void export(SimilarityResult similarityResult) { applyInTransaction(statement -> { long node1 = similarityResult.item1; long node2 = similarityResult.item2; try { long relationshipId = statement.dataWrite().relationshipCreate(node1, relationshipTypeId, node2); statement.dataWrite().relationshipSetProperty( relationshipId, propertyId, Values.doubleValue(similarityResult.similarity)); } catch (KernelException e) { ExceptionUtil.throwKernelException(e); } return null; }); }