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
impl Page
Sourcepub fn new(page_size: PageSize) -> Self
pub fn new(page_size: PageSize) -> Self
Create an empty, zeroed page of the given size with a valid header.
Sourcepub fn from_bytes(page_size: PageSize, bytes: &[u8]) -> PageResult<Self>
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
PageError::ShortReadifbytes.len()is notpage_size.PageError::BadMagic/PageError::UnsupportedVersionif the header is not a page-db page this build understands.PageError::ChecksumMismatchif the checksum does not match.
Sourcepub fn id(&self) -> PageId
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.
Sourcepub fn set_lsn(&mut self, lsn: Lsn)
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).
Sourcepub fn payload_mut(&mut self) -> &mut [u8] ⓘ
pub fn payload_mut(&mut self) -> &mut [u8] ⓘ
The payload, mutably.
Sourcepub fn to_checksummed_bytes(&self) -> Vec<u8> ⓘ
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).