diff options
Diffstat (limited to 'base/src/database.rs')
-rw-r--r-- | base/src/database.rs | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/base/src/database.rs b/base/src/database.rs index 407db29..32f1464 100644 --- a/base/src/database.rs +++ b/base/src/database.rs @@ -14,7 +14,8 @@ use redb::{Durability, ReadableTable, StorageError, TableDefinition}; use std::{ fs::create_dir_all, hash::{DefaultHasher, Hasher}, - path::Path, + path::{Path, PathBuf}, + str::FromStr, sync::{Arc, RwLock}, time::SystemTime, }; @@ -38,6 +39,8 @@ const T_NODE_EXTERNAL_ID: TableDefinition<(&str, &str), [u8; 32]> = TableDefinition::new("node_external_id"); const T_IMPORT_FILE_MTIME: TableDefinition<&[u8], u64> = TableDefinition::new("import_file_mtime"); const T_NODE_MTIME: TableDefinition<[u8; 32], u64> = TableDefinition::new("node_mtime"); +const T_NODE_MEDIA_PATHS: TableDefinition<([u8; 32], &str), ()> = + TableDefinition::new("node_media_paths"); #[derive(Clone)] pub struct Database { @@ -67,6 +70,7 @@ impl Database { txn.open_table(T_NODE_MTIME)?; txn.open_table(T_NODE_CHILDREN)?; txn.open_table(T_NODE_EXTERNAL_ID)?; + txn.open_table(T_NODE_MEDIA_PATHS)?; txn.open_table(T_IMPORT_FILE_MTIME)?; txn.commit()?; } @@ -123,17 +127,20 @@ impl Database { let mut t_node_children = txn.open_table(T_NODE_CHILDREN)?; let mut t_node_external_id = txn.open_table(T_NODE_EXTERNAL_ID)?; let mut t_import_file_mtime = txn.open_table(T_IMPORT_FILE_MTIME)?; + let mut t_node_media_paths = txn.open_table(T_NODE_MEDIA_PATHS)?; t_node.retain(|_, _| false)?; t_node_mtime.retain(|_, _| false)?; t_node_children.retain(|_, _| false)?; t_node_external_id.retain(|_, _| false)?; t_import_file_mtime.retain(|_, _| false)?; + t_node_media_paths.retain(|_, _| false)?; drop(( t_node, t_node_mtime, t_node_children, t_node_external_id, t_import_file_mtime, + t_node_media_paths, )); txn.set_durability(Durability::Eventual); txn.commit()?; @@ -189,6 +196,24 @@ impl Database { txn.commit()?; Ok(()) } + pub fn get_node_media_paths(&self, id: NodeID) -> Result<Vec<PathBuf>> { + let txn = self.inner.begin_read()?; + let table = txn.open_table(T_NODE_MEDIA_PATHS)?; + let mut paths = Vec::new(); + // TODO fix this + for p in table.range((id.0, "\0")..(id.0, "\x7f"))? { + paths.push(PathBuf::from_str(p?.0.value().1)?); + } + Ok(paths) + } + pub fn insert_node_media_path(&self, id: NodeID, path: &Path) -> Result<()> { + let txn = self.inner.begin_write()?; + let mut table = txn.open_table(T_NODE_MEDIA_PATHS)?; + table.insert((id.0, path.to_str().unwrap()), ())?; + drop(table); + txn.commit()?; + Ok(()) + } pub fn update_node_udata( &self, |