Package com.mckoi.store
Class LoggingBufferManager
- java.lang.Object
-
- com.mckoi.store.LoggingBufferManager
-
public class LoggingBufferManager extends java.lang.ObjectA 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 classLoggingBufferManager.BMPageA page from a store that is currently being cached in memory.private static classLoggingBufferManager.BResourceA data resource that is being buffered.static interfaceLoggingBufferManager.StoreDataAccessorFactoryA factory interface for creating StoreDataAccessor objects from resource names.
-
Field Summary
Fields Modifier and Type Field Description private booleancheck_point_in_progressSet to true when a 'setCheckPoint' is in progress.private intcurrent_page_countThe number of pages in this buffer.private longcurrent_TA timer that represents the T value in buffer pages.private JournalledSystemjournalled_systemThe JournalledSystem object that handles journalling of all data.private intmax_pagesThe maximum number of pages that should be kept in memory before pages are written out to disk.private java.util.ComparatorPAGE_CACHE_COMPARATORA Comparator used to sort cache entries.private java.util.ArrayListpage_listThe list of all pages.private LoggingBufferManager.BMPage[]page_mapA hash map of all pages currently in memory keyed by store_id and page number.private intpage_sizeThe size of each page.private static booleanPARANOID_CHECKSSet to true for extra assertions.private java.lang.ObjectT_lockA lock used when accessing the current_T, page_list and current_page_count members.private intunique_id_seqA unique id key counter for all stores using this buffer manager.private java.lang.Objectwrite_lockA mutex for when modifying the write lock information.private intwrite_lock_countThe 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 intcalcHashCode(long id, long page_number)Calculates a hash code given an id value and a page_number value.(package private) voidclose(JournalledResource data)(package private) JournalledResourcecreateResource(java.lang.String resource_name)Creates a new resource.private LoggingBufferManager.BMPagefetchPage(JournalledResource data, long page_number)Fetches and returns a page from a store.(package private) longgetDataAreaSize(JournalledResource data)voidlockForWrite()Obtains a write lock on the buffer.private voidpageAccessed(LoggingBufferManager.BMPage page)Called when a page is accessed.private voidpageCreated(LoggingBufferManager.BMPage page)Called when a new page is created.(package private) intreadByteArrayFrom(JournalledResource data, long position, byte[] buf, int off, int len)(package private) intreadByteFrom(JournalledResource data, long position)voidsetCheckPoint(boolean flush_journals)Sets a check point in the log.(package private) voidsetDataAreaSize(JournalledResource data, long new_size)voidstart()Starts the buffer manager.voidstop()Stops the buffer manager.voidunlockForWrite()Releases a write lock on the buffer.(package private) voidwriteByteArrayTo(JournalledResource data, long position, byte[] buf, int off, int len)(package private) voidwriteByteTo(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.IOExceptionStarts the buffer manager.- Throws:
java.io.IOException
-
stop
public void stop() throws java.io.IOExceptionStops 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.InterruptedExceptionObtains 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.InterruptedExceptionSets 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.IOExceptionjava.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
-
-