Class Media

All Implemented Interfaces:
AutoCloseable, Closeable

public class Media extends RootClosableImpl
A major class representing the ro-store storage.

It is a basic media, which represents a simple basic building block of the media, which can be extended.

  • Field Details

  • Constructor Details

    • Media

      protected Media(File file, MediaProperties mediaProperties, BiConsumer<Media,DataWriter> headerStream)
      Create a new media

      A is used to extend current media by any functionality that is build on top of it.

      The media object is fully constructed when the consumer is executed, so any construction can happen on its basis. The consumer should write the necessary data to the DataWriter so that the state of the construction can be persisted.

      Parameters:
      file - a file where the data should be persisted
      mediaProperties - the properties of the media
      headerStream - consumer that receives both the created media object and a DataWriter that can be used to write additional header information, which allows to extend the media header.
    • Media

      protected Media(File file, BiConsumer<Media,DataReader> headerStream)
      Load an existing media

      A is used to read the header information that has previously been stored at the creation time to the header of the media in the call to Media(File, MediaProperties, BiConsumer). This allows to restore the entities created in the media.

      Parameters:
      file - a file where the data is stored
      headerStream - consumer that receives both the created media object and a DataReader that can be used to read additional header information, which allows to extend the media basic functionality.
  • Method Details

    • getBlockAllocatorListeners

      public BlockAllocatorListeners getBlockAllocatorListeners()
    • getMediaProperties

      public MediaProperties getMediaProperties()
      Provides the properties that has been used for the creation of the media
      Returns:
      the media properties
    • getRootBlockAllocator

      public BlockAllocator getRootBlockAllocator()
      Provides the root block allocator.
      Returns:
      the root block allocator
    • getMemoryConsumption

      public MemoryConsumption getMemoryConsumption()
      Provides information about the objects this Media holds.
      Returns:
      info about the memory consumption
    • getBlockIndexSequences

      public BlockIndexSequences getBlockIndexSequences()
      Provides the set of currently opened BlockIndexSequence
      Returns:
      media's currently active block sequences
    • getMappedPhysicalBlocks

      public MappedPhysicalBlocks getMappedPhysicalBlocks()
      Provides the set of currently mapped physical blocks
      Returns:
      media's currently active blocks
    • close

      public void close()
      Closes the instance of media.

      This function will not regard any open BlockContainer, the caller should finish all the transactions before.

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

      public BlockAllocation getBlockAllocation()
      Provides information about block allocation within the media

      As a source the RootBlockAllocator is used.

      Returns:
      the block allocation
    • closeExpired

      public void closeExpired()
      This function can be called periodically to close the unused blocks and sequences.

      This is a proactive operation. The clean-up is also executed on every operation, but to ensure that the clean-up is also executed in the time when media is idling this operation can be used.

    • create

      public static Media create(File file, MediaProperties mediaProperties)
      Creates a blank media
      Parameters:
      file - the file where the media should be persisted
      mediaProperties - the properties of the media
      Returns:
      the constructed media object
    • create

      public static <T> Media create(File file, MediaProperties mediaProperties, Function<Media,T> headerFactory)
      Creates a new media and store a header class to the header of the media
      Type Parameters:
      T - a type of the header object, must be serializable by the BinaryMapper
      Parameters:
      file - the file location where the media data should be persisted
      mediaProperties - the properties of the media
      headerFactory - creates a header object that will be stored as additional information to the media's header
      Returns:
      the created media object
    • create

      public static Media create(File file, MediaProperties mediaProperties, BiConsumer<Media,DataWriter> headerStream)
      Creates a new media and store with an option to store to headers stream of the media.
      Parameters:
      file - the file where the storage data should be persisted
      mediaProperties - the properties of the storage
      headerStream - a consumer that can write any binary data to the header
      Returns:
      the created media
    • open

      public static Media open(File file)
      Opens the media object from the existing file
      Parameters:
      file - the file where the storage is located
      Returns:
      the media object
    • open

      public static <T> Media open(File file, Class<T> headerClass, BiConsumer<Media,T> header)
      Opens a media, reads the header and provides it for further initialization
      Type Parameters:
      T - the type of the header class
      Parameters:
      file - the file where the storage data is located
      headerClass - a class of the custom header, must be serializable by BinaryMapper
      header - bi consumer that provides both fully initialized media object and the deserialized header object
      Returns:
      the media object
    • open

      public static Media open(File file, BiConsumer<Media,DataReader> headerStream)
      Opens a media, allows to read the header from a binary stream
      Parameters:
      file - the file where the storage data is located
      headerStream - consumer that receives both the fully initialized media object and a DataReader that can be used to read additional header information, to initialize further entities
      Returns:
      opened media
    • map

      public MappedByteBuffer map(long blockIndex)
      A low-level operation of mapping of the block referenced by its index to the mapped byte buffer.

      This operation should not be used directly by the clients of media, instead InternalBlockProvider should be used to get access to the block's data.

      Parameters:
      blockIndex - the index of the block
      Returns:
      the mapped memory block
    • newBlockContainer

      public BlockContainer newBlockContainer()
      Creates a new Block container, which represents a transactional boundary in ro-store, which must be closed after usage.
      Returns:
      the block container
    • getBlockContainer

      public BlockContainer getBlockContainer(int blockContainerId)
      Provides a block container by its id

      Should not be used directly.

      Parameters:
      blockContainerId - the id of the block container
      Returns:
      the block container
    • freeBlockContainer

      public void freeBlockContainer(int blockContainerId)
      Frees a block container

      Should not be used directly.

      Parameters:
      blockContainerId - the id of the block container
    • createSecondaryBlockAllocator

      public BlockAllocator createSecondaryBlockAllocator(String allocatorName, long upperBlockNumberLimit)
      Parameters:
      allocatorName - the name of the allocator
      upperBlockNumberLimit - the maximum number of blocks
      Returns:
      the newly created secondary allocator
    • loadSecondaryBlockAllocator

      public BlockAllocator loadSecondaryBlockAllocator(String allocatorName, long startIndex, long upperBlockNumberLimit)
      Loads SecondaryBlockAllocator based on the internal root allocator. See SecondaryBlockAllocator.load(String, BlockAllocator, long, long)

      Media do not hold instances of the secondary allocators, so the client should hold them and prevent several instances of these allocator to be loaded.

      Parameters:
      allocatorName - the name of the allocator
      startIndex - the first block of the allocator
      upperBlockNumberLimit - the maximum number of blocks
      Returns:
      the loaded secondary allocator
    • removeSecondaryBlockAllocator

      public void removeSecondaryBlockAllocator(BlockAllocator blockAllocator)
      Call to this function will free all the blocks allocated in the secondary allocator.
      Parameters:
      blockAllocator - the allocator to clean up
    • dump

      public void dump()