-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
TreeNode.java
461 lines (396 loc) · 17 KB
/
TreeNode.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
/*
* Copyright (c) 2002-2016 "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.gbptree;
import java.io.IOException;
import java.util.Comparator;
import java.util.function.Consumer;
import org.neo4j.io.pagecache.PageCursor;
/**
* Methods to manipulate single node such as set and get header fields,
* insert and fetch keys, values and children.
* <p>
* DESIGN
* <p>
* Using Separate design the internal nodes should look like
* <pre>
* # = empty space
*
* [ HEADER 81B ]|[ KEYS ]|[ CHILDREN ]
* [TYPE][GEN][KEYCOUNT][RIGHTSIBLING][LEFTSIBLING][NEWGEN]|[[KEY][KEY]...##]|[[CHILD][CHILD][CHILD]...##]
* 0 1 5 9 33 57
* </pre>
* Calc offset for key i (starting from 0)
* HEADER_LENGTH + i * SIZE_KEY
* <p>
* Calc offset for child i
* HEADER_LENGTH + SIZE_KEY * MAX_KEY_COUNT_INTERNAL + i * SIZE_CHILD
* <p>
* Using Separate design the leaf nodes should look like
*
* <pre>
* [ HEADER 81B ]|[ KEYS ]|[ VALUES ]
* [TYPE][GEN][KEYCOUNT][RIGHTSIBLING][LEFTSIBLING][NEWGEN]|[[KEY][KEY]...##]|[[VALUE][VALUE]...##]
* 0 1 5 9 33 57
* </pre>
*
* Calc offset for key i (starting from 0)
* HEADER_LENGTH + i * SIZE_KEY
* <p>
* Calc offset for value i
* HEADER_LENGTH + SIZE_KEY * MAX_KEY_COUNT_LEAF + i * SIZE_VALUE
*
* @param <KEY> type of key
* @param <VALUE> type of value
*/
class TreeNode<KEY,VALUE>
{
private static final int SIZE_PAGE_REFERENCE = GenSafePointerPair.SIZE;
private static final int BYTE_POS_TYPE = 0;
private static final int BYTE_POS_GEN = BYTE_POS_TYPE + 1;
private static final int BYTE_POS_KEYCOUNT = BYTE_POS_GEN + 4;
private static final int BYTE_POS_RIGHTSIBLING = BYTE_POS_KEYCOUNT + Integer.BYTES;
private static final int BYTE_POS_LEFTSIBLING = BYTE_POS_RIGHTSIBLING + SIZE_PAGE_REFERENCE;
private static final int BYTE_POS_NEWGEN = BYTE_POS_LEFTSIBLING + SIZE_PAGE_REFERENCE;
static final int HEADER_LENGTH = BYTE_POS_NEWGEN + SIZE_PAGE_REFERENCE;
private static final byte LEAF_FLAG = 1;
private static final byte INTERNAL_FLAG = 0;
static final long NO_NODE_FLAG = 0;
private final int internalMaxKeyCount;
private final int leafMaxKeyCount;
private final Layout<KEY,VALUE> layout;
private final int keySize;
private final int valueSize;
TreeNode( int pageSize, Layout<KEY,VALUE> layout )
{
this.layout = layout;
this.keySize = layout.keySize();
this.valueSize = layout.valueSize();
this.internalMaxKeyCount = Math.floorDiv( pageSize - (HEADER_LENGTH + SIZE_PAGE_REFERENCE),
keySize + SIZE_PAGE_REFERENCE);
this.leafMaxKeyCount = Math.floorDiv( pageSize - HEADER_LENGTH, keySize + valueSize );
if ( internalMaxKeyCount < 2 )
{
throw new IllegalArgumentException( "A page size of " + pageSize + " would only fit " +
internalMaxKeyCount + " internal keys, minimum is 2" );
}
if ( leafMaxKeyCount < 2 )
{
throw new IllegalArgumentException( "A page size of " + pageSize + " would only fit " +
leafMaxKeyCount + " leaf keys, minimum is 2" );
}
}
private void initialize( PageCursor cursor, byte type, long stableGeneration, long unstableGeneration )
{
cursor.putByte( BYTE_POS_TYPE, type );
setGen( cursor, unstableGeneration );
setKeyCount( cursor, 0 );
setRightSibling( cursor, NO_NODE_FLAG, stableGeneration, unstableGeneration );
setLeftSibling( cursor, NO_NODE_FLAG, stableGeneration, unstableGeneration );
setNewGen( cursor, NO_NODE_FLAG, stableGeneration, unstableGeneration );
}
void initializeLeaf( PageCursor cursor, long stableGeneration, long unstableGeneration )
{
initialize( cursor, LEAF_FLAG, stableGeneration, unstableGeneration );
}
void initializeInternal( PageCursor cursor, long stableGeneration, long unstableGeneration )
{
initialize( cursor, INTERNAL_FLAG, stableGeneration, unstableGeneration );
}
// HEADER METHODS
boolean isLeaf( PageCursor cursor )
{
return cursor.getByte( BYTE_POS_TYPE ) == LEAF_FLAG;
}
boolean isInternal( PageCursor cursor )
{
return cursor.getByte( BYTE_POS_TYPE ) == INTERNAL_FLAG;
}
long gen( PageCursor cursor )
{
return cursor.getInt( BYTE_POS_GEN ) & GenSafePointer.GENERATION_MASK;
}
int keyCount( PageCursor cursor )
{
return cursor.getInt( BYTE_POS_KEYCOUNT );
}
long rightSibling( PageCursor cursor, long stableGeneration, long unstableGeneration )
{
cursor.setOffset( BYTE_POS_RIGHTSIBLING );
return GenSafePointerPair.read( cursor, stableGeneration, unstableGeneration );
}
long leftSibling( PageCursor cursor, long stableGeneration, long unstableGeneration )
{
cursor.setOffset( BYTE_POS_LEFTSIBLING );
return GenSafePointerPair.read( cursor, stableGeneration, unstableGeneration );
}
long newGen( PageCursor cursor, long stableGeneration, long unstableGeneration )
{
cursor.setOffset( BYTE_POS_NEWGEN );
return GenSafePointerPair.read( cursor, stableGeneration, unstableGeneration );
}
void setGen( PageCursor cursor, long generation )
{
GenSafePointer.assertGeneration( generation );
cursor.putInt( BYTE_POS_GEN, (int) generation );
}
void setKeyCount( PageCursor cursor, int count )
{
cursor.putInt( BYTE_POS_KEYCOUNT, count );
}
void setRightSibling( PageCursor cursor, long rightSiblingId, long stableGeneration, long unstableGeneration )
{
cursor.setOffset( BYTE_POS_RIGHTSIBLING );
GenSafePointerPair.write( cursor, rightSiblingId, stableGeneration, unstableGeneration );
}
void setLeftSibling( PageCursor cursor, long leftSiblingId, long stableGeneration, long unstableGeneration )
{
cursor.setOffset( BYTE_POS_LEFTSIBLING );
GenSafePointerPair.write( cursor, leftSiblingId, stableGeneration, unstableGeneration );
}
void setNewGen( PageCursor cursor, long newGenId, long stableGeneration, long unstableGeneration )
{
cursor.setOffset( BYTE_POS_NEWGEN );
GenSafePointerPair.write( cursor, newGenId, stableGeneration, unstableGeneration );
}
// BODY METHODS
KEY keyAt( PageCursor cursor, KEY into, int pos )
{
cursor.setOffset( keyOffset( pos ) );
layout.readKey( cursor, into );
return into;
}
void insertKeyAt( PageCursor cursor, KEY key, int pos, int keyCount, byte[] tmp )
{
insertSlotAt( cursor, pos, keyCount, keyOffset( 0 ), keySize, tmp );
cursor.setOffset( keyOffset( pos ) );
layout.writeKey( cursor, key );
}
void removeKeyAt( PageCursor cursor, int pos, int keyCount, byte[] tmp )
{
removeSlotAt( cursor, pos, keyCount, keyOffset( 0 ), keySize, tmp );
}
private void removeSlotAt( PageCursor cursor, int pos, int keyCount, int baseOffset, int itemSize, byte[] tmp )
{
int from = pos + 1;
int count = keyCount - from;
copyItems( cursor, from, count, baseOffset, itemSize, tmp );
writeItems( cursor, pos, count, baseOffset, itemSize, tmp );
}
/**
* Moves items (key/value/child) one step to the right, which means rewriting all items of the particular type
* from pos - keyCount.
*/
private void insertSlotAt( PageCursor cursor, int pos, int toExcluding, int baseOffset, int itemSize, byte[] tmp )
{
// Move all items after pos one step to the right
int count = toExcluding - pos;
if ( count > 0 )
{
copyItems( cursor, pos, count, baseOffset, itemSize, tmp );
writeItems( cursor, pos + 1, count, baseOffset, itemSize, tmp );
}
}
private void writeItems( PageCursor cursor, int pos, int count, int baseOffset, int itemSize, byte[] tmp )
{
cursor.setOffset( baseOffset + pos * itemSize );
cursor.putBytes( tmp, 0, count * itemSize );
}
private void copyItems( PageCursor cursor, int pos, int count, int baseOffset, int itemSize, byte[] tmp )
{
cursor.setOffset( baseOffset + pos * itemSize );
cursor.getBytes( tmp, 0, count * itemSize );
}
VALUE valueAt( PageCursor cursor, VALUE value, int pos )
{
cursor.setOffset( valueOffset( pos ) );
layout.readValue( cursor, value );
return value;
}
void insertValueAt( PageCursor cursor, VALUE value, int pos, int keyCount, byte[] tmp )
{
insertSlotAt( cursor, pos, keyCount, valueOffset( 0 ), valueSize, tmp );
setValueAt( cursor, value, pos );
}
void removeValueAt( PageCursor cursor, int pos, int keyCount, byte[] tmp )
{
removeSlotAt( cursor, pos, keyCount, valueOffset( 0 ), valueSize, tmp );
}
void setValueAt( PageCursor cursor, VALUE value, int pos )
{
cursor.setOffset( valueOffset( pos ) );
layout.writeValue( cursor, value );
}
long childAt( PageCursor cursor, int pos, long stableGeneration, long unstableGeneration )
{
cursor.setOffset( childOffset( pos ) );
return GenSafePointerPair.read( cursor, stableGeneration, unstableGeneration );
}
void insertChildAt( PageCursor cursor, long child, int pos, int keyCount, byte[] tmp,
long stableGeneration, long unstableGeneration )
{
insertSlotAt( cursor, pos, keyCount + 1, childOffset( 0 ), SIZE_PAGE_REFERENCE, tmp );
setChildAt( cursor, child, pos, stableGeneration, unstableGeneration );
}
void setChildAt( PageCursor cursor, long child, int pos, long stableGeneration, long unstableGeneration )
{
cursor.setOffset( childOffset( pos ) );
writeChild( cursor, child, stableGeneration, unstableGeneration );
}
void writeChild( PageCursor cursor, long child, long stableGeneration, long unstableGeneration)
{
GenSafePointerPair.write( cursor, child, stableGeneration, unstableGeneration );
}
int internalMaxKeyCount()
{
return internalMaxKeyCount;
}
int leafMaxKeyCount()
{
return leafMaxKeyCount;
}
// HELPERS
int keyOffset( int pos )
{
return HEADER_LENGTH + pos * keySize;
}
private int valueOffset( int pos )
{
return HEADER_LENGTH + leafMaxKeyCount * keySize + pos * valueSize;
}
int childOffset( int pos )
{
return HEADER_LENGTH + internalMaxKeyCount * keySize + pos * SIZE_PAGE_REFERENCE;
}
boolean isNode( long node )
{
return node != NO_NODE_FLAG;
}
int keySize()
{
return keySize;
}
int valueSize()
{
return valueSize;
}
int childSize()
{
return SIZE_PAGE_REFERENCE;
}
Comparator<KEY> keyComparator()
{
return layout;
}
int readKeysWithInsertRecordInPosition( PageCursor cursor, Consumer<PageCursor> newRecordWriter,
int insertPosition, int totalNumberOfRecords, byte[] into )
{
return readRecordsWithInsertRecordInPosition( cursor, newRecordWriter, insertPosition, totalNumberOfRecords,
keySize, keyOffset( 0 ), into );
}
int readValuesWithInsertRecordInPosition( PageCursor cursor, Consumer<PageCursor> newRecordWriter,
int insertPosition, int totalNumberOfRecords, byte[] into )
{
return readRecordsWithInsertRecordInPosition( cursor, newRecordWriter, insertPosition, totalNumberOfRecords,
valueSize, valueOffset( 0 ), into );
}
int readChildrenWithInsertRecordInPosition( PageCursor cursor, Consumer<PageCursor> newRecordWriter,
int insertPosition, int totalNumberOfRecords, byte[] into )
{
return readRecordsWithInsertRecordInPosition( cursor, newRecordWriter, insertPosition, totalNumberOfRecords,
childSize(), childOffset( 0 ), into );
}
void goTo( PageCursor cursor, long childId, long stableGeneration, long unstableGeneration )
throws IOException
{
if ( !cursor.next( childId ) )
{
throw new IllegalStateException( "Could not go to " + childId );
}
verifyGen( cursor, stableGeneration, unstableGeneration );
}
private void verifyGen( PageCursor cursor, long stableGeneration, long unstableGeneration )
{
long gen = gen( cursor );
if ( ( gen > stableGeneration && gen < unstableGeneration ) || gen > unstableGeneration )
{
throw new IllegalStateException( "Reached a node with generation=" + gen +
", stableGeneration=" + stableGeneration + ", unstableGeneration=" + unstableGeneration );
}
}
/**
* Leaves cursor on same page as when called. No guarantees on offset.
* <p>
* Create a byte[] with totalNumberOfRecords of recordSize from cursor reading from baseRecordOffset
* with newRecord inserted in insertPosition, with the following records shifted to the right.
* <p>
* Simply: Records of size recordSize that can be read from offset baseRecordOffset in page pinned by cursor has
* some ordering. This ordering is preserved with new record inserted in insertPosition in the returned byte[],
* NOT in the page.
*
* @param cursor {@link org.neo4j.io.pagecache.PageCursor} pinned to page to read records from
* @param newRecordWriter new data to be inserted in insertPosition in returned byte[].
* This is a {@link Consumer} since the type of data can differ (value/child),
* although perhaps this can be done in a better way
* @param insertPosition position of newRecord. 0 being before all other records,
* (totalNumberOfRecords - 1) being after all other records
* @param totalNumberOfRecords the total number of records to be contained in returned byte[], including newRecord
* @param recordSize the size in number of bytes of one record
* @param baseRecordOffset the offset from where cursor should start read records
* @param into byte array to copy bytes into
* @return number of bytes copied into the {@code into} byte[],
* that is insertPosition * recordSize
*/
private int readRecordsWithInsertRecordInPosition( PageCursor cursor, Consumer<PageCursor> newRecordWriter,
int insertPosition, int totalNumberOfRecords, int recordSize, int baseRecordOffset, byte[] into )
{
int length = (totalNumberOfRecords) * recordSize;
// First read all records
// Read all records on previous to insertPosition
cursor.setOffset( baseRecordOffset );
cursor.getBytes( into, 0, insertPosition * recordSize );
// Read newRecord
PageCursor buffer = ByteArrayPageCursor.wrap( into, insertPosition * recordSize, recordSize );
newRecordWriter.accept( buffer );
// Read all records following insertPosition
cursor.setOffset( baseRecordOffset + insertPosition * recordSize );
cursor.getBytes( into, (insertPosition + 1) * recordSize,
((totalNumberOfRecords - 1) - insertPosition) * recordSize );
return length;
}
private void writeAll( PageCursor cursor, byte[] source, int sourcePos, int targetPos, int count,
int baseOffset, int recordSize )
{
int arrayOffset = sourcePos * recordSize;
cursor.setOffset( baseOffset + recordSize * targetPos );
cursor.putBytes( source, arrayOffset, count * recordSize );
}
void writeKeys( PageCursor cursor, byte[] source, int sourcePos, int targetPos, int count )
{
writeAll( cursor, source, sourcePos, targetPos, count, keyOffset( 0 ), keySize() );
}
void writeValues( PageCursor cursor, byte[] source, int sourcePos, int targetPos, int count )
{
writeAll( cursor, source, sourcePos, targetPos, count, valueOffset( 0 ), valueSize() );
}
void writeChildren( PageCursor cursor, byte[] source, int sourcePos, int targetPos, int count )
{
writeAll( cursor, source, sourcePos, targetPos, count, childOffset( 0 ), childSize() );
}
}