1 unstable release
new 0.1.0 | Apr 8, 2025 |
---|
#103 in #payload
56 downloads per month
43KB
1K
SLoC
simple_conddb
Simple Condition Database library implemented in Rust, based on Git CondDB.
lib.rs
:
Simple Conditions Database library.
The Conditions Database (CondDB) implementation is based on a CondDB type that access a storage layer via a Backend trait. The Backend maps a &str identifier (e.g. a filesystem path) to a Payload type which must implement the IntoCondition trait, so that the CondDB instance can use the information in the Payload instance returned by the Backend.
Examples
A minimal in-memory CondDB implementation can be as simple as
use simple_conddb::backend::{Backend, ConditionCollection};
use simple_conddb::conddb::CondDB;
use simple_conddb::iov::Iov;
use std::collections::HashMap;
type MyPayload = ConditionCollection<String, Iov<u32>>;
struct MyBackend(HashMap<String, MyPayload>);
// Backend implementation
impl Backend for MyBackend {
type Configuration = HashMap<String, Self::Payload>;
type Payload = MyPayload;
fn connect(config: Self::Configuration) -> Result<Self, String> {
Ok(MyBackend(config))
}
fn get(&self, path: &str) -> Option<Self::Payload> {
self.0.get(path).cloned()
}
fn set(&mut self, path: &str, payload: Self::Payload) {
self.0.insert(String::from(path), payload);
}
}
// Instantiate the backend and fill it with some data
let mut be = MyBackend::connect(HashMap::new()).unwrap();
be.set(
"payload1",
ConditionCollection::Payload(String::from("data 1")),
);
be.set(
"payload2",
ConditionCollection::Payload(String::from("data 2")),
);
be.set(
"condition",
ConditionCollection::Timeline(vec![
(Iov::new(0, 100), String::from("payload1")),
(Iov::new(100, 200), String::from("payload2")),
(Iov::new(200, u32::MAX), String::from("payload1")),
]),
);
// Make a conditions database
let cdb = CondDB::new(be);
// Access conditions
assert_eq!(
cdb.get("condition", 150).unwrap(),
(String::from("data 2"), Iov::new(100, 200))
);
assert_eq!(
cdb.get("condition", 500).unwrap(),
(String::from("data 1"), Iov::new(200, u32::MAX))
);
The simple memory Backend implementation is also provided with a library call:
use simple_conddb::backend::{Backend, ConditionCollection};
use simple_conddb::backend::mem::make_mem_backend;
use simple_conddb::conddb::CondDB;
use simple_conddb::iov::Iov;
// Instantiate the backend and fill it with some data
let mut be = make_mem_backend();
be.set(
"payload1",
ConditionCollection::Payload(String::from("data 1")),
);
be.set(
"payload2",
ConditionCollection::Payload(String::from("data 2")),
);
be.set(
"condition",
ConditionCollection::Timeline(vec![
(Iov::new(0, 100), String::from("payload1")),
(Iov::new(100, 200), String::from("payload2")),
(Iov::new(200, u32::MAX), String::from("payload1")),
]),
);
// Make a conditions database
let cdb = CondDB::new(be);
// Access conditions
assert_eq!(
cdb.get("condition", 150).unwrap(),
(String::from("data 2"), Iov::new(100, 200))
);
assert_eq!(
cdb.get("condition", 500).unwrap(),
(String::from("data 1"), Iov::new(200, u32::MAX))
);
Crate features
The memory backends is always enabled and, by default
also filesystem, JSON and Git backends are enabled, but the last three
can be disabled with default-features = false
and enabling the required
ones with the features fs, json and git.
A simple CLI to query a fs or git backend is available under the feature cli.
Dependencies
~0–12MB
~148K SLoC