Class HeapDisk

java.lang.Object
com.google.common.jimfs.HeapDisk

final class HeapDisk extends Object
A resizable pseudo-disk acting as a shared space for storing file data. A disk allocates fixed size blocks of bytes to files as needed and may cache blocks that have been freed for reuse. A memory disk has a fixed maximum number of blocks it will allocate at a time (which sets the total "size" of the disk) and a maximum number of unused blocks it will cache for reuse at a time (which sets the minimum amount of space the disk will use once
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private int
    The current total number of blocks that are currently allocated to files.
    (package private) final RegularFile
    Cache of free blocks to be allocated to files.
    private final int
    Fixed size of each block for this disk.
    private final int
    Maximum total number of blocks that the disk may contain at any time.
    private final int
    Maximum total number of unused blocks that may be cached for reuse at any time.
  • Constructor Summary

    Constructors
    Constructor
    Description
    HeapDisk(int blockSize, int maxBlockCount, int maxCachedBlockCount)
    Creates a new disk with the given blockSize, maxBlockCount and maxCachedBlockCount.
    Creates a new disk using settings from the given configuration.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    allocate(RegularFile file, int count)
    Allocates the given number of blocks and adds them to the given file.
    int
    Returns the size of blocks created by this disk.
    private RegularFile
    createBlockCache(int maxCachedBlockCount)
     
    void
    Frees all blocks in the given file.
    void
    free(RegularFile file, int count)
    Frees the last count blocks from the given file.
    long
    Returns the total size of this disk.
    long
    Returns the current number of unallocated bytes on this disk.
    private static int
    toBlockCount(long size, int blockSize)
    Returns the nearest multiple of blockSize that is invalid input: '<'= size.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • blockSize

      private final int blockSize
      Fixed size of each block for this disk.
    • maxBlockCount

      private final int maxBlockCount
      Maximum total number of blocks that the disk may contain at any time.
    • maxCachedBlockCount

      private final int maxCachedBlockCount
      Maximum total number of unused blocks that may be cached for reuse at any time.
    • blockCache

      final RegularFile blockCache
      Cache of free blocks to be allocated to files. While this is stored as a file, it isn't used like a normal file: only the methods for accessing its blocks are used.
    • allocatedBlockCount

      private int allocatedBlockCount
      The current total number of blocks that are currently allocated to files.
  • Constructor Details

    • HeapDisk

      public HeapDisk(Configuration config)
      Creates a new disk using settings from the given configuration.
    • HeapDisk

      public HeapDisk(int blockSize, int maxBlockCount, int maxCachedBlockCount)
      Creates a new disk with the given blockSize, maxBlockCount and maxCachedBlockCount.
  • Method Details

    • toBlockCount

      private static int toBlockCount(long size, int blockSize)
      Returns the nearest multiple of blockSize that is invalid input: '<'= size.
    • createBlockCache

      private RegularFile createBlockCache(int maxCachedBlockCount)
    • blockSize

      public int blockSize()
      Returns the size of blocks created by this disk.
    • getTotalSpace

      public long getTotalSpace()
      Returns the total size of this disk. This is the maximum size of the disk and does not reflect the amount of data currently allocated or cached.
    • getUnallocatedSpace

      public long getUnallocatedSpace()
      Returns the current number of unallocated bytes on this disk. This is the maximum number of additional bytes that could be allocated and does not reflect the number of bytes currently actually cached in the disk.
    • allocate

      public void allocate(RegularFile file, int count) throws IOException
      Allocates the given number of blocks and adds them to the given file.
      Throws:
      IOException
    • free

      public void free(RegularFile file)
      Frees all blocks in the given file.
    • free

      public void free(RegularFile file, int count)
      Frees the last count blocks from the given file.