2021-12-16 21:31:36 +08:00
|
|
|
use crate::{errors::FlowyError, services::doc::revision::RevisionCache};
|
2021-12-15 23:01:50 +08:00
|
|
|
use flowy_collaboration::{
|
|
|
|
entities::doc::Doc,
|
|
|
|
util::{md5, RevIdCounter},
|
|
|
|
};
|
2021-12-14 18:04:51 +08:00
|
|
|
use flowy_error::FlowyResult;
|
2021-12-13 13:55:44 +08:00
|
|
|
use lib_infra::future::FutureResult;
|
2021-12-07 22:32:34 +08:00
|
|
|
use lib_ot::{
|
|
|
|
core::OperationTransformable,
|
2021-12-16 21:31:36 +08:00
|
|
|
revision::{RevType, Revision, RevisionRange},
|
2021-12-07 22:32:34 +08:00
|
|
|
rich_text::RichTextDelta,
|
|
|
|
};
|
2021-10-07 20:46:29 +08:00
|
|
|
use std::sync::Arc;
|
2021-10-02 17:19:54 +08:00
|
|
|
|
|
|
|
pub trait RevisionServer: Send + Sync {
|
2021-12-14 18:04:51 +08:00
|
|
|
fn fetch_document(&self, doc_id: &str) -> FutureResult<Doc, FlowyError>;
|
2021-10-02 17:19:54 +08:00
|
|
|
}
|
2021-10-01 19:39:08 +08:00
|
|
|
|
|
|
|
pub struct RevisionManager {
|
|
|
|
doc_id: String,
|
2021-12-09 22:28:11 +08:00
|
|
|
user_id: String,
|
2021-10-01 19:39:08 +08:00
|
|
|
rev_id_counter: RevIdCounter,
|
2021-12-08 14:17:40 +08:00
|
|
|
cache: Arc<RevisionCache>,
|
2021-10-01 19:39:08 +08:00
|
|
|
}
|
2021-10-02 17:19:54 +08:00
|
|
|
|
2021-10-01 19:39:08 +08:00
|
|
|
impl RevisionManager {
|
2021-12-16 21:31:36 +08:00
|
|
|
pub fn new(user_id: &str, doc_id: &str, cache: Arc<RevisionCache>) -> Self {
|
2021-10-07 20:46:29 +08:00
|
|
|
let rev_id_counter = RevIdCounter::new(0);
|
2021-10-03 11:33:19 +08:00
|
|
|
Self {
|
|
|
|
doc_id: doc_id.to_string(),
|
2021-12-09 22:28:11 +08:00
|
|
|
user_id: user_id.to_owned(),
|
2021-10-01 19:39:08 +08:00
|
|
|
rev_id_counter,
|
2021-12-08 14:17:40 +08:00
|
|
|
cache,
|
2021-10-03 11:33:19 +08:00
|
|
|
}
|
2021-10-01 19:39:08 +08:00
|
|
|
}
|
|
|
|
|
2021-12-14 18:04:51 +08:00
|
|
|
pub async fn load_document(&mut self) -> FlowyResult<RichTextDelta> {
|
2021-12-08 17:33:22 +08:00
|
|
|
let doc = self.cache.load_document().await?;
|
2021-11-11 19:56:30 +08:00
|
|
|
self.update_rev_id_counter_value(doc.rev_id);
|
2021-10-07 20:46:29 +08:00
|
|
|
Ok(doc.delta()?)
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:04:51 +08:00
|
|
|
pub async fn add_remote_revision(&self, revision: &Revision) -> Result<(), FlowyError> {
|
2021-12-13 22:46:35 +08:00
|
|
|
let _ = self.cache.add_remote_revision(revision.clone()).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:04:51 +08:00
|
|
|
pub async fn add_local_revision(&self, revision: &Revision) -> Result<(), FlowyError> {
|
2021-12-13 22:46:35 +08:00
|
|
|
let _ = self.cache.add_local_revision(revision.clone()).await?;
|
2021-10-07 20:46:29 +08:00
|
|
|
Ok(())
|
2021-10-01 19:39:08 +08:00
|
|
|
}
|
|
|
|
|
2021-12-16 21:31:36 +08:00
|
|
|
pub async fn ack_revision(&self, rev_id: i64) -> Result<(), FlowyError> {
|
|
|
|
self.cache.ack_revision(rev_id).await;
|
2021-10-01 19:39:08 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rev_id(&self) -> i64 { self.rev_id_counter.value() }
|
|
|
|
|
|
|
|
pub fn next_rev_id(&self) -> (i64, i64) {
|
|
|
|
let cur = self.rev_id_counter.value();
|
|
|
|
let next = self.rev_id_counter.next();
|
|
|
|
(cur, next)
|
|
|
|
}
|
|
|
|
|
2021-11-11 19:56:30 +08:00
|
|
|
pub fn update_rev_id_counter_value(&self, rev_id: i64) { self.rev_id_counter.set(rev_id); }
|
2021-10-04 17:38:56 +08:00
|
|
|
|
2021-12-14 18:04:51 +08:00
|
|
|
pub async fn mk_revisions(&self, range: RevisionRange) -> Result<Revision, FlowyError> {
|
2021-11-27 19:19:41 +08:00
|
|
|
debug_assert!(range.doc_id == self.doc_id);
|
2021-12-08 14:17:40 +08:00
|
|
|
let revisions = self.cache.revisions_in_range(range.clone()).await?;
|
2021-12-07 10:39:01 +08:00
|
|
|
let mut new_delta = RichTextDelta::new();
|
2021-10-04 21:53:06 +08:00
|
|
|
for revision in revisions {
|
2021-12-07 10:39:01 +08:00
|
|
|
match RichTextDelta::from_bytes(revision.delta_data) {
|
2021-10-04 21:53:06 +08:00
|
|
|
Ok(delta) => {
|
|
|
|
new_delta = new_delta.compose(&delta)?;
|
|
|
|
},
|
2021-11-27 19:19:41 +08:00
|
|
|
Err(e) => log::error!("{}", e),
|
2021-10-04 21:53:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let delta_data = new_delta.to_bytes();
|
2021-12-15 23:01:50 +08:00
|
|
|
let md5 = md5(&delta_data);
|
2021-10-04 21:53:06 +08:00
|
|
|
let revision = Revision::new(
|
2021-12-15 23:01:50 +08:00
|
|
|
&self.doc_id,
|
2021-10-06 23:21:57 +08:00
|
|
|
range.start,
|
|
|
|
range.end,
|
2021-12-15 23:01:50 +08:00
|
|
|
delta_data,
|
2021-10-04 21:53:06 +08:00
|
|
|
RevType::Remote,
|
2021-12-15 23:01:50 +08:00
|
|
|
&self.user_id,
|
|
|
|
md5,
|
2021-10-04 21:53:06 +08:00
|
|
|
);
|
2021-10-01 19:39:08 +08:00
|
|
|
|
2021-10-04 21:53:06 +08:00
|
|
|
Ok(revision)
|
2021-10-04 14:24:35 +08:00
|
|
|
}
|
2021-12-08 21:51:06 +08:00
|
|
|
|
2021-12-16 21:31:36 +08:00
|
|
|
pub fn next_sync_revision(&self) -> FutureResult<Option<Revision>, FlowyError> { self.cache.next_revision() }
|
2021-10-02 17:19:54 +08:00
|
|
|
}
|
2021-12-08 14:17:40 +08:00
|
|
|
|
2021-12-08 21:51:06 +08:00
|
|
|
#[cfg(feature = "flowy_unit_test")]
|
|
|
|
impl RevisionManager {
|
|
|
|
pub fn revision_cache(&self) -> Arc<RevisionCache> { self.cache.clone() }
|
2021-12-08 14:17:40 +08:00
|
|
|
}
|