diff options
Diffstat (limited to 'database/src/table.rs')
| -rw-r--r-- | database/src/table.rs | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/database/src/table.rs b/database/src/table.rs index ba95f8e..42e03a3 100644 --- a/database/src/table.rs +++ b/database/src/table.rs @@ -4,21 +4,23 @@ Copyright (C) 2026 metamuffin <metamuffin.org> */ +use std::sync::Arc; + use crate::{ backends::{ReadTransaction, WriteTransaction}, indices::Index, }; use anyhow::Result; -use serde::{Serialize, de::DeserializeOwned}; +use jellycommon::jellyobject::{Object, ObjectBuffer}; pub type TableNum = u64; pub type RowNum = u64; -pub struct Table<T> { +pub struct Table { id: u32, - pub(crate) indices: Vec<Box<dyn Index<T>>>, + pub(crate) indices: Vec<Box<dyn Index>>, } -impl<T: Serialize + DeserializeOwned> Table<T> { +impl Table { pub fn new(id: u32) -> Self { Self { id, @@ -49,18 +51,20 @@ impl<T: Serialize + DeserializeOwned> Table<T> { Ok(id_counter) } - pub fn get(&self, db: &dyn ReadTransaction, row: RowNum) -> Result<Option<T>> { - Ok(db - .get(&self.key(row))? - .map(|v| serde_json::from_slice(&v)) - .transpose()?) + pub fn add_index<T: Index + Clone + 'static>(&mut self, index: T) -> T { + self.indices.push(Box::new(index.clone())); + index + } + pub fn get(&self, db: &dyn ReadTransaction, row: RowNum) -> Result<Option<ObjectBuffer>> { + Ok(db.get(&self.key(row))?.map(ObjectBuffer::from)) } pub fn remove(&self, db: &mut dyn WriteTransaction, row: RowNum) -> Result<bool> { let Some(entry) = self.get(db, row)? else { return Ok(false); }; + let ob = entry.as_object(); for idx in &self.indices { - idx.remove(db, row, &entry)?; + idx.remove(db, row, ob)?; } db.del(&self.key(row))?; Ok(true) |