public override void LineIsProcessed(Guid resultId) { Log.Trace(string.Format("{0}: ({1}) is processed.", LogContext.LogType, resultId)); if (_lineInProcess.ResultId != resultId) { throw new InvalidOperationException("Result ids does not match."); } string filePath; _changedFiles.TryPeek(out filePath); if (_lineInProcess.FilePath != filePath) { throw new InvalidOperationException("File paths does not match."); } var positionKey = GetPositionKey(filePath); using (var fs = new TextFileLineReader(filePath, _encoding)) { fs.Position = LogContext.Storage.Get <long>(positionKey); if (_lineInProcess.Position >= fs.Length) { _changedFiles.TryDequeue(out filePath); } } LogContext.Storage.Insert(positionKey, _lineInProcess.Position); _lineInProcess = null; }
private void ReadLinesFromFile(string filePath, int limit) { try { using (var fs = new TextFileLineReader(filePath, _encoding)) { var originalPosition = GetPosition(filePath); fs.Position = originalPosition; while (fs.Position < fs.Length) { var lineResult = fs.ReadLine(); if (string.IsNullOrWhiteSpace(lineResult)) { continue; } var result = new Result(LogContext) { Line = lineResult }; result.MetaData[MetaDataKeys.FilePath] = filePath; result.Position = fs.Position; Log.Trace(string.Format("{0}: ({1}) from '{2}' at byte position {3}.", LogContext.LogType, result.Id, filePath, originalPosition)); Log.Trace(string.Format("{0}: ({1}) line '{2}' read.", LogContext.LogType, result.Id, lineResult)); _unprocessed.Enqueue(result); if (_unprocessed.Count >= limit) { break; } } } } catch (FileNotFoundException) { bool tmp; _files.TryRemove(filePath, out tmp); } }
public override Result GetLine() { while (true) { string filePath; while (!_changedFiles.TryPeek(out filePath)) { Thread.Sleep(TimeSpan.FromSeconds(1)); } using (var fs = new TextFileLineReader(filePath, _encoding)) { var originalPosition = LogContext.Storage.Get <long>(GetPositionKey(filePath)); fs.Position = originalPosition; while (fs.Position < fs.Length) { var lineResult = fs.ReadLine(); if (string.IsNullOrWhiteSpace(lineResult)) { continue; } var result = new Result { Line = lineResult }; result.MetaData[MetaDataKeys.FilePath] = filePath; Log.Trace(string.Format("{0}: ({1}) from '{2}' at byte position {3}.", LogContext.LogType, result.Id, filePath, originalPosition)); Log.Trace(string.Format("{0}: ({1}) line '{2}' read.", LogContext.LogType, result.Id, lineResult)); SetLineInProcess(result.Id, filePath, fs.Position); return(result); } _changedFiles.TryDequeue(out filePath); } } }
private void ReadLinesFromFile(string filePath, int limit) { try { using (var fs = new TextFileLineReader(filePath, _encoding)) { var originalPosition = GetPosition(filePath); fs.Position = originalPosition; while (fs.Position < fs.Length) { var lineResult = fs.ReadLine(); if (string.IsNullOrWhiteSpace(lineResult)) continue; var result = new Result(LogContext) {Line = lineResult}; result.MetaData[MetaDataKeys.FilePath] = filePath; result.Position = fs.Position; Log.Trace(string.Format("{0}: ({1}) from '{2}' at byte position {3}.", LogContext.LogType, result.Id, filePath, originalPosition)); Log.Trace(string.Format("{0}: ({1}) line '{2}' read.", LogContext.LogType, result.Id, lineResult)); _unprocessed.Enqueue(result); if (_unprocessed.Count >= limit) { break; } } } } catch (FileNotFoundException) { bool tmp; _files.TryRemove(filePath, out tmp); } }