use core::{num::NonZeroUsize, time::Duration};
use std::collections::BTreeMap;
use smol_bytes::Utf8Bytes;
pub(crate) mod driver;
#[cfg(feature = "async-graphql")]
pub(crate) mod dynamic;
pub(crate) mod projection;
pub(crate) mod state;
pub(crate) mod tables;
pub use driver::{DriverEngine, RegistryDriver};
#[cfg(feature = "async-graphql")]
pub(crate) use dynamic::DynRegistry;
pub use projection::RootProjection;
pub use state::{Flip, RetireOrigin, Retirement, RootAction, RootAdvance, RootState, Stamp};
pub(crate) use tables::Counting;
pub use tables::{GenerationLedger, Registries, Registry, RegistryCounts, RetiredLedger, RootKey};
pub const DEFAULT_DEBOUNCE: Duration = Duration::from_millis(300);
pub const DEFAULT_BATCH_MAX_ITEMS: NonZeroUsize = NonZeroUsize::new(10).expect("10 is not zero");
pub const DEFAULT_BATCH_MAX_WAIT: Duration = Duration::from_millis(500);
pub const DEFAULT_RETIRE_DRAIN: Duration = Duration::from_secs(30);
pub const DEFAULT_SETTLE: Duration = Duration::ZERO;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct RegistryOptions {
#[cfg_attr(
feature = "serde",
serde(default = "default_debounce", with = "humantime_serde")
)]
debounce: Duration,
#[cfg_attr(feature = "serde", serde(default = "default_batch_max_items"))]
batch_max_items: NonZeroUsize,
#[cfg_attr(
feature = "serde",
serde(default = "default_batch_max_wait", with = "humantime_serde")
)]
batch_max_wait: Duration,
#[cfg_attr(
feature = "serde",
serde(default = "default_retire_drain", with = "humantime_serde")
)]
retire_drain: Duration,
#[cfg_attr(
feature = "serde",
serde(default = "default_settle", with = "humantime_serde")
)]
settle: Duration,
#[cfg_attr(feature = "serde", serde(flatten))]
families: BTreeMap<Utf8Bytes, RegistryOverride>,
}
impl RegistryOptions {
#[inline]
#[must_use]
pub const fn new() -> Self {
Self {
debounce: DEFAULT_DEBOUNCE,
batch_max_items: DEFAULT_BATCH_MAX_ITEMS,
batch_max_wait: DEFAULT_BATCH_MAX_WAIT,
retire_drain: DEFAULT_RETIRE_DRAIN,
settle: DEFAULT_SETTLE,
families: BTreeMap::new(),
}
}
#[inline]
#[must_use]
pub const fn debounce(&self) -> Duration {
self.debounce
}
#[inline]
#[must_use]
pub const fn with_debounce(mut self, debounce: Duration) -> Self {
self.set_debounce(debounce);
self
}
#[inline]
pub const fn set_debounce(&mut self, debounce: Duration) -> &mut Self {
self.debounce = debounce;
self
}
#[inline]
#[must_use]
pub const fn batch_max_items(&self) -> NonZeroUsize {
self.batch_max_items
}
#[inline]
#[must_use]
pub const fn with_batch_max_items(mut self, batch_max_items: NonZeroUsize) -> Self {
self.set_batch_max_items(batch_max_items);
self
}
#[inline]
pub const fn set_batch_max_items(&mut self, batch_max_items: NonZeroUsize) -> &mut Self {
self.batch_max_items = batch_max_items;
self
}
#[inline]
#[must_use]
pub const fn batch_max_wait(&self) -> Duration {
self.batch_max_wait
}
#[inline]
#[must_use]
pub const fn with_batch_max_wait(mut self, batch_max_wait: Duration) -> Self {
self.set_batch_max_wait(batch_max_wait);
self
}
#[inline]
pub const fn set_batch_max_wait(&mut self, batch_max_wait: Duration) -> &mut Self {
self.batch_max_wait = batch_max_wait;
self
}
#[inline]
#[must_use]
pub const fn retire_drain(&self) -> Duration {
self.retire_drain
}
#[inline]
#[must_use]
pub const fn with_retire_drain(mut self, retire_drain: Duration) -> Self {
self.set_retire_drain(retire_drain);
self
}
#[inline]
pub const fn set_retire_drain(&mut self, retire_drain: Duration) -> &mut Self {
self.retire_drain = retire_drain;
self
}
#[inline]
#[must_use]
pub const fn settle(&self) -> Duration {
self.settle
}
#[inline]
#[must_use]
pub const fn with_settle(mut self, settle: Duration) -> Self {
self.set_settle(settle);
self
}
#[inline]
pub const fn set_settle(&mut self, settle: Duration) -> &mut Self {
self.settle = settle;
self
}
#[inline]
#[must_use]
pub fn with_family(mut self, family: impl Into<Utf8Bytes>, over: RegistryOverride) -> Self {
self.families.insert(family.into(), over);
self
}
#[inline]
pub fn families(&self) -> impl Iterator<Item = &str> {
self.families.keys().map(Utf8Bytes::as_str)
}
#[must_use]
pub fn for_family(&self, family: &str) -> Self {
let over = self.families.get(family);
Self {
debounce: over
.and_then(RegistryOverride::debounce)
.unwrap_or(self.debounce),
batch_max_items: over
.and_then(RegistryOverride::batch_max_items)
.unwrap_or(self.batch_max_items),
batch_max_wait: over
.and_then(RegistryOverride::batch_max_wait)
.unwrap_or(self.batch_max_wait),
retire_drain: over
.and_then(RegistryOverride::retire_drain)
.unwrap_or(self.retire_drain),
settle: over
.and_then(RegistryOverride::settle)
.unwrap_or(self.settle),
families: BTreeMap::new(),
}
}
}
impl Default for RegistryOptions {
#[inline]
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct RegistryOverride {
#[cfg_attr(feature = "serde", serde(default, with = "humantime_serde::option"))]
debounce: Option<Duration>,
#[cfg_attr(feature = "serde", serde(default))]
batch_max_items: Option<NonZeroUsize>,
#[cfg_attr(feature = "serde", serde(default, with = "humantime_serde::option"))]
batch_max_wait: Option<Duration>,
#[cfg_attr(feature = "serde", serde(default, with = "humantime_serde::option"))]
retire_drain: Option<Duration>,
#[cfg_attr(feature = "serde", serde(default, with = "humantime_serde::option"))]
settle: Option<Duration>,
}
impl RegistryOverride {
#[inline]
#[must_use]
pub const fn new() -> Self {
Self {
debounce: None,
batch_max_items: None,
batch_max_wait: None,
retire_drain: None,
settle: None,
}
}
#[inline]
#[must_use]
pub const fn debounce(&self) -> Option<Duration> {
self.debounce
}
#[inline]
#[must_use]
pub const fn with_debounce(mut self, debounce: Duration) -> Self {
self.set_debounce(Some(debounce));
self
}
#[inline]
pub const fn set_debounce(&mut self, debounce: Option<Duration>) -> &mut Self {
self.debounce = debounce;
self
}
#[inline]
#[must_use]
pub const fn batch_max_items(&self) -> Option<NonZeroUsize> {
self.batch_max_items
}
#[inline]
#[must_use]
pub const fn with_batch_max_items(mut self, batch_max_items: NonZeroUsize) -> Self {
self.set_batch_max_items(Some(batch_max_items));
self
}
#[inline]
pub const fn set_batch_max_items(&mut self, batch_max_items: Option<NonZeroUsize>) -> &mut Self {
self.batch_max_items = batch_max_items;
self
}
#[inline]
#[must_use]
pub const fn batch_max_wait(&self) -> Option<Duration> {
self.batch_max_wait
}
#[inline]
#[must_use]
pub const fn with_batch_max_wait(mut self, batch_max_wait: Duration) -> Self {
self.set_batch_max_wait(Some(batch_max_wait));
self
}
#[inline]
pub const fn set_batch_max_wait(&mut self, batch_max_wait: Option<Duration>) -> &mut Self {
self.batch_max_wait = batch_max_wait;
self
}
#[inline]
#[must_use]
pub const fn retire_drain(&self) -> Option<Duration> {
self.retire_drain
}
#[inline]
#[must_use]
pub const fn with_retire_drain(mut self, retire_drain: Duration) -> Self {
self.set_retire_drain(Some(retire_drain));
self
}
#[inline]
pub const fn set_retire_drain(&mut self, retire_drain: Option<Duration>) -> &mut Self {
self.retire_drain = retire_drain;
self
}
#[inline]
#[must_use]
pub const fn settle(&self) -> Option<Duration> {
self.settle
}
#[inline]
#[must_use]
pub const fn with_settle(mut self, settle: Duration) -> Self {
self.set_settle(Some(settle));
self
}
#[inline]
pub const fn set_settle(&mut self, settle: Option<Duration>) -> &mut Self {
self.settle = settle;
self
}
}
#[cfg(feature = "serde")]
fn default_debounce() -> Duration {
DEFAULT_DEBOUNCE
}
#[cfg(feature = "serde")]
fn default_batch_max_items() -> NonZeroUsize {
DEFAULT_BATCH_MAX_ITEMS
}
#[cfg(feature = "serde")]
fn default_batch_max_wait() -> Duration {
DEFAULT_BATCH_MAX_WAIT
}
#[cfg(feature = "serde")]
fn default_retire_drain() -> Duration {
DEFAULT_RETIRE_DRAIN
}
#[cfg(feature = "serde")]
fn default_settle() -> Duration {
DEFAULT_SETTLE
}
#[cfg(test)]
mod tests;