use std::cell::Cell;
use std::collections::{BTreeMap, BTreeSet};
use std::fmt;
use std::io::{self, Read};
use std::path::Path;
use serde::de::{self, DeserializeSeed, Deserializer, IgnoredAny, MapAccess, SeqAccess, Visitor};
use serde::Serialize;
use super::sync::{open_state_file, read_capped, replace_file_atomically};
pub(super) const MAX_OUTBOX_FILE_BYTES: u64 = 160 * 1024 * 1024;
pub(super) const MAX_OUTBOX_ENTRIES: usize = 2_000_000;
const MAX_PARTITIONS: usize = 1_024;
const MAX_ID_BYTES: usize = 128;
const MAX_SCOPE_BYTES: usize = 1_024;
const FORMAT_VERSION: u64 = 2;
#[derive(Serialize)]
struct PartitionV2 {
scope: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
epoch: Option<u64>,
entries: BTreeMap<String, u64>,
}
#[derive(Serialize)]
struct HandedV2 {
scope: String,
epoch: u64,
ids: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
fresh: Vec<String>,
}
fn is_zero(value: &u64) -> bool {
*value == 0
}
#[derive(Serialize)]
struct FileV2 {
v: u64,
#[serde(skip_serializing_if = "is_zero")]
high: u64,
partitions: Vec<PartitionV2>,
#[serde(skip_serializing_if = "Option::is_none")]
handed: Option<HandedV2>,
#[serde(skip_serializing_if = "Vec::is_empty")]
unproven_marks: Vec<MarkGroupV2>,
#[serde(skip_serializing_if = "Vec::is_empty")]
unproven: Vec<String>,
}
#[derive(Serialize)]
struct MarkGroupV2 {
scope: String,
epoch: u64,
ids: Vec<String>,
}
const HEAD_PROBE_BYTES: usize = 4096;
#[derive(Debug, Clone, PartialEq, Eq)]
struct Handed {
scope: String,
epoch: u64,
ids: BTreeSet<String>,
fresh: BTreeSet<String>,
}
#[derive(Debug, Default)]
pub(crate) struct Outbox {
partitions: BTreeMap<Option<String>, BTreeMap<String, u64>>,
epochs: BTreeMap<String, u64>,
high: u64,
handed: Option<Handed>,
unproven: BTreeMap<String, BTreeMap<String, u64>>,
legacy_unproven: BTreeSet<String>,
}
struct Limits {
remaining: Cell<usize>,
reason: Cell<Option<&'static str>>,
}
impl Limits {
fn new() -> Self {
Self {
remaining: Cell::new(MAX_OUTBOX_ENTRIES),
reason: Cell::new(None),
}
}
fn fail<E: de::Error>(&self, reason: &'static str) -> E {
self.reason.set(Some(reason));
E::custom(reason)
}
fn take<E: de::Error>(&self) -> Result<(), E> {
match self.remaining.get().checked_sub(1) {
Some(left) => {
self.remaining.set(left);
Ok(())
}
None => Err(self.fail("holds more entries than the cap")),
}
}
}
type Entries = BTreeMap<String, u64>;
type Partition = (Option<String>, Option<u64>, Entries);
fn insert_min<E: de::Error>(
limits: &Limits,
entries: &mut Entries,
id: String,
at: u64,
) -> Result<(), E> {
if id.is_empty() || id.len() > MAX_ID_BYTES {
return Err(limits.fail("has an invalid id"));
}
let slot = entries.entry(id).or_insert(at);
*slot = (*slot).min(at);
Ok(())
}
struct EntriesSeed<'a>(&'a Limits);
impl<'de> DeserializeSeed<'de> for EntriesSeed<'_> {
type Value = Entries;
fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<Entries, D::Error> {
deserializer.deserialize_map(EntriesVisitor(self.0))
}
}
struct EntriesVisitor<'a>(&'a Limits);
impl<'de> Visitor<'de> for EntriesVisitor<'_> {
type Value = Entries;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("a map of logical id to queued time")
}
fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Entries, A::Error> {
let mut entries = Entries::new();
while let Some((id, at)) = map.next_entry::<String, u64>()? {
self.0.take::<A::Error>()?;
insert_min::<A::Error>(self.0, &mut entries, id, at)?;
}
Ok(entries)
}
}
struct PartitionSeed<'a>(&'a Limits);
impl<'de> DeserializeSeed<'de> for PartitionSeed<'_> {
type Value = Partition;
fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<Partition, D::Error> {
deserializer.deserialize_map(PartitionVisitor(self.0))
}
}
struct PartitionVisitor<'a>(&'a Limits);
impl<'de> Visitor<'de> for PartitionVisitor<'_> {
type Value = Partition;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("an outbox partition")
}
fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Partition, A::Error> {
let mut scope: Option<String> = None;
let mut entries: Option<Entries> = None;
let mut epoch: Option<u64> = None;
while let Some(key) = map.next_key::<String>()? {
match key.as_str() {
"epoch" => epoch = map.next_value::<Option<u64>>()?,
"scope" => {
let value = map.next_value::<Option<String>>()?;
if value
.as_ref()
.is_some_and(|s| s.is_empty() || s.len() > MAX_SCOPE_BYTES)
{
return Err(self.0.fail("has an invalid scope"));
}
scope = value;
}
"entries" => entries = Some(map.next_value_seed(EntriesSeed(self.0))?),
_ => {
map.next_value::<IgnoredAny>()?;
}
}
}
match entries {
Some(entries) => Ok((scope, epoch, entries)),
None => Err(self.0.fail("has a partition without entries")),
}
}
}
struct PartitionsSeed<'a>(&'a Limits);
impl<'de> DeserializeSeed<'de> for PartitionsSeed<'_> {
type Value = Vec<Partition>;
fn deserialize<D: Deserializer<'de>>(
self,
deserializer: D,
) -> Result<Vec<Partition>, D::Error> {
deserializer.deserialize_seq(PartitionsVisitor(self.0))
}
}
struct PartitionsVisitor<'a>(&'a Limits);
impl<'de> Visitor<'de> for PartitionsVisitor<'_> {
type Value = Vec<Partition>;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("a list of outbox partitions")
}
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Vec<Partition>, A::Error> {
let mut partitions = Vec::new();
while let Some(partition) = seq.next_element_seed(PartitionSeed(self.0))? {
if partitions.len() >= MAX_PARTITIONS {
return Err(self.0.fail("has more partitions than the cap"));
}
partitions.push(partition);
}
Ok(partitions)
}
}
struct IdsSeed<'a>(&'a Limits);
impl<'de> DeserializeSeed<'de> for IdsSeed<'_> {
type Value = BTreeSet<String>;
fn deserialize<D: Deserializer<'de>>(
self,
deserializer: D,
) -> Result<BTreeSet<String>, D::Error> {
deserializer.deserialize_seq(IdsVisitor(self.0))
}
}
struct IdsVisitor<'a>(&'a Limits);
impl<'de> Visitor<'de> for IdsVisitor<'_> {
type Value = BTreeSet<String>;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("a list of logical ids")
}
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<BTreeSet<String>, A::Error> {
let mut ids = BTreeSet::new();
while let Some(id) = seq.next_element::<String>()? {
self.0.take::<A::Error>()?;
if id.is_empty() || id.len() > MAX_ID_BYTES {
return Err(self.0.fail("has an invalid id"));
}
ids.insert(id);
}
Ok(ids)
}
}
struct HandedSeed<'a>(&'a Limits);
impl<'de> DeserializeSeed<'de> for HandedSeed<'_> {
type Value = Handed;
fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<Handed, D::Error> {
deserializer.deserialize_map(HandedVisitor(self.0))
}
}
struct HandedVisitor<'a>(&'a Limits);
impl<'de> Visitor<'de> for HandedVisitor<'_> {
type Value = Handed;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("a hand-over record")
}
fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Handed, A::Error> {
let mut scope: Option<String> = None;
let mut epoch: Option<u64> = None;
let mut ids: Option<BTreeSet<String>> = None;
let mut fresh: BTreeSet<String> = BTreeSet::new();
while let Some(key) = map.next_key::<String>()? {
match key.as_str() {
"scope" => scope = Some(map.next_value::<String>()?),
"epoch" => epoch = Some(map.next_value::<u64>()?),
"ids" => ids = Some(map.next_value_seed(IdsSeed(self.0))?),
"fresh" => fresh = map.next_value_seed(IdsSeed(self.0))?,
_ => {
map.next_value::<IgnoredAny>()?;
}
}
}
match (scope, epoch, ids) {
(Some(scope), Some(epoch), Some(ids))
if !scope.is_empty() && scope.len() <= MAX_SCOPE_BYTES =>
{
fresh.retain(|id| ids.contains(id));
Ok(Handed {
scope,
epoch,
ids,
fresh,
})
}
_ => Err(self.0.fail("has an invalid hand-over record")),
}
}
}
type MarkGroup = (String, u64, BTreeSet<String>);
struct MarksSeed<'a>(&'a Limits);
impl<'de> DeserializeSeed<'de> for MarksSeed<'_> {
type Value = Vec<MarkGroup>;
fn deserialize<D: Deserializer<'de>>(
self,
deserializer: D,
) -> Result<Vec<MarkGroup>, D::Error> {
deserializer.deserialize_seq(MarksVisitor(self.0))
}
}
struct MarksVisitor<'a>(&'a Limits);
impl<'de> Visitor<'de> for MarksVisitor<'_> {
type Value = Vec<MarkGroup>;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("a list of unproven mark groups")
}
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Vec<MarkGroup>, A::Error> {
let mut groups = Vec::new();
while let Some(group) = seq.next_element_seed(MarkGroupSeed(self.0))? {
if groups.len() >= MAX_PARTITIONS {
return Err(self.0.fail("has more mark groups than the cap"));
}
groups.push(group);
}
Ok(groups)
}
}
struct MarkGroupSeed<'a>(&'a Limits);
impl<'de> DeserializeSeed<'de> for MarkGroupSeed<'_> {
type Value = MarkGroup;
fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<MarkGroup, D::Error> {
deserializer.deserialize_map(MarkGroupVisitor(self.0))
}
}
struct MarkGroupVisitor<'a>(&'a Limits);
impl<'de> Visitor<'de> for MarkGroupVisitor<'_> {
type Value = MarkGroup;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("an unproven mark group")
}
fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<MarkGroup, A::Error> {
let mut scope: Option<String> = None;
let mut epoch: Option<u64> = None;
let mut ids: Option<BTreeSet<String>> = None;
while let Some(key) = map.next_key::<String>()? {
match key.as_str() {
"scope" => scope = Some(map.next_value::<String>()?),
"epoch" => epoch = Some(map.next_value::<u64>()?),
"ids" => ids = Some(map.next_value_seed(IdsSeed(self.0))?),
_ => {
map.next_value::<IgnoredAny>()?;
}
}
}
match (scope, epoch, ids) {
(Some(scope), Some(epoch), Some(ids))
if !scope.is_empty() && scope.len() <= MAX_SCOPE_BYTES =>
{
Ok((scope, epoch, ids))
}
_ => Err(self.0.fail("has an invalid unproven mark group")),
}
}
}
struct HighProbe<'a>(&'a Cell<Option<u64>>);
impl<'de> Visitor<'de> for HighProbe<'_> {
type Value = ();
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("a deletion outbox")
}
fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<(), A::Error> {
while let Some(key) = map.next_key::<String>()? {
match key.as_str() {
"high" => {
self.0.set(Some(map.next_value::<u64>()?));
return Err(<A::Error as de::Error>::custom("high found"));
}
"partitions" | "handed" => {
return Err(<A::Error as de::Error>::custom("high absent"));
}
_ => {
map.next_value::<IgnoredAny>()?;
}
}
}
Ok(())
}
}
fn high_water_of_head(head: &[u8]) -> Option<u64> {
let found: Cell<Option<u64>> = Cell::new(None);
let mut deserializer = serde_json::Deserializer::from_slice(head);
let _ = (&mut deserializer).deserialize_map(HighProbe(&found));
found.get()
}
struct FileSeed<'a>(&'a Limits);
impl<'de> DeserializeSeed<'de> for FileSeed<'_> {
type Value = Outbox;
fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<Outbox, D::Error> {
deserializer.deserialize_map(FileVisitor(self.0))
}
}
struct FileVisitor<'a>(&'a Limits);
impl<'de> Visitor<'de> for FileVisitor<'_> {
type Value = Outbox;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("a deletion outbox")
}
fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Outbox, A::Error> {
let mut version: Option<u64> = None;
let mut partitions: Option<Vec<Partition>> = None;
let mut high: Option<u64> = None;
let mut handed: Option<Handed> = None;
let mut legacy_unproven: BTreeSet<String> = BTreeSet::new();
let mut marks: Vec<MarkGroup> = Vec::new();
let mut legacy = Entries::new();
while let Some(key) = map.next_key::<String>()? {
match key.as_str() {
"v" => version = Some(map.next_value::<u64>()?),
"high" => high = Some(map.next_value::<u64>()?),
"partitions" => partitions = Some(map.next_value_seed(PartitionsSeed(self.0))?),
"handed" => handed = Some(map.next_value_seed(HandedSeed(self.0))?),
"unproven" => legacy_unproven = map.next_value_seed(IdsSeed(self.0))?,
"unproven_marks" => marks = map.next_value_seed(MarksSeed(self.0))?,
_ => {
let at = map.next_value::<u64>()?;
self.0.take::<A::Error>()?;
insert_min::<A::Error>(self.0, &mut legacy, key, at)?;
}
}
}
let mut outbox = Outbox::default();
if version.is_none() && partitions.is_none() {
if high.is_some()
|| handed.is_some()
|| !legacy_unproven.is_empty()
|| !marks.is_empty()
{
return Err(self.0.fail("has an unsupported version"));
}
outbox.merge(None, None, legacy);
return Ok(outbox);
}
match (version, partitions) {
(Some(FORMAT_VERSION), Some(partitions)) if legacy.is_empty() => {
let mut seen = high.unwrap_or(0);
if let Some(held) = &handed {
seen = seen.max(held.epoch);
}
for (scope, epoch, entries) in partitions {
if let Some(epoch) = epoch {
seen = seen.max(epoch);
}
outbox.merge(scope, epoch, entries);
}
outbox.high = seen;
outbox.handed = handed;
outbox.legacy_unproven = legacy_unproven;
for (scope, epoch, ids) in marks {
let slot = outbox.unproven.entry(scope).or_default();
for id in ids {
let at = slot.entry(id).or_insert(epoch);
*at = (*at).min(epoch);
}
}
Ok(outbox)
}
_ => Err(self.0.fail("has an unsupported version")),
}
}
}
impl Outbox {
pub(crate) fn load(path: &Path) -> Result<Self, String> {
let bytes = match read_capped(path, MAX_OUTBOX_FILE_BYTES) {
Ok(bytes) => bytes,
Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(Self::default()),
Err(error) => return Err(format!("read deletion outbox: {error}")),
};
let limits = Limits::new();
let refused = |error: serde_json::Error| match limits.reason.get() {
Some(reason) => format!("deletion outbox {reason}"),
None => format!("parse deletion outbox: {:?}", error.classify()),
};
let mut deserializer = serde_json::Deserializer::from_slice(&bytes);
let outbox = FileSeed(&limits)
.deserialize(&mut deserializer)
.map_err(&refused)?;
deserializer.end().map_err(&refused)?;
Ok(outbox)
}
pub(crate) fn save(&self, path: &Path) -> Result<(), String> {
if self.counted_entries() > MAX_OUTBOX_ENTRIES {
return Err("deletion outbox holds more entries than the cap; not saved".to_string());
}
if self
.partitions
.values()
.filter(|entries| !entries.is_empty())
.count()
> MAX_PARTITIONS
{
return Err(
"deletion outbox holds more partitions than the cap; not saved".to_string(),
);
}
let file = FileV2 {
v: FORMAT_VERSION,
high: self.high,
handed: self.handed.as_ref().map(|held| HandedV2 {
scope: held.scope.clone(),
epoch: held.epoch,
ids: held.ids.iter().cloned().collect(),
fresh: held.fresh.iter().cloned().collect(),
}),
unproven_marks: self.mark_groups(),
unproven: self.legacy_unproven.iter().cloned().collect(),
partitions: self
.partitions
.iter()
.filter(|(_, entries)| !entries.is_empty())
.map(|(scope, entries)| PartitionV2 {
scope: scope.clone(),
epoch: scope.as_ref().and_then(|s| self.epochs.get(s).copied()),
entries: entries.clone(),
})
.collect(),
};
let bytes = serde_json::to_vec(&file)
.map_err(|error| format!("serialize deletion outbox: {error}"))?;
if bytes.len() as u64 > MAX_OUTBOX_FILE_BYTES {
return Err("deletion outbox is larger than its cap".to_string());
}
replace_file_atomically(path, &bytes)
.map_err(|error| format!("write deletion outbox: {error}"))
}
pub(crate) fn len(&self) -> usize {
self.partitions.values().map(BTreeMap::len).sum()
}
fn counted_entries(&self) -> usize {
let handed = self
.handed
.as_ref()
.map_or(0, |held| held.ids.len().saturating_add(held.fresh.len()));
self.len()
.saturating_add(handed)
.saturating_add(self.count_unproven())
.saturating_add(self.legacy_unproven.len())
}
fn mark_groups(&self) -> Vec<MarkGroupV2> {
let mut groups: Vec<MarkGroupV2> = Vec::new();
for (scope, ids) in &self.unproven {
let mut by_epoch: BTreeMap<u64, Vec<String>> = BTreeMap::new();
for (id, epoch) in ids {
by_epoch.entry(*epoch).or_default().push(id.clone());
}
for (epoch, ids) in by_epoch {
groups.push(MarkGroupV2 {
scope: scope.clone(),
epoch,
ids,
});
}
}
groups
}
pub(crate) fn high_water(&self) -> u64 {
self.high
}
pub(crate) fn note_epoch(&mut self, epoch: u64) -> bool {
if epoch > self.high {
self.high = epoch;
true
} else {
false
}
}
pub(crate) fn handed_at(&self, scope: &str, epoch: u64, id: &str) -> bool {
self.handed
.as_ref()
.is_some_and(|held| held.scope == scope && held.epoch == epoch && held.ids.contains(id))
}
pub(crate) fn retain_handed(&mut self, keep: impl Fn(&str) -> bool) -> bool {
let Some(held) = self.handed.as_mut() else {
return false;
};
let before = held.ids.len();
held.ids.retain(|id| keep(id));
let ids = &held.ids;
held.fresh.retain(|id| ids.contains(id));
let changed = held.ids.len() != before;
if held.ids.is_empty() {
self.handed = None;
return true;
}
changed
}
pub(crate) fn handed_fresh(&self, scope: &str, id: &str) -> bool {
self.handed
.as_ref()
.is_some_and(|held| held.scope == scope && held.fresh.contains(id))
}
pub(crate) fn mark_unproven(
&mut self,
scope: &str,
epoch: u64,
ids: &BTreeSet<String>,
) -> bool {
if ids.is_empty() || scope.is_empty() || scope.len() > MAX_SCOPE_BYTES {
return false;
}
let slot = self.unproven.entry(scope.to_string()).or_default();
let before = slot.len();
for id in ids {
slot.entry(id.clone()).or_insert(epoch);
}
slot.len() != before
}
pub(crate) fn unproven_among(&self, scope: &str, ids: &BTreeSet<String>) -> BTreeSet<String> {
let scoped = self.unproven.get(scope);
ids.iter()
.filter(|id| {
scoped.is_some_and(|marks| marks.contains_key(*id))
|| self.legacy_unproven.contains(*id)
})
.cloned()
.collect()
}
pub(crate) fn count_unproven(&self) -> usize {
self.unproven.values().map(BTreeMap::len).sum()
}
#[allow(dead_code)]
pub(crate) fn count_legacy_unproven(&self) -> usize {
self.legacy_unproven.len()
}
pub(crate) fn hand_over(
&mut self,
scope: &str,
epoch: u64,
ids: &BTreeSet<String>,
fresh: &BTreeSet<String>,
) -> Result<(), String> {
if epoch < self.high {
return Err("deletion outbox holds a newer consent epoch".to_string());
}
if scope.is_empty() || scope.len() > MAX_SCOPE_BYTES {
return Err("deletion outbox: invalid scope".to_string());
}
let same = self
.handed
.as_ref()
.is_some_and(|held| held.scope == scope && held.epoch == epoch);
if !same {
self.handed = None;
}
let held = self.handed.get_or_insert_with(|| Handed {
scope: scope.to_string(),
epoch,
ids: BTreeSet::new(),
fresh: BTreeSet::new(),
});
for id in ids {
if id.is_empty() || id.len() > MAX_ID_BYTES {
return Err("deletion outbox: invalid id".to_string());
}
if held.ids.len() >= MAX_OUTBOX_ENTRIES && !held.ids.contains(id) {
return Err("deletion hand-over record is full".to_string());
}
if held.ids.insert(id.clone()) && fresh.contains(id) {
held.fresh.insert(id.clone());
}
}
self.high = self.high.max(epoch);
Ok(())
}
pub(crate) fn peek_high_water(path: &Path) -> Result<u64, String> {
let file = match open_state_file(path) {
Ok(file) => file,
Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(0),
Err(error) => return Err(format!("read deletion outbox: {error}")),
};
let mut head: Vec<u8> = Vec::with_capacity(HEAD_PROBE_BYTES);
file.take(HEAD_PROBE_BYTES as u64)
.read_to_end(&mut head)
.map_err(|error| format!("read deletion outbox: {error}"))?;
if let Some(high) = high_water_of_head(&head) {
return Ok(high);
}
Self::load(path).map(|outbox| outbox.high_water())
}
fn merge(&mut self, scope: Option<String>, epoch: Option<u64>, entries: Entries) {
if entries.is_empty() {
return;
}
if let Some(name) = &scope {
let occupied = self.partitions.get(&scope).is_some_and(|e| !e.is_empty());
match (occupied, self.epochs.get(name).copied(), epoch) {
(false, _, Some(e)) => {
self.epochs.insert(name.clone(), e);
}
(true, Some(a), Some(b)) if a == b => {}
_ => {
self.epochs.remove(name);
}
}
}
let slot = self.partitions.entry(scope).or_default();
for (id, at) in entries {
let entry = slot.entry(id).or_insert(at);
*entry = (*entry).min(at);
}
}
pub(crate) fn add(
&mut self,
scope: Option<&str>,
epoch: u64,
id: &str,
at: u64,
) -> Result<(), String> {
if let Some(name) = scope {
if epoch < self.high {
return Err("deletion outbox holds a newer consent epoch".to_string());
}
match self.epochs.get(name).copied() {
Some(held) if held == epoch => {}
Some(held) if held > epoch => {
return Err("deletion outbox holds a newer consent epoch".to_string());
}
_ => {
self.demote(name);
self.epochs.insert(name.to_string(), epoch);
}
}
self.high = self.high.max(epoch);
}
let exists = self
.partitions
.get(&scope.map(str::to_string))
.is_some_and(|entries| entries.contains_key(id));
if !exists && self.len() >= MAX_OUTBOX_ENTRIES {
return Err("deletion outbox is full".to_string());
}
let entry = self
.partitions
.entry(scope.map(str::to_string))
.or_default()
.entry(id.to_string())
.or_insert(at);
*entry = (*entry).min(at);
Ok(())
}
fn demote(&mut self, scope: &str) -> usize {
self.epochs.remove(scope);
let Some(entries) = self.partitions.remove(&Some(scope.to_string())) else {
return 0;
};
let count = entries.len();
self.merge(None, None, entries);
count
}
pub(crate) fn fence(&mut self, current: u64) -> usize {
let stale: Vec<String> = self
.partitions
.iter()
.filter_map(|(key, entries)| {
let name = key.as_ref()?;
(!entries.is_empty() && self.epochs.get(name).copied() != Some(current))
.then(|| name.clone())
})
.collect();
stale.iter().map(|name| self.demote(name)).sum()
}
fn scoped(&self, scope: &str, epoch: u64) -> Option<&Entries> {
if self.epochs.get(scope).copied() != Some(epoch) {
return None;
}
self.partitions.get(&Some(scope.to_string()))
}
pub(crate) fn eligible(&self, scope: &str, epoch: u64, limit: usize) -> BTreeSet<String> {
self.scoped(scope, epoch)
.map(|entries| entries.keys().take(limit).cloned().collect())
.unwrap_or_default()
}
pub(crate) fn held_ids(&self, scope: &str, epoch: u64) -> BTreeSet<String> {
self.scoped(scope, epoch)
.map(|entries| entries.keys().cloned().collect())
.unwrap_or_default()
}
pub(crate) fn count_for(&self, scope: &str) -> usize {
self.partitions
.get(&Some(scope.to_string()))
.map_or(0, BTreeMap::len)
}
pub(crate) fn count_unverified(&self) -> usize {
self.partitions.get(&None).map_or(0, BTreeMap::len)
}
pub(crate) fn count_other_accounts(&self, scope: Option<&str>) -> usize {
self.partitions
.iter()
.filter(|(key, _)| key.is_some() && key.as_deref() != scope)
.map(|(_, entries)| entries.len())
.sum()
}
pub(crate) fn promote(&mut self, scope: &str, epoch: u64, proven: &BTreeSet<String>) -> usize {
let Some(unverified) = self.partitions.get_mut(&None) else {
return 0;
};
let mut moved: Entries = Entries::new();
for id in proven {
if let Some(at) = unverified.remove(id) {
moved.insert(id.clone(), at);
}
}
let count = moved.len();
self.merge(Some(scope.to_string()), Some(epoch), moved);
count
}
pub(crate) fn remove(&mut self, scope: &str, ids: &BTreeSet<String>) -> bool {
let mut changed = false;
if let Some(entries) = self.partitions.get_mut(&Some(scope.to_string())) {
let before = entries.len();
entries.retain(|id, _| !ids.contains(id));
changed = entries.len() != before;
}
if let Some(marks) = self.unproven.get_mut(scope) {
let before = marks.len();
marks.retain(|id, _| !ids.contains(id));
changed |= marks.len() != before;
if marks.is_empty() {
self.unproven.remove(scope);
}
}
changed
}
}