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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
use pin_project_lite::pin_project;
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};

use sled::Transactional;

use crate::{Error, Key, Raw, Transaction, TransactionError, Value};

/// Provides typed access to the key/value store
#[derive(Clone)]
pub struct Bucket<'a, K: Key<'a>, V: Value>(
    pub(crate) sled::Tree,
    PhantomData<K>,
    PhantomData<V>,
    PhantomData<&'a ()>,
);

/// Key/value pair
#[derive(Clone)]
pub struct Item<K, V>(Raw, Raw, PhantomData<K>, PhantomData<V>);

/// Batch update
#[derive(Clone)]
pub struct Batch<K, V>(pub(crate) sled::Batch, PhantomData<K>, PhantomData<V>);

pin_project! {
    /// Subscribe to key updated
    pub struct Watch<K, V> {
        #[pin]
        subscriber: sled::Subscriber,
        phantom: PhantomData<(K, V)>
    }
}

/// Event is used to describe the type of update
pub enum Event<K, V> {
    /// A key has been updated
    Set(Item<K, V>),
    /// A key has been removed
    Remove(Raw),
}

impl<'a, K: Key<'a>, V> Event<K, V> {
    fn from_sled(event: sled::Event) -> Self {
        match event {
            sled::Event::Insert { key, value } => {
                Event::Set(Item(key, value, PhantomData, PhantomData))
            }
            sled::Event::Remove { key } => Event::Remove(key),
        }
    }
}

impl<'a, K: Key<'a>, V> Iterator for Watch<K, V> {
    type Item = Result<Event<K, V>, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.subscriber.next() {
            None => None,
            Some(e) => Some(Ok(Event::from_sled(e))),
        }
    }
}

impl<'a, K: Key<'a>, V> Future for Watch<K, V> {
    type Output = Option<Event<K, V>>;
    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.project();
        match this.subscriber.poll(cx) {
            Poll::Pending => Poll::Pending,
            Poll::Ready(r) => Poll::Ready(r.map(Event::from_sled)),
        }
    }
}

impl<'a, K: Key<'a>, V: Value> Event<K, V> {
    /// Returns true when event is `Set`
    pub fn is_set(&self) -> bool {
        matches!(self, Event::Set(_))
    }

    /// Returns true when event is `Remove`
    pub fn is_remove(&self) -> bool {
        matches!(self, Event::Remove(_))
    }

    /// Get event key
    pub fn key(&'a self) -> Result<K, Error> {
        match self {
            Event::Remove(k) => K::from_raw_key(k),
            Event::Set(item) => item.key(),
        }
    }

    /// Get event value (for insert)
    pub fn value(&'a self) -> Result<Option<V>, Error> {
        match self {
            Event::Remove(_) => Ok(None),
            Event::Set(item) => item.value().map(Some),
        }
    }
}

impl<'a, K: Key<'a>, V: Value> Item<K, V> {
    /// Get the value associated with the specified key
    pub fn value<T: From<V>>(&'a self) -> Result<T, Error> {
        let x = V::from_raw_value(self.1.clone())?;
        Ok(x.into())
    }

    /// Get the value associated with the specified key
    pub fn key<T>(&'a self) -> Result<T, Error>
    where
        K: Into<T>,
    {
        let k = K::from_raw_key(&self.0)?;
        Ok(k.into())
    }
}

/// Iterator over Bucket keys and values
pub struct Iter<K, V>(sled::Iter, PhantomData<K>, PhantomData<V>);

impl<'a, K, V> Iterator for Iter<K, V>
where
    K: Key<'a>,
    V: Value,
{
    type Item = Result<Item<K, V>, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.0.next() {
            None => None,
            Some(Err(e)) => Some(Err(e.into())),
            Some(Ok((k, v))) => Some(Ok(Item(k, v, PhantomData, PhantomData))),
        }
    }
}

impl<'a, K, V> DoubleEndedIterator for Iter<K, V>
where
    K: Key<'a>,
    V: Value,
{
    fn next_back(&mut self) -> Option<Self::Item> {
        match self.0.next_back() {
            None => None,
            Some(Err(e)) => Some(Err(e.into())),
            Some(Ok((k, v))) => Some(Ok(Item(k, v, PhantomData, PhantomData))),
        }
    }
}

impl<'a, K: Key<'a>, V: Value> Bucket<'a, K, V> {
    pub(crate) fn new(t: sled::Tree) -> Bucket<'a, K, V> {
        Bucket(t, PhantomData, PhantomData, PhantomData)
    }

    /// Returns true if the bucket contains the given key
    pub fn contains(&self, key: &K) -> Result<bool, Error> {
        let v = self.0.contains_key(key.to_raw_key()?)?;
        Ok(v)
    }

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

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

    /// Set the value associated with the specified key to the provided value
    pub fn set(&self, key: &K, value: &V) -> Result<Option<V>, Error> {
        let v = value.to_raw_value()?;
        Ok(self
            .0
            .insert(key.to_raw_key()?, v)?
            .map(|x| V::from_raw_value(x))
            // 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))?)
    }

    /// Set the value associated with the specified key to the provided value, only if the existing
    /// value matches the `old` parameter
    pub fn compare_and_swap(
        &self,
        key: &K,
        old: Option<&V>,
        value: Option<&V>,
    ) -> Result<(), Error> {
        let old = match old {
            Some(x) => Some(x.to_raw_value()?),
            None => None,
        };

        let value = match value {
            Some(x) => Some(x.to_raw_value()?),
            None => None,
        };

        let a = self.0.compare_and_swap(key.to_raw_key()?, old, value)?;

        Ok(a?)
    }

    /// Remove the value associated with the specified key from the database
    pub fn remove(&self, key: &K) -> Result<Option<V>, Error> {
        Ok(self
            .0
            .remove(key.to_raw_key()?)?
            .map(|x| V::from_raw_value(x))
            // 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))?)
    }

    /// Get an iterator over keys/values
    pub fn iter(&self) -> Iter<K, V> {
        Iter(self.0.iter(), PhantomData, PhantomData)
    }

    /// Get an iterator over keys/values in the specified range
    pub fn iter_range(&self, a: &K, b: &K) -> Result<Iter<K, V>, Error> {
        let a = a.to_raw_key()?;
        let b = b.to_raw_key()?;
        Ok(Iter(self.0.range(a..b), PhantomData, PhantomData))
    }

    /// Iterate over keys/values with the specified prefix
    pub fn iter_prefix(&self, a: &K) -> Result<Iter<K, V>, Error> {
        let a = a.to_raw_key()?;
        Ok(Iter(self.0.scan_prefix(a), PhantomData, PhantomData))
    }

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

    /// Get updates when a key with the given prefix is changed
    pub fn watch_prefix(&self, prefix: Option<&K>) -> Result<Watch<K, V>, Error> {
        let k = match prefix {
            Some(k) => k.to_raw_key()?,
            None => b"".into(),
        };
        let subscriber = self.0.watch_prefix(k);
        Ok(Watch {
            subscriber,
            phantom: PhantomData {},
        })
    }

    /// Execute a transaction
    pub fn transaction<
        A,
        E: From<sled::Error>,
        F: Fn(Transaction<K, V>) -> Result<A, TransactionError<E>>,
    >(
        &self,
        f: F,
    ) -> Result<A, E> {
        let result = self.0.transaction(|t| {
            let txn = Transaction::new(t);
            f(txn)
        });

        match result {
            Ok(x) => Ok(x),
            Err(sled::transaction::TransactionError::Abort(x)) => Err(x),
            Err(sled::transaction::TransactionError::Storage(e)) => Err(e.into()),
        }
    }

    /// Create a transaction with access to two buckets
    pub fn transaction2<
        A,
        T: Key<'a>,
        U: Value,
        E: From<sled::Error>,
        F: Fn(Transaction<K, V>, Transaction<T, U>) -> Result<A, TransactionError<E>>,
    >(
        &self,
        other: &Bucket<'a, T, U>,
        f: F,
    ) -> Result<A, E> {
        let result = (&self.0, &other.0).transaction(|(a, b)| {
            let a = Transaction::new(a);
            let b = Transaction::new(b);
            f(a, b)
        });

        match result {
            Ok(x) => Ok(x),
            Err(sled::transaction::TransactionError::Abort(x)) => Err(x),
            Err(sled::transaction::TransactionError::Storage(e)) => Err(e.into()),
        }
    }

    /// Create a transaction with access to three buckets
    pub fn transaction3<
        A,
        T: Key<'a>,
        U: Value,
        X: Key<'a>,
        Y: Value,
        E: From<sled::Error>,
        F: Fn(
            Transaction<K, V>,
            Transaction<T, U>,
            Transaction<X, Y>,
        ) -> Result<A, TransactionError<E>>,
    >(
        &self,
        other: &Bucket<'a, T, U>,
        other1: &Bucket<'a, X, Y>,
        f: F,
    ) -> Result<A, E> {
        let result = (&self.0, &other.0, &other1.0).transaction(|(a, b, c)| {
            let a = Transaction::new(a);
            let b = Transaction::new(b);
            let c = Transaction::new(c);
            f(a, b, c)
        });

        match result {
            Ok(x) => Ok(x),
            Err(sled::transaction::TransactionError::Abort(x)) => Err(x),
            Err(sled::transaction::TransactionError::Storage(e)) => Err(e.into()),
        }
    }

    /// Get previous key and value in order, if one exists
    pub fn prev_key(&self, key: &K) -> Result<Option<Item<K, V>>, Error> {
        let item = self.0.get_lt(key)?;
        Ok(item.map(|(k, v)| Item(k, v, PhantomData, PhantomData)))
    }

    /// Get next key and value in order, if one exists
    pub fn next_key(&self, key: &K) -> Result<Option<Item<K, V>>, Error> {
        let item = self.0.get_gt(key)?;
        Ok(item.map(|(k, v)| Item(k, v, PhantomData, PhantomData)))
    }

    /// Flush to disk
    pub fn flush(&self) -> Result<usize, Error> {
        Ok(self.0.flush()?)
    }

    /// Flush to disk
    pub async fn flush_async(&self) -> Result<usize, Error> {
        let f = self.0.flush_async().await?;
        Ok(f)
    }

    /// Remove and return the last item
    pub fn pop_back(&self) -> Result<Option<Item<K, V>>, Error> {
        let x = self.0.pop_max()?;
        Ok(x.map(|(k, v)| Item(k, v, PhantomData, PhantomData)))
    }

    /// Remove and return the first item
    pub fn pop_front(&self) -> Result<Option<Item<K, V>>, Error> {
        let x = self.0.pop_min()?;
        Ok(x.map(|(k, v)| Item(k, v, PhantomData, PhantomData)))
    }

    /// Get the first item
    pub fn first(&self) -> Result<Option<Item<K, V>>, Error> {
        let x = self.0.first()?;
        Ok(x.map(|(k, v)| Item(k, v, PhantomData, PhantomData)))
    }

    /// Get the last item
    pub fn last(&self) -> Result<Option<Item<K, V>>, Error> {
        let x = self.0.last()?;
        Ok(x.map(|(k, v)| Item(k, v, PhantomData, PhantomData)))
    }

    /// Get the number of items
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Returns true when there are no items
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Remove all items
    pub fn clear(&self) -> Result<(), Error> {
        self.0.clear()?;
        Ok(())
    }

    /// CRC32 checksum of all keys and values
    pub fn checksum(&self) -> Result<u32, Error> {
        Ok(self.0.checksum()?)
    }
}

impl<'a, K: Key<'a>, V: Value> Default for Batch<K, V> {
    fn default() -> Self {
        Batch::new()
    }
}

impl<'a, K: Key<'a>, V: Value> Batch<K, V> {
    /// Create a new Batch instance
    pub fn new() -> Batch<K, V> {
        Batch(sled::Batch::default(), PhantomData, PhantomData)
    }

    /// Set the value associated with the specified key to the provided value
    pub fn set(&mut self, key: &K, value: &V) -> Result<(), Error> {
        let v = value.to_raw_value()?;
        self.0.insert(key.to_raw_key()?, v);
        Ok(())
    }

    /// Remove the value associated with the specified key from the database
    pub fn remove(&mut self, key: &K) -> Result<(), Error> {
        self.0.remove(key.to_raw_key()?);
        Ok(())
    }
}