Class ContainerShard

java.lang.Object
org.rostore.v2.container.ContainerShard
All Implemented Interfaces:
AutoCloseable, Closeable

public class ContainerShard extends Object implements Closeable
Container Shards ar independent portion of the Container, that have its own BlockAllocator and independent set of keys. The user of the container should implement a function that determines which shard should be used. Typically, some sort of hash function that splits the space of the keys can be applied to partition it into the shards.

Container itself does not provide functionality of that kind and only offers the access to the shards.

The shards are using their own BlockAllocator, which allows the independent allocation of the blocks and also allows to remove the shard without knowing of its internal structure. Everything stored with this allocator can be removed when the allocator is removed.

  • Method Details

    • getShardAllocator

      public BlockAllocator getShardAllocator()
    • getDescriptor

      public ContainerShardDescriptor getDescriptor()
    • getIndex

      public int getIndex()
    • getContainer

      public Container getContainer()
    • open

      protected static ContainerShard open(Container container, int index, ContainerShardDescriptor containerShardDescriptor)
      Opens a shard of the container
      Parameters:
      container - the parent container
      index - the index of the shard
      containerShardDescriptor - the information about properties of the shard (should be retrieved from the underlying media)
      Returns:
      the shard object
    • create

      protected static ContainerShard create(Container container, int index)
      Creates a new shard in the storage

      As the shard requires its own resources, the Media.getRootBlockAllocator() is used to create the obtain them.

      Parameters:
      container - the parent container
      index - the index if the shard
      Returns:
      the shard object
    • getBlockAllocation

      public BlockAllocation getBlockAllocation()
      Provides information about block allocation within the shard
      Returns:
      the block allocation
    • putValue

      public <T extends InputStream> long putValue(T data)
      Function to store the body of the value, using the shard's allocator
      Type Parameters:
      T - the type of the input stream
      Parameters:
      data - the data to be stored
      Returns:
      the block index where the data is stored
    • getValue

      public <T extends OutputStream> void getValue(Record record, T outputStream)
      Function to store the body of the value, using the shard's allocator
      Type Parameters:
      T - the type of the output stream
      Parameters:
      record - the record information to retrieve the block where the data starts
      outputStream - the stream to put the data to
    • removeValue

      public void removeValue(long id)
      Function to remove the value from the shard's allocator and free the blocks used by it
      Parameters:
      id - the block index where the data starts
    • toString

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

      public void remove()
      Removes all the data associated with the shard
    • 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
    • keyFunction

      public <T> T keyFunction(Function<ContainerShardKeyOperations,T> keyFunction)
      Executes an operation on the shard's keys catalog.

      The function will provide a cached ContainerShardKeyOperations if one exists, or create one if cache is depleted.

      After the operation is over the CachedCatalogBlockOperations will be returned to the cache, so it can be reused later.

      The operation does not do any blocking, for example for write-operations, it should be enforced by the caller.

      Type Parameters:
      T - the result type, e.g. Record
      Parameters:
      keyFunction - that will receive a ContainerShardKeyOperations instance to execute the operation
      Returns:
      the result of the function execution