2021-07-23 06:37:18 +00:00
|
|
|
use crate::{
|
2021-12-14 10:04:51 +00:00
|
|
|
errors::FlowyError,
|
2021-09-11 06:26:30 +00:00
|
|
|
module::DocumentUser,
|
2021-10-02 09:19:54 +00:00
|
|
|
services::{
|
|
|
|
doc::{
|
2021-12-16 13:31:36 +00:00
|
|
|
edit::ClientDocEditor,
|
2021-12-08 13:51:06 +00:00
|
|
|
revision::{RevisionCache, RevisionManager, RevisionServer},
|
2021-12-16 13:31:36 +00:00
|
|
|
DocumentWsHandlers,
|
2021-10-02 09:19:54 +00:00
|
|
|
},
|
|
|
|
server::Server,
|
|
|
|
},
|
2021-09-22 15:21:44 +00:00
|
|
|
};
|
2021-11-09 08:00:09 +00:00
|
|
|
use bytes::Bytes;
|
2021-12-10 03:05:23 +00:00
|
|
|
use dashmap::DashMap;
|
2021-12-11 05:47:16 +00:00
|
|
|
use flowy_collaboration::entities::doc::{Doc, DocDelta, DocIdentifier};
|
2021-10-06 07:23:38 +00:00
|
|
|
use flowy_database::ConnectionPool;
|
2021-12-14 10:04:51 +00:00
|
|
|
use flowy_error::FlowyResult;
|
2021-12-13 05:55:44 +00:00
|
|
|
use lib_infra::future::FutureResult;
|
2021-11-09 08:00:09 +00:00
|
|
|
use std::sync::Arc;
|
2021-07-23 06:37:18 +00:00
|
|
|
|
2021-09-14 08:22:44 +00:00
|
|
|
pub(crate) struct DocController {
|
2021-09-09 07:43:05 +00:00
|
|
|
server: Server,
|
2021-12-16 13:31:36 +00:00
|
|
|
ws_handlers: Arc<DocumentWsHandlers>,
|
2021-12-10 03:05:23 +00:00
|
|
|
open_cache: Arc<OpenDocCache>,
|
2021-09-09 07:43:05 +00:00
|
|
|
user: Arc<dyn DocumentUser>,
|
2021-07-23 06:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DocController {
|
2021-12-16 13:31:36 +00:00
|
|
|
pub(crate) fn new(server: Server, user: Arc<dyn DocumentUser>, ws_handlers: Arc<DocumentWsHandlers>) -> Self {
|
2021-12-10 03:05:23 +00:00
|
|
|
let open_cache = Arc::new(OpenDocCache::new());
|
2021-12-16 13:31:36 +00:00
|
|
|
Self { server, ws_handlers, open_cache, user }
|
2021-07-23 06:37:18 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 10:04:51 +00:00
|
|
|
pub(crate) fn init(&self) -> FlowyResult<()> {
|
2021-12-16 13:31:36 +00:00
|
|
|
self.ws_handlers.init();
|
2021-10-05 06:37:45 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-26 08:39:57 +00:00
|
|
|
pub(crate) async fn open(
|
|
|
|
&self,
|
2021-10-15 07:52:08 +00:00
|
|
|
params: DocIdentifier,
|
2021-09-26 08:39:57 +00:00
|
|
|
pool: Arc<ConnectionPool>,
|
2021-12-14 10:04:51 +00:00
|
|
|
) -> Result<Arc<ClientDocEditor>, FlowyError> {
|
2021-12-10 03:05:23 +00:00
|
|
|
if !self.open_cache.contains(¶ms.doc_id) {
|
2021-09-26 12:04:47 +00:00
|
|
|
let edit_ctx = self.make_edit_context(¶ms.doc_id, pool.clone()).await?;
|
|
|
|
return Ok(edit_ctx);
|
2021-09-11 12:09:46 +00:00
|
|
|
}
|
2021-09-22 15:21:44 +00:00
|
|
|
|
2021-12-10 03:05:23 +00:00
|
|
|
let edit_doc_ctx = self.open_cache.get(¶ms.doc_id)?;
|
2021-09-23 05:15:35 +00:00
|
|
|
Ok(edit_doc_ctx)
|
2021-09-09 07:43:05 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 10:04:51 +00:00
|
|
|
pub(crate) fn close(&self, doc_id: &str) -> Result<(), FlowyError> {
|
2021-12-13 14:46:35 +00:00
|
|
|
tracing::debug!("Close doc {}", doc_id);
|
2021-12-10 03:05:23 +00:00
|
|
|
self.open_cache.remove(doc_id);
|
2021-12-16 13:31:36 +00:00
|
|
|
self.ws_handlers.remove_handler(doc_id);
|
2021-09-22 15:21:44 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-10-06 07:23:38 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self), err)]
|
2021-12-14 10:04:51 +00:00
|
|
|
pub(crate) fn delete(&self, params: DocIdentifier) -> Result<(), FlowyError> {
|
2021-09-22 15:21:44 +00:00
|
|
|
let doc_id = ¶ms.doc_id;
|
2021-12-10 03:05:23 +00:00
|
|
|
self.open_cache.remove(doc_id);
|
2021-12-16 13:31:36 +00:00
|
|
|
self.ws_handlers.remove_handler(doc_id);
|
2021-07-23 06:37:18 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-09-23 05:15:35 +00:00
|
|
|
|
2021-11-03 05:52:33 +00:00
|
|
|
// the delta's data that contains attributes with null value will be considered
|
|
|
|
// as None e.g.
|
|
|
|
// json : {"retain":7,"attributes":{"bold":null}}
|
|
|
|
// deserialize delta: [ {retain: 7, attributes: {Bold: AttributeValue(None)}} ]
|
2021-11-13 03:53:50 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, delta, db_pool), fields(doc_id = %delta.doc_id), err)]
|
|
|
|
pub(crate) async fn apply_local_delta(
|
|
|
|
&self,
|
|
|
|
delta: DocDelta,
|
|
|
|
db_pool: Arc<ConnectionPool>,
|
2021-12-14 10:04:51 +00:00
|
|
|
) -> Result<DocDelta, FlowyError> {
|
2021-12-10 03:05:23 +00:00
|
|
|
if !self.open_cache.contains(&delta.doc_id) {
|
2021-11-13 03:53:50 +00:00
|
|
|
let doc_identifier: DocIdentifier = delta.doc_id.clone().into();
|
|
|
|
let _ = self.open(doc_identifier, db_pool).await?;
|
|
|
|
}
|
|
|
|
|
2021-12-10 03:05:23 +00:00
|
|
|
let edit_doc_ctx = self.open_cache.get(&delta.doc_id)?;
|
2021-11-04 04:47:41 +00:00
|
|
|
let _ = edit_doc_ctx.composing_local_delta(Bytes::from(delta.data)).await?;
|
2021-10-06 07:23:38 +00:00
|
|
|
Ok(edit_doc_ctx.delta().await?)
|
2021-09-23 05:15:35 +00:00
|
|
|
}
|
2021-09-09 07:43:05 +00:00
|
|
|
}
|
2021-07-23 06:37:18 +00:00
|
|
|
|
2021-09-09 07:43:05 +00:00
|
|
|
impl DocController {
|
2021-12-03 14:40:56 +00:00
|
|
|
async fn make_edit_context(
|
|
|
|
&self,
|
|
|
|
doc_id: &str,
|
|
|
|
pool: Arc<ConnectionPool>,
|
2021-12-14 10:04:51 +00:00
|
|
|
) -> Result<Arc<ClientDocEditor>, FlowyError> {
|
2021-12-08 13:51:06 +00:00
|
|
|
let user = self.user.clone();
|
|
|
|
let rev_manager = self.make_rev_manager(doc_id, pool.clone())?;
|
2021-12-16 13:31:36 +00:00
|
|
|
let doc_editor = ClientDocEditor::new(doc_id, user, pool, rev_manager, self.ws_handlers.ws()).await?;
|
|
|
|
let ws_handler = doc_editor.ws_handler();
|
|
|
|
self.ws_handlers.register_handler(doc_id, ws_handler);
|
|
|
|
self.open_cache.insert(&doc_id, &doc_editor);
|
|
|
|
Ok(doc_editor)
|
2021-12-08 13:51:06 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 10:04:51 +00:00
|
|
|
fn make_rev_manager(&self, doc_id: &str, pool: Arc<ConnectionPool>) -> Result<RevisionManager, FlowyError> {
|
2021-09-26 12:04:47 +00:00
|
|
|
// Opti: require upgradable_read lock and then upgrade to write lock using
|
|
|
|
// RwLockUpgradableReadGuard::upgrade(xx) of ws
|
2021-10-02 09:19:54 +00:00
|
|
|
// let doc = self.read_doc(doc_id, pool.clone()).await?;
|
|
|
|
let token = self.user.token()?;
|
2021-12-09 14:28:11 +00:00
|
|
|
let user_id = self.user.user_id()?;
|
2021-10-02 09:19:54 +00:00
|
|
|
let server = Arc::new(RevisionServerImpl {
|
|
|
|
token,
|
|
|
|
server: self.server.clone(),
|
|
|
|
});
|
2021-12-09 14:28:11 +00:00
|
|
|
let cache = Arc::new(RevisionCache::new(&user_id, doc_id, pool, server));
|
2021-12-16 13:31:36 +00:00
|
|
|
Ok(RevisionManager::new(&user_id, doc_id, cache))
|
2021-07-23 06:37:18 +00:00
|
|
|
}
|
2021-09-21 07:07:07 +00:00
|
|
|
}
|
2021-09-26 08:39:57 +00:00
|
|
|
|
2021-10-02 09:19:54 +00:00
|
|
|
struct RevisionServerImpl {
|
|
|
|
token: String,
|
|
|
|
server: Server,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RevisionServer for RevisionServerImpl {
|
2021-10-18 14:08:35 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
2021-12-14 10:04:51 +00:00
|
|
|
fn fetch_document(&self, doc_id: &str) -> FutureResult<Doc, FlowyError> {
|
2021-10-15 07:52:08 +00:00
|
|
|
let params = DocIdentifier {
|
2021-10-02 09:19:54 +00:00
|
|
|
doc_id: doc_id.to_string(),
|
|
|
|
};
|
|
|
|
let server = self.server.clone();
|
|
|
|
let token = self.token.clone();
|
|
|
|
|
2021-12-13 05:55:44 +00:00
|
|
|
FutureResult::new(async move {
|
2021-10-02 09:19:54 +00:00
|
|
|
match server.read_doc(&token, params).await? {
|
2021-12-14 10:04:51 +00:00
|
|
|
None => Err(FlowyError::record_not_found().context("Remote doesn't have this document")),
|
2021-10-06 07:23:38 +00:00
|
|
|
Some(doc) => Ok(doc),
|
2021-10-02 09:19:54 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-10 03:05:23 +00:00
|
|
|
pub struct OpenDocCache {
|
|
|
|
inner: DashMap<String, Arc<ClientDocEditor>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OpenDocCache {
|
|
|
|
fn new() -> Self { Self { inner: DashMap::new() } }
|
|
|
|
|
2021-12-16 13:31:36 +00:00
|
|
|
pub(crate) fn insert(&self, doc_id: &str, doc: &Arc<ClientDocEditor>) {
|
|
|
|
if self.inner.contains_key(doc_id) {
|
|
|
|
log::warn!("Doc:{} already exists in cache", doc_id);
|
2021-12-10 03:05:23 +00:00
|
|
|
}
|
2021-12-16 13:31:36 +00:00
|
|
|
self.inner.insert(doc_id.to_string(), doc.clone());
|
2021-12-10 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn contains(&self, doc_id: &str) -> bool { self.inner.get(doc_id).is_some() }
|
|
|
|
|
2021-12-14 10:04:51 +00:00
|
|
|
pub(crate) fn get(&self, doc_id: &str) -> Result<Arc<ClientDocEditor>, FlowyError> {
|
2021-12-10 03:05:23 +00:00
|
|
|
if !self.contains(&doc_id) {
|
|
|
|
return Err(doc_not_found());
|
|
|
|
}
|
|
|
|
let opened_doc = self.inner.get(doc_id).unwrap();
|
|
|
|
Ok(opened_doc.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn remove(&self, id: &str) {
|
|
|
|
let doc_id = id.to_string();
|
|
|
|
match self.get(id) {
|
|
|
|
Ok(editor) => editor.stop_sync(),
|
|
|
|
Err(e) => log::error!("{}", e),
|
2021-09-26 08:39:57 +00:00
|
|
|
}
|
2021-12-10 03:05:23 +00:00
|
|
|
self.inner.remove(&doc_id);
|
|
|
|
}
|
2021-09-26 08:39:57 +00:00
|
|
|
}
|
2021-12-10 03:05:23 +00:00
|
|
|
|
2021-12-14 10:04:51 +00:00
|
|
|
fn doc_not_found() -> FlowyError {
|
|
|
|
FlowyError::record_not_found().context("Doc is close or you should call open first")
|
|
|
|
}
|