public override PipelineContinuationTokenTestsOutput ExecuteTest( PipelineContinuationTokenTestsInput input) { if (!CosmosElement.TryParse(input.ContinuationToken, out CosmosElement cosmosElementContinuationToken)) { return(new PipelineContinuationTokenTestsOutputNegative("Failed to parse token.")); } if (!PipelineContinuationToken.TryCreateFromCosmosElement( cosmosElementContinuationToken, out PipelineContinuationToken pipelineContinuationToken)) { return(new PipelineContinuationTokenTestsOutputNegative("Failed to parse token.")); } if (!PipelineContinuationToken.TryConvertToLatest( pipelineContinuationToken, out PipelineContinuationTokenV1_1 latestPipelineContinuationToken)) { return(new PipelineContinuationTokenTestsOutputNegative("Failed to convert to latest")); } return(new PipelineContinuationTokenTestsOutputPositive( pipelineContinuationToken, latestPipelineContinuationToken)); }
private static TryCatch <IAggregator> TryCreate(bool isMinAggregation, string continuationToken) { CosmosElement globalMinMax; if (continuationToken != null) { if (continuationToken == MinMaxAggregator.MaxValueContinuationToken) { globalMinMax = ItemComparer.MaxValue; } else if (continuationToken == MinMaxAggregator.MinValueContinuationToken) { globalMinMax = ItemComparer.MinValue; } else if (continuationToken == MinMaxAggregator.UndefinedContinuationToken) { globalMinMax = MinMaxAggregator.Undefined; } else { if (!CosmosElement.TryParse(continuationToken, out globalMinMax)) { return(TryCatch <IAggregator> .FromException( new MalformedContinuationTokenException($"Malformed continuation token: {continuationToken}"))); } } } else { globalMinMax = isMinAggregation ? (CosmosElement)ItemComparer.MaxValue : (CosmosElement)ItemComparer.MinValue; } return(TryCatch <IAggregator> .FromResult( new MinMaxAggregator(isMinAggregation : isMinAggregation, globalMinMax : globalMinMax))); }
public static bool TryParse(string value, out GroupByContinuationToken groupByContinuationToken) { if (!CosmosElement.TryParse <CosmosObject>(value, out CosmosObject groupByContinuationTokenObject)) { groupByContinuationToken = default; return(false); } if (!groupByContinuationTokenObject.TryGetValue( nameof(GroupByContinuationToken.GroupingTableContinuationToken), out CosmosString groupingTableContinuationToken)) { groupByContinuationToken = default; return(false); } if (!groupByContinuationTokenObject.TryGetValue( nameof(GroupByContinuationToken.SourceContinuationToken), out CosmosString sourceContinuationToken)) { groupByContinuationToken = default; return(false); } groupByContinuationToken = new GroupByContinuationToken( groupingTableContinuationToken.Value, sourceContinuationToken.Value); return(true); }
public static bool TryParse( string serializedContinuationToken, out AggregateContinuationToken aggregateContinuationToken) { if (serializedContinuationToken == null) { throw new ArgumentNullException(nameof(serializedContinuationToken)); } if (!CosmosElement.TryParse <CosmosObject>(serializedContinuationToken, out CosmosObject rawAggregateContinuationToken)) { aggregateContinuationToken = default; return(false); } CosmosElement rawSingleGroupAggregatorContinuationToken = rawAggregateContinuationToken[AggregateContinuationToken.SingleGroupAggregatorContinuationTokenName]; if (!(rawSingleGroupAggregatorContinuationToken is CosmosString singleGroupAggregatorContinuationToken)) { aggregateContinuationToken = default; return(false); } CosmosElement rawSourceContinuationToken = rawAggregateContinuationToken[AggregateContinuationToken.SourceContinuationTokenName]; if (!(rawSourceContinuationToken is CosmosString sourceContinuationToken)) { aggregateContinuationToken = default; return(false); } aggregateContinuationToken = new AggregateContinuationToken(rawAggregateContinuationToken); return(true); }
public static TryCatch <SingleGroupAggregator> TryCreate( IReadOnlyDictionary <string, AggregateOperator?> aggregateAliasToAggregateType, IReadOnlyList <string> orderedAliases, string continuationToken) { CosmosObject aliasToContinuationToken; if (continuationToken != null) { if (!CosmosElement.TryParse(continuationToken, out aliasToContinuationToken)) { return(TryCatch <SingleGroupAggregator> .FromException( new MalformedContinuationTokenException( $"{nameof(SelectListAggregateValues)} continuation token is malformed: {continuationToken}."))); } } else { aliasToContinuationToken = null; } Dictionary <string, AggregateValue> groupingTable = new Dictionary <string, AggregateValue>(); foreach (KeyValuePair <string, AggregateOperator?> aliasToAggregate in aggregateAliasToAggregateType) { string alias = aliasToAggregate.Key; AggregateOperator?aggregateOperator = aliasToAggregate.Value; string aliasContinuationToken; if (aliasToContinuationToken != null) { if (!(aliasToContinuationToken[alias] is CosmosString parsedAliasContinuationToken)) { return(TryCatch <SingleGroupAggregator> .FromException( new MalformedContinuationTokenException( $"{nameof(SelectListAggregateValues)} continuation token is malformed: {continuationToken}."))); } aliasContinuationToken = parsedAliasContinuationToken.Value; } else { aliasContinuationToken = null; } TryCatch <AggregateValue> tryCreateAggregateValue = AggregateValue.TryCreate( aggregateOperator, aliasContinuationToken); if (tryCreateAggregateValue.Succeeded) { groupingTable[alias] = tryCreateAggregateValue.Result; } else { return(TryCatch <SingleGroupAggregator> .FromException(tryCreateAggregateValue.Exception)); } } return(TryCatch <SingleGroupAggregator> .FromResult(new SelectListAggregateValues(groupingTable, orderedAliases))); }
public static TryCatch <GroupingTable> TryCreateFromContinuationToken( IReadOnlyDictionary <string, AggregateOperator?> groupByAliasToAggregateType, IReadOnlyList <string> orderedAliases, bool hasSelectValue, string groupingTableContinuationToken) { GroupingTable groupingTable = new GroupingTable( groupByAliasToAggregateType, orderedAliases, hasSelectValue); if (groupingTableContinuationToken != null) { if (!CosmosElement.TryParse( groupingTableContinuationToken, out CosmosObject parsedGroupingTableContinuations)) { return(TryCatch <GroupingTable> .FromException( new MalformedContinuationTokenException($"Invalid GroupingTableContinuationToken"))); } foreach (KeyValuePair <string, CosmosElement> kvp in parsedGroupingTableContinuations) { string key = kvp.Key; CosmosElement value = kvp.Value; if (!UInt128.TryParse(key, out UInt128 groupByKey)) { return(TryCatch <GroupingTable> .FromException( new MalformedContinuationTokenException($"Invalid GroupingTableContinuationToken"))); } if (!(value is CosmosString singleGroupAggregatorContinuationToken)) { return(TryCatch <GroupingTable> .FromException( new MalformedContinuationTokenException($"Invalid GroupingTableContinuationToken"))); } TryCatch <SingleGroupAggregator> tryCreateSingleGroupAggregator = SingleGroupAggregator.TryCreate( EmptyAggregateOperators, groupByAliasToAggregateType, orderedAliases, hasSelectValue, singleGroupAggregatorContinuationToken.Value); if (tryCreateSingleGroupAggregator.Succeeded) { groupingTable.table[groupByKey] = tryCreateSingleGroupAggregator.Result; } else { return(TryCatch <GroupingTable> .FromException(tryCreateSingleGroupAggregator.Exception)); } } } return(TryCatch <GroupingTable> .FromResult(groupingTable)); }
public static async Task <TryCatch <IDocumentQueryExecutionComponent> > TryCreateTopDocumentQueryExecutionComponentAsync( int topCount, CosmosElement requestContinuationToken, Func <CosmosElement, Task <TryCatch <IDocumentQueryExecutionComponent> > > tryCreateSourceAsync) { if (topCount < 0) { throw new ArgumentException($"{nameof(topCount)}: {topCount} must be a non negative number."); } if (tryCreateSourceAsync == null) { throw new ArgumentNullException(nameof(tryCreateSourceAsync)); } TopContinuationToken topContinuationToken; if (requestContinuationToken != null) { if (!TopContinuationToken.TryParse(requestContinuationToken.ToString(), out topContinuationToken)) { return(TryCatch <IDocumentQueryExecutionComponent> .FromException( new MalformedContinuationTokenException($"Malformed {nameof(LimitContinuationToken)}: {requestContinuationToken}."))); } } else { topContinuationToken = new TopContinuationToken(topCount, null); } if (topContinuationToken.Top > topCount) { return(TryCatch <IDocumentQueryExecutionComponent> .FromException( new MalformedContinuationTokenException($"{nameof(TopContinuationToken.Top)} in {nameof(TopContinuationToken)}: {requestContinuationToken}: {topContinuationToken.Top} can not be greater than the top count in the query: {topCount}."))); } CosmosElement sourceToken; if (topContinuationToken.SourceToken != null) { if (!CosmosElement.TryParse(topContinuationToken.SourceToken, out sourceToken)) { return(TryCatch <IDocumentQueryExecutionComponent> .FromException( new MalformedContinuationTokenException($"{nameof(TopContinuationToken.SourceToken)} in {nameof(TopContinuationToken)}: {requestContinuationToken}: {topContinuationToken.SourceToken} was malformed."))); } } else { sourceToken = null; } return((await tryCreateSourceAsync(sourceToken)) .Try <IDocumentQueryExecutionComponent>((source) => new ClientTakeDocumentQueryExecutionComponent( source, topContinuationToken.Top, TakeEnum.Top))); }
public static async Task <TryCatch <IDocumentQueryExecutionComponent> > TryCreateAsync( int offsetCount, CosmosElement continuationToken, Func <CosmosElement, Task <TryCatch <IDocumentQueryExecutionComponent> > > tryCreateSourceAsync) { if (tryCreateSourceAsync == null) { throw new ArgumentNullException(nameof(tryCreateSourceAsync)); } OffsetContinuationToken offsetContinuationToken; if (continuationToken != null) { if (!OffsetContinuationToken.TryParse(continuationToken.ToString(), out offsetContinuationToken)) { return(TryCatch <IDocumentQueryExecutionComponent> .FromException( new MalformedContinuationTokenException($"Invalid {nameof(SkipDocumentQueryExecutionComponent)}: {continuationToken}."))); } } else { offsetContinuationToken = new OffsetContinuationToken(offsetCount, null); } if (offsetContinuationToken.Offset > offsetCount) { return(TryCatch <IDocumentQueryExecutionComponent> .FromException( new MalformedContinuationTokenException("offset count in continuation token can not be greater than the offsetcount in the query."))); } CosmosElement sourceToken; if (offsetContinuationToken.SourceToken != null) { if (!CosmosElement.TryParse(offsetContinuationToken.SourceToken, out sourceToken)) { return(TryCatch <IDocumentQueryExecutionComponent> .FromException( new MalformedContinuationTokenException("source token is not valid."))); } } else { sourceToken = null; } return((await tryCreateSourceAsync(sourceToken)) .Try <IDocumentQueryExecutionComponent>((source) => new ClientSkipDocumentQueryExecutionComponent( source, offsetContinuationToken.Offset))); }
public static SelectListAggregateValues Create( CosmosQueryClient cosmosQueryClient, IReadOnlyDictionary <string, AggregateOperator?> aggregateAliasToAggregateType, IReadOnlyList <string> orderedAliases, string continuationToken) { CosmosObject aliasToContinuationToken; if (continuationToken != null) { if (!CosmosElement.TryParse(continuationToken, out aliasToContinuationToken)) { throw cosmosQueryClient.CreateBadRequestException( $"{nameof(SelectListAggregateValues)} continuation token is malformed: {continuationToken}."); } } else { aliasToContinuationToken = null; } Dictionary <string, AggregateValue> groupingTable = new Dictionary <string, AggregateValue>(); foreach (KeyValuePair <string, AggregateOperator?> aliasToAggregate in aggregateAliasToAggregateType) { string alias = aliasToAggregate.Key; AggregateOperator?aggregateOperator = aliasToAggregate.Value; string aliasContinuationToken; if (aliasToContinuationToken != null) { if (!(aliasToContinuationToken[alias] is CosmosString parsedAliasContinuationToken)) { throw cosmosQueryClient.CreateBadRequestException( $"{nameof(SelectListAggregateValues)} continuation token is malformed: {continuationToken}."); } aliasContinuationToken = parsedAliasContinuationToken.Value; } else { aliasContinuationToken = null; } groupingTable[alias] = AggregateValue.Create(aggregateOperator, aliasContinuationToken); } return(new SelectListAggregateValues(groupingTable, orderedAliases)); }
public static bool TryParse(string serializedAverageInfo, out AverageInfo averageInfo) { if (serializedAverageInfo == null) { throw new ArgumentNullException(nameof(serializedAverageInfo)); } if (!CosmosElement.TryParse(serializedAverageInfo, out CosmosElement cosmosElementAverageInfo)) { averageInfo = default(AverageInfo); return(false); } averageInfo = AverageInfo.Create(cosmosElementAverageInfo); return(true); }
public static GroupingTable CreateFromContinuationToken(CosmosQueryClient cosmosQueryClient, IReadOnlyDictionary <string, AggregateOperator?> groupByAliasToAggregateType, IReadOnlyList <string> orderedAliases, bool hasSelectValue, string groupingTableContinuationToken) { GroupingTable groupingTable = new GroupingTable( cosmosQueryClient, groupByAliasToAggregateType, orderedAliases, hasSelectValue); if (groupingTableContinuationToken != null) { if (!CosmosElement.TryParse( groupingTableContinuationToken, out CosmosObject parsedGroupingTableContinuations)) { throw cosmosQueryClient.CreateBadRequestException($"Invalid GroupingTableContinuationToken"); } foreach (KeyValuePair <string, CosmosElement> kvp in parsedGroupingTableContinuations) { string key = kvp.Key; CosmosElement value = kvp.Value; UInt128 groupByKey = UInt128.Parse(key); if (!(value is CosmosString singleGroupAggregatorContinuationToken)) { throw cosmosQueryClient.CreateBadRequestException($"Invalid GroupingTableContinuationToken"); } SingleGroupAggregator singleGroupAggregator = SingleGroupAggregator.Create( cosmosQueryClient, EmptyAggregateOperators, groupByAliasToAggregateType, orderedAliases, hasSelectValue, singleGroupAggregatorContinuationToken.Value); groupingTable.table[groupByKey] = singleGroupAggregator; } } return(groupingTable); }
public static ScalarAggregateValue Create(string continuationToken) { CosmosElement initialValue; bool initialized; if (continuationToken != null) { if (!CosmosElement.TryParse(continuationToken, out initialValue)) { throw new ArgumentException($"Invalid {nameof(ScalarAggregateValue)}: {continuationToken}"); } initialized = true; } else { initialValue = null; initialized = false; } return(new ScalarAggregateValue(initialValue, initialized)); }
public static TryCatch <AggregateValue> TryCreate(string continuationToken) { CosmosElement value; bool initialized; if (continuationToken != null) { if (!CosmosElement.TryParse <CosmosObject>( continuationToken, out CosmosObject rawContinuationToken)) { return(TryCatch <AggregateValue> .FromException( new MalformedContinuationTokenException($"Invalid {nameof(ScalarAggregateValue)}: {continuationToken}"))); } if (!rawContinuationToken.TryGetValue <CosmosBoolean>( nameof(ScalarAggregateValue.initialized), out CosmosBoolean rawInitialized)) { return(TryCatch <AggregateValue> .FromException( new MalformedContinuationTokenException($"Invalid {nameof(ScalarAggregateValue)}: {continuationToken}"))); } if (!rawContinuationToken.TryGetValue(nameof(ScalarAggregateValue.value), out value)) { value = null; } initialized = rawInitialized.Value; } else { value = null; initialized = false; } return(TryCatch <AggregateValue> .FromResult(new ScalarAggregateValue(value, initialized))); }
public static ScalarAggregateValue Create(string continuationToken) { CosmosElement value; bool initialized; if (continuationToken != null) { if (!CosmosElement.TryParse <CosmosObject>( continuationToken, out CosmosObject rawContinuationToken)) { throw new ArgumentException($"Invalid {nameof(ScalarAggregateValue)}: {continuationToken}"); } if (!rawContinuationToken.TryGetValue <CosmosBoolean>( nameof(ScalarAggregateValue.initialized), out CosmosBoolean rawInitialized)) { throw new ArgumentException($"Invalid {nameof(ScalarAggregateValue)}: {continuationToken}"); } if (!rawContinuationToken.TryGetValue(nameof(ScalarAggregateValue.value), out value)) { value = null; } initialized = rawInitialized.Value; } else { value = null; initialized = false; } return(new ScalarAggregateValue(value, initialized)); }
public static bool TryParse( string rawContinuationToken, out PipelineContinuationToken pipelineContinuationToken) { if (rawContinuationToken == null) { throw new ArgumentNullException(nameof(rawContinuationToken)); } if (!CosmosElement.TryParse <CosmosObject>( rawContinuationToken, out CosmosObject parsedContinuationToken)) { // Failed to parse so we need to assume it's a V0 token if (!PipelineContinuationTokenV0.TryParse( rawContinuationToken, out PipelineContinuationTokenV0 pipelineContinuationTokenV0)) { pipelineContinuationToken = default; return(false); } pipelineContinuationToken = pipelineContinuationTokenV0; return(true); } if (!PipelineContinuationToken.TryParseVersion( parsedContinuationToken, out Version version)) { pipelineContinuationToken = default; return(false); } if (version == PipelineContinuationTokenV0.VersionNumber) { if (!PipelineContinuationTokenV0.TryParse( rawContinuationToken, out PipelineContinuationTokenV0 pipelineContinuationTokenV0)) { pipelineContinuationToken = default; return(false); } pipelineContinuationToken = pipelineContinuationTokenV0; } else if (version == PipelineContinuationTokenV1.VersionNumber) { if (!PipelineContinuationTokenV1.TryParse( parsedContinuationToken, out PipelineContinuationTokenV1 pipelineContinuationTokenV1)) { pipelineContinuationToken = default; return(false); } pipelineContinuationToken = pipelineContinuationTokenV1; } else if (version == PipelineContinuationTokenV1_1.VersionNumber) { if (!PipelineContinuationTokenV1_1.TryParse( parsedContinuationToken, out PipelineContinuationTokenV1_1 pipelineContinuationTokenV1_1)) { pipelineContinuationToken = default; return(false); } pipelineContinuationToken = pipelineContinuationTokenV1_1; } else { pipelineContinuationToken = default; return(false); } return(true); }
public static QueryIterator Create( CosmosQueryClient client, SqlQuerySpec sqlQuerySpec, string continuationToken, QueryRequestOptions queryRequestOptions, Uri resourceLink, bool isContinuationExpected, bool allowNonValueAggregateQuery, PartitionedQueryExecutionInfo partitionedQueryExecutionInfo) { if (queryRequestOptions == null) { queryRequestOptions = new QueryRequestOptions(); } CosmosQueryContext cosmosQueryContext = new CosmosQueryContextCore( client: client, queryRequestOptions: queryRequestOptions, resourceTypeEnum: Documents.ResourceType.Document, operationType: Documents.OperationType.Query, resourceType: typeof(QueryResponseCore), resourceLink: resourceLink, isContinuationExpected: isContinuationExpected, allowNonValueAggregateQuery: allowNonValueAggregateQuery, correlatedActivityId: Guid.NewGuid()); CosmosElement requestContinuationToken; switch (queryRequestOptions.ExecutionEnvironment.GetValueOrDefault(ExecutionEnvironment.Client)) { case ExecutionEnvironment.Client: if (continuationToken != null) { if (!CosmosElement.TryParse(continuationToken, out requestContinuationToken)) { return(new QueryIterator( cosmosQueryContext, new QueryExecutionContextWithException( new MalformedContinuationTokenException( $"Malformed Continuation Token: {requestContinuationToken}")), queryRequestOptions.CosmosSerializationFormatOptions, queryRequestOptions)); } } else { requestContinuationToken = null; } break; case ExecutionEnvironment.Compute: requestContinuationToken = queryRequestOptions.CosmosElementContinuationToken; break; default: throw new ArgumentOutOfRangeException($"Unknown {nameof(ExecutionEnvironment)}: {queryRequestOptions.ExecutionEnvironment.Value}."); } CosmosQueryExecutionContextFactory.InputParameters inputParameters = new CosmosQueryExecutionContextFactory.InputParameters( sqlQuerySpec: sqlQuerySpec, initialUserContinuationToken: requestContinuationToken, maxConcurrency: queryRequestOptions.MaxConcurrency, maxItemCount: queryRequestOptions.MaxItemCount, maxBufferedItemCount: queryRequestOptions.MaxBufferedItemCount, partitionKey: queryRequestOptions.PartitionKey, properties: queryRequestOptions.Properties, partitionedQueryExecutionInfo: partitionedQueryExecutionInfo, executionEnvironment: queryRequestOptions.ExecutionEnvironment, returnResultsInDeterministicOrder: queryRequestOptions.ReturnResultsInDeterministicOrder, testInjections: queryRequestOptions.TestSettings); return(new QueryIterator( cosmosQueryContext, CosmosQueryExecutionContextFactory.Create(cosmosQueryContext, inputParameters), queryRequestOptions.CosmosSerializationFormatOptions, queryRequestOptions)); }
public static async Task <TryCatch <IDocumentQueryExecutionComponent> > TryCreateAsync( CosmosElement requestContinuation, Func <CosmosElement, Task <TryCatch <IDocumentQueryExecutionComponent> > > tryCreateSourceAsync, DistinctQueryType distinctQueryType) { if (tryCreateSourceAsync == null) { throw new ArgumentNullException(nameof(tryCreateSourceAsync)); } DistinctContinuationToken distinctContinuationToken; if (requestContinuation != null) { if (!DistinctContinuationToken.TryParse(requestContinuation, out distinctContinuationToken)) { return(TryCatch <IDocumentQueryExecutionComponent> .FromException( new MalformedContinuationTokenException( $"Invalid {nameof(DistinctContinuationToken)}: {requestContinuation}"))); } } else { distinctContinuationToken = new DistinctContinuationToken( sourceToken: null, distinctMapToken: null); } CosmosElement distinctMapToken; if (distinctContinuationToken.DistinctMapToken != null) { distinctMapToken = CosmosString.Create(distinctContinuationToken.DistinctMapToken); } else { distinctMapToken = null; } TryCatch <DistinctMap> tryCreateDistinctMap = DistinctMap.TryCreate( distinctQueryType, distinctMapToken); if (!tryCreateDistinctMap.Succeeded) { return(TryCatch <IDocumentQueryExecutionComponent> .FromException(tryCreateDistinctMap.Exception)); } CosmosElement sourceToken; if (distinctContinuationToken.SourceToken != null) { if (!CosmosElement.TryParse(distinctContinuationToken.SourceToken, out sourceToken)) { return(TryCatch <IDocumentQueryExecutionComponent> .FromException( new MalformedContinuationTokenException( $"Invalid Source Token: {distinctContinuationToken.SourceToken}"))); } } else { sourceToken = null; } TryCatch <IDocumentQueryExecutionComponent> tryCreateSource = await tryCreateSourceAsync(sourceToken); if (!tryCreateSource.Succeeded) { return(TryCatch <IDocumentQueryExecutionComponent> .FromException(tryCreateSource.Exception)); } return(TryCatch <IDocumentQueryExecutionComponent> .FromResult( new ClientDistinctDocumentQueryExecutionComponent( distinctQueryType, tryCreateDistinctMap.Result, tryCreateSource.Result))); }