public abstract class PageCursor extends Object implements AutoCloseable
PagedFile.io(long, int),
and is used to scan through pages and process them in a consistent and safe fashion.
A page must be processed in the following manner:
try ( PageCursor cursor = pagedFile.io( pageId, pf_flags ) )
{
// Use 'if' for processing a single page,
// use 'while' for scanning through pages:
if ( cursor.next() )
{
do
{
processPage( cursor );
} while ( cursor.shouldRetry() );
// Any finalising, non-repeatable post-processing
// goes here.
}
}
catch ( IOException e )
{
// handle the error, somehow
}
There are a couple of things to this pattern that are worth noting:
next(long) method, to navigate the
pages you need in a non-linear fashion.| Modifier and Type | Field and Description |
|---|---|
static long |
UNBOUND_PAGE_ID |
static int |
UNBOUND_PAGE_SIZE |
| Constructor and Description |
|---|
PageCursor() |
| Modifier and Type | Method and Description |
|---|---|
abstract boolean |
checkAndClearBoundsFlag()
Discern whether an out-of-bounds access has occurred since the last call to
next() or
next(long), or since the last call to shouldRetry() that returned true, or since the
last call to this method. |
abstract void |
checkAndClearCursorException()
Check if a cursor error has been set on this or any linked cursor, and if so, remove it from the cursor
and throw it as a
CursorException. |
abstract void |
clearCursorException()
Unconditionally clear any error condition that has been set on this or any linked cursor, without throwing an
exception.
|
abstract void |
close()
Relinquishes all resources associated with this cursor, including the
cursor itself, and any linked cursors opened through it.
|
abstract int |
copyTo(int sourceOffset,
PageCursor targetCursor,
int targetOffset,
int lengthInBytes)
Copy the specified number of bytes from the given offset of this page, to the given offset of the target page.
|
abstract byte |
getByte()
Get the signed byte at the current page offset, and then increment the offset by one.
|
abstract byte |
getByte(int offset)
Get the signed byte at the given offset into the page.
|
abstract void |
getBytes(byte[] data)
Fill the given array with bytes from the page, beginning at the current offset into the page,
and then increment the current offset by the length of the array.
|
abstract void |
getBytes(byte[] data,
int arrayOffset,
int length)
Read the given length of bytes from the page into the given array, starting from the current offset into the
page, and writing from the given array offset, and then increment the current offset by the length argument.
|
abstract File |
getCurrentFile()
Get the file the cursor is currently bound to, or
null if next() has not yet been called on this
cursor, or returned false. |
abstract long |
getCurrentPageId()
Get the file page id that the cursor is currently positioned at, or
UNBOUND_PAGE_ID if next() has not yet been called on this cursor, or returned false.
|
abstract int |
getCurrentPageSize()
Get the file page size of the page that the cursor is currently positioned at,
or UNBOUND_PAGE_SIZE if next() has not yet been called on this cursor, or returned false.
|
abstract int |
getInt()
Get the signed int at the current page offset, and then increment the offset by one.
|
abstract int |
getInt(int offset)
Get the signed int at the given offset into the page.
|
abstract long |
getLong()
Get the signed long at the current page offset, and then increment the offset by one.
|
abstract long |
getLong(int offset)
Get the signed long at the given offset into the page.
|
abstract int |
getOffset()
Get the current offset into the page, which is the location on the page where the next interaction would take
place through the read and write methods that do not take a specific offset as an argument.
|
abstract short |
getShort()
Get the signed short at the current page offset, and then increment the offset by one.
|
abstract short |
getShort(int offset)
Get the signed short at the given offset into the page.
|
abstract boolean |
next()
Moves the cursor to the next page, if any, and returns true when it is
ready to be processed.
|
abstract boolean |
next(long pageId)
Moves the cursor to the page specified by the given pageId, if any,
and returns true when it is ready to be processed.
|
abstract PageCursor |
openLinkedCursor(long pageId)
Open a new page cursor with the same pf_flags as this cursor, as if calling the
PagedFile.io(long, int)
on the relevant paged file. |
abstract void |
putByte(byte value)
Set the signed byte at the current offset into the page, and then increment the offset by one.
|
abstract void |
putByte(int offset,
byte value)
Set the signed byte at the given offset into the page.
|
abstract void |
putBytes(byte[] data)
Write out all the bytes of the given array into the page, beginning at the current offset into the page,
and then increment the current offset by the length of the array.
|
abstract void |
putBytes(byte[] data,
int arrayOffset,
int length)
Write out the given length of bytes from the given offset into the the given array of bytes, into the page,
beginning at the current offset into the page, and then increment the current offset by the length argument.
|
abstract void |
putInt(int value)
Set the signed int at the current offset into the page, and then increment the offset by one.
|
abstract void |
putInt(int offset,
int value)
Set the signed int at the given offset into the page.
|
abstract void |
putLong(int offset,
long value)
Set the signed long at the given offset into the page.
|
abstract void |
putLong(long value)
Set the signed long at the current offset into the page, and then increment the offset by one.
|
abstract void |
putShort(int offset,
short value)
Set the signed short at the given offset into the page.
|
abstract void |
putShort(short value)
Set the signed short at the current offset into the page, and then increment the offset by one.
|
abstract void |
raiseOutOfBounds()
Explicitly raise the out-of-bounds flag.
|
abstract void |
rewind()
Rewinds the cursor to its initial condition, as if freshly returned from
an equivalent io() call.
|
abstract void |
setCursorException(String message)
Set an error condition on the cursor with the given message.
|
abstract void |
setOffset(int offset)
Set the current offset into the page, for interacting with the page through the read and write methods that do
not take a specific offset as an argument.
|
abstract boolean |
shouldRetry()
Returns true if the page has entered an inconsistent state since the
last call to next() or shouldRetry().
|
public static final long UNBOUND_PAGE_ID
public static final int UNBOUND_PAGE_SIZE
public abstract byte getByte()
public abstract byte getByte(int offset)
public abstract void putByte(byte value)
public abstract void putByte(int offset,
byte value)
public abstract long getLong()
public abstract long getLong(int offset)
public abstract void putLong(long value)
public abstract void putLong(int offset,
long value)
public abstract int getInt()
public abstract int getInt(int offset)
public abstract void putInt(int value)
public abstract void putInt(int offset,
int value)
public abstract void getBytes(byte[] data)
public abstract void getBytes(byte[] data,
int arrayOffset,
int length)
public abstract void putBytes(byte[] data)
public abstract void putBytes(byte[] data,
int arrayOffset,
int length)
public abstract short getShort()
public abstract short getShort(int offset)
public abstract void putShort(short value)
public abstract void putShort(int offset,
short value)
public abstract void setOffset(int offset)
public abstract int getOffset()
public abstract long getCurrentPageId()
public abstract int getCurrentPageSize()
public abstract File getCurrentFile()
null if next() has not yet been called on this
cursor, or returned false.
A call to rewind() will make the cursor unbound as well, until next() is called.public abstract void rewind()
public abstract boolean next()
throws IOException
NOTE: When using read locks, read operations can be inconsistent
and may return completely random data. The data returned from a
read-locked page cursor should not be interpreted until after
shouldRetry() has returned false.
Not interpreting the data also implies that you cannot throw exceptions
from data validation errors until after shouldRetry() has told
you that your read was consistent.
IOExceptionpublic abstract boolean next(long pageId)
throws IOException
NOTE: When using read locks, read operations can be inconsistent
and may return completely random data. The data returned from a
read-locked page cursor should not be interpreted until after
shouldRetry() has returned false.
Not interpreting the data also implies that you cannot throw exceptions
from data validation errors until after shouldRetry() has told
you that your read was consistent.
IOExceptionpublic abstract void close()
close in interface AutoCloseableAutoCloseable.close()public abstract boolean shouldRetry()
throws IOException
IOException - If the page was evicted while doing IO, the cursor will have
to do a page fault to get the page back.
This may throw an IOException.public abstract int copyTo(int sourceOffset,
PageCursor targetCursor,
int targetOffset,
int lengthInBytes)
If the length reaches beyond the end of either cursor, then only as many bytes as are available in this cursor, or can fit in the target cursor, are actually copied.
sourceOffset - The offset into this page to copy from.targetCursor - The cursor the data will be copied to.targetOffset - The offset into the target cursor to copy to.lengthInBytes - The number of bytes to copy.public abstract boolean checkAndClearBoundsFlag()
next() or
next(long), or since the last call to shouldRetry() that returned true, or since the
last call to this method.true if an access was out of bounds, or the raiseOutOfBounds() method has been called.public abstract void checkAndClearCursorException()
throws CursorException
CursorException.CursorExceptionpublic abstract void raiseOutOfBounds()
checkAndClearBoundsFlag()public abstract void setCursorException(String message)
This will make calls to checkAndClearCursorException() throw a CursorException with the given
message, unless the error has gotten cleared by a shouldRetry() call that returned true,
a call to next() or next(long), or the cursor is closed.
message - The message of the CursorException that checkAndClearCursorException() will throw.public abstract void clearCursorException()
public abstract PageCursor openLinkedCursor(long pageId)
PagedFile.io(long, int)
on the relevant paged file. This cursor will then also delegate to the linked cursor when checking
shouldRetry() and checkAndClearBoundsFlag().
Opening a linked cursor on a cursor that already has a linked cursor, will close the older linked cursor. Closing a cursor also closes any linked cursor.
pageId - The page id that the linked cursor will be placed at after its first call to next().Copyright © 2002–2017 The Neo4j Graph Database Project. All rights reserved.