-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[draft] a simple PagedIterable / PagedResponse #43484
Draft
weidongxu-microsoft
wants to merge
2
commits into
main
Choose a base branch
from
clientcore-paged
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+318
−0
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
206 changes: 206 additions & 0 deletions
206
sdk/clientcore/core/src/main/java/io/clientcore/core/http/models/PagedIterable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package io.clientcore.core.http.models; | ||
|
||
import io.clientcore.core.util.ClientLogger; | ||
|
||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
import java.util.Queue; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Stream; | ||
import java.util.stream.StreamSupport; | ||
|
||
/** | ||
* This class provides utility to iterate over {@link PagedResponse} using {@link Stream} and {@link Iterable} | ||
* interfaces. | ||
* | ||
* @param <T> The type of items in the page. | ||
*/ | ||
public final class PagedIterable<T> implements Iterable<T> { | ||
|
||
private final Function<String, PagedResponse<T>> pageRetriever; | ||
|
||
/** | ||
* Creates an instance of {@link PagedIterable} that consists of only a single page. This constructor takes a {@code | ||
* Supplier} that return the single page of {@code T}. | ||
* | ||
* @param firstPageRetriever Supplier that retrieves the first page. | ||
*/ | ||
public PagedIterable(Supplier<PagedResponse<T>> firstPageRetriever) { | ||
this(firstPageRetriever, null); | ||
} | ||
|
||
/** | ||
* Creates an instance of {@link PagedIterable}. The constructor takes a {@code Supplier} and {@code Function}. The | ||
* {@code Supplier} returns the first page of {@code T}, the {@code Function} retrieves subsequent pages of {@code | ||
* T}. | ||
* | ||
* @param firstPageRetriever Supplier that retrieves the first page. | ||
* @param nextPageRetriever Function that retrieves the next page given a continuation token | ||
*/ | ||
public PagedIterable(Supplier<PagedResponse<T>> firstPageRetriever, | ||
Function<String, PagedResponse<T>> nextPageRetriever) { | ||
this.pageRetriever = (continuationToken) -> (continuationToken == null) | ||
? firstPageRetriever.get() | ||
: nextPageRetriever.apply(continuationToken); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public Iterator<T> iterator() { | ||
return iterableByItemInternal().iterator(); | ||
} | ||
|
||
/** | ||
* Retrieve the {@link Iterable}, one page at a time. It will provide same {@link Iterable} of T values from | ||
* starting if called multiple times. | ||
* | ||
* @return {@link Iterable} of a pages | ||
*/ | ||
public Iterable<PagedResponse<T>> iterableByPage() { | ||
return iterableByPageInternal(); | ||
} | ||
|
||
/** | ||
* Utility function to provide {@link Stream} of value {@code T}. | ||
* | ||
* @return {@link Stream} of value {@code T}. | ||
*/ | ||
public Stream<T> stream() { | ||
return StreamSupport.stream(iterableByItemInternal().spliterator(), false); | ||
} | ||
|
||
/** | ||
* Retrieve the {@link Stream}, one page at a time. It will provide same {@link Stream} of T values from starting if | ||
* called multiple times. | ||
* | ||
* @return {@link Stream} of a pages | ||
*/ | ||
public Stream<PagedResponse<T>> streamByPage() { | ||
return StreamSupport.stream(iterableByPage().spliterator(), false); | ||
} | ||
|
||
private Iterable<T> iterableByItemInternal() { | ||
return () -> new PagedByIterator<>(pageRetriever) { | ||
|
||
private final Queue<Iterator<T>> pages = new ConcurrentLinkedQueue<>(); | ||
private volatile Iterator<T> currentPage; | ||
|
||
@Override | ||
boolean needToRequestPage() { | ||
return (currentPage == null || !currentPage.hasNext()) && pages.peek() == null; | ||
} | ||
|
||
@Override | ||
boolean isNextAvailable() { | ||
return (currentPage != null && currentPage.hasNext()) || pages.peek() != null; | ||
} | ||
|
||
@Override | ||
T getNext() { | ||
if ((currentPage == null || !currentPage.hasNext()) && pages.peek() != null) { | ||
currentPage = pages.poll(); | ||
} | ||
|
||
return currentPage.next(); | ||
} | ||
|
||
@Override | ||
void addPage(PagedResponse<T> page) { | ||
Iterator<T> pageValues = page.getValue().iterator(); | ||
if (pageValues.hasNext()) { | ||
this.pages.add(pageValues); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
private Iterable<PagedResponse<T>> iterableByPageInternal() { | ||
return () -> new PagedByIterator<T, PagedResponse<T>>(pageRetriever) { | ||
|
||
private final Queue<PagedResponse<T>> pages = new ConcurrentLinkedQueue<>(); | ||
|
||
@Override | ||
boolean needToRequestPage() { | ||
return pages.peek() == null; | ||
} | ||
|
||
@Override | ||
boolean isNextAvailable() { | ||
return pages.peek() != null; | ||
} | ||
|
||
@Override | ||
PagedResponse<T> getNext() { | ||
return pages.poll(); | ||
} | ||
|
||
@Override | ||
void addPage(PagedResponse<T> page) { | ||
this.pages.add(page); | ||
} | ||
}; | ||
} | ||
|
||
private abstract static class PagedByIterator<T, E> implements Iterator<E> { | ||
private static final ClientLogger LOGGER = new ClientLogger(PagedByIterator.class); | ||
|
||
private final Function<String, PagedResponse<T>> pageRetriever; | ||
private volatile String continuationToken; | ||
private volatile boolean done; | ||
|
||
PagedByIterator(Function<String, PagedResponse<T>> pageRetriever) { | ||
this.pageRetriever = pageRetriever; | ||
} | ||
|
||
@Override | ||
public E next() { | ||
if (!hasNext()) { | ||
throw LOGGER.logThrowableAsError(new NoSuchElementException("Iterator contains no more elements.")); | ||
} | ||
|
||
return getNext(); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
// Request next pages in a loop in case we are returned empty pages for the by item implementation. | ||
while (!done && needToRequestPage()) { | ||
requestPage(); | ||
} | ||
|
||
return isNextAvailable(); | ||
} | ||
|
||
abstract boolean needToRequestPage(); | ||
|
||
abstract boolean isNextAvailable(); | ||
|
||
abstract E getNext(); | ||
|
||
synchronized void requestPage() { | ||
AtomicBoolean receivedPages = new AtomicBoolean(false); | ||
PagedResponse<T> page = pageRetriever.apply(continuationToken); | ||
if (page != null) { | ||
receivePage(receivedPages, page); | ||
} | ||
} | ||
|
||
abstract void addPage(PagedResponse<T> page); | ||
|
||
private void receivePage(AtomicBoolean receivedPages, PagedResponse<T> page) { | ||
receivedPages.set(true); | ||
addPage(page); | ||
|
||
continuationToken = page.getNextLink(); | ||
this.done = continuationToken == null || continuationToken.isEmpty(); | ||
} | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
sdk/clientcore/core/src/main/java/io/clientcore/core/http/models/PagedResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package io.clientcore.core.http.models; | ||
|
||
import io.clientcore.core.util.binarydata.BinaryData; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
/** | ||
* Response of a REST API that returns page. | ||
* | ||
* @see Response | ||
* | ||
* @param <T> The type of items in the page. | ||
*/ | ||
public final class PagedResponse<T> implements Response<List<T>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For azure-core, the In such case, we can even generate the implementation class in emitter (so clientcore does not need to have the |
||
|
||
private final HttpRequest request; | ||
private final int statusCode; | ||
private final HttpHeaders headers; | ||
private final List<T> items; | ||
private final String nextLink; | ||
private final BinaryData body; | ||
|
||
/** | ||
* Creates a new instance of the PagedResponse type. | ||
* | ||
* @param request The HttpRequest that was sent to the service whose response resulted in this response. | ||
* @param statusCode The status code from the response. | ||
* @param headers The headers from the response. | ||
* @param body The body from the response. | ||
* @param items The items returned from the service within the response. | ||
* @param nextLink The next page reference returned from the service, to enable future requests to pick up | ||
* from the same place in the paged iteration. | ||
*/ | ||
public PagedResponse(HttpRequest request, int statusCode, HttpHeaders headers, BinaryData body, List<T> items, | ||
String nextLink) { | ||
this.request = request; | ||
this.statusCode = statusCode; | ||
this.headers = headers; | ||
this.body = body; | ||
this.items = items; | ||
this.nextLink = nextLink; | ||
} | ||
|
||
/** | ||
* Gets the reference to the next page. | ||
* | ||
* @return The next page reference or null if there isn't a next page. | ||
*/ | ||
public String getNextLink() { | ||
return nextLink; | ||
} | ||
|
||
// TODO | ||
// public String getContinuationToken() {} | ||
// public String getPreviousLink() {} | ||
// public String getFirstLink() {} | ||
// public String getLastLink() {} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public int getStatusCode() { | ||
return statusCode; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public HttpHeaders getHeaders() { | ||
return headers; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public HttpRequest getRequest() { | ||
return request; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public List<T> getValue() { | ||
return items; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public BinaryData getBody() { | ||
return body; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void close() throws IOException { | ||
if (body != null) { | ||
body.close(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
receivePage
(and related var) would be different, if server support bothcontinuationToken
andnextLink
.SDK likely would only provide one
pageRetriever
with either of the approach. It may also provide a e.g.continuationTokenRetriever
to specify whether to callgetNextLink
orgetContinuationToken
.