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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
use sled::IVec;
use std::{marker::PhantomData, ops::RangeBounds};

use crate::{
    encoding::Encoding,
    error::{coerce, Result},
};

#[derive(Clone)]
/// A flash-sympathetic persistent lock-free B+ tree
pub struct StructuredTree<V, E>(sled::Tree, String, PhantomData<V>, PhantomData<E>);

/// An iterator over keys and values in a `Tree`.
pub struct StructuredIter<V, E>(sled::Iter, PhantomData<V>, PhantomData<E>);

#[derive(Clone, Debug, Default)]
/// A batch of updates that will be applied atomically to the Tree.
pub struct StructuredBatch<V, E>(sled::Batch, PhantomData<V>, PhantomData<E>);

#[derive(Clone)]
/// A transaction that will be applied atomically to the Tree.
pub struct StructuredTransactionalTree<'a, V, E>(
    &'a sled::TransactionalTree,
    PhantomData<V>,
    PhantomData<E>,
);

impl<V, E> StructuredTree<V, E>
where
    E: Encoding<V> + 'static,
{
    pub(crate) fn new(db: &sled::Db, name: &str) -> Result<Self> {
        Ok(StructuredTree(
            db.open_tree(name)?,
            name.to_owned(),
            PhantomData,
            PhantomData,
        ))
    }

    /// Clone for structures where V and E aren't Clone
    pub fn cloned(&self) -> Self {
        StructuredTree(self.0.clone(), self.1.clone(), PhantomData, PhantomData)
    }

    /// Perform a multi-key serializable transaction.
    ///
    /// Transactions also work on tuples of Trees, preserving serializable ACID semantics! In this
    /// example, we treat two trees like a work queue, atomically apply updates to data and move
    /// them from the unprocessed Tree to the processed Tree.
    pub fn transaction<F, R>(&self, f: F) -> sled::TransactionResult<Result<R>>
    where
        F: Fn(StructuredTransactionalTree<V, E>) -> sled::TransactionResult<Result<R>>,
    {
        self.0.transaction(move |trans_tree| {
            (f)(StructuredTransactionalTree(
                trans_tree,
                PhantomData,
                PhantomData,
            ))
        })
    }

    /// Create a new batched update that can be atomically applied.
    ///
    /// It is possible to apply a Batch in a transaction as well, which is the way you can apply a Batch to multiple Trees atomically.
    pub fn apply_batch(&self, batch: StructuredBatch<V, E>) -> Result<()> {
        Ok(self.0.apply_batch(batch.0)?)
    }

    /// Compare and swap. Capable of unique creation, conditional modification, or deletion. If old
    /// is None, this will only set the value if it doesn't exist yet. If new is None, will delete
    /// the value if old is correct. If both old and new are Some, will modify the value if old is
    /// correct. If Tree is read-only, will do nothing.
    pub fn cas<K>(
        &self,
        key: K,
        old: Option<V>,
        new: Option<V>,
    ) -> Result<std::result::Result<(), Option<V>>>
    where
        K: AsRef<[u8]>,
    {
        let ov = coerce(old.map(|value| E::encode(&value)))?;
        let nv = coerce(new.map(|value| E::encode(&value)))?;

        match self.0.cas(key, ov, nv)? {
            Ok(()) => Ok(Ok(())),
            Err(None) => Ok(Err(None)),
            Err(Some(v)) => Ok(Err(Some(E::decode(&v)?))),
        }
    }

    /// Retrieve a value from the Tree if it exists.
    pub fn get<K>(&self, key: K) -> Result<Option<V>>
    where
        K: AsRef<[u8]>,
    {
        let opt = self.0.get(key)?;

        if let Some(v) = opt {
            Ok(Some(E::decode(&v)?))
        } else {
            Ok(None)
        }
    }

    /// Insert a key to a new value, returning the last value if it was set.
    pub fn insert<K>(&self, key: K, value: V) -> Result<Option<V>>
    where
        IVec: From<K>,
        K: AsRef<[u8]>,
    {
        let v = E::encode(&value)?;

        let opt = self.0.insert::<K, Vec<u8>>(key, v)?;

        if let Some(v) = opt {
            Ok(Some(E::decode(&v)?))
        } else {
            Ok(None)
        }
    }

    /// Delete a value, returning the old value if it existed.
    pub fn remove<K>(&self, key: K) -> Result<Option<V>>
    where
        K: AsRef<[u8]>,
    {
        let opt = self.0.remove(key)?;

        if let Some(v) = opt {
            Ok(Some(E::decode(&v)?))
        } else {
            Ok(None)
        }
    }

    /// Fetch the value, apply a function to it and return the result.
    ///
    /// ### Note
    /// This may call the function multiple times if the value has been changed from other threads
    /// in the meantime.
    pub fn update_and_fetch<K>(
        &self,
        key: K,
        f: impl Fn(Option<V>) -> Option<V>,
    ) -> Result<Option<V>>
    where
        K: AsRef<[u8]>,
    {
        let opt = self.0.update_and_fetch(key, |opt| {
            let o = opt.and_then(|v| E::decode(&v).ok());

            (f)(o).and_then(|value| E::encode(&value).ok())
        })?;

        if let Some(v) = opt {
            Ok(Some(E::decode(&v)?))
        } else {
            Ok(None)
        }
    }

    /// Fetch the value, apply a function to it and return the previous value.
    ///
    /// ### Note
    /// This may call the function multiple times if the value has been changed from other threads in the meantime.
    pub fn fetch_and_update<K>(
        &self,
        key: K,
        f: impl Fn(Option<V>) -> Option<V>,
    ) -> Result<Option<V>>
    where
        K: AsRef<[u8]>,
    {
        let opt = self.0.fetch_and_update(key, |opt| {
            let o = opt.and_then(|v| E::decode(&v).ok());

            (f)(o).and_then(|value| E::encode(&value).ok())
        })?;

        if let Some(v) = opt {
            Ok(Some(E::decode(&v)?))
        } else {
            Ok(None)
        }
    }

    /// Subscribe to `Event`s that happen to keys that have the specified prefix. Events for
    /// particular keys are guaranteed to be witnessed in the same order by all threads, but
    /// threads may witness different interleavings of `Event`s across different keys. If
    /// subscribers don't keep up with new writes, they will cause new writes to block. There is a
    /// buffer of 1024 items per `Subscriber`. This can be used to build reactive and replicated
    /// systems.
    pub fn watch_prefix(&self, prefix: Vec<u8>) -> sled::Subscriber {
        self.0.watch_prefix(prefix)
    }

    /// Synchronously flushes all dirty IO buffers and calls fsync. If this succeeds, it is guaranteed that all previous writes will be recovered if the system crashes. Returns the number of bytes flushed during this call.
    ///
    /// Flushing can take quite a lot of time, and you should measure the performance impact of using it on realistic sustained workloads running on realistic hardware.
    pub fn flush(&self) -> Result<()> {
        self.0.flush()?;
        Ok(())
    }

    /// Returns `true` if the `Tree` contains a value for the specified key.
    pub fn contains_key<K>(&self, key: K) -> Result<bool>
    where
        K: AsRef<[u8]>,
    {
        Ok(self.0.contains_key(key)?)
    }

    /// Create a double-ended iterator over the tuples of keys and values in this tree.
    pub fn iter(&self) -> StructuredIter<V, E> {
        StructuredIter::new(self.0.iter())
    }

    /// Create a double-ended iterator over tuples of keys and values, where the keys fall
    /// within the specified range.
    pub fn range<K, R>(&self, range: R) -> StructuredIter<V, E>
    where
        K: AsRef<[u8]>,
        R: RangeBounds<K>,
    {
        StructuredIter::new(self.0.range(range))
    }

    /// Retrieve the key and value before the provided key, if one exists.
    pub fn get_lt<K>(&self, key: K) -> Result<Option<(IVec, V)>>
    where
        K: AsRef<[u8]>,
    {
        match self.0.get_lt(key)? {
            Some((k, v)) => {
                let value = E::decode(&v)?;
                Ok(Some((k, value)))
            }
            None => Ok(None),
        }
    }

    /// Retrieve the next key and value from the Tree after the provided key.
    ///
    /// ### Note
    /// The order follows the Ord implementation for Vec<u8>:
    ///
    /// `[] < [0] < [255] < [255, 0] < [255, 255] ...`
    ///
    /// To retain the ordering of numerical types use big endian reprensentation
    pub fn get_gt<K>(&self, key: K) -> Result<Option<(IVec, V)>>
    where
        K: AsRef<[u8]>,
    {
        match self.0.get_gt(key)? {
            Some((k, v)) => {
                let value = E::decode(&v)?;
                Ok(Some((k, value)))
            }
            None => Ok(None),
        }
    }

    /// Create an iterator over tuples of keys and values, where the all the keys starts with the
    /// given prefix.
    pub fn scan_prefix<P>(&self, prefix: P) -> StructuredIter<V, E>
    where
        P: AsRef<[u8]>,
    {
        StructuredIter::new(self.0.scan_prefix(prefix))
    }

    /// Atomically removes the maximum item in the `Tree` instance.
    pub fn pop_max(&self) -> Result<Option<(IVec, V)>> {
        match self.0.pop_max()? {
            Some((k, v)) => {
                let value = E::decode(&v)?;
                Ok(Some((k, value)))
            }
            None => Ok(None),
        }
    }

    /// Atomically removes the minimum item in the `Tree` instance.
    pub fn pop_min(&self) -> Result<Option<(IVec, V)>> {
        match self.0.pop_min()? {
            Some((k, v)) => {
                let value = E::decode(&v)?;
                Ok(Some((k, value)))
            }
            None => Ok(None),
        }
    }

    /// Returns the number of elements in this tree.
    ///
    /// Beware: performs a full O(n) scan under the hood.
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Returns `true` if the `Tree` contains no elements.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Clears the `Tree`, removing all values.
    ///
    /// Note that this is not atomic.
    pub fn clear(&self) -> Result<()> {
        Ok(self.0.clear()?)
    }

    /// Returns the name of the tree.
    pub fn name(&self) -> String {
        self.1.clone()
    }
}

impl<V, E> StructuredIter<V, E>
where
    E: Encoding<V> + 'static,
{
    fn new(iter: sled::Iter) -> Self {
        StructuredIter(iter, PhantomData, PhantomData)
    }

    /// Iterate over the keys of this Tree
    pub fn keys(self) -> impl DoubleEndedIterator<Item = Result<IVec>> {
        self.map(|res| res.map(|(key, _)| key))
    }

    /// Iterate over the values of this Tree
    pub fn values(self) -> impl DoubleEndedIterator<Item = Result<V>> {
        self.map(|res| res.map(|(_, v)| v))
    }
}

impl<V, E> StructuredBatch<V, E>
where
    E: Encoding<V>,
{
    /// Set a key to a new value
    pub fn insert<K>(&mut self, key: K, value: V) -> Result<()>
    where
        IVec: From<K>,
    {
        let v = E::encode(&value)?;
        self.0.insert::<_, Vec<u8>>(key, v);
        Ok(())
    }

    /// Remove a key
    pub fn remove<K>(&mut self, key: K)
    where
        IVec: From<K>,
    {
        self.0.remove(key)
    }
}

impl<'a, V, E> StructuredTransactionalTree<'a, V, E>
where
    E: Encoding<V>,
{
    /// Set a key to a new value
    pub fn insert<K>(&self, key: K, value: V) -> sled::TransactionResult<Result<Option<V>>>
    where
        IVec: From<K>,
        K: AsRef<[u8]>,
    {
        let v = match E::encode(&value) {
            Ok(v) => v,
            Err(e) => return Ok(Err(e)),
        };

        let opt = self.0.insert::<_, Vec<_>>(key, v)?;

        if let Some(v) = opt {
            match E::decode(&v) {
                Ok(i) => return Ok(Ok(Some(i))),
                Err(e) => return Ok(Err(e)),
            }
        } else {
            Ok(Ok(None))
        }
    }

    /// Remove a key
    pub fn remove<K>(&self, key: K) -> sled::TransactionResult<Result<Option<V>>>
    where
        IVec: From<K>,
        K: AsRef<[u8]>,
    {
        let opt = self.0.remove(key)?;

        if let Some(v) = opt {
            match E::decode(&v) {
                Ok(i) => return Ok(Ok(Some(i))),
                Err(e) => return Ok(Err(e)),
            }
        } else {
            Ok(Ok(None))
        }
    }

    /// Get the value associated with a key
    pub fn get<K>(&self, key: K) -> sled::TransactionResult<Result<Option<V>>>
    where
        K: AsRef<[u8]>,
    {
        let opt = self.0.get(key)?;

        if let Some(v) = opt {
            match E::decode(&v) {
                Ok(i) => return Ok(Ok(Some(i))),
                Err(e) => return Ok(Err(e)),
            }
        } else {
            Ok(Ok(None))
        }
    }

    /// Atomically apply multiple inserts and removals.
    pub fn apply_batch(&self, batch: StructuredBatch<V, E>) -> sled::TransactionResult<()> {
        self.0.apply_batch(batch.0)
    }
}

impl<V, E> Iterator for StructuredIter<V, E>
where
    E: Encoding<V>,
{
    type Item = Result<(IVec, V)>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.0.next()? {
            Ok((key, v)) => Some(E::decode(&v).map(move |value| (key, value))),
            Err(e) => Some(Err(e.into())),
        }
    }
}

impl<V, E> DoubleEndedIterator for StructuredIter<V, E>
where
    E: Encoding<V>,
{
    fn next_back(&mut self) -> Option<<Self as Iterator>::Item> {
        match self.0.next_back()? {
            Ok((key, v)) => Some(E::decode(&v).map(move |value| (key, value))),
            Err(e) => Some(Err(e.into())),
        }
    }
}