Пример #1
0
        public SearchFileNamesResult SearchFileNames(SearchParams searchParams)
        {
            var matchFunction = SearchPreProcessParams <FileName>(searchParams, MatchFileName, MatchFileRelativePath);

            if (matchFunction == null)
            {
                return(SearchFileNamesResult.Empty);
            }

            // taskCancellation is used to make sure we cancel previous tasks as fast
            // as possible to avoid using too many CPU resources if the caller keeps
            // asking us to search for things. Note that this assumes the caller is
            // only interested in the result of the *last* query, while the previous
            // queries will throw an OperationCanceled exception.
            _taskCancellation.CancelAll();

            var matches = _currentFileDatabase.FileNames
                          .AsParallel()
                          // We need the line below because of "Take" (.net 4.0 PLinq limitation)
                          .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
                          .WithCancellation(_taskCancellation.GetNewToken())
                          .Where(item => matchFunction(item))
                          .Take(searchParams.MaxResults)
                          .ToList();

            return(new SearchFileNamesResult {
                FileNames = matches,
                TotalCount = _currentFileDatabase.FileNames.Count
            });
        }
Пример #2
0
        public SearchFilePathsResult SearchFilePaths(SearchParams searchParams)
        {
            // taskCancellation is used to make sure we cancel previous tasks as fast
            // as possible to avoid using too many CPU resources if the caller keeps
            // asking us to search for things. Note that this assumes the caller is
            // only interested in the result of the *last* query, while the previous
            // queries will throw an OperationCanceled exception.
            _taskCancellation.CancelAll();

            var preProcessResult = PreProcessFileSystemNameSearch <FileName>(
                searchParams,
                MatchFileName,
                MatchFileRelativePath);

            if (preProcessResult == null)
            {
                return(SearchFilePathsResult.Empty);
            }

            using (preProcessResult) {
                var searchedFileCount = 0;
                var matches           = _currentFileDatabase.FileNames
                                        .AsParallel()
                                        // We need the line below because of "Take" (.net 4.0 PLinq
                                        // limitation)
                                        .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
                                        .WithCancellation(_taskCancellation.GetNewToken())
                                        .Where(
                    item => {
                    if (!searchParams.IncludeSymLinks)
                    {
                        if (_currentFileDatabase.IsContainedInSymLink(item))
                        {
                            return(false);
                        }
                    }
                    Interlocked.Increment(ref searchedFileCount);
                    return(preProcessResult.Matcher(item));
                })
                                        .Take(searchParams.MaxResults)
                                        .ToList();

                return(new SearchFilePathsResult {
                    FileNames = matches,
                    TotalCount = searchedFileCount
                });
            }
        }
Пример #3
0
        private void SearchFilesPaths(string searchPattern, bool immediate)
        {
            var processedSearchPattern = PreproccessFilePathSearchString(searchPattern);

            // Cancel all previously running tasks
            _taskCancellation.CancelAll();
            var cancellationToken = _taskCancellation.GetNewToken();

            var id         = Interlocked.Increment(ref _operationSequenceId);
            var progressId = string.Format("{0}-{1}", OperationsIds.SearchFilePaths, id);
            var sw         = new Stopwatch();
            var request    = new DispatchThreadServerRequest {
                // Note: Having a single ID for all searches ensures previous search
                // requests are superseeded.
                Id      = "MetaSearch",
                Request = new SearchFilePathsRequest {
                    SearchParams = new SearchParams {
                        SearchString    = processedSearchPattern,
                        MaxResults      = 200, // TM_TODO
                        MatchCase       = false,
                        MatchWholeWord  = false,
                        IncludeSymLinks = true,
                        UseRe2Engine    = true,
                        Regex           = false,
                    }
                },
                Delay = TimeSpan.FromMilliseconds(immediate ? 0 : GlobalSettings.AutoSearchDelayMsec), OnThreadPoolSend = () => { sw.Start(); }, OnThreadPoolReceive = () => { sw.Stop(); }, OnDispatchThreadSuccess = typedResponse => {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return;
                    }
                    var response = ((SearchFilePathsResponse)typedResponse);
                    var msg      = string.Format("Found {0:n0} path(s) among {1:n0} ({2:0.00} seconds) matching pattern \"{3}\"",
                                                 response.HitCount,
                                                 response.TotalCount,
                                                 sw.Elapsed.TotalSeconds,
                                                 processedSearchPattern);
                    var viewModel = CreateSearchFilePathsResult(response.SearchResult, msg, "", true);
                    ViewModel.UpdateFileList(viewModel);
                    _control.UpdateSelection();
                }, OnDispatchThreadError = errorResponse => {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return;
                    }
                }
            };

            _dispatchThreadServerRequestExecutor.Post(request);
        }
Пример #4
0
        private void SearchWorker(SearchWorkerParams workerParams)
        {
            // Cancel all previously running tasks
            _taskCancellation.CancelAll();
            var cancellationToken = _taskCancellation.GetNewToken();

            var id         = Interlocked.Increment(ref _operationSequenceId);
            var progressId = string.Format("{0}-{1}", workerParams.OperationName, id);
            var sw         = new Stopwatch();
            var request    = new DispatchThreadServerRequest {
                // Note: Having a single ID for all searches ensures previous search
                // requests are superseeded.
                Id               = "MetaSearch",
                Request          = workerParams.TypedRequest,
                Delay            = workerParams.Delay,
                OnThreadPoolSend = () => {
                    sw.Start();
                    _progressBarTracker.Start(progressId, workerParams.HintText);
                },
                OnThreadPoolReceive = () => {
                    sw.Stop();
                    _progressBarTracker.Stop(progressId);
                },
                OnDispatchThreadSuccess = typedResponse => {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return;
                    }
                    workerParams.ProcessResponse(typedResponse, sw);
                },
                OnDispatchThreadError = errorResponse => {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return;
                    }
                    workerParams.ProcessError(errorResponse, sw);
                }
            };

            _dispatchThreadServerRequestExecutor.Post(request);
        }