Exemplo n.º 1
0
        internal IEnumerable <string> GetAvailableFileFilters(IndexViewModel indexDirectory)
        {
            if (indexDirectory == null)
            {
                return(Enumerable.Empty <string>());
            }

            if (!LuceneHelper.IsValidIndexDirectory(indexDirectory.IndexDirectory))
            {
                return(Enumerable.Empty <string>());
            }

            var availableExtensions = new List <string>();

            using (var reader = IndexReader.Open(FSDirectory.Open(indexDirectory.IndexDirectory), true))
            {
                var termEnum = reader.Terms();
                while (termEnum.Next())
                {
                    var curTerm = termEnum.Term;
                    if (curTerm.Field != Constants.IndexFields.Extension)
                    {
                        continue;
                    }

                    if (!availableExtensions.Contains(curTerm.Text))
                    {
                        availableExtensions.Add("*" + curTerm.Text);
                    }
                }
            }

            return(availableExtensions);
        }
Exemplo n.º 2
0
        private static async void RunSearch(SearchViewModel searchView, string searchText)
        {
            if (string.IsNullOrEmpty(searchText) ||
                ApplicationView.CurrentIndexFile == null ||
                !LuceneHelper.IsValidIndexDirectory(ApplicationView.CurrentIndexFile.IndexDirectory))
            {
                return;
            }

            Guid opId = Guid.Empty;
            CancellationToken cancelToken;

            if (!ApplicationView.BeginOperation(StatusKind.Searching, out opId, out cancelToken))
            {
                return;
            }

            Action lazySearchFinishedAction = () => ApplicationView.EndOperation(opId);

            if (searchView.IsSearchingInResults)
            {
                await Task.Run(() => searchView.RunFileSearch(ApplicationView.CurrentIndexFile, searchText, searchView.FixedResultFiles, cancelToken, lazySearchFinishedAction), cancelToken);
            }
            else
            {
                await Task.Run(() => searchView.RunSearch(ApplicationView.CurrentIndexFile, searchText, cancelToken, lazySearchFinishedAction), cancelToken);
            }
        }
Exemplo n.º 3
0
        private static async Task <bool> LoadIndexSilent(string indexFile)
        {
            if (ApplicationView.CurrentIndexFile != null && ApplicationView.CurrentIndexFile.IndexFile == indexFile)
            {
                return(true);
            }

            Guid opId = Guid.Empty;

            if (!ApplicationView.BeginOperation(StatusKind.Loading, out opId))
            {
                return(false);
            }

            try
            {
                ApplicationView.CurrentIndexFile = IndexViewModel.Load(indexFile);
                if (ApplicationView.CurrentIndexFile == null)
                {
                    return(false);
                }

                bool validIndexDirectoryFound = await Task.Run <bool>(() => LuceneHelper.IsValidIndexDirectory(ApplicationView.CurrentIndexFile.IndexDirectory));

                if (!validIndexDirectoryFound)
                {
                    ApplicationView.CurrentIndexFile = null;
                    return(false);
                }

                ApplicationView.UpdateSearchFilter();
                ApplicationView.FileWatcher.WatchDirectories(ApplicationView.CurrentIndexFile.SourceDirectories);
            }
            finally
            {
                ApplicationView.EndOperation(opId);
            }

            RefreshIndexAtStartup(ApplicationView.CurrentIndexFile);
            return(true);
        }
Exemplo n.º 4
0
        /// <summary>
        /// TODO refactor
        /// </summary>
        public IEnumerable <SearchResultViewModel> Search(IndexViewModel indexDirectory,
                                                          string searchString,
                                                          bool matchCase,
                                                          bool matchWholeWord,
                                                          bool useWildcards,
                                                          out Task <IEnumerable <SearchResultViewModel> > ongoingSearchTask,
                                                          CancellationToken cancelToken,
                                                          IEnumerable <string> fileFilters = null)
        {
            ongoingSearchTask = null;

            if (!LuceneHelper.IsValidIndexDirectory(indexDirectory.IndexDirectory) || string.IsNullOrWhiteSpace(searchString))
            {
                return(Enumerable.Empty <SearchResultViewModel>());
            }

            List <SearchResultViewModel> results = new List <SearchResultViewModel>();

            IsSearching = true;

            try
            {
                using (var reader = IndexReader.Open(FSDirectory.Open(indexDirectory.IndexDirectory), true))
                    using (var searcher = new IndexSearcher(reader))
                    {
                        BooleanQuery resultQuery = new BooleanQuery();
                        if (matchWholeWord)
                        {
                            resultQuery.Add(new BooleanClause(BuildMatchWholeWordContentQuery(searchString, matchCase, useWildcards), Occur.MUST));
                        }
                        else
                        {
                            resultQuery.Add(new BooleanClause(BuildMatchAnywhereQuery(reader, searchString, matchCase, useWildcards), Occur.MUST));
                        }

                        //TODO use this instead of manual filter.
                        //Doesn't work now because recursive booleanQuery doesn't work and can't say to match one of following filters.

                        //Add fileFilter query
                        //if (fileFilters != null && fileFilters.Any())
                        //{
                        //    foreach (var query in BuildFileFilterQueries(fileFilters))
                        //        resultQuery.Add(new BooleanClause(query, Occur.MUST));
                        //}

                        //Add blacklist query
                        var blacklist = Settings.CodeIDXSettings.Blacklist.BlacklistDirectories;
                        if (ApplicationView.UserSettings.IsBlacklistEnabled)
                        {
                            foreach (var curClause in BuildBlacklistQueryClauses(blacklist))
                            {
                                resultQuery.Add(curClause);
                            }
                        }

                        Sort sort = new Sort(new SortField[]
                        {
                            new SortField(Constants.IndexFields.Directory, SortField.STRING),
                            new SortField(Constants.IndexFields.Filename, SortField.STRING),
                            new SortField(Constants.IndexFields.Extension, SortField.STRING)
                        });

                        TopFieldDocs resultCollector = searcher.Search(resultQuery, null, Int32.MaxValue, sort);

                        string adjustedSearchString       = matchCase ? searchString : searchString.ToLower();
                        IEnumerable <string> patternParts = null;
                        if (useWildcards)
                        {
                            patternParts = GetWildcardPatternParts(adjustedSearchString);
                        }

                        //kein Parallel.Foreach verwenden!
                        //durch die grosse Anzahl der Threads die erstellt und verworfen werden ist die Performance sehr schlecht!

                        int lastMatchIndex = 0;
                        foreach (var match in resultCollector.ScoreDocs)
                        {
                            if (cancelToken.IsCancellationRequested)
                            {
                                return(results);
                            }

                            var    curDoc           = reader.Document(match.Doc);
                            string docDirectory     = curDoc.Get(Constants.IndexFields.Directory);
                            string docFilename      = curDoc.Get(Constants.IndexFields.Filename);
                            string docExtension     = curDoc.Get(Constants.IndexFields.Extension);
                            string documentFilename = Path.Combine(docDirectory, docFilename) + docExtension;

                            if (fileFilters != null && !fileFilters.Contains(documentFilename))
                            {
                                continue;
                            }

                            IEnumerable <LineMatch> matchingLines = GetMatchingLines(new GetMatchingLinesArgs(documentFilename, adjustedSearchString, patternParts, matchCase, matchWholeWord));

                            bool isFirst = true;
                            foreach (var lineMatch in matchingLines)
                            {
                                results.Add(new SearchResultViewModel
                                            (
                                                isFirst,
                                                docDirectory,
                                                docFilename,
                                                docExtension,
                                                lineMatch.LineNumber,
                                                lineMatch.Line,
                                                lineMatch.Highlights
                                            ));

                                isFirst = false;
                            }

                            if (results.Count >= CodeIDXSettings.Search.PageSize)
                            {
                                var docNumbers = resultCollector.ScoreDocs.Select(cur => cur.Doc).ToList();
                                ongoingSearchTask = Task.Run <IEnumerable <SearchResultViewModel> >(() => GetRemainingLazyDocuments(indexDirectory,
                                                                                                                                    docNumbers,
                                                                                                                                    lastMatchIndex,
                                                                                                                                    adjustedSearchString,
                                                                                                                                    patternParts,
                                                                                                                                    matchCase,
                                                                                                                                    matchWholeWord,
                                                                                                                                    cancelToken,
                                                                                                                                    fileFilters));

                                return(results);
                            }

                            lastMatchIndex++;
                        }
                    }
            }
            catch { }
            finally
            {
                if (ongoingSearchTask == null)
                {
                    IsSearching = false;
                }
            }

            return(results);
        }
Exemplo n.º 5
0
        public static async Task <bool> LoadIndex(string indexFile)
        {
            if (ApplicationView.CurrentIndexFile != null && ApplicationView.CurrentIndexFile.IndexFile == indexFile)
            {
                return(true);
            }

            Guid opId = Guid.Empty;

            if (!ApplicationView.BeginOperation(StatusKind.Loading, out opId))
            {
                return(false);
            }

            try
            {
                ApplicationView.CurrentIndexFile = IndexViewModel.Load(indexFile);
                if (ApplicationView.CurrentIndexFile == null)
                {
                    if (MessageBox.Show(indexFile + " not found.\n\nThe index file was not found.\nDo you want to remove it from the recent indices?", "Index file not found",
                                        MessageBoxButton.YesNo,
                                        MessageBoxImage.Exclamation) == MessageBoxResult.Yes)
                    {
                        ApplicationView.UserSettings.RemoveRecentIndex(indexFile);
                    }

                    return(false);
                }

                bool validIndexDirectoryFound = await Task.Run <bool>(() => LuceneHelper.IsValidIndexDirectory(ApplicationView.CurrentIndexFile.IndexDirectory));

                if (!validIndexDirectoryFound &&
                    MessageBox.Show(ApplicationView.CurrentIndexFile.IndexDirectory + " not found.\n\nThe index was moved or deleted.\nDo you want to select a new index directory?", "Index not found",
                                    MessageBoxButton.YesNo,
                                    MessageBoxImage.Exclamation) == MessageBoxResult.Yes)
                {
                    var openFolderDialog = new Ookii.Dialogs.Wpf.VistaFolderBrowserDialog
                    {
                        SelectedPath           = Path.GetDirectoryName(ApplicationView.CurrentIndexFile.IndexDirectory),
                        Description            = "New Index Location",
                        UseDescriptionForTitle = true
                    };

                    if (openFolderDialog.ShowDialog() == true)
                    {
                        ApplicationView.CurrentIndexFile.IndexDirectory = openFolderDialog.SelectedPath;
                        ApplicationView.CurrentIndexFile.SaveIndexFile();
                        validIndexDirectoryFound = await Task.Run <bool>(() => LuceneHelper.IsValidIndexDirectory(openFolderDialog.SelectedPath));
                    }
                }

                if (!validIndexDirectoryFound)
                {
                    ApplicationView.CurrentIndexFile = null;
                    ApplicationView.UserSettings.RemoveRecentIndex(indexFile);
                    return(false);
                }

                ApplicationView.UserSettings.AddRecentIndex(ApplicationView.CurrentIndexFile.Name, ApplicationView.CurrentIndexFile.IndexFile);
                ApplicationView.UpdateSearchFilter();

                ApplicationView.FileWatcher.WatchDirectories(ApplicationView.CurrentIndexFile.SourceDirectories);
            }
            finally
            {
                ApplicationView.EndOperation(opId);
            }

            RefreshIndexAtStartup(ApplicationView.CurrentIndexFile);
            return(true);
        }