public async Task AddAsync(ITransaction tx, string key, TValue value)
        {
            var metadata = await GetMetadataAsync(tx, key).ConfigureAwait(false);

            var index   = metadata.From + metadata.Count;
            var listKey = new ReliableListKey {
                Key = key, Id = index
            };
            await _valueStore.SetAsync(tx, listKey, value).ConfigureAwait(false);

            var updatedMetadata = new ReliableListMetaData {
                From = metadata.From, Count = metadata.Count + 1
            };

            await UpdateMetadataAsync(tx, key, updatedMetadata).ConfigureAwait(false);
        }
Esempio n. 2
0
        private async Task RemoveAsync(ITransaction tx, TKey key, IEnumerable <string> words, TimeSpan timeout, CancellationToken token)
        {
            foreach (var word in words)
            {
                // This key should exist in the index for each word.
                var result = await _index.TryGetValueAsync(tx, word, LockMode.Update, timeout, token).ConfigureAwait(false);

                if (!result.HasValue)
                {
                    throw new KeyNotFoundException();
                }

                // Remove this key from the index.
                var updatedIndex = result.Value.CopyAndRemove(key);
                if (updatedIndex.Length > 0)
                {
                    // Update the index.
                    await _index.SetAsync(tx, word, updatedIndex, timeout, token).ConfigureAwait(false);
                }
                else
                {
                    // Remove the index completely if this was the last key with this filter value.
                    await _index.TryRemoveAsync(tx, word, timeout, token).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 Task UpdateMetadataAsync(ITransaction tx, string key, ReliableListMetaData updatedMetadata) =>
 _metadataStore.SetAsync(tx, MakeMetadataKey(key), updatedMetadata);