[−][src]Crate mhdb
MHdb is a pure Rust embedded key-value store database, based on dbm.
Simple use
use mhdb::{Db, prelude::*}; let mut db = Db::open("mydb")?; let key = "Number".to_owned(); let val = 42i32; db.store(key.clone(), val)?; let num: Option<i32> = db.fetch(42u8)?; assert_eq!(num, Some(val)); println!("{}", num.unwrap());
Any type implementing Datum
can be stored in the database.
Datum
A datum represents a single item in the database. Types
used as keys and/or values must therefore implement the
Datum
trait.
Datum
is automatically implemented on any type implementing
the Serde traits Serialize
and Deserialize
.
In-memory database
Any type which implements the Source
trait can be used
as database sources. This trait is in turn automatically
implemented for any type which implements the Read
,
Write
, Seek
, Sync
, and Send
standard library traits.
A vector may therefore be used as an in-memory database.
use mhdb::{Db, prelude::*}; use std::io::Cursor; let dirf = Cursor::new(Vec::<u8>::new()); let pagf = Cursor::new(Vec::<u8>::new()); let mut db: Db = Db::with_sources(Box::new(pagf), Box::new(dirf))?; db.store(42u8, "Hello world".to_owned())?; let txt: Option<String> = db.fetch(42u8)?; assert!(txt.is_some()); println!("{}", txt.unwrap());
Limitations
- Key-value pairs can not be larger than 506B
Db
objects are not thread safe
Modules
prelude | The Db prelude. |
Structs
Db | The database object. All interraction with mhdb databases is done through this object. |
Enums
Error | Errors returned by database operations. |
Traits
Datum | A datum represents a single item in the database. Any type used as
key or value needs to implement |
Source | Trait required by database sources. Automatically implemented for standard
library types such as |