示例#1
0
        void RenameIndex(Document document, string oldFilePath, string nowFilePath)
        {
            var pathField = document.Get(nameof(CodeSource.FilePath));
            var nowPath   = pathField.Replace(oldFilePath, nowFilePath);

            document.RemoveField(nameof(CodeSource.FilePath));
            document.RemoveField(nameof(CodeSource.FilePath) + Constants.NoneTokenizeFieldSuffix);
            document.Add(new TextField(nameof(CodeSource.FilePath), nowPath, Field.Store.YES));
            document.Add(new StringField(nameof(CodeSource.FilePath) + Constants.NoneTokenizeFieldSuffix, nowPath, Field.Store.YES));
            CodeIndexPool.UpdateIndex(new Term(nameof(CodeSource.CodePK), document.Get(nameof(CodeSource.CodePK))), document);
        }
示例#2
0
        public IndexBuildResults CreateIndex(FileInfo fileInfo)
        {
            try
            {
                if (fileInfo.Exists)
                {
                    var source = CodeSource.GetCodeSource(fileInfo, FilesContentHelper.ReadAllText(fileInfo.FullName));

                    var words = new HashSet <string>();
                    AddHintWords(words, source.Content);

                    var doc = IndexBuilderHelper.GetDocumentFromSource(source);
                    CodeIndexPool.UpdateIndex(GetNoneTokenizeFieldTerm(nameof(CodeSource.FilePath), source.FilePath), doc);

                    foreach (var word in words)
                    {
                        HintIndexPool.UpdateIndex(new Term(nameof(CodeWord.Word), word), new Document
                        {
                            new StringField(nameof(CodeWord.Word), word, Field.Store.YES),
                            new StringField(nameof(CodeWord.WordLower), word.ToLowerInvariant(), Field.Store.YES)
                        });
                    }

                    Log.LogInformation($"{Name}: Create index For {source.FilePath} finished");
                }

                return(IndexBuildResults.Successful);
            }
            catch (Exception ex)
            {
                Log.LogError($"{Name}: Create index for {fileInfo.FullName} failed, exception: " + ex);

                if (ex is IOException)
                {
                    return(IndexBuildResults.FailedWithIOException);
                }
                else if (ex is OperationCanceledException)
                {
                    throw;
                }

                return(IndexBuildResults.FailedWithError);
            }
        }
示例#3
0
        public IndexBuildResults UpdateIndex(FileInfo fileInfo, CancellationToken cancellationToken)
        {
            try
            {
                if (fileInfo.Exists)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    var source = CodeSource.GetCodeSource(fileInfo, FilesContentHelper.ReadAllText(fileInfo.FullName));

                    var words = new HashSet <string>();
                    AddHintWords(words, source.Content);

                    var doc = IndexBuilderHelper.GetDocumentFromSource(source);
                    CodeIndexPool.UpdateIndex(GetNoneTokenizeFieldTerm(nameof(CodeSource.FilePath), source.FilePath), doc, out var rawDocuments);

                    if (rawDocuments.Length >= 1)
                    {
                        var rawWords = new HashSet <string>();
                        AddHintWords(rawWords, GetCodeSourceFromDocument(rawDocuments[0]).Content);

                        var wordsNeedToRemove = rawWords.Except(words).ToArray();
                        var wordsNeedToAdd    = words.Except(rawWords);
                        words = wordsNeedToAdd.ToHashSet();

                        Log.LogInformation($"{Name}: Find {wordsNeedToRemove.Length} Delete Candidates Words, {words.Count} Update Candidates Words With Path {source.FilePath}");

                        if (rawDocuments.Length > 1)
                        {
                            Log.LogError($"{Name}: Find {rawDocuments.Length} Documents With Path {source.FilePath} To Update");
                        }

                        foreach (var needToDeleteWord in wordsNeedToRemove)
                        {
                            if (!CodeIndexPool.Exists(new TermQuery(new Term(GetCaseSensitiveField(nameof(CodeSource.Content)), needToDeleteWord))))
                            {
                                HintIndexPool.DeleteIndex(new Term(nameof(CodeWord.Word), needToDeleteWord));
                            }
                        }
                    }
                    else
                    {
                        Log.LogError($"{Name}: Find 0 Document To Update With Path {source.FilePath}, Create New Index");
                    }

                    foreach (var word in words)
                    {
                        HintIndexPool.UpdateIndex(new Term(nameof(CodeWord.Word), word), new Document
                        {
                            new StringField(nameof(CodeWord.Word), word, Field.Store.YES),
                            new StringField(nameof(CodeWord.WordLower), word.ToLowerInvariant(), Field.Store.YES)
                        });
                    }

                    Log.LogInformation($"{Name}: Update index For {source.FilePath} finished");
                }

                return(IndexBuildResults.Successful);
            }
            catch (Exception ex)
            {
                Log.LogError($"{Name}: Update index for {fileInfo.FullName} failed, exception: " + ex);

                if (ex is IOException)
                {
                    return(IndexBuildResults.FailedWithIOException);
                }
                else if (ex is OperationCanceledException)
                {
                    throw;
                }

                return(IndexBuildResults.FailedWithError);
            }
        }