8
8
9
9
namespace Nest
10
10
{
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
+ }
12
17
13
18
public class RouteValues : Dictionary < string , IUrlParameter >
14
19
{
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 ; }
18
24
19
25
internal ResolvedRouteValues Resolve ( IConnectionSettingsValues configurationValues )
20
26
{
21
- var resolved = new ResolvedRouteValues ( ) ;
27
+ if ( Count == 0 ) return ResolvedRouteValues . Empty ;
28
+
29
+ var resolved = new ResolvedRouteValues ( Count ) ;
22
30
foreach ( var kv in this )
23
31
{
24
32
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 ;
26
36
}
27
- Resolved = resolved ;
28
37
return resolved ;
29
38
}
30
39
@@ -33,19 +42,20 @@ private RouteValues Route(string name, IUrlParameter routeValue, bool required =
33
42
switch ( routeValue ) {
34
43
case null when ! required : {
35
44
if ( ! ContainsKey ( name ) ) return this ;
36
-
37
45
Remove ( name ) ;
38
- Resolved = null ; //invalidate cache
46
+ if ( IsId ( name ) ) ContainsId = false ; // invalidate cache
39
47
return this ;
40
48
}
41
49
case null : throw new ArgumentNullException ( name , $ "{ name } is required to build a url to this API") ;
42
50
default :
43
51
this [ name ] = routeValue ;
44
- Resolved = null ; //invalidate cache
52
+ if ( IsId ( name ) ) ContainsId = false ; // invalidate cache
45
53
return this ;
46
54
}
47
55
}
48
56
57
+ private static bool IsId ( string key ) => key . Equals ( "id" , StringComparison . OrdinalIgnoreCase ) ;
58
+
49
59
internal RouteValues Required ( string route , IUrlParameter value ) => Route ( route , value ) ;
50
60
51
61
internal RouteValues Optional ( string route , IUrlParameter value ) => Route ( route , value , false ) ;
0 commit comments