java.lang.Object
net.metanotion.io.block.FreeListBlock
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 TypeFieldDescriptionprivate final int[]The free pages stored in this block.private final RandomAccessInterfaceThe file the block is read and written through.private static final intHeader size of the block, in bytes.private intFree pages held in this block.private static final longMagic tag of a free list block, "#frList#".private static final longMagic tag of a freed block, "~!FREE!~".private static final intMost free pages one block holds.private intThe next block in the free list chain.final intPage number of this free list block.Constructor Summary
ConstructorsConstructorDescriptionFreeListBlock(RandomAccessInterface file, int startPage) Reads the free list recorded on disk and caches its branch offsets.Method Summary
Modifier and TypeMethodDescriptionvoidaddPage(int freePage) Adds free page and writes new len to diskbooleanflbck(boolean fix) Log this block and then, following the next-page chain, every block after it in the free list.private longgetMagic(int freePage) Reads the magic tag of the block.intGet the page number of the next block in the free list chain.static voidinitPage(RandomAccessInterface file, int page) Initialize a new free list block page with default values.booleanisEmpty()Report whether this block has no free page references left.booleanisFull()Report whether this block already holds as many free page references as fit on one disk page.private voidmarkFree(int freePage) Marks the free block as freed.voidsetNextPage(int nxt) Set and write the next page onlyinttakePage()Takes next page and writes new len to disktoString()Summarise this block for log and diagnostic output.voidWrite this block's data to disk.private voidWrite the length and new page onlyprivate voidwriteLen()Write the length only
Field Details
MAGIC
private static final long MAGICMagic tag of a free list block, "#frList#".- See Also:
MAGIC_FREE
private static final long MAGIC_FREEMagic tag of a freed block, "~!FREE!~".- See Also:
HEADER_LEN
private static final int HEADER_LENHeader size of the block, in bytes.- See Also:
MAX_SIZE
page
public final int pagePage number of this free list block.nextPage
private int nextPageThe next block in the free list chain.len
private int lenFree pages held in this block.branches
private final int[] branchesThe free pages stored in this block.file
The file the block is read and written through.
Constructor Details
FreeListBlock
Reads the free list recorded on disk and caches its branch offsets.- Parameters:
file- the backing store holding this block's disk pagestartPage- 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
Write this block's data to disk.- Throws:
IOException- if the seek or write to this block's page fails
writeLen
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
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
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
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 backIllegalStateException- if the block is already full
takePage
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 corruptIllegalStateException- if the block holds no free page references
markFree
Marks the free block as freed.- Parameters:
freePage- the page to mark- Throws:
IOException- if the writing fails
getMagic
Reads the magic tag of the block.- Parameters:
freePage- the page to read- Returns:
- the magic tag
- Throws:
IOException- if the reading fails
initPage
Initialize a new free list block page with default values.- Parameters:
file- the backing store to initialize inpage- the disk page to initialize as an empty free list block- Throws:
IOException- if the seek or write to the page fails
flbck
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