Skip to content

Commit 1ba30d2

Browse files
committed
Refactor to remove wrapping types and add back non-generic interfaces for collectors
1 parent 7e54ca0 commit 1ba30d2

13 files changed

Lines changed: 523 additions & 222 deletions

src/Lucene.Net.Grouping/AbstractAllGroupsCollector.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Lucene.Net.Index;
2+
using Lucene.Net.Support;
23
using System.Collections.Generic;
34

45
namespace Lucene.Net.Search.Grouping
@@ -34,7 +35,7 @@ namespace Lucene.Net.Search.Grouping
3435
/// </summary>
3536
/// <typeparam name="TGroupValue"></typeparam>
3637
// ReSharper disable once RedundantExtendsListEntry
37-
public abstract class AbstractAllGroupsCollector<TGroupValue> : ICollector
38+
public abstract class AbstractAllGroupsCollector<TGroupValue> : IAbstractAllGroupsCollector
3839
{
3940
/// <summary>
4041
/// Returns the total number of groups for the executed search.
@@ -88,5 +89,31 @@ public virtual void SetScorer(Scorer scorer)
8889
public abstract void SetNextReader(AtomicReaderContext context);
8990

9091
public virtual bool AcceptsDocsOutOfOrder => true;
92+
93+
#region Explicit interface implementations
94+
95+
ICollection<object> IAbstractAllGroupsCollector.Groups => new CastingCollectionAdapter<TGroupValue, object>(Groups);
96+
97+
#endregion
98+
}
99+
100+
/// <summary>
101+
/// LUCENENET specific interface to provide a non-generic abstraction
102+
/// for <see cref="AbstractAllGroupsCollector{TGroupValue}"/>.
103+
/// </summary>
104+
public interface IAbstractAllGroupsCollector : ICollector
105+
{
106+
/// <summary>
107+
/// Returns the total number of groups for the executed search.
108+
/// </summary>
109+
int GroupCount { get; }
110+
111+
/// <summary>
112+
/// Returns the group values
113+
/// <para />
114+
/// This is an unordered collections of group values. For each group that matched the query there is a <see cref="Util.BytesRef"/>
115+
/// representing a group value.
116+
/// </summary>
117+
ICollection<object> Groups { get; }
91118
}
92119
}

src/Lucene.Net.Grouping/AbstractDistinctValuesCollector.cs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Lucene.Net.Index;
2+
using Lucene.Net.Support;
23
using System.Collections.Generic;
34
using JCG = J2N.Collections.Generic;
45

@@ -32,7 +33,7 @@ namespace Lucene.Net.Search.Grouping
3233
/// The <typeparamref name="TGroupValue"/> type parameter is LUCENENET specific to allow for
3334
/// strongly-typed group values.
3435
/// </remarks>
35-
public abstract class AbstractDistinctValuesCollector<GC, TGroupValue> : ICollector
36+
public abstract class AbstractDistinctValuesCollector<GC, TGroupValue> : IAbstractDistinctValuesCollector
3637
where GC : AbstractDistinctValuesCollector.GroupCount<TGroupValue>
3738
{
3839
/// <summary>
@@ -74,6 +75,16 @@ public virtual void SetScorer(Scorer scorer)
7475
/// </summary>
7576
/// <param name="context">next atomic reader context </param>
7677
public abstract void SetNextReader(AtomicReaderContext context);
78+
79+
#region Explicit interface implementations
80+
81+
/// <summary>
82+
/// LUCENENET specific implementation to provide a non-generic abstraction
83+
/// </summary>
84+
IList<AbstractDistinctValuesCollector.IGroupCount> IAbstractDistinctValuesCollector.Groups
85+
=> new CastingListAdapter<GC, AbstractDistinctValuesCollector.IGroupCount>(Groups);
86+
87+
#endregion
7788
}
7889

7990
/// <summary>
@@ -93,7 +104,7 @@ public static class AbstractDistinctValuesCollector // LUCENENET specific: CA105
93104
/// LUCENENET - removed this class from being a nested class of
94105
/// <see cref="AbstractDistinctValuesCollector{GC, TGroupValue}"/>
95106
/// </remarks>
96-
public abstract class GroupCount<TGroupValue>
107+
public abstract class GroupCount<TGroupValue> : IGroupCount
97108
{
98109
public TGroupValue GroupValue { get; protected set; }
99110
public ISet<TGroupValue> UniqueValues { get; protected set; }
@@ -103,6 +114,37 @@ protected GroupCount(TGroupValue groupValue) // LUCENENET: CA1012: Abstract type
103114
this.GroupValue = groupValue;
104115
this.UniqueValues = new JCG.HashSet<TGroupValue>();
105116
}
117+
118+
#region Explicit interface implementations
119+
120+
object IGroupCount.GroupValue => GroupValue;
121+
122+
ISet<object> IGroupCount.UniqueValues => new CastingSetAdapter<TGroupValue, object>(UniqueValues);
123+
124+
#endregion
106125
}
126+
127+
/// <summary>
128+
/// LUCENENET specific interface to provide a non-generic abstraction
129+
/// for <see cref="GroupCount{TGroupValue}"/>.
130+
/// </summary>
131+
public interface IGroupCount
132+
{
133+
object GroupValue { get; }
134+
135+
ISet<object> UniqueValues { get; }
136+
}
137+
}
138+
139+
/// <summary>
140+
/// LUCENENET specific interface to provide a non-generic abstraction
141+
/// for <see cref="AbstractDistinctValuesCollector{GC, TGroupValue}"/>.
142+
/// </summary>
143+
public interface IAbstractDistinctValuesCollector : ICollector
144+
{
145+
/// <summary>
146+
/// Returns all unique values for each top N group.
147+
/// </summary>
148+
IList<AbstractDistinctValuesCollector.IGroupCount> Groups { get; }
107149
}
108150
}

src/Lucene.Net.Grouping/AbstractFirstPassGroupingCollector.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Lucene.Net.Diagnostics;
22
using Lucene.Net.Index;
3+
using Lucene.Net.Support;
34
using Lucene.Net.Support.Threading;
45
using System;
56
using System.Collections.Generic;
@@ -39,7 +40,7 @@ namespace Lucene.Net.Search.Grouping
3940
/// @lucene.experimental
4041
/// </summary>
4142
/// <typeparam name="TGroupValue"></typeparam>
42-
public abstract class AbstractFirstPassGroupingCollector<TGroupValue> : ICollector
43+
public abstract class AbstractFirstPassGroupingCollector<TGroupValue> : IAbstractFirstPassGroupingCollector
4344
{
4445
private readonly Sort groupSort;
4546
private readonly FieldComparer[] comparers;
@@ -419,5 +420,29 @@ public virtual void SetNextReader(AtomicReaderContext context)
419420
/// <param name="reuse">Optionally a reuse instance to prevent a new instance creation</param>
420421
/// <returns>a copy of the specified group value</returns>
421422
protected abstract TGroupValue CopyDocGroupValue(TGroupValue groupValue, TGroupValue reuse);
423+
424+
#region Explicit interface implementations
425+
426+
ICollection<ISearchGroup> IAbstractFirstPassGroupingCollector.GetTopGroups(int groupOffset, bool fillFields)
427+
=> new CastingCollectionAdapter<SearchGroup<TGroupValue>, ISearchGroup>(GetTopGroups(groupOffset, fillFields));
428+
429+
#endregion
430+
}
431+
432+
/// <summary>
433+
/// LUCENENET specific interface to provide a non-generic abstraction
434+
/// for <see cref="AbstractFirstPassGroupingCollector{TGroupValue}"/>.
435+
/// </summary>
436+
public interface IAbstractFirstPassGroupingCollector : ICollector
437+
{
438+
/// <summary>
439+
/// Returns top groups, starting from offset. This may
440+
/// return null, if no groups were collected, or if the
441+
/// number of unique groups collected is &lt;= offset.
442+
/// </summary>
443+
/// <param name="groupOffset">The offset in the collected groups</param>
444+
/// <param name="fillFields">Whether to fill to <see cref="ISearchGroup.SortValues"/></param>
445+
/// <returns>top groups, starting from offset</returns>
446+
ICollection<ISearchGroup> GetTopGroups(int groupOffset, bool fillFields);
422447
}
423448
}

src/Lucene.Net.Grouping/AbstractSecondPassGroupingCollector.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace Lucene.Net.Search.Grouping
3636
/// @lucene.experimental
3737
/// </summary>
3838
/// <typeparam name="TGroupValue"></typeparam>
39-
public abstract class AbstractSecondPassGroupingCollector<TGroupValue> : ICollector
39+
public abstract class AbstractSecondPassGroupingCollector<TGroupValue> : IAbstractSecondPassGroupingCollector
4040
{
4141
protected readonly IDictionary<TGroupValue, AbstractSecondPassGroupingCollector.SearchGroupDocs<TGroupValue>> m_groupMap;
4242
private readonly int maxDocsPerGroup;
@@ -146,7 +146,12 @@ public virtual TopGroups<TGroupValue> GetTopGroups(int withinGroupOffset)
146146
}
147147

148148

149+
#region Explicit interface implementations
149150

151+
ITopGroups IAbstractSecondPassGroupingCollector.GetTopGroups(int withinGroupOffset)
152+
=> GetTopGroups(withinGroupOffset);
153+
154+
#endregion
150155
}
151156

152157
/// <summary>
@@ -173,4 +178,13 @@ public SearchGroupDocs(TGroupValue groupValue, ITopDocsCollector collector)
173178
}
174179
}
175180
}
181+
182+
/// <summary>
183+
/// LUCENENET specific interface to provide a non-generic abstraction
184+
/// for <see cref="AbstractSecondPassGroupingCollector{TGroupValue}"/>.
185+
/// </summary>
186+
public interface IAbstractSecondPassGroupingCollector : ICollector
187+
{
188+
ITopGroups GetTopGroups(int withinGroupOffset);
189+
}
176190
}

src/Lucene.Net.Grouping/GroupDocs.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace Lucene.Net.Search.Grouping
2525
///
2626
/// @lucene.experimental
2727
/// </summary>
28-
public class GroupDocs<TGroupValue>
28+
public class GroupDocs<TGroupValue> : IGroupDocs
2929
{
3030
/// <summary>
3131
/// The groupField value for all docs in this group; this
@@ -72,5 +72,30 @@ public GroupDocs(float score, float maxScore, int totalHits, ScoreDoc[] scoreDoc
7272
GroupValue = groupValue;
7373
GroupSortValues = groupSortValues;
7474
}
75+
76+
#region Explicit interface implementations
77+
78+
object IGroupDocs.GroupValue => GroupValue;
79+
80+
#endregion
81+
}
82+
83+
/// <summary>
84+
/// LUCENENET specific interface to provide a non-generic abstraction
85+
/// for <see cref="GroupDocs{TGroupValue}"/>.
86+
/// </summary>
87+
public interface IGroupDocs
88+
{
89+
object GroupValue { get; }
90+
91+
float MaxScore { get; }
92+
93+
float Score { get; }
94+
95+
ScoreDoc[] ScoreDocs { get; }
96+
97+
int TotalHits { get; }
98+
99+
object[] GroupSortValues { get; }
75100
}
76101
}

src/Lucene.Net.Grouping/SearchGroup.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace Lucene.Net.Search.Grouping
3030
/// @lucene.experimental
3131
/// </summary>
3232
/// <typeparam name="TGroupValue"></typeparam>
33-
public class SearchGroup<TGroupValue>
33+
public class SearchGroup<TGroupValue> : ISearchGroup
3434
{
3535
/// <summary>
3636
/// The value that defines this group
@@ -79,6 +79,16 @@ public override int GetHashCode()
7979
{
8080
return GroupValue != null ? GroupValue.GetHashCode() : 0;
8181
}
82+
83+
#region Explicit interface implementations
84+
85+
object ISearchGroup.GroupValue
86+
{
87+
get => GroupValue;
88+
set => GroupValue = (TGroupValue)value;
89+
}
90+
91+
#endregion
8292
}
8393

8494
/// <summary>
@@ -473,4 +483,15 @@ public static ICollection<SearchGroup<T>> Merge<T>(IList<ICollection<SearchGroup
473483
}
474484
}
475485
}
486+
487+
/// <summary>
488+
/// LUCENENET specific interface to provide a non-generic abstraction
489+
/// for <see cref="SearchGroup{TGroupValue}"/>.
490+
/// </summary>
491+
public interface ISearchGroup
492+
{
493+
object GroupValue { get; set; }
494+
495+
object[] SortValues { get; set; }
496+
}
476497
}

src/Lucene.Net.Grouping/TopGroups.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using J2N.Collections;
22
using Lucene.Net.Support;
33
using System;
4+
using System.Collections.Generic;
45
using System.Diagnostics.CodeAnalysis;
56

67
namespace Lucene.Net.Search.Grouping
@@ -27,7 +28,7 @@ namespace Lucene.Net.Search.Grouping
2728
///
2829
/// @lucene.experimental
2930
/// </summary>
30-
public class TopGroups<TGroupValue>
31+
public class TopGroups<TGroupValue> : ITopGroups
3132
{
3233
/// <summary>
3334
/// Number of documents matching the search </summary>
@@ -86,6 +87,12 @@ public TopGroups(TopGroups<TGroupValue> oldTopGroups, int? totalGroupCount)
8687
MaxScore = oldTopGroups.MaxScore;
8788
TotalGroupCount = totalGroupCount;
8889
}
90+
91+
#region Explicit interface implementations
92+
93+
IList<IGroupDocs> ITopGroups.Groups => new CastingListAdapter<GroupDocs<TGroupValue>, IGroupDocs>(Groups);
94+
95+
#endregion
8996
}
9097

9198
/// <summary>
@@ -267,4 +274,30 @@ public static TopGroups<T> Merge<T>(TopGroups<T>[] shardGroups, Sort groupSort,
267274
return new TopGroups<T>(groupSort.GetSort(), docSort?.GetSort(), totalHitCount, totalGroupedHitCount, mergedGroupDocs, totalMaxScore);
268275
}
269276
}
277+
278+
/// <summary>
279+
/// LUCENENET specific interface to provide a non-generic abstraction
280+
/// for <see cref="TopGroups{TGroupValue}"/>.
281+
/// </summary>
282+
public interface ITopGroups
283+
{
284+
int TotalHitCount { get; }
285+
286+
int TotalGroupedHitCount { get; }
287+
288+
int? TotalGroupCount { get; }
289+
290+
/// <summary>
291+
/// LUCENENET specific - this uses IList instead of an array
292+
/// as it would require a new array to be created each time
293+
/// the property is accessed.
294+
/// </summary>
295+
IList<IGroupDocs> Groups { get; }
296+
297+
SortField[] GroupSort { get; }
298+
299+
SortField[] WithinGroupSort { get; }
300+
301+
float MaxScore { get; }
302+
}
270303
}

0 commit comments

Comments
 (0)