Skip to content

Commit a87e321

Browse files
author
Zhen Li
committed
Removed IIdentity from IEntity, added long Id instead.
1 parent 89ac803 commit a87e321

File tree

6 files changed

+53
-107
lines changed

6 files changed

+53
-107
lines changed

Neo4j.Driver/Neo4j.Driver.Tck.Tests/TCK/CypherRecordParser.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ internal IPath ParsePath(string input)
151151
var end = ParseNode("(" + segs[i + 3] + ")");
152152
if (segs[i].Trim().Equals("-") && segs[i + 2].Trim().Equals("->"))
153153
{
154-
relationship.SetStartAndEnd(start.Identity, end.Identity);
154+
relationship.SetStartAndEnd(start.Id, end.Id);
155155
}
156156
else
157157
{
158-
relationship.SetStartAndEnd(end.Identity, start.Identity);
158+
relationship.SetStartAndEnd(end.Id, start.Id);
159159
}
160160
segments.Add(new Segment(start, relationship, end));
161161
relationships.Add(relationship);
@@ -232,7 +232,7 @@ public static string PathToString(IPath path)
232232
start = path.Nodes[i];
233233
end = path.Nodes[i + 1];
234234

235-
if (rel.Start.Equals(start.Identity))
235+
if (rel.StartNodeId.Equals(start.Id))
236236
{
237237
str += NodeToString(start) + "-" + RelToString(rel) + "->";
238238
}

Neo4j.Driver/Neo4j.Driver.Tests/PackStream/PackStreamMessageFormatV1Tests.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,9 @@ public void ShouldUnpackRelationshipCorrectly()
359359
IRelationship rel = real as IRelationship;
360360
rel.Should().NotBeNull();
361361

362-
rel.Identity.Id.Should().Be(1);
363-
rel.Start.Id.Should().Be(2);
364-
rel.End.Id.Should().Be(3);
362+
rel.Id.Should().Be(1);
363+
rel.StartNodeId.Should().Be(2);
364+
rel.EndNodeId.Should().Be(3);
365365
rel.Type.Should().BeEmpty();
366366
rel.Properties.Should().BeEmpty();
367367
}
@@ -381,7 +381,7 @@ public void ShouldUnpackNodeCorrectly()
381381
INode n = node as INode;
382382
n.Should().NotBeNull();
383383

384-
n.Identity.Id.Should().Be(1);
384+
n.Id.Should().Be(1);
385385
n.Properties.Should().BeEmpty();
386386
n.Labels.Should().BeEmpty();
387387
}
@@ -403,7 +403,7 @@ public void ShouldUnpackPathCorrectly()
403403

404404
p.Start.Should().NotBeNull();
405405
p.End.Should().NotBeNull();
406-
p.Start.Identity.Id.Should().Be(1);
406+
p.Start.Id.Should().Be(1);
407407
p.Start.Properties.Should().BeEmpty();
408408
p.Start.Labels.Should().BeEmpty();
409409
p.Nodes.Should().HaveCount(1);
@@ -490,7 +490,7 @@ public void ShouldUnpackPathWithLenghTwoCorrectly()
490490
p.Start.Should().NotBeNull();
491491
p.End.Should().NotBeNull();
492492
p.Start.Equals(TestNodes.Alice).Should().BeTrue();
493-
p.End.Equals(TestNodes.Dave).Should().BeTrue($"Got {p.End.Identity.Id}");
493+
p.End.Equals(TestNodes.Dave).Should().BeTrue($"Got {p.End.Id}");
494494

495495
List<INode> correctOrder = new List<INode> {TestNodes.Alice, TestNodes.Carol, TestNodes.Dave};
496496
p.Nodes.Should().ContainInOrder(correctOrder);
@@ -528,7 +528,7 @@ public void ShouldUnpackPathWithRelationshipTraversedAgainstItsDirectionCorrectl
528528
p.Start.Should().NotBeNull();
529529
p.End.Should().NotBeNull();
530530
p.Start.Equals(TestNodes.Alice).Should().BeTrue();
531-
p.End.Equals(TestNodes.Dave).Should().BeTrue($"Got {p.End.Identity.Id}");
531+
p.End.Equals(TestNodes.Dave).Should().BeTrue($"Got {p.End.Id}");
532532

533533
List<INode> correctOrder = new List<INode>
534534
{
@@ -573,7 +573,7 @@ public void ShouldUnpackPathWithNodeVisitedMulTimesCorrectly()
573573
p.Start.Should().NotBeNull();
574574
p.End.Should().NotBeNull();
575575
p.Start.Equals(TestNodes.Alice).Should().BeTrue();
576-
p.End.Equals(TestNodes.Carol).Should().BeTrue($"Got {p.End.Identity.Id}");
576+
p.End.Equals(TestNodes.Carol).Should().BeTrue($"Got {p.End.Id}");
577577

578578
List<INode> correctOrder = new List<INode>
579579
{
@@ -622,7 +622,7 @@ public void ShouldUnpackPathWithRelTraversedMulTimesInSameDirectionCorrectly()
622622
p.Start.Should().NotBeNull();
623623
p.End.Should().NotBeNull();
624624
p.Start.Equals(TestNodes.Alice).Should().BeTrue();
625-
p.End.Equals(TestNodes.Dave).Should().BeTrue($"Got {p.End.Identity.Id}");
625+
p.End.Equals(TestNodes.Dave).Should().BeTrue($"Got {p.End.Id}");
626626

627627
List<INode> correctOrder = new List<INode>
628628
{
@@ -671,7 +671,7 @@ public void ShouldUnpackPathWithLoopCorrectly()
671671
p.Start.Should().NotBeNull();
672672
p.End.Should().NotBeNull();
673673
p.Start.Equals(TestNodes.Carol).Should().BeTrue();
674-
p.End.Equals(TestNodes.Dave).Should().BeTrue($"Got {p.End.Identity.Id}");
674+
p.End.Equals(TestNodes.Dave).Should().BeTrue($"Got {p.End.Id}");
675675

676676
List<INode> correctOrder = new List<INode>
677677
{
@@ -726,28 +726,28 @@ private static class TestRelationships
726726

727727
// IRelationships
728728
public static IRelationship AliceKnowsBob =
729-
new Relationship(new Identity(12L), TestNodes.Alice.Identity,
730-
TestNodes.Bob.Identity, KNOWS,
729+
new Relationship( 12L, TestNodes.Alice.Id,
730+
TestNodes.Bob.Id, KNOWS,
731731
new Dictionary<string, object> {{"since", 1999L}});
732732

733733
public static IRelationship AliceLikesCarol =
734-
new Relationship(new Identity(13L), TestNodes.Alice.Identity,
735-
TestNodes.Carol.Identity, LIKES,
734+
new Relationship(13L, TestNodes.Alice.Id,
735+
TestNodes.Carol.Id, LIKES,
736736
new Dictionary<string, object>());
737737

738738
public static IRelationship CarolDislikesBob =
739-
new Relationship(new Identity(32L), TestNodes.Carol.Identity,
740-
TestNodes.Bob.Identity, DISLIKES,
739+
new Relationship(32L, TestNodes.Carol.Id,
740+
TestNodes.Bob.Id, DISLIKES,
741741
new Dictionary<string, object>());
742742

743743
public static IRelationship CarolMarriedToDave =
744-
new Relationship(new Identity(34L), TestNodes.Carol.Identity,
745-
TestNodes.Dave.Identity, MARRIED_TO,
744+
new Relationship(34L, TestNodes.Carol.Id,
745+
TestNodes.Dave.Id, MARRIED_TO,
746746
new Dictionary<string, object>());
747747

748748
public static IRelationship DaveWorksForDave =
749-
new Relationship(new Identity(44L), TestNodes.Dave.Identity,
750-
TestNodes.Dave.Identity, WORKS_FOR,
749+
new Relationship(44L, TestNodes.Dave.Id,
750+
TestNodes.Dave.Id, WORKS_FOR,
751751
new Dictionary<string, object>());
752752
}
753753
}

Neo4j.Driver/Neo4j.Driver.Tests/ValueExtensionsTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public void ShouldConvertToNode()
251251
new List<string> {"l1", "l2"},
252252
new Dictionary<string, object> { {"key1", "value1"}, {"key2", 2 } });
253253
var actual = obj.As<INode>();
254-
actual.Identity.Id.Should().Be(1);
254+
actual.Id.Should().Be(1);
255255
actual.Labels.Count.Should().Be(2);
256256
actual.Labels[0].Should().Be("l1");
257257
actual.Labels[1].Should().Be("l2");
@@ -268,7 +268,7 @@ public void ShouldConvertToRel()
268268
new Dictionary<string, object> {{"key1", "value1"}, {"key2", 2}});
269269

270270
var actual = obj.As<IRelationship>();
271-
actual.Identity.Id.Should().Be(1);
271+
actual.Id.Should().Be(1);
272272
actual.Type.Should().Be("Type");
273273
actual.Properties["key1"].As<string>().Should().Be("value1");
274274
actual.Properties["key2"].As<string>().Should().Be("2");
@@ -291,8 +291,8 @@ public void ShouldConvertToPath()
291291
new List<IRelationship>());
292292

293293
var actual = obj.As<IPath>();
294-
actual.Start.Identity.Id.Should().Be(1);
295-
actual.End.Identity.Id.Should().Be(1);
294+
actual.Start.Id.Should().Be(1);
295+
actual.End.Id.Should().Be(1);
296296
var node = actual.Nodes[0];
297297
node.Properties["key1"].As<string>().Should().Be("value1");
298298
node.Properties["key2"].As<string>().Should().Be("2");

Neo4j.Driver/Neo4j.Driver/IEntity.cs

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,14 @@ public interface IEntity
2929
/// <param name="key">the key</param>
3030
/// <returns>the value specified by the given key in <see cref="Properties"/></returns>
3131
object this[string key] { get; }
32-
33-
/// <summary>
34-
/// Gets the unique <see cref="IIdentity"/> of the <c>Entity</c>.
35-
/// </summary>
36-
IIdentity Identity { get; }
3732
/// <summary>
3833
/// Gets the properties of the <c>Entity</c>.
3934
/// </summary>
4035
IReadOnlyDictionary<string, object> Properties { get; }
36+
/// <summary>
37+
/// Get the identity as a <see cref="long"/> number
38+
/// </summary>
39+
long Id { get; }
4140
}
4241

4342
/// <summary>
@@ -49,7 +48,6 @@ public interface INode: IEntity
4948
/// Gets the lables of the node.
5049
/// </summary>
5150
IReadOnlyList<string> Labels { get; }
52-
//bool HasLabel(string label);
5351
}
5452

5553
/// <summary>
@@ -63,13 +61,13 @@ public interface IRelationship:IEntity
6361
string Type { get; }
6462
//bool HasType(string type);
6563
/// <summary>
66-
/// Gets the <see cref="IIdentity"/> of the start node of the relationship.
64+
/// Gets the id of the start node of the relationship.
6765
/// </summary>
68-
IIdentity Start { get; }
66+
long StartNodeId { get; }
6967
/// <summary>
70-
/// Gets the <see cref="IIdentity"/> of the end node of the relationship.
68+
/// Gets the id of the end node of the relationship.
7169
/// </summary>
72-
IIdentity End { get; }
70+
long EndNodeId { get; }
7371
}
7472

7573
/// <summary>
@@ -101,17 +99,4 @@ public interface IPath
10199
/// </summary>
102100
IReadOnlyList<IRelationship> Relationships { get; }
103101
}
104-
105-
/// <summary>
106-
/// A unique identifer
107-
/// </summary>
108-
public interface IIdentity
109-
{
110-
// bool equal(object id)
111-
// int hashCode();
112-
/// <summary>
113-
/// Get the identity as a <see cref="long"/> number
114-
/// </summary>
115-
long Id { get; }
116-
}
117102
}

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

Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ namespace Neo4j.Driver.Internal
2222
{
2323
public class Node : INode, IEquatable<INode>
2424
{
25-
public IIdentity Identity { get; }
25+
public long Id { get; }
2626
public IReadOnlyList<string> Labels { get; }
2727
public IReadOnlyDictionary<string, object> Properties { get; }
2828
public object this[string key] => Properties[key];
2929

3030
public Node(long id, IReadOnlyList<string> lables, IReadOnlyDictionary<string, object> prop)
3131
{
32-
Identity = new Identity(id);
32+
Id = id;
3333
Labels = lables;
3434
Properties = prop;
3535
}
3636

3737
public bool Equals(INode other)
3838
{
39-
return Equals(Identity, other.Identity);
39+
return Equals(Id, other.Id);
4040
}
4141

4242
public override bool Equals(object obj)
@@ -49,42 +49,32 @@ public override bool Equals(object obj)
4949

5050
public override int GetHashCode()
5151
{
52-
return Identity?.GetHashCode() ?? 0;
52+
return Id.GetHashCode();
5353
}
5454
}
5555

5656
public class Relationship : IRelationship, IEquatable<IRelationship>
5757
{
58-
public IIdentity Identity { get; }
58+
public long Id { get; }
5959
public string Type { get; }
60-
public IIdentity Start { get; internal set; }
61-
public IIdentity End { get; internal set; }
60+
public long StartNodeId { get; internal set; }
61+
public long EndNodeId { get; internal set; }
6262
public IReadOnlyDictionary<string, object> Properties { get; }
6363
public object this[string key] => Properties[key];
6464

6565
public Relationship(long id, long startId, long endId, string relType,
6666
IReadOnlyDictionary<string, object> props)
6767
{
68-
Identity = new Identity(id);
69-
Start = new Identity(startId);
70-
End = new Identity(endId);
71-
Type = relType;
72-
Properties = props;
73-
}
74-
75-
public Relationship(IIdentity id, IIdentity start, IIdentity end, string relType,
76-
IReadOnlyDictionary<string, object> props)
77-
{
78-
Identity = id;
79-
Start = start;
80-
End = end;
68+
Id = id;
69+
StartNodeId = startId;
70+
EndNodeId = endId;
8171
Type = relType;
8272
Properties = props;
8373
}
8474

8575
public bool Equals(IRelationship other)
8676
{
87-
return Equals(Identity, other.Identity);
77+
return Equals(Id, other.Id);
8878
}
8979

9080
public override bool Equals(object obj)
@@ -97,41 +87,13 @@ public override bool Equals(object obj)
9787

9888
public override int GetHashCode()
9989
{
100-
return Identity?.GetHashCode() ?? 0;
101-
}
102-
103-
internal void SetStartAndEnd(IIdentity start, IIdentity end)
104-
{
105-
Start = start;
106-
End = end;
107-
}
108-
}
109-
110-
public class Identity : IIdentity, IEquatable<IIdentity>
111-
{
112-
public long Id { get; }
113-
114-
public Identity(long id)
115-
{
116-
Id = id;
117-
}
118-
119-
public bool Equals(IIdentity other)
120-
{
121-
return Id == other.Id;
122-
}
123-
124-
public override bool Equals(object obj)
125-
{
126-
if (ReferenceEquals(null, obj)) return false;
127-
if (ReferenceEquals(this, obj)) return true;
128-
if (obj.GetType() != GetType()) return false;
129-
return Equals((Identity) obj);
90+
return Id.GetHashCode();
13091
}
13192

132-
public override int GetHashCode()
93+
internal void SetStartAndEnd(long start, long end)
13394
{
134-
return Id.GetHashCode();
95+
StartNodeId = start;
96+
EndNodeId = end;
13597
}
13698
}
13799

@@ -231,7 +193,6 @@ public override int GetHashCode()
231193
{
232194
var hashCode = Nodes?.GetHashCode() ?? 0;
233195
hashCode = (hashCode * 397) ^ (Relationships?.GetHashCode() ?? 0);
234-
hashCode = (hashCode * 397) ^ (_segments?.GetHashCode() ?? 0);
235196
return hashCode;
236197
}
237198
}

Neo4j.Driver/Neo4j.Driver/Internal/PackStream/PackStreamMessageFormatV1.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private IPath UnpackPath()
137137
var urn = _unpacker.UnpackLong();
138138
var relType = _unpacker.UnpackString();
139139
var props = UnpackMap();
140-
uniqRels[i]=new Relationship(new Identity(urn), null, null, relType, props);
140+
uniqRels[i]=new Relationship(urn, -1, -1, relType, props);
141141
}
142142

143143
// Path sequence
@@ -160,12 +160,12 @@ private IPath UnpackPath()
160160
if (relIdx < 0)
161161
{
162162
rel = uniqRels[(-relIdx) - 1]; // -1 because rel idx are 1-indexed
163-
rel.SetStartAndEnd(nextNode.Identity, prevNode.Identity);
163+
rel.SetStartAndEnd(nextNode.Id, prevNode.Id);
164164
}
165165
else
166166
{
167167
rel = uniqRels[relIdx - 1];
168-
rel.SetStartAndEnd(prevNode.Identity, nextNode.Identity);
168+
rel.SetStartAndEnd(prevNode.Id, nextNode.Id);
169169
}
170170

171171
nodes[i + 1] = nextNode;

0 commit comments

Comments
 (0)