From 29752c72d03ec34ce81532f4d83566b0960e5a23 Mon Sep 17 00:00:00 2001 From: Mykhailo Pylyp Date: Mon, 14 Dec 2020 18:07:26 +0200 Subject: [PATCH] remove the indexer plugin from constant search plugins (#8367) --- .../Plugins/Microsoft.Plugin.Indexer/Main.cs | 5 +- .../SearchHelper/WindowsSearchAPI.cs | 23 +---- .../Wox.Test/Plugins/WindowsIndexerTest.cs | 85 ------------------- 3 files changed, 6 insertions(+), 107 deletions(-) diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Indexer/Main.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Indexer/Main.cs index e8b58aa9f1..d122279724 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Indexer/Main.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Indexer/Main.cs @@ -101,7 +101,7 @@ namespace Microsoft.Plugin.Indexer // This uses the Microsoft.Search.Interop assembly var searchManager = new CSearchManager(); - var searchResultsList = _api.Search(searchQuery, searchManager, isFullQuery, maxCount: _settings.MaxSearchCount).ToList(); + var searchResultsList = _api.Search(searchQuery, searchManager, maxCount: _settings.MaxSearchCount).ToList(); // If the delayed execution query is not required (since the SQL query is fast) return empty results if (searchResultsList.Count == 0 && isFullQuery) @@ -179,7 +179,8 @@ namespace Microsoft.Plugin.Indexer // This function uses the Windows indexer and returns the list of results obtained. This version is required to implement the interface public List Query(Query query) { - return Query(query, false); + // All plugins have to implement IPlugin interface. We return empty collection as we do not want any computation with constant search plugins. + return new List(); } public void Init(PluginInitContext context) diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Indexer/SearchHelper/WindowsSearchAPI.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Indexer/SearchHelper/WindowsSearchAPI.cs index a2a4401ac6..a47a2281a0 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Indexer/SearchHelper/WindowsSearchAPI.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Indexer/SearchHelper/WindowsSearchAPI.cs @@ -16,7 +16,6 @@ namespace Microsoft.Plugin.Indexer.SearchHelper private readonly ISearch windowsIndexerSearch; private const uint _fileAttributeHidden = 0x2; - private static readonly Regex _likeRegex = new Regex(@"[^\s(]+\s+LIKE\s+'([^']|'')*'\s+OR\s+", RegexOptions.Compiled); public WindowsSearchAPI(ISearch windowsIndexerSearch, bool displayHiddenFiles = false) { @@ -24,7 +23,7 @@ namespace Microsoft.Plugin.Indexer.SearchHelper DisplayHiddenFiles = displayHiddenFiles; } - public List ExecuteQuery(ISearchQueryHelper queryHelper, string keyword, bool isFullQuery = false) + public List ExecuteQuery(ISearchQueryHelper queryHelper, string keyword) { if (queryHelper == null) { @@ -35,17 +34,6 @@ namespace Microsoft.Plugin.Indexer.SearchHelper // Generate SQL from our parameters, converting the userQuery from AQS->WHERE clause string sqlQuery = queryHelper.GenerateSQLFromUserQuery(keyword); - var simplifiedQuery = SimplifyQuery(sqlQuery); - - if (!isFullQuery) - { - sqlQuery = simplifiedQuery; - } - else if (simplifiedQuery.Equals(sqlQuery, StringComparison.CurrentCultureIgnoreCase)) - { - // if a full query is requested but there is no difference between the queries, return empty results - return results; - } // execute the command, which returns the results as an OleDBResults. List oleDBResults = windowsIndexerSearch.Query(queryHelper.ConnectionString, sqlQuery); @@ -141,7 +129,7 @@ namespace Microsoft.Plugin.Indexer.SearchHelper queryHelper.QuerySorting = "System.DateModified DESC"; } - public IEnumerable Search(string keyword, ISearchManager manager, bool isFullQuery = false, string pattern = "*", int maxCount = 30) + public IEnumerable Search(string keyword, ISearchManager manager, string pattern = "*", int maxCount = 30) { if (manager == null) { @@ -151,12 +139,7 @@ namespace Microsoft.Plugin.Indexer.SearchHelper ISearchQueryHelper queryHelper; InitQueryHelper(out queryHelper, manager, maxCount, DisplayHiddenFiles); ModifyQueryHelper(ref queryHelper, pattern); - return ExecuteQuery(queryHelper, keyword, isFullQuery); - } - - public static string SimplifyQuery(string sqlQuery) - { - return _likeRegex.Replace(sqlQuery, string.Empty); + return ExecuteQuery(queryHelper, keyword); } } } diff --git a/src/modules/launcher/Wox.Test/Plugins/WindowsIndexerTest.cs b/src/modules/launcher/Wox.Test/Plugins/WindowsIndexerTest.cs index b9ca054e78..a2863c54ba 100644 --- a/src/modules/launcher/Wox.Test/Plugins/WindowsIndexerTest.cs +++ b/src/modules/launcher/Wox.Test/Plugins/WindowsIndexerTest.cs @@ -368,90 +368,5 @@ namespace Wox.Test.Plugins // Act & Assert return driveDetection.DisplayWarning(); } - - [Test] - public void SimplifyQueryShouldRemoveLikeQueryWhenSQLQueryUsesLIKESyntax() - { - // Arrange - string sqlQuery = "SELECT TOP 30 \"System.ItemUrl\", \"System.FileName\", \"System.FileAttributes\" FROM \"SystemIndex\" WHERE (System.FileName LIKE 'abcd.%' OR CONTAINS(System.FileName,'\"abcd.*\"',1033)) AND scope='file:' ORDER BY System.DateModified DESC"; - - // Act - var simplifiedSqlQuery = WindowsSearchAPI.SimplifyQuery(sqlQuery); - - // Assert - string expectedSqlQuery = "SELECT TOP 30 \"System.ItemUrl\", \"System.FileName\", \"System.FileAttributes\" FROM \"SystemIndex\" WHERE (CONTAINS(System.FileName,'\"abcd.*\"',1033)) AND scope='file:' ORDER BY System.DateModified DESC"; - - // Using InvariantCultureIgnoreCase since this relates to sql code in string form - Assert.IsFalse(simplifiedSqlQuery.Equals(sqlQuery, StringComparison.InvariantCultureIgnoreCase)); - Assert.IsTrue(simplifiedSqlQuery.Equals(expectedSqlQuery, StringComparison.InvariantCultureIgnoreCase)); - } - - [Test] - public void SimplifyQueryShouldReturnArgumentWhenSQLQueryDoesNotUseLIKESyntax() - { - // Arrange - string sqlQuery = "SELECT TOP 30 \"System.ItemUrl\", \"System.FileName\", \"System.FileAttributes\" FROM \"SystemIndex\" WHERE CONTAINS(System.FileName,'\"abcd*\"',1033) AND scope='file:' ORDER BY System.DateModified DESC"; - - // Act - var simplifiedSqlQuery = WindowsSearchAPI.SimplifyQuery(sqlQuery); - - // Assert - // Using InvariantCultureIgnoreCase since this relates to sql code in string form - Assert.IsTrue(simplifiedSqlQuery.Equals(sqlQuery, StringComparison.InvariantCultureIgnoreCase)); - } - - [Test] - public void SimplifyQueryShouldRemoveAllOccurrencesOfLikeQueryWhenSQLQueryUsesLIKESyntaxMultipleTimes() - { - // Arrange - string sqlQuery = "SELECT TOP 30 \"System.ItemUrl\", \"System.FileName\", \"System.FileAttributes\", \"System.FileExtension\" FROM \"SystemIndex\" WHERE (System.FileName LIKE 'ab.%' OR CONTAINS(System.FileName,'\"ab.*\"',1033)) AND (System.FileExtension LIKE '.cd%' OR CONTAINS(System.FileName,'\".cd*\"',1033)) AND scope='file:' ORDER BY System.DateModified DESC"; - - // Act - var simplifiedSqlQuery = WindowsSearchAPI.SimplifyQuery(sqlQuery); - - // Assert - string expectedSqlQuery = "SELECT TOP 30 \"System.ItemUrl\", \"System.FileName\", \"System.FileAttributes\", \"System.FileExtension\" FROM \"SystemIndex\" WHERE (CONTAINS(System.FileName,'\"ab.*\"',1033)) AND (CONTAINS(System.FileName,'\".cd*\"',1033)) AND scope='file:' ORDER BY System.DateModified DESC"; - - // Using InvariantCultureIgnoreCase since this relates to sql code in string form - Assert.IsFalse(simplifiedSqlQuery.Equals(sqlQuery, StringComparison.InvariantCultureIgnoreCase)); - Assert.IsTrue(simplifiedSqlQuery.Equals(expectedSqlQuery, StringComparison.InvariantCultureIgnoreCase)); - } - - [Test] - public void SimplifyQueryShouldRemoveLikeQueryWhenSQLQueryUsesLIKESyntaxAndContainsEscapedSingleQuotationMarks() - { - // Arrange - string sqlQuery = "SELECT TOP 30 \"System.ItemUrl\", \"System.FileName\", \"System.FileAttributes\" FROM \"SystemIndex\" WHERE (System.FileName LIKE '''ab.cd''%' OR CONTAINS(System.FileName,'\"'ab.cd'*\"',1033)) AND scope='file:' ORDER BY System.DateModified DESC"; - - // Act - var simplifiedSqlQuery = WindowsSearchAPI.SimplifyQuery(sqlQuery); - - // Assert - string expectedSqlQuery = "SELECT TOP 30 \"System.ItemUrl\", \"System.FileName\", \"System.FileAttributes\" FROM \"SystemIndex\" WHERE (CONTAINS(System.FileName,'\"'ab.cd'*\"',1033)) AND scope='file:' ORDER BY System.DateModified DESC"; - - // Using InvariantCultureIgnoreCase since this relates to sql code in string form - Assert.IsFalse(simplifiedSqlQuery.Equals(sqlQuery, StringComparison.InvariantCultureIgnoreCase)); - Assert.IsTrue(simplifiedSqlQuery.Equals(expectedSqlQuery, StringComparison.InvariantCultureIgnoreCase)); - } - - [Test] - public void WindowsSearchAPIShouldReturnEmptyResultsWhenIsFullQueryIsTrueAndTheQueryDoesNotRequireLIKESyntax() - { - // Arrange - OleDBResult file1 = new OleDBResult(new List() { "C:/test/path/file1.txt", DBNull.Value }); - OleDBResult file2 = new OleDBResult(new List() { "C:/test/path/file2.txt", "file2.txt" }); - - List results = new List() { file1, file2 }; - var mock = new Mock(); - mock.Setup(x => x.Query(It.IsAny(), It.IsAny())).Returns(results); - WindowsSearchAPI api = new WindowsSearchAPI(mock.Object, false); - var searchManager = GetMockSearchManager(); - - // Act - var windowsSearchAPIResults = api.Search("file", searchManager, true); - - // Assert - Assert.IsTrue(!windowsSearchAPIResults.Any()); - } } }