Class DataReader

java.lang.Object
java.io.InputStream
org.rostore.v2.data.DataReader
All Implemented Interfaces:
Closeable, AutoCloseable, Closeable, Committable

public class DataReader extends InputStream implements Committable
This is a counterpart of DataWriter, that allows to read the data from the storage that has been previously written by the writer. Is not thread-safe
  • Method Details

    • open

      public static DataReader open(Media media, long startIndex)
      Opens the data reader to start reading from the given block.

      The data reader should not be used to free the blocks free(), as it will use the root allocator for it.

      Parameters:
      media - the media to be used
      startIndex - the first block to start reading from
      Returns:
      the data reader object
    • open

      public static DataReader open(BlockAllocator allocator, long startIndex)
      Opens the data reader to start reading from the given block.

      This is a more specific operation comparing to open(Media, long), as it specifies a specific allocator, and not the root's one.

      The free() can be executed.

      Parameters:
      allocator - allocator to be used
      startIndex - the first block to start reading from
      Returns:
      the data reader object
    • safeReader

      public static void safeReader(Media media, long startIndex, Consumer<DataReader> dataReaderConsumer)
    • readObject

      public static <T> T readObject(Media media, long startIndex, Class<T> clazz)
    • toOutputStream

      public static <T extends OutputStream> void toOutputStream(Media media, long startIndex, T outputStream)
      Reads the data from the sequence starting at start index and writes it to the output stream.
      Type Parameters:
      T - the subtype of the output stream
      Parameters:
      media - the media to read from
      startIndex - the first block index
      outputStream - the output stream to write to
    • readObject

      public <T> T readObject(Class<T> clazz)
      Reads the java object from the data reader.

      The function uses BinaryMapper to deserialize the object.

      Type Parameters:
      T - the type of the object
      Parameters:
      clazz - the class of the java object
      Returns:
      the java object
    • length

      public long length()
    • position

      public long position()
    • read

      public int read()
      Specified by:
      read in class InputStream
    • free

      public void free()
      Operation is to free the blocks that has previously been allocated by the DataWriter.

      Note, that the BlockAllocator should be correctly provided open(BlockAllocator, long).

      If the open(Media, long) is used, the root allocator will be used.

    • iterateIndices

      public void iterateIndices(Consumer<CatalogBlockIndices> consumer)
      Executed block by block with its content.

      For the very long data sequences, it might be called several times.

    • hasMore

      public boolean hasMore()
    • 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
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InputStream
    • 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