Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stale app cache results #209

Merged
merged 1 commit into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions Pinpoint.Plugin.AppSearch/AppSearchPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ namespace Pinpoint.Plugin.AppSearch
public class AppSearchPlugin : AbstractPlugin
{
private static readonly UkkonenTrie<IApp> CachedAppsTrie = new();
private static readonly IAppProvider StandardAppProvider = new StandardAppProvider();
private static readonly IAppProvider PathAppProvider = new PathAppProvider();
private static readonly IAppProvider[] RuntimeAppProviders = {
new StandardAppProvider(),
new PathAppProvider()
StandardAppProvider,
PathAppProvider,
};

public AppSearchFrequency AppSearchFrequency;
Expand All @@ -32,9 +34,9 @@ public override async Task<bool> Initialize()
var tasks = new List<Task>
{
Task.Run(() => PopulateCache(new UwpAppProvider())),
}.Concat(
RuntimeAppProviders.Select(provider => Task.Run(() => PopulateCache(provider))).ToList()
);
Task.Run(() => PathAppProvider.Provide().ToList()),
Task.Run(() => StandardAppProvider.Provide().ToList()),
};
await Task.WhenAll(tasks);

return true;
Expand All @@ -46,14 +48,14 @@ public override async IAsyncEnumerable<AbstractQueryResult> ProcessQuery(Query q
{
var queryLower = query.RawQuery.ToLower();

var staticMatches = CachedAppsTrie.Retrieve(queryLower);
var cachedMatches = CachedAppsTrie.Retrieve(queryLower);
var runtimeMatches = RuntimeAppProviders.SelectMany(provider => provider.Provide())
.Where(a =>
{
var variations = GenerateAliases(a.Name);
return variations.Any(v => v.StartsWith(queryLower));
});
var allMatches = staticMatches
var allMatches = cachedMatches
.Concat(runtimeMatches)
.OrderByDescending(a => AppSearchFrequency.FrequencyOfFor(queryLower, a.FilePath));

Expand Down
22 changes: 16 additions & 6 deletions Pinpoint.Plugin.AppSearch/Providers/DirectoryAppProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,19 @@ public IEnumerable<IApp> Provide()
_cachedApps.Add(app);
}
}
foreach (var cachedApp in _cachedApps)

for (int i = 0; i < _cachedApps.Count;)
{
var cachedApp = _cachedApps[i];
if (!File.Exists(cachedApp.FilePath))
{
_uniqueAppNames.Remove(cachedApp.Name);
_cachedApps.Remove(cachedApp);
_lastPathWriteCache[Path.GetDirectoryName(cachedApp.FilePath)] = DateTime.MinValue;
continue;
}

i++;
yield return cachedApp;
}
}
Expand All @@ -65,10 +75,10 @@ IEnumerable<string> SafeGetFiles(string ext)
return Array.Empty<string>();
}
}
return extensions.SelectMany(SafeGetFiles);

return extensions.SelectMany(SafeGetFiles);
}

private IEnumerable<string> GetPathsNeedUpdate()
{
foreach (var path in _paths.Where(Directory.Exists))
Expand All @@ -78,7 +88,7 @@ private IEnumerable<string> GetPathsNeedUpdate()
{
continue;
}

_lastPathWriteCache[path] = currentLastWrite;
yield return path;
}
Expand Down