Skip to content

Commit

Permalink
optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennenMM7 committed Oct 20, 2023
1 parent 67a3b92 commit 968c345
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 45 deletions.
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ exclude = [
]

[dependencies]
libc = "0.2"
memmap2 = "0.7"
page_size = "0.5"
fs2 = "0.4"
sha3 = "0.10"
bytes = "1"
bumpalo = "3.12.0"
libc = "0.2.149"
memmap2 = "0.9.0"
page_size = "0.6.0"
fs4 = "0.7.0"
bytes = "1.5.0"
bumpalo = "3.14.0"
fnv = "1.0.7"

[dev-dependencies]
bytes = { version = "1", features = ["serde"] }
Expand Down
2 changes: 1 addition & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
sync::{Arc, Mutex, RwLock},
};

use fs2::FileExt;
use fs4::FileExt;
use memmap2::Mmap;
use page_size::get as get_page_size;

Expand Down
56 changes: 19 additions & 37 deletions src/meta.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
use std::io::Write;

// use std::mem::size_of;
use bytes::BufMut;
use sha3::{Digest, Sha3_256};

use fnv::FnvHasher;
use std::hash::Hasher;
use crate::{bucket::BucketMeta, page::PageID};

// const META_SIZE: usize = size_of::<Meta>();

#[repr(C)]
#[derive(Debug, Clone)]
pub(crate) struct Meta {
Expand All @@ -19,41 +13,32 @@ pub(crate) struct Meta {
pub(crate) num_pages: PageID,
pub(crate) freelist_page: PageID,
pub(crate) tx_id: u64,
pub(crate) hash: [u8; 32],
pub(crate) hash: u64,
}

impl Meta {
pub(crate) fn valid(&self) -> bool {
self.hash == self.hash_self()
}

pub(crate) fn hash_self(&self) -> [u8; 32] {
let mut hash_result: [u8; 32] = [0; 32];
let mut hasher = Sha3_256::new();
hasher.update(self.bytes());
let hash = hasher.finalize();
assert_eq!(hash.len(), 32);
hash_result.copy_from_slice(&hash[..]);
hash_result
}
pub(crate) fn hash_self(&self) -> u64 {
let mut hasher = FnvHasher::default();

fn bytes(&self) -> bytes::Bytes {
let buf = bytes::BytesMut::new();
let mut w = buf.writer();
let _ = w.write(&self.meta_page.to_be_bytes());
let _ = w.write(&self.magic.to_be_bytes());
let _ = w.write(&self.version.to_be_bytes());
let _ = w.write(&self.pagesize.to_be_bytes());
let _ = w.write(&self.root.root_page.to_be_bytes());
let _ = w.write(&self.root.next_int.to_be_bytes());
let _ = w.write(&self.num_pages.to_be_bytes());
let _ = w.write(&self.freelist_page.to_be_bytes());
let _ = w.write(&self.tx_id.to_be_bytes());
hasher.write(&self.meta_page.to_be_bytes());
hasher.write(&self.magic.to_be_bytes());
hasher.write(&self.version.to_be_bytes());
hasher.write(&self.pagesize.to_be_bytes());
hasher.write(&self.root.root_page.to_be_bytes());
hasher.write(&self.root.next_int.to_be_bytes());
hasher.write(&self.num_pages.to_be_bytes());
hasher.write(&self.freelist_page.to_be_bytes());
hasher.write(&self.tx_id.to_be_bytes());

w.into_inner().freeze()
hasher.finish()
}
}


#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -72,20 +57,17 @@ mod tests {
num_pages: 13,
freelist_page: 3,
tx_id: 8,
hash: [0; 32],
hash: 64,
};

assert!(!meta.valid());
meta.hash = meta.hash_self();
assert!(meta.valid());
assert_eq!(meta.hash, meta.hash_self());
// modify the last property before the hash
// to change the hash

meta.tx_id = 88;
assert_ne!(meta.hash, meta.hash_self());
// reset hash and make sure it is still valid

meta.hash = meta.hash_self();
assert!(meta.valid());
assert_eq!(meta.hash, meta.hash_self());
}
}

0 comments on commit 968c345

Please sign in to comment.