2021-10-01 11:39:08 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use bytes::Bytes;
|
|
|
|
use parking_lot::RwLock;
|
|
|
|
use tokio::time::{interval, Duration};
|
|
|
|
|
|
|
|
use flowy_database::{ConnectionPool, SqliteConnection};
|
2021-10-02 09:19:54 +00:00
|
|
|
use flowy_infra::future::{wrap_future, FnFuture, ResultFuture};
|
2021-10-01 11:39:08 +00:00
|
|
|
|
2021-07-23 06:37:18 +00:00
|
|
|
use crate::{
|
2021-09-26 12:04:47 +00:00
|
|
|
entities::doc::{CreateDocParams, Doc, DocDelta, QueryDocParams},
|
2021-10-02 13:35:06 +00:00
|
|
|
errors::{internal_error, DocError},
|
2021-09-11 06:26:30 +00:00
|
|
|
module::DocumentUser,
|
2021-10-02 09:19:54 +00:00
|
|
|
services::{
|
|
|
|
cache::DocCache,
|
|
|
|
doc::{
|
|
|
|
edit::ClientEditDoc,
|
|
|
|
revision::{DocRevision, RevisionServer},
|
|
|
|
},
|
|
|
|
server::Server,
|
|
|
|
ws::WsDocumentManager,
|
|
|
|
},
|
2021-09-27 15:23:23 +00:00
|
|
|
sql_tables::doc::{DocTable, DocTableSql},
|
2021-09-22 15:21:44 +00:00
|
|
|
};
|
2021-10-02 09:19:54 +00:00
|
|
|
use flowy_ot::core::Delta;
|
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-09-23 05:15:35 +00:00
|
|
|
doc_sql: Arc<DocTableSql>,
|
2021-09-23 11:59:58 +00:00
|
|
|
ws: Arc<RwLock<WsDocumentManager>>,
|
2021-09-22 15:21:44 +00:00
|
|
|
cache: Arc<DocCache>,
|
2021-09-09 07:43:05 +00:00
|
|
|
user: Arc<dyn DocumentUser>,
|
2021-07-23 06:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DocController {
|
2021-09-23 11:59:58 +00:00
|
|
|
pub(crate) fn new(server: Server, user: Arc<dyn DocumentUser>, ws: Arc<RwLock<WsDocumentManager>>) -> Self {
|
2021-09-23 05:15:35 +00:00
|
|
|
let doc_sql = Arc::new(DocTableSql {});
|
2021-09-22 15:21:44 +00:00
|
|
|
let cache = Arc::new(DocCache::new());
|
2021-09-26 08:39:57 +00:00
|
|
|
let controller = Self {
|
2021-09-22 15:21:44 +00:00
|
|
|
server,
|
2021-09-23 05:15:35 +00:00
|
|
|
doc_sql,
|
2021-09-22 15:21:44 +00:00
|
|
|
user,
|
|
|
|
ws,
|
2021-09-26 08:39:57 +00:00
|
|
|
cache: cache.clone(),
|
|
|
|
};
|
|
|
|
controller
|
2021-07-23 06:37:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 06:26:30 +00:00
|
|
|
#[tracing::instrument(skip(self, conn), err)]
|
2021-09-14 08:22:44 +00:00
|
|
|
pub(crate) fn create(&self, params: CreateDocParams, conn: &SqliteConnection) -> Result<(), DocError> {
|
2021-09-10 07:53:24 +00:00
|
|
|
let doc = Doc {
|
|
|
|
id: params.id,
|
|
|
|
data: params.data,
|
2021-09-25 13:47:02 +00:00
|
|
|
rev_id: 0,
|
2021-09-10 07:53:24 +00:00
|
|
|
};
|
2021-09-23 05:15:35 +00:00
|
|
|
let _ = self.doc_sql.create_doc_table(DocTable::new(doc), conn)?;
|
2021-09-10 07:53:24 +00:00
|
|
|
Ok(())
|
2021-07-23 06:37:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 12:09:46 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, pool), err)]
|
2021-09-26 08:39:57 +00:00
|
|
|
pub(crate) async fn open(
|
|
|
|
&self,
|
|
|
|
params: QueryDocParams,
|
|
|
|
pool: Arc<ConnectionPool>,
|
2021-10-02 09:19:54 +00:00
|
|
|
) -> Result<Arc<ClientEditDoc>, DocError> {
|
2021-09-22 15:21:44 +00:00
|
|
|
if self.cache.is_opened(¶ms.doc_id) == false {
|
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-09-23 05:15:35 +00:00
|
|
|
let edit_doc_ctx = self.cache.get(¶ms.doc_id)?;
|
|
|
|
Ok(edit_doc_ctx)
|
2021-09-09 07:43:05 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 15:21:44 +00:00
|
|
|
pub(crate) fn close(&self, doc_id: &str) -> Result<(), DocError> {
|
|
|
|
self.cache.remove(doc_id);
|
|
|
|
self.ws.write().remove_handler(doc_id);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-11 06:26:30 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, conn), err)]
|
2021-09-14 08:22:44 +00:00
|
|
|
pub(crate) fn delete(&self, params: QueryDocParams, conn: &SqliteConnection) -> Result<(), DocError> {
|
2021-09-22 15:21:44 +00:00
|
|
|
let doc_id = ¶ms.doc_id;
|
2021-09-23 05:15:35 +00:00
|
|
|
let _ = self.doc_sql.delete_doc(doc_id, &*conn)?;
|
2021-09-22 15:21:44 +00:00
|
|
|
|
|
|
|
self.cache.remove(doc_id);
|
|
|
|
self.ws.write().remove_handler(doc_id);
|
2021-09-09 07:43:05 +00:00
|
|
|
let _ = self.delete_doc_on_server(params)?;
|
2021-07-23 06:37:18 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-09-23 05:15:35 +00:00
|
|
|
|
2021-09-25 13:47:02 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, delta), err)]
|
2021-10-01 11:39:08 +00:00
|
|
|
pub(crate) async fn edit_doc(&self, delta: DocDelta) -> Result<Doc, DocError> {
|
2021-09-23 05:15:35 +00:00
|
|
|
let edit_doc_ctx = self.cache.get(&delta.doc_id)?;
|
2021-10-01 11:39:08 +00:00
|
|
|
let _ = edit_doc_ctx.compose_local_delta(Bytes::from(delta.data)).await?;
|
|
|
|
Ok(edit_doc_ctx.doc().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-09-26 12:04:47 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self), err)]
|
|
|
|
fn delete_doc_on_server(&self, params: QueryDocParams) -> Result<(), DocError> {
|
2021-09-09 07:43:05 +00:00
|
|
|
let token = self.user.token()?;
|
|
|
|
let server = self.server.clone();
|
|
|
|
tokio::spawn(async move {
|
2021-09-26 12:04:47 +00:00
|
|
|
match server.delete_doc(&token, params).await {
|
2021-09-09 07:43:05 +00:00
|
|
|
Ok(_) => {},
|
|
|
|
Err(e) => {
|
|
|
|
// TODO: retry?
|
2021-09-26 12:04:47 +00:00
|
|
|
log::error!("Delete doc failed: {:?}", e);
|
2021-09-09 07:43:05 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-10-02 09:19:54 +00:00
|
|
|
async fn make_edit_context(&self, doc_id: &str, pool: Arc<ConnectionPool>) -> Result<Arc<ClientEditDoc>, DocError> {
|
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?;
|
2021-09-26 12:04:47 +00:00
|
|
|
let ws_sender = self.ws.read().sender();
|
2021-10-02 09:19:54 +00:00
|
|
|
let token = self.user.token()?;
|
2021-10-03 03:33:19 +00:00
|
|
|
let user = self.user.clone();
|
2021-10-02 09:19:54 +00:00
|
|
|
let server = Arc::new(RevisionServerImpl {
|
|
|
|
token,
|
|
|
|
server: self.server.clone(),
|
|
|
|
});
|
|
|
|
|
2021-10-03 03:33:19 +00:00
|
|
|
let edit_ctx = Arc::new(ClientEditDoc::new(doc_id, pool, ws_sender, server, user).await?);
|
2021-09-27 06:49:05 +00:00
|
|
|
self.ws.write().register_handler(doc_id, edit_ctx.clone());
|
2021-09-26 12:04:47 +00:00
|
|
|
self.cache.set(edit_ctx.clone());
|
|
|
|
Ok(edit_ctx)
|
2021-07-23 06:37:18 +00:00
|
|
|
}
|
2021-09-11 12:09:46 +00:00
|
|
|
|
2021-10-02 09:19:54 +00:00
|
|
|
#[allow(dead_code)]
|
2021-09-26 12:04:47 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, pool), err)]
|
2021-09-27 06:49:05 +00:00
|
|
|
async fn read_doc(&self, doc_id: &str, pool: Arc<ConnectionPool>) -> Result<Doc, DocError> {
|
2021-09-26 12:04:47 +00:00
|
|
|
match self.doc_sql.read_doc_table(doc_id, pool.clone()) {
|
2021-09-27 06:49:05 +00:00
|
|
|
Ok(doc_table) => Ok(doc_table.into()),
|
2021-09-22 15:21:44 +00:00
|
|
|
Err(error) => {
|
|
|
|
if error.is_record_not_found() {
|
2021-09-26 12:04:47 +00:00
|
|
|
let token = self.user.token()?;
|
|
|
|
let params = QueryDocParams {
|
|
|
|
doc_id: doc_id.to_string(),
|
|
|
|
};
|
|
|
|
match self.server.read_doc(&token, params).await? {
|
|
|
|
None => Err(DocError::not_found()),
|
|
|
|
Some(doc) => {
|
|
|
|
let conn = &*pool.get().map_err(internal_error)?;
|
|
|
|
let _ = self.doc_sql.create_doc_table(doc.clone().into(), conn)?;
|
2021-09-27 06:49:05 +00:00
|
|
|
Ok(doc)
|
2021-09-26 12:04:47 +00:00
|
|
|
},
|
|
|
|
}
|
2021-09-23 05:15:35 +00:00
|
|
|
} else {
|
|
|
|
return Err(error);
|
2021-09-22 15:21:44 +00:00
|
|
|
}
|
|
|
|
},
|
2021-09-11 12:09:46 +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 {
|
|
|
|
fn fetch_document_from_remote(&self, doc_id: &str) -> ResultFuture<DocRevision, DocError> {
|
|
|
|
let params = QueryDocParams {
|
|
|
|
doc_id: doc_id.to_string(),
|
|
|
|
};
|
|
|
|
let server = self.server.clone();
|
|
|
|
let token = self.token.clone();
|
|
|
|
|
|
|
|
ResultFuture::new(async move {
|
|
|
|
match server.read_doc(&token, params).await? {
|
|
|
|
None => Err(DocError::not_found()),
|
|
|
|
Some(doc) => {
|
|
|
|
let delta = Delta::from_bytes(doc.data)?;
|
|
|
|
Ok(DocRevision {
|
2021-10-02 13:35:06 +00:00
|
|
|
rev_id: doc.rev_id.into(),
|
2021-10-02 09:19:54 +00:00
|
|
|
delta,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-26 08:39:57 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
fn event_loop(_cache: Arc<DocCache>) -> FnFuture<()> {
|
|
|
|
let mut i = interval(Duration::from_secs(3));
|
|
|
|
wrap_future(async move {
|
|
|
|
loop {
|
|
|
|
// cache.all_docs().iter().for_each(|doc| doc.tick());
|
|
|
|
i.tick().await;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|