[PT Run] Split indexer plugin's queries into a fast and slow query (#5748)

* Added regex code

* Added regex based method to remove all LIKE queries

* Made regex readonly

* Added plugin interface and code to execute slower plugins after the fast plugins

* Added scoring for indexer and added statement to remove old indexer results

* Refactored from master and added thread sleep for debugging

* Removed lock from indexer plugin and added checks to avoid exceptions

* Remove debug statement

* Removed selected index update and fixed tests not building

* Added tests

* Removed scoring

* Removed lock

* Resolve merge conflicts

* Moved dispatcher code to function and add parallel foreach loop

* Removed DelayedExec metadata and modified QueryForPlugin to run only delayed exec plugins when bool param is true

* Removed metadata from plugin.json
This commit is contained in:
Arjun Balgovind
2020-08-11 14:52:03 -07:00
committed by GitHub
parent 304981fcf2
commit dcd0ca8daa
7 changed files with 208 additions and 41 deletions

View File

@@ -20,7 +20,7 @@ using Wox.Plugin;
namespace Microsoft.Plugin.Indexer
{
internal class Main : ISettingProvider, IPlugin, ISavable, IPluginI18n, IContextMenu, IDisposable
internal class Main : ISettingProvider, IPlugin, ISavable, IPluginI18n, IContextMenu, IDisposable, IDelayedExecutionPlugin
{
// This variable contains metadata about the Plugin
private PluginInitContext _context;
@@ -54,7 +54,7 @@ namespace Microsoft.Plugin.Indexer
// This function uses the Windows indexer and returns the list of results obtained
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "We want to keep the process alive but will log the exception")]
public List<Result> Query(Query query)
public List<Result> Query(Query query, bool isFullQuery)
{
var results = new List<Result>();
@@ -95,7 +95,14 @@ namespace Microsoft.Plugin.Indexer
});
}
var searchResultsList = _api.Search(searchQuery, maxCount: _settings.MaxSearchCount).ToList();
var searchResultsList = _api.Search(searchQuery, isFullQuery, 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)
{
return new List<Result>();
}
foreach (var searchResult in searchResultsList)
{
var path = searchResult.Path;
@@ -161,6 +168,12 @@ namespace Microsoft.Plugin.Indexer
return results;
}
// This function uses the Windows indexer and returns the list of results obtained. This version is required to implement the interface
public List<Result> Query(Query query)
{
return Query(query, false);
}
public void Init(PluginInitContext context)
{
// initialize the context of the plugin

View File

@@ -33,9 +33,9 @@ namespace Microsoft.Plugin.Indexer.SearchHelper
{
using (wDSResults = command.ExecuteReader())
{
if (wDSResults.HasRows)
if (!wDSResults.IsClosed && wDSResults.HasRows)
{
while (wDSResults.Read())
while (!wDSResults.IsClosed && wDSResults.Read())
{
List<object> fieldData = new List<object>();
for (int i = 0; i < wDSResults.FieldCount; i++)

View File

@@ -4,6 +4,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;
using Microsoft.Search.Interop;
namespace Microsoft.Plugin.Indexer.SearchHelper
@@ -13,8 +15,9 @@ namespace Microsoft.Plugin.Indexer.SearchHelper
public bool DisplayHiddenFiles { get; set; }
private readonly ISearch windowsIndexerSearch;
private readonly object _lock = new object();
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)
{
@@ -22,7 +25,7 @@ namespace Microsoft.Plugin.Indexer.SearchHelper
DisplayHiddenFiles = displayHiddenFiles;
}
public List<SearchResult> ExecuteQuery(ISearchQueryHelper queryHelper, string keyword)
public List<SearchResult> ExecuteQuery(ISearchQueryHelper queryHelper, string keyword, bool isFullQuery = false)
{
if (queryHelper == null)
{
@@ -33,6 +36,17 @@ 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<OleDBResult> oleDBResults = windowsIndexerSearch.Query(queryHelper.ConnectionString, sqlQuery);
@@ -121,15 +135,17 @@ namespace Microsoft.Plugin.Indexer.SearchHelper
queryHelper.QuerySorting = "System.DateModified DESC";
}
public IEnumerable<SearchResult> Search(string keyword, string pattern = "*", int maxCount = 30)
public IEnumerable<SearchResult> Search(string keyword, bool isFullQuery = false, string pattern = "*", int maxCount = 30)
{
lock (_lock)
{
ISearchQueryHelper queryHelper;
InitQueryHelper(out queryHelper, maxCount);
ModifyQueryHelper(ref queryHelper, pattern);
return ExecuteQuery(queryHelper, keyword);
}
ISearchQueryHelper queryHelper;
InitQueryHelper(out queryHelper, maxCount);
ModifyQueryHelper(ref queryHelper, pattern);
return ExecuteQuery(queryHelper, keyword, isFullQuery);
}
public static string SimplifyQuery(string sqlQuery)
{
return _likeRegex.Replace(sqlQuery, string.Empty);
}
}
}