Skip to content

Commit 68c14c3

Browse files
committed
fix id properties configured on connection settings not utilizing idlook delegate cache
1 parent ded5868 commit 68c14c3

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

src/Nest/ExposedInternals/ElasticInferrer.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public string DefaultIndex
4747
public ElasticInferrer(IConnectionSettingsValues connectionSettings)
4848
{
4949
this._connectionSettings = connectionSettings;
50-
this.IdResolver = new IdResolver();
50+
this.IdResolver = new IdResolver(this._connectionSettings);
5151
this.IndexNameResolver = new IndexNameResolver(this._connectionSettings);
5252
this.TypeNameResolver = new TypeNameResolver(this._connectionSettings);
5353
this.PropertyNameResolver = new PropertyNameResolver(this._connectionSettings);
@@ -128,11 +128,6 @@ public string Id<T>(T obj) where T : class
128128
{
129129
if (obj == null) return null;
130130

131-
string idProperty;
132-
this._connectionSettings.IdProperties.TryGetValue(typeof(T), out idProperty);
133-
if (!idProperty.IsNullOrEmpty())
134-
return this.IdResolver.GetIdFor(obj, idProperty);
135-
136131
return this.IdResolver.GetIdFor(obj);
137132
}
138133

src/Nest/Resolvers/IdResolver.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@ namespace Nest.Resolvers
88
{
99
public class IdResolver
1010
{
11+
private readonly IConnectionSettingsValues _connectionSettings;
1112
private static ConcurrentDictionary<Type, Func<object, string>> IdDelegates = new ConcurrentDictionary<Type, Func<object, string>>();
1213
private static MethodInfo MakeDelegateMethodInfo = typeof(IdResolver).GetMethod("MakeDelegate", BindingFlags.Static | BindingFlags.NonPublic);
1314

15+
public IdResolver(IConnectionSettingsValues connectionSettings)
16+
{
17+
_connectionSettings = connectionSettings;
18+
}
19+
20+
1421
internal Func<T, string> CreateIdSelector<T>() where T : class
1522
{
1623
Func<T, string> idSelector = (@object) => this.GetIdFor(@object);
@@ -24,6 +31,7 @@ internal static Func<T, object> MakeDelegate<T, U>(MethodInfo @get)
2431
return t => f(t);
2532
}
2633

34+
[Obsolete("Scheduled to be removed in 2.0, is not cached and not used internally")]
2735
public string GetIdFor<T>(T @object, string idProperty)
2836
{
2937
try
@@ -81,26 +89,31 @@ private PropertyInfo GetInferredId(Type type)
8189
{
8290
// if the type specifies through ElasticAttribute what the id prop is
8391
// use that no matter what
92+
93+
string propertyName;
94+
this._connectionSettings.IdProperties.TryGetValue(type, out propertyName);
95+
if (!propertyName.IsNullOrEmpty())
96+
return GetPropertyCaseInsensitive(type, propertyName);
97+
8498
var esTypeAtt = ElasticAttributes.Type(type);
8599
if (esTypeAtt != null && !string.IsNullOrWhiteSpace(esTypeAtt.IdProperty))
86100
return GetPropertyCaseInsensitive(type, esTypeAtt.IdProperty);
87101

88-
var propertyName = "Id";
89-
102+
propertyName = "Id";
90103
//Try Id on its own case insensitive
91104
var idProperty = GetPropertyCaseInsensitive(type, propertyName);
92105
if (idProperty != null)
93106
return idProperty;
94107

108+
//TODO remove in 2.0 ?
95109
//Try TypeNameId case insensitive
96110
idProperty = GetPropertyCaseInsensitive(type, type.Name + propertyName);
97111
if (idProperty != null)
98112
return idProperty;
99113

114+
//TODO remove in 2.0 ?
100115
//Try TypeName_Id case insensitive
101116
idProperty = GetPropertyCaseInsensitive(type, type.Name + "_" + propertyName);
102-
if (idProperty != null)
103-
return idProperty;
104117

105118
return idProperty;
106119
}

src/Tests/Nest.Tests.Unit/Internals/Inferno/IdLookupTests.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ namespace Nest.Tests.Unit.Internals.Inferno
88
[TestFixture]
99
public class IdLookupTests
1010
{
11+
private IdResolver _idResolver;
12+
13+
public IdLookupTests()
14+
{
15+
this._idResolver = new IdResolver(new ConnectionSettings());
16+
}
17+
1118
[ElasticType(IdProperty = "Guid")]
1219
internal class AlternateIdClass
1320
{
@@ -35,60 +42,60 @@ internal class InheritedIdClass : BaseIdClass { public string Name { get; set; }
3542
public void TestAlternateIdLookup()
3643
{
3744
var expectedGuid = Guid.NewGuid();
38-
var id = new IdResolver().GetIdFor(new AlternateIdClass { Guid = expectedGuid });
45+
var id = this._idResolver.GetIdFor(new AlternateIdClass { Guid = expectedGuid });
3946
StringAssert.AreEqualIgnoringCase(expectedGuid.ToString(), id);
4047
}
4148

4249
[Test]
4350
public void TestIntLookup()
4451
{
4552
var expected = 12;
46-
var id = new IdResolver().GetIdFor(new IntIdClass { Id = expected });
53+
var id = this._idResolver.GetIdFor(new IntIdClass { Id = expected });
4754
StringAssert.AreEqualIgnoringCase(expected.ToString(CultureInfo.InvariantCulture), id);
4855
}
4956
[Test]
5057
public void TestDecimalLookup()
5158
{
5259
var expected = 12m;
53-
var id = new IdResolver().GetIdFor(new DecimalIdClass { Id = expected });
60+
var id = this._idResolver.GetIdFor(new DecimalIdClass { Id = expected });
5461
StringAssert.AreEqualIgnoringCase(expected.ToString(CultureInfo.InvariantCulture), id);
5562
}
5663
[Test]
5764
public void TestFloatLookup()
5865
{
5966
var expected = 12f;
60-
var id = new IdResolver().GetIdFor(new FloatIdClass { Id = expected });
67+
var id = this._idResolver.GetIdFor(new FloatIdClass { Id = expected });
6168
StringAssert.AreEqualIgnoringCase(expected.ToString(CultureInfo.InvariantCulture), id);
6269
}
6370
[Test]
6471
public void TestLongLookup()
6572
{
6673
var expected = long.MaxValue;
67-
var id = new IdResolver().GetIdFor(new LongIdClass { Id = expected });
74+
var id = this._idResolver.GetIdFor(new LongIdClass { Id = expected });
6875
StringAssert.AreEqualIgnoringCase(expected.ToString(CultureInfo.InvariantCulture), id);
6976
}
7077

7178
[Test]
7279
public void TestDoubleLookup()
7380
{
7481
var expected = 12d;
75-
var id = new IdResolver().GetIdFor(new DoubleIdClass { Id = expected });
82+
var id = this._idResolver.GetIdFor(new DoubleIdClass { Id = expected });
7683
StringAssert.AreEqualIgnoringCase(expected.ToString(CultureInfo.InvariantCulture), id);
7784
}
7885
[Test]
7986
public void TestCustomLookup()
8087
{
8188
var expected = new MyCustomClass();
82-
var id = new IdResolver().GetIdFor(new CustomObjectIdClass { Id = expected });
89+
var id = this._idResolver.GetIdFor(new CustomObjectIdClass { Id = expected });
8390
StringAssert.AreEqualIgnoringCase(expected.ToString(), id);
8491
}
8592

8693
[Test]
8794
public void TestInheritedLookup()
8895
{
8996
var expected = new InheritedIdClass() { Id = 123 };
90-
var id = new IdResolver().GetIdFor(expected);
91-
id = new IdResolver().GetIdFor(expected);
97+
var id = this._idResolver.GetIdFor(expected);
98+
id = this._idResolver.GetIdFor(expected);
9299
Assert.AreEqual(expected.Id.ToString(), id);
93100
}
94101

@@ -97,8 +104,8 @@ public void TestInheritedLookup()
97104
public void TestHitsCache()
98105
{
99106
var expected = 12m;
100-
var id = new IdResolver().GetIdFor(new DecimalIdClass { Id = expected });
101-
id = new IdResolver().GetIdFor(new DecimalIdClass { Id = expected });
107+
var id = this._idResolver.GetIdFor(new DecimalIdClass { Id = expected });
108+
id = this._idResolver.GetIdFor(new DecimalIdClass { Id = expected });
102109
StringAssert.AreEqualIgnoringCase(expected.ToString(), id);
103110
}
104111
}

0 commit comments

Comments
 (0)