chore: read block rows with filters

This commit is contained in:
appflowy 2022-06-26 15:14:24 +08:00
parent b73c68c6f6
commit 7504ea7555
10 changed files with 105 additions and 104 deletions

View File

@ -9,7 +9,7 @@ use flowy_grid_data_model::entities::{
CellChangeset, GridRowsChangeset, IndexRowOrder, Row, RowOrder, UpdatedRowOrder, CellChangeset, GridRowsChangeset, IndexRowOrder, Row, RowOrder, UpdatedRowOrder,
}; };
use flowy_grid_data_model::revision::{ use flowy_grid_data_model::revision::{
CellRevision, GridBlockRevision, GridBlockRevisionChangeset, RowMetaChangeset, RowRevision, CellRevision, GridBlockMetaRevision, GridBlockMetaRevisionChangeset, RowMetaChangeset, RowRevision,
}; };
use flowy_revision::disk::SQLiteGridBlockMetaRevisionPersistence; use flowy_revision::disk::SQLiteGridBlockMetaRevisionPersistence;
use flowy_revision::{RevisionManager, RevisionPersistence}; use flowy_revision::{RevisionManager, RevisionPersistence};
@ -30,7 +30,7 @@ impl GridBlockManager {
pub(crate) async fn new( pub(crate) async fn new(
grid_id: &str, grid_id: &str,
user: &Arc<dyn GridUser>, user: &Arc<dyn GridUser>,
block_revs: Vec<Arc<GridBlockRevision>>, block_revs: Vec<Arc<GridBlockMetaRevision>>,
persistence: Arc<BlockIndexCache>, persistence: Arc<BlockIndexCache>,
) -> FlowyResult<Self> { ) -> FlowyResult<Self> {
let editor_map = make_block_meta_editor_map(user, block_revs).await?; let editor_map = make_block_meta_editor_map(user, block_revs).await?;
@ -86,7 +86,7 @@ impl GridBlockManager {
pub(crate) async fn insert_row( pub(crate) async fn insert_row(
&self, &self,
rows_by_block_id: HashMap<String, Vec<RowRevision>>, rows_by_block_id: HashMap<String, Vec<RowRevision>>,
) -> FlowyResult<Vec<GridBlockRevisionChangeset>> { ) -> FlowyResult<Vec<GridBlockMetaRevisionChangeset>> {
let mut changesets = vec![]; let mut changesets = vec![];
for (block_id, row_revs) in rows_by_block_id { for (block_id, row_revs) in rows_by_block_id {
let mut inserted_row_orders = vec![]; let mut inserted_row_orders = vec![];
@ -100,7 +100,7 @@ impl GridBlockManager {
row_order.index = index; row_order.index = index;
inserted_row_orders.push(row_order); inserted_row_orders.push(row_order);
} }
changesets.push(GridBlockRevisionChangeset::from_row_count(&block_id, row_count)); changesets.push(GridBlockMetaRevisionChangeset::from_row_count(&block_id, row_count));
let _ = self let _ = self
.notify_did_update_block(&block_id, GridRowsChangeset::insert(&block_id, inserted_row_orders)) .notify_did_update_block(&block_id, GridRowsChangeset::insert(&block_id, inserted_row_orders))
@ -148,7 +148,10 @@ impl GridBlockManager {
Ok(()) Ok(())
} }
pub(crate) async fn delete_rows(&self, row_orders: Vec<RowOrder>) -> FlowyResult<Vec<GridBlockRevisionChangeset>> { pub(crate) async fn delete_rows(
&self,
row_orders: Vec<RowOrder>,
) -> FlowyResult<Vec<GridBlockMetaRevisionChangeset>> {
let mut changesets = vec![]; let mut changesets = vec![];
for block_order in block_from_row_orders(row_orders) { for block_order in block_from_row_orders(row_orders) {
let editor = self.get_editor(&block_order.id).await?; let editor = self.get_editor(&block_order.id).await?;
@ -158,7 +161,7 @@ impl GridBlockManager {
.map(|row_order| Cow::Owned(row_order.row_id)) .map(|row_order| Cow::Owned(row_order.row_id))
.collect::<Vec<Cow<String>>>(); .collect::<Vec<Cow<String>>>();
let row_count = editor.delete_rows(row_ids).await?; let row_count = editor.delete_rows(row_ids).await?;
let changeset = GridBlockRevisionChangeset::from_row_count(&block_order.id, row_count); let changeset = GridBlockMetaRevisionChangeset::from_row_count(&block_order.id, row_count);
changesets.push(changeset); changesets.push(changeset);
} }
@ -262,7 +265,7 @@ impl GridBlockManager {
async fn make_block_meta_editor_map( async fn make_block_meta_editor_map(
user: &Arc<dyn GridUser>, user: &Arc<dyn GridUser>,
block_revs: Vec<Arc<GridBlockRevision>>, block_revs: Vec<Arc<GridBlockMetaRevision>>,
) -> FlowyResult<DashMap<String, Arc<GridBlockRevisionEditor>>> { ) -> FlowyResult<DashMap<String, Arc<GridBlockRevisionEditor>>> {
let editor_map = DashMap::new(); let editor_map = DashMap::new();
for block_rev in block_revs { for block_rev in block_revs {

View File

@ -1,7 +1,7 @@
use bytes::Bytes; use bytes::Bytes;
use flowy_error::{FlowyError, FlowyResult}; use flowy_error::{FlowyError, FlowyResult};
use flowy_grid_data_model::entities::RowOrder; use flowy_grid_data_model::entities::RowOrder;
use flowy_grid_data_model::revision::{CellRevision, GridBlockRevisionData, RowMetaChangeset, RowRevision}; use flowy_grid_data_model::revision::{CellRevision, GridBlockRevision, RowMetaChangeset, RowRevision};
use flowy_revision::{RevisionCloudService, RevisionCompactor, RevisionManager, RevisionObjectBuilder}; use flowy_revision::{RevisionCloudService, RevisionCompactor, RevisionManager, RevisionObjectBuilder};
use flowy_sync::client_grid::{GridBlockMetaChange, GridBlockRevisionPad}; use flowy_sync::client_grid::{GridBlockMetaChange, GridBlockRevisionPad};
use flowy_sync::entities::revision::Revision; use flowy_sync::entities::revision::Revision;
@ -42,7 +42,7 @@ impl GridBlockRevisionEditor {
}) })
} }
pub async fn duplicate_block(&self, duplicated_block_id: &str) -> GridBlockRevisionData { pub async fn duplicate_block(&self, duplicated_block_id: &str) -> GridBlockRevision {
self.pad.read().await.duplicate_data(duplicated_block_id).await self.pad.read().await.duplicate_data(duplicated_block_id).await
} }

View File

@ -46,7 +46,7 @@ impl GridRevisionEditor {
let grid_pad = rev_manager.load::<GridPadBuilder>(Some(cloud)).await?; let grid_pad = rev_manager.load::<GridPadBuilder>(Some(cloud)).await?;
let rev_manager = Arc::new(rev_manager); let rev_manager = Arc::new(rev_manager);
let grid_pad = Arc::new(RwLock::new(grid_pad)); let grid_pad = Arc::new(RwLock::new(grid_pad));
let block_revs = grid_pad.read().await.get_block_revs(); let block_revs = grid_pad.read().await.get_block_meta_revs();
let block_meta_manager = Arc::new(GridBlockManager::new(grid_id, &user, block_revs, persistence).await?); let block_meta_manager = Arc::new(GridBlockManager::new(grid_id, &user, block_revs, persistence).await?);
Ok(Arc::new(Self { Ok(Arc::new(Self {
@ -243,14 +243,14 @@ impl GridRevisionEditor {
Ok(field_revs) Ok(field_revs)
} }
pub async fn create_block(&self, grid_block: GridBlockRevision) -> FlowyResult<()> { pub async fn create_block(&self, block_meta_rev: GridBlockMetaRevision) -> FlowyResult<()> {
let _ = self let _ = self
.modify(|grid_pad| Ok(grid_pad.create_block_rev(grid_block)?)) .modify(|grid_pad| Ok(grid_pad.create_block_meta_rev(block_meta_rev)?))
.await?; .await?;
Ok(()) Ok(())
} }
pub async fn update_block(&self, changeset: GridBlockRevisionChangeset) -> FlowyResult<()> { pub async fn update_block(&self, changeset: GridBlockMetaRevisionChangeset) -> FlowyResult<()> {
let _ = self let _ = self
.modify(|grid_pad| Ok(grid_pad.update_block_rev(changeset)?)) .modify(|grid_pad| Ok(grid_pad.update_block_rev(changeset)?))
.await?; .await?;
@ -270,7 +270,7 @@ impl GridRevisionEditor {
let row_count = self.block_manager.create_row(&block_id, row_rev, start_row_id).await?; let row_count = self.block_manager.create_row(&block_id, row_rev, start_row_id).await?;
// update block row count // update block row count
let changeset = GridBlockRevisionChangeset::from_row_count(&block_id, row_count); let changeset = GridBlockMetaRevisionChangeset::from_row_count(&block_id, row_count);
let _ = self.update_block(changeset).await?; let _ = self.update_block(changeset).await?;
Ok(row_order) Ok(row_order)
} }
@ -408,9 +408,9 @@ impl GridRevisionEditor {
make_grid_blocks(block_ids, block_snapshots) make_grid_blocks(block_ids, block_snapshots)
} }
pub async fn get_block_metas(&self) -> FlowyResult<Vec<Arc<GridBlockRevision>>> { pub async fn get_block_meta_revs(&self) -> FlowyResult<Vec<Arc<GridBlockMetaRevision>>> {
let grid_blocks = self.grid_pad.read().await.get_block_revs(); let block_meta_revs = self.grid_pad.read().await.get_block_meta_revs();
Ok(grid_blocks) Ok(block_meta_revs)
} }
pub async fn delete_rows(&self, row_orders: Vec<RowOrder>) -> FlowyResult<()> { pub async fn delete_rows(&self, row_orders: Vec<RowOrder>) -> FlowyResult<()> {
@ -425,7 +425,7 @@ impl GridRevisionEditor {
let pad_read_guard = self.grid_pad.read().await; let pad_read_guard = self.grid_pad.read().await;
let field_orders = pad_read_guard.get_field_orders(); let field_orders = pad_read_guard.get_field_orders();
let mut block_orders = vec![]; let mut block_orders = vec![];
for block_rev in pad_read_guard.get_block_revs() { for block_rev in pad_read_guard.get_block_meta_revs() {
let row_orders = self.block_manager.get_row_orders(&block_rev.block_id).await?; let row_orders = self.block_manager.get_row_orders(&block_rev.block_id).await?;
let block_order = GridBlock { let block_order = GridBlock {
id: block_rev.block_id.clone(), id: block_rev.block_id.clone(),
@ -469,7 +469,7 @@ impl GridRevisionEditor {
.grid_pad .grid_pad
.read() .read()
.await .await
.get_block_revs() .get_block_meta_revs()
.iter() .iter()
.map(|block_rev| block_rev.block_id.clone()) .map(|block_rev| block_rev.block_id.clone())
.collect::<Vec<String>>(), .collect::<Vec<String>>(),
@ -519,8 +519,8 @@ impl GridRevisionEditor {
pub async fn duplicate_grid(&self) -> FlowyResult<BuildGridContext> { pub async fn duplicate_grid(&self) -> FlowyResult<BuildGridContext> {
let grid_pad = self.grid_pad.read().await; let grid_pad = self.grid_pad.read().await;
let original_blocks = grid_pad.get_block_revs(); let original_blocks = grid_pad.get_block_meta_revs();
let (duplicated_fields, duplicated_blocks) = grid_pad.duplicate_grid_meta().await; let (duplicated_fields, duplicated_blocks) = grid_pad.duplicate_grid_block_meta().await;
let mut blocks_meta_data = vec![]; let mut blocks_meta_data = vec![];
if original_blocks.len() == duplicated_blocks.len() { if original_blocks.len() == duplicated_blocks.len() {
@ -576,7 +576,7 @@ impl GridRevisionEditor {
} }
async fn block_id(&self) -> FlowyResult<String> { async fn block_id(&self) -> FlowyResult<String> {
match self.grid_pad.read().await.get_block_revs().last() { match self.grid_pad.read().await.get_block_meta_revs().last() {
None => Err(FlowyError::internal().context("There is no grid block in this grid")), None => Err(FlowyError::internal().context("There is no grid block in this grid")),
Some(grid_block) => Ok(grid_block.block_id.clone()), Some(grid_block) => Ok(grid_block.block_id.clone()),
} }

View File

@ -220,6 +220,14 @@ where
} }
} }
/// The data is encoded by protobuf or utf8. You should choose the corresponding decode struct to parse it.
///
/// For example:
///
/// * Use DateCellData to parse the data when the FieldType is Date.
/// * Use URLCellData to parse the data when the FieldType is URL.
/// * Use String to parse the data when the FieldType is RichText, Number, or Checkbox.
/// * Check out the implementation of CellDataOperation trait for more information.
#[derive(Default)] #[derive(Default)]
pub struct DecodedCellData { pub struct DecodedCellData {
pub data: Vec<u8>, pub data: Vec<u8>,

View File

@ -1,13 +1,13 @@
use crate::grid::script::EditorScript::*; use crate::grid::script::EditorScript::*;
use crate::grid::script::*; use crate::grid::script::*;
use flowy_grid_data_model::revision::{GridBlockRevision, GridBlockRevisionChangeset}; use flowy_grid_data_model::revision::{GridBlockMetaRevision, GridBlockMetaRevisionChangeset};
#[tokio::test] #[tokio::test]
async fn grid_create_block() { async fn grid_create_block() {
let grid_block = GridBlockRevision::new(); let block_meta_rev = GridBlockMetaRevision::new();
let scripts = vec![ let scripts = vec![
AssertBlockCount(1), AssertBlockCount(1),
CreateBlock { block: grid_block }, CreateBlock { block: block_meta_rev },
AssertBlockCount(2), AssertBlockCount(2),
]; ];
GridEditorTest::new().await.run_scripts(scripts).await; GridEditorTest::new().await.run_scripts(scripts).await;
@ -15,10 +15,10 @@ async fn grid_create_block() {
#[tokio::test] #[tokio::test]
async fn grid_update_block() { async fn grid_update_block() {
let grid_block = GridBlockRevision::new(); let block_meta_rev = GridBlockMetaRevision::new();
let mut cloned_grid_block = grid_block.clone(); let mut cloned_grid_block = block_meta_rev.clone();
let changeset = GridBlockRevisionChangeset { let changeset = GridBlockMetaRevisionChangeset {
block_id: grid_block.block_id.clone(), block_id: block_meta_rev.block_id.clone(),
start_row_index: Some(2), start_row_index: Some(2),
row_count: Some(10), row_count: Some(10),
}; };
@ -28,7 +28,7 @@ async fn grid_update_block() {
let scripts = vec![ let scripts = vec![
AssertBlockCount(1), AssertBlockCount(1),
CreateBlock { block: grid_block }, CreateBlock { block: block_meta_rev },
UpdateBlock { changeset }, UpdateBlock { changeset },
AssertBlockCount(2), AssertBlockCount(2),
AssertBlockEqual { AssertBlockEqual {

View File

@ -31,10 +31,10 @@ pub enum EditorScript {
field_rev: FieldRevision, field_rev: FieldRevision,
}, },
CreateBlock { CreateBlock {
block: GridBlockRevision, block: GridBlockMetaRevision,
}, },
UpdateBlock { UpdateBlock {
changeset: GridBlockRevisionChangeset, changeset: GridBlockMetaRevisionChangeset,
}, },
AssertBlockCount(usize), AssertBlockCount(usize),
AssertBlock { AssertBlock {
@ -44,7 +44,7 @@ pub enum EditorScript {
}, },
AssertBlockEqual { AssertBlockEqual {
block_index: usize, block_index: usize,
block: GridBlockRevision, block: GridBlockMetaRevision,
}, },
CreateEmptyRow, CreateEmptyRow,
CreateRow { CreateRow {
@ -87,7 +87,7 @@ pub struct GridEditorTest {
pub grid_id: String, pub grid_id: String,
pub editor: Arc<GridRevisionEditor>, pub editor: Arc<GridRevisionEditor>,
pub field_revs: Vec<FieldRevision>, pub field_revs: Vec<FieldRevision>,
pub grid_block_revs: Vec<GridBlockRevision>, pub grid_block_revs: Vec<GridBlockMetaRevision>,
pub row_revs: Vec<Arc<RowRevision>>, pub row_revs: Vec<Arc<RowRevision>>,
pub field_count: usize, pub field_count: usize,
@ -103,7 +103,7 @@ impl GridEditorTest {
let test = ViewTest::new_grid_view(&sdk, view_data.to_vec()).await; let test = ViewTest::new_grid_view(&sdk, view_data.to_vec()).await;
let editor = sdk.grid_manager.open_grid(&test.view.id).await.unwrap(); let editor = sdk.grid_manager.open_grid(&test.view.id).await.unwrap();
let field_revs = editor.get_field_revs::<FieldOrder>(None).await.unwrap(); let field_revs = editor.get_field_revs::<FieldOrder>(None).await.unwrap();
let grid_blocks = editor.get_block_metas().await.unwrap(); let grid_blocks = editor.get_block_meta_revs().await.unwrap();
let row_revs = editor.grid_block_snapshots(None).await.unwrap().pop().unwrap().row_revs; let row_revs = editor.grid_block_snapshots(None).await.unwrap().pop().unwrap().row_revs;
assert_eq!(row_revs.len(), 3); assert_eq!(row_revs.len(), 3);
assert_eq!(grid_blocks.len(), 1); assert_eq!(grid_blocks.len(), 1);
@ -172,13 +172,13 @@ impl GridEditorTest {
} }
EditorScript::CreateBlock { block } => { EditorScript::CreateBlock { block } => {
self.editor.create_block(block).await.unwrap(); self.editor.create_block(block).await.unwrap();
self.grid_block_revs = self.editor.get_block_metas().await.unwrap(); self.grid_block_revs = self.editor.get_block_meta_revs().await.unwrap();
} }
EditorScript::UpdateBlock { changeset: change } => { EditorScript::UpdateBlock { changeset: change } => {
self.editor.update_block(change).await.unwrap(); self.editor.update_block(change).await.unwrap();
} }
EditorScript::AssertBlockCount(count) => { EditorScript::AssertBlockCount(count) => {
assert_eq!(self.editor.get_block_metas().await.unwrap().len(), count); assert_eq!(self.editor.get_block_meta_revs().await.unwrap().len(), count);
} }
EditorScript::AssertBlock { EditorScript::AssertBlock {
block_index, block_index,
@ -189,7 +189,7 @@ impl GridEditorTest {
assert_eq!(self.grid_block_revs[block_index].start_row_index, start_row_index); assert_eq!(self.grid_block_revs[block_index].start_row_index, start_row_index);
} }
EditorScript::AssertBlockEqual { block_index, block } => { EditorScript::AssertBlockEqual { block_index, block } => {
let blocks = self.editor.get_block_metas().await.unwrap(); let blocks = self.editor.get_block_meta_revs().await.unwrap();
let compared_block = blocks[block_index].clone(); let compared_block = blocks[block_index].clone();
assert_eq!(compared_block, block); assert_eq!(compared_block, block);
} }
@ -197,7 +197,7 @@ impl GridEditorTest {
let row_order = self.editor.create_row(None).await.unwrap(); let row_order = self.editor.create_row(None).await.unwrap();
self.row_order_by_row_id.insert(row_order.row_id.clone(), row_order); self.row_order_by_row_id.insert(row_order.row_id.clone(), row_order);
self.row_revs = self.get_row_revs().await; self.row_revs = self.get_row_revs().await;
self.grid_block_revs = self.editor.get_block_metas().await.unwrap(); self.grid_block_revs = self.editor.get_block_meta_revs().await.unwrap();
} }
EditorScript::CreateRow { payload: context } => { EditorScript::CreateRow { payload: context } => {
let row_orders = self.editor.insert_rows(vec![context]).await.unwrap(); let row_orders = self.editor.insert_rows(vec![context]).await.unwrap();
@ -205,7 +205,7 @@ impl GridEditorTest {
self.row_order_by_row_id.insert(row_order.row_id.clone(), row_order); self.row_order_by_row_id.insert(row_order.row_id.clone(), row_order);
} }
self.row_revs = self.get_row_revs().await; self.row_revs = self.get_row_revs().await;
self.grid_block_revs = self.editor.get_block_metas().await.unwrap(); self.grid_block_revs = self.editor.get_block_meta_revs().await.unwrap();
} }
EditorScript::UpdateRow { changeset: change } => self.editor.update_row(change).await.unwrap(), EditorScript::UpdateRow { changeset: change } => self.editor.update_row(change).await.unwrap(),
EditorScript::DeleteRows { row_ids } => { EditorScript::DeleteRows { row_ids } => {
@ -216,7 +216,7 @@ impl GridEditorTest {
self.editor.delete_rows(row_orders).await.unwrap(); self.editor.delete_rows(row_orders).await.unwrap();
self.row_revs = self.get_row_revs().await; self.row_revs = self.get_row_revs().await;
self.grid_block_revs = self.editor.get_block_metas().await.unwrap(); self.grid_block_revs = self.editor.get_block_meta_revs().await.unwrap();
} }
EditorScript::AssertRow { expected_row } => { EditorScript::AssertRow { expected_row } => {
let row = &*self let row = &*self

View File

@ -30,7 +30,7 @@ pub fn gen_field_id() -> String {
pub struct GridRevision { pub struct GridRevision {
pub grid_id: String, pub grid_id: String,
pub fields: Vec<FieldRevision>, pub fields: Vec<FieldRevision>,
pub blocks: Vec<Arc<GridBlockRevision>>, pub blocks: Vec<Arc<GridBlockMetaRevision>>,
#[serde(default)] #[serde(default)]
pub setting: GridSettingRevision, pub setting: GridSettingRevision,
@ -57,13 +57,13 @@ impl GridRevision {
} }
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct GridBlockRevision { pub struct GridBlockMetaRevision {
pub block_id: String, pub block_id: String,
pub start_row_index: i32, pub start_row_index: i32,
pub row_count: i32, pub row_count: i32,
} }
impl GridBlockRevision { impl GridBlockMetaRevision {
pub fn len(&self) -> i32 { pub fn len(&self) -> i32 {
self.row_count self.row_count
} }
@ -73,22 +73,22 @@ impl GridBlockRevision {
} }
} }
impl GridBlockRevision { impl GridBlockMetaRevision {
pub fn new() -> Self { pub fn new() -> Self {
GridBlockRevision { GridBlockMetaRevision {
block_id: gen_block_id(), block_id: gen_block_id(),
..Default::default() ..Default::default()
} }
} }
} }
pub struct GridBlockRevisionChangeset { pub struct GridBlockMetaRevisionChangeset {
pub block_id: String, pub block_id: String,
pub start_row_index: Option<i32>, pub start_row_index: Option<i32>,
pub row_count: Option<i32>, pub row_count: Option<i32>,
} }
impl GridBlockRevisionChangeset { impl GridBlockMetaRevisionChangeset {
pub fn from_row_count(block_id: &str, row_count: i32) -> Self { pub fn from_row_count(block_id: &str, row_count: i32) -> Self {
Self { Self {
block_id: block_id.to_string(), block_id: block_id.to_string(),
@ -99,9 +99,9 @@ impl GridBlockRevisionChangeset {
} }
#[derive(Debug, Clone, Default, Serialize, Deserialize)] #[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct GridBlockRevisionData { pub struct GridBlockRevision {
pub block_id: String, pub block_id: String,
pub rows: Vec<RowRevision>, pub rows: Vec<Arc<RowRevision>>,
} }
#[derive(Debug, Clone, Default, Serialize, Deserialize, Eq, PartialEq)] #[derive(Debug, Clone, Default, Serialize, Deserialize, Eq, PartialEq)]
@ -292,8 +292,8 @@ impl CellRevision {
#[derive(Clone, Default, Deserialize, Serialize)] #[derive(Clone, Default, Deserialize, Serialize)]
pub struct BuildGridContext { pub struct BuildGridContext {
pub field_revs: Vec<FieldRevision>, pub field_revs: Vec<FieldRevision>,
pub blocks: Vec<GridBlockRevision>, pub blocks: Vec<GridBlockMetaRevision>,
pub blocks_meta_data: Vec<GridBlockRevisionData>, pub blocks_meta_data: Vec<GridBlockRevision>,
} }
impl BuildGridContext { impl BuildGridContext {

View File

@ -2,40 +2,37 @@ use crate::entities::revision::{md5, RepeatedRevision, Revision};
use crate::errors::{CollaborateError, CollaborateResult}; use crate::errors::{CollaborateError, CollaborateResult};
use crate::util::{cal_diff, make_delta_from_revisions}; use crate::util::{cal_diff, make_delta_from_revisions};
use flowy_grid_data_model::revision::{ use flowy_grid_data_model::revision::{
gen_block_id, gen_row_id, CellRevision, GridBlockRevisionData, RowMetaChangeset, RowRevision, gen_block_id, gen_row_id, CellRevision, GridBlockRevision, RowMetaChangeset, RowRevision,
}; };
use lib_ot::core::{OperationTransformable, PlainTextAttributes, PlainTextDelta, PlainTextDeltaBuilder}; use lib_ot::core::{OperationTransformable, PlainTextAttributes, PlainTextDelta, PlainTextDeltaBuilder};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::borrow::Cow; use std::borrow::Cow;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
pub type GridBlockRevisionDelta = PlainTextDelta; pub type GridBlockRevisionDelta = PlainTextDelta;
pub type GridBlockRevisionDeltaBuilder = PlainTextDeltaBuilder; pub type GridBlockRevisionDeltaBuilder = PlainTextDeltaBuilder;
#[derive(Debug, Deserialize, Serialize, Clone)] #[derive(Debug, Clone)]
pub struct GridBlockRevisionPad { pub struct GridBlockRevisionPad {
block_id: String, block_revision: GridBlockRevision,
rows: Vec<Arc<RowRevision>>,
#[serde(skip)]
pub(crate) delta: GridBlockRevisionDelta, pub(crate) delta: GridBlockRevisionDelta,
} }
impl GridBlockRevisionPad { impl GridBlockRevisionPad {
pub async fn duplicate_data(&self, duplicated_block_id: &str) -> GridBlockRevisionData { pub async fn duplicate_data(&self, duplicated_block_id: &str) -> GridBlockRevision {
let duplicated_rows = self let duplicated_rows = self
.block_revision
.rows .rows
.iter() .iter()
.map(|row| { .map(|row| {
let mut duplicated_row = row.as_ref().clone(); let mut duplicated_row = row.as_ref().clone();
duplicated_row.id = gen_row_id(); duplicated_row.id = gen_row_id();
duplicated_row.block_id = duplicated_block_id.to_string(); duplicated_row.block_id = duplicated_block_id.to_string();
duplicated_row Arc::new(duplicated_row)
}) })
.collect::<Vec<RowRevision>>(); .collect::<Vec<Arc<RowRevision>>>();
GridBlockRevisionData { GridBlockRevision {
block_id: duplicated_block_id.to_string(), block_id: duplicated_block_id.to_string(),
rows: duplicated_rows, rows: duplicated_rows,
} }
@ -43,18 +40,12 @@ impl GridBlockRevisionPad {
pub fn from_delta(delta: GridBlockRevisionDelta) -> CollaborateResult<Self> { pub fn from_delta(delta: GridBlockRevisionDelta) -> CollaborateResult<Self> {
let s = delta.to_str()?; let s = delta.to_str()?;
let meta_data: GridBlockRevisionData = serde_json::from_str(&s).map_err(|e| { let block_revision: GridBlockRevision = serde_json::from_str(&s).map_err(|e| {
let msg = format!("Deserialize delta to block meta failed: {}", e); let msg = format!("Deserialize delta to block meta failed: {}", e);
tracing::error!("{}", s); tracing::error!("{}", s);
CollaborateError::internal().context(msg) CollaborateError::internal().context(msg)
})?; })?;
let block_id = meta_data.block_id; Ok(Self { block_revision, delta })
let rows = meta_data
.rows
.into_iter()
.map(Arc::new)
.collect::<Vec<Arc<RowRevision>>>();
Ok(Self { block_id, rows, delta })
} }
pub fn from_revisions(_grid_id: &str, revisions: Vec<Revision>) -> CollaborateResult<Self> { pub fn from_revisions(_grid_id: &str, revisions: Vec<Revision>) -> CollaborateResult<Self> {
@ -95,9 +86,10 @@ impl GridBlockRevisionPad {
T: AsRef<str> + ToOwned + ?Sized, T: AsRef<str> + ToOwned + ?Sized,
{ {
match row_ids { match row_ids {
None => Ok(self.rows.to_vec()), None => Ok(self.block_revision.rows.clone()),
Some(row_ids) => { Some(row_ids) => {
let row_map = self let row_map = self
.block_revision
.rows .rows
.iter() .iter()
.map(|row| (row.id.as_str(), row.clone())) .map(|row| (row.id.as_str(), row.clone()))
@ -137,11 +129,12 @@ impl GridBlockRevisionPad {
} }
pub fn number_of_rows(&self) -> i32 { pub fn number_of_rows(&self) -> i32 {
self.rows.len() as i32 self.block_revision.rows.len() as i32
} }
pub fn index_of_row(&self, row_id: &str) -> Option<i32> { pub fn index_of_row(&self, row_id: &str) -> Option<i32> {
self.rows self.block_revision
.rows
.iter() .iter()
.position(|row| row.id == row_id) .position(|row| row.id == row_id)
.map(|index| index as i32) .map(|index| index as i32)
@ -190,7 +183,7 @@ impl GridBlockRevisionPad {
F: for<'a> FnOnce(&'a mut Vec<Arc<RowRevision>>) -> CollaborateResult<Option<()>>, F: for<'a> FnOnce(&'a mut Vec<Arc<RowRevision>>) -> CollaborateResult<Option<()>>,
{ {
let cloned_self = self.clone(); let cloned_self = self.clone();
match f(&mut self.rows)? { match f(&mut self.block_revision.rows)? {
None => Ok(None), None => Ok(None),
Some(_) => { Some(_) => {
let old = cloned_self.to_json()?; let old = cloned_self.to_json()?;
@ -226,7 +219,7 @@ impl GridBlockRevisionPad {
} }
pub fn to_json(&self) -> CollaborateResult<String> { pub fn to_json(&self) -> CollaborateResult<String> {
serde_json::to_string(self) serde_json::to_string(&self.block_revision)
.map_err(|e| CollaborateError::internal().context(format!("serial trash to json failed: {}", e))) .map_err(|e| CollaborateError::internal().context(format!("serial trash to json failed: {}", e)))
} }
@ -245,12 +238,12 @@ pub struct GridBlockMetaChange {
pub md5: String, pub md5: String,
} }
pub fn make_block_meta_delta(grid_block_meta_data: &GridBlockRevisionData) -> GridBlockRevisionDelta { pub fn make_block_meta_delta(block_rev: &GridBlockRevision) -> GridBlockRevisionDelta {
let json = serde_json::to_string(&grid_block_meta_data).unwrap(); let json = serde_json::to_string(&block_rev).unwrap();
PlainTextDeltaBuilder::new().insert(&json).build() PlainTextDeltaBuilder::new().insert(&json).build()
} }
pub fn make_block_meta_revisions(user_id: &str, grid_block_meta_data: &GridBlockRevisionData) -> RepeatedRevision { pub fn make_block_meta_revisions(user_id: &str, grid_block_meta_data: &GridBlockRevision) -> RepeatedRevision {
let delta = make_block_meta_delta(grid_block_meta_data); let delta = make_block_meta_delta(grid_block_meta_data);
let bytes = delta.to_delta_bytes(); let bytes = delta.to_delta_bytes();
let revision = Revision::initial_revision(user_id, &grid_block_meta_data.block_id, bytes); let revision = Revision::initial_revision(user_id, &grid_block_meta_data.block_id, bytes);
@ -259,17 +252,13 @@ pub fn make_block_meta_revisions(user_id: &str, grid_block_meta_data: &GridBlock
impl std::default::Default for GridBlockRevisionPad { impl std::default::Default for GridBlockRevisionPad {
fn default() -> Self { fn default() -> Self {
let block_meta_data = GridBlockRevisionData { let block_revision = GridBlockRevision {
block_id: gen_block_id(), block_id: gen_block_id(),
rows: vec![], rows: vec![],
}; };
let delta = make_block_meta_delta(&block_meta_data); let delta = make_block_meta_delta(&block_revision);
GridBlockRevisionPad { GridBlockRevisionPad { block_revision, delta }
block_id: block_meta_data.block_id,
rows: block_meta_data.rows.into_iter().map(Arc::new).collect::<Vec<_>>(),
delta,
}
} }
} }

View File

@ -1,7 +1,8 @@
use crate::errors::{CollaborateError, CollaborateResult}; use crate::errors::{CollaborateError, CollaborateResult};
use flowy_grid_data_model::revision::{ use flowy_grid_data_model::revision::{
BuildGridContext, FieldRevision, GridBlockRevision, GridBlockRevisionData, RowRevision, BuildGridContext, FieldRevision, GridBlockMetaRevision, GridBlockRevision, RowRevision,
}; };
use std::sync::Arc;
pub struct GridBuilder { pub struct GridBuilder {
build_context: BuildGridContext, build_context: BuildGridContext,
@ -11,8 +12,8 @@ impl std::default::Default for GridBuilder {
fn default() -> Self { fn default() -> Self {
let mut build_context = BuildGridContext::new(); let mut build_context = BuildGridContext::new();
let block_meta = GridBlockRevision::new(); let block_meta = GridBlockMetaRevision::new();
let block_meta_data = GridBlockRevisionData { let block_meta_data = GridBlockRevision {
block_id: block_meta.block_id.clone(), block_id: block_meta.block_id.clone(),
rows: vec![], rows: vec![],
}; };
@ -32,10 +33,10 @@ impl GridBuilder {
pub fn add_empty_row(mut self) -> Self { pub fn add_empty_row(mut self) -> Self {
let row = RowRevision::new(&self.build_context.blocks.first().unwrap().block_id); let row = RowRevision::new(&self.build_context.blocks.first().unwrap().block_id);
let block_meta = self.build_context.blocks.first_mut().unwrap(); let block_meta_rev = self.build_context.blocks.first_mut().unwrap();
let block_meta_data = self.build_context.blocks_meta_data.first_mut().unwrap(); let block_rev = self.build_context.blocks_meta_data.first_mut().unwrap();
block_meta_data.rows.push(row); block_rev.rows.push(Arc::new(row));
block_meta.row_count += 1; block_meta_rev.row_count += 1;
self self
} }
@ -59,10 +60,10 @@ fn check_rows(fields: &[FieldRevision], rows: &[RowRevision]) -> CollaborateResu
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::client_grid::{make_block_meta_delta, make_grid_delta, GridBuilder}; use crate::client_grid::{make_block_meta_delta, make_grid_delta, GridBuilder};
use flowy_grid_data_model::entities::FieldType; use flowy_grid_data_model::entities::FieldType;
use flowy_grid_data_model::revision::{FieldRevision, GridBlockRevisionData, GridRevision}; use flowy_grid_data_model::revision::{FieldRevision, GridBlockRevision, GridRevision};
use std::sync::Arc;
#[test] #[test]
fn create_default_grid_test() { fn create_default_grid_test() {
@ -78,7 +79,7 @@ mod tests {
let grid_rev = GridRevision { let grid_rev = GridRevision {
grid_id, grid_id,
fields: build_context.field_revs, fields: build_context.field_revs,
blocks: build_context.blocks, blocks: build_context.blocks.into_iter().map(Arc::new).collect(),
setting: Default::default(), setting: Default::default(),
}; };
@ -86,6 +87,6 @@ mod tests {
let _: GridRevision = serde_json::from_str(&grid_meta_delta.to_str().unwrap()).unwrap(); let _: GridRevision = serde_json::from_str(&grid_meta_delta.to_str().unwrap()).unwrap();
let grid_block_meta_delta = make_block_meta_delta(build_context.blocks_meta_data.first().unwrap()); let grid_block_meta_delta = make_block_meta_delta(build_context.blocks_meta_data.first().unwrap());
let _: GridBlockRevisionData = serde_json::from_str(&grid_block_meta_delta.to_str().unwrap()).unwrap(); let _: GridBlockRevision = serde_json::from_str(&grid_block_meta_delta.to_str().unwrap()).unwrap();
} }
} }

View File

@ -6,7 +6,7 @@ use flowy_grid_data_model::entities::{FieldChangesetParams, FieldOrder};
use flowy_grid_data_model::entities::{FieldType, GridSettingChangesetParams}; use flowy_grid_data_model::entities::{FieldType, GridSettingChangesetParams};
use flowy_grid_data_model::revision::{ use flowy_grid_data_model::revision::{
gen_block_id, gen_grid_filter_id, gen_grid_group_id, gen_grid_id, gen_grid_sort_id, FieldRevision, gen_block_id, gen_grid_filter_id, gen_grid_group_id, gen_grid_id, gen_grid_sort_id, FieldRevision,
GridBlockRevision, GridBlockRevisionChangeset, GridFilterRevision, GridGroupRevision, GridLayoutRevision, GridBlockMetaRevision, GridBlockMetaRevisionChangeset, GridFilterRevision, GridGroupRevision, GridLayoutRevision,
GridRevision, GridSettingRevision, GridSortRevision, GridRevision, GridSettingRevision, GridSortRevision,
}; };
use lib_infra::util::move_vec_element; use lib_infra::util::move_vec_element;
@ -27,7 +27,7 @@ pub trait JsonDeserializer {
} }
impl GridRevisionPad { impl GridRevisionPad {
pub async fn duplicate_grid_meta(&self) -> (Vec<FieldRevision>, Vec<GridBlockRevision>) { pub async fn duplicate_grid_block_meta(&self) -> (Vec<FieldRevision>, Vec<GridBlockMetaRevision>) {
let fields = self.grid_rev.fields.to_vec(); let fields = self.grid_rev.fields.to_vec();
let blocks = self let blocks = self
@ -39,7 +39,7 @@ impl GridRevisionPad {
duplicated_block.block_id = gen_block_id(); duplicated_block.block_id = gen_block_id();
duplicated_block duplicated_block
}) })
.collect::<Vec<GridBlockRevision>>(); .collect::<Vec<GridBlockMetaRevision>>();
(fields, blocks) (fields, blocks)
} }
@ -281,7 +281,7 @@ impl GridRevisionPad {
} }
} }
pub fn create_block_rev(&mut self, block: GridBlockRevision) -> CollaborateResult<Option<GridChangeset>> { pub fn create_block_meta_rev(&mut self, block: GridBlockMetaRevision) -> CollaborateResult<Option<GridChangeset>> {
self.modify_grid(|grid_meta| { self.modify_grid(|grid_meta| {
if grid_meta.blocks.iter().any(|b| b.block_id == block.block_id) { if grid_meta.blocks.iter().any(|b| b.block_id == block.block_id) {
tracing::warn!("Duplicate grid block"); tracing::warn!("Duplicate grid block");
@ -304,13 +304,13 @@ impl GridRevisionPad {
}) })
} }
pub fn get_block_revs(&self) -> Vec<Arc<GridBlockRevision>> { pub fn get_block_meta_revs(&self) -> Vec<Arc<GridBlockMetaRevision>> {
self.grid_rev.blocks.clone() self.grid_rev.blocks.clone()
} }
pub fn update_block_rev( pub fn update_block_rev(
&mut self, &mut self,
changeset: GridBlockRevisionChangeset, changeset: GridBlockMetaRevisionChangeset,
) -> CollaborateResult<Option<GridChangeset>> { ) -> CollaborateResult<Option<GridChangeset>> {
let block_id = changeset.block_id.clone(); let block_id = changeset.block_id.clone();
self.modify_block(&block_id, |block| { self.modify_block(&block_id, |block| {
@ -457,7 +457,7 @@ impl GridRevisionPad {
fn modify_block<F>(&mut self, block_id: &str, f: F) -> CollaborateResult<Option<GridChangeset>> fn modify_block<F>(&mut self, block_id: &str, f: F) -> CollaborateResult<Option<GridChangeset>>
where where
F: FnOnce(&mut GridBlockRevision) -> CollaborateResult<Option<()>>, F: FnOnce(&mut GridBlockMetaRevision) -> CollaborateResult<Option<()>>,
{ {
self.modify_grid( self.modify_grid(
|grid_rev| match grid_rev.blocks.iter().position(|block| block.block_id == block_id) { |grid_rev| match grid_rev.blocks.iter().position(|block| block.block_id == block_id) {