java.lang.Object
net.metanotion.io.block.FreeListBlock

class FreeListBlockextends Object
Manages free pages in a block file.

Tracks available pages for allocation using a linked list of blocks. Each block contains multiple free page references and fits on a single disk page.

On-disk format:

   Magic number (long)
   next freelist block page (unsigned int)
   size (unsigned int)
   that many free pages (unsigned ints)

Free page format:

   Magic number (long)
  • Field Summary

    Modifier and Type
    Field
    Description
    private final int[]
    The free pages stored in this block.
    private final RandomAccessInterface
    The file the block is read and written through.
    private static final int
    Header size of the block, in bytes.
    private int
    Free pages held in this block.
    private static final long
    Magic tag of a free list block, "#frList#".
    private static final long
    Magic tag of a freed block, "~!FREE!~".
    private static final int
    Most free pages one block holds.
    private int
    The next block in the free list chain.
    final int
    Page number of this free list block.
  • Constructor Summary

    Constructors
    Constructor
    Description
    FreeListBlock(RandomAccessInterface file, int startPage)
    Reads the free list recorded on disk and caches its branch offsets.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addPage(int freePage)
    Adds free page and writes new len to disk
    boolean
    flbck(boolean fix)
    Log this block and then, following the next-page chain, every block after it in the free list.
    private long
    getMagic(int freePage)
    Reads the magic tag of the block.
    int
    Get the page number of the next block in the free list chain.
    static void
    Initialize a new free list block page with default values.
    boolean
    Report whether this block has no free page references left.
    boolean
    Report whether this block already holds as many free page references as fit on one disk page.
    private void
    markFree(int freePage)
    Marks the free block as freed.
    void
    setNextPage(int nxt)
    Set and write the next page only
    int
    Takes next page and writes new len to disk
    Summarise this block for log and diagnostic output.
    void
    Write this block's data to disk.
    private void
    Write the length and new page only
    private void
    Write the length only

    Methods inherited from class Object

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

    • MAGIC

      private static final long MAGIC
      Magic tag of a free list block, "#frList#".
      See Also:
    • MAGIC_FREE

      private static final long MAGIC_FREE
      Magic tag of a freed block, "~!FREE!~".
      See Also:
    • HEADER_LEN

      private static final int HEADER_LEN
      Header size of the block, in bytes.
      See Also:
    • MAX_SIZE

      private static final int MAX_SIZE
      Most free pages one block holds.
      See Also:
    • page

      public final int page
      Page number of this free list block.
    • nextPage

      private int nextPage
      The next block in the free list chain.
    • len

      private int len
      Free pages held in this block.
    • branches

      private final int[] branches
      The free pages stored in this block.
    • file

      private final RandomAccessInterface file
      The file the block is read and written through.
  • Constructor Details

    • FreeListBlock

      public FreeListBlock(RandomAccessInterface file, int startPage) throws IOException
      Reads the free list recorded on disk and caches its branch offsets.
      Parameters:
      file - the backing store holding this block's disk page
      startPage - the disk page on which this block is stored
      Throws:
      IOException - if the page's magic number or recorded free count is corrupt, or if the free page references cannot be read
  • Method Details

    • writeBlock

      public void writeBlock() throws IOException
      Write this block's data to disk.
      Throws:
      IOException - if the seek or write to this block's page fails
    • writeLen

      private void writeLen() throws IOException
      Write the length only
      Throws:
      IOException - if the seek or write to this block's page fails
    • getNextPage

      public int getNextPage()
      Get the page number of the next block in the free list chain.
      Returns:
      the disk page of the next block, or 0 when this is the last one
    • setNextPage

      public void setNextPage(int nxt) throws IOException
      Set and write the next page only
      Parameters:
      nxt - the disk page of the next block in the free list chain, 0 to end the chain
      Throws:
      IOException - if the seek or write to this block's page fails
    • writeFreePage

      private void writeFreePage() throws IOException
      Write the length and new page only
      Throws:
      IOException - if the seek or write to this block's page fails
    • isEmpty

      public boolean isEmpty()
      Report whether this block has no free page references left.
      Returns:
      true when no free page references remain, so the block holds no space that could be handed out by takePage()
    • isFull

      public boolean isFull()
      Report whether this block already holds as many free page references as fit on one disk page.
      Returns:
      true once the free page references fill the block's share of the disk page, which makes addPage(int) reject the next page
    • addPage

      public void addPage(int freePage) throws IOException
      Adds free page and writes new len to disk
      Parameters:
      freePage - the disk page to add to this block's free list
      Throws:
      IOException - if the page cannot be marked free, or if the updated count cannot be written back
      IllegalStateException - if the block is already full
    • takePage

      public int takePage() throws IOException
      Takes next page and writes new len to disk
      Returns:
      the disk page at the head of this block's free list, released for the caller to allocate
      Throws:
      IOException - if the page taken is not marked free, which means the on-disk free list is corrupt
      IllegalStateException - if the block holds no free page references
    • markFree

      private void markFree(int freePage) throws IOException
      Marks the free block as freed.
      Parameters:
      freePage - the page to mark
      Throws:
      IOException - if the writing fails
    • getMagic

      private long getMagic(int freePage) throws IOException
      Reads the magic tag of the block.
      Parameters:
      freePage - the page to read
      Returns:
      the magic tag
      Throws:
      IOException - if the reading fails
    • initPage

      public static void initPage(RandomAccessInterface file, int page) throws IOException
      Initialize a new free list block page with default values.
      Parameters:
      file - the backing store to initialize in
      page - the disk page to initialize as an empty free list block
      Throws:
      IOException - if the seek or write to the page fails
    • flbck

      public boolean flbck(boolean fix) throws IOException
      Log this block and then, following the next-page chain, every block after it in the free list. Used for diagnostics; it never repairs anything and always reports success.
      Parameters:
      fix - passed down the chain but unused; nothing is repaired
      Returns:
      always true
      Throws:
      IOException - if a following block in the chain cannot be read
    • toString

      public String toString()
      Summarise this block for log and diagnostic output.
      Overrides:
      toString in class Object
      Returns:
      the block identity, its free page count against the block capacity, its disk page and the next block in the chain