3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
8 // CountAggregationOperator.cs
10 // <OWNER>Microsoft</OWNER>
12 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
14 using System.Collections.Generic;
15 using System.Diagnostics.Contracts;
16 using System.Threading;
18 using System.Core; // for System.Core.SR
21 namespace System.Linq.Parallel
24 /// An inlined count aggregation and its enumerator.
26 /// <typeparam name="TSource"></typeparam>
27 internal sealed class LongCountAggregationOperator<TSource> : InlinedAggregationOperator<TSource, long, long>
30 //---------------------------------------------------------------------------------------
31 // Constructs a new instance of the operator.
34 internal LongCountAggregationOperator(IEnumerable<TSource> child) : base(child)
38 //---------------------------------------------------------------------------------------
39 // Executes the entire query tree, and aggregates the intermediate results into the
40 // final result based on the binary operators and final reduction.
43 // The single result of aggregation.
46 protected override long InternalAggregate(ref Exception singularExceptionToThrow)
48 // Because the final reduction is typically much cheaper than the intermediate
49 // reductions over the individual partitions, and because each parallel partition
50 // will do a lot of work to produce a single output element, we prefer to turn off
51 // pipelining, and process the final reductions serially.
52 using (IEnumerator<long> enumerator = GetEnumerator(ParallelMergeOptions.FullyBuffered, true))
54 // We just reduce the elements in each output partition.
56 while (enumerator.MoveNext())
60 count += enumerator.Current;
68 //---------------------------------------------------------------------------------------
69 // Creates an enumerator that is used internally for the final aggregation step.
72 protected override QueryOperatorEnumerator<long, int> CreateEnumerator<TKey>(
73 int index, int count, QueryOperatorEnumerator<TSource, TKey> source, object sharedData, CancellationToken cancellationToken)
75 return new LongCountAggregationOperatorEnumerator<TKey>(source, index, cancellationToken);
78 //---------------------------------------------------------------------------------------
79 // This enumerator type encapsulates the intermediary aggregation over the underlying
80 // (possibly partitioned) data source.
83 private class LongCountAggregationOperatorEnumerator<TKey> : InlinedAggregationOperatorEnumerator<long>
85 private readonly QueryOperatorEnumerator<TSource, TKey> m_source; // The source data.
87 //---------------------------------------------------------------------------------------
88 // Instantiates a new aggregation operator.
91 internal LongCountAggregationOperatorEnumerator(QueryOperatorEnumerator<TSource, TKey> source, int partitionIndex,
92 CancellationToken cancellationToken) :
93 base(partitionIndex, cancellationToken)
95 Contract.Assert(source != null);
99 //---------------------------------------------------------------------------------------
100 // Counts the elements in the underlying data source, walking the entire thing the first
101 // time MoveNext is called on this object.
104 protected override bool MoveNextCore(ref long currentElement)
106 TSource elementUnused = default(TSource);
107 TKey keyUnused = default(TKey);
109 QueryOperatorEnumerator<TSource, TKey> source = m_source;
110 if (source.MoveNext(ref elementUnused, ref keyUnused))
112 // We just scroll through the enumerator and keep a running count.
117 if ((i++ & CancellationState.POLL_INTERVAL) == 0)
118 CancellationState.ThrowIfCanceled(m_cancellationToken);
125 while (source.MoveNext(ref elementUnused, ref keyUnused));
127 currentElement = count;
134 //---------------------------------------------------------------------------------------
135 // Dispose of resources associated with the underlying enumerator.
138 protected override void Dispose(bool disposing)
140 Contract.Assert(m_source != null);