Class PageNavigator

All Implemented Interfaces:
Closeable, AutoCloseable, Channel, ReadableByteChannel, Seekable

public class PageNavigator extends AutoCloseableWithLeakDetectionBase implements Seekable
An object for (predominantly relative) positioning over a sequence of fixed size pages
Author:
raven
  • Field Details

    • pageManager

      protected PageManager pageManager
    • pageSize

      protected int pageSize
    • page

      protected long page
      Current page
    • index

      protected int index
      Index is relative to the the page's buffer.position()
    • pageObj

      protected Ref<? extends Page> pageObj
    • pageBuffer

      protected ByteBuffer 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

      public PageNavigator(PageManager pageManager)
    • PageNavigator

      public PageNavigator(PageManager pageManager, long minPos, long maxPos)
      Parameters:
      pageManager -
      minPos - inclusive
      maxPos - exclusive
  • Method Details

    • clone

      public PageNavigator clone()
      Clone the state of the navigator, allowing independent positioning
      Specified by:
      clone in interface Seekable
      Overrides:
      clone in class Object
    • limitNext

      public PageNavigator limitNext(long length)
      Restrict the maximum number of items that can be read to length items from the current position.
      Parameters:
      length -
      Returns:
    • limitPrev

      public PageNavigator limitPrev(long length)
      Limit the maximum position by length bytes from the current position.
      Parameters:
      length -
      Returns:
    • getPos

      public long getPos()
      Description copied from interface: Seekable
      Optional operation. Get the position in this seekable
      Specified by:
      getPos in interface Seekable
      Returns:
    • isPosAfterEnd

      public boolean isPosAfterEnd()
      Description copied from interface: Seekable
      The 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:
      isPosAfterEnd in interface Seekable
      Returns:
    • isPosBeforeStart

      public boolean isPosBeforeStart()
      Description copied from interface: Seekable
      The 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:
      isPosBeforeStart in interface Seekable
      Returns:
    • getBufferForPage

      public ByteBuffer getBufferForPage(long page) throws IOException
      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

      public ByteBuffer getBufferForPos(long pos) throws IOException
      Throws:
      IOException
    • getPageForPos

      public long getPageForPos(long pos)
    • getIndexForPos

      public int getIndexForPos(long pos)
    • posToStart

      public void posToStart()
      Description copied from interface: Seekable
      Optional operation. Move one unit before the start of the seekable; raises an exception on infinite seekables
      Specified by:
      posToStart in interface Seekable
    • posToEnd

      public void posToEnd()
      Description copied from interface: Seekable
      Optional operation. Move to one unit beyond the end of the seekable; raises an exception on infinite seekables
      Specified by:
      posToEnd in interface Seekable
    • setPos

      public void setPos(long page, int index)
    • setPos

      public void setPos(long pos)
      Description copied from interface: Seekable
      Optional operation. Get the position in this seekable
      Specified by:
      setPos in interface Seekable
    • getMinPos

      public long getMinPos()
    • getMaxPos

      public long getMaxPos()
    • get

      public byte get() throws IOException
      Get the byte at the current position Does not change the position Throws an exception if beyond end of data
      Specified by:
      get in interface Seekable
      Returns:
      Throws:
      IOException
    • canNextPos

      public boolean canNextPos()
    • canPrevPos

      public boolean canPrevPos()
    • checkNext

      public int checkNext(int len, boolean changePos) throws IOException
      Description copied from interface: Seekable
      Attempt 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:
      checkNext in interface Seekable
      Returns:
      Throws:
      IOException
    • checkPrev

      public int checkPrev(int len, boolean changePos) throws IOException
      Specified by:
      checkPrev in interface Seekable
      Throws:
      IOException
    • nextPos

      public boolean nextPos(int delta) throws IOException
      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:
      nextPos in interface Seekable
      Returns:
      Throws:
      IOException
    • prevPos

      public boolean prevPos(int delta) throws IOException
      Description copied from interface: Seekable
      Attempt to step back the position by the given number of bytes. Argument must not be negative.
      Specified by:
      prevPos in interface Seekable
      Returns:
      True if the position was changed by the *requested* amount of bytes. False means that the position was unchanged.
      Throws:
      IOException
    • posToNext

      public boolean posToNext(byte delimiter) throws IOException
      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:
      posToNext in interface Seekable
      Parameters:
      delimiter -
      Returns:
      Throws:
      IOException
    • posToPrev

      public boolean posToPrev(byte delimiter) throws IOException
      Description copied from interface: Seekable
      Move 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:
      posToPrev in interface Seekable
      Returns:
      true if the position was changed, false otherwise
      Throws:
      IOException
    • getNextPosFor

      public long getNextPosFor(byte delimiter) throws IOException
      Returns the position of the next delimiter or the end of the stream
      Parameters:
      delimiter -
      Returns:
      Throws:
      IOException
    • getPrevPosFor

      public long getPrevPosFor(byte delimiter) throws IOException
      Throws:
      IOException
    • compareToPrefix

      public int compareToPrefix(byte[] prefix) throws IOException
      Description copied from interface: Seekable
      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. 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:
      compareToPrefix in interface Seekable
      Returns:
      Throws:
      IOException
    • peekNextBytes

      public int peekNextBytes(byte[] dst, int offset, int len) throws IOException
      Reads bytes at the current position; does not advance pos
      Specified by:
      peekNextBytes in interface Seekable
      Parameters:
      n -
      Returns:
      Throws:
      IOException
    • readLine

      public String readLine() throws IOException
      Throws:
      IOException
    • readString

      public String readString(int n) throws IOException
      Specified by:
      readString in interface Seekable
      Throws:
      IOException
    • read

      public int read(ByteBuffer dst) throws IOException
      Specified by:
      read in interface ReadableByteChannel
      Specified by:
      read in interface Seekable
      Throws:
      IOException
    • readRemaining

      public static int readRemaining(ByteBuffer dst, ByteBuffer src)
    • isOpen

      public boolean isOpen()
      Specified by:
      isOpen in interface Channel
    • closeActual

      public void closeActual() throws Exception
      Overrides:
      closeActual in class AutoCloseableBase
      Throws:
      Exception