APIdia Launch Partner
Top Description Fields Constructors Methods
io.github.spannm.jackcess.impl

public Class PageChannel

extends Object
implements Channel, Flushable
Class Inheritance
All Implemented Interfaces
java.io.Flushable, java.nio.channels.Channel, java.io.Closeable, java.lang.AutoCloseable
Imports
java.io.Flushable, .IOException, java.nio.ByteBuffer, .ByteOrder, java.nio.channels.Channel, .FileChannel

Reads and writes individual pages in a database file
Author
Tim McCune

Field Summary

Modifier and TypeField and Description
private final boolean
autoSync

whether or not to force all writes to disk immediately

private final FileChannel
channel

Channel containing the database

private final boolean
closeChannel

whether or not the channel should be closed by this class

private CodecHandler
codecHandler

handler for the current database encoding type

public static final ByteOrder
DEFAULT_BYTE_ORDER

default byte order of access mdb files

private final ByteBuffer
forceBytes

dummy buffer used when allocating new pages

private final 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 static final byte[]
INVALID_PAGE_BYTE_HEADER

invalid page header, used when deallocating old pages.

pack-priv static final int
private final ByteBuffer
invalidPageBytes

buffer used when deallocating old pages.

private long
knownFileSize

Cached size of the channel, in bytes.

pack-priv static final int
PAGE_GLOBAL_USAGE_MAP

Global usage map always lives on page 1

pack-priv static final int
ROW_GLOBAL_USAGE_MAP

Global usage map always lives at row 0

private TempBufferHolder
private int

Constructor Summary

AccessConstructor and Description
protected
PageChannel(boolean testing)

Only used by unit tests

public
PageChannel(FileChannel
Channel containing the database
channel
,
boolean closeChannel, JetFormat
Format of the database in the channel
format
,
boolean autoSync)

Method Summary

Modifier and TypeMethod and Description
public int
allocateNewPage()

Allocates a new page in the database.

private void
applyHeaderMask(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.

public void
close()

Implements java.nio.channels.Channel.close.

Closes this channel.

public static ByteBuffer

Returns:

A newly-allocated buffer of the given size and DEFAULT_BYTE_ORDER byte order
createBuffer
(int size)

Returns a newly-allocated buffer of the given size and DEFAULT_BYTE_ORDER byte order.

public static ByteBuffer

Returns:

A newly-allocated buffer of the given size and byte order
createBuffer
(int size, ByteOrder order)

Returns a newly-allocated buffer of the given size and byte order.

public ByteBuffer

Returns:

A newly-allocated buffer that can be passed to readPage
createPageBuffer
()

Returns a newly-allocated buffer that can be passed to readPage.

public void
deallocatePage(int pageNumber)

Deallocate a previously used page in the database.

public void
finishWrite()

Completes a "logical" write operation.

public void
flush()

Implements java.io.Flushable.flush.

Flushes this stream by writing any buffered output to the underlying stream.

private long
getFileSize()

Returns: the current channel size, in bytes, tracked locally instead of queried from the OS on every call

public JetFormat
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.

public void
initialize(DatabaseImpl database, CodecProvider codecProvider)

Does second-stage initialization, must be called after construction.

public boolean
public boolean
isOpen()

Implements java.nio.channels.Channel.isOpen.

Tells whether or not this channel is open.

public boolean
isWriting()

Returns true if a logical write operation is in progress, false otherwise.

public static ByteBuffer

Returns:

a duplicate of the current buffer narrowed to the given position and limit. mark will be set at the current position.
narrowBuffer
(ByteBuffer buffer, int position, int limit)

Returns a duplicate of the current buffer narrowed to the given position and limit.

public void
readPage(ByteBuffer
Buffer to read the page into
buffer
,
int
Number of the page to read in (starting at 0)
pageNumber
)

Reads the given page into the buffer, decoding it if necessary.

public void
readRootPage(ByteBuffer
Buffer to read the root page into
buffer
)

Reads the root page into the buffer; this works before the channel is fully initialized.

public void
startExclusiveWrite()

Begins an exclusive "logical" write operation (throws an exception if another write operation is outstanding).

public void
startWrite()

Begins a "logical" write operation.

private void
validatePageNumber(int pageNumber)

Validates that the given pageNumber is valid for this database.

public static ByteBuffer
wrap(byte[] bytes)

Returns a ByteBuffer wrapping the given bytes and configured with the default byte order.

public void
writePage(ByteBuffer
Page to write
page
,
int
Page number to write the page to
pageNumber
)

Write a page to disk

public void
writePage(ByteBuffer
Page to write
page
,
int
Page number to write the page to
pageNumber
,
int
offset within the page at which to start writing the page data
pageOffset
)

Write a page (or part of a page) to disk

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

autoSyncback to summary
private final boolean autoSync

whether or not to force all writes to disk immediately

channelback to summary
private final FileChannel channel

Channel containing the database

closeChannelback to summary
private final boolean closeChannel

whether or not the channel should be closed by this class

codecHandlerback to summary
private CodecHandler codecHandler

handler for the current database encoding type

DEFAULT_BYTE_ORDERback to summary
public static final ByteOrder DEFAULT_BYTE_ORDER

default byte order of access mdb files

forceBytesback to summary
private final ByteBuffer forceBytes

dummy buffer used when allocating new pages

formatback to summary
private final JetFormat format

Format of the database in the channel

fullPageEncodeBufferHback to summary
private TempPageHolder fullPageEncodeBufferH

temp page buffer used when pages cannot be partially encoded

globalUsageMapback to summary
private UsageMap globalUsageMap

Tracks free pages in the database.

INVALID_PAGE_BYTE_HEADERback to summary
private static final byte[] INVALID_PAGE_BYTE_HEADER

invalid page header, used when deallocating old pages. data pages generally have 4 interesting bytes at the beginning which we want to reset.

INVALID_PAGE_NUMBERback to summary
pack-priv static final int INVALID_PAGE_NUMBER
invalidPageBytesback to summary
private final ByteBuffer invalidPageBytes

buffer used when deallocating old pages. data pages generally have 4 interesting bytes at the beginning which we want to reset.

knownFileSizeback to summary
private long knownFileSize

Cached size of the channel, in bytes. The file only ever grows, and only via allocateNewPage, so this is tracked locally to avoid an OS-level size query (e.g. fstat) on every single page read/write. Negative until lazily initialized.

PAGE_GLOBAL_USAGE_MAPback to summary
pack-priv static final int PAGE_GLOBAL_USAGE_MAP

Global usage map always lives on page 1

ROW_GLOBAL_USAGE_MAPback to summary
pack-priv static final int ROW_GLOBAL_USAGE_MAP

Global usage map always lives at row 0

tempDecodeBufferHback to summary
private TempBufferHolder tempDecodeBufferH
writeCountback to summary
private int writeCount

Constructor Detail

PageChannelback to summary
protected PageChannel(boolean testing)

Only used by unit tests

PageChannelback to summary
public PageChannel(FileChannel channel, boolean closeChannel, JetFormat format, boolean autoSync)
Parameters
channel:FileChannel

Channel containing the database

format:JetFormat

Format of the database in the channel

Method Detail

allocateNewPageback to summary
public int allocateNewPage() throws IOException

Allocates a new page in the database. Data in the page is undefined until it is written in a call to writePage(ByteBuffer, int).

applyHeaderMaskback to summary
private void applyHeaderMask(ByteBuffer buffer)

Applies the XOR mask to the database header in the given buffer.

assertWritingback to summary
private void assertWriting()

Asserts that a write operation is in progress.

closeback to summary
public void close() throws IOException

Implements java.nio.channels.Channel.close.

Doc from java.nio.channels.Channel.close.

Closes this channel.

After a channel is closed, any further attempt to invoke I/O operations upon it will cause a ClosedChannelException to be thrown.

If this channel is already closed then invoking this method has no effect.

This method may be invoked at any time. If some other thread has already invoked it, however, then another invocation will block until the first invocation is complete, after which it will return without effect.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
createBufferback to summary
public static ByteBuffer createBuffer(int size)

Returns a newly-allocated buffer of the given size and DEFAULT_BYTE_ORDER byte order.

Returns:ByteBuffer

A newly-allocated buffer of the given size and DEFAULT_BYTE_ORDER byte order

createBufferback to summary
public static ByteBuffer createBuffer(int size, ByteOrder order)

Returns a newly-allocated buffer of the given size and byte order.

Returns:ByteBuffer

A newly-allocated buffer of the given size and byte order

createPageBufferback to summary
public ByteBuffer createPageBuffer()

Returns a newly-allocated buffer that can be passed to readPage.

Returns:ByteBuffer

A newly-allocated buffer that can be passed to readPage

deallocatePageback to summary
public void deallocatePage(int pageNumber) throws IOException

Deallocate a previously used page in the database.

finishWriteback to summary
public void finishWrite() throws IOException

Completes a "logical" write operation. This method should be called in finally block which wraps a logical write operation (which is preceded by a 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,

flushback to summary
public void flush() throws IOException

Implements java.io.Flushable.flush.

Doc from java.io.Flushable.flush.

Flushes this stream by writing any buffered output to the underlying stream.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
getFileSizeback to summary
private long getFileSize() throws IOException
Returns:long

the current channel size, in bytes, tracked locally instead of queried from the OS on every call

getFormatback to summary
public JetFormat getFormat()
getNextPageNumberback to summary
private int getNextPageNumber(long size)

Returns the next page number based on the given file size.

getPageOffsetback to summary
private long getPageOffset(int pageNumber)

Returns the offset for a page within the file.

initializeback to summary
public void initialize(DatabaseImpl database, CodecProvider codecProvider) throws IOException

Does second-stage initialization, must be called after construction.

isAutoSyncback to summary
public boolean isAutoSync()
isOpenback to summary
public boolean isOpen()

Implements java.nio.channels.Channel.isOpen.

Doc from java.nio.channels.Channel.isOpen.

Tells whether or not this channel is open.

Annotations
@Override
isWritingback to summary
public boolean isWriting()

Returns true if a logical write operation is in progress, false otherwise.

narrowBufferback to summary
public static ByteBuffer narrowBuffer(ByteBuffer buffer, int position, int limit)

Returns a duplicate of the current buffer narrowed to the given position and limit. mark will be set at the current position.

Returns:ByteBuffer

a duplicate of the current buffer narrowed to the given position and limit. mark will be set at the current position.

readPageback to summary
public void readPage(ByteBuffer buffer, int pageNumber) throws IOException

Reads the given page into the buffer, decoding it if necessary.

Parameters
buffer:ByteBuffer

Buffer to read the page into

pageNumber:int

Number of the page to read in (starting at 0)

readRootPageback to summary
public void readRootPage(ByteBuffer buffer) throws IOException

Reads the root page into the buffer; this works before the channel is fully initialized.

Parameters
buffer:ByteBuffer

Buffer to read the root page into

startExclusiveWriteback to summary
public void startExclusiveWrite()

Begins an exclusive "logical" write operation (throws an exception if another write operation is outstanding). See finishWrite for more details.

startWriteback to summary
public void startWrite()

Begins a "logical" write operation. See finishWrite for more details.

validatePageNumberback to summary
private void validatePageNumber(int pageNumber) throws IOException

Validates that the given pageNumber is valid for this database.

wrapback to summary
public static ByteBuffer wrap(byte[] bytes)

Returns a ByteBuffer wrapping the given bytes and configured with the default byte order.

writePageback to summary
public void writePage(ByteBuffer page, int pageNumber) throws IOException

Write a page to disk

Parameters
page:ByteBuffer

Page to write

pageNumber:int

Page number to write the page to

writePageback to summary
public void writePage(ByteBuffer page, int pageNumber, int pageOffset) throws IOException

Write a page (or part of a page) to disk

Parameters
page:ByteBuffer

Page to write

pageNumber:int

Page number to write the page to

pageOffset:int

offset within the page at which to start writing the page data