transaction.rs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. use crate::core::attributes::Attributes;
  2. use crate::core::{NodeData, NodeOperation, NodeTree, Path};
  3. use crate::errors::OTError;
  4. use indextree::NodeId;
  5. use std::rc::Rc;
  6. use super::{NodeBodyChangeset, NodeOperationList};
  7. #[derive(Debug, Clone, Default)]
  8. pub struct Transaction {
  9. operations: NodeOperationList,
  10. }
  11. impl Transaction {
  12. pub fn new() -> Self {
  13. Self::default()
  14. }
  15. pub fn from_operations<T: Into<NodeOperationList>>(operations: T) -> Self {
  16. Self {
  17. operations: operations.into(),
  18. }
  19. }
  20. pub fn into_operations(self) -> Vec<Rc<NodeOperation>> {
  21. self.operations.into_inner()
  22. }
  23. /// Make the `other` can be applied to the version after applying the `self` transaction.
  24. ///
  25. /// The semantics of transform is used when editing conflicts occur, which is often determined by the version id。
  26. /// the operations of the transaction will be transformed into the conflict operations.
  27. pub fn transform(&self, other: &Transaction) -> Result<Transaction, OTError> {
  28. let mut new_transaction = other.clone();
  29. for other_operation in new_transaction.iter_mut() {
  30. let other_operation = Rc::make_mut(other_operation);
  31. for operation in self.operations.iter() {
  32. operation.transform(other_operation);
  33. }
  34. }
  35. Ok(new_transaction)
  36. }
  37. pub fn compose(&mut self, other: &Transaction) -> Result<(), OTError> {
  38. // For the moment, just append `other` operations to the end of `self`.
  39. for operation in other.operations.iter() {
  40. self.operations.push(operation.clone());
  41. }
  42. Ok(())
  43. }
  44. }
  45. impl std::ops::Deref for Transaction {
  46. type Target = Vec<Rc<NodeOperation>>;
  47. fn deref(&self) -> &Self::Target {
  48. &self.operations
  49. }
  50. }
  51. impl std::ops::DerefMut for Transaction {
  52. fn deref_mut(&mut self) -> &mut Self::Target {
  53. &mut self.operations
  54. }
  55. }
  56. pub struct TransactionBuilder<'a> {
  57. node_tree: &'a NodeTree,
  58. operations: NodeOperationList,
  59. }
  60. impl<'a> TransactionBuilder<'a> {
  61. pub fn new(node_tree: &'a NodeTree) -> TransactionBuilder {
  62. TransactionBuilder {
  63. node_tree,
  64. operations: NodeOperationList::default(),
  65. }
  66. }
  67. ///
  68. ///
  69. /// # Arguments
  70. ///
  71. /// * `path`: the path that is used to save the nodes
  72. /// * `nodes`: the nodes you will be save in the path
  73. ///
  74. /// # Examples
  75. ///
  76. /// ```
  77. /// // -- 0 (root)
  78. /// // 0 -- text_1
  79. /// // 1 -- text_2
  80. /// use lib_ot::core::{NodeTree, NodeData, TransactionBuilder};
  81. /// let mut node_tree = NodeTree::new("root");
  82. /// let transaction = TransactionBuilder::new(&node_tree)
  83. /// .insert_nodes_at_path(0,vec![ NodeData::new("text_1"), NodeData::new("text_2")])
  84. /// .finalize();
  85. /// node_tree.apply_transaction(transaction).unwrap();
  86. ///
  87. /// node_tree.node_id_at_path(vec![0, 0]);
  88. /// ```
  89. ///
  90. pub fn insert_nodes_at_path<T: Into<Path>>(self, path: T, nodes: Vec<NodeData>) -> Self {
  91. self.push(NodeOperation::Insert {
  92. path: path.into(),
  93. nodes,
  94. })
  95. }
  96. ///
  97. ///
  98. /// # Arguments
  99. ///
  100. /// * `path`: the path that is used to save the nodes
  101. /// * `node`: the node data will be saved in the path
  102. ///
  103. /// # Examples
  104. ///
  105. /// ```
  106. /// // 0
  107. /// // -- 0
  108. /// // |-- text
  109. /// use lib_ot::core::{NodeTree, NodeData, TransactionBuilder};
  110. /// let mut node_tree = NodeTree::new("root");
  111. /// let transaction = TransactionBuilder::new(&node_tree)
  112. /// .insert_node_at_path(0, NodeData::new("text"))
  113. /// .finalize();
  114. /// node_tree.apply_transaction(transaction).unwrap();
  115. /// ```
  116. ///
  117. pub fn insert_node_at_path<T: Into<Path>>(self, path: T, node: NodeData) -> Self {
  118. self.insert_nodes_at_path(path, vec![node])
  119. }
  120. pub fn update_attributes_at_path(mut self, path: &Path, attributes: Attributes) -> Self {
  121. match self.node_tree.get_node_at_path(path) {
  122. Some(node) => {
  123. let mut old_attributes = Attributes::new();
  124. for key in attributes.keys() {
  125. let old_attrs = &node.attributes;
  126. if let Some(value) = old_attrs.get(key.as_str()) {
  127. old_attributes.insert(key.clone(), value.clone());
  128. }
  129. }
  130. self.operations.add_op(NodeOperation::UpdateAttributes {
  131. path: path.clone(),
  132. new: attributes,
  133. old: old_attributes,
  134. });
  135. }
  136. None => tracing::warn!("Update attributes at path: {:?} failed. Node is not exist", path),
  137. }
  138. self
  139. }
  140. pub fn update_body_at_path(mut self, path: &Path, changeset: NodeBodyChangeset) -> Self {
  141. match self.node_tree.node_id_at_path(path) {
  142. Some(_) => {
  143. self.operations.add_op(NodeOperation::UpdateBody {
  144. path: path.clone(),
  145. changeset,
  146. });
  147. }
  148. None => tracing::warn!("Update attributes at path: {:?} failed. Node is not exist", path),
  149. }
  150. self
  151. }
  152. pub fn delete_node_at_path(self, path: &Path) -> Self {
  153. self.delete_nodes_at_path(path, 1)
  154. }
  155. pub fn delete_nodes_at_path(mut self, path: &Path, length: usize) -> Self {
  156. let mut node = self.node_tree.node_id_at_path(path).unwrap();
  157. let mut deleted_nodes = vec![];
  158. for _ in 0..length {
  159. deleted_nodes.push(self.get_deleted_nodes(node));
  160. node = self.node_tree.following_siblings(node).next().unwrap();
  161. }
  162. self.operations.add_op(NodeOperation::Delete {
  163. path: path.clone(),
  164. nodes: deleted_nodes,
  165. });
  166. self
  167. }
  168. fn get_deleted_nodes(&self, node_id: NodeId) -> NodeData {
  169. let node_data = self.node_tree.get_node(node_id).unwrap();
  170. let mut children = vec![];
  171. self.node_tree.children_from_node(node_id).for_each(|child_id| {
  172. children.push(self.get_deleted_nodes(child_id));
  173. });
  174. NodeData {
  175. node_type: node_data.node_type.clone(),
  176. attributes: node_data.attributes.clone(),
  177. body: node_data.body.clone(),
  178. children,
  179. }
  180. }
  181. pub fn push(mut self, op: NodeOperation) -> Self {
  182. self.operations.add_op(op);
  183. self
  184. }
  185. pub fn finalize(self) -> Transaction {
  186. Transaction::from_operations(self.operations)
  187. }
  188. }