-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathkey-value-store.rs
More file actions
64 lines (53 loc) · 2.14 KB
/
Copy pathkey-value-store.rs
File metadata and controls
64 lines (53 loc) · 2.14 KB
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
use std::time::Duration;
use bonsaidb::core::keyvalue::{KeyStatus, KeyValue};
use bonsaidb::local::config::{Builder, StorageConfiguration};
use bonsaidb::local::Database;
// BonsaiDb supports a lightweight, atomic key-value store in addition to its
// acid-compliant transactional storage. This interface is meant to replicate
// functionality that you might use something like Redis for -- lightweight
// caching, or fast atomic operations. As with all of BonsaiDb's core features,
// the Key-Value store is supported across all methods of accessing BonsaiDb.
fn main() -> Result<(), bonsaidb::core::Error> {
let db = Database::open::<()>(StorageConfiguration::new("key-value-store.bonsaidb"))?;
// The set_key method can be awaited to insert/replace a key. Values can be
// anything supported by Serde.
db.set_key("mykey", &1_u32).execute()?;
// Or, you can customize it's behavior:
let old_value = db
.set_key("mykey", &2_u32)
.only_if_exists()
.returning_previous_as()?;
assert_eq!(old_value, Some(1_u32));
// Retrieving is simple too.
let value = db.get_key("mykey").into()?;
assert_eq!(value, Some(2_u32));
// Namespacing is built-in as well, so that you can easily separate storage.
let value = db
.with_key_namespace("anamespace")
.get_key("mykey")
.query()?;
assert!(value.is_none());
// Because of the atomic nature of the key-value store, you can use set_key
// as a synchronized lock:
let result = db
.set_key("lockname", &()) // A useful value would be the unique id of the worker that aquired the lock.
.only_if_vacant()
.expire_in(Duration::from_millis(100))
.execute()?;
assert_eq!(result, KeyStatus::Inserted);
let second_try = db
.set_key("lockname", &())
.only_if_vacant()
.expire_in(Duration::from_millis(100))
.execute()?;
assert_eq!(second_try, KeyStatus::NotChanged);
Ok(())
}
#[test]
fn runs() {
let path = std::path::Path::new("key-value-store.bonsaidb");
if path.exists() {
std::fs::remove_dir_all(path).unwrap();
}
main().unwrap()
}