Skip to content

Commit

Permalink
simple extern works
Browse files Browse the repository at this point in the history
  • Loading branch information
Codetector1374 committed Aug 3, 2024
1 parent 49b5e84 commit 4f6dc32
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 75 deletions.
4 changes: 2 additions & 2 deletions src/innodb/buffer_manager/lru.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use super::BufferManager;
use super::{BufferManager, PageGuard};
use anyhow::{anyhow, Result};
use crate::innodb::page::Page;

pub struct LRUBufferManager {}

impl BufferManager for LRUBufferManager {
fn open_page(&self, space_id: u32, offset: u32) -> Result<Page> {
fn open_page(&self, space_id: u32, offset: u32) -> Result<PageGuard> {
todo!()
}

Expand Down
23 changes: 16 additions & 7 deletions src/innodb/buffer_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,33 @@ pub mod lru;
pub mod simple;

pub trait BufferManager {
fn open_page(&self, space_id: u32, offset: u32) -> Result<Page>;
fn open_page(&self, space_id: u32, offset: u32) -> Result<PageGuard>;
fn close_page(&self, page: Page);
}

pub struct PageGuard<'a, B: BufferManager> {
pub struct PageGuard<'a> {
page: Page<'a>,
buffer_manager: &'a B
buffer_manager: &'a dyn BufferManager
}

impl <'a, B: BufferManager> Deref for PageGuard<'a, B> {
impl <'a> PageGuard<'a> {
pub fn new(page: Page<'a>, buffer_manager: &'a dyn BufferManager) -> Self {
PageGuard {
page,
buffer_manager,
}
}
}

impl <'a> Deref for PageGuard<'a> {
type Target = Page<'a>;

fn deref(&self) -> &Self::Target {
&self.page
}
}

impl <'a, B: BufferManager> Drop for PageGuard<'a, B> {
impl <'a> Drop for PageGuard<'a> {
fn drop(&mut self) {
self.buffer_manager.close_page(std::mem::take(&mut self.page));
}
Expand All @@ -33,11 +42,11 @@ impl <'a, B: BufferManager> Drop for PageGuard<'a, B> {
pub struct DummyBufferMangaer;

impl BufferManager for DummyBufferMangaer {
fn open_page(&self, _space_id: u32, _offset: u32) -> Result<Page> {
fn open_page(&self, _space_id: u32, _offset: u32) -> Result<PageGuard> {
Err(anyhow!("Dummy buffer manager can't open"))
}

fn close_page(&self, _: Page) {
panic!("This doens't open how can we close");
}
}
}
10 changes: 6 additions & 4 deletions src/innodb/buffer_manager/simple.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::Result;
use tracing::trace;
use std::{
cell::RefCell,
collections::HashMap,
Expand All @@ -10,7 +11,7 @@ use std::{

use crate::innodb::page::{Page, FIL_PAGE_SIZE};

use super::BufferManager;
use super::{BufferManager, PageGuard};

pub struct SimpleBufferManager {
page_directory: PathBuf,
Expand Down Expand Up @@ -52,12 +53,13 @@ impl SimpleBufferManager {
}

impl BufferManager for SimpleBufferManager {
fn open_page(&self, space_id: u32, offset: u32) -> Result<Page> {
fn open_page(&self, space_id: u32, offset: u32) -> Result<PageGuard> {
let buf = self.get_page(space_id, offset)?;
Page::from_bytes(buf)
trace!("Opened ({}, {})", space_id, offset);
Ok(PageGuard::new(Page::from_bytes(buf)?, self))
}

fn close_page(&self, page: Page) {
// Nothing
trace!("Closed ({:?}, {})", page.space_id, page.header.offset);
}
}
85 changes: 60 additions & 25 deletions src/innodb/file_list.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,48 @@
use anyhow::{anyhow, Result};
use anyhow::{anyhow, Ok, Result};

pub const FIL_NULL: u32 = 0xFFFF_FFFF;

#[derive(Debug, Clone, Copy)]
pub struct FileAddress {
pub page_number: u32,
pub offset: u16,
}

impl FileAddress {
pub fn new(page_number: u32, offset: u16) -> Self {
FileAddress {
page_number,
offset,
}
}

pub fn try_from_bytes(buf: &[u8]) -> Result<Self> {
if buf.len() < 6 {
return Err(anyhow!("Buffer is too small"));
}

let page_number = u32::from_be_bytes([buf[0], buf[1], buf[2], buf[3]]);
let offset = u16::from_be_bytes([buf[4], buf[5]]);
Ok(FileAddress {
page_number,
offset,
})
}

pub fn is_null(&self) -> bool {
self.page_number == FIL_NULL
}

fn size() -> usize {
6
}
}

#[derive(Debug, Clone)]
pub struct FileListBaseNode {
pub list_len: u32,
pub first_node_page_number: u32,
pub first_node_offset: u16,
pub last_node_page_number: u32,
pub last_node_offset: u16,
pub first_node: FileAddress,
pub last_node: FileAddress,
}

impl FileListBaseNode {
Expand All @@ -16,27 +52,25 @@ impl FileListBaseNode {
}

let list_len = u32::from_be_bytes([buf[0], buf[1], buf[2], buf[3]]);
let first_node_page_number = u32::from_be_bytes([buf[4], buf[5], buf[6], buf[7]]);
let first_node_offset = u16::from_be_bytes([buf[8], buf[9]]);
let last_node_page_number = u32::from_be_bytes([buf[10], buf[11], buf[12], buf[13]]);
let last_node_offset = u16::from_be_bytes([buf[14], buf[15]]);
let first_node = FileAddress::try_from_bytes(&buf[4..10])?;
let last_node = FileAddress::try_from_bytes(&buf[10..16])?;

Ok(FileListBaseNode {
list_len,
first_node_page_number,
first_node_offset,
last_node_page_number,
last_node_offset,
first_node,
last_node
})
}

pub(crate) fn size() -> usize {
4 + FileAddress::size() + FileAddress::size()
}
}

#[derive(Debug, Clone)]
pub struct FileListInnerNode {
pub prev_node_page_number: u32,
pub prev_node_offset: u16,
pub next_node_page_number: u32,
pub next_node_offset: u16,
pub prev: FileAddress,
pub next: FileAddress,
}

impl FileListInnerNode {
Expand All @@ -45,16 +79,17 @@ impl FileListInnerNode {
return Err(anyhow!("Buffer is too small"));
}

let prev_node_page_number = u32::from_be_bytes([buf[0], buf[1], buf[2], buf[3]]);
let prev_node_offset = u16::from_be_bytes([buf[4], buf[5]]);
let next_node_page_number = u32::from_be_bytes([buf[6], buf[7], buf[8], buf[9]]);
let next_node_offset = u16::from_be_bytes([buf[10], buf[11]]);

let prev = FileAddress::try_from_bytes(&buf[0..6])?;
let next = FileAddress::try_from_bytes(&buf[6..12])?;

Ok(FileListInnerNode {
prev_node_page_number,
prev_node_offset,
next_node_page_number,
next_node_offset,
prev,
next
})
}

pub fn size() -> usize {
FileAddress::size() + FileAddress::size()
}
}
Loading

0 comments on commit 4f6dc32

Please sign in to comment.