public class PageChannel
extends java.lang.Object
implements java.nio.channels.Channel, java.io.Flushable
| Modifier and Type | Field and Description |
|---|---|
private boolean |
_autoSync
whether or not to force all writes to disk immediately
|
private java.nio.channels.FileChannel |
_channel
Channel containing the database
|
private boolean |
_closeChannel
whether or not the _channel should be closed by this class
|
private CodecHandler |
_codecHandler
handler for the current database encoding type
|
private java.nio.ByteBuffer |
_forceBytes
dummy buffer used when allocating new pages
|
private JetFormat |
_format
Format of the database in the channel
|
private TempPageHolder |
_fullPageEncodeBufferH
temp page buffer used when pages cannot be partially encoded
|
private UsageMap |
_globalUsageMap
Tracks free pages in the database.
|
private java.nio.ByteBuffer |
_invalidPageBytes
buffer used when deallocating old pages.
|
private TempBufferHolder |
_tempDecodeBufferH |
private int |
_writeCount |
(package private) static java.nio.ByteOrder |
DEFAULT_BYTE_ORDER |
private static byte[] |
INVALID_PAGE_BYTE_HEADER
invalid page header, used when deallocating old pages.
|
(package private) static int |
INVALID_PAGE_NUMBER |
(package private) static int |
PAGE_GLOBAL_USAGE_MAP
Global usage map always lives on page 1
|
(package private) static int |
ROW_GLOBAL_USAGE_MAP
Global usage map always lives at row 0
|
| Modifier | Constructor and Description |
|---|---|
protected |
PageChannel(boolean testing)
Only used by unit tests
|
|
PageChannel(java.nio.channels.FileChannel channel,
boolean closeChannel,
JetFormat format,
boolean autoSync) |
| Modifier and Type | Method and Description |
|---|---|
int |
allocateNewPage()
Allocates a new page in the database.
|
private void |
applyHeaderMask(java.nio.ByteBuffer buffer)
Applies the XOR mask to the database header in the given buffer.
|
private void |
assertWriting()
Asserts that a write operation is in progress.
|
void |
close() |
static java.nio.ByteBuffer |
createBuffer(int size) |
static java.nio.ByteBuffer |
createBuffer(int size,
java.nio.ByteOrder order) |
java.nio.ByteBuffer |
createPageBuffer() |
void |
deallocatePage(int pageNumber)
Deallocate a previously used page in the database.
|
void |
finishWrite()
Completes a "logical" write operation.
|
void |
flush() |
JetFormat |
getFormat() |
private int |
getNextPageNumber(long size)
Returns the next page number based on the given file size.
|
private long |
getPageOffset(int pageNumber)
Returns the offset for a page within the file.
|
void |
initialize(DatabaseImpl database,
CodecProvider codecProvider)
Does second-stage initialization, must be called after construction.
|
boolean |
isAutoSync() |
boolean |
isOpen() |
boolean |
isWriting()
Returns
true if a logical write operation is in progress, false otherwise. |
static java.nio.ByteBuffer |
narrowBuffer(java.nio.ByteBuffer buffer,
int position,
int limit) |
void |
readPage(java.nio.ByteBuffer buffer,
int pageNumber) |
void |
startWrite()
Begins a "logical" write operation.
|
private void |
validatePageNumber(int pageNumber)
Validates that the given pageNumber is valid for this database.
|
static java.nio.ByteBuffer |
wrap(byte[] bytes)
Returns a ByteBuffer wrapping the given bytes and configured with the
default byte order.
|
void |
writePage(java.nio.ByteBuffer page,
int pageNumber)
Write a page to disk
|
void |
writePage(java.nio.ByteBuffer page,
int pageNumber,
int pageOffset)
Write a page (or part of a page) to disk
|
static final int INVALID_PAGE_NUMBER
static final java.nio.ByteOrder DEFAULT_BYTE_ORDER
private static final byte[] INVALID_PAGE_BYTE_HEADER
static final int PAGE_GLOBAL_USAGE_MAP
static final int ROW_GLOBAL_USAGE_MAP
private final java.nio.channels.FileChannel _channel
private final boolean _closeChannel
private final JetFormat _format
private final boolean _autoSync
private final java.nio.ByteBuffer _invalidPageBytes
private final java.nio.ByteBuffer _forceBytes
private UsageMap _globalUsageMap
private CodecHandler _codecHandler
private TempPageHolder _fullPageEncodeBufferH
private TempBufferHolder _tempDecodeBufferH
private int _writeCount
protected PageChannel(boolean testing)
public PageChannel(java.nio.channels.FileChannel channel,
boolean closeChannel,
JetFormat format,
boolean autoSync)
throws java.io.IOException
channel - Channel containing the databaseformat - Format of the database in the channeljava.io.IOExceptionpublic void initialize(DatabaseImpl database, CodecProvider codecProvider) throws java.io.IOException
java.io.IOExceptionpublic JetFormat getFormat()
public boolean isAutoSync()
public void startWrite()
finishWrite() for more
details.public void finishWrite()
throws java.io.IOException
startWrite() call). Logical write operations may be nested. If
the database is configured for "auto-sync", the channel will be flushed
when the outermost operation is complete,java.io.IOExceptionpublic boolean isWriting()
true if a logical write operation is in progress, false otherwise.private void assertWriting()
private int getNextPageNumber(long size)
private long getPageOffset(int pageNumber)
private void validatePageNumber(int pageNumber)
throws java.io.IOException
java.io.IOExceptionpublic void readPage(java.nio.ByteBuffer buffer,
int pageNumber)
throws java.io.IOException
buffer - Buffer to read the page intopageNumber - Number of the page to read in (starting at 0)java.io.IOExceptionpublic void writePage(java.nio.ByteBuffer page,
int pageNumber)
throws java.io.IOException
page - Page to writepageNumber - Page number to write the page tojava.io.IOExceptionpublic void writePage(java.nio.ByteBuffer page,
int pageNumber,
int pageOffset)
throws java.io.IOException
page - Page to writepageNumber - Page number to write the page topageOffset - offset within the page at which to start writing the
page datajava.io.IOExceptionpublic int allocateNewPage()
throws java.io.IOException
writePage(ByteBuffer,int).java.io.IOExceptionpublic void deallocatePage(int pageNumber)
throws java.io.IOException
java.io.IOExceptionpublic java.nio.ByteBuffer createPageBuffer()
public static java.nio.ByteBuffer createBuffer(int size)
public static java.nio.ByteBuffer createBuffer(int size,
java.nio.ByteOrder order)
public void flush()
throws java.io.IOException
flush in interface java.io.Flushablejava.io.IOExceptionpublic void close()
throws java.io.IOException
close in interface java.io.Closeableclose in interface java.lang.AutoCloseableclose in interface java.nio.channels.Channeljava.io.IOExceptionpublic boolean isOpen()
isOpen in interface java.nio.channels.Channelprivate void applyHeaderMask(java.nio.ByteBuffer buffer)
public static java.nio.ByteBuffer narrowBuffer(java.nio.ByteBuffer buffer,
int position,
int limit)
public static java.nio.ByteBuffer wrap(byte[] bytes)