/** * A convenience operation that, given an id of a node that is attached to this * relationship, returns the id of the other node. For example if <code>id</code> is * the start node id, the end node id will be returned, and vice versa. * <p> * This operation will throw a runtime exception if <code>id</code> is * not the id of either of this relationship's nodes. * <p> * Note that this id can get reused in the future, if this relationship and the given node are deleted. * * @param id the id of the start or end node of this relationship * @return the id of the other node * @throws RuntimeException if the given node id is not the id of either the start or end * node of this relationship. */ default long getOtherNodeId( long id ) { long start = getStartNodeId(); long end = getEndNodeId(); if ( id == start ) { return end; } else if ( id == end ) { return start; } throw new NotFoundException( "Node[" + id + "] not connected to this relationship[" + getId() + "]" ); } }
@Test public void shouldIterateThroughRelationshipsInReverse() { // given Path path = new PathProxy( proxySPI, new long[] {1, 2, 3}, new long[] {100, 200}, new int[] {0, ~0} ); Iterator<Relationship> iterator = path.reverseRelationships().iterator(); Relationship relationship; // then assertTrue( iterator.hasNext() ); assertThat( relationship = iterator.next(), instanceOf( Relationship.class ) ); assertEquals( 200, relationship.getId() ); assertEquals( 3, relationship.getStartNodeId() ); assertEquals( 2, relationship.getEndNodeId() ); assertTrue( iterator.hasNext() ); assertThat( relationship = iterator.next(), instanceOf( Relationship.class ) ); assertEquals( 100, relationship.getId() ); assertEquals( 1, relationship.getStartNodeId() ); assertEquals( 2, relationship.getEndNodeId() ); assertFalse( iterator.hasNext() ); }
@Test public void shouldIterateThroughRelationships() { // given Path path = new PathProxy( proxySPI, new long[] {1, 2, 3}, new long[] {100, 200}, new int[] {0, ~0} ); Iterator<Relationship> iterator = path.relationships().iterator(); Relationship relationship; // then assertTrue( iterator.hasNext() ); assertThat( relationship = iterator.next(), instanceOf( Relationship.class ) ); assertEquals( 100, relationship.getId() ); assertEquals( 1, relationship.getStartNodeId() ); assertEquals( 2, relationship.getEndNodeId() ); assertTrue( iterator.hasNext() ); assertThat( relationship = iterator.next(), instanceOf( Relationship.class ) ); assertEquals( 200, relationship.getId() ); assertEquals( 3, relationship.getStartNodeId() ); assertEquals( 2, relationship.getEndNodeId() ); assertFalse( iterator.hasNext() ); }
private boolean relationshipExistsByQuery( RelationshipIndex index, Node startNode, Node endNode, boolean specifyStartNode ) { boolean found = false; try ( Transaction tx = db.beginTx(); IndexHits<Relationship> query = index .query( "key", QueryContext.numericRange( "key", 0, 3 ), specifyStartNode ? startNode : null, null ) ) { for ( Relationship relationship : query ) { if ( relationship.getStartNodeId() == startNode.getId() && relationship.getEndNodeId() == endNode.getId() ) { found = true; break; } } tx.success(); } return found; }
private Set<Long> getKeywordIds(Node annotatedText) { Set<Long> ids = new HashSet<>(); if (annotatedText != null) { annotatedText.getRelationships(Direction.INCOMING, Relationships.DESCRIBES).forEach(relationship -> { ids.add(relationship.getStartNodeId()); }); } return ids; }
/** * A convenience operation that, given an id of a node that is attached to this * relationship, returns the id of the other node. For example if <code>id</code> is * the start node id, the end node id will be returned, and vice versa. * <p> * This operation will throw a runtime exception if <code>id</code> is * not the id of either of this relationship's nodes. * <p> * Note that this id can get reused in the future, if this relationship and the given node are deleted. * * @param id the id of the start or end node of this relationship * @return the id of the other node * @throws RuntimeException if the given node id is not the id of either the start or end * node of this relationship. */ default long getOtherNodeId( long id ) { long start = getStartNodeId(); long end = getEndNodeId(); if ( id == start ) { return end; } else if ( id == end ) { return start; } throw new NotFoundException( "Node[" + id + "] not connected to this relationship[" + getId() + "]" ); } }