|
| 1 | +use super::{shrink::ShrinkMap, Closable, Id, Ids, ToProto}; |
| 2 | +use std::collections::{HashMap, HashSet}; |
| 3 | +use std::ops::{Deref, DerefMut}; |
| 4 | +use std::time::{Duration, SystemTime}; |
| 5 | + |
| 6 | +pub(crate) struct IdData<T> { |
| 7 | + data: ShrinkMap<Id, (T, bool)>, |
| 8 | +} |
| 9 | + |
| 10 | +pub(crate) struct Updating<'a, T>(&'a mut (T, bool)); |
| 11 | + |
| 12 | +pub(crate) enum Include { |
| 13 | + All, |
| 14 | + UpdatedOnly, |
| 15 | +} |
| 16 | + |
| 17 | +// === impl IdData === |
| 18 | + |
| 19 | +impl<T> Default for IdData<T> { |
| 20 | + fn default() -> Self { |
| 21 | + IdData { |
| 22 | + data: ShrinkMap::<Id, (T, bool)>::new(), |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +impl<T> IdData<T> { |
| 28 | + pub(crate) fn update_or_default(&mut self, id: Id) -> Updating<'_, T> |
| 29 | + where |
| 30 | + T: Default, |
| 31 | + { |
| 32 | + Updating(self.data.entry(id).or_default()) |
| 33 | + } |
| 34 | + |
| 35 | + pub(crate) fn update(&mut self, id: &Id) -> Option<Updating<'_, T>> { |
| 36 | + self.data.get_mut(id).map(Updating) |
| 37 | + } |
| 38 | + |
| 39 | + pub(crate) fn insert(&mut self, id: Id, data: T) { |
| 40 | + self.data.insert(id, (data, true)); |
| 41 | + } |
| 42 | + |
| 43 | + pub(crate) fn since_last_update(&mut self) -> impl Iterator<Item = (&Id, &mut T)> { |
| 44 | + self.data.iter_mut().filter_map(|(id, (data, dirty))| { |
| 45 | + if *dirty { |
| 46 | + *dirty = false; |
| 47 | + Some((id, data)) |
| 48 | + } else { |
| 49 | + None |
| 50 | + } |
| 51 | + }) |
| 52 | + } |
| 53 | + |
| 54 | + pub(crate) fn all(&self) -> impl Iterator<Item = (&Id, &T)> { |
| 55 | + self.data.iter().map(|(id, (data, _))| (id, data)) |
| 56 | + } |
| 57 | + |
| 58 | + pub(crate) fn get(&self, id: &Id) -> Option<&T> { |
| 59 | + self.data.get(id).map(|(data, _)| data) |
| 60 | + } |
| 61 | + |
| 62 | + pub(crate) fn as_proto(&mut self, include: Include) -> HashMap<u64, T::Output> |
| 63 | + where |
| 64 | + T: ToProto, |
| 65 | + { |
| 66 | + match include { |
| 67 | + Include::UpdatedOnly => self |
| 68 | + .since_last_update() |
| 69 | + .map(|(id, d)| (*id, d.to_proto())) |
| 70 | + .collect(), |
| 71 | + Include::All => self.all().map(|(id, d)| (*id, d.to_proto())).collect(), |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + pub(crate) fn drop_closed<R: Closable>( |
| 76 | + &mut self, |
| 77 | + stats: &mut IdData<R>, |
| 78 | + now: SystemTime, |
| 79 | + retention: Duration, |
| 80 | + has_watchers: bool, |
| 81 | + ids: &mut Ids, |
| 82 | + ) { |
| 83 | + let _span = tracing::debug_span!( |
| 84 | + "drop_closed", |
| 85 | + entity = %std::any::type_name::<T>(), |
| 86 | + stats = %std::any::type_name::<R>(), |
| 87 | + ) |
| 88 | + .entered(); |
| 89 | + |
| 90 | + // drop closed entities |
| 91 | + tracing::trace!(?retention, has_watchers, "dropping closed"); |
| 92 | + |
| 93 | + let mut dropped_ids = HashSet::new(); |
| 94 | + stats.data.retain_and_shrink(|id, (stats, dirty)| { |
| 95 | + if let Some(closed) = stats.closed_at() { |
| 96 | + let closed_for = now.duration_since(closed).unwrap_or_default(); |
| 97 | + let should_drop = |
| 98 | + // if there are any clients watching, retain all dirty tasks regardless of age |
| 99 | + (*dirty && has_watchers) |
| 100 | + || closed_for > retention; |
| 101 | + tracing::trace!( |
| 102 | + stats.id = ?id, |
| 103 | + stats.closed_at = ?closed, |
| 104 | + stats.closed_for = ?closed_for, |
| 105 | + stats.dirty = *dirty, |
| 106 | + should_drop, |
| 107 | + ); |
| 108 | + |
| 109 | + if should_drop { |
| 110 | + dropped_ids.insert(*id); |
| 111 | + } |
| 112 | + return !should_drop; |
| 113 | + } |
| 114 | + |
| 115 | + true |
| 116 | + }); |
| 117 | + |
| 118 | + // drop closed entities which no longer have stats. |
| 119 | + self.data |
| 120 | + .retain_and_shrink(|id, (_, _)| stats.data.contains_key(id)); |
| 121 | + |
| 122 | + if !dropped_ids.is_empty() { |
| 123 | + // drop closed entities which no longer have stats. |
| 124 | + self.data |
| 125 | + .retain_and_shrink(|id, (_, _)| stats.data.contains_key(id)); |
| 126 | + ids.remove_all(&dropped_ids); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// === impl Updating === |
| 132 | + |
| 133 | +impl<'a, T> Deref for Updating<'a, T> { |
| 134 | + type Target = T; |
| 135 | + fn deref(&self) -> &Self::Target { |
| 136 | + &self.0 .0 |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +impl<'a, T> DerefMut for Updating<'a, T> { |
| 141 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 142 | + &mut self.0 .0 |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +impl<'a, T> Drop for Updating<'a, T> { |
| 147 | + fn drop(&mut self) { |
| 148 | + self.0 .1 = true; |
| 149 | + } |
| 150 | +} |
0 commit comments