Skip to content

Align OffsetScrolling to zero-based indexes #2891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>7.3.0-SNAPSHOT</version>
<version>7.3.0-GH-2890-SNAPSHOT</version>

<name>Spring Data Neo4j</name>
<description>Next generation Object-Graph-Mapping for Spring Data.</description>
Expand Down Expand Up @@ -113,7 +113,7 @@

<skipUnitTests>${skipTests}</skipUnitTests>

<springdata.commons>3.3.0-SNAPSHOT</springdata.commons>
<springdata.commons>3.3.x-3070-SNAPSHOT</springdata.commons>
</properties>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,9 @@ private QueryFragments createQueryFragments(@Nullable Condition condition, Sort

queryFragments.setRequiresReverseSort(keysetScrollPosition.scrollsBackward());
} else if (scrollPosition instanceof OffsetScrollPosition offsetScrollPosition) {
queryFragments.setSkip(offsetScrollPosition.getOffset());
if (!offsetScrollPosition.isInitial()) {
queryFragments.setSkip(offsetScrollPosition.getOffset() + 1);
}
queryFragments.setLimit(limitModifier.apply(pagingParameter.isUnpaged() ? maxResults.intValue() : pagingParameter.getPageSize()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public Window<R> scroll(ScrollPosition scrollPosition) {

var skip = scrollPosition.isInitial()
? 0
: (scrollPosition instanceof OffsetScrollPosition offsetScrollPosition) ? offsetScrollPosition.getOffset()
: (scrollPosition instanceof OffsetScrollPosition offsetScrollPosition) ? offsetScrollPosition.getOffset() + 1
: 0;

Condition condition = scrollPosition instanceof KeysetScrollPosition keysetScrollPosition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ final Window<R> scroll(ScrollPosition scrollPosition, List<R> rawResult, Neo4jPe

var skip = scrollPosition.isInitial()
? 0
: (scrollPosition instanceof OffsetScrollPosition offsetScrollPosition) ? offsetScrollPosition.getOffset()
: (scrollPosition instanceof OffsetScrollPosition offsetScrollPosition) ? offsetScrollPosition.getOffset() + 1
: 0;

var scrollDirection = scrollPosition instanceof KeysetScrollPosition keysetScrollPosition ? keysetScrollPosition.getDirection() : ScrollPosition.Direction.FORWARD;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ final Window<?> createWindow(ResultProcessor resultProcessor, boolean incrementL

return Window.from(getSubList(rawResult, limit, scrollDirection), v -> {
if (scrollPosition instanceof OffsetScrollPosition offsetScrollPosition) {
return offsetScrollPosition.advanceBy(v + limit);
return offsetScrollPosition.advanceBy(v);
} else {
var accessor = neo4jPersistentEntity.getPropertyAccessor(rawResult.get(v));
var keys = new LinkedHashMap<String, Object>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ static QueryFragmentsAndParameters forConditionWithScrollPosition(Neo4jPersisten
if (scrollPosition instanceof OffsetScrollPosition offsetScrollPosition) {
skip = offsetScrollPosition.isInitial()
? 0
: offsetScrollPosition.getOffset();
: offsetScrollPosition.getOffset() + 1;

return forCondition(entityMetaData, condition, null, sort, null, limit, skip, includeField);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public Mono<Window<R>> scroll(ScrollPosition scrollPosition) {

var skip = scrollPosition.isInitial()
? 0
: (scrollPosition instanceof OffsetScrollPosition offsetScrollPosition) ? offsetScrollPosition.getOffset()
: (scrollPosition instanceof OffsetScrollPosition offsetScrollPosition) ? offsetScrollPosition.getOffset() + 1
: 0;

Condition condition = scrollPosition instanceof KeysetScrollPosition keysetScrollPosition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ void scrollByExampleWithNoOffset(@Autowired QueryDSLPersonRepository repository)
Predicate predicate = Expressions.predicate(Ops.EQ, firstNamePath, Expressions.asString("Helge"))
.or(Expressions.predicate(Ops.EQ, lastNamePath, Expressions.asString("B.")));

Window<Person> peopleWindow = repository.findBy(predicate, q -> q.limit(1).sortBy(Sort.by("firstName").descending()).scroll(ScrollPosition.offset(0)));
Window<Person> peopleWindow = repository.findBy(predicate, q -> q.limit(1).sortBy(Sort.by("firstName").descending()).scroll(ScrollPosition.offset()));

assertThat(peopleWindow.getContent()).extracting(Person::getFirstName)
.containsExactlyInAnyOrder("Helge");

assertThat(peopleWindow.isLast()).isFalse();
assertThat(peopleWindow.hasNext()).isTrue();

assertThat(peopleWindow.positionAt(peopleWindow.getContent().get(0))).isEqualTo(ScrollPosition.offset(1));
assertThat(peopleWindow.positionAt(peopleWindow.getContent().get(0))).isEqualTo(ScrollPosition.offset(0));
}

@Test
Expand All @@ -154,14 +154,14 @@ void scrollByExampleWithOffset(@Autowired QueryDSLPersonRepository repository) {
Predicate predicate = Expressions.predicate(Ops.EQ, firstNamePath, Expressions.asString("Helge"))
.or(Expressions.predicate(Ops.EQ, lastNamePath, Expressions.asString("B.")));

Window<Person> peopleWindow = repository.findBy(predicate, q -> q.limit(1).sortBy(Sort.by("firstName").descending()).scroll(ScrollPosition.offset(1)));
Window<Person> peopleWindow = repository.findBy(predicate, q -> q.limit(1).sortBy(Sort.by("firstName").descending()).scroll(ScrollPosition.offset(0)));

assertThat(peopleWindow.getContent()).extracting(Person::getFirstName)
.containsExactlyInAnyOrder("Bela");

assertThat(peopleWindow.isLast()).isTrue();

assertThat(peopleWindow.positionAt(peopleWindow.getContent().get(0))).isEqualTo(ScrollPosition.offset(2));
assertThat(peopleWindow.positionAt(peopleWindow.getContent().get(0))).isEqualTo(ScrollPosition.offset(1));
}

@Test
Expand All @@ -170,7 +170,7 @@ void scrollByExampleWithContinuingOffset(@Autowired QueryDSLPersonRepository rep
Predicate predicate = Expressions.predicate(Ops.EQ, firstNamePath, Expressions.asString("Helge"))
.or(Expressions.predicate(Ops.EQ, lastNamePath, Expressions.asString("B.")));

Window<Person> peopleWindow = repository.findBy(predicate, q -> q.limit(1).sortBy(Sort.by("firstName").descending()).scroll(ScrollPosition.offset(0)));
Window<Person> peopleWindow = repository.findBy(predicate, q -> q.limit(1).sortBy(Sort.by("firstName").descending()).scroll(ScrollPosition.offset()));
ScrollPosition currentPosition = peopleWindow.positionAt(peopleWindow.getContent().get(0));
peopleWindow = repository.findBy(predicate, q -> q.limit(1).scroll(currentPosition));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2923,7 +2923,7 @@ void scrollByExample(@Autowired PersonRepository repository) {

Example<PersonWithAllConstructor> example = Example.of(sameValuePerson,
ExampleMatcher.matchingAll().withIgnoreNullValues());
Window<PersonWithAllConstructor> person = repository.findBy(example, q -> q.sortBy(Sort.by("name")).limit(1).scroll(ScrollPosition.offset(0)));
Window<PersonWithAllConstructor> person = repository.findBy(example, q -> q.sortBy(Sort.by("name")).limit(1).scroll(ScrollPosition.offset()));

assertThat(person).isNotNull();
assertThat(person.getContent().get(0)).isEqualTo(person1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ void scrollByExampleWithNoOffset(@Autowired QueryDSLPersonRepository repository)
Predicate predicate = Expressions.predicate(Ops.EQ, firstNamePath, Expressions.asString("Helge"))
.or(Expressions.predicate(Ops.EQ, lastNamePath, Expressions.asString("B.")));

repository.findBy(predicate, q -> q.limit(1).sortBy(Sort.by("firstName").descending()).scroll(ScrollPosition.offset(0)))
repository.findBy(predicate, q -> q.limit(1).sortBy(Sort.by("firstName").descending()).scroll(ScrollPosition.offset()))
.as(StepVerifier::create)
.expectNextMatches(peopleWindow -> {

Expand All @@ -206,7 +206,7 @@ void scrollByExampleWithNoOffset(@Autowired QueryDSLPersonRepository repository)
assertThat(peopleWindow.isLast()).isFalse();
assertThat(peopleWindow.hasNext()).isTrue();

assertThat(peopleWindow.positionAt(peopleWindow.getContent().get(0))).isEqualTo(ScrollPosition.offset(1));
assertThat(peopleWindow.positionAt(peopleWindow.getContent().get(0))).isEqualTo(ScrollPosition.offset(0));
return true;
}).verifyComplete();
}
Expand All @@ -217,14 +217,14 @@ void scrollByExampleWithOffset(@Autowired QueryDSLPersonRepository repository) {
Predicate predicate = Expressions.predicate(Ops.EQ, firstNamePath, Expressions.asString("Helge"))
.or(Expressions.predicate(Ops.EQ, lastNamePath, Expressions.asString("B.")));

repository.findBy(predicate, q -> q.limit(1).sortBy(Sort.by("firstName").descending()).scroll(ScrollPosition.offset(1)))
repository.findBy(predicate, q -> q.limit(1).sortBy(Sort.by("firstName").descending()).scroll(ScrollPosition.offset(0)))
.as(StepVerifier::create)
.expectNextMatches(peopleWindow -> {
assertThat(peopleWindow.getContent()).extracting(Person::getFirstName)
.containsExactlyInAnyOrder("Bela");

assertThat(peopleWindow.isLast()).isTrue();
assertThat(peopleWindow.positionAt(peopleWindow.getContent().get(0))).isEqualTo(ScrollPosition.offset(2));
assertThat(peopleWindow.positionAt(peopleWindow.getContent().get(0))).isEqualTo(ScrollPosition.offset(1));
return true;
}).verifyComplete();
}
Expand Down