forked from Azure/azure-sdk-for-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Porting offset limit support from V2
Adding test to drain all the documents (Azure#257)
- Loading branch information
Showing
7 changed files
with
443 additions
and
10 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
...re-cosmos/src/main/java/com/azure/data/cosmos/internal/query/OffsetContinuationToken.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,71 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.data.cosmos.internal.query; | ||
|
||
import com.azure.data.cosmos.BridgeInternal; | ||
import com.azure.data.cosmos.JsonSerializable; | ||
import com.azure.data.cosmos.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(); | ||
} | ||
|
||
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; | ||
} | ||
|
||
public String getSourceToken() { | ||
return super.getString(TOKEN_PROPERTY_NAME); | ||
} | ||
|
||
private void setSourceToken(String sourceToken) { | ||
BridgeInternal.setProperty(this, TOKEN_PROPERTY_NAME, sourceToken); | ||
} | ||
|
||
public int getOffset() { | ||
return super.getInt(OFFSET_PROPERTY_NAME); | ||
} | ||
|
||
private void setOffset(int offset) { | ||
BridgeInternal.setProperty(this, OFFSET_PROPERTY_NAME, offset); | ||
} | ||
} | ||
|
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
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
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
87 changes: 87 additions & 0 deletions
87
...src/main/java/com/azure/data/cosmos/internal/query/SkipDocumentQueryExecutionContext.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,87 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.data.cosmos.internal.query; | ||
|
||
import com.azure.data.cosmos.BridgeInternal; | ||
import com.azure.data.cosmos.CosmosClientException; | ||
import com.azure.data.cosmos.FeedResponse; | ||
import com.azure.data.cosmos.Resource; | ||
import com.azure.data.cosmos.internal.HttpConstants; | ||
import com.azure.data.cosmos.internal.Utils; | ||
import reactor.core.publisher.Flux; | ||
|
||
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) { | ||
if (component == null) { | ||
throw new IllegalArgumentException("documentQueryExecutionComponent cannot be null"); | ||
} | ||
this.component = component; | ||
this.skipCount = skipCount; | ||
} | ||
|
||
public static <T extends Resource> Flux<IDocumentQueryExecutionComponent<T>> createAsync( | ||
Function<String, Flux<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); | ||
CosmosClientException dce = | ||
BridgeInternal.createCosmosClientException(HttpConstants.StatusCodes.BADREQUEST, | ||
message); | ||
return Flux.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 Flux<FeedResponse<T>> drainAsync(int maxPageSize) { | ||
|
||
return this.component.drainAsync(maxPageSize).map(tFeedResponse -> { | ||
|
||
List<T> documentsAfterSkip = | ||
tFeedResponse.results().stream().skip(this.skipCount).collect(Collectors.toList()); | ||
|
||
int numberOfDocumentsSkipped = tFeedResponse.results().size() - documentsAfterSkip.size(); | ||
this.skipCount -= numberOfDocumentsSkipped; | ||
|
||
Map<String, String> headers = new HashMap<>(tFeedResponse.responseHeaders()); | ||
if (this.skipCount >= 0) { | ||
// Add Offset Continuation Token | ||
String sourceContinuationToken = tFeedResponse.continuationToken(); | ||
OffsetContinuationToken offsetContinuationToken = new OffsetContinuationToken(this.skipCount, | ||
sourceContinuationToken); | ||
headers.put(HttpConstants.HttpHeaders.CONTINUATION, offsetContinuationToken.toJson()); | ||
} | ||
|
||
return BridgeInternal.createFeedResponseWithQueryMetrics(documentsAfterSkip, headers, | ||
BridgeInternal.queryMetricsFromFeedResponse(tFeedResponse)); | ||
}); | ||
} | ||
|
||
IDocumentQueryExecutionComponent<T> getComponent() { | ||
return this.component; | ||
} | ||
} |
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
Oops, something went wrong.