Permalink
Browse files

Reduced the number of map lookups for a getProperty call if done outs…

…ide a

transaction or inside a transaction which has got no changes.

Previously there were 8 (whereof 2 synchronized and 3 on concurrent
hashmap) map lookups per getProperty.
Now for read-only transactions or outside a transaction there is 3 whereof
no synchronized, and also reduced to 5 when inside transaction.

Conflicts:

	kernel/src/main/java/org/neo4j/kernel/impl/core/LockReleaser.java
	kernel/src/main/java/org/neo4j/kernel/impl/core/NodeImpl.java
	kernel/src/main/java/org/neo4j/kernel/impl/core/NodeManager.java
	kernel/src/main/java/org/neo4j/kernel/impl/core/RelationshipImpl.java
  • Loading branch information...
1 parent f7133b8 commit 870925ee25b598b61d275526a554441f4785b22b @tinwelint committed Aug 31, 2012
@@ -22,6 +22,8 @@
import java.util.HashMap;
import java.util.Map;
+import javax.transaction.Transaction;
+
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.PropertyContainer;
@@ -58,7 +60,8 @@ public GraphDatabaseService getGraphDatabase()
}
@Override
- protected PropertyData changeProperty( NodeManager nodeManager, PropertyData property, Object value )
+ protected PropertyData changeProperty( NodeManager nodeManager, PropertyData property, Object value,
+ Transaction tx)
{
return nodeManager.graphChangeProperty( property, value );
}
@@ -70,7 +73,7 @@ protected PropertyData addProperty( NodeManager nodeManager, PropertyIndex index
}
@Override
- protected void removeProperty( NodeManager nodeManager, PropertyData property )
+ protected void removeProperty( NodeManager nodeManager, PropertyData property, Transaction tx )
{
nodeManager.graphRemoveProperty( property );
}
@@ -27,6 +27,7 @@
import org.neo4j.graphdb.NotFoundException;
import org.neo4j.graphdb.Relationship;
import org.neo4j.helpers.collection.PrefetchingIterator;
+import org.neo4j.kernel.impl.util.ArrayMap;
import org.neo4j.kernel.impl.util.RelIdArray;
import org.neo4j.kernel.impl.util.RelIdArray.DirectionWrapper;
import org.neo4j.kernel.impl.util.RelIdIterator;
@@ -115,13 +116,15 @@ else if ( fromNode.getMoreRelationships( nodeManager ) ||
// initiate iterators for them
if ( allTypes )
{
+ ArrayMap<Integer, Collection<Long>> skipMap = nodeManager.getCowRelationshipRemoveMap(
+ fromNode, nodeManager.getCurrentTransactionIfHasChanges() );
for ( RelIdArray ids : fromNode.getRelationshipIds() )
{
int type = ids.getType();
RelIdIterator itr = newRels.get( type );
if ( itr == null )
{
- Collection<Long> remove = nodeManager.getCowRelationshipRemoveMap( fromNode, type );
+ Collection<Long> remove = skipMap != null ? skipMap.get( type ) : null;
itr = remove == null ? ids.iterator( direction ) :
RelIdArray.from( ids, null, remove ).iterator( direction );
newRels.put( type, itr );
@@ -32,14 +32,14 @@
import javax.transaction.Synchronization;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
-import javax.transaction.TransactionManager;
import org.neo4j.graphdb.NotInTransactionException;
import org.neo4j.graphdb.TransactionFailureException;
import org.neo4j.graphdb.event.TransactionData;
import org.neo4j.kernel.impl.nioneo.store.NameData;
import org.neo4j.kernel.impl.nioneo.store.PropertyData;
import org.neo4j.kernel.impl.nioneo.store.Record;
+import org.neo4j.kernel.impl.transaction.AbstractTransactionManager;
import org.neo4j.kernel.impl.transaction.LockManager;
import org.neo4j.kernel.impl.transaction.LockType;
import org.neo4j.kernel.impl.util.ArrayMap;
@@ -62,7 +62,7 @@
private NodeManager nodeManager;
private final LockManager lockManager;
- private final TransactionManager transactionManager;
+ private final AbstractTransactionManager transactionManager;
private PropertyIndexManager propertyIndexManager;
public void setNodeManager(NodeManager nodeManager)
@@ -227,7 +227,7 @@ public String toString()
}
public LockReleaser( LockManager lockManager,
- TransactionManager transactionManager,
+ AbstractTransactionManager transactionManager,
NodeManager nodeManager,
PropertyIndexManager propertyIndexManager)
{
@@ -291,7 +291,7 @@ public LockElement addLockToTransaction( Object resource, LockType type )
public LockElement addLockToTransaction( Object resource, LockType type, Transaction tx )
throws NotInTransactionException
{
- tx = (tx == null ? getTransaction() : tx);
+ tx = (tx == null ? getTransaction( true ) : tx);
List<LockElement> lockElements = lockMap.get( tx );
if ( lockElements != null )
@@ -330,11 +330,11 @@ public LockElement addLockToTransaction( Object resource, LockType type, Transac
}
}
- private Transaction getTransaction()
+ private Transaction getTransaction( boolean forWrite )
{
try
{
- return transactionManager.getTransaction();
+ return forWrite ? transactionManager.getTransaction() : transactionManager.getTransactionIfHasChanges();
}
catch ( SystemException e )
{
@@ -343,25 +343,23 @@ private Transaction getTransaction()
}
}
- public Collection<Long> getCowRelationshipRemoveMap( NodeImpl node, int type )
+ public ArrayMap<Integer, Collection<Long>> getCowRelationshipRemoveMap( NodeImpl node, Transaction tx )
{
- PrimitiveElement primitiveElement = cowMap.get( getTransaction() );
+ if ( tx == null )
+ return null;
+ PrimitiveElement primitiveElement = cowMap.get( tx );
if ( primitiveElement != null )
{
ArrayMap<Long,CowNodeElement> cowElements =
primitiveElement.nodes;
CowNodeElement element = cowElements.get( node.getId() );
- if ( element != null && element.relationshipRemoveMap != null )
- {
- return element.relationshipRemoveMap.get( type );
- }
+ return element != null ? element.relationshipRemoveMap : null;
}
return null;
}
-
- public Collection<Long> getOrCreateCowRelationshipRemoveMap( NodeImpl node, int type )
+ public Collection<Long> getOrCreateCowRelationshipRemoveMap( NodeImpl node, int type, Transaction tx )
{
- return getPrimitiveElement( true ).nodeElement( node.getId(), true ).getRelationshipRemoveMap( type, true );
+ return getPrimitiveElement( tx, true ).nodeElement( node.getId(), true ).getRelationshipRemoveMap( type, true );
}
public void setFirstIds( long nodeId, long firstRel, long firstProp )
@@ -371,28 +369,22 @@ public void setFirstIds( long nodeId, long firstRel, long firstProp )
nodeElement.firstProp = firstProp;
}
- public ArrayMap<Integer,RelIdArray> getCowRelationshipAddMap( NodeImpl node )
+ public ArrayMap<Integer,RelIdArray> getCowRelationshipAddMap( NodeImpl node, Transaction tx )
{
- PrimitiveElement primitiveElement = getPrimitiveElement( false );
+ PrimitiveElement primitiveElement = getPrimitiveElement( tx, false );
if ( primitiveElement == null ) return null;
CowNodeElement element = primitiveElement.nodeElement( node.getId(), false );
return element != null ? element.relationshipAddMap : null;
}
- public RelIdArray getCowRelationshipAddMap( NodeImpl node, int type )
- {
- ArrayMap<Integer, RelIdArray> map = getCowRelationshipAddMap( node );
- return map != null ? map.get( type ) : null;
- }
-
- public RelIdArray getOrCreateCowRelationshipAddMap( NodeImpl node, int type )
+ public RelIdArray getOrCreateCowRelationshipAddMap( NodeImpl node, int type, Transaction tx )
{
- return getPrimitiveElement( true ).nodeElement( node.getId(), true ).getRelationshipAddMap( type, true );
+ return getPrimitiveElement( tx, true ).nodeElement( node.getId(), true ).getRelationshipAddMap( type, true );
}
public void commit()
{
- commit(getTransaction());
+ commit(getTransaction( true ));
}
public void commit(Transaction tx)
@@ -403,14 +395,14 @@ public void commit(Transaction tx)
public void commitCows()
{
- Transaction tx = getTransaction();
+ Transaction tx = getTransaction( true );
propertyIndexManager.commit( tx );
releaseCows( tx, Status.STATUS_COMMITTED );
}
public void rollback()
{
- rollback(getTransaction());
+ rollback(getTransaction( true ));
}
public void rollback(Transaction tx)
@@ -528,26 +520,30 @@ public void dumpLocks()
}
public ArrayMap<Integer,PropertyData> getCowPropertyRemoveMap(
- Primitive primitive )
+ Primitive primitive, Transaction tx )
{
- PrimitiveElement primitiveElement = cowMap.get( getTransaction() );
+ if ( tx == null )
+ return null;
+ PrimitiveElement primitiveElement = cowMap.get( tx );
if ( primitiveElement == null ) return null;
CowEntityElement element = primitive.getEntityElement( primitiveElement, false );
return element != null ? element.getPropertyRemoveMap( false ) : null;
}
public ArrayMap<Integer,PropertyData> getCowPropertyAddMap(
- Primitive primitive )
+ Primitive primitive, Transaction tx )
{
- PrimitiveElement primitiveElement = cowMap.get( getTransaction() );
+ if ( tx == null )
+ return null;
+ PrimitiveElement primitiveElement = cowMap.get( tx );
if ( primitiveElement == null ) return null;
CowEntityElement element = primitive.getEntityElement( primitiveElement, false );
return element != null ? element.getPropertyAddMap( false ) : null;
}
public PrimitiveElement getPrimitiveElement( boolean create )
{
- return getPrimitiveElement( getTransaction(), create );
+ return getPrimitiveElement( getTransaction( create ), create );
}
public PrimitiveElement getPrimitiveElement( Transaction tx, boolean create )
@@ -566,20 +562,20 @@ public PrimitiveElement getPrimitiveElement( Transaction tx, boolean create )
}
public ArrayMap<Integer,PropertyData> getOrCreateCowPropertyAddMap(
- Primitive primitive )
+ Primitive primitive, Transaction tx )
{
- return primitive.getEntityElement( getPrimitiveElement( true ), true ).getPropertyAddMap( true );
+ return primitive.getEntityElement( getPrimitiveElement( tx, true ), true ).getPropertyAddMap( true );
}
public ArrayMap<Integer,PropertyData> getOrCreateCowPropertyRemoveMap(
- Primitive primitive )
+ Primitive primitive, Transaction tx )
{
- return primitive.getEntityElement( getPrimitiveElement( true ), true ).getPropertyRemoveMap( true );
+ return primitive.getEntityElement( getPrimitiveElement( tx, true ), true ).getPropertyRemoveMap( true );
}
- public void deletePrimitive( Primitive primitive )
+ public void deletePrimitive( Primitive primitive, Transaction tx )
{
- primitive.getEntityElement( getPrimitiveElement( true ), true ).deleted = true;
+ primitive.getEntityElement( getPrimitiveElement( tx, true ), true ).deleted = true;
}
public void removeNodeFromCache( long nodeId )
@@ -662,25 +658,26 @@ public void clearCache()
public TransactionData getTransactionData()
{
TransactionDataImpl result = new TransactionDataImpl();
- PrimitiveElement element = cowMap.get( getTransaction() );
+ Transaction tx = getTransaction( false );
+ PrimitiveElement element = cowMap.get( tx );
populateCreatedNodes( element, result );
if ( element == null )
{
return result;
}
if ( element.nodes != null )
{
- populateNodeRelEvent( element, result );
+ populateNodeRelEvent( element, result, tx );
}
if ( element.relationships != null )
{
- populateRelationshipPropertyEvents( element, result );
+ populateRelationshipPropertyEvents( element, result, tx );
}
return result;
}
private void populateRelationshipPropertyEvents( PrimitiveElement element,
- TransactionDataImpl result )
+ TransactionDataImpl result, Transaction tx )
{
for ( long relId : element.relationships.keySet() )
{
@@ -700,8 +697,8 @@ private void populateRelationshipPropertyEvents( PrimitiveElement element,
{
for ( PropertyData data : relElement.propertyAddMap.values() )
{
- String key = nodeManager.getKeyForProperty( data );
- Object oldValue = relImpl.getCommittedPropertyValue( nodeManager, key );
+ String key = nodeManager.getKeyForProperty( data, tx );
+ Object oldValue = relImpl.getCommittedPropertyValue( nodeManager, key, tx );
Object newValue = data.getValue();
result.assignedProperty( rel, key, newValue, oldValue );
}
@@ -710,11 +707,11 @@ private void populateRelationshipPropertyEvents( PrimitiveElement element,
{
for ( PropertyData data : relElement.propertyRemoveMap.values() )
{
- String key = nodeManager.getKeyForProperty( data );
+ String key = nodeManager.getKeyForProperty( data, tx );
Object oldValue = data.getValue();
if ( oldValue != null && !relElement.deleted )
{
- relImpl.getCommittedPropertyValue( nodeManager, key );
+ relImpl.getCommittedPropertyValue( nodeManager, key, tx );
}
result.removedProperty( rel, key, oldValue );
}
@@ -723,7 +720,7 @@ private void populateRelationshipPropertyEvents( PrimitiveElement element,
}
private void populateNodeRelEvent( PrimitiveElement element,
- TransactionDataImpl result )
+ TransactionDataImpl result, Transaction tx )
{
for ( long nodeId : element.nodes.keySet() )
{
@@ -769,8 +766,8 @@ private void populateNodeRelEvent( PrimitiveElement element,
{
for ( PropertyData data : nodeElement.propertyAddMap.values() )
{
- String key = nodeManager.getKeyForProperty( data );
- Object oldValue = nodeImpl.getCommittedPropertyValue( nodeManager, key );
+ String key = nodeManager.getKeyForProperty( data, tx );
+ Object oldValue = nodeImpl.getCommittedPropertyValue( nodeManager, key, tx );
Object newValue = data.getValue();
result.assignedProperty( node, key, newValue, oldValue );
}
@@ -779,11 +776,11 @@ private void populateNodeRelEvent( PrimitiveElement element,
{
for ( PropertyData data : nodeElement.propertyRemoveMap.values() )
{
- String key = nodeManager.getKeyForProperty( data );
+ String key = nodeManager.getKeyForProperty( data, tx );
Object oldValue = data.getValue();
if ( oldValue == null && !nodeElement.deleted )
{
- nodeImpl.getCommittedPropertyValue( nodeManager, key );
+ nodeImpl.getCommittedPropertyValue( nodeManager, key, tx );
}
result.removedProperty( node, key, oldValue );
}
@@ -828,25 +825,4 @@ private void populateCreatedNodes( PrimitiveElement element,
result.created( nodeManager.newNodeProxyById( nodeId ) );
}
}
-
- boolean hasRelationshipModifications( NodeImpl node )
- {
- Transaction tx = getTransaction();
- if ( tx == null )
- {
- return false;
- }
- PrimitiveElement primitiveElement = cowMap.get( tx );
- if ( primitiveElement != null )
- {
- ArrayMap<Long,CowNodeElement> cowElements =
- primitiveElement.nodes;
- CowNodeElement element = cowElements.get( node.getId() );
- if ( element != null && (element.relationshipAddMap != null || element.relationshipRemoveMap != null) )
- {
- return true;
- }
- }
- return false;
- }
}
Oops, something went wrong.

0 comments on commit 870925e

Please sign in to comment.