Skip to main content

hematite/storage/
types.rs

1//! Core storage types and constants.
2//!
3//! This file defines the vocabulary shared by the storage layer:
4//! - `PageId`: logical page address;
5//! - `Page`: a full fixed-size page image;
6//! - reserved page ids and page size;
7//! - pager integrity reporting types used by higher layers.
8//!
9//! The key distinction is that a `Page` is just bytes plus an id. Any meaning attached to those
10//! bytes belongs to higher layers such as the B-tree or catalog code.
11
12use crate::error::Result;
13
14pub const PAGE_SIZE: usize = 4096;
15
16pub const DB_HEADER_PAGE_ID: u32 = 0;
17pub const STORAGE_METADATA_PAGE_ID: u32 = 1;
18pub const INVALID_PAGE_ID: u32 = u32::MAX;
19
20pub type PageId = u32;
21
22#[derive(Debug, Clone)]
23pub struct Page {
24    pub id: PageId,
25    pub data: Vec<u8>,
26}
27
28impl Page {
29    pub fn new(id: PageId) -> Self {
30        Self {
31            id,
32            data: vec![0u8; PAGE_SIZE],
33        }
34    }
35
36    pub fn from_bytes(id: PageId, data: Vec<u8>) -> Result<Self> {
37        if data.len() != PAGE_SIZE {
38            return Err(crate::error::HematiteError::InvalidPage(id));
39        }
40        Ok(Self { id, data })
41    }
42}
43
44#[derive(Debug, Clone, PartialEq, Eq)]
45pub struct PagerIntegrityReport {
46    pub allocated_page_count: usize,
47    pub free_page_count: usize,
48    pub fragmented_free_page_count: usize,
49    pub trailing_free_page_count: usize,
50    pub checksummed_page_count: usize,
51    pub verified_checksum_pages: usize,
52}