rename some structs

This commit is contained in:
appflowy
2021-12-03 22:40:56 +08:00
parent 6fa7b0632a
commit e7cc11cc44
17 changed files with 163 additions and 172 deletions

View File

@ -1,7 +1,7 @@
use crate::{
errors::DocError,
services::{
doc::{doc_controller::DocController, ClientEditDoc},
doc::{doc_controller::DocController, ClientDocEditor},
server::construct_doc_server,
ws::WsDocumentManager,
},
@ -44,7 +44,7 @@ impl FlowyDocument {
Ok(())
}
pub async fn open(&self, params: DocIdentifier) -> Result<Arc<ClientEditDoc>, DocError> {
pub async fn open(&self, params: DocIdentifier) -> Result<Arc<ClientDocEditor>, DocError> {
let edit_context = self.doc_ctrl.open(params, self.user.db_pool()?).await?;
Ok(edit_context)
}

View File

@ -4,25 +4,25 @@ use dashmap::DashMap;
use crate::{
errors::DocError,
services::doc::{ClientEditDoc, DocId},
services::doc::{ClientDocEditor, DocId},
};
pub(crate) struct DocCache {
inner: DashMap<DocId, Arc<ClientEditDoc>>,
inner: DashMap<DocId, Arc<ClientDocEditor>>,
}
impl DocCache {
pub(crate) fn new() -> Self { Self { inner: DashMap::new() } }
#[allow(dead_code)]
pub(crate) fn all_docs(&self) -> Vec<Arc<ClientEditDoc>> {
pub(crate) fn all_docs(&self) -> Vec<Arc<ClientDocEditor>> {
self.inner
.iter()
.map(|kv| kv.value().clone())
.collect::<Vec<Arc<ClientEditDoc>>>()
.collect::<Vec<Arc<ClientDocEditor>>>()
}
pub(crate) fn set(&self, doc: Arc<ClientEditDoc>) {
pub(crate) fn set(&self, doc: Arc<ClientDocEditor>) {
let doc_id = doc.doc_id.clone();
if self.inner.contains_key(&doc_id) {
log::warn!("Doc:{} already exists in cache", &doc_id);
@ -32,7 +32,7 @@ impl DocCache {
pub(crate) fn contains(&self, doc_id: &str) -> bool { self.inner.get(doc_id).is_some() }
pub(crate) fn get(&self, doc_id: &str) -> Result<Arc<ClientEditDoc>, DocError> {
pub(crate) fn get(&self, doc_id: &str) -> Result<Arc<ClientDocEditor>, DocError> {
if !self.contains(&doc_id) {
return Err(doc_not_found());
}

View File

@ -4,7 +4,7 @@ use crate::{
services::{
cache::DocCache,
doc::{
edit::{ClientEditDoc, EditDocWsHandler},
edit::{ClientDocEditor, EditDocWsHandler},
revision::RevisionServer,
},
server::Server,
@ -45,7 +45,7 @@ impl DocController {
&self,
params: DocIdentifier,
pool: Arc<ConnectionPool>,
) -> Result<Arc<ClientEditDoc>, DocError> {
) -> Result<Arc<ClientDocEditor>, DocError> {
if !self.cache.contains(&params.doc_id) {
let edit_ctx = self.make_edit_context(&params.doc_id, pool.clone()).await?;
return Ok(edit_ctx);
@ -91,7 +91,11 @@ impl DocController {
}
impl DocController {
async fn make_edit_context(&self, doc_id: &str, pool: Arc<ConnectionPool>) -> Result<Arc<ClientEditDoc>, DocError> {
async fn make_edit_context(
&self,
doc_id: &str,
pool: Arc<ConnectionPool>,
) -> Result<Arc<ClientDocEditor>, DocError> {
// Opti: require upgradable_read lock and then upgrade to write lock using
// RwLockUpgradableReadGuard::upgrade(xx) of ws
// let doc = self.read_doc(doc_id, pool.clone()).await?;
@ -103,7 +107,7 @@ impl DocController {
server: self.server.clone(),
});
let edit_ctx = Arc::new(ClientEditDoc::new(doc_id, pool, ws, server, user).await?);
let edit_ctx = Arc::new(ClientDocEditor::new(doc_id, pool, ws, server, user).await?);
let ws_handler = Arc::new(EditDocWsHandler(edit_ctx.clone()));
self.ws_manager.register_handler(doc_id, ws_handler);
self.cache.set(edit_ctx.clone());

View File

@ -10,14 +10,14 @@ use lib_ot::core::{Attribute, Delta, Interval, OperationTransformable};
use std::{convert::TryFrom, sync::Arc};
use tokio::sync::{mpsc, oneshot, RwLock};
pub struct DocumentActor {
pub(crate) struct EditCommandQueue {
doc_id: String,
document: Arc<RwLock<Document>>,
receiver: Option<mpsc::UnboundedReceiver<DocumentMsg>>,
receiver: Option<mpsc::UnboundedReceiver<EditCommand>>,
}
impl DocumentActor {
pub fn new(doc_id: &str, delta: Delta, receiver: mpsc::UnboundedReceiver<DocumentMsg>) -> Self {
impl EditCommandQueue {
pub(crate) fn new(doc_id: &str, delta: Delta, receiver: mpsc::UnboundedReceiver<EditCommand>) -> Self {
let document = Arc::new(RwLock::new(Document::from_delta(delta)));
Self {
doc_id: doc_id.to_owned(),
@ -26,7 +26,7 @@ impl DocumentActor {
}
}
pub async fn run(mut self) {
pub(crate) async fn run(mut self) {
let mut receiver = self.receiver.take().expect("Should only call once");
let stream = stream! {
loop {
@ -46,13 +46,13 @@ impl DocumentActor {
.await;
}
async fn handle_message(&self, msg: DocumentMsg) -> Result<(), DocumentError> {
async fn handle_message(&self, msg: EditCommand) -> Result<(), DocumentError> {
match msg {
DocumentMsg::Delta { delta, ret } => {
EditCommand::ComposeDelta { delta, ret } => {
let result = self.composed_delta(delta).await;
let _ = ret.send(result);
},
DocumentMsg::RemoteRevision { bytes, ret } => {
EditCommand::RemoteRevision { bytes, ret } => {
let revision = Revision::try_from(bytes)?;
let delta = Delta::from_bytes(&revision.delta_data)?;
let rev_id: RevId = revision.rev_id.into();
@ -64,15 +64,15 @@ impl DocumentActor {
};
let _ = ret.send(Ok(transform_delta));
},
DocumentMsg::Insert { index, data, ret } => {
EditCommand::Insert { index, data, ret } => {
let delta = self.document.write().await.insert(index, data);
let _ = ret.send(delta);
},
DocumentMsg::Delete { interval, ret } => {
EditCommand::Delete { interval, ret } => {
let result = self.document.write().await.delete(interval);
let _ = ret.send(result);
},
DocumentMsg::Format {
EditCommand::Format {
interval,
attribute,
ret,
@ -80,25 +80,25 @@ impl DocumentActor {
let result = self.document.write().await.format(interval, attribute);
let _ = ret.send(result);
},
DocumentMsg::Replace { interval, data, ret } => {
EditCommand::Replace { interval, data, ret } => {
let result = self.document.write().await.replace(interval, data);
let _ = ret.send(result);
},
DocumentMsg::CanUndo { ret } => {
EditCommand::CanUndo { ret } => {
let _ = ret.send(self.document.read().await.can_undo());
},
DocumentMsg::CanRedo { ret } => {
EditCommand::CanRedo { ret } => {
let _ = ret.send(self.document.read().await.can_redo());
},
DocumentMsg::Undo { ret } => {
EditCommand::Undo { ret } => {
let result = self.document.write().await.undo();
let _ = ret.send(result);
},
DocumentMsg::Redo { ret } => {
EditCommand::Redo { ret } => {
let result = self.document.write().await.redo();
let _ = ret.send(result);
},
DocumentMsg::Doc { ret } => {
EditCommand::ReadDoc { ret } => {
let data = self.document.read().await.to_json();
let _ = ret.send(Ok(data));
},
@ -122,9 +122,9 @@ impl DocumentActor {
}
}
pub type Ret<T> = oneshot::Sender<Result<T, DocumentError>>;
pub enum DocumentMsg {
Delta {
pub(crate) type Ret<T> = oneshot::Sender<Result<T, DocumentError>>;
pub(crate) enum EditCommand {
ComposeDelta {
delta: Delta,
ret: Ret<()>,
},
@ -164,12 +164,12 @@ pub enum DocumentMsg {
Redo {
ret: Ret<UndoResult>,
},
Doc {
ReadDoc {
ret: Ret<String>,
},
}
pub struct TransformDeltas {
pub(crate) struct TransformDeltas {
pub client_prime: Delta,
pub server_prime: Delta,
pub server_rev_id: RevId,

View File

@ -2,7 +2,7 @@ use crate::{
errors::{internal_error, DocError, DocResult},
module::DocumentUser,
services::{
doc::{DocumentActor, DocumentMsg, OpenDocAction, RevisionManager, RevisionServer, TransformDeltas},
doc::{EditCommand, EditCommandQueue, OpenDocAction, RevisionManager, RevisionServer, TransformDeltas},
ws::{DocumentWebSocket, WsDocumentHandler},
},
};
@ -24,15 +24,15 @@ use tokio::sync::{mpsc, mpsc::UnboundedSender, oneshot};
pub type DocId = String;
pub struct ClientEditDoc {
pub struct ClientDocEditor {
pub doc_id: DocId,
rev_manager: Arc<RevisionManager>,
document: UnboundedSender<DocumentMsg>,
edit_tx: UnboundedSender<EditCommand>,
ws: Arc<dyn DocumentWebSocket>,
user: Arc<dyn DocumentUser>,
}
impl ClientEditDoc {
impl ClientDocEditor {
pub(crate) async fn new(
doc_id: &str,
pool: Arc<ConnectionPool>,
@ -45,13 +45,13 @@ impl ClientEditDoc {
spawn_rev_receiver(receiver, ws.clone());
let delta = rev_manager.load_document().await?;
let document = spawn_doc_edit_actor(doc_id, delta, pool.clone());
let edit_queue_tx = spawn_edit_queue(doc_id, delta, pool.clone());
let doc_id = doc_id.to_string();
let rev_manager = Arc::new(rev_manager);
let edit_doc = Self {
doc_id,
rev_manager,
document,
edit_tx: edit_queue_tx,
ws,
user,
};
@ -61,12 +61,12 @@ impl ClientEditDoc {
pub async fn insert<T: ToString>(&self, index: usize, data: T) -> Result<(), DocError> {
let (ret, rx) = oneshot::channel::<DocumentResult<Delta>>();
let msg = DocumentMsg::Insert {
let msg = EditCommand::Insert {
index,
data: data.to_string(),
ret,
};
let _ = self.document.send(msg);
let _ = self.edit_tx.send(msg);
let delta = rx.await.map_err(internal_error)??;
let _ = self.save_local_delta(delta).await?;
Ok(())
@ -74,8 +74,8 @@ impl ClientEditDoc {
pub async fn delete(&self, interval: Interval) -> Result<(), DocError> {
let (ret, rx) = oneshot::channel::<DocumentResult<Delta>>();
let msg = DocumentMsg::Delete { interval, ret };
let _ = self.document.send(msg);
let msg = EditCommand::Delete { interval, ret };
let _ = self.edit_tx.send(msg);
let delta = rx.await.map_err(internal_error)??;
let _ = self.save_local_delta(delta).await?;
Ok(())
@ -83,12 +83,12 @@ impl ClientEditDoc {
pub async fn format(&self, interval: Interval, attribute: Attribute) -> Result<(), DocError> {
let (ret, rx) = oneshot::channel::<DocumentResult<Delta>>();
let msg = DocumentMsg::Format {
let msg = EditCommand::Format {
interval,
attribute,
ret,
};
let _ = self.document.send(msg);
let _ = self.edit_tx.send(msg);
let delta = rx.await.map_err(internal_error)??;
let _ = self.save_local_delta(delta).await?;
Ok(())
@ -96,12 +96,12 @@ impl ClientEditDoc {
pub async fn replace<T: ToString>(&mut self, interval: Interval, data: T) -> Result<(), DocError> {
let (ret, rx) = oneshot::channel::<DocumentResult<Delta>>();
let msg = DocumentMsg::Replace {
let msg = EditCommand::Replace {
interval,
data: data.to_string(),
ret,
};
let _ = self.document.send(msg);
let _ = self.edit_tx.send(msg);
let delta = rx.await.map_err(internal_error)??;
let _ = self.save_local_delta(delta).await?;
Ok(())
@ -109,38 +109,38 @@ impl ClientEditDoc {
pub async fn can_undo(&self) -> bool {
let (ret, rx) = oneshot::channel::<bool>();
let msg = DocumentMsg::CanUndo { ret };
let _ = self.document.send(msg);
let msg = EditCommand::CanUndo { ret };
let _ = self.edit_tx.send(msg);
rx.await.unwrap_or(false)
}
pub async fn can_redo(&self) -> bool {
let (ret, rx) = oneshot::channel::<bool>();
let msg = DocumentMsg::CanRedo { ret };
let _ = self.document.send(msg);
let msg = EditCommand::CanRedo { ret };
let _ = self.edit_tx.send(msg);
rx.await.unwrap_or(false)
}
pub async fn undo(&self) -> Result<UndoResult, DocError> {
let (ret, rx) = oneshot::channel::<DocumentResult<UndoResult>>();
let msg = DocumentMsg::Undo { ret };
let _ = self.document.send(msg);
let msg = EditCommand::Undo { ret };
let _ = self.edit_tx.send(msg);
let r = rx.await.map_err(internal_error)??;
Ok(r)
}
pub async fn redo(&self) -> Result<UndoResult, DocError> {
let (ret, rx) = oneshot::channel::<DocumentResult<UndoResult>>();
let msg = DocumentMsg::Redo { ret };
let _ = self.document.send(msg);
let msg = EditCommand::Redo { ret };
let _ = self.edit_tx.send(msg);
let r = rx.await.map_err(internal_error)??;
Ok(r)
}
pub async fn delta(&self) -> DocResult<DocDelta> {
let (ret, rx) = oneshot::channel::<DocumentResult<String>>();
let msg = DocumentMsg::Doc { ret };
let _ = self.document.send(msg);
let msg = EditCommand::ReadDoc { ret };
let _ = self.edit_tx.send(msg);
let data = rx.await.map_err(internal_error)??;
Ok(DocDelta {
@ -162,11 +162,11 @@ impl ClientEditDoc {
pub(crate) async fn composing_local_delta(&self, data: Bytes) -> Result<(), DocError> {
let delta = Delta::from_bytes(&data)?;
let (ret, rx) = oneshot::channel::<DocumentResult<()>>();
let msg = DocumentMsg::Delta {
let msg = EditCommand::ComposeDelta {
delta: delta.clone(),
ret,
};
let _ = self.document.send(msg);
let _ = self.edit_tx.send(msg);
let _ = rx.await.map_err(internal_error)??;
let _ = self.save_local_delta(delta).await?;
@ -176,8 +176,8 @@ impl ClientEditDoc {
#[cfg(feature = "flowy_test")]
pub async fn doc_json(&self) -> DocResult<String> {
let (ret, rx) = oneshot::channel::<DocumentResult<String>>();
let msg = DocumentMsg::Doc { ret };
let _ = self.document.send(msg);
let msg = EditCommand::ReadDoc { ret };
let _ = self.edit_tx.send(msg);
let s = rx.await.map_err(internal_error)??;
Ok(s)
}
@ -208,7 +208,7 @@ impl ClientEditDoc {
async fn handle_push_rev(&self, bytes: Bytes) -> DocResult<()> {
// Transform the revision
let (ret, rx) = oneshot::channel::<DocumentResult<TransformDeltas>>();
let _ = self.document.send(DocumentMsg::RemoteRevision { bytes, ret });
let _ = self.edit_tx.send(EditCommand::RemoteRevision { bytes, ret });
let TransformDeltas {
client_prime,
server_prime,
@ -222,11 +222,11 @@ impl ClientEditDoc {
// compose delta
let (ret, rx) = oneshot::channel::<DocumentResult<()>>();
let msg = DocumentMsg::Delta {
let msg = EditCommand::ComposeDelta {
delta: client_prime.clone(),
ret,
};
let _ = self.document.send(msg);
let _ = self.edit_tx.send(msg);
let _ = rx.await.map_err(internal_error)??;
// update rev id
@ -278,7 +278,7 @@ impl ClientEditDoc {
}
}
pub struct EditDocWsHandler(pub Arc<ClientEditDoc>);
pub struct EditDocWsHandler(pub Arc<ClientDocEditor>);
impl WsDocumentHandler for EditDocWsHandler {
fn receive(&self, doc_data: WsDocumentData) {
@ -313,9 +313,9 @@ fn spawn_rev_receiver(mut receiver: mpsc::UnboundedReceiver<Revision>, ws: Arc<d
});
}
fn spawn_doc_edit_actor(doc_id: &str, delta: Delta, _pool: Arc<ConnectionPool>) -> UnboundedSender<DocumentMsg> {
let (sender, receiver) = mpsc::unbounded_channel::<DocumentMsg>();
let actor = DocumentActor::new(doc_id, delta, receiver);
fn spawn_edit_queue(doc_id: &str, delta: Delta, _pool: Arc<ConnectionPool>) -> UnboundedSender<EditCommand> {
let (sender, receiver) = mpsc::unbounded_channel::<EditCommand>();
let actor = EditCommandQueue::new(doc_id, delta, receiver);
tokio::spawn(actor.run());
sender
}

View File

@ -1,7 +1,7 @@
mod doc_actor;
mod edit_doc;
mod editor;
mod model;
pub(crate) use doc_actor::*;
pub use edit_doc::*;
pub use editor::*;
pub(crate) use model::*;

View File

@ -5,7 +5,6 @@ use crate::{
use flowy_database::ConnectionPool;
use flowy_document_infra::entities::doc::{Revision, RevisionRange};
use lib_infra::future::ResultFuture;
use std::sync::Arc;
use tokio::sync::broadcast;