finding max size okay

redo-gragh
Avril 3 years ago
parent 8cbe975d50
commit 998b76cf6f
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -1,6 +1,6 @@
use super::*;
use std::collections::HashMap;
use std::path::PathBuf;
use std::path::{Path,PathBuf};
use std::borrow::Borrow;
/// Contains a graph of all paths and inodes that were successfully stat'd
@ -88,6 +88,17 @@ enum NodeKind
File(u64),
}
impl NodeKind
{
pub fn len(&self) -> Option<u64>
{
match self {
Self::File(f) => Some(*f),
Self::Directory(_, o) => *o,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct HierarchicalNode
{
@ -95,6 +106,26 @@ struct HierarchicalNode
inode: INode,
}
impl HierarchicalNode
{
#[inline(always)] pub fn len(&self) -> Option<u64>
{
self.kind.len()
}
pub fn is_dir(&self) -> bool
{
match self.kind {
NodeKind::Directory(_, _) => true,
_ => false
}
}
#[inline] pub fn is_file(&self) -> bool
{
!self.is_dir()
}
}
/// A hierarchical graph of node sizes
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HierarchicalINodeGraph
@ -210,6 +241,25 @@ impl HierarchicalINodeGraph
new
}
/// Get the max size and the path with this size
pub fn path_max_size_for(&self, kind: FsObjKind) -> Option<(&Path, u64)>
{
self.table.iter().filter_map(|(path, hi)| {
match kind {
FsObjKind::File if hi.is_file() => hi.len().map(|x| (path.as_path(), x)),
FsObjKind::Directory if hi.is_dir() => hi.len().map(|x| (path.as_path(), x)),
_ => None
}
}).max_by_key(|x| x.1)
}
/// Get the max size and the path with this size, whether it's a `Directory` or a `File`.
pub fn path_max_size(&self) -> Option<(&Path, u64)>
{
self.table.iter().filter_map(|(path, hi)| {
hi.len().map(|x| (path.as_path(), x))
}).max_by_key(|x| x.1)
}
}
impl From<INodeInfoGraph> for HierarchicalINodeGraph
@ -219,3 +269,11 @@ impl From<INodeInfoGraph> for HierarchicalINodeGraph
Self::create(from)
}
}
/// A file or directory
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Copy)]
pub enum FsObjKind
{
Directory,
File,
}

@ -13,7 +13,12 @@ pub const CACHE_GATE_TIMEOUT: Duration = duration!(100 ms);
pub const CACHE_GATED_LAG: Duration = duration!(10 ms);
mod cache; pub use cache::Cache;
pub mod graph; pub use graph::INodeInfoGraph;
pub mod graph;
pub use graph::{
INodeInfoGraph,
HierarchicalINodeGraph,
FsObjKind as FsKind,
};
/// A raw file or directory inode number
///

@ -52,6 +52,10 @@ async fn main() -> eyre::Result<()> {
graph.compute_recursive_sizes();
println!("{:?}", graph);
println!("Max size file: {:?}", graph.path_max_size_for(data::FsKind::File));
println!("Max size dir: {:?}", graph.path_max_size_for(data::FsKind::Directory));
println!("Max size all: {:?}", graph.path_max_size());
/* let max_size = graph.directories().map(|x| x.size()).max();
println!("Max size: {:?}", max_size);*/

Loading…
Cancel
Save