165 lines
5.3 KiB
Rust
Raw Normal View History

2022-03-05 10:59:44 +08:00
use crate::manager::GridUser;
2022-03-04 21:26:32 +08:00
use crate::services::kv_persistence::{GridKVPersistence, KVTransaction};
2022-03-04 18:11:12 +08:00
use flowy_collaboration::client_grid::{GridChange, GridPad};
use flowy_collaboration::entities::revision::Revision;
use flowy_collaboration::util::make_delta_from_revisions;
use flowy_error::{FlowyError, FlowyResult};
2022-03-05 10:59:44 +08:00
use flowy_grid_data_model::entities::{Field, Grid, GridId, RawRow};
2022-03-04 18:11:12 +08:00
use flowy_sync::{
RevisionCloudService, RevisionCompact, RevisionManager, RevisionObjectBuilder, RevisionPersistence,
RevisionWebSocket, RevisionWebSocketManager,
};
use lib_infra::future::FutureResult;
2022-03-05 10:59:44 +08:00
use lib_infra::uuid;
2022-03-04 18:11:12 +08:00
use lib_ot::core::PlainTextAttributes;
use lib_sqlite::ConnectionPool;
use std::sync::Arc;
2022-03-05 10:59:44 +08:00
use tokio::sync::RwLock;
2022-03-04 18:11:12 +08:00
pub struct ClientGridEditor {
2022-03-04 22:09:16 +08:00
grid_id: String,
2022-03-05 10:59:44 +08:00
user: Arc<dyn GridUser>,
grid_pad: Arc<RwLock<GridPad>>,
2022-03-04 18:11:12 +08:00
rev_manager: Arc<RevisionManager>,
2022-03-05 10:59:44 +08:00
kv_persistence: Arc<GridKVPersistence>,
2022-03-04 18:11:12 +08:00
}
impl ClientGridEditor {
pub async fn new(
2022-03-04 22:09:16 +08:00
grid_id: &str,
2022-03-05 10:59:44 +08:00
user: Arc<dyn GridUser>,
mut rev_manager: RevisionManager,
kv_persistence: Arc<GridKVPersistence>,
2022-03-04 22:09:16 +08:00
) -> FlowyResult<Arc<Self>> {
2022-03-05 10:59:44 +08:00
let token = user.token()?;
let cloud = Arc::new(GridRevisionCloudService { token });
let grid_pad = Arc::new(RwLock::new(
2022-03-04 18:11:12 +08:00
rev_manager.load::<GridPadBuilder, GridRevisionCompact>(cloud).await?,
));
let rev_manager = Arc::new(rev_manager);
2022-03-04 22:09:16 +08:00
Ok(Arc::new(Self {
2022-03-05 10:59:44 +08:00
grid_id: grid_id.to_owned(),
user,
grid_pad,
2022-03-04 18:11:12 +08:00
rev_manager,
2022-03-05 10:59:44 +08:00
kv_persistence,
2022-03-04 22:09:16 +08:00
}))
2022-03-04 18:11:12 +08:00
}
2022-03-05 10:59:44 +08:00
pub async fn create_empty_row(&self) -> FlowyResult<()> {
let row = RawRow {
id: uuid(),
grid_id: self.grid_id.clone(),
cell_by_field_id: Default::default(),
};
self.create_row(row).await?;
Ok(())
}
async fn create_row(&self, row: RawRow) -> FlowyResult<()> {
2022-03-04 21:26:32 +08:00
let _ = self.modify(|grid| Ok(grid.create_row(&row)?)).await?;
2022-03-05 10:59:44 +08:00
let _ = self.kv_persistence.set(row)?;
2022-03-04 18:11:12 +08:00
Ok(())
}
2022-03-04 21:26:32 +08:00
pub async fn delete_rows(&self, ids: Vec<String>) -> FlowyResult<()> {
let _ = self.modify(|grid| Ok(grid.delete_rows(&ids)?)).await?;
// let _ = self.kv.batch_delete(ids)?;
Ok(())
}
pub async fn create_field(&mut self, field: Field) -> FlowyResult<()> {
let _ = self.modify(|grid| Ok(grid.create_field(&field)?)).await?;
2022-03-05 10:59:44 +08:00
let _ = self.kv_persistence.set(field)?;
2022-03-04 21:26:32 +08:00
Ok(())
}
pub async fn delete_field(&mut self, field_id: &str) -> FlowyResult<()> {
let _ = self.modify(|grid| Ok(grid.delete_field(field_id)?)).await?;
// let _ = self.kv.remove(field_id)?;
Ok(())
}
2022-03-05 10:59:44 +08:00
pub async fn grid_data(&self) -> Grid {
self.grid_pad.read().await.grid_data()
}
2022-03-04 21:26:32 +08:00
async fn modify<F>(&self, f: F) -> FlowyResult<()>
2022-03-04 18:11:12 +08:00
where
2022-03-05 10:59:44 +08:00
F: for<'a> FnOnce(&'a mut GridPad) -> FlowyResult<Option<GridChange>>,
2022-03-04 18:11:12 +08:00
{
2022-03-05 10:59:44 +08:00
let mut write_guard = self.grid_pad.write().await;
2022-03-04 18:11:12 +08:00
match f(&mut *write_guard)? {
None => {}
Some(change) => {
let _ = self.apply_change(change).await?;
}
}
Ok(())
}
async fn apply_change(&self, change: GridChange) -> FlowyResult<()> {
let GridChange { delta, md5 } = change;
2022-03-05 10:59:44 +08:00
let user_id = self.user.user_id()?;
2022-03-04 18:11:12 +08:00
let (base_rev_id, rev_id) = self.rev_manager.next_rev_id_pair();
let delta_data = delta.to_bytes();
let revision = Revision::new(
&self.rev_manager.object_id,
base_rev_id,
rev_id,
delta_data,
2022-03-05 10:59:44 +08:00
&user_id,
2022-03-04 18:11:12 +08:00
md5,
);
let _ = self
.rev_manager
.add_local_revision::<GridRevisionCompact>(&revision)
.await?;
Ok(())
}
}
struct GridPadBuilder();
impl RevisionObjectBuilder for GridPadBuilder {
type Output = GridPad;
2022-03-05 10:59:44 +08:00
fn build_object(object_id: &str, revisions: Vec<Revision>) -> FlowyResult<Self::Output> {
let pad = GridPad::from_revisions(object_id, revisions)?;
2022-03-04 18:11:12 +08:00
Ok(pad)
}
}
struct GridRevisionCloudService {
#[allow(dead_code)]
token: String,
}
impl RevisionCloudService for GridRevisionCloudService {
#[tracing::instrument(level = "trace", skip(self))]
fn fetch_object(&self, _user_id: &str, _object_id: &str) -> FutureResult<Vec<Revision>, FlowyError> {
FutureResult::new(async move { Ok(vec![]) })
}
}
struct GridRevisionCompact();
impl RevisionCompact for GridRevisionCompact {
fn compact_revisions(user_id: &str, object_id: &str, mut revisions: Vec<Revision>) -> FlowyResult<Revision> {
if revisions.is_empty() {
return Err(FlowyError::internal().context("Can't compact the empty folder's revisions"));
}
if revisions.len() == 1 {
return Ok(revisions.pop().unwrap());
}
let first_revision = revisions.first().unwrap();
let last_revision = revisions.last().unwrap();
let (base_rev_id, rev_id) = first_revision.pair_rev_id();
let md5 = last_revision.md5.clone();
let delta = make_delta_from_revisions::<PlainTextAttributes>(revisions)?;
let delta_data = delta.to_bytes();
Ok(Revision::new(object_id, base_rev_id, rev_id, delta_data, user_id, md5))
}
}