Skip to content

Commit 21606e5

Browse files
committed
Normalized inference further and added caching of the resolved values (this is per connectionsettings and added a test to confirm this
1 parent 8454c4a commit 21606e5

File tree

6 files changed

+247
-168
lines changed

6 files changed

+247
-168
lines changed

src/Nest/CommonAbstractions/Infer/Field/Field.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class Field : IEquatable<Field>, IUrlParameter
1313
public PropertyInfo Property { get; set; }
1414
public double? Boost { get; set; }
1515

16-
private string ComparisonValue { get; set; }
16+
private object ComparisonValue { get; set; }
1717

1818
public Fields And<T>(Expression<Func<T, object>> field) where T : class =>
1919
new Fields(new [] { this, field });
@@ -57,15 +57,15 @@ public static implicit operator Field(Expression expression)
5757
if (memberExpression == null)
5858
return new Field { Expression = expression, ComparisonValue = expression.ToString() };
5959

60-
return new Field { Expression = expression, ComparisonValue = memberExpression.Member.Name};
60+
return new Field { Expression = expression, ComparisonValue = memberExpression};
6161
}
6262

6363
public static implicit operator Field(PropertyInfo property)
6464
{
6565
return property == null ? null : new Field
6666
{
6767
Property = property,
68-
ComparisonValue = property.Name
68+
ComparisonValue = property
6969
};
7070
}
7171

src/Nest/CommonAbstractions/Infer/Field/FieldResolver.cs

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections;
3+
using System.Collections.Concurrent;
34
using System.Collections.Generic;
45
using System.Collections.ObjectModel;
56
using System.Globalization;
@@ -17,27 +18,48 @@ public class FieldResolver : ExpressionVisitor
1718
{
1819
private readonly IConnectionSettingsValues _settings;
1920

21+
private readonly ConcurrentDictionary<Field, string> Fields = new ConcurrentDictionary<Field, string>();
22+
private readonly ConcurrentDictionary<PropertyName, string> Properties = new ConcurrentDictionary<PropertyName, string>();
23+
2024
public FieldResolver(IConnectionSettingsValues settings)
2125
{
2226
settings.ThrowIfNull(nameof(settings));
2327
this._settings = settings;
2428
}
2529

26-
public string Resolve(Field field) =>
27-
field.IsConditionless() ? null : Resolve(field.Name, field.Expression, field.Property);
30+
public string Resolve(Field field)
31+
{
32+
if (field.IsConditionless()) return null;
33+
if (!field.Name.IsNullOrEmpty()) return field.Name;
34+
string f;
35+
if (this.Fields.TryGetValue(field, out f))
36+
return f;
37+
f = this.Resolve(field.Expression, field.Property);
38+
this.Fields.TryAdd(field, f);
39+
return f;
40+
}
2841

29-
internal string Resolve(PropertyName property) =>
30-
property.IsConditionless() ? null : Resolve(property.Name, property.Expression, property.Property);
42+
internal string Resolve(PropertyName property)
43+
{
3144

32-
private string Resolve(string verbatim, Expression expression, MemberInfo member)
45+
if (property.IsConditionless()) return null;
46+
if (!property.Name.IsNullOrEmpty())
47+
return property.Name;
48+
string f;
49+
if (this.Properties.TryGetValue(property, out f))
50+
return f;
51+
f = this.Resolve(property.Expression, property.Property);
52+
this.Properties.TryAdd(property, f);
53+
return f;
54+
}
55+
56+
private string Resolve(Expression expression, MemberInfo member)
3357
{
34-
var name = !verbatim.IsNullOrEmpty()
35-
? verbatim
36-
: expression != null
37-
? this.ResolveExpression(expression)
38-
: member != null
39-
? this.ResolveMemberInfo(member)
40-
: null;
58+
var name = expression != null
59+
? this.ResolveExpression(expression)
60+
: member != null
61+
? this.ResolveMemberInfo(member)
62+
: null;
4163

4264
if (name == null)
4365
throw new ArgumentException("Could not resolve a property name");
@@ -52,6 +74,10 @@ private string ResolveMemberInfo(MemberInfo info)
5274

5375
var name = info.Name;
5476

77+
IPropertyMapping propertyMapping = null;
78+
if (this._settings.PropertyMappings.TryGetValue(info, out propertyMapping))
79+
return propertyMapping.Name;
80+
5581
var att = ElasticsearchPropertyAttribute.From(info);
5682
if (att != null && !att.Name.IsNullOrEmpty())
5783
return att.Name;

src/Nest/CommonAbstractions/Infer/PropertyName/PropertyName.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,7 @@ public class PropertyName : IEquatable<PropertyName>, IUrlParameter
1212
public Expression Expression { get; set; }
1313
public PropertyInfo Property { get; set; }
1414

15-
private string ComparisonValue;
16-
17-
public static PropertyName Create(string name, double? boost = null)
18-
{
19-
PropertyName propertyName = name;
20-
return propertyName;
21-
}
22-
23-
public static PropertyName Create(Expression expression, double? boost = null)
24-
{
25-
PropertyName propertyName = expression;
26-
return propertyName;
27-
}
15+
private object ComparisonValue;
2816

2917
public static implicit operator PropertyName(string name)
3018
{
@@ -49,7 +37,7 @@ public static implicit operator PropertyName(PropertyInfo property)
4937
return property == null ? null : new PropertyName
5038
{
5139
Property = property,
52-
ComparisonValue = property.Name
40+
ComparisonValue = property
5341
};
5442
}
5543

src/Nest/CommonAbstractions/Infer/TypeName/TypeNameResolver.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
2+
using System.Collections.Concurrent;
23

34
namespace Nest
45
{
56
public class TypeNameResolver
67
{
78
private readonly IConnectionSettingsValues _connectionSettings;
9+
private readonly ConcurrentDictionary<Type, string> TypeNames = new ConcurrentDictionary<Type, string>();
810

911
public TypeNameResolver(IConnectionSettingsValues connectionSettings)
1012
{
@@ -21,14 +23,21 @@ private string ResolveType(Type type)
2123
if (type == null) return null;
2224
string typeName;
2325

26+
if (TypeNames.TryGetValue(type, out typeName))
27+
return typeName;
28+
2429
if (_connectionSettings.DefaultTypeNames.TryGetValue(type, out typeName))
30+
{
31+
TypeNames.TryAdd(type, typeName);
2532
return typeName;
33+
}
2634

2735
var att = ElasticsearchTypeAttribute.From(type);
2836
if (att != null && !att.Name.IsNullOrEmpty())
2937
typeName = att.Name;
3038
else
3139
typeName = _connectionSettings.DefaultTypeNameInferrer(type);
40+
TypeNames.TryAdd(type, typeName);
3241
return typeName;
3342
}
3443

0 commit comments

Comments
 (0)