Skip to content

Commit 326e457

Browse files
author
Zhen Li
committed
Made entity equal to use id equal
Made path equal to check start node equal and each relationship equal Some changes in Config: Renamed `IdleSessionPoolSize` to `MaxIdleSessionPoolSize` Changed `WithEncryptionEnabled(bool)` to `WithEncryptionLevel(EncryptionLevel)`
1 parent af7f70e commit 326e457

File tree

10 files changed

+111
-33
lines changed

10 files changed

+111
-33
lines changed

Neo4j.Driver/Neo4j.Driver.IntegrationTests/Examples.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public void ResultSummaryNotifications()
275275
public void TlsRequireEncryption()
276276
{
277277
//tag::tls-require-encryption[]
278-
var driver = GraphDatabase.Driver("bolt://localhost:7687", Config.Builder.WithTlsEnabled(true).ToConfig());
278+
var driver = GraphDatabase.Driver("bolt://localhost:7687", Config.Builder.WithEncryptionLevel(EncryptionLevel.Encrypted).ToConfig());
279279
//end::tls-require-encryption[]
280280
driver.Dispose();
281281
}
@@ -284,7 +284,7 @@ public void TlsRequireEncryption()
284284
public void TlsSigned()
285285
{
286286
//tag::tls-signed[]
287-
var driver = GraphDatabase.Driver("bolt://localhost:7687", Config.Builder.WithTlsEnabled(true).ToConfig());
287+
var driver = GraphDatabase.Driver("bolt://localhost:7687", Config.Builder.WithEncryptionLevel(EncryptionLevel.Encrypted).ToConfig());
288288
//end::tls-signed[]
289289
driver.Dispose();
290290
}

Neo4j.Driver/Neo4j.Driver.Tck.Tests/TCK/TypeSystem.feature.steps.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public static void GlobalBeforeScenario()
110110
throw;
111111
}
112112
var config = Config.DefaultConfig;
113-
config.IdleSessionPoolSize = Config.InfiniteIdleSessionPoolSize;
113+
config.MaxIdleSessionPoolSize = Config.InfiniteMaxIdleSessionPoolSize;
114114
Driver = GraphDatabase.Driver(Url, config);
115115
}
116116

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) 2002-2016 "Neo Technology,"
2+
// Network Engine for Objects in Lund AB [http://neotechnology.com]
3+
//
4+
// This file is part of Neo4j.
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
using FluentAssertions;
18+
using Neo4j.Driver.Internal;
19+
using Xunit;
20+
21+
namespace Neo4j.Driver.Tests
22+
{
23+
public class EntityTests
24+
{
25+
public class NodeTests
26+
{
27+
[Fact]
28+
public void ShouldEqualIfIdEquals()
29+
{
30+
var node1 = new Node(123, new []{"buibui"}, null);
31+
var node2 = new Node(123, new []{"lala"}, null);
32+
node1.Equals(node2).Should().BeTrue();
33+
Equals(node1, node2).Should().BeTrue();
34+
node1.GetHashCode().Should().Be(node2.GetHashCode());
35+
}
36+
}
37+
38+
public class RelationshipTests
39+
{
40+
[Fact]
41+
public void ShouldEqualIfIdEquals()
42+
{
43+
var rel1 = new Relationship(123, 000, 111, "buibui", null);
44+
var rel2 = new Relationship(123, 222, 333, "lala", null);
45+
rel1.Equals(rel2).Should().BeTrue();
46+
Equals(rel1, rel2).Should().BeTrue();
47+
rel1.GetHashCode().Should().Be(rel2.GetHashCode());
48+
}
49+
}
50+
51+
public class PathTests
52+
{
53+
[Fact]
54+
public void ShouldEqualIfIdEquals()
55+
{
56+
var path1 = new Path(null,
57+
new []{ new Node(123, new []{"buibui"}, null) },
58+
new []{ new Relationship(1, 000, 111, "buibui", null)});
59+
var path2 = new Path(null,
60+
new[] { new Node(123, new[] { "lala" }, null) },
61+
new []{ new Relationship(1, 222, 333, "lala", null)});
62+
path1.Equals(path2).Should().BeTrue();
63+
Equals(path1, path2).Should().BeTrue();
64+
path1.GetHashCode().Should().Be(path2.GetHashCode());
65+
}
66+
}
67+
}
68+
}

Neo4j.Driver/Neo4j.Driver.Tests/Neo4j.Driver.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
<Compile Include="Connector\ChunkedOutputTest.cs" />
9191
<Compile Include="Connector\MessageResponseHandlerTests.cs" />
9292
<Compile Include="DriverTests.cs" />
93+
<Compile Include="EntityTests.cs" />
9394
<Compile Include="Messaging\MessageTests.cs" />
9495
<Compile Include="Result\ResultBuilderTests.cs" />
9596
<Compile Include="SessionPoolTests.cs" />

Neo4j.Driver/Neo4j.Driver.Tests/SessionPoolTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public GetSessionMethod(ITestOutputHelper output)
4343
public void ShouldNotThrowExceptionWhenIdlePoolSizeReached()
4444
{
4545
var mock = new Mock<IConnection>();
46-
var config = new Config {IdleSessionPoolSize = 2};
46+
var config = new Config {MaxIdleSessionPoolSize = 2};
4747
var pool = new SessionPool(TestUri, AuthTokens.None, null, config, mock.Object);
4848
pool.GetSession();
4949
pool.GetSession();
@@ -60,7 +60,7 @@ public void ShouldNotExceedIdleLimit()
6060
var mock = new Mock<IConnection>();
6161
mock.Setup(x => x.IsOpen).Returns(true);
6262

63-
var config = new Config {IdleSessionPoolSize = 2};
63+
var config = new Config {MaxIdleSessionPoolSize = 2};
6464
var pool = new SessionPool(TestUri, AuthTokens.None, null, config, mock.Object);
6565

6666
var sessions = new List<ISession>();
@@ -85,7 +85,7 @@ public void ShouldGiveSessionsFromPoolIfAvailable()
8585
var mock = new Mock<IConnection>();
8686
mock.Setup(x => x.IsOpen).Returns(true);
8787

88-
var config = new Config {IdleSessionPoolSize = 2};
88+
var config = new Config {MaxIdleSessionPoolSize = 2};
8989
var pool = new SessionPool(TestUri, AuthTokens.None, null, config, mock.Object);
9090

9191
for (var i = 0; i < 4; i++)

Neo4j.Driver/Neo4j.Driver/Config.cs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,31 @@
1919

2020
namespace Neo4j.Driver
2121
{
22+
/// <summary>
23+
/// Control the level of encryption to require.
24+
/// </summary>
25+
public enum EncryptionLevel
26+
{
27+
None,
28+
Encrypted
29+
}
30+
2231
/// <summary>
2332
/// Use this class to config the <see cref="Driver"/> in a certain way
2433
/// </summary>
2534
public class Config
2635
{
2736
/// <summary>
28-
/// When the <see cref="IdleSessionPoolSize"/> is set to <see cref="InfiniteIdleSessionPoolSize" /> the idle session pool will pool all sessions created by the driver.
37+
/// When the <see cref="MaxIdleSessionPoolSize"/> is set to <see cref="InfiniteMaxIdleSessionPoolSize" /> the idle session pool will pool all sessions created by the driver.
2938
/// </summary>
30-
public const int InfiniteIdleSessionPoolSize = -1;
39+
public const int InfiniteMaxIdleSessionPoolSize = -1;
3140
static Config()
3241
{
3342
DefaultConfig = new Config
3443
{
35-
TlsEnabled = false,
44+
EncryptionLevel = EncryptionLevel.None,
3645
Logger = new DebugLogger {Level = LogLevel.Info},
37-
IdleSessionPoolSize = 20
46+
MaxIdleSessionPoolSize = 10
3847
};
3948
}
4049

@@ -44,9 +53,9 @@ static Config()
4453
/// <remarks>
4554
/// The defaults are <br/>
4655
/// <list type="bullet">
47-
/// <item><see cref="TlsEnabled"/> : <c>false</c></item>
56+
/// <item><see cref="EncryptionLevel"/> : <c><see cref="EncryptionLevel"/> None</c> </item>
4857
/// <item><see cref="Logger"/> : <c>DebugLogger</c> at <c><see cref="LogLevel"/> Info</c> </item>
49-
/// <item><see cref="IdleSessionPoolSize"/> : <c>20</c> </item>
58+
/// <item><see cref="MaxIdleSessionPoolSize"/> : <c>10</c> </item>
5059
/// </list>
5160
/// </remarks>
5261
public static Config DefaultConfig { get; }
@@ -57,21 +66,21 @@ static Config()
5766
public static IConfigBuilder Builder => new ConfigBuilder(new Config());
5867

5968
/// <summary>
60-
/// Gets or sets the use of TLS for all the connections created by the Driver.
69+
/// Gets or sets the use of encryption for all the connections created by the Driver.
6170
/// </summary>
62-
public bool TlsEnabled { get; set; }
71+
public EncryptionLevel EncryptionLevel { get; set; }
6372

6473
/// <summary>
6574
/// Gets or sets the <see cref="ILogger"/> instance to be used by the <see cref="ISession"/>s.
6675
/// </summary>
6776
public ILogger Logger { get; set; }
6877

6978
/// <summary>
70-
/// Gets or sets the idle session pool size.
79+
/// Gets or sets the max idle session pool size.
7180
/// </summary>
72-
/// <remarks> The idle session pool size represents the maximum number of sessions buffered for the driver. A buffered <see cref="ISession"/>
81+
/// <remarks> The max idle session pool size represents the maximum number of sessions buffered for the driver. A buffered <see cref="ISession"/>
7382
/// is a session that has already been connected to the database instance and doesn't need to re-initialize.</remarks>
74-
public int IdleSessionPoolSize { get; set; }
83+
public int MaxIdleSessionPoolSize { get; set; }
7584

7685
private class ConfigBuilder : IConfigBuilder
7786
{
@@ -82,9 +91,9 @@ internal ConfigBuilder(Config config)
8291
_config = config;
8392
}
8493

85-
public IConfigBuilder WithTlsEnabled(bool enableTls)
94+
public IConfigBuilder WithEncryptionLevel(EncryptionLevel level)
8695
{
87-
_config.TlsEnabled = enableTls;
96+
_config.EncryptionLevel = level;
8897
return this;
8998
}
9099

@@ -94,9 +103,9 @@ public IConfigBuilder WithLogger(ILogger logger)
94103
return this;
95104
}
96105

97-
public IConfigBuilder WithIdleSessionPoolSize(int size)
106+
public IConfigBuilder WithMaxIdleSessionPoolSize(int size)
98107
{
99-
_config.IdleSessionPoolSize = size;
108+
_config.MaxIdleSessionPoolSize = size;
100109
return this;
101110
}
102111

@@ -121,12 +130,12 @@ public interface IConfigBuilder
121130
Config ToConfig();
122131

123132
/// <summary>
124-
/// Sets the <see cref="Config"/> to use TLS if <paramref name="enableTls"/> is <c>true</c>.
133+
/// Sets the <see cref="Config"/> to use TLS if <paramref name="level"/> is <c>true</c>.
125134
/// </summary>
126-
/// <param name="enableTls"><c>true</c> enables TLS for the connection, <c>false</c> otherwise.</param>
135+
/// <param name="level"><c>Encrypted</c> enables TLS for the connection, <c>None</c> otherwise. See <see cref="EncryptionLevel"/> for more info</param>
127136
/// <returns>An <see cref="IConfigBuilder"/> instance for further configuration options.</returns>
128137
/// <remarks>Must call <see cref="ToConfig"/> to generate a <see cref="Config"/> instance.</remarks>
129-
IConfigBuilder WithTlsEnabled(bool enableTls);
138+
IConfigBuilder WithEncryptionLevel(EncryptionLevel level);
130139

131140
/// <summary>
132141
/// Sets the <see cref="Config"/> to use a given <see cref="ILogger"/> instance.
@@ -139,10 +148,10 @@ public interface IConfigBuilder
139148
/// <summary>
140149
/// Sets the size of the idle session pool.
141150
/// </summary>
142-
/// <param name="size">The size of the <see cref="Config.IdleSessionPoolSize"/>, set to <see cref="Config.InfiniteIdleSessionPoolSize"/> to pool all sessions.</param>
151+
/// <param name="size">The size of the <see cref="Config.MaxIdleSessionPoolSize"/>, set to <see cref="Config.InfiniteMaxIdleSessionPoolSize"/> to pool all sessions.</param>
143152
/// <returns>An <see cref="IConfigBuilder"/> instance for further configuration options.</returns>
144153
/// <remarks>Must call <see cref="ToConfig"/> to generate a <see cref="Config"/> instance.</remarks>
145-
IConfigBuilder WithIdleSessionPoolSize(int size);
154+
IConfigBuilder WithMaxIdleSessionPoolSize(int size);
146155
}
147156

148157

Neo4j.Driver/Neo4j.Driver/Driver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace Neo4j.Driver
2727
/// <remarks>
2828
/// The Driver maintains a session pool buffering the <see cref="ISession" />s created by the user. The size of the
2929
/// buffer can be
30-
/// configured by the <see cref="Config.IdleSessionPoolSize" /> property on the <see cref="Config" /> when creating the
30+
/// configured by the <see cref="Config.MaxIdleSessionPoolSize" /> property on the <see cref="Config" /> when creating the
3131
/// Driver.
3232
/// </remarks>
3333
public class Driver : LoggerBase

Neo4j.Driver/Neo4j.Driver/Internal/Connector/SocketClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void Dispose()
5050

5151
public async Task Start()
5252
{
53-
await _tcpSocketClient.ConnectAsync(_url.Host, _url.Port, _config.TlsEnabled).ConfigureAwait(false);
53+
await _tcpSocketClient.ConnectAsync(_url.Host, _url.Port, _config.EncryptionLevel == EncryptionLevel.Encrypted).ConfigureAwait(false);
5454
IsOpen = true;
5555
_config.Logger?.Debug($"~~ [CONNECT] {_url}");
5656

Neo4j.Driver/Neo4j.Driver/Internal/Entity.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public Path(IReadOnlyList<ISegment> segments, IReadOnlyList<INode> nodes,
176176

177177
public bool Equals(IPath other)
178178
{
179-
return Equals(Nodes, other.Nodes) && Equals(Relationships, other.Relationships);
179+
return Equals(Start, other.Start) && Relationships.SequenceEqual(other.Relationships);
180180
}
181181

182182
public override bool Equals(object obj)
@@ -191,8 +191,8 @@ public override int GetHashCode()
191191
{
192192
unchecked
193193
{
194-
var hashCode = Nodes?.GetHashCode() ?? 0;
195-
hashCode = (hashCode * 397) ^ (Relationships?.GetHashCode() ?? 0);
194+
var hashCode = Start?.GetHashCode() ?? 0;
195+
hashCode = Relationships?.Aggregate(hashCode, (current, relationship) => (current*397) ^ relationship.GetHashCode()) ?? hashCode;
196196
return hashCode;
197197
}
198198
}

Neo4j.Driver/Neo4j.Driver/Internal/SessionPool.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public SessionPool(Uri uri, IAuthToken authToken, ILogger logger, Config config,
3737
_authToken = authToken;
3838
_config = config;
3939
_connection = connection;
40-
_idleSessionPoolSize = config.IdleSessionPoolSize;
40+
_idleSessionPoolSize = config.MaxIdleSessionPoolSize;
4141
}
4242

4343
internal SessionPool(
@@ -111,7 +111,7 @@ public void Release(Guid sessionId)
111111
lock (_availableSessions)
112112
{
113113
if (_availableSessions.Count < _idleSessionPoolSize ||
114-
_idleSessionPoolSize == Config.InfiniteIdleSessionPoolSize)
114+
_idleSessionPoolSize == Config.InfiniteMaxIdleSessionPoolSize)
115115
{
116116
_availableSessions.Enqueue(session);
117117
}

0 commit comments

Comments
 (0)