Class DataWriter

java.lang.Object
java.io.OutputStream
org.rostore.v2.data.DataWriter
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable, Closeable, Committable

public class DataWriter extends OutputStream implements Committable
Class to write the data to the storage using the provided block allocator.

The data is provided in the form of the InputStream, byte array or java object.

It can allocate all the blocks or start writing to some predefined block and allocate the rest, the later is handy for the headers of the Media, or for storages with the predefined layout.

The operations are written for the input streams that have no predefined length, the data is written to the storage as it comes.

In case of any error (e.g. if space is not enough to store the data), the allocated data will safely be released and an error will be thrown.

  • Method Details

    • safeWriter

      public static long safeWriter(BlockAllocator blockAllocator, Consumer<DataWriter> dataWriterConsumer)
      Write the data from the unknown source to the storage in a safe mode.

      The operation creates a data writer object and executes the write operations as a consumer, if any error during write operation happens, it get reverted and only after it the exception is thrown.

      There is no need for any additional clean up in the case of error.

      Parameters:
      blockAllocator - the block allocator be used
      dataWriterConsumer - the consumer that receive the data writer object and should provide the data to it
      Returns:
      the first block index
    • writeObject

      public static <T> long writeObject(BlockAllocator blockAllocator, T object)
      Writes the serialized java object to the store in the safe manner.

      It is similar to safeWriter(BlockAllocator, Consumer), only the data is provided in the for of java object.

      The java object is serialized with BinaryMapper.

      Type Parameters:
      T - the type of the java object
      Parameters:
      blockAllocator - the block allocator to get the blocks
      object - the java object to store
      Returns:
      the index of the first allocated block
    • write

      public void write(byte[] b)
      Overrides:
      write in class OutputStream
    • safeWriter

      public static long safeWriter(BlockAllocator blockAllocator, long startIndex, Consumer<DataWriter> dataWriterConsumer)
      Write the data from the unknown source to the storage in a safe mode.

      This one is similar to safeWriter(BlockAllocator, Consumer), only the first block index is provided explicitly, all other blocks are allocated by the block allocator.

      Parameters:
      blockAllocator - the block allocator to be used
      startIndex - the index of the first block
      dataWriterConsumer - the secured consumer that is writing to this data writer
      Returns:
      the start index
    • open

      public static DataWriter open(BlockAllocator blockAllocator, long startIndex)
      Opens the data writer at the specific starting block index
      Parameters:
      blockAllocator - allocator to be used
      startIndex - the block index to start writing to
      Returns:
      the data writer object
    • fromInputStream

      public static <T extends InputStream> long fromInputStream(BlockAllocator blockAllocator, T inputStream)
      Writes the data from the input stream

      Operation will be reverted if any error happens.

      Type Parameters:
      T - the subtype of the input stream
      Parameters:
      blockAllocator - the block allocator to be used
      inputStream - the input stream with the data
      Returns:
      the first block of the data
    • getStartIndex

      public long getStartIndex()
      Provides the first block of the stored data
      Returns:
      the first block index
    • writeObject

      public <T> void writeObject(T object)
      Writes a java object to the storage

      The object is serialized by BinaryMapper.

      Type Parameters:
      T - the type of the object
      Parameters:
      object - the object to store
    • write

      public void write(int data)
      Writes one byte of the data
      Specified by:
      write in class OutputStream
      Parameters:
      data - the byte.
    • 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 OutputStream
    • getStatus

      public Status getStatus()
      Description copied from interface: Closeable
      Provides a status of this entity
      Specified by:
      getStatus in interface Closeable
      Returns:
      the status
    • unwind

      public void unwind()
      This should be called if operation has been aborted and must be set back. It is done automatically if safe mode operations are used, like those started with or #safeWriter(BlockAllocator, long, Consumer)
    • commit

      public void commit()
      Synchronizes the written data with the storage
      Specified by:
      commit in interface Committable