Class BlockSequence<T extends SequenceBlock>
- Type Parameters:
T
- a type of the block with the functionality to access its content, it depends on the type of sequence
- All Implemented Interfaces:
AutoCloseable
,Closeable
This object is used to manage a dynamic object, where the blocks can be added and removed from.
There are two major cases cases for the block sequence usage: CatalogBlockOperations
and KeyBlockOperations
.
Catalog block operations manages the list of blocks, that allow to build BlockAllocator
that accounting all the blocks in the media or some part of it.
Key block operations manage the set of keys.
This entity is an active counterpart of BlockIndexSequence
, which only manages the
sequence, when this object adds all the functionality of SequenceBlock
, that
implements block data manipulation.
-
Constructor Summary
ConstructorDescriptionBlockSequence
(BlockProvider blockProvider, long startBlockIndex, Function<BlockSequence<T>, T> factory, BlockType blockType) load existingBlockSequence
(BlockProvider blockProvider, CatalogBlockIndices catalogBlockIndices, Function<BlockSequence<T>, T> factory, BlockType blockType) Create a new sequence.BlockSequence
(BlockProvider blockProvider, BlockIndexSequence blockIndexSequence, Function<BlockSequence<T>, T> factory, BlockType blockType) Creates block sequence from cached blockIndex sequence -
Method Summary
Modifier and TypeMethodDescriptionvoid
addFreeBlock
(int afterIndex) Inserts a new block after the given index in the sequence.void
close()
Closes an instance of the block sequence, and decrements the number of references inBlockIndexSequence
.getBlockByIndex
(int seqIndex) Provides a block by the sequence number, starting from 0Provides the underlying block index sequenceint
length()
void
Because the sequences can be used in the allocators, it can't rely all the time on the external allocators, as they might use the same sequence as this one, which would lead to a concurrent operation caused by the allocator.void
removeFreeBlock
(int index) Operation marks the block with the given index as a free one, assuming previously it was used by the data.Methods inherited from class org.rostore.v2.media.RootClosableImpl
getStatus
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.rostore.v2.media.Closeable
checkOpened
-
Constructor Details
-
BlockSequence
public BlockSequence(BlockProvider blockProvider, CatalogBlockIndices catalogBlockIndices, Function<BlockSequence<T>, T> factory, BlockType blockType) Create a new sequence.- Parameters:
blockProvider
- a block provider that will be used internally to get access to the blockscatalogBlockIndices
- a set of blocks where the sequence should be storedfactory
- a factory to create aSequenceBlock
, which will hold data manipulation logic on the blocks of sequenceblockType
- a type of the blocks in the sequence
-
BlockSequence
public BlockSequence(BlockProvider blockProvider, BlockIndexSequence blockIndexSequence, Function<BlockSequence<T>, T> factory, BlockType blockType) Creates block sequence from cached blockIndex sequence- Parameters:
blockProvider
-blockIndexSequence
-factory
-blockType
-
-
BlockSequence
public BlockSequence(BlockProvider blockProvider, long startBlockIndex, Function<BlockSequence<T>, T> factory, BlockType blockType) load existing
-
-
Method Details
-
getBlockProvider
-
getSequenceBlock
-
length
public int length() -
rebalance
public void rebalance()Because the sequences can be used in the allocators, it can't rely all the time on the external allocators, as they might use the same sequence as this one, which would lead to a concurrent operation caused by the allocator. That's why the sequence uses a preallocated set of blocks (free blocks), that can be used during its operation. After the operation is over the sequence should be "rebalanced" to reinstate the number of free blocks that it might use in the next operation. -
addFreeBlock
public void addFreeBlock(int afterIndex) Inserts a new block after the given index in the sequence.This operation is a self-allocation logic. Block sequence selects any free block preallocated in the sequence and adds it to the specified location.
This added block can be used after this operation.
- Parameters:
afterIndex
- the index in the sequence after which a free block should be inserted
-
removeFreeBlock
public void removeFreeBlock(int index) Operation marks the block with the given index as a free one, assuming previously it was used by the data.This is self-allocation operation, that allows to mark a block as free without involving any allocator. A sequence manages treats these blocks as free.
- Parameters:
index
- the index of the block in the sequence to mark as free
-
getBlockByIndex
Provides a block by the sequence number, starting from 0- Parameters:
seqIndex
- the index in the sequence- Returns:
- the block from the sequence
-
getBlockIndexSequence
Provides the underlying block index sequence- Returns:
- the block index sequence
-
close
public void close()Closes an instance of the block sequence, and decrements the number of references inBlockIndexSequence
.Once the number of references in
BlockIndexSequence
is 0, the block index sequence can be garbage collected.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classRootClosableImpl
-