@Override public void setProperty( String key, Object value ) { KernelTransaction transaction = safeAcquireTransaction(); int propertyKeyId; try { propertyKeyId = transaction.tokenWrite().propertyKeyGetOrCreateForName( key ); } catch ( IllegalTokenNameException e ) { throw new IllegalArgumentException( format( "Invalid property key '%s'.", key ), e ); } try ( Statement ignore = transaction.acquireStatement() ) { transaction.dataWrite().graphSetProperty( propertyKeyId, Values.of( value, false ) ); } catch ( InvalidTransactionTypeKernelException e ) { throw new ConstraintViolationException( e.getMessage(), e ); } }
@Test public void shouldNotWriteWhenSettingPropertyToSameValue() throws Exception { // Given int prop; Value theValue = stringValue( "The Value" ); try ( Transaction tx = beginTransaction() ) { prop = tx.tokenWrite().propertyKeyGetOrCreateForName( "prop" ); tx.dataWrite().graphSetProperty( prop, theValue ); tx.success(); } // When Transaction tx = beginTransaction(); assertThat( tx.dataWrite().graphSetProperty( prop, theValue ), equalTo( theValue ) ); tx.success(); assertThat( tx.closeTransaction(), equalTo( Transaction.READ_ONLY ) ); } }
@Test public void shouldBeAbleToWriteNewGraphProperty() throws Exception { int prop; try ( Transaction tx = beginTransaction() ) { prop = tx.tokenWrite().propertyKeyGetOrCreateForName( "prop" ); assertThat( tx.dataWrite().graphSetProperty( prop, stringValue( "hello" ) ), equalTo( NO_VALUE ) ); tx.success(); } try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { assertThat( testSupport.graphProperties().getProperty( "prop" ), equalTo( "hello" ) ); } }
@Test public void shouldBeAbleToReplaceExistingGraphProperty() throws Exception { int prop; try ( Transaction tx = beginTransaction() ) { prop = tx.tokenWrite().propertyKeyGetOrCreateForName( "prop" ); assertThat( tx.dataWrite().graphSetProperty( prop, stringValue( "hello" ) ), equalTo( NO_VALUE ) ); tx.success(); } try ( Transaction tx = beginTransaction() ) { assertThat( tx.dataWrite().graphSetProperty( prop, stringValue( "good bye" ) ), equalTo( stringValue("hello") ) ); tx.success(); } try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { assertThat( testSupport.graphProperties().getProperty( "prop" ), equalTo( "good bye" ) ); } }
@Test public void shouldSeeNewGraphPropertyInTransaction() throws Exception { try ( Transaction tx = beginTransaction(); PropertyCursor cursor = tx.cursors().allocatePropertyCursor() ) { int prop = tx.tokenWrite().propertyKeyGetOrCreateForName( "prop" ); assertThat( tx.dataWrite().graphSetProperty( prop, stringValue( "hello" ) ), equalTo( NO_VALUE ) ); tx.dataRead().graphProperties( cursor ); assertTrue( cursor.next() ); assertThat( cursor.propertyKey(), equalTo( prop ) ); assertThat( cursor.propertyValue(), equalTo( stringValue( "hello" ) ) ); } }
@Test public void shouldSeeUpdatedGraphPropertyInTransaction() throws Exception { int prop; try ( Transaction tx = beginTransaction() ) { prop = tx.tokenWrite().propertyKeyGetOrCreateForName( "prop" ); assertThat( tx.dataWrite().graphSetProperty( prop, stringValue( "hello" ) ), equalTo( NO_VALUE ) ); tx.success(); } try ( Transaction tx = beginTransaction(); PropertyCursor cursor = tx.cursors().allocatePropertyCursor() ) { assertThat( tx.dataWrite().graphSetProperty( prop, stringValue( "good bye" ) ), equalTo( stringValue( "hello" ) ) ); tx.dataRead().graphProperties( cursor ); assertTrue( cursor.next() ); assertThat( cursor.propertyKey(), equalTo( prop ) ); assertThat( cursor.propertyValue(), equalTo( stringValue( "good bye" ) ) ); assertFalse( cursor.next() ); } }
@Test public void shouldBeAbleToReadExistingGraphProperties() throws Exception { int prop1, prop2, prop3; try ( Transaction tx = beginTransaction() ) { prop1 = tx.tokenWrite().propertyKeyGetOrCreateForName( "prop1" ); prop2 = tx.tokenWrite().propertyKeyGetOrCreateForName( "prop2" ); prop3 = tx.tokenWrite().propertyKeyGetOrCreateForName( "prop3" ); tx.dataWrite().graphSetProperty( prop1, stringValue( "hello" ) ); tx.dataWrite().graphSetProperty( prop2, stringValue( "world" ) ); tx.dataWrite().graphSetProperty( prop3, stringValue( "etc" ) ); tx.success(); } try ( Transaction tx = beginTransaction(); PropertyCursor cursor = tx.cursors().allocatePropertyCursor() ) { tx.dataRead().graphProperties( cursor ); assertTrue( cursor.next() ); assertThat( cursor.propertyKey(), equalTo( prop1 ) ); assertThat( cursor.propertyValue(), equalTo( stringValue( "hello" ) ) ); assertTrue( cursor.next() ); assertThat( cursor.propertyKey(), equalTo( prop2 ) ); assertThat( cursor.propertyValue(), equalTo( stringValue( "world" ) ) ); assertTrue( cursor.next() ); assertThat( cursor.propertyKey(), equalTo( prop3 ) ); assertThat( cursor.propertyValue(), equalTo( stringValue( "etc" ) ) ); assertFalse( cursor.next() ); } }
@Test public void shouldBeAbleToRemoveExistingGraphProperty() throws Exception { int prop; try ( Transaction tx = beginTransaction() ) { prop = tx.tokenWrite().propertyKeyGetOrCreateForName( "prop" ); assertThat( tx.dataWrite().graphSetProperty( prop, stringValue( "hello" ) ), equalTo( NO_VALUE ) ); tx.success(); } try ( Transaction tx = beginTransaction() ) { assertThat( tx.dataWrite().graphRemoveProperty( prop ), equalTo( stringValue("hello") ) ); tx.success(); } try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { assertFalse( testSupport.graphProperties().hasProperty( "prop" ) ); } }
@Test public void shouldNotSeeRemovedGraphPropertyInTransaction() throws Exception { int prop; try ( Transaction tx = beginTransaction() ) { prop = tx.tokenWrite().propertyKeyGetOrCreateForName( "prop" ); assertThat( tx.dataWrite().graphSetProperty( prop, stringValue( "hello" ) ), equalTo( NO_VALUE ) ); tx.success(); } try ( Transaction tx = beginTransaction(); PropertyCursor cursor = tx.cursors().allocatePropertyCursor() ) { assertThat( tx.dataWrite().graphRemoveProperty( prop ), equalTo( stringValue( "hello" ) ) ); tx.dataRead().graphProperties( cursor ); assertFalse( cursor.next() ); } }
@Override public void setProperty( String key, Object value ) { KernelTransaction transaction = safeAcquireTransaction(); int propertyKeyId; try { propertyKeyId = transaction.tokenWrite().propertyKeyGetOrCreateForName( key ); } catch ( IllegalTokenNameException e ) { throw new IllegalArgumentException( format( "Invalid property key '%s'.", key ), e ); } try ( Statement ignore = transaction.acquireStatement() ) { transaction.dataWrite().graphSetProperty( propertyKeyId, Values.of( value, false ) ); } catch ( InvalidTransactionTypeKernelException e ) { throw new ConstraintViolationException( e.getMessage(), e ); } }