Skip to content

Commit

Permalink
Fix off-by-one for relationship count vs max id
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwalker committed Sep 28, 2023
1 parent 4c9a724 commit 28546cf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public abstract class InMemoryRelationshipCursor
private final List<PropertyCursor[]> propertyCursorCache;
private MutableDoubleList propertyValuesCache;

protected long maxRelationshipId;
protected long totalRelationshipCount;

protected long sourceId;
protected long targetId;
Expand All @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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()) {
Expand All @@ -75,16 +75,20 @@ public boolean scanBatch(AllRelationshipsScan scan, int sizeHint) {
reset();
}

highMark = maxRelationshipId;
highMarkExclusive = totalRelationshipCount;
return ((BaseRecordScan<AbstractInMemoryRelationshipScanCursor>) 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);
Expand Down

0 comments on commit 28546cf

Please sign in to comment.