/* 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, Table, TableNum}, }; use anyhow::Result; use bytemuck::{NoUninit, bytes_of}; pub struct OrderIndex { id: TableNum, value: fn(&T) -> [u8; 8], } #[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 })); Self { id, value } } } impl Index for OrderIndex { 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: 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) } }