2022-03-10 22:27:19 +08:00
|
|
|
use crate::disk::RevisionState;
|
2022-06-10 22:27:19 +08:00
|
|
|
use crate::{RevisionPersistence, RevisionSnapshotDiskCache, RevisionSnapshotManager, WSDataProviderDataSource};
|
2022-03-11 21:36:00 +08:00
|
|
|
use bytes::Bytes;
|
2022-03-19 16:52:28 +08:00
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
|
|
|
use flowy_sync::{
|
2022-03-10 22:27:19 +08:00
|
|
|
entities::revision::{RepeatedRevision, Revision, RevisionRange},
|
2022-01-14 15:23:21 +08:00
|
|
|
util::{pair_rev_id_from_revisions, RevIdCounter},
|
2021-12-15 23:01:50 +08:00
|
|
|
};
|
2021-12-13 13:55:44 +08:00
|
|
|
use lib_infra::future::FutureResult;
|
2022-02-19 11:34:31 +08:00
|
|
|
use std::sync::Arc;
|
2021-10-02 17:19:54 +08:00
|
|
|
|
2022-01-14 15:23:21 +08:00
|
|
|
pub trait RevisionCloudService: Send + Sync {
|
2022-10-13 23:29:37 +08:00
|
|
|
/// Read the object's revision from remote
|
|
|
|
/// Returns a list of revisions that used to build the object
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `user_id`: the id of the user
|
|
|
|
/// * `object_id`: the id of the object
|
|
|
|
///
|
2022-01-14 15:23:21 +08:00
|
|
|
fn fetch_object(&self, user_id: &str, object_id: &str) -> FutureResult<Vec<Revision>, FlowyError>;
|
2021-10-02 17:19:54 +08:00
|
|
|
}
|
2021-10-01 19:39:08 +08:00
|
|
|
|
2022-10-13 23:29:37 +08:00
|
|
|
pub trait RevisionObjectDeserializer: Send + Sync {
|
2022-01-14 15:23:21 +08:00
|
|
|
type Output;
|
2022-10-13 23:29:37 +08:00
|
|
|
/// Deserialize the list of revisions into an concrete object type.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `object_id`: the id of the object
|
|
|
|
/// * `revisions`: a list of revisions that represent the object
|
|
|
|
///
|
|
|
|
fn deserialize_revisions(object_id: &str, revisions: Vec<Revision>) -> FlowyResult<Self::Output>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait RevisionObjectSerializer: Send + Sync {
|
2022-10-20 11:35:11 +08:00
|
|
|
/// Serialize a list of revisions into one in `Bytes` format
|
2022-10-13 23:29:37 +08:00
|
|
|
///
|
|
|
|
/// * `revisions`: a list of revisions will be serialized to `Bytes`
|
|
|
|
///
|
2022-10-20 11:35:11 +08:00
|
|
|
fn combine_revisions(revisions: Vec<Revision>) -> FlowyResult<Bytes>;
|
2022-01-14 15:23:21 +08:00
|
|
|
}
|
|
|
|
|
2022-10-13 23:29:37 +08:00
|
|
|
/// `RevisionCompress` is used to compress multiple revisions into one revision
|
|
|
|
///
|
|
|
|
pub trait RevisionCompress: Send + Sync {
|
|
|
|
fn compress_revisions(
|
|
|
|
&self,
|
|
|
|
user_id: &str,
|
|
|
|
object_id: &str,
|
|
|
|
mut revisions: Vec<Revision>,
|
|
|
|
) -> FlowyResult<Revision> {
|
2022-03-11 21:36:00 +08:00
|
|
|
if revisions.is_empty() {
|
2022-10-13 23:29:37 +08:00
|
|
|
return Err(FlowyError::internal().context("Can't compact the empty revisions"));
|
2022-03-11 21:36:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2022-10-20 11:35:11 +08:00
|
|
|
let bytes = self.combine_revisions(revisions)?;
|
2022-10-10 11:56:48 +08:00
|
|
|
Ok(Revision::new(object_id, base_rev_id, rev_id, bytes, user_id, md5))
|
2022-03-11 21:36:00 +08:00
|
|
|
}
|
|
|
|
|
2022-10-20 11:35:11 +08:00
|
|
|
fn combine_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes>;
|
2022-01-25 20:37:48 +08:00
|
|
|
}
|
|
|
|
|
2022-11-01 18:59:53 +08:00
|
|
|
pub struct RevisionManager<Connection> {
|
2022-01-14 15:23:21 +08:00
|
|
|
pub object_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,
|
2022-11-01 18:59:53 +08:00
|
|
|
rev_persistence: Arc<RevisionPersistence<Connection>>,
|
2022-07-20 15:30:48 +08:00
|
|
|
#[allow(dead_code)]
|
2022-06-10 22:27:19 +08:00
|
|
|
rev_snapshot: Arc<RevisionSnapshotManager>,
|
2022-10-13 23:29:37 +08:00
|
|
|
rev_compress: Arc<dyn RevisionCompress>,
|
2022-01-22 18:48:43 +08:00
|
|
|
#[cfg(feature = "flowy_unit_test")]
|
2022-02-18 23:04:55 +08:00
|
|
|
rev_ack_notifier: tokio::sync::broadcast::Sender<i64>,
|
2021-10-01 19:39:08 +08:00
|
|
|
}
|
2021-10-02 17:19:54 +08:00
|
|
|
|
2022-11-01 18:59:53 +08:00
|
|
|
impl<Connection: 'static> RevisionManager<Connection> {
|
2022-07-20 14:07:54 +08:00
|
|
|
pub fn new<SP, C>(
|
2022-06-09 20:58:56 +08:00
|
|
|
user_id: &str,
|
|
|
|
object_id: &str,
|
2022-11-01 18:59:53 +08:00
|
|
|
rev_persistence: RevisionPersistence<Connection>,
|
2022-10-22 21:57:44 +08:00
|
|
|
rev_compress: C,
|
2022-06-10 22:27:19 +08:00
|
|
|
snapshot_persistence: SP,
|
2022-06-09 20:58:56 +08:00
|
|
|
) -> Self
|
|
|
|
where
|
2022-06-10 22:27:19 +08:00
|
|
|
SP: 'static + RevisionSnapshotDiskCache,
|
2022-10-13 23:29:37 +08:00
|
|
|
C: 'static + RevisionCompress,
|
2022-06-09 20:58:56 +08:00
|
|
|
{
|
2021-10-07 20:46:29 +08:00
|
|
|
let rev_id_counter = RevIdCounter::new(0);
|
2022-10-22 21:57:44 +08:00
|
|
|
let rev_compress = Arc::new(rev_compress);
|
2022-06-09 20:58:56 +08:00
|
|
|
let rev_persistence = Arc::new(rev_persistence);
|
2022-06-10 22:27:19 +08:00
|
|
|
let rev_snapshot = Arc::new(RevisionSnapshotManager::new(user_id, object_id, snapshot_persistence));
|
2022-01-22 18:48:43 +08:00
|
|
|
#[cfg(feature = "flowy_unit_test")]
|
2022-01-24 17:35:58 +08:00
|
|
|
let (revision_ack_notifier, _) = tokio::sync::broadcast::channel(1);
|
2022-01-22 18:48:43 +08:00
|
|
|
|
2021-10-03 11:33:19 +08:00
|
|
|
Self {
|
2022-01-14 15:23:21 +08:00
|
|
|
object_id: object_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,
|
2022-02-25 22:27:44 +08:00
|
|
|
rev_persistence,
|
2022-06-10 22:27:19 +08:00
|
|
|
rev_snapshot,
|
2022-10-22 21:57:44 +08:00
|
|
|
rev_compress,
|
2022-01-22 18:48:43 +08:00
|
|
|
#[cfg(feature = "flowy_unit_test")]
|
2022-02-18 23:04:55 +08:00
|
|
|
rev_ack_notifier: revision_ack_notifier,
|
2021-10-03 11:33:19 +08:00
|
|
|
}
|
2021-10-01 19:39:08 +08:00
|
|
|
}
|
|
|
|
|
2022-03-20 17:17:06 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip_all, fields(object_id) err)]
|
2022-03-12 21:06:15 +08:00
|
|
|
pub async fn load<B>(&mut self, cloud: Option<Arc<dyn RevisionCloudService>>) -> FlowyResult<B::Output>
|
2022-01-14 15:23:21 +08:00
|
|
|
where
|
2022-10-13 23:29:37 +08:00
|
|
|
B: RevisionObjectDeserializer,
|
2022-01-14 15:23:21 +08:00
|
|
|
{
|
2022-01-23 22:33:47 +08:00
|
|
|
let (revisions, rev_id) = RevisionLoader {
|
2022-01-14 15:23:21 +08:00
|
|
|
object_id: self.object_id.clone(),
|
2021-12-18 00:23:26 +08:00
|
|
|
user_id: self.user_id.clone(),
|
2022-03-12 21:06:15 +08:00
|
|
|
cloud,
|
2022-03-01 23:38:26 +08:00
|
|
|
rev_persistence: self.rev_persistence.clone(),
|
2021-12-18 00:23:26 +08:00
|
|
|
}
|
2022-02-19 11:34:31 +08:00
|
|
|
.load()
|
2021-12-18 00:23:26 +08:00
|
|
|
.await?;
|
2022-01-23 22:33:47 +08:00
|
|
|
self.rev_id_counter.set(rev_id);
|
2022-03-20 17:17:06 +08:00
|
|
|
tracing::Span::current().record("object_id", &self.object_id.as_str());
|
2022-10-13 23:29:37 +08:00
|
|
|
B::deserialize_revisions(&self.object_id, revisions)
|
2021-10-07 20:46:29 +08:00
|
|
|
}
|
|
|
|
|
2022-10-22 21:57:44 +08:00
|
|
|
pub async fn load_revisions(&self) -> FlowyResult<Vec<Revision>> {
|
|
|
|
let revisions = RevisionLoader {
|
|
|
|
object_id: self.object_id.clone(),
|
|
|
|
user_id: self.user_id.clone(),
|
|
|
|
cloud: None,
|
|
|
|
rev_persistence: self.rev_persistence.clone(),
|
|
|
|
}
|
|
|
|
.load_revisions()
|
|
|
|
.await?;
|
|
|
|
Ok(revisions)
|
|
|
|
}
|
|
|
|
|
2022-01-01 14:23:58 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, revisions), err)]
|
2022-01-14 15:23:21 +08:00
|
|
|
pub async fn reset_object(&self, revisions: RepeatedRevision) -> FlowyResult<()> {
|
2022-01-02 10:34:42 +08:00
|
|
|
let rev_id = pair_rev_id_from_revisions(&revisions).1;
|
2022-02-25 22:27:44 +08:00
|
|
|
let _ = self.rev_persistence.reset(revisions.into_inner()).await?;
|
2022-01-02 10:34:42 +08:00
|
|
|
self.rev_id_counter.set(rev_id);
|
|
|
|
Ok(())
|
2022-01-01 14:23:58 +08:00
|
|
|
}
|
|
|
|
|
2022-01-02 10:34:42 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, revision), err)]
|
2021-12-14 18:04:51 +08:00
|
|
|
pub async fn add_remote_revision(&self, revision: &Revision) -> Result<(), FlowyError> {
|
2022-10-10 11:56:48 +08:00
|
|
|
if revision.bytes.is_empty() {
|
2022-10-13 23:29:37 +08:00
|
|
|
return Err(FlowyError::internal().context("Remote revisions is empty"));
|
2022-01-01 23:09:13 +08:00
|
|
|
}
|
2022-01-26 23:29:18 +08:00
|
|
|
|
2022-02-25 22:27:44 +08:00
|
|
|
let _ = self.rev_persistence.add_ack_revision(revision).await?;
|
2022-07-20 14:07:54 +08:00
|
|
|
// self.rev_history.add_revision(revision).await;
|
2022-01-07 17:37:11 +08:00
|
|
|
self.rev_id_counter.set(revision.rev_id);
|
2021-12-13 22:46:35 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-03-11 21:36:00 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip_all, err)]
|
2022-06-09 20:58:56 +08:00
|
|
|
pub async fn add_local_revision(&self, revision: &Revision) -> Result<(), FlowyError> {
|
2022-10-10 11:56:48 +08:00
|
|
|
if revision.bytes.is_empty() {
|
2022-10-13 23:29:37 +08:00
|
|
|
return Err(FlowyError::internal().context("Local revisions is empty"));
|
2022-01-01 23:09:13 +08:00
|
|
|
}
|
2022-06-09 20:58:56 +08:00
|
|
|
let rev_id = self
|
|
|
|
.rev_persistence
|
2022-10-13 23:29:37 +08:00
|
|
|
.add_sync_revision(revision, &self.rev_compress)
|
2022-06-09 20:58:56 +08:00
|
|
|
.await?;
|
2022-07-20 14:07:54 +08:00
|
|
|
// self.rev_history.add_revision(revision).await;
|
2022-01-26 23:29:18 +08:00
|
|
|
self.rev_id_counter.set(rev_id);
|
2021-10-07 20:46:29 +08:00
|
|
|
Ok(())
|
2021-10-01 19:39:08 +08:00
|
|
|
}
|
|
|
|
|
2022-01-01 16:16:06 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip(self), err)]
|
2021-12-16 21:31:36 +08:00
|
|
|
pub async fn ack_revision(&self, rev_id: i64) -> Result<(), FlowyError> {
|
2022-02-25 22:27:44 +08:00
|
|
|
if self.rev_persistence.ack_revision(rev_id).await.is_ok() {
|
2022-01-24 21:54:33 +08:00
|
|
|
#[cfg(feature = "flowy_unit_test")]
|
2022-02-18 23:04:55 +08:00
|
|
|
let _ = self.rev_ack_notifier.send(rev_id);
|
2022-01-24 21:54:33 +08:00
|
|
|
}
|
2021-10-01 19:39:08 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-01-24 17:35:58 +08:00
|
|
|
pub fn rev_id(&self) -> i64 {
|
|
|
|
self.rev_id_counter.value()
|
|
|
|
}
|
2021-10-01 19:39:08 +08:00
|
|
|
|
2022-01-02 10:34:42 +08:00
|
|
|
pub fn next_rev_id_pair(&self) -> (i64, i64) {
|
2021-10-01 19:39:08 +08:00
|
|
|
let cur = self.rev_id_counter.value();
|
|
|
|
let next = self.rev_id_counter.next();
|
|
|
|
(cur, next)
|
|
|
|
}
|
|
|
|
|
2021-12-25 21:44:45 +08:00
|
|
|
pub async fn get_revisions_in_range(&self, range: RevisionRange) -> Result<Vec<Revision>, FlowyError> {
|
2022-02-25 22:27:44 +08:00
|
|
|
let revisions = self.rev_persistence.revisions_in_range(&range).await?;
|
2021-12-25 21:44:45 +08:00
|
|
|
Ok(revisions)
|
2021-10-04 14:24:35 +08:00
|
|
|
}
|
2021-12-08 21:51:06 +08:00
|
|
|
|
2022-01-25 20:37:48 +08:00
|
|
|
pub async fn next_sync_revision(&self) -> FlowyResult<Option<Revision>> {
|
2022-10-12 15:41:34 +08:00
|
|
|
self.rev_persistence.next_sync_revision().await
|
2022-01-01 16:16:06 +08:00
|
|
|
}
|
2021-12-18 18:35:45 +08:00
|
|
|
|
2021-12-25 21:44:45 +08:00
|
|
|
pub async fn get_revision(&self, rev_id: i64) -> Option<Revision> {
|
2022-02-25 22:27:44 +08:00
|
|
|
self.rev_persistence.get(rev_id).await.map(|record| record.revision)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 18:59:53 +08:00
|
|
|
impl<Connection: 'static> WSDataProviderDataSource for Arc<RevisionManager<Connection>> {
|
2022-02-25 22:27:44 +08:00
|
|
|
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 21:44:45 +08:00
|
|
|
}
|
2021-10-02 17:19:54 +08:00
|
|
|
}
|
2021-12-08 14:17:40 +08:00
|
|
|
|
2022-01-25 20:37:48 +08:00
|
|
|
#[cfg(feature = "flowy_unit_test")]
|
2022-11-01 18:59:53 +08:00
|
|
|
impl<Connection> RevisionManager<Connection> {
|
|
|
|
pub async fn revision_cache(&self) -> Arc<RevisionPersistence<Connection>> {
|
2022-02-25 22:27:44 +08:00
|
|
|
self.rev_persistence.clone()
|
2022-01-25 20:37:48 +08:00
|
|
|
}
|
2022-02-18 23:04:55 +08:00
|
|
|
pub fn ack_notify(&self) -> tokio::sync::broadcast::Receiver<i64> {
|
|
|
|
self.rev_ack_notifier.subscribe()
|
2022-01-01 16:16:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 18:59:53 +08:00
|
|
|
pub struct RevisionLoader<Connection> {
|
2022-03-01 23:38:26 +08:00
|
|
|
pub object_id: String,
|
|
|
|
pub user_id: String,
|
|
|
|
pub cloud: Option<Arc<dyn RevisionCloudService>>,
|
2022-11-01 18:59:53 +08:00
|
|
|
pub rev_persistence: Arc<RevisionPersistence<Connection>>,
|
2021-12-18 00:23:26 +08:00
|
|
|
}
|
|
|
|
|
2022-11-01 18:59:53 +08:00
|
|
|
impl<Connection: 'static> RevisionLoader<Connection> {
|
2022-03-01 23:38:26 +08:00
|
|
|
pub async fn load(&self) -> Result<(Vec<Revision>, i64), FlowyError> {
|
|
|
|
let records = self.rev_persistence.batch_get(&self.object_id)?;
|
2021-12-18 00:23:26 +08:00
|
|
|
let revisions: Vec<Revision>;
|
2022-01-23 22:33:47 +08:00
|
|
|
let mut rev_id = 0;
|
2022-03-01 23:38:26 +08: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 15:23:21 +08:00
|
|
|
for revision in &remote_revisions {
|
2022-01-23 22:33:47 +08:00
|
|
|
rev_id = revision.rev_id;
|
2022-03-01 23:38:26 +08:00
|
|
|
let _ = self.rev_persistence.add_ack_revision(revision).await?;
|
2022-01-14 15:23:21 +08:00
|
|
|
}
|
|
|
|
revisions = remote_revisions;
|
2021-12-18 00:23:26 +08:00
|
|
|
} else {
|
2022-01-25 20:37:48 +08: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 23:38:26 +08:00
|
|
|
let _ = self.rev_persistence.sync_revision(&record.revision).await?;
|
2022-01-23 22:33:47 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-18 00:23:26 +08:00
|
|
|
revisions = records.into_iter().map(|record| record.revision).collect::<_>();
|
|
|
|
}
|
|
|
|
|
2022-01-23 22:33:47 +08:00
|
|
|
if let Some(revision) = revisions.last() {
|
|
|
|
debug_assert_eq!(rev_id, revision.rev_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((revisions, rev_id))
|
2021-12-18 00:23:26 +08:00
|
|
|
}
|
2022-10-22 21:57:44 +08:00
|
|
|
|
|
|
|
pub async fn load_revisions(&self) -> Result<Vec<Revision>, FlowyError> {
|
|
|
|
let records = self.rev_persistence.batch_get(&self.object_id)?;
|
|
|
|
let revisions = records.into_iter().map(|record| record.revision).collect::<_>();
|
|
|
|
Ok(revisions)
|
|
|
|
}
|
2021-12-18 00:23:26 +08:00
|
|
|
}
|