PageRenderTime 14ms CodeModel.GetById 8ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/Rhino.Etl.Core/Operations/AbstractAggregationOperation.cs

http://github.com/ayende/rhino-etl
C# | 58 lines | 31 code | 4 blank | 23 comment | 2 complexity | 53298ff6a7d1ad1168320bf293e644e9 MD5 | raw file
 1namespace Rhino.Etl.Core.Operations
 2{
 3    using System.Collections.Generic;
 4
 5    /// <summary>
 6    /// An aggregation operation, handles all the backend stuff of the aggregation,
 7    /// leaving client code just the accumulation process
 8    /// </summary>
 9    public abstract class AbstractAggregationOperation : AbstractOperation
10    {
11        /// <summary>
12        /// Executes this operation
13        /// </summary>
14        /// <param name="rows">The rows.</param>
15        /// <returns></returns>
16        public override IEnumerable<Row> Execute(IEnumerable<Row> rows)
17        {
18            IDictionary<ObjectArrayKeys, Row> aggregations = new Dictionary<ObjectArrayKeys, Row>();
19            string[] groupBy = GetColumnsToGroupBy();
20            foreach (Row row in rows)
21            {
22                ObjectArrayKeys key = row.CreateKey(groupBy);
23                Row aggregate;
24                if (aggregations.TryGetValue(key, out aggregate) == false)
25                    aggregations[key] = aggregate = new Row();
26                Accumulate(row, aggregate);
27            }
28            foreach (Row row in aggregations.Values)
29            {
30                FinishAggregation(row);
31                yield return row;
32            }
33        }
34
35        /// <summary>
36        /// Allow a derived class to perform final processing on the
37        /// aggregate, before sending it downward in the pipeline.
38        /// </summary>
39        /// <param name="aggregate">The row.</param>
40        protected virtual void FinishAggregation(Row aggregate){}
41
42        /// <summary>
43        /// Accumulate the current row to the current aggregation
44        /// </summary>
45        /// <param name="row">The row.</param>
46        /// <param name="aggregate">The aggregate.</param>
47        protected abstract void Accumulate(Row row, Row aggregate);
48
49        /// <summary>
50        /// Gets the columns list to group each row by
51        /// </summary>
52        /// <value>The group by.</value>
53        protected virtual string[] GetColumnsToGroupBy()
54        {
55            return new string[0];
56        }
57    }
58}