示例#1
0
        public async Task <List <SearchResult> > Search(string path, bool nesting, string searchText)
        {
            Stopwatch getStopWatch = new Stopwatch();

            var files = _fileManager.GetFiles(path, nesting);

            PrintFindFile.Print(files);
            List <SearchResult> result = new List <SearchResult>();

            foreach (var file in files)
            {
                Data     data;
                FileOpen fileOpen = new FileOpen();
                Stream   stream;
                Data     prevData = null;
                using (var reader = _unityContainer.Resolve <IReader>())
                {
                    stream = fileOpen.Open(file);
                    reader.InitVariables(stream, SizeBufferReader, SizeBufferWritter);
                    while ((data = await reader.ReadAsync()) != null)
                    {
                        data.Path = file.Path;
                        if (prevData != null)
                        {
                            // Добавление конца текста из предыдущего чтения
                            data.Buffer = prevData.Buffer.Substring(prevData.Buffer.Length - (searchText.Length - 1)) + data.Buffer;

                            // Изменение позиции с учетом того что добавлен текст в начало файла
                            data.Position = data.Position - (searchText.Length - 1);
                        }

                        // Поиск подстроки в строке
                        getStopWatch.Start();
                        result.AddRange(_searcherMethod.Search(data, searchText));
                        getStopWatch.Stop();

                        if (prevData == null)
                        {
                            prevData = new Data();
                        }

                        prevData.Buffer = data.Buffer;
                    }
                }
            }

            ResultTime.queryListSearch.Enqueue(getStopWatch.ElapsedMilliseconds);
            return(result);
        }
        public async Task <List <SearchResult> > Search(string path, bool nesting, string searchText)
        {
            var token = new CancellationTokenSource();
            var tasks = new List <Task>();

            var files = _fileManager.GetFiles(path, nesting);

            PrintFindFile.Print(files);
            var reader = _unityContainer.Resolve <IReaderMulti>();

            reader.RegisterReadWithCounts(_unityContainer, new FileOpen());

            var buffer = _unityContainer.Resolve <IBuffer>();

            buffer.RegisterInterceptor(new BufferInterceptor(searchText));

            var searcher = _unityContainer.Resolve <ISearcherMethodDecorator>();

            var totalRunTimeRead = Stopwatch.StartNew();

            foreach (var file in files)
            {
                // Запускает чтение файла в отдельном потоке
                tasks.Add(Task.Run(() => reader.ReadAsync(file, _sizeBufferReader, _sizeBufferWritter)));
            }

            var watchAndCount = _unityContainer.Resolve <WatchAndCount>();

            // Запускает поиск
            var searchTask = Task.Run(() => searcher.SearchAsync(searchText));

            await Task.WhenAll(tasks);

            totalRunTimeRead.Stop();
            watchAndCount.TotalRunTimeRead = totalRunTimeRead.ElapsedMilliseconds;

            buffer.Stop();

            token.Cancel();

            var result = await searchTask;

            return(result);
        }