/Rhino.Etl.Core/Operations/PartialProcessOperation.cs

http://github.com/ayende/rhino-etl · C# · 140 lines · 89 code · 10 blank · 41 comment · 0 complexity · b589a9b48a44ad4062af6f1aedd2b047 MD5 · raw file

  1. namespace Rhino.Etl.Core.Operations
  2. {
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Threading;
  7. /// <summary>
  8. /// A partial process that can take part in another process
  9. /// </summary>
  10. public class PartialProcessOperation : EtlProcessBase<PartialProcessOperation>, IOperation
  11. {
  12. private IPipelineExecuter pipelineExeuter;
  13. private readonly OperationStatistics statistics = new OperationStatistics();
  14. /// <summary>
  15. /// Occurs when all the rows has finished processing.
  16. /// </summary>
  17. public event Action<IOperation> OnFinishedProcessing
  18. {
  19. add
  20. {
  21. foreach (IOperation operation in operations)
  22. {
  23. operation.OnFinishedProcessing += value;
  24. }
  25. }
  26. remove
  27. {
  28. foreach (IOperation operation in operations)
  29. {
  30. operation.OnFinishedProcessing -= value;
  31. }
  32. }
  33. }
  34. /// <summary>
  35. /// Initializes the current instance
  36. /// </summary>
  37. /// <param name="pipelineExecuter">The current pipeline executer.</param>
  38. public void PrepareForExecution(IPipelineExecuter pipelineExecuter)
  39. {
  40. this.pipelineExeuter = pipelineExecuter;
  41. foreach (IOperation operation in operations)
  42. {
  43. operation.PrepareForExecution(pipelineExecuter);
  44. }
  45. Statistics.MarkStarted();
  46. }
  47. /// <summary>
  48. /// Gets the statistics for this operation
  49. /// </summary>
  50. /// <value>The statistics.</value>
  51. public OperationStatistics Statistics
  52. {
  53. get { return statistics; }
  54. }
  55. /// <summary>
  56. /// Occurs when a row is processed.
  57. /// </summary>
  58. public event Action<IOperation, Row> OnRowProcessed
  59. {
  60. add
  61. {
  62. foreach (IOperation operation in operations)
  63. {
  64. operation.OnRowProcessed += value;
  65. }
  66. }
  67. remove
  68. {
  69. foreach (IOperation operation in operations)
  70. {
  71. operation.OnRowProcessed -= value;
  72. }
  73. }
  74. }
  75. /// <summary>
  76. /// Executes this operation
  77. /// </summary>
  78. /// <param name="rows">The rows.</param>
  79. /// <returns></returns>
  80. public IEnumerable<Row> Execute(IEnumerable<Row> rows)
  81. {
  82. MergeLastOperationsToOperations();
  83. return pipelineExeuter.PipelineToEnumerable(operations, rows, enumerable => enumerable);
  84. }
  85. ///<summary>
  86. ///Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  87. ///</summary>
  88. ///<filterpriority>2</filterpriority>
  89. public void Dispose()
  90. {
  91. foreach (IOperation operation in operations)
  92. {
  93. operation.Dispose();
  94. }
  95. }
  96. /// <summary>
  97. /// Raises the row processed event
  98. /// </summary>
  99. /// <param name="dictionary">The dictionary.</param>
  100. void IOperation.RaiseRowProcessed(Row dictionary)
  101. {
  102. Statistics.MarkRowProcessed();
  103. // we don't have a real event here, so we ignore it
  104. // it will be handled by the children at any rate
  105. }
  106. /// <summary>
  107. /// Raises the finished processing event
  108. /// </summary>
  109. void IOperation.RaiseFinishedProcessing()
  110. {
  111. Statistics.MarkFinished();
  112. // we don't have a real event here, so we ignore it
  113. // it will be handled by the children at any rate
  114. }
  115. /// <summary>
  116. /// Gets all errors that occured when running this operation
  117. /// </summary>
  118. /// <returns></returns>
  119. public IEnumerable<Exception> GetAllErrors()
  120. {
  121. foreach (IOperation operation in operations)
  122. {
  123. foreach (Exception error in operation.GetAllErrors())
  124. {
  125. yield return error;
  126. }
  127. }
  128. }
  129. }
  130. }