Add WindowIterator and rename Scroll to Window.

The intend of WindowIterator is to support users who need to iterate multiple windows. It keeps track of the position and loads the next window if needed so that the user does not have to interact with the position at all.

Also remove the Window methods to get the first/last position and enforce the index based variant.

Update the documentation to make use of the newly introduced API.

See: #2151
Original Pull Request: #2787
This commit is contained in:
Christoph Strobl
2023-03-15 09:23:53 +01:00
parent 035965a3a2
commit 0c0c1afc8e
12 changed files with 357 additions and 121 deletions

View File

@@ -0,0 +1,131 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.domain;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.data.domain.WindowIterator.WindowIteratorBuilder;
/**
* @author Christoph Strobl
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class WindowIteratorUnitTests<T> {
@Mock Function<ScrollPosition, Window<T>> fkt;
@Mock Window<T> window;
@Mock ScrollPosition scrollPosition;
@Captor ArgumentCaptor<ScrollPosition> scrollCaptor;
@BeforeEach
void beforeEach() {
when(fkt.apply(any())).thenReturn(window);
}
@Test // GH-2151
void loadsDataOnCreation() {
WindowIteratorBuilder<T> of = WindowIterator.of(fkt);
verifyNoInteractions(fkt);
of.startingAt(scrollPosition);
verify(fkt).apply(eq(scrollPosition));
}
@Test // GH-2151
void hasNextReturnsFalseIfNoDataAvailable() {
when(window.isLast()).thenReturn(true);
when(window.isEmpty()).thenReturn(true);
assertThat(WindowIterator.of(fkt).startingAt(scrollPosition).hasNext()).isFalse();
}
@Test // GH-2151
void hasNextReturnsTrueIfDataAvailableButOnlyOnePage() {
when(window.isLast()).thenReturn(true);
when(window.isEmpty()).thenReturn(false);
assertThat(WindowIterator.of(fkt).startingAt(scrollPosition).hasNext()).isTrue();
}
@Test // GH-2151
void allowsToIterateAllWindows() {
ScrollPosition p1 = mock(ScrollPosition.class);
ScrollPosition p2 = mock(ScrollPosition.class);
when(window.isEmpty()).thenReturn(false, false, false);
when(window.isLast()).thenReturn(false, false, true);
when(window.hasNext()).thenReturn(true, true, false);
when(window.size()).thenReturn(1, 1, 1);
when(window.positionAt(anyInt())).thenReturn(p1, p2);
when(window.getContent()).thenReturn(List.of((T) "0"), List.of((T) "1"), List.of((T) "2"));
WindowIterator<T> iterator = WindowIterator.of(fkt).startingAt(scrollPosition);
List<T> capturedResult = new ArrayList<>(3);
while (iterator.hasNext()) {
capturedResult.addAll(iterator.next());
}
verify(fkt, times(3)).apply(scrollCaptor.capture());
assertThat(scrollCaptor.getAllValues()).containsExactly(scrollPosition, p1, p2);
assertThat(capturedResult).containsExactly((T) "0", (T) "1", (T) "2");
}
@Test // GH-2151
void stopsAfterFirstPageIfOnlyOneWindowAvailable() {
ScrollPosition p1 = mock(ScrollPosition.class);
when(window.isEmpty()).thenReturn(false);
when(window.isLast()).thenReturn(true);
when(window.hasNext()).thenReturn(false);
when(window.size()).thenReturn(1);
when(window.positionAt(anyInt())).thenReturn(p1);
when(window.getContent()).thenReturn(List.of((T) "0"));
WindowIterator<T> iterator = WindowIterator.of(fkt).startingAt(scrollPosition);
List<T> capturedResult = new ArrayList<>(1);
while (iterator.hasNext()) {
capturedResult.addAll(iterator.next());
}
verify(fkt).apply(scrollCaptor.capture());
assertThat(scrollCaptor.getAllValues()).containsExactly(scrollPosition);
assertThat(capturedResult).containsExactly((T) "0");
}
}

View File

@@ -23,31 +23,32 @@ import java.util.function.IntFunction;
import org.junit.jupiter.api.Test;
/**
* Unit tests for {@link Scroll}.
* Unit tests for {@link Window}.
*
* @author Mark Paluch
* @author Christoph Strobl
*/
class ScrollUnitTests {
class WindowUnitTests {
@Test // GH-2151
void equalsAndHashCode() {
IntFunction<OffsetScrollPosition> positionFunction = OffsetScrollPosition.positionFunction(0);
Scroll<Integer> one = Scroll.from(List.of(1, 2, 3), positionFunction);
Scroll<Integer> two = Scroll.from(List.of(1, 2, 3), positionFunction);
Window<Integer> one = Window.from(List.of(1, 2, 3), positionFunction);
Window<Integer> two = Window.from(List.of(1, 2, 3), positionFunction);
assertThat(one).isEqualTo(two).hasSameHashCodeAs(two);
assertThat(one.equals(two)).isTrue();
assertThat(Scroll.from(List.of(1, 2, 3), positionFunction, true)).isNotEqualTo(two).doesNotHaveSameHashCodeAs(two);
assertThat(Window.from(List.of(1, 2, 3), positionFunction, true)).isNotEqualTo(two).doesNotHaveSameHashCodeAs(two);
}
@Test // GH-2151
void allowsIteration() {
Scroll<Integer> scroll = Scroll.from(List.of(1, 2, 3), OffsetScrollPosition.positionFunction(0));
Window<Integer> window = Window.from(List.of(1, 2, 3), OffsetScrollPosition.positionFunction(0));
for (Integer integer : scroll) {
for (Integer integer : window) {
assertThat(integer).isBetween(1, 3);
}
}
@@ -55,15 +56,15 @@ class ScrollUnitTests {
@Test // GH-2151
void shouldCreateCorrectPositions() {
Scroll<Integer> scroll = Scroll.from(List.of(1, 2, 3), OffsetScrollPosition.positionFunction(0));
Window<Integer> window = Window.from(List.of(1, 2, 3), OffsetScrollPosition.positionFunction(0));
assertThat(scroll.firstPosition()).isEqualTo(OffsetScrollPosition.of(1));
assertThat(scroll.lastPosition()).isEqualTo(OffsetScrollPosition.of(3));
assertThat(window.positionAt(0)).isEqualTo(OffsetScrollPosition.of(1));
assertThat(window.positionAt(window.size() - 1)).isEqualTo(OffsetScrollPosition.of(3));
// by index
assertThat(scroll.positionAt(1)).isEqualTo(OffsetScrollPosition.of(2));
assertThat(window.positionAt(1)).isEqualTo(OffsetScrollPosition.of(2));
// by object
assertThat(scroll.positionAt(Integer.valueOf(1))).isEqualTo(OffsetScrollPosition.of(1));
assertThat(window.positionAt(Integer.valueOf(1))).isEqualTo(OffsetScrollPosition.of(1));
}
}

View File

@@ -28,7 +28,7 @@ import org.reactivestreams.Publisher;
import org.springframework.data.domain.OffsetScrollPosition;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Scroll;
import org.springframework.data.domain.Window;
import org.springframework.data.domain.Sort;
import org.springframework.test.util.ReflectionTestUtils;
@@ -231,7 +231,7 @@ class ParametersUnitTests {
Page<Object> customPageable(SomePageable pageable);
Scroll<Object> customScrollPosition(OffsetScrollPosition request);
Window<Object> customScrollPosition(OffsetScrollPosition request);
}
interface SomePageable extends Pageable {}

View File

@@ -19,6 +19,7 @@ import static org.assertj.core.api.Assertions.*;
import io.vavr.collection.Seq;
import io.vavr.control.Option;
import org.springframework.data.domain.Window;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@@ -32,7 +33,6 @@ import org.eclipse.collections.api.list.ImmutableList;
import org.junit.jupiter.api.Test;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Scroll;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.domain.Slice;
import org.springframework.data.projection.ProjectionFactory;
@@ -351,15 +351,15 @@ class QueryMethodUnitTests {
ImmutableList<User> returnsEclipseCollection();
Scroll<User> cursorWindow(ScrollPosition cursorRequest);
Window<User> cursorWindow(ScrollPosition cursorRequest);
Mono<Scroll<User>> reactiveCursorWindow(ScrollPosition cursorRequest);
Mono<Window<User>> reactiveCursorWindow(ScrollPosition cursorRequest);
Flux<Scroll<User>> invalidReactiveCursorWindow(ScrollPosition cursorRequest);
Flux<Window<User>> invalidReactiveCursorWindow(ScrollPosition cursorRequest);
Page<User> cursorWindowMethodWithInvalidReturnType(ScrollPosition cursorRequest);
Scroll<User> cursorWindowWithoutScrollPosition();
Window<User> cursorWindowWithoutScrollPosition();
}
class User {