Class Block

java.lang.Object
org.rostore.v2.media.block.Block
All Implemented Interfaces:
AutoCloseable, Closeable

public class Block extends Object implements Closeable
An object to access and modify the data in the boundaries of one block. The data can be accessed by primitives like byte, int, and so further.

An interface of the block is based upon ByteBuffer, but adds some utility functions.

This interface has a current position, when the write or read operation is executed - this position is incremented based on the length of the data block.

Position can explicitly be modified.

The block is all the time associated with one BlockContainer.

Containers should only be used in one process, so that the block instances are also meant to be used withing this one user process.

That's why both Block and BlockContainer are inherently thread unsafe.

When container closes - all the blocks are get closed.

  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Block(ByteBuffer byteBuffer, MappedPhysicalBlock mappedPhysicalBlock, BlockContainer blockContainer)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    back(int bytesPerRecord)
    Moves current position by the provided number of bytes back.
    void
    Moves current position by the size of the block index length back (see MapperProperties.getBytesPerBlockIndex()).
    void
    Moves current position by the size of the 4 bytes back.
    void
    Moves current position by the size of the 8 bytes back.
    void
    Sets a position to 0 and sets the content to 0
    void
    Closes this entity and all related blocks
    void
    collapseWindow(int windowSize, int tailSize)
    This function moves the data that starts at position+bytes to position the position is set back to the current position
    int
    compare(byte[] data, int offset, int length)
    Compares the given number of bytes from starting from the given offset in the byte array with the block's respective data.
    void
    get(byte[] data, int offset, int length)
    Loads the length number of bytes from the current block's position to the offset in the data array
    long
    Provides the index of the current block in the rostore media.
    byte
    Reads a byte from the current position and increments the current position.
    int
    Reads an int at the current position and increment the current position by 4
    long
    Reads a long at the current position and increment the current position by 8
    long
    getLong(int length)
    Reads the given number of bytes from the current position and increment the current position by this number of bytes.
    Provides a status of this entity
    void
    insertWindows(int windowSize, int tailSize)
    To shift all the data in the block that starts at current position and move it to the location shifted at the windowSize to the front (insert operation)
    int
    The size left in the block starting from current position
    int
    The total size of the block
    protected void
     
    int
    Provides current position in the block
    void
    position(int position)
    Sets current position in the block
    void
    put(byte[] data, int offset, int length)
    Writes a subset of bytes from byte array
    void
    put(Block sourceBlock, int length)
    Puts a given number of bytes from the given block to this one
    void
    putByte(byte b)
    Writes a byte in the current position and increment current position by 1
    void
    putInt(int i)
    Writes an int in the current position and increment current position by 4
    void
    putLong(long l)
    Writes a long in the current position and increment current position by 8
    void
    putLong(long l, int length)
    Writes a provided number of bytes from the provided long in the current position and increment current position by the provided number of bytes.
    long
    Reads a block index from the block at current position, and increment position with the size of block index.
    void
    skip(int bytes)
    Moves current position by the provided number of bytes forward.
     
    void
    writeBlockIndex(long blockIndex)
    Writes a block index to the block at current position, and increment position with the size of block index.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.rostore.v2.media.Closeable

    checkOpened
  • Constructor Details

  • Method Details

    • markDirty

      protected void markDirty()
    • backLong

      public void backLong()
      Moves current position by the size of the 8 bytes back.
    • backInt

      public void backInt()
      Moves current position by the size of the 4 bytes back.
    • back

      public void back(int bytesPerRecord)
      Moves current position by the provided number of bytes back.
    • backBlockIndex

      public void backBlockIndex()
      Moves current position by the size of the block index length back (see MapperProperties.getBytesPerBlockIndex()).
    • getAbsoluteIndex

      public long getAbsoluteIndex()
      Provides the index of the current block in the rostore media.
      Returns:
      the block index
    • position

      public void position(int position)
      Sets current position in the block
      Parameters:
      position - the position withing the block
    • skip

      public void skip(int bytes)
      Moves current position by the provided number of bytes forward.
    • position

      public int position()
      Provides current position in the block
      Returns:
      the position form the start of the block
    • length

      public int length()
      The total size of the block
      Returns:
      size in bytes
    • left

      public int left()
      The size left in the block starting from current position
      Returns:
      the size in bytes
    • putByte

      public void putByte(byte b)
      Writes a byte in the current position and increment current position by 1
      Parameters:
      b - the byte to write
    • putLong

      public void putLong(long l)
      Writes a long in the current position and increment current position by 8
      Parameters:
      l - the long to write
    • putInt

      public void putInt(int i)
      Writes an int in the current position and increment current position by 4
      Parameters:
      i - the int to write
    • collapseWindow

      public void collapseWindow(int windowSize, int tailSize)
      This function moves the data that starts at position+bytes to position the position is set back to the current position
      Parameters:
      windowSize - how many bytes to shift back
      tailSize - number of bytes to copy
    • insertWindows

      public void insertWindows(int windowSize, int tailSize)
      To shift all the data in the block that starts at current position and move it to the location shifted at the windowSize to the front (insert operation)
      Parameters:
      windowSize - the size of the window to clear
      tailSize - number of bytes to move
    • writeBlockIndex

      public void writeBlockIndex(long blockIndex)
      Writes a block index to the block at current position, and increment position with the size of block index.

      See MapperProperties.getBytesPerBlockIndex()

      Parameters:
      blockIndex - the block index
    • putLong

      public void putLong(long l, int length)
      Writes a provided number of bytes from the provided long in the current position and increment current position by the provided number of bytes.
      Parameters:
      l - the long to write
      length - number of bytes to write
    • put

      public void put(byte[] data, int offset, int length)
      Writes a subset of bytes from byte array
      See Also:
    • put

      public void put(Block sourceBlock, int length)
      Puts a given number of bytes from the given block to this one
      Parameters:
      sourceBlock - the block to copy from
      length - number of bytes
    • compare

      public int compare(byte[] data, int offset, int length)
      Compares the given number of bytes from starting from the given offset in the byte array with the block's respective data.
      Parameters:
      data - the byte array to compare with
      offset - the offset in the given array to start with
      length - the number of bytes to compare
      Returns:
      0 if all the bytes are equal, negative if the bytes is found in the data that is lower than the respective one in the block, positive - opposite
    • get

      public void get(byte[] data, int offset, int length)
      Loads the length number of bytes from the current block's position to the offset in the data array
      Parameters:
      data - the array to copy to
      offset - where the data should start in the data array
      length - the number of bytes to copy
    • getLong

      public long getLong(int length)
      Reads the given number of bytes from the current position and increment the current position by this number of bytes.
      Parameters:
      length - the number of bytes to read
      Returns:
      the long where the data is put to
    • readBlockIndex

      public long readBlockIndex()
      Reads a block index from the block at current position, and increment position with the size of block index.

      See MapperProperties.getBytesPerBlockIndex()

      Returns:
      the block index
    • getByte

      public byte getByte()
      Reads a byte from the current position and increments the current position.
      Returns:
      the byte
    • getLong

      public long getLong()
      Reads a long at the current position and increment the current position by 8
      Returns:
      the long that has been read
    • getInt

      public int getInt()
      Reads an int at the current position and increment the current position by 4
      Returns:
      the int that has been read
    • clean

      public void clean()
      Sets a position to 0 and sets the content to 0
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • close

      public void close()
      Description copied from interface: Closeable
      Closes this entity and all related blocks
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • getStatus

      public Status getStatus()
      Description copied from interface: Closeable
      Provides a status of this entity
      Specified by:
      getStatus in interface Closeable
      Returns:
      the status