From 72a718fffb236c4b157e4d62c2e486ca7b326a26 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sun, 11 Jan 2026 02:51:24 +0100 Subject: things --- database/src/indices/key.rs | 68 ------------------------------------- database/src/indices/mod.rs | 6 ++-- database/src/indices/order.rs | 41 ---------------------- database/src/indices/rating.rs | 41 ++++++++++++++++++++++ database/src/indices/reference.rs | 71 +++++++++++++++++++++++++++++++++++++++ database/src/lib.rs | 16 ++++++++- 6 files changed, 130 insertions(+), 113 deletions(-) delete mode 100644 database/src/indices/key.rs delete mode 100644 database/src/indices/order.rs create mode 100644 database/src/indices/rating.rs create mode 100644 database/src/indices/reference.rs (limited to 'database') diff --git a/database/src/indices/key.rs b/database/src/indices/key.rs deleted file mode 100644 index 2790220..0000000 --- a/database/src/indices/key.rs +++ /dev/null @@ -1,68 +0,0 @@ -/* - This file is part of jellything (https://codeberg.org/metamuffin/jellything) - which is licensed under the GNU Affero General Public License (version 3); see /COPYING. - Copyright (C) 2026 metamuffin -*/ - -use crate::{ - backends::{ReadTransaction, WriteTransaction}, - indices::Index, - prefix_iterator::PrefixIterator, - table::{RowNum, TableNum}, -}; -use anyhow::Result; -use jellycommon::jellyobject::{Object, Tag}; - -pub struct KeyIndex { - id: TableNum, - key: Vec, -} - -impl KeyIndex { - pub fn new(id: TableNum, key: Vec) -> Self { - Self { id, key } - } - pub fn key(&self, id: RowNum, key: &[u8]) -> Vec { - let mut v = Vec::new(); - v.extend(self.id.to_be_bytes()); - v.extend(key); - v.extend(id.to_be_bytes()); - v - } - pub fn lookup<'a>( - &self, - db: &'a dyn ReadTransaction, - key: &[u8], - ) -> Result> { - let mut prefix = Vec::new(); - prefix.extend(self.id.to_be_bytes()); - prefix.extend(key); - Ok(PrefixIterator { - inner: db.iter(&prefix, false)?, - prefix: prefix.into(), - }) - } -} -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: Object) -> Result<()> { - // db.del(&self.key(id, (self.key)(val))) - } - fn compare(&self, before: Object, after: Object) -> bool { - // (self.key)(before) == (self.key)(after) - } -} - -// fn inc_key(key: &[u8]) -> Vec { -// let mut key = key.to_owned(); -// for i in (0..key.len()).rev() { -// let (nv, carry) = key[i].overflowing_add(1); -// key[i] = nv; -// if !carry { -// break; -// } -// } -// key -// } diff --git a/database/src/indices/mod.rs b/database/src/indices/mod.rs index ad3d00f..2d8642a 100644 --- a/database/src/indices/mod.rs +++ b/database/src/indices/mod.rs @@ -8,14 +8,14 @@ use crate::{backends::WriteTransaction, table::RowNum}; use anyhow::Result; use jellycommon::jellyobject::Object; -pub mod key; -pub mod order; +pub mod reference; +pub mod rating; 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 + false } } diff --git a/database/src/indices/order.rs b/database/src/indices/order.rs deleted file mode 100644 index 911ec37..0000000 --- a/database/src/indices/order.rs +++ /dev/null @@ -1,41 +0,0 @@ -/* - This file is part of jellything (https://codeberg.org/metamuffin/jellything) - which is licensed under the GNU Affero General Public License (version 3); see /COPYING. - Copyright (C) 2026 metamuffin -*/ - -use crate::{ - backends::WriteTransaction, - indices::Index, - table::{RowNum, TableNum}, -}; -use anyhow::Result; -use bytemuck::{NoUninit, bytes_of}; -use jellycommon::jellyobject::{Object, Tag}; - -#[derive(Clone)] -pub struct OrderIndex { - id: TableNum, - key: Vec, -} - -#[repr(C)] -#[derive(NoUninit, Clone, Copy)] -struct Key(TableNum, [u8; 8], RowNum); - -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: Object) -> Result<()> { - db.set(bytes_of(&Key(self.id, (self.value)(val), id)), &[]) - } - 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: Object, after: Object) -> bool { - (self.value)(before) == (self.value)(after) - } -} diff --git a/database/src/indices/rating.rs b/database/src/indices/rating.rs new file mode 100644 index 0000000..003d327 --- /dev/null +++ b/database/src/indices/rating.rs @@ -0,0 +1,41 @@ +/* + This file is part of jellything (https://codeberg.org/metamuffin/jellything) + which is licensed under the GNU Affero General Public License (version 3); see /COPYING. + Copyright (C) 2026 metamuffin +*/ + +use crate::{ + backends::WriteTransaction, + indices::Index, + table::{RowNum, TableNum}, +}; +use anyhow::Result; +use bytemuck::NoUninit; +use jellycommon::jellyobject::{Object, Tag, TypedTag}; + +#[derive(Clone)] +pub struct RatingIndex { + id: TableNum, + tag: TypedTag>, +} + +#[repr(C)] +#[derive(NoUninit, Clone, Copy)] +struct Key(TableNum, Tag, u32, u64, RowNum); + +impl RatingIndex { + pub fn new(id: TableNum, tag: TypedTag>) -> Self { + Self { id, tag } + } +} +impl Index for RatingIndex { + fn add(&self, db: &mut dyn WriteTransaction, id: RowNum, val: Object) -> Result<()> { + todo!() + } + fn remove(&self, db: &mut dyn WriteTransaction, id: RowNum, val: Object) -> Result<()> { + todo!() + } + fn compare(&self, before: Object, after: Object) -> bool { + todo!() + } +} diff --git a/database/src/indices/reference.rs b/database/src/indices/reference.rs new file mode 100644 index 0000000..66efa24 --- /dev/null +++ b/database/src/indices/reference.rs @@ -0,0 +1,71 @@ +/* + This file is part of jellything (https://codeberg.org/metamuffin/jellything) + which is licensed under the GNU Affero General Public License (version 3); see /COPYING. + Copyright (C) 2026 metamuffin +*/ + +use crate::{ + backends::{ReadTransaction, WriteTransaction}, + indices::Index, + prefix_iterator::PrefixIterator, + table::{RowNum, TableNum}, +}; +use anyhow::Result; +use bytemuck::{NoUninit, bytes_of}; +use jellycommon::jellyobject::{Object, TypedTag}; + +pub struct ReferenceIndex { + id: TableNum, + tag: TypedTag, +} + +#[repr(C)] +#[derive(NoUninit, Clone, Copy)] +pub struct Key(TableNum, RowNum, RowNum); + +impl ReferenceIndex { + pub fn new(id: TableNum, tag: TypedTag) -> Self { + Self { id, tag } + } + pub fn lookup<'a>( + &self, + db: &'a dyn ReadTransaction, + to: RowNum, + ) -> Result> { + let mut prefix = Vec::new(); + prefix.extend(self.id.to_be_bytes()); + prefix.extend(to.to_be_bytes()); + Ok(PrefixIterator { + inner: db.iter(&prefix, false)?, + prefix: prefix.into(), + }) + } +} +impl Index for ReferenceIndex { + fn add(&self, db: &mut dyn WriteTransaction, id: RowNum, val: Object) -> Result<()> { + for to in val.iter(self.tag) { + db.set(bytes_of(&Key(self.id, to, id)), &[])?; + } + Ok(()) + } + fn remove(&self, db: &mut dyn WriteTransaction, id: RowNum, val: Object) -> Result<()> { + for to in val.iter(self.tag) { + db.del(bytes_of(&Key(self.id, to, id)))?; + } + Ok(()) + } + fn compare(&self, before: Object, after: Object) -> bool { + let mut before = before.iter(self.tag); + let mut after = after.iter(self.tag); + loop { + let b = before.next(); + let a = after.next(); + if a.is_none() && b.is_none() { + break true; + } + if a != b { + break false; + } + } + } +} diff --git a/database/src/lib.rs b/database/src/lib.rs index 40bda72..32d160b 100644 --- a/database/src/lib.rs +++ b/database/src/lib.rs @@ -5,7 +5,21 @@ */ pub mod backends; pub mod indices; -pub mod table; pub mod prefix_iterator; +pub mod table; pub type Pad32 = u32; + +use jellycommon::jellyobject::{Object, Tag, TypedTag}; + +enum Query<'a> { + MatchStr(Match<'a, &'a str>), + MatchF64(Match<'a, f64>), + MatchU64(Match<'a, u64>), + MatchTag(Match<'a, Tag>), + Has(Path<'a>), +} + +pub struct Match<'a, T>(pub TypedPath<'a, T>, pub T); +pub struct TypedPath<'a, T>(pub &'a [TypedTag>], pub TypedTag); +pub struct Path<'a>(pub &'a [TypedTag>], pub Tag); -- cgit v1.3