mirror of
https://github.com/Start9Labs/patch-db.git
synced 2026-03-26 10:21:53 +00:00
export diffpatch
This commit is contained in:
committed by
Aiden McClelland
parent
9ff57e33d3
commit
d3c1c3f9d9
@@ -23,7 +23,7 @@ pub use locker::{LockType, Locker};
|
||||
pub use model::{
|
||||
BoxModel, HasModel, Map, MapModel, Model, ModelData, ModelDataMut, OptionModel, VecModel,
|
||||
};
|
||||
pub use patch::Revision;
|
||||
pub use patch::{Revision, DiffPatch};
|
||||
pub use patch_db_macro::HasModel;
|
||||
pub use store::{PatchDb, Store};
|
||||
pub use transaction::Transaction;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use futures::{future::BoxFuture, FutureExt};
|
||||
use json_ptr::{JsonPointer, SegList};
|
||||
use qutex_2::{QrwLock, ReadGuard, WriteGuard};
|
||||
use tokio::sync::Mutex;
|
||||
@@ -67,6 +68,15 @@ impl Locker {
|
||||
pub fn new() -> Self {
|
||||
Locker(QrwLock::new(HashMap::new()))
|
||||
}
|
||||
fn lock_root_read<'a>(guard: &'a ReadGuard<HashMap<String, Locker>>) -> BoxFuture<'a, ()> {
|
||||
async move {
|
||||
for (_, v) in &**guard {
|
||||
let g = v.0.clone().read().await.unwrap();
|
||||
Self::lock_root_read(&g).await;
|
||||
}
|
||||
}
|
||||
.boxed()
|
||||
}
|
||||
pub async fn lock_read<S: AsRef<str>, V: SegList>(
|
||||
&self,
|
||||
ptr: &JsonPointer<S, V>,
|
||||
@@ -83,7 +93,9 @@ impl Locker {
|
||||
};
|
||||
lock = Some(new_lock);
|
||||
}
|
||||
lock.unwrap()
|
||||
let res = lock.unwrap();
|
||||
Self::lock_root_read(&res);
|
||||
res
|
||||
}
|
||||
pub(crate) async fn add_read_lock<S: AsRef<str> + Clone, V: SegList + Clone>(
|
||||
&self,
|
||||
@@ -105,6 +117,15 @@ impl Locker {
|
||||
LockerGuard::Read(self.lock_read(ptr).await.into()),
|
||||
));
|
||||
}
|
||||
fn lock_root_write<'a>(guard: &'a WriteGuard<HashMap<String, Locker>>) -> BoxFuture<'a, ()> {
|
||||
async move {
|
||||
for (_, v) in &**guard {
|
||||
let g = v.0.clone().write().await.unwrap();
|
||||
Self::lock_root_write(&g).await;
|
||||
}
|
||||
}
|
||||
.boxed()
|
||||
}
|
||||
pub async fn lock_write<S: AsRef<str>, V: SegList>(
|
||||
&self,
|
||||
ptr: &JsonPointer<S, V>,
|
||||
@@ -119,7 +140,9 @@ impl Locker {
|
||||
};
|
||||
lock = new_lock;
|
||||
}
|
||||
lock
|
||||
let res = lock;
|
||||
Self::lock_root_write(&res);
|
||||
res
|
||||
}
|
||||
pub(crate) async fn add_write_lock<S: AsRef<str> + Clone, V: SegList + Clone>(
|
||||
&self,
|
||||
|
||||
@@ -11,6 +11,7 @@ use serde_json::Value;
|
||||
use crate::Error;
|
||||
use crate::{locker::LockType, DbHandle};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ModelData<T: Serialize + for<'de> Deserialize<'de>>(T);
|
||||
impl<T: Serialize + for<'de> Deserialize<'de>> Deref for ModelData<T> {
|
||||
type Target = T;
|
||||
@@ -18,7 +19,13 @@ impl<T: Serialize + for<'de> Deserialize<'de>> Deref for ModelData<T> {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
impl<T: Serialize + for<'de> Deserialize<'de>> ModelData<T> {
|
||||
pub fn to_owned(self) -> T {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ModelDataMut<T: Serialize + for<'de> Deserialize<'de>> {
|
||||
original: Value,
|
||||
current: T,
|
||||
|
||||
@@ -35,6 +35,12 @@ impl Transaction<&mut PatchDbHandle> {
|
||||
drop(store);
|
||||
Ok(rev)
|
||||
}
|
||||
pub async fn abort(mut self) -> Result<DiffPatch, Error> {
|
||||
let store_lock = self.parent.store();
|
||||
let _store = store_lock.read().await;
|
||||
self.rebase()?;
|
||||
Ok(self.updates)
|
||||
}
|
||||
}
|
||||
impl<Parent: DbHandle + Send + Sync> Transaction<Parent> {
|
||||
pub async fn save(mut self) -> Result<(), Error> {
|
||||
|
||||
Reference in New Issue
Block a user