/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

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