From 28546cf12a3be59a83da970a1cdaada6446fc754 Mon Sep 17 00:00:00 2001 From: Paul Horn Date: Thu, 28 Sep 2023 14:40:54 +0200 Subject: [PATCH] Fix off-by-one for relationship count vs max id --- .../InMemoryRelationshipCursor.java | 6 +++--- ...bstractInMemoryRelationshipScanCursor.java | 20 +++++++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/compatibility/api/storage-engine-adapter/src/main/java/org/neo4j/gds/storageengine/InMemoryRelationshipCursor.java b/compatibility/api/storage-engine-adapter/src/main/java/org/neo4j/gds/storageengine/InMemoryRelationshipCursor.java index dd0c8dfd385..8a7192039ec 100644 --- a/compatibility/api/storage-engine-adapter/src/main/java/org/neo4j/gds/storageengine/InMemoryRelationshipCursor.java +++ b/compatibility/api/storage-engine-adapter/src/main/java/org/neo4j/gds/storageengine/InMemoryRelationshipCursor.java @@ -52,7 +52,7 @@ public abstract class InMemoryRelationshipCursor private final List propertyCursorCache; private MutableDoubleList propertyValuesCache; - protected long maxRelationshipId; + protected long totalRelationshipCount; protected long sourceId; protected long targetId; @@ -73,7 +73,7 @@ public InMemoryRelationshipCursor(CypherGraphStore graphStore, TokenHolders toke this.propertyCursorCache = new ArrayList<>(); this.propertyValuesCache = new DoubleArrayList(); - this.maxRelationshipId = 0; + this.totalRelationshipCount = 0; this.graphStore.relationshipIds().registerUpdateListener(this); } @@ -138,7 +138,7 @@ public void onRelationshipIdsAdded(RelationshipIds.RelationshipIdContext relatio ); var newSize = this.propertyCursorCache.stream().mapToInt(cursor -> cursor.length).max().orElse(0); this.propertyValuesCache = new DoubleArrayList(new double[newSize]); - this.maxRelationshipId += relationshipIdContext.relationshipCount(); + this.totalRelationshipCount += relationshipIdContext.relationshipCount(); } @Override diff --git a/compatibility/api/storage-engine-adapter/src/main/java/org/neo4j/internal/recordstorage/AbstractInMemoryRelationshipScanCursor.java b/compatibility/api/storage-engine-adapter/src/main/java/org/neo4j/internal/recordstorage/AbstractInMemoryRelationshipScanCursor.java index f4af14fb8b7..b495317b5d8 100644 --- a/compatibility/api/storage-engine-adapter/src/main/java/org/neo4j/internal/recordstorage/AbstractInMemoryRelationshipScanCursor.java +++ b/compatibility/api/storage-engine-adapter/src/main/java/org/neo4j/internal/recordstorage/AbstractInMemoryRelationshipScanCursor.java @@ -30,7 +30,7 @@ public abstract class AbstractInMemoryRelationshipScanCursor extends InMemoryRelationshipCursor implements StorageRelationshipScanCursor { - private long highMark; + private long highMarkExclusive; public AbstractInMemoryRelationshipScanCursor(CypherGraphStore graphStore, TokenHolders tokenHolders) { super(graphStore, tokenHolders); @@ -41,14 +41,14 @@ public void scan() { reset(); this.sourceId = 0; this.selection = RelationshipSelection.ALL_RELATIONSHIPS; - this.highMark = maxRelationshipId; + this.highMarkExclusive = totalRelationshipCount; } @Override public void single(long reference) { reset(); setId(reference - 1); - this.highMark = reference; + this.highMarkExclusive = reference + 1; this.selection = RelationshipSelection.ALL_RELATIONSHIPS; initializeForRelationshipReference(reference); @@ -57,7 +57,7 @@ public void single(long reference) { @Override public boolean next() { if (super.next()) { - return getId() <= highMark; + return getId() < highMarkExclusive; } else { this.sourceId++; if (this.sourceId >= graphStore.nodeCount()) { @@ -75,16 +75,20 @@ public boolean scanBatch(AllRelationshipsScan scan, int sizeHint) { reset(); } - highMark = maxRelationshipId; + highMarkExclusive = totalRelationshipCount; return ((BaseRecordScan) scan).scanBatch(sizeHint, this); } - public boolean scanRange(long start, long stop) { + public boolean scanRange(long start, long stopInclusive) { reset(); this.selection = RelationshipSelection.ALL_RELATIONSHIPS; - highMark = min(stop, maxRelationshipId); + if (stopInclusive < Long.MAX_VALUE) { + highMarkExclusive = min(stopInclusive + 1, totalRelationshipCount); + } else { + highMarkExclusive = totalRelationshipCount; + } - if (start > maxRelationshipId) { + if (start >= highMarkExclusive) { return false; } initializeForRelationshipReference(start);