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

263 lines
9.2 KiB
Rust
Raw Normal View History

2022-10-13 15:29:37 +00:00
use crate::editor::DocumentRevisionCompactor;
2022-09-14 13:04:12 +00:00
use crate::entities::EditParams;
2022-10-13 15:29:37 +00:00
use crate::{editor::DocumentEditor, errors::FlowyError, DocumentCloudService};
2021-11-09 08:00:09 +00:00
use bytes::Bytes;
2021-12-10 03:05:23 +00:00
use dashmap::DashMap;
2022-03-19 08:52:28 +00:00
use flowy_database::ConnectionPool;
use flowy_error::FlowyResult;
2022-10-13 15:29:37 +00:00
use flowy_revision::disk::SQLiteDocumentRevisionPersistence;
use flowy_revision::{
2022-07-20 06:07:54 +00:00
RevisionCloudService, RevisionManager, RevisionPersistence, RevisionWebSocket, SQLiteRevisionSnapshotPersistence,
};
2022-03-19 08:52:28 +00:00
use flowy_sync::entities::{
2022-10-13 15:29:37 +00:00
document::{DocumentIdPB, DocumentOperationsPB},
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-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-10-13 15:29:37 +00:00
pub trait DocumentUser: 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-10-13 15:29:37 +00:00
pub struct DocumentManager {
cloud_service: Arc<dyn DocumentCloudService>,
2022-02-18 15:04:55 +00:00
rev_web_socket: Arc<dyn RevisionWebSocket>,
2022-10-13 15:29:37 +00:00
editor_map: Arc<DocumentEditorMap>,
user: Arc<dyn DocumentUser>,
}
2022-10-13 15:29:37 +00:00
impl DocumentManager {
2022-01-14 12:52:03 +00:00
pub fn new(
2022-10-13 15:29:37 +00:00
cloud_service: Arc<dyn DocumentCloudService>,
document_user: Arc<dyn DocumentUser>,
2022-02-18 15:04:55 +00:00
rev_web_socket: Arc<dyn RevisionWebSocket>,
2021-12-25 13:44:45 +00:00
) -> Self {
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-10-13 15:29:37 +00:00
editor_map: Arc::new(DocumentEditorMap::new()),
user: document_user,
2021-12-17 16:23:26 +00:00
}
}
2022-01-22 10:48:43 +00:00
pub fn init(&self) -> FlowyResult<()> {
2022-03-10 09:14:10 +00:00
listen_ws_state_changed(self.rev_web_socket.clone(), self.editor_map.clone());
2021-12-25 13:44:45 +00:00
2021-10-05 06:37:45 +00:00
Ok(())
}
2022-09-13 12:23:56 +00:00
#[tracing::instrument(level = "trace", skip(self, editor_id), fields(editor_id), err)]
2022-10-13 15:29:37 +00:00
pub async fn open_document_editor<T: AsRef<str>>(&self, editor_id: T) -> Result<Arc<DocumentEditor>, FlowyError> {
2022-09-13 12:23:56 +00:00
let editor_id = editor_id.as_ref();
tracing::Span::current().record("editor_id", &editor_id);
2022-10-13 15:29:37 +00:00
self.get_document_editor(editor_id).await
}
2022-09-13 12:23:56 +00:00
#[tracing::instrument(level = "trace", skip(self, editor_id), fields(editor_id), err)]
2022-10-13 15:29:37 +00:00
pub fn close_document_editor<T: AsRef<str>>(&self, editor_id: T) -> Result<(), FlowyError> {
2022-09-13 12:23:56 +00:00
let editor_id = editor_id.as_ref();
tracing::Span::current().record("editor_id", &editor_id);
self.editor_map.remove(editor_id);
Ok(())
}
2022-10-13 15:29:37 +00:00
#[tracing::instrument(level = "debug", skip(self, payload), err)]
pub async fn receive_local_operations(
&self,
payload: DocumentOperationsPB,
) -> Result<DocumentOperationsPB, FlowyError> {
let editor = self.get_document_editor(&payload.doc_id).await?;
let _ = editor
.compose_local_operations(Bytes::from(payload.operations_str))
.await?;
let operations_str = editor.get_operation_str().await?;
Ok(DocumentOperationsPB {
doc_id: payload.doc_id.clone(),
operations_str,
2021-12-31 02:32:25 +00:00
})
2021-09-23 05:15:35 +00:00
}
2021-12-31 02:32:25 +00:00
2022-09-13 12:23:56 +00:00
pub async fn apply_edit(&self, params: EditParams) -> FlowyResult<()> {
2022-10-13 15:29:37 +00:00
let editor = self.get_document_editor(&params.doc_id).await?;
let _ = editor
.compose_local_operations(Bytes::from(params.operations_str))
.await?;
2022-09-13 12:23:56 +00:00
Ok(())
}
2022-10-13 15:29:37 +00:00
pub async fn create_document<T: AsRef<str>>(&self, doc_id: T, revisions: RepeatedRevision) -> FlowyResult<()> {
let doc_id = doc_id.as_ref().to_owned();
2022-03-10 09:14:10 +00:00
let db_pool = self.user.db_pool()?;
2022-10-13 15:29:37 +00:00
// Maybe we could save the document to disk without creating the RevisionManager
let rev_manager = self.make_document_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-03-10 09:14:10 +00:00
Ok(data) => match self.editor_map.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-03-10 09:14:10 +00:00
Some(editor) => match 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-10-13 15:29:37 +00:00
impl DocumentManager {
/// Returns the `DocumentEditor`
/// Initializes the document editor if it's not initialized yet. Otherwise, returns the opened
/// editor.
///
/// # Arguments
///
/// * `doc_id`: the id of the document
///
/// returns: Result<Arc<DocumentEditor>, FlowyError>
///
async fn get_document_editor(&self, doc_id: &str) -> FlowyResult<Arc<DocumentEditor>> {
match self.editor_map.get(doc_id) {
2022-01-01 06:23:58 +00:00
None => {
2022-03-10 09:14:10 +00:00
let db_pool = self.user.db_pool()?;
2022-10-13 15:29:37 +00:00
self.init_document_editor(doc_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-10-13 15:29:37 +00:00
/// Initializes a document editor with the doc_id
///
/// # Arguments
///
/// * `doc_id`: the id of the document
/// * `pool`: sqlite connection pool
///
/// returns: Result<Arc<DocumentEditor>, FlowyError>
///
2022-04-10 08:29:45 +00:00
#[tracing::instrument(level = "trace", skip(self, pool), err)]
2022-10-13 15:29:37 +00:00
async fn init_document_editor(
2021-12-31 02:32:25 +00:00
&self,
2022-10-13 15:29:37 +00:00
doc_id: &str,
2021-12-31 02:32:25 +00:00
pool: Arc<ConnectionPool>,
2022-10-13 15:29:37 +00:00
) -> Result<Arc<DocumentEditor>, FlowyError> {
2022-03-10 09:14:10 +00:00
let user = self.user.clone();
let token = self.user.token()?;
2022-10-13 15:29:37 +00:00
let rev_manager = self.make_document_rev_manager(doc_id, pool.clone())?;
let cloud_service = Arc::new(DocumentRevisionCloudService {
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-10-13 15:29:37 +00:00
let editor = DocumentEditor::new(doc_id, user, rev_manager, self.rev_web_socket.clone(), cloud_service).await?;
self.editor_map.insert(doc_id, &editor);
Ok(editor)
2021-12-08 13:51:06 +00:00
}
2022-10-13 15:29:37 +00:00
fn make_document_rev_manager(
2022-09-13 12:23:56 +00:00
&self,
doc_id: &str,
pool: Arc<ConnectionPool>,
) -> Result<RevisionManager, FlowyError> {
2022-03-10 09:14:10 +00:00
let user_id = self.user.user_id()?;
2022-10-13 15:29:37 +00:00
let disk_cache = SQLiteDocumentRevisionPersistence::new(&user_id, pool.clone());
let rev_persistence = RevisionPersistence::new(&user_id, doc_id, disk_cache);
2022-07-20 06:07:54 +00:00
// let history_persistence = SQLiteRevisionHistoryPersistence::new(doc_id, pool.clone());
2022-06-10 14:27:19 +00:00
let snapshot_persistence = SQLiteRevisionSnapshotPersistence::new(doc_id, pool);
2022-10-13 15:29:37 +00:00
let rev_compactor = DocumentRevisionCompactor();
Ok(RevisionManager::new(
&user_id,
doc_id,
rev_persistence,
rev_compactor,
2022-07-20 06:07:54 +00:00
// history_persistence,
2022-06-10 14:27:19 +00:00
snapshot_persistence,
))
}
2021-09-21 07:07:07 +00:00
}
2021-09-26 08:39:57 +00:00
2022-10-13 15:29:37 +00:00
struct DocumentRevisionCloudService {
token: String,
2022-10-13 15:29:37 +00:00
server: Arc<dyn DocumentCloudService>,
}
2022-10-13 15:29:37 +00:00
impl RevisionCloudService for DocumentRevisionCloudService {
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-10-13 15:29:37 +00:00
let params: DocumentIdPB = 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-10-13 15:29:37 +00:00
match server.fetch_document(&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-10-13 15:29:37 +00:00
Some(payload) => {
let bytes = Bytes::from(payload.content.clone());
let doc_md5 = md5(&bytes);
2022-03-02 13:12:21 +00:00
let revision = Revision::new(
2022-10-13 15:29:37 +00:00
&payload.doc_id,
payload.base_rev_id,
payload.rev_id,
bytes,
2022-03-02 13:12:21 +00:00
&user_id,
doc_md5,
);
2022-01-14 07:23:21 +00:00
Ok(vec![revision])
2022-01-24 09:35:58 +00:00
}
}
})
}
}
2022-10-13 15:29:37 +00:00
pub struct DocumentEditorMap {
inner: DashMap<String, Arc<DocumentEditor>>,
2021-12-10 03:05:23 +00:00
}
2022-10-13 15:29:37 +00:00
impl DocumentEditorMap {
2022-01-24 09:35:58 +00:00
fn new() -> Self {
Self { inner: DashMap::new() }
}
2021-12-10 03:05:23 +00:00
2022-10-13 15:29:37 +00:00
pub(crate) fn insert(&self, editor_id: &str, doc: &Arc<DocumentEditor>) {
2022-09-13 12:23:56 +00:00
if self.inner.contains_key(editor_id) {
log::warn!("Doc:{} already exists in cache", editor_id);
2021-12-10 03:05:23 +00:00
}
2022-09-13 12:23:56 +00:00
self.inner.insert(editor_id.to_string(), doc.clone());
2021-12-10 03:05:23 +00:00
}
2022-10-13 15:29:37 +00:00
pub(crate) fn get(&self, editor_id: &str) -> Option<Arc<DocumentEditor>> {
2022-09-13 12:23:56 +00:00
Some(self.inner.get(editor_id)?.clone())
2021-12-10 03:05:23 +00:00
}
2022-09-13 12:23:56 +00:00
pub(crate) fn remove(&self, editor_id: &str) {
if let Some(editor) = self.get(editor_id) {
2022-01-01 06:23:58 +00:00
editor.stop()
2021-09-26 08:39:57 +00:00
}
2022-09-13 12:23:56 +00:00
self.inner.remove(editor_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-10-13 15:29:37 +00:00
fn listen_ws_state_changed(web_socket: Arc<dyn RevisionWebSocket>, handlers: Arc<DocumentEditorMap>) {
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
}
});
}