diff options
Diffstat (limited to 'base/src')
-rw-r--r-- | base/src/database.rs | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/base/src/database.rs b/base/src/database.rs index e9fe156..bec630a 100644 --- a/base/src/database.rs +++ b/base/src/database.rs @@ -10,7 +10,7 @@ use jellycommon::{ Node, NodeID, }; use log::info; -use redb::{ReadableTable, TableDefinition}; +use redb::{ReadableTable, StorageError, TableDefinition}; use std::{ fs::create_dir_all, path::Path, @@ -29,6 +29,8 @@ const T_USER_NODE: TableDefinition<(&str, [u8; 32]), Ser<NodeUserData>> = TableDefinition::new("user_node"); const T_INVITE: TableDefinition<&str, ()> = TableDefinition::new("invite"); const T_NODE: TableDefinition<[u8; 32], Ser<Node>> = TableDefinition::new("node"); +const T_NODE_CHILDREN: TableDefinition<([u8; 32], [u8; 32]), ()> = + TableDefinition::new("node_children"); const T_IMPORT_FILE_MTIME: TableDefinition<&[u8], u64> = TableDefinition::new("import_file_mtime"); #[derive(Clone)] @@ -75,11 +77,21 @@ impl Database { Ok(None) } } + pub fn get_node_children(&self, id: NodeID) -> Result<Vec<NodeID>> { + let txn = self.inner.begin_read()?; + let t_node_children = txn.open_table(T_NODE_CHILDREN)?; + Ok(t_node_children + .range((id.0, NodeID::MIN.0)..(id.0, NodeID::MAX.0))? + .map(|r| r.map(|r| NodeID(r.0.value().1))) + .collect::<Result<Vec<_>, StorageError>>()?) + } pub fn clear_nodes(&self) -> Result<()> { let txn = self.inner.begin_write()?; - let mut table = txn.open_table(T_NODE)?; - table.retain(|_, _| false)?; - drop(table); + let mut t_node = txn.open_table(T_NODE)?; + let mut t_node_children = txn.open_table(T_NODE_CHILDREN)?; + t_node.retain(|_, _| false)?; + t_node_children.retain(|_, _| false)?; + drop((t_node, t_node_children)); txn.commit()?; Ok(()) } @@ -209,7 +221,7 @@ impl Database { txn.commit()?; Ok(()) } - pub fn list_nodes_with_udata(&self, username: &str) -> Result<Vec<(Node, NodeUserData)>> { + pub fn list_nodes_with_udata(&self, username: &str) -> Result<Vec<(Arc<Node>, NodeUserData)>> { let txn = self.inner.begin_read()?; let nodes = txn.open_table(T_NODE)?; let node_users = txn.open_table(T_USER_NODE)?; @@ -217,7 +229,7 @@ impl Database { .iter()? .map(|a| { let (x, y) = a.unwrap(); - let (x, y) = (x.value().to_owned(), y.value().0); + let (x, y) = (x.value().to_owned(), Arc::new(y.value().0)); let z = node_users .get(&(username, x)) .unwrap() @@ -285,12 +297,15 @@ impl Database { ) -> Result<()> { let txn = self.inner.begin_write()?; let mut t_nodes = txn.open_table(T_NODE)?; + let mut t_node_children = txn.open_table(T_NODE_CHILDREN)?; let mut node = t_nodes.get(id.0)?.map(|v| v.value().0).unwrap_or_default(); update(&mut node)?; + for parent in &node.parents { + t_node_children.insert((parent.0, id.0), ())?; + } t_nodes.insert(&id.0, Ser(node))?; - drop(t_nodes); + drop((t_nodes, t_node_children)); txn.commit()?; - Ok(()) } pub fn get_import_file_mtime(&self, path: &Path) -> Result<Option<u64>> { |