Example #1
0
        public void TestGetHints()
        {
            WordsHintBuilder.Words.Add("ABCD");
            WordsHintBuilder.Words.Add("Abcdef");
            WordsHintBuilder.BuildIndexByBatch(Config, true, true, true, null, true);

            CollectionAssert.AreEquivalent(new[] { "ABCD" }, LucenePool.GetHints(Config.LuceneIndexForHint, "ABC", 10, true));
            CollectionAssert.AreEquivalent(new[] { "ABCD", "Abcdef" }, LucenePool.GetHints(Config.LuceneIndexForHint, "ABC", 10, false));
        }
Example #2
0
        public void TestGetHints()
        {
            WordsHintBuilder.Words.Add("Abcd");
            WordsHintBuilder.BuildIndexByBatch(Config, true, true, true, null, true, 1000);

            CollectionAssert.AreEquivalent(new[] { "Abcd" }, CodeIndexSearcher.GetHints(Config.LuceneIndexForHint, "abc", 20, false));
            CollectionAssert.IsEmpty(CodeIndexSearcher.GetHints(Config.LuceneIndexForHint, "abc", 20, true));
            CollectionAssert.AreEquivalent(new[] { "Abcd" }, CodeIndexSearcher.GetHints(Config.LuceneIndexForHint, "Abc", 20, true));
        }
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");
        }
Example #6
0
        public void InitializeIndex(CodeIndexConfiguration config, out List <FileInfo> failedIndexFiles, bool forceDeleteAllIndex = false)
        {
            config.RequireNotNull(nameof(config));

            log?.Info($"Initialize start for {config.LuceneIndex}");

            var             allFiles         = FilesFetcher.FetchAllFiles(config.MonitorFolder, config.ExcludedExtensionsArray, config.ExcludedPathsArray, includedExtensions: config.IncludedExtensionsArray, isInLinux: config.IsInLinux).ToList();
            List <FileInfo> needToBuildIndex = null;
            var             firstInitialize  = true;

            CodeIndexBuilder.InitIndexFolderIfNeeded(config, log);

            if (CodeIndexBuilder.IndexExists(config.LuceneIndexForCode))
            {
                if (forceDeleteAllIndex)
                {
                    log?.Info("Delete exist index");
                    CodeIndexBuilder.DeleteAllIndex(config);
                }
                else
                {
                    firstInitialize = false;
                    log?.Info("Compare index difference");

                    var allCodeSource = CodeIndexBuilder.GetAllIndexedCodeSource(config.LuceneIndexForCode);
                    needToBuildIndex = new List <FileInfo>();

                    foreach (var codeSource in allCodeSource)
                    {
                        var fileInfo = allFiles.FirstOrDefault(u => u.FullName == codeSource.FilePath);

                        if (fileInfo != null)
                        {
                            if (fileInfo.LastWriteTimeUtc != codeSource.LastWriteTimeUtc)
                            {
                                log?.Info($"File {fileInfo.FullName} modified");

                                CodeIndexBuilder.DeleteIndex(config.LuceneIndexForCode, CodeFilesIndexMaintainer.GetNoneTokenizeFieldTerm(nameof(CodeSource.FilePath), codeSource.FilePath));
                                needToBuildIndex.Add(fileInfo);
                            }

                            allFiles.Remove(fileInfo);
                        }
                        else
                        {
                            log?.Info($"File {codeSource.FilePath} deleted");

                            CodeIndexBuilder.DeleteIndex(config.LuceneIndexForCode, CodeFilesIndexMaintainer.GetNoneTokenizeFieldTerm(nameof(CodeSource.FilePath), codeSource.FilePath));
                        }
                    }

                    foreach (var needToCreateFiles in allFiles)
                    {
                        log?.Info($"Found new file {needToCreateFiles.FullName}");
                        needToBuildIndex.Add(needToCreateFiles);
                    }
                }
            }

            CodeIndexBuilder.BuildIndexByBatch(config, true, true, true, needToBuildIndex ?? allFiles, false, log, out failedIndexFiles);

            LucenePool.SaveResultsAndClearLucenePool(config.LuceneIndexForCode);

            WordsHintBuilder.BuildIndexByBatch(config, true, true, true, log, firstInitialize);

            log?.Info($"Initialize finished for {config.LuceneIndex}");
        }