private void assertExpectedRelationshipTypes( Set<String> expectedTypes, Node node, boolean commit ) { Set<String> actual = Iterables.asSet( asStrings( node.getRelationshipTypes() ) ); assertEquals( expectedTypes, actual ); if ( commit ) { newTransaction(); } assertEquals( expectedTypes, Iterables.asSet( asStrings( node.getRelationshipTypes() ) ) ); }
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 shouldOnlyReturnTypeOnce() { // Given Node node; try ( Transaction tx = db.beginTx() ) { node = db.createNode(); node.createRelationshipTo( db.createNode(), RelationshipType.withName( "R" ) ); node.createRelationshipTo( db.createNode(), RelationshipType.withName( "R" ) ); node.createRelationshipTo( db.createNode(), RelationshipType.withName( "R" ) ); tx.success(); } // Then try ( Transaction tx = db.beginTx() ) { assertThat( Iterables.asList( node.getRelationshipTypes() ), equalTo( singletonList( RelationshipType.withName( "R" ) ) ) ); } }
public void mergeRelsWithSameTypeAndDirectionInMergeNodes(Node node, RefactorConfig config, Direction dir) { for (RelationshipType type : node.getRelationshipTypes()) { StreamSupport.stream(node.getRelationships(dir,type).spliterator(), false) .collect(Collectors.groupingBy(rel -> Pair.of(rel.getStartNode(), rel.getEndNode()))) .values().stream() .filter(list -> !list.isEmpty()) .forEach(list -> { Relationship first = list.get(0); for (int i = 1; i < list.size(); i++) { mergeRels(list.get(i), first, true, config); } }); } } }
private void addRelationships(Map<String, Map<String, MetaResult>> metaData, Map<String, MetaResult> nodeMeta, String labelName, Node node, Map<String, Iterable<ConstraintDefinition>> relConstraints) { for (RelationshipType type : node.getRelationshipTypes()) { int out = node.getDegree(type, Direction.OUTGOING); if (out == 0) continue; String typeName = type.name(); Iterable<ConstraintDefinition> constraints = relConstraints.get(typeName); if (!nodeMeta.containsKey(typeName)) nodeMeta.put(typeName, new MetaResult(labelName,typeName)); // int in = node.getDegree(type, Direction.INCOMING); Map<String, MetaResult> typeMeta = metaData.get(typeName); if (!typeMeta.containsKey(labelName)) typeMeta.put(labelName,new MetaResult(typeName,labelName)); MetaResult relMeta = nodeMeta.get(typeName); addOtherNodeInfo(node, labelName, out, type, relMeta , typeMeta, constraints); } }
@UserFunction("apoc.node.relationship.types") @Description("apoc.node.relationship.types(node, rel-direction-pattern) - returns a list of distinct relationship types") public List<String> relationshipTypes(@Name("node") Node node, @Name(value = "types",defaultValue = "") String types) { if (node==null) return null; List<String> relTypes = Iterables.asList(Iterables.map(RelationshipType::name, node.getRelationshipTypes())); if (types == null || types.isEmpty()) return relTypes; List<String> result = new ArrayList<>(relTypes.size()); for (Pair<RelationshipType, Direction> p : parse(types)) { String name = p.first().name(); if (relTypes.contains(name) && node.hasRelationship(p.first(),p.other())) { result.add(name); } } return result; }
public void sample(GraphDatabaseService db, Sampler sampler, int sampleSize) { for (Label label : db.getAllLabelsInUse()) { ResourceIterator<Node> nodes = db.findNodes(label); int count = 0; while (nodes.hasNext() && count++ < sampleSize) { Node node = nodes.next(); sampler.sample(label,count,node); for (RelationshipType type : node.getRelationshipTypes()) { sampleRels(sampleSize, sampler, label, count, node, type); } } nodes.close(); } }
/** * @see org.neo4j.graphdb.Node#getRelationshipTypes(). */ public Iterable<RelationshipType> getRelationshipTypes() { return getWrappedNode().getRelationshipTypes(); }