public class BlockFile extends Object implements Closeable
Provides paged file storage with skiplist indexes for efficient key-value data retrieval. Supports multiple named indexes within a single file.
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.
| Modifier and Type | Field and Description |
|---|---|
RandomAccessInterface | fileThe underlying file interface for this block file |
Log | logLogger instance for this BlockFile |
static int | MAGIC_CONTMagic number for continuation pages ("CONT") |
static int | METAINDEX_PAGEPage number for the metaindex |
static long | OFFSET_MOUNTEDFile offset where the mounted flag is stored |
static int | PAGESIZEPage size in bytes for block file pages |
int | spanSizeDefault span size for new skiplists |
| Constructor and Description |
|---|
BlockFile(File f,
boolean init)Create a BlockFile from a File with optional initialization. |
BlockFile(RandomAccessFile raf)Create a BlockFile from a RandomAccessFile. |
BlockFile(RandomAccessFile raf,
boolean init)Create a BlockFile from a RandomAccessFile with optional initialization. |
BlockFile(RandomAccessInterface rai)Use this constructor with a readonly RAI for a readonly blockfile. |
BlockFile(RandomAccessInterface rai,
boolean init)Create a BlockFile from a RandomAccessInterface with optional initialization. |
| Modifier and Type | Method and Description |
|---|---|
int | allocPage()Allocate a new page from the free list or extend the file. |
boolean | bfck(boolean fix)Run an integrity check on the blockfile and all the skiplists in it. |
void | 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> | 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> | 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 from multiple pages. |
<K extends Comparable<? super K>,V> | reformatIndex(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 across multiple pages. |
boolean | wasMounted()Check if the file was locked when opened. |
int | writeMultiPageData(byte[] data,
int page,
int[] curPageOff,
int[] nextPage)Write bytes to multiple pages. |
public final RandomAccessInterface file
public final Log log
public static final int MAGIC_CONT
public static final int METAINDEX_PAGE
public static final long OFFSET_MOUNTED
public static final int PAGESIZE
public int spanSize
public BlockFile(File f, boolean init) throws IOException
f - the File to useinit - if true, initialize the file with a new block structureIOException - if an I/O error occurspublic BlockFile(RandomAccessFile raf) throws IOException
raf - the RandomAccessFile to useIOException - if an I/O error occurspublic BlockFile(RandomAccessFile raf, boolean init) throws IOException
raf - the RandomAccessFile to useinit - if true, initialize the file with a new block structureIOException - if an I/O error occurspublic BlockFile(RandomAccessInterface rai) throws IOException
rai - the RandomAccessInterface to useIOException - if an I/O error occurspublic BlockFile(RandomAccessInterface rai, boolean init) throws IOException
rai - the RandomAccessInterface to useinit - if true, initialize the file with a new block structureIOException - if an I/O error occurspublic int allocPage()
throws IOExceptionIOException - if an I/O error occurspublic boolean bfck(boolean fix)
fix - if true, attempt to fix any corruption foundpublic void close()
throws IOExceptionclose in interface Closeableclose in interface AutoCloseableIOExceptionpublic void closeIndex(String name)
name - the namepublic void delIndex(String name) throws IOException
name - the nameIOException - if it is closed.public void freePage(int page)
page - the page number to freepublic <K extends Comparable<? super K>,V> BSkipList<K,V> getIndex(String name, Serializer<K> key, Serializer<V> val) throws IOException
K - the type of keys in the skiplistV - the type of values in the skiplistname - the namekey - the Serializer for keysval - the Serializer for valuesIOException - if an I/O error occurspublic static void main(String[] args)
args - command line arguments (file path)public <K extends Comparable<? super K>,V> BSkipList<K,V> makeIndex(String name, Serializer<K> key, Serializer<V> val) throws IOException
K - the type of keys in the skiplistV - the type of values in the skiplistname - the namekey - the Serializer for keysval - the Serializer for valuesIOException - if already exists or other errorspublic static void pageSeek(RandomAccessInterface file, int page) throws IOException
file - the RandomAccessInterface to seek inpage - the page number (>= 2)IOException - if page is invalidpublic int readMultiPageData(byte[] arr,
int page,
int[] curPageOff,
int[] nextPage)
throws IOExceptionarr - 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 0IOException - if an I/O error occurs or not enough pages to readpublic <K extends Comparable<? super K>,V> void reformatIndex(String name, Serializer<K> oldKey, Serializer<V> oldVal, Serializer<K> newKey, Serializer<V> newVal) throws IOException
K - the type of keys in the skiplistV - the type of values in the skiplistname - the nameoldKey - the current Serializer for keysoldVal - the current Serializer for valuesnewKey - the new Serializer for keysnewVal - the new Serializer for valuesIOException - if it is open or on errorspublic int skipMultiPageBytes(int length,
int page,
int[] curPageOff,
int[] nextPage)
throws IOExceptionlength - number of bytes to skippage - current pagecurPageOff - in (current) and out (new) parameter at index 0nextPage - in (current) and out (new) parameter at index 0IOException - if an I/O error occurs or not enough pages to skippublic boolean wasMounted()
public int writeMultiPageData(byte[] data,
int page,
int[] curPageOff,
int[] nextPage)
throws IOExceptiondata - data to writepage - current pagecurPageOff - in (current) and out (new) parameter at index 0nextPage - in (current) and out (new) parameter at index 0IOException - if an I/O error occurs