AppFlowy/frontend/rust-lib/flowy-block/src/manager.rs

227 lines
8.2 KiB
Rust
Raw Normal View History

2022-02-26 03:03:42 +00:00
use crate::{block_editor::ClientBlockEditor, errors::FlowyError, BlockCloudService};
2021-11-09 08:00:09 +00:00
use bytes::Bytes;
2021-12-10 03:05:23 +00:00
use dashmap::DashMap;
2022-01-01 06:23:58 +00:00
use flowy_collaboration::entities::{
2022-02-25 14:27:44 +00:00
document_info::{BlockDelta, BlockId},
2022-01-14 07:23:21 +00:00
revision::{md5, RepeatedRevision, Revision},
2022-01-15 03:20:28 +00:00
ws_data::ServerRevisionWSData,
2022-01-01 06:23:58 +00:00
};
2021-10-06 07:23:38 +00:00
use flowy_database::ConnectionPool;
2021-12-14 10:04:51 +00:00
use flowy_error::FlowyResult;
2022-02-25 14:27:44 +00:00
use flowy_sync::{RevisionCloudService, RevisionManager, RevisionPersistence, RevisionWebSocket};
2021-12-13 05:55:44 +00:00
use lib_infra::future::FutureResult;
2022-01-14 12:52:03 +00:00
use std::{convert::TryInto, sync::Arc};
2022-02-25 14:27:44 +00:00
pub trait BlockUser: Send + Sync {
2022-01-22 10:48:43 +00:00
fn user_dir(&self) -> Result<String, FlowyError>;
fn user_id(&self) -> Result<String, FlowyError>;
fn token(&self) -> Result<String, FlowyError>;
fn db_pool(&self) -> Result<Arc<ConnectionPool>, FlowyError>;
}
2022-02-25 14:27:44 +00:00
pub struct BlockManager {
cloud_service: Arc<dyn BlockCloudService>,
2022-02-18 15:04:55 +00:00
rev_web_socket: Arc<dyn RevisionWebSocket>,
2022-02-26 03:03:42 +00:00
block_editors: Arc<BlockEditors>,
block_user: Arc<dyn BlockUser>,
}
2022-02-25 14:27:44 +00:00
impl BlockManager {
2022-01-14 12:52:03 +00:00
pub fn new(
2022-02-25 14:27:44 +00:00
cloud_service: Arc<dyn BlockCloudService>,
2022-02-26 03:03:42 +00:00
block_user: Arc<dyn BlockUser>,
2022-02-18 15:04:55 +00:00
rev_web_socket: Arc<dyn RevisionWebSocket>,
2021-12-25 13:44:45 +00:00
) -> Self {
2022-03-04 14:09:16 +00:00
let block_editors = Arc::new(BlockEditors::new());
2021-12-17 16:23:26 +00:00
Self {
2022-01-10 15:45:59 +00:00
cloud_service,
2022-02-18 15:04:55 +00:00
rev_web_socket,
2022-03-04 14:09:16 +00:00
block_editors,
2022-02-26 03:03:42 +00:00
block_user,
2021-12-17 16:23:26 +00:00
}
}
2022-01-22 10:48:43 +00:00
pub fn init(&self) -> FlowyResult<()> {
2022-02-26 03:03:42 +00:00
listen_ws_state_changed(self.rev_web_socket.clone(), self.block_editors.clone());
2021-12-25 13:44:45 +00:00
2021-10-05 06:37:45 +00:00
Ok(())
}
2022-02-25 14:27:44 +00:00
#[tracing::instrument(level = "debug", skip(self, block_id), fields(block_id), err)]
pub async fn open_block<T: AsRef<str>>(&self, block_id: T) -> Result<Arc<ClientBlockEditor>, FlowyError> {
let block_id = block_id.as_ref();
tracing::Span::current().record("block_id", &block_id);
self.get_block_editor(block_id).await
}
2022-02-25 14:27:44 +00:00
#[tracing::instrument(level = "trace", skip(self, block_id), fields(block_id), err)]
pub fn close_block<T: AsRef<str>>(&self, block_id: T) -> Result<(), FlowyError> {
let block_id = block_id.as_ref();
tracing::Span::current().record("block_id", &block_id);
2022-02-26 03:03:42 +00:00
self.block_editors.remove(block_id);
Ok(())
}
2021-12-31 02:32:25 +00:00
#[tracing::instrument(level = "debug", skip(self, doc_id), fields(doc_id), err)]
pub fn delete<T: AsRef<str>>(&self, doc_id: T) -> Result<(), FlowyError> {
let doc_id = doc_id.as_ref();
tracing::Span::current().record("doc_id", &doc_id);
2022-02-26 03:03:42 +00:00
self.block_editors.remove(doc_id);
Ok(())
}
2021-09-23 05:15:35 +00:00
2022-02-25 14:27:44 +00:00
#[tracing::instrument(level = "debug", skip(self, delta), fields(doc_id = %delta.block_id), err)]
pub async fn receive_local_delta(&self, delta: BlockDelta) -> Result<BlockDelta, FlowyError> {
let editor = self.get_block_editor(&delta.block_id).await?;
2021-12-31 02:32:25 +00:00
let _ = editor.compose_local_delta(Bytes::from(delta.delta_json)).await?;
2022-02-25 14:27:44 +00:00
let document_json = editor.block_json().await?;
Ok(BlockDelta {
block_id: delta.block_id.clone(),
2021-12-31 02:32:25 +00:00
delta_json: document_json,
})
2021-09-23 05:15:35 +00:00
}
2021-12-31 02:32:25 +00:00
2022-02-28 08:00:43 +00:00
pub async fn create_block<T: AsRef<str>>(&self, doc_id: T, revisions: RepeatedRevision) -> FlowyResult<()> {
2022-01-01 06:23:58 +00:00
let doc_id = doc_id.as_ref().to_owned();
2022-02-26 03:03:42 +00:00
let db_pool = self.block_user.db_pool()?;
2022-02-28 08:00:43 +00:00
// Maybe we could save the block to disk without creating the RevisionManager
2022-01-01 06:23:58 +00:00
let rev_manager = self.make_rev_manager(&doc_id, db_pool)?;
2022-01-14 07:23:21 +00:00
let _ = rev_manager.reset_object(revisions).await?;
2022-01-01 06:23:58 +00:00
Ok(())
}
2022-02-18 15:04:55 +00:00
pub async fn receive_ws_data(&self, data: Bytes) {
2022-01-20 15:51:11 +00:00
let result: Result<ServerRevisionWSData, protobuf::ProtobufError> = data.try_into();
match result {
2022-02-26 03:03:42 +00:00
Ok(data) => match self.block_editors.get(&data.object_id) {
2022-01-21 13:41:24 +00:00
None => tracing::error!("Can't find any source handler for {:?}-{:?}", data.object_id, data.ty),
2022-02-25 14:27:44 +00:00
Some(block_editor) => match block_editor.receive_ws_data(data).await {
2022-01-24 09:35:58 +00:00
Ok(_) => {}
2022-01-20 15:51:11 +00:00
Err(e) => tracing::error!("{}", e),
},
},
Err(e) => {
tracing::error!("Document ws data parser failed: {:?}", e);
2022-01-24 09:35:58 +00:00
}
2022-01-14 12:52:03 +00:00
}
}
}
2022-02-25 14:27:44 +00:00
impl BlockManager {
async fn get_block_editor(&self, block_id: &str) -> FlowyResult<Arc<ClientBlockEditor>> {
2022-02-26 03:03:42 +00:00
match self.block_editors.get(block_id) {
2022-01-01 06:23:58 +00:00
None => {
2022-02-26 03:03:42 +00:00
let db_pool = self.block_user.db_pool()?;
2022-02-25 14:27:44 +00:00
self.make_block_editor(block_id, db_pool).await
2022-01-24 09:35:58 +00:00
}
2022-01-01 06:23:58 +00:00
Some(editor) => Ok(editor),
}
}
2022-02-25 14:27:44 +00:00
async fn make_block_editor(
2021-12-31 02:32:25 +00:00
&self,
2022-02-25 14:27:44 +00:00
block_id: &str,
2021-12-31 02:32:25 +00:00
pool: Arc<ConnectionPool>,
2022-02-25 14:27:44 +00:00
) -> Result<Arc<ClientBlockEditor>, FlowyError> {
2022-02-26 03:03:42 +00:00
let user = self.block_user.clone();
let token = self.block_user.token()?;
2022-02-25 14:27:44 +00:00
let rev_manager = self.make_rev_manager(block_id, pool.clone())?;
2022-02-26 03:03:42 +00:00
let cloud_service = Arc::new(BlockRevisionCloudService {
2021-12-17 16:23:26 +00:00
token,
2022-01-10 15:45:59 +00:00
server: self.cloud_service.clone(),
2021-12-17 16:23:26 +00:00
});
2022-02-18 15:04:55 +00:00
let doc_editor =
2022-02-25 14:27:44 +00:00
ClientBlockEditor::new(block_id, user, rev_manager, self.rev_web_socket.clone(), cloud_service).await?;
2022-02-26 03:03:42 +00:00
self.block_editors.insert(block_id, &doc_editor);
2021-12-16 13:31:36 +00:00
Ok(doc_editor)
2021-12-08 13:51:06 +00:00
}
2022-01-14 07:23:21 +00:00
fn make_rev_manager(&self, doc_id: &str, pool: Arc<ConnectionPool>) -> Result<RevisionManager, FlowyError> {
2022-02-26 03:03:42 +00:00
let user_id = self.block_user.user_id()?;
2022-02-25 14:27:44 +00:00
let rev_persistence = Arc::new(RevisionPersistence::new(&user_id, doc_id, pool));
Ok(RevisionManager::new(&user_id, doc_id, rev_persistence))
}
2021-09-21 07:07:07 +00:00
}
2021-09-26 08:39:57 +00:00
2022-02-26 03:03:42 +00:00
struct BlockRevisionCloudService {
token: String,
2022-02-25 14:27:44 +00:00
server: Arc<dyn BlockCloudService>,
}
2022-02-26 03:03:42 +00:00
impl RevisionCloudService for BlockRevisionCloudService {
2022-01-23 14:33:47 +00:00
#[tracing::instrument(level = "trace", skip(self))]
2022-02-24 12:36:24 +00:00
fn fetch_object(&self, user_id: &str, object_id: &str) -> FutureResult<Vec<Revision>, FlowyError> {
2022-02-25 14:27:44 +00:00
let params: BlockId = object_id.to_string().into();
let server = self.server.clone();
let token = self.token.clone();
2022-01-14 07:23:21 +00:00
let user_id = user_id.to_string();
2021-12-13 05:55:44 +00:00
FutureResult::new(async move {
2022-02-25 14:27:44 +00:00
match server.read_block(&token, params).await? {
2021-12-14 10:04:51 +00:00
None => Err(FlowyError::record_not_found().context("Remote doesn't have this document")),
2022-01-14 07:23:21 +00:00
Some(doc) => {
let delta_data = Bytes::from(doc.text.clone());
let doc_md5 = md5(&delta_data);
2022-03-02 13:12:21 +00:00
let revision = Revision::new(
&doc.block_id,
doc.base_rev_id,
doc.rev_id,
delta_data,
&user_id,
doc_md5,
);
2022-01-14 07:23:21 +00:00
Ok(vec![revision])
2022-01-24 09:35:58 +00:00
}
}
})
}
}
2022-02-26 03:03:42 +00:00
pub struct BlockEditors {
2022-02-25 14:27:44 +00:00
inner: DashMap<String, Arc<ClientBlockEditor>>,
2021-12-10 03:05:23 +00:00
}
2022-02-26 03:03:42 +00:00
impl BlockEditors {
2022-01-24 09:35:58 +00:00
fn new() -> Self {
Self { inner: DashMap::new() }
}
2021-12-10 03:05:23 +00:00
2022-02-25 14:27:44 +00:00
pub(crate) fn insert(&self, block_id: &str, doc: &Arc<ClientBlockEditor>) {
if self.inner.contains_key(block_id) {
log::warn!("Doc:{} already exists in cache", block_id);
2021-12-10 03:05:23 +00:00
}
2022-02-25 14:27:44 +00:00
self.inner.insert(block_id.to_string(), doc.clone());
2021-12-10 03:05:23 +00:00
}
2022-02-25 14:27:44 +00:00
pub(crate) fn contains(&self, block_id: &str) -> bool {
self.inner.get(block_id).is_some()
2022-01-24 09:35:58 +00:00
}
2021-12-10 03:05:23 +00:00
2022-02-25 14:27:44 +00:00
pub(crate) fn get(&self, block_id: &str) -> Option<Arc<ClientBlockEditor>> {
if !self.contains(block_id) {
2022-01-01 06:23:58 +00:00
return None;
2021-12-10 03:05:23 +00:00
}
2022-02-25 14:27:44 +00:00
let opened_doc = self.inner.get(block_id).unwrap();
2022-01-01 06:23:58 +00:00
Some(opened_doc.clone())
2021-12-10 03:05:23 +00:00
}
2022-02-25 14:27:44 +00:00
pub(crate) fn remove(&self, block_id: &str) {
if let Some(editor) = self.get(block_id) {
2022-01-01 06:23:58 +00:00
editor.stop()
2021-09-26 08:39:57 +00:00
}
2022-02-25 14:27:44 +00:00
self.inner.remove(block_id);
2021-12-10 03:05:23 +00:00
}
2021-09-26 08:39:57 +00:00
}
2021-12-10 03:05:23 +00:00
2022-02-25 14:27:44 +00:00
#[tracing::instrument(level = "trace", skip(web_socket, handlers))]
2022-02-26 03:03:42 +00:00
fn listen_ws_state_changed(web_socket: Arc<dyn RevisionWebSocket>, handlers: Arc<BlockEditors>) {
2021-12-25 13:44:45 +00:00
tokio::spawn(async move {
2022-01-24 08:27:40 +00:00
let mut notify = web_socket.subscribe_state_changed().await;
while let Ok(state) = notify.recv().await {
2022-02-25 14:27:44 +00:00
handlers.inner.iter().for_each(|handler| {
handler.receive_ws_state(&state);
})
2021-12-25 13:44:45 +00:00
}
});
}