Beispiel #1
0
        /// <summary>
        /// Executes a table query and processes the results as they arrive.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="processResultAsync"></param>
        /// <param name="cancellationToken"></param>
        public static async Task ForEachQueryResultAsync <T>(
            this TableQuery <T> query,
            Func <T, Task <bool> > processResultAsync,
            CancellationToken cancellationToken = default) where T : ITableEntity, new()
        {
            TableContinuationToken?token = null;

            var count = 0;

            do
            {
                var seg = await query.ExecuteSegmentedAsync(token);

                token = seg.ContinuationToken;
                foreach (var result in seg)
                {
                    if (!await processResultAsync(result))
                    {
                        break;
                    }

                    count++;
                }
            }while (token != null && !cancellationToken.IsCancellationRequested && (query.TakeCount == null || count < query.TakeCount.Value));
        }
        /// <summary>
        /// Read data entries and their corresponding eTags from the Azure table.
        /// </summary>
        /// <param name="predicate">Predicate function to use for querying the table and filtering the results.</param>
        /// <returns>Enumeration of entries in the table which match the query condition.</returns>
        public async Task <IEnumerable <Tuple <T, string> > > ReadTableEntriesAndEtagsAsync(Expression <Func <T, bool> > predicate)
        {
            const string operation = "ReadTableEntriesAndEtags";
            var          startTime = DateTime.UtcNow;

            try
            {
                TableQuery <T> cloudTableQuery = predicate == null
                    ? tableReference.CreateQuery <T>()
                    : tableReference.CreateQuery <T>().Where(predicate).AsTableQuery();

                try
                {
                    Func <Task <List <T> > > executeQueryHandleContinuations = async() =>
                    {
                        TableQuerySegment <T> querySegment = null;
                        var list = new List <T>();
                        while (querySegment == null || querySegment.ContinuationToken != null)
                        {
                            querySegment = await cloudTableQuery.ExecuteSegmentedAsync(querySegment?.ContinuationToken);

                            list.AddRange(querySegment);
                        }

                        return(list);
                    };

                    IBackoffProvider backoff = new FixedBackoff(AzureTableDefaultPolicies.PauseBetweenTableOperationRetries);

                    List <T> results = await AsyncExecutorWithRetries.ExecuteWithRetries(
                        counter => executeQueryHandleContinuations(),
                        AzureTableDefaultPolicies.MaxTableOperationRetries,
                        (exc, counter) => AzureStorageUtils.AnalyzeReadException(exc.GetBaseException(), counter, TableName, Logger),
                        AzureTableDefaultPolicies.TableOperationTimeout,
                        backoff);

                    // Data was read successfully if we got to here
                    return(results.Select(i => Tuple.Create(i, i.ETag)).ToList());
                }
                catch (Exception exc)
                {
                    // Out of retries...
                    var errorMsg = $"Failed to read Azure storage table {TableName}: {exc.Message}";
                    if (!AzureStorageUtils.TableStorageDataNotFound(exc))
                    {
                        Logger.Warn(ErrorCode.AzureTable_09, errorMsg, exc);
                    }
                    throw new OrleansException(errorMsg, exc);
                }
            }
            finally
            {
                CheckAlertSlowAccess(startTime, operation);
            }
        }
        public static async IAsyncEnumerable <T> ExecuteAsync <T>(this TableQuery <T> query, [EnumeratorCancellation] CancellationToken token = default)
        {
            TableQuerySegment <T> segment;

            do
            {
                segment = await query.ExecuteSegmentedAsync(null, token);

                foreach (var result in segment.Results)
                {
                    yield return(result);
                }
            } while (segment.ContinuationToken != null && !token.IsCancellationRequested);
        }
Beispiel #4
0
        public static async Task <IList <T> > ExecuteQueryAsync <T>(this TableQuery <T> query, CancellationToken ct = default(CancellationToken)) where T : ITableEntity, new()
        {
            var items = new List <T>();
            TableContinuationToken token = null;

            do
            {
                TableQuerySegment <T> seg = await query.ExecuteSegmentedAsync(token).ContinueOnAnyContext();

                token = seg.ContinuationToken;
                items.AddRange(seg);
            } while (token != null && !ct.IsCancellationRequested && (query.TakeCount == null || items.Count < query.TakeCount.Value));

            return(items);
        }
Beispiel #5
0
        async Task <List <T> > _querySegment(TableQuery <T> query)
        {
            TableQuerySegment <T> querySegment = null;
            var returnList = new List <T>();

            while (querySegment == null || querySegment.ContinuationToken != null)
            {
                querySegment = await query
                               .ExecuteSegmentedAsync(querySegment != null?
                                                      querySegment.ContinuationToken : null);

                if (querySegment.Results != null)
                {
                    returnList.AddRange(querySegment.Results);
                }
            }

            return(returnList);
        }
        private async Task <IList <TResult> > ExecuteAsync <TResult>(TableQuery <TResult> query)
        {
            TableContinuationToken continuationToken = null;
            IList <TResult>        results           = new List <TResult>();

            do
            {
                var res = await query.ExecuteSegmentedAsync(continuationToken);

                foreach (var item in res)
                {
                    results.Add(item);
                }

                continuationToken = res.ContinuationToken;
            } while (continuationToken != null);

            return(results);
        }
Beispiel #7
0
        /// <summary>
        /// Executes a table query and processes the results as they arrive
        /// </summary>
        public static async Task ForEachQueryResultAsync <T>(this TableQuery <T> query, Func <T, Task> processResultAsync, CancellationToken ct = default(CancellationToken)) where T : ITableEntity, new()
        {
            TableContinuationToken token = null;

            int count = 0;

            do
            {
                TableQuerySegment <T> seg = await query.ExecuteSegmentedAsync(token).ContinueOnAnyContext();

                token = seg.ContinuationToken;
                foreach (var result in seg)
                {
                    await processResultAsync(result).ContinueOnAnyContext();

                    count++;
                }
            } while (token != null && !ct.IsCancellationRequested && (query.TakeCount == null || count < query.TakeCount.Value));
        }
        /// <summary>
        /// Gets all entities from the table.
        /// </summary>
        /// <returns>An enumerable of all stored entities.</returns>
        public async Task <IEnumerable <T> > GetAll()
        {
            TableQuery <T> query = (from map in this.table.CreateQuery <T>() select map).AsTableQuery <T>();

            Func <TableContinuationToken, Task <TableQuerySegment <T> > > queryExecute = (token) => query.ExecuteSegmentedAsync(token);

            return(await ExecuteQuery(queryExecute));
        }