use std::time::Duration;
use serde::Serialize;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Lane {
Fast,
Bulk,
}
impl Lane {
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Fast => "fast",
Self::Bulk => "bulk",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub struct Limits {
pub max_values_fast: usize,
pub max_values: usize,
pub max_options_bytes: usize,
pub max_radius_64: u32,
pub max_radius_256: u32,
pub max_matches: usize,
pub query_time_fast_ms: u64,
pub query_time_ms: u64,
pub max_result_bytes: usize,
pub max_change_records: usize,
pub max_remove_ids: usize,
pub max_log_bytes: u64,
}
impl Limits {
#[must_use]
pub const fn new() -> Self {
Self {
max_values_fast: 1,
max_values: 5_000,
max_options_bytes: 1 << 20,
max_radius_64: 16,
max_radius_256: 96,
max_matches: 10_000,
query_time_fast_ms: 2_000,
query_time_ms: 30_000,
max_result_bytes: 32 << 20,
max_change_records: 1_000,
max_remove_ids: 10_000,
max_log_bytes: 256 << 20,
}
}
#[must_use]
pub const fn max_values(&self, lane: Lane) -> usize {
match lane {
Lane::Fast => self.max_values_fast,
Lane::Bulk => self.max_values,
}
}
#[must_use]
pub const fn query_time(&self, lane: Lane) -> Duration {
Duration::from_millis(match lane {
Lane::Fast => self.query_time_fast_ms,
Lane::Bulk => self.query_time_ms,
})
}
#[must_use]
pub const fn max_radius(&self, width: u32) -> Option<u32> {
match width {
64 => Some(self.max_radius_64),
256 => Some(self.max_radius_256),
_ => None,
}
}
#[must_use]
pub fn log_read_bound(&self) -> u64 {
let task = super::log::task_bytes(256, self.max_change_records.max(self.max_remove_ids)).unwrap_or(u64::MAX);
self.max_log_bytes.saturating_add(task)
}
pub fn validate(&self) -> Result<(), String> {
if self.max_values_fast == 0 || self.max_values == 0 {
return Err("a request must be allowed at least one value".to_owned());
}
if self.max_matches == 0 {
return Err("a query must be allowed at least one match".to_owned());
}
if self.max_radius_64 > 64 || self.max_radius_256 > 256 {
return Err("a radius bound can't be wider than its width".to_owned());
}
if self.query_time_fast_ms == 0 || self.query_time_ms == 0 {
return Err("a time budget must be above 0".to_owned());
}
if self.max_options_bytes == 0 {
return Err("`options` must be allowed at least one byte".to_owned());
}
if self.max_change_records == 0 || self.max_remove_ids == 0 {
return Err("a change task must be allowed at least one record".to_owned());
}
let records = self.max_change_records.max(self.max_remove_ids);
let task = super::log::task_bytes(256, records).ok_or_else(|| {
format!("a change task of {records} records takes more bytes of log than can be counted: lower the change bounds")
})?;
if task > self.max_log_bytes {
return Err(format!(
"the largest change task takes up to {task} bytes of log, more than the log's bound of {}",
self.max_log_bytes
));
}
let largest = self.max_values_fast.max(self.max_values);
let least = super::answer::result_bytes(256, true, largest, 1);
if least > self.max_result_bytes {
return Err(format!(
"a result of {largest} queries with one 256-bit match each takes up to {least} bytes, \
more than the result budget of {}",
self.max_result_bytes
));
}
Ok(())
}
}
impl Default for Limits {
fn default() -> Self {
Self::new()
}
}