-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
66 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
mod disk_manager; | ||
pub mod index; | ||
pub mod index_page; | ||
mod serialize; | ||
mod table_heap; | ||
mod table_page; | ||
mod tuple; | ||
|
||
pub use disk_manager::DiskManager; | ||
pub use serialize::Serializable; | ||
pub use table_heap::{TableHeap, TableIterator}; | ||
pub use table_page::TablePage; | ||
pub use tuple::{Tuple, TupleMeta}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use crate::{BustubxError, BustubxResult}; | ||
use std::u16; | ||
|
||
pub trait Serializable: Sized { | ||
fn serialize(&self) -> BustubxResult<Vec<u8>>; | ||
|
||
fn deserialize(bytes: &[u8]) -> BustubxResult<(Self, &[u8])>; | ||
} | ||
|
||
impl Serializable for u16 { | ||
fn serialize(&self) -> BustubxResult<Vec<u8>> { | ||
Ok(self.to_be_bytes().to_vec()) | ||
} | ||
|
||
fn deserialize(bytes: &[u8]) -> BustubxResult<(Self, &[u8])> { | ||
if bytes.len() < 2 { | ||
return Err(BustubxError::Storage(format!( | ||
"bytes length {} is less than {}", | ||
bytes.len(), | ||
2 | ||
))); | ||
} | ||
let data = [bytes[0], bytes[1]]; | ||
Ok((u16::from_be_bytes(data), &bytes[2..])) | ||
} | ||
} | ||
|
||
impl Serializable for u32 { | ||
fn serialize(&self) -> BustubxResult<Vec<u8>> { | ||
Ok(self.to_ne_bytes().to_vec()) | ||
} | ||
|
||
fn deserialize(bytes: &[u8]) -> BustubxResult<(Self, &[u8])> { | ||
if bytes.len() < 4 { | ||
return Err(BustubxError::Storage(format!( | ||
"bytes length {} is less than {}", | ||
bytes.len(), | ||
4 | ||
))); | ||
} | ||
let data = [bytes[0], bytes[1], bytes[2], bytes[3]]; | ||
Ok((u32::from_ne_bytes(data), &bytes[4..])) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters