Skip to content

Commit

Permalink
Add null checks for inverse index related methods
Browse files Browse the repository at this point in the history
Co-Authored-By: Max Kießling <[email protected]>
  • Loading branch information
s1ck and DarthMax committed Dec 21, 2023
1 parent f87bac6 commit 88ccd78
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
11 changes: 11 additions & 0 deletions core/src/main/java/org/neo4j/gds/core/huge/NodeFilteredGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public int degreeWithoutParallelRelationships(long nodeId) {

@Override
public int degreeInverse(long nodeId) {
validateIndexInverse();
int cachedDegree = this.degreeInverseCache.get(nodeId);
if (cachedDegree != NO_DEGREE) {
return cachedDegree;
Expand Down Expand Up @@ -253,6 +254,7 @@ public void forEachRelationship(long nodeId, double fallbackValue, RelationshipW

@Override
public void forEachInverseRelationship(long nodeId, RelationshipConsumer consumer) {
validateIndexInverse();
super.forEachInverseRelationship(
filteredIdMap.toRootNodeId(nodeId),
(s, t) -> filterAndConsume(s, t, consumer)
Expand All @@ -265,6 +267,7 @@ public void forEachInverseRelationship(
double fallbackValue,
RelationshipWithPropertyConsumer consumer
) {
validateIndexInverse();
super.forEachInverseRelationship(
filteredIdMap.toRootNodeId(nodeId),
fallbackValue,
Expand Down Expand Up @@ -363,6 +366,14 @@ public NodePropertyValues nodeProperties(String propertyKey) {
return new FilteredNodePropertyValues.FilteredToOriginalNodePropertyValues(properties, this);
}

private void validateIndexInverse() {
if (this.degreeInverseCache == null) {
throw new UnsupportedOperationException(
"Cannot access inverse relationships as this graph is not inverse indexed."
);
}
}

private boolean filterAndConsume(long source, long target, RelationshipConsumer consumer) {
if (filteredIdMap.containsRootNodeId(source) && filteredIdMap.containsRootNodeId(target)) {
long internalSourceId = filteredIdMap.toFilteredNodeId(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.function.Function;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;

@GdlExtension
Expand Down Expand Up @@ -83,7 +84,8 @@ void filteredIdMapThatIncludesAllNodes() {
unfilteredGraph.forEachNode(nodeId -> {
long filteredNodeId = filteredGraph.toFilteredNodeId(nodeId);
if (unfilteredGraph.hasLabel(nodeId, filterLabel)) {
assertThat(filteredGraph.toOriginalNodeId(filteredNodeId)).isEqualTo(unfilteredGraph.toOriginalNodeId(nodeId));
assertThat(filteredGraph.toOriginalNodeId(filteredNodeId))
.isEqualTo(unfilteredGraph.toOriginalNodeId(nodeId));
} else {
assertThat(filteredNodeId).isEqualTo(IdMap.NOT_FOUND);
}
Expand Down Expand Up @@ -230,6 +232,24 @@ void shouldStreamRelationshipsCorrectly() {

}

@Test
void throwDegreeInverseIfNotIndexed() {
var graph = GdlFactory.of("(a:A)-->(b:B)-->(:B)").build().getGraph(NodeLabel.of("B"));

assertThatThrownBy(() -> graph.degreeInverse(0))
.isInstanceOf(UnsupportedOperationException.class)
.hasMessageContaining("Cannot access inverse relationships as this graph is not inverse indexed.");
}

@Test
void throwForeachInverseRelationshipIfNotIndexed() {
var graph = GdlFactory.of("(a:A)-->(b:B)-->(:B)").build().getGraph(NodeLabel.of("B"));

assertThatThrownBy(() -> graph.forEachInverseRelationship(0, (sourceNodeId, targetNodeId) -> true))
.isInstanceOf(UnsupportedOperationException.class)
.hasMessageContaining("Cannot access inverse relationships as this graph is not inverse indexed.");
}

Function<String, Long> filteredIdFunction(Graph graph) {
return (variable) -> graph.toMappedNodeId(idFunction.of(variable));
}
Expand Down

0 comments on commit 88ccd78

Please sign in to comment.