Skip to content

Commit e758583

Browse files
authored
Optimise RouteValues (#5293) (#5300)
(cherry picked from commit 46caff6)
1 parent acad200 commit e758583

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/Nest/CommonAbstractions/Request/RouteValues.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,32 @@
88

99
namespace Nest
1010
{
11-
internal class ResolvedRouteValues : Dictionary<string, string> { }
11+
internal class ResolvedRouteValues : Dictionary<string, string>
12+
{
13+
internal static ResolvedRouteValues Empty = new(0);
14+
15+
public ResolvedRouteValues(int size) : base(size) { }
16+
}
1217

1318
public class RouteValues : Dictionary<string, IUrlParameter>
1419
{
15-
// Not too happy with this, only exists because IndexRequest needs a resolved
16-
// id to know if it has to send a PUT or POST.
17-
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;}
1824

1925
internal ResolvedRouteValues Resolve(IConnectionSettingsValues configurationValues)
2026
{
21-
var resolved = new ResolvedRouteValues();
27+
if (Count == 0) return ResolvedRouteValues.Empty;
28+
29+
var resolved = new ResolvedRouteValues(Count);
2230
foreach (var kv in this)
2331
{
2432
var value = this[kv.Key].GetString(configurationValues);
25-
if (!value.IsNullOrEmpty()) resolved[kv.Key] = value;
33+
if (value.IsNullOrEmpty()) continue;
34+
resolved[kv.Key] = value;
35+
if (IsId(kv.Key)) ContainsId = true;
2636
}
27-
Resolved = resolved;
2837
return resolved;
2938
}
3039

@@ -33,19 +42,20 @@ private RouteValues Route(string name, IUrlParameter routeValue, bool required =
3342
switch (routeValue) {
3443
case null when !required: {
3544
if (!ContainsKey(name)) return this;
36-
3745
Remove(name);
38-
Resolved = null; //invalidate cache
46+
if (IsId(name)) ContainsId = false; // invalidate cache
3947
return this;
4048
}
4149
case null: throw new ArgumentNullException(name, $"{name} is required to build a url to this API");
4250
default:
4351
this[name] = routeValue;
44-
Resolved = null; //invalidate cache
52+
if (IsId(name)) ContainsId = false; // invalidate cache
4553
return this;
4654
}
4755
}
4856

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

5161
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(ITransportSerializer sourceSerializer, Stream strea
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)