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
//! `sled` is a flash-sympathetic persistent lock-free B+ tree.
//!
//! # Examples
//!
//! ```
//! use sled::{Db, IVec};
//!
//! let t = Db::open("my_db").unwrap();
//! t.insert(b"yo!", b"v1".to_vec());
//! assert_eq!(t.get(b"yo!"), Ok(Some(IVec::from(b"v1"))));
//!
//! // Atomic compare-and-swap.
//! t.cas(
//!     b"yo!",       // key
//!     Some(b"v1"),  // old value, None for not present
//!     Some(b"v2"),  // new value, None for delete
//! ).unwrap();
//!
//! // Iterates over key-value pairs, starting at the given key.
//! let scan_key: &[u8] = b"a non-present key before yo!";
//! let mut iter = t.range(scan_key..);
//! assert_eq!(iter.next().unwrap(), Ok((IVec::from(b"yo!"), IVec::from(b"v2"))));
//! assert_eq!(iter.next(), None);
//!
//! t.remove(b"yo!");
//! assert_eq!(t.get(b"yo!"), Ok(None));
//! ```

#![deny(missing_docs)]
#![cfg_attr(test, deny(clippy::warnings))]
#![cfg_attr(test, deny(clippy::bad_style))]
#![cfg_attr(test, deny(clippy::future_incompatible))]
#![cfg_attr(test, deny(clippy::nonstandard_style))]
#![cfg_attr(test, deny(clippy::rust_2018_compatibility))]
#![cfg_attr(test, deny(clippy::rust_2018_idioms))]

mod batch;
mod binary_search;
mod context;
mod data;
mod db;
mod flusher;
mod frag;
mod iter;
mod ivec;
mod materializer;
mod meta;
mod node;
mod prefix;
mod subscription;
mod tree;
mod tx;

const DEFAULT_TREE_ID: &[u8] = b"__sled__default";

pub use {
    self::{
        batch::Batch,
        db::Db,
        iter::Iter,
        ivec::IVec,
        subscription::{Event, Subscriber},
        tree::Tree,
        tx::{
            TransactionError, TransactionResult, Transactional,
            TransactionalTree,
        },
    },
    pagecache::{Config, ConfigBuilder, Error, Result},
};

use {
    self::{
        binary_search::binary_search_lub,
        context::Context,
        data::Data,
        frag::Frag,
        node::Node,
        prefix::{
            prefix_cmp, prefix_cmp_encoded, prefix_decode, prefix_encode,
            prefix_reencode,
        },
        subscription::Subscriptions,
    },
    log::{debug, error, trace},
    pagecache::{
        debug_delay, pin, Materializer, Measure, PageCache, PageId,
        RecoveryGuard, M,
    },
    serde::{Deserialize, Serialize},
};

type TreePtr<'g> = pagecache::PagePtr<'g, Frag>;

/// Allows arbitrary logic to be injected into mere operations of the `PageCache`.
pub type MergeOperator = fn(
    key: &[u8],
    last_value: Option<&[u8]>,
    new_merge: &[u8],
) -> Option<Vec<u8>>;