Class BlockFile

java.lang.Object
net.metanotion.io.block.BlockFile
All Implemented Interfaces:
Closeable, AutoCloseable

public class BlockFile extends Object implements Closeable
On-disk format: Magic number (6 bytes) Version major/minor (2 bytes) file length (long) free list start (unsigned int) is mounted (unsigned short) 0 = no, 1 = yes span size (unsigned short) block size (unsigned int) Metaindex skiplist is on page 2 Pages are 1 KB and are numbered starting from 1. e.g. the Metaindex skiplist is at offset 1024 bytes
  • Field Details

  • Constructor Details

  • Method Details

    • main

      public static void main(String[] args)
      Run an integrity check on the blockfile and all the skiplists in it. WARNING: This only works on skiplists using UTF8StringBytes as a key serializer, unless the exception has been coded in bfck below. Will CORRUPT other skiplists.
    • writeMultiPageData

      public int writeMultiPageData(byte[] data, int page, int[] curPageOff, int[] nextPage) throws IOException
      Write bytes This will allocate additional continuation pages as necessary.
      Parameters:
      data - data to write
      page - current page
      curPageOff - in (current) and out (new) parameter at index 0
      nextPage - in (current) and out (new) parameter at index 0
      Returns:
      current page
      Throws:
      IOException
    • readMultiPageData

      public int readMultiPageData(byte[] arr, int page, int[] curPageOff, int[] nextPage) throws IOException
      Read bytes
      Parameters:
      arr - fill this array fully with data
      page - current page
      curPageOff - in (current) and out (new) parameter at index 0
      nextPage - in (current) and out (new) parameter at index 0
      Returns:
      current page
      Throws:
      IOException
    • skipMultiPageBytes

      public int skipMultiPageBytes(int length, int page, int[] curPageOff, int[] nextPage) throws IOException
      Skip length bytes The same as readMultiPageData() without returning a result
      Parameters:
      length - number of bytes to skip
      page - current page
      curPageOff - in (current) and out (new) parameter at index 0
      nextPage - in (current) and out (new) parameter at index 0
      Returns:
      current page
      Throws:
      IOException
    • wasMounted

      public boolean wasMounted()
      I2P was the file locked when we opened it?
      Since:
      0.8.8
    • pageSeek

      public static void pageSeek(RandomAccessInterface file, int page) throws IOException
      Go to any page but the superblock. Page 1 is the superblock, must use file.seek(0) to get there.
      Parameters:
      page - >= 2
      Throws:
      IOException
    • allocPage

      public int allocPage() throws IOException
      Throws:
      IOException
    • freePage

      public void freePage(int page)
      Add the page to the free list. The file is never shrunk. TODO: Reclaim free pages at end of file, or even do a full compaction. Does not throw exceptions; logs on failure.
    • getIndex

      public <K extends Comparable<? super K>, V> BSkipList<K,V> getIndex(String name, Serializer<K> key, Serializer<V> val) throws IOException
      Open a skiplist if it exists. Returns null if the skiplist does not exist. Empty skiplists are not preserved after close. If the file is writable, this runs an integrity check and repair on first open.
      Returns:
      null if not found
      Throws:
      IOException
    • makeIndex

      public <K extends Comparable<? super K>, V> BSkipList<K,V> makeIndex(String name, Serializer<K> key, Serializer<V> val) throws IOException
      Create and open a new skiplist if it does not exist. Throws IOException if it already exists.
      Throws:
      IOException - if already exists or other errors
    • delIndex

      public void delIndex(String name) throws IOException
      Delete a skiplist if it exists. Must be open. Throws IOException if exists but is closed. Broken before 0.9.26.
      Throws:
      IOException - if it is closed.
    • closeIndex

      public void closeIndex(String name)
      Close a skiplist if it is open. Added I2P
    • reformatIndex

      public <K extends Comparable<? super K>, V> void reformatIndex(String name, Serializer<K> oldKey, Serializer<V> oldVal, Serializer<K> newKey, Serializer<V> newVal) throws IOException
      Reformat a skiplist with new Serializers if it exists. The skiplist must be closed. Throws IOException if the skiplist is open. The skiplist will remain closed after completion.
      Throws:
      IOException - if it is open or on errors
      Since:
      0.9.26
    • close

      public void close() throws IOException
      Closes all open skiplists and then the blockfile itself. Note (I2P) Does NOT close the RAF / RAI.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • bfck

      public boolean bfck(boolean fix)
      Run an integrity check on the blockfile and all the skiplists in it
      Returns:
      true if the levels were modified.