Re: [Neo] Checking if relationship exists between two nodes

2010-04-26 Thread Craig Taverner
>
> Yes, I understood that. But I think you should be explicit about which
> relationship types you *expect* to remove. This means that if the node had
> relationships of types that you didn't expect those will not be removed and
> an exception will be thrown when you commit the transaction. This is a good
> thing, since it helps you detect errors in your code. If for example the
> node is part of an in-graph data structure, it would probably have to be
> gracefully removed from that data structure, just removing all of the
> relationships from the node would probably break the data structure. I
> would
> much prefer the utility methods to aid you by signaling that you forgot to
> remove the node, by throwing an exception, than to silently break your data
> structures.
>

One convenient convention I use that is a kind of compromise here is that I
assume each node has some level of ownership of the outgoing relationships,
but not the incoming ones. So I delete all incoming relationships directly,
but only explicitly delete the outgoing relationships of the types I expect,
before finally deleting the node. So I don't care if there is a mistake in
the incoming relationships, but do care if there is a mistake in the
outgoing ones.

I guess this pattern may only suite the conventions of my data model.
___
Neo mailing list
User@lists.neo4j.org
https://lists.neo4j.org/mailman/listinfo/user


Re: [Neo] Checking if relationship exists between two nodes

2010-04-26 Thread Tobias Ivarsson
On Mon, Apr 26, 2010 at 7:38 AM, Rick Bullotta <
rick.bullo...@burningskysoftware.com> wrote:

> Hi, Tobias.  I was only referring to the case when it was the intent to
> delete the node also.  In that case, you *have* to delete all the
> relationships to the node in order to delete the node.  No worries, it is
> easy enough to implement!
>

Yes, I understood that. But I think you should be explicit about which
relationship types you *expect* to remove. This means that if the node had
relationships of types that you didn't expect those will not be removed and
an exception will be thrown when you commit the transaction. This is a good
thing, since it helps you detect errors in your code. If for example the
node is part of an in-graph data structure, it would probably have to be
gracefully removed from that data structure, just removing all of the
relationships from the node would probably break the data structure. I would
much prefer the utility methods to aid you by signaling that you forgot to
remove the node, by throwing an exception, than to silently break your data
structures.

On Mon, Apr 26, 2010 at 12:08 AM, Niels Hoogeveen  wrote:

>
> 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 );}
>
>
Yes, and I will not stop you, I just think that the utilities we endorse
should be utilities that encourage good code (see above).

-- 
Tobias Ivarsson 
Hacker, Neo Technology
www.neotechnology.com
Cellphone: +46 706 534857
___
Neo mailing list
User@lists.neo4j.org
https://lists.neo4j.org/mailman/listinfo/user


Re: [Neo] Checking if relationship exists between two nodes

2010-04-25 Thread Rick Bullotta
Hi, Tobias.  I was only referring to the case when it was the intent to
delete the node also.  In that case, you *have* to delete all the
relationships to the node in order to delete the node.  No worries, it is
easy enough to implement!


-Original Message-
From: user-boun...@lists.neo4j.org [mailto:user-boun...@lists.neo4j.org] On
Behalf Of Tobias Ivarsson
Sent: Sunday, April 25, 2010 5:53 PM
To: Neo user discussions
Subject: Re: [Neo] Checking if relationship exists between two nodes

On Sun, Apr 25, 2010 at 10:42 PM, Mattias Persson  wrote:

> 2010/4/25, Rick Bullotta :
> > 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-boun...@lists.neo4j.org [mailto:user-boun...@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, [matt...@neotechnology.com]
> > Hacker, Neo Technology
> > www.neotechnology.com
> > ___
> > Neo mailing list
> > User@lists.neo4j.org
> > https://lists.neo4j.org/mailman/listinfo/user
> >
> > ___
> > Neo mailing list
> > User@lists.neo4j.org
> > https://lists.neo4j.org/mailman/listinfo/user
> >
>
>
> --
> Mattias Persson, [matt...@neotechnology.com]
> Hacker, Neo Technology
> www.neotechnology.com
> ___
> Neo mailing list
> User@lists.neo4j.org
> https://lists.neo4j.org/mailman/listinfo/user
>



-- 
Tobias Ivarsson 
Hacker, Neo Technology
www.neotechnology.com
Cellphone: +46 706 534857
___
Neo mailing list
User@lists.neo4j.org
https://lists.neo4j.org/mailman/listinfo/user

___
Neo mailing list
User@lists.neo4j.org
https://lists.neo4j.org/mailman/listinfo/user


Re: [Neo] Checking if relationship exists between two nodes

2010-04-25 Thread Niels Hoogeveen

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.ivars...@neotechnology.com
> Date: Sun, 25 Apr 2010 23:52:45 +0200
> To: user@lists.neo4j.org
> Subject: Re: [Neo] Checking if relationship exists between two nodes
> 
> On Sun, Apr 25, 2010 at 10:42 PM, Mattias Persson  > wrote:
> 
> > 2010/4/25, Rick Bullotta :
> > > 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-boun...@lists.neo4j.org [mailto:user-boun...@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, [matt...@neotechnology.com]
> > > Hacker, Neo Technology
> > > www.neotechnology.com
> > > ___
> > > Neo mailing list
> > > User@lists.neo4j.org
> > > https://lists.neo4j.org/mailman/listinfo/user
> > >
> > > ___
> > > Neo mailing list
> > > User@lists.neo4j.org
> > > https://lists.neo4j.org/mailman/listinfo/user
> > >
> >
> >
> > --
> > Mattias Persson, [matt...@neotechnology.com]
> > Hacker, Neo Technology
> > www.neotechnology.com
> > ___
> > Neo mailing list
> > User@lists.neo4j.org
> > https://lists.neo4j.org/mailman/listinfo/user
> >
> 
> 
> 
> -- 
> Tobias Ivarsson 
> Hacker, Neo Technology
> www.neotechnology.com
> Cellphone: +46 706 534857
> ___
> Neo mailing list
> User@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
___
Neo mailing list
User@lists.neo4j.org
https://lists.neo4j.org/mailman/listinfo/user


Re: [Neo] Checking if relationship exists between two nodes

2010-04-25 Thread Tobias Ivarsson
On Sun, Apr 25, 2010 at 10:42 PM, Mattias Persson  wrote:

> 2010/4/25, Rick Bullotta :
> > 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-boun...@lists.neo4j.org [mailto:user-boun...@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, [matt...@neotechnology.com]
> > Hacker, Neo Technology
> > www.neotechnology.com
> > ___
> > Neo mailing list
> > User@lists.neo4j.org
> > https://lists.neo4j.org/mailman/listinfo/user
> >
> > ___
> > Neo mailing list
> > User@lists.neo4j.org
> > https://lists.neo4j.org/mailman/listinfo/user
> >
>
>
> --
> Mattias Persson, [matt...@neotechnology.com]
> Hacker, Neo Technology
> www.neotechnology.com
> ___
> Neo mailing list
> User@lists.neo4j.org
> https://lists.neo4j.org/mailman/listinfo/user
>



-- 
Tobias Ivarsson 
Hacker, Neo Technology
www.neotechnology.com
Cellphone: +46 706 534857
___
Neo mailing list
User@lists.neo4j.org
https://lists.neo4j.org/mailman/listinfo/user


Re: [Neo] Checking if relationship exists between two nodes

2010-04-25 Thread Mattias Persson
2010/4/25, Rick Bullotta :
> 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
>
>
> -Original Message-
> From: user-boun...@lists.neo4j.org [mailto:user-boun...@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, [matt...@neotechnology.com]
> Hacker, Neo Technology
> www.neotechnology.com
> ___
> Neo mailing list
> User@lists.neo4j.org
> https://lists.neo4j.org/mailman/listinfo/user
>
> ___
> Neo mailing list
> User@lists.neo4j.org
> https://lists.neo4j.org/mailman/listinfo/user
>


-- 
Mattias Persson, [matt...@neotechnology.com]
Hacker, Neo Technology
www.neotechnology.com
___
Neo mailing list
User@lists.neo4j.org
https://lists.neo4j.org/mailman/listinfo/user


Re: [Neo] Checking if relationship exists between two nodes

2010-04-25 Thread Rick Bullotta
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.

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.



-Original Message-
From: user-boun...@lists.neo4j.org [mailto:user-boun...@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, [matt...@neotechnology.com]
Hacker, Neo Technology
www.neotechnology.com
___
Neo mailing list
User@lists.neo4j.org
https://lists.neo4j.org/mailman/listinfo/user

___
Neo mailing list
User@lists.neo4j.org
https://lists.neo4j.org/mailman/listinfo/user