<commit_before>﻿using System.Threading.Tasks;

using Bytes2you.Validation;

using Parser.LogFileReader.Contracts;
using Parser.LogFileReader.Factories;

namespace Parser.LogFileReader.Engines
{
    public class LogFileReaderEngine : ILogFileReaderEngine
    {
        private const int AutoResetEventWaitTimeoutInMiliseconds = 250;

        private readonly ICommandParsingStrategy commandParsingStrategy;
        private readonly ICommandUtilizationStrategy commandUtilizationStrategy;
        private readonly ILogFilePathDiscoveryStrategy logFilePathDiscoveryStrategy;
        private readonly IFileReaderAutoResetEventFactory fileReaderAutoResetEventFactory;
        private readonly IFileReaderFileSystemWatcherFactory fileReaderFileSystemWatcherFactory;
        private readonly IFileReaderInputProviderFactory fileReaderInputProviderFactory;

        private bool isRunning = false;

        public LogFileReaderEngine(ICommandParsingStrategy commandParsingStrategy, ICommandUtilizationStrategy commandUtilizationStrategy, ILogFilePathDiscoveryStrategy logFilePathDiscoveryStrategy, IFileReaderAutoResetEventFactory fileReaderAutoResetEventFactory, IFileReaderFileSystemWatcherFactory fileReaderFileSystemWatcherFactory, IFileReaderInputProviderFactory fileReaderInputProviderFactory)
        {
            Guard.WhenArgument(commandParsingStrategy, nameof(ICommandParsingStrategy)).IsNull().Throw();
            Guard.WhenArgument(commandUtilizationStrategy, nameof(ICommandUtilizationStrategy)).IsNull().Throw();
            Guard.WhenArgument(logFilePathDiscoveryStrategy, nameof(ILogFilePathDiscoveryStrategy)).IsNull().Throw();
            Guard.WhenArgument(fileReaderAutoResetEventFactory, nameof(IFileReaderAutoResetEventFactory)).IsNull().Throw();
            Guard.WhenArgument(fileReaderFileSystemWatcherFactory, nameof(IFileReaderFileSystemWatcherFactory)).IsNull().Throw();
            Guard.WhenArgument(fileReaderInputProviderFactory, nameof(IFileReaderInputProviderFactory)).IsNull().Throw();

            this.commandParsingStrategy = commandParsingStrategy;
            this.commandUtilizationStrategy = commandUtilizationStrategy;
            this.logFilePathDiscoveryStrategy = logFilePathDiscoveryStrategy;
            this.fileReaderAutoResetEventFactory = fileReaderAutoResetEventFactory;
            this.fileReaderFileSystemWatcherFactory = fileReaderFileSystemWatcherFactory;
            this.fileReaderInputProviderFactory = fileReaderInputProviderFactory;
        }

        public Task StartAsync()
        {
            return Task.Run(() => this.Run());
        }

        public void Stop()
        {
            this.isRunning = false;
        }

        private void Run()
        {
            this.isRunning = true;

            var logFilePath = this.logFilePathDiscoveryStrategy.DiscoverLogFile();

            var autoResetEvent = this.fileReaderAutoResetEventFactory.CreateFileReaderAutoResetEvent(false);
            var fileSystemWatcher = this.fileReaderFileSystemWatcherFactory.CreateFileReaderFileSystemWatcher(logFilePath, true, autoResetEvent);

            using (var inputProvider = this.fileReaderInputProviderFactory.CreateFileReaderInputProvider(logFilePath))
            {
                var nextInputLine = string.Empty;
                while (this.isRunning)
                {
                    nextInputLine = inputProvider.ReadLine();
                    if (nextInputLine != null)
                    {
                        var nextParsedCommand = this.commandParsingStrategy.ParseCommand(nextInputLine);
                        this.commandUtilizationStrategy.UtilizeCommand(nextParsedCommand);
                    }
                    else
                    {
                        autoResetEvent.WaitOne(LogFileReaderEngine.AutoResetEventWaitTimeoutInMiliseconds);
                    }
                }
            }
        }
    }
}<commit_msg>added explicit Dispose invocation to engine<commit_after>﻿using System.Threading.Tasks;

using Bytes2you.Validation;

using Parser.LogFileReader.Contracts;
using Parser.LogFileReader.Factories;

namespace Parser.LogFileReader.Engines
{
    public class LogFileReaderEngine : ILogFileReaderEngine
    {
        private const int AutoResetEventWaitTimeoutInMiliseconds = 250;

        private readonly ICommandParsingStrategy commandParsingStrategy;
        private readonly ICommandUtilizationStrategy commandUtilizationStrategy;
        private readonly ILogFilePathDiscoveryStrategy logFilePathDiscoveryStrategy;
        private readonly IFileReaderAutoResetEventFactory fileReaderAutoResetEventFactory;
        private readonly IFileReaderFileSystemWatcherFactory fileReaderFileSystemWatcherFactory;
        private readonly IFileReaderInputProviderFactory fileReaderInputProviderFactory;

        private bool isRunning = false;

        public LogFileReaderEngine(ICommandParsingStrategy commandParsingStrategy, ICommandUtilizationStrategy commandUtilizationStrategy, ILogFilePathDiscoveryStrategy logFilePathDiscoveryStrategy, IFileReaderAutoResetEventFactory fileReaderAutoResetEventFactory, IFileReaderFileSystemWatcherFactory fileReaderFileSystemWatcherFactory, IFileReaderInputProviderFactory fileReaderInputProviderFactory)
        {
            Guard.WhenArgument(commandParsingStrategy, nameof(ICommandParsingStrategy)).IsNull().Throw();
            Guard.WhenArgument(commandUtilizationStrategy, nameof(ICommandUtilizationStrategy)).IsNull().Throw();
            Guard.WhenArgument(logFilePathDiscoveryStrategy, nameof(ILogFilePathDiscoveryStrategy)).IsNull().Throw();
            Guard.WhenArgument(fileReaderAutoResetEventFactory, nameof(IFileReaderAutoResetEventFactory)).IsNull().Throw();
            Guard.WhenArgument(fileReaderFileSystemWatcherFactory, nameof(IFileReaderFileSystemWatcherFactory)).IsNull().Throw();
            Guard.WhenArgument(fileReaderInputProviderFactory, nameof(IFileReaderInputProviderFactory)).IsNull().Throw();

            this.commandParsingStrategy = commandParsingStrategy;
            this.commandUtilizationStrategy = commandUtilizationStrategy;
            this.logFilePathDiscoveryStrategy = logFilePathDiscoveryStrategy;
            this.fileReaderAutoResetEventFactory = fileReaderAutoResetEventFactory;
            this.fileReaderFileSystemWatcherFactory = fileReaderFileSystemWatcherFactory;
            this.fileReaderInputProviderFactory = fileReaderInputProviderFactory;
        }

        public Task StartAsync()
        {
            return Task.Run(() => this.Run());
        }

        public void Stop()
        {
            this.isRunning = false;
        }

        private void Run()
        {
            this.isRunning = true;

            var logFilePath = this.logFilePathDiscoveryStrategy.DiscoverLogFile();

            var autoResetEvent = this.fileReaderAutoResetEventFactory.CreateFileReaderAutoResetEvent(false);
            var fileSystemWatcher = this.fileReaderFileSystemWatcherFactory.CreateFileReaderFileSystemWatcher(logFilePath, true, autoResetEvent);

            using (var inputProvider = this.fileReaderInputProviderFactory.CreateFileReaderInputProvider(logFilePath))
            {
                var nextInputLine = string.Empty;
                while (this.isRunning)
                {
                    nextInputLine = inputProvider.ReadLine();
                    if (nextInputLine != null)
                    {
                        var nextParsedCommand = this.commandParsingStrategy.ParseCommand(nextInputLine);
                        this.commandUtilizationStrategy.UtilizeCommand(nextParsedCommand);
                    }
                    else
                    {
                        autoResetEvent.WaitOne(LogFileReaderEngine.AutoResetEventWaitTimeoutInMiliseconds);
                    }
                }

                inputProvider.Dispose();
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.059046177138531414, 'fraction_numerical': 0.0010093363613424174, 'mean_word_length': 3.1743022643496577, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7058261', 'n_tokens_mistral': 1798, 'n_tokens_neox': 1759, 'n_words': 268}