-
Notifications
You must be signed in to change notification settings - Fork 64
Offset limit query support #234
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
Merged
christopheranderson
merged 3 commits into
master
from
mbhaskar/offset-limit-query-support
Jul 24, 2019
Merged
Changes from all commits
Commits
Show all changes
3 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
This file contains hidden or 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
67 changes: 67 additions & 0 deletions
67
...src/main/java/com/microsoft/azure/cosmosdb/rx/internal/query/OffsetContinuationToken.java
This file contains hidden or 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,67 @@ | ||
| package com.microsoft.azure.cosmosdb.rx.internal.query; | ||
|
|
||
| import com.microsoft.azure.cosmosdb.JsonSerializable; | ||
| import com.microsoft.azure.cosmosdb.rx.internal.Utils; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public final class OffsetContinuationToken extends JsonSerializable { | ||
| private static final String TOKEN_PROPERTY_NAME = "sourceToken"; | ||
| private static final String OFFSET_PROPERTY_NAME = "offset"; | ||
| private static final Logger logger = LoggerFactory.getLogger(CompositeContinuationToken.class); | ||
|
|
||
| public OffsetContinuationToken(int offset, String sourceToken) { | ||
|
|
||
| if (offset < 0) { | ||
| throw new IllegalArgumentException("offset should be non negative"); | ||
| } | ||
|
|
||
| this.setOffset(offset); | ||
| this.setSourceToken(sourceToken); | ||
| } | ||
|
|
||
| public OffsetContinuationToken(String serializedCompositeToken) { | ||
| super(serializedCompositeToken); | ||
| this.getOffset(); | ||
| this.getSourceToken(); | ||
| } | ||
|
|
||
| private void setSourceToken(String sourceToken) { | ||
| super.set(TOKEN_PROPERTY_NAME, sourceToken); | ||
| } | ||
|
|
||
| private void setOffset(int offset) { | ||
| super.set(OFFSET_PROPERTY_NAME, offset); | ||
| } | ||
|
|
||
| public String getSourceToken() { | ||
| return super.getString(TOKEN_PROPERTY_NAME); | ||
| } | ||
|
|
||
| public int getOffset() { | ||
| return super.getInt(OFFSET_PROPERTY_NAME); | ||
|
mbhaskar marked this conversation as resolved.
|
||
| } | ||
|
|
||
| public static boolean tryParse(String serializedOffsetContinuationToken, | ||
| Utils.ValueHolder<OffsetContinuationToken> outOffsetContinuationToken) { | ||
| if (StringUtils.isEmpty(serializedOffsetContinuationToken)) { | ||
| return false; | ||
| } | ||
|
|
||
| boolean parsed; | ||
| try { | ||
| outOffsetContinuationToken.v = new OffsetContinuationToken(serializedOffsetContinuationToken); | ||
| parsed = true; | ||
| } catch (Exception ex) { | ||
| logger.debug("Received exception {} when trying to parse: {}", | ||
| ex.getMessage(), | ||
| serializedOffsetContinuationToken); | ||
| parsed = false; | ||
| outOffsetContinuationToken.v = null; | ||
| } | ||
|
|
||
| return parsed; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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
84 changes: 84 additions & 0 deletions
84
...ava/com/microsoft/azure/cosmosdb/rx/internal/query/SkipDocumentQueryExecutionContext.java
This file contains hidden or 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,84 @@ | ||
| package com.microsoft.azure.cosmosdb.rx.internal.query; | ||
|
|
||
| import com.microsoft.azure.cosmosdb.BridgeInternal; | ||
| import com.microsoft.azure.cosmosdb.DocumentClientException; | ||
| import com.microsoft.azure.cosmosdb.FeedResponse; | ||
| import com.microsoft.azure.cosmosdb.Resource; | ||
| import com.microsoft.azure.cosmosdb.internal.HttpConstants; | ||
| import com.microsoft.azure.cosmosdb.rx.internal.Utils; | ||
| import rx.Observable; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public final class SkipDocumentQueryExecutionContext<T extends Resource> implements IDocumentQueryExecutionComponent<T> { | ||
|
|
||
| private final IDocumentQueryExecutionComponent<T> component; | ||
| private int skipCount; | ||
|
|
||
| SkipDocumentQueryExecutionContext(IDocumentQueryExecutionComponent<T> component, int skipCount) { | ||
|
mbhaskar marked this conversation as resolved.
|
||
| if (component == null){ | ||
| throw new IllegalArgumentException("documentQueryExecutionComponent cannot be null"); | ||
| } | ||
| this.component = component; | ||
| this.skipCount = skipCount; | ||
| } | ||
|
|
||
| public static <T extends Resource> Observable<IDocumentQueryExecutionComponent<T>> createAsync( | ||
| Function<String, Observable<IDocumentQueryExecutionComponent<T>>> createSourceComponentFunction, | ||
| int skipCount, | ||
| String continuationToken) { | ||
| OffsetContinuationToken offsetContinuationToken; | ||
| Utils.ValueHolder<OffsetContinuationToken> outOffsetContinuationToken = new Utils.ValueHolder<>(); | ||
|
|
||
| if (continuationToken != null) { | ||
| if (!OffsetContinuationToken.tryParse(continuationToken, outOffsetContinuationToken)) { | ||
| String message = String.format("Invalid JSON in continuation token %s for Skip~Context", | ||
| continuationToken); | ||
| DocumentClientException dce = new DocumentClientException(HttpConstants.StatusCodes.BADREQUEST, | ||
| message); | ||
| return Observable.error(dce); | ||
| } | ||
|
|
||
| offsetContinuationToken = outOffsetContinuationToken.v; | ||
| } else { | ||
| offsetContinuationToken = new OffsetContinuationToken(skipCount, null); | ||
| } | ||
|
|
||
| return createSourceComponentFunction.apply(offsetContinuationToken.getSourceToken()) | ||
| .map(component -> new SkipDocumentQueryExecutionContext<>(component, offsetContinuationToken.getOffset())); | ||
| } | ||
|
|
||
| @Override | ||
| public Observable<FeedResponse<T>> drainAsync(int maxPageSize) { | ||
|
|
||
| return this.component.drainAsync(maxPageSize).map(tFeedResponse -> { | ||
|
|
||
| List<T> documentsAfterSkip = | ||
| tFeedResponse.getResults().stream().skip(this.skipCount).collect(Collectors.toList()); | ||
|
|
||
| int numberOfDocumentsSkipped = tFeedResponse.getResults().size() - documentsAfterSkip.size(); | ||
| this.skipCount -= numberOfDocumentsSkipped; | ||
|
|
||
| Map<String, String> headers = new HashMap<>(tFeedResponse.getResponseHeaders()); | ||
| if (this.skipCount >= 0) { | ||
| // Add Offset Continuation Token | ||
| String sourceContinuationToken = tFeedResponse.getResponseContinuation(); | ||
| OffsetContinuationToken offsetContinuationToken = new OffsetContinuationToken(this.skipCount, | ||
| sourceContinuationToken); | ||
| headers.put(HttpConstants.HttpHeaders.CONTINUATION, offsetContinuationToken.toJson()); | ||
| } | ||
|
|
||
| return BridgeInternal.createFeedResponseWithQueryMetrics(documentsAfterSkip, headers, | ||
| tFeedResponse.getQueryMetrics()); | ||
| }); | ||
| } | ||
|
|
||
| IDocumentQueryExecutionComponent<T> getComponent() { | ||
| return this.component; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.