diff options
| author | metamuffin <metamuffin@disroot.org> | 2025-12-19 21:52:59 +0100 |
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2025-12-19 21:52:59 +0100 |
| commit | eac0de36221440571fe686074b04b71bf98cf727 (patch) | |
| tree | 76bad4c937e203bc13b6adcced9ed0b41432c2ae /database/src/indices | |
| parent | da985cc06e4caa7501222dbf54f212536fd42b0c (diff) | |
| download | jellything-eac0de36221440571fe686074b04b71bf98cf727.tar jellything-eac0de36221440571fe686074b04b71bf98cf727.tar.bz2 jellything-eac0de36221440571fe686074b04b71bf98cf727.tar.zst | |
things
Diffstat (limited to 'database/src/indices')
| -rw-r--r-- | database/src/indices/key.rs | 42 | ||||
| -rw-r--r-- | database/src/indices/mod.rs | 1 | ||||
| -rw-r--r-- | database/src/indices/order.rs | 33 |
3 files changed, 60 insertions, 16 deletions
diff --git a/database/src/indices/key.rs b/database/src/indices/key.rs new file mode 100644 index 0000000..ab56f76 --- /dev/null +++ b/database/src/indices/key.rs @@ -0,0 +1,42 @@ +/* + 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) 2025 metamuffin <metamuffin.org> +*/ + +use crate::{ + backends::WriteTransaction, + indices::Index, + table::{RowNum, Table, TableNum}, +}; +use anyhow::Result; + +pub struct KeyIndex<T> { + id: TableNum, + key: fn(&T) -> &[u8], +} + +impl<T: 'static> KeyIndex<T> { + pub fn new(table: &mut Table<T>, id: TableNum, key: fn(&T) -> &[u8]) -> Self { + table.indices.push(Box::new(Self { id, key })); + Self { id, key } + } + pub fn key(&self, id: RowNum, val: &T) -> Vec<u8> { + let mut v = Vec::new(); + v.extend(self.id.to_be_bytes()); + v.extend((self.key)(val)); + v.extend(id.to_be_bytes()); + v + } +} +impl<T: 'static> Index<T> for KeyIndex<T> { + fn add(&self, db: &mut dyn WriteTransaction, id: RowNum, val: &T) -> Result<()> { + db.set(&self.key(id, val), &[]) + } + fn remove(&self, db: &mut dyn WriteTransaction, id: RowNum, val: &T) -> Result<()> { + db.del(&self.key(id, val)) + } + fn compare(&self, before: &T, after: &T) -> bool { + (self.key)(before) == (self.key)(after) + } +} diff --git a/database/src/indices/mod.rs b/database/src/indices/mod.rs index 523235e..e291c91 100644 --- a/database/src/indices/mod.rs +++ b/database/src/indices/mod.rs @@ -8,6 +8,7 @@ use crate::{backends::WriteTransaction, table::RowNum}; use anyhow::Result; pub mod order; +pub mod key; pub trait Index<T> { fn add(&self, db: &mut dyn WriteTransaction, row: RowNum, val: &T) -> Result<()>; diff --git a/database/src/indices/order.rs b/database/src/indices/order.rs index 5b7924b..25ab01b 100644 --- a/database/src/indices/order.rs +++ b/database/src/indices/order.rs @@ -4,34 +4,35 @@ Copyright (C) 2025 metamuffin <metamuffin.org> */ -use crate::{backends::WriteTransaction, indices::Index, table::Table}; +use crate::{ + backends::WriteTransaction, + indices::Index, + table::{RowNum, Table, TableNum}, +}; use anyhow::Result; +use bytemuck::{NoUninit, bytes_of}; pub struct OrderIndex<T> { - id: u32, + id: TableNum, value: fn(&T) -> [u8; 8], } + +#[repr(C)] +#[derive(NoUninit, Clone, Copy)] +struct Key(TableNum, [u8; 8], RowNum); + impl<T: 'static> OrderIndex<T> { - pub fn new(table: &mut Table<T>, id: u32, value: fn(&T) -> [u8; 8]) -> Self { + pub fn new(table: &mut Table<T>, id: TableNum, value: fn(&T) -> [u8; 8]) -> Self { table.indices.push(Box::new(Self { id, value })); Self { id, value } } - fn key(&self, id: u64, val: &T) -> Vec<u8> { - let mut key = Vec::new(); - key.extend(self.id.to_be_bytes()); - key.extend((self.value)(val)); - key.extend(id.to_be_bytes()); - key - } } impl<T: 'static> Index<T> for OrderIndex<T> { - fn add(&self, db: &mut dyn WriteTransaction, id: u64, val: &T) -> Result<()> { - db.set(&self.key(id, val), &[])?; - Ok(()) + fn add(&self, db: &mut dyn WriteTransaction, id: RowNum, val: &T) -> Result<()> { + db.set(bytes_of(&Key(self.id, (self.value)(val), id)), &[]) } - fn remove(&self, db: &mut dyn WriteTransaction, id: u64, val: &T) -> Result<()> { - db.del(&self.key(id, val))?; - Ok(()) + fn remove(&self, db: &mut dyn WriteTransaction, id: RowNum, val: &T) -> Result<()> { + db.del(bytes_of(&Key(self.id, (self.value)(val), id))) } fn compare(&self, before: &T, after: &T) -> bool { (self.value)(before) == (self.value)(after) |