Package net.metanotion.io.block
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 Summary
Fields Modifier and Type Field Description RandomAccessInterface
file
Log
log
static int
MAGIC_CONT
static int
METAINDEX_PAGE
static long
OFFSET_MOUNTED
static int
PAGESIZE
int
spanSize
-
Constructor Summary
Constructors Constructor Description BlockFile(File f, boolean init)
File must be writableBlockFile(RandomAccessFile raf)
RAF must be writableBlockFile(RandomAccessFile raf, boolean init)
RAF must be writableBlockFile(RandomAccessInterface rai)
Use this constructor with a readonly RAI for a readonly blockfileBlockFile(RandomAccessInterface rai, boolean init)
Use this constructor with a readonly RAI and init = false for a readonly blockfile
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
allocPage()
boolean
bfck(boolean fix)
Run an integrity check on the blockfile and all the skiplists in itvoid
close()
Closes all open skiplists and then the blockfile itself.void
closeIndex(String name)
Close a skiplist if it is open.void
delIndex(String name)
Delete a skiplist if it exists.void
freePage(int page)
Add the page to the free list.<K extends Comparable<? super K>,V>
BSkipList<K,V>getIndex(String name, Serializer<K> key, Serializer<V> val)
Open a skiplist if it exists.static void
main(String[] args)
Run an integrity check on the blockfile and all the skiplists in it.<K extends Comparable<? super K>,V>
BSkipList<K,V>makeIndex(String name, Serializer<K> key, Serializer<V> val)
Create and open a new skiplist if it does not exist.static void
pageSeek(RandomAccessInterface file, int page)
Go to any page but the superblock.int
readMultiPageData(byte[] arr, int page, int[] curPageOff, int[] nextPage)
Read bytes<K extends Comparable<? super K>,V>
voidreformatIndex(String name, Serializer<K> oldKey, Serializer<V> oldVal, Serializer<K> newKey, Serializer<V> newVal)
Reformat a skiplist with new Serializers if it exists.int
skipMultiPageBytes(int length, int page, int[] curPageOff, int[] nextPage)
Skip length bytes The same as readMultiPageData() without returning a resultboolean
wasMounted()
I2P was the file locked when we opened it?int
writeMultiPageData(byte[] data, int page, int[] curPageOff, int[] nextPage)
Write bytes This will allocate additional continuation pages as necessary.
-
-
-
Field Detail
-
PAGESIZE
public static final int PAGESIZE
- See Also:
- Constant Field Values
-
OFFSET_MOUNTED
public static final long OFFSET_MOUNTED
- See Also:
- Constant Field Values
-
log
public final Log log
-
file
public final RandomAccessInterface file
-
MAGIC_CONT
public static final int MAGIC_CONT
- See Also:
- Constant Field Values
-
METAINDEX_PAGE
public static final int METAINDEX_PAGE
- See Also:
- Constant Field Values
-
spanSize
public int spanSize
-
-
Constructor Detail
-
BlockFile
public BlockFile(RandomAccessInterface rai) throws IOException
Use this constructor with a readonly RAI for a readonly blockfile- Throws:
IOException
-
BlockFile
public BlockFile(RandomAccessFile raf) throws IOException
RAF must be writable- Throws:
IOException
-
BlockFile
public BlockFile(RandomAccessFile raf, boolean init) throws IOException
RAF must be writable- Throws:
IOException
-
BlockFile
public BlockFile(File f, boolean init) throws IOException
File must be writable- Throws:
IOException
-
BlockFile
public BlockFile(RandomAccessInterface rai, boolean init) throws IOException
Use this constructor with a readonly RAI and init = false for a readonly blockfile- Throws:
IOException
-
-
Method Detail
-
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 writepage
- current pagecurPageOff
- in (current) and out (new) parameter at index 0nextPage
- 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 datapage
- current pagecurPageOff
- in (current) and out (new) parameter at index 0nextPage
- 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 skippage
- current pagecurPageOff
- in (current) and out (new) parameter at index 0nextPage
- 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 interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- 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.
-
-