Skip to content

Commit 2c61008

Browse files
authored
Remove use of getID (neo4j#258)
1 parent 75ada6f commit 2c61008

File tree

22 files changed

+397
-394
lines changed

22 files changed

+397
-394
lines changed

common/src/main/java/apoc/export/cypher/formatter/AbstractCypherFormatter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public void buildStatementForNodes(String nodeClause, String setClause,
156156
AtomicInteger nodeCount = new AtomicInteger(0);
157157
Function<Node, Map.Entry<Set<String>, Set<String>>> keyMapper = (node) -> {
158158
try (Transaction tx = db.beginTx()) {
159-
node = tx.getNodeById(node.getId());
159+
node = tx.getNodeByElementId(node.getElementId());
160160
Set<String> idProperties = CypherFormatterUtils.getNodeIdProperties(node, uniqueConstraints).keySet();
161161
Set<String> labels = getLabels(node);
162162
tx.commit();
@@ -266,7 +266,7 @@ public void buildStatementForRelationships(String relationshipClause,
266266

267267
Function<Relationship, Map<String, Object>> keyMapper = (rel) -> {
268268
try (Transaction tx = db.beginTx()) {
269-
rel = tx.getRelationshipById(rel.getId());
269+
rel = tx.getRelationshipByElementId(rel.getElementId());
270270
Node start = rel.getStartNode();
271271
Set<String> startLabels = getLabels(start);
272272

common/src/main/java/apoc/result/VirtualNode.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,7 @@ void delete(Relationship rel) {
261261

262262
@Override
263263
public boolean equals(Object o) {
264-
return this == o || o instanceof Node && id == ((Node) o).getId();
265-
264+
return this == o || o instanceof Node && Objects.equals(getElementId(), ((Node) o).getElementId());
266265
}
267266

268267
@Override

common/src/main/java/apoc/result/VirtualRelationship.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ public Map<String, Object> getAllProperties() {
153153

154154
@Override
155155
public boolean equals(Object o) {
156-
return this == o || o instanceof Relationship && id == ((Relationship) o).getId();
157-
156+
return this == o || o instanceof Relationship &&
157+
Objects.equals(getElementId(), ((Relationship) o).getElementId());
158158
}
159159

160160
@Override

common/src/main/java/apoc/result/WrappedNode.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ void delete(Relationship rel) {
214214

215215
@Override
216216
public boolean equals(Object o) {
217-
return this == o || o instanceof Node && id == ((Node) o).getId();
217+
return this == o || o instanceof Node && Objects.equals(getElementId(), ((Node) o).getElementId());
218218

219219
}
220220

common/src/main/java/apoc/util/Util.java

+18-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.eclipse.collections.api.iterator.LongIterator;
1717
import org.neo4j.graphdb.ExecutionPlanDescription;
1818
import org.neo4j.graphdb.Result;
19+
import org.neo4j.kernel.impl.coreapi.InternalTransaction;
1920
import org.neo4j.logging.NullLog;
2021
import org.neo4j.procedure.Mode;
2122
import org.neo4j.values.storable.CoordinateReferenceSystem;
@@ -942,11 +943,11 @@ public static Map<String, Object> flattenMap(Map<String, Object> map, String pre
942943
}
943944

944945
public static Node rebind(Transaction tx, Node node) {
945-
return node instanceof VirtualNode ? node : tx.getNodeById(node.getId());
946+
return node instanceof VirtualNode ? node : tx.getNodeByElementId(node.getElementId());
946947
}
947948

948949
public static Relationship rebind(Transaction tx, Relationship rel) {
949-
return rel instanceof VirtualRelationship ? rel : tx.getRelationshipById(rel.getId());
950+
return rel instanceof VirtualRelationship ? rel : tx.getRelationshipByElementId(rel.getElementId());
950951
}
951952

952953
public static <T extends Entity> T rebind(Transaction tx, T e) {
@@ -1089,7 +1090,7 @@ public static Map<String, Object> extractCredentialsIfNeeded(String url, boolean
10891090
}
10901091

10911092
public static boolean isSelfRel(Relationship rel) {
1092-
return rel.getStartNodeId() == rel.getEndNodeId();
1093+
return Objects.equals(rel.getStartNode().getElementId(), rel.getEndNode().getElementId());
10931094
}
10941095

10951096
public static String toCypherMap(Map<String, Object> map) {
@@ -1146,4 +1147,18 @@ public static <T extends Entity> T withTransactionAndRebind(GraphDatabaseService
11461147
T result = retryInTx(NullLog.getInstance(), db, action, 0, 0, r -> {});
11471148
return rebind(transaction, result);
11481149
}
1150+
1151+
public static long getNodeId(InternalTransaction tx, String elementId) {
1152+
return tx.elementIdMapper().nodeId(elementId);
1153+
}
1154+
1155+
public static long getRelationshipId(InternalTransaction tx, String elementId) {
1156+
return tx.elementIdMapper().relationshipId(elementId);
1157+
}
1158+
public static String getNodeElementId(InternalTransaction tx, long id) {
1159+
return tx.elementIdMapper().nodeElementId(id);
1160+
}
1161+
public static String getRelationshipElementId(InternalTransaction tx, long id) {
1162+
return tx.elementIdMapper().relationshipElementId(id);
1163+
}
11491164
}

common/src/main/java/org/neo4j/cypher/export/CypherResultSubGraph.java

+69-112
Original file line numberDiff line numberDiff line change
@@ -10,194 +10,151 @@
1010
import java.util.stream.Collectors;
1111
import java.util.stream.StreamSupport;
1212

13-
public class CypherResultSubGraph implements SubGraph
14-
{
13+
public class CypherResultSubGraph implements SubGraph {
1514

16-
private final SortedMap<Long, Node> nodes = new TreeMap<>();
17-
private final SortedMap<Long, Relationship> relationships = new TreeMap<>();
15+
private final SortedMap<String, Node> nodes = new TreeMap<>();
16+
private final SortedMap<String, Relationship> relationships = new TreeMap<>();
1817
private final Collection<Label> labels = new HashSet<>();
1918
private final Collection<RelationshipType> types = new HashSet<>();
2019
private final Collection<IndexDefinition> indexes = new HashSet<>();
2120
private final Collection<ConstraintDefinition> constraints = new HashSet<>();
2221

23-
public void add( Node node )
24-
{
25-
final long id = node.getId();
26-
if ( !nodes.containsKey( id ) )
27-
{
28-
addNode( id, node );
22+
public void add(Node node) {
23+
final String id = node.getElementId();
24+
if (!nodes.containsKey(id)) {
25+
addNode(id, node);
2926
}
3027
}
3128

32-
void addNode( long id, Node data )
33-
{
34-
nodes.put( id, data );
35-
labels.addAll( Iterables.asList( data.getLabels() ) );
29+
void addNode(String id, Node data) {
30+
nodes.put(id, data);
31+
labels.addAll(Iterables.asList(data.getLabels()));
3632
}
3733

38-
public void add( Relationship rel )
39-
{
40-
final long id = rel.getId();
41-
if ( !relationships.containsKey( id ) )
42-
{
43-
addRel( id, rel );
44-
add( rel.getStartNode() );
45-
add( rel.getEndNode() );
34+
public void add(Relationship rel) {
35+
final String id = rel.getElementId();
36+
if (!relationships.containsKey(id)) {
37+
addRel(id, rel);
38+
add(rel.getStartNode());
39+
add(rel.getEndNode());
4640
}
4741
}
4842

49-
public static SubGraph from(Transaction tx, Result result, boolean addBetween)
50-
{
43+
public static SubGraph from(Transaction tx, Result result, boolean addBetween) {
5144
final CypherResultSubGraph graph = new CypherResultSubGraph();
5245
final List<String> columns = result.columns();
53-
result.forEachRemaining( row ->
54-
{
55-
for ( String column : columns )
56-
{
57-
final Object value = row.get( column );
58-
graph.addToGraph( value );
46+
result.forEachRemaining(row -> {
47+
for (String column : columns) {
48+
final Object value = row.get(column);
49+
graph.addToGraph(value);
5950
}
60-
} );
61-
for ( IndexDefinition def : tx.schema().getIndexes() )
62-
{
63-
if ( def.getIndexType() != IndexType.LOOKUP )
64-
{
65-
if ( def.isNodeIndex() )
66-
{
67-
for ( Label label : def.getLabels() )
68-
{
69-
if ( graph.getLabels().contains( label ) )
70-
{
71-
graph.addIndex( def );
51+
});
52+
for (IndexDefinition def : tx.schema().getIndexes()) {
53+
if (def.getIndexType() != IndexType.LOOKUP) {
54+
if (def.isNodeIndex()) {
55+
for (Label label : def.getLabels()) {
56+
if (graph.getLabels().contains(label)) {
57+
graph.addIndex(def);
7258
break;
7359
}
7460
}
75-
}
76-
else
77-
{
78-
for ( RelationshipType type : def.getRelationshipTypes() )
79-
{
80-
if ( graph.getTypes().contains( type ) )
81-
{
82-
graph.addIndex( def );
61+
} else {
62+
for (RelationshipType type : def.getRelationshipTypes()) {
63+
if (graph.getTypes().contains(type)) {
64+
graph.addIndex(def);
8365
break;
8466
}
8567
}
8668
}
8769
}
8870
}
89-
for ( ConstraintDefinition def : tx.schema().getConstraints() )
90-
{
91-
if ( graph.getLabels().contains( def.getLabel() ) )
92-
{
93-
graph.addConstraint( def );
71+
for (ConstraintDefinition def : tx.schema().getConstraints()) {
72+
if (graph.getLabels().contains(def.getLabel())) {
73+
graph.addConstraint(def);
9474
}
95-
}
96-
if ( addBetween )
97-
{
75+
} if (addBetween) {
9876
graph.addRelationshipsBetweenNodes();
9977
}
10078
return graph;
10179
}
10280

103-
private void addIndex( IndexDefinition def )
104-
{
105-
indexes.add( def );
81+
private void addIndex(IndexDefinition def) {
82+
indexes.add(def);
10683
}
10784

108-
private void addConstraint( ConstraintDefinition def )
109-
{
110-
constraints.add( def );
85+
private void addConstraint(ConstraintDefinition def) {
86+
constraints.add(def);
11187
}
11288

113-
private void addRelationshipsBetweenNodes()
114-
{
89+
private void addRelationshipsBetweenNodes() {
11590
Set<Node> newNodes = new HashSet<>();
116-
for ( Node node : nodes.values() )
117-
{
118-
for ( Relationship relationship : node.getRelationships() )
119-
{
120-
if ( !relationships.containsKey( relationship.getId() ) )
121-
{
91+
for (Node node : nodes.values()) {
92+
for (Relationship relationship : node.getRelationships()) {
93+
if (!relationships.containsKey(relationship.getElementId())) {
12294
continue;
12395
}
12496

125-
final Node other = relationship.getOtherNode( node );
126-
if ( nodes.containsKey( other.getId() ) || newNodes.contains( other ) )
127-
{
97+
final Node other = relationship.getOtherNode(node);
98+
if (nodes.containsKey(other.getElementId()) || newNodes.contains(other)) {
12899
continue;
129100
}
130-
newNodes.add( other );
101+
newNodes.add(other);
131102
}
132103
}
133-
for ( Node node : newNodes )
134-
{
135-
add( node );
104+
for (Node node : newNodes) {
105+
add(node);
136106
}
137107
}
138108

139-
private void addToGraph( Object value )
140-
{
141-
if ( value instanceof Node )
142-
{
143-
add( (Node) value );
109+
private void addToGraph(Object value) {
110+
if (value instanceof Node) {
111+
add((Node) value);
144112
}
145-
if ( value instanceof Relationship )
146-
{
147-
add( (Relationship) value );
113+
if (value instanceof Relationship) {
114+
add((Relationship) value);
148115
}
149-
if ( value instanceof Iterable )
150-
{
151-
for ( Object inner : (Iterable) value )
152-
{
153-
addToGraph( inner );
116+
if (value instanceof Iterable) {
117+
for (Object inner : (Iterable) value) {
118+
addToGraph(inner);
154119
}
155120
}
156121
}
157122

158123
@Override
159-
public Iterable<Node> getNodes()
160-
{
124+
public Iterable<Node> getNodes() {
161125
return nodes.values();
162126
}
163127

164128
@Override
165-
public Iterable<Relationship> getRelationships()
166-
{
129+
public Iterable<Relationship> getRelationships() {
167130
return relationships.values();
168131
}
169132

170-
public Collection<Label> getLabels()
171-
{
133+
public Collection<Label> getLabels() {
172134
return labels;
173135
}
174136

175-
public Collection<RelationshipType> getTypes()
176-
{
137+
public Collection<RelationshipType> getTypes() {
177138
return types;
178139
}
179140

180-
void addRel( Long id, Relationship rel )
181-
{
182-
relationships.put( id, rel );
141+
void addRel(String id, Relationship rel) {
142+
relationships.put(id, rel);
183143
types.add(rel.getType());
184144
}
185145

186146
@Override
187-
public boolean contains( Relationship relationship )
188-
{
189-
return relationships.containsKey( relationship.getId() );
147+
public boolean contains(Relationship relationship) {
148+
return relationships.containsKey(relationship.getElementId());
190149
}
191150

192151
@Override
193-
public Iterable<IndexDefinition> getIndexes()
194-
{
152+
public Iterable<IndexDefinition> getIndexes() {
195153
return indexes;
196154
}
197155

198156
@Override
199-
public Iterable<ConstraintDefinition> getConstraints()
200-
{
157+
public Iterable<ConstraintDefinition> getConstraints() {
201158
return constraints;
202159
}
203160

@@ -245,8 +202,8 @@ public long countsForRelationship(Label start, RelationshipType type, Label end)
245202
return relationships.values().stream()
246203
.filter(r -> {
247204
boolean matchType = r.getType().equals(type);
248-
boolean matchStart = start != null ? r.getStartNode().hasLabel(start) : true;
249-
boolean matchEnd = end != null ? r.getEndNode().hasLabel(end) : true;
205+
boolean matchStart = start == null || r.getStartNode().hasLabel(start);
206+
boolean matchEnd = end == null || r.getEndNode().hasLabel(end);
250207
return matchType && matchStart && matchEnd;
251208
})
252209
.count();

common/src/main/java/org/neo4j/cypher/export/DatabaseSubGraph.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public Iterable<Relationship> getRelationships()
4242
@Override
4343
public boolean contains( Relationship relationship )
4444
{
45-
return transaction.getRelationshipById( relationship.getId() ) != null;
45+
return transaction.getRelationshipByElementId( relationship.getElementId() ) != null;
4646
}
4747

4848
@Override

0 commit comments

Comments
 (0)