262 lines
9.3 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-14 17:24:25 +08:00
use crate::services::kv_persistence::{GridKVPersistence, KVTransaction};
2022-03-11 21:36:00 +08:00
use bytes::Bytes;
2022-03-10 12:01:31 +08:00
use flowy_collaboration::client_grid::{GridChange, 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-15 11:07:18 +08:00
CellMetaChangeset, Field, FieldChangeset, FieldMeta, Grid, GridBlock, GridBlockChangeset, RepeatedFieldOrder,
2022-03-14 17:24:25 +08:00
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-14 17:24:25 +08:00
use crate::services::row::{
make_row_by_row_id, make_rows, row_meta_from_context, CreateRowContext, CreateRowContextBuilder,
};
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-10 12:01:31 +08:00
grid_meta_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-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 });
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-10 12:01:31 +08:00
let grid_meta_pad = Arc::new(RwLock::new(grid_pad));
2022-03-12 09:30:13 +08:00
let block_meta_manager =
2022-03-13 11:06:28 +08:00
Arc::new(GridBlockMetaEditorManager::new(&user, grid_meta_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-10 12:01:31 +08:00
grid_meta_pad,
2022-03-04 18:11:12 +08:00
rev_manager,
2022-03-11 21:36:00 +08:00
block_meta_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-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-04 21:26:32 +08:00
Ok(())
}
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-13 11:06:28 +08:00
pub async fn create_block(&self, grid_block: GridBlock) -> FlowyResult<()> {
let _ = self.modify(|grid| Ok(grid.create_block(grid_block)?)).await?;
Ok(())
}
2022-03-14 17:24:25 +08:00
pub async fn update_block(&self, changeset: GridBlockChangeset) -> FlowyResult<()> {
let _ = self.modify(|grid| Ok(grid.update_block(changeset)?)).await?;
2022-03-13 11:06:28 +08:00
Ok(())
}
2022-03-12 09:30:13 +08:00
pub async fn create_row(&self) -> FlowyResult<()> {
2022-03-15 11:07:18 +08:00
let field_metas = self.grid_meta_pad.read().await.get_field_metas(None)?;
2022-03-14 17:24:25 +08:00
let block_id = self.last_block_id().await?;
2022-03-15 11:07:18 +08:00
let row = row_meta_from_context(&block_id, CreateRowContextBuilder::new(&field_metas).build());
2022-03-14 17:24:25 +08:00
let row_count = self.block_meta_manager.create_row(row).await?;
let changeset = GridBlockChangeset::from_row_count(&block_id, row_count);
let _ = self.update_block(changeset).await?;
Ok(())
}
2022-03-13 23:16:52 +08:00
2022-03-14 17:24:25 +08:00
pub async fn insert_rows(&self, contexts: Vec<CreateRowContext>) -> FlowyResult<()> {
let block_id = self.last_block_id().await?;
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())
.or_insert(Vec::new())
.push(row_meta);
}
let changesets = self.block_meta_manager.insert_row(rows_by_block_id).await?;
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<()> {
let row_changeset: RowMetaChangeset = changeset.into();
self.update_row(row_changeset).await
2022-03-05 17:52:25 +08:00
}
2022-03-14 17:24:25 +08:00
pub async fn get_rows(&self, row_orders: Option<RepeatedRowOrder>) -> FlowyResult<Vec<Row>> {
2022-03-15 11:07:18 +08:00
let row_metas = self.get_row_metas(row_orders.as_ref()).await?;
let field_meta = self.grid_meta_pad.read().await.get_field_metas(None)?;
2022-03-14 17:24:25 +08:00
match row_orders {
2022-03-15 11:07:18 +08:00
None => Ok(make_rows(&field_meta, row_metas)),
2022-03-14 17:24:25 +08:00
Some(row_orders) => {
2022-03-15 11:07:18 +08:00
let mut row_map: HashMap<String, Row> = make_row_by_row_id(&field_meta, row_metas);
2022-03-14 17:24:25 +08:00
let rows = row_orders
.iter()
.flat_map(|row_order| row_map.remove(&row_order.row_id))
.collect::<Vec<_>>();
Ok(rows)
}
}
2022-03-13 23:16:52 +08:00
}
2022-03-15 11:07:18 +08:00
pub async fn get_row_metas(&self, row_orders: Option<&RepeatedRowOrder>) -> FlowyResult<Vec<Arc<RowMeta>>> {
2022-03-14 17:24:25 +08:00
match row_orders {
None => {
let grid_blocks = self.grid_meta_pad.read().await.get_blocks();
let row_metas = self.block_meta_manager.get_all_rows(grid_blocks).await?;
Ok(row_metas)
}
Some(row_orders) => {
let row_metas = self.block_meta_manager.get_rows(row_orders).await?;
Ok(row_metas)
}
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> {
let field_orders = self.grid_meta_pad.read().await.get_field_orders();
let grid_blocks = self.grid_meta_pad.read().await.get_blocks();
let row_orders = self.block_meta_manager.get_row_orders(grid_blocks).await?;
Ok(Grid {
id: self.grid_id.clone(),
field_orders,
row_orders,
})
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>> {
let field_meta = self.grid_meta_pad.read().await.get_field_metas(field_orders)?;
Ok(field_meta)
2022-03-05 10:59:44 +08:00
}
2022-03-13 11:06:28 +08:00
pub async fn get_blocks(&self) -> FlowyResult<Vec<GridBlock>> {
let grid_blocks = self.grid_meta_pad.read().await.get_blocks();
Ok(grid_blocks)
}
2022-03-05 22:30:42 +08:00
pub async fn delta_str(&self) -> String {
2022-03-10 12:01:31 +08:00
self.grid_meta_pad.read().await.delta_str()
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-10 12:01:31 +08:00
F: for<'a> FnOnce(&'a mut GridMetaPad) -> FlowyResult<Option<GridChange>>,
2022-03-04 18:11:12 +08:00
{
2022-03-10 12:01:31 +08:00
let mut write_guard = self.grid_meta_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
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
async fn last_block_id(&self) -> FlowyResult<String> {
match self.grid_meta_pad.read().await.get_blocks().last() {
None => Err(FlowyError::internal().context("There is no grid block in this grid")),
Some(grid_block) => Ok(grid_block.id.clone()),
}
}
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)?;
Ok(delta.to_bytes())
}
}