2022-07-01 16:10:13 +00:00
|
|
|
use crate::entities::GridRowId;
|
2022-07-01 12:32:11 +00:00
|
|
|
use flowy_derive::ProtoBuf;
|
|
|
|
use flowy_error::ErrorCode;
|
|
|
|
use flowy_grid_data_model::parser::NotEmptyStr;
|
|
|
|
use flowy_grid_data_model::revision::RowRevision;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Default, ProtoBuf)]
|
|
|
|
pub struct GridBlock {
|
|
|
|
#[pb(index = 1)]
|
|
|
|
pub id: String,
|
|
|
|
|
|
|
|
#[pb(index = 2)]
|
2022-07-01 16:10:13 +00:00
|
|
|
pub row_infos: Vec<BlockRowInfo>,
|
2022-07-01 12:32:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GridBlock {
|
2022-07-01 16:10:13 +00:00
|
|
|
pub fn new(block_id: &str, row_orders: Vec<BlockRowInfo>) -> Self {
|
2022-07-01 12:32:11 +00:00
|
|
|
Self {
|
|
|
|
id: block_id.to_owned(),
|
2022-07-01 16:10:13 +00:00
|
|
|
row_infos: row_orders,
|
2022-07-01 12:32:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default, Clone, ProtoBuf)]
|
2022-07-01 16:10:13 +00:00
|
|
|
pub struct BlockRowInfo {
|
2022-07-01 12:32:11 +00:00
|
|
|
#[pb(index = 1)]
|
2022-07-01 16:10:13 +00:00
|
|
|
pub block_id: String,
|
2022-07-01 12:32:11 +00:00
|
|
|
|
|
|
|
#[pb(index = 2)]
|
2022-07-01 16:10:13 +00:00
|
|
|
pub row_id: String,
|
2022-07-01 12:32:11 +00:00
|
|
|
|
|
|
|
#[pb(index = 3)]
|
|
|
|
pub height: i32,
|
|
|
|
}
|
|
|
|
|
2022-07-01 16:10:13 +00:00
|
|
|
impl BlockRowInfo {
|
|
|
|
pub fn row_id(&self) -> &str {
|
|
|
|
&self.row_id
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn block_id(&self) -> &str {
|
|
|
|
&self.block_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::convert::From<&RowRevision> for BlockRowInfo {
|
|
|
|
fn from(rev: &RowRevision) -> Self {
|
2022-07-01 12:32:11 +00:00
|
|
|
Self {
|
2022-07-01 16:10:13 +00:00
|
|
|
block_id: rev.block_id.clone(),
|
|
|
|
row_id: rev.id.clone(),
|
|
|
|
height: rev.height,
|
2022-07-01 12:32:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-01 16:10:13 +00:00
|
|
|
impl std::convert::From<&Arc<RowRevision>> for BlockRowInfo {
|
|
|
|
fn from(rev: &Arc<RowRevision>) -> Self {
|
2022-07-01 12:32:11 +00:00
|
|
|
Self {
|
2022-07-01 16:10:13 +00:00
|
|
|
block_id: rev.block_id.clone(),
|
|
|
|
row_id: rev.id.clone(),
|
|
|
|
height: rev.height,
|
2022-07-01 12:32:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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)]
|
|
|
|
pub row: Option<Row>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default, ProtoBuf)]
|
|
|
|
pub struct RepeatedRow {
|
|
|
|
#[pb(index = 1)]
|
|
|
|
pub items: Vec<Row>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::convert::From<Vec<Row>> for RepeatedRow {
|
|
|
|
fn from(items: Vec<Row>) -> Self {
|
|
|
|
Self { items }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[derive(Debug, Default, ProtoBuf)]
|
|
|
|
pub struct RepeatedGridBlock {
|
|
|
|
#[pb(index = 1)]
|
|
|
|
pub items: Vec<GridBlock>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::convert::From<Vec<GridBlock>> for RepeatedGridBlock {
|
|
|
|
fn from(items: Vec<GridBlock>) -> Self {
|
|
|
|
Self { items }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Default, ProtoBuf)]
|
2022-07-04 07:24:40 +00:00
|
|
|
pub struct InsertedRow {
|
2022-07-01 12:32:11 +00:00
|
|
|
#[pb(index = 1)]
|
2022-07-04 07:24:40 +00:00
|
|
|
pub block_id: String,
|
|
|
|
|
|
|
|
#[pb(index = 2)]
|
|
|
|
pub row_id: String,
|
|
|
|
|
|
|
|
#[pb(index = 3)]
|
|
|
|
pub height: i32,
|
2022-07-01 12:32:11 +00:00
|
|
|
|
2022-07-04 07:24:40 +00:00
|
|
|
#[pb(index = 4, one_of)]
|
2022-07-01 12:32:11 +00:00
|
|
|
pub index: Option<i32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default, ProtoBuf)]
|
2022-07-04 07:24:40 +00:00
|
|
|
pub struct UpdatedRow {
|
2022-07-01 12:32:11 +00:00
|
|
|
#[pb(index = 1)]
|
2022-07-04 07:24:40 +00:00
|
|
|
pub block_id: String,
|
2022-07-01 12:32:11 +00:00
|
|
|
|
|
|
|
#[pb(index = 2)]
|
2022-07-04 07:24:40 +00:00
|
|
|
pub row_id: String,
|
|
|
|
|
|
|
|
#[pb(index = 3)]
|
2022-07-01 12:32:11 +00:00
|
|
|
pub row: Row,
|
|
|
|
}
|
|
|
|
|
2022-07-04 07:24:40 +00:00
|
|
|
impl UpdatedRow {
|
2022-07-01 12:32:11 +00:00
|
|
|
pub fn new(row_rev: &RowRevision, row: Row) -> Self {
|
|
|
|
Self {
|
2022-07-04 07:24:40 +00:00
|
|
|
row_id: row_rev.id.clone(),
|
|
|
|
block_id: row_rev.block_id.clone(),
|
2022-07-01 12:32:11 +00:00
|
|
|
row,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-04 07:24:40 +00:00
|
|
|
impl std::convert::From<BlockRowInfo> for InsertedRow {
|
2022-07-01 16:10:13 +00:00
|
|
|
fn from(row_info: BlockRowInfo) -> Self {
|
2022-07-04 07:24:40 +00:00
|
|
|
Self {
|
|
|
|
row_id: row_info.row_id,
|
|
|
|
block_id: row_info.block_id,
|
|
|
|
height: row_info.height,
|
|
|
|
index: None,
|
|
|
|
}
|
2022-07-01 12:32:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-04 07:24:40 +00:00
|
|
|
impl std::convert::From<&RowRevision> for InsertedRow {
|
2022-07-01 12:32:11 +00:00
|
|
|
fn from(row: &RowRevision) -> Self {
|
2022-07-01 16:10:13 +00:00
|
|
|
let row_order = BlockRowInfo::from(row);
|
2022-07-01 12:32:11 +00:00
|
|
|
Self::from(row_order)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default, ProtoBuf)]
|
2022-07-04 07:24:40 +00:00
|
|
|
pub struct GridBlockChangeset {
|
2022-07-01 12:32:11 +00:00
|
|
|
#[pb(index = 1)]
|
|
|
|
pub block_id: String,
|
|
|
|
|
|
|
|
#[pb(index = 2)]
|
2022-07-04 07:24:40 +00:00
|
|
|
pub inserted_rows: Vec<InsertedRow>,
|
2022-07-01 12:32:11 +00:00
|
|
|
|
|
|
|
#[pb(index = 3)]
|
2022-07-01 16:10:13 +00:00
|
|
|
pub deleted_rows: Vec<GridRowId>,
|
2022-07-01 12:32:11 +00:00
|
|
|
|
|
|
|
#[pb(index = 4)]
|
2022-07-04 07:24:40 +00:00
|
|
|
pub updated_rows: Vec<UpdatedRow>,
|
2022-07-01 12:32:11 +00:00
|
|
|
}
|
2022-07-04 07:24:40 +00:00
|
|
|
impl GridBlockChangeset {
|
|
|
|
pub fn insert(block_id: &str, inserted_rows: Vec<InsertedRow>) -> Self {
|
2022-07-01 12:32:11 +00:00
|
|
|
Self {
|
|
|
|
block_id: block_id.to_owned(),
|
|
|
|
inserted_rows,
|
|
|
|
deleted_rows: vec![],
|
|
|
|
updated_rows: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-01 16:10:13 +00:00
|
|
|
pub fn delete(block_id: &str, deleted_rows: Vec<GridRowId>) -> Self {
|
2022-07-01 12:32:11 +00:00
|
|
|
Self {
|
|
|
|
block_id: block_id.to_owned(),
|
|
|
|
inserted_rows: vec![],
|
|
|
|
deleted_rows,
|
|
|
|
updated_rows: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-04 07:24:40 +00:00
|
|
|
pub fn update(block_id: &str, updated_rows: Vec<UpdatedRow>) -> Self {
|
2022-07-01 12:32:11 +00:00
|
|
|
Self {
|
|
|
|
block_id: block_id.to_owned(),
|
|
|
|
inserted_rows: vec![],
|
|
|
|
deleted_rows: vec![],
|
|
|
|
updated_rows,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(ProtoBuf, Default)]
|
|
|
|
pub struct QueryGridBlocksPayload {
|
|
|
|
#[pb(index = 1)]
|
|
|
|
pub grid_id: String,
|
|
|
|
|
|
|
|
#[pb(index = 2)]
|
|
|
|
pub block_ids: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct QueryGridBlocksParams {
|
|
|
|
pub grid_id: String,
|
|
|
|
pub block_ids: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryInto<QueryGridBlocksParams> for QueryGridBlocksPayload {
|
|
|
|
type Error = ErrorCode;
|
|
|
|
|
|
|
|
fn try_into(self) -> Result<QueryGridBlocksParams, Self::Error> {
|
|
|
|
let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
|
|
|
|
Ok(QueryGridBlocksParams {
|
|
|
|
grid_id: grid_id.0,
|
|
|
|
block_ids: self.block_ids,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|