|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.caucho.db.block.BlockStore
public class BlockStore
The store manages the block-based persistent store file. Each table will have its own store file, table.db. The store is block-based, where each block is 64k. Block allocation is tracked by a free block, block 0. Each block is represented as a two-byte value. The first byte is the allocation code: free, row, or used. The second byte is a fragment allocation mask. Since 64k stores 32k entries, the allocation block can handle a 2G database size. If the database is larger, another free block occurs at block 32k handling another 2G. The blocks are marked as free (00), row (01), used (10) or fragment(11). Row-blocks are table rows, so a table iterator will only look at the row blocks. Used blocks are for special blocks like the free list. Fragments are for blobs. Each store has a unique id in the database. The store id is merged with the block number in the store to create a unique block id. There are 64k allowed stores (and therefore 64k tables), leaving 64 - 16 = 48 bits for the blocks in a table, i.e. 2 ^ 48 blocks = 256T blocks. block index: the block number in the file. address: the address of a byte within the store, treating the file as a flat file. block id: the unique id of the block in the database.
Field Summary | |
---|---|
protected BlockManager |
_blockManager
|
protected Database |
_database
|
static int |
ALLOC_DATA
|
static int |
ALLOC_FREE
|
static int |
ALLOC_INDEX
|
static int |
ALLOC_INODE_PTR
|
static int |
ALLOC_MASK
|
static int |
ALLOC_MINI_FRAG
|
static int |
ALLOC_ROW
|
static int |
BLOCK_BITS
|
static long |
BLOCK_INDEX_MASK
|
static long |
BLOCK_MASK
|
static long |
BLOCK_OFFSET_MASK
|
static int |
BLOCK_SIZE
|
static java.lang.String |
DATABASE_CORRUPT_EVENT
|
static long |
METADATA_START
|
static int |
MINI_FRAG_ALLOC_OFFSET
|
static int |
MINI_FRAG_PER_BLOCK
|
static int |
MINI_FRAG_SIZE
|
static int |
STORE_CREATE_END
|
Constructor Summary | |
---|---|
BlockStore(Database database,
java.lang.String name,
java.util.concurrent.locks.ReadWriteLock tableLock)
|
|
BlockStore(Database database,
java.lang.String name,
java.util.concurrent.locks.ReadWriteLock rowLock,
Path path)
Creates a new store. |
|
BlockStore(Database database,
java.lang.String name,
java.util.concurrent.locks.ReadWriteLock rowLock,
Path path,
boolean isEnableMmap)
Creates a new store. |
Method Summary | |
---|---|
long |
addressToBlockId(long address)
Converts from the block index to the unique block id. |
Block |
allocateBlock()
Allocates a new block for a non-row. |
Block |
allocateIndexBlock()
Allocates a new block for an index |
Block |
allocateIndirectBlock()
Allocates a new block for a non-row. |
long |
allocateMiniFragment()
Allocates a new miniFragment. |
Block |
allocateRow()
Allocates a new block for a row. |
protected void |
assertStoreActive()
Check that an allocated block is valid. |
static long |
blockIdToAddress(long blockId)
Converts from the block index to the unique block id. |
static long |
blockIdToAddress(long blockId,
int offset)
Converts from the block index to the unique block id. |
static long |
blockIdToIndex(long blockId)
Converts from the block index to the address for database storage. |
static long |
blockIndexToAddr(long blockIndex)
Converts from the block index to the address for database storage. |
void |
close()
Closes the store. |
static java.lang.String |
codeToName(int code)
Debug names for the allocation. |
void |
create()
Creates the store. |
static BlockStore |
create(Path path)
Creates an independent store. |
static BlockStore |
create(Path path,
boolean isMmap)
Creates an independent store. |
static BlockStore |
createMmap(Path path)
Creates an independent store. |
static BlockStore |
createNoMmap(Path path)
Creates an independent store. |
void |
deallocateBlock(long blockId)
Frees a block. |
void |
deleteMiniFragment(long fragmentAddress)
Deletes a miniFragment. |
void |
fatalCorrupted(java.lang.String msg)
|
long |
firstBlock(long blockId,
int type)
Returns the first block id which contains a row. |
long |
firstRowBlock(long blockId)
Returns the first block id which contains a row. |
void |
flush()
Flush the store. |
boolean |
fsync()
Closes the store. |
int |
getAllocation(long blockIndex)
Sets the allocation for a block. |
int |
getAllocationByAddress(long blockAddress)
Sets the allocation for a block. |
byte[] |
getAllocationTable()
Returns a copy of the allocation table. |
long |
getBlockCount()
Returns the block count. |
BlockManager |
getBlockManager()
Returns the block manager. |
long |
getFileSize()
Returns the file size. |
int |
getId()
Returns the store's id. |
RandomAccessStream |
getMmap()
|
java.lang.String |
getName()
Returns the store's name. |
protected BlockReadWrite |
getReadWrite()
|
java.util.concurrent.locks.Lock |
getWriteLock()
Returns the table's lock. |
void |
init()
|
boolean |
isClosed()
True if destroyed. |
boolean |
isFlushDirtyBlocksOnCommit()
If true, dirty blocks are written at commit time. |
boolean |
isIndexBlock(long blockAddress)
Return true if the block is an index block. |
boolean |
isInodePtrBlock(long blockAddress)
Return true if the block is an index block. |
boolean |
isRowBlock(long blockAddress)
Return true if the block is a row block. |
Block |
loadBlock(long blockAddress)
Returns the matching block. |
Block |
readBlock(long blockAddress)
Returns the matching block. |
int |
readBlock(long blockAddress,
int blockOffset,
byte[] buffer,
int offset,
int length)
Reads a block. |
int |
readBlock(long blockAddress,
int blockOffset,
char[] buffer,
int offset,
int length)
Reads a block for a clob. |
void |
readBlock(long blockId,
int blockOffset,
java.io.OutputStream os,
int length)
Reads a block to an output stream. |
long |
readBlockLong(long blockAddress,
int offset)
Reads a long value from a block. |
void |
readBlockNoLock(long blockId,
int blockOffset,
java.io.OutputStream os,
int length)
Reads a block to an output stream. |
static long |
readLong(byte[] buffer,
int offset)
Reads the long. |
int |
readMiniFragment(long fragmentAddress,
int fragmentOffset,
byte[] buffer,
int offset,
int length)
Reads a fragment. |
int |
readMiniFragment(long fragmentAddress,
int fragmentOffset,
char[] buffer,
int offset,
int length)
Reads a miniFragment for a clob. |
long |
readMiniFragmentLong(long fragmentAddress,
int fragmentOffset)
Reads a long value from a miniFragment. |
int |
readMiniFragmentNoLock(long fragmentAddress,
int fragmentOffset,
int length,
java.io.OutputStream os)
Reads a fragment. |
void |
remove()
|
void |
saveAllocation()
Sets the allocation for a block. |
void |
setEnableMmap(boolean isEnable)
|
void |
setFlushDirtyBlocksOnCommit(boolean flushOnCommit)
If true, dirty blocks are written at commit time. |
java.lang.String |
toString()
|
protected void |
validateBlockId(long blockId)
Check that an allocated block is valid. |
Block |
writeBlock(long blockAddress,
int blockOffset,
byte[] buffer,
int offset,
int length)
Writes a block. |
Block |
writeBlock(long blockAddress,
int blockOffset,
char[] buffer,
int offset,
int charLength)
Writes a character based block |
Block |
writeBlockLong(long blockAddress,
int offset,
long value)
Writes a long value to a block |
static void |
writeLong(byte[] buffer,
int offset,
long v)
Writes the long. |
Block |
writeMiniFragment(long fragmentAddress,
int fragmentOffset,
byte[] buffer,
int offset,
int length)
Writes a miniFragment. |
Block |
writeMiniFragment(long fragmentAddress,
int fragmentOffset,
char[] buffer,
int offset,
int length)
Writes a character based |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final int BLOCK_BITS
public static final int BLOCK_SIZE
public static final long BLOCK_INDEX_MASK
public static final long BLOCK_MASK
public static final long BLOCK_OFFSET_MASK
public static final int ALLOC_FREE
public static final int ALLOC_ROW
public static final int ALLOC_DATA
public static final int ALLOC_INODE_PTR
public static final int ALLOC_INDEX
public static final int ALLOC_MINI_FRAG
public static final int ALLOC_MASK
public static final int MINI_FRAG_SIZE
public static final int MINI_FRAG_PER_BLOCK
public static final int MINI_FRAG_ALLOC_OFFSET
public static final long METADATA_START
public static final int STORE_CREATE_END
public static final java.lang.String DATABASE_CORRUPT_EVENT
protected final Database _database
protected final BlockManager _blockManager
Constructor Detail |
---|
public BlockStore(Database database, java.lang.String name, java.util.concurrent.locks.ReadWriteLock tableLock)
public BlockStore(Database database, java.lang.String name, java.util.concurrent.locks.ReadWriteLock rowLock, Path path)
database
- the owning database.name
- the store namelock
- the table lockpath
- the path to the filespublic BlockStore(Database database, java.lang.String name, java.util.concurrent.locks.ReadWriteLock rowLock, Path path, boolean isEnableMmap)
database
- the owning database.name
- the store namelock
- the table lockpath
- the path to the filesMethod Detail |
---|
public static BlockStore create(Path path) throws java.io.IOException, java.sql.SQLException
java.io.IOException
java.sql.SQLException
public static BlockStore createNoMmap(Path path) throws java.io.IOException, java.sql.SQLException
java.io.IOException
java.sql.SQLException
public static BlockStore createMmap(Path path) throws java.io.IOException, java.sql.SQLException
java.io.IOException
java.sql.SQLException
public static BlockStore create(Path path, boolean isMmap) throws java.io.IOException, java.sql.SQLException
java.io.IOException
java.sql.SQLException
public void setEnableMmap(boolean isEnable)
public void setFlushDirtyBlocksOnCommit(boolean flushOnCommit)
public boolean isFlushDirtyBlocksOnCommit()
public java.lang.String getName()
public int getId()
public java.util.concurrent.locks.Lock getWriteLock()
public BlockManager getBlockManager()
protected BlockReadWrite getReadWrite()
public RandomAccessStream getMmap()
public long getFileSize()
public long getBlockCount()
public static long blockIndexToAddr(long blockIndex)
public static long blockIdToIndex(long blockId)
public final long addressToBlockId(long address)
public static long blockIdToAddress(long blockId)
public static long blockIdToAddress(long blockId, int offset)
public void create() throws java.io.IOException, java.sql.SQLException
java.io.IOException
java.sql.SQLException
public void init() throws java.io.IOException
java.io.IOException
public void remove() throws java.sql.SQLException
java.sql.SQLException
public long firstRowBlock(long blockId) throws java.io.IOException
java.io.IOException
public long firstBlock(long blockId, int type) throws java.io.IOException
java.io.IOException
public final Block readBlock(long blockAddress) throws java.io.IOException
java.io.IOException
public final Block loadBlock(long blockAddress) throws java.io.IOException
java.io.IOException
public Block allocateRow() throws java.io.IOException
java.io.IOException
public boolean isRowBlock(long blockAddress)
public Block allocateBlock() throws java.io.IOException
java.io.IOException
public Block allocateIndirectBlock() throws java.io.IOException
java.io.IOException
public Block allocateIndexBlock() throws java.io.IOException
java.io.IOException
public boolean isIndexBlock(long blockAddress)
public boolean isInodePtrBlock(long blockAddress)
protected void validateBlockId(long blockId) throws java.lang.IllegalArgumentException, java.lang.IllegalStateException
java.lang.IllegalArgumentException
java.lang.IllegalStateException
protected void assertStoreActive() throws java.lang.IllegalStateException
java.lang.IllegalStateException
public void deallocateBlock(long blockId) throws java.io.IOException
java.io.IOException
public final int getAllocationByAddress(long blockAddress)
public final int getAllocation(long blockIndex)
public void saveAllocation() throws java.io.IOException
java.io.IOException
public void readBlock(long blockId, int blockOffset, java.io.OutputStream os, int length) throws java.io.IOException
blockAddress
- the address of the blockblockOffset
- the offset inside the block to start readingos
- the result output streamlength
- the number of bytes to read
java.io.IOException
public void readBlockNoLock(long blockId, int blockOffset, java.io.OutputStream os, int length) throws java.io.IOException
blockAddress
- the address of the blockblockOffset
- the offset inside the block to start readingos
- the result output streamlength
- the number of bytes to read
java.io.IOException
public int readBlock(long blockAddress, int blockOffset, byte[] buffer, int offset, int length) throws java.io.IOException
blockAddress
- the address of the blockblockOffset
- the offset inside the block to start readingbuffer
- the result bufferoffset
- offset into the result bufferlength
- the number of bytes to read
java.io.IOException
public int readBlock(long blockAddress, int blockOffset, char[] buffer, int offset, int length) throws java.io.IOException
blockAddress
- the address of the blockblockOffset
- the offset inside the block to start readingbuffer
- the result bufferoffset
- offset into the result bufferlength
- the length of the block in characters
java.io.IOException
public long readBlockLong(long blockAddress, int offset) throws java.io.IOException
java.io.IOException
public Block writeBlock(long blockAddress, int blockOffset, byte[] buffer, int offset, int length) throws java.io.IOException
blockAddress
- the block to writeblockOffset
- the offset into the blockbuffer
- the write bufferoffset
- offset into the write bufferlength
- the number of bytes to write
java.io.IOException
public Block writeBlock(long blockAddress, int blockOffset, char[] buffer, int offset, int charLength) throws java.io.IOException
blockAddress
- the fragment to writeblockOffset
- the offset into the fragmentbuffer
- the write bufferoffset
- offset into the write bufferlength
- the number of bytes to write
java.io.IOException
public Block writeBlockLong(long blockAddress, int offset, long value) throws java.io.IOException
java.io.IOException
public int readMiniFragment(long fragmentAddress, int fragmentOffset, byte[] buffer, int offset, int length) throws java.io.IOException
fragmentAddress
- the address of the fragmentfragmentOffset
- the offset inside the fragment to start readingbuffer
- the result bufferoffset
- offset into the result bufferlength
- the number of bytes to read
java.io.IOException
public int readMiniFragmentNoLock(long fragmentAddress, int fragmentOffset, int length, java.io.OutputStream os) throws java.io.IOException
fragmentAddress
- the address of the fragmentfragmentOffset
- the offset inside the fragment to start readingbuffer
- the result bufferoffset
- offset into the result bufferlength
- the number of bytes to read
java.io.IOException
public int readMiniFragment(long fragmentAddress, int fragmentOffset, char[] buffer, int offset, int length) throws java.io.IOException
fragmentAddress
- the address of the fragmentfragmentOffset
- the offset inside the fragment to start readingbuffer
- the result bufferoffset
- offset into the result bufferlength
- the length of the fragment in characters
java.io.IOException
public long readMiniFragmentLong(long fragmentAddress, int fragmentOffset) throws java.io.IOException
java.io.IOException
public long allocateMiniFragment() throws java.io.IOException
java.io.IOException
public void deleteMiniFragment(long fragmentAddress) throws java.io.IOException
java.io.IOException
public Block writeMiniFragment(long fragmentAddress, int fragmentOffset, byte[] buffer, int offset, int length) throws java.io.IOException
fragmentAddress
- the fragment to writefragmentOffset
- the offset into the fragmentbuffer
- the write bufferoffset
- offset into the write bufferlength
- the number of bytes to write
java.io.IOException
public Block writeMiniFragment(long fragmentAddress, int fragmentOffset, char[] buffer, int offset, int length) throws java.io.IOException
miniFragmentAddress
- the fragment to writefragmentOffset
- the offset into the fragmentbuffer
- the write bufferoffset
- offset into the write bufferlength
- the number of bytes to write
java.io.IOException
public void flush()
public void fatalCorrupted(java.lang.String msg)
public boolean isClosed()
public void close()
public boolean fsync() throws java.io.IOException
java.io.IOException
public byte[] getAllocationTable()
public static long readLong(byte[] buffer, int offset)
public static void writeLong(byte[] buffer, int offset, long v)
public static java.lang.String codeToName(int code)
public java.lang.String toString()
toString
in class java.lang.Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |