Skip to content

Commit 2d02183

Browse files
committed
Work on concurrent searching
1 parent 9f2fc50 commit 2d02183

File tree

6 files changed

+87
-28
lines changed

6 files changed

+87
-28
lines changed

SmartImage 3/Mode/CliMode.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using JetBrains.Annotations;
88
using SmartImage.Lib;
99
using SmartImage.Lib.Results;
10+
using SmartImage.Utilities;
1011
using Spectre.Console;
1112
using Spectre.Console.Rendering;
1213
using AConsole = Spectre.Console.AnsiConsole;
@@ -51,11 +52,14 @@ public CliMode()
5152

5253
public async Task<object?> RunAsync(object? c)
5354
{
55+
var cstr = (string) c;
56+
Debug.WriteLine($"Input: {cstr}");
57+
5458
await AConsole.Progress().AutoRefresh(true).StartAsync(async ctx =>
5559
{
5660
var p = ctx.AddTask("Creating query");
5761
p.IsIndeterminate = true;
58-
m_query = await SearchQuery.TryCreateAsync((string) c!);
62+
m_query = await SearchQuery.TryCreateAsync(cstr);
5963
p.Increment(COMPLETE);
6064
ctx.Refresh();
6165
});
@@ -78,10 +82,11 @@ await AConsole.Progress().AutoRefresh(true).StartAsync(async ctx =>
7882

7983
SConsole.CancelKeyPress += (sender, args) =>
8084
{
81-
args.Cancel = false;
82-
m_cts.Cancel();
8385
AConsole.MarkupLine($"[red]Cancellation requested[/]");
84-
Environment.Exit(-1);
86+
m_cts.Cancel();
87+
args.Cancel = false;
88+
89+
Environment.Exit(ConsoleUtil.CODE_ERR);
8590
};
8691

8792
// await Prg_1.StartAsync(RunSearchAsync);

SmartImage 3/Program.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,8 @@ public static async Task<int> Main(string[] args)
106106
return i;
107107

108108
}
109-
else if (args.Contains("-d")) {
110-
var sc =new SearchClient(SearchConfig.Default);
111-
var sq = await SearchQuery.TryCreateAsync("https://i.imgur.com/QtCausw.png");
112-
await sq.UploadAsync();
113-
var r =await sc.RunSearchAsync(sq);
114-
115-
foreach (SearchResult searchResult in r) {
116-
Console.WriteLine(searchResult);
117-
}
118109

119-
return 0;
120-
}
121-
else {
122-
return -1;
123-
}
110+
return ConsoleUtil.CODE_ERR;
124111
}
125112
else {
126113
main1:
@@ -137,7 +124,7 @@ public static async Task<int> Main(string[] args)
137124
goto main1;
138125
}
139126

140-
return 0;
127+
return ConsoleUtil.CODE_OK;
141128
}
142129
}
143130
}

SmartImage 3/Utilities/ConsoleUtil.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,7 @@ internal static void FlashTaskbar()
7373

7474
Native.FlashWindowEx(ref pwfi);
7575
}
76+
77+
internal const int CODE_ERR = -1;
78+
internal const int CODE_OK = 0;
7679
}

SmartImage.Lib 3/Engines/ILoginEngine.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66

77
public interface ILoginEngine
88
{
9-
public string Username { get; set; }
10-
public string Password { get; set; }
9+
public string Username { get; set; }
10+
public string Password { get; set; }
1111

12-
public Task<bool> LoginAsync();
13-
14-
public bool IsLoggedIn { get; }
12+
public Task<bool> LoginAsync();
1513

14+
public bool IsLoggedIn { get; }
1615
}

SmartImage.Lib 3/Engines/Impl/Search/YandexEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
159159
var otherImages = GetOtherImages(doc, sr);
160160
sr.Results.AddRange(otherImages);
161161

162-
var ext = ParseExternalInfo(doc, sr);
162+
var ext = ParseExternalInfo(doc, sr);
163163
sr.Results.AddRange(ext);
164164

165165
//

SmartImage.Lib 3/SearchClient.cs

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,76 @@ static SearchClient()
8888
/// </summary>
8989
/// <param name="query">Search query</param>
9090
/// <param name="token">Cancellation token passed to <see cref="BaseSearchEngine.GetResultAsync"/></param>
91-
public async Task<SearchResult[]> RunSearchAsync(SearchQuery query, CancellationToken? token = null, [CBN] IProgress<int> p=null)
91+
/// <param name="p"><see cref="IProgress{T}"/></param>
92+
public async Task<SearchResult[]> RunSearchAsync(SearchQuery[] query, CancellationToken? token = null,
93+
[CBN] IProgress<int> p = null)
9294
{
9395
if (!ConfigApplied) {
9496
await ApplyConfigAsync();
9597
}
9698

99+
token ??= CancellationToken.None;
100+
var tasks = query.SelectMany(e => GetSearchTasks(e, token.Value)).ToList();
101+
102+
var results = new SearchResult[tasks.Count];
103+
int i = 0;
104+
105+
while (tasks.Any()) {
106+
if (token.Value.IsCancellationRequested) {
107+
108+
Logger.LogWarning("Cancellation requested");
109+
IsComplete = true;
110+
111+
return results;
112+
}
113+
114+
var task = await Task.WhenAny(tasks);
115+
116+
var result = await task;
117+
118+
OnResult?.Invoke(this, result);
119+
p?.Report(i);
120+
121+
if (Config.PriorityEngines.HasFlag(result.Engine.EngineOption)) {
122+
123+
OpenResult(result);
124+
}
125+
126+
results[i] = result;
127+
i++;
128+
// results.Add(result);
129+
tasks.Remove(task);
130+
}
131+
132+
OnComplete?.Invoke(this, results);
133+
134+
IsComplete = true;
135+
136+
if (Config.PriorityEngines == SearchEngineOptions.Auto) {
137+
138+
// var sri = results.SelectMany(r => r.Results).ToArray();
139+
// var result = Optimize(sri).FirstOrDefault() ?? sri.FirstOrDefault();
140+
//todo
141+
OpenResult(results.FirstOrDefault());
142+
143+
}
144+
145+
return results;
146+
}
147+
148+
/// <summary>
149+
/// Runs a search of <paramref name="query"/>.
150+
/// </summary>
151+
/// <param name="query">Search query</param>
152+
/// <param name="token">Cancellation token passed to <see cref="BaseSearchEngine.GetResultAsync"/></param>
153+
/// <param name="p"><see cref="IProgress{T}"/></param>
154+
public Task<SearchResult[]> RunSearchAsync(SearchQuery query, CancellationToken? token = null,
155+
[CBN] IProgress<int> p = null)
156+
{
157+
/*if (!ConfigApplied) {
158+
await ApplyConfigAsync();
159+
}
160+
97161
token ??= CancellationToken.None;
98162
99163
var tasks = GetSearchTasks(query, token.Value);
@@ -141,7 +205,9 @@ public async Task<SearchResult[]> RunSearchAsync(SearchQuery query, Cancellation
141205
142206
}
143207
144-
return results;
208+
return results;*/
209+
210+
return RunSearchAsync(new SearchQuery[] { query }, token, p);
145211
}
146212

147213
private void OpenResult(SearchResult result)
@@ -271,5 +337,4 @@ public void Dispose()
271337
ConfigApplied = false;
272338
IsComplete = false;
273339
}
274-
275340
}

0 commit comments

Comments
 (0)