AppFlowy/frontend/rust-lib/flowy-grid/src/manager.rs

176 lines
6.9 KiB
Rust
Raw Normal View History

2022-06-07 06:22:20 +00:00
use crate::services::block::make_grid_block_meta_rev_manager;
use crate::services::grid_meta_editor::GridMetaEditor;
2022-05-26 09:28:44 +00:00
use crate::services::persistence::block_index::BlockIndexCache;
2022-04-05 06:25:07 +00:00
use crate::services::persistence::kv::GridKVPersistence;
use crate::services::persistence::GridDatabase;
2022-03-15 11:00:28 +00:00
use bytes::Bytes;
2022-03-04 14:09:16 +00:00
use dashmap::DashMap;
2022-03-19 08:23:34 +00:00
use flowy_database::ConnectionPool;
2022-03-04 14:09:16 +00:00
use flowy_error::{FlowyError, FlowyResult};
2022-03-15 11:00:28 +00:00
use flowy_grid_data_model::entities::{BuildGridContext, GridMeta};
2022-06-07 06:22:20 +00:00
use flowy_revision::disk::SQLiteGridRevisionPersistence;
2022-03-19 08:52:28 +00:00
use flowy_revision::{RevisionManager, RevisionPersistence, RevisionWebSocket};
use flowy_sync::client_grid::{make_block_meta_delta, make_grid_delta};
use flowy_sync::entities::revision::{RepeatedRevision, Revision};
2022-03-04 14:09:16 +00:00
use std::sync::Arc;
pub trait GridUser: Send + Sync {
fn user_id(&self) -> Result<String, FlowyError>;
fn token(&self) -> Result<String, FlowyError>;
fn db_pool(&self) -> Result<Arc<ConnectionPool>, FlowyError>;
}
pub struct GridManager {
2022-05-26 09:28:44 +00:00
editor_map: Arc<DashMap<String, Arc<GridMetaEditor>>>,
2022-03-04 14:09:16 +00:00
grid_user: Arc<dyn GridUser>,
2022-05-26 09:28:44 +00:00
block_index_cache: Arc<BlockIndexCache>,
2022-04-07 07:34:00 +00:00
#[allow(dead_code)]
2022-04-05 06:25:07 +00:00
kv_persistence: Arc<GridKVPersistence>,
2022-03-04 14:09:16 +00:00
}
impl GridManager {
2022-04-05 06:25:07 +00:00
pub fn new(
grid_user: Arc<dyn GridUser>,
_rev_web_socket: Arc<dyn RevisionWebSocket>,
database: Arc<dyn GridDatabase>,
) -> Self {
2022-05-26 09:28:44 +00:00
let grid_editors = Arc::new(DashMap::new());
2022-04-05 06:25:07 +00:00
let kv_persistence = Arc::new(GridKVPersistence::new(database.clone()));
2022-06-07 06:22:20 +00:00
let block_index_cache = Arc::new(BlockIndexCache::new(database));
2022-03-04 14:09:16 +00:00
Self {
2022-03-06 03:28:24 +00:00
editor_map: grid_editors,
2022-03-04 14:09:16 +00:00
grid_user,
2022-06-07 06:22:20 +00:00
block_index_cache,
2022-03-05 02:59:44 +00:00
kv_persistence,
}
}
#[tracing::instrument(level = "debug", skip_all, err)]
2022-03-05 14:30:42 +00:00
pub async fn create_grid<T: AsRef<str>>(&self, grid_id: T, revisions: RepeatedRevision) -> FlowyResult<()> {
2022-03-05 02:59:44 +00:00
let grid_id = grid_id.as_ref();
let db_pool = self.grid_user.db_pool()?;
let rev_manager = self.make_grid_rev_manager(grid_id, db_pool)?;
let _ = rev_manager.reset_object(revisions).await?;
Ok(())
2022-03-04 14:09:16 +00:00
}
2022-03-12 01:30:13 +00:00
#[tracing::instrument(level = "debug", skip_all, err)]
pub async fn create_grid_block_meta<T: AsRef<str>>(
&self,
block_id: T,
revisions: RepeatedRevision,
) -> FlowyResult<()> {
2022-06-07 06:22:20 +00:00
let rev_manager = make_grid_block_meta_rev_manager(&self.grid_user, block_id.as_ref())?;
2022-03-12 01:30:13 +00:00
let _ = rev_manager.reset_object(revisions).await?;
Ok(())
}
2022-03-05 02:59:44 +00:00
#[tracing::instrument(level = "debug", skip_all, fields(grid_id), err)]
2022-05-26 09:28:44 +00:00
pub async fn open_grid<T: AsRef<str>>(&self, grid_id: T) -> FlowyResult<Arc<GridMetaEditor>> {
2022-03-04 14:09:16 +00:00
let grid_id = grid_id.as_ref();
tracing::Span::current().record("grid_id", &grid_id);
2022-03-05 02:59:44 +00:00
self.get_or_create_grid_editor(grid_id).await
2022-03-04 14:09:16 +00:00
}
2022-04-10 08:29:45 +00:00
#[tracing::instrument(level = "debug", skip_all, fields(grid_id), err)]
2022-03-05 02:59:44 +00:00
pub fn close_grid<T: AsRef<str>>(&self, grid_id: T) -> FlowyResult<()> {
2022-03-04 14:09:16 +00:00
let grid_id = grid_id.as_ref();
tracing::Span::current().record("grid_id", &grid_id);
2022-03-06 03:28:24 +00:00
self.editor_map.remove(grid_id);
2022-03-04 14:09:16 +00:00
Ok(())
}
#[tracing::instrument(level = "debug", skip(self, grid_id), fields(doc_id), err)]
2022-03-05 02:59:44 +00:00
pub fn delete_grid<T: AsRef<str>>(&self, grid_id: T) -> FlowyResult<()> {
2022-03-04 14:09:16 +00:00
let grid_id = grid_id.as_ref();
tracing::Span::current().record("grid_id", &grid_id);
2022-03-06 03:28:24 +00:00
self.editor_map.remove(grid_id);
2022-03-04 14:09:16 +00:00
Ok(())
}
2022-03-20 09:17:06 +00:00
// #[tracing::instrument(level = "debug", skip(self), err)]
2022-05-26 09:28:44 +00:00
pub fn get_grid_editor(&self, grid_id: &str) -> FlowyResult<Arc<GridMetaEditor>> {
2022-03-06 03:28:24 +00:00
match self.editor_map.get(grid_id) {
2022-03-05 02:59:44 +00:00
None => Err(FlowyError::internal().context("Should call open_grid function first")),
2022-05-26 09:28:44 +00:00
Some(editor) => Ok(editor.clone()),
2022-03-05 02:59:44 +00:00
}
}
2022-05-26 09:28:44 +00:00
async fn get_or_create_grid_editor(&self, grid_id: &str) -> FlowyResult<Arc<GridMetaEditor>> {
2022-03-06 03:28:24 +00:00
match self.editor_map.get(grid_id) {
2022-03-04 14:09:16 +00:00
None => {
2022-03-06 03:28:24 +00:00
tracing::trace!("Create grid editor with id: {}", grid_id);
2022-03-04 14:09:16 +00:00
let db_pool = self.grid_user.db_pool()?;
2022-03-06 03:28:24 +00:00
let editor = self.make_grid_editor(grid_id, db_pool).await?;
2022-05-26 09:28:44 +00:00
if self.editor_map.contains_key(grid_id) {
tracing::warn!("Grid:{} already exists in cache", grid_id);
}
self.editor_map.insert(grid_id.to_string(), editor.clone());
2022-03-06 03:28:24 +00:00
Ok(editor)
2022-03-04 14:09:16 +00:00
}
2022-05-26 09:28:44 +00:00
Some(editor) => Ok(editor.clone()),
2022-03-04 14:09:16 +00:00
}
}
#[tracing::instrument(level = "trace", skip(self, pool), err)]
2022-03-04 14:09:16 +00:00
async fn make_grid_editor(
&self,
grid_id: &str,
pool: Arc<ConnectionPool>,
2022-05-26 09:28:44 +00:00
) -> Result<Arc<GridMetaEditor>, FlowyError> {
2022-03-05 02:59:44 +00:00
let user = self.grid_user.clone();
let rev_manager = self.make_grid_rev_manager(grid_id, pool.clone())?;
2022-05-26 09:28:44 +00:00
let grid_editor = GridMetaEditor::new(grid_id, user, rev_manager, self.block_index_cache.clone()).await?;
2022-03-04 14:09:16 +00:00
Ok(grid_editor)
}
2022-03-05 02:59:44 +00:00
2022-03-12 13:06:15 +00:00
pub fn make_grid_rev_manager(&self, grid_id: &str, pool: Arc<ConnectionPool>) -> FlowyResult<RevisionManager> {
2022-03-05 02:59:44 +00:00
let user_id = self.grid_user.user_id()?;
2022-03-11 13:36:00 +00:00
let disk_cache = Arc::new(SQLiteGridRevisionPersistence::new(&user_id, pool));
let rev_persistence = Arc::new(RevisionPersistence::new(&user_id, grid_id, disk_cache));
2022-03-05 02:59:44 +00:00
let rev_manager = RevisionManager::new(&user_id, grid_id, rev_persistence);
Ok(rev_manager)
}
2022-03-04 14:09:16 +00:00
}
2022-03-15 11:00:28 +00:00
pub async fn make_grid_view_data(
user_id: &str,
view_id: &str,
grid_manager: Arc<GridManager>,
build_context: BuildGridContext,
) -> FlowyResult<Bytes> {
let grid_meta = GridMeta {
grid_id: view_id.to_string(),
fields: build_context.field_metas,
2022-06-06 12:06:08 +00:00
blocks: build_context.blocks,
2022-03-15 11:00:28 +00:00
};
2022-04-07 07:34:00 +00:00
// Create grid
2022-03-15 11:00:28 +00:00
let grid_meta_delta = make_grid_delta(&grid_meta);
let grid_delta_data = grid_meta_delta.to_delta_bytes();
let repeated_revision: RepeatedRevision =
Revision::initial_revision(user_id, view_id, grid_delta_data.clone()).into();
let _ = grid_manager.create_grid(view_id, repeated_revision).await?;
2022-06-06 12:06:08 +00:00
for block_meta_data in build_context.blocks_meta_data {
let block_id = block_meta_data.block_id.clone();
// Indexing the block's rows
block_meta_data.rows.iter().for_each(|row| {
let _ = grid_manager.block_index_cache.insert(&row.block_id, &row.id);
});
// Create grid's block
let grid_block_meta_delta = make_block_meta_delta(&block_meta_data);
let block_meta_delta_data = grid_block_meta_delta.to_delta_bytes();
let repeated_revision: RepeatedRevision =
Revision::initial_revision(user_id, &block_id, block_meta_delta_data).into();
let _ = grid_manager
.create_grid_block_meta(&block_id, repeated_revision)
.await?;
}
2022-03-15 11:00:28 +00:00
Ok(grid_delta_data)
}