Feat/restore revision (#1549)

* chore: write snapshot

* chore: add tests

* chore: sync close

* chore: restore from snapshot

* chore: delete invalid revisions after restored from snapshot

* chore: create default view if it fail to deserialize view's revisions when there is no snapshot

* chore: auto generate snapshot

Co-authored-by: nathan <nathan@appflowy.io>
This commit is contained in:
Nathan.fooo
2022-12-09 09:19:47 +08:00
committed by GitHub
parent a507fb8ec6
commit 8c225fe547
67 changed files with 1140 additions and 582 deletions

View File

@ -12,4 +12,4 @@ pin-project = "1.0"
futures-core = { version = "0.3" }
tokio = { version = "1.0", features = ["time", "rt"] }
rand = "0.8.5"
async-trait = "0.1.59"

View File

@ -8,7 +8,7 @@ use std::{
task::{Context, Poll},
};
pub fn to_future<T, O>(f: T) -> Fut<O>
pub fn to_fut<T, O>(f: T) -> Fut<O>
where
T: Future<Output = O> + Send + Sync + 'static,
{

View File

@ -2,3 +2,5 @@ pub mod future;
pub mod ref_map;
pub mod retry;
pub mod util;
pub use async_trait;

View File

@ -1,8 +1,10 @@
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;
#[async_trait]
pub trait RefCountValue {
fn did_remove(&self) {}
async fn did_remove(&self) {}
}
struct RefCountHandler<T> {
@ -30,7 +32,7 @@ impl<T> std::default::Default for RefCountHashMap<T> {
impl<T> RefCountHashMap<T>
where
T: Clone + Send + Sync + RefCountValue,
T: Clone + Send + Sync + RefCountValue + 'static,
{
pub fn new() -> Self {
Self::default()
@ -53,7 +55,7 @@ where
}
}
pub fn remove(&mut self, key: &str) {
pub async fn remove(&mut self, key: &str) {
let mut should_remove = false;
if let Some(value) = self.0.get_mut(key) {
if value.ref_count > 0 {
@ -64,17 +66,20 @@ where
if should_remove {
if let Some(handler) = self.0.remove(key) {
handler.inner.did_remove();
tokio::spawn(async move {
handler.inner.did_remove().await;
});
}
}
}
}
#[async_trait]
impl<T> RefCountValue for Arc<T>
where
T: RefCountValue,
T: RefCountValue + Sync + Send,
{
fn did_remove(&self) {
(**self).did_remove()
async fn did_remove(&self) {
(**self).did_remove().await
}
}