mirror of
https://github.com/Start9Labs/patch-db.git
synced 2026-03-26 02:11:54 +00:00
expire_id
This commit is contained in:
committed by
Aiden McClelland
parent
e898d4d7ee
commit
bfb1376be6
@@ -194,11 +194,11 @@ impl DbHandle for PatchDbHandle {
|
||||
ptr: &JsonPointer<S, V>,
|
||||
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<S: AsRef<str> + Clone + Send + Sync, V: SegList + Clone + Send + Sync>(
|
||||
@@ -241,7 +241,7 @@ impl DbHandle for PatchDbHandle {
|
||||
ptr: &JsonPointer<S, V>,
|
||||
value: &T,
|
||||
) -> Result<(), Error> {
|
||||
self.db.put(ptr, value).await?;
|
||||
self.db.put(ptr, value, None).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ use serde_json::Value;
|
||||
pub struct Revision {
|
||||
pub id: u64,
|
||||
pub patch: DiffPatch,
|
||||
pub expire_id: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
|
||||
@@ -143,15 +143,20 @@ impl Store {
|
||||
&mut self,
|
||||
ptr: &JsonPointer<S, V>,
|
||||
value: &T,
|
||||
expire_id: Option<String>,
|
||||
) -> Result<Arc<Revision>, 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<Arc<Revision>, Error> {
|
||||
pub(crate) async fn apply(
|
||||
&mut self,
|
||||
patch: DiffPatch,
|
||||
expire_id: Option<String>,
|
||||
) -> Result<Arc<Revision>, 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<S, V>,
|
||||
value: &T,
|
||||
expire_id: Option<String>,
|
||||
) -> Result<Arc<Revision>, 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<String>,
|
||||
store_write_lock: Option<RwLockWriteGuard<'_, Store>>,
|
||||
) -> Result<Arc<Revision>, 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)
|
||||
}
|
||||
|
||||
@@ -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<Revision> {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -26,11 +26,11 @@ pub struct Transaction<Parent: DbHandle> {
|
||||
pub(crate) sub: Receiver<Arc<Revision>>,
|
||||
}
|
||||
impl Transaction<&mut PatchDbHandle> {
|
||||
pub async fn commit(mut self) -> Result<Arc<Revision>, Error> {
|
||||
pub async fn commit(mut self, expire_id: Option<String>) -> Result<Arc<Revision>, 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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user