Skip to content

Commit f46cedf

Browse files
Mpdreamzgmarz
authored andcommitted
fix #1029, doing string.GetHashCode comparisons *sigh*
1 parent 528fa63 commit f46cedf

File tree

8 files changed

+157
-53
lines changed

8 files changed

+157
-53
lines changed

build/build.fsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,6 @@ let getAssemblyVersion = (fun _ ->
170170
assemblyVersion
171171
)
172172

173-
174-
175-
176173
Target "Version" (fun _ ->
177174
let assemblyVersion = getAssemblyVersion()
178175

src/Nest/Domain/Marker/IndexNameMarker.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ public bool EqualsString(string other)
5353

5454
public bool EqualsMarker(IndexNameMarker other)
5555
{
56-
return other != null && this.GetHashCode() == other.GetHashCode();
56+
if (!this.Name.IsNullOrEmpty() && other != null && !other.Name.IsNullOrEmpty())
57+
return EqualsString(other.Name);
58+
if (this.Type != null && other != null && other.Type != null)
59+
return this.GetHashCode() == other.GetHashCode();
60+
return false;
5761
}
5862
}
5963
}

src/Nest/Domain/Marker/PropertyNameMarker.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ public override int GetHashCode()
6565
}
6666
public bool EqualsMarker(PropertyNameMarker other)
6767
{
68-
return other != null && this.GetHashCode() == other.GetHashCode();
68+
if (!this.Name.IsNullOrEmpty() && other != null && !other.Name.IsNullOrEmpty())
69+
return EqualsString(other.Name);
70+
if (this.Type != null && other != null && other.Type != null)
71+
return this.GetHashCode() == other.GetHashCode();
72+
return false;
73+
6974
}
7075
public bool EqualsString(string other)
7176
{

src/Nest/Domain/Marker/PropertyPathMarker.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ public override bool Equals(object obj)
6161

6262
public bool EqualsMarker(PropertyPathMarker other)
6363
{
64-
return other != null && this.GetHashCode() == other.GetHashCode();
64+
if (!this.Name.IsNullOrEmpty() && other != null && !other.Name.IsNullOrEmpty())
65+
return EqualsString(other.Name);
66+
if (this.Type != null && other != null && other.Type != null)
67+
return this.GetHashCode() == other.GetHashCode();
68+
return false;
69+
6570
}
6671
public bool EqualsString(string other)
6772
{

src/Nest/Domain/Marker/TypeNameMarker.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ public override bool Equals(object obj)
5959

6060
public bool EqualsMarker(TypeNameMarker other)
6161
{
62-
return other != null && this.GetHashCode() == other.GetHashCode();
62+
if (!this.Name.IsNullOrEmpty() && other != null && !other.Name.IsNullOrEmpty())
63+
return EqualsString(other.Name);
64+
if (this.Type != null && other != null && other.Type != null)
65+
return this.GetHashCode() == other.GetHashCode();
66+
return false;
6367
}
6468

6569
public bool EqualsString(string other)

src/Tests/Nest.Tests.Integration/Cluster/GetSettingsTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ public void GetSettings()
1414
.Add("discovery.zen.minimum_master_nodes", 1)
1515
)
1616
);
17-
17+
1818
var r = this.Client.ClusterGetSettings(p=>p
1919
.FlatSettings()
2020
);
2121
Assert.True(r.IsValid);
2222
Assert.AreEqual(r.Transient["discovery.zen.minimum_master_nodes"], "1");
2323
}
24-
24+
2525
}
26-
}
26+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Elasticsearch.Net;
4+
using FluentAssertions;
5+
using NUnit.Framework;
6+
using System.Linq.Expressions;
7+
using Newtonsoft.Json;
8+
9+
namespace Nest.Tests.Unit.Internals.Inferno
10+
{
11+
[TestFixture]
12+
public class HashCodeCollisionTests : BaseJsonTests
13+
{
14+
[Test]
15+
public void PropertyNameMarkerEqualsDoesNotUseHashCode()
16+
{
17+
var collision = GetHashCollison();
18+
19+
var propertyMarker1 = new PropertyNameMarker { Name = collision.Item1 };
20+
var propertyMarker2 = new PropertyNameMarker { Name = collision.Item2 };
21+
this.TestAddingToDictionary(propertyMarker1, propertyMarker2);
22+
}
23+
24+
[Test]
25+
public void PropertyPathMarkerEqualsDoesNotUseHashCode()
26+
{
27+
var collision = GetHashCollison();
28+
29+
var propertyMarker1 = new PropertyPathMarker { Name = collision.Item1 };
30+
var propertyMarker2 = new PropertyPathMarker { Name = collision.Item2 };
31+
this.TestAddingToDictionary(propertyMarker1, propertyMarker2);
32+
}
33+
34+
[Test]
35+
public void IndexNameMarkerEqualsDoesNotUseHashCode()
36+
{
37+
var collision = GetHashCollison();
38+
39+
var indexMarker1 = new IndexNameMarker { Name = collision.Item1 };
40+
var indexMarker2 = new IndexNameMarker { Name = collision.Item2 };
41+
this.TestAddingToDictionary(indexMarker1, indexMarker2);
42+
}
43+
44+
[Test]
45+
public void TypeNameMarkerEqualsDoesNotUseHashCode()
46+
{
47+
var collision = GetHashCollison();
48+
49+
var typeNameMarker1 = new TypeNameMarker { Name = collision.Item1 };
50+
var typeNameMarker2 = new TypeNameMarker { Name = collision.Item2 };
51+
this.TestAddingToDictionary(typeNameMarker1, typeNameMarker2);
52+
}
53+
54+
public void TestAddingToDictionary<T>(T first, T second)
55+
{
56+
first.GetHashCode().Should().Be(second.GetHashCode());
57+
58+
var dict = new Dictionary<T, bool> {{first, true}};
59+
Assert.DoesNotThrow(() => dict.Add(second, true));
60+
61+
}
62+
63+
public Tuple<string, string> GetHashCollison()
64+
{
65+
var hashes = new Dictionary<int, string>();
66+
67+
for (var x = 1; x < 10000; x++)
68+
{
69+
for (var i = 0; i < 1000000; i++)
70+
{
71+
var guid = Guid.NewGuid().ToString();
72+
var hashCode = guid.GetHashCode();
73+
74+
if (hashes.ContainsKey(hashCode) && hashes[hashCode] != guid)
75+
return Tuple.Create(guid, hashes[hashCode]);
76+
77+
hashes.Add(hashCode, guid);
78+
}
79+
}
80+
Assert.Fail("no collision found during test run");
81+
return null;
82+
}
83+
84+
85+
86+
}
87+
}

src/Tests/Nest.Tests.Unit/Internals/Inferno/PropertyNameResolverTests.cs

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using Elasticsearch.Net;
4+
using FluentAssertions;
45
using NUnit.Framework;
56
using System.Linq.Expressions;
67
using Newtonsoft.Json;
@@ -17,7 +18,7 @@ internal class SomeClass
1718
[JsonConverter(typeof(DictionaryKeysAreNotPropertyNamesJsonConverter))]
1819
public Dictionary<string, SomeOtherClass> StringDict { get; set; }
1920
public Dictionary<int, MyCustomClass> IntDict { get; set; }
20-
public IList<MyCustomClass> ListOfCustomClasses { get; set; }
21+
public IList<MyCustomClass> ListOfCustomClasses { get; set; }
2122
}
2223
[ElasticType(IdProperty = "Guid")]
2324
internal class SomeOtherClass
@@ -80,7 +81,7 @@ public void TestUsesElasticProperty()
8081
var expected = "myCustomClass.MID";
8182
Assert.AreEqual(expected, propertyName);
8283
}
83-
84+
8485
[Test]
8586
public void TestUsesOtherElasticProperty()
8687
{
@@ -89,7 +90,7 @@ public void TestUsesOtherElasticProperty()
8990
var expected = "custom.MID";
9091
Assert.AreEqual(expected, propertyName);
9192
}
92-
93+
9394
[Test]
9495
public void TestUsesOtherElasticTypePropertyIsIgnored()
9596
{
@@ -98,7 +99,7 @@ public void TestUsesOtherElasticTypePropertyIsIgnored()
9899
var expected = "myCustomOtherClass.MID";
99100
Assert.AreEqual(expected, propertyName);
100101
}
101-
102+
102103
[Test]
103104
public void TestCreatedDate()
104105
{
@@ -107,12 +108,12 @@ public void TestCreatedDate()
107108
var expected = "CreateDate";
108109
Assert.AreEqual(expected, propertyName);
109110
}
110-
111+
111112
[Test]
112113
public void TestDictionaryConstStringExpression()
113114
{
114115
Expression<Func<SomeClass, object>> exp = (m) => m.StringDict["someValue"].CreateDate;
115-
var propertyName =_client.Infer.PropertyPath(exp);
116+
var propertyName = _client.Infer.PropertyPath(exp);
116117
var expected = "stringDict.someValue.CreateDate";
117118
Assert.AreEqual(expected, propertyName);
118119
}
@@ -126,40 +127,40 @@ public void TestDictionaryConstIntExpression()
126127
Assert.AreEqual(expected, propertyName);
127128
}
128129

129-
[Test]
130-
public void TestDictionaryStringExpression()
131-
{
132-
string index = "someValue";
133-
Expression<Func<SomeClass, object>> exp = (m) => m.StringDict[index].CreateDate;
134-
var propertyName = _client.Infer.PropertyPath(exp);
135-
var expected = "stringDict.someValue.CreateDate";
136-
Assert.AreEqual(expected, propertyName);
137-
}
138-
139-
[Test]
140-
public void TestDictionaryIntExpression()
141-
{
142-
var index = 101;
143-
Expression<Func<SomeClass, object>> exp = (m) => m.IntDict[index].MyProperty;
144-
var propertyName =_client.Infer.PropertyPath(exp);
145-
var expected = "intDict.101.MID";
146-
Assert.AreEqual(expected, propertyName);
147-
}
148-
149-
[Test]
150-
public void TestDictionaryStringDiffValues()
151-
{
152-
string index = "someValue1";
153-
Expression<Func<SomeClass, object>> exp = (m) => m.StringDict[index].CreateDate;
154-
var propertyName =_client.Infer.PropertyPath(exp);
155-
var expected1 = "stringDict.someValue1.CreateDate";
156-
Assert.AreEqual(expected1, propertyName);
157-
index = "someValue2";
158-
exp = (m) => m.StringDict[index].CreateDate;
159-
propertyName = _client.Infer.PropertyPath(exp);
160-
var expected2 = "stringDict.someValue2.CreateDate";
161-
Assert.AreEqual(expected2, propertyName);
162-
}
130+
[Test]
131+
public void TestDictionaryStringExpression()
132+
{
133+
string index = "someValue";
134+
Expression<Func<SomeClass, object>> exp = (m) => m.StringDict[index].CreateDate;
135+
var propertyName = _client.Infer.PropertyPath(exp);
136+
var expected = "stringDict.someValue.CreateDate";
137+
Assert.AreEqual(expected, propertyName);
138+
}
139+
140+
[Test]
141+
public void TestDictionaryIntExpression()
142+
{
143+
var index = 101;
144+
Expression<Func<SomeClass, object>> exp = (m) => m.IntDict[index].MyProperty;
145+
var propertyName = _client.Infer.PropertyPath(exp);
146+
var expected = "intDict.101.MID";
147+
Assert.AreEqual(expected, propertyName);
148+
}
149+
150+
[Test]
151+
public void TestDictionaryStringDiffValues()
152+
{
153+
string index = "someValue1";
154+
Expression<Func<SomeClass, object>> exp = (m) => m.StringDict[index].CreateDate;
155+
var propertyName = _client.Infer.PropertyPath(exp);
156+
var expected1 = "stringDict.someValue1.CreateDate";
157+
Assert.AreEqual(expected1, propertyName);
158+
index = "someValue2";
159+
exp = (m) => m.StringDict[index].CreateDate;
160+
propertyName = _client.Infer.PropertyPath(exp);
161+
var expected2 = "stringDict.someValue2.CreateDate";
162+
Assert.AreEqual(expected2, propertyName);
163+
}
163164

164165
[Test]
165166
public void TestCollectionIndexExpressionDoesNotEndUpInPath()
@@ -170,7 +171,7 @@ public void TestCollectionIndexExpressionDoesNotEndUpInPath()
170171
Assert.AreEqual(expected, propertyName);
171172
}
172173

173-
[Test]
174+
[Test]
174175
public void SearchUsesTheProperResolver()
175176
{
176177
var result = this._client.Search<SomeOtherClass>(s => s
@@ -182,7 +183,7 @@ public void SearchUsesTheProperResolver()
182183
mp => mp.ConstantScore(cs => cs.Filter(filter => filter.Term(x => x.MyCustomOtherClass.MyProperty, "serverid")))
183184
)
184185
)
185-
&& query.Term(f=>f.CreateDate, "x")
186+
&& query.Term(f => f.CreateDate, "x")
186187
)
187188
);
188189
var request = result.ConnectionStatus.Request.Utf8String();
@@ -191,7 +192,7 @@ public void SearchUsesTheProperResolver()
191192
StringAssert.Contains("CreateDate", request);
192193
}
193194

194-
[Test]
195+
[Test]
195196
public void SearchDoesntLowercaseStringFieldOverload()
196197
{
197198
var result = this._client.Search<SomeOtherClass>(s => s
@@ -216,5 +217,6 @@ public void SearchDoesntLowercaseStringFieldOverloadInSearch()
216217
);
217218
StringAssert.DoesNotContain("createDate2", result.ConnectionStatus.Request.Utf8String());
218219
}
220+
219221
}
220222
}

0 commit comments

Comments
 (0)