public FdbDistinctAsyncIterator([NotNull] IFdbAsyncEnumerable <TSource> source, IEqualityComparer <TSource> comparer)
            : base(source)
        {
            Contract.Requires(comparer != null);

            m_comparer = comparer;
        }
예제 #2
0
        //TODO: also accept a Func<TSource, CT, Task<bool>> ?

        public FdbTakeWhileAsyncIterator([NotNull] IFdbAsyncEnumerable <TSource> source, [NotNull] Func <TSource, bool> condition)
            : base(source)
        {
            Contract.Requires(condition != null);

            m_condition = condition;
        }
        public FdbWhereAsyncIterator([NotNull] IFdbAsyncEnumerable <TSource> source, AsyncFilterExpression <TSource> filter)
            : base(source)
        {
            Contract.Requires(filter != null, "there can be only one kind of filter specified");

            m_filter = filter;
        }
        /// <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);
        }
        /// <summary>Helper async method to get the first 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="single">If true, the sequence must contain at most one element</param>
        /// <param name="orDefault">When the sequence is empty: If true then returns the default value for the type. Otherwise, throws an exception</param>
        /// <param name="ct">Cancellation token that can be used to cancel the operation</param>
        /// <returns>Value of the first element of the <param ref="source"/> sequence, or the default value, or an exception (depending on <paramref name="single"/> and <paramref name="orDefault"/></returns>
        internal static async Task <TSource> Head <TSource>(
            [NotNull] IFdbAsyncEnumerable <TSource> source,
            bool single,
            bool orDefault,
            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...

            using (var iterator = source.GetEnumerator(FdbAsyncMode.Head))
            {
                Contract.Assert(iterator != null, "The underlying sequence returned a null async iterator");

                if (await iterator.MoveNext(ct))
                {
                    TSource first = iterator.Current;
                    if (single)
                    {
                        if (await iterator.MoveNext(ct))
                        {
                            throw new InvalidOperationException("The sequence contained more than one element");
                        }
                    }
                    return(first);
                }
                if (!orDefault)
                {
                    throw new InvalidOperationException("The sequence was empty");
                }
                return(default(TSource));
            }
        }
 /// <summary>Create a new async sequence that will transform an inner async sequence</summary>
 /// <typeparam name="TSource">Type of elements of the inner async sequence</typeparam>
 /// <typeparam name="TResult">Type of elements of the outer async sequence</typeparam>
 /// <param name="source">Source async sequence that will be wrapped</param>
 /// <param name="factory">Factory method called when the outer sequence starts iterating. Must return an async enumerator</param>
 /// <returns>New async sequence</returns>
 internal static FdbAsyncSequence <TSource, TResult> Create <TSource, TResult>(
     IFdbAsyncEnumerable <TSource> source,
     Func <IFdbAsyncEnumerator <TSource>,
           IFdbAsyncEnumerator <TResult> > factory)
 {
     return(new FdbAsyncSequence <TSource, TResult>(source, factory));
 }
예제 #7
0
            protected OrderedSequence(IFdbAsyncEnumerable <TSource> source, bool descending, OrderedSequence <TSource> parent)
            {
                Contract.Requires(source != null);

                m_source     = source;
                m_descending = descending;
                m_parent     = parent;
            }
 internal static FdbWhereSelectAsyncIterator <TSource, TResult> Map <TSource, TResult>(
     [NotNull] IFdbAsyncEnumerable <TSource> source,
     [NotNull] AsyncTransformExpression <TSource, TResult> selector,
     int?limit = null, int?
     offset    = null)
 {
     return(new FdbWhereSelectAsyncIterator <TSource, TResult>(source, filter: null, transform: selector, limit: limit, offset: offset));
 }
        public FdbSelectManyAsyncIterator([NotNull] IFdbAsyncEnumerable <TSource> source, AsyncTransformExpression <TSource, IEnumerable <TResult> > selector)
            : base(source)
        {
            // Must have at least one, but not both
            Contract.Requires(selector != null);

            m_selector = selector;
        }
        /// <summary>Create a new batching iterator</summary>
        /// <param name="source">Source sequence of items that must be batched by waves</param>
        /// <param name="batchSize">Maximum size of a batch to return down the line</param>
        public FdbBatchingAsyncIterator(IFdbAsyncEnumerable <TInput> source, int batchSize)
            : base(source)
        {
            Contract.Requires(batchSize > 0);

            m_source    = source;
            m_batchSize = batchSize;
        }
예제 #11
0
        public static FdbQueryAsyncEnumerableExpression <T> Sequence <T>([NotNull] IFdbAsyncEnumerable <T> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            return(new FdbQueryAsyncEnumerableExpression <T>(source));
        }
예제 #12
0
            protected readonly OrderedSequence <TSource> m_parent;      // null if primary sort key

            public OrderedSequence(IFdbAsyncEnumerable <TSource> source, IComparer <TSource> comparer, bool descending, OrderedSequence <TSource> parent)
            {
                Contract.Requires(source != null);

                m_source     = source;
                m_descending = descending;
                m_comparer   = comparer ?? Comparer <TSource> .Default;
                m_parent     = parent;
            }
예제 #13
0
 /// <summary>Sequence the return only the elements of <paramref name="first"/> that are not in <paramref name="second"/></summary>
 /// <typeparam name="TResult">Type of the results of the query</typeparam>
 /// <param name="first">Fisrt query that contains the elements that could be in the result</param>
 /// <param name="second">Second query that contains the elements that cannot be in the result</param>
 /// <param name="comparer">Instance used to compare elements</param>
 /// <returns>Async query that returns only the elements that are in <paramref name="first"/>, and not in <paramref name="second"/></returns>
 public static IFdbAsyncEnumerable <TResult> Except <TResult>(this IFdbAsyncEnumerable <TResult> first, IFdbAsyncEnumerable <TResult> second, IComparer <TResult> comparer = null)
 {
     return(new FdbExceptIterator <TResult, TResult, TResult>(
                new[] { first, second },
                null,
                TaskHelpers.Cache <TResult> .Identity,
                TaskHelpers.Cache <TResult> .Identity,
                comparer
                ));
 }
예제 #14
0
 public static IFdbAsyncEnumerable <TResult> Intersect <TKey, TResult>(this IFdbAsyncEnumerable <TResult> first, IFdbAsyncEnumerable <TResult> second, Func <TResult, TKey> keySelector, IComparer <TKey> keyComparer = null)
 {
     return(new FdbIntersectIterator <TResult, TKey, TResult>(
                new[] { first, second },
                null,
                keySelector,
                TaskHelpers.Cache <TResult> .Identity,
                keyComparer
                ));
 }
 internal static FdbSelectManyAsyncIterator <TSource, TCollection, TResult> Flatten <TSource, TCollection, TResult>(
     [NotNull] IFdbAsyncEnumerable <TSource> source,
     [NotNull] AsyncTransformExpression <TSource, IEnumerable <TCollection> > collectionSelector,
     [NotNull] Func <TSource, TCollection, TResult> resultSelector)
 {
     return(new FdbSelectManyAsyncIterator <TSource, TCollection, TResult>(
                source,
                collectionSelector,
                resultSelector
                ));
 }
 public static IFdbAsyncEnumerable <TResult> Except <TResult>([NotNull] this IFdbAsyncEnumerable <TResult> first, [NotNull] IFdbAsyncEnumerable <TResult> second, IComparer <TResult> comparer = null)
 {
     Contract.NotNull(first, nameof(first));
     Contract.NotNull(second, nameof(second));
     return(new FdbExceptIterator <TResult, TResult, TResult>(
                new[] { first, second },
                null,
                (x) => x,
                (x) => x,
                comparer
                ));
 }
예제 #17
0
        public FdbParallelSelectAsyncIterator(
            [NotNull] IFdbAsyncEnumerable <TSource> source,
            [NotNull] Func <TSource, CancellationToken, Task <TResult> > taskSelector,
            [NotNull] FdbParallelQueryOptions options
            )
            : base(source)
        {
            Contract.Requires(taskSelector != null && options != null);

            m_taskSelector = taskSelector;
            m_options      = options;
        }
 public static IFdbAsyncEnumerable <TResult> Intersect <TKey, TResult>([NotNull] this IFdbAsyncEnumerable <TResult> first, IFdbAsyncEnumerable <TResult> second, Func <TResult, TKey> keySelector, IComparer <TKey> keyComparer = null)
 {
     Contract.NotNull(first, nameof(first));
     Contract.NotNull(second, nameof(second));
     return(new FdbIntersectIterator <TResult, TKey, TResult>(
                new[] { first, second },
                null,
                keySelector,
                (x) => x,
                keyComparer
                ));
 }
        public FdbWhereSelectAsyncIterator(
            [NotNull] IFdbAsyncEnumerable <TSource> source,
            AsyncFilterExpression <TSource> filter,
            AsyncTransformExpression <TSource, TResult> transform,
            int?limit,
            int?offset
            )
            : base(source)
        {
            Contract.Requires(transform != null);                       // must do at least something
            Contract.Requires((limit ?? 0) >= 0 && (offset ?? 0) >= 0); // bounds cannot be negative

            m_filter    = filter;
            m_transform = transform;
            m_limit     = limit;
            m_offset    = offset;
        }
예제 #20
0
 protected FdbAsyncFilterIterator([NotNull] IFdbAsyncEnumerable <TSource> source)
 {
     Contract.Requires(source != null);
     m_source = source;
 }
 /// <summary>Create a new batching iterator</summary>
 /// <param name="source">Source sequence of items that must be batched by waves</param>
 /// <param name="prefetchCount">Maximum size of a batch to return down the line</param>
 public FdbPrefetchingAsyncIterator(IFdbAsyncEnumerable <TInput> source, int prefetchCount)
     : base(source)
 {
     Contract.Requires(prefetchCount > 0);
     m_prefetchCount = prefetchCount;
 }
 internal static FdbTakeWhileAsyncIterator <TResult> Limit <TResult>(
     [NotNull] IFdbAsyncEnumerable <TResult> source,
     [NotNull] Func <TResult, bool> condition)
 {
     return(new FdbTakeWhileAsyncIterator <TResult>(source, condition));
 }
 internal static FdbWhereSelectAsyncIterator <TResult, TResult> Limit <TResult>(
     [NotNull] IFdbAsyncEnumerable <TResult> source,
     int limit)
 {
     return(new FdbWhereSelectAsyncIterator <TResult, TResult>(source, filter: null, transform: new AsyncTransformExpression <TResult, TResult>(TaskHelpers.Cache <TResult> .Identity), limit: limit, offset: null));
 }
 internal static FdbWhereAsyncIterator <TResult> Filter <TResult>(
     [NotNull] IFdbAsyncEnumerable <TResult> source,
     [NotNull] AsyncFilterExpression <TResult> filter)
 {
     return(new FdbWhereAsyncIterator <TResult>(source, filter));
 }
 internal static FdbSelectManyAsyncIterator <TSource, TResult> Flatten <TSource, TResult>(
     [NotNull] IFdbAsyncEnumerable <TSource> source,
     [NotNull] AsyncTransformExpression <TSource, IEnumerable <TResult> > selector)
 {
     return(new FdbSelectManyAsyncIterator <TSource, TResult>(source, selector));
 }
예제 #26
0
 internal FdbQueryAsyncEnumerableExpression(IFdbAsyncEnumerable <T> source)
 {
     Contract.Requires(source != null);
     this.Source = source;
 }
 public FdbObserverIterator(IFdbAsyncEnumerable <TSource> source, AsyncObserverExpression <TSource> observer)
     : base(source)
 {
     Contract.Requires(observer != null);
     m_observer = observer;
 }
예제 #28
0
 /// <summary>Create a new batching iterator</summary>
 /// <param name="source">Source sequence of items that must be batched by waves</param>
 /// <param name="maxWindowSize">Maximum size of a batch to return down the line</param>
 public FdbWindowingAsyncIterator(IFdbAsyncEnumerable <TInput> source, int maxWindowSize)
     : base(source)
 {
     Contract.Requires(maxWindowSize > 0);
     m_maxWindowSize = maxWindowSize;
 }
예제 #29
0
 public FdbAsyncSequence([NotNull] IFdbAsyncEnumerable <TSource> source, [NotNull] Func <IFdbAsyncEnumerator <TSource>, IFdbAsyncEnumerator <TResult> > factory)
 {
     Contract.Requires(source != null && factory != null);
     this.Source  = source;
     this.Factory = factory;
 }