private void appendRelationship( PrintWriter out, Relationship rel ) { out.print( "create (" ); out.print( identifier( rel.getStartNode() ) ); out.print( ")-[:" ); out.print( quote( rel.getType().name() ) ); formatProperties( out, rel ); out.print( "]->(" ); out.print( identifier( rel.getEndNode() ) ); out.println( ")" ); }
private static long createRelationship( GraphDatabaseService db ) { long relationshipId; try ( Transaction tx = db.beginTx() ) { Node start = db.createNode( Label.label( System.currentTimeMillis() + "" ) ); Node end = db.createNode( Label.label( System.currentTimeMillis() + "" ) ); relationshipId = start.createRelationshipTo( end, withName( "KNOWS" ) ).getId(); tx.success(); } return relationshipId; }
@Test public void mustBeAbleToConsistencyCheckRelationshipIndexWithMultipleRelationshipTypesAndOneProperty() throws Exception { GraphDatabaseService db = createDatabase(); RelationshipType relType1 = RelationshipType.withName( "R1" ); RelationshipType relType2 = RelationshipType.withName( "R2" ); try ( Transaction tx = db.beginTx() ) { db.execute( format( RELATIONSHIP_CREATE, "rels", array( "R1", "R2" ), array( "p1" ) ) ).close(); tx.success(); } try ( Transaction tx = db.beginTx() ) { db.schema().awaitIndexesOnline( 1, TimeUnit.MINUTES ); Node n1 = db.createNode(); Node n2 = db.createNode(); n1.createRelationshipTo( n1, relType1 ).setProperty( "p1", "value" ); n1.createRelationshipTo( n1, relType2 ).setProperty( "p1", "value" ); n2.createRelationshipTo( n2, relType1 ).setProperty( "p1", "value" ); n2.createRelationshipTo( n2, relType2 ).setProperty( "p1", "value" ); tx.success(); } db.shutdown(); assertIsConsistent( checkConsistency() ); }
@Test public void shouldUpdateRelationshipWithLabelCountsWhenRemovingLabelAndDeletingRelationship() { // given Node foo; try ( Transaction tx = db.beginTx() ) { foo = db.createNode( label( "Foo" ) ); Node bar = db.createNode( label( "Bar" ) ); foo.createRelationshipTo( bar, withName( "BAZ" ) ); tx.success(); } long before = numberOfRelationshipsMatching( label( "Foo" ), withName( "BAZ" ), null ); // when try ( Transaction tx = db.beginTx() ) { for ( Relationship relationship : foo.getRelationships() ) { relationship.delete(); } foo.removeLabel( label("Foo")); tx.success(); } long after = numberOfRelationshipsMatching( label( "Foo" ), withName( "BAZ" ), null ); // then assertEquals( before - 1, after ); }
@Test public void shouldMapDirectRelationship() { // Given Node start, end; Relationship relationship; try ( Transaction tx = db.beginTx() ) { start = db.createNode(); end = db.createNode(); relationship = start.createRelationshipTo( end, RelationshipType.withName( "R" ) ); tx.success(); } RelationshipValue relationshipValue = VirtualValues.relationshipValue( relationship.getId(), nodeValue( start.getId(), Values.EMPTY_TEXT_ARRAY, EMPTY_MAP ), nodeValue( start.getId(), Values.EMPTY_TEXT_ARRAY, EMPTY_MAP ), stringValue( "R" ), EMPTY_MAP ); // When Relationship coreAPIRelationship = mapper.mapRelationship( relationshipValue ); // Then try ( Transaction ignore = db.beginTx() ) { assertThat( coreAPIRelationship.getId(), equalTo( relationship.getId() ) ); assertThat( coreAPIRelationship.getStartNode(), equalTo( start ) ); assertThat( coreAPIRelationship.getEndNode(), equalTo( end ) ); } }
@Test public void shouldProvideTheCorrectRelationshipData() GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); try ( Transaction tx = db.beginTx() ) db.createNode().createRelationshipTo( db.createNode(), withName( "TYPE" ) ); RelationshipType livesIn = withName( "LIVES_IN" ); long relId; try ( Transaction tx = db.beginTx() ) Node person = db.createNode( label( "Person" ) ); Node city = db.createNode( label( "City" ) ); Relationship rel = person.createRelationshipTo( city, livesIn ); rel.setProperty( "since", 2009 ); relId = rel.getId(); tx.success(); Relationship rel = db.getRelationshipById( relId ); rel.setProperty( "since", 2010 ); tx.success(); changedRelationships.contains( livesIn.name() ) );
private void createData( GraphDatabaseService database, int numberOfNodes ) { for ( int i = 0; i < numberOfNodes; i++ ) { try ( Transaction transaction = database.beginTx() ) { Node node = database.createNode( Label.label( FOOD_LABEL ), Label.label( CLOTHES_LABEL ), Label.label( WEATHER_LABEL ) ); node.setProperty( PROPERTY_NAME, "Node" + i ); Relationship relationship = node.createRelationshipTo( node, RelationshipType.withName( FOOD_LABEL ) ); relationship.setProperty( PROPERTY_NAME, "Relationship" + i ); transaction.success(); } } }
@Override public void createTestGraph( GraphDatabaseService graphDb ) { try ( Transaction tx = graphDb.beginTx() ) { for ( int i = 0; i < N_NODES; i++ ) { nodeIds.add( graphDb.createNode( Label.label( "LABEL" + i ) ).getId() ); } tx.success(); } try ( Transaction tx = graphDb.beginTx() ) { for ( int i = 0; i < N_RELATIONSHIPS; i++ ) { Long source = nodeIds.get( random.nextInt( N_NODES ) ); Long target = nodeIds.get( random.nextInt( N_NODES ) ); graphDb.getNodeById( source ).createRelationshipTo( graphDb.getNodeById( target ), RelationshipType.withName( "REL" + (i % 10) ) ); } tx.success(); } }
@Test public void shouldDeleteRelationship() throws Exception { long n1, r; try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() ) { Node node1 = graphDb.createNode(); Node node2 = graphDb.createNode(); n1 = node1.getId(); r = node1.createRelationshipTo( node2, RelationshipType.withName( "R" ) ).getId(); tx.success(); } try ( Transaction tx = beginTransaction() ) { assertTrue( "should delete relationship", tx.dataWrite().relationshipDelete( r ) ); tx.success(); } try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { assertEquals( 0, graphDb.getNodeById( n1 ).getDegree() ); } }
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; } }
private DbRepresentation createSomeData() { RelationshipType type = withName( "KNOWS" ); GraphDatabaseService db = new TestGraphDatabaseFactory() .setFileSystem( new UncloseableDelegatingFileSystemAbstraction( fs.get() ) ) .newImpermanentDatabase( testDirectory.databaseDir() ); try ( Transaction tx = db.beginTx() ) { Node prevNode = db.createNode(); for ( int i = 0; i < 100; i++ ) { Node node = db.createNode(); Relationship rel = prevNode.createRelationshipTo( node, type ); node.setProperty( "someKey" + i % 10, i % 15 ); rel.setProperty( "since", System.currentTimeMillis() ); } tx.success(); } DbRepresentation result = DbRepresentation.of( db ); db.shutdown(); return result; }
@Test public void shouldAddPropertyToRelationship() throws Exception { // Given long relationshipId; String propertyKey = "prop"; try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() ) { Node node1 = graphDb.createNode(); Node node2 = graphDb.createNode(); relationshipId = node1.createRelationshipTo( node2, RelationshipType.withName( "R" ) ).getId(); tx.success(); } // When try ( Transaction tx = beginTransaction() ) { int token = tx.token().propertyKeyGetOrCreateForName( propertyKey ); assertThat( tx.dataWrite().relationshipSetProperty( relationshipId, token, stringValue( "hello" ) ), equalTo( NO_VALUE ) ); tx.success(); } // Then try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { assertThat( graphDb.getRelationshipById( relationshipId ).getProperty( "prop" ), equalTo( "hello" ) ); } }
@Test public void canAvoidDeadlockThatWouldHappenIfTheRelationshipTypeCreationTransactionModifiedData() { GraphDatabaseService graphdb = database.getGraphDatabaseAPI(); Node node = null; try ( Transaction tx = graphdb.beginTx() ) { node = graphdb.createNode(); node.setProperty( "counter", 0L ); tx.success(); } graphdb.registerTransactionEventHandler( new RelationshipCounterTransactionEventHandler( node ) ); try ( Transaction tx = graphdb.beginTx() ) { node.setProperty( "state", "not broken yet" ); node.createRelationshipTo( graphdb.createNode(), RelationshipType.withName( "TEST" ) ); node.removeProperty( "state" ); tx.success(); } assertThat( node, inTx( graphdb, hasProperty( "counter" ).withValue( 1L ) ) ); }
@Override public Map<String, Node> create( GraphDatabaseService graphdb ) { Map<String, Node> result = new HashMap<>(); try ( Transaction tx = graphdb.beginTx() ) { graphdb.index().getRelationshipAutoIndexer().setEnabled( autoIndexRelationships ); for ( NODE def : nodes ) { Node node = init( graphdb.createNode(), def.setNameProperty() ? def.name() : null, def.properties(), graphdb.index().getNodeAutoIndexer(), autoIndexNodes ); for ( LABEL label : def.labels() ) { node.addLabel( label( label.value() ) ); } result.put( def.name(), node ); } for ( REL def : rels ) { init( result.get( def.start() ).createRelationshipTo( result.get( def.end() ), RelationshipType.withName( def.type() ) ), def.setNameProperty() ? def.name() : null, def.properties(), graphdb.index().getRelationshipAutoIndexer(), autoIndexRelationships ); } tx.success(); } return result; }
@Test public void shouldNotWriteWhenSettingPropertyToSameValue() throws Exception { // Given long relationshipId; String propertyKey = "prop"; Value theValue = stringValue( "The Value" ); try ( org.neo4j.graphdb.Transaction ctx = graphDb.beginTx() ) { Node node1 = graphDb.createNode(); Node node2 = graphDb.createNode(); Relationship r = node1.createRelationshipTo( node2, RelationshipType.withName( "R" ) ); r.setProperty( propertyKey, theValue.asObject() ); relationshipId = r.getId(); ctx.success(); } // When Transaction tx = beginTransaction(); int property = tx.token().propertyKeyGetOrCreateForName( propertyKey ); assertThat( tx.dataWrite().relationshipSetProperty( relationshipId, property, theValue ), equalTo( theValue ) ); tx.success(); assertThat( tx.closeTransaction(), equalTo( Transaction.READ_ONLY ) ); } }
@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 queryShouldFindDataAddedInLaterTransactions() { db = createDatabase(); db.execute( format( NODE_CREATE, "node", array( "Label1", "Label2" ), array( "prop1", "prop2" ) ) ).close(); db.execute( format( RELATIONSHIP_CREATE, "rel", array( "Reltype1", "Reltype2" ), array( "prop1", "prop2" ) ) ).close(); awaitIndexesOnline(); long horseId; long horseRelId; try ( Transaction tx = db.beginTx() ) { Node zebra = db.createNode(); zebra.setProperty( "prop1", "zebra" ); Node horse = db.createNode( Label.label( "Label1" ) ); horse.setProperty( "prop2", "horse" ); horse.setProperty( "prop3", "zebra" ); Relationship horseRel = zebra.createRelationshipTo( horse, RelationshipType.withName( "Reltype1" ) ); horseRel.setProperty( "prop1", "horse" ); Relationship loop = horse.createRelationshipTo( horse, RelationshipType.withName( "loop" ) ); loop.setProperty( "prop2", "zebra" ); horseId = horse.getId(); horseRelId = horseRel.getId(); tx.success(); } assertQueryFindsIds( db, true, "node", "horse", newSetWith( horseId ) ); assertQueryFindsIds( db, true, "node", "horse zebra", newSetWith( horseId ) ); assertQueryFindsIds( db, false, "rel", "horse", newSetWith( horseRelId ) ); assertQueryFindsIds( db, false, "rel", "horse zebra", newSetWith( horseRelId ) ); }
@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(); } }
@Before public void prepDB() { try ( Transaction transaction = db.beginTx() ) { node1 = db.createNode( label( "hej" ), label( "ha" ), label( "he" ) ); node1.setProperty( "hej", "value" ); node1.setProperty( "ha", "value1" ); node1.setProperty( "he", "value2" ); node1.setProperty( "ho", "value3" ); node1.setProperty( "hi", "value4" ); node2 = db.createNode(); Relationship rel = node1.createRelationshipTo( node2, RelationshipType.withName( "hej" ) ); rel.setProperty( "hej", "valuuu" ); rel.setProperty( "ha", "value1" ); rel.setProperty( "he", "value2" ); rel.setProperty( "ho", "value3" ); rel.setProperty( "hi", "value4" ); 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 ); } }