Skip to content

Commit 01589c6

Browse files
committed
chore: compose operation
1 parent 4b654de commit 01589c6

File tree

10 files changed

+197
-182
lines changed

10 files changed

+197
-182
lines changed

frontend/rust-lib/flowy-text-block/src/event_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::entities::{EditParams, EditPayloadPB, ExportDataPB, ExportParams, ExportPayloadPB, TextBlockPB};
22
use crate::TextEditorManager;
33
use flowy_error::FlowyError;
4-
use flowy_sync::entities::text_block::{TextBlockDeltaPB, TextBlockIdPB};
4+
use flowy_sync::entities::text_block::TextBlockIdPB;
55
use lib_dispatch::prelude::{data_result, AppData, Data, DataResult};
66
use std::convert::TryInto;
77
use std::sync::Arc;

frontend/rust-lib/flowy-text-block/src/manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::entities::{EditParams, EditPayloadPB};
1+
use crate::entities::EditParams;
22
use crate::queue::TextBlockRevisionCompactor;
33
use crate::{editor::TextBlockEditor, errors::FlowyError, TextEditorCloudService};
44
use bytes::Bytes;

shared-lib/lib-ot/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition = "2018"
77

88
[dependencies]
99
bytecount = "0.6.0"
10-
serde = { version = "1.0", features = ["derive"] }
10+
serde = { version = "1.0", features = ["derive", "rc"] }
1111
#protobuf = {version = "2.18.0"}
1212
#flowy-derive = { path = "../flowy-derive" }
1313
tokio = { version = "1", features = ["sync"] }

shared-lib/lib-ot/src/core/document/node_tree.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::core::document::path::Path;
33
use crate::core::{Node, NodeBodyChangeset, NodeData, NodeOperation, OperationTransform, Transaction};
44
use crate::errors::{ErrorBuilder, OTError, OTErrorCode};
55
use indextree::{Arena, Children, FollowingSiblings, NodeId};
6+
use std::rc::Rc;
67

78
use super::NodeOperationList;
89

@@ -26,14 +27,13 @@ impl NodeTree {
2627
}
2728

2829
pub fn from_bytes(root_name: &str, bytes: Vec<u8>) -> Result<Self, OTError> {
29-
let operations = NodeOperationList::from_bytes(bytes)?.into_inner();
30+
let operations = NodeOperationList::from_bytes(bytes)?;
3031
Self::from_operations(root_name, operations)
3132
}
3233

33-
pub fn from_operations(root_name: &str, operations: Vec<NodeOperation>) -> Result<Self, OTError> {
34+
pub fn from_operations(root_name: &str, operations: NodeOperationList) -> Result<Self, OTError> {
3435
let mut node_tree = NodeTree::new(root_name);
35-
36-
for operation in operations {
36+
for operation in operations.into_inner().into_iter() {
3737
let _ = node_tree.apply_op(operation)?;
3838
}
3939
Ok(node_tree)
@@ -54,13 +54,14 @@ impl NodeTree {
5454
/// # Examples
5555
///
5656
/// ```
57+
/// use std::rc::Rc;
5758
/// use lib_ot::core::{NodeOperation, NodeTree, NodeData, Path};
5859
/// let nodes = vec![NodeData::new("text".to_string())];
5960
/// let root_path: Path = vec![0].into();
6061
/// let op = NodeOperation::Insert {path: root_path.clone(),nodes };
6162
///
6263
/// let mut node_tree = NodeTree::new("root");
63-
/// node_tree.apply_op(op).unwrap();
64+
/// node_tree.apply_op(Rc::new(op)).unwrap();
6465
/// let node_id = node_tree.node_id_at_path(&root_path).unwrap();
6566
/// let node_path = node_tree.path_from_node_id(node_id);
6667
/// debug_assert_eq!(node_path, root_path);
@@ -105,23 +106,25 @@ impl NodeTree {
105106
counter
106107
}
107108

108-
///
109+
/// Returns the note_id at the position of the tree with id note_id
109110
/// # Arguments
110111
///
111-
/// * `node_id`:
112-
/// * `index`:
112+
/// * `node_id`: the node id of the child's parent
113+
/// * `index`: index of the node in parent children list
113114
///
114115
/// returns: Option<NodeId>
115116
///
116117
/// # Examples
117118
///
118119
/// ```
120+
/// use std::rc::Rc;
119121
/// use lib_ot::core::{NodeOperation, NodeTree, NodeData, Path};
120122
/// let node_1 = NodeData::new("text".to_string());
121123
/// let inserted_path: Path = vec![0].into();
122124
///
123125
/// let mut node_tree = NodeTree::new("root");
124-
/// node_tree.apply_op(NodeOperation::Insert {path: inserted_path.clone(),nodes: vec![node_1.clone()] }).unwrap();
126+
/// let op = NodeOperation::Insert {path: inserted_path.clone(),nodes: vec![node_1.clone()] };
127+
/// node_tree.apply_op(Rc::new(op)).unwrap();
125128
///
126129
/// let node_2 = node_tree.get_node_at_path(&inserted_path).unwrap();
127130
/// assert_eq!(node_2.node_type, node_1.node_type);
@@ -137,6 +140,10 @@ impl NodeTree {
137140
None
138141
}
139142

143+
/// Returns all children whose parent node id is node_id
144+
///
145+
/// * `node_id`: the children's parent node id
146+
///
140147
pub fn children_from_node(&self, node_id: NodeId) -> Children<'_, Node> {
141148
node_id.children(&self.arena)
142149
}
@@ -159,18 +166,23 @@ impl NodeTree {
159166
node_id.following_siblings(&self.arena)
160167
}
161168

162-
pub fn apply(&mut self, transaction: Transaction) -> Result<(), OTError> {
169+
pub fn apply_transaction(&mut self, transaction: Transaction) -> Result<(), OTError> {
163170
let operations = transaction.into_operations();
164171
for operation in operations {
165172
self.apply_op(operation)?;
166173
}
167174
Ok(())
168175
}
169176

170-
pub fn apply_op(&mut self, op: NodeOperation) -> Result<(), OTError> {
177+
pub fn apply_op(&mut self, op: Rc<NodeOperation>) -> Result<(), OTError> {
178+
let op = match Rc::try_unwrap(op) {
179+
Ok(op) => op,
180+
Err(op) => op.as_ref().clone(),
181+
};
182+
171183
match op {
172184
NodeOperation::Insert { path, nodes } => self.insert_nodes(&path, nodes),
173-
NodeOperation::UpdateAttributes { path, attributes, .. } => self.update_attributes(&path, attributes),
185+
NodeOperation::UpdateAttributes { path, new, .. } => self.update_attributes(&path, new),
174186
NodeOperation::UpdateBody { path, changeset } => self.update_body(&path, changeset),
175187
NodeOperation::Delete { path, nodes } => self.delete_node(&path, nodes),
176188
}

shared-lib/lib-ot/src/core/document/operation.rs

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
use crate::core::attributes::Attributes;
22
use crate::core::document::path::Path;
3-
use crate::core::{NodeBodyChangeset, NodeData, OperationTransform};
3+
use crate::core::{NodeBodyChangeset, NodeData};
44
use crate::errors::OTError;
55
use serde::{Deserialize, Serialize};
6+
use std::rc::Rc;
67

78
#[derive(Debug, Clone, Serialize, Deserialize)]
89
#[serde(tag = "op")]
910
pub enum NodeOperation {
1011
#[serde(rename = "insert")]
1112
Insert { path: Path, nodes: Vec<NodeData> },
1213

13-
#[serde(rename = "update")]
14+
#[serde(rename = "update-attribute")]
1415
UpdateAttributes {
1516
path: Path,
16-
attributes: Attributes,
17-
#[serde(rename = "oldAttributes")]
18-
old_attributes: Attributes,
17+
new: Attributes,
18+
old: Attributes,
1919
},
2020

2121
#[serde(rename = "update-body")]
@@ -27,35 +27,6 @@ pub enum NodeOperation {
2727
Delete { path: Path, nodes: Vec<NodeData> },
2828
}
2929

30-
// impl OperationTransform for NodeOperation {
31-
// fn compose(&self, other: &Self) -> Result<Self, OTError>
32-
// where
33-
// Self: Sized,
34-
// {
35-
// match self {
36-
// NodeOperation::Insert { path, nodes } => {
37-
// let new_path = Path::transform(path, other.path(), nodes.len() as i64);
38-
// Ok((self.clone(), other.clone_with_new_path(new_path)))
39-
// }
40-
// NodeOperation::Delete { path, nodes } => {
41-
// let new_path = Path::transform(path, other.path(), nodes.len() as i64);
42-
// other.clone_with_new_path(new_path)
43-
// }
44-
// _ => other.clone(),
45-
// }
46-
// }
47-
//
48-
// fn transform(&self, other: &Self) -> Result<(Self, Self), OTError>
49-
// where
50-
// Self: Sized,
51-
// {
52-
// todo!()
53-
// }
54-
//
55-
// fn invert(&self, other: &Self) -> Self {
56-
// todo!()
57-
// }
58-
// }
5930
impl NodeOperation {
6031
pub fn get_path(&self) -> &Path {
6132
match self {
@@ -83,12 +54,12 @@ impl NodeOperation {
8354
},
8455
NodeOperation::UpdateAttributes {
8556
path,
86-
attributes,
87-
old_attributes,
57+
new: attributes,
58+
old: old_attributes,
8859
} => NodeOperation::UpdateAttributes {
8960
path: path.clone(),
90-
attributes: old_attributes.clone(),
91-
old_attributes: attributes.clone(),
61+
new: old_attributes.clone(),
62+
old: attributes.clone(),
9263
},
9364
NodeOperation::Delete { path, nodes } => NodeOperation::Insert {
9465
path: path.clone(),
@@ -101,10 +72,10 @@ impl NodeOperation {
10172
}
10273
}
10374

104-
/// Make the `other` operation to be applied to the version that has been modified.
75+
/// Make the `other` operation can be applied to the version after applying the `self` operation.
10576
/// The semantics of transform is used when editing conflicts occur, which is often determined by the version id。
106-
/// For example, if the inserted position has been acquired by others, then you need to do transform to make sure
107-
/// your position is value.
77+
/// For example, if the inserted position has been acquired by others, then it's needed to do the transform to
78+
/// make sure the inserted position is right.
10879
///
10980
/// # Arguments
11081
///
@@ -143,24 +114,30 @@ impl NodeOperation {
143114
let new_path = path.transform(other.get_path(), nodes.len());
144115
*other.get_mut_path() = new_path;
145116
}
146-
_ => {}
117+
_ => {
118+
// Only insert/delete will change the path.
119+
}
147120
}
148121
}
149122
}
150123

151124
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
152125
pub struct NodeOperationList {
153-
operations: Vec<NodeOperation>,
126+
operations: Vec<Rc<NodeOperation>>,
154127
}
155128

156129
impl NodeOperationList {
157-
pub fn into_inner(self) -> Vec<NodeOperation> {
130+
pub fn into_inner(self) -> Vec<Rc<NodeOperation>> {
158131
self.operations
159132
}
133+
134+
pub fn add_op(&mut self, operation: NodeOperation) {
135+
self.operations.push(Rc::new(operation));
136+
}
160137
}
161138

162139
impl std::ops::Deref for NodeOperationList {
163-
type Target = Vec<NodeOperation>;
140+
type Target = Vec<Rc<NodeOperation>>;
164141

165142
fn deref(&self) -> &Self::Target {
166143
&self.operations
@@ -175,13 +152,15 @@ impl std::ops::DerefMut for NodeOperationList {
175152

176153
impl std::convert::From<Vec<NodeOperation>> for NodeOperationList {
177154
fn from(operations: Vec<NodeOperation>) -> Self {
178-
Self { operations }
155+
Self::new(operations)
179156
}
180157
}
181158

182159
impl NodeOperationList {
183160
pub fn new(operations: Vec<NodeOperation>) -> Self {
184-
Self { operations }
161+
Self {
162+
operations: operations.into_iter().map(Rc::new).collect(),
163+
}
185164
}
186165

187166
pub fn from_bytes(bytes: Vec<u8>) -> Result<Self, OTError> {

0 commit comments

Comments
 (0)