2021-10-01 19:39:08 +08:00
|
|
|
use crate::{
|
2021-10-04 17:47:21 +08:00
|
|
|
entities::doc::{RevId, Revision, RevisionRange},
|
2021-10-04 14:24:35 +08:00
|
|
|
errors::{internal_error, DocError},
|
2021-10-04 17:47:21 +08:00
|
|
|
services::{doc::revision::store_actor::RevisionCmd, util::RevIdCounter, ws::DocumentWebSocket},
|
2021-10-03 11:33:19 +08:00
|
|
|
};
|
2021-10-04 17:47:21 +08:00
|
|
|
use flowy_infra::future::ResultFuture;
|
2021-10-02 17:19:54 +08:00
|
|
|
use flowy_ot::core::Delta;
|
2021-10-04 17:47:21 +08:00
|
|
|
|
2021-10-02 21:35:06 +08:00
|
|
|
use tokio::sync::{mpsc, oneshot};
|
2021-10-02 17:19:54 +08:00
|
|
|
|
|
|
|
pub struct DocRevision {
|
2021-10-02 21:35:06 +08:00
|
|
|
pub rev_id: RevId,
|
2021-10-02 17:19:54 +08:00
|
|
|
pub delta: Delta,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait RevisionServer: Send + Sync {
|
|
|
|
fn fetch_document_from_remote(&self, doc_id: &str) -> ResultFuture<DocRevision, DocError>;
|
|
|
|
}
|
2021-10-01 19:39:08 +08:00
|
|
|
|
|
|
|
pub struct RevisionManager {
|
|
|
|
doc_id: String,
|
|
|
|
rev_id_counter: RevIdCounter,
|
2021-10-03 11:33:19 +08:00
|
|
|
rev_store: mpsc::Sender<RevisionCmd>,
|
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-04 17:38:56 +08:00
|
|
|
pub fn new(doc_id: &str, rev_id: RevId, rev_store: mpsc::Sender<RevisionCmd>) -> Self {
|
2021-10-02 21:35:06 +08:00
|
|
|
let rev_id_counter = RevIdCounter::new(rev_id.into());
|
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-02 17:19:54 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
2021-10-04 17:38:56 +08:00
|
|
|
pub async fn add_revision(&self, revision: &Revision) -> Result<(), DocError> {
|
2021-10-04 14:24:35 +08:00
|
|
|
let (ret, rx) = oneshot::channel();
|
2021-10-03 11:33:19 +08:00
|
|
|
let cmd = RevisionCmd::Revision {
|
2021-10-01 19:39:08 +08:00
|
|
|
revision: revision.clone(),
|
2021-10-04 14:24:35 +08:00
|
|
|
ret,
|
2021-10-01 19:39:08 +08:00
|
|
|
};
|
2021-10-03 11:33:19 +08:00
|
|
|
let _ = self.rev_store.send(cmd).await;
|
2021-10-04 14:24:35 +08:00
|
|
|
let result = rx.await.map_err(internal_error)?;
|
|
|
|
result
|
2021-10-01 19:39:08 +08:00
|
|
|
}
|
|
|
|
|
2021-10-02 21:35:06 +08:00
|
|
|
pub fn ack_rev(&self, rev_id: RevId) -> Result<(), DocError> {
|
2021-10-03 11:33:19 +08:00
|
|
|
let sender = self.rev_store.clone();
|
2021-10-01 19:39:08 +08:00
|
|
|
tokio::spawn(async move {
|
2021-10-03 11:33:19 +08:00
|
|
|
let _ = sender.send(RevisionCmd::AckRevision { 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-04 17:38:56 +08:00
|
|
|
pub fn update_rev_id(&self, rev_id: i64) { self.rev_id_counter.set(rev_id); }
|
|
|
|
|
2021-10-04 14:24:35 +08:00
|
|
|
pub async fn send_revisions(&self, range: RevisionRange) -> Result<(), DocError> {
|
2021-10-01 19:39:08 +08:00
|
|
|
debug_assert!(&range.doc_id == &self.doc_id);
|
2021-10-04 14:24:35 +08:00
|
|
|
let (ret, rx) = oneshot::channel();
|
2021-10-03 11:33:19 +08:00
|
|
|
let sender = self.rev_store.clone();
|
2021-10-04 14:24:35 +08:00
|
|
|
let _ = sender.send(RevisionCmd::SendRevisions { range, ret }).await;
|
2021-10-04 17:47:21 +08:00
|
|
|
let _revisions = rx.await.map_err(internal_error)??;
|
2021-10-01 19:39:08 +08:00
|
|
|
|
|
|
|
unimplemented!()
|
2021-10-04 14:24:35 +08:00
|
|
|
// Ok(())
|
|
|
|
}
|
2021-10-02 17:19:54 +08:00
|
|
|
}
|