2021-12-08 14:17:40 +08:00
|
|
|
use crate::{
|
2021-12-14 18:04:51 +08:00
|
|
|
errors::FlowyError,
|
2021-12-13 22:46:35 +08:00
|
|
|
services::doc::revision::{
|
2021-12-18 00:23:26 +08:00
|
|
|
cache::{
|
|
|
|
disk::{Persistence, RevisionDiskCache},
|
|
|
|
memory::{RevisionMemoryCache, RevisionMemoryCacheMissing},
|
|
|
|
sync::RevisionSyncSeq,
|
|
|
|
},
|
2021-12-13 22:46:35 +08:00
|
|
|
RevisionRecord,
|
|
|
|
},
|
2021-12-08 14:17:40 +08:00
|
|
|
};
|
2021-12-18 00:23:26 +08:00
|
|
|
|
2021-12-08 14:17:40 +08:00
|
|
|
use flowy_database::ConnectionPool;
|
2021-12-14 18:04:51 +08:00
|
|
|
use flowy_error::{internal_error, FlowyResult};
|
2021-12-13 13:55:44 +08:00
|
|
|
use lib_infra::future::FutureResult;
|
2021-12-08 14:17:40 +08:00
|
|
|
use lib_ot::{
|
2021-12-18 00:23:26 +08:00
|
|
|
core::Operation,
|
|
|
|
revision::{RevState, Revision, RevisionRange},
|
2021-12-08 14:17:40 +08:00
|
|
|
rich_text::RichTextDelta,
|
|
|
|
};
|
2021-12-18 00:23:26 +08:00
|
|
|
use std::sync::Arc;
|
2021-12-08 14:17:40 +08:00
|
|
|
use tokio::{
|
2021-12-18 00:23:26 +08:00
|
|
|
sync::RwLock,
|
2021-12-08 14:17:40 +08:00
|
|
|
task::{spawn_blocking, JoinHandle},
|
|
|
|
};
|
|
|
|
|
2021-12-18 00:23:26 +08:00
|
|
|
type DocRevisionDiskCache = dyn RevisionDiskCache<Error = FlowyError>;
|
2021-12-08 14:17:40 +08:00
|
|
|
|
|
|
|
pub struct RevisionCache {
|
|
|
|
doc_id: String,
|
2021-12-18 00:23:26 +08:00
|
|
|
pub disk_cache: Arc<DocRevisionDiskCache>,
|
2021-12-08 14:17:40 +08:00
|
|
|
memory_cache: Arc<RevisionMemoryCache>,
|
2021-12-18 00:23:26 +08:00
|
|
|
sync_seq: Arc<RevisionSyncSeq>,
|
2021-12-08 14:17:40 +08:00
|
|
|
defer_save: RwLock<Option<JoinHandle<()>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RevisionCache {
|
2021-12-18 00:23:26 +08:00
|
|
|
pub fn new(user_id: &str, doc_id: &str, pool: Arc<ConnectionPool>) -> RevisionCache {
|
|
|
|
let disk_cache = Arc::new(Persistence::new(user_id, pool));
|
|
|
|
let memory_cache = Arc::new(RevisionMemoryCache::new(doc_id, Arc::new(disk_cache.clone())));
|
|
|
|
let sync_seq = Arc::new(RevisionSyncSeq::new());
|
2021-12-08 14:17:40 +08:00
|
|
|
let doc_id = doc_id.to_owned();
|
|
|
|
Self {
|
|
|
|
doc_id,
|
2021-12-18 00:23:26 +08:00
|
|
|
disk_cache,
|
2021-12-08 14:17:40 +08:00
|
|
|
memory_cache,
|
2021-12-18 00:23:26 +08:00
|
|
|
sync_seq,
|
2021-12-08 14:17:40 +08:00
|
|
|
defer_save: RwLock::new(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip(self, revision))]
|
2021-12-14 18:04:51 +08:00
|
|
|
pub async fn add_local_revision(&self, revision: Revision) -> FlowyResult<()> {
|
2021-12-08 14:17:40 +08:00
|
|
|
if self.memory_cache.contains(&revision.rev_id) {
|
2021-12-14 18:04:51 +08:00
|
|
|
return Err(FlowyError::internal().context(format!("Duplicate revision id: {}", revision.rev_id)));
|
2021-12-08 14:17:40 +08:00
|
|
|
}
|
2021-12-13 22:46:35 +08:00
|
|
|
let record = RevisionRecord {
|
|
|
|
revision,
|
2021-12-14 15:31:44 +08:00
|
|
|
state: RevState::StateLocal,
|
2021-12-13 22:46:35 +08:00
|
|
|
};
|
2021-12-18 00:23:26 +08:00
|
|
|
let _ = self.memory_cache.add_revision(&record).await;
|
|
|
|
self.sync_seq.add_revision(record).await?;
|
2021-12-13 22:46:35 +08:00
|
|
|
self.save_revisions().await;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip(self, revision))]
|
2021-12-14 18:04:51 +08:00
|
|
|
pub async fn add_remote_revision(&self, revision: Revision) -> FlowyResult<()> {
|
2021-12-13 22:46:35 +08:00
|
|
|
if self.memory_cache.contains(&revision.rev_id) {
|
2021-12-14 18:04:51 +08:00
|
|
|
return Err(FlowyError::internal().context(format!("Duplicate revision id: {}", revision.rev_id)));
|
2021-12-13 22:46:35 +08:00
|
|
|
}
|
|
|
|
let record = RevisionRecord {
|
|
|
|
revision,
|
2021-12-14 15:31:44 +08:00
|
|
|
state: RevState::StateLocal,
|
2021-12-13 22:46:35 +08:00
|
|
|
};
|
2021-12-18 00:23:26 +08:00
|
|
|
self.memory_cache.add_revision(&record).await;
|
2021-12-08 14:17:40 +08:00
|
|
|
self.save_revisions().await;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-09 19:01:58 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, rev_id), fields(rev_id = %rev_id))]
|
|
|
|
pub async fn ack_revision(&self, rev_id: i64) {
|
2021-12-18 00:23:26 +08:00
|
|
|
self.sync_seq.ack_revision(&rev_id).await;
|
2021-12-08 14:17:40 +08:00
|
|
|
self.save_revisions().await;
|
|
|
|
}
|
|
|
|
|
2021-12-18 00:23:26 +08:00
|
|
|
pub async fn get_revision(&self, _doc_id: &str, rev_id: i64) -> Option<RevisionRecord> {
|
|
|
|
self.memory_cache.get_revision(&rev_id).await
|
2021-12-09 19:01:58 +08:00
|
|
|
}
|
|
|
|
|
2021-12-08 14:17:40 +08:00
|
|
|
async fn save_revisions(&self) {
|
2021-12-18 00:23:26 +08:00
|
|
|
// https://github.com/async-graphql/async-graphql/blob/ed8449beec3d9c54b94da39bab33cec809903953/src/dataloader/mod.rs#L362
|
2021-12-08 14:17:40 +08:00
|
|
|
if let Some(handler) = self.defer_save.write().await.take() {
|
|
|
|
handler.abort();
|
|
|
|
}
|
|
|
|
|
2021-12-18 00:23:26 +08:00
|
|
|
// if self.sync_seq.is_empty() {
|
|
|
|
// return;
|
|
|
|
// }
|
2021-12-08 14:17:40 +08:00
|
|
|
|
2021-12-18 00:23:26 +08:00
|
|
|
// let memory_cache = self.sync_seq.clone();
|
|
|
|
// let disk_cache = self.disk_cache.clone();
|
|
|
|
// *self.defer_save.write().await = Some(tokio::spawn(async move {
|
|
|
|
// tokio::time::sleep(Duration::from_millis(300)).await;
|
|
|
|
// let (ids, records) = memory_cache.revisions();
|
|
|
|
// match disk_cache.create_revisions(records) {
|
|
|
|
// Ok(_) => {
|
|
|
|
// memory_cache.remove_revisions(ids);
|
|
|
|
// },
|
|
|
|
// Err(e) => log::error!("Save revision failed: {:?}", e),
|
|
|
|
// }
|
|
|
|
// }));
|
2021-12-08 14:17:40 +08:00
|
|
|
}
|
|
|
|
|
2021-12-14 18:04:51 +08:00
|
|
|
pub async fn revisions_in_range(&self, range: RevisionRange) -> FlowyResult<Vec<Revision>> {
|
2021-12-18 00:23:26 +08:00
|
|
|
let records = self.memory_cache.get_revisions_in_range(&range).await?;
|
|
|
|
Ok(records
|
|
|
|
.into_iter()
|
|
|
|
.map(|record| record.revision)
|
|
|
|
.collect::<Vec<Revision>>())
|
2021-12-08 14:17:40 +08:00
|
|
|
}
|
|
|
|
|
2021-12-16 21:31:36 +08:00
|
|
|
pub(crate) fn next_revision(&self) -> FutureResult<Option<Revision>, FlowyError> {
|
2021-12-18 00:23:26 +08:00
|
|
|
let sync_seq = self.sync_seq.clone();
|
|
|
|
let disk_cache = self.disk_cache.clone();
|
2021-12-08 14:17:40 +08:00
|
|
|
let doc_id = self.doc_id.clone();
|
2021-12-13 13:55:44 +08:00
|
|
|
FutureResult::new(async move {
|
2021-12-18 00:23:26 +08:00
|
|
|
match sync_seq.next_sync_revision().await {
|
|
|
|
None => match sync_seq.next_sync_rev_id().await {
|
2021-12-15 16:28:18 +08:00
|
|
|
None => Ok(None),
|
|
|
|
Some(rev_id) => match disk_cache.read_revision(&doc_id, rev_id)? {
|
2021-12-08 14:17:40 +08:00
|
|
|
None => Ok(None),
|
2021-12-16 21:31:36 +08:00
|
|
|
Some(record) => Ok(Some(record.revision)),
|
2021-12-15 16:28:18 +08:00
|
|
|
},
|
2021-12-08 14:17:40 +08:00
|
|
|
},
|
2021-12-16 21:31:36 +08:00
|
|
|
Some((_, record)) => Ok(Some(record.revision)),
|
2021-12-08 14:17:40 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-18 00:23:26 +08:00
|
|
|
impl RevisionMemoryCacheMissing for Arc<Persistence> {
|
|
|
|
fn get_revision_record(&self, doc_id: &str, rev_id: i64) -> Result<Option<RevisionRecord>, FlowyError> {
|
|
|
|
match self.read_revision(&doc_id, rev_id)? {
|
|
|
|
None => {
|
|
|
|
tracing::warn!("Can't find revision in {} with rev_id: {}", doc_id, rev_id);
|
|
|
|
Ok(None)
|
|
|
|
},
|
|
|
|
Some(record) => Ok(Some(record)),
|
2021-12-08 17:33:22 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-08 14:17:40 +08:00
|
|
|
|
2021-12-18 00:23:26 +08:00
|
|
|
fn get_revision_records_with_range(
|
|
|
|
&self,
|
|
|
|
doc_id: &str,
|
|
|
|
range: RevisionRange,
|
|
|
|
) -> FutureResult<Vec<RevisionRecord>, FlowyError> {
|
|
|
|
let disk_cache = self.clone();
|
|
|
|
let doc_id = doc_id.to_owned();
|
|
|
|
FutureResult::new(async move {
|
|
|
|
let records = spawn_blocking(move || disk_cache.revisions_in_range(&doc_id, &range))
|
|
|
|
.await
|
|
|
|
.map_err(internal_error)??;
|
2021-12-08 14:17:40 +08:00
|
|
|
|
2021-12-18 00:23:26 +08:00
|
|
|
Ok::<Vec<RevisionRecord>, FlowyError>(records)
|
2021-12-08 14:17:40 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-12-08 21:51:06 +08:00
|
|
|
|
|
|
|
#[cfg(feature = "flowy_unit_test")]
|
|
|
|
impl RevisionCache {
|
2021-12-18 00:23:26 +08:00
|
|
|
pub fn disk_cache(&self) -> Arc<DocRevisionDiskCache> { self.disk_cache.clone() }
|
2021-12-08 21:51:06 +08:00
|
|
|
|
2021-12-18 00:23:26 +08:00
|
|
|
pub fn memory_cache(&self) -> Arc<RevisionSyncSeq> { self.sync_seq.clone() }
|
2021-12-08 21:51:06 +08:00
|
|
|
}
|