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-06 09:03:02 +08:00
|
|
|
|
2022-03-13 11:06:28 +08:00
|
|
|
use crate::services::grid_meta_editor::{ClientGridBlockMetaEditor, GridBlockMetaEditorManager};
|
2022-03-11 21:36:00 +08:00
|
|
|
use bytes::Bytes;
|
2022-03-05 17:52:25 +08:00
|
|
|
use dashmap::DashMap;
|
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-13 11:06:28 +08:00
|
|
|
Field, FieldChangeset, Grid, GridBlock, GridBlockChangeset, RepeatedField, RepeatedFieldOrder, RepeatedRow,
|
|
|
|
RepeatedRowOrder, Row,
|
2022-03-12 09:30:13 +08:00
|
|
|
};
|
|
|
|
use flowy_sync::disk::SQLiteGridBlockMetaRevisionPersistence;
|
|
|
|
use flowy_sync::{
|
|
|
|
RevisionCloudService, RevisionCompactor, RevisionManager, RevisionObjectBuilder, RevisionPersistence,
|
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-12 21:06:15 +08:00
|
|
|
pub async fn create_field(&self, field: Field) -> FlowyResult<()> {
|
2022-03-10 12:01:31 +08:00
|
|
|
let _ = self.modify(|grid| Ok(grid.create_field(field)?)).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(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update_block(&self, change: GridBlockChangeset) -> FlowyResult<()> {
|
|
|
|
let _ = self.modify(|grid| Ok(grid.update_block(change)?)).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-03-12 09:30:13 +08:00
|
|
|
pub async fn create_row(&self) -> FlowyResult<()> {
|
2022-03-13 11:06:28 +08:00
|
|
|
let fields = self.grid_meta_pad.read().await.get_fields(None)?;
|
|
|
|
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) => {
|
|
|
|
let row_count = self.block_meta_manager.create_row(fields, grid_block).await?;
|
|
|
|
let change = GridBlockChangeset::from_row_count(&grid_block.id, row_count);
|
|
|
|
let _ = self.update_block(change).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2022-03-11 21:36:00 +08:00
|
|
|
}
|
2022-03-05 17:52:25 +08:00
|
|
|
|
2022-03-13 11:06:28 +08:00
|
|
|
pub async fn get_rows(&self, row_orders: Option<RepeatedRowOrder>) -> FlowyResult<Vec<Row>> {
|
|
|
|
let fields = self.grid_meta_pad.read().await.get_fields(None)?;
|
|
|
|
let rows = self.block_meta_manager.get_rows(fields, row_orders).await?;
|
|
|
|
Ok(rows)
|
2022-03-05 17:52:25 +08:00
|
|
|
}
|
|
|
|
|
2022-03-13 11:06:28 +08:00
|
|
|
pub async fn delete_rows(&self, row_orders: Option<RepeatedRowOrder>) -> FlowyResult<()> {
|
|
|
|
let row_counts = self.block_meta_manager.delete_rows(row_orders).await?;
|
|
|
|
for (block_id, row_count) in row_counts {
|
|
|
|
let _ = self
|
|
|
|
.update_block(GridBlockChangeset::from_row_count(&block_id, row_count))
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2022-03-05 17:52:25 +08:00
|
|
|
}
|
|
|
|
|
2022-03-05 10:59:44 +08:00
|
|
|
pub async fn grid_data(&self) -> Grid {
|
2022-03-11 21:36:00 +08:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
2022-03-13 11:06:28 +08:00
|
|
|
pub async fn get_fields(&self, field_orders: Option<RepeatedFieldOrder>) -> FlowyResult<Vec<Field>> {
|
2022-03-11 21:36:00 +08:00
|
|
|
let fields = self.grid_meta_pad.read().await.get_fields(field_orders)?;
|
|
|
|
Ok(fields)
|
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-12 21:06:15 +08:00
|
|
|
#[cfg(feature = "flowy_unit_test")]
|
|
|
|
impl ClientGridEditor {
|
|
|
|
pub fn rev_manager(&self) -> Arc<RevisionManager> {
|
|
|
|
self.rev_manager.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-05 17:52:25 +08:00
|
|
|
async fn load_all_fields(
|
2022-03-10 12:01:31 +08:00
|
|
|
grid_pad: &GridMetaPad,
|
2022-03-05 17:52:25 +08:00
|
|
|
kv_persistence: &Arc<GridKVPersistence>,
|
|
|
|
) -> FlowyResult<DashMap<String, Field>> {
|
|
|
|
let field_ids = grid_pad
|
2022-03-10 12:01:31 +08:00
|
|
|
.fields()
|
2022-03-05 17:52:25 +08:00
|
|
|
.iter()
|
2022-03-10 12:01:31 +08:00
|
|
|
.map(|field| field.id.clone())
|
2022-03-05 17:52:25 +08:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let fields = kv_persistence.batch_get::<Field>(field_ids)?;
|
|
|
|
let map = DashMap::new();
|
|
|
|
for field in fields {
|
|
|
|
map.insert(field.id.clone(), field);
|
|
|
|
}
|
|
|
|
Ok(map)
|
|
|
|
}
|
|
|
|
|
2022-03-12 21:06:15 +08:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|