Skip to main content

yykv_allocator/
lib.rs

1#![warn(missing_docs)]
2
3use yykv_hal::DeviceInfo;
4use yykv_types::{MediumType, PageId};
5
6pub struct PageAllocator {
7    medium_type: MediumType,
8    page_size: u32,
9    next_page_id: PageId,
10}
11
12impl PageAllocator {
13    pub fn new(device_info: &DeviceInfo) -> Self {
14        let page_size = match device_info.medium_type {
15            MediumType::NVM => 4096,     // 4K
16            MediumType::NvmeSSD => 4096, // 4K
17            MediumType::SataSSD => 8192, // 8K
18            MediumType::HDD => 262144,   // 256K
19        };
20
21        Self {
22            medium_type: device_info.medium_type,
23            page_size,
24            next_page_id: 0,
25        }
26    }
27
28    pub fn allocate(&mut self) -> PageId {
29        let id = self.next_page_id;
30        self.next_page_id += 1;
31        id
32    }
33
34    pub fn page_size(&self) -> u32 {
35        self.page_size
36    }
37
38    pub fn medium_type(&self) -> MediumType {
39        self.medium_type
40    }
41}