-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
TreeNode.java
412 lines (352 loc) · 14 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
/*
* 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 java.io.IOException;
import java.util.Comparator;
import org.neo4j.io.pagecache.PageCursor;
import static org.neo4j.index.internal.gbptree.GenSafePointerPair.NO_LOGICAL_POS;
import static org.neo4j.index.internal.gbptree.GenSafePointerPair.read;
/**
* Methods to manipulate single tree 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 82B ]|[ KEYS ]|[ CHILDREN ]
* [NODETYPE][TYPE][GEN][KEYCOUNT][RIGHTSIBLING][LEFTSIBLING][NEWGEN]|[[KEY][KEY]...##]|[[CHILD][CHILD][CHILD]...##]
* 0 1 6 10 34 58
* </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 82B ]|[ KEYS ]|[ VALUES ]
* [NODETYPE][TYPE][GEN][KEYCOUNT][RIGHTSIBLING][LEFTSIBLING][NEWGEN]|[[KEY][KEY]...##]|[[VALUE][VALUE]...##]
* 0 1 6 10 34 58
* </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>
{
// Shared between all node types: TreeNode and FreelistNode
static final int BYTE_POS_NODE_TYPE = 0;
static final byte NODE_TYPE_TREE_NODE = 1;
static final byte NODE_TYPE_FREE_LIST_NODE = 2;
static final int SIZE_PAGE_REFERENCE = GenSafePointerPair.SIZE;
static final int BYTE_POS_TYPE = BYTE_POS_NODE_TYPE + Byte.BYTES;
static final int BYTE_POS_GEN = BYTE_POS_TYPE + Byte.BYTES;
static final int BYTE_POS_KEYCOUNT = BYTE_POS_GEN + Integer.BYTES;
static final int BYTE_POS_RIGHTSIBLING = BYTE_POS_KEYCOUNT + Integer.BYTES;
static final int BYTE_POS_LEFTSIBLING = BYTE_POS_RIGHTSIBLING + SIZE_PAGE_REFERENCE;
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;
static final byte INTERNAL_FLAG = 0;
static final long NO_NODE_FLAG = 0;
private final int pageSize;
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.pageSize = pageSize;
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 MetadataMismatchException( "For layout " + layout + " a page size of " + pageSize +
" would only fit " + internalMaxKeyCount + " internal keys, minimum is 2" );
}
if ( leafMaxKeyCount < 2 )
{
throw new MetadataMismatchException( "A page size of " + pageSize + " would only fit " +
leafMaxKeyCount + " leaf keys, minimum is 2" );
}
}
static byte nodeType( PageCursor cursor )
{
return cursor.getByte( BYTE_POS_NODE_TYPE );
}
private void initialize( PageCursor cursor, byte type, long stableGeneration, long unstableGeneration )
{
cursor.putByte( BYTE_POS_NODE_TYPE, NODE_TYPE_TREE_NODE );
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
static boolean isLeaf( PageCursor cursor )
{
return cursor.getByte( BYTE_POS_TYPE ) == LEAF_FLAG;
}
static 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 read( cursor, stableGeneration, unstableGeneration, NO_LOGICAL_POS );
}
long leftSibling( PageCursor cursor, long stableGeneration, long unstableGeneration )
{
cursor.setOffset( BYTE_POS_LEFTSIBLING );
return read( cursor, stableGeneration, unstableGeneration, NO_LOGICAL_POS );
}
long newGen( PageCursor cursor, long stableGeneration, long unstableGeneration )
{
cursor.setOffset( BYTE_POS_NEWGEN );
return read( cursor, stableGeneration, unstableGeneration, NO_LOGICAL_POS );
}
void setGen( PageCursor cursor, long generation )
{
GenSafePointer.assertGenerationOnWrite( 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 );
long result = GenSafePointerPair.write( cursor, rightSiblingId, stableGeneration, unstableGeneration );
GenSafePointerPair.assertSuccess( result );
}
void setLeftSibling( PageCursor cursor, long leftSiblingId, long stableGeneration, long unstableGeneration )
{
cursor.setOffset( BYTE_POS_LEFTSIBLING );
long result = GenSafePointerPair.write( cursor, leftSiblingId, stableGeneration, unstableGeneration );
GenSafePointerPair.assertSuccess( result );
}
void setNewGen( PageCursor cursor, long newGenId, long stableGeneration, long unstableGeneration )
{
cursor.setOffset( BYTE_POS_NEWGEN );
long result = GenSafePointerPair.write( cursor, newGenId, stableGeneration, unstableGeneration );
GenSafePointerPair.assertSuccess( result );
}
long pointerGen( PageCursor cursor, long readResult )
{
if ( !GenSafePointerPair.isRead( readResult ) )
{
throw new IllegalArgumentException( "Expected read result, but got " + readResult );
}
int offset = GenSafePointerPair.genOffset( readResult );
int gsppOffset = GenSafePointerPair.isLogicalPos( readResult ) ? childOffset( offset ) : offset;
int gspOffset = GenSafePointerPair.resultIsFromSlotA( readResult ) ?
gsppOffset : gsppOffset + GenSafePointer.SIZE;
cursor.setOffset( gspOffset );
return GenSafePointer.readGeneration( cursor );
}
// 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 )
{
insertKeySlotsAt( cursor, pos, 1, keyCount );
cursor.setOffset( keyOffset( pos ) );
layout.writeKey( cursor, key );
}
void removeKeyAt( PageCursor cursor, int pos, int keyCount )
{
removeSlotAt( cursor, pos, keyCount, keyOffset( 0 ), keySize );
}
private void removeSlotAt( PageCursor cursor, int pos, int itemCount, int baseOffset, int itemSize )
{
for ( int posToMoveLeft = pos + 1, offset = baseOffset + posToMoveLeft * itemSize;
posToMoveLeft < itemCount; posToMoveLeft++, offset += itemSize )
{
cursor.copyTo( offset, cursor, offset - itemSize, itemSize );
}
}
void setKeyAt( PageCursor cursor, KEY key, int pos )
{
cursor.setOffset( keyOffset( pos ) );
layout.writeKey( cursor, key );
}
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 )
{
insertValueSlotsAt( cursor, pos, 1, keyCount );
setValueAt( cursor, value, pos );
}
void removeValueAt( PageCursor cursor, int pos, int keyCount )
{
removeSlotAt( cursor, pos, keyCount, valueOffset( 0 ), valueSize );
}
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 read( cursor, stableGeneration, unstableGeneration, pos );
}
void insertChildAt( PageCursor cursor, long child, int pos, int keyCount,
long stableGeneration, long unstableGeneration )
{
insertChildSlotsAt( cursor, pos, 1, keyCount );
setChildAt( cursor, child, pos, stableGeneration, unstableGeneration );
}
void removeChildAt( PageCursor cursor, int pos, int keyCount )
{
removeSlotAt( cursor, pos, keyCount + 1, childOffset( 0 ), childSize() );
}
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 );
}
/**
* Moves items (key/value/child) one step to the right, which means rewriting all items of the particular type
* from pos - itemCount.
* itemCount is keyCount for key and value, but keyCount+1 for children.
*/
private void insertSlotsAt( PageCursor cursor, int pos, int numberOfSlots, int itemCount, int baseOffset,
int itemSize )
{
for ( int posToMoveRight = itemCount - 1, offset = baseOffset + posToMoveRight * itemSize;
posToMoveRight >= pos; posToMoveRight--, offset -= itemSize )
{
cursor.copyTo( offset, cursor, offset + itemSize * numberOfSlots, itemSize );
}
}
void insertKeySlotsAt( PageCursor cursor, int pos, int numberOfSlots, int keyCount )
{
insertSlotsAt( cursor, pos, numberOfSlots, keyCount, keyOffset( 0 ), keySize );
}
void insertValueSlotsAt( PageCursor cursor, int pos, int numberOfSlots, int keyCount )
{
insertSlotsAt( cursor, pos, numberOfSlots, keyCount, valueOffset( 0 ), valueSize );
}
void insertChildSlotsAt( PageCursor cursor, int pos, int numberOfSlots, int keyCount )
{
insertSlotsAt( cursor, pos, numberOfSlots, keyCount + 1, childOffset( 0 ), childSize() );
}
int internalMaxKeyCount()
{
return internalMaxKeyCount;
}
int leafMaxKeyCount()
{
return leafMaxKeyCount;
}
// HELPERS
int keyOffset( int pos )
{
return HEADER_LENGTH + pos * keySize;
}
int valueOffset( int pos )
{
return HEADER_LENGTH + leafMaxKeyCount * keySize + pos * valueSize;
}
int childOffset( int pos )
{
return HEADER_LENGTH + internalMaxKeyCount * keySize + pos * SIZE_PAGE_REFERENCE;
}
static boolean isNode( long node )
{
return GenSafePointerPair.pointer( node ) != NO_NODE_FLAG;
}
int keySize()
{
return keySize;
}
int valueSize()
{
return valueSize;
}
int childSize()
{
return SIZE_PAGE_REFERENCE;
}
Comparator<KEY> keyComparator()
{
return layout;
}
void goTo( PageCursor cursor, String messageOnError, long nodeId )
throws IOException
{
PageCursorUtil.goTo( cursor, messageOnError, GenSafePointerPair.pointer( nodeId ) );
}
@Override
public String toString()
{
return "TreeNode[pageSize:" + pageSize + ", internalMax:" + internalMaxKeyCount +
", leafMax:" + leafMaxKeyCount + ", keySize:" + keySize + ", valueSize:" + valueSize + "]";
}
}