Class BlockSequence<T extends SequenceBlock>

java.lang.Object
org.rostore.v2.media.RootClosableImpl
org.rostore.v2.seq.BlockSequence<T>
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

public class BlockSequence<T extends SequenceBlock> extends RootClosableImpl
Sequences manage a set of blocks. It is needed to be able to manage an object that is longer than one block.

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 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 blocks
      catalogBlockIndices - a set of blocks where the sequence should be stored
      factory - a factory to create a SequenceBlock, which will hold data manipulation logic on the blocks of sequence
      blockType - 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

      public BlockProvider getBlockProvider()
    • getSequenceBlock

      public T 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

      public Block getBlockByIndex(int seqIndex)
      Provides a block by the sequence number, starting from 0
      Parameters:
      seqIndex - the index in the sequence
      Returns:
      the block from the sequence
    • getBlockIndexSequence

      public BlockIndexSequence 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 in BlockIndexSequence.

      Once the number of references in BlockIndexSequence is 0, the block index sequence can be garbage collected.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class RootClosableImpl