Skip to content

Commit

Permalink
Introduce empty readable transaction state
Browse files Browse the repository at this point in the history
Simplify code by avoiding many not null checks
  • Loading branch information
davidegrohmann committed May 8, 2017
1 parent 0157505 commit 61d45b3
Show file tree
Hide file tree
Showing 56 changed files with 802 additions and 618 deletions.
Expand Up @@ -345,8 +345,7 @@ final class TransactionBoundQueryContext(tc: TransactionalContextWrapper)
def indexQuery(name: String, query: Any): Iterator[Node] =
JavaConversionSupport.mapToScalaENFXSafe(tc.statement.readOperations().nodeLegacyIndexQuery(name, query))(getById)

def isDeleted(n: Node): Boolean =
tc.stateView.hasTxStateWithChanges && tc.stateView.txState().nodeIsDeletedInThisTx(n.getId)
def isDeleted(n: Node): Boolean = tc.stateView.readableTxState().nodeIsDeletedInThisTx(n.getId)
}

class RelationshipOperations extends BaseOperations[Relationship] {
Expand Down Expand Up @@ -417,7 +416,7 @@ final class TransactionBoundQueryContext(tc: TransactionalContextWrapper)
JavaConversionSupport.mapToScalaENFXSafe(tc.statement.readOperations().relationshipLegacyIndexQuery(name, query, -1, -1))(getById)

override def isDeleted(r: Relationship): Boolean =
tc.stateView.hasTxStateWithChanges && tc.stateView.txState().relationshipIsDeletedInThisTx(r.getId)
tc.stateView.readableTxState().relationshipIsDeletedInThisTx(r.getId)
}

override def getOrCreatePropertyKeyId(propertyKey: String) =
Expand Down
Expand Up @@ -351,8 +351,7 @@ final class TransactionBoundQueryContext(txContext: TransactionalContextWrapper)

override def isDeletedInThisTx(n: Node): Boolean = isDeletedInThisTx(n.getId)

def isDeletedInThisTx(id: Long): Boolean =
txContext.stateView.hasTxStateWithChanges && txContext.stateView.txState().nodeIsDeletedInThisTx(id)
def isDeletedInThisTx(id: Long): Boolean = txContext.stateView.readableTxState().nodeIsDeletedInThisTx(id)

override def acquireExclusiveLock(obj: Long) =
txContext.statement.readOperations().acquireExclusive(ResourceTypes.NODE, obj)
Expand Down Expand Up @@ -429,7 +428,7 @@ final class TransactionBoundQueryContext(txContext: TransactionalContextWrapper)
isDeletedInThisTx(r.getId)

def isDeletedInThisTx(id: Long): Boolean =
txContext.stateView.hasTxStateWithChanges && txContext.stateView.txState().relationshipIsDeletedInThisTx(id)
txContext.stateView.readableTxState().relationshipIsDeletedInThisTx(id)

override def acquireExclusiveLock(obj: Long) =
txContext.statement.readOperations().acquireExclusive(ResourceTypes.RELATIONSHIP, obj)
Expand Down
Expand Up @@ -358,8 +358,7 @@ final class TransactionBoundQueryContext(val transactionalContext: Transactional

override def isDeletedInThisTx(n: Node): Boolean = isDeletedInThisTx(n.getId)

def isDeletedInThisTx(id: Long): Boolean =
transactionalContext.stateView.hasTxStateWithChanges && transactionalContext.stateView.txState().nodeIsDeletedInThisTx(id)
def isDeletedInThisTx(id: Long): Boolean = transactionalContext.stateView.readableTxState().nodeIsDeletedInThisTx(id)

override def acquireExclusiveLock(obj: Long) =
transactionalContext.statement.readOperations().acquireExclusive(ResourceTypes.NODE, obj)
Expand Down Expand Up @@ -436,7 +435,7 @@ final class TransactionBoundQueryContext(val transactionalContext: Transactional
isDeletedInThisTx(r.getId)

def isDeletedInThisTx(id: Long): Boolean =
transactionalContext.stateView.hasTxStateWithChanges && transactionalContext.stateView.txState().relationshipIsDeletedInThisTx(id)
transactionalContext.stateView.readableTxState().relationshipIsDeletedInThisTx(id)

override def acquireExclusiveLock(obj: Long) =
transactionalContext.statement.readOperations().acquireExclusive(ResourceTypes.RELATIONSHIP, obj)
Expand Down
Expand Up @@ -79,7 +79,8 @@ class TriadicIntegrationTest extends ExecutionEngineFunSuite {

test("triadic should not handle complex incoming predicates for now") {
// given
execute("CREATE INDEX ON :Person(name)")
graph.createIndex( "Person", "name ")

execute( """CREATE (a:Person{name:"a"}), (b:Person{name:"b"}), (c:Person{name:"c",age:39}), (d:Person{name:"d"}), (e:Person{name:"e"})
|CREATE (a)-[:FRIEND]->(b), (b)-[:FRIEND]->(c), (b)-[:FRIEND]->(d), (b)-[:FRIEND]->(e)
|CREATE (a)-[:FRIEND]->(c), (a)-[:FRIEND]->(d), (c)-[:FRIEND]->(d)""".stripMargin)
Expand Down
Expand Up @@ -34,7 +34,6 @@
import org.neo4j.storageengine.api.txstate.ReadableTransactionState;
import org.neo4j.storageengine.api.txstate.TxStateVisitor;

import static org.neo4j.function.Predicates.ALWAYS_TRUE_INT;
import static org.neo4j.kernel.api.ReadOperations.ANY_LABEL;
import static org.neo4j.kernel.api.ReadOperations.ANY_RELATIONSHIP_TYPE;

Expand Down Expand Up @@ -66,7 +65,8 @@ public void visitCreatedNode( long id )
public void visitDeletedNode( long id )
{
counts.incrementNodeCount( ANY_LABEL, -1 );
storeLayer.nodeCursor( statement, id, null ).forAll( this::decrementCountForLabelsAndRelationships );
storeLayer.nodeCursor( statement, id, ReadableTransactionState.EMPTY )
.forAll( this::decrementCountForLabelsAndRelationships );
super.visitDeletedNode( id );
}

Expand Down Expand Up @@ -94,7 +94,8 @@ public void visitCreatedRelationship( long id, int type, long startNode, long en
@Override
public void visitDeletedRelationship( long id )
{
try ( Cursor<RelationshipItem> cursor = storeLayer.relationshipCursor( statement, id, null ) )
try ( Cursor<RelationshipItem> cursor = storeLayer
.relationshipCursor( statement, id, ReadableTransactionState.EMPTY ) )
{
if ( !cursor.next() )
{
Expand Down Expand Up @@ -124,7 +125,7 @@ public void visitNodeLabelChanges( long id, final Set<Integer> added, final Set<
}
// get the relationship counts from *before* this transaction,
// the relationship changes will compensate for what happens during the transaction
storeLayer.nodeCursor( statement, id, null )
storeLayer.nodeCursor( statement, id, ReadableTransactionState.EMPTY )
.forAll( node -> storeLayer.degrees( statement, node, ( type, out, in ) ->
{
added.forEach( label -> updateRelationshipsCountsFromDegrees( type, label, out, in ) );
Expand Down
Expand Up @@ -19,14 +19,8 @@
*/
package org.neo4j.kernel.api.txstate;

import org.neo4j.kernel.api.properties.DefinedProperty;
import org.neo4j.kernel.api.properties.Property;
import org.neo4j.kernel.api.schema.LabelSchemaDescriptor;
import org.neo4j.kernel.api.schema.OrderedPropertyValues;
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.ReadableTransactionState;
import org.neo4j.storageengine.api.txstate.WritableTransactionState;

/**
* Kernel transaction state, please see {@link org.neo4j.kernel.impl.api.state.TxState} for implementation details.
Expand All @@ -36,62 +30,6 @@
* This naming convention helps deciding where to set {@link #hasChanges()} in the
* {@link org.neo4j.kernel.impl.api.state.TxState main implementation class}.
*/
public interface TransactionState extends ReadableTransactionState
public interface TransactionState extends ReadableTransactionState, WritableTransactionState
{
// ENTITY RELATED

void relationshipDoCreate( long id, int relationshipTypeId, long startNodeId, long endNodeId );

void nodeDoCreate( long id );

void relationshipDoDelete( long relationshipId, int type, long startNode, long endNode );

void relationshipDoDeleteAddedInThisTx( long relationshipId );

void nodeDoDelete( long nodeId );

void nodeDoAddProperty( long nodeId, DefinedProperty newProperty );

void nodeDoChangeProperty( long nodeId, DefinedProperty replacedProperty, DefinedProperty newProperty );

void relationshipDoReplaceProperty( long relationshipId,
Property replacedProperty, DefinedProperty newProperty );

void graphDoReplaceProperty( Property replacedProperty, DefinedProperty newProperty );

void nodeDoRemoveProperty( long nodeId, DefinedProperty removedProperty );

void relationshipDoRemoveProperty( long relationshipId, DefinedProperty removedProperty );

void graphDoRemoveProperty( DefinedProperty removedProperty );

void nodeDoAddLabel( int labelId, long nodeId );

void nodeDoRemoveLabel( int labelId, long nodeId );

// TOKEN RELATED

void labelDoCreateForName( String labelName, int id );

void propertyKeyDoCreateForName( String propertyKeyName, int id );

void relationshipTypeDoCreateForName( String relationshipTypeName, int id );

// SCHEMA RELATED

void indexRuleDoAdd( IndexDescriptor descriptor );

void indexDoDrop( IndexDescriptor descriptor );

boolean indexDoUnRemove( IndexDescriptor constraint );

void constraintDoAdd( ConstraintDescriptor constraint );

void constraintDoAdd( IndexBackedConstraintDescriptor constraint, long indexId );

void constraintDoDrop( ConstraintDescriptor constraint );

boolean constraintDoUnRemove( ConstraintDescriptor constraint );

void indexDoUpdateEntry( LabelSchemaDescriptor descriptor, long nodeId, OrderedPropertyValues before, OrderedPropertyValues after );
}
Expand Up @@ -19,9 +19,12 @@
*/
package org.neo4j.kernel.api.txstate;

import org.neo4j.storageengine.api.txstate.ReadableTransactionState;
import org.neo4j.storageengine.api.txstate.WritableTransactionState;

public interface TxStateHolder
{
TransactionState txState();
ReadableTransactionState readableTxState();
WritableTransactionState writableTxState();
LegacyIndexTransactionState legacyIndexTxState();
boolean hasTxStateWithChanges();
}
Expand Up @@ -45,6 +45,8 @@
import org.neo4j.kernel.impl.locking.StatementLocks;
import org.neo4j.kernel.impl.proc.Procedures;
import org.neo4j.storageengine.api.StorageStatement;
import org.neo4j.storageengine.api.txstate.ReadableTransactionState;
import org.neo4j.storageengine.api.txstate.WritableTransactionState;

/**
* A resource efficient implementation of {@link Statement}. Designed to be reused within a
Expand Down Expand Up @@ -150,21 +152,21 @@ public QueryRegistryOperations queryRegistration()
}

@Override
public TransactionState txState()
public ReadableTransactionState readableTxState()
{
return txStateHolder.txState();
return txStateHolder.readableTxState();
}

@Override
public LegacyIndexTransactionState legacyIndexTxState()
public WritableTransactionState writableTxState()
{
return txStateHolder.legacyIndexTxState();
return txStateHolder.writableTxState();
}

@Override
public boolean hasTxStateWithChanges()
public LegacyIndexTransactionState legacyIndexTxState()
{
return txStateHolder.hasTxStateWithChanges();
return txStateHolder.legacyIndexTxState();
}

@Override
Expand Down
Expand Up @@ -67,7 +67,9 @@
import org.neo4j.storageengine.api.StorageEngine;
import org.neo4j.storageengine.api.StorageStatement;
import org.neo4j.storageengine.api.StoreReadLayer;
import org.neo4j.storageengine.api.txstate.ReadableTransactionState;
import org.neo4j.storageengine.api.txstate.TxStateVisitor;
import org.neo4j.storageengine.api.txstate.WritableTransactionState;

import static org.neo4j.storageengine.api.TransactionApplicationMode.INTERNAL;

Expand Down Expand Up @@ -353,7 +355,7 @@ private void dropCreatedConstraintIndexes() throws TransactionFailureException
{
if ( hasTxStateWithChanges() )
{
for ( IndexDescriptor createdConstraintIndex : txState().constraintIndexesCreatedInTx() )
for ( IndexDescriptor createdConstraintIndex : readableTxState().constraintIndexesCreatedInTx() )
{
try
{
Expand All @@ -370,7 +372,13 @@ private void dropCreatedConstraintIndexes() throws TransactionFailureException
}

@Override
public TransactionState txState()
public ReadableTransactionState readableTxState()
{
return txState == null ? ReadableTransactionState.EMPTY : txState;
}

@Override
public WritableTransactionState writableTxState()
{
if ( txState == null )
{
Expand All @@ -387,8 +395,7 @@ public LegacyIndexTransactionState legacyIndexTxState()
(legacyIndexTransactionState = legacyIndexTxStateSupplier.get());
}

@Override
public boolean hasTxStateWithChanges()
private boolean hasTxStateWithChanges()
{
return txState != null && txState.hasChanges();
}
Expand Down
Expand Up @@ -525,15 +525,15 @@ public String indexGetFailure( Statement state, IndexDescriptor descriptor )

private void acquireExclusiveNodeLock( KernelStatement state, long nodeId )
{
if ( !state.hasTxStateWithChanges() || !state.txState().nodeIsAddedInThisTx( nodeId ) )
if ( !state.readableTxState().nodeIsAddedInThisTx( nodeId ) )
{
state.locks().optimistic().acquireExclusive( state.lockTracer(), ResourceTypes.NODE, nodeId );
}
}

private void acquireExclusiveRelationshipLock( KernelStatement state, long relationshipId )
{
if ( !state.hasTxStateWithChanges() || !state.txState().relationshipIsAddedInThisTx( relationshipId ) )
if ( !state.readableTxState().relationshipIsAddedInThisTx( relationshipId ) )
{
state.locks().optimistic().acquireExclusive( state.lockTracer(), ResourceTypes.RELATIONSHIP, relationshipId );
}
Expand Down

0 comments on commit 61d45b3

Please sign in to comment.