Goal: Embedded database that maintains coherence between Rust types and stored data with minimal boilerplate, enjoy 😌🍃.
enum
, struct
, tuple
etc.).get
, watch
, iter
etc.) using explicit type or type inference. insert
, update
and delete
operations.Early development. Not ready for production.
See docs.rs.
```rust use serde::{Deserialize, Serialize}; use struct_db::*;
fn_primary_key(p_key),
fn_secondary_key(s_key),
)] struct Data(u32, String);
impl Data {
// p_key
returns the primary key of the Data
struct as a vector of bytes.
// In this case, it is the big-endian byte representation of the i32
value.
// Using big-endian representation for the primary key maintains a consistent
// lexicographical ordering of the keys, which is useful for ordered key-value
// stores and efficient range queries.
pub fn pkey(&self) -> Vec
// `s_key` generates a secondary key for the `Data` struct as a vector of bytes.
// The secondary key consists of the big-endian byte representation of the `i32` value
// (the primary key) followed by the String field. This combined key allows for more
// versatile querying options.
pub fn skey(&self) -> Vec
fn main() { let mut db = Db::inittmp("mydb").unwrap(); // Initialize the schema db.addschema(Data::structdb_schema());
let data = Data(1,"test".to_string());
// Insert data
let txn = db.transaction().unwrap();
{
let mut tables = txn.tables();
tables.insert(&txn, data).unwrap();
}
txn.commit().unwrap();
// Get data
let txn_read = db.read_transaction().unwrap();
let retrieve_data: Data = txn_read.tables().primary_get(&txn_read, &1_u32.to_be_bytes()).unwrap().unwrap();
assert_eq!(&retrieve_data, &Data(1,"test".to_string()));
// Remove data
let txn = db.transaction().unwrap();
{
let mut tables = txn.tables();
tables.remove(&txn, retrieve_data).unwrap();
}
txn.commit().unwrap();
} ```
The following features are planned before the 1.0 release