/// <summary>Immediately execute an async action on each element of an async sequence</summary> /// <typeparam name="TSource">Type of elements of the async sequence</typeparam> /// <param name="source">Source async sequence</param> /// <param name="mode">Expected execution mode of the query</param> /// <param name="action">Asynchronous action to perform on each element as it arrives</param> /// <param name="ct">Cancellation token that can be used to cancel the operation</param> /// <returns>Number of items that have been processed</returns> internal static async Task <long> Run <TSource>( [NotNull] IFdbAsyncEnumerable <TSource> source, FdbAsyncMode mode, [NotNull] Func <TSource, Task> action, CancellationToken ct) { ct.ThrowIfCancellationRequested(); //note: we should not use "ConfigureAwait(false)" here because we would like to execute the action in the original synchronization context if possible... long count = 0; using (var iterator = source.GetEnumerator(mode)) { Contract.Assert(iterator != null, "The underlying sequence returned a null async iterator"); while (await iterator.MoveNext(ct)) { ct.ThrowIfCancellationRequested(); await action(iterator.Current); ++count; } } return(count); }
public IFdbAsyncEnumerator <TResult> GetEnumerator(FdbAsyncMode _) { IEnumerator <TSource> inner = null; try { inner = this.Source.GetEnumerator(); Contract.Assert(inner != null, "The underlying sequence returned an empty enumerator"); var outer = this.Factory(inner); if (outer == null) { throw new InvalidOperationException("The async factory returned en empty enumerator"); } return(outer); } catch (Exception) { //make sure that the inner iterator gets disposed if something went wrong if (inner != null) { inner.Dispose(); } throw; } }
public IFdbAsyncEnumerator <TResult> GetEnumerator(FdbAsyncMode mode) { // reuse the same instance the first time if (Interlocked.CompareExchange(ref m_state, STATE_INIT, STATE_SEQ) == STATE_SEQ) { m_mode = mode; return(this); } // create a new one var iter = Clone(); iter.m_mode = mode; Volatile.Write(ref iter.m_state, STATE_INIT); return(iter); }
public IFdbAsyncEnumerator <TSource> GetEnumerator(FdbAsyncMode mode = FdbAsyncMode.Default) { var sorter = GetEnumerableSorter(null); var enumerator = default(IFdbAsyncEnumerator <TSource>); try { enumerator = m_source.GetEnumerator(mode); return(new OrderedEnumerator <TSource>(enumerator, sorter)); } catch (Exception) { if (enumerator != null) { enumerator.Dispose(); } throw; } }
public IFdbAsyncEnumerator <TSource> GetEnumerator(FdbAsyncMode mode) { return(this); }
public IFdbAsyncEnumerator <TElement> GetEnumerator(FdbAsyncMode mode = FdbAsyncMode.Default) { return(new SingletonSequence <TElement>(m_lambda)); }
public IFdbAsyncEnumerator <T> GetEnumerator(FdbAsyncMode mode) { return(new ResultIterator(this, this.Transaction, this.Transform).GetEnumerator(mode)); }
public IFdbAsyncEnumerator <T> GetEnumerator(FdbAsyncMode _) { return(m_factory(m_state)); }
internal static IFdbAsyncEnumerator <T> GetEnumerator([NotNull] FdbAsyncSequenceQuery <T> sequence, FdbAsyncMode mode) { var generator = sequence.CompileSequence(sequence.Expression); if (sequence.Transaction != null) { return(generator(sequence.Transaction).GetEnumerator(mode)); } //BUGBUG: how do we get a CancellationToken without a transaction? var ct = CancellationToken.None; IFdbTransaction trans = null; IFdbAsyncEnumerator <T> iterator = null; bool success = true; try { trans = sequence.Database.BeginTransaction(ct); iterator = generator(trans).GetEnumerator(); return(new TransactionIterator(trans, iterator)); } catch (Exception) { success = false; throw; } finally { if (!success) { if (iterator != null) { iterator.Dispose(); } if (trans != null) { trans.Dispose(); } } } }
/// <summary>Return an async sequence that will return the results of this query</summary> public IFdbAsyncEnumerable <T> ToEnumerable(FdbAsyncMode mode = FdbAsyncMode.Default) { return(FdbAsyncEnumerable.Create((_) => GetEnumerator(this, mode))); }