From 91fc730c2ca17903aeefbd4cb387a6c3f676472a Mon Sep 17 00:00:00 2001 From: Mpdreamz Date: Thu, 1 Mar 2018 10:45:28 -0800 Subject: [PATCH 1/2] fix #3116 instances of gethashcode in equals implementation --- .../CommonAbstractions/Infer/IndexName/IndexName.cs | 9 +++++---- .../CommonAbstractions/Infer/Indices/Indices.cs | 13 ++++++++++--- .../Infer/RelationName/RelationName.cs | 8 +++----- .../CommonAbstractions/Infer/TypeName/TypeName.cs | 4 ++-- src/Nest/CommonAbstractions/Infer/Types/Types.cs | 13 ++++++++++--- .../HighLevel/Inference/IndexNameInference.doc.cs | 2 +- .../Configuration/TestConfigurationBase.cs | 7 +------ .../Configuration/Versions/ElasticsearchVersion.cs | 3 --- src/Tests/Framework/ElasticsearchVersionTests.cs | 7 +------ 9 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/Nest/CommonAbstractions/Infer/IndexName/IndexName.cs b/src/Nest/CommonAbstractions/Infer/IndexName/IndexName.cs index 6f73ab99671..852656cca7f 100644 --- a/src/Nest/CommonAbstractions/Infer/IndexName/IndexName.cs +++ b/src/Nest/CommonAbstractions/Infer/IndexName/IndexName.cs @@ -92,12 +92,13 @@ private bool EqualsString(string other) private bool EqualsMarker(IndexName other) { - if (!this.Name.IsNullOrEmpty() && other != null && !other.Name.IsNullOrEmpty()) + if (other == null) return false; + if (!this.Name.IsNullOrEmpty() && !other.Name.IsNullOrEmpty()) return EqualsString(PrefixClusterName(other,other.Name)); - if (this.Type != null && other != null && other.Type != null) - return this.GetHashCode() == other.GetHashCode(); - return false; + if ((!this.Cluster.IsNullOrEmpty() || !other.Cluster.IsNullOrEmpty()) && this.Cluster != other.Cluster) return false; + + return this.Type != null && other?.Type != null && this.Type == other.Type; } public string GetString(IConnectionConfigurationValues settings) diff --git a/src/Nest/CommonAbstractions/Infer/Indices/Indices.cs b/src/Nest/CommonAbstractions/Infer/Indices/Indices.cs index 1aa61ff880c..fff74a2da9f 100644 --- a/src/Nest/CommonAbstractions/Infer/Indices/Indices.cs +++ b/src/Nest/CommonAbstractions/Infer/Indices/Indices.cs @@ -87,17 +87,24 @@ string IUrlParameter.GetString(IConnectionConfigurationValues settings) public override bool Equals(object obj) { - var other = obj as Indices; - if (other == null) return false; + if (!(obj is Indices other)) return false; return this.Match( all => other.Match(a => true, m => false), many => other.Match( a => false, - m => this.GetHashCode().Equals(other.GetHashCode()) + m => EqualsAllIndices(m.Indices, many.Indices) ) ); } + private static bool EqualsAllIndices(IReadOnlyList indicesCurrent, IReadOnlyList indicesOther) + { + if (indicesCurrent == null && indicesOther == null) return true; + if (indicesCurrent == null || indicesOther == null) return false; + if (indicesCurrent.Count != indicesOther.Count) return false; + return indicesCurrent.Zip(indicesOther, Tuple.Create).All(t=>t.Item1.Equals(t.Item2)); + } + public override int GetHashCode() { return this.Match( diff --git a/src/Nest/CommonAbstractions/Infer/RelationName/RelationName.cs b/src/Nest/CommonAbstractions/Infer/RelationName/RelationName.cs index 1f3f59ad30a..a6d8a417b8c 100644 --- a/src/Nest/CommonAbstractions/Infer/RelationName/RelationName.cs +++ b/src/Nest/CommonAbstractions/Infer/RelationName/RelationName.cs @@ -53,9 +53,7 @@ public override bool Equals(object obj) { var s = obj as string; if (!s.IsNullOrEmpty()) return this.EqualsString(s); - var pp = obj as RelationName; - if (pp != null) return this.EqualsMarker(pp); - + if (obj is RelationName pp) return this.EqualsMarker(pp); return base.Equals(obj); } @@ -70,8 +68,8 @@ public bool EqualsMarker(RelationName other) { if (!this.Name.IsNullOrEmpty() && other != null && !other.Name.IsNullOrEmpty()) return EqualsString(other.Name); - if (this.Type != null && other != null && other.Type != null) - return this.GetHashCode() == other.GetHashCode(); + if (this.Type != null && other?.Type != null) + return this.Type == other.Type; return false; } diff --git a/src/Nest/CommonAbstractions/Infer/TypeName/TypeName.cs b/src/Nest/CommonAbstractions/Infer/TypeName/TypeName.cs index e69c14c1b6f..7413fad440a 100644 --- a/src/Nest/CommonAbstractions/Infer/TypeName/TypeName.cs +++ b/src/Nest/CommonAbstractions/Infer/TypeName/TypeName.cs @@ -66,8 +66,8 @@ private bool EqualsMarker(TypeName other) { if (!this.Name.IsNullOrEmpty() && other != null && !other.Name.IsNullOrEmpty()) return EqualsString(other.Name); - if (this.Type != null && other != null && other.Type != null) - return this.GetHashCode() == other.GetHashCode(); + if (this.Type != null && other?.Type != null) + return this.Type == other.Type; return false; } diff --git a/src/Nest/CommonAbstractions/Infer/Types/Types.cs b/src/Nest/CommonAbstractions/Infer/Types/Types.cs index 348e37192a9..a6c5046579f 100644 --- a/src/Nest/CommonAbstractions/Infer/Types/Types.cs +++ b/src/Nest/CommonAbstractions/Infer/Types/Types.cs @@ -88,17 +88,24 @@ string IUrlParameter.GetString(IConnectionConfigurationValues settings) public override bool Equals(object obj) { - var other = obj as Types; - if (other == null) return false; + if (!(obj is Types other)) return false; return this.Match( all => other.Match(a => true, m => false), many => other.Match( a => false, - m => this.GetHashCode().Equals(other.GetHashCode()) + m => EqualsAllTypes(m.Types, many.Types) ) ); } + private static bool EqualsAllTypes(IReadOnlyList indicesCurrent, IReadOnlyList indicesOther) + { + if (indicesCurrent == null && indicesOther == null) return true; + if (indicesCurrent == null || indicesOther == null) return false; + if (indicesCurrent.Count != indicesOther.Count) return false; + return indicesCurrent.Zip(indicesOther, Tuple.Create).All(t=>t.Item1.Equals(t.Item2)); + } + public override int GetHashCode() { return this.Match( diff --git a/src/Tests/ClientConcepts/HighLevel/Inference/IndexNameInference.doc.cs b/src/Tests/ClientConcepts/HighLevel/Inference/IndexNameInference.doc.cs index c24f763650f..f1a0c283e5e 100644 --- a/src/Tests/ClientConcepts/HighLevel/Inference/IndexNameInference.doc.cs +++ b/src/Tests/ClientConcepts/HighLevel/Inference/IndexNameInference.doc.cs @@ -170,6 +170,7 @@ [U] public void EqualsValidation() { var clusterIndex = (IndexName)"cluster_one:p"; var index = (IndexName)"p"; + Index("cluster_one").Should().NotBe(Index("cluster_two")); clusterIndex.Should().NotBe(index); clusterIndex.Should().Be("cluster_one:p"); @@ -177,7 +178,6 @@ [U] public void EqualsValidation() Index().Should().Be(Index()); Index().Should().NotBe(Index("cluster_two")); - Index("cluster_one").Should().NotBe(Index("cluster_two")); Index("cluster_one").Should().NotBe("cluster_one:project"); Index().Should().NotBe(Index()); Index("cluster_one").Should().NotBe(Index("cluster_one")); diff --git a/src/Tests/Framework/Configuration/TestConfigurationBase.cs b/src/Tests/Framework/Configuration/TestConfigurationBase.cs index 0c4a57fcc17..c92c190989e 100644 --- a/src/Tests/Framework/Configuration/TestConfigurationBase.cs +++ b/src/Tests/Framework/Configuration/TestConfigurationBase.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Tests.Framework.Versions; +using Tests.Framework.Versions; namespace Tests.Framework.Configuration { diff --git a/src/Tests/Framework/Configuration/Versions/ElasticsearchVersion.cs b/src/Tests/Framework/Configuration/Versions/ElasticsearchVersion.cs index 67593abb69e..1ecb608d673 100644 --- a/src/Tests/Framework/Configuration/Versions/ElasticsearchVersion.cs +++ b/src/Tests/Framework/Configuration/Versions/ElasticsearchVersion.cs @@ -1,8 +1,5 @@ using System; using System.Collections.Concurrent; -using System.Net; -using System.Text.RegularExpressions; -using Nest; using Version = SemVer.Version; namespace Tests.Framework.Versions diff --git a/src/Tests/Framework/ElasticsearchVersionTests.cs b/src/Tests/Framework/ElasticsearchVersionTests.cs index d4494356728..08574958cf9 100644 --- a/src/Tests/Framework/ElasticsearchVersionTests.cs +++ b/src/Tests/Framework/ElasticsearchVersionTests.cs @@ -5,10 +5,7 @@ namespace Tests.Framework { public class MockElasticsearchVersionResolver : ElasticsearchVersionResolver { - public MockElasticsearchVersionResolver() - { - - } + public MockElasticsearchVersionResolver() { } public override string LatestSnapshot => "7.3.1-SNAPSHOT"; public override string LatestVersion => "7.3.1"; @@ -18,8 +15,6 @@ public override string SnapshotZipFilename(string version) } } - - public class ElasticsearchVersionTests { public ElasticsearchVersion Create(string version) => ElasticsearchVersion.Create(version, new MockElasticsearchVersionResolver()); From 0798fc0687c21301cfef70b3db3e158d86b04346 Mon Sep 17 00:00:00 2001 From: Mpdreamz Date: Sun, 4 Mar 2018 15:32:14 -0800 Subject: [PATCH 2/2] Equals on types and indices now ignores order and implements == operator --- .../CommonAbstractions/Infer/Indices/Indices.cs | 16 +++++++++------- src/Nest/CommonAbstractions/Infer/Types/Types.cs | 13 ++++++++----- .../Inference/IndexNameInference.doc.cs | 5 +++++ .../Inference/TypesAndRelationsInference.doc.cs | 6 ++++++ 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/Nest/CommonAbstractions/Infer/Indices/Indices.cs b/src/Nest/CommonAbstractions/Infer/Indices/Indices.cs index fff74a2da9f..b399d31c1ff 100644 --- a/src/Nest/CommonAbstractions/Infer/Indices/Indices.cs +++ b/src/Nest/CommonAbstractions/Infer/Indices/Indices.cs @@ -74,8 +74,7 @@ string IUrlParameter.GetString(IConnectionConfigurationValues settings) all => "_all", many => { - var nestSettings = settings as IConnectionSettingsValues; - if (nestSettings == null) + if (!(settings is IConnectionSettingsValues nestSettings)) throw new Exception("Tried to pass index names on querysting but it could not be resolved because no nest settings are available"); var infer = nestSettings.Inferrer; @@ -85,6 +84,10 @@ string IUrlParameter.GetString(IConnectionConfigurationValues settings) ); } + public static bool operator ==(Indices left, Indices right) => Equals(left, right); + + public static bool operator !=(Indices left, Indices right) => !Equals(left, right); + public override bool Equals(object obj) { if (!(obj is Indices other)) return false; @@ -97,12 +100,11 @@ public override bool Equals(object obj) ); } - private static bool EqualsAllIndices(IReadOnlyList indicesCurrent, IReadOnlyList indicesOther) + private static bool EqualsAllIndices(IReadOnlyList thisIndices, IReadOnlyList otherIndices) { - if (indicesCurrent == null && indicesOther == null) return true; - if (indicesCurrent == null || indicesOther == null) return false; - if (indicesCurrent.Count != indicesOther.Count) return false; - return indicesCurrent.Zip(indicesOther, Tuple.Create).All(t=>t.Item1.Equals(t.Item2)); + if (thisIndices == null && otherIndices == null) return true; + if (thisIndices == null || otherIndices == null) return false; + return thisIndices.Count == otherIndices.Count && !thisIndices.Except(otherIndices).Any(); } public override int GetHashCode() diff --git a/src/Nest/CommonAbstractions/Infer/Types/Types.cs b/src/Nest/CommonAbstractions/Infer/Types/Types.cs index a6c5046579f..f18d79be311 100644 --- a/src/Nest/CommonAbstractions/Infer/Types/Types.cs +++ b/src/Nest/CommonAbstractions/Infer/Types/Types.cs @@ -85,6 +85,9 @@ string IUrlParameter.GetString(IConnectionConfigurationValues settings) ); } + public static bool operator ==(Types left, Types right) => Equals(left, right); + + public static bool operator !=(Types left, Types right) => !Equals(left, right); public override bool Equals(object obj) { @@ -98,12 +101,12 @@ public override bool Equals(object obj) ); } - private static bool EqualsAllTypes(IReadOnlyList indicesCurrent, IReadOnlyList indicesOther) + private static bool EqualsAllTypes(IReadOnlyList thisTypes, IReadOnlyList otherTypes) { - if (indicesCurrent == null && indicesOther == null) return true; - if (indicesCurrent == null || indicesOther == null) return false; - if (indicesCurrent.Count != indicesOther.Count) return false; - return indicesCurrent.Zip(indicesOther, Tuple.Create).All(t=>t.Item1.Equals(t.Item2)); + if (thisTypes == null && otherTypes == null) return true; + if (thisTypes == null || otherTypes == null) return false; + if (thisTypes.Count != otherTypes.Count) return false; + return thisTypes.Count == otherTypes.Count && !thisTypes.Except(otherTypes).Any(); } public override int GetHashCode() diff --git a/src/Tests/ClientConcepts/HighLevel/Inference/IndexNameInference.doc.cs b/src/Tests/ClientConcepts/HighLevel/Inference/IndexNameInference.doc.cs index f1a0c283e5e..357da5da6d7 100644 --- a/src/Tests/ClientConcepts/HighLevel/Inference/IndexNameInference.doc.cs +++ b/src/Tests/ClientConcepts/HighLevel/Inference/IndexNameInference.doc.cs @@ -181,6 +181,11 @@ [U] public void EqualsValidation() Index("cluster_one").Should().NotBe("cluster_one:project"); Index().Should().NotBe(Index()); Index("cluster_one").Should().NotBe(Index("cluster_one")); + + Nest.Indices indices1 = "foo,bar"; + Nest.Indices indices2 = "bar,foo"; + indices1.Should().Be(indices2); + (indices1 == indices2).Should().BeTrue(); } //hide diff --git a/src/Tests/ClientConcepts/HighLevel/Inference/TypesAndRelationsInference.doc.cs b/src/Tests/ClientConcepts/HighLevel/Inference/TypesAndRelationsInference.doc.cs index 1b12d2dc251..37bcbf92ab3 100644 --- a/src/Tests/ClientConcepts/HighLevel/Inference/TypesAndRelationsInference.doc.cs +++ b/src/Tests/ClientConcepts/HighLevel/Inference/TypesAndRelationsInference.doc.cs @@ -129,6 +129,12 @@ [U] public void EqualsValidation() Relation().Should().Be(Relation()); Relation().Should().NotBe(Relation()); + + + Nest.Types types1 = "foo,bar"; + Nest.Types types2 = "bar,foo"; + types1.Should().Be(types2); + (types1 == types2).Should().BeTrue(); } //hide