|
| 1 | +use crate::client_folder::workspace_node::WorkspaceNode; |
| 2 | +use crate::errors::{CollaborateError, CollaborateResult}; |
| 3 | +use folder_rev_model::{AppRevision, ViewRevision, WorkspaceRevision}; |
| 4 | +use lib_ot::core::{ |
| 5 | + AttributeEntry, AttributeHashMap, AttributeValue, Changeset, Node, NodeDataBuilder, NodeOperation, NodeTree, Path, |
| 6 | + Transaction, |
| 7 | +}; |
| 8 | +use parking_lot::RwLock; |
| 9 | +use std::string::ToString; |
| 10 | +use std::sync::Arc; |
| 11 | + |
| 12 | +pub type AtomicNodeTree = RwLock<NodeTree>; |
| 13 | + |
| 14 | +pub struct FolderNodePad { |
| 15 | + tree: Arc<AtomicNodeTree>, |
| 16 | + workspaces: Vec<Arc<WorkspaceNode>>, |
| 17 | + trash: Vec<Arc<TrashNode>>, |
| 18 | +} |
| 19 | + |
| 20 | +impl FolderNodePad { |
| 21 | + pub fn new() -> Self { |
| 22 | + Self::default() |
| 23 | + } |
| 24 | + |
| 25 | + pub fn get_workspace(&self, workspace_id: &str) -> Option<&Arc<WorkspaceNode>> { |
| 26 | + self.workspaces.iter().find(|workspace| workspace.id == workspace_id) |
| 27 | + } |
| 28 | + |
| 29 | + pub fn get_mut_workspace(&mut self, workspace_id: &str) -> Option<&mut Arc<WorkspaceNode>> { |
| 30 | + self.workspaces |
| 31 | + .iter_mut() |
| 32 | + .find(|workspace| workspace.id == workspace_id) |
| 33 | + } |
| 34 | + |
| 35 | + pub fn remove_workspace(&mut self, workspace_id: &str) { |
| 36 | + if let Some(workspace) = self.workspaces.iter().find(|workspace| workspace.id == workspace_id) { |
| 37 | + let mut nodes = vec![]; |
| 38 | + let workspace_node = self.tree.read().get_node_data_at_path(&workspace.path); |
| 39 | + debug_assert!(workspace_node.is_some()); |
| 40 | + |
| 41 | + if let Some(node_data) = workspace_node { |
| 42 | + nodes.push(node_data); |
| 43 | + } |
| 44 | + let delete_operation = NodeOperation::Delete { |
| 45 | + path: workspace.path.clone(), |
| 46 | + nodes, |
| 47 | + }; |
| 48 | + let _ = self.tree.write().apply_op(delete_operation); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + pub fn add_workspace(&mut self, revision: WorkspaceRevision) -> CollaborateResult<()> { |
| 53 | + let mut transaction = Transaction::new(); |
| 54 | + let workspace_node = WorkspaceNode::from_workspace_revision( |
| 55 | + &mut transaction, |
| 56 | + revision, |
| 57 | + self.tree.clone(), |
| 58 | + workspaces_path().clone_with(self.workspaces.len()), |
| 59 | + )?; |
| 60 | + let _ = self.tree.write().apply_transaction(transaction)?; |
| 61 | + self.workspaces.push(Arc::new(workspace_node)); |
| 62 | + Ok(()) |
| 63 | + } |
| 64 | + |
| 65 | + pub fn to_json(&self, pretty: bool) -> CollaborateResult<String> { |
| 66 | + self.tree |
| 67 | + .read() |
| 68 | + .to_json(pretty) |
| 69 | + .map_err(|e| CollaborateError::serde().context(e)) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +fn folder_path() -> Path { |
| 74 | + vec![0].into() |
| 75 | +} |
| 76 | + |
| 77 | +fn workspaces_path() -> Path { |
| 78 | + folder_path().clone_with(0) |
| 79 | +} |
| 80 | + |
| 81 | +fn trash_path() -> Path { |
| 82 | + folder_path().clone_with(1) |
| 83 | +} |
| 84 | + |
| 85 | +pub fn get_attributes(tree: Arc<AtomicNodeTree>, path: &Path) -> Option<AttributeHashMap> { |
| 86 | + tree.read() |
| 87 | + .get_node_at_path(&path) |
| 88 | + .and_then(|node| Some(node.attributes.clone())) |
| 89 | +} |
| 90 | + |
| 91 | +pub fn get_attributes_value(tree: Arc<AtomicNodeTree>, path: &Path, key: &str) -> Option<AttributeValue> { |
| 92 | + tree.read() |
| 93 | + .get_node_at_path(&path) |
| 94 | + .and_then(|node| node.attributes.get(key).cloned()) |
| 95 | +} |
| 96 | + |
| 97 | +pub fn get_attributes_str_value(tree: Arc<AtomicNodeTree>, path: &Path, key: &str) -> Option<String> { |
| 98 | + tree.read() |
| 99 | + .get_node_at_path(&path) |
| 100 | + .and_then(|node| node.attributes.get(key).cloned()) |
| 101 | + .and_then(|value| value.str_value()) |
| 102 | +} |
| 103 | + |
| 104 | +pub fn set_attributes_str_value( |
| 105 | + tree: Arc<AtomicNodeTree>, |
| 106 | + path: &Path, |
| 107 | + key: &str, |
| 108 | + value: String, |
| 109 | +) -> CollaborateResult<()> { |
| 110 | + let old_attributes = match get_attributes(tree.clone(), path) { |
| 111 | + None => AttributeHashMap::new(), |
| 112 | + Some(attributes) => attributes, |
| 113 | + }; |
| 114 | + let mut new_attributes = old_attributes.clone(); |
| 115 | + new_attributes.insert(key, value); |
| 116 | + |
| 117 | + let update_operation = NodeOperation::Update { |
| 118 | + path: path.clone(), |
| 119 | + changeset: Changeset::Attributes { |
| 120 | + new: new_attributes, |
| 121 | + old: old_attributes, |
| 122 | + }, |
| 123 | + }; |
| 124 | + let _ = tree.write().apply_op(update_operation)?; |
| 125 | + Ok(()) |
| 126 | +} |
| 127 | + |
| 128 | +impl std::default::Default for FolderNodePad { |
| 129 | + fn default() -> Self { |
| 130 | + let workspace_node = NodeDataBuilder::new("workspaces").build(); |
| 131 | + let trash_node = NodeDataBuilder::new("trash").build(); |
| 132 | + let folder_node = NodeDataBuilder::new("folder") |
| 133 | + .add_node_data(workspace_node) |
| 134 | + .add_node_data(trash_node) |
| 135 | + .build(); |
| 136 | + |
| 137 | + let operation = NodeOperation::Insert { |
| 138 | + path: folder_path(), |
| 139 | + nodes: vec![folder_node], |
| 140 | + }; |
| 141 | + let mut tree = NodeTree::default(); |
| 142 | + let _ = tree.apply_op(operation).unwrap(); |
| 143 | + |
| 144 | + Self { |
| 145 | + tree: Arc::new(RwLock::new(tree)), |
| 146 | + workspaces: vec![], |
| 147 | + trash: vec![], |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +pub struct TrashNode { |
| 153 | + tree: Arc<AtomicNodeTree>, |
| 154 | + parent_path: Path, |
| 155 | +} |
0 commit comments