From bfb1376be63ebbba609168ca6c37b3adfd70b985 Mon Sep 17 00:00:00 2001 From: Aiden McClelland Date: Thu, 29 Apr 2021 10:59:10 -0600 Subject: [PATCH] expire_id --- patch-db/src/handle.rs | 6 +++--- patch-db/src/patch.rs | 1 + patch-db/src/store.rs | 21 ++++++++++++++++----- patch-db/src/test.rs | 7 ++++--- patch-db/src/transaction.rs | 4 ++-- 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/patch-db/src/handle.rs b/patch-db/src/handle.rs index 7ba74d0..421abdd 100644 --- a/patch-db/src/handle.rs +++ b/patch-db/src/handle.rs @@ -194,11 +194,11 @@ impl DbHandle for PatchDbHandle { ptr: &JsonPointer, value: &Value, ) -> Result<(), Error> { - self.db.put(ptr, value).await?; + self.db.put(ptr, value, None).await?; Ok(()) } async fn apply(&mut self, patch: DiffPatch) -> Result<(), Error> { - self.db.apply(patch, None).await?; + self.db.apply(patch, None, None).await?; Ok(()) } async fn lock + Clone + Send + Sync, V: SegList + Clone + Send + Sync>( @@ -241,7 +241,7 @@ impl DbHandle for PatchDbHandle { ptr: &JsonPointer, value: &T, ) -> Result<(), Error> { - self.db.put(ptr, value).await?; + self.db.put(ptr, value, None).await?; Ok(()) } } diff --git a/patch-db/src/patch.rs b/patch-db/src/patch.rs index b9d46a0..540fb59 100644 --- a/patch-db/src/patch.rs +++ b/patch-db/src/patch.rs @@ -9,6 +9,7 @@ use serde_json::Value; pub struct Revision { pub id: u64, pub patch: DiffPatch, + pub expire_id: Option, } #[derive(Debug, Clone, Deserialize, Serialize)] diff --git a/patch-db/src/store.rs b/patch-db/src/store.rs index 37e05c2..6211fbe 100644 --- a/patch-db/src/store.rs +++ b/patch-db/src/store.rs @@ -143,15 +143,20 @@ impl Store { &mut self, ptr: &JsonPointer, value: &T, + expire_id: Option, ) -> Result, Error> { let mut patch = diff( ptr.get(self.get_data()?).unwrap_or(&Value::Null), &serde_json::to_value(value)?, ); patch.prepend(ptr); - self.apply(patch).await + self.apply(patch, expire_id).await } - pub(crate) async fn apply(&mut self, patch: DiffPatch) -> Result, Error> { + pub(crate) async fn apply( + &mut self, + patch: DiffPatch, + expire_id: Option, + ) -> Result, Error> { use tokio::io::AsyncWriteExt; self.check_cache_corrupted()?; @@ -173,7 +178,11 @@ impl Store { let id = self.revision; self.revision += 1; - let res = Arc::new(Revision { id, patch }); + let res = Arc::new(Revision { + id, + patch, + expire_id, + }); Ok(res) } @@ -214,15 +223,17 @@ impl PatchDb { &self, ptr: &JsonPointer, value: &T, + expire_id: Option, ) -> Result, Error> { let mut store = self.store.write().await; - let rev = store.put(ptr, value).await?; + let rev = store.put(ptr, value, expire_id).await?; self.subscriber.send(rev.clone()).unwrap_or_default(); Ok(rev) } pub async fn apply( &self, patch: DiffPatch, + expire_id: Option, store_write_lock: Option>, ) -> Result, Error> { let mut store = if let Some(store_write_lock) = store_write_lock { @@ -230,7 +241,7 @@ impl PatchDb { } else { self.store.write().await }; - let rev = store.apply(patch).await?; + let rev = store.apply(patch, expire_id).await?; self.subscriber.send(rev.clone()).unwrap_or_default(); // ignore errors Ok(rev) } diff --git a/patch-db/src/test.rs b/patch-db/src/test.rs index 19abb41..434f69d 100644 --- a/patch-db/src/test.rs +++ b/patch-db/src/test.rs @@ -23,6 +23,7 @@ async fn init_db(db_name: String) -> PatchDb { c: NewType(None), }, }, + None, ) .await .unwrap(); @@ -34,7 +35,7 @@ async fn cleanup_db(db_name: &str) { } async fn put_string_into_root(db: PatchDb, s: String) -> Arc { - db.put(&JsonPointer::<&'static str>::default(), &s) + db.put(&JsonPointer::<&'static str>::default(), &s, None) .await .unwrap() } @@ -45,7 +46,7 @@ async fn basic() { let ptr: JsonPointer = "/b/b".parse().unwrap(); let mut get_res: Value = db.get(&ptr).await.unwrap(); assert_eq!(get_res, 1); - db.put(&ptr, "hello").await.unwrap(); + db.put(&ptr, "hello", None).await.unwrap(); get_res = db.get(&ptr).await.unwrap(); assert_eq!(get_res, "hello"); cleanup_db("test.db").await; @@ -59,7 +60,7 @@ async fn transaction() { let ptr: JsonPointer = "/b/b".parse().unwrap(); tx.put(&ptr, &(2 as usize)).await.unwrap(); tx.put(&ptr, &(1 as usize)).await.unwrap(); - let _res = tx.commit().await.unwrap(); + let _res = tx.commit(None).await.unwrap(); println!("res = {:?}", _res); cleanup_db("test.db").await; } diff --git a/patch-db/src/transaction.rs b/patch-db/src/transaction.rs index 3e13f5c..7be7df2 100644 --- a/patch-db/src/transaction.rs +++ b/patch-db/src/transaction.rs @@ -26,11 +26,11 @@ pub struct Transaction { pub(crate) sub: Receiver>, } impl Transaction<&mut PatchDbHandle> { - pub async fn commit(mut self) -> Result, Error> { + pub async fn commit(mut self, expire_id: Option) -> Result, Error> { let store_lock = self.parent.store(); let store = store_lock.read().await; self.rebase()?; - let rev = self.parent.db.apply(self.updates, None).await?; + let rev = self.parent.db.apply(self.updates, expire_id, None).await?; drop(store); Ok(rev) }