From b2371f467f32b67dabe599619d01d3f5d4f91403 Mon Sep 17 00:00:00 2001 From: Avril Date: Sat, 6 Feb 2021 18:19:20 +0000 Subject: [PATCH] move cache to own mod --- src/{data.rs => data/cache.rs} | 79 -------------------------------- src/data/mod.rs | 83 ++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 79 deletions(-) rename src/{data.rs => data/cache.rs} (66%) create mode 100644 src/data/mod.rs diff --git a/src/data.rs b/src/data/cache.rs similarity index 66% rename from src/data.rs rename to src/data/cache.rs index b52cc17..6c81008 100644 --- a/src/data.rs +++ b/src/data/cache.rs @@ -1,4 +1,3 @@ -//! Datatypes for the program use super::*; use std::collections::HashMap; use std::sync::Arc; @@ -8,7 +7,6 @@ use tokio::sync::{ watch, }; use tokio::time::Duration; -use std::fs::Metadata; /// How many unprocessed insertion requests are allowed in the backlog before the insertion stream backpressures? pub const CACHE_SEND_BUFFER_SIZE: usize = 30; @@ -19,62 +17,6 @@ pub const CACHE_GATE_TIMEOUT: Duration = duration!(100 ms); /// How long should the insertion stream be forced to wait before accepting new blocks to insert. pub const CACHE_GATED_LAG: Duration = duration!(10 ms); -/// A raw file or directory inode number -/// -/// Ususally created from the `.inode()` extension method on `fs::Metadata` found in prelude. -/// Can also be created with `new()` from a `fs::Metadata` reference, or created unsafely from an arbitrary `u64` with `new_unchecked`. -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] -#[repr(transparent)] -pub struct INode(u64); - -impl INode -{ - /// Create a new `INode` wrapper from any `u64` without checking if it is a real inode. - #[inline] pub const unsafe fn new_unchecked(ino: u64) -> Self - { - Self(ino) - } - - /// Get `ino` from an `fs::Metadata` object. - #[inline] pub fn new(meta: &Metadata) -> Self - { - use std::os::unix::fs::MetadataExt as _; - Self(meta.ino()) - } - - /// Convert into raw `u64` inode number. - #[inline] pub const fn into_inner(self) -> u64 - { - self.0 - } -} - -impl<'a> From<&'a Metadata> for INode -{ - #[inline] fn from(from: &'a Metadata) -> Self - { - from.inode() - } -} - -impl From for u64 -{ - #[inline] fn from(from: INode) -> Self - { - from.0 - } -} - -/// A valid file system info -/// -/// # Note -/// This does not contains the INode of the fs object itself, that is considered that key to the table. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum FsInfo -{ - File(u64, INode), //Size, parent dir inode - Directory, -} #[derive(Debug)] struct INodeCache @@ -175,24 +117,3 @@ impl Cache } } } - -#[cfg(test)] -mod tests -{ - use super::*; - #[tokio::test] - async fn cache_insert_and_consume() - { - let mut cache = Cache::new(); - for x in 0..500 - { - cache.insert(unsafe { INode::new_unchecked(x) }, FsInfo::Directory).await; - } - let output = cache.try_complete().await.unwrap(); - assert_eq!(output.len(), 500); - for x in 0..500 - { - assert_eq!(output.get(&unsafe { INode::new_unchecked(x) }).unwrap(), &FsInfo::Directory); - } - } -} diff --git a/src/data/mod.rs b/src/data/mod.rs new file mode 100644 index 0000000..904152e --- /dev/null +++ b/src/data/mod.rs @@ -0,0 +1,83 @@ +//! Datatypes for the program +use super::*; +use std::fs::Metadata; + +mod cache; pub use cache::*; + +/// A raw file or directory inode number +/// +/// Ususally created from the `.inode()` extension method on `fs::Metadata` found in prelude. +/// Can also be created with `new()` from a `fs::Metadata` reference, or created unsafely from an arbitrary `u64` with `new_unchecked`. +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] +#[repr(transparent)] +pub struct INode(u64); + +impl INode +{ + /// Create a new `INode` wrapper from any `u64` without checking if it is a real inode. + #[inline] pub const unsafe fn new_unchecked(ino: u64) -> Self + { + Self(ino) + } + + /// Get `ino` from an `fs::Metadata` object. + #[inline] pub fn new(meta: &Metadata) -> Self + { + use std::os::unix::fs::MetadataExt as _; + Self(meta.ino()) + } + + /// Convert into raw `u64` inode number. + #[inline] pub const fn into_inner(self) -> u64 + { + self.0 + } +} + +impl<'a> From<&'a Metadata> for INode +{ + #[inline] fn from(from: &'a Metadata) -> Self + { + from.inode() + } +} + +impl From for u64 +{ + #[inline] fn from(from: INode) -> Self + { + from.0 + } +} + +/// A valid file system info +/// +/// # Note +/// This does not contains the INode of the fs object itself, that is considered that key to the table. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub enum FsInfo +{ + File(u64, INode), //Size, parent dir inode + Directory, +} + +#[cfg(test)] +mod tests +{ + use super::*; + #[tokio::test] + async fn cache_insert_and_consume() + { + let mut cache = Cache::new(); + for x in 0..500 + { + cache.insert(unsafe { INode::new_unchecked(x) }, FsInfo::Directory).await; + } + let output = cache.try_complete().await.unwrap(); + assert_eq!(output.len(), 500); + for x in 0..500 + { + assert_eq!(output.get(&unsafe { INode::new_unchecked(x) }).unwrap(), &FsInfo::Directory); + } + } +}