use microrm::prelude::*;
mod schema {
use microrm::prelude::*;
#[derive(Entity, PartialEq, Clone)]
pub struct KVEntry {
#[key]
pub key: String,
pub value: Vec<u8>,
}
#[derive(Schema)]
pub struct KVSchema {
pub kvs: microrm::IDMap<KVEntry>,
}
}
#[global_allocator]
static ALLOCATOR: alloc_tracker::Allocator<std::alloc::System> = alloc_tracker::Allocator::system();
fn main() {
let (cp, schema): (_, schema::KVSchema) =
microrm::ConnectionPool::open(":memory:").expect("could not open in-memory database?");
cp.run_transaction(1, |txn| {
schema.kvs.insert_ref(
txn,
schema::KVEntryRef {
key: "keyA",
value: &[],
},
)?;
schema.kvs.insert_ref(
txn,
schema::KVEntryRef {
key: "keyB",
value: &[1, 2, 3],
},
)?;
schema.kvs.insert_ref(
txn,
schema::KVEntryRef {
key: "keyC",
value: &[3, 2, 1],
},
)?;
Ok(())
})
.expect("could not insert initial data");
let session = alloc_tracker::Session::new();
cp.run_transaction(1, |txn| {
schema.kvs.iter(txn)?.for_each(|_| ());
schema
.kvs
.iter_refs(txn, |_| std::ops::ControlFlow::Continue(()))?;
Ok(())
})
.expect("warmup failed");
{
println!("iter():");
let operation = session.operation("iter()");
let _span = operation.measure_thread();
cp.run_transaction(1, |txn| {
for kv in schema.kvs.iter(txn)? {
let kv = kv?;
println!("kv entry: key = {}, values = {:?}", kv.key, kv.value);
}
Ok(())
})
.expect("could not iterate over data");
}
{
println!("iter_refs():");
let operation = session.operation("iter_refs()");
let _span = operation.measure_thread();
cp.run_transaction(1, |txn| {
schema.kvs.iter_refs(txn, |kv| {
println!("kv entry: key = {}, values = {:?}", kv.key, kv.value);
std::ops::ControlFlow::Continue(())
})
})
.expect("could not iterate over data");
}
session.print_to_stdout();
}