@SuppressWarnings( "unchecked" ) public Representation getNodeDegree( long nodeId, RelationshipDirection direction, Collection<String> types ) throws NodeNotFoundException { Node node = node( nodeId ); if ( types.isEmpty() ) { return PropertiesRepresentation.value( node.getDegree( direction.internal ) ); } else { int sum = 0; for ( String type : types ) { sum += node.getDegree( RelationshipType.withName( type ), direction.internal ); } return PropertiesRepresentation.value( sum ); } }
private void assertCounts( Node me, Map<RelType, int[]> expectedCounts ) { assertEquals( totalCount( expectedCounts, Direction.BOTH ), me.getDegree() ); assertEquals( totalCount( expectedCounts, Direction.BOTH ), me.getDegree( Direction.BOTH ) ); assertEquals( totalCount( expectedCounts, Direction.OUTGOING ), me.getDegree( Direction.OUTGOING ) ); assertEquals( totalCount( expectedCounts, Direction.INCOMING ), me.getDegree( Direction.INCOMING ) ); for ( Map.Entry<RelType, int[]> entry : expectedCounts.entrySet() ) { RelType type = entry.getKey(); assertEquals( totalCount( entry.getValue(), Direction.BOTH ), me.getDegree( type ) ); assertEquals( totalCount( entry.getValue(), Direction.OUTGOING ), me.getDegree( type, Direction.OUTGOING ) ); assertEquals( totalCount( entry.getValue(), Direction.INCOMING ), me.getDegree( type, Direction.INCOMING ) ); assertEquals( totalCount( entry.getValue(), Direction.BOTH ), me.getDegree( type, Direction.BOTH ) ); } }
@Test public void movingBilaterallyOfTheDenseNodeThresholdIsConsistent() { // GIVEN GraphDatabaseService db = databaseRule.getGraphDatabaseAPI(); Node root; // WHEN try ( Transaction tx = db.beginTx() ) { root = db.createNode(); createRelationshipsOnNode( db, root, 100 ); deleteRelationshipsFromNode( root, 80 ); assertEquals( 20, root.getDegree() ); assertEquals( 20, root.getDegree( Direction.OUTGOING ) ); assertEquals( 0, root.getDegree( Direction.INCOMING ) ); tx.success(); } // THEN try ( Transaction tx = db.beginTx() ) { assertEquals( 20, root.getDegree() ); assertEquals( 20, root.getDegree( Direction.OUTGOING ) ); assertEquals( 0, root.getDegree( Direction.INCOMING ) ); tx.success(); } }
@Test public void withLoops() { // Just to make sure it doesn't work by accident what with ids aligning with count for ( int i = 0; i < 10; i++ ) { getGraphDb().createNode().createRelationshipTo( getGraphDb().createNode(), MyRelTypes.TEST ); } Node node = getGraphDb().createNode(); assertEquals( 0, node.getDegree() ); node.createRelationshipTo( node, MyRelTypes.TEST ); assertEquals( 1, node.getDegree() ); Node otherNode = getGraphDb().createNode(); Relationship rel2 = node.createRelationshipTo( otherNode, MyRelTypes.TEST2 ); assertEquals( 2, node.getDegree() ); assertEquals( 1, otherNode.getDegree() ); newTransaction(); assertEquals( 2, node.getDegree() ); Relationship rel3 = node.createRelationshipTo( node, MyRelTypes.TEST_TRAVERSAL ); assertEquals( 3, node.getDegree() ); assertEquals( 1, otherNode.getDegree() ); rel2.delete(); assertEquals( 2, node.getDegree() ); assertEquals( 0, otherNode.getDegree() ); rel3.delete(); assertEquals( 1, node.getDegree() ); }
assertEquals( 100, source.getDegree() ); assertEquals( 100, source.getDegree( Direction.OUTGOING ) ); assertEquals( 0, source.getDegree( Direction.INCOMING ) ); assertEquals( 25, source.getDegree( RelationshipType.withName( "Type0" ) ) ); assertEquals( 25, source.getDegree( RelationshipType.withName( "Type1" ) ) ); assertEquals( 25, source.getDegree( RelationshipType.withName( "Type2" ) ) ); assertEquals( 25, source.getDegree( RelationshipType.withName( "Type3" ) ) ); assertEquals( 100, sink.getDegree() ); assertEquals( 0, sink.getDegree( Direction.OUTGOING ) ); assertEquals( 100, sink.getDegree( Direction.INCOMING ) ); assertEquals( 25, sink.getDegree( RelationshipType.withName( "Type0" ) ) ); assertEquals( 25, sink.getDegree( RelationshipType.withName( "Type1" ) ) ); assertEquals( 25, sink.getDegree( RelationshipType.withName( "Type2" ) ) ); assertEquals( 25, sink.getDegree( RelationshipType.withName( "Type3" ) ) ); tx.success(); assertEquals( 100, source.getDegree() ); assertEquals( 100, source.getDegree( Direction.OUTGOING ) ); assertEquals( 0, source.getDegree( Direction.INCOMING ) ); assertEquals( 25, source.getDegree( RelationshipType.withName( "Type0" ) ) ); assertEquals( 25, source.getDegree( RelationshipType.withName( "Type1" ) ) ); assertEquals( 25, source.getDegree( RelationshipType.withName( "Type2" ) ) ); assertEquals( 25, source.getDegree( RelationshipType.withName( "Type3" ) ) ); assertEquals( 100, sink.getDegree() ); assertEquals( 0, sink.getDegree( Direction.OUTGOING ) ); assertEquals( 100, sink.getDegree( Direction.INCOMING ) ); assertEquals( 25, sink.getDegree( RelationshipType.withName( "Type0" ) ) );
@Test public void deletingRelationshipsFromDenseNodeIsConsistent() { // GIVEN GraphDatabaseService db = databaseRule.getGraphDatabaseAPI(); Node root; try ( Transaction tx = db.beginTx() ) { root = db.createNode(); createRelationshipsOnNode( db, root, 100 ); tx.success(); } try ( Transaction tx = db.beginTx() ) { deleteRelationshipsFromNode( root, 80 ); assertEquals( 20, root.getDegree() ); assertEquals( 20, root.getDegree( Direction.OUTGOING ) ); assertEquals( 0, root.getDegree( Direction.INCOMING ) ); tx.success(); } try ( Transaction tx = db.beginTx() ) { assertEquals( 20, root.getDegree() ); assertEquals( 20, root.getDegree( Direction.OUTGOING ) ); assertEquals( 0, root.getDegree( Direction.INCOMING ) ); tx.success(); } }
assertEquals( 100, root.getDegree() ); assertEquals( 100, root.getDegree( Direction.OUTGOING ) ); assertEquals( 0, root.getDegree( Direction.INCOMING ) ); assertEquals( 25, root.getDegree( RelationshipType.withName( "Type0" ) ) ); assertEquals( 25, root.getDegree( RelationshipType.withName( "Type1" ) ) ); assertEquals( 25, root.getDegree( RelationshipType.withName( "Type2" ) ) ); assertEquals( 25, root.getDegree( RelationshipType.withName( "Type3" ) ) ); tx.success(); assertEquals( 100, root.getDegree() ); assertEquals( 100, root.getDegree( Direction.OUTGOING ) ); assertEquals( 0, root.getDegree( Direction.INCOMING ) ); assertEquals( 25, root.getDegree( RelationshipType.withName( "Type0" ) ) ); assertEquals( 25, root.getDegree( RelationshipType.withName( "Type1" ) ) ); assertEquals( 25, root.getDegree( RelationshipType.withName( "Type2" ) ) ); assertEquals( 25, root.getDegree( RelationshipType.withName( "Type3" ) ) ); tx.success();
assertEquals( 0, node1.getDegree() ); assertEquals( 0, node2.getDegree() ); node1.createRelationshipTo( node2, MyRelTypes.TEST ); assertEquals( 1, node1.getDegree() ); assertEquals( 1, node2.getDegree() ); node1.createRelationshipTo( getGraphDb().createNode(), MyRelTypes.TEST2 ); assertEquals( 2, node1.getDegree() ); assertEquals( 1, node2.getDegree() ); newTransaction(); assertEquals( 2, node1.getDegree() ); assertEquals( 1, node2.getDegree() ); assertEquals( i + 2 + 1, node1.getDegree() ); assertEquals( i + 1 + 1, node2.getDegree() ); if ( i % 10 == 0 ) assertEquals( 1002, node1.getDegree() ); assertEquals( 1002, node1.getDegree( Direction.BOTH ) ); assertEquals( 502, node1.getDegree( Direction.OUTGOING ) ); assertEquals( 500, node1.getDegree( Direction.INCOMING ) ); assertEquals( 1, node1.getDegree( MyRelTypes.TEST2 ) ); assertEquals( 1001, node1.getDegree( MyRelTypes.TEST ) ); assertEquals( 1001, node1.getDegree( MyRelTypes.TEST, Direction.BOTH ) ); assertEquals( 501, node1.getDegree( MyRelTypes.TEST, Direction.OUTGOING ) ); assertEquals( 500, node1.getDegree( MyRelTypes.TEST, Direction.INCOMING ) ); assertEquals( 1, node1.getDegree( MyRelTypes.TEST2, Direction.OUTGOING ) ); assertEquals( 0, node1.getDegree( MyRelTypes.TEST2, Direction.INCOMING ) ); newTransaction();
private void assertDegrees( Node node ) { for ( RelationshipType type : node.getRelationshipTypes() ) { for ( Direction direction : Direction.values() ) { long degree = node.getDegree( type, direction ); long actualDegree = count( node.getRelationships( type, direction ) ); assertEquals( actualDegree, degree ); } } }
@Test public void convertNodeToDense() { Node node = getGraphDb().createNode(); EnumMap<MyRelTypes,Set<Relationship>> rels = new EnumMap<>( MyRelTypes.class ); for ( MyRelTypes type : MyRelTypes.values() ) { rels.put( type, new HashSet<>() ); } int expectedRelCount = 0; for ( int i = 0; i < 6; i++, expectedRelCount++ ) { MyRelTypes type = MyRelTypes.values()[i % MyRelTypes.values().length]; Relationship rel = node.createRelationshipTo( getGraphDb().createNode(), type ); rels.get( type ).add( rel ); } newTransaction(); for ( int i = 0; i < 1000; i++, expectedRelCount++ ) { node.createRelationshipTo( getGraphDb().createNode(), MyRelTypes.TEST ); } assertEquals( expectedRelCount, node.getDegree() ); assertEquals( expectedRelCount, node.getDegree( Direction.BOTH ) ); assertEquals( expectedRelCount, node.getDegree( Direction.OUTGOING ) ); assertEquals( 0, node.getDegree( Direction.INCOMING ) ); assertEquals( rels.get( MyRelTypes.TEST2 ), Iterables.asSet( node.getRelationships( MyRelTypes.TEST2 ) ) ); assertEquals( join( rels.get( MyRelTypes.TEST_TRAVERSAL ), rels.get( MyRelTypes.TEST2 ) ), Iterables.asSet( node.getRelationships( MyRelTypes.TEST_TRAVERSAL, MyRelTypes.TEST2 ) ) ); }
@Test public void createDenseNodeWithLowThreshold() { newDb( 2 ); // Create node with two relationships Node node; try ( Transaction tx = db.beginTx() ) { node = db.createNode(); node.createRelationshipTo( db.createNode(), MyRelTypes.TEST ); node.createRelationshipTo( db.createNode(), MyRelTypes.TEST2 ); assertEquals( 2, node.getDegree() ); assertEquals( 1, node.getDegree( MyRelTypes.TEST ) ); assertEquals( 1, node.getDegree( MyRelTypes.TEST2 ) ); tx.success(); } try ( Transaction tx = db.beginTx() ) { node.createRelationshipTo( db.createNode(), MyRelTypes.TEST ); tx.success(); } db.shutdown(); }
private void add(Node node) { count++; int degree = node.getDegree(); sumDegree += degree; if (degree > maxDegree) maxDegree = degree; if (degree < minDegree) minDegree = degree; for (Label label : node.getLabels()) labels.add(label.name()); for (String key : node.getPropertyKeys()) properties.add(key); } Map<String,Object> toMap() {
@Test public void shouldRollbackRelationshipOnFailure() throws Exception { long n1, n2; try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() ) { n1 = graphDb.createNode().getId(); n2 = graphDb.createNode().getId(); tx.success(); } try ( Transaction tx = beginTransaction() ) { int label = tx.token().relationshipTypeGetOrCreateForName( "R" ); tx.dataWrite().relationshipCreate( n1, label, n2 ); tx.failure(); } try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { assertEquals( 0, graphDb.getNodeById( n1 ).getDegree() ); } }
@UserFunction("apoc.node.degree.out") @Description("apoc.node.degree.out(node, relationshipName) - returns total number number of outgoing relationships") public long degreeOut(@Name("node") Node node, @Name(value = "types",defaultValue = "") String type) { if (type==null || type.isEmpty()) { return node.getDegree(Direction.OUTGOING); } return node.getDegree(RelationshipType.withName(type), Direction.OUTGOING); }
@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() ); } }
assertEquals( 1, otherNode.getDegree() );
@Test public void shouldDeleteRelationshipAddedInTransaction() throws Exception { long n1, n2; try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() ) { n1 = graphDb.createNode().getId(); n2 = graphDb.createNode().getId(); tx.success(); } try ( Transaction tx = beginTransaction() ) { int label = tx.token().relationshipTypeGetOrCreateForName( "R" ); long r = tx.dataWrite().relationshipCreate( n1, label, n2 ); assertTrue( tx.dataWrite().relationshipDelete( r ) ); tx.success(); } try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { assertEquals( 0, graphDb.getNodeById( n1 ).getDegree() ); } }
@UserFunction("apoc.node.degree") @Description("apoc.node.degree(node, rel-direction-pattern) - returns total degrees of the given relationships in the pattern, can use '>' or '<' for all outgoing or incoming relationships") public long degree(@Name("node") Node node, @Name(value = "types",defaultValue = "") String types) { if (types==null || types.isEmpty()) return node.getDegree(); long degree = 0; for (Pair<RelationshipType, Direction> pair : parse(types)) { degree += getDegreeSafe(node, pair.first(), pair.other()); } return degree; }
private void sampleRels(int sampleSize, Sampler sampler, Label label, int count, Node node, RelationshipType type) { Direction direction = Direction.OUTGOING; int degree = node.getDegree(type, direction); sampler.sample(label,count,node,type,direction,degree,null); if (degree==0) return; ResourceIterator<Relationship> relIt = ((ResourceIterable<Relationship>)node.getRelationships(direction, type)).iterator(); int relCount = 0; while (relIt.hasNext() && relCount++ < sampleSize) { sampler.sample(label,count,node,type,direction,degree,relIt.next()); } relIt.close(); }