Example #1
0
        public static Document[] Search(string luceneIndex, Query query, int maxResults)
        {
            luceneIndex.RequireNotNullOrEmpty(nameof(luceneIndex));
            query.RequireNotNull(nameof(query));
            maxResults.RequireRange(nameof(maxResults), int.MaxValue, 1);

            return(LucenePool.Search(luceneIndex, query, maxResults));
        }
Example #2
0
        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));
        }
Example #3
0
        public void TestBuildIndexByBatch()
        {
            WordsHintBuilder.AddWords(new[] { "AAAA", "Bbbb", "DDDDD" });
            WordsHintBuilder.BuildIndexByBatch(Config, true, true, true, null, true);

            var docs = LucenePool.Search(Config.LuceneIndexForHint, new MatchAllDocsQuery(), 1000);

            Assert.AreEqual(3, docs.Length);
            CollectionAssert.AreEquivalent(new[] { "AAAA", "Bbbb", "DDDDD" }, docs.Select(u => u.Get(nameof(CodeWord.Word))));
            CollectionAssert.AreEquivalent(new[] { "aaaa", "bbbb", "ddddd" }, docs.Select(u => u.Get(nameof(CodeWord.WordLower))));
            Assert.AreEqual(0, WordsHintBuilder.Words.Count);
        }
Example #4
0
        public void TestUpdateWordsAndUpdateIndex()
        {
            WordsHintBuilder.AddWords(new[] { "AAAA", "Bbbbb", "DDDDD" });
            WordsHintBuilder.BuildIndexByBatch(Config, true, true, true, null, true);

            var docs = LucenePool.Search(Config.LuceneIndexForHint, new MatchAllDocsQuery(), 1000);

            Assert.AreEqual(3, docs.Length);

            WordsHintBuilder.UpdateWordsHint(Config, new[] { "AAAA", "Bbbbb", "EEEEE", "ABC", "VeryLongWord".PadRight(200, 'L') }, null);
            docs = LucenePool.Search(Config.LuceneIndexForHint, new MatchAllDocsQuery(), 1000);
            Assert.AreEqual(4, docs.Length, "Skip duplicate and length must larger than 3 and less than 200");
            CollectionAssert.AreEquivalent(new[] { "AAAA", "Bbbbb", "DDDDD", "EEEEE" }, docs.Select(u => u.Get(nameof(CodeWord.Word))));
            CollectionAssert.AreEquivalent(new[] { "aaaa", "bbbbb", "ddddd", "eeeee" }, docs.Select(u => u.Get(nameof(CodeWord.WordLower))));
        }
Example #5
0
        public void TestBuildIndexByBatch_NotFirstInit()
        {
            WordsHintBuilder.AddWords(new[] { "AAAA", "Bbbb", "DDDDD" });
            WordsHintBuilder.BuildIndexByBatch(Config, true, true, true, null, true);

            var docs = LucenePool.Search(Config.LuceneIndexForHint, new MatchAllDocsQuery(), 1000);

            Assert.AreEqual(3, docs.Length);
            CollectionAssert.AreEquivalent(new[] { "AAAA", "Bbbb", "DDDDD" }, docs.Select(u => u.Get(nameof(CodeWord.Word))));

            WordsHintBuilder.AddWords(new[] { "AAAA" });
            WordsHintBuilder.BuildIndexByBatch(Config, true, true, true, null, false);
            docs = LucenePool.Search(Config.LuceneIndexForHint, new MatchAllDocsQuery(), 1000);
            Assert.AreEqual(3, docs.Length, "When is not first init, do update documents");
        }