void CreateNewIndex(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); CodeIndexBuilder.BuildIndex(config, false, false, false, new[] { CodeSource.GetCodeSource(fileInfo, content) }); WordsHintBuilder.UpdateWordsHint(config, WordSegmenter.GetWords(content), log); pendingChanges++; } } catch (IOException) { AddFileChangesToRetrySouce(fullPath, WatcherChangeTypes.Created, pendingRetrySource); } catch (Exception ex) { log?.Error(ex.ToString()); } } }
public void TestSearch_ReaderFromWriter() { CodeIndexBuilder.BuildIndex(Config, true, true, true, new[] { new CodeSource { FileName = "Dummy File 1", FileExtension = "cs", FilePath = @"C:\Dummy File 1.cs", Content = "Test Content" + Environment.NewLine + "A New Line For Test" }, new CodeSource { FileName = "Dummy File 2", FileExtension = "cs", FilePath = @"C:\Dummy File 2.cs", Content = "Test Content" + Environment.NewLine + "A New Line For Test" }, new CodeSource { FileName = "Dummy File 2", FileExtension = "xml", FilePath = @"C:\Dummy File.xml", Content = "Test Content" + Environment.NewLine + "A New Line For Test" } }); var results = CodeIndexSearcher.Search(Config.LuceneIndexForCode, new TermQuery(new Term(nameof(CodeSource.FileExtension), "xml")), 10); Assert.That(results.Length, Is.EqualTo(1)); }
public void TestThreadSafeForIndexReader() { CodeIndexBuilder.BuildIndex(Config, true, true, true, new[] { new CodeSource { FileName = "Dummy File 1", FileExtension = "cs", FilePath = @"C:\Dummy File 1.cs", Content = "Test Content" + Environment.NewLine + "A New Line For Test" }, new CodeSource { FileName = "Dummy File 2", FileExtension = "cs", FilePath = @"C:\Dummy File 2.cs", Content = "Test Content" + Environment.NewLine + "A New Line For Test" }, new CodeSource { FileName = "Dummy File 2", FileExtension = "xml", FilePath = @"C:\Dummy File.xml", Content = "Test Content" + Environment.NewLine + "A New Line For Test" } }); LucenePool.SaveResultsAndClearLucenePool(Config); var taskList = new List <Task>(); for (int i = 0; i < 20; i++) { taskList.Add(Task.Run(() => { for (int j = 0; j < 50; j++) { if (j % 4 == 0) { CodeIndexBuilder.BuildIndex(Config, true, true, true, new[] { new CodeSource { FileName = $"Dummy File 1 {i} {j}", FileExtension = "cs", FilePath = $@"C:\Dummy File 1 {i} {j}.cs", Content = "Test Content" + Environment.NewLine + "A New Line For Test" } }); } LucenePool.Search(Config.LuceneIndexForCode, new MatchAllDocsQuery(), int.MaxValue); LucenePool.Search(Config.LuceneIndexForCode, new MatchAllDocsQuery(), int.MaxValue); } })); } Assert.DoesNotThrowAsync(async() => await Task.WhenAll(taskList)); }
void BuildIndex() { CodeIndexBuilder.BuildIndex(Config, true, true, true, new[] { new CodeSource { FileName = "Dummy File", FileExtension = "cs", FilePath = @"C:\Dummy File.cs", Content = "Test Content" + Environment.NewLine + "A New Line For Test", IndexDate = new DateTime(2020, 1, 1) }, new CodeSource { FileName = "A new File", FileExtension = "xml", FilePath = @"D:\DDDD\A new Name.cs", Content = "FFFF Content A new Line" } }); }
public void TestBuildIndex_DeleteOldIndexWithSamePath() { BuildIndex(); LucenePool.SaveResultsAndClearLucenePool(Config.LuceneIndexForCode); var result = CodeIndexSearcher.Search(Config.LuceneIndexForCode, Generator.GetQueryFromStr($"{nameof(CodeSource.FileName)}:\"Dummy File\""), 10); Assert.AreEqual("Test Content" + Environment.NewLine + "A New Line For Test", result.Single().Get(nameof(CodeSource.Content))); CodeIndexBuilder.BuildIndex(Config, true, true, true, new[] { new CodeSource { FileName = "Dummy File New", FileExtension = "cs", FilePath = @"C:\Dummy File.cs", Content = "ABC", IndexDate = new DateTime(2020, 1, 1) } }); LucenePool.SaveResultsAndClearLucenePool(Config.LuceneIndexForCode); result = CodeIndexSearcher.Search(Config.LuceneIndexForCode, Generator.GetQueryFromStr($"{nameof(CodeSource.FileName)}:\"Dummy File New\""), 10); Assert.AreEqual("ABC", result.Single().Get(nameof(CodeSource.Content))); }
public void TestMaintainerIndex() { Config.ExcludedExtensions = ".dll"; Config.SaveIntervalSeconds = 1; var waitMS = 1500; Directory.CreateDirectory(MonitorFolder); Directory.CreateDirectory(Path.Combine(MonitorFolder, "FolderA")); Directory.CreateDirectory(Path.Combine(MonitorFolder, "FolderB")); var fileAPath = Path.Combine(MonitorFolder, "FolderA", "AAA.cs"); File.Create(fileAPath).Close(); File.AppendAllText(fileAPath, "12345"); var fileBPath = Path.Combine(MonitorFolder, "FolderB", "BBB.xml"); File.Create(fileBPath).Close(); File.AppendAllText(fileBPath, "this is a content for test, that's it\r\na new line;"); var fileCPath = Path.Combine(MonitorFolder, "CCC.xml"); File.Create(fileCPath).Close(); File.AppendAllText(fileCPath, "this is a content for test"); var fileDPath = Path.Combine(MonitorFolder, "DDD.txt"); CodeIndexBuilder.BuildIndex(Config, true, true, true, new[] { CodeSource.GetCodeSource(new FileInfo(fileAPath), "12345"), CodeSource.GetCodeSource(new FileInfo(fileBPath), "this is a content for test, that's it\r\na new line;"), CodeSource.GetCodeSource(new FileInfo(fileCPath), "this is a content for test") }); LucenePool.SaveResultsAndClearLucenePool(Config.LuceneIndexForCode); var codeSources = CodeIndexSearcher.SearchCode(Config.LuceneIndexForCode, new MatchAllDocsQuery(), 100); CollectionAssert.AreEquivalent(new[] { "AAA.cs", "BBB.xml", "CCC.xml" }, codeSources.Select(u => u.FileName)); using var maintainer = new CodeFilesIndexMaintainer(Config, new DummyLog()); maintainer.StartWatch(); maintainer.SetInitalizeFinishedToTrue(); File.AppendAllText(fileAPath, "56789"); // Changed File.Delete(fileBPath); // Deleted File.Move(fileCPath, Path.Combine(MonitorFolder, "NewCCC.xml")); // Rename File.Create(fileDPath).Close(); // Created Thread.Sleep(waitMS); // wait task finish saving codeSources = CodeIndexSearcher.SearchCode(Config.LuceneIndexForCode, new MatchAllDocsQuery(), 100); Assert.Multiple(() => { CollectionAssert.AreEquivalent(new[] { "AAA.cs", "NewCCC.xml", "DDD.txt" }, codeSources.Select(u => u.FileName)); CollectionAssert.AreEquivalent(new[] { "1234556789", "this is a content for test", string.Empty }, codeSources.Select(u => u.Content)); CollectionAssert.AreEquivalent(new[] { fileAPath, Path.Combine(MonitorFolder, "NewCCC.xml"), fileDPath }, codeSources.Select(u => u.FilePath)); }); maintainer.Dispose(); }