2022-03-13 11:06:28 +08:00
|
|
|
use crate::manager::GridUser;
|
2022-03-16 21:19:51 +08:00
|
|
|
use crate::services::row::{make_cell, make_row_ids_per_block, make_rows};
|
2022-03-11 21:36:00 +08:00
|
|
|
use bytes::Bytes;
|
2022-03-13 23:16:52 +08:00
|
|
|
|
2022-03-13 11:06:28 +08:00
|
|
|
use dashmap::DashMap;
|
2022-03-11 21:36:00 +08:00
|
|
|
use flowy_collaboration::client_grid::{GridBlockMetaChange, GridBlockMetaPad};
|
|
|
|
|
use flowy_collaboration::entities::revision::Revision;
|
|
|
|
|
use flowy_collaboration::util::make_delta_from_revisions;
|
|
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
2022-03-14 17:24:25 +08:00
|
|
|
use flowy_grid_data_model::entities::{
|
2022-03-16 21:19:51 +08:00
|
|
|
Cell, FieldMeta, GridBlock, GridBlockChangeset, RepeatedCell, RepeatedRow, RepeatedRowOrder, RowMeta,
|
|
|
|
|
RowMetaChangeset, RowOrder,
|
2022-03-14 17:24:25 +08:00
|
|
|
};
|
2022-03-13 11:06:28 +08:00
|
|
|
use flowy_sync::disk::SQLiteGridBlockMetaRevisionPersistence;
|
|
|
|
|
use flowy_sync::{
|
|
|
|
|
RevisionCloudService, RevisionCompactor, RevisionManager, RevisionObjectBuilder, RevisionPersistence,
|
|
|
|
|
};
|
2022-03-11 21:36:00 +08:00
|
|
|
use lib_infra::future::FutureResult;
|
|
|
|
|
use lib_ot::core::PlainTextAttributes;
|
2022-03-13 23:16:52 +08:00
|
|
|
|
2022-03-13 11:06:28 +08:00
|
|
|
use std::collections::HashMap;
|
2022-03-14 17:24:25 +08:00
|
|
|
|
2022-03-16 21:19:51 +08:00
|
|
|
use crate::dart_notification::{send_dart_notification, GridNotification};
|
2022-03-11 21:36:00 +08:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
use tokio::sync::RwLock;
|
|
|
|
|
|
2022-03-14 17:24:25 +08:00
|
|
|
type RowId = String;
|
|
|
|
|
type BlockId = String;
|
|
|
|
|
|
2022-03-13 11:06:28 +08:00
|
|
|
pub(crate) struct GridBlockMetaEditorManager {
|
2022-03-16 21:19:51 +08:00
|
|
|
grid_id: String,
|
2022-03-13 11:06:28 +08:00
|
|
|
user: Arc<dyn GridUser>,
|
|
|
|
|
editor_map: DashMap<String, Arc<ClientGridBlockMetaEditor>>,
|
2022-03-14 17:24:25 +08:00
|
|
|
block_id_by_row_id: DashMap<BlockId, RowId>,
|
2022-03-13 11:06:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl GridBlockMetaEditorManager {
|
2022-03-16 21:19:51 +08:00
|
|
|
pub(crate) async fn new(grid_id: &str, user: &Arc<dyn GridUser>, blocks: Vec<GridBlock>) -> FlowyResult<Self> {
|
2022-03-13 11:06:28 +08:00
|
|
|
let editor_map = make_block_meta_editor_map(user, blocks).await?;
|
|
|
|
|
let user = user.clone();
|
2022-03-14 17:24:25 +08:00
|
|
|
let block_id_by_row_id = DashMap::new();
|
2022-03-16 21:19:51 +08:00
|
|
|
let grid_id = grid_id.to_owned();
|
2022-03-14 17:24:25 +08:00
|
|
|
let manager = Self {
|
2022-03-16 21:19:51 +08:00
|
|
|
grid_id,
|
2022-03-14 17:24:25 +08:00
|
|
|
user,
|
|
|
|
|
editor_map,
|
|
|
|
|
block_id_by_row_id,
|
|
|
|
|
};
|
2022-03-13 11:06:28 +08:00
|
|
|
Ok(manager)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) async fn get_editor(&self, block_id: &str) -> FlowyResult<Arc<ClientGridBlockMetaEditor>> {
|
|
|
|
|
match self.editor_map.get(block_id) {
|
|
|
|
|
None => {
|
|
|
|
|
tracing::error!("The is a fatal error, block is not exist");
|
|
|
|
|
let editor = Arc::new(make_block_meta_editor(&self.user, block_id).await?);
|
|
|
|
|
self.editor_map.insert(block_id.to_owned(), editor.clone());
|
|
|
|
|
Ok(editor)
|
|
|
|
|
}
|
|
|
|
|
Some(editor) => Ok(editor.clone()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-16 21:19:51 +08:00
|
|
|
pub(crate) async fn create_row(
|
|
|
|
|
&self,
|
|
|
|
|
field_metas: &[FieldMeta],
|
|
|
|
|
row_meta: RowMeta,
|
|
|
|
|
upper_row_id: Option<String>,
|
|
|
|
|
) -> FlowyResult<i32> {
|
|
|
|
|
self.block_id_by_row_id
|
|
|
|
|
.insert(row_meta.id.clone(), row_meta.block_id.clone());
|
|
|
|
|
let editor = self.get_editor(&row_meta.block_id).await?;
|
|
|
|
|
|
|
|
|
|
let rows = make_rows(field_metas, vec![row_meta.clone().into()]);
|
|
|
|
|
send_dart_notification(&self.grid_id, GridNotification::GridDidCreateRows)
|
|
|
|
|
.payload(RepeatedRow::from(rows))
|
|
|
|
|
.send();
|
|
|
|
|
|
|
|
|
|
self.notify_did_create_rows(field_metas, vec![row_meta.clone()]);
|
|
|
|
|
|
|
|
|
|
editor.create_row(row_meta, upper_row_id).await
|
2022-03-13 11:06:28 +08:00
|
|
|
}
|
|
|
|
|
|
2022-03-14 17:24:25 +08:00
|
|
|
pub(crate) async fn insert_row(
|
|
|
|
|
&self,
|
2022-03-16 21:19:51 +08:00
|
|
|
field_metas: &[FieldMeta],
|
2022-03-14 17:24:25 +08:00
|
|
|
rows_by_block_id: HashMap<String, Vec<RowMeta>>,
|
|
|
|
|
) -> FlowyResult<Vec<GridBlockChangeset>> {
|
|
|
|
|
let mut changesets = vec![];
|
2022-03-16 21:19:51 +08:00
|
|
|
for (block_id, row_metas) in rows_by_block_id {
|
2022-03-14 17:24:25 +08:00
|
|
|
let editor = self.get_editor(&block_id).await?;
|
|
|
|
|
let mut row_count = 0;
|
2022-03-16 21:19:51 +08:00
|
|
|
for row in &row_metas {
|
2022-03-14 17:24:25 +08:00
|
|
|
self.block_id_by_row_id.insert(row.id.clone(), row.block_id.clone());
|
2022-03-16 21:19:51 +08:00
|
|
|
row_count = editor.create_row(row.clone(), None).await?;
|
2022-03-14 17:24:25 +08:00
|
|
|
}
|
|
|
|
|
changesets.push(GridBlockChangeset::from_row_count(&block_id, row_count));
|
2022-03-16 21:19:51 +08:00
|
|
|
self.notify_did_create_rows(field_metas, row_metas);
|
2022-03-14 17:24:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(changesets)
|
2022-03-13 11:06:28 +08:00
|
|
|
}
|
|
|
|
|
|
2022-03-14 17:24:25 +08:00
|
|
|
pub(crate) async fn delete_rows(&self, row_ids: Vec<String>) -> FlowyResult<Vec<GridBlockChangeset>> {
|
|
|
|
|
let row_orders = row_ids
|
|
|
|
|
.into_iter()
|
2022-03-14 23:16:25 +08:00
|
|
|
.flat_map(|row_id| {
|
|
|
|
|
self.block_id_by_row_id.get(&row_id).map(|block_id| RowOrder {
|
2022-03-14 17:24:25 +08:00
|
|
|
row_id,
|
|
|
|
|
block_id: block_id.clone(),
|
2022-03-14 23:16:25 +08:00
|
|
|
})
|
2022-03-14 17:24:25 +08:00
|
|
|
})
|
|
|
|
|
.collect::<Vec<RowOrder>>();
|
|
|
|
|
let mut changesets = vec![];
|
|
|
|
|
let row_ids_per_blocks = make_row_ids_per_block(&row_orders);
|
|
|
|
|
for row_ids_per_block in row_ids_per_blocks {
|
|
|
|
|
let editor = self.get_editor(&row_ids_per_block.block_id).await?;
|
|
|
|
|
let row_count = editor.delete_rows(row_ids_per_block.row_ids).await?;
|
|
|
|
|
|
|
|
|
|
let changeset = GridBlockChangeset::from_row_count(&row_ids_per_block.block_id, row_count);
|
|
|
|
|
changesets.push(changeset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(changesets)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn update_row(&self, changeset: RowMetaChangeset) -> FlowyResult<()> {
|
2022-03-16 21:19:51 +08:00
|
|
|
let editor = self.get_editor_from_row_id(&changeset.row_id).await?;
|
|
|
|
|
let _ = editor.update_row(changeset.clone()).await?;
|
|
|
|
|
let _ = self.notify_did_update_row()?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn update_cells(&self, field_metas: &[FieldMeta], changeset: RowMetaChangeset) -> FlowyResult<()> {
|
|
|
|
|
let editor = self.get_editor_from_row_id(&changeset.row_id).await?;
|
|
|
|
|
let _ = editor.update_row(changeset.clone()).await?;
|
|
|
|
|
self.notify_did_update_cells(changeset, field_metas)?;
|
|
|
|
|
Ok(())
|
2022-03-14 17:24:25 +08:00
|
|
|
}
|
|
|
|
|
|
2022-03-15 11:07:18 +08:00
|
|
|
pub(crate) async fn get_all_rows(&self, grid_blocks: Vec<GridBlock>) -> FlowyResult<Vec<Arc<RowMeta>>> {
|
2022-03-14 17:24:25 +08:00
|
|
|
let mut row_metas = vec![];
|
2022-03-13 23:16:52 +08:00
|
|
|
for grid_block in grid_blocks {
|
|
|
|
|
let editor = self.get_editor(&grid_block.id).await?;
|
2022-03-15 11:07:18 +08:00
|
|
|
let new_row_metas = editor.get_row_metas(None).await?;
|
2022-03-14 17:24:25 +08:00
|
|
|
new_row_metas.iter().for_each(|row_meta| {
|
|
|
|
|
self.block_id_by_row_id
|
|
|
|
|
.insert(row_meta.id.clone(), row_meta.block_id.clone());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
row_metas.extend(new_row_metas);
|
2022-03-13 23:16:52 +08:00
|
|
|
}
|
2022-03-14 17:24:25 +08:00
|
|
|
Ok(row_metas)
|
2022-03-13 23:16:52 +08:00
|
|
|
}
|
|
|
|
|
|
2022-03-15 11:07:18 +08:00
|
|
|
pub(crate) async fn get_row_orders(&self, grid_blocks: Vec<GridBlock>) -> FlowyResult<Vec<RowOrder>> {
|
|
|
|
|
let mut row_orders = vec![];
|
|
|
|
|
for grid_block in grid_blocks {
|
|
|
|
|
let editor = self.get_editor(&grid_block.id).await?;
|
|
|
|
|
let row_metas = editor.get_row_metas(None).await?;
|
2022-03-16 10:02:37 +08:00
|
|
|
let block_row_orders = row_metas.iter().map(RowOrder::from);
|
2022-03-15 11:07:18 +08:00
|
|
|
row_orders.extend(block_row_orders);
|
|
|
|
|
}
|
|
|
|
|
Ok(row_orders)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) async fn get_rows(&self, row_orders: &RepeatedRowOrder) -> FlowyResult<Vec<Arc<RowMeta>>> {
|
2022-03-14 17:24:25 +08:00
|
|
|
let row_ids_per_blocks = make_row_ids_per_block(row_orders);
|
|
|
|
|
let mut row_metas = vec![];
|
2022-03-13 23:16:52 +08:00
|
|
|
for row_ids_per_block in row_ids_per_blocks {
|
|
|
|
|
let editor = self.get_editor(&row_ids_per_block.block_id).await?;
|
2022-03-15 11:07:18 +08:00
|
|
|
let new_row_metas = editor.get_row_metas(Some(row_ids_per_block.row_ids)).await?;
|
2022-03-14 17:24:25 +08:00
|
|
|
new_row_metas.iter().for_each(|row_meta| {
|
|
|
|
|
self.block_id_by_row_id
|
|
|
|
|
.insert(row_meta.id.clone(), row_meta.block_id.clone());
|
|
|
|
|
});
|
|
|
|
|
row_metas.extend(new_row_metas);
|
2022-03-13 11:06:28 +08:00
|
|
|
}
|
2022-03-14 17:24:25 +08:00
|
|
|
Ok(row_metas)
|
2022-03-13 11:06:28 +08:00
|
|
|
}
|
2022-03-16 21:19:51 +08:00
|
|
|
|
|
|
|
|
async fn get_editor_from_row_id(&self, row_id: &str) -> FlowyResult<Arc<ClientGridBlockMetaEditor>> {
|
|
|
|
|
match self.block_id_by_row_id.get(row_id) {
|
|
|
|
|
None => {
|
|
|
|
|
let msg = format!(
|
|
|
|
|
"Update Row failed. Can't find the corresponding block with row_id: {}",
|
|
|
|
|
row_id
|
|
|
|
|
);
|
|
|
|
|
Err(FlowyError::internal().context(msg))
|
|
|
|
|
}
|
|
|
|
|
Some(block_id) => {
|
|
|
|
|
let editor = self.get_editor(&block_id).await?;
|
|
|
|
|
Ok(editor)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn notify_did_create_rows(&self, field_metas: &[FieldMeta], row_metas: Vec<RowMeta>) {
|
|
|
|
|
let rows = make_rows(
|
|
|
|
|
field_metas,
|
|
|
|
|
row_metas
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|row_meta| Arc::new(row_meta))
|
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
|
);
|
|
|
|
|
send_dart_notification(&self.grid_id, GridNotification::GridDidCreateRows)
|
|
|
|
|
.payload(RepeatedRow::from(rows))
|
|
|
|
|
.send();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn notify_did_update_row(&self) -> FlowyResult<()> {
|
|
|
|
|
// send_dart_notification(&changeset.row_id, GridNotification::GridDidUpdateRows)
|
|
|
|
|
// .payload(RepeatedRow::from(cells))
|
|
|
|
|
// .send();
|
|
|
|
|
|
|
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn notify_did_update_cells(&self, changeset: RowMetaChangeset, field_metas: &[FieldMeta]) -> FlowyResult<()> {
|
|
|
|
|
let field_meta_map = field_metas
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|field_meta| (&field_meta.id, field_meta))
|
|
|
|
|
.collect::<HashMap<&String, &FieldMeta>>();
|
|
|
|
|
|
|
|
|
|
let mut cells = vec![];
|
|
|
|
|
changeset
|
|
|
|
|
.cell_by_field_id
|
|
|
|
|
.into_iter()
|
|
|
|
|
.for_each(
|
|
|
|
|
|(field_id, cell_meta)| match make_cell(&field_meta_map, field_id, cell_meta) {
|
|
|
|
|
None => {}
|
|
|
|
|
Some((_, cell)) => cells.push(cell),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if !cells.is_empty() {
|
|
|
|
|
send_dart_notification(&changeset.row_id, GridNotification::GridDidUpdateCells)
|
|
|
|
|
.payload(RepeatedCell::from(cells))
|
|
|
|
|
.send();
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2022-03-13 11:06:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn make_block_meta_editor_map(
|
|
|
|
|
user: &Arc<dyn GridUser>,
|
|
|
|
|
blocks: Vec<GridBlock>,
|
|
|
|
|
) -> FlowyResult<DashMap<String, Arc<ClientGridBlockMetaEditor>>> {
|
|
|
|
|
let editor_map = DashMap::new();
|
|
|
|
|
for block in blocks {
|
|
|
|
|
let editor = make_block_meta_editor(user, &block.id).await?;
|
|
|
|
|
editor_map.insert(block.id, Arc::new(editor));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(editor_map)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn make_block_meta_editor(user: &Arc<dyn GridUser>, block_id: &str) -> FlowyResult<ClientGridBlockMetaEditor> {
|
|
|
|
|
let token = user.token()?;
|
|
|
|
|
let user_id = user.user_id()?;
|
|
|
|
|
let pool = user.db_pool()?;
|
|
|
|
|
|
|
|
|
|
let disk_cache = Arc::new(SQLiteGridBlockMetaRevisionPersistence::new(&user_id, pool));
|
|
|
|
|
let rev_persistence = Arc::new(RevisionPersistence::new(&user_id, block_id, disk_cache));
|
|
|
|
|
let rev_manager = RevisionManager::new(&user_id, block_id, rev_persistence);
|
|
|
|
|
ClientGridBlockMetaEditor::new(&user_id, &token, block_id, rev_manager).await
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-11 21:36:00 +08:00
|
|
|
pub struct ClientGridBlockMetaEditor {
|
|
|
|
|
user_id: String,
|
2022-03-12 09:30:13 +08:00
|
|
|
pub block_id: String,
|
2022-03-11 21:36:00 +08:00
|
|
|
meta_pad: Arc<RwLock<GridBlockMetaPad>>,
|
|
|
|
|
rev_manager: Arc<RevisionManager>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ClientGridBlockMetaEditor {
|
|
|
|
|
pub async fn new(
|
|
|
|
|
user_id: &str,
|
|
|
|
|
token: &str,
|
2022-03-12 09:30:13 +08:00
|
|
|
block_id: &str,
|
2022-03-11 21:36:00 +08:00
|
|
|
mut rev_manager: RevisionManager,
|
|
|
|
|
) -> FlowyResult<Self> {
|
|
|
|
|
let cloud = Arc::new(GridBlockMetaRevisionCloudService {
|
|
|
|
|
token: token.to_owned(),
|
|
|
|
|
});
|
2022-03-12 21:06:15 +08:00
|
|
|
let block_meta_pad = rev_manager.load::<GridBlockMetaPadBuilder>(Some(cloud)).await?;
|
2022-03-11 21:36:00 +08:00
|
|
|
let meta_pad = Arc::new(RwLock::new(block_meta_pad));
|
|
|
|
|
let rev_manager = Arc::new(rev_manager);
|
|
|
|
|
let user_id = user_id.to_owned();
|
2022-03-12 09:30:13 +08:00
|
|
|
let block_id = block_id.to_owned();
|
2022-03-11 21:36:00 +08:00
|
|
|
Ok(Self {
|
|
|
|
|
user_id,
|
|
|
|
|
block_id,
|
|
|
|
|
meta_pad,
|
|
|
|
|
rev_manager,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-16 16:10:35 +08:00
|
|
|
async fn create_row(&self, row: RowMeta, upper_row_id: Option<String>) -> FlowyResult<i32> {
|
2022-03-13 11:06:28 +08:00
|
|
|
let mut row_count = 0;
|
|
|
|
|
let _ = self
|
|
|
|
|
.modify(|pad| {
|
2022-03-16 16:10:35 +08:00
|
|
|
let change = pad.add_row(row, upper_row_id)?;
|
2022-03-13 11:06:28 +08:00
|
|
|
row_count = pad.number_of_rows();
|
|
|
|
|
Ok(change)
|
|
|
|
|
})
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
Ok(row_count)
|
2022-03-11 21:36:00 +08:00
|
|
|
}
|
|
|
|
|
|
2022-03-14 17:24:25 +08:00
|
|
|
pub async fn delete_rows(&self, ids: Vec<String>) -> FlowyResult<i32> {
|
|
|
|
|
let mut row_count = 0;
|
|
|
|
|
let _ = self
|
|
|
|
|
.modify(|pad| {
|
|
|
|
|
let changeset = pad.delete_rows(&ids)?;
|
|
|
|
|
row_count = pad.number_of_rows();
|
|
|
|
|
Ok(changeset)
|
|
|
|
|
})
|
|
|
|
|
.await?;
|
|
|
|
|
Ok(row_count)
|
2022-03-11 21:36:00 +08:00
|
|
|
}
|
|
|
|
|
|
2022-03-12 09:30:13 +08:00
|
|
|
pub async fn update_row(&self, changeset: RowMetaChangeset) -> FlowyResult<()> {
|
|
|
|
|
let _ = self.modify(|pad| Ok(pad.update_row(changeset)?)).await?;
|
2022-03-11 21:36:00 +08:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 11:07:18 +08:00
|
|
|
pub async fn get_row_metas(&self, row_ids: Option<Vec<String>>) -> FlowyResult<Vec<Arc<RowMeta>>> {
|
|
|
|
|
let row_metas = self.meta_pad.read().await.get_rows(row_ids)?;
|
|
|
|
|
Ok(row_metas)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn get_row_orders(&self) -> FlowyResult<Vec<RowOrder>> {
|
|
|
|
|
let row_orders = self
|
|
|
|
|
.meta_pad
|
|
|
|
|
.read()
|
|
|
|
|
.await
|
|
|
|
|
.get_rows(None)?
|
|
|
|
|
.iter()
|
2022-03-16 10:02:37 +08:00
|
|
|
.map(RowOrder::from)
|
2022-03-15 11:07:18 +08:00
|
|
|
.collect::<Vec<RowOrder>>();
|
|
|
|
|
Ok(row_orders)
|
2022-03-12 09:30:13 +08:00
|
|
|
}
|
|
|
|
|
|
2022-03-11 21:36:00 +08:00
|
|
|
async fn modify<F>(&self, f: F) -> FlowyResult<()>
|
|
|
|
|
where
|
|
|
|
|
F: for<'a> FnOnce(&'a mut GridBlockMetaPad) -> FlowyResult<Option<GridBlockMetaChange>>,
|
|
|
|
|
{
|
|
|
|
|
let mut write_guard = self.meta_pad.write().await;
|
|
|
|
|
match f(&mut *write_guard)? {
|
|
|
|
|
None => {}
|
|
|
|
|
Some(change) => {
|
|
|
|
|
let _ = self.apply_change(change).await?;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn apply_change(&self, change: GridBlockMetaChange) -> FlowyResult<()> {
|
|
|
|
|
let GridBlockMetaChange { delta, md5 } = change;
|
|
|
|
|
let user_id = self.user_id.clone();
|
|
|
|
|
let (base_rev_id, rev_id) = self.rev_manager.next_rev_id_pair();
|
2022-03-15 19:00:28 +08:00
|
|
|
let delta_data = delta.to_delta_bytes();
|
2022-03-11 21:36:00 +08:00
|
|
|
let revision = Revision::new(
|
|
|
|
|
&self.rev_manager.object_id,
|
|
|
|
|
base_rev_id,
|
|
|
|
|
rev_id,
|
|
|
|
|
delta_data,
|
|
|
|
|
&user_id,
|
|
|
|
|
md5,
|
|
|
|
|
);
|
|
|
|
|
let _ = self
|
|
|
|
|
.rev_manager
|
|
|
|
|
.add_local_revision(&revision, Box::new(GridBlockMetaRevisionCompactor()))
|
|
|
|
|
.await?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct GridBlockMetaRevisionCloudService {
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
|
token: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl RevisionCloudService for GridBlockMetaRevisionCloudService {
|
|
|
|
|
#[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![]) })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct GridBlockMetaPadBuilder();
|
|
|
|
|
impl RevisionObjectBuilder for GridBlockMetaPadBuilder {
|
|
|
|
|
type Output = GridBlockMetaPad;
|
|
|
|
|
|
|
|
|
|
fn build_object(object_id: &str, revisions: Vec<Revision>) -> FlowyResult<Self::Output> {
|
|
|
|
|
let pad = GridBlockMetaPad::from_revisions(object_id, revisions)?;
|
|
|
|
|
Ok(pad)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct GridBlockMetaRevisionCompactor();
|
|
|
|
|
impl RevisionCompactor for GridBlockMetaRevisionCompactor {
|
|
|
|
|
fn bytes_from_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes> {
|
|
|
|
|
let delta = make_delta_from_revisions::<PlainTextAttributes>(revisions)?;
|
2022-03-15 19:00:28 +08:00
|
|
|
Ok(delta.to_delta_bytes())
|
2022-03-11 21:36:00 +08:00
|
|
|
}
|
|
|
|
|
}
|