-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
MuninnPageCursor.java
886 lines (804 loc) · 29.3 KB
/
MuninnPageCursor.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
/*
* Copyright (c) 2002-2018 "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.io.pagecache.impl.muninn;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
import org.neo4j.io.pagecache.CursorException;
import org.neo4j.io.pagecache.PageCursor;
import org.neo4j.io.pagecache.PageSwapper;
import org.neo4j.io.pagecache.tracing.PageFaultEvent;
import org.neo4j.io.pagecache.tracing.PinEvent;
import org.neo4j.io.pagecache.tracing.cursor.PageCursorTracer;
import org.neo4j.io.pagecache.tracing.cursor.context.VersionContext;
import org.neo4j.io.pagecache.tracing.cursor.context.VersionContextSupplier;
import org.neo4j.unsafe.impl.internal.dragons.UnsafeUtil;
import static org.neo4j.io.pagecache.PagedFile.PF_EAGER_FLUSH;
import static org.neo4j.io.pagecache.PagedFile.PF_SHARED_WRITE_LOCK;
import static org.neo4j.io.pagecache.impl.muninn.MuninnPagedFile.UNMAPPED_TTE;
import static org.neo4j.unsafe.impl.internal.dragons.FeatureToggles.flag;
abstract class MuninnPageCursor extends PageCursor
{
private static final boolean usePreciseCursorErrorStackTraces =
flag( MuninnPageCursor.class, "usePreciseCursorErrorStackTraces", false );
private static final boolean boundsCheck = flag( MuninnPageCursor.class, "boundsCheck", true );
// Size of the respective primitive types in bytes.
private static final int SIZE_OF_BYTE = Byte.BYTES;
private static final int SIZE_OF_SHORT = Short.BYTES;
private static final int SIZE_OF_INT = Integer.BYTES;
private static final int SIZE_OF_LONG = Long.BYTES;
private final long victimPage;
private final PageCursorTracer tracer;
protected MuninnPagedFile pagedFile;
protected PageSwapper swapper;
protected int swapperId;
protected long pinnedPageRef;
protected PinEvent pinEvent;
protected long pageId;
protected int pf_flags;
protected boolean eagerFlush;
protected long currentPageId;
protected long nextPageId;
protected MuninnPageCursor linkedCursor;
private long pointer;
private int pageSize;
private int filePageSize;
protected final VersionContextSupplier versionContextSupplier;
private int offset;
private boolean outOfBounds;
private boolean isLinkedCursor;
// This is a String with the exception message if usePreciseCursorErrorStackTraces is false, otherwise it is a
// CursorExceptionWithPreciseStackTrace with the message and stack trace pointing more or less directly at the
// offending code.
private Object cursorException;
MuninnPageCursor( long victimPage, PageCursorTracer tracer, VersionContextSupplier versionContextSupplier )
{
this.victimPage = victimPage;
this.pointer = victimPage;
this.tracer = tracer;
this.versionContextSupplier = versionContextSupplier;
}
final void initialiseFile( MuninnPagedFile pagedFile )
{
this.swapper = pagedFile.swapper;
this.swapperId = pagedFile.swapperId;
this.filePageSize = pagedFile.filePageSize;
}
final void initialiseFlags( MuninnPagedFile pagedFile, long pageId, int pf_flags )
{
this.pagedFile = pagedFile;
this.pageId = pageId;
this.pf_flags = pf_flags;
this.eagerFlush = (pf_flags & PF_EAGER_FLUSH) == PF_EAGER_FLUSH;
}
@Override
public final void rewind()
{
nextPageId = pageId;
currentPageId = UNBOUND_PAGE_ID;
}
public final void reset( long pageRef )
{
this.pinnedPageRef = pageRef;
this.offset = 0;
this.pointer = pagedFile.getAddress( pageRef );
this.pageSize = filePageSize;
pinEvent.setCachePageId( pagedFile.toId( pageRef ) );
}
@Override
public final boolean next( long pageId ) throws IOException
{
if ( currentPageId == pageId )
{
verifyContext();
return true;
}
nextPageId = pageId;
return next();
}
void verifyContext()
{
VersionContext versionContext = versionContextSupplier.getVersionContext();
if ( versionContext.lastClosedTransactionId() == Long.MAX_VALUE )
{
return;
}
if ( page.getLastModifiedTxId() > versionContext.lastClosedTransactionId() ||
pagedFile.getHighestEvictedTransactionId() > versionContext.lastClosedTransactionId() )
{
versionContext.markAsDirty();
}
}
@Override
public final void close()
{
if ( pagedFile == null )
{
return; // already closed
}
closeLinks( this );
if ( !isLinkedCursor )
{
releaseCursor();
}
}
private void closeLinks( MuninnPageCursor cursor )
{
while ( cursor != null && cursor.pagedFile != null )
{
cursor.unpinCurrentPage();
// We null out the pagedFile field to allow it and its (potentially big) translation table to be garbage
// collected when the file is unmapped, since the cursors can stick around in thread local caches, etc.
cursor.pagedFile = null;
cursor = cursor.linkedCursor;
}
}
private void closeLinkedCursorIfAny() throws IOException
{
if ( linkedCursor != null )
{
closeLinks( linkedCursor );
}
}
@Override
public PageCursor openLinkedCursor( long pageId ) throws IOException
{
closeLinkedCursorIfAny();
MuninnPagedFile pf = pagedFile;
if ( pf == null )
{
// This cursor has been closed
throw new IllegalStateException( "Cannot open linked cursor on closed page cursor" );
}
if ( linkedCursor != null )
{
linkedCursor.initialiseFlags( pf, pageId, pf_flags );
linkedCursor.rewind();
}
else
{
linkedCursor = (MuninnPageCursor) pf.io( pageId, pf_flags );
linkedCursor.isLinkedCursor = true;
}
return linkedCursor;
}
/**
* Must be called by {@link #unpinCurrentPage()}.
*/
void clearPageState()
{
pointer = victimPage; // make all future page access go to the victim page
pageSize = 0; // make all future bound checks fail
pinnedPageRef = 0;
currentPageId = UNBOUND_PAGE_ID;
cursorException = null;
}
@Override
public final long getCurrentPageId()
{
return currentPageId;
}
@Override
public final int getCurrentPageSize()
{
return currentPageId == UNBOUND_PAGE_ID ? UNBOUND_PAGE_SIZE : pagedFile.pageSize();
}
@Override
public final File getCurrentFile()
{
return currentPageId == UNBOUND_PAGE_ID ? null : pagedFile.file();
}
/**
* Pin the desired file page to this cursor, page faulting it into memory if it isn't there already.
* @param filePageId The file page id we want to pin this cursor to.
* @param writeLock 'true' if we will be taking a write lock on the page as part of the pin.
* @throws IOException if anything goes wrong with the pin, most likely during a page fault.
*/
protected void pin( long filePageId, boolean writeLock ) throws IOException
{
pinEvent = tracer.beginPin( writeLock, filePageId, swapper );
int chunkId = MuninnPagedFile.computeChunkId( filePageId );
// The chunkOffset is the addressing offset into the chunk array object for the relevant array slot. Using
// this, we can access the array slot with Unsafe.
long chunkOffset = MuninnPagedFile.computeChunkOffset( filePageId );
int[][] tt = pagedFile.translationTable;
if ( tt.length <= chunkId )
{
tt = expandTranslationTableCapacity( chunkId );
}
int[] chunk = tt[chunkId];
// Now, if the reference in the chunk slot is a latch, we wait on it and look up again (in a loop, since the
// page might get evicted right after the page fault completes). If we find a page, we lock it and check its
// binding (since it might get evicted and faulted into something else in the time between our look up and
// our locking of the page). If the reference is null or it referred to a page that had wrong bindings, we CAS
// in a latch. If that CAS succeeds, we page fault, set the slot to the faulted in page and open the latch.
// If the CAS failed, we retry the look up and start over from the top.
for (;;)
{
int mappedPageId = UnsafeUtil.getIntVolatile( chunk, chunkOffset );
if ( mappedPageId != UNMAPPED_TTE )
{
// We got *a* page, but we might be racing with eviction. To cope with that, we have to take some
// kind of lock on the page, and check that it is indeed bound to what we expect. If not, then it has
// been evicted, and possibly even page faulted into something else. In this case, we discard the
// item and try again, as the eviction thread would have set the chunk array slot to null.
long pageRef = pagedFile.deref( mappedPageId );
boolean locked = tryLockPage( pageRef );
if ( locked & pagedFile.isBoundTo( pageRef, swapperId, filePageId ) )
{
pinCursorToPage( pageRef, filePageId, swapper );
pinEvent.hit();
return;
}
if ( locked )
{
unlockPage( pageRef );
}
}
else
{
if ( uncommonPin( filePageId, chunkOffset, chunk ) )
{
return;
}
}
}
}
private int[][] expandTranslationTableCapacity( int chunkId )
{
return pagedFile.expandCapacity( chunkId );
}
private boolean uncommonPin( long filePageId, long chunkOffset, int[] chunk ) throws IOException
{
// Looks like there's no mapping, so we'd like to do a page fault.
LatchMap.Latch latch = pagedFile.pageFaultLatches.takeOrAwaitLatch( filePageId );
if ( latch != null )
{
// We managed to inject our latch, so we now own the right to perform the page fault. We also
// have a duty to eventually release and remove the latch, no matter what happens now.
// However, we first have to double-check that a page fault did not complete in-between our initial
// check in the translation table, and us getting a latch.
if ( UnsafeUtil.getIntVolatile( chunk, chunkOffset ) == UNMAPPED_TTE )
{
// Sweet, we didn't race with any other fault on this translation table entry.
long pageRef = pageFault( filePageId, swapper, chunkOffset, chunk, latch );
pinCursorToPage( pageRef, filePageId, swapper );
return true;
}
// Oops, looks like we raced with another page fault on this file page.
// Let's release our latch and retry the pin.
latch.release();
}
// We found a latch, so someone else is already doing a page fault for this page.
// The `takeOrAwaitLatch` already waited for this latch to be released on our behalf,
// so now we just have to do another iteration of the loop to see what's in the translation table now.
return false;
}
private long pageFault(
long filePageId, PageSwapper swapper, long chunkOffset, int[] chunk, LatchMap.Latch latch )
throws IOException
{
// We are page faulting. This is a critical time, because we currently have the given latch in the chunk array
// slot that we are faulting into. We MUST make sure to release that latch, and remove it from the chunk, no
// matter what happens. Otherwise other threads will get stuck waiting forever for our page fault to finish.
// If we manage to get a free page to fault into, then we will also be taking a write lock on that page, to
// protect it against concurrent eviction as we assigning a binding to the page. If anything goes wrong, then
// we must make sure to release that write lock as well.
PageFaultEvent faultEvent = pinEvent.beginPageFault();
long pageRef;
try
{
// The grabFreePage method might throw.
pageRef = pagedFile.grabFreeAndExclusivelyLockedPage( faultEvent );
// We got a free page, and we know that we have race-free access to it. Well, it's not entirely race
// free, because other paged files might have it in their translation tables (or rather, their reads of
// their translation tables might race with eviction) and try to pin it.
// However, they will all fail because when they try to pin, because the page will be exclusively locked
// and possibly bound to our page.
}
catch ( Throwable throwable )
{
// Make sure to unstuck the page fault latch.
abortPageFault( throwable, chunk, chunkOffset, latch, faultEvent );
throw throwable;
}
try
{
// Check if we're racing with unmapping. We have the page lock
// here, so the unmapping would have already happened. We do this
// check before page.fault(), because that would otherwise reopen
// the file channel.
assertPagedFileStillMappedAndGetIdOfLastPage();
pagedFile.initBuffer( pageRef );
pagedFile.fault( pageRef, swapper, pagedFile.swapperId, filePageId, faultEvent );
}
catch ( Throwable throwable )
{
// Make sure to unlock the page, so the eviction thread can pick up our trash.
pagedFile.unlockExclusive( pageRef );
// Make sure to unstuck the page fault latch.
abortPageFault( throwable, chunk, chunkOffset, latch, faultEvent );
throw throwable;
}
// Put the page in the translation table before we undo the exclusive lock, as we could otherwise race with
// eviction, and the onEvict callback expects to find a MuninnPage object in the table.
UnsafeUtil.putIntVolatile( chunk, chunkOffset, pagedFile.toId( pageRef ) );
// Once we page has been published to the translation table, we can convert our exclusive lock to whatever we
// need for the page cursor.
convertPageFaultLock( pageRef );
latch.release();
faultEvent.done();
return pageRef;
}
private void abortPageFault( Throwable throwable, int[] chunk, long chunkOffset,
LatchMap.Latch latch,
PageFaultEvent faultEvent ) throws IOException
{
UnsafeUtil.putIntVolatile( chunk, chunkOffset, UNMAPPED_TTE );
latch.release();
faultEvent.done( throwable );
pinEvent.done();
}
long assertPagedFileStillMappedAndGetIdOfLastPage()
{
return pagedFile.getLastPageId();
}
protected abstract void unpinCurrentPage();
protected abstract void convertPageFaultLock( long pageRef );
protected abstract void pinCursorToPage( long pageRef, long filePageId, PageSwapper swapper );
protected abstract boolean tryLockPage( long pageRef );
protected abstract void unlockPage( long pageRef );
protected abstract void releaseCursor();
// --- IO methods:
/**
* Compute a pointer that guarantees (assuming {@code size} is less than or equal to {@link #pageSize}) that the
* page access will be within the bounds of the page.
* This might mean that the pointer won't point to where one might naively expect, but will instead be
* truncated to point within the page. In this case, an overflow has happened and the {@link #outOfBounds}
* flag will be raised.
*/
private long getBoundedPointer( int offset, int size )
{
long p = pointer;
long can = p + offset;
if ( boundsCheck )
{
if ( can + size > p + pageSize || can < p )
{
outOfBounds = true;
// Return the victim page when we are out of bounds, since at this point we can't tell if the pointer
// will be used for reading or writing.
return victimPage;
}
}
return can;
}
/**
* Compute a pointer that guarantees (assuming {@code size} is less than or equal to {@link #pageSize}) that the
* page access will be within the bounds of the page.
* This works just like {@link #getBoundedPointer(int, int)}, except in terms of the current {@link #offset}.
* This version is faster when applicable, because it can ignore the <em>page underflow</em> case.
*/
private long nextBoundedPointer( int size )
{
int offset = this.offset;
long can = pointer + offset;
if ( boundsCheck )
{
if ( offset + size > pageSize )
{
outOfBounds = true;
// Return the victim page when we are out of bounds, since at this point we can't tell if the pointer
// will be used for reading or writing.
return victimPage;
}
}
return can;
}
@Override
public final byte getByte()
{
long p = nextBoundedPointer( SIZE_OF_BYTE );
byte b = UnsafeUtil.getByte( p );
offset++;
return b;
}
@Override
public byte getByte( int offset )
{
long p = getBoundedPointer( offset, SIZE_OF_BYTE );
return UnsafeUtil.getByte( p );
}
@Override
public void putByte( byte value )
{
long p = nextBoundedPointer( SIZE_OF_BYTE );
UnsafeUtil.putByte( p, value );
offset++;
}
@Override
public void putByte( int offset, byte value )
{
long p = getBoundedPointer( offset, SIZE_OF_BYTE );
UnsafeUtil.putByte( p, value );
}
@Override
public long getLong()
{
long p = nextBoundedPointer( SIZE_OF_LONG );
long value = getLongAt( p );
offset += SIZE_OF_LONG;
return value;
}
@Override
public long getLong( int offset )
{
long p = getBoundedPointer( offset, SIZE_OF_LONG );
return getLongAt( p );
}
private long getLongAt( long p )
{
long value;
if ( UnsafeUtil.allowUnalignedMemoryAccess )
{
value = UnsafeUtil.getLong( p );
if ( !UnsafeUtil.storeByteOrderIsNative )
{
value = Long.reverseBytes( value );
}
}
else
{
value = getLongBigEndian( p );
}
return value;
}
private long getLongBigEndian( long p )
{
long a = UnsafeUtil.getByte( p ) & 0xFF;
long b = UnsafeUtil.getByte( p + 1 ) & 0xFF;
long c = UnsafeUtil.getByte( p + 2 ) & 0xFF;
long d = UnsafeUtil.getByte( p + 3 ) & 0xFF;
long e = UnsafeUtil.getByte( p + 4 ) & 0xFF;
long f = UnsafeUtil.getByte( p + 5 ) & 0xFF;
long g = UnsafeUtil.getByte( p + 6 ) & 0xFF;
long h = UnsafeUtil.getByte( p + 7 ) & 0xFF;
return (a << 56) | (b << 48) | (c << 40) | (d << 32) | (e << 24) | (f << 16) | (g << 8) | h;
}
@Override
public void putLong( long value )
{
long p = nextBoundedPointer( SIZE_OF_LONG );
putLongAt( p, value );
offset += SIZE_OF_LONG;
}
@Override
public void putLong( int offset, long value )
{
long p = getBoundedPointer( offset, SIZE_OF_LONG );
putLongAt( p, value );
}
private void putLongAt( long p, long value )
{
if ( UnsafeUtil.allowUnalignedMemoryAccess )
{
UnsafeUtil.putLong( p, UnsafeUtil.storeByteOrderIsNative ? value : Long.reverseBytes( value ) );
}
else
{
putLongBigEndian( value, p );
}
}
private void putLongBigEndian( long value, long p )
{
UnsafeUtil.putByte( p , (byte)( value >> 56 ) );
UnsafeUtil.putByte( p + 1, (byte)( value >> 48 ) );
UnsafeUtil.putByte( p + 2, (byte)( value >> 40 ) );
UnsafeUtil.putByte( p + 3, (byte)( value >> 32 ) );
UnsafeUtil.putByte( p + 4, (byte)( value >> 24 ) );
UnsafeUtil.putByte( p + 5, (byte)( value >> 16 ) );
UnsafeUtil.putByte( p + 6, (byte)( value >> 8 ) );
UnsafeUtil.putByte( p + 7, (byte) value );
}
@Override
public int getInt()
{
long p = nextBoundedPointer( SIZE_OF_INT );
int i = getIntAt( p );
offset += SIZE_OF_INT;
return i;
}
@Override
public int getInt( int offset )
{
long p = getBoundedPointer( offset, SIZE_OF_INT );
return getIntAt( p );
}
private int getIntAt( long p )
{
if ( UnsafeUtil.allowUnalignedMemoryAccess )
{
int x = UnsafeUtil.getInt( p );
return UnsafeUtil.storeByteOrderIsNative ? x : Integer.reverseBytes( x );
}
return getIntBigEndian( p );
}
private int getIntBigEndian( long p )
{
int a = UnsafeUtil.getByte( p ) & 0xFF;
int b = UnsafeUtil.getByte( p + 1 ) & 0xFF;
int c = UnsafeUtil.getByte( p + 2 ) & 0xFF;
int d = UnsafeUtil.getByte( p + 3 ) & 0xFF;
return (a << 24) | (b << 16) | (c << 8) | d;
}
@Override
public void putInt( int value )
{
long p = nextBoundedPointer( SIZE_OF_INT );
putIntAt( p, value );
offset += SIZE_OF_INT;
}
@Override
public void putInt( int offset, int value )
{
long p = getBoundedPointer( offset, SIZE_OF_INT );
putIntAt( p, value );
}
private void putIntAt( long p, int value )
{
if ( UnsafeUtil.allowUnalignedMemoryAccess )
{
UnsafeUtil.putInt( p, UnsafeUtil.storeByteOrderIsNative ? value : Integer.reverseBytes( value ) );
}
else
{
putIntBigEndian( value, p );
}
}
private void putIntBigEndian( int value, long p )
{
UnsafeUtil.putByte( p , (byte)( value >> 24 ) );
UnsafeUtil.putByte( p + 1, (byte)( value >> 16 ) );
UnsafeUtil.putByte( p + 2, (byte)( value >> 8 ) );
UnsafeUtil.putByte( p + 3, (byte) value );
}
@Override
public void getBytes( byte[] data )
{
getBytes( data, 0, data.length );
}
@Override
public void getBytes( byte[] data, int arrayOffset, int length )
{
long p = getBoundedPointer( offset, length );
if ( !outOfBounds )
{
for ( int i = 0; i < length; i++ )
{
data[arrayOffset + i] = UnsafeUtil.getByte( p + i );
}
}
offset += length;
}
@Override
public final void putBytes( byte[] data )
{
putBytes( data, 0, data.length );
}
@Override
public void putBytes( byte[] data, int arrayOffset, int length )
{
long p = getBoundedPointer( offset, length );
if ( !outOfBounds )
{
for ( int i = 0; i < length; i++ )
{
byte b = data[arrayOffset + i];
UnsafeUtil.putByte( p + i, b );
}
}
offset += length;
}
@Override
public final short getShort()
{
long p = nextBoundedPointer( SIZE_OF_SHORT );
short s = getShortAt( p );
offset += SIZE_OF_SHORT;
return s;
}
@Override
public short getShort( int offset )
{
long p = getBoundedPointer( offset, SIZE_OF_SHORT );
return getShortAt( p );
}
private short getShortAt( long p )
{
if ( UnsafeUtil.allowUnalignedMemoryAccess )
{
short x = UnsafeUtil.getShort( p );
return UnsafeUtil.storeByteOrderIsNative ? x : Short.reverseBytes( x );
}
return getShortBigEndian( p );
}
private short getShortBigEndian( long p )
{
short a = (short) (UnsafeUtil.getByte( p ) & 0xFF);
short b = (short) (UnsafeUtil.getByte( p + 1 ) & 0xFF);
return (short) ((a << 8) | b);
}
@Override
public void putShort( short value )
{
long p = nextBoundedPointer( SIZE_OF_SHORT );
putShortAt( p, value );
offset += SIZE_OF_SHORT;
}
@Override
public void putShort( int offset, short value )
{
long p = getBoundedPointer( offset, SIZE_OF_SHORT );
putShortAt( p, value );
}
private void putShortAt( long p, short value )
{
if ( UnsafeUtil.allowUnalignedMemoryAccess )
{
UnsafeUtil.putShort( p, UnsafeUtil.storeByteOrderIsNative ? value : Short.reverseBytes( value ) );
}
else
{
putShortBigEndian( value, p );
}
}
private void putShortBigEndian( short value, long p )
{
UnsafeUtil.putByte( p , (byte)( value >> 8 ) );
UnsafeUtil.putByte( p + 1, (byte) value );
}
@Override
public int copyTo( int sourceOffset, PageCursor targetCursor, int targetOffset, int lengthInBytes )
{
int sourcePageSize = getCurrentPageSize();
int targetPageSize = targetCursor.getCurrentPageSize();
if ( targetCursor.getClass() != MuninnWritePageCursor.class )
{
throw new IllegalArgumentException( "Target cursor must be writable" );
}
if ( sourceOffset >= 0
& targetOffset >= 0
& sourceOffset < sourcePageSize
& targetOffset < targetPageSize
& lengthInBytes >= 0 )
{
MuninnPageCursor cursor = (MuninnPageCursor) targetCursor;
int remainingSource = sourcePageSize - sourceOffset;
int remainingTarget = targetPageSize - targetOffset;
int bytes = Math.min( lengthInBytes, Math.min( remainingSource, remainingTarget ) );
UnsafeUtil.copyMemory( pointer + sourceOffset, cursor.pointer + targetOffset, bytes );
return bytes;
}
outOfBounds = true;
return 0;
}
@Override
public void setOffset( int offset )
{
this.offset = offset;
if ( offset < 0 | offset > filePageSize )
{
this.offset = 0;
outOfBounds = true;
}
}
@Override
public final int getOffset()
{
return offset;
}
@Override
public boolean checkAndClearBoundsFlag()
{
MuninnPageCursor cursor = this;
boolean result = false;
do
{
result |= cursor.outOfBounds;
cursor.outOfBounds = false;
cursor = cursor.linkedCursor;
}
while ( cursor != null );
return result;
}
@Override
public void checkAndClearCursorException() throws CursorException
{
MuninnPageCursor cursor = this;
do
{
Object error = cursor.cursorException;
if ( error != null )
{
clearCursorError( cursor );
if ( usePreciseCursorErrorStackTraces )
{
throw (CursorExceptionWithPreciseStackTrace) error;
}
else
{
throw new CursorException( (String) error );
}
}
cursor = cursor.linkedCursor;
}
while ( cursor != null );
}
@Override
public void clearCursorException()
{
clearCursorError( this );
}
private void clearCursorError( MuninnPageCursor cursor )
{
while ( cursor != null )
{
cursor.cursorException = null;
cursor = cursor.linkedCursor;
}
}
@Override
public void raiseOutOfBounds()
{
outOfBounds = true;
}
@Override
public void setCursorException( String message )
{
Objects.requireNonNull( message );
if ( usePreciseCursorErrorStackTraces )
{
this.cursorException = new CursorExceptionWithPreciseStackTrace( message );
}
else
{
this.cursorException = message;
}
}
@Override
public void zapPage()
{
if ( pageSize == 0 )
{
// if this page has been closed then pageSize == 0 and we must adhere to making writes
// trigger outOfBounds when closed
outOfBounds = true;
}
else
{
UnsafeUtil.setMemory( pointer, pageSize, (byte) 0 );
}
}
@Override
public boolean isWriteLocked()
{
return (pf_flags & PF_SHARED_WRITE_LOCK) == PF_SHARED_WRITE_LOCK;
}
}