Skip to content

Commit 88ccd78

Browse files
s1ckDarthMax
andcommitted
Add null checks for inverse index related methods
Co-Authored-By: Max Kießling <max.kiessling@neotechnology.com>
1 parent f87bac6 commit 88ccd78

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

core/src/main/java/org/neo4j/gds/core/huge/NodeFilteredGraph.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ public int degreeWithoutParallelRelationships(long nodeId) {
147147

148148
@Override
149149
public int degreeInverse(long nodeId) {
150+
validateIndexInverse();
150151
int cachedDegree = this.degreeInverseCache.get(nodeId);
151152
if (cachedDegree != NO_DEGREE) {
152153
return cachedDegree;
@@ -253,6 +254,7 @@ public void forEachRelationship(long nodeId, double fallbackValue, RelationshipW
253254

254255
@Override
255256
public void forEachInverseRelationship(long nodeId, RelationshipConsumer consumer) {
257+
validateIndexInverse();
256258
super.forEachInverseRelationship(
257259
filteredIdMap.toRootNodeId(nodeId),
258260
(s, t) -> filterAndConsume(s, t, consumer)
@@ -265,6 +267,7 @@ public void forEachInverseRelationship(
265267
double fallbackValue,
266268
RelationshipWithPropertyConsumer consumer
267269
) {
270+
validateIndexInverse();
268271
super.forEachInverseRelationship(
269272
filteredIdMap.toRootNodeId(nodeId),
270273
fallbackValue,
@@ -363,6 +366,14 @@ public NodePropertyValues nodeProperties(String propertyKey) {
363366
return new FilteredNodePropertyValues.FilteredToOriginalNodePropertyValues(properties, this);
364367
}
365368

369+
private void validateIndexInverse() {
370+
if (this.degreeInverseCache == null) {
371+
throw new UnsupportedOperationException(
372+
"Cannot access inverse relationships as this graph is not inverse indexed."
373+
);
374+
}
375+
}
376+
366377
private boolean filterAndConsume(long source, long target, RelationshipConsumer consumer) {
367378
if (filteredIdMap.containsRootNodeId(source) && filteredIdMap.containsRootNodeId(target)) {
368379
long internalSourceId = filteredIdMap.toFilteredNodeId(source);

core/src/test/java/org/neo4j/gds/core/huge/NodeFilteredGraphTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.function.Function;
4040

4141
import static org.assertj.core.api.Assertions.assertThat;
42+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4243
import static org.junit.jupiter.api.Assertions.assertEquals;
4344

4445
@GdlExtension
@@ -83,7 +84,8 @@ void filteredIdMapThatIncludesAllNodes() {
8384
unfilteredGraph.forEachNode(nodeId -> {
8485
long filteredNodeId = filteredGraph.toFilteredNodeId(nodeId);
8586
if (unfilteredGraph.hasLabel(nodeId, filterLabel)) {
86-
assertThat(filteredGraph.toOriginalNodeId(filteredNodeId)).isEqualTo(unfilteredGraph.toOriginalNodeId(nodeId));
87+
assertThat(filteredGraph.toOriginalNodeId(filteredNodeId))
88+
.isEqualTo(unfilteredGraph.toOriginalNodeId(nodeId));
8789
} else {
8890
assertThat(filteredNodeId).isEqualTo(IdMap.NOT_FOUND);
8991
}
@@ -230,6 +232,24 @@ void shouldStreamRelationshipsCorrectly() {
230232

231233
}
232234

235+
@Test
236+
void throwDegreeInverseIfNotIndexed() {
237+
var graph = GdlFactory.of("(a:A)-->(b:B)-->(:B)").build().getGraph(NodeLabel.of("B"));
238+
239+
assertThatThrownBy(() -> graph.degreeInverse(0))
240+
.isInstanceOf(UnsupportedOperationException.class)
241+
.hasMessageContaining("Cannot access inverse relationships as this graph is not inverse indexed.");
242+
}
243+
244+
@Test
245+
void throwForeachInverseRelationshipIfNotIndexed() {
246+
var graph = GdlFactory.of("(a:A)-->(b:B)-->(:B)").build().getGraph(NodeLabel.of("B"));
247+
248+
assertThatThrownBy(() -> graph.forEachInverseRelationship(0, (sourceNodeId, targetNodeId) -> true))
249+
.isInstanceOf(UnsupportedOperationException.class)
250+
.hasMessageContaining("Cannot access inverse relationships as this graph is not inverse indexed.");
251+
}
252+
233253
Function<String, Long> filteredIdFunction(Graph graph) {
234254
return (variable) -> graph.toMappedNodeId(idFunction.of(variable));
235255
}

0 commit comments

Comments
 (0)