-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
TreeNodeTest.java
759 lines (649 loc) · 26.1 KB
/
TreeNodeTest.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/*
* 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.index.internal.gbptree;
import org.apache.commons.lang3.mutable.MutableLong;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import java.io.IOException;
import org.neo4j.io.pagecache.PageCursor;
import org.neo4j.test.rule.RandomRule;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.neo4j.index.internal.gbptree.GenerationSafePointerPair.pointer;
import static org.neo4j.index.internal.gbptree.GenerationSafePointerPair.resultIsFromSlotA;
import static org.neo4j.index.internal.gbptree.TreeNode.NO_NODE_FLAG;
import static org.neo4j.index.internal.gbptree.TreeNode.Type.INTERNAL;
import static org.neo4j.index.internal.gbptree.TreeNode.Type.LEAF;
public class TreeNodeTest
{
private static final int STABLE_GENERATION = 1;
private static final int CRASH_GENERATION = 2;
private static final int UNSTABLE_GENERATION = 3;
private static final int HIGH_GENERATION = 4;
private static final int PAGE_SIZE = 512;
private final PageCursor cursor = new PageAwareByteArrayCursor( PAGE_SIZE );
private final Layout<MutableLong,MutableLong> layout = new SimpleLongLayout();
private final TreeNode<MutableLong,MutableLong> node = new TreeNode<>( PAGE_SIZE, layout );
@Rule
public final RandomRule random = new RandomRule();
@Before
public void prepareCursor() throws IOException
{
cursor.next();
}
@Test
public void shouldInitializeLeaf() throws Exception
{
// WHEN
TreeNode.initializeLeaf( cursor, STABLE_GENERATION, UNSTABLE_GENERATION );
// THEN
assertEquals( TreeNode.NODE_TYPE_TREE_NODE, TreeNode.nodeType( cursor ) );
assertTrue( TreeNode.isLeaf( cursor ) );
assertFalse( TreeNode.isInternal( cursor ) );
assertEquals( UNSTABLE_GENERATION, TreeNode.generation( cursor ) );
assertEquals( 0, TreeNode.keyCount( cursor ) );
assertEquals( NO_NODE_FLAG, leftSibling( cursor, STABLE_GENERATION, UNSTABLE_GENERATION ) );
assertEquals( NO_NODE_FLAG, rightSibling( cursor, STABLE_GENERATION, UNSTABLE_GENERATION ) );
assertEquals( NO_NODE_FLAG, successor( cursor, STABLE_GENERATION, UNSTABLE_GENERATION ) );
}
@Test
public void shouldInitializeInternal() throws Exception
{
// WHEN
TreeNode.initializeInternal( cursor, STABLE_GENERATION, UNSTABLE_GENERATION );
// THEN
assertEquals( TreeNode.NODE_TYPE_TREE_NODE, TreeNode.nodeType( cursor ) );
assertFalse( TreeNode.isLeaf( cursor ) );
assertTrue( TreeNode.isInternal( cursor ) );
assertEquals( UNSTABLE_GENERATION, TreeNode.generation( cursor ) );
assertEquals( 0, TreeNode.keyCount( cursor ) );
assertEquals( NO_NODE_FLAG, leftSibling( cursor, STABLE_GENERATION, UNSTABLE_GENERATION ) );
assertEquals( NO_NODE_FLAG, rightSibling( cursor, STABLE_GENERATION, UNSTABLE_GENERATION ) );
assertEquals( NO_NODE_FLAG, successor( cursor, STABLE_GENERATION, UNSTABLE_GENERATION ) );
}
@Test
public void shouldWriteAndReadMaxGeneration() throws Exception
{
// GIVEN
TreeNode.initializeLeaf( cursor, STABLE_GENERATION, UNSTABLE_GENERATION );
// WHEN
TreeNode.setGeneration( cursor, GenerationSafePointer.MAX_GENERATION );
// THEN
long generation = TreeNode.generation( cursor );
assertEquals( GenerationSafePointer.MAX_GENERATION, generation );
}
@Test
public void shouldThrowIfWriteTooLargeGeneration() throws Exception
{
// GIVEN
TreeNode.initializeLeaf( cursor, STABLE_GENERATION, UNSTABLE_GENERATION );
// THEN
try
{
TreeNode.setGeneration( cursor, GenerationSafePointer.MAX_GENERATION + 1 );
fail( "Expected throw" );
}
catch ( IllegalArgumentException e )
{
// Good
}
}
@Test
public void shouldThrowIfWriteTooSmallGeneration() throws Exception
{
// GIVEN
TreeNode.initializeLeaf( cursor, STABLE_GENERATION, UNSTABLE_GENERATION );
// THEN
try
{
TreeNode.setGeneration( cursor, GenerationSafePointer.MIN_GENERATION - 1 );
fail( "Expected throw" );
}
catch ( IllegalArgumentException e )
{
// Good
}
}
private void shouldSetAndGetKey( TreeNode.Type type ) throws Exception
{
// GIVEN
MutableLong key = layout.newKey();
// WHEN
long firstKey = 10;
key.setValue( firstKey );
node.insertKeyAt( cursor, key, 0, 0, type );
long otherKey = 19;
key.setValue( otherKey );
node.insertKeyAt( cursor, key, 1, 1, type );
// THEN
assertEquals( firstKey, node.keyAt( cursor, key, 0, type ).longValue() );
assertEquals( otherKey, node.keyAt( cursor, key, 1, type ).longValue() );
}
@Test
public void shouldSetAndGetKeyInLeaf() throws Exception
{
// GIVEN
TreeNode.initializeLeaf( cursor, STABLE_GENERATION, UNSTABLE_GENERATION );
// THEN
shouldSetAndGetKey( INTERNAL );
}
@Test
public void shouldSetAndGetKeyInInternal() throws Exception
{
// GIVEN
TreeNode.initializeInternal( cursor, STABLE_GENERATION, UNSTABLE_GENERATION );
// THEN
shouldSetAndGetKey( INTERNAL );
}
private void shouldRemoveKey( TreeNode.Type type ) throws Exception
{
// GIVEN
MutableLong key = layout.newKey();
long firstKey = 10;
key.setValue( firstKey );
node.insertKeyAt( cursor, key, 0, 0, type );
long otherKey = 19;
key.setValue( otherKey );
node.insertKeyAt( cursor, key, 1, 1, type );
long thirdKey = 123;
key.setValue( thirdKey );
node.insertKeyAt( cursor, key, 2, 2, type );
// WHEN
node.removeKeyAt( cursor, 1, 3, type );
// THEN
assertEquals( firstKey, node.keyAt( cursor, key, 0, type ).longValue() );
assertEquals( thirdKey, node.keyAt( cursor, key, 1, type ).longValue() );
}
@Test
public void shouldRemoveKeyInLeaf() throws Exception
{
// GIVEN
TreeNode.initializeLeaf( cursor, STABLE_GENERATION, UNSTABLE_GENERATION );
// THEN
shouldRemoveKey( LEAF );
}
@Test
public void shouldRemoveKeyInInternal() throws Exception
{
// GIVEN
TreeNode.initializeInternal( cursor, STABLE_GENERATION, UNSTABLE_GENERATION );
// THEN
shouldRemoveKey( INTERNAL );
}
@Test
public void shouldSetAndGetKeyValue() throws Exception
{
// GIVEN
TreeNode.initializeLeaf( cursor, STABLE_GENERATION, UNSTABLE_GENERATION );
MutableLong key = layout.newKey();
MutableLong value = layout.newValue();
// WHEN
long firstKey = 24680;
key.setValue( firstKey );
long firstValue = 123456789;
value.setValue( firstValue );
node.insertKeyValueAt( cursor, key , value, 0, 0 );
long otherKey = 13579;
key.setValue( otherKey );
long otherValue = 987654321;
value.setValue( otherValue );
node.insertKeyValueAt( cursor, key, value, 1, 1 );
// THEN
assertEquals( firstKey, node.keyAt( cursor, key, 0, LEAF ).longValue() );
assertEquals( otherKey, node.keyAt( cursor, key, 1, LEAF ).longValue() );
assertEquals( firstValue, node.valueAt( cursor, value, 0 ).longValue() );
assertEquals( otherValue, node.valueAt( cursor, value, 1 ).longValue() );
}
@Test
public void shouldRemoveValue() throws Exception
{
// GIVEN
TreeNode.initializeLeaf( cursor, STABLE_GENERATION, UNSTABLE_GENERATION );
MutableLong key = layout.newKey();
MutableLong value = layout.newValue();
long firstKey = 24680;
key.setValue( firstKey );
long firstValue = 123456789;
value.setValue( firstValue );
node.insertKeyValueAt( cursor, key, value, 0, 0 );
long otherKey = 13579;
key.setValue( otherKey );
long otherValue = 987654321;
value.setValue( otherValue );
node.insertKeyValueAt( cursor, key, value, 1, 1 );
long thirdKey = 1029384756;
key.setValue( thirdKey );
long thirdValue = 49756;
value.setValue( thirdValue );
node.insertKeyValueAt( cursor, key, value, 2, 2 );
// WHEN
node.removeKeyValueAt( cursor, 1, 3 );
// THEN
assertEquals( firstKey, node.keyAt( cursor, key, 0, LEAF ).longValue() );
assertEquals( thirdKey, node.keyAt( cursor, key, 1, LEAF ).longValue() );
assertEquals( firstValue, node.valueAt( cursor, value, 0 ).longValue() );
assertEquals( thirdValue, node.valueAt( cursor, value, 1 ).longValue() );
}
@Test
public void shouldOverwriteValue() throws Exception
{
// GIVEN
TreeNode.initializeLeaf( cursor, STABLE_GENERATION, UNSTABLE_GENERATION );
MutableLong key = layout.newKey();
MutableLong value = layout.newValue();
key.setValue( 10 );
value.setValue( 1 );
node.insertKeyValueAt( cursor, key, value, 0, 0 );
// WHEN
long overwrittenValue = 2;
value.setValue( overwrittenValue );
node.setValueAt( cursor, value, 0 );
// THEN
assertEquals( overwrittenValue, node.valueAt( cursor, value, 0 ).longValue() );
}
@Test
public void shouldSetAndGetChild() throws Exception
{
// GIVEN
TreeNode.initializeInternal( cursor, STABLE_GENERATION, UNSTABLE_GENERATION );
// WHEN
long firstChild = 123456789;
node.insertChildAt( cursor, firstChild, 0, 0, STABLE_GENERATION, UNSTABLE_GENERATION );
long otherChild = 987654321;
node.insertChildAt( cursor, otherChild, 1, 1, STABLE_GENERATION, UNSTABLE_GENERATION );
// THEN
assertEquals( firstChild, childAt( cursor, 0, STABLE_GENERATION, UNSTABLE_GENERATION ) );
assertEquals( otherChild, childAt( cursor, 1, STABLE_GENERATION, UNSTABLE_GENERATION ) );
}
@Test
public void shouldOverwriteChild() throws Exception
{
// GIVEN
long child = GenerationSafePointer.MIN_POINTER;
TreeNode.initializeInternal( cursor, STABLE_GENERATION, UNSTABLE_GENERATION );
node.insertChildAt( cursor, child, 0, 0, STABLE_GENERATION, UNSTABLE_GENERATION );
// WHEN
long overwrittenChild = child + 1;
node.setChildAt( cursor, overwrittenChild, 0, STABLE_GENERATION, UNSTABLE_GENERATION );
// THEN
assertEquals( overwrittenChild, childAt( cursor, 0, STABLE_GENERATION, UNSTABLE_GENERATION ) );
}
@Test
public void shouldSetAndGetKeyCount() throws Exception
{
// GIVEN
TreeNode.initializeLeaf( cursor, STABLE_GENERATION, UNSTABLE_GENERATION );
assertEquals( 0, TreeNode.keyCount( cursor ) );
// WHEN
int keyCount = 5;
TreeNode.setKeyCount( cursor, keyCount );
// THEN
assertEquals( keyCount, TreeNode.keyCount( cursor ) );
}
@Test
public void shouldSetAndGetSiblings() throws Exception
{
// GIVEN
TreeNode.initializeLeaf( cursor, STABLE_GENERATION, UNSTABLE_GENERATION );
// WHEN
TreeNode.setLeftSibling( cursor, 123, STABLE_GENERATION, UNSTABLE_GENERATION );
TreeNode.setRightSibling( cursor, 456, STABLE_GENERATION, UNSTABLE_GENERATION );
// THEN
assertEquals( 123, leftSibling( cursor, STABLE_GENERATION, UNSTABLE_GENERATION ) );
assertEquals( 456, rightSibling( cursor, STABLE_GENERATION, UNSTABLE_GENERATION ) );
}
@Test
public void shouldSetAndGetSuccessor() throws Exception
{
// GIVEN
TreeNode.initializeLeaf( cursor, STABLE_GENERATION, UNSTABLE_GENERATION );
// WHEN
TreeNode.setSuccessor( cursor, 123, STABLE_GENERATION, UNSTABLE_GENERATION );
// THEN
assertEquals( 123, successor( cursor, STABLE_GENERATION, UNSTABLE_GENERATION ) );
}
@Test
public void shouldReadAndInsertKeys() throws Exception
{
// GIVEN
TreeNode.initializeLeaf( cursor, STABLE_GENERATION, UNSTABLE_GENERATION );
MutableLong key = layout.newKey();
key.setValue( 1 );
node.insertKeyAt( cursor, key, 0, 0, LEAF );
key.setValue( 3 );
node.insertKeyAt( cursor, key, 1, 1, LEAF );
// WHEN
key.setValue( 2 );
node.insertKeyAt( cursor, key, 1, 2, LEAF );
// THEN
assertEquals( 1, node.keyAt( cursor, key, 0, LEAF ).longValue() );
assertEquals( 2, node.keyAt( cursor, key, 1, LEAF ).longValue() );
assertEquals( 3, node.keyAt( cursor, key, 2, LEAF ).longValue() );
}
@Test
public void shouldReadAndInsertKeyValues() throws Exception
{
// GIVEN
TreeNode.initializeLeaf( cursor, STABLE_GENERATION, UNSTABLE_GENERATION );
MutableLong key = layout.newKey();
MutableLong value = layout.newValue();
key.setValue( 1 );
value.setValue( 1 );
node.insertKeyValueAt( cursor, key, value, 0, 0 );
key.setValue( 3 );
value.setValue( 3 );
node.insertKeyValueAt( cursor, key, value, 1, 1 );
// WHEN
key.setValue( 2 );
value.setValue( 2 );
node.insertKeyValueAt( cursor, key, value, 1, 2 );
// THEN
assertEquals( 1, node.keyAt( cursor, key, 0, LEAF ).longValue() );
assertEquals( 2, node.keyAt( cursor, key, 1, LEAF ).longValue() );
assertEquals( 3, node.keyAt( cursor, key, 2, LEAF ).longValue() );
assertEquals( 1, node.valueAt( cursor, value, 0 ).longValue() );
assertEquals( 2, node.valueAt( cursor, value, 1 ).longValue() );
assertEquals( 3, node.valueAt( cursor, value, 2 ).longValue() );
}
@Test
public void shouldReadAndInsertChildren() throws Exception
{
// GIVEN
long firstChild = GenerationSafePointer.MIN_POINTER;
long secondChild = firstChild + 1;
long thirdChild = secondChild + 1;
TreeNode.initializeInternal( cursor, STABLE_GENERATION, UNSTABLE_GENERATION );
node.insertChildAt( cursor, firstChild, 0, 0, STABLE_GENERATION, UNSTABLE_GENERATION );
node.insertChildAt( cursor, thirdChild, 1, 1, STABLE_GENERATION, UNSTABLE_GENERATION );
// WHEN
node.insertChildAt( cursor, secondChild, 1, 2, STABLE_GENERATION, UNSTABLE_GENERATION );
// THEN
assertEquals( firstChild, childAt( cursor, 0, STABLE_GENERATION, UNSTABLE_GENERATION ) );
assertEquals( secondChild, childAt( cursor, 1, STABLE_GENERATION, UNSTABLE_GENERATION ) );
assertEquals( thirdChild, childAt( cursor, 2, STABLE_GENERATION, UNSTABLE_GENERATION ) );
}
@Test
public void shouldInsertAndRemoveRandomKeysAndValues() throws Exception
{
// This test doesn't care about sorting, that's an aspect that lies outside of TreeNode, really
// GIVEN
TreeNode.initializeLeaf( cursor, STABLE_GENERATION, UNSTABLE_GENERATION );
int maxKeyCount = node.leafMaxKeyCount();
// add +1 to these to simplify some array logic in the test itself
long[] expectedKeys = new long[maxKeyCount + 1];
long[] expectedValues = new long[maxKeyCount + 1];
int expectedKeyCount = 0;
MutableLong key = layout.newKey();
MutableLong value = layout.newValue();
// WHEN/THEN
for ( int i = 0; i < 1000; i++ )
{
if ( random.nextFloat() < 0.7 )
{ // 70% insert
if ( expectedKeyCount < maxKeyCount )
{ // there's room
int position = expectedKeyCount == 0 ? 0 : random.nextInt( expectedKeyCount );
// ensure unique
do
{
key.setValue( random.nextLong() );
}
while ( contains( expectedKeys, 0, expectedKeyCount, key.longValue() ) );
value.setValue( random.nextLong() );
node.insertKeyValueAt( cursor, key, value, position, expectedKeyCount );
insert( expectedKeys, expectedKeyCount, key.longValue(), position );
insert( expectedValues, expectedKeyCount, value.longValue(), position );
TreeNode.setKeyCount( cursor, ++expectedKeyCount );
}
}
else
{ // 30% remove
if ( expectedKeyCount > 0 )
{ // there are things to remove
int position = random.nextInt( expectedKeyCount );
node.keyAt( cursor, key, position, LEAF );
node.valueAt( cursor, value, position );
node.removeKeyValueAt( cursor, position, expectedKeyCount );
long expectedKey = remove( expectedKeys, expectedKeyCount, position );
long expectedValue = remove( expectedValues, expectedKeyCount, position );
assertEquals( expectedKey, key.longValue() );
assertEquals( expectedValue, value.longValue() );
TreeNode.setKeyCount( cursor, --expectedKeyCount );
}
}
}
// THEN
assertEquals( expectedKeyCount, TreeNode.keyCount( cursor ) );
for ( int i = 0; i < expectedKeyCount; i++ )
{
long expectedKey = expectedKeys[i];
node.keyAt( cursor, key, i, LEAF );
assertEquals( expectedKey, key.longValue() );
long expectedValue = expectedValues[i];
node.valueAt( cursor, value, i );
assertEquals( "For key " + key.longValue(), expectedValue, value.longValue() );
}
}
@Test
public void shouldAssertPageSizeBigEnoughForAtLeastTwoKeys() throws Exception
{
// WHEN
try
{
new TreeNode<>( TreeNode.HEADER_LENGTH + layout.keySize() + layout.valueSize(), layout );
fail( "Should have failed" );
}
catch ( MetadataMismatchException e )
{
// THEN good
}
}
@Test
public void shouldReadPointerGenerationFromAbsoluteOffsetSlotA() throws Exception
{
// GIVEN
long generation = UNSTABLE_GENERATION;
long pointer = 12;
TreeNode.setRightSibling( cursor, pointer, STABLE_GENERATION, generation );
// WHEN
long readResult = TreeNode.rightSibling( cursor, STABLE_GENERATION, generation );
long readGeneration = node.pointerGeneration( cursor, readResult );
// THEN
assertEquals( pointer, pointer( readResult ) );
assertEquals( generation, readGeneration );
assertTrue( resultIsFromSlotA( readResult ) );
}
@Test
public void shouldReadPointerGenerationFromAbsoluteOffsetSlotB() throws Exception
{
// GIVEN
long generation = HIGH_GENERATION;
long oldPointer = 12;
long pointer = 123;
TreeNode.setRightSibling( cursor, oldPointer, STABLE_GENERATION, UNSTABLE_GENERATION );
TreeNode.setRightSibling( cursor, pointer, UNSTABLE_GENERATION, generation );
// WHEN
long readResult = TreeNode.rightSibling( cursor, UNSTABLE_GENERATION, generation );
long readGeneration = node.pointerGeneration( cursor, readResult );
// THEN
assertEquals( pointer, pointer( readResult ) );
assertEquals( generation, readGeneration );
assertFalse( resultIsFromSlotA( readResult ) );
}
@Test
public void shouldReadPointerGenerationFromLogicalPosSlotA() throws Exception
{
// GIVEN
long generation = UNSTABLE_GENERATION;
long pointer = 12;
int childPos = 2;
node.setChildAt( cursor, pointer, childPos, STABLE_GENERATION, generation );
// WHEN
long readResult = node.childAt( cursor, childPos, STABLE_GENERATION, generation );
long readGeneration = node.pointerGeneration( cursor, readResult );
// THEN
assertEquals( pointer, pointer( readResult ) );
assertEquals( generation, readGeneration );
assertTrue( resultIsFromSlotA( readResult ) );
}
@Test
public void shouldReadPointerGenerationFromLogicalPosZeroSlotA() throws Exception
{
// GIVEN
long generation = UNSTABLE_GENERATION;
long pointer = 12;
int childPos = 0;
node.setChildAt( cursor, pointer, childPos, STABLE_GENERATION, generation );
// WHEN
long readResult = node.childAt( cursor, childPos, STABLE_GENERATION, generation );
long readGeneration = node.pointerGeneration( cursor, readResult );
// THEN
assertEquals( pointer, pointer( readResult ) );
assertEquals( generation, readGeneration );
assertTrue( resultIsFromSlotA( readResult ) );
}
@Test
public void shouldReadPointerGenerationFromLogicalPosZeroSlotB() throws Exception
{
// GIVEN
long generation = HIGH_GENERATION;
long oldPointer = 13;
long pointer = 12;
int childPos = 0;
node.setChildAt( cursor, oldPointer, childPos, STABLE_GENERATION, UNSTABLE_GENERATION );
node.setChildAt( cursor, pointer, childPos, UNSTABLE_GENERATION, generation );
// WHEN
long readResult = node.childAt( cursor, childPos, UNSTABLE_GENERATION, generation );
long readGeneration = node.pointerGeneration( cursor, readResult );
// THEN
assertEquals( pointer, pointer( readResult ) );
assertEquals( generation, readGeneration );
assertFalse( resultIsFromSlotA( readResult ) );
}
@Test
public void shouldReadPointerGenerationFromLogicalPosSlotB() throws Exception
{
// GIVEN
long generation = HIGH_GENERATION;
long oldPointer = 12;
long pointer = 123;
int childPos = 2;
node.setChildAt( cursor, oldPointer, childPos, STABLE_GENERATION, UNSTABLE_GENERATION );
node.setChildAt( cursor, pointer, childPos, UNSTABLE_GENERATION, generation );
// WHEN
long readResult = node.childAt( cursor, childPos, UNSTABLE_GENERATION, generation );
long readGeneration = node.pointerGeneration( cursor, readResult );
// THEN
assertEquals( pointer, pointer( readResult ) );
assertEquals( generation, readGeneration );
assertFalse( resultIsFromSlotA( readResult ) );
}
@Test
public void shouldThrowIfReadingPointerGenerationOnWriteResult() throws Exception
{
// GIVEN
long writeResult = GenerationSafePointerPair.write( cursor, 123, STABLE_GENERATION, UNSTABLE_GENERATION );
try
{
// WHEN
node.pointerGeneration( cursor, writeResult );
fail( "Should have failed" );
}
catch ( IllegalArgumentException e )
{
// THEN good
}
}
@Test
public void shouldThrowIfReadingPointerGenerationOnZeroReadResultHeader() throws Exception
{
// GIVEN
long pointer = 123;
try
{
// WHEN
node.pointerGeneration( cursor, pointer );
fail( "Should have failed" );
}
catch ( IllegalArgumentException e )
{
// THEN good
}
}
@Test
public void shouldUseLogicalGenerationPosWhenReadingChild() throws Exception
{
// GIVEN
long child = 101;
int pos = 3;
node.setChildAt( cursor, child, pos, STABLE_GENERATION, UNSTABLE_GENERATION );
// WHEN
long result = node.childAt( cursor, pos, STABLE_GENERATION, UNSTABLE_GENERATION );
// THEN
assertTrue( GenerationSafePointerPair.isLogicalPos( result ) );
}
private static long remove( long[] from, int length, int position )
{
long result = from[position];
for ( int i = position; i < length; i++ )
{
from[i] = from[i + 1];
}
return result;
}
private static void insert( long[] into, int length, long value, int position )
{
for ( int i = length - 1; i >= position; i-- )
{
into[i + 1] = into[i];
}
into[position] = value;
}
private static boolean contains( long[] array, int offset, int length, long value )
{
for ( int i = 0; i < length; i++ )
{
if ( array[offset + i] == value )
{
return true;
}
}
return false;
}
private long childAt( PageCursor cursor, int pos, long stableGeneration, long unstableGeneration )
{
return pointer( node.childAt( cursor, pos, stableGeneration, unstableGeneration ) );
}
private long rightSibling( PageCursor cursor, long stableGeneration, long unstableGeneration )
{
return pointer( TreeNode.rightSibling( cursor, stableGeneration, unstableGeneration ) );
}
private long leftSibling( PageCursor cursor, long stableGeneration, long unstableGeneration )
{
return pointer( TreeNode.leftSibling( cursor, stableGeneration, unstableGeneration ) );
}
private long successor( PageCursor cursor, long stableGeneration, long unstableGeneration )
{
return pointer( TreeNode.successor( cursor, stableGeneration, unstableGeneration ) );
}
}