示例#1
0
        /// <summary>
        /// Attempts to get an existing <see cref="IReliableIndexedDictionary{TKey, TValue}"/> with the given name, along with its indexes.
        /// </summary>
        /// <remarks>
        /// The index definitions indicate the indexes that should be created, or which should exist with this reliable collection.  The index definitions should be
        /// consistent when creating/reading/removing reliable collections, and should not be changed after creation.  Doing so can cause the index to become out of sync
        /// with the primary reliable collection, which will cause runtime exceptions.
        /// </remarks>
        public static async Task <ConditionalValue <IReliableIndexedDictionary <TKey, TValue> > > TryGetAsync <TKey, TValue>(this IReliableStateManager stateManager, Uri name, params IIndexDefinition <TKey, TValue>[] indexes)
            where TKey : IComparable <TKey>, IEquatable <TKey>
        {
            var result = await stateManager.TryGetAsync <IReliableDictionary2 <TKey, TValue> >(name).ConfigureAwait(false);

            if (!result.HasValue)
            {
                return(new ConditionalValue <IReliableIndexedDictionary <TKey, TValue> >());
            }

            return(await stateManager.TryGetIndexedAsync(result.Value, indexes).ConfigureAwait(false));
        }
        public void TestInitialize()
        {
            userDictionaryManager = new MockReliableStateManager();

            IReliableDictionary2 <UserName, Basic.Common.UserProfile> users =
                userDictionaryManager.GetOrAddAsync <IReliableDictionary2 <UserName, Basic.Common.UserProfile> >("users").Result;
            var indexed_users = userDictionaryManager.GetOrAddIndexedAsync <UserName, Basic.Common.UserProfile>("indexed_users",
                                                                                                                FilterableIndex <UserName, Basic.Common.UserProfile, string> .CreateQueryableInstance("Email"),
                                                                                                                FilterableIndex <UserName, Basic.Common.UserProfile, int> .CreateQueryableInstance("Age")).Result;

            for (int i = 0; i < 5; i++)
            {
                using (var tx = userDictionaryManager.CreateTransaction())
                {
                    var user = new Basic.Common.UserProfile
                    {
                        Name = new UserName
                        {
                            First = $"First{i}",
                            Last  = $"Last{i}",
                        },
                        Email   = $"user-{i}@example.com",
                        Age     = 20 + i / 3,
                        Address = new Basic.Common.Address
                        {
                            AddressLine1 = $"1{i} Main St.",
                            City         = "Seattle",
                            State        = "WA",
                            Zipcode      = 98117,
                        },
                    };


                    users.SetAsync(tx, user.Name, user, TimeSpan.FromSeconds(4), new CancellationToken());
                    indexed_users.SetAsync(tx, user.Name, user, TimeSpan.FromSeconds(4), new CancellationToken());
                    tx.CommitAsync();
                }
            }

            Assert.IsTrue(userDictionaryManager.TryGetAsync <IReliableDictionary2 <UserName, Basic.Common.UserProfile> >("users").Result.HasValue);
            Assert.IsTrue(userDictionaryManager.TryGetIndexedAsync <UserName, Basic.Common.UserProfile>("indexed_users",
                                                                                                        FilterableIndex <UserName, Basic.Common.UserProfile, string> .CreateQueryableInstance("Email"),
                                                                                                        FilterableIndex <UserName, Basic.Common.UserProfile, int> .CreateQueryableInstance("Age")).Result.HasValue);
        }
        private static async Task <IEnumerable <KeyValuePair <TKey, TValue> > > TryFilterQuery <TKey, TValue>(ODataQueryOptions options, IReliableStateManager stateManager, string dictName, CancellationToken cancellationToken)
            where TKey : IComparable <TKey>, IEquatable <TKey>
        {
            if (options.Filter == null)
            {
                return(null);
            }
            SingleValueNode    root         = options.Filter.FilterClause.Expression;
            IEnumerable <TKey> filteredKeys = await TryFilterNode <TKey, TValue>(root, false, stateManager, dictName, cancellationToken);

            // Expression was not filterable
            if (filteredKeys == null)
            {
                return(null);
            }
            else
            {
                ConditionalValue <IReliableIndexedDictionary <TKey, TValue> > dictOption = await stateManager.TryGetIndexedAsync <TKey, TValue>(dictName, new IIndexDefinition <TKey, TValue>[] { });

                if (dictOption.HasValue)
                {
                    using (var tx = stateManager.CreateTransaction())
                    {
                        IEnumerable <KeyValuePair <TKey, TValue> > result = await dictOption.Value.GetAllAsync(tx, filteredKeys, TimeSpan.FromSeconds(4), cancellationToken).AsEnumerable();

                        await tx.CommitAsync();

                        return(result);
                    }
                }

                return(null); // Baseline Dictionary not found
            }
        }
        private static async Task <IReliableIndexedDictionary <TKey, TValue> > GetIndexedDictionaryByPropertyName <TKey, TValue, TFilter>(IReliableStateManager stateManager, string dictName, string propertyName)
            where TKey : IComparable <TKey>, IEquatable <TKey>
            where TFilter : IComparable <TFilter>, IEquatable <TFilter>
        {
            FilterableIndex <TKey, TValue, TFilter> filter = FilterableIndex <TKey, TValue, TFilter> .CreateQueryableInstance(propertyName);

            ConditionalValue <IReliableIndexedDictionary <TKey, TValue> > dictOption = await stateManager.TryGetIndexedAsync <TKey, TValue>(dictName, new[] { filter });

            if (dictOption.HasValue)
            {
                return(dictOption.Value);
            }
            else
            {
                return(null);
            }
        }