@@ -8,18 +8,33 @@ namespace Nest
88 [ DebuggerDisplay ( "{DebugDisplay,nq}" ) ]
99 public class IndexName : IEquatable < IndexName > , IUrlParameter
1010 {
11+ private static readonly char [ ] ClusterSeparator = { ':' } ;
12+ internal string DebugDisplay => Type == null ? Name : $ "{ nameof ( IndexName ) } for typeof: { Type ? . Name } ";
13+
14+ //TODO 6.0 make setters private and use constructor
15+ public string Cluster { get ; set ; }
1116 public string Name { get ; set ; }
1217 public Type Type { get ; set ; }
1318
14- internal string DebugDisplay => Type == null ? Name : $ "{ nameof ( IndexName ) } for typeof: { Type ? . Name } ";
19+ public static IndexName From < T > ( ) => typeof ( T ) ;
20+ public static IndexName From < T > ( string clusterName ) => From ( typeof ( T ) , clusterName ) ;
21+ private static IndexName From ( Type t , string clusterName ) => new IndexName { Type = t , Cluster = clusterName } ;
22+
23+ public Indices And < T > ( ) => new Indices ( new [ ] { this , typeof ( T ) } ) ;
24+ public Indices And < T > ( string clusterName ) => new Indices ( new [ ] { this , From ( typeof ( T ) , clusterName ) } ) ;
25+ public Indices And ( IndexName index ) => new Indices ( new [ ] { this , index } ) ;
1526
16- public static implicit operator IndexName ( string typeName ) => typeName . IsNullOrEmpty ( )
17- ? null
18- : new IndexName { Name = typeName . Trim ( ) } ;
27+ private static IndexName Parse ( string indexName )
28+ {
29+ if ( string . IsNullOrWhiteSpace ( indexName ) ) return null ;
30+ var tokens = indexName . Split ( ClusterSeparator , 2 , StringSplitOptions . RemoveEmptyEntries ) ;
31+ return tokens . Length == 1
32+ ? new IndexName { Name = tokens [ 0 ] . Trim ( ) }
33+ : new IndexName { Name = tokens [ 1 ] . Trim ( ) , Cluster = tokens [ 0 ] . Trim ( ) } ;
34+ }
1935
20- public static implicit operator IndexName ( Type type ) => type == null
21- ? null
22- : new IndexName { Type = type } ;
36+ public static implicit operator IndexName ( string indexName ) => Parse ( indexName ) ;
37+ public static implicit operator IndexName ( Type type ) => type == null ? null : new IndexName { Type = type } ;
2338
2439 bool IEquatable < IndexName > . Equals ( IndexName other ) => EqualsMarker ( other ) ;
2540
@@ -33,31 +48,33 @@ public override bool Equals(object obj)
3348
3449 public override int GetHashCode ( )
3550 {
36- if ( this . Name != null )
37- return this . Name . GetHashCode ( ) ;
38- if ( this . Type != null )
39- return this . Type . GetHashCode ( ) ;
40- return 0 ;
51+ unchecked
52+ {
53+ var hashCode = this . Name ? . GetHashCode ( ) ?? this . Type ? . GetHashCode ( ) ?? 0 ;
54+ hashCode = ( hashCode * 397 ) ^ ( this . Cluster ? . GetHashCode ( ) ?? 0 ) ;
55+ return hashCode ;
56+ }
4157 }
4258
4359 public override string ToString ( )
4460 {
4561 if ( ! this . Name . IsNullOrEmpty ( ) )
46- return this . Name ;
47- if ( this . Type != null )
48- return this . Type . Name ;
49- return string . Empty ;
62+ return PrefixClusterName ( this . Name ) ;
63+ return this . Type != null ? PrefixClusterName ( this . Type . Name ) : string . Empty ;
5064 }
65+ private string PrefixClusterName ( string name ) => PrefixClusterName ( this , name ) ;
66+ private static string PrefixClusterName ( IndexName i , string name ) => i . Cluster . IsNullOrEmpty ( ) ? name : $ "{ i . Cluster } :{ name } ";
5167
5268 public bool EqualsString ( string other )
5369 {
54- return ! other . IsNullOrEmpty ( ) && other == this . Name ;
70+ return ! other . IsNullOrEmpty ( ) && other == PrefixClusterName ( this . Name ) ;
5571 }
5672
5773 public bool EqualsMarker ( IndexName other )
5874 {
5975 if ( ! this . Name . IsNullOrEmpty ( ) && other != null && ! other . Name . IsNullOrEmpty ( ) )
60- return EqualsString ( other . Name ) ;
76+ return EqualsString ( PrefixClusterName ( other , other . Name ) ) ;
77+
6178 if ( this . Type != null && other != null && other . Type != null )
6279 return this . GetHashCode ( ) == other . GetHashCode ( ) ;
6380 return false ;
@@ -72,9 +89,5 @@ public string GetString(IConnectionConfigurationValues settings)
7289 return nestSettings . Inferrer . IndexName ( this ) ;
7390 }
7491
75- public static IndexName From < T > ( ) => typeof ( T ) ;
76-
77- public Indices And < T > ( ) => new Indices ( new IndexName [ ] { this , typeof ( T ) } ) ;
78- public Indices And ( IndexName index ) => new Indices ( new IndexName [ ] { this , index } ) ;
7992 }
8093}
0 commit comments