use crate::locked;
use crate::storage::{SessionStorage, StorageChange, StorageSubscription};
use crate::types::ResourceId;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
type Gate = Box<dyn Fn() -> bool + Send + Sync>;
struct Coupling {
storage: Arc<dyn SessionStorage>,
key: String,
pending: Mutex<HashMap<ResourceId, String>>,
gate: Mutex<Option<Gate>>,
}
impl Coupling {
fn stored(stored: Option<&str>) -> HashMap<ResourceId, String> {
stored
.and_then(|stored| serde_json::from_str(stored).ok())
.unwrap_or_default()
}
fn flush(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let reached = {
let mut pending = locked(&self.pending);
let at_rest = locked(&self.gate).as_ref().is_none_or(|gate| gate());
if pending.is_empty() || !at_rest {
return Ok(());
}
std::mem::take(&mut *pending)
};
self.storage.update(&self.key, &mut |stored| {
let mut places = Coupling::stored(stored);
places.extend(reached.clone());
serde_json::to_string(&places).ok()
})
}
}
#[derive(Clone)]
pub struct CoupledBookmarks {
coupling: Arc<Coupling>,
}
impl CoupledBookmarks {
pub fn new(storage: Arc<dyn SessionStorage>, key: &str) -> CoupledBookmarks {
CoupledBookmarks {
coupling: Arc::new(Coupling {
storage,
key: key.to_owned(),
pending: Mutex::new(HashMap::new()),
gate: Mutex::new(None),
}),
}
}
pub fn load(&self) -> HashMap<ResourceId, String> {
Coupling::stored(self.coupling.storage.get(&self.coupling.key).as_deref())
}
pub fn save(&self, scope: &ResourceId, event_id: &str) {
locked(&self.coupling.pending).insert(scope.clone(), event_id.to_owned());
}
pub fn set_flush_gate(&self, at_rest: impl Fn() -> bool + Send + Sync + 'static) {
*locked(&self.coupling.gate) = Some(Box::new(at_rest));
}
pub fn storage(&self) -> Arc<dyn SessionStorage> {
Arc::new(Carrying(self.coupling.clone()))
}
}
struct Carrying(Arc<Coupling>);
impl SessionStorage for Carrying {
fn get(&self, key: &str) -> Option<String> {
self.0.storage.get(key)
}
fn set(&self, key: &str, value: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
self.0.storage.set(key, value)?;
self.0.flush()
}
fn delete(&self, key: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
self.0.storage.delete(key)
}
fn update(
&self,
key: &str,
change: &mut dyn FnMut(Option<&str>) -> Option<String>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
self.0.storage.update(key, change)?;
self.0.flush()
}
fn subscribe(&self, on_change: StorageChange) -> Option<StorageSubscription> {
self.0.storage.subscribe(on_change)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::storage::InMemorySessionStorage;
use std::sync::atomic::{AtomicBool, Ordering};
const KEY: &str = "semiont.lastEventId.kb-a";
fn places(pairs: &[(&str, &str)]) -> HashMap<ResourceId, String> {
pairs
.iter()
.map(|(scope, id)| (scope.parse().unwrap(), (*id).to_owned()))
.collect()
}
#[test]
fn a_place_is_written_with_the_next_write_of_a_cache_and_not_before() {
let storage = Arc::new(InMemorySessionStorage::new());
let bookmarks = CoupledBookmarks::new(storage.clone(), KEY);
let caches = bookmarks.storage();
assert!(bookmarks.load().is_empty());
bookmarks.save(&"res-1".parse().unwrap(), "p-7");
bookmarks.save(&"res-1".parse().unwrap(), "p-8");
bookmarks.save(&"res-2".parse().unwrap(), "p-3");
assert_eq!(storage.get(KEY), None);
caches
.set("semiont.cache.kb-a.resource", "{}")
.expect("written");
assert_eq!(
storage.get("semiont.cache.kb-a.resource").as_deref(),
Some("{}")
);
assert_eq!(
bookmarks.load(),
places(&[("res-1", "p-8"), ("res-2", "p-3")])
);
bookmarks.save(&"res-2".parse().unwrap(), "p-4");
caches
.update("semiont.cache.kb-a.resource", &mut |_| {
Some("{}".to_owned())
})
.expect("written");
assert_eq!(
bookmarks.load(),
places(&[("res-1", "p-8"), ("res-2", "p-4")])
);
}
#[test]
fn a_place_waits_for_the_caches_to_be_at_rest() {
let storage = Arc::new(InMemorySessionStorage::new());
let bookmarks = CoupledBookmarks::new(storage.clone(), KEY);
let caches = bookmarks.storage();
let at_rest = Arc::new(AtomicBool::new(false));
let gate = at_rest.clone();
bookmarks.set_flush_gate(move || gate.load(Ordering::SeqCst));
bookmarks.save(&"res-1".parse().unwrap(), "p-7");
caches
.set("semiont.cache.kb-a.resource", "{}")
.expect("written");
assert_eq!(storage.get(KEY), None);
at_rest.store(true, Ordering::SeqCst);
caches
.set("semiont.cache.kb-a.annotations", "{}")
.expect("written");
assert_eq!(bookmarks.load(), places(&[("res-1", "p-7")]));
}
#[test]
fn what_is_kept_under_the_key_and_is_not_places_is_nothing_kept() {
let storage = Arc::new(InMemorySessionStorage::new());
for kept in ["not json", "[1, 2]", r#"{"res-1": 7}"#] {
storage.set(KEY, kept).expect("written");
assert!(
CoupledBookmarks::new(storage.clone(), KEY)
.load()
.is_empty(),
"{kept}"
);
}
let bookmarks = CoupledBookmarks::new(storage.clone(), KEY);
bookmarks.save(&"res-1".parse().unwrap(), "p-1");
bookmarks
.storage()
.set("semiont.cache.kb-a.resource", "{}")
.expect("written");
assert_eq!(bookmarks.load(), places(&[("res-1", "p-1")]));
}
#[derive(Default)]
struct Full(InMemorySessionStorage);
const CACHE: &str = "semiont.cache.kb-a.resource";
impl SessionStorage for Full {
fn get(&self, key: &str) -> Option<String> {
self.0.get(key)
}
fn set(
&self,
key: &str,
value: &str,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
if key == CACHE {
return Err("the storage is full".into());
}
self.0.set(key, value)
}
fn delete(&self, key: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
self.0.delete(key)
}
fn update(
&self,
key: &str,
change: &mut dyn FnMut(Option<&str>) -> Option<String>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
match change(self.0.get(key).as_deref()) {
Some(next) => self.set(key, &next),
None => self.delete(key),
}
}
fn subscribe(&self, _: StorageChange) -> Option<StorageSubscription> {
None
}
}
#[test]
fn a_place_is_written_after_the_caches_own_write_so_one_that_fails_leaves_no_place_ahead_of_it()
{
let storage = Arc::new(Full::default());
let bookmarks = CoupledBookmarks::new(storage.clone(), KEY);
bookmarks.save(&"res-1".parse().unwrap(), "p-7");
let failed = bookmarks.storage().set(CACHE, "{}");
assert_eq!(
failed.map_err(|error| error.to_string()),
Err("the storage is full".to_owned())
);
assert_eq!(storage.get(KEY), None);
bookmarks
.storage()
.set("semiont.cache.kb-a.annotations", "{}")
.expect("written");
assert_eq!(bookmarks.load(), places(&[("res-1", "p-7")]));
}
}