| 1 | use crate::doc::DocAddr; |
| 2 | use crate::transaction::Subdocs; |
| 3 | use crate::{DeleteSet, Doc, StateVector, TransactionMut}; |
| 4 | use std::collections::HashMap; |
| 5 | |
| 6 | /// An update event passed to a callback subscribed with [Doc::observe_update_v1]/[Doc::observe_update_v2]. |
| 7 | pub struct UpdateEvent { |
| 8 | /// A binary which contains information about all inserted and deleted changes performed within |
| 9 | /// the scope of its [TransactionMut]. |
| 10 | pub update: Vec<u8>, |
| 11 | } |
| 12 | |
| 13 | impl UpdateEvent { |
| 14 | pub(crate) fn new_v1(txn: &TransactionMut) -> Self { |
| 15 | UpdateEvent { |
| 16 | update: txn.encode_update_v1(), |
| 17 | } |
| 18 | } |
| 19 | pub(crate) fn new_v2(txn: &TransactionMut) -> Self { |
| 20 | UpdateEvent { |
| 21 | update: txn.encode_update_v2(), |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | /// Holds transaction update information from a commit after state vectors have been compressed. |
| 27 | #[derive(Debug, Clone)] |
| 28 | pub struct TransactionCleanupEvent { |
| 29 | pub before_state: StateVector, |
| 30 | pub after_state: StateVector, |
| 31 | pub delete_set: DeleteSet, |
| 32 | } |
| 33 | |
| 34 | impl TransactionCleanupEvent { |
| 35 | pub fn new(txn: &TransactionMut) -> Self { |
| 36 | TransactionCleanupEvent { |
| 37 | before_state: txn.before_state.clone(), |
| 38 | after_state: txn.after_state.clone(), |
| 39 | delete_set: txn.delete_set.clone(), |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /// Event used to communicate load requests from the underlying subdocuments. |
| 45 | #[derive(Debug, Clone)] |
| 46 | pub struct SubdocsEvent { |
| 47 | pub(crate) added: HashMap<DocAddr, Doc>, |
| 48 | pub(crate) removed: HashMap<DocAddr, Doc>, |
| 49 | pub(crate) loaded: HashMap<DocAddr, Doc>, |
| 50 | } |
| 51 | |
| 52 | impl SubdocsEvent { |
| 53 | pub(crate) fn new(inner: Box<Subdocs>) -> Self { |
| 54 | SubdocsEvent { |
| 55 | added: inner.added, |
| 56 | removed: inner.removed, |
| 57 | loaded: inner.loaded, |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /// Returns an iterator over all sub-documents added to a current document within a scope of |
| 62 | /// committed transaction. |
| 63 | pub fn added(&self) -> SubdocsEventIter { |
| 64 | SubdocsEventIter(self.added.values()) |
| 65 | } |
| 66 | |
| 67 | /// Returns an iterator over all sub-documents removed from a current document within a scope of |
| 68 | /// committed transaction. |
| 69 | pub fn removed(&self) -> SubdocsEventIter { |
| 70 | SubdocsEventIter(self.removed.values()) |
| 71 | } |
| 72 | |
| 73 | /// Returns an iterator over all sub-documents living in a parent document, that have requested |
| 74 | /// to be loaded within a scope of committed transaction. |
| 75 | pub fn loaded(&self) -> SubdocsEventIter { |
| 76 | SubdocsEventIter(self.loaded.values()) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | #[repr(transparent)] |
| 81 | pub struct SubdocsEventIter<'a>(std::collections::hash_map::Values<'a, DocAddr, Doc>); |
| 82 | |
| 83 | impl<'a> Iterator for SubdocsEventIter<'a> { |
| 84 | type Item = &'a Doc; |
| 85 | |
| 86 | fn next(&mut self) -> Option<Self::Item> { |
| 87 | self.0.next() |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | impl<'a> ExactSizeIterator for SubdocsEventIter<'a> { |
| 92 | fn len(&self) -> usize { |
| 93 | self.0.len() |
| 94 | } |
| 95 | } |
| 96 | |