void UpdateIndex(string fullPath, PendingRetrySource pendingRetrySource = null) { if (IsFile(fullPath)) { var fileInfo = new FileInfo(fullPath); try { Thread.Sleep(WaitMilliseconds); // Wait to let file finished write to disk if (fileInfo.Exists) { var content = FilesContentHelper.ReadAllText(fullPath); var document = CodeIndexBuilder.GetDocumentFromSource(CodeSource.GetCodeSource(fileInfo, content)); CodeIndexBuilder.UpdateIndex(config.LuceneIndexForCode, GetNoneTokenizeFieldTerm(nameof(CodeSource.FilePath), fullPath), document); WordsHintBuilder.UpdateWordsHint(config, WordSegmenter.GetWords(content), log); pendingChanges++; } } catch (IOException) { AddFileChangesToRetrySouce(fullPath, WatcherChangeTypes.Changed, pendingRetrySource); } catch (Exception ex) { log?.Error(ex.ToString()); } } }
public void TestUpdateCodeFilePath() { var document = CodeIndexBuilder.GetDocumentFromSource(new CodeSource { Content = "AAA", FileExtension = "CCC", FilePath = "BBB/DDD/1.txt", FileName = "1.txt", IndexDate = new DateTime(1999, 12, 31), LastWriteTimeUtc = new DateTime(2000, 1, 1) }); CodeIndexBuilder.UpdateCodeFilePath(document, "BBB/DDD/", "AAA/EEE/"); Assert.AreEqual(@"AAA/EEE/1.txt", document.Get(nameof(CodeSource.FilePath))); Assert.AreEqual(@"AAA/EEE/1.txt", document.Get(nameof(CodeSource.FilePath) + Constants.NoneTokenizeFieldSuffix)); }
void FileRenamed(string oldFullPath, string fullPath, PendingRetrySource pendingRetrySource = null) { try { if (IsFile(fullPath)) { var fileInfo = new FileInfo(fullPath); try { if (fileInfo.Exists) { var content = FilesContentHelper.ReadAllText(fullPath); var document = CodeIndexBuilder.GetDocumentFromSource(CodeSource.GetCodeSource(fileInfo, content)); CodeIndexBuilder.UpdateIndex(config.LuceneIndexForCode, GetNoneTokenizeFieldTerm(nameof(CodeSource.FilePath), oldFullPath), document); pendingChanges++; } } catch (IOException) { AddFileChangesToRetrySouce(fullPath, WatcherChangeTypes.Renamed, pendingRetrySource, oldFullPath); } } else if (IsDirectory(fullPath)) { // Rebuild All Sub Directory Index File, rename the index path var term = new PrefixQuery(GetNoneTokenizeFieldTerm(nameof(CodeSource.FilePath), oldFullPath)); var docs = CodeIndexSearcher.Search(config.LuceneIndexForCode, term, int.MaxValue); foreach (var doc in docs) { CodeIndexBuilder.UpdateCodeFilePath(doc, oldFullPath, fullPath); CodeIndexBuilder.UpdateIndex(config.LuceneIndexForCode, new Term(nameof(CodeSource.CodePK), doc.Get(nameof(CodeSource.CodePK))), doc); pendingChanges++; } } } catch (Exception ex) { log?.Error(ex.ToString()); } }
public void TestUpdateIndex() { BuildIndex(); LucenePool.SaveResultsAndClearLucenePool(Config.LuceneIndexForCode); var result = CodeIndexSearcher.Search(Config.LuceneIndexForCode, new TermQuery(new Term(nameof(CodeSource.FilePath) + Constants.NoneTokenizeFieldSuffix, @"D:\DDDD\A new Name.cs")), 10); Assert.That(result.Length, Is.EqualTo(1)); CodeIndexBuilder.UpdateIndex(Config.LuceneIndexForCode, new Term(nameof(CodeSource.FilePath) + Constants.NoneTokenizeFieldSuffix, @"d:\dddd\a new name.cs"), CodeIndexBuilder.GetDocumentFromSource(new CodeSource() { Content = "AAA", FileExtension = "CCC", FilePath = "BBB", FileName = "DDD", IndexDate = new DateTime(1999, 12, 31), LastWriteTimeUtc = new DateTime(2000, 1, 1) })); LucenePool.SaveResultsAndClearLucenePool(Config.LuceneIndexForCode); result = CodeIndexSearcher.Search(Config.LuceneIndexForCode, new TermQuery(new Term(nameof(CodeSource.FilePath) + Constants.NoneTokenizeFieldSuffix, @"d:\dddd\a new name.cs")), 10); Assert.That(result.Length, Is.EqualTo(0)); result = CodeIndexSearcher.Search(Config.LuceneIndexForCode, new TermQuery(new Term(nameof(CodeSource.FilePath) + Constants.NoneTokenizeFieldSuffix, "BBB")), 10); Assert.That(result.Length, Is.EqualTo(1)); Assert.AreEqual(@"DDD", result[0].Get(nameof(CodeSource.FileName))); Assert.AreEqual(@"CCC", result[0].Get(nameof(CodeSource.FileExtension))); Assert.AreEqual(@"BBB", result[0].Get(nameof(CodeSource.FilePath))); Assert.AreEqual("AAA", result[0].Get(nameof(CodeSource.Content))); Assert.AreEqual(new DateTime(1999, 12, 31).Ticks, result[0].GetField(nameof(CodeSource.IndexDate)).GetInt64Value()); Assert.AreEqual(new DateTime(2000, 1, 1).Ticks, result[0].GetField(nameof(CodeSource.LastWriteTimeUtc)).GetInt64Value()); }