diff options
| author | metamuffin <metamuffin@disroot.org> | 2026-01-11 02:51:24 +0100 |
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2026-01-11 02:51:24 +0100 |
| commit | 72a718fffb236c4b157e4d62c2e486ca7b326a26 (patch) | |
| tree | 17acd02fcd88dca48ce92e94d950a413d1cc8340 /database/src/indices/reference.rs | |
| parent | 095d4234558c39f28ddc777f123a15e410a74c14 (diff) | |
| download | jellything-72a718fffb236c4b157e4d62c2e486ca7b326a26.tar jellything-72a718fffb236c4b157e4d62c2e486ca7b326a26.tar.bz2 jellything-72a718fffb236c4b157e4d62c2e486ca7b326a26.tar.zst | |
things
Diffstat (limited to 'database/src/indices/reference.rs')
| -rw-r--r-- | database/src/indices/reference.rs | 71 |
1 files changed, 71 insertions, 0 deletions
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 <metamuffin.org> +*/ + +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<RowNum>, +} + +#[repr(C)] +#[derive(NoUninit, Clone, Copy)] +pub struct Key(TableNum, RowNum, RowNum); + +impl ReferenceIndex { + pub fn new(id: TableNum, tag: TypedTag<RowNum>) -> Self { + Self { id, tag } + } + pub fn lookup<'a>( + &self, + db: &'a dyn ReadTransaction, + to: RowNum, + ) -> Result<PrefixIterator<'a>> { + 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; + } + } + } +} |