Class CachedCatalogBlockOperations

java.lang.Object
org.rostore.v2.catalog.CachedCatalogBlockOperations
All Implemented Interfaces:
AutoCloseable, Closeable, Committable

public class CachedCatalogBlockOperations extends Object implements Committable
This class extends CatalogBlockOperations by caching some subset of the catalog in memory. It will make operations faster, but also would make some operation less fault-tolerant.

When the catalog is created, a min and max size of the cache is provided. if the internal cache become bigger than the given maximum, then the entries are written to the catalog. If the cache size is lower than the minimum, the values are extracted form the catalog.

  • Constructor Details

    • CachedCatalogBlockOperations

      public CachedCatalogBlockOperations(CatalogBlockOperations catalogBlockOperations, int minCacheSize, int maxCacheSize)
      Creates a cached version with the CatalogBlockOperations backend
      Parameters:
      catalogBlockOperations - the backing catalog ops
      minCacheSize - the minimum size of the cache
      maxCacheSize - the maximum size of the cache
  • Method Details

    • getSequenceIndexFreeBlockNumber

      public long getSequenceIndexFreeBlockNumber()
      Provides a number of free blocks in the underlying BlockIndexSequence. It is not the same as the free blocks that might be managed by the catalog itself.
      Returns:
      the number of free blocks
    • getCachedBlockNumber

      public long getCachedBlockNumber()
      Provides the number of blocks in the cache
      Returns:
      the cache size
    • getBlockProvider

      public BlockProvider getBlockProvider()
      A block provider that is used to manage the blocks in this catalog operations
      Returns:
      the block provider
    • getAddedNumber

      public long getAddedNumber()
      Catalog counts and persists internally the total number of blocks in the catalog.
      Returns:
      the total number of blocks in the catalog
    • extractIndex

      public CatalogBlockIndices extractIndex(int blockNumber, boolean rebalance)
      This is the version of CatalogBlockOperations.extractIndex(long, boolean) that uses internal cache
      Parameters:
      blockNumber - the number of blocks to extract
      rebalance - indicates if rebalance should be executed
      Returns:
      the indices that has been extracted
    • add

      public void add(CatalogBlockIndices catalogBlockIndices, boolean rebalance)
      This is a version of CatalogBlockOperations.add(CatalogBlockIndices, boolean) with the cache

      With regards to rebalance, see BlockSequence.rebalance()

      Parameters:
      catalogBlockIndices - the set of blocks
      rebalance - indicates if the rebalance should be executed
    • remove

      public void remove(CatalogBlockIndices catalogBlockIndices, boolean rebalance)
      This is a version of CatalogBlockOperations.remove(CatalogBlockIndices, boolean) with the cache

      With regards to rebalance, see BlockSequence.rebalance()

      Parameters:
      catalogBlockIndices - the set of blocks
      rebalance - indicates if the rebalance should be executed
    • close

      public void close()
      Returns all the entries from the cache back to catalog and closes it.
      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
    • commit

      public void commit()
      Operation commits all the active blocks. It will mark the block to be garbage-collected, the data from the associated physical block will be flushed to the persistent storage.
      Specified by:
      commit in interface Committable
    • dump

      public void dump()
    • getStartIndex

      public long getStartIndex()
      Gives the index of the first block of the sequence where the data associated with catalog is stored.

      It is enough to store this index somewhere to be able to restore the whole catalog

      Returns:
      the first block index of this catalog
    • getMedia

      public Media getMedia()
      Provides the parent media
      Returns:
      the parent media
    • iterateAll

      public void iterateAll(Consumer<CatalogBlockIndices> consumer)
      Iterates all blocks in the catalog, inclusive those from the cache
      Parameters:
      consumer - the consumer to consume all the blocks