1use std::ops::{Deref, DerefMut};
2use std::sync::atomic::{AtomicU64, Ordering};
3use std::sync::Arc;
4
5use async_trait::async_trait;
6use mf_model::mark::Mark;
7use mf_model::node_type::NodeEnum;
8use mf_model::types::NodeId;
9use mf_transform::TransformResult;
10use serde_json::Value;
11
12use super::state::State;
13use mf_model::node_pool::NodePool;
14use mf_transform::attr_step::AttrStep;
15use mf_transform::node_step::{AddNodeStep, RemoveNodeStep};
16use mf_transform::mark_step::{AddMarkStep, RemoveMarkStep};
17use mf_transform::transform::{Transform};
18use std::fmt::Debug;
19
20static IDS: AtomicU64 = AtomicU64::new(1);
21pub fn get_transaction_id() -> u64 {
22 IDS.fetch_add(1, Ordering::SeqCst)
24}
25
26#[async_trait]
29pub trait Command: Send + Sync + Debug {
30 async fn execute(
31 &self,
32 tr: &mut Transaction,
33 ) -> TransformResult<()>;
34 fn name(&self) -> String;
35}
36#[derive(Debug, Clone)]
38pub struct Transaction {
39 pub meta: im::HashMap<String, Arc<dyn std::any::Any>>,
41 pub id: u64,
43 transform: Transform,
44}
45unsafe impl Send for Transaction {}
46unsafe impl Sync for Transaction {}
47impl Deref for Transaction {
48 type Target = Transform;
49
50 fn deref(&self) -> &Self::Target {
51 &self.transform
52 }
53}
54
55impl DerefMut for Transaction {
56 fn deref_mut(&mut self) -> &mut Self::Target {
57 &mut self.transform
58 }
59}
60
61impl Transaction {
62 pub fn new(state: &State) -> Self {
66 let node = state.doc();
67 let schema = state.schema();
68 Transaction {
69 meta: im::HashMap::new(),
70 id: get_transaction_id(),
71 transform: Transform::new(node, schema),
72 }
73 }
74 pub fn merge(
75 &mut self,
76 other: &mut Self,
77 ) {
78 let steps_to_apply: Vec<_> = other.steps.iter().cloned().collect();
80 if let Err(e) = self.apply_steps_batch(steps_to_apply) {
81 eprintln!("批量应用步骤失败: {}", e);
82 }
83 }
84 pub fn doc(&self) -> Arc<NodePool> {
86 self.transform.doc()
87 }
88 pub fn set_node_attribute(
92 &mut self,
93 id: String,
94 values: im::HashMap<String, Value>,
95 ) -> TransformResult<()> {
96 self.step(Arc::new(AttrStep::new(id, values)))?;
97 Ok(())
98 }
99 pub fn add_node(
103 &mut self,
104 parent_id: NodeId,
105 nodes: Vec<NodeEnum>,
106 ) -> TransformResult<()> {
107 self.step(Arc::new(AddNodeStep::new(parent_id, nodes)))?;
108 Ok(())
109 }
110 pub fn remove_node(
114 &mut self,
115 parent_id: NodeId,
116 node_ids: Vec<NodeId>,
117 ) -> TransformResult<()> {
118 self.step(Arc::new(RemoveNodeStep::new(parent_id, node_ids)))?;
119 Ok(())
120 }
121 pub fn add_mark(
125 &mut self,
126 id: NodeId,
127 marks: Vec<Mark>,
128 ) -> TransformResult<()> {
129 self.step(Arc::new(AddMarkStep::new(id, marks)))?;
130 Ok(())
131 }
132 pub fn remove_mark(
136 &mut self,
137 id: NodeId,
138 mark_types: Vec<String>,
139 ) -> TransformResult<()> {
140 self.step(Arc::new(RemoveMarkStep::new(id, mark_types)))?;
141 Ok(())
142 }
143 pub fn set_meta<K, T: std::any::Any>(
147 &mut self,
148 key: K,
149 value: T,
150 ) -> &mut Self
151 where
152 K: Into<String>,
153 {
154 let key_str = key.into();
155 self.meta.insert(key_str, Arc::new(value));
156 self
157 }
158 pub fn get_meta<T: 'static>(
162 &self,
163 key: &str,
164 ) -> Option<&Arc<T>> {
165 self.meta.get(key)?.downcast_ref::<Arc<T>>()
166 }
167}