PageRenderTime 243ms CodeModel.GetById 100ms app.highlight 5ms RepoModel.GetById 136ms app.codeStats 0ms

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