2022-04-07 07:34:00 +00:00
|
|
|
use crate::dart_notification::{send_dart_notification, GridNotification};
|
|
|
|
use crate::manager::GridUser;
|
2022-06-15 07:13:50 +00:00
|
|
|
use crate::services::block_revision_editor::GridBlockRevisionEditor;
|
2022-05-26 09:28:44 +00:00
|
|
|
use crate::services::persistence::block_index::BlockIndexCache;
|
2022-06-24 07:23:39 +00:00
|
|
|
use crate::services::row::{block_from_row_orders, GridBlockSnapshot};
|
2022-04-07 07:34:00 +00:00
|
|
|
use dashmap::DashMap;
|
2022-04-25 14:03:10 +00:00
|
|
|
use flowy_error::FlowyResult;
|
2022-04-07 07:34:00 +00:00
|
|
|
use flowy_grid_data_model::entities::{
|
2022-06-15 07:13:50 +00:00
|
|
|
CellChangeset, GridRowsChangeset, IndexRowOrder, Row, RowOrder, UpdatedRowOrder,
|
|
|
|
};
|
|
|
|
use flowy_grid_data_model::revision::{
|
2022-06-26 07:14:24 +00:00
|
|
|
CellRevision, GridBlockMetaRevision, GridBlockMetaRevisionChangeset, RowMetaChangeset, RowRevision,
|
2022-04-07 07:34:00 +00:00
|
|
|
};
|
|
|
|
use flowy_revision::disk::SQLiteGridBlockMetaRevisionPersistence;
|
|
|
|
use flowy_revision::{RevisionManager, RevisionPersistence};
|
2022-04-25 14:03:10 +00:00
|
|
|
use std::borrow::Cow;
|
2022-04-07 07:34:00 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-05-26 09:28:44 +00:00
|
|
|
type BlockId = String;
|
|
|
|
pub(crate) struct GridBlockManager {
|
2022-06-24 10:13:40 +00:00
|
|
|
#[allow(dead_code)]
|
2022-04-07 07:34:00 +00:00
|
|
|
grid_id: String,
|
|
|
|
user: Arc<dyn GridUser>,
|
2022-05-26 09:28:44 +00:00
|
|
|
persistence: Arc<BlockIndexCache>,
|
2022-06-15 07:13:50 +00:00
|
|
|
block_editor_map: DashMap<BlockId, Arc<GridBlockRevisionEditor>>,
|
2022-04-07 07:34:00 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 09:28:44 +00:00
|
|
|
impl GridBlockManager {
|
2022-04-07 07:34:00 +00:00
|
|
|
pub(crate) async fn new(
|
|
|
|
grid_id: &str,
|
|
|
|
user: &Arc<dyn GridUser>,
|
2022-06-26 07:14:24 +00:00
|
|
|
block_revs: Vec<Arc<GridBlockMetaRevision>>,
|
2022-05-26 09:28:44 +00:00
|
|
|
persistence: Arc<BlockIndexCache>,
|
2022-04-07 07:34:00 +00:00
|
|
|
) -> FlowyResult<Self> {
|
2022-06-24 10:13:40 +00:00
|
|
|
let editor_map = make_block_meta_editor_map(user, block_revs).await?;
|
2022-04-07 07:34:00 +00:00
|
|
|
let user = user.clone();
|
|
|
|
let grid_id = grid_id.to_owned();
|
|
|
|
let manager = Self {
|
|
|
|
grid_id,
|
|
|
|
user,
|
2022-05-26 09:28:44 +00:00
|
|
|
block_editor_map: editor_map,
|
2022-04-07 07:34:00 +00:00
|
|
|
persistence,
|
|
|
|
};
|
|
|
|
Ok(manager)
|
|
|
|
}
|
|
|
|
|
|
|
|
// #[tracing::instrument(level = "trace", skip(self))]
|
2022-06-15 07:13:50 +00:00
|
|
|
pub(crate) async fn get_editor(&self, block_id: &str) -> FlowyResult<Arc<GridBlockRevisionEditor>> {
|
2022-04-07 07:34:00 +00:00
|
|
|
debug_assert!(!block_id.is_empty());
|
2022-05-26 09:28:44 +00:00
|
|
|
match self.block_editor_map.get(block_id) {
|
2022-04-07 07:34:00 +00:00
|
|
|
None => {
|
2022-06-06 12:06:08 +00:00
|
|
|
tracing::error!("This is a fatal error, block with id:{} is not exist", block_id);
|
2022-04-07 07:34:00 +00:00
|
|
|
let editor = Arc::new(make_block_meta_editor(&self.user, block_id).await?);
|
2022-05-26 09:28:44 +00:00
|
|
|
self.block_editor_map.insert(block_id.to_owned(), editor.clone());
|
2022-04-07 07:34:00 +00:00
|
|
|
Ok(editor)
|
|
|
|
}
|
|
|
|
Some(editor) => Ok(editor.clone()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-15 07:13:50 +00:00
|
|
|
async fn get_editor_from_row_id(&self, row_id: &str) -> FlowyResult<Arc<GridBlockRevisionEditor>> {
|
2022-04-07 07:34:00 +00:00
|
|
|
let block_id = self.persistence.get_block_id(row_id)?;
|
|
|
|
Ok(self.get_editor(&block_id).await?)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn create_row(
|
|
|
|
&self,
|
|
|
|
block_id: &str,
|
2022-06-15 07:13:50 +00:00
|
|
|
row_rev: RowRevision,
|
2022-04-07 07:34:00 +00:00
|
|
|
start_row_id: Option<String>,
|
|
|
|
) -> FlowyResult<i32> {
|
2022-06-15 07:13:50 +00:00
|
|
|
let _ = self.persistence.insert(&row_rev.block_id, &row_rev.id)?;
|
|
|
|
let editor = self.get_editor(&row_rev.block_id).await?;
|
2022-04-10 06:24:12 +00:00
|
|
|
|
2022-06-15 07:13:50 +00:00
|
|
|
let mut index_row_order = IndexRowOrder::from(&row_rev);
|
|
|
|
let (row_count, row_index) = editor.create_row(row_rev, start_row_id).await?;
|
2022-04-10 06:24:12 +00:00
|
|
|
index_row_order.index = row_index;
|
2022-04-10 02:31:55 +00:00
|
|
|
|
|
|
|
let _ = self
|
2022-06-24 07:23:39 +00:00
|
|
|
.notify_did_update_block(block_id, GridRowsChangeset::insert(block_id, vec![index_row_order]))
|
2022-04-10 02:31:55 +00:00
|
|
|
.await?;
|
2022-04-07 07:34:00 +00:00
|
|
|
Ok(row_count)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn insert_row(
|
|
|
|
&self,
|
2022-06-15 07:13:50 +00:00
|
|
|
rows_by_block_id: HashMap<String, Vec<RowRevision>>,
|
2022-06-26 07:14:24 +00:00
|
|
|
) -> FlowyResult<Vec<GridBlockMetaRevisionChangeset>> {
|
2022-04-07 07:34:00 +00:00
|
|
|
let mut changesets = vec![];
|
2022-06-15 07:13:50 +00:00
|
|
|
for (block_id, row_revs) in rows_by_block_id {
|
2022-04-10 02:31:55 +00:00
|
|
|
let mut inserted_row_orders = vec![];
|
2022-04-07 07:34:00 +00:00
|
|
|
let editor = self.get_editor(&block_id).await?;
|
|
|
|
let mut row_count = 0;
|
2022-06-15 07:13:50 +00:00
|
|
|
for row in row_revs {
|
2022-05-26 09:28:44 +00:00
|
|
|
let _ = self.persistence.insert(&row.block_id, &row.id)?;
|
2022-04-21 13:24:28 +00:00
|
|
|
let mut row_order = IndexRowOrder::from(&row);
|
|
|
|
let (count, index) = editor.create_row(row, None).await?;
|
|
|
|
row_count = count;
|
|
|
|
row_order.index = index;
|
|
|
|
inserted_row_orders.push(row_order);
|
2022-04-07 07:34:00 +00:00
|
|
|
}
|
2022-06-26 07:14:24 +00:00
|
|
|
changesets.push(GridBlockMetaRevisionChangeset::from_row_count(&block_id, row_count));
|
2022-04-10 06:24:12 +00:00
|
|
|
|
2022-04-10 02:31:55 +00:00
|
|
|
let _ = self
|
2022-06-24 07:23:39 +00:00
|
|
|
.notify_did_update_block(&block_id, GridRowsChangeset::insert(&block_id, inserted_row_orders))
|
2022-04-10 02:31:55 +00:00
|
|
|
.await?;
|
2022-04-07 07:34:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(changesets)
|
|
|
|
}
|
|
|
|
|
2022-04-25 00:13:09 +00:00
|
|
|
pub async fn update_row<F>(&self, changeset: RowMetaChangeset, row_builder: F) -> FlowyResult<()>
|
|
|
|
where
|
2022-06-15 07:13:50 +00:00
|
|
|
F: FnOnce(Arc<RowRevision>) -> Option<Row>,
|
2022-04-25 00:13:09 +00:00
|
|
|
{
|
2022-04-10 00:25:01 +00:00
|
|
|
let editor = self.get_editor_from_row_id(&changeset.row_id).await?;
|
|
|
|
let _ = editor.update_row(changeset.clone()).await?;
|
2022-06-15 07:13:50 +00:00
|
|
|
match editor.get_row_rev(&changeset.row_id).await? {
|
2022-04-25 00:13:09 +00:00
|
|
|
None => tracing::error!("Internal error: can't find the row with id: {}", changeset.row_id),
|
2022-06-15 07:13:50 +00:00
|
|
|
Some(row_rev) => {
|
|
|
|
if let Some(row) = row_builder(row_rev.clone()) {
|
|
|
|
let row_order = UpdatedRowOrder::new(&row_rev, row);
|
2022-04-25 00:13:09 +00:00
|
|
|
let block_order_changeset = GridRowsChangeset::update(&editor.block_id, vec![row_order]);
|
2022-06-24 07:23:39 +00:00
|
|
|
let _ = self
|
|
|
|
.notify_did_update_block(&editor.block_id, block_order_changeset)
|
|
|
|
.await?;
|
2022-04-25 00:13:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-10 00:25:01 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-04-10 06:24:12 +00:00
|
|
|
pub async fn delete_row(&self, row_id: &str) -> FlowyResult<()> {
|
|
|
|
let row_id = row_id.to_owned();
|
|
|
|
let block_id = self.persistence.get_block_id(&row_id)?;
|
|
|
|
let editor = self.get_editor(&block_id).await?;
|
2022-04-24 15:20:28 +00:00
|
|
|
match editor.get_row_order(&row_id).await? {
|
|
|
|
None => {}
|
|
|
|
Some(row_order) => {
|
|
|
|
let _ = editor.delete_rows(vec![Cow::Borrowed(&row_id)]).await?;
|
|
|
|
let _ = self
|
2022-06-24 07:23:39 +00:00
|
|
|
.notify_did_update_block(&block_id, GridRowsChangeset::delete(&block_id, vec![row_order]))
|
2022-04-24 15:20:28 +00:00
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
}
|
2022-04-10 06:24:12 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-06-26 07:14:24 +00:00
|
|
|
pub(crate) async fn delete_rows(
|
|
|
|
&self,
|
|
|
|
row_orders: Vec<RowOrder>,
|
|
|
|
) -> FlowyResult<Vec<GridBlockMetaRevisionChangeset>> {
|
2022-04-07 07:34:00 +00:00
|
|
|
let mut changesets = vec![];
|
2022-06-24 07:23:39 +00:00
|
|
|
for block_order in block_from_row_orders(row_orders) {
|
|
|
|
let editor = self.get_editor(&block_order.id).await?;
|
2022-04-10 00:25:01 +00:00
|
|
|
let row_ids = block_order
|
|
|
|
.row_orders
|
|
|
|
.into_iter()
|
|
|
|
.map(|row_order| Cow::Owned(row_order.row_id))
|
|
|
|
.collect::<Vec<Cow<String>>>();
|
|
|
|
let row_count = editor.delete_rows(row_ids).await?;
|
2022-06-26 07:14:24 +00:00
|
|
|
let changeset = GridBlockMetaRevisionChangeset::from_row_count(&block_order.id, row_count);
|
2022-04-07 07:34:00 +00:00
|
|
|
changesets.push(changeset);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(changesets)
|
|
|
|
}
|
|
|
|
|
2022-04-16 01:25:12 +00:00
|
|
|
pub(crate) async fn move_row(&self, row_id: &str, from: usize, to: usize) -> FlowyResult<()> {
|
|
|
|
let editor = self.get_editor_from_row_id(row_id).await?;
|
|
|
|
let _ = editor.move_row(row_id, from, to).await?;
|
|
|
|
|
2022-06-15 07:13:50 +00:00
|
|
|
match editor.get_row_revs(Some(vec![Cow::Borrowed(row_id)])).await?.pop() {
|
2022-04-16 01:25:12 +00:00
|
|
|
None => {}
|
2022-06-15 07:13:50 +00:00
|
|
|
Some(row_rev) => {
|
|
|
|
let row_order = RowOrder::from(&row_rev);
|
2022-04-16 01:25:12 +00:00
|
|
|
let insert_row = IndexRowOrder {
|
|
|
|
row_order: row_order.clone(),
|
|
|
|
index: Some(to as i32),
|
|
|
|
};
|
|
|
|
let notified_changeset = GridRowsChangeset {
|
|
|
|
block_id: editor.block_id.clone(),
|
|
|
|
inserted_rows: vec![insert_row],
|
|
|
|
deleted_rows: vec![row_order],
|
|
|
|
updated_rows: vec![],
|
|
|
|
};
|
|
|
|
|
2022-06-24 07:23:39 +00:00
|
|
|
let _ = self
|
|
|
|
.notify_did_update_block(&editor.block_id, notified_changeset)
|
|
|
|
.await?;
|
2022-04-16 01:25:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-04-25 00:13:09 +00:00
|
|
|
pub async fn update_cell<F>(&self, changeset: CellChangeset, row_builder: F) -> FlowyResult<()>
|
|
|
|
where
|
2022-06-15 07:13:50 +00:00
|
|
|
F: FnOnce(Arc<RowRevision>) -> Option<Row>,
|
2022-04-25 00:13:09 +00:00
|
|
|
{
|
2022-04-07 07:34:00 +00:00
|
|
|
let row_changeset: RowMetaChangeset = changeset.clone().into();
|
2022-04-25 00:13:09 +00:00
|
|
|
let _ = self.update_row(row_changeset, row_builder).await?;
|
|
|
|
self.notify_did_update_cell(changeset).await?;
|
2022-04-07 07:34:00 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-06-15 07:13:50 +00:00
|
|
|
pub async fn get_row_rev(&self, row_id: &str) -> FlowyResult<Option<Arc<RowRevision>>> {
|
2022-04-07 07:34:00 +00:00
|
|
|
let editor = self.get_editor_from_row_id(row_id).await?;
|
2022-04-10 00:25:01 +00:00
|
|
|
let row_ids = vec![Cow::Borrowed(row_id)];
|
2022-06-15 07:13:50 +00:00
|
|
|
let mut row_revs = editor.get_row_revs(Some(row_ids)).await?;
|
|
|
|
if row_revs.is_empty() {
|
2022-04-07 07:34:00 +00:00
|
|
|
Ok(None)
|
|
|
|
} else {
|
2022-06-15 07:13:50 +00:00
|
|
|
Ok(row_revs.pop())
|
2022-04-07 07:34:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-10 00:25:01 +00:00
|
|
|
pub async fn get_row_orders(&self, block_id: &str) -> FlowyResult<Vec<RowOrder>> {
|
|
|
|
let editor = self.get_editor(block_id).await?;
|
2022-04-10 06:24:12 +00:00
|
|
|
editor.get_row_orders::<&str>(None).await
|
2022-04-10 00:25:01 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 07:34:00 +00:00
|
|
|
pub(crate) async fn make_block_snapshots(&self, block_ids: Vec<String>) -> FlowyResult<Vec<GridBlockSnapshot>> {
|
|
|
|
let mut snapshots = vec![];
|
|
|
|
for block_id in block_ids {
|
|
|
|
let editor = self.get_editor(&block_id).await?;
|
2022-06-15 07:13:50 +00:00
|
|
|
let row_revs = editor.get_row_revs::<&str>(None).await?;
|
|
|
|
snapshots.push(GridBlockSnapshot { block_id, row_revs });
|
2022-04-07 07:34:00 +00:00
|
|
|
}
|
|
|
|
Ok(snapshots)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optimization: Using the shared memory(Arc, Cow,etc.) to reduce memory usage.
|
|
|
|
#[allow(dead_code)]
|
2022-06-15 07:13:50 +00:00
|
|
|
pub async fn get_cell_revs(
|
2022-04-07 07:34:00 +00:00
|
|
|
&self,
|
|
|
|
block_ids: Vec<String>,
|
|
|
|
field_id: &str,
|
2022-04-10 00:25:01 +00:00
|
|
|
row_ids: Option<Vec<Cow<'_, String>>>,
|
2022-06-15 07:13:50 +00:00
|
|
|
) -> FlowyResult<Vec<CellRevision>> {
|
|
|
|
let mut block_cell_revs = vec![];
|
2022-04-07 07:34:00 +00:00
|
|
|
for block_id in block_ids {
|
|
|
|
let editor = self.get_editor(&block_id).await?;
|
2022-06-15 07:13:50 +00:00
|
|
|
let cell_revs = editor.get_cell_revs(field_id, row_ids.clone()).await?;
|
|
|
|
block_cell_revs.extend(cell_revs);
|
2022-04-07 07:34:00 +00:00
|
|
|
}
|
2022-06-15 07:13:50 +00:00
|
|
|
Ok(block_cell_revs)
|
2022-04-07 07:34:00 +00:00
|
|
|
}
|
|
|
|
|
2022-06-24 07:23:39 +00:00
|
|
|
async fn notify_did_update_block(&self, block_id: &str, changeset: GridRowsChangeset) -> FlowyResult<()> {
|
2022-06-24 10:13:40 +00:00
|
|
|
send_dart_notification(block_id, GridNotification::DidUpdateGridBlock)
|
2022-04-10 02:31:55 +00:00
|
|
|
.payload(changeset)
|
|
|
|
.send();
|
2022-04-07 07:34:00 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-04-25 00:13:09 +00:00
|
|
|
async fn notify_did_update_cell(&self, changeset: CellChangeset) -> FlowyResult<()> {
|
|
|
|
let id = format!("{}:{}", changeset.row_id, changeset.field_id);
|
|
|
|
send_dart_notification(&id, GridNotification::DidUpdateCell).send();
|
2022-04-07 07:34:00 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn make_block_meta_editor_map(
|
|
|
|
user: &Arc<dyn GridUser>,
|
2022-06-26 07:14:24 +00:00
|
|
|
block_revs: Vec<Arc<GridBlockMetaRevision>>,
|
2022-06-15 07:13:50 +00:00
|
|
|
) -> FlowyResult<DashMap<String, Arc<GridBlockRevisionEditor>>> {
|
2022-04-07 07:34:00 +00:00
|
|
|
let editor_map = DashMap::new();
|
2022-06-24 10:13:40 +00:00
|
|
|
for block_rev in block_revs {
|
|
|
|
let editor = make_block_meta_editor(user, &block_rev.block_id).await?;
|
|
|
|
editor_map.insert(block_rev.block_id.clone(), Arc::new(editor));
|
2022-04-07 07:34:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(editor_map)
|
|
|
|
}
|
|
|
|
|
2022-06-15 07:13:50 +00:00
|
|
|
async fn make_block_meta_editor(user: &Arc<dyn GridUser>, block_id: &str) -> FlowyResult<GridBlockRevisionEditor> {
|
2022-06-06 12:06:08 +00:00
|
|
|
tracing::trace!("Open block:{} meta editor", block_id);
|
2022-04-07 07:34:00 +00:00
|
|
|
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);
|
2022-06-15 07:13:50 +00:00
|
|
|
GridBlockRevisionEditor::new(&user_id, &token, block_id, rev_manager).await
|
2022-04-07 07:34:00 +00:00
|
|
|
}
|