313 lines
11 KiB
Rust
Raw Normal View History

2022-03-05 10:59:44 +08:00
use crate::entities::revision::{md5, RepeatedRevision, Revision};
2022-03-04 18:11:12 +08:00
use crate::errors::{internal_error, CollaborateError, CollaborateResult};
use crate::util::{cal_diff, make_delta_from_revisions};
2022-03-15 19:00:28 +08:00
use bytes::Bytes;
2022-03-11 21:36:00 +08:00
use flowy_grid_data_model::entities::{
2022-03-27 09:35:10 +08:00
FieldChangesetParams, FieldMeta, FieldOrder, GridBlockMeta, GridBlockMetaChangeset, GridMeta, RepeatedFieldOrder,
2022-03-11 21:36:00 +08:00
};
2022-03-26 20:27:32 +08:00
2022-03-04 18:11:12 +08:00
use lib_infra::uuid;
2022-03-06 09:03:02 +08:00
use lib_ot::core::{OperationTransformable, PlainTextAttributes, PlainTextDelta, PlainTextDeltaBuilder};
2022-03-11 21:36:00 +08:00
use std::collections::HashMap;
2022-03-27 09:35:10 +08:00
use std::string::FromUtf8Error;
2022-03-04 18:11:12 +08:00
use std::sync::Arc;
2022-03-12 09:30:13 +08:00
pub type GridMetaDelta = PlainTextDelta;
2022-03-04 18:11:12 +08:00
pub type GridDeltaBuilder = PlainTextDeltaBuilder;
2022-03-10 12:01:31 +08:00
pub struct GridMetaPad {
pub(crate) grid_meta: Arc<GridMeta>,
2022-03-12 09:30:13 +08:00
pub(crate) delta: GridMetaDelta,
2022-03-04 18:11:12 +08:00
}
2022-03-10 12:01:31 +08:00
impl GridMetaPad {
2022-03-12 09:30:13 +08:00
pub fn from_delta(delta: GridMetaDelta) -> CollaborateResult<Self> {
2022-03-04 18:11:12 +08:00
let s = delta.to_str()?;
2022-03-10 12:01:31 +08:00
let grid: GridMeta = serde_json::from_str(&s)
2022-03-04 18:11:12 +08:00
.map_err(|e| CollaborateError::internal().context(format!("Deserialize delta to grid failed: {}", e)))?;
Ok(Self {
2022-03-10 12:01:31 +08:00
grid_meta: Arc::new(grid),
2022-03-04 18:11:12 +08:00
delta,
})
}
2022-03-05 10:59:44 +08:00
pub fn from_revisions(_grid_id: &str, revisions: Vec<Revision>) -> CollaborateResult<Self> {
2022-03-12 09:30:13 +08:00
let grid_delta: GridMetaDelta = make_delta_from_revisions::<PlainTextAttributes>(revisions)?;
2022-03-10 12:01:31 +08:00
Self::from_delta(grid_delta)
2022-03-04 18:11:12 +08:00
}
2022-03-25 20:55:56 +08:00
pub fn create_field(
&mut self,
new_field_meta: FieldMeta,
start_field_id: Option<String>,
) -> CollaborateResult<Option<GridChangeset>> {
2022-03-04 18:11:12 +08:00
self.modify_grid(|grid| {
2022-03-24 17:09:05 +08:00
// Check if the field exists or not
2022-03-26 20:27:32 +08:00
if grid.fields.iter().any(|field_meta| field_meta.id == new_field_meta.id) {
2022-03-24 17:09:05 +08:00
tracing::warn!("Duplicate grid field");
return Ok(None);
}
let insert_index = match start_field_id {
None => None,
Some(start_field_id) => grid.fields.iter().position(|field| field.id == start_field_id),
};
match insert_index {
2022-03-25 20:55:56 +08:00
None => grid.fields.push(new_field_meta),
Some(index) => grid.fields.insert(index, new_field_meta),
2022-03-13 11:06:28 +08:00
}
Ok(Some(()))
2022-03-04 18:11:12 +08:00
})
}
2022-03-16 21:19:51 +08:00
pub fn delete_field(&mut self, field_id: &str) -> CollaborateResult<Option<GridChangeset>> {
2022-03-10 12:01:31 +08:00
self.modify_grid(|grid| match grid.fields.iter().position(|field| field.id == field_id) {
None => Ok(None),
Some(index) => {
grid.fields.remove(index);
Ok(Some(()))
2022-03-04 18:11:12 +08:00
}
})
}
2022-03-15 19:00:28 +08:00
pub fn contain_field(&self, field_id: &str) -> bool {
2022-03-16 10:02:37 +08:00
self.grid_meta.fields.iter().any(|field| field.id == field_id)
}
pub fn get_field(&self, field_id: &str) -> Option<&FieldMeta> {
self.grid_meta.fields.iter().find(|field| field.id == field_id)
2022-03-15 19:00:28 +08:00
}
2022-03-15 11:07:18 +08:00
pub fn get_field_orders(&self) -> Vec<FieldOrder> {
2022-03-16 10:02:37 +08:00
self.grid_meta.fields.iter().map(FieldOrder::from).collect()
2022-03-15 11:07:18 +08:00
}
pub fn get_field_metas(&self, field_orders: Option<RepeatedFieldOrder>) -> CollaborateResult<Vec<FieldMeta>> {
2022-03-12 22:52:24 +08:00
match field_orders {
2022-03-13 23:16:52 +08:00
None => Ok(self.grid_meta.fields.clone()),
2022-03-12 22:52:24 +08:00
Some(field_orders) => {
let field_by_field_id = self
.grid_meta
.fields
.iter()
.map(|field| (&field.id, field))
2022-03-15 11:07:18 +08:00
.collect::<HashMap<&String, &FieldMeta>>();
2022-03-11 21:36:00 +08:00
2022-03-12 22:52:24 +08:00
let fields = field_orders
.iter()
.flat_map(|field_order| match field_by_field_id.get(&field_order.field_id) {
None => {
tracing::error!("Can't find the field with id: {}", field_order.field_id);
None
}
Some(field) => Some((*field).clone()),
})
2022-03-15 11:07:18 +08:00
.collect::<Vec<FieldMeta>>();
2022-03-13 11:06:28 +08:00
Ok(fields)
2022-03-12 22:52:24 +08:00
}
}
2022-03-11 21:36:00 +08:00
}
2022-03-27 09:35:10 +08:00
pub fn update_field(&mut self, changeset: FieldChangesetParams) -> CollaborateResult<Option<GridChangeset>> {
2022-03-14 17:24:25 +08:00
let field_id = changeset.field_id.clone();
2022-03-11 21:36:00 +08:00
self.modify_field(&field_id, |field| {
let mut is_changed = None;
2022-03-14 17:24:25 +08:00
if let Some(name) = changeset.name {
2022-03-11 21:36:00 +08:00
field.name = name;
is_changed = Some(())
}
2022-03-14 17:24:25 +08:00
if let Some(desc) = changeset.desc {
2022-03-11 21:36:00 +08:00
field.desc = desc;
is_changed = Some(())
}
2022-03-14 17:24:25 +08:00
if let Some(field_type) = changeset.field_type {
2022-03-11 21:36:00 +08:00
field.field_type = field_type;
is_changed = Some(())
}
2022-03-14 17:24:25 +08:00
if let Some(frozen) = changeset.frozen {
2022-03-11 21:36:00 +08:00
field.frozen = frozen;
is_changed = Some(())
}
2022-03-14 17:24:25 +08:00
if let Some(visibility) = changeset.visibility {
2022-03-11 21:36:00 +08:00
field.visibility = visibility;
is_changed = Some(())
}
2022-03-14 17:24:25 +08:00
if let Some(width) = changeset.width {
2022-03-11 21:36:00 +08:00
field.width = width;
is_changed = Some(())
}
2022-03-27 09:35:10 +08:00
if let Some(type_option_data) = changeset.type_option_data {
match String::from_utf8(type_option_data) {
Ok(type_option_json) => {
field.type_option_json = type_option_json;
is_changed = Some(())
}
Err(err) => {
tracing::error!("Deserialize data to type option json failed: {}", err);
}
}
2022-03-11 21:36:00 +08:00
}
Ok(is_changed)
})
}
2022-03-17 17:25:43 +08:00
pub fn create_block(&mut self, block: GridBlockMeta) -> CollaborateResult<Option<GridChangeset>> {
2022-03-11 21:36:00 +08:00
self.modify_grid(|grid| {
2022-03-18 17:14:46 +08:00
if grid.block_metas.iter().any(|b| b.block_id == block.block_id) {
2022-03-13 11:06:28 +08:00
tracing::warn!("Duplicate grid block");
Ok(None)
} else {
2022-03-18 17:14:46 +08:00
match grid.block_metas.last() {
None => grid.block_metas.push(block),
2022-03-15 11:07:18 +08:00
Some(last_block) => {
if last_block.start_row_index > block.start_row_index
&& last_block.len() > block.start_row_index
{
2022-03-16 10:02:37 +08:00
let msg = "GridBlock's start_row_index should be greater than the last_block's start_row_index and its len".to_string();
2022-03-15 11:07:18 +08:00
return Err(CollaborateError::internal().context(msg))
}
2022-03-18 17:14:46 +08:00
grid.block_metas.push(block);
2022-03-15 11:07:18 +08:00
}
}
2022-03-13 11:06:28 +08:00
Ok(Some(()))
}
2022-03-11 21:36:00 +08:00
})
}
2022-03-17 17:25:43 +08:00
pub fn get_blocks(&self) -> Vec<GridBlockMeta> {
2022-03-18 17:14:46 +08:00
self.grid_meta.block_metas.clone()
2022-03-12 09:30:13 +08:00
}
2022-03-17 17:25:43 +08:00
pub fn update_block(&mut self, changeset: GridBlockMetaChangeset) -> CollaborateResult<Option<GridChangeset>> {
2022-03-14 17:24:25 +08:00
let block_id = changeset.block_id.clone();
2022-03-11 21:36:00 +08:00
self.modify_block(&block_id, |block| {
let mut is_changed = None;
2022-03-14 17:24:25 +08:00
if let Some(row_count) = changeset.row_count {
2022-03-11 21:36:00 +08:00
block.row_count = row_count;
is_changed = Some(());
}
2022-03-14 17:24:25 +08:00
if let Some(start_row_index) = changeset.start_row_index {
2022-03-13 11:06:28 +08:00
block.start_row_index = start_row_index;
is_changed = Some(());
}
2022-03-11 21:36:00 +08:00
Ok(is_changed)
})
}
pub fn md5(&self) -> String {
2022-03-15 19:00:28 +08:00
md5(&self.delta.to_delta_bytes())
2022-03-05 10:59:44 +08:00
}
2022-03-05 22:30:42 +08:00
pub fn delta_str(&self) -> String {
self.delta.to_delta_str()
}
2022-03-15 19:00:28 +08:00
pub fn delta_bytes(&self) -> Bytes {
self.delta.to_delta_bytes()
}
2022-03-15 11:07:18 +08:00
pub fn fields(&self) -> &[FieldMeta] {
2022-03-10 12:01:31 +08:00
&self.grid_meta.fields
2022-03-05 17:52:25 +08:00
}
2022-03-16 21:19:51 +08:00
fn modify_grid<F>(&mut self, f: F) -> CollaborateResult<Option<GridChangeset>>
2022-03-04 18:11:12 +08:00
where
2022-03-10 12:01:31 +08:00
F: FnOnce(&mut GridMeta) -> CollaborateResult<Option<()>>,
2022-03-04 18:11:12 +08:00
{
2022-03-10 12:01:31 +08:00
let cloned_grid = self.grid_meta.clone();
match f(Arc::make_mut(&mut self.grid_meta))? {
2022-03-04 18:11:12 +08:00
None => Ok(None),
Some(_) => {
let old = json_from_grid(&cloned_grid)?;
2022-03-10 12:01:31 +08:00
let new = json_from_grid(&self.grid_meta)?;
2022-03-04 18:11:12 +08:00
match cal_diff::<PlainTextAttributes>(old, new) {
None => Ok(None),
Some(delta) => {
self.delta = self.delta.compose(&delta)?;
2022-03-16 21:19:51 +08:00
Ok(Some(GridChangeset { delta, md5: self.md5() }))
2022-03-04 18:11:12 +08:00
}
}
}
}
}
2022-03-11 21:36:00 +08:00
2022-03-16 21:19:51 +08:00
pub fn modify_block<F>(&mut self, block_id: &str, f: F) -> CollaborateResult<Option<GridChangeset>>
2022-03-11 21:36:00 +08:00
where
2022-03-17 17:25:43 +08:00
F: FnOnce(&mut GridBlockMeta) -> CollaborateResult<Option<()>>,
2022-03-11 21:36:00 +08:00
{
2022-03-17 17:25:43 +08:00
self.modify_grid(
2022-03-18 17:14:46 +08:00
|grid| match grid.block_metas.iter().position(|block| block.block_id == block_id) {
2022-03-17 17:25:43 +08:00
None => {
tracing::warn!("[GridMetaPad]: Can't find any block with id: {}", block_id);
Ok(None)
}
2022-03-18 17:14:46 +08:00
Some(index) => f(&mut grid.block_metas[index]),
2022-03-17 17:25:43 +08:00
},
)
2022-03-11 21:36:00 +08:00
}
2022-03-16 21:19:51 +08:00
pub fn modify_field<F>(&mut self, field_id: &str, f: F) -> CollaborateResult<Option<GridChangeset>>
2022-03-11 21:36:00 +08:00
where
2022-03-15 11:07:18 +08:00
F: FnOnce(&mut FieldMeta) -> CollaborateResult<Option<()>>,
2022-03-11 21:36:00 +08:00
{
self.modify_grid(|grid| match grid.fields.iter().position(|field| field.id == field_id) {
None => {
tracing::warn!("[GridMetaPad]: Can't find any field with id: {}", field_id);
Ok(None)
}
Some(index) => f(&mut grid.fields[index]),
})
}
2022-03-04 18:11:12 +08:00
}
2022-03-10 12:01:31 +08:00
fn json_from_grid(grid: &Arc<GridMeta>) -> CollaborateResult<String> {
2022-03-04 18:11:12 +08:00
let json = serde_json::to_string(grid)
.map_err(|err| internal_error(format!("Serialize grid to json str failed. {:?}", err)))?;
Ok(json)
}
2022-03-16 21:19:51 +08:00
pub struct GridChangeset {
2022-03-12 09:30:13 +08:00
pub delta: GridMetaDelta,
2022-03-04 18:11:12 +08:00
/// md5: the md5 of the grid after applying the change.
pub md5: String,
}
2022-03-12 09:30:13 +08:00
pub fn make_grid_delta(grid_meta: &GridMeta) -> GridMetaDelta {
2022-03-10 12:01:31 +08:00
let json = serde_json::to_string(&grid_meta).unwrap();
2022-03-04 18:11:12 +08:00
PlainTextDeltaBuilder::new().insert(&json).build()
}
2022-03-10 12:01:31 +08:00
pub fn make_grid_revisions(user_id: &str, grid_meta: &GridMeta) -> RepeatedRevision {
let delta = make_grid_delta(grid_meta);
2022-03-15 19:00:28 +08:00
let bytes = delta.to_delta_bytes();
2022-03-10 12:01:31 +08:00
let revision = Revision::initial_revision(user_id, &grid_meta.grid_id, bytes);
2022-03-05 10:59:44 +08:00
revision.into()
}
2022-03-10 12:01:31 +08:00
impl std::default::Default for GridMetaPad {
2022-03-04 18:11:12 +08:00
fn default() -> Self {
2022-03-10 12:01:31 +08:00
let grid = GridMeta {
grid_id: uuid(),
fields: vec![],
2022-03-18 17:14:46 +08:00
block_metas: vec![],
2022-03-04 18:11:12 +08:00
};
2022-03-05 10:59:44 +08:00
let delta = make_grid_delta(&grid);
2022-03-10 12:01:31 +08:00
GridMetaPad {
grid_meta: Arc::new(grid),
2022-03-04 18:11:12 +08:00
delta,
}
}
}