Package com.mckoi.store
Class LoggingBufferManager
- java.lang.Object
-
- com.mckoi.store.LoggingBufferManager
-
public class LoggingBufferManager extends java.lang.Object
A paged random access buffer manager that caches access between a Store and the underlying filesystem and that also handles check point logging and crash recovery (via a JournalledSystem object).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
LoggingBufferManager.BMPage
A page from a store that is currently being cached in memory.private static class
LoggingBufferManager.BResource
A data resource that is being buffered.static interface
LoggingBufferManager.StoreDataAccessorFactory
A factory interface for creating StoreDataAccessor objects from resource names.
-
Field Summary
Fields Modifier and Type Field Description private boolean
check_point_in_progress
Set to true when a 'setCheckPoint' is in progress.private int
current_page_count
The number of pages in this buffer.private long
current_T
A timer that represents the T value in buffer pages.private JournalledSystem
journalled_system
The JournalledSystem object that handles journalling of all data.private int
max_pages
The maximum number of pages that should be kept in memory before pages are written out to disk.private java.util.Comparator
PAGE_CACHE_COMPARATOR
A Comparator used to sort cache entries.private java.util.ArrayList
page_list
The list of all pages.private LoggingBufferManager.BMPage[]
page_map
A hash map of all pages currently in memory keyed by store_id and page number.private int
page_size
The size of each page.private static boolean
PARANOID_CHECKS
Set to true for extra assertions.private java.lang.Object
T_lock
A lock used when accessing the current_T, page_list and current_page_count members.private int
unique_id_seq
A unique id key counter for all stores using this buffer manager.private java.lang.Object
write_lock
A mutex for when modifying the write lock information.private int
write_lock_count
The number of write locks currently on the buffer.
-
Constructor Summary
Constructors Constructor Description LoggingBufferManager(java.io.File journal_path, boolean read_only, int max_pages, int page_size, LoggingBufferManager.StoreDataAccessorFactory sda_factory, DebugLogger debug, boolean enable_logging)
Constructs the manager.LoggingBufferManager(java.io.File resource_path, java.io.File journal_path, boolean read_only, int max_pages, int page_size, java.lang.String file_ext, long max_slice_size, DebugLogger debug, boolean enable_logging)
Constructs the manager with a scattering store implementation that converts the resource to a file in the given path.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static int
calcHashCode(long id, long page_number)
Calculates a hash code given an id value and a page_number value.(package private) void
close(JournalledResource data)
(package private) JournalledResource
createResource(java.lang.String resource_name)
Creates a new resource.private LoggingBufferManager.BMPage
fetchPage(JournalledResource data, long page_number)
Fetches and returns a page from a store.(package private) long
getDataAreaSize(JournalledResource data)
void
lockForWrite()
Obtains a write lock on the buffer.private void
pageAccessed(LoggingBufferManager.BMPage page)
Called when a page is accessed.private void
pageCreated(LoggingBufferManager.BMPage page)
Called when a new page is created.(package private) int
readByteArrayFrom(JournalledResource data, long position, byte[] buf, int off, int len)
(package private) int
readByteFrom(JournalledResource data, long position)
void
setCheckPoint(boolean flush_journals)
Sets a check point in the log.(package private) void
setDataAreaSize(JournalledResource data, long new_size)
void
start()
Starts the buffer manager.void
stop()
Stops the buffer manager.void
unlockForWrite()
Releases a write lock on the buffer.(package private) void
writeByteArrayTo(JournalledResource data, long position, byte[] buf, int off, int len)
(package private) void
writeByteTo(JournalledResource data, long position, int b)
-
-
-
Field Detail
-
PARANOID_CHECKS
private static boolean PARANOID_CHECKS
Set to true for extra assertions.
-
current_T
private long current_T
A timer that represents the T value in buffer pages.
-
current_page_count
private int current_page_count
The number of pages in this buffer.
-
page_list
private java.util.ArrayList page_list
The list of all pages.
-
T_lock
private final java.lang.Object T_lock
A lock used when accessing the current_T, page_list and current_page_count members.
-
page_map
private final LoggingBufferManager.BMPage[] page_map
A hash map of all pages currently in memory keyed by store_id and page number. NOTE: This MUST be final for the 'fetchPage' method to be safe.
-
unique_id_seq
private int unique_id_seq
A unique id key counter for all stores using this buffer manager.
-
journalled_system
private JournalledSystem journalled_system
The JournalledSystem object that handles journalling of all data.
-
max_pages
private final int max_pages
The maximum number of pages that should be kept in memory before pages are written out to disk.
-
page_size
private final int page_size
The size of each page.
-
check_point_in_progress
private boolean check_point_in_progress
Set to true when a 'setCheckPoint' is in progress.
-
write_lock_count
private int write_lock_count
The number of write locks currently on the buffer. Any number of write locks can be obtained, however a 'setCheckpoint' can only be achieved when there are no write operations in progress.
-
write_lock
private final java.lang.Object write_lock
A mutex for when modifying the write lock information.
-
PAGE_CACHE_COMPARATOR
private final java.util.Comparator PAGE_CACHE_COMPARATOR
A Comparator used to sort cache entries.
-
-
Constructor Detail
-
LoggingBufferManager
public LoggingBufferManager(java.io.File journal_path, boolean read_only, int max_pages, int page_size, LoggingBufferManager.StoreDataAccessorFactory sda_factory, DebugLogger debug, boolean enable_logging)
Constructs the manager.
-
LoggingBufferManager
public LoggingBufferManager(java.io.File resource_path, java.io.File journal_path, boolean read_only, int max_pages, int page_size, java.lang.String file_ext, long max_slice_size, DebugLogger debug, boolean enable_logging)
Constructs the manager with a scattering store implementation that converts the resource to a file in the given path.
-
-
Method Detail
-
start
public void start() throws java.io.IOException
Starts the buffer manager.- Throws:
java.io.IOException
-
stop
public void stop() throws java.io.IOException
Stops the buffer manager.- Throws:
java.io.IOException
-
createResource
JournalledResource createResource(java.lang.String resource_name)
Creates a new resource.
-
lockForWrite
public void lockForWrite() throws java.lang.InterruptedException
Obtains a write lock on the buffer. This will block if a 'setCheckPoint' is in progress, otherwise it will always succeed.- Throws:
java.lang.InterruptedException
-
unlockForWrite
public void unlockForWrite()
Releases a write lock on the buffer. This MUST be called if the 'lockForWrite' method is called. This should be called from a 'finally' clause.
-
setCheckPoint
public void setCheckPoint(boolean flush_journals) throws java.io.IOException, java.lang.InterruptedException
Sets a check point in the log. This logs a point in which a recovery process should at least be able to be rebuild back to. This will block if there are any write locks.Some things to keep in mind when using this. You must ensure that no writes can occur while this operation is occuring. Typically this will happen at the end of a commit but you need to ensure that nothing can happen in the background, such as records being deleted or items being inserted. It is required that the 'no write' restriction is enforced at a high level. If care is not taken then the image written will not be clean and if a crash occurs the image that is recovered will not be stable.
- Throws:
java.io.IOException
java.lang.InterruptedException
-
pageCreated
private void pageCreated(LoggingBufferManager.BMPage page) throws java.io.IOException
Called when a new page is created.- Throws:
java.io.IOException
-
pageAccessed
private void pageAccessed(LoggingBufferManager.BMPage page)
Called when a page is accessed.
-
calcHashCode
private static int calcHashCode(long id, long page_number)
Calculates a hash code given an id value and a page_number value.
-
fetchPage
private LoggingBufferManager.BMPage fetchPage(JournalledResource data, long page_number) throws java.io.IOException
Fetches and returns a page from a store. Pages may be cached. If the page is not available in the cache then a new BMPage object is created for the page requested.- Throws:
java.io.IOException
-
readByteFrom
int readByteFrom(JournalledResource data, long position) throws java.io.IOException
- Throws:
java.io.IOException
-
readByteArrayFrom
int readByteArrayFrom(JournalledResource data, long position, byte[] buf, int off, int len) throws java.io.IOException
- Throws:
java.io.IOException
-
writeByteTo
void writeByteTo(JournalledResource data, long position, int b) throws java.io.IOException
- Throws:
java.io.IOException
-
writeByteArrayTo
void writeByteArrayTo(JournalledResource data, long position, byte[] buf, int off, int len) throws java.io.IOException
- Throws:
java.io.IOException
-
setDataAreaSize
void setDataAreaSize(JournalledResource data, long new_size) throws java.io.IOException
- Throws:
java.io.IOException
-
getDataAreaSize
long getDataAreaSize(JournalledResource data) throws java.io.IOException
- Throws:
java.io.IOException
-
close
void close(JournalledResource data) throws java.io.IOException
- Throws:
java.io.IOException
-
-