-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
InternalTreeLogicTest.java
1728 lines (1497 loc) · 58.9 KB
/
InternalTreeLogicTest.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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2002-2017 "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 org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.neo4j.io.pagecache.PageCursor;
import org.neo4j.test.rule.RandomRule;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import static org.neo4j.index.internal.gbptree.ConsistencyChecker.assertNoCrashOrBrokenPointerInGSPP;
import static org.neo4j.index.internal.gbptree.GenerationSafePointerPair.pointer;
import static org.neo4j.index.internal.gbptree.ValueMergers.overwrite;
@RunWith( Parameterized.class )
public class InternalTreeLogicTest
{
private static final ValueMerger<MutableLong,MutableLong> ADDER = ( existingKey, newKey, base, add ) ->
{
base.add( add.longValue() );
return base;
};
private final int pageSize = 256;
private final SimpleIdProvider id = new SimpleIdProvider();
private final Layout<MutableLong,MutableLong> layout = new SimpleLongLayout();
private final TreeNode<MutableLong,MutableLong> node = new TreeNode<>( pageSize, layout );
private final InternalTreeLogic<MutableLong,MutableLong> treeLogic = new InternalTreeLogic<>( id, node, layout );
private final PageAwareByteArrayCursor cursor = new PageAwareByteArrayCursor( pageSize );
private final PageAwareByteArrayCursor readCursor = cursor.duplicate();
private final int maxKeyCount = node.leafMaxKeyCount();
private final MutableLong insertKey = new MutableLong();
private final MutableLong insertValue = new MutableLong();
private final MutableLong readKey = new MutableLong();
private final MutableLong readValue = new MutableLong();
private final StructurePropagation<MutableLong> structurePropagation = new StructurePropagation<>(
layout.newKey(), layout.newKey(), layout.newKey() );
private static long stableGeneration = GenerationSafePointer.MIN_GENERATION;
private static long unstableGeneration = stableGeneration + 1;
@Parameterized.Parameters( name = "{0}" )
public static Collection<Object[]> generators()
{
List<Object[]> parameters = new ArrayList<>();
// Initial state has same generation as update state
parameters.add( new Object[]{
"NoCheckpoint", GenerationManager.NO_OP_GENERATION, false} );
// Update state in next generation
parameters.add( new Object[]{
"Checkpoint", GenerationManager.DEFAULT, true} );
return parameters;
}
@Parameterized.Parameter( 0 )
public String name;
@Parameterized.Parameter( 1 )
public GenerationManager generationManager;
@Parameterized.Parameter( 2 )
public boolean isCheckpointing;
@Rule
public RandomRule random = new RandomRule();
private long rootId;
private long rootGeneration;
private int numberOfRootSplits;
private int numberOfRootSuccessors;
@Before
public void setUp() throws IOException
{
id.reset();
long newId = id.acquireNewId( stableGeneration, unstableGeneration );
goTo( cursor, newId );
readCursor.next( newId );
}
@Test
public void modifierMustInsertAtFirstPositionInEmptyLeaf() throws Exception
{
// given
initialize();
long key = 1L;
long value = 1L;
goTo( readCursor, rootId );
assertThat( keyCount(), is( 0 ) );
// when
generationManager.checkpoint();
insert( key, value );
// then
goTo( readCursor, rootId );
assertThat( keyCount(), is( 1 ) );
assertThat( keyAt( 0 ), is( key ) );
assertThat( valueAt( 0 ), is( key ) );
}
@Test
public void modifierMustSortCorrectlyOnInsertFirstInLeaf() throws Exception
{
// given
initialize();
generationManager.checkpoint();
for ( int i = 0; i < maxKeyCount; i++ )
{
// when
long key = maxKeyCount - i;
insert( key, key );
// then
readCursor.next( rootId );
assertThat( keyAt( 0 ), is( key ) );
assertThat( valueAt( 0 ), is( key ) );
}
}
@Test
public void modifierMustSortCorrectlyOnInsertLastInLeaf() throws Exception
{
// given
initialize();
generationManager.checkpoint();
for ( int i = 0; i < maxKeyCount; i++ )
{
// when
insert( i, i );
// then
readCursor.next( rootId );
assertThat( keyAt( i ), is( (long) i ) );
assertThat( valueAt( i ), is( (long) i ) );
}
}
@Test
public void modifierMustSortCorrectlyOnInsertInMiddleOfLeaf() throws Exception
{
// given
initialize();
generationManager.checkpoint();
for ( int i = 0; i < maxKeyCount; i++ )
{
// when
long key = i % 2 == 0 ? i / 2 : maxKeyCount - i / 2;
insert( key, key );
// then
readCursor.next( rootId );
assertThat( keyAt( (i + 1) / 2 ), is( key ) );
}
}
@Test
public void modifierMustSplitWhenInsertingMiddleOfFullLeaf() throws Exception
{
// given
initialize();
for ( int i = 0; i < maxKeyCount; i++ )
{
long key = i % 2 == 0 ? i : maxKeyCount * 2 - i;
insert( key, key );
}
// when
generationManager.checkpoint();
long middle = maxKeyCount;
insert( middle, middle );
// then
assertEquals( 1, numberOfRootSplits );
}
@Test
public void modifierMustSplitWhenInsertingLastInFullLeaf() throws Exception
{
// given
initialize();
long key = 0;
while ( key < maxKeyCount )
{
insert( key, key );
assertFalse( structurePropagation.hasRightKeyInsert );
key++;
}
// when
generationManager.checkpoint();
insert( key, key );
// then
assertEquals( 1, numberOfRootSplits ); // Should cause a split
}
@Test
public void modifierMustSplitWhenInsertingFirstInFullLeaf() throws Exception
{
// given
initialize();
for ( int i = 0; i < maxKeyCount; i++ )
{
long key = i + 1;
insert( key, key );
assertFalse( structurePropagation.hasRightKeyInsert );
}
// when
generationManager.checkpoint();
insert( 0L, 0L );
// then
assertEquals( 1, numberOfRootSplits );
}
@Test
public void modifierMustUpdatePointersInSiblingsToSplit() throws Exception
{
// given
initialize();
long someLargeNumber = maxKeyCount * 1000;
long i = 0;
while ( i < maxKeyCount )
{
insert( someLargeNumber - i, i );
i++;
}
// First split
generationManager.checkpoint();
insert( someLargeNumber - i, i );
i++;
// Assert child pointers and sibling pointers are intact after split in root
goTo( readCursor, rootId );
long child0 = childAt( readCursor, 0, stableGeneration, unstableGeneration );
long child1 = childAt( readCursor, 1, stableGeneration, unstableGeneration );
assertSiblingOrderAndPointers( child0, child1 );
// Insert until we have another split in leftmost leaf
while ( keyCount( rootId ) == 1 )
{
insert( someLargeNumber - i, i );
i++;
}
// Just to be sure
assertTrue( TreeNode.isInternal( readCursor ) );
assertThat( TreeNode.keyCount( readCursor ), is( 2 ) );
// Assert child pointers and sibling pointers are intact
// AND that node not involved in split also has its left sibling pointer updated
child0 = childAt( readCursor, 0, stableGeneration, unstableGeneration );
child1 = childAt( readCursor, 1, stableGeneration, unstableGeneration );
long child2 = childAt( readCursor, 2, stableGeneration, unstableGeneration ); // <- right sibling to split-node before split
assertSiblingOrderAndPointers( child0, child1, child2 );
}
/* REMOVE */
@Test
public void modifierMustRemoveFirstInEmptyLeaf() throws Exception
{
// given
initialize();
long key = 1L;
long value = 1L;
insert( key, value );
// when
generationManager.checkpoint();
remove( key, readValue );
// then
goTo( readCursor, rootId );
assertThat( TreeNode.keyCount( cursor ), is( 0 ) );
}
@Test
public void modifierMustRemoveFirstInFullLeaf() throws Exception
{
// given
initialize();
for ( int i = 0; i < maxKeyCount; i++ )
{
insert( i, i );
}
// when
generationManager.checkpoint();
remove( 0, readValue );
// then
goTo( readCursor, rootId );
assertThat( TreeNode.keyCount( readCursor ), is( maxKeyCount - 1 ) );
for ( int i = 0; i < maxKeyCount - 1; i++ )
{
assertThat( keyAt( i ), is( i + 1L ) );
}
}
@Test
public void modifierMustRemoveInMiddleInFullLeaf() throws Exception
{
// given
initialize();
int middle = maxKeyCount / 2;
for ( int i = 0; i < maxKeyCount; i++ )
{
insert( i, i );
}
// when
generationManager.checkpoint();
remove( middle, readValue );
// then
goTo( readCursor, rootId );
assertThat( keyCount(), is( maxKeyCount - 1 ) );
assertThat( keyAt( middle ), is( middle + 1L ) );
for ( int i = 0; i < maxKeyCount - 1; i++ )
{
long expected = i < middle ? i : i + 1L;
assertThat( keyAt( i ), is( expected ) );
}
}
@Test
public void modifierMustRemoveLastInFullLeaf() throws Exception
{
initialize();
for ( int i = 0; i < maxKeyCount; i++ )
{
insert( i, i );
}
// when
generationManager.checkpoint();
remove( maxKeyCount - 1, readValue );
// then
goTo( readCursor, rootId );
assertThat( keyCount(), is( maxKeyCount - 1 ) );
for ( int i = 0; i < maxKeyCount - 1; i++ )
{
Long actual = keyAt( i );
assertThat( actual, is( (long) i ) );
}
}
@Test
public void modifierMustRemoveFromLeftChild() throws Exception
{
initialize();
for ( int i = 0; numberOfRootSplits == 0; i++ )
{
insert( i, i );
}
// when
generationManager.checkpoint();
goTo( readCursor, structurePropagation.midChild );
assertThat( keyAt( 0 ), is( 0L ) );
remove( 0, readValue );
// then
goTo( readCursor, structurePropagation.midChild );
assertThat( keyAt( 0 ), is( 1L ) );
}
@Test
public void modifierMustRemoveFromRightChildButNotFromInternalWithHitOnInternalSearch() throws Exception
{
initialize();
for ( int i = 0; numberOfRootSplits == 0; i++ )
{
insert( i, i );
}
// when key to remove exists in internal
Long keyToRemove = structurePropagation.rightKey.getValue();
goTo( readCursor, rootId );
assertThat( keyAt( 0 ), is( keyToRemove ) );
// and as first key in right child
long rightChild = structurePropagation.rightChild;
goTo( readCursor, rightChild );
int keyCountInRightChild = keyCount();
assertThat( keyAt( 0 ), is( keyToRemove ) );
// and we remove it
generationManager.checkpoint();
remove( keyToRemove, readValue );
// then we should still find it in internal
goTo( readCursor, rootId );
assertThat( keyCount(), is( 1 ) );
assertThat( keyAt( 0 ), is( keyToRemove ) );
// but not in right leaf
rightChild = childAt( readCursor, 1, stableGeneration, unstableGeneration );
goTo( readCursor, rightChild );
assertThat( keyCount(), is( keyCountInRightChild - 1 ) );
assertThat( keyAt( 0 ), is( keyToRemove + 1 ) );
}
@Test
public void modifierMustNotRemoveWhenKeyDoesNotExist() throws Exception
{
// given
initialize();
for ( int i = 0; i < maxKeyCount; i++ )
{
insert( i, i );
}
// when
generationManager.checkpoint();
assertNull( remove( maxKeyCount, readValue ) );
// then
goTo( readCursor, rootId );
assertThat( keyCount(), is( maxKeyCount ) );
for ( int i = 0; i < maxKeyCount; i++ )
{
Long actual = keyAt( i );
assertThat( actual, is( (long) i ) );
}
}
@Test
public void modifierMustNotRemoveWhenKeyOnlyExistInInternal() throws Exception
{
// given
initialize();
for ( int i = 0; numberOfRootSplits == 0; i++ )
{
insert( i, i );
}
// when key to remove exists in internal
Long keyToRemove = structurePropagation.rightKey.getValue();
assertThat( keyAt( rootId, 0 ), is( keyToRemove ) );
// and as first key in right child
long currentRightChild = structurePropagation.rightChild;
goTo( readCursor, currentRightChild );
int keyCountInRightChild = keyCount();
assertThat( keyAt( 0 ), is( keyToRemove ) );
// and we remove it
generationManager.checkpoint();
remove( keyToRemove, readValue ); // Possibly create successor of right child
goTo( readCursor, rootId );
currentRightChild = childAt( readCursor, 1, stableGeneration, unstableGeneration );
// then we should still find it in internal
assertThat( keyCount(), is( 1 ) );
assertThat( keyAt( 0 ), is( keyToRemove ) );
// but not in right leaf
goTo( readCursor, currentRightChild );
assertThat( keyCount(), is( keyCountInRightChild - 1 ) );
assertThat( keyAt( 0 ), is( keyToRemove + 1 ) );
// and when we remove same key again, nothing should change
assertNull( remove( keyToRemove, readValue ) );
}
/* REBALANCE */
@Test
public void mustNotRebalanceFromRightToLeft() throws Exception
{
// given
initialize();
long key = 0;
while ( numberOfRootSplits == 0 )
{
insert( key, key );
key++;
}
// ... enough keys in right child to share with left child if rebalance is needed
insert( key, key );
key++;
// ... and the prim key diving key range for left child and right child
goTo( readCursor, rootId );
long primKey = keyAt( 0 );
// when
// ... removing all keys from left child
for ( long i = 0; i < primKey; i++ )
{
remove( i, readValue );
}
// then
// ... looking a right child
long rightChild = childAt( readCursor, 1, stableGeneration, unstableGeneration );
goTo( readCursor, rightChild );
// ... no keys should have moved from right sibling
int pos = 0;
long expected = primKey;
while ( expected < key )
{
assertThat( keyAt( pos ), is( expected ) );
pos++;
expected++;
}
}
@Test
public void mustPropagateAllStructureChanges() throws Exception
{
assumeTrue( "No checkpointing, no successor", isCheckpointing );
//given
initialize();
long key = 10;
while ( numberOfRootSplits == 0 )
{
insert( key, key );
key++;
}
// ... enough keys in left child to share with right child if rebalance is needed
for ( long smallKey = 0; smallKey < 2; smallKey++ )
{
insert( smallKey, smallKey );
}
// ... and the prim key dividing key range for left and right child
goTo( readCursor, rootId );
long oldPrimKey = keyAt( 0 );
// ... and left and right child
long originalLeftChild = childAt( readCursor, 0, stableGeneration, unstableGeneration );
long originalRightChild = childAt( readCursor, 1, stableGeneration, unstableGeneration );
goTo( readCursor, originalRightChild );
List<Long> keysInRightChild = allKeys( readCursor );
// when
// ... after checkpoint
generationManager.checkpoint();
// ... removing keys from right child until rebalance is triggered
int index = 0;
long rightChild;
long leftmostInRightChild;
do
{
remove( keysInRightChild.get( index ), readValue );
index++;
goTo( readCursor, rootId );
rightChild = childAt( readCursor, 1, stableGeneration, unstableGeneration );
goTo( readCursor, rightChild );
leftmostInRightChild = keyAt( 0 );
} while ( leftmostInRightChild >= keysInRightChild.get( 0 ) );
// then
// ... primKey in root is updated
goTo( readCursor, rootId );
Long primKey = keyAt( 0 );
assertThat( primKey, is( leftmostInRightChild ) );
assertThat( primKey, is( not( oldPrimKey ) ) );
// ... new versions of left and right child
long newLeftChild = childAt( readCursor, 0, stableGeneration, unstableGeneration );
long newRightChild = childAt( readCursor, 1, stableGeneration, unstableGeneration );
assertThat( newLeftChild, is( not( originalLeftChild ) ) );
assertThat( newRightChild, is( not( originalRightChild ) ) );
}
/* MERGE */
@Test
public void mustPropagateStructureOnMergeFromLeft() throws Exception
{
assumeTrue( "No checkpointing, no successor", isCheckpointing );
// GIVEN:
// ------root-------
// / | \
// v v v
// left <--> middle <--> right
List<Long> allKeys = new ArrayList<>();
initialize();
long targetLastId = id.lastId() + 3; // 2 splits and 1 new allocated root
long i = 0;
for ( ; id.lastId() < targetLastId; i++ )
{
insert( i, i );
allKeys.add( i );
}
goTo( readCursor, rootId );
assertEquals( 2, keyCount() );
long oldLeftChild = childAt( readCursor, 0, stableGeneration, unstableGeneration );
long oldMiddleChild = childAt( readCursor, 1, stableGeneration, unstableGeneration );
long oldRightChild = childAt( readCursor, 2, stableGeneration, unstableGeneration );
assertSiblings( oldLeftChild, oldMiddleChild, oldRightChild );
// WHEN
generationManager.checkpoint();
long middleKey = keyAt( 0 ) + 1; // Should be located in middle leaf
remove( middleKey, insertValue );
allKeys.remove( middleKey );
// THEN
// old root should still have 2 keys
assertEquals( 2, keyCount() );
// new root should have only 1 key
goTo( readCursor, rootId );
assertEquals( 1, keyCount() );
// left child should be a new node
long newLeftChild = childAt( readCursor, 0, stableGeneration, unstableGeneration );
assertNotEquals( newLeftChild, oldLeftChild );
assertNotEquals( newLeftChild, oldMiddleChild );
// right child should be same old node
long newRightChild = childAt( readCursor, 1, stableGeneration, unstableGeneration );
assertEquals( newRightChild, oldRightChild );
// old left and old middle has new left as successor
goTo( readCursor, oldLeftChild );
assertEquals( newLeftChild, successor( readCursor, stableGeneration, unstableGeneration ) );
goTo( readCursor, oldMiddleChild );
assertEquals( newLeftChild, successor( readCursor, stableGeneration, unstableGeneration ) );
// new left child contain keys from old left and old middle
goTo( readCursor, oldRightChild );
Long firstKeyOfOldRightChild = keyAt( 0 );
List<Long> expectedKeysInNewLeftChild = allKeys.subList( 0, allKeys.indexOf( firstKeyOfOldRightChild ) );
goTo( readCursor, newLeftChild );
assertNodeContainsExpectedKeys( expectedKeysInNewLeftChild );
// new children are siblings
assertSiblings( newLeftChild, oldRightChild, TreeNode.NO_NODE_FLAG );
}
@Test
public void mustPropagateStructureOnMergeToRight() throws Exception
{
assumeTrue( "No checkpointing, no successor", isCheckpointing );
// GIVEN:
// ---------root---------
// / | \
// v v v
// oldleft <-> oldmiddle <-> oldright
List<Long> allKeys = new ArrayList<>();
initialize();
long targetLastId = id.lastId() + 3; // 2 splits and 1 new allocated root
long i = 0;
for ( ; id.lastId() < targetLastId; i++ )
{
insert( i, i );
allKeys.add( i );
}
goTo( readCursor, rootId );
assertEquals( 2, keyCount() );
long oldLeftChild = childAt( readCursor, 0, stableGeneration, unstableGeneration );
long oldMiddleChild = childAt( readCursor, 1, stableGeneration, unstableGeneration );
long oldRightChild = childAt( readCursor, 2, stableGeneration, unstableGeneration );
assertSiblings( oldLeftChild, oldMiddleChild, oldRightChild );
// WHEN
generationManager.checkpoint();
long keyInLeftChild = keyAt( 0 ) - 1; // Should be located in left leaf
remove( keyInLeftChild, insertValue );
allKeys.remove( keyInLeftChild );
// New structure
// NOTE: oldleft gets a successor (intermediate) before removing key and then another one once it is merged,
// effectively creating a chain of successor pointers to our newleft that in the end contain keys from
// oldleft and oldmiddle
// ----root----
// / |
// v v
// oldleft -[successor]-> intermediate -[successor]-> newleft <-> oldright
// ^
// \-[successor]- oldmiddle
// THEN
// old root should still have 2 keys
assertEquals( 2, keyCount() );
// new root should have only 1 key
goTo( readCursor, rootId );
assertEquals( 1, keyCount() );
// left child should be a new node
long newLeftChild = childAt( readCursor, 0, stableGeneration, unstableGeneration );
assertNotEquals( newLeftChild, oldLeftChild );
assertNotEquals( newLeftChild, oldMiddleChild );
// right child should be same old node
long newRightChild = childAt( readCursor, 1, stableGeneration, unstableGeneration );
assertEquals( newRightChild, oldRightChild );
// old left and old middle has new left as successor
goTo( readCursor, oldLeftChild );
assertEquals( newLeftChild, newestGeneration( readCursor, stableGeneration, unstableGeneration ) );
goTo( readCursor, oldMiddleChild );
assertEquals( newLeftChild, successor( readCursor, stableGeneration, unstableGeneration ) );
// new left child contain keys from old left and old middle
goTo( readCursor, oldRightChild );
Long firstKeyInOldRightChild = keyAt( 0 );
List<Long> expectedKeysInNewLeftChild = allKeys.subList( 0, allKeys.indexOf( firstKeyInOldRightChild ) );
goTo( readCursor, newLeftChild );
assertNodeContainsExpectedKeys( expectedKeysInNewLeftChild );
// new children are siblings
assertSiblings( newLeftChild, oldRightChild, TreeNode.NO_NODE_FLAG );
}
@Test
public void mustPropagateStructureWhenMergingBetweenDifferentSubtrees() throws Exception
{
// GIVEN
// We will merge oldLeft into oldRight
// -----root----
// / \
// v v
// _____leftParent <-> rightParent_____
// / / / \ / \ \ \
// v v v v v v v v
// [some more children] oldLeft <-> oldRight [some more children]
initialize();
long i = 0;
while ( numberOfRootSplits < 2 )
{
insert( i, i );
i++;
}
long oldLeft = rightmostLeafInSubtree( rootId, 0 );
long oldRight = leftmostLeafInSubtree( rootId, 1 );
long oldSplitter = keyAt( 0 );
long rightmostKeyInLeftSubtree = rightmostInternalKeyInSubtree( rootId, 0 );
ArrayList<Long> allKeysInOldLeftAndOldRight = new ArrayList<>();
goTo( readCursor, oldLeft );
allKeys( readCursor, allKeysInOldLeftAndOldRight );
goTo( readCursor, oldRight );
allKeys( readCursor, allKeysInOldLeftAndOldRight );
long keyInOldRight = keyAt( 0 );
// WHEN
generationManager.checkpoint();
remove( keyInOldRight, readValue );
allKeysInOldLeftAndOldRight.remove( keyInOldRight );
// THEN
// oldSplitter in root should have been replaced by rightmostKeyInLeftSubtree
goTo( readCursor, rootId );
Long newSplitter = keyAt( 0 );
assertThat( newSplitter, is( not( oldSplitter ) ) );
assertThat( newSplitter, is( rightmostKeyInLeftSubtree ) );
// rightmostKeyInLeftSubtree should have been removed from successor version of leftParent
long newRightmostInternalKeyInLeftSubtree = rightmostInternalKeyInSubtree( rootId, 0 );
assertThat( newRightmostInternalKeyInLeftSubtree, is( not( rightmostKeyInLeftSubtree ) ) );
// newRight contain all
goToSuccessor( readCursor, oldRight );
List<Long> allKeysInNewRight = allKeys( readCursor );
assertEquals( allKeysInOldLeftAndOldRight, allKeysInNewRight );
}
@Test
public void mustLeaveSingleLeafAsRootWhenEverythingIsRemoved() throws Exception
{
// GIVEN
// a tree with some keys
List<Long> allKeys = new ArrayList<>();
initialize();
long i = 0;
while ( numberOfRootSplits < 3 )
{
insert( i, i );
allKeys.add( i );
i++;
}
// WHEN
// removing all keys but one
generationManager.checkpoint();
for ( int j = 0; j < allKeys.size() - 1; j++ )
{
remove( allKeys.get( j ), readValue );
}
// THEN
goTo( readCursor, rootId );
assertTrue( TreeNode.isLeaf( readCursor ) );
}
/* OVERALL CONSISTENCY */
@Test
public void modifierMustProduceConsistentTreeWithRandomInserts() throws Exception
{
// given
initialize();
int numberOfEntries = 100_000;
for ( int i = 0; i < numberOfEntries; i++ )
{
// when
insert( random.nextLong(), random.nextLong() );
if ( i == numberOfEntries / 2 )
{
generationManager.checkpoint();
}
}
// then
goTo( readCursor, rootId );
ConsistencyChecker<MutableLong> consistencyChecker =
new ConsistencyChecker<>( node, layout, stableGeneration, unstableGeneration );
consistencyChecker.check( readCursor, rootGeneration );
}
/* TEST VALUE MERGER */
@Test
public void modifierMustOverwriteWithOverwriteMerger() throws Exception
{
// given
initialize();
long key = random.nextLong();
long firstValue = random.nextLong();
insert( key, firstValue );
// when
generationManager.checkpoint();
long secondValue = random.nextLong();
insert( key, secondValue, ValueMergers.overwrite() );
// then
goTo( readCursor, rootId );
assertThat( keyCount(), is( 1 ) );
assertThat( valueAt( 0 ), is( secondValue ) );
}
@Test
public void modifierMustKeepExistingWithKeepExistingMerger() throws Exception
{
// given
initialize();
long key = random.nextLong();
long firstValue = random.nextLong();
insert( key, firstValue, ValueMergers.keepExisting() );
goTo( readCursor, rootId );
assertThat( keyCount(), is( 1 ) );
Long actual = valueAt( 0 );
assertThat( actual, is( firstValue ) );
// when
generationManager.checkpoint();
long secondValue = random.nextLong();
insert( key, secondValue, ValueMergers.keepExisting() );
// then
goTo( readCursor, rootId );
assertThat( keyCount(), is( 1 ) );
actual = valueAt( 0 );
assertThat( actual, is( firstValue ) );
}
@Test
public void shouldMergeValueInRootLeaf() throws Exception
{
// GIVEN
initialize();
long key = 10;
long baseValue = 100;
insert( key, baseValue );
// WHEN
generationManager.checkpoint();
int toAdd = 5;
insert( key, toAdd, ADDER );
// THEN
goTo( readCursor, rootId );
int searchResult = KeySearch.search( readCursor, node, key( key ), new MutableLong(), keyCount() );
assertTrue( KeySearch.isHit( searchResult ) );
int pos = KeySearch.positionOf( searchResult );
assertEquals( 0, pos );
assertEquals( key, keyAt( pos ).longValue() );
assertEquals( baseValue + toAdd, valueAt( pos ).longValue() );
}
@Test
public void shouldMergeValueInLeafLeftOfParentKey() throws Exception
{
// GIVEN
initialize();
for ( int i = 0; numberOfRootSplits == 0; i++ )
{
insert( i, i );
}
// WHEN
generationManager.checkpoint();
long key = 1;
int toAdd = 5;
insert( key, toAdd, ADDER );
// THEN
goTo( readCursor, structurePropagation.midChild );
int searchResult = KeySearch.search( readCursor, node, key( key ), new MutableLong(), keyCount() );
assertTrue( KeySearch.isHit( searchResult ) );
int pos = KeySearch.positionOf( searchResult );
assertEquals( 1, pos );
assertEquals( key, keyAt( pos ).longValue() );
assertEquals( key + toAdd, valueAt( pos ).longValue() );
}
@Test
public void shouldMergeValueInLeafAtParentKey() throws Exception
{
// GIVEN
initialize();
for ( int i = 0; numberOfRootSplits == 0; i++ )
{
insert( i, i );
}
// WHEN
generationManager.checkpoint();
long key = structurePropagation.rightKey.longValue();
int toAdd = 5;
insert( key, toAdd, ADDER );
// THEN
goTo( readCursor, rootId );
long rightChild = childAt( readCursor, 1, stableGeneration, unstableGeneration );
goTo( readCursor, rightChild );
int searchResult = KeySearch.search( readCursor, node, key( key ), new MutableLong(), keyCount() );
assertTrue( KeySearch.isHit( searchResult ) );
int pos = KeySearch.positionOf( searchResult );
assertEquals( 0, pos );
assertEquals( key, keyAt( pos ).longValue() );
assertEquals( key + toAdd, valueAt( pos ).longValue() );
}
@Test
public void shouldMergeValueInLeafBetweenTwoParentKeys() throws Exception
{
// GIVEN
initialize();
long firstSplitPrimKey = -1;
for ( int i = 0; numberOfRootSplits == 0 || keyCount( rootId ) < 1; i++ )
{