Skip to main content

PageFile

Struct PageFile 

Source
pub struct PageFile { /* private fields */ }
Expand description

A file of fixed-size pages.

A PageFile is an array of Pages on disk, addressed by PageId: page n occupies the byte range n * page_size .. (n + 1) * page_size. Reads and writes are positioned and take &self, so the handle is shared freely across threads — there is no shared file cursor to contend on. (The cache that will front these reads is a later release; today every read goes to disk.)

Durability is two steps, deliberately: write_page places bytes, and sync makes them durable. Batch many writes, then sync once.

§Examples

use page_db::{PageFile, PageFileOptions, PageId, Lsn};

let file = PageFileOptions::new().direct_io(false).open(&path)?;

let mut page = file.allocate_page();
page.set_lsn(Lsn::new(1));
page.payload_mut()[..3].copy_from_slice(b"abc");
file.write_page(PageId::new(0), &mut page)?;
file.sync()?;

let got = file.read_page(PageId::new(0))?;
assert_eq!(&got.payload()[..3], b"abc");
assert_eq!(file.page_count()?, 1);

Implementations§

Source§

impl PageFile

Source

pub fn open<P: AsRef<Path>>(path: P, page_size: PageSize) -> PageResult<Self>

Open a page file at path with the given page size and the default options (Direct I/O on, create-if-absent).

For buffered I/O or other tuning, use PageFileOptions.

§Errors

Returns PageError::Io if the file cannot be opened.

Source

pub fn page_size(&self) -> usize

The page size of this file, in bytes.

Source

pub fn page_count(&self) -> PageResult<u64>

The number of whole pages currently in the file.

§Errors

Returns PageError::Io if the file metadata cannot be read.

Source

pub fn allocate_page(&self) -> Page

Allocate a fresh, zeroed page sized and aligned for this file.

The page is in memory only; write it with write_page to place it in a slot.

Source

pub fn read_page(&self, id: PageId) -> PageResult<Page>

Read the page at slot id, verifying its header and checksum.

The page’s magic, version, and CRC32C are checked, and its stamped id is matched against id, before it is returned — so a corrupt or misdirected page surfaces as an error rather than bad data.

§Errors
Source

pub fn read_into(&self, id: PageId, page: &mut Page) -> PageResult<()>

Read the page at slot id into an existing buffer, verifying it.

This is the zero-allocation form of read_page: it reuses page’s buffer instead of allocating a fresh one, which is how a buffer pool recycles a frame on a cache miss. page.page_size() must match the file’s.

§Errors

As read_page, plus PageError::InvalidPageSize if page’s size does not match the file’s.

Source

pub fn write_page(&self, id: PageId, page: &mut Page) -> PageResult<()>

Write page to slot id, stamping the slot id and a fresh checksum.

The page’s id and checksum header fields are updated in place, so the same page can be written, mutated, and written again. The write places the bytes; call sync to make them durable.

§Errors
Source

pub fn sync(&self) -> PageResult<()>

Flush all written pages to stable storage.

Returns once the data is durable — fdatasync on Linux, FlushFileBuffers on Windows, F_FULLFSYNC on macOS.

§Errors

Returns PageError::Io if the flush fails.

Trait Implementations§

Source§

impl Debug for PageFile

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PageStore for PageFile

Source§

fn page_size(&self) -> usize

The fixed page size of this store, in bytes.
Source§

fn allocate_page(&self) -> Page

Allocate a blank page sized for this store. The page is in memory only.
Source§

fn read_into(&self, id: PageId, page: &mut Page) -> PageResult<()>

Read the page at id into page, verifying it. Read more
Source§

fn write_page(&self, id: PageId, page: &mut Page) -> PageResult<()>

Write page to slot id. The page’s header is stamped (id + checksum) as part of the write. Read more
Source§

fn sync(&self) -> PageResult<()>

Flush all written pages to stable storage. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.