Skip to content

Conversation

@bsbodden
Copy link
Collaborator

Implements the cursor API as supported by RediSearch using the Spring Framework Slice construct.
The test below creates a SearchStream and gets the initial "Slice" by usingloadAll() and toList. The overloaded toList takes a Spring PageRequest and the target class for the Slice.

The aggregation is lazily executed when the underlying slice is interrogated in any form that would require executing it. The case below will be in the while-loop hasContent() call. Subsequent slices can be obtained the "Spring Way" by passing the page's nextPageable:

@Test void testManagedCursorSession() {
    int pageSize = 45;
    SearchStream<Game> searchStream = entityStream.of(Game.class);

    Slice<Game> page = searchStream //
        .loadAll()
        .limit(300)
        .toList(PageRequest.ofSize(pageSize), Game.class);

    // loop through the slices using the SearchStream.getSlice method passing the next page request
    // obtained from the current page
    Map<Integer, Integer> pageCounts = new HashMap<>();
    while (page.hasContent()) {
      List<Game> contents = page.getContent();
      // collect the page counts
      pageCounts.put(page.getNumber(), contents.size());
      page = page.hasNext() ? searchStream.getSlice(page.nextPageable()) : Page.empty();
    }

    // assert the page counts
    assertAll("page counts",
        () -> assertEquals(pageSize, pageCounts.get(0)),
        () -> assertEquals(pageSize, pageCounts.get(1)),
        () -> assertEquals(pageSize, pageCounts.get(2)),
        () -> assertEquals(pageSize, pageCounts.get(3)),
        () -> assertEquals(pageSize, pageCounts.get(4)),
        () -> assertEquals(pageSize, pageCounts.get(5)),
        () -> assertEquals(30, pageCounts.get(6))
    );
  }

@bsbodden bsbodden requested review from sazzad16 and slorello89 May 10, 2023 22:33
@bsbodden bsbodden removed the request for review from slorello89 May 11, 2023 13:59
@bsbodden bsbodden merged commit 5ca6565 into redis:main May 11, 2023
@bsbodden bsbodden deleted the bsb/cursors-feature branch June 5, 2024 22:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants