lunar-lib 0.9.0

Common utilities for lunar applications
Documentation
use crate::database::{CompareAndSwapTransaction, DatabaseEntry, TransactionError};

pub trait Mergeable: DatabaseEntry {
    /// Merges `from` into the `self` entry
    ///
    /// This means the identifier of `self` will be preserved, while `from` is deleted
    /// All references to `from` should be relinked to `self`
    ///
    /// This function should be a single atomic transaction
    fn tx_merge(
        self,
        from: Self,
        cas_tx: &mut CompareAndSwapTransaction<Self::Db>,
    ) -> Result<Self, TransactionError>;

    /// Merges all items from the `batch` into the `self` entry
    ///
    /// This means the identifier of `self` will be preserved, while `from` is deleted
    /// All references to `from` should be relinked to `self`
    ///
    /// This function should be a single atomic transaction
    fn tx_merge_batch(
        self,
        batch: impl IntoIterator<Item = Self>,
        cas_tx: &mut CompareAndSwapTransaction<Self::Db>,
    ) -> Result<Self, TransactionError> {
        batch
            .into_iter()
            .try_fold(self, |current, from| current.tx_merge(from, cas_tx))
    }
}