Skip to content

Commit 46caff6

Browse files
authored
Optimise RouteValues (#5293)
1 parent a6c529b commit 46caff6

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/Nest/CommonAbstractions/Request/RouteValues.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,30 @@ namespace Nest
1010
{
1111
internal class ResolvedRouteValues : Dictionary<string, string>
1212
{
13+
internal static ResolvedRouteValues Empty = new(0);
1314

15+
public ResolvedRouteValues(int size) : base(size) { }
1416
}
1517

1618
public class RouteValues : Dictionary<string, IUrlParameter>
1719
{
18-
// Not too happy with this, only exists because IndexRequest needs a resolved
19-
// id to know if it has to send a PUT or POST.
20-
internal ResolvedRouteValues Resolved { get; private set; }
20+
/// <summary>
21+
/// Used specifically by index requests to determine whether to use PUT or POST.
22+
/// </summary>
23+
internal bool ContainsId { get; private set;}
2124

2225
internal ResolvedRouteValues Resolve(IConnectionSettingsValues configurationValues)
2326
{
24-
var resolved = new ResolvedRouteValues();
27+
if (Count == 0) return ResolvedRouteValues.Empty;
28+
29+
var resolved = new ResolvedRouteValues(Count);
2530
foreach (var kv in this)
2631
{
2732
var value = this[kv.Key].GetString(configurationValues);
28-
if (!value.IsNullOrEmpty()) resolved[kv.Key] = value;
33+
if (value.IsNullOrEmpty()) continue;
34+
resolved[kv.Key] = value;
35+
if (IsId(kv.Key)) ContainsId = true;
2936
}
30-
Resolved = resolved;
3137
return resolved;
3238
}
3339

@@ -36,19 +42,20 @@ private RouteValues Route(string name, IUrlParameter routeValue, bool required =
3642
switch (routeValue) {
3743
case null when !required: {
3844
if (!ContainsKey(name)) return this;
39-
4045
Remove(name);
41-
Resolved = null; //invalidate cache
46+
if (IsId(name)) ContainsId = false; // invalidate cache
4247
return this;
4348
}
4449
case null: throw new ArgumentNullException(name, $"{name} is required to build a url to this API");
4550
default:
4651
this[name] = routeValue;
47-
Resolved = null; //invalidate cache
52+
if (IsId(name)) ContainsId = false; // invalidate cache
4853
return this;
4954
}
5055
}
5156

57+
private static bool IsId(string key) => key.Equals("id", StringComparison.OrdinalIgnoreCase);
58+
5259
internal RouteValues Required(string route, IUrlParameter value) => Route(route, value);
5360

5461
internal RouteValues Optional(string route, IUrlParameter value) => Route(route, value, false);

src/Nest/Document/Single/Index/IndexRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void IProxyRequest.WriteJson(IElasticsearchSerializer sourceSerializer, Stream s
2626
sourceSerializer.Serialize(Document, stream, formatting);
2727

2828
internal static HttpMethod GetHttpMethod(IIndexRequest<TDocument> request) =>
29-
request.Id?.StringOrLongValue != null || (request.RouteValues.Resolved?.ContainsKey("id") ?? false) ? HttpMethod.PUT : HttpMethod.POST;
29+
request.Id?.StringOrLongValue != null || request.RouteValues.ContainsId ? HttpMethod.PUT : HttpMethod.POST;
3030

3131
partial void DocumentFromPath(TDocument document) => Document = document;
3232
}

0 commit comments

Comments
 (0)