Skip to content

Commit 6937992

Browse files
committed
refactoring
1 parent cb58df3 commit 6937992

7 files changed

Lines changed: 124 additions & 54 deletions

File tree

src/WebExpress.WebIndex.Test/Token/UnitTestIndexAnalyze.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public void Ressource(string culture, string ressource, int count)
7070
var tokens = Fixture.TokenAnalyzer.Analyze(input, CultureInfo.GetCultureInfo(culture));
7171

7272
Assert.Equal(count, tokens.Count());
73+
Assert.DoesNotContain(tokens.Select(x => x.Value), new object[] { "it", "or" });
7374
}
7475
}
7576
}

src/WebExpress.WebIndex.Test/Token/UnitTestIndexPipeStageLowerCase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ public UnitTestIndexPipeStageLowerCase(UnitTestIndexFixtureToken fixture, ITestO
4545
[InlineData("Families", "families", "en")]
4646
public void LowerCase(string term, string normalizeTerm, string cultureString)
4747
{
48+
// preconditions
4849
var culture = CultureInfo.GetCultureInfo(cultureString);
4950
var pipeStage = new IndexPipeStageConverterLowerCase(Fixture.Context);
5051

52+
// test execution
5153
var res = pipeStage.Process(IndexTermTokenizer.Tokenize(term, culture), culture)
5254
.FirstOrDefault();
5355

src/WebExpress.WebIndex.Test/Token/UnitTestIndexPipeStageStopWord.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ public UnitTestIndexPipeStageStopWord(UnitTestIndexFixtureToken fixture, ITestOu
3838
"May the force be with you.",
3939
"may", "the", "be", "with", "you"
4040
)]
41+
[InlineData
42+
(
43+
"en",
44+
"Would you like tea or coffee?",
45+
"would", "you", "like", "or", "coffee"
46+
)]
47+
[InlineData
48+
(
49+
"en",
50+
"If it rains tomorrow, we will stay indoors.",
51+
"if", "it", "we", "will", "stay"
52+
)]
4153
[Theory]
4254
[InlineData
4355
(
@@ -53,17 +65,18 @@ public UnitTestIndexPipeStageStopWord(UnitTestIndexFixtureToken fixture, ITestOu
5365
)]
5466
public void StopWord(string cultureStr, string str, params string[] tokenStr)
5567
{
68+
// preconditions
5669
var culture = CultureInfo.GetCultureInfo(cultureStr);
5770
var pipeStage = new IndexPipeStageFilterStopWord(Fixture.Context);
5871

5972
var token = IndexTermTokenizer.Tokenize(str.ToLower(), culture);
6073

74+
// test execution
6175
var res = pipeStage.Process(token, culture)
6276
.Select(x => x.Value)
6377
.ToList();
6478

6579
Assert.DoesNotContain(tokenStr, res);
66-
Assert.True(token.Count() - (tokenStr?.Length ?? 0) == res.Count);
6780
}
6881
}
6982
}

src/WebExpress.WebIndex/IIndexSchema.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23

34
namespace WebExpress.WebIndex
45
{
@@ -12,6 +13,11 @@ public interface IIndexSchema : IDisposable
1213
/// </summary>
1314
IIndexContext Context { get; }
1415

16+
/// <summary>
17+
/// Return the index field data.
18+
/// </summary>
19+
IEnumerable<IndexFieldData> Fields { get; }
20+
1521
/// <summary>
1622
/// Delete this file from storage.
1723
/// </summary>

src/WebExpress.WebIndex/IndexDocument.cs

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public class IndexDocument<TIndexItem> : IIndexDocument<TIndexItem>
1818
where TIndexItem : IIndexItem
1919
{
2020
private readonly Dictionary<PropertyInfo, IIndexReverse<TIndexItem>> _dict = [];
21-
private readonly List<IndexFieldData> _fields = [];
2221

2322
/// <summary>
2423
/// Event that is triggered when the schema has changed.
@@ -41,9 +40,9 @@ public class IndexDocument<TIndexItem> : IIndexDocument<TIndexItem>
4140
public IndexType IndexType { get; private set; }
4241

4342
/// <summary>
44-
/// Return the index field names.
43+
/// Return the index field data.
4544
/// </summary>
46-
public IEnumerable<IndexFieldData> Fields => _fields;
45+
public IEnumerable<IndexFieldData> Fields => Schema.Fields;
4746

4847
/// <summary>
4948
/// Returns the index context.
@@ -124,11 +123,9 @@ protected virtual void ReBuild(uint capacity)
124123
}
125124
}
126125

127-
_fields.Clear();
128-
_fields.AddRange(GetFieldData(typeof(TIndexItem)));
129126
_dict.Clear();
130127

131-
foreach (var field in _fields)
128+
foreach (var field in Schema.Fields)
132129
{
133130
Add(field);
134131
}
@@ -183,8 +180,7 @@ protected virtual async Task ReBuildAsync(uint capacity)
183180
}
184181
}
185182

186-
var properties = GetFieldData(typeof(TIndexItem));
187-
var tasks = properties.Select(property => Task.Run(() => Add(property)));
183+
var tasks = Schema.Fields.Select(property => Task.Run(() => Add(property)));
188184

189185
await Task.WhenAll(tasks);
190186
}
@@ -580,44 +576,5 @@ private static bool IsNumericType(PropertyInfo property)
580576
type == typeof(float) || type == typeof(double) ||
581577
type == typeof(decimal);
582578
}
583-
584-
/// <summary>
585-
/// Recursively retrieves the field names of the specified type.
586-
/// </summary>
587-
/// <param name="type">The type whose field names to retrieve.</param>
588-
/// <param name="prefix">The prefix to prepend to each field name.</param>
589-
/// <param name="processedTypes">A set of types that have already been processed to avoid circular references.</param>
590-
/// <returns>An enumerable collection of field names.</returns>
591-
private static IEnumerable<IndexFieldData> GetFieldData(Type type, string prefix = "", HashSet<Type> processedTypes = null)
592-
{
593-
processedTypes ??= [];
594-
595-
if (processedTypes.Contains(type))
596-
{
597-
yield break;
598-
}
599-
600-
processedTypes.Add(type);
601-
602-
foreach (var property in type.GetProperties())
603-
{
604-
string propertyName = string.IsNullOrEmpty(prefix) ? property.Name : $"{prefix}.{property.Name}";
605-
606-
yield return new IndexFieldData
607-
{
608-
Name = propertyName,
609-
Type = property.PropertyType,
610-
PropertyInfo = property
611-
};
612-
613-
if (property.PropertyType.IsClass && property.PropertyType != typeof(string))
614-
{
615-
foreach (var subProperty in GetFieldData(property.PropertyType, propertyName, processedTypes))
616-
{
617-
yield return subProperty;
618-
}
619-
}
620-
}
621-
}
622579
}
623580
}

src/WebExpress.WebIndex/Memory/IndexMemorySchema.cs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace WebExpress.WebIndex.Memory
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace WebExpress.WebIndex.Memory
25
{
36
/// <summary>
47
/// Represents a index schema file.
@@ -12,6 +15,11 @@ public class IndexMemorySchema<TIndexItem> : IIndexSchema<TIndexItem>
1215
/// </summary>
1316
public IIndexContext Context { get; private set; }
1417

18+
/// <summary>
19+
/// Return the index field data.
20+
/// </summary>
21+
public IEnumerable<IndexFieldData> Fields => GetFieldData(typeof(TIndexItem));
22+
1523
/// <summary>
1624
/// Initializes a new instance of the <see cref="IndexSchema"/> class.
1725
/// </summary>
@@ -52,7 +60,46 @@ public void Drop()
5260
/// </summary>
5361
public virtual void Dispose()
5462
{
63+
GC.SuppressFinalize(this);
64+
}
65+
66+
/// <summary>
67+
/// Recursively retrieves the field names of the specified type.
68+
/// </summary>
69+
/// <param name="type">The type whose field names to retrieve.</param>
70+
/// <param name="prefix">The prefix to prepend to each field name.</param>
71+
/// <param name="processedTypes">A set of types that have already been processed to avoid circular references.</param>
72+
/// <returns>An enumerable collection of field names.</returns>
73+
private static IEnumerable<IndexFieldData> GetFieldData(Type type, string prefix = "", HashSet<Type> processedTypes = null)
74+
{
75+
processedTypes ??= [];
76+
77+
if (processedTypes.Contains(type))
78+
{
79+
yield break;
80+
}
81+
82+
processedTypes.Add(type);
83+
84+
foreach (var property in type.GetProperties())
85+
{
86+
string propertyName = string.IsNullOrEmpty(prefix) ? property.Name : $"{prefix}.{property.Name}";
87+
88+
yield return new IndexFieldData
89+
{
90+
Name = propertyName,
91+
Type = property.PropertyType,
92+
PropertyInfo = property
93+
};
5594

95+
if (property.PropertyType.IsClass && property.PropertyType != typeof(string))
96+
{
97+
foreach (var subProperty in GetFieldData(property.PropertyType, propertyName, processedTypes))
98+
{
99+
yield return subProperty;
100+
}
101+
}
102+
}
56103
}
57104
}
58105
}

src/WebExpress.WebIndex/Storage/IndexStorageSchema.cs

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Linq;
45
using System.Reflection;
56
using System.Text.Json;
6-
using WebExpress.WebIndex.WebAttribute;
77

88
namespace WebExpress.WebIndex.Storage
99
{
@@ -29,6 +29,11 @@ public class IndexStorageSchema<TIndexItem> : IIndexSchema<TIndexItem>
2929
/// </summary>
3030
public IIndexContext Context { get; private set; }
3131

32+
/// <summary>
33+
/// Return the index field data.
34+
/// </summary>
35+
public IEnumerable<IndexFieldData> Fields => GetFieldData(typeof(TIndexItem));
36+
3237
/// <summary>
3338
/// Initializes a new instance of the class.
3439
/// </summary>
@@ -89,12 +94,12 @@ public void Migrate()
8994
private dynamic GetSchema()
9095
{
9196
var objectType = typeof(TIndexItem);
92-
var fields = objectType.GetProperties().Select(x => new
97+
var fields = Fields.Select(x => new
9398
{
9499
x.Name,
95-
Type = GetType(x),
96-
Ignore = Attribute.IsDefined(x, typeof(IndexIgnoreAttribute)),
97-
Abstract = x.GetGetMethod().IsAbstract
100+
Type = GetType(x.PropertyInfo),
101+
Ignore = !x.Enabled,
102+
Abstract = x.PropertyInfo.GetGetMethod().IsAbstract
98103
});
99104
var schema = new { objectType.Name, Fields = fields };
100105

@@ -170,5 +175,44 @@ public virtual void Dispose()
170175
{
171176
GC.SuppressFinalize(this);
172177
}
178+
179+
/// <summary>
180+
/// Recursively retrieves the field names of the specified type.
181+
/// </summary>
182+
/// <param name="type">The type whose field names to retrieve.</param>
183+
/// <param name="prefix">The prefix to prepend to each field name.</param>
184+
/// <param name="processedTypes">A set of types that have already been processed to avoid circular references.</param>
185+
/// <returns>An enumerable collection of field names.</returns>
186+
private static IEnumerable<IndexFieldData> GetFieldData(Type type, string prefix = "", HashSet<Type> processedTypes = null)
187+
{
188+
processedTypes ??= [];
189+
190+
if (processedTypes.Contains(type))
191+
{
192+
yield break;
193+
}
194+
195+
processedTypes.Add(type);
196+
197+
foreach (var property in type.GetProperties())
198+
{
199+
string propertyName = string.IsNullOrEmpty(prefix) ? property.Name : $"{prefix}.{property.Name}";
200+
201+
yield return new IndexFieldData
202+
{
203+
Name = propertyName,
204+
Type = property.PropertyType,
205+
PropertyInfo = property
206+
};
207+
208+
if (property.PropertyType.IsClass && property.PropertyType != typeof(string))
209+
{
210+
foreach (var subProperty in GetFieldData(property.PropertyType, propertyName, processedTypes))
211+
{
212+
yield return subProperty;
213+
}
214+
}
215+
}
216+
}
173217
}
174218
}

0 commit comments

Comments
 (0)