306 lines
11 KiB
Rust
Raw Normal View History

2022-03-05 10:59:44 +08:00
use crate::manager::GridUser;
2022-03-14 23:16:25 +08:00
use crate::services::block_meta_editor::GridBlockMetaEditorManager;
2022-03-11 21:36:00 +08:00
use bytes::Bytes;
2022-03-16 21:19:51 +08:00
use flowy_collaboration::client_grid::{GridChangeset, GridMetaPad};
2022-03-04 18:11:12 +08:00
use flowy_collaboration::entities::revision::Revision;
use flowy_collaboration::util::make_delta_from_revisions;
use flowy_error::{FlowyError, FlowyResult};
2022-03-05 17:52:25 +08:00
use flowy_grid_data_model::entities::{
2022-03-17 17:25:43 +08:00
Cell, CellMetaChangeset, Field, FieldChangeset, FieldMeta, Grid, GridBlockMeta, GridBlockMetaChangeset,
RepeatedField, RepeatedFieldOrder, RepeatedGridBlock, RepeatedRowOrder, Row, RowMeta, RowMetaChangeset,
2022-03-04 18:11:12 +08:00
};
2022-03-14 17:24:25 +08:00
use std::collections::HashMap;
2022-03-13 23:16:52 +08:00
2022-03-16 21:19:51 +08:00
use crate::dart_notification::{send_dart_notification, GridNotification};
2022-03-14 17:24:25 +08:00
use crate::services::row::{
2022-03-17 17:25:43 +08:00
make_grid_block_from_block_metas, make_grid_blocks, make_row_ids_per_block, row_meta_from_context,
serialize_cell_data, GridBlockMetaDataSnapshot, RowMetaContext, RowMetaContextBuilder,
2022-03-14 17:24:25 +08:00
};
2022-03-13 23:16:52 +08:00
use flowy_sync::{RevisionCloudService, RevisionCompactor, RevisionManager, RevisionObjectBuilder};
2022-03-04 18:11:12 +08:00
use lib_infra::future::FutureResult;
2022-03-12 09:30:13 +08:00
use lib_ot::core::PlainTextAttributes;
2022-03-04 18:11:12 +08:00
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>,
2022-03-17 17:25:43 +08:00
pad: Arc<RwLock<GridMetaPad>>,
2022-03-04 18:11:12 +08:00
rev_manager: Arc<RevisionManager>,
2022-03-11 21:36:00 +08:00
block_meta_manager: Arc<GridBlockMetaEditorManager>,
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,
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 });
2022-03-12 21:06:15 +08:00
let grid_pad = rev_manager.load::<GridPadBuilder>(Some(cloud)).await?;
2022-03-04 18:11:12 +08:00
let rev_manager = Arc::new(rev_manager);
2022-03-17 17:25:43 +08:00
let pad = Arc::new(RwLock::new(grid_pad));
2022-03-12 09:30:13 +08:00
2022-03-17 17:25:43 +08:00
let block_meta_manager =
Arc::new(GridBlockMetaEditorManager::new(grid_id, &user, pad.read().await.get_blocks().clone()).await?);
2022-03-05 17:52:25 +08:00
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,
2022-03-17 17:25:43 +08:00
pad,
2022-03-04 18:11:12 +08:00
rev_manager,
2022-03-11 21:36:00 +08:00
block_meta_manager,
2022-03-04 22:09:16 +08:00
}))
2022-03-04 18:11:12 +08:00
}
2022-03-15 11:07:18 +08:00
pub async fn create_field(&self, field_meta: FieldMeta) -> FlowyResult<()> {
let _ = self.modify(|grid| Ok(grid.create_field(field_meta)?)).await?;
2022-03-16 21:19:51 +08:00
let _ = self.notify_did_update_fields().await?;
2022-03-04 21:26:32 +08:00
Ok(())
}
2022-03-15 19:00:28 +08:00
pub async fn contain_field(&self, field_meta: &FieldMeta) -> bool {
2022-03-17 17:25:43 +08:00
self.pad.read().await.contain_field(&field_meta.id)
2022-03-15 19:00:28 +08:00
}
2022-03-12 22:52:24 +08:00
pub async fn update_field(&self, change: FieldChangeset) -> FlowyResult<()> {
let _ = self.modify(|grid| Ok(grid.update_field(change)?)).await?;
Ok(())
}
2022-03-12 21:06:15 +08:00
pub async fn delete_field(&self, field_id: &str) -> FlowyResult<()> {
2022-03-04 21:26:32 +08:00
let _ = self.modify(|grid| Ok(grid.delete_field(field_id)?)).await?;
Ok(())
}
2022-03-17 17:25:43 +08:00
pub async fn create_block(&self, grid_block: GridBlockMeta) -> FlowyResult<()> {
2022-03-13 11:06:28 +08:00
let _ = self.modify(|grid| Ok(grid.create_block(grid_block)?)).await?;
Ok(())
}
2022-03-17 17:25:43 +08:00
pub async fn update_block(&self, changeset: GridBlockMetaChangeset) -> FlowyResult<()> {
2022-03-14 17:24:25 +08:00
let _ = self.modify(|grid| Ok(grid.update_block(changeset)?)).await?;
2022-03-13 11:06:28 +08:00
Ok(())
}
2022-03-17 17:25:43 +08:00
pub async fn create_row(&self, start_row_id: Option<String>) -> FlowyResult<()> {
let field_metas = self.pad.read().await.get_field_metas(None)?;
let block_id = self.block_id().await?;
// insert empty row below the row whose id is upper_row_id
let row_meta_ctx = RowMetaContextBuilder::new(&field_metas).build();
let row_meta = row_meta_from_context(&block_id, row_meta_ctx);
// insert the row
let row_count = self
.block_meta_manager
2022-03-17 17:25:43 +08:00
.create_row(&block_id, row_meta, start_row_id)
.await?;
// update block row count
2022-03-17 17:25:43 +08:00
let changeset = GridBlockMetaChangeset::from_row_count(&block_id, row_count);
2022-03-14 17:24:25 +08:00
let _ = self.update_block(changeset).await?;
2022-03-16 21:19:51 +08:00
Ok(())
2022-03-14 17:24:25 +08:00
}
2022-03-13 23:16:52 +08:00
pub async fn insert_rows(&self, contexts: Vec<RowMetaContext>) -> FlowyResult<()> {
2022-03-17 17:25:43 +08:00
let block_id = self.block_id().await?;
2022-03-14 17:24:25 +08:00
let mut rows_by_block_id: HashMap<String, Vec<RowMeta>> = HashMap::new();
for ctx in contexts {
let row_meta = row_meta_from_context(&block_id, ctx);
rows_by_block_id
.entry(block_id.clone())
2022-03-16 10:02:37 +08:00
.or_insert_with(Vec::new)
2022-03-14 17:24:25 +08:00
.push(row_meta);
}
2022-03-17 17:25:43 +08:00
let changesets = self.block_meta_manager.insert_row(rows_by_block_id).await?;
2022-03-14 17:24:25 +08:00
for changeset in changesets {
let _ = self.update_block(changeset).await?;
}
2022-03-13 23:16:52 +08:00
Ok(())
2022-03-11 21:36:00 +08:00
}
2022-03-05 17:52:25 +08:00
2022-03-14 17:24:25 +08:00
pub async fn update_row(&self, changeset: RowMetaChangeset) -> FlowyResult<()> {
self.block_meta_manager.update_row(changeset).await
}
pub async fn update_cell(&self, changeset: CellMetaChangeset) -> FlowyResult<()> {
2022-03-16 10:02:37 +08:00
if let Some(cell_data) = changeset.data.as_ref() {
2022-03-17 17:25:43 +08:00
match self.pad.read().await.get_field(&changeset.field_id) {
2022-03-16 10:02:37 +08:00
None => {
return Err(FlowyError::internal()
.context(format!("Can not find the field with id: {}", &changeset.field_id)));
}
Some(field_meta) => {
let _ = serialize_cell_data(cell_data, field_meta)?;
}
}
}
2022-03-16 21:19:51 +08:00
let field_metas = self.get_field_metas(None).await?;
2022-03-14 17:24:25 +08:00
let row_changeset: RowMetaChangeset = changeset.into();
2022-03-16 21:19:51 +08:00
let _ = self
.block_meta_manager
.update_cells(&field_metas, row_changeset)
.await?;
Ok(())
2022-03-05 17:52:25 +08:00
}
2022-03-17 17:25:43 +08:00
pub async fn get_grid_blocks(
&self,
grid_block_metas: Option<Vec<GridBlockMeta>>,
) -> FlowyResult<RepeatedGridBlock> {
let grid_block_meta_snapshots = self.get_grid_block_meta_snapshots(grid_block_metas.as_ref()).await?;
let field_meta = self.pad.read().await.get_field_metas(None)?;
match grid_block_metas {
None => make_grid_blocks(&field_meta, grid_block_meta_snapshots),
Some(grid_block_metas) => {
make_grid_block_from_block_metas(&field_meta, grid_block_metas, grid_block_meta_snapshots)
2022-03-14 17:24:25 +08:00
}
}
2022-03-13 23:16:52 +08:00
}
2022-03-17 17:25:43 +08:00
pub(crate) async fn get_grid_block_meta_snapshots(
&self,
grid_block_infos: Option<&Vec<GridBlockMeta>>,
) -> FlowyResult<Vec<GridBlockMetaDataSnapshot>> {
match grid_block_infos {
2022-03-14 17:24:25 +08:00
None => {
2022-03-17 17:25:43 +08:00
let grid_blocks = self.pad.read().await.get_blocks();
let row_metas_per_block = self
.block_meta_manager
.get_block_meta_snapshot_from_blocks(grid_blocks)
.await?;
Ok(row_metas_per_block)
2022-03-14 17:24:25 +08:00
}
2022-03-17 17:25:43 +08:00
Some(grid_block_infos) => {
let row_metas_per_block = self
.block_meta_manager
.get_block_meta_snapshot_from_row_orders(grid_block_infos)
.await?;
Ok(row_metas_per_block)
2022-03-14 17:24:25 +08:00
}
2022-03-13 11:06:28 +08:00
}
2022-03-14 17:24:25 +08:00
}
2022-03-13 11:06:28 +08:00
2022-03-14 17:24:25 +08:00
pub async fn delete_rows(&self, row_ids: Vec<String>) -> FlowyResult<()> {
let changesets = self.block_meta_manager.delete_rows(row_ids).await?;
for changeset in changesets {
let _ = self.update_block(changeset).await?;
}
2022-03-13 11:06:28 +08:00
Ok(())
2022-03-05 17:52:25 +08:00
}
2022-03-15 11:07:18 +08:00
pub async fn grid_data(&self) -> FlowyResult<Grid> {
2022-03-17 17:25:43 +08:00
let field_orders = self.pad.read().await.get_field_orders();
let block_orders = self.pad.read().await.get_blocks();
2022-03-15 11:07:18 +08:00
Ok(Grid {
id: self.grid_id.clone(),
field_orders,
2022-03-17 17:25:43 +08:00
blocks: block_orders,
2022-03-15 11:07:18 +08:00
})
2022-03-11 21:36:00 +08:00
}
2022-03-15 11:07:18 +08:00
pub async fn get_field_metas(&self, field_orders: Option<RepeatedFieldOrder>) -> FlowyResult<Vec<FieldMeta>> {
2022-03-17 17:25:43 +08:00
let field_meta = self.pad.read().await.get_field_metas(field_orders)?;
2022-03-15 11:07:18 +08:00
Ok(field_meta)
2022-03-05 10:59:44 +08:00
}
2022-03-17 17:25:43 +08:00
pub async fn get_blocks(&self) -> FlowyResult<Vec<GridBlockMeta>> {
let grid_blocks = self.pad.read().await.get_blocks();
2022-03-13 11:06:28 +08:00
Ok(grid_blocks)
}
2022-03-15 19:00:28 +08:00
pub async fn delta_bytes(&self) -> Bytes {
2022-03-17 17:25:43 +08:00
self.pad.read().await.delta_bytes()
2022-03-05 22:30:42 +08:00
}
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-16 21:19:51 +08:00
F: for<'a> FnOnce(&'a mut GridMetaPad) -> FlowyResult<Option<GridChangeset>>,
2022-03-04 18:11:12 +08:00
{
2022-03-17 17:25:43 +08:00
let mut write_guard = self.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(())
}
2022-03-16 21:19:51 +08:00
async fn apply_change(&self, change: GridChangeset) -> FlowyResult<()> {
let GridChangeset { 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();
2022-03-15 19:00:28 +08:00
let delta_data = delta.to_delta_bytes();
2022-03-04 18:11:12 +08:00
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
2022-03-11 21:36:00 +08:00
.add_local_revision(&revision, Box::new(GridRevisionCompactor()))
2022-03-04 18:11:12 +08:00
.await?;
Ok(())
}
2022-03-14 17:24:25 +08:00
2022-03-17 17:25:43 +08:00
async fn block_id(&self) -> FlowyResult<String> {
match self.pad.read().await.get_blocks().last() {
2022-03-14 17:24:25 +08:00
None => Err(FlowyError::internal().context("There is no grid block in this grid")),
2022-03-17 17:25:43 +08:00
Some(grid_block) => Ok(grid_block.block_id.clone()),
2022-03-14 17:24:25 +08:00
}
}
2022-03-16 21:19:51 +08:00
async fn notify_did_update_fields(&self) -> FlowyResult<()> {
let field_metas = self.get_field_metas(None).await?;
let repeated_field: RepeatedField = field_metas.into_iter().map(Field::from).collect::<Vec<_>>().into();
send_dart_notification(&self.grid_id, GridNotification::GridDidUpdateFields)
.payload(repeated_field)
.send();
Ok(())
}
2022-03-04 18:11:12 +08:00
}
2022-03-12 21:06:15 +08:00
#[cfg(feature = "flowy_unit_test")]
impl ClientGridEditor {
pub fn rev_manager(&self) -> Arc<RevisionManager> {
self.rev_manager.clone()
}
}
pub struct GridPadBuilder();
2022-03-04 18:11:12 +08:00
impl RevisionObjectBuilder for GridPadBuilder {
2022-03-10 12:01:31 +08:00
type Output = GridMetaPad;
2022-03-04 18:11:12 +08:00
2022-03-05 10:59:44 +08:00
fn build_object(object_id: &str, revisions: Vec<Revision>) -> FlowyResult<Self::Output> {
2022-03-10 12:01:31 +08:00
let pad = GridMetaPad::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![]) })
}
}
2022-03-11 21:36:00 +08:00
struct GridRevisionCompactor();
impl RevisionCompactor for GridRevisionCompactor {
fn bytes_from_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes> {
let delta = make_delta_from_revisions::<PlainTextAttributes>(revisions)?;
2022-03-15 19:00:28 +08:00
Ok(delta.to_delta_bytes())
2022-03-11 21:36:00 +08:00
}
}