diff options
author | metamuffin <metamuffin@disroot.org> | 2025-01-29 22:34:57 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-01-29 22:34:57 +0100 |
commit | 8099c51e56b6d253c05cac9c235f52027ad736fa (patch) | |
tree | d1b8d02ef332caa407ec8937fed56b6e5d5a5d3d /base | |
parent | db511d3fe50f05329615f718515fab1b80d9e06a (diff) | |
download | jellything-8099c51e56b6d253c05cac9c235f52027ad736fa.tar jellything-8099c51e56b6d253c05cac9c235f52027ad736fa.tar.bz2 jellything-8099c51e56b6d253c05cac9c235f52027ad736fa.tar.zst |
individual immediate file import
Diffstat (limited to 'base')
-rw-r--r-- | base/src/database.rs | 83 |
1 files changed, 16 insertions, 67 deletions
diff --git a/base/src/database.rs b/base/src/database.rs index 0f18097..1f3efbf 100644 --- a/base/src/database.rs +++ b/base/src/database.rs @@ -29,6 +29,7 @@ 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_IMPORT_MTIME: TableDefinition<&[u8], u64> = TableDefinition::new("import-mtime"); #[derive(Clone)] pub struct Database { @@ -268,6 +269,21 @@ impl Database { txn.commit().unwrap(); Ok(()) } + pub fn update_node_init( + &self, + id: NodeID, + update: impl FnOnce(&mut Node) -> Result<()>, + ) -> Result<()> { + let txn = self.inner.begin_write()?; + let mut t_nodes = txn.open_table(T_NODE)?; + let mut node = t_nodes.get(id.0)?.map(|v| v.value().0).unwrap_or_default(); + update(&mut node)?; + t_nodes.insert(&id.0, Ser(node))?; + drop(t_nodes); + txn.commit()?; + + Ok(()) + } } pub struct NodeIndex { @@ -322,73 +338,6 @@ impl NodeIndex { } } -// pub trait TableExt<Key, KeyRef, Value> { -// fn get(self, db: &Database, key: KeyRef) -> anyhow::Result<Option<Value>>; -// fn insert(self, db: &Database, key: KeyRef, value: Value) -> anyhow::Result<()>; -// fn remove(self, db: &Database, key: KeyRef) -> anyhow::Result<Option<Value>>; -// } -// impl<'a, 'b, 'c, Key, Value, KeyRef> TableExt<Key, KeyRef, Value> -// for redb::TableDefinition<'a, Key, Ser<Value>> -// where -// Key: Borrow<<Key as redb::Value>::SelfType<'b>> + redb::Key, -// Value: Encode + Decode + std::fmt::Debug + Serialize + for<'x> Deserialize<'x>, -// KeyRef: Borrow<<Key as redb::Value>::SelfType<'c>>, -// { -// fn get(self, db: &Database, key: KeyRef) -> anyhow::Result<Option<Value>> { -// let txn = db.inner.begin_read()?; -// let table = txn.open_table(self)?; -// let user = table.get(key)?.map(|v| v.value().0); -// drop(table); -// Ok(user) -// } -// fn insert(self, db: &Database, key: KeyRef, value: Value) -> anyhow::Result<()> { -// let txn = db.inner.begin_write()?; -// let mut table = txn.open_table(self)?; -// table.insert(key, Ser(value))?; -// drop(table); -// txn.commit()?; -// Ok(()) -// } -// fn remove(self, db: &Database, key: KeyRef) -> anyhow::Result<Option<Value>> { -// let txn = db.inner.begin_write()?; -// let mut table = txn.open_table(self)?; -// let prev = table.remove(key)?.map(|v| v.value().0); -// drop(table); -// txn.commit()?; -// Ok(prev) -// } -// } - -// pub trait TableIterExt< -// 'a, -// Key: redb::redb::Key + 'static, -// Value: redb::redb::Value + 'static, -// F: FnOnce(&redb::Range<'a, Key, Value>) -> anyhow::Result<T>, -// T: 'static, -// > -// { -// fn iter(self, db: &'a DataAcid, f: F) -> anyhow::Result<T>; -// } -// impl<'a, Key, Value, F, T> TableIterExt<'a, Key, Value, F, T> -// for TableDefinition<'static, Key, Value> -// where -// Key: redb::redb::Key, -// Value: redb::redb::Value, -// F: FnOnce(&redb::Range<'a, Key, Value>) -> anyhow::Result<T>, -// T: 'static, -// { -// fn iter(self, db: &DataAcid, f: F) -> anyhow::Result<T> { -// let txn = db.begin_read()?; -// let table = txn.open_table(self)?; -// let iter = table.iter()?; -// let ret = f(&iter)?; -// drop(iter); -// drop(table); -// drop(txn); -// Ok(ret) -// } -// } - #[derive(Debug)] #[cfg(not(feature = "db_json"))] pub struct Ser<T>(pub T); |