2021-10-01 19:39:08 +08:00
|
|
|
use crate::{
|
2021-10-06 15:23:38 +08:00
|
|
|
entities::doc::{Doc, RevId, RevType, Revision, RevisionRange},
|
2021-10-08 13:46:23 +08:00
|
|
|
errors::{DocError, DocResult},
|
|
|
|
services::{doc::revision::RevisionStore, util::RevIdCounter},
|
2021-10-03 11:33:19 +08:00
|
|
|
};
|
2021-10-07 20:46:29 +08:00
|
|
|
use flowy_database::ConnectionPool;
|
2021-10-04 17:47:21 +08:00
|
|
|
use flowy_infra::future::ResultFuture;
|
2021-10-04 21:53:06 +08:00
|
|
|
use flowy_ot::core::{Delta, OperationTransformable};
|
2021-10-07 20:46:29 +08:00
|
|
|
use std::sync::Arc;
|
2021-10-08 13:46:23 +08:00
|
|
|
use tokio::sync::mpsc;
|
2021-10-02 17:19:54 +08:00
|
|
|
|
|
|
|
pub trait RevisionServer: Send + Sync {
|
2021-10-06 15:23:38 +08:00
|
|
|
fn fetch_document_from_remote(&self, doc_id: &str) -> ResultFuture<Doc, DocError>;
|
2021-10-02 17:19:54 +08:00
|
|
|
}
|
2021-10-01 19:39:08 +08:00
|
|
|
|
|
|
|
pub struct RevisionManager {
|
|
|
|
doc_id: String,
|
|
|
|
rev_id_counter: RevIdCounter,
|
2021-10-08 13:46:23 +08:00
|
|
|
rev_store: Arc<RevisionStore>,
|
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-10-07 20:46:29 +08:00
|
|
|
pub fn new(
|
|
|
|
doc_id: &str,
|
|
|
|
pool: Arc<ConnectionPool>,
|
|
|
|
server: Arc<dyn RevisionServer>,
|
2021-10-08 15:08:56 +08:00
|
|
|
pending_rev_sender: mpsc::UnboundedSender<Revision>,
|
2021-10-07 20:46:29 +08:00
|
|
|
) -> Self {
|
2021-10-08 13:46:23 +08:00
|
|
|
let rev_store = RevisionStore::new(doc_id, pool, server, pending_rev_sender);
|
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-10-01 19:39:08 +08:00
|
|
|
rev_id_counter,
|
2021-10-03 11:33:19 +08:00
|
|
|
rev_store,
|
|
|
|
}
|
2021-10-01 19:39:08 +08:00
|
|
|
}
|
|
|
|
|
2021-10-07 20:46:29 +08:00
|
|
|
pub async fn load_document(&mut self) -> DocResult<Delta> {
|
|
|
|
let doc = self.rev_store.fetch_document().await?;
|
|
|
|
self.set_rev_id(doc.rev_id);
|
|
|
|
Ok(doc.delta()?)
|
|
|
|
}
|
|
|
|
|
2021-10-04 17:38:56 +08:00
|
|
|
pub async fn add_revision(&self, revision: &Revision) -> Result<(), DocError> {
|
2021-10-07 20:46:29 +08:00
|
|
|
let _ = self.rev_store.handle_new_revision(revision.clone()).await?;
|
|
|
|
Ok(())
|
2021-10-01 19:39:08 +08:00
|
|
|
}
|
|
|
|
|
2021-10-08 15:08:56 +08:00
|
|
|
pub async fn ack_rev(&self, rev_id: RevId) -> Result<(), DocError> {
|
|
|
|
self.rev_store.handle_revision_acked(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-10-06 15:23:38 +08:00
|
|
|
pub fn set_rev_id(&self, rev_id: i64) { self.rev_id_counter.set(rev_id); }
|
2021-10-04 17:38:56 +08:00
|
|
|
|
2021-10-05 10:19:43 +08:00
|
|
|
pub async fn construct_revisions(&self, range: RevisionRange) -> Result<Revision, DocError> {
|
2021-10-01 19:39:08 +08:00
|
|
|
debug_assert!(&range.doc_id == &self.doc_id);
|
2021-10-07 20:46:29 +08:00
|
|
|
let revisions = self.rev_store.revs_in_range(range.clone()).await?;
|
2021-10-04 21:53:06 +08:00
|
|
|
let mut new_delta = Delta::new();
|
|
|
|
for revision in revisions {
|
|
|
|
match Delta::from_bytes(revision.delta_data) {
|
|
|
|
Ok(delta) => {
|
|
|
|
new_delta = new_delta.compose(&delta)?;
|
|
|
|
},
|
|
|
|
Err(_) => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let delta_data = new_delta.to_bytes();
|
|
|
|
let revision = Revision::new(
|
2021-10-06 23:21:57 +08:00
|
|
|
range.start,
|
|
|
|
range.end,
|
2021-10-04 21:53:06 +08:00
|
|
|
delta_data.to_vec(),
|
|
|
|
&self.doc_id,
|
|
|
|
RevType::Remote,
|
|
|
|
);
|
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-10-02 17:19:54 +08:00
|
|
|
}
|