diff --git a/src/hashing.rs b/src/hashing.rs index bd94802..0994c9a 100644 --- a/src/hashing.rs +++ b/src/hashing.rs @@ -31,9 +31,6 @@ pub(crate) struct PartedHash { pub(crate) const INVALID_SIG: u32 = 0; -pub(crate) const USER_NAMESPACE: &[u8] = &[1]; -pub(crate) const TYPED_NAMESPACE: &[u8] = &[2]; - impl PartedHash { #[allow(dead_code)] pub const LEN: usize = size_of::(); @@ -60,28 +57,30 @@ impl PartedHash { } } - #[cfg(test)] + #[allow(dead_code)] pub fn to_u64(&self) -> u64 { ((self.shard_selector as u64) << 48) | ((self.row_selector as u64) << 32) | (self.signature as u64) } - - // pub fn from_u64(val: u64) -> Self { - // Self { - // shard_selector: (val >> 48) as u16, - // row_selector: (val >> 32) as u16, - // signature: val as u32, - // } - // } - // pub fn as_bytes(&self) -> [u8; Self::LEN] { - // self.to_u64().to_le_bytes() - // } - // pub fn from_bytes(b: &[u8]) -> Self { - // assert_eq!(b.len(), Self::LEN); - // let buf: [u8; 8] = [b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]]; - // Self::from_u64(u64::from_le_bytes(buf)) - // } + #[allow(dead_code)] + pub fn to_bytes(&self) -> [u8; Self::LEN] { + self.to_u64().to_le_bytes() + } + #[allow(dead_code)] + pub fn from_u64(val: u64) -> Self { + Self { + shard_selector: (val >> 48) as u16, + row_selector: (val >> 32) as u16, + signature: val as u32, + } + } + #[allow(dead_code)] + pub fn from_bytes(b: &[u8]) -> Self { + assert_eq!(b.len(), Self::LEN); + let buf: [u8; 8] = [b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]]; + Self::from_u64(u64::from_le_bytes(buf)) + } } #[test] diff --git a/src/shard.rs b/src/shard.rs index 4868499..3575925 100644 --- a/src/shard.rs +++ b/src/shard.rs @@ -74,10 +74,10 @@ pub(crate) struct ByHashIterator<'a> { start_idx: usize, } -type Entry = (Vec, Vec); +pub(crate) type KVPair = (Vec, Vec); impl<'a> Iterator for ByHashIterator<'a> { - type Item = Result; + type Item = Result; fn next(&mut self) -> Option { while let Some(idx) = self.row.signatures[self.start_idx..] .iter() @@ -151,16 +151,22 @@ impl Shard { Ok(()) } + // #[inline] + // fn is_special_offset(offset_and_size: u64) -> bool { + // (offset_and_size >> 62) != 0 + // } + #[inline] fn extract_offset_and_size(offset_and_size: u64) -> (usize, usize, u64) { let klen = (offset_and_size >> 48) as usize; + debug_assert_eq!(klen >> 14, 0, "attempting to read a special key"); let vlen = ((offset_and_size >> 32) & 0xffff) as usize; let offset = (offset_and_size as u32) as u64; (klen, vlen, offset) } // reading doesn't require holding any locks - we only ever extend the file, never overwrite data - pub(crate) fn read_kv(&self, offset_and_size: u64) -> Result { + fn read_kv(&self, offset_and_size: u64) -> Result { let (klen, vlen, offset) = Self::extract_offset_and_size(offset_and_size); let mut buf = vec![0u8; klen + vlen]; @@ -191,7 +197,7 @@ impl Shard { Ok(((key.len() as u64) << 48) | ((val.len() as u64) << 32) | write_offset) } - pub(crate) fn read_at(&self, row_idx: usize, entry_idx: usize) -> Option> { + pub(crate) fn read_at(&self, row_idx: usize, entry_idx: usize) -> Option> { let _guard = self.row_locks[row_idx].read().unwrap(); let row = &self.header.rows.0[row_idx]; if row.signatures[entry_idx] != INVALID_SIG { @@ -201,13 +207,15 @@ impl Shard { } } - pub(crate) fn unlocked_iter<'b>(&'b self) -> impl Iterator> + 'b { + pub(crate) fn unlocked_iter<'b>(&'b self) -> impl Iterator> + 'b { self.header.rows.0.iter().flat_map(|row| { - row.signatures - .iter() - .enumerate() - .filter_map(|(idx, &sig)| (sig != INVALID_SIG).then_some(idx)) - .map(|idx| self.read_kv(row.offsets_and_sizes[idx])) + row.signatures.iter().enumerate().filter_map(|(idx, &sig)| { + if sig == INVALID_SIG { + None + } else { + Some(self.read_kv(row.offsets_and_sizes[idx])) + } + }) }) } @@ -279,15 +287,6 @@ impl Shard { (guard, row) } - /*pub(crate) fn insert_multikey( - &self, - keys: &[&[u8]], - val: &[u8], - mode: InsertMode, - ) -> Result { - self.insert_fullkey(ph, &full_key, val, mode) - }*/ - pub(crate) fn insert( &self, ph: PartedHash, diff --git a/src/store.rs b/src/store.rs index 64e9402..59952af 100644 --- a/src/store.rs +++ b/src/store.rs @@ -8,13 +8,13 @@ use std::{ }, }; -use crate::hashing::PartedHash; -use crate::{ - hashing::USER_NAMESPACE, - shard::{Shard, NUM_ROWS, ROW_WIDTH}, -}; +use crate::shard::{Shard, NUM_ROWS, ROW_WIDTH}; +use crate::{hashing::PartedHash, shard::KVPair}; use crate::{Config, Result}; +pub(crate) const USER_NAMESPACE: &[u8] = &[1]; +pub(crate) const TYPED_NAMESPACE: &[u8] = &[2]; + /// Stats from VickyStore, mainly useful for debugging #[derive(Debug, PartialEq, Eq, Clone)] pub struct Stats { @@ -74,7 +74,7 @@ impl<'a> VickyStoreIterator<'a> { } impl<'a> Iterator for VickyStoreIterator<'a> { - type Item = Result<(Vec, Vec)>; + type Item = Result; fn next(&mut self) -> Option { let guard = self.db.shards.read().unwrap(); @@ -307,7 +307,7 @@ impl VickyStore { } #[allow(dead_code)] - pub(crate) fn get_by_hash(&self, ph: PartedHash) -> Vec, Vec)>> { + pub(crate) fn get_by_hash(&self, ph: PartedHash) -> Vec> { self.shards .read() .unwrap() diff --git a/src/typed.rs b/src/typed.rs index b319e61..86fc825 100644 --- a/src/typed.rs +++ b/src/typed.rs @@ -1,8 +1,8 @@ use std::{borrow::Borrow, marker::PhantomData, sync::Arc}; use crate::{ - hashing::TYPED_NAMESPACE, insertion::{GetOrCreateStatus, ReplaceStatus, SetStatus}, + store::TYPED_NAMESPACE, VickyStore, };