2022-03-05 10:59:44 +08:00
|
|
|
use crate::manager::GridUser;
|
2022-03-04 21:26:32 +08:00
|
|
|
use crate::services::kv_persistence::{GridKVPersistence, KVTransaction};
|
2022-03-06 09:03:02 +08:00
|
|
|
|
2022-03-11 21:36:00 +08:00
|
|
|
use crate::services::grid_meta_editor::ClientGridBlockMetaEditor;
|
|
|
|
use bytes::Bytes;
|
2022-03-05 17:52:25 +08:00
|
|
|
use dashmap::DashMap;
|
2022-03-10 12:01:31 +08:00
|
|
|
use flowy_collaboration::client_grid::{GridChange, GridMetaPad};
|
2022-03-04 18:11:12 +08:00
|
|
|
use flowy_collaboration::entities::revision::Revision;
|
|
|
|
use flowy_collaboration::util::make_delta_from_revisions;
|
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
2022-03-05 17:52:25 +08:00
|
|
|
use flowy_grid_data_model::entities::{
|
2022-03-12 09:30:13 +08:00
|
|
|
Field, Grid, GridBlock, RepeatedField, RepeatedFieldOrder, RepeatedRow, RepeatedRowOrder,
|
|
|
|
};
|
|
|
|
use flowy_sync::disk::SQLiteGridBlockMetaRevisionPersistence;
|
|
|
|
use flowy_sync::{
|
|
|
|
RevisionCloudService, RevisionCompactor, RevisionManager, RevisionObjectBuilder, RevisionPersistence,
|
2022-03-04 18:11:12 +08:00
|
|
|
};
|
|
|
|
use lib_infra::future::FutureResult;
|
2022-03-12 09:30:13 +08:00
|
|
|
use lib_ot::core::PlainTextAttributes;
|
2022-03-04 18:11:12 +08:00
|
|
|
use std::sync::Arc;
|
2022-03-05 10:59:44 +08:00
|
|
|
use tokio::sync::RwLock;
|
2022-03-04 18:11:12 +08:00
|
|
|
|
|
|
|
pub struct ClientGridEditor {
|
2022-03-04 22:09:16 +08:00
|
|
|
grid_id: String,
|
2022-03-05 10:59:44 +08:00
|
|
|
user: Arc<dyn GridUser>,
|
2022-03-10 12:01:31 +08:00
|
|
|
grid_meta_pad: Arc<RwLock<GridMetaPad>>,
|
2022-03-04 18:11:12 +08:00
|
|
|
rev_manager: Arc<RevisionManager>,
|
2022-03-11 21:36:00 +08:00
|
|
|
block_meta_manager: Arc<GridBlockMetaEditorManager>,
|
2022-03-05 10:59:44 +08:00
|
|
|
kv_persistence: Arc<GridKVPersistence>,
|
2022-03-04 18:11:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ClientGridEditor {
|
|
|
|
pub async fn new(
|
2022-03-04 22:09:16 +08:00
|
|
|
grid_id: &str,
|
2022-03-05 10:59:44 +08:00
|
|
|
user: Arc<dyn GridUser>,
|
|
|
|
mut rev_manager: RevisionManager,
|
|
|
|
kv_persistence: Arc<GridKVPersistence>,
|
2022-03-04 22:09:16 +08:00
|
|
|
) -> FlowyResult<Arc<Self>> {
|
2022-03-05 10:59:44 +08:00
|
|
|
let token = user.token()?;
|
|
|
|
let cloud = Arc::new(GridRevisionCloudService { token });
|
2022-03-11 21:36:00 +08:00
|
|
|
let grid_pad = rev_manager.load::<GridPadBuilder>(cloud).await?;
|
2022-03-04 18:11:12 +08:00
|
|
|
let rev_manager = Arc::new(rev_manager);
|
2022-03-10 12:01:31 +08:00
|
|
|
let grid_meta_pad = Arc::new(RwLock::new(grid_pad));
|
2022-03-12 09:30:13 +08:00
|
|
|
|
|
|
|
let block_meta_manager =
|
|
|
|
Arc::new(GridBlockMetaEditorManager::new(&user, grid_meta_pad.read().await.get_blocks()).await?);
|
2022-03-05 17:52:25 +08:00
|
|
|
|
2022-03-04 22:09:16 +08:00
|
|
|
Ok(Arc::new(Self {
|
2022-03-05 10:59:44 +08:00
|
|
|
grid_id: grid_id.to_owned(),
|
|
|
|
user,
|
2022-03-10 12:01:31 +08:00
|
|
|
grid_meta_pad,
|
2022-03-04 18:11:12 +08:00
|
|
|
rev_manager,
|
2022-03-11 21:36:00 +08:00
|
|
|
block_meta_manager,
|
2022-03-05 10:59:44 +08:00
|
|
|
kv_persistence,
|
2022-03-04 22:09:16 +08:00
|
|
|
}))
|
2022-03-04 18:11:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-04 21:26:32 +08:00
|
|
|
pub async fn create_field(&mut self, field: Field) -> FlowyResult<()> {
|
2022-03-10 12:01:31 +08:00
|
|
|
let _ = self.modify(|grid| Ok(grid.create_field(field)?)).await?;
|
2022-03-04 21:26:32 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn delete_field(&mut self, field_id: &str) -> FlowyResult<()> {
|
|
|
|
let _ = self.modify(|grid| Ok(grid.delete_field(field_id)?)).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-03-12 09:30:13 +08:00
|
|
|
pub async fn create_row(&self) -> FlowyResult<()> {
|
2022-03-11 21:36:00 +08:00
|
|
|
todo!()
|
|
|
|
}
|
2022-03-05 17:52:25 +08:00
|
|
|
|
2022-03-12 09:30:13 +08:00
|
|
|
pub async fn get_rows(&self, _row_orders: RepeatedRowOrder) -> FlowyResult<RepeatedRow> {
|
2022-03-11 21:36:00 +08:00
|
|
|
todo!()
|
2022-03-05 17:52:25 +08:00
|
|
|
}
|
|
|
|
|
2022-03-12 09:30:13 +08:00
|
|
|
pub async fn delete_rows(&self, _ids: Vec<String>) -> FlowyResult<()> {
|
2022-03-11 21:36:00 +08:00
|
|
|
todo!()
|
2022-03-05 17:52:25 +08:00
|
|
|
}
|
|
|
|
|
2022-03-05 10:59:44 +08:00
|
|
|
pub async fn grid_data(&self) -> Grid {
|
2022-03-11 21:36:00 +08:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_fields(&self, field_orders: RepeatedFieldOrder) -> FlowyResult<RepeatedField> {
|
|
|
|
let fields = self.grid_meta_pad.read().await.get_fields(field_orders)?;
|
|
|
|
Ok(fields)
|
2022-03-05 10:59:44 +08:00
|
|
|
}
|
|
|
|
|
2022-03-05 22:30:42 +08:00
|
|
|
pub async fn delta_str(&self) -> String {
|
2022-03-10 12:01:31 +08:00
|
|
|
self.grid_meta_pad.read().await.delta_str()
|
2022-03-05 22:30:42 +08:00
|
|
|
}
|
|
|
|
|
2022-03-04 21:26:32 +08:00
|
|
|
async fn modify<F>(&self, f: F) -> FlowyResult<()>
|
2022-03-04 18:11:12 +08:00
|
|
|
where
|
2022-03-10 12:01:31 +08:00
|
|
|
F: for<'a> FnOnce(&'a mut GridMetaPad) -> FlowyResult<Option<GridChange>>,
|
2022-03-04 18:11:12 +08:00
|
|
|
{
|
2022-03-10 12:01:31 +08:00
|
|
|
let mut write_guard = self.grid_meta_pad.write().await;
|
2022-03-04 18:11:12 +08:00
|
|
|
match f(&mut *write_guard)? {
|
|
|
|
None => {}
|
|
|
|
Some(change) => {
|
|
|
|
let _ = self.apply_change(change).await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn apply_change(&self, change: GridChange) -> FlowyResult<()> {
|
|
|
|
let GridChange { delta, md5 } = change;
|
2022-03-05 10:59:44 +08:00
|
|
|
let user_id = self.user.user_id()?;
|
2022-03-04 18:11:12 +08:00
|
|
|
let (base_rev_id, rev_id) = self.rev_manager.next_rev_id_pair();
|
|
|
|
let delta_data = delta.to_bytes();
|
|
|
|
let revision = Revision::new(
|
|
|
|
&self.rev_manager.object_id,
|
|
|
|
base_rev_id,
|
|
|
|
rev_id,
|
|
|
|
delta_data,
|
2022-03-05 10:59:44 +08:00
|
|
|
&user_id,
|
2022-03-04 18:11:12 +08:00
|
|
|
md5,
|
|
|
|
);
|
|
|
|
let _ = self
|
|
|
|
.rev_manager
|
2022-03-11 21:36:00 +08:00
|
|
|
.add_local_revision(&revision, Box::new(GridRevisionCompactor()))
|
2022-03-04 18:11:12 +08:00
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-05 17:52:25 +08:00
|
|
|
async fn load_all_fields(
|
2022-03-10 12:01:31 +08:00
|
|
|
grid_pad: &GridMetaPad,
|
2022-03-05 17:52:25 +08:00
|
|
|
kv_persistence: &Arc<GridKVPersistence>,
|
|
|
|
) -> FlowyResult<DashMap<String, Field>> {
|
|
|
|
let field_ids = grid_pad
|
2022-03-10 12:01:31 +08:00
|
|
|
.fields()
|
2022-03-05 17:52:25 +08:00
|
|
|
.iter()
|
2022-03-10 12:01:31 +08:00
|
|
|
.map(|field| field.id.clone())
|
2022-03-05 17:52:25 +08:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let fields = kv_persistence.batch_get::<Field>(field_ids)?;
|
|
|
|
let map = DashMap::new();
|
|
|
|
for field in fields {
|
|
|
|
map.insert(field.id.clone(), field);
|
|
|
|
}
|
|
|
|
Ok(map)
|
|
|
|
}
|
|
|
|
|
2022-03-04 18:11:12 +08:00
|
|
|
struct GridPadBuilder();
|
|
|
|
impl RevisionObjectBuilder for GridPadBuilder {
|
2022-03-10 12:01:31 +08:00
|
|
|
type Output = GridMetaPad;
|
2022-03-04 18:11:12 +08:00
|
|
|
|
2022-03-05 10:59:44 +08:00
|
|
|
fn build_object(object_id: &str, revisions: Vec<Revision>) -> FlowyResult<Self::Output> {
|
2022-03-10 12:01:31 +08:00
|
|
|
let pad = GridMetaPad::from_revisions(object_id, revisions)?;
|
2022-03-04 18:11:12 +08:00
|
|
|
Ok(pad)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct GridRevisionCloudService {
|
|
|
|
#[allow(dead_code)]
|
|
|
|
token: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RevisionCloudService for GridRevisionCloudService {
|
|
|
|
#[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![]) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-11 21:36:00 +08:00
|
|
|
struct GridRevisionCompactor();
|
|
|
|
impl RevisionCompactor for GridRevisionCompactor {
|
|
|
|
fn bytes_from_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes> {
|
|
|
|
let delta = make_delta_from_revisions::<PlainTextAttributes>(revisions)?;
|
|
|
|
Ok(delta.to_bytes())
|
|
|
|
}
|
|
|
|
}
|
2022-03-04 18:11:12 +08:00
|
|
|
|
2022-03-11 21:36:00 +08:00
|
|
|
struct GridBlockMetaEditorManager {
|
|
|
|
editor_map: DashMap<String, Arc<ClientGridBlockMetaEditor>>,
|
|
|
|
}
|
2022-03-04 18:11:12 +08:00
|
|
|
|
2022-03-11 21:36:00 +08:00
|
|
|
impl GridBlockMetaEditorManager {
|
2022-03-12 09:30:13 +08:00
|
|
|
async fn new(user: &Arc<dyn GridUser>, blocks: Vec<GridBlock>) -> FlowyResult<Self> {
|
|
|
|
let editor_map = make_block_meta_editor_map(user, blocks).await?;
|
|
|
|
let manager = Self { editor_map };
|
|
|
|
Ok(manager)
|
2022-03-11 21:36:00 +08:00
|
|
|
}
|
2022-03-04 18:11:12 +08:00
|
|
|
|
2022-03-12 09:30:13 +08:00
|
|
|
async fn get_editor(&self, _block_id: &str) -> Arc<ClientGridBlockMetaEditor> {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_rows(&self, _row_orders: RepeatedRowOrder) -> FlowyResult<RepeatedRow> {
|
2022-03-11 21:36:00 +08:00
|
|
|
// let ids = row_orders
|
|
|
|
// .items
|
|
|
|
// .into_iter()
|
|
|
|
// .map(|row_order| row_order.row_id)
|
|
|
|
// .collect::<Vec<_>>();
|
|
|
|
// let row_metas: Vec<RowMeta> = self.kv_persistence.batch_get(ids)?;
|
|
|
|
//
|
|
|
|
// let make_cell = |field_id: String, raw_cell: CellMeta| {
|
|
|
|
// let some_field = self.field_map.get(&field_id);
|
|
|
|
// if some_field.is_none() {
|
|
|
|
// tracing::error!("Can't find the field with {}", field_id);
|
|
|
|
// return None;
|
|
|
|
// }
|
|
|
|
// self.cell_map.insert(raw_cell.id.clone(), raw_cell.clone());
|
|
|
|
//
|
|
|
|
// let field = some_field.unwrap();
|
|
|
|
// match stringify_deserialize(raw_cell.data, field.value()) {
|
|
|
|
// Ok(content) => {
|
|
|
|
// let cell = Cell {
|
|
|
|
// id: raw_cell.id,
|
|
|
|
// field_id: field_id.clone(),
|
|
|
|
// content,
|
|
|
|
// };
|
|
|
|
// Some((field_id, cell))
|
|
|
|
// }
|
|
|
|
// Err(_) => None,
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// let rows = row_metas
|
|
|
|
// .into_par_iter()
|
|
|
|
// .map(|row_meta| {
|
|
|
|
// let mut row = Row {
|
|
|
|
// id: row_meta.id.clone(),
|
|
|
|
// cell_by_field_id: Default::default(),
|
|
|
|
// height: row_meta.height,
|
|
|
|
// };
|
|
|
|
// row.cell_by_field_id = row_meta
|
|
|
|
// .cell_by_field_id
|
|
|
|
// .into_par_iter()
|
|
|
|
// .flat_map(|(field_id, raw_cell)| make_cell(field_id, raw_cell))
|
|
|
|
// .collect::<HashMap<String, Cell>>();
|
|
|
|
// row
|
|
|
|
// })
|
|
|
|
// .collect::<Vec<Row>>();
|
|
|
|
//
|
|
|
|
// Ok(rows.into())
|
|
|
|
todo!()
|
2022-03-04 18:11:12 +08:00
|
|
|
}
|
|
|
|
}
|
2022-03-12 09:30:13 +08:00
|
|
|
|
|
|
|
async fn make_block_meta_editor_map(
|
|
|
|
user: &Arc<dyn GridUser>,
|
|
|
|
blocks: Vec<GridBlock>,
|
|
|
|
) -> FlowyResult<DashMap<String, Arc<ClientGridBlockMetaEditor>>> {
|
|
|
|
let token = user.token()?;
|
|
|
|
let user_id = user.user_id()?;
|
|
|
|
let pool = user.db_pool()?;
|
|
|
|
|
|
|
|
let editor_map = DashMap::new();
|
|
|
|
for block in blocks {
|
|
|
|
let disk_cache = Arc::new(SQLiteGridBlockMetaRevisionPersistence::new(&user_id, pool.clone()));
|
|
|
|
let rev_persistence = Arc::new(RevisionPersistence::new(&user_id, &block.id, disk_cache));
|
|
|
|
let rev_manager = RevisionManager::new(&user_id, &block.id, rev_persistence);
|
|
|
|
let editor = ClientGridBlockMetaEditor::new(&user_id, &token, &block.id, rev_manager).await?;
|
|
|
|
editor_map.insert(block.id, Arc::new(editor));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(editor_map)
|
|
|
|
}
|