use std::io;
use std::path::Path;
use std::sync::Arc;
use bstack::FirstFitBStackAllocator;
use bstack_raii::{
BStack, BStackAllocator, BStackBlock, BStackDrop, Foreign, TryCloneIn, bstack_block, registry,
};
#[bstack_block]
struct Document {
size: u64,
checksum: u64,
}
#[bstack_block]
struct Card {
title: String,
#[bstack_owned]
body: Foreign<Document>,
}
#[bstack_block]
struct Bundle {
name: String,
#[bstack_owned]
parts: Vec<Foreign<Document>>,
}
fn main() -> io::Result<()> {
let dir = std::env::temp_dir();
let registry_path = dir.join("bstack_raii_registry.bstack");
let store_path = dir.join("bstack_raii_store.bstack");
let catalog_path = dir.join("bstack_raii_catalog.bstack");
for p in [®istry_path, &store_path, &catalog_path] {
let _ = std::fs::remove_file(p);
}
registry::init(®istry_path)?;
let catalog = FirstFitBStackAllocator::new(BStack::open(&catalog_path)?)?;
let store = Arc::new(FirstFitBStackAllocator::new(BStack::open(&store_path)?)?);
let store_id = registry::get()
.unwrap()
.attach(&store_path, store.clone())?;
println!("store attached as file id {}", store_id.get());
let doc = Document::new(&*store, 4096, 0xDEAD_BEEF)?;
let doc_off = doc.handle().range().start();
let extra: Vec<u64> = (0u64..3)
.map(|i| {
let d = Document::new(&*store, 1000 + i, i).unwrap();
d.handle().range().start()
})
.collect();
let card = Card::new(&catalog, "annual-report", unsafe {
Foreign::<Document>::new(store_id, doc_off)
})?;
let (size, sum) = card
.handle()
.get_body(catalog.stack())?
.with(&catalog, |d, fs| {
(d.get_size(fs).unwrap(), d.get_checksum(fs).unwrap())
})?
.expect("owned Foreign is never null");
println!("card 'annual-report' -> document size {size}, checksum {sum:#x}");
let card_copy = card.try_clone_in(&catalog)?;
let copy_off = card_copy.handle().get_body(catalog.stack())?.offset().get();
println!(
"cloned card -> independent document at store offset {copy_off} (original at {doc_off})"
);
assert_ne!(
copy_off, doc_off,
"the clone must not alias the original document"
);
let ptrs: Vec<Foreign<Document>> = extra
.iter()
.map(|&off| unsafe { Foreign::<Document>::new(store_id, off) })
.collect();
let bundle = Bundle::new(&catalog, "q3-batch", ptrs)?;
let bundle_sizes: Vec<u64> = bundle
.handle()
.get_parts(&catalog)?
.into_iter()
.map(|f| {
f.with(&catalog, |d, fs| d.get_size(fs).unwrap())
.unwrap()
.expect("owned Foreign is never null")
})
.collect();
println!(
"bundle 'q3-batch' -> {} documents, sizes {bundle_sizes:?}",
bundle_sizes.len()
);
let frontier = store.len()?;
card.bstack_drop(&catalog)?;
card_copy.bstack_drop(&catalog)?;
bundle.bstack_drop(&catalog)?;
let probe = Document::new(&*store, 1, 1)?;
let probe_off = probe.handle().range().start();
println!(
"after teardown, a new store document reuses freed offset {probe_off} (frontier was {frontier})"
);
assert!(
probe_off < frontier,
"cross-file teardown should have freed the documents' store space for reuse"
);
probe.bstack_drop(&*store)?;
println!("every document was reclaimed across the file boundary");
registry::detach(store_id);
for p in [®istry_path, &store_path, &catalog_path] {
let _ = std::fs::remove_file(p as &Path);
}
Ok(())
}