-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
RelationshipTraversalCursorTestBase.java
395 lines (344 loc) · 14.2 KB
/
RelationshipTraversalCursorTestBase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.internal.kernel.api;
import org.junit.Assume;
import org.junit.Test;
import java.util.Map;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.internal.kernel.api.exceptions.KernelException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.neo4j.graphdb.Direction.BOTH;
import static org.neo4j.graphdb.Direction.INCOMING;
import static org.neo4j.graphdb.Direction.OUTGOING;
import static org.neo4j.graphdb.RelationshipType.withName;
import static org.neo4j.internal.kernel.api.RelationshipTestSupport.assertCount;
import static org.neo4j.internal.kernel.api.RelationshipTestSupport.assertCounts;
import static org.neo4j.internal.kernel.api.RelationshipTestSupport.count;
public abstract class RelationshipTraversalCursorTestBase<G extends KernelAPIReadTestSupport>
extends KernelAPIReadTestBase<G>
{
private static long bare, start, end;
private static RelationshipTestSupport.StartNode sparse, dense;
protected boolean supportsDirectTraversal()
{
return true;
}
protected boolean supportsSparseNodes()
{
return true;
}
private static void bareStartAndEnd( GraphDatabaseService graphDb )
{
try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() )
{
bare = graphDb.createNode().getId();
Node x = graphDb.createNode(), y = graphDb.createNode();
start = x.getId();
end = y.getId();
x.createRelationshipTo( y, withName( "GEN" ) );
tx.success();
}
}
@Override
void createTestGraph( GraphDatabaseService graphDb )
{
RelationshipTestSupport.someGraph( graphDb );
bareStartAndEnd( graphDb );
sparse = RelationshipTestSupport.sparse( graphDb );
dense = RelationshipTestSupport.dense( graphDb );
}
@Test
public void shouldNotAccessGroupsOfBareNode()
{
// given
try ( NodeCursor node = cursors.allocateNodeCursor();
RelationshipGroupCursor group = cursors.allocateRelationshipGroupCursor() )
{
// when
read.singleNode( bare, node );
assertTrue( "access node", node.next() );
node.relationships( group );
// then
assertFalse( "access group", group.next() );
}
}
@Test
public void shouldTraverseRelationshipsOfGivenType()
{
// given
try ( NodeCursor node = cursors.allocateNodeCursor();
RelationshipGroupCursor group = cursors.allocateRelationshipGroupCursor();
RelationshipTraversalCursor relationship = cursors.allocateRelationshipTraversalCursor() )
{
int empty = 0;
// when
read.allNodesScan( node );
while ( node.next() )
{
node.relationships( group );
boolean none = true;
while ( group.next() )
{
none = false;
Sizes degree = new Sizes();
group.outgoing( relationship );
while ( relationship.next() )
{
assertEquals( "node #" + node.nodeReference() +
" relationship should have same label as group", group.relationshipLabel(),
relationship.label() );
degree.outgoing++;
}
group.incoming( relationship );
while ( relationship.next() )
{
assertEquals( "node #" + node.nodeReference() +
"relationship should have same label as group", group.relationshipLabel(),
relationship.label() );
degree.incoming++;
}
group.loops( relationship );
while ( relationship.next() )
{
assertEquals( "node #" + node.nodeReference() +
"relationship should have same label as group", group.relationshipLabel(),
relationship.label() );
degree.loop++;
}
// then
assertNotEquals( "all", 0, degree.incoming + degree.outgoing + degree.loop );
assertEquals(
"node #" + node.nodeReference() + " outgoing",
group.outgoingCount(),
degree.outgoing );
assertEquals(
"node #" + node.nodeReference() + " incoming",
group.incomingCount(),
degree.incoming );
assertEquals( "node #" + node.nodeReference() + " loop", group.loopCount(), degree.loop );
assertEquals( "node #" + node.nodeReference() + " all = incoming + outgoing - loop",
group.totalCount(), degree.incoming + degree.outgoing + degree.loop );
}
if ( none )
{
empty++;
}
}
// then
assertEquals( "number of empty nodes", 1, empty );
}
}
@Test
public void shouldFollowSpecificRelationship()
{
// given
try ( NodeCursor node = cursors.allocateNodeCursor();
RelationshipGroupCursor group = cursors.allocateRelationshipGroupCursor();
RelationshipTraversalCursor relationship = cursors.allocateRelationshipTraversalCursor() )
{
// when - traversing from start to end
read.singleNode( start, node );
assertTrue( "access start node", node.next() );
node.relationships( group );
assertTrue( "access relationship group", group.next() );
group.outgoing( relationship );
assertTrue( "access outgoing relationships", relationship.next() );
// then
assertEquals( "source node", start, relationship.sourceNodeReference() );
assertEquals( "target node", end, relationship.targetNodeReference() );
assertEquals( "node of origin", start, relationship.originNodeReference() );
assertEquals( "neighbouring node", end, relationship.neighbourNodeReference() );
assertEquals( "relationship should have same label as group", group.relationshipLabel(),
relationship.label() );
assertFalse( "only a single relationship", relationship.next() );
group.incoming( relationship );
assertFalse( "no incoming relationships", relationship.next() );
group.loops( relationship );
assertFalse( "no loop relationships", relationship.next() );
assertFalse( "only a single group", group.next() );
// when - traversing from end to start
read.singleNode( end, node );
assertTrue( "access start node", node.next() );
node.relationships( group );
assertTrue( "access relationship group", group.next() );
group.incoming( relationship );
assertTrue( "access incoming relationships", relationship.next() );
// then
assertEquals( "source node", start, relationship.sourceNodeReference() );
assertEquals( "target node", end, relationship.targetNodeReference() );
assertEquals( "node of origin", end, relationship.originNodeReference() );
assertEquals( "neighbouring node", start, relationship.neighbourNodeReference() );
assertEquals( "relationship should have same label as group", group.relationshipLabel(),
relationship.label() );
assertFalse( "only a single relationship", relationship.next() );
group.outgoing( relationship );
assertFalse( "no outgoing relationships", relationship.next() );
group.loops( relationship );
assertFalse( "no loop relationships", relationship.next() );
assertFalse( "only a single group", group.next() );
}
}
@Test
public void shouldHaveBeenAbleToCreateDenseAndSparseNodes()
{
// given
try ( NodeCursor node = cursors.allocateNodeCursor() )
{
read.singleNode( dense.id, node );
assertTrue( "access dense node", node.next() );
assertTrue( "dense node", node.isDense() );
read.singleNode( sparse.id, node );
assertTrue( "access sparse node", node.next() );
assertFalse( "sparse node", node.isDense() && supportsSparseNodes() );
}
}
@Test
public void shouldTraverseSparseNodeViaGroups() throws Exception
{
traverseViaGroups( sparse, false );
}
@Test
public void shouldTraverseDenseNodeViaGroups() throws Exception
{
traverseViaGroups( dense, false );
}
@Test
public void shouldTraverseSparseNodeViaGroupsWithDetachedReferences() throws Exception
{
traverseViaGroups( sparse, true );
}
@Test
public void shouldTraverseDenseNodeViaGroupsWithDetachedReferences() throws Exception
{
traverseViaGroups( dense, true );
}
@Test
public void shouldTraverseSparseNodeWithoutGroups() throws Exception
{
Assume.assumeTrue( supportsSparseNodes() && supportsDirectTraversal() );
traverseWithoutGroups( sparse, false );
}
@Test
public void shouldTraverseDenseNodeWithoutGroups() throws Exception
{
Assume.assumeTrue( supportsDirectTraversal() );
traverseWithoutGroups( dense, false );
}
@Test
public void shouldTraverseSparseNodeWithoutGroupsWithDetachedReferences() throws Exception
{
Assume.assumeTrue( supportsSparseNodes() );
traverseWithoutGroups( sparse, true );
}
@Test
public void shouldTraverseDenseNodeWithoutGroupsWithDetachedReferences() throws Exception
{
Assume.assumeTrue( supportsDirectTraversal() );
traverseWithoutGroups( dense, true );
}
private void traverseViaGroups( RelationshipTestSupport.StartNode start, boolean detached ) throws KernelException
{
// given
Map<String,Integer> expectedCounts = start.expectedCounts();
try ( NodeCursor node = cursors.allocateNodeCursor();
RelationshipGroupCursor group = cursors.allocateRelationshipGroupCursor();
RelationshipTraversalCursor relationship = cursors.allocateRelationshipTraversalCursor() )
{
// when
read.singleNode( start.id, node );
assertTrue( "access node", node.next() );
if ( detached )
{
read.relationshipGroups( start.id, node.relationshipGroupReference(), group );
}
else
{
node.relationships( group );
}
while ( group.next() )
{
// outgoing
if ( detached )
{
read.relationships( start.id, group.outgoingReference(), relationship );
}
else
{
group.outgoing( relationship );
}
// then
assertCount( session, relationship, expectedCounts, group.relationshipLabel(), OUTGOING );
// incoming
if ( detached )
{
read.relationships( start.id, group.incomingReference(), relationship );
}
else
{
group.incoming( relationship );
}
// then
assertCount( session, relationship, expectedCounts, group.relationshipLabel(), INCOMING );
// loops
if ( detached )
{
read.relationships( start.id, group.loopsReference(), relationship );
}
else
{
group.loops( relationship );
}
// then
assertCount( session, relationship, expectedCounts, group.relationshipLabel(), BOTH );
}
}
}
private void traverseWithoutGroups( RelationshipTestSupport.StartNode start, boolean detached )
throws KernelException
{
// given
try ( NodeCursor node = cursors.allocateNodeCursor();
RelationshipTraversalCursor relationship = cursors.allocateRelationshipTraversalCursor() )
{
// when
read.singleNode( start.id, node );
assertTrue( "access node", node.next() );
if ( detached )
{
read.relationships( start.id, node.allRelationshipsReference(), relationship );
}
else
{
node.allRelationships( relationship );
}
Map<String,Integer> counts = count( session, relationship );
// then
assertCounts( start.expectedCounts(), counts );
}
}
private static class Sizes
{
int incoming, outgoing, loop;
}
}