Class PageNavigator
java.lang.Object
org.aksw.commons.util.closeable.AutoCloseableBase
org.aksw.commons.util.closeable.AutoCloseableWithLeakDetectionBase
org.aksw.commons.io.block.impl.PageNavigator
- All Implemented Interfaces:
Closeable, AutoCloseable, Channel, ReadableByteChannel, Seekable
An object for (predominantly relative) positioning over a
sequence of fixed size pages
- Author:
- raven
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intprotected intprotected longprotected intprotected intIndex is relative to the the page's buffer.position()protected intprotected longprotected longprotected intprotected longprotected longprotected longCurrent pageprotected ByteBufferprotected PageManagerprotected intprotected intprotected intFields inherited from class AutoCloseableWithLeakDetectionBase
instantiationStackTraceFields inherited from class AutoCloseableBase
closeStackTrace, enableCloseStackTrace, isClosed -
Constructor Summary
ConstructorsConstructorDescriptionPageNavigator(PageManager pageManager) PageNavigator(PageManager pageManager, long minPos, long maxPos) -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanintcheckNext(int len, boolean changePos) Attempt to advance the position by the given number of bytes.intcheckPrev(int len, boolean changePos) clone()Clone the state of the navigator, allowing independent positioningvoidintcompareToPrefix(byte[] prefix) Compare the bytes at the current position to a given sequence of bytes If there are fewer bytes available in the seekable than provide for comparison, then only that many are compared.byteget()Get the byte at the current position Does not change the position Throws an exception if beyond end of datagetBufferForPage(long page) If a page with the given offset exists, the following fields are updated: - displacement - absMinIndexInPage - absMaxIndexInPage Note, that the relative caches are not updatedgetBufferForPos(long pos) intgetIndexForPos(long pos) longlonglonggetNextPosFor(byte delimiter) Returns the position of the next delimiter or the end of the streamlonggetPageForPos(long pos) longgetPos()Optional operation.longgetPrevPosFor(byte delimiter) intgetRelMaxIndex(long page) intgetRelMinIndex(long page) booleanisOpen()booleanThe state of a seekable may be one unit beyond the end.booleanThe state of a seekable may be one unit before the start.limitNext(long length) Restrict the maximum number of items that can be read to length items from the current position.limitPrev(long length) Limit the maximum position by length bytes from the current position.booleannextPos(int delta) Attempts to advance the position by delta bytes and returns true if this succeeded.intpeekNextBytes(byte[] dst, int offset, int len) Reads bytes at the current position; does not advance posvoidposToEnd()Optional operation.booleanposToNext(byte delimiter) Advances the position to the next matching delimiter or one byte past the end of the stream.booleanposToPrev(byte delimiter) Move the position to the previous delimiter if it exists, or one element past the end of data such that isPosBeforeStart() yields true.voidOptional operation.booleanprevPos(int delta) Attempt to step back the position by the given number of bytes.intread(ByteBuffer dst) readLine()static intreadRemaining(ByteBuffer dst, ByteBuffer src) readString(int n) voidsetPos(long pos) Optional operation.voidsetPos(long page, int index) voidupdateRelCache(long page) Update relative min and max index for the given page, taking one byte before and after the selected range into accountMethods inherited from class AutoCloseableWithLeakDetectionBase
finalize, getInstantiationStackTraceMethods inherited from class AutoCloseableBase
close, ensureOpen, throwClosedExceptionMethods inherited from class Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Seekable
binarySearch, cloneObject, close, deltaPos, get, posToNext, size
-
Field Details
-
pageManager
-
pageSize
protected int pageSize -
page
protected long pageCurrent page -
index
protected int indexIndex is relative to the the page's buffer.position() -
pageObj
-
pageBuffer
-
displacement
protected int displacement -
bufferForPage
protected long bufferForPage -
absMaxIndexInPage
protected int absMaxIndexInPage -
absMinIndexInPage
protected int absMinIndexInPage -
relMinIndexInPage
protected int relMinIndexInPage -
relMaxIndexInPage
protected int relMaxIndexInPage -
minPos
protected long minPos -
minPage
protected long minPage -
minIndex
protected int minIndex -
maxPos
protected long maxPos -
maxPage
protected long maxPage -
maxIndex
protected int maxIndex
-
-
Constructor Details
-
PageNavigator
-
PageNavigator
- Parameters:
pageManager-minPos- inclusivemaxPos- exclusive
-
-
Method Details
-
clone
Clone the state of the navigator, allowing independent positioning -
limitNext
Restrict the maximum number of items that can be read to length items from the current position.- Parameters:
length-- Returns:
-
limitPrev
Limit the maximum position by length bytes from the current position.- Parameters:
length-- Returns:
-
getPos
-
isPosAfterEnd
public boolean isPosAfterEnd()Description copied from interface:SeekableThe state of a seekable may be one unit beyond the end. In this state, if the seekable is non-empty, prevPos(1) must be a valid position- Specified by:
isPosAfterEndin interfaceSeekable- Returns:
-
isPosBeforeStart
public boolean isPosBeforeStart()Description copied from interface:SeekableThe state of a seekable may be one unit before the start. In this state, if the seekable is non-empty, nextPos(1) must be a valid position- Specified by:
isPosBeforeStartin interfaceSeekable- Returns:
-
getBufferForPage
If a page with the given offset exists, the following fields are updated: - displacement - absMinIndexInPage - absMaxIndexInPage Note, that the relative caches are not updated- Parameters:
page-- Returns:
- Throws:
IOException
-
updateRelCache
public void updateRelCache(long page) Update relative min and max index for the given page, taking one byte before and after the selected range into account- Parameters:
page-
-
getRelMaxIndex
public int getRelMaxIndex(long page) -
getRelMinIndex
public int getRelMinIndex(long page) -
getBufferForPos
- Throws:
IOException
-
getPageForPos
public long getPageForPos(long pos) -
getIndexForPos
public int getIndexForPos(long pos) -
posToStart
public void posToStart()Description copied from interface:SeekableOptional operation. Move one unit before the start of the seekable; raises an exception on infinite seekables- Specified by:
posToStartin interfaceSeekable
-
posToEnd
-
setPos
public void setPos(long page, int index) -
setPos
-
getMinPos
public long getMinPos() -
getMaxPos
public long getMaxPos() -
get
Get the byte at the current position Does not change the position Throws an exception if beyond end of data- Specified by:
getin interfaceSeekable- Returns:
- Throws:
IOException
-
canNextPos
public boolean canNextPos() -
canPrevPos
public boolean canPrevPos() -
checkNext
Description copied from interface:SeekableAttempt to advance the position by the given number of bytes. Return the number of bytes by which the position was changed. Returning less bytes than requested implies that a end position was reached which cannot be passed. This method cannot pass beyond the end - i.e. isPosAfterEnd cannot change from false to true by calling this method.- Specified by:
checkNextin interfaceSeekable- Parameters:
len-- Returns:
- Throws:
IOException
-
checkPrev
- Specified by:
checkPrevin interfaceSeekable- Throws:
IOException
-
nextPos
Attempts to advance the position by delta bytes and returns true if this succeeded. Position is unchanged if insufficient bytes are available Cannot advance beyond the last byte.- Specified by:
nextPosin interfaceSeekable- Parameters:
delta-- Returns:
- Throws:
IOException
-
prevPos
Description copied from interface:SeekableAttempt to step back the position by the given number of bytes. Argument must not be negative.- Specified by:
prevPosin interfaceSeekable- Parameters:
delta-- Returns:
- True if the position was changed by the *requested* amount of bytes. False means that the position was unchanged.
- Throws:
IOException
-
posToNext
Advances the position to the next matching delimiter or one byte past the end of the stream. Returns true if and only if the position was changed. Note, true does NOT mean that the byte at the new position matches delim. A match is implied if isPosAfterEnd() is false.- Specified by:
posToNextin interfaceSeekable- Parameters:
delimiter-- Returns:
- Throws:
IOException
-
posToPrev
Description copied from interface:SeekableMove the position to the previous delimiter if it exists, or one element past the end of data such that isPosBeforeStart() yields true. Position is unchanged if already at delimiter- Specified by:
posToPrevin interfaceSeekable- Parameters:
delimiter-- Returns:
- true if the position was changed, false otherwise
- Throws:
IOException
-
getNextPosFor
Returns the position of the next delimiter or the end of the stream- Parameters:
delimiter-- Returns:
- Throws:
IOException
-
getPrevPosFor
- Throws:
IOException
-
compareToPrefix
Description copied from interface:SeekableCompare the bytes at the current position to a given sequence of bytes If there are fewer bytes available in the seekable than provide for comparison, then only that many are compared. This default implementation uses read(ByteBuffer). Other implementations may override this behavior to compare the given prefix directly against their internal data structures without the intermediate buffer copy due to read.- Specified by:
compareToPrefixin interfaceSeekable- Parameters:
prefix-- Returns:
- Throws:
IOException
-
peekNextBytes
Reads bytes at the current position; does not advance pos- Specified by:
peekNextBytesin interfaceSeekable- Parameters:
dst-offset-len-n-- Returns:
- Throws:
IOException
-
readLine
- Throws:
IOException
-
readString
- Specified by:
readStringin interfaceSeekable- Throws:
IOException
-
read
- Specified by:
readin interfaceReadableByteChannel- Specified by:
readin interfaceSeekable- Throws:
IOException
-
readRemaining
-
isOpen
-
closeActual
- Overrides:
closeActualin classAutoCloseableBase- Throws:
Exception
-