|
27 | 27 | import java.time.OffsetDateTime; |
28 | 28 | import java.time.ZoneOffset; |
29 | 29 | import java.time.temporal.ChronoUnit; |
30 | | -import java.util.ArrayList; |
31 | | -import java.util.Arrays; |
32 | | -import java.util.Collections; |
33 | | -import java.util.List; |
34 | | -import java.util.Objects; |
35 | | -import java.util.Optional; |
36 | | -import java.util.Set; |
| 30 | +import java.util.*; |
37 | 31 | import java.util.function.Consumer; |
38 | 32 | import java.util.stream.Stream; |
39 | 33 |
|
@@ -1493,6 +1487,88 @@ void queryByWindowKeyset() { |
1493 | 1487 | assertThat(entities).extracting("idProp").contains(three.idProp); |
1494 | 1488 | } |
1495 | 1489 |
|
| 1490 | + @Test |
| 1491 | + void queryByWindowKeySetDesc() { |
| 1492 | + DummyEntity one = repository.save(createEntity("one")); |
| 1493 | + DummyEntity two = repository.save(createEntity("two")); |
| 1494 | + DummyEntity three = repository.save(createEntity("three")); |
| 1495 | + |
| 1496 | + WindowIterator<DummyEntity> iter = WindowIterator.of(position -> repository.findFirst2ByOrderByIdPropDesc(position)) |
| 1497 | + .startingAt(ScrollPosition.keyset()); |
| 1498 | + |
| 1499 | + List<DummyEntity> entities = new ArrayList<>(); |
| 1500 | + while (iter.hasNext()) |
| 1501 | + entities.add(iter.next()); |
| 1502 | + |
| 1503 | + assertThat(entities).extracting("idProp").contains(one.idProp); |
| 1504 | + } |
| 1505 | + |
| 1506 | + @Test |
| 1507 | + void queryByWindowKeySetDescBackward() { |
| 1508 | + DummyEntity one = repository.save(createEntity("one")); |
| 1509 | + DummyEntity two = repository.save(createEntity("two")); |
| 1510 | + DummyEntity three = repository.save(createEntity("three")); |
| 1511 | + DummyEntity four = repository.save(createEntity("four")); |
| 1512 | + DummyEntity five = repository.save(createEntity("five")); |
| 1513 | + |
| 1514 | + WindowIterator<DummyEntity> iter = WindowIterator.of(position -> repository.findFirst2ByOrderByIdPropDesc(position)) |
| 1515 | + .startingAt(ScrollPosition.backward(Map.of("idProp", 3))); |
| 1516 | + |
| 1517 | + List<DummyEntity> entities = new ArrayList<>(); |
| 1518 | + while (iter.hasNext()) |
| 1519 | + entities.add(iter.next()); |
| 1520 | + |
| 1521 | + assertThat(entities).extracting("idProp").contains(five.idProp); |
| 1522 | + } |
| 1523 | + |
| 1524 | + @Test |
| 1525 | + void queryByWindowKeySetAscBackward() { |
| 1526 | + DummyEntity one = repository.save(createEntity("one")); |
| 1527 | + DummyEntity two = repository.save(createEntity("two")); |
| 1528 | + DummyEntity three = repository.save(createEntity("three")); |
| 1529 | + DummyEntity four = repository.save(createEntity("four")); |
| 1530 | + DummyEntity five = repository.save(createEntity("five")); |
| 1531 | + |
| 1532 | + WindowIterator<DummyEntity> iter = WindowIterator.of(position -> repository.findFirst2ByOrderByIdPropAsc(position)) |
| 1533 | + .startingAt(ScrollPosition.backward(Map.of("idProp", 5))); |
| 1534 | + |
| 1535 | + List<DummyEntity> entities = new ArrayList<>(); |
| 1536 | + while (iter.hasNext()) |
| 1537 | + entities.add(iter.next()); |
| 1538 | + |
| 1539 | + assertThat(entities).extracting("idProp").contains(one.idProp); |
| 1540 | + } |
| 1541 | + |
| 1542 | + @Test |
| 1543 | + void queryByWindowKeySetEmptyDb() { |
| 1544 | + WindowIterator<DummyEntity> iter = WindowIterator.of(position -> repository.findFirst2ByOrderByIdPropAsc(position)) |
| 1545 | + .startingAt(ScrollPosition.backward(Map.of("idProp", 5))); |
| 1546 | + |
| 1547 | + List<DummyEntity> entities = new ArrayList<>(); |
| 1548 | + while (iter.hasNext()) |
| 1549 | + entities.add(iter.next()); |
| 1550 | + |
| 1551 | + assertThat(entities).isEmpty(); |
| 1552 | + } |
| 1553 | + |
| 1554 | + @Test |
| 1555 | + void queryByWindowKeySetTwoKeys() { |
| 1556 | + DummyEntity one = repository.save(createEntity("one", it -> it.setPointInTime(Instant.ofEpochSecond(1000)))); |
| 1557 | + DummyEntity two = repository.save(createEntity("two", it -> it.setPointInTime(Instant.ofEpochSecond(2000)))); |
| 1558 | + DummyEntity three = repository.save(createEntity("three", it -> it.setPointInTime(Instant.ofEpochSecond(3000)))); |
| 1559 | + DummyEntity four = repository.save(createEntity("four", it -> it.setPointInTime(Instant.ofEpochSecond(4000)))); |
| 1560 | + DummyEntity five = repository.save(createEntity("five", it -> it.setPointInTime(Instant.ofEpochSecond(5000)))); |
| 1561 | + |
| 1562 | + WindowIterator<DummyEntity> iter = WindowIterator.of(position -> repository.findFirst2ByOrderByIdPropDesc(position)) |
| 1563 | + .startingAt(ScrollPosition.forward(Map.of("idProp", 5, "pointInTime", Instant.now()))); |
| 1564 | + |
| 1565 | + List<DummyEntity> entities = new ArrayList<>(); |
| 1566 | + while (iter.hasNext()) |
| 1567 | + entities.add(iter.next()); |
| 1568 | + |
| 1569 | + assertThat(entities).extracting("idProp").contains(one.idProp); |
| 1570 | + } |
| 1571 | + |
1496 | 1572 | private Root createRoot(String namePrefix) { |
1497 | 1573 |
|
1498 | 1574 | return new Root(null, namePrefix, |
@@ -1640,6 +1716,8 @@ public interface DummyEntityRepository |
1640 | 1716 | List<DummyEntity> findByBytes(byte[] bytes); |
1641 | 1717 |
|
1642 | 1718 | Window<DummyEntity> findFirst2ByOrderByIdPropAsc(ScrollPosition position); |
| 1719 | + |
| 1720 | + Window<DummyEntity> findFirst2ByOrderByIdPropDesc(ScrollPosition position); |
1643 | 1721 | } |
1644 | 1722 |
|
1645 | 1723 | public interface RootRepository extends ListCrudRepository<Root, Long> { |
|
0 commit comments