-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
TreeNodeFixedSize.java
452 lines (394 loc) · 15.6 KB
/
TreeNodeFixedSize.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
/*
* 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.neo4j.io.pagecache.PageCursor;
import static org.neo4j.index.internal.gbptree.GenerationSafePointerPair.read;
import static org.neo4j.index.internal.gbptree.Layout.FIXED_SIZE_KEY;
import static org.neo4j.index.internal.gbptree.Layout.FIXED_SIZE_VALUE;
import static org.neo4j.index.internal.gbptree.TreeNode.Type.LEAF;
class TreeNodeFixedSize<KEY,VALUE> extends TreeNode<KEY,VALUE>
{
private final int internalMaxKeyCount;
private final int leafMaxKeyCount;
private final int keySize;
private final int valueSize;
TreeNodeFixedSize( int pageSize, Layout<KEY,VALUE> layout )
{
super( pageSize, layout );
this.keySize = layout.keySize( null );
this.valueSize = layout.valueSize( null );
this.internalMaxKeyCount = Math.floorDiv( pageSize - (BASE_HEADER_LENGTH + SIZE_PAGE_REFERENCE),
keySize + SIZE_PAGE_REFERENCE);
this.leafMaxKeyCount = Math.floorDiv( pageSize - BASE_HEADER_LENGTH, keySize + valueSize );
if ( internalMaxKeyCount < 2 )
{
throw new MetadataMismatchException(
"For layout %s a page size of %d would only fit %d internal keys, minimum is 2",
layout, pageSize, internalMaxKeyCount );
}
if ( leafMaxKeyCount < 2 )
{
throw new MetadataMismatchException( "A page size of %d would only fit leaf keys, minimum is 2",
pageSize, leafMaxKeyCount );
}
}
@Override
void writeAdditionalHeader( PageCursor cursor )
{ // no-op
}
private static int childSize()
{
return SIZE_PAGE_REFERENCE;
}
@Override
KEY keyAt( PageCursor cursor, KEY into, int pos, Type type )
{
cursor.setOffset( keyOffset( pos ) );
layout.readKey( cursor, into, FIXED_SIZE_KEY );
return into;
}
@Override
void insertKeyAndRightChildAt( PageCursor cursor, KEY key, long child, int pos, int keyCount, long stableGeneration,
long unstableGeneration )
{
insertKeyAt( cursor, key, pos, keyCount );
insertChildAt( cursor, child, pos + 1, keyCount, stableGeneration, unstableGeneration );
}
@Override
void insertKeyValueAt( PageCursor cursor, KEY key, VALUE value, int pos, int keyCount )
{
insertKeyAt( cursor, key, pos, keyCount );
insertValueAt( cursor, value, pos, keyCount );
}
@Override
void removeKeyValueAt( PageCursor cursor, int pos, int keyCount )
{
removeKeyAt( cursor, pos, keyCount );
removeValueAt( cursor, pos, keyCount );
}
@Override
void removeKeyAndLeftChildAt( PageCursor cursor, int keyPos, int keyCount )
{
removeKeyAt( cursor, keyPos, keyCount );
removeChildAt( cursor, keyPos, keyCount );
}
@Override
void removeKeyAndRightChildAt( PageCursor cursor, int keyPos, int keyCount )
{
removeKeyAt( cursor, keyPos, keyCount );
removeChildAt( cursor, keyPos + 1, keyCount );
}
@Override
void setKeyAt( PageCursor cursor, KEY key, int pos, Type type )
{
cursor.setOffset( keyOffset( pos ) );
layout.writeKey( cursor, key );
}
@Override
VALUE valueAt( PageCursor cursor, VALUE value, int pos )
{
cursor.setOffset( valueOffset( pos ) );
layout.readValue( cursor, value, FIXED_SIZE_VALUE );
return value;
}
@Override
boolean setValueAt( PageCursor cursor, VALUE value, int pos )
{
cursor.setOffset( valueOffset( pos ) );
layout.writeValue( cursor, value );
return true;
}
@Override
long childAt( PageCursor cursor, int pos, long stableGeneration, long unstableGeneration )
{
cursor.setOffset( childOffset( pos ) );
return read( cursor, stableGeneration, unstableGeneration, pos );
}
@Override
void setChildAt( PageCursor cursor, long child, int pos, long stableGeneration, long unstableGeneration )
{
cursor.setOffset( childOffset( pos ) );
writeChild( cursor, child, stableGeneration, unstableGeneration );
}
@Override
int leafMaxKeyCount()
{
return leafMaxKeyCount;
}
@Override
boolean reasonableKeyCount( int keyCount )
{
return keyCount >= 0 && keyCount <= Math.max( internalMaxKeyCount(), leafMaxKeyCount() );
}
@Override
boolean reasonableChildCount( int childCount )
{
return childCount >= 0 && childCount <= internalMaxKeyCount();
}
@Override
int childOffset( int pos )
{
return BASE_HEADER_LENGTH + internalMaxKeyCount * keySize + pos * SIZE_PAGE_REFERENCE;
}
private int internalMaxKeyCount()
{
return internalMaxKeyCount;
}
private void insertKeyAt( PageCursor cursor, KEY key, int pos, int keyCount )
{
insertKeySlotsAt( cursor, pos, 1, keyCount );
cursor.setOffset( keyOffset( pos ) );
layout.writeKey( cursor, key );
}
private void removeKeyAt( PageCursor cursor, int pos, int keyCount )
{
removeSlotAt( cursor, pos, keyCount, keyOffset( 0 ), keySize );
}
private void insertChildAt( PageCursor cursor, long child, int pos, int keyCount,
long stableGeneration, long unstableGeneration )
{
insertChildSlot( cursor, pos, keyCount );
setChildAt( cursor, child, pos, stableGeneration, unstableGeneration );
}
private void removeChildAt( PageCursor cursor, int pos, int keyCount )
{
removeSlotAt( cursor, pos, keyCount + 1, childOffset( 0 ), childSize() );
}
private void insertKeyValueSlots( PageCursor cursor, int numberOfSlots, int keyCount )
{
insertKeySlotsAt( cursor, 0, numberOfSlots, keyCount );
insertValueSlotsAt( cursor, 0, numberOfSlots, keyCount );
}
// Always insert together with key. Use insertKeyValueAt
private void insertValueAt( PageCursor cursor, VALUE value, int pos, int keyCount )
{
insertValueSlotsAt( cursor, pos, 1, keyCount );
setValueAt( cursor, value, pos );
}
// Always insert together with key. Use removeKeyValueAt
private void removeValueAt( PageCursor cursor, int pos, int keyCount )
{
removeSlotAt( cursor, pos, keyCount, valueOffset( 0 ), valueSize );
}
private void insertKeySlotsAt( PageCursor cursor, int pos, int numberOfSlots, int keyCount )
{
insertSlotsAt( cursor, pos, numberOfSlots, keyCount, keyOffset( 0 ), keySize );
}
private void insertValueSlotsAt( PageCursor cursor, int pos, int numberOfSlots, int keyCount )
{
insertSlotsAt( cursor, pos, numberOfSlots, keyCount, valueOffset( 0 ), valueSize );
}
private void insertChildSlot( PageCursor cursor, int pos, int keyCount )
{
insertSlotsAt( cursor, pos, 1, keyCount + 1, childOffset( 0 ), childSize() );
}
private int keyOffset( int pos )
{
return BASE_HEADER_LENGTH + pos * keySize;
}
private int valueOffset( int pos )
{
return BASE_HEADER_LENGTH + leafMaxKeyCount * keySize + pos * valueSize;
}
private int keySize()
{
return keySize;
}
private int valueSize()
{
return valueSize;
}
/* SPLIT, MERGE and REBALANCE*/
@Override
boolean internalOverflow( int currentKeyCount )
{
return currentKeyCount + 1 > internalMaxKeyCount();
}
@Override
Overflow leafOverflow( PageCursor cursor, int currentKeyCount, KEY newKey, VALUE newValue )
{
return currentKeyCount + 1 > leafMaxKeyCount() ? Overflow.YES : Overflow.NO;
}
@Override
void defragmentLeaf( PageCursor cursor )
{ // no-op
}
@Override
boolean leafUnderflow( PageCursor cursor, int keyCount )
{
return keyCount < (leafMaxKeyCount() + 1) / 2;
}
@Override
boolean canRebalanceLeaves( int leftKeyCount, int rightKeyCount )
{
return leftKeyCount + rightKeyCount >= leafMaxKeyCount();
}
@Override
boolean canMergeLeaves( int leftKeyCount, int rightKeyCount )
{
return leftKeyCount + rightKeyCount <= leafMaxKeyCount();
}
@Override
void doSplitLeaf( PageCursor leftCursor, int leftKeyCount, PageCursor rightCursor, int insertPos, KEY newKey,
VALUE newValue, StructurePropagation<KEY> structurePropagation )
{
int keyCountAfterInsert = leftKeyCount + 1;
int middlePos = middle( keyCountAfterInsert );
if ( middlePos == insertPos )
{
layout.copyKey( newKey, structurePropagation.rightKey );
}
else
{
keyAt( leftCursor, structurePropagation.rightKey, insertPos < middlePos ? middlePos - 1 : middlePos, LEAF );
}
int rightKeyCount = keyCountAfterInsert - middlePos;
if ( insertPos < middlePos )
{
// v-------v copy
// before _,_,_,_,_,_,_,_,_,_
// insert _,_,_,X,_,_,_,_,_,_,_
// middle ^
copyKeysAndValues( leftCursor, middlePos - 1, rightCursor, 0, rightKeyCount );
insertKeyValueAt( leftCursor, newKey, newValue, insertPos, middlePos - 1 );
}
else
{
// v---v first copy
// v-v second copy
// before _,_,_,_,_,_,_,_,_,_
// insert _,_,_,_,_,_,_,_,X,_,_
// middle ^
int countBeforePos = insertPos - middlePos;
if ( countBeforePos > 0 )
{
// first copy
copyKeysAndValues( leftCursor, middlePos, rightCursor, 0, countBeforePos );
}
insertKeyValueAt( rightCursor, newKey, newValue, countBeforePos, countBeforePos );
int countAfterPos = leftKeyCount - insertPos;
if ( countAfterPos > 0 )
{
// second copy
copyKeysAndValues( leftCursor, insertPos, rightCursor, countBeforePos + 1, countAfterPos );
}
}
TreeNode.setKeyCount( leftCursor, middlePos );
TreeNode.setKeyCount( rightCursor, rightKeyCount );
}
private static int middle( int keyCountAfterInsert )
{
return keyCountAfterInsert / 2;
}
@Override
void doSplitInternal( PageCursor leftCursor, int leftKeyCount, PageCursor rightCursor, int rightKeyCount, int insertPos, KEY newKey,
long newRightChild, int middlePos, long stableGeneration, long unstableGeneration )
{
if ( insertPos < middlePos )
{
// v-------v copy
// before key _,_,_,_,_,_,_,_,_,_
// before child -,-,-,-,-,-,-,-,-,-,-
// insert key _,_,X,_,_,_,_,_,_,_,_
// insert child -,-,-,x,-,-,-,-,-,-,-,-
// middle key ^
leftCursor.copyTo( keyOffset( middlePos ), rightCursor, keyOffset( 0 ), rightKeyCount * keySize() );
leftCursor.copyTo( childOffset( middlePos ), rightCursor, childOffset( 0 ), (rightKeyCount + 1) * childSize() );
insertKeyAt( leftCursor, newKey, insertPos, middlePos - 1 );
insertChildAt( leftCursor, newRightChild, insertPos + 1, middlePos - 1, stableGeneration, unstableGeneration );
}
else
{
// pos > middlePos
// v-v first copy
// v-v-v second copy
// before key _,_,_,_,_,_,_,_,_,_
// before child -,-,-,-,-,-,-,-,-,-,-
// insert key _,_,_,_,_,_,_,X,_,_,_
// insert child -,-,-,-,-,-,-,-,x,-,-,-
// middle key ^
// pos == middlePos
// first copy
// v-v-v-v-v second copy
// before key _,_,_,_,_,_,_,_,_,_
// before child -,-,-,-,-,-,-,-,-,-,-
// insert key _,_,_,_,_,X,_,_,_,_,_
// insert child -,-,-,-,-,-,x,-,-,-,-,-
// middle key ^
// Keys
int countBeforePos = insertPos - (middlePos + 1);
// ... first copy
if ( countBeforePos > 0 )
{
leftCursor.copyTo( keyOffset( middlePos + 1 ), rightCursor, keyOffset( 0 ), countBeforePos * keySize() );
}
// ... insert
if ( countBeforePos >= 0 )
{
insertKeyAt( rightCursor, newKey, countBeforePos, countBeforePos );
}
// ... second copy
int countAfterPos = leftKeyCount - insertPos;
if ( countAfterPos > 0 )
{
leftCursor.copyTo( keyOffset( insertPos ), rightCursor, keyOffset( countBeforePos + 1 ), countAfterPos * keySize() );
}
// Children
countBeforePos = insertPos - middlePos;
// ... first copy
if ( countBeforePos > 0 )
{
// first copy
leftCursor.copyTo( childOffset( middlePos + 1 ), rightCursor, childOffset( 0 ), countBeforePos * childSize() );
}
// ... insert
insertChildAt( rightCursor, newRightChild, countBeforePos, countBeforePos, stableGeneration, unstableGeneration );
// ... second copy
if ( countAfterPos > 0 )
{
leftCursor.copyTo( childOffset( insertPos + 1 ), rightCursor, childOffset( countBeforePos + 1 ),
countAfterPos * childSize() );
}
}
TreeNode.setKeyCount( leftCursor, middlePos );
TreeNode.setKeyCount( rightCursor, rightKeyCount );
}
@Override
void moveKeyValuesFromLeftToRight( PageCursor leftCursor, int leftKeyCount, PageCursor rightCursor, int rightKeyCount,
int fromPosInLeftNode )
{
int numberOfKeysToMove = leftKeyCount - fromPosInLeftNode;
// Push keys and values in right sibling to the right
insertKeyValueSlots( rightCursor, numberOfKeysToMove, rightKeyCount );
// Move keys and values from left sibling to right sibling
copyKeysAndValues( leftCursor, fromPosInLeftNode, rightCursor, 0, numberOfKeysToMove );
}
private void copyKeysAndValues( PageCursor fromCursor, int fromPos, PageCursor toCursor, int toPos, int count )
{
fromCursor.copyTo( keyOffset( fromPos ), toCursor, keyOffset( toPos ), count * keySize() );
fromCursor.copyTo( valueOffset( fromPos ), toCursor, valueOffset( toPos ),count * valueSize() );
}
@Override
public String toString()
{
return "TreeNodeFixedSize[pageSize:" + pageSize + ", internalMax:" + internalMaxKeyCount() + ", leafMax:" + leafMaxKeyCount() + ", " +
"keySize:" + keySize() + ", valueSize:" + valueSize + "]";
}
}