Skip to content

Commit

Permalink
Added new utilities to tx state
Browse files Browse the repository at this point in the history
- means to keep track of changes for a property reference
- minor utility method to find changed properties
  • Loading branch information
pontusmelke committed Dec 4, 2017
1 parent 657700f commit be46632
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 3 deletions.
Expand Up @@ -23,6 +23,7 @@
import org.neo4j.kernel.api.schema.constaints.ConstraintDescriptor;
import org.neo4j.kernel.api.schema.constaints.IndexBackedConstraintDescriptor;
import org.neo4j.kernel.api.schema.index.IndexDescriptor;
import org.neo4j.storageengine.api.txstate.PropertyContainerState;
import org.neo4j.storageengine.api.txstate.ReadableTransactionState;
import org.neo4j.values.storable.Value;
import org.neo4j.values.storable.ValueTuple;
Expand Down Expand Up @@ -67,6 +68,8 @@ public interface TransactionState extends ReadableTransactionState

void nodeDoRemoveLabel( int labelId, long nodeId );

void registerProperties( long ref, PropertyContainerState state );

// TOKEN RELATED

void labelDoCreateForName( String labelName, int id );
Expand All @@ -92,4 +95,5 @@ public interface TransactionState extends ReadableTransactionState
boolean constraintDoUnRemove( ConstraintDescriptor constraint );

void indexDoUpdateEntry( LabelSchemaDescriptor descriptor, long nodeId, ValueTuple before, ValueTuple after );

}
Expand Up @@ -28,7 +28,6 @@
import org.neo4j.collection.primitive.PrimitiveIntSet;
import org.neo4j.collection.primitive.PrimitiveLongCollections;
import org.neo4j.collection.primitive.PrimitiveLongIterator;
import org.neo4j.helpers.collection.Iterators;
import org.neo4j.kernel.api.exceptions.schema.ConstraintValidationException;
import org.neo4j.kernel.impl.api.state.RelationshipChangesForNode.DiffStrategy;
import org.neo4j.kernel.impl.util.diffsets.DiffSets;
Expand Down Expand Up @@ -324,6 +323,12 @@ public boolean hasChanges()
return false;
}

@Override
public boolean hasPropertyChanges()
{
return false;
}

@Override
public StorageProperty getChangedProperty( int propertyKeyId )
{
Expand All @@ -336,6 +341,12 @@ public StorageProperty getAddedProperty( int propertyKeyId )
return null;
}

@Override
public boolean isPropertyChangedOrRemoved( int propertyKey )
{
return false;
}

@Override
public boolean isPropertyRemoved( int propertyKeyId )
{
Expand Down
Expand Up @@ -78,7 +78,7 @@ public void clear()
}
}

public void changeProperty( int propertyKeyId, Value value )
void changeProperty( int propertyKeyId, Value value )
{
if ( addedProperties != null )
{
Expand All @@ -101,7 +101,7 @@ public void changeProperty( int propertyKeyId, Value value )
}
}

public void addProperty( int propertyKeyId, Value value )
void addProperty( int propertyKeyId, Value value )
{
if ( removedProperties != null )
{
Expand Down Expand Up @@ -204,6 +204,12 @@ public void accept( Visitor visitor ) throws ConstraintValidationException

@Override
public boolean hasChanges()
{
return hasPropertyChanges();
}

@Override
public boolean hasPropertyChanges()
{
return addedProperties != null || removedProperties != null || changedProperties != null;
}
Expand All @@ -220,6 +226,13 @@ public StorageProperty getAddedProperty( int propertyKeyId )
return addedProperties == null ? null : getPropertyOrNull( addedProperties, propertyKeyId );
}

@Override
public boolean isPropertyChangedOrRemoved( int propertyKey )
{
return (removedProperties != null && removedProperties.containsKey( propertyKey ))
|| (changedProperties != null && changedProperties.containsKey( propertyKey ));
}

@Override
public boolean isPropertyRemoved( int propertyKeyId )
{
Expand Down
Expand Up @@ -131,6 +131,12 @@ public boolean hasChanges()
return false;
}

@Override
public boolean hasPropertyChanges()
{
return false;
}

@Override
public StorageProperty getChangedProperty( int propertyKeyId )
{
Expand All @@ -143,6 +149,12 @@ public StorageProperty getAddedProperty( int propertyKeyId )
return null;
}

@Override
public boolean isPropertyChangedOrRemoved( int propertyKey )
{
return false;
}

@Override
public boolean isPropertyRemoved( int propertyKeyId )
{
Expand Down
Expand Up @@ -32,6 +32,7 @@
import org.neo4j.collection.primitive.PrimitiveIntObjectVisitor;
import org.neo4j.collection.primitive.PrimitiveIntSet;
import org.neo4j.collection.primitive.PrimitiveLongIterator;
import org.neo4j.collection.primitive.PrimitiveLongObjectMap;
import org.neo4j.collection.primitive.PrimitiveLongSet;
import org.neo4j.cursor.Cursor;
import org.neo4j.helpers.collection.Iterables;
Expand Down Expand Up @@ -151,6 +152,8 @@ void setMap( TxState state, Map<Long, RelationshipStateImpl> map )
// Tracks added and removed relationships, not modified relationships
private RelationshipDiffSets<Long> relationships;

private PrimitiveLongObjectMap<PropertyContainerState> propertiesMap = Primitive.longObjectMap();

/**
* These two sets are needed because create-delete in same transaction is a no-op in {@link DiffSets}
* but we still need to provide correct answer in {@link #nodeIsDeletedInThisTx(long)} and
Expand Down Expand Up @@ -661,6 +664,12 @@ public void nodeDoRemoveLabel( int labelId, long nodeId )
dataChanged();
}

@Override
public void registerProperties( long ref, PropertyContainerState state )
{
propertiesMap.put( ref, state );
}

@Override
public void labelDoCreateForName( String labelName, int id )
{
Expand Down Expand Up @@ -706,6 +715,12 @@ public RelationshipState getRelationshipState( long id )
return RELATIONSHIP_STATE.get( this, id );
}

@Override
public PropertyContainerState getPropertiesState( long reference )
{
return propertiesMap.get( reference );
}

@Override
public Cursor<NodeItem> augmentSingleNodeCursor( Cursor<NodeItem> cursor, long nodeId )
{
Expand Down
Expand Up @@ -57,10 +57,14 @@ void visitPropertyChanges( long entityId, Iterator<StorageProperty> added,

boolean hasChanges();

boolean hasPropertyChanges();

StorageProperty getChangedProperty( int propertyKeyId );

StorageProperty getAddedProperty( int propertyKeyId );

boolean isPropertyChangedOrRemoved( int propertyKey );

boolean isPropertyRemoved( int propertyKeyId );

PropertyContainerState EMPTY = new EmptyPropertyContainerState();
Expand Down Expand Up @@ -108,6 +112,12 @@ public boolean hasChanges()
return false;
}

@Override
public boolean hasPropertyChanges()
{
return false;
}

@Override
public StorageProperty getChangedProperty( int propertyKeyId )
{
Expand All @@ -120,6 +130,12 @@ public StorageProperty getAddedProperty( int propertyKeyId )
return null;
}

@Override
public boolean isPropertyChangedOrRemoved( int propertyKey )
{
return false;
}

@Override
public boolean isPropertyRemoved( int propertyKeyId )
{
Expand Down
Expand Up @@ -142,6 +142,8 @@ ReadableDiffSets<Long> indexUpdatesForRangeSeekByString( IndexDescriptor index,

RelationshipState getRelationshipState( long id );

PropertyContainerState getPropertiesState( long reference );

Cursor<NodeItem> augmentSingleNodeCursor( Cursor<NodeItem> cursor, long nodeId );

Cursor<PropertyItem> augmentPropertyCursor( Cursor<PropertyItem> cursor,
Expand Down
Expand Up @@ -46,7 +46,10 @@
import org.neo4j.kernel.api.txstate.TransactionState;
import org.neo4j.storageengine.api.Direction;
import org.neo4j.storageengine.api.RelationshipItem;
import org.neo4j.storageengine.api.txstate.NodeState;
import org.neo4j.storageengine.api.txstate.PropertyContainerState;
import org.neo4j.storageengine.api.txstate.ReadableDiffSets;
import org.neo4j.storageengine.api.txstate.RelationshipState;
import org.neo4j.storageengine.api.txstate.TxStateVisitor;
import org.neo4j.test.rule.RandomRule;
import org.neo4j.test.rule.RepeatRule;
Expand Down Expand Up @@ -1470,6 +1473,36 @@ public void shouldObserveCorrectAugmentedNodeRelationshipsState() throws Excepti
}
}

@Test
public void shouldGetNodeStateFromPropertyReference() throws Exception
{
// GIVEN
state.nodeDoCreate( 1337L );
NodeState nodeState = state.getNodeState( 1337L );
state.registerProperties( 42L, nodeState );

// WHEN
PropertyContainerState propertiesState = state.getPropertiesState( 42L );

// Then
assertThat( propertiesState, equalTo( nodeState ) );
}

@Test
public void shouldGetRelationshipStateFromPropertyReference() throws Exception
{
// GIVEN
state.relationshipDoCreate( 1337L, 0, 1L, 2L );
RelationshipState relState = state.getRelationshipState( 1337L );
state.registerProperties( 42L, relState );

// WHEN
PropertyContainerState propertiesState = state.getPropertiesState( 42L );

// Then
assertThat( propertiesState, equalTo( relState ) );
}

private Map<Long,RelationshipItem> relationshipsForNode( long nodeId, Map<Long,RelationshipItem> allRelationships,
Direction direction, int[] relationshipTypes )
{
Expand Down

0 comments on commit be46632

Please sign in to comment.