Skip to content
This repository has been archived by the owner on Apr 15, 2023. It is now read-only.

Commit

Permalink
Pusing up the first page code
Browse files Browse the repository at this point in the history
  • Loading branch information
chotchki committed Sep 24, 2021
1 parent 830ac8d commit 1867d1a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/engine/io/index_formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ mod btree_branch;
pub use btree_branch::BTreeBranch;
pub use btree_branch::BTreeBranchError;

mod btree_first_page;
pub use btree_first_page::BTreeFirstPage;
pub use btree_first_page::BTreeFirstPageError;

mod btree_leaf;
pub use btree_leaf::BTreeLeaf;
pub use btree_leaf::BTreeLeafError;
Expand Down
57 changes: 57 additions & 0 deletions src/engine/io/index_formats/btree_first_page.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::engine::io::{
format_traits::{Parseable, Serializable},
page_formats::{PageOffset, PageOffsetError},
};
use bytes::BufMut;
use thiserror::Error;

/// Special page that points to where the root page of the index is really located
#[derive(Debug, PartialEq)]
pub struct BTreeFirstPage {
pub root_offset: PageOffset,
}

impl Parseable<BTreeFirstPageError> for BTreeFirstPage {
type Output = Self;
fn parse(buffer: &mut impl bytes::Buf) -> Result<Self::Output, BTreeFirstPageError> {
let root_offset = PageOffset::parse(buffer)?;
Ok(BTreeFirstPage { root_offset })
}
}

impl Serializable for BTreeFirstPage {
fn serialize(&self, buffer: &mut impl BufMut) {
self.root_offset.serialize(buffer);
}
}

#[derive(Debug, Error)]
pub enum BTreeFirstPageError {
#[error(transparent)]
PageOffsetError(#[from] PageOffsetError),
}

#[cfg(test)]
mod tests {
use bytes::BytesMut;

use crate::constants::PAGE_SIZE;

use super::*;

#[test]
fn test_roundtrip() -> Result<(), Box<dyn std::error::Error>> {
let first = BTreeFirstPage {
root_offset: PageOffset(1),
};

let mut buffer = BytesMut::with_capacity(PAGE_SIZE as usize);
first.serialize(&mut buffer);

let result = BTreeFirstPage::parse(&mut buffer)?;

assert_eq!(first, result);

Ok(())
}
}

0 comments on commit 1867d1a

Please sign in to comment.