Skip to main content

Page

Struct Page 

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

A single fixed-size page: a header and a payload in one aligned buffer.

A Page owns a buffer aligned for Direct I/O, so it can be read into and written from a PageFile without an intermediate copy. Build one with Page::new (an empty page) or get one back from PageFile::read_page / PageFile::allocate_page.

The checksum is not maintained on every mutation — it would be wasted work to rechecksum after each set_lsn or payload write. Instead the page is checksummed once, when it is written (PageFile::write_page stamps it), or on demand via Page::from_bytes, which verifies as it loads.

§Examples

use page_db::{Page, PageSize, Lsn};

let mut page = Page::new(PageSize::new(4096)?);
page.set_lsn(Lsn::new(42));
page.payload_mut()[..3].copy_from_slice(b"abc");

// Serialize to a checksummed byte block and load it back, verified.
let bytes = page.to_checksummed_bytes();
let loaded = Page::from_bytes(PageSize::new(4096)?, &bytes)?;
assert_eq!(loaded.lsn(), Lsn::new(42));
assert_eq!(&loaded.payload()[..3], b"abc");

Implementations§

Source§

impl Page

Source

pub fn new(page_size: PageSize) -> Self

Create an empty, zeroed page of the given size with a valid header.

Source

pub fn from_bytes(page_size: PageSize, bytes: &[u8]) -> PageResult<Self>

Load a page from a byte block, verifying its header and checksum.

The block must be exactly page_size bytes. This is the inverse of Page::to_checksummed_bytes and the same validation PageFile::read_page performs after a read.

§Errors
Source

pub fn page_size(&self) -> usize

The page size in bytes.

Source

pub fn id(&self) -> PageId

The id stamped in the header. For a page from Page::new this is 0 until the page is written to a slot.

Source

pub fn lsn(&self) -> Lsn

The log sequence number stamped in the header.

Source

pub fn set_lsn(&mut self, lsn: Lsn)

Set the log sequence number. Takes effect in the checksum the next time the page is stamped (on PageFile::write_page).

Source

pub fn payload(&self) -> &[u8]

The payload — the page bytes after the header.

Source

pub fn payload_mut(&mut self) -> &mut [u8]

The payload, mutably.

Source

pub fn to_checksummed_bytes(&self) -> Vec<u8>

The whole page as a checksummed byte block, ready to persist elsewhere.

The returned vector is page_size bytes with a freshly computed checksum in the header; feed it back through Page::from_bytes to recover and verify the page. The stamped id is left untouched (0 unless the page came from a file).

Trait Implementations§

Source§

impl Clone for Page

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Page

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Page

§

impl RefUnwindSafe for Page

§

impl Send for Page

§

impl Sync for Page

§

impl Unpin for Page

§

impl UnsafeUnpin for Page

§

impl UnwindSafe for Page

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.