#[derive(Debug, Clone)]
pub struct ResourceLimits {
pub max_tuples: usize,
pub max_operations_per_batch: usize,
pub max_memory_mb: usize,
pub max_cascade_depth: usize,
pub max_facts_per_type: usize,
}
impl Default for ResourceLimits {
fn default() -> Self {
Self {
max_tuples: std::usize::MAX,
max_operations_per_batch: std::usize::MAX,
max_memory_mb: std::usize::MAX,
max_cascade_depth: std::usize::MAX,
max_facts_per_type: std::usize::MAX,
}
}
}
impl ResourceLimits {
pub fn conservative() -> Self {
Self {
max_tuples: 100_000,
max_operations_per_batch: 10_000,
max_memory_mb: 256,
max_cascade_depth: 100,
max_facts_per_type: 50_000,
}
}
pub fn aggressive() -> Self {
Self {
max_tuples: 100_000_000,
max_operations_per_batch: 1_000_000,
max_memory_mb: 8192,
max_cascade_depth: 10_000,
max_facts_per_type: 10_000_000,
}
}
#[inline]
pub fn estimate_memory_usage(&self, tuple_count: usize, node_count: usize) -> usize {
(tuple_count * 200 + node_count * 1024) / (1024 * 1024)
}
pub fn check_operation_limit(&self, current_ops: usize) -> crate::error::Result<()> {
if current_ops > self.max_operations_per_batch {
return Err(crate::error::GreynetError::resource_limit(
"operations_per_batch",
format!("Current: {}, Limit: {}", current_ops, self.max_operations_per_batch)
));
}
Ok(())
}
pub fn check_tuple_limit(&self, current_tuples: usize) -> crate::error::Result<()> {
if current_tuples > self.max_tuples {
return Err(crate::error::GreynetError::resource_limit(
"max_tuples",
format!("Current: {}, Limit: {}", current_tuples, self.max_tuples)
));
}
Ok(())
}
}