Class OverflowToDiskByteStorage

  • All Implemented Interfaces:
    ByteStorage, java.io.Closeable, java.lang.AutoCloseable

    public class OverflowToDiskByteStorage
    extends java.lang.Object
    implements ByteStorage
    Byte storage that keeps data in memory up to a certain size. After that, older sources are moved to disk and the newer ones served from memory.

    Once unloaded to disk, sources are not reloaded into memory as that would be in direct conflict with the filesystem's caching and the costs would probably outweight the benefits.

    The maximum memory used by storage is actually larger than the maximum provided. It may exceed the limit by the size of one source. That is because sources are always loaded into memory before the storage decides to flush them to disk.

    • Constructor Detail

      • OverflowToDiskByteStorage

        public OverflowToDiskByteStorage​(TemporaryDirectoryFactory temporaryDirectoryFactory)
                                  throws java.io.IOException
        Creates a new byte storage with the default memory cache using the provided temporary directory to write data that overflows the memory size.
        Parameters:
        temporaryDirectoryFactory - the factory used to create a temporary directory where to overflow to; the created directory will be closed when the OverflowToDiskByteStorage object is closed
        Throws:
        java.io.IOException - failed to create the temporary directory
      • OverflowToDiskByteStorage

        public OverflowToDiskByteStorage​(long memoryCacheSize,
                                         TemporaryDirectoryFactory temporaryDirectoryFactory)
                                  throws java.io.IOException
        Creates a new byte storage with the given memory cache size using the provided temporary directory to write data that overflows the memory size.
        Parameters:
        memoryCacheSize - the in-memory cache; a value of {@link 0} will effectively disable in-memory caching
        temporaryDirectoryFactory - the factory used to create a temporary directory where to overflow to; the created directory will be closed when the OverflowToDiskByteStorage object is closed
        Throws:
        java.io.IOException - failed to create the temporary directory
    • Method Detail

      • fromStream

        public CloseableByteSource fromStream​(java.io.InputStream stream)
                                       throws java.io.IOException
        Description copied from interface: ByteStorage
        Creates a new byte source by fully reading an input stream.
        Specified by:
        fromStream in interface ByteStorage
        Parameters:
        stream - the input stream
        Returns:
        a byte source containing the cached data from the given stream
        Throws:
        java.io.IOException - failed to read the stream
      • makeBuilder

        public CloseableByteSourceFromOutputStreamBuilder makeBuilder()
                                                               throws java.io.IOException
        Description copied from interface: ByteStorage
        Creates a builder that is an output stream and can create a byte source.
        Specified by:
        makeBuilder in interface ByteStorage
        Returns:
        a builder where data can be written to and a CloseableByteSource can eventually be obtained from
        Throws:
        java.io.IOException - failed to create the builder; this may happen if the builder require some preparation such as temporary storage allocation that may fail
      • fromSource

        public CloseableByteSource fromSource​(com.google.common.io.ByteSource source)
                                       throws java.io.IOException
        Description copied from interface: ByteStorage
        Creates a new byte source from another byte source.
        Specified by:
        fromSource in interface ByteStorage
        Parameters:
        source - the byte source to copy data from
        Returns:
        the tracked byte source
        Throws:
        java.io.IOException - failed to read data from the byte source
      • getBytesUsed

        public long getBytesUsed()
        Description copied from interface: ByteStorage
        Obtains the number of bytes currently used.
        Specified by:
        getBytesUsed in interface ByteStorage
        Returns:
        the number of bytes
      • getMaxBytesUsed

        public long getMaxBytesUsed()
        Description copied from interface: ByteStorage
        Obtains the maximum number of bytes ever used by this tracker.
        Specified by:
        getMaxBytesUsed in interface ByteStorage
        Returns:
        the number of bytes
      • getMemoryBytesUsed

        public long getMemoryBytesUsed()
        Obtains the number of bytes stored in memory.
      • getMaxMemoryBytesUsed

        public long getMaxMemoryBytesUsed()
        Obtains the maximum number of bytes ever stored in memory.
      • getDiskBytesUsed

        public long getDiskBytesUsed()
        Obtains the number of bytes stored in disk.
      • getMaxDiskBytesUsed

        public long getMaxDiskBytesUsed()
        Obtains the maximum number of bytes ever stored in disk.
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException