refactor: rename struct & add documentation

This commit is contained in:
appflowy
2022-07-17 11:29:02 +08:00
parent c860d1bc0b
commit 8073414485
20 changed files with 127 additions and 137 deletions

View File

@ -10,33 +10,33 @@ pub struct GridBlock {
pub id: String,
#[pb(index = 2)]
pub row_infos: Vec<RowInfo>,
pub rows: Vec<Row>,
}
impl GridBlock {
pub fn new(block_id: &str, row_orders: Vec<RowInfo>) -> Self {
pub fn new(block_id: &str, rows: Vec<Row>) -> Self {
Self {
id: block_id.to_owned(),
row_infos: row_orders,
rows,
}
}
}
#[derive(Debug, Default, Clone, ProtoBuf)]
pub struct RowInfo {
pub struct Row {
#[pb(index = 1)]
pub block_id: String,
#[pb(index = 2)]
pub row_id: String,
pub id: String,
#[pb(index = 3)]
pub height: i32,
}
impl RowInfo {
impl Row {
pub fn row_id(&self) -> &str {
&self.row_id
&self.id
}
pub fn block_id(&self) -> &str {
@ -44,35 +44,26 @@ impl RowInfo {
}
}
impl std::convert::From<&RowRevision> for RowInfo {
impl std::convert::From<&RowRevision> for Row {
fn from(rev: &RowRevision) -> Self {
Self {
block_id: rev.block_id.clone(),
row_id: rev.id.clone(),
id: rev.id.clone(),
height: rev.height,
}
}
}
impl std::convert::From<&Arc<RowRevision>> for RowInfo {
impl std::convert::From<&Arc<RowRevision>> for Row {
fn from(rev: &Arc<RowRevision>) -> Self {
Self {
block_id: rev.block_id.clone(),
row_id: rev.id.clone(),
id: rev.id.clone(),
height: rev.height,
}
}
}
#[derive(Debug, Default, ProtoBuf)]
pub struct Row {
#[pb(index = 1)]
pub id: String,
#[pb(index = 2)]
pub height: i32,
}
#[derive(Debug, Default, ProtoBuf)]
pub struct OptionalRow {
#[pb(index = 1, one_of)]
@ -139,10 +130,10 @@ impl UpdatedRow {
}
}
impl std::convert::From<RowInfo> for InsertedRow {
fn from(row_info: RowInfo) -> Self {
impl std::convert::From<Row> for InsertedRow {
fn from(row_info: Row) -> Self {
Self {
row_id: row_info.row_id,
row_id: row_info.id,
block_id: row_info.block_id,
height: row_info.height,
index: None,
@ -152,7 +143,7 @@ impl std::convert::From<RowInfo> for InsertedRow {
impl std::convert::From<&RowRevision> for InsertedRow {
fn from(row: &RowRevision) -> Self {
let row_order = RowInfo::from(row);
let row_order = Row::from(row);
Self::from(row_order)
}
}

View File

@ -57,24 +57,24 @@ impl std::convert::From<Arc<FieldRevision>> for Field {
}
}
#[derive(Debug, Clone, Default, ProtoBuf)]
pub struct FieldOrder {
pub struct GridField {
#[pb(index = 1)]
pub field_id: String,
}
impl std::convert::From<&str> for FieldOrder {
impl std::convert::From<&str> for GridField {
fn from(s: &str) -> Self {
FieldOrder { field_id: s.to_owned() }
GridField { field_id: s.to_owned() }
}
}
impl std::convert::From<String> for FieldOrder {
impl std::convert::From<String> for GridField {
fn from(s: String) -> Self {
FieldOrder { field_id: s }
GridField { field_id: s }
}
}
impl std::convert::From<&Arc<FieldRevision>> for FieldOrder {
impl std::convert::From<&Arc<FieldRevision>> for GridField {
fn from(field_rev: &Arc<FieldRevision>) -> Self {
Self {
field_id: field_rev.id.clone(),
@ -90,7 +90,7 @@ pub struct GridFieldChangeset {
pub inserted_fields: Vec<IndexField>,
#[pb(index = 3)]
pub deleted_fields: Vec<FieldOrder>,
pub deleted_fields: Vec<GridField>,
#[pb(index = 4)]
pub updated_fields: Vec<Field>,
@ -106,7 +106,7 @@ impl GridFieldChangeset {
}
}
pub fn delete(grid_id: &str, deleted_fields: Vec<FieldOrder>) -> Self {
pub fn delete(grid_id: &str, deleted_fields: Vec<GridField>) -> Self {
Self {
grid_id: grid_id.to_string(),
inserted_fields: vec![],
@ -259,18 +259,18 @@ impl std::convert::From<Vec<Field>> for RepeatedField {
#[derive(Debug, Clone, Default, ProtoBuf)]
pub struct RepeatedFieldOrder {
#[pb(index = 1)]
pub items: Vec<FieldOrder>,
pub items: Vec<GridField>,
}
impl std::ops::Deref for RepeatedFieldOrder {
type Target = Vec<FieldOrder>;
type Target = Vec<GridField>;
fn deref(&self) -> &Self::Target {
&self.items
}
}
impl std::convert::From<Vec<FieldOrder>> for RepeatedFieldOrder {
fn from(field_orders: Vec<FieldOrder>) -> Self {
impl std::convert::From<Vec<GridField>> for RepeatedFieldOrder {
fn from(field_orders: Vec<GridField>) -> Self {
RepeatedFieldOrder { items: field_orders }
}
}
@ -278,7 +278,7 @@ impl std::convert::From<Vec<FieldOrder>> for RepeatedFieldOrder {
impl std::convert::From<String> for RepeatedFieldOrder {
fn from(s: String) -> Self {
RepeatedFieldOrder {
items: vec![FieldOrder::from(s)],
items: vec![GridField::from(s)],
}
}
}

View File

@ -1,4 +1,4 @@
use crate::entities::{FieldOrder, GridBlock};
use crate::entities::{GridBlock, GridField};
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
use flowy_error::ErrorCode;
use flowy_grid_data_model::parser::NotEmptyStr;
@ -8,7 +8,7 @@ pub struct Grid {
pub id: String,
#[pb(index = 2)]
pub field_orders: Vec<FieldOrder>,
pub fields: Vec<GridField>,
#[pb(index = 3)]
pub blocks: Vec<GridBlock>,

View File

@ -1,5 +1,5 @@
use crate::dart_notification::{send_dart_notification, GridNotification};
use crate::entities::{CellChangeset, GridBlockChangeset, InsertedRow, Row, RowInfo, UpdatedRow};
use crate::entities::{CellChangeset, GridBlockChangeset, InsertedRow, Row, UpdatedRow};
use crate::manager::GridUser;
use crate::services::block_revision_editor::GridBlockRevisionEditor;
use crate::services::persistence::block_index::BlockIndexCache;
@ -138,7 +138,7 @@ impl GridBlockManager {
Some(row_info) => {
let _ = editor.delete_rows(vec![Cow::Borrowed(&row_id)]).await?;
let _ = self
.notify_did_update_block(&block_id, GridBlockChangeset::delete(&block_id, vec![row_info.row_id]))
.notify_did_update_block(&block_id, GridBlockChangeset::delete(&block_id, vec![row_info.id]))
.await?;
}
}
@ -146,15 +146,12 @@ impl GridBlockManager {
Ok(())
}
pub(crate) async fn delete_rows(
&self,
row_orders: Vec<RowInfo>,
) -> FlowyResult<Vec<GridBlockMetaRevisionChangeset>> {
pub(crate) async fn delete_rows(&self, row_orders: Vec<Row>) -> FlowyResult<Vec<GridBlockMetaRevisionChangeset>> {
let mut changesets = vec![];
for grid_block in block_from_row_orders(row_orders) {
let editor = self.get_editor(&grid_block.id).await?;
let row_ids = grid_block
.row_infos
.rows
.into_iter()
.map(|row_info| Cow::Owned(row_info.row_id().to_owned()))
.collect::<Vec<Cow<String>>>();
@ -217,7 +214,7 @@ impl GridBlockManager {
}
}
pub async fn get_row_orders(&self, block_id: &str) -> FlowyResult<Vec<RowInfo>> {
pub async fn get_row_orders(&self, block_id: &str) -> FlowyResult<Vec<Row>> {
let editor = self.get_editor(block_id).await?;
editor.get_row_infos::<&str>(None).await
}

View File

@ -1,4 +1,4 @@
use crate::entities::RowInfo;
use crate::entities::Row;
use bytes::Bytes;
use flowy_error::{FlowyError, FlowyResult};
use flowy_grid_data_model::revision::{CellRevision, GridBlockRevision, RowMetaChangeset, RowRevision};
@ -123,12 +123,12 @@ impl GridBlockRevisionEditor {
Ok(cell_revs)
}
pub async fn get_row_info(&self, row_id: &str) -> FlowyResult<Option<RowInfo>> {
pub async fn get_row_info(&self, row_id: &str) -> FlowyResult<Option<Row>> {
let row_ids = Some(vec![Cow::Borrowed(row_id)]);
Ok(self.get_row_infos(row_ids).await?.pop())
}
pub async fn get_row_infos<T>(&self, row_ids: Option<Vec<Cow<'_, T>>>) -> FlowyResult<Vec<RowInfo>>
pub async fn get_row_infos<T>(&self, row_ids: Option<Vec<Cow<'_, T>>>) -> FlowyResult<Vec<Row>>
where
T: AsRef<str> + ToOwned + ?Sized,
{
@ -138,8 +138,8 @@ impl GridBlockRevisionEditor {
.await
.get_row_revs(row_ids)?
.iter()
.map(RowInfo::from)
.collect::<Vec<RowInfo>>();
.map(Row::from)
.collect::<Vec<Row>>();
Ok(row_infos)
}

View File

@ -189,7 +189,7 @@ impl GridRevisionEditor {
pub async fn delete_field(&self, field_id: &str) -> FlowyResult<()> {
let _ = self.modify(|grid_pad| Ok(grid_pad.delete_field_rev(field_id)?)).await?;
let field_order = FieldOrder::from(field_id);
let field_order = GridField::from(field_id);
let notified_changeset = GridFieldChangeset::delete(&self.grid_id, vec![field_order]);
let _ = self.notify_did_update_grid(notified_changeset).await?;
Ok(())
@ -269,14 +269,14 @@ impl GridRevisionEditor {
Ok(())
}
pub async fn create_row(&self, start_row_id: Option<String>) -> FlowyResult<RowInfo> {
pub async fn create_row(&self, start_row_id: Option<String>) -> FlowyResult<Row> {
let field_revs = self.grid_pad.read().await.get_field_revs(None)?;
let block_id = self.block_id().await?;
// insert empty row below the row whose id is upper_row_id
let row_rev_ctx = CreateRowRevisionBuilder::new(&field_revs).build();
let row_rev = make_row_rev_from_context(&block_id, row_rev_ctx);
let row_order = RowInfo::from(&row_rev);
let row_order = Row::from(&row_rev);
// insert the row
let row_count = self.block_manager.create_row(&block_id, row_rev, start_row_id).await?;
@ -287,13 +287,13 @@ impl GridRevisionEditor {
Ok(row_order)
}
pub async fn insert_rows(&self, contexts: Vec<CreateRowRevisionPayload>) -> FlowyResult<Vec<RowInfo>> {
pub async fn insert_rows(&self, contexts: Vec<CreateRowRevisionPayload>) -> FlowyResult<Vec<Row>> {
let block_id = self.block_id().await?;
let mut rows_by_block_id: HashMap<String, Vec<RowRevision>> = HashMap::new();
let mut row_orders = vec![];
for ctx in contexts {
let row_rev = make_row_rev_from_context(&block_id, ctx);
row_orders.push(RowInfo::from(&row_rev));
row_orders.push(Row::from(&row_rev));
rows_by_block_id
.entry(block_id.clone())
.or_insert_with(Vec::new)
@ -421,7 +421,7 @@ impl GridRevisionEditor {
Ok(block_meta_revs)
}
pub async fn delete_rows(&self, row_orders: Vec<RowInfo>) -> FlowyResult<()> {
pub async fn delete_rows(&self, row_orders: Vec<Row>) -> FlowyResult<()> {
let changesets = self.block_manager.delete_rows(row_orders).await?;
for changeset in changesets {
let _ = self.update_block(changeset).await?;
@ -434,21 +434,21 @@ impl GridRevisionEditor {
let field_orders = pad_read_guard
.get_field_revs(None)?
.iter()
.map(FieldOrder::from)
.map(GridField::from)
.collect();
let mut block_orders = vec![];
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 block_order = GridBlock {
id: block_rev.block_id.clone(),
row_infos: row_orders,
rows: row_orders,
};
block_orders.push(block_order);
}
Ok(Grid {
id: self.grid_id.clone(),
field_orders,
fields: field_orders,
blocks: block_orders,
})
}
@ -517,7 +517,7 @@ impl GridRevisionEditor {
.modify(|grid_pad| Ok(grid_pad.move_field(field_id, from as usize, to as usize)?))
.await?;
if let Some((index, field_rev)) = self.grid_pad.read().await.get_field_rev(field_id) {
let delete_field_order = FieldOrder::from(field_id);
let delete_field_order = GridField::from(field_id);
let insert_field = IndexField::from_field_rev(field_rev, index);
let notified_changeset = GridFieldChangeset {
grid_id: self.grid_id.clone(),

View File

@ -1,4 +1,4 @@
use crate::entities::{GridBlock, RepeatedGridBlock, Row, RowInfo};
use crate::entities::{GridBlock, RepeatedGridBlock, Row};
use flowy_error::FlowyResult;
use flowy_grid_data_model::revision::{FieldRevision, RowRevision};
use std::collections::HashMap;
@ -9,7 +9,7 @@ pub struct GridBlockSnapshot {
pub row_revs: Vec<Arc<RowRevision>>,
}
pub(crate) fn block_from_row_orders(row_orders: Vec<RowInfo>) -> Vec<GridBlock> {
pub(crate) fn block_from_row_orders(row_orders: Vec<Row>) -> Vec<GridBlock> {
let mut map: HashMap<String, GridBlock> = HashMap::new();
row_orders.into_iter().for_each(|row_info| {
// Memory Optimization: escape clone block_id
@ -17,7 +17,7 @@ pub(crate) fn block_from_row_orders(row_orders: Vec<RowInfo>) -> Vec<GridBlock>
let cloned_block_id = block_id.clone();
map.entry(block_id)
.or_insert_with(|| GridBlock::new(&cloned_block_id, vec![]))
.row_infos
.rows
.push(row_info);
});
map.into_values().collect::<Vec<_>>()
@ -35,8 +35,8 @@ pub(crate) fn block_from_row_orders(row_orders: Vec<RowInfo>) -> Vec<GridBlock>
// Some((field_id, cell))
// }
pub(crate) fn make_row_orders_from_row_revs(row_revs: &[Arc<RowRevision>]) -> Vec<RowInfo> {
row_revs.iter().map(RowInfo::from).collect::<Vec<_>>()
pub(crate) fn make_row_orders_from_row_revs(row_revs: &[Arc<RowRevision>]) -> Vec<Row> {
row_revs.iter().map(Row::from).collect::<Vec<_>>()
}
pub(crate) fn make_row_from_row_rev(fields: &[Arc<FieldRevision>], row_rev: Arc<RowRevision>) -> Option<Row> {
@ -58,6 +58,7 @@ pub(crate) fn make_rows_from_row_revs(_fields: &[Arc<FieldRevision>], row_revs:
// .collect::<HashMap<String, Cell>>();
Row {
block_id: row_rev.block_id.clone(),
id: row_rev.id.clone(),
height: row_rev.height,
}

View File

@ -1,5 +1,5 @@
use crate::grid::grid_editor::GridEditorTest;
use flowy_grid::entities::RowInfo;
use flowy_grid::entities::Row;
use flowy_grid::services::row::{CreateRowRevisionBuilder, CreateRowRevisionPayload};
use flowy_grid_data_model::revision::{
FieldRevision, GridBlockMetaRevision, GridBlockMetaRevisionChangeset, RowMetaChangeset, RowRevision,
@ -90,7 +90,7 @@ impl GridRowTest {
let row_orders = row_ids
.into_iter()
.map(|row_id| self.row_order_by_row_id.get(&row_id).unwrap().clone())
.collect::<Vec<RowInfo>>();
.collect::<Vec<Row>>();
self.editor.delete_rows(row_orders).await.unwrap();
self.row_revs = self.get_row_revs().await;

View File

@ -30,7 +30,7 @@ pub struct GridEditorTest {
pub block_meta_revs: Vec<Arc<GridBlockMetaRevision>>,
pub row_revs: Vec<Arc<RowRevision>>,
pub field_count: usize,
pub row_order_by_row_id: HashMap<String, RowInfo>,
pub row_order_by_row_id: HashMap<String, Row>,
}
impl GridEditorTest {