예제 #1
0
 public PipeRunner(int pipeNumber, Dictionary <string, ITransformation> transformationPipe, int errorsAllowed, ILogger logger, RowLogAction rowLogAction)
 {
     _transformationPipe = transformationPipe;
     _pipeNumber         = pipeNumber;
     _errorsAllowed      = errorsAllowed;
     _rowLogAction       = rowLogAction;
     _logger             = logger;
 }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="inputQueue"></param>
        /// <param name="errorCount"></param>
        /// <param name="ct"></param>
        /// <param name="logger"></param>
        /// <param name="rowLogAction"></param>
        public void Load(BlockingCollection <Dictionary <string, object> > inputQueue, ref int errorCount, CancellationToken ct, ILogger logger, RowLogAction rowLogAction)
        {
            using (CsvReader csv = new CsvReader(new StreamReader(_fileName), _hasHeader, _delimeter))
            {
                LookupFieldIndexFromName(csv);

                while (csv.ReadNextRecord() && !ct.IsCancellationRequested)
                {
                    try
                    {
                        var row = new Dictionary <string, object>();

                        foreach (var field in _fields)
                        {
                            try
                            {
                                row.Add(field.Map, field.Converter(csv[field.Index.Value]));
                            }
                            catch (Exception ex)
                            {
                                throw new FieldConversionException(string.Format("Invalid field conversion {0} ({1})", field.Map, field.Index.Value), ex);
                            }
                        }

                        row.Add("#row", csv.CurrentRecordIndex);

                        inputQueue.Add(row);
                    }
                    catch (Exception ex)
                    {
                        rowLogAction?.Invoke(false, false, csv.CurrentRecordIndex, ex.Message);

                        Interlocked.Increment(ref errorCount);
                        if (_errorsAllowed != -1 && errorCount >= _errorsAllowed)
                        {
                            throw (new ReaderException(string.Format("Number of Errors has Exceeded the limit {0}", _errorsAllowed)));
                        }
                    }
                }
            }
        }