Esempio n. 1
0
        //Generic query method returning a IList<KeyValuePair<Guid, T>>
        public static async Task <IList <KeyValuePair <Guid, T> > > QueryReliableDictionaryKeyValuePairList <T>(Microsoft.ServiceFabric.Data.IReliableStateManager stateManager,
                                                                                                                string reliableDictionaryName,
                                                                                                                Func <T, bool> filter)
        {
            var result = new List <KeyValuePair <Guid, T> >();

            IReliableDictionary <Guid, T> reliableDictionary =
                await stateManager.GetOrAddAsync <IReliableDictionary <Guid, T> >(reliableDictionaryName);

            using (var tx = stateManager.CreateTransaction())
            {
                Microsoft.ServiceFabric.Data.IAsyncEnumerable <KeyValuePair <Guid, T> > asyncEnumerable =
                    await reliableDictionary.CreateEnumerableAsync(tx);

                using (Microsoft.ServiceFabric.Data.IAsyncEnumerator <KeyValuePair <Guid, T> > asyncEnumerator = asyncEnumerable.GetAsyncEnumerator())
                {
                    while (await asyncEnumerator.MoveNextAsync(CancellationToken.None))
                    {
                        if (filter(asyncEnumerator.Current.Value))
                        {
                            result.Add(asyncEnumerator.Current);
                        }
                    }
                }
            }
            return(result);
        }
        public async Task InfiniteLoop_Issue91()
        {
            var sut = new MockReliableStateManager();

            var collection = await sut.GetOrAddAsync <IReliableDictionary2 <Guid, long> >("Collection");

            using (var tx = sut.CreateTransaction())
            {
                var query = await collection.CreateEnumerableAsync(tx, key => false, EnumerationMode.Unordered);

                var list = new List <Guid>();
                //This goes into infinite loop if the query returns an empty collection with a key value pair of null for both the key and the value.
                Microsoft.ServiceFabric.Data.IAsyncEnumerator <KeyValuePair <Guid, long> > asyncEnumerator = query.GetAsyncEnumerator();
                while (await asyncEnumerator.MoveNextAsync(CancellationToken.None))
                {
                    list.Add(asyncEnumerator.Current.Key);
                }
                await tx.CommitAsync();
            }
            Assert.IsTrue(true, "Seems to work.");
            //Assert.Fail("Shouldn't reach here.");
        }
        public static async Task ForEach <T>(
            [NotNull] this Microsoft.ServiceFabric.Data.IAsyncEnumerable <T> enumerable,
            CancellationToken token,
            [NotNull][InstantHandle] Action <T> block)
        {
            if (enumerable == null)
            {
                throw new ArgumentNullException(nameof(enumerable));
            }

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

            using (Microsoft.ServiceFabric.Data.IAsyncEnumerator <T> enumerator = enumerable.GetAsyncEnumerator())
            {
                while (await enumerator.MoveNextAsync(token))
                {
                    block(enumerator.Current);
                }
            }
        }
Esempio n. 4
0
        public async Task <IEnumerable <Product.Model.Product> > GetAllProducts()
        {
            IReliableDictionary <Guid, Product.Model.Product> products = await
                                                                         _stateManager.GetOrAddAsync <IReliableDictionary <Guid, Product.Model.Product> >("products");

            var result = new List <Product.Model.Product>();

            using (ITransaction tx = _stateManager.CreateTransaction())
            {
                Microsoft.ServiceFabric.Data.IAsyncEnumerable <KeyValuePair <Guid, Product.Model.Product> > allProducts = await
                                                                                                                          products.CreateEnumerableAsync(tx, EnumerationMode.Unordered);

                using (Microsoft.ServiceFabric.Data.IAsyncEnumerator <KeyValuePair <Guid, Product.Model.Product> > enumerator = allProducts.GetAsyncEnumerator())
                {
                    while (await enumerator.MoveNextAsync(CancellationToken.None))
                    {
                        KeyValuePair <Guid, Product.Model.Product> current = enumerator.Current;
                        result.Add(current.Value);
                    }
                }
            }

            return(result);
        }
        public static async Task ForEach <TKey, TValue>(
            [NotNull] this Microsoft.ServiceFabric.Data.IAsyncEnumerable <KeyValuePair <TKey, TValue> > enumerable,
            CancellationToken token,
            [NotNull][InstantHandle] Func <TKey, TValue, Task> block)
        {
            if (enumerable == null)
            {
                throw new ArgumentNullException(nameof(enumerable));
            }

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

            using (Microsoft.ServiceFabric.Data.IAsyncEnumerator <KeyValuePair <TKey, TValue> > enumerator = enumerable.GetAsyncEnumerator())
            {
                while (await enumerator.MoveNextAsync(token))
                {
                    KeyValuePair <TKey, TValue> pair = enumerator.Current;
                    await block(pair.Key, pair.Value);
                }
            }
        }