Class BlockAllocator

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

public class BlockAllocator extends Object implements Closeable
This interface allows to allocate and free blocks.

There are two types of the allocators: root and secondary.

Root one allocates and frees on the storage level and used to manage all the blocks in the storage, where as the secondary can be created to manage some groups of blocks. All the blocks allocated over the secondary one can be freed easily.

  • Method Details

    • getName

      public String getName()
      Used to identify the allocator, e.g. in logs. It has no any further meaning as naming this entity.
    • wrap

      public static BlockAllocator wrap(BlockAllocatorInternal blockAllocatorInternal)
      Wraps BlockAllocatorInternal to create an allocator.

      The internal version is the one that exposes for each function a rebalance flag, which is needed to cover the allocation peculiarities, described in BlockSequence.rebalance()

      The internal version implements the heavy-lifting operation, and this class is only hides the rebablancing flag, as all external calls should be executed with rebalance=true flag.

      Parameters:
      blockAllocatorInternal - the internal entity that implements allocator's logic
      Returns:
      the newly created block allocator
    • getBlockAllocation

      public BlockAllocation getBlockAllocation()
      Provides information about block allocation within this block allocator
      Returns:
      the block allocation
    • getBlockAllocatorInternal

      public BlockAllocatorInternal getBlockAllocatorInternal()
    • getFreeBlocks

      public long getFreeBlocks()
    • allocate

      public CatalogBlockIndices allocate(BlockType blockType, int blockNumber)
      Allocates a given number of blocks.

      The call is executed on the best effort basis. If quotas are reached an exception can be thrown.

      Parameters:
      blockType - the block type
      blockNumber - the number of blocks to allocate
      Returns:
      a set of blocks that has been allocated
    • allocate

      public long allocate(BlockType blockType)
      Allocate one block
      Parameters:
      blockType - the type of the block
      Returns:
      the block index of the block
    • free

      public void free(long blockIndex)
      Marks the provided block as free
      Parameters:
      blockIndex - the block index
    • free

      public void free(CatalogBlockIndices indices)
      Marks all the blocks in the provided index as free
      Parameters:
      indices - a set of block indices to free
    • close

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

      public Status getStatus()
      Provides a status of this entity
      Specified by:
      getStatus in interface Closeable
      Returns:
      the status
    • getStartIndex

      public long getStartIndex()
      Provides the first block index of the allocator itself

      Can be used to persist and restore the block allocator.

      Returns:
      the block index
    • remove

      public void remove()
      Frees all the blocks managed by the allocator.

      Only useful for secondary allocator. Root would not do anything as a reaction to this call.

    • getMedia

      public Media getMedia()
      Provides parent media
      Returns:
      the hosting object