2023-04-21 09:02:26 +00:00
|
|
|
use collab::plugin_impl::rocks_disk::RocksDiskPlugin;
|
|
|
|
use collab::preclude::{Collab, CollabBuilder};
|
|
|
|
use collab_persistence::kv::rocks_kv::RocksCollabDB;
|
2023-04-13 10:53:51 +00:00
|
|
|
use parking_lot::RwLock;
|
2023-04-21 09:02:26 +00:00
|
|
|
use std::{collections::HashMap, sync::Arc};
|
2023-04-13 10:53:51 +00:00
|
|
|
|
2023-04-28 06:08:53 +00:00
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
|
|
|
|
2023-04-13 10:53:51 +00:00
|
|
|
use crate::{
|
|
|
|
document::{Document, DocumentDataWrapper},
|
2023-04-24 06:25:00 +00:00
|
|
|
entities::DocEventPB,
|
2023-04-13 10:53:51 +00:00
|
|
|
notification::{send_notification, DocumentNotification},
|
|
|
|
};
|
|
|
|
|
|
|
|
pub trait DocumentUser: Send + Sync {
|
|
|
|
fn user_id(&self) -> Result<i64, FlowyError>;
|
|
|
|
fn token(&self) -> Result<String, FlowyError>; // unused now.
|
2023-04-21 09:02:26 +00:00
|
|
|
fn kv_db(&self) -> Result<Arc<RocksCollabDB>, FlowyError>;
|
2023-04-13 10:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct DocumentManager {
|
|
|
|
documents: Arc<RwLock<HashMap<String, Arc<Document>>>>,
|
|
|
|
user: Arc<dyn DocumentUser>,
|
|
|
|
}
|
|
|
|
|
|
|
|
// unsafe impl Send for DocumentManager {}
|
|
|
|
// unsafe impl Sync for DocumentManager {}
|
|
|
|
|
|
|
|
impl DocumentManager {
|
|
|
|
pub fn new(user: Arc<dyn DocumentUser>) -> Self {
|
|
|
|
Self {
|
|
|
|
documents: Default::default(),
|
|
|
|
user,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-18 11:06:21 +00:00
|
|
|
pub fn create_document(
|
|
|
|
&self,
|
|
|
|
doc_id: String,
|
|
|
|
data: DocumentDataWrapper,
|
|
|
|
) -> FlowyResult<Arc<Document>> {
|
2023-04-17 02:12:04 +00:00
|
|
|
let collab = self.get_collab_for_doc_id(&doc_id)?;
|
2023-04-24 06:25:00 +00:00
|
|
|
let document = Arc::new(Document::create_with_data(collab, data.0)?);
|
2023-04-28 12:47:40 +00:00
|
|
|
self.documents.write().insert(doc_id, document.clone());
|
2023-04-17 02:12:04 +00:00
|
|
|
Ok(document)
|
|
|
|
}
|
|
|
|
|
2023-04-13 10:53:51 +00:00
|
|
|
pub fn open_document(&self, doc_id: String) -> FlowyResult<Arc<Document>> {
|
|
|
|
if let Some(doc) = self.documents.read().get(&doc_id) {
|
|
|
|
return Ok(doc.clone());
|
|
|
|
}
|
2023-04-24 06:25:00 +00:00
|
|
|
tracing::debug!("open_document: {:?}", &doc_id);
|
|
|
|
let collab = self.get_collab_for_doc_id(&doc_id)?;
|
|
|
|
let document = Arc::new(Document::new(collab)?);
|
2023-04-13 10:53:51 +00:00
|
|
|
|
|
|
|
let clone_doc_id = doc_id.clone();
|
2023-04-24 06:25:00 +00:00
|
|
|
document
|
2023-04-13 10:53:51 +00:00
|
|
|
.lock()
|
2023-04-17 02:12:04 +00:00
|
|
|
.open(move |events, is_remote| {
|
2023-05-03 03:18:25 +00:00
|
|
|
tracing::debug!("data_change: {:?}, from remote: {}", &events, is_remote);
|
2023-04-17 02:12:04 +00:00
|
|
|
send_notification(&clone_doc_id, DocumentNotification::DidReceiveUpdate)
|
2023-04-25 07:52:57 +00:00
|
|
|
.payload::<DocEventPB>((events, is_remote).into())
|
2023-04-18 11:06:21 +00:00
|
|
|
.send();
|
2023-04-13 10:53:51 +00:00
|
|
|
})
|
|
|
|
.map_err(|err| FlowyError::internal().context(err))?;
|
2023-04-28 12:47:40 +00:00
|
|
|
self.documents.write().insert(doc_id, document.clone());
|
2023-04-13 10:53:51 +00:00
|
|
|
Ok(document)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn close_document(&self, doc_id: String) -> FlowyResult<()> {
|
|
|
|
self.documents.write().remove(&doc_id);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-04-21 09:02:26 +00:00
|
|
|
fn get_collab_for_doc_id(&self, doc_id: &str) -> Result<Collab, FlowyError> {
|
2023-04-13 10:53:51 +00:00
|
|
|
let uid = self.user.user_id()?;
|
|
|
|
let kv_db = self.user.kv_db()?;
|
|
|
|
let mut collab = CollabBuilder::new(uid, doc_id).build();
|
|
|
|
let disk_plugin = Arc::new(
|
2023-04-21 09:02:26 +00:00
|
|
|
RocksDiskPlugin::new(uid, kv_db).map_err(|err| FlowyError::internal().context(err))?,
|
2023-04-13 10:53:51 +00:00
|
|
|
);
|
|
|
|
collab.add_plugin(disk_plugin);
|
|
|
|
collab.initial();
|
|
|
|
Ok(collab)
|
|
|
|
}
|
|
|
|
}
|