Class SeekableFromBlock

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

public class SeekableFromBlock extends AutoCloseableWithLeakDetectionBase implements Seekable
Segment could have predecessor / successor methods, but how can we slice segments we request? The use case is to scan backward until a condition is no longer satisfied, maybe we don't need slicing then
Author:
raven
  • Field Details

    • startBlockRef

      protected Ref<? extends Block> startBlockRef
    • startPosInStartSegment

      protected int startPosInStartSegment
    • exposedStartPos

      protected long exposedStartPos
      The start position exposed - may be non-zero or even negative!
    • maxPos

      protected long maxPos
    • minPos

      protected long minPos
    • currentBlockRef

      protected Ref<? extends Block> currentBlockRef
    • currentBlock

      protected Block currentBlock
    • currentSeekable

      protected Seekable currentSeekable
    • actualPos

      protected long actualPos
  • Constructor Details

    • SeekableFromBlock

      public SeekableFromBlock(Ref<? extends Block> startBlockRef, int posInStartSegment, long exposedStartPos)
    • SeekableFromBlock

      public SeekableFromBlock(Ref<? extends Block> startBlockRef, int posInStartSegment, long exposedStartPos, long minPos, long maxPos)
      The startBlockRef is considered to be owned by this object. Closing this seekable also closes the reference.
      Parameters:
      startBlockRef -
      posInStartSegment -
      exposedStartPos -
      minPos -
      maxPos -
  • Method Details

    • init

      protected void init()
    • isOpen

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

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

      public Seekable clone()
      Specified by:
      clone in interface Seekable
      Overrides:
      clone in class Object
    • getPos

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

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

      public void posToStart() throws IOException
      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
      Throws:
      IOException
    • posToEnd

      public void posToEnd() throws IOException
      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
      Throws:
      IOException
    • isPosBeforeStart

      public boolean isPosBeforeStart() throws IOException
      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:
      Throws:
      IOException
    • isPosAfterEnd

      public boolean isPosAfterEnd() throws IOException
      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:
      Throws:
      IOException
    • loadNextBlock

      protected boolean loadNextBlock() throws IOException
      Throws:
      IOException
    • posToNext

      public int posToNext(byte delimiter, boolean changePos) throws IOException
      positive: relative position issue: 0 means no position change, but it does not tell whether the current position is a match or not we could return a pair with +0 with current position matches and -0 current position does not match but no further bytes are available
      Specified by:
      posToNext in interface Seekable
      Parameters:
      delimiter -
      changePos - If no delimiter is found, move the pos to the end
      Returns:
      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
      Parameters:
      prefix -
      Returns:
      Throws:
      IOException
    • 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
      Parameters:
      len -
      Returns:
      Throws:
      IOException
    • checkPrev

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

      public byte get() throws IOException
      Description copied from interface: Seekable
      Read a byte at the current position
      Specified by:
      get in interface Seekable
      Returns:
      The byte at the current position if the position is valid
      Throws:
      IOException
    • read

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

      public int readBroken(ByteBuffer dst) throws IOException
      Throws:
      IOException
    • readString

      public String readString(int len) throws IOException
      Specified by:
      readString in interface Seekable
      Throws:
      IOException