AppFlowy/frontend/rust-lib/flowy-sync/src/rev_manager.rs

220 lines
7.5 KiB
Rust
Raw Normal View History

2022-03-10 14:27:19 +00:00
use crate::disk::RevisionState;
2022-02-25 14:27:44 +00:00
use crate::{RevisionPersistence, WSDataProviderDataSource};
2022-03-11 13:36:00 +00:00
use bytes::Bytes;
2021-12-15 15:01:50 +00:00
use flowy_collaboration::{
2022-03-10 14:27:19 +00:00
entities::revision::{RepeatedRevision, Revision, RevisionRange},
2022-01-14 07:23:21 +00:00
util::{pair_rev_id_from_revisions, RevIdCounter},
2021-12-15 15:01:50 +00:00
};
2022-01-14 07:23:21 +00:00
use flowy_error::{FlowyError, FlowyResult};
2021-12-13 05:55:44 +00:00
use lib_infra::future::FutureResult;
2022-02-19 03:34:31 +00:00
use std::sync::Arc;
2022-01-14 07:23:21 +00:00
pub trait RevisionCloudService: Send + Sync {
fn fetch_object(&self, user_id: &str, object_id: &str) -> FutureResult<Vec<Revision>, FlowyError>;
}
2022-01-14 07:23:21 +00:00
pub trait RevisionObjectBuilder: Send + Sync {
type Output;
2022-02-19 03:34:31 +00:00
fn build_object(object_id: &str, revisions: Vec<Revision>) -> FlowyResult<Self::Output>;
2022-01-14 07:23:21 +00:00
}
2022-03-11 13:36:00 +00:00
pub trait RevisionCompactor: Send + Sync {
fn compact(&self, user_id: &str, object_id: &str, mut revisions: Vec<Revision>) -> FlowyResult<Revision> {
if revisions.is_empty() {
return Err(FlowyError::internal().context("Can't compact the empty folder's revisions"));
}
if revisions.len() == 1 {
return Ok(revisions.pop().unwrap());
}
let first_revision = revisions.first().unwrap();
let last_revision = revisions.last().unwrap();
let (base_rev_id, rev_id) = first_revision.pair_rev_id();
let md5 = last_revision.md5.clone();
let delta_data = self.bytes_from_revisions(revisions)?;
Ok(Revision::new(object_id, base_rev_id, rev_id, delta_data, user_id, md5))
}
fn bytes_from_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes>;
2022-01-25 12:37:48 +00:00
}
2022-01-14 07:23:21 +00:00
pub struct RevisionManager {
pub object_id: String,
2021-12-09 14:28:11 +00:00
user_id: String,
rev_id_counter: RevIdCounter,
2022-02-25 14:27:44 +00:00
rev_persistence: Arc<RevisionPersistence>,
2022-01-22 10:48:43 +00:00
#[cfg(feature = "flowy_unit_test")]
2022-02-18 15:04:55 +00:00
rev_ack_notifier: tokio::sync::broadcast::Sender<i64>,
}
2022-01-14 07:23:21 +00:00
impl RevisionManager {
2022-02-25 14:27:44 +00:00
pub fn new(user_id: &str, object_id: &str, rev_persistence: Arc<RevisionPersistence>) -> Self {
2021-10-07 12:46:29 +00:00
let rev_id_counter = RevIdCounter::new(0);
2022-01-22 10:48:43 +00:00
#[cfg(feature = "flowy_unit_test")]
2022-01-24 09:35:58 +00:00
let (revision_ack_notifier, _) = tokio::sync::broadcast::channel(1);
2022-01-22 10:48:43 +00:00
Self {
2022-01-14 07:23:21 +00:00
object_id: object_id.to_string(),
2021-12-09 14:28:11 +00:00
user_id: user_id.to_owned(),
rev_id_counter,
2022-02-25 14:27:44 +00:00
rev_persistence,
2022-01-22 10:48:43 +00:00
#[cfg(feature = "flowy_unit_test")]
2022-02-18 15:04:55 +00:00
rev_ack_notifier: revision_ack_notifier,
}
}
2022-03-11 13:36:00 +00:00
pub async fn load<B>(&mut self, cloud: Arc<dyn RevisionCloudService>) -> FlowyResult<B::Output>
2022-01-14 07:23:21 +00:00
where
2022-01-25 12:37:48 +00:00
B: RevisionObjectBuilder,
2022-01-14 07:23:21 +00:00
{
2022-01-23 14:33:47 +00:00
let (revisions, rev_id) = RevisionLoader {
2022-01-14 07:23:21 +00:00
object_id: self.object_id.clone(),
2021-12-17 16:23:26 +00:00
user_id: self.user_id.clone(),
2022-03-01 15:38:26 +00:00
cloud: Some(cloud),
rev_persistence: self.rev_persistence.clone(),
2021-12-17 16:23:26 +00:00
}
2022-02-19 03:34:31 +00:00
.load()
2021-12-17 16:23:26 +00:00
.await?;
2022-01-23 14:33:47 +00:00
self.rev_id_counter.set(rev_id);
2022-02-19 03:34:31 +00:00
B::build_object(&self.object_id, revisions)
2021-10-07 12:46:29 +00:00
}
2022-01-01 06:23:58 +00:00
#[tracing::instrument(level = "debug", skip(self, revisions), err)]
2022-01-14 07:23:21 +00:00
pub async fn reset_object(&self, revisions: RepeatedRevision) -> FlowyResult<()> {
2022-01-02 02:34:42 +00:00
let rev_id = pair_rev_id_from_revisions(&revisions).1;
2022-02-25 14:27:44 +00:00
let _ = self.rev_persistence.reset(revisions.into_inner()).await?;
2022-01-02 02:34:42 +00:00
self.rev_id_counter.set(rev_id);
Ok(())
2022-01-01 06:23:58 +00:00
}
2022-01-02 02:34:42 +00:00
#[tracing::instrument(level = "debug", skip(self, revision), err)]
2021-12-14 10:04:51 +00:00
pub async fn add_remote_revision(&self, revision: &Revision) -> Result<(), FlowyError> {
2022-01-01 15:09:13 +00:00
if revision.delta_data.is_empty() {
return Err(FlowyError::internal().context("Delta data should be empty"));
}
2022-01-26 15:29:18 +00:00
2022-02-25 14:27:44 +00:00
let _ = self.rev_persistence.add_ack_revision(revision).await?;
2022-01-07 09:37:11 +00:00
self.rev_id_counter.set(revision.rev_id);
2021-12-13 14:46:35 +00:00
Ok(())
}
2022-03-11 13:36:00 +00:00
#[tracing::instrument(level = "debug", skip_all, err)]
pub async fn add_local_revision<'a>(
&'a self,
revision: &Revision,
compactor: Box<dyn RevisionCompactor + 'a>,
) -> Result<(), FlowyError> {
2022-01-01 15:09:13 +00:00
if revision.delta_data.is_empty() {
return Err(FlowyError::internal().context("Delta data should be empty"));
}
2022-03-11 13:36:00 +00:00
let rev_id = self.rev_persistence.add_sync_revision(revision, compactor).await?;
2022-01-26 15:29:18 +00:00
self.rev_id_counter.set(rev_id);
2021-10-07 12:46:29 +00:00
Ok(())
}
2022-01-01 08:16:06 +00:00
#[tracing::instrument(level = "debug", skip(self), err)]
2021-12-16 13:31:36 +00:00
pub async fn ack_revision(&self, rev_id: i64) -> Result<(), FlowyError> {
2022-02-25 14:27:44 +00:00
if self.rev_persistence.ack_revision(rev_id).await.is_ok() {
#[cfg(feature = "flowy_unit_test")]
2022-02-18 15:04:55 +00:00
let _ = self.rev_ack_notifier.send(rev_id);
}
Ok(())
}
2022-01-24 09:35:58 +00:00
pub fn rev_id(&self) -> i64 {
self.rev_id_counter.value()
}
2022-01-02 02:34:42 +00:00
pub fn next_rev_id_pair(&self) -> (i64, i64) {
let cur = self.rev_id_counter.value();
let next = self.rev_id_counter.next();
(cur, next)
}
2021-12-25 13:44:45 +00:00
pub async fn get_revisions_in_range(&self, range: RevisionRange) -> Result<Vec<Revision>, FlowyError> {
2022-02-25 14:27:44 +00:00
let revisions = self.rev_persistence.revisions_in_range(&range).await?;
2021-12-25 13:44:45 +00:00
Ok(revisions)
}
2021-12-08 13:51:06 +00:00
2022-01-25 12:37:48 +00:00
pub async fn next_sync_revision(&self) -> FlowyResult<Option<Revision>> {
2022-02-25 14:27:44 +00:00
Ok(self.rev_persistence.next_sync_revision().await?)
2022-01-01 08:16:06 +00:00
}
2021-12-18 10:35:45 +00:00
2021-12-25 13:44:45 +00:00
pub async fn get_revision(&self, rev_id: i64) -> Option<Revision> {
2022-02-25 14:27:44 +00:00
self.rev_persistence.get(rev_id).await.map(|record| record.revision)
}
}
impl WSDataProviderDataSource for Arc<RevisionManager> {
fn next_revision(&self) -> FutureResult<Option<Revision>, FlowyError> {
let rev_manager = self.clone();
FutureResult::new(async move { rev_manager.next_sync_revision().await })
}
fn ack_revision(&self, rev_id: i64) -> FutureResult<(), FlowyError> {
let rev_manager = self.clone();
FutureResult::new(async move { (*rev_manager).ack_revision(rev_id).await })
}
fn current_rev_id(&self) -> i64 {
self.rev_id()
2021-12-25 13:44:45 +00:00
}
}
2021-12-08 06:17:40 +00:00
2022-01-25 12:37:48 +00:00
#[cfg(feature = "flowy_unit_test")]
impl RevisionManager {
2022-02-25 14:27:44 +00:00
pub async fn revision_cache(&self) -> Arc<RevisionPersistence> {
self.rev_persistence.clone()
2022-01-25 12:37:48 +00:00
}
2022-02-18 15:04:55 +00:00
pub fn ack_notify(&self) -> tokio::sync::broadcast::Receiver<i64> {
self.rev_ack_notifier.subscribe()
2022-01-01 08:16:06 +00:00
}
}
2022-03-01 15:38:26 +00:00
pub struct RevisionLoader {
pub object_id: String,
pub user_id: String,
pub cloud: Option<Arc<dyn RevisionCloudService>>,
pub rev_persistence: Arc<RevisionPersistence>,
2021-12-17 16:23:26 +00:00
}
impl RevisionLoader {
2022-03-01 15:38:26 +00:00
pub async fn load(&self) -> Result<(Vec<Revision>, i64), FlowyError> {
let records = self.rev_persistence.batch_get(&self.object_id)?;
2021-12-17 16:23:26 +00:00
let revisions: Vec<Revision>;
2022-01-23 14:33:47 +00:00
let mut rev_id = 0;
2022-03-01 15:38:26 +00:00
if records.is_empty() && self.cloud.is_some() {
let remote_revisions = self
.cloud
.as_ref()
.unwrap()
.fetch_object(&self.user_id, &self.object_id)
.await?;
2022-01-14 07:23:21 +00:00
for revision in &remote_revisions {
2022-01-23 14:33:47 +00:00
rev_id = revision.rev_id;
2022-03-01 15:38:26 +00:00
let _ = self.rev_persistence.add_ack_revision(revision).await?;
2022-01-14 07:23:21 +00:00
}
revisions = remote_revisions;
2021-12-17 16:23:26 +00:00
} else {
2022-01-25 12:37:48 +00:00
for record in &records {
rev_id = record.revision.rev_id;
if record.state == RevisionState::Sync {
// Sync the records if their state is RevisionState::Sync.
2022-03-01 15:38:26 +00:00
let _ = self.rev_persistence.sync_revision(&record.revision).await?;
2022-01-23 14:33:47 +00:00
}
}
2021-12-17 16:23:26 +00:00
revisions = records.into_iter().map(|record| record.revision).collect::<_>();
}
2022-01-23 14:33:47 +00:00
if let Some(revision) = revisions.last() {
debug_assert_eq!(rev_id, revision.rev_id);
}
Ok((revisions, rev_id))
2021-12-17 16:23:26 +00:00
}
}