AppFlowy/frontend/rust-lib/flowy-net/src/local_server/persistence.rs

197 lines
6.7 KiB
Rust
Raw Normal View History

2022-01-11 14:23:19 +00:00
use flowy_collaboration::{
2022-02-25 14:27:44 +00:00
entities::{document_info::BlockInfo, folder_info::FolderInfo},
2022-01-11 14:23:19 +00:00
errors::CollaborateError,
protobuf::{RepeatedRevision as RepeatedRevisionPB, Revision as RevisionPB},
2022-01-12 09:08:50 +00:00
server_document::*,
2022-01-21 13:41:24 +00:00
server_folder::FolderCloudPersistence,
util::{make_document_info_from_revisions_pb, make_folder_from_revisions_pb},
2022-01-11 14:23:19 +00:00
};
use lib_infra::future::BoxResultFuture;
use std::{
fmt::{Debug, Formatter},
sync::Arc,
};
2022-01-15 15:58:36 +00:00
// For the moment, we use memory to cache the data, it will be implemented with
// other storage. Like the Firestore,Dropbox.etc.
2022-01-14 12:52:03 +00:00
pub trait RevisionCloudStorage: Send + Sync {
2022-01-13 02:53:30 +00:00
fn set_revisions(&self, repeated_revision: RepeatedRevisionPB) -> BoxResultFuture<(), CollaborateError>;
fn get_revisions(
&self,
2022-01-14 12:52:03 +00:00
object_id: &str,
2022-01-13 02:53:30 +00:00
rev_ids: Option<Vec<i64>>,
) -> BoxResultFuture<RepeatedRevisionPB, CollaborateError>;
2022-01-14 12:52:03 +00:00
fn reset_object(
2022-01-13 02:53:30 +00:00
&self,
2022-01-14 12:52:03 +00:00
object_id: &str,
2022-01-13 02:53:30 +00:00
repeated_revision: RepeatedRevisionPB,
) -> BoxResultFuture<(), CollaborateError>;
}
2022-01-15 03:20:28 +00:00
pub(crate) struct LocalDocumentCloudPersistence {
2022-01-14 12:52:03 +00:00
storage: Arc<dyn RevisionCloudStorage>,
2022-01-11 14:23:19 +00:00
}
2022-01-15 03:20:28 +00:00
impl Debug for LocalDocumentCloudPersistence {
2022-01-24 09:35:58 +00:00
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str("LocalRevisionCloudPersistence")
}
2022-01-11 14:23:19 +00:00
}
2022-01-15 03:20:28 +00:00
impl std::default::Default for LocalDocumentCloudPersistence {
2022-01-11 14:23:19 +00:00
fn default() -> Self {
2022-01-15 03:20:28 +00:00
LocalDocumentCloudPersistence {
2022-01-11 14:23:19 +00:00
storage: Arc::new(MemoryDocumentCloudStorage::default()),
}
}
}
2022-01-21 13:41:24 +00:00
impl FolderCloudPersistence for LocalDocumentCloudPersistence {
fn read_folder(&self, _user_id: &str, folder_id: &str) -> BoxResultFuture<FolderInfo, CollaborateError> {
let storage = self.storage.clone();
let folder_id = folder_id.to_owned();
Box::pin(async move {
let repeated_revision = storage.get_revisions(&folder_id, None).await?;
match make_folder_from_revisions_pb(&folder_id, repeated_revision)? {
Some(folder_info) => Ok(folder_info),
None => Err(CollaborateError::record_not_found()),
}
})
}
fn create_folder(
&self,
_user_id: &str,
folder_id: &str,
repeated_revision: RepeatedRevisionPB,
) -> BoxResultFuture<Option<FolderInfo>, CollaborateError> {
let folder_id = folder_id.to_owned();
let storage = self.storage.clone();
Box::pin(async move {
let _ = storage.set_revisions(repeated_revision.clone()).await?;
make_folder_from_revisions_pb(&folder_id, repeated_revision)
})
}
fn save_folder_revisions(&self, repeated_revision: RepeatedRevisionPB) -> BoxResultFuture<(), CollaborateError> {
let storage = self.storage.clone();
Box::pin(async move {
let _ = storage.set_revisions(repeated_revision).await?;
Ok(())
})
}
fn read_folder_revisions(
&self,
folder_id: &str,
rev_ids: Option<Vec<i64>>,
) -> BoxResultFuture<Vec<RevisionPB>, CollaborateError> {
let folder_id = folder_id.to_owned();
let storage = self.storage.clone();
Box::pin(async move {
let mut repeated_revision = storage.get_revisions(&folder_id, rev_ids).await?;
let revisions: Vec<RevisionPB> = repeated_revision.take_items().into();
Ok(revisions)
})
}
fn reset_folder(
&self,
folder_id: &str,
repeated_revision: RepeatedRevisionPB,
) -> BoxResultFuture<(), CollaborateError> {
let storage = self.storage.clone();
let folder_id = folder_id.to_owned();
Box::pin(async move {
let _ = storage.reset_object(&folder_id, repeated_revision).await?;
Ok(())
})
}
}
2022-01-15 03:20:28 +00:00
impl DocumentCloudPersistence for LocalDocumentCloudPersistence {
2022-02-25 14:27:44 +00:00
fn read_document(&self, doc_id: &str) -> BoxResultFuture<BlockInfo, CollaborateError> {
2022-01-11 14:23:19 +00:00
let storage = self.storage.clone();
let doc_id = doc_id.to_owned();
Box::pin(async move {
let repeated_revision = storage.get_revisions(&doc_id, None).await?;
2022-01-21 13:41:24 +00:00
match make_document_info_from_revisions_pb(&doc_id, repeated_revision)? {
Some(document_info) => Ok(document_info),
None => Err(CollaborateError::record_not_found()),
2022-01-11 14:23:19 +00:00
}
})
}
fn create_document(
&self,
doc_id: &str,
repeated_revision: RepeatedRevisionPB,
2022-02-25 14:27:44 +00:00
) -> BoxResultFuture<Option<BlockInfo>, CollaborateError> {
2022-01-11 14:23:19 +00:00
let doc_id = doc_id.to_owned();
let storage = self.storage.clone();
Box::pin(async move {
let _ = storage.set_revisions(repeated_revision.clone()).await?;
2022-01-21 13:41:24 +00:00
make_document_info_from_revisions_pb(&doc_id, repeated_revision)
2022-01-11 14:23:19 +00:00
})
}
2022-01-21 13:41:24 +00:00
fn read_document_revisions(
2022-01-11 14:23:19 +00:00
&self,
doc_id: &str,
rev_ids: Option<Vec<i64>>,
) -> BoxResultFuture<Vec<RevisionPB>, CollaborateError> {
let doc_id = doc_id.to_owned();
let storage = self.storage.clone();
Box::pin(async move {
let mut repeated_revision = storage.get_revisions(&doc_id, rev_ids).await?;
let revisions: Vec<RevisionPB> = repeated_revision.take_items().into();
Ok(revisions)
})
}
2022-01-21 13:41:24 +00:00
fn save_document_revisions(&self, repeated_revision: RepeatedRevisionPB) -> BoxResultFuture<(), CollaborateError> {
2022-01-11 14:23:19 +00:00
let storage = self.storage.clone();
Box::pin(async move {
let _ = storage.set_revisions(repeated_revision).await?;
Ok(())
})
}
fn reset_document(&self, doc_id: &str, revisions: RepeatedRevisionPB) -> BoxResultFuture<(), CollaborateError> {
let storage = self.storage.clone();
let doc_id = doc_id.to_owned();
Box::pin(async move {
2022-01-14 12:52:03 +00:00
let _ = storage.reset_object(&doc_id, revisions).await?;
2022-01-11 14:23:19 +00:00
Ok(())
})
}
}
2022-01-24 09:35:58 +00:00
#[derive(Default)]
2022-01-11 14:23:19 +00:00
struct MemoryDocumentCloudStorage {}
2022-01-14 12:52:03 +00:00
impl RevisionCloudStorage for MemoryDocumentCloudStorage {
2022-01-11 14:23:19 +00:00
fn set_revisions(&self, _repeated_revision: RepeatedRevisionPB) -> BoxResultFuture<(), CollaborateError> {
Box::pin(async move { Ok(()) })
}
fn get_revisions(
&self,
_doc_id: &str,
_rev_ids: Option<Vec<i64>>,
) -> BoxResultFuture<RepeatedRevisionPB, CollaborateError> {
Box::pin(async move {
let repeated_revisions = RepeatedRevisionPB::new();
Ok(repeated_revisions)
})
}
2022-01-14 12:52:03 +00:00
fn reset_object(
2022-01-11 14:23:19 +00:00
&self,
_doc_id: &str,
_repeated_revision: RepeatedRevisionPB,
) -> BoxResultFuture<(), CollaborateError> {
Box::pin(async move { Ok(()) })
}
}