llrb_index/lib.rs
1//! LLRB, [left-leaning-red-black][wiki-llrb], tree is memory optimized data
2//! structure for indexing sortable data. This package provides a basic
3//! implementation with following properties:
4//!
5//! - Each entry in LLRB instance correspond to a {Key, Value} pair.
6//! - Parametrised over Key type and Value type.
7//! - CRUD operations, via create(), set(), get(), delete() api.
8//! - No Durability guarantee.
9//! - Not thread safe.
10//! - Full table scan, to iterate over all entries.
11//! - Range scan, to iterate between a ``low`` and ``high``.
12//! - Reverse iteration.
13//!
14//! [Llrb] instance and its API uses Rust's ownership model and borrow
15//! semantics to ensure thread safe operation.
16//!
17//! Constructing a new [Llrb] instance:
18//! ```
19//! use llrb_index::Llrb;
20//! let llrb: Llrb<i32,i32> = Llrb::new("myinstance");
21//! let id = llrb.id();
22//! assert_eq!(id, "myinstance");
23//! ```
24//!
25//! CRUD operations on [Llrb] instance:
26//! ```
27//! use llrb_index::Llrb;
28//! let mut llrb: Llrb<String,String> = Llrb::new("myinstance");
29//!
30//! llrb.create("key1".to_string(), "value1".to_string());
31//! llrb.create("key2".to_string(), "value2".to_string());
32//! llrb.set("key2".to_string(), "value3".to_string());
33//!
34//! let n = llrb.len();
35//! assert_eq!(n, 2);
36//!
37//! let value = llrb.get("key1").unwrap();
38//! assert_eq!(value, "value1".to_string());
39//! let value = llrb.get("key2").unwrap();
40//! assert_eq!(value, "value3".to_string());
41//!
42//! let old_value = llrb.delete("key1").unwrap();
43//! assert_eq!(old_value, "value1".to_string());
44//! ```
45//!
46//! Full table scan:
47//! ```
48//! use llrb_index::Llrb;
49//! let mut llrb: Llrb<String,String> = Llrb::new("myinstance");
50//! llrb.set("key1".to_string(), "value1".to_string());
51//! llrb.set("key2".to_string(), "value2".to_string());
52//!
53//! for (i, (key, value)) in llrb.iter().enumerate() {
54//! let refkey = format!("key{}", i+1);
55//! let refval = format!("value{}", i+1);
56//! assert_eq!(refkey, key);
57//! assert_eq!(refval, value);
58//! }
59//! ```
60//!
61//! Range scan:
62//! ```
63//! use std::ops::Bound;
64//! use llrb_index::Llrb;
65//! let mut llrb: Llrb<String,String> = Llrb::new("myinstance");
66//!
67//! llrb.set("key1".to_string(), "value1".to_string());
68//! llrb.set("key2".to_string(), "value2".to_string());
69//! llrb.set("key3".to_string(), "value3".to_string());
70//!
71//! let low = Bound::Excluded("key1");
72//! let high = Bound::Excluded("key2");
73//! let item = llrb.range::<str, _>((low, high)).next();
74//! assert_eq!(item, None);
75//!
76//! let low = Bound::Excluded("key1");
77//! let high = Bound::Excluded("key3");
78//! let item = llrb.range::<str, _>((low, high)).next();
79//! assert_eq!(item, Some(("key2".to_string(), "value2".to_string())));
80//!
81//! let low = Bound::Included("key1");
82//! let high = Bound::Included("key3");
83//! let mut ranger = llrb.range::<str, _>((low, high));
84//! let item = ranger.next();
85//! assert_eq!(item, Some(("key1".to_string(), "value1".to_string())));
86//! let item = ranger.last();
87//! assert_eq!(item, Some(("key3".to_string(), "value3".to_string())));
88//! ```
89//!
90//! Reverse scan:
91//! ```
92//! use std::ops::Bound;
93//! use llrb_index::Llrb;
94//! let mut llrb: Llrb<String,String> = Llrb::new("myinstance");
95//!
96//! llrb.set("key1".to_string(), "value1".to_string());
97//! llrb.set("key2".to_string(), "value2".to_string());
98//! llrb.set("key3".to_string(), "value3".to_string());
99//!
100//! let low = Bound::Included("key1");
101//! let high = Bound::Included("key3");
102//! let mut iter = llrb.reverse::<_, str>((low, high));
103//! let item = iter.next();
104//! assert_eq!(item, Some(("key3".to_string(), "value3".to_string())));
105//! let item = iter.last();
106//! assert_eq!(item, Some(("key1".to_string(), "value1".to_string())));
107//! ```
108//!
109//! [wiki-llrb]: https://en.wikipedia.org/wiki/Left-leaning_red-black_tree
110mod depth;
111mod empty;
112mod error;
113mod llrb;
114
115pub use crate::depth::Depth;
116pub use crate::empty::Empty;
117pub use crate::error::Error;
118pub use crate::llrb::Llrb;
119pub use crate::llrb::Stats;