pub struct BufferPool<S = PageFile> { /* private fields */ }Expand description
A bounded cache of pages over a PageStore.
BufferPool<S> is generic over its backing store; the default is
PageFile, so BufferPool without a type parameter is a pool over a file
of pages. The handle is Send + Sync and every method takes &self, so it
is shared across threads behind an Arc with no outer lock.
§Examples
use page_db::{BufferPool, PageId, Lsn, DEFAULT_PAGE_SIZE};
// A pool of 128 frames over a 4 KiB-page file.
let pool = BufferPool::open(&path, DEFAULT_PAGE_SIZE, 128)?;
// Create page 0, write to it (which marks it dirty), then release the pin.
{
let guard = pool.new_page(PageId::new(0))?;
let mut page = guard.write();
page.set_lsn(Lsn::new(1));
page.payload_mut()[..5].copy_from_slice(b"hello");
}
// Flush dirty pages to the file and make them durable.
pool.flush_all()?;
pool.sync()?;
// Fetch it back — served from cache if resident, else read from the file.
let guard = pool.fetch(PageId::new(0))?;
assert_eq!(&guard.read().payload()[..5], b"hello");Implementations§
Source§impl BufferPool<PageFile>
impl BufferPool<PageFile>
Sourcepub fn open<P: AsRef<Path>>(
path: P,
page_size: PageSize,
capacity: usize,
) -> PageResult<Self>
pub fn open<P: AsRef<Path>>( path: P, page_size: PageSize, capacity: usize, ) -> PageResult<Self>
Open a page file and wrap it in a pool of capacity frames.
A convenience over PageFile::open followed by BufferPool::new.
§Errors
Returns PageError::Io if the file cannot be opened.
Source§impl<S: PageStore> BufferPool<S>
impl<S: PageStore> BufferPool<S>
Sourcepub fn new(store: S, capacity: usize) -> Self
pub fn new(store: S, capacity: usize) -> Self
Build a pool of capacity frames over store.
capacity is the number of pages held resident; it is clamped up to at
least one. The frame buffers are allocated once, here — the pool does no
per-request allocation on the hot path.
Sourcepub fn resident_len(&self) -> usize
pub fn resident_len(&self) -> usize
The number of pages currently resident.
Sourcepub fn is_resident(&self, id: PageId) -> bool
pub fn is_resident(&self, id: PageId) -> bool
Whether page id is currently held in the pool.
Sourcepub fn fetch(&self, id: PageId) -> PageResult<PageGuard>
pub fn fetch(&self, id: PageId) -> PageResult<PageGuard>
Fetch the page at id, pinning it and returning a guard.
Served from cache if resident; otherwise a frame is found (a free one, or
an evicted victim, flushing it first if dirty) and the page is read from
the store into it. The returned PageGuard holds a pin for its
lifetime.
§Errors
PageError::BufferPoolExhaustedif every frame is pinned.- Whatever the store’s read returns (for a file:
PageError::ShortReadpast end-of-file, or an integrity error). PageError::Ioif flushing an evicted dirty victim fails.
Sourcepub fn new_page(&self, id: PageId) -> PageResult<PageGuard>
pub fn new_page(&self, id: PageId) -> PageResult<PageGuard>
Introduce a fresh, zeroed page at id, pinning it and returning a guard.
The page is created in memory and marked dirty, so it is written to the
store on the next flush; no read is performed. If id is already
resident it is reset to a blank page. The caller chooses the id — the
free-list allocator that picks ids is a later release.
§Errors
PageError::BufferPoolExhaustedif every frame is pinned.PageError::Ioif flushing an evicted dirty victim fails.
Sourcepub fn flush(&self, id: PageId) -> PageResult<()>
pub fn flush(&self, id: PageId) -> PageResult<()>
Sourcepub fn flush_all(&self) -> PageResult<()>
pub fn flush_all(&self) -> PageResult<()>
Flush every dirty resident page to the store.
§Errors
Whatever the store’s write returns. On error, some pages may already have been flushed; the operation is safe to retry.
Sourcepub fn checkpoint(&self) -> PageResult<()>
pub fn checkpoint(&self) -> PageResult<()>
Sourcepub fn sync(&self) -> PageResult<()>
pub fn sync(&self) -> PageResult<()>
Make the store durable (the pages already written to it).
This does not flush dirty cached pages first; use
flush_all or
checkpoint for that.
§Errors
Whatever the store’s sync returns.