3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
8 // LongSumAggregationOperator.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 sum aggregation and its enumerator, for longs.
26 internal sealed class LongSumAggregationOperator : InlinedAggregationOperator<long, long, long>
29 //---------------------------------------------------------------------------------------
30 // Constructs a new instance of a sum associative operator.
33 internal LongSumAggregationOperator(IEnumerable<long> child) : base(child)
37 //---------------------------------------------------------------------------------------
38 // Executes the entire query tree, and aggregates the intermediate results into the
39 // final result based on the binary operators and final reduction.
42 // The single result of aggregation.
45 protected override long InternalAggregate(ref Exception singularExceptionToThrow)
47 // Because the final reduction is typically much cheaper than the intermediate
48 // reductions over the individual partitions, and because each parallel partition
49 // will do a lot of work to produce a single output element, we prefer to turn off
50 // pipelining, and process the final reductions serially.
51 using (IEnumerator<long> enumerator = GetEnumerator(ParallelMergeOptions.FullyBuffered, true))
53 // We just reduce the elements in each output partition.
55 while (enumerator.MoveNext())
59 sum += enumerator.Current;
67 //---------------------------------------------------------------------------------------
68 // Creates an enumerator that is used internally for the final aggregation step.
71 protected override QueryOperatorEnumerator<long,int> CreateEnumerator<TKey>(
72 int index, int count, QueryOperatorEnumerator<long, TKey> source, object sharedData, CancellationToken cancellationToken)
74 return new LongSumAggregationOperatorEnumerator<TKey>(source, index, cancellationToken);
77 //---------------------------------------------------------------------------------------
78 // This enumerator type encapsulates the intermediary aggregation over the underlying
79 // (possibly partitioned) data source.
82 private class LongSumAggregationOperatorEnumerator<TKey> : InlinedAggregationOperatorEnumerator<long>
84 private readonly QueryOperatorEnumerator<long, TKey> m_source; // The source data.
86 //---------------------------------------------------------------------------------------
87 // Instantiates a new aggregation operator.
90 internal LongSumAggregationOperatorEnumerator(QueryOperatorEnumerator<long, TKey> source, int partitionIndex,
91 CancellationToken cancellationToken) :
92 base(partitionIndex, cancellationToken)
94 Contract.Assert(source != null);
98 //---------------------------------------------------------------------------------------
99 // Tallies up the sum of the underlying data source, walking the entire thing the first
100 // time MoveNext is called on this object.
103 protected override bool MoveNextCore(ref long currentElement)
105 long element = default(long);
106 TKey keyUnused = default(TKey);
108 QueryOperatorEnumerator<long, TKey> source = m_source;
109 if (source.MoveNext(ref element, ref keyUnused))
111 // We just scroll through the enumerator and accumulate the sum.
116 if ((i++ & CancellationState.POLL_INTERVAL) == 0)
117 CancellationState.ThrowIfCanceled(m_cancellationToken);
124 while (source.MoveNext(ref element, ref keyUnused));
126 // The sum has been calculated. Now just return.
127 currentElement = tempSum;
134 //---------------------------------------------------------------------------------------
135 // Dispose of resources associated with the underlying enumerator.
138 protected override void Dispose(bool disposing)
140 Contract.Assert(m_source != null);