pub struct StorageQuotaManager { /* private fields */ }Expand description
Thread-safe manager that tracks per-namespace storage quotas.
§Example
use ipfrs_storage::quota_manager::{StorageQuotaManager, QuotaPolicy};
let mgr = StorageQuotaManager::new(0.8);
mgr.register_namespace("ns1".to_owned(), 1024, QuotaPolicy::HardLimit).unwrap();
mgr.check_write("ns1", 512).unwrap();
mgr.record_write("ns1", 512).unwrap();
let stats = mgr.stats("ns1").unwrap();
assert_eq!(stats.used_bytes, 512);Implementations§
Source§impl StorageQuotaManager
impl StorageQuotaManager
Sourcepub fn new(soft_threshold: f64) -> Self
pub fn new(soft_threshold: f64) -> Self
Create a new StorageQuotaManager.
soft_threshold is a ratio in [0.0, 1.0] — namespaces whose usage
exceeds this fraction of their limit are considered “over threshold”.
The recommended default is 0.8 (80 %).
Sourcepub fn register_namespace(
&self,
namespace: String,
limit_bytes: u64,
policy: QuotaPolicy,
) -> Result<(), QuotaError>
pub fn register_namespace( &self, namespace: String, limit_bytes: u64, policy: QuotaPolicy, ) -> Result<(), QuotaError>
Register a new namespace with the given limit and policy.
Returns QuotaError::InvalidLimit if limit_bytes is 0 and
the policy is not QuotaPolicy::NoLimit.
Sourcepub fn check_write(
&self,
namespace: &str,
size_bytes: u64,
) -> Result<(), QuotaError>
pub fn check_write( &self, namespace: &str, size_bytes: u64, ) -> Result<(), QuotaError>
Check whether a write of size_bytes is permitted for namespace.
QuotaPolicy::HardLimit: returnsErr(QuotaExceeded)whenused + size > limit.QuotaPolicy::SoftLimit: logs a warning when the limit would be exceeded, but still returnsOk(()).QuotaPolicy::NoLimit: always returnsOk(()).
Returns QuotaError::NamespaceNotFound for unknown namespaces.
Sourcepub fn record_write(
&self,
namespace: &str,
size_bytes: u64,
) -> Result<(), QuotaError>
pub fn record_write( &self, namespace: &str, size_bytes: u64, ) -> Result<(), QuotaError>
Record that size_bytes have been successfully written to namespace.
Atomically increments used_bytes and block_count.
Sourcepub fn record_delete(&self, namespace: &str, size_bytes: u64)
pub fn record_delete(&self, namespace: &str, size_bytes: u64)
Record that size_bytes have been deleted from namespace.
Uses saturating subtraction so that underflow is safe.
block_count is also decremented using saturating subtraction.
Sourcepub fn stats(&self, namespace: &str) -> Result<QuotaStats, QuotaError>
pub fn stats(&self, namespace: &str) -> Result<QuotaStats, QuotaError>
Return a snapshot of quota statistics for namespace.
Sourcepub fn all_stats(&self) -> Vec<QuotaStats>
pub fn all_stats(&self) -> Vec<QuotaStats>
Return snapshots for all registered namespaces, sorted by namespace name.
Sourcepub fn namespaces_over_threshold(&self) -> Vec<String>
pub fn namespaces_over_threshold(&self) -> Vec<String>
Return namespaces whose usage ratio exceeds self.soft_threshold.
Sourcepub fn reset_namespace(&self, namespace: &str) -> Result<(), QuotaError>
pub fn reset_namespace(&self, namespace: &str) -> Result<(), QuotaError>
Zero out used_bytes and block_count for namespace.
Sourcepub fn remove_namespace(&self, namespace: &str) -> bool
pub fn remove_namespace(&self, namespace: &str) -> bool
Remove namespace from the manager.
Returns true if the namespace existed, false otherwise.
Auto Trait Implementations§
impl !Freeze for StorageQuotaManager
impl RefUnwindSafe for StorageQuotaManager
impl Send for StorageQuotaManager
impl Sync for StorageQuotaManager
impl Unpin for StorageQuotaManager
impl UnsafeUnpin for StorageQuotaManager
impl UnwindSafe for StorageQuotaManager
Blanket Implementations§
impl<T> Allocation for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more