bincode_sled/
transaction.rs

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use std::marker::PhantomData;

use sled::transaction::{ConflictableTransactionResult, TransactionResult};

use crate::{deserialize, serialize, Batch, Tree, Key, Value};

pub struct TransactionalTree<'a, K, V> 
where K: Key, V:Value{
    inner: &'a sled::transaction::TransactionalTree,
    _key: PhantomData<fn() -> K>,
    _value: PhantomData<fn() -> V>,
}

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

    pub fn insert(
        &self,
        key: &K,
        value: &V,
    ) -> std::result::Result<Option<V>, sled::transaction::UnabortableTransactionError>
    {
        self.inner
            .insert(serialize(key), serialize(value))
            .map(|opt| opt.map(|v| deserialize(&v)))
    }

    pub fn remove(
        &self,
        key: &K,
    ) -> std::result::Result<Option<V>, sled::transaction::UnabortableTransactionError>
    {
        self.inner
            .remove(serialize(key))
            .map(|opt| opt.map(|v| deserialize(&v)))
    }

    pub fn get(
        &self,
        key: &K,
    ) -> std::result::Result<Option<V>, sled::transaction::UnabortableTransactionError>
    {
        self.inner
            .get(serialize(key))
            .map(|opt| opt.map(|v| deserialize(&v)))
    }

    pub fn apply_batch(
        &self,
        batch: &Batch<K, V>,
    ) -> std::result::Result<(), sled::transaction::UnabortableTransactionError> {
        self.inner.apply_batch(&batch.inner)
    }

    pub fn flush(&self) {
        self.inner.flush()
    }

    pub fn generate_id(&self) -> sled::Result<u64> {
        self.inner.generate_id()
    }
}

pub trait Transactional<E = ()> {
    type View<'a>;

    fn transaction<F, A>(&self, f: F) -> TransactionResult<A, E>
    where
        F: for<'a> Fn(Self::View<'a>) -> ConflictableTransactionResult<A, E>;
}

macro_rules! impl_transactional {
  ($($k:ident, $v:ident, $i:tt),+) => {
      impl<E, $($k:Key, $v:Value),+> Transactional<E> for ($(&Tree<$k, $v>),+) {
          type View<'a> = (
              $(TransactionalTree<'a, $k, $v>),+
          );

          fn transaction<F, A>(&self, f: F) -> TransactionResult<A, E>
          where
              F: for<'a> Fn(Self::View<'a>) -> ConflictableTransactionResult<A, E>,
          {
              use sled::Transactional;

              ($(&self.$i.inner),+).transaction(|trees| {
                  f((
                      $(TransactionalTree::new(&trees.$i)),+
                  ))
              })
          }
      }
  };
}

impl_transactional!(K0, V0, 0, K1, V1, 1);
impl_transactional!(K0, V0, 0, K1, V1, 1, K2, V2, 2);
impl_transactional!(K0, V0, 0, K1, V1, 1, K2, V2, 2, K3, V3, 3);
impl_transactional!(K0, V0, 0, K1, V1, 1, K2, V2, 2, K3, V3, 3, K4, V4, 4);
impl_transactional!(K0, V0, 0, K1, V1, 1, K2, V2, 2, K3, V3, 3, K4, V4, 4, K5, V5, 5);
impl_transactional!(K0, V0, 0, K1, V1, 1, K2, V2, 2, K3, V3, 3, K4, V4, 4, K5, V5, 5, K6, V6, 6);
impl_transactional!(
    K0, V0, 0, K1, V1, 1, K2, V2, 2, K3, V3, 3, K4, V4, 4, K5, V5, 5, K6, V6, 6, K7, V7, 7
);
impl_transactional!(
    K0, V0, 0, K1, V1, 1, K2, V2, 2, K3, V3, 3, K4, V4, 4, K5, V5, 5, K6, V6, 6, K7, V7, 7, K8, V8,
    8
);
impl_transactional!(
    K0, V0, 0, K1, V1, 1, K2, V2, 2, K3, V3, 3, K4, V4, 4, K5, V5, 5, K6, V6, 6, K7, V7, 7, K8, V8,
    8, K9, V9, 9
);
impl_transactional!(
    K0, V0, 0, K1, V1, 1, K2, V2, 2, K3, V3, 3, K4, V4, 4, K5, V5, 5, K6, V6, 6, K7, V7, 7, K8, V8,
    8, K9, V9, 9, K10, V10, 10
);

#[test]
fn test_multiple_tree_transaction() {
    let db = sled::Config::new().temporary(true).open().unwrap();
    let tree0 = Tree::<u32, i32>::open(&db, "tree0");
    let tree1 = Tree::<u16, i16>::open(&db, "tree1");
    let tree2 = Tree::<u8, i8>::open(&db, "tree2");

    (&tree0, &tree1, &tree2)
        .transaction(|trees| {
            trees.0.insert(&0, &0)?;
            trees.1.insert(&0, &0)?;
            trees.2.insert(&0, &0)?;
            // Todo: E in ConflitableTransactionResult<A, E> is not inferred
            // automatically, although Transactional<E = ()> has default E = () type.
            Ok::<(), sled::transaction::ConflictableTransactionError<()>>(())
        })
        .unwrap();

    assert_eq!(tree0.get(&0), Ok(Some(0)));
    assert_eq!(tree1.get(&0), Ok(Some(0)));
}