use std::collections::BTreeMap;
use std::sync::{Arc, Mutex, MutexGuard};
use serde_json::Value;
use super::handler::entity_tag;
use super::store::{Matched, Replaced, ResourceStore, Selection, StoreResult};
type Collections = BTreeMap<String, Vec<Value>>;
#[derive(Debug, Clone, Default)]
pub struct MemoryStore {
collections: Arc<Mutex<Collections>>,
}
impl MemoryStore {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn seed(&self, collection: &str, resource: Value) {
self.lock()
.entry(collection.to_owned())
.or_default()
.push(resource);
}
pub fn seed_all(&self, collection: &str, resources: impl IntoIterator<Item = Value>) {
self.lock()
.entry(collection.to_owned())
.or_default()
.extend(resources);
}
#[must_use]
pub fn collection(&self, collection: &str) -> Vec<Value> {
self.lock().get(collection).cloned().unwrap_or_default()
}
pub fn clear(&self) {
self.lock().clear();
}
fn lock(&self) -> MutexGuard<'_, Collections> {
self.collections
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
}
fn id_of(resource: &Value) -> Option<&str> {
resource.get("id").and_then(Value::as_str)
}
#[async_trait::async_trait]
impl ResourceStore for MemoryStore {
async fn list(&self, collection: &str, selection: &Selection) -> StoreResult<Matched> {
Ok(selection.apply(self.collection(collection)))
}
async fn get(&self, collection: &str, id: &str) -> StoreResult<Option<Value>> {
Ok(self
.lock()
.get(collection)
.and_then(|items| items.iter().find(|item| id_of(item) == Some(id)).cloned()))
}
async fn create(&self, collection: &str, resource: Value) -> StoreResult<Value> {
self.seed(collection, resource.clone());
Ok(resource)
}
async fn replace(
&self,
collection: &str,
id: &str,
resource: Value,
) -> StoreResult<Option<Value>> {
let mut held = self.lock();
let Some(items) = held.get_mut(collection) else {
return Ok(None);
};
let Some(slot) = items.iter_mut().find(|item| id_of(item) == Some(id)) else {
return Ok(None);
};
*slot = resource.clone();
Ok(Some(resource))
}
async fn delete(&self, collection: &str, id: &str) -> StoreResult<bool> {
let mut held = self.lock();
let Some(items) = held.get_mut(collection) else {
return Ok(false);
};
let before = items.len();
items.retain(|item| id_of(item) != Some(id));
Ok(items.len() != before)
}
async fn replace_if_unchanged(
&self,
collection: &str,
id: &str,
resource: Value,
expected_tag: &str,
) -> StoreResult<Replaced> {
let mut held = self.lock();
let Some(slot) = held
.get_mut(collection)
.and_then(|items| items.iter_mut().find(|item| id_of(item) == Some(id)))
else {
return Ok(Replaced::Missing);
};
if entity_tag(slot) != expected_tag {
return Ok(Replaced::Stale);
}
*slot = resource.clone();
Ok(Replaced::Updated(resource))
}
async fn delete_if_unchanged(
&self,
collection: &str,
id: &str,
expected_tag: &str,
) -> StoreResult<Replaced> {
let mut held = self.lock();
let Some(items) = held.get_mut(collection) else {
return Ok(Replaced::Missing);
};
let Some(at) = items.iter().position(|item| id_of(item) == Some(id)) else {
return Ok(Replaced::Missing);
};
if entity_tag(&items[at]) != expected_tag {
return Ok(Replaced::Stale);
}
Ok(Replaced::Updated(items.remove(at)))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn clones_share_one_store() {
let store = MemoryStore::new();
let other = store.clone();
other.seed("thing", serde_json::json!({"id": "1"}));
assert_eq!(store.collection("thing").len(), 1);
assert!(store.get("thing", "1").await.unwrap().is_some());
}
#[tokio::test]
async fn replacing_an_absent_resource_reports_it_rather_than_inserting() {
let store = MemoryStore::new();
let outcome = store
.replace("thing", "1", serde_json::json!({"id": "1"}))
.await
.unwrap();
assert_eq!(outcome, None);
assert_eq!(store.collection("thing"), Vec::<Value>::new());
}
#[tokio::test]
async fn a_conditional_write_refuses_a_resource_that_moved_underneath_it() {
let store = MemoryStore::new();
store.seed("thing", serde_json::json!({"id": "1", "name": "original"}));
let read = store.get("thing", "1").await.unwrap().unwrap();
let tag = entity_tag(&read);
let winner = store
.replace_if_unchanged(
"thing",
"1",
serde_json::json!({"id": "1", "name": "first"}),
&tag,
)
.await
.unwrap();
assert!(matches!(winner, Replaced::Updated(_)));
let loser = store
.replace_if_unchanged(
"thing",
"1",
serde_json::json!({"id": "1", "name": "second"}),
&tag,
)
.await
.unwrap();
assert_eq!(loser, Replaced::Stale, "the first write must not be lost");
assert_eq!(store.collection("thing")[0]["name"], "first");
assert_eq!(
store.delete_if_unchanged("thing", "1", &tag).await.unwrap(),
Replaced::Stale,
);
assert_eq!(store.collection("thing").len(), 1);
}
#[tokio::test]
async fn a_conditional_write_reports_an_absent_resource_apart_from_a_stale_one() {
let store = MemoryStore::new();
assert_eq!(
store
.replace_if_unchanged("thing", "1", serde_json::json!({"id": "1"}), "\"any\"")
.await
.unwrap(),
Replaced::Missing,
);
assert_eq!(
store
.delete_if_unchanged("thing", "1", "\"any\"")
.await
.unwrap(),
Replaced::Missing,
);
}
#[tokio::test]
async fn deleting_reports_whether_there_was_anything_to_delete() {
let store = MemoryStore::new();
store.seed("thing", serde_json::json!({"id": "1"}));
assert!(store.delete("thing", "1").await.unwrap());
assert!(!store.delete("thing", "1").await.unwrap());
}
}