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
#![deny(missing_docs)]

//! `kv` is a simple way to embed a key/value store in Rust applications. It is built using
//! [sled](https://docs.rs/sled) and aims to be as lightweight as possible while still
//! providing a nice high level interface.
//!
//! ## Getting started
//!
//! ```rust
//! use kv::*;
//!
//! #[derive(serde::Serialize, serde::Deserialize, PartialEq)]
//! struct SomeType {
//!     a: i32,
//!     b: i32
//! }
//!
//! fn run() -> Result<(), Error> {
//!     // Configure the database
//!     let mut cfg = Config::new("./test/example");
//!
//!     // Open the key/value store
//!     let store = Store::new(cfg)?;
//!
//!     // A Bucket provides typed access to a section of the key/value store
//!     let test = store.bucket::<Raw, Raw>(Some("test"))?;
//!
//!     // Set testing = 123
//!     test.set(b"test", b"123")?;
//!     assert!(test.get(b"test").unwrap().unwrap() == "123");
//!     assert!(test.get(b"something else").unwrap() == None);
//!
//!     // Integer keys
//!     let aaa = store.bucket::<Integer, String>(Some("aaa"))?;
//!     aaa.set(1, "Testing");
//!
//!     #[cfg(feature = "json-value")]
//!     {
//!         // Using a Json encoded type is easy, thanks to Serde
//!         let bucket = store.bucket::<&str, Json<SomeType>>(None)?;
//!
//!         let x = SomeType {a: 1, b: 2};
//!         bucket.set("example", Json(x))?;
//!
//!         let x: Json<SomeType> = bucket.get("example")?.unwrap();
//!
//!         for item in bucket.iter() {
//!             let item = item?;
//!             let key: String = item.key()?;
//!             let value = item.value::<Json<SomeType>>()?;
//!             println!("key: {}, value: {}", key, value);
//!         }
//!
//!         // A transaction
//!         bucket.transaction(|txn| {
//!             txn.set("x", Json(SomeType {a: 1, b: 2}))?;
//!             txn.set("y", Json(SomeType {a: 3, b: 4}))?;
//!             txn.set("z", Json(SomeType {a: 5, b: 6}))?;
//!
//!             // A nested transaction
//!             test.transaction(|txn2| {
//!                 let x = txn.get("x")?.unwrap();
//!                 let v = format!("{}", x.as_ref().a);
//!                 txn2.set(b"x", v.as_str())?;
//!                 Ok(())
//!             })?;
//!             Ok(())
//!         })?;
//!     }
//!     Ok(())
//! }
//! #
//! # fn main() {
//! #     run().unwrap();
//! # }
//! ```

mod bucket;
mod codec;
mod config;
mod error;
mod store;
mod transaction;
mod types;

pub use bucket::{Batch, Bucket, Event, Item, Iter, Watch};
pub use codec::*;
pub use config::Config;
pub use error::Error;
pub use store::Store;
pub use transaction::{Transaction, TransactionError};
pub use types::{Integer, Key, Raw, Value};

/// Abort a transaction
pub fn abort<E>(x: E) -> TransactionError<E> {
    TransactionError::Abort(x)
}

#[cfg(test)]
mod tests;