|
@@ -5,7 +5,7 @@ use crate::services::rev_sqlite::{SQLiteDeltaDocumentRevisionPersistence, SQLite
|
|
use crate::services::DocumentPersistence;
|
|
use crate::services::DocumentPersistence;
|
|
use crate::{errors::FlowyError, DocumentCloudService};
|
|
use crate::{errors::FlowyError, DocumentCloudService};
|
|
use bytes::Bytes;
|
|
use bytes::Bytes;
|
|
-use dashmap::DashMap;
|
|
|
|
|
|
+
|
|
use flowy_database::ConnectionPool;
|
|
use flowy_database::ConnectionPool;
|
|
use flowy_error::FlowyResult;
|
|
use flowy_error::FlowyResult;
|
|
use flowy_revision::{
|
|
use flowy_revision::{
|
|
@@ -16,9 +16,11 @@ use flowy_sync::client_document::initial_delta_document_content;
|
|
use flowy_sync::entities::{document::DocumentIdPB, revision::Revision, ws_data::ServerRevisionWSData};
|
|
use flowy_sync::entities::{document::DocumentIdPB, revision::Revision, ws_data::ServerRevisionWSData};
|
|
use flowy_sync::util::md5;
|
|
use flowy_sync::util::md5;
|
|
use lib_infra::future::FutureResult;
|
|
use lib_infra::future::FutureResult;
|
|
|
|
+use lib_infra::ref_map::{RefCountHashMap, RefCountValue};
|
|
use lib_ws::WSConnectState;
|
|
use lib_ws::WSConnectState;
|
|
use std::any::Any;
|
|
use std::any::Any;
|
|
use std::{convert::TryInto, sync::Arc};
|
|
use std::{convert::TryInto, sync::Arc};
|
|
|
|
+use tokio::sync::RwLock;
|
|
|
|
|
|
pub trait DocumentUser: Send + Sync {
|
|
pub trait DocumentUser: Send + Sync {
|
|
fn user_dir(&self) -> Result<String, FlowyError>;
|
|
fn user_dir(&self) -> Result<String, FlowyError>;
|
|
@@ -76,7 +78,7 @@ impl std::default::Default for DocumentConfig {
|
|
pub struct DocumentManager {
|
|
pub struct DocumentManager {
|
|
cloud_service: Arc<dyn DocumentCloudService>,
|
|
cloud_service: Arc<dyn DocumentCloudService>,
|
|
rev_web_socket: Arc<dyn RevisionWebSocket>,
|
|
rev_web_socket: Arc<dyn RevisionWebSocket>,
|
|
- editor_map: Arc<DocumentEditorMap>,
|
|
|
|
|
|
+ editor_map: Arc<RwLock<RefCountHashMap<RefCountDocumentHandler>>>,
|
|
user: Arc<dyn DocumentUser>,
|
|
user: Arc<dyn DocumentUser>,
|
|
persistence: Arc<DocumentPersistence>,
|
|
persistence: Arc<DocumentPersistence>,
|
|
#[allow(dead_code)]
|
|
#[allow(dead_code)]
|
|
@@ -94,7 +96,7 @@ impl DocumentManager {
|
|
Self {
|
|
Self {
|
|
cloud_service,
|
|
cloud_service,
|
|
rev_web_socket,
|
|
rev_web_socket,
|
|
- editor_map: Arc::new(DocumentEditorMap::new()),
|
|
|
|
|
|
+ editor_map: Arc::new(RwLock::new(RefCountHashMap::new())),
|
|
user: document_user,
|
|
user: document_user,
|
|
persistence: Arc::new(DocumentPersistence::new(database)),
|
|
persistence: Arc::new(DocumentPersistence::new(database)),
|
|
config,
|
|
config,
|
|
@@ -124,10 +126,10 @@ impl DocumentManager {
|
|
}
|
|
}
|
|
|
|
|
|
#[tracing::instrument(level = "trace", skip(self, editor_id), fields(editor_id), err)]
|
|
#[tracing::instrument(level = "trace", skip(self, editor_id), fields(editor_id), err)]
|
|
- pub fn close_document_editor<T: AsRef<str>>(&self, editor_id: T) -> Result<(), FlowyError> {
|
|
|
|
|
|
+ pub async fn close_document_editor<T: AsRef<str>>(&self, editor_id: T) -> Result<(), FlowyError> {
|
|
let editor_id = editor_id.as_ref();
|
|
let editor_id = editor_id.as_ref();
|
|
tracing::Span::current().record("editor_id", &editor_id);
|
|
tracing::Span::current().record("editor_id", &editor_id);
|
|
- self.editor_map.remove(editor_id);
|
|
|
|
|
|
+ self.editor_map.write().await.remove(editor_id);
|
|
Ok(())
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
|
|
@@ -149,9 +151,9 @@ impl DocumentManager {
|
|
pub async fn receive_ws_data(&self, data: Bytes) {
|
|
pub async fn receive_ws_data(&self, data: Bytes) {
|
|
let result: Result<ServerRevisionWSData, protobuf::ProtobufError> = data.try_into();
|
|
let result: Result<ServerRevisionWSData, protobuf::ProtobufError> = data.try_into();
|
|
match result {
|
|
match result {
|
|
- Ok(data) => match self.editor_map.get(&data.object_id) {
|
|
|
|
|
|
+ Ok(data) => match self.editor_map.read().await.get(&data.object_id) {
|
|
None => tracing::error!("Can't find any source handler for {:?}-{:?}", data.object_id, data.ty),
|
|
None => tracing::error!("Can't find any source handler for {:?}-{:?}", data.object_id, data.ty),
|
|
- Some(editor) => match editor.receive_ws_data(data).await {
|
|
|
|
|
|
+ Some(handler) => match handler.0.receive_ws_data(data).await {
|
|
Ok(_) => {}
|
|
Ok(_) => {}
|
|
Err(e) => tracing::error!("{}", e),
|
|
Err(e) => tracing::error!("{}", e),
|
|
},
|
|
},
|
|
@@ -180,13 +182,13 @@ impl DocumentManager {
|
|
/// returns: Result<Arc<DocumentEditor>, FlowyError>
|
|
/// returns: Result<Arc<DocumentEditor>, FlowyError>
|
|
///
|
|
///
|
|
async fn get_document_editor(&self, doc_id: &str) -> FlowyResult<Arc<dyn DocumentEditor>> {
|
|
async fn get_document_editor(&self, doc_id: &str) -> FlowyResult<Arc<dyn DocumentEditor>> {
|
|
- match self.editor_map.get(doc_id) {
|
|
|
|
|
|
+ match self.editor_map.read().await.get(doc_id) {
|
|
None => {
|
|
None => {
|
|
//
|
|
//
|
|
tracing::warn!("Should call init_document_editor first");
|
|
tracing::warn!("Should call init_document_editor first");
|
|
self.init_document_editor(doc_id).await
|
|
self.init_document_editor(doc_id).await
|
|
}
|
|
}
|
|
- Some(editor) => Ok(editor),
|
|
|
|
|
|
+ Some(handler) => Ok(handler.0.clone()),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -216,14 +218,20 @@ impl DocumentManager {
|
|
DeltaDocumentEditor::new(doc_id, user, rev_manager, self.rev_web_socket.clone(), cloud_service)
|
|
DeltaDocumentEditor::new(doc_id, user, rev_manager, self.rev_web_socket.clone(), cloud_service)
|
|
.await?,
|
|
.await?,
|
|
);
|
|
);
|
|
- self.editor_map.insert(doc_id, editor.clone());
|
|
|
|
|
|
+ self.editor_map
|
|
|
|
+ .write()
|
|
|
|
+ .await
|
|
|
|
+ .insert(doc_id.to_string(), RefCountDocumentHandler(editor.clone()));
|
|
Ok(editor)
|
|
Ok(editor)
|
|
}
|
|
}
|
|
DocumentVersionPB::V1 => {
|
|
DocumentVersionPB::V1 => {
|
|
let rev_manager = self.make_document_rev_manager(doc_id, pool.clone())?;
|
|
let rev_manager = self.make_document_rev_manager(doc_id, pool.clone())?;
|
|
let editor: Arc<dyn DocumentEditor> =
|
|
let editor: Arc<dyn DocumentEditor> =
|
|
Arc::new(AppFlowyDocumentEditor::new(doc_id, user, rev_manager, cloud_service).await?);
|
|
Arc::new(AppFlowyDocumentEditor::new(doc_id, user, rev_manager, cloud_service).await?);
|
|
- self.editor_map.insert(doc_id, editor.clone());
|
|
|
|
|
|
+ self.editor_map
|
|
|
|
+ .write()
|
|
|
|
+ .await
|
|
|
|
+ .insert(doc_id.to_string(), RefCountDocumentHandler(editor.clone()));
|
|
Ok(editor)
|
|
Ok(editor)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -247,7 +255,7 @@ impl DocumentManager {
|
|
) -> Result<RevisionManager<Arc<ConnectionPool>>, FlowyError> {
|
|
) -> Result<RevisionManager<Arc<ConnectionPool>>, FlowyError> {
|
|
let user_id = self.user.user_id()?;
|
|
let user_id = self.user.user_id()?;
|
|
let disk_cache = SQLiteDocumentRevisionPersistence::new(&user_id, pool.clone());
|
|
let disk_cache = SQLiteDocumentRevisionPersistence::new(&user_id, pool.clone());
|
|
- let configuration = RevisionPersistenceConfiguration::new(100);
|
|
|
|
|
|
+ let configuration = RevisionPersistenceConfiguration::new(100, true);
|
|
let rev_persistence = RevisionPersistence::new(&user_id, doc_id, disk_cache, configuration);
|
|
let rev_persistence = RevisionPersistence::new(&user_id, doc_id, disk_cache, configuration);
|
|
// let history_persistence = SQLiteRevisionHistoryPersistence::new(doc_id, pool.clone());
|
|
// let history_persistence = SQLiteRevisionHistoryPersistence::new(doc_id, pool.clone());
|
|
let snapshot_persistence = SQLiteRevisionSnapshotPersistence::new(doc_id, pool);
|
|
let snapshot_persistence = SQLiteRevisionSnapshotPersistence::new(doc_id, pool);
|
|
@@ -268,7 +276,7 @@ impl DocumentManager {
|
|
) -> Result<RevisionManager<Arc<ConnectionPool>>, FlowyError> {
|
|
) -> Result<RevisionManager<Arc<ConnectionPool>>, FlowyError> {
|
|
let user_id = self.user.user_id()?;
|
|
let user_id = self.user.user_id()?;
|
|
let disk_cache = SQLiteDeltaDocumentRevisionPersistence::new(&user_id, pool.clone());
|
|
let disk_cache = SQLiteDeltaDocumentRevisionPersistence::new(&user_id, pool.clone());
|
|
- let configuration = RevisionPersistenceConfiguration::new(100);
|
|
|
|
|
|
+ let configuration = RevisionPersistenceConfiguration::new(100, true);
|
|
let rev_persistence = RevisionPersistence::new(&user_id, doc_id, disk_cache, configuration);
|
|
let rev_persistence = RevisionPersistence::new(&user_id, doc_id, disk_cache, configuration);
|
|
// let history_persistence = SQLiteRevisionHistoryPersistence::new(doc_id, pool.clone());
|
|
// let history_persistence = SQLiteRevisionHistoryPersistence::new(doc_id, pool.clone());
|
|
let snapshot_persistence = SQLiteRevisionSnapshotPersistence::new(doc_id, pool);
|
|
let snapshot_persistence = SQLiteRevisionSnapshotPersistence::new(doc_id, pool);
|
|
@@ -309,40 +317,32 @@ impl RevisionCloudService for DocumentRevisionCloudService {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-pub struct DocumentEditorMap {
|
|
|
|
- inner: DashMap<String, Arc<dyn DocumentEditor>>,
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-impl DocumentEditorMap {
|
|
|
|
- fn new() -> Self {
|
|
|
|
- Self { inner: DashMap::new() }
|
|
|
|
- }
|
|
|
|
|
|
+#[derive(Clone)]
|
|
|
|
+struct RefCountDocumentHandler(Arc<dyn DocumentEditor>);
|
|
|
|
|
|
- pub(crate) fn insert(&self, editor_id: &str, editor: Arc<dyn DocumentEditor>) {
|
|
|
|
- if self.inner.contains_key(editor_id) {
|
|
|
|
- log::warn!("Editor:{} already open", editor_id);
|
|
|
|
- }
|
|
|
|
- self.inner.insert(editor_id.to_string(), editor);
|
|
|
|
|
|
+impl RefCountValue for RefCountDocumentHandler {
|
|
|
|
+ fn did_remove(&self) {
|
|
|
|
+ self.0.close();
|
|
}
|
|
}
|
|
|
|
+}
|
|
|
|
|
|
- pub(crate) fn get(&self, editor_id: &str) -> Option<Arc<dyn DocumentEditor>> {
|
|
|
|
- Some(self.inner.get(editor_id)?.clone())
|
|
|
|
- }
|
|
|
|
|
|
+impl std::ops::Deref for RefCountDocumentHandler {
|
|
|
|
+ type Target = Arc<dyn DocumentEditor>;
|
|
|
|
|
|
- pub(crate) fn remove(&self, editor_id: &str) {
|
|
|
|
- if let Some(editor) = self.get(editor_id) {
|
|
|
|
- editor.close()
|
|
|
|
- }
|
|
|
|
- self.inner.remove(editor_id);
|
|
|
|
|
|
+ fn deref(&self) -> &Self::Target {
|
|
|
|
+ &self.0
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
#[tracing::instrument(level = "trace", skip(web_socket, handlers))]
|
|
#[tracing::instrument(level = "trace", skip(web_socket, handlers))]
|
|
-fn listen_ws_state_changed(web_socket: Arc<dyn RevisionWebSocket>, handlers: Arc<DocumentEditorMap>) {
|
|
|
|
|
|
+fn listen_ws_state_changed(
|
|
|
|
+ web_socket: Arc<dyn RevisionWebSocket>,
|
|
|
|
+ handlers: Arc<RwLock<RefCountHashMap<RefCountDocumentHandler>>>,
|
|
|
|
+) {
|
|
tokio::spawn(async move {
|
|
tokio::spawn(async move {
|
|
let mut notify = web_socket.subscribe_state_changed().await;
|
|
let mut notify = web_socket.subscribe_state_changed().await;
|
|
while let Ok(state) = notify.recv().await {
|
|
while let Ok(state) = notify.recv().await {
|
|
- handlers.inner.iter().for_each(|handler| {
|
|
|
|
|
|
+ handlers.read().await.values().iter().for_each(|handler| {
|
|
handler.receive_ws_state(&state);
|
|
handler.receive_ws_state(&state);
|
|
})
|
|
})
|
|
}
|
|
}
|