Пример #1
0
        public async Task <IAsyncEnumerable <TKey> > CreateKeyEnumerableAsync(ITransaction tx, EnumerationMode enumerationMode, TimeSpan timeout, CancellationToken cancellationToken)
        {
            var keys = new System.Collections.Generic.List <TKey>();

            try
            {
                BeginTransaction(tx);
                foreach (var key in Dictionary.Keys)
                {
                    await LockManager.AcquireLock(tx.TransactionId, key, LockMode.Default, timeout, cancellationToken);

                    keys.Add(key);
                }

                if (enumerationMode == EnumerationMode.Ordered)
                {
                    keys.Sort();
                }

                IAsyncEnumerable <TKey> result = new MockAsyncEnumerable <TKey>(keys);
                return(result);
            }
            catch
            {
                foreach (var key in keys)
                {
                    LockManager.ReleaseLock(tx.TransactionId, key);
                }

                throw;
            }
        }
Пример #2
0
        public async Task <IAsyncEnumerable <System.Collections.Generic.KeyValuePair <TKey, TValue> > > CreateEnumerableAsync(ITransaction tx, Func <TKey, bool> filter, EnumerationMode enumerationMode)
        {
            var keys = new System.Collections.Generic.List <TKey>();

            try
            {
                BeginTransaction(tx);

                foreach (var key in Dictionary.Keys)
                {
                    if (filter == null || filter(key))
                    {
                        await LockManager.AcquireLock(tx.TransactionId, key, LockMode.Default);

                        keys.Add(key);
                    }
                }

                if (enumerationMode == EnumerationMode.Ordered)
                {
                    keys.Sort();
                }

                IAsyncEnumerable <System.Collections.Generic.KeyValuePair <TKey, TValue> > result = new MockAsyncEnumerable <System.Collections.Generic.KeyValuePair <TKey, TValue> >(keys.Select(k => new System.Collections.Generic.KeyValuePair <TKey, TValue>(k, Dictionary[k])));
                return(result);
            }
            catch
            {
                foreach (var key in keys)
                {
                    LockManager.ReleaseLock(tx.TransactionId, key);
                }

                throw;
            }
        }