use std::collections::BTreeMap;
use std::path::PathBuf;
use std::sync::mpsc::{self, Receiver, Sender};
use std::sync::{Mutex, OnceLock};
use std::thread;
use std::time::{Duration, Instant};
use get_size2::GetSize;
use objshelf::backend::fs::{FsBackend, FsConfig, FsError};
use objshelf::{Error, Handle, ObjShelf};
use serde::{Deserialize, Serialize};
type Mem = ObjShelf<FsBackend>;
const ROUNDS: u32 = 20;
const PERIOD_MS: u32 = 250;
const SAVE_EVERY: u32 = 5;
const SAMPLES: usize = 64;
const CACHE_BYTES: usize = 16 * 1024;
#[derive(Serialize, Deserialize, GetSize)]
struct Settings {
brightness: u8,
name: String,
}
impl Default for Settings {
fn default() -> Self {
Self {
brightness: 0,
name: "objshelf example".into(),
}
}
}
#[derive(Default, Serialize, Deserialize, GetSize)]
struct Samples(Vec<u16>);
fn read_sensor(round: u32) -> u16 {
2000 + (round * 37 % 100) as u16
}
const STEPS: usize = 512;
#[derive(Serialize, Deserialize, GetSize)]
struct Pattern {
steps: Vec<Step>,
edits: u32,
}
#[derive(Clone, Copy, Serialize, Deserialize, GetSize)]
struct Step {
note: u8,
velocity: u8,
}
impl Default for Pattern {
fn default() -> Self {
let step = Step {
note: 60,
velocity: 100,
};
Self {
steps: vec![step; STEPS],
edits: 0,
}
}
}
impl Pattern {
fn edit(&mut self) {
let step = self.edits as usize % STEPS;
self.steps[step].note = 36 + (self.edits % 48) as u8;
self.edits += 1;
}
fn summary(&self) -> (usize, u32, u32) {
let notes = self.steps.iter().map(|step| u32::from(step.note)).sum();
(self.steps.len(), self.edits, notes)
}
}
#[derive(Clone, Copy)]
struct Objects {
settings: Handle<Settings>,
samples: Handle<Samples>,
pattern: Handle<Pattern>,
}
enum Save {
All,
Settings(Handle<Settings>),
Report(Objects),
}
static SAVER: OnceLock<Sender<Save>> = OnceLock::new();
struct SendReport<'a> {
saver: &'a Sender<Save>,
objects: Objects,
}
impl Drop for SendReport<'_> {
fn drop(&mut self) {
self.saver.send(Save::Report(self.objects)).ok();
}
}
struct Shared<'a>(&'a Mutex<Mem>);
impl objshelf::Mutex for Shared<'_> {
type T = Mem;
fn lock<R>(&mut self, f: impl FnOnce(&mut Mem) -> R) -> R {
f(&mut self.0.lock().expect("no thread panics with the lock"))
}
}
#[derive(Default)]
struct Time {
calls: u32,
total: Duration,
longest: Duration,
}
static TIMES: Mutex<BTreeMap<&str, Time>> = Mutex::new(BTreeMap::new());
fn timed<R>(name: &'static str, call: impl FnOnce() -> R) -> R {
let start = Instant::now();
let result = call();
let elapsed = start.elapsed();
let mut times = TIMES.lock().expect("no thread panics with the lock");
let time = times.entry(name).or_default();
time.calls += 1;
time.total += elapsed;
time.longest = time.longest.max(elapsed);
result
}
fn print_times() {
println!("time in objshelf:");
println!(
" {:<18} {:>5} {:>10} {:>10}",
"function", "calls", "mean", "longest"
);
for (name, time) in TIMES.lock().expect("no thread panics with the lock").iter() {
let mean = time.total / time.calls;
println!(
" {name:<18} {:>5} {mean:>10.1?} {:>10.1?}",
time.calls, time.longest,
);
}
}
fn main() -> Result<(), Error<FsError>> {
let dir = std::env::args_os().nth(1).map_or_else(
|| std::env::temp_dir().join("objshelf-std-fs"),
PathBuf::from,
);
println!("objects in {}", dir.display());
let config = FsConfig {
dir,
capacity: 1 << 20,
direct_io: true,
};
let (saver, work) = mpsc::channel();
SAVER.set(saver.clone()).ok();
let mut mem = timed("mount", || {
Mem::mount(config, CACHE_BYTES, || {
if let Some(saver) = SAVER.get() {
saver.send(Save::All).ok();
}
})
})?;
let stored = timed("list", || objshelf::list(&mut mem));
let origin = |name: &str| {
if stored.iter().any(|stored| stored == name) {
"found"
} else {
"created"
}
};
let boots = timed("get_or_insert_with", || {
objshelf::get_or_insert_with(&mut mem, "boots", || 0u32)
})?
.expect("fits");
let boot = timed("with_mut", || {
objshelf::with_mut(&mut mem, &boots, |n| {
*n += 1;
*n
})
})?
.expect("nothing else has the memory");
timed("flush_object", || objshelf::flush_object(&mut mem, &boots))?;
println!("boot {boot}");
let settings = timed("get_or_insert_with", || {
objshelf::get_or_insert_with(&mut mem, "settings", Settings::default)
})?;
let settings = settings.expect("fits");
let samples = timed("get_or_insert_with", || {
objshelf::get_or_insert_with(&mut mem, "samples", Samples::default)
})?;
let samples = samples.expect("fits");
let pattern = timed("get_or_insert_with", || {
objshelf::get_or_insert_with(&mut mem, "pattern", Pattern::default)
})?;
let pattern = pattern.expect("fits");
let (name, brightness) = timed("with", || {
objshelf::with(&mut mem, &settings, |s| (s.name.clone(), s.brightness))
})?
.expect("nothing else has the memory");
println!(
"{} settings: {name:?}, brightness {brightness}",
origin("settings")
);
let (count, last) = timed("with", || {
objshelf::with(&mut mem, &samples, |s| (s.0.len(), s.0.last().copied()))
})?
.expect("nothing else has the memory");
println!("{} samples: {count}, last {last:?}", origin("samples"));
let (steps, edits, notes) = timed("with", || {
objshelf::with(&mut mem, &pattern, Pattern::summary)
})?
.expect("nothing else has the memory");
println!(
"{} pattern: {steps} steps, {edits} edits, sum of notes {notes}",
origin("pattern")
);
let objects = Objects {
settings,
samples,
pattern,
};
let mem = Mutex::new(mem);
thread::scope(|scope| {
let thread = scope.spawn(|| save(Shared(&mem), work));
let done = {
let _report = SendReport {
saver: &saver,
objects,
};
app(Shared(&mem), &saver, objects)
};
let saved = thread
.join()
.unwrap_or_else(|panic| std::panic::resume_unwind(panic));
done.and(saved)
})?;
let mem = mem.into_inner().expect("no thread panics with the lock");
timed("unmount", || mem.unmount())?;
print_times();
Ok(())
}
fn app(mut mem: Shared, saver: &Sender<Save>, objects: Objects) -> Result<(), Error<FsError>> {
let Objects {
settings,
samples,
pattern,
} = objects;
for round in 1..=ROUNDS {
thread::sleep(Duration::from_millis(PERIOD_MS.into()));
let brightness = timed("with_mut", || {
objshelf::with_mut(&mut mem, &settings, |s| {
s.brightness = s.brightness.wrapping_add(1);
s.brightness
})
})?;
match brightness {
Some(brightness) => {
println!("round {round}: brightness {brightness}");
saver.send(Save::Settings(settings)).ok();
}
None => println!("round {round}: busy, next round"),
}
let reading = read_sensor(round);
let added = timed("with_mut", || {
objshelf::with_mut(&mut mem, &samples, |s| {
if s.0.len() == SAMPLES {
s.0.remove(0);
}
s.0.push(reading);
})
})?;
if added.is_none() {
println!("round {round}: busy, sample {reading} dropped");
}
let edited = timed("with_mut", || {
objshelf::with_mut(&mut mem, &pattern, Pattern::edit)
})?;
if edited.is_none() {
println!("round {round}: busy, pattern edit dropped");
}
if round % SAVE_EVERY == 0 {
saver.send(Save::All).ok();
}
}
Ok(())
}
fn save(mut mem: Shared, work: Receiver<Save>) -> Result<(), Error<FsError>> {
for job in work {
match job {
Save::All => {
timed("flush", || objshelf::flush(&mut mem))?;
println!("saved all the changes");
}
Save::Settings(settings) => {
timed("flush_object", || {
objshelf::flush_object(&mut mem, &settings)
})?;
}
Save::Report(objects) => return report(mem, objects),
}
}
Ok(())
}
fn report(mut mem: Shared, objects: Objects) -> Result<(), Error<FsError>> {
let Objects {
settings,
samples,
pattern,
} = objects;
timed("flush", || objshelf::flush(&mut mem))?;
let mut names = timed("list", || objshelf::list(&mut mem));
names.sort();
println!("objects: {names:?}");
let settings = timed("with", || {
objshelf::with(&mut mem, &settings, |s| (s.name.clone(), s.brightness))
})?;
if let Some((name, brightness)) = settings {
println!("settings: {name:?}, brightness {brightness}");
}
let samples = timed("with", || {
objshelf::with(&mut mem, &samples, |s| (s.0.len(), s.0.last().copied()))
})?;
if let Some((count, last)) = samples {
println!("samples: {count}, last {last:?}");
}
let pattern = timed("with", || {
objshelf::with(&mut mem, &pattern, Pattern::summary)
})?;
if let Some((steps, edits, notes)) = pattern {
println!("pattern: {steps} steps, {edits} edits, sum of notes {notes}");
}
println!("usage: {:?}", timed("usage", || objshelf::usage(&mut mem)));
match timed("wear", || objshelf::wear(&mut mem)) {
Some(wear) => println!("wear: {wear:?}"),
None => println!("wear: not tracked by this backend"),
}
Ok(())
}