public override async Task <TryCatch <QueryPage> > MonadicQueryAsync(
                SqlQuerySpec sqlQuerySpec,
                FeedRangeState <QueryState> feedRangeState,
                QueryPaginationOptions queryPaginationOptions,
                ITrace trace,
                CancellationToken cancellationToken)
            {
                await this.semaphore.WaitAsync(this.cancellationToken);

                int count = ParseQueryState(feedRangeState.State);

                QueryState continuationToken = count < this.continuationCount ? CreateQueryState(++count) : default;
                QueryPage  page = new QueryPage(
                    documents: new List <CosmosElement> {
                },
                    requestCharge: 3.0,
                    activityId: "E7980B1F-436E-44DF-B7A5-655C56D38648",
                    responseLengthInBytes: 48,
                    cosmosQueryExecutionInfo: new Lazy <CosmosQueryExecutionInfo>(() => new CosmosQueryExecutionInfo(false, false)),
                    disallowContinuationTokenMessage: null,
                    additionalHeaders: null,
                    state: continuationToken);

                return(continuationToken != default ?
                       TryCatch <QueryPage> .FromResult(page) :
                       await base.MonadicQueryAsync(
                           sqlQuerySpec,
                           new FeedRangeState <QueryState>(feedRangeState.FeedRange, default),
                           queryPaginationOptions,
                           trace,
                           cancellationToken));
            }
 public Task <TryCatch <QueryPage> > MonadicQueryAsync(
     SqlQuerySpec sqlQuerySpec,
     FeedRangeState <QueryState> feedRangeState,
     QueryPaginationOptions queryPaginationOptions,
     ITrace trace,
     CancellationToken cancellationToken) => this.monadicDocumentContainer.MonadicQueryAsync(
     sqlQuerySpec,
     feedRangeState,
     queryPaginationOptions,
     trace,
     cancellationToken);
Beispiel #3
0
 private static CreatePartitionRangePageAsyncEnumerator <QueryPage, QueryState> MakeCreateFunction(
     IQueryDataSource queryDataSource,
     SqlQuerySpec sqlQuerySpec,
     QueryPaginationOptions queryPaginationOptions,
     Cosmos.PartitionKey?partitionKey,
     CancellationToken cancellationToken) => (FeedRangeState <QueryState> feedRangeState) => new QueryPartitionRangePageAsyncEnumerator(
     queryDataSource,
     sqlQuerySpec,
     feedRangeState,
     partitionKey,
     queryPaginationOptions,
     cancellationToken);
 public Task <QueryPage> QueryAsync(
     SqlQuerySpec sqlQuerySpec,
     FeedRangeState <QueryState> feedRangeState,
     QueryPaginationOptions queryPaginationOptions,
     ITrace trace,
     CancellationToken cancellationToken) => TryCatch <QueryPage> .UnsafeGetResultAsync(
     this.MonadicQueryAsync(
         sqlQuerySpec,
         feedRangeState,
         queryPaginationOptions,
         trace,
         cancellationToken),
     cancellationToken);
Beispiel #5
0
 public QueryPartitionRangePageAsyncEnumerator(
     IQueryDataSource queryDataSource,
     SqlQuerySpec sqlQuerySpec,
     FeedRangeState <QueryState> feedRangeState,
     Cosmos.PartitionKey?partitionKey,
     QueryPaginationOptions queryPaginationOptions,
     CancellationToken cancellationToken)
     : base(feedRangeState, cancellationToken)
 {
     this.queryDataSource        = queryDataSource ?? throw new ArgumentNullException(nameof(queryDataSource));
     this.sqlQuerySpec           = sqlQuerySpec ?? throw new ArgumentNullException(nameof(sqlQuerySpec));
     this.queryPaginationOptions = queryPaginationOptions;
     this.partitionKey           = partitionKey;
 }
 public InnerEnumerator(
     IQueryDataSource queryDataSource,
     SqlQuerySpec sqlQuerySpec,
     FeedRangeState <QueryState> feedRangeState,
     PartitionKey?partitionKey,
     QueryPaginationOptions queryPaginationOptions,
     string filter,
     CancellationToken cancellationToken)
     : base(feedRangeState, cancellationToken)
 {
     this.queryDataSource        = queryDataSource ?? throw new ArgumentNullException(nameof(queryDataSource));
     this.SqlQuerySpec           = sqlQuerySpec ?? throw new ArgumentNullException(nameof(sqlQuerySpec));
     this.PartitionKey           = partitionKey;
     this.queryPaginationOptions = queryPaginationOptions ?? QueryPaginationOptions.Default;
     this.Filter = filter;
 }
        public static TryCatch <IQueryPipelineStage> MonadicCreate(
            IDocumentContainer documentContainer,
            SqlQuerySpec sqlQuerySpec,
            IReadOnlyList <FeedRangeEpk> targetRanges,
            Cosmos.PartitionKey?partitionKey,
            QueryPaginationOptions queryPaginationOptions,
            int maxConcurrency,
            PrefetchPolicy prefetchPolicy,
            CosmosElement continuationToken,
            CancellationToken cancellationToken)
        {
            if (targetRanges == null)
            {
                throw new ArgumentNullException(nameof(targetRanges));
            }

            if (targetRanges.Count == 0)
            {
                throw new ArgumentException($"{nameof(targetRanges)} must have some elements");
            }

            TryCatch <CrossFeedRangeState <QueryState> > monadicExtractState = MonadicExtractState(continuationToken, targetRanges);

            if (monadicExtractState.Failed)
            {
                return(TryCatch <IQueryPipelineStage> .FromException(monadicExtractState.Exception));
            }

            CrossFeedRangeState <QueryState> state = monadicExtractState.Result;

            CrossPartitionRangePageAsyncEnumerator <QueryPage, QueryState> crossPartitionPageEnumerator = new CrossPartitionRangePageAsyncEnumerator <QueryPage, QueryState>(
                feedRangeProvider: documentContainer,
                createPartitionRangeEnumerator: ParallelCrossPartitionQueryPipelineStage.MakeCreateFunction(documentContainer, sqlQuerySpec, queryPaginationOptions, partitionKey, cancellationToken),
                comparer: Comparer.Singleton,
                maxConcurrency: maxConcurrency,
                prefetchPolicy: prefetchPolicy,
                state: state,
                cancellationToken: cancellationToken);

            ParallelCrossPartitionQueryPipelineStage stage = new ParallelCrossPartitionQueryPipelineStage(crossPartitionPageEnumerator, cancellationToken);

            return(TryCatch <IQueryPipelineStage> .FromResult(stage));
        }
 public OrderByQueryPartitionRangePageAsyncEnumerator(
     IQueryDataSource queryDataSource,
     SqlQuerySpec sqlQuerySpec,
     FeedRangeState <QueryState> feedRangeState,
     PartitionKey?partitionKey,
     QueryPaginationOptions queryPaginationOptions,
     string filter,
     CancellationToken cancellationToken)
     : base(feedRangeState, cancellationToken)
 {
     this.StartOfPageState = feedRangeState.State;
     this.innerEnumerator  = new InnerEnumerator(
         queryDataSource,
         sqlQuerySpec,
         feedRangeState,
         partitionKey,
         queryPaginationOptions,
         filter,
         cancellationToken);
     this.bufferedEnumerator = new BufferedPartitionRangePageAsyncEnumerator <OrderByQueryPage, QueryState>(
         this.innerEnumerator,
         cancellationToken);
 }
        public static TryCatch <IQueryPipelineStage> MonadicCreate(
            ExecutionEnvironment executionEnvironment,
            IDocumentContainer documentContainer,
            SqlQuerySpec sqlQuerySpec,
            IReadOnlyList <FeedRangeEpk> targetRanges,
            PartitionKey?partitionKey,
            QueryInfo queryInfo,
            QueryPaginationOptions queryPaginationOptions,
            int maxConcurrency,
            CosmosElement requestContinuationToken,
            CancellationToken requestCancellationToken)
        {
            if (documentContainer == null)
            {
                throw new ArgumentNullException(nameof(documentContainer));
            }

            if (sqlQuerySpec == null)
            {
                throw new ArgumentNullException(nameof(sqlQuerySpec));
            }

            if (targetRanges == null)
            {
                throw new ArgumentNullException(nameof(targetRanges));
            }

            if (targetRanges.Count == 0)
            {
                throw new ArgumentException($"{nameof(targetRanges)} must not be empty.");
            }

            if (queryInfo == null)
            {
                throw new ArgumentNullException(nameof(queryInfo));
            }

            sqlQuerySpec = !string.IsNullOrEmpty(queryInfo.RewrittenQuery) ? new SqlQuerySpec(queryInfo.RewrittenQuery, sqlQuerySpec.Parameters) : sqlQuerySpec;

            PrefetchPolicy prefetchPolicy = DeterminePrefetchPolicy(queryInfo);

            MonadicCreatePipelineStage monadicCreatePipelineStage;

            if (queryInfo.HasOrderBy)
            {
                monadicCreatePipelineStage = (continuationToken, cancellationToken) => OrderByCrossPartitionQueryPipelineStage.MonadicCreate(
                    documentContainer: documentContainer,
                    sqlQuerySpec: sqlQuerySpec,
                    targetRanges: targetRanges,
                    partitionKey: partitionKey,
                    orderByColumns: queryInfo
                    .OrderByExpressions
                    .Zip(queryInfo.OrderBy, (expression, sortOrder) => new OrderByColumn(expression, sortOrder)).ToList(),
                    queryPaginationOptions: queryPaginationOptions,
                    maxConcurrency: maxConcurrency,
                    continuationToken: continuationToken,
                    cancellationToken: cancellationToken);
            }
            else
            {
                monadicCreatePipelineStage = (continuationToken, cancellationToken) => ParallelCrossPartitionQueryPipelineStage.MonadicCreate(
                    documentContainer: documentContainer,
                    sqlQuerySpec: sqlQuerySpec,
                    targetRanges: targetRanges,
                    queryPaginationOptions: queryPaginationOptions,
                    partitionKey: partitionKey,
                    prefetchPolicy: prefetchPolicy,
                    maxConcurrency: maxConcurrency,
                    continuationToken: continuationToken,
                    cancellationToken: cancellationToken);
            }

            if (queryInfo.HasAggregates && !queryInfo.HasGroupBy)
            {
                MonadicCreatePipelineStage monadicCreateSourceStage = monadicCreatePipelineStage;
                monadicCreatePipelineStage = (continuationToken, cancellationToken) => AggregateQueryPipelineStage.MonadicCreate(
                    executionEnvironment,
                    queryInfo.Aggregates,
                    queryInfo.GroupByAliasToAggregateType,
                    queryInfo.GroupByAliases,
                    queryInfo.HasSelectValue,
                    continuationToken,
                    cancellationToken,
                    monadicCreateSourceStage);
            }

            if (queryInfo.HasDistinct)
            {
                MonadicCreatePipelineStage monadicCreateSourceStage = monadicCreatePipelineStage;
                monadicCreatePipelineStage = (continuationToken, cancellationToken) => DistinctQueryPipelineStage.MonadicCreate(
                    executionEnvironment,
                    continuationToken,
                    cancellationToken,
                    monadicCreateSourceStage,
                    queryInfo.DistinctType);
            }

            if (queryInfo.HasGroupBy)
            {
                MonadicCreatePipelineStage monadicCreateSourceStage = monadicCreatePipelineStage;
                monadicCreatePipelineStage = (continuationToken, cancellationToken) => GroupByQueryPipelineStage.MonadicCreate(
                    executionEnvironment,
                    continuationToken,
                    cancellationToken,
                    monadicCreateSourceStage,
                    queryInfo.GroupByAliasToAggregateType,
                    queryInfo.GroupByAliases,
                    queryInfo.HasSelectValue,
                    (queryPaginationOptions ?? QueryPaginationOptions.Default).PageSizeLimit.GetValueOrDefault(int.MaxValue));
            }

            if (queryInfo.HasOffset)
            {
                MonadicCreatePipelineStage monadicCreateSourceStage = monadicCreatePipelineStage;
                monadicCreatePipelineStage = (continuationToken, cancellationToken) => SkipQueryPipelineStage.MonadicCreate(
                    executionEnvironment,
                    queryInfo.Offset.Value,
                    continuationToken,
                    cancellationToken,
                    monadicCreateSourceStage);
            }

            if (queryInfo.HasLimit)
            {
                MonadicCreatePipelineStage monadicCreateSourceStage = monadicCreatePipelineStage;
                monadicCreatePipelineStage = (continuationToken, cancellationToken) => TakeQueryPipelineStage.MonadicCreateLimitStage(
                    executionEnvironment,
                    queryInfo.Limit.Value,
                    continuationToken,
                    cancellationToken,
                    monadicCreateSourceStage);
            }

            if (queryInfo.HasTop)
            {
                MonadicCreatePipelineStage monadicCreateSourceStage = monadicCreatePipelineStage;
                monadicCreatePipelineStage = (continuationToken, cancellationToken) => TakeQueryPipelineStage.MonadicCreateTopStage(
                    executionEnvironment,
                    queryInfo.Top.Value,
                    continuationToken,
                    cancellationToken,
                    monadicCreateSourceStage);
            }

            if (queryInfo.HasDCount)
            {
                MonadicCreatePipelineStage monadicCreateSourceStage = monadicCreatePipelineStage;
                monadicCreatePipelineStage = (continuationToken, cancellationToken) => DCountQueryPipelineStage.MonadicCreate(
                    executionEnvironment,
                    queryInfo.DCountInfo,
                    continuationToken,
                    cancellationToken,
                    monadicCreateSourceStage);
            }

            return(monadicCreatePipelineStage(requestContinuationToken, requestCancellationToken)
                   .Try <IQueryPipelineStage>(onSuccess: (stage) => new SkipEmptyPageQueryPipelineStage(stage, requestCancellationToken)));
        }