}
if ( nodeToRemove.equals( underlyingNode ) )
{
throw new IllegalArgumentException( "Cannot remove underlying node" );
}
Transaction tx = graphDb.beginTx();
try
{
Relationship instanceRel = null;
for ( Relationship rel : nodeToRemove.getRelationships( RelTypes.TIMELINE_INSTANCE ) )
{
if ( rel.getProperty( TIMELINE_NAME, "" ).equals( name ) )
{
assert instanceRel == null;
instanceRel = rel;
}
}
if ( instanceRel == null )
{
throw new IllegalArgumentException(
"Node[" + nodeToRemove.getId()
+ "] not added to Timeline[" + name + "]" );
}
Node node = instanceRel.getStartNode();
instanceRel.delete();
if ( firstNode != null && firstNode.equals( nodeToRemove ) )
{
firstNode = null;
}
if ( lastNode != null && lastNode.equals( nodeToRemove ) )
{
lastNode = null;
}
if ( node.getRelationships( RelTypes.TIMELINE_INSTANCE ).iterator().hasNext() )
{
// still have instances connected to this time
return;
}
Relationship incoming = node.getSingleRelationship(
RelTypes.TIMELINE_NEXT_ENTRY, Direction.INCOMING );
if ( incoming == null )
{
throw new RuntimeException( "No incoming relationship of "
+ RelTypes.TIMELINE_NEXT_ENTRY
+ " found" );
}
Relationship outgoing = node.getSingleRelationship(
RelTypes.TIMELINE_NEXT_ENTRY, Direction.OUTGOING );
if ( outgoing == null )
{
throw new RuntimeException( "No outgoing relationship of "
+ RelTypes.TIMELINE_NEXT_ENTRY
+ " found" );
}
Node previous = incoming.getStartNode();
Node next = outgoing.getEndNode();
incoming.delete();
outgoing.delete();
// TODO: this needs proper synchronization
if ( node.hasProperty( INDEX_COUNT ) )
{
long nodeId = (Long) indexBTree.removeEntry( (Long) node.getProperty( TIMESTAMP ) );
assert nodeId == node.getId();
int count = (Integer) node.getProperty( INDEX_COUNT );
count--;
if ( !previous.equals( underlyingNode )
&& !previous.hasProperty( INDEX_COUNT ) )
{
previous.setProperty( INDEX_COUNT, count );
indexBTree.addEntry(
(Long) previous.getProperty( TIMESTAMP ),
previous.getId() );
}
}
else
{
long timestamp = (Long) node.getProperty( TIMESTAMP );
if ( indexed )
{
Long nodeId = (Long) indexBTree.getClosestHigherEntry( timestamp );
if ( nodeId != null )
{
Node indexedNode = graphDb.getNodeById( nodeId );
int count = (Integer) indexedNode.getProperty( INDEX_COUNT );
count--;
indexedNode.setProperty( INDEX_COUNT, count );
}
else
{
if ( underlyingNode.hasProperty( INDEX_COUNT ) )
{
int count = (Integer) underlyingNode.getProperty( INDEX_COUNT );
count--;
underlyingNode.setProperty( INDEX_COUNT, count );
}
}
}
}
node.delete();
if ( !previous.equals( next ) )
{
previous.createRelationshipTo( next,
RelTypes.TIMELINE_NEXT_ENTRY );
}
tx.success();
}
finally
{
if(transactional)
{
tx.finish();
}
}
}