Package org.rostore.v2.media.block
Class Block
java.lang.Object
org.rostore.v2.media.block.Block
- All Implemented Interfaces:
AutoCloseable
,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
ModifierConstructorDescriptionprotected
Block
(ByteBuffer byteBuffer, MappedPhysicalBlock mappedPhysicalBlock, BlockContainer blockContainer) -
Method Summary
Modifier and TypeMethodDescriptionvoid
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 (seeMapperProperties.getBytesPerBlockIndex()
).void
backInt()
Moves current position by the size of the 4 bytes back.void
backLong()
Moves current position by the size of the 8 bytes back.void
clean()
Sets a position to 0 and sets the content to 0void
close()
Closes this entity and all related blocksvoid
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 positionint
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 arraylong
Provides the index of the current block in the rostore media.byte
getByte()
Reads a byte from the current position and increments the current position.int
getInt()
Reads an int at the current position and increment the current position by 4long
getLong()
Reads a long at the current position and increment the current position by 8long
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 entityvoid
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
left()
The size left in the block starting from current positionint
length()
The total size of the blockprotected void
int
position()
Provides current position in the blockvoid
position
(int position) Sets current position in the blockvoid
put
(byte[] data, int offset, int length) Writes a subset of bytes from byte arrayvoid
Puts a given number of bytes from the given block to this onevoid
putByte
(byte b) Writes a byte in the current position and increment current position by 1void
putInt
(int i) Writes an int in the current position and increment current position by 4void
putLong
(long l) Writes a long in the current position and increment current position by 8void
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.toString()
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
-
Block
protected Block(ByteBuffer byteBuffer, MappedPhysicalBlock mappedPhysicalBlock, BlockContainer blockContainer)
-
-
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 (seeMapperProperties.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 backtailSize
- 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 cleartailSize
- 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.- 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 writelength
- 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
Puts a given number of bytes from the given block to this one- Parameters:
sourceBlock
- the block to copy fromlength
- 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 withoffset
- the offset in the given array to start withlength
- 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 tooffset
- where the data should start in the data arraylength
- 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.- 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
-
close
public void close()Description copied from interface:Closeable
Closes this entity and all related blocks- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
getStatus
Description copied from interface:Closeable
Provides a status of this entity
-