2021-07-23 06:37:18 +00:00
|
|
|
use crate::{
|
2021-09-23 05:15:35 +00:00
|
|
|
entities::doc::{CreateDocParams, Doc, DocDelta, QueryDocParams, UpdateDocParams},
|
|
|
|
errors::{internal_error, DocError},
|
2021-09-11 06:26:30 +00:00
|
|
|
module::DocumentUser,
|
2021-09-22 15:21:44 +00:00
|
|
|
services::{
|
|
|
|
cache::DocCache,
|
2021-09-23 05:15:35 +00:00
|
|
|
doc::edit_context::{DocId, EditDocContext, EditDocPersistence},
|
|
|
|
server::Server,
|
2021-09-22 15:21:44 +00:00
|
|
|
ws::WsManager,
|
|
|
|
},
|
2021-09-23 05:15:35 +00:00
|
|
|
sql_tables::doc::{DocTable, DocTableChangeset, DocTableSql, OpTableSql},
|
2021-09-22 15:21:44 +00:00
|
|
|
};
|
2021-09-23 05:15:35 +00:00
|
|
|
use bytes::Bytes;
|
|
|
|
use flowy_database::{ConnectionPool, SqliteConnection};
|
2021-09-22 15:21:44 +00:00
|
|
|
use parking_lot::RwLock;
|
2021-07-23 06:37:18 +00:00
|
|
|
use std::sync::Arc;
|
2021-09-11 12:09:46 +00:00
|
|
|
use tokio::task::JoinHandle;
|
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>,
|
|
|
|
op_sql: Arc<OpTableSql>,
|
2021-09-22 15:21:44 +00:00
|
|
|
ws: Arc<RwLock<WsManager>>,
|
|
|
|
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-22 15:21:44 +00:00
|
|
|
pub(crate) fn new(server: Server, user: Arc<dyn DocumentUser>, ws: Arc<RwLock<WsManager>>) -> Self {
|
2021-09-23 05:15:35 +00:00
|
|
|
let doc_sql = Arc::new(DocTableSql {});
|
|
|
|
let op_sql = Arc::new(OpTableSql {});
|
2021-09-22 15:21:44 +00:00
|
|
|
let cache = Arc::new(DocCache::new());
|
|
|
|
Self {
|
|
|
|
server,
|
2021-09-23 05:15:35 +00:00
|
|
|
doc_sql,
|
|
|
|
op_sql,
|
2021-09-22 15:21:44 +00:00
|
|
|
user,
|
|
|
|
ws,
|
|
|
|
cache,
|
|
|
|
}
|
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-22 15:21:44 +00:00
|
|
|
revision: 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-23 05:15:35 +00:00
|
|
|
pub(crate) async fn open(&self, params: QueryDocParams, pool: Arc<ConnectionPool>) -> Result<Arc<EditDocContext>, DocError> {
|
2021-09-22 15:21:44 +00:00
|
|
|
if self.cache.is_opened(¶ms.doc_id) == false {
|
2021-09-23 05:15:35 +00:00
|
|
|
return match self._open(params, pool).await {
|
2021-09-22 15:21:44 +00:00
|
|
|
Ok(doc) => Ok(doc),
|
|
|
|
Err(error) => Err(error),
|
|
|
|
};
|
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
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip(self, delta, pool), err)]
|
|
|
|
pub(crate) fn edit_doc(&self, delta: DocDelta, pool: Arc<ConnectionPool>) -> Result<Doc, DocError> {
|
|
|
|
let edit_doc_ctx = self.cache.get(&delta.doc_id)?;
|
|
|
|
let _ = edit_doc_ctx.apply_delta(Bytes::from(delta.data), pool)?;
|
|
|
|
Ok(edit_doc_ctx.doc())
|
|
|
|
}
|
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-13 07:51:13 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, params), err)]
|
2021-09-22 15:21:44 +00:00
|
|
|
fn update_doc_on_server(&self, params: UpdateDocParams) -> Result<(), DocError> {
|
2021-09-09 07:43:05 +00:00
|
|
|
let token = self.user.token()?;
|
|
|
|
let server = self.server.clone();
|
|
|
|
tokio::spawn(async move {
|
|
|
|
match server.update_doc(&token, params).await {
|
|
|
|
Ok(_) => {},
|
|
|
|
Err(e) => {
|
|
|
|
// TODO: retry?
|
|
|
|
log::error!("Update doc failed: {}", e);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-11 12:09:46 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, pool), err)]
|
|
|
|
fn read_doc_from_server(
|
|
|
|
&self,
|
|
|
|
params: QueryDocParams,
|
|
|
|
pool: Arc<ConnectionPool>,
|
2021-09-23 05:15:35 +00:00
|
|
|
) -> Result<JoinHandle<Result<Arc<EditDocContext>, DocError>>, DocError> {
|
2021-09-09 07:43:05 +00:00
|
|
|
let token = self.user.token()?;
|
|
|
|
let server = self.server.clone();
|
2021-09-23 05:15:35 +00:00
|
|
|
let doc_sql = self.doc_sql.clone();
|
|
|
|
let op_sql = self.op_sql.clone();
|
|
|
|
let ws = self.ws.clone();
|
|
|
|
let cache = self.cache.clone();
|
2021-09-09 07:43:05 +00:00
|
|
|
|
2021-09-11 12:09:46 +00:00
|
|
|
Ok(tokio::spawn(async move {
|
|
|
|
match server.read_doc(&token, params).await? {
|
2021-09-16 04:35:55 +00:00
|
|
|
None => Err(DocError::not_found()),
|
2021-09-11 12:09:46 +00:00
|
|
|
Some(doc) => {
|
|
|
|
let doc_table = DocTable::new(doc.clone());
|
2021-09-23 05:15:35 +00:00
|
|
|
let _ = doc_sql.create_doc_table(doc_table, &*(pool.get().map_err(internal_error)?))?;
|
|
|
|
let edit_doc_ctx = make_edit_context(ws, cache, op_sql, doc)?;
|
|
|
|
Ok(edit_doc_ctx)
|
2021-09-11 12:09:46 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2021-09-09 09:34:01 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self), err)]
|
2021-09-09 07:43:05 +00:00
|
|
|
fn delete_doc_on_server(&self, params: QueryDocParams) -> Result<(), DocError> {
|
|
|
|
let token = self.user.token()?;
|
|
|
|
let server = self.server.clone();
|
|
|
|
tokio::spawn(async move {
|
|
|
|
match server.delete_doc(&token, params).await {
|
|
|
|
Ok(_) => {},
|
|
|
|
Err(e) => {
|
|
|
|
// TODO: retry?
|
|
|
|
log::error!("Delete doc failed: {:?}", e);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Ok(())
|
2021-07-23 06:37:18 +00:00
|
|
|
}
|
2021-09-11 12:09:46 +00:00
|
|
|
|
2021-09-23 05:15:35 +00:00
|
|
|
async fn _open(&self, params: QueryDocParams, pool: Arc<ConnectionPool>) -> Result<Arc<EditDocContext>, DocError> {
|
|
|
|
match self.doc_sql.read_doc_table(¶ms.doc_id, &*(pool.get().map_err(internal_error)?)) {
|
2021-09-22 15:21:44 +00:00
|
|
|
Ok(doc_table) => {
|
2021-09-23 05:15:35 +00:00
|
|
|
let edit_doc_ctx = make_edit_context(self.ws.clone(), self.cache.clone(), self.op_sql.clone(), doc_table.into())?;
|
|
|
|
Ok(edit_doc_ctx)
|
2021-09-22 15:21:44 +00:00
|
|
|
},
|
|
|
|
Err(error) => {
|
|
|
|
if error.is_record_not_found() {
|
|
|
|
log::debug!("Doc:{} don't exist, reading from server", params.doc_id);
|
2021-09-23 05:15:35 +00:00
|
|
|
match self.read_doc_from_server(params, pool)?.await.map_err(internal_error)? {
|
|
|
|
Ok(edit_doc_ctx) => Ok(edit_doc_ctx),
|
|
|
|
Err(error) => Err(error),
|
|
|
|
}
|
|
|
|
} 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-23 05:15:35 +00:00
|
|
|
fn make_edit_context(
|
|
|
|
ws: Arc<RwLock<WsManager>>,
|
|
|
|
cache: Arc<DocCache>,
|
|
|
|
op_sql: Arc<OpTableSql>,
|
|
|
|
doc: Doc,
|
|
|
|
) -> Result<Arc<EditDocContext>, DocError> {
|
|
|
|
// Opti: require upgradable_read lock and then upgrade to write lock using
|
|
|
|
// RwLockUpgradableReadGuard::upgrade(xx)
|
|
|
|
let edit_doc_ctx = Arc::new(EditDocContext::new(doc, ws.read().sender.clone(), op_sql)?);
|
|
|
|
ws.write().register_handler(edit_doc_ctx.id.as_ref(), edit_doc_ctx.clone());
|
|
|
|
cache.set(edit_doc_ctx.clone());
|
|
|
|
Ok(edit_doc_ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EditDocPersistence for DocController {
|
2021-09-22 15:21:44 +00:00
|
|
|
fn save(&self, params: UpdateDocParams, pool: Arc<ConnectionPool>) -> Result<(), DocError> {
|
2021-09-21 07:07:07 +00:00
|
|
|
let changeset = DocTableChangeset::new(params.clone());
|
2021-09-23 05:15:35 +00:00
|
|
|
let _ = self.doc_sql.update_doc_table(changeset, &*(pool.get().map_err(internal_error)?))?;
|
2021-09-21 07:07:07 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|