Added a basic commit function.

This commit is contained in:
2023-06-29 00:17:49 -04:00
parent 933d48a47c
commit 05d445c58b
2 changed files with 75 additions and 19 deletions

View File

@ -16,18 +16,20 @@ use store::Store;
const ENTRY: &str = "EntryPoint";
#[derive(Debug)]
pub struct CacheGet {
id: String,
pub struct ToCacheMsg<D> {
data: D,
result: Sender<FromCache>,
}
#[derive(Debug)]
pub enum ToCache {
Get(CacheGet),
Get(ToCacheMsg<String>),
Commit(ToCacheMsg<Store>),
}
#[derive(Debug)]
pub enum FromCache {
Ok,
Str(Store),
Error(MTTError),
}
@ -73,14 +75,29 @@ impl MoreThanText {
async fn session(&self) -> Result<Store, MTTError> {
let (s, r) = unbounded();
let msg = CacheGet {
id: ENTRY.to_string(),
let msg = ToCacheMsg {
data: ENTRY.to_string(),
result: s,
};
self.to_cache.send(ToCache::Get(msg)).await.unwrap();
match r.recv().await.unwrap() {
FromCache::Str(store) => Ok(store),
FromCache::Error(err) => Err(err),
_ => unreachable!(),
}
}
async fn commit(&self, store: Store) -> Result<(), MTTError> {
let (s, r) = unbounded();
let msg = ToCacheMsg {
data: store,
result: s,
};
self.to_cache.send(ToCache::Commit(msg)).await.unwrap();
match r.recv().await.unwrap() {
FromCache::Ok => Ok(()),
FromCache::Error(err) => Err(err),
_ => unreachable!(),
}
}
}
@ -100,6 +117,18 @@ mod mtt {
let expected: Vec<String> = Vec::new();
assert_eq!(store.list(), expected);
}
#[async_std::test]
async fn commit_db() {
let dir = tempdir().unwrap();
let db = "fred";
let mtt = start_db(dir.path()).await.unwrap();
let mut store = mtt.session().await.unwrap();
store.add(db).unwrap();
mtt.commit(store).await.unwrap();
let store2 = mtt.session().await.unwrap();
assert_eq!(store2.list(), [db]);
}
}
pub async fn start_db<P>(dir: P) -> Result<MoreThanText, MTTError>
@ -109,7 +138,7 @@ where
let path = dir.into();
let (s, r) = unbounded();
spawn(async move {
let cache = Cache::new(path).await;
let mut cache = Cache::new(path).await;
cache.listen(r).await;
});
Ok(MoreThanText::new(s))