Package net.metanotion.io.block
Class BlockFile
java.lang.Object
net.metanotion.io.block.BlockFile
- All Implemented Interfaces:
Closeable
,AutoCloseable
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
Modifier and TypeFieldDescriptionfinal RandomAccessInterface
final Log
static final int
static final int
static final long
static final int
int
-
Constructor Summary
ConstructorDescriptionFile must be writableRAF must be writableBlockFile
(RandomAccessFile raf, boolean init) RAF must be writableUse 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
Modifier and TypeMethodDescriptionint
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
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
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
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 Details
-
PAGESIZE
public static final int PAGESIZE- See Also:
-
OFFSET_MOUNTED
public static final long OFFSET_MOUNTED- See Also:
-
log
-
file
-
MAGIC_CONT
public static final int MAGIC_CONT- See Also:
-
METAINDEX_PAGE
public static final int METAINDEX_PAGE- See Also:
-
spanSize
public int spanSize
-
-
Constructor Details
-
BlockFile
Use this constructor with a readonly RAI for a readonly blockfile- Throws:
IOException
-
BlockFile
RAF must be writable- Throws:
IOException
-
BlockFile
RAF must be writable- Throws:
IOException
-
BlockFile
File must be writable- Throws:
IOException
-
BlockFile
Use this constructor with a readonly RAI and init = false for a readonly blockfile- Throws:
IOException
-
-
Method Details
-
main
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
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
- 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
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
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
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.
-