Skip to content

Commit 628681c

Browse files
committed
Refactor SearchClient & P/C model
1 parent 96d29e5 commit 628681c

File tree

8 files changed

+164
-47
lines changed

8 files changed

+164
-47
lines changed

SmartImage.Lib/Images/Uni/UniImage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
using SixLabors.ImageSharp.Processing;
2424
using SmartImage.Lib.Results.Data;
2525

26-
#pragma warning disable CS0168 // Variable is declared but never used
26+
// #pragma warning disable CS0168 // Variable is declared but never used
2727

2828
namespace SmartImage.Lib.Images.Uni;
2929
#nullable disable

SmartImage.Lib/Results/Data/ISimilarity.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace SmartImage.Lib.Results.Data;
55

6-
#pragma warning disable CS0168
6+
// #pragma warning disable CS0168
77
public interface ISimilarity
88
{
99

SmartImage.Lib/SearchClient.cs

Lines changed: 100 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
global using CMN = System.Runtime.CompilerServices.CallerMemberNameAttribute;
1+
global using EC = System.Runtime.CompilerServices.EnumeratorCancellationAttribute;
2+
global using CMN = System.Runtime.CompilerServices.CallerMemberNameAttribute;
23
global using JI = System.Text.Json.Serialization.JsonIgnoreAttribute;
34
global using ICBN = JetBrains.Annotations.ItemCanBeNullAttribute;
45
global using INN = JetBrains.Annotations.ItemNotNullAttribute;
@@ -34,6 +35,7 @@
3435
using SmartImage.Lib.Results.Data;
3536
using static System.Runtime.InteropServices.JavaScript.JSType;
3637
using SmartImage.Lib.Utilities.Diagnostics;
38+
using Kantan.Monad;
3739

3840
namespace SmartImage.Lib;
3941

@@ -62,12 +64,13 @@ public SearchClient(SearchConfig cfg)
6264

6365
}
6466

65-
static SearchClient()
66-
{ }
67+
static SearchClient() { }
6768

6869
[ModuleInitializer]
6970
public static void Init()
7071
{
72+
Trace.AutoFlush = true;
73+
Debug.AutoFlush = true;
7174
s_logger.LogInformation("Init");
7275

7376

@@ -99,23 +102,27 @@ public void OpenChannel()
99102
{
100103
var ok = ResultChannel?.Writer.TryComplete(new ChannelClosedException("Reopened channel"));
101104

102-
if (ok.HasValue && ok.Value) { }
105+
if (ok.HasValue && ok.Value) {
106+
// ...
107+
}
103108

104109
ResultChannel = Channel.CreateUnbounded<SearchResult>(new UnboundedChannelOptions()
105110
{
106111
SingleWriter = true,
112+
107113
});
108114
}
109115

116+
#if OLD
110117
/// <summary>
111118
/// Runs a search of <paramref name="query"/>.
112119
/// </summary>
113120
/// <param name="query">Search query</param>
114121
/// <param name="scheduler"></param>
115122
/// <param name="token">Cancellation token passed to <see cref="WebSearchEngine{T}.GetResultAsync(SmartImage.Lib.SearchQuery,System.Threading.CancellationToken)"/></param>
116-
public async Task<SearchResult[]> RunSearchAsync(SearchQuery query,
117-
TaskScheduler scheduler = default,
118-
CancellationToken token = default)
123+
public async Task<SearchResult[]> RunSearchAsync1(SearchQuery query,
124+
TaskScheduler scheduler = default,
125+
CancellationToken token = default)
119126
{
120127
scheduler ??= TaskScheduler.Default;
121128

@@ -140,12 +147,12 @@ public async Task<SearchResult[]> RunSearchAsync(SearchQuery query,
140147

141148
Debug.WriteLine($"Config: {Config} | {Engines.QuickJoin()}");
142149

143-
List<Task<SearchResult>> tasks = GetSearchTasks(query, scheduler, token).ToList();
150+
var tasks = GetSearchTasks(query, scheduler, token);
144151

145-
var results = new SearchResult[tasks.Count];
146-
int i = 0;
152+
// var results = new SearchResult[tasks.Count];
153+
int i = 0;
147154

148-
while (tasks.Count > 0) {
155+
/*while (tasks.Count > 0) {
149156
if (token.IsCancellationRequested) {
150157
151158
Debugger.Break();
@@ -164,6 +171,19 @@ public async Task<SearchResult[]> RunSearchAsync(SearchQuery query,
164171
165172
results[i] = result;
166173
i++;
174+
}*/
175+
176+
177+
await foreach (var task in Task.WhenEach(tasks).WithCancellation(token)) {
178+
if (token.IsCancellationRequested) {
179+
180+
Debugger.Break();
181+
s_logger.LogWarning("Cancellation requested");
182+
CompleteSearchAsync();
183+
}
184+
185+
var result = await task;
186+
ProcessResult(result);
167187
}
168188

169189
CompleteSearchAsync();
@@ -191,8 +211,60 @@ public async Task<SearchResult[]> RunSearchAsync(SearchQuery query,
191211

192212
return results;
193213
}
214+
#endif
215+
216+
public async IAsyncEnumerable<SearchResult> RunSearchAsync(SearchQuery query,
217+
TaskScheduler scheduler = default,
218+
[EC] CancellationToken token = default)
219+
{
220+
await RunSearchAsync2(query, ResultChannel.Writer, token);
221+
222+
await foreach (var result in ResultChannel.Reader.ReadAllAsync(token)) {
223+
yield return result;
224+
}
225+
}
226+
227+
228+
/// <summary>
229+
/// Runs a search of <paramref name="query"/>.
230+
/// </summary>
231+
public async ValueTask<bool> RunSearchAsync2(SearchQuery query,
232+
ChannelWriter<SearchResult> cw,
233+
CancellationToken token = default)
234+
{
235+
if (!query.IsUploaded) {
236+
throw new SmartImageException($"{query} was not uploaded");
237+
}
238+
239+
var tasks = Engines.Select(e =>
240+
{
241+
var task = e.GetResultAsync(query, token);
242+
return task;
243+
});
244+
245+
await foreach (var task in Task.WhenEach(tasks).WithCancellation(token)) {
246+
247+
var result = await task;
248+
249+
if (task.IsFaulted || task.IsCanceled) {
250+
Trace.WriteLine($"{task} faulted or was canceled");
251+
}
252+
253+
if (cw.TryWrite(result)) {
254+
//
255+
}
256+
257+
if (Config.PriorityEngines.HasFlag(result.Engine.EngineOption)) {
258+
var url = Config.OpenRaw ? result.RawUrl : result.GetBestResult()?.Url;
259+
260+
OpenResult(url);
261+
}
262+
}
263+
264+
return default;
265+
}
194266

195-
public static SearchResultItem GetBest(SearchResult[] results)
267+
public static SearchResultItem GetBest(IEnumerable<SearchResult> results)
196268
{
197269
var ordered = results.Select(x => x.GetBestResult())
198270
.Where(x => x != null)
@@ -259,16 +331,10 @@ public IEnumerable<Task<SearchResult>> GetSearchTasks(SearchQuery query, TaskSch
259331
{
260332
var tasks = Engines.Select(e =>
261333
{
262-
try {
334+
/*try {
263335
Debug.WriteLine($"Starting {e} for {query}");
264336
265-
Task<SearchResult> res = e.GetResultAsync(query, token: token)
266-
.ContinueWith((r) =>
267-
{
268-
ProcessResult(r.Result);
269-
return r.Result;
270337
271-
}, token, TaskContinuationOptions.None, scheduler);
272338
273339
return res;
274340
}
@@ -279,7 +345,19 @@ public IEnumerable<Task<SearchResult>> GetSearchTasks(SearchQuery query, TaskSch
279345
// return Task.FromException(exception);
280346
}
281347
282-
return default;
348+
return default;*/
349+
350+
/*Task<SearchResult> res = e.GetResultAsync(query, token: token)
351+
.ContinueWith((r) =>
352+
{
353+
ProcessResult(r.Result);
354+
return r.Result;
355+
356+
}, token, TaskContinuationOptions.None, scheduler)*/
357+
;
358+
359+
Task<SearchResult> res = e.GetResultAsync(query, token: token);
360+
return res;
283361
});
284362

285363
return tasks;
@@ -306,7 +384,7 @@ public async ValueTask LoadEnginesAsync(CancellationToken token = default)
306384
}
307385

308386
if (Config.FlareSolverr && !FlareSolverrClient.Value.IsInitialized) {
309-
387+
310388

311389
var ok = FlareSolverrClient.Value.Configure(Config.FlareSolverrApiUrl);
312390

@@ -360,4 +438,4 @@ public void Dispose()
360438
ResultChannel?.Writer.Complete();
361439
}
362440

363-
}
441+
}

SmartImage.Lib/SearchServer.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ private async Task<object> HandleRequestAsync(HttpListenerRequest request, HttpL
8181

8282
var url = await sq.UploadAsync();
8383

84-
var results = await Client.RunSearchAsync(sq);
84+
var results1 = Client.RunSearchAsync(sq);
85+
86+
await foreach (var v in results1) {
87+
88+
}
8589

8690
var best = SearchClient.GetBest(results);
8791

SmartImage.Lib/SmartImage.Lib.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<PublishTrimmed>True</PublishTrimmed>
1212
<!-- <PublishSingleFile>true</PublishSingleFile> -->
1313
<PublishSingleFile Condition="'$(Configuration)' == 'Release|AnyCPU'">true</PublishSingleFile>
14-
<NoWarn>AD0001;IDE0290;CS0168</NoWarn>
14+
<NoWarn>AD0001;IDE0290</NoWarn>
1515
</PropertyGroup>
1616
<PropertyGroup>
1717
<SatelliteResourceLanguages>en-US;en</SatelliteResourceLanguages>

SmartImage.UI/MainWindow.Handlers.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,4 +893,10 @@ private void HashItem_OnClick(object sender, RoutedEventArgs e)
893893
e.Handled = true;
894894
}
895895

896+
private void Tb_ProgFolder_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
897+
{
898+
FileSystem.Open(BaseOSIntegration.ExecutableDirectory);
899+
e.Handled=true;
900+
}
901+
896902
}

SmartImage.UI/MainWindow.xaml

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,17 @@
771771
<ProgressBar x:Name="Pb_Preview" Grid.Column="1" HorizontalAlignment="Right" Height="10"
772772
Margin="0,105,174,0" Grid.Row="1" VerticalAlignment="Top" Width="320"
773773
Value="{Binding CurrentResult.PreviewProgress}" />
774+
<Label x:Name="Lb_SelPrevUrl" Content="Preview" HorizontalAlignment="Left" Margin="4,31,0,0"
775+
VerticalAlignment="Top" Height="26" Foreground="White"
776+
Background="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}" Width="55"
777+
HorizontalContentAlignment="Center"
778+
Grid.Row="1" FontWeight="Bold" />
779+
<TextBox x:Name="Tb_SelPrevUrl" HorizontalAlignment="Left" Margin="64,34,0,0"
780+
VerticalAlignment="Top" Width="434" IsReadOnly="True" Height="20" Foreground="White"
781+
Background="Black"
782+
Text="{Binding CurrentResult.Result.Thumbnail, Converter={StaticResource urlConverter}, ElementName=Wnd_Main, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
783+
ToolTip="Preview image URL"
784+
Grid.Row="1" MouseDoubleClick="Tb_SelPrevUrl_MouseDoubleClick" />
774785

775786
<Label x:Name="Lb_SelMainUrl" Content="Item" HorizontalAlignment="Left" Margin="4,59,0,0"
776787
VerticalAlignment="Top" Height="26" Foreground="White"
@@ -817,20 +828,6 @@
817828
ToolTip="Preview image dimensions"
818829
Grid.Row="1" />
819830

820-
<Label x:Name="Lb_SelPrevUrl" Content="Preview" HorizontalAlignment="Left" Margin="4,31,0,0"
821-
VerticalAlignment="Top" Height="26" Foreground="White"
822-
Background="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}" Width="55"
823-
HorizontalContentAlignment="Center"
824-
Grid.Row="1" FontWeight="Bold" />
825-
826-
<TextBox x:Name="Tb_SelPrevUrl" HorizontalAlignment="Left" Margin="64,34,0,0"
827-
VerticalAlignment="Top" Width="434" IsReadOnly="True" Height="20" Foreground="White"
828-
Background="Black"
829-
Text="{Binding CurrentResult.Result.Thumbnail, Converter={StaticResource urlConverter}, ElementName=Wnd_Main, Mode=OneWay,
830-
UpdateSourceTrigger=PropertyChanged}"
831-
ToolTip="Preview image URL"
832-
Grid.Row="1" MouseDoubleClick="Tb_SelPrevUrl_MouseDoubleClick" />
833-
834831
</Grid>
835832
</TabItem>
836833
<TabItem x:Name="Ti_Config" HorizontalAlignment="Center" Height="20" Header="Config"
@@ -918,12 +915,34 @@
918915
<TabItem x:Name="Ti_Info" HorizontalAlignment="Center" Height="20" Header="Info" VerticalAlignment="Center"
919916
Width="54">
920917
<Grid Background="{DynamicResource Black1}">
921-
<Label x:Name="Lb_Version" Content="Current version" HorizontalAlignment="Left" Margin="10,21,0,0"
918+
<Label x:Name="Lb_Version" Content="Current version" HorizontalAlignment="Left" Margin="8,11,0,0"
919+
VerticalAlignment="Top" Height="26" Foreground="White"
920+
Background="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}" Width="100"
921+
HorizontalContentAlignment="Center" FontWeight="Bold" />
922+
<TextBox x:Name="Tb_Version" HorizontalAlignment="Left" Margin="113,14,0,0"
923+
VerticalAlignment="Top" Width="130" IsReadOnly="True" Height="20" Foreground="White"
924+
Background="Black" />
925+
926+
<Label x:Name="Lb_Version2" Content="Latest version" HorizontalAlignment="Left" Margin="8,42,0,0"
927+
VerticalAlignment="Top" Height="26" Foreground="White"
928+
Background="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}" Width="100"
929+
HorizontalContentAlignment="Center" FontWeight="Bold" />
930+
931+
<TextBox x:Name="Tb_Version2" HorizontalAlignment="Left" Margin="113,45,0,0"
932+
VerticalAlignment="Top" Width="130" IsReadOnly="True" Height="20" Foreground="White"
933+
Background="Black" />
934+
<Label x:Name="Lb_ProgFolder" Content="Program folder" HorizontalAlignment="Left" Margin="8,78,0,0"
935+
VerticalAlignment="Top" Height="26" Foreground="White"
936+
Background="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}" Width="100"
937+
HorizontalContentAlignment="Center" FontWeight="Bold" />
938+
<TextBox x:Name="Tb_ProgFolder" HorizontalAlignment="Left" Margin="113,81,0,0"
939+
VerticalAlignment="Top" Width="500" IsReadOnly="True" Height="20" Foreground="White"
940+
Background="Black" MouseDoubleClick="Tb_ProgFolder_OnMouseDoubleClick" TextWrapping="WrapWithOverflow"/>
941+
942+
<!--<Label x:Name="Lb_Version" Content="Current version" HorizontalAlignment="Left" Margin="10,21,0,0"
922943
VerticalAlignment="Top" Foreground="White" FontWeight="Bold" />
923944
<TextBlock x:Name="Tb_Version" HorizontalAlignment="Left" Margin="112,26,0,0" TextWrapping="Wrap"
924-
VerticalAlignment="Top" Foreground="White" Width="115" />
925-
<Button x:Name="Btn_OpenFolder" Content="Open program folder" HorizontalAlignment="Left"
926-
Margin="10,162,0,0" VerticalAlignment="Top" Click="Btn_OpenFolder_Click" />
945+
VerticalAlignment="Top" Foreground="White" Width="115" />-->
927946
<ListView x:Name="Lv_Logs" ItemsSource="{Binding Logs}"
928947
Foreground="White"
929948
Background="{DynamicResource Black2}"
@@ -935,12 +954,17 @@
935954
</GridView>
936955
</ListView.View>
937956
</ListView>
938-
<Label x:Name="Lb_Version2" Content="Latest version" HorizontalAlignment="Left" Margin="10,52,0,0"
957+
958+
<!--<Label x:Name="Lb_Version2" Content="Latest version" HorizontalAlignment="Left" Margin="10,52,0,0"
939959
VerticalAlignment="Top" Foreground="White" FontWeight="Bold" />
940960
<TextBlock x:Name="Tb_Version2" HorizontalAlignment="Left" Margin="103,57,0,0" TextWrapping="Wrap"
941-
VerticalAlignment="Top" Foreground="White" Width="124" />
961+
VerticalAlignment="Top" Foreground="White" Width="124" />-->
962+
942963
<Button x:Name="Btn_OpenWiki" Content="Open wiki" HorizontalAlignment="Left" Margin="10,132,0,0"
943964
VerticalAlignment="Top" Width="70" Click="Btn_OpenWiki_Click" />
965+
<Button x:Name="Btn_StackTrace" Content="Generate stack trace" HorizontalAlignment="Left"
966+
Margin="95,132,0,0"
967+
VerticalAlignment="Top" Width="120" Click="Btn_OpenWiki_Click" />
944968
</Grid>
945969
</TabItem>
946970
</TabControl>

0 commit comments

Comments
 (0)