-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
GBPTree.java
514 lines (467 loc) · 20.3 KB
/
GBPTree.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
/*
* 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.File;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.atomic.AtomicReference;
import org.neo4j.cursor.RawCursor;
import org.neo4j.index.Hit;
import org.neo4j.index.Index;
import org.neo4j.index.IndexWriter;
import org.neo4j.index.ValueMerger;
import org.neo4j.index.ValueMergers;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.io.pagecache.PageCursor;
import org.neo4j.io.pagecache.PagedFile;
/**
* A generation-aware B+tree (GB+Tree) implementation directly atop a {@link PageCache} with no caching in between.
* Additionally internal and leaf nodes on same level are linked both left and right (sibling pointers),
* this to provide correct reading when concurrently {@link #writer(IndexWriter.Options) modifying}
* the tree.
* <p>
* Generation is incremented on {@link #flush() flushing a.k.a check-pointing}.
* Generation awareness allows for recovery from last {@link #flush()}, provided the same updates will be
* replayed onto the index since that point in time.
* <p>
* Changes to tree nodes are made so that stable nodes (i.e. nodes that have survived at least one flush)
* are immutable w/ regards to keys values and child/sibling pointers.
* Making a change in a stable node will copy the node to an unstable generation first and then make the change
* in that unstable version. Further change in that node in the same generation will not require a copy since
* it's already unstable.
* <p>
* Every pointer to another node (child/sibling pointer) consists of two pointers, one to a stable version and
* one to a potentially unstable version. A stable -> unstable node copy will have its parent redirect one of its
* two pointers to the new unstable version, redirecting readers and writers to the new unstable version,
* while at the same time keeping one pointer to the stable version, in case there's a crash or non-clean
* shutdown, followed by recovery.
* <p>
* Currently no leaves will be removed or merged as part of {@link IndexWriter#remove(Object) removals}.
* <p>
* A single writer w/ multiple concurrent readers is supported. Assuming usage adheres to this
* constraint neither writer nor readers are blocking. Readers are virtually garbage-free.
* <p>
* An reader of GB+Tree is a {@link SeekCursor} that returns result as it finds them.
* As the cursor move over keys/values, returned results are considered "behind" it
* and likewise keys not yet returned "in front of".
* Readers will always read latest written changes in front of it but will not see changes that appear behind.
* The isolation level is thus read committed.
* The tree have no knowledge about transactions and apply updates as isolated units of work one entry at the time.
* Therefore, readers can see parts of transactions that are not fully applied yet.
*
* @param <KEY> type of keys
* @param <VALUE> type of values
*/
public class GBPTree<KEY,VALUE> implements Index<KEY,VALUE>, IdProvider
{
/**
* Paged file in a {@link PageCache} providing the means of storage.
*/
private final PagedFile pagedFile;
/**
* User-provided layout of key/value as well as custom additional meta information.
* This allows for custom key/value and comparison representation. The layout provided during index
* creation, i.e. the first time constructor is called for the given paged file, will be stored
* in the meta page and it's asserted that the same layout is passed to the constructor when opening the tree.
*/
private final Layout<KEY,VALUE> layout;
/**
* Instance of {@link TreeNode} which handles reading/writing physical bytes from pages representing tree nodes.
*/
private final TreeNode<KEY,VALUE> bTreeNode;
/**
* Currently an index only supports one concurrent writer and so this reference will act both as
* guard so that only one thread can have it at any given time and also as synchronization between threads
* wanting it.
*/
private final AtomicReference<SingleIndexWriter> writer;
/**
* Page size, i.e. tree node size, of the tree nodes in this tree. The page size is determined on
* tree creation, stored in meta page and read when opening tree later.
*/
private int pageSize;
/**
* Whether or not the tree was created this time it was instantiated.
*/
private boolean created;
/**
* Current page id which contains the root of the tree.
*/
private volatile long rootId = IdSpace.MIN_TREE_NODE_ID;
/**
* Last allocated page id, used for allocating new ids as more data gets inserted into the tree.
*/
private volatile long lastId = rootId;
/**
* Stable generation, i.e. generation which has survived the last {@link #flush()}.
* Unsigned int.
*/
private volatile long stableGeneration = 0;
/**
* Unstable generation, i.e. the current generation under evolution. This generation will be the
* {@link #stableGeneration} in the next {@link #flush()}.
* Unsigned int.
*/
private volatile long unstableGeneration = 1;
/**
* Opens an index {@code indexFile} in the {@code pageCache}, creating and initializing it if it doesn't exist.
* If the index doesn't exist it will be created and the {@link Layout} and {@code pageSize} will
* be written in index header.
* If the index exists it will be opened and the {@link Layout} will be matched with the information
* in the header. At the very least {@link Layout#identifier()} will be matched, but also if the
* index has {@link Layout#writeMetaData(PageCursor)} additional meta data it will be
* {@link Layout#readMetaData(PageCursor)}.
*
* @param pageCache {@link PageCache} to use to map index file
* @param indexFile {@link File} containing the actual index
* @param tentativePageSize page size, i.e. tree node size. Must be less than or equal to that of the page cache.
* A pageSize of {@code 0} means to use whatever the page cache has (at creation)
* @param layout {@link Layout} to use in the tree, this must match the existing layout
* we're just opening the index
* @throws IOException on page cache error
*/
public GBPTree( PageCache pageCache, File indexFile, Layout<KEY,VALUE> layout, int tentativePageSize )
throws IOException
{
this.layout = layout;
this.pagedFile = openOrCreate( pageCache, indexFile, tentativePageSize, layout );
this.bTreeNode = new TreeNode<>( pageSize, layout );
this.writer = new AtomicReference<>(
new SingleIndexWriter( new InternalTreeLogic<>( this, bTreeNode, layout ) ) );
if ( created )
{
// Initialize index root node to a leaf node.
try ( PageCursor cursor = pagedFile.io( rootId, PagedFile.PF_SHARED_WRITE_LOCK ) )
{
goToRoot( cursor );
bTreeNode.initializeLeaf( cursor, stableGeneration, unstableGeneration );
checkOutOfBounds( cursor );
}
}
}
private PagedFile openOrCreate( PageCache pageCache, File indexFile,
int pageSizeForCreation, Layout<KEY,VALUE> layout ) throws IOException
{
try
{
PagedFile pagedFile = pageCache.map( indexFile, pageCache.pageSize() );
// This index already exists, verify the header with what we got passed into the constructor this time
try
{
// Read header
long layoutIdentifier;
int majorVersion;
int minorVersion;
try ( PageCursor metaCursor = openMetaPageCursor( pagedFile ) )
{
do
{
pageSize = metaCursor.getInt();
rootId = metaCursor.getLong();
lastId = metaCursor.getLong();
layoutIdentifier = metaCursor.getLong();
majorVersion = metaCursor.getInt();
minorVersion = metaCursor.getInt();
layout.readMetaData( metaCursor );
}
while ( metaCursor.shouldRetry() );
checkOutOfBounds( metaCursor );
}
if ( layoutIdentifier != layout.identifier() )
{
throw new IllegalArgumentException( "Tried to open " + indexFile + " using different layout "
+ layout.identifier() + " than what it was created with " + layoutIdentifier );
}
if ( majorVersion != layout.majorVersion() || minorVersion != layout.minorVersion() )
{
throw new IllegalArgumentException( "Index is of another version than the layout " +
"it tries to be opened with. Index version is [" + majorVersion + "." + minorVersion + "]" +
", but tried to load the index with version [" +
layout.majorVersion() + "." + layout.minorVersion() + "]" );
}
// This index was created with another page size, re-open with that actual page size
if ( pageSize != pageCache.pageSize() )
{
if ( pageSize > pageCache.pageSize() )
{
throw new IllegalStateException( "Index was created with page size:" + pageSize
+ ", but page cache used to open it this time has a smaller page size:"
+ pageCache.pageSize() + " so cannot be opened" );
}
pagedFile.close();
pagedFile = pageCache.map( indexFile, pageSize );
}
return pagedFile;
}
catch ( Throwable t )
{
try
{
pagedFile.close();
}
catch ( IOException e )
{
t.addSuppressed( e );
}
throw t;
}
}
catch ( NoSuchFileException e )
{
pageSize = pageSizeForCreation == 0 ? pageCache.pageSize() : pageSizeForCreation;
if ( pageSize > pageCache.pageSize() )
{
throw new IllegalStateException( "Index was about to be created with page size:" + pageSize +
", but page cache used to create it has a smaller page size:" +
pageCache.pageSize() + " so cannot be created" );
}
// We need to create this index
PagedFile pagedFile = pageCache.map( indexFile, pageSize, StandardOpenOption.CREATE );
// Write header
try ( PageCursor metaCursor = openMetaPageCursor( pagedFile ) )
{
metaCursor.putInt( pageSize );
metaCursor.putLong( rootId );
metaCursor.putLong( lastId );
metaCursor.putLong( layout.identifier() );
metaCursor.putInt( layout.majorVersion() );
metaCursor.putInt( layout.minorVersion() );
layout.writeMetaData( metaCursor );
checkOutOfBounds( metaCursor );
}
pagedFile.flushAndForce();
created = true;
return pagedFile;
}
}
private PageCursor openMetaPageCursor( PagedFile pagedFile ) throws IOException
{
PageCursor metaCursor = pagedFile.io( IdSpace.META_PAGE_ID, PagedFile.PF_SHARED_WRITE_LOCK );
if ( !metaCursor.next() )
{
throw new IllegalStateException( "Couldn't go to meta data page " + IdSpace.META_PAGE_ID );
}
return metaCursor;
}
@Override
public RawCursor<Hit<KEY,VALUE>,IOException> seek( KEY fromInclusive, KEY toExclusive ) throws IOException
{
PageCursor cursor = pagedFile.io( rootId, PagedFile.PF_SHARED_READ_LOCK );
KEY key = layout.newKey();
VALUE value = layout.newValue();
goToRoot( cursor );
boolean isInternal;
int keyCount;
long childId = 0; // initialized to satisfy compiler
int pos;
do
{
do
{
isInternal = bTreeNode.isInternal( cursor );
// Find the left-most key within from-range
keyCount = bTreeNode.keyCount( cursor );
int search = KeySearch.search( cursor, bTreeNode, fromInclusive, key, keyCount );
pos = KeySearch.positionOf( search );
// Assuming unique keys
if ( isInternal && KeySearch.isHit( search ) )
{
pos++;
}
if ( isInternal )
{
childId = bTreeNode.childAt( cursor, pos, stableGeneration, unstableGeneration );
}
}
while ( cursor.shouldRetry() );
checkOutOfBounds( cursor );
if ( isInternal )
{
PointerChecking.checkChildPointer( childId );
if ( !cursor.next( childId ) )
{
throw new IllegalStateException( "Couldn't go to child " + childId );
}
}
}
while ( isInternal && keyCount > 0 );
// Returns cursor which is now initiated with left-most leaf node for the specified range
return new SeekCursor<>( cursor, key, value, bTreeNode, fromInclusive, toExclusive, layout,
stableGeneration, unstableGeneration, pos, keyCount );
}
@Override
public long acquireNewId()
{
lastId++;
return lastId;
}
// Utility method
public void printTree() throws IOException
{
try ( PageCursor cursor = pagedFile.io( rootId, PagedFile.PF_SHARED_READ_LOCK ) )
{
cursor.next();
TreePrinter.printTree( cursor, bTreeNode, layout, stableGeneration, unstableGeneration, System.out );
}
}
// Utility method
boolean consistencyCheck() throws IOException
{
try ( PageCursor cursor = pagedFile.io( rootId, PagedFile.PF_SHARED_READ_LOCK ) )
{
cursor.next();
return new ConsistencyChecker<>( bTreeNode, layout, stableGeneration, unstableGeneration )
.check( cursor );
}
}
@Override
public void flush() throws IOException
{
try ( PageCursor cursor = openMetaPageCursor( pagedFile ) )
{
cursor.putLong( 4, rootId );
cursor.putLong( 12, lastId );
// generations should be incremented as part of flush, but this functionality doesn't exist yet.
checkOutOfBounds( cursor );
}
}
@Override
public void close() throws IOException
{
flush();
pagedFile.close();
}
/**
* @return the single {@link IndexWriter} for this index. The returned writer must be
* {@link IndexWriter#close() closed} before another caller can acquire this writer.
* @throws IllegalStateException for calls made between a successful call to this method and closing the
* returned writer.
*/
@Override
public IndexWriter<KEY,VALUE> writer( IndexWriter.Options options ) throws IOException
{
SingleIndexWriter result = this.writer.getAndSet( null );
if ( result == null )
{
throw new IllegalStateException( "Only supports one concurrent writer" );
}
return result.take( rootId, options );
}
private void goToRoot( PageCursor cursor ) throws IOException
{
if ( !cursor.next( rootId ) )
{
throw new IllegalStateException( "Could not go to root " + rootId );
}
}
private void checkOutOfBounds( PageCursor cursor )
{
if ( cursor.checkAndClearBoundsFlag() )
{
throw new IllegalStateException( "Some internal problem causing out of bounds" );
}
}
private class SingleIndexWriter implements IndexWriter<KEY,VALUE>
{
private final InternalTreeLogic<KEY,VALUE> treeLogic;
private final StructurePropagation<KEY> structurePropagation;
private PageCursor cursor;
private IndexWriter.Options options;
private final byte[] tmp = new byte[0];
SingleIndexWriter( InternalTreeLogic<KEY,VALUE> treeLogic )
{
this.structurePropagation = new StructurePropagation<>( layout.newKey() );
this.treeLogic = treeLogic;
}
SingleIndexWriter take( long rootId, IndexWriter.Options options ) throws IOException
{
this.options = options;
cursor = pagedFile.io( rootId, PagedFile.PF_SHARED_WRITE_LOCK );
return this;
}
@Override
public void put( KEY key, VALUE value ) throws IOException
{
merge( key, value, ValueMergers.overwrite() );
}
@Override
public void merge( KEY key, VALUE value, ValueMerger<VALUE> valueMerger ) throws IOException
{
goToRoot( cursor );
treeLogic.insert( cursor, structurePropagation, key, value, valueMerger, options,
stableGeneration, unstableGeneration );
if ( structurePropagation.hasSplit )
{
structurePropagation.hasSplit = false;
// New root
long newRootId = acquireNewId();
if ( !cursor.next( newRootId ) )
{
throw new IllegalStateException( "Could not go to new root " + newRootId );
}
bTreeNode.initializeInternal( cursor, stableGeneration, unstableGeneration );
bTreeNode.insertKeyAt( cursor, structurePropagation.primKey, 0, 0, tmp );
bTreeNode.setKeyCount( cursor, 1 );
bTreeNode.setChildAt( cursor, structurePropagation.left, 0, stableGeneration, unstableGeneration );
bTreeNode.setChildAt( cursor, structurePropagation.right, 1, stableGeneration, unstableGeneration );
rootId = newRootId;
}
else if ( structurePropagation.hasNewGen )
{
rootId = structurePropagation.left;
}
structurePropagation.hasNewGen = false;
checkOutOfBounds( cursor );
}
@Override
public VALUE remove( KEY key ) throws IOException
{
goToRoot( cursor );
VALUE result = treeLogic.remove( cursor, structurePropagation, key, layout.newValue(),
stableGeneration, unstableGeneration );
if ( structurePropagation.hasNewGen )
{
structurePropagation.hasNewGen = false;
rootId = structurePropagation.left;
}
checkOutOfBounds( cursor );
return result;
}
@Override
public void close() throws IOException
{
boolean success = false;
try
{
cursor.close();
success = true;
}
finally
{
// Check success to avoid suppressing exception from cursor.close()
if ( success && !GBPTree.this.writer.compareAndSet( null, this ) )
{
throw new IllegalStateException( "Tried to give back the writer, but somebody else already did" );
}
}
}
}
}