Class KeyBlockOperations

java.lang.Object
org.rostore.v2.keys.KeyBlockOperations
All Implemented Interfaces:
AutoCloseable, Closeable, Committable

public class KeyBlockOperations extends Object implements Committable
This is a class to manage keys.

It is NOT thread-safe and should be protected in multi-thread environment.

This class organizes the access to the KeyBlockEntry and facilitates its work with VarSizeBlock.

The moves within the key sequence is synchronized with the respective VarSizeBlock and its variants like VarSizeEntry and VarSizeMultiBlock.

  • Method Details

    • getBlockSequence

      public BlockSequence getBlockSequence()
      A block sequence that underlies this operation's object
      Returns:
      the block sequence
    • remove

      public void remove()
      Function removes all the keys from the sequence and remove the sequence itself, freeing all the resources
    • dump

      public void dump()
      Dumps the content of the key catalog to the System.out. This is for debugging purposes.
    • load

      public static KeyBlockOperations load(BlockAllocator blockAllocator, long startIndex, RecordLengths recordLengths)
      Loads the key operations
      Parameters:
      blockAllocator - the block allocator to allocate new blocks on behalf of the keys
      startIndex - the index of the first block in the sequence
      recordLengths - the specification of lengths of elements for this class
      Returns:
      the key operations object
    • create

      public static KeyBlockOperations create(BlockAllocator blockAllocator, RecordLengths recordLengths)
      Creates a new key operations, allocates a new sequence to manage the keys.

      In this way created element stays permanently in the underlying storage.

      It can be loaded afterwards by load(BlockAllocator, long, RecordLengths)

      Parameters:
      blockAllocator - the block allocator to be used to get the new blocks
      recordLengths - the length of key entry's elements
      Returns:
      the created instance
    • getStartIndex

      public long getStartIndex()
      This is first block's index.
      Returns:
      the index of first memory block
    • removeIfExpired

      public long removeIfExpired(int blockIndex)
      This function is to look up the expired entries and remove them
      Parameters:
      blockIndex - the key block to lookup in
      Returns:
      the id of the removed expired entry or Utils.ID_UNDEFINED if nothing has been deleted
    • remove

      public boolean remove(byte[] key, Record record)
      Searches a key and removes the key if found. throws VersionMismatchException if provided version is not matches to the state in the storage
      Parameters:
      key - the key to remove
      Returns:
      true if the key is removed
    • put

      public long put(byte[] key, Record record)
      Store the record for specified key
      Parameters:
      key - the key to store
      record - the record to store
      Returns:
      the previous id (if one exists) or Utils.ID_UNDEFINED
    • list

      public KeyList list(byte[] startWithKey, byte[] continuationKey, long maxNumber, long maxSize)
      List the keys managed by this instance.

      It returns only subset of keys limited by maxNumber and maxSize, as well as startWithKey

      Parameters:
      startWithKey - the prefix of the keys
      continuationKey - the key to start with
      maxNumber - the maximum number of keys to return
      maxSize - the maximum size of keys to return
      Returns:
      the list of keys
    • getRecord

      public Record getRecord(byte[] key)
      Searches for the provided key and return the Record associated with it.
      Parameters:
      key - a key to return
      Returns:
      the record if one has been found or null
    • 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
    • commit

      public void commit()
      Description copied from interface: Committable
      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