Class ChunkedBlobOutputStream<T>

java.lang.Object
java.io.OutputStream
org.elasticsearch.repositories.blobstore.ChunkedBlobOutputStream<T>
Type Parameters:
T - type of chunk identifier
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public abstract class ChunkedBlobOutputStream<T> extends OutputStream
Base class for doing chunked writes to a blob store. Some blob stores require either up-front knowledge of the size of the blob that will be written or writing it in chunks that are then joined into the final blob at the end of the write. This class provides a basis on which to implement an output stream that encapsulates such a chunked write.
  • Field Details

    • parts

      protected final List<T> parts
      List of identifiers of already written chunks.
    • buffer

      protected ReleasableBytesStreamOutput buffer
      Current write buffer.
    • successful

      protected boolean successful
      Set to true once no more calls to write(int) are expected and the blob has been received by write(int) in full so that close() knows whether to clean up existing chunks or finish a chunked write.
    • flushedBytes

      protected long flushedBytes
      Number of bytes flushed to blob storage so far.
  • Constructor Details

    • ChunkedBlobOutputStream

      protected ChunkedBlobOutputStream(BigArrays bigArrays, long maxBytesToBuffer)
  • Method Details

    • write

      public final void write(int b) throws IOException
      Specified by:
      write in class OutputStream
      Throws:
      IOException
    • write

      public final void write(byte[] b, int off, int len) throws IOException
      Overrides:
      write in class OutputStream
      Throws:
      IOException
    • close

      public final void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException
    • markSuccess

      public final void markSuccess()
      Mark all blob bytes as properly received by write(int), indicating that close() may finalize the blob.
    • finishPart

      protected final void finishPart(T partId)
      Finish writing the current buffer contents to storage and track them by the given partId. Depending on whether all contents have already been written either prepare the write buffer for additional writes or release the buffer.
      Parameters:
      partId - part identifier to track for use when closing
    • flushBuffer

      protected abstract void flushBuffer() throws IOException
      Write the contents of buffer to storage. Implementations should call finishPart(T) at the end to track the the chunk of data just written and ready buffer for the next write.
      Throws:
      IOException
    • onCompletion

      protected abstract void onCompletion() throws IOException
      Invoked once all write chunks/parts are ready to be combined into the final blob. Implementations must invoke the necessary logic for combining the uploaded chunks into the final blob in this method.
      Throws:
      IOException
    • onFailure

      protected abstract void onFailure()
      Invoked in case writing all chunks of data to storage failed. Implementations should run any cleanup required for the already written data in this method.