From 6af2221add56f0a557b37a268ef9fb2299a05255 Mon Sep 17 00:00:00 2001 From: Aiden McClelland <3732071+dr-bonez@users.noreply.github.com> Date: Mon, 16 Oct 2023 16:02:02 -0600 Subject: [PATCH] remove revision cache (#62) --- patch-db-util/src/main.rs | 4 +-- patch-db/src/store.rs | 69 ++++++++------------------------------- 2 files changed, 14 insertions(+), 59 deletions(-) diff --git a/patch-db-util/src/main.rs b/patch-db-util/src/main.rs index b4b26bb..301e36e 100644 --- a/patch-db-util/src/main.rs +++ b/patch-db-util/src/main.rs @@ -1,5 +1,3 @@ -use std::path::Path; - use patch_db::json_ptr::{JsonPointer, PtrSegment}; use serde_json::Value; @@ -25,7 +23,7 @@ async fn main() { Some(("dump", matches)) => { let path = matches.value_of("PATH").unwrap(); let db = patch_db::PatchDb::open(path).await.unwrap(); - let dump = db.dump().await.unwrap(); + let dump = db.dump().await; serde_json::to_writer_pretty(&mut std::io::stdout(), &dump.value).unwrap(); println!(); } diff --git a/patch-db/src/store.rs b/patch-db/src/store.rs index 211d8cb..61b5283 100644 --- a/patch-db/src/store.rs +++ b/patch-db/src/store.rs @@ -1,4 +1,4 @@ -use std::collections::{BTreeSet, HashMap, VecDeque}; +use std::collections::{BTreeSet, HashMap}; use std::fs::OpenOptions; use std::io::{SeekFrom, Write}; use std::panic::UnwindSafe; @@ -24,38 +24,6 @@ lazy_static! { static ref OPEN_STORES: Mutex>>> = Mutex::new(HashMap::new()); } -pub struct RevisionCache { - cache: VecDeque>, - capacity: usize, -} -impl RevisionCache { - pub fn with_capacity(capacity: usize) -> Self { - RevisionCache { - cache: VecDeque::with_capacity(capacity), - capacity, - } - } - pub fn push(&mut self, revision: Arc) { - while self.capacity > 0 && self.cache.len() >= self.capacity { - self.cache.pop_front(); - } - self.cache.push_back(revision); - } - pub fn since(&self, id: u64) -> Option>> { - let start = self.cache.get(0).map(|rev| rev.id)?; - if id < start - 1 { - return None; - } - Some( - self.cache - .iter() - .skip((id - start + 1) as usize) - .cloned() - .collect(), - ) - } -} - pub struct Store { path: PathBuf, file: FdLock, @@ -63,7 +31,6 @@ pub struct Store { _lock: OwnedMutexGuard<()>, persistent: Value, revision: u64, - revision_cache: RevisionCache, broadcast: Broadcast>, } impl Store { @@ -132,7 +99,6 @@ impl Store { _lock, persistent, revision, - revision_cache: RevisionCache::with_capacity(64), broadcast: Broadcast::new(), }) }) @@ -140,12 +106,6 @@ impl Store { res.compress().await?; Ok(res) } - pub(crate) fn get_revisions_since(&self, id: u64) -> Option>> { - if id >= self.revision { - return Some(Vec::new()); - } - self.revision_cache.since(id) - } pub async fn close(mut self) -> Result<(), Error> { use tokio::io::AsyncWriteExt; @@ -175,11 +135,14 @@ impl Store { ) -> Result { Ok(imbl_value::from_value(self.get_value(ptr))?) } - pub(crate) fn dump(&self) -> Result { - Ok(Dump { + pub(crate) fn dump(&self) -> Dump { + Dump { id: self.revision, value: self.persistent.clone(), - }) + } + } + pub(crate) fn sequence(&self) -> u64 { + self.revision } pub(crate) fn subscribe(&mut self) -> Subscriber { self.broadcast.subscribe() @@ -285,7 +248,6 @@ impl Store { let id = self.revision; let res = Arc::new(Revision { id, patch }); - self.revision_cache.push(res.clone()); self.broadcast.send(&res); Ok(Some(res)) @@ -302,21 +264,16 @@ impl PatchDb { store: Arc::new(RwLock::new(Store::open(path).await?)), }) } - pub async fn sync(&self, sequence: u64) -> Result>, Dump>, Error> { - let store = self.store.read().await; - if let Some(revs) = store.get_revisions_since(sequence) { - Ok(Ok(revs)) - } else { - Ok(Err(store.dump()?)) - } - } - pub async fn dump(&self) -> Result { + pub async fn dump(&self) -> Dump { self.store.read().await.dump() } - pub async fn dump_and_sub(&self) -> Result<(Dump, Subscriber), Error> { + pub async fn sequence(&self) -> u64 { + self.store.read().await.sequence() + } + pub async fn dump_and_sub(&self) -> (Dump, Subscriber) { let mut store = self.store.write().await; let sub = store.broadcast.subscribe(); - Ok((store.dump()?, sub)) + (store.dump(), sub) } pub async fn subscribe(&self) -> Subscriber { self.store.write().await.subscribe()