예제 #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ValueBatchesEnumerator{TSource,TValue}"/> class.
 /// </summary>
 /// <param name="source">
 /// The source.
 /// </param>
 /// <param name="policy">
 /// The policy.
 /// </param>
 /// <param name="batchSize">
 /// The batch Size.
 /// </param>
 /// <param name="valueSelector">
 /// The value Selector.
 /// </param>
 /// <param name="comparer">
 /// The comparer.
 /// </param>
 public ValueBatchesEnumerator(IAsyncEnumerable <TSource> source, IMaterializationPolicy policy, long batchSize, Func <TSource, TValue> valueSelector, IComparer <TValue> comparer)
 {
     this.source        = source;
     this.policy        = policy;
     this.batchSize     = batchSize;
     this.valueSelector = valueSelector;
     this.comparer      = comparer;
 }
예제 #2
0
        /// <summary>
        /// Resets all fields to null, and sets the state to 'disposed'.
        /// </summary>
        /// <param name="disposing">
        /// The disposing.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            this.state = 3;

            this.source       = null;
            this.policy       = null;
            this.materialized = null;
            this.enumerator   = null;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="FactoryEnumerable{TElement}"/> class.
 /// </summary>
 /// <param name="policy">
 /// The policy.
 /// </param>
 /// <param name="factory">
 /// The factory.
 /// </param>
 /// <param name="name">
 /// The name of the enumerator Used for debugging.
 /// </param>
 public FactoryEnumerable(IMaterializationPolicy policy, Func <IAsyncEnumerator <TElement> > factory, string name)
 {
     this.Policy  = policy;
     this.factory = factory;
     this.name    = name;
 }
 internal static IAsyncEnumerable <T> CreateAsyncEnumerable <T>(this IMaterializationPolicy policy, [NotNull] Expression <Func <IAsyncEnumerator <T> > > factory)
 {
     return(new FactoryEnumerable <T>(policy, factory.Compile(), factory.Body.Type.Name));
 }
 /// <summary>
 /// Converts an enumerable to an <see cref="IAsyncEnumerable{T}"/>.
 /// </summary>
 /// <param name="policy">
 /// The policy.
 /// </param>
 /// <param name="enumerable">
 /// The enumerable.
 /// </param>
 /// <typeparam name="T">
 /// The type of the items.
 /// </typeparam>
 /// <returns>
 /// The <see cref="IAsyncEnumerable{T}"/>.
 /// </returns>
 public static IAsyncEnumerable <T> ToAsyncEnumerable <T>(this IMaterializationPolicy policy, IEnumerable <T> enumerable)
 {
     return(policy.CreateAsyncEnumerableAndRunOnce(() => Task.FromResult(enumerable)));
 }
        /// <summary>
        /// Retrieves all elements from the <see cref="IAsyncEnumerable{T}"/> from the source and stores them in a persistent
        ///     <see cref="IAsyncReadOnlyCollection{T}"/>.
        ///     This means that it can be enumerated multiple times without having to access the source over and over again.
        /// </summary>
        /// <typeparam name="T">
        /// The type of the items.
        /// </typeparam>
        /// <param name="policy">
        /// The materialization policy.
        /// </param>
        /// <param name="source">
        /// The <see cref="IAsyncEnumerable{T}"/> to materialize.
        /// </param>
        /// <returns>
        /// If <paramref name="source"/> was already a <see cref="IAsyncReadOnlyCollection{T}"/>, <paramref name="source"/>,
        ///     otherwise a new
        ///     <see cref="IAsyncReadOnlyCollection{T}"/> containing the elements in the sequence.
        /// </returns>
        public static async Task <IAsyncReadOnlyCollection <T> > MaterializeAsync <T>(this IMaterializationPolicy policy, [NotNull] IAsyncEnumerable <T> source)
        {
            if (source.Policy == policy && source is IAsyncReadOnlyCollection <T> )
            {
                return((IAsyncReadOnlyCollection <T>)source);
            }

            var builder = policy.CreateBuilder <T>();

            using (var enumerator = source.GetAsyncEnumerator())
            {
                if (builder is ISynchronousAsyncEnumerableBuilder <T> syncBuilder)
                {
                    do
                    {
                        syncBuilder.Add(enumerator.CurrentBatchToEnumerable());
                    }while (!enumerator.IsSynchronous && await enumerator.NextBatchAsync().ConfigureAwait(false));
                }
                else
                {
                    do
                    {
                        await builder.AddAsync(enumerator.CurrentBatchToEnumerable());
                    }while (!enumerator.IsSynchronous && await enumerator.NextBatchAsync().ConfigureAwait(false));
                }
            }

            return(await builder.BuildAsync().ConfigureAwait(false));
        }
 /// <summary>
 /// Creates an empty async enumerable.
 /// </summary>
 /// <param name="policy">
 /// The policy.
 /// </param>
 /// <typeparam name="T">
 /// The type of the elements.
 /// </typeparam>
 /// <returns>
 /// The <see cref="IAsyncEnumerable{T}"/>.
 /// </returns>
 public static IAsyncEnumerable <T> CreateEmptyAsyncEnumerable <T>(this IMaterializationPolicy policy)
 {
     return(policy.CreateAsyncEnumerable(() => new EmptyEnumerator <T>()));
 }
 /// <summary>
 /// Creates an <see cref="IAsyncEnumerable{T}"/> by calling <paramref name="itemGenerator"/> once.
 /// </summary>
 /// <param name="policy">
 /// The policy.
 /// </param>
 /// <param name="initialize">
 /// The initialize.
 /// </param>
 /// <param name="itemGenerator">
 /// Returns the items.
 /// </param>
 /// <param name="dispose">
 /// Called when the enumerator is disposed.
 /// </param>
 /// <typeparam name="T">
 /// The type of the items.
 /// </typeparam>
 /// <typeparam name="TState">
 /// The type of the enumerator state.
 /// </typeparam>
 /// <returns>
 /// The <see cref="IAsyncEnumerable{T}"/>.
 /// </returns>
 public static IAsyncEnumerable <T> CreateAsyncEnumerableAndRunOnce <T, TState>(this IMaterializationPolicy policy, Func <Task <TState> > initialize, Func <TState, IEnumerable <T> > itemGenerator, Action <TState> dispose)
     where T : class
 => policy.CreateAsyncEnumerable(
     async() => new SingleRunContext <TState>(await initialize()),
     src => Task.Run(() => src.NumberOfRuns++ == 0 ? itemGenerator(src.Context) : null),
     src => dispose(src == null ? default(TState) : src.Context));
 /// <summary>
 /// Creates an asynchronous enumerable from an <see cref="IEnumerable{T}"/>.
 /// </summary>
 /// <param name="policy">
 /// The policy.
 /// </param>
 /// <param name="enumerable">
 /// The enumerable.
 /// </param>
 /// <typeparam name="T">
 /// The type of the items.
 /// </typeparam>
 /// <returns>
 /// The <see cref="IAsyncEnumerable{T}"/>.
 /// </returns>
 public static IAsyncEnumerable <T> CreateAsyncEnumerable <T>(this IMaterializationPolicy policy, IEnumerable <T> enumerable)
 {
     return(policy.CreateAsyncEnumerable(() => new EnumerableEnumerator <T>(enumerable)));
 }
 /// <summary>
 /// Creates an <see cref="IAsyncEnumerable{T}"/> from a generator.
 /// </summary>
 /// <param name="policy">
 /// The policy.
 /// </param>
 /// <param name="generateItem">
 /// The generate Item.
 /// </param>
 /// <param name="dispose">
 /// Function to call when disposing the generator.
 /// </param>
 /// <typeparam name="T">
 /// The type of the items.
 /// </typeparam>
 /// <typeparam name="TState">
 /// The type of the enumerator state.
 /// </typeparam>
 /// <returns>
 /// The <see cref="IAsyncEnumerable{T}"/>.
 /// </returns>
 public static IAsyncEnumerable <T> CreateAsyncEnumerable <T, TState>(this IMaterializationPolicy policy, Func <TState, Task <T> > generateItem, [CanBeNull] Action <TState> dispose = null)
     where TState : new()
     where T : class
 => policy.CreateAsyncEnumerable(() => new GeneratorEnumerable <T, TState>(() => Task.FromResult(new TState()), context => MaterializationPolicyExtensions.ToEnumerableAsync(generateItem(context)), dispose));
 /// <summary>
 /// Creates an <see cref="IAsyncEnumerable{T}"/> from a generator.
 /// </summary>
 /// <param name="policy">
 /// The policy.
 /// </param>
 /// <param name="initialize">
 /// Initializes the enumerable, and returns the context.
 /// </param>
 /// <param name="generateItem">
 /// Returns an item or <c>null</c> when no more items are available.
 /// </param>
 /// <param name="dispose">
 /// Function to call when disposing the generator.
 /// </param>
 /// <typeparam name="T">
 /// The type of the items.
 /// </typeparam>
 /// <typeparam name="TState">
 /// The type of the enumerator state.
 /// </typeparam>
 /// <returns>
 /// The <see cref="IAsyncEnumerable{T}"/>.
 /// </returns>
 public static IAsyncEnumerable <T> CreateAsyncEnumerable <T, TState>(this IMaterializationPolicy policy, Func <Task <TState> > initialize, Func <TState, Task <T> > generateItem, [CanBeNull] Action <TState> dispose = null)
     where T : class
 => policy.CreateAsyncEnumerable(initialize, context => MaterializationPolicyExtensions.ToEnumerableAsync(generateItem(context)), dispose);
 /// <summary>
 /// Creates an <see cref="IAsyncEnumerable{T}"/> from a generator.
 /// </summary>
 /// <param name="policy">
 /// The policy.
 /// </param>
 /// <param name="generateItems">
 /// Returns a batch of items or <c>null</c> when no more batches are available.
 /// </param>
 /// <param name="dispose">
 /// Function to call when disposing the generator.
 /// </param>
 /// <typeparam name="T">
 /// The type of the items.
 /// </typeparam>
 /// <typeparam name="TState">
 /// The type of the enumerator state.
 /// </typeparam>
 /// <returns>
 /// The <see cref="IAsyncEnumerable{T}"/>.
 /// </returns>
 public static IAsyncEnumerable <T> CreateAsyncEnumerable <T, TState>(this IMaterializationPolicy policy, Func <TState, Task <IEnumerable <T> > > generateItems, [CanBeNull] Action <TState> dispose = null)
     where TState : new()
 => policy.CreateAsyncEnumerable(() => Task.FromResult(new TState()), generateItems, dispose);
예제 #13
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="InMemoryAsyncReadOnlyCollection{T}" /> class.
 /// </summary>
 /// <param name="policy">
 ///     The policy.
 /// </param>
 /// <param name="items">
 ///     The items.
 /// </param>
 public InMemoryAsyncReadOnlyCollection(IMaterializationPolicy policy, T[] items)
 {
     this.Policy = policy;
     this.Items  = items;
 }
예제 #14
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="InMemoryAsyncEnumerableBuilder{T}" /> class.
 /// </summary>
 /// <param name="policy">
 ///     The policy.
 /// </param>
 public InMemoryAsyncEnumerableBuilder(IMaterializationPolicy policy)
 {
     this.policy = policy;
 }
 /// <summary>
 /// Creates an <see cref="IAsyncEnumerable{T}"/> from a generator.
 /// </summary>
 /// <param name="policy">
 /// The policy.
 /// </param>
 /// <param name="initialize">
 /// Initializes the enumerable, and returns the context.
 /// </param>
 /// <param name="generateItems">
 /// Returns a batch of items or <c>null</c> when no more batches are available.
 /// </param>
 /// <param name="dispose">
 /// Function to call when disposing the generator.
 /// </param>
 /// <typeparam name="T">
 /// The type of the items.
 /// </typeparam>
 /// <typeparam name="TState">
 /// The type of the enumerator state.
 /// </typeparam>
 /// <returns>
 /// The <see cref="IAsyncEnumerable{T}"/>.
 /// </returns>
 public static IAsyncEnumerable <T> CreateAsyncEnumerable <T, TState>(this IMaterializationPolicy policy, Func <TState> initialize, Func <TState, Task <IEnumerable <T> > > generateItems, [CanBeNull] Action <TState> dispose = null)
 => policy.CreateAsyncEnumerable(() => Task.Run(initialize), generateItems, dispose);
 /// <summary>
 /// Creates an <see cref="IAsyncEnumerable{T}"/> from a generator.
 /// </summary>
 /// <param name="policy">
 /// The policy.
 /// </param>
 /// <param name="factory">
 /// The factory.
 /// </param>
 /// <typeparam name="T">
 /// The type of the items.
 /// </typeparam>
 /// <returns>
 /// The <see cref="IAsyncEnumerable{T}"/>.
 /// </returns>
 public static IAsyncEnumerable <T> CreateAsyncEnumerable <T>(this IMaterializationPolicy policy, Func <Task <IAsyncEnumerable <T> > > factory)
 => policy.CreateAsyncEnumerable(() => new FactoryEnumerator <T>(factory));
 /// <summary>
 /// Creates an <see cref="IAsyncEnumerable{T}"/> by calling <paramref name="itemGenerator"/> once.
 /// </summary>
 /// <param name="policy">
 /// The policy.
 /// </param>
 /// <param name="itemGenerator">
 /// Returns the items.
 /// </param>
 /// <param name="dispose">
 /// Called when the enumerator is disposed.
 /// </param>
 /// <typeparam name="T">
 /// The type of the items.
 /// </typeparam>
 /// <returns>
 /// The <see cref="IAsyncEnumerable{T}"/>.
 /// </returns>
 public static IAsyncEnumerable <T> CreateAsyncEnumerableAndRunOnce <T>(this IMaterializationPolicy policy, Func <Task <IEnumerable <T> > > itemGenerator, Action dispose)
     where T : class
 => policy.CreateAsyncEnumerable <T, SingleRunContext <object> >(src => src.NumberOfRuns++ == 0 ? itemGenerator() : null, src => dispose());