From 9faa96d307f128296912a1e6cb88cd0da5a5a6c0 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Sat, 15 Apr 2023 09:47:48 -0400 Subject: [PATCH] Cleaned up the cache interface. --- src/morethantext/mod.rs | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/morethantext/mod.rs b/src/morethantext/mod.rs index 9666256..bbb1e88 100644 --- a/src/morethantext/mod.rs +++ b/src/morethantext/mod.rs @@ -270,30 +270,37 @@ impl Cache { Ok(Self { channel: channel }) } - async fn query(&self, qry: CacheQuery) { - for id in qry.ids { + async fn query(&self, qry: &Vec) -> Result, MTTError> { + let mut output = HashMap::new(); + for id in qry { if id == ENTRY { - let mut holder = HashMap::new(); - holder.insert(ENTRY.to_string(), DataType::new("store")); - qry.reply.send(FromCache::Data(holder)).await.unwrap(); + output.insert(ENTRY.to_string(), DataType::new("store")); } else { - qry.reply - .send(FromCache::Error(MTTError::new("fred"))) - .await - .unwrap(); + return Err(MTTError::new("fred")) } } + Ok(output) } - async fn commit(&self, commit: CacheCommit) { - commit.reply.send(FromCache::Ok).await.unwrap(); + async fn commit(&self) -> Result<(), MTTError> { + Ok(()) } async fn start(&self) { loop { match self.channel.recv().await.unwrap() { - ToCache::Query(qry) => self.query(qry).await, - ToCache::Commit(commit) => self.commit(commit).await, + ToCache::Query(qry) => { + match self.query(&qry.ids).await { + Ok(data) => qry.reply.send(FromCache::Data(data)).await.unwrap(), + Err(error) => qry.reply.send(FromCache::Error(error)).await.unwrap(), + } + }, + ToCache::Commit(commit) => { + match self.commit().await { + Ok(_) => commit.reply.send(FromCache::Ok).await.unwrap(), + Err(error) => commit.reply.send(FromCache::Error(error)).await.unwrap(), + } + }, } } }