|
10 | 10 | import java.util.stream.Collectors;
|
11 | 11 | import java.util.stream.StreamSupport;
|
12 | 12 |
|
13 |
| -public class CypherResultSubGraph implements SubGraph |
14 |
| -{ |
| 13 | +public class CypherResultSubGraph implements SubGraph { |
15 | 14 |
|
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<>(); |
18 | 17 | private final Collection<Label> labels = new HashSet<>();
|
19 | 18 | private final Collection<RelationshipType> types = new HashSet<>();
|
20 | 19 | private final Collection<IndexDefinition> indexes = new HashSet<>();
|
21 | 20 | private final Collection<ConstraintDefinition> constraints = new HashSet<>();
|
22 | 21 |
|
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); |
29 | 26 | }
|
30 | 27 | }
|
31 | 28 |
|
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())); |
36 | 32 | }
|
37 | 33 |
|
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()); |
46 | 40 | }
|
47 | 41 | }
|
48 | 42 |
|
49 |
| - public static SubGraph from(Transaction tx, Result result, boolean addBetween) |
50 |
| - { |
| 43 | + public static SubGraph from(Transaction tx, Result result, boolean addBetween) { |
51 | 44 | final CypherResultSubGraph graph = new CypherResultSubGraph();
|
52 | 45 | 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); |
59 | 50 | }
|
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); |
72 | 58 | break;
|
73 | 59 | }
|
74 | 60 | }
|
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); |
83 | 65 | break;
|
84 | 66 | }
|
85 | 67 | }
|
86 | 68 | }
|
87 | 69 | }
|
88 | 70 | }
|
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); |
94 | 74 | }
|
95 |
| - } |
96 |
| - if ( addBetween ) |
97 |
| - { |
| 75 | + } if (addBetween) { |
98 | 76 | graph.addRelationshipsBetweenNodes();
|
99 | 77 | }
|
100 | 78 | return graph;
|
101 | 79 | }
|
102 | 80 |
|
103 |
| - private void addIndex( IndexDefinition def ) |
104 |
| - { |
105 |
| - indexes.add( def ); |
| 81 | + private void addIndex(IndexDefinition def) { |
| 82 | + indexes.add(def); |
106 | 83 | }
|
107 | 84 |
|
108 |
| - private void addConstraint( ConstraintDefinition def ) |
109 |
| - { |
110 |
| - constraints.add( def ); |
| 85 | + private void addConstraint(ConstraintDefinition def) { |
| 86 | + constraints.add(def); |
111 | 87 | }
|
112 | 88 |
|
113 |
| - private void addRelationshipsBetweenNodes() |
114 |
| - { |
| 89 | + private void addRelationshipsBetweenNodes() { |
115 | 90 | 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())) { |
122 | 94 | continue;
|
123 | 95 | }
|
124 | 96 |
|
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)) { |
128 | 99 | continue;
|
129 | 100 | }
|
130 |
| - newNodes.add( other ); |
| 101 | + newNodes.add(other); |
131 | 102 | }
|
132 | 103 | }
|
133 |
| - for ( Node node : newNodes ) |
134 |
| - { |
135 |
| - add( node ); |
| 104 | + for (Node node : newNodes) { |
| 105 | + add(node); |
136 | 106 | }
|
137 | 107 | }
|
138 | 108 |
|
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); |
144 | 112 | }
|
145 |
| - if ( value instanceof Relationship ) |
146 |
| - { |
147 |
| - add( (Relationship) value ); |
| 113 | + if (value instanceof Relationship) { |
| 114 | + add((Relationship) value); |
148 | 115 | }
|
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); |
154 | 119 | }
|
155 | 120 | }
|
156 | 121 | }
|
157 | 122 |
|
158 | 123 | @Override
|
159 |
| - public Iterable<Node> getNodes() |
160 |
| - { |
| 124 | + public Iterable<Node> getNodes() { |
161 | 125 | return nodes.values();
|
162 | 126 | }
|
163 | 127 |
|
164 | 128 | @Override
|
165 |
| - public Iterable<Relationship> getRelationships() |
166 |
| - { |
| 129 | + public Iterable<Relationship> getRelationships() { |
167 | 130 | return relationships.values();
|
168 | 131 | }
|
169 | 132 |
|
170 |
| - public Collection<Label> getLabels() |
171 |
| - { |
| 133 | + public Collection<Label> getLabels() { |
172 | 134 | return labels;
|
173 | 135 | }
|
174 | 136 |
|
175 |
| - public Collection<RelationshipType> getTypes() |
176 |
| - { |
| 137 | + public Collection<RelationshipType> getTypes() { |
177 | 138 | return types;
|
178 | 139 | }
|
179 | 140 |
|
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); |
183 | 143 | types.add(rel.getType());
|
184 | 144 | }
|
185 | 145 |
|
186 | 146 | @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()); |
190 | 149 | }
|
191 | 150 |
|
192 | 151 | @Override
|
193 |
| - public Iterable<IndexDefinition> getIndexes() |
194 |
| - { |
| 152 | + public Iterable<IndexDefinition> getIndexes() { |
195 | 153 | return indexes;
|
196 | 154 | }
|
197 | 155 |
|
198 | 156 | @Override
|
199 |
| - public Iterable<ConstraintDefinition> getConstraints() |
200 |
| - { |
| 157 | + public Iterable<ConstraintDefinition> getConstraints() { |
201 | 158 | return constraints;
|
202 | 159 | }
|
203 | 160 |
|
@@ -245,8 +202,8 @@ public long countsForRelationship(Label start, RelationshipType type, Label end)
|
245 | 202 | return relationships.values().stream()
|
246 | 203 | .filter(r -> {
|
247 | 204 | 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); |
250 | 207 | return matchType && matchStart && matchEnd;
|
251 | 208 | })
|
252 | 209 | .count();
|
|
0 commit comments