private static Map<String,List<StartRelationship>> buildSparseDenseRels( Node node ) { Map<String,List<StartRelationship>> relationshipMap = new HashMap<>(); for ( Function<Node,StartRelationship> rel : sparseDenseRels ) { StartRelationship r = rel.apply( node ); List<StartRelationship> relsOfType = relationshipMap.computeIfAbsent( computeKey( r ), key -> new ArrayList<>() ); relsOfType.add( r ); } return relationshipMap; }
private static String computeKey( StartRelationship r ) { return computeKey( r.type.name(), r.direction ); }
static Map<String,Integer> count( org.neo4j.internal.kernel.api.Transaction transaction, RelationshipTraversalCursor relationship ) throws KernelException { HashMap<String,Integer> counts = new HashMap<>(); while ( relationship.next() ) { String key = computeKey( transaction, relationship ); counts.compute( key, ( k, value ) -> value == null ? 1 : value + 1 ); } return counts; }
static void assertCount( org.neo4j.internal.kernel.api.Transaction transaction, RelationshipTraversalCursor relationship, Map<String,Integer> expectedCounts, int expectedType, Direction direction ) throws KernelException { String key = computeKey( transaction.token().relationshipTypeName( expectedType ), direction ); int expectedCount = expectedCounts.getOrDefault( key, 0 ); int count = 0; while ( relationship.next() ) { assertEquals( "same type", expectedType, relationship.type() ); count++; } assertEquals( format( "expected number of relationships for key '%s'", key ), expectedCount, count ); }
static StartNode dense( GraphDatabaseService graphDb ) { Node node; Map<String,List<StartRelationship>> relationshipMap; try ( Transaction tx = graphDb.beginTx() ) { node = graphDb.createNode(); relationshipMap = buildSparseDenseRels( node ); List<StartRelationship> bulk = new ArrayList<>(); RelationshipType bulkType = withName( "BULK" ); for ( int i = 0; i < 200; i++ ) { Relationship r = node.createRelationshipTo( graphDb.createNode(), bulkType ); bulk.add( new StartRelationship( r.getId(), Direction.OUTGOING, bulkType ) ); } String bulkKey = computeKey( "BULK", Direction.OUTGOING ); relationshipMap.put( bulkKey, bulk ); tx.success(); } return new StartNode( node.getId(), relationshipMap ); }
tx.dataWrite().relationshipCreate( start.id, newType, start.id ); expectedCounts.put( computeKey( newTypeName, OUTGOING ), 1 ); expectedCounts.put( computeKey( newTypeName, INCOMING ), 1 ); expectedCounts.put( computeKey( newTypeName, BOTH ), 1 );
private static String computeKey( org.neo4j.internal.kernel.api.Transaction transaction, RelationshipTraversalCursor r ) throws KernelException { Direction d; if ( r.sourceNodeReference() == r.targetNodeReference() ) { d = Direction.BOTH; } else if ( r.sourceNodeReference() == r.originNodeReference() ) { d = Direction.OUTGOING; } else { d = Direction.INCOMING; } return computeKey( transaction.token().relationshipTypeName( r.type() ), d ); }