1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
7 // CountAggregationOperator.cs
9 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
11 using System.Collections.Generic;
12 using System.Diagnostics;
13 using System.Threading;
15 namespace System.Linq.Parallel
18 /// An inlined count aggregation and its enumerator.
20 /// <typeparam name="TSource"></typeparam>
21 internal sealed class LongCountAggregationOperator<TSource> : InlinedAggregationOperator<TSource, long, long>
23 //---------------------------------------------------------------------------------------
24 // Constructs a new instance of the operator.
27 internal LongCountAggregationOperator(IEnumerable<TSource> child) : base(child)
31 //---------------------------------------------------------------------------------------
32 // Executes the entire query tree, and aggregates the intermediate results into the
33 // final result based on the binary operators and final reduction.
36 // The single result of aggregation.
39 protected override long InternalAggregate(ref Exception singularExceptionToThrow)
41 // Because the final reduction is typically much cheaper than the intermediate
42 // reductions over the individual partitions, and because each parallel partition
43 // will do a lot of work to produce a single output element, we prefer to turn off
44 // pipelining, and process the final reductions serially.
45 using (IEnumerator<long> enumerator = GetEnumerator(ParallelMergeOptions.FullyBuffered, true))
47 // We just reduce the elements in each output partition.
49 while (enumerator.MoveNext())
53 count += enumerator.Current;
61 //---------------------------------------------------------------------------------------
62 // Creates an enumerator that is used internally for the final aggregation step.
65 protected override QueryOperatorEnumerator<long, int> CreateEnumerator<TKey>(
66 int index, int count, QueryOperatorEnumerator<TSource, TKey> source, object sharedData, CancellationToken cancellationToken)
68 return new LongCountAggregationOperatorEnumerator<TKey>(source, index, cancellationToken);
71 //---------------------------------------------------------------------------------------
72 // This enumerator type encapsulates the intermediary aggregation over the underlying
73 // (possibly partitioned) data source.
76 private class LongCountAggregationOperatorEnumerator<TKey> : InlinedAggregationOperatorEnumerator<long>
78 private readonly QueryOperatorEnumerator<TSource, TKey> _source; // The source data.
80 //---------------------------------------------------------------------------------------
81 // Instantiates a new aggregation operator.
84 internal LongCountAggregationOperatorEnumerator(QueryOperatorEnumerator<TSource, TKey> source, int partitionIndex,
85 CancellationToken cancellationToken) :
86 base(partitionIndex, cancellationToken)
88 Debug.Assert(source != null);
92 //---------------------------------------------------------------------------------------
93 // Counts the elements in the underlying data source, walking the entire thing the first
94 // time MoveNext is called on this object.
97 protected override bool MoveNextCore(ref long currentElement)
99 TSource elementUnused = default(TSource);
100 TKey keyUnused = default(TKey);
102 QueryOperatorEnumerator<TSource, TKey> source = _source;
103 if (source.MoveNext(ref elementUnused, ref keyUnused))
105 // We just scroll through the enumerator and keep a running count.
110 if ((i++ & CancellationState.POLL_INTERVAL) == 0)
111 CancellationState.ThrowIfCanceled(_cancellationToken);
118 while (source.MoveNext(ref elementUnused, ref keyUnused));
120 currentElement = count;
127 //---------------------------------------------------------------------------------------
128 // Dispose of resources associated with the underlying enumerator.
131 protected override void Dispose(bool disposing)
133 Debug.Assert(_source != null);