|
19 | 19 | */ |
20 | 20 | package org.neo4j.gds.core.loading; |
21 | 21 |
|
| 22 | +import org.immutables.value.Value; |
22 | 23 | import org.neo4j.gds.PropertyMappings; |
23 | 24 | import org.neo4j.gds.RelationshipProjection; |
24 | 25 | import org.neo4j.gds.RelationshipType; |
@@ -51,6 +52,12 @@ public interface RelationshipsAndProperties { |
51 | 52 |
|
52 | 53 | Map<RelationshipType, Direction> directions(); |
53 | 54 |
|
| 55 | + @Value.Parameter(false) |
| 56 | + @Value.Default |
| 57 | + default Map<RelationshipType, SingleTypeRelationshipImportResult> importResults() { |
| 58 | + return Map.of(); |
| 59 | + } |
| 60 | + |
54 | 61 | static RelationshipsAndProperties of(Map<RelationshipTypeAndProjection, List<RelationshipsAndDirection>> relationshipsByType) { |
55 | 62 | var relTypeCount = relationshipsByType.size(); |
56 | 63 | Map<RelationshipType, Relationships.Topology> topologies = new HashMap<>(relTypeCount); |
@@ -100,59 +107,46 @@ static RelationshipsAndProperties of(Map<RelationshipTypeAndProjection, List<Rel |
100 | 107 | * @return a wrapper type ready to be consumed by a {@link org.neo4j.gds.api.GraphStore} |
101 | 108 | */ |
102 | 109 | static RelationshipsAndProperties of(Collection<SingleTypeRelationshipImporter.SingleTypeRelationshipImportContext> importContexts) { |
103 | | - var relTypeCount = importContexts.size(); |
104 | | - Map<RelationshipType, ImmutableTopology.Builder> relationshipBuilders = new HashMap<>(relTypeCount); |
105 | | - Map<RelationshipType, RelationshipPropertyStore> relationshipPropertyStores = new HashMap<>(relTypeCount); |
106 | | - Map<RelationshipType, Direction> directions = new HashMap<>(relTypeCount); |
| 110 | + var builders = new HashMap<RelationshipType, ImmutableSingleTypeRelationshipImportResult.Builder>(importContexts.size()); |
107 | 111 |
|
108 | 112 | importContexts.forEach((importContext) -> { |
109 | 113 | var adjacencyListsWithProperties = importContext.singleTypeRelationshipImporter().build(); |
| 114 | + var isInverseRelationship = importContext.inverseOfRelationshipType().isPresent(); |
110 | 115 |
|
111 | | - var adjacency = adjacencyListsWithProperties.adjacency(); |
112 | | - var properties = adjacencyListsWithProperties.properties(); |
113 | | - var relationshipCount = adjacencyListsWithProperties.relationshipCount(); |
114 | | - var relationshipProjection = importContext.relationshipProjection(); |
| 116 | + var direction = Direction.fromOrientation(importContext.relationshipProjection().orientation()); |
115 | 117 |
|
116 | | - var topologyBuilder = relationshipBuilders.computeIfAbsent( |
117 | | - importContext.relationshipType(), |
118 | | - relationshipType -> ImmutableTopology.builder() |
119 | | - .elementCount(relationshipCount) |
120 | | - .isMultiGraph(relationshipProjection.isMultiGraph()) |
121 | | - ); |
| 118 | + var topology = ImmutableTopology.builder() |
| 119 | + .adjacencyList(adjacencyListsWithProperties.adjacency()) |
| 120 | + .elementCount(adjacencyListsWithProperties.relationshipCount()) |
| 121 | + .isMultiGraph(importContext.relationshipProjection().isMultiGraph()) |
| 122 | + .build(); |
122 | 123 |
|
123 | | - importContext.inverseOfRelationshipType().ifPresentOrElse( |
124 | | - __ -> topologyBuilder.inverseAdjacencyList(adjacency), |
125 | | - () -> topologyBuilder.adjacencyList(adjacency) |
| 124 | + var properties = constructRelationshipPropertyStore( |
| 125 | + importContext.relationshipProjection(), |
| 126 | + adjacencyListsWithProperties.properties(), |
| 127 | + adjacencyListsWithProperties.relationshipCount() |
126 | 128 | ); |
127 | 129 |
|
128 | | - // TODO: we only add the properties for the forward relationships for now |
129 | | - if (!relationshipProjection.properties().isEmpty() && importContext.inverseOfRelationshipType().isEmpty()) { |
130 | | - relationshipPropertyStores.put( |
131 | | - importContext.relationshipType(), |
132 | | - constructRelationshipPropertyStore( |
133 | | - relationshipProjection, |
134 | | - properties, |
135 | | - relationshipCount |
136 | | - ) |
137 | | - ); |
138 | | - } |
139 | | - |
140 | | - directions.put( |
| 130 | + var importResultBuilder = builders.computeIfAbsent( |
141 | 131 | importContext.relationshipType(), |
142 | | - Direction.fromOrientation(importContext.relationshipProjection().orientation()) |
| 132 | + relationshipType -> ImmutableSingleTypeRelationshipImportResult.builder().direction(direction) |
143 | 133 | ); |
| 134 | + |
| 135 | + if (isInverseRelationship) { |
| 136 | + importResultBuilder.inverseTopology(topology).inverseProperties(properties); |
| 137 | + } else { |
| 138 | + importResultBuilder.forwardTopology(topology).forwardProperties(properties); |
| 139 | + } |
144 | 140 | }); |
145 | 141 |
|
146 | | - var relationships = relationshipBuilders.entrySet().stream().collect( |
| 142 | + var importResults = builders.entrySet().stream().collect( |
147 | 143 | Collectors.toMap( |
148 | 144 | Map.Entry::getKey, |
149 | 145 | e -> e.getValue().build() |
150 | 146 | )); |
151 | 147 |
|
152 | 148 | return ImmutableRelationshipsAndProperties.builder() |
153 | | - .relationships(relationships) |
154 | | - .properties(relationshipPropertyStores) |
155 | | - .directions(directions) |
| 149 | + .importResults(importResults) |
156 | 150 | .build(); |
157 | 151 | } |
158 | 152 |
|
|
0 commit comments