Skip to content

Commit 362e148

Browse files
committed
Experiment refactoring of SearchResultItem type hierarchy
1 parent a89b1da commit 362e148

22 files changed

Lines changed: 320 additions & 387 deletions

SmartImage.Lib/Engines/BaseSearchEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace SmartImage.Lib.Engines;
2323
#pragma warning disable CA1822
2424
#nullable disable
2525

26-
public abstract class BaseSearchEngine : IDisposable, IEquatable<BaseSearchEngine>, ISearchEngine, IUrl
26+
public abstract class BaseSearchEngine : ISearchEngine, IDisposable, IEquatable<BaseSearchEngine>, IUrl
2727
{
2828

2929
protected static readonly ILogger Logger = AppSupport.Factory.CreateLogger(nameof(BaseSearchEngine));

SmartImage.Lib/Engines/Results/SearchResultItem.cs

Lines changed: 43 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ namespace SmartImage.Lib.Engines.Results;
1919

2020
// todo: refactor to not inherit from UniImageUrl and instead contain a UniImageUrl
2121

22-
public class SearchResultItem : UniImageUrl, IComparable<SearchResultItem>, IComparable, IEquatable<SearchResultItem>
22+
public class SearchResultItem : IComparable<SearchResultItem>, IComparable, IEquatable<SearchResultItem>, IResultItem, IUrl
2323
{
2424

25+
#region
26+
2527
/// <summary>
2628
/// Result containing this result item
2729
/// </summary>
@@ -33,12 +35,16 @@ public class SearchResultItem : UniImageUrl, IComparable<SearchResultItem>, ICom
3335
[JI]
3436
public SearchResultItem Parent { get; private set; }
3537

38+
public bool IsCloned { get; private init; }
39+
3640
[MNNW(true, nameof(Parent))]
3741
public bool IsChild => Parent != null && Parent != this;
3842

39-
// [MN]
40-
// [JPN("url")]
41-
// public Url Url { get; protected set; }
43+
#endregion
44+
45+
[MN]
46+
[JPN("url")]
47+
public Url Url { get; protected set; }
4248

4349
/// <summary>
4450
/// Title/caption of this result
@@ -96,6 +102,16 @@ public class SearchResultItem : UniImageUrl, IComparable<SearchResultItem>, ICom
96102
/// </summary>
97103
public DateTime? Time { get; internal set; }
98104

105+
public double? Similarity { get; internal set; }
106+
107+
[MNNW(true, nameof(Similarity))]
108+
public bool HasSimilarity => Similarity.HasValue;
109+
110+
public ulong? Hash { get; internal set; }
111+
112+
[MNNW(true, nameof(Hash))]
113+
public bool HasHash => Hash.HasValue;
114+
99115
/// <summary>
100116
/// Additional metadata.
101117
/// </summary>
@@ -109,9 +125,6 @@ public class SearchResultItem : UniImageUrl, IComparable<SearchResultItem>, ICom
109125
[JI]
110126
public bool IsRaw { get; }
111127

112-
[MNNW(true, nameof(Similarity))]
113-
public bool HasSimilarity => Similarity.HasValue;
114-
115128
public double Score
116129
{
117130
get
@@ -121,9 +134,9 @@ public double Score
121134

122135
var s = 0d;
123136

124-
if (HasImage) {
137+
/*if (HasImage) {
125138
s++;
126-
}
139+
}*/
127140

128141
if (HasHash) {
129142
s++;
@@ -158,8 +171,6 @@ public double Score
158171

159172
#region
160173

161-
public bool IsCloned { get; private init; }
162-
163174
[CBN]
164175
[field: CBN]
165176
public Url Thumbnail
@@ -206,6 +217,12 @@ internal SearchResultItem(SearchResult r, bool isRaw = false) : base(null)
206217

207218
#region
208219

220+
/*public List<SearchResultItem> ScannedItems { get; }
221+
222+
[MNNW(true, nameof(ScannedItems))]
223+
public bool HasScannedItems => ScannedItems is { Count: > 0 };
224+
225+
[MNNW(true, nameof(Image))]
209226
public override async ValueTask<bool> AllocImageAsync(CancellationToken ct = default)
210227
{
211228
if (Url == null) {
@@ -232,9 +249,9 @@ public override async ValueTask<bool> AllocImageAsync(CancellationToken ct = def
232249
else { }
233250
234251
return HasImage;
235-
}
236-
252+
}*/
237253

254+
[MNNW(true, nameof(Thumbnail))]
238255
public async ValueTask<bool> LoadThumbnailAsync(CancellationToken ct = default)
239256
{
240257
if (!HasThumbnail) {
@@ -251,66 +268,6 @@ public async ValueTask<bool> LoadThumbnailAsync(CancellationToken ct = default)
251268
return HasThumbnail;
252269
}
253270

254-
/*public async ValueTask<bool> ScanAsync(CancellationToken ct = default)
255-
{
256-
if (!(await AllocImageAsync(ct))) {
257-
// return false;
258-
}
259-
260-
if (HasImage || HasScannedItems) {
261-
return true;
262-
}
263-
264-
await using var stream = GetStream();
265-
using var sr = new StreamReader(stream);
266-
var str = await sr.ReadToEndAsync(ct);
267-
268-
var hp = new HtmlParser();
269-
var urls = ImageScanner.ParseImageUrlsByRegex(str, Url);
270-
using var doc = await hp.ParseDocumentAsync(str);
271-
var sriNews = new ConcurrentBag<SearchResultItem>();
272-
273-
await Parallel.ForEachAsync(urls, ct, async (s, token) =>
274-
{
275-
var sriNew = new SearchResultItem(Root, false)
276-
{
277-
Parent = this,
278-
Url = s,
279-
Artist = Artist,
280-
Character = Character,
281-
Description = Description,
282-
Title = Title,
283-
Site = Site,
284-
Source = Source,
285-
Time = Time,
286-
IsScanResult = true,
287-
};
288-
289-
// var sriNew = MemberwiseCloneWithUrl(s);
290-
291-
var allocImgOk = await sriNew.AllocImageAsync(token);
292-
293-
if (allocImgOk) {
294-
sriNews.Add(sriNew);
295-
}
296-
else {
297-
sriNew?.Dispose();
298-
}
299-
});
300-
301-
// Root.Results.InsertRange(Root.Results.IndexOf(this), sriNews);
302-
// ScannedItems = sriNews.ToList();
303-
304-
ScannedItems.AddRange(sriNews);
305-
306-
return HasScannedItems;
307-
}*/
308-
309-
public List<SearchResultItem> ScannedItems { get; }
310-
311-
[MNNW(true, nameof(ScannedItems))]
312-
public bool HasScannedItems => ScannedItems is { Count: > 0 };
313-
314271
public async ValueTask<bool> ScanAsync(CancellationToken ct = default)
315272
{
316273
if (HasScannedItems) {
@@ -321,7 +278,7 @@ public async ValueTask<bool> ScanAsync(CancellationToken ct = default)
321278

322279
var task = ScanAsync(cw.Writer, s =>
323280
{
324-
var clone= PartialCopyCloneWithUrl(s);
281+
var clone = PartialCopyCloneWithUrl(s);
325282
return clone;
326283
}, ct);
327284

@@ -351,7 +308,7 @@ public SearchResultItem PartialCopyCloneWithUrl(Url s)
351308
Site = Site,
352309
Source = Source,
353310
Time = Time,
354-
Metadata = Metadata,
311+
Metadata = Metadata,
355312
IsCloned = true,
356313
};
357314
}
@@ -480,49 +437,20 @@ public int CompareTo(object obj)
480437

481438
#endregion
482439

483-
/*public int Find()
484-
{
485-
int i = 0;
486-
487-
if (IsChild) {
488-
i += Parent.ScannedItems.IndexOf(this);
489-
}
490-
491-
var parent = this.Parent ?? this;
492-
493-
for (i = 0; i < parent.Root.Results.Count; i++) {
494-
var res = parent.Root.Results[i];
495-
496-
if (res.IsChild) {
497-
var cSc = res.ScannedItems.IndexOf(this);
498-
i += cSc == -1 ? 0 : cSc;
499-
}
500-
501-
}
440+
public event PropertyChangedEventHandler PropertyChanged;
502441

503-
return i;
442+
protected virtual void OnPropertyChanged([CMN] string propertyName = null)
443+
{
444+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
504445
}
505446

506-
public static int FindSumIndex(SearchResultItem targetItem)
447+
protected virtual bool SetField<T>(ref T field, T value, [CMN] string propertyName = null)
507448
{
508-
if (targetItem == null || targetItem.Root == null || targetItem.Root.Results == null)
509-
throw new ArgumentNullException(nameof(targetItem), "Target item or its root results cannot be null.");
510-
511-
int sumIndex = targetItem.Root.Results.IndexOf(targetItem);
512-
513-
// Iterate through the Results list in the SearchResult
514-
for (int i = 0; i < targetItem.Root.Results.Count; i++) {
515-
var parentItem = targetItem.Root.Results[i];
516-
517-
// Check if the target item is in the ScannedItems of the current parent item
518-
if (parentItem.ScannedItems != null && parentItem.ScannedItems.Contains(targetItem)) {
519-
int scannedIndex = parentItem.ScannedItems.IndexOf(targetItem);
520-
sumIndex = i + scannedIndex;
521-
break;
522-
}
523-
}
524-
525-
return sumIndex;
526-
}*/
449+
if (EqualityComparer<T>.Default.Equals(field, value))
450+
return false;
527451

452+
field = value;
453+
OnPropertyChanged(propertyName);
454+
return true;
455+
}
528456
}

SmartImage.Lib/Engines/Search/FluffleEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public ValueTask<SearchResultItem> ToItem(SearchResult sr)
170170
{
171171
Artist = Credits.FirstOrDefault()?.Name,
172172
Url = Location,
173-
Similarity = Math.Round(Score * 100.0d, 2),
173+
Similarity = Math.Round(Score * 1000.0d, 2),
174174
Metadata = this,
175175
Thumbnail = Thumbnail?.Location,
176176
Site = Platform

0 commit comments

Comments
 (0)