Skip to content

Commit

Permalink
Support replacing existing graph property
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Mar 1, 2018
1 parent 7315810 commit 158d372
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Expand Up @@ -50,6 +50,29 @@ public void shouldBeAbleToWriteNewGraphProperty() throws Exception
}
}

@Test
public void shouldBeAbleToReplaceExistingGraphProperty() throws Exception
{
int prop;
try ( Transaction tx = session.beginTransaction() )
{
prop = tx.tokenWrite().propertyKeyGetOrCreateForName( "prop" );
assertThat( tx.dataWrite().graphSetProperty( prop, stringValue( "hello" ) ), equalTo( NO_VALUE ) );
tx.success();
}

try ( Transaction tx = session.beginTransaction() )
{
assertThat( tx.dataWrite().graphSetProperty( prop, stringValue( "good bye" ) ), equalTo( stringValue("hello") ) );
tx.success();
}

try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() )
{
assertThat( graphProperties().getProperty( "prop" ), equalTo( "good bye" ) );
}
}

@Test
public void shouldBeAbleToReadExistingGraphProperties() throws Exception
{
Expand Down
Expand Up @@ -497,8 +497,8 @@ public Value graphSetProperty( int propertyKey, Value value )
{
ktx.locks().optimistic().acquireExclusive( ktx.lockTracer(), ResourceTypes.GRAPH_PROPS, ResourceTypes.graphPropertyResource() );
ktx.assertOpen();
//TODO check value before writing
Value existingValue = NO_VALUE;

Value existingValue = readGraphProperty( propertyKey );
if ( !existingValue.equals( value ))
{
ktx.txState().graphDoReplaceProperty( propertyKey, existingValue, value );
Expand Down Expand Up @@ -646,6 +646,23 @@ private Value readRelationshipProperty( int propertyKey )
return existingValue;
}

private Value readGraphProperty( int propertyKey )
{
allStoreHolder.graphProperties( propertyCursor );

//Find out if the property had a value
Value existingValue = NO_VALUE;
while ( propertyCursor.next() )
{
if ( propertyCursor.propertyKey() == propertyKey )
{
existingValue = propertyCursor.propertyValue();
break;
}
}
return existingValue;
}

public CursorFactory cursors()
{
return cursors;
Expand Down

0 comments on commit 158d372

Please sign in to comment.