Exemple #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CosmosDbArgs{T, TModel}"/> class.
 /// </summary>
 /// <param name="mapper">The <see cref="IEntityMapper{T, TModel}"/>.</param>
 /// <param name="containerId">The <see cref="Container"/> identifier.</param>
 /// <param name="partitionKey">The <see cref="PartitionKey"/>.</param>
 /// <param name="paging">The <see cref="PagingResult"/>.</param>
 /// <param name="requestOptions">The optional <see cref="FeedOptions"/>.</param>
 public CosmosDbArgs(IEntityMapper <T, TModel> mapper, string containerId, PartitionKey?partitionKey, PagingResult paging, QueryRequestOptions?requestOptions = null)
 {
     Mapper              = Check.NotNull(mapper, nameof(mapper));
     ContainerId         = Check.NotEmpty(containerId, nameof(containerId));
     PartitionKey        = partitionKey;
     Paging              = Check.NotNull(paging, nameof(paging));
     QueryRequestOptions = requestOptions;
 }
        /// <summary>
        /// Executes a query and iterates the result with a synchronous iterator.
        /// </summary>
        /// <typeparam name="T">The type of the entity to iterate.</typeparam>
        /// <param name="queryText">The query text.</param>
        /// <param name="container">The Cosmos Container.</param>
        /// <param name="scenarioContext">The scenario context in which to set the results.</param>
        /// <param name="resultsKey">The key in which to set the results (or null if the results do not need to be set).</param>
        /// <param name="batchSize">The batch size, or null if the default is to be used.</param>
        /// <param name="maxBatchCount">The max batch count, or null if the default is to be used.</param>
        /// <returns>The people found when iterating the query.</returns>
        internal static async Task <IList <T> > IteratePeopleWithSyncMethodAsync <T>(string queryText, Container container, ScenarioContext scenarioContext, string?resultsKey = null, int?batchSize = null, int?maxBatchCount = null)
        {
            var results = new List <T>();
            QueryRequestOptions?requestOptions = batchSize.HasValue ? new QueryRequestOptions {
                MaxItemCount = batchSize
            } : null;
            await container.ForEachAsync <T>(queryText, t => results.Add(t), requestOptions, maxBatchCount).ConfigureAwait(false);

            scenarioContext.Set(results, resultsKey);
            return(results);
        }
Exemple #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CosmosDbArgs{T, TModel}"/> class.
 /// </summary>
 /// <param name="mapper">The <see cref="IEntityMapper{T, TModel}"/>.</param>
 /// <param name="containerId">The <see cref="Container"/> identifier.</param>
 /// <param name="partitionKey">The <see cref="PartitionKey"/>.</param>
 /// <param name="paging">The <see cref="PagingResult"/>.</param>
 /// <param name="requestOptions">The optional <see cref="FeedOptions"/>.</param>
 public CosmosDbArgs(IEntityMapper <T, TModel> mapper, string containerId, PartitionKey?partitionKey, PagingArgs paging, QueryRequestOptions?requestOptions = null)
     : this(mapper, containerId, partitionKey, new PagingResult(Check.NotNull(paging, (nameof(paging)))), requestOptions)
 {
 }
Exemple #4
0
        /// <summary>
        /// Creates a <see cref="CosmosDbArgs"/> with an <i>AutoMapper</i> <paramref name="mapper"/>.
        /// </summary>
        /// <param name="mapper">The <i>AutoMapper</i> <see cref="IMapper"/>.</param>
        /// <param name="containerId">The <see cref="Container"/> identifier.</param>
        /// <param name="partitionKey">The <see cref="Microsoft.Azure.Cosmos.PartitionKey"/>.</param>
        /// <param name="paging">The <see cref="PagingResult"/>.</param>
        /// <param name="requestOptions">The optional <see cref="FeedOptions"/>.</param>
        /// <param name="onCreate">Optional action to perform additional processing.</param>
        /// <returns>The <see cref="CosmosDbArgs"/>.</returns>
        public static CosmosDbArgs Create(IMapper mapper, string containerId, PagingResult paging, PartitionKey?partitionKey = null, QueryRequestOptions?requestOptions = null, Action <CosmosDbArgs>?onCreate = null)
        {
            var dbArgs = new CosmosDbArgs(mapper, containerId, paging, partitionKey, requestOptions);

            onCreate?.Invoke(dbArgs);
            return(dbArgs);
        }
        /// <summary>
        /// Enumerate the entities matching a particular query.
        /// </summary>
        /// <typeparam name="T">The type of entity to enumerate.</typeparam>
        /// <param name="container">The Cosmos container against which to execute the query.</param>
        /// <param name="queryDefinition">The query definition.</param>
        /// <param name="action">The action to execute.</param>
        /// <param name="requestOptions">Request options for the query.</param>
        /// <param name="maxBatchCount">The maximum number of batches to process.</param>
        /// <param name="continuationToken">The continuation token from which to resume processing.</param>
        /// <param name="cancellationToken">A cancellation token to terminate the option early.</param>
        /// <returns>A <see cref="Task"/> which provides a continuation token if it terminates before .</returns>
        public static async Task <string?> ForEachAsync <T>(this Container container, QueryDefinition queryDefinition, Action <T> action, QueryRequestOptions?requestOptions = null, int?maxBatchCount = null, string?continuationToken = null, CancellationToken cancellationToken = default)
        {
            if (container is null)
            {
                throw new ArgumentNullException(nameof(container));
            }

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

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

            FeedIterator <T> iterator = container.GetItemQueryIterator <T>(queryDefinition, continuationToken, requestOptions);

            int    batchCount = 0;
            string?previousContinuationToken = null;
            string?responseContinuationToken = null;

            try
            {
                while (iterator.HasMoreResults && (!maxBatchCount.HasValue || batchCount < maxBatchCount.Value))
                {
                    batchCount++;

                    FeedResponse <T> response = await Retriable.RetryAsync(
                        () => iterator.ReadNextAsync(cancellationToken),
                        CancellationToken.None,
                        new Backoff(3, TimeSpan.FromSeconds(1)),
                        RetryOnBusyPolicy.Instance,
                        false)
                                                .ConfigureAwait(false);

                    previousContinuationToken = responseContinuationToken;
                    responseContinuationToken = response.ContinuationToken;
                    foreach (T item in response)
                    {
                        action(item);
                        cancellationToken.ThrowIfCancellationRequested();
                    }
                }
            }
            catch (OperationCanceledException)
            {
                // We cancelled the operation so we revert to the previous continuation token to allow reprocessing from the previous batch
                responseContinuationToken = previousContinuationToken;
            }

            return(responseContinuationToken);
        }
        /// <summary>
        /// Enumerate the entities matching a particular query.
        /// </summary>
        /// <typeparam name="T">The type of entity to enumerate.</typeparam>
        /// <param name="container">The Cosmos container against which to execute the query.</param>
        /// <param name="queryText">The query text.</param>
        /// <param name="action">The action to execute.</param>
        /// <param name="requestOptions">Request options for the query.</param>
        /// <param name="maxBatchCount">The maximum number of batches to process.</param>
        /// <param name="continuationToken">The continuation token from which to resume processing.</param>
        /// <param name="cancellationToken">A cancellation token to terminate the option early.</param>
        /// <returns>A <see cref="Task"/> which provides a continuation token if it terminates before .</returns>
        public static Task <string?> ForEachAsync <T>(this Container container, string queryText, Action <T> action, QueryRequestOptions?requestOptions = null, int?maxBatchCount = null, string?continuationToken = null, CancellationToken cancellationToken = default)
        {
            if (container is null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            if (queryText is null)
            {
                throw new ArgumentNullException(nameof(queryText));
            }

            if (action is null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            return(ForEachAsync(container, new QueryDefinition(queryText), action, requestOptions, maxBatchCount, continuationToken, cancellationToken));
        }