[Neo] Checking if relationship exists between two nodes

Niels Hoogeveen pd_aficionado at hotmail.com
Mon Apr 26 00:08:48 CEST 2010


The moment there is a utility class SomeUtilityClass.delete( Node node, RelationshipType... expectedTypes ), the following "unsafe" construct is easily written:
for(RelationshipType rt : graphDb.getRelationshipTypes()){  SomeUtilityClass.delete( node, rt );}

> From: tobias.ivarsson at neotechnology.com
> Date: Sun, 25 Apr 2010 23:52:45 +0200
> To: user at lists.neo4j.org
> Subject: Re: [Neo] Checking if relationship exists between two nodes
> 
> On Sun, Apr 25, 2010 at 10:42 PM, Mattias Persson <mattias at neotechnology.com
> > wrote:
> 
> > 2010/4/25, Rick Bullotta <rick.bullotta at burningskysoftware.com>:
> > > Cool, Mattias.  I just ran into that requirement again today.
> > >
> > > This is also another good case for the cached/persisted "number of
> > > relationships" counter on a node that has also been discussed. It would
> > > enable the optimization to occur automatically.
> > Although for this method it doesn't matter, sort of, because it'll
> > iterate from both nodes in parallell (unless it immediately finds it
> > from the first direction, within the first 50 or so)
> > >
> > > Another API function that would be useful (though it is also easy to
> > > implement) is one that automatically deletes all relationships for a
> > node,
> > > then deletes the node, since that is the required path of action anyway
> > to
> > > delete a node.
> > >
> > well, yeah maybe. I don't think I've ever wanted to do that so I
> > haven't created a utility method for it. But it may be time to add it
> >
> 
> I think just removing all relationships of a node is a bit unsafe. I would
> prefer if you had to supply the RelationshipTypes to delete, so that you
> don't destroy unexpected structures.
> 
> SomeUtilityClass.delete( Node node, RelationshipType... expectedTypes )
> 
> >
> > >
> > > -----Original Message-----
> > > From: user-bounces at lists.neo4j.org [mailto:user-bounces at lists.neo4j.org]
> > On
> > > Behalf Of Mattias Persson
> > > Sent: Sunday, April 25, 2010 3:52 PM
> > > To: Neo4j user discussions
> > > Subject: [Neo] Checking if relationship exists between two nodes
> > >
> > > As you all (probably) know the Neo4j API has no method
> > > Node#hasRelationshipTo(Node otherNode) and to find out such a thing could
> > > potentially be slow if both the nodes has many relationships (of the
> > > type/direction you want to check).
> > >
> > > I just created a utility for that which starts to iterate from the node
> > (of
> > > the two) that the developer thinks/knows has the least relationships. If
> > a
> > > certain amount of relationships have been iterated and not yet found a
> > new
> > > thread is spawned which starts to iterate from the other node in the
> > > opposite direction. The method will return as soon as the first thread
> > finds
> > > a match. I'd guess that's the fastest way currently to do that. The next
> > > level of this would be to add indexing for relationships (which is an
> > > upcoming feature) and go via that instead.
> > >
> > > So the code may look like:
> > >
> > > (it's in the neo4j-utils component)
> > >
> > >   GraphDatabaseUtil graphDbUtil = new GraphDatabaseUtil( graphDb );
> > >   boolean exists = graphDbUtil.relationshipExistsBetween( node1, node2,
> > >       MyRelTypes.MY_TYPE, Direction.OUTGOING );
> > >
> > > --
> > > Mattias Persson, [mattias at neotechnology.com]
> > > Hacker, Neo Technology
> > > www.neotechnology.com
> > > _______________________________________________
> > > Neo mailing list
> > > User at lists.neo4j.org
> > > https://lists.neo4j.org/mailman/listinfo/user
> > >
> > > _______________________________________________
> > > Neo mailing list
> > > User at lists.neo4j.org
> > > https://lists.neo4j.org/mailman/listinfo/user
> > >
> >
> >
> > --
> > Mattias Persson, [mattias at neotechnology.com]
> > Hacker, Neo Technology
> > www.neotechnology.com
> > _______________________________________________
> > Neo mailing list
> > User at lists.neo4j.org
> > https://lists.neo4j.org/mailman/listinfo/user
> >
> 
> 
> 
> -- 
> Tobias Ivarsson <tobias.ivarsson at neotechnology.com>
> Hacker, Neo Technology
> www.neotechnology.com
> Cellphone: +46 706 534857
> _______________________________________________
> Neo mailing list
> User at lists.neo4j.org
> https://lists.neo4j.org/mailman/listinfo/user
 		 	   		  
_________________________________________________________________
New Windows 7: Simplify what you do everyday. Find the right PC for you.
http://windows.microsoft.com/shop


More information about the User mailing list