//! Tracked documents: the one writer of a partition and the one recorder of //! what a transaction changed. //! //! A `Docs` is a map whose every write goes through a method that remembers the //! key's value as it stood before the transaction (first touch wins). `finish` //! turns that record into the keys whose value really changed, with their old //! values; `rollback` puts every touched key back. Reads see the plain map //! through `Deref`, and there is no `DerefMut`, so a write cannot skip the log. //! The fields are private to this module for the same reason. use serde::{Serialize, Serializer}; use std::collections::BTreeMap; use std::collections::btree_map::Entry; use std::ops::Deref; /// A key whose value changed in a transaction, with the value it had before /// (`None` when the key was absent). The new value is whatever the map holds. pub(crate) type Changed = (K, Option); #[derive(Clone, Debug)] pub(crate) struct Docs { map: BTreeMap, touched: BTreeMap>, } impl Default for Docs { fn default() -> Self { Self { map: BTreeMap::new(), touched: BTreeMap::new(), } } } /// Equality is the documents, never the log: two worlds with the same content /// are equal whatever transaction state they are in. impl PartialEq for Docs { fn eq(&self, other: &Self) -> bool { self.map == other.map } } impl Eq for Docs {} impl Deref for Docs { type Target = BTreeMap; fn deref(&self) -> &BTreeMap { &self.map } } impl<'a, K: Ord + Clone, V: Clone + PartialEq> IntoIterator for &'a Docs { type Item = (&'a K, &'a V); type IntoIter = std::collections::btree_map::Iter<'a, K, V>; fn into_iter(self) -> Self::IntoIter { self.map.iter() } } /// A partition encodes as its map, so the encoded world is what it was before /// the partitions were tracked. impl Serialize for Docs { fn serialize(&self, serializer: S) -> Result { self.map.serialize(serializer) } } impl Docs { fn touch(&mut self, key: &K) { if !self.touched.contains_key(key) { self.touched.insert(key.clone(), self.map.get(key).cloned()); } } pub(crate) fn insert(&mut self, key: K, value: V) -> Option { self.touch(&key); self.map.insert(key, value) } pub(crate) fn remove(&mut self, key: &K) -> Option { self.touch(key); self.map.remove(key) } /// Touches only a key that is present: a miss writes nothing. pub(crate) fn get_mut(&mut self, key: &K) -> Option<&mut V> { if self.map.contains_key(key) { self.touch(key); } self.map.get_mut(key) } pub(crate) fn entry(&mut self, key: K) -> Entry<'_, K, V> { self.touch(&key); self.map.entry(key) } /// The keys whose value differs from the start of the transaction, with the /// value they had then. A key written back to what it was is not reported. /// The log is empty afterwards. pub(crate) fn finish(&mut self) -> Vec> { std::mem::take(&mut self.touched) .into_iter() .filter(|(key, old)| self.map.get(key) != old.as_ref()) .collect() } /// Puts every touched key back as it was and empties the log. pub(crate) fn rollback(&mut self) { for (key, old) in std::mem::take(&mut self.touched) { match old { Some(value) => { self.map.insert(key, value); } None => { self.map.remove(&key); } } } } pub(crate) fn is_clean(&self) -> bool { self.touched.is_empty() } } /// One tracked value: the same first-touch rule as a `Docs` key, for the /// world's own fields. #[derive(Clone, Debug)] pub(crate) struct Tracked { value: T, /// `None` untouched; `Some(None)` logged as absent (genesis), so `finish` /// reports it as added. old: Option>, } impl PartialEq for Tracked { fn eq(&self, other: &Self) -> bool { self.value == other.value } } impl Eq for Tracked {} impl Deref for Tracked { type Target = T; fn deref(&self) -> &T { &self.value } } impl Tracked { /// A value that did not exist before this transaction. pub(crate) fn absent(value: T) -> Self { Self { value, old: Some(None), } } pub(crate) fn get_mut(&mut self) -> &mut T { if self.old.is_none() { self.old = Some(Some(self.value.clone())); } &mut self.value } /// The old value when the value changed (`Some(None)`: it was absent). pub(crate) fn finish(&mut self) -> Option> { match self.old.take() { Some(old) if old.as_ref() != Some(&self.value) => Some(old), _ => None, } } pub(crate) fn rollback(&mut self) { if let Some(Some(old)) = self.old.take() { self.value = old; } } pub(crate) fn is_clean(&self) -> bool { self.old.is_none() } }