small_db/btree/page/
page_id.rs

1use std::fmt;
2
3use crate::{
4    btree::page_cache::PageCache,
5    io::{Condensable, SmallReader, SmallWriter, Vaporizable},
6};
7
8pub const EMPTY_PAGE_ID: u32 = 0;
9
10#[derive(PartialEq, Copy, Clone, Eq, Hash, Debug)]
11pub enum PageCategory {
12    RootPointer,
13    Internal,
14    Leaf,
15    Header,
16}
17
18impl Condensable for PageCategory {
19    fn to_bytes(&self) -> Vec<u8> {
20        match self {
21            PageCategory::RootPointer => vec![0, 0, 0, 0],
22            PageCategory::Internal => vec![0, 0, 0, 1],
23            PageCategory::Leaf => vec![0, 0, 0, 2],
24            PageCategory::Header => vec![0, 0, 0, 3],
25        }
26    }
27}
28
29impl Vaporizable for PageCategory {
30    fn read_from(reader: &mut SmallReader) -> Self {
31        let data = reader.read_exact(4);
32        match data {
33            [0, 0, 0, 0] => PageCategory::RootPointer,
34            [0, 0, 0, 1] => PageCategory::Internal,
35            [0, 0, 0, 2] => PageCategory::Leaf,
36            [0, 0, 0, 3] => PageCategory::Header,
37            _ => panic!("invalid page category: {:?}", data),
38        }
39    }
40}
41
42// PageID identifies a unique page, and contains the
43// necessary metadata
44#[derive(Copy, Clone, PartialEq, Eq, Hash)]
45pub struct BTreePageID {
46    /// category indicates the category of the page
47    pub category: PageCategory,
48
49    /// page_index represents the position of the page in
50    /// the table, start from 0
51    pub page_index: u32,
52
53    pub table_id: u32,
54}
55
56impl fmt::Display for BTreePageID {
57    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58        write!(
59            f,
60            "{:?}_{}(table_{})",
61            self.category, self.page_index, self.table_id
62        )
63    }
64}
65
66impl fmt::Debug for BTreePageID {
67    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
68        write!(f, "{}", self)
69    }
70}
71
72impl BTreePageID {
73    pub fn new(
74        category: PageCategory,
75        table_id: u32,
76        page_index: u32,
77    ) -> Self {
78        Self {
79            category,
80            page_index,
81            table_id,
82        }
83    }
84
85    pub fn empty() -> Self {
86        Self {
87            category: PageCategory::RootPointer,
88            page_index: 0,
89            table_id: 0,
90        }
91    }
92
93    pub fn get_table_id(&self) -> u32 {
94        self.table_id
95    }
96
97    pub fn get_short_repr(&self) -> String {
98        format!("{:?}_{}", self.category, self.page_index)
99    }
100}
101
102impl Condensable for BTreePageID {
103    fn to_bytes(&self) -> Vec<u8> {
104        let mut writer = SmallWriter::new();
105        writer.write(&self.category);
106        writer.write(&self.page_index);
107        writer.write(&self.table_id);
108        return writer.to_bytes();
109    }
110}
111
112impl Vaporizable for BTreePageID {
113    fn read_from(reader: &mut SmallReader) -> Self {
114        let category = reader.read();
115        let page_index = reader.read();
116        let table_id = reader.read();
117        Self {
118            category,
119            page_index,
120            table_id,
121        }
122    }
123}
124
125pub fn empty_page_data() -> Vec<u8> {
126    let data: Vec<u8> = vec![0; PageCache::get_page_size()];
127    data
128}