@Override protected Relationship obtainEntityInTransaction( GraphDatabaseService graphDatabaseService ) { return graphDatabaseService .createNode() .createRelationshipTo( graphDatabaseService.createNode(), withName( "foo" ) ); } }
private void createRelationshipsBetweenNodes( Node source, Node sink, int numberOfRelationships ) { for ( int i = 0; i < numberOfRelationships; i++ ) { source.createRelationshipTo( sink, RelationshipType.withName( "Type" + (i % 4) ) ) .setProperty( "" + i, i ); } } }
@Override public Long apply( GraphDatabaseService graphDb ) { try ( Transaction tx = graphDb.beginTx() ) { Node node = graphDb.createNode(); node.createRelationshipTo( graphDb.createNode(), withName( "KNOWS" ) ); node.createRelationshipTo( graphDb.createNode(), withName( "KNOWS" ) ); long whatThisThreadSees = countsForRelationship( null, null, null ); barrier.reached(); tx.success(); return whatThisThreadSees; } } }, graphDb );
@Test @Graph( { "a TO b", "b TO c", "c TO a" } ) public void canCreateGraphFromMultipleStrings() { Map<String,Node> graph = data.get(); Set<Node> unique = new HashSet<>(); Node n = graph.get( "a" ); while ( unique.add( n ) ) { try ( Transaction ignored = graphdb.beginTx() ) { n = n.getSingleRelationship( RelationshipType.withName( "TO" ), Direction.OUTGOING ).getEndNode(); } } assertEquals( graph.size(), unique.size() ); }
@Test public void shouldGiveHelpfulExceptionWhenDeletingNodeWithRels() { // Given Node node; node = db.createNode(); Node node2 = db.createNode(); node.createRelationshipTo( node2, RelationshipType.withName( "MAYOR_OF" ) ); tx.success(); // And given a transaction deleting just the node tx.begin(); node.delete(); // Expect exception.expect( ConstraintViolationException.class ); exception.expectMessage( "Cannot delete node<" + node.getId() + ">, because it still has relationships. " + "To delete this node, you must first delete its relationships." ); // When I commit tx.success(); }
@Test public void nodeGlobalUniqueness() { /* * (a)-TO->(b)-TO->(c) * \----TO---->/ */ createGraph( "a TO b", "a TO c", "b TO c" ); RelationshipType to = withName( "TO" ); try ( Transaction tx = beginTx() ) { Node a = getNodeWithName( "a" ); Node c = getNodeWithName( "c" ); Iterator<Path> path = getGraphDb().traversalDescription().relationships( to, OUTGOING ).uniqueness( NODE_GLOBAL ).evaluator( includeWhereEndNodeIs( c ) ).traverse( a ).iterator(); Path thePath = path.next(); assertFalse( path.hasNext() ); NodePathRepresentation pathRepresentation = new NodePathRepresentation( NAME_PROPERTY_REPRESENTATION ); assertEquals( "a,b,c", pathRepresentation.represent( thePath ) ); tx.success(); } }
@Before public void setup() { db = (GraphDatabaseFacade) new TestGraphDatabaseFactory().newImpermanentDatabase(); try ( Transaction tx = db.beginTx() ) { Node node = db.createNode(); node.createRelationshipTo( db.createNode(), withName( "a" ) ); node.createRelationshipTo( db.createNode(), withName( "b" ) ); relId = node.createRelationshipTo( db.createNode(), withName( "c" ) ).getId(); tx.success(); } }
@PluginTarget( Node.class ) public Path pathToReference( @Source Node me ) { PathFinder<Path> finder = GraphAlgoFactory.shortestPath( PathExpanders.allTypesAndDirections(), 6 ); try ( Transaction tx = me.getGraphDatabase().beginTx() ) { Node other; if ( me.hasRelationship( RelationshipType.withName( "friend" ) ) ) { ResourceIterable<Relationship> relationships = (ResourceIterable<Relationship>) me.getRelationships( RelationshipType.withName( "friend" ) ); try ( ResourceIterator<Relationship> resourceIterator = relationships.iterator() ) { other = resourceIterator.next().getOtherNode( me ); } } else { other = me.getGraphDatabase().createNode(); } Path path = finder.findSinglePath( other, me ); tx.success(); return path; } }
@Test public void shouldNotGetTheSameRelationshipMoreThanOnceWhenAskingForTheSameTypeMultipleTimes() { // given Node node = getGraphDb().createNode(); node.createRelationshipTo( getGraphDb().createNode(), withName( "FOO" ) ); // when long relationships = Iterables.count( node.getRelationships( withName( "FOO" ), withName( "FOO" ) ) ); // then assertEquals( 1, relationships ); }
@Test public void testNodeHasProperty() { BatchInserter inserter = globalInserter; long theNode = inserter.createNode( properties ); long anotherNode = inserter.createNode( Collections.emptyMap() ); long relationship = inserter.createRelationship( theNode, anotherNode, RelationshipType.withName( "foo" ), properties ); for ( String key : properties.keySet() ) { assertTrue( inserter.nodeHasProperty( theNode, key ) ); assertFalse( inserter.nodeHasProperty( theNode, key + "-" ) ); assertTrue( inserter.relationshipHasProperty( relationship, key ) ); assertFalse( inserter.relationshipHasProperty( relationship, key + "-" ) ); } }
@Test public void testDefaultsAreSeparateForNodesAndRelationships() { stopDb(); config = new HashMap<>(); config.put( GraphDatabaseSettings.node_keys_indexable.name(), "propName" ); config.put( GraphDatabaseSettings.node_auto_indexing.name(), "true" ); // Now only node properties named propName should be indexed. startDb(); newTransaction(); Node node1 = graphDb.createNode(); Node node2 = graphDb.createNode(); node1.setProperty( "propName", "node1" ); node2.setProperty( "propName", "node2" ); node2.setProperty( "propName_", "node2" ); Relationship rel = node1.createRelationshipTo( node2, RelationshipType.withName( "DYNAMIC" ) ); rel.setProperty( "propName", "rel1" ); newTransaction(); ReadableIndex<Node> autoIndex = graphDb.index().getNodeAutoIndexer().getAutoIndex(); assertEquals( node1, autoIndex.get( "propName", "node1" ).getSingle() ); assertEquals( node2, autoIndex.get( "propName", "node2" ).getSingle() ); assertFalse( graphDb.index().getRelationshipAutoIndexer().getAutoIndex().get( "propName", "rel1" ).hasNext() ); }
private static void generateTransaction( GraphDatabaseAPI database ) { try ( Transaction transaction = database.beginTx() ) { Node startNode = database.createNode( Label.label( "startNode" ) ); startNode.setProperty( "key", "value" ); Node endNode = database.createNode( Label.label( "endNode" ) ); endNode.setProperty( "key", "value" ); startNode.createRelationshipTo( endNode, RelationshipType.withName( "connects" ) ); transaction.success(); } }
private void createRelationshipsOnNode( GraphDatabaseService db, Node root, int numberOfRelationships ) { for ( int i = 0; i < numberOfRelationships; i++ ) { root.createRelationshipTo( db.createNode(), RelationshipType.withName( "Type" + (i % 4) ) ) .setProperty( "" + i, i ); } }
private static Function<Node,StartRelationship> loop( String type ) { return node -> { RelationshipType relType = withName( type ); return new StartRelationship( node.createRelationshipTo( node, relType ).getId(), Direction.BOTH, relType ); }; } }
private void createInitialData( GraphDatabaseService graphdb ) { try ( Transaction tx = graphdb.beginTx() ) { Node first = properties( graphdb.createNode() ); Node other = properties( graphdb.createNode() ); properties( first.createRelationshipTo( other, RelationshipType.withName( "KNOWS" ) ) ); properties( other.createRelationshipTo( first, RelationshipType.withName( "DISTRUSTS" ) ) ); tx.success(); } }
@Test @Graph( "I know you" ) public void get_Relationship_by_ID() throws JsonParseException { Node node = data.get().get( "I" ); Relationship relationship; try ( Transaction transaction = node.getGraphDatabase().beginTx() ) { relationship = node.getSingleRelationship( RelationshipType.withName( "know" ), Direction.OUTGOING ); } String response = gen().expectedStatus( com.sun.jersey.api.client.ClientResponse.Status.OK.getStatusCode() ).get( getRelationshipUri( relationship ) ).entity(); assertTrue( JsonHelper.jsonToMap( response ).containsKey( "start" ) ); }
@Before public void setup() { for ( int i = 0; i < 100; i++ ) { try ( Transaction tx = db.beginTx() ) { Node prev = null; for ( int j = 0; j < 100; j++ ) { Node node = db.createNode( label( "L" ) ); if ( prev != null ) { Relationship rel = prev.createRelationshipTo( node, RelationshipType.withName( "T" ) ); rel.setProperty( "prop", i + j ); } prev = node; } tx.success(); } } }
private static Function<Node,StartRelationship> outgoing( String type ) { return node -> { GraphDatabaseService db = node.getGraphDatabase(); RelationshipType relType = withName( type ); return new StartRelationship( node.createRelationshipTo( db.createNode(), relType ).getId(), Direction.OUTGOING, relType ); }; }
private static Function<GraphDatabaseService, Relationship> relate( final Pair<Node, Node> nodes ) { return graphDb -> nodes.first().createRelationshipTo( nodes.other(), withName( "RELATED" ) ); }
private long initWithRel( GraphDatabaseService db ) { try ( Transaction tx = db.beginTx() ) { Node node = db.createNode(); node.setProperty( "a", "prop" ); Relationship rel = node.createRelationshipTo( db.createNode(), RelationshipType.withName( "T" ) ); long id = rel.getId(); tx.success(); return id; } }