|
20 | 20 | package org.neo4j.gds.core.cypher; |
21 | 21 |
|
22 | 22 | import org.immutables.value.Value; |
| 23 | +import org.jetbrains.annotations.NotNull; |
23 | 24 | import org.neo4j.gds.RelationshipType; |
24 | 25 | import org.neo4j.gds.annotation.ValueClass; |
25 | 26 | import org.neo4j.gds.api.AdjacencyList; |
|
37 | 38 |
|
38 | 39 | import static org.neo4j.gds.utils.StringFormatting.formatWithLocale; |
39 | 40 |
|
40 | | -public final class RelationshipIds { |
| 41 | +public final class RelationshipIds extends CypherGraphStore.StateVisitor.Adapter { |
41 | 42 |
|
| 43 | + private final GraphStore graphStore; |
| 44 | + private final TokenHolders tokenHolders; |
42 | 45 | private final List<RelationshipIdContext> relationshipIdContexts; |
| 46 | + private final List<UpdateListener> updateListeners; |
| 47 | + |
| 48 | + public interface UpdateListener { |
| 49 | + void onRelationshipIdsAdded(RelationshipIdContext relationshipIdContext); |
| 50 | + } |
43 | 51 |
|
44 | 52 | static RelationshipIds fromGraphStore(GraphStore graphStore, TokenHolders tokenHolders) { |
45 | 53 | var relationshipIdContexts = new ArrayList<RelationshipIdContext>(graphStore.relationshipTypes().size()); |
46 | 54 |
|
47 | | - graphStore.relationshipTypes().forEach(relType -> { |
48 | | - var relCount = graphStore.relationshipCount(relType); |
49 | | - var graph = (CSRGraph) graphStore.getGraph(relType); |
50 | | - var offsets = computeAccumulatedOffsets(graph); |
51 | | - int relTypeId = tokenHolders.relationshipTypeTokens().getIdByName(relType.name); |
52 | | - |
53 | | - List<RelationshipProperty> relationshipProperties = graphStore.relationshipPropertyKeys(relType) |
54 | | - .stream() |
55 | | - .map(relProperty -> graphStore.relationshipPropertyValues(relType, relProperty)) |
56 | | - .collect(Collectors.toList()); |
57 | | - |
58 | | - int[] propertyIds = relationshipProperties |
59 | | - .stream() |
60 | | - .mapToInt(relationshipProperty -> tokenHolders |
61 | | - .propertyKeyTokens() |
62 | | - .getIdByName(relationshipProperty.key())) |
63 | | - .toArray(); |
64 | | - |
65 | | - AdjacencyProperties[] adjacencyProperties = relationshipProperties |
66 | | - .stream() |
67 | | - .map(relationshipProperty -> relationshipProperty.values().propertiesList()) |
68 | | - .toArray(AdjacencyProperties[]::new); |
69 | | - |
70 | | - relationshipIdContexts.add(ImmutableRelationshipIdContext.of(relType, relTypeId, relCount, graph, offsets, propertyIds, adjacencyProperties)); |
71 | | - }); |
72 | | - return new RelationshipIds(relationshipIdContexts); |
| 55 | + graphStore.relationshipTypes() |
| 56 | + .stream() |
| 57 | + .map(relType -> relationshipIdContextFromRelType(graphStore, tokenHolders, relType)) |
| 58 | + .forEach(relationshipIdContexts::add); |
| 59 | + return new RelationshipIds(graphStore, tokenHolders, relationshipIdContexts); |
73 | 60 | } |
74 | 61 |
|
75 | | - private RelationshipIds(List<RelationshipIdContext> relationshipIdContexts) { |
| 62 | + private RelationshipIds(GraphStore graphStore, TokenHolders tokenHolders, List<RelationshipIdContext> relationshipIdContexts) { |
| 63 | + this.graphStore = graphStore; |
| 64 | + this.tokenHolders = tokenHolders; |
76 | 65 | this.relationshipIdContexts = relationshipIdContexts; |
77 | | - } |
78 | | - |
79 | | - public List<RelationshipIdContext> relationshipIdContexts() { |
80 | | - return relationshipIdContexts; |
| 66 | + this.updateListeners = new ArrayList<>(); |
81 | 67 | } |
82 | 68 |
|
83 | 69 | public <T> T resolveRelationshipId(long relationshipId, ResolvedRelationshipIdFunction<T> relationshipIdConsumer) { |
@@ -108,6 +94,58 @@ public <T> T resolveRelationshipId(long relationshipId, ResolvedRelationshipIdFu |
108 | 94 | throw new IllegalArgumentException(formatWithLocale("No relationship with id %d was found.", relationshipId)); |
109 | 95 | } |
110 | 96 |
|
| 97 | + public void registerUpdateListener(UpdateListener updateListener) { |
| 98 | + this.updateListeners.add(updateListener); |
| 99 | + // replay added relationship id contexts |
| 100 | + relationshipIdContexts.forEach(updateListener::onRelationshipIdsAdded); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void relationshipTypeAdded(String relationshipType) { |
| 105 | + var relationshipIdContext = relationshipIdContextFromRelType(graphStore, tokenHolders, RelationshipType.of(relationshipType)); |
| 106 | + relationshipIdContexts.add(relationshipIdContext); |
| 107 | + updateListeners.forEach(updateListener -> updateListener.onRelationshipIdsAdded(relationshipIdContext)); |
| 108 | + } |
| 109 | + |
| 110 | + @NotNull |
| 111 | + private static RelationshipIdContext relationshipIdContextFromRelType( |
| 112 | + GraphStore graphStore, |
| 113 | + TokenHolders tokenHolders, |
| 114 | + RelationshipType relType |
| 115 | + ) { |
| 116 | + var relCount = graphStore.relationshipCount(relType); |
| 117 | + var graph = (CSRGraph) graphStore.getGraph(relType); |
| 118 | + var offsets = computeAccumulatedOffsets(graph); |
| 119 | + int relTypeId = tokenHolders.relationshipTypeTokens().getIdByName(relType.name); |
| 120 | + |
| 121 | + List<RelationshipProperty> relationshipProperties = graphStore.relationshipPropertyKeys(relType) |
| 122 | + .stream() |
| 123 | + .map(relProperty -> graphStore.relationshipPropertyValues(relType, relProperty)) |
| 124 | + .collect(Collectors.toList()); |
| 125 | + |
| 126 | + int[] propertyIds = relationshipProperties |
| 127 | + .stream() |
| 128 | + .mapToInt(relationshipProperty -> tokenHolders |
| 129 | + .propertyKeyTokens() |
| 130 | + .getIdByName(relationshipProperty.key())) |
| 131 | + .toArray(); |
| 132 | + |
| 133 | + AdjacencyProperties[] adjacencyProperties = relationshipProperties |
| 134 | + .stream() |
| 135 | + .map(relationshipProperty -> relationshipProperty.values().propertiesList()) |
| 136 | + .toArray(AdjacencyProperties[]::new); |
| 137 | + |
| 138 | + return ImmutableRelationshipIdContext.of( |
| 139 | + relType, |
| 140 | + relTypeId, |
| 141 | + relCount, |
| 142 | + graph, |
| 143 | + offsets, |
| 144 | + propertyIds, |
| 145 | + adjacencyProperties |
| 146 | + ); |
| 147 | + } |
| 148 | + |
111 | 149 | private static HugeLongArray computeAccumulatedOffsets(Graph graph) { |
112 | 150 | var offsets = HugeLongArray.newArray(graph.nodeCount()); |
113 | 151 |
|
|
0 commit comments