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 async Task <IReliableDictionary2 <string, int> > GetState()
        {
            if (reliableDictionary != null)
            {
                return(reliableDictionary);
            }

            IReliableDictionary2 <string, int> state = null;

            var stateValue = await stateManagerProvider.Current
                             .TryGetAsync <IReliableDictionary2 <string, int> >(Key)
                             .ConfigureAwait(false);

            if (!stateValue.HasValue)
            {
                // this has to be done in a separate transaction. https://github.com/Azure/service-fabric-issues/issues/24
                using (var transaction = stateManagerProvider.Current.CreateTransaction())
                {
                    state = await stateManagerProvider
                            .Current
                            .GetOrAddAsync <IReliableDictionary2 <string, int> >(transaction, Key)
                            .ConfigureAwait(false);

                    await transaction.CommitAsync();
                }
            }
            else
            {
                state = stateValue.Value;
            }

            lock (LockObject)
            {
                if (reliableDictionary == null)
                {
                    reliableDictionary = state;
                }
            }

            return(reliableDictionary);
        }
Пример #3
0
 /// <summary>
 /// This is the main entry point for your service replica.
 /// This method executes when this replica of your service becomes primary and has write status.
 /// </summary>
 /// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service replica.</param>
 /// <returns>Async task to indicate the completion of this method.</returns>
 protected override async Task RunAsync(CancellationToken cancellationToken)
 {
     this.fabricDataStore = await this.StateManager.GetOrAddAsync <IReliableDictionary2 <string, byte[]> >("FabricDataStore").ConfigureAwait(false);
 }
Пример #4
0
        /// <summary>
        /// Load or create the reliable collection for this index and cache it.
        /// This is called internally and should not be directly called.
        /// </summary>
        async Task IIndexDefinition <TKey, TValue> .GetOrAddIndexAsync(ITransaction tx, IReliableStateManager stateManager, Uri baseName, TimeSpan timeout)
        {
            var indexName = GetIndexName(baseName);

            _index = await stateManager.GetOrAddAsync <IReliableDictionary2 <TFilter, TKey[]> >(tx, indexName, timeout).ConfigureAwait(false);
        }
Пример #5
0
        private static async Task <ConditionalValue <IReliableIndexedDictionary <TKey, TValue> > > TryGetIndexedAsync <TKey, TValue>(this IReliableStateManager stateManager, IReliableDictionary2 <TKey, TValue> dictionary, params IIndexDefinition <TKey, TValue>[] indexes)
            where TKey : IComparable <TKey>, IEquatable <TKey>
        {
            // Get or create each index.
            Uri baseName = GetBaseIndexUri(dictionary.Name);

            foreach (var index in indexes)
            {
                if (!await index.TryGetIndexAsync(stateManager, baseName).ConfigureAwait(false))
                {
                    return(new ConditionalValue <IReliableIndexedDictionary <TKey, TValue> >());
                }
            }

            var result = new ReliableIndexedDictionary <TKey, TValue>(dictionary, indexes);

            return(new ConditionalValue <IReliableIndexedDictionary <TKey, TValue> >(true, result));
        }
Пример #6
0
        private static async Task <IReliableIndexedDictionary <TKey, TValue> > GetOrAddIndexedAsync <TKey, TValue>(this IReliableStateManager stateManager, ITransaction tx, TimeSpan timeout, IReliableDictionary2 <TKey, TValue> dictionary, Uri baseName, params IIndexDefinition <TKey, TValue>[] indexes)
            where TKey : IComparable <TKey>, IEquatable <TKey>
        {
            // Get or create each index.
            foreach (var index in indexes)
            {
                await index.GetOrAddIndexAsync(tx, stateManager, baseName, timeout).ConfigureAwait(false);
            }

            return(new ReliableIndexedDictionary <TKey, TValue>(dictionary, indexes));
        }
 public ReliableLists(IReliableStateManager stateManager, IReliableDictionary2 <string, ReliableListMetaData> metadataStore, IReliableDictionary2 <ReliableListKey, TValue> valueStore, string name)
 {
     _stateManager  = stateManager;
     _metadataStore = metadataStore;
     _valueStore    = valueStore;
     _name          = name;
 }
        internal async static Task <ReliableLists <TValue> > CreateAsync(IReliableStateManager stateManager, IReliableDictionary2 <string, ReliableListMetaData> metadataStore, string name)
        {
            var valueStore = await stateManager.GetOrAddAsync <IReliableDictionary2 <ReliableListKey, TValue> >(name).ConfigureAwait(false);

            return(new ReliableLists <TValue>(stateManager, metadataStore, valueStore, name));
        }
Пример #9
0
 private ReliableListManager(IReliableStateManager stateManager, IReliableDictionary2 <string, ReliableListMetaData> metadataStore)
 {
     _stateManager  = stateManager;
     _metadataStore = metadataStore;
 }