1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use std::marker::PhantomData;

use crate::{Batch, Error, Key, Value};

/// Transaction error
pub type TransactionError<E> = sled::transaction::ConflictableTransactionError<E>;

/// Transaction
#[derive(Clone)]
pub struct Transaction<'a, 'b, K: Key<'a>, V: Value>(
    &'b sled::transaction::TransactionalTree,
    PhantomData<K>,
    PhantomData<V>,
    PhantomData<&'a ()>,
);

impl<'a, 'b, K: Key<'a>, V: Value> Transaction<'a, 'b, K, V> {
    pub(crate) fn new(t: &'b sled::transaction::TransactionalTree) -> Self {
        Transaction(t, PhantomData, PhantomData, PhantomData)
    }

    /// Get the value associated with the specified key
    pub fn get(&self, key: &K) -> Result<Option<V>, TransactionError<Error>> {
        let v = self
            .0
            .get(key.to_raw_key().map_err(TransactionError::Abort)?)?;

        match v {
            None => Ok(None),
            Some(x) => Ok(Some(V::from_raw_value(x).map_err(TransactionError::Abort)?)),
        }
    }

    /// Set the value associated with the specified key to the provided value
    pub fn set(&self, key: &K, value: &V) -> Result<Option<V>, TransactionError<Error>> {
        let v = value.to_raw_value().map_err(TransactionError::Abort)?;
        Ok(self
            .0
            .insert(key.to_raw_key().map_err(TransactionError::Abort)?, v)?
            .map(|x| V::from_raw_value(x).map_err(TransactionError::Abort))
            // https://users.rust-lang.org/t/convenience-method-for-flipping-option-result-to-result-option/13695/7
            .map_or(Ok(None), |v| v.map(Some))?)
    }

    /// Remove the value associated with the specified key from the database
    pub fn remove(&self, key: &K) -> Result<Option<V>, TransactionError<Error>> {
        Ok(self
            .0
            .remove(key.to_raw_key().map_err(TransactionError::Abort)?)?
            .map(|x| V::from_raw_value(x).map_err(TransactionError::Abort))
            // https://users.rust-lang.org/t/convenience-method-for-flipping-option-result-to-result-option/13695/7
            .map_or(Ok(None), |v| v.map(Some))?)
    }

    /// Apply batch update
    pub fn batch(&self, batch: &Batch<K, V>) -> Result<(), TransactionError<Error>> {
        self.0.apply_batch(&batch.0)?;
        Ok(())
    }

    /// Generate a monotonic ID. Not guaranteed to be contiguous or idempotent, can produce different values in the same transaction in case of conflicts
    pub fn generate_id(&self) -> Result<u64, TransactionError<Error>> {
        Ok(self.0.generate_id()?)
    }
}