@@ -40,26 +40,40 @@ public AsyncPaginator(Builder<PropertiesT> builder) {
4040 }
4141
4242 public CompletableFuture <Void > forEach (Consumer <WeaviateObject <PropertiesT , Object , QueryMetadata >> action ) {
43+ return resultSet
44+ .thenCompose (rs -> rs .isEmpty () ? rs .fetchNextPage () : CompletableFuture .completedFuture (rs ))
45+ .thenCompose (processEachAndAdvance (action ));
46+ }
47+
48+ public CompletableFuture <Void > forPage (Consumer <List <WeaviateObject <PropertiesT , Object , QueryMetadata >>> action ) {
4349 return resultSet
4450 .thenCompose (rs -> rs .isEmpty () ? rs .fetchNextPage () : CompletableFuture .completedFuture (rs ))
4551 .thenCompose (processPageAndAdvance (action ));
4652 }
4753
48- public Function <AsyncResultSet <PropertiesT >, CompletableFuture <Void >> processPageAndAdvance (
54+ public Function <AsyncResultSet <PropertiesT >, CompletableFuture <Void >> processEachAndAdvance (
4955 Consumer <WeaviateObject <PropertiesT , Object , QueryMetadata >> action ) {
56+ return processAndAdvanceFunc (rs -> rs .forEach (action ));
57+ }
58+
59+ public Function <AsyncResultSet <PropertiesT >, CompletableFuture <Void >> processPageAndAdvance (
60+ Consumer <List <WeaviateObject <PropertiesT , Object , QueryMetadata >>> action ) {
61+ return processAndAdvanceFunc (rs -> action .accept (rs .currentPage ()));
62+ }
63+
64+ public Function <AsyncResultSet <PropertiesT >, CompletableFuture <Void >> processAndAdvanceFunc (
65+ Consumer <AsyncResultSet <PropertiesT >> action ) {
5066 return rs -> {
5167 // Empty result set means there were no more objects to fetch.
5268 if (rs .isEmpty ()) {
5369 return CompletableFuture .completedFuture (null );
5470 }
5571
5672 // Apply provided callback for each method -- consume current page.
57- for (var object : rs ) {
58- action .accept (object );
59- }
73+ action .accept (rs );
6074
6175 // Advance iteration.
62- return rs .fetchNextPage ().thenCompose (processPageAndAdvance (action ));
76+ return rs .fetchNextPage ().thenCompose (processAndAdvanceFunc (action ));
6377 };
6478 }
6579
0 commit comments