nut/
lib.rs

1//! # Nut
2//!
3//! Key-value embed database, which is basically port of Bolt DB
4//!
5//! # Examples
6//! ## Create db and put something
7//! ```no_run
8//! use nut::DBBuilder;
9//!
10//! let mut db = DBBuilder::new("test.db").build().unwrap();
11//! let mut tx = db.begin_rw_tx().unwrap();
12//! {
13//!   let mut flowers = tx.create_bucket(b"flowers").unwrap();
14//!   // returns mutable reference to bucket,
15//!   // which prevents of reborrowing tx
16//!
17//!   flowers.put(
18//!     b"iris",
19//!     b"song by American alternative rock band Goo Goo Dolls".to_vec()
20//!   ).unwrap();
21//!   flowers.put(
22//!     b"binary data",
23//!     vec![127, 127, 127, 127]
24//!   ).unwrap();
25//!
26//!   {
27//!     // you can create subbuckets as well
28//!     let mut annuals = flowers
29//!       .create_bucket_if_not_exists(b"annuals").unwrap();
30//!
31//!     // api is basically same as for transaction
32//!     annuals.put(
33//!       b"corn",
34//!       b"American nu metal band from Bakersfield".to_vec()
35//!     ).unwrap();
36//!     // releasing subbucket
37//!   }
38//!
39//!   // releasing bucket to be able use tx again
40//! }
41//! // due to RAII tx will be automatically closed if no error occured,
42//! // or rolled back if there was some.
43//! // Additionally you can commit or rollback manually
44//! tx.rollback().unwrap();
45//! ```
46//!
47//! ## Getting data back
48//! ```no_run
49//! use nut::DBBuilder;
50//!
51//! let mut db = DBBuilder::new("test.db").build().unwrap();
52//!
53//! // creating read only transaction
54//! // read only ransaction will be automatically rolled back
55//! let mut tx = db.begin_tx().unwrap();
56//!
57//! // getting bucket
58//! let flowers = tx.bucket(b"flowers").unwrap();
59//! let data = flowers.get(b"iris").unwrap();
60//! assert_eq!(
61//!   data,
62//!   &b"song by American alternative rock band Goo Goo Dolls"[..]
63//! );
64//! ```
65//!
66//! ## Getting available buckets
67//! ```no_run
68//! use nut::DBBuilder;
69//!
70//! let mut db = DBBuilder::new("test.db").build().unwrap();
71//! let mut tx = db.begin_tx().unwrap();
72//!
73//! {
74//!   // .buckets() available to conveniently retrieve all buckets keys
75//!   let bucket_names = tx.buckets(); // returns Vec<Vec<u8>>
76//!
77//!   // bucket key is any binary data, not only string
78//!   assert_eq!(bucket_names, vec!["flowers".as_bytes().to_vec()]);
79//! }
80//!
81//! {
82//!   // additionally there is .cursor() method
83//!   // that returns Cursor struct,
84//!   // which is able to iterate through bucket contents
85//!   let cursor = tx.cursor();
86//!
87//!   assert_eq!(
88//!     &cursor.first().unwrap().value.unwrap(),
89//!     &"flowers".as_bytes()
90//!   );
91//! }
92//! ```
93
94#![allow(clippy::cast_ptr_alignment, clippy::module_inception)]
95
96#[macro_use]
97extern crate memoffset;
98
99#[macro_use]
100extern crate bitflags;
101
102#[cfg(test)]
103mod test_utils;
104
105#[cfg(test)]
106mod concurrency_tests;
107
108mod bucket;
109mod consts;
110mod db;
111mod errors;
112mod freelist;
113mod meta;
114mod node;
115mod page;
116mod tx;
117mod utils;
118
119pub use bucket::{Bucket, BucketStats, Cursor, CursorItem};
120pub use consts::Flags;
121pub use db::{CheckMode, DBBuilder, RWTxGuard, Stats as DBStats, TxGuard, DB};
122pub use errors::Error;
123pub use page::FreedOrPageInfo;
124pub use tx::{Tx, TxStats};