Skip to content

Commit 87ac369

Browse files
adamnschknutwalkerFlorentinD
committed
Use the correct node count for the IdMapBuilder
Co-Authored-By: Paul Horn <[email protected]> Co-Authored-By: Florentin Dörre <[email protected]>
1 parent be12d58 commit 87ac369

File tree

5 files changed

+24
-10
lines changed

5 files changed

+24
-10
lines changed

core/src/main/java/org/neo4j/graphalgo/core/GraphDimensionsReader.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,15 @@ public GraphDimensions apply(KernelTransaction transaction) throws RuntimeExcept
106106
Map<String, Integer> nodePropertyTokens = loadNodePropertyTokens(tokenRead);
107107
ResolvedPropertyMappings relProperties = loadPropertyMapping(tokenRead, setup.relationshipPropertyMappings());
108108

109+
// Every node will be counted once for every label it has.
109110
long nodeCount = labelTokenNodeLabelMappings.keyStream()
110111
.mapToLong(dataRead::countsForNode)
111112
.sum();
112-
final long allNodesCount = InternalReadOps.getHighestPossibleNodeCount(dataRead, api);
113+
final long highestPossibleNodeCount = InternalReadOps.getHighestPossibleNodeCount(dataRead, api);
114+
// This is also an upper bound on the node count.
113115
long finalNodeCount = labelTokenNodeLabelMappings.keys().contains(ANY_LABEL)
114-
? allNodesCount
115-
: Math.min(nodeCount, allNodesCount);
116+
? highestPossibleNodeCount
117+
: Math.min(nodeCount, highestPossibleNodeCount);
116118

117119
// TODO: this will double count relationships between distinct labels
118120
Map<RelationshipType, Long> relationshipCounts = relationshipProjectionMappings
@@ -133,7 +135,7 @@ public GraphDimensions apply(KernelTransaction transaction) throws RuntimeExcept
133135

134136
return ImmutableGraphDimensions.builder()
135137
.nodeCount(finalNodeCount)
136-
.highestNeoId(allNodesCount)
138+
.highestNeoId(highestPossibleNodeCount)
137139
.maxRelCount(maxRelCount)
138140
.relationshipCounts(relationshipCounts)
139141
.nodeLabelIds(labelTokenNodeLabelMappings.keys())

core/src/main/java/org/neo4j/graphalgo/core/loading/IdMap.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public IdMap withFilteredLabels(BitSet unionedBitSet, int concurrency) {
135135

136136
SparseNodeMapping newNodeToGraphIds = IdMapBuilder.buildSparseNodeMapping(
137137
newGraphIds,
138+
newGraphIds.size(),
138139
nodeToGraphIds.getCapacity(),
139140
concurrency,
140141
AllocationTracker.EMPTY

core/src/main/java/org/neo4j/graphalgo/core/loading/IdMapBuilder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,22 @@ public static IdMap build(
5959

6060
HugeLongArray graphIds = idMapBuilder.build();
6161

62-
SparseNodeMapping nodeToGraphIds = buildSparseNodeMapping(graphIds, highestNodeId, concurrency, tracker);
62+
SparseNodeMapping nodeToGraphIds = buildSparseNodeMapping(graphIds, idMapBuilder.size(), highestNodeId, concurrency, tracker);
6363
return new IdMap(graphIds, nodeToGraphIds, maybeLabelInformation, idMapBuilder.size());
6464
}
6565

6666
@NotNull
6767
static SparseNodeMapping buildSparseNodeMapping(
6868
HugeLongArray graphIds,
69+
long nodeCount,
6970
long highestNodeId,
7071
int concurrency,
7172
AllocationTracker tracker
7273
) {
7374
SparseNodeMapping.Builder nodeMappingBuilder = SparseNodeMapping.Builder.create(highestNodeId + 1, tracker);
7475
ParallelUtil.readParallel(
7576
concurrency,
76-
graphIds.size(),
77+
nodeCount,
7778
Pools.DEFAULT,
7879
(start, end) -> {
7980
try (HugeCursor<long[]> cursor = graphIds.initCursor(graphIds.newCursor(), start, end)) {

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
package org.neo4j.graphalgo.core.huge;
2121

22+
import org.apache.commons.lang3.mutable.MutableLong;
2223
import org.junit.jupiter.api.AfterEach;
2324
import org.junit.jupiter.api.BeforeEach;
2425
import org.junit.jupiter.api.Test;
@@ -36,14 +37,15 @@
3637
import static java.util.Arrays.asList;
3738
import static org.junit.jupiter.api.Assertions.assertEquals;
3839
import static org.neo4j.graphalgo.QueryRunner.runQuery;
40+
import static org.neo4j.graphalgo.QueryRunner.runQueryWithRowConsumer;
3941

4042
public class NodeFilteredGraphTest {
4143

4244
private GraphDbApi db;
4345

4446
private static final String DB_CYPHER =
4547
" CREATE" +
46-
" (a:Person)," +
48+
" (a:Person {name: 'Florentin'})," +
4749
" (b:Ignore:Person)," +
4850
" (c:Ignore:Person)," +
4951
" (d:Person)," +
@@ -62,7 +64,6 @@ void teardown() {
6264
db.shutdown();
6365
}
6466

65-
6667
@Test
6768
void filterDegree() {
6869
GraphStore graphStore = new StoreLoaderBuilder()
@@ -79,8 +80,16 @@ void filterDegree() {
7980
4
8081
);
8182

82-
long nodeIdOfA = graph.toMappedNodeId(0);
83-
assertEquals(1L, graph.degree(nodeIdOfA));
83+
String query = "MATCH(n {name: 'Florentin'}) " +
84+
"RETURN id(n) AS id";
85+
MutableLong neoNodeIdForA = new MutableLong();
86+
87+
runQueryWithRowConsumer(db, query, row -> {
88+
neoNodeIdForA.setValue(row.getNumber("id"));
89+
});
90+
91+
long mappedNodeIdForA = graph.toMappedNodeId(neoNodeIdForA.getValue());
92+
assertEquals(1L, graph.degree(mappedNodeIdForA));
8493
}
8594

8695
}

core/src/test/java/org/neo4j/graphalgo/core/loading/IdMapBuilderTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ void buildSparseNodeMappingWithPageSizeEntries() {
3636

3737
SparseNodeMapping hugeSparseLongArray = IdMapBuilder.buildSparseNodeMapping(
3838
hugeLongArray,
39+
hugeLongArray.size(),
3940
nodeId,
4041
1,
4142
AllocationTracker.EMPTY

0 commit comments

Comments
 (0)