From ffa6b5c4ae2cdd3e07426ed0330f3f66e90ee57b Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 6 Jan 2026 19:10:37 +0100 Subject: tag registry --- database/src/indices/key.rs | 26 +++++++++++++------------- database/src/indices/mod.rs | 11 ++++++----- database/src/indices/order.rs | 21 +++++++++++---------- database/src/table.rs | 24 ++++++++++++++---------- 4 files changed, 44 insertions(+), 38 deletions(-) (limited to 'database/src') diff --git a/database/src/indices/key.rs b/database/src/indices/key.rs index ab38d71..2790220 100644 --- a/database/src/indices/key.rs +++ b/database/src/indices/key.rs @@ -8,18 +8,18 @@ use crate::{ backends::{ReadTransaction, WriteTransaction}, indices::Index, prefix_iterator::PrefixIterator, - table::{RowNum, Table, TableNum}, + table::{RowNum, TableNum}, }; use anyhow::Result; +use jellycommon::jellyobject::{Object, Tag}; -pub struct KeyIndex { +pub struct KeyIndex { id: TableNum, - key: fn(&T) -> &[u8], + key: Vec, } -impl KeyIndex { - pub fn new(table: &mut Table, id: TableNum, key: fn(&T) -> &[u8]) -> Self { - table.indices.push(Box::new(Self { id, key })); +impl KeyIndex { + pub fn new(id: TableNum, key: Vec) -> Self { Self { id, key } } pub fn key(&self, id: RowNum, key: &[u8]) -> Vec { @@ -43,15 +43,15 @@ impl KeyIndex { }) } } -impl Index for KeyIndex { - fn add(&self, db: &mut dyn WriteTransaction, id: RowNum, val: &T) -> Result<()> { - db.set(&self.key(id, (self.key)(val)), &[]) +impl Index for KeyIndex { + fn add(&self, db: &mut dyn WriteTransaction, id: RowNum, val: Object) -> Result<()> { + // db.set(&self.key(id, (self.key)(val)), &[]) } - fn remove(&self, db: &mut dyn WriteTransaction, id: RowNum, val: &T) -> Result<()> { - db.del(&self.key(id, (self.key)(val))) + fn remove(&self, db: &mut dyn WriteTransaction, id: RowNum, val: Object) -> Result<()> { + // db.del(&self.key(id, (self.key)(val))) } - fn compare(&self, before: &T, after: &T) -> bool { - (self.key)(before) == (self.key)(after) + fn compare(&self, before: Object, after: Object) -> bool { + // (self.key)(before) == (self.key)(after) } } diff --git a/database/src/indices/mod.rs b/database/src/indices/mod.rs index ab37589..ad3d00f 100644 --- a/database/src/indices/mod.rs +++ b/database/src/indices/mod.rs @@ -6,14 +6,15 @@ use crate::{backends::WriteTransaction, table::RowNum}; use anyhow::Result; +use jellycommon::jellyobject::Object; -pub mod order; pub mod key; +pub mod order; -pub trait Index { - fn add(&self, db: &mut dyn WriteTransaction, row: RowNum, val: &T) -> Result<()>; - fn remove(&self, db: &mut dyn WriteTransaction, row: RowNum, val: &T) -> Result<()>; - fn compare(&self, before: &T, after: &T) -> bool { +pub trait Index { + fn add(&self, db: &mut dyn WriteTransaction, row: RowNum, val: Object) -> Result<()>; + fn remove(&self, db: &mut dyn WriteTransaction, row: RowNum, val: Object) -> Result<()>; + fn compare(&self, before: Object, after: Object) -> bool { let _ = (before, after); true } diff --git a/database/src/indices/order.rs b/database/src/indices/order.rs index 9163681..911ec37 100644 --- a/database/src/indices/order.rs +++ b/database/src/indices/order.rs @@ -7,34 +7,35 @@ use crate::{ backends::WriteTransaction, indices::Index, - table::{RowNum, Table, TableNum}, + table::{RowNum, TableNum}, }; use anyhow::Result; use bytemuck::{NoUninit, bytes_of}; +use jellycommon::jellyobject::{Object, Tag}; -pub struct OrderIndex { +#[derive(Clone)] +pub struct OrderIndex { id: TableNum, - value: fn(&T) -> [u8; 8], + key: Vec, } #[repr(C)] #[derive(NoUninit, Clone, Copy)] struct Key(TableNum, [u8; 8], RowNum); -impl OrderIndex { - pub fn new(table: &mut Table, id: TableNum, value: fn(&T) -> [u8; 8]) -> Self { - table.indices.push(Box::new(Self { id, value })); +impl OrderIndex { + pub fn new(id: TableNum, value: fn(Object) -> [u8; 8]) -> Self { Self { id, value } } } -impl Index for OrderIndex { - fn add(&self, db: &mut dyn WriteTransaction, id: RowNum, val: &T) -> Result<()> { +impl Index for OrderIndex { + fn add(&self, db: &mut dyn WriteTransaction, id: RowNum, val: Object) -> Result<()> { db.set(bytes_of(&Key(self.id, (self.value)(val), id)), &[]) } - fn remove(&self, db: &mut dyn WriteTransaction, id: RowNum, val: &T) -> Result<()> { + fn remove(&self, db: &mut dyn WriteTransaction, id: RowNum, val: Object) -> Result<()> { db.del(bytes_of(&Key(self.id, (self.value)(val), id))) } - fn compare(&self, before: &T, after: &T) -> bool { + fn compare(&self, before: Object, after: Object) -> bool { (self.value)(before) == (self.value)(after) } } 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 */ +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 { +pub struct Table { id: u32, - pub(crate) indices: Vec>>, + pub(crate) indices: Vec>, } -impl Table { +impl Table { pub fn new(id: u32) -> Self { Self { id, @@ -49,18 +51,20 @@ impl Table { Ok(id_counter) } - pub fn get(&self, db: &dyn ReadTransaction, row: RowNum) -> Result> { - Ok(db - .get(&self.key(row))? - .map(|v| serde_json::from_slice(&v)) - .transpose()?) + pub fn add_index(&mut self, index: T) -> T { + self.indices.push(Box::new(index.clone())); + index + } + pub fn get(&self, db: &dyn ReadTransaction, row: RowNum) -> Result> { + Ok(db.get(&self.key(row))?.map(ObjectBuffer::from)) } pub fn remove(&self, db: &mut dyn WriteTransaction, row: RowNum) -> Result { 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) -- cgit v1.3