Skip to content

Commit 4f224c9

Browse files
committed
IndexRequest is an odd one it needs a reference to a resolved id in order to determine the right HttpMethod
Made this explicit through ResolvedRouteValues but not too keen to keep this around. ConnectionSettings small refactor, now exposes Id to uniquely identify the instance
1 parent 208351b commit 4f224c9

File tree

10 files changed

+149
-195
lines changed

10 files changed

+149
-195
lines changed

src/Elasticsearch.Net/Configuration/ConnectionConfiguration.cs

Lines changed: 28 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -64,71 +64,42 @@ public abstract class ConnectionConfiguration<T> : IConnectionConfigurationValue
6464
where T : ConnectionConfiguration<T>
6565
{
6666
private readonly IConnection _connection;
67-
6867
private readonly IConnectionPool _connectionPool;
69-
7068
private readonly NameValueCollection _headers = new NameValueCollection();
71-
7269
private readonly NameValueCollection _queryString = new NameValueCollection();
7370
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
74-
71+
private readonly ElasticsearchUrlFormatter _urlFormatter;
72+
7573
private BasicAuthenticationCredentials _basicAuthCredentials;
76-
7774
private X509CertificateCollection _clientCertificates;
7875
private Action<IApiCallDetails> _completedRequestHandler = DefaultCompletedRequestHandler;
79-
8076
private int _connectionLimit;
81-
8277
private TimeSpan? _deadTimeout;
83-
8478
private bool _disableAutomaticProxyDetection = false;
85-
8679
private bool _disableDirectStreaming = false;
87-
8880
private bool _disablePings;
89-
9081
private bool _enableHttpCompression;
91-
9282
private bool _enableHttpPipelining = true;
93-
9483
private TimeSpan? _keepAliveInterval;
95-
9684
private TimeSpan? _keepAliveTime;
97-
9885
private TimeSpan? _maxDeadTimeout;
99-
10086
private int? _maxRetries;
101-
10287
private TimeSpan? _maxRetryTimeout;
103-
10488
private Func<Node, bool> _nodePredicate = DefaultNodePredicate;
10589
private Action<RequestData> _onRequestDataCreated = DefaultRequestDataCreated;
106-
10790
private TimeSpan? _pingTimeout;
108-
10991
private bool _prettyJson;
110-
11192
private string _proxyAddress;
112-
11393
private string _proxyPassword;
114-
11594
private string _proxyUsername;
116-
11795
private TimeSpan _requestTimeout;
118-
11996
private Func<object, X509Certificate, X509Chain, SslPolicyErrors, bool> _serverCertificateValidationCallback;
120-
12197
private IReadOnlyCollection<int> _skipDeserializationForStatusCodes = new ReadOnlyCollection<int>(new int[] { });
122-
12398
private TimeSpan? _sniffLifeSpan;
124-
12599
private bool _sniffOnConnectionFault;
126-
127100
private bool _sniffOnStartup;
128-
129101
private bool _throwExceptions;
130-
131-
private readonly ElasticsearchUrlFormatter _urlFormatter;
102+
private string _uniqueId = Guid.NewGuid().ToString("N");
132103

133104
protected ConnectionConfiguration(IConnectionPool connectionPool, IConnection connection, IElasticsearchSerializer requestResponseSerializer)
134105
{
@@ -147,6 +118,7 @@ protected ConnectionConfiguration(IConnectionPool connectionPool, IConnection co
147118
_urlFormatter = new ElasticsearchUrlFormatter(this);
148119
}
149120

121+
string IConnectionConfigurationValues.Id => _uniqueId;
150122
protected IElasticsearchSerializer UseThisRequestResponseSerializer { get; set; }
151123
BasicAuthenticationCredentials IConnectionConfigurationValues.BasicAuthenticationCredentials => _basicAuthCredentials;
152124
SemaphoreSlim IConnectionConfigurationValues.BootstrapLock => _semaphore;
@@ -205,11 +177,11 @@ private static void DefaultRequestDataCreated(RequestData response) { }
205177

206178
private static bool DefaultNodePredicate(Node node) => true;
207179

208-
private T Assign<TValue>(TValue value, Action<ConnectionConfiguration<T>, TValue> assigner) => Fluent.Assign((T)this, value, assigner);
180+
protected T UpdateId() => Fluent.Assign<T, T, string>((T)this, Guid.NewGuid().ToString("N"), (a, v) => a._uniqueId = v);
181+
182+
protected T Assign<TValue>(TValue value, Action<T, TValue> assigner) => Fluent.Assign((T)this, value, assigner).UpdateId();
209183

210-
/// <summary>
211-
/// The default serializer used to serialize documents to and from JSON
212-
/// </summary>
184+
/// <summary> The default serializer used to serialize documents to and from JSON </summary>
213185
protected virtual IElasticsearchSerializer DefaultSerializer(T settings) => new LowLevelRequestResponseSerializer();
214186

215187
/// <summary>
@@ -225,9 +197,7 @@ public T EnableTcpKeepAlive(TimeSpan keepAliveTime, TimeSpan keepAliveInterval)
225197
Assign(keepAliveTime, (a, v) => a._keepAliveTime = v)
226198
.Assign(keepAliveInterval, (a, v) => a._keepAliveInterval = v);
227199

228-
/// <summary>
229-
/// The maximum number of retries for a given request,
230-
/// </summary>
200+
/// <summary> The maximum number of retries for a given request </summary>
231201
public T MaximumRetries(int maxRetries) => Assign(maxRetries, (a, v) => a._maxRetries = v);
232202

233203
/// <summary>
@@ -341,14 +311,10 @@ public T SniffOnConnectionFault(bool sniffsOnConnectionFault = true) =>
341311
/// <summary>
342312
/// If your connection has to go through proxy, use this method to specify the proxy url
343313
/// </summary>
344-
public T Proxy(Uri proxyAdress, string username, string password)
345-
{
346-
proxyAdress.ThrowIfNull(nameof(proxyAdress));
347-
_proxyAddress = proxyAdress.ToString();
348-
_proxyUsername = username;
349-
_proxyPassword = password;
350-
return (T)this;
351-
}
314+
public T Proxy(Uri proxyAdress, string username, string password) =>
315+
Assign(proxyAdress.ToString(), (a, v) => a._proxyAddress = v)
316+
.Assign(username, (a, v) => a._proxyUsername = v)
317+
.Assign(password, (a, v) => a._proxyPassword = v);
352318

353319
/// <summary>
354320
/// Forces all requests to have ?pretty=true querystring parameter appended,
@@ -422,13 +388,7 @@ public T BasicAuthentication(string userName, string password) =>
422388
/// verbatim.
423389
/// </summary>
424390
/// <param name="predicate">Return true if you want the node to be used for API calls</param>
425-
public T NodePredicate(Func<Node, bool> predicate)
426-
{
427-
if (predicate == null) return (T)this;
428-
429-
_nodePredicate = predicate;
430-
return (T)this;
431-
}
391+
public T NodePredicate(Func<Node, bool> predicate) => Assign(predicate ?? DefaultNodePredicate, (a, v) => a._nodePredicate = predicate);
432392

433393
/// <summary>
434394
/// Turns on settings that aid in debugging like DisableDirectStreaming() and PrettyJson()
@@ -440,22 +400,20 @@ public T NodePredicate(Func<Node, bool> predicate)
440400
/// ConnectionSettings. If no callback is passed, DebugInformation from the response
441401
/// will be written to the debug output by default.
442402
/// </param>
443-
public T EnableDebugMode(Action<IApiCallDetails> onRequestCompleted = null)
444-
{
445-
_disableDirectStreaming = true;
446-
PrettyJson(true);
447-
IncludeServerStackTraceOnError(true);
448-
449-
var originalCompletedRequestHandler = _completedRequestHandler;
450-
var debugCompletedRequestHandler = onRequestCompleted ?? (d => Debug.WriteLine(d.DebugInformation));
451-
_completedRequestHandler = d =>
452-
{
453-
originalCompletedRequestHandler?.Invoke(d);
454-
debugCompletedRequestHandler?.Invoke(d);
455-
};
456-
457-
return (T)this;
458-
}
403+
public T EnableDebugMode(Action<IApiCallDetails> onRequestCompleted = null) =>
404+
PrettyJson()
405+
.IncludeServerStackTraceOnError()
406+
.DisableDirectStreaming()
407+
.Assign(onRequestCompleted, (a, v) =>
408+
{
409+
var originalCompletedRequestHandler = _completedRequestHandler;
410+
var debugCompletedRequestHandler = v ?? (d => Debug.WriteLine(d.DebugInformation));
411+
_completedRequestHandler = d =>
412+
{
413+
originalCompletedRequestHandler?.Invoke(d);
414+
debugCompletedRequestHandler.Invoke(d);
415+
};
416+
});
459417

460418
/// <summary>
461419
/// Register a ServerCertificateValidationCallback, this is called per endpoint until it returns true.

src/Elasticsearch.Net/Configuration/IConnectionConfigurationValues.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ namespace Elasticsearch.Net
99
{
1010
public interface IConnectionConfigurationValues : IDisposable
1111
{
12+
/// <summary> A unique id for this connection settings instance </summary>
13+
string Id { get; }
14+
1215
/// <summary>
1316
/// Basic access authorization credentials to specify with all requests.
1417
/// </summary>

src/Elasticsearch.Net/Extensions/Fluent.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@ namespace Elasticsearch.Net
44
{
55
internal static class Fluent
66
{
7-
[Obsolete("Use the overload that accepts TValue")]
8-
internal static TDescriptor Assign<TDescriptor, TInterface>(TDescriptor self, Action<TInterface> assign)
9-
where TDescriptor : class, TInterface
10-
{
11-
assign(self);
12-
return self;
13-
}
14-
157
internal static TDescriptor Assign<TDescriptor, TInterface, TValue>(TDescriptor self, TValue value, Action<TInterface, TValue> assign)
168
where TDescriptor : class, TInterface
179
{

src/Nest/CommonAbstractions/ConnectionSettings/ConnectionSettingsBase.cs

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@ public abstract class ConnectionSettingsBase<TConnectionSettings> : ConnectionCo
7373
private Func<string, string> _defaultFieldNameInferrer;
7474
private string _defaultIndex;
7575

76-
private string _defaultTypeName;
77-
78-
private Func<Type, string> _defaultTypeNameInferrer;
79-
8076
private HashSet<Type> _disableIdInference = new HashSet<Type>();
8177
private bool _defaultDisableAllInference;
8278

@@ -96,22 +92,17 @@ IPropertyMappingProvider propertyMappingProvider
9692
var serializerAsMappingProvider = _sourceSerializer as IPropertyMappingProvider;
9793
_propertyMappingProvider = propertyMappingProvider ?? serializerAsMappingProvider ?? new PropertyMappingProvider();
9894

99-
_defaultTypeNameInferrer = t => !_defaultTypeName.IsNullOrEmpty() ? _defaultTypeName : t.Name.ToLowerInvariant();
10095
_defaultFieldNameInferrer = p => p.ToCamelCase();
10196
_defaultIndices = new FluentDictionary<Type, string>();
10297
_defaultTypeNames = new FluentDictionary<Type, string>();
10398
_defaultRelationNames = new FluentDictionary<Type, string>();
104-
10599
_inferrer = new Inferrer(this);
106100
}
107101

108102
Func<string, string> IConnectionSettingsValues.DefaultFieldNameInferrer => _defaultFieldNameInferrer;
109103
string IConnectionSettingsValues.DefaultIndex => _defaultIndex;
110104
FluentDictionary<Type, string> IConnectionSettingsValues.DefaultIndices => _defaultIndices;
111105
FluentDictionary<Type, string> IConnectionSettingsValues.DefaultRelationNames => _defaultRelationNames;
112-
string IConnectionSettingsValues.DefaultTypeName => _defaultTypeName;
113-
Func<Type, string> IConnectionSettingsValues.DefaultTypeNameInferrer => _defaultTypeNameInferrer;
114-
FluentDictionary<Type, string> IConnectionSettingsValues.DefaultTypeNames => _defaultTypeNames;
115106
FluentDictionary<Type, string> IConnectionSettingsValues.IdProperties => _idProperties;
116107
Inferrer IConnectionSettingsValues.Inferrer => _inferrer;
117108
IPropertyMappingProvider IConnectionSettingsValues.PropertyMappingProvider => _propertyMappingProvider;
@@ -122,40 +113,14 @@ IPropertyMappingProvider propertyMappingProvider
122113
bool IConnectionSettingsValues.DefaultDisableIdInference => _defaultDisableAllInference;
123114

124115
/// <inheritdoc cref="IConnectionSettingsValues.DefaultIndex"/>
125-
public TConnectionSettings DefaultIndex(string defaultIndex)
126-
{
127-
_defaultIndex = defaultIndex;
128-
return (TConnectionSettings)this;
129-
}
130-
131-
/// <inheritdoc cref="IConnectionSettingsValues.DefaultTypeName"/>
132-
public TConnectionSettings DefaultTypeName(string defaultTypeName)
133-
{
134-
_defaultTypeName = defaultTypeName;
135-
return (TConnectionSettings)this;
136-
}
116+
public TConnectionSettings DefaultIndex(string defaultIndex) => Assign(defaultIndex, (a, v) => a._defaultIndex = v);
137117

138118
/// <inheritdoc cref="IConnectionSettingsValues.DefaultFieldNameInferrer"/>
139-
public TConnectionSettings DefaultFieldNameInferrer(Func<string, string> fieldNameInferrer)
140-
{
141-
_defaultFieldNameInferrer = fieldNameInferrer;
142-
return (TConnectionSettings)this;
143-
}
119+
public TConnectionSettings DefaultFieldNameInferrer(Func<string, string> fieldNameInferrer) =>
120+
Assign(fieldNameInferrer, (a, v) => a._defaultFieldNameInferrer = v);
144121

145122
/// <inheritdoc cref="IConnectionSettingsValues.DisableIdInference"/>
146-
public TConnectionSettings DefaultDisableIdInference(bool disable = true)
147-
{
148-
_defaultDisableAllInference = disable;
149-
return (TConnectionSettings)this;
150-
}
151-
152-
/// <inheritdoc cref="IConnectionSettingsValues.DefaultTypeNameInferrer"/>
153-
public TConnectionSettings DefaultTypeNameInferrer(Func<Type, string> typeNameInferrer)
154-
{
155-
typeNameInferrer.ThrowIfNull(nameof(typeNameInferrer));
156-
_defaultTypeNameInferrer = typeNameInferrer;
157-
return (TConnectionSettings)this;
158-
}
123+
public TConnectionSettings DefaultDisableIdInference(bool disable = true) => Assign(disable, (a, v) => a._defaultDisableAllInference = v);
159124

160125
/// <inheritdoc cref="IConnectionSettingsValues.IdProperties"/>
161126
private void MapIdPropertyFor<TDocument>(Expression<Func<TDocument, object>> objectPath)
@@ -247,7 +212,7 @@ private void ApplyPropertyMappings<TDocument>(IList<IClrPropertyMapping<TDocumen
247212
[Obsolete("Please use " + nameof(DefaultMappingFor))]
248213
public TConnectionSettings InferMappingFor<TDocument>(Func<ClrTypeMappingDescriptor<TDocument>, IClrTypeMapping<TDocument>> selector)
249214
where TDocument : class =>
250-
DefaultMappingFor<TDocument>(selector);
215+
DefaultMappingFor(selector);
251216

252217
/// <inheritdoc cref="InferMappingFor{TDocument}"/>
253218
public TConnectionSettings DefaultMappingFor<TDocument>(Func<ClrTypeMappingDescriptor<TDocument>, IClrTypeMapping<TDocument>> selector)
@@ -266,19 +231,19 @@ public TConnectionSettings DefaultMappingFor<TDocument>(Func<ClrTypeMappingDescr
266231
if (!string.IsNullOrWhiteSpace(inferMapping.IdPropertyName))
267232
_idProperties[inferMapping.ClrType] = inferMapping.IdPropertyName;
268233

269-
if (inferMapping.IdProperty != null)
270-
MapIdPropertyFor<TDocument>(inferMapping.IdProperty);
234+
if (inferMapping.IdProperty != null)
235+
MapIdPropertyFor(inferMapping.IdProperty);
271236

272237
if (inferMapping.RoutingProperty != null)
273-
MapRoutePropertyFor<TDocument>(inferMapping.RoutingProperty);
238+
MapRoutePropertyFor(inferMapping.RoutingProperty);
274239

275240
if (inferMapping.Properties != null)
276-
ApplyPropertyMappings<TDocument>(inferMapping.Properties);
241+
ApplyPropertyMappings(inferMapping.Properties);
277242

278243
if (inferMapping.DisableIdInference) _disableIdInference.Add(inferMapping.ClrType);
279244
else _disableIdInference.Remove(inferMapping.ClrType);
280245

281-
return (TConnectionSettings)this;
246+
return UpdateId();
282247
}
283248

284249
/// <summary>
@@ -300,13 +265,13 @@ public TConnectionSettings DefaultMappingFor(Type documentType, Func<ClrTypeMapp
300265
if (!string.IsNullOrWhiteSpace(inferMapping.IdPropertyName))
301266
_idProperties[inferMapping.ClrType] = inferMapping.IdPropertyName;
302267

303-
return (TConnectionSettings)this;
268+
return UpdateId();
304269
}
305270

306271
/// <inheritdoc cref="DefaultMappingFor(Type, Func{ClrTypeMappingDescriptor,IClrTypeMapping})"/>
307272
public TConnectionSettings DefaultMappingFor(IEnumerable<IClrTypeMapping> typeMappings)
308273
{
309-
if (typeMappings == null) return (TConnectionSettings)this;
274+
if (typeMappings == null) return UpdateId();
310275

311276
foreach (var inferMapping in typeMappings)
312277
{
@@ -320,7 +285,7 @@ public TConnectionSettings DefaultMappingFor(IEnumerable<IClrTypeMapping> typeMa
320285
_defaultRelationNames.Add(inferMapping.ClrType, inferMapping.RelationName);
321286
}
322287

323-
return (TConnectionSettings)this;
288+
return UpdateId();
324289
}
325290
}
326291
}

src/Nest/CommonAbstractions/ConnectionSettings/IConnectionSettingsValues.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,6 @@ public interface IConnectionSettingsValues : IConnectionConfigurationValues
3636
/// </summary>
3737
FluentDictionary<Type, string> DefaultRelationNames { get; }
3838

39-
/// <summary>
40-
/// A default type name to use within Elasticsearch for all CLR types. If <see cref="DefaultTypeNameInferrer" /> is also set, a configured
41-
/// default type name will only be used when <see cref="DefaultTypeNameInferrer" />returns null or empty. If unset, the default type
42-
/// name for types will be the lowercased CLR type name.
43-
/// </summary>
44-
string DefaultTypeName { get; }
45-
46-
/// <summary>
47-
/// Specify how a type name is inferred from a CLR type.
48-
/// By default, a type names is inferred by calling <see cref="string.ToLowerInvariant" /> on the CLR type's name.
49-
/// </summary>
50-
Func<Type, string> DefaultTypeNameInferrer { get; }
51-
52-
FluentDictionary<Type, string> DefaultTypeNames { get; }
53-
5439
/// <summary>
5540
/// Specify a property for a CLR type to use to infer the _id of the document when indexed in Elasticsearch.
5641
/// </summary>

src/Nest/CommonAbstractions/Infer/RelationName/RelationNameResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private string ResolveType(Type type)
3737
if (att != null && !att.RelationName.IsNullOrEmpty())
3838
typeName = att.RelationName;
3939
else
40-
typeName = _connectionSettings.DefaultTypeNameInferrer(type);
40+
typeName = type.Name.ToLowerInvariant();
4141

4242
RelationNames.TryAdd(type, typeName);
4343
return typeName;

0 commit comments

Comments
 (0)