Skip to content

Commit 61668e2

Browse files
committed
feat: provide forPage to allow processing objects in batches in user-land
1 parent 774469e commit 61668e2

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

src/main/java/io/weaviate/client6/v1/api/collections/pagination/AsyncPaginator.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/main/java/io/weaviate/client6/v1/api/collections/pagination/AsyncResultSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class AsyncResultSet<PropertiesT> implements Iterable<WeaviateObject<Prop
3131
this.currentPage = currentPage;
3232
}
3333

34-
public Iterable<WeaviateObject<PropertiesT, Object, QueryMetadata>> currentPage() {
34+
public List<WeaviateObject<PropertiesT, Object, QueryMetadata>> currentPage() {
3535
return currentPage;
3636
}
3737

0 commit comments

Comments
 (0)