Skip to content

Commit 6d6c444

Browse files
committed
fixed concurrency issues
1 parent 68c14c3 commit 6d6c444

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

src/Nest/Resolvers/IdResolver.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Nest.Resolvers
99
public class IdResolver
1010
{
1111
private readonly IConnectionSettingsValues _connectionSettings;
12+
private ConcurrentDictionary<Type, Func<object, string>> LocalIdDelegates = new ConcurrentDictionary<Type, Func<object, string>>();
1213
private static ConcurrentDictionary<Type, Func<object, string>> IdDelegates = new ConcurrentDictionary<Type, Func<object, string>>();
1314
private static MethodInfo MakeDelegateMethodInfo = typeof(IdResolver).GetMethod("MakeDelegate", BindingFlags.Static | BindingFlags.NonPublic);
1415

@@ -56,7 +57,14 @@ public string GetIdFor<T>(T @object)
5657

5758
var type = typeof(T);
5859
Func<object, string> cachedLookup;
59-
if (IdDelegates.TryGetValue(type, out cachedLookup))
60+
string propertyName;
61+
62+
var preferLocal = this._connectionSettings.IdProperties.TryGetValue(type, out propertyName);
63+
64+
if (LocalIdDelegates.TryGetValue(type, out cachedLookup))
65+
return cachedLookup(@object);
66+
67+
if (!preferLocal && IdDelegates.TryGetValue(type, out cachedLookup))
6068
return cachedLookup(@object);
6169

6270
var idProperty = GetInferredId(type);
@@ -75,7 +83,10 @@ public string GetIdFor<T>(T @object)
7583
var v = func(obj);
7684
return v != null ? v.ToString() : null;
7785
};
78-
IdDelegates.TryAdd(type, cachedLookup);
86+
if (preferLocal)
87+
LocalIdDelegates.TryAdd(type, cachedLookup);
88+
else
89+
IdDelegates.TryAdd(type, cachedLookup);
7990
return cachedLookup(@object);
8091
}
8192
catch

src/Nest/Resolvers/TypeNameResolver.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ namespace Nest.Resolvers
77
public class TypeNameResolver
88
{
99
private readonly IConnectionSettingsValues _connectionSettings;
10-
private PropertyNameResolver _propertyNameResolver;
1110

1211
public TypeNameResolver(IConnectionSettingsValues connectionSettings)
1312
{
1413
connectionSettings.ThrowIfNull("connectionSettings");
1514
this._connectionSettings = connectionSettings;
16-
this._propertyNameResolver = new PropertyNameResolver(this._connectionSettings);
1715
}
1816

1917
public string GetTypeNameFor<T>()

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public void IdPropertyMapped_And_TypeHasIdPropertyAttribute_MappingTakesPreceden
118118
};
119119

120120
Assert.AreEqual(doc.Id, client.Infer.Id<IdPropertyTestWithAttribute>(doc));
121+
Assert.AreEqual(doc.Id, client.Infer.Id<IdPropertyTestWithAttribute>(doc));
121122
}
122123
}
123124
}

0 commit comments

Comments
 (0)